Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Xuanqiang Luo <xuanqiang.luo@linux.dev>
To: printk@lists.linux.dev
Cc: Petr Mladek <pmladek@suse.com>,
	John Ogness <john.ogness@linutronix.de>,
	Steven Rostedt <rostedt@goodmis.org>,
	Sergey Senozhatsky <senozhatsky@chromium.org>,
	linux-kernel@vger.kernel.org,
	Xuanqiang Luo <luoxuanqiang@kylinos.cn>,
	stable@vger.kernel.org
Subject: [PATCH v1] printk: clear saved_console_loglevel on /proc/sys/kernel/printk writes
Date: Fri, 27 Nov 2026 16:15:48 +0900	[thread overview]
Message-ID: <20261127071548.333115-1-xuanqiang.luo@linux.dev> (raw)

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

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@vger.kernel.org
Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 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


                 reply	other threads:[~2026-11-27  7:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261127071548.333115-1-xuanqiang.luo@linux.dev \
    --to=xuanqiang.luo@linux.dev \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luoxuanqiang@kylinos.cn \
    --cc=pmladek@suse.com \
    --cc=printk@lists.linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=stable@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox