1
2
3
4
5
6
7
8
9
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
19#include <libaio.h>
20
21
22
23
24
25
26
27
28
29#define MAX_EVENTS 128
30
31struct qemu_laiocb {
32 BlockAIOCB common;
33 Coroutine *co;
34 LinuxAioState *ctx;
35 struct iocb iocb;
36 ssize_t ret;
37 size_t nbytes;
38 QEMUIOVector *qiov;
39 bool is_read;
40 QSIMPLEQ_ENTRY(qemu_laiocb) next;
41};
42
43typedef struct {
44 int plugged;
45 unsigned int in_queue;
46 unsigned int in_flight;
47 bool blocked;
48 QSIMPLEQ_HEAD(, qemu_laiocb) pending;
49} LaioQueue;
50
51struct LinuxAioState {
52 AioContext *aio_context;
53
54 io_context_t ctx;
55 EventNotifier e;
56
57
58 LaioQueue io_q;
59
60
61 QEMUBH *completion_bh;
62 int event_idx;
63 int event_max;
64};
65
66static void ioq_submit(LinuxAioState *s);
67
68static inline ssize_t io_event_ret(struct io_event *ev)
69{
70 return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
71}
72
73
74
75
76static void qemu_laio_process_completion(struct qemu_laiocb *laiocb)
77{
78 int ret;
79
80 ret = laiocb->ret;
81 if (ret != -ECANCELED) {
82 if (ret == laiocb->nbytes) {
83 ret = 0;
84 } else if (ret >= 0) {
85
86 if (laiocb->is_read) {
87 qemu_iovec_memset(laiocb->qiov, ret, 0,
88 laiocb->qiov->size - ret);
89 } else {
90 ret = -ENOSPC;
91 }
92 }
93 }
94
95 laiocb->ret = ret;
96 if (laiocb->co) {
97
98
99
100
101
102 if (!qemu_coroutine_entered(laiocb->co)) {
103 qemu_coroutine_enter(laiocb->co);
104 }
105 } else {
106 laiocb->common.cb(laiocb->common.opaque, ret);
107 qemu_aio_unref(laiocb);
108 }
109}
110
111
112
113
114
115
116
117struct aio_ring {
118 unsigned id;
119 unsigned nr;
120 unsigned head;
121 unsigned tail;
122
123 unsigned magic;
124 unsigned compat_features;
125 unsigned incompat_features;
126 unsigned header_length;
127
128 struct io_event io_events[0];
129};
130
131
132
133
134
135
136
137
138
139
140
141static inline unsigned int io_getevents_peek(io_context_t ctx,
142 struct io_event **events)
143{
144 struct aio_ring *ring = (struct aio_ring *)ctx;
145 unsigned int head = ring->head, tail = ring->tail;
146 unsigned int nr;
147
148 nr = tail >= head ? tail - head : ring->nr - head;
149 *events = ring->io_events + head;
150
151
152 smp_rmb();
153
154 return nr;
155}
156
157
158
159
160
161
162
163
164static inline void io_getevents_commit(io_context_t ctx, unsigned int nr)
165{
166 struct aio_ring *ring = (struct aio_ring *)ctx;
167
168 if (nr) {
169 ring->head = (ring->head + nr) % ring->nr;
170 }
171}
172
173
174
175
176
177
178
179
180
181static inline unsigned int
182io_getevents_advance_and_peek(io_context_t ctx,
183 struct io_event **events,
184 unsigned int nr)
185{
186 io_getevents_commit(ctx, nr);
187 return io_getevents_peek(ctx, events);
188}
189
190
191
192
193
194
195
196
197
198
199
200
201
202static void qemu_laio_process_completions(LinuxAioState *s)
203{
204 struct io_event *events;
205
206
207 qemu_bh_schedule(s->completion_bh);
208
209 while ((s->event_max = io_getevents_advance_and_peek(s->ctx, &events,
210 s->event_idx))) {
211 for (s->event_idx = 0; s->event_idx < s->event_max; ) {
212 struct iocb *iocb = events[s->event_idx].obj;
213 struct qemu_laiocb *laiocb =
214 container_of(iocb, struct qemu_laiocb, iocb);
215
216 laiocb->ret = io_event_ret(&events[s->event_idx]);
217
218
219 s->io_q.in_flight--;
220 s->event_idx++;
221 qemu_laio_process_completion(laiocb);
222 }
223 }
224
225 qemu_bh_cancel(s->completion_bh);
226
227
228
229
230 s->event_max = 0;
231 s->event_idx = 0;
232}
233
234static void qemu_laio_process_completions_and_submit(LinuxAioState *s)
235{
236 qemu_laio_process_completions(s);
237 if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
238 ioq_submit(s);
239 }
240}
241
242static void qemu_laio_completion_bh(void *opaque)
243{
244 LinuxAioState *s = opaque;
245
246 qemu_laio_process_completions_and_submit(s);
247}
248
249static void qemu_laio_completion_cb(EventNotifier *e)
250{
251 LinuxAioState *s = container_of(e, LinuxAioState, e);
252
253 if (event_notifier_test_and_clear(&s->e)) {
254 qemu_laio_process_completions_and_submit(s);
255 }
256}
257
258static void laio_cancel(BlockAIOCB *blockacb)
259{
260 struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
261 struct io_event event;
262 int ret;
263
264 if (laiocb->ret != -EINPROGRESS) {
265 return;
266 }
267 ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
268 laiocb->ret = -ECANCELED;
269 if (ret != 0) {
270
271 return;
272 }
273
274 laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
275}
276
277static const AIOCBInfo laio_aiocb_info = {
278 .aiocb_size = sizeof(struct qemu_laiocb),
279 .cancel_async = laio_cancel,
280};
281
282static void ioq_init(LaioQueue *io_q)
283{
284 QSIMPLEQ_INIT(&io_q->pending);
285 io_q->plugged = 0;
286 io_q->in_queue = 0;
287 io_q->in_flight = 0;
288 io_q->blocked = false;
289}
290
291static void ioq_submit(LinuxAioState *s)
292{
293 int ret, len;
294 struct qemu_laiocb *aiocb;
295 struct iocb *iocbs[MAX_EVENTS];
296 QSIMPLEQ_HEAD(, qemu_laiocb) completed;
297
298 do {
299 if (s->io_q.in_flight >= MAX_EVENTS) {
300 break;
301 }
302 len = 0;
303 QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
304 iocbs[len++] = &aiocb->iocb;
305 if (s->io_q.in_flight + len >= MAX_EVENTS) {
306 break;
307 }
308 }
309
310 ret = io_submit(s->ctx, len, iocbs);
311 if (ret == -EAGAIN) {
312 break;
313 }
314 if (ret < 0) {
315
316 aiocb = QSIMPLEQ_FIRST(&s->io_q.pending);
317 QSIMPLEQ_REMOVE_HEAD(&s->io_q.pending, next);
318 s->io_q.in_queue--;
319 aiocb->ret = ret;
320 qemu_laio_process_completion(aiocb);
321 continue;
322 }
323
324 s->io_q.in_flight += ret;
325 s->io_q.in_queue -= ret;
326 aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
327 QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
328 } while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
329 s->io_q.blocked = (s->io_q.in_queue > 0);
330
331 if (s->io_q.in_flight) {
332
333
334 qemu_laio_process_completions(s);
335
336
337
338
339
340
341
342 }
343}
344
345void laio_io_plug(BlockDriverState *bs, LinuxAioState *s)
346{
347 s->io_q.plugged++;
348}
349
350void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s)
351{
352 assert(s->io_q.plugged);
353 if (--s->io_q.plugged == 0 &&
354 !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
355 ioq_submit(s);
356 }
357}
358
359static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,
360 int type)
361{
362 LinuxAioState *s = laiocb->ctx;
363 struct iocb *iocbs = &laiocb->iocb;
364 QEMUIOVector *qiov = laiocb->qiov;
365
366 switch (type) {
367 case QEMU_AIO_WRITE:
368 io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
369 break;
370 case QEMU_AIO_READ:
371 io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
372 break;
373
374 default:
375 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
376 __func__, type);
377 return -EIO;
378 }
379 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
380
381 QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
382 s->io_q.in_queue++;
383 if (!s->io_q.blocked &&
384 (!s->io_q.plugged ||
385 s->io_q.in_flight + s->io_q.in_queue >= MAX_EVENTS)) {
386 ioq_submit(s);
387 }
388
389 return 0;
390}
391
392int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
393 uint64_t offset, QEMUIOVector *qiov, int type)
394{
395 int ret;
396 struct qemu_laiocb laiocb = {
397 .co = qemu_coroutine_self(),
398 .nbytes = qiov->size,
399 .ctx = s,
400 .ret = -EINPROGRESS,
401 .is_read = (type == QEMU_AIO_READ),
402 .qiov = qiov,
403 };
404
405 ret = laio_do_submit(fd, &laiocb, offset, type);
406 if (ret < 0) {
407 return ret;
408 }
409
410 if (laiocb.ret == -EINPROGRESS) {
411 qemu_coroutine_yield();
412 }
413 return laiocb.ret;
414}
415
416BlockAIOCB *laio_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
417 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
418 BlockCompletionFunc *cb, void *opaque, int type)
419{
420 struct qemu_laiocb *laiocb;
421 off_t offset = sector_num * BDRV_SECTOR_SIZE;
422 int ret;
423
424 laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque);
425 laiocb->nbytes = nb_sectors * BDRV_SECTOR_SIZE;
426 laiocb->ctx = s;
427 laiocb->ret = -EINPROGRESS;
428 laiocb->is_read = (type == QEMU_AIO_READ);
429 laiocb->qiov = qiov;
430
431 ret = laio_do_submit(fd, laiocb, offset, type);
432 if (ret < 0) {
433 qemu_aio_unref(laiocb);
434 return NULL;
435 }
436
437 return &laiocb->common;
438}
439
440void laio_detach_aio_context(LinuxAioState *s, AioContext *old_context)
441{
442 aio_set_event_notifier(old_context, &s->e, false, NULL);
443 qemu_bh_delete(s->completion_bh);
444}
445
446void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
447{
448 s->aio_context = new_context;
449 s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
450 aio_set_event_notifier(new_context, &s->e, false,
451 qemu_laio_completion_cb);
452}
453
454LinuxAioState *laio_init(void)
455{
456 LinuxAioState *s;
457
458 s = g_malloc0(sizeof(*s));
459 if (event_notifier_init(&s->e, false) < 0) {
460 goto out_free_state;
461 }
462
463 if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
464 goto out_close_efd;
465 }
466
467 ioq_init(&s->io_q);
468
469 return s;
470
471out_close_efd:
472 event_notifier_cleanup(&s->e);
473out_free_state:
474 g_free(s);
475 return NULL;
476}
477
478void laio_cleanup(LinuxAioState *s)
479{
480 event_notifier_cleanup(&s->e);
481
482 if (io_destroy(s->ctx) != 0) {
483 fprintf(stderr, "%s: destroy AIO context %p failed\n",
484 __func__, &s->ctx);
485 }
486 g_free(s);
487}
488