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 v8 13/21] printk: Toggle ignore_per_console_loglevel via syslog
Date: Fri, 28 Nov 2025 03:44:01 +0800	[thread overview]
Message-ID: <299a3c124f4e0ece36f2a04db2d4d2143e66ebca.1764272407.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1764272407.git.chris@chrisdown.name>

The syslog() system call provides SYSLOG_ACTION_CONSOLE_OFF and
SYSLOG_ACTION_CONSOLE_ON to disable and re-enable console logging.
These actions save and restore the global console_loglevel.

With per-console loglevels, simply setting console_loglevel to
minimum_console_loglevel during CONSOLE_OFF is insufficient - consoles
with explicit per-console loglevels would continue printing based on
their local settings.

Augment these actions to also save and restore the state of
ignore_per_console_loglevel. When console logging is disabled via
syslog(), ignore_per_console_loglevel is set to true so that the
minimum_console_loglevel applies to all consoles. When logging is
re-enabled, the previous ignore_per_console_loglevel state is restored.

A forced flag tracks whether this code path actually set
ignore_per_console_loglevel. This prevents blindly restoring stale
state if other contexts (sysrq, module parameter writes) legitimately
modified the flag in parallel.

SYSLOG_ACTION_CONSOLE_LEVEL now emits a one-time warning when
per-console loglevels are active, informing administrators that their
level change only affects consoles using the global loglevel. The
level change also properly restores ignore_per_console_loglevel state
if it was forced by a previous CONSOLE_OFF.

These changes allow userspace tools like dmesg to properly control
console output even in the presence of per-console loglevels.

Reviewed-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
 kernel/printk/printk.c | 53 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 45 insertions(+), 8 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index e5b6a926135e..1d28887e7218 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1869,6 +1869,8 @@ 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;
+	static bool saved_ignore_per_console_loglevel;
+	static bool saved_ignore_per_console_loglevel_forced;
 	int error;
 
 	error = check_syslog_permissions(type, source);
@@ -1910,9 +1912,23 @@ int do_syslog(int type, char __user *buf, int len, int source)
 	/* Disable logging to console */
 	case SYSLOG_ACTION_CONSOLE_OFF:
 		mutex_lock(&syslog_lock);
-		if (saved_console_loglevel == LOGLEVEL_DEFAULT)
+		if (saved_console_loglevel == LOGLEVEL_DEFAULT) {
 			saved_console_loglevel = console_loglevel;
+			saved_ignore_per_console_loglevel = ignore_per_console_loglevel;
+			saved_ignore_per_console_loglevel_forced = false;
+		}
 		console_loglevel = minimum_console_loglevel;
+		if (!ignore_per_console_loglevel) {
+			/*
+			 * Only remember the saved value if this path actually
+			 * forced the override. Other contexts (sysrq, module
+			 * parameter writes, etc.) can legitimately toggle the
+			 * flag in parallel, so blindly restoring it later would
+			 * resurrect stale state.
+			 */
+			ignore_per_console_loglevel = true;
+			saved_ignore_per_console_loglevel_forced = true;
+		}
 		mutex_unlock(&syslog_lock);
 		break;
 	/* Enable logging to console */
@@ -1920,20 +1936,41 @@ int do_syslog(int type, char __user *buf, int len, int source)
 		mutex_lock(&syslog_lock);
 		if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
 			console_loglevel = saved_console_loglevel;
+			if (saved_ignore_per_console_loglevel_forced)
+				ignore_per_console_loglevel = saved_ignore_per_console_loglevel;
 			saved_console_loglevel = LOGLEVEL_DEFAULT;
+			saved_ignore_per_console_loglevel_forced = false;
 		}
 		mutex_unlock(&syslog_lock);
 		break;
 	/* Set level of messages printed to console */
-	case SYSLOG_ACTION_CONSOLE_LEVEL:
-		if (len < 1 || len > 8)
+	case SYSLOG_ACTION_CONSOLE_LEVEL: {
+		int new_level = len;
+
+		if (!ignore_per_console_loglevel)
+			pr_warn_once(
+				"SYSLOG_ACTION_CONSOLE_LEVEL is ignored by consoles with an explicitly set per-console loglevel, see Documentation/admin-guide/per-console-loglevel.rst\n");
+		if (new_level < 1 || new_level > 8)
 			return -EINVAL;
-		if (len < minimum_console_loglevel)
-			len = minimum_console_loglevel;
-		console_loglevel = len;
-		/* Implicitly re-enable logging to console */
-		saved_console_loglevel = LOGLEVEL_DEFAULT;
+		if (new_level < minimum_console_loglevel)
+			new_level = minimum_console_loglevel;
+
+		mutex_lock(&syslog_lock);
+		console_loglevel = new_level;
+		/*
+		 * If SYSLOG_ACTION_CONSOLE_OFF forced ignore_per_console_loglevel,
+		 * restore the previous setting so per-console loglevels continue
+		 * to work after administrators re-enable logging via syslog().
+		 */
+		if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
+			if (saved_ignore_per_console_loglevel_forced)
+				ignore_per_console_loglevel = saved_ignore_per_console_loglevel;
+			saved_console_loglevel = LOGLEVEL_DEFAULT;
+			saved_ignore_per_console_loglevel_forced = false;
+		}
+		mutex_unlock(&syslog_lock);
 		break;
