2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "block/block.h"
19 #include "qemu/queue.h"
20 #include "qemu/sockets.h"
21 #ifdef CONFIG_EPOLL_CREATE1
22 #include <sys/epoll.h>
33 QLIST_ENTRY(AioHandler) node;
36 #ifdef CONFIG_EPOLL_CREATE1
38 /* The fd number threashold to switch to epoll */
39 #define EPOLL_ENABLE_THRESHOLD 64
41 static void aio_epoll_disable(AioContext *ctx)
43 ctx->epoll_available = false;
44 if (!ctx->epoll_enabled) {
47 ctx->epoll_enabled = false;
51 static inline int epoll_events_from_pfd(int pfd_events)
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);
59 static bool aio_epoll_try_enable(AioContext *ctx)
62 struct epoll_event event;
64 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
66 if (node->deleted || !node->pfd.events) {
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);
76 ctx->epoll_enabled = true;
80 static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
82 struct epoll_event event;
86 if (!ctx->epoll_enabled) {
89 if (!node->pfd.events) {
92 event.data.ptr = node;
93 event.events = epoll_events_from_pfd(node->pfd.events);
94 ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
97 r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event);
99 aio_epoll_disable(ctx);
103 static int aio_epoll(AioContext *ctx, GPollFD *pfds,
104 unsigned npfd, int64_t timeout)
108 struct epoll_event events[128];
111 assert(pfds[0].fd == ctx->epollfd);
113 ret = qemu_poll_ns(pfds, npfd, timeout);
115 if (timeout <= 0 || ret > 0) {
116 ret = epoll_wait(ctx->epollfd, events,
117 sizeof(events) / sizeof(events[0]),
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);
135 static bool aio_epoll_enabled(AioContext *ctx)
137 /* Fall back to ppoll when external clients are disabled. */
138 return !aio_external_disabled(ctx) && ctx->epoll_enabled;
141 static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
142 unsigned npfd, int64_t timeout)
144 if (!ctx->epoll_available) {
147 if (aio_epoll_enabled(ctx)) {
150 if (npfd >= EPOLL_ENABLE_THRESHOLD) {
151 if (aio_epoll_try_enable(ctx)) {
154 aio_epoll_disable(ctx);
162 static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
166 static int aio_epoll(AioContext *ctx, GPollFD *pfds,
167 unsigned npfd, int64_t timeout)
172 static bool aio_epoll_enabled(AioContext *ctx)
177 static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
178 unsigned npfd, int64_t timeout)
185 static AioHandler *find_aio_handler(AioContext *ctx, int fd)
189 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
190 if (node->pfd.fd == fd)
198 void aio_set_fd_handler(AioContext *ctx,
207 bool deleted = false;
209 node = find_aio_handler(ctx, fd);
211 /* Are we deleting the fd handler? */
212 if (!io_read && !io_write) {
217 g_source_remove_poll(&ctx->source, &node->pfd);
219 /* If the lock is held, just mark the node as deleted */
220 if (ctx->walking_handlers) {
222 node->pfd.revents = 0;
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.
228 QLIST_REMOVE(node, node);
233 /* Alloc and insert if it's not already there */
234 node = g_new0(AioHandler, 1);
236 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
238 g_source_add_poll(&ctx->source, &node->pfd);
241 /* Update handler with latest information */
242 node->io_read = io_read;
243 node->io_write = io_write;
244 node->opaque = opaque;
245 node->is_external = is_external;
247 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);
251 aio_epoll_update(ctx, node, is_new);
258 void aio_set_event_notifier(AioContext *ctx,
259 EventNotifier *notifier,
261 EventNotifierHandler *io_read)
263 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
264 is_external, (IOHandler *)io_read, NULL, notifier);
267 bool aio_prepare(AioContext *ctx)
272 bool aio_pending(AioContext *ctx)
276 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
279 revents = node->pfd.revents & node->pfd.events;
280 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read &&
281 aio_node_check(ctx, node->is_external)) {
284 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write &&
285 aio_node_check(ctx, node->is_external)) {
293 bool aio_dispatch(AioContext *ctx)
296 bool progress = false;
299 * If there are callbacks left that have been queued, we need to call them.
300 * Do not call select in this case, because it is possible that the caller
301 * does not need a complete flush (as is the case for aio_poll loops).
303 if (aio_bh_poll(ctx)) {
308 * We have to walk very carefully in case aio_set_fd_handler is
309 * called while we're walking.
311 node = QLIST_FIRST(&ctx->aio_handlers);
316 ctx->walking_handlers++;
318 revents = node->pfd.revents & node->pfd.events;
319 node->pfd.revents = 0;
321 if (!node->deleted &&
322 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
323 aio_node_check(ctx, node->is_external) &&
325 node->io_read(node->opaque);
327 /* aio_notify() does not count as progress */
328 if (node->opaque != &ctx->notifier) {
332 if (!node->deleted &&
333 (revents & (G_IO_OUT | G_IO_ERR)) &&
334 aio_node_check(ctx, node->is_external) &&
336 node->io_write(node->opaque);
341 node = QLIST_NEXT(node, node);
343 ctx->walking_handlers--;
345 if (!ctx->walking_handlers && tmp->deleted) {
346 QLIST_REMOVE(tmp, node);
352 progress |= timerlistgroup_run_timers(&ctx->tlg);
357 /* These thread-local variables are used only in a small part of aio_poll
358 * around the call to the poll() system call. In particular they are not
359 * used while aio_poll is performing callbacks, which makes it much easier
360 * to think about reentrancy!
362 * Stack-allocated arrays would be perfect but they have size limitations;
363 * heap allocation is expensive enough that we want to reuse arrays across
364 * calls to aio_poll(). And because poll() has to be called without holding
365 * any lock, the arrays cannot be stored in AioContext. Thread-local data
366 * has none of the disadvantages of these three options.
368 static __thread GPollFD *pollfds;
369 static __thread AioHandler **nodes;
370 static __thread unsigned npfd, nalloc;
371 static __thread Notifier pollfds_cleanup_notifier;
373 static void pollfds_cleanup(Notifier *n, void *unused)
381 static void add_pollfd(AioHandler *node)
383 if (npfd == nalloc) {
385 pollfds_cleanup_notifier.notify = pollfds_cleanup;
386 qemu_thread_atexit_add(&pollfds_cleanup_notifier);
389 g_assert(nalloc <= INT_MAX);
392 pollfds = g_renew(GPollFD, pollfds, nalloc);
393 nodes = g_renew(AioHandler *, nodes, nalloc);
396 pollfds[npfd] = (GPollFD) {
398 .events = node->pfd.events,
403 bool aio_poll(AioContext *ctx, bool blocking)
410 aio_context_acquire(ctx);
413 /* aio_notify can avoid the expensive event_notifier_set if
414 * everything (file descriptors, bottom halves, timers) will
415 * be re-evaluated before the next blocking poll(). This is
416 * already true when aio_poll is called with blocking == false;
417 * if blocking == true, it is only true after poll() returns,
418 * so disable the optimization now.
421 atomic_add(&ctx->notify_me, 2);
424 ctx->walking_handlers++;
430 if (!aio_epoll_enabled(ctx)) {
431 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
432 if (!node->deleted && node->pfd.events
433 && aio_node_check(ctx, node->is_external)) {
439 timeout = blocking ? aio_compute_timeout(ctx) : 0;
441 /* wait until next event */
443 aio_context_release(ctx);
445 if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
446 AioHandler epoll_handler;
448 epoll_handler.pfd.fd = ctx->epollfd;
449 epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
451 add_pollfd(&epoll_handler);
452 ret = aio_epoll(ctx, pollfds, npfd, timeout);
454 ret = qemu_poll_ns(pollfds, npfd, timeout);
457 atomic_sub(&ctx->notify_me, 2);
460 aio_context_acquire(ctx);
463 aio_notify_accept(ctx);
465 /* if we have any readable fds, dispatch event */
467 for (i = 0; i < npfd; i++) {
468 nodes[i]->pfd.revents = pollfds[i].revents;
473 ctx->walking_handlers--;
475 /* Run dispatch even if there were no readable fds to run timers */
476 if (aio_dispatch(ctx)) {
480 aio_context_release(ctx);
485 void aio_context_setup(AioContext *ctx)
487 #ifdef CONFIG_EPOLL_CREATE1
488 assert(!ctx->epollfd);
489 ctx->epollfd = epoll_create1(EPOLL_CLOEXEC);
490 if (ctx->epollfd == -1) {
491 fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno));
492 ctx->epoll_available = false;
494 ctx->epoll_available = true;