mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] reboot: log the task that initiated the reboot
@ 2026-08-27 22:57 Bradley Morgan
  2026-08-27 22:57 ` [PATCH 2/2] kexec: log the task that initiated the kexec reboot Bradley Morgan
  0 siblings, 1 reply; 2+ messages in thread
From: Bradley Morgan @ 2026-08-27 22:57 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Kees Cook, Baoquan He, linux-kernel, brads

From: Bradley Morgan <brads@mainlining.org>

When a machine reboots or powers off, the kernel log records what
happened but not who asked for it. The reboot syscall throws the
caller identity away, and userspace does not reliably record it either:
systemd only journals shutdowns that go through logind, anything calling
reboot(2) directly (watchdog daemons, container agents, orchestration
tooling) leaves no record at all, and the journal is being torn down
while the machine goes away, so even the lines that should be written
can be lost. The kernel is the only place that always sees the caller,
and a kernel log line survives via pstore or a serial console when
userspace logs do not.

Log the comm and pid of the calling task in the reboot syscall, once
the requested command is committed and can no longer fail, e.g:

  reboot: initiated by systemd-shutdow[1]
  reboot: Restarting system

The existing "Restarting system", "System halted" and "Power down"
lines are left untouched, so anything parsing dmesg today keeps working.
The two ctrl alt del toggle commands are excluded so init setting the
mode does not add a line to dmesg on every boot.

Signed-off-by: Bradley Morgan <brads@mainlining.org>
---
 include/linux/reboot.h |  1 +
 kernel/reboot.c        | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/include/linux/reboot.h b/include/linux/reboot.h
index aa08c3bbbf59..3fb8d8533563 100644
--- a/include/linux/reboot.h
+++ b/include/linux/reboot.h
@@ -172,6 +172,7 @@ extern void kernel_restart(char *cmd);
 extern void kernel_halt(void);
 extern void kernel_power_off(void);
 extern bool kernel_can_power_off(void);
+void reboot_log_initiator(void);
 
 void ctrl_alt_del(void);
 
diff --git a/kernel/reboot.c b/kernel/reboot.c
index f070c5c1103a..42517e0b621b 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -717,6 +717,19 @@ EXPORT_SYMBOL_GPL(kernel_power_off);
 
 DEFINE_MUTEX(system_transition_mutex);
 
+/*
+ * Log the task that asked for the transition once the requested
+ * command is committed and can no longer fail, e.g:
+ *
+ *   reboot: initiated by systemd-shutdow[1]
+ *   reboot: Restarting system
+ */
+void reboot_log_initiator(void)
+{
+	pr_emerg("initiated by %s[%d]\n", current->comm, task_pid_nr(current));
+}
+EXPORT_SYMBOL_GPL(reboot_log_initiator);
+
 /*
  * Reboot system call: for obvious reasons only root may call it,
  * and even root needs to set up some magic numbers in the registers
@@ -764,6 +777,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 	mutex_lock(&system_transition_mutex);
 	switch (cmd) {
 	case LINUX_REBOOT_CMD_RESTART:
+		reboot_log_initiator();
 		kernel_restart(NULL);
 		break;
 
@@ -776,10 +790,12 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		break;
 
 	case LINUX_REBOOT_CMD_HALT:
+		reboot_log_initiator();
 		kernel_halt();
 		do_exit(0);
 
 	case LINUX_REBOOT_CMD_POWER_OFF:
+		reboot_log_initiator();
 		kernel_power_off();
 		do_exit(0);
 		break;
@@ -792,6 +808,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		}
 		buffer[sizeof(buffer) - 1] = '\0';
 
+		reboot_log_initiator();
 		kernel_restart(buffer);
 		break;
 
-- 
2.47.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] kexec: log the task that initiated the kexec reboot
  2026-08-27 22:57 [PATCH 1/2] reboot: log the task that initiated the reboot Bradley Morgan
@ 2026-08-27 22:57 ` Bradley Morgan
  0 siblings, 0 replies; 2+ messages in thread
From: Bradley Morgan @ 2026-08-27 22:57 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Kees Cook, Baoquan He, linux-kernel, brads

From: Bradley Morgan <brads@mainlining.org>

Give the kexec path the same treatment as the reboot syscall.

Log the comm and pid of the calling task after kexec_in_progress is set
to true but before kernel_restart_prepare() shuts down devices, so the
message survives even if device shutdown hangs, e.g:

  reboot: initiated by systemd-shutdow[1]
  kexec: Starting new kernel

The existing "Starting new kernel" line is left untouched, so anything
parsing dmesg today keeps working.

Signed-off-by: Bradley Morgan <brads@mainlining.org>
---
 kernel/kexec_core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index dc770b9a6d05..d940b0c29f86 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -1189,6 +1189,7 @@ int kernel_kexec(void)
 #endif
 	{
 		kexec_in_progress = true;
+		reboot_log_initiator();
 		kernel_restart_prepare("kexec reboot");
 		migrate_to_reboot_cpu();
 		syscore_shutdown();
-- 
2.47.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-27 22:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-27 22:57 [PATCH 1/2] reboot: log the task that initiated the reboot Bradley Morgan
2026-08-27 22:57 ` [PATCH 2/2] kexec: log the task that initiated the kexec reboot Bradley Morgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®