Skip to contentSkip to conversation

qdev: switch reset to post-order, clean up PCI reset

PATCH In review Paolo Bonzini 2 participants 12 messages Oct 3, 2013 – Oct 9, 2013

lore
Share & tools
lore
#0 lore raw

cover qdev: switch reset to post-order, clean up PCI reset

PCI is handling resetting of its devices before the bus is reset, but this is only necessary because qdev is broken and usually does pre-order reset. Post-order is a much better definition. Drop the unnecessary flexibility that lets bus decide the reset order, convert to post-order, and make PCI use common code for reset.

Paolo Bonzini (4):
pci: do not export pci_bus_reset
pci: clean up resetting of IRQs
qdev: allow both pre- and post-order vists in qdev walking functions
qdev: switch reset to post-order

 hw/core/qdev.c         |   47 ++++++++++++++++++++++++++++++++++-------------
 hw/pci/pci.c           |   42 ++++++++++++++++++++----------------------
 hw/pci/pci_bridge.c    |    2 +-
 include/hw/pci/pci.h   |    1 -
 include/hw/qdev-core.h |   15 ++++++++++-----
 5 files changed, 65 insertions(+), 42 deletions(-)
#1 lore raw

PATCH 1/4 pci: do not export pci_bus_reset

qbus_reset_all can be used instead. There is no semantic change because pcibus_reset returns 1 and takes care of the device tree traversal.

---
 hw/pci/pci.c         |    8 ++------
 hw/pci/pci_bridge.c  |    2 +-
 include/hw/pci/pci.h |    1 -
 3 files changed, 3 insertions(+), 8 deletions(-)
hw/pci/pci.c +2 -6
index bd084c7..ac3244b 100644
@@ -210,8 +210,9 @@ void pci_device_reset(PCIDevice *dev)
  * Trigger pci bus reset under a given bus.
  * To be called on RST# assert.
  */
-void pci_bus_reset(PCIBus *bus)
+static int pcibus_reset(BusState *qbus)
 {
+    PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
     int i;
 
     for (i = 0; i < bus->nirq; i++) {
@@ -222,11 +223,6 @@ void pci_bus_reset(PCIBus *bus)
             pci_device_reset(bus->devices[i]);
         }
     }
-}
-
-static int pcibus_reset(BusState *qbus)
-{
-    pci_bus_reset(DO_UPCAST(PCIBus, qbus, qbus));
 
     /* topology traverse is done by pci_bus_reset().
        Tell qbus/qdev walker not to traverse the tree */
hw/pci/pci_bridge.c +1 -1
index 307e076..06831a2 100644
@@ -268,7 +268,7 @@ void pci_bridge_write_config(PCIDevice *d,
     newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
     if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) {
         /* Trigger hot reset on 0->1 transition. */
-        pci_bus_reset(&s->sec_bus);
+        qbus_reset_all(&s->sec_bus.qbus);
     }
 }
 
include/hw/pci/pci.h +0 -1
index ccec2ba..32f1419 100644
@@ -376,7 +376,6 @@ void pci_bus_fire_intx_routing_notifier(PCIBus *bus);
 void pci_device_set_intx_routing_notifier(PCIDevice *dev,
                                           PCIINTxRoutingNotifier notifier);
 void pci_device_reset(PCIDevice *dev);
-void pci_bus_reset(PCIBus *bus);
 
 PCIDevice *pci_nic_init(NICInfo *nd, PCIBus *rootbus,
                         const char *default_model,
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/4 pci: clean up resetting of IRQs

In reply to Paolo Bonzini

pci_device_reset will deassert the INTX pins, and this will make the irq_count array all-zeroes. Check that this is the case, and remove the existing loop which might even unsync irq_count and irq_state.

---
 hw/pci/pci.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
hw/pci/pci.c +4 -3
index ac3244b..0efc544 100644
@@ -215,15 +215,16 @@ static int pcibus_reset(BusState *qbus)
     PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
     int i;
 
-    for (i = 0; i < bus->nirq; i++) {
-        bus->irq_count[i] = 0;
-    }
     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
         if (bus->devices[i]) {
             pci_device_reset(bus->devices[i]);
         }
     }
 
