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" |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 21 | #include "qemu/cutils.h" |
| 22 | #include "trace.h" |
Matthew Fortune | 147dfab | 2016-02-23 15:42:14 +0000 | [diff] [blame] | 23 | #ifdef CONFIG_EPOLL_CREATE1 |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 24 | #include <sys/epoll.h> |
| 25 | #endif |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 26 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 27 | struct AioHandler |
| 28 | { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 29 | GPollFD pfd; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 30 | IOHandler *io_read; |
| 31 | IOHandler *io_write; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 32 | AioPollFn *io_poll; |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 33 | IOHandler *io_poll_begin; |
| 34 | IOHandler *io_poll_end; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 35 | int deleted; |
| 36 | void *opaque; |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 37 | bool is_external; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 38 | QLIST_ENTRY(AioHandler) node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Matthew Fortune | 147dfab | 2016-02-23 15:42:14 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_EPOLL_CREATE1 |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 42 | |
| 43 | /* The fd number threashold to switch to epoll */ |
| 44 | #define EPOLL_ENABLE_THRESHOLD 64 |
| 45 | |
| 46 | static void aio_epoll_disable(AioContext *ctx) |
| 47 | { |
| 48 | ctx->epoll_available = false; |
| 49 | if (!ctx->epoll_enabled) { |
| 50 | return; |
| 51 | } |
| 52 | ctx->epoll_enabled = false; |
| 53 | close(ctx->epollfd); |
| 54 | } |
| 55 | |
| 56 | static inline int epoll_events_from_pfd(int pfd_events) |
| 57 | { |
| 58 | return (pfd_events & G_IO_IN ? EPOLLIN : 0) | |
| 59 | (pfd_events & G_IO_OUT ? EPOLLOUT : 0) | |
| 60 | (pfd_events & G_IO_HUP ? EPOLLHUP : 0) | |
| 61 | (pfd_events & G_IO_ERR ? EPOLLERR : 0); |
| 62 | } |
| 63 | |
| 64 | static bool aio_epoll_try_enable(AioContext *ctx) |
| 65 | { |
| 66 | AioHandler *node; |
| 67 | struct epoll_event event; |
| 68 | |
| 69 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 70 | int r; |
| 71 | if (node->deleted || !node->pfd.events) { |
| 72 | continue; |
| 73 | } |
| 74 | event.events = epoll_events_from_pfd(node->pfd.events); |
| 75 | event.data.ptr = node; |
| 76 | r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event); |
| 77 | if (r) { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | ctx->epoll_enabled = true; |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new) |
| 86 | { |
| 87 | struct epoll_event event; |
| 88 | int r; |
Paolo Bonzini | 35dd66e | 2016-11-08 14:55:24 +0100 | [diff] [blame] | 89 | int ctl; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 90 | |
| 91 | if (!ctx->epoll_enabled) { |
| 92 | return; |
| 93 | } |
| 94 | if (!node->pfd.events) { |
Paolo Bonzini | 35dd66e | 2016-11-08 14:55:24 +0100 | [diff] [blame] | 95 | ctl = EPOLL_CTL_DEL; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 96 | } else { |
| 97 | event.data.ptr = node; |
| 98 | event.events = epoll_events_from_pfd(node->pfd.events); |
Paolo Bonzini | 35dd66e | 2016-11-08 14:55:24 +0100 | [diff] [blame] | 99 | ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD; |
| 100 | } |
| 101 | |
| 102 | r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event); |
| 103 | if (r) { |
| 104 | aio_epoll_disable(ctx); |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | static int aio_epoll(AioContext *ctx, GPollFD *pfds, |
| 109 | unsigned npfd, int64_t timeout) |
| 110 | { |
| 111 | AioHandler *node; |
| 112 | int i, ret = 0; |
| 113 | struct epoll_event events[128]; |
| 114 | |
| 115 | assert(npfd == 1); |
| 116 | assert(pfds[0].fd == ctx->epollfd); |
| 117 | if (timeout > 0) { |
| 118 | ret = qemu_poll_ns(pfds, npfd, timeout); |
| 119 | } |
| 120 | if (timeout <= 0 || ret > 0) { |
| 121 | ret = epoll_wait(ctx->epollfd, events, |
| 122 | sizeof(events) / sizeof(events[0]), |
| 123 | timeout); |
| 124 | if (ret <= 0) { |
| 125 | goto out; |
| 126 | } |
| 127 | for (i = 0; i < ret; i++) { |
| 128 | int ev = events[i].events; |
| 129 | node = events[i].data.ptr; |
| 130 | node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) | |
| 131 | (ev & EPOLLOUT ? G_IO_OUT : 0) | |
| 132 | (ev & EPOLLHUP ? G_IO_HUP : 0) | |
| 133 | (ev & EPOLLERR ? G_IO_ERR : 0); |
| 134 | } |
| 135 | } |
| 136 | out: |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | static bool aio_epoll_enabled(AioContext *ctx) |
| 141 | { |
| 142 | /* Fall back to ppoll when external clients are disabled. */ |
| 143 | return !aio_external_disabled(ctx) && ctx->epoll_enabled; |
| 144 | } |
| 145 | |
| 146 | static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds, |
| 147 | unsigned npfd, int64_t timeout) |
| 148 | { |
| 149 | if (!ctx->epoll_available) { |
| 150 | return false; |
| 151 | } |
| 152 | if (aio_epoll_enabled(ctx)) { |
| 153 | return true; |
| 154 | } |
| 155 | if (npfd >= EPOLL_ENABLE_THRESHOLD) { |
| 156 | if (aio_epoll_try_enable(ctx)) { |
| 157 | return true; |
| 158 | } else { |
| 159 | aio_epoll_disable(ctx); |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | #else |
| 166 | |
| 167 | static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | static int aio_epoll(AioContext *ctx, GPollFD *pfds, |
| 172 | unsigned npfd, int64_t timeout) |
| 173 | { |
| 174 | assert(false); |
| 175 | } |
| 176 | |
| 177 | static bool aio_epoll_enabled(AioContext *ctx) |
| 178 | { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds, |
| 183 | unsigned npfd, int64_t timeout) |
| 184 | { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | #endif |
| 189 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 190 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 191 | { |
| 192 | AioHandler *node; |
| 193 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 194 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 195 | if (node->pfd.fd == fd) |
Alexander Graf | 79d5ca5 | 2009-05-06 02:58:48 +0200 | [diff] [blame] | 196 | if (!node->deleted) |
| 197 | return node; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 203 | void aio_set_fd_handler(AioContext *ctx, |
| 204 | int fd, |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 205 | bool is_external, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 206 | IOHandler *io_read, |
| 207 | IOHandler *io_write, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 208 | AioPollFn *io_poll, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 209 | void *opaque) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 210 | { |
| 211 | AioHandler *node; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 212 | bool is_new = false; |
Fam Zheng | 0ed39f3d | 2015-11-16 14:32:14 +0800 | [diff] [blame] | 213 | bool deleted = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 214 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 215 | node = find_aio_handler(ctx, fd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 216 | |
| 217 | /* Are we deleting the fd handler? */ |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 218 | if (!io_read && !io_write && !io_poll) { |
Paolo Bonzini | 36173ec | 2016-11-08 14:55:23 +0100 | [diff] [blame] | 219 | if (node == NULL) { |
| 220 | return; |
| 221 | } |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 222 | |
Paolo Bonzini | 36173ec | 2016-11-08 14:55:23 +0100 | [diff] [blame] | 223 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 224 | |
| 225 | /* If the lock is held, just mark the node as deleted */ |
| 226 | if (ctx->walking_handlers) { |
| 227 | node->deleted = 1; |
| 228 | node->pfd.revents = 0; |
| 229 | } else { |
| 230 | /* Otherwise, delete it for real. We can't just mark it as |
| 231 | * deleted because deleted nodes are only cleaned up after |
| 232 | * releasing the walking_handlers lock. |
| 233 | */ |
| 234 | QLIST_REMOVE(node, node); |
| 235 | deleted = true; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 236 | } |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 237 | |
| 238 | if (!node->io_poll) { |
| 239 | ctx->poll_disable_cnt--; |
| 240 | } |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 241 | } else { |
| 242 | if (node == NULL) { |
| 243 | /* Alloc and insert if it's not already there */ |
Markus Armbruster | 3ba235a | 2014-12-04 13:55:09 +0100 | [diff] [blame] | 244 | node = g_new0(AioHandler, 1); |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 245 | node->pfd.fd = fd; |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 246 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 247 | |
| 248 | g_source_add_poll(&ctx->source, &node->pfd); |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 249 | is_new = true; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 250 | |
| 251 | ctx->poll_disable_cnt += !io_poll; |
| 252 | } else { |
| 253 | ctx->poll_disable_cnt += !io_poll - !node->io_poll; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 254 | } |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 255 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 256 | /* Update handler with latest information */ |
| 257 | node->io_read = io_read; |
| 258 | node->io_write = io_write; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 259 | node->io_poll = io_poll; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 260 | node->opaque = opaque; |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 261 | node->is_external = is_external; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 262 | |
Stefan Hajnoczi | b5a01a7 | 2013-02-20 11:28:33 +0100 | [diff] [blame] | 263 | node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); |
| 264 | node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 265 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 266 | |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 267 | aio_epoll_update(ctx, node, is_new); |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 268 | aio_notify(ctx); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 269 | |
Fam Zheng | 0ed39f3d | 2015-11-16 14:32:14 +0800 | [diff] [blame] | 270 | if (deleted) { |
| 271 | g_free(node); |
| 272 | } |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 275 | void aio_set_fd_poll(AioContext *ctx, int fd, |
| 276 | IOHandler *io_poll_begin, |
| 277 | IOHandler *io_poll_end) |
| 278 | { |
| 279 | AioHandler *node = find_aio_handler(ctx, fd); |
| 280 | |
| 281 | if (!node) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | node->io_poll_begin = io_poll_begin; |
| 286 | node->io_poll_end = io_poll_end; |
| 287 | } |
| 288 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 289 | void aio_set_event_notifier(AioContext *ctx, |
| 290 | EventNotifier *notifier, |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 291 | bool is_external, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 292 | EventNotifierHandler *io_read, |
| 293 | AioPollFn *io_poll) |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 294 | { |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 295 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), is_external, |
| 296 | (IOHandler *)io_read, NULL, io_poll, notifier); |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 299 | void aio_set_event_notifier_poll(AioContext *ctx, |
| 300 | EventNotifier *notifier, |
| 301 | EventNotifierHandler *io_poll_begin, |
| 302 | EventNotifierHandler *io_poll_end) |
| 303 | { |
| 304 | aio_set_fd_poll(ctx, event_notifier_get_fd(notifier), |
| 305 | (IOHandler *)io_poll_begin, |
| 306 | (IOHandler *)io_poll_end); |
| 307 | } |
| 308 | |
| 309 | static void poll_set_started(AioContext *ctx, bool started) |
| 310 | { |
| 311 | AioHandler *node; |
| 312 | |
| 313 | if (started == ctx->poll_started) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | ctx->poll_started = started; |
| 318 | |
| 319 | ctx->walking_handlers++; |
| 320 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 321 | IOHandler *fn; |
| 322 | |
| 323 | if (node->deleted) { |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | if (started) { |
| 328 | fn = node->io_poll_begin; |
| 329 | } else { |
| 330 | fn = node->io_poll_end; |
| 331 | } |
| 332 | |
| 333 | if (fn) { |
| 334 | fn(node->opaque); |
| 335 | } |
| 336 | } |
| 337 | ctx->walking_handlers--; |
| 338 | } |
| 339 | |
| 340 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 341 | bool aio_prepare(AioContext *ctx) |
| 342 | { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 343 | /* Poll mode cannot be used with glib's event loop, disable it. */ |
| 344 | poll_set_started(ctx, false); |
| 345 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 349 | bool aio_pending(AioContext *ctx) |
| 350 | { |
| 351 | AioHandler *node; |
| 352 | |
| 353 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 354 | int revents; |
| 355 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 356 | revents = node->pfd.revents & node->pfd.events; |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 357 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read && |
| 358 | aio_node_check(ctx, node->is_external)) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 359 | return true; |
| 360 | } |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 361 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write && |
| 362 | aio_node_check(ctx, node->is_external)) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return false; |
| 368 | } |
| 369 | |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame] | 370 | /* |
| 371 | * Note that dispatch_fds == false has the side-effect of post-poning the |
| 372 | * freeing of deleted handlers. |
| 373 | */ |
| 374 | bool aio_dispatch(AioContext *ctx, bool dispatch_fds) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 375 | { |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame] | 376 | AioHandler *node = NULL; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 377 | bool progress = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 378 | |
Kevin Wolf | 8febfa2 | 2009-10-22 17:54:36 +0200 | [diff] [blame] | 379 | /* |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 380 | * If there are callbacks left that have been queued, we need to call them. |
| 381 | * Do not call select in this case, because it is possible that the caller |
| 382 | * does not need a complete flush (as is the case for aio_poll loops). |
| 383 | */ |
| 384 | if (aio_bh_poll(ctx)) { |
| 385 | progress = true; |
| 386 | } |
| 387 | |
| 388 | /* |
Paolo Bonzini | 87f68d3 | 2014-07-07 15:18:02 +0200 | [diff] [blame] | 389 | * 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] | 390 | * called while we're walking. |
| 391 | */ |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame] | 392 | if (dispatch_fds) { |
| 393 | node = QLIST_FIRST(&ctx->aio_handlers); |
| 394 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 395 | while (node) { |
| 396 | AioHandler *tmp; |
| 397 | int revents; |
| 398 | |
| 399 | ctx->walking_handlers++; |
| 400 | |
| 401 | revents = node->pfd.revents & node->pfd.events; |
| 402 | node->pfd.revents = 0; |
| 403 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 404 | if (!node->deleted && |
| 405 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 406 | aio_node_check(ctx, node->is_external) && |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 407 | node->io_read) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 408 | node->io_read(node->opaque); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 409 | |
| 410 | /* aio_notify() does not count as progress */ |
| 411 | if (node->opaque != &ctx->notifier) { |
| 412 | progress = true; |
| 413 | } |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 414 | } |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 415 | if (!node->deleted && |
| 416 | (revents & (G_IO_OUT | G_IO_ERR)) && |
Fam Zheng | 37989ce | 2016-04-22 21:53:55 +0800 | [diff] [blame] | 417 | aio_node_check(ctx, node->is_external) && |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 418 | node->io_write) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 419 | node->io_write(node->opaque); |
| 420 | progress = true; |
| 421 | } |
| 422 | |
| 423 | tmp = node; |
| 424 | node = QLIST_NEXT(node, node); |
| 425 | |
| 426 | ctx->walking_handlers--; |
| 427 | |
| 428 | if (!ctx->walking_handlers && tmp->deleted) { |
| 429 | QLIST_REMOVE(tmp, node); |
| 430 | g_free(tmp); |
| 431 | } |
| 432 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 433 | |
| 434 | /* Run our timers */ |
| 435 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
| 436 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 437 | return progress; |
| 438 | } |
| 439 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 440 | /* These thread-local variables are used only in a small part of aio_poll |
| 441 | * around the call to the poll() system call. In particular they are not |
| 442 | * used while aio_poll is performing callbacks, which makes it much easier |
| 443 | * to think about reentrancy! |
| 444 | * |
| 445 | * Stack-allocated arrays would be perfect but they have size limitations; |
| 446 | * heap allocation is expensive enough that we want to reuse arrays across |
| 447 | * calls to aio_poll(). And because poll() has to be called without holding |
| 448 | * any lock, the arrays cannot be stored in AioContext. Thread-local data |
| 449 | * has none of the disadvantages of these three options. |
| 450 | */ |
| 451 | static __thread GPollFD *pollfds; |
| 452 | static __thread AioHandler **nodes; |
| 453 | static __thread unsigned npfd, nalloc; |
| 454 | static __thread Notifier pollfds_cleanup_notifier; |
| 455 | |
| 456 | static void pollfds_cleanup(Notifier *n, void *unused) |
| 457 | { |
| 458 | g_assert(npfd == 0); |
| 459 | g_free(pollfds); |
| 460 | g_free(nodes); |
| 461 | nalloc = 0; |
| 462 | } |
| 463 | |
| 464 | static void add_pollfd(AioHandler *node) |
| 465 | { |
| 466 | if (npfd == nalloc) { |
| 467 | if (nalloc == 0) { |
| 468 | pollfds_cleanup_notifier.notify = pollfds_cleanup; |
| 469 | qemu_thread_atexit_add(&pollfds_cleanup_notifier); |
| 470 | nalloc = 8; |
| 471 | } else { |
| 472 | g_assert(nalloc <= INT_MAX); |
| 473 | nalloc *= 2; |
| 474 | } |
| 475 | pollfds = g_renew(GPollFD, pollfds, nalloc); |
| 476 | nodes = g_renew(AioHandler *, nodes, nalloc); |
| 477 | } |
| 478 | nodes[npfd] = node; |
| 479 | pollfds[npfd] = (GPollFD) { |
| 480 | .fd = node->pfd.fd, |
| 481 | .events = node->pfd.events, |
| 482 | }; |
| 483 | npfd++; |
| 484 | } |
| 485 | |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 486 | static bool run_poll_handlers_once(AioContext *ctx) |
| 487 | { |
| 488 | bool progress = false; |
| 489 | AioHandler *node; |
| 490 | |
| 491 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 492 | if (!node->deleted && node->io_poll && |
| 493 | node->io_poll(node->opaque)) { |
| 494 | progress = true; |
| 495 | } |
| 496 | |
| 497 | /* Caller handles freeing deleted nodes. Don't do it here. */ |
| 498 | } |
| 499 | |
| 500 | return progress; |
| 501 | } |
| 502 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 503 | /* run_poll_handlers: |
| 504 | * @ctx: the AioContext |
| 505 | * @max_ns: maximum time to poll for, in nanoseconds |
| 506 | * |
| 507 | * Polls for a given time. |
| 508 | * |
| 509 | * Note that ctx->notify_me must be non-zero so this function can detect |
| 510 | * aio_notify(). |
| 511 | * |
| 512 | * Note that the caller must have incremented ctx->walking_handlers. |
| 513 | * |
| 514 | * Returns: true if progress was made, false otherwise |
| 515 | */ |
| 516 | static bool run_poll_handlers(AioContext *ctx, int64_t max_ns) |
| 517 | { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 518 | bool progress; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 519 | int64_t end_time; |
| 520 | |
| 521 | assert(ctx->notify_me); |
| 522 | assert(ctx->walking_handlers > 0); |
| 523 | assert(ctx->poll_disable_cnt == 0); |
| 524 | |
| 525 | trace_run_poll_handlers_begin(ctx, max_ns); |
| 526 | |
| 527 | end_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + max_ns; |
| 528 | |
| 529 | do { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 530 | progress = run_poll_handlers_once(ctx); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 531 | } while (!progress && qemu_clock_get_ns(QEMU_CLOCK_REALTIME) < end_time); |
| 532 | |
| 533 | trace_run_poll_handlers_end(ctx, progress); |
| 534 | |
| 535 | return progress; |
| 536 | } |
| 537 | |
| 538 | /* try_poll_mode: |
| 539 | * @ctx: the AioContext |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 540 | * @blocking: busy polling is only attempted when blocking is true |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 541 | * |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 542 | * ctx->notify_me must be non-zero so this function can detect aio_notify(). |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 543 | * |
| 544 | * Note that the caller must have incremented ctx->walking_handlers. |
| 545 | * |
| 546 | * Returns: true if progress was made, false otherwise |
| 547 | */ |
| 548 | static bool try_poll_mode(AioContext *ctx, bool blocking) |
| 549 | { |
| 550 | if (blocking && ctx->poll_max_ns && ctx->poll_disable_cnt == 0) { |
| 551 | /* See qemu_soonest_timeout() uint64_t hack */ |
| 552 | int64_t max_ns = MIN((uint64_t)aio_compute_timeout(ctx), |
| 553 | (uint64_t)ctx->poll_max_ns); |
| 554 | |
| 555 | if (max_ns) { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 556 | poll_set_started(ctx, true); |
| 557 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 558 | if (run_poll_handlers(ctx, max_ns)) { |
| 559 | return true; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame^] | 564 | poll_set_started(ctx, false); |
| 565 | |
| 566 | /* Even if we don't run busy polling, try polling once in case it can make |
| 567 | * progress and the caller will be able to avoid ppoll(2)/epoll_wait(2). |
| 568 | */ |
| 569 | return run_poll_handlers_once(ctx); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 572 | bool aio_poll(AioContext *ctx, bool blocking) |
| 573 | { |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 574 | AioHandler *node; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 575 | int i; |
| 576 | int ret = 0; |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 577 | bool progress; |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 578 | int64_t timeout; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 579 | |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 580 | aio_context_acquire(ctx); |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 581 | progress = false; |
| 582 | |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 583 | /* aio_notify can avoid the expensive event_notifier_set if |
| 584 | * everything (file descriptors, bottom halves, timers) will |
Paolo Bonzini | e4c7e2d | 2014-07-09 11:53:05 +0200 | [diff] [blame] | 585 | * be re-evaluated before the next blocking poll(). This is |
| 586 | * already true when aio_poll is called with blocking == false; |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 587 | * if blocking == true, it is only true after poll() returns, |
| 588 | * so disable the optimization now. |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 589 | */ |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 590 | if (blocking) { |
| 591 | atomic_add(&ctx->notify_me, 2); |
| 592 | } |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 593 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 594 | ctx->walking_handlers++; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 595 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 596 | if (try_poll_mode(ctx, blocking)) { |
| 597 | progress = true; |
| 598 | } else { |
| 599 | assert(npfd == 0); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 600 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 601 | /* fill pollfds */ |
Yaowei Bai | 6b94246 | 2016-09-14 07:03:39 -0400 | [diff] [blame] | 602 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 603 | if (!aio_epoll_enabled(ctx)) { |
| 604 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 605 | if (!node->deleted && node->pfd.events |
| 606 | && aio_node_check(ctx, node->is_external)) { |
| 607 | add_pollfd(node); |
| 608 | } |
Yaowei Bai | 6b94246 | 2016-09-14 07:03:39 -0400 | [diff] [blame] | 609 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 610 | } |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 611 | |
| 612 | timeout = blocking ? aio_compute_timeout(ctx) : 0; |
| 613 | |
| 614 | /* wait until next event */ |
| 615 | if (timeout) { |
| 616 | aio_context_release(ctx); |
| 617 | } |
| 618 | if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) { |
| 619 | AioHandler epoll_handler; |
| 620 | |
| 621 | epoll_handler.pfd.fd = ctx->epollfd; |
| 622 | epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR; |
| 623 | npfd = 0; |
| 624 | add_pollfd(&epoll_handler); |
| 625 | ret = aio_epoll(ctx, pollfds, npfd, timeout); |
| 626 | } else { |
| 627 | ret = qemu_poll_ns(pollfds, npfd, timeout); |
| 628 | } |
| 629 | if (timeout) { |
| 630 | aio_context_acquire(ctx); |
| 631 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 634 | if (blocking) { |
| 635 | atomic_sub(&ctx->notify_me, 2); |
| 636 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 637 | |
Paolo Bonzini | 05e514b | 2015-07-21 16:07:53 +0200 | [diff] [blame] | 638 | aio_notify_accept(ctx); |
Paolo Bonzini | 21a03d1 | 2015-07-21 16:07:52 +0200 | [diff] [blame] | 639 | |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 640 | /* if we have any readable fds, dispatch event */ |
| 641 | if (ret > 0) { |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 642 | for (i = 0; i < npfd; i++) { |
| 643 | nodes[i]->pfd.revents = pollfds[i].revents; |
Stefan Hajnoczi | 6b5f876 | 2013-02-20 11:28:32 +0100 | [diff] [blame] | 644 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 647 | npfd = 0; |
| 648 | ctx->walking_handlers--; |
| 649 | |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 650 | /* Run dispatch even if there were no readable fds to run timers */ |
Stefan Hajnoczi | 721671a | 2016-12-01 19:26:40 +0000 | [diff] [blame] | 651 | if (aio_dispatch(ctx, ret > 0)) { |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 652 | progress = true; |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 653 | } |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 654 | |
Paolo Bonzini | 4911017 | 2015-02-20 17:26:51 +0100 | [diff] [blame] | 655 | aio_context_release(ctx); |
| 656 | |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 657 | return progress; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 658 | } |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 659 | |
Cao jin | 7e00346 | 2016-07-15 18:28:44 +0800 | [diff] [blame] | 660 | void aio_context_setup(AioContext *ctx) |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 661 | { |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 662 | /* TODO remove this in final patch submission */ |
| 663 | if (getenv("QEMU_AIO_POLL_MAX_NS")) { |
| 664 | fprintf(stderr, "The QEMU_AIO_POLL_MAX_NS environment variable has " |
| 665 | "been replaced with -object iothread,poll-max-ns=NUM\n"); |
| 666 | exit(1); |
| 667 | } |
| 668 | |
Matthew Fortune | 147dfab | 2016-02-23 15:42:14 +0000 | [diff] [blame] | 669 | #ifdef CONFIG_EPOLL_CREATE1 |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 670 | assert(!ctx->epollfd); |
| 671 | ctx->epollfd = epoll_create1(EPOLL_CLOEXEC); |
| 672 | if (ctx->epollfd == -1) { |
Cao jin | 7e00346 | 2016-07-15 18:28:44 +0800 | [diff] [blame] | 673 | fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno)); |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 674 | ctx->epoll_available = false; |
| 675 | } else { |
| 676 | ctx->epoll_available = true; |
| 677 | } |
| 678 | #endif |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 679 | } |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 680 | |
| 681 | void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, Error **errp) |
| 682 | { |
| 683 | /* No thread synchronization here, it doesn't matter if an incorrect poll |
| 684 | * timeout is used once. |
| 685 | */ |
| 686 | ctx->poll_max_ns = max_ns; |
| 687 | |
| 688 | aio_notify(ctx); |
| 689 | } |