blob: 7f2c99729d4475640b7c6b21977123a8e6f97bae [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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010017#include "block/block.h"
Nicolas Saenz Julienne71ad4712022-04-25 09:57:23 +020018#include "block/thread-pool.h"
Kevin Wolf9ce44e22020-10-05 17:58:50 +020019#include "qemu/main-loop.h"
Stefan Hajnoczif25c0b52020-02-18 18:27:08 +000020#include "qemu/rcu.h"
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +010021#include "qemu/rcu_queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/sockets.h"
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +000023#include "qemu/cutils.h"
Paolo Bonzinic2b38b22017-02-13 14:52:18 +010024#include "trace.h"
Stefan Hajnoczi1f050a42020-03-05 17:08:02 +000025#include "aio-posix.h"
aliguoria76bab42008-09-22 19:17:18 +000026
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +000027/* 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 Hajnocziaa38e192020-03-05 17:08:05 +000030bool aio_poll_disabled(AioContext *ctx)
31{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +010032 return qatomic_read(&ctx->poll_disable_cnt);
Stefan Hajnocziaa38e192020-03-05 17:08:05 +000033}
34
Stefan Hajnoczi1f050a42020-03-05 17:08:02 +000035void aio_add_ready_handler(AioHandlerList *ready_list,
36 AioHandler *node,
37 int revents)
Stefan Hajnoczi7391d342020-02-14 17:17:12 +000038{
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 Hajnoczifc879642022-02-23 15:57:03 +000044static 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 Bonzinia915f4b2012-09-13 12:28:51 +020052static AioHandler *find_aio_handler(AioContext *ctx, int fd)
aliguoria76bab42008-09-22 19:17:18 +000053{
54 AioHandler *node;
55
Paolo Bonzinia915f4b2012-09-13 12:28:51 +020056 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
Stefan Hajnoczi47490792020-02-14 17:17:11 +000057 if (node->pfd.fd == fd) {
58 if (!QLIST_IS_INSERTED(node, node_deleted)) {
Alexander Graf79d5ca52009-05-06 02:58:48 +020059 return node;
Stefan Hajnoczi47490792020-02-14 17:17:11 +000060 }
61 }
aliguoria76bab42008-09-22 19:17:18 +000062 }
63
64 return NULL;
65}
66
Remy Noelfef16602018-12-20 16:20:30 +010067static 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 Hajnoczi73fd2822020-03-05 17:08:04 +000078 node->pfd.revents = 0;
Stefan Hajnoczifc879642022-02-23 15:57:03 +000079 node->poll_ready = false;
Stefan Hajnoczi73fd2822020-03-05 17:08:04 +000080
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 Noelfef16602018-12-20 16:20:30 +010086 /* If a read is in progress, just mark the node as deleted */
87 if (qemu_lockcnt_count(&ctx->list_lock)) {
Stefan Hajnoczi47490792020-02-14 17:17:11 +000088 QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_deleted);
Remy Noelfef16602018-12-20 16:20:30 +010089 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 Hajnoczid37d0e32020-03-05 17:08:06 +000095 QLIST_SAFE_REMOVE(node, node_poll);
Remy Noelfef16602018-12-20 16:20:30 +010096 QLIST_REMOVE(node, node);
97 return true;
98}
99
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200100void aio_set_fd_handler(AioContext *ctx,
101 int fd,
102 IOHandler *io_read,
103 IOHandler *io_write,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000104 AioPollFn *io_poll,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000105 IOHandler *io_poll_ready,
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200106 void *opaque)
aliguoria76bab42008-09-22 19:17:18 +0000107{
108 AioHandler *node;
Remy Noelfef16602018-12-20 16:20:30 +0100109 AioHandler *new_node = NULL;
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800110 bool is_new = false;
Fam Zheng0ed39f3d2015-11-16 14:32:14 +0800111 bool deleted = false;
Paolo Bonzinid7be5dd2018-09-12 19:10:38 +0200112 int poll_disable_change;
aliguoria76bab42008-09-22 19:17:18 +0000113
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000114 if (io_poll && !io_poll_ready) {
115 io_poll = NULL; /* polling only makes sense if there is a handler */
116 }
117
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100118 qemu_lockcnt_lock(&ctx->list_lock);
119
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200120 node = find_aio_handler(ctx, fd);
aliguoria76bab42008-09-22 19:17:18 +0000121
122 /* Are we deleting the fd handler? */
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000123 if (!io_read && !io_write && !io_poll) {
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100124 if (node == NULL) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100125 qemu_lockcnt_unlock(&ctx->list_lock);
Paolo Bonzini36173ec2016-11-08 14:55:23 +0100126 return;
127 }
Remy Noel8821b342018-12-20 16:20:29 +0100128 /* Clean events in order to unregister fd from the ctx epoll. */
129 node->pfd.events = 0;
130
Paolo Bonzinid7be5dd2018-09-12 19:10:38 +0200131 poll_disable_change = -!node->io_poll;
aliguoria76bab42008-09-22 19:17:18 +0000132 } else {
Paolo Bonzinid7be5dd2018-09-12 19:10:38 +0200133 poll_disable_change = !io_poll - (node && !node->io_poll);
aliguoria76bab42008-09-22 19:17:18 +0000134 if (node == NULL) {
Fam Zhengfbe3fc52015-10-30 12:06:29 +0800135 is_new = true;
aliguoria76bab42008-09-22 19:17:18 +0000136 }
Remy Noelfef16602018-12-20 16:20:30 +0100137 /* Alloc and insert if it's not already there */
138 new_node = g_new0(AioHandler, 1);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000139
aliguoria76bab42008-09-22 19:17:18 +0000140 /* Update handler with latest information */
Remy Noelfef16602018-12-20 16:20:30 +0100141 new_node->io_read = io_read;
142 new_node->io_write = io_write;
143 new_node->io_poll = io_poll;
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000144 new_node->io_poll_ready = io_poll_ready;
Remy Noelfef16602018-12-20 16:20:30 +0100145 new_node->opaque = opaque;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200146
Remy Noelfef16602018-12-20 16:20:30 +0100147 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 Bonzini7ed2b242012-09-25 10:22:39 +0200159
Paolo Bonzinid7be5dd2018-09-12 19:10:38 +0200160 /* 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 Hajnoczid73415a2020-09-23 11:56:46 +0100166 qatomic_set(&ctx->poll_disable_cnt,
167 qatomic_read(&ctx->poll_disable_cnt) + poll_disable_change);
Paolo Bonzinid7be5dd2018-09-12 19:10:38 +0200168
Stefan Hajnoczib3210512020-03-05 17:08:03 +0000169 ctx->fdmon_ops->update(ctx, node, new_node);
Stefan Hajnoczi73fd2822020-03-05 17:08:04 +0000170 if (node) {
171 deleted = aio_remove_fd_handler(ctx, node);
172 }
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100173 qemu_lockcnt_unlock(&ctx->list_lock);
Paolo Bonzini7ed2b242012-09-25 10:22:39 +0200174 aio_notify(ctx);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000175
Fam Zheng0ed39f3d2015-11-16 14:32:14 +0800176 if (deleted) {
177 g_free(node);
178 }
aliguoria76bab42008-09-22 19:17:18 +0000179}
180
Marc-André Lureau6eeef442023-02-21 16:47:53 +0400181static void aio_set_fd_poll(AioContext *ctx, int fd,
182 IOHandler *io_poll_begin,
183 IOHandler *io_poll_end)
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000184{
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 Bonzinia915f4b2012-09-13 12:28:51 +0200195void aio_set_event_notifier(AioContext *ctx,
196 EventNotifier *notifier,
Stefan Hajnoczif6a51c82016-12-01 19:26:41 +0000197 EventNotifierHandler *io_read,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000198 AioPollFn *io_poll,
199 EventNotifierHandler *io_poll_ready)
Paolo Bonzini9958c352012-06-09 03:44:00 +0200200{
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400201 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000202 (IOHandler *)io_read, NULL, io_poll,
203 (IOHandler *)io_poll_ready, notifier);
Paolo Bonzini9958c352012-06-09 03:44:00 +0200204}
205
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000206void 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 Hajnoczi826cc322021-12-07 13:23:31 +0000216static bool poll_set_started(AioContext *ctx, AioHandlerList *ready_list,
217 bool started)
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000218{
219 AioHandler *node;
Stefan Hajnoczie4346192020-03-05 17:08:00 +0000220 bool progress = false;
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000221
222 if (started == ctx->poll_started) {
Stefan Hajnoczie4346192020-03-05 17:08:00 +0000223 return false;
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000224 }
225
226 ctx->poll_started = started;
227
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100228 qemu_lockcnt_inc(&ctx->list_lock);
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000229 QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000230 IOHandler *fn;
231
Stefan Hajnoczi47490792020-02-14 17:17:11 +0000232 if (QLIST_IS_INSERTED(node, node_deleted)) {
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000233 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 Hajnoczie4346192020-03-05 17:08:00 +0000245
246 /* Poll one last time in case ->io_poll_end() raced with the event */
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000247 if (!started && node->io_poll(node->opaque)) {
Stefan Hajnoczifc879642022-02-23 15:57:03 +0000248 aio_add_poll_ready_handler(ready_list, node);
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000249 progress = true;
Stefan Hajnoczie4346192020-03-05 17:08:00 +0000250 }
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000251 }
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100252 qemu_lockcnt_dec(&ctx->list_lock);
Stefan Hajnoczie4346192020-03-05 17:08:00 +0000253
254 return progress;
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000255}
256
257
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200258bool aio_prepare(AioContext *ctx)
259{
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000260 AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
261
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000262 /* Poll mode cannot be used with glib's event loop, disable it. */
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000263 poll_set_started(ctx, &ready_list, false);
264 /* TODO what to do with this list? */
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000265
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200266 return false;
267}
268
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200269bool aio_pending(AioContext *ctx)
270{
271 AioHandler *node;
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100272 bool result = false;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200273
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100274 /*
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 Bonzinicd9ba1e2012-09-24 14:57:22 +0200281 int revents;
282
Stefan Hajnoczifc879642022-02-23 15:57:03 +0000283 /* TODO should this check poll ready? */
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200284 revents = node->pfd.revents & node->pfd.events;
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400285 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100286 result = true;
287 break;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200288 }
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400289 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100290 result = true;
291 break;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200292 }
293 }
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100294 qemu_lockcnt_dec(&ctx->list_lock);
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200295
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100296 return result;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200297}
298
Stefan Hajnoczi47490792020-02-14 17:17:11 +0000299static 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 Hajnoczid37d0e32020-03-05 17:08:06 +0000313 QLIST_SAFE_REMOVE(node, node_poll);
Stefan Hajnoczi47490792020-02-14 17:17:11 +0000314 g_free(node);
315 }
316
317 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
318}
319
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000320static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node)
321{
322 bool progress = false;
Stefan Hajnoczifc879642022-02-23 15:57:03 +0000323 bool poll_ready;
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000324 int revents;
325
326 revents = node->pfd.revents & node->pfd.events;
327 node->pfd.revents = 0;
328
Stefan Hajnoczifc879642022-02-23 15:57:03 +0000329 poll_ready = node->poll_ready;
330 node->poll_ready = false;
331
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000332 /*
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 Hajnoczi826cc322021-12-07 13:23:31 +0000347 if (!QLIST_IS_INSERTED(node, node_deleted) &&
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400348 poll_ready && revents == 0 && node->io_poll_ready) {
Stefan Hajnoczi6d740fb2023-05-02 14:41:33 -0400349 /*
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 Hajnoczi826cc322021-12-07 13:23:31 +0000356 node->io_poll_ready(node->opaque);
357
Stefan Hajnoczi6d740fb2023-05-02 14:41:33 -0400358 if (!QLIST_IS_INSERTED(node, node_poll)) {
359 QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll);
360 }
361
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000362 /*
363 * Return early since revents was zero. aio_notify() does not count as
364 * progress.
365 */
366 return node->opaque != &ctx->notifier;
367 }
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000368
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000369 if (!QLIST_IS_INSERTED(node, node_deleted) &&
370 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000371 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 Hajnoczi7391d342020-02-14 17:17:12 +0000381 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 */
393static 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 Hajnoczic39cbed2020-02-24 10:34:06 +0000400 QLIST_REMOVE(node, node_ready);
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000401 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 Bonzini56d2c3c2017-01-12 19:07:55 +0100408static bool aio_dispatch_handlers(AioContext *ctx)
aliguoria76bab42008-09-22 19:17:18 +0000409{
Paolo Bonziniabf90d32017-01-12 19:07:56 +0100410 AioHandler *node, *tmp;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100411 bool progress = false;
aliguoria76bab42008-09-22 19:17:18 +0000412
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100413 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000414 progress = aio_dispatch_handler(ctx, node) || progress;
Paolo Bonzinicd9ba1e2012-09-24 14:57:22 +0200415 }
Alex Bligh438e1f42013-08-21 16:02:53 +0100416
Paolo Bonzini56d2c3c2017-01-12 19:07:55 +0100417 return progress;
418}
419
Paolo Bonzinia153bf52017-02-13 14:52:33 +0100420void aio_dispatch(AioContext *ctx)
Paolo Bonzini56d2c3c2017-01-12 19:07:55 +0100421{
Paolo Bonzinia153bf52017-02-13 14:52:33 +0100422 qemu_lockcnt_inc(&ctx->list_lock);
Paolo Bonzinibd451432017-02-13 14:52:34 +0100423 aio_bh_poll(ctx);
Paolo Bonzinia153bf52017-02-13 14:52:33 +0100424 aio_dispatch_handlers(ctx);
Stefan Hajnoczi47490792020-02-14 17:17:11 +0000425 aio_free_deleted_handlers(ctx);
Paolo Bonzinia153bf52017-02-13 14:52:33 +0100426 qemu_lockcnt_dec(&ctx->list_lock);
Paolo Bonzini56d2c3c2017-01-12 19:07:55 +0100427
Paolo Bonzinia153bf52017-02-13 14:52:33 +0100428 timerlistgroup_run_timers(&ctx->tlg);
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100429}
430
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000431static bool run_poll_handlers_once(AioContext *ctx,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000432 AioHandlerList *ready_list,
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000433 int64_t now,
434 int64_t *timeout)
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000435{
436 bool progress = false;
437 AioHandler *node;
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000438 AioHandler *tmp;
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000439
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000440 QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) {
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400441 if (node->io_poll(node->opaque)) {
Stefan Hajnoczifc879642022-02-23 15:57:03 +0000442 aio_add_poll_ready_handler(ready_list, node);
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000443
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000444 node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS;
445
Paolo Bonzini993ed892019-04-09 14:28:23 +0200446 /*
447 * Polling was successful, exit try_poll_mode immediately
448 * to adjust the next polling time.
449 */
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200450 *timeout = 0;
Paolo Bonzinicfeb35d2018-09-12 19:10:40 +0200451 if (node->opaque != &ctx->notifier) {
452 progress = true;
453 }
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000454 }
455
456 /* Caller handles freeing deleted nodes. Don't do it here. */
457 }
458
459 return progress;
460}
461
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000462static bool fdmon_supports_polling(AioContext *ctx)
463{
464 return ctx->fdmon_ops->need_wait != aio_poll_disabled;
465}
466
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000467static bool remove_idle_poll_handlers(AioContext *ctx,
468 AioHandlerList *ready_list,
469 int64_t now)
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000470{
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 Hajnoczi826cc322021-12-07 13:23:31 +0000500 if (node->io_poll(node->opaque)) {
Stefan Hajnoczifc879642022-02-23 15:57:03 +0000501 aio_add_poll_ready_handler(ready_list, node);
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000502 progress = true;
503 }
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000504 }
505 }
506 }
507
508 return progress;
509}
510
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000511/* run_poll_handlers:
512 * @ctx: the AioContext
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000513 * @ready_list: the list to place ready handlers on
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000514 * @max_ns: maximum time to poll for, in nanoseconds
515 *
516 * Polls for a given time.
517 *
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100518 * Note that the caller must have incremented ctx->list_lock.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000519 *
520 * Returns: true if progress was made, false otherwise
521 */
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000522static bool run_poll_handlers(AioContext *ctx, AioHandlerList *ready_list,
523 int64_t max_ns, int64_t *timeout)
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000524{
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000525 bool progress;
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200526 int64_t start_time, elapsed_time;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000527
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100528 assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000529
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200530 trace_run_poll_handlers_begin(ctx, max_ns, *timeout);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000531
Stefan Hajnoczi3aa221b2020-03-05 17:08:01 +0000532 /*
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 Bonzinie30cffa2018-09-12 19:10:39 +0200542 start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000543 do {
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000544 progress = run_poll_handlers_once(ctx, ready_list,
545 start_time, timeout);
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200546 elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time;
Paolo Bonzini993ed892019-04-09 14:28:23 +0200547 max_ns = qemu_soonest_timeout(*timeout, max_ns);
548 assert(!(max_ns && progress));
Stefan Hajnocziaa38e192020-03-05 17:08:05 +0000549 } while (elapsed_time < max_ns && !ctx->fdmon_ops->need_wait(ctx));
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000550
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000551 if (remove_idle_poll_handlers(ctx, ready_list,
552 start_time + elapsed_time)) {
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000553 *timeout = 0;
554 progress = true;
555 }
556
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200557 /* 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 Hajnoczi4a1cba32016-12-01 19:26:42 +0000563
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200564 trace_run_poll_handlers_end(ctx, progress, *timeout);
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000565 return progress;
566}
567
568/* try_poll_mode:
569 * @ctx: the AioContext
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000570 * @ready_list: list to add handlers that need to be run
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200571 * @timeout: timeout for blocking wait, computed by the caller and updated if
572 * polling succeeds.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000573 *
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100574 * Note that the caller must have incremented ctx->list_lock.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000575 *
576 * Returns: true if progress was made, false otherwise
577 */
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000578static bool try_poll_mode(AioContext *ctx, AioHandlerList *ready_list,
579 int64_t *timeout)
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000580{
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000581 int64_t max_ns;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000582
Stefan Hajnoczid37d0e32020-03-05 17:08:06 +0000583 if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
584 return false;
585 }
586
587 max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
Stefan Hajnocziaa38e192020-03-05 17:08:05 +0000588 if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
Chao Gao816a4302022-07-10 20:08:49 +0800589 /*
590 * Enable poll mode. It pairs with the poll_set_started() in
591 * aio_poll() which disables poll mode.
592 */
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000593 poll_set_started(ctx, ready_list, true);
Stefan Hajnoczi684e5082016-12-01 19:26:49 +0000594
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000595 if (run_poll_handlers(ctx, ready_list, max_ns, timeout)) {
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200596 return true;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000597 }
598 }
Stefan Hajnoczie4346192020-03-05 17:08:00 +0000599 return false;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000600}
601
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100602bool aio_poll(AioContext *ctx, bool blocking)
603{
Stefan Hajnoczi7391d342020-02-14 17:17:12 +0000604 AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200605 bool progress;
Stefan Hajnoczi44277bf2020-08-06 14:18:02 +0100606 bool use_notify_me;
Paolo Bonzinie98ab092015-02-20 17:26:50 +0100607 int64_t timeout;
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000608 int64_t start = 0;
Stefan Hajnoczid0c8d2c2013-02-20 11:28:31 +0100609
Paolo Bonzini5710a3e2020-04-07 10:07:46 -0400610 /*
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 Wolf9ce44e22020-10-05 17:58:50 +0200614 *
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 Bonzini5710a3e2020-04-07 10:07:46 -0400618 */
Kevin Wolf9ce44e22020-10-05 17:58:50 +0200619 assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ?
620 qemu_get_aio_context() : ctx));
Kevin Wolf0dc165c2019-02-14 13:13:36 +0100621
Paolo Bonzini2bbf11d2017-01-12 19:07:57 +0100622 qemu_lockcnt_inc(&ctx->list_lock);
aliguoria76bab42008-09-22 19:17:18 +0000623
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000624 if (ctx->poll_max_ns) {
625 start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
626 }
627
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200628 timeout = blocking ? aio_compute_timeout(ctx) : 0;
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000629 progress = try_poll_mode(ctx, &ready_list, &timeout);
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200630 assert(!(timeout && progress));
631
Stefan Hajnoczi44277bf2020-08-06 14:18:02 +0100632 /*
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 Hajnoczid73415a2020-09-23 11:56:46 +0100642 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2);
Stefan Hajnoczi44277bf2020-08-06 14:18:02 +0100643 /*
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 Hajnoczid73415a2020-09-23 11:56:46 +0100650 if (qatomic_read(&ctx->notified)) {
Stefan Hajnoczi44277bf2020-08-06 14:18:02 +0100651 timeout = 0;
652 }
653 }
654
Paolo Bonzinie30cffa2018-09-12 19:10:39 +0200655 /* 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 Hajnocziaa38e192020-03-05 17:08:05 +0000658 if (timeout || ctx->fdmon_ops->need_wait(ctx)) {
Chao Gao816a4302022-07-10 20:08:49 +0800659 /*
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 Hajnoczi826cc322021-12-07 13:23:31 +0000670 ctx->fdmon_ops->wait(ctx, &ready_list, timeout);
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200671 }
672
Stefan Hajnoczi44277bf2020-08-06 14:18:02 +0100673 if (use_notify_me) {
Paolo Bonzini5710a3e2020-04-07 10:07:46 -0400674 /* Finish the poll before clearing the flag. */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100675 qatomic_store_release(&ctx->notify_me,
676 qatomic_read(&ctx->notify_me) - 2);
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200677 }
Paolo Bonzini9eb0bfc2012-04-12 14:00:56 +0200678
Stefan Hajnoczi44277bf2020-08-06 14:18:02 +0100679 aio_notify_accept(ctx);
680
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000681 /* 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 Bonzinia153bf52017-02-13 14:52:33 +0100722 progress |= aio_bh_poll(ctx);
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000723 progress |= aio_dispatch_ready_handlers(ctx, &ready_list);
Paolo Bonzinibcdc1852012-04-12 14:00:55 +0200724
Stefan Hajnoczi47490792020-02-14 17:17:11 +0000725 aio_free_deleted_handlers(ctx);
726
Paolo Bonzinibd451432017-02-13 14:52:34 +0100727 qemu_lockcnt_dec(&ctx->list_lock);
728
Paolo Bonzinia153bf52017-02-13 14:52:33 +0100729 progress |= timerlistgroup_run_timers(&ctx->tlg);
730
Stefan Hajnoczi164a1012013-04-11 16:56:50 +0200731 return progress;
aliguoria76bab42008-09-22 19:17:18 +0000732}
Fam Zheng37fcee52015-10-30 12:06:28 +0800733
Cao jin7e003462016-07-15 18:28:44 +0800734void aio_context_setup(AioContext *ctx)
Fam Zheng37fcee52015-10-30 12:06:28 +0800735{
Stefan Hajnoczi1f050a42020-03-05 17:08:02 +0000736 ctx->fdmon_ops = &fdmon_poll_ops;
737 ctx->epollfd = -1;
738
Stefan Hajnoczi73fd2822020-03-05 17:08:04 +0000739 /* Use the fastest fd monitoring implementation if available */
740 if (fdmon_io_uring_setup(ctx)) {
741 return;
742 }
743
Stefan Hajnoczi1f050a42020-03-05 17:08:02 +0000744 fdmon_epoll_setup(ctx);
Fam Zheng37fcee52015-10-30 12:06:28 +0800745}
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000746
Jie Wangcd0a6d22018-05-17 08:42:43 +0800747void aio_context_destroy(AioContext *ctx)
748{
Stefan Hajnoczi73fd2822020-03-05 17:08:04 +0000749 fdmon_io_uring_destroy(ctx);
Stefan Hajnoczi1f050a42020-03-05 17:08:02 +0000750 fdmon_epoll_disable(ctx);
Stefan Hajnoczide137e42020-05-11 19:36:29 +0100751 aio_free_deleted_handlers(ctx);
Jie Wangcd0a6d22018-05-17 08:42:43 +0800752}
753
Stefan Hajnocziba607ca2020-05-11 19:36:30 +0100754void 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 Hajnoczi82a41182016-12-01 19:26:51 +0000766void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
767 int64_t grow, int64_t shrink, Error **errp)
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000768{
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000769 /* No thread synchronization here, it doesn't matter if an incorrect value
770 * is used once.
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000771 */
772 ctx->poll_max_ns = max_ns;
Stefan Hajnoczi82a41182016-12-01 19:26:51 +0000773 ctx->poll_ns = 0;
774 ctx->poll_grow = grow;
775 ctx->poll_shrink = shrink;
Stefan Hajnoczi4a1cba32016-12-01 19:26:42 +0000776
777 aio_notify(ctx);
778}
Stefano Garzarella1793ad02021-07-21 11:42:10 +0200779
780void 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}