bql: Fix bql_locked status with condvar APIs
RFC PATCH 1/1 bql: Fix bql_locked status with condvar APIs
QEMU has a per-thread "bql_locked" variable stored in TLS section, showing whether the current thread is holding the BQL lock.
It's a pretty handy variable. Function-wise, QEMU have codes trying to conditionally take bql, relying on the var reflecting the locking status (e.g. BQL_LOCK_GUARD), or in a GDB debugging session, we could also look at the variable (in reality, co_tls_bql_locked), to see which thread is currently holding the bql.
When using that as a debugging facility, sometimes we can observe multiple threads holding bql at the same time. It's because QEMU's condvar APIs bypassed the bql_*() API, hence they do not update bql_locked even if they have released the mutex while waiting.
It can cause confusion if one does "thread apply all p co_tls_bql_locked" and see multiple threads reporting true.
Fix this by moving the bql status updates into the mutex debug hooks. Now the variable should always reflect the reality.
--- include/qemu/main-loop.h | 18 ++++++++++++++++++ util/qemu-thread-common.h | 7 +++++++ stubs/iothread-lock.c | 9 +++++++++ system/cpus.c | 14 ++++++++++++-- 4 files changed, 46 insertions(+), 2 deletions(-)
include/qemu/main-loop.h +18 -0
@@ -270,6 +270,24 @@ void rust_bql_mock_lock(void);*/ bool bql_locked (void ); +/** +* mutex_is_bql: +* +* @mutex: the mutex pointer +* +* Returns whether the mutex is the BQL. +*/ +bool mutex_is_bql (QemuMutex *mutex); + +/** +* set_bql_locked: +* +* @locked: update status on whether the BQL is locked +* +* NOTE: this should normally only be invoked when the status changed. +*/ +void bql_update_status (bool locked); +/** * bql_block: Allow/deny releasing the BQL *
util/qemu-thread-common.h +7 -0
@@ -14,6 +14,7 @@#define QEMU_THREAD_COMMON_H #include "qemu/thread.h" +#include "qemu/main-loop.h" #include "trace.h" static inline void qemu_mutex_post_init (QemuMutex *mutex)
@@ -39,6 +40,9 @@ static inline void qemu_mutex_post_lock(QemuMutex *mutex, mutex->line = line;#endif trace_qemu_mutex_locked (mutex, file, line); +if (mutex_is_bql (mutex)) { +bql_update_status (true); + } } static inline void qemu_mutex_pre_unlock(QemuMutex *mutex,
@@ -49,6 +53,9 @@ static inline void qemu_mutex_pre_unlock(QemuMutex *mutex, mutex->line =0 ;#endif trace_qemu_mutex_unlock (mutex, file, line); +if (mutex_is_bql (mutex)) { +bql_update_status (false); + } }#endif
stubs/iothread-lock.c +9 -0
@@ -34,3 +34,12 @@ void bql_block_unlock(bool increase)assert ((new_value > bql_unlock_blocked) == increase); bql_unlock_blocked = new_value; } + +bool mutex_is_bql (QemuMutex *mutex) +{ +return false; +} + +void bql_update_status (bool locked) +{ +}
system/cpus.c +12 -2
@@ -517,6 +517,18 @@ bool qemu_in_vcpu_thread(void) QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked) +bool mutex_is_bql (QemuMutex *mutex) +{ +return mutex == &bql; +} + +void bql_update_status (bool locked) +{ +/* This function should only be used when an update happened.. */ +assert (bql_locked () != locked); +set_bql_locked (locked); +} +static uint32_t bql_unlock_blocked;void bql_block_unlock (bool increase)
@@ -557,14 +569,12 @@ void bql_lock_impl(const char *file, int line)g_assert (!bql_locked ());bql_lock_fn (&bql, file, line); -set_bql_locked (true); }void bql_unlock (void ) {g_assert (bql_locked ());g_assert (!bql_unlock_blocked); -set_bql_locked (false);qemu_mutex_unlock (&bql); }
Signature · 1 line
2.50.1
Quoted 37 lines · Peter Xu
QEMU has a per-thread "
bql_locked" variable stored in TLS section, showing whether the current thread is holding the BQL lock.It's a pretty handy variable. Function-wise, QEMU have codes trying to conditionally take bql, relying on the var reflecting the locking status (e.g.
BQL_LOCK_GUARD), or in a GDB debugging session, we could also look at the variable (in reality,co_tls_bql_locked), to see which thread is currently holding the bql.When using that as a debugging facility, sometimes we can observe multiple threads holding bql at the same time. It's because QEMU's condvar APIs bypassed the bql_*() API, hence they do not update
bql_lockedeven if they have released the mutex while waiting.It can cause confusion if one does "thread apply all p
co_tls_bql_locked" and see multiple threads reporting true.Fix this by moving the bql status updates into the mutex debug hooks. Now the variable should always reflect the reality.
Signed-off-by Peter Xu <[email protected]>--- include/qemu/main-loop.h | 18 ++++++++++++++++++ util/qemu-thread-common.h | 7 +++++++ stubs/iothread-lock.c | 9 +++++++++ system/cpus.c | 14 ++++++++++++-- 4 files changed, 46 insertions(+), 2 deletions(-)include/qemu/main-loop.h +1
-0@@ -270,6 +270,24 @@ void rust_bql_mock_lock(void);*/ bool bql_locked (void ); +/**
include/qemu/main-loop.h void rust_bql_mock_lock(void); · line 283
Quoted 10 lines
include/qemu/main-loop.h +10
-0+* mutex_is_bql: +* +* @mutex: the mutex pointer +* +* Returns whether the mutex is the BQL. +*/ +bool mutex_is_bql (QemuMutex *mutex); + +/** +* set_bql_locked:
This does not match the actual function name (bql_update_status()).
Quoted 100 lines
include/qemu/main-loop.h +7
-0+* +* @locked: update status on whether the BQL is locked +* +* NOTE: this should normally only be invoked when the status changed. +*/ +void bql_update_status (bool locked); +/** * bql_block: Allow/deny releasing the BQL * util/qemu-thread-common.h +7
-0@@ -14,6 +14,7 @@#define QEMU_THREAD_COMMON_H #include "qemu/thread.h" +#include "qemu/main-loop.h" #include "trace.h" static inline void qemu_mutex_post_init (QemuMutex *mutex)@@ -39,6 +40,9 @@ static inline void qemu_mutex_post_lock(QemuMutex *mutex, mutex->line = line;#endif trace_qemu_mutex_locked (mutex, file, line); +if (mutex_is_bql (mutex)) { +bql_update_status (true); + } } static inline void qemu_mutex_pre_unlock(QemuMutex *mutex,@@ -49,6 +53,9 @@ static inline void qemu_mutex_pre_unlock(QemuMutex *mutex, mutex->line =0 ;#endif trace_qemu_mutex_unlock (mutex, file, line); +if (mutex_is_bql (mutex)) { +bql_update_status (false); + } }#endif stubs/iothread-lock.c +9
-0@@ -34,3 +34,12 @@ void bql_block_unlock(bool increase)assert ((new_value > bql_unlock_blocked) == increase); bql_unlock_blocked = new_value; } + +bool mutex_is_bql (QemuMutex *mutex) +{ +return false; +} + +void bql_update_status (bool locked) +{ +}system/cpus.c +12
-2@@ -517,6 +517,18 @@ bool qemu_in_vcpu_thread(void) QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked) +bool mutex_is_bql (QemuMutex *mutex) +{ +return mutex == &bql; +} + +void bql_update_status (bool locked) +{ +/* This function should only be used when an update happened.. */ +assert (bql_locked () != locked); +set_bql_locked (locked); +} +static uint32_t bql_unlock_blocked;void bql_block_unlock (bool increase)@@ -557,14 +569,12 @@ void bql_lock_impl(const char *file, int line)g_assert (!bql_locked ());bql_lock_fn (&bql, file, line); -set_bql_locked (true); }void bql_unlock (void ) {g_assert (bql_locked ());g_assert (!bql_unlock_blocked); -set_bql_locked (false);qemu_mutex_unlock (&bql); }Signature · 1 line
2.50.1
Quoted 4 lines · Stefan Hajnoczi
Quoted 2 lines
+/**
+ *set_bql_locked:This does not match the actual function name (
bql_update_status()).
Ah yes.. will fix, thanks.
Signature · 1 line
Peter Xu