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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 17 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 18 | #include "block/block.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 19 | #include "qemu/queue.h" |
| 20 | #include "qemu/sockets.h" |
Matthew Fortune | 147dfab | 2016-02-23 15:42:14 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_EPOLL_CREATE1 |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 22 | #include <sys/epoll.h> |
| 23 | #endif |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 24 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 25 | struct AioHandler |
| 26 | { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 27 | GPollFD pfd; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 28 | IOHandler *io_read; |
| 29 | IOHandler *io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 30 | int deleted; |
| 31 | void *opaque; |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 32 | bool is_external; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 33 | QLIST_ENTRY(AioHandler) node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Matthew Fortune | 147dfab | 2016-02-23 15:42:14 +0000 | [diff] [blame] | 36 | #ifdef CONFIG_EPOLL_CREATE1 |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 37 | |
| 38 | /* The fd number threashold to switch to epoll */ |
| 39 | #define EPOLL_ENABLE_THRESHOLD 64 |
| 40 | |
| 41 | static void aio_epoll_disable(AioContext *ctx) |
| 42 | { |
| 43 | ctx->epoll_available = false; |
| 44 | if (!ctx->epoll_enabled) { |
| 45 | return; |
| 46 | } |
| 47 | ctx->epoll_enabled = false; |
| 48 | close(ctx->epollfd); |
| 49 | } |
| 50 | |
| 51 | static inline int epoll_events_from_pfd(int pfd_events) |
| 52 | { |
| 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); |
| 57 | } |
| 58 | |
| 59 | static bool aio_epoll_try_enable(AioContext *ctx) |
| 60 | { |
| 61 | AioHandler *node; |
| 62 | struct epoll_event event; |
| 63 | |
| 64 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 65 | int r; |
| 66 | if (node->deleted || !node->pfd.events) { |
| 67 | continue; |
| 68 | } |
| 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); |
| 72 | if (r) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | ctx->epoll_enabled = true; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new) |
| 81 | { |
| 82 | struct epoll_event event; |
| 83 | int r; |
Paolo Bonzini | 35dd66e | 2016-11-08 14:55:24 +0100 | [diff] [blame] | 84 | int ctl; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 85 | |
| 86 | if (!ctx->epoll_enabled) { |
| 87 | return; |
| 88 | } |
| 89 | if (!node->pfd.events) { |
Paolo Bonzini | 35dd66e | 2016-11-08 14:55:24 +0100 | [diff] [blame] | 90 | ctl = EPOLL_CTL_DEL; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 91 | } else { |
| 92 | event.data.ptr = node; |
| 93 | event.events = epoll_events_from_pfd(node->pfd.events); |
Paolo Bonzini | 35dd66e | 2016-11-08 14:55:24 +0100 | [diff] [blame] | 94 | ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD; |
| 95 | } |
| 96 | |
| 97 | r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event); |
| 98 | if (r) { |
| 99 | aio_epoll_disable(ctx); |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | static int aio_epoll(AioContext *ctx, GPollFD *pfds, |
| 104 | unsigned npfd, int64_t timeout) |
| 105 | { |
| 106 | AioHandler *node; |
| 107 | int i, ret = 0; |
| 108 | struct epoll_event events[128]; |
| 109 | |
| 110 | assert(npfd == 1); |
| 111 | assert(pfds[0].fd == ctx->epollfd); |
| 112 | if (timeout > 0) { |
| 113 | ret = qemu_poll_ns(pfds, npfd, timeout); |
| 114 | } |
| 115 | if (timeout <= 0 || ret > 0) { |
| 116 | ret = epoll_wait(ctx->epollfd, events, |
| 117 | sizeof(events) / sizeof(events[0]), |
| 118 | timeout); |
| 119 | if (ret <= 0) { |
| 120 | goto out; |
| 121 | } |
| 122 | for (i = 0; i < ret; i++) { |
| 123 | int ev = events[i].events; |
| 124 | node = events[i].data.ptr; |
| 125 | node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) | |
| 126 | (ev & EPOLLOUT ? G_IO_OUT : 0) | |
| 127 | (ev & EPOLLHUP ? G_IO_HUP : 0) | |
| 128 | (ev & EPOLLERR ? G_IO_ERR : 0); |
| 129 | } |
| 130 | } |
| 131 | out: |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | static bool aio_epoll_enabled(AioContext *ctx) |
| 136 | { |
| 137 | /* Fall back to ppoll when external clients are disabled. */ |
| 138 | return !aio_external_disabled(ctx) && ctx->epoll_enabled; |
| 139 | } |
| 140 | |
| 141 | static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds, |
| 142 | unsigned npfd, int64_t timeout) |
| 143 | { |
| 144 | if (!ctx->epoll_available) { |
| 145 | return false; |
| 146 | } |
| 147 | if (aio_epoll_enabled(ctx)) { |
| 148 | return true; |
| 149 | } |
| 150 | if (npfd >= EPOLL_ENABLE_THRESHOLD) { |
| 151 | if (aio_epoll_try_enable(ctx)) { |
| 152 | return true; |
| 153 | } else { |
| 154 | aio_epoll_disable(ctx); |
| 155 | } |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | #else |
| 161 | |
| 162 | static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | static int aio_epoll(AioContext *ctx, GPollFD *pfds, |
| 167 | unsigned npfd, int64_t timeout) |
| 168 | { |
| 169 | assert(false); |
| 170 | } |
| 171 | |
| 172 | static bool aio_epoll_enabled(AioContext *ctx) |
| 173 | { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds, |
| 178 | unsigned npfd, int64_t timeout) |
| 179 | { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | #endif |
| 184 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 185 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 186 | { |
| 187 | AioHandler *node; |
| 188 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 189 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 190 | if (node->pfd.fd == fd) |
Alexander Graf | 79d5ca5 | 2009-05-06 02:58:48 +0200 | [diff] [blame] | 191 | if (!node->deleted) |
| 192 | return node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return NULL; |
| 196 | } |
| 197 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 198 | void aio_set_fd_handler(AioContext *ctx, |
| 199 | int fd, |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 200 | bool is_external, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 201 | IOHandler *io_read, |
| 202 | IOHandler *io_write, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 203 | void *opaque) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 204 | { |
| 205 | AioHandler *node; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 206 | bool is_new = false; |
Fam Zheng | 0ed39f3d | 2015-11-16 14:32:14 +0800 | [diff] [blame] | 207 | bool deleted = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 208 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 209 | node = find_aio_handler(ctx, fd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 210 | |
| 211 | /* Are we deleting the fd handler? */ |
| 212 | if (!io_read && !io_write) { |
Paolo Bonzini | 36173ec | 2016-11-08 14:55:23 +0100 | [diff] [blame] | 213 | if (node == NULL) { |
| 214 | return; |
| 215 | } |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 216 | |
Paolo Bonzini | 36173ec | 2016-11-08 14:55:23 +0100 | [diff] [blame] | 217 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 218 | |
| 219 | /* If the lock is held, just mark the node as deleted */ |
| 220 | if (ctx->walking_handlers) { |
| 221 | node->deleted = 1; |
| 222 | node->pfd.revents = 0; |
| 223 | } else { |
| 224 | /* Otherwise, delete it for real. We can't just mark it as |
| 225 | * deleted because deleted nodes are only cleaned up after |
| 226 | * releasing the walking_handlers lock. |
| 227 | */ |
| 228 | QLIST_REMOVE(node, node); |
| 229 | deleted = true; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 230 | } |
| 231 | } else { |
| 232 | if (node == NULL) { |
| 233 | /* Alloc and insert if it's not already there */ |
Markus Armbruster | 3ba235a | 2014-12-04 13:55:09 +0100 | [diff] [blame] | 234 | node = g_new0(AioHandler, 1); |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 235 | node->pfd.fd = fd; |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 236 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 237 | |
| 238 | g_source_add_poll(&ctx->source, &node->pfd); |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 239 | is_new = true; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 240 | } |
| 241 | /* Update handler with latest information */ |
| 242 | node->io_read = io_read; |
| 243 | node->io_write = io_write; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 244 | node->opaque = opaque; |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 245 | node->is_external = is_external; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 246 | |
Stefan Hajnoczi | b5a01a7 | 2013-02-20 11:28:33 +0100 | [diff] [blame] | 247 | node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); |
| 248 | node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 249 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 250 | |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 251 | aio_epoll_update(ctx, node, is_new); |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 252 | aio_notify(ctx); |
Fam Zheng | 0ed39f3d | 2015-11-16 14:32:14 +0800 | [diff] [blame] | 253 | if (deleted) { |
| 254 | g_free(node); |
| 255 | } |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 258 | void aio_set_event_notifier(AioContext *ctx, |
| 259 | EventNotifier *notifier, |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 260 | bool is_external, |
Stefan Hajnoczi | f2e5dca | 2013-04-11 17:26:25 +0200 | [diff] [blame] | 261 | EventNotifierHandler *io_read) |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 262 | { |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 263 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 264 | is_external, (IOHandler *)io_read, NULL, notifier); |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 267 | bool aio_prepare(AioContext *ctx) |
| 268 | { |
| 269 | return false; |
| 270 | } |
| 271 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 272 | bool aio_pending(AioContext *ctx) |
| 273 | { |
| 274 | AioHandler *node; |
| 275 | |
| 276 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 277 | int revents; |
| 278 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 279 | revents = node->pfd.revents & node->pfd.events; |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 280 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read && |
| 281 | aio_node_check(ctx, node->is_external)) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 282 | return true; |
| 283 | } |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 284 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write && |
| 285 | aio_node_check(ctx, node->is_external)) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return false; |
| 291 | } |
| 292 | |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame^] | 293 | /* |
| 294 | * Note that dispatch_fds == false has the side-effect of post-poning the |
| 295 | * freeing of deleted handlers. |
| 296 | */ |
| 297 | bool aio_dispatch(AioContext *ctx, bool dispatch_fds) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 298 | { |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame^] | 299 | AioHandler *node = NULL; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 300 | bool progress = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 301 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 302 | /* |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 303 | * If there are callbacks left that have been queued, we need to call them. |
| 304 | * Do not call select in this case, because it is possible that the caller |
| 305 | * does not need a complete flush (as is the case for aio_poll loops). |
| 306 | */ |
| 307 | if (aio_bh_poll(ctx)) { |
| 308 | progress = true; |
| 309 | } |
| 310 | |
| 311 | /* |
Paolo Bonzini | 87f68d3 | 2014-07-07 15:18:02 +0200 | [diff] [blame] | 312 | * 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] | 313 | * called while we're walking. |
| 314 | */ |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame^] | 315 | if (dispatch_fds) { |
| 316 | node = QLIST_FIRST(&ctx->aio_handlers); |
| 317 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 318 | while (node) { |
| 319 | AioHandler *tmp; |
| 320 | int revents; |
| 321 | |
| 322 | ctx->walking_handlers++; |
| 323 | |
| 324 | revents = node->pfd.revents & node->pfd.events; |
| 325 | node->pfd.revents = 0; |
| 326 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 327 | if (!node->deleted && |
| 328 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 329 | aio_node_check(ctx, node->is_external) && |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 330 | node->io_read) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 331 | node->io_read(node->opaque); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 332 | |
| 333 | /* aio_notify() does not count as progress */ |
| 334 | if (node->opaque != &ctx->notifier) { |
| 335 | progress = true; |
| 336 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 337 | } |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 338 | if (!node->deleted && |
| 339 | (revents & (G_IO_OUT | G_IO_ERR)) && |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 340 | aio_node_check(ctx, node->is_external) && |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 341 | node->io_write) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 342 | node->io_write(node->opaque); |
| 343 | progress = true; |
| 344 | } |
| 345 | |
| 346 | tmp = node; |
| 347 | node = QLIST_NEXT(node, node); |
| 348 | |
| 349 | ctx->walking_handlers--; |
| 350 | |
| 351 | if (!ctx->walking_handlers && tmp->deleted) { |
| 352 | QLIST_REMOVE(tmp, node); |
| 353 | g_free(tmp); |
| 354 | } |
| 355 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 356 | |
| 357 | /* Run our timers */ |
| 358 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
| 359 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 360 | return progress; |
| 361 | } |
| 362 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 363 | /* These thread-local variables are used only in a small part of aio_poll |
| 364 | * around the call to the poll() system call. In particular they are not |
| 365 | * used while aio_poll is performing callbacks, which makes it much easier |
| 366 | * to think about reentrancy! |
| 367 | * |
| 368 | * Stack-allocated arrays would be perfect but they have size limitations; |
| 369 | * heap allocation is expensive enough that we want to reuse arrays across |
| 370 | * calls to aio_poll(). And because poll() has to be called without holding |
| 371 | * any lock, the arrays cannot be stored in AioContext. Thread-local data |
| 372 | * has none of the disadvantages of these three options. |
| 373 | */ |
| 374 | static __thread GPollFD *pollfds; |
| 375 | static __thread AioHandler **nodes; |
| 376 | static __thread unsigned npfd, nalloc; |
| 377 | static __thread Notifier pollfds_cleanup_notifier; |
| 378 | |
| 379 | static void pollfds_cleanup(Notifier *n, void *unused) |
| 380 | { |
| 381 | g_assert(npfd == 0); |
| 382 | g_free(pollfds); |
| 383 | g_free(nodes); |
| 384 | nalloc = 0; |
| 385 | } |
| 386 | |
| 387 | static void add_pollfd(AioHandler *node) |
| 388 | { |
| 389 | if (npfd == nalloc) { |
| 390 | if (nalloc == 0) { |
| 391 | pollfds_cleanup_notifier.notify = pollfds_cleanup; |
| 392 | qemu_thread_atexit_add(&pollfds_cleanup_notifier); |
| 393 | nalloc = 8; |
| 394 | } else { |
| 395 | g_assert(nalloc <= INT_MAX); |
| 396 | nalloc *= 2; |
| 397 | } |
| 398 | pollfds = g_renew(GPollFD, pollfds, nalloc); |
| 399 | nodes = g_renew(AioHandler *, nodes, nalloc); |
| 400 | } |
| 401 | nodes[npfd] = node; |
| 402 | pollfds[npfd] = (GPollFD) { |
| 403 | .fd = node->pfd.fd, |
| 404 | .events = node->pfd.events, |
| 405 | }; |
| 406 | npfd++; |
| 407 | } |
| 408 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 409 | bool aio_poll(AioContext *ctx, bool blocking) |
| 410 | { |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 411 | AioHandler *node; |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 412 | int i, ret; |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 413 | bool progress; |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 414 | int64_t timeout; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 415 | |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 416 | aio_context_acquire(ctx); |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 417 | progress = false; |
| 418 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 419 | /* aio_notify can avoid the expensive event_notifier_set if |
| 420 | * everything (file descriptors, bottom halves, timers) will |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 421 | * be re-evaluated before the next blocking poll(). This is |
| 422 | * already true when aio_poll is called with blocking == false; |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 423 | * if blocking == true, it is only true after poll() returns, |
| 424 | * so disable the optimization now. |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 425 | */ |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 426 | if (blocking) { |
| 427 | atomic_add(&ctx->notify_me, 2); |
| 428 | } |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 429 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 430 | ctx->walking_handlers++; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 431 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 432 | assert(npfd == 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 433 | |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 434 | /* fill pollfds */ |
Yaowei Bai | 6b94246 | 2016-09-14 07:03:39 -0400 | [diff] [blame] | 435 | |
| 436 | if (!aio_epoll_enabled(ctx)) { |
| 437 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 438 | if (!node->deleted && node->pfd.events |
| 439 | && aio_node_check(ctx, node->is_external)) { |
| 440 | add_pollfd(node); |
| 441 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 445 | timeout = blocking ? aio_compute_timeout(ctx) : 0; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 446 | |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 447 | /* wait until next event */ |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 448 | if (timeout) { |
| 449 | aio_context_release(ctx); |
| 450 | } |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 451 | if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) { |
| 452 | AioHandler epoll_handler; |
| 453 | |
| 454 | epoll_handler.pfd.fd = ctx->epollfd; |
| 455 | epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR; |
| 456 | npfd = 0; |
| 457 | add_pollfd(&epoll_handler); |
| 458 | ret = aio_epoll(ctx, pollfds, npfd, timeout); |
| 459 | } else { |
| 460 | ret = qemu_poll_ns(pollfds, npfd, timeout); |
| 461 | } |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 462 | if (blocking) { |
| 463 | atomic_sub(&ctx->notify_me, 2); |
| 464 | } |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 465 | if (timeout) { |
| 466 | aio_context_acquire(ctx); |
| 467 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 468 | |
Paolo Bonzini | 05e514b | 2015-07-21 16:07:53 +0200 | [diff] [blame] | 469 | aio_notify_accept(ctx); |
Paolo Bonzini | 21a03d1 | 2015-07-21 16:07:52 +0200 | [diff] [blame] | 470 | |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 471 | /* if we have any readable fds, dispatch event */ |
| 472 | if (ret > 0) { |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 473 | for (i = 0; i < npfd; i++) { |
| 474 | nodes[i]->pfd.revents = pollfds[i].revents; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 475 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 478 | npfd = 0; |
| 479 | ctx->walking_handlers--; |
| 480 | |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 481 | /* Run dispatch even if there were no readable fds to run timers */ |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame^] | 482 | if (aio_dispatch(ctx, ret > 0)) { |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 483 | progress = true; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 484 | } |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 485 | |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 486 | aio_context_release(ctx); |
| 487 | |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 488 | return progress; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 489 | } |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 490 | |
Cao jin | 7e00346 | 2016-07-15 18:28:44 +0800 | [diff] [blame] | 491 | void aio_context_setup(AioContext *ctx) |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 492 | { |
Matthew Fortune | 147dfab | 2016-02-23 15:42:14 +0000 | [diff] [blame] | 493 | #ifdef CONFIG_EPOLL_CREATE1 |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 494 | assert(!ctx->epollfd); |
| 495 | ctx->epollfd = epoll_create1(EPOLL_CLOEXEC); |
| 496 | if (ctx->epollfd == -1) { |
Cao jin | 7e00346 | 2016-07-15 18:28:44 +0800 | [diff] [blame] | 497 | fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno)); |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 498 | ctx->epoll_available = false; |
| 499 | } else { |
| 500 | ctx->epoll_available = true; |
| 501 | } |
| 502 | #endif |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 503 | } |