From d5d2b15ecf62c662985983ca065ddeeec48fd248 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Tue, 22 Feb 2022 14:01:50 +0000 Subject: cpus: use coroutine TLS macros for iothread_locked qemu_mutex_iothread_locked() may be used from coroutines. Standard __thread variables cannot be used by coroutines. Use the coroutine TLS macros instead. Signed-off-by: Stefan Hajnoczi Message-Id: <20220222140150.27240-5-stefanha@redhat.com> Signed-off-by: Kevin Wolf --- softmmu/cpus.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'softmmu') diff --git a/softmmu/cpus.c b/softmmu/cpus.c index 035395ae13..d1ff3cfea1 100644 --- a/softmmu/cpus.c +++ b/softmmu/cpus.c @@ -25,6 +25,7 @@ #include "qemu/osdep.h" #include "qemu-common.h" #include "monitor/monitor.h" +#include "qemu/coroutine-tls.h" #include "qapi/error.h" #include "qapi/qapi-commands-machine.h" #include "qapi/qapi-commands-misc.h" @@ -473,11 +474,11 @@ bool qemu_in_vcpu_thread(void) return current_cpu && qemu_cpu_is_self(current_cpu); } -static __thread bool iothread_locked = false; +QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked) bool qemu_mutex_iothread_locked(void) { - return iothread_locked; + return get_iothread_locked(); } /* @@ -490,13 +491,13 @@ void qemu_mutex_lock_iothread_impl(const char *file, int line) g_assert(!qemu_mutex_iothread_locked()); bql_lock(&qemu_global_mutex, file, line); - iothread_locked = true; + set_iothread_locked(true); } void qemu_mutex_unlock_iothread(void) { g_assert(qemu_mutex_iothread_locked()); - iothread_locked = false; + set_iothread_locked(false); qemu_mutex_unlock(&qemu_global_mutex); } -- cgit v1.2.3 From 6538692e2802666926e62d9309ddddda5ec9dc3b Mon Sep 17 00:00:00 2001 From: Emanuele Giuseppe Esposito Date: Thu, 3 Mar 2022 10:15:46 -0500 Subject: main-loop.h: introduce qemu_in_main_thread() When invoked from the main loop, this function is the same as qemu_mutex_iothread_locked, and returns true if the BQL is held. When invoked from iothreads or tests, it returns true only if the current AioContext is the Main Loop. This essentially just extends qemu_mutex_iothread_locked to work also in unit tests or other users like storage-daemon, that run in the Main Loop but end up using the implementation in stubs/iothread-lock.c. Using qemu_mutex_iothread_locked in unit tests defaults to false because they use the implementation in stubs/iothread-lock, making all assertions added in next patches fail despite the AioContext is still the main loop. See the comment in the function header for more information. Signed-off-by: Emanuele Giuseppe Esposito Message-Id: <20220303151616.325444-2-eesposit@redhat.com> Signed-off-by: Kevin Wolf --- softmmu/cpus.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'softmmu') diff --git a/softmmu/cpus.c b/softmmu/cpus.c index d1ff3cfea1..1681844b61 100644 --- a/softmmu/cpus.c +++ b/softmmu/cpus.c @@ -481,6 +481,11 @@ bool qemu_mutex_iothread_locked(void) return get_iothread_locked(); } +bool qemu_in_main_thread(void) +{ + return qemu_mutex_iothread_locked(); +} + /* * The BQL is taken from so many places that it is worth profiling the * callers directly, instead of funneling them all through a single function. -- cgit v1.2.3 From 0439c5a4623d674efa0c72abd62ca6e98bb7cf87 Mon Sep 17 00:00:00 2001 From: Emanuele Giuseppe Esposito Date: Thu, 3 Mar 2022 10:15:53 -0500 Subject: block/block-backend.c: assertions for block-backend All the global state (GS) API functions will check that qemu_in_main_thread() returns true. If not, it means that the safety of BQL cannot be guaranteed, and they need to be moved to I/O. Signed-off-by: Emanuele Giuseppe Esposito Message-Id: <20220303151616.325444-9-eesposit@redhat.com> Signed-off-by: Kevin Wolf --- softmmu/qdev-monitor.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'softmmu') diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c index 01f3834db5..ff14af8396 100644 --- a/softmmu/qdev-monitor.c +++ b/softmmu/qdev-monitor.c @@ -971,6 +971,8 @@ BlockBackend *blk_by_qdev_id(const char *id, Error **errp) DeviceState *dev; BlockBackend *blk; + GLOBAL_STATE_CODE(); + dev = find_device_state(id, errp); if (dev == NULL) { return NULL; -- cgit v1.2.3