+    for (i = 0; i < bus->nirq; i++) {
+        assert(bus->irq_count[i] == 0);
+    }
+
     /* topology traverse is done by pci_bus_reset().
        Tell qbus/qdev walker not to traverse the tree */
     return 1;
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/4 qdev: allow both pre- and post-order vists in qdev walking functions

In reply to Paolo Bonzini

Resetting should be done in post-order, not pre-order. However, qdev_walk_children and qbus_walk_children do not allow this. Fix it by adding two extra arguments to the functions.

---
 hw/core/qdev.c         |   45 +++++++++++++++++++++++++++++++++------------
 include/hw/qdev-core.h |   13 +++++++++----
 2 files changed, 42 insertions(+), 16 deletions(-)
hw/core/qdev.c +33 -12
index 758de9f..1c114b7 100644
@@ -240,12 +240,12 @@ static int qbus_reset_one(BusState *bus, void *opaque)
 
 void qdev_reset_all(DeviceState *dev)
 {
-    qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL);
+    qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL, NULL, NULL);
 }
 
 void qbus_reset_all(BusState *bus)
 {
-    qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL);
+    qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL, NULL, NULL);
 }
 
 void qbus_reset_all_fn(void *opaque)
@@ -343,49 +343,70 @@ BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
     return NULL;
 }
 
-int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
-                       qbus_walkerfn *busfn, void *opaque)
+int qbus_walk_children(BusState *bus,
+                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
+                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
+                       void *opaque)
 {
     BusChild *kid;
     int err;
 
-    if (busfn) {
-        err = busfn(bus, opaque);
+    if (pre_busfn) {
+        err = pre_busfn(bus, opaque);
         if (err) {
             return err;
         }
     }
 
     QTAILQ_FOREACH(kid, &bus->children, sibling) {
-        err = qdev_walk_children(kid->child, devfn, busfn, opaque);
+        err = qdev_walk_children(kid->child,
+                                 pre_devfn, pre_busfn,
+                                 post_devfn, post_busfn, opaque);
         if (err < 0) {
             return err;
         }
     }
 
+    if (post_busfn) {
+        err = post_busfn(bus, opaque);
+        if (err) {
+            return err;
+        }
+    }
+
     return 0;
 }
 
-int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
-                       qbus_walkerfn *busfn, void *opaque)
+int qdev_walk_children(DeviceState *dev,
+                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
+                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
+                       void *opaque)
 {
     BusState *bus;
     int err;
 
-    if (devfn) {
-        err = devfn(dev, opaque);
+    if (pre_devfn) {
+        err = pre_devfn(dev, opaque);
         if (err) {
             return err;
         }
     }
 
     QLIST_FOREACH(bus, &dev->child_bus, sibling) {
-        err = qbus_walk_children(bus, devfn, busfn, opaque);
+        err = qbus_walk_children(bus, pre_devfn, pre_busfn,
+                                 post_devfn, post_busfn, opaque);
         if (err < 0) {
             return err;
         }
     }
 
+    if (post_devfn) {
+        err = post_devfn(dev, opaque);
+        if (err) {
+            return err;
+        }
+    }
+
     return 0;
 }
 
include/hw/qdev-core.h +9 -4
index d840f06..21ea2c6 100644
@@ -274,10 +274,15 @@ BusState *qbus_create(const char *typename, DeviceState *parent, const char *nam
 /* Returns > 0 if either devfn or busfn skip walk somewhere in cursion,
  *         < 0 if either devfn or busfn terminate walk somewhere in cursion,
  *           0 otherwise. */
-int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
-                       qbus_walkerfn *busfn, void *opaque);
-int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
-                       qbus_walkerfn *busfn, void *opaque);
+int qbus_walk_children(BusState *bus,
+                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
+                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
+                       void *opaque);
+int qdev_walk_children(DeviceState *dev,
+                       qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
+                       qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
+                       void *opaque);
+
 void qdev_reset_all(DeviceState *dev);
 
 /**
Signature · 1 line
1.7.1
#4 lore raw

PATCH 4/4 qdev: switch reset to post-order

In reply to Paolo Bonzini

Post-order is the only sensible direction for the reset signals. For example, suppose pre-order is used and the parent has some data structures that cache children state (for example a list of active requests). When the reset method is invoked on the parent, these caches could be in any state.

If post-order is used, on the other hand, these will be in a known state when the reset method is invoked on the parent.

This change means that it is no longer possible to block the visit of the devices, so the callback is changed to return void. This is not a problem, because PCI was returning 1 exactly in order to achieve the same ordering that this patch implements.

PCI can then rely on the qdev core having sent a "reset signal" (whatever that means) to the device, and only do the PCI-specific initialization with pci_do_device_reset.

---
 hw/core/qdev.c         |    6 +++---
 hw/pci/pci.c           |   31 ++++++++++++++++---------------
 include/hw/qdev-core.h |    2 +-
 3 files changed, 20 insertions(+), 19 deletions(-)
hw/core/qdev.c +3 -3
index 1c114b7..9ba8ab1 100644
@@ -233,19 +233,19 @@ static int qbus_reset_one(BusState *bus, void *opaque)
 {
     BusClass *bc = BUS_GET_CLASS(bus);
     if (bc->reset) {
-        return bc->reset(bus);
+        bc->reset(bus);
     }
     return 0;
 }
 
 void qdev_reset_all(DeviceState *dev)
 {
-    qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL, NULL, NULL);
+    qdev_walk_children(dev, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
 }
 
 void qbus_reset_all(BusState *bus)
 {
-    qbus_walk_children(bus, qdev_reset_one, qbus_reset_one, NULL, NULL, NULL);
+    qbus_walk_children(bus, NULL, NULL, qdev_reset_one, qbus_reset_one, NULL);
 }
 
 void qbus_reset_all_fn(void *opaque)
hw/pci/pci.c +16 -15
index 0efc544..e10d74b 100644
@@ -46,7 +46,7 @@
 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
 static char *pcibus_get_dev_path(DeviceState *dev);
 static char *pcibus_get_fw_dev_path(DeviceState *dev);
-static int pcibus_reset(BusState *qbus);
+static void pcibus_reset(BusState *qbus);
 
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -165,16 +165,10 @@ void pci_device_deassert_intx(PCIDevice *dev)
     }
 }
 
-/*
- * This function is called on #RST and FLR.
- * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
- */
-void pci_device_reset(PCIDevice *dev)
+static void pci_do_device_reset(PCIDevice *dev)
 {
     int r;
 
-    qdev_reset_all(&dev->qdev);
-
     dev->irq_state = 0;
     pci_update_irq_status(dev);
     pci_device_deassert_intx(dev);
@@ -207,27 +201,34 @@ void pci_device_reset(PCIDevice *dev)
 }
 
 /*
+ * This function is called on #RST and FLR.
+ * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
+ */
+void pci_device_reset(PCIDevice *dev)
+{
+    qdev_reset_all(&dev->qdev);
+    pci_do_device_reset(dev);
+}
+
+/*
  * Trigger pci bus reset under a given bus.
- * To be called on RST# assert.
+ * Called via qbus_reset_all on RST# assert, after the devices
+ * have been reset qdev_reset_all-ed already.
  */
-static int pcibus_reset(BusState *qbus)
+static void pcibus_reset(BusState *qbus)
 {
     PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
     int i;
 
     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
         if (bus->devices[i]) {
-            pci_device_reset(bus->devices[i]);
+            pci_do_device_reset(bus->devices[i]);
         }
     }
 
     for (i = 0; i < bus->nirq; i++) {
         assert(bus->irq_count[i] == 0);
     }
-
-    /* topology traverse is done by pci_bus_reset().
-       Tell qbus/qdev walker not to traverse the tree */
-    return 1;
 }
 
 static void pci_host_bus_register(PCIBus *bus, DeviceState *parent)
include/hw/qdev-core.h +1 -1
index 21ea2c6..409fd71 100644
@@ -178,7 +178,7 @@ struct BusClass {
      * bindings can be found at http://playground.sun.com/1275/bindings/.
      */
     char *(*get_fw_dev_path)(DeviceState *dev);
-    int (*reset)(BusState *bus);
+    void (*reset)(BusState *bus);
     /* maximum devices allowed on the bus, 0: no limit. */
     int max_dev;
 };
Signature · 1 line
1.7.1
#5 lore raw
In reply to Paolo Bonzini
Quoted 5 lines · Paolo Bonzini

PCI is handling resetting of its devices before the bus is reset, but this is only necessary because qdev is broken and usually does pre-order reset. Post-order is a much better definition. Drop the unnecessary flexibility that lets bus decide the reset order, convert to post-order, and make PCI use common code for reset.

