tracing: probes: Use heap instead of stack for temporary buffers
Masami Hiramatsu (Google)
maintainerv3 cover tracing: probes: Use heap instead of stack for temporary buffers
Hi,
This is the 3rd version of the series of cleanup patches which allocate temporary buffers and objects on heap (slab) instead of the stack. The previous version is here;
https://lore.kernel.org/all/175299249728.418723.17799706394466693180.stgit@devnote2/
This version splits the sorting #include part as the first patch and adds Steve's reviewed-by for [5/7].
Thank you,
---
Masami Hiramatsu (Google) (7):
tracing: probes: Sort #include alphabetically
tracing: probe: Allocate traceprobe_parse_context from heap
tracing: fprobe-event: Allocate string buffers from heap
tracing: kprobe-event: Allocate string buffers from heap
tracing: eprobe-event: Allocate string buffers from heap
tracing: uprobe-event: Allocate string buffers from heap
tracing: probes: Add a kerneldoc for traceprobe_parse_event_name()
kernel/trace/trace_eprobe.c | 40 +++++++++++++++++++++--------- kernel/trace/trace_fprobe.c | 55 +++++++++++++++++++++++++++-------------- kernel/trace/trace_kprobe.c | 57 +++++++++++++++++++++++++++---------------- kernel/trace/trace_probe.c | 19 +++++++++++++- kernel/trace/trace_probe.h | 26 ++++++++++++++------ kernel/trace/trace_uprobe.c | 53 +++++++++++++++++++++++++--------------- 6 files changed, 168 insertions(+), 82 deletions(-)
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v3 1/7 tracing: probes: Sort #include alphabetically
From: Masami Hiramatsu (Google) <[email protected]>
Sort the #include directives in trace_probe* files alphabetically for easier maintenance and avoid double includes.
This also groups headers as linux-generic, asm-generic, and local headers.
---
Changes in v3:
- Newly added.
--- kernel/trace/trace_eprobe.c | 4 ++-- kernel/trace/trace_fprobe.c | 3 ++- kernel/trace/trace_kprobe.c | 8 ++++---- kernel/trace/trace_probe.c | 2 +- kernel/trace/trace_probe.h | 17 +++++++++-------- kernel/trace/trace_uprobe.c | 12 ++++++------ 6 files changed, 24 insertions(+), 22 deletions(-)
kernel/trace/trace_eprobe.c +2 -2
@@ -9,14 +9,14 @@* Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov [email protected]> * */ +#include <linux/ftrace.h> #include <linux/module.h> #include <linux/mutex.h> -#include <linux/ftrace.h> #include "trace_dynevent.h" #include "trace_probe.h" -#include "trace_probe_tmpl.h" #include "trace_probe_kernel.h" +#include "trace_probe_tmpl.h" #define EPROBE_EVENT_SYSTEM "eprobes"
kernel/trace/trace_fprobe.c +2 -1
@@ -4,7 +4,6 @@* Copyright (C) 2022 Google LLC. */ #define pr_fmt (fmt) "trace_fprobe: " fmt -#include <asm/ptrace.h> #include <linux/fprobe.h> #include <linux/list.h>
@@ -15,6 +14,8 @@#include <linux/tracepoint.h> #include <linux/uaccess.h> +#include <asm/ptrace.h> +#include "trace_dynevent.h" #include "trace_probe.h" #include "trace_probe_kernel.h"
kernel/trace/trace_kprobe.c +4 -4
@@ -9,19 +9,19 @@#include <linux/bpf-cgroup.h> #include <linux/cleanup.h> -#include <linux/security.h> +#include <linux/error-injection.h> #include <linux/module.h> -#include <linux/uaccess.h> #include <linux/rculist.h> -#include <linux/error-injection.h> +#include <linux/security.h> +#include <linux/uaccess.h> #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ #include "trace_dynevent.h" #include "trace_kprobe_selftest.h" #include "trace_probe.h" -#include "trace_probe_tmpl.h" #include "trace_probe_kernel.h" +#include "trace_probe_tmpl.h" #define KPROBE_EVENT_SYSTEM "kprobes"#define KRETPROBE_MAXACTIVE_MAX 4096
kernel/trace/trace_probe.c +1 -1
@@ -13,8 +13,8 @@#include <linux/bpf.h> #include <linux/fs.h> -#include "trace_btf.h" +#include "trace_btf.h" #include "trace_probe.h" #undef C
kernel/trace/trace_probe.h +9 -8
@@ -10,20 +10,21 @@* Author: Srikar Dronamraju */ +#include <linux/bitops.h> +#include <linux/btf.h> +#include <linux/kprobes.h> +#include <linux/limits.h> +#include <linux/perf_event.h> +#include <linux/ptrace.h> #include <linux/seq_file.h> #include <linux/slab.h> #include <linux/smp.h> -#include <linux/tracefs.h> -#include <linux/types.h> #include <linux/string.h> -#include <linux/ptrace.h> -#include <linux/perf_event.h> -#include <linux/kprobes.h> #include <linux/stringify.h> -#include <linux/limits.h> +#include <linux/tracefs.h> +#include <linux/types.h> #include <linux/uaccess.h> -#include <linux/bitops.h> -#include <linux/btf.h> +#include <asm/bitsperlong.h> #include "trace.h"
kernel/trace/trace_uprobe.c +6 -6
@@ -8,16 +8,16 @@#define pr_fmt (fmt) "trace_uprobe: " fmt#include <linux/bpf-cgroup.h> -#include <linux/security.h> #include <linux/ctype.h> +#include <linux/filter.h> #include <linux/module.h> -#include <linux/uaccess.h> -#include <linux/uprobes.h> #include <linux/namei.h> -#include <linux/string.h> -#include <linux/rculist.h> -#include <linux/filter.h> #include <linux/percpu.h> +#include <linux/rculist.h> +#include <linux/security.h> +#include <linux/string.h> +#include <linux/uaccess.h> +#include <linux/uprobes.h> #include "trace_dynevent.h" #include "trace_probe.h"
Masami Hiramatsu (Google)
maintainerPATCH v3 2/7 tracing: probe: Allocate traceprobe_parse_context from heap
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Instead of allocating traceprobe_parse_context on stack, allocate it dynamically from heap (slab).
This change is likely intended to prevent potential stack overflow issues, which can be a concern in the kernel environment where stack space is limited.
---
Changes in v3:
- Split #include sorting.
Changes in v2:
- Sort #include alphabetically.
- Just NULL check for freeing traceprobe_parse_context.
- Do not change the semantics of traceprobe_parse_context for uprobe event. (alloc/free in loop)
--- kernel/trace/trace_eprobe.c | 14 ++++++++------ kernel/trace/trace_fprobe.c | 13 ++++++++----- kernel/trace/trace_kprobe.c | 10 +++++++--- kernel/trace/trace_probe.h | 9 +++++++++ kernel/trace/trace_uprobe.c | 13 ++++++++----- 5 files changed, 40 insertions(+), 19 deletions(-)
kernel/trace/trace_eprobe.c +8 -6
@@ -797,18 +797,20 @@ find_and_get_event(const char *system, const char *event_name)static int trace_eprobe_tp_update_arg (struct trace_eprobe *ep,const char *argv[],int i) { -struct traceprobe_parse_context ctx = { - .event = ep->event, - .flags =TPARG_FL_KERNEL |TPARG_FL_TEVENT , - }; + struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;int ret; - ret =traceprobe_parse_probe_arg (&ep->tp, i, argv[i], &ctx); + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return -ENOMEM ; + ctx->event = ep->event; + ctx->flags =TPARG_FL_KERNEL |TPARG_FL_TEVENT ; + + ret =traceprobe_parse_probe_arg (&ep->tp, i, argv[i], ctx);/* Handle symbols "@" */ if (!ret) ret =traceprobe_update_arg (&ep->tp.args[i]); -traceprobe_finish_parse (&ctx);return ret; }
kernel/trace/trace_fprobe.c +8 -5
@@ -1384,14 +1384,17 @@ static int trace_fprobe_create_internal(int argc, const char *argv[],static int trace_fprobe_create_cb (int argc,const char *argv[]) { -struct traceprobe_parse_context ctx = { - .flags =TPARG_FL_KERNEL |TPARG_FL_FPROBE , - }; + struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;int ret; + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return -ENOMEM ; + + ctx->flags =TPARG_FL_KERNEL |TPARG_FL_FPROBE , +trace_probe_log_init ("trace_fprobe" , argc, argv); - ret =trace_fprobe_create_internal (argc, argv, &ctx); -traceprobe_finish_parse (&ctx); + ret =trace_fprobe_create_internal (argc, argv, ctx);trace_probe_log_clear ();return ret; }
kernel/trace/trace_kprobe.c +7 -3
@@ -1065,14 +1065,18 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],static int trace_kprobe_create_cb (int argc,const char *argv[]) { -struct traceprobe_parse_context ctx = { .flags =TPARG_FL_KERNEL }; + struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;int ret; + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return -ENOMEM ; + ctx->flags =TPARG_FL_KERNEL ; +trace_probe_log_init ("trace_kprobe" , argc, argv); - ret =trace_kprobe_create_internal (argc, argv, &ctx); + ret =trace_kprobe_create_internal (argc, argv, ctx); -traceprobe_finish_parse (&ctx);trace_probe_log_clear ();return ret; }
kernel/trace/trace_probe.h +9 -0
@@ -12,6 +12,7 @@#include <linux/bitops.h> #include <linux/btf.h> +#include <linux/cleanup.h> #include <linux/kprobes.h> #include <linux/limits.h> #include <linux/perf_event.h>
@@ -439,6 +440,14 @@ extern void traceprobe_free_probe_arg(struct probe_arg *arg);* this MUST be called for clean up the context and return a resource. */ void traceprobe_finish_parse (struct traceprobe_parse_context *ctx); +static inline void traceprobe_free_parse_ctx (struct traceprobe_parse_context *ctx) +{ +traceprobe_finish_parse (ctx); +kfree (ctx); +} + +DEFINE_FREE(traceprobe_parse_context, struct traceprobe_parse_context *, + if (_T) traceprobe_free_parse_ctx(_T))extern int traceprobe_split_symbol_offset (char *symbol,long *offset); int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
kernel/trace/trace_uprobe.c +8 -5
@@ -695,13 +695,16 @@ static int __trace_uprobe_create(int argc, const char **argv)/* parse arguments */ for (i =0 ; i < argc; i++) { -struct traceprobe_parse_context ctx = { - .flags = (is_return ?TPARG_FL_RETURN :0 ) |TPARG_FL_USER , - }; + struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) + =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) { + ret = -ENOMEM ; + goto error; + } + ctx->flags = (is_return ?TPARG_FL_RETURN :0 ) |TPARG_FL_USER ;trace_probe_log_set_index (i +2 ); - ret =traceprobe_parse_probe_arg (&tu->tp, i, argv[i], &ctx); -traceprobe_finish_parse (&ctx); + ret =traceprobe_parse_probe_arg (&tu->tp, i, argv[i], ctx);if (ret) goto error; }
Re: [PATCH v3 2/7] tracing: probe: Allocate traceprobe_parse_context from heap
Quoted 5 lines · On Wed, 23 Jul 2025 07:01:46 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Instead of allocating
traceprobe_parse_contexton stack, allocate it dynamically from heap (slab).
Quoted 7 lines
This change is likely intended to prevent potential stack overflow issues, which can be a concern in the kernel environment where stack space is limited.
-- Steve
Masami Hiramatsu (Google)
maintainerRe: [PATCH v3 2/7] tracing: probe: Allocate traceprobe_parse_context from heap
Quoted 12 lines · On Tue, 22 Jul 2025 18:38:13 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Wed, 23 Jul 2025 07:01:46 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Instead of allocating
traceprobe_parse_contexton stack, allocate it dynamically from heap (slab).This change is likely intended to prevent potential stack overflow issues, which can be a concern in the kernel environment where stack space is limited.
Quoted 6 lines
Quoted 3 lines
Reviewed-by Steven Rostedt (Google) <[email protected]>
Thanks!
Quoted 1 line
-- Steve
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v3 3/7 tracing: fprobe-event: Allocate string buffers from heap
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Allocate temporary string buffers for fprobe-event from heap instead of stack. This fixes the stack frame exceed limit error.
--- kernel/trace/trace_fprobe.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-)
kernel/trace/trace_fprobe.c +26 -13
@@ -1235,18 +1235,18 @@ static int trace_fprobe_create_internal(int argc, const char *argv[],* FETCHARG:TYPE : use TYPE instead of unsigned long. */ struct trace_fprobe *tf __free(free_trace_fprobe) = NULL; - struct module *mod __free(module_put) = NULL; -int i, new_argc =0 , ret =0 ; -bool is_return = false; - char *symbol __free(kfree) = NULL;const char *event =NULL , *group =FPROBE_EVENT_SYSTEM ; + struct module *mod __free(module_put) = NULL; const char **new_argv __free(kfree) = NULL; -char buf[MAX_EVENT_NAME_LEN ]; -char gbuf[MAX_EVENT_NAME_LEN ]; -char sbuf[KSYM_NAME_LEN ]; -char abuf[MAX_BTF_ARGS_LEN ]; + char *symbol __free(kfree) = NULL; + char *ebuf __free(kfree) = NULL; + char *gbuf __free(kfree) = NULL; + char *sbuf __free(kfree) = NULL; + char *abuf __free(kfree) = NULL; char *dbuf __free(kfree) = NULL; +int i, new_argc =0 , ret =0 ;bool is_tracepoint = false; +bool is_return = false;if ((argv[0 ][0 ] !='f' && argv[0 ][0 ] !='t' ) || argc <2 )return -ECANCELED ;
@@ -1274,6 +1274,9 @@ static int trace_fprobe_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 ]);if (ret)
@@ -1281,15 +1284,18 @@ static int trace_fprobe_create_internal(int argc, const char *argv[], }if (!event) { + ebuf =kmalloc (MAX_EVENT_NAME_LEN ,GFP_KERNEL ); +if (!ebuf) +return -ENOMEM ;/* Make a new event name */ if (is_tracepoint) -snprintf (buf,MAX_EVENT_NAME_LEN ,"%s%s" , +snprintf (ebuf,MAX_EVENT_NAME_LEN ,"%s%s" ,isdigit (*symbol) ?"_" :"" , symbol);else -snprintf (buf,MAX_EVENT_NAME_LEN ,"%s__%s" , symbol, +snprintf (ebuf,MAX_EVENT_NAME_LEN ,"%s__%s" , symbol, is_return ?"exit" :"entry" ); -sanitize_event_name (buf); - event = buf; +sanitize_event_name (ebuf); + event = ebuf; }if (is_return)
@@ -1305,13 +1311,20 @@ static int trace_fprobe_create_internal(int argc, const char *argv[], ctx->flags |=TPARG_FL_TPOINT ; mod =NULL ; tpoint =find_tracepoint (symbol, &mod); -if (tpoint) +if (tpoint) { + sbuf =kmalloc (KSYM_NAME_LEN ,GFP_KERNEL ); +if (!sbuf) +return -ENOMEM ; ctx->funcname =kallsyms_lookup ((unsigned long )tpoint->probestub,NULL ,NULL ,NULL , sbuf); + } }if (!ctx->funcname) ctx->funcname = symbol; + abuf =kmalloc (MAX_BTF_ARGS_LEN ,GFP_KERNEL ); +if (!abuf) +return -ENOMEM ; argc -=2 ; argv +=2 ; new_argv =traceprobe_expand_meta_args (argc, argv, &new_argc, abuf,MAX_BTF_ARGS_LEN , ctx);
Re: [PATCH v3 3/7] tracing: fprobe-event: Allocate string buffers from heap
Quoted 8 lines · On Wed, 23 Jul 2025 07:01:55 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Allocate temporary string buffers for fprobe-event from heap instead of stack. This fixes the stack frame exceed limit error.
-- Steve
Masami Hiramatsu (Google)
maintainerPATCH v3 4/7 tracing: kprobe-event: Allocate string buffers from heap
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Allocate temporary string buffers for parsing kprobe-events from heap instead of stack.
--- kernel/trace/trace_kprobe.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-)
kernel/trace/trace_kprobe.c +25 -14
@@ -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; +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 ]);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,
Masami Hiramatsu (Google)
maintainerPATCH v3 5/7 tracing: eprobe-event: Allocate string buffers from heap
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Allocate temporary string buffers for parsing eprobe-events from heap instead of stack.
---
Changes in v3:
- Split #include sorting.
Changes in v2:
- Use new label for returning -ENOMEM.
- Sort #include alphabetically.
--- kernel/trace/trace_eprobe.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
kernel/trace/trace_eprobe.c +18 -4
@@ -9,6 +9,7 @@* Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov [email protected]> * */ +#include <linux/cleanup.h> #include <linux/ftrace.h> #include <linux/module.h> #include <linux/mutex.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,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 ]);
@@ -894,6 +898,11 @@ 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 );
@@ -901,7 +910,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; }
@@ -974,6 +985,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)
maintainerPATCH v3 6/7 tracing: uprobe-event: Allocate string buffers from heap
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Allocate temporary string buffers for parsing uprobe-events from heap instead of stack.
---
Changes in v3:
- Split #include sorting.
Changes in v2:
- Sort #include alphabetically.
- Add fail_mem label for handling ENOMEM error.
--- kernel/trace/trace_uprobe.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-)
kernel/trace/trace_uprobe.c +19 -9
@@ -8,6 +8,7 @@#define pr_fmt (fmt) "trace_uprobe: " fmt#include <linux/bpf-cgroup.h> +#include <linux/cleanup.h> #include <linux/ctype.h> #include <linux/filter.h> #include <linux/module.h>
@@ -19,6 +20,7 @@#include <linux/uaccess.h> #include <linux/uprobes.h> +#include "trace.h" #include "trace_dynevent.h" #include "trace_probe.h" #include "trace_probe_tmpl.h"
@@ -537,15 +539,15 @@ static int register_trace_uprobe(struct trace_uprobe *tu)*/ static int __trace_uprobe_create (int argc,const char **argv) { -struct trace_uprobe *tu;const char *event =NULL , *group =UPROBE_EVENT_SYSTEM ;char *arg, *filename, *rctr, *rctr_end, *tmp; -char buf[MAX_EVENT_NAME_LEN ]; -char gbuf[MAX_EVENT_NAME_LEN ]; -enum probe_print_type ptype; -struct path path;unsigned long offset, ref_ctr_offset; + char *gbuf __free(kfree) = NULL; + char *buf __free(kfree) = NULL; +enum probe_print_type ptype; +struct trace_uprobe *tu;bool is_return = false; +struct path path;int i, ret; ref_ctr_offset =0 ;
@@ -653,6 +655,10 @@ static int __trace_uprobe_create(int argc, const char **argv)/* setup a probe */ trace_probe_log_set_index (0 );if (event) { + gbuf =kmalloc (MAX_EVENT_NAME_LEN ,GFP_KERNEL ); +if (!gbuf) + goto fail_mem; + ret =traceprobe_parse_event_name (&event, &group, gbuf, event - argv[0 ]);if (ret)
@@ -664,15 +670,16 @@ static int __trace_uprobe_create(int argc, const char **argv)char *ptr; tail =kstrdup (kbasename (filename),GFP_KERNEL ); -if (!tail) { - ret = -ENOMEM ; - goto fail_address_parse; - } +if (!tail) + goto fail_mem; ptr =strpbrk (tail,".-_" );if (ptr) *ptr ='\0' ; + buf =kmalloc (MAX_EVENT_NAME_LEN ,GFP_KERNEL ); +if (!buf) + goto fail_mem;snprintf (buf,MAX_EVENT_NAME_LEN ,"%c_%s_0x%lx" ,'p' , tail, offset); event = buf;kfree (tail);
@@ -724,6 +731,9 @@ static int __trace_uprobe_create(int argc, const char **argv)trace_probe_log_clear ();return ret; +fail_mem: + ret = -ENOMEM ; + fail_address_parse:trace_probe_log_clear ();path_put (&path);
Re: [PATCH v3 6/7] tracing: uprobe-event: Allocate string buffers from heap
Quoted 6 lines · On Wed, 23 Jul 2025 07:02:25 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Allocate temporary string buffers for parsing uprobe-events from heap instead of stack.
Signed-off-by Masami Hiramatsu (Google) <[email protected]>
-- Steve
Masami Hiramatsu (Google)
maintainerPATCH v3 7/7 tracing: probes: Add a kerneldoc for traceprobe_parse_event_name()
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Since traceprobe_parse_event_name() is a bit complicated, add a kerneldoc for explaining the behavior.
--- kernel/trace/trace_probe.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
kernel/trace/trace_probe.c +16 -1
@@ -247,7 +247,22 @@ int traceprobe_split_symbol_offset(char *symbol, long *offset)return 0 ; } -/* @buf must has MAX_EVENT_NAME_LEN size */ +/** +* traceprobe_parse_event_name - Parse a string into group and event names +* @pevent: A pointer to the string to be parsed. On return, this is updated +* to point to the event name part of the string. +* @pgroup: A pointer to the group name. This is updated to point to the parsed +* group name, which is stored in @buf. +* @buf: A buffer to store the parsed group name. +* @offset: The offset of the string in the original user command, for logging. +* +* Description: This parses a string with the format `[GROUP/][EVENT]` or +* `[GROUP.][EVENT]` (either GROUP or EVENT or both must be specified). +* The parsed group name is stored in @buf. +* The caller must ensure @buf is at least MAX_EVENT_NAME_LEN bytes. +* +* Return: 0 on success, or -EINVAL on failure. +*/ int traceprobe_parse_event_name (const char **pevent,const char **pgroup,char *buf,int offset) {
Steven Rostedt
maintainerRe: [PATCH v3 7/7] tracing: probes: Add a kerneldoc for traceprobe_parse_event_name()
Quoted 16 lines · On Wed, 23 Jul 2025 07:02:35 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Since
traceprobe_parse_event_name()is a bit complicated, add a kerneldoc for explaining the behavior.Suggested-by Steven Rostedt <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>--- kernel/trace/trace_probe.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)kernel/trace/trace_probe.c +0
-0
kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 253
Quoted 8 lines
kernel/trace/trace_probe.c +4
-1@@ -247,7 +247,22 @@ int traceprobe_split_symbol_offset(char *symbol, long *offset)return 0 ; } -/* @buf must has MAX_EVENT_NAME_LEN size */ +/** +* traceprobe_parse_event_name - Parse a string into group and event names +* @pevent: A pointer to the string to be parsed. On return, this is updated +* to point to the event name part of the string.
I believe, these are supposed to be single lines. What it has on return should be in the "Return" section below.
kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 264
Quoted 11 lines
kernel/trace/trace_probe.c +11
-0+* @pgroup: A pointer to the group name. This is updated to point to the parsed +* group name, which is stored in @buf. +* @buf: A buffer to store the parsed group name. +* @offset: The offset of the string in the original user command, for logging. +* +* Description: This parses a string with the format `[GROUP/][EVENT]` or +* `[GROUP.][EVENT]` (either GROUP or EVENT or both must be specified). +* The parsed group name is stored in @buf. +* The caller must ensure @buf is at least MAX_EVENT_NAME_LEN bytes. +* +* Return: 0 on success, or -EINVAL on failure.
So the Return section here should state what is returned in the parameters too. Like:
* @pgroup is updated to point to the parsed group which is stored in @buf.
etc.
-- Steve
Quoted 4 lines
kernel/trace/trace_probe.c +1
-0+*/ int traceprobe_parse_event_name (const char **pevent,const char **pgroup,char *buf,int offset) {
Masami Hiramatsu (Google)
maintainerRe: [PATCH v3 7/7] tracing: probes: Add a kerneldoc for traceprobe_parse_event_name()
Quoted 22 lines · On Tue, 22 Jul 2025 18:44:20 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 19 lines · On Wed, 23 Jul 2025 07:02:35 +0900 "Masami Hiramatsu (Google)"…
From: Masami Hiramatsu (Google) <[email protected]>
Since
traceprobe_parse_event_name()is a bit complicated, add a kerneldoc for explaining the behavior.Suggested-by Steven Rostedt <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>--- kernel/trace/trace_probe.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)kernel/trace/trace_probe.c +0
-0@@ -247,7 +247,22 @@ int traceprobe_split_symbol_offset(char *symbol, long *offset)return 0 ; }
Quoted 8 lines
kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 253
Quoted 5 lines
kernel/trace/trace_probe.c +4
-1-/* @buf must has MAX_EVENT_NAME_LEN size */ +/** +* traceprobe_parse_event_name - Parse a string into group and event names +* @pevent: A pointer to the string to be parsed. On return, this is updated +* to point to the event name part of the string. I believe, these are supposed to be single lines. What it has on return should be in the "Return" section below.
Hmm, indeed. OK, I'll move it into Return section.
Quoted 3 lines
kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 255
Quoted 3 lines
kernel/trace/trace_probe.c +2
-0+* @pgroup: A pointer to the group name. This is updated to point to the parsed +* group name, which is stored in @buf.
Ditto.
Quoted 10 lines
kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 264
Quoted 9 lines
kernel/trace/trace_probe.c +9
-0+* @buf: A buffer to store the parsed group name. +* @offset: The offset of the string in the original user command, for logging. +* +* Description: This parses a string with the format `[GROUP/][EVENT]` or +* `[GROUP.][EVENT]` (either GROUP or EVENT or both must be specified). +* The parsed group name is stored in @buf. +* The caller must ensure @buf is at least MAX_EVENT_NAME_LEN bytes. +* +* Return: 0 on success, or -EINVAL on failure.
Quoted 4 lines
So the Return section here should state what is returned in the parameters too. Like:
* @pgroup is updated to point to the parsed group which is stored in @buf.
OK, thanks!
Quoted 12 lines
etc.
-- Steve
Quoted 4 lines
kernel/trace/trace_probe.c +1
-0+*/ int traceprobe_parse_event_name (const char **pevent,const char **pgroup,char *buf,int offset) {
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>