From nobody Sat May 11 19:04:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1710774781; cv=none; d=zohomail.com; s=zohoarc; b=DS1R++HRCNHEeZH3UclvEYNGuuDg71H374Ihp/wdV9rGt6HpFcTz/bd8PCIgjx7tYrYQ1K9OOurH681zWhd509zzesouJJMaJ5Y9NTuwIeeZq/BwzWXicqOgRkOHvHVfcmYlA+MlTC4WSLuqAql9DLWmtluRpOBt1A/rNaBtyzY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710774781; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=krMRbwmlagypL/SnSyYoRchJUfMdP19K6/CS5D1AhrE=; b=lWC0/88/CQTjO8e69pwZvdNPEBPYv9j1F+FjlTVTbfDGUpjmwxPng/5emxpk8GxTQK1Frj6rUlJ06QdCFgCGgBE7klf0jEgkYWb+G4tiuocVGiiBl74HoqfrhL/vduXwDTrPPCaG1F7miZHAsKIU89+FK5PNqQ52wnG4Q/9ImxI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1710774781347620.3040360226304; Mon, 18 Mar 2024 08:13:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rmEfI-0000tD-Jc; Mon, 18 Mar 2024 11:12:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmEfG-0000sw-Sm for qemu-devel@nongnu.org; Mon, 18 Mar 2024 11:12:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmEfE-0001PT-Q9 for qemu-devel@nongnu.org; Mon, 18 Mar 2024 11:12:38 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-620-9sKmyWezPeWXoqRAQiYvjQ-1; Mon, 18 Mar 2024 11:12:32 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B3AEE101A586 for ; Mon, 18 Mar 2024 15:12:32 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.39.208.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3BFFE10F53; Mon, 18 Mar 2024 15:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710774756; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=krMRbwmlagypL/SnSyYoRchJUfMdP19K6/CS5D1AhrE=; b=cqquuoWZKsFYLM/w1JKmXhsI0rsDbOHHjrxLjKFKVUeiRQVa3LOzR9zADL1fTdG7JFVyYW 2Dm4plSGEtWI32r9hr+HLCo7txMVgNA0vWUuNLrBgI48KMQ10Q7Y83qgBzsBlykV0ngJGj aJ9M9MDXpY21PZNrrcsldfvMGyiYbzg= X-MC-Unique: 9sKmyWezPeWXoqRAQiYvjQ-1 From: Anthony Harivel To: pbonzini@redhat.com, mtosatti@redhat.com, berrange@redhat.com Cc: qemu-devel@nongnu.org, vchundur@redhat.com, rjarry@redhat.com, Anthony Harivel Subject: [PATCH v4 1/3] qio: add support for SO_PEERCRED for socket channel Date: Mon, 18 Mar 2024 16:12:14 +0100 Message-ID: <20240318151216.32833-2-aharivel@redhat.com> In-Reply-To: <20240318151216.32833-1-aharivel@redhat.com> References: <20240318151216.32833-1-aharivel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=aharivel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1710774782047100003 Content-Type: text/plain; charset="utf-8" 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 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. Signed-off-by: Anthony Harivel --- include/io/channel.h | 21 +++++++++++++++++++++ io/channel-socket.c | 24 ++++++++++++++++++++++++ io/channel.c | 12 ++++++++++++ 3 files changed, 57 insertions(+) diff --git a/include/io/channel.h b/include/io/channel.h index 7986c49c713a..01ad7bd7e430 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -160,6 +160,9 @@ struct QIOChannelClass { void *opaque); int (*io_flush)(QIOChannel *ioc, Error **errp); + void (*io_peerpid)(QIOChannel *ioc, + unsigned int *pid, + Error **errp); }; =20 /* General I/O handling functions */ @@ -981,4 +984,22 @@ int coroutine_mixed_fn qio_channel_writev_full_all(QIO= Channel *ioc, int qio_channel_flush(QIOChannel *ioc, Error **errp); =20 +/** + * 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 an error with pid -1 for the non-Linux OS. + * + */ +void qio_channel_get_peerpid(QIOChannel *ioc, + unsigned int *pid, + Error **errp); + #endif /* QIO_CHANNEL_H */ diff --git a/io/channel-socket.c b/io/channel-socket.c index 3a899b060858..fcff92ecc151 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -841,6 +841,29 @@ qio_channel_socket_set_cork(QIOChannel *ioc, socket_set_cork(sioc->fd, v); } =20 +static void +qio_channel_socket_get_peerpid(QIOChannel *ioc, + unsigned int *pid, + Error **errp) +{ +#ifdef CONFIG_LINUX + QIOChannelSocket *sioc =3D QIO_CHANNEL_SOCKET(ioc); + Error *err =3D NULL; + socklen_t len =3D sizeof(struct ucred); + + struct ucred cred; + if (getsockopt(sioc->fd, + SOL_SOCKET, SO_PEERCRED, + &cred, &len) =3D=3D -1) { + error_setg_errno(&err, errno, "Unable to get peer credentials"); + error_propagate(errp, err); + } + *pid =3D (unsigned int)cred.pid; +#else + error_setg(errp, "Unsupported feature"); + *pid =3D -1; +#endif +} =20 static int qio_channel_socket_close(QIOChannel *ioc, @@ -938,6 +961,7 @@ static void qio_channel_socket_class_init(ObjectClass *= klass, #ifdef QEMU_MSG_ZEROCOPY ioc_klass->io_flush =3D qio_channel_socket_flush; #endif + ioc_klass->io_peerpid =3D qio_channel_socket_get_peerpid; } =20 static const TypeInfo qio_channel_socket_info =3D { diff --git a/io/channel.c b/io/channel.c index a1f12f8e9096..777989bc9a81 100644 --- a/io/channel.c +++ b/io/channel.c @@ -548,6 +548,18 @@ void qio_channel_set_cork(QIOChannel *ioc, } } =20 +void qio_channel_get_peerpid(QIOChannel *ioc, + unsigned int *pid, + Error **errp) +{ + QIOChannelClass *klass =3D 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); +} =20 off_t qio_channel_io_seek(QIOChannel *ioc, off_t offset, --=20 2.44.0 From nobody Sat May 11 19:04:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1710774773; cv=none; d=zohomail.com; s=zohoarc; b=UJQCcXBvc8KCSWqTERlvouVWLjVEkSPReM0OjXqHNHC9Re4yo8FRohhMPIuI17OPAs2qyH03zO7r1gnkf7wbxlRBeLyOZlrJlUm1iDYOV/XQbZuwoWqa/MHuH3lIHMU267kv9yPfXDZSxF9wZ1DqKT1YHWMByS1qVVXG8c3TNXo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710774773; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=dl4b7L7Ot8dh9gI32pUUoX8pbfUKU3URT27mZkop3SA=; b=NGBMAxjLINHM7YaYm61cqeRfWySp05FMdk/diWCY8pe8un66WV1z7CzlKcmdvhdTAjkO/68KVOYYJ4X2kmhUXFTstB7oNLaTOaopPqohfKMSK3WF39Q5MWChkSi1cff0cG5fMBjZXV8rOwqu0nnyhkJ+Tvyt9+iF68T9/Y7UPrM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1710774773420808.9193300770764; Mon, 18 Mar 2024 08:12:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rmEfO-0000tr-IL; Mon, 18 Mar 2024 11:12:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmEfL-0000tb-96 for qemu-devel@nongnu.org; Mon, 18 Mar 2024 11:12:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmEfI-0001QI-FO for qemu-devel@nongnu.org; Mon, 18 Mar 2024 11:12:43 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695--ZFCN13qOpSghCrpxMT2sQ-1; Mon, 18 Mar 2024 11:12:38 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E85F5855424 for ; Mon, 18 Mar 2024 15:12:34 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.39.208.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 51B7510F43; Mon, 18 Mar 2024 15:12:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710774759; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dl4b7L7Ot8dh9gI32pUUoX8pbfUKU3URT27mZkop3SA=; b=DuZC1Uty1seVP2dkdPC899uUeTM0md9hBMn0QyyJDMC154d/Okt8J+v08yKeB6z9E83eyk znm4puI/fGJ+PydEg/JMZyvUmJ7dQ/8bNukVLHQV23x7k1BUhYUwjbsgD1hZMp0OQbZiqs +rpIRfVgac9Rklbw7H9IzOoI1JhX5wM= X-MC-Unique: -ZFCN13qOpSghCrpxMT2sQ-1 From: Anthony Harivel To: pbonzini@redhat.com, mtosatti@redhat.com, berrange@redhat.com Cc: qemu-devel@nongnu.org, vchundur@redhat.com, rjarry@redhat.com, Anthony Harivel Subject: [PATCH v4 2/3] tools: build qemu-vmsr-helper Date: Mon, 18 Mar 2024 16:12:15 +0100 Message-ID: <20240318151216.32833-3-aharivel@redhat.com> In-Reply-To: <20240318151216.32833-1-aharivel@redhat.com> References: <20240318151216.32833-1-aharivel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aharivel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1710774774044100007 Content-Type: text/plain; charset="utf-8" Introduce a privileged helper to access RAPL MSR. The privileged helper tool, qemu-vmsr-helper, is designed to provide virtual machines with the ability to read specific RAPL (Running Average Power Limit) MSRs without requiring CAP_SYS_RAWIO privileges or relying on external, out-of-tree patches. The helper tool leverages Unix permissions and SO_PEERCRED socket options to enforce access control, ensuring that only processes explicitly requesting read access via readmsr() from a valid Thread ID can access these MSRs. The list of RAPL MSRs that are allowed to be read by the helper tool is defined in rapl-msr-index.h. This list corresponds to the RAPL MSRs that will be supported in the next commit titled "Add support for RAPL MSRs in KVM/QEMU." The tool is intentionally designed to run on the Linux x86 platform. This initial implementation is tailored for Intel CPUs but can be extended to support AMD CPUs in the future. Signed-off-by: Anthony Harivel --- 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 | 564 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 7 files changed, 711 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 diff --git a/contrib/systemd/qemu-vmsr-helper.service b/contrib/systemd/qem= u-vmsr-helper.service new file mode 100644 index 000000000000..8fd397bf79a9 --- /dev/null +++ b/contrib/systemd/qemu-vmsr-helper.service @@ -0,0 +1,15 @@ +[Unit] +Description=3DVirtual RAPL MSR Daemon for QEMU + +[Service] +WorkingDirectory=3D/tmp +Type=3Dsimple +ExecStart=3D/usr/bin/qemu-vmsr-helper +PrivateTmp=3Dyes +ProtectSystem=3Dstrict +ReadWritePaths=3D/var/run +RestrictAddressFamilies=3DAF_UNIX +Restart=3Dalways +RestartSec=3D0 + +[Install] diff --git a/contrib/systemd/qemu-vmsr-helper.socket b/contrib/systemd/qemu= -vmsr-helper.socket new file mode 100644 index 000000000000..183e8304d6e2 --- /dev/null +++ b/contrib/systemd/qemu-vmsr-helper.socket @@ -0,0 +1,9 @@ +[Unit] +Description=3DVirtual RAPL MSR helper for QEMU + +[Socket] +ListenStream=3D/run/qemu-vmsr-helper.sock +SocketMode=3D0600 + +[Install] +WantedBy=3Dmulti-user.target diff --git a/docs/tools/index.rst b/docs/tools/index.rst index 8e65ce0dfc7b..33ad438e86f6 100644 --- a/docs/tools/index.rst +++ b/docs/tools/index.rst @@ -16,3 +16,4 @@ command line utilities and other standalone programs. qemu-pr-helper qemu-trace-stap virtfs-proxy-helper + qemu-vmsr-helper diff --git a/docs/tools/qemu-vmsr-helper.rst b/docs/tools/qemu-vmsr-helper.= rst new file mode 100644 index 000000000000..6ec87b49d962 --- /dev/null +++ b/docs/tools/qemu-vmsr-helper.rst @@ -0,0 +1,89 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +QEMU virtual RAPL MSR helper +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Synopsis +-------- + +**qemu-vmsr-helper** [*OPTION*] + +Description +----------- + +Implements the virtual RAPL MSR helper for QEMU. + +Accessing the RAPL (Running Average Power Limit) MSR enables the RAPL powe= rcap +driver to advertise and monitor the power consumption or accumulated energy +consumption of different power domains, such as CPU packages, DRAM, and ot= her +components when available. + +However those register are accesible under priviliged access (CAP_SYS_RAWI= O). +QEMU can use an external helper to access those priviliged register. + +:program:`qemu-vmsr-helper` is that external helper; it creates a listener +socket which will accept incoming connections for communication with QEMU. + +If you want to run VMs in a setup like this, this helper should be started= as a +system service, and you should read the QEMU manual section on "RAPL MSR +support" to find out how to configure QEMU to connect to the socket create= d by +:program:`qemu-vmsr-helper`. + +After connecting to the socket, :program:`qemu-vmsr-helper` can +optionally drop root privileges, except for those capabilities that +are needed for its operation. + +:program:`qemu-vmsr-helper` can also use the systemd socket activation +protocol. In this case, the systemd socket unit should specify a +Unix stream socket, like this:: + + [Socket] + ListenStream=3D/var/run/qemu-vmsr-helper.sock + +Options +------- + +.. program:: qemu-vmsr-helper + +.. option:: -d, --daemon + + run in the background (and create a PID file) + +.. option:: -q, --quiet + + decrease verbosity + +.. option:: -v, --verbose + + increase verbosity + +.. option:: -f, --pidfile=3DPATH + + PID file when running as a daemon. By default the PID file + is created in the system runtime state directory, for example + :file:`/var/run/qemu-vmsr-helper.pid`. + +.. option:: -k, --socket=3DPATH + + path to the socket. By default the socket is created in + the system runtime state directory, for example + :file:`/var/run/qemu-vmsr-helper.sock`. + +.. option:: -T, --trace [[enable=3D]PATTERN][,events=3DFILE][,file=3DFILE] + + .. include:: ../qemu-option-trace.rst.inc + +.. option:: -u, --user=3DUSER + + user to drop privileges to + +.. option:: -g, --group=3DGROUP + + group to drop privileges to + +.. option:: -h, --help + + Display a help message and exit. + +.. option:: -V, --version + + Display version information and exit. diff --git a/meson.build b/meson.build index b375248a7614..376da49b60ab 100644 --- a/meson.build +++ b/meson.build @@ -4052,6 +4052,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 =20 if have_ivshmem diff --git a/tools/i386/qemu-vmsr-helper.c b/tools/i386/qemu-vmsr-helper.c new file mode 100644 index 000000000000..d8439dc173af --- /dev/null +++ b/tools/i386/qemu-vmsr-helper.c @@ -0,0 +1,564 @@ +/* + * Privileged RAPL MSR helper commands for QEMU + * + * Copyright (C) 2024 Red Hat, Inc. + * + * Author: Anthony Harivel + * + * 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 . + */ + +#include "qemu/osdep.h" +#include +#include +#include +#ifdef CONFIG_LIBCAP_NG +#include +#endif +#include +#include + +#include "qemu/help-texts.h" +#include "qapi/error.h" +#include "qemu/cutils.h" +#include "qemu/main-loop.h" +#include "qemu/module.h" +#include "qemu/error-report.h" +#include "qemu/config-file.h" +#include "qemu-version.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/log.h" +#include "qemu/systemd.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "trace/control.h" +#include "qemu-version.h" +#include "rapl-msr-index.h" + +#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 =3D 1; + +#ifdef CONFIG_LIBCAP_NG +static int uid =3D -1; +static int gid =3D -1; +#endif + +static void compute_default_paths(void) +{ + g_autofree char *state =3D qemu_get_local_state_dir(); + + socket_path =3D g_build_filename(state, "run", "qemu-vmsr-helper.sock"= , NULL); + pidfile =3D g_build_filename(state, "run", "qemu-vmsr-helper.pid", NUL= L); +} + +static int is_intel_processor(void) +{ + int result; + int ebx, ecx, edx; + + /* Execute CPUID instruction with eax=3D0 (basic identification) */ + asm volatile ( + "cpuid" + : "=3Db" (ebx), "=3Dc" (ecx), "=3Dd" (edx) + : "a" (0) + ); + + /* + * Check if processor is "GenuineIntel" + * 0x756e6547 =3D "Genu" + * 0x49656e69 =3D "ineI" + * 0x6c65746e =3D "ntel" + */ + result =3D (ebx =3D=3D 0x756e6547) && (edx =3D=3D 0x49656e69) && (ecx = =3D=3D 0x6c65746e); + + return result; +} + +static int is_rapl_enabled(void) +{ + const char *path =3D "/sys/class/powercap/intel-rapl/enabled"; + FILE *file =3D fopen(path, "r"); + int value =3D 0; + + if (file !=3D NULL) { + if (fscanf(file, "%d", &value) !=3D 1) { + error_report("INTEL RAPL not enabled"); + } + fclose(file); + } else { + error_report("Error opening %s", path); + } + + return value; +} + +/* + * Check if the TID that request the MSR read + * belongs to the peer. It be should a TID of a vCPU. + */ +static bool is_tid_present(pid_t pid, pid_t tid) +{ + g_autofree char *pidStr; + g_autofree char *tidStr; + + pidStr =3D g_strdup_printf("%d", pid); + tidStr =3D g_strdup_printf("%d", tid); + + char *tidPath; + + tidPath =3D g_strdup_printf("/proc/%s/task/%s", pidStr, tidStr); + + /* Check if the TID directory exists within the PID directory */ + if (access(tidPath, F_OK) =3D=3D 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; + } +} + +static uint64_t vmsr_read_msr(uint32_t msr_register, unsigned int cpu_id) +{ + int fd; + uint64_t result =3D 0; + + g_autofree char *path; + path =3D g_strdup_printf(MSR_PATH_TEMPLATE, cpu_id); + + /* + * 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); + + if (!CPU_ISSET(cpu_id, &cpu_set)) { + fprintf(stderr, "CPU %u is not in the thread's affinity.\n", cpu_i= d); + return result; + } + + fd =3D 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) !=3D sizeof(resul= t)) { + error_report("Failed to read MSR"); + result =3D 0; + } + + close(fd); + return result; +} + +static void usage(const char *name) +{ + (printf) ( +"Usage: %s [OPTIONS] FILE\n" +"Virtual RAPL MSR helper program for QEMU\n" +"\n" +" -h, --help display this help and exit\n" +" -V, --version output version information and exit\n" +"\n" +" -d, --daemon run in the background\n" +" -f, --pidfile=3DPATH PID file when running as a daemon\n" +" (default '%s')\n" +" -k, --socket=3DPATH path to the unix socket\n" +" (default '%s')\n" +" -T, --trace [[enable=3D]][,events=3D][,file=3D]\n" +" specify tracing options\n" +#ifdef CONFIG_LIBCAP_NG +" -u, --user=3DUSER user to drop privileges to\n" +" -g, --group=3DGROUP group to drop privileges to\n" +#endif +"\n" +QEMU_HELP_BOTTOM "\n" + , name, pidfile, socket_path); +} + +static void version(const char *name) +{ + printf( +"%s " QEMU_FULL_VERSION "\n" +"Written by Anthony Harivel.\n" +"\n" +QEMU_COPYRIGHT "\n" +"This is free software; see the source for copying conditions. There is N= O\n" +"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOS= E.\n" + , name); +} + +typedef struct VMSRHelperClient { + QIOChannelSocket *ioc; + Coroutine *co; +} VMSRHelperClient; + +static void coroutine_fn vh_co_entry(void *opaque) +{ + VMSRHelperClient *client =3D opaque; + Error *local_err =3D NULL; + unsigned int peer_pid; + uint32_t request[3]; + uint64_t vmsr; + int r; + + qio_channel_set_blocking(QIO_CHANNEL(client->ioc), + false, NULL); + + qio_channel_set_follow_coroutine_ctx(QIO_CHANNEL(client->ioc), true); + + /* + * Check peer credentials + */ + qio_channel_get_peerpid(QIO_CHANNEL(client->ioc), &peer_pid, &local_er= r); + + if (peer_pid =3D=3D 0) { + if (local_err !=3D NULL) { + error_report_err(local_err); + } + error_report("Failed to get peer credentials"); + goto out; + } + + /* + * Read the requested MSR + * Only RAPL MSR in rapl-msr-index.h is allowed + */ + r =3D qio_channel_read_all(QIO_CHANNEL(client->ioc), + (char *) &request, sizeof(request), &local_er= r); + if ((local_err !=3D NULL) || r < 0) { + error_report("Read request fail"); + error_report_err(local_err); + goto out; + } + if (!is_msr_allowed(request[0])) { + error_report("Requested unallowed msr: %d", request[0]); + goto out; + } + + vmsr =3D 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]); + vmsr =3D 0; + } + + r =3D qio_channel_write_all(QIO_CHANNEL(client->ioc), + (char *) &vmsr, sizeof(vmsr), &local_err); + if ((local_err !=3D NULL) || r < 0) { + error_report("Write request fail"); + error_report_err(local_err); + goto out; + } + +out: + object_unref(OBJECT(client->ioc)); +} + +static gboolean accept_client(QIOChannel *ioc, + GIOCondition cond, + gpointer opaque) +{ + QIOChannelSocket *cioc; + VMSRHelperClient *vmsrh; + + cioc =3D qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), + NULL); + if (!cioc) { + return FALSE; + } + + vmsrh =3D g_new(VMSRHelperClient, 1); + vmsrh->ioc =3D cioc; + vmsrh->co =3D 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 =3D -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; + } + + /* + * 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 !=3D -1 ? uid : getuid(), + gid !=3D -1 ? gid : getgid(), + CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING)) { + return -1; + } + + return 0; +} +#endif + +int main(int argc, char **argv) +{ + const char *sopt =3D "hVk:f:dT:u:g:vq"; + struct option lopt[] =3D { + { "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 =3D 0; + int ch; + Error *local_err =3D NULL; + bool daemonize =3D false; + bool pidfile_specified =3D false; + bool socket_path_specified =3D false; + unsigned socket_activation; + + struct sigaction sa_sigterm; + memset(&sa_sigterm, 0, sizeof(sa_sigterm)); + sa_sigterm.sa_handler =3D 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(); + + /* + * Sanity check + * 1. cpu must be Intel cpu + * 2. RAPL must be enabled + */ + if (!is_intel_processor()) { + error_report("error: CPU is not INTEL cpu"); + exit(EXIT_FAILURE); + } + + if (!is_rapl_enabled()) { + error_report("error: RAPL driver not enable"); + exit(EXIT_FAILURE); + } + + while ((ch =3D getopt_long(argc, argv, sopt, lopt, &opt_ind)) !=3D -1)= { + switch (ch) { + case 'k': + g_free(socket_path); + socket_path =3D g_strdup(optarg); + socket_path_specified =3D true; + if (socket_path[0] !=3D '/') { + error_report("socket path must be absolute"); + exit(EXIT_FAILURE); + } + break; + case 'f': + g_free(pidfile); + pidfile =3D g_strdup(optarg); + pidfile_specified =3D true; + break; +#ifdef CONFIG_LIBCAP_NG + case 'u': { + unsigned long res; + struct passwd *userinfo =3D getpwnam(optarg); + if (userinfo) { + uid =3D userinfo->pw_uid; + } else if (qemu_strtoul(optarg, NULL, 10, &res) =3D=3D 0 && + (uid_t)res =3D=3D res) { + uid =3D res; + } else { + error_report("invalid user '%s'", optarg); + exit(EXIT_FAILURE); + } + break; + } + case 'g': { + unsigned long res; + struct group *groupinfo =3D getgrnam(optarg); + if (groupinfo) { + gid =3D groupinfo->gr_gid; + } else if (qemu_strtoul(optarg, NULL, 10, &res) =3D=3D 0 && + (gid_t)res =3D=3D res) { + gid =3D 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 =3D 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 =3D check_socket_activation(); + if (socket_activation =3D=3D 0) { + SocketAddress saddr; + saddr =3D (SocketAddress){ + .type =3D SOCKET_ADDRESS_TYPE_UNIX, + .u.q_unix.path =3D socket_path, + }; + server_ioc =3D 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 =3D qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_F= D, + &local_err); + if (server_ioc =3D=3D NULL) { + error_reportf_err(local_err, + "Failed to use socket activation: "); + exit(EXIT_FAILURE); + } + } + + qemu_init_main_loop(&error_fatal); + + server_watch =3D 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 + + info_report("Listening on %s", socket_path); + + state =3D RUNNING; + do { + main_loop_wait(false); + if (state =3D=3D TERMINATE) { + state =3D TERMINATING; + close_server_socket(); + } + } while (num_active_sockets > 0); + + exit(EXIT_SUCCESS); +} diff --git a/tools/i386/rapl-msr-index.h b/tools/i386/rapl-msr-index.h new file mode 100644 index 000000000000..9a7118639ae3 --- /dev/null +++ b/tools/i386/rapl-msr-index.h @@ -0,0 +1,28 @@ +/* + * Allowed list of MSR for Privileged RAPL MSR helper commands for QEMU + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Anthony Harivel + * + * 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 . + */ + +/* + * 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 --=20 2.44.0 From nobody Sat May 11 19:04:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1710774800; cv=none; d=zohomail.com; s=zohoarc; b=FZ24A9YNDu1N/FgN7a3HJxBPK7zJIhf1XvrV/TzvpKdQDF9WSXaVdK7tlW8bwNr5KYbAWDwKolCSQMkaNqNiJnC0Ljl+xn/OxUIgr0YxN8q9mKEshfipGZaoMmJciRHtn+3gGRoaKDAQ0AvkiISiVpGJ/YGzga9Yv94qQ9o7ytw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1710774800; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=3X9/wpEcVyXLZD5WUogGcovIzm3b1TNzu631P6uGjeI=; b=MA3qCIwhWQlelnXst/sAWNhOHHDo9mgwgeOv0MIW5CGlPwd4uim76oGWBthubt9vUHiYkZonnR/KXGHtJJsgLUZ9QpFEwNkYTOCtniYLpxjXcSz4SwD3MKOm/ZCMMu/wPE4+n6l4ITw/0cwxjR9x1prVDINg6yJTeVcVOjVkV1Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1710774800881150.72787489927327; Mon, 18 Mar 2024 08:13:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rmEfU-00011T-B9; Mon, 18 Mar 2024 11:12:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmEfS-0000zF-MO for qemu-devel@nongnu.org; Mon, 18 Mar 2024 11:12:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmEfK-0001QH-5p for qemu-devel@nongnu.org; Mon, 18 Mar 2024 11:12:50 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-175-OJ3bTSfXM5yOY8qK7U7esA-1; Mon, 18 Mar 2024 11:12:37 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DCEA18007AF for ; Mon, 18 Mar 2024 15:12:36 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.39.208.9]) by smtp.corp.redhat.com (Postfix) with ESMTP id 48E7A10F43; Mon, 18 Mar 2024 15:12:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710774759; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3X9/wpEcVyXLZD5WUogGcovIzm3b1TNzu631P6uGjeI=; b=Jpom1iYAju5qTmffFvhm6UffY93PKWczH2yJfRAJ39WgaGq7WpUhorNzIliU6GoCJBAEKL fxOqqumaNNsOWngDyc9dc/Bnp2G1kQOG9+YnSGoBbinIDiTKd2txY9ZBv2gDH+kqhlaqyx r93afgyxVQsJXxO1O2D3X/0zdvxqOZ0= X-MC-Unique: OJ3bTSfXM5yOY8qK7U7esA-1 From: Anthony Harivel To: pbonzini@redhat.com, mtosatti@redhat.com, berrange@redhat.com Cc: qemu-devel@nongnu.org, vchundur@redhat.com, rjarry@redhat.com, Anthony Harivel Subject: [PATCH v4 3/3] Add support for RAPL MSRs in KVM/Qemu Date: Mon, 18 Mar 2024 16:12:16 +0100 Message-ID: <20240318151216.32833-4-aharivel@redhat.com> In-Reply-To: <20240318151216.32833-1-aharivel@redhat.com> References: <20240318151216.32833-1-aharivel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aharivel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1710774802185100003 Content-Type: text/plain; charset="utf-8" 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_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=3Dtrue,path=3D/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 --- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 155 +++++++++++++ include/sysemu/kvm.h | 2 + include/sysemu/kvm_int.h | 30 +++ target/i386/cpu.h | 8 + target/i386/kvm/kvm-cpu.c | 7 + target/i386/kvm/kvm.c | 420 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 381 ++++++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 97 ++++++++ 11 files changed, 1129 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 diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index a8cecd040ebc..7649f226767a 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -3613,6 +3613,21 @@ static void kvm_set_device(Object *obj, s->device =3D g_strdup(value); } =20 +static void kvm_set_kvm_rapl(Object *obj, bool value, Error **errp) +{ + KVMState *s =3D KVM_STATE(obj); + s->msr_energy.enable =3D value; +} + +static void kvm_set_kvm_rapl_socket_path(Object *obj, + const char *str, + Error **errp) +{ + KVMState *s =3D KVM_STATE(obj); + g_free(s->msr_energy.socket_path); + s->msr_energy.socket_path =3D g_strdup(str); +} + static void kvm_accel_instance_init(Object *obj) { KVMState *s =3D KVM_STATE(obj); @@ -3632,6 +3647,7 @@ static void kvm_accel_instance_init(Object *obj) s->xen_gnttab_max_frames =3D 64; s->xen_evtchn_max_pirq =3D 256; s->device =3D NULL; + s->msr_energy.enable =3D false; } =20 /** @@ -3676,6 +3692,17 @@ static void kvm_accel_class_init(ObjectClass *oc, vo= id *data) object_class_property_set_description(oc, "device", "Path to the device node to use (default: /dev/kvm)"); =20 + 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); } =20 diff --git a/docs/specs/index.rst b/docs/specs/index.rst index 1484e3e76077..e738ea7d102f 100644 --- a/docs/specs/index.rst +++ b/docs/specs/index.rst @@ -33,3 +33,4 @@ guest hardware that is specific to QEMU. virt-ctlr vmcoreinfo vmgenid + rapl-msr diff --git a/docs/specs/rapl-msr.rst b/docs/specs/rapl-msr.rst new file mode 100644 index 000000000000..1202ee89bee0 --- /dev/null +++ b/docs/specs/rapl-msr.rst @@ -0,0 +1,155 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +RAPL MSR support +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The RAPL interface (Running Average Power Limit) is advertising the accumu= lated +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 Joule= s. + +Thanks to the MSR Filtering patch [#a]_ not all MSRs are handled by KVM. S= ome +of them can now be handled by the userspace (QEMU). It uses a mechanism ca= lled +"MSR filtering" where a list of MSRs is given at init time of a VM to KVM = so +that a callback is put in place. The design of this patch uses only this +mechanism for handling the MSRs between guest/host. + +At the moment the following MSRs are involved: + +.. code:: C + + #define MSR_RAPL_POWER_UNIT 0x00000606 + #define MSR_PKG_POWER_LIMIT 0x00000610 + #define MSR_PKG_ENERGY_STATUS 0x00000611 + #define MSR_PKG_POWER_INFO 0x00000614 + +The ``*_POWER_UNIT``, ``*_POWER_LIMIT``, ``*_POWER INFO`` are part of the = RAPL +spec and specify the power limit of the package, provide range of paramete= r(min +power, max power,..) and also the information of the multiplier for the en= ergy +counter to calculate the power. Those MSRs are populated once at the begin= ning +by reading the host CPU MSRs and are given back to the guest 1:1 when +requested. + +The MSR_PKG_ENERGY_STATUS is a counter; it represents the total amount of +energy consumed since the last time the register was cleared. If you multi= ply +it with the UNIT provided above you'll get the power in micro-joules. This +counter is always increasing and it increases more or less faster dependin= g on +the consumption of the package. This counter is supposed to overflow at so= me +point. + +Each core belonging to the same Package reading the MSR_PKG_ENERGY_STATUS = (i.e +"rdmsr 0x611") will retrieve the same value. The value represents the ener= gy +for the whole package. Whatever Core reading it will get the same value an= d a +core that belongs to PKG-0 will not be able to get the value of PKG-1 and +vice-versa. + +High level implementation +------------------------- + +In order to update the value of the virtual MSR, a QEMU thread is created. +The thread is basically just an infinity loop that does: + +1. Snapshot of the time metrics of all QEMU threads (Time spent scheduled = in + Userspace and System) + +2. Snapshot of the actual MSR_PKG_ENERGY_STATUS counter of all packages wh= ere + the QEMU threads are running on. + +3. Sleep for 1 second - During this pause the vcpu and other non-vcpu thre= ads + will do what they have to do and so the energy counter will increase. + +4. Repeat 2. and 3. and calculate the delta of every metrics representing = the + time spent scheduled for each QEMU thread *and* the energy spent by the + packages during the pause. + +5. Filter the vcpu threads and the non-vcpu threads. + +6. Retrieve the topology of the Virtual Machine. This helps identify which + vCPU is running on which virtual package. + +7. The total energy spent by the non-vcpu threads is divided by the number + of vcpu threads so that each vcpu thread will get an equal part of the + energy spent by the QEMU workers. + +8. Calculate the ratio of energy spent per vcpu threads. + +9. Calculate the energy for each virtual package. + +10. The virtual MSRs are updated for each virtual package. Each vCPU that + belongs to the same package will return the same value when accessing = the + the MSR. + +11. Loop back to 1. + +Ratio calculation +----------------- + +In Linux, a process has an execution time associated with it. The schedule= r is +dividing the time in clock ticks. The number of clock ticks per second can= be +found by the sysconf system call. A typical value of clock ticks per secon= d is +100. So a core can run a process at the maximum of 100 ticks per second. I= f a +package has 4 cores, 400 ticks maximum can be scheduled on all the cores +of the package for a period of 1 second. + +The /proc/[pid]/stat [#b]_ is a sysfs file that can give the executed time= of a +process with the [pid] as the process ID. It gives the amount of ticks the +process has been scheduled in userspace (utime) and kernel space (stime). + +By reading those metrics for a thread, one can calculate the ratio of time= the +package has spent executing the thread. + +Example: + +A 4 cores package can schedule a maximum of 400 ticks per second with 100 = ticks +per second per core. If a thread was scheduled for 100 ticks between a sec= ond +on this package, that means my thread has been scheduled for 1/4 of the wh= ole +package. With that, the calculation of the energy spent by the thread on t= his +package during this whole second is 1/4 of the total energy spent by the +package. + +Usage +----- + +Currently this feature is only working on an Intel CPU that has the RAPL d= river +mounted and available in the sysfs. if not, QEMU fails at start-up. + +This feature is activated with -accel +kvm,rapl=3Dtrue,rapl-helper-socket=3D/path/sock.sock + +It is important that the socket path is the same as the one +:program:`qemu-vmsr-helper` is listening to. + +qemu-vmsr-helper +---------------- + +The qemu-vmsr-helper is working very much like the qemu-pr-helper. Instead= of +making persistent reservation, qemu-vmsr-helper is here to overcome the +CVE-2020-8694 which remove user access to the rapl msr attributes. + +A socket communication is established between QEMU processes that has the = RAPL +MSR support activated and the qemu-vmsr-helper. A systemd service and sock= et +activation is provided in contrib/systemd/qemu-vmsr-helper.(service/socket= ). + +The systemd socket uses 600, like contrib/systemd/qemu-pr-helper.socket. T= he +socket can be passed via SCM_RIGHTS by libvirt, or its permissions can be +changed (e.g. 660 and root:kvm for a Debian system for example). Libvirt c= ould +also start a separate helper if needed. All in all, the policy is left to = the +user. + +See the qemu-pr-helper documentation or manpage for further details. + +Current Limitations +------------------- + +- Works only on Intel host CPUs because AMD CPUs are using different MSR + addresses. + +- Only the Package Power-Plane (MSR_PKG_ENERGY_STATUS) is reported at the + moment. + +References +---------- + +.. [#a] https://patchwork.kernel.org/project/kvm/patch/20200916202951.2376= 0-7-graf@amazon.com/ +.. [#b] https://man7.org/linux/man-pages/man5/proc.5.html diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index fad9a7e8ff30..37f68c496807 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -544,4 +544,6 @@ uint32_t kvm_dirty_ring_size(void); * reported for the VM. */ bool kvm_hwpoisoned_mem(void); + +bool kvm_is_rapl_feat_enable(CPUState *cs); #endif diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 882e37e12c5b..ea4587b53eb1 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -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" =20 typedef struct KVMSlot { @@ -48,6 +50,33 @@ typedef struct KVMMemoryListener { =20 #define KVM_MSI_HASHTAB_SIZE 256 =20 +typedef struct KVMHostTopoInfo { + /* Number of package on the Host */ + unsigned int maxpkgs; + /* Number of cpus on the Host */ + unsigned int maxcpus; + /* Number of cpus on each different package */ + unsigned int *pkg_cpu_count; + /* Each package can have different maxticks */ + unsigned int *maxticks; +} KVMHostTopoInfo; + +struct KVMMsrEnergy { + pid_t pid; + bool enable; + char *socket_path; + QemuThread msr_thr; + unsigned int vcpus; + unsigned int vsockets; + X86CPUTopoInfo topo_info; + KVMHostTopoInfo host_topo; + 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 =3D 0, /* The reaper is sleeping */ @@ -114,6 +143,7 @@ struct KVMState bool 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; diff --git a/target/i386/cpu.h b/target/i386/cpu.h index 952174bb6f52..135bda8a5db9 100644 --- a/target/i386/cpu.h +++ b/target/i386/cpu.h @@ -400,6 +400,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 @@ -1795,6 +1799,10 @@ typedef struct CPUArchState { =20 uintptr_t retaddr; =20 + /* 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; =20 diff --git a/target/i386/kvm/kvm-cpu.c b/target/i386/kvm/kvm-cpu.c index 9c791b7b0520..246de2bac2f1 100644 --- a/target/i386/kvm/kvm-cpu.c +++ b/target/i386/kvm/kvm-cpu.c @@ -50,6 +50,13 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) MSR_IA32_UCODE_REV); } } + if (kvm_is_rapl_feat_enable(cs)) { + if (IS_INTEL_CPU(env)) { + error_setg(errp, "RAPL feature is enable and CPU is not INTEL = CPU"); + return false; + }; + }; + return host_cpu_realizefn(cs, errp); } =20 diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index e68cbe929302..27dacdf1aa54 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -16,9 +16,12 @@ #include "qapi/qapi-events-run-state.h" #include "qapi/error.h" #include "qapi/visitor.h" +#include #include #include #include +#include +#include =20 #include #include "standard-headers/asm-x86/kvm_para.h" @@ -26,6 +29,7 @@ =20 #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" @@ -2479,6 +2483,54 @@ static bool kvm_rdmsr_core_thread_count(X86CPU *cpu,= uint32_t msr, return true; } =20 +static bool kvm_rdmsr_rapl_power_unit(X86CPU *cpu, uint32_t msr, + uint64_t *val) +{ + + CPUState *cs =3D CPU(cpu); + + *val =3D cs->kvm_state->msr_energy.msr_unit; + + return true; +} + +static bool kvm_rdmsr_pkg_power_limit(X86CPU *cpu, uint32_t msr, + uint64_t *val) +{ + + CPUState *cs =3D CPU(cpu); + + *val =3D cs->kvm_state->msr_energy.msr_limit; + + return true; +} + +static bool kvm_rdmsr_pkg_power_info(X86CPU *cpu, uint32_t msr, + uint64_t *val) +{ + + CPUState *cs =3D CPU(cpu); + + *val =3D cs->kvm_state->msr_energy.msr_info; + + return true; +} + +static bool kvm_rdmsr_pkg_energy_status(X86CPU *cpu, uint32_t msr, + uint64_t *val) +{ + + CPUState *cs =3D CPU(cpu); + *val =3D cs->kvm_state->msr_energy.msr_value[cs->cpu_index]; + + return true; +} + +bool kvm_is_rapl_feat_enable(CPUState *cs) +{ + return cs->kvm_state->msr_energy.enable; +} + static Notifier smram_machine_done; static KVMMemoryListener smram_listener; static AddressSpace smram_address_space; @@ -2513,6 +2565,331 @@ static void register_smram_listener(Notifier *n, vo= id *unused) &smram_address_space, 1, "kvm-smram"); } =20 +static void *kvm_msr_energy_thread(void *data) +{ + KVMState *s =3D data; + struct KVMMsrEnergy *vmsr =3D &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat =3D NULL; + g_autofree thread_stat *thd_stat =3D NULL; + g_autofree pid_t *thread_ids =3D NULL; + g_autofree CPUState *cpu =3D NULL; + g_autofree unsigned int *vpkgs_energy_stat =3D NULL; + unsigned int num_threads =3D 0; + unsigned int tmp_num_threads =3D 0; + + X86CPUTopoIDs topo_ids; + + rcu_register_thread(); + + /* Allocate memory for each package energy status */ + pkg_stat =3D g_new0(package_energy_stat, vmsr->host_topo.maxpkgs); + + /* Allocate memory for thread stats */ + thd_stat =3D g_new0(thread_stat, 1); + + /* Allocate memory for holding virtual package energy counter */ + vpkgs_energy_stat =3D g_new0(unsigned int, vmsr->vsockets); + + /* Populate the max tick of each packages */ + for (int i =3D 0; i <=3D vmsr->host_topo.maxpkgs; i++) { + /* + * Max numbers of ticks per package + * Time in second * Number of ticks/second * Number of cores/packa= ge + * ex: 100 ticks/second/CPU, 12 CPUs per Package gives 1200 ticks = max + */ + vmsr->host_topo.maxticks[i] =3D (MSR_ENERGY_THREAD_SLEEP_US / 1000= 000) + * sysconf(_SC_CLK_TCK) + * vmsr->host_topo.pkg_cpu_count[i]; + } + + while (true) { + /* Get all qemu threads id */ + thread_ids =3D vmsr_get_thread_ids(vmsr->pid, &num_threads); + + if (thread_ids =3D=3D NULL) { + goto clean; + } + + if (tmp_num_threads < num_threads) { + thd_stat =3D g_renew(thread_stat, thd_stat, num_threads); + } + + tmp_num_threads =3D num_threads; + + /* Populate all the thread stats */ + for (int i =3D 0; i < num_threads; i++) { + thd_stat[i].utime =3D g_new0(unsigned long long, 2); + thd_stat[i].stime =3D g_new0(unsigned long long, 2); + thd_stat[i].thread_id =3D thread_ids[i]; + vmsr_read_thread_stat(vmsr->pid, + thd_stat[i].thread_id, + thd_stat[i].utime, + thd_stat[i].stime, + &thd_stat[i].cpu_id); + thd_stat[i].pkg_id =3D + vmsr_get_physical_package_id(thd_stat[i].cpu_id); + } + + /* Retrieve all packages power plane energy counter */ + for (int i =3D 0; i < vmsr->host_topo.maxpkgs; i++) { + for (int j =3D 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].pkg_id =3D=3D i) { + pkg_stat[i].e_start =3D + vmsr_read_msr(MSR_PKG_ENERGY_STATUS, + thd_stat[j].cpu_id, + thd_stat[j].thread_id, + 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 =3D 0; i < vmsr->host_topo.maxpkgs; i++) { + for (int j =3D 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].pkg_id =3D=3D i) { + pkg_stat[i].e_end =3D + 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 period. + */ + if (pkg_stat[i].e_end > pkg_stat[i].e_start) { + pkg_stat[i].e_delta =3D + pkg_stat[i].e_end - pkg_stat[i].e_start; + } else { + pkg_stat[i].e_delta =3D 0; + } + break; + } + } + } + + /* Delta of ticks spend by each thread between the sample */ + for (int i =3D 0; i < num_threads; i++) { + vmsr_read_thread_stat(vmsr->pid, + thd_stat[i].thread_id, + thd_stat[i].utime, + thd_stat[i].stime, + &thd_stat[i].cpu_id); + + if (vmsr->pid < 0) { + /* + * We don't count the dead thread + * i.e threads that existed before the sleep + * and not anymore + */ + thd_stat[i].delta_ticks =3D 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 =3D 0; i < num_threads; i++) { + if (cpu->thread_id =3D=3D thd_stat[i].thread_id) { + thd_stat[i].is_vcpu =3D true; + thd_stat[i].vcpu_id =3D cpu->cpu_index; + pkg_stat[thd_stat[i].pkg_id].nb_vcpu++; + thd_stat[i].acpi_id =3D kvm_arch_vcpu_id(cpu); + break; + } + } + } + + /* Retrieve the virtual package number of each vCPU */ + for (int i =3D 0; i < vmsr->x86_cpu_list->len; i++) { + for (int j =3D 0; j < num_threads; j++) { + if ((thd_stat[j].acpi_id =3D=3D vmsr->x86_cpu_list->cpus[i= ].arch_id) + && (thd_stat[j].is_vcpu =3D=3D true)) { + x86_topo_ids_from_apicid(thd_stat[j].acpi_id, + &vmsr->topo_info, &topo_ids); + thd_stat[j].vpkg_id =3D topo_ids.pkg_id; + } + } + } + + /* Calculate the total energy of all non-vCPU thread */ + for (int i =3D 0; i < num_threads; i++) { + double temp; + if ((thd_stat[i].is_vcpu !=3D true) && + (thd_stat[i].delta_ticks > 0)) { + temp =3D vmsr_get_ratio(pkg_stat[thd_stat[i].pkg_id].e_del= ta, + thd_stat[i].delta_ticks, + vmsr->host_topo.maxticks[thd_stat[i].pkg_id]); + pkg_stat[thd_stat[i].pkg_id].e_ratio + +=3D (uint64_t)lround(temp); + } + } + + /* Calculate the ratio per non-vCPU thread of each package */ + for (int i =3D 0; i < vmsr->host_topo.maxpkgs; i++) { + if (pkg_stat[i].nb_vcpu > 0) { + pkg_stat[i].e_ratio =3D pkg_stat[i].e_ratio / pkg_stat[i].= nb_vcpu; + } + } + + /* + * Calculate the energy for each Package: + * Energy Package =3D sum of each vCPU energy that belongs to the = package + */ + for (int i =3D 0; i < num_threads; i++) { + double temp; + + if ((thd_stat[i].is_vcpu =3D=3D true) && \ + (thd_stat[i].delta_ticks > 0)) { + temp =3D vmsr_get_ratio(pkg_stat[thd_stat[i].pkg_id].e_del= ta, + thd_stat[i].delta_ticks, + vmsr->host_topo.maxticks[thd_stat[i].pkg_id]); + vpkgs_energy_stat[thd_stat[i].vpkg_id] +=3D + (uint64_t)lround(temp); + vpkgs_energy_stat[thd_stat[i].vpkg_id] +=3D + pkg_stat[thd_stat[i].pkg_id].e_ratio; + } + } + + /* + * Finally populate the vmsr register of each vCPU with the total + * package value to emulate the real hardware where each CPU retur= n the + * value of the package it belongs. + */ + for (int i =3D 0; i < num_threads; i++) { + if ((thd_stat[i].is_vcpu =3D=3D true) && \ + (thd_stat[i].delta_ticks > 0)) { + vmsr->msr_value[thd_stat[i].vcpu_id] =3D \ + vpkgs_energy_stat[thd_stat[i].vpkg= _id]; + } + } + + /* Freeing memory before zeroing the pointer */ + for (int i =3D 0; i < num_threads; i++) { + g_free(thd_stat[i].utime); + g_free(thd_stat[i].stime); + } + /* Zeroing the reused memory with g_renew */ + 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 =3D &s->msr_energy; + int ret =3D 0; + + /* + * Sanity check + * 1. Host cpu must be Intel cpu + * 2. RAPL must be enabled on the Host + */ + if (is_host_cpu_intel()) { + error_report("error: Host CPU is not INTEL cpu"); + ret =3D 1; + goto out; + } + + if (!is_rapl_enabled()) { + error_report("error: RAPL driver not enable on Host"); + ret =3D 1; + goto out; + } + + /* Retrieve the virtual topology */ + vmsr_init_topo_info(&r->topo_info, ms); + + /* Retrieve the number of vcpu */ + r->vcpus =3D ms->smp.cpus; + + /* Retrieve the number of virtual sockets */ + r->vsockets =3D ms->smp.sockets; + + /* Allocate register memory (MSR_PKG_STATUS) for each vcpu */ + r->msr_value =3D g_new0(uint64_t, r->vcpus); + + /* Retrieve the CPUArchIDlist */ + r->x86_cpu_list =3D x86_possible_cpu_arch_ids(ms); + + /* Max number of cpus on the Host */ + r->host_topo.maxcpus =3D vmsr_get_maxcpus(); + if (r->host_topo.maxcpus <=3D 0) { + error_report("error: host max cpus <=3D 0"); + ret =3D 1; + goto out; + } + + /* Max number of packages on the host */ + r->host_topo.maxpkgs =3D vmsr_get_max_physical_package(r->host_topo.ma= xcpus); + if (r->host_topo.maxpkgs <=3D 0) { + error_report("error: host max pkgs <=3D 0"); + ret =3D 1; + goto out; + } + + /* Allocate memory for each package on the host */ + r->host_topo.pkg_cpu_count =3D g_new0(unsigned int, r->host_topo.maxpk= gs); + r->host_topo.maxticks =3D g_new0(unsigned int, r->host_topo.maxpkgs); + + vmsr_count_cpus_per_package(r->host_topo.pkg_cpu_count, + r->host_topo.maxpkgs); + for (int i =3D 0; i < r->host_topo.maxpkgs; i++) { + if (r->host_topo.pkg_cpu_count[i] <=3D 0) { + error_report("error: cpu per packages <=3D 0 on package_%d", i= ); + ret =3D 1; + goto out; + } + } + + /* Get QEMU PID*/ + r->pid =3D getpid(); + + /* Those MSR values should not change */ + r->msr_unit =3D vmsr_read_msr(MSR_RAPL_POWER_UNIT, 0, r->pid, + s->msr_energy.socket_path); + r->msr_limit =3D vmsr_read_msr(MSR_PKG_POWER_LIMIT, 0, r->pid, + s->msr_energy.socket_path); + r->msr_info =3D vmsr_read_msr(MSR_PKG_POWER_INFO, 0, r->pid, + s->msr_energy.socket_path); + if (r->msr_unit =3D=3D 0 || r->msr_limit =3D=3D 0 || r->msr_info =3D= =3D 0) { + error_report("error: can't read any virtual msr"); + ret =3D 1; + goto out; + } + + qemu_thread_create(&r->msr_thr, "kvm-msr", + kvm_msr_energy_thread, + s, QEMU_THREAD_JOINABLE); +out: + return ret; +} + int kvm_arch_get_default_type(MachineState *ms) { return 0; @@ -2715,6 +3092,49 @@ int kvm_arch_init(MachineState *ms, KVMState *s) strerror(-ret)); exit(1); } + + if (s->msr_energy.enable =3D=3D true) { + r =3D 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 =3D 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 =3D 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 =3D 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); + } + r =3D kvm_msr_energy_thread_init(s, ms); + if (r) { + error_report("kvm : error RAPL feature requirement not mee= t"); + exit(1); + } + + } } =20 return 0; diff --git a/target/i386/kvm/meson.build b/target/i386/kvm/meson.build index 84d9143e6029..16010638df69 100644 --- a/target/i386/kvm/meson.build +++ b/target/i386/kvm/meson.build @@ -3,6 +3,7 @@ i386_kvm_ss =3D ss.source_set() i386_kvm_ss.add(files( 'kvm.c', 'kvm-cpu.c', + 'vmsr_energy.c', )) =20 i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c')) diff --git a/target/i386/kvm/vmsr_energy.c b/target/i386/kvm/vmsr_energy.c new file mode 100644 index 000000000000..20ac50c3f0b6 --- /dev/null +++ b/target/i386/kvm/vmsr_energy.c @@ -0,0 +1,381 @@ +/* + * QEMU KVM support -- x86 virtual RAPL msr + * + * Copyright 2024 Red Hat, Inc. 2024 + * + * Author: + * Anthony Harivel + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "vmsr_energy.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "hw/boards.h" +#include "cpu.h" +#include "host-cpu.h" + +static char *compute_default_paths(void) +{ + g_autofree char *state =3D qemu_get_local_state_dir(); + + return g_build_filename(state, "run", "qemu-vmsr-helper.sock", NULL); +} + +bool is_host_cpu_intel(void) +{ + int family, model, stepping; + char vendor[CPUID_VENDOR_SZ + 1]; + + host_cpu_vendor_fms(vendor, &family, &model, &stepping); + + return strcmp(vendor, CPUID_VENDOR_INTEL); +} + +int is_rapl_enabled(void) +{ + const char *path =3D "/sys/class/powercap/intel-rapl/enabled"; + FILE *file =3D fopen(path, "r"); + int value =3D 0; + + if (file !=3D NULL) { + if (fscanf(file, "%d", &value) !=3D 1) { + error_report("INTEL RAPL not enabled"); + } + fclose(file); + } else { + error_report("Error opening %s", path); + } + + return value; +} + +static int vmsr_helper_socket_write(QIOChannel *ioc, + int fd, + const void *buf, int sz, Error **errp) +{ + size_t nfds =3D (fd !=3D -1); + while (sz > 0) { + struct iovec iov; + ssize_t n_written; + + iov.iov_base =3D (void *)buf; + iov.iov_len =3D sz; + n_written =3D qio_channel_writev_full(QIO_CHANNEL(ioc), &iov, 1, + nfds ? &fd : NULL, nfds, 0, er= rp); + + if (n_written <=3D 0) { + assert(n_written !=3D QIO_CHANNEL_ERR_BLOCK); + object_unref(OBJECT(ioc)); + ioc =3D NULL; + return n_written < 0 ? -EINVAL : 0; + } + + nfds =3D 0; + buf +=3D n_written; + sz -=3D n_written; + } + + return 0; +} + +static int vmsr_helper_socket_read(QIOChannel *ioc, + void *buf, int sz, Error **errp) +{ + ssize_t r =3D qio_channel_read_all(ioc, buf, sz, errp); + + if (r < 0) { + object_unref(OBJECT(ioc)); + ioc =3D NULL; + return -EINVAL; + } + + return 0; +} + +uint64_t vmsr_read_msr(uint32_t reg, unsigned int cpu_id, uint32_t tid, + const char *path) +{ + uint64_t data =3D 0; + char *socket_path =3D NULL; + char buffer[3]; + + if (path =3D=3D NULL) { + socket_path =3D compute_default_paths(); + } else { + socket_path =3D g_strdup(path); + } + + SocketAddress saddr =3D { + .type =3D SOCKET_ADDRESS_TYPE_UNIX, + .u.q_unix.path =3D socket_path + }; + QIOChannelSocket *sioc =3D qio_channel_socket_new(); + Error *local_err =3D 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; + } + + /* + * Send the required arguments: + * 1. RAPL MSR register to read + * 2. On which CPU ID + * 3. From which vCPU (Thread ID) + */ + buffer[0] =3D reg; + buffer[1] =3D cpu_id; + buffer[2] =3D tid; + + r =3D vmsr_helper_socket_write(QIO_CHANNEL(sioc), + -1, + &buffer, sizeof(buffer), + &local_err); + if (r < 0) { + goto out_close; + } + + r =3D vmsr_helper_socket_read(QIO_CHANNEL(sioc), + &data, sizeof(data), + &local_err); + if (r < 0) { + data =3D 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 package */ +unsigned int vmsr_get_max_physical_package(unsigned int max_cpus) +{ + const char *dir =3D "/sys/devices/system/cpu/"; + const char *topo_path =3D "topology/physical_package_id"; + g_autofree int *uniquePackages =3D 0; + unsigned int packageCount =3D 0; + FILE *file =3D NULL; + + uniquePackages =3D g_new0(int, max_cpus); + + for (int i =3D 0; i < max_cpus; i++) { + g_autofree char *filePath =3D NULL; + g_autofree char *cpuid =3D g_strdup_printf("cpu%d", i); + + filePath =3D g_build_filename(dir, cpuid, topo_path, NULL); + + file =3D fopen(filePath, "r"); + + if (file =3D=3D NULL) { + error_report("Error opening physical_package_id file"); + return 0; + } + + char packageId[10]; + if (fgets(packageId, sizeof(packageId), file) =3D=3D NULL) { + packageCount =3D 0; + } + + fclose(file); + + int currentPackageId =3D atoi(packageId); + + bool isUnique =3D true; + for (int j =3D 0; j < packageCount; j++) { + if (uniquePackages[j] =3D=3D currentPackageId) { + isUnique =3D false; + break; + } + } + + if (isUnique) { + uniquePackages[packageCount] =3D currentPackageId; + packageCount++; + + if (packageCount >=3D max_cpus) { + break; + } + } + } + + return (packageCount =3D=3D 0) ? 1 : packageCount; +} + +/* Retrieve the max number of physical cpu on the host */ +unsigned int vmsr_get_maxcpus(void) +{ + GDir *dir; + const gchar *entry_name; + unsigned int cpu_count =3D 0; + const char *path =3D "/sys/devices/system/cpu/"; + + dir =3D g_dir_open(path, 0, NULL); + if (dir =3D=3D NULL) { + error_report("Unable to open cpu directory"); + return -1; + } + + while ((entry_name =3D g_dir_read_name(dir)) !=3D NULL) { + if (g_ascii_strncasecmp(entry_name, "cpu", 3) =3D=3D 0 && + isdigit(entry_name[3])) { + cpu_count++; + } + } + + g_dir_close(dir); + + return cpu_count; +} + +/* Count the number of physical cpu on each packages */ +unsigned int vmsr_count_cpus_per_package(unsigned int *package_count, + unsigned int max_pkgs) +{ + g_autofree char *file_contents =3D NULL; + g_autofree char *path =3D NULL; + gsize length; + + /* Iterate over cpus and count cpus in each package */ + for (int cpu_id =3D 0; ; cpu_id++) { + path =3D g_build_filename( + g_strdup_printf("/sys/devices/system/cpu/cpu%d/" + "topology/physical_package_id", cpu_id), NULL); + + if (!g_file_get_contents(path, &file_contents, &length, NULL)) { + break; /* No more cpus */ + } + + /* Get the physical package ID for this CPU */ + int package_id =3D atoi(file_contents); + + /* Check if the package ID is within the known number of packages = */ + if (package_id >=3D 0 && package_id < max_pkgs) { + /* If yes, count the cpu for this package*/ + package_count[package_id]++; + } + } + + return 0; +} + +/* Get the physical package id from a given cpu id */ +int vmsr_get_physical_package_id(int cpu_id) +{ + g_autofree char *file_contents =3D NULL; + g_autofree char *file_path =3D NULL; + int package_id =3D -1; + gsize length; + + file_path =3D g_strdup_printf("/sys/devices/system/cpu/cpu%d\ + /topology/physical_package_id", cpu_id); + + if (!g_file_get_contents(file_path, &file_contents, &length, NULL)) { + goto out; + } + + package_id =3D atoi(file_contents); + +out: + return package_id; +} + +/* Read the scheduled time for a given thread of a give pid */ +void vmsr_read_thread_stat(pid_t pid, + unsigned int thread_id, + unsigned long long *utime, + unsigned long long *stime, + unsigned int *cpu_id) +{ + g_autofree char *path =3D NULL; + + path =3D g_build_filename(g_strdup_printf("/proc/%u/task/%d/stat", pid= , \ + thread_id), NULL); + + FILE *file =3D fopen(path, "r"); + if (file =3D=3D NULL) { + pid =3D -1; + return; + } + + 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", + utime, stime, cpu_id) !=3D 3) + { + pid =3D -1; + return; + } + + fclose(file); + return; +} + +/* Read QEMU stat task folder to retrieve all QEMU threads ID */ +pid_t *vmsr_get_thread_ids(pid_t pid, unsigned int *num_threads) +{ + g_autofree char *path =3D g_build_filename("/proc", + g_strdup_printf("%d/task", pid), NULL); + + DIR *dir =3D opendir(path); + if (dir =3D=3D NULL) { + error_report("Error opening /proc/qemu/task"); + return NULL; + } + + pid_t *thread_ids =3D NULL; + unsigned int thread_count =3D 0; + + g_autofree struct dirent *ent =3D NULL; + while ((ent =3D readdir(dir)) !=3D NULL) { + if (ent->d_name[0] =3D=3D '.') { + continue; + } + pid_t tid =3D atoi(ent->d_name); + if (pid !=3D tid) { + thread_ids =3D g_renew(pid_t, thread_ids, (thread_count + 1)); + thread_ids[thread_count] =3D tid; + thread_count++; + } + } + + closedir(dir); + + *num_threads =3D thread_count; + return thread_ids; +} + +void vmsr_delta_ticks(thread_stat *thd_stat, int i) +{ + thd_stat[i].delta_ticks =3D (thd_stat[i].utime[1] + thd_stat[i].stime[= 1]) + - (thd_stat[i].utime[0] + thd_stat[i].stime[0]= ); +} + +double vmsr_get_ratio(uint64_t e_delta, + unsigned long long delta_ticks, + unsigned int maxticks) +{ + return (e_delta / 100.0) * ((100.0 / maxticks) * delta_ticks); +} + +void vmsr_init_topo_info(X86CPUTopoInfo *topo_info, + const MachineState *ms) +{ + topo_info->dies_per_pkg =3D ms->smp.dies; + topo_info->cores_per_die =3D ms->smp.cores; + topo_info->threads_per_core =3D ms->smp.threads; +} diff --git a/target/i386/kvm/vmsr_energy.h b/target/i386/kvm/vmsr_energy.h new file mode 100644 index 000000000000..e8aff1fabdfa --- /dev/null +++ b/target/i386/kvm/vmsr_energy.h @@ -0,0 +1,97 @@ +/* + * QEMU KVM support -- x86 virtual energy-related MSR. + * + * Copyright 2024 Red Hat, Inc. 2024 + * + * Author: + * Anthony Harivel + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef VMSR_ENERGY_H +#define VMSR_ENERGY_H + +#include "qemu/osdep.h" +#include +#include +#include "hw/i386/topology.h" + +/* + * Define the interval time in micro seconds between 2 samples of + * energy related MSRs + */ +#define MSR_ENERGY_THREAD_SLEEP_US 1000000.0 + +/* + * Thread statistic + * @ thread_id: TID (thread ID) + * @ is_vcpu: true if TID is vCPU thread + * @ cpu_id: CPU number last executed on + * @ pkg_id: package number of the CPU + * @ vcpu_id: vCPU ID + * @ vpkg: virtual package number + * @ acpi_id: APIC id of the vCPU + * @ utime: amount of clock ticks the thread + * has been scheduled in User mode + * @ stime: amount of clock ticks the thread + * has been scheduled in System mode + * @ delta_ticks: delta of utime+stime between + * the two samples (before/after sleep) + */ +struct thread_stat { + unsigned int thread_id; + bool is_vcpu; + unsigned int cpu_id; + unsigned int pkg_id; + unsigned int vpkg_id; + unsigned int vcpu_id; + unsigned long acpi_id; + unsigned long long *utime; + unsigned long long *stime; + unsigned long long delta_ticks; +}; + +/* + * Package statistic + * @ e_start: package energy counter before the sleep + * @ e_end: package energy counter after the sleep + * @ e_delta: delta of package energy counter + * @ e_ratio: store the energy ratio of non-vCPU thread + * @ nb_vcpu: number of vCPU running on this package + */ +struct package_energy_stat { + uint64_t e_start; + uint64_t e_end; + uint64_t e_delta; + uint64_t e_ratio; + unsigned int nb_vcpu; +}; + +typedef struct thread_stat thread_stat; +typedef struct package_energy_stat package_energy_stat; + +void vmsr_read_thread_stat(pid_t pid, + unsigned int thread_id, + unsigned long long *utime, + unsigned long long *stime, + unsigned int *cpu_id); + +uint64_t vmsr_read_msr(uint32_t reg, unsigned int cpu_id, + unsigned int tid, const char *path); +void vmsr_delta_ticks(thread_stat *thd_stat, int i); +unsigned int vmsr_get_maxcpus(void); +unsigned int vmsr_get_max_physical_package(unsigned int max_cpus); +unsigned int vmsr_count_cpus_per_package(unsigned int *package_count, + unsigned int max_pkgs); +int vmsr_get_physical_package_id(int cpu_id); +pid_t *vmsr_get_thread_ids(pid_t pid, unsigned int *num_threads); +double vmsr_get_ratio(uint64_t e_delta, + unsigned long long delta_ticks, + unsigned int maxticks); +void vmsr_init_topo_info(X86CPUTopoInfo *topo_info, const MachineState *ms= ); +bool is_host_cpu_intel(void); +int is_rapl_enabled(void); +#endif /* VMSR_ENERGY_H */ --=20 2.44.0