From patchwork Sun Apr 11 10:46:16 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 49910 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 75663B7D21 for ; Sun, 11 Apr 2010 21:05:03 +1000 (EST) Received: from localhost ([127.0.0.1]:34407 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O0urV-0004sA-9K for incoming@patchwork.ozlabs.org; Sun, 11 Apr 2010 06:57:57 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O0ujw-0002cQ-Iy for qemu-devel@nongnu.org; Sun, 11 Apr 2010 06:50:08 -0400 Received: from [140.186.70.92] (port=46636 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O0uju-0002ab-Ho for qemu-devel@nongnu.org; Sun, 11 Apr 2010 06:50:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O0ujr-0001Ao-Uy for qemu-devel@nongnu.org; Sun, 11 Apr 2010 06:50:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:4992) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O0ujr-0001Ac-LC for qemu-devel@nongnu.org; Sun, 11 Apr 2010 06:50:03 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o3BAo075009033 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 11 Apr 2010 06:50:00 -0400 Received: from redhat.com (vpn1-4-27.ams2.redhat.com [10.36.4.27]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id o3BAnuUH005326; Sun, 11 Apr 2010 06:49:59 -0400 Date: Sun, 11 Apr 2010 13:46:16 +0300 From: "Michael S. Tsirkin" To: Isaku Yamahata Message-ID: <20100411104616.GB8992@redhat.com> References: <20100409101324.GC14603@valinux.co.jp> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20100409101324.GC14603@valinux.co.jp> User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Blue Swirl , qemu-devel@nongnu.org Subject: [Qemu-devel] Re: [PATCH] pci: fix pci_find_bus(). 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 On Fri, Apr 09, 2010 at 07:13:24PM +0900, Isaku Yamahata wrote: > When looking down child bus, it looked parent bridge's > bus number. > It should look child bridge's. > > Cc: Blue Swirl > Cc: "Michael S. Tsirkin" > Signed-off-by: Isaku Yamahata > --- > hw/pci.c | 4 ++-- > 1 files changed, 2 insertions(+), 2 deletions(-) > > diff --git a/hw/pci.c b/hw/pci.c > index 0dbca17..2f6907b 100644 > --- a/hw/pci.c > +++ b/hw/pci.c > @@ -1557,9 +1557,9 @@ PCIBus *pci_find_bus(PCIBus *bus, int bus_num) > > /* try child bus */ > QLIST_FOREACH(sec, &bus->child, sibling) { > - if (!bus->parent_dev /* pci host bridge */ > + if (!sec->parent_dev /* pci host bridge */ I don't understand this first test. As far as I can tell secondary bus must always have a device, as set by pci_register_secondary_bus. Should this rather be assert(sec->parent_dev)? > || (pci_bus_num(sec) <= bus_num && And so the above should just use sec->parent_dev->config[PCI_SECONDARY_BUS] instead of a wrapper that tests sec->parent_dev. > - bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS]) ) { > + bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) ) { > ret = pci_find_bus(sec, bus_num); > if (ret) { I think that in the above, we can just as well do return pci_find_bus() - if multiple children claim the same bus range, on real hardware only one of them will claim the transaction. > return ret; I find the use of recursion here confusing. Since as pointed out above this can be a tail recursion, it can easily be converted to a loop. How does the below look, for example? Note: completely untested, likely broken: > -- > 1.6.6.1 > diff --git a/hw/pci.c b/hw/pci.c index b6abd67..e9d6def 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -1546,26 +1546,33 @@ static void pci_bridge_write_config(PCIDevice *d, PCIBus *pci_find_bus(PCIBus *bus, int bus_num) { - PCIBus *sec, *ret; + PCIBus *sec; + bool found; - if (!bus) + if (!bus) { return NULL; + } if (pci_bus_num(bus) == bus_num) { return bus; } /* try child bus */ - QLIST_FOREACH(sec, &bus->child, sibling) { - if (!bus->parent_dev /* pci host bridge */ - || (pci_bus_num(sec) <= bus_num && - bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS]) ) { - ret = pci_find_bus(sec, bus_num); - if (ret) { - return ret; + do { + found = false; + QLIST_FOREACH(sec, &bus->child, sibling) { + assert(sec->parent_dev); + if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) { + return sec; + } + if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num && + bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) { + bus = sec; + found = true; + break; } } - } + } while (found); return NULL; }