Skip to content

Commit 43beb5e

Browse files
committed
tracing: probe: Allocate traceprobe_parse_context from heap
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. Link: https://lore.kernel.org/all/175323425650.57270.280750740753792504.stgit@devnote2/ Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Signed-off-by: Masami Hiramatsu (Google) <[email protected]> Reviewed-by: Steven Rostedt (Google) <[email protected]>
1 parent 2f02a61 commit 43beb5e

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

kernel/trace/trace_eprobe.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,18 +797,20 @@ find_and_get_event(const char *system, const char *event_name)
797797

798798
static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[], int i)
799799
{
800-
struct traceprobe_parse_context ctx = {
801-
.event = ep->event,
802-
.flags = TPARG_FL_KERNEL | TPARG_FL_TEVENT,
803-
};
800+
struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;
804801
int ret;
805802

806-
ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], &ctx);
803+
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
804+
if (!ctx)
805+
return -ENOMEM;
806+
ctx->event = ep->event;
807+
ctx->flags = TPARG_FL_KERNEL | TPARG_FL_TEVENT;
808+
809+
ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], ctx);
807810
/* Handle symbols "@" */
808811
if (!ret)
809812
ret = traceprobe_update_arg(&ep->tp.args[i]);
810813

811-
traceprobe_finish_parse(&ctx);
812814
return ret;
813815
}
814816

kernel/trace/trace_fprobe.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,14 +1384,17 @@ static int trace_fprobe_create_internal(int argc, const char *argv[],
13841384

13851385
static int trace_fprobe_create_cb(int argc, const char *argv[])
13861386
{
1387-
struct traceprobe_parse_context ctx = {
1388-
.flags = TPARG_FL_KERNEL | TPARG_FL_FPROBE,
1389-
};
1387+
struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;
13901388
int ret;
13911389

1390+
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1391+
if (!ctx)
1392+
return -ENOMEM;
1393+
1394+
ctx->flags = TPARG_FL_KERNEL | TPARG_FL_FPROBE,
1395+
13921396
trace_probe_log_init("trace_fprobe", argc, argv);
1393-
ret = trace_fprobe_create_internal(argc, argv, &ctx);
1394-
traceprobe_finish_parse(&ctx);
1397+
ret = trace_fprobe_create_internal(argc, argv, ctx);
13951398
trace_probe_log_clear();
13961399
return ret;
13971400
}

kernel/trace/trace_kprobe.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,14 +1065,18 @@ static int trace_kprobe_create_internal(int argc, const char *argv[],
10651065

10661066
static int trace_kprobe_create_cb(int argc, const char *argv[])
10671067
{
1068-
struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
1068+
struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) = NULL;
10691069
int ret;
10701070

1071+
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1072+
if (!ctx)
1073+
return -ENOMEM;
1074+
ctx->flags = TPARG_FL_KERNEL;
1075+
10711076
trace_probe_log_init("trace_kprobe", argc, argv);
10721077

1073-
ret = trace_kprobe_create_internal(argc, argv, &ctx);
1078+
ret = trace_kprobe_create_internal(argc, argv, ctx);
10741079

1075-
traceprobe_finish_parse(&ctx);
10761080
trace_probe_log_clear();
10771081
return ret;
10781082
}

kernel/trace/trace_probe.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/bitops.h>
1414
#include <linux/btf.h>
15+
#include <linux/cleanup.h>
1516
#include <linux/kprobes.h>
1617
#include <linux/limits.h>
1718
#include <linux/perf_event.h>
@@ -439,6 +440,14 @@ extern void traceprobe_free_probe_arg(struct probe_arg *arg);
439440
* this MUST be called for clean up the context and return a resource.
440441
*/
441442
void traceprobe_finish_parse(struct traceprobe_parse_context *ctx);
443+
static inline void traceprobe_free_parse_ctx(struct traceprobe_parse_context *ctx)
444+
{
445+
traceprobe_finish_parse(ctx);
446+
kfree(ctx);
447+
}
448+
449+
DEFINE_FREE(traceprobe_parse_context, struct traceprobe_parse_context *,
450+
if (_T) traceprobe_free_parse_ctx(_T))
442451

443452
extern int traceprobe_split_symbol_offset(char *symbol, long *offset);
444453
int traceprobe_parse_event_name(const char **pevent, const char **pgroup,

kernel/trace/trace_uprobe.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,16 @@ static int __trace_uprobe_create(int argc, const char **argv)
695695

696696
/* parse arguments */
697697
for (i = 0; i < argc; i++) {
698-
struct traceprobe_parse_context ctx = {
699-
.flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER,
700-
};
698+
struct traceprobe_parse_context *ctx __free(traceprobe_parse_context)
699+
= kzalloc(sizeof(*ctx), GFP_KERNEL);
701700

701+
if (!ctx) {
702+
ret = -ENOMEM;
703+
goto error;
704+
}
705+
ctx->flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER;
702706
trace_probe_log_set_index(i + 2);
703-
ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], &ctx);
704-
traceprobe_finish_parse(&ctx);
707+
ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], ctx);
705708
if (ret)
706709
goto error;
707710
}

0 commit comments

Comments
 (0)