]>
Commit | Line | Data |
---|---|---|
a76bab49 AL |
1 | /* |
2 | * QEMU aio implementation | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | * | |
6b620ca3 PB |
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. | |
a76bab49 AL |
14 | */ |
15 | ||
d38ea87a | 16 | #include "qemu/osdep.h" |
a76bab49 | 17 | #include "qemu-common.h" |
737e150e | 18 | #include "block/block.h" |
2bbf11d7 | 19 | #include "qemu/rcu_queue.h" |
1de7afc9 | 20 | #include "qemu/sockets.h" |
4a1cba38 | 21 | #include "qemu/cutils.h" |
c2b38b27 | 22 | #include "trace.h" |
147dfab7 | 23 | #ifdef CONFIG_EPOLL_CREATE1 |
fbe3fc5c FZ |
24 | #include <sys/epoll.h> |
25 | #endif | |
a76bab49 | 26 | |
a76bab49 AL |
27 | struct AioHandler |
28 | { | |
cd9ba1eb | 29 | GPollFD pfd; |
a76bab49 AL |
30 | IOHandler *io_read; |
31 | IOHandler *io_write; | |
4a1cba38 | 32 | AioPollFn *io_poll; |
684e508c SH |
33 | IOHandler *io_poll_begin; |
34 | IOHandler *io_poll_end; | |
a76bab49 AL |
35 | int deleted; |
36 | void *opaque; | |
dca21ef2 | 37 | bool is_external; |
72cf2d4f | 38 | QLIST_ENTRY(AioHandler) node; |
a76bab49 AL |
39 | }; |
40 | ||
147dfab7 | 41 | #ifdef CONFIG_EPOLL_CREATE1 |
fbe3fc5c | 42 | |
82dfee5a | 43 | /* The fd number threshold to switch to epoll */ |
fbe3fc5c FZ |
44 | #define EPOLL_ENABLE_THRESHOLD 64 |
45 | ||
46 | static void aio_epoll_disable(AioContext *ctx) | |
47 | { | |
cd0a6d2b JW |
48 | ctx->epoll_enabled = false; |
49 | if (!ctx->epoll_available) { | |
fbe3fc5c FZ |
50 | return; |
51 | } | |
cd0a6d2b | 52 | ctx->epoll_available = false; |
fbe3fc5c FZ |
53 | close(ctx->epollfd); |
54 | } | |
55 | ||
56 | static 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 | ||
64 | static bool aio_epoll_try_enable(AioContext *ctx) | |
65 | { | |
66 | AioHandler *node; | |
67 | struct epoll_event event; | |
68 | ||
2bbf11d7 | 69 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
fbe3fc5c FZ |
70 | 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 | ||
85 | static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new) | |
86 | { | |
87 | struct epoll_event event; | |
88 | int r; | |
35dd66e2 | 89 | int ctl; |
fbe3fc5c FZ |
90 | |
91 | if (!ctx->epoll_enabled) { | |
92 | return; | |
93 | } | |
94 | if (!node->pfd.events) { | |
35dd66e2 | 95 | ctl = EPOLL_CTL_DEL; |
fbe3fc5c FZ |
96 | } else { |
97 | event.data.ptr = node; | |
98 | event.events = epoll_events_from_pfd(node->pfd.events); | |
35dd66e2 PB |
99 | 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); | |
fbe3fc5c FZ |
105 | } |
106 | } | |
107 | ||
108 | static 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, | |
8f801baf | 122 | ARRAY_SIZE(events), |
fbe3fc5c FZ |
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 | } | |
136 | out: | |
137 | return ret; | |
138 | } | |
139 | ||
140 | static 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 | ||
146 | static 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 | ||
167 | static void aio_epoll_update(AioContext *ctx, AioHandler *node, bool is_new) | |
168 | { | |
169 | } | |
170 | ||
171 | static int aio_epoll(AioContext *ctx, GPollFD *pfds, | |
172 | unsigned npfd, int64_t timeout) | |
173 | { | |
174 | assert(false); | |
175 | } | |
176 | ||
177 | static bool aio_epoll_enabled(AioContext *ctx) | |
178 | { | |
179 | return false; | |
180 | } | |
181 | ||
182 | static bool aio_epoll_check_poll(AioContext *ctx, GPollFD *pfds, | |
183 | unsigned npfd, int64_t timeout) | |
184 | { | |
185 | return false; | |
186 | } | |
187 | ||
188 | #endif | |
189 | ||
a915f4bc | 190 | static AioHandler *find_aio_handler(AioContext *ctx, int fd) |
a76bab49 AL |
191 | { |
192 | AioHandler *node; | |
193 | ||
a915f4bc | 194 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
cd9ba1eb | 195 | if (node->pfd.fd == fd) |
79d5ca56 AG |
196 | if (!node->deleted) |
197 | return node; | |
a76bab49 AL |
198 | } |
199 | ||
200 | return NULL; | |
201 | } | |
202 | ||
fef16601 RN |
203 | static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node) |
204 | { | |
205 | /* If the GSource is in the process of being destroyed then | |
206 | * g_source_remove_poll() causes an assertion failure. Skip | |
207 | * removal in that case, because glib cleans up its state during | |
208 | * destruction anyway. | |
209 | */ | |
210 | if (!g_source_is_destroyed(&ctx->source)) { | |
211 | g_source_remove_poll(&ctx->source, &node->pfd); | |
212 | } | |
213 | ||
214 | /* If a read is in progress, just mark the node as deleted */ | |
215 | if (qemu_lockcnt_count(&ctx->list_lock)) { | |
216 | node->deleted = 1; | |
217 | node->pfd.revents = 0; | |
218 | return false; | |
219 | } | |
220 | /* Otherwise, delete it for real. We can't just mark it as | |
221 | * deleted because deleted nodes are only cleaned up while | |
222 | * no one is walking the handlers list. | |
223 | */ | |
224 | QLIST_REMOVE(node, node); | |
225 | return true; | |
226 | } | |
227 | ||
a915f4bc PB |
228 | void aio_set_fd_handler(AioContext *ctx, |
229 | int fd, | |
dca21ef2 | 230 | bool is_external, |
a915f4bc PB |
231 | IOHandler *io_read, |
232 | IOHandler *io_write, | |
f6a51c84 | 233 | AioPollFn *io_poll, |
a915f4bc | 234 | void *opaque) |
a76bab49 AL |
235 | { |
236 | AioHandler *node; | |
fef16601 | 237 | AioHandler *new_node = NULL; |
fbe3fc5c | 238 | bool is_new = false; |
0ed39f3d | 239 | bool deleted = false; |
d7be5dd1 | 240 | int poll_disable_change; |
a76bab49 | 241 | |
2bbf11d7 PB |
242 | qemu_lockcnt_lock(&ctx->list_lock); |
243 | ||
a915f4bc | 244 | node = find_aio_handler(ctx, fd); |
a76bab49 AL |
245 | |
246 | /* Are we deleting the fd handler? */ | |
4a1cba38 | 247 | if (!io_read && !io_write && !io_poll) { |
36173ec5 | 248 | if (node == NULL) { |
2bbf11d7 | 249 | qemu_lockcnt_unlock(&ctx->list_lock); |
36173ec5 PB |
250 | return; |
251 | } | |
8821b34a RN |
252 | /* Clean events in order to unregister fd from the ctx epoll. */ |
253 | node->pfd.events = 0; | |
254 | ||
d7be5dd1 | 255 | poll_disable_change = -!node->io_poll; |
a76bab49 | 256 | } else { |
d7be5dd1 | 257 | poll_disable_change = !io_poll - (node && !node->io_poll); |
a76bab49 | 258 | if (node == NULL) { |
fbe3fc5c | 259 | is_new = true; |
a76bab49 | 260 | } |
fef16601 RN |
261 | /* Alloc and insert if it's not already there */ |
262 | new_node = g_new0(AioHandler, 1); | |
4a1cba38 | 263 | |
a76bab49 | 264 | /* Update handler with latest information */ |
fef16601 RN |
265 | new_node->io_read = io_read; |
266 | new_node->io_write = io_write; | |
267 | new_node->io_poll = io_poll; | |
268 | new_node->opaque = opaque; | |
269 | new_node->is_external = is_external; | |
270 | ||
271 | if (is_new) { | |
272 | new_node->pfd.fd = fd; | |
273 | } else { | |
274 | new_node->pfd = node->pfd; | |
275 | } | |
276 | g_source_add_poll(&ctx->source, &new_node->pfd); | |
277 | ||
278 | new_node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); | |
279 | new_node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0); | |
280 | ||
281 | QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node); | |
282 | } | |
283 | if (node) { | |
284 | deleted = aio_remove_fd_handler(ctx, node); | |
a76bab49 | 285 | } |
7ed2b24c | 286 | |
d7be5dd1 PB |
287 | /* No need to order poll_disable_cnt writes against other updates; |
288 | * the counter is only used to avoid wasting time and latency on | |
289 | * iterated polling when the system call will be ultimately necessary. | |
290 | * Changing handlers is a rare event, and a little wasted polling until | |
291 | * the aio_notify below is not an issue. | |
292 | */ | |
293 | atomic_set(&ctx->poll_disable_cnt, | |
294 | atomic_read(&ctx->poll_disable_cnt) + poll_disable_change); | |
295 | ||
fef16601 RN |
296 | if (new_node) { |
297 | aio_epoll_update(ctx, new_node, is_new); | |
298 | } else if (node) { | |
299 | /* Unregister deleted fd_handler */ | |
300 | aio_epoll_update(ctx, node, false); | |
301 | } | |
2bbf11d7 | 302 | qemu_lockcnt_unlock(&ctx->list_lock); |
7ed2b24c | 303 | aio_notify(ctx); |
4a1cba38 | 304 | |
0ed39f3d FZ |
305 | if (deleted) { |
306 | g_free(node); | |
307 | } | |
9958c351 PB |
308 | } |
309 | ||
684e508c SH |
310 | void aio_set_fd_poll(AioContext *ctx, int fd, |
311 | IOHandler *io_poll_begin, | |
312 | IOHandler *io_poll_end) | |
313 | { | |
314 | AioHandler *node = find_aio_handler(ctx, fd); | |
315 | ||
316 | if (!node) { | |
317 | return; | |
318 | } | |
319 | ||
320 | node->io_poll_begin = io_poll_begin; | |
321 | node->io_poll_end = io_poll_end; | |
322 | } | |
323 | ||
a915f4bc PB |
324 | void aio_set_event_notifier(AioContext *ctx, |
325 | EventNotifier *notifier, | |
dca21ef2 | 326 | bool is_external, |
f6a51c84 SH |
327 | EventNotifierHandler *io_read, |
328 | AioPollFn *io_poll) | |
a76bab49 | 329 | { |
f6a51c84 SH |
330 | aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), is_external, |
331 | (IOHandler *)io_read, NULL, io_poll, notifier); | |
a76bab49 AL |
332 | } |
333 | ||
684e508c SH |
334 | void aio_set_event_notifier_poll(AioContext *ctx, |
335 | EventNotifier *notifier, | |
336 | EventNotifierHandler *io_poll_begin, | |
337 | EventNotifierHandler *io_poll_end) | |
338 | { | |
339 | aio_set_fd_poll(ctx, event_notifier_get_fd(notifier), | |
340 | (IOHandler *)io_poll_begin, | |
341 | (IOHandler *)io_poll_end); | |
342 | } | |
343 | ||
344 | static void poll_set_started(AioContext *ctx, bool started) | |
345 | { | |
346 | AioHandler *node; | |
347 | ||
348 | if (started == ctx->poll_started) { | |
349 | return; | |
350 | } | |
351 | ||
352 | ctx->poll_started = started; | |
353 | ||
2bbf11d7 PB |
354 | qemu_lockcnt_inc(&ctx->list_lock); |
355 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { | |
684e508c SH |
356 | IOHandler *fn; |
357 | ||
358 | if (node->deleted) { | |
359 | continue; | |
360 | } | |
361 | ||
362 | if (started) { | |
363 | fn = node->io_poll_begin; | |
364 | } else { | |
365 | fn = node->io_poll_end; | |
366 | } | |
367 | ||
368 | if (fn) { | |
369 | fn(node->opaque); | |
370 | } | |
371 | } | |
2bbf11d7 | 372 | qemu_lockcnt_dec(&ctx->list_lock); |
684e508c SH |
373 | } |
374 | ||
375 | ||
a3462c65 PB |
376 | bool aio_prepare(AioContext *ctx) |
377 | { | |
684e508c SH |
378 | /* Poll mode cannot be used with glib's event loop, disable it. */ |
379 | poll_set_started(ctx, false); | |
380 | ||
a3462c65 PB |
381 | return false; |
382 | } | |
383 | ||
cd9ba1eb PB |
384 | bool aio_pending(AioContext *ctx) |
385 | { | |
386 | AioHandler *node; | |
2bbf11d7 | 387 | bool result = false; |
cd9ba1eb | 388 | |
2bbf11d7 PB |
389 | /* |
390 | * We have to walk very carefully in case aio_set_fd_handler is | |
391 | * called while we're walking. | |
392 | */ | |
393 | qemu_lockcnt_inc(&ctx->list_lock); | |
394 | ||
395 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { | |
cd9ba1eb PB |
396 | int revents; |
397 | ||
cd9ba1eb | 398 | revents = node->pfd.revents & node->pfd.events; |
37989ced FZ |
399 | if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read && |
400 | aio_node_check(ctx, node->is_external)) { | |
2bbf11d7 PB |
401 | result = true; |
402 | break; | |
cd9ba1eb | 403 | } |
37989ced FZ |
404 | if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write && |
405 | aio_node_check(ctx, node->is_external)) { | |
2bbf11d7 PB |
406 | result = true; |
407 | break; | |
cd9ba1eb PB |
408 | } |
409 | } | |
2bbf11d7 | 410 | qemu_lockcnt_dec(&ctx->list_lock); |
cd9ba1eb | 411 | |
2bbf11d7 | 412 | return result; |
cd9ba1eb PB |
413 | } |
414 | ||
56d2c3c6 | 415 | static bool aio_dispatch_handlers(AioContext *ctx) |
a76bab49 | 416 | { |
abf90d39 | 417 | AioHandler *node, *tmp; |
d0c8d2c0 | 418 | bool progress = false; |
7c0628b2 | 419 | |
2bbf11d7 | 420 | QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) { |
abf90d39 | 421 | int revents; |
cd9ba1eb PB |
422 | |
423 | revents = node->pfd.revents & node->pfd.events; | |
424 | node->pfd.revents = 0; | |
425 | ||
d0c8d2c0 SH |
426 | if (!node->deleted && |
427 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && | |
37989ced | 428 | aio_node_check(ctx, node->is_external) && |
d0c8d2c0 | 429 | node->io_read) { |
cd9ba1eb | 430 | node->io_read(node->opaque); |
164a101f SH |
431 | |
432 | /* aio_notify() does not count as progress */ | |
433 | if (node->opaque != &ctx->notifier) { | |
434 | progress = true; | |
435 | } | |
cd9ba1eb | 436 | } |
d0c8d2c0 SH |
437 | if (!node->deleted && |
438 | (revents & (G_IO_OUT | G_IO_ERR)) && | |
37989ced | 439 | aio_node_check(ctx, node->is_external) && |
d0c8d2c0 | 440 | node->io_write) { |
cd9ba1eb PB |
441 | node->io_write(node->opaque); |
442 | progress = true; | |
443 | } | |
444 | ||
abf90d39 | 445 | if (node->deleted) { |
2bbf11d7 | 446 | if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { |
abf90d39 PB |
447 | QLIST_REMOVE(node, node); |
448 | g_free(node); | |
2bbf11d7 | 449 | qemu_lockcnt_inc_and_unlock(&ctx->list_lock); |
abf90d39 | 450 | } |
cd9ba1eb PB |
451 | } |
452 | } | |
438e1f47 | 453 | |
56d2c3c6 PB |
454 | return progress; |
455 | } | |
456 | ||
a153bf52 | 457 | void aio_dispatch(AioContext *ctx) |
56d2c3c6 | 458 | { |
a153bf52 | 459 | qemu_lockcnt_inc(&ctx->list_lock); |
bd451435 | 460 | aio_bh_poll(ctx); |
a153bf52 PB |
461 | aio_dispatch_handlers(ctx); |
462 | qemu_lockcnt_dec(&ctx->list_lock); | |
438e1f47 | 463 | |
a153bf52 | 464 | timerlistgroup_run_timers(&ctx->tlg); |
d0c8d2c0 SH |
465 | } |
466 | ||
e98ab097 PB |
467 | /* These thread-local variables are used only in a small part of aio_poll |
468 | * around the call to the poll() system call. In particular they are not | |
469 | * used while aio_poll is performing callbacks, which makes it much easier | |
470 | * to think about reentrancy! | |
471 | * | |
472 | * Stack-allocated arrays would be perfect but they have size limitations; | |
473 | * heap allocation is expensive enough that we want to reuse arrays across | |
474 | * calls to aio_poll(). And because poll() has to be called without holding | |
475 | * any lock, the arrays cannot be stored in AioContext. Thread-local data | |
476 | * has none of the disadvantages of these three options. | |
477 | */ | |
478 | static __thread GPollFD *pollfds; | |
479 | static __thread AioHandler **nodes; | |
480 | static __thread unsigned npfd, nalloc; | |
481 | static __thread Notifier pollfds_cleanup_notifier; | |
482 | ||
483 | static void pollfds_cleanup(Notifier *n, void *unused) | |
484 | { | |
485 | g_assert(npfd == 0); | |
486 | g_free(pollfds); | |
487 | g_free(nodes); | |
488 | nalloc = 0; | |
489 | } | |
490 | ||
491 | static void add_pollfd(AioHandler *node) | |
492 | { | |
493 | if (npfd == nalloc) { | |
494 | if (nalloc == 0) { | |
495 | pollfds_cleanup_notifier.notify = pollfds_cleanup; | |
496 | qemu_thread_atexit_add(&pollfds_cleanup_notifier); | |
497 | nalloc = 8; | |
498 | } else { | |
499 | g_assert(nalloc <= INT_MAX); | |
500 | nalloc *= 2; | |
501 | } | |
502 | pollfds = g_renew(GPollFD, pollfds, nalloc); | |
503 | nodes = g_renew(AioHandler *, nodes, nalloc); | |
504 | } | |
505 | nodes[npfd] = node; | |
506 | pollfds[npfd] = (GPollFD) { | |
507 | .fd = node->pfd.fd, | |
508 | .events = node->pfd.events, | |
509 | }; | |
510 | npfd++; | |
511 | } | |
512 | ||
e30cffa0 | 513 | static bool run_poll_handlers_once(AioContext *ctx, int64_t *timeout) |
684e508c SH |
514 | { |
515 | bool progress = false; | |
516 | AioHandler *node; | |
517 | ||
2bbf11d7 | 518 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
684e508c | 519 | if (!node->deleted && node->io_poll && |
59c9f437 | 520 | aio_node_check(ctx, node->is_external) && |
cfeb35d6 | 521 | node->io_poll(node->opaque)) { |
e30cffa0 | 522 | *timeout = 0; |
cfeb35d6 PB |
523 | if (node->opaque != &ctx->notifier) { |
524 | progress = true; | |
525 | } | |
684e508c SH |
526 | } |
527 | ||
528 | /* Caller handles freeing deleted nodes. Don't do it here. */ | |
529 | } | |
530 | ||
531 | return progress; | |
532 | } | |
533 | ||
4a1cba38 SH |
534 | /* run_poll_handlers: |
535 | * @ctx: the AioContext | |
536 | * @max_ns: maximum time to poll for, in nanoseconds | |
537 | * | |
538 | * Polls for a given time. | |
539 | * | |
540 | * Note that ctx->notify_me must be non-zero so this function can detect | |
541 | * aio_notify(). | |
542 | * | |
2bbf11d7 | 543 | * Note that the caller must have incremented ctx->list_lock. |
4a1cba38 SH |
544 | * |
545 | * Returns: true if progress was made, false otherwise | |
546 | */ | |
e30cffa0 | 547 | static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout) |
4a1cba38 | 548 | { |
684e508c | 549 | bool progress; |
e30cffa0 | 550 | int64_t start_time, elapsed_time; |
4a1cba38 SH |
551 | |
552 | assert(ctx->notify_me); | |
2bbf11d7 | 553 | assert(qemu_lockcnt_count(&ctx->list_lock) > 0); |
4a1cba38 | 554 | |
e30cffa0 | 555 | trace_run_poll_handlers_begin(ctx, max_ns, *timeout); |
4a1cba38 | 556 | |
e30cffa0 | 557 | start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
4a1cba38 | 558 | do { |
e30cffa0 PB |
559 | progress = run_poll_handlers_once(ctx, timeout); |
560 | elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time; | |
561 | } while (!progress && elapsed_time < max_ns | |
d7be5dd1 | 562 | && !atomic_read(&ctx->poll_disable_cnt)); |
4a1cba38 | 563 | |
e30cffa0 PB |
564 | /* If time has passed with no successful polling, adjust *timeout to |
565 | * keep the same ending time. | |
566 | */ | |
567 | if (*timeout != -1) { | |
568 | *timeout -= MIN(*timeout, elapsed_time); | |
569 | } | |
4a1cba38 | 570 | |
e30cffa0 | 571 | trace_run_poll_handlers_end(ctx, progress, *timeout); |
4a1cba38 SH |
572 | return progress; |
573 | } | |
574 | ||
575 | /* try_poll_mode: | |
576 | * @ctx: the AioContext | |
e30cffa0 PB |
577 | * @timeout: timeout for blocking wait, computed by the caller and updated if |
578 | * polling succeeds. | |
4a1cba38 | 579 | * |
684e508c | 580 | * ctx->notify_me must be non-zero so this function can detect aio_notify(). |
4a1cba38 | 581 | * |
2bbf11d7 | 582 | * Note that the caller must have incremented ctx->list_lock. |
4a1cba38 SH |
583 | * |
584 | * Returns: true if progress was made, false otherwise | |
585 | */ | |
e30cffa0 | 586 | static bool try_poll_mode(AioContext *ctx, int64_t *timeout) |
4a1cba38 | 587 | { |
e30cffa0 PB |
588 | /* See qemu_soonest_timeout() uint64_t hack */ |
589 | int64_t max_ns = MIN((uint64_t)*timeout, (uint64_t)ctx->poll_ns); | |
4a1cba38 | 590 | |
e30cffa0 PB |
591 | if (max_ns && !atomic_read(&ctx->poll_disable_cnt)) { |
592 | poll_set_started(ctx, true); | |
684e508c | 593 | |
e30cffa0 PB |
594 | if (run_poll_handlers(ctx, max_ns, timeout)) { |
595 | return true; | |
4a1cba38 SH |
596 | } |
597 | } | |
598 | ||
684e508c SH |
599 | poll_set_started(ctx, false); |
600 | ||
601 | /* Even if we don't run busy polling, try polling once in case it can make | |
602 | * progress and the caller will be able to avoid ppoll(2)/epoll_wait(2). | |
603 | */ | |
e30cffa0 | 604 | return run_poll_handlers_once(ctx, timeout); |
4a1cba38 SH |
605 | } |
606 | ||
d0c8d2c0 SH |
607 | bool aio_poll(AioContext *ctx, bool blocking) |
608 | { | |
d0c8d2c0 | 609 | AioHandler *node; |
4a1cba38 SH |
610 | int i; |
611 | int ret = 0; | |
164a101f | 612 | bool progress; |
e98ab097 | 613 | int64_t timeout; |
82a41186 | 614 | int64_t start = 0; |
d0c8d2c0 | 615 | |
0dc165c1 KW |
616 | assert(in_aio_context_home_thread(ctx)); |
617 | ||
0ceb849b PB |
618 | /* aio_notify can avoid the expensive event_notifier_set if |
619 | * everything (file descriptors, bottom halves, timers) will | |
e4c7e2d1 PB |
620 | * be re-evaluated before the next blocking poll(). This is |
621 | * already true when aio_poll is called with blocking == false; | |
eabc9779 PB |
622 | * if blocking == true, it is only true after poll() returns, |
623 | * so disable the optimization now. | |
0ceb849b | 624 | */ |
eabc9779 PB |
625 | if (blocking) { |
626 | atomic_add(&ctx->notify_me, 2); | |
627 | } | |
0ceb849b | 628 | |
2bbf11d7 | 629 | qemu_lockcnt_inc(&ctx->list_lock); |
a76bab49 | 630 | |
82a41186 SH |
631 | if (ctx->poll_max_ns) { |
632 | start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
633 | } | |
634 | ||
e30cffa0 PB |
635 | timeout = blocking ? aio_compute_timeout(ctx) : 0; |
636 | progress = try_poll_mode(ctx, &timeout); | |
637 | assert(!(timeout && progress)); | |
638 | ||
639 | /* If polling is allowed, non-blocking aio_poll does not need the | |
640 | * system call---a single round of run_poll_handlers_once suffices. | |
641 | */ | |
642 | if (timeout || atomic_read(&ctx->poll_disable_cnt)) { | |
4a1cba38 | 643 | assert(npfd == 0); |
a76bab49 | 644 | |
4a1cba38 | 645 | /* fill pollfds */ |
6b942468 | 646 | |
4a1cba38 | 647 | if (!aio_epoll_enabled(ctx)) { |
2bbf11d7 | 648 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
4a1cba38 SH |
649 | if (!node->deleted && node->pfd.events |
650 | && aio_node_check(ctx, node->is_external)) { | |
651 | add_pollfd(node); | |
652 | } | |
6b942468 | 653 | } |
9eb0bfca | 654 | } |
a76bab49 | 655 | |
4a1cba38 | 656 | /* wait until next event */ |
4a1cba38 SH |
657 | if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) { |
658 | AioHandler epoll_handler; | |
659 | ||
660 | epoll_handler.pfd.fd = ctx->epollfd; | |
661 | epoll_handler.pfd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR; | |
662 | npfd = 0; | |
663 | add_pollfd(&epoll_handler); | |
664 | ret = aio_epoll(ctx, pollfds, npfd, timeout); | |
665 | } else { | |
666 | ret = qemu_poll_ns(pollfds, npfd, timeout); | |
667 | } | |
fbe3fc5c | 668 | } |
4a1cba38 | 669 | |
eabc9779 PB |
670 | if (blocking) { |
671 | atomic_sub(&ctx->notify_me, 2); | |
b37548fc | 672 | aio_notify_accept(ctx); |
eabc9779 | 673 | } |
9eb0bfca | 674 | |
82a41186 SH |
675 | /* Adjust polling time */ |
676 | if (ctx->poll_max_ns) { | |
677 | int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start; | |
678 | ||
679 | if (block_ns <= ctx->poll_ns) { | |
680 | /* This is the sweet spot, no adjustment needed */ | |
681 | } else if (block_ns > ctx->poll_max_ns) { | |
682 | /* We'd have to poll for too long, poll less */ | |
683 | int64_t old = ctx->poll_ns; | |
684 | ||
685 | if (ctx->poll_shrink) { | |
686 | ctx->poll_ns /= ctx->poll_shrink; | |
687 | } else { | |
688 | ctx->poll_ns = 0; | |
689 | } | |
690 | ||
691 | trace_poll_shrink(ctx, old, ctx->poll_ns); | |
692 | } else if (ctx->poll_ns < ctx->poll_max_ns && | |
693 | block_ns < ctx->poll_max_ns) { | |
694 | /* There is room to grow, poll longer */ | |
695 | int64_t old = ctx->poll_ns; | |
696 | int64_t grow = ctx->poll_grow; | |
697 | ||
698 | if (grow == 0) { | |
699 | grow = 2; | |
700 | } | |
701 | ||
702 | if (ctx->poll_ns) { | |
703 | ctx->poll_ns *= grow; | |
704 | } else { | |
705 | ctx->poll_ns = 4000; /* start polling at 4 microseconds */ | |
706 | } | |
707 | ||
708 | if (ctx->poll_ns > ctx->poll_max_ns) { | |
709 | ctx->poll_ns = ctx->poll_max_ns; | |
710 | } | |
711 | ||
712 | trace_poll_grow(ctx, old, ctx->poll_ns); | |
713 | } | |
714 | } | |
715 | ||
9eb0bfca PB |
716 | /* if we have any readable fds, dispatch event */ |
717 | if (ret > 0) { | |
e98ab097 PB |
718 | for (i = 0; i < npfd; i++) { |
719 | nodes[i]->pfd.revents = pollfds[i].revents; | |
a76bab49 | 720 | } |
438e1f47 AB |
721 | } |
722 | ||
e98ab097 | 723 | npfd = 0; |
e98ab097 | 724 | |
a153bf52 PB |
725 | progress |= aio_bh_poll(ctx); |
726 | ||
727 | if (ret > 0) { | |
a153bf52 | 728 | progress |= aio_dispatch_handlers(ctx); |
9eb0bfca | 729 | } |
bcdc1857 | 730 | |
bd451435 PB |
731 | qemu_lockcnt_dec(&ctx->list_lock); |
732 | ||
a153bf52 PB |
733 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
734 | ||
164a101f | 735 | return progress; |
a76bab49 | 736 | } |
37fcee5d | 737 | |
7e003465 | 738 | void aio_context_setup(AioContext *ctx) |
37fcee5d | 739 | { |
147dfab7 | 740 | #ifdef CONFIG_EPOLL_CREATE1 |
fbe3fc5c FZ |
741 | assert(!ctx->epollfd); |
742 | ctx->epollfd = epoll_create1(EPOLL_CLOEXEC); | |
743 | if (ctx->epollfd == -1) { | |
7e003465 | 744 | fprintf(stderr, "Failed to create epoll instance: %s", strerror(errno)); |
fbe3fc5c FZ |
745 | ctx->epoll_available = false; |
746 | } else { | |
747 | ctx->epoll_available = true; | |
748 | } | |
749 | #endif | |
37fcee5d | 750 | } |
4a1cba38 | 751 | |
cd0a6d2b JW |
752 | void aio_context_destroy(AioContext *ctx) |
753 | { | |
754 | #ifdef CONFIG_EPOLL_CREATE1 | |
755 | aio_epoll_disable(ctx); | |
756 | #endif | |
757 | } | |
758 | ||
82a41186 SH |
759 | void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, |
760 | int64_t grow, int64_t shrink, Error **errp) | |
4a1cba38 | 761 | { |
82a41186 SH |
762 | /* No thread synchronization here, it doesn't matter if an incorrect value |
763 | * is used once. | |
4a1cba38 SH |
764 | */ |
765 | ctx->poll_max_ns = max_ns; | |
82a41186 SH |
766 | ctx->poll_ns = 0; |
767 | ctx->poll_grow = grow; | |
768 | ctx->poll_shrink = shrink; | |
4a1cba38 SH |
769 | |
770 | aio_notify(ctx); | |
771 | } |