Skip to content

Commit d4128a1

Browse files
keesgregkh
authored andcommitted
pstore: Convert buf_lock to semaphore
commit ea84b58 upstream. Instead of running with interrupts disabled, use a semaphore. This should make it easier for backends that may need to sleep (e.g. EFI) when performing a write: |BUG: sleeping function called from invalid context at kernel/sched/completion.c:99 |in_atomic(): 1, irqs_disabled(): 1, pid: 2236, name: sig-xstate-bum |Preemption disabled at: |[<ffffffff99d60512>] pstore_dump+0x72/0x330 |CPU: 26 PID: 2236 Comm: sig-xstate-bum Tainted: G D 4.20.0-rc3 #45 |Call Trace: | dump_stack+0x4f/0x6a | ___might_sleep.cold.91+0xd3/0xe4 | __might_sleep+0x50/0x90 | wait_for_completion+0x32/0x130 | virt_efi_query_variable_info+0x14e/0x160 | efi_query_variable_store+0x51/0x1a0 | efivar_entry_set_safe+0xa3/0x1b0 | efi_pstore_write+0x109/0x140 | pstore_dump+0x11c/0x330 | kmsg_dump+0xa4/0xd0 | oops_exit+0x22/0x30 ... Reported-by: Sebastian Andrzej Siewior <[email protected]> Fixes: 21b3ddd ("efi: Don't use spinlocks for efi vars") Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c63ce71 commit d4128a1

File tree

6 files changed

+27
-32
lines changed

6 files changed

+27
-32
lines changed

arch/powerpc/kernel/nvram_64.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,6 @@ static int nvram_pstore_init(void)
563563
nvram_pstore_info.buf = oops_data;
564564
nvram_pstore_info.bufsize = oops_data_sz;
565565

566-
spin_lock_init(&nvram_pstore_info.buf_lock);
567-
568566
rc = pstore_register(&nvram_pstore_info);
569567
if (rc && (rc != -EPERM))
570568
/* Print error only when pstore.backend == nvram */

drivers/acpi/apei/erst.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,6 @@ static int __init erst_init(void)
11761176
"Error Record Serialization Table (ERST) support is initialized.\n");
11771177

11781178
buf = kmalloc(erst_erange.size, GFP_KERNEL);
1179-
spin_lock_init(&erst_info.buf_lock);
11801179
if (buf) {
11811180
erst_info.buf = buf + sizeof(struct cper_pstore_record);
11821181
erst_info.bufsize = erst_erange.size -

drivers/firmware/efi/efi-pstore.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ static int efi_pstore_write(struct pstore_record *record)
259259
efi_name[i] = name[i];
260260

261261
ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
262-
!pstore_cannot_block_path(record->reason),
263-
record->size, record->psi->buf);
262+
preemptible(), record->size, record->psi->buf);
264263

265264
if (record->reason == KMSG_DUMP_OOPS)
266265
efivar_run_worker();
@@ -369,7 +368,6 @@ static __init int efivars_pstore_init(void)
369368
return -ENOMEM;
370369

371370
efi_pstore_info.bufsize = 1024;
372-
spin_lock_init(&efi_pstore_info.buf_lock);
373371

374372
if (pstore_register(&efi_pstore_info)) {
375373
kfree(efi_pstore_info.buf);

fs/pstore/platform.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,27 @@ static const char *get_reason_str(enum kmsg_dump_reason reason)
124124
}
125125
}
126126

127-
bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
127+
/*
128+
* Should pstore_dump() wait for a concurrent pstore_dump()? If
129+
* not, the current pstore_dump() will report a failure to dump
130+
* and return.
131+
*/
132+
static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
128133
{
129-
/*
130-
* In case of NMI path, pstore shouldn't be blocked
131-
* regardless of reason.
132-
*/
134+
/* In NMI path, pstore shouldn't block regardless of reason. */
133135
if (in_nmi())
134136
return true;
135137

136138
switch (reason) {
137139
/* In panic case, other cpus are stopped by smp_send_stop(). */
138140
case KMSG_DUMP_PANIC:
139-
/* Emergency restart shouldn't be blocked by spin lock. */
141+
/* Emergency restart shouldn't be blocked. */
140142
case KMSG_DUMP_EMERG:
141143
return true;
142144
default:
143145
return false;
144146
}
145147
}
146-
EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
147148

