Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ldmsys/648e2a50b799b07bde8a1973d9d28a6d to your computer and use it in GitHub Desktop.
Save ldmsys/648e2a50b799b07bde8a1973d9d28a6d to your computer and use it in GitHub Desktop.
From dd0dc8f645a038a50de65664ac072db5ca069931 Mon Sep 17 00:00:00 2001
From: Dongmin Lee <ldmldm05@gmail.com>
Date: Fri, 3 Nov 2023 12:59:28 +0900
Subject: [PATCH] kernel/reboot: Explictly notify if halt occurred instead of
power off
When kernel_can_power_off() returns false, and reboot has called with
LINUX_REBOOT_CMD_POWER_OFF, kernel_halt() will be initiated instead of
actual power off function.
However, in this situation, Kernel never explictly notifies user that
system halted instead of requested power off.
Since halt and power off perform different behavior, and user initiated
reboot call with power off command, not halt, This could be unintended
behavior to user, like this:
~ # poweroff -f
[ 3.581482] reboot: System halted
Therefore, this explictly notifies user that poweroff is not available,
and halting has been occured as a alternative behavior instead:
~ # poweroff -f
[ 4.123668] reboot: Power off not available: System halted instead
Signed-off-by: Dongmin Lee <ldmldm05@gmail.com>
---
kernel/reboot.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/kernel/reboot.c b/kernel/reboot.c
index 395a0ea3c7a8..dd33b07cc2f1 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -58,6 +58,13 @@ struct sys_off_handler {
struct device *dev;
};
+/*
+ * This variable is used to indicate if a halt initiated instead when
+ * reboot call is invoked with LINUX_REBOOT_CMD_POWER_OFF, but system
+ * cannot be powered off. This allowes kernel_halt() to notify that.
+ */
+int poweroff_fallback_to_halt = 0;
+
/*
* Temporary stub that prevents linkage failure while we're in process
* of removing all uses of legacy pm_power_off() around the kernel.
@@ -297,7 +304,10 @@ void kernel_halt(void)
kernel_shutdown_prepare(SYSTEM_HALT);
migrate_to_reboot_cpu();
syscore_shutdown();
- pr_emerg("System halted\n");
+ if(poweroff_fallback_to_halt)
+ pr_emerg("Power off not available: System halted instead\n");
+ else
+ pr_emerg("System halted\n");
kmsg_dump(KMSG_DUMP_SHUTDOWN);
machine_halt();
}
@@ -732,8 +742,10 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
/* Instead of trying to make the power_off code look like
* halt when pm_power_off is not set do it the easy way.
*/
- if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off())
+ if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off()) {
+ poweroff_fallback_to_halt = 1;
cmd = LINUX_REBOOT_CMD_HALT;
+ }
mutex_lock(&system_transition_mutex);
switch (cmd) {
base-commit: bc3012f4e3a9765de81f454cb8f9bb16aafc6ff5
--
2.39.3 (Apple Git-145)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment