50

I'm running debian testing with the 4.1 kernel and version 4.1 of the perf tool. In this version they seem to have added some sort of protection to keep normal users from collecting data from that tool. So running perf as normal user will give this error:

perf stat ls
Error:
You may not have permission to collect stats.
Consider tweaking /proc/sys/kernel/perf_event_paranoid:
 -1 - Not paranoid at all
  0 - Disallow raw tracepoint access for unpriv
  1 - Disallow cpu events for unpriv
  2 - Disallow kernel profiling for unpriv

perf_event_paranoid contains 3 in my installation. Unfortunately I can't change that file even as root. How can I allow my own user to use perf without sudo rights?

I have an application I would like to benchmark which doesn't need root and I don't want to run it as root to benchmark it.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
kain88
  • 645
  • 1
  • 6
  • 6
  • Does `perf stat -e cycles:u` work? "3" value of `perf_event_paranoid` was added in 2016 https://lwn.net/Articles/696216/ "Disallowing perf_event_open()" and was enabled in "Android and Debian" (also https://lkml.org/lkml/2016/1/11/587 https://bugs.launchpad.net/bugs/1612790 https://www.debian.org/security/2017/dsa-3791) – osgx May 25 '18 at 03:54
  • 1
    @osgx: For me with paranoid level 3 your suggestion does not work, I am still not allowed to use it. – Martin Ueding May 15 '19 at 12:00

1 Answers1

79

Files in /proc that are writable are usually changed by echoing a value into them. You should try:

sudo sh -c 'echo 1 >/proc/sys/kernel/perf_event_paranoid'

The files under /proc/sys/ also have the sysctl command for easy access, so you can instead do:

sudo sysctl -w kernel.perf_event_paranoid=1

(though the -w for write seems to be optional). To ensure this is done at boot time create your own /etc/sysctl.d/99-mysettings.conf file with the line

kernel.perf_event_paranoid=1

Choose a filename that will not override existing files in /run/sysctl.d/ and /usr/lib/sysctl.d/. See man sysctl.d.

meuh
  • 6,119
  • 1
  • 20
  • 26
  • 17
    To persist across reboots: `sudo sh -c 'echo kernel.perf_event_paranoid=1 > /etc/sysctl.d/local.conf'` – Márcio Mar 12 '17 at 06:43
  • 1
    add the comment to the answer, please – Leos313 Aug 13 '19 at 11:14
  • 13
    @Márcio: You want to *append* to `local.conf`, not truncate. Or write to `/etc/sysctl.d/perf.conf` – Peter Cordes Nov 05 '19 at 10:59
  • Was able to change the value, but still got the same error (Tried -1,0,1) – Jasper Wu Jan 28 '23 at 00:00
  • 1
    This is also useful reading https://unix.stackexchange.com/questions/519070/security-implications-of-changing-perf-event-paranoid (setting capabilities for perf) & the discussion it points to https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html – Bruce Adams May 01 '23 at 08:46