coroutine-ucontext: use QEMU_DEFINE_STATIC_CO_TLS()
Thread-Local Storage variables cannot be used directly from coroutine code because the compiler may optimize TLS variable accesses across qemu_coroutine_yield() calls. When the coroutine is re-entered from another thread the TLS variables from the old thread must no longer be used. Use QEMU_DEFINE_STATIC_CO_TLS() for the current and leader variables. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20220307153853.602859-2-stefanha@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
ecf3200703
commit
34145a307d
@ -25,6 +25,7 @@
|
|||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include <ucontext.h>
|
#include <ucontext.h>
|
||||||
#include "qemu/coroutine_int.h"
|
#include "qemu/coroutine_int.h"
|
||||||
|
#include "qemu/coroutine-tls.h"
|
||||||
|
|
||||||
#ifdef CONFIG_VALGRIND_H
|
#ifdef CONFIG_VALGRIND_H
|
||||||
#include <valgrind/valgrind.h>
|
#include <valgrind/valgrind.h>
|
||||||
@ -66,8 +67,8 @@ typedef struct {
|
|||||||
/**
|
/**
|
||||||
* Per-thread coroutine bookkeeping
|
* Per-thread coroutine bookkeeping
|
||||||
*/
|
*/
|
||||||
static __thread CoroutineUContext leader;
|
QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
|
||||||
static __thread Coroutine *current;
|
QEMU_DEFINE_STATIC_CO_TLS(CoroutineUContext, leader);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* va_args to makecontext() must be type 'int', so passing
|
* va_args to makecontext() must be type 'int', so passing
|
||||||
@ -97,14 +98,15 @@ static inline __attribute__((always_inline))
|
|||||||
void finish_switch_fiber(void *fake_stack_save)
|
void finish_switch_fiber(void *fake_stack_save)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_ASAN
|
#ifdef CONFIG_ASAN
|
||||||
|
CoroutineUContext *leaderp = get_ptr_leader();
|
||||||
const void *bottom_old;
|
const void *bottom_old;
|
||||||
size_t size_old;
|
size_t size_old;
|
||||||
|
|
||||||
__sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
|
__sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
|
||||||
|
|
||||||
if (!leader.stack) {
|
if (!leaderp->stack) {
|
||||||
leader.stack = (void *)bottom_old;
|
leaderp->stack = (void *)bottom_old;
|
||||||
leader.stack_size = size_old;
|
leaderp->stack_size = size_old;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef CONFIG_TSAN
|
#ifdef CONFIG_TSAN
|
||||||
@ -161,8 +163,10 @@ static void coroutine_trampoline(int i0, int i1)
|
|||||||
|
|
||||||
/* Initialize longjmp environment and switch back the caller */
|
/* Initialize longjmp environment and switch back the caller */
|
||||||
if (!sigsetjmp(self->env, 0)) {
|
if (!sigsetjmp(self->env, 0)) {
|
||||||
start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, leader.stack,
|
CoroutineUContext *leaderp = get_ptr_leader();
|
||||||
leader.stack_size);
|
|
||||||
|
start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save,
|
||||||
|
leaderp->stack, leaderp->stack_size);
|
||||||
start_switch_fiber_tsan(&fake_stack_save, self, true); /* true=caller */
|
start_switch_fiber_tsan(&fake_stack_save, self, true); /* true=caller */
|
||||||
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
|
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
|
||||||
}
|
}
|
||||||
@ -297,7 +301,7 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
|
|||||||
int ret;
|
int ret;
|
||||||
void *fake_stack_save = NULL;
|
void *fake_stack_save = NULL;
|
||||||
|
|
||||||
current = to_;
|
set_current(to_);
|
||||||
|
|
||||||
ret = sigsetjmp(from->env, 0);
|
ret = sigsetjmp(from->env, 0);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
@ -315,18 +319,24 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
|
|||||||
|
|
||||||
Coroutine *qemu_coroutine_self(void)
|
Coroutine *qemu_coroutine_self(void)
|
||||||
{
|
{
|
||||||
if (!current) {
|
Coroutine *self = get_current();
|
||||||
current = &leader.base;
|
CoroutineUContext *leaderp = get_ptr_leader();
|
||||||
|
|
||||||
|
if (!self) {
|
||||||
|
self = &leaderp->base;
|
||||||
|
set_current(self);
|
||||||
}
|
}
|
||||||
#ifdef CONFIG_TSAN
|
#ifdef CONFIG_TSAN
|
||||||
if (!leader.tsan_co_fiber) {
|
if (!leaderp->tsan_co_fiber) {
|
||||||
leader.tsan_co_fiber = __tsan_get_current_fiber();
|
leaderp->tsan_co_fiber = __tsan_get_current_fiber();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return current;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool qemu_in_coroutine(void)
|
bool qemu_in_coroutine(void)
|
||||||
{
|
{
|
||||||
return current && current->caller;
|
Coroutine *self = get_current();
|
||||||
|
|
||||||
|
return self && self->caller;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user