Skip to contentSkip to conversation

xen-block: Fix removal of backend instance via xenstore

PATCH In review Anthony PERARD via 3 participants 8 messages Mar 8, 2021 – Mar 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-block: Fix removal of backend instance via xenstore

From: Anthony PERARD <[email protected]>

Whenever a Xen block device is detach via xenstore, the image associated with it remained open by the backend QEMU and an error is logged:
qemu-system-i386: failed to destroy drive: Node xvdz-qcow2 is in use

This happened since object_unparent() doesn't immediately frees the object and thus keep a reference to the node we are trying to free. The reference is hold by the "drive" property and the call xen_block_drive_destroy() fails.

In order to fix that, we call drain_call_rcu() to run the callback setup by bus_remove_child() via object_unparent().

Fixes 2d24a6466154 ("device-core: use RCU for list of children of a bus")

---
CCing people whom introduced/reviewed the change to use RCU to give them a chance to say if the change is fine.

---
 hw/block/xen-block.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/block/xen-block.c +9 -0
index a3b69e27096f..fe5f828e2d25 100644
@@ -972,6 +972,15 @@ static void xen_block_device_destroy(XenBackendInstance *backend,
 
     object_unparent(OBJECT(xendev));
 
+    /*
+     * Drall all pending RCU callbacks as object_unparent() frees `xendev'
+     * in a RCU callback.
+     * And due to the property "drive" still existing in `xendev', we
+     * cann't destroy the XenBlockDrive associated with `xendev' with
+     * xen_block_drive_destroy() below.
+     */
+    drain_call_rcu();
+
     if (iothread) {
         xen_block_iothread_destroy(iothread, errp);
         if (*errp) {
Signature · 1 line
Anthony PERARD
#1 lore raw
Quoted 15 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Whenever a Xen block device is detach via xenstore, the image associated with it remained open by the backend QEMU and an error is logged:
qemu-system-i386: failed to destroy drive: Node xvdz-qcow2 is in use

This happened since object_unparent() doesn't immediately frees the object and thus keep a reference to the node we are trying to free. The reference is hold by the "drive" property and the call xen_block_drive_destroy() fails.

In order to fix that, we call drain_call_rcu() to run the callback setup by bus_remove_child() via object_unparent().

Quoted 6 lines
Fixes 2d24a6466154 ("device-core: use RCU for list of children of a bus")

---
CCing people whom introduced/reviewed the change to use RCU to give them a chance to say if the change is fine.

If nothing else works then I guess it's okay, but why can't you do the xen_block_drive_destroy from e.g. an unrealize callback?

Paolo

Quoted 25 lines
---
  hw/block/xen-block.c | 9 +++++++++
  1 file changed, 9 insertions(+)
hw/block/xen-block.c +9 -0
index a3b69e27096f..fe5f828e2d25 100644
@@ -972,6 +972,15 @@ static void xen_block_device_destroy(XenBackendInstance *backend,
 
      object_unparent(OBJECT(xendev));
 
+    /*
+     * Drall all pending RCU callbacks as object_unparent() frees `xendev'
+     * in a RCU callback.
+     * And due to the property "drive" still existing in `xendev', we
+     * cann't destroy the XenBlockDrive associated with `xendev' with
+     * xen_block_drive_destroy() below.
+     */
+    drain_call_rcu();
+
      if (iothread) {
          xen_block_iothread_destroy(iothread, errp);
          if (*errp) {
#2 lore raw
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Whenever a Xen block device is detach via xenstore, the image associated with it remained open by the backend QEMU and an error is logged:
qemu-system-i386: failed to destroy drive: Node xvdz-qcow2 is in use

This happened since object_unparent() doesn't immediately frees the object and thus keep a reference to the node we are trying to free. The reference is hold by the "drive" property and the call xen_block_drive_destroy() fails.

In order to fix that, we call drain_call_rcu() to run the callback setup by bus_remove_child() via object_unparent().

Fixes 2d24a6466154 ("device-core: use RCU for list of children of a bus")
Quoted 7 lines
Quoted 4 lines

---
CCing people whom introduced/reviewed the change to use RCU to give them a chance to say if the change is fine.

If nothing else works then I guess it's okay, but why can't you do the xen_block_drive_destroy from e.g. an unrealize callback?

I'm not sure if that's possible.

xen_block_device_create/xen_block_device_destroy() is supposed to be
equivalent to do those qmp commands:
blockdev-add node-name=xvdz-qcow2 driver=qcow2 file={"driver":"file","filename":"disk.qcow2","locking":"off"} device_add id=xvdz driver=xen-disk vdev=xvdz drive=xvdz-qcow2

But I tried to add a call xen_block_drive_destroy from xen_block_unrealize, but that still is called too early, it's called before object_property_del_all() which would delete "drive" and call release_drive() which would free the node.

So, no, I don't think we can use an unrealized callback.

I though of trying to delete the "drive" property ahead of calling object_unparent() but I didn't figure out how to do so and it's maybe not possible.

So either drain_call_rcu or adding call_rcu(xen_block_drive_destroy) seems to be the way, but since xen_block_drive_destroy uses qmp_blockdev_del, it seems better to drain_call_rcu.

Cheers,

Signature · 1 line
Anthony PERARD
#3 lore raw
Quoted 10 lines · Anthony PERARD
Quoted 2 lines

If nothing else works then I guess it's okay, but why can't you do the xen_block_drive_destroy from e.g. an unrealize callback?

I'm not sure if that's possible.

xen_block_device_create/xen_block_device_destroy() is supposed to be
equivalent to do those qmp commands:
blockdev-add node-name=xvdz-qcow2 driver=qcow2 file={"driver":"file","filename":"disk.qcow2","locking":"off"} device_add id=xvdz driver=xen-disk vdev=xvdz drive=xvdz-qcow2

Quoted 4 lines

But I tried to add a call xen_block_drive_destroy from xen_block_unrealize, but that still is called too early, it's called before object_property_del_all() which would delete "drive" and call release_drive() which would free the node.

Can you use blockdev_mark_auto_del? Then you don't have to call xen_block_drive_destroy at all.

Paolo

Quoted 12 lines

So, no, I don't think we can use an unrealized callback.

I though of trying to delete the "drive" property ahead of calling object_unparent() but I didn't figure out how to do so and it's maybe not possible.

So either drain_call_rcu or adding call_rcu(xen_block_drive_destroy) seems to be the way, but since xen_block_drive_destroy uses qmp_blockdev_del, it seems better to drain_call_rcu.

Cheers,

#4 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Anthony PERARD
Quoted 2 lines

If nothing else works then I guess it's okay, but why can't you do the xen_block_drive_destroy from e.g. an unrealize callback?

I'm not sure if that's possible.

xen_block_device_create/xen_block_device_destroy() is supposed to be
equivalent to do those qmp commands:
blockdev-add node-name=xvdz-qcow2 driver=qcow2 file={"driver":"file","filename":"disk.qcow2","locking":"off"} device_add id=xvdz driver=xen-disk vdev=xvdz drive=xvdz-qcow2

Quoted 7 lines
Quoted 4 lines

But I tried to add a call xen_block_drive_destroy from xen_block_unrealize, but that still is called too early, it's called before object_property_del_all() which would delete "drive" and call release_drive() which would free the node.

Can you use blockdev_mark_auto_del? Then you don't have to call xen_block_drive_destroy at all.

There is no legacy_dinfo, so blockdev_mark_auto_del doesn't work.

Signature · 1 line
Anthony PERARD
#5 lore raw
Quoted 17 lines · Anthony PERARD
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · Anthony PERARD
Quoted 2 lines

If nothing else works then I guess it's okay, but why can't you do the xen_block_drive_destroy from e.g. an unrealize callback?

I'm not sure if that's possible.

xen_block_device_create/xen_block_device_destroy() is supposed to be
equivalent to do those qmp commands:
blockdev-add node-name=xvdz-qcow2 driver=qcow2 file={"driver":"file","filename":"disk.qcow2","locking":"off"} device_add id=xvdz driver=xen-disk vdev=xvdz drive=xvdz-qcow2

But I tried to add a call xen_block_drive_destroy from xen_block_unrealize, but that still is called too early, it's called before object_property_del_all() which would delete "drive" and call release_drive() which would free the node.

Quoted 4 lines
Quoted 2 lines

Can you use blockdev_mark_auto_del? Then you don't have to call xen_block_drive_destroy at all.

There is no legacy_dinfo, so blockdev_mark_auto_del doesn't work.

Then I guess it's okay. Perhaps you can rename the function to xen_block_blockdev_destroy so that it's clear it's a blockdev and no drive. Thanks,

Paolo

#6 lore raw
In reply to Anthony PERARD via

Hi Paul, Stefano,

Could one of you could give a Ack to this patch?

Thanks,

Quoted 48 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Whenever a Xen block device is detach via xenstore, the image associated with it remained open by the backend QEMU and an error is logged:
qemu-system-i386: failed to destroy drive: Node xvdz-qcow2 is in use

This happened since object_unparent() doesn't immediately frees the object and thus keep a reference to the node we are trying to free. The reference is hold by the "drive" property and the call xen_block_drive_destroy() fails.

In order to fix that, we call drain_call_rcu() to run the callback setup by bus_remove_child() via object_unparent().

Fixes 2d24a6466154 ("device-core: use RCU for list of children of a bus")

---
CCing people whom introduced/reviewed the change to use RCU to give them a chance to say if the change is fine.

---
 hw/block/xen-block.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/block/xen-block.c +9 -0
index a3b69e27096f..fe5f828e2d25 100644
@@ -972,6 +972,15 @@ static void xen_block_device_destroy(XenBackendInstance *backend,
 
     object_unparent(OBJECT(xendev));
 
+    /*
+     * Drall all pending RCU callbacks as object_unparent() frees `xendev'
+     * in a RCU callback.
+     * And due to the property "drive" still existing in `xendev', we
+     * cann't destroy the XenBlockDrive associated with `xendev' with
+     * xen_block_drive_destroy() below.
+     */
+    drain_call_rcu();
+
     if (iothread) {
         xen_block_iothread_destroy(iothread, errp);
         if (*errp) {
Signature · 1 line
Anthony PERARD
Signature · 1 line
Anthony PERARD
Reviewed-by on v1 1/1
#7 lore raw
In reply to Anthony PERARD via
Quoted 30 lines · Anthony PERARD

From: Anthony PERARD <[email protected]>

Whenever a Xen block device is detach via xenstore, the image associated with it remained open by the backend QEMU and an error is logged:
qemu-system-i386: failed to destroy drive: Node xvdz-qcow2 is in use

This happened since object_unparent() doesn't immediately frees the object and thus keep a reference to the node we are trying to free. The reference is hold by the "drive" property and the call xen_block_drive_destroy() fails.

In order to fix that, we call drain_call_rcu() to run the callback setup by bus_remove_child() via object_unparent().

Fixes 2d24a6466154 ("device-core: use RCU for list of children of a bus")

---
CCing people whom introduced/reviewed the change to use RCU to give them a chance to say if the change is fine.

---
  hw/block/xen-block.c | 9 +++++++++
  1 file changed, 9 insertions(+)
hw/block/xen-block.c +0 -0
index a3b69e27096f..fe5f828e2d25 100644

hw/block/xen-block.c static void xen_block_device_destroy(XenBackendInstance *backend, · line 976

Quoted 5 lines
hw/block/xen-block.c +2 -0
@@ -972,6 +972,15 @@ static void xen_block_device_destroy(XenBackendInstance *backend,
 
      object_unparent(OBJECT(xendev));
 
+    /*
+     * Drall all pending RCU callbacks as object_unparent() frees `xendev'

s/Drall/Drain ?

hw/block/xen-block.c static void xen_block_device_destroy(XenBackendInstance *backend, · line 979

Quoted 3 lines
hw/block/xen-block.c +3 -0
+     * in a RCU callback.
+     * And due to the property "drive" still existing in `xendev', we
+     * cann't destroy the XenBlockDrive associated with `xendev' with

s/cann't/can't

With those fixed...

Quoted 8 lines
hw/block/xen-block.c +4 -0
+     * xen_block_drive_destroy() below.
+     */
+    drain_call_rcu();
+
      if (iothread) {
          xen_block_iothread_destroy(iothread, errp);
          if (*errp) {