Skip to content
systemcoding.dev
Performance

Prepare a Linux Box for Profiling With perf_event_paranoid, kptr_restrict and CAP_PERFMON

Set kernel.perf_event_paranoid, kernel.kptr_restrict and CAP_PERFMON so perf can sample user and kernel code without running everything as root.

6 min read
Summarize in AI
Flat illustration of a CPU core with its cache levels and main memory, with the Linux, Intel and Apple logos.

The most common first failure with perf is not a bug in your program. It is a message such as "Access to performance monitoring and observability operations is limited" or a report full of unresolved kernel addresses. Both come from kernel settings that decide who may measure what. This post explains the three controls that matter on Linux 6.8, kernel.perf_event_paranoid, kernel.kptr_restrict and the CAP_PERFMON capability, and how to open exactly as much as profiling needs. It assumes you have already installed perf and want to move past sudo perf.

What perf_event_paranoid controls

perf_event_paranoid is a sysctl that limits unprivileged use of perf_event_open(2). The kernel's perf security documentation defines the levels for processes without CAP_PERFMON (and without CAP_SYS_ADMIN on older kernels):

ValueAllowed without privilege
-1Everything, including raw tracepoint access and no CPU or kernel restriction
0User and kernel measurements, including CPU-wide and system-wide counting
1User and kernel measurements, but only per-process, not CPU-wide
2User-space measurements only (the upstream default)
3Distribution patch (Debian lineage): no unprivileged use at all
4Distribution patch in some kernels: the same denial, kept for compatibility

Levels 3 and above exist only in patched distribution kernels; upstream stops at 2 as the most restrictive, so confirm the last two rows against your own kernel's documentation. Read the current setting:

Shell
cat /proc/sys/kernel/perf_event_paranoid
Text
2

At 2 you can profile your own user-space code but cannot see kernel frames or measure other users' processes. At 1 you gain kernel samples for your own processes. At 0 you gain system-wide measurement of the CPU, which is what perf top and perf record -a need.

What kptr_restrict hides

Even with permission to sample the kernel, perf must translate the sampled kernel addresses to symbol names, and the kernel hides the address-to-name map by default through kernel.kptr_restrict:

ValueEffect on /proc/kallsyms
0Addresses visible to everyone
1Addresses visible only to processes with CAP_SYSLOG
2Addresses hidden from everyone, shown as zeros

When the value hides addresses, perf report shows [unknown] or raw hexadecimal for kernel frames and prints a warning that kernel samples cannot be resolved. Check it:

Shell
cat /proc/sys/kernel/kptr_restrict
head -3 /proc/kallsyms
Text
1
0000000000000000 A fixed_percpu_data
0000000000000000 A __per_cpu_start
0000000000000000 T startup_64

All zeros mean the process reading the file lacks permission to see addresses. Run the same head under sudo and real addresses appear. The kernel profiling workflow is covered in profiling the Linux kernel itself with perf and kallsyms.

Use CAP_PERFMON instead of root

Since Linux 5.8 there is a capability for exactly this job, CAP_PERFMON. It grants performance monitoring and observability operations without the broad power of CAP_SYS_ADMIN. Give it to the perf binary with file capabilities, so one binary can profile and nothing else runs privileged:

Shell
sudo setcap "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" "$(command -v perf)"
getcap "$(command -v perf)"
Text
/usr/bin/perf cap_sys_ptrace,cap_syslog,cap_perfmon=ep

The three capabilities do different work. CAP_PERFMON permits the measurements. CAP_SYSLOG lets the tool read kernel addresses when kptr_restrict is 1. CAP_SYS_PTRACE is needed to attach to other users' processes by PID. Only grant what you use; if you profile your own programs and kernel samples are not needed, CAP_PERFMON alone is a start. The kernel documentation recommends this approach over loosening perf_event_paranoid for a whole machine, because the capability applies to one executable rather than every process.

A caution: file capabilities on a copy of perf in your home directory work, but capabilities on a binary that any user can run give every user that power. Keep the capable copy in a directory only trusted users can execute, or use a group-restricted binary as the kernel docs describe.

Set it persistently

For a developer machine you own, set the sysctls through a drop-in file so they survive a reboot:

Shell
sudo tee /etc/sysctl.d/90-perf.conf >/dev/null <<'EOF'
kernel.perf_event_paranoid = 1
kernel.kptr_restrict = 0
EOF
sudo sysctl --system
Text
* Applying /etc/sysctl.d/90-perf.conf ...
kernel.perf_event_paranoid = 1
kernel.kptr_restrict = 0

Value 1 allows kernel samples of your own processes; 0 for kptr_restrict exposes kernel addresses. The latter weakens a mitigation against kernel address disclosure, which attackers use to defeat address randomization. That makes the setting right for a lab machine and wrong for a shared server.

Containers and cloud hosts

Inside a container the sysctls are host-wide and usually cannot be changed from within. Docker's default seccomp profile blocks perf_event_open unless you grant CAP_PERFMON or CAP_SYS_ADMIN, so a container needs --cap-add=PERFMON or a custom profile on a recent runtime. Virtual machines may not expose hardware counters at all, which is separate from permissions and covered in what perf can and cannot see in VMs and containers. On a managed cloud instance you often cannot change the sysctl; ask for capabilities or use software events.

Check that it worked

Verify with a kernel-inclusive record of a short command:

Shell
perf record -F 999 -g -- dd if=/dev/zero of=/dev/null bs=1M count=2000
perf report --stdio --sort symbol | head -8
Text
2000+0 records in
2000+0 records out
2097152000 bytes (2.1 GB, 2.0 GiB) copied, 0.41 s, 5.1 GB/s

  Overhead  Symbol
  ........  ......
    31.02%  [k] copy_user_enhanced_fast_string
    12.40%  [k] read_zero
     9.15%  [.] __memcpy_avx_unaligned_erms

The [k] symbols show kernel names resolved. The timing and percentages are an illustration and will differ. If the kernel rows show raw addresses, kptr_restrict or missing CAP_SYSLOG is the cause.

When to leave it locked

On a production host serving other tenants, do not lower these globally. Use a capability on one binary, a short maintenance window with the sysctl restored afterwards, or collect on a staging replica. The decision depends on who else can run code on the machine: if only you can, relax freely; if anyone else can, give the capability to one tool and keep the sysctls at their defaults.

A quick audit before you profile

Three commands tell you whether a machine is ready, and running them first saves a confusing failure later. Read both sysctls, check whether the binary carries capabilities, and ask the tool itself. perf has a built-in check that prints exactly what it can and cannot do under the current settings:

Shell
perf record -e cycles:k -o /dev/null -- true
Text
Error:
Access to performance monitoring and observability operations is limited.
Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open
access to performance monitoring and observability operations for processes
without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.

The suffix :k asks for kernel-only sampling, which is exactly what a restrictive setting refuses, so this one-liner works as a probe. When the command succeeds with no output, kernel profiling is allowed for your user. The message names the capabilities that would also grant access, which matches the three described above.

Write the result of the audit down next to the profile. A flame graph made with kptr_restrict hiding kernel symbols looks as if the kernel does nothing, and someone reading it a month later has no way to know that the kernel time was invisible rather than absent. A one-line note such as the sysctl values and the capability set goes a long way toward making profiles comparable between machines.

Also inDebugging
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.