pstore: change mutex locking to spin_locks
Subsystems (3)
PATCH 1/1 pstore: change mutex locking to spin_locks
pstore was using mutex locking to protect read/write access to the backend plug-ins. This causes problems when pstore is executed in an NMI context through panic() -> kmsg_dump().
This patch changes the mutex to a spin_lock_irqsave then also checks to see if we are in an NMI context. If we are in an NMI and can't get the lock, just print a message stating that and blow by the locking.
All this is probably a hack around the bigger locking problem but it solves my current situation of trying to sleep in an NMI context.
Tested by loading the lkdtm module and executing a HARDLOCKUP which will cause the machine to panic inside the nmi handler.
--- drivers/acpi/apei/erst.c | 2 +- fs/pstore/platform.c | 28 ++++++++++++++++++++++------ include/linux/pstore.h | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-)
drivers/acpi/apei/erst.c +1 -1
@@ -1155,7 +1155,7 @@ static int __init erst_init(void) goto err_release_erange; buf =kmalloc (erst_erange.size,GFP_KERNEL ); -mutex_init (&erst_info.buf_mutex); +spin_lock_init (&erst_info.buf_lock);if (buf) { erst_info.buf = buf +sizeof (struct cper_pstore_record ); erst_info.bufsize = erst_erange.size -
fs/pstore/platform.c +22 -6
@@ -27,6 +27,7 @@#include <linux/string.h> #include <linux/slab.h> #include <linux/uaccess.h> +#include <linux/hardirq.h> #include "internal.h"
@@ -68,13 +69,21 @@ static void pstore_dump(struct kmsg_dumper *dumper,char *dst, *why;u64 id;int hsize, part =1 ; +unsigned long flags =0 ; +int is_locked =0 ;if (reason <ARRAY_SIZE (reason_str)) why = reason_str[reason];else why ="Unknown" ; -mutex_lock (&psinfo->buf_mutex); +if (in_nmi ()) { + is_locked =spin_trylock (&psinfo->buf_lock); +if (!is_locked) +pr_err ("pstore dump routine blocked in NMI, may corrupt error record\n" ); + }else { +spin_lock_irqsave (&psinfo->buf_lock, flags); + } oopscount++;while (total < kmsg_bytes) { dst = psinfo->buf;
@@ -103,7 +112,12 @@ static void pstore_dump(struct kmsg_dumper *dumper, l2 -= l2_cpy; total += l1_cpy + l2_cpy; } -mutex_unlock (&psinfo->buf_mutex); +if (in_nmi ()) { +if (is_locked) +spin_unlock (&psinfo->buf_lock); + }else { +spin_unlock_irqrestore (&psinfo->buf_lock, flags); + } }static struct kmsg_dumper pstore_dumper = {
@@ -157,11 +171,12 @@ void pstore_get_records(void)enum pstore_type_id type;struct timespec time;int failed =0 , rc; +unsigned long flags;if (!psi)return ; -mutex_lock (&psinfo->buf_mutex); +spin_lock_irqsave (&psinfo->buf_lock, flags); rc = psi->open (psi);if (rc) goto out;
@@ -173,7 +188,7 @@ void pstore_get_records(void) } psi->close (psi); out: -mutex_unlock (&psinfo->buf_mutex); +spin_unlock_irqrestore (&psinfo->buf_lock, flags);if (failed) printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
@@ -187,6 +202,7 @@ out:int pstore_write (enum pstore_type_id type,char *buf,size_t size) {u64 id; +unsigned long flags;if (!psinfo)return -ENODEV ;
@@ -194,13 +210,13 @@ int pstore_write(enum pstore_type_id type, char *buf, size_t size)if (size > psinfo->bufsize)return -EFBIG ; -mutex_lock (&psinfo->buf_mutex); +spin_lock_irqsave (&psinfo->buf_lock, flags);memcpy (psinfo->buf, buf, size); id = psinfo->write (type, size);if (pstore_is_mounted ())pstore_mkfile (PSTORE_TYPE_DMESG , psinfo->name, id, psinfo->buf, size,CURRENT_TIME , psinfo->erase); -mutex_unlock (&psinfo->buf_mutex); +spin_unlock_irqrestore (&psinfo->buf_lock, flags);return 0 ; }
include/linux/pstore.h +1 -1
@@ -32,7 +32,7 @@ enum pstore_type_id {struct pstore_info {struct module *owner;char *name; -struct mutex buf_mutex;/* serialize access to 'buf' */ +spinlock_t buf_lock;/* serialize access to 'buf' */ char *buf;size_t bufsize;int (*open)(struct pstore_info *psi);
Signature · 1 line
1.7.6
Quoted 117 lines · Don Zickus
pstore was using mutex locking to protect read/write access to the backend plug-ins. This causes problems when pstore is executed in an NMI context through
panic()->kmsg_dump().This patch changes the mutex to a
spin_lock_irqsavethen also checks to see if we are in an NMI context. If we are in an NMI and can't get the lock, just print a message stating that and blow by the locking.All this is probably a hack around the bigger locking problem but it solves my current situation of trying to sleep in an NMI context.
Tested by loading the lkdtm module and executing a HARDLOCKUP which will cause the machine to panic inside the nmi handler.
Signed-off-by Don Zickus <[email protected]>--- drivers/acpi/apei/erst.c | 2 +- fs/pstore/platform.c | 28 ++++++++++++++++++++++------ include/linux/pstore.h | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-)drivers/acpi/apei/erst.c +1
-1@@ -1155,7 +1155,7 @@ static int __init erst_init(void) goto err_release_erange; buf =kmalloc (erst_erange.size,GFP_KERNEL ); -mutex_init (&erst_info.buf_mutex); +spin_lock_init (&erst_info.buf_lock);if (buf) { erst_info.buf = buf +sizeof (struct cper_pstore_record ); erst_info.bufsize = erst_erange.size -fs/pstore/platform.c +20
-4@@ -27,6 +27,7 @@#include <linux/string.h> #include <linux/slab.h> #include <linux/uaccess.h> +#include <linux/hardirq.h> #include "internal.h" @@ -68,13 +69,21 @@ static void pstore_dump(struct kmsg_dumper *dumper,char *dst, *why;u64 id;int hsize, part =1 ; +unsigned long flags =0 ; +int is_locked =0 ;if (reason <ARRAY_SIZE (reason_str)) why = reason_str[reason];else why ="Unknown" ; -mutex_lock (&psinfo->buf_mutex); +if (in_nmi ()) { + is_locked =spin_trylock (&psinfo->buf_lock); +if (!is_locked) +pr_err ("pstore dump routine blocked in NMI, may corrupt error record\n" ); + }else { +spin_lock_irqsave (&psinfo->buf_lock, flags); + } oopscount++;while (total < kmsg_bytes) { dst = psinfo->buf;@@ -103,7 +112,12 @@ static void pstore_dump(struct kmsg_dumper *dumper, l2 -= l2_cpy; total += l1_cpy + l2_cpy; } -mutex_unlock (&psinfo->buf_mutex); +if (in_nmi ()) { +if (is_locked) +spin_unlock (&psinfo->buf_lock); + }else { +spin_unlock_irqrestore (&psinfo->buf_lock, flags); + } }static struct kmsg_dumper pstore_dumper = {@@ -157,11 +171,12 @@ void pstore_get_records(void)enum pstore_type_id type;struct timespec time;int failed =0 , rc; +unsigned long flags;if (!psi)return ; -mutex_lock (&psinfo->buf_mutex); +spin_lock_irqsave (&psinfo->buf_lock, flags); rc = psi->open (psi);if (rc) goto out;@@ -173,7 +188,7 @@ void pstore_get_records(void) } psi->close (psi); out: -mutex_unlock (&psinfo->buf_mutex); +spin_unlock_irqrestore (&psinfo->buf_lock, flags);if (failed) printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",@@ -187,6 +202,7 @@ out:int pstore_write (enum pstore_type_id type,char *buf,size_t size) {u64 id; +unsigned long flags;if (!psinfo)return -ENODEV ;@@ -194,13 +210,13 @@ int pstore_write(enum pstore_type_id type, char *buf, size_t size)if (size > psinfo->bufsize)return -EFBIG ;
fs/pstore/platform.c int pstore_write(enum pstore_type_id type, char *buf, size_t size) · line 219
Quoted 10 lines
fs/pstore/platform.c +2
-2-mutex_lock (&psinfo->buf_mutex); +spin_lock_irqsave (&psinfo->buf_lock, flags);memcpy (psinfo->buf, buf, size); id = psinfo->write (type, size);if (pstore_is_mounted ())pstore_mkfile (PSTORE_TYPE_DMESG , psinfo->name, id, psinfo->buf, size,CURRENT_TIME , psinfo->erase); -mutex_unlock (&psinfo->buf_mutex); +spin_unlock_irqrestore (&psinfo->buf_lock, flags);
Is it safe to call pstore_mkfile with IRQ disabled?
pstore_mkfile -> d_alloc_name -> d_alloc -> kmem_cache_alloc(, GFP_KERNEL).
Best Regards,
Huang Ying
Quoted 131 lines · Huang Ying
Quoted 117 lines · Don Zickus
pstore was using mutex locking to protect read/write access to the backend plug-ins. This causes problems when pstore is executed in an NMI context through
panic()->kmsg_dump().This patch changes the mutex to a
spin_lock_irqsavethen also checks to see if we are in an NMI context. If we are in an NMI and can't get the lock, just print a message stating that and blow by the locking.All this is probably a hack around the bigger locking problem but it solves my current situation of trying to sleep in an NMI context.
Tested by loading the lkdtm module and executing a HARDLOCKUP which will cause the machine to panic inside the nmi handler.
Signed-off-by Don Zickus <[email protected]>--- drivers/acpi/apei/erst.c | 2 +- fs/pstore/platform.c | 28 ++++++++++++++++++++++------ include/linux/pstore.h | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-)drivers/acpi/apei/erst.c +1
-1@@ -1155,7 +1155,7 @@ static int __init erst_init(void) goto err_release_erange; buf =kmalloc (erst_erange.size,GFP_KERNEL ); -mutex_init (&erst_info.buf_mutex); +spin_lock_init (&erst_info.buf_lock);if (buf) { erst_info.buf = buf +sizeof (struct cper_pstore_record ); erst_info.bufsize = erst_erange.size -fs/pstore/platform.c +20
-4@@ -27,6 +27,7 @@#include <linux/string.h> #include <linux/slab.h> #include <linux/uaccess.h> +#include <linux/hardirq.h> #include "internal.h" @@ -68,13 +69,21 @@ static void pstore_dump(struct kmsg_dumper *dumper,char *dst, *why;u64 id;int hsize, part =1 ; +unsigned long flags =0 ; +int is_locked =0 ;if (reason <ARRAY_SIZE (reason_str)) why = reason_str[reason];else why ="Unknown" ; -mutex_lock (&psinfo->buf_mutex); +if (in_nmi ()) { + is_locked =spin_trylock (&psinfo->buf_lock); +if (!is_locked) +pr_err ("pstore dump routine blocked in NMI, may corrupt error record\n" ); + }else { +spin_lock_irqsave (&psinfo->buf_lock, flags); + } oopscount++;while (total < kmsg_bytes) { dst = psinfo->buf;@@ -103,7 +112,12 @@ static void pstore_dump(struct kmsg_dumper *dumper, l2 -= l2_cpy; total += l1_cpy + l2_cpy; } -mutex_unlock (&psinfo->buf_mutex); +if (in_nmi ()) { +if (is_locked) +spin_unlock (&psinfo->buf_lock); + }else { +spin_unlock_irqrestore (&psinfo->buf_lock, flags); + } }static struct kmsg_dumper pstore_dumper = {@@ -157,11 +171,12 @@ void pstore_get_records(void)enum pstore_type_id type;struct timespec time;int failed =0 , rc; +unsigned long flags;if (!psi)return ; -mutex_lock (&psinfo->buf_mutex); +spin_lock_irqsave (&psinfo->buf_lock, flags); rc = psi->open (psi);if (rc) goto out;@@ -173,7 +188,7 @@ void pstore_get_records(void) } psi->close (psi); out: -mutex_unlock (&psinfo->buf_mutex); +spin_unlock_irqrestore (&psinfo->buf_lock, flags);if (failed) printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",@@ -187,6 +202,7 @@ out:int pstore_write (enum pstore_type_id type,char *buf,size_t size) {u64 id; +unsigned long flags;if (!psinfo)return -ENODEV ;@@ -194,13 +210,13 @@ int pstore_write(enum pstore_type_id type, char *buf, size_t size)if (size > psinfo->bufsize)return -EFBIG ;fs/pstore/platform.c int pstore_write(enum pstore_type_id type, char *buf, size_t size) · line 219
Quoted 10 lines
fs/pstore/platform.c +2
-2-mutex_lock (&psinfo->buf_mutex); +spin_lock_irqsave (&psinfo->buf_lock, flags);memcpy (psinfo->buf, buf, size); id = psinfo->write (type, size);if (pstore_is_mounted ())pstore_mkfile (PSTORE_TYPE_DMESG , psinfo->name, id, psinfo->buf, size,CURRENT_TIME , psinfo->erase); -mutex_unlock (&psinfo->buf_mutex); +spin_unlock_irqrestore (&psinfo->buf_lock, flags);Is it safe to call
pstore_mkfilewith IRQ disabled?
Quoted 1 line
pstore_mkfile->d_alloc_name->d_alloc->kmem_cache_alloc(, GFP_KERNEL).
Don't know. But would that mean we would have to put the pstore_mkfile on a workqueue then or something similar?
Cheers,
Don
Luck, Tony
reviewerQuoted 6 lines
Quoted 3 lines
Is it safe to call
pstore_mkfilewith IRQ disabled?
pstore_mkfile->d_alloc_name->d_alloc->kmem_cache_alloc(, GFP_KERNEL).Don't know. But would that mean we would have to put the
pstore_mkfileon a workqueue then or something similar?
That might be a good idea anyway. In the "oops" case we'd like the file to appear in the pstore filesystem if the system stayed healthy despite the oops[1]. There isn't any reason why the pstore entry must appear instantly. Delaying the creation would avoid running into problems related to the oops.
-Tony
[1] So we can remove it to free pstore backend space - if we didn't die from the oops, then the oops is going to be in /var/log/messages.
Quoted 4 lines · Tony
Quoted 4 lines
Quoted 3 lines
Is it safe to call
pstore_mkfilewith IRQ disabled?
pstore_mkfile->d_alloc_name->d_alloc->kmem_cache_alloc(, GFP_KERNEL).
Quoted 8 lines
Quoted 2 lines
Don't know. But would that mean we would have to put the
pstore_mkfileon a workqueue then or something similar?That might be a good idea anyway. In the "oops" case we'd like the file to appear in the pstore filesystem if the system stayed healthy despite the oops[1]. There isn't any reason why the pstore entry must appear instantly. Delaying the creation would avoid running into problems related to the oops.
For oops, it may be better to delay writing into something like workqueue. But for panic, I think we should write the record to backend (such as ERST) as soon as possible. So maybe it is better to write to backend as soon as possible and delay writing to pstore filesystem.
Best Regards,
Huang Ying
Quoted 14 lines · Huang Ying
Quoted 4 lines · Tony
Quoted 4 lines
Quoted 3 lines
Is it safe to call
pstore_mkfilewith IRQ disabled?
pstore_mkfile->d_alloc_name->d_alloc->kmem_cache_alloc(, GFP_KERNEL).Quoted 8 lines
Quoted 2 lines
Don't know. But would that mean we would have to put the
pstore_mkfileon a workqueue then or something similar?That might be a good idea anyway. In the "oops" case we'd like the file to appear in the pstore filesystem if the system stayed healthy despite the oops[1]. There isn't any reason why the pstore entry must appear instantly. Delaying the creation would avoid running into problems related to the oops.
Quoted 4 lines
For oops, it may be better to delay writing into something like workqueue. But for panic, I think we should write the record to backend (such as ERST) as soon as possible. So maybe it is better to write to backend as soon as possible and delay writing to pstore filesystem.
In the panic case do we care if the pstore fs is mounted (which leads us to run pstore_mkfile)?
Actually it seems like most of the entry points into pstore_dumper would not require the fs to create a new file. I think the exception is an oops.
Cheers,
Don
Quoted 20 lines · Don Zickus
Quoted 14 lines · Huang Ying
Quoted 4 lines · Tony
Quoted 4 lines
Quoted 3 lines
Is it safe to call
pstore_mkfilewith IRQ disabled?
pstore_mkfile->d_alloc_name->d_alloc->kmem_cache_alloc(, GFP_KERNEL).Quoted 8 lines
Quoted 2 lines
Don't know. But would that mean we would have to put the
pstore_mkfileon a workqueue then or something similar?That might be a good idea anyway. In the "oops" case we'd like the file to appear in the pstore filesystem if the system stayed healthy despite the oops[1]. There isn't any reason why the pstore entry must appear instantly. Delaying the creation would avoid running into problems related to the oops.
Quoted 4 lines
For oops, it may be better to delay writing into something like workqueue. But for panic, I think we should write the record to backend (such as ERST) as soon as possible. So maybe it is better to write to backend as soon as possible and delay writing to pstore filesystem.
Quoted 6 lines
In the panic case do we care if the pstore fs is mounted (which leads us to run
pstore_mkfile)?Actually it seems like most of the entry points into
pstore_dumperwould not require the fs to create a new file. I think the exception is an oops.
Yes. I think so too.
Best Regards,
Huang Ying
Luck, Tony
reviewerQuoted 3 lines
Actually it seems like most of the entry points into
pstore_dumperwould not require the fs to create a new file. I think the exception is an oops.
Yup - oops is the only one where the current kernel continues - all the others (halt, reboot, kexec, panic) lead to the termination of the current kernel - and thus no user processes that would be able to see the shiny new entry in the pstore file system.
-Tony
Tony Luck
reviewerQuoted 4 lines · Tony
Yup - oops is the only one where the current kernel continues - all the others (halt, reboot, kexec, panic) lead to the termination of the current kernel - and thus no user processes that would be able to see the shiny new entry in the pstore file system.
Doh - I went to look at what it would take to fix this - and found that I'd already implemented it:
if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id,
psinfo->buf, hsize + l1_cpy + l2_cpy,
CURRENT_TIME, psinfo);-Tony
Quoted 6 lines · Tony Luck
Quoted 4 lines · Tony
Yup - oops is the only one where the current kernel continues - all the others (halt, reboot, kexec, panic) lead to the termination of the current kernel - and thus no user processes that would be able to see the shiny new entry in the pstore file system.
Quoted 8 lines
Doh - I went to look at what it would take to fix this - and found that I'd already implemented it:
if (reason == KMSG_DUMP_OOPS && pstore_is_mounted()) pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf, hsize + l1_cpy + l2_cpy, CURRENT_TIME, psinfo);
Right, but it still has to be created with locks surrounding it. And the question is what type of locks should we use. Mutexes don't work when called from the NMI case and spinlocks don't seem to work when called from the OOPs (pstore_mkfile) case.
Cheers,
Don