blob: d4dd2cc799283ecbbcad2ddd3a271e77685863dc [file] [log] [blame]
Kevin Wolf4f999d02009-10-22 17:54:37 +02001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
Peter Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Kevin Wolf4f999d02009-10-22 17:54:37 +020026#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010027#include "block/aio.h"
Stefan Hajnoczi9b342772013-03-07 13:41:47 +010028#include "block/thread-pool.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/main-loop.h"
Paolo Bonzini0ceb8492014-07-07 15:18:04 +020030#include "qemu/atomic.h"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020031
Kevin Wolf4f999d02009-10-22 17:54:37 +020032/***********************************************************/
33/* bottom halves (can be seen as timers which expire ASAP) */
34
35struct QEMUBH {
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +020036 AioContext *ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +020037 QEMUBHFunc *cb;
38 void *opaque;
Kevin Wolf4f999d02009-10-22 17:54:37 +020039 QEMUBH *next;
Stefan Weil9b47b172012-04-29 19:08:45 +020040 bool scheduled;
41 bool idle;
42 bool deleted;
Kevin Wolf4f999d02009-10-22 17:54:37 +020043};
44
Paolo Bonzinif627aab2012-10-29 23:45:23 +010045QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
Kevin Wolf4f999d02009-10-22 17:54:37 +020046{
47 QEMUBH *bh;
Paolo Bonziniee823102014-12-17 16:10:00 +010048 bh = g_new(QEMUBH, 1);
49 *bh = (QEMUBH){
50 .ctx = ctx,
51 .cb = cb,
52 .opaque = opaque,
53 };
Liu Ping Fandcc772e2013-07-16 12:28:58 +080054 qemu_mutex_lock(&ctx->bh_lock);
Paolo Bonzinif627aab2012-10-29 23:45:23 +010055 bh->next = ctx->first_bh;
Liu Ping Fandcc772e2013-07-16 12:28:58 +080056 /* Make sure that the members are ready before putting bh into list */
57 smp_wmb();
Paolo Bonzinif627aab2012-10-29 23:45:23 +010058 ctx->first_bh = bh;
Liu Ping Fandcc772e2013-07-16 12:28:58 +080059 qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf4f999d02009-10-22 17:54:37 +020060 return bh;
61}
62
Pavel Dovgalyukdf281b82015-09-17 19:24:50 +030063void aio_bh_call(QEMUBH *bh)
64{
65 bh->cb(bh->opaque);
66}
67
Liu Ping Fandcc772e2013-07-16 12:28:58 +080068/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010069int aio_bh_poll(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +020070{
Kevin Wolf7887f622011-06-07 17:51:21 +020071 QEMUBH *bh, **bhp, *next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020072 int ret;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020073
Paolo Bonzinif627aab2012-10-29 23:45:23 +010074 ctx->walking_bh++;
Kevin Wolf4f999d02009-10-22 17:54:37 +020075
76 ret = 0;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010077 for (bh = ctx->first_bh; bh; bh = next) {
Liu Ping Fandcc772e2013-07-16 12:28:58 +080078 /* Make sure that fetching bh happens before accessing its members */
79 smp_read_barrier_depends();
Kevin Wolf7887f622011-06-07 17:51:21 +020080 next = bh->next;
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +020081 /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
82 * implicit memory barrier ensures that the callback sees all writes
83 * done by the scheduling thread. It also ensures that the scheduling
84 * thread sees the zero before bh->cb has run, and thus will call
85 * aio_notify again if necessary.
86 */
87 if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
Stefan Hajnoczica96ac42015-07-28 18:34:09 +020088 /* Idle BHs and the notify BH don't count as progress */
89 if (!bh->idle && bh != ctx->notify_dummy_bh) {
Kevin Wolf4f999d02009-10-22 17:54:37 +020090 ret = 1;
Stefan Hajnoczica96ac42015-07-28 18:34:09 +020091 }
Kevin Wolf4f999d02009-10-22 17:54:37 +020092 bh->idle = 0;
Pavel Dovgalyukdf281b82015-09-17 19:24:50 +030093 aio_bh_call(bh);
Kevin Wolf4f999d02009-10-22 17:54:37 +020094 }
95 }
96
Paolo Bonzinif627aab2012-10-29 23:45:23 +010097 ctx->walking_bh--;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020098
Kevin Wolf4f999d02009-10-22 17:54:37 +020099 /* remove deleted bhs */
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100100 if (!ctx->walking_bh) {
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800101 qemu_mutex_lock(&ctx->bh_lock);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100102 bhp = &ctx->first_bh;
Kevin Wolf648fb0e2011-09-01 16:16:10 +0200103 while (*bhp) {
104 bh = *bhp;
105 if (bh->deleted) {
106 *bhp = bh->next;
107 g_free(bh);
108 } else {
109 bhp = &bh->next;
110 }
111 }
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800112 qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200113 }
114
115 return ret;
116}
117
118void qemu_bh_schedule_idle(QEMUBH *bh)
119{
Kevin Wolf4f999d02009-10-22 17:54:37 +0200120 bh->idle = 1;
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800121 /* Make sure that idle & any writes needed by the callback are done
122 * before the locations are read in the aio_bh_poll.
123 */
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +0200124 atomic_mb_set(&bh->scheduled, 1);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200125}
126
127void qemu_bh_schedule(QEMUBH *bh)
128{
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200129 AioContext *ctx;
130
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200131 ctx = bh->ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200132 bh->idle = 0;
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +0200133 /* The memory barrier implicit in atomic_xchg makes sure that:
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200134 * 1. idle & any writes needed by the callback are done before the
135 * locations are read in the aio_bh_poll.
136 * 2. ctx is loaded before scheduled is set and the callback has a chance
137 * to execute.
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800138 */
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +0200139 if (atomic_xchg(&bh->scheduled, 1) == 0) {
140 aio_notify(ctx);
141 }
Kevin Wolf4f999d02009-10-22 17:54:37 +0200142}
143
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800144
145/* This func is async.
146 */
Kevin Wolf4f999d02009-10-22 17:54:37 +0200147void qemu_bh_cancel(QEMUBH *bh)
148{
149 bh->scheduled = 0;
150}
151
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800152/* This func is async.The bottom half will do the delete action at the finial
153 * end.
154 */
Kevin Wolf4f999d02009-10-22 17:54:37 +0200155void qemu_bh_delete(QEMUBH *bh)
156{
157 bh->scheduled = 0;
158 bh->deleted = 1;
159}
160
Paolo Bonzini845ca102014-07-09 11:53:01 +0200161int64_t
162aio_compute_timeout(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +0200163{
Paolo Bonzini845ca102014-07-09 11:53:01 +0200164 int64_t deadline;
165 int timeout = -1;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200166 QEMUBH *bh;
167
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100168 for (bh = ctx->first_bh; bh; bh = bh->next) {
Kevin Wolf4f999d02009-10-22 17:54:37 +0200169 if (!bh->deleted && bh->scheduled) {
170 if (bh->idle) {
171 /* idle bottom halves will be polled at least
172 * every 10ms */
Paolo Bonzini845ca102014-07-09 11:53:01 +0200173 timeout = 10000000;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200174 } else {
175 /* non-idle bottom halves will be executed
176 * immediately */
Paolo Bonzini845ca102014-07-09 11:53:01 +0200177 return 0;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200178 }
179 }
180 }
Kevin Wolf4f999d02009-10-22 17:54:37 +0200181
Paolo Bonzini845ca102014-07-09 11:53:01 +0200182 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
Alex Bligh533a8cf2013-08-21 16:02:51 +0100183 if (deadline == 0) {
Paolo Bonzini845ca102014-07-09 11:53:01 +0200184 return 0;
Alex Bligh533a8cf2013-08-21 16:02:51 +0100185 } else {
Paolo Bonzini845ca102014-07-09 11:53:01 +0200186 return qemu_soonest_timeout(timeout, deadline);
Alex Bligh533a8cf2013-08-21 16:02:51 +0100187 }
Paolo Bonzini845ca102014-07-09 11:53:01 +0200188}
Alex Bligh533a8cf2013-08-21 16:02:51 +0100189
Paolo Bonzini845ca102014-07-09 11:53:01 +0200190static gboolean
191aio_ctx_prepare(GSource *source, gint *timeout)
192{
193 AioContext *ctx = (AioContext *) source;
194
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200195 atomic_or(&ctx->notify_me, 1);
196
Paolo Bonzini845ca102014-07-09 11:53:01 +0200197 /* We assume there is no timeout already supplied */
198 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200199
200 if (aio_prepare(ctx)) {
201 *timeout = 0;
202 }
203
Paolo Bonzini845ca102014-07-09 11:53:01 +0200204 return *timeout == 0;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200205}
206
207static gboolean
208aio_ctx_check(GSource *source)
209{
210 AioContext *ctx = (AioContext *) source;
211 QEMUBH *bh;
212
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200213 atomic_and(&ctx->notify_me, ~1);
Paolo Bonzini05e514b2015-07-21 16:07:53 +0200214 aio_notify_accept(ctx);
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200215
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200216 for (bh = ctx->first_bh; bh; bh = bh->next) {
217 if (!bh->deleted && bh->scheduled) {
218 return true;
219 }
220 }
Alex Bligh533a8cf2013-08-21 16:02:51 +0100221 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200222}
223
224static gboolean
225aio_ctx_dispatch(GSource *source,
226 GSourceFunc callback,
227 gpointer user_data)
228{
229 AioContext *ctx = (AioContext *) source;
230
231 assert(callback == NULL);
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200232 aio_dispatch(ctx);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200233 return true;
234}
235
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200236static void
237aio_ctx_finalize(GSource *source)
238{
239 AioContext *ctx = (AioContext *) source;
240
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200241 qemu_bh_delete(ctx->notify_dummy_bh);
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100242 thread_pool_free(ctx->thread_pool);
Stefan Hajnoczia0769722015-07-28 18:34:08 +0200243
244 qemu_mutex_lock(&ctx->bh_lock);
245 while (ctx->first_bh) {
246 QEMUBH *next = ctx->first_bh->next;
247
248 /* qemu_bh_delete() must have been called on BHs in this AioContext */
249 assert(ctx->first_bh->deleted);
250
251 g_free(ctx->first_bh);
252 ctx->first_bh = next;
253 }
254 qemu_mutex_unlock(&ctx->bh_lock);
255
Fam Zhengdca21ef2015-10-23 11:08:05 +0800256 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200257 event_notifier_cleanup(&ctx->notifier);
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100258 rfifolock_destroy(&ctx->lock);
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800259 qemu_mutex_destroy(&ctx->bh_lock);
Alex Blighdae21b92013-08-21 16:02:49 +0100260 timerlistgroup_deinit(&ctx->tlg);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200261}
262
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200263static GSourceFuncs aio_source_funcs = {
264 aio_ctx_prepare,
265 aio_ctx_check,
266 aio_ctx_dispatch,
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200267 aio_ctx_finalize
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200268};
269
270GSource *aio_get_g_source(AioContext *ctx)
271{
272 g_source_ref(&ctx->source);
273 return &ctx->source;
274}
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200275
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100276ThreadPool *aio_get_thread_pool(AioContext *ctx)
277{
278 if (!ctx->thread_pool) {
279 ctx->thread_pool = thread_pool_new(ctx);
280 }
281 return ctx->thread_pool;
282}
283
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200284void aio_notify(AioContext *ctx)
285{
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200286 /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
287 * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
288 */
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200289 smp_mb();
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200290 if (ctx->notify_me) {
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200291 event_notifier_set(&ctx->notifier);
Paolo Bonzini05e514b2015-07-21 16:07:53 +0200292 atomic_mb_set(&ctx->notified, true);
293 }
294}
295
296void aio_notify_accept(AioContext *ctx)
297{
298 if (atomic_xchg(&ctx->notified, false)) {
299 event_notifier_test_and_clear(&ctx->notifier);
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200300 }
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200301}
302
Alex Blighd5541d82013-08-21 16:02:50 +0100303static void aio_timerlist_notify(void *opaque)
304{
305 aio_notify(opaque);
306}
307
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100308static void aio_rfifolock_cb(void *opaque)
309{
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200310 AioContext *ctx = opaque;
311
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100312 /* Kick owner thread in case they are blocked in aio_poll() */
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200313 qemu_bh_schedule(ctx->notify_dummy_bh);
314}
315
316static void notify_dummy_bh(void *opaque)
317{
318 /* Do nothing, we were invoked just to force the event loop to iterate */
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100319}
320
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200321static void event_notifier_dummy_cb(EventNotifier *e)
322{
323}
324
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300325AioContext *aio_context_new(Error **errp)
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100326{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300327 int ret;
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200328 AioContext *ctx;
Fam Zheng37fcee52015-10-30 12:06:28 +0800329 Error *local_err = NULL;
330
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200331 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
Fam Zheng37fcee52015-10-30 12:06:28 +0800332 aio_context_setup(ctx, &local_err);
333 if (local_err) {
334 error_propagate(errp, local_err);
335 goto fail;
336 }
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300337 ret = event_notifier_init(&ctx->notifier, false);
338 if (ret < 0) {
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300339 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
Fam Zheng37fcee52015-10-30 12:06:28 +0800340 goto fail;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300341 }
Paolo Bonzinifcf5def2014-12-17 16:09:58 +0100342 g_source_set_can_recurse(&ctx->source, true);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300343 aio_set_event_notifier(ctx, &ctx->notifier,
Fam Zhengdca21ef2015-10-23 11:08:05 +0800344 false,
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300345 (EventNotifierHandler *)
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200346 event_notifier_dummy_cb);
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100347 ctx->thread_pool = NULL;
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800348 qemu_mutex_init(&ctx->bh_lock);
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100349 rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
Alex Blighd5541d82013-08-21 16:02:50 +0100350 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200351
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200352 ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
353
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200354 return ctx;
Fam Zheng37fcee52015-10-30 12:06:28 +0800355fail:
356 g_source_destroy(&ctx->source);
357 return NULL;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200358}
359
360void aio_context_ref(AioContext *ctx)
361{
362 g_source_ref(&ctx->source);
363}
364
365void aio_context_unref(AioContext *ctx)
366{
367 g_source_unref(&ctx->source);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100368}
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100369
370void aio_context_acquire(AioContext *ctx)
371{
372 rfifolock_lock(&ctx->lock);
373}
374
375void aio_context_release(AioContext *ctx)
376{
377 rfifolock_unlock(&ctx->lock);
378}