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
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 "block/raw-aio.h"
33 #include "qemu/coroutine_int.h"
36 /***********************************************************/
37 /* bottom halves (can be seen as timers which expire ASAP) */
49 void aio_bh_schedule_oneshot(AioContext
*ctx
, QEMUBHFunc
*cb
, void *opaque
)
52 bh
= g_new(QEMUBH
, 1);
58 qemu_lockcnt_lock(&ctx
->list_lock
);
59 bh
->next
= ctx
->first_bh
;
62 /* Make sure that the members are ready before putting bh into list */
65 qemu_lockcnt_unlock(&ctx
->list_lock
);
69 QEMUBH
*aio_bh_new(AioContext
*ctx
, QEMUBHFunc
*cb
, void *opaque
)
72 bh
= g_new(QEMUBH
, 1);
78 qemu_lockcnt_lock(&ctx
->list_lock
);
79 bh
->next
= ctx
->first_bh
;
80 /* Make sure that the members are ready before putting bh into list */
83 qemu_lockcnt_unlock(&ctx
->list_lock
);
87 void aio_bh_call(QEMUBH
*bh
)
92 /* Multiple occurrences of aio_bh_poll cannot be called concurrently.
93 * The count in ctx->list_lock is incremented before the call, and is
94 * not affected by the call.
96 int aio_bh_poll(AioContext
*ctx
)
98 QEMUBH
*bh
, **bhp
, *next
;
100 bool deleted
= false;
103 for (bh
= atomic_rcu_read(&ctx
->first_bh
); bh
; bh
= next
) {
104 next
= atomic_rcu_read(&bh
->next
);
105 /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
106 * implicit memory barrier ensures that the callback sees all writes
107 * done by the scheduling thread. It also ensures that the scheduling
108 * thread sees the zero before bh->cb has run, and thus will call
109 * aio_notify again if necessary.
111 if (atomic_xchg(&bh
->scheduled
, 0)) {
112 /* Idle BHs don't count as progress */
124 /* remove deleted bhs */
129 if (qemu_lockcnt_dec_if_lock(&ctx
->list_lock
)) {
130 bhp
= &ctx
->first_bh
;
133 if (bh
->deleted
&& !bh
->scheduled
) {
140 qemu_lockcnt_inc_and_unlock(&ctx
->list_lock
);
145 void qemu_bh_schedule_idle(QEMUBH
*bh
)
148 /* Make sure that idle & any writes needed by the callback are done
149 * before the locations are read in the aio_bh_poll.
151 atomic_mb_set(&bh
->scheduled
, 1);
154 void qemu_bh_schedule(QEMUBH
*bh
)
160 /* The memory barrier implicit in atomic_xchg makes sure that:
161 * 1. idle & any writes needed by the callback are done before the
162 * locations are read in the aio_bh_poll.
163 * 2. ctx is loaded before scheduled is set and the callback has a chance
166 if (atomic_xchg(&bh
->scheduled
, 1) == 0) {
172 /* This func is async.
174 void qemu_bh_cancel(QEMUBH
*bh
)
176 atomic_mb_set(&bh
->scheduled
, 0);
179 /* This func is async.The bottom half will do the delete action at the finial
182 void qemu_bh_delete(QEMUBH
*bh
)
189 aio_compute_timeout(AioContext
*ctx
)
195 for (bh
= atomic_rcu_read(&ctx
->first_bh
); bh
;
196 bh
= atomic_rcu_read(&bh
->next
)) {
199 /* idle bottom halves will be polled at least
203 /* non-idle bottom halves will be executed
210 deadline
= timerlistgroup_deadline_ns(&ctx
->tlg
);
214 return qemu_soonest_timeout(timeout
, deadline
);
219 aio_ctx_prepare(GSource
*source
, gint
*timeout
)
221 AioContext
*ctx
= (AioContext
*) source
;
223 atomic_or(&ctx
->notify_me
, 1);
225 /* We assume there is no timeout already supplied */
226 *timeout
= qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
));
228 if (aio_prepare(ctx
)) {
232 return *timeout
== 0;
236 aio_ctx_check(GSource
*source
)
238 AioContext
*ctx
= (AioContext
*) source
;
241 atomic_and(&ctx
->notify_me
, ~1);
242 aio_notify_accept(ctx
);
244 for (bh
= ctx
->first_bh
; bh
; bh
= bh
->next
) {
249 return aio_pending(ctx
) || (timerlistgroup_deadline_ns(&ctx
->tlg
) == 0);
253 aio_ctx_dispatch(GSource
*source
,
254 GSourceFunc callback
,
257 AioContext
*ctx
= (AioContext
*) source
;
259 assert(callback
== NULL
);
265 aio_ctx_finalize(GSource
*source
)
267 AioContext
*ctx
= (AioContext
*) source
;
269 thread_pool_free(ctx
->thread_pool
);
271 #ifdef CONFIG_LINUX_AIO
272 if (ctx
->linux_aio
) {
273 laio_detach_aio_context(ctx
->linux_aio
, ctx
);
274 laio_cleanup(ctx
->linux_aio
);
275 ctx
->linux_aio
= NULL
;
279 assert(QSLIST_EMPTY(&ctx
->scheduled_coroutines
));
280 qemu_bh_delete(ctx
->co_schedule_bh
);
282 qemu_lockcnt_lock(&ctx
->list_lock
);
283 assert(!qemu_lockcnt_count(&ctx
->list_lock
));
284 while (ctx
->first_bh
) {
285 QEMUBH
*next
= ctx
->first_bh
->next
;
287 /* qemu_bh_delete() must have been called on BHs in this AioContext */
288 assert(ctx
->first_bh
->deleted
);
290 g_free(ctx
->first_bh
);
291 ctx
->first_bh
= next
;
293 qemu_lockcnt_unlock(&ctx
->list_lock
);
295 aio_set_event_notifier(ctx
, &ctx
->notifier
, false, NULL
, NULL
);
296 event_notifier_cleanup(&ctx
->notifier
);
297 qemu_rec_mutex_destroy(&ctx
->lock
);
298 qemu_lockcnt_destroy(&ctx
->list_lock
);
299 timerlistgroup_deinit(&ctx
->tlg
);
300 aio_context_destroy(ctx
);
303 static GSourceFuncs aio_source_funcs
= {
310 GSource
*aio_get_g_source(AioContext
*ctx
)
312 g_source_ref(&ctx
->source
);
316 ThreadPool
*aio_get_thread_pool(AioContext
*ctx
)
318 if (!ctx
->thread_pool
) {
319 ctx
->thread_pool
= thread_pool_new(ctx
);
321 return ctx
->thread_pool
;
324 #ifdef CONFIG_LINUX_AIO
325 LinuxAioState
*aio_setup_linux_aio(AioContext
*ctx
, Error
**errp
)
327 if (!ctx
->linux_aio
) {
328 ctx
->linux_aio
= laio_init(errp
);
329 if (ctx
->linux_aio
) {
330 laio_attach_aio_context(ctx
->linux_aio
, ctx
);
333 return ctx
->linux_aio
;
336 LinuxAioState
*aio_get_linux_aio(AioContext
*ctx
)
338 assert(ctx
->linux_aio
);
339 return ctx
->linux_aio
;
343 void aio_notify(AioContext
*ctx
)
345 /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
346 * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
349 if (ctx
->notify_me
) {
350 event_notifier_set(&ctx
->notifier
);
351 atomic_mb_set(&ctx
->notified
, true);
355 void aio_notify_accept(AioContext
*ctx
)
357 if (atomic_xchg(&ctx
->notified
, false)) {
358 event_notifier_test_and_clear(&ctx
->notifier
);
362 static void aio_timerlist_notify(void *opaque
, QEMUClockType type
)
367 static void event_notifier_dummy_cb(EventNotifier
*e
)
371 /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */
372 static bool event_notifier_poll(void *opaque
)
374 EventNotifier
*e
= opaque
;
375 AioContext
*ctx
= container_of(e
, AioContext
, notifier
);
377 return atomic_read(&ctx
->notified
);
380 static void co_schedule_bh_cb(void *opaque
)
382 AioContext
*ctx
= opaque
;
383 QSLIST_HEAD(, Coroutine
) straight
, reversed
;
385 QSLIST_MOVE_ATOMIC(&reversed
, &ctx
->scheduled_coroutines
);
386 QSLIST_INIT(&straight
);
388 while (!QSLIST_EMPTY(&reversed
)) {
389 Coroutine
*co
= QSLIST_FIRST(&reversed
);
390 QSLIST_REMOVE_HEAD(&reversed
, co_scheduled_next
);
391 QSLIST_INSERT_HEAD(&straight
, co
, co_scheduled_next
);
394 while (!QSLIST_EMPTY(&straight
)) {
395 Coroutine
*co
= QSLIST_FIRST(&straight
);
396 QSLIST_REMOVE_HEAD(&straight
, co_scheduled_next
);
397 trace_aio_co_schedule_bh_cb(ctx
, co
);
398 aio_context_acquire(ctx
);
400 /* Protected by write barrier in qemu_aio_coroutine_enter */
401 atomic_set(&co
->scheduled
, NULL
);
402 qemu_aio_coroutine_enter(ctx
, co
);
403 aio_context_release(ctx
);
407 AioContext
*aio_context_new(Error
**errp
)
412 ctx
= (AioContext
*) g_source_new(&aio_source_funcs
, sizeof(AioContext
));
413 aio_context_setup(ctx
);
415 ret
= event_notifier_init(&ctx
->notifier
, false);
417 error_setg_errno(errp
, -ret
, "Failed to initialize event notifier");
420 g_source_set_can_recurse(&ctx
->source
, true);
421 qemu_lockcnt_init(&ctx
->list_lock
);
423 ctx
->co_schedule_bh
= aio_bh_new(ctx
, co_schedule_bh_cb
, ctx
);
424 QSLIST_INIT(&ctx
->scheduled_coroutines
);
426 aio_set_event_notifier(ctx
, &ctx
->notifier
,
428 (EventNotifierHandler
*)
429 event_notifier_dummy_cb
,
430 event_notifier_poll
);
431 #ifdef CONFIG_LINUX_AIO
432 ctx
->linux_aio
= NULL
;
434 ctx
->thread_pool
= NULL
;
435 qemu_rec_mutex_init(&ctx
->lock
);
436 timerlistgroup_init(&ctx
->tlg
, aio_timerlist_notify
, ctx
);
439 ctx
->poll_max_ns
= 0;
441 ctx
->poll_shrink
= 0;
445 g_source_destroy(&ctx
->source
);
449 void aio_co_schedule(AioContext
*ctx
, Coroutine
*co
)
451 trace_aio_co_schedule(ctx
, co
);
452 const char *scheduled
= atomic_cmpxchg(&co
->scheduled
, NULL
,
457 "%s: Co-routine was already scheduled in '%s'\n",
458 __func__
, scheduled
);
462 /* The coroutine might run and release the last ctx reference before we
463 * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until
466 aio_context_ref(ctx
);
468 QSLIST_INSERT_HEAD_ATOMIC(&ctx
->scheduled_coroutines
,
469 co
, co_scheduled_next
);
470 qemu_bh_schedule(ctx
->co_schedule_bh
);
472 aio_context_unref(ctx
);
475 void aio_co_wake(struct Coroutine
*co
)
479 /* Read coroutine before co->ctx. Matches smp_wmb in
480 * qemu_coroutine_enter.
482 smp_read_barrier_depends();
483 ctx
= atomic_read(&co
->ctx
);
485 aio_co_enter(ctx
, co
);
488 void aio_co_enter(AioContext
*ctx
, struct Coroutine
*co
)
490 if (ctx
!= qemu_get_current_aio_context()) {
491 aio_co_schedule(ctx
, co
);
495 if (qemu_in_coroutine()) {
496 Coroutine
*self
= qemu_coroutine_self();
498 QSIMPLEQ_INSERT_TAIL(&self
->co_queue_wakeup
, co
, co_queue_next
);
500 aio_context_acquire(ctx
);
501 qemu_aio_coroutine_enter(ctx
, co
);
502 aio_context_release(ctx
);
506 void aio_context_ref(AioContext
*ctx
)
508 g_source_ref(&ctx
->source
);
511 void aio_context_unref(AioContext
*ctx
)
513 g_source_unref(&ctx
->source
);
516 void aio_context_acquire(AioContext
*ctx
)
518 qemu_rec_mutex_lock(&ctx
->lock
);
521 void aio_context_release(AioContext
*ctx
)
523 qemu_rec_mutex_unlock(&ctx
->lock
);