Need to go carefully here. I remember a bunch of targets were relying on reset in this order, though don't have the detail right now.

What kind of testing did this patchset go through?

Quoted 12 lines

Paolo Bonzini (4):
pci: do not export pci_bus_reset
pci: clean up resetting of IRQs
qdev: allow both pre- and post-order vists in qdev walking functions
qdev: switch reset to post-order

 hw/core/qdev.c         |   47 ++++++++++++++++++++++++++++++++++-------------
 hw/pci/pci.c           |   42 ++++++++++++++++++++----------------------
 hw/pci/pci_bridge.c    |    2 +-
 include/hw/pci/pci.h   |    1 -
 include/hw/qdev-core.h |   15 ++++++++++-----
 5 files changed, 65 insertions(+), 42 deletions(-)
#6 lore raw

Il 03/10/2013 15:54, Michael S. Tsirkin ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 5 lines

PCI is handling resetting of its devices before the bus is reset, but this is only necessary because qdev is broken and usually does pre-order reset. Post-order is a much better definition. Drop the unnecessary flexibility that lets bus decide the reset order, convert to post-order, and make PCI use common code for reset.

Need to go carefully here. I remember a bunch of targets were relying on reset in this order, though don't have the detail right now.

The main change is that sub-devices, which basically exist only for USB, virtio, and SCSI, are reset _before_ the corresponding parent device (the USB host controller, virtio proxy device, or SCSI HBA).

For SCSI, this patch fixes bugs that I'm currently working around in virtio-scsi (see the manual reset in virtio_scsi_reset, that manually resets the subdevices).

USB and virtio are not using qdev reset at all, so they are unaffected by the patches. They rely on the parent device's own reset method to propagate the reset. virtio already uses post-order, so it could be changed to use qdev reset, but I don't plan to do it unless it fixes bugs. USB is more complicated and I don't really understand it.

For PCI, the change is that all devices are reset first, and then the interrupts state and config space are reset for all devices.

Quoted 1 line

What kind of testing did this patchset go through?

For each PCI device I tried creating a VM with an instance of it (a few devices at a time), and did VM resets. Earlier versions were tested by the guy who reported the SCSI problems.

Paolo

Quoted 14 lines
Quoted 12 lines

Paolo Bonzini (4):
pci: do not export pci_bus_reset
pci: clean up resetting of IRQs
qdev: allow both pre- and post-order vists in qdev walking functions
qdev: switch reset to post-order

 hw/core/qdev.c         |   47 ++++++++++++++++++++++++++++++++++-------------
 hw/pci/pci.c           |   42 ++++++++++++++++++++----------------------
 hw/pci/pci_bridge.c    |    2 +-
 include/hw/pci/pci.h   |    1 -
 include/hw/qdev-core.h |   15 ++++++++++-----
 5 files changed, 65 insertions(+), 42 deletions(-)
#7 lore raw
Quoted 29 lines · Paolo Bonzini

Il 03/10/2013 15:54, Michael S. Tsirkin ha scritto:

Quoted 10 lines · Paolo Bonzini
Quoted 5 lines

PCI is handling resetting of its devices before the bus is reset, but this is only necessary because qdev is broken and usually does pre-order reset. Post-order is a much better definition. Drop the unnecessary flexibility that lets bus decide the reset order, convert to post-order, and make PCI use common code for reset.

Need to go carefully here. I remember a bunch of targets were relying on reset in this order, though don't have the detail right now.

The main change is that sub-devices, which basically exist only for USB, virtio, and SCSI, are reset _before_ the corresponding parent device (the USB host controller, virtio proxy device, or SCSI HBA).

For SCSI, this patch fixes bugs that I'm currently working around in virtio-scsi (see the manual reset in virtio_scsi_reset, that manually resets the subdevices).

USB and virtio are not using qdev reset at all, so they are unaffected by the patches. They rely on the parent device's own reset method to propagate the reset. virtio already uses post-order, so it could be changed to use qdev reset, but I don't plan to do it unless it fixes bugs. USB is more complicated and I don't really understand it.

For PCI, the change is that all devices are reset first, and then the interrupts state and config space are reset for all devices.

