Skip to contentSkip to conversation

qdev: Fix device_add bus assumptions

PATCH Applied Andreas Färber 5 participants 12 messages Apr 16, 2013 – Apr 22, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qdev: Fix device_add bus assumptions

Drop an unreachable fallback bus assignment to SysBus.

If no ,bus= is specified, only search busses recursively for bus type if the DeviceClass has a bus_type specified. Handle resulting NULL cases.

---
 qdev-monitor.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
qdev-monitor.c +8 -8
index 9a78ccf..73d7946 100644
@@ -18,6 +18,7 @@
  */
 
 #include "hw/qdev.h"
+#include "hw/sysbus.h"
 #include "monitor/monitor.h"
 #include "monitor/qdev.h"
 #include "qmp-commands.h"
@@ -415,7 +416,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     DeviceClass *k;
     const char *driver, *path, *id;
     DeviceState *qdev;
-    BusState *bus;
+    BusState *bus = NULL;
 
     driver = qemu_opt_get(opts, "driver");
     if (!driver) {
@@ -453,7 +454,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
                           driver, object_get_typename(OBJECT(bus)));
             return NULL;
         }
-    } else {
+    } else if (k->bus_type != NULL) {
         bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
         if (!bus) {
             qerror_report(QERR_NO_BUS_FOR_DEVICE,
@@ -461,18 +462,17 @@ DeviceState *qdev_device_add(QemuOpts *opts)
             return NULL;
         }
     }
-    if (qdev_hotplug && !bus->allow_hotplug) {
+    if (qdev_hotplug && bus && !bus->allow_hotplug) {
         qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
         return NULL;
     }
 
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
     id = qemu_opts_id(opts);
     if (id) {
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 44 lines · On Tue, 16 Apr 2013 03:50:21 +0200 Andreas Färber <[email protected]> wrote:

Drop an unreachable fallback bus assignment to SysBus.

If no ,bus= is specified, only search busses recursively for bus type if the DeviceClass has a bus_type specified. Handle resulting NULL cases.

---
 qdev-monitor.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
qdev-monitor.c +3 -2
index 9a78ccf..73d7946 100644
@@ -18,6 +18,7 @@
  */
 
 #include "hw/qdev.h"
+#include "hw/sysbus.h"
 #include "monitor/monitor.h"
 #include "monitor/qdev.h"
 #include "qmp-commands.h"
@@ -415,7 +416,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     DeviceClass *k;
     const char *driver, *path, *id;
     DeviceState *qdev;
-    BusState *bus;
+    BusState *bus = NULL;
 
     driver = qemu_opt_get(opts, "driver");
     if (!driver) {
@@ -453,7 +454,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
                           driver, object_get_typename(OBJECT(bus)));
             return NULL;
         }
-    } else {
+    } else if (k->bus_type != NULL) {
         bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
         if (!bus) {
             qerror_report(QERR_NO_BUS_FOR_DEVICE,
@@ -461,18 +462,17 @@ DeviceState *qdev_device_add(QemuOpts *opts)
             return NULL;
         }
     }

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 472

Quoted 10 lines
qdev-monitor.c +1 -5
-    if (qdev_hotplug && !bus->allow_hotplug) {
+    if (qdev_hotplug && bus && !bus->allow_hotplug) {
         qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
         return NULL;
     }
 
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type, with only one exception of TYPE_CPU. So it should be safe to remove fallback from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set to not NULL in qdev_unplug() and qdev_print()

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -1
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
     }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
         qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
         qbus_print(mon, child, indent);
     }
Quoted 10 lines
qdev-monitor.c +4 -1
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
     id = qemu_opts_id(opts);
     if (id) {
#2 lore raw

On Thu, 18 Apr 2013 10:41:56 +0200
Igor Mammedov <[email protected]> wrote:

[...]

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type, with only one exception of TYPE_CPU. So it should be safe to remove fallback from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set to not NULL in qdev_unplug() and qdev_print()

Err, qdev_print() is safe since it's called on bus children only, so it has parent_bus.

Quoted 45 lines

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -1
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
     }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState *dev,
 int indent) qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
         qbus_print(mon, child, indent);
     }
