util/coroutine: Cleanup start_switch_fiber_ for TSAN.
PATCH 1/1 util/coroutine: Cleanup start_switch_fiber_ for TSAN.
This is a cleanup patch to follow-up the patch which introduced TSAN. This patch makes separate start_switch_fiber_ functions for TSAN and ASAN.
This does two things:
1. Unrelated ASAN and TSAN code is separate and each function only has arguments that are actually needed.
2. The co->tsan_caller_fiber and co->tsan_co_fiber fields are only access from within #ifdef CONFIG_TSAN.
--- util/coroutine-ucontext.c | 52 +++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-)
util/coroutine-ucontext.c +34 -18
@@ -47,8 +47,10 @@ typedef struct {size_t stack_size;sigjmp_buf env; +#ifdef CONFIG_TSAN void *tsan_co_fiber;void *tsan_caller_fiber; +#endif #ifdef CONFIG_VALGRIND_H unsigned int valgrind_stack_id;
@@ -72,7 +74,10 @@ union cc_arg {int i[2 ]; }; -/* QEMU_ALWAYS_INLINE only does so if __OPTIMIZE__, so we cannot use it. */ +/* +* QEMU_ALWAYS_INLINE only does so if __OPTIMIZE__, so we cannot use it. +* always_inline is required to avoid TSan runtime fatal errors. +*/ static inline __attribute__((always_inline))void on_new_fiber (CoroutineUContext *co) {
@@ -82,6 +87,7 @@ void on_new_fiber(CoroutineUContext *co)#endif } +/* always_inline is required to avoid TSan runtime fatal errors. */ static inline __attribute__((always_inline))void finish_switch_fiber (void *fake_stack_save) {
@@ -104,18 +110,29 @@ void finish_switch_fiber(void *fake_stack_save)#endif } -static inline __attribute__((always_inline))void start_switch_fiber ( -CoroutineAction action,void **fake_stack_save, -const void *bottom,size_t size,void *new_fiber) +/* always_inline is required to avoid TSan runtime fatal errors. */ +static inline __attribute__((always_inline)) +void start_switch_fiber_asan (CoroutineAction action,void **fake_stack_save, +const void *bottom,size_t size) {#ifdef CONFIG_ASAN __sanitizer_start_switch_fiber ( action ==COROUTINE_TERMINATE ?NULL : fake_stack_save, bottom, size);#endif +} + +/* always_inline is required to avoid TSan runtime fatal errors. */ +static inline __attribute__((always_inline)) +void start_switch_fiber_tsan (void **fake_stack_save, +CoroutineUContext *co, +bool caller) +{#ifdef CONFIG_TSAN -void *curr_fiber = -__tsan_get_current_fiber (); +void *new_fiber = caller ? + co->tsan_caller_fiber : + co->tsan_co_fiber; +void *curr_fiber =__tsan_get_current_fiber ();__tsan_acquire (curr_fiber); *fake_stack_save = curr_fiber;
@@ -139,12 +156,9 @@ static void coroutine_trampoline(int i0, int i1)/* Initialize longjmp environment and switch back the caller */ if (!sigsetjmp (self->env,0 )) { -start_switch_fiber ( -COROUTINE_YIELD , - &fake_stack_save, - leader.stack, - leader.stack_size, - self->tsan_caller_fiber); +start_switch_fiber_asan (COROUTINE_YIELD , &fake_stack_save, leader.stack, + leader.stack_size); +start_switch_fiber_tsan (&fake_stack_save, self, true);/* true=caller */ siglongjmp (*(sigjmp_buf *)co->entry_arg,1 ); }
@@ -199,10 +213,10 @@ Coroutine *qemu_coroutine_new(void)/* swapcontext() in, siglongjmp() back out */ if (!sigsetjmp (old_env,0 )) { -start_switch_fiber ( -COROUTINE_YIELD , - &fake_stack_save, - co->stack, co->stack_size, co->tsan_co_fiber); +start_switch_fiber_asan (COROUTINE_YIELD , &fake_stack_save, co->stack, + co->stack_size); +start_switch_fiber_tsan (&fake_stack_save, + co, false);/* false=not caller */ swapcontext (&old_uc, &uc); }
@@ -259,8 +273,10 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, ret =sigsetjmp (from->env,0 );if (ret ==0 ) { -start_switch_fiber (action, &fake_stack_save, - to->stack, to->stack_size, to->tsan_co_fiber); +start_switch_fiber_asan (action, &fake_stack_save, to->stack, + to->stack_size); +start_switch_fiber_tsan (&fake_stack_save, + to, false);/* false=not caller */ siglongjmp (to->env, action); }
Signature · 1 line
2.17.1
Quoted 9 lines · Robert Foley
This is a cleanup patch to follow-up the patch which introduced TSAN. This patch makes separate
start_switch_fiber_functions for TSAN and ASAN.This does two things:
1. Unrelated ASAN and TSAN code is separate and each function only has arguments that are actually needed.
2. Theco->tsan_caller_fiberandco->tsan_co_fiberfields are only access from within #ifdefCONFIG_TSAN.
Quoted 4 lines
Signed-off-by Robert Foley <[email protected]>--- util/coroutine-ucontext.c | 52 +++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-)
Thank you for revisiting this patch after it was applied!
Quoted 10 lines · Stefan Hajnoczi
Quoted 9 lines · Robert Foley
This is a cleanup patch to follow-up the patch which introduced TSAN. This patch makes separate
start_switch_fiber_functions for TSAN and ASAN.This does two things:
1. Unrelated ASAN and TSAN code is separate and each function only has arguments that are actually needed.
2. Theco->tsan_caller_fiberandco->tsan_co_fiberfields are only access from within #ifdefCONFIG_TSAN.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Robert Foley <[email protected]>--- util/coroutine-ucontext.c | 52 +++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-)Thank you for revisiting this patch after it was applied!
Reviewed-by Stefan Hajnoczi <[email protected]>
Who takes care of coroutine patches or should this go via a different tree?
Signature · 1 line
Alex Bennée
Quoted 18 lines · Alex Bennée
Quoted 15 lines · Stefan Hajnoczi
Quoted 13 lines · Robert Foley
This is a cleanup patch to follow-up the patch which introduced TSAN. This patch makes separate
start_switch_fiber_functions for TSAN and ASAN.This does two things:
1. Unrelated ASAN and TSAN code is separate and each function only has arguments that are actually needed.
2. Theco->tsan_caller_fiberandco->tsan_co_fiberfields are only access from within #ifdefCONFIG_TSAN.Signed-off-by Robert Foley <[email protected]>--- util/coroutine-ucontext.c | 52 +++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-)
Quoted 6 lines
Quoted 3 lines
Thank you for revisiting this patch after it was applied!
Reviewed-by Stefan Hajnoczi <[email protected]>Who takes care of coroutine patches or should this go via a different tree?
Commit 0aebab04b9289bd37017593b413ce7a762b54c55 ("configure: add --enable-tsan flag + fiber annotations for coroutine-ucontext") went through your tree. Please take this follow-up patch too.
Stefan