8
8
#define pr_fmt (fmt ) "trace_kprobe: " fmt
9
9
10
10
#include <linux/bpf-cgroup.h>
11
+ #include <linux/cleanup.h>
11
12
#include <linux/security.h>
12
13
#include <linux/module.h>
13
14
#include <linux/uaccess.h>
@@ -257,6 +258,9 @@ static void free_trace_kprobe(struct trace_kprobe *tk)
257
258
}
258
259
}
259
260
261
+ DEFINE_FREE (free_trace_kprobe , struct trace_kprobe * ,
262
+ if (!IS_ERR_OR_NULL (_T )) free_trace_kprobe (_T ))
263
+
260
264
/*
261
265
* Allocate new trace_probe and initialize it (including kprobes).
262
266
*/
@@ -268,7 +272,7 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,
268
272
int maxactive ,
269
273
int nargs , bool is_return )
270
274
{
271
- struct trace_kprobe * tk ;
275
+ struct trace_kprobe * tk __free ( free_trace_kprobe ) = NULL ;
272
276
int ret = - ENOMEM ;
273
277
274
278
tk = kzalloc (struct_size (tk , tp .args , nargs ), GFP_KERNEL );
@@ -277,12 +281,12 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,
277
281
278
282
tk -> nhit = alloc_percpu (unsigned long );
279
283
if (!tk -> nhit )
280
- goto error ;
284
+ return ERR_PTR ( ret ) ;
281
285
282
286
if (symbol ) {
283
287
tk -> symbol = kstrdup (symbol , GFP_KERNEL );
284
288
if (!tk -> symbol )
285
- goto error ;
289
+ return ERR_PTR ( ret ) ;
286
290
tk -> rp .kp .symbol_name = tk -> symbol ;
287
291
tk -> rp .kp .offset = offs ;
288
292
} else
@@ -299,13 +303,10 @@ static struct trace_kprobe *alloc_trace_kprobe(const char *group,
299
303
300
304
ret = trace_probe_init (& tk -> tp , event , group , false, nargs );
301
305
if (ret < 0 )
302
- goto error ;
306
+ return ERR_PTR ( ret ) ;
303
307
304
308
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 );
309
310
}
310
311
311
312
static struct trace_kprobe * find_trace_kprobe (const char * event ,
@@ -861,11 +862,12 @@ static int __trace_kprobe_create(int argc, const char *argv[])
861
862
* Type of args:
862
863
* FETCHARG:TYPE : use TYPE instead of unsigned long.
863
864
*/
864
- struct trace_kprobe * tk = NULL ;
865
+ struct trace_kprobe * tk __free ( free_trace_kprobe ) = NULL ;
865
866
int i , len , new_argc = 0 , ret = 0 ;
866
867
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 ;
869
871
const char * event = NULL , * group = KPROBE_EVENT_SYSTEM ;
870
872
enum probe_print_type ptype ;
871
873
int maxactive = 0 ;
@@ -874,7 +876,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
874
876
char buf [MAX_EVENT_NAME_LEN ];
875
877
char gbuf [MAX_EVENT_NAME_LEN ];
876
878
char abuf [MAX_BTF_ARGS_LEN ];
877
- char * dbuf = NULL ;
879
+ char * dbuf __free ( kfree ) = NULL ;
878
880
struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
879
881
880
882
switch (argv [0 ][0 ]) {
@@ -931,13 +933,13 @@ static int __trace_kprobe_create(int argc, const char *argv[])
931
933
/* Check whether uprobe event specified */
932
934
if (strchr (argv [1 ], '/' ) && strchr (argv [1 ], ':' )) {
933
935
ret = - ECANCELED ;
934
- goto error ;
936
+ goto out ;
935
937
}
936
938
/* a symbol specified */
937
939
symbol = kstrdup (argv [1 ], GFP_KERNEL );
938
940
if (!symbol ) {
939
941
ret = - ENOMEM ;
940
- goto error ;
942
+ goto out ;
941
943
}
942
944
943
945
tmp = strchr (symbol , '%' );
@@ -1035,7 +1037,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
1035
1037
ctx .offset = 0 ;
1036
1038
ret = traceprobe_parse_probe_arg (& tk -> tp , i , argv [i ], & ctx );
1037
1039
if (ret )
1038
- goto error ; /* This can be -ENOMEM */
1040
+ goto out ; /* This can be -ENOMEM */
1039
1041
}
1040
1042
/* entry handler for kretprobe */
1041
1043
if (is_return && tk -> tp .entry_arg ) {
@@ -1046,7 +1048,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
1046
1048
ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL ;
1047
1049
ret = traceprobe_set_print_fmt (& tk -> tp , ptype );
1048
1050
if (ret < 0 )
1049
- goto error ;
1051
+ goto out ;
1050
1052
1051
1053
ret = register_trace_kprobe (tk );
1052
1054
if (ret ) {
@@ -1057,21 +1059,20 @@ static int __trace_kprobe_create(int argc, const char *argv[])
1057
1059
trace_probe_log_err (0 , BAD_PROBE_ADDR );
1058
1060
else if (ret != - ENOMEM && ret != - EEXIST )
1059
1061
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 ;
1062
1068
1063
1069
out :
1064
1070
traceprobe_finish_parse (& ctx );
1065
1071
trace_probe_log_clear ();
1066
- kfree (new_argv );
1067
- kfree (symbol );
1068
- kfree (dbuf );
1069
1072
return ret ;
1070
1073
1071
1074
parse_error :
1072
1075
ret = - EINVAL ;
1073
- error :
1074
- free_trace_kprobe (tk );
1075
1076
goto out ;
1076
1077
}
1077
1078
@@ -1893,7 +1894,7 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1893
1894
bool is_return )
1894
1895
{
1895
1896
enum probe_print_type ptype ;
1896
- struct trace_kprobe * tk ;
1897
+ struct trace_kprobe * tk __free ( free_trace_kprobe ) = NULL ;
1897
1898
int ret ;
1898
1899
char * event ;
1899
1900
@@ -1924,19 +1925,14 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1924
1925
1925
1926
ptype = trace_kprobe_is_return (tk ) ?
1926
1927
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 );
1931
1930
1932
1931
ret = __register_trace_kprobe (tk );
1933
1932
if (ret < 0 )
1934
- goto error ;
1933
+ return ERR_PTR ( ret ) ;
1935
1934
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 ));
1940
1936
}
1941
1937
1942
1938
void destroy_local_trace_kprobe (struct trace_event_call * event_call )
0 commit comments