From nobody Tue Dec 2 02:41:51 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 2C8712DE6ED for ; Tue, 18 Nov 2025 19:07:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763492862; cv=none; b=gvASXf065lFYftbCYgL39tQmow4lF4jQO8f1uxq7YlIp0l3Ct82cG0K8xxJN1CKqT9wl9z6dNuJPysVB8FEQHl2N5EjMrBa2/BEF3O9Vi7loMrZ5efc9pn9PhKFI7x5zBjon2BQbkKjDRKhfYy5e+fuPf1f0AoPWx1F8LSlaydk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763492862; c=relaxed/simple; bh=zMmsDfXzBtA8QccjGa2qJ6o8bc+OkNzS64LTK4x9B4w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kLV145NcB3njLiB1HEAZcYB9BFil3sjgtrDLH4ngfLUMhjpg34tfJx6EbuAziAukzmbOV7Tiu/DoalsZH2pg4RIkvnN1XzENraXRSGwCOaeMnbV20o/L5Itw3KLkLRnVX4I7K9+oQuG0X2ACPmx2mB0swYS2+VaedPWPzMECyFw= 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=qyv5ZrXe; arc=none smtp.client-ip=209.85.210.179 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="qyv5ZrXe" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7bc248dc16aso3543265b3a.0 for ; Tue, 18 Nov 2025 11:07:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1763492859; x=1764097659; 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=zcW4jHl/TIT8YeAjrkHTb4XBZJtoJtM1QESiU4rYiNI=; b=qyv5ZrXeQyG+OdxULZ+nKuXzyEhmppWg0tkrXZAsjyd1zAbZ92yg4Thk0oh+b3Tb2r fsEXiNt0f/ExLgfvnZmGvo659Bn8pUXspTVmnJRFt4BbB7tllPCmj+nYgaazQSwxkOwc irr9QsB2S5+bfZV55Lkcd9+/gaD+uD69k1EPA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763492859; x=1764097659; 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=zcW4jHl/TIT8YeAjrkHTb4XBZJtoJtM1QESiU4rYiNI=; b=Kt/wfjcxoCbpVY1eGQ3/wSIxlkResYP4RxGODxK/SZPBxXHUm2n3kTH4fu0wsdD6Gw /maly/5of7gODeymSbQsJ6gj4CcNw1UC7YP8ncYbpC+ICcyTsZe9eFKwJv6SCOIHaCOe 6ixvxfnC0Hcdx944uHzbThiAuKObT0j4Ejvv11fZyj1Stvv1hahCyu1donm8wGfO8AFv C1sHMMo4FkRpje+k2VrFT3XszzZgbMAJTW3arfhgwgLFr+eKFEJCzUXYnzHZ0uz7L0fu UyyoOHgI3M/BE3Q03kvIrJuckiq5KkaqPLPqx/bGLFefiE9gJRjbxR6UVHuzWfF4BoXK xxpg== X-Gm-Message-State: AOJu0YygLvEcaWKQVbs8sPrEU7UfjP7gGTwNhjUzWyAyuXvsi/3Gx28v E5rbVD7fv11QQvZ/n2VArCye70QmKsD8vQtVDOglOKA4FSVn6UxQDReRuSM+xV4mm7I= X-Gm-Gg: ASbGnctZrH/5Tn5xznzMflYf0tqow6YaGcBEqV9XWwLz830Np7CxoLfcecohfrQG6wq rrQQ3JMDlqFGIUc/yFnMKbNfUifyiDWVy1K0Hnly6Wx3erVLTXi8YPOlNFtt673wKT2nHTDoccq Q6x6ihOpERa78mMYj0JbwLTo6/f7FmKIsiI0oX0QTMj1lhX8MYjUuMn7yPHwndPSC4rK3QXkTvX rcKuGt/nfiBWXIbhMisa/ppAUc7NUiJCD/584wvo3otZ4ih+IAWdr5yDck1fl34FF0vMNQbLJdN PKCslNX0+AXDMrqcZMCbDWCXJlcHC4rEAhy1TccI3mpOTaBRIqiLgDAFlaLZULvo/PuG2+IaDAx s7TFn9xjpy7AQ4gEcj8iwOuvez20NVgZFy3iei56bOyyHHzG4I94snlpy5d+5ESCGvObX0+Vz3C 9tREp4MUEl X-Google-Smtp-Source: AGHT+IGeBxzS9A/VPFSV0f6oOr56wNvWSyEmbbakeZ46lLzBW6N/6YKw1eBAMgubbrW6oR1cMdI/Ow== X-Received: by 2002:a05:6a20:734d:b0:35e:6c3:c8d0 with SMTP id adf61e73a8af0-35e06c3cb56mr14930972637.54.1763492859412; Tue, 18 Nov 2025 11:07:39 -0800 (PST) Received: from localhost ([116.86.198.140]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b92772fa72sm17295597b3a.55.2025.11.18.11.07.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Nov 2025 11:07:38 -0800 (PST) Date: Wed, 19 Nov 2025 03:07:37 +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 v7 09/13] printk: Support setting initial console loglevel via console= on cmdline Message-ID: <77aa59337507e067f3a4ad7e15375893612bcfa3.1763492585.git.chris@chrisdown.name> 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 Signed-off-by: Chris Down Reviewed-by: Petr Mladek Tested-by: Petr Mladek --- .../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 | 108 +++++++++++++++++- 5 files changed, 161 insertions(+), 15 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 5fabcf9367d1..5a7405986934 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 fb54101133d2..a6e1fa924294 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2660,12 +2660,108 @@ static void set_user_specified(struct console_cmdl= ine *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; + + 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 =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; +} + +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; +} + static int __add_preferred_console(const char *name, const short idx, const char *devname, char *options, char *brl_options, bool user_specified) { struct console_cmdline *c; - int i; + int i, ret; =20 if (!name && !devname) return -EINVAL; @@ -2702,6 +2798,13 @@ static int __add_preferred_console(const char *name,= const short idx, strscpy(c->name, name); if (devname) strscpy(c->devname, devname); + + ret =3D find_and_remove_loglevel_option(options); + if (ret >=3D 0) + c->level =3D ret; + else + c->level =3D -1; + c->options =3D options; set_user_specified(c, user_specified); braille_set_options(c, brl_options); @@ -3979,8 +4082,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