Skip to content

Commit 9270546

Browse files
committed
tracing/kprobes: Simplify __trace_kprobe_create() by removing gotos
Simplify __trace_kprobe_create() by removing gotos. Link: https://lore.kernel.org/all/173643301102.1514810.6149004416601259466.stgit@devnote2/ Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 7dcc352 commit 9270546

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

kernel/trace/trace_kprobe.c

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ static int validate_probe_symbol(char *symbol)
836836
static int trace_kprobe_entry_handler(struct kretprobe_instance *ri,
837837
struct pt_regs *regs);
838838

839-
static int __trace_kprobe_create(int argc, const char *argv[])
839+
static int trace_kprobe_create_internal(int argc, const char *argv[],
840+
struct traceprobe_parse_context *ctx)
840841
{
841842
/*
842843
* Argument syntax:
@@ -877,7 +878,6 @@ static int __trace_kprobe_create(int argc, const char *argv[])
877878
char gbuf[MAX_EVENT_NAME_LEN];
878879
char abuf[MAX_BTF_ARGS_LEN];
879880
char *dbuf __free(kfree) = NULL;
880-
struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
881881

882882
switch (argv[0][0]) {
883883
case 'r':
@@ -891,38 +891,36 @@ static int __trace_kprobe_create(int argc, const char *argv[])
891891
if (argc < 2)
892892
return -ECANCELED;
893893

894-
trace_probe_log_init("trace_kprobe", argc, argv);
895-
896894
event = strchr(&argv[0][1], ':');
897895
if (event)
898896
event++;
899897

900898
if (isdigit(argv[0][1])) {
901899
if (!is_return) {
902900
trace_probe_log_err(1, BAD_MAXACT_TYPE);
903-
goto parse_error;
901+
return -EINVAL;
904902
}
905903
if (event)
906904
len = event - &argv[0][1] - 1;
907905
else
908906
len = strlen(&argv[0][1]);
909907
if (len > MAX_EVENT_NAME_LEN - 1) {
910908
trace_probe_log_err(1, BAD_MAXACT);
911-
goto parse_error;
909+
return -EINVAL;
912910
}
913911
memcpy(buf, &argv[0][1], len);
914912
buf[len] = '\0';
915913
ret = kstrtouint(buf, 0, &maxactive);
916914
if (ret || !maxactive) {
917915
trace_probe_log_err(1, BAD_MAXACT);
918-
goto parse_error;
916+
return -EINVAL;
919917
}
920918
/* kretprobes instances are iterated over via a list. The
921919
* maximum should stay reasonable.
922920
*/
923921
if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
924922
trace_probe_log_err(1, MAXACT_TOO_BIG);
925-
goto parse_error;
923+
return -EINVAL;
926924
}
927925
}
928926

@@ -931,16 +929,13 @@ static int __trace_kprobe_create(int argc, const char *argv[])
931929
if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
932930
trace_probe_log_set_index(1);
933931
/* Check whether uprobe event specified */
934-
if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
935-
ret = -ECANCELED;
936-
goto out;
937-
}
932+
if (strchr(argv[1], '/') && strchr(argv[1], ':'))
933+
return -ECANCELED;
934+
938935
/* a symbol specified */
939936
symbol = kstrdup(argv[1], GFP_KERNEL);
940-
if (!symbol) {
941-
ret = -ENOMEM;
942-
goto out;
943-
}
937+
if (!symbol)
938+
return -ENOMEM;
944939

945940
tmp = strchr(symbol, '%');
946941
if (tmp) {
@@ -949,33 +944,33 @@ static int __trace_kprobe_create(int argc, const char *argv[])
949944
is_return = true;
950945
} else {
951946
trace_probe_log_err(tmp - symbol, BAD_ADDR_SUFFIX);
952-
goto parse_error;
947+
return -EINVAL;
953948
}
954949
}
955950

956951
/* TODO: support .init module functions */
957952
ret = traceprobe_split_symbol_offset(symbol, &offset);
958953
if (ret || offset < 0 || offset > UINT_MAX) {
959954
trace_probe_log_err(0, BAD_PROBE_ADDR);
960-
goto parse_error;
955+
return -EINVAL;
961956
}
962957
ret = validate_probe_symbol(symbol);
963958
if (ret) {
964959
if (ret == -EADDRNOTAVAIL)
965960
trace_probe_log_err(0, NON_UNIQ_SYMBOL);
966961
else
967962
trace_probe_log_err(0, BAD_PROBE_ADDR);
968-
goto parse_error;
963+
return -EINVAL;
969964
}
970965
if (is_return)
971-
ctx.flags |= TPARG_FL_RETURN;
966+
ctx->flags |= TPARG_FL_RETURN;
972967
ret = kprobe_on_func_entry(NULL, symbol, offset);
973968
if (ret == 0 && !is_return)
974-
ctx.flags |= TPARG_FL_FENTRY;
969+
ctx->flags |= TPARG_FL_FENTRY;
975970
/* Defer the ENOENT case until register kprobe */
976971
if (ret == -EINVAL && is_return) {
977972
trace_probe_log_err(0, BAD_RETPROBE);
978-
goto parse_error;
973+
return -EINVAL;
979974
}
980975
}
981976

