Skip to content

Commit 8d25b7b

Browse files
committed
KVM: x86: pull kvm->srcu read-side to kvm_arch_vcpu_ioctl_run
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(). Reported-by: Like Xu <likexu@tencent.com> Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent c6c937d commit 8d25b7b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

arch/x86/kvm/x86.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9180,6 +9180,7 @@ static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
91809180
likely(!pic_in_kernel(vcpu->kvm));
91819181
}
91829182

9183+
/* Called within kvm->srcu read side. */
91839184
static void post_kvm_run_save(struct kvm_vcpu *vcpu)
91849185
{
91859186
struct kvm_run *kvm_run = vcpu->run;
@@ -9188,16 +9189,9 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu)
91889189
kvm_run->cr8 = kvm_get_cr8(vcpu);
91899190
kvm_run->apic_base = kvm_get_apic_base(vcpu);
91909191

9191-
/*
9192-
* The call to kvm_ready_for_interrupt_injection() may end up in
9193-
* kvm_xen_has_interrupt() which may require the srcu lock to be
9194-
* held, to protect against changes in the vcpu_info address.
9195-
*/
9196-
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
91979192
kvm_run->ready_for_interrupt_injection =
91989193
pic_in_kernel(vcpu->kvm) ||
91999194
kvm_vcpu_ready_for_interrupt_injection(vcpu);
9200-
srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
92019195

92029196
if (is_smm(vcpu))
92039197
kvm_run->flags |= KVM_RUN_X86_SMM;
@@ -9815,6 +9809,7 @@ void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
98159809
EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
98169810

98179811
/*
9812+
* Called within kvm->srcu read side.
98189813
* Returns 1 to let vcpu_run() continue the guest execution loop without
98199814
* exiting to the userspace. Otherwise, the value will be returned to the
98209815
* userspace.
@@ -10193,6 +10188,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1019310188
return r;
1019410189
}
1019510190

10191+
/* Called within kvm->srcu read side. */
1019610192
static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
1019710193
{
1019810194
bool hv_timer;
@@ -10252,12 +10248,12 @@ static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
1025210248
!vcpu->arch.apf.halted);
1025310249
}
1025410250

10251+
/* Called within kvm->srcu read side. */
1025510252
static int vcpu_run(struct kvm_vcpu *vcpu)
1025610253
{
1025710254
int r;
1025810255
struct kvm *kvm = vcpu->kvm;
1025910256

10260-
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
1026110257
vcpu->arch.l1tf_flush_l1d = true;
1026210258

1026310259
for (;;) {
@@ -10285,14 +10281,12 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
1028510281
if (__xfer_to_guest_mode_work_pending()) {
1028610282
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
1028710283
r = xfer_to_guest_mode_handle_work(vcpu);
10284+
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
1028810285
if (r)
1028910286
return r;
10290-
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
1029110287
}
1029210288
}
1029310289

10294-
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
10295-
1029610290
return r;
1029710291
}
1029810292

@@ -10398,13 +10392,15 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
1039810392
int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1039910393
{
1040010394
struct kvm_run *kvm_run = vcpu->run;
10395+
struct kvm *kvm = vcpu->kvm;
1040110396
int r;
1040210397

1040310398
vcpu_load(vcpu);
1040410399
kvm_sigset_activate(vcpu);
1040510400
kvm_run->flags = 0;
1040610401
kvm_load_guest_fpu(vcpu);
1040710402

10403+
vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1040810404
if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
1040910405
if (kvm_run->immediate_exit) {
1041010406
r = -EINTR;
@@ -10415,7 +10411,11 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1041510411
* use before KVM has ever run the vCPU.
1041610412
*/
1041710413
WARN_ON_ONCE(kvm_lapic_hv_timer_in_use(vcpu));
10414+
10415+
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
1041810416
kvm_vcpu_block(vcpu);
10417+
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
10418+
1041910419
if (kvm_apic_accept_events(vcpu) < 0) {
1042010420
r = 0;
1042110421
goto out;
@@ -10475,8 +10475,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1047510475
if (kvm_run->kvm_valid_regs)
1047610476
store_regs(vcpu);
1047710477
post_kvm_run_save(vcpu);
10478-
kvm_sigset_deactivate(vcpu);
10478+
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
1047910479

10480+
kvm_sigset_deactivate(vcpu);
1048010481
vcpu_put(vcpu);
1048110482
return r;
1048210483
}

0 commit comments

Comments
 (0)