Skip to contentSkip to conversation

pci: unregister vmstate_pcibus on unplug

PATCH In review Bandan Das 4 participants 7 messages Nov 6, 2013 – Nov 19, 2013

lore
Share & tools
lore

Bandan Das

author
#0 lore raw

PATCH 1/1 pci: unregister vmstate_pcibus on unplug

PCIBus registers a vmstate during init. Unregister it upon removal/unplug.

---
Note that I didn't add a instance_init to register vmstate (yet) due to concerns expressed by Andreas that we shouldn't be registering global state there.

 hw/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/pci/pci.c +8 -0
index a98c8a0..63ef7ce 100644
@@ -47,6 +47,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 pci_bus_finalize(Object *obj);
 
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -73,6 +74,7 @@ static const TypeInfo pci_bus_info = {
     .name = TYPE_PCI_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(PCIBus),
+    .instance_finalize = pci_bus_finalize,
     .class_init = pci_bus_class_init,
 };
 
@@ -401,6 +403,12 @@ int pci_bus_num(PCIBus *s)
     return s->parent_dev->config[PCI_SECONDARY_BUS];
 }
 
+static void pci_bus_finalize(Object *obj)
+{
+    PCIBus *bus = PCI_BUS(obj);
+    vmstate_unregister(NULL, &vmstate_pcibus, bus);
+}
+
 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
 {
     PCIDevice *s = container_of(pv, PCIDevice, config);
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw

Am 06.11.2013 23:52, schrieb Bandan Das:

Quoted 5 lines

PCIBus registers a vmstate during init. Unregister it upon removal/unplug.

Michael, this patch looks good for 1.7 to me, are you planning to still pick it up? Only one small comment below.

Quoted 4 lines

---
Note that I didn't add a instance_init to register vmstate (yet) due to concerns expressed by Andreas that we shouldn't be registering global state there.

What's happening here is the following: instance_init does in fact not register anything, but vmstate_unregister() becomes a no-op loop if the vmsd+opaque combo is not registered, so it is safe. The registration happens in pci_bus_new() / pci_bus_new_inplace(), which I believe all PCI buses to date inside QEMU use, i.e. after instance_init, so in practice unregistering will not be no-op.

hw/pci/pci.c static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); · line 50

Quoted 12 lines
 hw/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/pci/pci.c +1 -0
index a98c8a0..63ef7ce 100644
@@ -47,6 +47,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 pci_bus_finalize(Object *obj);

It may be nicer to avoid the prototype by moving the new pci_bus_finalize() above pci_bus_info. But since what counts is the fix to avoid segfaults during migration on access to a dangling opaque pointer after hot-unplug of a PCI-PCI bridge,

Thanks,
Andreas

Quoted 24 lines
hw/pci/pci.c +7 -0
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -73,6 +74,7 @@ static const TypeInfo pci_bus_info = {
     .name = TYPE_PCI_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(PCIBus),
+    .instance_finalize = pci_bus_finalize,
     .class_init = pci_bus_class_init,
 };
 
@@ -401,6 +403,12 @@ int pci_bus_num(PCIBus *s)
     return s->parent_dev->config[PCI_SECONDARY_BUS];
 }
 
+static void pci_bus_finalize(Object *obj)
+{
+    PCIBus *bus = PCI_BUS(obj);
+    vmstate_unregister(NULL, &vmstate_pcibus, bus);
+}
+
 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
 {
     PCIDevice *s = container_of(pv, PCIDevice, config);
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
#2 lore raw
Quoted 41 lines · Andreas Färber

Am 06.11.2013 23:52, schrieb Bandan Das:

Quoted 5 lines

PCIBus registers a vmstate during init. Unregister it upon removal/unplug.

Michael, this patch looks good for 1.7 to me, are you planning to still pick it up? Only one small comment below.

Quoted 4 lines

---
Note that I didn't add a instance_init to register vmstate (yet) due to concerns expressed by Andreas that we shouldn't be registering global state there.

What's happening here is the following: instance_init does in fact not register anything, but vmstate_unregister() becomes a no-op loop if the vmsd+opaque combo is not registered, so it is safe. The registration happens in pci_bus_new() / pci_bus_new_inplace(), which I believe all PCI buses to date inside QEMU use, i.e. after instance_init, so in practice unregistering will not be no-op.

hw/pci/pci.c static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); · line 50

