[PATCH RFC 09/10] KVM: x86: Add guarded_by annotations for kvm_arch, kvm_hv, and ioapic

From: Marco Elver

Date: Thu Sep 10 2026 - 12:45:19 EST


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@xxxxxxxxxx>
---
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