mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Feng Tang <feng.tang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Lance Yang <lance.yang@linux.dev>,
	Jonathan Corbet <corbet@lwn.net>,
	linux-kernel@vger.kernel.org, paulmck@kernel.org,
	john.ogness@linutronix.de
Subject: Re: [PATCH V2 3/5] sys_info: add help to translate sys_info string to bitmap
Date: Mon, 23 Jun 2025 16:04:56 +0200	[thread overview]
Message-ID: <aFlfCCFZQJ9IJm-8@pathway.suse.cz> (raw)
In-Reply-To: <20250616010840.38258-4-feng.tang@linux.alibaba.com>

On Mon 2025-06-16 09:08:38, Feng Tang wrote:
> Bitmap definition is hard to remember and decode. Add sysctl interface
> to translate human readable string like "tasks,mem,timer,lock,ftrace,..."
> to bitmap.
> 
> Suggested-by: Petr Mladek <pmladek@suse.com>
> Signed-off-by: Feng Tang <feng.tang@linux.alibaba.com>
> ---
>  include/linux/sys_info.h |  7 +++
>  lib/sys_info.c           | 97 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 104 insertions(+)
> 
> diff --git a/include/linux/sys_info.h b/include/linux/sys_info.h
> index 79bf4a942e5f..0b49863cd414 100644
> --- a/include/linux/sys_info.h
> +++ b/include/linux/sys_info.h
> @@ -16,5 +16,12 @@
>  #define SYS_SHOW_BLOCKED_TASKS		0x00000080
>  
>  extern void sys_show_info(unsigned long info_mask);
> +extern unsigned long sys_info_parse_param(char *str);
>  
> +#ifdef CONFIG_SYSCTL
> +struct ctl_table;
> +extern int sysctl_sys_info_handler(const struct ctl_table *ro_table, int write,
> +					  void *buffer, size_t *lenp,
> +					  loff_t *ppos);
> +#endif
>  #endif	/* _LINUX_SYS_INFO_H */
> diff --git a/lib/sys_info.c b/lib/sys_info.c
> index 90a79b5164c9..9693542435ba 100644
> --- a/lib/sys_info.c
> +++ b/lib/sys_info.c
> @@ -5,6 +5,103 @@
>  #include <linux/console.h>
>  #include <linux/nmi.h>
>  
> +struct sys_info_name {
> +	unsigned long bit;
> +	const char *name;
> +};
> +
> +static const char sys_info_avail[] = "tasks,mem,timer,lock,ftrace,all_bt,blocked_tasks";
> +
> +static const struct sys_info_name  si_names[] = {
> +	{ SYS_SHOW_TASK_INFO,	"tasks" },
> +	{ SYS_SHOW_MEM_INFO,	"mem" },
> +	{ SYS_SHOW_TIMER_INFO,	"timer" },

I see that the naming is a bit inconsistent in using singulars
vs. plurals. I suggest to use plulars when it makes sense.
It means here:

	{ SYS_SHOW_TIMERS_INFO,	"timers" },

> +	{ SYS_SHOW_LOCK_INFO,	"lock" },

and here

	{ SYS_SHOW_LOCKS_INFO,	"locks" },

> +	{ SYS_SHOW_FTRACE_INFO, "ftrace" },
> +	{ SYS_SHOW_ALL_CPU_BT,	"all_bt" },
> +	{ SYS_SHOW_BLOCKED_TASKS, "blocked_tasks" },
> +};

[...]

