mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH 4/5] tracing: eprobe-event: Allocate string buffers from heap
Date: Sat, 19 Jul 2025 10:11:56 +0900	[thread overview]
Message-ID: <20250719101156.f9edeca2680e54aa184c439a@kernel.org> (raw)
In-Reply-To: <20250718135549.387b98ab@batman.local.home>

On Fri, 18 Jul 2025 13:55:49 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Fri, 18 Jul 2025 20:34:40 +0900
> "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:
> 
> > From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > 
> > Allocate temporary string buffers for parsing eprobe-events
> > from heap instead of stack.
> > 
> > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > ---
> >  kernel/trace/trace_eprobe.c |   24 ++++++++++++++++++++----
> >  1 file changed, 20 insertions(+), 4 deletions(-)
> > 
> > diff --git a/kernel/trace/trace_eprobe.c b/kernel/trace/trace_eprobe.c
> > index 1e18a8619b40..75d8208cd859 100644
> > --- a/kernel/trace/trace_eprobe.c
> > +++ b/kernel/trace/trace_eprobe.c
> > @@ -9,6 +9,7 @@
> >   * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov tz.stoyanov@gmail.com>
> >   *
> >   */
> > +#include <linux/cleanup.h>
> >  #include <linux/module.h>
> >  #include <linux/mutex.h>
> >  #include <linux/ftrace.h>
> > @@ -871,10 +872,10 @@ static int __trace_eprobe_create(int argc, const char *argv[])
> >  	const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
> >  	const char *sys_event = NULL, *sys_name = NULL;
> >  	struct trace_event_call *event_call;
> > +	char *buf1 __free(kfree) = NULL;
> > +	char *buf2 __free(kfree) = NULL;
> > +	char *gbuf __free(kfree) = NULL;
> >  	struct trace_eprobe *ep = NULL;
> > -	char buf1[MAX_EVENT_NAME_LEN];
> > -	char buf2[MAX_EVENT_NAME_LEN];
> > -	char gbuf[MAX_EVENT_NAME_LEN];
> >  	int ret = 0, filter_idx = 0;
> >  	int i, filter_cnt;
> >  
> > @@ -885,6 +886,11 @@ static int __trace_eprobe_create(int argc, const char *argv[])
> >  
> >  	event = strchr(&argv[0][1], ':');
> >  	if (event) {
> > +		gbuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
> > +		if (!gbuf) {
> > +			ret = -ENOMEM;
> > +			goto parse_error;
> > +		}
> >  		event++;
> >  		ret = traceprobe_parse_event_name(&event, &group, gbuf,
> >  						  event - argv[0]);
> > @@ -894,6 +900,12 @@ static int __trace_eprobe_create(int argc, const char *argv[])
> >  
> >  	trace_probe_log_set_index(1);
> >  	sys_event = argv[1];
> > +
> > +	buf2 = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
> > +	if (!buf2) {
> > +		ret = -ENOMEM;
> > +		goto parse_error;
> > +	}
> >  	ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
> >  	if (ret || !sys_event || !sys_name) {
> >  		trace_probe_log_err(0, NO_EVENT_INFO);
> > @@ -901,7 +913,11 @@ static int __trace_eprobe_create(int argc, const char *argv[])
> >  	}
> >  
> >  	if (!event) {
> > -		strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN);
> > +		buf1 = kstrdup(sys_event, GFP_KERNEL);
> > +		if (!buf1) {
> > +			ret = -ENOMEM;
> > +			goto error;
> > +		}
> 
> I kinda hate all these updating of "ret" before jumping to error.

Agreed.

> 
> >  		event = buf1;
> >  	}
> >  
> 
> What about this:

Looks good to me. Note that eventually I will use cleanup.h to
remove gotos from this function as same as other probes too.

Anyway, in this series I will use the below code.

Thank you!

> 
> -- Steve
> 
> diff --git a/kernel/trace/trace_eprobe.c b/kernel/trace/trace_eprobe.c
> index 916555f0de81..48cc1079a1dd 100644
> --- a/kernel/trace/trace_eprobe.c
> +++ b/kernel/trace/trace_eprobe.c
> @@ -9,6 +9,7 @@
>   * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov tz.stoyanov@gmail.com>
>   *
>   */
> +#include <linux/cleanup.h>
>  #include <linux/module.h>
>  #include <linux/mutex.h>
>  #include <linux/ftrace.h>
> @@ -869,10 +870,10 @@ static int __trace_eprobe_create(int argc, const char *argv[])
>  	const char *event = NULL, *group = EPROBE_EVENT_SYSTEM;
>  	const char *sys_event = NULL, *sys_name = NULL;
>  	struct trace_event_call *event_call;
> +	char *buf1 __free(kfree) = NULL;
> +	char *buf2 __free(kfree) = NULL;
> +	char *gbuf __free(kfree) = NULL;
>  	struct trace_eprobe *ep = NULL;
> -	char buf1[MAX_EVENT_NAME_LEN];
> -	char buf2[MAX_EVENT_NAME_LEN];
> -	char gbuf[MAX_EVENT_NAME_LEN];
>  	int ret = 0, filter_idx = 0;
>  	int i, filter_cnt;
>  
> @@ -883,6 +884,9 @@ static int __trace_eprobe_create(int argc, const char *argv[])
>  
>  	event = strchr(&argv[0][1], ':');
>  	if (event) {
> +		gbuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
> +		if (!gbuf)
> +			goto mem_error;
>  		event++;
>  		ret = traceprobe_parse_event_name(&event, &group, gbuf,
>  						  event - argv[0]);
> @@ -892,6 +896,10 @@ static int __trace_eprobe_create(int argc, const char *argv[])
>  
>  	trace_probe_log_set_index(1);
>  	sys_event = argv[1];
> +
> +	buf2 = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
> +	if (!buf2)
> +		goto mem_error;
>  	ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0);
>  	if (ret || !sys_event || !sys_name) {
>  		trace_probe_log_err(0, NO_EVENT_INFO);
> @@ -899,7 +907,9 @@ static int __trace_eprobe_create(int argc, const char *argv[])
>  	}
>  
>  	if (!event) {
> -		strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN);
> +		buf1 = kstrdup(sys_event, GFP_KERNEL);
> +		if (!buf1)
> +			goto mem_error;
>  		event = buf1;
>  	}
>  
> @@ -972,6 +982,9 @@ static int __trace_eprobe_create(int argc, const char *argv[])
>  	trace_probe_log_clear();
>  	return ret;
>  
> +mem_error:
> +	ret = -ENOMEM;
> +	goto error;
>  parse_error:
>  	ret = -EINVAL;
>  error:


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

  reply	other threads:[~2025-07-19  1:12 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
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 [this message]
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=20250719101156.f9edeca2680e54aa184c439a@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --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®