Quoted 7 lines
Quoted 1 line

What kind of testing did this patchset go through?

For each PCI device I tried creating a VM with an instance of it (a few devices at a time), and did VM resets. Earlier versions were tested by the guy who reported the SCSI problems.

Paolo

x86 kvm only?

Quoted 14 lines
Quoted 14 lines
Quoted 12 lines

Paolo Bonzini (4):
pci: do not export pci_bus_reset
pci: clean up resetting of IRQs
qdev: allow both pre- and post-order vists in qdev walking functions
qdev: switch reset to post-order

 hw/core/qdev.c         |   47 ++++++++++++++++++++++++++++++++++-------------
 hw/pci/pci.c           |   42 ++++++++++++++++++++----------------------
 hw/pci/pci_bridge.c    |    2 +-
 include/hw/pci/pci.h   |    1 -
 include/hw/qdev-core.h |   15 ++++++++++-----
 5 files changed, 65 insertions(+), 42 deletions(-)
#8 lore raw

Il 03/10/2013 18:54, Michael S. Tsirkin ha scritto:

Quoted 5 lines
Quoted 3 lines

For each PCI device I tried creating a VM with an instance of it (a few devices at a time), and did VM resets. Earlier versions were tested by the guy who reported the SCSI problems.

x86 kvm only?

Yes.

Paolo

#9 lore raw
Quoted 10 lines · Paolo Bonzini

Il 03/10/2013 18:54, Michael S. Tsirkin ha scritto:

Quoted 5 lines
Quoted 3 lines

For each PCI device I tried creating a VM with an instance of it (a few devices at a time), and did VM resets. Earlier versions were tested by the guy who reported the SCSI problems.

x86 kvm only?

Yes.

Paolo

Hmm, I'm not sure that's enough for this kind of change.

#10 lore raw

Il 06/10/2013 20:28, Michael S. Tsirkin ha scritto:

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

For each PCI device I tried creating a VM with an instance of it (a few devices at a time), and did VM resets. Earlier versions were tested by the guy who reported the SCSI problems.

x86 kvm only?

Yes.

Hmm, I'm not sure that's enough for this kind of change.

I'll do more tests though, from looking at the source code, I'm not sure what could happen depending on the host bridge. To make it clearer, the difference is that pre-patch you have

device-level reset for device 1
bus-level reset for device 1
device-level reset for device 2
bus-level reset for device 2
device-level reset for device 3
bus-level reset for device 3

and afterwards you have

device-level reset for device 1
device-level reset for device 2
device-level reset for device 3
bus-level reset for device 1
bus-level reset for device 2
bus-level reset for device 3

I could also preserve exactly the same semantics if you prefer. The patch is a bit more complicated, but it's doable.

Paolo

#11 lore raw

Il 06/10/2013 22:34, Paolo Bonzini ha scritto:

Quoted 5 lines

Il 06/10/2013 20:28, Michael S. Tsirkin ha scritto:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

For each PCI device I tried creating a VM with an instance of it (a few devices at a time), and did VM resets. Earlier versions were tested by the guy who reported the SCSI problems.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

x86 kvm only?

Yes.

Hmm, I'm not sure that's enough for this kind of change.

I'll do more tests though, from looking at the source code, I'm not sure what could happen depending on the host bridge.

Did more tests, PPC g3beige and PPC64 mac99 both work.

I also tested resetting the secondary bus of a PCI bridge (via setpci), and it also works as expected.

Finally, I looked more at the history of the code to justify patch 2.

Initially, zeroing the irq_state was added in commit 6eaa684 (Add pci_bus_reset() function., 2009-06-17) to deal with this issue:

Quoted 7 lines
Quoted 6 lines

Shouldn't each device's reset function bring its line low, thus zeroing the irq_state naturally?

If not, we have a bug somewhere. Note we have exactly the same issue with save/restore.

They should, but I haven't found one that does.

More registers were then cleared by pci_device_reset in your commit c0b1905 (qemu/pci: reset device registers on bus reset, 2009-09-16). Deasserting interrupts explicitly came in later as part of PCI bus and FLR support in commit 4c92325 (pci: deassert intx on reset., 2011-01-20). That should be the point where the code starts following the invariant of patch 2.

Paolo