Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU aio implementation |
| 3 | * |
| 4 | * Copyright IBM Corp., 2008 |
| 5 | * Copyright Red Hat Inc., 2012 |
| 6 | * |
| 7 | * Authors: |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * Paolo Bonzini <pbonzini@redhat.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 12 | * the COPYING file in the top-level directory. |
| 13 | * |
| 14 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 15 | * GNU GPL, version 2 or (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 19 | #include "block/block.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 20 | #include "qemu/queue.h" |
| 21 | #include "qemu/sockets.h" |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 22 | |
| 23 | struct AioHandler { |
| 24 | EventNotifier *e; |
| 25 | EventNotifierHandler *io_notify; |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 26 | GPollFD pfd; |
| 27 | int deleted; |
| 28 | QLIST_ENTRY(AioHandler) node; |
| 29 | }; |
| 30 | |
| 31 | void aio_set_event_notifier(AioContext *ctx, |
| 32 | EventNotifier *e, |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 33 | EventNotifierHandler *io_notify) |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 34 | { |
| 35 | AioHandler *node; |
| 36 | |
| 37 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 38 | if (node->e == e && !node->deleted) { |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /* Are we deleting the fd handler? */ |
| 44 | if (!io_notify) { |
| 45 | if (node) { |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 46 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 47 | |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 48 | /* If the lock is held, just mark the node as deleted */ |
| 49 | if (ctx->walking_handlers) { |
| 50 | node->deleted = 1; |
| 51 | node->pfd.revents = 0; |
| 52 | } else { |
| 53 | /* Otherwise, delete it for real. We can't just mark it as |
| 54 | * deleted because deleted nodes are only cleaned up after |
| 55 | * releasing the walking_handlers lock. |
| 56 | */ |
| 57 | QLIST_REMOVE(node, node); |
| 58 | g_free(node); |
| 59 | } |
| 60 | } |
| 61 | } else { |
| 62 | if (node == NULL) { |
| 63 | /* Alloc and insert if it's not already there */ |
| 64 | node = g_malloc0(sizeof(AioHandler)); |
| 65 | node->e = e; |
| 66 | node->pfd.fd = (uintptr_t)event_notifier_get_handle(e); |
| 67 | node->pfd.events = G_IO_IN; |
| 68 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 69 | |
| 70 | g_source_add_poll(&ctx->source, &node->pfd); |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 71 | } |
| 72 | /* Update handler with latest information */ |
| 73 | node->io_notify = io_notify; |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 74 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 75 | |
| 76 | aio_notify(ctx); |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame^] | 79 | bool aio_prepare(AioContext *ctx) |
| 80 | { |
| 81 | return false; |
| 82 | } |
| 83 | |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 84 | bool aio_pending(AioContext *ctx) |
| 85 | { |
| 86 | AioHandler *node; |
| 87 | |
| 88 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 89 | if (node->pfd.revents && node->io_notify) { |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 97 | static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event) |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 98 | { |
| 99 | AioHandler *node; |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 100 | bool progress = false; |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 101 | |
| 102 | /* |
Paolo Bonzini | 87f68d3 | 2014-07-07 15:18:02 +0200 | [diff] [blame] | 103 | * We have to walk very carefully in case aio_set_fd_handler is |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 104 | * called while we're walking. |
| 105 | */ |
| 106 | node = QLIST_FIRST(&ctx->aio_handlers); |
| 107 | while (node) { |
| 108 | AioHandler *tmp; |
| 109 | |
| 110 | ctx->walking_handlers++; |
| 111 | |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 112 | if (!node->deleted && |
| 113 | (node->pfd.revents || event_notifier_get_handle(node->e) == event) && |
| 114 | node->io_notify) { |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 115 | node->pfd.revents = 0; |
| 116 | node->io_notify(node->e); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 117 | |
| 118 | /* aio_notify() does not count as progress */ |
Stefan Hajnoczi | 8b2d42d | 2013-08-22 15:28:35 +0200 | [diff] [blame] | 119 | if (node->e != &ctx->notifier) { |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 120 | progress = true; |
| 121 | } |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | tmp = node; |
| 125 | node = QLIST_NEXT(node, node); |
| 126 | |
| 127 | ctx->walking_handlers--; |
| 128 | |
| 129 | if (!ctx->walking_handlers && tmp->deleted) { |
| 130 | QLIST_REMOVE(tmp, node); |
| 131 | g_free(tmp); |
| 132 | } |
| 133 | } |
| 134 | |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 135 | return progress; |
| 136 | } |
| 137 | |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 138 | bool aio_dispatch(AioContext *ctx) |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 139 | { |
| 140 | bool progress; |
| 141 | |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 142 | progress = aio_bh_poll(ctx); |
| 143 | progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE); |
Paolo Bonzini | d397ec99 | 2014-07-09 11:53:02 +0200 | [diff] [blame] | 144 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 145 | return progress; |
| 146 | } |
| 147 | |
| 148 | bool aio_poll(AioContext *ctx, bool blocking) |
| 149 | { |
| 150 | AioHandler *node; |
| 151 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
Paolo Bonzini | 0a9dd16 | 2014-07-09 11:53:07 +0200 | [diff] [blame] | 152 | bool was_dispatching, progress, first; |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 153 | int count; |
| 154 | int timeout; |
| 155 | |
Paolo Bonzini | 0a9dd16 | 2014-07-09 11:53:07 +0200 | [diff] [blame] | 156 | was_dispatching = ctx->dispatching; |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 157 | progress = false; |
| 158 | |
Paolo Bonzini | 0a9dd16 | 2014-07-09 11:53:07 +0200 | [diff] [blame] | 159 | /* aio_notify can avoid the expensive event_notifier_set if |
| 160 | * everything (file descriptors, bottom halves, timers) will |
| 161 | * be re-evaluated before the next blocking poll(). This is |
| 162 | * already true when aio_poll is called with blocking == false; |
| 163 | * if blocking == true, it is only true after poll() returns. |
| 164 | * |
| 165 | * If we're in a nested event loop, ctx->dispatching might be true. |
| 166 | * In that case we can restore it just before returning, but we |
| 167 | * have to clear it now. |
| 168 | */ |
| 169 | aio_set_dispatching(ctx, !blocking); |
| 170 | |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 171 | ctx->walking_handlers++; |
| 172 | |
| 173 | /* fill fd sets */ |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 174 | count = 0; |
| 175 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 176 | if (!node->deleted && node->io_notify) { |
| 177 | events[count++] = event_notifier_get_handle(node->e); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | ctx->walking_handlers--; |
Paolo Bonzini | 3672fa5 | 2014-07-09 11:53:04 +0200 | [diff] [blame] | 182 | first = true; |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 183 | |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 184 | /* wait until next event */ |
Paolo Bonzini | b022b4a | 2012-11-23 15:59:43 +0100 | [diff] [blame] | 185 | while (count > 0) { |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 186 | int ret; |
| 187 | |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 188 | timeout = blocking |
| 189 | ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0; |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 190 | ret = WaitForMultipleObjects(count, events, FALSE, timeout); |
Paolo Bonzini | 0a9dd16 | 2014-07-09 11:53:07 +0200 | [diff] [blame] | 191 | aio_set_dispatching(ctx, true); |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 192 | |
Paolo Bonzini | 3672fa5 | 2014-07-09 11:53:04 +0200 | [diff] [blame] | 193 | if (first && aio_bh_poll(ctx)) { |
| 194 | progress = true; |
| 195 | } |
| 196 | first = false; |
| 197 | |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 198 | /* if we have any signaled events, dispatch event */ |
| 199 | if ((DWORD) (ret - WAIT_OBJECT_0) >= count) { |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | blocking = false; |
| 204 | |
Paolo Bonzini | a398dea | 2014-07-09 11:53:03 +0200 | [diff] [blame] | 205 | progress |= aio_dispatch_handlers(ctx, events[ret - WAIT_OBJECT_0]); |
Paolo Bonzini | b022b4a | 2012-11-23 15:59:43 +0100 | [diff] [blame] | 206 | |
| 207 | /* Try again, but only call each handler once. */ |
| 208 | events[ret - WAIT_OBJECT_0] = events[--count]; |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 211 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 212 | |
Paolo Bonzini | 0a9dd16 | 2014-07-09 11:53:07 +0200 | [diff] [blame] | 213 | aio_set_dispatching(ctx, was_dispatching); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 214 | return progress; |
Paolo Bonzini | f42b220 | 2012-06-09 04:01:51 +0200 | [diff] [blame] | 215 | } |