MAINTAINERS: Add entry for rdma migration
[qemu/armbru.git] / util / aio-posix.c
blob7f2c99729d4475640b7c6b21977123a8e6f97bae
1 /*
2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
6 * Authors:
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 "block/block.h"
18 #include "block/thread-pool.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/rcu.h"
21 #include "qemu/rcu_queue.h"
22 #include "qemu/sockets.h"
23 #include "qemu/cutils.h"
24 #include "trace.h"
25 #include "aio-posix.h"
27 /* Stop userspace polling on a handler if it isn't active for some time */
28 #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
30 bool aio_poll_disabled(AioContext *ctx)
32 return qatomic_read(&ctx->poll_disable_cnt);
35 void aio_add_ready_handler(AioHandlerList *ready_list,
36 AioHandler *node,
37 int revents)
39 QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */
40 node->pfd.revents = revents;
41 QLIST_INSERT_HEAD(ready_list, node, node_ready);
44 static void aio_add_poll_ready_handler(AioHandlerList *ready_list,
45 AioHandler *node)
47 QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */
48 node->poll_ready = true;
49 QLIST_INSERT_HEAD(ready_list, node, node_ready);
52 static AioHandler *find_aio_handler(AioContext *ctx, int fd)
54 AioHandler *node;
56 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
57 if (node->pfd.fd == fd) {
58 if (!QLIST_IS_INSERTED(node, node_deleted)) {
59 return node;
64 return NULL;
67 static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
69 /* If the GSource is in the process of being destroyed then
70 * g_source_remove_poll() causes an assertion failure. Skip
71 * removal in that case, because glib cleans up its state during
72 * destruction anyway.
74 if (!g_source_is_destroyed(&ctx->source)) {
75 g_source_remove_poll(&ctx->source, &node->pfd);
78 node->pfd.revents = 0;
79 node->poll_ready = false;
81 /* If the fd monitor has already marked it deleted, leave it alone */
82 if (QLIST_IS_INSERTED(node, node_deleted)) {
83 return false;
86 /* If a read is in progress, just mark the node as deleted */
87 if (qemu_lockcnt_count(&ctx->list_lock)) {
88 QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_deleted);
89 return false;
91 /* Otherwise, delete it for real. We can't just mark it as
92 * deleted because deleted nodes are only cleaned up while
93 * no one is walking the handlers list.
95 QLIST_SAFE_REMOVE(node, node_poll);
96 QLIST_REMOVE(node, node);
97 return true;
100 void aio_set_fd_handler(AioContext *ctx,
101 int fd,
102 IOHandler *io_read,
103 IOHandler *io_write,
104 AioPollFn *io_poll,
105 IOHandler *io_poll_ready,
106 void *opaque)
108 AioHandler *node;
109 AioHandler *new_node = NULL;
110 bool is_new = false;
111 bool deleted = false;
112 int poll_disable_change;
114 if (io_poll && !io_poll_ready) {
115 io_poll = NULL; /* polling only makes sense if there is a handler */
118 qemu_lockcnt_lock(&ctx->list_lock);
120 node = find_aio_handler(ctx, fd);
122 /* Are we deleting the fd handler? */
123 if (!io_read && !io_write && !io_poll) {
124 if (node == NULL) {
125 qemu_lockcnt_unlock(&ctx->list_lock);
126 return;
128 /* Clean events in order to unregister fd from the ctx epoll. */
129 node->pfd.events = 0;
131 poll_disable_change = -!node->io_poll;
132 } else {
133 poll_disable_change = !io_poll - (node && !node->io_poll);
134 if (node == NULL) {
135 is_new = true;
137 /* Alloc and insert if it's not already there */
138 new_node = g_new0(AioHandler, 1);
140 /* Update handler with latest information */
141 new_node->io_read = io_read;
142 new_node->io_write = io_write;
143 new_node->io_poll = io_poll;
144 new_node->io_poll_ready = io_poll_ready;
145 new_node->opaque = opaque;
147 if (is_new) {
148 new_node->pfd.fd = fd;
149 } else {
150 new_node->pfd = node->pfd;
152 g_source_add_poll(&ctx->source, &new_node->pfd);
154 new_node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
155 new_node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
157 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node);
160 /* No need to order poll_disable_cnt writes against other updates;
161 * the counter is only used to avoid wasting time and latency on
162 * iterated polling when the system call will be ultimately necessary.
163 * Changing handlers is a rare event, and a little wasted polling until
164 * the aio_notify below is not an issue.
166 qatomic_set(&ctx->poll_disable_cnt,
167 qatomic_read(&ctx->poll_disable_cnt) + poll_disable_change);
169 ctx->fdmon_ops->update(ctx, node, new_node);
170 if (node) {
171 deleted = aio_remove_fd_handler(ctx, node);
173 qemu_lockcnt_unlock(&ctx->list_lock);
174 aio_notify(ctx);
176 if (deleted) {
177 g_free(node);
181 static void aio_set_fd_poll(AioContext *ctx, int fd,
182 IOHandler *io_poll_begin,
183 IOHandler *io_poll_end)
185 AioHandler *node = find_aio_handler(ctx, fd);
187 if (!node) {
188 return;
191 node->io_poll_begin = io_poll_begin;
192 node->io_poll_end = io_poll_end;
195 void aio_set_event_notifier(AioContext *ctx,
196 EventNotifier *notifier,
197 EventNotifierHandler *io_read,
198 AioPollFn *io_poll,
199 EventNotifierHandler *io_poll_ready)
201 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
202 (IOHandler *)io_read, NULL, io_poll,
203 (IOHandler *)io_poll_ready, notifier);
206 void aio_set_event_notifier_poll(AioContext *ctx,
207 EventNotifier *notifier,
208 EventNotifierHandler *io_poll_begin,
209 EventNotifierHandler *io_poll_end)
211 aio_set_fd_poll(ctx, event_notifier_get_fd(notifier),
212 (IOHandler *)io_poll_begin,
213 (IOHandler *)io_poll_end);
216 static bool poll_set_started(AioContext *ctx, AioHandlerList *ready_list,
217 bool started)
219 AioHandler *node;
220 bool progress = false;
222 if (started == ctx->poll_started) {
223 return false;
226 ctx->poll_started = started;
228 qemu_lockcnt_inc(&ctx->list_lock);
229 QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
230 IOHandler *fn;
232 if (QLIST_IS_INSERTED(node, node_deleted)) {
233 continue;
236 if (started) {
237 fn = node->io_poll_begin;
238 } else {
239 fn = node->io_poll_end;
242 if (fn) {
243 fn(node->opaque);
246 /* Poll one last time in case ->io_poll_end() raced with the event */
247 if (!started && node->io_poll(node->opaque)) {
248 aio_add_poll_ready_handler(ready_list, node);
249 progress = true;
252 qemu_lockcnt_dec(&ctx->list_lock);
254 return progress;
258 bool aio_prepare(AioContext *ctx)
260 AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
262 /* Poll mode cannot be used with glib's event loop, disable it. */
263 poll_set_started(ctx, &ready_list, false);
264 /* TODO what to do with this list? */
266 return false;
269 bool aio_pending(AioContext *ctx)
271 AioHandler *node;
272 bool result = false;
275 * We have to walk very carefully in case aio_set_fd_handler is
276 * called while we're walking.
278 qemu_lockcnt_inc(&ctx->list_lock);
280 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
281 int revents;
283 /* TODO should this check poll ready? */
284 revents = node->pfd.revents & node->pfd.events;
285 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
286 result = true;
287 break;
289 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
290 result = true;
291 break;
294 qemu_lockcnt_dec(&ctx->list_lock);
296 return result;
299 static void aio_free_deleted_handlers(AioContext *ctx)
301 AioHandler *node;
303 if (QLIST_EMPTY_RCU(&ctx->deleted_aio_handlers)) {
304 return;
306 if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
307 return; /* we are nested, let the parent do the freeing */
310 while ((node = QLIST_FIRST_RCU(&ctx->deleted_aio_handlers))) {
311 QLIST_REMOVE(node, node);
312 QLIST_REMOVE(node, node_deleted);
313 QLIST_SAFE_REMOVE(node, node_poll);
314 g_free(node);
317 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
320 static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node)
322 bool progress = false;
323 bool poll_ready;
324 int revents;
326 revents = node->pfd.revents & node->pfd.events;
327 node->pfd.revents = 0;
329 poll_ready = node->poll_ready;
330 node->poll_ready = false;
333 * Start polling AioHandlers when they become ready because activity is
334 * likely to continue. Note that starvation is theoretically possible when
335 * fdmon_supports_polling(), but only until the fd fires for the first
336 * time.
338 if (!QLIST_IS_INSERTED(node, node_deleted) &&
339 !QLIST_IS_INSERTED(node, node_poll) &&
340 node->io_poll) {
341 trace_poll_add(ctx, node, node->pfd.fd, revents);
342 if (ctx->poll_started && node->io_poll_begin) {
343 node->io_poll_begin(node->opaque);
345 QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll);
347 if (!QLIST_IS_INSERTED(node, node_deleted) &&
348 poll_ready && revents == 0 && node->io_poll_ready) {
350 * Remove temporarily to avoid infinite loops when ->io_poll_ready()
351 * calls aio_poll() before clearing the condition that made the poll
352 * handler become ready.
354 QLIST_SAFE_REMOVE(node, node_poll);
356 node->io_poll_ready(node->opaque);
358 if (!QLIST_IS_INSERTED(node, node_poll)) {
359 QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll);
363 * Return early since revents was zero. aio_notify() does not count as
364 * progress.
366 return node->opaque != &ctx->notifier;
369 if (!QLIST_IS_INSERTED(node, node_deleted) &&
370 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
371 node->io_read) {
372 node->io_read(node->opaque);
374 /* aio_notify() does not count as progress */
375 if (node->opaque != &ctx->notifier) {
376 progress = true;
379 if (!QLIST_IS_INSERTED(node, node_deleted) &&
380 (revents & (G_IO_OUT | G_IO_ERR)) &&
381 node->io_write) {
382 node->io_write(node->opaque);
383 progress = true;
386 return progress;
390 * If we have a list of ready handlers then this is more efficient than
391 * scanning all handlers with aio_dispatch_handlers().
393 static bool aio_dispatch_ready_handlers(AioContext *ctx,
394 AioHandlerList *ready_list)
396 bool progress = false;
397 AioHandler *node;
399 while ((node = QLIST_FIRST(ready_list))) {
400 QLIST_REMOVE(node, node_ready);
401 progress = aio_dispatch_handler(ctx, node) || progress;
404 return progress;
407 /* Slower than aio_dispatch_ready_handlers() but only used via glib */
408 static bool aio_dispatch_handlers(AioContext *ctx)
410 AioHandler *node, *tmp;
411 bool progress = false;
413 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
414 progress = aio_dispatch_handler(ctx, node) || progress;
417 return progress;
420 void aio_dispatch(AioContext *ctx)
422 qemu_lockcnt_inc(&ctx->list_lock);
423 aio_bh_poll(ctx);
424 aio_dispatch_handlers(ctx);
425 aio_free_deleted_handlers(ctx);
426 qemu_lockcnt_dec(&ctx->list_lock);
428 timerlistgroup_run_timers(&ctx->tlg);
431 static bool run_poll_handlers_once(AioContext *ctx,
432 AioHandlerList *ready_list,
433 int64_t now,
434 int64_t *timeout)
436 bool progress = false;
437 AioHandler *node;
438 AioHandler *tmp;
440 QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) {
441 if (node->io_poll(node->opaque)) {
442 aio_add_poll_ready_handler(ready_list, node);
444 node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS;
447 * Polling was successful, exit try_poll_mode immediately
448 * to adjust the next polling time.
450 *timeout = 0;
451 if (node->opaque != &ctx->notifier) {
452 progress = true;
456 /* Caller handles freeing deleted nodes. Don't do it here. */
459 return progress;
462 static bool fdmon_supports_polling(AioContext *ctx)
464 return ctx->fdmon_ops->need_wait != aio_poll_disabled;
467 static bool remove_idle_poll_handlers(AioContext *ctx,
468 AioHandlerList *ready_list,
469 int64_t now)
471 AioHandler *node;
472 AioHandler *tmp;
473 bool progress = false;
476 * File descriptor monitoring implementations without userspace polling
477 * support suffer from starvation when a subset of handlers is polled
478 * because fds will not be processed in a timely fashion. Don't remove
479 * idle poll handlers.
481 if (!fdmon_supports_polling(ctx)) {
482 return false;
485 QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) {
486 if (node->poll_idle_timeout == 0LL) {
487 node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS;
488 } else if (now >= node->poll_idle_timeout) {
489 trace_poll_remove(ctx, node, node->pfd.fd);
490 node->poll_idle_timeout = 0LL;
491 QLIST_SAFE_REMOVE(node, node_poll);
492 if (ctx->poll_started && node->io_poll_end) {
493 node->io_poll_end(node->opaque);
496 * Final poll in case ->io_poll_end() races with an event.
497 * Nevermind about re-adding the handler in the rare case where
498 * this causes progress.
500 if (node->io_poll(node->opaque)) {
501 aio_add_poll_ready_handler(ready_list, node);
502 progress = true;
508 return progress;
511 /* run_poll_handlers:
512 * @ctx: the AioContext
513 * @ready_list: the list to place ready handlers on
514 * @max_ns: maximum time to poll for, in nanoseconds
516 * Polls for a given time.
518 * Note that the caller must have incremented ctx->list_lock.
520 * Returns: true if progress was made, false otherwise
522 static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list,
523 int64_t max_ns, int64_t *timeout)
525 bool progress;
526 int64_t start_time, elapsed_time;
528 assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
530 trace_run_poll_handlers_begin(ctx, max_ns, *timeout);
533 * Optimization: ->io_poll() handlers often contain RCU read critical
534 * sections and we therefore see many rcu_read_lock() -> rcu_read_unlock()
535 * -> rcu_read_lock() -> ... sequences with expensive memory
536 * synchronization primitives. Make the entire polling loop an RCU
537 * critical section because nested rcu_read_lock()/rcu_read_unlock() calls
538 * are cheap.
540 RCU_READ_LOCK_GUARD();
542 start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
543 do {
544 progress = run_poll_handlers_once(ctx, ready_list,
545 start_time, timeout);
546 elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time;
547 max_ns = qemu_soonest_timeout(*timeout, max_ns);
548 assert(!(max_ns && progress));
549 } while (elapsed_time < max_ns && !ctx->fdmon_ops->need_wait(ctx));
551 if (remove_idle_poll_handlers(ctx, ready_list,
552 start_time + elapsed_time)) {
553 *timeout = 0;
554 progress = true;
557 /* If time has passed with no successful polling, adjust *timeout to
558 * keep the same ending time.
560 if (*timeout != -1) {
561 *timeout -= MIN(*timeout, elapsed_time);
564 trace_run_poll_handlers_end(ctx, progress, *timeout);
565 return progress;
568 /* try_poll_mode:
569 * @ctx: the AioContext
570 * @ready_list: list to add handlers that need to be run
571 * @timeout: timeout for blocking wait, computed by the caller and updated if
572 * polling succeeds.
574 * Note that the caller must have incremented ctx->list_lock.
576 * Returns: true if progress was made, false otherwise
578 static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
579 int64_t *timeout)
581 int64_t max_ns;
583 if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
584 return false;
587 max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
588 if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
590 * Enable poll mode. It pairs with the poll_set_started() in
591 * aio_poll() which disables poll mode.
593 poll_set_started(ctx, ready_list, true);
595 if (run_poll_handlers(ctx, ready_list, max_ns, timeout)) {
596 return true;
599 return false;
602 bool aio_poll(AioContext *ctx, bool blocking)
604 AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
605 bool progress;
606 bool use_notify_me;
607 int64_t timeout;
608 int64_t start = 0;
611 * There cannot be two concurrent aio_poll calls for the same AioContext (or
612 * an aio_poll concurrent with a GSource prepare/check/dispatch callback).
613 * We rely on this below to avoid slow locked accesses to ctx->notify_me.
615 * aio_poll() may only be called in the AioContext's thread. iohandler_ctx
616 * is special in that it runs in the main thread, but that thread's context
617 * is qemu_aio_context.
619 assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ?
620 qemu_get_aio_context() : ctx));
622 qemu_lockcnt_inc(&ctx->list_lock);
624 if (ctx->poll_max_ns) {
625 start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
628 timeout = blocking ? aio_compute_timeout(ctx) : 0;
629 progress = try_poll_mode(ctx, &ready_list, &timeout);
630 assert(!(timeout && progress));
633 * aio_notify can avoid the expensive event_notifier_set if
634 * everything (file descriptors, bottom halves, timers) will
635 * be re-evaluated before the next blocking poll(). This is
636 * already true when aio_poll is called with blocking == false;
637 * if blocking == true, it is only true after poll() returns,
638 * so disable the optimization now.
640 use_notify_me = timeout != 0;
641 if (use_notify_me) {
642 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2);
644 * Write ctx->notify_me before reading ctx->notified. Pairs with
645 * smp_mb in aio_notify().
647 smp_mb();
649 /* Don't block if aio_notify() was called */
650 if (qatomic_read(&ctx->notified)) {
651 timeout = 0;
655 /* If polling is allowed, non-blocking aio_poll does not need the
656 * system call---a single round of run_poll_handlers_once suffices.
658 if (timeout || ctx->fdmon_ops->need_wait(ctx)) {
660 * Disable poll mode. poll mode should be disabled before the call
661 * of ctx->fdmon_ops->wait() so that guest's notification can wake
662 * up IO threads when some work becomes pending. It is essential to
663 * avoid hangs or unnecessary latency.
665 if (poll_set_started(ctx, &ready_list, false)) {
666 timeout = 0;
667 progress = true;
670 ctx->fdmon_ops->wait(ctx, &ready_list, timeout);
673 if (use_notify_me) {
674 /* Finish the poll before clearing the flag. */
675 qatomic_store_release(&ctx->notify_me,
676 qatomic_read(&ctx->notify_me) - 2);
679 aio_notify_accept(ctx);
681 /* Adjust polling time */
682 if (ctx->poll_max_ns) {
683 int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start;
685 if (block_ns <= ctx->poll_ns) {
686 /* This is the sweet spot, no adjustment needed */
687 } else if (block_ns > ctx->poll_max_ns) {
688 /* We'd have to poll for too long, poll less */
689 int64_t old = ctx->poll_ns;
691 if (ctx->poll_shrink) {
692 ctx->poll_ns /= ctx->poll_shrink;
693 } else {
694 ctx->poll_ns = 0;
697 trace_poll_shrink(ctx, old, ctx->poll_ns);
698 } else if (ctx->poll_ns < ctx->poll_max_ns &&
699 block_ns < ctx->poll_max_ns) {
700 /* There is room to grow, poll longer */
701 int64_t old = ctx->poll_ns;
702 int64_t grow = ctx->poll_grow;
704 if (grow == 0) {
705 grow = 2;
708 if (ctx->poll_ns) {
709 ctx->poll_ns *= grow;
710 } else {
711 ctx->poll_ns = 4000; /* start polling at 4 microseconds */
714 if (ctx->poll_ns > ctx->poll_max_ns) {
715 ctx->poll_ns = ctx->poll_max_ns;
718 trace_poll_grow(ctx, old, ctx->poll_ns);
722 progress |= aio_bh_poll(ctx);
723 progress |= aio_dispatch_ready_handlers(ctx, &ready_list);
725 aio_free_deleted_handlers(ctx);
727 qemu_lockcnt_dec(&ctx->list_lock);
729 progress |= timerlistgroup_run_timers(&ctx->tlg);
731 return progress;
734 void aio_context_setup(AioContext *ctx)
736 ctx->fdmon_ops = &fdmon_poll_ops;
737 ctx->epollfd = -1;
739 /* Use the fastest fd monitoring implementation if available */
740 if (fdmon_io_uring_setup(ctx)) {
741 return;
744 fdmon_epoll_setup(ctx);
747 void aio_context_destroy(AioContext *ctx)
749 fdmon_io_uring_destroy(ctx);
750 fdmon_epoll_disable(ctx);
751 aio_free_deleted_handlers(ctx);
754 void aio_context_use_g_source(AioContext *ctx)
757 * Disable io_uring when the glib main loop is used because it doesn't
758 * support mixed glib/aio_poll() usage. It relies on aio_poll() being
759 * called regularly so that changes to the monitored file descriptors are
760 * submitted, otherwise a list of pending fd handlers builds up.
762 fdmon_io_uring_destroy(ctx);
763 aio_free_deleted_handlers(ctx);
766 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
767 int64_t grow, int64_t shrink, Error **errp)
769 /* No thread synchronization here, it doesn't matter if an incorrect value
770 * is used once.
772 ctx->poll_max_ns = max_ns;
773 ctx->poll_ns = 0;
774 ctx->poll_grow = grow;
775 ctx->poll_shrink = shrink;
777 aio_notify(ctx);
780 void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch,
781 Error **errp)
784 * No thread synchronization here, it doesn't matter if an incorrect value
785 * is used once.
787 ctx->aio_max_batch = max_batch;
789 aio_notify(ctx);