Quoted 11 lines
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
(quoted diff) +4 -1
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
     id = qemu_opts_id(opts);
     if (id) {
#3 lore raw

Hi Igor,

When I use the config below,an error occurs.Is there anything wrong?

Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -drive file=/home/img/win7.qed,if=virtio,index=0,format=qed -monitor stdio -vga qxl -vnc :1

Error output:
-device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0: Bus 'virtio-serial0.0' is full -device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0: Bus 'virtio-serial0.0' not found

Any feedback are appliciated.
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Igor Mammedov
Sent: Thursday, April 18, 2013 5:02 PM
To: Igor Mammedov
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH] qdev: Fix device_add bus assumptions

On Thu, 18 Apr 2013 10:41:56 +0200
Igor Mammedov <[email protected]> wrote:

[...]

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type, with only one exception of TYPE_CPU. So it should be safe to remove fallback from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set to not NULL in qdev_unplug() and qdev_print()

Err, qdev_print() is safe since it's called on bus children only, so it has parent_bus.

Quoted 45 lines

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -1
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
     }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState *dev,
 int indent) qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
         qbus_print(mon, child, indent);
     }
Quoted 11 lines
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
(quoted diff) +4 -1
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
     id = qemu_opts_id(opts);
     if (id) {
#4 lore raw

Hi,

Am 22.04.2013 13:51, schrieb Libaiqing:

Quoted 9 lines

When I use the config below,an error occurs.Is there anything wrong?

Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -drive file=/home/img/win7.qed,if=virtio,index=0,format=qed -monitor stdio -vga qxl -vnc :1

Error output:
-device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0: Bus 'virtio-serial0.0' is full -device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0: Bus 'virtio-serial0.0' not found

Any feedback are appliciated.

This does not sound related to this patch at all...

Instead it sounds as if the virtio refactorings had some effect not only on virtio-net but also on virtio-serial. Fred?

Andreas

Quoted 70 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Igor Mammedov
Sent: Thursday, April 18, 2013 5:02 PM
To: Igor Mammedov
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH] qdev: Fix device_add bus assumptions

On Thu, 18 Apr 2013 10:41:56 +0200
Igor Mammedov <[email protected]> wrote:

[...]

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type, with only one exception of TYPE_CPU. So it should be safe to remove fallback from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set to not NULL in qdev_unplug() and qdev_print()

Err, qdev_print() is safe since it's called on bus children only, so it has parent_bus.

Quoted 45 lines

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -1
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
     }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState *dev,
 int indent) qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
         qbus_print(mon, child, indent);
     }
Quoted 11 lines
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
(quoted diff) +4 -1
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
     id = qemu_opts_id(opts);
     if (id) {
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#5 lore raw
Quoted 11 lines · Andreas Färber

Hi,

Am 22.04.2013 13:51, schrieb Libaiqing:

Quoted 8 lines

When I use the config below,an error occurs.Is there anything wrong?

   Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c  -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0  -drive file=/home/img/win7.qed,if=virtio,index=0,format=qed  -monitor stdio   -vga qxl  -vnc :1

Error output:
-device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0: Bus 'virtio-serial0.0' is full -device virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0: Bus 'virtio-serial0.0' not found

Quoted 7 lines
Quoted 1 line

Any feedback are appliciated.

This does not sound related to this patch at all...

Instead it sounds as if the virtio refactorings had some effect not only on virtio-net but also on virtio-serial. Fred?

Andreas

Hi,

Yes, sounds like the same issue as virtio-net:

bus: pci.0
type PCI
dev: virtio-serial-pci, id "virtio-serial0"
ioeventfd = off
vectors = 2
class = 0x780
indirect_desc = on
event_idx = on
max_ports = 31
addr = 04.0
romfile = <null>
rombar = 1
multifunction = off
command_serr_enable = on
class Class 0780, addr 00:04.0, pci id 1af4:1003 (sub 1af4:0003) bar 0: i/o at 0xc040 [0xc05f]
bar 1: mem at 0xfebf1000 [0xfebf1fff]
bus: virtio-serial0.0
type virtio-pci-bus
dev: virtio-serial-device, id ""
max_ports = 31
bus: virtio-serial-bus.0
type virtio-serial-bus

The autogenerated bus name "deviceid.n" (virtio-serial0.0) became a virtio-bus...

virtio-serial-bus.0 is the right bus to connect virtserialport.

Any idea how to fix that?

Sorry for that,
Fred

Quoted 69 lines
Quoted 68 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Igor Mammedov
Sent: Thursday, April 18, 2013 5:02 PM
To: Igor Mammedov
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH] qdev: Fix device_add bus assumptions

