Skip to content

Commit 3d3aab1

Browse files
borntraegeravikivity
authored andcommitted
KVM: set owner of cpu and vm file operations
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 <linux/delay.h> > 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 killing the userspace, followed by an rmmod. 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. This patch requires the fix for anon_inodes (anon_inodes: use fops->owner for module refcount). With this patch, we can set the owner of all anonymous KVM inodes file operations. The VFS will then control the KVM module refcount as long as there is an open file. kvm_destroy_vm will be called by the release function of the last closed file - before the VFS drops the module refcount. Signed-off-by: Christian Borntraeger <[email protected]> Signed-off-by: Avi Kivity <[email protected]>
1 parent e3a2a0d commit 3d3aab1

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

virt/kvm/kvm_main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ static int kvm_vcpu_release(struct inode *inode, struct file *filp)
14981498
return 0;
14991499
}
15001500

1501-
static const struct file_operations kvm_vcpu_fops = {
1501+
static struct file_operations kvm_vcpu_fops = {
15021502
.release = kvm_vcpu_release,
15031503
.unlocked_ioctl = kvm_vcpu_ioctl,
15041504
.compat_ioctl = kvm_vcpu_ioctl,
@@ -1892,7 +1892,7 @@ static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
18921892
return 0;
18931893
}
18941894

1895-
static const struct file_operations kvm_vm_fops = {
1895+
static struct file_operations kvm_vm_fops = {
18961896
.release = kvm_vm_release,
18971897
.unlocked_ioctl = kvm_vm_ioctl,
18981898
.compat_ioctl = kvm_vm_ioctl,
@@ -2256,6 +2256,8 @@ int kvm_init(void *opaque, unsigned int vcpu_size,
22562256
}
22572257

22582258
kvm_chardev_ops.owner = module;
2259+
kvm_vm_fops.owner = module;
2260+
kvm_vcpu_fops.owner = module;
22592261

22602262
r = misc_register(&kvm_dev);
22612263
if (r) {

0 commit comments

Comments
 (0)