]> git.proxmox.com Git - mirror_qemu.git/blob - block/linux-aio.c
block: explicitly acquire aiocontext in bottom halves that need it
[mirror_qemu.git] / block / linux-aio.c
1 /*
2 * Linux native AIO support.
3 *
4 * Copyright (C) 2009 IBM, Corp.
5 * Copyright (C) 2009 Red Hat, Inc.
6 *
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.
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 * Queue size (per-device).
23 *
24 * XXX: eventually we need to communicate this to the guest and/or make it
25 * tunable by the guest. If we get more outstanding requests at a time
26 * than this we will get EAGAIN from io_submit which is communicated to
27 * the guest as an I/O error.
28 */
29 #define MAX_EVENTS 128
30
31 struct 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
43 typedef 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
51 struct LinuxAioState {
52 AioContext *aio_context;
53
54 io_context_t ctx;
55 EventNotifier e;
56
57 /* io queue for submit at batch. Protected by AioContext lock. */
58 LaioQueue io_q;
59
60 /* I/O completion processing. Only runs in I/O thread. */
61 QEMUBH *completion_bh;
62 int event_idx;
63 int event_max;
64 };
65
66 static void ioq_submit(LinuxAioState *s);
67
68 static 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 * Completes an AIO request (calls the callback and frees the ACB).
75 */
76 static void qemu_laio_process_completion(struct qemu_laiocb *laiocb)
77 {
78 LinuxAioState *s = laiocb->ctx;
79 int ret;
80
81 ret = laiocb->ret;
82 if (ret != -ECANCELED) {
83 if (ret == laiocb->nbytes) {
84 ret = 0;
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);
90 } else {
91 ret = -ENOSPC;
92 }
93 }
94 }
95
96 laiocb->ret = ret;
97 aio_context_acquire(s->aio_context);
98 if (laiocb->co) {
99 /* If the coroutine is already entered it must be in ioq_submit() and
100 * will notice laio->ret has been filled in when it eventually runs
101 * later. Coroutines cannot be entered recursively so avoid doing
102 * that!
103 */
104 if (!qemu_coroutine_entered(laiocb->co)) {
105 qemu_coroutine_enter(laiocb->co);
106 }
107 } else {
108 laiocb->common.cb(laiocb->common.opaque, ret);
109 qemu_aio_unref(laiocb);
110 }
111 aio_context_release(s->aio_context);
112 }
113
114 /**
115 * aio_ring buffer which is shared between userspace and kernel.
116 *
117 * This copied from linux/fs/aio.c, common header does not exist
118 * but AIO exists for ages so we assume ABI is stable.
119 */
120 struct aio_ring {
121 unsigned id; /* kernel internal index number */
122 unsigned nr; /* number of io_events */
123 unsigned head; /* Written to by userland or by kernel. */
124 unsigned tail;
125
126 unsigned magic;
127 unsigned compat_features;
128 unsigned incompat_features;
129 unsigned header_length; /* size of aio_ring */
130
131 struct io_event io_events[0];
132 };
133
134 /**
135 * io_getevents_peek:
136 * @ctx: AIO context
137 * @events: pointer on events array, output value
138
139 * Returns the number of completed events and sets a pointer
140 * on events array. This function does not update the internal
141 * ring buffer, only reads head and tail. When @events has been
142 * processed io_getevents_commit() must be called.
143 */
144 static inline unsigned int io_getevents_peek(io_context_t ctx,
145 struct io_event **events)
146 {
147 struct aio_ring *ring = (struct aio_ring *)ctx;
148 unsigned int head = ring->head, tail = ring->tail;
149 unsigned int nr;
150
151 nr = tail >= head ? tail - head : ring->nr - head;
152 *events = ring->io_events + head;
153 /* To avoid speculative loads of s->events[i] before observing tail.
154 Paired with smp_wmb() inside linux/fs/aio.c: aio_complete(). */
155 smp_rmb();
156
157 return nr;
158 }
159
160 /**
161 * io_getevents_commit:
162 * @ctx: AIO context
163 * @nr: the number of events on which head should be advanced
164 *
165 * Advances head of a ring buffer.
166 */
167 static inline void io_getevents_commit(io_context_t ctx, unsigned int nr)
168 {
169 struct aio_ring *ring = (struct aio_ring *)ctx;
170
171 if (nr) {
172 ring->head = (ring->head + nr) % ring->nr;
173 }
174 }
175
176 /**
177 * io_getevents_advance_and_peek:
178 * @ctx: AIO context
179 * @events: pointer on events array, output value
180 * @nr: the number of events on which head should be advanced
181 *
182 * Advances head of a ring buffer and returns number of elements left.
183 */
184 static inline unsigned int
185 io_getevents_advance_and_peek(io_context_t ctx,
186 struct io_event **events,
187 unsigned int nr)
188 {
189 io_getevents_commit(ctx, nr);
190 return io_getevents_peek(ctx, events);
191 }
192
193 /**
194 * qemu_laio_process_completions:
195 * @s: AIO state
196 *
197 * Fetches completed I/O requests and invokes their callbacks.
198 *
199 * The function is somewhat tricky because it supports nested event loops, for
200 * example when a request callback invokes aio_poll(). In order to do this,
201 * indices are kept in LinuxAioState. Function schedules BH completion so it
202 * can be called again in a nested event loop. When there are no events left
203 * to complete the BH is being canceled.
204 */
205 static void qemu_laio_process_completions(LinuxAioState *s)
206 {
207 struct io_event *events;
208
209 /* Reschedule so nested event loops see currently pending completions */
210 qemu_bh_schedule(s->completion_bh);
211
212 while ((s->event_max = io_getevents_advance_and_peek(s->ctx, &events,
213 s->event_idx))) {
214 for (s->event_idx = 0; s->event_idx < s->event_max; ) {
215 struct iocb *iocb = events[s->event_idx].obj;
216 struct qemu_laiocb *laiocb =
217 container_of(iocb, struct qemu_laiocb, iocb);
218
219 laiocb->ret = io_event_ret(&events[s->event_idx]);
220
221 /* Change counters one-by-one because we can be nested. */
222 s->io_q.in_flight--;
223 s->event_idx++;
224 qemu_laio_process_completion(laiocb);
225 }
226 }
227
228 qemu_bh_cancel(s->completion_bh);
229
230 /* If we are nested we have to notify the level above that we are done
231 * by setting event_max to zero, upper level will then jump out of it's
232 * own `for` loop. If we are the last all counters droped to zero. */
233 s->event_max = 0;
234 s->event_idx = 0;
235 }
236
237 static void qemu_laio_process_completions_and_submit(LinuxAioState *s)
238 {
239 qemu_laio_process_completions(s);
240
241 aio_context_acquire(s->aio_context);
242 if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
243 ioq_submit(s);
244 }
245 aio_context_release(s->aio_context);
246 }
247
248 static void qemu_laio_completion_bh(void *opaque)
249 {
250 LinuxAioState *s = opaque;
251
252 qemu_laio_process_completions_and_submit(s);
253 }
254
255 static void qemu_laio_completion_cb(EventNotifier *e)
256 {
257 LinuxAioState *s = container_of(e, LinuxAioState, e);
258
259 if (event_notifier_test_and_clear(&s->e)) {
260 qemu_laio_process_completions_and_submit(s);
261 }
262 }
263
264 static bool qemu_laio_poll_cb(void *opaque)
265 {
266 EventNotifier *e = opaque;
267 LinuxAioState *s = container_of(e, LinuxAioState, e);
268 struct io_event *events;
269
270 if (!io_getevents_peek(s->ctx, &events)) {
271 return false;
272 }
273
274 qemu_laio_process_completions_and_submit(s);
275 return true;
276 }
277
278 static void laio_cancel(BlockAIOCB *blockacb)
279 {
280 struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
281 struct io_event event;
282 int ret;
283
284 if (laiocb->ret != -EINPROGRESS) {
285 return;
286 }
287 ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
288 laiocb->ret = -ECANCELED;
289 if (ret != 0) {
290 /* iocb is not cancelled, cb will be called by the event loop later */
291 return;
292 }
293
294 laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
295 }
296
297 static const AIOCBInfo laio_aiocb_info = {
298 .aiocb_size = sizeof(struct qemu_laiocb),
299 .cancel_async = laio_cancel,
300 };
301
302 static void ioq_init(LaioQueue *io_q)
303 {
304 QSIMPLEQ_INIT(&io_q->pending);
305 io_q->plugged = 0;
306 io_q->in_queue = 0;
307 io_q->in_flight = 0;
308 io_q->blocked = false;
309 }
310
311 static void ioq_submit(LinuxAioState *s)
312 {
313 int ret, len;
314 struct qemu_laiocb *aiocb;
315 struct iocb *iocbs[MAX_EVENTS];
316 QSIMPLEQ_HEAD(, qemu_laiocb) completed;
317
318 do {
319 if (s->io_q.in_flight >= MAX_EVENTS) {
320 break;
321 }
322 len = 0;
323 QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
324 iocbs[len++] = &aiocb->iocb;
325 if (s->io_q.in_flight + len >= MAX_EVENTS) {
326 break;
327 }
328 }
329
330 ret = io_submit(s->ctx, len, iocbs);
331 if (ret == -EAGAIN) {
332 break;
333 }
334 if (ret < 0) {
335 /* Fail the first request, retry the rest */
336 aiocb = QSIMPLEQ_FIRST(&s->io_q.pending);
337 QSIMPLEQ_REMOVE_HEAD(&s->io_q.pending, next);
338 s->io_q.in_queue--;
339 aiocb->ret = ret;
340 qemu_laio_process_completion(aiocb);
341 continue;
342 }
343
344 s->io_q.in_flight += ret;
345 s->io_q.in_queue -= ret;
346 aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
347 QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
348 } while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
349 s->io_q.blocked = (s->io_q.in_queue > 0);
350
351 if (s->io_q.in_flight) {
352 /* We can try to complete something just right away if there are
353 * still requests in-flight. */
354 qemu_laio_process_completions(s);
355 /*
356 * Even we have completed everything (in_flight == 0), the queue can
357 * have still pended requests (in_queue > 0). We do not attempt to
358 * repeat submission to avoid IO hang. The reason is simple: s->e is
359 * still set and completion callback will be called shortly and all
360 * pended requests will be submitted from there.
361 */
362 }
363 }
364
365 void laio_io_plug(BlockDriverState *bs, LinuxAioState *s)
366 {
367 s->io_q.plugged++;
368 }
369
370 void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s)
371 {
372 assert(s->io_q.plugged);
373 if (--s->io_q.plugged == 0 &&
374 !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
375 ioq_submit(s);
376 }
377 }
378
379 static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,
380 int type)
381 {
382 LinuxAioState *s = laiocb->ctx;
383 struct iocb *iocbs = &laiocb->iocb;
384 QEMUIOVector *qiov = laiocb->qiov;
385
386 switch (type) {
387 case QEMU_AIO_WRITE:
388 io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
389 break;
390 case QEMU_AIO_READ:
391 io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
392 break;
393 /* Currently Linux kernel does not support other operations */
394 default:
395 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
396 __func__, type);
397 return -EIO;
398 }
399 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
400
401 QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
402 s->io_q.in_queue++;
403 if (!s->io_q.blocked &&
404 (!s->io_q.plugged ||
405 s->io_q.in_flight + s->io_q.in_queue >= MAX_EVENTS)) {
406 ioq_submit(s);
407 }
408
409 return 0;
410 }
411
412 int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
413 uint64_t offset, QEMUIOVector *qiov, int type)
414 {
415 int ret;
416 struct qemu_laiocb laiocb = {
417 .co = qemu_coroutine_self(),
418 .nbytes = qiov->size,
419 .ctx = s,
420 .ret = -EINPROGRESS,
421 .is_read = (type == QEMU_AIO_READ),
422 .qiov = qiov,
423 };
424
425 ret = laio_do_submit(fd, &laiocb, offset, type);
426 if (ret < 0) {
427 return ret;
428 }
429
430 if (laiocb.ret == -EINPROGRESS) {
431 qemu_coroutine_yield();
432 }
433 return laiocb.ret;
434 }
435
436 BlockAIOCB *laio_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
437 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
438 BlockCompletionFunc *cb, void *opaque, int type)
439 {
440 struct qemu_laiocb *laiocb;
441 off_t offset = sector_num * BDRV_SECTOR_SIZE;
442 int ret;
443
444 laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque);
445 laiocb->nbytes = nb_sectors * BDRV_SECTOR_SIZE;
446 laiocb->ctx = s;
447 laiocb->ret = -EINPROGRESS;
448 laiocb->is_read = (type == QEMU_AIO_READ);
449 laiocb->qiov = qiov;
450
451 ret = laio_do_submit(fd, laiocb, offset, type);
452 if (ret < 0) {
453 qemu_aio_unref(laiocb);
454 return NULL;
455 }
456
457 return &laiocb->common;
458 }
459
460 void laio_detach_aio_context(LinuxAioState *s, AioContext *old_context)
461 {
462 aio_set_event_notifier(old_context, &s->e, false, NULL, NULL);
463 qemu_bh_delete(s->completion_bh);
464 s->aio_context = NULL;
465 }
466
467 void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
468 {
469 s->aio_context = new_context;
470 s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
471 aio_set_event_notifier(new_context, &s->e, false,
472 qemu_laio_completion_cb,
473 qemu_laio_poll_cb);
474 }
475
476 LinuxAioState *laio_init(void)
477 {
478 LinuxAioState *s;
479
480 s = g_malloc0(sizeof(*s));
481 if (event_notifier_init(&s->e, false) < 0) {
482 goto out_free_state;
483 }
484
485 if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
486 goto out_close_efd;
487 }
488
489 ioq_init(&s->io_q);
490
491 return s;
492
493 out_close_efd:
494 event_notifier_cleanup(&s->e);
495 out_free_state:
496 g_free(s);
497 return NULL;
498 }
499
500 void laio_cleanup(LinuxAioState *s)
501 {
502 event_notifier_cleanup(&s->e);
503
504 if (io_destroy(s->ctx) != 0) {
505 fprintf(stderr, "%s: destroy AIO context %p failed\n",
506 __func__, &s->ctx);
507 }
508 g_free(s);
509 }