Merge tag 'qemu-macppc-20230206' of https://github.com/mcayland/qemu into staging
[qemu.git] / block / graph-lock.c
blob454c31e691cf490ec11d1a02f5a13117f6b374fe
1 /*
2 * Graph lock: rwlock to protect block layer graph manipulations (add/remove
3 * edges and nodes)
5 * Copyright (c) 2022 Red Hat
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/main-loop.h"
23 #include "block/graph-lock.h"
24 #include "block/block.h"
25 #include "block/block_int.h"
27 /* Dummy lock object to use for Thread Safety Analysis (TSA) */
28 BdrvGraphLock graph_lock;
30 /* Protects the list of aiocontext and orphaned_reader_count */
31 static QemuMutex aio_context_list_lock;
33 /* Written and read with atomic operations. */
34 static int has_writer;
37 * A reader coroutine could move from an AioContext to another.
38 * If this happens, there is no problem from the point of view of
39 * counters. The problem is that the total count becomes
40 * unbalanced if one of the two AioContexts gets deleted.
41 * The count of readers must remain correct, so the AioContext's
42 * balance is transferred to this glboal variable.
43 * Protected by aio_context_list_lock.
45 static uint32_t orphaned_reader_count;
47 /* Queue of readers waiting for the writer to finish */
48 static CoQueue reader_queue;
50 struct BdrvGraphRWlock {
51 /* How many readers are currently reading the graph. */
52 uint32_t reader_count;
55 * List of BdrvGraphRWlock kept in graph-lock.c
56 * Protected by aio_context_list_lock
58 QTAILQ_ENTRY(BdrvGraphRWlock) next_aio;
62 * List of BdrvGraphRWlock. This list ensures that each BdrvGraphRWlock
63 * can safely modify only its own counter, avoid reading/writing
64 * others and thus improving performances by avoiding cacheline bounces.
66 static QTAILQ_HEAD(, BdrvGraphRWlock) aio_context_list =
67 QTAILQ_HEAD_INITIALIZER(aio_context_list);
69 static void __attribute__((__constructor__)) bdrv_init_graph_lock(void)
71 qemu_mutex_init(&aio_context_list_lock);
72 qemu_co_queue_init(&reader_queue);
75 void register_aiocontext(AioContext *ctx)
77 ctx->bdrv_graph = g_new0(BdrvGraphRWlock, 1);
78 QEMU_LOCK_GUARD(&aio_context_list_lock);
79 assert(ctx->bdrv_graph->reader_count == 0);
80 QTAILQ_INSERT_TAIL(&aio_context_list, ctx->bdrv_graph, next_aio);
83 void unregister_aiocontext(AioContext *ctx)
85 QEMU_LOCK_GUARD(&aio_context_list_lock);
86 orphaned_reader_count += ctx->bdrv_graph->reader_count;
87 QTAILQ_REMOVE(&aio_context_list, ctx->bdrv_graph, next_aio);
88 g_free(ctx->bdrv_graph);
91 static uint32_t reader_count(void)
93 BdrvGraphRWlock *brdv_graph;
94 uint32_t rd;
96 QEMU_LOCK_GUARD(&aio_context_list_lock);
98 /* rd can temporarly be negative, but the total will *always* be >= 0 */
99 rd = orphaned_reader_count;
100 QTAILQ_FOREACH(brdv_graph, &aio_context_list, next_aio) {
101 rd += qatomic_read(&brdv_graph->reader_count);
104 /* shouldn't overflow unless there are 2^31 readers */
105 assert((int32_t)rd >= 0);
106 return rd;
109 void bdrv_graph_wrlock(void)
111 GLOBAL_STATE_CODE();
112 assert(!qatomic_read(&has_writer));
114 /* Make sure that constantly arriving new I/O doesn't cause starvation */
115 bdrv_drain_all_begin_nopoll();
118 * reader_count == 0: this means writer will read has_reader as 1
119 * reader_count >= 1: we don't know if writer read has_writer == 0 or 1,
120 * but we need to wait.
121 * Wait by allowing other coroutine (and possible readers) to continue.
123 do {
125 * has_writer must be 0 while polling, otherwise we get a deadlock if
126 * any callback involved during AIO_WAIT_WHILE() tries to acquire the
127 * reader lock.
129 qatomic_set(&has_writer, 0);
130 AIO_WAIT_WHILE(qemu_get_aio_context(), reader_count() >= 1);
131 qatomic_set(&has_writer, 1);
134 * We want to only check reader_count() after has_writer = 1 is visible
135 * to other threads. That way no more readers can sneak in after we've
136 * determined reader_count() == 0.
138 smp_mb();
139 } while (reader_count() >= 1);
141 bdrv_drain_all_end();
144 void bdrv_graph_wrunlock(void)
146 GLOBAL_STATE_CODE();
147 QEMU_LOCK_GUARD(&aio_context_list_lock);
148 assert(qatomic_read(&has_writer));
151 * No need for memory barriers, this works in pair with
152 * the slow path of rdlock() and both take the lock.
154 qatomic_store_release(&has_writer, 0);
156 /* Wake up all coroutine that are waiting to read the graph */
157 qemu_co_enter_all(&reader_queue, &aio_context_list_lock);
160 void coroutine_fn bdrv_graph_co_rdlock(void)
162 BdrvGraphRWlock *bdrv_graph;
163 bdrv_graph = qemu_get_current_aio_context()->bdrv_graph;
165 /* Do not lock if in main thread */
166 if (qemu_in_main_thread()) {
167 return;
170 for (;;) {
171 qatomic_set(&bdrv_graph->reader_count,
172 bdrv_graph->reader_count + 1);
173 /* make sure writer sees reader_count before we check has_writer */
174 smp_mb();
177 * has_writer == 0: this means writer will read reader_count as >= 1
178 * has_writer == 1: we don't know if writer read reader_count == 0
179 * or > 0, but we need to wait anyways because
180 * it will write.
182 if (!qatomic_read(&has_writer)) {
183 break;
187 * Synchronize access with reader_count() in bdrv_graph_wrlock().
188 * Case 1:
189 * If this critical section gets executed first, reader_count will
190 * decrease and the reader will go to sleep.
191 * Then the writer will read reader_count that does not take into
192 * account this reader, and if there's no other reader it will
193 * enter the write section.
194 * Case 2:
195 * If reader_count() critical section gets executed first,
196 * then writer will read reader_count >= 1.
197 * It will wait in AIO_WAIT_WHILE(), but once it releases the lock
198 * we will enter this critical section and call aio_wait_kick().
200 WITH_QEMU_LOCK_GUARD(&aio_context_list_lock) {
202 * Additional check when we use the above lock to synchronize
203 * with bdrv_graph_wrunlock().
204 * Case 1:
205 * If this gets executed first, has_writer is still 1, so we reduce
206 * reader_count and go to sleep.
207 * Then the writer will set has_writer to 0 and wake up all readers,
208 * us included.
209 * Case 2:
210 * If bdrv_graph_wrunlock() critical section gets executed first,
211 * then it will set has_writer to 0 and wake up all other readers.
212 * Then we execute this critical section, and therefore must check
213 * again for has_writer, otherwise we sleep without any writer
214 * actually running.
216 if (!qatomic_read(&has_writer)) {
217 return;
220 /* slow path where reader sleeps */
221 bdrv_graph->reader_count--;
222 aio_wait_kick();
223 qemu_co_queue_wait(&reader_queue, &aio_context_list_lock);
228 void coroutine_fn bdrv_graph_co_rdunlock(void)
230 BdrvGraphRWlock *bdrv_graph;
231 bdrv_graph = qemu_get_current_aio_context()->bdrv_graph;
233 /* Do not lock if in main thread */
234 if (qemu_in_main_thread()) {
235 return;
238 qatomic_store_release(&bdrv_graph->reader_count,
239 bdrv_graph->reader_count - 1);
240 /* make sure writer sees reader_count before we check has_writer */
241 smp_mb();
244 * has_writer == 0: this means reader will read reader_count decreased
245 * has_writer == 1: we don't know if writer read reader_count old or
246 * new. Therefore, kick again so on next iteration
247 * writer will for sure read the updated value.
249 if (qatomic_read(&has_writer)) {
250 aio_wait_kick();
254 void bdrv_graph_rdlock_main_loop(void)
256 GLOBAL_STATE_CODE();
257 assert(!qemu_in_coroutine());
260 void bdrv_graph_rdunlock_main_loop(void)
262 GLOBAL_STATE_CODE();
263 assert(!qemu_in_coroutine());
266 void assert_bdrv_graph_readable(void)
268 assert(qemu_in_main_thread() || reader_count());
271 void assert_bdrv_graph_writable(void)
273 assert(qemu_in_main_thread());
274 assert(qatomic_read(&has_writer));