From nobody Wed Aug 26 15:53:29 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=1783113838; cv=none; d=zohomail.com; s=zohoarc; b=czouLIJIkzfMvwr0J7eE+hwfkX7jmaAadlfAI4uPkcNTlFaRng98+7gu40cggj2wuJiUTQhujPnkpfV/pN+PI5u+dhz+VXcl85BM5jOlXNY1onL2WmDhJDyL957gQOqUvyruo1bIU6Np7EXTTMQc4WNxtiD6yWwR17pfhssmPDQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783113838; 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=1OUv5CdFlAHAtCyTuuEwus8cGCX4Zb6KK8eBdOb5xl8=; b=ZxuODSdL8HI4sr0MxuGKsmR1aE1nM0/H+JQ0iFMqGnaS9lXOioJM2SpEG6Zj1Z4bWO2GZ1U1DAikjftIH8NZdq/9G8ei9eKEzND19Y+F0GNBH1PryuR2LF2xUjZXwVNLrmJ59R8YxusM0YhD2vGwU7JoMp4kRY8i0Z1LHZK5CE4= 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 1783113838436188.77296280045846; Fri, 3 Jul 2026 14:23:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1353878.1609618 (Exim 4.92) (envelope-from ) id 1wflMD-0005DE-6U; Fri, 03 Jul 2026 21:23:33 +0000 Received: by outflank-mailman (output) from mailman id 1353878.1609618; Fri, 03 Jul 2026 21:23:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflMD-0005D0-2w; Fri, 03 Jul 2026 21:23:33 +0000 Received: by outflank-mailman (input) for mailman id 1353878; Fri, 03 Jul 2026 21:23:31 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflMB-00052w-Ee for xen-devel@lists.xenproject.org; Fri, 03 Jul 2026 21:23:31 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wflMA-00BElj-Rp; Fri, 03 Jul 2026 23:23:30 +0200 Received: from [10.42.69.10] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a48282a-5cb7-0a2a0a5109dd-0a2a450aa8e8-10 for ; Fri, 03 Jul 2026 23:23:30 +0200 Received: from [90.155.92.199] (helo=desiato.infradead.org) by tlsNG-4011c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a48284f-e40e-0a2a450a0019-5a9b5cc7a18a-3 for ; Fri, 03 Jul 2026 23:23:27 +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 1wflKe-000000059OE-0v6i; Fri, 03 Jul 2026 21:22:53 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.99.2 #2 (Red Hat Linux)) id 1wflKX-00000001RPx-2LRG; Fri, 03 Jul 2026 22:21:49 +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=1OUv5CdFlAHAtCyTuuEwus8cGCX4Zb6KK8eBdOb5xl8=; b=iAY6MPfXrlFmZymwYSi+vVUVlO Foj75r55Pg0RY2rE1G43N+8fD9XfvQ9wvrkZ6IQFehekLM47aqnS2WyUEiaEq45e/K3+QqpL883g2 oxc0gIJzsIQslagkNwURfAqdrJBtEiZF1ADO/kBfNTsCiCu2whVOx6OV+YtpWXnWiJjHf/km1xwi8 BjZeLG8R28itKIdJU2ORDJdGL/twrFkXyZF4cfcv/Ls01m6xV3PS2mcL+rBFHVol6Gi9uDe/rCWHY weB/t2G31ola0QBkAmIxp7fDT6IzQ5OkKGjhGdZVLn5cAiSUfsWQGKBUU3K3pD2jWVTS4EmGKfQAR jmWut/Bw==; 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 32/36] KVM: x86: Use ktime_get_snapshot_id() for master clock Date: Fri, 3 Jul 2026 22:18:11 +0100 Message-ID: <20260703212145.343527-33-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-4011c0/1783113807-CF537DDE-DC71D870/0/0 X-purgate-type: clean X-purgate-size: 3954 X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1783113838999158500 Content-Type: text/plain; charset="utf-8" From: David Woodhouse Replace the KVM-private vgettsc()/do_kvmclock_base()/do_monotonic()/ do_realtime() timekeeping reimplementation with calls to the generic ktime_get_snapshot_id() interface. The snapshot provides both the system time and the raw_cycles (TSC) atomically paired. When raw_cycles is zero, the clocksource could not provide a raw hardware counter value, which is equivalent to the previous vgettsc() returning VDSO_CLOCKMODE_NONE. For kvm_get_time_and_clockread(), the kvmclock base time is CLOCK_MONOTONIC_RAW + offs_boot. The snapshot provides the raw time atomically paired with the TSC; offs_boot is added separately as it only changes at suspend/resume boundaries. This is a step towards eliminating the pvclock_gtod_data private copy of timekeeping state and the associated notifier callback. Signed-off-by: David Woodhouse Assisted-by: Kiro:claude-opus-4.6-1m --- arch/x86/kvm/x86.c | 53 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 37a768b2fe16..56afd5e66c06 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -35,6 +35,7 @@ #include "smm.h" =20 #include +#include #include #include #include @@ -3154,14 +3155,39 @@ static int do_realtime(struct timespec64 *ts, u64 *= tsc_timestamp) * reports the TSC value from which it do so. Returns true if host is * using TSC based clocksource. */ +static bool kvm_snapshot_has_tsc(struct system_time_snapshot *snap, + u64 *tsc_timestamp) +{ + /* + * ktime_get_snapshot_id() cannot fail for standard clock IDs + * (only for invalid/aux clocks or during suspend, with a WARN). + */ + if (!snap->valid) + return false; + + if (snap->cs_id =3D=3D CSID_X86_TSC) { + *tsc_timestamp =3D snap->cycles; + return true; + } + + if (snap->hw_csid =3D=3D CSID_X86_TSC && snap->hw_cycles) { + *tsc_timestamp =3D snap->hw_cycles; + return true; + } + + return false; +} + static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_MONOTONIC_RAW, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_kvmclock_base(kernel_ns, - tsc_timestamp)); + *kernel_ns =3D ktime_to_ns(ktime_mono_to_any(snap.systime, TK_OFFS_BOOT)); + return true; } =20 /* @@ -3170,12 +3196,14 @@ static bool kvm_get_time_and_clockread(s64 *kernel_= ns, u64 *tsc_timestamp) */ bool kvm_get_monotonic_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_MONOTONIC, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_monotonic(kernel_ns, - tsc_timestamp)); + *kernel_ns =3D ktime_to_ns(snap.systime); + return true; } =20 /* @@ -3188,11 +3216,14 @@ bool kvm_get_monotonic_and_clockread(s64 *kernel_ns= , u64 *tsc_timestamp) static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_REALTIME, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); + *ts =3D ktime_to_timespec64(snap.systime); + return true; } #endif =20 --=20 2.54.0