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" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 17 | #include "block/block.h" |
Nicolas Saenz Julienne | 71ad471 | 2022-04-25 09:57:23 +0200 | [diff] [blame] | 18 | #include "block/thread-pool.h" |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 19 | #include "qemu/main-loop.h" |
Stefan Hajnoczi | f25c0b5 | 2020-02-18 18:27:08 +0000 | [diff] [blame] | 20 | #include "qemu/rcu.h" |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 21 | #include "qemu/rcu_queue.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 22 | #include "qemu/sockets.h" |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 23 | #include "qemu/cutils.h" |
Paolo Bonzini | c2b38b2 | 2017-02-13 14:52:18 +0100 | [diff] [blame] | 24 | #include "trace.h" |
Stefan Hajnoczi | 1f050a4 | 2020-03-05 17:08:02 +0000 | [diff] [blame] | 25 | #include "aio-posix.h" |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 26 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 27 | /* Stop userspace polling on a handler if it isn't active for some time */ |
| 28 | #define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND) |
| 29 | |
Stefan Hajnoczi | aa38e19 | 2020-03-05 17:08:05 +0000 | [diff] [blame] | 30 | bool aio_poll_disabled(AioContext *ctx) |
| 31 | { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 32 | return qatomic_read(&ctx->poll_disable_cnt); |
Stefan Hajnoczi | aa38e19 | 2020-03-05 17:08:05 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Stefan Hajnoczi | 1f050a4 | 2020-03-05 17:08:02 +0000 | [diff] [blame] | 35 | void aio_add_ready_handler(AioHandlerList *ready_list, |
| 36 | AioHandler *node, |
| 37 | int revents) |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 38 | { |
| 39 | QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */ |
| 40 | node->pfd.revents = revents; |
| 41 | QLIST_INSERT_HEAD(ready_list, node, node_ready); |
| 42 | } |
| 43 | |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 44 | static void aio_add_poll_ready_handler(AioHandlerList *ready_list, |
| 45 | AioHandler *node) |
| 46 | { |
| 47 | QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */ |
| 48 | node->poll_ready = true; |
| 49 | QLIST_INSERT_HEAD(ready_list, node, node_ready); |
| 50 | } |
| 51 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 52 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 53 | { |
| 54 | AioHandler *node; |
| 55 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 56 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 57 | if (node->pfd.fd == fd) { |
| 58 | if (!QLIST_IS_INSERTED(node, node_deleted)) { |
Alexander Graf | 79d5ca5 | 2009-05-06 02:58:48 +0200 | [diff] [blame] | 59 | return node; |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return NULL; |
| 65 | } |
| 66 | |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 67 | static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node) |
| 68 | { |
| 69 | /* If the GSource is in the process of being destroyed then |
| 70 | * g_source_remove_poll() causes an assertion failure. Skip |
| 71 | * removal in that case, because glib cleans up its state during |
| 72 | * destruction anyway. |
| 73 | */ |
| 74 | if (!g_source_is_destroyed(&ctx->source)) { |
| 75 | g_source_remove_poll(&ctx->source, &node->pfd); |
| 76 | } |
| 77 | |
Stefan Hajnoczi | 73fd282 | 2020-03-05 17:08:04 +0000 | [diff] [blame] | 78 | node->pfd.revents = 0; |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 79 | node->poll_ready = false; |
Stefan Hajnoczi | 73fd282 | 2020-03-05 17:08:04 +0000 | [diff] [blame] | 80 | |
| 81 | /* If the fd monitor has already marked it deleted, leave it alone */ |
| 82 | if (QLIST_IS_INSERTED(node, node_deleted)) { |
| 83 | return false; |
| 84 | } |
| 85 | |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 86 | /* If a read is in progress, just mark the node as deleted */ |
| 87 | if (qemu_lockcnt_count(&ctx->list_lock)) { |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 88 | QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_deleted); |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | /* Otherwise, delete it for real. We can't just mark it as |
| 92 | * deleted because deleted nodes are only cleaned up while |
| 93 | * no one is walking the handlers list. |
| 94 | */ |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 95 | QLIST_SAFE_REMOVE(node, node_poll); |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 96 | QLIST_REMOVE(node, node); |
| 97 | return true; |
| 98 | } |
| 99 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 100 | void aio_set_fd_handler(AioContext *ctx, |
| 101 | int fd, |
| 102 | IOHandler *io_read, |
| 103 | IOHandler *io_write, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 104 | AioPollFn *io_poll, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 105 | IOHandler *io_poll_ready, |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 106 | void *opaque) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 107 | { |
| 108 | AioHandler *node; |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 109 | AioHandler *new_node = NULL; |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 110 | bool is_new = false; |
Fam Zheng | 0ed39f3d | 2015-11-16 14:32:14 +0800 | [diff] [blame] | 111 | bool deleted = false; |
Paolo Bonzini | d7be5dd | 2018-09-12 19:10:38 +0200 | [diff] [blame] | 112 | int poll_disable_change; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 113 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 114 | if (io_poll && !io_poll_ready) { |
| 115 | io_poll = NULL; /* polling only makes sense if there is a handler */ |
| 116 | } |
| 117 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 118 | qemu_lockcnt_lock(&ctx->list_lock); |
| 119 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 120 | node = find_aio_handler(ctx, fd); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 121 | |
| 122 | /* Are we deleting the fd handler? */ |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 123 | if (!io_read && !io_write && !io_poll) { |
Paolo Bonzini | 36173ec | 2016-11-08 14:55:23 +0100 | [diff] [blame] | 124 | if (node == NULL) { |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 125 | qemu_lockcnt_unlock(&ctx->list_lock); |
Paolo Bonzini | 36173ec | 2016-11-08 14:55:23 +0100 | [diff] [blame] | 126 | return; |
| 127 | } |
Remy Noel | 8821b34 | 2018-12-20 16:20:29 +0100 | [diff] [blame] | 128 | /* Clean events in order to unregister fd from the ctx epoll. */ |
| 129 | node->pfd.events = 0; |
| 130 | |
Paolo Bonzini | d7be5dd | 2018-09-12 19:10:38 +0200 | [diff] [blame] | 131 | poll_disable_change = -!node->io_poll; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 132 | } else { |
Paolo Bonzini | d7be5dd | 2018-09-12 19:10:38 +0200 | [diff] [blame] | 133 | poll_disable_change = !io_poll - (node && !node->io_poll); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 134 | if (node == NULL) { |
Fam Zheng | fbe3fc5 | 2015-10-30 12:06:29 +0800 | [diff] [blame] | 135 | is_new = true; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 136 | } |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 137 | /* Alloc and insert if it's not already there */ |
| 138 | new_node = g_new0(AioHandler, 1); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 139 | |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 140 | /* Update handler with latest information */ |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 141 | new_node->io_read = io_read; |
| 142 | new_node->io_write = io_write; |
| 143 | new_node->io_poll = io_poll; |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 144 | new_node->io_poll_ready = io_poll_ready; |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 145 | new_node->opaque = opaque; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 146 | |
Remy Noel | fef1660 | 2018-12-20 16:20:30 +0100 | [diff] [blame] | 147 | if (is_new) { |
| 148 | new_node->pfd.fd = fd; |
| 149 | } else { |
| 150 | new_node->pfd = node->pfd; |
| 151 | } |
| 152 | g_source_add_poll(&ctx->source, &new_node->pfd); |
| 153 | |
| 154 | new_node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); |
| 155 | new_node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); |
| 156 | |
| 157 | QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node); |
| 158 | } |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 159 | |
Paolo Bonzini | d7be5dd | 2018-09-12 19:10:38 +0200 | [diff] [blame] | 160 | /* No need to order poll_disable_cnt writes against other updates; |
| 161 | * the counter is only used to avoid wasting time and latency on |
| 162 | * iterated polling when the system call will be ultimately necessary. |
| 163 | * Changing handlers is a rare event, and a little wasted polling until |
| 164 | * the aio_notify below is not an issue. |
| 165 | */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 166 | qatomic_set(&ctx->poll_disable_cnt, |
| 167 | qatomic_read(&ctx->poll_disable_cnt) + poll_disable_change); |
Paolo Bonzini | d7be5dd | 2018-09-12 19:10:38 +0200 | [diff] [blame] | 168 | |
Stefan Hajnoczi | b321051 | 2020-03-05 17:08:03 +0000 | [diff] [blame] | 169 | ctx->fdmon_ops->update(ctx, node, new_node); |
Stefan Hajnoczi | 73fd282 | 2020-03-05 17:08:04 +0000 | [diff] [blame] | 170 | if (node) { |
| 171 | deleted = aio_remove_fd_handler(ctx, node); |
| 172 | } |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 173 | qemu_lockcnt_unlock(&ctx->list_lock); |
Paolo Bonzini | 7ed2b24 | 2012-09-25 10:22:39 +0200 | [diff] [blame] | 174 | aio_notify(ctx); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 175 | |
Fam Zheng | 0ed39f3d | 2015-11-16 14:32:14 +0800 | [diff] [blame] | 176 | if (deleted) { |
| 177 | g_free(node); |
| 178 | } |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Marc-André Lureau | 6eeef44 | 2023-02-21 16:47:53 +0400 | [diff] [blame] | 181 | static void aio_set_fd_poll(AioContext *ctx, int fd, |
| 182 | IOHandler *io_poll_begin, |
| 183 | IOHandler *io_poll_end) |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 184 | { |
| 185 | AioHandler *node = find_aio_handler(ctx, fd); |
| 186 | |
| 187 | if (!node) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | node->io_poll_begin = io_poll_begin; |
| 192 | node->io_poll_end = io_poll_end; |
| 193 | } |
| 194 | |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 195 | void aio_set_event_notifier(AioContext *ctx, |
| 196 | EventNotifier *notifier, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 197 | EventNotifierHandler *io_read, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 198 | AioPollFn *io_poll, |
| 199 | EventNotifierHandler *io_poll_ready) |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 200 | { |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 201 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 202 | (IOHandler *)io_read, NULL, io_poll, |
| 203 | (IOHandler *)io_poll_ready, notifier); |
Paolo Bonzini | 9958c35 | 2012-06-09 03:44:00 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 206 | void aio_set_event_notifier_poll(AioContext *ctx, |
| 207 | EventNotifier *notifier, |
| 208 | EventNotifierHandler *io_poll_begin, |
| 209 | EventNotifierHandler *io_poll_end) |
| 210 | { |
| 211 | aio_set_fd_poll(ctx, event_notifier_get_fd(notifier), |
| 212 | (IOHandler *)io_poll_begin, |
| 213 | (IOHandler *)io_poll_end); |
| 214 | } |
| 215 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 216 | static bool poll_set_started(AioContext *ctx, AioHandlerList *ready_list, |
| 217 | bool started) |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 218 | { |
| 219 | AioHandler *node; |
Stefan Hajnoczi | e434619 | 2020-03-05 17:08:00 +0000 | [diff] [blame] | 220 | bool progress = false; |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 221 | |
| 222 | if (started == ctx->poll_started) { |
Stefan Hajnoczi | e434619 | 2020-03-05 17:08:00 +0000 | [diff] [blame] | 223 | return false; |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | ctx->poll_started = started; |
| 227 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 228 | qemu_lockcnt_inc(&ctx->list_lock); |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 229 | QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 230 | IOHandler *fn; |
| 231 | |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 232 | if (QLIST_IS_INSERTED(node, node_deleted)) { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 233 | continue; |
| 234 | } |
| 235 | |
| 236 | if (started) { |
| 237 | fn = node->io_poll_begin; |
| 238 | } else { |
| 239 | fn = node->io_poll_end; |
| 240 | } |
| 241 | |
| 242 | if (fn) { |
| 243 | fn(node->opaque); |
| 244 | } |
Stefan Hajnoczi | e434619 | 2020-03-05 17:08:00 +0000 | [diff] [blame] | 245 | |
| 246 | /* Poll one last time in case ->io_poll_end() raced with the event */ |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 247 | if (!started && node->io_poll(node->opaque)) { |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 248 | aio_add_poll_ready_handler(ready_list, node); |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 249 | progress = true; |
Stefan Hajnoczi | e434619 | 2020-03-05 17:08:00 +0000 | [diff] [blame] | 250 | } |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 251 | } |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 252 | qemu_lockcnt_dec(&ctx->list_lock); |
Stefan Hajnoczi | e434619 | 2020-03-05 17:08:00 +0000 | [diff] [blame] | 253 | |
| 254 | return progress; |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 258 | bool aio_prepare(AioContext *ctx) |
| 259 | { |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 260 | AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list); |
| 261 | |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 262 | /* Poll mode cannot be used with glib's event loop, disable it. */ |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 263 | poll_set_started(ctx, &ready_list, false); |
| 264 | /* TODO what to do with this list? */ |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 265 | |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 269 | bool aio_pending(AioContext *ctx) |
| 270 | { |
| 271 | AioHandler *node; |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 272 | bool result = false; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 273 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 274 | /* |
| 275 | * We have to walk very carefully in case aio_set_fd_handler is |
| 276 | * called while we're walking. |
| 277 | */ |
| 278 | qemu_lockcnt_inc(&ctx->list_lock); |
| 279 | |
| 280 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 281 | int revents; |
| 282 | |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 283 | /* TODO should this check poll ready? */ |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 284 | revents = node->pfd.revents & node->pfd.events; |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 285 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) { |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 286 | result = true; |
| 287 | break; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 288 | } |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 289 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) { |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 290 | result = true; |
| 291 | break; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 292 | } |
| 293 | } |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 294 | qemu_lockcnt_dec(&ctx->list_lock); |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 295 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 296 | return result; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 299 | static void aio_free_deleted_handlers(AioContext *ctx) |
| 300 | { |
| 301 | AioHandler *node; |
| 302 | |
| 303 | if (QLIST_EMPTY_RCU(&ctx->deleted_aio_handlers)) { |
| 304 | return; |
| 305 | } |
| 306 | if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { |
| 307 | return; /* we are nested, let the parent do the freeing */ |
| 308 | } |
| 309 | |
| 310 | while ((node = QLIST_FIRST_RCU(&ctx->deleted_aio_handlers))) { |
| 311 | QLIST_REMOVE(node, node); |
| 312 | QLIST_REMOVE(node, node_deleted); |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 313 | QLIST_SAFE_REMOVE(node, node_poll); |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 314 | g_free(node); |
| 315 | } |
| 316 | |
| 317 | qemu_lockcnt_inc_and_unlock(&ctx->list_lock); |
| 318 | } |
| 319 | |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 320 | static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node) |
| 321 | { |
| 322 | bool progress = false; |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 323 | bool poll_ready; |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 324 | int revents; |
| 325 | |
| 326 | revents = node->pfd.revents & node->pfd.events; |
| 327 | node->pfd.revents = 0; |
| 328 | |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 329 | poll_ready = node->poll_ready; |
| 330 | node->poll_ready = false; |
| 331 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 332 | /* |
| 333 | * Start polling AioHandlers when they become ready because activity is |
| 334 | * likely to continue. Note that starvation is theoretically possible when |
| 335 | * fdmon_supports_polling(), but only until the fd fires for the first |
| 336 | * time. |
| 337 | */ |
| 338 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
| 339 | !QLIST_IS_INSERTED(node, node_poll) && |
| 340 | node->io_poll) { |
| 341 | trace_poll_add(ctx, node, node->pfd.fd, revents); |
| 342 | if (ctx->poll_started && node->io_poll_begin) { |
| 343 | node->io_poll_begin(node->opaque); |
| 344 | } |
| 345 | QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll); |
| 346 | } |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 347 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 348 | poll_ready && revents == 0 && node->io_poll_ready) { |
Stefan Hajnoczi | 6d740fb | 2023-05-02 14:41:33 -0400 | [diff] [blame] | 349 | /* |
| 350 | * Remove temporarily to avoid infinite loops when ->io_poll_ready() |
| 351 | * calls aio_poll() before clearing the condition that made the poll |
| 352 | * handler become ready. |
| 353 | */ |
| 354 | QLIST_SAFE_REMOVE(node, node_poll); |
| 355 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 356 | node->io_poll_ready(node->opaque); |
| 357 | |
Stefan Hajnoczi | 6d740fb | 2023-05-02 14:41:33 -0400 | [diff] [blame] | 358 | if (!QLIST_IS_INSERTED(node, node_poll)) { |
| 359 | QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll); |
| 360 | } |
| 361 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 362 | /* |
| 363 | * Return early since revents was zero. aio_notify() does not count as |
| 364 | * progress. |
| 365 | */ |
| 366 | return node->opaque != &ctx->notifier; |
| 367 | } |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 368 | |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 369 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
| 370 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 371 | node->io_read) { |
| 372 | node->io_read(node->opaque); |
| 373 | |
| 374 | /* aio_notify() does not count as progress */ |
| 375 | if (node->opaque != &ctx->notifier) { |
| 376 | progress = true; |
| 377 | } |
| 378 | } |
| 379 | if (!QLIST_IS_INSERTED(node, node_deleted) && |
| 380 | (revents & (G_IO_OUT | G_IO_ERR)) && |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 381 | node->io_write) { |
| 382 | node->io_write(node->opaque); |
| 383 | progress = true; |
| 384 | } |
| 385 | |
| 386 | return progress; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * If we have a list of ready handlers then this is more efficient than |
| 391 | * scanning all handlers with aio_dispatch_handlers(). |
| 392 | */ |
| 393 | static bool aio_dispatch_ready_handlers(AioContext *ctx, |
| 394 | AioHandlerList *ready_list) |
| 395 | { |
| 396 | bool progress = false; |
| 397 | AioHandler *node; |
| 398 | |
| 399 | while ((node = QLIST_FIRST(ready_list))) { |
Stefan Hajnoczi | c39cbed | 2020-02-24 10:34:06 +0000 | [diff] [blame] | 400 | QLIST_REMOVE(node, node_ready); |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 401 | progress = aio_dispatch_handler(ctx, node) || progress; |
| 402 | } |
| 403 | |
| 404 | return progress; |
| 405 | } |
| 406 | |
| 407 | /* Slower than aio_dispatch_ready_handlers() but only used via glib */ |
Paolo Bonzini | 56d2c3c | 2017-01-12 19:07:55 +0100 | [diff] [blame] | 408 | static bool aio_dispatch_handlers(AioContext *ctx) |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 409 | { |
Paolo Bonzini | abf90d3 | 2017-01-12 19:07:56 +0100 | [diff] [blame] | 410 | AioHandler *node, *tmp; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 411 | bool progress = false; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 412 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 413 | QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) { |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 414 | progress = aio_dispatch_handler(ctx, node) || progress; |
Paolo Bonzini | cd9ba1e | 2012-09-24 14:57:22 +0200 | [diff] [blame] | 415 | } |
Alex Bligh | 438e1f4 | 2013-08-21 16:02:53 +0100 | [diff] [blame] | 416 | |
Paolo Bonzini | 56d2c3c | 2017-01-12 19:07:55 +0100 | [diff] [blame] | 417 | return progress; |
| 418 | } |
| 419 | |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 420 | void aio_dispatch(AioContext *ctx) |
Paolo Bonzini | 56d2c3c | 2017-01-12 19:07:55 +0100 | [diff] [blame] | 421 | { |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 422 | qemu_lockcnt_inc(&ctx->list_lock); |
Paolo Bonzini | bd45143 | 2017-02-13 14:52:34 +0100 | [diff] [blame] | 423 | aio_bh_poll(ctx); |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 424 | aio_dispatch_handlers(ctx); |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 425 | aio_free_deleted_handlers(ctx); |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 426 | qemu_lockcnt_dec(&ctx->list_lock); |
Paolo Bonzini | 56d2c3c | 2017-01-12 19:07:55 +0100 | [diff] [blame] | 427 | |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 428 | timerlistgroup_run_timers(&ctx->tlg); |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 431 | static bool run_poll_handlers_once(AioContext *ctx, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 432 | AioHandlerList *ready_list, |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 433 | int64_t now, |
| 434 | int64_t *timeout) |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 435 | { |
| 436 | bool progress = false; |
| 437 | AioHandler *node; |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 438 | AioHandler *tmp; |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 439 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 440 | QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) { |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 441 | if (node->io_poll(node->opaque)) { |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 442 | aio_add_poll_ready_handler(ready_list, node); |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 443 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 444 | node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS; |
| 445 | |
Paolo Bonzini | 993ed89 | 2019-04-09 14:28:23 +0200 | [diff] [blame] | 446 | /* |
| 447 | * Polling was successful, exit try_poll_mode immediately |
| 448 | * to adjust the next polling time. |
| 449 | */ |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 450 | *timeout = 0; |
Paolo Bonzini | cfeb35d | 2018-09-12 19:10:40 +0200 | [diff] [blame] | 451 | if (node->opaque != &ctx->notifier) { |
| 452 | progress = true; |
| 453 | } |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Caller handles freeing deleted nodes. Don't do it here. */ |
| 457 | } |
| 458 | |
| 459 | return progress; |
| 460 | } |
| 461 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 462 | static bool fdmon_supports_polling(AioContext *ctx) |
| 463 | { |
| 464 | return ctx->fdmon_ops->need_wait != aio_poll_disabled; |
| 465 | } |
| 466 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 467 | static bool remove_idle_poll_handlers(AioContext *ctx, |
| 468 | AioHandlerList *ready_list, |
| 469 | int64_t now) |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 470 | { |
| 471 | AioHandler *node; |
| 472 | AioHandler *tmp; |
| 473 | bool progress = false; |
| 474 | |
| 475 | /* |
| 476 | * File descriptor monitoring implementations without userspace polling |
| 477 | * support suffer from starvation when a subset of handlers is polled |
| 478 | * because fds will not be processed in a timely fashion. Don't remove |
| 479 | * idle poll handlers. |
| 480 | */ |
| 481 | if (!fdmon_supports_polling(ctx)) { |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) { |
| 486 | if (node->poll_idle_timeout == 0LL) { |
| 487 | node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS; |
| 488 | } else if (now >= node->poll_idle_timeout) { |
| 489 | trace_poll_remove(ctx, node, node->pfd.fd); |
| 490 | node->poll_idle_timeout = 0LL; |
| 491 | QLIST_SAFE_REMOVE(node, node_poll); |
| 492 | if (ctx->poll_started && node->io_poll_end) { |
| 493 | node->io_poll_end(node->opaque); |
| 494 | |
| 495 | /* |
| 496 | * Final poll in case ->io_poll_end() races with an event. |
| 497 | * Nevermind about re-adding the handler in the rare case where |
| 498 | * this causes progress. |
| 499 | */ |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 500 | if (node->io_poll(node->opaque)) { |
Stefan Hajnoczi | fc87964 | 2022-02-23 15:57:03 +0000 | [diff] [blame] | 501 | aio_add_poll_ready_handler(ready_list, node); |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 502 | progress = true; |
| 503 | } |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return progress; |
| 509 | } |
| 510 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 511 | /* run_poll_handlers: |
| 512 | * @ctx: the AioContext |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 513 | * @ready_list: the list to place ready handlers on |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 514 | * @max_ns: maximum time to poll for, in nanoseconds |
| 515 | * |
| 516 | * Polls for a given time. |
| 517 | * |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 518 | * Note that the caller must have incremented ctx->list_lock. |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 519 | * |
| 520 | * Returns: true if progress was made, false otherwise |
| 521 | */ |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 522 | static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list, |
| 523 | int64_t max_ns, int64_t *timeout) |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 524 | { |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 525 | bool progress; |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 526 | int64_t start_time, elapsed_time; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 527 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 528 | assert(qemu_lockcnt_count(&ctx->list_lock) > 0); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 529 | |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 530 | trace_run_poll_handlers_begin(ctx, max_ns, *timeout); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 531 | |
Stefan Hajnoczi | 3aa221b | 2020-03-05 17:08:01 +0000 | [diff] [blame] | 532 | /* |
| 533 | * Optimization: ->io_poll() handlers often contain RCU read critical |
| 534 | * sections and we therefore see many rcu_read_lock() -> rcu_read_unlock() |
| 535 | * -> rcu_read_lock() -> ... sequences with expensive memory |
| 536 | * synchronization primitives. Make the entire polling loop an RCU |
| 537 | * critical section because nested rcu_read_lock()/rcu_read_unlock() calls |
| 538 | * are cheap. |
| 539 | */ |
| 540 | RCU_READ_LOCK_GUARD(); |
| 541 | |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 542 | start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 543 | do { |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 544 | progress = run_poll_handlers_once(ctx, ready_list, |
| 545 | start_time, timeout); |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 546 | elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time; |
Paolo Bonzini | 993ed89 | 2019-04-09 14:28:23 +0200 | [diff] [blame] | 547 | max_ns = qemu_soonest_timeout(*timeout, max_ns); |
| 548 | assert(!(max_ns && progress)); |
Stefan Hajnoczi | aa38e19 | 2020-03-05 17:08:05 +0000 | [diff] [blame] | 549 | } while (elapsed_time < max_ns && !ctx->fdmon_ops->need_wait(ctx)); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 550 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 551 | if (remove_idle_poll_handlers(ctx, ready_list, |
| 552 | start_time + elapsed_time)) { |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 553 | *timeout = 0; |
| 554 | progress = true; |
| 555 | } |
| 556 | |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 557 | /* If time has passed with no successful polling, adjust *timeout to |
| 558 | * keep the same ending time. |
| 559 | */ |
| 560 | if (*timeout != -1) { |
| 561 | *timeout -= MIN(*timeout, elapsed_time); |
| 562 | } |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 563 | |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 564 | trace_run_poll_handlers_end(ctx, progress, *timeout); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 565 | return progress; |
| 566 | } |
| 567 | |
| 568 | /* try_poll_mode: |
| 569 | * @ctx: the AioContext |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 570 | * @ready_list: list to add handlers that need to be run |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 571 | * @timeout: timeout for blocking wait, computed by the caller and updated if |
| 572 | * polling succeeds. |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 573 | * |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 574 | * Note that the caller must have incremented ctx->list_lock. |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 575 | * |
| 576 | * Returns: true if progress was made, false otherwise |
| 577 | */ |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 578 | static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list, |
| 579 | int64_t *timeout) |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 580 | { |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 581 | int64_t max_ns; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 582 | |
Stefan Hajnoczi | d37d0e3 | 2020-03-05 17:08:06 +0000 | [diff] [blame] | 583 | if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) { |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns); |
Stefan Hajnoczi | aa38e19 | 2020-03-05 17:08:05 +0000 | [diff] [blame] | 588 | if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) { |
Chao Gao | 816a430 | 2022-07-10 20:08:49 +0800 | [diff] [blame] | 589 | /* |
| 590 | * Enable poll mode. It pairs with the poll_set_started() in |
| 591 | * aio_poll() which disables poll mode. |
| 592 | */ |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 593 | poll_set_started(ctx, ready_list, true); |
Stefan Hajnoczi | 684e508 | 2016-12-01 19:26:49 +0000 | [diff] [blame] | 594 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 595 | if (run_poll_handlers(ctx, ready_list, max_ns, timeout)) { |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 596 | return true; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Stefan Hajnoczi | e434619 | 2020-03-05 17:08:00 +0000 | [diff] [blame] | 599 | return false; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 602 | bool aio_poll(AioContext *ctx, bool blocking) |
| 603 | { |
Stefan Hajnoczi | 7391d34 | 2020-02-14 17:17:12 +0000 | [diff] [blame] | 604 | AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list); |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 605 | bool progress; |
Stefan Hajnoczi | 44277bf | 2020-08-06 14:18:02 +0100 | [diff] [blame] | 606 | bool use_notify_me; |
Paolo Bonzini | e98ab09 | 2015-02-20 17:26:50 +0100 | [diff] [blame] | 607 | int64_t timeout; |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 608 | int64_t start = 0; |
Stefan Hajnoczi | d0c8d2c | 2013-02-20 11:28:31 +0100 | [diff] [blame] | 609 | |
Paolo Bonzini | 5710a3e | 2020-04-07 10:07:46 -0400 | [diff] [blame] | 610 | /* |
| 611 | * There cannot be two concurrent aio_poll calls for the same AioContext (or |
| 612 | * an aio_poll concurrent with a GSource prepare/check/dispatch callback). |
| 613 | * We rely on this below to avoid slow locked accesses to ctx->notify_me. |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 614 | * |
| 615 | * aio_poll() may only be called in the AioContext's thread. iohandler_ctx |
| 616 | * is special in that it runs in the main thread, but that thread's context |
| 617 | * is qemu_aio_context. |
Paolo Bonzini | 5710a3e | 2020-04-07 10:07:46 -0400 | [diff] [blame] | 618 | */ |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 619 | assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ? |
| 620 | qemu_get_aio_context() : ctx)); |
Kevin Wolf | 0dc165c | 2019-02-14 13:13:36 +0100 | [diff] [blame] | 621 | |
Paolo Bonzini | 2bbf11d | 2017-01-12 19:07:57 +0100 | [diff] [blame] | 622 | qemu_lockcnt_inc(&ctx->list_lock); |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 623 | |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 624 | if (ctx->poll_max_ns) { |
| 625 | start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 626 | } |
| 627 | |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 628 | timeout = blocking ? aio_compute_timeout(ctx) : 0; |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 629 | progress = try_poll_mode(ctx, &ready_list, &timeout); |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 630 | assert(!(timeout && progress)); |
| 631 | |
Stefan Hajnoczi | 44277bf | 2020-08-06 14:18:02 +0100 | [diff] [blame] | 632 | /* |
| 633 | * aio_notify can avoid the expensive event_notifier_set if |
| 634 | * everything (file descriptors, bottom halves, timers) will |
| 635 | * be re-evaluated before the next blocking poll(). This is |
| 636 | * already true when aio_poll is called with blocking == false; |
| 637 | * if blocking == true, it is only true after poll() returns, |
| 638 | * so disable the optimization now. |
| 639 | */ |
| 640 | use_notify_me = timeout != 0; |
| 641 | if (use_notify_me) { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 642 | qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2); |
Stefan Hajnoczi | 44277bf | 2020-08-06 14:18:02 +0100 | [diff] [blame] | 643 | /* |
| 644 | * Write ctx->notify_me before reading ctx->notified. Pairs with |
| 645 | * smp_mb in aio_notify(). |
| 646 | */ |
| 647 | smp_mb(); |
| 648 | |
| 649 | /* Don't block if aio_notify() was called */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 650 | if (qatomic_read(&ctx->notified)) { |
Stefan Hajnoczi | 44277bf | 2020-08-06 14:18:02 +0100 | [diff] [blame] | 651 | timeout = 0; |
| 652 | } |
| 653 | } |
| 654 | |
Paolo Bonzini | e30cffa | 2018-09-12 19:10:39 +0200 | [diff] [blame] | 655 | /* If polling is allowed, non-blocking aio_poll does not need the |
| 656 | * system call---a single round of run_poll_handlers_once suffices. |
| 657 | */ |
Stefan Hajnoczi | aa38e19 | 2020-03-05 17:08:05 +0000 | [diff] [blame] | 658 | if (timeout || ctx->fdmon_ops->need_wait(ctx)) { |
Chao Gao | 816a430 | 2022-07-10 20:08:49 +0800 | [diff] [blame] | 659 | /* |
| 660 | * Disable poll mode. poll mode should be disabled before the call |
| 661 | * of ctx->fdmon_ops->wait() so that guest's notification can wake |
| 662 | * up IO threads when some work becomes pending. It is essential to |
| 663 | * avoid hangs or unnecessary latency. |
| 664 | */ |
| 665 | if (poll_set_started(ctx, &ready_list, false)) { |
| 666 | timeout = 0; |
| 667 | progress = true; |
| 668 | } |
| 669 | |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 670 | ctx->fdmon_ops->wait(ctx, &ready_list, timeout); |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 671 | } |
| 672 | |
Stefan Hajnoczi | 44277bf | 2020-08-06 14:18:02 +0100 | [diff] [blame] | 673 | if (use_notify_me) { |
Paolo Bonzini | 5710a3e | 2020-04-07 10:07:46 -0400 | [diff] [blame] | 674 | /* Finish the poll before clearing the flag. */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 675 | qatomic_store_release(&ctx->notify_me, |
| 676 | qatomic_read(&ctx->notify_me) - 2); |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 677 | } |
Paolo Bonzini | 9eb0bfc | 2012-04-12 14:00:56 +0200 | [diff] [blame] | 678 | |
Stefan Hajnoczi | 44277bf | 2020-08-06 14:18:02 +0100 | [diff] [blame] | 679 | aio_notify_accept(ctx); |
| 680 | |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 681 | /* Adjust polling time */ |
| 682 | if (ctx->poll_max_ns) { |
| 683 | int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start; |
| 684 | |
| 685 | if (block_ns <= ctx->poll_ns) { |
| 686 | /* This is the sweet spot, no adjustment needed */ |
| 687 | } else if (block_ns > ctx->poll_max_ns) { |
| 688 | /* We'd have to poll for too long, poll less */ |
| 689 | int64_t old = ctx->poll_ns; |
| 690 | |
| 691 | if (ctx->poll_shrink) { |
| 692 | ctx->poll_ns /= ctx->poll_shrink; |
| 693 | } else { |
| 694 | ctx->poll_ns = 0; |
| 695 | } |
| 696 | |
| 697 | trace_poll_shrink(ctx, old, ctx->poll_ns); |
| 698 | } else if (ctx->poll_ns < ctx->poll_max_ns && |
| 699 | block_ns < ctx->poll_max_ns) { |
| 700 | /* There is room to grow, poll longer */ |
| 701 | int64_t old = ctx->poll_ns; |
| 702 | int64_t grow = ctx->poll_grow; |
| 703 | |
| 704 | if (grow == 0) { |
| 705 | grow = 2; |
| 706 | } |
| 707 | |
| 708 | if (ctx->poll_ns) { |
| 709 | ctx->poll_ns *= grow; |
| 710 | } else { |
| 711 | ctx->poll_ns = 4000; /* start polling at 4 microseconds */ |
| 712 | } |
| 713 | |
| 714 | if (ctx->poll_ns > ctx->poll_max_ns) { |
| 715 | ctx->poll_ns = ctx->poll_max_ns; |
| 716 | } |
| 717 | |
| 718 | trace_poll_grow(ctx, old, ctx->poll_ns); |
| 719 | } |
| 720 | } |
| 721 | |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 722 | progress |= aio_bh_poll(ctx); |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 723 | progress |= aio_dispatch_ready_handlers(ctx, &ready_list); |
Paolo Bonzini | bcdc185 | 2012-04-12 14:00:55 +0200 | [diff] [blame] | 724 | |
Stefan Hajnoczi | 4749079 | 2020-02-14 17:17:11 +0000 | [diff] [blame] | 725 | aio_free_deleted_handlers(ctx); |
| 726 | |
Paolo Bonzini | bd45143 | 2017-02-13 14:52:34 +0100 | [diff] [blame] | 727 | qemu_lockcnt_dec(&ctx->list_lock); |
| 728 | |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 729 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
| 730 | |
Stefan Hajnoczi | 164a101 | 2013-04-11 16:56:50 +0200 | [diff] [blame] | 731 | return progress; |
aliguori | a76bab4 | 2008-09-22 19:17:18 +0000 | [diff] [blame] | 732 | } |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 733 | |
Cao jin | 7e00346 | 2016-07-15 18:28:44 +0800 | [diff] [blame] | 734 | void aio_context_setup(AioContext *ctx) |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 735 | { |
Stefan Hajnoczi | 1f050a4 | 2020-03-05 17:08:02 +0000 | [diff] [blame] | 736 | ctx->fdmon_ops = &fdmon_poll_ops; |
| 737 | ctx->epollfd = -1; |
| 738 | |
Stefan Hajnoczi | 73fd282 | 2020-03-05 17:08:04 +0000 | [diff] [blame] | 739 | /* Use the fastest fd monitoring implementation if available */ |
| 740 | if (fdmon_io_uring_setup(ctx)) { |
| 741 | return; |
| 742 | } |
| 743 | |
Stefan Hajnoczi | 1f050a4 | 2020-03-05 17:08:02 +0000 | [diff] [blame] | 744 | fdmon_epoll_setup(ctx); |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 745 | } |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 746 | |
Jie Wang | cd0a6d2 | 2018-05-17 08:42:43 +0800 | [diff] [blame] | 747 | void aio_context_destroy(AioContext *ctx) |
| 748 | { |
Stefan Hajnoczi | 73fd282 | 2020-03-05 17:08:04 +0000 | [diff] [blame] | 749 | fdmon_io_uring_destroy(ctx); |
Stefan Hajnoczi | 1f050a4 | 2020-03-05 17:08:02 +0000 | [diff] [blame] | 750 | fdmon_epoll_disable(ctx); |
Stefan Hajnoczi | de137e4 | 2020-05-11 19:36:29 +0100 | [diff] [blame] | 751 | aio_free_deleted_handlers(ctx); |
Jie Wang | cd0a6d2 | 2018-05-17 08:42:43 +0800 | [diff] [blame] | 752 | } |
| 753 | |
Stefan Hajnoczi | ba607ca | 2020-05-11 19:36:30 +0100 | [diff] [blame] | 754 | void aio_context_use_g_source(AioContext *ctx) |
| 755 | { |
| 756 | /* |
| 757 | * Disable io_uring when the glib main loop is used because it doesn't |
| 758 | * support mixed glib/aio_poll() usage. It relies on aio_poll() being |
| 759 | * called regularly so that changes to the monitored file descriptors are |
| 760 | * submitted, otherwise a list of pending fd handlers builds up. |
| 761 | */ |
| 762 | fdmon_io_uring_destroy(ctx); |
| 763 | aio_free_deleted_handlers(ctx); |
| 764 | } |
| 765 | |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 766 | void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, |
| 767 | int64_t grow, int64_t shrink, Error **errp) |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 768 | { |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 769 | /* No thread synchronization here, it doesn't matter if an incorrect value |
| 770 | * is used once. |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 771 | */ |
| 772 | ctx->poll_max_ns = max_ns; |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 773 | ctx->poll_ns = 0; |
| 774 | ctx->poll_grow = grow; |
| 775 | ctx->poll_shrink = shrink; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 776 | |
| 777 | aio_notify(ctx); |
| 778 | } |
Stefano Garzarella | 1793ad0 | 2021-07-21 11:42:10 +0200 | [diff] [blame] | 779 | |
| 780 | void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch, |
| 781 | Error **errp) |
| 782 | { |
| 783 | /* |
| 784 | * No thread synchronization here, it doesn't matter if an incorrect value |
| 785 | * is used once. |
| 786 | */ |
| 787 | ctx->aio_max_batch = max_batch; |
| 788 | |
| 789 | aio_notify(ctx); |
| 790 | } |