Skip to content

Commit 5d8d2bf

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(). Reported-by: Like Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 1e2277e commit 5d8d2bf

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

arch/x86/kvm/x86.c

Lines changed: 8 additions & 11 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 (;;) {
@@ -10291,8 +10287,6 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
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;
@@ -10475,8 +10471,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1047510471
if (kvm_run->kvm_valid_regs)
1047610472
store_regs(vcpu);
1047710473
post_kvm_run_save(vcpu);
10478-
kvm_sigset_deactivate(vcpu);
10474+
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
1047910475

10476+
kvm_sigset_deactivate(vcpu);
1048010477
vcpu_put(vcpu);
1048110478
return r;
1048210479
}

0 commit comments

Comments
 (0)