| 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 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include "qemu/osdep.h" |
| 17 | #include "block/block.h" |
| 18 | #include "block/thread-pool.h" |
| 19 | #include "qapi/error.h" |
| 20 | #include "qemu/main-loop.h" |
| 21 | #include "qemu/lockcnt.h" |
| 22 | #include "qemu/rcu.h" |
| 23 | #include "qemu/rcu_queue.h" |
| 24 | #include "qemu/sockets.h" |
| 25 | #include "qemu/cutils.h" |
| 26 | #include "system/iothread.h" |
| 27 | #include "trace.h" |
| 28 | #include "aio-posix.h" |
| 29 | |
| 30 | /* Stop userspace polling on a handler if it isn't active for some time */ |
| 31 | #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND) |
| 32 | |
| 33 | static void update_handler_poll_times(AioContext *ctx, int64_t block_ns, |
| 34 | int64_t dispatch_time); |
| 35 | static void adjust_polling_time(AioContext *ctx, int64_t block_ns); |
| 36 | |
| 37 | bool aio_poll_disabled(AioContext *ctx) |
| 38 | { |
| 39 | return qatomic_read(&ctx->poll_disable_cnt); |
| 40 | } |
| 41 | |
| 42 | void aio_add_ready_handler(AioHandlerList *ready_list, |
| 43 | AioHandler *node, |
| 44 | int revents) |
| 45 | { |
| 46 | QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */ |
| 47 | node->pfd.revents = revents; |
| 48 | QLIST_INSERT_HEAD(ready_list, node, node_ready); |
| 49 | } |
| 50 | |
| 51 | static void aio_add_poll_ready_handler(AioHandlerList *ready_list, |
| 52 | AioHandler *node) |
| 53 | { |
| 54 | QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */ |
| 55 | node->poll_ready = true; |
| 56 | QLIST_INSERT_HEAD(ready_list, node, node_ready); |
| 57 | } |
| 58 | |
| 59 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
| 60 | { |
| 61 | AioHandler *node; |
| 62 | |
| 63 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 64 | if (node->pfd.fd == fd) { |
| 65 | if (!QLIST_IS_INSERTED(node, node_deleted)) { |
| 66 | return node; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node) |
| 75 | { |
| 76 | node->pfd.revents = 0; |
| 77 | node->poll_ready = false; |
| 78 | |
| 79 | /* If the fd monitor has already marked it deleted, leave it alone */ |
| 80 | if (QLIST_IS_INSERTED(node, node_deleted)) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | /* If a read is in progress, just mark the node as deleted */ |
| 85 | if (qemu_lockcnt_count(&ctx->list_lock)) { |
| 86 | QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_deleted); |
| 87 | return false; |
| 88 | } |
| 89 | /* Otherwise, delete it for real. We can't just mark it as |
| 90 | * deleted because deleted nodes are only cleaned up while |
| 91 | * no one is walking the handlers list. |
| 92 | */ |
| 93 | QLIST_SAFE_REMOVE(node, node_poll); |
| 94 | QLIST_REMOVE(node, node); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void aio_set_fd_handler(AioContext *ctx, |
| 99 | int fd, |
| 100 | IOHandler *io_read, |
| 101 | IOHandler *io_write, |
| 102 | AioPollFn *io_poll, |
| 103 | IOHandler *io_poll_ready, |
| 104 | void *opaque) |
| 105 | { |
| 106 | AioHandler *node; |
| 107 | AioHandler *new_node = NULL; |
| 108 | bool is_new = false; |
| 109 | bool deleted = false; |
| 110 | int poll_disable_change; |
| 111 | |
| 112 | if (io_poll && !io_poll_ready) { |
| 113 | io_poll = NULL; /* polling only makes sense if there is a handler */ |
| 114 | } |
| 115 | |
| 116 | qemu_lockcnt_lock(&ctx->list_lock); |
| 117 | |
| 118 | node = find_aio_handler(ctx, fd); |
| 119 | |
| 120 | /* Are we deleting the fd handler? */ |
| 121 | if (!io_read && !io_write && !io_poll) { |
| 122 | if (node == NULL) { |
| 123 | qemu_lockcnt_unlock(&ctx->list_lock); |
| 124 | return; |
| 125 | } |
| 126 | /* Clean events in order to unregister fd from the ctx epoll. */ |
| 127 | node->pfd.events = 0; |
| 128 | |
| 129 | poll_disable_change = -!node->io_poll; |
| 130 | } else { |
| 131 | poll_disable_change = !io_poll - (node && !node->io_poll); |
| 132 | if (node == NULL) { |
| 133 | is_new = true; |
| 134 | } |
| 135 | /* Alloc and insert if it's not already there */ |
| 136 | new_node = g_new0(AioHandler, 1); |
| 137 | |
| 138 | /* Update handler with latest information */ |
| 139 | new_node->io_read = io_read; |
| 140 | new_node->io_write = io_write; |
| 141 | new_node->io_poll = io_poll; |
| 142 | new_node->io_poll_ready = io_poll_ready; |
| 143 | new_node->opaque = opaque; |
| 144 | |
| 145 | if (is_new) { |
| 146 | new_node->pfd.fd = fd; |
| 147 | } else { |
| 148 | new_node->pfd = node->pfd; |
| 149 | } |
| 150 | |
| 151 | new_node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); |
| 152 | new_node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); |
| 153 | |
| 154 | QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node); |
| 155 | } |
| 156 | |
| 157 | /* No need to order poll_disable_cnt writes against other updates; |
| 158 | * the counter is only used to avoid wasting time and latency on |
| 159 | * iterated polling when the system call will be ultimately necessary. |
| 160 | * Changing handlers is a rare event, and a little wasted polling until |
| 161 | * the aio_notify below is not an issue. |
| 162 | */ |
| 163 | qatomic_set(&ctx->poll_disable_cnt, |
| 164 | qatomic_read(&ctx->poll_disable_cnt) + poll_disable_change); |
| 165 | |
| 166 | ctx->fdmon_ops->update(ctx, node, new_node); |
| 167 | if (node) { |
| 168 | deleted = aio_remove_fd_handler(ctx, node); |
| 169 | } |
| 170 | qemu_lockcnt_unlock(&ctx->list_lock); |
| 171 | aio_notify(ctx); |
| 172 | |
| 173 | if (deleted) { |
| 174 | g_free(node); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static void aio_set_fd_poll(AioContext *ctx, int fd, |
| 179 | IOHandler *io_poll_begin, |
| 180 | IOHandler *io_poll_end) |
| 181 | { |
| 182 | AioHandler *node = find_aio_handler(ctx, fd); |
| 183 | |
| 184 | if (!node) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | node->io_poll_begin = io_poll_begin; |
| 189 | node->io_poll_end = io_poll_end; |
| 190 | } |
| 191 | |
| 192 | void aio_set_event_notifier(AioContext *ctx, |
| 193 | EventNotifier *notifier, |
| 194 | EventNotifierHandler *io_read, |
| 195 | AioPollFn *io_poll, |
| 196 | EventNotifierHandler *io_poll_ready) |
| 197 | { |
| 198 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
| 199 | (IOHandler *)io_read, NULL, io_poll, |
| 200 | (IOHandler *)io_poll_ready, notifier); |
| 201 | } |
| 202 | |
| 203 | void aio_set_event_notifier_poll(AioContext *ctx, |
| 204 | EventNotifier *notifier, |
| 205 | EventNotifierHandler *io_poll_begin, |
| 206 | EventNotifierHandler *io_poll_end) |
| 207 | { |
| 208 | aio_set_fd_poll(ctx, event_notifier_get_fd(notifier), |
| 209 | (IOHandler *)io_poll_begin, |
| 210 | (IOHandler *)io_poll_end); |
| 211 | } |
| 212 | |
| 213 | static bool poll_set_started(AioContext *ctx, AioHandlerList *ready_list, |
| 214 | bool started) |
| 215 | { |
| 216 | AioHandler *node; |
| 217 | bool progress = false; |
| 218 | |
| 219 | if (started == ctx->poll_started) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | ctx->poll_started = started; |
| 224 | |
| 225 | qemu_lockcnt_inc(&ctx->list_lock); |
| 226 | QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) { |
| 227 | IOHandler *fn; |
| 228 | |
| 229 | if (QLIST_IS_INSERTED(node, node_deleted)) { |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | if (started) { |
| 234 | fn = node->io_poll_begin; |
| 235 | } else { |
| 236 | fn = node->io_poll_end; |
| 237 | } |
| 238 | |
| 239 | if (fn) { |
| 240 | fn(node->opaque); |
| 241 | } |
| 242 | |
| 243 | /* Poll one last time in case ->io_poll_end() raced with the event */ |
| 244 | if (!started && node->io_poll(node->opaque)) { |
| 245 | aio_add_poll_ready_handler(ready_list, node); |
| 246 | progress = true; |
| 247 | } |
| 248 | } |
| 249 | qemu_lockcnt_dec(&ctx->list_lock); |
| 250 | |
| 251 | return progress; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | bool aio_prepare(AioContext *ctx) |
| 256 | { |
| 257 | AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list); |
| 258 | |
| 259 | /* Poll mode cannot be used with glib's event loop, disable it. */ |
| 260 | poll_set_started(ctx, &ready_list, false); |
| 261 | /* TODO what to do with this list? */ |
| 262 | |
| 263 | ctx->fdmon_ops->gsource_prepare(ctx); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | bool aio_pending(AioContext *ctx) |
| 268 | { |
| 269 | return ctx->fdmon_ops->gsource_check(ctx); |
| 270 | } |
| 271 | |
| 272 | static void aio_free_deleted_handlers(AioContext *ctx) |
| 273 | { |
| 274 | AioHandler *node; |
| 275 | |
| 276 | if (QLIST_EMPTY_RCU(&ctx->deleted_aio_handlers)) { |
| 277 | return; |
| 278 | } |
| 279 | if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { |
| 280 | return; /* we are nested, let the parent do the freeing */ |
| 281 | } |
| 282 | |
| 283 | while ((node = QLIST_FIRST_RCU(&ctx->deleted_aio_handlers))) { |
| 284 | QLIST_REMOVE(node, node); |
| 285 | QLIST_REMOVE(node, node_deleted); |
| 286 | QLIST_SAFE_REMOVE(node, node_poll); |
| 287 | g_free(node); |
| 288 | } |
| 289 | |
| 290 | qemu_lockcnt_inc_and_unlock(&ctx->list_lock); |
| 291 | } |
| 292 | |
| 293 | static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node) |
| 294 | { |
| 295 | bool progress = false; |
| 296 | bool poll_ready; |
| 297 | int revents; |
| 298 | |
| 299 | revents = node->pfd.revents & node->pfd.events; |
| 300 | node->pfd.revents = 0; |
| 301 | |
| 302 | poll_ready = node->poll_ready; |
| 303 | node->poll_ready = false; |
| 304 | |
| 305 | /* |
| 306 | * Start polling AioHandlers when they become ready because activity is |
| 307 | * likely to continue. Note that starvation is theoretically possible when |
| 308 | * fdmon_supports_polling(), but only until the fd fires for the first |
| 309 | * time. |
| 310 | */ |
| 311 | if (ctx->poll_max_ns && !QLIST_IS_INSERTED(node, node_deleted) && |
| 312 | !QLIST_IS_INSERTED(node, node_poll) && node->io_poll) { |
| 313 | trace_poll_add(ctx, node, node->pfd.fd, revents); |
| 314 | if (ctx->poll_started && node->io_poll_begin) { |
| 315 | node->io_poll_begin(node->opaque); |
| 316 | } |
| 317 | QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll); |
| 318 | } |
| 319 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
| 320 | poll_ready && revents == 0 && node->io_poll_ready) { |
| 321 | /* |
| 322 | * Remove temporarily to avoid infinite loops when ->io_poll_ready() |
| 323 | * calls aio_poll() before clearing the condition that made the poll |
| 324 | * handler become ready. |
| 325 | */ |
| 326 | QLIST_SAFE_REMOVE(node, node_poll); |
| 327 | |
| 328 | node->io_poll_ready(node->opaque); |
| 329 | |
| 330 | if (!QLIST_IS_INSERTED(node, node_poll)) { |
| 331 | QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Return early since revents was zero. aio_notify() does not count as |
| 336 | * progress. |
| 337 | */ |
| 338 | return node->opaque != &ctx->notifier; |
| 339 | } |
| 340 | |
| 341 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
| 342 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
| 343 | node->io_read) { |
| 344 | node->io_read(node->opaque); |
| 345 | |
| 346 | /* aio_notify() does not count as progress */ |
| 347 | if (node->opaque != &ctx->notifier) { |
| 348 | progress = true; |
| 349 | } |
| 350 | } |
| 351 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
| 352 | (revents & (G_IO_OUT | G_IO_ERR)) && |
| 353 | node->io_write) { |
| 354 | node->io_write(node->opaque); |
| 355 | progress = true; |
| 356 | } |
| 357 | |
| 358 | return progress; |
| 359 | } |
| 360 | |
| 361 | static bool aio_dispatch_ready_handlers(AioContext *ctx, |
| 362 | AioHandlerList *ready_list, |
| 363 | int64_t dispatch_time) |
| 364 | { |
| 365 | bool progress = false; |
| 366 | AioHandler *node; |
| 367 | |
| 368 | while ((node = QLIST_FIRST(ready_list))) { |
| 369 | QLIST_REMOVE(node, node_ready); |
| 370 | progress = aio_dispatch_handler(ctx, node) || progress; |
| 371 | |
| 372 | /* |
| 373 | * Update last_dispatch_timestamp to mark this as an active |
| 374 | * handler for polling time adjustment and prevent idle removal. |
| 375 | */ |
| 376 | if (ctx->poll_max_ns && QLIST_IS_INSERTED(node, node_poll)) { |
| 377 | node->last_dispatch_timestamp = dispatch_time; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return progress; |
| 382 | } |
| 383 | |
| 384 | void aio_dispatch(AioContext *ctx) |
| 385 | { |
| 386 | AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list); |
| 387 | |
| 388 | qemu_lockcnt_inc(&ctx->list_lock); |
| 389 | |
| 390 | aio_bh_poll(ctx); |
| 391 | |
| 392 | ctx->fdmon_ops->gsource_dispatch(ctx, &ready_list); |
| 393 | |
| 394 | if (ctx->fdmon_ops->dispatch) { |
| 395 | ctx->fdmon_ops->dispatch(ctx); |
| 396 | } |
| 397 | |
| 398 | /* Set now to 0 as polling is disabled in the glib event loop */ |
| 399 | aio_dispatch_ready_handlers(ctx, &ready_list, 0); |
| 400 | |
| 401 | aio_free_deleted_handlers(ctx); |
| 402 | qemu_lockcnt_dec(&ctx->list_lock); |
| 403 | |
| 404 | timerlistgroup_run_timers(&ctx->tlg); |
| 405 | } |
| 406 | |
| 407 | static bool run_poll_handlers_once(AioContext *ctx, |
| 408 | AioHandlerList *ready_list, |
| 409 | int64_t now, |
| 410 | int64_t *timeout) |
| 411 | { |
| 412 | bool progress = false; |
| 413 | AioHandler *node; |
| 414 | AioHandler *tmp; |
| 415 | |
| 416 | QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) { |
| 417 | if (node->io_poll(node->opaque)) { |
| 418 | aio_add_poll_ready_handler(ready_list, node); |
| 419 | /* |
| 420 | * Polling was successful, exit try_poll_mode immediately |
| 421 | * to adjust the next polling time. |
| 422 | */ |
| 423 | *timeout = 0; |
| 424 | if (node->opaque != &ctx->notifier) { |
| 425 | progress = true; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /* Caller handles freeing deleted nodes. Don't do it here. */ |
| 430 | } |
| 431 | |
| 432 | return progress; |
| 433 | } |
| 434 | |
| 435 | static bool fdmon_supports_polling(AioContext *ctx) |
| 436 | { |
| 437 | return ctx->fdmon_ops->need_wait != aio_poll_disabled; |
| 438 | } |
| 439 | |
| 440 | static bool remove_idle_poll_handlers(AioContext *ctx, |
| 441 | AioHandlerList *ready_list, |
| 442 | int64_t now) |
| 443 | { |
| 444 | AioHandler *node; |
| 445 | AioHandler *tmp; |
| 446 | bool progress = false; |
| 447 | |
| 448 | /* |
| 449 | * File descriptor monitoring implementations without userspace polling |
| 450 | * support suffer from starvation when a subset of handlers is polled |
| 451 | * because fds will not be processed in a timely fashion. Don't remove |
| 452 | * idle poll handlers. |
| 453 | */ |
| 454 | if (!fdmon_supports_polling(ctx)) { |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) { |
| 459 | if (node->poll_ready == false && |
| 460 | now >= node->last_dispatch_timestamp + POLL_IDLE_INTERVAL_NS) { |
| 461 | trace_poll_remove(ctx, node, node->pfd.fd); |
| 462 | node->last_dispatch_timestamp = 0LL; |
| 463 | QLIST_SAFE_REMOVE(node, node_poll); |
| 464 | if (ctx->poll_started && node->io_poll_end) { |
| 465 | node->io_poll_end(node->opaque); |
| 466 | |
| 467 | /* |
| 468 | * Final poll in case ->io_poll_end() races with an event. |
| 469 | * Nevermind about re-adding the handler in the rare case where |
| 470 | * this causes progress. |
| 471 | */ |
| 472 | if (node->io_poll(node->opaque)) { |
| 473 | aio_add_poll_ready_handler(ready_list, node); |
| 474 | progress = true; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return progress; |
| 481 | } |
| 482 | |
| 483 | /* run_poll_handlers: |
| 484 | * @ctx: the AioContext |
| 485 | * @ready_list: the list to place ready handlers on |
| 486 | * @max_ns: maximum time to poll for, in nanoseconds |
| 487 | * |
| 488 | * Polls for a given time. |
| 489 | * |
| 490 | * Note that the caller must have incremented ctx->list_lock. |
| 491 | * |
| 492 | * Returns: true if progress was made, false otherwise |
| 493 | */ |
| 494 | static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list, |
| 495 | int64_t max_ns, int64_t *timeout) |
| 496 | { |
| 497 | bool progress; |
| 498 | int64_t start_time, elapsed_time; |
| 499 | |
| 500 | assert(qemu_lockcnt_count(&ctx->list_lock) > 0); |
| 501 | |
| 502 | trace_run_poll_handlers_begin(ctx, max_ns, *timeout); |
| 503 | |
| 504 | /* |
| 505 | * Optimization: ->io_poll() handlers often contain RCU read critical |
| 506 | * sections and we therefore see many rcu_read_lock() -> rcu_read_unlock() |
| 507 | * -> rcu_read_lock() -> ... sequences with expensive memory |
| 508 | * synchronization primitives. Make the entire polling loop an RCU |
| 509 | * critical section because nested rcu_read_lock()/rcu_read_unlock() calls |
| 510 | * are cheap. |
| 511 | */ |
| 512 | RCU_READ_LOCK_GUARD(); |
| 513 | |
| 514 | start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 515 | do { |
| 516 | progress = run_poll_handlers_once(ctx, ready_list, |
| 517 | start_time, timeout); |
| 518 | elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time; |
| 519 | max_ns = qemu_soonest_timeout(*timeout, max_ns); |
| 520 | assert(!(max_ns && progress)); |
| 521 | |
| 522 | if (ctx->fdmon_ops->need_wait(ctx)) { |
| 523 | if (fdmon_supports_polling(ctx)) { |
| 524 | *timeout = 0; /* stay in polling mode */ |
| 525 | } |
| 526 | break; |
| 527 | } |
| 528 | } while (elapsed_time < max_ns); |
| 529 | |
| 530 | if (remove_idle_poll_handlers(ctx, ready_list, |
| 531 | start_time + elapsed_time)) { |
| 532 | *timeout = 0; |
| 533 | progress = true; |
| 534 | } |
| 535 | |
| 536 | /* If time has passed with no successful polling, adjust *timeout to |
| 537 | * keep the same ending time. |
| 538 | */ |
| 539 | if (*timeout != -1) { |
| 540 | *timeout -= MIN(*timeout, elapsed_time); |
| 541 | } |
| 542 | |
| 543 | trace_run_poll_handlers_end(ctx, progress, *timeout); |
| 544 | return progress; |
| 545 | } |
| 546 | |
| 547 | /* try_poll_mode: |
| 548 | * @ctx: the AioContext |
| 549 | * @ready_list: list to add handlers that need to be run |
| 550 | * @timeout: timeout for blocking wait, computed by the caller and updated if |
| 551 | * polling succeeds. |
| 552 | * |
| 553 | * Note that the caller must have incremented ctx->list_lock. |
| 554 | * |
| 555 | * Returns: true if progress was made, false otherwise |
| 556 | */ |
| 557 | static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, |
| 558 | int64_t *timeout) |
| 559 | { |
| 560 | int64_t max_ns; |
| 561 | |
| 562 | if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) { |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns); |
| 567 | |
| 568 | if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) { |
| 569 | /* |
| 570 | * Enable poll mode. It pairs with the poll_set_started() in |
| 571 | * aio_poll() which disables poll mode. |
| 572 | */ |
| 573 | poll_set_started(ctx, ready_list, true); |
| 574 | |
| 575 | if (run_poll_handlers(ctx, ready_list, max_ns, timeout)) { |
| 576 | return true; |
| 577 | } |
| 578 | } |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | static void adjust_polling_time(AioContext *ctx, int64_t block_ns) |
| 583 | { |
| 584 | if (block_ns > ctx->poll_ns) { |
| 585 | /* There is room to grow, poll longer */ |
| 586 | int64_t old = ctx->poll_ns; |
| 587 | int64_t grow = ctx->poll_grow; |
| 588 | |
| 589 | if (block_ns > ctx->poll_ns * grow) { |
| 590 | ctx->poll_ns = block_ns; |
| 591 | } else { |
| 592 | ctx->poll_ns *= grow; |
| 593 | } |
| 594 | |
| 595 | if (ctx->poll_ns > ctx->poll_max_ns) { |
| 596 | ctx->poll_ns = ctx->poll_max_ns; |
| 597 | } |
| 598 | |
| 599 | trace_poll_grow(ctx, old, ctx->poll_ns); |
| 600 | } else if (block_ns < (ctx->poll_ns / ctx->poll_shrink)) { |
| 601 | int64_t old = ctx->poll_ns; |
| 602 | ctx->poll_ns /= ctx->poll_shrink; |
| 603 | |
| 604 | trace_poll_shrink(ctx, old, ctx->poll_ns); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static void update_handler_poll_times(AioContext *ctx, int64_t block_ns, |
| 609 | int64_t dispatch_time) |
| 610 | { |
| 611 | AioHandler *node; |
| 612 | int64_t max_poll_ns = -1; |
| 613 | |
| 614 | QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) { |
| 615 | if (node->last_dispatch_timestamp == dispatch_time) { |
| 616 | /* |
| 617 | * Active handler: had an event in this aio_poll() call. |
| 618 | * Update poll.ns using a weighted average of the current |
| 619 | * block_ns and previous poll.ns to smooth adjustments. |
| 620 | */ |
| 621 | node->poll.ns = node->poll.ns |
| 622 | ? (node->poll.ns - (node->poll.ns >> ctx->poll_weight)) |
| 623 | + (block_ns >> ctx->poll_weight) : block_ns; |
| 624 | |
| 625 | if (node->poll.ns > ctx->poll_max_ns) { |
| 626 | node->poll.ns = 0; |
| 627 | } |
| 628 | /* |
| 629 | * Track the maximum poll.ns among active handlers to |
| 630 | * calculate the next polling time. |
| 631 | */ |
| 632 | max_poll_ns = MAX(max_poll_ns, node->poll.ns); |
| 633 | } else { |
| 634 | /* |
| 635 | * Inactive handler: no event in this aio_poll() call but |
| 636 | * was active before. Increase poll.ns by block_ns. If it |
| 637 | * exceeds poll_max_ns, reset to 0 until next event. |
| 638 | */ |
| 639 | if (node->poll.ns != 0) { |
| 640 | node->poll.ns += block_ns; |
| 641 | if (node->poll.ns > ctx->poll_max_ns) { |
| 642 | node->poll.ns = 0; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | if (max_poll_ns >= 0) { |
| 648 | adjust_polling_time(ctx, max_poll_ns); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | bool aio_poll(AioContext *ctx, bool blocking) |
| 653 | { |
| 654 | AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list); |
| 655 | bool progress = false; |
| 656 | bool use_notify_me; |
| 657 | int64_t timeout; |
| 658 | int64_t start = 0; |
| 659 | int64_t block_ns = 0; |
| 660 | int64_t dispatch_ns = 0; |
| 661 | |
| 662 | /* |
| 663 | * There cannot be two concurrent aio_poll calls for the same AioContext (or |
| 664 | * an aio_poll concurrent with a GSource prepare/check/dispatch callback). |
| 665 | * We rely on this below to avoid slow locked accesses to ctx->notify_me. |
| 666 | * |
| 667 | * aio_poll() may only be called in the AioContext's thread. iohandler_ctx |
| 668 | * is special in that it runs in the main thread, but that thread's context |
| 669 | * is qemu_aio_context. |
| 670 | */ |
| 671 | assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ? |
| 672 | qemu_get_aio_context() : ctx)); |
| 673 | |
| 674 | qemu_lockcnt_inc(&ctx->list_lock); |
| 675 | |
| 676 | if (ctx->poll_max_ns) { |
| 677 | start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 678 | } |
| 679 | |
| 680 | timeout = blocking ? aio_compute_timeout(ctx) : 0; |
| 681 | if (ctx->poll_max_ns != 0) { |
| 682 | progress = try_poll_mode(ctx, &ready_list, &timeout); |
| 683 | } |
| 684 | assert(!(timeout && progress)); |
| 685 | |
| 686 | /* |
| 687 | * aio_notify can avoid the expensive event_notifier_set if |
| 688 | * everything (file descriptors, bottom halves, timers) will |
| 689 | * be re-evaluated before the next blocking poll(). This is |
| 690 | * already true when aio_poll is called with blocking == false; |
| 691 | * if blocking == true, it is only true after poll() returns, |
| 692 | * so disable the optimization now. |
| 693 | */ |
| 694 | use_notify_me = timeout != 0; |
| 695 | if (use_notify_me) { |
| 696 | qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2); |
| 697 | /* |
| 698 | * Write ctx->notify_me before reading ctx->notified. Pairs with |
| 699 | * smp_mb in aio_notify(). |
| 700 | */ |
| 701 | smp_mb(); |
| 702 | |
| 703 | /* Don't block if aio_notify() was called */ |
| 704 | if (qatomic_read(&ctx->notified)) { |
| 705 | timeout = 0; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* If polling is allowed, non-blocking aio_poll does not need the |
| 710 | * system call---a single round of run_poll_handlers_once suffices. |
| 711 | */ |
| 712 | if (timeout || ctx->fdmon_ops->need_wait(ctx)) { |
| 713 | /* |
| 714 | * Disable poll mode. poll mode should be disabled before the call |
| 715 | * of ctx->fdmon_ops->wait() so that guest's notification can wake |
| 716 | * up IO threads when some work becomes pending. It is essential to |
| 717 | * avoid hangs or unnecessary latency. |
| 718 | */ |
| 719 | if (timeout && poll_set_started(ctx, &ready_list, false)) { |
| 720 | timeout = 0; |
| 721 | progress = true; |
| 722 | } |
| 723 | |
| 724 | ctx->fdmon_ops->wait(ctx, &ready_list, timeout); |
| 725 | } |
| 726 | |
| 727 | if (use_notify_me) { |
| 728 | /* Finish the poll before clearing the flag. */ |
| 729 | qatomic_store_release(&ctx->notify_me, |
| 730 | qatomic_read(&ctx->notify_me) - 2); |
| 731 | } |
| 732 | |
| 733 | aio_notify_accept(ctx); |
| 734 | |
| 735 | /* Calculate blocked time for adaptive polling */ |
| 736 | if (ctx->poll_max_ns) { |
| 737 | dispatch_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 738 | block_ns = dispatch_ns - start; |
| 739 | } |
| 740 | |
| 741 | if (ctx->fdmon_ops->dispatch) { |
| 742 | progress |= ctx->fdmon_ops->dispatch(ctx); |
| 743 | } |
| 744 | |
| 745 | progress |= aio_bh_poll(ctx); |
| 746 | progress |= aio_dispatch_ready_handlers(ctx, &ready_list, dispatch_ns); |
| 747 | |
| 748 | aio_free_deleted_handlers(ctx); |
| 749 | |
| 750 | if (ctx->poll_max_ns) { |
| 751 | update_handler_poll_times(ctx, block_ns, dispatch_ns); |
| 752 | } |
| 753 | |
| 754 | qemu_lockcnt_dec(&ctx->list_lock); |
| 755 | |
| 756 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
| 757 | |
| 758 | return progress; |
| 759 | } |
| 760 | |
| 761 | bool aio_context_setup(AioContext *ctx, Error **errp) |
| 762 | { |
| 763 | ctx->fdmon_ops = &fdmon_poll_ops; |
| 764 | ctx->epollfd = -1; |
| 765 | ctx->epollfd_tag = NULL; |
| 766 | |
| 767 | #ifdef CONFIG_LINUX_IO_URING |
| 768 | { |
| 769 | static bool need_io_uring; |
| 770 | Error *local_err = NULL; /* ERRP_GUARD() doesn't handle error_abort */ |
| 771 | |
| 772 | /* io_uring takes precedence because it provides aio_add_sqe() support */ |
| 773 | if (fdmon_io_uring_setup(ctx, &local_err)) { |
| 774 | /* |
| 775 | * If one AioContext gets io_uring, then all AioContexts need io_uring |
| 776 | * so that aio_add_sqe() support is available across all threads. |
| 777 | */ |
| 778 | need_io_uring = true; |
| 779 | return true; |
| 780 | } |
| 781 | if (need_io_uring) { |
| 782 | error_propagate(errp, local_err); |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | /* Silently fall back on systems where io_uring is unavailable */ |
| 787 | error_free(local_err); |
| 788 | } |
| 789 | #endif /* CONFIG_LINUX_IO_URING */ |
| 790 | |
| 791 | fdmon_epoll_setup(ctx); |
| 792 | return true; |
| 793 | } |
| 794 | |
| 795 | void aio_context_destroy(AioContext *ctx) |
| 796 | { |
| 797 | #ifdef CONFIG_LINUX_IO_URING |
| 798 | fdmon_io_uring_destroy(ctx); |
| 799 | #endif |
| 800 | |
| 801 | qemu_lockcnt_lock(&ctx->list_lock); |
| 802 | fdmon_epoll_disable(ctx); |
| 803 | qemu_lockcnt_unlock(&ctx->list_lock); |
| 804 | |
| 805 | aio_free_deleted_handlers(ctx); |
| 806 | } |
| 807 | |
| 808 | void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, |
| 809 | int64_t grow, int64_t shrink, |
| 810 | int64_t weight, Error **errp) |
| 811 | { |
| 812 | AioHandler *node; |
| 813 | |
| 814 | qemu_lockcnt_inc(&ctx->list_lock); |
| 815 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
| 816 | node->poll.ns = 0; |
| 817 | } |
| 818 | qemu_lockcnt_dec(&ctx->list_lock); |
| 819 | |
| 820 | /* No thread synchronization here, it doesn't matter if an incorrect value |
| 821 | * is used once. |
| 822 | */ |
| 823 | ctx->poll_max_ns = max_ns; |
| 824 | ctx->poll_grow = (grow ? grow : IOTHREAD_POLL_GROW_DEFAULT); |
| 825 | ctx->poll_shrink = (shrink ? shrink : IOTHREAD_POLL_SHRINK_DEFAULT); |
| 826 | ctx->poll_weight = (weight ? weight : IOTHREAD_POLL_WEIGHT_DEFAULT); |
| 827 | ctx->poll_ns = 0; |
| 828 | |
| 829 | aio_notify(ctx); |
| 830 | } |
| 831 | |
| 832 | void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch) |
| 833 | { |
| 834 | /* |
| 835 | * No thread synchronization here, it doesn't matter if an incorrect value |
| 836 | * is used once. |
| 837 | */ |
| 838 | ctx->aio_max_batch = max_batch; |
| 839 | |
| 840 | aio_notify(ctx); |
| 841 | } |
| 842 | |
| 843 | #ifdef CONFIG_LINUX_IO_URING |
| 844 | void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), |
| 845 | void *opaque, CqeHandler *cqe_handler) |
| 846 | { |
| 847 | AioContext *ctx = qemu_get_current_aio_context(); |
| 848 | ctx->fdmon_ops->add_sqe(ctx, prep_sqe, opaque, cqe_handler); |
| 849 | |
| 850 | /* |
| 851 | * Wake the main loop if it is sleeping in ppoll(). When a vCPU thread |
| 852 | * queues SQEs, the actual io_uring_submit() only happens in |
| 853 | * gsource_prepare() in the main loop thread. Without this notify, the |
| 854 | * main loop thread's ppoll() can sleep up to 499ms before submitting. |
| 855 | */ |
| 856 | aio_notify(ctx); |
| 857 | } |
| 858 | #endif /* CONFIG_LINUX_IO_URING */ |