From nobody Mon Dec 1 22:02:18 2025 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 530D830DD3B for ; Thu, 27 Nov 2025 19:44:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764272670; cv=none; b=GhlgkgQWUc0iTvryhuy/jr654/sF6uVPwwc1uR4PT5UX2Xd8HxeNrEmkwFIjaA11D3tCSPLsw7tCt1gJsTuNoQT7q87TAtg+9aLUW/usxDgcsrESoB884O2vs/TH8Wqv4YC4zzgJnyGb6xr5itMgBQk2OjGAs7iIsNHGK8NIbYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764272670; c=relaxed/simple; bh=PQE/TRxbIOeTjq/mGbd3hnlS/gc1wrsExFo43UAhnPM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=qiHI/7dEb5ZkFpI1CYZ1/JB74gqtFaKn5A/mnAe4upZYVz7zZlhQHioSWjORChqf9rvaqkKMgL1/onkuzwk1JOod84qDKQHPn53ngVz2Y9LUx49izz6nXqjOgw2XGO0LN6me3y6/1eZ/O6Bsj0m39mhaVw6S2rHLKYJvTw5OF+o= 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=lB7xUQKU; arc=none smtp.client-ip=209.85.216.42 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="lB7xUQKU" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-3414de5b27eso920670a91.0 for ; Thu, 27 Nov 2025 11:44:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1764272668; x=1764877468; 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=zbcpOZU85ga6/3hEJ9/9dlVjWqHR/+lXH8zQfvkpEhA=; b=lB7xUQKUvOcrHR2MtjEUip47p03J0RCBQQJXgudKKxgjrHVaTmFYtdoK3zwGgGczhL tjMOQhe248jHWGVgPwIzLdUtFL/bnAuHPbtia3sxCaFcxU7NadjMLZEW92bTt5QosyKo bI5HZOKGDp324WFKSXOopBOUI57odWmOWBcZ0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764272668; x=1764877468; 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=zbcpOZU85ga6/3hEJ9/9dlVjWqHR/+lXH8zQfvkpEhA=; b=Vqk2j6tORCH2juVyDDrYMj7SMKtzEvbgw2TtqxcAibHDU3BdEkjPymy9nE18L0jMM2 y7X7Z6EYKq1Vtlr7tRAppElMLyys5vxMjE/gSzmXo44nWKSkZSm66uGvDV1p0Gph+25m iNwmW3DiZtyAf3QwlFVeXPZp0JMsKKecGVaTYOo7YE+nX/yw2hVfQYAbvV3J1x7BBG8A /jTRRpeAi0dnsmkgDtC/bDjsbwQsgSPJOgXgU2m4M2nXa47e6zihw4s7d8joc3T9wEwb bUdfnOYCkMyFCNiccyRU222InNC/LvLp5Gu14VZ0KBB1qsLxQcA9NjJ574Pn2iNcxKaa /6yQ== X-Gm-Message-State: AOJu0Yzo1IUNSjzlpwSAKDZDDmPzgLIhis8L2lGdhLzgNjpKN0itZt72 JJvj0gawhdTmWe/nXMmVRqGeQadjLklSwpPuN+HMooCFhVFzqjQHathzEBB0kNNT1kQ= X-Gm-Gg: ASbGnctR+z6kvGFwIUfsjPuW6js+aPN2kSSC2grv0lk0NE8d1J266g2zUlgR+UnTOvG UfAdOqBekE7yYodi6eQOVRuuxM9qzZAnLgrmPcPr5YEce1q/VXB/BmCvikp8aTxeSKaUITwiMu3 jBxzbOopHZOAMCiLn2Si15Fu383YHuEVFLgKSp+mptlz/2Am171aFtRVmqJWuUEMs+2UqyoKedK t6rSTcE4jvxMpwx/TvraasdY88cs5TMdXqA35vMio52uNrB/WfkN+udOVmHuR8PJO4zFK7WgDH0 I+xVsApLdSEj7HKAxam5O6XDL3BrctyCODbQQsJdIqY1Am5uYeoHUfwcUrdRvKG8gM70PjkLl8e 35pK2xGnBDh8Lno+GG9oqMLHugg3IJnxP6cXuEtQOu2rRWsP7RyER9EPyg7/q3NJzeTP7sqq6Yl ISP0a4Las9 X-Google-Smtp-Source: AGHT+IForXZeWhWXQQp5dCKaVFRIrw2T1JfAVGhwu8FY8vT7qcFABAJOkufjnRcs6JP57kEvT8NFkA== X-Received: by 2002:a17:90a:c2ce:b0:343:6c71:6d31 with SMTP id 98e67ed59e1d1-34733e93813mr23812578a91.11.1764272667746; Thu, 27 Nov 2025 11:44:27 -0800 (PST) Received: from localhost ([116.86.198.140]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3477b74c83esm2581449a91.15.2025.11.27.11.44.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Nov 2025 11:44:27 -0800 (PST) Date: Fri, 28 Nov 2025 03:44:25 +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 18/21] printk: Deconstruct kernel.printk into discrete sysctl controls 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" 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. Reviewed-by: Petr Mladek Tested-by: Petr Mladek Signed-off-by: Chris Down --- Documentation/admin-guide/sysctl/kernel.rst | 17 +++++- include/linux/sysctl.h | 7 +++ kernel/printk/sysctl.c | 63 +++++++++++++++++++++ kernel/sysctl.c | 18 +++--- 4 files changed, 95 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..034739939a61 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,50 @@ 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; + + /* + * Honour the administrator-configured minimum console + * loglevel (third element of kernel.printk). This mirrors + * the syslog() and sysfs control paths so that once the floor + * is raised we do not let this sysctl silently bypass it. + */ + if (minimum_console_loglevel > CONSOLE_LOGLEVEL_MIN && + level < minimum_console_loglevel) + level =3D minimum_console_loglevel; + + 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 +123,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