KVM: x86: complete SRCU coverage for vCPU events
Subsystems (2)
v2 cover KVM: x86: complete SRCU coverage for vCPU events
Hi,
v1 was a single patch: the 5.15.y version of commit 4bcdd831d9d0 ("KVM:
x86: Acquire kvm->srcu when handling KVM_SET_VCPU_EVENTS"). On 5.10 that isn't enough. KVM_RUN also reaches kvm_vcpu_ioctl_x86_set_vcpu_events() through sync_regs(), and on 5.10 that happens before vcpu_run() takes kvm->srcu. Upstream covers that path with commit 8d25b7beca7e ("KVM:
x86: pull kvm->srcu read-side to kvm_arch_vcpu_ioctl_run"), which 5.10 doesn't have.
Patch 1 is that commit with its follow-up commit 2d08935682ac ("KVM:
x86: Don't re-acquire SRCU lock in complete_emulated_io()") folded in, so emulated MMIO and PIO completion can't overwrite the outer SRCU index. Patch 2 is the v1 patch.
Tested on v5.10.275:
- x86_64 allmodconfig W=1 build of the touched objects, base vs patched, no new warnings
- booted a PROVE_LOCKING/PROVE_RCU kernel as a nested KVM host under QEMU and ran the evmcs, smm, state and sync_regs selftests. All pass with no lockdep or RCU splats. vmx_preemption_timer_test fails the same way on base and patched in this nested setup.
5.15.y has the SET_VCPU_EVENTS fix but not patch 1, so I'm sending patch 1 for 5.15.y separately.
Changes in v2:
- v1 <CAFaZ9eoOtFxAansjaUbA-CDNQLcVAVJre2R9vaTQONG9MYUEng@mail.gmail.com> only had patch 2 and missed the sync_regs() path. Added patch 1.
- resent through a whitespace-safe path, v1 was also damaged by my mail client.
Thanks,
Dickson
---
Paolo Bonzini (1):
KVM: x86: pull kvm->srcu read-side to kvm_arch_vcpu_ioctl_run
Sean Christopherson (1):
KVM: x86: Acquire kvm->srcu when handling KVM_SET_VCPU_EVENTS
arch/x86/kvm/x86.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
---
base-commit: 99112ea818e35b606397c84fbc05e6fadb6ea162
change-id: 20261126-kvm-510d-6cc40d855004
Best regards,
Signature · 1 line
Dickson Wu <[email protected]>
PATCH v2 1/2 KVM: x86: pull kvm->srcu read-side to kvm_arch_vcpu_ioctl_run
From: Paolo Bonzini <[email protected]>
commit 8d25b7beca7ed6ca34f53f0f8abd009e2be15d94 upstream.
kvm_arch_vcpu_ioctl_run is already doing srcu_read_lock/unlock in two places, namely vcpu_run and post_kvm_run_save, and a third is actually needed around the call to vcpu->arch.complete_userspace_io to avoid the following splat:
WARNING: suspicious RCU usage
arch/x86/kvm/pmu.c:190 suspicious rcu_dereference_check() usage!
other info that might help us debug this:
rcu_scheduler_active = 2, debug_locks = 1
1 lock held by CPU 28/KVM/370841:
#0: ff11004089f280b8 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x87/0x730 [kvm]
Call Trace: <TASK> dump_stack_lvl+0x59/0x73 reprogram_fixed_counter+0x15d/0x1a0 [kvm] kvm_pmu_trigger_event+0x1a3/0x260 [kvm] ? free_moved_vector+0x1b4/0x1e0 complete_fast_pio_in+0x8a/0xd0 [kvm]
This splat is not at all unexpected, since complete_userspace_io callbacks can execute similar code to vmexits. For example, SVM with nrips=false will call into the emulator from svm_skip_emulated_instruction().
While it's tempting to never acquire kvm->srcu for an uninitialized vCPU, practically speaking there's no penalty to acquiring kvm->srcu "early" as the KVM_MP_STATE_UNINITIALIZED path is a one-time thing per vCPU. On the other hand, seemingly innocuous helpers like kvm_apic_accept_events() and sync_regs() can theoretically reach code that might access SRCU-protected data structures, e.g. sync_regs() can trigger forced existing of nested mode via kvm_vcpu_ioctl_x86_set_vcpu_events().
---
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
arch/x86/kvm/x86.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
arch/x86/kvm/x86.c +13 -11
@@ -8486,6 +8486,7 @@ static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)likely (!pic_in_kernel (vcpu->kvm)); } +/* Called within kvm->srcu read side. */ static void post_kvm_run_save (struct kvm_vcpu *vcpu) {struct kvm_run *kvm_run = vcpu->run;
@@ -9098,6 +9099,7 @@ void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)EXPORT_SYMBOL_GPL (__kvm_request_immediate_exit);/* +* Called within kvm->srcu read side. * Returns 1 to let vcpu_run() continue the guest execution loop without * exiting to the userspace. Otherwise, the value will be returned to the * userspace.
@@ -9421,6 +9423,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)return r; } +/* Called within kvm->srcu read side. */ static inline int vcpu_block (struct kvm *kvm,struct kvm_vcpu *vcpu) { if (!kvm_arch_vcpu_runnable(vcpu) &&
@@ -9463,12 +9466,12 @@ static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) !vcpu->arch.apf.halted); } +/* Called within kvm->srcu read side. */ static int vcpu_run (struct kvm_vcpu *vcpu) {int r;struct kvm *kvm = vcpu->kvm; - vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); vcpu->arch.l1tf_flush_l1d = true;for (;;) {
@@ -9503,25 +9506,18 @@ static int vcpu_run(struct kvm_vcpu *vcpu)if (__xfer_to_guest_mode_work_pending ()) {srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); r =xfer_to_guest_mode_handle_work (vcpu); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu);if (r)return r; - vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); } } -srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); -return r; }static inline int complete_emulated_io (struct kvm_vcpu *vcpu) { -int r; - - vcpu->srcu_idx =srcu_read_lock (&vcpu->kvm->srcu); - r =kvm_emulate_instruction (vcpu,EMULTYPE_NO_DECODE ); -srcu_read_unlock (&vcpu->kvm->srcu, vcpu->srcu_idx); -return r; +return kvm_emulate_instruction (vcpu,EMULTYPE_NO_DECODE ); }static int complete_emulated_pio (struct kvm_vcpu *vcpu)
@@ -9646,18 +9642,23 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu) {struct kvm_run *kvm_run = vcpu->run; +struct kvm *kvm = vcpu->kvm;int r;vcpu_load (vcpu);kvm_sigset_activate (vcpu);kvm_load_guest_fpu (vcpu); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu);if (unlikely (vcpu->arch.mp_state ==KVM_MP_STATE_UNINITIALIZED )) {if (!vcpu->wants_to_run) { r = -EINTR ; goto out; } +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx);kvm_vcpu_block (vcpu); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); +kvm_apic_accept_events (vcpu);kvm_clear_request (KVM_REQ_UNHALT , vcpu); r = -EAGAIN ;
@@ -9707,8 +9708,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)if (kvm_run->kvm_valid_regs)store_regs (vcpu);post_kvm_run_save (vcpu); -kvm_sigset_deactivate (vcpu); +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +kvm_sigset_deactivate (vcpu);vcpu_put (vcpu);return r; }
Signature · 1 line
2.43.0
PATCH v2 2/2 KVM: x86: Acquire kvm->srcu when handling KVM_SET_VCPU_EVENTS
In reply to Dickson WuFrom: Sean Christopherson <[email protected]>
commit 4bcdd831d9d01e0fb64faea50732b59b2ee88da1 upstream.
Grab kvm->srcu when processing KVM_SET_VCPU_EVENTS, as KVM will forcibly leave nested VMX/SVM if SMM mode is being toggled, and leaving nested VMX reads guest memory.
Note, kvm_vcpu_ioctl_x86_set_vcpu_events() can also be called from KVM_RUN via sync_regs(), which already holds SRCU. I.e. trying to precisely use kvm_vcpu_srcu_read_lock() around the problematic SMM code would cause problems. Acquiring SRCU isn't all that expensive, so for simplicity, grab it unconditionally for KVM_SET_VCPU_EVENTS.
=============================
WARNING: suspicious RCU usage 6.10.0-rc7-332d2c1d713e-next-vm #552 Not tainted
-----------------------------
include/linux/kvm_host.h:1027 suspicious rcu_dereference_check() usage!
other info that might help us debug this:
rcu_scheduler_active = 2, debug_locks = 1
1 lock held by repro/1071:
#0: ffff88811e424430 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x7d/0x970 [kvm]
stack backtrace: CPU: 15 PID: 1071 Comm: repro Not tainted 6.10.0-rc7-332d2c1d713e-next-vm #552 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 Call Trace: <TASK> dump_stack_lvl+0x7f/0x90 lockdep_rcu_suspicious+0x13f/0x1a0 kvm_vcpu_gfn_to_memslot+0x168/0x190 [kvm] kvm_vcpu_read_guest+0x3e/0x90 [kvm] nested_vmx_load_msr+0x6b/0x1d0 [kvm_intel] load_vmcs12_host_state+0x432/0xb40 [kvm_intel] vmx_leave_nested+0x30/0x40 [kvm_intel] kvm_vcpu_ioctl_x86_set_vcpu_events+0x15d/0x2b0 [kvm] kvm_arch_vcpu_ioctl+0x1107/0x1750 [kvm] ? mark_held_locks+0x49/0x70 ? kvm_vcpu_ioctl+0x7d/0x970 [kvm] ? kvm_vcpu_ioctl+0x497/0x970 [kvm] kvm_vcpu_ioctl+0x497/0x970 [kvm] ? lock_acquire+0xba/0x2d0 ? find_held_lock+0x2b/0x80 ? do_user_addr_fault+0x40c/0x6f0 ? lock_release+0xb7/0x270 __x64_sys_ioctl+0x82/0xb0 do_syscall_64+0x6c/0x170 entry_SYSCALL_64_after_hwframe+0x4b/0x53 RIP: 0033:0x7ff11eb1b539 </TASK>
---
The backport and the test runs were prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the result before signing off.
arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/x86.c +2 -0
@@ -4970,7 +4970,9 @@ long kvm_arch_vcpu_ioctl(struct file *filp,if (copy_from_user (&events, argp,sizeof (struct kvm_vcpu_events )))break ; + vcpu->srcu_idx =srcu_read_lock (&vcpu->kvm->srcu); r =kvm_vcpu_ioctl_x86_set_vcpu_events (vcpu, &events); +srcu_read_unlock (&vcpu->kvm->srcu, vcpu->srcu_idx);break ; }case KVM_GET_DEBUGREGS : {
Signature · 1 line
2.43.0