qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Fam Zheng <fam@euphon.net>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	Emanuele Giuseppe Esposito <eesposit@redhat.com>,
	qemu-devel@nongnu.org, Hanna Reitz <hreitz@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>, John Snow <jsnow@redhat.com>
Subject: [RFC PATCH v2 2/8] coroutine-lock: release lock when restarting all coroutines
Date: Tue, 26 Apr 2022 04:51:08 -0400	[thread overview]
Message-ID: <20220426085114.199647-3-eesposit@redhat.com> (raw)
In-Reply-To: <20220426085114.199647-1-eesposit@redhat.com>

Current implementation of qemu_co_queue_do_restart
does not releases the lock before calling aio_co_wake.
Most of the time this is fine, but if the coroutine
acquires the lock again then we have a deadlock.

Instead of duplicating code, use qemu_co_enter_next_impl, since
it implements exactly the same functionality that we
want.

Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
 include/qemu/coroutine.h   | 10 ++++++++++
 util/qemu-coroutine-lock.c | 26 ++++++++++----------------
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index c828a95ee0..c49cdc21b4 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -220,6 +220,16 @@ bool qemu_co_queue_next(CoQueue *queue);
  */
 void qemu_co_queue_restart_all(CoQueue *queue);
 
+/**
+ * Empties the CoQueue; all coroutines are woken up.
+ * OK to run from coroutine and non-coroutine context.
+ * Unlocks lock before waking up each coroutine takes it again
+ * when done.
+ */
+#define qemu_co_queue_restart_all_lockable(queue, lock) \
+    qemu_co_queue_restart_all_impl(queue, QEMU_MAKE_LOCKABLE(lock))
+void qemu_co_queue_restart_all_impl(CoQueue *queue, QemuLockable *lock);
+
 /**
  * Removes the next coroutine from the CoQueue, and wake it up.  Unlike
  * qemu_co_queue_next, this function releases the lock during aio_co_wake
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
index 2669403839..17bb0d0c95 100644
--- a/util/qemu-coroutine-lock.c
+++ b/util/qemu-coroutine-lock.c
@@ -67,32 +67,26 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock)
     }
 }
 
-static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
+static void qemu_co_queue_do_restart(CoQueue *queue, QemuLockable *lock)
 {
-    Coroutine *next;
-
-    if (QSIMPLEQ_EMPTY(&queue->entries)) {
-        return false;
+    while (qemu_co_enter_next_impl(queue, lock)) {
+        /* nop */
     }
-
-    while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
-        QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
-        aio_co_wake(next);
-        if (single) {
-            break;
-        }
-    }
-    return true;
 }
 
 bool qemu_co_queue_next(CoQueue *queue)
 {
-    return qemu_co_queue_do_restart(queue, true);
+    return qemu_co_enter_next_impl(queue, NULL);
 }
 
 void qemu_co_queue_restart_all(CoQueue *queue)
 {
-    qemu_co_queue_do_restart(queue, false);
+    qemu_co_queue_do_restart(queue, NULL);
+}
+
+void qemu_co_queue_restart_all_impl(CoQueue *queue, QemuLockable *lock)
+{
+    qemu_co_queue_do_restart(queue, lock);
 }
 
 bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
