Skip to content

Commit e2d3fca

Browse files
committed
KVM: fix placement of refcount initialization
Reported by syzkaller: ============================= WARNING: suspicious RCU usage ----------------------------- ./include/linux/kvm_host.h:536 suspicious rcu_dereference_check() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 no locks held by repro_11/12688. stack backtrace: Call Trace: dump_stack+0x7d/0xc5 lockdep_rcu_suspicious+0x123/0x170 kvm_dev_ioctl+0x9a9/0x1260 [kvm] do_vfs_ioctl+0x1a1/0xfb0 ksys_ioctl+0x6d/0x80 __x64_sys_ioctl+0x73/0xb0 do_syscall_64+0x108/0xaa0 entry_SYSCALL_64_after_hwframe+0x49/0xbe Commit a97b0e7 (kvm: call kvm_arch_destroy_vm if vm creation fails) sets users_count to 1 before kvm_arch_init_vm(), however, if kvm_arch_init_vm() fails, we need to decrease this count. By moving it earlier, we can push the decrease to out_err_no_arch_destroy_vm without introducing yet another error label. syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=15209b84e00000 Reported-by: [email protected] Fixes: a97b0e7 ("kvm: call kvm_arch_destroy_vm if vm creation fails") Cc: Jim Mattson <[email protected]> Analyzed-by: Wanpeng Li <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 8a44119 commit e2d3fca

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

virt/kvm/kvm_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
650650
if (init_srcu_struct(&kvm->irq_srcu))
651651
goto out_err_no_irq_srcu;
652652

653+
refcount_set(&kvm->users_count, 1);
653654
for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
654655
struct kvm_memslots *slots = kvm_alloc_memslots();
655656

@@ -667,7 +668,6 @@ static struct kvm *kvm_create_vm(unsigned long type)
667668
goto out_err_no_arch_destroy_vm;
668669
}
669670

670-
refcount_set(&kvm->users_count, 1);
671671
r = kvm_arch_init_vm(kvm, type);
672672
if (r)
673673
goto out_err_no_arch_destroy_vm;
@@ -696,8 +696,8 @@ static struct kvm *kvm_create_vm(unsigned long type)
696696
hardware_disable_all();
697697
out_err_no_disable:
698698
kvm_arch_destroy_vm(kvm);
699-
WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
700699
out_err_no_arch_destroy_vm:
700+
WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
701701
for (i = 0; i < KVM_NR_BUSES; i++)
702702
kfree(kvm_get_bus(kvm, i));
703703
for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)

0 commit comments

Comments
 (0)