All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marco Elver <elver@google.com>
To: elver@google.com
Cc: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Thomas Gleixner <tglx@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org,  "H. Peter Anvin" <hpa@zytor.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Kiryl Shutsemau <kas@kernel.org>,
	 Rick Edgecombe <rick.p.edgecombe@intel.com>,
	David Hildenbrand <david@kernel.org>,
	kvm@vger.kernel.org,  linux-coco@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH RFC 09/10] KVM: x86: Add guarded_by annotations for kvm_arch, kvm_hv, and ioapic
Date: Thu, 10 Sep 2026 16:21:42 +0000	[thread overview]
Message-ID: <20260910162343.4092060-10-elver@google.com> (raw)
In-Reply-To: <20260910162343.4092060-1-elver@google.com>

Add __guarded_by annotations to a subset of fields across x86 state
(struct kvm_hv, struct kvm_arch, and struct kvm_ioapic) where the
protecting locks reside in the same struct scope. Mark deliberate
lockless updates with data_race().

No functional change intended.

Signed-off-by: Marco Elver <elver@google.com>
---
 arch/x86/include/asm/kvm_host.h | 32 ++++++++++++++++----------------
 arch/x86/kvm/ioapic.c           |  4 ++--
 arch/x86/kvm/ioapic.h           | 16 ++++++++--------
 arch/x86/kvm/x86.c              |  8 ++++----
 4 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 683bb8bf43a9..a0d2d6c08e47 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1073,20 +1073,20 @@ struct kvm_hv {
 	struct mutex hv_lock;
 	u64 hv_guest_os_id;
 	u64 hv_hypercall;
-	u64 hv_tsc_page;
+	u64 hv_tsc_page __guarded_by(&hv_lock);
 	enum hv_tsc_page_status hv_tsc_page_status;
 
 	/* Hyper-v based guest crash (NT kernel bugcheck) parameters */
-	u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS];
-	u64 hv_crash_ctl;
+	u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS] __guarded_by(&hv_lock);
+	u64 hv_crash_ctl __guarded_by(&hv_lock);
 
 	struct ms_hyperv_tsc_page tsc_ref;
 
 	struct idr conn_to_evt;
 
-	u64 hv_reenlightenment_control;
-	u64 hv_tsc_emulation_control;
-	u64 hv_tsc_emulation_status;
+	u64 hv_reenlightenment_control __guarded_by(&hv_lock);
+	u64 hv_tsc_emulation_control __guarded_by(&hv_lock);
+	u64 hv_tsc_emulation_status __guarded_by(&hv_lock);
 	u64 hv_invtsc_control;
 
 	/* How many vCPUs have VP index != vCPU index */
