xref: /openbmc/qemu/accel/accel-blocker.c (revision ca5aa28e)
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/lockcnt.h"
29 #include "qemu/thread.h"
30 #include "qemu/main-loop.h"
31 #include "hw/core/cpu.h"
32 #include "sysemu/accel-blocker.h"
33 
34 static QemuLockCnt accel_in_ioctl_lock;
35 static QemuEvent accel_in_ioctl_event;
36 
37 void accel_blocker_init(void)
38 {
39     qemu_lockcnt_init(&accel_in_ioctl_lock);
40     qemu_event_init(&accel_in_ioctl_event, false);
41 }
42 
43 void accel_ioctl_begin(void)
44 {
45     if (likely(bql_locked())) {
46         return;
47     }
48 
49     /* block if lock is taken in kvm_ioctl_inhibit_begin() */
50     qemu_lockcnt_inc(&accel_in_ioctl_lock);
51 }
52 
53 void accel_ioctl_end(void)
54 {
55     if (likely(bql_locked())) {
56         return;
57     }
58 
59     qemu_lockcnt_dec(&accel_in_ioctl_lock);
60     /* change event to SET. If event was BUSY, wake up all waiters */
61     qemu_event_set(&accel_in_ioctl_event);
62 }
63 
64 void accel_cpu_ioctl_begin(CPUState *cpu)
65 {
66     if (unlikely(bql_locked())) {
67         return;
68     }
69 
70     /* block if lock is taken in kvm_ioctl_inhibit_begin() */
71     qemu_lockcnt_inc(&cpu->in_ioctl_lock);
72 }
73 
74 void accel_cpu_ioctl_end(CPUState *cpu)
75 {
76     if (unlikely(bql_locked())) {
77         return;
78     }
79 
80     qemu_lockcnt_dec(&cpu->in_ioctl_lock);
81     /* change event to SET. If event was BUSY, wake up all waiters */
82     qemu_event_set(&accel_in_ioctl_event);
83 }
84 
85 static bool accel_has_to_wait(void)
86 {
87     CPUState *cpu;
88     bool needs_to_wait = false;
89 
90     CPU_FOREACH(cpu) {
91         if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
92             /* exit the ioctl, if vcpu is running it */
93             qemu_cpu_kick(cpu);
94             needs_to_wait = true;
95         }
96     }
97 
98     return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
99 }
100 
101 void accel_ioctl_inhibit_begin(void)
102 {
103     CPUState *cpu;
104 
105     /*
106      * We allow to inhibit only when holding the BQL, so we can identify
107      * when an inhibitor wants to issue an ioctl easily.
108      */
109     g_assert(bql_locked());
110 
111     /* Block further invocations of the ioctls outside the BQL.  */
112     CPU_FOREACH(cpu) {
113         qemu_lockcnt_lock(&cpu->in_ioctl_lock);
114     }
115     qemu_lockcnt_lock(&accel_in_ioctl_lock);
116 
117     /* Keep waiting until there are running ioctls */
118     while (true) {
119 
120         /* Reset event to FREE. */
121         qemu_event_reset(&accel_in_ioctl_event);
122 
123         if (accel_has_to_wait()) {
124             /*
125              * If event is still FREE, and there are ioctls still in progress,
126              * wait.
127              *
128              *  If an ioctl finishes before qemu_event_wait(), it will change
129              * the event state to SET. This will prevent qemu_event_wait() from
130              * blocking, but it's not a problem because if other ioctls are
131              * still running the loop will iterate once more and reset the event
132              * status to FREE so that it can wait properly.
133              *
134              * If an ioctls finishes while qemu_event_wait() is blocking, then
135              * it will be waken up, but also here the while loop makes sure
136              * to re-enter the wait if there are other running ioctls.
137              */
138             qemu_event_wait(&accel_in_ioctl_event);
139         } else {
140             /* No ioctl is running */
141             return;
142         }
143     }
144 }
145 
146 void accel_ioctl_inhibit_end(void)
147 {
148     CPUState *cpu;
149 
150     qemu_lockcnt_unlock(&accel_in_ioctl_lock);
151     CPU_FOREACH(cpu) {
152         qemu_lockcnt_unlock(&cpu->in_ioctl_lock);
153     }
154 }
155 
156