Skip to contentSkip to conversation

perf record: fix priv level with branch sampling for paranoid=2

PATCH Superseded Stephane Eranian 2 participants 4 messages Sep 4, 2019 – Sep 20, 2019 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf record: fix priv level with branch sampling for paranoid=2

Now that the default perf_events paranoid level is set to 2, a regular user cannot monitor kernel level activity anymore. As such, with the following cmdline:

$ perf record -e cycles date

The perf tool first tries cycles:uk but then falls back to cycles:u as can be seen in the perf report --header-only output:

cmdline : /export/hda3/tmp/perf.tip record -e cycles ls event : name = cycles:u, , id = { 436186, ... }

This is okay as long as there is way to learn the priv level was changed internally by the tool.

But consider a similar example:

$ perf record -b -e cycles date
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).
...

Why is that treated differently given that the branch sampling inherits the priv level of the first event in this case, i.e., cycles:u? It turns out that the branch sampling code is more picky and also checks exclude_hv.

In the fallback path, perf record is setting exclude_kernel = 1, but it does not change exclude_hv. This does not seem to match the restriction imposed by paranoid = 2.

This patch fixes the problem by forcing exclude_hv = 1 in the fallback for paranoid=2. With this in place:

$ perf record -b -e cycles date
  cmdline : /export/hda3/tmp/perf.tip record -b -e cycles ls
  event : name = cycles:u, , id = { 436847, ... }

And the command succeeds as expected.

---
 tools/perf/util/evsel.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +4 -2
index 85825384f9e8..3cbe06fdf7f7 100644
@@ -2811,9 +2811,11 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
 		if (evsel->name)
 			free(evsel->name);
 		evsel->name = new_name;
-		scnprintf(msg, msgsize,
-"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
+		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
+			  "to fall back to excluding kernel and hypervisor "
+			  " samples", paranoid);
 		evsel->core.attr.exclude_kernel = 1;
+		evsel->core.attr.exclude_hv     = 1;
 
 		return true;
 	}
Signature · 1 line
2.23.0.187.g17f5b7556c-goog
#1 lore raw
Quoted 35 lines · Stephane Eranian

Now that the default perf_events paranoid level is set to 2, a regular user cannot monitor kernel level activity anymore. As such, with the following cmdline:

$ perf record -e cycles date

The perf tool first tries cycles:uk but then falls back to cycles:u as can be seen in the perf report --header-only output:

cmdline : /export/hda3/tmp/perf.tip record -e cycles ls event : name = cycles:u, , id = { 436186, ... }

This is okay as long as there is way to learn the priv level was changed internally by the tool.

But consider a similar example:

$ perf record -b -e cycles date
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).
...

Why is that treated differently given that the branch sampling inherits the priv level of the first event in this case, i.e., cycles:u? It turns out that the branch sampling code is more picky and also checks exclude_hv.

In the fallback path, perf record is setting exclude_kernel = 1, but it does not change exclude_hv. This does not seem to match the restriction imposed by paranoid = 2.

Quoted 9 lines

This patch fixes the problem by forcing exclude_hv = 1 in the fallback for paranoid=2. With this in place:

$ perf record -b -e cycles date
  cmdline : /export/hda3/tmp/perf.tip record -b -e cycles ls
  event : name = cycles:u, , id = { 436847, ... }

And the command succeeds as expected.

Any comment on this patch?

Quoted 26 lines
---
 tools/perf/util/evsel.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +4 -3
index 85825384f9e8..3cbe06fdf7f7 100644
@@ -2811,9 +2811,11 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
                if (evsel->name)
                        free(evsel->name);
                evsel->name = new_name;
-               scnprintf(msg, msgsize,
-"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
+               scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
+                         "to fall back to excluding kernel and hypervisor "
+                         " samples", paranoid);
                evsel->core.attr.exclude_kernel = 1;
+               evsel->core.attr.exclude_hv     = 1;
 
                return true;
        }
--

2.23.0.187.g17f5b7556c-goog

Acked-by on v1 1/1
#2 lore raw
In reply to Stephane Eranian
Quoted 53 lines · Stephane Eranian

