* [PATCH 0/3] Cleanup CoQueue restart functions
@ 2022-04-27 13:08 Paolo Bonzini
2022-04-27 13:08 ` [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next Paolo Bonzini
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Paolo Bonzini @ 2022-04-27 13:08 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, stefanha
Sending this out now that NBD's incorrect uses have been fixed.
There is no equivalent of qemu_co_queue_enter_next that restarts all
coroutines but that is incorrect because qemu_co_queue_restart_all should
really be a coroutine_fn. The NBD uses have been fixed by removing the
calls to qemu_co_queue_restart_all; graphic_hw_update_done works just
because it uses the BQL; but the new rwlock for the BlockDriverState
graph will need it.
The series introduces the new function, removes some duplicated code
around it, and marks qemu_co_queue_next and qemu_co_queue_restart_all
as coroutine_fn.
Paolo
Paolo Bonzini (3):
coroutine-lock: qemu_co_queue_next is a coroutine-only
qemu_co_enter_next
coroutine-lock: introduce qemu_co_queue_enter_all
coroutine-lock: qemu_co_queue_restart_all is a coroutine-only
qemu_co_enter_all
block/io.c | 2 +-
include/qemu/coroutine.h | 27 +++++++++++++++++-----
ui/console.c | 2 +-
util/qemu-coroutine-lock.c | 47 +++++++++++++++-----------------------
4 files changed, 42 insertions(+), 36 deletions(-)
--
2.35.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next
2022-04-27 13:08 [PATCH 0/3] Cleanup CoQueue restart functions Paolo Bonzini
@ 2022-04-27 13:08 ` Paolo Bonzini
2022-04-27 14:07 ` Eric Blake
2022-04-27 13:08 ` [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all Paolo Bonzini
2022-04-27 13:08 ` [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all Paolo Bonzini
2 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2022-04-27 13:08 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, stefanha
qemu_co_queue_next is basically the same as qemu_co_enter_next but
without a QemuLockable argument. That's perfectly fine, but only
as long as the function is marked coroutine_fn. If used outside
coroutine context, qemu_co_queue_wait will attempt to take the lock
and that is just broken: if you are calling qemu_co_queue_next outside
coroutine context, the lock is going to be a QemuMutex which cannot be
taken twice by the same thread.
The patch adds the marker and reimplements qemu_co_queue_next in terms of
qemu_co_enter_next_impl, to remove duplicated code and to clarify that the
latter also works in coroutine context.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qemu/coroutine.h | 7 ++++---
util/qemu-coroutine-lock.c | 21 +++++++--------------
2 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index 284571badb..c23d41e1ff 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -208,11 +208,12 @@ void qemu_co_queue_init(CoQueue *queue);
void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock);
/**
- * Removes the next coroutine from the CoQueue, and wake it up.
+ * Removes the next coroutine from the CoQueue, and queue it to run after
+ * the currently-running coroutine yields.
* Returns true if a coroutine was removed, false if the queue is empty.
- * OK to run from coroutine and non-coroutine context.
+ * Used from coroutine context, use qemu_co_enter_next outside.
*/
-bool qemu_co_queue_next(CoQueue *queue);
+bool coroutine_fn qemu_co_queue_next(CoQueue *queue);
/**
* Empties the CoQueue; all coroutines are woken up.
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
index 2669403839..5705cfea2e 100644
--- a/util/qemu-coroutine-lock.c
+++ b/util/qemu-coroutine-lock.c
@@ -67,7 +67,7 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock)
}
}
-static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
+void qemu_co_queue_restart_all(CoQueue *queue)
{
Coroutine *next;
@@ -78,23 +78,10 @@ static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
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);
-}
-
-void qemu_co_queue_restart_all(CoQueue *queue)
-{
- qemu_co_queue_do_restart(queue, false);
-}
-
bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
{
Coroutine *next;
@@ -115,6 +102,12 @@ bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
return true;
}
+bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
+{
+ /* No unlock/lock needed in coroutine context. */
+ return qemu_co_enter_next_impl(queue, NULL);
+}
+
bool qemu_co_queue_empty(CoQueue *queue)
{
return QSIMPLEQ_FIRST(&queue->entries) == NULL;
--
2.35.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all
2022-04-27 13:08 [PATCH 0/3] Cleanup CoQueue restart functions Paolo Bonzini
2022-04-27 13:08 ` [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next Paolo Bonzini
@ 2022-04-27 13:08 ` Paolo Bonzini
2022-04-27 14:12 ` Eric Blake
2022-04-27 13:08 ` [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all Paolo Bonzini
2 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2022-04-27 13:08 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, stefanha
Because qemu_co_queue_restart_all does not release the lock, it should
be used only in coroutine context. Introduce a new function that,
like qemu_co_enter_next, does release the lock, and use it whenever
qemu_co_queue_restart_all was used outside coroutine context.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qemu/coroutine.h | 13 +++++++++++++
ui/console.c | 2 +-
util/qemu-coroutine-lock.c | 7 +++++++
3 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index c23d41e1ff..e5954635f6 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -234,6 +234,19 @@ void qemu_co_queue_restart_all(CoQueue *queue);
qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock))
bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock);
+/**
+ * Empties the CoQueue, waking the waiting coroutine one at a time. Unlike
+ * qemu_co_queue_all, this function releases the lock during aio_co_wake
+ * because it is meant to be used outside coroutine context; in that case, the
+ * coroutine is entered immediately, before qemu_co_enter_all returns.
+ *
+ * If used in coroutine context, qemu_co_enter_all is equivalent to
+ * qemu_co_queue_all.
+ */
+#define qemu_co_enter_all(queue, lock) \
+ qemu_co_enter_all_impl(queue, QEMU_MAKE_LOCKABLE(lock))
+void qemu_co_enter_all_impl(CoQueue *queue, QemuLockable *lock);
+
/**
* Checks if the CoQueue is empty.
*/
diff --git a/ui/console.c b/ui/console.c
index 1752f2ec88..afe3159394 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -218,7 +218,7 @@ static void gui_setup_refresh(DisplayState *ds)
void graphic_hw_update_done(QemuConsole *con)
{
if (con) {
- qemu_co_queue_restart_all(&con->dump_queue);
+ qemu_co_enter_all(&con->dump_queue, NULL);
}
}
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
index 5705cfea2e..5b0342faed 100644
--- a/util/qemu-coroutine-lock.c
+++ b/util/qemu-coroutine-lock.c
@@ -108,6 +108,13 @@ bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
return qemu_co_enter_next_impl(queue, NULL);
}
+void qemu_co_enter_all_impl(CoQueue *queue, QemuLockable *lock)
+{
+ while (qemu_co_enter_next_impl(queue, lock)) {
+ /* just loop */
+ }
+}
+
bool qemu_co_queue_empty(CoQueue *queue)
{
return QSIMPLEQ_FIRST(&queue->entries) == NULL;
--
2.35.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all
2022-04-27 13:08 [PATCH 0/3] Cleanup CoQueue restart functions Paolo Bonzini
2022-04-27 13:08 ` [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next Paolo Bonzini
2022-04-27 13:08 ` [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all Paolo Bonzini
@ 2022-04-27 13:08 ` Paolo Bonzini
2022-04-27 14:21 ` Eric Blake
2 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2022-04-27 13:08 UTC (permalink / raw)
To: qemu-devel; +Cc: qemu-block, stefanha
qemu_co_queue_restart_all is basically the same as qemu_co_enter_all
but without a QemuLockable argument. That's perfectly fine, but only as
long as the function is marked coroutine_fn. If used outside coroutine
context, qemu_co_queue_wait will attempt to take the lock and that
is just broken: if you are calling qemu_co_queue_restart_all outside
coroutine context, the lock is going to be a QemuMutex which cannot be
taken twice by the same thread.
The patch adds the marker to qemu_co_queue_restart_all and to its sole
non-coroutine_fn caller; it then reimplements the function in terms of
qemu_co_enter_all_impl, to remove duplicated code and to clarify that the
latter also works in coroutine context.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
block/io.c | 2 +-
include/qemu/coroutine.h | 7 ++++---
util/qemu-coroutine-lock.c | 21 ++++++---------------
3 files changed, 11 insertions(+), 19 deletions(-)
diff --git a/block/io.c b/block/io.c
index 9769ec53b0..789e6373d5 100644
--- a/block/io.c
+++ b/block/io.c
@@ -751,7 +751,7 @@ void bdrv_drain_all(void)
*
* This function should be called when a tracked request is completing.
*/
-static void tracked_request_end(BdrvTrackedRequest *req)
+static void coroutine_fn tracked_request_end(BdrvTrackedRequest *req)
{
if (req->serialising) {
qatomic_dec(&req->bs->serialising_in_flight);
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index e5954635f6..43df7a7e66 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -216,10 +216,11 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock);
bool coroutine_fn qemu_co_queue_next(CoQueue *queue);
/**
- * Empties the CoQueue; all coroutines are woken up.
- * OK to run from coroutine and non-coroutine context.
+ * Empties the CoQueue and queues the coroutine to run after
+ * the currently-running coroutine yields.
+ * Used from coroutine context, use qemu_co_enter_all outside.
*/
-void qemu_co_queue_restart_all(CoQueue *queue);
+void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue);
/**
* Removes the next coroutine from the CoQueue, and wake it up. Unlike
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
index 5b0342faed..9ad24ab1af 100644
--- a/util/qemu-coroutine-lock.c
+++ b/util/qemu-coroutine-lock.c
@@ -67,21 +67,6 @@ void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock)
}
}
-void qemu_co_queue_restart_all(CoQueue *queue)
-{
- Coroutine *next;
-
- if (QSIMPLEQ_EMPTY(&queue->entries)) {
- return false;
- }
-
- while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
- QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
- aio_co_wake(next);
- }
- return true;
-}
-
bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
{
Coroutine *next;
@@ -115,6 +100,12 @@ void qemu_co_enter_all_impl(CoQueue *queue, QemuLockable *lock)
}
}
+void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
+{
+ /* No unlock/lock needed in coroutine context. */
+ qemu_co_enter_all_impl(queue, NULL);
+}
+
bool qemu_co_queue_empty(CoQueue *queue)
{
return QSIMPLEQ_FIRST(&queue->entries) == NULL;
--
2.35.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next
2022-04-27 13:08 ` [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next Paolo Bonzini
@ 2022-04-27 14:07 ` Eric Blake
0 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2022-04-27 14:07 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: stefanha, qemu-devel, qemu-block
On Wed, Apr 27, 2022 at 03:08:28PM +0200, Paolo Bonzini wrote:
> qemu_co_queue_next is basically the same as qemu_co_enter_next but
> without a QemuLockable argument. That's perfectly fine, but only
> as long as the function is marked coroutine_fn. If used outside
> coroutine context, qemu_co_queue_wait will attempt to take the lock
> and that is just broken: if you are calling qemu_co_queue_next outside
> coroutine context, the lock is going to be a QemuMutex which cannot be
> taken twice by the same thread.
>
> The patch adds the marker and reimplements qemu_co_queue_next in terms of
> qemu_co_enter_next_impl, to remove duplicated code and to clarify that the
> latter also works in coroutine context.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> include/qemu/coroutine.h | 7 ++++---
> util/qemu-coroutine-lock.c | 21 +++++++--------------
> 2 files changed, 11 insertions(+), 17 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all
2022-04-27 13:08 ` [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all Paolo Bonzini
@ 2022-04-27 14:12 ` Eric Blake
0 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2022-04-27 14:12 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: stefanha, qemu-devel, qemu-block
On Wed, Apr 27, 2022 at 03:08:29PM +0200, Paolo Bonzini wrote:
> Because qemu_co_queue_restart_all does not release the lock, it should
> be used only in coroutine context. Introduce a new function that,
> like qemu_co_enter_next, does release the lock, and use it whenever
> qemu_co_queue_restart_all was used outside coroutine context.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> include/qemu/coroutine.h | 13 +++++++++++++
> ui/console.c | 2 +-
> util/qemu-coroutine-lock.c | 7 +++++++
> 3 files changed, 21 insertions(+), 1 deletion(-)
>
> diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
> index c23d41e1ff..e5954635f6 100644
> --- a/include/qemu/coroutine.h
> +++ b/include/qemu/coroutine.h
> @@ -234,6 +234,19 @@ void qemu_co_queue_restart_all(CoQueue *queue);
> qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock))
> bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock);
>
> +/**
> + * Empties the CoQueue, waking the waiting coroutine one at a time. Unlike
maybe s/coroutine/coroutine(s)/
> + * qemu_co_queue_all, this function releases the lock during aio_co_wake
> + * because it is meant to be used outside coroutine context; in that case, the
> + * coroutine is entered immediately, before qemu_co_enter_all returns.
> + *
> + * If used in coroutine context, qemu_co_enter_all is equivalent to
> + * qemu_co_queue_all.
> + */
> +#define qemu_co_enter_all(queue, lock) \
> + qemu_co_enter_all_impl(queue, QEMU_MAKE_LOCKABLE(lock))
> +void qemu_co_enter_all_impl(CoQueue *queue, QemuLockable *lock);
> +
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all
2022-04-27 13:08 ` [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all Paolo Bonzini
@ 2022-04-27 14:21 ` Eric Blake
0 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2022-04-27 14:21 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: stefanha, qemu-devel, qemu-block
On Wed, Apr 27, 2022 at 03:08:30PM +0200, Paolo Bonzini wrote:
> qemu_co_queue_restart_all is basically the same as qemu_co_enter_all
> but without a QemuLockable argument. That's perfectly fine, but only as
> long as the function is marked coroutine_fn. If used outside coroutine
> context, qemu_co_queue_wait will attempt to take the lock and that
> is just broken: if you are calling qemu_co_queue_restart_all outside
> coroutine context, the lock is going to be a QemuMutex which cannot be
> taken twice by the same thread.
>
> The patch adds the marker to qemu_co_queue_restart_all and to its sole
> non-coroutine_fn caller; it then reimplements the function in terms of
> qemu_co_enter_all_impl, to remove duplicated code and to clarify that the
> latter also works in coroutine context.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> block/io.c | 2 +-
> include/qemu/coroutine.h | 7 ++++---
> util/qemu-coroutine-lock.c | 21 ++++++---------------
> 3 files changed, 11 insertions(+), 19 deletions(-)
>
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-04-27 14:22 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-27 13:08 [PATCH 0/3] Cleanup CoQueue restart functions Paolo Bonzini
2022-04-27 13:08 ` [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next Paolo Bonzini
2022-04-27 14:07 ` Eric Blake
2022-04-27 13:08 ` [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all Paolo Bonzini
2022-04-27 14:12 ` Eric Blake
2022-04-27 13:08 ` [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all Paolo Bonzini
2022-04-27 14:21 ` Eric Blake
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).