All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bradley Morgan <include@grrlz.net>
To: akpm@linux-foundation.org
Cc: pavel@kernel.org, tony.luck@intel.com, baoquan.he@linux.dev,
	rafael@kernel.org, kees@kernel.org, linux-pm@vger.kernel.org,
	kexec@lists.infradead.org, linux-kernel@vger.kernel.org,
	rppt@kernel.org, Bradley Morgan <include@grrlz.net>,
	pasha.tatashin@soleen.com, pratyush@kernel.org, lenb@kernel.org
Subject: [PATCH v2 1/3] reboot: log the task that initiated the reboot
Date: Tue, 21 Jul 2026 16:13:56 +0100	[thread overview]
Message-ID: <20260721151358.4185467-2-include@grrlz.net> (raw)
In-Reply-To: <20260721151358.4185467-1-include@grrlz.net>

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 <include@grrlz.net>
---
 include/linux/reboot.h |  1 +
 kernel/reboot.c        | 16 ++++++++++++++++
 2 files changed, 17 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 bed6967bfa96..3f4534cd5b9c 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -717,6 +717,18 @@ 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_info("initiated by %s[%d]\n", current->comm, task_pid_nr(current));
+}
+
 /*
  * 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 +776,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 +789,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 +807,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		}
 		buffer[sizeof(buffer) - 1] = '\0';
 
+		reboot_log_initiator();
 		kernel_restart(buffer);
 		break;
 
-- 
2.50.1



WARNING: multiple messages have this Message-ID (diff)
From: Bradley Morgan <include@grrlz.net>
To: akpm@linux-foundation.org
Cc: baoquan.he@linux.dev, rppt@kernel.org, pasha.tatashin@soleen.com,
	pratyush@kernel.org, rafael@kernel.org, lenb@kernel.org,
	pavel@kernel.org, kees@kernel.org, tony.luck@intel.com,
	gpiccoli@igalia.com, linux-kernel@vger.kernel.org,
	kexec@lists.infradead.org, linux-pm@vger.kernel.org,
	Bradley Morgan <include@grrlz.net>
Subject: [PATCH v2 1/3] reboot: log the task that initiated the reboot
Date: Tue, 21 Jul 2026 16:13:56 +0100	[thread overview]
Message-ID: <20260721151358.4185467-2-include@grrlz.net> (raw)
In-Reply-To: <20260721151358.4185467-1-include@grrlz.net>

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 <include@grrlz.net>
---
 include/linux/reboot.h |  1 +
 kernel/reboot.c        | 16 ++++++++++++++++
 2 files changed, 17 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 bed6967bfa96..3f4534cd5b9c 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -717,6 +717,18 @@ 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_info("initiated by %s[%d]\n", current->comm, task_pid_nr(current));
+}
+
 /*
  * 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 +776,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 +789,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 +807,7 @@ SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
 		}
 		buffer[sizeof(buffer) - 1] = '\0';
 
+		reboot_log_initiator();
 		kernel_restart(buffer);
 		break;
 
-- 
2.50.1


  reply	other threads:[~2026-07-21 15:14 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-21 15:13 [PATCH v2 0/3] reboot: log the task that initiated Bradley Morgan
2026-07-21 15:13 ` Bradley Morgan
2026-07-21 15:13 ` Bradley Morgan [this message]
2026-07-21 15:13   ` [PATCH v2 1/3] reboot: log the task that initiated the reboot Bradley Morgan
2026-07-21 15:13 ` [PATCH v2 2/3] kexec: log the task that initiated the kexec reboot Bradley Morgan
2026-07-21 15:13   ` Bradley Morgan
2026-07-21 15:13 ` [PATCH v2 3/3] PM: hibernate: log the task that initiated the power down Bradley Morgan
2026-07-21 15:13   ` Bradley Morgan
2026-07-23 13:55   ` Rafael J. Wysocki (Intel)
2026-07-23 13:55     ` Rafael J. Wysocki (Intel)
2026-07-23 14:46     ` Bradley Morgan
2026-07-23 14:46       ` Bradley Morgan
2026-07-23 14:55       ` Rafael J. Wysocki (Intel)
2026-07-23 14:55         ` Rafael J. Wysocki (Intel)
2026-07-23 15:38         ` Bradley Morgan
2026-07-23 15:38           ` Bradley Morgan

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=20260721151358.4185467-2-include@grrlz.net \
    --to=include@grrlz.net \
    --cc=akpm@linux-foundation.org \
    --cc=baoquan.he@linux.dev \
    --cc=kees@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=lenb@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=pavel@kernel.org \
    --cc=pratyush@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rppt@kernel.org \
    --cc=tony.luck@intel.com \
    /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 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.