Now that the default perf_events paranoid level is set to 2, a regular user cannot monitor kernel level activity anymore. As such, with the following cmdline:

$ perf record -e cycles date

The perf tool first tries cycles:uk but then falls back to cycles:u as can be seen in the perf report --header-only output:

cmdline : /export/hda3/tmp/perf.tip record -e cycles ls event : name = cycles:u, , id = { 436186, ... }

This is okay as long as there is way to learn the priv level was changed internally by the tool.

But consider a similar example:

$ perf record -b -e cycles date
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).
...

Why is that treated differently given that the branch sampling inherits the priv level of the first event in this case, i.e., cycles:u? It turns out that the branch sampling code is more picky and also checks exclude_hv.

In the fallback path, perf record is setting exclude_kernel = 1, but it does not change exclude_hv. This does not seem to match the restriction imposed by paranoid = 2.

This patch fixes the problem by forcing exclude_hv = 1 in the fallback for paranoid=2. With this in place:

$ perf record -b -e cycles date
  cmdline : /export/hda3/tmp/perf.tip record -b -e cycles ls
  event : name = cycles:u, , id = { 436847, ... }

And the command succeeds as expected.

---
 tools/perf/util/evsel.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +0 -0
index 85825384f9e8..3cbe06fdf7f7 100644

tools/perf/util/evsel.c bool perf_evsel__fallback(struct evsel *evsel, int err, · line 2816

Quoted 8 lines
tools/perf/util/evsel.c +3 -2
@@ -2811,9 +2811,11 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
 		if (evsel->name)
 			free(evsel->name);
 		evsel->name = new_name;
-		scnprintf(msg, msgsize,
-"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
+		scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
+			  "to fall back to excluding kernel and hypervisor "
+			  " samples", paranoid);

extra space in here ^

Warning:
kernel.perf_event_paranoid=2, trying to fall back to excluding kernel and hypervisor samples

other than that it looks good to me

thanks,
jirka

#3 lore raw
Quoted 66 lines · Jiri Olsa
Quoted 53 lines · Stephane Eranian

Now that the default perf_events paranoid level is set to 2, a regular user cannot monitor kernel level activity anymore. As such, with the following cmdline:

$ perf record -e cycles date

The perf tool first tries cycles:uk but then falls back to cycles:u as can be seen in the perf report --header-only output:

cmdline : /export/hda3/tmp/perf.tip record -e cycles ls event : name = cycles:u, , id = { 436186, ... }

This is okay as long as there is way to learn the priv level was changed internally by the tool.

But consider a similar example:

$ perf record -b -e cycles date
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).
...

Why is that treated differently given that the branch sampling inherits the priv level of the first event in this case, i.e., cycles:u? It turns out that the branch sampling code is more picky and also checks exclude_hv.

In the fallback path, perf record is setting exclude_kernel = 1, but it does not change exclude_hv. This does not seem to match the restriction imposed by paranoid = 2.

This patch fixes the problem by forcing exclude_hv = 1 in the fallback for paranoid=2. With this in place:

$ perf record -b -e cycles date
  cmdline : /export/hda3/tmp/perf.tip record -b -e cycles ls
  event : name = cycles:u, , id = { 436847, ... }

And the command succeeds as expected.

---
 tools/perf/util/evsel.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +0 -0
index 85825384f9e8..3cbe06fdf7f7 100644

tools/perf/util/evsel.c bool perf_evsel__fallback(struct evsel *evsel, int err, · line 2816

Quoted 8 lines
tools/perf/util/evsel.c +3 -2
@@ -2811,9 +2811,11 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
              if (evsel->name)
                      free(evsel->name);
              evsel->name = new_name;
-             scnprintf(msg, msgsize,
-"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
+             scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying "
+                       "to fall back to excluding kernel and hypervisor "
+                       " samples", paranoid);

extra space in here ^

Quoted 5 lines

Warning:
kernel.perf_event_paranoid=2, trying to fall back to excluding kernel and hypervisor samples

other than that it looks good to me

Fixed in v2.

Quoted 4 lines

thanks,
jirka