[PATCH v7 04/13] printk: Ignore per-console loglevel in sysrq

Chris Down posted 13 patches 1 week, 6 days ago
There is a newer version of this series
[PATCH v7 04/13] printk: Ignore per-console loglevel in sysrq
Posted by Chris Down 1 week, 6 days ago
When the global console loglevel is changed via sysrq, clear any
per-console loglevels that may have been set. This ensures that the
sysrq loglevel change takes effect immediately across all consoles.

Warn once if any per-console loglevels were being overridden.

Signed-off-by: Chris Down <chris@chrisdown.name>
---
 drivers/tty/sysrq.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
index 97f8a9a52285..1763eaefd581 100644
--- a/drivers/tty/sysrq.c
+++ b/drivers/tty/sysrq.c
@@ -51,6 +51,7 @@
 #include <linux/syscalls.h>
 #include <linux/of.h>
 #include <linux/rcupdate.h>
+#include <linux/console.h>
 
 #include <asm/ptrace.h>
 #include <asm/irq_regs.h>
@@ -101,10 +102,25 @@ __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
 static void sysrq_handle_loglevel(u8 key)
 {
 	u8 loglevel = key - '0';
+	int cookie;
+	struct console *con;
 
 	console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
 	pr_info("Loglevel set to %u\n", loglevel);
 	console_loglevel = loglevel;
+
+	/*
+	 * When changing loglevel via sysrq, override all per-console loglevels
+	 * to ensure the new global loglevel takes effect immediately and all
+	 * consoles see the messages.
+	 */
+	cookie = console_srcu_read_lock();
+	for_each_console_srcu(con) {
+		if (has_per_console_loglevel(con))
+			pr_warn_once("Overriding per-console loglevel from sysrq\n");
+		WRITE_ONCE(con->level, LOGLEVEL_DEFAULT);
+	}
+	console_srcu_read_unlock(cookie);
 }
 static const struct sysrq_key_op sysrq_loglevel_op = {
 	.handler	= sysrq_handle_loglevel,
-- 
2.51.2
Re: [PATCH v7 04/13] printk: Ignore per-console loglevel in sysrq
Posted by Petr Mladek 1 week, 5 days ago
On Wed 2025-11-19 03:07:11, Chris Down wrote:
> When the global console loglevel is changed via sysrq, clear any
> per-console loglevels that may have been set. This ensures that the
> sysrq loglevel change takes effect immediately across all consoles.
> 
> Warn once if any per-console loglevels were being overridden.
> 
> Signed-off-by: Chris Down <chris@chrisdown.name>

Looks good to me:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr