From nobody Wed Aug 26 21:49:36 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=infradead.org ARC-Seal: i=1; a=rsa-sha256; t=1783113847; cv=none; d=zohomail.com; s=zohoarc; b=OvgCso3G7yynCP4WTdBDDX/mVKYtJqQfkbekhHu6xHzoDbr2dd5SEyzjmnSr+yE6B8Wcu36d936uNP+6wcvLKP8kR1c56w/qE5KWIP45iQromn2lJK/2v2e/jlARzVNTYs+aMnVC6cIvNRXqn+pSDgeqj5+WHGGopd55cSx9gtk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783113847; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=B9z9WHPmvLHevYFOtmsj0qG2r63h9YkhzzxvRZTeAmQ=; b=LvYC9pUpNg+p6LutyU6HiHv9QC9mwZb5hf85ap9RXGEbXNwExsB4YEHGRKvSfVqIqAqs9ejiQ1iS0au2+aQFGOaOzkm42jGgrohVctFR4EtQ+/6y5c2Ywui1z9ulWuNpPVZfG/UCtUExeO5RSHa2BEHp2lo4glZAt/+bGDpdM3o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1783113847667758.3790450654755; Fri, 3 Jul 2026 14:24:07 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1353883.1609632 (Exim 4.92) (envelope-from ) id 1wflMM-0005kO-Un; Fri, 03 Jul 2026 21:23:42 +0000 Received: by outflank-mailman (output) from mailman id 1353883.1609632; Fri, 03 Jul 2026 21:23:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflMM-0005jN-PI; Fri, 03 Jul 2026 21:23:42 +0000 Received: by outflank-mailman (input) for mailman id 1353883; Fri, 03 Jul 2026 21:23:42 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflMM-0005fl-0s for xen-devel@lists.xenproject.org; Fri, 03 Jul 2026 21:23:42 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wflML-00A83n-Dv; Fri, 03 Jul 2026 23:23:41 +0200 Received: from [10.42.69.2] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a482855-e002-0a2a0a5209dd-0a2a450295e8-8 for ; Fri, 03 Jul 2026 23:23:41 +0200 Received: from [90.155.92.199] (helo=desiato.infradead.org) by tlsNG-720697.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a48285c-5a27-0a2a45020019-5a9b5cc7c936-3 for ; Fri, 03 Jul 2026 23:23:41 +0200 Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.99.2 #2 (Red Hat Linux)) id 1wflKc-000000059O2-31Bq; Fri, 03 Jul 2026 21:23:04 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.99.2 #2 (Red Hat Linux)) id 1wflKW-00000001ROh-26Xn; Fri, 03 Jul 2026 22:21:48 +0100 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=desiato.20200630 header.d=infradead.org header.i="@infradead.org" header.h="Sender:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From" 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=B9z9WHPmvLHevYFOtmsj0qG2r63h9YkhzzxvRZTeAmQ=; b=onaWDNN2jSP0Q3PZPJs7nogpsV djN5zQDEAAEwp07PHEgjWunIEq3y03q0c8S7zV2OcMlYhl/J34JEKZ7hDMXXyEtTrI/81rwJ8Q8gM QFTPzOTXCK8UXssjTS9zoGTX9rQa4JMpnl9rJ36ID3INFYr2lBLrrofRd04iiPjaseA5YUq4XVSN+ P9NXmp10mCYVX7lINntbmHveElZKC1lFoVpWLbMkv65zJAIwkRzxBWGO+p0yyYJDfQF/6Kweqcw6j YMLtCZsL3NEMhJ26U9Dc5d2d7RIA/mGXd6lQ4sqAxNmH6Bvcz9akyjmHc63sIuT7ieyCDZ6azwkhU pCaM9FxA==; From: David Woodhouse To: Paolo Bonzini , Jonathan Corbet , Shuah Khan , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , David Woodhouse , Paul Durrant , Jonathan Cameron , Sascha Bischoff , Marc Zyngier , Joey Gouly , Jack Allister , Dongli Zhang , joe.jin@oracle.com, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, linux-kselftest@vger.kernel.org Subject: [PATCH v6 14/36] KVM: x86: Use get_kvmclock() in kvm_get_wall_clock_epoch() Date: Fri, 3 Jul 2026 22:17:53 +0100 Message-ID: <20260703212145.343527-15-dwmw2@infradead.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260703212145.343527-1-dwmw2@infradead.org> References: <20260703212145.343527-1-dwmw2@infradead.org> 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 X-purgate-ID: tlsNG-720697/1783113821-7751C7C5-1868F5BF/0/0 X-purgate-type: clean X-purgate-size: 3112 X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1783113848897158500 Content-Type: text/plain; charset="utf-8" From: David Woodhouse Now that get_kvmclock() correctly handles TSC scaling and captures both wallclock and kvmclock from the same TSC reading, kvm_get_wall_clock_epoch() can simply call it instead of duplicating the pvclock computation. This eliminates the last instance of the "definition C" kvmclock calculation that computed nanoseconds directly from the host TSC without accounting for guest TSC scaling. Signed-off-by: David Woodhouse --- arch/x86/kvm/x86.c | 59 +++++++--------------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 65aa0fb0b5e1..376b8dc2ade9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3480,63 +3480,22 @@ int kvm_guest_time_update(struct kvm_vcpu *v) * wallclock and kvmclock times, and subtracting one from the other. * * Fall back to using their values at slightly different moments by - * calling ktime_get_real_ns() and get_kvmclock_ns() separately. + * calling ktime_get_real_ns() and get_kvmclock() separately. */ uint64_t kvm_get_wall_clock_epoch(struct kvm *kvm) { -#ifdef CONFIG_X86_64 - struct pvclock_vcpu_time_info hv_clock; - struct kvm_arch *ka =3D &kvm->arch; - unsigned long seq, local_tsc_khz; - struct timespec64 ts; - uint64_t host_tsc; - - do { - seq =3D read_seqcount_begin(&ka->pvclock_sc); - - local_tsc_khz =3D 0; - if (!ka->use_master_clock) - break; - - /* - * The TSC read and the call to get_cpu_tsc_khz() must happen - * on the same CPU. - */ - get_cpu(); - - local_tsc_khz =3D get_cpu_tsc_khz(); - - if (local_tsc_khz && - !kvm_get_walltime_and_clockread(&ts, &host_tsc)) - local_tsc_khz =3D 0; /* Fall back to old method */ - - put_cpu(); - - /* - * These values must be snapshotted within the seqcount loop. - * After that, it's just mathematics which can happen on any - * CPU at any time. - */ - hv_clock.tsc_timestamp =3D ka->master_cycle_now; - hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; + struct kvm_clock_data data; =20 - } while (read_seqcount_retry(&ka->pvclock_sc, seq)); + get_kvmclock(kvm, &data); =20 /* - * If the conditions were right, and obtaining the wallclock+TSC was - * successful, calculate the KVM clock at the corresponding time and - * subtract one from the other to get the guest's epoch in nanoseconds - * since 1970-01-01. + * If get_kvmclock() captured both wallclock and kvmclock from the + * same TSC reading, use them for a precise epoch calculation. */ - if (local_tsc_khz) { - kvm_get_time_scale(NSEC_PER_SEC, local_tsc_khz * NSEC_PER_USEC, - &hv_clock.tsc_shift, - &hv_clock.tsc_to_system_mul); - return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - - __pvclock_read_cycles(&hv_clock, host_tsc); - } -#endif - return ktime_get_real_ns() - get_kvmclock_ns(kvm); + if (data.flags & KVM_CLOCK_REALTIME) + return data.realtime - data.clock; + + return ktime_get_real_ns() - data.clock; } =20 /* --=20 2.54.0