@@ -984,7 +979,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
984979
ret = traceprobe_parse_event_name(&event, &group, gbuf,
985980
event - argv[0]);
986981
if (ret)
987-
goto parse_error;
982+
return ret;
988983
}
989984

990985
if (!event) {
@@ -1000,26 +995,24 @@ static int __trace_kprobe_create(int argc, const char *argv[])
1000995
}
1001996

1002997
argc -= 2; argv += 2;
1003-
ctx.funcname = symbol;
998+
ctx->funcname = symbol;
1004999
new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc,
1005-
abuf, MAX_BTF_ARGS_LEN, &ctx);
1000+
abuf, MAX_BTF_ARGS_LEN, ctx);
10061001
if (IS_ERR(new_argv)) {
10071002
ret = PTR_ERR(new_argv);
10081003
new_argv = NULL;
1009-
goto out;
1004+
return ret;
10101005
}
10111006
if (new_argv) {
10121007
argc = new_argc;
10131008
argv = new_argv;
10141009
}
1015-
if (argc > MAX_TRACE_ARGS) {
1016-
ret = -E2BIG;
1017-
goto out;
1018-
}
1010+
if (argc > MAX_TRACE_ARGS)
1011+
return -E2BIG;
10191012

10201013
ret = traceprobe_expand_dentry_args(argc, argv, &dbuf);
10211014
if (ret)
1022-
goto out;
1015+
return ret;
10231016

10241017
/* setup a probe */
10251018
tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
@@ -1028,16 +1021,16 @@ static int __trace_kprobe_create(int argc, const char *argv[])
10281021
ret = PTR_ERR(tk);
10291022
/* This must return -ENOMEM, else there is a bug */
10301023
WARN_ON_ONCE(ret != -ENOMEM);
1031-
goto out; /* We know tk is not allocated */
1024+
return ret; /* We know tk is not allocated */
10321025
}
10331026

10341027
/* parse arguments */
10351028
for (i = 0; i < argc; i++) {
10361029
trace_probe_log_set_index(i + 2);
1037-
ctx.offset = 0;
1038-
ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], &ctx);
1030+
ctx->offset = 0;
1031+
ret = traceprobe_parse_probe_arg(&tk->tp, i, argv[i], ctx);
10391032
if (ret)
1040-
goto out; /* This can be -ENOMEM */
1033+
return ret; /* This can be -ENOMEM */
10411034
}
10421035
/* entry handler for kretprobe */
10431036
if (is_return && tk->tp.entry_arg) {
@@ -1048,7 +1041,7 @@ static int __trace_kprobe_create(int argc, const char *argv[])
10481041
ptype = is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
10491042
ret = traceprobe_set_print_fmt(&tk->tp, ptype);
10501043
if (ret < 0)
1051-
goto out;
1044+
return ret;
10521045

10531046
ret = register_trace_kprobe(tk);
10541047
if (ret) {
@@ -1059,26 +1052,34 @@ static int __trace_kprobe_create(int argc, const char *argv[])
10591052
trace_probe_log_err(0, BAD_PROBE_ADDR);
10601053
else if (ret != -ENOMEM && ret != -EEXIST)
10611054
trace_probe_log_err(0, FAIL_REG_PROBE);
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;
1055+
return ret;
1056+
}
1057+
/*
1058+
* Here, 'tk' has been registered to the list successfully,
1059+
* so we don't need to free it.
1060+
*/
1061+
tk = NULL;
1062+
1063+
return 0;
1064+
}
1065+
1066+
static int trace_kprobe_create_cb(int argc, const char *argv[])
1067+
{
1068+
struct traceprobe_parse_context ctx = { .flags = TPARG_FL_KERNEL };
1069+
int ret;
1070+
1071+
trace_probe_log_init("trace_kprobe", argc, argv);
1072+
1073+
ret = trace_kprobe_create_internal(argc, argv, &ctx);
10681074

1069-
out:
10701075
traceprobe_finish_parse(&ctx);
10711076
trace_probe_log_clear();
10721077
return ret;
1073-
1074-
parse_error:
1075-
ret = -EINVAL;
1076-
goto out;
10771078
}
10781079

10791080
static int trace_kprobe_create(const char *raw_command)
10801081
{
1081-
return trace_probe_create(raw_command, __trace_kprobe_create);
1082+
return trace_probe_create(raw_command, trace_kprobe_create_cb);
10821083
}
10831084

10841085
static int create_or_delete_trace_kprobe(const char *raw_command)

0 commit comments

Comments
 (0)