Skip to content

Commit 7dcc352

Browse files
committed
tracing: Use __free() for kprobe events to cleanup
Use __free() in trace_kprobe.c to cleanup code. Link: https://lore.kernel.org/all/173643299989.1514810.2924926552980462072.stgit@devnote2/ Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 4af0532 commit 7dcc352

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

kernel/trace/trace_kprobe.c

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define pr_fmt(fmt) "trace_kprobe: " fmt
99

1010
#include <linux/bpf-cgroup.h>
11+
#include <linux/cleanup.h>
1112
#include <linux/security.h>
1213
#include <linux/module.h>
1314
#include <linux/uaccess.h>
@@ -257,6 +258,9 @@ static void free_trace_kprobe(struct trace_kprobe *tk)
257258
}
258259
}
259260

261+
DEFINE_FREE(free_trace_kprobe, struct trace_kprobe *,
262+
if (!IS_ERR_OR_NULL(_T)) free_trace_kprobe(_T))
263+
260264
/*
261265
* Allocate new trace_probe and initialize it (including kprobes).
262266
*/
@@ -268,7 +272,7 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,
268272
int maxactive,
269273
int nargs, bool is_return)
270274
{
271-
struct trace_kprobe *tk;
275+
struct trace_kprobe *tk __free(free_trace_kprobe) = NULL;
272276
int ret = -ENOMEM;
273277

274278
tk = kzalloc(struct_size(tk, tp.args, nargs), GFP_KERNEL);
@@ -277,12 +281,12 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,
277281

278282
tk->nhit = alloc_percpu(unsigned long);
279283
if (!tk->nhit)
280-
goto error;
284+
return ERR_PTR(ret);
281285

282286
if (symbol) {
283287
tk->symbol = kstrdup(symbol, GFP_KERNEL);
284288
if (!tk->symbol)
285-
goto error;
289+
return ERR_PTR(ret);
286290
tk->rp.kp.symbol_name = tk->symbol;
287291
tk->rp.kp.offset = offs;
288292
} else
@@ -299,13 +303,10 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,
299303

300304
ret = trace_probe_init(&tk->tp, event, group, false, nargs);
301305
if (ret < 0)
302-
goto error;
306+
return ERR_PTR(ret);
303307

304308
dyn_event_init(&tk->devent, &trace_kprobe_ops);
305-
return tk;
306-
error:
307-
free_trace_kprobe(tk);
308-
return ERR_PTR(ret);
309+
return_ptr(tk);
309310
}
310311

311312
static struct trace_kprobe *find_trace_kprobe(const char *event,
@@ -861,11 +862,12 @@ static int __trace_kprobe_create(int argc, const char *argv[])
861862
* Type of args:
862863
* FETCHARG:TYPE : use TYPE instead of unsigned long.
863864
*/
864-
struct trace_kprobe *tk = NULL;
865+
struct trace_kprobe *tk __free(free_trace_kprobe) = NULL;
865866
int i, len, new_argc = 0, ret = 0;
866867
bool is_return = false;
867-
char *symbol = NULL, *tmp = NULL;
868-
const char **new_argv = NULL;
868+
char *symbol __free(kfree) = NULL;
869+
char *tmp = NULL;
870+
const char **new_argv __free(kfree) = NULL;
869871
const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
870872
enum probe_print_type ptype;
871873
int maxactive = 0;
@@ -874,7 +876,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
874876
char buf[MAX_EVENT_NAME_LEN];
875877
char gbuf[MAX_EVENT_NAME_LEN];
876878
char abuf[MAX_BTF_ARGS_LEN];
877-
char *dbuf = NULL;
879+
char *dbuf __free(kfree) = NULL;
878880
struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
879881

880882
switch (argv[0][0]) {
@@ -931,13 +933,13 @@ static int __trace_kprobe_create(int argc, const char *argv[])
931933
/* Check whether uprobe event specified */
932934
if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
933935
ret = -ECANCELED;
934-
goto error;
936+
goto out;
935937
}
936938
/* a symbol specified */
937939
symbol = kstrdup(argv[1], GFP_KERNEL);
938940
if (!symbol) {
939941
ret = -ENOMEM;
940-
goto error;
942+
goto out;
941943
}
942944

943945
tmp = strchr(symbol, '%');
@@ -1035,7 +1037,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
10351037
ctx.offset = 0;
10361038
ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], &ctx);
10371039
if (ret)
1038-
goto error; /* This can be -ENOMEM */
1040+
goto out; /* This can be -ENOMEM */
10391041
}
10401042
/* entry handler for kretprobe */
10411043
if (is_return && tk->tp.entry_arg) {
@@ -1046,7 +1048,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
10461048
ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
10471049
ret = traceprobe_set_print_fmt(&tk->tp, ptype);
10481050
if (ret < 0)
1049-
goto error;
1051+
goto out;
10501052

10511053
ret = register_trace_kprobe(tk);
10521054
if (ret) {
@@ -1057,21 +1059,20 @@ static int __trace_kprobe_create(int argc, const char *argv[])
10571059
trace_probe_log_err(0, BAD_PROBE_ADDR);
10581060
else if (ret != -ENOMEM && ret != -EEXIST)
10591061
trace_probe_log_err(0, FAIL_REG_PROBE);
1060-
goto error;
1061-
}
1062+
} else
1063+
/*
1064+
* Here, 'tk' has been registered to the list successfully,
1065+
* so we don't need to free it.
1066+
*/
1067+
tk = NULL;
10621068

10631069
out:
10641070
traceprobe_finish_parse(&ctx);
10651071
trace_probe_log_clear();
1066-
kfree(new_argv);
1067-
kfree(symbol);
1068-
kfree(dbuf);
10691072
return ret;
10701073

10711074
parse_error:
10721075
ret = -EINVAL;
1073-
error:
1074-
free_trace_kprobe(tk);
10751076
goto out;
10761077
}
10771078

@@ -1893,7 +1894,7 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
18931894
bool is_return)
18941895
{
18951896
enum probe_print_type ptype;
1896-
struct trace_kprobe *tk;
1897+
struct trace_kprobe *tk __free(free_trace_kprobe) = NULL;
18971898
int ret;
18981899
char *event;
18991900

@@ -1924,19 +1925,14 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
19241925

19251926
ptype = trace_kprobe_is_return(tk) ?
19261927
PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
1927-
if (traceprobe_set_print_fmt(&tk->tp, ptype) < 0) {
1928-
ret = -ENOMEM;
1929-
goto error;
1930-
}
1928+
if (traceprobe_set_print_fmt(&tk->tp, ptype) < 0)
1929+
return ERR_PTR(-ENOMEM);
19311930

19321931
ret = __register_trace_kprobe(tk);
19331932
if (ret < 0)
1934-
goto error;
1933+
return ERR_PTR(ret);
19351934

1936-
return trace_probe_event_call(&tk->tp);
1937-
error:
1938-
free_trace_kprobe(tk);
1939-
return ERR_PTR(ret);
1935+
return trace_probe_event_call(&(no_free_ptr(tk)->tp));
19401936
}
19411937

19421938
void destroy_local_trace_kprobe(struct trace_event_call *event_call)

0 commit comments

Comments
 (0)