2 * Linux native AIO support.
4 * Copyright (C) 2009 IBM, Corp.
5 * Copyright (C) 2009 Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu-common.h"
12 #include "block/aio.h"
13 #include "qemu/queue.h"
14 #include "block/block.h"
15 #include "block/raw-aio.h"
16 #include "qemu/event_notifier.h"
17 #include "qemu/coroutine.h"
18 #include "qapi/error.h"
23 * Queue size (per-device).
25 * XXX: eventually we need to communicate this to the guest and/or make it
26 * tunable by the guest. If we get more outstanding requests at a time
27 * than this we will get EAGAIN from io_submit which is communicated to
28 * the guest as an I/O error.
30 #define MAX_EVENTS 128
41 QSIMPLEQ_ENTRY(qemu_laiocb
) next
;
46 unsigned int in_queue
;
47 unsigned int in_flight
;
49 QSIMPLEQ_HEAD(, qemu_laiocb
) pending
;
52 struct LinuxAioState
{
53 AioContext
*aio_context
;
58 /* io queue for submit at batch. Protected by AioContext lock. */
61 /* I/O completion processing. Only runs in I/O thread. */
62 QEMUBH
*completion_bh
;
67 static void ioq_submit(LinuxAioState
*s
);
69 static inline ssize_t
io_event_ret(struct io_event
*ev
)
71 return (ssize_t
)(((uint64_t)ev
->res2
<< 32) | ev
->res
);
75 * Completes an AIO request (calls the callback and frees the ACB).
77 static void qemu_laio_process_completion(struct qemu_laiocb
*laiocb
)
82 if (ret
!= -ECANCELED
) {
83 if (ret
== laiocb
->nbytes
) {
85 } else if (ret
>= 0) {
86 /* Short reads mean EOF, pad with zeros. */
87 if (laiocb
->is_read
) {
88 qemu_iovec_memset(laiocb
->qiov
, ret
, 0,
89 laiocb
->qiov
->size
- ret
);
98 /* If the coroutine is already entered it must be in ioq_submit() and
99 * will notice laio->ret has been filled in when it eventually runs
100 * later. Coroutines cannot be entered recursively so avoid doing
103 if (!qemu_coroutine_entered(laiocb
->co
)) {
104 aio_co_wake(laiocb
->co
);
107 laiocb
->common
.cb(laiocb
->common
.opaque
, ret
);
108 qemu_aio_unref(laiocb
);
113 * aio_ring buffer which is shared between userspace and kernel.
115 * This copied from linux/fs/aio.c, common header does not exist
116 * but AIO exists for ages so we assume ABI is stable.
119 unsigned id
; /* kernel internal index number */
120 unsigned nr
; /* number of io_events */
121 unsigned head
; /* Written to by userland or by kernel. */
125 unsigned compat_features
;
126 unsigned incompat_features
;
127 unsigned header_length
; /* size of aio_ring */
129 struct io_event io_events
[0];
135 * @events: pointer on events array, output value
137 * Returns the number of completed events and sets a pointer
138 * on events array. This function does not update the internal
139 * ring buffer, only reads head and tail. When @events has been
140 * processed io_getevents_commit() must be called.
142 static inline unsigned int io_getevents_peek(io_context_t ctx
,
143 struct io_event
**events
)
145 struct aio_ring
*ring
= (struct aio_ring
*)ctx
;
146 unsigned int head
= ring
->head
, tail
= ring
->tail
;
149 nr
= tail
>= head
? tail
- head
: ring
->nr
- head
;
150 *events
= ring
->io_events
+ head
;
151 /* To avoid speculative loads of s->events[i] before observing tail.
152 Paired with smp_wmb() inside linux/fs/aio.c: aio_complete(). */
159 * io_getevents_commit:
161 * @nr: the number of events on which head should be advanced
163 * Advances head of a ring buffer.
165 static inline void io_getevents_commit(io_context_t ctx
, unsigned int nr
)
167 struct aio_ring
*ring
= (struct aio_ring
*)ctx
;
170 ring
->head
= (ring
->head
+ nr
) % ring
->nr
;
175 * io_getevents_advance_and_peek:
177 * @events: pointer on events array, output value
178 * @nr: the number of events on which head should be advanced
180 * Advances head of a ring buffer and returns number of elements left.
182 static inline unsigned int
183 io_getevents_advance_and_peek(io_context_t ctx
,
184 struct io_event
**events
,
187 io_getevents_commit(ctx
, nr
);
188 return io_getevents_peek(ctx
, events
);
192 * qemu_laio_process_completions:
195 * Fetches completed I/O requests and invokes their callbacks.
197 * The function is somewhat tricky because it supports nested event loops, for
198 * example when a request callback invokes aio_poll(). In order to do this,
199 * indices are kept in LinuxAioState. Function schedules BH completion so it
200 * can be called again in a nested event loop. When there are no events left
201 * to complete the BH is being canceled.
203 static void qemu_laio_process_completions(LinuxAioState
*s
)
205 struct io_event
*events
;
207 /* Reschedule so nested event loops see currently pending completions */
208 qemu_bh_schedule(s
->completion_bh
);
210 while ((s
->event_max
= io_getevents_advance_and_peek(s
->ctx
, &events
,
212 for (s
->event_idx
= 0; s
->event_idx
< s
->event_max
; ) {
213 struct iocb
*iocb
= events
[s
->event_idx
].obj
;
214 struct qemu_laiocb
*laiocb
=
215 container_of(iocb
, struct qemu_laiocb
, iocb
);
217 laiocb
->ret
= io_event_ret(&events
[s
->event_idx
]);
219 /* Change counters one-by-one because we can be nested. */
222 qemu_laio_process_completion(laiocb
);
226 qemu_bh_cancel(s
->completion_bh
);
228 /* If we are nested we have to notify the level above that we are done
229 * by setting event_max to zero, upper level will then jump out of it's
230 * own `for` loop. If we are the last all counters droped to zero. */
235 static void qemu_laio_process_completions_and_submit(LinuxAioState
*s
)
237 qemu_laio_process_completions(s
);
239 aio_context_acquire(s
->aio_context
);
240 if (!s
->io_q
.plugged
&& !QSIMPLEQ_EMPTY(&s
->io_q
.pending
)) {
243 aio_context_release(s
->aio_context
);
246 static void qemu_laio_completion_bh(void *opaque
)
248 LinuxAioState
*s
= opaque
;
250 qemu_laio_process_completions_and_submit(s
);
253 static void qemu_laio_completion_cb(EventNotifier
*e
)
255 LinuxAioState
*s
= container_of(e
, LinuxAioState
, e
);
257 if (event_notifier_test_and_clear(&s
->e
)) {
258 qemu_laio_process_completions_and_submit(s
);
262 static bool qemu_laio_poll_cb(void *opaque
)
264 EventNotifier
*e
= opaque
;
265 LinuxAioState
*s
= container_of(e
, LinuxAioState
, e
);
266 struct io_event
*events
;
268 if (!io_getevents_peek(s
->ctx
, &events
)) {
272 qemu_laio_process_completions_and_submit(s
);
276 static void laio_cancel(BlockAIOCB
*blockacb
)
278 struct qemu_laiocb
*laiocb
= (struct qemu_laiocb
*)blockacb
;
279 struct io_event event
;
282 if (laiocb
->ret
!= -EINPROGRESS
) {
285 ret
= io_cancel(laiocb
->ctx
->ctx
, &laiocb
->iocb
, &event
);
286 laiocb
->ret
= -ECANCELED
;
288 /* iocb is not cancelled, cb will be called by the event loop later */
292 laiocb
->common
.cb(laiocb
->common
.opaque
, laiocb
->ret
);
295 static const AIOCBInfo laio_aiocb_info
= {
296 .aiocb_size
= sizeof(struct qemu_laiocb
),
297 .cancel_async
= laio_cancel
,
300 static void ioq_init(LaioQueue
*io_q
)
302 QSIMPLEQ_INIT(&io_q
->pending
);
306 io_q
->blocked
= false;
309 static void ioq_submit(LinuxAioState
*s
)
312 struct qemu_laiocb
*aiocb
;
313 struct iocb
*iocbs
[MAX_EVENTS
];
314 QSIMPLEQ_HEAD(, qemu_laiocb
) completed
;
317 if (s
->io_q
.in_flight
>= MAX_EVENTS
) {
321 QSIMPLEQ_FOREACH(aiocb
, &s
->io_q
.pending
, next
) {
322 iocbs
[len
++] = &aiocb
->iocb
;
323 if (s
->io_q
.in_flight
+ len
>= MAX_EVENTS
) {
328 ret
= io_submit(s
->ctx
, len
, iocbs
);
329 if (ret
== -EAGAIN
) {
333 /* Fail the first request, retry the rest */
334 aiocb
= QSIMPLEQ_FIRST(&s
->io_q
.pending
);
335 QSIMPLEQ_REMOVE_HEAD(&s
->io_q
.pending
, next
);
338 qemu_laio_process_completion(aiocb
);
342 s
->io_q
.in_flight
+= ret
;
343 s
->io_q
.in_queue
-= ret
;
344 aiocb
= container_of(iocbs
[ret
- 1], struct qemu_laiocb
, iocb
);
345 QSIMPLEQ_SPLIT_AFTER(&s
->io_q
.pending
, aiocb
, next
, &completed
);
346 } while (ret
== len
&& !QSIMPLEQ_EMPTY(&s
->io_q
.pending
));
347 s
->io_q
.blocked
= (s
->io_q
.in_queue
> 0);
349 if (s
->io_q
.in_flight
) {
350 /* We can try to complete something just right away if there are
351 * still requests in-flight. */
352 qemu_laio_process_completions(s
);
354 * Even we have completed everything (in_flight == 0), the queue can
355 * have still pended requests (in_queue > 0). We do not attempt to
356 * repeat submission to avoid IO hang. The reason is simple: s->e is
357 * still set and completion callback will be called shortly and all
358 * pended requests will be submitted from there.
363 void laio_io_plug(BlockDriverState
*bs
, LinuxAioState
*s
)
368 void laio_io_unplug(BlockDriverState
*bs
, LinuxAioState
*s
)
370 assert(s
->io_q
.plugged
);
371 if (--s
->io_q
.plugged
== 0 &&
372 !s
->io_q
.blocked
&& !QSIMPLEQ_EMPTY(&s
->io_q
.pending
)) {
377 static int laio_do_submit(int fd
, struct qemu_laiocb
*laiocb
, off_t offset
,
380 LinuxAioState
*s
= laiocb
->ctx
;
381 struct iocb
*iocbs
= &laiocb
->iocb
;
382 QEMUIOVector
*qiov
= laiocb
->qiov
;
386 io_prep_pwritev(iocbs
, fd
, qiov
->iov
, qiov
->niov
, offset
);
389 io_prep_preadv(iocbs
, fd
, qiov
->iov
, qiov
->niov
, offset
);
391 /* Currently Linux kernel does not support other operations */
393 fprintf(stderr
, "%s: invalid AIO request type 0x%x.\n",
397 io_set_eventfd(&laiocb
->iocb
, event_notifier_get_fd(&s
->e
));
399 QSIMPLEQ_INSERT_TAIL(&s
->io_q
.pending
, laiocb
, next
);
401 if (!s
->io_q
.blocked
&&
403 s
->io_q
.in_flight
+ s
->io_q
.in_queue
>= MAX_EVENTS
)) {
410 int coroutine_fn
laio_co_submit(BlockDriverState
*bs
, LinuxAioState
*s
, int fd
,
411 uint64_t offset
, QEMUIOVector
*qiov
, int type
)
414 struct qemu_laiocb laiocb
= {
415 .co
= qemu_coroutine_self(),
416 .nbytes
= qiov
->size
,
419 .is_read
= (type
== QEMU_AIO_READ
),
423 ret
= laio_do_submit(fd
, &laiocb
, offset
, type
);
428 if (laiocb
.ret
== -EINPROGRESS
) {
429 qemu_coroutine_yield();
434 BlockAIOCB
*laio_submit(BlockDriverState
*bs
, LinuxAioState
*s
, int fd
,
435 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
436 BlockCompletionFunc
*cb
, void *opaque
, int type
)
438 struct qemu_laiocb
*laiocb
;
439 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
442 laiocb
= qemu_aio_get(&laio_aiocb_info
, bs
, cb
, opaque
);
443 laiocb
->nbytes
= nb_sectors
* BDRV_SECTOR_SIZE
;
445 laiocb
->ret
= -EINPROGRESS
;
446 laiocb
->is_read
= (type
== QEMU_AIO_READ
);
449 ret
= laio_do_submit(fd
, laiocb
, offset
, type
);
451 qemu_aio_unref(laiocb
);
455 return &laiocb
->common
;
458 void laio_detach_aio_context(LinuxAioState
*s
, AioContext
*old_context
)
460 aio_set_event_notifier(old_context
, &s
->e
, false, NULL
, NULL
);
461 qemu_bh_delete(s
->completion_bh
);
462 s
->aio_context
= NULL
;
465 void laio_attach_aio_context(LinuxAioState
*s
, AioContext
*new_context
)
467 s
->aio_context
= new_context
;
468 s
->completion_bh
= aio_bh_new(new_context
, qemu_laio_completion_bh
, s
);
469 aio_set_event_notifier(new_context
, &s
->e
, false,
470 qemu_laio_completion_cb
,
474 LinuxAioState
*laio_init(Error
**errp
)
479 s
= g_malloc0(sizeof(*s
));
480 rc
= event_notifier_init(&s
->e
, false);
482 error_setg_errno(errp
, -rc
, "failed to to initialize event notifier");
486 rc
= io_setup(MAX_EVENTS
, &s
->ctx
);
488 error_setg_errno(errp
, -rc
, "failed to create linux AIO context");
497 event_notifier_cleanup(&s
->e
);
503 void laio_cleanup(LinuxAioState
*s
)
505 event_notifier_cleanup(&s
->e
);
507 if (io_destroy(s
->ctx
) != 0) {
508 fprintf(stderr
, "%s: destroy AIO context %p failed\n",