blob: 2eada2e0499a4e15ecd02a9c1d5d4467a9920641 [file] [log] [blame]
aliguoria76bab42008-09-22 19:17:18 +00001/*
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 Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
aliguoria76bab42008-09-22 19:17:18 +000014 */
15
16#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010017#include "block/block.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/queue.h"
19#include "qemu/sockets.h"
aliguoria76bab42008-09-22 19:17:18 +000020
aliguoria76bab42008-09-22 19:17:18 +000021struct AioHandler
22{
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020023 GPollFD pfd;
aliguoria76bab42008-09-22 19:17:18 +000024 IOHandler *io_read;
25 IOHandler *io_write;
aliguoria76bab42008-09-22 19:17:18 +000026 int deleted;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010027 int pollfds_idx;
aliguoria76bab42008-09-22 19:17:18 +000028 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +000029 QLIST_ENTRY(AioHandler) node;
aliguoria76bab42008-09-22 19:17:18 +000030};
31
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020032static AioHandler *find_aio_handler(AioContext *ctx, int fd)
aliguoria76bab42008-09-22 19:17:18 +000033{
34 AioHandler *node;
35
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020036 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020037 if (node->pfd.fd == fd)
Alexander Graf79d5ca52009-05-06 02:58:48 +020038 if (!node->deleted)
39 return node;
aliguoria76bab42008-09-22 19:17:18 +000040 }
41
42 return NULL;
43}
44
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020045void aio_set_fd_handler(AioContext *ctx,
46 int fd,
47 IOHandler *io_read,
48 IOHandler *io_write,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020049 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +000050{
51 AioHandler *node;
52
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020053 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +000054
55 /* Are we deleting the fd handler? */
56 if (!io_read && !io_write) {
57 if (node) {
Paolo Bonzinie3713e02012-09-24 14:57:41 +020058 g_source_remove_poll(&ctx->source, &node->pfd);
59
aliguoria76bab42008-09-22 19:17:18 +000060 /* If the lock is held, just mark the node as deleted */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020061 if (ctx->walking_handlers) {
aliguoria76bab42008-09-22 19:17:18 +000062 node->deleted = 1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020063 node->pfd.revents = 0;
64 } else {
aliguoria76bab42008-09-22 19:17:18 +000065 /* 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 Swirl72cf2d42009-09-12 07:36:22 +000069 QLIST_REMOVE(node, node);
Anthony Liguori7267c092011-08-20 22:09:37 -050070 g_free(node);
aliguoria76bab42008-09-22 19:17:18 +000071 }
72 }
73 } else {
74 if (node == NULL) {
75 /* Alloc and insert if it's not already there */
Anthony Liguori7267c092011-08-20 22:09:37 -050076 node = g_malloc0(sizeof(AioHandler));
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020077 node->pfd.fd = fd;
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020078 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +020079
80 g_source_add_poll(&ctx->source, &node->pfd);
aliguoria76bab42008-09-22 19:17:18 +000081 }
82 /* Update handler with latest information */
83 node->io_read = io_read;
84 node->io_write = io_write;
aliguoria76bab42008-09-22 19:17:18 +000085 node->opaque = opaque;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +010086 node->pollfds_idx = -1;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020087
Stefan Hajnoczib5a01a72013-02-20 11:28:33 +010088 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);
aliguoria76bab42008-09-22 19:17:18 +000090 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +020091
92 aio_notify(ctx);
aliguoria76bab42008-09-22 19:17:18 +000093}
94
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020095void aio_set_event_notifier(AioContext *ctx,
96 EventNotifier *notifier,
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +020097 EventNotifierHandler *io_read)
Paolo Bonzini9958c352012-06-09 03:44:00 +020098{
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020099 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +0200100 (IOHandler *)io_read, NULL, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200101}
102
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200103bool aio_pending(AioContext *ctx)
104{
105 AioHandler *node;
106
107 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
108 int revents;
109
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200110 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 Hajnoczid0c8d2c2013-02-20 11:28:31 +0100122static bool aio_dispatch(AioContext *ctx)
aliguoria76bab42008-09-22 19:17:18 +0000123{
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200124 AioHandler *node;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100125 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000126
Kevin Wolf8febfa22009-10-22 17:54:36 +0200127 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200128 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200129 * 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 Hajnoczid0c8d2c2013-02-20 11:28:31 +0100141 if (!node->deleted &&
142 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
143 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200144 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200145
146 /* aio_notify() does not count as progress */
147 if (node->opaque != &ctx->notifier) {
148 progress = true;
149 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200150 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100151 if (!node->deleted &&
152 (revents & (G_IO_OUT | G_IO_ERR)) &&
153 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200154 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 Bligh438e1f42013-08-21 16:02:53 +0100168
169 /* Run our timers */
170 progress |= timerlistgroup_run_timers(&ctx->tlg);
171
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100172 return progress;
173}
174
175bool aio_poll(AioContext *ctx, bool blocking)
176{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100177 AioHandler *node;
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200178 bool was_dispatching;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100179 int ret;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200180 bool progress;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100181
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200182 was_dispatching = ctx->dispatching;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100183 progress = false;
184
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200185 /* aio_notify can avoid the expensive event_notifier_set if
186 * everything (file descriptors, bottom halves, timers) will
187 * be re-evaluated before the next blocking poll(). This happens
188 * in two cases:
189 *
190 * 1) when aio_poll is called with blocking == false
191 *
192 * 2) when we are called after poll(). If we are called before
193 * poll(), bottom halves will not be re-evaluated and we need
194 * aio_notify() if blocking == true.
195 *
196 * The first aio_dispatch() only does something when AioContext is
197 * running as a GSource, and in that case aio_poll is used only
198 * with blocking == false, so this optimization is already quite
199 * effective. However, the code is ugly and should be restructured
200 * to have a single aio_dispatch() call. To do this, we need to
201 * reorganize aio_poll into a prepare/poll/dispatch model like
202 * glib's.
203 *
204 * If we're in a nested event loop, ctx->dispatching might be true.
205 * In that case we can restore it just before returning, but we
206 * have to clear it now.
207 */
208 aio_set_dispatching(ctx, !blocking);
209
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100210 /*
211 * If there are callbacks left that have been queued, we need to call them.
212 * Do not call select in this case, because it is possible that the caller
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200213 * does not need a complete flush (as is the case for aio_poll loops).
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100214 */
215 if (aio_bh_poll(ctx)) {
216 blocking = false;
217 progress = true;
218 }
219
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200220 /* Re-evaluate condition (1) above. */
221 aio_set_dispatching(ctx, !blocking);
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100222 if (aio_dispatch(ctx)) {
223 progress = true;
224 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200225
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200226 if (progress && !blocking) {
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200227 goto out;
Paolo Bonzinibafbd6a2012-04-12 14:00:54 +0200228 }
Kevin Wolf8febfa22009-10-22 17:54:36 +0200229
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200230 ctx->walking_handlers++;
aliguoria76bab42008-09-22 19:17:18 +0000231
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100232 g_array_set_size(ctx->pollfds, 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200233
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100234 /* fill pollfds */
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200235 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100236 node->pollfds_idx = -1;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100237 if (!node->deleted && node->pfd.events) {
238 GPollFD pfd = {
239 .fd = node->pfd.fd,
240 .events = node->pfd.events,
241 };
242 node->pollfds_idx = ctx->pollfds->len;
243 g_array_append_val(ctx->pollfds, pfd);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200244 }
245 }
246
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200247 ctx->walking_handlers--;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200248
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200249 /* wait until next event */
Alex Bligh438e1f42013-08-21 16:02:53 +0100250 ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
251 ctx->pollfds->len,
252 blocking ? timerlistgroup_deadline_ns(&ctx->tlg) : 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200253
254 /* if we have any readable fds, dispatch event */
255 if (ret > 0) {
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100256 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
257 if (node->pollfds_idx != -1) {
258 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
259 node->pollfds_idx);
260 node->pfd.revents = pfd->revents;
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200261 }
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100262 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100263 }
264
265 /* Run dispatch even if there were no readable fds to run timers */
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200266 aio_set_dispatching(ctx, true);
Alex Bligh438e1f42013-08-21 16:02:53 +0100267 if (aio_dispatch(ctx)) {
268 progress = true;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200269 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200270
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200271out:
272 aio_set_dispatching(ctx, was_dispatching);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200273 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000274}