Skip to contentSkip to conversation

printk: clear saved_console_loglevel on /proc/sys/kernel/printk writes

PATCH New Xuanqiang Luo 1 participant 1 message Nov 27

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 printk: clear saved_console_loglevel on /proc/sys/kernel/printk writes

From: Xuanqiang Luo <[email protected]>

When console logging is temporarily disabled via syslog(2) SYSLOG_ACTION_CONSOLE_OFF (e.g., "dmesg -D" or Plymouth during boot), do_syslog() stores console_loglevel in saved_console_loglevel and later restores it on SYSLOG_ACTION_CONSOLE_ON (e.g., "dmesg -E" or plymouth-quit).

Commit 1aaad49e856c ("printk: Restore previous console_loglevel when re-enabling logging") clears saved_console_loglevel on SYSLOG_ACTION_CONSOLE_LEVEL ("dmesg -n") so an explicit loglevel update implicitly re-enables console logging. However, /proc/sys/kernel/printk uses proc_dointvec() directly and leaves saved_console_loglevel stale. If systemd-sysctl sets kernel.printk (e.g., "4 4 1 7") while console logging is disabled during boot, a later SYSLOG_ACTION_CONSOLE_ON silently overwrites console_loglevel back to the old saved value (e.g., 7), flooding slow serial consoles with notice/info messages.

Move saved_console_loglevel to file scope and wrap proc_dointvec() in printk_sysctl_set_loglvl() to reset saved_console_loglevel to LOGLEVEL_DEFAULT on successful writes, while leaving it intact on reads, empty/invalid writes, and non-zero-offset writes ignored under SYSCTL_WRITES_STRICT. Because /proc/sys/kernel/printk always writes starting at console_loglevel, valid offset-0 writes clear saved_console_loglevel even when re-writing the current loglevel (e.g., kernel.printk = "1 4 1 7"), matching SYSLOG_ACTION_CONSOLE_LEVEL semantics (at the cost of also clearing it if a caller rewrites console_loglevel solely to adjust later fields).

Serialize saved_console_loglevel and console_printk[] updates across do_syslog() and /proc/sys/kernel/printk with a dedicated console_loglevel_lock mutex (using READ_ONCE/WRITE_ONCE for console_loglevel to match proc_dointvec()), keeping syslog_lock scoped to ringbuffer reader state and avoiding contention or lock-ordering dependencies with syslog readers and console registration.

Fixes 1aaad49e856c ("printk: Restore previous console_loglevel when re-enabling logging")
---
 kernel/printk/internal.h |  2 ++
 kernel/printk/printk.c   | 50 ++++++++++++++++++++++++++++++++++++----
 kernel/printk/sysctl.c   |  2 +-
 3 files changed, 48 insertions(+), 6 deletions(-)
kernel/printk/internal.h +2 -0
index 85fbf1801cbe..8bae853c64b7 100644
@@ -10,6 +10,8 @@
 void __init printk_sysctl_init(void);
 int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write,
 			      void *buffer, size_t *lenp, loff_t *ppos);
+int printk_sysctl_set_loglvl(const struct ctl_table *table, int write,
+			     void *buffer, size_t *lenp, loff_t *ppos);
 #else
 #define printk_sysctl_init() do { } while (0)
 #endif
kernel/printk/printk.c +45 -5

88 lines not on this page · raw message

kernel/printk/sysctl.c +1 -1
index f15732e93c2e..a2c204320898 100644
@@ -25,7 +25,7 @@ static const struct ctl_table printk_sysctls[] = {
 		.data		= &console_loglevel,
 		.maxlen		= 4*sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= printk_sysctl_set_loglvl,
 	},
 	{
 		.procname	= "printk_ratelimit",

base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48

Signature · 1 line
2.43.0