Quoted 12 lines
 hw/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/pci/pci.c +1 -0
index a98c8a0..63ef7ce 100644
@@ -47,6 +47,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 pci_bus_finalize(Object *obj);

It may be nicer to avoid the prototype by moving the new pci_bus_finalize() above pci_bus_info. But since what counts is the fix to avoid segfaults during migration on access to a dangling opaque pointer after hot-unplug of a PCI-PCI bridge,

Quoted 5 lines

Thanks,
Andreas

Thanks for the review, I'll review and hopefully merge later today.

Quoted 28 lines
Quoted 24 lines
hw/pci/pci.c +7 -0
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -73,6 +74,7 @@ static const TypeInfo pci_bus_info = {
     .name = TYPE_PCI_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(PCIBus),
+    .instance_finalize = pci_bus_finalize,
     .class_init = pci_bus_class_init,
 };
 
@@ -401,6 +403,12 @@ int pci_bus_num(PCIBus *s)
     return s->parent_dev->config[PCI_SECONDARY_BUS];
 }
 
+static void pci_bus_finalize(Object *obj)
+{
+    PCIBus *bus = PCI_BUS(obj);
+    vmstate_unregister(NULL, &vmstate_pcibus, bus);
+}
+
 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
 {
     PCIDevice *s = container_of(pv, PCIDevice, config);
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
author
#3 lore raw
In reply to Andreas Färber
Quoted 17 lines · Andreas Färber

Am 06.11.2013 23:52, schrieb Bandan Das:

Quoted 5 lines

PCIBus registers a vmstate during init. Unregister it upon removal/unplug.

Michael, this patch looks good for 1.7 to me, are you planning to still pick it up? Only one small comment below.

Quoted 4 lines

---
Note that I didn't add a instance_init to register vmstate (yet) due to concerns expressed by Andreas that we shouldn't be registering global state there.

Quoted 6 lines

What's happening here is the following: instance_init does in fact not register anything, but vmstate_unregister() becomes a no-op loop if the vmsd+opaque combo is not registered, so it is safe. The registration happens in pci_bus_new() / pci_bus_new_inplace(), which I believe all PCI buses to date inside QEMU use, i.e. after instance_init, so in practice unregistering will not be no-op.

Ok, thanks! Based on your explanation, I think it should be safe to move vmstate_register to instance_init as Paolo had suggested. If Michael and rest of the folks agree, I am inclined to send in a new version (which also fixes the issue you noted below).

Bandan

Quoted 47 lines

hw/pci/pci.c static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); · line 50

Quoted 12 lines
 hw/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/pci/pci.c +1 -0
index a98c8a0..63ef7ce 100644
@@ -47,6 +47,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 pci_bus_finalize(Object *obj);

It may be nicer to avoid the prototype by moving the new pci_bus_finalize() above pci_bus_info. But since what counts is the fix to avoid segfaults during migration on access to a dangling opaque pointer after hot-unplug of a PCI-PCI bridge,

Thanks,
Andreas

Quoted 24 lines
hw/pci/pci.c +7 -0
 static Property pci_props[] = {
     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
@@ -73,6 +74,7 @@ static const TypeInfo pci_bus_info = {
     .name = TYPE_PCI_BUS,
     .parent = TYPE_BUS,
     .instance_size = sizeof(PCIBus),
+    .instance_finalize = pci_bus_finalize,
     .class_init = pci_bus_class_init,
 };
 
@@ -401,6 +403,12 @@ int pci_bus_num(PCIBus *s)
     return s->parent_dev->config[PCI_SECONDARY_BUS];
 }
 
