Skip to content

Commit f72ecfe

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 <bigeasy@linutronix.de> Fixes: 21b3ddd ("efi: Don't use spinlocks for efi vars") Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d80d6f6 commit f72ecfe

File tree

6 files changed

+27
-32
lines changed

6 files changed

+27
-32
lines changed

arch/powerpc/kernel/nvram_64.c

-2
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,6 @@ static int nvram_pstore_init(void)
566566
nvram_pstore_info.buf = oops_data;
567567
nvram_pstore_info.bufsize = oops_data_sz;
568568

569-
spin_lock_init(&nvram_pstore_info.buf_lock);
570-
571569
rc = pstore_register(&nvram_pstore_info);
572570
if (rc && (rc != -EPERM))
573571
/* Print error only when pstore.backend == nvram */

drivers/acpi/apei/erst.c

-1
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,6 @@ static int __init erst_init(void)
11751175
"Error Record Serialization Table (ERST) support is initialized.\n");
11761176

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

drivers/firmware/efi/efi-pstore.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ static int efi_pstore_write(struct pstore_record *record)
258258
efi_name[i] = name[i];
259259

260260
ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES,
261-
!pstore_cannot_block_path(record->reason),
262-
record->size, record->psi->buf);
261+
preemptible(), record->size, record->psi->buf);
263262

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

370369
efi_pstore_info.bufsize = 1024;
371-
spin_lock_init(&efi_pstore_info.buf_lock);
372370

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

fs/pstore/platform.c

+23-21
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,27 @@ static const char *get_reason_str(enum kmsg_dump_reason reason)
129129
}
130130
}
131131

132-
bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
132+
/*
133+
* Should pstore_dump() wait for a concurrent pstore_dump()? If
134+
* not, the current pstore_dump() will report a failure to dump
135+
* and return.
136+
*/
137+
static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
133138
{
134-
/*
135-
* In case of NMI path, pstore shouldn't be blocked
136-
* regardless of reason.
137-
*/
139+
/* In NMI path, pstore shouldn't block regardless of reason. */
138140
if (in_nmi())
139141
return true;
140142

141143
switch (reason) {
142144
/* In panic case, other cpus are stopped by smp_send_stop(). */
143145
case KMSG_DUMP_PANIC:
144-
/* Emergency restart shouldn't be blocked by spin lock. */
146+
/* Emergency restart shouldn't be blocked. */
145147
case KMSG_DUMP_EMERG:
146148
return true;
147149
default:
148150
return false;
149151
}
150152
}
151-
EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
152153

153154
#ifdef CONFIG_PSTORE_ZLIB_COMPRESS
154155
/* Derived from logfs_compress() */
@@ -499,23 +500,23 @@ static void pstore_dump(struct kmsg_dumper *dumper,
499500
unsigned long total = 0;
500501
const char *why;
501502
unsigned int part = 1;
502-
unsigned long flags = 0;
503-
int is_locked;
504503
int ret;
505504

506505
why = get_reason_str(reason);
507506

508-
if (pstore_cannot_block_path(reason)) {
509-
is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
510-
if (!is_locked) {
511-
pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
512-
, in_nmi() ? "NMI" : why);
507+
if (down_trylock(&psinfo->buf_lock)) {
508+
/* Failed to acquire lock: give up if we cannot wait. */
509+
if (pstore_cannot_wait(reason)) {
510+
pr_err("dump skipped in %s path: may corrupt error record\n",
511+
in_nmi() ? "NMI" : why);
512+
return;
513+
}
514+
if (down_interruptible(&psinfo->buf_lock)) {
515+
pr_err("could not grab semaphore?!\n");
513516
return;
514517
}
515-
} else {
516-
spin_lock_irqsave(&psinfo->buf_lock, flags);
517-
is_locked = 1;
518518
}
519+
519520
oopscount++;
520521
while (total < kmsg_bytes) {
521522
char *dst;
@@ -532,7 +533,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
532533
record.part = part;
533534
record.buf = psinfo->buf;
534535

535-
if (big_oops_buf && is_locked) {
536+
if (big_oops_buf) {
536537
dst = big_oops_buf;
537538
dst_size = big_oops_buf_sz;
538539
} else {
@@ -550,7 +551,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
550551
dst_size, &dump_size))
551552
break;
552553

553-
if (big_oops_buf && is_locked) {
554+
if (big_oops_buf) {
554555
zipped_len = pstore_compress(dst, psinfo->buf,
555556
header_size + dump_size,
556557
psinfo->bufsize);
@@ -573,8 +574,8 @@ static void pstore_dump(struct kmsg_dumper *dumper,
573574
total += record.size;
574575
part++;
575576
}
576-
if (is_locked)
577-
spin_unlock_irqrestore(&psinfo->buf_lock, flags);
577+
578+
up(&psinfo->buf_lock);
578579
}
579580

580581
static struct kmsg_dumper pstore_dumper = {
@@ -693,6 +694,7 @@ int pstore_register(struct pstore_info *psi)
693694
psi->write_user = pstore_write_user_compat;
694695
psinfo = psi;
695696
mutex_init(&psinfo->read_mutex);
697+
sema_init(&psinfo->buf_lock, 1);
696698
spin_unlock(&pstore_lock);
697699

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

fs/pstore/ram.c

-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,6 @@ static int ramoops_probe(struct platform_device *pdev)
812812
err = -ENOMEM;
813813
goto fail_clear;
814814
}
815-
spin_lock_init(&cxt->pstore.buf_lock);
816815

817816
cxt->pstore.flags = PSTORE_FLAGS_DMESG;
818817
if (cxt->console_size)

include/linux/pstore.h

+3-4
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)