From nobody Mon Dec 1 22:03:19 2025 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79DC430C610 for ; Thu, 27 Nov 2025 19:44:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764272666; cv=none; b=t4C4dkqcMTV7Y9eJQfR6PuJDrj+G+IEVcjA6d3suFzSJNXVOY45Rbj7mGJRzU8T/TUMYagvkRS/9+xht1fFoPdlWGK26uRiSfrEp74jy2Yb5RCYn1IjEZ9xys/Hg4SsJ8irtKBUXLX+9OTzeda8HajqZGJB2WszvQb++5qRQRsg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764272666; c=relaxed/simple; bh=z/e8+bDyKfAwrtie0875Kk7T+MTSTii290y1QPvE3A0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Noj2uv+X49B3IaEVlODxrfrFKqLaw9DeS53mXRUEhGcY/jNfsiU4su81yyz/9uz4r2OmOtsSMGBrM3tOrLsfZg3aN7GVINaGy/iRasa6j8/NjOB9bylqxTVJqHNXPQj5O1KHh/6ZZWkUm+yl+SYeOSEZ8SELTqe+vMgvwAXGQMY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=VXEaoy4U; arc=none smtp.client-ip=209.85.210.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="VXEaoy4U" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7bc0cd6a13aso725245b3a.0 for ; Thu, 27 Nov 2025 11:44:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1764272663; x=1764877463; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=CTKkhOMViq3+EF9TRRjB04NgDV2gqhW7rqGe647M9ZQ=; b=VXEaoy4URjdyA9b6UR+4CQ+QwxPH7RU2jzg/aVoJ8Qca4p6g1IM+NZaVNM8EQZfez7 H3XLFYCfyLm+WQyRqjeFTOqH6yWKFpxCRNyXfQJyDxwCLPu22LiPNiPcNfpon4qx8bXW t8AOiK/s4mjmF0wd71Hohge5T5SAuUkAqqVIY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764272663; x=1764877463; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=CTKkhOMViq3+EF9TRRjB04NgDV2gqhW7rqGe647M9ZQ=; b=QpqXnXllcxmvwgSqTd4FzGE5t04vMCCsguVIJl6yqRZfxpyWqfSj3wRLOn4QCjnlxQ f+rg00QLmtEN4JCyNO6kiFxLFqC3rwT/JQ5a8nazJJvKPttR93iXvK5MNGx6utEnixfD R0djGuVMFs2wVwAZ69yF6pSdrlSJGqCIHqSxsLCA8NEQATyFjNeGhpIsTjXaMK0cWDrH HGpBAI5KfHLYFlehweu28wD9sucCPF41Dp4xyW2XoCdwhsoI+7JA/UH9N3iSnWf7/9xZ 5XgaDamiACqdcUKgRgkOCOju9NYTsxZIIH9D8Ffmn1hjCSR1eZD5/5AjDZkHeTnOQomq tt+g== X-Gm-Message-State: AOJu0YzBGCMB9sot/823cLuzpwKb7I1ARVhYSLOSTxdiFDxEK5RZqK/W Yiy8T0Wwv595U8vVIigZQ2gfr5Gv+hUxKoj3wUwAjkjH1mjGzjVmG+eoDYTZKLGgH/w= X-Gm-Gg: ASbGnctWpz2GX0xbKEB1AllzHNHD5jY0VQt+V3qMa3ufSIVtlKmGi8stnqmM2qMUYue Cyev77EzS5fMR092mbP2a/bvLKtgmNVPr6EJiT8iX/NlCKqzct/0Vk+oJbaULRvI2wIgKCUG0ZZ mWq9nhOMzoII1Vfc8hqi2xR0+6tM3OhLrHcHj85qU8xwQF/nryfICG8Xr/nWcBUcWvj+xUtbSKb iOEHefh/XiOrH9lxLZBRwi2ptWEi8gOjrzjUrDtT03IDvfhSGNd85paRzkQYVG1OTn5bdrmEMm/ XiVNH/IbJrVQqqNJpL9AFXihxkpAaR8kh0wCh5FB3GjfklgZ7Hw90qnSFUfEjovA56//OPhdxT/ YO5jIF9H6mfMfGXjcwgcf5gC3uzYnKpkBlLODs8zI6tk+rMn6tIhewrlfUN7tNzACmcbm77Xqqz AzMsuOMeYy X-Google-Smtp-Source: AGHT+IHNaqA7bszyeXJhPE8QP0fmNS1SqIldUeyBtcvPNQmipeWQE4bfORXyoM0rdmn6kt/4UhLi6g== X-Received: by 2002:a05:6a00:2d8b:b0:78a:f6be:74f2 with SMTP id d2e1a72fcca58-7c56801b7e0mr25021677b3a.5.1764272663454; Thu, 27 Nov 2025 11:44:23 -0800 (PST) Received: from localhost ([116.86.198.140]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7d150b684e2sm2728957b3a.11.2025.11.27.11.44.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Nov 2025 11:44:23 -0800 (PST) Date: Fri, 28 Nov 2025 03:44:21 +0800 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v8 17/21] printk: Support setting initial console loglevel via console= on cmdline Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.15 (2b349c5e) (2025-10-02) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the console=3D kernel command line parameter to support specifying per-console loglevels at boot time. This is achieved by introducing a new loglevel option that can be passed as a loglevel option within a console=3D stanza. For example, this is an example of how one might configure netconsole devices to print messages with a higher priority than loglevel 3 (KERN_ERR) at startup: console=3Dnetcon0,loglevel:3 Reviewed-by: Petr Mladek Tested-by: Petr Mladek Signed-off-by: Chris Down --- .../admin-guide/kernel-parameters.txt | 24 ++- Documentation/admin-guide/serial-console.rst | 37 +++- Documentation/networking/netconsole.rst | 6 +- kernel/printk/console_cmdline.h | 1 + kernel/printk/printk.c | 168 +++++++++++++++++- 5 files changed, 219 insertions(+), 17 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index 7b62a99489b8..dec9e29ba8d2 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -819,13 +819,18 @@ ttyS[,options] ttyUSB0[,options] Use the specified serial port. The options are of - the form "bbbbpnf", where "bbbb" is the baud rate, - "p" is parity ("n", "o", or "e"), "n" is number of - bits, and "f" is flow control ("r" for RTS or - omit it). Default is "9600n8". + the form "bbbbpnf,extra", where "bbbb" is the baud + rate, "p" is parity ("n", "o", or "e"), "n" is + number of bits, and "f" is flow control ("r" for RTS + or omit it). Default is "9600n8". =20 - See Documentation/admin-guide/serial-console.rst for more - information. See + At present the only extra option is "loglevel" to + set the per-console loglevel. For example: + + console=3DttyS0,9600n8,loglevel:3 + + See Documentation/admin-guide/serial-console.rst for + more information. See Documentation/networking/netconsole.rst for an alternative. =20 @@ -3411,8 +3416,11 @@ loglevel=3D [KNL,EARLY] All Kernel Messages with a loglevel smaller than the console loglevel will be printed to the console. It can - also be changed with klogd or other programs. The - loglevels are defined as follows: + also be changed with klogd or other programs. Note that + this can be overridden per-console, see + Documentation/admin-guide/per-console-loglevel.rst. + + The loglevels are defined as follows: =20 0 (KERN_EMERG) system is unusable 1 (KERN_ALERT) action must be taken immediately diff --git a/Documentation/admin-guide/serial-console.rst b/Documentation/a= dmin-guide/serial-console.rst index 1609e7479249..b8163e6eb95a 100644 --- a/Documentation/admin-guide/serial-console.rst +++ b/Documentation/admin-guide/serial-console.rst @@ -32,6 +32,33 @@ The format of this option is:: and F is flow control ('r' for RTS). Default is 9600n8. The maximum baudrate is 115200. =20 + One can also specify the per-console loglevel for this + console by providing a loglevel parameter, for example + "loglevel:4" to set this console's loglevel to 4. The + value provided can be from 1 (LOGLEVEL_ALERT) to 8 + (LOGLEVEL_DEBUG + 1), and messages below that will be + emitted onto the console as they become available. + +The available loglevels are defined thus: + ++---+--------------+-----------------------------------+ +| 0 | KERN_EMERG | system is unusable | ++---+--------------+-----------------------------------+ +| 1 | KERN_ALERT | action must be taken immediately | ++---+--------------+-----------------------------------+ +| 2 | KERN_CRIT | critical conditions | ++---+--------------+-----------------------------------+ +| 3 | KERN_ERR | error conditions | ++---+--------------+-----------------------------------+ +| 4 | KERN_WARNING | warning conditions | ++---+--------------+-----------------------------------+ +| 5 | KERN_NOTICE | normal but significant condition | ++---+--------------+-----------------------------------+ +| 6 | KERN_INFO | informational | ++---+--------------+-----------------------------------+ +| 7 | KERN_DEBUG | debug-level messages | ++---+--------------+-----------------------------------+ + You can specify multiple console=3D options on the kernel command line. =20 The behavior is well defined when each device type is mentioned only once. @@ -39,11 +66,14 @@ In this case, the output will appear on all requested c= onsoles. And the last device will be used when you open ``/dev/console``. So, for example:: =20 - console=3DttyS1,9600 console=3Dtty0 + console=3DttyS1,9600,loglevel:5 console=3Dtty0 =20 defines that opening ``/dev/console`` will get you the current foreground -virtual console, and kernel messages will appear on both the VGA -console and the 2nd serial port (ttyS1 or COM2) at 9600 baud. +virtual console, and kernel messages will appear on both the VGA console a= nd +the 2nd serial port (ttyS1 or COM2) at 9600 baud. The optional loglevel "5" +indicates that this console will emit messages more serious than +LOGLEVEL_NOTICE (that is, LOGLEVEL_WARNING and below, since more serious +messages have lower ordering). =20 The behavior is more complicated when the same device type is defined more times. In this case, there are the following two rules: @@ -145,3 +175,4 @@ Replace the sample values as needed. the integration of these patches into m68k, ppc and alpha. =20 Miquel van Smoorenburg , 11-Jun-2000 +Chris Down , 18-Nov-2025 diff --git a/Documentation/networking/netconsole.rst b/Documentation/networ= king/netconsole.rst index 5ff12e88e5b8..1bcb56151b14 100644 --- a/Documentation/networking/netconsole.rst +++ b/Documentation/networking/netconsole.rst @@ -78,7 +78,11 @@ Built-in netconsole starts immediately after the TCP sta= ck is initialized and attempts to bring up the supplied dev at the supplied address. =20 -You can also set a loglevel at runtime:: +You can also set a loglevel at boot time on the kernel command line:: + + console=3Dnetcon0,loglevel:2 + +This can also be changed at runtime:: =20 $ ls -l /sys/class/console/netcon0/ total 0 diff --git a/kernel/printk/console_cmdline.h b/kernel/printk/console_cmdlin= e.h index 0ab573b6d4dc..cb3b99d31d80 100644 --- a/kernel/printk/console_cmdline.h +++ b/kernel/printk/console_cmdline.h @@ -7,6 +7,7 @@ struct console_cmdline char name[16]; /* Name of the driver */ int index; /* Minor dev. to use */ char devname[32]; /* DEVNAME:0.0 style device name */ + int level; /* Log level to use */ bool user_specified; /* Specified by command line vs. platform */ char *options; /* Options for the driver */ #ifdef CONFIG_A11Y_BRAILLE_CONSOLE diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 32ea9b69f515..cb796af0b867 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -2735,6 +2736,160 @@ static void set_user_specified(struct console_cmdli= ne *c, bool user_specified) console_set_on_cmdline =3D 1; } =20 +/** + * find_and_remove_console_option - Find and remove a named option from co= nsole options string + * @options: The console options string (will be modified in-place) + * @key: The option name to find (e.g., "loglevel") + * @val_buf: Buffer to store the option value (if present) + * @val_buf_size: Size of @val_buf + * + * This function searches for a named option in a comma-separated options = string + * (e.g., "9600n8,loglevel:3,other:value"). If found, it extracts the value + * (the part after ':') and removes the entire option from the string. + * + * The function modifies @options in-place by: + * 1. Temporarily null-terminating option names and values during parsing + * 2. Restoring separators if the option isn't found + * 3. Removing the found option by shifting the remaining string + * + * Return: true if the option was found and removed, false otherwise + */ +static bool find_and_remove_console_option(char *options, const char *key, + char *val_buf, size_t val_buf_size) +{ + bool found =3D false, first =3D true; + char *option, *next =3D options; + + if (!val_buf_size) + return false; + + while ((option =3D strsep(&next, ","))) { + char *value; + + value =3D strchr(option, ':'); + if (value) + *(value++) =3D '\0'; + + if (strcmp(option, key) =3D=3D 0) { + found =3D true; + if (value) { + if (strlen(value) >=3D val_buf_size) { + pr_warn("Can't copy console option value for %s:%s: not enough space = (%zu)\n", + option, value, val_buf_size); + found =3D false; + } else { + strscpy(val_buf, value, val_buf_size); + } + } else { + val_buf[0] =3D '\0'; + } + } + + if (found) + break; + + if (next) + *(next - 1) =3D ','; + if (value) + *(value - 1) =3D ':'; + + first =3D false; + } + + if (found) { + if (next) + memmove(option, next, strlen(next) + 1); + else if (first) + *option =3D '\0'; + else + *--option =3D '\0'; + } + + return found; +} + +/** + * find_and_remove_loglevel_option - Extract and remove loglevel option fr= om string + * + * @options: The console options string (modified in-place) + * + * Searches for a "loglevel:N" option in the options string, validates the + * value, and removes the option from the string if found. + * + * Return: The parsed loglevel value (> 0) on success, or a negative error= code: + * -ENOENT if no loglevel option was found + * -EINVAL if the loglevel value could not be parsed + * -ERANGE if the loglevel value is out of valid range + */ +static int find_and_remove_loglevel_option(char *options) +{ + char val[16]; + int loglevel; + + if (!find_and_remove_console_option(options, "loglevel", val, + sizeof(val))) + return -ENOENT; + + if (kstrtoint(val, 10, &loglevel)) { + pr_warn("Invalid console loglevel, ignoring: %s\n", val); + return -EINVAL; + } + + /* Reject level 0 (KERN_EMERG) - per-console loglevel must be > 0 */ + if (loglevel =3D=3D 0) { + pr_warn("Per-console loglevel 0 (KERN_EMERG) is not allowed, ignoring\n"= ); + return -ERANGE; + } + + if (console_clamp_loglevel(loglevel) !=3D loglevel) { + pr_warn("Per-console loglevel out of range, ignoring: %d\n", loglevel); + return -ERANGE; + } + + return loglevel; +} + +/** + * console_cmdline_set_options - Parse and set options for a console_cmdli= ne entry + * + * @c: The console_cmdline entry to configure + * @options: The options string from console=3D parameter (may be NULL) + * + * Parses the options string for known named options (currently "loglevel"= ), + * removes them from the string, and stores the remaining options in the + * console_cmdline entry. The loglevel option, if present and valid, is st= ored + * in c->level. + * + * Return: 0 on success, -ENOMEM if memory allocation fails + */ +static int console_cmdline_set_options(struct console_cmdline *c, char *op= tions) +{ + char *options_buf =3D NULL; + int level =3D -ENOENT; + + if (options) { + options_buf =3D kstrdup(options, GFP_KERNEL); + if (!options_buf) + return -ENOMEM; + + level =3D find_and_remove_loglevel_option(options_buf); + if (options_buf && !*options_buf) { + kfree(options_buf); + options_buf =3D NULL; + } + } + + kfree(c->options); + c->options =3D options_buf; + + if (level >=3D 0) + c->level =3D level; + else if (!options || level =3D=3D -ENOENT) + c->level =3D -1; + + return 0; +} + static int __add_preferred_console(const char *name, const short idx, const char *devname, char *options, char *brl_options, bool user_specified) @@ -2766,8 +2921,8 @@ static int __add_preferred_console(const char *name, = const short idx, if (!brl_options) preferred_console =3D i; =20 - if (options) - c->options =3D options; + if (console_cmdline_set_options(c, options)) + return -ENOMEM; =20 set_user_specified(c, user_specified); braille_set_options(c, brl_options); @@ -2782,7 +2937,11 @@ static int __add_preferred_console(const char *name,= const short idx, strscpy(c->name, name); if (devname) strscpy(c->devname, devname); - c->options =3D options; + + c->level =3D -1; + + if (console_cmdline_set_options(c, options)) + return -ENOMEM; set_user_specified(c, user_specified); braille_set_options(c, brl_options); =20 @@ -4059,8 +4218,7 @@ static int try_enable_preferred_console(struct consol= e *newcon, if (newcon->index < 0) newcon->index =3D c->index; =20 - /* TODO: will be configurable in a later patch */ - newcon->level =3D LOGLEVEL_DEFAULT; + newcon->level =3D c->level; =20 if (_braille_register_console(newcon, c)) return 0; --=20 2.51.2