On Thu, 18 Apr 2013 10:41:56 +0200
Igor Mammedov <[email protected]> wrote:

[...]

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type, with only one exception of TYPE_CPU. So it should be safe to remove fallback from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set to not NULL in qdev_unplug() and qdev_print()

Err, qdev_print() is safe since it's called on bus children only, so it has parent_bus.

Quoted 43 lines

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -1
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
  {
      DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
          error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
          return;
      }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState *dev,
 int indent) qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
          class = object_class_get_parent(class);
      } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
      QLIST_FOREACH(child, &dev->child_bus, sibling) {
          qbus_print(mon, child, indent);
      }
Quoted 11 lines
      /* create device, set properties */
      qdev = DEVICE(object_new(driver));
(quoted diff) +4 -1
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
      id = qemu_opts_id(opts);
      if (id) {
#6 lore raw

Hi Fred,

Am 22.04.2013 15:54, schrieb KONRAD Frédéric:

Quoted 51 lines · Andreas Färber
Quoted 18 lines

Am 22.04.2013 13:51, schrieb Libaiqing:

Quoted 17 lines

When I use the config below,an error occurs.Is there anything wrong?

   Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c 
(quoted diff) +0 -2
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4
-chardev spicevmc,id=charchannel0,name=vdagent -device

virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -drive file=/home/img/win7.qed,if=virtio,index=0,format=qed -monitor
stdio -vga qxl -vnc :1

Error output:
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:
Bus 'virtio-serial0.0' is full
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:
Bus 'virtio-serial0.0' not found

Quoted 5 lines
Quoted 1 line

Any feedback are appliciated.

This does not sound related to this patch at all...

Instead it sounds as if the virtio refactorings had some effect not only on virtio-net but also on virtio-serial. Fred?

Yes, sounds like the same issue as virtio-net:

bus: pci.0
type PCI
dev: virtio-serial-pci, id "virtio-serial0"
ioeventfd = off
vectors = 2
class = 0x780
indirect_desc = on
event_idx = on
max_ports = 31
addr = 04.0
romfile = <null>
rombar = 1
multifunction = off
command_serr_enable = on
class Class 0780, addr 00:04.0, pci id 1af4:1003 (sub 1af4:0003) bar 0: i/o at 0xc040 [0xc05f]
bar 1: mem at 0xfebf1000 [0xfebf1fff]
bus: virtio-serial0.0
type virtio-pci-bus
dev: virtio-serial-device, id ""
max_ports = 31
bus: virtio-serial-bus.0
type virtio-serial-bus

Quoted 6 lines

The autogenerated bus name "deviceid.n" (virtio-serial0.0) became a virtio-bus...

virtio-serial-bus.0 is the right bus to connect virtserialport.

Any idea how to fix that?

The only idea I can come up with right now is to overwrite the bus name on realize/qdev-init of the containing (virtio-serial-pci) device.

Whether we want that is another question... :) It would fix command line backwards compatibility but would be inconsistent then; I guess the former is more important here.