-- 
2.31.1



  parent reply	other threads:[~2022-04-26  8:54 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26  8:51 [RFC PATCH v2 0/8] Removal of AioContext lock, bs->parents and ->children: new rwlock Emanuele Giuseppe Esposito
2022-04-26  8:51 ` [RFC PATCH v2 1/8] aio_wait_kick: add missing memory barrier Emanuele Giuseppe Esposito
2022-04-28 11:09   ` Stefan Hajnoczi
2022-04-29  8:06     ` Emanuele Giuseppe Esposito
2022-04-30  5:21       ` Stefan Hajnoczi
2022-04-29  8:12   ` Paolo Bonzini
2022-04-26  8:51 ` Emanuele Giuseppe Esposito [this message]
2022-04-26 14:59   ` [RFC PATCH v2 2/8] coroutine-lock: release lock when restarting all coroutines Paolo Bonzini
2022-04-28 11:21   ` Stefan Hajnoczi
2022-04-28 22:14     ` Paolo Bonzini
2022-04-29  9:35       ` Emanuele Giuseppe Esposito
2022-04-26  8:51 ` [RFC PATCH v2 3/8] block: introduce a lock to protect graph operations Emanuele Giuseppe Esposito
2022-04-26 15:00   ` Paolo Bonzini
2022-04-28 13:45   ` Stefan Hajnoczi
2022-04-29  8:37     ` Emanuele Giuseppe Esposito
2022-04-30  5:48       ` Stefan Hajnoczi
2022-05-02  7:54         ` Emanuele Giuseppe Esposito
2022-05-03 10:50           ` Stefan Hajnoczi
2022-04-26  8:51 ` [RFC PATCH v2 4/8] async: register/unregister aiocontext in graph lock list Emanuele Giuseppe Esposito
2022-04-28 13:46   ` Stefan Hajnoczi
2022-04-28 22:19     ` Paolo Bonzini
2022-04-29  8:37       ` Emanuele Giuseppe Esposito
2022-04-26  8:51 ` [RFC PATCH v2 5/8] block.c: wrlock in bdrv_replace_child_noperm Emanuele Giuseppe Esposito
2022-04-26 15:07   ` Paolo Bonzini
2022-04-28 13:55   ` Stefan Hajnoczi
2022-04-29  8:41     ` Emanuele Giuseppe Esposito
2022-04-26  8:51 ` [RFC PATCH v2 6/8] block: assert that graph read and writes are performed correctly Emanuele Giuseppe Esposito
2022-04-28 14:43   ` Stefan Hajnoczi
2022-04-26  8:51 ` [RFC PATCH v2 7/8] graph-lock: implement WITH_GRAPH_RDLOCK_GUARD and GRAPH_RDLOCK_GUARD macros Emanuele Giuseppe Esposito
2022-04-28 15:00   ` Stefan Hajnoczi
2022-04-26  8:51 ` [RFC PATCH v2 8/8] mirror: protect drains in coroutine with rdlock Emanuele Giuseppe Esposito
2022-04-27  6:55 ` [RFC PATCH v2 0/8] Removal of AioContext lock, bs->parents and ->children: new rwlock Emanuele Giuseppe Esposito
2022-04-28 10:45   ` Stefan Hajnoczi
2022-04-28 21:56     ` Emanuele Giuseppe Esposito
2022-04-30  5:17       ` Stefan Hajnoczi
2022-05-02  8:02         ` Emanuele Giuseppe Esposito
2022-05-02 13:15           ` Paolo Bonzini
2022-05-03  8:24           ` Kevin Wolf
2022-05-03 11:04           ` Stefan Hajnoczi
2022-04-28 10:34 ` Stefan Hajnoczi
2022-04-29  8:06   ` Emanuele Giuseppe Esposito
2022-05-04 13:39 ` Stefan Hajnoczi
2022-05-17 10:59   ` Stefan Hajnoczi
2022-05-18 12:28     ` Emanuele Giuseppe Esposito
2022-05-18 12:43       ` Paolo Bonzini
2022-05-18 14:57         ` Stefan Hajnoczi
2022-05-18 16:14         ` Kevin Wolf
2022-05-19 11:27           ` Stefan Hajnoczi
2022-05-19 12:52             ` Kevin Wolf
2022-05-22 15:06           ` Stefan Hajnoczi
2022-05-23  8:48             ` Emanuele Giuseppe Esposito
2022-05-23 13:15               ` Stefan Hajnoczi
2022-05-23 13:54                 ` Emanuele Giuseppe Esposito
2022-05-23 13:02             ` Kevin Wolf
2022-05-23 15:13               ` Stefan Hajnoczi
2022-05-23 16:04                 ` Kevin Wolf
2022-05-23 16:45                   ` Stefan Hajnoczi
2022-05-24  7:55             ` Paolo Bonzini
2022-05-24  8:08               ` Stefan Hajnoczi
2022-05-24  9:17                 ` Paolo Bonzini
2022-05-24 10:20                   ` Stefan Hajnoczi
2022-05-24 17:25                     ` Paolo Bonzini
2022-05-24 10:36         ` Kevin Wolf
2022-05-25  7:41           ` Paolo Bonzini
2022-05-18 14:27       ` Stefan Hajnoczi
2022-05-24 12:10       ` Kevin Wolf
2022-05-25  8:27         ` Emanuele Giuseppe Esposito

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220426085114.199647-3-eesposit@redhat.com \
    --to=eesposit@redhat.com \
    --cc=fam@euphon.net \
    --cc=hreitz@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).