1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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"
36
37
38
39
40
41enum {
42
43 BH_PENDING = (1 << 0),
44
45
46 BH_SCHEDULED = (1 << 1),
47
48
49 BH_DELETED = (1 << 2),
50
51
52 BH_ONESHOT = (1 << 3),
53
54
55 BH_IDLE = (1 << 4),
56};
57
58struct QEMUBH {
59 AioContext *ctx;
60 QEMUBHFunc *cb;
61 void *opaque;
62 QSLIST_ENTRY(QEMUBH) next;
63 unsigned flags;
64};
65
66
67static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags)
68{
69 AioContext *ctx = bh->ctx;
70 unsigned old_flags;
71
72
73
74
75
76
77
78
79 old_flags = atomic_fetch_or(&bh->flags, BH_PENDING | new_flags);
80 if (!(old_flags & BH_PENDING)) {
81 QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next);
82 }
83
84 aio_notify(ctx);
85}
86
87
88static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags)
89{
90 QEMUBH *bh = QSLIST_FIRST_RCU(head);
91
92 if (!bh) {
93 return NULL;
94 }
95
96 QSLIST_REMOVE_HEAD(head, next);
97
98
99
100
101
102
103
104
105 *flags = atomic_fetch_and(&bh->flags,
106 ~(BH_PENDING | BH_SCHEDULED | BH_IDLE));
107 return bh;
108}
109
110void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
111{
112 QEMUBH *bh;
113 bh = g_new(QEMUBH, 1);
114 *bh = (QEMUBH){
115 .ctx = ctx,
116 .cb = cb,
117 .opaque = opaque,
118 };
119 aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT);
120}
121
122QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
123{
124 QEMUBH *bh;
125 bh = g_new(QEMUBH, 1);
126 *bh = (QEMUBH){
127 .ctx = ctx,
128 .cb = cb,
129 .opaque = opaque,
130 };
131 return bh;
132}
133
134void aio_bh_call(QEMUBH *bh)
135{
136 bh->cb(bh->opaque);
137}
138
139
140int aio_bh_poll(AioContext *ctx)
141{
142 BHListSlice slice;
143 BHListSlice *s;
144 int ret = 0;
145
146 QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list);
147 QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next);
148
149 while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) {
150 QEMUBH *bh;
151 unsigned flags;
152
153 bh = aio_bh_dequeue(&s->bh_list, &flags);
154 if (!bh) {
155 QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next);
156 continue;
157 }
158
159 if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
160
161 if (!(flags & BH_IDLE)) {
162 ret = 1;
163 }
164 aio_bh_call(bh);
165 }
166 if (flags & (BH_DELETED | BH_ONESHOT)) {
167 g_free(bh);
168 }
169 }
170
171 return ret;
172}
173
174void qemu_bh_schedule_idle(QEMUBH *bh)
175{
176 aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE);
177}
178
179void qemu_bh_schedule(QEMUBH *bh)
180{
181 aio_bh_enqueue(bh, BH_SCHEDULED);
182}
183
184
185
186void qemu_bh_cancel(QEMUBH *bh)
187{
188 atomic_and(&bh->flags, ~BH_SCHEDULED);
189}
190
191
192
193
194void qemu_bh_delete(QEMUBH *bh)
195{
196 aio_bh_enqueue(bh, BH_DELETED);
197}
198
199static int64_t aio_compute_bh_timeout(BHList *head, int timeout)
200{
201 QEMUBH *bh;
202
203 QSLIST_FOREACH_RCU(bh, head, next) {
204 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
205 if (bh->flags & BH_IDLE) {
206
207
208 timeout = 10000000;
209 } else {
210
211
212 return 0;
213 }
214 }
215 }
216
217 return timeout;
218}
219
220int64_t
221aio_compute_timeout(AioContext *ctx)
222{
223 BHListSlice *s;
224 int64_t deadline;
225 int timeout = -1;
226
227 timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout);
228 if (timeout == 0) {
229 return 0;
230 }
231
232 QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
233 timeout = aio_compute_bh_timeout(&s->bh_list, timeout);
234 if (timeout == 0) {
235 return 0;
236 }
237 }
238
239 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
240 if (deadline == 0) {
241 return 0;
242 } else {
243 return qemu_soonest_timeout(timeout, deadline);
244 }
245}
246
247static gboolean
248aio_ctx_prepare(GSource *source, gint *timeout)
249{
250 AioContext *ctx = (AioContext *) source;
251
252 atomic_set(&ctx->notify_me, atomic_read(&ctx->notify_me) | 1);
253
254
255
256
257
258
259 smp_mb();
260
261
262 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
263
264 if (aio_prepare(ctx)) {
265 *timeout = 0;
266 }
267
268 return *timeout == 0;
269}
270
271static gboolean
272aio_ctx_check(GSource *source)
273{
274 AioContext *ctx = (AioContext *) source;
275 QEMUBH *bh;
276 BHListSlice *s;
277
278
279 atomic_store_release(&ctx->notify_me, atomic_read(&ctx->notify_me) & ~1);
280 aio_notify_accept(ctx);
281
282 QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) {
283 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
284 return true;
285 }
286 }
287
288 QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) {
289 QSLIST_FOREACH_RCU(bh, &s->bh_list, next) {
290 if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) {
291 return true;
292 }
293 }
294 }
295 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
296}
297
298static gboolean
299aio_ctx_dispatch(GSource *source,
300 GSourceFunc callback,
301 gpointer user_data)
302{
303 AioContext *ctx = (AioContext *) source;
304
305 assert(callback == NULL);
306 aio_dispatch(ctx);
307 return true;
308}
309
310static void
311aio_ctx_finalize(GSource *source)
312{
313 AioContext *ctx = (AioContext *) source;
314 QEMUBH *bh;
315 unsigned flags;
316
317 thread_pool_free(ctx->thread_pool);
318
319#ifdef CONFIG_LINUX_AIO
320 if (ctx->linux_aio) {
321 laio_detach_aio_context(ctx->linux_aio, ctx);
322 laio_cleanup(ctx->linux_aio);
323 ctx->linux_aio = NULL;
324 }
325#endif
326
327#ifdef CONFIG_LINUX_IO_URING
328 if (ctx->linux_io_uring) {
329 luring_detach_aio_context(ctx->linux_io_uring, ctx);
330 luring_cleanup(ctx->linux_io_uring);
331 ctx->linux_io_uring = NULL;
332 }
333#endif
334
335 assert(QSLIST_EMPTY(&ctx->scheduled_coroutines));
336 qemu_bh_delete(ctx->co_schedule_bh);
337
338
339 assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list));
340
341 while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) {
342
343 assert(flags & BH_DELETED);
344
345 g_free(bh);
346 }
347
348 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL);
349 event_notifier_cleanup(&ctx->notifier);
350 qemu_rec_mutex_destroy(&ctx->lock);
351 qemu_lockcnt_destroy(&ctx->list_lock);
352 timerlistgroup_deinit(&ctx->tlg);
353 aio_context_destroy(ctx);
354}
355
356static GSourceFuncs aio_source_funcs = {
357 aio_ctx_prepare,
358 aio_ctx_check,
359 aio_ctx_dispatch,
360 aio_ctx_finalize
361};
362
363GSource *aio_get_g_source(AioContext *ctx)
364{
365 g_source_ref(&ctx->source);
366 return &ctx->source;
367}
368
369ThreadPool *aio_get_thread_pool(AioContext *ctx)
370{
371 if (!ctx->thread_pool) {
372 ctx->thread_pool = thread_pool_new(ctx);
373 }
374 return ctx->thread_pool;
375}
376
377#ifdef CONFIG_LINUX_AIO
378LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp)
379{
380 if (!ctx->linux_aio) {
381 ctx->linux_aio = laio_init(errp);
382 if (ctx->linux_aio) {
383 laio_attach_aio_context(ctx->linux_aio, ctx);
384 }
385 }
386 return ctx->linux_aio;
387}
388
389LinuxAioState *aio_get_linux_aio(AioContext *ctx)
390{
391 assert(ctx->linux_aio);
392 return ctx->linux_aio;
393}
394#endif
395
396#ifdef CONFIG_LINUX_IO_URING
397LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp)
398{
399 if (ctx->linux_io_uring) {
400 return ctx->linux_io_uring;
401 }
402
403 ctx->linux_io_uring = luring_init(errp);
404 if (!ctx->linux_io_uring) {
405 return NULL;
406 }
407
408 luring_attach_aio_context(ctx->linux_io_uring, ctx);
409 return ctx->linux_io_uring;
410}
411
412LuringState *aio_get_linux_io_uring(AioContext *ctx)
413{
414 assert(ctx->linux_io_uring);
415 return ctx->linux_io_uring;
416}
417#endif
418
419void aio_notify(AioContext *ctx)
420{
421
422
423
424 smp_mb();
425 if (atomic_read(&ctx->notify_me)) {
426 event_notifier_set(&ctx->notifier);
427 atomic_mb_set(&ctx->notified, true);
428 }
429}
430
431void aio_notify_accept(AioContext *ctx)
432{
433 if (atomic_xchg(&ctx->notified, false)
434#ifdef WIN32
435 || true
436#endif
437 ) {
438 event_notifier_test_and_clear(&ctx->notifier);
439 }
440}
441
442static void aio_timerlist_notify(void *opaque, QEMUClockType type)
443{
444 aio_notify(opaque);
445}
446
447static void event_notifier_dummy_cb(EventNotifier *e)
448{
449}
450
451
452static bool event_notifier_poll(void *opaque)
453{
454 EventNotifier *e = opaque;
455 AioContext *ctx = container_of(e, AioContext, notifier);
456
457 return atomic_read(&ctx->notified);
458}
459
460static void co_schedule_bh_cb(void *opaque)
461{
462 AioContext *ctx = opaque;
463 QSLIST_HEAD(, Coroutine) straight, reversed;
464
465 QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines);
466 QSLIST_INIT(&straight);
467
468 while (!QSLIST_EMPTY(&reversed)) {
469 Coroutine *co = QSLIST_FIRST(&reversed);
470 QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next);
471 QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next);
472 }
473
474 while (!QSLIST_EMPTY(&straight)) {
475 Coroutine *co = QSLIST_FIRST(&straight);
476 QSLIST_REMOVE_HEAD(&straight, co_scheduled_next);
477 trace_aio_co_schedule_bh_cb(ctx, co);
478 aio_context_acquire(ctx);
479
480
481 atomic_set(&co->scheduled, NULL);
482 qemu_aio_coroutine_enter(ctx, co);
483 aio_context_release(ctx);
484 }
485}
486
487AioContext *aio_context_new(Error **errp)
488{
489 int ret;
490 AioContext *ctx;
491
492 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
493 QSLIST_INIT(&ctx->bh_list);
494 QSIMPLEQ_INIT(&ctx->bh_slice_list);
495 aio_context_setup(ctx);
496
497 ret = event_notifier_init(&ctx->notifier, false);
498 if (ret < 0) {
499 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
500 goto fail;
501 }
502 g_source_set_can_recurse(&ctx->source, true);
503 qemu_lockcnt_init(&ctx->list_lock);
504
505 ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx);
506 QSLIST_INIT(&ctx->scheduled_coroutines);
507
508 aio_set_event_notifier(ctx, &ctx->notifier,
509 false,
510 event_notifier_dummy_cb,
511 event_notifier_poll);
512#ifdef CONFIG_LINUX_AIO
513 ctx->linux_aio = NULL;
514#endif
515
516#ifdef CONFIG_LINUX_IO_URING
517 ctx->linux_io_uring = NULL;
518#endif
519
520 ctx->thread_pool = NULL;
521 qemu_rec_mutex_init(&ctx->lock);
522 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
523
524 ctx->poll_ns = 0;
525 ctx->poll_max_ns = 0;
526 ctx->poll_grow = 0;
527 ctx->poll_shrink = 0;
528
529 return ctx;
530fail:
531 g_source_destroy(&ctx->source);
532 return NULL;
533}
534
535void aio_co_schedule(AioContext *ctx, Coroutine *co)
536{
537 trace_aio_co_schedule(ctx, co);
538 const char *scheduled = atomic_cmpxchg(&co->scheduled, NULL,
539 __func__);
540
541 if (scheduled) {
542 fprintf(stderr,
543 "%s: Co-routine was already scheduled in '%s'\n",
544 __func__, scheduled);
545 abort();
546 }
547
548
549
550
551
552 aio_context_ref(ctx);
553
554 QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines,
555 co, co_scheduled_next);
556 qemu_bh_schedule(ctx->co_schedule_bh);
557
558 aio_context_unref(ctx);
559}
560
561void aio_co_wake(struct Coroutine *co)
562{
563 AioContext *ctx;
564
565
566
567
568 smp_read_barrier_depends();
569 ctx = atomic_read(&co->ctx);
570
571 aio_co_enter(ctx, co);
572}
573
574void aio_co_enter(AioContext *ctx, struct Coroutine *co)
575{
576 if (ctx != qemu_get_current_aio_context()) {
577 aio_co_schedule(ctx, co);
578 return;
579 }
580
581 if (qemu_in_coroutine()) {
582 Coroutine *self = qemu_coroutine_self();
583 assert(self != co);
584 QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next);
585 } else {
586 aio_context_acquire(ctx);
587 qemu_aio_coroutine_enter(ctx, co);
588 aio_context_release(ctx);
589 }
590}
591
592void aio_context_ref(AioContext *ctx)
593{
594 g_source_ref(&ctx->source);
595}
596
597void aio_context_unref(AioContext *ctx)
598{
599 g_source_unref(&ctx->source);
600}
601
602void aio_context_acquire(AioContext *ctx)
603{
604 qemu_rec_mutex_lock(&ctx->lock);
605}
606
607void aio_context_release(AioContext *ctx)
608{
609 qemu_rec_mutex_unlock(&ctx->lock);
610}
611