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