From nobody Mon May 6 17:32:10 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1628097110; cv=none; d=zohomail.com; s=zohoarc; b=iwQt9KMB6rQGANWvamEQtxIcTbrg6lIm0ZqmJKQb8s6eOjKhEBVM7/ZtZdNT6xXkA40/xOKMY+vPY2yvKBGcIZ81B2uu3z7Shbc+ldc0FCYuf2BXdSTmBsxNahIdlnKvlSacP2ZCDj8KywLZ09pW/Bbt1JEiOJEws1UQQRzalns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628097110; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=mtUQiDEDAu23JVfv46Gj2D/ZCaaODO0pEgVktSZfGJY=; b=YeQ9EWDZlx5LxoZe311571g87qWkOb+XIGRuWBekRBhYpY518PE6oOrypvxrAsVbY8wNXDS+ZjwnLhqwIDobwadA32oyFcqM4s44r/k0SdXCjP1dzOTpy9ZFa+YIGHmj2i2U0qEtUIlwE7w4KfJUgu1vm/N1IRL7mY5R4TbjUN0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1628097110309979.2001780771126; Wed, 4 Aug 2021 10:11:50 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-595-ODOG4swvN9euw5V4Mv4_xw-1; Wed, 04 Aug 2021 13:11:45 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 862091007383; Wed, 4 Aug 2021 17:11:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0E1D75D9C6; Wed, 4 Aug 2021 17:11:39 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 061EF4BB7C; Wed, 4 Aug 2021 17:11:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 174H9nZj004803 for ; Wed, 4 Aug 2021 13:09:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id AE95260C0F; Wed, 4 Aug 2021 17:09:49 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.216]) by smtp.corp.redhat.com (Postfix) with ESMTP id BB3DB60BF4; Wed, 4 Aug 2021 17:09:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628097108; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=mtUQiDEDAu23JVfv46Gj2D/ZCaaODO0pEgVktSZfGJY=; b=g9v2UnhrLKyxgMB+q8kmw6ZM19fju+8I4HIMazFHBykibqiNxjxxEaIV3600O94FTGrqQE amGHMLyZBloNJ+9lgCdhMS5Majenob1KUGATxqFN1GVlkSLxr0KGE3cXz/uqI0LbWKEl+7 KmgZ6XAhsmC+7hKBKLCZpbhrWIWh1Ss= X-MC-Unique: ODOG4swvN9euw5V4Mv4_xw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [PATCH] util: directly query KVM for TSC scaling support Date: Wed, 4 Aug 2021 18:09:42 +0100 Message-Id: <20210804170942.3449396-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Roman Mohr X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1628097111024100001 We currently query the host MSRs to determine if TSC scaling is supported. This works OK when running privileged and can open the /dev/cpu/0/msr. When unprivileged we fallback to querying MSRs from /dev/kvm. This is incorrect because /dev/kvm only reports accurate info for MSRs that are valid to use from inside a guest. The TSC scaling support MSR is not, thus we always end up reporting lack of TSC scaling when unprivileged. The solution to this is easy, because KVM can directly report whether TSC scaling is available, which matches what QEMU will do at startup. https://gitlab.com/libvirt/libvirt/-/issues/188 Reported-by: Roman Mohr Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/util/virhostcpu.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index 7aa92ad11d..f140610b47 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -1349,11 +1349,10 @@ virHostCPUGetMSR(unsigned long index, virHostCPUTscInfo * virHostCPUGetTscInfo(void) { - virHostCPUTscInfo *info; + g_autofree virHostCPUTscInfo *info =3D g_new0(virHostCPUTscInfo, 1); VIR_AUTOCLOSE kvmFd =3D -1; VIR_AUTOCLOSE vmFd =3D -1; VIR_AUTOCLOSE vcpuFd =3D -1; - uint64_t msr =3D 0; int rc; =20 if ((kvmFd =3D open(KVM_DEVICE, O_RDONLY)) < 0) { @@ -1378,23 +1377,19 @@ virHostCPUGetTscInfo(void) _("Unable to probe TSC frequency")); return NULL; } - - info =3D g_new0(virHostCPUTscInfo, 1); - info->frequency =3D rc * 1000ULL; =20 - if (virHostCPUGetMSR(VMX_PROCBASED_CTLS2_MSR, &msr) =3D=3D 0) { - /* High 32 bits of the MSR value indicate whether specific control - * can be set to 1. */ - msr >>=3D 32; - - info->scaling =3D virTristateBoolFromBool(!!(msr & VMX_USE_TSC_SCA= LING)); + if ((rc =3D ioctl(kvmFd, KVM_CHECK_EXTENSION, KVM_CAP_TSC_CONTROL)) < = 0) { + virReportSystemError(errno, "%s", + _("Unable to query TSC scaling support")); + return NULL; } + info->scaling =3D rc ? VIR_TRISTATE_BOOL_YES : VIR_TRISTATE_BOOL_NO; =20 VIR_DEBUG("Detected TSC frequency %llu Hz, scaling %s", info->frequency, virTristateBoolTypeToString(info->scaling)); =20 - return info; + return g_steal_pointer(&info); } =20 #else --=20 2.31.1