mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Down <chris@chrisdown.name>
To: Petr Mladek <pmladek@suse.com>
Cc: linux-kernel@vger.kernel.org,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sergey Senozhatsky <senozhatsky@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	John Ogness <john.ogness@linutronix.de>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Tony Lindgren <tony.lindgren@linux.intel.com>,
	kernel-team@fb.com
Subject: [PATCH v7 05/13] printk: Add synchronisation for concurrent console state changes
Date: Wed, 19 Nov 2025 03:07:17 +0800	[thread overview]
Message-ID: <9ffc0801a64e5f7d0685c263c1908a34cf89d1a5.1763492585.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1763492585.git.chris@chrisdown.name>

The syslog actions SYSLOG_ACTION_CONSOLE_OFF and
SYSLOG_ACTION_CONSOLE_ON currently run without any locking. This creates
a race condition if two processes attempt to toggle the console state
simultaneously.

While this race has existed for donkey's years, it was previously
somewhat tolerable because it involved only a single integer variable
(saved_console_loglevel). However, upcoming changes will introduce a
second piece of state (saved_ignore_per_console_loglevel) to be saved
and restored. With two variables, this can result in saved state getting
lost.

Here is a demonstration:

 CPU 0 (SYSLOG_ACTION_CONSOLE_OFF)        CPU 1 (SYSLOG_ACTION_CONSOLE_ON)
 ---------------------------------        --------------------------------
 // saved_console_loglevel is DEFAULT
 if (saved == DEFAULT) (True)
   saved = console_loglevel (7)
                                          // Race triggers here
                                          if (saved != DEFAULT) (True)
                                            loglevel = saved (7)
                                            saved = DEFAULT
   // CPU 0 continues, unaware saved
   // was just reset by CPU 1
   loglevel = minimum_console_loglevel (1)

The result is that the console is now set to the minimum loglevel, but
saved_console_loglevel is LOGLEVEL_DEFAULT. A subsequent CONSOLE_ON call
will see saved == LOGLEVEL_DEFAULT and thus refuse to restore the
original loglevel. The console is effectively stuck in quiet mode until
manually reset via sysctl. Oh dear.

The callers of do_syslog are syscalls (so user context) or procfs write
handlers. These contexts are allowed to sleep, so acquiring a mutex is
safe. We also already make use of syslog_lock in
SYSLOG_ACTION_SIZE_UNREAD and SYSLOG_ACTION_READ, so this is consistent.

Signed-off-by: Chris Down <chris@chrisdown.name>
---
 kernel/printk/printk.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 928d77c56c77..b8679b0da42f 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1903,16 +1903,20 @@ int do_syslog(int type, char __user *buf, int len, int source)
 		break;
 	/* Disable logging to console */
 	case SYSLOG_ACTION_CONSOLE_OFF:
+		mutex_lock(&syslog_lock);
 		if (saved_console_loglevel == LOGLEVEL_DEFAULT)
 			saved_console_loglevel = console_loglevel;
 		console_loglevel = minimum_console_loglevel;
+		mutex_unlock(&syslog_lock);
 		break;
 	/* Enable logging to console */
 	case SYSLOG_ACTION_CONSOLE_ON:
+		mutex_lock(&syslog_lock);
 		if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
 			console_loglevel = saved_console_loglevel;
 			saved_console_loglevel = LOGLEVEL_DEFAULT;
 		}
+		mutex_unlock(&syslog_lock);
 		break;
 	/* Set level of messages printed to console */
 	case SYSLOG_ACTION_CONSOLE_LEVEL:
-- 
2.51.2


  parent reply	other threads:[~2025-11-18 19:07 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-18 19:06 [PATCH v7 00/13] printk: console: Per-console loglevels Chris Down
2025-11-18 19:06 ` [PATCH v7 01/13] printk: Avoid delaying messages that aren't solicited by any console Chris Down
2025-11-18 19:33   ` Chris Down
2025-11-19 15:46     ` Petr Mladek
2025-11-18 19:06 ` [PATCH v7 02/13] printk: Use effective loglevel for suppression and extended console state Chris Down
2025-11-18 19:07 ` [PATCH v7 03/13] printk: console: Implement core per-console loglevel infrastructure Chris Down
2025-11-18 19:34   ` Chris Down
2025-11-19 16:49   ` Petr Mladek
2025-11-18 19:07 ` [PATCH v7 04/13] printk: Ignore per-console loglevel in sysrq Chris Down
2025-11-19 16:51   ` Petr Mladek
2025-11-18 19:07 ` Chris Down [this message]
2025-11-19 16:58   ` [PATCH v7 05/13] printk: Add synchronisation for concurrent console state changes Petr Mladek
2025-11-18 19:07 ` [PATCH v7 06/13] printk: Support toggling per-console loglevel via syslog() and cmdline Chris Down
2025-11-20 10:05   ` Petr Mladek
2025-11-18 19:07 ` [PATCH v7 07/13] printk: console: Introduce sysfs interface for per-console loglevels Chris Down
2025-11-20 16:50   ` Petr Mladek
2025-11-21 11:38   ` Petr Mladek
2025-11-18 19:07 ` [PATCH v7 08/13] printk: Constrain hardware-addressed console checks to name position Chris Down
2025-11-18 19:07 ` [PATCH v7 09/13] printk: Support setting initial console loglevel via console= on cmdline Chris Down
2025-11-21 14:05   ` Petr Mladek
2025-11-18 19:07 ` [PATCH v7 10/13] printk: Add sysctl interface to set global loglevels Chris Down
2025-11-21 14:23   ` Petr Mladek
2025-11-18 19:08 ` [PATCH v7 11/13] printk: docs: Add comprehensive guidance for per-console loglevels Chris Down
2025-11-21 15:52   ` Petr Mladek
2025-11-23 21:21   ` John Ogness
2025-11-18 19:08 ` [PATCH v7 12/13] printk: Deprecate the kernel.printk sysctl interface Chris Down
2025-11-18 19:08 ` [PATCH v7 13/13] printk: Purge default_console_loglevel Chris Down

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=9ffc0801a64e5f7d0685c263c1908a34cf89d1a5.1763492585.git.chris@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=geert@linux-m68k.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=john.ogness@linutronix.de \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=tony.lindgren@linux.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 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®