]> git.proxmox.com Git - mirror_qemu.git/blame - block/linux-aio.c
migration/postcopy: the valid condition is one less then end
[mirror_qemu.git] / block / linux-aio.c
CommitLineData
5c6c3a6c
CH
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 */
80c71a24 10#include "qemu/osdep.h"
737e150e 11#include "block/aio.h"
1de7afc9 12#include "qemu/queue.h"
2174f12b 13#include "block/block.h"
9f8540ec 14#include "block/raw-aio.h"
1de7afc9 15#include "qemu/event_notifier.h"
2174f12b 16#include "qemu/coroutine.h"
ed6e2161 17#include "qapi/error.h"
5c6c3a6c 18
5c6c3a6c
CH
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
31struct qemu_laiocb {
2174f12b 32 Coroutine *co;
dd7f7ed1 33 LinuxAioState *ctx;
5c6c3a6c
CH
34 struct iocb iocb;
35 ssize_t ret;
36 size_t nbytes;
b161e2e4
KW
37 QEMUIOVector *qiov;
38 bool is_read;
28b24087 39 QSIMPLEQ_ENTRY(qemu_laiocb) next;
5c6c3a6c
CH
40};
41
1b3abdcc 42typedef struct {
1b3abdcc 43 int plugged;
5e1b34a3
RP
44 unsigned int in_queue;
45 unsigned int in_flight;
43f2376e 46 bool blocked;
28b24087 47 QSIMPLEQ_HEAD(, qemu_laiocb) pending;
1b3abdcc
ML
48} LaioQueue;
49
dd7f7ed1 50struct LinuxAioState {
0187f5c9
PB
51 AioContext *aio_context;
52
5c6c3a6c 53 io_context_t ctx;
c90caf25 54 EventNotifier e;
1b3abdcc 55
1919631e 56 /* io queue for submit at batch. Protected by AioContext lock. */
1b3abdcc 57 LaioQueue io_q;
2cdff7f6 58
1919631e 59 /* I/O completion processing. Only runs in I/O thread. */
2cdff7f6 60 QEMUBH *completion_bh;
2cdff7f6
SH
61 int event_idx;
62 int event_max;
5c6c3a6c
CH
63};
64
dd7f7ed1 65static void ioq_submit(LinuxAioState *s);
28b24087 66
5c6c3a6c
CH
67static inline ssize_t io_event_ret(struct io_event *ev)
68{
69 return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
70}
71
db0ffc24 72/*
2b02fd81 73 * Completes an AIO request.
db0ffc24 74 */
dd7f7ed1 75static void qemu_laio_process_completion(struct qemu_laiocb *laiocb)
db0ffc24
KW
76{
77 int ret;
78
db0ffc24
KW
79 ret = laiocb->ret;
80 if (ret != -ECANCELED) {
b161e2e4 81 if (ret == laiocb->nbytes) {
db0ffc24 82 ret = 0;
b161e2e4
KW
83 } else if (ret >= 0) {
84 /* Short reads mean EOF, pad with zeros. */
85 if (laiocb->is_read) {
3d9b4925
MT
86 qemu_iovec_memset(laiocb->qiov, ret, 0,
87 laiocb->qiov->size - ret);
b161e2e4 88 } else {
1c42f149 89 ret = -ENOSPC;
b161e2e4
KW
90 }
91 }
db0ffc24
KW
92 }
93
2174f12b 94 laiocb->ret = ret;
2b02fd81
JS
95
96 /*
97 * If the coroutine is already entered it must be in ioq_submit() and
98 * will notice laio->ret has been filled in when it eventually runs
99 * later. Coroutines cannot be entered recursively so avoid doing
100 * that!
101 */
102 if (!qemu_coroutine_entered(laiocb->co)) {
103 aio_co_wake(laiocb->co);
2174f12b 104 }
db0ffc24
KW
105}
106
9e909a58
RP
107/**
108 * aio_ring buffer which is shared between userspace and kernel.
109 *
110 * This copied from linux/fs/aio.c, common header does not exist
111 * but AIO exists for ages so we assume ABI is stable.
112 */
113struct aio_ring {
114 unsigned id; /* kernel internal index number */
115 unsigned nr; /* number of io_events */
116 unsigned head; /* Written to by userland or by kernel. */
117 unsigned tail;
118
119 unsigned magic;
120 unsigned compat_features;
121 unsigned incompat_features;
122 unsigned header_length; /* size of aio_ring */
123
124 struct io_event io_events[0];
125};
126
127/**
128 * io_getevents_peek:
129 * @ctx: AIO context
130 * @events: pointer on events array, output value
131
132 * Returns the number of completed events and sets a pointer
133 * on events array. This function does not update the internal
134 * ring buffer, only reads head and tail. When @events has been
135 * processed io_getevents_commit() must be called.
136 */
137static inline unsigned int io_getevents_peek(io_context_t ctx,
138 struct io_event **events)
139{
140 struct aio_ring *ring = (struct aio_ring *)ctx;
141 unsigned int head = ring->head, tail = ring->tail;
142 unsigned int nr;
143
144 nr = tail >= head ? tail - head : ring->nr - head;
145 *events = ring->io_events + head;
146 /* To avoid speculative loads of s->events[i] before observing tail.
147 Paired with smp_wmb() inside linux/fs/aio.c: aio_complete(). */
148 smp_rmb();
149
150 return nr;
151}
152
153/**
154 * io_getevents_commit:
155 * @ctx: AIO context
156 * @nr: the number of events on which head should be advanced
157 *
158 * Advances head of a ring buffer.
159 */
160static inline void io_getevents_commit(io_context_t ctx, unsigned int nr)
161{
162 struct aio_ring *ring = (struct aio_ring *)ctx;
163
164 if (nr) {
165 ring->head = (ring->head + nr) % ring->nr;
166 }
167}
168
169/**
170 * io_getevents_advance_and_peek:
171 * @ctx: AIO context
172 * @events: pointer on events array, output value
173 * @nr: the number of events on which head should be advanced
174 *
175 * Advances head of a ring buffer and returns number of elements left.
176 */
177static inline unsigned int
178io_getevents_advance_and_peek(io_context_t ctx,
179 struct io_event **events,
180 unsigned int nr)
181{
182 io_getevents_commit(ctx, nr);
183 return io_getevents_peek(ctx, events);
184}
185
3407de57
RP
186/**
187 * qemu_laio_process_completions:
188 * @s: AIO state
189 *
190 * Fetches completed I/O requests and invokes their callbacks.
2cdff7f6
SH
191 *
192 * The function is somewhat tricky because it supports nested event loops, for
193 * example when a request callback invokes aio_poll(). In order to do this,
3407de57
RP
194 * indices are kept in LinuxAioState. Function schedules BH completion so it
195 * can be called again in a nested event loop. When there are no events left
196 * to complete the BH is being canceled.
2cdff7f6 197 */
3407de57 198static void qemu_laio_process_completions(LinuxAioState *s)
5c6c3a6c 199{
9e909a58 200 struct io_event *events;
5c6c3a6c 201
2cdff7f6
SH
202 /* Reschedule so nested event loops see currently pending completions */
203 qemu_bh_schedule(s->completion_bh);
5c6c3a6c 204
9e909a58
RP
205 while ((s->event_max = io_getevents_advance_and_peek(s->ctx, &events,
206 s->event_idx))) {
207 for (s->event_idx = 0; s->event_idx < s->event_max; ) {
208 struct iocb *iocb = events[s->event_idx].obj;
209 struct qemu_laiocb *laiocb =
2cdff7f6
SH
210 container_of(iocb, struct qemu_laiocb, iocb);
211
9e909a58 212 laiocb->ret = io_event_ret(&events[s->event_idx]);
2cdff7f6 213
9e909a58
RP
214 /* Change counters one-by-one because we can be nested. */
215 s->io_q.in_flight--;
216 s->event_idx++;
217 qemu_laio_process_completion(laiocb);
218 }
2cdff7f6 219 }
28b24087 220
9e909a58
RP
221 qemu_bh_cancel(s->completion_bh);
222
223 /* If we are nested we have to notify the level above that we are done
224 * by setting event_max to zero, upper level will then jump out of it's
225 * own `for` loop. If we are the last all counters droped to zero. */
226 s->event_max = 0;
227 s->event_idx = 0;
3407de57 228}
9e909a58 229
3407de57
RP
230static void qemu_laio_process_completions_and_submit(LinuxAioState *s)
231{
e091f0e9 232 aio_context_acquire(s->aio_context);
3407de57 233 qemu_laio_process_completions(s);
1919631e 234
28b24087
PB
235 if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
236 ioq_submit(s);
237 }
1919631e 238 aio_context_release(s->aio_context);
2cdff7f6
SH
239}
240
3407de57
RP
241static void qemu_laio_completion_bh(void *opaque)
242{
243 LinuxAioState *s = opaque;
244
245 qemu_laio_process_completions_and_submit(s);
246}
247
2cdff7f6
SH
248static void qemu_laio_completion_cb(EventNotifier *e)
249{
dd7f7ed1 250 LinuxAioState *s = container_of(e, LinuxAioState, e);
2cdff7f6
SH
251
252 if (event_notifier_test_and_clear(&s->e)) {
3407de57 253 qemu_laio_process_completions_and_submit(s);
5c6c3a6c
CH
254 }
255}
256
ee686975
SH
257static bool qemu_laio_poll_cb(void *opaque)
258{
259 EventNotifier *e = opaque;
260 LinuxAioState *s = container_of(e, LinuxAioState, e);
261 struct io_event *events;
262
263 if (!io_getevents_peek(s->ctx, &events)) {
264 return false;
265 }
266
267 qemu_laio_process_completions_and_submit(s);
268 return true;
269}
270
1b3abdcc
ML
271static void ioq_init(LaioQueue *io_q)
272{
28b24087 273 QSIMPLEQ_INIT(&io_q->pending);
1b3abdcc 274 io_q->plugged = 0;
5e1b34a3
RP
275 io_q->in_queue = 0;
276 io_q->in_flight = 0;
43f2376e 277 io_q->blocked = false;
1b3abdcc
ML
278}
279
dd7f7ed1 280static void ioq_submit(LinuxAioState *s)
1b3abdcc 281{
82595da8 282 int ret, len;
28b24087 283 struct qemu_laiocb *aiocb;
5e1b34a3 284 struct iocb *iocbs[MAX_EVENTS];
82595da8 285 QSIMPLEQ_HEAD(, qemu_laiocb) completed;
1b3abdcc 286
43f2376e 287 do {
5e1b34a3
RP
288 if (s->io_q.in_flight >= MAX_EVENTS) {
289 break;
290 }
43f2376e
PB
291 len = 0;
292 QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
293 iocbs[len++] = &aiocb->iocb;
5e1b34a3 294 if (s->io_q.in_flight + len >= MAX_EVENTS) {
43f2376e
PB
295 break;
296 }
28b24087 297 }
1b3abdcc 298
43f2376e
PB
299 ret = io_submit(s->ctx, len, iocbs);
300 if (ret == -EAGAIN) {
82595da8 301 break;
43f2376e
PB
302 }
303 if (ret < 0) {
44713c9e
KW
304 /* Fail the first request, retry the rest */
305 aiocb = QSIMPLEQ_FIRST(&s->io_q.pending);
306 QSIMPLEQ_REMOVE_HEAD(&s->io_q.pending, next);
307 s->io_q.in_queue--;
308 aiocb->ret = ret;
309 qemu_laio_process_completion(aiocb);
310 continue;
43f2376e
PB
311 }
312
5e1b34a3
RP
313 s->io_q.in_flight += ret;
314 s->io_q.in_queue -= ret;
82595da8
PB
315 aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
316 QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
43f2376e 317 } while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
5e1b34a3 318 s->io_q.blocked = (s->io_q.in_queue > 0);
0ed93d84
RP
319
320 if (s->io_q.in_flight) {
321 /* We can try to complete something just right away if there are
322 * still requests in-flight. */
323 qemu_laio_process_completions(s);
324 /*
325 * Even we have completed everything (in_flight == 0), the queue can
326 * have still pended requests (in_queue > 0). We do not attempt to
327 * repeat submission to avoid IO hang. The reason is simple: s->e is
328 * still set and completion callback will be called shortly and all
329 * pended requests will be submitted from there.
330 */
331 }
1b3abdcc
ML
332}
333
dd7f7ed1 334void laio_io_plug(BlockDriverState *bs, LinuxAioState *s)
1b3abdcc 335{
0187f5c9 336 s->io_q.plugged++;
1b3abdcc
ML
337}
338
dd7f7ed1 339void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s)
1b3abdcc 340{
6b98bd64 341 assert(s->io_q.plugged);
0187f5c9
PB
342 if (--s->io_q.plugged == 0 &&
343 !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
de354644 344 ioq_submit(s);
1b3abdcc 345 }
1b3abdcc
ML
346}
347
2174f12b
KW
348static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,
349 int type)
5c6c3a6c 350{
2174f12b
KW
351 LinuxAioState *s = laiocb->ctx;
352 struct iocb *iocbs = &laiocb->iocb;
353 QEMUIOVector *qiov = laiocb->qiov;
5c6c3a6c
CH
354
355 switch (type) {
356 case QEMU_AIO_WRITE:
357 io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
7d37435b 358 break;
5c6c3a6c
CH
359 case QEMU_AIO_READ:
360 io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
7d37435b 361 break;
c30e624d 362 /* Currently Linux kernel does not support other operations */
5c6c3a6c
CH
363 default:
364 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
365 __func__, type);
2174f12b 366 return -EIO;
5c6c3a6c 367 }
c90caf25 368 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
5c6c3a6c 369
28b24087 370 QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
5e1b34a3 371 s->io_q.in_queue++;
43f2376e 372 if (!s->io_q.blocked &&
5e1b34a3
RP
373 (!s->io_q.plugged ||
374 s->io_q.in_flight + s->io_q.in_queue >= MAX_EVENTS)) {
28b24087 375 ioq_submit(s);
1b3abdcc 376 }
5c6c3a6c 377
2174f12b
KW
378 return 0;
379}
380
381int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
9d52aa3c 382 uint64_t offset, QEMUIOVector *qiov, int type)
2174f12b 383{
2174f12b 384 int ret;
2174f12b
KW
385 struct qemu_laiocb laiocb = {
386 .co = qemu_coroutine_self(),
9d52aa3c 387 .nbytes = qiov->size,
2174f12b 388 .ctx = s,
0ed93d84 389 .ret = -EINPROGRESS,
2174f12b
KW
390 .is_read = (type == QEMU_AIO_READ),
391 .qiov = qiov,
392 };
393
394 ret = laio_do_submit(fd, &laiocb, offset, type);
395 if (ret < 0) {
396 return ret;
397 }
398
0ed93d84
RP
399 if (laiocb.ret == -EINPROGRESS) {
400 qemu_coroutine_yield();
401 }
2174f12b
KW
402 return laiocb.ret;
403}
404
dd7f7ed1 405void laio_detach_aio_context(LinuxAioState *s, AioContext *old_context)
c2f3426c 406{
f6a51c84 407 aio_set_event_notifier(old_context, &s->e, false, NULL, NULL);
2cdff7f6 408 qemu_bh_delete(s->completion_bh);
1919631e 409 s->aio_context = NULL;
c2f3426c
SH
410}
411
dd7f7ed1 412void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
c2f3426c 413{
0187f5c9 414 s->aio_context = new_context;
2cdff7f6 415 s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
dca21ef2 416 aio_set_event_notifier(new_context, &s->e, false,
ee686975
SH
417 qemu_laio_completion_cb,
418 qemu_laio_poll_cb);
c2f3426c
SH
419}
420
ed6e2161 421LinuxAioState *laio_init(Error **errp)
5c6c3a6c 422{
ed6e2161 423 int rc;
dd7f7ed1 424 LinuxAioState *s;
5c6c3a6c 425
7267c094 426 s = g_malloc0(sizeof(*s));
ed6e2161
NA
427 rc = event_notifier_init(&s->e, false);
428 if (rc < 0) {
429 error_setg_errno(errp, -rc, "failed to to initialize event notifier");
5c6c3a6c 430 goto out_free_state;
c90caf25 431 }
5c6c3a6c 432
ed6e2161
NA
433 rc = io_setup(MAX_EVENTS, &s->ctx);
434 if (rc < 0) {
435 error_setg_errno(errp, -rc, "failed to create linux AIO context");
5c6c3a6c 436 goto out_close_efd;
c90caf25 437 }
5c6c3a6c 438
1b3abdcc
ML
439 ioq_init(&s->io_q);
440
5c6c3a6c
CH
441 return s;
442
443out_close_efd:
c90caf25 444 event_notifier_cleanup(&s->e);
5c6c3a6c 445out_free_state:
7267c094 446 g_free(s);
5c6c3a6c
CH
447 return NULL;
448}
abd269b7 449
dd7f7ed1 450void laio_cleanup(LinuxAioState *s)
abd269b7 451{
abd269b7 452 event_notifier_cleanup(&s->e);
a1abf40d
GA
453
454 if (io_destroy(s->ctx) != 0) {
455 fprintf(stderr, "%s: destroy AIO context %p failed\n",
456 __func__, &s->ctx);
457 }
abd269b7
SH
458 g_free(s);
459}