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 "qemu-common.h"
29 #include "block/aio.h"
30 #include "block/thread-pool.h"
31 #include "qemu/main-loop.h"
32 #include "qemu/atomic.h"
33 #include "block/raw-aio.h"
34 #include "qemu/coroutine_int.h"
37 /***********************************************************/
38 /* bottom halves (can be seen as timers which expire ASAP) */
50 void aio_bh_schedule_oneshot(AioContext
*ctx
, QEMUBHFunc
*cb
, void *opaque
)
53 bh
= g_new(QEMUBH
, 1);
59 qemu_lockcnt_lock(&ctx
->list_lock
);
60 bh
->next
= ctx
->first_bh
;
63 /* Make sure that the members are ready before putting bh into list */
66 qemu_lockcnt_unlock(&ctx
->list_lock
);
70 QEMUBH
*aio_bh_new(AioContext
*ctx
, QEMUBHFunc
*cb
, void *opaque
)
73 bh
= g_new(QEMUBH
, 1);
79 qemu_lockcnt_lock(&ctx
->list_lock
);
80 bh
->next
= ctx
->first_bh
;
81 /* Make sure that the members are ready before putting bh into list */
84 qemu_lockcnt_unlock(&ctx
->list_lock
);
88 void aio_bh_call(QEMUBH
*bh
)
93 /* Multiple occurrences of aio_bh_poll cannot be called concurrently.
94 * The count in ctx->list_lock is incremented before the call, and is
95 * not affected by the call.
97 int aio_bh_poll(AioContext
*ctx
)
99 QEMUBH
*bh
, **bhp
, *next
;
101 bool deleted
= false;
104 for (bh
= atomic_rcu_read(&ctx
->first_bh
); bh
; bh
= next
) {
105 next
= atomic_rcu_read(&bh
->next
);
106 /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
107 * implicit memory barrier ensures that the callback sees all writes
108 * done by the scheduling thread. It also ensures that the scheduling
109 * thread sees the zero before bh->cb has run, and thus will call
110 * aio_notify again if necessary.
112 if (atomic_xchg(&bh
->scheduled
, 0)) {
113 /* Idle BHs don't count as progress */
125 /* remove deleted bhs */
130 if (qemu_lockcnt_dec_if_lock(&ctx
->list_lock
)) {
131 bhp
= &ctx
->first_bh
;
134 if (bh
->deleted
&& !bh
->scheduled
) {
141 qemu_lockcnt_inc_and_unlock(&ctx
->list_lock
);
146 void qemu_bh_schedule_idle(QEMUBH
*bh
)
149 /* Make sure that idle & any writes needed by the callback are done
150 * before the locations are read in the aio_bh_poll.
152 atomic_mb_set(&bh
->scheduled
, 1);
155 void qemu_bh_schedule(QEMUBH
*bh
)
161 /* The memory barrier implicit in atomic_xchg makes sure that:
162 * 1. idle & any writes needed by the callback are done before the
163 * locations are read in the aio_bh_poll.
164 * 2. ctx is loaded before scheduled is set and the callback has a chance
167 if (atomic_xchg(&bh
->scheduled
, 1) == 0) {
173 /* This func is async.
175 void qemu_bh_cancel(QEMUBH
*bh
)
177 atomic_mb_set(&bh
->scheduled
, 0);
180 /* This func is async.The bottom half will do the delete action at the finial
183 void qemu_bh_delete(QEMUBH
*bh
)
190 aio_compute_timeout(AioContext
*ctx
)
196 for (bh
= atomic_rcu_read(&ctx
->first_bh
); bh
;
197 bh
= atomic_rcu_read(&bh
->next
)) {
200 /* idle bottom halves will be polled at least
204 /* non-idle bottom halves will be executed
211 deadline
= timerlistgroup_deadline_ns(&ctx
->tlg
);
215 return qemu_soonest_timeout(timeout
, deadline
);
220 aio_ctx_prepare(GSource
*source
, gint
*timeout
)
222 AioContext
*ctx
= (AioContext
*) source
;
224 atomic_or(&ctx
->notify_me
, 1);
226 /* We assume there is no timeout already supplied */
227 *timeout
= qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
));
229 if (aio_prepare(ctx
)) {
233 return *timeout
== 0;
237 aio_ctx_check(GSource
*source
)
239 AioContext
*ctx
= (AioContext
*) source
;
242 atomic_and(&ctx
->notify_me
, ~1);
243 aio_notify_accept(ctx
);
245 for (bh
= ctx
->first_bh
; bh
; bh
= bh
->next
) {
250 return aio_pending(ctx
) || (timerlistgroup_deadline_ns(&ctx
->tlg
) == 0);
254 aio_ctx_dispatch(GSource
*source
,
255 GSourceFunc callback
,
258 AioContext
*ctx
= (AioContext
*) source
;
260 assert(callback
== NULL
);
266 aio_ctx_finalize(GSource
*source
)
268 AioContext
*ctx
= (AioContext
*) source
;
270 thread_pool_free(ctx
->thread_pool
);
272 #ifdef CONFIG_LINUX_AIO
273 if (ctx
->linux_aio
) {
274 laio_detach_aio_context(ctx
->linux_aio
, ctx
);
275 laio_cleanup(ctx
->linux_aio
);
276 ctx
->linux_aio
= NULL
;
280 assert(QSLIST_EMPTY(&ctx
->scheduled_coroutines
));
281 qemu_bh_delete(ctx
->co_schedule_bh
);
283 qemu_lockcnt_lock(&ctx
->list_lock
);
284 assert(!qemu_lockcnt_count(&ctx
->list_lock
));
285 while (ctx
->first_bh
) {
286 QEMUBH
*next
= ctx
->first_bh
->next
;
288 /* qemu_bh_delete() must have been called on BHs in this AioContext */
289 assert(ctx
->first_bh
->deleted
);
291 g_free(ctx
->first_bh
);
292 ctx
->first_bh
= next
;
294 qemu_lockcnt_unlock(&ctx
->list_lock
);
296 aio_set_event_notifier(ctx
, &ctx
->notifier
, false, NULL
, NULL
);
297 event_notifier_cleanup(&ctx
->notifier
);
298 qemu_rec_mutex_destroy(&ctx
->lock
);
299 qemu_lockcnt_destroy(&ctx
->list_lock
);
300 timerlistgroup_deinit(&ctx
->tlg
);
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_get_linux_aio(AioContext
*ctx
)
327 if (!ctx
->linux_aio
) {
328 ctx
->linux_aio
= laio_init();
329 laio_attach_aio_context(ctx
->linux_aio
, ctx
);
331 return ctx
->linux_aio
;
335 void aio_notify(AioContext
*ctx
)
337 /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
338 * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
341 if (ctx
->notify_me
) {
342 event_notifier_set(&ctx
->notifier
);
343 atomic_mb_set(&ctx
->notified
, true);
347 void aio_notify_accept(AioContext
*ctx
)
349 if (atomic_xchg(&ctx
->notified
, false)) {
350 event_notifier_test_and_clear(&ctx
->notifier
);
354 static void aio_timerlist_notify(void *opaque
, QEMUClockType type
)
359 static void event_notifier_dummy_cb(EventNotifier
*e
)
363 /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */
364 static bool event_notifier_poll(void *opaque
)
366 EventNotifier
*e
= opaque
;
367 AioContext
*ctx
= container_of(e
, AioContext
, notifier
);
369 return atomic_read(&ctx
->notified
);
372 static void co_schedule_bh_cb(void *opaque
)
374 AioContext
*ctx
= opaque
;
375 QSLIST_HEAD(, Coroutine
) straight
, reversed
;
377 QSLIST_MOVE_ATOMIC(&reversed
, &ctx
->scheduled_coroutines
);
378 QSLIST_INIT(&straight
);
380 while (!QSLIST_EMPTY(&reversed
)) {
381 Coroutine
*co
= QSLIST_FIRST(&reversed
);
382 QSLIST_REMOVE_HEAD(&reversed
, co_scheduled_next
);
383 QSLIST_INSERT_HEAD(&straight
, co
, co_scheduled_next
);
386 while (!QSLIST_EMPTY(&straight
)) {
387 Coroutine
*co
= QSLIST_FIRST(&straight
);
388 QSLIST_REMOVE_HEAD(&straight
, co_scheduled_next
);
389 trace_aio_co_schedule_bh_cb(ctx
, co
);
390 aio_context_acquire(ctx
);
391 qemu_coroutine_enter(co
);
392 aio_context_release(ctx
);
396 AioContext
*aio_context_new(Error
**errp
)
401 ctx
= (AioContext
*) g_source_new(&aio_source_funcs
, sizeof(AioContext
));
402 aio_context_setup(ctx
);
404 ret
= event_notifier_init(&ctx
->notifier
, false);
406 error_setg_errno(errp
, -ret
, "Failed to initialize event notifier");
409 g_source_set_can_recurse(&ctx
->source
, true);
410 qemu_lockcnt_init(&ctx
->list_lock
);
412 ctx
->co_schedule_bh
= aio_bh_new(ctx
, co_schedule_bh_cb
, ctx
);
413 QSLIST_INIT(&ctx
->scheduled_coroutines
);
415 aio_set_event_notifier(ctx
, &ctx
->notifier
,
417 (EventNotifierHandler
*)
418 event_notifier_dummy_cb
,
419 event_notifier_poll
);
420 #ifdef CONFIG_LINUX_AIO
421 ctx
->linux_aio
= NULL
;
423 ctx
->thread_pool
= NULL
;
424 qemu_rec_mutex_init(&ctx
->lock
);
425 timerlistgroup_init(&ctx
->tlg
, aio_timerlist_notify
, ctx
);
428 ctx
->poll_max_ns
= 0;
430 ctx
->poll_shrink
= 0;
434 g_source_destroy(&ctx
->source
);
438 void aio_co_schedule(AioContext
*ctx
, Coroutine
*co
)
440 trace_aio_co_schedule(ctx
, co
);
441 QSLIST_INSERT_HEAD_ATOMIC(&ctx
->scheduled_coroutines
,
442 co
, co_scheduled_next
);
443 qemu_bh_schedule(ctx
->co_schedule_bh
);
446 void aio_co_wake(struct Coroutine
*co
)
450 /* Read coroutine before co->ctx. Matches smp_wmb in
451 * qemu_coroutine_enter.
453 smp_read_barrier_depends();
454 ctx
= atomic_read(&co
->ctx
);
456 aio_co_enter(ctx
, co
);
459 void aio_co_enter(AioContext
*ctx
, struct Coroutine
*co
)
461 if (ctx
!= qemu_get_current_aio_context()) {
462 aio_co_schedule(ctx
, co
);
466 if (qemu_in_coroutine()) {
467 Coroutine
*self
= qemu_coroutine_self();
469 QSIMPLEQ_INSERT_TAIL(&self
->co_queue_wakeup
, co
, co_queue_next
);
471 aio_context_acquire(ctx
);
472 qemu_aio_coroutine_enter(ctx
, co
);
473 aio_context_release(ctx
);
477 void aio_context_ref(AioContext
*ctx
)
479 g_source_ref(&ctx
->source
);
482 void aio_context_unref(AioContext
*ctx
)
484 g_source_unref(&ctx
->source
);
487 void aio_context_acquire(AioContext
*ctx
)
489 qemu_rec_mutex_lock(&ctx
->lock
);
492 void aio_context_release(AioContext
*ctx
)
494 qemu_rec_mutex_unlock(&ctx
->lock
);