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

Configure Feed

Select the types of activity you want to include in your feed.

perf record: Add --setup-filter option

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>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
3dee4b83 73bf63a4

+20
+5
tools/perf/Documentation/perf-record.txt
··· 828 828 only, as of now. So the applications built without the frame 829 829 pointer might see bogus addresses. 830 830 831 + --setup-filter=<action>:: 832 + Prepare BPF filter to be used by regular users. The action should be 833 + either "pin" or "unpin". The filter can be used after it's pinned. 834 + 835 + 831 836 include::intel-hybrid.txt[] 832 837 833 838 SEE ALSO
+15
tools/perf/builtin-record.c
··· 171 171 bool timestamp_filename; 172 172 bool timestamp_boundary; 173 173 bool off_cpu; 174 + const char *filter_action; 174 175 struct switch_output switch_output; 175 176 unsigned long long samples; 176 177 unsigned long output_max_size; /* = 0: unlimited */ ··· 3558 3557 "write collected trace data into several data files using parallel threads", 3559 3558 record__parse_threads), 3560 3559 OPT_BOOLEAN(0, "off-cpu", &record.off_cpu, "Enable off-cpu analysis"), 3560 + OPT_STRING(0, "setup-filter", &record.filter_action, "pin|unpin", 3561 + "BPF filter action"), 3561 3562 OPT_END() 3562 3563 }; 3563 3564 ··· 4087 4084 if (rec->timestamp_filename && record__threads_enabled(rec)) { 4088 4085 rec->timestamp_filename = false; 4089 4086 pr_warning("WARNING: --timestamp-filename option is not available in parallel streaming mode.\n"); 4087 + } 4088 + 4089 + if (rec->filter_action) { 4090 + if (!strcmp(rec->filter_action, "pin")) 4091 + err = perf_bpf_filter__pin(); 4092 + else if (!strcmp(rec->filter_action, "unpin")) 4093 + err = perf_bpf_filter__unpin(); 4094 + else { 4095 + pr_warning("Unknown BPF filter action: %s\n", rec->filter_action); 4096 + err = -EINVAL; 4097 + } 4098 + goto out_opts; 4090 4099 } 4091 4100 4092 4101 /*