Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 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 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 10 | #include "qemu/osdep.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 11 | #include "block/aio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 12 | #include "qemu/queue.h" |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 13 | #include "block/block.h" |
Paolo Bonzini | 9f8540e | 2012-06-09 10:57:37 +0200 | [diff] [blame] | 14 | #include "block/raw-aio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 15 | #include "qemu/event_notifier.h" |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 16 | #include "qemu/coroutine.h" |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 17 | #include "qapi/error.h" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 18 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 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 | */ |
Wangyong | 2558cb8 | 2020-01-07 06:01:01 +0000 | [diff] [blame] | 29 | #define MAX_EVENTS 1024 |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 30 | |
| 31 | struct qemu_laiocb { |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 32 | Coroutine *co; |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 33 | LinuxAioState *ctx; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 34 | struct iocb iocb; |
| 35 | ssize_t ret; |
| 36 | size_t nbytes; |
Kevin Wolf | b161e2e | 2011-10-13 15:42:52 +0200 | [diff] [blame] | 37 | QEMUIOVector *qiov; |
| 38 | bool is_read; |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 39 | QSIMPLEQ_ENTRY(qemu_laiocb) next; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 42 | typedef struct { |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 43 | int plugged; |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 44 | unsigned int in_queue; |
| 45 | unsigned int in_flight; |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 46 | bool blocked; |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 47 | QSIMPLEQ_HEAD(, qemu_laiocb) pending; |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 48 | } LaioQueue; |
| 49 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 50 | struct LinuxAioState { |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 51 | AioContext *aio_context; |
| 52 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 53 | io_context_t ctx; |
Paolo Bonzini | c90caf2 | 2012-02-24 08:39:02 +0100 | [diff] [blame] | 54 | EventNotifier e; |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 55 | |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 56 | /* io queue for submit at batch. Protected by AioContext lock. */ |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 57 | LaioQueue io_q; |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 58 | |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 59 | /* I/O completion processing. Only runs in I/O thread. */ |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 60 | QEMUBH *completion_bh; |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 61 | int event_idx; |
| 62 | int event_max; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 65 | static void ioq_submit(LinuxAioState *s); |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 66 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 67 | static inline ssize_t io_event_ret(struct io_event *ev) |
| 68 | { |
| 69 | return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res); |
| 70 | } |
| 71 | |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 72 | /* |
Julia Suvorova | 2b02fd8 | 2019-06-02 23:17:09 +0300 | [diff] [blame] | 73 | * Completes an AIO request. |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 74 | */ |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 75 | static void qemu_laio_process_completion(struct qemu_laiocb *laiocb) |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 76 | { |
| 77 | int ret; |
| 78 | |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 79 | ret = laiocb->ret; |
| 80 | if (ret != -ECANCELED) { |
Kevin Wolf | b161e2e | 2011-10-13 15:42:52 +0200 | [diff] [blame] | 81 | if (ret == laiocb->nbytes) { |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 82 | ret = 0; |
Kevin Wolf | b161e2e | 2011-10-13 15:42:52 +0200 | [diff] [blame] | 83 | } else if (ret >= 0) { |
| 84 | /* Short reads mean EOF, pad with zeros. */ |
| 85 | if (laiocb->is_read) { |
Michael Tokarev | 3d9b492 | 2012-03-10 16:54:23 +0400 | [diff] [blame] | 86 | qemu_iovec_memset(laiocb->qiov, ret, 0, |
| 87 | laiocb->qiov->size - ret); |
Kevin Wolf | b161e2e | 2011-10-13 15:42:52 +0200 | [diff] [blame] | 88 | } else { |
Denis V. Lunev | 1c42f14 | 2016-06-23 14:37:16 +0300 | [diff] [blame] | 89 | ret = -ENOSPC; |
Kevin Wolf | b161e2e | 2011-10-13 15:42:52 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 94 | laiocb->ret = ret; |
Julia Suvorova | 2b02fd8 | 2019-06-02 23:17:09 +0300 | [diff] [blame] | 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); |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 104 | } |
Kevin Wolf | db0ffc2 | 2009-10-22 17:54:41 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 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 | */ |
| 113 | struct 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 | |
Philippe Mathieu-Daudé | f7795e4 | 2020-03-04 16:38:15 +0100 | [diff] [blame] | 124 | struct io_event io_events[]; |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 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 | */ |
| 137 | static 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 | */ |
| 160 | static 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 | */ |
| 177 | static inline unsigned int |
| 178 | io_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 | |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 186 | /** |
| 187 | * qemu_laio_process_completions: |
| 188 | * @s: AIO state |
| 189 | * |
| 190 | * Fetches completed I/O requests and invokes their callbacks. |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 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, |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 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. |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 197 | */ |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 198 | static void qemu_laio_process_completions(LinuxAioState *s) |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 199 | { |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 200 | struct io_event *events; |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 201 | |
| 202 | /* Reschedule so nested event loops see currently pending completions */ |
| 203 | qemu_bh_schedule(s->completion_bh); |
| 204 | |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 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 = |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 210 | container_of(iocb, struct qemu_laiocb, iocb); |
| 211 | |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 212 | laiocb->ret = io_event_ret(&events[s->event_idx]); |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 213 | |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 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 | } |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 219 | } |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 220 | |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 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; |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 228 | } |
Roman Pen | 9e909a5 | 2016-07-19 14:27:41 +0200 | [diff] [blame] | 229 | |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 230 | static void qemu_laio_process_completions_and_submit(LinuxAioState *s) |
| 231 | { |
Sergio Lopez | e091f0e | 2018-09-05 13:23:34 +0200 | [diff] [blame] | 232 | aio_context_acquire(s->aio_context); |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 233 | qemu_laio_process_completions(s); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 234 | |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 235 | if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) { |
| 236 | ioq_submit(s); |
| 237 | } |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 238 | aio_context_release(s->aio_context); |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 241 | static void qemu_laio_completion_bh(void *opaque) |
| 242 | { |
| 243 | LinuxAioState *s = opaque; |
| 244 | |
| 245 | qemu_laio_process_completions_and_submit(s); |
| 246 | } |
| 247 | |
Paolo Bonzini | c90caf2 | 2012-02-24 08:39:02 +0100 | [diff] [blame] | 248 | static void qemu_laio_completion_cb(EventNotifier *e) |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 249 | { |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 250 | LinuxAioState *s = container_of(e, LinuxAioState, e); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 251 | |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 252 | if (event_notifier_test_and_clear(&s->e)) { |
Roman Pen | 3407de5 | 2016-07-19 14:27:42 +0200 | [diff] [blame] | 253 | qemu_laio_process_completions_and_submit(s); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Stefan Hajnoczi | ee68697 | 2016-12-01 19:26:44 +0000 | [diff] [blame] | 257 | static 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 | |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 271 | static void ioq_init(LaioQueue *io_q) |
| 272 | { |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 273 | QSIMPLEQ_INIT(&io_q->pending); |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 274 | io_q->plugged = 0; |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 275 | io_q->in_queue = 0; |
| 276 | io_q->in_flight = 0; |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 277 | io_q->blocked = false; |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 278 | } |
| 279 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 280 | static void ioq_submit(LinuxAioState *s) |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 281 | { |
Paolo Bonzini | 82595da | 2014-12-11 14:52:30 +0100 | [diff] [blame] | 282 | int ret, len; |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 283 | struct qemu_laiocb *aiocb; |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 284 | struct iocb *iocbs[MAX_EVENTS]; |
Paolo Bonzini | 82595da | 2014-12-11 14:52:30 +0100 | [diff] [blame] | 285 | QSIMPLEQ_HEAD(, qemu_laiocb) completed; |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 286 | |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 287 | do { |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 288 | if (s->io_q.in_flight >= MAX_EVENTS) { |
| 289 | break; |
| 290 | } |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 291 | len = 0; |
| 292 | QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) { |
| 293 | iocbs[len++] = &aiocb->iocb; |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 294 | if (s->io_q.in_flight + len >= MAX_EVENTS) { |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 295 | break; |
| 296 | } |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 297 | } |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 298 | |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 299 | ret = io_submit(s->ctx, len, iocbs); |
| 300 | if (ret == -EAGAIN) { |
Paolo Bonzini | 82595da | 2014-12-11 14:52:30 +0100 | [diff] [blame] | 301 | break; |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 302 | } |
| 303 | if (ret < 0) { |
Kevin Wolf | 44713c9 | 2016-08-09 13:20:19 +0200 | [diff] [blame] | 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; |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 311 | } |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 312 | |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 313 | s->io_q.in_flight += ret; |
| 314 | s->io_q.in_queue -= ret; |
Paolo Bonzini | 82595da | 2014-12-11 14:52:30 +0100 | [diff] [blame] | 315 | aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb); |
| 316 | QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed); |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 317 | } while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending)); |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 318 | s->io_q.blocked = (s->io_q.in_queue > 0); |
Roman Pen | 0ed93d8 | 2016-07-19 14:27:43 +0200 | [diff] [blame] | 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 | } |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 334 | void laio_io_plug(BlockDriverState *bs, LinuxAioState *s) |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 335 | { |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 336 | s->io_q.plugged++; |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 337 | } |
| 338 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 339 | void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s) |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 340 | { |
Paolo Bonzini | 6b98bd6 | 2016-04-07 18:33:34 +0200 | [diff] [blame] | 341 | assert(s->io_q.plugged); |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 342 | if (--s->io_q.plugged == 0 && |
| 343 | !s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) { |
Paolo Bonzini | de35464 | 2014-12-11 14:52:29 +0100 | [diff] [blame] | 344 | ioq_submit(s); |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 345 | } |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 346 | } |
| 347 | |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 348 | static int laio_do_submit(int fd, struct qemu_laiocb *laiocb, off_t offset, |
| 349 | int type) |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 350 | { |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 351 | LinuxAioState *s = laiocb->ctx; |
| 352 | struct iocb *iocbs = &laiocb->iocb; |
| 353 | QEMUIOVector *qiov = laiocb->qiov; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 354 | |
| 355 | switch (type) { |
| 356 | case QEMU_AIO_WRITE: |
| 357 | io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset); |
Paolo Bonzini | 7d37435 | 2018-12-13 23:37:37 +0100 | [diff] [blame] | 358 | break; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 359 | case QEMU_AIO_READ: |
| 360 | io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset); |
Paolo Bonzini | 7d37435 | 2018-12-13 23:37:37 +0100 | [diff] [blame] | 361 | break; |
Frediano Ziglio | c30e624 | 2011-08-30 09:46:11 +0200 | [diff] [blame] | 362 | /* Currently Linux kernel does not support other operations */ |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 363 | default: |
| 364 | fprintf(stderr, "%s: invalid AIO request type 0x%x.\n", |
| 365 | __func__, type); |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 366 | return -EIO; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 367 | } |
Paolo Bonzini | c90caf2 | 2012-02-24 08:39:02 +0100 | [diff] [blame] | 368 | io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e)); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 369 | |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 370 | QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next); |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 371 | s->io_q.in_queue++; |
Paolo Bonzini | 43f2376 | 2014-12-11 14:52:27 +0100 | [diff] [blame] | 372 | if (!s->io_q.blocked && |
Roman Pen | 5e1b34a | 2016-07-13 15:03:24 +0200 | [diff] [blame] | 373 | (!s->io_q.plugged || |
| 374 | s->io_q.in_flight + s->io_q.in_queue >= MAX_EVENTS)) { |
Paolo Bonzini | 28b2408 | 2014-12-11 14:52:26 +0100 | [diff] [blame] | 375 | ioq_submit(s); |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 376 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 377 | |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, int fd, |
Kevin Wolf | 9d52aa3 | 2016-06-03 17:36:27 +0200 | [diff] [blame] | 382 | uint64_t offset, QEMUIOVector *qiov, int type) |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 383 | { |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 384 | int ret; |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 385 | struct qemu_laiocb laiocb = { |
| 386 | .co = qemu_coroutine_self(), |
Kevin Wolf | 9d52aa3 | 2016-06-03 17:36:27 +0200 | [diff] [blame] | 387 | .nbytes = qiov->size, |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 388 | .ctx = s, |
Roman Pen | 0ed93d8 | 2016-07-19 14:27:43 +0200 | [diff] [blame] | 389 | .ret = -EINPROGRESS, |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 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 | |
Roman Pen | 0ed93d8 | 2016-07-19 14:27:43 +0200 | [diff] [blame] | 399 | if (laiocb.ret == -EINPROGRESS) { |
| 400 | qemu_coroutine_yield(); |
| 401 | } |
Kevin Wolf | 2174f12 | 2014-08-06 17:18:07 +0200 | [diff] [blame] | 402 | return laiocb.ret; |
| 403 | } |
| 404 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 405 | void laio_detach_aio_context(LinuxAioState *s, AioContext *old_context) |
Stefan Hajnoczi | c2f3426 | 2014-05-08 16:34:47 +0200 | [diff] [blame] | 406 | { |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 407 | aio_set_event_notifier(old_context, &s->e, false, NULL, NULL); |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 408 | qemu_bh_delete(s->completion_bh); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 409 | s->aio_context = NULL; |
Stefan Hajnoczi | c2f3426 | 2014-05-08 16:34:47 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 412 | void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context) |
Stefan Hajnoczi | c2f3426 | 2014-05-08 16:34:47 +0200 | [diff] [blame] | 413 | { |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 414 | s->aio_context = new_context; |
Stefan Hajnoczi | 2cdff7f | 2014-08-04 16:56:33 +0100 | [diff] [blame] | 415 | s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s); |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 416 | aio_set_event_notifier(new_context, &s->e, false, |
Stefan Hajnoczi | ee68697 | 2016-12-01 19:26:44 +0000 | [diff] [blame] | 417 | qemu_laio_completion_cb, |
| 418 | qemu_laio_poll_cb); |
Stefan Hajnoczi | c2f3426 | 2014-05-08 16:34:47 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 421 | LinuxAioState *laio_init(Error **errp) |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 422 | { |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 423 | int rc; |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 424 | LinuxAioState *s; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 425 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 426 | s = g_malloc0(sizeof(*s)); |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 427 | rc = event_notifier_init(&s->e, false); |
| 428 | if (rc < 0) { |
| 429 | error_setg_errno(errp, -rc, "failed to to initialize event notifier"); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 430 | goto out_free_state; |
Paolo Bonzini | c90caf2 | 2012-02-24 08:39:02 +0100 | [diff] [blame] | 431 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 432 | |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 433 | rc = io_setup(MAX_EVENTS, &s->ctx); |
| 434 | if (rc < 0) { |
| 435 | error_setg_errno(errp, -rc, "failed to create linux AIO context"); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 436 | goto out_close_efd; |
Paolo Bonzini | c90caf2 | 2012-02-24 08:39:02 +0100 | [diff] [blame] | 437 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 438 | |
Ming Lei | 1b3abdc | 2014-07-04 18:04:34 +0800 | [diff] [blame] | 439 | ioq_init(&s->io_q); |
| 440 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 441 | return s; |
| 442 | |
| 443 | out_close_efd: |
Paolo Bonzini | c90caf2 | 2012-02-24 08:39:02 +0100 | [diff] [blame] | 444 | event_notifier_cleanup(&s->e); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 445 | out_free_state: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 446 | g_free(s); |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
Stefan Hajnoczi | abd269b | 2014-05-08 16:34:48 +0200 | [diff] [blame] | 449 | |
Paolo Bonzini | dd7f7ed | 2016-04-07 18:33:35 +0200 | [diff] [blame] | 450 | void laio_cleanup(LinuxAioState *s) |
Stefan Hajnoczi | abd269b | 2014-05-08 16:34:48 +0200 | [diff] [blame] | 451 | { |
Stefan Hajnoczi | abd269b | 2014-05-08 16:34:48 +0200 | [diff] [blame] | 452 | event_notifier_cleanup(&s->e); |
Gonglei | a1abf40 | 2014-07-12 11:43:37 +0800 | [diff] [blame] | 453 | |
| 454 | if (io_destroy(s->ctx) != 0) { |
| 455 | fprintf(stderr, "%s: destroy AIO context %p failed\n", |
| 456 | __func__, &s->ctx); |
| 457 | } |
Stefan Hajnoczi | abd269b | 2014-05-08 16:34:48 +0200 | [diff] [blame] | 458 | g_free(s); |
| 459 | } |