Regards,
Andreas

Quoted 82 lines

Sorry for that,
Fred

Quoted 77 lines
Quoted 76 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Igor Mammedov
Sent: Thursday, April 18, 2013 5:02 PM
To: Igor Mammedov
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH] qdev: Fix device_add bus assumptions

On Thu, 18 Apr 2013 10:41:56 +0200
Igor Mammedov <[email protected]> wrote:

[...]

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-
Quoted 8 lines

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type,
with only one exception of TYPE_CPU. So it should be safe to remove fallback
from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set
to not NULL in qdev_unplug() and qdev_print()

Err, qdev_print() is safe since it's called on bus children only, so it has
parent_bus.

Quoted 43 lines

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -0
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
  {
      DeviceClass *dc = DEVICE_GET_CLASS(dev);
  -    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
          error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
          return;
      }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState
 *dev,
 int indent) qdev_print_props(mon, dev, DEVICE_CLASS(class)->props,
 indent);
          class = object_class_get_parent(class);
      } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
      QLIST_FOREACH(child, &dev->child_bus, sibling) {
          qbus_print(mon, child, indent);
      }
Quoted 9 lines
      /* create device, set properties */
      qdev = DEVICE(object_new(driver));
(quoted diff) +4 -1
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
        id = qemu_opts_id(opts);
      if (id) {
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#7 lore raw
Quoted 62 lines · Andreas Färber

Hi Fred,

Am 22.04.2013 15:54, schrieb KONRAD Frédéric:

Quoted 50 lines · Andreas Färber
Quoted 18 lines

Am 22.04.2013 13:51, schrieb Libaiqing:

Quoted 17 lines

When I use the config below,an error occurs.Is there anything wrong?

Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c

(quoted diff) +0 -2
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4
-chardev spicevmc,id=charchannel0,name=vdagent -device

virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -drive file=/home/img/win7.qed,if=virtio,index=0,format=qed -monitor
stdio -vga qxl -vnc :1

Error output:
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:
Bus 'virtio-serial0.0' is full
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:
Bus 'virtio-serial0.0' not found

Quoted 5 lines
Quoted 1 line

Any feedback are appliciated.

This does not sound related to this patch at all...

Instead it sounds as if the virtio refactorings had some effect not only on virtio-net but also on virtio-serial. Fred?

Yes, sounds like the same issue as virtio-net:

bus: pci.0
type PCI
dev: virtio-serial-pci, id "virtio-serial0"
ioeventfd = off
vectors = 2
class = 0x780
indirect_desc = on
event_idx = on
max_ports = 31
addr = 04.0
romfile = <null>
rombar = 1
multifunction = off
command_serr_enable = on
class Class 0780, addr 00:04.0, pci id 1af4:1003 (sub 1af4:0003) bar 0: i/o at 0xc040 [0xc05f]
bar 1: mem at 0xfebf1000 [0xfebf1fff]
bus: virtio-serial0.0
type virtio-pci-bus
dev: virtio-serial-device, id ""
max_ports = 31
bus: virtio-serial-bus.0
type virtio-serial-bus

Quoted 6 lines

The autogenerated bus name "deviceid.n" (virtio-serial0.0) became a virtio-bus...

virtio-serial-bus.0 is the right bus to connect virtserialport.

Any idea how to fix that?

The only idea I can come up with right now is to overwrite the bus name on realize/qdev-init of the containing (virtio-serial-pci) device.

Quoted 6 lines

Whether we want that is another question... :) It would fix command line backwards compatibility but would be inconsistent then; I guess the former is more important here.

Regards,
Andreas

I'm not sure that only overwriting the bus name will fix the issue.

virtio-serial-device's bus still won't have the right name?

Here with the command line, we expect virtio-serial-pci,id=id0 to create a virtio-serial-bus called id0.0 is that right?

Thanks,
Fred

Quoted 78 lines
Quoted 77 lines

Sorry for that,
Fred

Quoted 73 lines
Quoted 73 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Igor Mammedov
Sent: Thursday, April 18, 2013 5:02 PM
To: Igor Mammedov
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH] qdev: Fix device_add bus assumptions

On Thu, 18 Apr 2013 10:41:56 +0200
Igor Mammedov <[email protected]> wrote:

[...]

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +0 -4
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-
Quoted 8 lines

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type,
with only one exception of TYPE_CPU. So it should be safe to remove fallback
from qdev_device_add POV.
However TYPE_CPU breaks assumption that device always has parent_bus set
to not NULL in qdev_unplug() and qdev_print()

Err, qdev_print() is safe since it's called on bus children only, so it has
parent_bus.

Quoted 42 lines

It would be better to add something like this:
// untested

hw/core/qdev.c +1 -0
index 4eb0134..45009ba 100644
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
   {
       DeviceClass *dc = DEVICE_GET_CLASS(dev);
   -    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
           error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
           return;
       }
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState
 *dev,
 int indent) qdev_print_props(mon, dev, DEVICE_CLASS(class)->props,
 indent);
           class = object_class_get_parent(class);
       } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
       QLIST_FOREACH(child, &dev->child_bus, sibling) {
           qbus_print(mon, child, indent);
       }
Quoted 9 lines
       /* create device, set properties */
       qdev = DEVICE(object_new(driver));
(quoted diff) +4 -1
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
         id = qemu_opts_id(opts);
       if (id) {
#8 lore raw

Am 22.04.2013 16:15, schrieb KONRAD Frédéric:

Quoted 73 lines · Andreas Färber
Quoted 72 lines

Hi Fred,

Am 22.04.2013 15:54, schrieb KONRAD Frédéric:

Quoted 56 lines · Andreas Färber
Quoted 20 lines

Am 22.04.2013 13:51, schrieb Libaiqing:

Quoted 19 lines

When I use the config below,an error occurs.Is there anything
wrong?

    Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c
(quoted diff) +0 -2
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4
-chardev spicevmc,id=charchannel0,name=vdagent -device

virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0

-drive file=/home/img/win7.qed,if=virtio,index=0,format=qed -monitor
stdio -vga qxl -vnc :1

Error output:
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:

Bus 'virtio-serial0.0' is full
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:

Quoted 8 lines
Quoted 3 lines

Bus 'virtio-serial0.0' not found

Any feedback are appliciated.

This does not sound related to this patch at all...

Instead it sounds as if the virtio refactorings had some effect not only
on virtio-net but also on virtio-serial. Fred?

Yes, sounds like the same issue as virtio-net:

bus: pci.0
type PCI
dev: virtio-serial-pci, id "virtio-serial0"
ioeventfd = off
vectors = 2
class = 0x780
indirect_desc = on
event_idx = on
max_ports = 31
addr = 04.0
romfile = <null>
rombar = 1
multifunction = off
command_serr_enable = on
class Class 0780, addr 00:04.0, pci id 1af4:1003 (sub
1af4:0003)

         bar 0: i/o at 0xc040 [0xc05f]
         bar 1: mem at 0xfebf1000 [0xfebf1fff]
         bus: virtio-serial0.0
           type virtio-pci-bus
           dev: virtio-serial-device, id ""
             max_ports = 31
             bus: virtio-serial-bus.0
               type virtio-serial-bus
Quoted 6 lines

The autogenerated bus name "deviceid.n" (virtio-serial0.0) became a virtio-bus...

virtio-serial-bus.0 is the right bus to connect virtserialport.

Any idea how to fix that?

The only idea I can come up with right now is to overwrite the bus name on realize/qdev-init of the containing (virtio-serial-pci) device.

Whether we want that is another question... :) It would fix command line backwards compatibility but would be inconsistent then; I guess the former is more important here.

