blob: 577527fda5fe41e85859e5fc8abaaf801263d94e [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
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
aliguoria76bab42008-09-22 19:17:18 +000017#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010018#include "block/block.h"
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +010019#include "qemu/rcu_queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/sockets.h"
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +000021#include "qemu/cutils.h"
Daniel P. Berrange0ab8ed12017-01-25 16:14:15 +000022#include "trace-root.h"
Matthew Fortune147dfab2016-02-23 15:42:14 +000023#ifdef CONFIG_EPOLL_CREATE1
Fam Zhengfbe3fc52015-10-30 12:06:29 +080024#include <sys/epoll.h>
25#endif
aliguoria76bab42008-09-22 19:17:18 +000026
aliguoria76bab42008-09-22 19:17:18 +000027struct AioHandler
28{
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +020029 GPollFD pfd;
aliguoria76bab42008-09-22 19:17:18 +000030 IOHandler *io_read;
31 IOHandler *io_write;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +000032 AioPollFn *io_poll;
Stefan Hajnoczi684e5082016-12-01 19:26:49 +000033 IOHandler *io_poll_begin;
34 IOHandler *io_poll_end;
aliguoria76bab42008-09-22 19:17:18 +000035 int deleted;
36 void *opaque;
Fam Zhengdca21ef2015-10-23 11:08:05 +080037 bool is_external;
Blue Swirl72cf2d42009-09-12 07:36:22 +000038 QLIST_ENTRY(AioHandler) node;
aliguoria76bab42008-09-22 19:17:18 +000039};
40
Matthew Fortune147dfab2016-02-23 15:42:14 +000041#ifdef CONFIG_EPOLL_CREATE1
Fam Zhengfbe3fc52015-10-30 12:06:29 +080042
43/* The fd number threashold to switch to epoll */
44#define EPOLL_ENABLE_THRESHOLD 64
45
46static void aio_epoll_disable(AioContext *ctx)
47{
48 ctx->epoll_available = false;
49 if (!ctx->epoll_enabled) {
50 return;
51 }
52 ctx->epoll_enabled = false;
53 close(ctx->epollfd);
54}
55
56static inline int epoll_events_from_pfd(int pfd_events)
57{
58 return (pfd_events & G_IO_IN ? EPOLLIN : 0) |
59 (pfd_events & G_IO_OUT ? EPOLLOUT : 0) |
60 (pfd_events & G_IO_HUP ? EPOLLHUP : 0) |
61 (pfd_events & G_IO_ERR ? EPOLLERR : 0);
62}
63
64static bool aio_epoll_try_enable(AioContext *ctx)
65{
66 AioHandler *node;
67 struct epoll_event event;
68
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +010069 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
Fam Zhengfbe3fc52015-10-30 12:06:29 +080070 int r;
71 if (node->deleted || !node->pfd.events) {
72 continue;
73 }
74 event.events = epoll_events_from_pfd(node->pfd.events);
75 event.data.ptr = node;
76 r = epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, node->pfd.fd, &event);
77 if (r) {
78 return false;
79 }
80 }
81 ctx->epoll_enabled = true;
82 return true;
83}
84
85static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
86{
87 struct epoll_event event;
88 int r;
Paolo Bonzini35dd66e2016-11-08 14:55:24 +010089 int ctl;
Fam Zhengfbe3fc52015-10-30 12:06:29 +080090
91 if (!ctx->epoll_enabled) {
92 return;
93 }
94 if (!node->pfd.events) {
Paolo Bonzini35dd66e2016-11-08 14:55:24 +010095 ctl = EPOLL_CTL_DEL;
Fam Zhengfbe3fc52015-10-30 12:06:29 +080096 } else {
97 event.data.ptr = node;
98 event.events = epoll_events_from_pfd(node->pfd.events);
Paolo Bonzini35dd66e2016-11-08 14:55:24 +010099 ctl = is_new ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
100 }
101
102 r = epoll_ctl(ctx->epollfd, ctl, node->pfd.fd, &event);
103 if (r) {
104 aio_epoll_disable(ctx);
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800105 }
106}
107
108static int aio_epoll(AioContext *ctx, GPollFD *pfds,
109 unsigned npfd, int64_t timeout)
110{
111 AioHandler *node;
112 int i, ret = 0;
113 struct epoll_event events[128];
114
115 assert(npfd == 1);
116 assert(pfds[0].fd == ctx->epollfd);
117 if (timeout > 0) {
118 ret = qemu_poll_ns(pfds, npfd, timeout);
119 }
120 if (timeout <= 0 || ret > 0) {
121 ret = epoll_wait(ctx->epollfd, events,
122 sizeof(events) / sizeof(events[0]),
123 timeout);
124 if (ret <= 0) {
125 goto out;
126 }
127 for (i = 0; i < ret; i++) {
128 int ev = events[i].events;
129 node = events[i].data.ptr;
130 node->pfd.revents = (ev & EPOLLIN ? G_IO_IN : 0) |
131 (ev & EPOLLOUT ? G_IO_OUT : 0) |
132 (ev & EPOLLHUP ? G_IO_HUP : 0) |
133 (ev & EPOLLERR ? G_IO_ERR : 0);
134 }
135 }
136out:
137 return ret;
138}
139
140static bool aio_epoll_enabled(AioContext *ctx)
141{
142 /* Fall back to ppoll when external clients are disabled. */
143 return !aio_external_disabled(ctx) && ctx->epoll_enabled;
144}
145
146static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
147 unsigned npfd, int64_t timeout)
148{
149 if (!ctx->epoll_available) {
150 return false;
151 }
152 if (aio_epoll_enabled(ctx)) {
153 return true;
154 }
155 if (npfd >= EPOLL_ENABLE_THRESHOLD) {
156 if (aio_epoll_try_enable(ctx)) {
157 return true;
158 } else {
159 aio_epoll_disable(ctx);
160 }
161 }
162 return false;
163}
164
165#else
166
167static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new)
168{
169}
170
171static int aio_epoll(AioContext *ctx, GPollFD *pfds,
172 unsigned npfd, int64_t timeout)
173{
174 assert(false);
175}
176
177static bool aio_epoll_enabled(AioContext *ctx)
178{
179 return false;
180}
181
182static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds,
183 unsigned npfd, int64_t timeout)
184{
185 return false;
186}
187
188#endif
189
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200190static AioHandler *find_aio_handler(AioContext *ctx, int fd)
aliguoria76bab42008-09-22 19:17:18 +0000191{
192 AioHandler *node;
193
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200194 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200195 if (node->pfd.fd == fd)
Alexander Graf79d5ca52009-05-06 02:58:48 +0200196 if (!node->deleted)
197 return node;
aliguoria76bab42008-09-22 19:17:18 +0000198 }
199
200 return NULL;
201}
202
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200203void aio_set_fd_handler(AioContext *ctx,
204 int fd,
Fam Zhengdca21ef2015-10-23 11:08:05 +0800205 bool is_external,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200206 IOHandler *io_read,
207 IOHandler *io_write,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000208 AioPollFn *io_poll,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200209 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +0000210{
211 AioHandler *node;
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800212 bool is_new = false;
Fam Zheng0ed39f3d2015-11-16 14:32:14 +0800213 bool deleted = false;
aliguoria76bab42008-09-22 19:17:18 +0000214
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100215 qemu_lockcnt_lock(&ctx->list_lock);
216
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200217 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +0000218
219 /* Are we deleting the fd handler? */
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000220 if (!io_read && !io_write && !io_poll) {
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100221 if (node == NULL) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100222 qemu_lockcnt_unlock(&ctx->list_lock);
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100223 return;
224 }
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200225
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100226 g_source_remove_poll(&ctx->source, &node->pfd);
227
228 /* If the lock is held, just mark the node as deleted */
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100229 if (qemu_lockcnt_count(&ctx->list_lock)) {
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100230 node->deleted = 1;
231 node->pfd.revents = 0;
232 } else {
233 /* Otherwise, delete it for real. We can't just mark it as
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100234 * deleted because deleted nodes are only cleaned up while
235 * no one is walking the handlers list.
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100236 */
237 QLIST_REMOVE(node, node);
238 deleted = true;
aliguoria76bab42008-09-22 19:17:18 +0000239 }
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000240
241 if (!node->io_poll) {
242 ctx->poll_disable_cnt--;
243 }
aliguoria76bab42008-09-22 19:17:18 +0000244 } else {
245 if (node == NULL) {
246 /* Alloc and insert if it's not already there */
Markus Armbruster3ba235a2014-12-04 13:55:09 +0100247 node = g_new0(AioHandler, 1);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200248 node->pfd.fd = fd;
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100249 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200250
251 g_source_add_poll(&ctx->source, &node->pfd);
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800252 is_new = true;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000253
254 ctx->poll_disable_cnt += !io_poll;
255 } else {
256 ctx->poll_disable_cnt += !io_poll - !node->io_poll;
aliguoria76bab42008-09-22 19:17:18 +0000257 }
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000258
aliguoria76bab42008-09-22 19:17:18 +0000259 /* Update handler with latest information */
260 node->io_read = io_read;
261 node->io_write = io_write;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000262 node->io_poll = io_poll;
aliguoria76bab42008-09-22 19:17:18 +0000263 node->opaque = opaque;
Fam Zhengdca21ef2015-10-23 11:08:05 +0800264 node->is_external = is_external;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200265
Stefan Hajnoczib5a01a72013-02-20 11:28:33 +0100266 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
267 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
aliguoria76bab42008-09-22 19:17:18 +0000268 }
Paolo Bonzini7ed2b242012-09-25 10:22:39 +0200269
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800270 aio_epoll_update(ctx, node, is_new);
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100271 qemu_lockcnt_unlock(&ctx->list_lock);
Paolo Bonzini7ed2b242012-09-25 10:22:39 +0200272 aio_notify(ctx);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000273
Fam Zheng0ed39f3d2015-11-16 14:32:14 +0800274 if (deleted) {
275 g_free(node);
276 }
aliguoria76bab42008-09-22 19:17:18 +0000277}
278
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000279void aio_set_fd_poll(AioContext *ctx, int fd,
280 IOHandler *io_poll_begin,
281 IOHandler *io_poll_end)
282{
283 AioHandler *node = find_aio_handler(ctx, fd);
284
285 if (!node) {
286 return;
287 }
288
289 node->io_poll_begin = io_poll_begin;
290 node->io_poll_end = io_poll_end;
291}
292
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200293void aio_set_event_notifier(AioContext *ctx,
294 EventNotifier *notifier,
Fam Zhengdca21ef2015-10-23 11:08:05 +0800295 bool is_external,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000296 EventNotifierHandler *io_read,
297 AioPollFn *io_poll)
Paolo Bonzini9958c352012-06-09 03:44:00 +0200298{
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000299 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), is_external,
300 (IOHandler *)io_read, NULL, io_poll, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200301}
302
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000303void aio_set_event_notifier_poll(AioContext *ctx,
304 EventNotifier *notifier,
305 EventNotifierHandler *io_poll_begin,
306 EventNotifierHandler *io_poll_end)
307{
308 aio_set_fd_poll(ctx, event_notifier_get_fd(notifier),
309 (IOHandler *)io_poll_begin,
310 (IOHandler *)io_poll_end);
311}
312
313static void poll_set_started(AioContext *ctx, bool started)
314{
315 AioHandler *node;
316
317 if (started == ctx->poll_started) {
318 return;
319 }
320
321 ctx->poll_started = started;
322
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100323 qemu_lockcnt_inc(&ctx->list_lock);
324 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000325 IOHandler *fn;
326
327 if (node->deleted) {
328 continue;
329 }
330
331 if (started) {
332 fn = node->io_poll_begin;
333 } else {
334 fn = node->io_poll_end;
335 }
336
337 if (fn) {
338 fn(node->opaque);
339 }
340 }
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100341 qemu_lockcnt_dec(&ctx->list_lock);
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000342}
343
344
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200345bool aio_prepare(AioContext *ctx)
346{
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000347 /* Poll mode cannot be used with glib's event loop, disable it. */
348 poll_set_started(ctx, false);
349
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200350 return false;
351}
352
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200353bool aio_pending(AioContext *ctx)
354{
355 AioHandler *node;
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100356 bool result = false;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200357
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100358 /*
359 * We have to walk very carefully in case aio_set_fd_handler is
360 * called while we're walking.
361 */
362 qemu_lockcnt_inc(&ctx->list_lock);
363
364 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200365 int revents;
366
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200367 revents = node->pfd.revents & node->pfd.events;
Fam Zheng37989ce2016-04-22 21:53:55 +0800368 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read &&
369 aio_node_check(ctx, node->is_external)) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100370 result = true;
371 break;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200372 }
Fam Zheng37989ce2016-04-22 21:53:55 +0800373 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write &&
374 aio_node_check(ctx, node->is_external)) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100375 result = true;
376 break;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200377 }
378 }
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100379 qemu_lockcnt_dec(&ctx->list_lock);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200380
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100381 return result;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200382}
383
Paolo Bonzini56d2c3c2017-01-12 19:07:55 +0100384static bool aio_dispatch_handlers(AioContext *ctx)
aliguoria76bab42008-09-22 19:17:18 +0000385{
Paolo Bonziniabf90d32017-01-12 19:07:56 +0100386 AioHandler *node, *tmp;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100387 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000388
Kevin Wolf8febfa22009-10-22 17:54:36 +0200389 /*
Paolo Bonzini87f68d32014-07-07 15:18:02 +0200390 * We have to walk very carefully in case aio_set_fd_handler is
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200391 * called while we're walking.
392 */
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100393 qemu_lockcnt_inc(&ctx->list_lock);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200394
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100395 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
Paolo Bonziniabf90d32017-01-12 19:07:56 +0100396 int revents;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200397
398 revents = node->pfd.revents & node->pfd.events;
399 node->pfd.revents = 0;
400
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100401 if (!node->deleted &&
402 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
Fam Zheng37989ce2016-04-22 21:53:55 +0800403 aio_node_check(ctx, node->is_external) &&
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100404 node->io_read) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200405 node->io_read(node->opaque);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200406
407 /* aio_notify() does not count as progress */
408 if (node->opaque != &ctx->notifier) {
409 progress = true;
410 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200411 }
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100412 if (!node->deleted &&
413 (revents & (G_IO_OUT | G_IO_ERR)) &&
Fam Zheng37989ce2016-04-22 21:53:55 +0800414 aio_node_check(ctx, node->is_external) &&
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100415 node->io_write) {
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200416 node->io_write(node->opaque);
417 progress = true;
418 }
419
Paolo Bonziniabf90d32017-01-12 19:07:56 +0100420 if (node->deleted) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100421 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
Paolo Bonziniabf90d32017-01-12 19:07:56 +0100422 QLIST_REMOVE(node, node);
423 g_free(node);
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100424 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
Paolo Bonziniabf90d32017-01-12 19:07:56 +0100425 }
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200426 }
427 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100428
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100429 qemu_lockcnt_dec(&ctx->list_lock);
Paolo Bonzini56d2c3c2017-01-12 19:07:55 +0100430 return progress;
431}
432
433/*
434 * Note that dispatch_fds == false has the side-effect of post-poning the
435 * freeing of deleted handlers.
436 */
437bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
438{
439 bool progress;
440
441 /*
442 * If there are callbacks left that have been queued, we need to call them.
443 * Do not call select in this case, because it is possible that the caller
444 * does not need a complete flush (as is the case for aio_poll loops).
445 */
446 progress = aio_bh_poll(ctx);
447
448 if (dispatch_fds) {
449 progress |= aio_dispatch_handlers(ctx);
450 }
451
Alex Bligh438e1f42013-08-21 16:02:53 +0100452 /* Run our timers */
453 progress |= timerlistgroup_run_timers(&ctx->tlg);
454
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100455 return progress;
456}
457
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100458/* These thread-local variables are used only in a small part of aio_poll
459 * around the call to the poll() system call. In particular they are not
460 * used while aio_poll is performing callbacks, which makes it much easier
461 * to think about reentrancy!
462 *
463 * Stack-allocated arrays would be perfect but they have size limitations;
464 * heap allocation is expensive enough that we want to reuse arrays across
465 * calls to aio_poll(). And because poll() has to be called without holding
466 * any lock, the arrays cannot be stored in AioContext. Thread-local data
467 * has none of the disadvantages of these three options.
468 */
469static __thread GPollFD *pollfds;
470static __thread AioHandler **nodes;
471static __thread unsigned npfd, nalloc;
472static __thread Notifier pollfds_cleanup_notifier;
473
474static void pollfds_cleanup(Notifier *n, void *unused)
475{
476 g_assert(npfd == 0);
477 g_free(pollfds);
478 g_free(nodes);
479 nalloc = 0;
480}
481
482static void add_pollfd(AioHandler *node)
483{
484 if (npfd == nalloc) {
485 if (nalloc == 0) {
486 pollfds_cleanup_notifier.notify = pollfds_cleanup;
487 qemu_thread_atexit_add(&pollfds_cleanup_notifier);
488 nalloc = 8;
489 } else {
490 g_assert(nalloc <= INT_MAX);
491 nalloc *= 2;
492 }
493 pollfds = g_renew(GPollFD, pollfds, nalloc);
494 nodes = g_renew(AioHandler *, nodes, nalloc);
495 }
496 nodes[npfd] = node;
497 pollfds[npfd] = (GPollFD) {
498 .fd = node->pfd.fd,
499 .events = node->pfd.events,
500 };
501 npfd++;
502}
503
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000504static bool run_poll_handlers_once(AioContext *ctx)
505{
506 bool progress = false;
507 AioHandler *node;
508
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100509 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000510 if (!node->deleted && node->io_poll &&
Stefan Hajnoczi59c9f432017-01-24 09:53:50 +0000511 aio_node_check(ctx, node->is_external) &&
512 node->io_poll(node->opaque)) {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000513 progress = true;
514 }
515
516 /* Caller handles freeing deleted nodes. Don't do it here. */
517 }
518
519 return progress;
520}
521
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000522/* run_poll_handlers:
523 * @ctx: the AioContext
524 * @max_ns: maximum time to poll for, in nanoseconds
525 *
526 * Polls for a given time.
527 *
528 * Note that ctx->notify_me must be non-zero so this function can detect
529 * aio_notify().
530 *
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100531 * Note that the caller must have incremented ctx->list_lock.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000532 *
533 * Returns: true if progress was made, false otherwise
534 */
535static bool run_poll_handlers(AioContext *ctx, int64_t max_ns)
536{
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000537 bool progress;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000538 int64_t end_time;
539
540 assert(ctx->notify_me);
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100541 assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000542 assert(ctx->poll_disable_cnt == 0);
543
544 trace_run_poll_handlers_begin(ctx, max_ns);
545
546 end_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + max_ns;
547
548 do {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000549 progress = run_poll_handlers_once(ctx);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000550 } while (!progress && qemu_clock_get_ns(QEMU_CLOCK_REALTIME) < end_time);
551
552 trace_run_poll_handlers_end(ctx, progress);
553
554 return progress;
555}
556
557/* try_poll_mode:
558 * @ctx: the AioContext
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000559 * @blocking: busy polling is only attempted when blocking is true
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000560 *
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000561 * ctx->notify_me must be non-zero so this function can detect aio_notify().
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000562 *
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100563 * Note that the caller must have incremented ctx->list_lock.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000564 *
565 * Returns: true if progress was made, false otherwise
566 */
567static bool try_poll_mode(AioContext *ctx, bool blocking)
568{
569 if (blocking && ctx->poll_max_ns && ctx->poll_disable_cnt == 0) {
570 /* See qemu_soonest_timeout() uint64_t hack */
571 int64_t max_ns = MIN((uint64_t)aio_compute_timeout(ctx),
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000572 (uint64_t)ctx->poll_ns);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000573
574 if (max_ns) {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000575 poll_set_started(ctx, true);
576
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000577 if (run_poll_handlers(ctx, max_ns)) {
578 return true;
579 }
580 }
581 }
582
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000583 poll_set_started(ctx, false);
584
585 /* Even if we don't run busy polling, try polling once in case it can make
586 * progress and the caller will be able to avoid ppoll(2)/epoll_wait(2).
587 */
588 return run_poll_handlers_once(ctx);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000589}
590
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100591bool aio_poll(AioContext *ctx, bool blocking)
592{
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100593 AioHandler *node;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000594 int i;
595 int ret = 0;
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200596 bool progress;
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100597 int64_t timeout;
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000598 int64_t start = 0;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100599
Paolo Bonzini49110172015-02-20 17:26:51 +0100600 aio_context_acquire(ctx);
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100601 progress = false;
602
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200603 /* aio_notify can avoid the expensive event_notifier_set if
604 * everything (file descriptors, bottom halves, timers) will
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200605 * be re-evaluated before the next blocking poll(). This is
606 * already true when aio_poll is called with blocking == false;
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200607 * if blocking == true, it is only true after poll() returns,
608 * so disable the optimization now.
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200609 */
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200610 if (blocking) {
611 atomic_add(&ctx->notify_me, 2);
612 }
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200613
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100614 qemu_lockcnt_inc(&ctx->list_lock);
aliguoria76bab42008-09-22 19:17:18 +0000615
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000616 if (ctx->poll_max_ns) {
617 start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
618 }
619
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000620 if (try_poll_mode(ctx, blocking)) {
621 progress = true;
622 } else {
623 assert(npfd == 0);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200624
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000625 /* fill pollfds */
Yaowei Bai6b942462016-09-14 07:03:39 -0400626
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000627 if (!aio_epoll_enabled(ctx)) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100628 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000629 if (!node->deleted && node->pfd.events
630 && aio_node_check(ctx, node->is_external)) {
631 add_pollfd(node);
632 }
Yaowei Bai6b942462016-09-14 07:03:39 -0400633 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200634 }
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000635
636 timeout = blocking ? aio_compute_timeout(ctx) : 0;
637
638 /* wait until next event */
639 if (timeout) {
640 aio_context_release(ctx);
641 }
642 if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) {
643 AioHandler epoll_handler;
644
645 epoll_handler.pfd.fd = ctx->epollfd;
646 epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
647 npfd = 0;
648 add_pollfd(&epoll_handler);
649 ret = aio_epoll(ctx, pollfds, npfd, timeout);
650 } else {
651 ret = qemu_poll_ns(pollfds, npfd, timeout);
652 }
653 if (timeout) {
654 aio_context_acquire(ctx);
655 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200656 }
657
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200658 if (blocking) {
659 atomic_sub(&ctx->notify_me, 2);
660 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200661
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000662 /* Adjust polling time */
663 if (ctx->poll_max_ns) {
664 int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start;
665
666 if (block_ns <= ctx->poll_ns) {
667 /* This is the sweet spot, no adjustment needed */
668 } else if (block_ns > ctx->poll_max_ns) {
669 /* We'd have to poll for too long, poll less */
670 int64_t old = ctx->poll_ns;
671
672 if (ctx->poll_shrink) {
673 ctx->poll_ns /= ctx->poll_shrink;
674 } else {
675 ctx->poll_ns = 0;
676 }
677
678 trace_poll_shrink(ctx, old, ctx->poll_ns);
679 } else if (ctx->poll_ns < ctx->poll_max_ns &&
680 block_ns < ctx->poll_max_ns) {
681 /* There is room to grow, poll longer */
682 int64_t old = ctx->poll_ns;
683 int64_t grow = ctx->poll_grow;
684
685 if (grow == 0) {
686 grow = 2;
687 }
688
689 if (ctx->poll_ns) {
690 ctx->poll_ns *= grow;
691 } else {
692 ctx->poll_ns = 4000; /* start polling at 4 microseconds */
693 }
694
695 if (ctx->poll_ns > ctx->poll_max_ns) {
696 ctx->poll_ns = ctx->poll_max_ns;
697 }
698
699 trace_poll_grow(ctx, old, ctx->poll_ns);
700 }
701 }
702
Paolo Bonzini05e514b2015-07-21 16:07:53 +0200703 aio_notify_accept(ctx);
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200704
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200705 /* if we have any readable fds, dispatch event */
706 if (ret > 0) {
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100707 for (i = 0; i < npfd; i++) {
708 nodes[i]->pfd.revents = pollfds[i].revents;
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100709 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100710 }
711
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100712 npfd = 0;
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100713 qemu_lockcnt_dec(&ctx->list_lock);
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100714
Alex Bligh438e1f42013-08-21 16:02:53 +0100715 /* Run dispatch even if there were no readable fds to run timers */
Stefan Hajnoczi721671a2016-12-01 19:26:40 +0000716 if (aio_dispatch(ctx, ret > 0)) {
Alex Bligh438e1f42013-08-21 16:02:53 +0100717 progress = true;
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200718 }
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200719
Paolo Bonzini49110172015-02-20 17:26:51 +0100720 aio_context_release(ctx);
721
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200722 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000723}
Fam Zheng37fcee52015-10-30 12:06:28 +0800724
Cao jin7e003462016-07-15 18:28:44 +0800725void aio_context_setup(AioContext *ctx)
Fam Zheng37fcee52015-10-30 12:06:28 +0800726{
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000727 /* TODO remove this in final patch submission */
728 if (getenv("QEMU_AIO_POLL_MAX_NS")) {
729 fprintf(stderr, "The QEMU_AIO_POLL_MAX_NS environment variable has "
730 "been replaced with -object iothread,poll-max-ns=NUM\n");
731 exit(1);
732 }
733
Matthew Fortune147dfab2016-02-23 15:42:14 +0000734#ifdef CONFIG_EPOLL_CREATE1
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800735 assert(!ctx->epollfd);
736 ctx->epollfd = epoll_create1(EPOLL_CLOEXEC);
737 if (ctx->epollfd == -1) {
Cao jin7e003462016-07-15 18:28:44 +0800738 fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno));
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800739 ctx->epoll_available = false;
740 } else {
741 ctx->epoll_available = true;
742 }
743#endif
Fam Zheng37fcee52015-10-30 12:06:28 +0800744}
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000745
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000746void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
747 int64_t grow, int64_t shrink, Error **errp)
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000748{
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000749 /* No thread synchronization here, it doesn't matter if an incorrect value
750 * is used once.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000751 */
752 ctx->poll_max_ns = max_ns;
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000753 ctx->poll_ns = 0;
754 ctx->poll_grow = grow;
755 ctx->poll_shrink = shrink;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000756
757 aio_notify(ctx);
758}