lkdtm/bugs: add test for panic() with stuck secondary CPUs
Subsystems (2)
PATCH 1/1 lkdtm/bugs: add test for panic() with stuck secondary CPUs
Upon a panic() the kernel will use either smp_send_stop() or crash_smp_send_stop() to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.
This patch adds a test for panic() where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stops work.
For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---
On arm64 *with* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 1 PID: 19 Comm: migration/1 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | Kernel Offset: 0x55a9c0bc0000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,fce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---
--- drivers/misc/lkdtm/bugs.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
I've tested this with the arm64 NMI IPI patches:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Specifically, with the patch that uses an NMI for IPI_CPU_STOP and IPI_CPU_CRASH_STOP:
Mark.
drivers/misc/lkdtm/bugs.c +28 -1
@@ -6,12 +6,14 @@* test source files. */ #include "lkdtm.h" +#include <linux/cpu.h> #include <linux/list.h> #include <linux/sched.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> -#include <linux/uaccess.h> #include <linux/slab.h> +#include <linux/stop_machine.h> +#include <linux/uaccess.h> #if IS_ENABLED (CONFIG_X86_32 ) && !IS_ENABLED (CONFIG_UML )#include <asm/desc.h>
@@ -73,6 +75,30 @@ static void lkdtm_PANIC(void)panic ("dumptest" ); } +static int panic_stop_irqoff_fn (void *arg) +{ +atomic_t *v = arg; + +/* +* Trigger the panic after all other CPUs have entered this function, +* so that they are guaranteed to have IRQs disabled. +*/ +if (atomic_inc_return (v) ==num_online_cpus ()) +panic ("panic stop irqoff test" ); + +for (;;) +cpu_relax (); +} + +static void lkdtm_PANIC_STOP_IRQOFF (void ) +{ +atomic_t v =ATOMIC_INIT (0 ); + +cpus_read_lock (); +stop_machine (panic_stop_irqoff_fn, &v, cpu_online_mask); +cpus_read_unlock (); +} +static void lkdtm_BUG (void ) {BUG ();
@@ -598,6 +624,7 @@ static noinline void lkdtm_CORRUPT_PAC(void)static struct crashtype crashtypes[] = {CRASHTYPE (PANIC ), +CRASHTYPE (PANIC_STOP_IRQOFF ),CRASHTYPE (BUG ),CRASHTYPE (WARNING ),CRASHTYPE (WARNING_MESSAGE ),
Signature · 1 line
2.30.2
Hi Mark,
Thanks for putting up a test case for this.
Quoted 11 lines · Mark Rutland
Upon a
panic()the kernel will use eithersmp_send_stop()orcrash_smp_send_stop()to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.This patch adds a test for
panic()where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stops
s/whe/when/
Quoted 30 lines
work.
For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---
Quoted 1 line
On arm64 *with* an NMI, this results in:
I suppose a more interesting test scenario to show difference among NMI stop IPI and regular stop IPI would be:
- First put any CPU into hard lockup state via:
$ echo HARDLOCKUP > /sys/kernel/debug/provoke-crash/DIRECT
- And then provoke following from other CPU:
$ echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT
Quoted 82 lines
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 1 PID: 19 Comm: migration/1 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | Kernel Offset: 0x55a9c0bc0000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,fce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---Signed-off-by Mark Rutland <[email protected]>Cc Douglas Anderson <[email protected]>Cc Kees Cook <[email protected]>Cc Stephen Boyd <[email protected]Cc Sumit Garg <[email protected]>--- drivers/misc/lkdtm/bugs.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)I've tested this with the arm64 NMI IPI patches:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Specifically, with the patch that uses an NMI for
IPI_CPU_STOPandIPI_CPU_CRASH_STOP:Mark.
drivers/misc/lkdtm/bugs.c +15
-1@@ -6,12 +6,14 @@* test source files. */ #include "lkdtm.h" +#include <linux/cpu.h> #include <linux/list.h> #include <linux/sched.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> -#include <linux/uaccess.h> #include <linux/slab.h> +#include <linux/stop_machine.h> +#include <linux/uaccess.h> #if IS_ENABLED (CONFIG_X86_32 ) && !IS_ENABLED (CONFIG_UML )#include <asm/desc.h> @@ -73,6 +75,30 @@ static void lkdtm_PANIC(void)panic ("dumptest" ); } +static int panic_stop_irqoff_fn (void *arg) +{ +atomic_t *v = arg; + +/* +* Trigger the panic after all other CPUs have entered this function, +* so that they are guaranteed to have IRQs disabled. +*/ +if (atomic_inc_return (v) ==num_online_cpus ()) +panic ("panic stop irqoff test" ); + +for (;;)
drivers/misc/lkdtm/bugs.c static void lkdtm_PANIC(void) · line 99
Quoted 10 lines
drivers/misc/lkdtm/bugs.c +10
-0+cpu_relax (); +} + +static void lkdtm_PANIC_STOP_IRQOFF (void ) +{ +atomic_t v =ATOMIC_INIT (0 ); + +cpus_read_lock (); +stop_machine (panic_stop_irqoff_fn, &v, cpu_online_mask); +cpus_read_unlock ();
stop_machine() does internally use cpus_read_{lock/unlock}(), is there any need to have them here as well?
-Sumit
Quoted 16 lines
drivers/misc/lkdtm/bugs.c +3
-1+} +static void lkdtm_BUG (void ) {BUG ();@@ -598,6 +624,7 @@ static noinline void lkdtm_CORRUPT_PAC(void)static struct crashtype crashtypes[] = {CRASHTYPE (PANIC ), +CRASHTYPE (PANIC_STOP_IRQOFF ),CRASHTYPE (BUG ),CRASHTYPE (WARNING ),CRASHTYPE (WARNING_MESSAGE ), --2.30.2
Quoted 51 lines · Sumit Garg
Hi Mark,
Thanks for putting up a test case for this.
Quoted 11 lines · Mark Rutland
Upon a
panic()the kernel will use eithersmp_send_stop()orcrash_smp_send_stop()to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.This patch adds a test for
panic()where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stopss/whe/when/
Quoted 30 lines
work.
For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---Quoted 1 line
On arm64 *with* an NMI, this results in:
Quoted 8 lines
I suppose a more interesting test scenario to show difference among NMI stop IPI and regular stop IPI would be:
- First put any CPU into hard lockup state via:
$ echo HARDLOCKUP > /sys/kernel/debug/provoke-crash/DIRECT- And then provoke following from other CPU:
$ echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT
I don't follow. IIUC that's only going to test whether a HW watchdog can fire and reset the system?
The PANIC_STOP_IRQOFF test has each CPU run panic_stop_irqoff_fn() with IRQs disabled, and if one CPU is stuck in the HARDLOCKUP test, we'll never get all CPUs into panic_stop_irqoff_fn(), and so all CPUs will be stuck with IRQs disabled, spinning.
The PANIC_STOP_IRQOFF test itself tests the different between an NMI stop IPI and regular stop IPI, as the results in the commit message shows. Look for the line above that says:
| SMP: failed to stop secondary CPUs 0-3
... which is *not* present in the NMI case (though we don't have an explicit "stoppped all CPUs" message).
Quoted 87 lines
Quoted 87 lines
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 1 PID: 19 Comm: migration/1 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | Kernel Offset: 0x55a9c0bc0000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,fce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---Signed-off-by Mark Rutland <[email protected]>Cc Douglas Anderson <[email protected]>Cc Kees Cook <[email protected]>Cc Stephen Boyd <[email protected]Cc Sumit Garg <[email protected]>--- drivers/misc/lkdtm/bugs.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)I've tested this with the arm64 NMI IPI patches:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Specifically, with the patch that uses an NMI for
IPI_CPU_STOPandIPI_CPU_CRASH_STOP:Mark.
drivers/misc/lkdtm/bugs.c +20
-1@@ -6,12 +6,14 @@* test source files. */ #include "lkdtm.h" +#include <linux/cpu.h> #include <linux/list.h> #include <linux/sched.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> -#include <linux/uaccess.h> #include <linux/slab.h> +#include <linux/stop_machine.h> +#include <linux/uaccess.h> #if IS_ENABLED (CONFIG_X86_32 ) && !IS_ENABLED (CONFIG_UML )#include <asm/desc.h> @@ -73,6 +75,30 @@ static void lkdtm_PANIC(void)panic ("dumptest" ); } +static int panic_stop_irqoff_fn (void *arg) +{ +atomic_t *v = arg; + +/* +* Trigger the panic after all other CPUs have entered this function, +* so that they are guaranteed to have IRQs disabled. +*/ +if (atomic_inc_return (v) ==num_online_cpus ()) +panic ("panic stop irqoff test" ); + +for (;;) +cpu_relax (); +} + +static void lkdtm_PANIC_STOP_IRQOFF (void ) +{
Quoted 8 lines
drivers/misc/lkdtm/bugs.c static void lkdtm_PANIC(void) · line 99
Quoted 5 lines
drivers/misc/lkdtm/bugs.c +5
-0+atomic_t v =ATOMIC_INIT (0 ); + +cpus_read_lock (); +stop_machine (panic_stop_irqoff_fn, &v, cpu_online_mask); +cpus_read_unlock ();
stop_machine()does internally use cpus_read_{lock/unlock}(), is there any need to have them here as well?
For some reason I thought that stop_machine() copied the mask prior to calling cpus_read_lock(), and hance it was necessary to avoid the online mask changing, but from taking a look just now that's not the case.
I'll drop the cpus_read_lock() .. cpus_read_unlock() here.
Thanks,
Mark.
Quoted 70 lines · Mark Rutland
Quoted 51 lines · Sumit Garg
Hi Mark,
Thanks for putting up a test case for this.
Quoted 11 lines · Mark Rutland
Upon a
panic()the kernel will use eithersmp_send_stop()orcrash_smp_send_stop()to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.This patch adds a test for
panic()where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stopss/whe/when/
Quoted 30 lines
work.
For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---Quoted 1 line
On arm64 *with* an NMI, this results in:
Quoted 8 lines
I suppose a more interesting test scenario to show difference among NMI stop IPI and regular stop IPI would be:
- First put any CPU into hard lockup state via:
$ echo HARDLOCKUP > /sys/kernel/debug/provoke-crash/DIRECT- And then provoke following from other CPU:
$ echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECTI don't follow. IIUC that's only going to test whether a HW watchdog can fire and reset the system?
The
PANIC_STOP_IRQOFFtest has each CPU runpanic_stop_irqoff_fn()with IRQs disabled, and if one CPU is stuck in the HARDLOCKUP test, we'll never get all CPUs intopanic_stop_irqoff_fn(), and so all CPUs will be stuck with IRQs disabled, spinning.
Quoted 8 lines
The
PANIC_STOP_IRQOFFtest itself tests the different between an NMI stop IPI and regular stop IPI, as the results in the commit message shows. Look for the line above that says:| SMP: failed to stop secondary CPUs 0-3
... which is *not* present in the NMI case (though we don't have an explicit "stoppped all CPUs" message).
Ah, I see your point as I missed that difference when I first looked up the panic() logs. So it's the post panic() CPU stop behaviour that we are testing here. Thanks for the explanation.
-Sumit
Hi,
Quoted 59 lines · Mark Rutland
Upon a
panic()the kernel will use eithersmp_send_stop()orcrash_smp_send_stop()to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.This patch adds a test for
panic()where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stops work.For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---On arm64 *with* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 1 PID: 19 Comm: migration/1 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248
Quoted 8 lines
| kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | Kernel Offset: 0x55a9c0bc0000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,fce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---
Since Sumit didn't notice it and I had to use a "diff" tool to help me spot the difference in your two examples, maybe telegraph for the reader of the commit message that the important thing is the lack of "SMP: failed to stop secondary CPUs" in your example.
drivers/misc/lkdtm/bugs.c static void lkdtm_PANIC(void) · line 98
Quoted 6 lines
drivers/misc/lkdtm/bugs.c +6
-0+static void lkdtm_PANIC_STOP_IRQOFF (void ) +{ +atomic_t v =ATOMIC_INIT (0 ); + +cpus_read_lock (); +stop_machine (panic_stop_irqoff_fn, &v, cpu_online_mask);
It broke my brain just a little bit that stop_machine() isn't backed by "IPI_CPU_STOP" since the point of this was to test the patch modifying how "IPI_CPU_STOP" worked. ;-) Assuming I didn't mess up, I guess stop_machine() is just used to get all the CPUs running with interrupts disabled and then the last CPU does a panic. The _panic_ then tests "IPI_CPU_STOP" / "IPI_CPU_CRASH_STOP".
Probably this is obvious to everyone but me, so no action is needed unless you agree that it's confusing and can think of a good comment / commit message addition to help.
In any case, after Sumit's feedback is addressed:
Quoted 71 lines · Doug Anderson
Hi,
Quoted 59 lines · Mark Rutland
Upon a
panic()the kernel will use eithersmp_send_stop()orcrash_smp_send_stop()to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.This patch adds a test for
panic()where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stops work.For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---On arm64 *with* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 1 PID: 19 Comm: migration/1 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248Quoted 8 lines
| kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | Kernel Offset: 0x55a9c0bc0000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,fce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---
Quoted 4 lines
Since Sumit didn't notice it and I had to use a "diff" tool to help me spot the difference in your two examples, maybe telegraph for the reader of the commit message that the important thing is the lack of "SMP: failed to stop secondary CPUs" in your example.
Sure; I've added explicit "note" lines to v2 for that.
Quoted 5 lines
Quoted 5 lines
drivers/misc/lkdtm/bugs.c +5
-0+static void lkdtm_PANIC_STOP_IRQOFF (void ) +{ +atomic_t v =ATOMIC_INIT (0 ); + +cpus_read_lock ();
Quoted 8 lines
Quoted 1 line
+
stop_machine(panic_stop_irqoff_fn, &v, cpu_online_mask);It broke my brain just a little bit that
stop_machine()isn't backed by "IPI_CPU_STOP" since the point of this was to test the patch modifying how "IPI_CPU_STOP" worked. ;-) Assuming I didn't mess up, I guessstop_machine()is just used to get all the CPUs running with interrupts disabled and then the last CPU does a panic. The _panic_ then tests "IPI_CPU_STOP" / "IPI_CPU_CRASH_STOP".
Yup; I had hoped that the comment within panic_stop_irqoff_fn() was sufficient for that:
/* * Trigger the panic after all other CPUs have entered this function, * so that they are guaranteed to have IRQs disabled. */
I've made that a bit more explicit now:
/*
* As stop_machine() disables interrupts, all CPUs within this function
* have interrupts disabled and cannot take a regular IPI. *
* The last CPU which enters here will trigger a panic, and as all CPUs
* cannot take a regular IPI, we'll only be able to stop secondaries if
* smp_send_stop() or crash_smp_send_stop() uses an NMI. */
Quoted 3 lines
Probably this is obvious to everyone but me, so no action is needed unless you agree that it's confusing and can think of a good comment / commit message addition to help.
Hopefully the above is good enough?
I agree the stop_machine() naming is also somewhat confusing here, but I can't do much about that.
Quoted 3 lines
In any case, after Sumit's feedback is addressed:
Reviewed-by Douglas Anderson <[email protected]>
Thanks!
Mark.
Quoted 139 lines · Mark Rutland
Upon a
panic()the kernel will use eithersmp_send_stop()orcrash_smp_send_stop()to attempt to stop secondary CPUs via an IPI, which may or may not be an NMI. Generally it's preferable that this is an NMI so that CPUs can be stopped in as many situations as possible, but it's not always possible to provide an NMI, and there are cases where CPUs may be unable to handle the NMI regardless.This patch adds a test for
panic()where all other CPUs are stuck with interrupts disabled, which can be used to check whether the kernel gracefully handles CPUs failing to respond to a stop, and whe NMIs stops work.For example, on arm64 *without* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 2 PID: 24 Comm: migration/2 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | SMP: failed to stop secondary CPUs 0-3 | Kernel Offset: 0x401cf3490000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,cce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---On arm64 *with* an NMI, this results in:
| # echo PANIC_STOP_IRQOFF > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry PANIC_STOP_IRQOFF | Kernel panic - not syncing: panic stop irqoff test | CPU: 1 PID: 19 Comm: migration/1 Not tainted 6.5.0-rc3-00077-ge6c782389895-dirty #4 | Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 | Stopper: multi_cpu_stop+0x0/0x1a0 <- stop_machine_cpuslocked+0x158/0x1a4 | Call trace: | dump_backtrace+0x94/0xec | show_stack+0x18/0x24 | dump_stack_lvl+0x74/0xc0 | dump_stack+0x18/0x24 | panic+0x358/0x3e8 | lkdtm_PANIC+0x0/0x18 | multi_cpu_stop+0x9c/0x1a0 | cpu_stopper_thread+0x84/0x118 | smpboot_thread_fn+0x224/0x248 | kthread+0x114/0x118 | ret_from_fork+0x10/0x20 | SMP: stopping secondary CPUs | Kernel Offset: 0x55a9c0bc0000 from 0xffff800080000000 | PHYS_OFFSET: 0x40000000 | CPU features: 0x00000000,68c167a1,fce6773f | Memory Limit: none | ---[ end Kernel panic - not syncing: panic stop irqoff test ]---Signed-off-by Mark Rutland <[email protected]>Cc Douglas Anderson <[email protected]>Cc Kees Cook <[email protected]>Cc Stephen Boyd <[email protected]Cc Sumit Garg <[email protected]>--- drivers/misc/lkdtm/bugs.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)I've tested this with the arm64 NMI IPI patches:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Specifically, with the patch that uses an NMI for
IPI_CPU_STOPandIPI_CPU_CRASH_STOP:Mark.
drivers/misc/lkdtm/bugs.c +27
-1@@ -6,12 +6,14 @@* test source files. */ #include "lkdtm.h" +#include <linux/cpu.h> #include <linux/list.h> #include <linux/sched.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> -#include <linux/uaccess.h> #include <linux/slab.h> +#include <linux/stop_machine.h> +#include <linux/uaccess.h> #if IS_ENABLED (CONFIG_X86_32 ) && !IS_ENABLED (CONFIG_UML )#include <asm/desc.h> @@ -73,6 +75,30 @@ static void lkdtm_PANIC(void)panic ("dumptest" ); } +static int panic_stop_irqoff_fn (void *arg) +{ +atomic_t *v = arg; + +/* +* Trigger the panic after all other CPUs have entered this function, +* so that they are guaranteed to have IRQs disabled. +*/ +if (atomic_inc_return (v) ==num_online_cpus ()) +panic ("panic stop irqoff test" ); + +for (;;) +cpu_relax (); +} + +static void lkdtm_PANIC_STOP_IRQOFF (void ) +{ +atomic_t v =ATOMIC_INIT (0 ); + +cpus_read_lock (); +stop_machine (panic_stop_irqoff_fn, &v, cpu_online_mask); +cpus_read_unlock (); +} +static void lkdtm_BUG (void ) {BUG ();
drivers/misc/lkdtm/bugs.c static noinline void lkdtm_CORRUPT_PAC(void) · line 630
Quoted 7 lines
drivers/misc/lkdtm/bugs.c +1
-0@@ -598,6 +624,7 @@ static noinline void lkdtm_CORRUPT_PAC(void)static struct crashtype crashtypes[] = {CRASHTYPE (PANIC ), +CRASHTYPE (PANIC_STOP_IRQOFF ),CRASHTYPE (BUG ),CRASHTYPE (WARNING ),CRASHTYPE (WARNING_MESSAGE ),
Modulo the other comments in the thread, this looks good to me. :)
Signature · 1 line
Kees Cook