]> git.proxmox.com Git - pve-qemu.git/blobdiff - debian/patches/extra/0007-coroutine-ucontext-use-QEMU_DEFINE_STATIC_CO_TLS.patch
cherry-pick upstream fixes for 7.0.0
[pve-qemu.git] / debian / patches / extra / 0007-coroutine-ucontext-use-QEMU_DEFINE_STATIC_CO_TLS.patch
diff --git a/debian/patches/extra/0007-coroutine-ucontext-use-QEMU_DEFINE_STATIC_CO_TLS.patch b/debian/patches/extra/0007-coroutine-ucontext-use-QEMU_DEFINE_STATIC_CO_TLS.patch
new file mode 100644 (file)
index 0000000..6c4d523
--- /dev/null
@@ -0,0 +1,121 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Stefan Hajnoczi <stefanha@redhat.com>
+Date: Mon, 7 Mar 2022 15:38:51 +0000
+Subject: [PATCH] coroutine-ucontext: use QEMU_DEFINE_STATIC_CO_TLS()
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+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>
+(cherry-picked from commit 34145a307d849d0b6734d0222a7aa0bb9eef7407)
+Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
+---
+ util/coroutine-ucontext.c | 38 ++++++++++++++++++++++++--------------
+ 1 file changed, 24 insertions(+), 14 deletions(-)
+
+diff --git a/util/coroutine-ucontext.c b/util/coroutine-ucontext.c
+index 904b375192..127d5a13c8 100644
+--- a/util/coroutine-ucontext.c
++++ b/util/coroutine-ucontext.c
+@@ -25,6 +25,7 @@
+ #include "qemu/osdep.h"
+ #include <ucontext.h>
+ #include "qemu/coroutine_int.h"
++#include "qemu/coroutine-tls.h"
+ #ifdef CONFIG_VALGRIND_H
+ #include <valgrind/valgrind.h>
+@@ -66,8 +67,8 @@ typedef struct {
+ /**
+  * Per-thread coroutine bookkeeping
+  */
+-static __thread CoroutineUContext leader;
+-static __thread Coroutine *current;
++QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
++QEMU_DEFINE_STATIC_CO_TLS(CoroutineUContext, leader);
+ /*
+  * 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)
+ {
+ #ifdef CONFIG_ASAN
++    CoroutineUContext *leaderp = get_ptr_leader();
+     const void *bottom_old;
+     size_t size_old;
+     __sanitizer_finish_switch_fiber(fake_stack_save, &bottom_old, &size_old);
+-    if (!leader.stack) {
+-        leader.stack = (void *)bottom_old;
+-        leader.stack_size = size_old;
++    if (!leaderp->stack) {
++        leaderp->stack = (void *)bottom_old;
++        leaderp->stack_size = size_old;
+     }
+ #endif
+ #ifdef CONFIG_TSAN
+@@ -161,8 +163,10 @@ static void coroutine_trampoline(int i0, int i1)
+     /* Initialize longjmp environment and switch back the caller */
+     if (!sigsetjmp(self->env, 0)) {
+-        start_switch_fiber_asan(COROUTINE_YIELD, &fake_stack_save, leader.stack,
+-                                leader.stack_size);
++        CoroutineUContext *leaderp = get_ptr_leader();
++
++        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 */
+         siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
+     }
+@@ -297,7 +301,7 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
+     int ret;
+     void *fake_stack_save = NULL;
+-    current = to_;
++    set_current(to_);
+     ret = sigsetjmp(from->env, 0);
+     if (ret == 0) {
+@@ -315,18 +319,24 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
+ Coroutine *qemu_coroutine_self(void)
+ {
+-    if (!current) {
+-        current = &leader.base;
++    Coroutine *self = get_current();
++    CoroutineUContext *leaderp = get_ptr_leader();
++
++    if (!self) {
++        self = &leaderp->base;
++        set_current(self);
+     }
+ #ifdef CONFIG_TSAN
+-    if (!leader.tsan_co_fiber) {
+-        leader.tsan_co_fiber = __tsan_get_current_fiber();
++    if (!leaderp->tsan_co_fiber) {
++        leaderp->tsan_co_fiber = __tsan_get_current_fiber();
+     }
+ #endif
+-    return current;
++    return self;
+ }
+ bool qemu_in_coroutine(void)
+ {
+-    return current && current->caller;
++    Coroutine *self = get_current();
++
++    return self && self->caller;
+ }