From nobody Tue Dec 2 02:41:46 2025 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 2550F2DE6ED for ; Tue, 18 Nov 2025 19:07:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763492872; cv=none; b=HokmoDBsVFRTW+1v3D8un0BZZunml6Bn+thN9vUPj2YmYn2k3yUIzB4WBglLAh8kXDsGNVxDkqVHLznEuVcqL1RxmzwYd1FgVseU+RS/iw68Ub8XuOtKRUJckGVaaE0XvcP/G1vSSTIsoymfxyf2DE9KGC6kgGm9oq+FBy7RsUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763492872; c=relaxed/simple; bh=jp5TF+mMssM49N2V0hrjmfoqlHwyjkzqZ5qatUhGias=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mwj6Z+EvLC4qCslaUf8PtBnvK7OABvYukS+s2PhLOMQLw4UMp+AA3BWvTAEvGa8ypmgPGypI9I5XagRvA9PHNtHBGbFs1pSyYJXHuz/mWRlez8CdA4xw/wgDzLYjXjm6nThdlqlSy1gyvQkwmxRZKHWSS+6LuYn+RoF9vDghsV4= 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=eePQN13C; arc=none smtp.client-ip=209.85.210.171 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="eePQN13C" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7b852bb31d9so6596206b3a.0 for ; Tue, 18 Nov 2025 11:07:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1763492869; x=1764097669; 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=OebVyWjLvth45w1zsGJ4pPsdGFSD9/D/2nuZsy8PNDs=; b=eePQN13CQqZKhDry6WXiQ30XCShpq2QbKv2tGD7CVlf/OKB6mXzSx2gBk1lIvmZr4t E72rJvjX/9zvfxOXFciAZYsj7i+FkUdS8SujIttb9r06lf+WAaRlZJwKwiDeTg+biw6R t/UXwsSHkrwGumq5MtzAus3R9/mTmM4eFx8B4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763492869; x=1764097669; 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=OebVyWjLvth45w1zsGJ4pPsdGFSD9/D/2nuZsy8PNDs=; b=PZw+65wH5W8OYOTdBrN+i7wNWQjr88ajpnyC5tW3nW/yz3h7jXc11Xe5S0x0TssWOe mx4eQIHxYwfHiCQRQROnN5YV9AcNHo+cMMAFQBPae5UCcBL8eGmsgnBfGanucziH5/bK krnbnQhLFUnzjgOYEPpjWOPtFizv33+v05y6/xspGWecrUJXnDshtZaKTuRuB0phpJRs 0YCeHM31V4c+g4XMTxw2Za/MjaD+6URVSaxZpKdMJ7jsI2WjLS02mptOw6Ly4l+rwf9K E4fadNH9NwhICVgR4WXWg4l2nTMYJUj2gUbSUG9shYfVPpePxaauJOSI6mJACXxSkfRu W1lQ== X-Gm-Message-State: AOJu0Yyirq7Of3wlD4rLBBUDSW5YHqFvqfRKQKkJaNlk+nv8RhYBbUPy /fIBVV6geqdIyut5JgnEepujqJ9HqGniC1Auv2xcbtjiuq8VAHgTEv4Zf0LlQa/vzBM= X-Gm-Gg: ASbGncvAlhd9F/UA8suuGqo6aiMd91AVNqaL3+1nMTIAUUsLrTKFZ/UApzQ0ZykvshW R/HpgNHh6e+8Udj/okf6S1NtfbdbdHjoba/8ICnPctuTOkSAdzq+bHU3y8Cl1AoK6na2bWBBt90 rptSV33290043BpSutRFPukwE58pUMnQEpSp31pR4iPziUrLG77SYw8Ikg4Qfmfw+2Cbbqr+UJf mRII0WcdyD3GGUOvUWrmPkQUAehxefDbSjAywnCSDUflG6ajB34AqoOV36OzsmoaxblfDpzAFp6 zHWPMEHErplskrAzGs8S+TOwAnyjdmlDqhyg/vBu5C2H3smK1mpR2IAmvFv3zxv3Z636K3yBFEf h65xhvlofjvmLWP/jOSRqMUAsgeUuIPAbJVENI8bWBnVO5/ZIkNqedONcqmav+CUAdp8953aG73 WkeMMceYbyNWarhxs1xkuVAkpLELqZmw== X-Google-Smtp-Source: AGHT+IH7jwOFUNrZfph0q4lJ6LbmxlBZ7N+lbrnGmia6R1n2lXif5uLlit7ShIsTGbhUQ6w403PZLw== X-Received: by 2002:a05:6a00:3d42:b0:7ab:fc99:d856 with SMTP id d2e1a72fcca58-7ba3b4aaf8emr20206582b3a.18.1763492869405; Tue, 18 Nov 2025 11:07:49 -0800 (PST) Received: from localhost ([116.86.198.140]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b927150b13sm17153904b3a.46.2025.11.18.11.07.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Nov 2025 11:07:48 -0800 (PST) Date: Wed, 19 Nov 2025 03:07:46 +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 10/13] printk: Add sysctl interface to set global loglevels Message-ID: <95295969cda8464d95339b2a7ecaefe1d865f8d8.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" Introduce two new sysctl interfaces for configuring global loglevels: - kernel.console_loglevel: Sets the global console loglevel, determining the minimum priority of messages printed to consoles. Messages with a loglevel lower than this value will be printed. - kernel.default_message_loglevel: Sets the default loglevel for messages that do not specify an explicit loglevel. The kernel.printk sysctl was previously used to set multiple loglevel parameters simultaneously, but it was confusing and lacked proper validation. By introducing these dedicated sysctl interfaces, we provide a clearer and more granular way to configure the loglevels. Signed-off-by: Petr Mladek Signed-off-by: Chris Down Reviewed-by: Petr Mladek Tested-by: Petr Mladek --- Documentation/admin-guide/sysctl/kernel.rst | 17 ++++++- include/linux/sysctl.h | 7 +++ kernel/printk/sysctl.c | 52 +++++++++++++++++++++ kernel/sysctl.c | 18 +++---- 4 files changed, 84 insertions(+), 10 deletions(-) diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/ad= min-guide/sysctl/kernel.rst index f3ee807b5d8b..043d5f663b7d 100644 --- a/Documentation/admin-guide/sysctl/kernel.rst +++ b/Documentation/admin-guide/sysctl/kernel.rst @@ -1079,6 +1079,20 @@ otherwise the 'doze' mode will be used. =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 +Some of these settings may be overridden per-console, see +Documentation/admin-guide/per-console-loglevel.rst. See ``man 2 syslog`` f= or +more information on the different loglevels. + +console_loglevel +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Messages with a higher priority than this will be printed to consoles. + +default_message_loglevel +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Messages without an explicit priority will be printed with this priority. + printk =3D=3D=3D=3D=3D=3D =20 @@ -1087,8 +1101,7 @@ The four values in printk denote: ``console_loglevel`= `, ``default_console_loglevel`` respectively. =20 These values influence printk() behavior when printing or -logging error messages. See '``man 2 syslog``' for more info on -the different loglevels. +logging error messages. =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D console_loglevel messages with a higher priority than diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h index 92e9146b1104..c2f72d172f64 100644 --- a/include/linux/sysctl.h +++ b/include/linux/sysctl.h @@ -235,6 +235,13 @@ extern struct ctl_table_header *register_sysctl_mount_= point(const char *path); =20 void do_sysctl_args(void); bool sysctl_is_alias(char *param); +int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp, int *valp, + int write, void *data); +int do_proc_dointvec(const struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos, + int (*conv)(bool *negp, unsigned long *lvalp, int *valp, + int write, void *data), + void *data); int do_proc_douintvec(const struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos, int (*conv)(unsigned long *lvalp, diff --git a/kernel/printk/sysctl.c b/kernel/printk/sysctl.c index da77f3f5c1fe..feec6cf939af 100644 --- a/kernel/printk/sysctl.c +++ b/kernel/printk/sysctl.c @@ -11,6 +11,9 @@ =20 static const int ten_thousand =3D 10000; =20 +static int min_msg_loglevel =3D LOGLEVEL_EMERG; +static int max_msg_loglevel =3D LOGLEVEL_DEBUG; + static int proc_dointvec_minmax_sysadmin(const struct ctl_table *table, in= t write, void *buffer, size_t *lenp, loff_t *ppos) { @@ -20,6 +23,39 @@ static int proc_dointvec_minmax_sysadmin(const struct ct= l_table *table, int writ return proc_dointvec_minmax(table, write, buffer, lenp, ppos); } =20 +static int do_proc_dointvec_console_loglevel(bool *negp, unsigned long *lv= alp, + int *valp, + int write, void *data) +{ + int level, ret; + + /* + * If writing, first do so via a temporary local int so we can + * bounds-check it before touching *valp. + */ + int *intp =3D write ? &level : valp; + + ret =3D do_proc_dointvec_conv(negp, lvalp, intp, write, data); + if (ret) + return ret; + + if (write) { + if (level !=3D console_clamp_loglevel(level)) + return -ERANGE; + WRITE_ONCE(*valp, level); + } + + return 0; +} + +static int proc_dointvec_console_loglevel(const struct ctl_table *table, + int write, void *buffer, size_t *lenp, + loff_t *ppos) +{ + return do_proc_dointvec(table, write, buffer, lenp, ppos, + do_proc_dointvec_console_loglevel, NULL); +} + static const struct ctl_table printk_sysctls[] =3D { { .procname =3D "printk", @@ -76,6 +112,22 @@ static const struct ctl_table printk_sysctls[] =3D { .extra1 =3D SYSCTL_ZERO, .extra2 =3D SYSCTL_TWO, }, + { + .procname =3D "console_loglevel", + .data =3D &console_loglevel, + .maxlen =3D sizeof(int), + .mode =3D 0644, + .proc_handler =3D proc_dointvec_console_loglevel, + }, + { + .procname =3D "default_message_loglevel", + .data =3D &default_message_loglevel, + .maxlen =3D sizeof(int), + .mode =3D 0644, + .proc_handler =3D proc_dointvec_minmax, + .extra1 =3D &min_msg_loglevel, + .extra2 =3D &max_msg_loglevel, + }, }; =20 void __init printk_sysctl_init(void) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index cb6196e3fa99..3ed010b8f6b3 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -354,9 +354,9 @@ static void proc_put_char(void **buf, size_t *size, cha= r c) } } =20 -static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp, - int *valp, - int write, void *data) +int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp, + int *valp, + int write, void *data) { if (write) { if (*negp) { @@ -380,6 +380,7 @@ static int do_proc_dointvec_conv(bool *negp, unsigned l= ong *lvalp, } return 0; } +EXPORT_SYMBOL(do_proc_dointvec_conv); =20 static int do_proc_douintvec_conv(unsigned long *lvalp, unsigned int *valp, @@ -471,15 +472,16 @@ static int __do_proc_dointvec(void *tbl_data, const s= truct ctl_table *table, return err; } =20 -static int do_proc_dointvec(const struct ctl_table *table, int write, - void *buffer, size_t *lenp, loff_t *ppos, - int (*conv)(bool *negp, unsigned long *lvalp, int *valp, - int write, void *data), - void *data) +int do_proc_dointvec(const struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos, + int (*conv)(bool *negp, unsigned long *lvalp, int *valp, + int write, void *data), + void *data) { return __do_proc_dointvec(table->data, table, write, buffer, lenp, ppos, conv, data); } +EXPORT_SYMBOL(do_proc_dointvec); =20 static int do_proc_douintvec_w(unsigned int *tbl_data, const struct ctl_table *table, --=20 2.51.2