* [Qemu-devel] [PATCH v2] virtio-blk: schedule virtio_notify_config to run on main context
@ 2019-09-13 10:56 Sergio Lopez
2019-09-13 20:29 ` [Qemu-devel] [Qemu-block] " John Snow
2019-09-16 8:09 ` [Qemu-devel] " Kevin Wolf
0 siblings, 2 replies; 3+ messages in thread
From: Sergio Lopez @ 2019-09-13 10:56 UTC (permalink / raw)
To: qemu-block; +Cc: kwolf, Sergio Lopez, mst, qemu-devel, mreitz, stefanha
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.
Signed-off-by: Sergio Lopez <slp@redhat.com>
---
Changelog
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 | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
index 18851601cb..669dc60f5b 100644
--- a/hw/block/virtio-blk.c
+++ b/hw/block/virtio-blk.c
@@ -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,29 @@ 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);
+ if (qemu_get_current_aio_context() != qemu_get_aio_context()) {
+ /*
+ * 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);
+ } else {
+ virtio_notify_config(vdev);
+ }
}
static const BlockDevOps virtio_block_ops = {
--
2.21.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v2] virtio-blk: schedule virtio_notify_config to run on main context
2019-09-13 10:56 [Qemu-devel] [PATCH v2] virtio-blk: schedule virtio_notify_config to run on main context Sergio Lopez
@ 2019-09-13 20:29 ` John Snow
2019-09-16 8:09 ` [Qemu-devel] " Kevin Wolf
1 sibling, 0 replies; 3+ messages in thread
From: John Snow @ 2019-09-13 20:29 UTC (permalink / raw)
To: Sergio Lopez, qemu-block; +Cc: kwolf, mreitz, qemu-devel, stefanha, mst
On 9/13/19 6:56 AM, Sergio Lopez wrote:
> 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.
>
> Signed-off-by: Sergio Lopez <slp@redhat.com>
> ---
> Changelog
>
> 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 | 21 ++++++++++++++++++++-
> 1 file changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
> index 18851601cb..669dc60f5b 100644
> --- a/hw/block/virtio-blk.c
> +++ b/hw/block/virtio-blk.c
> @@ -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,29 @@ 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);
> + if (qemu_get_current_aio_context() != qemu_get_aio_context()) {
> + /*
> + * 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);
> + } else {
> + virtio_notify_config(vdev);
> + }
> }
>
> static const BlockDevOps virtio_block_ops = {
>
Do we need to bother to check our current context before firing off the
oneshot? It's simpler to just fire off the oneshot. I can't imagine that
resize is a hot path that needs to worry about being that efficient.
Well, I guess it doesn't hurt either.
Reviewed-by: John Snow <jsnow@redhat.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH v2] virtio-blk: schedule virtio_notify_config to run on main context
2019-09-13 10:56 [Qemu-devel] [PATCH v2] virtio-blk: schedule virtio_notify_config to run on main context Sergio Lopez
2019-09-13 20:29 ` [Qemu-devel] [Qemu-block] " John Snow
@ 2019-09-16 8:09 ` Kevin Wolf
1 sibling, 0 replies; 3+ messages in thread
From: Kevin Wolf @ 2019-09-16 8:09 UTC (permalink / raw)
To: Sergio Lopez; +Cc: mreitz, stefanha, qemu-devel, qemu-block, mst
Am 13.09.2019 um 12:56 hat Sergio Lopez geschrieben:
> 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.
>
> Signed-off-by: Sergio Lopez <slp@redhat.com>
> ---
> Changelog
>
> 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 | 21 ++++++++++++++++++++-
> 1 file changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
> index 18851601cb..669dc60f5b 100644
> --- a/hw/block/virtio-blk.c
> +++ b/hw/block/virtio-blk.c
> @@ -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,29 @@ 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);
> + if (qemu_get_current_aio_context() != qemu_get_aio_context()) {
> + /*
> + * 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);
> + } else {
> + virtio_notify_config(vdev);
Let's call virtio_resize_cb() instead to keep both code paths the same.
Otherwise, we might add more code to virtio_resize_cb() later and miss
that it must be duplicated here.
Kevin
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-09-16 8:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-13 10:56 [Qemu-devel] [PATCH v2] virtio-blk: schedule virtio_notify_config to run on main context Sergio Lopez
2019-09-13 20:29 ` [Qemu-devel] [Qemu-block] " John Snow
2019-09-16 8:09 ` [Qemu-devel] " Kevin Wolf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).