All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Isaku Yamahata <yamahata@valinux.co.jp>
Cc: Blue Swirl <blauwirbel@gmail.com>, qemu-devel@nongnu.org
Subject: [Qemu-devel] Re: [PATCH] pci: fix pci_find_bus().
Date: Sun, 11 Apr 2010 13:46:16 +0300	[thread overview]
Message-ID: <20100411104616.GB8992@redhat.com> (raw)
In-Reply-To: <20100409101324.GC14603@valinux.co.jp>

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 <blauwirbel@gmail.com>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
> ---
>  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:

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;
 }

> -- 
> 1.6.6.1
> 

  parent reply	other threads:[~2010-04-11 10:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-09 10:13 [Qemu-devel] [PATCH] pci: fix pci_find_bus() Isaku Yamahata
2010-04-09 23:48 ` [Qemu-devel] " Isaku Yamahata
2010-04-11 10:51   ` Michael S. Tsirkin
2010-04-12  2:58     ` [Qemu-devel] [PATCH v2] " Isaku Yamahata
2010-04-20 17:19       ` [Qemu-devel] " Blue Swirl
2010-04-11 10:46 ` Michael S. Tsirkin [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-02-08  6:38 [Qemu-devel] [PATCH] " Isaku Yamahata
2010-02-08 10:30 ` [Qemu-devel] " Michael S. Tsirkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100411104616.GB8992@redhat.com \
    --to=mst@redhat.com \
    --cc=blauwirbel@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=yamahata@valinux.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.