* [PATCH 0/2] Fix a few snafus in the reboot code @ 2026-11-26 21:47 Bradley Morgan 2026-11-26 21:47 ` [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data Bradley Morgan 2026-11-26 21:47 ` [PATCH 2/2] reboot: register legacy pm_power_off once at boot Bradley Morgan 0 siblings, 2 replies; 7+ messages in thread From: Bradley Morgan @ 2026-11-26 21:47 UTC (permalink / raw) To: Andrew Morton, Rafael J . Wysocki Cc: Dmitry Osipenko, Max Filippov, Chris Zankel, Andrew Davis, Oleg Nesterov, Petr Mladek, linux-kernel, Bradley Morgan In reboot, i have found a few small snafus which i have fixed, Patch 1 fixes a small snafu in platform_sys_off_handler, where **IF** a caller passes NULL for cb_data, a second SYS_OFF_PRIO_PLATFORM registration thinks that platform_sys_off_handler is still free, hence, it reuses the **SAME** struct, then links the same notifier_block into two lists at once... Patch 2 also fixes a snafu, where do_kernel_power_off() registers a pm_power_off quickly, and then ends up allocating with GFP_KERNEL or calling synchronize_rcu() with interrupts off and other CPUs stopped. I am aware these bugs are quite "edge-casey", but better to fix them now rather then never! :) Happy Reviewing! Bradley Morgan (2): reboot: fix platform sys_off handler reuse with NULL cb_data reboot: register legacy pm_power_off once at boot kernel/reboot.c | 83 ++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 35 deletions(-) -- 2.53.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data 2026-11-26 21:47 [PATCH 0/2] Fix a few snafus in the reboot code Bradley Morgan @ 2026-11-26 21:47 ` Bradley Morgan 2026-12-03 5:50 ` Andrew Morton 2026-12-03 21:30 ` + reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch added to mm-nonmm-unstable branch Andrew Morton 2026-11-26 21:47 ` [PATCH 2/2] reboot: register legacy pm_power_off once at boot Bradley Morgan 1 sibling, 2 replies; 7+ messages in thread From: Bradley Morgan @ 2026-11-26 21:47 UTC (permalink / raw) To: Andrew Morton, Rafael J . Wysocki Cc: Dmitry Osipenko, Max Filippov, Chris Zankel, Andrew Davis, Oleg Nesterov, Petr Mladek, linux-kernel, Bradley Morgan In alloc_sys_off_handler(), the check for whether the static platform_sys_off_handler is already in use checks handler->cb_data, which is a weird assumption since cb_data is optional and callers can just pass NULL. If a caller registers at SYS_OFF_PRIO_PLATFORM with NULL cb_data, a second SYS_OFF_PRIO_PLATFORM registration gets the same struct back, overwrites its fields, and links the same notifier_block into another chain while it is still on the first one. For example, on xtensa ISS platform_setup() registers iss_restart and iss_power_off this way, so restarting calls iss_power_off() instead and splices the two lists together if any lower priority poweroff handler is around. To fix this, resolve the mode and list first, keep one static handler per mode in platform_sys_off_handlers[], and check handler->list instead of handler->cb_data so a NULL cb_data cannot slip past -EBUSY. Fixes: 587b9bfe0668 ("kernel/reboot: Use static handler for register_platform_power_off()") Signed-off-by: Bradley Morgan <bradley.morgan@linux.dev> --- kernel/reboot.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/kernel/reboot.c b/kernel/reboot.c index 9a84401adde5a..87f307ab46faa 100644 --- a/kernel/reboot.c +++ b/kernel/reboot.c @@ -363,9 +363,11 @@ static int sys_off_notify(struct notifier_block *nb, return handler->sys_off_cb(&data); } -static struct sys_off_handler platform_sys_off_handler; +static struct sys_off_handler + platform_sys_off_handlers[SYS_OFF_MODE_RESTART + 1]; -static struct sys_off_handler *alloc_sys_off_handler(int priority) +static struct sys_off_handler *alloc_sys_off_handler(enum sys_off_mode mode, + int priority) { struct sys_off_handler *handler; gfp_t flags; @@ -375,8 +377,8 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority) * at the early boot time because memory allocator isn't available yet. */ if (priority == SYS_OFF_PRIO_PLATFORM) { - handler = &platform_sys_off_handler; - if (handler->cb_data) + handler = &platform_sys_off_handlers[mode]; + if (handler->list) return ERR_PTR(-EBUSY); } else { if (system_state > SYSTEM_RUNNING) @@ -394,7 +396,9 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority) static void free_sys_off_handler(struct sys_off_handler *handler) { - if (handler == &platform_sys_off_handler) + if (handler >= platform_sys_off_handlers && + handler < platform_sys_off_handlers + + ARRAY_SIZE(platform_sys_off_handlers)) memset(handler, 0, sizeof(*handler)); else kfree(handler); @@ -428,36 +432,40 @@ register_sys_off_handler(enum sys_off_mode mode, void *cb_data) { struct sys_off_handler *handler; + bool blocking = false; + void *list; int err; - handler = alloc_sys_off_handler(priority); - if (IS_ERR(handler)) - return handler; - switch (mode) { case SYS_OFF_MODE_POWER_OFF_PREPARE: - handler->list = &power_off_prep_handler_list; - handler->blocking = true; + list = &power_off_prep_handler_list; + blocking = true; break; case SYS_OFF_MODE_POWER_OFF: - handler->list = &power_off_handler_list; + list = &power_off_handler_list; break; case SYS_OFF_MODE_RESTART_PREPARE: - handler->list = &restart_prep_handler_list; - handler->blocking = true; + list = &restart_prep_handler_list; + blocking = true; break; case SYS_OFF_MODE_RESTART: - handler->list = &restart_handler_list; + list = &restart_handler_list; break; default: - free_sys_off_handler(handler); return ERR_PTR(-EINVAL); } + handler = alloc_sys_off_handler(mode, priority); + if (IS_ERR(handler)) + return handler; + + handler->list = list; + handler->blocking = blocking; + handler->nb.notifier_call = sys_off_notify; handler->nb.priority = priority; handler->sys_off_cb = callback; -- 2.53.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data 2026-11-26 21:47 ` [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data Bradley Morgan @ 2026-12-03 5:50 ` Andrew Morton 2026-12-03 5:53 ` Andrew Morton 2026-12-03 21:30 ` + reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch added to mm-nonmm-unstable branch Andrew Morton 1 sibling, 1 reply; 7+ messages in thread From: Andrew Morton @ 2026-12-03 5:50 UTC (permalink / raw) To: Bradley Morgan Cc: Rafael J . Wysocki, Dmitry Osipenko, Max Filippov, Chris Zankel, Andrew Davis, Oleg Nesterov, Petr Mladek, linux-kernel On Thu, 26 Nov 2026 21:47:53 +0000 Bradley Morgan <bradley.morgan@linux.dev> wrote: > In alloc_sys_off_handler(), the check for whether the static > platform_sys_off_handler is already in use checks handler->cb_data, > which is a weird assumption since cb_data is optional and callers can > just pass NULL. If a caller registers at SYS_OFF_PRIO_PLATFORM with > NULL cb_data, a second SYS_OFF_PRIO_PLATFORM registration gets the same > struct back, overwrites its fields, and links the same notifier_block > into another chain while it is still on the first one. For example, on > xtensa ISS platform_setup() registers iss_restart and iss_power_off > this way, so restarting calls iss_power_off() instead and splices the > two lists together if any lower priority poweroff handler is around. > > To fix this, resolve the mode and list first, keep one static handler > per mode in platform_sys_off_handlers[], and check handler->list instead > of handler->cb_data so a NULL cb_data cannot slip past -EBUSY. > > ... > > --- a/kernel/reboot.c > +++ b/kernel/reboot.c > @@ -394,7 +396,9 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority) > > static void free_sys_off_handler(struct sys_off_handler *handler) > { > - if (handler == &platform_sys_off_handler) > + if (handler >= platform_sys_off_handlers && > + handler < platform_sys_off_handlers + > + ARRAY_SIZE(platform_sys_off_handlers)) But `handler' can be allocated with kzalloc_obj(), so comparing that pointer with the statically allocated array members isn't meaningful? ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data 2026-12-03 5:50 ` Andrew Morton @ 2026-12-03 5:53 ` Andrew Morton 0 siblings, 0 replies; 7+ messages in thread From: Andrew Morton @ 2026-12-03 5:53 UTC (permalink / raw) To: Bradley Morgan, Rafael J . Wysocki, Dmitry Osipenko, Max Filippov, Chris Zankel, Andrew Davis, Oleg Nesterov, Petr Mladek, linux-kernel On Wed, 2 Dec 2026 21:50:35 -0800 Andrew Morton <akpm@linux-foundation.org> wrote: > On Thu, 26 Nov 2026 21:47:53 +0000 Bradley Morgan <bradley.morgan@linux.dev> wrote: > > > In alloc_sys_off_handler(), the check for whether the static > > platform_sys_off_handler is already in use checks handler->cb_data, > > which is a weird assumption since cb_data is optional and callers can > > just pass NULL. If a caller registers at SYS_OFF_PRIO_PLATFORM with > > NULL cb_data, a second SYS_OFF_PRIO_PLATFORM registration gets the same > > struct back, overwrites its fields, and links the same notifier_block > > into another chain while it is still on the first one. For example, on > > xtensa ISS platform_setup() registers iss_restart and iss_power_off > > this way, so restarting calls iss_power_off() instead and splices the > > two lists together if any lower priority poweroff handler is around. > > > > To fix this, resolve the mode and list first, keep one static handler > > per mode in platform_sys_off_handlers[], and check handler->list instead > > of handler->cb_data so a NULL cb_data cannot slip past -EBUSY. > > > > ... > > > > --- a/kernel/reboot.c > > +++ b/kernel/reboot.c > > @@ -394,7 +396,9 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority) > > > > static void free_sys_off_handler(struct sys_off_handler *handler) > > { > > - if (handler == &platform_sys_off_handler) > > + if (handler >= platform_sys_off_handlers && > > + handler < platform_sys_off_handlers + > > + ARRAY_SIZE(platform_sys_off_handlers)) > > But `handler' can be allocated with kzalloc_obj(), so comparing that > pointer with the statically allocated array members isn't meaningful? > Well. The comparison is officially undefined in C. But I can see what you're doing there and I'm sure it's OK in kernel ;) But can the comparisons easily be avoided? Or at least commented. ^ permalink raw reply [flat|nested] 7+ messages in thread
* + reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch added to mm-nonmm-unstable branch 2026-11-26 21:47 ` [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data Bradley Morgan 2026-12-03 5:50 ` Andrew Morton @ 2026-12-03 21:30 ` Andrew Morton 1 sibling, 0 replies; 7+ messages in thread From: Andrew Morton @ 2026-12-03 21:30 UTC (permalink / raw) To: mm-commits, rafael, pmladek, oleg, jcmvbkbc, dmitry.osipenko, chris, afd, bradley.morgan, akpm The patch titled Subject: reboot: fix platform sys_off handler reuse with NULL cb_data has been added to the -mm mm-nonmm-unstable branch. Its filename is reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Bradley Morgan <bradley.morgan@linux.dev> Subject: reboot: fix platform sys_off handler reuse with NULL cb_data Date: Thu, 26 Nov 2026 21:47:53 +0000 Patch series "Fix a few snafus in the reboot code". In reboot, i have found a few small snafus which i have fixed, Patch 1 fixes a small snafu in platform_sys_off_handler, where **IF** a caller passes NULL for cb_data, a second SYS_OFF_PRIO_PLATFORM registration thinks that platform_sys_off_handler is still free, hence, it reuses the **SAME** struct, then links the same notifier_block into two lists at once... Patch 2 also fixes a snafu, where do_kernel_power_off() registers a pm_power_off quickly, and then ends up allocating with GFP_KERNEL or calling synchronize_rcu() with interrupts off and other CPUs stopped. This patch (of 2): In alloc_sys_off_handler(), the check for whether the static platform_sys_off_handler is already in use checks handler->cb_data, which is a weird assumption since cb_data is optional and callers can just pass NULL. If a caller registers at SYS_OFF_PRIO_PLATFORM with NULL cb_data, a second SYS_OFF_PRIO_PLATFORM registration gets the same struct back, overwrites its fields, and links the same notifier_block into another chain while it is still on the first one. For example, on xtensa ISS platform_setup() registers iss_restart and iss_power_off this way, so restarting calls iss_power_off() instead and splices the two lists together if any lower priority poweroff handler is around. To fix this, resolve the mode and list first, keep one static handler per mode in platform_sys_off_handlers[], and check handler->list instead of handler->cb_data so a NULL cb_data cannot slip past -EBUSY. Link: https://lore.kernel.org/20261126214754.3630965-1-bradley.morgan@linux.dev Link: https://patch.msgid.link/20261126214754.3630965-2-bradley.morgan@linux.dev Fixes: 587b9bfe0668 ("kernel/reboot: Use static handler for register_platform_power_off()") Signed-off-by: Bradley Morgan <bradley.morgan@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Rafael J . Wysocki <rafael@kernel.org> Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com> Cc: Max Filippov <jcmvbkbc@gmail.com> Cc: Chris Zankel <chris@zankel.net> Cc: Andrew Davis <afd@ti.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Petr Mladek <pmladek@suse.com> kernel/reboot.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) --- a/kernel/reboot.c~reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data +++ a/kernel/reboot.c @@ -363,9 +363,11 @@ static int sys_off_notify(struct notifie return handler->sys_off_cb(&data); } -static struct sys_off_handler platform_sys_off_handler; +static struct sys_off_handler + platform_sys_off_handlers[SYS_OFF_MODE_RESTART + 1]; -static struct sys_off_handler *alloc_sys_off_handler(int priority) +static struct sys_off_handler *alloc_sys_off_handler(enum sys_off_mode mode, + int priority) { struct sys_off_handler *handler; gfp_t flags; @@ -375,8 +377,8 @@ static struct sys_off_handler *alloc_sys * at the early boot time because memory allocator isn't available yet. */ if (priority == SYS_OFF_PRIO_PLATFORM) { - handler = &platform_sys_off_handler; - if (handler->cb_data) + handler = &platform_sys_off_handlers[mode]; + if (handler->list) return ERR_PTR(-EBUSY); } else { if (system_state > SYSTEM_RUNNING) @@ -394,7 +396,9 @@ static struct sys_off_handler *alloc_sys static void free_sys_off_handler(struct sys_off_handler *handler) { - if (handler == &platform_sys_off_handler) + if (handler >= platform_sys_off_handlers && + handler < platform_sys_off_handlers + + ARRAY_SIZE(platform_sys_off_handlers)) memset(handler, 0, sizeof(*handler)); else kfree(handler); @@ -428,36 +432,40 @@ register_sys_off_handler(enum sys_off_mo void *cb_data) { struct sys_off_handler *handler; + bool blocking = false; + void *list; int err; - handler = alloc_sys_off_handler(priority); - if (IS_ERR(handler)) - return handler; - switch (mode) { case SYS_OFF_MODE_POWER_OFF_PREPARE: - handler->list = &power_off_prep_handler_list; - handler->blocking = true; + list = &power_off_prep_handler_list; + blocking = true; break; case SYS_OFF_MODE_POWER_OFF: - handler->list = &power_off_handler_list; + list = &power_off_handler_list; break; case SYS_OFF_MODE_RESTART_PREPARE: - handler->list = &restart_prep_handler_list; - handler->blocking = true; + list = &restart_prep_handler_list; + blocking = true; break; case SYS_OFF_MODE_RESTART: - handler->list = &restart_handler_list; + list = &restart_handler_list; break; default: - free_sys_off_handler(handler); return ERR_PTR(-EINVAL); } + handler = alloc_sys_off_handler(mode, priority); + if (IS_ERR(handler)) + return handler; + + handler->list = list; + handler->blocking = blocking; + handler->nb.notifier_call = sys_off_notify; handler->nb.priority = priority; handler->sys_off_cb = callback; _ Patches currently in -mm which might be from bradley.morgan@linux.dev are reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch reboot-register-legacy-pm_power_off-once-at-boot.patch ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] reboot: register legacy pm_power_off once at boot 2026-11-26 21:47 [PATCH 0/2] Fix a few snafus in the reboot code Bradley Morgan 2026-11-26 21:47 ` [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data Bradley Morgan @ 2026-11-26 21:47 ` Bradley Morgan 2026-12-03 21:30 ` + reboot-register-legacy-pm_power_off-once-at-boot.patch added to mm-nonmm-unstable branch Andrew Morton 1 sibling, 1 reply; 7+ messages in thread From: Bradley Morgan @ 2026-11-26 21:47 UTC (permalink / raw) To: Andrew Morton, Rafael J . Wysocki Cc: Dmitry Osipenko, Max Filippov, Chris Zankel, Andrew Davis, Oleg Nesterov, Petr Mladek, linux-kernel, Bradley Morgan In do_kernel_power_off(), the legacy pm_power_off wrapper gets registered on the fly and unregistered right after walking the poweroff chain. If pm_power_off is set, calling do_kernel_power_off() from xmon while system_state is still SYSTEM_RUNNING ends up calling kzalloc_obj() with GFP_KERNEL in atomic context, and on arches like arm64 or x86 that disable IRQs and stop the other CPUs before calling it, returning from pm_power_off() hits synchronize_rcu() in unregister_sys_off_handler() with interrupts disabled and secondary CPUs halted. To fix this, register legacy_pm_power_off once at core_initcall using a static notifier_block at priority SYS_OFF_PRIO_DEFAULT - 1 so default priority handlers still run ahead of it, and count registered SYS_OFF_MODE_POWER_OFF handlers in power_off_handlers so kernel_can_power_off() does not treat the static wrapper as a real one. Fixes: 2b8c612c6102 ("kernel/reboot: Fix powering off using a non-syscall code paths") Signed-off-by: Bradley Morgan <bradley.morgan@linux.dev> --- kernel/reboot.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/kernel/reboot.c b/kernel/reboot.c index 87f307ab46faa..12449adfcc61a 100644 --- a/kernel/reboot.c +++ b/kernel/reboot.c @@ -347,6 +347,7 @@ static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list); * to power off system. */ static ATOMIC_NOTIFIER_HEAD(power_off_handler_list); +static atomic_t power_off_handlers; static int sys_off_notify(struct notifier_block *nb, unsigned long mode, void *cmd) @@ -493,6 +494,9 @@ register_sys_off_handler(enum sys_off_mode mode, return ERR_PTR(err); } + if (mode == SYS_OFF_MODE_POWER_OFF) + atomic_inc(&power_off_handlers); + return handler; } EXPORT_SYMBOL_GPL(register_sys_off_handler); @@ -520,6 +524,9 @@ void unregister_sys_off_handler(struct sys_off_handler *handler) /* sanity check, shall never happen */ WARN_ON(err); + if (!err && handler->mode == SYS_OFF_MODE_POWER_OFF) + atomic_dec(&power_off_handlers); + free_sys_off_handler(handler); } EXPORT_SYMBOL_GPL(unregister_sys_off_handler); @@ -660,7 +667,8 @@ void unregister_platform_power_off(void (*power_off)(void)) } EXPORT_SYMBOL_GPL(unregister_platform_power_off); -static int legacy_pm_power_off(struct sys_off_data *data) +static int legacy_pm_power_off(struct notifier_block *nb, + unsigned long mode, void *cmd) { if (pm_power_off) pm_power_off(); @@ -668,6 +676,20 @@ static int legacy_pm_power_off(struct sys_off_data *data) return NOTIFY_DONE; } +static struct notifier_block legacy_pm_power_off_nb = { + .notifier_call = legacy_pm_power_off, + .priority = SYS_OFF_PRIO_DEFAULT - 1, +}; + +/* Keep pm_power_off static so do_kernel_power_off() cannot sleep */ +static int __init legacy_pm_init(void) +{ + atomic_notifier_chain_register(&power_off_handler_list, + &legacy_pm_power_off_nb); + return 0; +} +core_initcall(legacy_pm_init); + static void do_kernel_power_off_prepare(void) { blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); @@ -683,23 +705,7 @@ static void do_kernel_power_off_prepare(void) */ void do_kernel_power_off(void) { - struct sys_off_handler *sys_off = NULL; - - /* - * Register sys-off handlers for legacy PM callback. This allows - * legacy PM callbacks temporary co-exist with the new sys-off API. - * - * TODO: Remove legacy handlers once all legacy PM users will be - * switched to the sys-off based APIs. - */ - if (pm_power_off) - sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, - SYS_OFF_PRIO_DEFAULT, - legacy_pm_power_off, NULL); - atomic_notifier_call_chain(&power_off_handler_list, 0, NULL); - - unregister_sys_off_handler(sys_off); } /** @@ -710,8 +716,7 @@ void do_kernel_power_off(void) */ bool kernel_can_power_off(void) { - return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) || - pm_power_off; + return atomic_read(&power_off_handlers) > 0 || pm_power_off; } EXPORT_SYMBOL_GPL(kernel_can_power_off); -- 2.53.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* + reboot-register-legacy-pm_power_off-once-at-boot.patch added to mm-nonmm-unstable branch 2026-11-26 21:47 ` [PATCH 2/2] reboot: register legacy pm_power_off once at boot Bradley Morgan @ 2026-12-03 21:30 ` Andrew Morton 0 siblings, 0 replies; 7+ messages in thread From: Andrew Morton @ 2026-12-03 21:30 UTC (permalink / raw) To: mm-commits, rafael, pmladek, oleg, jcmvbkbc, dmitry.osipenko, chris, afd, bradley.morgan, akpm The patch titled Subject: reboot: register legacy pm_power_off once at boot has been added to the -mm mm-nonmm-unstable branch. Its filename is reboot-register-legacy-pm_power_off-once-at-boot.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/reboot-register-legacy-pm_power_off-once-at-boot.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Bradley Morgan <bradley.morgan@linux.dev> Subject: reboot: register legacy pm_power_off once at boot Date: Thu, 26 Nov 2026 21:47:54 +0000 In do_kernel_power_off(), the legacy pm_power_off wrapper gets registered on the fly and unregistered right after walking the poweroff chain. If pm_power_off is set, calling do_kernel_power_off() from xmon while system_state is still SYSTEM_RUNNING ends up calling kzalloc_obj() with GFP_KERNEL in atomic context, and on arches like arm64 or x86 that disable IRQs and stop the other CPUs before calling it, returning from pm_power_off() hits synchronize_rcu() in unregister_sys_off_handler() with interrupts disabled and secondary CPUs halted. To fix this, register legacy_pm_power_off once at core_initcall using a static notifier_block at priority SYS_OFF_PRIO_DEFAULT - 1 so default priority handlers still run ahead of it, and count registered SYS_OFF_MODE_POWER_OFF handlers in power_off_handlers so kernel_can_power_off() does not treat the static wrapper as a real one. Link: https://patch.msgid.link/20261126214754.3630965-3-bradley.morgan@linux.dev Fixes: 2b8c612c6102 ("kernel/reboot: Fix powering off using a non-syscall code paths") Signed-off-by: Bradley Morgan <bradley.morgan@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Rafael J . Wysocki <rafael@kernel.org> Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com> Cc: Max Filippov <jcmvbkbc@gmail.com> Cc: Chris Zankel <chris@zankel.net> Cc: Andrew Davis <afd@ti.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Petr Mladek <pmladek@suse.com> kernel/reboot.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) --- a/kernel/reboot.c~reboot-register-legacy-pm_power_off-once-at-boot +++ a/kernel/reboot.c @@ -347,6 +347,7 @@ static BLOCKING_NOTIFIER_HEAD(power_off_ * to power off system. */ static ATOMIC_NOTIFIER_HEAD(power_off_handler_list); +static atomic_t power_off_handlers; static int sys_off_notify(struct notifier_block *nb, unsigned long mode, void *cmd) @@ -493,6 +494,9 @@ register_sys_off_handler(enum sys_off_mo return ERR_PTR(err); } + if (mode == SYS_OFF_MODE_POWER_OFF) + atomic_inc(&power_off_handlers); + return handler; } EXPORT_SYMBOL_GPL(register_sys_off_handler); @@ -520,6 +524,9 @@ void unregister_sys_off_handler(struct s /* sanity check, shall never happen */ WARN_ON(err); + if (!err && handler->mode == SYS_OFF_MODE_POWER_OFF) + atomic_dec(&power_off_handlers); + free_sys_off_handler(handler); } EXPORT_SYMBOL_GPL(unregister_sys_off_handler); @@ -660,7 +667,8 @@ void unregister_platform_power_off(void } EXPORT_SYMBOL_GPL(unregister_platform_power_off); -static int legacy_pm_power_off(struct sys_off_data *data) +static int legacy_pm_power_off(struct notifier_block *nb, + unsigned long mode, void *cmd) { if (pm_power_off) pm_power_off(); @@ -668,6 +676,20 @@ static int legacy_pm_power_off(struct sy return NOTIFY_DONE; } +static struct notifier_block legacy_pm_power_off_nb = { + .notifier_call = legacy_pm_power_off, + .priority = SYS_OFF_PRIO_DEFAULT - 1, +}; + +/* Keep pm_power_off static so do_kernel_power_off() cannot sleep */ +static int __init legacy_pm_init(void) +{ + atomic_notifier_chain_register(&power_off_handler_list, + &legacy_pm_power_off_nb); + return 0; +} +core_initcall(legacy_pm_init); + static void do_kernel_power_off_prepare(void) { blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); @@ -683,23 +705,7 @@ static void do_kernel_power_off_prepare( */ void do_kernel_power_off(void) { - struct sys_off_handler *sys_off = NULL; - - /* - * Register sys-off handlers for legacy PM callback. This allows - * legacy PM callbacks temporary co-exist with the new sys-off API. - * - * TODO: Remove legacy handlers once all legacy PM users will be - * switched to the sys-off based APIs. - */ - if (pm_power_off) - sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, - SYS_OFF_PRIO_DEFAULT, - legacy_pm_power_off, NULL); - atomic_notifier_call_chain(&power_off_handler_list, 0, NULL); - - unregister_sys_off_handler(sys_off); } /** @@ -710,8 +716,7 @@ void do_kernel_power_off(void) */ bool kernel_can_power_off(void) { - return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) || - pm_power_off; + return atomic_read(&power_off_handlers) > 0 || pm_power_off; } EXPORT_SYMBOL_GPL(kernel_can_power_off); _ Patches currently in -mm which might be from bradley.morgan@linux.dev are reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch reboot-register-legacy-pm_power_off-once-at-boot.patch ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-03 21:30 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-26 21:47 [PATCH 0/2] Fix a few snafus in the reboot code Bradley Morgan 2026-11-26 21:47 ` [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data Bradley Morgan 2026-12-03 5:50 ` Andrew Morton 2026-12-03 5:53 ` Andrew Morton 2026-12-03 21:30 ` + reboot-fix-platform-sys_off-handler-reuse-with-null-cb_data.patch added to mm-nonmm-unstable branch Andrew Morton 2026-11-26 21:47 ` [PATCH 2/2] reboot: register legacy pm_power_off once at boot Bradley Morgan 2026-12-03 21:30 ` + reboot-register-legacy-pm_power_off-once-at-boot.patch added to mm-nonmm-unstable branch Andrew Morton
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.