From nobody Sat Oct 4 15:55:17 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D3B12EBBAC; Thu, 14 Aug 2025 12:03:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755172994; cv=none; b=OpDfXyCCk8CFoDRKK+QhjbMvlZzbzpQFSa5FhsSmZ1+0IkbyArcUPncEXbwZNbmshnM3sgqjkLoKkIwxEeq/cjpYGFmNIjTJXGCj4atp5tFiZjVcto+NLEJ3yg4vChmUBY/Y0Qo/J/8WegHPj7HquLVZXZ8j/j4hi55dbZ//k6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755172994; c=relaxed/simple; bh=Uk0cXH8F2Qchs59LmWckrLh9k+2D2InGvd+K6U+WQ5U=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=no7VveQwtYs+5wgUxGslgurbcGJSgEo2WaiF5Ur2Wl6khB7y/GA4ffG3WZ0TIQMdKlRUiqpkGosQoRUogsaiTV8seJcGujdy3xiA3GihU1zJCJEge31/46OkA3u9Wz/Es2m7T9VDIaERFNWG4uWl7dENPvgyo6uAU1tRust7uns= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=Y9MbNak6; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Y9MbNak6" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description; bh=IP9zfjZjBpKjNLU5g3/LbX3rTsuu30U8uTB+FpB+6ug=; b=Y9MbNak6vMIQ2x0NWfyt0TLZTu +N1fI/uCk5lvS9oV5WhpRWbdacic58afgKvnxwzZs1w1Q6kmMPVgZnnZNxsm4uPDYqK3GnuyW4tTF 5ldLEoBs7w/hMbCQCwd8Wc3MaY/0i4cFuvpi+ZC5Rig+ZUh/ojVXX3GdRzQEA4xid7Mt1y/oc3r5q euQDSfVrNMW/HCDJndAnpErWCZMXx3ejVcFQO28VtAAG14rTuKnkKyxvxmxd3+/tCIlr8FpbLYMWC 77IAobTCqAMfcF1isLVq2bPPpqnjFRU73Z71uQgVmXbj+IMRufzHWcrVxPIZ3at3iKaf+7MOpuOgS IRA66CpQ==; Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1umWfd-00000000IZv-1LUu; Thu, 14 Aug 2025 12:03:02 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1umWfd-0000000AMTj-1ITv; Thu, 14 Aug 2025 13:03:01 +0100 From: David Woodhouse To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, graf@amazon.de, Ajay Kaher , Alexey Makhalov , Alok N Kataria Subject: [PATCH 1/3] KVM: x86: Restore caching of KVM CPUID base Date: Thu, 14 Aug 2025 12:56:03 +0100 Message-ID: <20250814120237.2469583-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250814120237.2469583-1-dwmw2@infradead.org> References: <20250814120237.2469583-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse This mostly reverts commit a5b32718081e ("KVM: x86: Remove unnecessary caching of KVM's PV CPUID base"). Sure, caching state which might change has certain risks, but KVM already does cache the CPUID contents, and the whole point of calling kvm_apply_cpuid_pv_features_quirk() from kvm_vcpu_after_set_cpuid() is to cache the contents of that leaf too, so that guest_pv_has() can access them quickly. An upcoming commit is going to want to use vcpu->arch.kvm_cpuid from kvm_cpuid() at runtime too, so put it back. Signed-off-by: David Woodhouse Tested-by: syzbot@syzkaller.appspotmail.com --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index f19a76d3ca0e..50febd333f5f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -897,6 +897,7 @@ struct kvm_vcpu_arch { =20 int cpuid_nent; struct kvm_cpuid_entry2 *cpuid_entries; + struct kvm_hypervisor_cpuid kvm_cpuid; bool cpuid_dynamic_bits_dirty; bool is_amd_compatible; =20 diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index e2836a255b16..bcce3a75c3f2 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -178,7 +178,12 @@ static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu= , struct kvm_cpuid_entry2 =20 /* * Apply runtime CPUID updates to the incoming CPUID entries to avoid - * false positives due mismatches on KVM-owned feature flags. + * false positives due mismatches on KVM-owned feature flags. Note, + * runtime CPUID updates may consume other CPUID-driven vCPU state, + * e.g. KVM or Xen CPUID bases. Updating runtime state before full + * CPUID processing is functionally correct only because any change in + * CPUID is disallowed, i.e. using stale data is ok because the below + * checks will reject the change. * * Note! @e2 and @nent track the _old_ CPUID entries! */ @@ -231,14 +236,14 @@ static struct kvm_hypervisor_cpuid kvm_get_hypervisor= _cpuid(struct kvm_vcpu *vcp =20 static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu) { - struct kvm_hypervisor_cpuid kvm_cpuid; struct kvm_cpuid_entry2 *best; + u32 features_leaf =3D vcpu->arch.kvm_cpuid.base | KVM_CPUID_FEATURES; =20 - kvm_cpuid =3D kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); - if (!kvm_cpuid.base) + if (!vcpu->arch.kvm_cpuid.base || + vcpu->arch.kvm_cpuid.limit < features_leaf) return 0; =20 - best =3D kvm_find_cpuid_entry(vcpu, kvm_cpuid.base | KVM_CPUID_FEATURES); + best =3D kvm_find_cpuid_entry(vcpu, features_leaf); if (!best) return 0; =20 @@ -541,6 +546,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct = kvm_cpuid_entry2 *e2, if (r) goto err; =20 + vcpu->arch.kvm_cpuid =3D kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); #ifdef CONFIG_KVM_XEN vcpu->arch.xen.cpuid =3D kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE); #endif --=20 2.49.0 From nobody Sat Oct 4 15:55:17 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45BAE2EBBAC; Thu, 14 Aug 2025 12:03:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755172999; cv=none; b=V3lMM2ckHp9NuGXxOo77BRN2wbd9cnNdfdxy9HtVenCrnZOs3QPlaXiv9w7ibRACp+2GCCER7suEMYg0tTzhQVLKPtf7hEkIWbc/gGZAfLPWIFXF/RvdF6kr4HArNUy5T4eytYykudr3o/QRPOPdj0jgBYaM6VIxWOTPyvXfGKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755172999; c=relaxed/simple; bh=VISw+hcde9qZ1n3g0CEwLcclTN2nmaRHfPekI3FOWQg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dZ/l+BgOHNWzYZvlIG7He+KO7mYl/S/umD0CAOF/BfRYsG+skqOuB9Y3GbPdDsE2fWdrk/ubn2LxU2cecWdHbU9TMm/OqAc/CgY1Cbyqs6CwtPNJ13uzVVT/4la+uxI80D/6Azvk9Q5jdF4L9KijDLn8deDOk9/SEEvjdczpxak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=Q7DJ5d3l; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Q7DJ5d3l" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description; bh=BjEXtZI4JMihCuWWOH01u+3jFj7nMQzbdSm5sr6kODQ=; b=Q7DJ5d3lPW78Q+RjTZY0LfCfNk x038+qg8PLaZgBpT47tbxxyXWPXIYg/fnzbqvspRACaaNCzoD+FlkfsWUoM0/NbrlpnP/1qsIQ+rG ppQDR0nipYQPzZi37vMPlJV83m7OIn9dSysEYgAVsZFXk2G92MgU8VC5CeEmLylg5A423KLdL5c+P enFkBCpv0ThvaXDt7xLfWXy48YgOaySJWVNRsT822Knt9GSLHOPAEHRZu9LC/63HTgbBDAXpz8WPE tAiJNlxRPpkR/Gsea1jL8n70fRqD8k4PNW7JkH1sggrfgFvMWwppylj4t8+j8ap2q3O3RKYLVAnyv YymTjasQ==; Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1umWfe-0000000GNRJ-3vse; Thu, 14 Aug 2025 12:03:03 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1umWfd-0000000AMTn-1aWA; Thu, 14 Aug 2025 13:03:01 +0100 From: David Woodhouse To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, graf@amazon.de, Ajay Kaher , Alexey Makhalov , Alok N Kataria Subject: [PATCH 2/3] KVM: x86: Provide TSC frequency in "generic" timing infomation CPUID leaf Date: Thu, 14 Aug 2025 12:56:04 +0100 Message-ID: <20250814120237.2469583-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250814120237.2469583-1-dwmw2@infradead.org> References: <20250814120237.2469583-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse In https://lkml.org/lkml/2008/10/1/246 a proposal was made for generic CPUID leaves, of which only 0x40000010 was defined, to contain the TSC and local APIC frequencies. The proposal from VMware was mostly shot down in flames, *but* XNU does unconditionally assume that this leaf contains the frequency information, if it's present on any hypervisor: https://github.com/apple/darwin-xnu/blob/main/osfmk/i386/cpuid.c So does FreeBSD: https://github.com/freebsd/freebsd-src/commit/4a432614f68 So at this point it would be daft for a hypervisor to expose 0x40000010 for any *other* content. KVM might as well adopt it, and fill in the accurate TSC frequency just as it does for the Xen TSC leaf. Signed-off-by: David Woodhouse Tested-by: syzbot@syzkaller.appspotmail.com --- arch/x86/include/uapi/asm/kvm_para.h | 11 +++++++++++ arch/x86/kvm/cpuid.c | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/a= sm/kvm_para.h index a1efa7907a0b..1597c4a2a24a 100644 --- a/arch/x86/include/uapi/asm/kvm_para.h +++ b/arch/x86/include/uapi/asm/kvm_para.h @@ -44,6 +44,17 @@ */ #define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24 =20 + +/* + * Proposed by VMware in https://lkml.org/lkml/2008/10/1/246 the timing + * information leaf provides the TSC and local APIC timer frequencies: + * + * # EAX: (Virtual) TSC frequency in kHz. + * # EBX: (Virtual) Bus (local apic timer) frequency in kHz. + * # ECX, EDX: RESERVED (reserved fields are set to zero). + */ +#define KVM_CPUID_TIMING_INFO 0x40000010 + #define MSR_KVM_WALL_CLOCK 0x11 #define MSR_KVM_SYSTEM_TIME 0x12 =20 diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index bcce3a75c3f2..1bd69d9c86b7 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -2029,6 +2029,13 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 = *ebx, } else if (index =3D=3D 2) { *eax =3D vcpu->arch.hw_tsc_khz; } + } else if (vcpu->arch.kvm_cpuid.base && + function <=3D vcpu->arch.kvm_cpuid.limit && + function =3D=3D (vcpu->arch.kvm_cpuid.base | KVM_CPUID_TIMING_INFO))= { + if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) + kvm_guest_time_update(vcpu); + + *eax =3D vcpu->arch.hw_tsc_khz; } } else { *eax =3D *ebx =3D *ecx =3D *edx =3D 0; --=20 2.49.0 From nobody Sat Oct 4 15:55:17 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D34E2727FC; Thu, 14 Aug 2025 12:03:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755172994; cv=none; b=Q1H0ae06V5/jMij7lbYEWr0cIOBayIJSRI0bUBUes6AvCMn7a77J1jxRl7SxN1DrURrGATV+IdfLekkVktmrN8UpJm321i6gJ7Bs3DOxXOxReSJ3HM07dKibUYtOj5tZYb49aFA7UyEYQulvx9uzA8YL8967mP/M00D/EC9HgVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755172994; c=relaxed/simple; bh=Xv927clxzvv7etIdOV5tAySmi+ZdvMYf+t8s6rS2Duo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hRNE7ewt0ye2PFMUIeOQmAMKrchqLD4lBgCe+qjunaf/APwqpPb/4RyyxFPKWGSSIFaVPzZwmvC3c/0O2RsYFwZznJgQU2ykco8qxsg0Cto2cRvlZMvQSld7Ficrlkq2/yVzUyRwhWgud22Bcmdr4V3MTkUkdEDWcCC9G8qquS4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=Ew59hpzk; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Ew59hpzk" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To: From:Reply-To:Cc:Content-ID:Content-Description; bh=mGS5KD4K3Hl92P1KTfNmSqYsreyv1JVai3wachOAJC4=; b=Ew59hpzkFKy5/CCOhMlmgv5Rhn YQq+zfS+bfn8QqRkS7i8EsCvK2qSxfKizAobTAF9KvgvWy5rSbl1MuEQXsp/yFRQznEvxeChBleb4 Q9En+X/DzXwgzp5lOK0q/6dvcfVEn549Z4yCoNZff+/5cpUs1HvpcI+jWOWZAuvfNW71nlIROzTO0 qjLyogW6KoUViwqOiyJD4x9uOJvxXfcbBZ0epx6i4jeZY0Nt2fcEkZafmrSU3cS0JYsLJ0vfsCAlI Z3uX/UkmJnd5jKICzgNBbakzPBU/8nXiR0Z0QEp0rHRnNPuF4M1J+JnZPiSFCrPjZ+puWvsHyCt7R fUH9zaFg==; Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1umWfd-00000000Ia4-1vq5; Thu, 14 Aug 2025 12:03:02 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1umWfd-0000000AMTr-1hl9; Thu, 14 Aug 2025 13:03:01 +0100 From: David Woodhouse To: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, graf@amazon.de, Ajay Kaher , Alexey Makhalov , Alok N Kataria Subject: [PATCH 3/3] x86/kvm: Obtain TSC frequency from CPUID if present Date: Thu, 14 Aug 2025 12:56:05 +0100 Message-ID: <20250814120237.2469583-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250814120237.2469583-1-dwmw2@infradead.org> References: <20250814120237.2469583-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse In https://lkml.org/lkml/2008/10/1/246 a proposal was made for generic CPUID conventions across hypervisors. It was mostly shot down in flames, but the leaf at 0x40000010 containing timing information didn't die. It's used by XNU and FreeBSD guests under all hypervisors=C2=B9=C2=B2 to de= termine the TSC frequency, and also exposed by the EC2 Nitro hypervisor (as well as, presumably, VMware). FreeBSD's Bhyve is probably just about to start exposing it too. Use it under KVM to obtain the TSC frequency more accurately, instead of reverse-calculating the frequency from the mul/shift values in the KVM clock. Before: [ 0.000020] tsc: Detected 2900.014 MHz processor After: [ 0.000020] tsc: Detected 2900.015 MHz processor $ cpuid -1 -l 0x40000010 CPU: hypervisor generic timing information (0x40000010): TSC frequency (Hz) =3D 2900015 bus frequency (Hz) =3D 1000000 =C2=B9 https://github.com/apple/darwin-xnu/blob/main/osfmk/i386/cpuid.c =C2=B2 https://github.com/freebsd/freebsd-src/commit/4a432614f68 Signed-off-by: David Woodhouse Tested-by: syzbot@syzkaller.appspotmail.com --- arch/x86/include/asm/kvm_para.h | 1 + arch/x86/kernel/kvm.c | 10 ++++++++++ arch/x86/kernel/kvmclock.c | 7 ++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_par= a.h index 57bc74e112f2..d53927103cab 100644 --- a/arch/x86/include/asm/kvm_para.h +++ b/arch/x86/include/asm/kvm_para.h @@ -121,6 +121,7 @@ static inline long kvm_sev_hypercall3(unsigned int nr, = unsigned long p1, void kvmclock_init(void); void kvmclock_disable(void); bool kvm_para_available(void); +unsigned int kvm_para_tsc_khz(void); unsigned int kvm_arch_para_features(void); unsigned int kvm_arch_para_hints(void); void kvm_async_pf_task_wait_schedule(u32 token); diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 8ae750cde0c6..1a80f4e5c854 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -896,6 +896,16 @@ bool kvm_para_available(void) } EXPORT_SYMBOL_GPL(kvm_para_available); =20 +unsigned int kvm_para_tsc_khz() +{ + u32 base =3D kvm_cpuid_base(); + + if (cpuid_eax(base) >=3D (base | KVM_CPUID_TIMING_INFO)) + return cpuid_eax(base | KVM_CPUID_TIMING_INFO); + + return 0; +} + unsigned int kvm_arch_para_features(void) { return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES); diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index ca0a49eeac4a..0908450ebac9 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -117,7 +117,12 @@ static inline void kvm_sched_clock_init(bool stable) static unsigned long kvm_get_tsc_khz(void) { setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); - return pvclock_tsc_khz(this_cpu_pvti()); + + /* + * If KVM advertises the frequency directly in CPUID, use that + * instead of reverse-calculating it from the KVM clock data. + */ + return kvm_para_tsc_khz() ? : pvclock_tsc_khz(this_cpu_pvti()); } =20 static void __init kvm_get_preset_lpj(void) --=20 2.49.0