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