]>
Commit | Line | Data |
---|---|---|
4f999d05 | 1 | /* |
c2b38b27 | 2 | * Data plane event loop |
4f999d05 KW |
3 | * |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
c2b38b27 | 5 | * Copyright (c) 2009-2017 QEMU contributors |
4f999d05 KW |
6 | * |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
d38ea87a | 26 | #include "qemu/osdep.h" |
da34e65c | 27 | #include "qapi/error.h" |
4f999d05 | 28 | #include "qemu-common.h" |
737e150e | 29 | #include "block/aio.h" |
9b34277d | 30 | #include "block/thread-pool.h" |
1de7afc9 | 31 | #include "qemu/main-loop.h" |
0ceb849b | 32 | #include "qemu/atomic.h" |
0187f5c9 | 33 | #include "block/raw-aio.h" |
0c330a73 PB |
34 | #include "qemu/coroutine_int.h" |
35 | #include "trace.h" | |
9a1e9481 | 36 | |
4f999d05 KW |
37 | /***********************************************************/ |
38 | /* bottom halves (can be seen as timers which expire ASAP) */ | |
39 | ||
40 | struct QEMUBH { | |
2f4dc3c1 | 41 | AioContext *ctx; |
4f999d05 KW |
42 | QEMUBHFunc *cb; |
43 | void *opaque; | |
4f999d05 | 44 | QEMUBH *next; |
9b47b17e SW |
45 | bool scheduled; |
46 | bool idle; | |
47 | bool deleted; | |
4f999d05 KW |
48 | }; |
49 | ||
5b8bb359 PB |
50 | void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque) |
51 | { | |
52 | QEMUBH *bh; | |
53 | bh = g_new(QEMUBH, 1); | |
54 | *bh = (QEMUBH){ | |
55 | .ctx = ctx, | |
56 | .cb = cb, | |
57 | .opaque = opaque, | |
58 | }; | |
d7c99a12 | 59 | qemu_lockcnt_lock(&ctx->list_lock); |
5b8bb359 PB |
60 | bh->next = ctx->first_bh; |
61 | bh->scheduled = 1; | |
62 | bh->deleted = 1; | |
63 | /* Make sure that the members are ready before putting bh into list */ | |
64 | smp_wmb(); | |
65 | ctx->first_bh = bh; | |
d7c99a12 | 66 | qemu_lockcnt_unlock(&ctx->list_lock); |
c9d1a561 | 67 | aio_notify(ctx); |
5b8bb359 PB |
68 | } |
69 | ||
f627aab1 | 70 | QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) |
4f999d05 KW |
71 | { |
72 | QEMUBH *bh; | |
ee82310f PB |
73 | bh = g_new(QEMUBH, 1); |
74 | *bh = (QEMUBH){ | |
75 | .ctx = ctx, | |
76 | .cb = cb, | |
77 | .opaque = opaque, | |
78 | }; | |
d7c99a12 | 79 | qemu_lockcnt_lock(&ctx->list_lock); |
f627aab1 | 80 | bh->next = ctx->first_bh; |
dcc772e2 LPF |
81 | /* Make sure that the members are ready before putting bh into list */ |
82 | smp_wmb(); | |
f627aab1 | 83 | ctx->first_bh = bh; |
d7c99a12 | 84 | qemu_lockcnt_unlock(&ctx->list_lock); |
4f999d05 KW |
85 | return bh; |
86 | } | |
87 | ||
df281b80 PD |
88 | void aio_bh_call(QEMUBH *bh) |
89 | { | |
90 | bh->cb(bh->opaque); | |
91 | } | |
92 | ||
bd451435 PB |
93 | /* Multiple occurrences of aio_bh_poll cannot be called concurrently. |
94 | * The count in ctx->list_lock is incremented before the call, and is | |
95 | * not affected by the call. | |
96 | */ | |
f627aab1 | 97 | int aio_bh_poll(AioContext *ctx) |
4f999d05 | 98 | { |
7887f620 | 99 | QEMUBH *bh, **bhp, *next; |
4f999d05 | 100 | int ret; |
7d506c90 | 101 | bool deleted = false; |
648fb0ea | 102 | |
4f999d05 | 103 | ret = 0; |
d7c99a12 PB |
104 | for (bh = atomic_rcu_read(&ctx->first_bh); bh; bh = next) { |
105 | next = atomic_rcu_read(&bh->next); | |
e8d3b1a2 PB |
106 | /* The atomic_xchg is paired with the one in qemu_bh_schedule. The |
107 | * implicit memory barrier ensures that the callback sees all writes | |
108 | * done by the scheduling thread. It also ensures that the scheduling | |
109 | * thread sees the zero before bh->cb has run, and thus will call | |
110 | * aio_notify again if necessary. | |
111 | */ | |
5b8bb359 | 112 | if (atomic_xchg(&bh->scheduled, 0)) { |
65c1b5b6 PB |
113 | /* Idle BHs don't count as progress */ |
114 | if (!bh->idle) { | |
4f999d05 | 115 | ret = 1; |
ca96ac44 | 116 | } |
4f999d05 | 117 | bh->idle = 0; |
df281b80 | 118 | aio_bh_call(bh); |
4f999d05 | 119 | } |
7d506c90 PB |
120 | if (bh->deleted) { |
121 | deleted = true; | |
122 | } | |
4f999d05 KW |
123 | } |
124 | ||
125 | /* remove deleted bhs */ | |
7d506c90 | 126 | if (!deleted) { |
7d506c90 PB |
127 | return ret; |
128 | } | |
129 | ||
bd451435 | 130 | if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { |
f627aab1 | 131 | bhp = &ctx->first_bh; |
648fb0ea KW |
132 | while (*bhp) { |
133 | bh = *bhp; | |
5b8bb359 | 134 | if (bh->deleted && !bh->scheduled) { |
648fb0ea KW |
135 | *bhp = bh->next; |
136 | g_free(bh); | |
137 | } else { | |
138 | bhp = &bh->next; | |
139 | } | |
140 | } | |
bd451435 | 141 | qemu_lockcnt_inc_and_unlock(&ctx->list_lock); |
4f999d05 | 142 | } |
4f999d05 KW |
143 | return ret; |
144 | } | |
145 | ||
146 | void qemu_bh_schedule_idle(QEMUBH *bh) | |
147 | { | |
4f999d05 | 148 | bh->idle = 1; |
dcc772e2 LPF |
149 | /* Make sure that idle & any writes needed by the callback are done |
150 | * before the locations are read in the aio_bh_poll. | |
151 | */ | |
e8d3b1a2 | 152 | atomic_mb_set(&bh->scheduled, 1); |
4f999d05 KW |
153 | } |
154 | ||
155 | void qemu_bh_schedule(QEMUBH *bh) | |
156 | { | |
924fe129 SH |
157 | AioContext *ctx; |
158 | ||
924fe129 | 159 | ctx = bh->ctx; |
4f999d05 | 160 | bh->idle = 0; |
e8d3b1a2 | 161 | /* The memory barrier implicit in atomic_xchg makes sure that: |
924fe129 SH |
162 | * 1. idle & any writes needed by the callback are done before the |
163 | * locations are read in the aio_bh_poll. | |
164 | * 2. ctx is loaded before scheduled is set and the callback has a chance | |
165 | * to execute. | |
dcc772e2 | 166 | */ |
e8d3b1a2 PB |
167 | if (atomic_xchg(&bh->scheduled, 1) == 0) { |
168 | aio_notify(ctx); | |
169 | } | |
4f999d05 KW |
170 | } |
171 | ||
dcc772e2 LPF |
172 | |
173 | /* This func is async. | |
174 | */ | |
4f999d05 KW |
175 | void qemu_bh_cancel(QEMUBH *bh) |
176 | { | |
177 | bh->scheduled = 0; | |
178 | } | |
179 | ||
dcc772e2 LPF |
180 | /* This func is async.The bottom half will do the delete action at the finial |
181 | * end. | |
182 | */ | |
4f999d05 KW |
183 | void qemu_bh_delete(QEMUBH *bh) |
184 | { | |
185 | bh->scheduled = 0; | |
186 | bh->deleted = 1; | |
187 | } | |
188 | ||
845ca10d PB |
189 | int64_t |
190 | aio_compute_timeout(AioContext *ctx) | |
4f999d05 | 191 | { |
845ca10d PB |
192 | int64_t deadline; |
193 | int timeout = -1; | |
4f999d05 KW |
194 | QEMUBH *bh; |
195 | ||
d7c99a12 PB |
196 | for (bh = atomic_rcu_read(&ctx->first_bh); bh; |
197 | bh = atomic_rcu_read(&bh->next)) { | |
5b8bb359 | 198 | if (bh->scheduled) { |
4f999d05 KW |
199 | if (bh->idle) { |
200 | /* idle bottom halves will be polled at least | |
201 | * every 10ms */ | |
845ca10d | 202 | timeout = 10000000; |
4f999d05 KW |
203 | } else { |
204 | /* non-idle bottom halves will be executed | |
205 | * immediately */ | |
845ca10d | 206 | return 0; |
4f999d05 KW |
207 | } |
208 | } | |
209 | } | |
e3713e00 | 210 | |
845ca10d | 211 | deadline = timerlistgroup_deadline_ns(&ctx->tlg); |
533a8cf3 | 212 | if (deadline == 0) { |
845ca10d | 213 | return 0; |
533a8cf3 | 214 | } else { |
845ca10d | 215 | return qemu_soonest_timeout(timeout, deadline); |
533a8cf3 | 216 | } |
845ca10d | 217 | } |
533a8cf3 | 218 | |
845ca10d PB |
219 | static gboolean |
220 | aio_ctx_prepare(GSource *source, gint *timeout) | |
221 | { | |
222 | AioContext *ctx = (AioContext *) source; | |
223 | ||
eabc9779 PB |
224 | atomic_or(&ctx->notify_me, 1); |
225 | ||
845ca10d PB |
226 | /* We assume there is no timeout already supplied */ |
227 | *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)); | |
a3462c65 PB |
228 | |
229 | if (aio_prepare(ctx)) { | |
230 | *timeout = 0; | |
231 | } | |
232 | ||
845ca10d | 233 | return *timeout == 0; |
e3713e00 PB |
234 | } |
235 | ||
236 | static gboolean | |
237 | aio_ctx_check(GSource *source) | |
238 | { | |
239 | AioContext *ctx = (AioContext *) source; | |
240 | QEMUBH *bh; | |
241 | ||
eabc9779 | 242 | atomic_and(&ctx->notify_me, ~1); |
05e514b1 | 243 | aio_notify_accept(ctx); |
21a03d17 | 244 | |
e3713e00 | 245 | for (bh = ctx->first_bh; bh; bh = bh->next) { |
5b8bb359 | 246 | if (bh->scheduled) { |
e3713e00 | 247 | return true; |
6977d901 | 248 | } |
e3713e00 | 249 | } |
533a8cf3 | 250 | return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0); |
e3713e00 PB |
251 | } |
252 | ||
253 | static gboolean | |
254 | aio_ctx_dispatch(GSource *source, | |
255 | GSourceFunc callback, | |
256 | gpointer user_data) | |
257 | { | |
258 | AioContext *ctx = (AioContext *) source; | |
259 | ||
260 | assert(callback == NULL); | |
a153bf52 | 261 | aio_dispatch(ctx); |
e3713e00 PB |
262 | return true; |
263 | } | |
264 | ||
2f4dc3c1 PB |
265 | static void |
266 | aio_ctx_finalize(GSource *source) | |
267 | { | |
268 | AioContext *ctx = (AioContext *) source; | |
269 | ||
9b34277d | 270 | thread_pool_free(ctx->thread_pool); |
a076972a | 271 | |
0187f5c9 PB |
272 | #ifdef CONFIG_LINUX_AIO |
273 | if (ctx->linux_aio) { | |
274 | laio_detach_aio_context(ctx->linux_aio, ctx); | |
275 | laio_cleanup(ctx->linux_aio); | |
276 | ctx->linux_aio = NULL; | |
277 | } | |
278 | #endif | |
279 | ||
0c330a73 PB |
280 | assert(QSLIST_EMPTY(&ctx->scheduled_coroutines)); |
281 | qemu_bh_delete(ctx->co_schedule_bh); | |
282 | ||
d7c99a12 PB |
283 | qemu_lockcnt_lock(&ctx->list_lock); |
284 | assert(!qemu_lockcnt_count(&ctx->list_lock)); | |
a076972a SH |
285 | while (ctx->first_bh) { |
286 | QEMUBH *next = ctx->first_bh->next; | |
287 | ||
288 | /* qemu_bh_delete() must have been called on BHs in this AioContext */ | |
289 | assert(ctx->first_bh->deleted); | |
290 | ||
291 | g_free(ctx->first_bh); | |
292 | ctx->first_bh = next; | |
293 | } | |
d7c99a12 | 294 | qemu_lockcnt_unlock(&ctx->list_lock); |
a076972a | 295 | |
f6a51c84 | 296 | aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL); |
2f4dc3c1 | 297 | event_notifier_cleanup(&ctx->notifier); |
3fe71223 | 298 | qemu_rec_mutex_destroy(&ctx->lock); |
d7c99a12 | 299 | qemu_lockcnt_destroy(&ctx->list_lock); |
dae21b98 | 300 | timerlistgroup_deinit(&ctx->tlg); |
2f4dc3c1 PB |
301 | } |
302 | ||
e3713e00 PB |
303 | static GSourceFuncs aio_source_funcs = { |
304 | aio_ctx_prepare, | |
305 | aio_ctx_check, | |
306 | aio_ctx_dispatch, | |
2f4dc3c1 | 307 | aio_ctx_finalize |
e3713e00 PB |
308 | }; |
309 | ||
310 | GSource *aio_get_g_source(AioContext *ctx) | |
311 | { | |
312 | g_source_ref(&ctx->source); | |
313 | return &ctx->source; | |
314 | } | |
a915f4bc | 315 | |
9b34277d SH |
316 | ThreadPool *aio_get_thread_pool(AioContext *ctx) |
317 | { | |
318 | if (!ctx->thread_pool) { | |
319 | ctx->thread_pool = thread_pool_new(ctx); | |
320 | } | |
321 | return ctx->thread_pool; | |
322 | } | |
323 | ||
0187f5c9 PB |
324 | #ifdef CONFIG_LINUX_AIO |
325 | LinuxAioState *aio_get_linux_aio(AioContext *ctx) | |
326 | { | |
327 | if (!ctx->linux_aio) { | |
328 | ctx->linux_aio = laio_init(); | |
329 | laio_attach_aio_context(ctx->linux_aio, ctx); | |
330 | } | |
331 | return ctx->linux_aio; | |
332 | } | |
333 | #endif | |
334 | ||
2f4dc3c1 PB |
335 | void aio_notify(AioContext *ctx) |
336 | { | |
eabc9779 PB |
337 | /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs |
338 | * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll. | |
339 | */ | |
0ceb849b | 340 | smp_mb(); |
eabc9779 | 341 | if (ctx->notify_me) { |
0ceb849b | 342 | event_notifier_set(&ctx->notifier); |
05e514b1 PB |
343 | atomic_mb_set(&ctx->notified, true); |
344 | } | |
345 | } | |
346 | ||
347 | void aio_notify_accept(AioContext *ctx) | |
348 | { | |
349 | if (atomic_xchg(&ctx->notified, false)) { | |
350 | event_notifier_test_and_clear(&ctx->notifier); | |
0ceb849b | 351 | } |
2f4dc3c1 PB |
352 | } |
353 | ||
d5541d86 AB |
354 | static void aio_timerlist_notify(void *opaque) |
355 | { | |
356 | aio_notify(opaque); | |
357 | } | |
358 | ||
21a03d17 PB |
359 | static void event_notifier_dummy_cb(EventNotifier *e) |
360 | { | |
361 | } | |
362 | ||
4a1cba38 SH |
363 | /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */ |
364 | static bool event_notifier_poll(void *opaque) | |
365 | { | |
366 | EventNotifier *e = opaque; | |
367 | AioContext *ctx = container_of(e, AioContext, notifier); | |
368 | ||
369 | return atomic_read(&ctx->notified); | |
370 | } | |
371 | ||
0c330a73 PB |
372 | static void co_schedule_bh_cb(void *opaque) |
373 | { | |
374 | AioContext *ctx = opaque; | |
375 | QSLIST_HEAD(, Coroutine) straight, reversed; | |
376 | ||
377 | QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines); | |
378 | QSLIST_INIT(&straight); | |
379 | ||
380 | while (!QSLIST_EMPTY(&reversed)) { | |
381 | Coroutine *co = QSLIST_FIRST(&reversed); | |
382 | QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next); | |
383 | QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next); | |
384 | } | |
385 | ||
386 | while (!QSLIST_EMPTY(&straight)) { | |
387 | Coroutine *co = QSLIST_FIRST(&straight); | |
388 | QSLIST_REMOVE_HEAD(&straight, co_scheduled_next); | |
389 | trace_aio_co_schedule_bh_cb(ctx, co); | |
1919631e | 390 | aio_context_acquire(ctx); |
0c330a73 | 391 | qemu_coroutine_enter(co); |
1919631e | 392 | aio_context_release(ctx); |
0c330a73 PB |
393 | } |
394 | } | |
395 | ||
2f78e491 | 396 | AioContext *aio_context_new(Error **errp) |
f627aab1 | 397 | { |
2f78e491 | 398 | int ret; |
2f4dc3c1 | 399 | AioContext *ctx; |
37fcee5d | 400 | |
2f4dc3c1 | 401 | ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext)); |
7e003465 C |
402 | aio_context_setup(ctx); |
403 | ||
2f78e491 CN |
404 | ret = event_notifier_init(&ctx->notifier, false); |
405 | if (ret < 0) { | |
2f78e491 | 406 | error_setg_errno(errp, -ret, "Failed to initialize event notifier"); |
37fcee5d | 407 | goto fail; |
2f78e491 | 408 | } |
fcf5def1 | 409 | g_source_set_can_recurse(&ctx->source, true); |
d7c99a12 | 410 | qemu_lockcnt_init(&ctx->list_lock); |
0c330a73 PB |
411 | |
412 | ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx); | |
413 | QSLIST_INIT(&ctx->scheduled_coroutines); | |
414 | ||
2f78e491 | 415 | aio_set_event_notifier(ctx, &ctx->notifier, |
dca21ef2 | 416 | false, |
2f78e491 | 417 | (EventNotifierHandler *) |
f6a51c84 | 418 | event_notifier_dummy_cb, |
4a1cba38 | 419 | event_notifier_poll); |
0187f5c9 PB |
420 | #ifdef CONFIG_LINUX_AIO |
421 | ctx->linux_aio = NULL; | |
422 | #endif | |
9b34277d | 423 | ctx->thread_pool = NULL; |
3fe71223 | 424 | qemu_rec_mutex_init(&ctx->lock); |
d5541d86 | 425 | timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx); |
2f4dc3c1 | 426 | |
82a41186 | 427 | ctx->poll_ns = 0; |
4a1cba38 | 428 | ctx->poll_max_ns = 0; |
82a41186 SH |
429 | ctx->poll_grow = 0; |
430 | ctx->poll_shrink = 0; | |
4a1cba38 | 431 | |
2f4dc3c1 | 432 | return ctx; |
37fcee5d FZ |
433 | fail: |
434 | g_source_destroy(&ctx->source); | |
435 | return NULL; | |
e3713e00 PB |
436 | } |
437 | ||
0c330a73 PB |
438 | void aio_co_schedule(AioContext *ctx, Coroutine *co) |
439 | { | |
440 | trace_aio_co_schedule(ctx, co); | |
441 | QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines, | |
442 | co, co_scheduled_next); | |
443 | qemu_bh_schedule(ctx->co_schedule_bh); | |
444 | } | |
445 | ||
446 | void aio_co_wake(struct Coroutine *co) | |
447 | { | |
448 | AioContext *ctx; | |
449 | ||
450 | /* Read coroutine before co->ctx. Matches smp_wmb in | |
451 | * qemu_coroutine_enter. | |
452 | */ | |
453 | smp_read_barrier_depends(); | |
454 | ctx = atomic_read(&co->ctx); | |
455 | ||
456 | if (ctx != qemu_get_current_aio_context()) { | |
457 | aio_co_schedule(ctx, co); | |
458 | return; | |
459 | } | |
460 | ||
461 | if (qemu_in_coroutine()) { | |
462 | Coroutine *self = qemu_coroutine_self(); | |
463 | assert(self != co); | |
464 | QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next); | |
465 | } else { | |
466 | aio_context_acquire(ctx); | |
467 | qemu_coroutine_enter(co); | |
468 | aio_context_release(ctx); | |
469 | } | |
470 | } | |
471 | ||
e3713e00 PB |
472 | void aio_context_ref(AioContext *ctx) |
473 | { | |
474 | g_source_ref(&ctx->source); | |
475 | } | |
476 | ||
477 | void aio_context_unref(AioContext *ctx) | |
478 | { | |
479 | g_source_unref(&ctx->source); | |
f627aab1 | 480 | } |
98563fc3 SH |
481 | |
482 | void aio_context_acquire(AioContext *ctx) | |
483 | { | |
3fe71223 | 484 | qemu_rec_mutex_lock(&ctx->lock); |
98563fc3 SH |
485 | } |
486 | ||
487 | void aio_context_release(AioContext *ctx) | |
488 | { | |
3fe71223 | 489 | qemu_rec_mutex_unlock(&ctx->lock); |
98563fc3 | 490 | } |