]>
Commit | Line | Data |
---|---|---|
4f999d05 | 1 | /* |
c2b38b27 | 2 | * Data plane event loop |
4f999d05 KW |
3 | * |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
c2b38b27 | 5 | * Copyright (c) 2009-2017 QEMU contributors |
4f999d05 KW |
6 | * |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
d38ea87a | 26 | #include "qemu/osdep.h" |
da34e65c | 27 | #include "qapi/error.h" |
737e150e | 28 | #include "block/aio.h" |
9b34277d | 29 | #include "block/thread-pool.h" |
1de7afc9 | 30 | #include "qemu/main-loop.h" |
0ceb849b | 31 | #include "qemu/atomic.h" |
8c6b0356 | 32 | #include "qemu/rcu_queue.h" |
0187f5c9 | 33 | #include "block/raw-aio.h" |
0c330a73 PB |
34 | #include "qemu/coroutine_int.h" |
35 | #include "trace.h" | |
9a1e9481 | 36 | |
4f999d05 KW |
37 | /***********************************************************/ |
38 | /* bottom halves (can be seen as timers which expire ASAP) */ | |
39 | ||
8c6b0356 SH |
40 | /* QEMUBH::flags values */ |
41 | enum { | |
42 | /* Already enqueued and waiting for aio_bh_poll() */ | |
43 | BH_PENDING = (1 << 0), | |
44 | ||
45 | /* Invoke the callback */ | |
46 | BH_SCHEDULED = (1 << 1), | |
47 | ||
48 | /* Delete without invoking callback */ | |
49 | BH_DELETED = (1 << 2), | |
50 | ||
51 | /* Delete after invoking callback */ | |
52 | BH_ONESHOT = (1 << 3), | |
53 | ||
54 | /* Schedule periodically when the event loop is idle */ | |
55 | BH_IDLE = (1 << 4), | |
56 | }; | |
57 | ||
4f999d05 | 58 | struct QEMUBH { |
2f4dc3c1 | 59 | AioContext *ctx; |
0f08586c | 60 | const char *name; |
4f999d05 KW |
61 | QEMUBHFunc *cb; |
62 | void *opaque; | |
8c6b0356 SH |
63 | QSLIST_ENTRY(QEMUBH) next; |
64 | unsigned flags; | |
4f999d05 KW |
65 | }; |
66 | ||
8c6b0356 SH |
67 | /* Called concurrently from any thread */ |
68 | static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags) | |
69 | { | |
70 | AioContext *ctx = bh->ctx; | |
71 | unsigned old_flags; | |
72 | ||
73 | /* | |
d73415a3 | 74 | * The memory barrier implicit in qatomic_fetch_or makes sure that: |
8c6b0356 SH |
75 | * 1. idle & any writes needed by the callback are done before the |
76 | * locations are read in the aio_bh_poll. | |
77 | * 2. ctx is loaded before the callback has a chance to execute and bh | |
78 | * could be freed. | |
79 | */ | |
d73415a3 | 80 | old_flags = qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags); |
8c6b0356 SH |
81 | if (!(old_flags & BH_PENDING)) { |
82 | QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next); | |
83 | } | |
84 | ||
85 | aio_notify(ctx); | |
86 | } | |
87 | ||
88 | /* Only called from aio_bh_poll() and aio_ctx_finalize() */ | |
89 | static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags) | |
90 | { | |
91 | QEMUBH *bh = QSLIST_FIRST_RCU(head); | |
92 | ||
93 | if (!bh) { | |
94 | return NULL; | |
95 | } | |
96 | ||
97 | QSLIST_REMOVE_HEAD(head, next); | |
98 | ||
99 | /* | |
d73415a3 | 100 | * The qatomic_and is paired with aio_bh_enqueue(). The implicit memory |
8c6b0356 SH |
101 | * barrier ensures that the callback sees all writes done by the scheduling |
102 | * thread. It also ensures that the scheduling thread sees the cleared | |
103 | * flag before bh->cb has run, and thus will call aio_notify again if | |
104 | * necessary. | |
105 | */ | |
d73415a3 | 106 | *flags = qatomic_fetch_and(&bh->flags, |
8c6b0356 SH |
107 | ~(BH_PENDING | BH_SCHEDULED | BH_IDLE)); |
108 | return bh; | |
109 | } | |
110 | ||
0f08586c SH |
111 | void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, |
112 | void *opaque, const char *name) | |
5b8bb359 PB |
113 | { |
114 | QEMUBH *bh; | |
115 | bh = g_new(QEMUBH, 1); | |
116 | *bh = (QEMUBH){ | |
117 | .ctx = ctx, | |
118 | .cb = cb, | |
119 | .opaque = opaque, | |
0f08586c | 120 | .name = name, |
5b8bb359 | 121 | }; |
8c6b0356 | 122 | aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT); |
5b8bb359 PB |
123 | } |
124 | ||
0f08586c SH |
125 | QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque, |
126 | const char *name) | |
4f999d05 KW |
127 | { |
128 | QEMUBH *bh; | |
ee82310f PB |
129 | bh = g_new(QEMUBH, 1); |
130 | *bh = (QEMUBH){ | |
131 | .ctx = ctx, | |
132 | .cb = cb, | |
133 | .opaque = opaque, | |
0f08586c | 134 | .name = name, |
ee82310f | 135 | }; |
4f999d05 KW |
136 | return bh; |
137 | } | |
138 | ||
df281b80 PD |
139 | void aio_bh_call(QEMUBH *bh) |
140 | { | |
141 | bh->cb(bh->opaque); | |
142 | } | |
143 | ||
8c6b0356 | 144 | /* Multiple occurrences of aio_bh_poll cannot be called concurrently. */ |
f627aab1 | 145 | int aio_bh_poll(AioContext *ctx) |
4f999d05 | 146 | { |
8c6b0356 SH |
147 | BHListSlice slice; |
148 | BHListSlice *s; | |
149 | int ret = 0; | |
150 | ||
151 | QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list); | |
152 | QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next); | |
153 | ||
154 | while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) { | |
155 | QEMUBH *bh; | |
156 | unsigned flags; | |
157 | ||
158 | bh = aio_bh_dequeue(&s->bh_list, &flags); | |
159 | if (!bh) { | |
160 | QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next); | |
161 | continue; | |
162 | } | |
163 | ||
164 | if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { | |
65c1b5b6 | 165 | /* Idle BHs don't count as progress */ |
8c6b0356 | 166 | if (!(flags & BH_IDLE)) { |
4f999d05 | 167 | ret = 1; |
ca96ac44 | 168 | } |
df281b80 | 169 | aio_bh_call(bh); |
4f999d05 | 170 | } |
8c6b0356 SH |
171 | if (flags & (BH_DELETED | BH_ONESHOT)) { |
172 | g_free(bh); | |
7d506c90 | 173 | } |
4f999d05 KW |
174 | } |
175 | ||
4f999d05 KW |
176 | return ret; |
177 | } | |
178 | ||
179 | void qemu_bh_schedule_idle(QEMUBH *bh) | |
180 | { | |
8c6b0356 | 181 | aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE); |
4f999d05 KW |
182 | } |
183 | ||
184 | void qemu_bh_schedule(QEMUBH *bh) | |
185 | { | |
8c6b0356 | 186 | aio_bh_enqueue(bh, BH_SCHEDULED); |
4f999d05 KW |
187 | } |
188 | ||
dcc772e2 LPF |
189 | /* This func is async. |
190 | */ | |
4f999d05 KW |
191 | void qemu_bh_cancel(QEMUBH *bh) |
192 | { | |
d73415a3 | 193 | qatomic_and(&bh->flags, ~BH_SCHEDULED); |
4f999d05 KW |
194 | } |
195 | ||
dcc772e2 LPF |
196 | /* This func is async.The bottom half will do the delete action at the finial |
197 | * end. | |
198 | */ | |
4f999d05 KW |
199 | void qemu_bh_delete(QEMUBH *bh) |
200 | { | |
8c6b0356 | 201 | aio_bh_enqueue(bh, BH_DELETED); |
4f999d05 KW |
202 | } |
203 | ||
8c6b0356 | 204 | static int64_t aio_compute_bh_timeout(BHList *head, int timeout) |
4f999d05 KW |
205 | { |
206 | QEMUBH *bh; | |
207 | ||
8c6b0356 SH |
208 | QSLIST_FOREACH_RCU(bh, head, next) { |
209 | if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { | |
210 | if (bh->flags & BH_IDLE) { | |
4f999d05 KW |
211 | /* idle bottom halves will be polled at least |
212 | * every 10ms */ | |
845ca10d | 213 | timeout = 10000000; |
4f999d05 KW |
214 | } else { |
215 | /* non-idle bottom halves will be executed | |
216 | * immediately */ | |
845ca10d | 217 | return 0; |
4f999d05 KW |
218 | } |
219 | } | |
220 | } | |
e3713e00 | 221 | |
8c6b0356 SH |
222 | return timeout; |
223 | } | |
224 | ||
225 | int64_t | |
226 | aio_compute_timeout(AioContext *ctx) | |
227 | { | |
228 | BHListSlice *s; | |
229 | int64_t deadline; | |
230 | int timeout = -1; | |
231 | ||
232 | timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout); | |
233 | if (timeout == 0) { | |
234 | return 0; | |
235 | } | |
236 | ||
237 | QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) { | |
238 | timeout = aio_compute_bh_timeout(&s->bh_list, timeout); | |
239 | if (timeout == 0) { | |
240 | return 0; | |
241 | } | |
242 | } | |
243 | ||
845ca10d | 244 | deadline = timerlistgroup_deadline_ns(&ctx->tlg); |
533a8cf3 | 245 | if (deadline == 0) { |
845ca10d | 246 | return 0; |
533a8cf3 | 247 | } else { |
845ca10d | 248 | return qemu_soonest_timeout(timeout, deadline); |
533a8cf3 | 249 | } |
845ca10d | 250 | } |
533a8cf3 | 251 | |
845ca10d PB |
252 | static gboolean |
253 | aio_ctx_prepare(GSource *source, gint *timeout) | |
254 | { | |
255 | AioContext *ctx = (AioContext *) source; | |
256 | ||
d73415a3 | 257 | qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) | 1); |
5710a3e0 PB |
258 | |
259 | /* | |
260 | * Write ctx->notify_me before computing the timeout | |
261 | * (reading bottom half flags, etc.). Pairs with | |
262 | * smp_mb in aio_notify(). | |
263 | */ | |
264 | smp_mb(); | |
eabc9779 | 265 | |
845ca10d PB |
266 | /* We assume there is no timeout already supplied */ |
267 | *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)); | |
a3462c65 PB |
268 | |
269 | if (aio_prepare(ctx)) { | |
270 | *timeout = 0; | |
271 | } | |
272 | ||
845ca10d | 273 | return *timeout == 0; |
e3713e00 PB |
274 | } |
275 | ||
276 | static gboolean | |
277 | aio_ctx_check(GSource *source) | |
278 | { | |
279 | AioContext *ctx = (AioContext *) source; | |
280 | QEMUBH *bh; | |
8c6b0356 | 281 | BHListSlice *s; |
e3713e00 | 282 | |
5710a3e0 | 283 | /* Finish computing the timeout before clearing the flag. */ |
d73415a3 | 284 | qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) & ~1); |
05e514b1 | 285 | aio_notify_accept(ctx); |
21a03d17 | 286 | |
8c6b0356 SH |
287 | QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) { |
288 | if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { | |
e3713e00 | 289 | return true; |
6977d901 | 290 | } |
e3713e00 | 291 | } |
8c6b0356 SH |
292 | |
293 | QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) { | |
294 | QSLIST_FOREACH_RCU(bh, &s->bh_list, next) { | |
295 | if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { | |
296 | return true; | |
297 | } | |
298 | } | |
299 | } | |
533a8cf3 | 300 | return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0); |
e3713e00 PB |
301 | } |
302 | ||
303 | static gboolean | |
304 | aio_ctx_dispatch(GSource *source, | |
305 | GSourceFunc callback, | |
306 | gpointer user_data) | |
307 | { | |
308 | AioContext *ctx = (AioContext *) source; | |
309 | ||
310 | assert(callback == NULL); | |
a153bf52 | 311 | aio_dispatch(ctx); |
e3713e00 PB |
312 | return true; |
313 | } | |
314 | ||
2f4dc3c1 PB |
315 | static void |
316 | aio_ctx_finalize(GSource *source) | |
317 | { | |
318 | AioContext *ctx = (AioContext *) source; | |
8c6b0356 SH |
319 | QEMUBH *bh; |
320 | unsigned flags; | |
2f4dc3c1 | 321 | |
9b34277d | 322 | thread_pool_free(ctx->thread_pool); |
a076972a | 323 | |
0187f5c9 PB |
324 | #ifdef CONFIG_LINUX_AIO |
325 | if (ctx->linux_aio) { | |
326 | laio_detach_aio_context(ctx->linux_aio, ctx); | |
327 | laio_cleanup(ctx->linux_aio); | |
328 | ctx->linux_aio = NULL; | |
329 | } | |
330 | #endif | |
331 | ||
fcb7a4a4 AM |
332 | #ifdef CONFIG_LINUX_IO_URING |
333 | if (ctx->linux_io_uring) { | |
334 | luring_detach_aio_context(ctx->linux_io_uring, ctx); | |
335 | luring_cleanup(ctx->linux_io_uring); | |
336 | ctx->linux_io_uring = NULL; | |
337 | } | |
338 | #endif | |
339 | ||
0c330a73 PB |
340 | assert(QSLIST_EMPTY(&ctx->scheduled_coroutines)); |
341 | qemu_bh_delete(ctx->co_schedule_bh); | |
342 | ||
8c6b0356 SH |
343 | /* There must be no aio_bh_poll() calls going on */ |
344 | assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list)); | |
a076972a | 345 | |
8c6b0356 | 346 | while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) { |
023ca420 SH |
347 | /* |
348 | * qemu_bh_delete() must have been called on BHs in this AioContext. In | |
349 | * many cases memory leaks, hangs, or inconsistent state occur when a | |
350 | * BH is leaked because something still expects it to run. | |
351 | * | |
352 | * If you hit this, fix the lifecycle of the BH so that | |
353 | * qemu_bh_delete() and any associated cleanup is called before the | |
354 | * AioContext is finalized. | |
355 | */ | |
356 | if (unlikely(!(flags & BH_DELETED))) { | |
357 | fprintf(stderr, "%s: BH '%s' leaked, aborting...\n", | |
358 | __func__, bh->name); | |
359 | abort(); | |
360 | } | |
a076972a | 361 | |
8c6b0356 | 362 | g_free(bh); |
a076972a | 363 | } |
a076972a | 364 | |
f6a51c84 | 365 | aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL); |
2f4dc3c1 | 366 | event_notifier_cleanup(&ctx->notifier); |
3fe71223 | 367 | qemu_rec_mutex_destroy(&ctx->lock); |
d7c99a12 | 368 | qemu_lockcnt_destroy(&ctx->list_lock); |
dae21b98 | 369 | timerlistgroup_deinit(&ctx->tlg); |
cd0a6d2b | 370 | aio_context_destroy(ctx); |
2f4dc3c1 PB |
371 | } |
372 | ||
e3713e00 PB |
373 | static GSourceFuncs aio_source_funcs = { |
374 | aio_ctx_prepare, | |
375 | aio_ctx_check, | |
376 | aio_ctx_dispatch, | |
2f4dc3c1 | 377 | aio_ctx_finalize |
e3713e00 PB |
378 | }; |
379 | ||
380 | GSource *aio_get_g_source(AioContext *ctx) | |
381 | { | |
ba607ca8 | 382 | aio_context_use_g_source(ctx); |
e3713e00 PB |
383 | g_source_ref(&ctx->source); |
384 | return &ctx->source; | |
385 | } | |
a915f4bc | 386 | |
9b34277d SH |
387 | ThreadPool *aio_get_thread_pool(AioContext *ctx) |
388 | { | |
389 | if (!ctx->thread_pool) { | |
390 | ctx->thread_pool = thread_pool_new(ctx); | |
391 | } | |
392 | return ctx->thread_pool; | |
393 | } | |
394 | ||
0187f5c9 | 395 | #ifdef CONFIG_LINUX_AIO |
ed6e2161 | 396 | LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp) |
0187f5c9 PB |
397 | { |
398 | if (!ctx->linux_aio) { | |
ed6e2161 NA |
399 | ctx->linux_aio = laio_init(errp); |
400 | if (ctx->linux_aio) { | |
401 | laio_attach_aio_context(ctx->linux_aio, ctx); | |
402 | } | |
0187f5c9 PB |
403 | } |
404 | return ctx->linux_aio; | |
405 | } | |
ed6e2161 NA |
406 | |
407 | LinuxAioState *aio_get_linux_aio(AioContext *ctx) | |
408 | { | |
409 | assert(ctx->linux_aio); | |
410 | return ctx->linux_aio; | |
411 | } | |
0187f5c9 PB |
412 | #endif |
413 | ||
fcb7a4a4 AM |
414 | #ifdef CONFIG_LINUX_IO_URING |
415 | LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp) | |
416 | { | |
417 | if (ctx->linux_io_uring) { | |
418 | return ctx->linux_io_uring; | |
419 | } | |
420 | ||
421 | ctx->linux_io_uring = luring_init(errp); | |
422 | if (!ctx->linux_io_uring) { | |
423 | return NULL; | |
424 | } | |
425 | ||
426 | luring_attach_aio_context(ctx->linux_io_uring, ctx); | |
427 | return ctx->linux_io_uring; | |
428 | } | |
429 | ||
430 | LuringState *aio_get_linux_io_uring(AioContext *ctx) | |
431 | { | |
432 | assert(ctx->linux_io_uring); | |
433 | return ctx->linux_io_uring; | |
434 | } | |
435 | #endif | |
436 | ||
2f4dc3c1 PB |
437 | void aio_notify(AioContext *ctx) |
438 | { | |
601829f8 SH |
439 | /* |
440 | * Write e.g. bh->flags before writing ctx->notified. Pairs with smp_mb in | |
441 | * aio_notify_accept. | |
442 | */ | |
443 | smp_wmb(); | |
d73415a3 | 444 | qatomic_set(&ctx->notified, true); |
601829f8 SH |
445 | |
446 | /* | |
447 | * Write ctx->notified before reading ctx->notify_me. Pairs | |
5710a3e0 | 448 | * with smp_mb in aio_ctx_prepare or aio_poll. |
eabc9779 | 449 | */ |
0ceb849b | 450 | smp_mb(); |
d73415a3 | 451 | if (qatomic_read(&ctx->notify_me)) { |
0ceb849b | 452 | event_notifier_set(&ctx->notifier); |
05e514b1 PB |
453 | } |
454 | } | |
455 | ||
456 | void aio_notify_accept(AioContext *ctx) | |
457 | { | |
d73415a3 | 458 | qatomic_set(&ctx->notified, false); |
601829f8 SH |
459 | |
460 | /* | |
461 | * Write ctx->notified before reading e.g. bh->flags. Pairs with smp_wmb | |
462 | * in aio_notify. | |
463 | */ | |
464 | smp_mb(); | |
2f4dc3c1 PB |
465 | } |
466 | ||
3f53bc61 | 467 | static void aio_timerlist_notify(void *opaque, QEMUClockType type) |
d5541d86 AB |
468 | { |
469 | aio_notify(opaque); | |
470 | } | |
471 | ||
601829f8 | 472 | static void aio_context_notifier_cb(EventNotifier *e) |
21a03d17 | 473 | { |
601829f8 SH |
474 | AioContext *ctx = container_of(e, AioContext, notifier); |
475 | ||
476 | event_notifier_test_and_clear(&ctx->notifier); | |
21a03d17 PB |
477 | } |
478 | ||
4a1cba38 | 479 | /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */ |
c13be5a1 | 480 | static bool aio_context_notifier_poll(void *opaque) |
4a1cba38 SH |
481 | { |
482 | EventNotifier *e = opaque; | |
483 | AioContext *ctx = container_of(e, AioContext, notifier); | |
484 | ||
d73415a3 | 485 | return qatomic_read(&ctx->notified); |
4a1cba38 SH |
486 | } |
487 | ||
0c330a73 PB |
488 | static void co_schedule_bh_cb(void *opaque) |
489 | { | |
490 | AioContext *ctx = opaque; | |
491 | QSLIST_HEAD(, Coroutine) straight, reversed; | |
492 | ||
493 | QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines); | |
494 | QSLIST_INIT(&straight); | |
495 | ||
496 | while (!QSLIST_EMPTY(&reversed)) { | |
497 | Coroutine *co = QSLIST_FIRST(&reversed); | |
498 | QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next); | |
499 | QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next); | |
500 | } | |
501 | ||
502 | while (!QSLIST_EMPTY(&straight)) { | |
503 | Coroutine *co = QSLIST_FIRST(&straight); | |
504 | QSLIST_REMOVE_HEAD(&straight, co_scheduled_next); | |
505 | trace_aio_co_schedule_bh_cb(ctx, co); | |
1919631e | 506 | aio_context_acquire(ctx); |
6133b39f JC |
507 | |
508 | /* Protected by write barrier in qemu_aio_coroutine_enter */ | |
d73415a3 | 509 | qatomic_set(&co->scheduled, NULL); |
6808ae04 | 510 | qemu_aio_coroutine_enter(ctx, co); |
1919631e | 511 | aio_context_release(ctx); |
0c330a73 PB |
512 | } |
513 | } | |
514 | ||
2f78e491 | 515 | AioContext *aio_context_new(Error **errp) |
f627aab1 | 516 | { |
2f78e491 | 517 | int ret; |
2f4dc3c1 | 518 | AioContext *ctx; |
37fcee5d | 519 | |
2f4dc3c1 | 520 | ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext)); |
8c6b0356 SH |
521 | QSLIST_INIT(&ctx->bh_list); |
522 | QSIMPLEQ_INIT(&ctx->bh_slice_list); | |
7e003465 C |
523 | aio_context_setup(ctx); |
524 | ||
2f78e491 CN |
525 | ret = event_notifier_init(&ctx->notifier, false); |
526 | if (ret < 0) { | |
2f78e491 | 527 | error_setg_errno(errp, -ret, "Failed to initialize event notifier"); |
37fcee5d | 528 | goto fail; |
2f78e491 | 529 | } |
fcf5def1 | 530 | g_source_set_can_recurse(&ctx->source, true); |
d7c99a12 | 531 | qemu_lockcnt_init(&ctx->list_lock); |
0c330a73 PB |
532 | |
533 | ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx); | |
534 | QSLIST_INIT(&ctx->scheduled_coroutines); | |
535 | ||
2f78e491 | 536 | aio_set_event_notifier(ctx, &ctx->notifier, |
dca21ef2 | 537 | false, |
601829f8 | 538 | aio_context_notifier_cb, |
c13be5a1 | 539 | aio_context_notifier_poll); |
0187f5c9 PB |
540 | #ifdef CONFIG_LINUX_AIO |
541 | ctx->linux_aio = NULL; | |
542 | #endif | |
fcb7a4a4 AM |
543 | |
544 | #ifdef CONFIG_LINUX_IO_URING | |
545 | ctx->linux_io_uring = NULL; | |
546 | #endif | |
547 | ||
9b34277d | 548 | ctx->thread_pool = NULL; |
3fe71223 | 549 | qemu_rec_mutex_init(&ctx->lock); |
d5541d86 | 550 | timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx); |
2f4dc3c1 | 551 | |
82a41186 | 552 | ctx->poll_ns = 0; |
4a1cba38 | 553 | ctx->poll_max_ns = 0; |
82a41186 SH |
554 | ctx->poll_grow = 0; |
555 | ctx->poll_shrink = 0; | |
4a1cba38 | 556 | |
2f4dc3c1 | 557 | return ctx; |
37fcee5d FZ |
558 | fail: |
559 | g_source_destroy(&ctx->source); | |
560 | return NULL; | |
e3713e00 PB |
561 | } |
562 | ||
0c330a73 PB |
563 | void aio_co_schedule(AioContext *ctx, Coroutine *co) |
564 | { | |
565 | trace_aio_co_schedule(ctx, co); | |
d73415a3 | 566 | const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL, |
6133b39f JC |
567 | __func__); |
568 | ||
569 | if (scheduled) { | |
570 | fprintf(stderr, | |
571 | "%s: Co-routine was already scheduled in '%s'\n", | |
572 | __func__, scheduled); | |
573 | abort(); | |
574 | } | |
575 | ||
f0f81002 SH |
576 | /* The coroutine might run and release the last ctx reference before we |
577 | * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until | |
578 | * we're done. | |
579 | */ | |
580 | aio_context_ref(ctx); | |
581 | ||
0c330a73 PB |
582 | QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines, |
583 | co, co_scheduled_next); | |
584 | qemu_bh_schedule(ctx->co_schedule_bh); | |
f0f81002 SH |
585 | |
586 | aio_context_unref(ctx); | |
0c330a73 PB |
587 | } |
588 | ||
26b0b698 KW |
589 | typedef struct AioCoRescheduleSelf { |
590 | Coroutine *co; | |
591 | AioContext *new_ctx; | |
592 | } AioCoRescheduleSelf; | |
593 | ||
594 | static void aio_co_reschedule_self_bh(void *opaque) | |
595 | { | |
596 | AioCoRescheduleSelf *data = opaque; | |
597 | aio_co_schedule(data->new_ctx, data->co); | |
598 | } | |
599 | ||
600 | void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx) | |
601 | { | |
602 | AioContext *old_ctx = qemu_get_current_aio_context(); | |
603 | ||
604 | if (old_ctx != new_ctx) { | |
605 | AioCoRescheduleSelf data = { | |
606 | .co = qemu_coroutine_self(), | |
607 | .new_ctx = new_ctx, | |
608 | }; | |
609 | /* | |
610 | * We can't directly schedule the coroutine in the target context | |
611 | * because this would be racy: The other thread could try to enter the | |
612 | * coroutine before it has yielded in this one. | |
613 | */ | |
614 | aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data); | |
615 | qemu_coroutine_yield(); | |
616 | } | |
617 | } | |
618 | ||
0c330a73 PB |
619 | void aio_co_wake(struct Coroutine *co) |
620 | { | |
621 | AioContext *ctx; | |
622 | ||
623 | /* Read coroutine before co->ctx. Matches smp_wmb in | |
624 | * qemu_coroutine_enter. | |
625 | */ | |
626 | smp_read_barrier_depends(); | |
d73415a3 | 627 | ctx = qatomic_read(&co->ctx); |
0c330a73 | 628 | |
8865852e FZ |
629 | aio_co_enter(ctx, co); |
630 | } | |
631 | ||
632 | void aio_co_enter(AioContext *ctx, struct Coroutine *co) | |
633 | { | |
0c330a73 PB |
634 | if (ctx != qemu_get_current_aio_context()) { |
635 | aio_co_schedule(ctx, co); | |
636 | return; | |
637 | } | |
638 | ||
639 | if (qemu_in_coroutine()) { | |
640 | Coroutine *self = qemu_coroutine_self(); | |
641 | assert(self != co); | |
642 | QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next); | |
643 | } else { | |
644 | aio_context_acquire(ctx); | |
8865852e | 645 | qemu_aio_coroutine_enter(ctx, co); |
0c330a73 PB |
646 | aio_context_release(ctx); |
647 | } | |
648 | } | |
649 | ||
e3713e00 PB |
650 | void aio_context_ref(AioContext *ctx) |
651 | { | |
652 | g_source_ref(&ctx->source); | |
653 | } | |
654 | ||
655 | void aio_context_unref(AioContext *ctx) | |
656 | { | |
657 | g_source_unref(&ctx->source); | |
f627aab1 | 658 | } |
98563fc3 SH |
659 | |
660 | void aio_context_acquire(AioContext *ctx) | |
661 | { | |
3fe71223 | 662 | qemu_rec_mutex_lock(&ctx->lock); |
98563fc3 SH |
663 | } |
664 | ||
665 | void aio_context_release(AioContext *ctx) | |
666 | { | |
3fe71223 | 667 | qemu_rec_mutex_unlock(&ctx->lock); |
98563fc3 | 668 | } |
5f50be9b PB |
669 | |
670 | static __thread AioContext *my_aiocontext; | |
671 | ||
672 | AioContext *qemu_get_current_aio_context(void) | |
673 | { | |
674 | if (my_aiocontext) { | |
675 | return my_aiocontext; | |
676 | } | |
677 | if (qemu_mutex_iothread_locked()) { | |
678 | /* Possibly in a vCPU thread. */ | |
679 | return qemu_get_aio_context(); | |
680 | } | |
681 | return NULL; | |
682 | } | |
683 | ||
684 | void qemu_set_current_aio_context(AioContext *ctx) | |
685 | { | |
686 | assert(!my_aiocontext); | |
687 | my_aiocontext = ctx; | |
688 | } |