]> git.proxmox.com Git - mirror_qemu.git/blame - accel/accel-blocker.c
gitlab: disable FF_SCRIPT_SECTIONS on msys jobs
[mirror_qemu.git] / accel / accel-blocker.c
CommitLineData
bd688fc9
EGE
1/*
2 * Lock to inhibit accelerator ioctls
3 *
4 * Copyright (c) 2022 Red Hat Inc.
5 *
6 * Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include "qemu/osdep.h"
28#include "qemu/thread.h"
29#include "qemu/main-loop.h"
30#include "hw/core/cpu.h"
31#include "sysemu/accel-blocker.h"
32
33static QemuLockCnt accel_in_ioctl_lock;
34static QemuEvent accel_in_ioctl_event;
35
36void accel_blocker_init(void)
37{
38 qemu_lockcnt_init(&accel_in_ioctl_lock);
39 qemu_event_init(&accel_in_ioctl_event, false);
40}
41
42void accel_ioctl_begin(void)
43{
44 if (likely(qemu_mutex_iothread_locked())) {
45 return;
46 }
47
48 /* block if lock is taken in kvm_ioctl_inhibit_begin() */
49 qemu_lockcnt_inc(&accel_in_ioctl_lock);
50}
51
52void accel_ioctl_end(void)
53{
54 if (likely(qemu_mutex_iothread_locked())) {
55 return;
56 }
57
58 qemu_lockcnt_dec(&accel_in_ioctl_lock);
59 /* change event to SET. If event was BUSY, wake up all waiters */
60 qemu_event_set(&accel_in_ioctl_event);
61}
62
63void accel_cpu_ioctl_begin(CPUState *cpu)
64{
65 if (unlikely(qemu_mutex_iothread_locked())) {
66 return;
67 }
68
69 /* block if lock is taken in kvm_ioctl_inhibit_begin() */
70 qemu_lockcnt_inc(&cpu->in_ioctl_lock);
71}
72
73void accel_cpu_ioctl_end(CPUState *cpu)
74{
75 if (unlikely(qemu_mutex_iothread_locked())) {
76 return;
77 }
78
79 qemu_lockcnt_dec(&cpu->in_ioctl_lock);
80 /* change event to SET. If event was BUSY, wake up all waiters */
81 qemu_event_set(&accel_in_ioctl_event);
82}
83
84static bool accel_has_to_wait(void)
85{
86 CPUState *cpu;
87 bool needs_to_wait = false;
88
89 CPU_FOREACH(cpu) {
90 if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
91 /* exit the ioctl, if vcpu is running it */
92 qemu_cpu_kick(cpu);
93 needs_to_wait = true;
94 }
95 }
96
97 return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
98}
99
100void accel_ioctl_inhibit_begin(void)
101{
102 CPUState *cpu;
103
104 /*
105 * We allow to inhibit only when holding the BQL, so we can identify
106 * when an inhibitor wants to issue an ioctl easily.
107 */
108 g_assert(qemu_mutex_iothread_locked());
109
110 /* Block further invocations of the ioctls outside the BQL. */
111 CPU_FOREACH(cpu) {
112 qemu_lockcnt_lock(&cpu->in_ioctl_lock);
113 }
114 qemu_lockcnt_lock(&accel_in_ioctl_lock);
115
116 /* Keep waiting until there are running ioctls */
117 while (true) {
118
119 /* Reset event to FREE. */
120 qemu_event_reset(&accel_in_ioctl_event);
121
122 if (accel_has_to_wait()) {
123 /*
124 * If event is still FREE, and there are ioctls still in progress,
125 * wait.
126 *
127 * If an ioctl finishes before qemu_event_wait(), it will change
128 * the event state to SET. This will prevent qemu_event_wait() from
129 * blocking, but it's not a problem because if other ioctls are
130 * still running the loop will iterate once more and reset the event
131 * status to FREE so that it can wait properly.
132 *
133 * If an ioctls finishes while qemu_event_wait() is blocking, then
134 * it will be waken up, but also here the while loop makes sure
135 * to re-enter the wait if there are other running ioctls.
136 */
137 qemu_event_wait(&accel_in_ioctl_event);
138 } else {
139 /* No ioctl is running */
140 return;
141 }
142 }
143}
144
145void accel_ioctl_inhibit_end(void)
146{
147 CPUState *cpu;
148
149 qemu_lockcnt_unlock(&accel_in_ioctl_lock);
150 CPU_FOREACH(cpu) {
151 qemu_lockcnt_unlock(&cpu->in_ioctl_lock);
152 }
153}
154