表现偏执四级有什么作用?

Nic*_*ell 6 kernel

在我安装的 Ubuntu Focal 上,kernel.perf_event_paranoid默认设置为 4:

$ sysctl kernel.perf_event_paranoid
kernel.perf_event_paranoid = 4
Run Code Online (Sandbox Code Playgroud)

(我已经检查/etc/sysctl.conf了相关的配置目录,我还没有设置它。)

这对我来说似乎很奇怪,因为内核文档没有描述大于 2 的值的任何额外效果:

perf_event_paranoid:

Controls use of the performance events system by unprivileged
users (without CAP_SYS_ADMIN).  The default value is 2.

 -1: Allow use of (almost) all events by all users
     Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>=0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
     Disallow raw tracepoint access by users without CAP_SYS_ADMIN
>=1: Disallow CPU event access by users without CAP_SYS_ADMIN
>=2: Disallow kernel profiling by users without CAP_SYS_ADMIN
Run Code Online (Sandbox Code Playgroud)

来源。)

我快速搜索了主线内核源代码,但找不到任何perf_event_paranoid与大于 2 的数字进行比较的地方。

然而,设置为 4 确实有效果。我以非 root用户身份运行了以下 perf 命令,并将其perf_event_paranoid设置为 4 :

perf stat -e context-switches,cpu-migrations -r 1 -- sleep 1
Run Code Online (Sandbox Code Playgroud)

它显示以下错误:

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.
More information can be found at 'Perf events and tool security' document:
https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html
perf_event_paranoid setting is 4:
  -1: Allow use of (almost) all events by all users
      Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>= 0: Disallow raw and ftrace function tracepoint access
>= 1: Disallow CPU event access
>= 2: Disallow kernel profiling
To make the adjusted perf_event_paranoid setting permanent preserve it
in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)
Run Code Online (Sandbox Code Playgroud)

如果我更改kernel.perf_event_paranoid为 3,并运行相同的命令,我会再次以非 root 用户身份得到以下结果:


 Performance counter stats for 'sleep 1':

                 0      context-switches:u                                          
                 0      cpu-migrations:u                                            

       1.000502603 seconds time elapsed

       0.000460000 seconds user
       0.000000000 seconds sys

Run Code Online (Sandbox Code Playgroud)

因此,将perf_event_paranoid设置从默认的 4 更改为 3 会产生一定的效果,即使 Linux 内核文档没有说明具体内容。

是什么赋予了?Ubuntu 是否附带了一个自定义补丁来添加新的、更偏执的性能级别?

其他发行版

Debian 似乎带有一个非标准补丁,创建了perf_event_paranoid3 级:

Jeff Vander Stoep 于 7 月 27 日发布了该补丁。它添加了另一个可以为 sysctl 参数(即kernel.perf_event_paranoid=3)设置的值,该值限制perf_event_open()具有该CAP_SYS_ADMIN功能的进程。目前,perf_event_paranoid默认设置为 2,这不允许不具备适当功能的进程访问某些性能功能(原始跟踪点访问、CPU 事件访问和内核分析);该补丁不会更改默认值。他还提交了另一个补丁,允许配置内核以使 3 成为默认的 perf_event_paranoid 值。

来源。

但 Ubuntu 似乎比这更偏执。

Nic*_*ell 7

4 级的作用与 Debian 补丁完全相同:它禁止非特权进程使用perf_event_open(). 然而,限制在偏执4级而不是偏执3级时生效。

当前的偏执级别记录在内核源代码文件中的注释中kernel/events/core.c

/*
 * perf event paranoia level:
 *  -1 - not paranoid at all
 *   0 - disallow raw tracepoint access for unpriv
 *   1 - disallow cpu events for unpriv
 *   2 - disallow kernel profiling for unpriv
 *   4 - disallow all unpriv perf event use
 */
Run Code Online (Sandbox Code Playgroud)

可以在此处找到进行此更改的提交。(注意:提交消息和内核注释相互矛盾。内核注释对我来说似乎是正确的。)

总结一下:

  • 级别 -1 到 2:与主线内核相同。
  • 3级:与2级相同?无法100%确认这一点。有一个常量PERF_SECURITY_TRACEPOINT设置为 3,某些地方仍在使用该常量。
  • 级别 4:对非特权用户完全禁用 perf_event_open。