Add support for the RAPL MSRs series
v3 cover Add support for the RAPL MSRs series
Dear maintainers,
First of all, thank you very much for your recent review of my patch [1].
In this version (v3), I have attempted to address the most crucial and challenging aspect highlighted in your last review.
I am hopeful that we can now engage in a discussion and address the remaining potential points that need attention.
Thank you for your continued guidance.
v2 -> v3
--------
- Move all memory allocations from Clib to Glib
- Compile on *BSD (working on Linux only)
- No more limitation on the virtual package: each vCPU that belongs to the same virtual package is giving the same results like expected on a real CPU.
This has been tested topology like:
-smp 4,sockets=2
-smp 16,sockets=4,cores=2,threads=2v1 -> v2
--------
- To overcome the CVE-2020-8694 a socket communication is created to a priviliged helper
- Add the priviliged helper (qemu-vmsr-helper)
- Add SO_PEERCRED in qio channel socket
RFC -> v1
---------
- Add vmsr_* in front of all vmsr specific function
- Change malloc()/calloc()... with all glib equivalent
- Pre-allocate all dynamic memories when possible
- Add a Documentation of implementation, limitation and usage
Best regards,
Anthony
[1]: https://www.mail-archive.com/[email protected]/msg1003382.html
Anthony Harivel (3):
qio: add support for SO_PEERCRED for socket channel
tools: build qemu-vmsr-helper
Add support for RAPL MSRs in KVM/Qemu
accel/kvm/kvm-all.c | 27 ++ contrib/systemd/qemu-vmsr-helper.service | 15 + contrib/systemd/qemu-vmsr-helper.socket | 9 + docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 133 ++++++ docs/tools/index.rst | 1 + docs/tools/qemu-vmsr-helper.rst | 89 ++++ include/io/channel.h | 21 + include/sysemu/kvm_int.h | 17 + io/channel-socket.c | 23 + io/channel.c | 12 + meson.build | 5 + target/i386/cpu.h | 8 + target/i386/kvm/kvm.c | 348 ++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 295 +++++++++++++ target/i386/kvm/vmsr_energy.h | 87 ++++ tools/i386/qemu-vmsr-helper.c | 507 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 19 files changed, 1627 insertions(+) create mode 100644 contrib/systemd/qemu-vmsr-helper.service create mode 100644 contrib/systemd/qemu-vmsr-helper.socket create mode 100644 docs/specs/rapl-msr.rst create mode 100644 docs/tools/qemu-vmsr-helper.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.h create mode 100644 tools/i386/qemu-vmsr-helper.c create mode 100644 tools/i386/rapl-msr-index.h
Signature · 1 line
2.43.0
PATCH v3 1/3 qio: add support for SO_PEERCRED for socket channel
The function qio_channel_get_peercred() returns a pointer to the credentials of the peer process connected to this socket.
This credentials structure is defined in <sys/socket.h> as follows:
struct ucred {
pid_t pid; /* Process ID of the sending process */ uid_t uid; /* User ID of the sending process */ gid_t gid; /* Group ID of the sending process */
};
The use of this function is possible only for connected AF_UNIX stream sockets and for AF_UNIX stream and datagram socket pairs.
On platform other than Linux, the function return 0.
--- include/io/channel.h | 21 +++++++++++++++++++++ io/channel-socket.c | 23 +++++++++++++++++++++++ io/channel.c | 12 ++++++++++++ 3 files changed, 56 insertions(+)
include/io/channel.h +21 -0
@@ -149,6 +149,9 @@ struct QIOChannelClass { void *opaque);int (*io_flush)(QIOChannel *ioc,Error **errp); +void (*io_peerpid)(QIOChannel *ioc, +unsigned int *pid, +Error **errp); };/* General I/O handling functions */
@@ -898,4 +901,22 @@ int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,int qio_channel_flush (QIOChannel *ioc,Error **errp); +/** +* qio_channel_get_peercred: +* @ioc: the channel object +* @pid: pointer to pid +* @errp: pointer to a NULL-initialized error object +* +* Returns the pid of the peer process connected to this socket. +* +* The use of this function is possible only for connected +* AF_UNIX stream sockets and for AF_UNIX stream and datagram +* socket pairs on Linux. +* Return 0 for the non-Linux OS. +* +*/ +void qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp); +#endif /* QIO_CHANNEL_H */
io/channel-socket.c +23 -0
@@ -841,6 +841,28 @@ qio_channel_socket_set_cork(QIOChannel *ioc,socket_set_cork (sioc->fd, v); } +static void +qio_channel_socket_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +#ifdef CONFIG_LINUX +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); +Error *err =NULL ; +socklen_t len =sizeof (struct ucred ); + +struct ucred cred; +if (getsockopt (sioc->fd, +SOL_SOCKET ,SO_PEERCRED , + &cred, &len) ==-1 ) { +error_setg_errno (&err, errno,"Unable to get peer credentials" ); +error_propagate (errp, err); + } + *pid = (unsigned int )cred.pid; +#else + *pid =0 ; +#endif +} static int qio_channel_socket_close(QIOChannel *ioc,
@@ -938,6 +960,7 @@ static void qio_channel_socket_class_init(ObjectClass *klass,#ifdef QEMU_MSG_ZEROCOPY ioc_klass->io_flush = qio_channel_socket_flush;#endif + ioc_klass->io_peerpid = qio_channel_socket_get_peerpid; }static const TypeInfo qio_channel_socket_info = {
io/channel.c +12 -0
@@ -490,6 +490,18 @@ void qio_channel_set_cork(QIOChannel *ioc, } } +void qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +QIOChannelClass *klass =QIO_CHANNEL_GET_CLASS (ioc); + +if (!klass->io_peerpid) { +error_setg (errp,"Channel does not support peer pid" ); +return ; + } + klass->io_peerpid (ioc, pid, errp); +} off_t qio_channel_io_seek(QIOChannel *ioc, off_t offset,
Signature · 1 line
2.43.0
Re: [PATCH v3 1/3] qio: add support for SO_PEERCRED for socket channel
Quoted 28 lines · Anthony Harivel
The function
qio_channel_get_peercred()returns a pointer to the credentials of the peer process connected to this socket.This credentials structure is defined in <
sys/socket.h> as follows:struct ucred {
pid_t pid; /* Process ID of the sending process */ uid_t uid; /* User ID of the sending process */ gid_t gid; /* Group ID of the sending process */};
The use of this function is possible only for connected
AF_UNIXstream sockets and forAF_UNIXstream and datagram socket pairs.On platform other than Linux, the function return 0.
Signed-off-by Anthony Harivel <[email protected]>--- include/io/channel.h | 21 +++++++++++++++++++++ io/channel-socket.c | 23 +++++++++++++++++++++++ io/channel.c | 12 ++++++++++++ 3 files changed, 56 insertions(+)include/io/channel.h +0
-0
include/io/channel.h struct QIOChannelClass { · line 154
Quoted 6 lines
include/io/channel.h +3
-0@@ -149,6 +149,9 @@ struct QIOChannelClass { void *opaque);int (*io_flush)(QIOChannel *ioc,Error **errp); +void (*io_peerpid)(QIOChannel *ioc, +unsigned int *pid, +Error **errp);
Idented 1 space too many
Quoted 9 lines
include/io/channel.h +2
-0};/* General I/O handling functions */ @@ -898,4 +901,22 @@ int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,int qio_channel_flush (QIOChannel *ioc,Error **errp); +/** +* qio_channel_get_peercred:
include/io/channel.h int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc, · line 915
Quoted 10 lines
include/io/channel.h +10
-0+* @ioc: the channel object +* @pid: pointer to pid +* @errp: pointer to a NULL-initialized error object +* +* Returns the pid of the peer process connected to this socket. +* +* The use of this function is possible only for connected +* AF_UNIX stream sockets and for AF_UNIX stream and datagram +* socket pairs on Linux. +* Return 0 for the non-Linux OS.
Update to say this returns an error for other platforms
include/io/channel.h int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc, · line 920
Quoted 5 lines
include/io/channel.h +5
-0+* +*/ +void qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp);
Idented 1 space too many
Quoted 20 lines
include/io/channel.h +1
-0+#endif /* QIO_CHANNEL_H */ io/channel-socket.c +10
-0@@ -841,6 +841,28 @@ qio_channel_socket_set_cork(QIOChannel *ioc,socket_set_cork (sioc->fd, v); } +static void +qio_channel_socket_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +#ifdef CONFIG_LINUX +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); +Error *err =NULL ; +socklen_t len =sizeof (struct ucred ); +
io/channel-socket.c qio_channel_socket_set_cork(QIOChannel *ioc, · line 863
Quoted 10 lines
io/channel-socket.c +10
-0+struct ucred cred; +if (getsockopt (sioc->fd, +SOL_SOCKET ,SO_PEERCRED , + &cred, &len) ==-1 ) { +error_setg_errno (&err, errno,"Unable to get peer credentials" ); +error_propagate (errp, err); + } + *pid = (unsigned int )cred.pid; +#else + *pid =0 ;
Defaulting 'pid' to 0 is potentially unsafe, because to a caller it now appears that the remote party is 'root' and thus implied to be a privileged account.
We should 'error_setg' for any platform we don't have an impl on, so the caller will see a fail for any usage.
Quoted 26 lines
io/channel-socket.c +3
-0+#endif +} static int qio_channel_socket_close(QIOChannel *ioc,@@ -938,6 +960,7 @@ static void qio_channel_socket_class_init(ObjectClass *klass,#ifdef QEMU_MSG_ZEROCOPY ioc_klass->io_flush = qio_channel_socket_flush;#endif + ioc_klass->io_peerpid = qio_channel_socket_get_peerpid; }static const TypeInfo qio_channel_socket_info = {io/channel.c +5
-0@@ -490,6 +490,18 @@ void qio_channel_set_cork(QIOChannel *ioc, } } +void qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +QIOChannelClass *klass =QIO_CHANNEL_GET_CLASS (ioc);
io/channel.c void qio_channel_set_cork(QIOChannel *ioc, · line 507
Quoted 10 lines
io/channel.c +7
-0+ +if (!klass->io_peerpid) { +error_setg (errp,"Channel does not support peer pid" ); +return ; + } + klass->io_peerpid (ioc, pid, errp); +} off_t qio_channel_io_seek(QIOChannel *ioc, off_t offset,
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 1/3] qio: add support for SO_PEERCRED for socket channel
Quoted 16 lines · Daniel P. Berrangé
Quoted 16 lines
io/channel-socket.c +16
-0+static void +qio_channel_socket_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +#ifdef CONFIG_LINUX +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); +Error *err =NULL ; +socklen_t len =sizeof (struct ucred ); + +struct ucred cred; +if (getsockopt (sioc->fd, +SOL_SOCKET ,SO_PEERCRED , + &cred, &len) ==-1 ) { +error_setg_errno (&err, errno,"Unable to get peer credentials" ); +error_propagate (errp, err);
Quoted 8 lines
io/channel-socket.c qio_channel_socket_set_cork(QIOChannel *ioc, · line 863
Quoted 4 lines
io/channel-socket.c +4
-0+ } + *pid = (unsigned int )cred.pid; +#else + *pid =0 ;Defaulting 'pid' to 0 is potentially unsafe, because to a caller it now appears that the remote party is 'root' and thus implied to be a privileged account.
This is a pid, so 0 cannot be confused; however, I agree that returning an error is better.
Paolo
Re: [PATCH v3 1/3] qio: add support for SO_PEERCRED for socket channel
Quoted 26 lines · Paolo Bonzini
Quoted 16 lines · Daniel P. Berrangé
Quoted 16 lines
io/channel-socket.c +16
-0+static void +qio_channel_socket_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +#ifdef CONFIG_LINUX +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); +Error *err =NULL ; +socklen_t len =sizeof (struct ucred ); + +struct ucred cred; +if (getsockopt (sioc->fd, +SOL_SOCKET ,SO_PEERCRED , + &cred, &len) ==-1 ) { +error_setg_errno (&err, errno,"Unable to get peer credentials" ); +error_propagate (errp, err);Quoted 8 lines
io/channel-socket.c qio_channel_socket_set_cork(QIOChannel *ioc, · line 863
Quoted 4 lines
io/channel-socket.c +4
-0+ } + *pid = (unsigned int )cred.pid; +#else + *pid =0 ;Defaulting 'pid' to 0 is potentially unsafe, because to a caller it now appears that the remote party is 'root' and thus implied to be a privileged account.
Quoted 2 lines
This is a pid, so 0 cannot be confused; however, I agree that returning an error is better.
Opps, face-palm !
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH v3 2/3 tools: build qemu-vmsr-helper
In reply to Anthony HarivelIntroduce 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.
--- contrib/systemd/qemu-vmsr-helper.service | 15 + contrib/systemd/qemu-vmsr-helper.socket | 9 + docs/tools/index.rst | 1 + docs/tools/qemu-vmsr-helper.rst | 89 ++++ meson.build | 5 + tools/i386/qemu-vmsr-helper.c | 507 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 7 files changed, 654 insertions(+) create mode 100644 contrib/systemd/qemu-vmsr-helper.service create mode 100644 contrib/systemd/qemu-vmsr-helper.socket create mode 100644 docs/tools/qemu-vmsr-helper.rst create mode 100644 tools/i386/qemu-vmsr-helper.c create mode 100644 tools/i386/rapl-msr-index.h
contrib/systemd/qemu-vmsr-helper.service new file +15 -0
@@ -0,0 +1,15 @@ +[Unit] +Description=Virtual RAPL MSR Daemon for QEMU + +[Service] +WorkingDirectory=/tmp +Type=simple +ExecStart=/usr/bin/qemu-vmsr-helper +PrivateTmp=yes +ProtectSystem=strict +ReadWritePaths=/var/run +RestrictAddressFamilies=AF_UNIX +Restart=always +RestartSec=0 + +[Install]
contrib/systemd/qemu-vmsr-helper.socket new file +9 -0
@@ -0,0 +1,9 @@ +[Unit] +Description=Virtual RAPL MSR helper for QEMU + +[Socket] +ListenStream=/run/qemu-vmsr-helper.sock +SocketMode=0600 + +[Install] +WantedBy=multi-user.target
docs/tools/index.rst +1 -0
@@ -16,3 +16,4 @@ command line utilities and other standalone programs. qemu-pr-helper qemu-trace-stap virtfs-proxy-helper + qemu-vmsr-helper
docs/tools/qemu-vmsr-helper.rst new file +89 -0
89 lines not on this page · raw message
meson.build +5 -0
@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'), + dependencies: [authz, crypto, io, qom, qemuutil, + libcap_ng, mpathpersist], + install: true) endif if have_ivshmem
tools/i386/qemu-vmsr-helper.c new file +507 -0
507 lines not on this page · raw message
tools/i386/rapl-msr-index.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Allowed list of MSR for Privileged RAPL MSR helper commands for QEMU +* +* Copyright (C) 2023 Red Hat, Inc. <[email protected]> +* +* Author: Anthony Harivel <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; under version 2 of the License. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Should stay in sync with the RAPL MSR +* in target/i386/cpu.h +*/ +#define MSR_RAPL_POWER_UNIT 0x00000606 +#define MSR_PKG_POWER_LIMIT 0x00000610 +#define MSR_PKG_ENERGY_STATUS 0x00000611 +#define MSR_PKG_POWER_INFO 0x00000614
Signature · 1 line
2.43.0
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Quoted 64 lines · Anthony Harivel
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_RAWIOprivileges or relying on external, out-of-tree patches.The helper tool leverages Unix permissions and
SO_PEERCREDsocket options to enforce access control, ensuring that only processes explicitly requesting read access viareadmsr()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]>--- contrib/systemd/qemu-vmsr-helper.service | 15 + contrib/systemd/qemu-vmsr-helper.socket | 9 + docs/tools/index.rst | 1 + docs/tools/qemu-vmsr-helper.rst | 89 ++++ meson.build | 5 + tools/i386/qemu-vmsr-helper.c | 507 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 7 files changed, 654 insertions(+) create mode 100644 contrib/systemd/qemu-vmsr-helper.service create mode 100644 contrib/systemd/qemu-vmsr-helper.socket create mode 100644 docs/tools/qemu-vmsr-helper.rst create mode 100644 tools/i386/qemu-vmsr-helper.c create mode 100644 tools/i386/rapl-msr-index.hcontrib/systemd/qemu-vmsr-helper.service new file +15
-0@@ -0,0 +1,15 @@ +[Unit] +Description=Virtual RAPL MSR Daemon for QEMU + +[Service] +WorkingDirectory=/tmp +Type=simple +ExecStart=/usr/bin/qemu-vmsr-helper +PrivateTmp=yes +ProtectSystem=strict +ReadWritePaths=/var/run +RestrictAddressFamilies=AF_UNIX +Restart=always +RestartSec=0 + +[Install]contrib/systemd/qemu-vmsr-helper.socket new file +0
-0
contrib/systemd/qemu-vmsr-helper.socket line 6
Quoted 6 lines
contrib/systemd/qemu-vmsr-helper.socket new file +6
-0@@ -0,0 +1,9 @@ +[Unit] +Description=Virtual RAPL MSR helper for QEMU + +[Socket] +ListenStream=/run/qemu-vmsr-helper.sock +SocketMode=0600
This mode means that only root can connect to it, which seems to defeat the purpose of allowing QEMU to run as non-root surely ?
meson.build if have_tools · line 4019
Quoted 10 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),
I'd suggest 'tools/x86/' since this works fine on 64-bit too
meson.build if have_tools · line 4022
Quoted 3 lines
meson.build +3
-0+ dependencies: [authz, crypto, io, qom, qemuutil, + libcap_ng, mpathpersist], + install: true)
Shouldn't this executable() call be conditional though, so this is only built for x86 host targets.
tools/i386/qemu-vmsr-helper.c line 50
Quoted 12 lines
meson.build +0
-0endif if have_ivshmemtools/i386/qemu-vmsr-helper.c new file +2
-0+ +#define MAX_PATH_LEN 256
Drop this constant as it is redundant
Quoted 9 lines
tools/i386/qemu-vmsr-helper.c new file +9
-0+#define MSR_PATH_TEMPLATE "/dev/cpu/%u/msr" + +static char *socket_path; +static char *pidfile; +static enum {RUNNING ,TERMINATE ,TERMINATING } state; +static QIOChannelSocket *server_ioc; +static int server_watch; +static int num_active_sockets =1 ; +
tools/i386/qemu-vmsr-helper.c line 69
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+#ifdef CONFIG_LIBCAP_NG +static int uid =-1 ; +static int gid =-1 ; +#endif + +static void compute_default_paths (void ) +{ + socket_path =g_build_filename ("/run" ,"qemu-vmsr-helper.sock" ,NULL ); + pidfile =g_build_filename ("/run" ,"qemu-vmsr-helper.pid" ,NULL ); +}
We shouldn't be hardcoding /run, we need to honour --prefix and --localstatedir args given to configure. /var/run is a symlink to /run so the end result ends up the same AFAIK
Quoted 4 lines
tools/i386/qemu-vmsr-helper.c new file +4
-0+ +/* +* Check if the TID that request the MSR read +* belongs to the peer. It be should a TID of a vCPU.
tools/i386/qemu-vmsr-helper.c line 83
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+*/ +static bool is_tid_present (pid_t pid,pid_t tid) +{ + g_autofree char *pidStr; + g_autofree char *tidStr; + + pidStr =g_strdup_printf ("%d" , pid); + tidStr =g_strdup_printf ("%d" , tid); + +char *tidPath;
This needs to be g_autofree too, and as a rule any variable declared with 'g_autofree' should always be initialized at the same time as declaration, so put the g_strdup_printf calls on the same line.
Quoted 25 lines
tools/i386/qemu-vmsr-helper.c new file +24
-0+ + tidPath =g_strdup_printf ("/proc/%s/task/%s" , pidStr, tidStr); + +/* Check if the TID directory exists within the PID directory */ +if (access (tidPath,F_OK ) ==0 ) { +return true; + } +error_report ("Failed to open /proc at %s" , tidPath); +return false; +} + +/* +* Only the RAPL MSR in target/i386/cpu.h are allowed +*/ +static bool is_msr_allowed (uint32_t reg) +{ +switch (reg) { +case MSR_RAPL_POWER_UNIT : +case MSR_PKG_POWER_LIMIT : +case MSR_PKG_ENERGY_STATUS : +case MSR_PKG_POWER_INFO : +return true; +default : +return false;
tools/i386/qemu-vmsr-helper.c line 117
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+ } +} + +static uint64_t vmsr_read_msr (uint32_t msr_register,unsigned int cpu_id) +{ +int fd; +uint64_t result =0 ; + + g_autofree char *path; + path =g_new0 (char,MAX_PATH_LEN );
This is a memory leak as it is overwritten on the next line.
Quoted 1 line
+ path =
g_strdup_printf(MSR_PATH_TEMPLATE, cpu_id);
Since this constant is only used in one place, I'd prefer to see it inline here, so an observer can see what format specifiers are relied upon
tools/i386/qemu-vmsr-helper.c line 125
Quoted 7 lines
tools/i386/qemu-vmsr-helper.c new file +7
-0+ +/* +* Check if the specified CPU is included in the thread's affinity +*/ +cpu_set_t cpu_set; +CPU_ZERO (&cpu_set); +sched_getaffinity (0 ,sizeof (cpu_set_t), &cpu_set);
Using cpu_set_t will break on machines with >= 1024 CPUs.
There are a numbr of painpoints with calling sched_getaffinity so rather than try to explain them, let me point you to the man page for sched_getaffinity underthe headeing:
"Handling systems with large CPU affinity masks"
tools/i386/qemu-vmsr-helper.c line 128
Quoted 3 lines
tools/i386/qemu-vmsr-helper.c new file +3
-0+ +if (!CPU_ISSET (cpu_id, &cpu_set)) { +fprintf (stderr,"CPU %u is not in the thread's affinity.\n" , cpu_id);
Use error_report() for consistency
tools/i386/qemu-vmsr-helper.c line 138
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+return result; + } + + fd =open (path,O_RDONLY ); +if (fd <0 ) { +error_report ("Failed to open MSR file at %s" , path); +return result; + } + +if (pread (fd, &result,sizeof (result), msr_register) !=sizeof (result)) {
Leaks 'fd'
Quoted 23 lines
tools/i386/qemu-vmsr-helper.c new file +21
-0+error_report ("Failed to read MSR" ); + result =0 ; + } + +close (fd); +return result; +} +static void coroutine_fn vh_co_entry(void *opaque) +{ +VMSRHelperClient *client = opaque; +uint64_t vmsr; +uint32_t request[3 ]; +unsigned int peer_pid; +int r; +Error *local_err =NULL ; + +qio_channel_set_blocking (QIO_CHANNEL (client->ioc), + false,NULL ); + +qio_channel_set_follow_coroutine_ctx (QIO_CHANNEL (client->ioc), true); +
tools/i386/qemu-vmsr-helper.c line 212
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+/* +* Check peer credentials +*/ +qio_channel_get_peerpid (QIO_CHANNEL (client->ioc), &peer_pid, &local_err); + +if (peer_pid ==0 ) { +if (local_err !=NULL ) { +error_report_err (local_err); + } +error_report ("Failed to get peer credentials" );
Using peer_pid == 0 as a substitute for checking if 'local_err' is set is bogus. Check for 'local_err != NULL'.
tools/i386/qemu-vmsr-helper.c line 222
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+ goto out; + } + +/* +* Read the requested MSR +* Only RAPL MSR in rapl-msr-index.h is allowed +*/ + r =qio_channel_read_all (QIO_CHANNEL (client->ioc), + (char *) &request,sizeof (request), &local_err); +if ((local_err !=NULL ) || r <0 ) {
Checking both conditions is redundant, pick one
Quoted 4 lines
tools/i386/qemu-vmsr-helper.c new file +4
-0+error_report ("Read request fail" ); +error_report_err (local_err); + goto out; + }
tools/i386/qemu-vmsr-helper.c line 236
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+if (!is_msr_allowed (request[0 ])) { +error_report ("Requested unallowed msr: %d" , request[0 ]); + goto out; + } + + vmsr =vmsr_read_msr (request[0 ], request[1 ]); + +if (!is_tid_present (peer_pid, request[2 ])) { +error_report ("Requested TID not in peer PID: %d %d" , + peer_pid, request[2 ]);
Indent is a little off.
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
tools/i386/qemu-vmsr-helper.c line 241
Quoted 5 lines
tools/i386/qemu-vmsr-helper.c new file +5
-0+ vmsr =0 ; + } + + r =qio_channel_write_all (QIO_CHANNEL (client->ioc), + (char *) &vmsr,sizeof (vmsr), &local_err);
Indent got a little off here too.
Quoted 1 line
+ if ((
local_err!= NULL) || r < 0) {
Again redundant checks
Quoted 59 lines
tools/i386/qemu-vmsr-helper.c new file +59
-0+error_report ("Write request fail" ); +error_report_err (local_err); + goto out; + } + +out: +object_unref (OBJECT (client->ioc)); +g_free (client); +} + +static gboolean accept_client (QIOChannel *ioc, +GIOCondition cond, +gpointer opaque) +{ +QIOChannelSocket *cioc; +VMSRHelperClient *vmsrh; + + cioc =qio_channel_socket_accept (QIO_CHANNEL_SOCKET (ioc), +NULL ); +if (!cioc) { +return TRUE; + } + + vmsrh =g_new (VMSRHelperClient,1 ); + vmsrh->ioc = cioc; + vmsrh->co =qemu_coroutine_create (vh_co_entry, vmsrh); +qemu_coroutine_enter (vmsrh->co); + +return TRUE; +} + +static void termsig_handler (int signum) +{ +qatomic_cmpxchg (&state,RUNNING ,TERMINATE ); +qemu_notify_event (); +} + +static void close_server_socket (void ) +{ +assert (server_ioc); + +g_source_remove (server_watch); + server_watch =-1 ; +object_unref (OBJECT (server_ioc)); + num_active_sockets--; +} + +#ifdef CONFIG_LIBCAP_NG +static int drop_privileges (void ) +{ +/* clear all capabilities */ +capng_clear (CAPNG_SELECT_BOTH ); + +if (capng_update (CAPNG_ADD ,CAPNG_EFFECTIVE |CAPNG_PERMITTED , +CAP_SYS_RAWIO ) <0 ) { +return -1 ; + } + +/*
tools/i386/qemu-vmsr-helper.c line 311
Quoted 10 lines
tools/i386/qemu-vmsr-helper.c new file +10
-0+* Change user/group id, retaining the capabilities. +* Because file descriptors are passed via SCM_RIGHTS, +* we don't need supplementary groups (and in fact the helper +* can run as "nobody"). +*/ +if (capng_change_id (uid !=-1 ? uid :getuid (), + gid !=-1 ? gid :getgid (), +CAPNG_DROP_SUPP_GRP |CAPNG_CLEAR_BOUNDING )) { +return -1 ; + }
Does this actually work ? IIUC, the file that requires privileges is /dev/cpu/%u/msr, and we're opening that fresh on every request, so how can this run as anything other than root ?
Quoted 233 lines
tools/i386/qemu-vmsr-helper.c new file +196
-0+ +return 0 ; +} +#endif + +int main (int argc,char **argv) +{ +const char *sopt ="hVk:f:dT:u:g:vq" ; +struct option lopt[] = { + {"help" , no_argument,NULL ,'h' }, + {"version" , no_argument,NULL ,'V' }, + {"socket" , required_argument,NULL ,'k' }, + {"pidfile" , required_argument,NULL ,'f' }, + {"daemon" , no_argument,NULL ,'d' }, + {"trace" , required_argument,NULL ,'T' }, + {"user" , required_argument,NULL ,'u' }, + {"group" , required_argument,NULL ,'g' }, + {"verbose" , no_argument,NULL ,'v' }, + {NULL ,0 ,NULL ,0 } + }; +int opt_ind =0 ; +int ch; +Error *local_err =NULL ; +bool daemonize = false; +bool pidfile_specified = false; +bool socket_path_specified = false; +unsigned socket_activation; + +struct sigaction sa_sigterm; +memset (&sa_sigterm,0 ,sizeof (sa_sigterm)); + sa_sigterm.sa_handler = termsig_handler; +sigaction (SIGTERM , &sa_sigterm,NULL ); +sigaction (SIGINT , &sa_sigterm,NULL ); +sigaction (SIGHUP , &sa_sigterm,NULL ); + +signal (SIGPIPE ,SIG_IGN ); + +error_init (argv[0 ]); +module_call_init (MODULE_INIT_TRACE ); +module_call_init (MODULE_INIT_QOM ); +qemu_add_opts (&qemu_trace_opts); +qemu_init_exec_dir (argv[0 ]); + +compute_default_paths (); + +while ((ch =getopt_long (argc, argv, sopt, lopt, &opt_ind)) !=-1 ) { +switch (ch) { +case 'k' : +g_free (socket_path); + socket_path =g_strdup (optarg); + socket_path_specified = true; +if (socket_path[0 ] !='/' ) { +error_report ("socket path must be absolute" ); +exit (EXIT_FAILURE ); + } +break ; +case 'f' : +g_free (pidfile); + pidfile =g_strdup (optarg); + pidfile_specified = true; +break ; +#ifdef CONFIG_LIBCAP_NG +case 'u' : { +unsigned long res; +struct passwd *userinfo =getpwnam (optarg); +if (userinfo) { + uid = userinfo->pw_uid; + }else if (qemu_strtoul (optarg,NULL ,10 , &res) ==0 && + (uid_t )res == res) { + uid = res; + }else { +error_report ("invalid user '%s'" , optarg); +exit (EXIT_FAILURE ); + } +break ; + } +case 'g' : { +unsigned long res; +struct group *groupinfo =getgrnam (optarg); +if (groupinfo) { + gid = groupinfo->gr_gid; + }else if (qemu_strtoul (optarg,NULL ,10 , &res) ==0 && + (gid_t )res == res) { + gid = res; + }else { +error_report ("invalid group '%s'" , optarg); +exit (EXIT_FAILURE ); + } +break ; + } +#else +case 'u' : +case 'g' : +error_report ("-%c not supported by this %s" , ch, argv[0 ]); +exit (1 ); +#endif +case 'd' : + daemonize = true; +break ; +case 'T' : +trace_opt_parse (optarg); +break ; +case 'V' : +version (argv[0 ]); +exit (EXIT_SUCCESS ); +break ; +case 'h' : +usage (argv[0 ]); +exit (EXIT_SUCCESS ); +break ; +case '?' : +error_report ("Try `%s --help' for more information." , argv[0 ]); +exit (EXIT_FAILURE ); + } + } + +if (!trace_init_backends ()) { +exit (EXIT_FAILURE ); + } +trace_init_file (); +qemu_set_log (LOG_TRACE , &error_fatal); + + socket_activation =check_socket_activation (); +if (socket_activation ==0 ) { +SocketAddress saddr; + saddr = (SocketAddress ){ + .type =SOCKET_ADDRESS_TYPE_UNIX , + .u.q_unix.path = socket_path, + }; + server_ioc =qio_channel_socket_new (); +if (qio_channel_socket_listen_sync (server_ioc, &saddr, +1 , &local_err) <0 ) { +object_unref (OBJECT (server_ioc)); +error_report_err (local_err); +return 1 ; + } + }else { +/* Using socket activation - check user didn't use -p etc. */ +if (socket_path_specified) { +error_report ("Unix socket can't be set when \ +using socket activation" ); +exit (EXIT_FAILURE ); + } + +/* Can only listen on a single socket. */ +if (socket_activation >1 ) { +error_report ("%s does not support socket activation \ +with LISTEN_FDS > 1" , + argv[0 ]); +exit (EXIT_FAILURE ); + } + server_ioc =qio_channel_socket_new_fd (FIRST_SOCKET_ACTIVATION_FD , + &local_err); +if (server_ioc ==NULL ) { +error_reportf_err (local_err, +"Failed to use socket activation: " ); +exit (EXIT_FAILURE ); + } + } + +qemu_init_main_loop (&error_fatal); + + server_watch =qio_channel_add_watch (QIO_CHANNEL (server_ioc), +G_IO_IN , + accept_client, +NULL ,NULL ); + +if (daemonize) { +if (daemon (0 ,0 ) <0 ) { +error_report ("Failed to daemonize: %s" ,strerror (errno)); +exit (EXIT_FAILURE ); + } + } + +if (daemonize || pidfile_specified) { +qemu_write_pidfile (pidfile, &error_fatal); + } + +#ifdef CONFIG_LIBCAP_NG +if (drop_privileges () <0 ) { +error_report ("Failed to drop privileges: %s" ,strerror (errno)); +exit (EXIT_FAILURE ); + } +#endif + + state =RUNNING ; +do { +main_loop_wait (false); +if (state ==TERMINATE ) { + state =TERMINATING ; +close_server_socket (); + } + }while (num_active_sockets >0 ); + +exit (EXIT_SUCCESS ); +}tools/i386/rapl-msr-index.h new file +28
-0@@ -0,0 +1,28 @@ +/* +* Allowed list of MSR for Privileged RAPL MSR helper commands for QEMU +* +* Copyright (C) 2023 Red Hat, Inc. <[email protected]> +* +* Author: Anthony Harivel <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; under version 2 of the License. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Should stay in sync with the RAPL MSR +* in target/i386/cpu.h +*/ +#define MSR_RAPL_POWER_UNIT 0x00000606 +#define MSR_PKG_POWER_LIMIT 0x00000610 +#define MSR_PKG_ENERGY_STATUS 0x00000611 +#define MSR_PKG_POWER_INFO 0x00000614Signature · 1 line
2.43.0
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Quoted 4 lines · Daniel P. Berrangé
Quoted 4 lines
meson.build +0
-0
Quoted 8 lines
meson.build if have_tools · line 4019
Quoted 6 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),I'd suggest '
tools/x86/' since this works fine on 64-bit too
QEMU tends to use i386 in the source to mean both 32- and 64-bit. Either is fine by me though.
Quoted 6 lines
meson.build if have_tools · line 4022
Quoted 3 lines
meson.build +3
-0+ dependencies: [authz, crypto, io, qom, qemuutil, + libcap_ng, mpathpersist], + install: true)Shouldn't this
executable()call be conditional though, so this is only built for x86 host targets.
Yes. Also should be 32- and 64-bit (careful because Meson uses 'x86' for 32-bit).
Quoted 9 lines
meson.build if have_tools
Quoted 5 lines
meson.build +5
-0+static void compute_default_paths(void) +{ + socket_path = g_build_filename("/run", "qemu-vmsr-helper.sock", NULL); + pidfile = g_build_filename("/run", "qemu-vmsr-helper.pid", NULL); +}We shouldn't be hardcoding /run, we need to honour --prefix and --localstatedir args given to configure.
/var/runis a symlink to /run so the end result ends up the same AFAIK
Indeed; just copy from scsi/qemu-pr-helper.c.
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via SCM_RIGHTS by libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.
Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
Quoted 17 lines
Quoted 6 lines
meson.build +6
-0+ if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED, + CAP_SYS_RAWIO) < 0) { + return -1; + } + + /*meson.build
Quoted 10 lines
meson.build +10
-0+ * Change user/group id, retaining the capabilities. + * Because file descriptors are passed via SCM_RIGHTS, + * we don't need supplementary groups (and in fact the helper + * can run as "nobody"). + */ + if (capng_change_id(uid != -1 ? uid : getuid(), + gid != -1 ? gid : getgid(), + CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING)) { + return -1; + }
Quoted 3 lines
Does this actually work ? IIUC, the file that requires privileges is /dev/cpu/%u/msr, and we're opening that fresh on every request, so how can this run as anything other than root ?
Agreed, the capabilities can be dropped but the uid and gid cannot.
Paolo
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Quoted 14 lines · Paolo Bonzini
Quoted 4 lines · Daniel P. Berrangé
Quoted 4 lines
meson.build +0
-0Quoted 8 lines
meson.build if have_tools · line 4019
Quoted 6 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),I'd suggest '
tools/x86/' since this works fine on 64-bit too
Quoted 1 line
QEMU tends to use i386 in the source to mean both 32- and 64-bit.
One day we should rename that to x86 too :-)
Quoted 12 lines
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Quoted 8 lines
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via
SCM_RIGHTSby libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
Ok, this code needs a comment to explain that we're relying on socket permissions to control who/what can access the daemon, combined with this PID+TID check to validate it is not spoofing its identity, as without context the TID check looks pointless.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Quoted 37 lines · Daniel P. Berrangé
Quoted 14 lines · Paolo Bonzini
Quoted 4 lines · Daniel P. Berrangé
Quoted 4 lines
meson.build +0
-0Quoted 8 lines
meson.build if have_tools · line 4019
Quoted 6 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),I'd suggest '
tools/x86/' since this works fine on 64-bit tooQuoted 1 line
QEMU tends to use i386 in the source to mean both 32- and 64-bit.
One day we should rename that to x86 too :-)
Quoted 18 lines
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via
SCM_RIGHTSby libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.
Quoted 7 lines
Quoted 2 lines
Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
Ok, this code needs a comment to explain that we're relying on socket permissions to control who/what can access the daemon, combined with this PID+TID check to validate it is not spoofing its identity, as without context the TID check looks pointless.
Looking again, the TID is never used after being checked. QEMU sends the TID, but the helper never does anything with this information except to check the TID belongs the PID. Why are we sending the TID ?
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Quoted 3 lines · Daniel P. Berrangé
Looking again, the TID is never used after being checked. QEMU sends the TID, but the helper never does anything with this information except to check the TID belongs the PID. Why are we sending the TID ?
Doh! sched_getaffinity's first argument should be the tid. (As an aside, sched_getaffinity does not need CAP_SYS_NICE to read the affinity of another user's thread).
Paolo
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
In reply to Daniel P. BerrangéDaniel P. Berrangé, Jan 29, 2024 at 20:45:
Quoted 37 lines · Paolo Bonzini
Quoted 14 lines · Daniel P. Berrangé
Quoted 4 lines
Quoted 4 lines
meson.build +0
-0Quoted 8 lines
meson.build if have_tools · line 4019
Quoted 6 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),I'd suggest '
tools/x86/' since this works fine on 64-bit tooQuoted 1 line
QEMU tends to use i386 in the source to mean both 32- and 64-bit.
One day we should rename that to x86 too :-)
Quoted 18 lines
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via
SCM_RIGHTSby libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.
Quoted 7 lines
Quoted 2 lines
Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
Ok, this code needs a comment to explain that we're relying on socket permissions to control who/what can access the daemon, combined with this PID+TID check to validate it is not spoofing its identity, as without context the TID check looks pointless.
Hi Daniel,
would you prefer a comment in the code or a security section in the doc (i.e docs/specs/rapl-msr.rst) ?
Regards,
Anthony
Quoted 8 lines
With regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Quoted 46 lines · Anthony Harivel
Daniel P. Berrangé, Jan 29, 2024 at 20:45:
Quoted 37 lines · Paolo Bonzini
Quoted 14 lines · Daniel P. Berrangé
Quoted 4 lines
Quoted 4 lines
meson.build +0
-0Quoted 8 lines
meson.build if have_tools · line 4019
Quoted 6 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),I'd suggest '
tools/x86/' since this works fine on 64-bit tooQuoted 1 line
QEMU tends to use i386 in the source to mean both 32- and 64-bit.
One day we should rename that to x86 too :-)
Quoted 18 lines
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via
SCM_RIGHTSby libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.Quoted 7 lines
Quoted 2 lines
Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
Ok, this code needs a comment to explain that we're relying on socket permissions to control who/what can access the daemon, combined with this PID+TID check to validate it is not spoofing its identity, as without context the TID check looks pointless.
Quoted 4 lines
Hi Daniel,
would you prefer a comment in the code or a security section in the doc (i.e
docs/specs/rapl-msr.rst) ?
I think it is worth creating a docs/specs/rapl-msr.rst to explain the overall design & usage & security considerations.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
Daniel P. Berrangé, Feb 21, 2024 at 14:47:
Quoted 47 lines · Anthony Harivel
Quoted 46 lines
Daniel P. Berrangé, Jan 29, 2024 at 20:45:
Quoted 44 lines · Paolo Bonzini
Quoted 14 lines · Daniel P. Berrangé
Quoted 4 lines
Quoted 4 lines
meson.build +0
-0Quoted 8 lines
meson.build if have_tools · line 4019
Quoted 6 lines
meson.build +2
-0@@ -4015,6 +4015,11 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'),I'd suggest '
tools/x86/' since this works fine on 64-bit tooQuoted 1 line
QEMU tends to use i386 in the source to mean both 32- and 64-bit.
One day we should rename that to x86 too :-)
Quoted 12 lines
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Quoted 8 lines
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via
SCM_RIGHTSby libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
Ok, this code needs a comment to explain that we're relying on socket permissions to control who/what can access the daemon, combined with this PID+TID check to validate it is not spoofing its identity, as without context the TID check looks pointless.
Quoted 7 lines
Quoted 4 lines
Hi Daniel,
would you prefer a comment in the code or a security section in the doc (i.e
docs/specs/rapl-msr.rst) ?I think it is worth creating a
docs/specs/rapl-msr.rstto explain the overall design & usage & security considerations.
It was already included in the add-support-for-RAPL-MSRs-in-KVM-Qemu.patch but indeed it needs now some updates for the v4 about security and change in design.
Regards,
Anthony
Quoted 7 lines
With regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/3] tools: build qemu-vmsr-helper
In reply to Paolo BonziniHi Paolo,
Quoted 10 lines
Quoted 9 lines
tools/i386/qemu-vmsr-helper.c line 69
Quoted 5 lines
tools/i386/qemu-vmsr-helper.c new file +5
-0+static void compute_default_paths (void ) +{ + socket_path =g_build_filename ("/run" ,"qemu-vmsr-helper.sock" ,NULL ); + pidfile =g_build_filename ("/run" ,"qemu-vmsr-helper.pid" ,NULL ); +}We shouldn't be hardcoding /run, we need to honour --prefix and --localstatedir args given to configure.
/var/runis a symlink to /run so the end result ends up the same AFAIK
Quoted 2 lines
Indeed; just copy from
scsi/qemu-pr-helper.c.
When I copy the same as compute-default() of scsi/qemu-pr-helper.c, the helper is listening to "/var/local/run/qemu-vmsr-helper.sock".
Problem is /var/local/run is 700 while /run is 755.
So I would need to adjust the qemu-vmsr-helper.service to give the --socket=PATH of qemu-vmsr-helper.socket (i.e
/run/qemu-vmsr-helper.sock)
Problem is when I do that , I fall into the case:
"Unix socket can't be set when using socket activation"
So I'm a bit confused what to do about that.
Quoted 12 lines
Quoted 11 lines
You never answered my question from the previous posting of this
This check is merely validating the the thread ID in the message is a child of the process ID connected to the socket. Any process on the entire host can satisfy this requirement.
I don't see what is limiting this to only QEMU as claimed by the commit message, unless you're expecting the UNIX socket permissions to be such that only processes under the qemu:qemu user:group pair can access to the socket ? That would be a libvirt based permissions assumption though.
Quoted 9 lines
Yes, this is why the systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. The socket can be passed via
SCM_RIGHTSby libvirt, or its permissions can be changed (e.g. 660 and root:kvm would make sense on a Debian system), or a separate helper can be started by libvirt.Either way, the policy is left to the user rather than embedding it in the provided systemd unit.
During this patch test, when I run by hand my VM (without libvirt), the vmsr helper systemd service/socket was like that:
[Service]
ExecStart=/usr/bin/qemu-vmsr-helper
User=root
Group=root
and
[Socket]
ListenStream=/run/qemu-vmsr-helper.sock
SocketUser=qemu
SocketGroup=qemu
SocketMode=0660
And it seems to works. So I'm not sure 100% what to do in my patch.
Should I follow the pr-helper systemd files anyway ?
Regards,
Anthony
PATCH v3 3/3 Add support for RAPL MSRs in KVM/Qemu
In reply to Anthony HarivelStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like MSR_PKG_ENERGY_STATUS for the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.
For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Actual limitation:
- Works only on Intel host CPU because AMD CPUs are using different MSR adresses.
- Only the Package Power-Plane (MSR_PKG_ENERGY_STATUS) is reported at the moment.
--- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 133 +++++++++++++ include/sysemu/kvm_int.h | 17 ++ target/i386/cpu.h | 8 + target/i386/kvm/kvm.c | 348 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 295 ++++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 87 +++++++++ 9 files changed, 917 insertions(+) create mode 100644 docs/specs/rapl-msr.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.h
accel/kvm/kvm-all.c +27 -0
@@ -3603,6 +3603,21 @@ static void kvm_set_device(Object *obj, s->device =g_strdup (value); } +static void kvm_set_kvm_rapl (Object *obj,bool value,Error **errp) +{ +KVMState *s =KVM_STATE (obj); + s->msr_energy.enable = value; +} + +static void kvm_set_kvm_rapl_socket_path (Object *obj, +const char *str, +Error **errp) +{ +KVMState *s =KVM_STATE (obj); +g_free (s->msr_energy.socket_path); + s->msr_energy.socket_path =g_strdup (str); +} +static void kvm_accel_instance_init (Object *obj) {KVMState *s =KVM_STATE (obj);
@@ -3622,6 +3637,7 @@ static void kvm_accel_instance_init(Object *obj) s->xen_gnttab_max_frames =64 ; s->xen_evtchn_max_pirq =256 ; s->device =NULL ; + s->msr_energy.enable = false; }/**
@@ -3666,6 +3682,17 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data)object_class_property_set_description (oc,"device" ,"Path to the device node to use (default: /dev/kvm)" ); +object_class_property_add_bool (oc,"rapl" , +NULL , + kvm_set_kvm_rapl); +object_class_property_set_description (oc,"rapl" , +"Allow energy related MSRs for RAPL interface in Guest" ); + +object_class_property_add_str (oc,"rapl-helper-socket" ,NULL , + kvm_set_kvm_rapl_socket_path); +object_class_property_set_description (oc,"rapl-helper-socket" , +"Socket Path for comminucating with the Virtual MSR helper daemon" ); +kvm_arch_accel_class_init (oc); }
docs/specs/index.rst +1 -0
@@ -32,3 +32,4 @@ guest hardware that is specific to QEMU. virt-ctlr vmcoreinfo vmgenid + rapl-msr
docs/specs/rapl-msr.rst new file +133 -0
133 lines not on this page · raw message
include/sysemu/kvm_int.h +17 -0
@@ -14,6 +14,8 @@#include "qemu/accel.h" #include "qemu/queue.h" #include "sysemu/kvm.h" +#include "hw/boards.h" +#include "hw/i386/topology.h" typedef struct KVMSlot {
@@ -48,6 +50,20 @@ typedef struct KVMMemoryListener {#define KVM_MSI_HASHTAB_SIZE 256 +struct KVMMsrEnergy { +bool enable; +char *socket_path; +QemuThread msr_thr; +unsigned int cpus; +unsigned int sockets; +X86CPUTopoInfo topo_info; +const CPUArchIdList *cpu_list; +uint64_t *msr_value; +uint64_t msr_unit; +uint64_t msr_limit; +uint64_t msr_info; +}; +enum KVMDirtyRingReaperState {KVM_DIRTY_RING_REAPER_NONE =0 ,/* The reaper is sleeping */
@@ -114,6 +130,7 @@ struct KVMStatebool kvm_dirty_ring_with_bitmap;uint64_t kvm_eager_split_size;/* Eager Page Splitting chunk size */ struct KVMDirtyRingReaper reaper; +struct KVMMsrEnergy msr_energy;NotifyVmexitOption notify_vmexit;uint32_t notify_window;uint32_t xen_version;
target/i386/cpu.h +8 -0
@@ -396,6 +396,10 @@ typedef enum X86Seg {#define MSR_IA32_TSX_CTRL 0x122#define MSR_IA32_TSCDEADLINE 0x6e0#define MSR_IA32_PKRS 0x6e1 +#define MSR_RAPL_POWER_UNIT 0x00000606 +#define MSR_PKG_POWER_LIMIT 0x00000610 +#define MSR_PKG_ENERGY_STATUS 0x00000611 +#define MSR_PKG_POWER_INFO 0x00000614#define MSR_ARCH_LBR_CTL 0x000014ce#define MSR_ARCH_LBR_DEPTH 0x000014cf#define MSR_ARCH_LBR_FROM_0 0x00001500
@@ -1788,6 +1792,10 @@ typedef struct CPUArchState {uintptr_t retaddr; +/* RAPL MSR */ +uint64_t msr_rapl_power_unit; +uint64_t msr_pkg_energy_status; +/* Fields up to this point are cleared by a CPU reset */ struct {} end_reset_fields;
target/i386/kvm/kvm.c +348 -0
382 lines not on this page · raw message
target/i386/kvm/meson.build +1 -0
@@ -3,6 +3,7 @@ i386_kvm_ss = ss.source_set() i386_kvm_ss.add(files( 'kvm.c', 'kvm-cpu.c', + 'vmsr_energy.c', )) i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c'))
target/i386/kvm/vmsr_energy.c new file +295 -0
295 lines not on this page · raw message
target/i386/kvm/vmsr_energy.h new file +87 -0
87 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 6 lines · Anthony Harivel
docs/specs/rapl-msr.rst new file +0
-0
docs/specs/rapl-msr.rst line 124
Quoted 10 lines
docs/specs/rapl-msr.rst new file +9
-0@@ -0,0 +1,133 @@ +================ +RAPL MSR support +================ + +Current Limitations +------------------- + +- Works only on Intel host CPUs because AMD CPUs are using different MSR + addresses.
The privileged helper program is validating an allow list of MSRs.
If those MSRs are only correct on Intel hosts, then the validation is incomplete, and it could be allowing unprivileged processes on AMD hosts to access forbidden MSRS whose address happen to clash with the Intel RAPL MSRs.
IOW, the privileged helper needs to call cpuid() and validate that the current host vendor is Intel.
I suspect we also need a feature check of some kind to validate that the intel processor supports this features, since old ones definitely didn't, and we shouldn't assume all future ones will either.
Quoted 9 lines
include/sysemu/kvm_int.h +0
-0target/i386/kvm/kvm.c +0
-0
target/i386/kvm/kvm.c line 20
Quoted 5 lines
target/i386/kvm/kvm.c +2
-0@@ -16,16 +16,22 @@#include "qapi/qapi-events-run-state.h" #include "qapi/error.h" #include "qapi/visitor.h" +#include <math.h> +#include <stdint.h>
stdint.h always gets included via osdep.h
target/i386/kvm/kvm.c line 28
Quoted 8 lines
target/i386/kvm/kvm.c +3
-0#include <sys/ioctl.h> #include <sys/utsname.h> #include <sys/syscall.h> +#include <sys/resource.h> +#include <sys/time.h> #include <linux/kvm.h> +#include <unistd.h>
unistd.h also always gets included via osdep.h
Quoted 44 lines
target/i386/kvm/kvm.c +31
-0#include "standard-headers/asm-x86/kvm_para.h" #include "hw/xen/interface/arch-x86/cpuid.h" #include "cpu.h" #include "host-cpu.h" +#include "vmsr_energy.h" #include "sysemu/sysemu.h" #include "sysemu/hw_accel.h" #include "sysemu/kvm_int.h" @@ -2509,6 +2558,265 @@ static void register_smram_listener(Notifier *n, void *unused) &smram_address_space, 1, "kvm-smram"); } +static void *kvm_msr_energy_thread (void *data) +{ +KVMState *s = data; +struct KVMMsrEnergy *vmsr = &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat = NULL; + g_autofree thread_stat *thd_stat = NULL; + g_autofree pid_t *thread_ids = NULL; + g_autofree CPUState *cpu = NULL; +unsigned int maxpkgs, maxcpus, maxticks; + g_autofree unsigned int *vpkgs_energy_stat = NULL; +unsigned int num_threads =0 ; +unsigned int tmp_num_threads =0 ; +pid_t pid; + +X86CPUTopoIDs topo_ids; + + +rcu_register_thread (); + +/* Get QEMU PID*/ + pid =getpid (); + +/* Nb of CPUS per packages */ + maxcpus =vmsr_get_maxcpus (0 ); + +/* Nb of Physical Packages on the system */ + maxpkgs =vmsr_get_max_physical_package (maxcpus); + +/* Those MSR values should not change as well */
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2600
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ vmsr->msr_unit =vmsr_read_msr (MSR_RAPL_POWER_UNIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_limit =vmsr_read_msr (MSR_PKG_POWER_LIMIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_info =vmsr_read_msr (MSR_PKG_POWER_INFO ,0 , pid, + s->msr_energy.socket_path); + +/* Allocate memory for each package energy status */ + pkg_stat = (package_energy_stat *) +g_new0 (package_energy_stat, maxpkgs);
Return value of g_new0 shouldn't need casting.
Quoted 35 lines
target/i386/kvm/kvm.c +35
-0+ +/* Pre-allocate memory for thread stats */ + thd_stat =g_new0 (thread_stat,1 ); + +/* Pre-allocate memory for holding Virtual Package Energy counter */ + vpkgs_energy_stat = g_new0(unsigned int, vmsr->sockets); + +/* +* Max numbers of ticks per package +* time in second * number of ticks/second * Number of cores / package +* ex: for 100 ticks/second/CPU, 12 CPUs per Package gives 1200 ticks max +*/ + maxticks = (MSR_ENERGY_THREAD_SLEEP_US /1000000 ) + *sysconf (_SC_CLK_TCK) * maxcpus; + +while (true) { +/* Get all qemu threads id */ + thread_ids =vmsr_get_thread_ids (pid, &num_threads); + +if (thread_ids ==NULL ) { + goto clean; + } + +if (tmp_num_threads < num_threads) { + thd_stat =g_renew (thread_stat, thd_stat, num_threads); + } + + tmp_num_threads = num_threads; + +/* Populate all the thread stats */ +for (int i =0 ; i < num_threads; i++) { + thd_stat[i].utime = g_new0(unsigned long long, 2); + thd_stat[i].stime = g_new0(unsigned long long, 2); + thd_stat[i].thread_id = thread_ids[i]; +vmsr_read_thread_stat (&thd_stat[i], pid,0 );
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2645
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ thd_stat[i].numa_node_id =numa_node_of_cpu (thd_stat[i].cpu_id); + } + +/* Retrieve all packages power plane energy counter */ +for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/
This says we're using a thread ID
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2648
Quoted 3 lines
target/i386/kvm/kvm.c +3
-0+if (thd_stat[j].numa_node_id == i) { + pkg_stat[i].e_start = + vmsr_read_msr(MSR_PKG_ENERGY_STATUS, i, pid,
but here we're using a pid ID, which is the thread ID of the initial thread.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2653
Quoted 5 lines
target/i386/kvm/kvm.c +5
-0+ s->msr_energy.socket_path); +break ; + } + } + }
This API design for vmsr_read_msr() is incredibly inefficient. We're making (maxpkgs * num_threads) calls to vmsr_read_msr(), and every one of those is opening and closing the socket.
Why isn't QEMU opening the socket once and then sending all the requests over the same socket ?
Quoted 8 lines
target/i386/kvm/kvm.c +8
-0+ +/* Sleep a short period while the other threads are working */ +usleep (MSR_ENERGY_THREAD_SLEEP_US ); + +/* +* Retrieve all packages power plane energy counter +* Calculate the delta of all packages +*/
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2671
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].numa_node_id == i) { + pkg_stat[i].e_end = + vmsr_read_msr(MSR_PKG_ENERGY_STATUS, + thd_stat[j].cpu_id,
This is passing the 'cpu_id' field, but in the previous call to vmsr_read_msr 20 lines earlier, we seemd to pass a value that was the NUMA node id.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2672
Quoted 1 line
target/i386/kvm/kvm.c +1
-0+ thd_stat[j].thread_id,
This is now passing a thread_id, while previously we passed the PID id.
Quoted 276 lines
target/i386/kvm/kvm.c +187
-0+ s->msr_energy.socket_path); +/* +* Prevent the case we have migrate the VM +* during the sleep period or any other cases +* were energy counter might be lower after +* the sleep. +*/ +if (pkg_stat[i].e_end > pkg_stat[i].e_start) { + pkg_stat[i].e_delta = + pkg_stat[i].e_end - pkg_stat[i].e_start; + }else { + pkg_stat[i].e_delta =0 ; + } +break ; + } + } + } + +/* Delta of ticks spend by each thread between the sample */ +for (int i =0 ; i < num_threads; i++) { +if (vmsr_read_thread_stat (&thd_stat[i], pid,1 ) !=0 ) { +/* +* We don't count the dead thread +* i.e threads that existed before the sleep +* and not anymore +*/ + thd_stat[i].delta_ticks =0 ; + }else { +vmsr_delta_ticks (thd_stat, i); + } + } + +/* +* Identify the vCPU threads +* Calculate the Number of vCPU per package +*/ +CPU_FOREACH (cpu) { +for (int i =0 ; i < num_threads; i++) { +if (cpu->thread_id == thd_stat[i].thread_id) { + thd_stat[i].is_vcpu = true; + thd_stat[i].vcpu_id = cpu->cpu_index; + pkg_stat[thd_stat[i].numa_node_id].nb_vcpu++; + thd_stat[i].acpi_id =kvm_arch_vcpu_id (cpu); +break ; + } + } + } + +/* Retrieve the virtual package number of each vCPU */ +for (int i =0 ; i < vmsr->x86_cpu_list->len; i++) { +for (int j =0 ; j < num_threads; j++) { +if ((thd_stat[j].acpi_id == vmsr->x86_cpu_list->cpus[i].arch_id) + && (thd_stat[j].is_vcpu == true)) { +x86_topo_ids_from_apicid (thd_stat[j].acpi_id, + &vmsr->topo_info, &topo_ids); + thd_stat[j].vpkg = topo_ids.pkg_id; + } + } + } + +/* Calculate the total energy of all non-vCPU thread */ +for (int i =0 ; i < num_threads; i++) { +double temp; +if ((thd_stat[i].is_vcpu != true) && + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat, thd_stat, maxticks, i); + pkg_stat[thd_stat[i].numa_node_id].e_ratio + += (uint64_t )lround (temp); + } + } + +/* Calculate the ratio per non-vCPU thread of each package */ +for (int i =0 ; i <= maxpkgs; i++) { +if (pkg_stat[i].nb_vcpu >0 ) { + pkg_stat[i].e_ratio = pkg_stat[i].e_ratio / pkg_stat[i].nb_vcpu; + } + } + +/* +* Calculate the energy for each Package: +* Energy Package = sum of each vCPU energy that belongs to the package +*/ +for (int i =0 ; i < num_threads; i++) { +double temp; + +if ((thd_stat[i].is_vcpu == true) && \ + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat, thd_stat, maxticks, i); + + vpkgs_energy_stat[thd_stat[i].vpkg] += (uint64_t )lround (temp); + vpkgs_energy_stat[thd_stat[i].vpkg] += + pkg_stat[thd_stat[i].numa_node_id].e_ratio; + } + } + +/* +* Finally populate the vmsr register of each vCPU with the total +* package value to emulate the real hardware where each CPU return the +* value of the package it belongs. +*/ +for (int i =0 ; i < num_threads; i++) { +if ((thd_stat[i].is_vcpu == true) && \ + (thd_stat[i].delta_ticks >0 )) { + vmsr->msr_value[thd_stat[i].vcpu_id] = \ + vpkgs_energy_stat[thd_stat[i].vpkg]; + } + } + +/* Zero out the memory */ +for (int i =0 ; i < num_threads; i++) { +memset (thd_stat[i].utime,0 ,2 *sizeof (unsigned long long )); +memset (thd_stat[i].stime,0 ,2 *sizeof (unsigned long long )); + } +memset (thd_stat,0 , num_threads *sizeof (thread_stat)); +memset (thread_ids,0 ,sizeof (pid_t)); + } + +clean: +rcu_unregister_thread (); +return NULL ; +} + +static int kvm_msr_energy_thread_init (KVMState *s,MachineState *ms) +{ +struct KVMMsrEnergy *r = &s->msr_energy; + +vmsr_init_topo_info (&r->topo_info, ms); + +/* Retrieve the number of vCPU */ + r->cpus = ms->smp.cpus; + +/* Retrieve the number of sockets */ + r->sockets = ms->smp.sockets; + +/* Allocate register memory (MSR_PKG_STATUS) for each vCPU */ + r->msr_value =g_new0 (uint64_t, r->cpus); + +/* Retrieve the CPUArchIDlist */ + r->x86_cpu_list =x86_possible_cpu_arch_ids (ms); + +qemu_thread_create (&r->msr_thr,"kvm-msr" , + kvm_msr_energy_thread, + s,QEMU_THREAD_JOINABLE ); + +return 0 ; +} +int kvm_arch_get_default_type (MachineState *ms) {return 0 ;@@ -2711,6 +3019,46 @@ int kvm_arch_init(MachineState *ms, KVMState *s) strerror(-ret));exit (1 ); } + +if (s->msr_energy.enable == true) { + + r =kvm_filter_msr (s,MSR_RAPL_POWER_UNIT , + kvm_rdmsr_rapl_power_unit,NULL ); +if (!r) { +error_report ("Could not install MSR_RAPL_POWER_UNIT \ +handler: %s" , +strerror (-ret)); +exit (1 ); + } + + r =kvm_filter_msr (s,MSR_PKG_POWER_LIMIT , + kvm_rdmsr_pkg_power_limit,NULL ); +if (!r) { +error_report ("Could not install MSR_PKG_POWER_LIMIT \ +handler: %s" , +strerror (-ret)); +exit (1 ); + } + + r =kvm_filter_msr (s,MSR_PKG_POWER_INFO , + kvm_rdmsr_pkg_power_info,NULL ); +if (!r) { +error_report ("Could not install MSR_PKG_POWER_INFO \ +handler: %s" , +strerror (-ret)); +exit (1 ); + } + r =kvm_filter_msr (s,MSR_PKG_ENERGY_STATUS , + kvm_rdmsr_pkg_energy_status,NULL ); +if (!r) { +error_report ("Could not install MSR_PKG_ENERGY_STATUS \ +handler: %s" , +strerror (-ret)); +exit (1 ); + }else { +kvm_msr_energy_thread_init (s, ms); + } + } }return 0 ;target/i386/kvm/meson.build +1
-0@@ -3,6 +3,7 @@ i386_kvm_ss = ss.source_set() i386_kvm_ss.add(files( 'kvm.c', 'kvm-cpu.c', + 'vmsr_energy.c', )) i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c'))target/i386/kvm/vmsr_energy.c new file +61
-0@@ -0,0 +1,295 @@ +/* +* QEMU KVM support -- x86 virtual energy-related MSR. +* +* Copyright 2023 Red Hat, Inc. 2023 +* +* Author: +* Anthony Harivel <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "vmsr_energy.h" +#include "qapi/error.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "hw/boards.h" + +#define MAX_PATH_LEN 256 +#define MAX_LINE_LEN 500 + +static char *compute_default_paths (void ) +{ + g_autofree char *state = qemu_get_local_state_dir(); + +return g_build_filename (state,"run" ,"qemu-vmsr-helper.sock" ,NULL ); +} + +static int vmsr_helper_socket_read (QIOChannel *ioc, +void *buf,int sz,Error **errp) +{ +ssize_t r =qio_channel_read_all (ioc, buf, sz, errp); + +if (r <0 ) { +object_unref (OBJECT (ioc)); + ioc =NULL ; +return -EINVAL ; + } + +return 0 ; +} + +static int vmsr_helper_socket_write (QIOChannel *ioc, +int fd, +const void *buf,int sz,Error **errp) +{ +size_t nfds = (fd !=-1 ); +while (sz >0 ) { +struct iovec iov; +ssize_t n_written; + + iov.iov_base = (void *)buf; + iov.iov_len = sz; + n_written =qio_channel_writev_full (QIO_CHANNEL (ioc), &iov,1 , + nfds ? &fd :NULL , nfds,0 , errp); + +if (n_written <=0 ) { +assert (n_written !=QIO_CHANNEL_ERR_BLOCK ); +object_unref (OBJECT (ioc)); + ioc =NULL ;
target/i386/kvm/vmsr_energy.c line 71
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+return n_written <0 ? -EINVAL :0 ; + } + + nfds =0 ; + buf += n_written; + sz -= n_written; + } + +return 0 ; +}
THis method seems pointless. It is being called with fd == -1. The caller should just directly call qio_channel_write_all which will write all data.
Quoted 31 lines
target/i386/kvm/vmsr_energy.c new file +31
-0+ +uint64_t vmsr_read_msr (uint32_t reg,unsigned int cpu_id,uint32_t tid, +const char *path) +{ +uint64_t data =0 ; +char *socket_path =NULL ; +unsigned int buffer[3 ]; + +if (path ==NULL ) { + socket_path =compute_default_paths (); + }else { + socket_path =g_strdup (path); + } + +SocketAddress saddr = { + .type =SOCKET_ADDRESS_TYPE_UNIX , + .u.q_unix.path = socket_path + }; +QIOChannelSocket *sioc =qio_channel_socket_new (); +Error *local_err =NULL ; + +int r; + +qio_channel_set_name (QIO_CHANNEL (sioc),"vmsr-helper" ); +qio_channel_socket_connect_sync (sioc, + &saddr, + &local_err); +g_free (socket_path); +if (local_err) { + goto out_close; + }
target/i386/kvm/vmsr_energy.c line 112
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+ +/* +* Send the required arguments: +* 1. RAPL MSR register to read +* 2. On which CPU ID +* 3. From which vCPU (Thread ID) +*/ + buffer[0 ] = reg; + buffer[1 ] = cpu_id; + buffer[2 ] = tid;
IMHO it is nicer to declare a packed struct for the fields being sent, and share its decl between the client and server code.
Quoted 44 lines
target/i386/kvm/vmsr_energy.c new file +44
-0+ + r =vmsr_helper_socket_write (QIO_CHANNEL (sioc), +-1 , + &buffer,sizeof (buffer), + &local_err); +if (r <0 ) { + goto out_close; + } + + r =vmsr_helper_socket_read (QIO_CHANNEL (sioc), + &data,sizeof (data), + &local_err); +if (r <0 ) { + data =0 ; + goto out_close; + } + +out_close: +/* Close socket. */ +qio_channel_close (QIO_CHANNEL (sioc),NULL ); +object_unref (OBJECT (sioc)); +return data; +} + +/* Retrieve the max number of physical CPU on the package */ +unsigned int vmsr_get_maxcpus (unsigned int package_num) +{ +int k, ncpus; +unsigned int maxcpus; +struct bitmask *cpus; + + cpus =numa_allocate_cpumask (); + ncpus = cpus->size; + +if (numa_node_to_cpus (package_num, cpus) <0 ) { +return 0 ; + } + + maxcpus =0 ; +for (k =0 ; k < ncpus; k++) { +if (numa_bitmask_isbitset (cpus, k)) { + maxcpus++; + } + }
target/i386/kvm/vmsr_energy.c line 166
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+ +return maxcpus; +} + +/* Retrieve the maximum number of physical packages */ +unsigned int vmsr_get_max_physical_package (unsigned int max_cpus) +{ +unsigned int packageCount =0 ; +const char *dir ="/sys/devices/system/cpu/" ; +int *uniquePackages;
Mark this as g_autofree() to eliminuate the multiple calls go g_free()in each exit path
target/i386/kvm/vmsr_energy.c line 175
Quoted 9 lines
target/i386/kvm/vmsr_energy.c new file +9
-0+ +char *filePath; +FILE *file; + + uniquePackages =g_new0 (int, max_cpus); + +for (int i =0 ; i < max_cpus; i++) { + filePath =g_build_filename (dir,g_strdup_printf ("cpu%d" , i), +"topology/physical_package_id" ,NULL );
Leaking the g_strdup_printf result.
Also filePath is allocated on every iteration, but only freed outside the loop, so another leak. Declare filePath inside the loop and mark it g_autofree
Quoted 35 lines
target/i386/kvm/vmsr_energy.c new file +35
-0+ + file =fopen (filePath,"r" ); + +if (file ==NULL ) { +perror ("Error opening file" ); +g_free (filePath); +g_free (uniquePackages); +return 0 ; + } + +char packageId[10 ]; +if (fgets (packageId,sizeof (packageId), file) ==NULL ) { + packageCount =0 ; + } + +fclose (file); + +int currentPackageId =atoi (packageId); + +bool isUnique = true; +for (int j =0 ; j < packageCount; j++) { +if (uniquePackages[j] == currentPackageId) { + isUnique = false; +break ; + } + } + +if (isUnique) { + uniquePackages[packageCount] = currentPackageId; + packageCount++; + +if (packageCount >= max_cpus) { +break ; + } + }
target/i386/kvm/vmsr_energy.c line 220
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+ } + +g_free (filePath); +g_free (uniquePackages); +return (packageCount ==0 ) ?1 : packageCount; +} +int vmsr_read_thread_stat (struct thread_stat *thread,int pid,int index) +{ +char *path; + path =g_new0 (char,MAX_PATH_LEN );
This pointer is overwritten on the very next line, so is leaked
target/i386/kvm/vmsr_energy.c line 223
Quoted 3 lines
target/i386/kvm/vmsr_energy.c new file +3
-0+ + path =g_build_filename (g_strdup_printf ("/proc/%u/task/%d/stat" , pid, \ + thread->thread_id),NULL );
This result is never freed, and the intermediate g_strdup_printf() is also not freed. Using g_build_filename is pointless if you're going to use g_strdup_printf() to buld the entire filename in one go.
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+ +FILE *file =fopen (path,"r" ); +if (file ==NULL ) { +return -1 ; + } + +if (fscanf (file,"%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u" +" %llu %llu %*d %*d %*d %*d %*d %*d %*u %*u %*d %*u %*u" +" %*u %*u %*u %*u %*u %*u %*u %*u %*u %*d %*u %*u %u" , + &thread->utime[index], &thread->stime[index], &thread->cpu_id) !=3 )
target/i386/kvm/vmsr_energy.c line 243
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+return -1 ; + +fclose (file); +return 0 ; +} + +/* Read QEMU stat task folder to retrieve all QEMU threads ID */ +pid_t *vmsr_get_thread_ids (pid_t pid,unsigned int *num_threads) +{ +char *path =g_build_filename ("/proc" ,g_strdup_printf ("%d/task" , pid),NULL );
Leaking the g_strdup_printf result
target/i386/kvm/vmsr_energy.c line 248
Quoted 5 lines
target/i386/kvm/vmsr_energy.c new file +5
-0+ +DIR *dir =opendir (path); +if (dir ==NULL ) { +perror ("opendir" ); +g_free (path);
Mark 'path' as g_autofree so g_free() isn't needed in every exit-path
Quoted 15 lines
target/i386/kvm/vmsr_energy.c new file +15
-0+return NULL ; + } + +pid_t *thread_ids =NULL ; +unsigned int thread_count =0 ; + +struct dirent *ent; +while ((ent =readdir (dir)) !=NULL ) { +if (ent->d_name[0 ] =='.' ) { +continue ; + } +pid_t tid =atoi (ent->d_name); +if (pid != tid) { + thread_ids =g_renew (pid_t, thread_ids, (thread_count +1 )); + thread_ids[thread_count] = tid;
target/i386/kvm/vmsr_energy.c line 273
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+ thread_count++; + } + } + +closedir (dir); + + *num_threads = thread_count; +g_free (path); +return thread_ids; +}
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Daniel P. Berrangé, Jan 29, 2024 at 20:29:
Quoted 25 lines · Anthony Harivel
Quoted 6 lines
docs/specs/rapl-msr.rst new file +0
-0docs/specs/rapl-msr.rst line 124
Quoted 10 lines
docs/specs/rapl-msr.rst new file +9
-0@@ -0,0 +1,133 @@ +================ +RAPL MSR support +================ + +Current Limitations +------------------- + +- Works only on Intel host CPUs because AMD CPUs are using different MSR + addresses.The privileged helper program is validating an allow list of MSRs.
If those MSRs are only correct on Intel hosts, then the validation is incomplete, and it could be allowing unprivileged processes on AMD hosts to access forbidden MSRS whose address happen to clash with the Intel RAPL MSRs.
Quoted 8 lines
IOW, the privileged helper needs to call
cpuid()and validate that the current host vendor is Intel.I suspect we also need a feature check of some kind to validate that the intel processor supports this features, since old ones definitely didn't, and we shouldn't assume all future ones will either.
To validate that the processor supports the RAPL feature I propose to check this on the Host:
$ cat /sys/class/powercap/intel-rapl/enabled 1
The only down side is that INTEL RAPL drivers needs to be mounted then. We don't need it because we directly read the MSRs.
Regards,
Anthony
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 38 lines · Anthony Harivel
Daniel P. Berrangé, Jan 29, 2024 at 20:29:
Quoted 25 lines · Anthony Harivel
Quoted 6 lines
docs/specs/rapl-msr.rst new file +0
-0docs/specs/rapl-msr.rst line 124
Quoted 10 lines
docs/specs/rapl-msr.rst new file +9
-0@@ -0,0 +1,133 @@ +================ +RAPL MSR support +================ + +Current Limitations +------------------- + +- Works only on Intel host CPUs because AMD CPUs are using different MSR + addresses.The privileged helper program is validating an allow list of MSRs.
If those MSRs are only correct on Intel hosts, then the validation is incomplete, and it could be allowing unprivileged processes on AMD hosts to access forbidden MSRS whose address happen to clash with the Intel RAPL MSRs.
Quoted 8 lines
IOW, the privileged helper needs to call
cpuid()and validate that the current host vendor is Intel.I suspect we also need a feature check of some kind to validate that the intel processor supports this features, since old ones definitely didn't, and we shouldn't assume all future ones will either.
To validate that the processor supports the RAPL feature I propose to check this on the Host:
Quoted 6 lines
$ cat /sys/class/powercap/intel-rapl/enabled 1The only down side is that INTEL RAPL drivers needs to be mounted then. We don't need it because we directly read the MSRs.
Awkward. I've looked around in the kernel drivers and didn't uncover any better option than this.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Daniel P. BerrangéHi Daniel,
Quoted 24 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2645
Quoted 8 lines
target/i386/kvm/kvm.c +8
-0+ +/* Retrieve all packages power plane energy counter */ +for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ This says we're using a thread ID
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2648
Quoted 3 lines
target/i386/kvm/kvm.c +3
-0+if (thd_stat[j].numa_node_id == i) { + pkg_stat[i].e_start = + vmsr_read_msr(MSR_PKG_ENERGY_STATUS, i, pid,but here we're using a pid ID, which is the thread ID of the initial thread.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2653
Quoted 5 lines
target/i386/kvm/kvm.c +5
-0+ s->msr_energy.socket_path); +break ; + } + } + }
Quoted 7 lines
This API design for
vmsr_read_msr()is incredibly inefficient. We're making (maxpkgs *num_threads) calls tovmsr_read_msr(), and every one of those is opening and closing the socket.Why isn't QEMU opening the socket once and then sending all the requests over the same socket ?
The usage of pid here is a mistake, thanks for pointing this out.
However, I'm more sceptical about the fact that the loop is inefficient. The confusion could definitely be because of the poor variable naming, and I apologize about that.
Let me try to explain what it's supposed to do:
Imagine we are running on machine that has i packages. QEMU has j threads running on whichever packages. We need to get the current packages energy of each packages that are used by the QEMU threads. (could be all i packages, only 1, 2.. we don't know what we need yet) So it loops first on the packages "0", and look if any thread has run on this packages.
If no, test the next thread.
if yes, we need the value, we call the vmsr_read_msr() then break and now loop for the next package, i.e package "1". And this until all packages has been tested.
So in the end, we 'only' have maximum "maxpkgs" calls of vmsr_read_msr().
Hope that's ok and that clear up the confusion!
Regards,
Anthony
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Anthony HarivelQuoted 151 lines · Anthony Harivel
Starting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Actual limitation:
- Works only on Intel host CPU because AMD CPUs are using different MSR adresses.- Only the Package Power-Plane (
MSR_PKG_ENERGY_STATUS) is reported at the moment.Signed-off-by Anthony Harivel <[email protected]>--- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 133 +++++++++++++ include/sysemu/kvm_int.h | 17 ++ target/i386/cpu.h | 8 + target/i386/kvm/kvm.c | 348 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 295 ++++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 87 +++++++++ 9 files changed, 917 insertions(+) create mode 100644 docs/specs/rapl-msr.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.hinclude/sysemu/kvm_int.h +65
-0@@ -14,6 +14,8 @@#include "qemu/accel.h" #include "qemu/queue.h" #include "sysemu/kvm.h" +#include "hw/boards.h" +#include "hw/i386/topology.h" typedef struct KVMSlot {@@ -48,6 +50,20 @@ typedef struct KVMMemoryListener {#define KVM_MSI_HASHTAB_SIZE 256 +struct KVMMsrEnergy { +bool enable; +char *socket_path; +QemuThread msr_thr; +unsigned int cpus; +unsigned int sockets; +X86CPUTopoInfo topo_info; +const CPUArchIdList *cpu_list; +uint64_t *msr_value; +uint64_t msr_unit; +uint64_t msr_limit; +uint64_t msr_info; +}; +enum KVMDirtyRingReaperState {KVM_DIRTY_RING_REAPER_NONE =0 ,/* The reaper is sleeping */ @@ -114,6 +130,7 @@ struct KVMStatebool kvm_dirty_ring_with_bitmap;uint64_t kvm_eager_split_size;/* Eager Page Splitting chunk size */ struct KVMDirtyRingReaper reaper; +struct KVMMsrEnergy msr_energy;NotifyVmexitOption notify_vmexit;uint32_t notify_window;uint32_t xen_version;@@ -2509,6 +2558,265 @@ static void register_smram_listener(Notifier *n, void *unused) &smram_address_space, 1, "kvm-smram"); } +static void *kvm_msr_energy_thread (void *data) +{ +KVMState *s = data; +struct KVMMsrEnergy *vmsr = &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat = NULL; + g_autofree thread_stat *thd_stat = NULL; + g_autofree pid_t *thread_ids = NULL; + g_autofree CPUState *cpu = NULL; +unsigned int maxpkgs, maxcpus, maxticks; + g_autofree unsigned int *vpkgs_energy_stat = NULL; +unsigned int num_threads =0 ; +unsigned int tmp_num_threads =0 ; +pid_t pid; + +X86CPUTopoIDs topo_ids; + + +rcu_register_thread (); + +/* Get QEMU PID*/ + pid =getpid (); + +/* Nb of CPUS per packages */ + maxcpus =vmsr_get_maxcpus (0 ); + +/* Nb of Physical Packages on the system */ + maxpkgs =vmsr_get_max_physical_package (maxcpus); + +/* Those MSR values should not change as well */ + vmsr->msr_unit =vmsr_read_msr (MSR_RAPL_POWER_UNIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_limit =vmsr_read_msr (MSR_PKG_POWER_LIMIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_info =vmsr_read_msr (MSR_PKG_POWER_INFO ,0 , pid, + s->msr_energy.socket_path); + +/* Allocate memory for each package energy status */ + pkg_stat = (package_energy_stat *) +g_new0 (package_energy_stat, maxpkgs); + +/* Pre-allocate memory for thread stats */ + thd_stat =g_new0 (thread_stat,1 ); + +/* Pre-allocate memory for holding Virtual Package Energy counter */ + vpkgs_energy_stat = g_new0(unsigned int, vmsr->sockets); + +/*
include/sysemu/kvm_int.h static void register_smram_listener(Notifier *n, void *unused) · line 2618
Quoted 10 lines
include/sysemu/kvm_int.h +10
-0+* Max numbers of ticks per package +* time in second * number of ticks/second * Number of cores / package +* ex: for 100 ticks/second/CPU, 12 CPUs per Package gives 1200 ticks max +*/ + maxticks = (MSR_ENERGY_THREAD_SLEEP_US /1000000 ) + *sysconf (_SC_CLK_TCK) * maxcpus; + +while (true) { +/* Get all qemu threads id */ + thread_ids =vmsr_get_thread_ids (pid, &num_threads);
This is allolcating thread_ids
Quoted 9 lines
include/sysemu/kvm_int.h +9
-0+ +if (thread_ids ==NULL ) { + goto clean; + } + +if (tmp_num_threads < num_threads) { + thd_stat =g_renew (thread_stat, thd_stat, num_threads); + } +
include/sysemu/kvm_int.h static void register_smram_listener(Notifier *n, void *unused) · line 2637
Quoted 10 lines
include/sysemu/kvm_int.h +10
-0+ tmp_num_threads = num_threads; + +/* Populate all the thread stats */ +for (int i =0 ; i < num_threads; i++) { + thd_stat[i].utime = g_new0(unsigned long long, 2); + thd_stat[i].stime = g_new0(unsigned long long, 2); + thd_stat[i].thread_id = thread_ids[i]; +vmsr_read_thread_stat (&thd_stat[i], pid,0 ); + thd_stat[i].numa_node_id =numa_node_of_cpu (thd_stat[i].cpu_id); + }
This loop is allocating memory for utime & stime..
include/sysemu/kvm_int.h static void register_smram_listener(Notifier *n, void *unused) · line 2644
Quoted 7 lines
include/sysemu/kvm_int.h +7
-0+ +/* Zero out the memory */ +for (int i =0 ; i < num_threads; i++) { +memset (thd_stat[i].utime,0 ,2 *sizeof (unsigned long long )); +memset (thd_stat[i].stime,0 ,2 *sizeof (unsigned long long )); + } +memset (thd_stat,0 , num_threads *sizeof (thread_stat));
..and here we're just wiping the thd_stat pointer, so this is surey a memory leak of the stime & utime fields.
include/sysemu/kvm_int.h static void register_smram_listener(Notifier *n, void *unused) · line 2645
Quoted 1 line
include/sysemu/kvm_int.h +1
-0+memset (thread_ids,0 ,sizeof (pid_t));
Nothing is free'ing thread_ids which was allocated earlier in the loop.
Quoted 36 lines
include/sysemu/kvm_int.h +32
-0+ } + +clean: +rcu_unregister_thread (); +return NULL ; +} + +static int kvm_msr_energy_thread_init (KVMState *s,MachineState *ms) +{ +struct KVMMsrEnergy *r = &s->msr_energy; + +vmsr_init_topo_info (&r->topo_info, ms); + +/* Retrieve the number of vCPU */ + r->cpus = ms->smp.cpus; + +/* Retrieve the number of sockets */ + r->sockets = ms->smp.sockets; + +/* Allocate register memory (MSR_PKG_STATUS) for each vCPU */ + r->msr_value =g_new0 (uint64_t, r->cpus); + +/* Retrieve the CPUArchIDlist */ + r->x86_cpu_list =x86_possible_cpu_arch_ids (ms); + +qemu_thread_create (&r->msr_thr,"kvm-msr" , + kvm_msr_energy_thread, + s,QEMU_THREAD_JOINABLE ); + +return 0 ; +} +int kvm_arch_get_default_type (MachineState *ms) {return 0 ;
include/sysemu/kvm_int.h int kvm_arch_init(MachineState *ms, KVMState *s) · line 3023
Quoted 5 lines
include/sysemu/kvm_int.h +2
-0@@ -2711,6 +3019,46 @@ int kvm_arch_init(MachineState *ms, KVMState *s) strerror(-ret));exit (1 ); } + +if (s->msr_energy.enable == true) {
This looks to be where we need to check that both the host CPU vendor is intel, and the guest CPU vendor is intel, and that the host CPU has the RAPL feature we're using.
Quoted 31 lines
include/sysemu/kvm_int.h +31
-0+ + r =kvm_filter_msr (s,MSR_RAPL_POWER_UNIT , + kvm_rdmsr_rapl_power_unit,NULL ); +if (!r) { +error_report ("Could not install MSR_RAPL_POWER_UNIT \ +handler: %s" , +strerror (-ret)); +exit (1 ); + } + + r =kvm_filter_msr (s,MSR_PKG_POWER_LIMIT , + kvm_rdmsr_pkg_power_limit,NULL ); +if (!r) { +error_report ("Could not install MSR_PKG_POWER_LIMIT \ +handler: %s" , +strerror (-ret)); +exit (1 ); + } + + r =kvm_filter_msr (s,MSR_PKG_POWER_INFO , + kvm_rdmsr_pkg_power_info,NULL ); +if (!r) { +error_report ("Could not install MSR_PKG_POWER_INFO \ +handler: %s" , +strerror (-ret)); +exit (1 ); + } + r =kvm_filter_msr (s,MSR_PKG_ENERGY_STATUS , + kvm_rdmsr_pkg_energy_status,NULL ); +if (!r) { + error_report("Could not install MSR_PKG_ENERGY_STATUS \
include/sysemu/kvm_int.h int kvm_arch_init(MachineState *ms, KVMState *s) · line 3064
Quoted 10 lines
include/sysemu/kvm_int.h +7
-0+ handler: %s", + strerror(-ret)); +exit (1 ); + }else { +kvm_msr_energy_thread_init (s, ms); + } + } }return 0 ;
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Daniel,
Quoted 12 lines
Quoted 1 line
+ if (
s->msr_energy.enable== true) {This looks to be where we need to check that both the host CPU vendor is intel, and the guest CPU vendor is intel, and that the host CPU has the RAPL feature we're using.
With regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Checking for the host cpu and RAPL enable is fine and done.
But checking for guest CPU is confusing me.
The RAPL feature is enable only with KVM enable.
This means "-cpu" can only be "host" or its derivative that essentially copy the host CPU definition, no?
That means if we are already checking the host cpu we don't need to do anything for the guest, do we ?
Regards,
Anthony
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 9 lines · Anthony Harivel
Hi Daniel,
Quoted 5 lines
Quoted 1 line
+ if (
s->msr_energy.enable== true) {This looks to be where we need to check that both the host CPU vendor is intel, and the guest CPU vendor is intel, and that the host CPU has the RAPL feature we're using.
Quoted 6 lines
Checking for the host cpu and RAPL enable is fine and done.
But checking for guest CPU is confusing me.
The RAPL feature is enable only with KVM enable.
This means "-cpu" can only be "host" or its derivative that essentially copy the host CPU definition, no?
KVM can use any named CPU.
Quoted 2 lines
That means if we are already checking the host cpu we don't need to do anything for the guest, do we ?
When I first wrote this I though it would be as simple as checknig a CPUID feature flag. That appears to not be the case, however, as Linux is just checking for various CPU models directly. With that in mind perhaps we should just check of the guest CPU model vendor == CPUID_VENDOR_INTEL and leave it at that.
eg, create an error if running an AMD CPU such as $QEMU -cpu EPYC
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Daniel P. Berrangé, Mar 04, 2024 at 15:48:
Quoted 22 lines · Anthony Harivel
Quoted 9 lines
Hi Daniel,
Quoted 5 lines
Quoted 1 line
+ if (
s->msr_energy.enable== true) {This looks to be where we need to check that both the host CPU vendor is intel, and the guest CPU vendor is intel, and that the host CPU has the RAPL feature we're using.
Quoted 6 lines
Checking for the host cpu and RAPL enable is fine and done.
But checking for guest CPU is confusing me.
The RAPL feature is enable only with KVM enable.
This means "-cpu" can only be "host" or its derivative that essentially copy the host CPU definition, no?KVM can use any named CPU.
Quoted 2 lines
That means if we are already checking the host cpu we don't need to do anything for the guest, do we ?
Quoted 7 lines
When I first wrote this I though it would be as simple as checknig a CPUID feature flag. That appears to not be the case, however, as Linux is just checking for various CPU models directly. With that in mind perhaps we should just check of the guest CPU model vendor ==
CPUID_VENDOR_INTELand leave it at that.eg, create an error if running an AMD CPU such as $QEMU -cpu EPYC
The idea looks good to me. Now the hiccups of this solution is that I cannot find a way to reach CPUArchState at this level of code (i.e kvm_arch_init() ) with only the MachineState or the KVMState. I can only reach the topology with x86_possible_cpu_arch_ids().
CPUArchState struct is holding the cpuid_vendor variables where we can use IS_INTEL_CPU() for checking.
Maybe you know the trick that I miss ?
Regards,
Anthony
Quoted 7 lines
With regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 36 lines · Anthony Harivel
Daniel P. Berrangé, Mar 04, 2024 at 15:48:
Quoted 22 lines · Anthony Harivel
Quoted 9 lines
Hi Daniel,
Quoted 5 lines
Quoted 1 line
+ if (
s->msr_energy.enable== true) {This looks to be where we need to check that both the host CPU vendor is intel, and the guest CPU vendor is intel, and that the host CPU has the RAPL feature we're using.
Quoted 6 lines
Checking for the host cpu and RAPL enable is fine and done.
But checking for guest CPU is confusing me.
The RAPL feature is enable only with KVM enable.
This means "-cpu" can only be "host" or its derivative that essentially copy the host CPU definition, no?KVM can use any named CPU.
Quoted 2 lines
That means if we are already checking the host cpu we don't need to do anything for the guest, do we ?
Quoted 7 lines
When I first wrote this I though it would be as simple as checknig a CPUID feature flag. That appears to not be the case, however, as Linux is just checking for various CPU models directly. With that in mind perhaps we should just check of the guest CPU model vendor ==
CPUID_VENDOR_INTELand leave it at that.eg, create an error if running an AMD CPU such as $QEMU -cpu EPYC
The idea looks good to me. Now the hiccups of this solution is that I cannot find a way to reach CPUArchState at this level of code (i.e
kvm_arch_init()) with only the MachineState or the KVMState. I can only reach the topology withx86_possible_cpu_arch_ids().
Quoted 4 lines
CPUArchState struct is holding the
cpuid_vendorvariables where we can useIS_INTEL_CPU()for checking.Maybe you know the trick that I miss ?
I think perhaps you can do a check in kvm_cpu_realizefn() from target/i386/kvm/kvm-cpu.c, as you have CPUX86State state which is what IS_INTEL_CPU wants.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Anthony HarivelQuoted 78 lines · Anthony Harivel
Starting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Actual limitation:
- Works only on Intel host CPU because AMD CPUs are using different MSR adresses.- Only the Package Power-Plane (
MSR_PKG_ENERGY_STATUS) is reported at the moment.Signed-off-by Anthony Harivel <[email protected]>--- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 133 +++++++++++++ include/sysemu/kvm_int.h | 17 ++ target/i386/cpu.h | 8 + target/i386/kvm/kvm.c | 348 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 295 ++++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 87 +++++++++ 9 files changed, 917 insertions(+) create mode 100644 docs/specs/rapl-msr.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.htarget/i386/kvm/kvm.c +18
-0@@ -2509,6 +2558,265 @@ static void register_smram_listener(Notifier *n, void *unused) &smram_address_space, 1, "kvm-smram"); } +static void *kvm_msr_energy_thread (void *data) +{ +KVMState *s = data; +struct KVMMsrEnergy *vmsr = &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat = NULL; + g_autofree thread_stat *thd_stat = NULL; + g_autofree pid_t *thread_ids = NULL; + g_autofree CPUState *cpu = NULL; +unsigned int maxpkgs, maxcpus, maxticks; + g_autofree unsigned int *vpkgs_energy_stat = NULL; +unsigned int num_threads =0 ; +unsigned int tmp_num_threads =0 ; +pid_t pid; + +X86CPUTopoIDs topo_ids; + +
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2588
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+rcu_register_thread (); + +/* Get QEMU PID*/ + pid =getpid (); + +/* Nb of CPUS per packages */ + maxcpus =vmsr_get_maxcpus (0 ); + +/* Nb of Physical Packages on the system */ + maxpkgs =vmsr_get_max_physical_package (maxcpus);
This function can fail so this needs to be checked & reported.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2596
Quoted 8 lines
target/i386/kvm/kvm.c +8
-0+ +/* Those MSR values should not change as well */ + vmsr->msr_unit =vmsr_read_msr (MSR_RAPL_POWER_UNIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_limit =vmsr_read_msr (MSR_PKG_POWER_LIMIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_info =vmsr_read_msr (MSR_PKG_POWER_INFO ,0 , pid, + s->msr_energy.socket_path);
This function can fail for a variety of reasons, most especially if someone gave an incorrect socket path, or if the daemon is not running. This is not getting diagnosed, and even if we try to report it here, we're in a background thread at this point.
I think we need to connect and report errors before even starting this thread, so that QEMU startup gets aborted upon configuration error.
Quoted 29 lines
target/i386/kvm/kvm.c +29
-0+ +/* Allocate memory for each package energy status */ + pkg_stat = (package_energy_stat *) +g_new0 (package_energy_stat, maxpkgs); + +/* Pre-allocate memory for thread stats */ + thd_stat =g_new0 (thread_stat,1 ); + +/* Pre-allocate memory for holding Virtual Package Energy counter */ + vpkgs_energy_stat = g_new0(unsigned int, vmsr->sockets); + +/* +* Max numbers of ticks per package +* time in second * number of ticks/second * Number of cores / package +* ex: for 100 ticks/second/CPU, 12 CPUs per Package gives 1200 ticks max +*/ + maxticks = (MSR_ENERGY_THREAD_SLEEP_US /1000000 ) + *sysconf (_SC_CLK_TCK) * maxcpus; + +while (true) { +/* Get all qemu threads id */ + thread_ids =vmsr_get_thread_ids (pid, &num_threads); + +if (thread_ids ==NULL ) { + goto clean; + } + +if (tmp_num_threads < num_threads) { + thd_stat =g_renew (thread_stat, thd_stat, num_threads);
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2635
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ } + + tmp_num_threads = num_threads; + +/* Populate all the thread stats */ +for (int i =0 ; i < num_threads; i++) { + thd_stat[i].utime = g_new0(unsigned long long, 2); + thd_stat[i].stime = g_new0(unsigned long long, 2); + thd_stat[i].thread_id = thread_ids[i]; +vmsr_read_thread_stat (&thd_stat[i], pid,0 );
It is non-obvious that the 3rd parameter here is an index into the utime & stime array. This function would be saner to review if called as:
vmsr_read_thread_stat(pid, thd_stat[i].thread_id, &thd_stat[i].utime[0], &thd_stat[i].stime[0], &thd_stat[i].cpu_id);
so we see what are input parameters and what are output parameters.
Also this method can fail, eg if the thread has exited already, so we need to take that into account and stop trying to get info for that thread in later code. eg by setting 'thread_id' to 0 and then skipping any thread_id == 0 later.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2646
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+ thd_stat[i].numa_node_id =numa_node_of_cpu (thd_stat[i].cpu_id); + } + +/* Retrieve all packages power plane energy counter */ +for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].numa_node_id == i) {
'i' is a CPU ID value, while 'numa_node_id' is a NUMA node ID value. I don't think it is semantically valid to compare them for equality.
I'm not sure the NUMA node is even relevant, since IIUC from the docs earlier, the power values are scoped per package, which would mean per CPU socket.
Quoted 38 lines
target/i386/kvm/kvm.c +37
-0+ pkg_stat[i].e_start = +vmsr_read_msr (MSR_PKG_ENERGY_STATUS , i, pid, + s->msr_energy.socket_path); +break ; + } + } + } + +/* Sleep a short period while the other threads are working */ +usleep (MSR_ENERGY_THREAD_SLEEP_US ); + +/* +* Retrieve all packages power plane energy counter +* Calculate the delta of all packages +*/ +for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].numa_node_id == i) { + pkg_stat[i].e_end = +vmsr_read_msr (MSR_PKG_ENERGY_STATUS , + thd_stat[j].cpu_id, + thd_stat[j].thread_id, + s->msr_energy.socket_path); +/* +* Prevent the case we have migrate the VM +* during the sleep period or any other cases +* were energy counter might be lower after +* the sleep. +*/ +if (pkg_stat[i].e_end > pkg_stat[i].e_start) { + pkg_stat[i].e_delta = + pkg_stat[i].e_end - pkg_stat[i].e_start; + }else {
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2693
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ pkg_stat[i].e_delta =0 ; + } +break ; + } + } + } + +/* Delta of ticks spend by each thread between the sample */ +for (int i =0 ; i < num_threads; i++) { +if (vmsr_read_thread_stat (&thd_stat[i], pid,1 ) !=0 ) {
Here we fetch the thread 'cpu_id' again. What is expected behaviour if 'cpu_id' has changed since we read it earlier ? The delta ticks calculation will be OK is cpu_is is still within the same package as earlier, but if it moved to another package it is not valid.
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+/* +* We don't count the dead thread +* i.e threads that existed before the sleep +* and not anymore +*/ + thd_stat[i].delta_ticks =0 ; + }else { +vmsr_delta_ticks (thd_stat, i); + } + } +
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2714
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+/* +* Identify the vCPU threads +* Calculate the Number of vCPU per package +*/ +CPU_FOREACH (cpu) { +for (int i =0 ; i < num_threads; i++) { +if (cpu->thread_id == thd_stat[i].thread_id) { + thd_stat[i].is_vcpu = true; + thd_stat[i].vcpu_id = cpu->cpu_index; + pkg_stat[thd_stat[i].numa_node_id].nb_vcpu++;
A numa node isn't a package AFAICT.
Quoted 69 lines
target/i386/kvm/kvm.c +69
-0+ thd_stat[i].acpi_id =kvm_arch_vcpu_id (cpu); +break ; + } + } + } + +/* Retrieve the virtual package number of each vCPU */ +for (int i =0 ; i < vmsr->x86_cpu_list->len; i++) { +for (int j =0 ; j < num_threads; j++) { +if ((thd_stat[j].acpi_id == vmsr->x86_cpu_list->cpus[i].arch_id) + && (thd_stat[j].is_vcpu == true)) { +x86_topo_ids_from_apicid (thd_stat[j].acpi_id, + &vmsr->topo_info, &topo_ids); + thd_stat[j].vpkg = topo_ids.pkg_id; + } + } + } + +/* Calculate the total energy of all non-vCPU thread */ +for (int i =0 ; i < num_threads; i++) { +double temp; +if ((thd_stat[i].is_vcpu != true) && + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat, thd_stat, maxticks, i); + pkg_stat[thd_stat[i].numa_node_id].e_ratio + += (uint64_t )lround (temp); + } + } + +/* Calculate the ratio per non-vCPU thread of each package */ +for (int i =0 ; i <= maxpkgs; i++) { +if (pkg_stat[i].nb_vcpu >0 ) { + pkg_stat[i].e_ratio = pkg_stat[i].e_ratio / pkg_stat[i].nb_vcpu; + } + } + +/* +* Calculate the energy for each Package: +* Energy Package = sum of each vCPU energy that belongs to the package +*/ +for (int i =0 ; i < num_threads; i++) { +double temp; + +if ((thd_stat[i].is_vcpu == true) && \ + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat, thd_stat, maxticks, i); + + vpkgs_energy_stat[thd_stat[i].vpkg] += (uint64_t )lround (temp); + vpkgs_energy_stat[thd_stat[i].vpkg] += + pkg_stat[thd_stat[i].numa_node_id].e_ratio; + } + } + +/* +* Finally populate the vmsr register of each vCPU with the total +* package value to emulate the real hardware where each CPU return the +* value of the package it belongs. +*/ +for (int i =0 ; i < num_threads; i++) { +if ((thd_stat[i].is_vcpu == true) && \ + (thd_stat[i].delta_ticks >0 )) { + vmsr->msr_value[thd_stat[i].vcpu_id] = \ + vpkgs_energy_stat[thd_stat[i].vpkg]; + } + } + +/* Zero out the memory */ +for (int i =0 ; i < num_threads; i++) { +memset (thd_stat[i].utime,0 ,2 *sizeof (unsigned long long ));
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2793
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+memset (thd_stat[i].stime,0 ,2 *sizeof (unsigned long long )); + } +memset (thd_stat,0 , num_threads *sizeof (thread_stat)); +memset (thread_ids,0 ,sizeof (pid_t)); + } + +clean: +rcu_unregister_thread (); +return NULL ; +}
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Daniel, Paolo,
Here my last questions before wrapping up and send v4, or maybe call off my attempt to add RAPL interface in QEMU.
Daniel P. Berrangé, Jan 30, 2024 at 10:39:
Quoted 22 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2588
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+rcu_register_thread (); + +/* Get QEMU PID*/ + pid =getpid (); + +/* Nb of CPUS per packages */ + maxcpus =vmsr_get_maxcpus (0 ); + +/* Nb of Physical Packages on the system */ + maxpkgs =vmsr_get_max_physical_package (maxcpus);This function can fail so this needs to be checked & reported.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2596
Quoted 8 lines
target/i386/kvm/kvm.c +8
-0+ +/* Those MSR values should not change as well */ + vmsr->msr_unit =vmsr_read_msr (MSR_RAPL_POWER_UNIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_limit =vmsr_read_msr (MSR_PKG_POWER_LIMIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_info =vmsr_read_msr (MSR_PKG_POWER_INFO ,0 , pid, + s->msr_energy.socket_path);
Quoted 8 lines
This function can fail for a variety of reasons, most especially if someone gave an incorrect socket path, or if the daemon is not running. This is not getting diagnosed, and even if we try to report it here, we're in a background thread at this point.
I think we need to connect and report errors before even starting this thread, so that QEMU startup gets aborted upon configuration error.
Fair enough. Would it be ok to do the sanity check before rcu_register_thread() and "return NULL;" in case of error or would you prefer me to check all of this before even calling the qemu_thread_create() ?
Quoted 17 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2635
Quoted 6 lines
target/i386/kvm/kvm.c +6
-0+/* Populate all the thread stats */ +for (int i =0 ; i < num_threads; i++) { + thd_stat[i].utime = g_new0(unsigned long long, 2); + thd_stat[i].stime = g_new0(unsigned long long, 2); + thd_stat[i].thread_id = thread_ids[i]; +vmsr_read_thread_stat (&thd_stat[i], pid,0 );It is non-obvious that the 3rd parameter here is an index into the utime & stime array. This function would be saner to review if called as:
vmsr_read_thread_stat(pid, thd_stat[i].thread_id, &thd_stat[i].utime[0], &thd_stat[i].stime[0], &thd_stat[i].cpu_id);
Quoted 8 lines
so we see what are input parameters and what are output parameters.
Also this method can fail, eg if the thread has exited already, so we need to take that into account and stop trying to get info for that thread in later code. eg by setting '
thread_id' to 0 and then skipping anythread_id== 0 later.
Good point. I'll rework the function and return "thread_id" to 0 in case of failure in order to test it later on.
Quoted 12 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2646
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+ thd_stat[i].numa_node_id =numa_node_of_cpu (thd_stat[i].cpu_id); + } + +/* Retrieve all packages power plane energy counter */ +for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].numa_node_id == i) {
Quoted 7 lines
'i' is a CPU ID value, while '
numa_node_id' is a NUMA node ID value. I don't think it is semantically valid to compare them for equality.I'm not sure the NUMA node is even relevant, since IIUC from the docs earlier, the power values are scoped per package, which would mean per CPU socket.
'i' here is the package number on the host.
I'm using functions of libnuma to populate the maxpkgs of the host. I tested this on different Intel CPU with multiple packages and this has always returned the good number of packages. A false positive ?
So here I'm checking if the thread has run on the package number 'i'. I populate 'numa_node_id' with numa_node_of_cpu().
I did not wanted to reinvent the wheel and the only lib that was talking about "node" was libnuma.
Maybe I'm wrong assuming that a "node" (defined as an area where all memory has the same speed as seen from a particular CPU) could lead me to the packages number ?
And this is what I see you wrote below:
"A numa node isn't a package AFAICT."
Regards,
Anthony
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 28 lines · Anthony Harivel
Daniel P. Berrangé, Jan 30, 2024 at 10:39:
Quoted 27 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2588
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+rcu_register_thread (); + +/* Get QEMU PID*/ + pid =getpid (); + +/* Nb of CPUS per packages */ + maxcpus =vmsr_get_maxcpus (0 ); + +/* Nb of Physical Packages on the system */ + maxpkgs =vmsr_get_max_physical_package (maxcpus);This function can fail so this needs to be checked & reported.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2596
Quoted 8 lines
target/i386/kvm/kvm.c +8
-0+ +/* Those MSR values should not change as well */ + vmsr->msr_unit =vmsr_read_msr (MSR_RAPL_POWER_UNIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_limit =vmsr_read_msr (MSR_PKG_POWER_LIMIT ,0 , pid, + s->msr_energy.socket_path); + vmsr->msr_info =vmsr_read_msr (MSR_PKG_POWER_INFO ,0 , pid, + s->msr_energy.socket_path);This function can fail for a variety of reasons, most especially if someone gave an incorrect socket path, or if the daemon is not running. This is not getting diagnosed, and even if we try to report it here, we're in a background thread at this point.
Quoted 8 lines
Quoted 3 lines
I think we need to connect and report errors before even starting this thread, so that QEMU startup gets aborted upon configuration error.
Fair enough. Would it be ok to do the sanity check before
rcu_register_thread()and "return NULL;" in case of error or would you prefer me to check all of this before even calling theqemu_thread_create()?
I think it is best to initialize in kvm_msr_energy_thread_init(), prior to thread creation, as that avoids some complexity cleaning up and reporting errors.
Quoted 15 lines
Quoted 15 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2646
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+ thd_stat[i].numa_node_id =numa_node_of_cpu (thd_stat[i].cpu_id); + } + +/* Retrieve all packages power plane energy counter */ +for (int i =0 ; i <= maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].numa_node_id == i) {'i' is a CPU ID value, while '
numa_node_id' is a NUMA node ID value. I don't think it is semantically valid to compare them for equality.
Quoted 6 lines
Quoted 4 lines
I'm not sure the NUMA node is even relevant, since IIUC from the docs earlier, the power values are scoped per package, which would mean per CPU socket.
'i' here is the package number on the host.
Opps, yes, 'maxpkgs' is iterating over CPU /socket/ ID numbers
The point still stands though. NUMA node ID numbers are not guaranteed to be the same as socket ID numbers. Very often then will be the same (which makes it annoying to test as it is easy to not realize the difference), but we can't rely on that.
Quoted 3 lines
I'm using functions of libnuma to populate the maxpkgs of the host. I tested this on different Intel CPU with multiple packages and this has always returned the good number of packages. A false positive ?
maxpkgs comes from vmsr_get_max_physical_package() which you're reading from sysfs, rather than libnuma.
Quoted 5 lines
So here I'm checking if the thread has run on the package number 'i'. I populate '
numa_node_id' withnuma_node_of_cpu().I did not wanted to reinvent the wheel and the only lib that was talking about "node" was libnuma.
I'm not actually convinced we need to use libnuma at all. IIUC, you're just trying to track all CPUs within the same physical socket (package). I don't think we need to care about NUMA nodes to do that tracking.
Quoted 3 lines
Maybe I'm wrong assuming that a "node" (defined as an area where all memory has the same speed as seen from a particular CPU) could lead me to the packages number ?
Historically you could have multiple sockets in the same NUMA node ie a m:1 mapping.
These days with AMD sockets, you can have 1 socket compromising many NUMA nodes, as individual dies within a socket are each their own NUMA node. So a 1:m mapping
On Intel I think it is still typical to have 1 socket per numa node, but again I don't think we can rely on that 1:1 mapping.
Fortunately I don't think it matters, since it looks like you don't really need to track NUMA nodes, only sockets (phnysical package IDs)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Daniel,
Daniel P. Berrangé, Mar 12, 2024 at 16:49:
Quoted 16 lines
The point still stands though. NUMA node ID numbers are not guaranteed to be the same as socket ID numbers. Very often then will be the same (which makes it annoying to test as it is easy to not realize the difference), but we can't rely on that.
Quoted 3 lines
I'm using functions of libnuma to populate the maxpkgs of the host. I tested this on different Intel CPU with multiple packages and this has always returned the good number of packages. A false positive ?
maxpkgs comes from
vmsr_get_max_physical_package()which you're reading from sysfs, rather than libnuma.Quoted 3 lines
So here I'm checking if the thread has run on the package number 'i'. I populate '
numa_node_id' withnuma_node_of_cpu().
Quoted 7 lines
Quoted 2 lines
I did not wanted to reinvent the wheel and the only lib that was talking about "node" was libnuma.
I'm not actually convinced we need to use libnuma at all. IIUC, you're just trying to track all CPUs within the same physical socket (package). I don't think we need to care about NUMA nodes to do that tracking.
Alright, having a deeper look I'm actually using NUMA for 2 info:
- How many cpu per Package: this helps me calculate the ratio.
- To whom package the cpu belongs: to calculate the ratio with the right package energy counter.
Without libnuma, I'm bit confused on how to handle this.
Should I parse /sys/bus/node/devices/node* to know how many packages ? Should I parse /sys/bus/node/devices/node0/cpu0/topology/core_cpus_list to handle which cpu belongs to which package ?
Would that be too cumbusome for the user to enter the detail about how many packages and how many cpu per pakages ?
i.e:
-kvm,rapl=true,maxpkgs=2,cpupkgs=8,rapl-helper-socket=/path/sock.sock
Quoted 11 lines
Quoted 3 lines
Maybe I'm wrong assuming that a "node" (defined as an area where all memory has the same speed as seen from a particular CPU) could lead me to the packages number ?
Historically you could have multiple sockets in the same NUMA node ie a m:1 mapping.
These days with AMD sockets, you can have 1 socket compromising many NUMA nodes, as individual dies within a socket are each their own NUMA node. So a 1:m mapping
Quoted 7 lines
On Intel I think it is still typical to have 1 socket per numa node, but again I don't think we can rely on that 1:1 mapping.
Fortunately I don't think it matters, since it looks like you don't really need to track NUMA nodes, only sockets (phnysical package IDs)
Very informative, thanks !
Quoted 6 lines
With regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Regards,
Anthony
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 32 lines · Anthony Harivel
Hi Daniel,
Daniel P. Berrangé, Mar 12, 2024 at 16:49:
Quoted 16 lines
The point still stands though. NUMA node ID numbers are not guaranteed to be the same as socket ID numbers. Very often then will be the same (which makes it annoying to test as it is easy to not realize the difference), but we can't rely on that.
Quoted 3 lines
I'm using functions of libnuma to populate the maxpkgs of the host. I tested this on different Intel CPU with multiple packages and this has always returned the good number of packages. A false positive ?
maxpkgs comes from
vmsr_get_max_physical_package()which you're reading from sysfs, rather than libnuma.Quoted 3 lines
So here I'm checking if the thread has run on the package number 'i'. I populate '
numa_node_id' withnuma_node_of_cpu().Quoted 7 lines
Quoted 2 lines
I did not wanted to reinvent the wheel and the only lib that was talking about "node" was libnuma.
I'm not actually convinced we need to use libnuma at all. IIUC, you're just trying to track all CPUs within the same physical socket (package). I don't think we need to care about NUMA nodes to do that tracking.
Alright, having a deeper look I'm actually using NUMA for 2 info:
- How many cpu per Package: this helps me calculate the ratio.
Quoted 8 lines
- To whom package the cpu belongs: to calculate the ratio with the right package energy counter.
Without libnuma, I'm bit confused on how to handle this.
Should I parse
/sys/bus/node/devices/node*to know how many packages ? Should I parse/sys/bus/node/devices/node0/cpu0/topology/core_cpus_listto handle which cpu belongs to which package ?
You don't need to access it via the /node/ hierarchy
The canonical path for CPUs would be
/sys/devices/system/cpu/cpuNNN/topology
The core_cpus_list file is giving you hyper-thread siblings within a core, which I don't think is what you want.
If you're after discrete physical packages, then 'package_cpus_list' gives you all CPUs within a physical socket (package) I believe.
Quoted 5 lines
Would that be too cumbusome for the user to enter the detail about how many packages and how many cpu per pakages ?
i.e:
-kvm,rapl=true,maxpkgs=2,cpupkgs=8,rapl-helper-socket=/path/sock.sock
That won't cope with asymmetrical CPU configurations, so I think it is preferrable to read the info from sysfs.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Daniel,
Quoted 4 lines
You don't need to access it via the /node/ hierarchy
The canonical path for CPUs would be
Quoted 8 lines
/sys/devices/system/cpu/cpuNNN/topologyThe
core_cpus_listfile is giving you hyper-thread siblings within a core, which I don't think is what you want.If you're after discrete physical packages, then '
package_cpus_list' gives you all CPUs within a physical socket (package) I believe.
Yes, this could work.
However, on laptop, I've got:
cat package_cpus_list
0-11
Where on server:package_cpus_list 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46
I asked my teammate: always the same results. This is I guess due to either a difference in the kernel version or how the kernel is handling the case where there is only one package, versus the case with multiple packages.
Anyway, writing a C function to handle both cases might not be easy.
The other solution would be to loop through /sys/devices/system/cpu/cpuNNN/ and update a table of integer of a fixed size (*) where I increment table[0] for package_0 when I encounter a CPU that belongs to package_0 and so on. Reading cpuNNN/topology/physical_package_id is giving to whom package the cpu belongs to.
This is a bit tedious but a safer solution, I think.
(*): Maybe dynamic allocation is better ?
Thanks again for your guidance.
Regards,
Anthony
Re: [PATCH v3 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 22 lines · Anthony Harivel
Hi Daniel,
Quoted 4 lines
You don't need to access it via the /node/ hierarchy
The canonical path for CPUs would be
Quoted 8 lines
/sys/devices/system/cpu/cpuNNN/topologyThe
core_cpus_listfile is giving you hyper-thread siblings within a core, which I don't think is what you want.If you're after discrete physical packages, then '
package_cpus_list' gives you all CPUs within a physical socket (package) I believe.Yes, this could work.
However, on laptop, I've got:
catpackage_cpus_list
0-11
Quoted 8 lines
Where on server:
package_cpus_list0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46I asked my teammate: always the same results. This is I guess due to either a difference in the kernel version or how the kernel is handling the case where there is only one package, versus the case with multiple packages.
Both are the same data format - it is a list of ranges. In the laptop example, there's only a single range to parse. In the server example there are many ranges but since each range is only 1 cpu, it has collapsed the ranges to the single CPU id.
Quoted 1 line
Anyway, writing a C function to handle both cases might not be easy.
Approximately
* Read the whole file with g_get_file_contents
* Use g_strsplit(data, ",", 0) to get a list of ranges
* Iterate over the return list of ranges and g_strsplit(range, "-", 2);
- The returned list should be either a single element (if there was no '-'), or a pair of elements (if there was a N-M)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|