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