tracing: probes: Use heap instead of stack for temporary buffers
Masami Hiramatsu (Google)
maintainerv4 cover tracing: probes: Use heap instead of stack for temporary buffers
Hi,
This is the 4th 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/175322168606.44400.9155291012158349647.stgit@devnote2/
This version fixes kernel-doc stlye and moves longer description of parameters to Return section in [7/7] and adds Steve's reviewed-by for [2/7],[3/7] and [6/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 | 22 +++++++++++++++-- kernel/trace/trace_probe.h | 26 ++++++++++++++------ kernel/trace/trace_uprobe.c | 53 +++++++++++++++++++++++++--------------- 6 files changed, 171 insertions(+), 82 deletions(-)
--
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH v4 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 v4 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; }
Masami Hiramatsu (Google)
maintainerRe: [PATCH v4 2/7] tracing: probe: Allocate traceprobe_parse_context from heap
Quoted 9 lines · On Wed, 23 Jul 2025 10:30:56 +0900 "Masami Hiramatsu (Google)"…
kernel/trace/trace_fprobe.c +0
-1@@ -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 = {
kernel/trace/trace_fprobe.c static int trace_fprobe_create_internal(int argc, const char *argv[], · line 1394
Quoted 10 lines
kernel/trace/trace_fprobe.c +6
-2- .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,
Oops, this last should be ';' instead of ','!
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerPATCH 1/1 tracing: trace_fprobe: Fix typo of the semicolon
In reply to Masami Hiramatsu (Google)From: Masami Hiramatsu (Google) <[email protected]>
Fix a typo that uses ',' instead of ';' for line delimiter.
--- kernel/trace/trace_fprobe.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_fprobe.c +1 -1
@@ -1404,7 +1404,7 @@ static int trace_fprobe_create_cb(int argc, const char *argv[])if (!ctx)return -ENOMEM ; - ctx->flags =TPARG_FL_KERNEL |TPARG_FL_FPROBE , + ctx->flags =TPARG_FL_KERNEL |TPARG_FL_FPROBE ;trace_probe_log_init ("trace_fprobe" , argc, argv); ret =trace_fprobe_create_internal (argc, argv, ctx);
Masami Hiramatsu (Google)
maintainerPATCH v4 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);
Masami Hiramatsu (Google)
maintainerPATCH v4 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 v4 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 v4 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);
Masami Hiramatsu (Google)
maintainerPATCH v4 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.
---
Changes in v4:
- Move longer parameter description into Return section.
- Rewrite the style according to Documentation/doc-guide/kernel-doc.rst
--- kernel/trace/trace_probe.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
kernel/trace/trace_probe.c +19 -1
@@ -247,7 +247,25 @@ 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. +* @pgroup: A pointer to the group name. +* @buf: A buffer to store the parsed group name. +* @offset: The offset of the string in the original user command, for logging. +* +* This parses a string with the format `[GROUP/][EVENT]` or `[GROUP.][EVENT]` +* (either GROUP or EVENT or both must be specified). +* Since 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. +* +* If success, *@pevent is updated to point to the event name part of the +* original string, or NULL if there is no event name. +* Also, *@pgroup is updated to point to the parsed group which is stored +* in @buf, or NULL if there is no group name. +*/ int traceprobe_parse_event_name (const char **pevent,const char **pgroup,char *buf,int offset) {
Re: [PATCH v4 7/7] tracing: probes: Add a kerneldoc for traceprobe_parse_event_name()
Quoted 13 lines · On Wed, 23 Jul 2025 10:31:45 +0900 "Masami Hiramatsu (Google)"…
kernel/trace/trace_probe.c +12
-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. +* @pgroup: A pointer to the group name. +* @buf: A buffer to store the parsed group name. +* @offset: The offset of the string in the original user command, for logging. +* +* This parses a string with the format `[GROUP/][EVENT]` or `[GROUP.][EVENT]` +* (either GROUP or EVENT or both must be specified). +* Since the parsed group name is stored in @buf, the caller must ensure @buf +* is at least MAX_EVENT_NAME_LEN bytes. +*
kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 271
Quoted 10 lines
kernel/trace/trace_probe.c +7
-0+* Return: 0 on success, or -EINVAL on failure. +* +* If success, *@pevent is updated to point to the event name part of the +* original string, or NULL if there is no event name. +* Also, *@pgroup is updated to point to the parsed group which is stored +* in @buf, or NULL if there is no group name. +*/ int traceprobe_parse_event_name (const char **pevent,const char **pgroup,char *buf,int offset) {
Much better.
-- Steve
Masami Hiramatsu (Google)
maintainerRe: [PATCH v4 7/7] tracing: probes: Add a kerneldoc for traceprobe_parse_event_name()
Quoted 29 lines · On Wed, 23 Jul 2025 10:00:49 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 13 lines · On Wed, 23 Jul 2025 10:31:45 +0900 "Masami Hiramatsu (Google)"…
kernel/trace/trace_probe.c +12
-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. +* @pgroup: A pointer to the group name. +* @buf: A buffer to store the parsed group name. +* @offset: The offset of the string in the original user command, for logging. +* +* This parses a string with the format `[GROUP/][EVENT]` or `[GROUP.][EVENT]` +* (either GROUP or EVENT or both must be specified). +* Since the parsed group name is stored in @buf, the caller must ensure @buf +* is at least MAX_EVENT_NAME_LEN bytes. +* kernel/trace/trace_probe.c int traceprobe_split_symbol_offset(char *symbol, long *offset) · line 271
Quoted 10 lines
kernel/trace/trace_probe.c +7
-0+* Return: 0 on success, or -EINVAL on failure. +* +* If success, *@pevent is updated to point to the event name part of the +* original string, or NULL if there is no event name. +* Also, *@pgroup is updated to point to the parsed group which is stored +* in @buf, or NULL if there is no group name. +*/ int traceprobe_parse_event_name (const char **pevent,const char **pgroup,char *buf,int offset) {Much better.
Quoted 1 line
Reviewed-by Steven Rostedt (Google) <[email protected]>
Thank you! Let me pick the series in probes/for-next.
Quoted 3 lines
-- Steve
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>