coroutine: Make qemu_coroutine_self() return NULL if not in coroutine
PATCH 1/1 coroutine: Make qemu_coroutine_self() return NULL if not in coroutine
qemu_coroutine_self() is used in several places outside of coroutine context (mostly in qcow2 tracing calls).
Ensure qemu_coroutine_self() works properly when not called from coroutine context, returning NULL in that case, and remove its coroutine_fn annotation.
--- include/qemu/coroutine.h | 5 +++-- util/coroutine-sigaltstack.c | 4 ++-- util/coroutine-ucontext.c | 18 +++++++++--------- util/coroutine-win32.c | 9 +-------- 4 files changed, 15 insertions(+), 21 deletions(-)
include/qemu/coroutine.h +3 -2
@@ -95,9 +95,10 @@ void coroutine_fn qemu_coroutine_yield(void);AioContext *qemu_coroutine_get_aio_context (Coroutine *co);/** -* Get the currently executing coroutine +* Get the currently executing coroutine, or NULL if not called from coroutine +* context */ -Coroutine *coroutine_fn qemu_coroutine_self(void); +Coroutine *qemu_coroutine_self (void );/** * Return whether or not currently inside a coroutine
util/coroutine-sigaltstack.c +2 -2
@@ -289,9 +289,9 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,Coroutine *qemu_coroutine_self (void ) { -CoroutineThreadState *s =coroutine_get_thread_state (); +CoroutineThreadState *s =pthread_getspecific (thread_state_key); -return s->current; +return s && s->current->caller ? s->current :NULL ; }bool qemu_in_coroutine (void )
util/coroutine-ucontext.c +9 -9
@@ -320,18 +320,18 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,Coroutine *qemu_coroutine_self (void ) {Coroutine *self =get_current (); -CoroutineUContext *leaderp =get_ptr_leader (); -if (!self) { - self = &leaderp->base; -set_current (self); - } +if (self && self->caller) {#ifdef CONFIG_TSAN -if (!leaderp->tsan_co_fiber) { - leaderp->tsan_co_fiber =__tsan_get_current_fiber (); - } +CoroutineUContext *leaderp =get_ptr_leader (); +if (!leaderp->tsan_co_fiber) { + leaderp->tsan_co_fiber =__tsan_get_current_fiber (); + }#endif -return self; +return self; + }else { +return NULL ; + } }bool qemu_in_coroutine (void )
util/coroutine-win32.c +1 -8
@@ -91,14 +91,7 @@ Coroutine *qemu_coroutine_self(void) {Coroutine *current =get_current (); -if (!current) { -CoroutineWin32 *leader =get_ptr_leader (); - - current = &leader->base; -set_current (current); - leader->fiber =ConvertThreadToFiber (NULL ); - } -return current; +return current && current->caller ? current :NULL ; }bool qemu_in_coroutine (void )
Signature · 1 line
2.37.3
Am 05.10.2022 um 16:20 hat Alberto Faria geschrieben:
Quoted 8 lines
qemu_coroutine_self()is used in several places outside of coroutine context (mostly in qcow2 tracing calls).Ensure
qemu_coroutine_self()works properly when not called from coroutine context, returning NULL in that case, and remove itscoroutine_fnannotation.Signed-off-by Alberto Faria <[email protected]>
The coroutine_fn annotation for qemu_coroutine_self() is wrong, but I think it already works outside of coroutine context, and consistently in all three backends, by returning &leader.
Changing that to NULL makes me kind of nervous because the callers might actually access the leader Coroutine object, and after this change they would crash. (And even if they didn't crash, they wouldn't be able to distinguish the leader coroutines of different threads any more.)
Do we have an actual reason to make this chance? That is, do we have any case that was broken before?
Kevin
Quoted 11 lines · Kevin Wolf
The
coroutine_fnannotation forqemu_coroutine_self()is wrong, but I think it already works outside of coroutine context, and consistently in all three backends, by returning &leader.Changing that to NULL makes me kind of nervous because the callers might actually access the leader Coroutine object, and after this change they would crash. (And even if they didn't crash, they wouldn't be able to distinguish the leader coroutines of different threads any more.)
Do we have an actual reason to make this chance? That is, do we have any case that was broken before?
No, I just wasn't sure if the current implementations would return a meaningful value when called from outside coroutine context, but it seems they do. I'll send a patch only dropping the coroutine_fn annotation.
Thanks,
Alberto
Quoted 8 lines · Kevin Wolf
Am 05.10.2022 um 16:20 hat Alberto Faria geschrieben:
Quoted 7 lines
qemu_coroutine_self()is used in several places outside of coroutine context (mostly in qcow2 tracing calls).Ensure
qemu_coroutine_self()works properly when not called from coroutine context, returning NULL in that case, and remove itscoroutine_fnannotation.
Quoted 5 lines
Quoted 1 line
Signed-off-by Alberto Faria <[email protected]>The
coroutine_fnannotation forqemu_coroutine_self()is wrong, but I think it already works outside of coroutine context, and consistently in all three backends, by returning &leader.
Yes, I remember implementing it specifically so that NULL is never returned.
The coroutine_fn annotation should be removed.
Stefan