meson: Fix to make QAPI generator output depend on main.py
[qemu/armbru.git] / block / io_uring.c
blob989f9a99ed8a1596675d2e50392d05398877fd60
1 /*
2 * Linux io_uring support.
4 * Copyright (C) 2009 IBM, Corp.
5 * Copyright (C) 2009 Red Hat, Inc.
6 * Copyright (C) 2019 Aarushi Mehta
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include <liburing.h>
13 #include "block/aio.h"
14 #include "qemu/queue.h"
15 #include "block/block.h"
16 #include "block/raw-aio.h"
17 #include "qemu/coroutine.h"
18 #include "qapi/error.h"
19 #include "trace.h"
21 /* Only used for assertions. */
22 #include "qemu/coroutine_int.h"
24 /* io_uring ring size */
25 #define MAX_ENTRIES 128
27 typedef struct LuringAIOCB {
28 Coroutine *co;
29 struct io_uring_sqe sqeq;
30 ssize_t ret;
31 QEMUIOVector *qiov;
32 bool is_read;
33 QSIMPLEQ_ENTRY(LuringAIOCB) next;
36 * Buffered reads may require resubmission, see
37 * luring_resubmit_short_read().
39 int total_read;
40 QEMUIOVector resubmit_qiov;
41 } LuringAIOCB;
43 typedef struct LuringQueue {
44 int plugged;
45 unsigned int in_queue;
46 unsigned int in_flight;
47 bool blocked;
48 QSIMPLEQ_HEAD(, LuringAIOCB) submit_queue;
49 } LuringQueue;
51 typedef struct LuringState {
52 AioContext *aio_context;
54 struct io_uring ring;
56 /* No locking required, only accessed from AioContext home thread */
57 LuringQueue io_q;
59 QEMUBH *completion_bh;
60 } LuringState;
62 /**
63 * luring_resubmit:
65 * Resubmit a request by appending it to submit_queue. The caller must ensure
66 * that ioq_submit() is called later so that submit_queue requests are started.
68 static void luring_resubmit(LuringState *s, LuringAIOCB *luringcb)
70 QSIMPLEQ_INSERT_TAIL(&s->io_q.submit_queue, luringcb, next);
71 s->io_q.in_queue++;
74 /**
75 * luring_resubmit_short_read:
77 * Short reads are rare but may occur. The remaining read request needs to be
78 * resubmitted.
80 static void luring_resubmit_short_read(LuringState *s, LuringAIOCB *luringcb,
81 int nread)
83 QEMUIOVector *resubmit_qiov;
84 size_t remaining;
86 trace_luring_resubmit_short_read(s, luringcb, nread);
88 /* Update read position */
89 luringcb->total_read += nread;
90 remaining = luringcb->qiov->size - luringcb->total_read;
92 /* Shorten qiov */
93 resubmit_qiov = &luringcb->resubmit_qiov;
94 if (resubmit_qiov->iov == NULL) {
95 qemu_iovec_init(resubmit_qiov, luringcb->qiov->niov);
96 } else {
97 qemu_iovec_reset(resubmit_qiov);
99 qemu_iovec_concat(resubmit_qiov, luringcb->qiov, luringcb->total_read,
100 remaining);
102 /* Update sqe */
103 luringcb->sqeq.off += nread;
104 luringcb->sqeq.addr = (__u64)(uintptr_t)luringcb->resubmit_qiov.iov;
105 luringcb->sqeq.len = luringcb->resubmit_qiov.niov;
107 luring_resubmit(s, luringcb);
111 * luring_process_completions:
112 * @s: AIO state
114 * Fetches completed I/O requests, consumes cqes and invokes their callbacks
115 * The function is somewhat tricky because it supports nested event loops, for
116 * example when a request callback invokes aio_poll().
118 * Function schedules BH completion so it can be called again in a nested
119 * event loop. When there are no events left to complete the BH is being
120 * canceled.
123 static void luring_process_completions(LuringState *s)
125 struct io_uring_cqe *cqes;
126 int total_bytes;
128 * Request completion callbacks can run the nested event loop.
129 * Schedule ourselves so the nested event loop will "see" remaining
130 * completed requests and process them. Without this, completion
131 * callbacks that wait for other requests using a nested event loop
132 * would hang forever.
134 * This workaround is needed because io_uring uses poll_wait, which
135 * is woken up when new events are added to the uring, thus polling on
136 * the same uring fd will block unless more events are received.
138 * Other leaf block drivers (drivers that access the data themselves)
139 * are networking based, so they poll sockets for data and run the
140 * correct coroutine.
142 qemu_bh_schedule(s->completion_bh);
144 while (io_uring_peek_cqe(&s->ring, &cqes) == 0) {
145 LuringAIOCB *luringcb;
146 int ret;
148 if (!cqes) {
149 break;
152 luringcb = io_uring_cqe_get_data(cqes);
153 ret = cqes->res;
154 io_uring_cqe_seen(&s->ring, cqes);
155 cqes = NULL;
157 /* Change counters one-by-one because we can be nested. */
158 s->io_q.in_flight--;
159 trace_luring_process_completion(s, luringcb, ret);
161 /* total_read is non-zero only for resubmitted read requests */
162 total_bytes = ret + luringcb->total_read;
164 if (ret < 0) {
166 * Only writev/readv/fsync requests on regular files or host block
167 * devices are submitted. Therefore -EAGAIN is not expected but it's
168 * known to happen sometimes with Linux SCSI. Submit again and hope
169 * the request completes successfully.
171 * For more information, see:
172 * https://lore.kernel.org/io-uring/20210727165811.284510-3-axboe@kernel.dk/T/#u
174 * If the code is changed to submit other types of requests in the
175 * future, then this workaround may need to be extended to deal with
176 * genuine -EAGAIN results that should not be resubmitted
177 * immediately.
179 if (ret == -EINTR || ret == -EAGAIN) {
180 luring_resubmit(s, luringcb);
181 continue;
183 } else if (!luringcb->qiov) {
184 goto end;
185 } else if (total_bytes == luringcb->qiov->size) {
186 ret = 0;
187 /* Only read/write */
188 } else {
189 /* Short Read/Write */
190 if (luringcb->is_read) {
191 if (ret > 0) {
192 luring_resubmit_short_read(s, luringcb, ret);
193 continue;
194 } else {
195 /* Pad with zeroes */
196 qemu_iovec_memset(luringcb->qiov, total_bytes, 0,
197 luringcb->qiov->size - total_bytes);
198 ret = 0;
200 } else {
201 ret = -ENOSPC;
204 end:
205 luringcb->ret = ret;
206 qemu_iovec_destroy(&luringcb->resubmit_qiov);
209 * If the coroutine is already entered it must be in ioq_submit()
210 * and will notice luringcb->ret has been filled in when it
211 * eventually runs later. Coroutines cannot be entered recursively
212 * so avoid doing that!
214 assert(luringcb->co->ctx == s->aio_context);
215 if (!qemu_coroutine_entered(luringcb->co)) {
216 aio_co_wake(luringcb->co);
219 qemu_bh_cancel(s->completion_bh);
222 static int ioq_submit(LuringState *s)
224 int ret = 0;
225 LuringAIOCB *luringcb, *luringcb_next;
227 while (s->io_q.in_queue > 0) {
229 * Try to fetch sqes from the ring for requests waiting in
230 * the overflow queue
232 QSIMPLEQ_FOREACH_SAFE(luringcb, &s->io_q.submit_queue, next,
233 luringcb_next) {
234 struct io_uring_sqe *sqes = io_uring_get_sqe(&s->ring);
235 if (!sqes) {
236 break;
238 /* Prep sqe for submission */
239 *sqes = luringcb->sqeq;
240 QSIMPLEQ_REMOVE_HEAD(&s->io_q.submit_queue, next);
242 ret = io_uring_submit(&s->ring);
243 trace_luring_io_uring_submit(s, ret);
244 /* Prevent infinite loop if submission is refused */
245 if (ret <= 0) {
246 if (ret == -EAGAIN || ret == -EINTR) {
247 continue;
249 break;
251 s->io_q.in_flight += ret;
252 s->io_q.in_queue -= ret;
254 s->io_q.blocked = (s->io_q.in_queue > 0);
256 if (s->io_q.in_flight) {
258 * We can try to complete something just right away if there are
259 * still requests in-flight.
261 luring_process_completions(s);
263 return ret;
266 static void luring_process_completions_and_submit(LuringState *s)
268 luring_process_completions(s);
270 if (!s->io_q.plugged && s->io_q.in_queue > 0) {
271 ioq_submit(s);
275 static void qemu_luring_completion_bh(void *opaque)
277 LuringState *s = opaque;
278 luring_process_completions_and_submit(s);
281 static void qemu_luring_completion_cb(void *opaque)
283 LuringState *s = opaque;
284 luring_process_completions_and_submit(s);
287 static bool qemu_luring_poll_cb(void *opaque)
289 LuringState *s = opaque;
291 return io_uring_cq_ready(&s->ring);
294 static void qemu_luring_poll_ready(void *opaque)
296 LuringState *s = opaque;
298 luring_process_completions_and_submit(s);
301 static void ioq_init(LuringQueue *io_q)
303 QSIMPLEQ_INIT(&io_q->submit_queue);
304 io_q->plugged = 0;
305 io_q->in_queue = 0;
306 io_q->in_flight = 0;
307 io_q->blocked = false;
310 void luring_io_plug(void)
312 AioContext *ctx = qemu_get_current_aio_context();
313 LuringState *s = aio_get_linux_io_uring(ctx);
314 trace_luring_io_plug(s);
315 s->io_q.plugged++;
318 void luring_io_unplug(void)
320 AioContext *ctx = qemu_get_current_aio_context();
321 LuringState *s = aio_get_linux_io_uring(ctx);
322 assert(s->io_q.plugged);
323 trace_luring_io_unplug(s, s->io_q.blocked, s->io_q.plugged,
324 s->io_q.in_queue, s->io_q.in_flight);
325 if (--s->io_q.plugged == 0 &&
326 !s->io_q.blocked && s->io_q.in_queue > 0) {
327 ioq_submit(s);
332 * luring_do_submit:
333 * @fd: file descriptor for I/O
334 * @luringcb: AIO control block
335 * @s: AIO state
336 * @offset: offset for request
337 * @type: type of request
339 * Fetches sqes from ring, adds to pending queue and preps them
342 static int luring_do_submit(int fd, LuringAIOCB *luringcb, LuringState *s,
343 uint64_t offset, int type)
345 int ret;
346 struct io_uring_sqe *sqes = &luringcb->sqeq;
348 switch (type) {
349 case QEMU_AIO_WRITE:
350 io_uring_prep_writev(sqes, fd, luringcb->qiov->iov,
351 luringcb->qiov->niov, offset);
352 break;
353 case QEMU_AIO_READ:
354 io_uring_prep_readv(sqes, fd, luringcb->qiov->iov,
355 luringcb->qiov->niov, offset);
356 break;
357 case QEMU_AIO_FLUSH:
358 io_uring_prep_fsync(sqes, fd, IORING_FSYNC_DATASYNC);
359 break;
360 default:
361 fprintf(stderr, "%s: invalid AIO request type, aborting 0x%x.\n",
362 __func__, type);
363 abort();
365 io_uring_sqe_set_data(sqes, luringcb);
367 QSIMPLEQ_INSERT_TAIL(&s->io_q.submit_queue, luringcb, next);
368 s->io_q.in_queue++;
369 trace_luring_do_submit(s, s->io_q.blocked, s->io_q.plugged,
370 s->io_q.in_queue, s->io_q.in_flight);
371 if (!s->io_q.blocked &&
372 (!s->io_q.plugged ||
373 s->io_q.in_flight + s->io_q.in_queue >= MAX_ENTRIES)) {
374 ret = ioq_submit(s);
375 trace_luring_do_submit_done(s, ret);
376 return ret;
378 return 0;
381 int coroutine_fn luring_co_submit(BlockDriverState *bs, int fd, uint64_t offset,
382 QEMUIOVector *qiov, int type)
384 int ret;
385 AioContext *ctx = qemu_get_current_aio_context();
386 LuringState *s = aio_get_linux_io_uring(ctx);
387 LuringAIOCB luringcb = {
388 .co = qemu_coroutine_self(),
389 .ret = -EINPROGRESS,
390 .qiov = qiov,
391 .is_read = (type == QEMU_AIO_READ),
393 trace_luring_co_submit(bs, s, &luringcb, fd, offset, qiov ? qiov->size : 0,
394 type);
395 ret = luring_do_submit(fd, &luringcb, s, offset, type);
397 if (ret < 0) {
398 return ret;
401 if (luringcb.ret == -EINPROGRESS) {
402 qemu_coroutine_yield();
404 return luringcb.ret;
407 void luring_detach_aio_context(LuringState *s, AioContext *old_context)
409 aio_set_fd_handler(old_context, s->ring.ring_fd, false,
410 NULL, NULL, NULL, NULL, s);
411 qemu_bh_delete(s->completion_bh);
412 s->aio_context = NULL;
415 void luring_attach_aio_context(LuringState *s, AioContext *new_context)
417 s->aio_context = new_context;
418 s->completion_bh = aio_bh_new(new_context, qemu_luring_completion_bh, s);
419 aio_set_fd_handler(s->aio_context, s->ring.ring_fd, false,
420 qemu_luring_completion_cb, NULL,
421 qemu_luring_poll_cb, qemu_luring_poll_ready, s);
424 LuringState *luring_init(Error **errp)
426 int rc;
427 LuringState *s = g_new0(LuringState, 1);
428 struct io_uring *ring = &s->ring;
430 trace_luring_init_state(s, sizeof(*s));
432 rc = io_uring_queue_init(MAX_ENTRIES, ring, 0);
433 if (rc < 0) {
434 error_setg_errno(errp, errno, "failed to init linux io_uring ring");
435 g_free(s);
436 return NULL;
439 ioq_init(&s->io_q);
440 return s;
444 void luring_cleanup(LuringState *s)
446 io_uring_queue_exit(&s->ring);
447 trace_luring_cleanup_state(s);
448 g_free(s);