148149
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
149150
static int zbufsize_deflate(size_t size)
@@ -378,23 +379,23 @@ static void pstore_dump(struct kmsg_dumper *dumper,
378379
unsigned long total = 0;
379380
const char *why;
380381
unsigned int part = 1;
381-
unsigned long flags = 0;
382-
int is_locked;
383382
int ret;
384383

385384
why = get_reason_str(reason);
386385

387-
if (pstore_cannot_block_path(reason)) {
388-
is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
389-
if (!is_locked) {
390-
pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
391-
, in_nmi() ? "NMI" : why);
386+
if (down_trylock(&psinfo->buf_lock)) {
387+
/* Failed to acquire lock: give up if we cannot wait. */
388+
if (pstore_cannot_wait(reason)) {
389+
pr_err("dump skipped in %s path: may corrupt error record\n",
390+
in_nmi() ? "NMI" : why);
391+
return;
392+
}
393+
if (down_interruptible(&psinfo->buf_lock)) {
394+
pr_err("could not grab semaphore?!\n");
392395
return;
393396
}
394-
} else {
395-
spin_lock_irqsave(&psinfo->buf_lock, flags);
396-
is_locked = 1;
397397
}
398+
398399
oopscount++;
399400
while (total < kmsg_bytes) {
400401
char *dst;
@@ -411,7 +412,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
411412
record.part = part;
412413
record.buf = psinfo->buf;
413414

414-
if (big_oops_buf && is_locked) {
415+
if (big_oops_buf) {
415416
dst = big_oops_buf;
416417
dst_size = big_oops_buf_sz;
417418
} else {
@@ -429,7 +430,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
429430
dst_size, &dump_size))
430431
break;
431432

432-
if (big_oops_buf && is_locked) {
433+
if (big_oops_buf) {
433434
zipped_len = pstore_compress(dst, psinfo->buf,
434435
header_size + dump_size,
435436
psinfo->bufsize);
@@ -452,8 +453,8 @@ static void pstore_dump(struct kmsg_dumper *dumper,
452453
total += record.size;
453454
part++;
454455
}
455-
if (is_locked)
456-
spin_unlock_irqrestore(&psinfo->buf_lock, flags);
456+
457+
up(&psinfo->buf_lock);
457458
}
458459

459460
static struct kmsg_dumper pstore_dumper = {
@@ -572,6 +573,7 @@ int pstore_register(struct pstore_info *psi)
572573
psi->write_user = pstore_write_user_compat;
573574
psinfo = psi;
574575
mutex_init(&psinfo->read_mutex);
576+
sema_init(&psinfo->buf_lock, 1);
575577
spin_unlock(&pstore_lock);
576578

577579
if (owner && !try_module_get(owner)) {

fs/pstore/ram.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,6 @@ static int ramoops_probe(struct platform_device *pdev)
814814
err = -ENOMEM;
815815
goto fail_clear;
816816
}
817-
spin_lock_init(&cxt->pstore.buf_lock);
818817

819818
cxt->pstore.flags = PSTORE_FLAGS_DMESG;
820819
if (cxt->console_size)

include/linux/pstore.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <linux/errno.h>
2727
#include <linux/kmsg_dump.h>
2828
#include <linux/mutex.h>
29-
#include <linux/spinlock.h>
29+
#include <linux/semaphore.h>
3030
#include <linux/time.h>
3131
#include <linux/types.h>
3232

@@ -88,7 +88,7 @@ struct pstore_record {
8888
* @owner: module which is repsonsible for this backend driver
8989
* @name: name of the backend driver
9090
*
91-
* @buf_lock: spinlock to serialize access to @buf
91+
* @buf_lock: semaphore to serialize access to @buf
9292
* @buf: preallocated crash dump buffer
9393
* @bufsize: size of @buf available for crash dump bytes (must match
9494
* smallest number of bytes available for writing to a
@@ -173,7 +173,7 @@ struct pstore_info {
173173
struct module *owner;
174174
char *name;
175175

176-
spinlock_t buf_lock;
176+
struct semaphore buf_lock;
177177
char *buf;
178178
size_t bufsize;
179179

@@ -199,7 +199,6 @@ struct pstore_info {
199199

200200
extern int pstore_register(struct pstore_info *);
201201
extern void pstore_unregister(struct pstore_info *);
202-
extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
203202

204203
struct pstore_ftrace_record {
205204
unsigned long ip;

0 commit comments

Comments
 (0)