]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
perf record: Add --setup-filter option
authorNamhyung Kim <namhyung@kernel.org>
Wed, 3 Jul 2024 22:30:34 +0000 (15:30 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 1 Aug 2024 15:11:33 +0000 (12:11 -0300)
To allow BPF filters for unprivileged users it needs to pin the BPF
objects to BPF-fs first.  Let's add a new option to pin and unpin the
objects easily.  I'm not sure 'perf record' is a right place to do this
but I don't have a better idea right now.

  $ sudo perf record --setup-filter pin

The above command would pin BPF program and maps for the filter when the
system has BPF-fs (usually at /sys/fs/bpf/).  To unpin the objects,
users can run the following command (as root).

  $ sudo perf record --setup-filter unpin

Committer testing:

  root@number:~# perf record --setup-filter pin
  root@number:~# ls -la /sys/fs/bpf/perf_filter/
  total 0
  drwxr-xr-x. 2 root root 0 Jul 31 10:43 .
  drwxr-xr-t. 3 root root 0 Jul 31 10:43 ..
  -rw-rw-rw-. 1 root root 0 Jul 31 10:43 dropped
  -rw-rw-rw-. 1 root root 0 Jul 31 10:43 filters
  -rwxrwxrwx. 1 root root 0 Jul 31 10:43 perf_sample_filter
  -rw-rw-rw-. 1 root root 0 Jul 31 10:43 pid_hash
  -rw-------. 1 root root 0 Jul 31 10:43 sample_f_rodata
  root@number:~# ls -la /sys/fs/bpf/perf_filter/perf_sample_filter
  -rwxrwxrwx. 1 root root 0 Jul 31 10:43 /sys/fs/bpf/perf_filter/perf_sample_filter
  root@number:~#

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: bpf@vger.kernel.org
Link: https://lore.kernel.org/r/20240703223035.2024586-8-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-record.txt
tools/perf/builtin-record.c

index d6532ed97c02540dee5f9fdddec0959ae6cc9d5c..41e36b4dc765f671b0623357bc5b21505033e8ff 100644 (file)
@@ -828,6 +828,11 @@ filtered through the mask provided by -C option.
        only, as of now.  So the applications built without the frame
        pointer might see bogus addresses.
 
+--setup-filter=<action>::
+       Prepare BPF filter to be used by regular users.  The action should be
+       either "pin" or "unpin".  The filter can be used after it's pinned.
+
+
 include::intel-hybrid.txt[]
 
 SEE ALSO
index 01fb95487a63cb0dcab97a39bba9418a6aa334c8..72345d1e54b097e0d0e9534c85bd0f03c930ac9c 100644 (file)
@@ -171,6 +171,7 @@ struct record {
        bool                    timestamp_filename;
        bool                    timestamp_boundary;
        bool                    off_cpu;
+       const char              *filter_action;
        struct switch_output    switch_output;
        unsigned long long      samples;
        unsigned long           output_max_size;        /* = 0: unlimited */
@@ -3557,6 +3558,8 @@ static struct option __record_options[] = {
                            "write collected trace data into several data files using parallel threads",
                            record__parse_threads),
        OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"),
+       OPT_STRING(0, "setup-filter", &record.filter_action, "pin|unpin",
+                  "BPF filter action"),
        OPT_END()
 };
 
@@ -4086,6 +4089,18 @@ int cmd_record(int argc, const char **argv)
                pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n");
        }
 
+       if (rec->filter_action) {
+               if (!strcmp(rec->filter_action, "pin"))
+                       err = perf_bpf_filter__pin();
+               else if (!strcmp(rec->filter_action, "unpin"))
+                       err = perf_bpf_filter__unpin();
+               else {
+                       pr_warning("Unknown BPF filter action: %s\n", rec->filter_action);
+                       err = -EINVAL;
+               }
+               goto out_opts;
+       }
+
        /*
         * Allow aliases to facilitate the lookup of symbols for address
         * filters. Refer to auxtrace_parse_filters().