blob: 5c104bd3cd407b96ac054f812843541f89c1f881 [file] [log] [blame]
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001/*
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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020011#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010012#include "block/aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010013#include "qemu/queue.h"
Kevin Wolf2174f122014-08-06 17:18:07 +020014#include "block/block.h"
Paolo Bonzini9f8540e2012-06-09 10:57:37 +020015#include "block/raw-aio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/event_notifier.h"
Kevin Wolf2174f122014-08-06 17:18:07 +020017#include "qemu/coroutine.h"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020018
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020019#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
Ming Lei1b3abdc2014-07-04 18:04:34 +080031#define MAX_QUEUED_IO 128
32
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020033struct qemu_laiocb {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020034 BlockAIOCB common;
Kevin Wolf2174f122014-08-06 17:18:07 +020035 Coroutine *co;
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +020036 LinuxAioState *ctx;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020037 struct iocb iocb;
38 ssize_t ret;
39 size_t nbytes;
Kevin Wolfb161e2e2011-10-13 15:42:52 +020040 QEMUIOVector *qiov;
41 bool is_read;
Paolo Bonzini28b24082014-12-11 14:52:26 +010042 QSIMPLEQ_ENTRY(qemu_laiocb) next;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020043};
44
Ming Lei1b3abdc2014-07-04 18:04:34 +080045typedef struct {
Ming Lei1b3abdc2014-07-04 18:04:34 +080046 int plugged;
Paolo Bonzini8455ce02014-12-11 14:52:28 +010047 unsigned int n;
Paolo Bonzini43f23762014-12-11 14:52:27 +010048 bool blocked;
Paolo Bonzini28b24082014-12-11 14:52:26 +010049 QSIMPLEQ_HEAD(, qemu_laiocb) pending;
Ming Lei1b3abdc2014-07-04 18:04:34 +080050} LaioQueue;
51
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +020052struct LinuxAioState {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020053 io_context_t ctx;
Paolo Bonzinic90caf22012-02-24 08:39:02 +010054 EventNotifier e;
Ming Lei1b3abdc2014-07-04 18:04:34 +080055
56 /* io queue for submit at batch */
57 LaioQueue io_q;
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +010058
59 /* I/O completion processing */
60 QEMUBH *completion_bh;
61 struct io_event events[MAX_EVENTS];
62 int event_idx;
63 int event_max;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020064};
65
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +020066static void ioq_submit(LinuxAioState *s);
Paolo Bonzini28b24082014-12-11 14:52:26 +010067
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020068static inline ssize_t io_event_ret(struct io_event *ev)
69{
70 return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
71}
72
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020073/*
74 * Completes an AIO request (calls the callback and frees the ACB).
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020075 */
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +020076static void qemu_laio_process_completion(struct qemu_laiocb *laiocb)
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020077{
78 int ret;
79
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020080 ret = laiocb->ret;
81 if (ret != -ECANCELED) {
Kevin Wolfb161e2e2011-10-13 15:42:52 +020082 if (ret == laiocb->nbytes) {
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020083 ret = 0;
Kevin Wolfb161e2e2011-10-13 15:42:52 +020084 } else if (ret >= 0) {
85 /* Short reads mean EOF, pad with zeros. */
86 if (laiocb->is_read) {
Michael Tokarev3d9b4922012-03-10 16:54:23 +040087 qemu_iovec_memset(laiocb->qiov, ret, 0,
88 laiocb->qiov->size - ret);
Kevin Wolfb161e2e2011-10-13 15:42:52 +020089 } else {
Denis V. Lunev1c42f142016-06-23 14:37:16 +030090 ret = -ENOSPC;
Kevin Wolfb161e2e2011-10-13 15:42:52 +020091 }
92 }
Kevin Wolfdb0ffc22009-10-22 17:54:41 +020093 }
94
Kevin Wolf2174f122014-08-06 17:18:07 +020095 laiocb->ret = ret;
96 if (laiocb->co) {
Paolo Bonzini0b8b8752016-07-04 19:10:01 +020097 qemu_coroutine_enter(laiocb->co);
Kevin Wolf2174f122014-08-06 17:18:07 +020098 } else {
99 laiocb->common.cb(laiocb->common.opaque, ret);
100 qemu_aio_unref(laiocb);
101 }
Kevin Wolfdb0ffc22009-10-22 17:54:41 +0200102}
103
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100104/* The completion BH fetches completed I/O requests and invokes their
105 * callbacks.
106 *
107 * The function is somewhat tricky because it supports nested event loops, for
108 * example when a request callback invokes aio_poll(). In order to do this,
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200109 * the completion events array and index are kept in LinuxAioState. The BH
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100110 * reschedules itself as long as there are completions pending so it will
111 * either be called again in a nested event loop or will be called after all
112 * events have been completed. When there are no events left to complete, the
113 * BH returns without rescheduling.
114 */
115static void qemu_laio_completion_bh(void *opaque)
116{
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200117 LinuxAioState *s = opaque;
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100118
119 /* Fetch more completion events when empty */
120 if (s->event_idx == s->event_max) {
121 do {
122 struct timespec ts = { 0 };
123 s->event_max = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS,
124 s->events, &ts);
125 } while (s->event_max == -EINTR);
126
127 s->event_idx = 0;
128 if (s->event_max <= 0) {
129 s->event_max = 0;
130 return; /* no more events */
131 }
132 }
133
134 /* Reschedule so nested event loops see currently pending completions */
135 qemu_bh_schedule(s->completion_bh);
136
137 /* Process completion events */
138 while (s->event_idx < s->event_max) {
139 struct iocb *iocb = s->events[s->event_idx].obj;
140 struct qemu_laiocb *laiocb =
141 container_of(iocb, struct qemu_laiocb, iocb);
142
143 laiocb->ret = io_event_ret(&s->events[s->event_idx]);
144 s->event_idx++;
145
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200146 qemu_laio_process_completion(laiocb);
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100147 }
Paolo Bonzini28b24082014-12-11 14:52:26 +0100148
149 if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
150 ioq_submit(s);
151 }
Kevin Wolfccb9dc12014-11-28 15:23:12 +0100152
153 qemu_bh_cancel(s->completion_bh);
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100154}
155
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100156static void qemu_laio_completion_cb(EventNotifier *e)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200157{
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200158 LinuxAioState *s = container_of(e, LinuxAioState, e);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200159
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100160 if (event_notifier_test_and_clear(&s->e)) {
Kevin Wolfccb9dc12014-11-28 15:23:12 +0100161 qemu_laio_completion_bh(s);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200162 }
163}
164
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200165static void laio_cancel(BlockAIOCB *blockacb)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200166{
167 struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
168 struct io_event event;
169 int ret;
170
Fam Zheng771b64d2014-09-11 13:41:13 +0800171 if (laiocb->ret != -EINPROGRESS) {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200172 return;
Fam Zheng771b64d2014-09-11 13:41:13 +0800173 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200174 ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
Fam Zheng771b64d2014-09-11 13:41:13 +0800175 laiocb->ret = -ECANCELED;
176 if (ret != 0) {
177 /* iocb is not cancelled, cb will be called by the event loop later */
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200178 return;
179 }
180
Fam Zheng771b64d2014-09-11 13:41:13 +0800181 laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200182}
183
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100184static const AIOCBInfo laio_aiocb_info = {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200185 .aiocb_size = sizeof(struct qemu_laiocb),
Fam Zheng771b64d2014-09-11 13:41:13 +0800186 .cancel_async = laio_cancel,
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200187};
188
Ming Lei1b3abdc2014-07-04 18:04:34 +0800189static void ioq_init(LaioQueue *io_q)
190{
Paolo Bonzini28b24082014-12-11 14:52:26 +0100191 QSIMPLEQ_INIT(&io_q->pending);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800192 io_q->plugged = 0;
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100193 io_q->n = 0;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100194 io_q->blocked = false;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800195}
196
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200197static void ioq_submit(LinuxAioState *s)
Ming Lei1b3abdc2014-07-04 18:04:34 +0800198{
Paolo Bonzini82595da2014-12-11 14:52:30 +0100199 int ret, len;
Paolo Bonzini28b24082014-12-11 14:52:26 +0100200 struct qemu_laiocb *aiocb;
201 struct iocb *iocbs[MAX_QUEUED_IO];
Paolo Bonzini82595da2014-12-11 14:52:30 +0100202 QSIMPLEQ_HEAD(, qemu_laiocb) completed;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800203
Paolo Bonzini43f23762014-12-11 14:52:27 +0100204 do {
205 len = 0;
206 QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
207 iocbs[len++] = &aiocb->iocb;
208 if (len == MAX_QUEUED_IO) {
209 break;
210 }
Paolo Bonzini28b24082014-12-11 14:52:26 +0100211 }
Ming Lei1b3abdc2014-07-04 18:04:34 +0800212
Paolo Bonzini43f23762014-12-11 14:52:27 +0100213 ret = io_submit(s->ctx, len, iocbs);
214 if (ret == -EAGAIN) {
Paolo Bonzini82595da2014-12-11 14:52:30 +0100215 break;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100216 }
217 if (ret < 0) {
218 abort();
219 }
Ming Lei1b3abdc2014-07-04 18:04:34 +0800220
Paolo Bonzini82595da2014-12-11 14:52:30 +0100221 s->io_q.n -= ret;
222 aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
223 QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
Paolo Bonzini43f23762014-12-11 14:52:27 +0100224 } while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100225 s->io_q.blocked = (s->io_q.n > 0);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800226}
227
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200228void laio_io_plug(BlockDriverState *bs, LinuxAioState *s)
Ming Lei1b3abdc2014-07-04 18:04:34 +0800229{
Paolo Bonzini6b98bd62016-04-07 18:33:34 +0200230 assert(!s->io_q.plugged);
231 s->io_q.plugged = 1;
Ming Lei1b3abdc2014-07-04 18:04:34 +0800232}
233
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200234void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s)
Ming Lei1b3abdc2014-07-04 18:04:34 +0800235{
Paolo Bonzini6b98bd62016-04-07 18:33:34 +0200236 assert(s->io_q.plugged);
237 s->io_q.plugged = 0;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100238 if (!s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
Paolo Bonzinide354642014-12-11 14:52:29 +0100239 ioq_submit(s);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800240 }
Ming Lei1b3abdc2014-07-04 18:04:34 +0800241}
242
Kevin Wolf2174f122014-08-06 17:18:07 +0200243static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset,
244 int type)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200245{
Kevin Wolf2174f122014-08-06 17:18:07 +0200246 LinuxAioState *s = laiocb->ctx;
247 struct iocb *iocbs = &laiocb->iocb;
248 QEMUIOVector *qiov = laiocb->qiov;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200249
250 switch (type) {
251 case QEMU_AIO_WRITE:
252 io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
253 break;
254 case QEMU_AIO_READ:
255 io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
256 break;
Frediano Ziglioc30e6242011-08-30 09:46:11 +0200257 /* Currently Linux kernel does not support other operations */
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200258 default:
259 fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
260 __func__, type);
Kevin Wolf2174f122014-08-06 17:18:07 +0200261 return -EIO;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200262 }
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100263 io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200264
Paolo Bonzini28b24082014-12-11 14:52:26 +0100265 QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100266 s->io_q.n++;
Paolo Bonzini43f23762014-12-11 14:52:27 +0100267 if (!s->io_q.blocked &&
Paolo Bonzini8455ce02014-12-11 14:52:28 +0100268 (!s->io_q.plugged || s->io_q.n >= MAX_QUEUED_IO)) {
Paolo Bonzini28b24082014-12-11 14:52:26 +0100269 ioq_submit(s);
Ming Lei1b3abdc2014-07-04 18:04:34 +0800270 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200271
Kevin Wolf2174f122014-08-06 17:18:07 +0200272 return 0;
273}
274
275int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
Kevin Wolf9d52aa32016-06-03 17:36:27 +0200276 uint64_t offset, QEMUIOVector *qiov, int type)
Kevin Wolf2174f122014-08-06 17:18:07 +0200277{
Kevin Wolf2174f122014-08-06 17:18:07 +0200278 int ret;
Kevin Wolf2174f122014-08-06 17:18:07 +0200279 struct qemu_laiocb laiocb = {
280 .co = qemu_coroutine_self(),
Kevin Wolf9d52aa32016-06-03 17:36:27 +0200281 .nbytes = qiov->size,
Kevin Wolf2174f122014-08-06 17:18:07 +0200282 .ctx = s,
283 .is_read = (type == QEMU_AIO_READ),
284 .qiov = qiov,
285 };
286
287 ret = laio_do_submit(fd, &laiocb, offset, type);
288 if (ret < 0) {
289 return ret;
290 }
291
292 qemu_coroutine_yield();
293 return laiocb.ret;
294}
295
296BlockAIOCB *laio_submit(BlockDriverState *bs, LinuxAioState *s, int fd,
297 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
298 BlockCompletionFunc *cb, void *opaque, int type)
299{
300 struct qemu_laiocb *laiocb;
301 off_t offset = sector_num * BDRV_SECTOR_SIZE;
302 int ret;
303
304 laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque);
305 laiocb->nbytes = nb_sectors * BDRV_SECTOR_SIZE;
306 laiocb->ctx = s;
307 laiocb->ret = -EINPROGRESS;
308 laiocb->is_read = (type == QEMU_AIO_READ);
309 laiocb->qiov = qiov;
310
311 ret = laio_do_submit(fd, laiocb, offset, type);
312 if (ret < 0) {
313 qemu_aio_unref(laiocb);
314 return NULL;
315 }
316
317 return &laiocb->common;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200318}
319
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200320void laio_detach_aio_context(LinuxAioState *s, AioContext *old_context)
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200321{
Fam Zhengdca21ef2015-10-23 11:08:05 +0800322 aio_set_event_notifier(old_context, &s->e, false, NULL);
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100323 qemu_bh_delete(s->completion_bh);
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200324}
325
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200326void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context)
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200327{
Stefan Hajnoczi2cdff7f2014-08-04 16:56:33 +0100328 s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
Fam Zhengdca21ef2015-10-23 11:08:05 +0800329 aio_set_event_notifier(new_context, &s->e, false,
330 qemu_laio_completion_cb);
Stefan Hajnoczic2f34262014-05-08 16:34:47 +0200331}
332
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200333LinuxAioState *laio_init(void)
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200334{
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200335 LinuxAioState *s;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200336
Anthony Liguori7267c092011-08-20 22:09:37 -0500337 s = g_malloc0(sizeof(*s));
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100338 if (event_notifier_init(&s->e, false) < 0) {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200339 goto out_free_state;
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100340 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200341
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100342 if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200343 goto out_close_efd;
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100344 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200345
Ming Lei1b3abdc2014-07-04 18:04:34 +0800346 ioq_init(&s->io_q);
347
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200348 return s;
349
350out_close_efd:
Paolo Bonzinic90caf22012-02-24 08:39:02 +0100351 event_notifier_cleanup(&s->e);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200352out_free_state:
Anthony Liguori7267c092011-08-20 22:09:37 -0500353 g_free(s);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200354 return NULL;
355}
Stefan Hajnocziabd269b2014-05-08 16:34:48 +0200356
Paolo Bonzinidd7f7ed2016-04-07 18:33:35 +0200357void laio_cleanup(LinuxAioState *s)
Stefan Hajnocziabd269b2014-05-08 16:34:48 +0200358{
Stefan Hajnocziabd269b2014-05-08 16:34:48 +0200359 event_notifier_cleanup(&s->e);
Gongleia1abf402014-07-12 11:43:37 +0800360
361 if (io_destroy(s->ctx) != 0) {
362 fprintf(stderr, "%s: destroy AIO context %p failed\n",
363 __func__, &s->ctx);
364 }
Stefan Hajnocziabd269b2014-05-08 16:34:48 +0200365 g_free(s);
366}