From: Christian Borntraeger Subject: [PATCH/RFC] kvm: fix refcounting race release vs. module unload Date: Thu, 20 Nov 2008 20:10:00 +0100 Message-ID: <200811202010.00125.borntraeger@de.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org To: Avi Kivity Return-path: Received: from mtagate1.uk.ibm.com ([194.196.100.161]:41475 "EHLO mtagate1.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753529AbYKTTKD (ORCPT ); Thu, 20 Nov 2008 14:10:03 -0500 Received: from d06nrmr1407.portsmouth.uk.ibm.com (d06nrmr1407.portsmouth.uk.ibm.com [9.149.38.185]) by mtagate1.uk.ibm.com (8.13.1/8.13.1) with ESMTP id mAKJA2nN030138 for ; Thu, 20 Nov 2008 19:10:02 GMT Received: from d06av02.portsmouth.uk.ibm.com (d06av02.portsmouth.uk.ibm.com [9.149.37.228]) by d06nrmr1407.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id mAKJA2tu917756 for ; Thu, 20 Nov 2008 19:10:02 GMT Received: from d06av02.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av02.portsmouth.uk.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id mAKJA13G013801 for ; Thu, 20 Nov 2008 19:10:02 GMT Content-Disposition: inline Sender: kvm-owner@vger.kernel.org List-ID: Hello Avi, while fixing a module reference counting problem for s390 I spotted another problem in the kvm main code. Any comments on the patch? From: Christian Borntraeger There is a race between a "close of the file descriptors" and module unload in the kvm module. You can easily trigger this problem by applying this debug patch: >--- kvm.orig/virt/kvm/kvm_main.c >+++ kvm/virt/kvm/kvm_main.c >@@ -648,10 +648,14 @@ void kvm_free_physmem(struct kvm *kvm) > kvm_free_physmem_slot(&kvm->memslots[i], NULL); > } > >+#include > static void kvm_destroy_vm(struct kvm *kvm) > { > struct mm_struct *mm = kvm->mm; > >+ printk("off1\n"); >+ msleep(5000); >+ printk("off2\n"); > spin_lock(&kvm_lock); > list_del(&kvm->vm_list); > spin_unlock(&kvm_lock); and changing userspace to closing the vcpu file descriptors after the vm file descriptors. (killall qemu ; rmmod kvm_intel, rmmod kvm also crashes my system) The problem is that kvm_destroy_vm can run while the module count is 0. That means, you can remove the module while kvm_destroy_vm is running. But kvm_destroy_vm is part of the module text. This causes a kerneloops. The race exists without the msleep but is much harder to trigger. Nevertheless, the right solution is to call kvm_destroy_vm only with module_ref_count > 0. This can be done by calling kvm_destroy_vm only via a release function, since the VFS will not allow module unload. This patch sets kvm_vcpu_fops.owner to the module and manually increases the module refcount after anon_inode_getfd, since anon_inode_getfd does not do it. Signed-off-by: Christian Borntraeger --- virt/kvm/kvm_main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) Index: kvm/virt/kvm/kvm_main.c =================================================================== --- kvm.orig/virt/kvm/kvm_main.c +++ kvm/virt/kvm/kvm_main.c @@ -1303,7 +1303,7 @@ static int kvm_vcpu_release(struct inode return 0; } -static const struct file_operations kvm_vcpu_fops = { +static struct file_operations kvm_vcpu_fops = { .release = kvm_vcpu_release, .unlocked_ioctl = kvm_vcpu_ioctl, .compat_ioctl = kvm_vcpu_ioctl, @@ -1318,6 +1318,7 @@ static int create_vcpu_fd(struct kvm_vcp int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0); if (fd < 0) kvm_put_kvm(vcpu->kvm); + __module_get(kvm_vcpu_fops.owner); return fd; } @@ -2061,6 +2062,7 @@ int kvm_init(void *opaque, unsigned int } kvm_chardev_ops.owner = module; + kvm_vcpu_fops.owner = module; r = misc_register(&kvm_dev); if (r) {