pcie: implement slot power control for pcie root ports
[qemu/kevin.git] / util / async.c
blob6f6717a34b6318ae0405d5f7f44c1d0a68339339
1 /*
2 * Data plane event loop
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009-2017 QEMU contributors
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "block/aio.h"
29 #include "block/thread-pool.h"
30 #include "qemu/main-loop.h"
31 #include "qemu/atomic.h"
32 #include "qemu/rcu_queue.h"
33 #include "block/raw-aio.h"
34 #include "qemu/coroutine_int.h"
35 #include "trace.h"
37 /***********************************************************/
38 /* bottom halves (can be seen as timers which expire ASAP) */
40 /* QEMUBH::flags values */
41 enum {
42 /* Already enqueued and waiting for aio_bh_poll() */
43 BH_PENDING = (1 << 0),
45 /* Invoke the callback */
46 BH_SCHEDULED = (1 << 1),
48 /* Delete without invoking callback */
49 BH_DELETED = (1 << 2),
51 /* Delete after invoking callback */
52 BH_ONESHOT = (1 << 3),
54 /* Schedule periodically when the event loop is idle */
55 BH_IDLE = (1 << 4),
58 struct QEMUBH {
59 AioContext *ctx;
60 const char *name;
61 QEMUBHFunc *cb;
62 void *opaque;
63 QSLIST_ENTRY(QEMUBH) next;
64 unsigned flags;
67 /* Called concurrently from any thread */
68 static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
70 AioContext *ctx = bh->ctx;
71 unsigned old_flags;
74 * The memory barrier implicit in qatomic_fetch_or makes sure that:
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.
80 old_flags = qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags);
81 if (!(old_flags & BH_PENDING)) {
82 QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next);
85 aio_notify(ctx);
88 /* Only called from aio_bh_poll() and aio_ctx_finalize() */
89 static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags)
91 QEMUBH *bh = QSLIST_FIRST_RCU(head);
93 if (!bh) {
94 return NULL;
97 QSLIST_REMOVE_HEAD(head, next);
100 * The qatomic_and is paired with aio_bh_enqueue(). The implicit memory
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.
106 *flags = qatomic_fetch_and(&bh->flags,
107 ~(BH_PENDING | BH_SCHEDULED | BH_IDLE));
108 return bh;
111 void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb,
112 void *opaque, const char *name)
114 QEMUBH *bh;
115 bh = g_new(QEMUBH, 1);
116 *bh = (QEMUBH){
117 .ctx = ctx,
118 .cb = cb,
119 .opaque = opaque,
120 .name = name,
122 aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT);
125 QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
126 const char *name)
128 QEMUBH *bh;
129 bh = g_new(QEMUBH, 1);
130 *bh = (QEMUBH){
131 .ctx = ctx,
132 .cb = cb,
133 .opaque = opaque,
134 .name = name,
136 return bh;
139 void aio_bh_call(QEMUBH *bh)
141 bh->cb(bh->opaque);
144 /* Multiple occurrences of aio_bh_poll cannot be called concurrently. */
145 int aio_bh_poll(AioContext *ctx)
147 BHListSlice slice;
148 BHListSlice *s;
149 int ret = 0;
151 QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list);
152 QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next);
154 while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) {
155 QEMUBH *bh;
156 unsigned flags;
158 bh = aio_bh_dequeue(&s->bh_list, &flags);
159 if (!bh) {
160 QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next);
161 continue;
164 if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
165 /* Idle BHs don't count as progress */
166 if (!(flags & BH_IDLE)) {
167 ret = 1;
169 aio_bh_call(bh);
171 if (flags & (BH_DELETED | BH_ONESHOT)) {
172 g_free(bh);
176 return ret;
179 void qemu_bh_schedule_idle(QEMUBH *bh)
181 aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE);
184 void qemu_bh_schedule(QEMUBH *bh)
186 aio_bh_enqueue(bh, BH_SCHEDULED);
189 /* This func is async.
191 void qemu_bh_cancel(QEMUBH *bh)
193 qatomic_and(&bh->flags, ~BH_SCHEDULED);
196 /* This func is async.The bottom half will do the delete action at the finial
197 * end.
199 void qemu_bh_delete(QEMUBH *bh)
201 aio_bh_enqueue(bh, BH_DELETED);
204 static int64_t aio_compute_bh_timeout(BHList *head, int timeout)
206 QEMUBH *bh;
208 QSLIST_FOREACH_RCU(bh, head, next) {
209 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
210 if (bh->flags & BH_IDLE) {
211 /* idle bottom halves will be polled at least
212 * every 10ms */
213 timeout = 10000000;
214 } else {
215 /* non-idle bottom halves will be executed
216 * immediately */
217 return 0;
222 return timeout;
225 int64_t
226 aio_compute_timeout(AioContext *ctx)
228 BHListSlice *s;
229 int64_t deadline;
230 int timeout = -1;
232 timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout);
233 if (timeout == 0) {
234 return 0;
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;
244 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
245 if (deadline == 0) {
246 return 0;
247 } else {
248 return qemu_soonest_timeout(timeout, deadline);
252 static gboolean
253 aio_ctx_prepare(GSource *source, gint *timeout)
255 AioContext *ctx = (AioContext *) source;
257 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) | 1);
260 * Write ctx->notify_me before computing the timeout
261 * (reading bottom half flags, etc.). Pairs with
262 * smp_mb in aio_notify().
264 smp_mb();
266 /* We assume there is no timeout already supplied */
267 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
269 if (aio_prepare(ctx)) {
270 *timeout = 0;
273 return *timeout == 0;
276 static gboolean
277 aio_ctx_check(GSource *source)
279 AioContext *ctx = (AioContext *) source;
280 QEMUBH *bh;
281 BHListSlice *s;
283 /* Finish computing the timeout before clearing the flag. */
284 qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) & ~1);
285 aio_notify_accept(ctx);
287 QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
288 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
289 return true;
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;
300 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
303 static gboolean
304 aio_ctx_dispatch(GSource *source,
305 GSourceFunc callback,
306 gpointer user_data)
308 AioContext *ctx = (AioContext *) source;
310 assert(callback == NULL);
311 aio_dispatch(ctx);
312 return true;
315 static void
316 aio_ctx_finalize(GSource *source)
318 AioContext *ctx = (AioContext *) source;
319 QEMUBH *bh;
320 unsigned flags;
322 thread_pool_free(ctx->thread_pool);
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;
330 #endif
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;
338 #endif
340 assert(QSLIST_EMPTY(&ctx->scheduled_coroutines));
341 qemu_bh_delete(ctx->co_schedule_bh);
343 /* There must be no aio_bh_poll() calls going on */
344 assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list));
346 while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) {
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.
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.
356 if (unlikely(!(flags & BH_DELETED))) {
357 fprintf(stderr, "%s: BH '%s' leaked, aborting...\n",
358 __func__, bh->name);
359 abort();
362 g_free(bh);
365 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL);
366 event_notifier_cleanup(&ctx->notifier);
367 qemu_rec_mutex_destroy(&ctx->lock);
368 qemu_lockcnt_destroy(&ctx->list_lock);
369 timerlistgroup_deinit(&ctx->tlg);
370 aio_context_destroy(ctx);
373 static GSourceFuncs aio_source_funcs = {
374 aio_ctx_prepare,
375 aio_ctx_check,
376 aio_ctx_dispatch,
377 aio_ctx_finalize
380 GSource *aio_get_g_source(AioContext *ctx)
382 aio_context_use_g_source(ctx);
383 g_source_ref(&ctx->source);
384 return &ctx->source;
387 ThreadPool *aio_get_thread_pool(AioContext *ctx)
389 if (!ctx->thread_pool) {
390 ctx->thread_pool = thread_pool_new(ctx);
392 return ctx->thread_pool;
395 #ifdef CONFIG_LINUX_AIO
396 LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp)
398 if (!ctx->linux_aio) {
399 ctx->linux_aio = laio_init(errp);
400 if (ctx->linux_aio) {
401 laio_attach_aio_context(ctx->linux_aio, ctx);
404 return ctx->linux_aio;
407 LinuxAioState *aio_get_linux_aio(AioContext *ctx)
409 assert(ctx->linux_aio);
410 return ctx->linux_aio;
412 #endif
414 #ifdef CONFIG_LINUX_IO_URING
415 LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp)
417 if (ctx->linux_io_uring) {
418 return ctx->linux_io_uring;
421 ctx->linux_io_uring = luring_init(errp);
422 if (!ctx->linux_io_uring) {
423 return NULL;
426 luring_attach_aio_context(ctx->linux_io_uring, ctx);
427 return ctx->linux_io_uring;
430 LuringState *aio_get_linux_io_uring(AioContext *ctx)
432 assert(ctx->linux_io_uring);
433 return ctx->linux_io_uring;
435 #endif
437 void aio_notify(AioContext *ctx)
440 * Write e.g. bh->flags before writing ctx->notified. Pairs with smp_mb in
441 * aio_notify_accept.
443 smp_wmb();
444 qatomic_set(&ctx->notified, true);
447 * Write ctx->notified before reading ctx->notify_me. Pairs
448 * with smp_mb in aio_ctx_prepare or aio_poll.
450 smp_mb();
451 if (qatomic_read(&ctx->notify_me)) {
452 event_notifier_set(&ctx->notifier);
456 void aio_notify_accept(AioContext *ctx)
458 qatomic_set(&ctx->notified, false);
461 * Write ctx->notified before reading e.g. bh->flags. Pairs with smp_wmb
462 * in aio_notify.
464 smp_mb();
467 static void aio_timerlist_notify(void *opaque, QEMUClockType type)
469 aio_notify(opaque);
472 static void aio_context_notifier_cb(EventNotifier *e)
474 AioContext *ctx = container_of(e, AioContext, notifier);
476 event_notifier_test_and_clear(&ctx->notifier);
479 /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */
480 static bool aio_context_notifier_poll(void *opaque)
482 EventNotifier *e = opaque;
483 AioContext *ctx = container_of(e, AioContext, notifier);
485 return qatomic_read(&ctx->notified);
488 static void co_schedule_bh_cb(void *opaque)
490 AioContext *ctx = opaque;
491 QSLIST_HEAD(, Coroutine) straight, reversed;
493 QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines);
494 QSLIST_INIT(&straight);
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);
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);
506 aio_context_acquire(ctx);
508 /* Protected by write barrier in qemu_aio_coroutine_enter */
509 qatomic_set(&co->scheduled, NULL);
510 qemu_aio_coroutine_enter(ctx, co);
511 aio_context_release(ctx);
515 AioContext *aio_context_new(Error **errp)
517 int ret;
518 AioContext *ctx;
520 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
521 QSLIST_INIT(&ctx->bh_list);
522 QSIMPLEQ_INIT(&ctx->bh_slice_list);
523 aio_context_setup(ctx);
525 ret = event_notifier_init(&ctx->notifier, false);
526 if (ret < 0) {
527 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
528 goto fail;
530 g_source_set_can_recurse(&ctx->source, true);
531 qemu_lockcnt_init(&ctx->list_lock);
533 ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx);
534 QSLIST_INIT(&ctx->scheduled_coroutines);
536 aio_set_event_notifier(ctx, &ctx->notifier,
537 false,
538 aio_context_notifier_cb,
539 aio_context_notifier_poll);
540 #ifdef CONFIG_LINUX_AIO
541 ctx->linux_aio = NULL;
542 #endif
544 #ifdef CONFIG_LINUX_IO_URING
545 ctx->linux_io_uring = NULL;
546 #endif
548 ctx->thread_pool = NULL;
549 qemu_rec_mutex_init(&ctx->lock);
550 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
552 ctx->poll_ns = 0;
553 ctx->poll_max_ns = 0;
554 ctx->poll_grow = 0;
555 ctx->poll_shrink = 0;
557 ctx->aio_max_batch = 0;
559 return ctx;
560 fail:
561 g_source_destroy(&ctx->source);
562 return NULL;
565 void aio_co_schedule(AioContext *ctx, Coroutine *co)
567 trace_aio_co_schedule(ctx, co);
568 const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
569 __func__);
571 if (scheduled) {
572 fprintf(stderr,
573 "%s: Co-routine was already scheduled in '%s'\n",
574 __func__, scheduled);
575 abort();
578 /* The coroutine might run and release the last ctx reference before we
579 * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until
580 * we're done.
582 aio_context_ref(ctx);
584 QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
585 co, co_scheduled_next);
586 qemu_bh_schedule(ctx->co_schedule_bh);
588 aio_context_unref(ctx);
591 typedef struct AioCoRescheduleSelf {
592 Coroutine *co;
593 AioContext *new_ctx;
594 } AioCoRescheduleSelf;
596 static void aio_co_reschedule_self_bh(void *opaque)
598 AioCoRescheduleSelf *data = opaque;
599 aio_co_schedule(data->new_ctx, data->co);
602 void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx)
604 AioContext *old_ctx = qemu_get_current_aio_context();
606 if (old_ctx != new_ctx) {
607 AioCoRescheduleSelf data = {
608 .co = qemu_coroutine_self(),
609 .new_ctx = new_ctx,
612 * We can't directly schedule the coroutine in the target context
613 * because this would be racy: The other thread could try to enter the
614 * coroutine before it has yielded in this one.
616 aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data);
617 qemu_coroutine_yield();
621 void aio_co_wake(struct Coroutine *co)
623 AioContext *ctx;
625 /* Read coroutine before co->ctx. Matches smp_wmb in
626 * qemu_coroutine_enter.
628 smp_read_barrier_depends();
629 ctx = qatomic_read(&co->ctx);
631 aio_co_enter(ctx, co);
634 void aio_co_enter(AioContext *ctx, struct Coroutine *co)
636 if (ctx != qemu_get_current_aio_context()) {
637 aio_co_schedule(ctx, co);
638 return;
641 if (qemu_in_coroutine()) {
642 Coroutine *self = qemu_coroutine_self();
643 assert(self != co);
644 QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next);
645 } else {
646 aio_context_acquire(ctx);
647 qemu_aio_coroutine_enter(ctx, co);
648 aio_context_release(ctx);
652 void aio_context_ref(AioContext *ctx)
654 g_source_ref(&ctx->source);
657 void aio_context_unref(AioContext *ctx)
659 g_source_unref(&ctx->source);
662 void aio_context_acquire(AioContext *ctx)
664 qemu_rec_mutex_lock(&ctx->lock);
667 void aio_context_release(AioContext *ctx)
669 qemu_rec_mutex_unlock(&ctx->lock);
672 static __thread AioContext *my_aiocontext;
674 AioContext *qemu_get_current_aio_context(void)
676 if (my_aiocontext) {
677 return my_aiocontext;
679 if (qemu_mutex_iothread_locked()) {
680 /* Possibly in a vCPU thread. */
681 return qemu_get_aio_context();
683 return NULL;
686 void qemu_set_current_aio_context(AioContext *ctx)
688 assert(!my_aiocontext);
689 my_aiocontext = ctx;