Skip to content

Commit 84369d7

Browse files
aharivelbonzini
authored andcommitted
tools: build qemu-vmsr-helper
Introduce a privileged helper to access RAPL MSR. The privileged helper tool, qemu-vmsr-helper, is designed to provide virtual machines with the ability to read specific RAPL (Running Average Power Limit) MSRs without requiring CAP_SYS_RAWIO privileges or relying on external, out-of-tree patches. The helper tool leverages Unix permissions and SO_PEERCRED socket options to enforce access control, ensuring that only processes explicitly requesting read access via readmsr() from a valid Thread ID can access these MSRs. The list of RAPL MSRs that are allowed to be read by the helper tool is defined in rapl-msr-index.h. This list corresponds to the RAPL MSRs that will be supported in the next commit titled "Add support for RAPL MSRs in KVM/QEMU." The tool is intentionally designed to run on the Linux x86 platform. This initial implementation is tailored for Intel CPUs but can be extended to support AMD CPUs in the future. Signed-off-by: Anthony Harivel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 95fa0c7 commit 84369d7

File tree

8 files changed

+680
-0
lines changed

8 files changed

+680
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ F: docs/system/target-i386*
140140
F: target/i386/*.[ch]
141141
F: target/i386/Kconfig
142142
F: target/i386/meson.build
143+
F: tools/i386/
143144

144145
Guest CPU cores (TCG)
145146
---------------------
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=Virtual RAPL MSR Daemon for QEMU
3+
4+
[Service]
5+
WorkingDirectory=/tmp
6+
Type=simple
7+
ExecStart=/usr/bin/qemu-vmsr-helper
8+
PrivateTmp=yes
9+
ProtectSystem=strict
10+
ReadWritePaths=/var/run
11+
RestrictAddressFamilies=AF_UNIX
12+
Restart=always
13+
RestartSec=0
14+
15+
[Install]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Virtual RAPL MSR helper for QEMU
3+
4+
[Socket]
5+
ListenStream=/run/qemu-vmsr-helper.sock
6+
SocketMode=0600
7+
8+
[Install]
9+
WantedBy=multi-user.target

docs/tools/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ command line utilities and other standalone programs.
1616
qemu-pr-helper
1717
qemu-trace-stap
1818
virtfs-proxy-helper
19+
qemu-vmsr-helper

docs/tools/qemu-vmsr-helper.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
==================================
2+
QEMU virtual RAPL MSR helper
3+
==================================
4+
5+
Synopsis
6+
--------
7+
8+
**qemu-vmsr-helper** [*OPTION*]
9+
10+
Description
11+
-----------
12+
13+
Implements the virtual RAPL MSR helper for QEMU.
14+
15+
Accessing the RAPL (Running Average Power Limit) MSR enables the RAPL powercap
16+
driver to advertise and monitor the power consumption or accumulated energy
17+
consumption of different power domains, such as CPU packages, DRAM, and other
18+
components when available.
19+
20+
However those register are accesible under priviliged access (CAP_SYS_RAWIO).
21+
QEMU can use an external helper to access those priviliged register.
22+
23+
:program:`qemu-vmsr-helper` is that external helper; it creates a listener
24+
socket which will accept incoming connections for communication with QEMU.
25+
26+
If you want to run VMs in a setup like this, this helper should be started as a
27+
system service, and you should read the QEMU manual section on "RAPL MSR
28+
support" to find out how to configure QEMU to connect to the socket created by
29+
:program:`qemu-vmsr-helper`.
30+
31+
After connecting to the socket, :program:`qemu-vmsr-helper` can
32+
optionally drop root privileges, except for those capabilities that
33+
are needed for its operation.
34+
35+
:program:`qemu-vmsr-helper` can also use the systemd socket activation
36+
protocol. In this case, the systemd socket unit should specify a
37+
Unix stream socket, like this::
38+
39+
[Socket]
40+
ListenStream=/var/run/qemu-vmsr-helper.sock
41+
42+
Options
43+
-------
44+
45+
.. program:: qemu-vmsr-helper
46+
47+
.. option:: -d, --daemon
48+
49+
run in the background (and create a PID file)
50+
51+
.. option:: -q, --quiet
52+
53+
decrease verbosity
54+
55+
.. option:: -v, --verbose
56+
57+
increase verbosity
58+
59+
.. option:: -f, --pidfile=PATH
60+
61+
PID file when running as a daemon. By default the PID file
62+
is created in the system runtime state directory, for example
63+
:file:`/var/run/qemu-vmsr-helper.pid`.
64+
65+
.. option:: -k, --socket=PATH
66+
67+
path to the socket. By default the socket is created in
68+
the system runtime state directory, for example
69+
:file:`/var/run/qemu-vmsr-helper.sock`.
70+
71+
.. option:: -T, --trace [[enable=]PATTERN][,events=FILE][,file=FILE]
72+
73+
.. include:: ../qemu-option-trace.rst.inc
74+
75+
.. option:: -u, --user=USER
76+
77+
user to drop privileges to
78+
79+
.. option:: -g, --group=GROUP
80+
81+
group to drop privileges to
82+
83+
.. option:: -h, --help
84+
85+
Display a help message and exit.
86+
87+
.. option:: -V, --version
88+
89+
Display version information and exit.

meson.build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,6 +4073,13 @@ if have_tools
40734073
dependencies: [authz, crypto, io, qom, qemuutil,
40744074
libcap_ng, mpathpersist],
40754075
install: true)
4076+
4077+
if cpu in ['x86', 'x86_64']
4078+
executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),
4079+
dependencies: [authz, crypto, io, qom, qemuutil,
4080+
libcap_ng, mpathpersist],
4081+
install: true)
4082+
endif
40764083
endif
40774084

40784085
if have_ivshmem

0 commit comments

Comments
 (0)