Skip to contentSkip to conversation

perf/tools: fix perf_evsel__fallback() for paranoid=2 and -b

PATCH In review Stephane Eranian 2 participants 2 messages Mar 24, 2020 – Mar 25, 2020 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/tools: fix perf_evsel__fallback() for paranoid=2 and -b

When perf_event_paranoid=2, regular users are prevented from sampling at the kernel level. However, it the user passes an event without a privilege level, the tool will force :u when it detects paranoid>1. This works well, except when branch sampling is requested. It has a more stringent requirement especially with exclude_hv.

$ perf record ls
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.001 MB /tmp/perf.data ]

But:

$ perf record -b ls
Error:
You may not have permission to collect stats.

Consider tweaking /proc/sys/kernel/perf_event_paranoid, which controls use of the performance events system by unprivileged users (without CAP_SYS_ADMIN).

The current value is 2:

-1: Allow use of (almost) all events by all users
>= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK >= 1: Disallow CPU event access by users without CAP_SYS_ADMIN >= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN

To make this setting permanent, edit /etc/sysctl.conf too, e.g.:

kernel.perf_event_paranoid = -1

The problem is that in the fallback cod only exclude_kernel is checked and if set, then exclude_hv is not forced to 1. When branch sampling is enabled exclude_hv must be set.

This patch fixes the bug in the fallback code by considering the value of exclude_hv and not just exclude_kernel. We prefer this approach to give a chance to exclude_hv=0.

---
 tools/perf/util/evsel.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/evsel.c +2 -1
index 816d930d774e7..db0e6112992e5 100644
@@ -2424,7 +2424,8 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
 
 		zfree(&evsel->name);
 		return true;
-	} else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
+	} else if (err == EACCES &&
+		   (!evsel->core.attr.exclude_kernel || !evsel->core.attr.exclude_hv) &&
 		   (paranoid = perf_event_paranoid()) > 1) {
 		const char *name = perf_evsel__name(evsel);
 		char *new_name;
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 33 lines · Stephane Eranian

When perf_event_paranoid=2, regular users are prevented from sampling at the kernel level. However, it the user passes an event without a privilege level, the tool will force :u when it detects paranoid>1. This works well, except when branch sampling is requested. It has a more stringent requirement especially with exclude_hv.

$ perf record ls
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.001 MB /tmp/perf.data ]

But:

$ perf record -b ls
Error:
You may not have permission to collect stats.

Consider tweaking /proc/sys/kernel/perf_event_paranoid, which controls use of the performance events system by unprivileged users (without CAP_SYS_ADMIN).

The current value is 2:

-1: Allow use of (almost) all events by all users
>= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK >= 1: Disallow CPU event access by users without CAP_SYS_ADMIN >= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN

To make this setting permanent, edit /etc/sysctl.conf too, e.g.:

kernel.perf_event_paranoid = -1

The problem is that in the fallback cod only exclude_kernel is checked and if set, then exclude_hv is not forced to 1. When branch sampling is enabled exclude_hv must be set.

Quoted 5 lines

This patch fixes the bug in the fallback code by considering the value of exclude_hv and not just exclude_kernel. We prefer this approach to give a chance to exclude_hv=0.

thanks,
jirka

Quoted 21 lines
---
 tools/perf/util/evsel.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/evsel.c +2 -1
index 816d930d774e7..db0e6112992e5 100644
@@ -2424,7 +2424,8 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
 
 		zfree(&evsel->name);
 		return true;
-	} else if (err == EACCES && !evsel->core.attr.exclude_kernel &&
+	} else if (err == EACCES &&
+		   (!evsel->core.attr.exclude_kernel || !evsel->core.attr.exclude_hv) &&
 		   (paranoid = perf_event_paranoid()) > 1) {
 		const char *name = perf_evsel__name(evsel);
 		char *new_name;
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog