The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Bradley Morgan <bradley.morgan@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>
Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>,
	Max Filippov <jcmvbkbc@gmail.com>,
	Chris Zankel <chris@zankel.net>, Andrew Davis <afd@ti.com>,
	Oleg Nesterov <oleg@redhat.com>, Petr Mladek <pmladek@suse.com>,
	linux-kernel@vger.kernel.org,
	Bradley Morgan <bradley.morgan@linux.dev>
Subject: [PATCH 2/2] reboot: register legacy pm_power_off once at boot
Date: Thu, 26 Nov 2026 21:47:54 +0000	[thread overview]
Message-ID: <20261126214754.3630965-3-bradley.morgan@linux.dev> (raw)
In-Reply-To: <20261126214754.3630965-1-bradley.morgan@linux.dev>

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


      parent reply	other threads:[~2026-11-26 21:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126214754.3630965-3-bradley.morgan@linux.dev \
    --to=bradley.morgan@linux.dev \
    --cc=afd@ti.com \
    --cc=akpm@linux-foundation.org \
    --cc=chris@zankel.net \
    --cc=dmitry.osipenko@collabora.com \
    --cc=jcmvbkbc@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=pmladek@suse.com \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox