From patchwork Tue May 11 10:44:21 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 52309 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 814B4B7DB2 for ; Wed, 12 May 2010 04:48:24 +1000 (EST) Received: from localhost ([127.0.0.1]:57730 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OBuVB-0004vC-SC for incoming@patchwork.ozlabs.org; Tue, 11 May 2010 14:48:21 -0400 Received: from [140.186.70.92] (port=59563 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OBuT8-00048S-Cn for qemu-devel@nongnu.org; Tue, 11 May 2010 14:46:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OBuT6-0002cc-JJ for qemu-devel@nongnu.org; Tue, 11 May 2010 14:46:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29817) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OBuT5-0002c8-Kk for qemu-devel@nongnu.org; Tue, 11 May 2010 14:46:12 -0400 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o4BIk9LV003664 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 11 May 2010 14:46:09 -0400 Received: from virtlab9.virt.bos.redhat.com (virtlab9.virt.bos.redhat.com [10.16.72.29]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o4BIk8GE032159; Tue, 11 May 2010 14:46:08 -0400 From: Alex Williamson To: qemu-devel@nongnu.org Date: Tue, 11 May 2010 06:44:21 -0400 Message-ID: <20100511104305.15360.71875.stgit@virtlab9.virt.bos.redhat.com> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: blauwirbel@gmail.com, alex.williamson@redhat.com, kvm@vger.kernel.org Subject: [Qemu-devel] [PATCH v2] pci: cleanly backout of pci_qdev_init() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org If the init function of a device fails, as might happen with device assignment, we never undo the work done by do_pci_register_device(). This not only causes a bit of a memory leak, but also leaves a bogus pointer in the bus devices array that can cause a segfault or garbage data from 'info pci'. Signed-off-by: Alex Williamson --- v2: Remove extraneous return from do_pci_unregister_device()( hw/pci.c | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index f167436..3ca5f09 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -625,6 +625,13 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus, return pci_dev; } +static void do_pci_unregister_device(PCIDevice *pci_dev) +{ + qemu_free_irqs(pci_dev->irq); + pci_dev->bus->devices[pci_dev->devfn] = NULL; + pci_config_free(pci_dev); +} + PCIDevice *pci_register_device(PCIBus *bus, const char *name, int instance_size, int devfn, PCIConfigReadFunc *config_read, @@ -680,10 +687,7 @@ static int pci_unregister_device(DeviceState *dev) return ret; pci_unregister_io_regions(pci_dev); - - qemu_free_irqs(pci_dev->irq); - pci_dev->bus->devices[pci_dev->devfn] = NULL; - pci_config_free(pci_dev); + do_pci_unregister_device(pci_dev); return 0; } @@ -1652,8 +1656,10 @@ static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) if (pci_dev == NULL) return -1; rc = info->init(pci_dev); - if (rc != 0) + if (rc != 0) { + do_pci_unregister_device(pci_dev); return rc; + } /* rom loading */ if (pci_dev->romfile == NULL && info->romfile != NULL)