Message ID | 20220218181950.1438236-1-jannh@google.com |
---|---|
State | New |
Headers | show |
Series | [v2] pstore: Don't use semaphores in always-atomic-context code | expand |
On February 18, 2022 10:19:50 AM PST, Jann Horn <jannh@google.com> wrote: >pstore_dump() is *always* invoked in atomic context (nowadays in an RCU >read-side critical section, before that under a spinlock). >It doesn't make sense to try to use semaphores here. Ah, very nice. Thanks for the analysis! >[...] >-static bool pstore_cannot_wait(enum kmsg_dump_reason reason) >+bool pstore_cannot_block_path(enum kmsg_dump_reason reason) Why the rename, extern, and EXPORT? This appears to still only have the same single caller? > [...] >- pr_err("dump skipped in %s path: may corrupt error record\n", >- in_nmi() ? "NMI" : why); >- return; >- } >- if (down_interruptible(&psinfo->buf_lock)) { >- pr_err("could not grab semaphore?!\n"); >+ if (pstore_cannot_block_path(reason)) { >+ if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { >+ pr_err("dump skipped in %s path because of concurrent dump\n" >+ , in_nmi() ? "NMI" : why); The pr_err had the comma following the format string moved, and the note about corruption removed. Is that no longer accurate? Otherwise looks good; thank you!
On Wed, Feb 23, 2022 at 8:50 AM Kees Cook <keescook@chromium.org> wrote: > On February 18, 2022 10:19:50 AM PST, Jann Horn <jannh@google.com> wrote: > >pstore_dump() is *always* invoked in atomic context (nowadays in an RCU > >read-side critical section, before that under a spinlock). > >It doesn't make sense to try to use semaphores here. > > Ah, very nice. Thanks for the analysis! > > >[...] > >-static bool pstore_cannot_wait(enum kmsg_dump_reason reason) > >+bool pstore_cannot_block_path(enum kmsg_dump_reason reason) > > Why the rename, That's one of the parts of commit ea84b580b955 that I included in the revert. "wait" in the name is not accurate, since "wait" in the kernel normally refers to scheduling away until some condition is fulfilled. (Though I guess "block" also isn't the best name either... idk.) The place where we might want to have different behavior depending on whether we're handling a kernel crash are spinlocks; during a kernel crash, we shouldn't deadlock on them, but otherwise, AFAIK it's fine to block on them. > extern, and EXPORT? This appears to still only have the same single caller? Also part of the revert. I figured it might make sense to also revert that part because: With this commit applied, the EFI code will always take the "nonblock" path for now, but that's kinda suboptimal; on some platforms the "blocking" path uses a semaphore, so we really can't take that, but on x86 it uses a spinlock, which we could block on if we're not oopsing. We could avoid needlessly losing non-crash dmesg dumps there; I don't know whether we care about that though. So I figured that we might want to start adding new callers to this later on. But if you want, I'll remove that part of the revert and resend? > > [...] > >- pr_err("dump skipped in %s path: may corrupt error record\n", > >- in_nmi() ? "NMI" : why); > >- return; > >- } > >- if (down_interruptible(&psinfo->buf_lock)) { > >- pr_err("could not grab semaphore?!\n"); > >+ if (pstore_cannot_block_path(reason)) { > >+ if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { > >+ pr_err("dump skipped in %s path because of concurrent dump\n" > >+ , in_nmi() ? "NMI" : why); > > The pr_err had the comma following the format string moved, Ah, whoops, that was also part of the revert, but I guess I should have left that part out... > and the note about corruption removed. Is that no longer accurate? There should be no more corruption since commit 959217c84c27 ("pstore: Actually give up during locking failure") - if we're bailing out, we can't be causing corruption, I believe?
On Wed, Feb 23, 2022 at 06:50:52PM +0100, Jann Horn wrote: > On Wed, Feb 23, 2022 at 8:50 AM Kees Cook <keescook@chromium.org> wrote: > > On February 18, 2022 10:19:50 AM PST, Jann Horn <jannh@google.com> wrote: > > >pstore_dump() is *always* invoked in atomic context (nowadays in an RCU > > >read-side critical section, before that under a spinlock). > > >It doesn't make sense to try to use semaphores here. > > > > Ah, very nice. Thanks for the analysis! > > > > >[...] > > >-static bool pstore_cannot_wait(enum kmsg_dump_reason reason) > > >+bool pstore_cannot_block_path(enum kmsg_dump_reason reason) > > > > Why the rename, > > That's one of the parts of commit ea84b580b955 that I included in the > revert. "wait" in the name is not accurate, since "wait" in the kernel > normally refers to scheduling away until some condition is fulfilled. > (Though I guess "block" also isn't the best name either... idk.) The > place where we might want to have different behavior depending on > whether we're handling a kernel crash are spinlocks; during a kernel > crash, we shouldn't deadlock on them, but otherwise, AFAIK it's fine > to block on them. Gotcha. I'm find to avoid "wait"; I was just curious why it was changing, but I see now. > > > extern, and EXPORT? This appears to still only have the same single caller? > > Also part of the revert. I figured it might make sense to also revert > that part because: > > With this commit applied, the EFI code will always take the "nonblock" > path for now, but that's kinda suboptimal; on some platforms the > "blocking" path uses a semaphore, so we really can't take that, but on > x86 it uses a spinlock, which we could block on if we're not oopsing. > We could avoid needlessly losing non-crash dmesg dumps there; I don't > know whether we care about that though. > > So I figured that we might want to start adding new callers to this > later on. But if you want, I'll remove that part of the revert and > resend? Yeah, let's just keep this static -- there's no reason to export it. > > > > [...] > > >- pr_err("dump skipped in %s path: may corrupt error record\n", > > >- in_nmi() ? "NMI" : why); > > >- return; > > >- } > > >- if (down_interruptible(&psinfo->buf_lock)) { > > >- pr_err("could not grab semaphore?!\n"); > > >+ if (pstore_cannot_block_path(reason)) { > > >+ if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { > > >+ pr_err("dump skipped in %s path because of concurrent dump\n" > > >+ , in_nmi() ? "NMI" : why); > > > > The pr_err had the comma following the format string moved, > > Ah, whoops, that was also part of the revert, but I guess I should > have left that part out... > > > and the note about corruption removed. Is that no longer accurate? > > There should be no more corruption since commit 959217c84c27 ("pstore: > Actually give up during locking failure") - if we're bailing out, we > can't be causing corruption, I believe? Yeah, agreed. String content change is fine, the weird leading comma I'd like to do without. :) Thanks!
diff --git a/drivers/firmware/efi/efi-pstore.c b/drivers/firmware/efi/efi-pstore.c index 0ef086e43090..7e771c56c13c 100644 --- a/drivers/firmware/efi/efi-pstore.c +++ b/drivers/firmware/efi/efi-pstore.c @@ -266,7 +266,7 @@ static int efi_pstore_write(struct pstore_record *record) efi_name[i] = name[i]; ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES, - preemptible(), record->size, record->psi->buf); + false, record->size, record->psi->buf); if (record->reason == KMSG_DUMP_OOPS && try_module_get(THIS_MODULE)) if (!schedule_work(&efivar_work)) diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c index f243cb5e6a4f..31d5f9331210 100644 --- a/fs/pstore/platform.c +++ b/fs/pstore/platform.c @@ -143,27 +143,29 @@ static void pstore_timer_kick(void) mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); } -/* - * Should pstore_dump() wait for a concurrent pstore_dump()? If - * not, the current pstore_dump() will report a failure to dump - * and return. - */ -static bool pstore_cannot_wait(enum kmsg_dump_reason reason) +bool pstore_cannot_block_path(enum kmsg_dump_reason reason) { - /* In NMI path, pstore shouldn't block regardless of reason. */ + /* + * In case of NMI path, pstore shouldn't be blocked + * regardless of reason. + */ if (in_nmi()) return true; switch (reason) { /* In panic case, other cpus are stopped by smp_send_stop(). */ case KMSG_DUMP_PANIC: - /* Emergency restart shouldn't be blocked. */ + /* + * Emergency restart shouldn't be blocked by spinning on + * pstore_info::buf_lock. + */ case KMSG_DUMP_EMERG: return true; default: return false; } } +EXPORT_SYMBOL_GPL(pstore_cannot_block_path); #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) static int zbufsize_deflate(size_t size) @@ -389,21 +391,19 @@ static void pstore_dump(struct kmsg_dumper *dumper, unsigned long total = 0; const char *why; unsigned int part = 1; + unsigned long flags = 0; int ret; why = kmsg_dump_reason_str(reason); - if (down_trylock(&psinfo->buf_lock)) { - /* Failed to acquire lock: give up if we cannot wait. */ - if (pstore_cannot_wait(reason)) { - pr_err("dump skipped in %s path: may corrupt error record\n", - in_nmi() ? "NMI" : why); - return; - } - if (down_interruptible(&psinfo->buf_lock)) { - pr_err("could not grab semaphore?!\n"); + if (pstore_cannot_block_path(reason)) { + if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) { + pr_err("dump skipped in %s path because of concurrent dump\n" + , in_nmi() ? "NMI" : why); return; } + } else { + spin_lock_irqsave(&psinfo->buf_lock, flags); } kmsg_dump_rewind(&iter); @@ -467,8 +467,7 @@ static void pstore_dump(struct kmsg_dumper *dumper, total += record.size; part++; } - - up(&psinfo->buf_lock); + spin_unlock_irqrestore(&psinfo->buf_lock, flags); } static struct kmsg_dumper pstore_dumper = { @@ -594,7 +593,7 @@ int pstore_register(struct pstore_info *psi) psi->write_user = pstore_write_user_compat; psinfo = psi; mutex_init(&psinfo->read_mutex); - sema_init(&psinfo->buf_lock, 1); + spin_lock_init(&psinfo->buf_lock); if (psi->flags & PSTORE_FLAGS_DMESG) allocate_buf_for_compression(); diff --git a/include/linux/pstore.h b/include/linux/pstore.h index eb93a54cff31..e6bd205ddc63 100644 --- a/include/linux/pstore.h +++ b/include/linux/pstore.h @@ -14,7 +14,7 @@ #include <linux/errno.h> #include <linux/kmsg_dump.h> #include <linux/mutex.h> -#include <linux/semaphore.h> +#include <linux/spinlock.h> #include <linux/time.h> #include <linux/types.h> @@ -87,7 +87,7 @@ struct pstore_record { * @owner: module which is responsible for this backend driver * @name: name of the backend driver * - * @buf_lock: semaphore to serialize access to @buf + * @buf_lock: spinlock to serialize access to @buf * @buf: preallocated crash dump buffer * @bufsize: size of @buf available for crash dump bytes (must match * smallest number of bytes available for writing to a @@ -178,7 +178,7 @@ struct pstore_info { struct module *owner; const char *name; - struct semaphore buf_lock; + spinlock_t buf_lock; char *buf; size_t bufsize; @@ -205,6 +205,7 @@ struct pstore_info { extern int pstore_register(struct pstore_info *); extern void pstore_unregister(struct pstore_info *); +extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason); struct pstore_ftrace_record { unsigned long ip;