Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf ftrace: Factor out check_ftrace_capable()

The check is a common part of the ftrace commands, let's move it out.

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Changbin Du <changbin.du@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Link: https://lore.kernel.org/lkml/20240729004127.238611-3-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
608585f4 c7780089

+20 -24
+20 -24
tools/perf/builtin-ftrace.c
··· 59 59 done = true; 60 60 } 61 61 62 + static int check_ftrace_capable(void) 63 + { 64 + if (!(perf_cap__capable(CAP_PERFMON) || 65 + perf_cap__capable(CAP_SYS_ADMIN))) { 66 + pr_err("ftrace only works for %s!\n", 67 + #ifdef HAVE_LIBCAP_SUPPORT 68 + "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" 69 + #else 70 + "root" 71 + #endif 72 + ); 73 + return -1; 74 + } 75 + return 0; 76 + } 77 + 62 78 static int __write_tracing_file(const char *name, const char *val, bool append) 63 79 { 64 80 char *file; ··· 602 586 .events = POLLIN, 603 587 }; 604 588 605 - if (!(perf_cap__capable(CAP_PERFMON) || 606 - perf_cap__capable(CAP_SYS_ADMIN))) { 607 - pr_err("ftrace only works for %s!\n", 608 - #ifdef HAVE_LIBCAP_SUPPORT 609 - "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" 610 - #else 611 - "root" 612 - #endif 613 - ); 614 - return -1; 615 - } 616 - 617 589 select_tracer(ftrace); 618 590 619 591 if (reset_tracing_files(ftrace) < 0) { ··· 905 901 .events = POLLIN, 906 902 }; 907 903 int buckets[NUM_BUCKET] = { }; 908 - 909 - if (!(perf_cap__capable(CAP_PERFMON) || 910 - perf_cap__capable(CAP_SYS_ADMIN))) { 911 - pr_err("ftrace only works for %s!\n", 912 - #ifdef HAVE_LIBCAP_SUPPORT 913 - "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" 914 - #else 915 - "root" 916 - #endif 917 - ); 918 - return -1; 919 - } 920 904 921 905 trace_fd = prepare_func_latency(ftrace); 922 906 if (trace_fd < 0) ··· 1211 1219 signal(SIGUSR1, sig_handler); 1212 1220 signal(SIGCHLD, sig_handler); 1213 1221 signal(SIGPIPE, sig_handler); 1222 + 1223 + ret = check_ftrace_capable(); 1224 + if (ret < 0) 1225 + return -1; 1214 1226 1215 1227 ret = perf_config(perf_ftrace_config, &ftrace); 1216 1228 if (ret < 0)