Cleanup CoQueue restart functions
cover Cleanup CoQueue restart functions
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(-)
Signature · 1 line
2.35.1
PATCH 1/3 coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next
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.
--- include/qemu/coroutine.h | 7 ++++--- util/qemu-coroutine-lock.c | 21 +++++++-------------- 2 files changed, 11 insertions(+), 17 deletions(-)
include/qemu/coroutine.h +4 -3
@@ -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.
util/qemu-coroutine-lock.c +7 -14
@@ -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 ;
Signature · 1 line
2.35.1
Re: [PATCH 1/3] coroutine-lock: qemu_co_queue_next is a coroutine-only qemu_co_enter_next
Quoted 12 lines · Paolo Bonzini
qemu_co_queue_nextis basically the same asqemu_co_enter_nextbut without a QemuLockable argument. That's perfectly fine, but only as long as the function is markedcoroutine_fn. If used outside coroutine context,qemu_co_queue_waitwill attempt to take the lock and that is just broken: if you are callingqemu_co_queue_nextoutside 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_nextin terms ofqemu_co_enter_next_impl, to remove duplicated code and to clarify that the latter also works in coroutine context.
Quoted 5 lines
Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/coroutine.h | 7 ++++--- util/qemu-coroutine-lock.c | 21 +++++++-------------- 2 files changed, 11 insertions(+), 17 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 2/3 coroutine-lock: introduce qemu_co_queue_enter_all
In reply to Paolo BonziniBecause 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.
--- include/qemu/coroutine.h | 13 +++++++++++++ ui/console.c | 2 +- util/qemu-coroutine-lock.c | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-)
include/qemu/coroutine.h +13 -0
@@ -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. */
ui/console.c +1 -1
@@ -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 ); } }
util/qemu-coroutine-lock.c +7 -0
@@ -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 ;
Signature · 1 line
2.35.1
Re: [PATCH 2/3] coroutine-lock: introduce qemu_co_queue_enter_all
Quoted 17 lines · Paolo Bonzini
Because
qemu_co_queue_restart_alldoes not release the lock, it should be used only in coroutine context. Introduce a new function that, likeqemu_co_enter_next, does release the lock, and use it wheneverqemu_co_queue_restart_allwas used outside coroutine context.Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/coroutine.h | 13 +++++++++++++ ui/console.c | 2 +- util/qemu-coroutine-lock.c | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-)include/qemu/coroutine.h +0
-0
include/qemu/coroutine.h void qemu_co_queue_restart_all(CoQueue *queue); · line 238
Quoted 5 lines
include/qemu/coroutine.h +2
-0@@ -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)/
include/qemu/coroutine.h void qemu_co_queue_restart_all(CoQueue *queue); · line 249
Quoted 11 lines
include/qemu/coroutine.h +11
-0+* 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); +
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH 3/3 coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all
In reply to Paolo Bonziniqemu_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.
--- block/io.c | 2 +- include/qemu/coroutine.h | 7 ++++--- util/qemu-coroutine-lock.c | 21 ++++++--------------- 3 files changed, 11 insertions(+), 19 deletions(-)
block/io.c +1 -1
@@ -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);
include/qemu/coroutine.h +4 -3
@@ -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
util/qemu-coroutine-lock.c +6 -15
@@ -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 ;
Signature · 1 line
2.35.1
Re: [PATCH 3/3] coroutine-lock: qemu_co_queue_restart_all is a coroutine-only qemu_co_enter_all
Quoted 13 lines · Paolo Bonzini
qemu_co_queue_restart_allis basically the same asqemu_co_enter_allbut without a QemuLockable argument. That's perfectly fine, but only as long as the function is markedcoroutine_fn. If used outside coroutine context,qemu_co_queue_waitwill attempt to take the lock and that is just broken: if you are callingqemu_co_queue_restart_alloutside 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_alland to its sole non-coroutine_fn caller; it then reimplements the function in terms ofqemu_co_enter_all_impl, to remove duplicated code and to clarify that the latter also works in coroutine context.
Quoted 7 lines
Signed-off-by Paolo Bonzini <[email protected]>--- block/io.c | 2 +- include/qemu/coroutine.h | 7 ++++--- util/qemu-coroutine-lock.c | 21 ++++++--------------- 3 files changed, 11 insertions(+), 19 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org