You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.4 KiB
100 lines
3.4 KiB
From 336581e6e9ace3f1ddd24ad0a258db9785f9b0ed Mon Sep 17 00:00:00 2001
|
|
From: Stefan Hajnoczi <stefanha@redhat.com>
|
|
Date: Tue, 17 May 2022 12:08:12 +0100
|
|
Subject: [PATCH 3/6] coroutine-win32: use QEMU_DEFINE_STATIC_CO_TLS()
|
|
MIME-Version: 1.0
|
|
Content-Type: text/plain; charset=UTF-8
|
|
Content-Transfer-Encoding: 8bit
|
|
|
|
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
|
|
RH-MergeRequest: 89: coroutine: use coroutine TLS macros to protect thread-local variables
|
|
RH-Commit: [3/3] 55b35dfdae1bc7d6f614ac9f81a92f5c6431f713 (stefanha/centos-stream-qemu-kvm)
|
|
RH-Bugzilla: 1952483
|
|
RH-Acked-by: Hanna Reitz <hreitz@redhat.com>
|
|
RH-Acked-by: Eric Blake <eblake@redhat.com>
|
|
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
|
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.
|
|
|
|
I think coroutine-win32.c could get away with __thread because the
|
|
variables are only used in situations where either the stale value is
|
|
correct (current) or outside coroutine context (loading leader when
|
|
current is NULL). Due to the difficulty of being sure that this is
|
|
really safe in all scenarios it seems worth converting it anyway.
|
|
|
|
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
Message-Id: <20220307153853.602859-4-stefanha@redhat.com>
|
|
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
|
|
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
|
|
(cherry picked from commit c1fe694357a328c807ae3cc6961c19e923448fcc)
|
|
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
---
|
|
util/coroutine-win32.c | 18 +++++++++++++-----
|
|
1 file changed, 13 insertions(+), 5 deletions(-)
|
|
|
|
diff --git a/util/coroutine-win32.c b/util/coroutine-win32.c
|
|
index de6bd4fd3e..c02a62c896 100644
|
|
--- a/util/coroutine-win32.c
|
|
+++ b/util/coroutine-win32.c
|
|
@@ -25,6 +25,7 @@
|
|
#include "qemu/osdep.h"
|
|
#include "qemu-common.h"
|
|
#include "qemu/coroutine_int.h"
|
|
+#include "qemu/coroutine-tls.h"
|
|
|
|
typedef struct
|
|
{
|
|
@@ -34,8 +35,8 @@ typedef struct
|
|
CoroutineAction action;
|
|
} CoroutineWin32;
|
|
|
|
-static __thread CoroutineWin32 leader;
|
|
-static __thread Coroutine *current;
|
|
+QEMU_DEFINE_STATIC_CO_TLS(CoroutineWin32, leader);
|
|
+QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
|
|
|
|
/* This function is marked noinline to prevent GCC from inlining it
|
|
* into coroutine_trampoline(). If we allow it to do that then it
|
|
@@ -52,7 +53,7 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
|
|
CoroutineWin32 *from = DO_UPCAST(CoroutineWin32, base, from_);
|
|
CoroutineWin32 *to = DO_UPCAST(CoroutineWin32, base, to_);
|
|
|
|
- current = to_;
|
|
+ set_current(to_);
|
|
|
|
to->action = action;
|
|
SwitchToFiber(to->fiber);
|
|
@@ -89,14 +90,21 @@ void qemu_coroutine_delete(Coroutine *co_)
|
|
|
|
Coroutine *qemu_coroutine_self(void)
|
|
{
|
|
+ Coroutine *current = get_current();
|
|
+
|
|
if (!current) {
|
|
- current = &leader.base;
|
|
- leader.fiber = ConvertThreadToFiber(NULL);
|
|
+ CoroutineWin32 *leader = get_ptr_leader();
|
|
+
|
|
+ current = &leader->base;
|
|
+ set_current(current);
|
|
+ leader->fiber = ConvertThreadToFiber(NULL);
|
|
}
|
|
return current;
|
|
}
|
|
|
|
bool qemu_in_coroutine(void)
|
|
{
|
|
+ Coroutine *current = get_current();
|
|
+
|
|
return current && current->caller;
|
|
}
|
|
--
|
|
2.31.1
|
|
|