Quoted 5 lines
Quoted 2 lines

Regards,
Andreas

I'm not sure that only overwriting the bus name will fix the issue.

virtio-serial-device's bus still won't have the right name?

I was talking of virtio-serial-pci overwriting virtio-serial-device's bus with its own id.0 after it's been created by virtio-serial-device with NULL argument! Assuming it gets created in instance_init and can't just access its parent's id in its own realize function to have it correct from the start.

Andreas

Quoted 5 lines

Here with the command line, we expect virtio-serial-pci,id=id0 to create a virtio-serial-bus called id0.0 is that right?

Thanks,
Fred

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#9 lore raw
Quoted 78 lines · Andreas Färber

Am 22.04.2013 16:15, schrieb KONRAD Frédéric:

Quoted 77 lines · Andreas Färber
Quoted 68 lines

Hi Fred,

Am 22.04.2013 15:54, schrieb KONRAD Frédéric:

Quoted 56 lines · Andreas Färber
Quoted 20 lines

Am 22.04.2013 13:51, schrieb Libaiqing:

Quoted 19 lines

When I use the config below,an error occurs.Is there anything
wrong?

     Qemu-kvm -enable-kvm -name win7 -M pc-0.15 -m 1024 -smp 2 -boot c
(quoted diff) +0 -2
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4
-chardev spicevmc,id=charchannel0,name=vdagent -device

virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0

-drive file=/home/img/win7.qed,if=virtio,index=0,format=qed -monitor
stdio -vga qxl -vnc :1

Error output:
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:

Bus 'virtio-serial0.0' is full
-device
virtserialport,bus=virtio-serial0.0,chardev=charchannel0,id=channel0,name=com.redhat.spice.0:

Quoted 8 lines
Quoted 3 lines

Bus 'virtio-serial0.0' not found

Any feedback are appliciated.

This does not sound related to this patch at all...

Instead it sounds as if the virtio refactorings had some effect not only
on virtio-net but also on virtio-serial. Fred?

Yes, sounds like the same issue as virtio-net:

bus: pci.0
type PCI
dev: virtio-serial-pci, id "virtio-serial0"
ioeventfd = off
vectors = 2
class = 0x780
indirect_desc = on
event_idx = on
max_ports = 31
addr = 04.0
romfile = <null>
rombar = 1
multifunction = off
command_serr_enable = on
class Class 0780, addr 00:04.0, pci id 1af4:1003 (sub
1af4:0003)

          bar 0: i/o at 0xc040 [0xc05f]
          bar 1: mem at 0xfebf1000 [0xfebf1fff]
          bus: virtio-serial0.0
            type virtio-pci-bus
            dev: virtio-serial-device, id ""
              max_ports = 31
              bus: virtio-serial-bus.0
                type virtio-serial-bus
Quoted 6 lines

The autogenerated bus name "deviceid.n" (virtio-serial0.0) became a virtio-bus...

virtio-serial-bus.0 is the right bus to connect virtserialport.

Any idea how to fix that?

The only idea I can come up with right now is to overwrite the bus name on realize/qdev-init of the containing (virtio-serial-pci) device.

Quoted 6 lines

Whether we want that is another question... :) It would fix command line backwards compatibility but would be inconsistent then; I guess the former is more important here.

Regards,
Andreas

I'm not sure that only overwriting the bus name will fix the issue.

Quoted 8 lines
Quoted 1 line

virtio-serial-device's bus still won't have the right name?

I was talking of virtio-serial-pci overwriting virtio-serial-device's bus with its own id.0 after it's been created by virtio-serial-device with NULL argument! Assuming it gets created in instance_init and can't just access its parent's id in its own realize function to have it correct from the start.

Andreas

Ok, I'll take a look.

Thanks for help :),
Fred

Quoted 6 lines
Quoted 6 lines

Here with the command line, we expect virtio-serial-pci,id=id0 to create a virtio-serial-bus called id0.0 is that right?

