2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
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
;
85 if (!ctx
->epoll_enabled
) {
88 if (!node
->pfd
.events
) {
89 r
= epoll_ctl(ctx
->epollfd
, EPOLL_CTL_DEL
, node
->pfd
.fd
, &event
);
91 aio_epoll_disable(ctx
);
94 event
.data
.ptr
= node
;
95 event
.events
= epoll_events_from_pfd(node
->pfd
.events
);
97 r
= epoll_ctl(ctx
->epollfd
, EPOLL_CTL_ADD
, node
->pfd
.fd
, &event
);
99 aio_epoll_disable(ctx
);
102 r
= epoll_ctl(ctx
->epollfd
, EPOLL_CTL_MOD
, node
->pfd
.fd
, &event
);
104 aio_epoll_disable(ctx
);
110 static int aio_epoll(AioContext
*ctx
, GPollFD
*pfds
,
111 unsigned npfd
, int64_t timeout
)
115 struct epoll_event events
[128];
118 assert(pfds
[0].fd
== ctx
->epollfd
);
120 ret
= qemu_poll_ns(pfds
, npfd
, timeout
);
122 if (timeout
<= 0 || ret
> 0) {
123 ret
= epoll_wait(ctx
->epollfd
, events
,
124 sizeof(events
) / sizeof(events
[0]),
129 for (i
= 0; i
< ret
; i
++) {
130 int ev
= events
[i
].events
;
131 node
= events
[i
].data
.ptr
;
132 node
->pfd
.revents
= (ev
& EPOLLIN
? G_IO_IN
: 0) |
133 (ev
& EPOLLOUT
? G_IO_OUT
: 0) |
134 (ev
& EPOLLHUP
? G_IO_HUP
: 0) |
135 (ev
& EPOLLERR
? G_IO_ERR
: 0);
142 static bool aio_epoll_enabled(AioContext
*ctx
)
144 /* Fall back to ppoll when external clients are disabled. */
145 return !aio_external_disabled(ctx
) && ctx
->epoll_enabled
;
148 static bool aio_epoll_check_poll(AioContext
*ctx
, GPollFD
*pfds
,
149 unsigned npfd
, int64_t timeout
)
151 if (!ctx
->epoll_available
) {
154 if (aio_epoll_enabled(ctx
)) {
157 if (npfd
>= EPOLL_ENABLE_THRESHOLD
) {
158 if (aio_epoll_try_enable(ctx
)) {
161 aio_epoll_disable(ctx
);
169 static void aio_epoll_update(AioContext
*ctx
, AioHandler
*node
, bool is_new
)
173 static int aio_epoll(AioContext
*ctx
, GPollFD
*pfds
,
174 unsigned npfd
, int64_t timeout
)
179 static bool aio_epoll_enabled(AioContext
*ctx
)
184 static bool aio_epoll_check_poll(AioContext
*ctx
, GPollFD
*pfds
,
185 unsigned npfd
, int64_t timeout
)
192 static AioHandler
*find_aio_handler(AioContext
*ctx
, int fd
)
196 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
197 if (node
->pfd
.fd
== fd
)
205 void aio_set_fd_handler(AioContext
*ctx
,
214 bool deleted
= false;
216 node
= find_aio_handler(ctx
, fd
);
218 /* Are we deleting the fd handler? */
219 if (!io_read
&& !io_write
) {
221 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
223 /* If the lock is held, just mark the node as deleted */
224 if (ctx
->walking_handlers
) {
226 node
->pfd
.revents
= 0;
228 /* Otherwise, delete it for real. We can't just mark it as
229 * deleted because deleted nodes are only cleaned up after
230 * releasing the walking_handlers lock.
232 QLIST_REMOVE(node
, node
);
238 /* Alloc and insert if it's not already there */
239 node
= g_new0(AioHandler
, 1);
241 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
243 g_source_add_poll(&ctx
->source
, &node
->pfd
);
246 /* Update handler with latest information */
247 node
->io_read
= io_read
;
248 node
->io_write
= io_write
;
249 node
->opaque
= opaque
;
250 node
->is_external
= is_external
;
252 node
->pfd
.events
= (io_read
? G_IO_IN
| G_IO_HUP
| G_IO_ERR
: 0);
253 node
->pfd
.events
|= (io_write
? G_IO_OUT
| G_IO_ERR
: 0);
256 aio_epoll_update(ctx
, node
, is_new
);
263 void aio_set_event_notifier(AioContext
*ctx
,
264 EventNotifier
*notifier
,
266 EventNotifierHandler
*io_read
)
268 aio_set_fd_handler(ctx
, event_notifier_get_fd(notifier
),
269 is_external
, (IOHandler
*)io_read
, NULL
, notifier
);
272 bool aio_prepare(AioContext
*ctx
)
277 bool aio_pending(AioContext
*ctx
)
281 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
284 revents
= node
->pfd
.revents
& node
->pfd
.events
;
285 if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
) && node
->io_read
&&
286 aio_node_check(ctx
, node
->is_external
)) {
289 if (revents
& (G_IO_OUT
| G_IO_ERR
) && node
->io_write
&&
290 aio_node_check(ctx
, node
->is_external
)) {
298 bool aio_dispatch(AioContext
*ctx
)
301 bool progress
= false;
304 * If there are callbacks left that have been queued, we need to call them.
305 * Do not call select in this case, because it is possible that the caller
306 * does not need a complete flush (as is the case for aio_poll loops).
308 if (aio_bh_poll(ctx
)) {
313 * We have to walk very carefully in case aio_set_fd_handler is
314 * called while we're walking.
316 node
= QLIST_FIRST(&ctx
->aio_handlers
);
321 ctx
->walking_handlers
++;
323 revents
= node
->pfd
.revents
& node
->pfd
.events
;
324 node
->pfd
.revents
= 0;
326 if (!node
->deleted
&&
327 (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
)) &&
328 aio_node_check(ctx
, node
->is_external
) &&
330 node
->io_read(node
->opaque
);
332 /* aio_notify() does not count as progress */
333 if (node
->opaque
!= &ctx
->notifier
) {
337 if (!node
->deleted
&&
338 (revents
& (G_IO_OUT
| G_IO_ERR
)) &&
339 aio_node_check(ctx
, node
->is_external
) &&
341 node
->io_write(node
->opaque
);
346 node
= QLIST_NEXT(node
, node
);
348 ctx
->walking_handlers
--;
350 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
351 QLIST_REMOVE(tmp
, node
);
357 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
362 /* These thread-local variables are used only in a small part of aio_poll
363 * around the call to the poll() system call. In particular they are not
364 * used while aio_poll is performing callbacks, which makes it much easier
365 * to think about reentrancy!
367 * Stack-allocated arrays would be perfect but they have size limitations;
368 * heap allocation is expensive enough that we want to reuse arrays across
369 * calls to aio_poll(). And because poll() has to be called without holding
370 * any lock, the arrays cannot be stored in AioContext. Thread-local data
371 * has none of the disadvantages of these three options.
373 static __thread GPollFD
*pollfds
;
374 static __thread AioHandler
**nodes
;
375 static __thread
unsigned npfd
, nalloc
;
376 static __thread Notifier pollfds_cleanup_notifier
;
378 static void pollfds_cleanup(Notifier
*n
, void *unused
)
386 static void add_pollfd(AioHandler
*node
)
388 if (npfd
== nalloc
) {
390 pollfds_cleanup_notifier
.notify
= pollfds_cleanup
;
391 qemu_thread_atexit_add(&pollfds_cleanup_notifier
);
394 g_assert(nalloc
<= INT_MAX
);
397 pollfds
= g_renew(GPollFD
, pollfds
, nalloc
);
398 nodes
= g_renew(AioHandler
*, nodes
, nalloc
);
401 pollfds
[npfd
] = (GPollFD
) {
403 .events
= node
->pfd
.events
,
408 bool aio_poll(AioContext
*ctx
, bool blocking
)
415 aio_context_acquire(ctx
);
418 /* aio_notify can avoid the expensive event_notifier_set if
419 * everything (file descriptors, bottom halves, timers) will
420 * be re-evaluated before the next blocking poll(). This is
421 * already true when aio_poll is called with blocking == false;
422 * if blocking == true, it is only true after poll() returns,
423 * so disable the optimization now.
426 atomic_add(&ctx
->notify_me
, 2);
429 ctx
->walking_handlers
++;
434 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
435 if (!node
->deleted
&& node
->pfd
.events
436 && !aio_epoll_enabled(ctx
)
437 && aio_node_check(ctx
, node
->is_external
)) {
442 timeout
= blocking
? aio_compute_timeout(ctx
) : 0;
444 /* wait until next event */
446 aio_context_release(ctx
);
448 if (aio_epoll_check_poll(ctx
, pollfds
, npfd
, timeout
)) {
449 AioHandler epoll_handler
;
451 epoll_handler
.pfd
.fd
= ctx
->epollfd
;
452 epoll_handler
.pfd
.events
= G_IO_IN
| G_IO_OUT
| G_IO_HUP
| G_IO_ERR
;
454 add_pollfd(&epoll_handler
);
455 ret
= aio_epoll(ctx
, pollfds
, npfd
, timeout
);
457 ret
= qemu_poll_ns(pollfds
, npfd
, timeout
);
460 atomic_sub(&ctx
->notify_me
, 2);
463 aio_context_acquire(ctx
);
466 aio_notify_accept(ctx
);
468 /* if we have any readable fds, dispatch event */
470 for (i
= 0; i
< npfd
; i
++) {
471 nodes
[i
]->pfd
.revents
= pollfds
[i
].revents
;
476 ctx
->walking_handlers
--;
478 /* Run dispatch even if there were no readable fds to run timers */
479 if (aio_dispatch(ctx
)) {
483 aio_context_release(ctx
);
488 void aio_context_setup(AioContext
*ctx
)
490 #ifdef CONFIG_EPOLL_CREATE1
491 assert(!ctx
->epollfd
);
492 ctx
->epollfd
= epoll_create1(EPOLL_CLOEXEC
);
493 if (ctx
->epollfd
== -1) {
494 fprintf(stderr
, "Failed to create epoll instance: %s", strerror(errno
));
495 ctx
->epoll_available
= false;
497 ctx
->epoll_available
= true;