[PATCH v1] printk: clear saved_console_loglevel on /proc/sys/kernel/printk writes

From: Xuanqiang Luo

Date: Fri Nov 27 2026 - 02:16:10 EST


From: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>

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")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Xuanqiang Luo <luoxuanqiang@xxxxxxxxxx>
---
kernel/printk/internal.h | 2 ++
kernel/printk/printk.c | 50 ++++++++++++++++++++++++++++++++++++----
kernel/printk/sysctl.c | 2 +-
3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 85fbf1801cbe..8bae853c64b7 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -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
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 5dbcf8a9909b..c6d2bec573b6 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1737,11 +1737,17 @@ static void syslog_clear(void)
mutex_unlock(&syslog_lock);
}

+/*
+ * Protects @saved_console_loglevel and serializes syslog(2) and
+ * /proc/sys/kernel/printk updates to console_printk[].
+ */
+static DEFINE_MUTEX(console_loglevel_lock);
+static int saved_console_loglevel = LOGLEVEL_DEFAULT;
+
int do_syslog(int type, char __user *buf, int len, int source)
{
struct printk_info info;
bool clear = false;
- static int saved_console_loglevel = LOGLEVEL_DEFAULT;
int error;

error = check_syslog_permissions(type, source);
@@ -1782,26 +1788,32 @@ int do_syslog(int type, char __user *buf, int len, int source)
break;
/* Disable logging to console */
case SYSLOG_ACTION_CONSOLE_OFF:
+ mutex_lock(&console_loglevel_lock);
if (saved_console_loglevel == LOGLEVEL_DEFAULT)
- saved_console_loglevel = console_loglevel;
- console_loglevel = minimum_console_loglevel;
+ saved_console_loglevel = READ_ONCE(console_loglevel);
+ WRITE_ONCE(console_loglevel, minimum_console_loglevel);
+ mutex_unlock(&console_loglevel_lock);
break;
/* Enable logging to console */
case SYSLOG_ACTION_CONSOLE_ON:
+ mutex_lock(&console_loglevel_lock);
if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
- console_loglevel = saved_console_loglevel;
+ WRITE_ONCE(console_loglevel, saved_console_loglevel);
saved_console_loglevel = LOGLEVEL_DEFAULT;
}
+ mutex_unlock(&console_loglevel_lock);
break;
/* Set level of messages printed to console */
case SYSLOG_ACTION_CONSOLE_LEVEL:
if (len < 1 || len > 8)
return -EINVAL;
+ mutex_lock(&console_loglevel_lock);
if (len < minimum_console_loglevel)
len = minimum_console_loglevel;
- console_loglevel = len;
+ WRITE_ONCE(console_loglevel, len);
/* Implicitly re-enable logging to console */
saved_console_loglevel = LOGLEVEL_DEFAULT;
+ mutex_unlock(&console_loglevel_lock);
break;
/* Number of chars in the log buffer */
case SYSLOG_ACTION_SIZE_UNREAD:
@@ -1855,6 +1867,34 @@ SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
return do_syslog(type, buf, len, SYSLOG_FROM_READER);
}

+#ifdef CONFIG_SYSCTL
+int printk_sysctl_set_loglvl(const struct ctl_table *table, int write,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ loff_t pos = *ppos;
+ int old_loglevel;
+ int err;
+
+ if (!write)
+ return proc_dointvec(table, write, buffer, lenp, ppos);
+
+ mutex_lock(&console_loglevel_lock);
+ old_loglevel = READ_ONCE(console_loglevel);
+ err = proc_dointvec(table, write, buffer, lenp, ppos);
+ /*
+ * Implicitly re-enable logging to console. Under SYSCTL_WRITES_STRICT,
+ * non-zero-offset writes return 0 without updating table->data, so
+ * require either offset 0 or an observed console_loglevel change.
+ */
+ if (!err && *lenp &&
+ (!pos || READ_ONCE(console_loglevel) != old_loglevel))
+ saved_console_loglevel = LOGLEVEL_DEFAULT;
+ mutex_unlock(&console_loglevel_lock);
+
+ return err;
+}
+#endif /* CONFIG_SYSCTL */
+
/*
* Special console_lock variants that help to reduce the risk of soft-lockups.
* They allow to pass console_lock to another printk() call using a busy wait.
diff --git a/kernel/printk/sysctl.c b/kernel/printk/sysctl.c
index f15732e93c2e..a2c204320898 100644
--- a/kernel/printk/sysctl.c
+++ b/kernel/printk/sysctl.c
@@ -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
--
2.43.0