Files
qemu/util/qemu-thread-common.h
Peter Xu c89d1c879a 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.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-ID: <20250904223158.1276992-1-peterx@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
2025-11-03 11:59:32 +01:00

62 lines
1.4 KiB
C

/*
* Common qemu-thread implementation header file.
*
* Copyright Red Hat, Inc. 2018
*
* Authors:
* Peter Xu <peterx@redhat.com>,
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef QEMU_THREAD_COMMON_H
#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)
{
#ifdef CONFIG_DEBUG_MUTEX
mutex->file = NULL;
mutex->line = 0;
#endif
mutex->initialized = true;
}
static inline void qemu_mutex_pre_lock(QemuMutex *mutex,
const char *file, int line)
{
trace_qemu_mutex_lock(mutex, file, line);
}
static inline void qemu_mutex_post_lock(QemuMutex *mutex,
const char *file, int line)
{
#ifdef CONFIG_DEBUG_MUTEX
mutex->file = file;
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,
const char *file, int line)
{
#ifdef CONFIG_DEBUG_MUTEX
mutex->file = NULL;
mutex->line = 0;
#endif
trace_qemu_mutex_unlock(mutex, file, line);
if (mutex_is_bql(mutex)) {
bql_update_status(false);
}
}
#endif