Re: [PATCH v8 15/21] printk: sysrq: Clamp console loglevel to valid range

From: Petr Mladek

Date: Fri Dec 12 2025 - 09:10:47 EST


On Fri 2025-11-28 03:44:10, Chris Down wrote:
> The sysrq loglevel handler (keys 0-9) directly assigns the numeric key
> value to console_loglevel without any validation. This creates an
> inconsistency with other interfaces: the new kernel.console_loglevel
> sysctl, the per-console sysfs loglevel interface, and the
> SYSLOG_ACTION_CONSOLE_LEVEL syslog command all clamp or reject values
> outside the valid range.
>
> In particular, sysrq 0 sets console_loglevel to 0 (LOGLEVEL_EMERG),
> which is problematic because:
>
> 1. LOGLEVEL_EMERG (0) is reserved for emergency messages that should
> always reach all consoles. Setting console_loglevel to 0 would mean
> only KERN_EMERG messages print, which is likely never intended.
>
> 2. The per-console loglevel infrastructure explicitly rejects level 0
> for this reason, creating an inconsistency where sysrq can set a
> value that other interfaces refuse.
>
> 3. CONSOLE_LOGLEVEL_MIN is defined as 1, indicating the minimum valid
> console loglevel, yet sysrq ignores this.
>
> Similarly, sysrq 9 sets console_loglevel to 9, which is above
> LOGLEVEL_DEBUG (7) and serves no useful purpose since no messages have
> a level that high.
>
> Use console_clamp_loglevel() to clamp the requested loglevel to the
> valid range [1, 8]. This ensures consistent behaviour across all
> loglevel-setting interfaces.
>
> Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>

Great catch! Feel free to use:

Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>

See a nit below.

> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -65,6 +65,7 @@ static inline const char *printk_skip_headers(const char *buffer)
> int match_devname_and_update_preferred_console(const char *match,
> const char *name,
> const short idx);
> +int console_clamp_loglevel(int level);

It makes the same declaration in kernel/printk/internal.h redundant
and it can be removed there.

> extern int console_printk[];
>

Best Regards,
Petr