+	}
 	/* Number of chars in the log buffer */
 	case SYSLOG_ACTION_SIZE_UNREAD:
 		mutex_lock(&syslog_lock);
-- 
2.51.2


  parent reply	other threads:[~2025-11-27 19:44 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27 19:43 [PATCH v8 00/21] printk: console: Per-console loglevels Chris Down
2025-11-27 19:43 ` [PATCH v8 01/21] printk: Fully resolve loglevel before deciding printk delay suppression Chris Down
2025-12-09 16:40   ` Petr Mladek
2025-12-11 14:49     ` Petr Mladek
2025-12-11 15:28       ` Petr Mladek
2025-11-27 19:43 ` [PATCH v8 02/21] printk: Avoid spuriously delaying messages not solicited by any console Chris Down
2025-11-27 19:43 ` [PATCH v8 03/21] printk: Prioritise user-specified configuration over SPCR/DT Chris Down
2025-12-10 14:38   ` Petr Mladek
2025-11-27 19:43 ` [PATCH v8 04/21] printk: Use effective loglevel for suppression and extended console state Chris Down
2025-11-27 19:43 ` [PATCH v8 05/21] printk: console: Add per-console loglevel support to struct console Chris Down
2025-11-27 19:43 ` [PATCH v8 06/21] printk: nbcon: Synchronise console unregistration against atomic flushers Chris Down
2025-12-10 15:12   ` Petr Mladek
2025-11-27 19:43 ` [PATCH v8 07/21] printk: Introduce per-console loglevel support Chris Down
2025-11-27 19:43 ` [PATCH v8 08/21] printk: Iterate registered consoles for delay suppression decisions Chris Down
2025-11-27 19:43 ` [PATCH v8 09/21] printk: Optimise printk_delay() to avoid walking consoles under SRCU Chris Down
2025-12-11 14:37   ` Petr Mladek
2025-11-27 19:43 ` [PATCH v8 10/21] printk: Add synchronisation for concurrent console state changes Chris Down
2025-11-27 19:43 ` [PATCH v8 11/21] printk: Add ignore_per_console_loglevel module parameter Chris Down
2025-11-27 19:43 ` [PATCH v8 12/21] printk: Ensure sysrq output bypasses per-console filtering Chris Down
2025-11-27 19:44 ` Chris Down [this message]
2025-11-27 19:44 ` [PATCH v8 14/21] printk: console: Introduce sysfs interface for per-console loglevels Chris Down
2025-12-12 14:04   ` Petr Mladek
2025-11-27 19:44 ` [PATCH v8 15/21] printk: sysrq: Clamp console loglevel to valid range Chris Down
2025-12-12 14:10   ` Petr Mladek
2025-11-27 19:44 ` [PATCH v8 16/21] printk: Constrain hardware-addressed console checks to name position Chris Down
2025-11-27 19:44 ` [PATCH v8 17/21] printk: Support setting initial console loglevel via console= on cmdline Chris Down
2025-11-27 19:44 ` [PATCH v8 18/21] printk: Deconstruct kernel.printk into discrete sysctl controls Chris Down
2025-12-12 15:24   ` Petr Mladek
2025-12-15 10:08     ` Joel Granados
2025-12-15 16:09       ` Petr Mladek
2025-11-27 19:44 ` [PATCH v8 19/21] printk: docs: Add comprehensive guidance for per-console loglevels Chris Down
2025-12-12 15:32   ` Petr Mladek
2025-11-27 19:44 ` [PATCH v8 20/21] printk: Deprecate the kernel.printk sysctl interface Chris Down
2025-12-12 15:51   ` Petr Mladek
2025-12-15  9:52     ` Joel Granados
2025-12-15 16:06       ` Petr Mladek
2025-12-17 11:47         ` Joel Granados
2025-12-17 14:33           ` Geert Uytterhoeven
2025-12-17 16:04             ` Steven Rostedt
2025-12-17 20:23               ` Joel Granados
2025-11-27 19:44 ` [PATCH v8 21/21] printk: Purge default_console_loglevel Chris Down
2025-12-12 16:11 ` [PATCH v8 00/21] printk: console: Per-console loglevels Petr Mladek

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=299a3c124f4e0ece36f2a04db2d4d2143e66ebca.1764272407.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®