]>
Commit | Line | Data |
---|---|---|
a76bab49 AL |
1 | /* |
2 | * QEMU aio implementation | |
3 | * | |
f42b2207 PB |
4 | * Copyright IBM Corp., 2008 |
5 | * Copyright Red Hat Inc., 2012 | |
a76bab49 AL |
6 | * |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
f42b2207 | 9 | * Paolo Bonzini <pbonzini@redhat.com> |
a76bab49 AL |
10 | * |
11 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
12 | * the COPYING file in the top-level directory. | |
13 | * | |
6b620ca3 PB |
14 | * Contributions after 2012-01-13 are licensed under the terms of the |
15 | * GNU GPL, version 2 or (at your option) any later version. | |
a76bab49 AL |
16 | */ |
17 | ||
d38ea87a | 18 | #include "qemu/osdep.h" |
737e150e | 19 | #include "block/block.h" |
eada6d92 | 20 | #include "qemu/main-loop.h" |
1de7afc9 PB |
21 | #include "qemu/queue.h" |
22 | #include "qemu/sockets.h" | |
4a1cba38 | 23 | #include "qapi/error.h" |
b92d9a91 | 24 | #include "qemu/rcu_queue.h" |
e2a3a219 | 25 | #include "qemu/error-report.h" |
a76bab49 | 26 | |
f42b2207 PB |
27 | struct AioHandler { |
28 | EventNotifier *e; | |
b493317d PB |
29 | IOHandler *io_read; |
30 | IOHandler *io_write; | |
f42b2207 | 31 | EventNotifierHandler *io_notify; |
cd9ba1eb | 32 | GPollFD pfd; |
a76bab49 | 33 | int deleted; |
b493317d | 34 | void *opaque; |
72cf2d4f | 35 | QLIST_ENTRY(AioHandler) node; |
a76bab49 AL |
36 | }; |
37 | ||
fef16601 RN |
38 | static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node) |
39 | { | |
da0652c0 YL |
40 | /* |
41 | * If the GSource is in the process of being destroyed then | |
42 | * g_source_remove_poll() causes an assertion failure. Skip | |
43 | * removal in that case, because glib cleans up its state during | |
44 | * destruction anyway. | |
45 | */ | |
46 | if (!g_source_is_destroyed(&ctx->source)) { | |
47 | g_source_remove_poll(&ctx->source, &node->pfd); | |
48 | } | |
49 | ||
fef16601 RN |
50 | /* If aio_poll is in progress, just mark the node as deleted */ |
51 | if (qemu_lockcnt_count(&ctx->list_lock)) { | |
52 | node->deleted = 1; | |
53 | node->pfd.revents = 0; | |
54 | } else { | |
55 | /* Otherwise, delete it for real. We can't just mark it as | |
56 | * deleted because deleted nodes are only cleaned up after | |
57 | * releasing the list_lock. | |
58 | */ | |
59 | QLIST_REMOVE(node, node); | |
60 | g_free(node); | |
61 | } | |
62 | } | |
63 | ||
b493317d PB |
64 | void aio_set_fd_handler(AioContext *ctx, |
65 | int fd, | |
66 | IOHandler *io_read, | |
67 | IOHandler *io_write, | |
4a1cba38 | 68 | AioPollFn *io_poll, |
826cc324 | 69 | IOHandler *io_poll_ready, |
b493317d PB |
70 | void *opaque) |
71 | { | |
fef16601 RN |
72 | AioHandler *old_node; |
73 | AioHandler *node = NULL; | |
abe34282 | 74 | SOCKET s; |
b493317d | 75 | |
e2a3a219 MAL |
76 | if (!fd_is_socket(fd)) { |
77 | error_report("fd=%d is not a socket, AIO implementation is missing", fd); | |
78 | return; | |
79 | } | |
80 | ||
abe34282 MAL |
81 | s = _get_osfhandle(fd); |
82 | ||
b92d9a91 | 83 | qemu_lockcnt_lock(&ctx->list_lock); |
fef16601 | 84 | QLIST_FOREACH(old_node, &ctx->aio_handlers, node) { |
abe34282 | 85 | if (old_node->pfd.fd == s && !old_node->deleted) { |
b493317d PB |
86 | break; |
87 | } | |
88 | } | |
89 | ||
fef16601 | 90 | if (io_read || io_write) { |
b493317d | 91 | HANDLE event; |
55d41b16 | 92 | long bitmask = 0; |
b493317d | 93 | |
fef16601 RN |
94 | /* Alloc and insert if it's not already there */ |
95 | node = g_new0(AioHandler, 1); | |
abe34282 | 96 | node->pfd.fd = s; |
b493317d PB |
97 | |
98 | node->pfd.events = 0; | |
99 | if (node->io_read) { | |
100 | node->pfd.events |= G_IO_IN; | |
101 | } | |
102 | if (node->io_write) { | |
103 | node->pfd.events |= G_IO_OUT; | |
104 | } | |
105 | ||
106 | node->e = &ctx->notifier; | |
107 | ||
108 | /* Update handler with latest information */ | |
109 | node->opaque = opaque; | |
110 | node->io_read = io_read; | |
111 | node->io_write = io_write; | |
112 | ||
55d41b16 AF |
113 | if (io_read) { |
114 | bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE; | |
115 | } | |
116 | ||
117 | if (io_write) { | |
118 | bitmask |= FD_WRITE | FD_CONNECT; | |
119 | } | |
120 | ||
fef16601 | 121 | QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node); |
b493317d | 122 | event = event_notifier_get_handle(&ctx->notifier); |
abe34282 | 123 | qemu_socket_select(fd, event, bitmask, NULL); |
b493317d | 124 | } |
fef16601 RN |
125 | if (old_node) { |
126 | aio_remove_fd_handler(ctx, old_node); | |
127 | } | |
b493317d | 128 | |
b92d9a91 | 129 | qemu_lockcnt_unlock(&ctx->list_lock); |
b493317d PB |
130 | aio_notify(ctx); |
131 | } | |
132 | ||
f42b2207 PB |
133 | void aio_set_event_notifier(AioContext *ctx, |
134 | EventNotifier *e, | |
4a1cba38 | 135 | EventNotifierHandler *io_notify, |
826cc324 SH |
136 | AioPollFn *io_poll, |
137 | EventNotifierHandler *io_poll_ready) | |
a76bab49 AL |
138 | { |
139 | AioHandler *node; | |
140 | ||
b92d9a91 | 141 | qemu_lockcnt_lock(&ctx->list_lock); |
a915f4bc | 142 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
f42b2207 PB |
143 | if (node->e == e && !node->deleted) { |
144 | break; | |
145 | } | |
a76bab49 AL |
146 | } |
147 | ||
a76bab49 | 148 | /* Are we deleting the fd handler? */ |
f42b2207 | 149 | if (!io_notify) { |
a76bab49 | 150 | if (node) { |
fef16601 | 151 | aio_remove_fd_handler(ctx, node); |
a76bab49 AL |
152 | } |
153 | } else { | |
154 | if (node == NULL) { | |
155 | /* Alloc and insert if it's not already there */ | |
3ba235a0 | 156 | node = g_new0(AioHandler, 1); |
f42b2207 PB |
157 | node->e = e; |
158 | node->pfd.fd = (uintptr_t)event_notifier_get_handle(e); | |
159 | node->pfd.events = G_IO_IN; | |
b92d9a91 | 160 | QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node); |
e3713e00 PB |
161 | |
162 | g_source_add_poll(&ctx->source, &node->pfd); | |
a76bab49 AL |
163 | } |
164 | /* Update handler with latest information */ | |
f42b2207 | 165 | node->io_notify = io_notify; |
a76bab49 | 166 | } |
7ed2b24c | 167 | |
b92d9a91 | 168 | qemu_lockcnt_unlock(&ctx->list_lock); |
7ed2b24c | 169 | aio_notify(ctx); |
9958c351 PB |
170 | } |
171 | ||
684e508c SH |
172 | void aio_set_event_notifier_poll(AioContext *ctx, |
173 | EventNotifier *notifier, | |
174 | EventNotifierHandler *io_poll_begin, | |
175 | EventNotifierHandler *io_poll_end) | |
176 | { | |
177 | /* Not implemented */ | |
178 | } | |
179 | ||
a3462c65 PB |
180 | bool aio_prepare(AioContext *ctx) |
181 | { | |
b493317d PB |
182 | static struct timeval tv0; |
183 | AioHandler *node; | |
184 | bool have_select_revents = false; | |
185 | fd_set rfds, wfds; | |
186 | ||
b92d9a91 PB |
187 | /* |
188 | * We have to walk very carefully in case aio_set_fd_handler is | |
189 | * called while we're walking. | |
190 | */ | |
191 | qemu_lockcnt_inc(&ctx->list_lock); | |
192 | ||
b493317d PB |
193 | /* fill fd sets */ |
194 | FD_ZERO(&rfds); | |
195 | FD_ZERO(&wfds); | |
b92d9a91 | 196 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
b493317d PB |
197 | if (node->io_read) { |
198 | FD_SET ((SOCKET)node->pfd.fd, &rfds); | |
199 | } | |
200 | if (node->io_write) { | |
201 | FD_SET ((SOCKET)node->pfd.fd, &wfds); | |
202 | } | |
203 | } | |
204 | ||
205 | if (select(0, &rfds, &wfds, NULL, &tv0) > 0) { | |
b92d9a91 | 206 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
b493317d PB |
207 | node->pfd.revents = 0; |
208 | if (FD_ISSET(node->pfd.fd, &rfds)) { | |
209 | node->pfd.revents |= G_IO_IN; | |
210 | have_select_revents = true; | |
211 | } | |
212 | ||
213 | if (FD_ISSET(node->pfd.fd, &wfds)) { | |
214 | node->pfd.revents |= G_IO_OUT; | |
215 | have_select_revents = true; | |
216 | } | |
217 | } | |
218 | } | |
219 | ||
b92d9a91 | 220 | qemu_lockcnt_dec(&ctx->list_lock); |
b493317d | 221 | return have_select_revents; |
a3462c65 PB |
222 | } |
223 | ||
cd9ba1eb PB |
224 | bool aio_pending(AioContext *ctx) |
225 | { | |
226 | AioHandler *node; | |
b92d9a91 | 227 | bool result = false; |
cd9ba1eb | 228 | |
b92d9a91 PB |
229 | /* |
230 | * We have to walk very carefully in case aio_set_fd_handler is | |
231 | * called while we're walking. | |
232 | */ | |
233 | qemu_lockcnt_inc(&ctx->list_lock); | |
234 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { | |
f42b2207 | 235 | if (node->pfd.revents && node->io_notify) { |
b92d9a91 PB |
236 | result = true; |
237 | break; | |
cd9ba1eb | 238 | } |
b493317d PB |
239 | |
240 | if ((node->pfd.revents & G_IO_IN) && node->io_read) { | |
b92d9a91 PB |
241 | result = true; |
242 | break; | |
b493317d PB |
243 | } |
244 | if ((node->pfd.revents & G_IO_OUT) && node->io_write) { | |
b92d9a91 PB |
245 | result = true; |
246 | break; | |
b493317d | 247 | } |
cd9ba1eb PB |
248 | } |
249 | ||
b92d9a91 PB |
250 | qemu_lockcnt_dec(&ctx->list_lock); |
251 | return result; | |
cd9ba1eb PB |
252 | } |
253 | ||
a398dea3 | 254 | static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event) |
a76bab49 | 255 | { |
b92d9a91 | 256 | AioHandler *node; |
a398dea3 | 257 | bool progress = false; |
b92d9a91 | 258 | AioHandler *tmp; |
7c0628b2 | 259 | |
cd9ba1eb | 260 | /* |
87f68d31 | 261 | * We have to walk very carefully in case aio_set_fd_handler is |
cd9ba1eb PB |
262 | * called while we're walking. |
263 | */ | |
b92d9a91 | 264 | QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) { |
b493317d | 265 | int revents = node->pfd.revents; |
cd9ba1eb | 266 | |
a398dea3 | 267 | if (!node->deleted && |
b493317d | 268 | (revents || event_notifier_get_handle(node->e) == event) && |
a398dea3 | 269 | node->io_notify) { |
f42b2207 PB |
270 | node->pfd.revents = 0; |
271 | node->io_notify(node->e); | |
164a101f SH |
272 | |
273 | /* aio_notify() does not count as progress */ | |
8b2d42d2 | 274 | if (node->e != &ctx->notifier) { |
164a101f SH |
275 | progress = true; |
276 | } | |
cd9ba1eb PB |
277 | } |
278 | ||
b493317d PB |
279 | if (!node->deleted && |
280 | (node->io_read || node->io_write)) { | |
281 | node->pfd.revents = 0; | |
282 | if ((revents & G_IO_IN) && node->io_read) { | |
283 | node->io_read(node->opaque); | |
284 | progress = true; | |
285 | } | |
286 | if ((revents & G_IO_OUT) && node->io_write) { | |
287 | node->io_write(node->opaque); | |
288 | progress = true; | |
289 | } | |
290 | ||
291 | /* if the next select() will return an event, we have progressed */ | |
292 | if (event == event_notifier_get_handle(&ctx->notifier)) { | |
293 | WSANETWORKEVENTS ev; | |
294 | WSAEnumNetworkEvents(node->pfd.fd, event, &ev); | |
295 | if (ev.lNetworkEvents) { | |
296 | progress = true; | |
297 | } | |
298 | } | |
299 | } | |
300 | ||
abf90d39 | 301 | if (node->deleted) { |
b92d9a91 | 302 | if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { |
abf90d39 PB |
303 | QLIST_REMOVE(node, node); |
304 | g_free(node); | |
b92d9a91 | 305 | qemu_lockcnt_inc_and_unlock(&ctx->list_lock); |
abf90d39 | 306 | } |
cd9ba1eb PB |
307 | } |
308 | } | |
309 | ||
a398dea3 PB |
310 | return progress; |
311 | } | |
312 | ||
a153bf52 | 313 | void aio_dispatch(AioContext *ctx) |
a398dea3 | 314 | { |
bd451435 | 315 | qemu_lockcnt_inc(&ctx->list_lock); |
a153bf52 PB |
316 | aio_bh_poll(ctx); |
317 | aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE); | |
bd451435 | 318 | qemu_lockcnt_dec(&ctx->list_lock); |
a153bf52 | 319 | timerlistgroup_run_timers(&ctx->tlg); |
a398dea3 PB |
320 | } |
321 | ||
322 | bool aio_poll(AioContext *ctx, bool blocking) | |
323 | { | |
324 | AioHandler *node; | |
e0d034bb | 325 | HANDLE events[MAXIMUM_WAIT_OBJECTS]; |
eabc9779 | 326 | bool progress, have_select_revents, first; |
e0d034bb | 327 | unsigned count; |
a398dea3 PB |
328 | int timeout; |
329 | ||
5710a3e0 PB |
330 | /* |
331 | * There cannot be two concurrent aio_poll calls for the same AioContext (or | |
332 | * an aio_poll concurrent with a GSource prepare/check/dispatch callback). | |
333 | * We rely on this below to avoid slow locked accesses to ctx->notify_me. | |
eada6d92 VR |
334 | * |
335 | * aio_poll() may only be called in the AioContext's thread. iohandler_ctx | |
336 | * is special in that it runs in the main thread, but that thread's context | |
337 | * is qemu_aio_context. | |
5710a3e0 | 338 | */ |
eada6d92 VR |
339 | assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ? |
340 | qemu_get_aio_context() : ctx)); | |
a398dea3 PB |
341 | progress = false; |
342 | ||
0a9dd166 PB |
343 | /* aio_notify can avoid the expensive event_notifier_set if |
344 | * everything (file descriptors, bottom halves, timers) will | |
345 | * be re-evaluated before the next blocking poll(). This is | |
346 | * already true when aio_poll is called with blocking == false; | |
eabc9779 PB |
347 | * if blocking == true, it is only true after poll() returns, |
348 | * so disable the optimization now. | |
0a9dd166 | 349 | */ |
eabc9779 | 350 | if (blocking) { |
d73415a3 | 351 | qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2); |
5710a3e0 PB |
352 | /* |
353 | * Write ctx->notify_me before computing the timeout | |
354 | * (reading bottom half flags, etc.). Pairs with | |
355 | * smp_mb in aio_notify(). | |
356 | */ | |
357 | smp_mb(); | |
eabc9779 | 358 | } |
0a9dd166 | 359 | |
b92d9a91 | 360 | qemu_lockcnt_inc(&ctx->list_lock); |
6493c975 PB |
361 | have_select_revents = aio_prepare(ctx); |
362 | ||
9eb0bfca | 363 | /* fill fd sets */ |
f42b2207 | 364 | count = 0; |
b92d9a91 | 365 | QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { |
60f782b6 | 366 | if (!node->deleted && node->io_notify) { |
e0d034bb | 367 | assert(count < MAXIMUM_WAIT_OBJECTS); |
f42b2207 | 368 | events[count++] = event_notifier_get_handle(node->e); |
9eb0bfca PB |
369 | } |
370 | } | |
a76bab49 | 371 | |
3672fa50 | 372 | first = true; |
a76bab49 | 373 | |
6493c975 PB |
374 | /* ctx->notifier is always registered. */ |
375 | assert(count > 0); | |
376 | ||
377 | /* Multiple iterations, all of them non-blocking except the first, | |
378 | * may be necessary to process all pending events. After the first | |
379 | * WaitForMultipleObjects call ctx->notify_me will be decremented. | |
380 | */ | |
381 | do { | |
b493317d | 382 | HANDLE event; |
438e1f47 AB |
383 | int ret; |
384 | ||
6493c975 | 385 | timeout = blocking && !have_select_revents |
845ca10d | 386 | ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0; |
438e1f47 | 387 | ret = WaitForMultipleObjects(count, events, FALSE, timeout); |
eabc9779 PB |
388 | if (blocking) { |
389 | assert(first); | |
d73415a3 SH |
390 | qatomic_store_release(&ctx->notify_me, |
391 | qatomic_read(&ctx->notify_me) - 2); | |
b37548fc | 392 | aio_notify_accept(ctx); |
eabc9779 | 393 | } |
f42b2207 | 394 | |
21a03d17 | 395 | if (first) { |
21a03d17 PB |
396 | progress |= aio_bh_poll(ctx); |
397 | first = false; | |
3672fa50 | 398 | } |
3672fa50 | 399 | |
f42b2207 | 400 | /* if we have any signaled events, dispatch event */ |
b493317d PB |
401 | event = NULL; |
402 | if ((DWORD) (ret - WAIT_OBJECT_0) < count) { | |
403 | event = events[ret - WAIT_OBJECT_0]; | |
a90d411e | 404 | events[ret - WAIT_OBJECT_0] = events[--count]; |
b493317d | 405 | } else if (!have_select_revents) { |
f42b2207 PB |
406 | break; |
407 | } | |
408 | ||
b493317d | 409 | have_select_revents = false; |
f42b2207 | 410 | blocking = false; |
9eb0bfca | 411 | |
b493317d | 412 | progress |= aio_dispatch_handlers(ctx, event); |
6493c975 | 413 | } while (count > 0); |
bcdc1857 | 414 | |
bd451435 PB |
415 | qemu_lockcnt_dec(&ctx->list_lock); |
416 | ||
e4c7e2d1 | 417 | progress |= timerlistgroup_run_timers(&ctx->tlg); |
164a101f | 418 | return progress; |
a76bab49 | 419 | } |
37fcee5d | 420 | |
7e003465 | 421 | void aio_context_setup(AioContext *ctx) |
37fcee5d FZ |
422 | { |
423 | } | |
4a1cba38 | 424 | |
cd0a6d2b JW |
425 | void aio_context_destroy(AioContext *ctx) |
426 | { | |
427 | } | |
428 | ||
ba607ca8 SH |
429 | void aio_context_use_g_source(AioContext *ctx) |
430 | { | |
431 | } | |
432 | ||
82a41186 SH |
433 | void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, |
434 | int64_t grow, int64_t shrink, Error **errp) | |
4a1cba38 | 435 | { |
90c558be PX |
436 | if (max_ns) { |
437 | error_setg(errp, "AioContext polling is not implemented on Windows"); | |
438 | } | |
4a1cba38 | 439 | } |
1793ad02 SG |
440 | |
441 | void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch, | |
442 | Error **errp) | |
443 | { | |
444 | } |