> +#ifdef CONFIG_SYSCTL
> +int sysctl_sys_info_handler(const struct ctl_table *ro_table, int write,
> +					  void *buffer, size_t *lenp,
> +					  loff_t *ppos)
> +{
> +	char names[sizeof(sys_info_avail) + 1];
> +	struct ctl_table table;
> +	unsigned long *si_bits_global;
> +	int i, ret, len;
> +
> +	si_bits_global = ro_table->data;
> +
> +	if (write) {
> +		unsigned long si_bits;
> +
> +		table = *ro_table;
> +		table.data = names;
> +		table.maxlen = sizeof(names);
> +		ret = proc_dostring(&table, write, buffer, lenp, ppos);
> +		if (ret)
> +			return ret;
> +
> +		si_bits = sys_info_parse_param(names);
> +		/*
> +		 * The access to the global value is not synchronized.
> +		 */

Nit, the comment fits on a single line. I would use:

		/* The access to the global value is not synchronized. */

> +		WRITE_ONCE(*si_bits_global, si_bits);
> +		return 0;
> +	} else {
> +		/* for 'read' operation */
> +		bool first = true;
> +		char *buf;
> +
> +		buf = names;
> +		for (i = 0; i < ARRAY_SIZE(si_names); i++) {
> +			if (*si_bits_global & si_names[i].bit) {
> +
> +				if (first) {
> +					first = false;
> +				} else {
> +					*buf = ',';
> +					buf++;
> +				}
> +
> +				len = strlen(si_names[i].name);
> +				strncpy(buf, si_names[i].name, len);

I am afraid of a buffer overflow when people add new entry into
si_names[] table but they forget to update sys_info_avail[] string.
I would prefer to limit the write to the buffer by the size of the buffer.

Something like (on top of this patch):

--- a/lib/sys_info.c
+++ b/lib/sys_info.c
@@ -50,7 +50,7 @@ int sysctl_sys_info_handler(const struct ctl_table *ro_table, int write,
 	char names[sizeof(sys_info_avail) + 1];
 	struct ctl_table table;
 	unsigned long *si_bits_global;
-	int i, ret, len;
+	int i, ret;
 
 	si_bits_global = ro_table->data;
 
@@ -72,27 +72,18 @@ int sysctl_sys_info_handler(const struct ctl_table *ro_table, int write,
 		return 0;
 	} else {
 		/* for 'read' operation */
-		bool first = true;
-		char *buf;
+		char *delim = "";
+		int len = 0;
 
-		buf = names;
+		names[0] = '\0';
 		for (i = 0; i < ARRAY_SIZE(si_names); i++) {
 			if (*si_bits_global & si_names[i].bit) {
-
-				if (first) {
-					first = false;
-				} else {
-					*buf = ',';
-					buf++;
-				}
-
-				len = strlen(si_names[i].name);
-				strncpy(buf, si_names[i].name, len);
-				buf += len;
+				len += scnprintf(names + len, sizeof(names) - len,
+						 "%s%s", delim, si_names[i].name);
+				delim = ",";
 			}
 
 		}
-		*buf = '\0';
 
 		table = *ro_table;
 		table.data = names;

Best Regards,
Petr


  parent reply	other threads:[~2025-06-23 14:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-16  1:08 [PATCH V2 0/5] generalize panic_print's dump function to be used by other kernel parts Feng Tang
2025-06-16  1:08 ` [PATCH V2 1/5] panic: clean up code for console replay Feng Tang
2025-06-20 14:19   ` Petr Mladek
2025-06-16  1:08 ` [PATCH V2 2/5] panic: generalize panic_print's function to show sys info Feng Tang
2025-06-20 15:21   ` Petr Mladek
2025-06-23  3:07     ` Feng Tang
2025-06-16  1:08 ` [PATCH V2 3/5] sys_info: add help to translate sys_info string to bitmap Feng Tang
2025-06-16  4:25   ` kernel test robot
2025-06-16 15:08     ` Feng Tang
2025-06-23 14:04   ` Petr Mladek [this message]
2025-06-24  1:48     ` Feng Tang
2025-06-16  1:08 ` [PATCH V2 4/5] panic: add 'panic_sys_info=' setup option for sysctl and kernel cmdline Feng Tang
2025-06-23 15:04   ` Petr Mladek
2025-06-24  1:55     ` Feng Tang
2025-06-16  1:08 ` [PATCH V2 5/5] panic: add note that panic_print interface is deprecated Feng Tang
2025-06-16  1:45   ` Lance Yang
2025-06-16  2:39     ` Feng Tang
2025-06-23 15:13     ` Petr Mladek
2025-06-23 15:22   ` Petr Mladek
2025-06-24  1:58     ` Feng Tang
2025-06-25  9:30     ` Feng Tang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aFlfCCFZQJ9IJm-8@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=corbet@lwn.net \
    --cc=feng.tang@linux.alibaba.com \
    --cc=john.ogness@linutronix.de \
    --cc=lance.yang@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®