From patchwork Tue Oct 29 16:13:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 3109031 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id BD7F39F431 for ; Tue, 29 Oct 2013 16:14:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8DF922037D for ; Tue, 29 Oct 2013 16:14:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DD3DF20304 for ; Tue, 29 Oct 2013 16:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758257Ab3J2QN1 (ORCPT ); Tue, 29 Oct 2013 12:13:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:23502 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758159Ab3J2QNZ (ORCPT ); Tue, 29 Oct 2013 12:13:25 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r9TGDNVC000610 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 29 Oct 2013 12:13:23 -0400 Received: from bling.home (ovpn-113-87.phx2.redhat.com [10.3.113.87]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r9TGDNMC020589; Tue, 29 Oct 2013 12:13:23 -0400 Subject: [PATCH 1/4] kvm: Destroy & free KVM devices on release To: kvm@vger.kernel.org, gleb@redhat.com From: Alex Williamson Cc: aik@ozlabs.ru, pbonzini@redhat.com, linux-kernel@vger.kernel.org Date: Tue, 29 Oct 2013 10:13:22 -0600 Message-ID: <20131029161322.22578.18997.stgit@bling.home> In-Reply-To: <20131029160019.22578.16409.stgit@bling.home> References: <20131029160019.22578.16409.stgit@bling.home> User-Agent: StGit/0.16 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The KVM device interface allocates a struct kvm_device and calls kvm_device_ops.create on it from KVM VM ioctl KVM_CREATE_DEVICE. This returns a file descriptor to the user for them to set/get/check further attributes. On closing the file descriptor, one would assume that kvm_device_ops.destroy is called and all traces of the device would go away. One would be wrong, it actually does nothing more than release the struct kvm reference, waiting until the VM is destroyed before doing more. This leaves devices that only want a single instance of themselves per VM in a tough spot. To fix this, do full cleanup on release of the device file descriptor. It's also non-symmetric that one of the existing devices frees the struct kvm_device from it's .destroy function, while the other doesn't. KVM-core allocates the structure and should therefore be responsible for freeing it. Finally, add a missing kfree for the device creation error path. Signed-off-by: Alex Williamson --- arch/powerpc/kvm/book3s_xics.c | 1 - virt/kvm/kvm_main.c | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/powerpc/kvm/book3s_xics.c b/arch/powerpc/kvm/book3s_xics.c index a3a5cb8..9a82426 100644 --- a/arch/powerpc/kvm/book3s_xics.c +++ b/arch/powerpc/kvm/book3s_xics.c @@ -1220,7 +1220,6 @@ static void kvmppc_xics_free(struct kvm_device *dev) for (i = 0; i <= xics->max_icsid; i++) kfree(xics->ics[i]); kfree(xics); - kfree(dev); } static int kvmppc_xics_create(struct kvm_device *dev, u32 type) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index a9dd682..fec8320 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -572,6 +572,7 @@ static void kvm_destroy_devices(struct kvm *kvm) list_del(node); dev->ops->destroy(dev); + kfree(dev); } } @@ -2231,6 +2232,9 @@ static int kvm_device_release(struct inode *inode, struct file *filp) struct kvm_device *dev = filp->private_data; struct kvm *kvm = dev->kvm; + list_del(&dev->vm_node); + dev->ops->destroy(dev); + kfree(dev); kvm_put_kvm(kvm); return 0; } @@ -2294,6 +2298,7 @@ static int kvm_ioctl_create_device(struct kvm *kvm, ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC); if (ret < 0) { ops->destroy(dev); + kfree(dev); return ret; }