|
|
586cba |
From ffbd90e5f4eba620c7cd631b04f0ed31beb22ffa Mon Sep 17 00:00:00 2001
|
|
|
586cba |
From: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
586cba |
Date: Tue, 17 May 2022 12:07:56 +0100
|
|
|
586cba |
Subject: [PATCH 1/6] coroutine-ucontext: use QEMU_DEFINE_STATIC_CO_TLS()
|
|
|
586cba |
MIME-Version: 1.0
|
|
|
586cba |
Content-Type: text/plain; charset=UTF-8
|
|
|
586cba |
Content-Transfer-Encoding: 8bit
|
|
|
586cba |
|
|
|
586cba |
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
586cba |
RH-MergeRequest: 89: coroutine: use coroutine TLS macros to protect thread-local variables
|
|
|
586cba |
RH-Commit: [1/3] a9782fe8e919c4bd317b7e8744c7ff57d898add3 (stefanha/centos-stream-qemu-kvm)
|
|
|
586cba |
RH-Bugzilla: 1952483
|
|
|
586cba |
RH-Acked-by: Hanna Reitz <hreitz@redhat.com>
|
|
|
586cba |
RH-Acked-by: Eric Blake <eblake@redhat.com>
|
|
|
586cba |
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
586cba |
|
|
|
586cba |
Thread-Local Storage variables cannot be used directly from coroutine
|
|
|
586cba |
code because the compiler may optimize TLS variable accesses across
|
|
|
586cba |
qemu_coroutine_yield() calls. When the coroutine is re-entered from
|
|
|
586cba |
another thread the TLS variables from the old thread must no longer be
|
|
|
586cba |
used.
|
|
|
586cba |
|
|
|
586cba |
Use QEMU_DEFINE_STATIC_CO_TLS() for the current and leader variables.
|
|
|
586cba |
|
|
|
586cba |
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
586cba |
Message-Id: <20220307153853.602859-2-stefanha@redhat.com>
|
|
|
586cba |
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
|
|
|
586cba |
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
586cba |
(cherry picked from commit 34145a307d849d0b6734d0222a7aa0bb9eef7407)
|
|
|
586cba |
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
586cba |
---
|
|
|
586cba |
util/coroutine-ucontext.c | 38 ++++++++++++++++++++++++--------------
|
|
|
586cba |
1 file changed, 24 insertions(+), 14 deletions(-)
|
|
|
586cba |
|
|
|
586cba |
diff --git a/util/coroutine-ucontext.c b/util/coroutine-ucontext.c
|
|
|
586cba |
index 904b375192..127d5a13c8 100644
|
|
|
586cba |
--- a/util/coroutine-ucontext.c
|
|
|
586cba |
+++ b/util/coroutine-ucontext.c
|
|
|
586cba |
@@ -25,6 +25,7 @@
|
|
|
586cba |
#include "qemu/osdep.h"
|
|
|
586cba |
#include <ucontext.h>
|
|
|
586cba |
#include "qemu/coroutine_int.h"
|
|
|
586cba |
+#include "qemu/coroutine-tls.h"
|
|
|
586cba |
|
|
|
586cba |
#ifdef CONFIG_VALGRIND_H
|
|
|
586cba |
#include <valgrind/valgrind.h>
|
|
|
586cba |
@@ -66,8 +67,8 @@ typedef struct {
|
|
|
586cba |
/**
|
|
|
586cba |
* Per-thread coroutine bookkeeping
|
|
|
586cba |
*/
|
|
|
586cba |
-static __thread CoroutineUContext leader;
|
|
|
586cba |
-static __thread Coroutine *current;
|
|
|
586cba |
+QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
|
|
|
586cba |
+QEMU_DEFINE_STATIC_CO_TLS(CoroutineUContext, leader);
|
|
|
586cba |
|
|
|
586cba |
/*
|
|
|
586cba |
* va_args to makecontext() must be type 'int', so passing
|
|
|
586cba |
@@ -97,14 +98,15 @@ static inline __attribute__((always_inline))
|
|
|
586cba |
void finish_switch_fiber(void *fake_stack_save)
|
|
|
586cba |
{
|
|
|
586cba |
#ifdef CONFIG_ASAN
|
|
|
586cba |
+ CoroutineUContext *leaderp = get_ptr_leader();
|
|
|
586cba |
const void *bottom_old;
|
|
|
586cba |
size_t size_old;
|
|
|
586cba |
|
|
|
586cba |
__sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
|
|
|
586cba |
|
|
|
586cba |
- if (!leader.stack) {
|
|
|
586cba |
- leader.stack = (void *)bottom_old;
|
|
|
586cba |
- leader.stack_size = size_old;
|
|
|
586cba |
+ if (!leaderp->stack) {
|
|
|
586cba |
+ leaderp->stack = (void *)bottom_old;
|
|
|
586cba |
+ leaderp->stack_size = size_old;
|
|
|
586cba |
}
|
|
|
586cba |
#endif
|
|
|
586cba |
#ifdef CONFIG_TSAN
|
|
|
586cba |
@@ -161,8 +163,10 @@ static void coroutine_trampoline(int i0, int i1)
|
|
|
586cba |
|
|
|
586cba |
/* Initialize longjmp environment and switch back the caller */
|
|
|
586cba |
if (!sigsetjmp(self->env, 0)) {
|
|
|
586cba |
- start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, leader.stack,
|
|
|
586cba |
- leader.stack_size);
|
|
|
586cba |
+ CoroutineUContext *leaderp = get_ptr_leader();
|
|
|
586cba |
+
|
|
|
586cba |
+ start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save,
|
|
|
586cba |
+ leaderp->stack, leaderp->stack_size);
|
|
|
586cba |
start_switch_fiber_tsan(&fake_stack_save, self, true); /* true=caller */
|
|
|
586cba |
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
|
|
|
586cba |
}
|
|
|
586cba |
@@ -297,7 +301,7 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
|
|
|
586cba |
int ret;
|
|
|
586cba |
void *fake_stack_save = NULL;
|
|
|
586cba |
|
|
|
586cba |
- current = to_;
|
|
|
586cba |
+ set_current(to_);
|
|
|
586cba |
|
|
|
586cba |
ret = sigsetjmp(from->env, 0);
|
|
|
586cba |
if (ret == 0) {
|
|
|
586cba |
@@ -315,18 +319,24 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
|
|
|
586cba |
|
|
|
586cba |
Coroutine *qemu_coroutine_self(void)
|
|
|
586cba |
{
|
|
|
586cba |
- if (!current) {
|
|
|
586cba |
- current = &leader.base;
|
|
|
586cba |
+ Coroutine *self = get_current();
|
|
|
586cba |
+ CoroutineUContext *leaderp = get_ptr_leader();
|
|
|
586cba |
+
|
|
|
586cba |
+ if (!self) {
|
|
|
586cba |
+ self = &leaderp->base;
|
|
|
586cba |
+ set_current(self);
|
|
|
586cba |
}
|
|
|
586cba |
#ifdef CONFIG_TSAN
|
|
|
586cba |
- if (!leader.tsan_co_fiber) {
|
|
|
586cba |
- leader.tsan_co_fiber = __tsan_get_current_fiber();
|
|
|
586cba |
+ if (!leaderp->tsan_co_fiber) {
|
|
|
586cba |
+ leaderp->tsan_co_fiber = __tsan_get_current_fiber();
|
|
|
586cba |
}
|
|
|
586cba |
#endif
|
|
|
586cba |
- return current;
|
|
|
586cba |
+ return self;
|
|
|
586cba |
}
|
|
|
586cba |
|
|
|
586cba |
bool qemu_in_coroutine(void)
|
|
|
586cba |
{
|
|
|
586cba |
- return current && current->caller;
|
|
|
586cba |
+ Coroutine *self = get_current();
|
|
|
586cba |
+
|
|
|
586cba |
+ return self && self->caller;
|
|
|
586cba |
}
|
|
|
586cba |
--
|
|
|
586cba |
2.31.1
|
|
|
586cba |
|