@@ -1232,15 +1232,15 @@ struct kvm_arch {
 	 * preemption-disabled region, so it must be a raw spinlock.
 	 */
 	raw_spinlock_t tsc_write_lock;
-	u64 last_tsc_nsec;
-	u64 last_tsc_write;
-	u32 last_tsc_khz;
-	u64 last_tsc_offset;
-	u64 cur_tsc_nsec;
-	u64 cur_tsc_write;
-	u64 cur_tsc_offset;
-	u64 cur_tsc_generation;
-	int nr_vcpus_matched_tsc;
+	u64 last_tsc_nsec __guarded_by(&tsc_write_lock);
+	u64 last_tsc_write __guarded_by(&tsc_write_lock);
+	u32 last_tsc_khz __guarded_by(&tsc_write_lock);
+	u64 last_tsc_offset __guarded_by(&tsc_write_lock);
+	u64 cur_tsc_nsec __guarded_by(&tsc_write_lock);
+	u64 cur_tsc_write __guarded_by(&tsc_write_lock);
+	u64 cur_tsc_offset __guarded_by(&tsc_write_lock);
+	u64 cur_tsc_generation __guarded_by(&tsc_write_lock);
+	int nr_vcpus_matched_tsc __guarded_by(&tsc_write_lock);
 
 	u32 default_tsc_khz;
 	bool user_set_tsc;
@@ -1370,7 +1370,7 @@ struct kvm_arch {
 #endif
 
 #if IS_ENABLED(CONFIG_HYPERV)
-	hpa_t	hv_root_tdp;
+	hpa_t	hv_root_tdp __guarded_by(&hv_root_tdp_lock);
 	spinlock_t hv_root_tdp_lock;
 	struct hv_partition_assist_pg *hv_pa_pg;
 #endif
diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c
index d6865e557abe..7affe2584036 100644
--- a/arch/x86/kvm/ioapic.c
+++ b/arch/x86/kvm/ioapic.c
@@ -739,11 +739,11 @@ int kvm_ioapic_init(struct kvm *kvm)
 	ioapic = kzalloc_obj(struct kvm_ioapic, GFP_KERNEL_ACCOUNT);
 	if (!ioapic)
 		return -ENOMEM;
-	spin_lock_init(&ioapic->lock);
 	INIT_DELAYED_WORK(&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work);
 	INIT_HLIST_HEAD(&ioapic->mask_notifier_list);
 	kvm->arch.vioapic = ioapic;
-	kvm_ioapic_reset(ioapic);
+	scoped_guard(spinlock_init, &ioapic->lock)
+		kvm_ioapic_reset(ioapic);
 	kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops);
 	ioapic->kvm = kvm;
 	mutex_lock(&kvm->slots_lock);
diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h
index 81b576513116..1f87396c0a79 100644
--- a/arch/x86/kvm/ioapic.h
+++ b/arch/x86/kvm/ioapic.h
@@ -70,19 +70,19 @@ union kvm_ioapic_redirect_entry {
 
 struct kvm_ioapic {
 	u64 base_address;
-	u32 ioregsel;
-	u32 id;
-	u32 irr;
+	u32 ioregsel __guarded_by(&lock);
+	u32 id __guarded_by(&lock);
+	u32 irr __guarded_by(&lock);
 	u32 pad;
-	union kvm_ioapic_redirect_entry redirtbl[IOAPIC_NUM_PINS];
-	unsigned long irq_states[IOAPIC_NUM_PINS];
+	union kvm_ioapic_redirect_entry redirtbl[IOAPIC_NUM_PINS] __guarded_by(&lock);
+	unsigned long irq_states[IOAPIC_NUM_PINS] __guarded_by(&lock);
 	struct kvm_io_device dev;
 	struct kvm *kvm;
 	spinlock_t lock;
-	struct rtc_status rtc_status;
+	struct rtc_status rtc_status __guarded_by(&lock);
 	struct delayed_work eoi_inject;
-	u32 irq_eoi[IOAPIC_NUM_PINS];
-	u32 irr_delivered;
+	u32 irq_eoi[IOAPIC_NUM_PINS] __guarded_by(&lock);
+	u32 irr_delivered __guarded_by(&lock);
 
 	/* reads protected by irq_srcu, writes by irq_lock */
 	struct hlist_head mask_notifier_list;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9893705d0dfa..5d4b2c7aa9b8 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9813,8 +9813,8 @@ int kvm_arch_enable_virtualization_cpu(void)
 			 * you may have some problem.  Solving this issue is
 			 * left as an exercise to the reader.
 			 */
-			kvm->arch.last_tsc_nsec = 0;
-			kvm->arch.last_tsc_write = 0;
+			data_race(kvm->arch.last_tsc_nsec = 0);
+			data_race(kvm->arch.last_tsc_write = 0);
 		}
 
 	}
@@ -9927,8 +9927,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	kvm->arch.enable_pmu = enable_pmu && !kvm->arch.has_protected_pmu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
-	spin_lock_init(&kvm->arch.hv_root_tdp_lock);
-	kvm->arch.hv_root_tdp = INVALID_PAGE;
+	scoped_guard(spinlock_init, &kvm->arch.hv_root_tdp_lock)
+		kvm->arch.hv_root_tdp = INVALID_PAGE;
 #endif
 
 	kvm_apicv_init(kvm);
-- 
2.55.0.1003.g10538fe699-goog


  parent reply	other threads:[~2026-09-10 16:24 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10 16:21 [PATCH RFC 00/10] KVM: Enable Clang Context Analysis Marco Elver
2026-09-10 16:21 ` [PATCH RFC 01/10] KVM: x86/pmu: Acquire SRCU in pmc_is_event_allowed() to protect filter lookup Marco Elver
2026-09-10 16:46   ` Sean Christopherson
2026-09-10 16:21 ` [PATCH RFC 02/10] KVM: Allow reading memslots while holding slots_arch_lock Marco Elver
2026-09-10 16:30   ` Sean Christopherson
2026-09-10 17:11     ` Marco Elver
2026-09-10 17:52       ` Sean Christopherson
2026-09-10 19:05         ` Marco Elver
2026-09-10 16:39   ` sashiko-bot
2026-09-10 16:21 ` [PATCH RFC 03/10] KVM: guest_memfd: Avoid conditional mmu_lock acquisition Marco Elver
2026-09-10 16:42   ` sashiko-bot
2026-09-10 16:21 ` [PATCH RFC 04/10] KVM: Refactor kvm_handle_hva_range() to avoid conditional mmu_lock Marco Elver
2026-09-10 16:38   ` Sean Christopherson
2026-09-10 16:21 ` [PATCH RFC 05/10] KVM: Refactor kvm_handle_gfn_range() " Marco Elver
2026-09-10 16:21 ` [PATCH RFC 06/10] KVM: Add basic lock context annotations Marco Elver
2026-09-10 16:34   ` sashiko-bot
2026-09-10 16:53     ` Marco Elver
2026-09-10 16:21 ` [PATCH RFC 07/10] KVM: x86: " Marco Elver
2026-09-10 16:21 ` [PATCH RFC 08/10] KVM: Add guarded_by to members in struct kvm Marco Elver
2026-09-10 16:21 ` Marco Elver [this message]
2026-09-10 16:46   ` [PATCH RFC 09/10] KVM: x86: Add guarded_by annotations for kvm_arch, kvm_hv, and ioapic sashiko-bot
2026-09-10 16:21 ` [PATCH RFC 10/10] KVM: x86: Enable CONTEXT_ANALYSIS with opt-outs Marco Elver
2026-09-10 16:55 ` [PATCH RFC 00/10] KVM: Enable Clang Context Analysis Sean Christopherson
2026-09-10 19:19   ` Marco Elver

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260910162343.4092060-10-elver@google.com \
    --to=elver@google.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=hpa@zytor.com \
    --cc=kas@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=seanjc@google.com \
    --cc=tglx@kernel.org \
    --cc=vkuznets@redhat.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.