From: Christian Borntraeger Subject: Re: [PATCH/RFC] kvm: fix refcounting race release vs. module unload Date: Mon, 24 Nov 2008 10:12:31 +0100 Message-ID: <200811241012.31743.borntraeger@de.ibm.com> References: <200811202010.00125.borntraeger@de.ibm.com> <49295F6D.8010200@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org To: Avi Kivity Return-path: Received: from mtagate3.uk.ibm.com ([195.212.29.136]:38176 "EHLO mtagate3.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750983AbYKXJMe (ORCPT ); Mon, 24 Nov 2008 04:12:34 -0500 Received: from d06nrmr1407.portsmouth.uk.ibm.com (d06nrmr1407.portsmouth.uk.ibm.com [9.149.38.185]) by mtagate3.uk.ibm.com (8.13.8/8.13.8) with ESMTP id mAO9CXFg201176 for ; Mon, 24 Nov 2008 09:12:33 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 mAO9CWMO774388 for ; Mon, 24 Nov 2008 09:12:32 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 mAO9CWGH008559 for ; Mon, 24 Nov 2008 09:12:32 GMT In-Reply-To: <49295F6D.8010200@redhat.com> Content-Disposition: inline Sender: kvm-owner@vger.kernel.org List-ID: > > --- 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) { > > > > Messing with module counts is slightly ugly. How about having a vm fd > fget() the /dev/kvm fd() instead? I personally find fget (and fput) slightly more ugly than handling the module reference count. Especially if the problem is module unloading...the module refcount looks so natural. I am also a bit worried by fget/fput, since we would call fput in the release function - which is part of the module. Wouldnt that open another very small race? In addition, we would need variables containing the fd and the file pointer for /dev/kvm, since fget/fput need some parameters, no? (Is there an easy way to get the fd from the struct file *filp? Searching current->files, seems to be the only method I know) To me, the fget approach looks more complicated and less safe. Christian