mirror of
https://gitlab.com/qemu-project/qemu.git
synced 2024-12-08 06:00:35 +03:00
195801d700
The Big QEMU Lock (BQL) has many names and they are confusing. The actual QemuMutex variable is called qemu_global_mutex but it's commonly referred to as the BQL in discussions and some code comments. The locking APIs, however, are called qemu_mutex_lock_iothread() and qemu_mutex_unlock_iothread(). The "iothread" name is historic and comes from when the main thread was split into into KVM vcpu threads and the "iothread" (now called the main loop thread). I have contributed to the confusion myself by introducing a separate --object iothread, a separate concept unrelated to the BQL. The "iothread" name is no longer appropriate for the BQL. Rename the locking APIs to: - void bql_lock(void) - void bql_unlock(void) - bool bql_locked(void) There are more APIs with "iothread" in their names. Subsequent patches will rename them. There are also comments and documentation that will be updated in later patches. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paul Durrant <paul@xen.org> Acked-by: Fabiano Rosas <farosas@suse.de> Acked-by: David Woodhouse <dwmw@amazon.co.uk> Reviewed-by: Cédric Le Goater <clg@kaod.org> Acked-by: Peter Xu <peterx@redhat.com> Acked-by: Eric Farman <farman@linux.ibm.com> Reviewed-by: Harsh Prateek Bora <harshpb@linux.ibm.com> Acked-by: Hyman Huang <yong.huang@smartx.com> Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com> Message-id: 20240102153529.486531-2-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
155 lines
4.5 KiB
C
155 lines
4.5 KiB
C
/*
|
|
* Lock to inhibit accelerator ioctls
|
|
*
|
|
* Copyright (c) 2022 Red Hat Inc.
|
|
*
|
|
* Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "qemu/osdep.h"
|
|
#include "qemu/thread.h"
|
|
#include "qemu/main-loop.h"
|
|
#include "hw/core/cpu.h"
|
|
#include "sysemu/accel-blocker.h"
|
|
|
|
static QemuLockCnt accel_in_ioctl_lock;
|
|
static QemuEvent accel_in_ioctl_event;
|
|
|
|
void accel_blocker_init(void)
|
|
{
|
|
qemu_lockcnt_init(&accel_in_ioctl_lock);
|
|
qemu_event_init(&accel_in_ioctl_event, false);
|
|
}
|
|
|
|
void accel_ioctl_begin(void)
|
|
{
|
|
if (likely(bql_locked())) {
|
|
return;
|
|
}
|
|
|
|
/* block if lock is taken in kvm_ioctl_inhibit_begin() */
|
|
qemu_lockcnt_inc(&accel_in_ioctl_lock);
|
|
}
|
|
|
|
void accel_ioctl_end(void)
|
|
{
|
|
if (likely(bql_locked())) {
|
|
return;
|
|
}
|
|
|
|
qemu_lockcnt_dec(&accel_in_ioctl_lock);
|
|
/* change event to SET. If event was BUSY, wake up all waiters */
|
|
qemu_event_set(&accel_in_ioctl_event);
|
|
}
|
|
|
|
void accel_cpu_ioctl_begin(CPUState *cpu)
|
|
{
|
|
if (unlikely(bql_locked())) {
|
|
return;
|
|
}
|
|
|
|
/* block if lock is taken in kvm_ioctl_inhibit_begin() */
|
|
qemu_lockcnt_inc(&cpu->in_ioctl_lock);
|
|
}
|
|
|
|
void accel_cpu_ioctl_end(CPUState *cpu)
|
|
{
|
|
if (unlikely(bql_locked())) {
|
|
return;
|
|
}
|
|
|
|
qemu_lockcnt_dec(&cpu->in_ioctl_lock);
|
|
/* change event to SET. If event was BUSY, wake up all waiters */
|
|
qemu_event_set(&accel_in_ioctl_event);
|
|
}
|
|
|
|
static bool accel_has_to_wait(void)
|
|
{
|
|
CPUState *cpu;
|
|
bool needs_to_wait = false;
|
|
|
|
CPU_FOREACH(cpu) {
|
|
if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
|
|
/* exit the ioctl, if vcpu is running it */
|
|
qemu_cpu_kick(cpu);
|
|
needs_to_wait = true;
|
|
}
|
|
}
|
|
|
|
return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
|
|
}
|
|
|
|
void accel_ioctl_inhibit_begin(void)
|
|
{
|
|
CPUState *cpu;
|
|
|
|
/*
|
|
* We allow to inhibit only when holding the BQL, so we can identify
|
|
* when an inhibitor wants to issue an ioctl easily.
|
|
*/
|
|
g_assert(bql_locked());
|
|
|
|
/* Block further invocations of the ioctls outside the BQL. */
|
|
CPU_FOREACH(cpu) {
|
|
qemu_lockcnt_lock(&cpu->in_ioctl_lock);
|
|
}
|
|
qemu_lockcnt_lock(&accel_in_ioctl_lock);
|
|
|
|
/* Keep waiting until there are running ioctls */
|
|
while (true) {
|
|
|
|
/* Reset event to FREE. */
|
|
qemu_event_reset(&accel_in_ioctl_event);
|
|
|
|
if (accel_has_to_wait()) {
|
|
/*
|
|
* If event is still FREE, and there are ioctls still in progress,
|
|
* wait.
|
|
*
|
|
* If an ioctl finishes before qemu_event_wait(), it will change
|
|
* the event state to SET. This will prevent qemu_event_wait() from
|
|
* blocking, but it's not a problem because if other ioctls are
|
|
* still running the loop will iterate once more and reset the event
|
|
* status to FREE so that it can wait properly.
|
|
*
|
|
* If an ioctls finishes while qemu_event_wait() is blocking, then
|
|
* it will be waken up, but also here the while loop makes sure
|
|
* to re-enter the wait if there are other running ioctls.
|
|
*/
|
|
qemu_event_wait(&accel_in_ioctl_event);
|
|
} else {
|
|
/* No ioctl is running */
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void accel_ioctl_inhibit_end(void)
|
|
{
|
|
CPUState *cpu;
|
|
|
|
qemu_lockcnt_unlock(&accel_in_ioctl_lock);
|
|
CPU_FOREACH(cpu) {
|
|
qemu_lockcnt_unlock(&cpu->in_ioctl_lock);
|
|
}
|
|
}
|
|
|