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; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 27 | int pollfds_idx; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 28 | void *opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 29 | QLIST_ENTRY(AioHandler) node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 32 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 33 | { |
| 34 | AioHandler *node; |
| 35 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 36 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 37 | if (node->pfd.fd == fd) |
Alexander Graf | 79d5ca5 | 2009-05-06 02:58:48 +0200 | [diff] [blame] | 38 | if (!node->deleted) |
| 39 | return node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | return NULL; |
| 43 | } |
| 44 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 45 | void aio_set_fd_handler(AioContext *ctx, |
| 46 | int fd, |
| 47 | IOHandler *io_read, |
| 48 | IOHandler *io_write, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 49 | void *opaque) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 50 | { |
| 51 | AioHandler *node; |
| 52 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 53 | node = find_aio_handler(ctx, fd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 54 | |
| 55 | /* Are we deleting the fd handler? */ |
| 56 | if (!io_read && !io_write) { |
| 57 | if (node) { |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 58 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 59 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 60 | /* If the lock is held, just mark the node as deleted */ |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 61 | if (ctx->walking_handlers) { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 62 | node->deleted = 1; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 63 | node->pfd.revents = 0; |
| 64 | } else { |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 65 | /* Otherwise, delete it for real. We can't just mark it as |
| 66 | * deleted because deleted nodes are only cleaned up after |
| 67 | * releasing the walking_handlers lock. |
| 68 | */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 69 | QLIST_REMOVE(node, node); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 70 | g_free(node); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } else { |
| 74 | if (node == NULL) { |
| 75 | /* Alloc and insert if it's not already there */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 76 | node = g_malloc0(sizeof(AioHandler)); |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 77 | node->pfd.fd = fd; |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 78 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 79 | |
| 80 | g_source_add_poll(&ctx->source, &node->pfd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 81 | } |
| 82 | /* Update handler with latest information */ |
| 83 | node->io_read = io_read; |
| 84 | node->io_write = io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 85 | node->opaque = opaque; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 86 | node->pollfds_idx = -1; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 87 | |
Stefan Hajnoczi | b5a01a7 | 2013-02-20 11:28:33 +0100 | [diff] [blame] | 88 | node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); |
| 89 | node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 90 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 91 | |
| 92 | aio_notify(ctx); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 95 | void aio_set_event_notifier(AioContext *ctx, |
| 96 | EventNotifier *notifier, |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 97 | EventNotifierHandler *io_read) |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 98 | { |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 99 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 100 | (IOHandler *)io_read, NULL, notifier); |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 103 | bool aio_prepare(AioContext *ctx) |
| 104 | { |
| 105 | return false; |
| 106 | } |
| 107 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 108 | bool aio_pending(AioContext *ctx) |
| 109 | { |
| 110 | AioHandler *node; |
| 111 | |
| 112 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 113 | int revents; |
| 114 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 115 | revents = node->pfd.revents & node->pfd.events; |
| 116 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { |
| 117 | return true; |
| 118 | } |
| 119 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 127 | bool aio_dispatch(AioContext *ctx) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 128 | { |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 129 | AioHandler *node; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 130 | bool progress = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 131 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 132 | /* |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 133 | * If there are callbacks left that have been queued, we need to call them. |
| 134 | * Do not call select in this case, because it is possible that the caller |
| 135 | * does not need a complete flush (as is the case for aio_poll loops). |
| 136 | */ |
| 137 | if (aio_bh_poll(ctx)) { |
| 138 | progress = true; |
| 139 | } |
| 140 | |
| 141 | /* |
Paolo Bonzini | 87f68d3 | 2014-07-07 15:18:02 +0200 | [diff] [blame] | 142 | * 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] | 143 | * called while we're walking. |
| 144 | */ |
| 145 | node = QLIST_FIRST(&ctx->aio_handlers); |
| 146 | while (node) { |
| 147 | AioHandler *tmp; |
| 148 | int revents; |
| 149 | |
| 150 | ctx->walking_handlers++; |
| 151 | |
| 152 | revents = node->pfd.revents & node->pfd.events; |
| 153 | node->pfd.revents = 0; |
| 154 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 155 | if (!node->deleted && |
| 156 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
| 157 | node->io_read) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 158 | node->io_read(node->opaque); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 159 | |
| 160 | /* aio_notify() does not count as progress */ |
| 161 | if (node->opaque != &ctx->notifier) { |
| 162 | progress = true; |
| 163 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 164 | } |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 165 | if (!node->deleted && |
| 166 | (revents & (G_IO_OUT | G_IO_ERR)) && |
| 167 | node->io_write) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 168 | node->io_write(node->opaque); |
| 169 | progress = true; |
| 170 | } |
| 171 | |
| 172 | tmp = node; |
| 173 | node = QLIST_NEXT(node, node); |
| 174 | |
| 175 | ctx->walking_handlers--; |
| 176 | |
| 177 | if (!ctx->walking_handlers && tmp->deleted) { |
| 178 | QLIST_REMOVE(tmp, node); |
| 179 | g_free(tmp); |
| 180 | } |
| 181 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 182 | |
| 183 | /* Run our timers */ |
| 184 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
| 185 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 186 | return progress; |
| 187 | } |
| 188 | |
| 189 | bool aio_poll(AioContext *ctx, bool blocking) |
| 190 | { |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 191 | AioHandler *node; |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 192 | bool was_dispatching; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 193 | int ret; |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 194 | bool progress; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 195 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 196 | was_dispatching = ctx->dispatching; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 197 | progress = false; |
| 198 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 199 | /* aio_notify can avoid the expensive event_notifier_set if |
| 200 | * everything (file descriptors, bottom halves, timers) will |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 201 | * be re-evaluated before the next blocking poll(). This is |
| 202 | * already true when aio_poll is called with blocking == false; |
| 203 | * if blocking == true, it is only true after poll() returns. |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 204 | * |
| 205 | * If we're in a nested event loop, ctx->dispatching might be true. |
| 206 | * In that case we can restore it just before returning, but we |
| 207 | * have to clear it now. |
| 208 | */ |
| 209 | aio_set_dispatching(ctx, !blocking); |
| 210 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 211 | ctx->walking_handlers++; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 212 | |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 213 | g_array_set_size(ctx->pollfds, 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 214 | |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 215 | /* fill pollfds */ |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 216 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 217 | node->pollfds_idx = -1; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 218 | if (!node->deleted && node->pfd.events) { |
| 219 | GPollFD pfd = { |
| 220 | .fd = node->pfd.fd, |
| 221 | .events = node->pfd.events, |
| 222 | }; |
| 223 | node->pollfds_idx = ctx->pollfds->len; |
| 224 | g_array_append_val(ctx->pollfds, pfd); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 228 | ctx->walking_handlers--; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 229 | |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 230 | /* wait until next event */ |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 231 | ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data, |
| 232 | ctx->pollfds->len, |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 233 | blocking ? aio_compute_timeout(ctx) : 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 234 | |
| 235 | /* if we have any readable fds, dispatch event */ |
| 236 | if (ret > 0) { |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 237 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 238 | if (node->pollfds_idx != -1) { |
| 239 | GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD, |
| 240 | node->pollfds_idx); |
| 241 | node->pfd.revents = pfd->revents; |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 242 | } |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 243 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* Run dispatch even if there were no readable fds to run timers */ |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 247 | aio_set_dispatching(ctx, true); |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 248 | if (aio_dispatch(ctx)) { |
| 249 | progress = true; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 250 | } |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 251 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 252 | aio_set_dispatching(ctx, was_dispatching); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 253 | return progress; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 254 | } |