+static void pci_bus_finalize(Object *obj)
+{
+    PCIBus *bus = PCI_BUS(obj);
+    vmstate_unregister(NULL, &vmstate_pcibus, bus);
+}
+
 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
 {
     PCIDevice *s = container_of(pv, PCIDevice, config);
#4 lore raw

Am 19.11.2013 18:03, schrieb Bandan Das:

Quoted 26 lines
Quoted 17 lines · Andreas Färber

Am 06.11.2013 23:52, schrieb Bandan Das:

Quoted 5 lines

PCIBus registers a vmstate during init. Unregister it upon removal/unplug.

Michael, this patch looks good for 1.7 to me, are you planning to still pick it up? Only one small comment below.

Quoted 4 lines

---
Note that I didn't add a instance_init to register vmstate (yet) due to concerns expressed by Andreas that we shouldn't be registering global state there.

Quoted 6 lines

What's happening here is the following: instance_init does in fact not register anything, but vmstate_unregister() becomes a no-op loop if the vmsd+opaque combo is not registered, so it is safe. The registration happens in pci_bus_new() / pci_bus_new_inplace(), which I believe all PCI buses to date inside QEMU use, i.e. after instance_init, so in practice unregistering will not be no-op.

Quoted 2 lines

Ok, thanks! Based on your explanation, I think it should be safe to move vmstate_register to instance_init as Paolo had suggested.

Why? I still think that would be wrong. We had previously discussed with Paolo that VMState is global state, which according to Anthony should not be registered before realization. So far we have a mix of PCI host bridges instantiating PCIBus before or after realization depending on whether the bus name needs to depend on the device id or not (with trend towards instantiating the PCIBus during instance_init), at which point in time the state should not be registered yet. The sketched solution was to implement a "realized" property for BusState, so that we can decouple vmstate_register() from instantation time rather than moving it into instance_init.

Andreas

Quoted 3 lines

If Michael
and rest of the folks agree, I am inclined to send in a new version (which also fixes the issue you noted below).

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

Il 19/11/2013 18:33, Andreas Färber ha scritto:

Quoted 10 lines

Why? I still think that would be wrong. We had previously discussed with Paolo that VMState is global state, which according to Anthony should not be registered before realization. So far we have a mix of PCI host bridges instantiating PCIBus before or after realization depending on whether the bus name needs to depend on the device id or not (with trend towards instantiating the PCIBus during instance_init), at which point in time the state should not be registered yet. The sketched solution was to implement a "realized" property for BusState, so that we can decouple vmstate_register() from instantation time rather than moving it into instance_init.

I agree. However, this is also a bug fix, and it would be a bit ugly to tie it to large infrastructure changes (same reason why I want to have my virtio hotplug/unplug fixes go in _before_ the realize patches, for example).

With that in mind, Bandan's patch could be good for 1.7.

Paolo

author
#6 lore raw
In reply to Andreas Färber
Quoted 27 lines · Andreas Färber

Am 19.11.2013 18:03, schrieb Bandan Das:

Quoted 26 lines
Quoted 23 lines · Andreas Färber

Am 06.11.2013 23:52, schrieb Bandan Das:

Quoted 5 lines

PCIBus registers a vmstate during init. Unregister it upon removal/unplug.

Michael, this patch looks good for 1.7 to me, are you planning to still pick it up? Only one small comment below.

Quoted 4 lines

---
Note that I didn't add a instance_init to register vmstate (yet) due to concerns expressed by Andreas that we shouldn't be registering global state there.

What's happening here is the following: instance_init does in fact not register anything, but vmstate_unregister() becomes a no-op loop if the vmsd+opaque combo is not registered, so it is safe. The registration happens in pci_bus_new() / pci_bus_new_inplace(), which I believe all PCI buses to date inside QEMU use, i.e. after instance_init, so in practice unregistering will not be no-op.

Quoted 6 lines
Quoted 2 lines

Ok, thanks! Based on your explanation, I think it should be safe to move vmstate_register to instance_init as Paolo had suggested.

Why? I still think that would be wrong. We had previously discussed with Paolo that VMState is global state, which according to Anthony should not be registered before realization. So far we have a mix of PCI host

Ugh. I again ignored this piece of information. And also got the other part wrong - pci_bus_new is called *after* instance_init, which means instance_init isn't the right place for a global state registration. Agreed, vmstate_register is at the right place currently.

Quoted 13 lines

bridges instantiating PCIBus before or after realization depending on whether the bus name needs to depend on the device id or not (with trend towards instantiating the PCIBus during instance_init), at which point in time the state should not be registered yet. The sketched solution was to implement a "realized" property for BusState, so that we can decouple vmstate_register() from instantation time rather than moving it into instance_init.

Andreas

Quoted 3 lines

If Michael
and rest of the folks agree, I am inclined to send in a new version (which also fixes the issue you noted below).