aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 17 | #include "block/block.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/queue.h" |
| 19 | #include "qemu/sockets.h" |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 20 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 21 | struct AioHandler |
| 22 | { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 23 | GPollFD pfd; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 24 | IOHandler *io_read; |
| 25 | IOHandler *io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 26 | int deleted; |
| 27 | void *opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 28 | QLIST_ENTRY(AioHandler) node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 31 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 32 | { |
| 33 | AioHandler *node; |
| 34 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 35 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 36 | if (node->pfd.fd == fd) |
Alexander Graf | 79d5ca5 | 2009-05-06 02:58:48 +0200 | [diff] [blame] | 37 | if (!node->deleted) |
| 38 | return node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 44 | void aio_set_fd_handler(AioContext *ctx, |
| 45 | int fd, |
| 46 | IOHandler *io_read, |
| 47 | IOHandler *io_write, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 48 | void *opaque) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 49 | { |
| 50 | AioHandler *node; |
| 51 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 52 | node = find_aio_handler(ctx, fd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 53 | |
| 54 | /* Are we deleting the fd handler? */ |
| 55 | if (!io_read && !io_write) { |
| 56 | if (node) { |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 57 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 58 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 59 | /* If the lock is held, just mark the node as deleted */ |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 60 | if (ctx->walking_handlers) { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 61 | node->deleted = 1; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 62 | node->pfd.revents = 0; |
| 63 | } else { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 64 | /* Otherwise, delete it for real. We can't just mark it as |
| 65 | * deleted because deleted nodes are only cleaned up after |
| 66 | * releasing the walking_handlers lock. |
| 67 | */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 68 | QLIST_REMOVE(node, node); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 69 | g_free(node); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | if (node == NULL) { |
| 74 | /* Alloc and insert if it's not already there */ |
Markus Armbruster | 3ba235a | 2014-12-04 13:55:09 +0100 | [diff] [blame] | 75 | node = g_new0(AioHandler, 1); |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 76 | node->pfd.fd = fd; |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 77 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 78 | |
| 79 | g_source_add_poll(&ctx->source, &node->pfd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 80 | } |
| 81 | /* Update handler with latest information */ |
| 82 | node->io_read = io_read; |
| 83 | node->io_write = io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 84 | node->opaque = opaque; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 85 | |
Stefan Hajnoczi | b5a01a7 | 2013-02-20 11:28:33 +0100 | [diff] [blame] | 86 | node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); |
| 87 | node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 88 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 89 | |
| 90 | aio_notify(ctx); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 93 | void aio_set_event_notifier(AioContext *ctx, |
| 94 | EventNotifier *notifier, |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 95 | EventNotifierHandler *io_read) |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 96 | { |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 97 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 98 | (IOHandler *)io_read, NULL, notifier); |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 101 | bool aio_prepare(AioContext *ctx) |
| 102 | { |
| 103 | return false; |
| 104 | } |
| 105 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 106 | bool aio_pending(AioContext *ctx) |
| 107 | { |
| 108 | AioHandler *node; |
| 109 | |
| 110 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 111 | int revents; |
| 112 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 113 | revents = node->pfd.revents & node->pfd.events; |
| 114 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { |
| 115 | return true; |
| 116 | } |
| 117 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { |
| 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 125 | bool aio_dispatch(AioContext *ctx) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 126 | { |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 127 | AioHandler *node; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 128 | bool progress = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 129 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 130 | /* |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 131 | * If there are callbacks left that have been queued, we need to call them. |
| 132 | * Do not call select in this case, because it is possible that the caller |
| 133 | * does not need a complete flush (as is the case for aio_poll loops). |
| 134 | */ |
| 135 | if (aio_bh_poll(ctx)) { |
| 136 | progress = true; |
| 137 | } |
| 138 | |
| 139 | /* |
Paolo Bonzini | 87f68d3 | 2014-07-07 15:18:02 +0200 | [diff] [blame] | 140 | * We have to walk very carefully in case aio_set_fd_handler is |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 141 | * called while we're walking. |
| 142 | */ |
| 143 | node = QLIST_FIRST(&ctx->aio_handlers); |
| 144 | while (node) { |
| 145 | AioHandler *tmp; |
| 146 | int revents; |
| 147 | |
| 148 | ctx->walking_handlers++; |
| 149 | |
| 150 | revents = node->pfd.revents & node->pfd.events; |
| 151 | node->pfd.revents = 0; |
| 152 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 153 | if (!node->deleted && |
| 154 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
| 155 | node->io_read) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 156 | node->io_read(node->opaque); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 157 | |
| 158 | /* aio_notify() does not count as progress */ |
| 159 | if (node->opaque != &ctx->notifier) { |
| 160 | progress = true; |
| 161 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 162 | } |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 163 | if (!node->deleted && |
| 164 | (revents & (G_IO_OUT | G_IO_ERR)) && |
| 165 | node->io_write) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 166 | node->io_write(node->opaque); |
| 167 | progress = true; |
| 168 | } |
| 169 | |
| 170 | tmp = node; |
| 171 | node = QLIST_NEXT(node, node); |
| 172 | |
| 173 | ctx->walking_handlers--; |
| 174 | |
| 175 | if (!ctx->walking_handlers && tmp->deleted) { |
| 176 | QLIST_REMOVE(tmp, node); |
| 177 | g_free(tmp); |
| 178 | } |
| 179 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 180 | |
| 181 | /* Run our timers */ |
| 182 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
| 183 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 184 | return progress; |
| 185 | } |
| 186 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 187 | /* These thread-local variables are used only in a small part of aio_poll |
| 188 | * around the call to the poll() system call. In particular they are not |
| 189 | * used while aio_poll is performing callbacks, which makes it much easier |
| 190 | * to think about reentrancy! |
| 191 | * |
| 192 | * Stack-allocated arrays would be perfect but they have size limitations; |
| 193 | * heap allocation is expensive enough that we want to reuse arrays across |
| 194 | * calls to aio_poll(). And because poll() has to be called without holding |
| 195 | * any lock, the arrays cannot be stored in AioContext. Thread-local data |
| 196 | * has none of the disadvantages of these three options. |
| 197 | */ |
| 198 | static __thread GPollFD *pollfds; |
| 199 | static __thread AioHandler **nodes; |
| 200 | static __thread unsigned npfd, nalloc; |
| 201 | static __thread Notifier pollfds_cleanup_notifier; |
| 202 | |
| 203 | static void pollfds_cleanup(Notifier *n, void *unused) |
| 204 | { |
| 205 | g_assert(npfd == 0); |
| 206 | g_free(pollfds); |
| 207 | g_free(nodes); |
| 208 | nalloc = 0; |
| 209 | } |
| 210 | |
| 211 | static void add_pollfd(AioHandler *node) |
| 212 | { |
| 213 | if (npfd == nalloc) { |
| 214 | if (nalloc == 0) { |
| 215 | pollfds_cleanup_notifier.notify = pollfds_cleanup; |
| 216 | qemu_thread_atexit_add(&pollfds_cleanup_notifier); |
| 217 | nalloc = 8; |
| 218 | } else { |
| 219 | g_assert(nalloc <= INT_MAX); |
| 220 | nalloc *= 2; |
| 221 | } |
| 222 | pollfds = g_renew(GPollFD, pollfds, nalloc); |
| 223 | nodes = g_renew(AioHandler *, nodes, nalloc); |
| 224 | } |
| 225 | nodes[npfd] = node; |
| 226 | pollfds[npfd] = (GPollFD) { |
| 227 | .fd = node->pfd.fd, |
| 228 | .events = node->pfd.events, |
| 229 | }; |
| 230 | npfd++; |
| 231 | } |
| 232 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 233 | bool aio_poll(AioContext *ctx, bool blocking) |
| 234 | { |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 235 | AioHandler *node; |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 236 | bool was_dispatching; |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 237 | int i, ret; |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 238 | bool progress; |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 239 | int64_t timeout; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 240 | |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 241 | aio_context_acquire(ctx); |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 242 | was_dispatching = ctx->dispatching; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 243 | progress = false; |
| 244 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 245 | /* aio_notify can avoid the expensive event_notifier_set if |
| 246 | * everything (file descriptors, bottom halves, timers) will |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 247 | * be re-evaluated before the next blocking poll(). This is |
| 248 | * already true when aio_poll is called with blocking == false; |
| 249 | * if blocking == true, it is only true after poll() returns. |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 250 | * |
| 251 | * If we're in a nested event loop, ctx->dispatching might be true. |
| 252 | * In that case we can restore it just before returning, but we |
| 253 | * have to clear it now. |
| 254 | */ |
| 255 | aio_set_dispatching(ctx, !blocking); |
| 256 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 257 | ctx->walking_handlers++; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 258 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 259 | assert(npfd == 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 260 | |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 261 | /* fill pollfds */ |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 262 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 263 | if (!node->deleted && node->pfd.events) { |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 264 | add_pollfd(node); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 268 | timeout = blocking ? aio_compute_timeout(ctx) : 0; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 269 | |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 270 | /* wait until next event */ |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 271 | if (timeout) { |
| 272 | aio_context_release(ctx); |
| 273 | } |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 274 | ret = qemu_poll_ns((GPollFD *)pollfds, npfd, timeout); |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 275 | if (timeout) { |
| 276 | aio_context_acquire(ctx); |
| 277 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 278 | |
| 279 | /* if we have any readable fds, dispatch event */ |
| 280 | if (ret > 0) { |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 281 | for (i = 0; i < npfd; i++) { |
| 282 | nodes[i]->pfd.revents = pollfds[i].revents; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 283 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 286 | npfd = 0; |
| 287 | ctx->walking_handlers--; |
| 288 | |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 289 | /* Run dispatch even if there were no readable fds to run timers */ |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 290 | aio_set_dispatching(ctx, true); |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 291 | if (aio_dispatch(ctx)) { |
| 292 | progress = true; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 293 | } |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 294 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 295 | aio_set_dispatching(ctx, was_dispatching); |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 296 | aio_context_release(ctx); |
| 297 | |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 298 | return progress; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 299 | } |