Thanks,
Fred

#10 lore raw
In reply to Igor Mammedov

Am 18.04.2013 10:41, schrieb Igor Mammedov:

Quoted 60 lines
Quoted 44 lines · On Tue, 16 Apr 2013 03:50:21 +0200 Andreas Färber <[email protected]> wrote:

Drop an unreachable fallback bus assignment to SysBus.

If no ,bus= is specified, only search busses recursively for bus type if the DeviceClass has a bus_type specified. Handle resulting NULL cases.

---
 qdev-monitor.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
qdev-monitor.c +3 -2
index 9a78ccf..73d7946 100644
@@ -18,6 +18,7 @@
  */
 
 #include "hw/qdev.h"
+#include "hw/sysbus.h"
 #include "monitor/monitor.h"
 #include "monitor/qdev.h"
 #include "qmp-commands.h"
@@ -415,7 +416,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
     DeviceClass *k;
     const char *driver, *path, *id;
     DeviceState *qdev;
-    BusState *bus;
+    BusState *bus = NULL;
 
     driver = qemu_opt_get(opts, "driver");
     if (!driver) {
@@ -453,7 +454,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)
                           driver, object_get_typename(OBJECT(bus)));
             return NULL;
         }
-    } else {
+    } else if (k->bus_type != NULL) {
         bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
         if (!bus) {
             qerror_report(QERR_NO_BUS_FOR_DEVICE,
@@ -461,18 +462,17 @@ DeviceState *qdev_device_add(QemuOpts *opts)
             return NULL;
         }
     }

qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 472

Quoted 10 lines
qdev-monitor.c +1 -5
-    if (qdev_hotplug && !bus->allow_hotplug) {
+    if (qdev_hotplug && bus && !bus->allow_hotplug) {
         qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
         return NULL;
     }
 
-    if (!bus) {
-        bus = sysbus_get_default();
-    }
-

I've checked all direct childs of TYPE_DEVICE and they all set k->bus_type, with only one exception of TYPE_CPU.

Quoted 2 lines

So it should be safe to remove fallback
from qdev_device_add POV.

Sorry, don't understand what exactly you are suggesting?

Quoted 11 lines

However TYPE_CPU breaks assumption that device always has parent_bus set to not NULL in qdev_unplug() and qdev_print()

It would be better to add something like this:
// untested

hw/core/qdev.c +0 -0
index 4eb0134..45009ba 100644

hw/core/qdev.c void qdev_unplug(DeviceState *dev, Error **errp) · line 213

Quoted 8 lines
hw/core/qdev.c +1 -1
@@ -207,7 +207,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-    if (!dev->parent_bus->allow_hotplug) {
+    if (dev->parent_bus && !dev->parent_bus->allow_hotplug) {
         error_set(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
     }

This part looks good, can you resend as a proper patch? Even if we can't test the no-parent_bus unplug path ATM, it shouldn't hurt to fix assumptions about it.

Andreas

Quoted 28 lines
qdev-monitor.c +3 -1
index 9a78ccf..2476e4e 100644
@@ -557,7 +557,9 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
         qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
         class = object_class_get_parent(class);
     } while (class != object_class_by_name(TYPE_DEVICE));
-    bus_print_dev(dev->parent_bus, mon, dev, indent);
+    if (dev->parent_bus) {
+        bus_print_dev(dev->parent_bus, mon, dev, indent);
+    }
     QLIST_FOREACH(child, &dev->child_bus, sibling) {
         qbus_print(mon, child, indent);
     }
Quoted 11 lines
qdev-monitor.c +4 -1
     /* create device, set properties */
     qdev = DEVICE(object_new(driver));
-    qdev_set_parent_bus(qdev, bus);
+
+    if (bus) {
+        qdev_set_parent_bus(qdev, bus);
+    }
 
     id = qemu_opts_id(opts);
     if (id) {
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg