blob: 4ac23463702d5bdaff8464b932cb847a588d8061 [file] [log] [blame]
aliguoria76bab42008-09-22 19:17:18 +00001/*
2 * QEMU aio implementation
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguoria76bab42008-09-22 19:17:18 +000014 */
15
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
aliguoria76bab42008-09-22 19:17:18 +000017#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010018#include "block/block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/queue.h"
20#include "qemu/sockets.h"
Matthew Fortune147dfab2016-02-23 15:42:14 +000021#ifdef CONFIG_EPOLL_CREATE1
Fam Zhengfbe3fc52015-10-30 12:06:29 +080022#include <sys/epoll.h>
23#endif
aliguoria76bab42008-09-22 19:17:18 +000024
aliguoria76bab42008-09-22 19:17:18 +000025struct AioHandler
26{
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020027 GPollFD pfd;
aliguoria76bab42008-09-22 19:17:18 +000028 IOHandler *io_read;
29 IOHandler *io_write;
aliguoria76bab42008-09-22 19:17:18 +000030 int deleted;
31 void *opaque;
Fam Zhengdca21ef2015-10-23 11:08:05 +080032 bool is_external;
Blue Swirl72cf2d42009-09-12 07:36:22 +000033 QLIST_ENTRY(AioHandler) node;
aliguoria76bab42008-09-22 19:17:18 +000034};
35
Matthew Fortune147dfab2016-02-23 15:42:14 +000036#ifdef CONFIG_EPOLL_CREATE1
Fam Zhengfbe3fc52015-10-30 12:06:29 +080037
38/* The fd number threashold to switch to epoll */
39#define EPOLL_ENABLE_THRESHOLD 64
40
41static void aio_epoll_disable(AioContext *ctx)
42{
43 ctx->epoll_available = false;
44 if (!ctx->epoll_enabled) {
45 return;
46 }
47 ctx->epoll_enabled = false;
48 close(ctx->epollfd);
49}
50
51static inline int epoll_events_from_pfd(int pfd_events)
52{
53 return (pfd_events & G_IO_IN ? EPOLLIN : 0) |
54 (pfd_events & G_IO_OUT ? EPOLLOUT : 0) |
55 (pfd_events & G_IO_HUP ? EPOLLHUP : 0) |
56 (pfd_events & G_IO_ERR ? EPOLLERR : 0);
57}
58
59static bool aio_epoll_try_enable(AioContext *ctx)
60{
61 AioHandler *node;
62 struct epoll_event event;
63
64 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
65 int r;
66 if (node->deleted || !node->pfd.events) {
67 continue;
68 }
69 event.events = epoll_events_from_pfd(node->pfd.events);
70 event.data.ptr = node;
71 r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event);
72 if (r) {
73 return false;
74 }
75 }
76 ctx->epoll_enabled = true;
77 return true;
78}
79
80static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
81{
82 struct epoll_event event;
83 int r;
Paolo Bonzini35dd66e2016-11-08 14:55:24 +010084 int ctl;
Fam Zhengfbe3fc52015-10-30 12:06:29 +080085
86 if (!ctx->epoll_enabled) {
87 return;
88 }
89 if (!node->pfd.events) {
Paolo Bonzini35dd66e2016-11-08 14:55:24 +010090 ctl = EPOLL_CTL_DEL;
Fam Zhengfbe3fc52015-10-30 12:06:29 +080091 } else {
92 event.data.ptr = node;
93 event.events = epoll_events_from_pfd(node->pfd.events);
Paolo Bonzini35dd66e2016-11-08 14:55:24 +010094 ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
95 }
96
97 r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event);
98 if (r) {
99 aio_epoll_disable(ctx);
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800100 }
101}
102
103static int aio_epoll(AioContext *ctx, GPollFD *pfds,
104 unsigned npfd, int64_t timeout)
105{
106 AioHandler *node;
107 int i, ret = 0;
108 struct epoll_event events[128];
109
110 assert(npfd == 1);
111 assert(pfds[0].fd == ctx->epollfd);
112 if (timeout > 0) {
113 ret = qemu_poll_ns(pfds, npfd, timeout);
114 }
115 if (timeout <= 0 || ret > 0) {
116 ret = epoll_wait(ctx->epollfd, events,
117 sizeof(events) / sizeof(events[0]),
118 timeout);
119 if (ret <= 0) {
120 goto out;
121 }
122 for (i = 0; i < ret; i++) {
123 int ev = events[i].events;
124 node = events[i].data.ptr;
125 node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) |
126 (ev & EPOLLOUT ? G_IO_OUT : 0) |
127 (ev & EPOLLHUP ? G_IO_HUP : 0) |
128 (ev & EPOLLERR ? G_IO_ERR : 0);
129 }
130 }
131out:
132 return ret;
133}
134
135static bool aio_epoll_enabled(AioContext *ctx)
136{
137 /* Fall back to ppoll when external clients are disabled. */
138 return !aio_external_disabled(ctx) && ctx->epoll_enabled;
139}
140
141static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
142 unsigned npfd, int64_t timeout)
143{
144 if (!ctx->epoll_available) {
145 return false;
146 }
147 if (aio_epoll_enabled(ctx)) {
148 return true;
149 }
150 if (npfd >= EPOLL_ENABLE_THRESHOLD) {
151 if (aio_epoll_try_enable(ctx)) {
152 return true;
153 } else {
154 aio_epoll_disable(ctx);
155 }
156 }
157 return false;
158}
159
160#else
161
162static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
163{
164}
165
166static int aio_epoll(AioContext *ctx, GPollFD *pfds,
167 unsigned npfd, int64_t timeout)
168{
169 assert(false);
170}
171
172static bool aio_epoll_enabled(AioContext *ctx)
173{
174 return false;
175}
176
177static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
178 unsigned npfd, int64_t timeout)
179{
180 return false;
181}
182
183#endif
184
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200185static AioHandler *find_aio_handler(AioContext *ctx, int fd)
aliguoria76bab42008-09-22 19:17:18 +0000186{
187 AioHandler *node;
188
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200189 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200190 if (node->pfd.fd == fd)
Alexander Graf79d5ca52009-05-06 02:58:48 +0200191 if (!node->deleted)
192 return node;
aliguoria76bab42008-09-22 19:17:18 +0000193 }
194
195 return NULL;
196}
197
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200198void aio_set_fd_handler(AioContext *ctx,
199 int fd,
Fam Zhengdca21ef2015-10-23 11:08:05 +0800200 bool is_external,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200201 IOHandler *io_read,
202 IOHandler *io_write,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200203 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +0000204{
205 AioHandler *node;
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800206 bool is_new = false;
Fam Zheng0ed39f3d2015-11-16 14:32:14 +0800207 bool deleted = false;
aliguoria76bab42008-09-22 19:17:18 +0000208
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200209 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +0000210
211 /* Are we deleting the fd handler? */
212 if (!io_read && !io_write) {
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100213 if (node == NULL) {
214 return;
215 }
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200216
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100217 g_source_remove_poll(&ctx->source, &node->pfd);
218
219 /* If the lock is held, just mark the node as deleted */
220 if (ctx->walking_handlers) {
221 node->deleted = 1;
222 node->pfd.revents = 0;
223 } else {
224 /* Otherwise, delete it for real. We can't just mark it as
225 * deleted because deleted nodes are only cleaned up after
226 * releasing the walking_handlers lock.
227 */
228 QLIST_REMOVE(node, node);
229 deleted = true;
aliguoria76bab42008-09-22 19:17:18 +0000230 }
231 } else {
232 if (node == NULL) {
233 /* Alloc and insert if it's not already there */
Markus Armbruster3ba235a2014-12-04 13:55:09 +0100234 node = g_new0(AioHandler, 1);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200235 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200236 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200237
238 g_source_add_poll(&ctx->source, &node->pfd);
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800239 is_new = true;
aliguoria76bab42008-09-22 19:17:18 +0000240 }
241 /* Update handler with latest information */
242 node->io_read = io_read;
243 node->io_write = io_write;
aliguoria76bab42008-09-22 19:17:18 +0000244 node->opaque = opaque;
Fam Zhengdca21ef2015-10-23 11:08:05 +0800245 node->is_external = is_external;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200246
Stefan Hajnoczib5a01a72013-02-20 11:28:33 +0100247 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
248 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
aliguoria76bab42008-09-22 19:17:18 +0000249 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +0200250
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800251 aio_epoll_update(ctx, node, is_new);
Paolo Bonzini7ed2b242012-09-25 10:22:39 +0200252 aio_notify(ctx);
Fam Zheng0ed39f3d2015-11-16 14:32:14 +0800253 if (deleted) {
254 g_free(node);
255 }
aliguoria76bab42008-09-22 19:17:18 +0000256}
257
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200258void aio_set_event_notifier(AioContext *ctx,
259 EventNotifier *notifier,
Fam Zhengdca21ef2015-10-23 11:08:05 +0800260 bool is_external,
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +0200261 EventNotifierHandler *io_read)
Paolo Bonzini9958c352012-06-09 03:44:00 +0200262{
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200263 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
Fam Zhengdca21ef2015-10-23 11:08:05 +0800264 is_external, (IOHandler *)io_read, NULL, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200265}
266
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200267bool aio_prepare(AioContext *ctx)
268{
269 return false;
270}
271
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200272bool aio_pending(AioContext *ctx)
273{
274 AioHandler *node;
275
276 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
277 int revents;
278
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200279 revents = node->pfd.revents & node->pfd.events;
Fam Zheng37989ce2016-04-22 21:53:55 +0800280 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read &&
281 aio_node_check(ctx, node->is_external)) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200282 return true;
283 }
Fam Zheng37989ce2016-04-22 21:53:55 +0800284 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write &&
285 aio_node_check(ctx, node->is_external)) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200286 return true;
287 }
288 }
289
290 return false;
291}
292
Stefan Hajnoczi721671a2016-12-01 19:26:40 +0000293/*
294 * Note that dispatch_fds == false has the side-effect of post-poning the
295 * freeing of deleted handlers.
296 */
297bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
aliguoria76bab42008-09-22 19:17:18 +0000298{
Stefan Hajnoczi721671a2016-12-01 19:26:40 +0000299 AioHandler *node = NULL;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100300 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000301
Kevin Wolf8febfa22009-10-22 17:54:36 +0200302 /*
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200303 * If there are callbacks left that have been queued, we need to call them.
304 * Do not call select in this case, because it is possible that the caller
305 * does not need a complete flush (as is the case for aio_poll loops).
306 */
307 if (aio_bh_poll(ctx)) {
308 progress = true;
309 }
310
311 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200312 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200313 * called while we're walking.
314 */
Stefan Hajnoczi721671a2016-12-01 19:26:40 +0000315 if (dispatch_fds) {
316 node = QLIST_FIRST(&ctx->aio_handlers);
317 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200318 while (node) {
319 AioHandler *tmp;
320 int revents;
321
322 ctx->walking_handlers++;
323
324 revents = node->pfd.revents & node->pfd.events;
325 node->pfd.revents = 0;
326
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100327 if (!node->deleted &&
328 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
Fam Zheng37989ce2016-04-22 21:53:55 +0800329 aio_node_check(ctx, node->is_external) &&
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100330 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200331 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200332
333 /* aio_notify() does not count as progress */
334 if (node->opaque != &ctx->notifier) {
335 progress = true;
336 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200337 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100338 if (!node->deleted &&
339 (revents & (G_IO_OUT | G_IO_ERR)) &&
Fam Zheng37989ce2016-04-22 21:53:55 +0800340 aio_node_check(ctx, node->is_external) &&
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100341 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200342 node->io_write(node->opaque);
343 progress = true;
344 }
345
346 tmp = node;
347 node = QLIST_NEXT(node, node);
348
349 ctx->walking_handlers--;
350
351 if (!ctx->walking_handlers && tmp->deleted) {
352 QLIST_REMOVE(tmp, node);
353 g_free(tmp);
354 }
355 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100356
357 /* Run our timers */
358 progress |= timerlistgroup_run_timers(&ctx->tlg);
359
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100360 return progress;
361}
362
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100363/* These thread-local variables are used only in a small part of aio_poll
364 * around the call to the poll() system call. In particular they are not
365 * used while aio_poll is performing callbacks, which makes it much easier
366 * to think about reentrancy!
367 *
368 * Stack-allocated arrays would be perfect but they have size limitations;
369 * heap allocation is expensive enough that we want to reuse arrays across
370 * calls to aio_poll(). And because poll() has to be called without holding
371 * any lock, the arrays cannot be stored in AioContext. Thread-local data
372 * has none of the disadvantages of these three options.
373 */
374static __thread GPollFD *pollfds;
375static __thread AioHandler **nodes;
376static __thread unsigned npfd, nalloc;
377static __thread Notifier pollfds_cleanup_notifier;
378
379static void pollfds_cleanup(Notifier *n, void *unused)
380{
381 g_assert(npfd == 0);
382 g_free(pollfds);
383 g_free(nodes);
384 nalloc = 0;
385}
386
387static void add_pollfd(AioHandler *node)
388{
389 if (npfd == nalloc) {
390 if (nalloc == 0) {
391 pollfds_cleanup_notifier.notify = pollfds_cleanup;
392 qemu_thread_atexit_add(&pollfds_cleanup_notifier);
393 nalloc = 8;
394 } else {
395 g_assert(nalloc <= INT_MAX);
396 nalloc *= 2;
397 }
398 pollfds = g_renew(GPollFD, pollfds, nalloc);
399 nodes = g_renew(AioHandler *, nodes, nalloc);
400 }
401 nodes[npfd] = node;
402 pollfds[npfd] = (GPollFD) {
403 .fd = node->pfd.fd,
404 .events = node->pfd.events,
405 };
406 npfd++;
407}
408
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100409bool aio_poll(AioContext *ctx, bool blocking)
410{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100411 AioHandler *node;
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100412 int i, ret;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200413 bool progress;
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100414 int64_t timeout;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100415
Paolo Bonzini49110172015-02-20 17:26:51 +0100416 aio_context_acquire(ctx);
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100417 progress = false;
418
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200419 /* aio_notify can avoid the expensive event_notifier_set if
420 * everything (file descriptors, bottom halves, timers) will
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200421 * be re-evaluated before the next blocking poll(). This is
422 * already true when aio_poll is called with blocking == false;
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200423 * if blocking == true, it is only true after poll() returns,
424 * so disable the optimization now.
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200425 */
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200426 if (blocking) {
427 atomic_add(&ctx->notify_me, 2);
428 }
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200429
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200430 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000431
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100432 assert(npfd == 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200433
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100434 /* fill pollfds */
Yaowei Bai6b942462016-09-14 07:03:39 -0400435
436 if (!aio_epoll_enabled(ctx)) {
437 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
438 if (!node->deleted && node->pfd.events
439 && aio_node_check(ctx, node->is_external)) {
440 add_pollfd(node);
441 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200442 }
443 }
444
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100445 timeout = blocking ? aio_compute_timeout(ctx) : 0;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200446
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200447 /* wait until next event */
Paolo Bonzini49110172015-02-20 17:26:51 +0100448 if (timeout) {
449 aio_context_release(ctx);
450 }
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800451 if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
452 AioHandler epoll_handler;
453
454 epoll_handler.pfd.fd = ctx->epollfd;
455 epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
456 npfd = 0;
457 add_pollfd(&epoll_handler);
458 ret = aio_epoll(ctx, pollfds, npfd, timeout);
459 } else {
460 ret = qemu_poll_ns(pollfds, npfd, timeout);
461 }
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200462 if (blocking) {
463 atomic_sub(&ctx->notify_me, 2);
464 }
Paolo Bonzini49110172015-02-20 17:26:51 +0100465 if (timeout) {
466 aio_context_acquire(ctx);
467 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200468
Paolo Bonzini05e514b2015-07-21 16:07:53 +0200469 aio_notify_accept(ctx);
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200470
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200471 /* if we have any readable fds, dispatch event */
472 if (ret > 0) {
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100473 for (i = 0; i < npfd; i++) {
474 nodes[i]->pfd.revents = pollfds[i].revents;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100475 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100476 }
477
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100478 npfd = 0;
479 ctx->walking_handlers--;
480
Alex Bligh438e1f42013-08-21 16:02:53 +0100481 /* Run dispatch even if there were no readable fds to run timers */
Stefan Hajnoczi721671a2016-12-01 19:26:40 +0000482 if (aio_dispatch(ctx, ret > 0)) {
Alex Bligh438e1f42013-08-21 16:02:53 +0100483 progress = true;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200484 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200485
Paolo Bonzini49110172015-02-20 17:26:51 +0100486 aio_context_release(ctx);
487
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200488 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000489}
Fam Zheng37fcee52015-10-30 12:06:28 +0800490
Cao jin7e003462016-07-15 18:28:44 +0800491void aio_context_setup(AioContext *ctx)
Fam Zheng37fcee52015-10-30 12:06:28 +0800492{
Matthew Fortune147dfab2016-02-23 15:42:14 +0000493#ifdef CONFIG_EPOLL_CREATE1
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800494 assert(!ctx->epollfd);
495 ctx->epollfd = epoll_create1(EPOLL_CLOEXEC);
496 if (ctx->epollfd == -1) {
Cao jin7e003462016-07-15 18:28:44 +0800497 fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno));
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800498 ctx->epoll_available = false;
499 } else {
500 ctx->epoll_available = true;
501 }
502#endif
Fam Zheng37fcee52015-10-30 12:06:28 +0800503}