From nobody Wed Aug 26 00:03:42 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=1783113766; cv=none; d=zohomail.com; s=zohoarc; b=ZKCYlhmiDE8kgLyH/oUHl2ifNpxWowoIxbCVLti+ZF+10keyA3lnI1TZqe9B0cDhZzK+dBPuzpzZU2lrk7GKSEo8C2MIZOdFCG4qJQWJ8guMBiwIlkIXiibaQKCvVRBDucuXPNDh5UwdNBbF52pYjRBh32yRRd3hS76L2NQ1q+g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783113766; 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=sAJ+w5HRGJ1JE3H1Wx/huE4jGAY90IdO6mwCHpL6bJA=; b=PgIjYEb7wyYyqtlUeghZ0Qoth9wdMod37ZU2WpENIggPqWW9TurwOtVP+NJH3CKo4lEBIR6QCTJNbBOrkHHcft1S/EhkMLtBJcy/GDOtGbiLruuycjdJ7n+XIALyNRXx00jX4zkjI6TU/YzbSrdKD+GpPlHyCnp3t+nrMQ7RKSE= 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 1783113765930549.5241687945248; Fri, 3 Jul 2026 14:22:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1353757.1609514 (Exim 4.92) (envelope-from ) id 1wflKr-0006dH-GM; Fri, 03 Jul 2026 21:22:09 +0000 Received: by outflank-mailman (output) from mailman id 1353757.1609514; Fri, 03 Jul 2026 21:22:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflKr-0006aR-6H; Fri, 03 Jul 2026 21:22:09 +0000 Received: by outflank-mailman (input) for mailman id 1353757; Fri, 03 Jul 2026 21:22:06 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflKo-0005h7-Ju for xen-devel@lists.xenproject.org; Fri, 03 Jul 2026 21:22:06 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wflKj-00BXCb-0b; Fri, 03 Jul 2026 23:22:06 +0200 Received: from [10.42.69.4] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4827c7-2eae-0a2a0a5409dd-0a2a4504c5b0-26 for ; Fri, 03 Jul 2026 23:22:00 +0200 Received: from [90.155.50.34] (helo=casper.infradead.org) by tlsNG-ebf023.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4827f7-a01d-0a2a45040019-5a9b3222e448-3 for ; Fri, 03 Jul 2026 23:22:00 +0200 Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wflKX-0000000AsYC-0U5S; Fri, 03 Jul 2026 21:21:49 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.99.2 #2 (Red Hat Linux)) id 1wflKW-00000001RPD-46vV; 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=casper.20170209 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=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=sAJ+w5HRGJ1JE3H1Wx/huE4jGAY90IdO6mwCHpL6bJA=; b=fPnUdJ68VFN82oFjJj/i57tghy 0ZFD0hlumo0Eo/gYwzBln/e4uVYPLsis5GydyD/+Vy4w0rNcBsFWA+dW+Ch1zFGpDHSdlShsvrN6H 2XPhUzEfjC6LB0Tg162oU459VFP6GwWorQp5lrMES88+J5LfEdmILh5zqQipXPKBFQL+QF6Pjkixl 636TJb/z0HGrnDJ1GFdnnpOTdabYuoPygi84otBK2A9xaA55UpAOVjtSsMueaNRJPCWb+Nf+bWhfL UHPuZPEewOXB2c2BM4PG3/hV3K+2lePVnxjsArjeyD/3F2zSR5t6Hi71bD2Gy6MSpkMgA9oCaaUzU Dg0T/Ycw==; 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 21/36] KVM: x86: Replace nr_vcpus_matched_tsc count with all_vcpus_matched_tsc bool Date: Fri, 3 Jul 2026 22:18:00 +0100 Message-ID: <20260703212145.343527-22-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 casper.infradead.org. See http://www.infradead.org/rpr.html X-purgate-ID: tlsNG-ebf023/1783113720-AFD241CC-35407CC4/0/0 X-purgate-type: clean X-purgate-size: 2850 X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1783113766521158500 Content-Type: text/plain; charset="utf-8" From: David Woodhouse Using a count and comparing with kvm->online_vcpus was always racy because a new vCPU could be created while kvm_track_tsc_matching() was running and comparing with kvm->online_vcpus. That variable is only atomic with respect to itself; kvm_arch_vcpu_create() runs before kvm->online_vcpus is incremented for the new vCPU. Replace the count with a boolean that is set in kvm_track_tsc_matching() after comparing the count, and cleared when a new TSC generation starts. The boolean is consumed by pvclock_update_vm_gtod_copy() under the tsc_write_lock, which serializes against __kvm_synchronize_tsc(). Keep the count for now as it's still used in the trace event. Signed-off-by: David Woodhouse --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index e752b13c0809..51d8bcbc2d1d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1492,6 +1492,7 @@ struct kvm_arch { u64 cur_tsc_write; u64 cur_tsc_offset; u64 cur_tsc_generation; + bool all_vcpus_matched_tsc; int nr_vcpus_matched_tsc; =20 u32 default_tsc_khz; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 478147aff56a..83c47fe5c9b8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2651,8 +2651,10 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *= vcpu, bool new_generation) * and all vCPUs must have matching TSCs. Note, the count for matching * vCPUs doesn't include the reference vCPU, hence "+1". */ - bool use_master_clock =3D (ka->nr_vcpus_matched_tsc + 1 =3D=3D - atomic_read(&vcpu->kvm->online_vcpus)) && + ka->all_vcpus_matched_tsc =3D (ka->nr_vcpus_matched_tsc + 1 =3D=3D + atomic_read(&vcpu->kvm->online_vcpus)); + + bool use_master_clock =3D ka->all_vcpus_matched_tsc && gtod_is_based_on_tsc(gtod->clock.vclock_mode); =20 /* @@ -2837,6 +2839,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vc= pu, u64 offset, u64 tsc, kvm->arch.cur_tsc_write =3D tsc; kvm->arch.cur_tsc_offset =3D offset; kvm->arch.nr_vcpus_matched_tsc =3D 0; + kvm->arch.all_vcpus_matched_tsc =3D false; } else if (vcpu->arch.this_tsc_generation !=3D kvm->arch.cur_tsc_generati= on) { kvm->arch.nr_vcpus_matched_tsc++; } @@ -3169,8 +3172,7 @@ static void pvclock_update_vm_gtod_copy(struct kvm *k= vm) bool host_tsc_clocksource, vcpus_matched; =20 lockdep_assert_held(&kvm->arch.tsc_write_lock); - vcpus_matched =3D (ka->nr_vcpus_matched_tsc + 1 =3D=3D - atomic_read(&kvm->online_vcpus)); + vcpus_matched =3D ka->all_vcpus_matched_tsc; =20 /* * If the host uses TSC clock, then passthrough TSC as stable --=20 2.54.0