mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH 3/5] tracing: kprobe-event: Allocate string buffers from heap
Date: Fri, 18 Jul 2025 13:46:27 -0400	[thread overview]
Message-ID: <20250718134627.5d483ca4@batman.local.home> (raw)
In-Reply-To: <175283846936.343578.3747359008449354291.stgit@devnote2>

On Fri, 18 Jul 2025 20:34:29 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:

> From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> Allocate temporary string buffers for parsing kprobe-events
> from heap instead of stack.
> 
> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> ---
>  kernel/trace/trace_kprobe.c |   39 +++++++++++++++++++++++++--------------
>  1 file changed, 25 insertions(+), 14 deletions(-)
> 
> diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
> index 15d7a381a128..793af6000f16 100644
> --- a/kernel/trace/trace_kprobe.c
> +++ b/kernel/trace/trace_kprobe.c
> @@ -861,20 +861,20 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],
>  	 *  FETCHARG:TYPE : use TYPE instead of unsigned long.
>  	 */
>  	struct trace_kprobe *tk __free(free_trace_kprobe) = NULL;
> +	const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
> +	const char **new_argv __free(kfree) = NULL;
>  	int i, len, new_argc = 0, ret = 0;
> -	bool is_return = false;
>  	char *symbol __free(kfree) = NULL;
> -	char *tmp = NULL;
> -	const char **new_argv __free(kfree) = NULL;
> -	const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
> +	char *ebuf __free(kfree) = NULL;
> +	char *gbuf __free(kfree) = NULL;
> +	char *abuf __free(kfree) = NULL;
> +	char *dbuf __free(kfree) = NULL;
>  	enum probe_print_type ptype;
> +	bool is_return = false;
>  	int maxactive = 0;
> -	long offset = 0;
>  	void *addr = NULL;
> -	char buf[MAX_EVENT_NAME_LEN];
> -	char gbuf[MAX_EVENT_NAME_LEN];
> -	char abuf[MAX_BTF_ARGS_LEN];
> -	char *dbuf __free(kfree) = NULL;
> +	char *tmp = NULL;
> +	long offset = 0;
>  
>  	switch (argv[0][0]) {
>  	case 'r':
> @@ -893,6 +893,8 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],
>  		event++;
>  
>  	if (isdigit(argv[0][1])) {
> +		char *buf __free(kfree) = NULL;

So this gets freed when this if block ends, right?

> +
>  		if (!is_return) {
>  			trace_probe_log_err(1, BAD_MAXACT_TYPE);
>  			return -EINVAL;
> @@ -905,7 +907,7 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],
>  			trace_probe_log_err(1, BAD_MAXACT);
>  			return -EINVAL;
>  		}
> -		memcpy(buf, &argv[0][1], len);
> +		buf = kmemdup(&argv[0][1], len + 1, GFP_KERNEL);
>  		buf[len] = '\0';
>  		ret = kstrtouint(buf, 0, &maxactive);
>  		if (ret || !maxactive) {
> @@ -973,6 +975,9 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],
>  
>  	trace_probe_log_set_index(0);
>  	if (event) {
> +		gbuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
> +		if (!gbuf)
> +			return -ENOMEM;
>  		ret = traceprobe_parse_event_name(&event, &group, gbuf,
>  						  event - argv[0]);

And you can't use the same trick here because
traceprobe_parse_event_name() assigns "group" to gbuf and is used
outside this if block.

I notice there's no comment that states this. At the very minimum,
traceprobe_parse_event_name() should have a kerneldoc comment above its
definition and state this. But that's not an issue with this patch
series. Just an observation. Thus...

Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>

-- Steve


>  		if (ret)
> @@ -981,16 +986,22 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],
>  
>  	if (!event) {
>  		/* Make a new event name */
> +		ebuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
> +		if (!ebuf)
> +			return -ENOMEM;
>  		if (symbol)
> -			snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
> +			snprintf(ebuf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
>  				 is_return ? 'r' : 'p', symbol, offset);
>  		else
> -			snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
> +			snprintf(ebuf, MAX_EVENT_NAME_LEN, "%c_0x%p",
>  				 is_return ? 'r' : 'p', addr);
> -		sanitize_event_name(buf);
> -		event = buf;
> +		sanitize_event_name(ebuf);
> +		event = ebuf;
>  	}
>  
> +	abuf = kmalloc(MAX_BTF_ARGS_LEN, GFP_KERNEL);
> +	if (!abuf)
> +		return -ENOMEM;
>  	argc -= 2; argv += 2;
>  	ctx->funcname = symbol;
>  	new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc,


  reply	other threads:[~2025-07-18 17:46 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-18 11:33 [PATCH 0/5] tracing: probes: Use heap instead of stack for temporary buffers Masami Hiramatsu (Google)
2025-07-18 11:34 ` [PATCH 1/5] tracing: probe: Allocate traceprobe_parse_context from heap Masami Hiramatsu (Google)
2025-07-18 16:58   ` Steven Rostedt
2025-07-19  0:33     ` Masami Hiramatsu
2025-07-18 11:34 ` [PATCH 2/5] tracing: fprobe-event: Allocate string buffers " Masami Hiramatsu (Google)
2025-07-18 17:39   ` Steven Rostedt
2025-07-19  0:57     ` Masami Hiramatsu
2025-07-19  4:35       ` Masami Hiramatsu
2025-07-18 11:34 ` [PATCH 3/5] tracing: kprobe-event: " Masami Hiramatsu (Google)
2025-07-18 17:46   ` Steven Rostedt [this message]
2025-07-19  1:17     ` Masami Hiramatsu
2025-07-18 11:34 ` [PATCH 4/5] tracing: eprobe-event: " Masami Hiramatsu (Google)
2025-07-18 17:55   ` Steven Rostedt
2025-07-19  1:11     ` Masami Hiramatsu
2025-07-18 11:34 ` [PATCH 5/5] tracing: uprobe-event: " Masami Hiramatsu (Google)
2025-07-18 17:58   ` Steven Rostedt
2025-07-19  1:13     ` Masami Hiramatsu

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=20250718134627.5d483ca4@batman.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.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®