Skip to contentSkip to conversation

virtio-blk: schedule virtio_notify_config to run on main context

PATCH Applied Sergio Lopez 3 participants 3 messages Sep 16, 2019 – Sep 20, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 virtio-blk: schedule virtio_notify_config to run on main context

virtio_notify_config() needs to acquire the global mutex, which isn't allowed from an iothread, and may lead to a deadlock like this:

- main thead
* Has acquired: qemu_global_mutex.
* Is trying the acquire: iothread AioContext lock via AIO_WAIT_WHILE (after aio_poll).

- iothread
* Has acquired: AioContext lock.
* Is trying to acquire: qemu_global_mutex (via

    virtio_notify_config->prepare_mmio_access).

If virtio_blk_resize() is called from an iothread, schedule virtio_notify_config() to be run in the main context BH.

---
Changelog

v3:
- Unconditionally schedule the work to be done in the main context BH (thanks John Snow and Kevin Wolf).

v2:
- Use aio_bh_schedule_oneshot instead of scheduling a coroutine (thanks Kevin Wolf).
- Switch from RFC to v2 patch.

---
 hw/block/virtio-blk.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
hw/block/virtio-blk.c +16 -1
index 18851601cb..0163285f6f 100644
@@ -16,6 +16,7 @@
 #include "qemu/iov.h"
 #include "qemu/module.h"
 #include "qemu/error-report.h"
+#include "qemu/main-loop.h"
 #include "trace.h"
 #include "hw/block/block.h"
 #include "hw/qdev-properties.h"
@@ -1086,11 +1087,25 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void virtio_resize_cb(void *opaque)
+{
+    VirtIODevice *vdev = opaque;
+
+    assert(qemu_get_current_aio_context() == qemu_get_aio_context());
+    virtio_notify_config(vdev);
+}
+
 static void virtio_blk_resize(void *opaque)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
 
-    virtio_notify_config(vdev);
+    /*
+     * virtio_notify_config() needs to acquire the global mutex,
+     * so it can't be called from an iothread. Instead, schedule
+     * it to be run in the main context BH.
+     */
+    aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                            virtio_resize_cb, vdev);
 }
 
 static const BlockDevOps virtio_block_ops = {
Signature · 1 line
2.21.0
Reviewed-by on v3 1/1
#1 lore raw

Am 16.09.2019 um 13:24 hat Sergio Lopez geschrieben:

Quoted 58 lines

virtio_notify_config() needs to acquire the global mutex, which isn't allowed from an iothread, and may lead to a deadlock like this:

- main thead
* Has acquired: qemu_global_mutex.
* Is trying the acquire: iothread AioContext lock via AIO_WAIT_WHILE (after aio_poll).

- iothread
* Has acquired: AioContext lock.
* Is trying to acquire: qemu_global_mutex (via

    virtio_notify_config->prepare_mmio_access).

If virtio_blk_resize() is called from an iothread, schedule virtio_notify_config() to be run in the main context BH.

---
Changelog

v3:
- Unconditionally schedule the work to be done in the main context BH (thanks John Snow and Kevin Wolf).

v2:
- Use aio_bh_schedule_oneshot instead of scheduling a coroutine (thanks Kevin Wolf).
- Switch from RFC to v2 patch.

---
 hw/block/virtio-blk.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
hw/block/virtio-blk.c +9 -0
index 18851601cb..0163285f6f 100644
@@ -16,6 +16,7 @@
 #include "qemu/iov.h"
 #include "qemu/module.h"
 #include "qemu/error-report.h"
+#include "qemu/main-loop.h"
 #include "trace.h"
 #include "hw/block/block.h"
 #include "hw/qdev-properties.h"
@@ -1086,11 +1087,25 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void virtio_resize_cb(void *opaque)
+{
+    VirtIODevice *vdev = opaque;
+
+    assert(qemu_get_current_aio_context() == qemu_get_aio_context());
+    virtio_notify_config(vdev);
+}
+
 static void virtio_blk_resize(void *opaque)
 {

hw/block/virtio-blk.c static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1108

Quoted 10 lines
hw/block/virtio-blk.c +7 -1
     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
 
-    virtio_notify_config(vdev);
+    /*
+     * virtio_notify_config() needs to acquire the global mutex,
+     * so it can't be called from an iothread. Instead, schedule
+     * it to be run in the main context BH.
+     */
+    aio_bh_schedule_oneshot(qemu_get_aio_context(),
+                            virtio_resize_cb, vdev);

This fits on a single line. With this fixed:

#2 lore raw
In reply to Sergio Lopez
Quoted 24 lines · Sergio Lopez

virtio_notify_config() needs to acquire the global mutex, which isn't allowed from an iothread, and may lead to a deadlock like this:

- main thead
* Has acquired: qemu_global_mutex.
* Is trying the acquire: iothread AioContext lock via AIO_WAIT_WHILE (after aio_poll).

- iothread
* Has acquired: AioContext lock.
* Is trying to acquire: qemu_global_mutex (via

    virtio_notify_config->prepare_mmio_access).

If virtio_blk_resize() is called from an iothread, schedule virtio_notify_config() to be run in the main context BH.

---
Changelog

v3:
- Unconditionally schedule the work to be done in the main context BH (thanks John Snow and Kevin Wolf).

Quoted 7 lines

v2:
- Use aio_bh_schedule_oneshot instead of scheduling a coroutine (thanks Kevin Wolf).
- Switch from RFC to v2 patch.

---
 hw/block/virtio-blk.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan