blob: e106072a44dfea02c67fdf7f5943fab29e609ca5 [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
25#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010026#include "block/aio.h"
Stefan Hajnoczi9b342772013-03-07 13:41:47 +010027#include "block/thread-pool.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/main-loop.h"
Paolo Bonzini0ceb8492014-07-07 15:18:04 +020029#include "qemu/atomic.h"
Kevin Wolf9a1e9482009-10-22 17:54:38 +020030
Kevin Wolf4f999d02009-10-22 17:54:37 +020031/***********************************************************/
32/* bottom halves (can be seen as timers which expire ASAP) */
33
34struct QEMUBH {
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +020035 AioContext *ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +020036 QEMUBHFunc *cb;
37 void *opaque;
Kevin Wolf4f999d02009-10-22 17:54:37 +020038 QEMUBH *next;
Stefan Weil9b47b172012-04-29 19:08:45 +020039 bool scheduled;
40 bool idle;
41 bool deleted;
Kevin Wolf4f999d02009-10-22 17:54:37 +020042};
43
Paolo Bonzinif627aab2012-10-29 23:45:23 +010044QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
Kevin Wolf4f999d02009-10-22 17:54:37 +020045{
46 QEMUBH *bh;
Paolo Bonziniee823102014-12-17 16:10:00 +010047 bh = g_new(QEMUBH, 1);
48 *bh = (QEMUBH){
49 .ctx = ctx,
50 .cb = cb,
51 .opaque = opaque,
52 };
Liu Ping Fandcc772e2013-07-16 12:28:58 +080053 qemu_mutex_lock(&ctx->bh_lock);
Paolo Bonzinif627aab2012-10-29 23:45:23 +010054 bh->next = ctx->first_bh;
Liu Ping Fandcc772e2013-07-16 12:28:58 +080055 /* Make sure that the members are ready before putting bh into list */
56 smp_wmb();
Paolo Bonzinif627aab2012-10-29 23:45:23 +010057 ctx->first_bh = bh;
Liu Ping Fandcc772e2013-07-16 12:28:58 +080058 qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf4f999d02009-10-22 17:54:37 +020059 return bh;
60}
61
Pavel Dovgalyukdf281b82015-09-17 19:24:50 +030062void aio_bh_call(QEMUBH *bh)
63{
64 bh->cb(bh->opaque);
65}
66
Liu Ping Fandcc772e2013-07-16 12:28:58 +080067/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010068int aio_bh_poll(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +020069{
Kevin Wolf7887f622011-06-07 17:51:21 +020070 QEMUBH *bh, **bhp, *next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020071 int ret;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020072
Paolo Bonzinif627aab2012-10-29 23:45:23 +010073 ctx->walking_bh++;
Kevin Wolf4f999d02009-10-22 17:54:37 +020074
75 ret = 0;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010076 for (bh = ctx->first_bh; bh; bh = next) {
Liu Ping Fandcc772e2013-07-16 12:28:58 +080077 /* Make sure that fetching bh happens before accessing its members */
78 smp_read_barrier_depends();
Kevin Wolf7887f622011-06-07 17:51:21 +020079 next = bh->next;
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +020080 /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
81 * implicit memory barrier ensures that the callback sees all writes
82 * done by the scheduling thread. It also ensures that the scheduling
83 * thread sees the zero before bh->cb has run, and thus will call
84 * aio_notify again if necessary.
85 */
86 if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
Stefan Hajnoczica96ac42015-07-28 18:34:09 +020087 /* Idle BHs and the notify BH don't count as progress */
88 if (!bh->idle && bh != ctx->notify_dummy_bh) {
Kevin Wolf4f999d02009-10-22 17:54:37 +020089 ret = 1;
Stefan Hajnoczica96ac42015-07-28 18:34:09 +020090 }
Kevin Wolf4f999d02009-10-22 17:54:37 +020091 bh->idle = 0;
Pavel Dovgalyukdf281b82015-09-17 19:24:50 +030092 aio_bh_call(bh);
Kevin Wolf4f999d02009-10-22 17:54:37 +020093 }
94 }
95
Paolo Bonzinif627aab2012-10-29 23:45:23 +010096 ctx->walking_bh--;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020097
Kevin Wolf4f999d02009-10-22 17:54:37 +020098 /* remove deleted bhs */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010099 if (!ctx->walking_bh) {
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800100 qemu_mutex_lock(&ctx->bh_lock);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100101 bhp = &ctx->first_bh;
Kevin Wolf648fb0e2011-09-01 16:16:10 +0200102 while (*bhp) {
103 bh = *bhp;
104 if (bh->deleted) {
105 *bhp = bh->next;
106 g_free(bh);
107 } else {
108 bhp = &bh->next;
109 }
110 }
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800111 qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200112 }
113
114 return ret;
115}
116
117void qemu_bh_schedule_idle(QEMUBH *bh)
118{
Kevin Wolf4f999d02009-10-22 17:54:37 +0200119 bh->idle = 1;
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800120 /* Make sure that idle & any writes needed by the callback are done
121 * before the locations are read in the aio_bh_poll.
122 */
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +0200123 atomic_mb_set(&bh->scheduled, 1);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200124}
125
126void qemu_bh_schedule(QEMUBH *bh)
127{
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200128 AioContext *ctx;
129
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200130 ctx = bh->ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200131 bh->idle = 0;
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +0200132 /* The memory barrier implicit in atomic_xchg makes sure that:
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200133 * 1. idle & any writes needed by the callback are done before the
134 * locations are read in the aio_bh_poll.
135 * 2. ctx is loaded before scheduled is set and the callback has a chance
136 * to execute.
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800137 */
Paolo Bonzinie8d3b1a2015-04-07 17:16:19 +0200138 if (atomic_xchg(&bh->scheduled, 1) == 0) {
139 aio_notify(ctx);
140 }
Kevin Wolf4f999d02009-10-22 17:54:37 +0200141}
142
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800143
144/* This func is async.
145 */
Kevin Wolf4f999d02009-10-22 17:54:37 +0200146void qemu_bh_cancel(QEMUBH *bh)
147{
148 bh->scheduled = 0;
149}
150
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800151/* This func is async.The bottom half will do the delete action at the finial
152 * end.
153 */
Kevin Wolf4f999d02009-10-22 17:54:37 +0200154void qemu_bh_delete(QEMUBH *bh)
155{
156 bh->scheduled = 0;
157 bh->deleted = 1;
158}
159
Paolo Bonzini845ca102014-07-09 11:53:01 +0200160int64_t
161aio_compute_timeout(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +0200162{
Paolo Bonzini845ca102014-07-09 11:53:01 +0200163 int64_t deadline;
164 int timeout = -1;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200165 QEMUBH *bh;
166
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100167 for (bh = ctx->first_bh; bh; bh = bh->next) {
Kevin Wolf4f999d02009-10-22 17:54:37 +0200168 if (!bh->deleted && bh->scheduled) {
169 if (bh->idle) {
170 /* idle bottom halves will be polled at least
171 * every 10ms */
Paolo Bonzini845ca102014-07-09 11:53:01 +0200172 timeout = 10000000;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200173 } else {
174 /* non-idle bottom halves will be executed
175 * immediately */
Paolo Bonzini845ca102014-07-09 11:53:01 +0200176 return 0;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200177 }
178 }
179 }
Kevin Wolf4f999d02009-10-22 17:54:37 +0200180
Paolo Bonzini845ca102014-07-09 11:53:01 +0200181 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
Alex Bligh533a8cf2013-08-21 16:02:51 +0100182 if (deadline == 0) {
Paolo Bonzini845ca102014-07-09 11:53:01 +0200183 return 0;
Alex Bligh533a8cf2013-08-21 16:02:51 +0100184 } else {
Paolo Bonzini845ca102014-07-09 11:53:01 +0200185 return qemu_soonest_timeout(timeout, deadline);
Alex Bligh533a8cf2013-08-21 16:02:51 +0100186 }
Paolo Bonzini845ca102014-07-09 11:53:01 +0200187}
Alex Bligh533a8cf2013-08-21 16:02:51 +0100188
Paolo Bonzini845ca102014-07-09 11:53:01 +0200189static gboolean
190aio_ctx_prepare(GSource *source, gint *timeout)
191{
192 AioContext *ctx = (AioContext *) source;
193
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200194 atomic_or(&ctx->notify_me, 1);
195
Paolo Bonzini845ca102014-07-09 11:53:01 +0200196 /* We assume there is no timeout already supplied */
197 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200198
199 if (aio_prepare(ctx)) {
200 *timeout = 0;
201 }
202
Paolo Bonzini845ca102014-07-09 11:53:01 +0200203 return *timeout == 0;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200204}
205
206static gboolean
207aio_ctx_check(GSource *source)
208{
209 AioContext *ctx = (AioContext *) source;
210 QEMUBH *bh;
211
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200212 atomic_and(&ctx->notify_me, ~1);
Paolo Bonzini05e514b2015-07-21 16:07:53 +0200213 aio_notify_accept(ctx);
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200214
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200215 for (bh = ctx->first_bh; bh; bh = bh->next) {
216 if (!bh->deleted && bh->scheduled) {
217 return true;
218 }
219 }
Alex Bligh533a8cf2013-08-21 16:02:51 +0100220 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200221}
222
223static gboolean
224aio_ctx_dispatch(GSource *source,
225 GSourceFunc callback,
226 gpointer user_data)
227{
228 AioContext *ctx = (AioContext *) source;
229
230 assert(callback == NULL);
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200231 aio_dispatch(ctx);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200232 return true;
233}
234
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200235static void
236aio_ctx_finalize(GSource *source)
237{
238 AioContext *ctx = (AioContext *) source;
239
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200240 qemu_bh_delete(ctx->notify_dummy_bh);
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100241 thread_pool_free(ctx->thread_pool);
Stefan Hajnoczia0769722015-07-28 18:34:08 +0200242
243 qemu_mutex_lock(&ctx->bh_lock);
244 while (ctx->first_bh) {
245 QEMUBH *next = ctx->first_bh->next;
246
247 /* qemu_bh_delete() must have been called on BHs in this AioContext */
248 assert(ctx->first_bh->deleted);
249
250 g_free(ctx->first_bh);
251 ctx->first_bh = next;
252 }
253 qemu_mutex_unlock(&ctx->bh_lock);
254
Fam Zhengdca21ef2015-10-23 11:08:05 +0800255 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200256 event_notifier_cleanup(&ctx->notifier);
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100257 rfifolock_destroy(&ctx->lock);
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800258 qemu_mutex_destroy(&ctx->bh_lock);
Alex Blighdae21b92013-08-21 16:02:49 +0100259 timerlistgroup_deinit(&ctx->tlg);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200260}
261
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200262static GSourceFuncs aio_source_funcs = {
263 aio_ctx_prepare,
264 aio_ctx_check,
265 aio_ctx_dispatch,
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200266 aio_ctx_finalize
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200267};
268
269GSource *aio_get_g_source(AioContext *ctx)
270{
271 g_source_ref(&ctx->source);
272 return &ctx->source;
273}
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200274
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100275ThreadPool *aio_get_thread_pool(AioContext *ctx)
276{
277 if (!ctx->thread_pool) {
278 ctx->thread_pool = thread_pool_new(ctx);
279 }
280 return ctx->thread_pool;
281}
282
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200283void aio_notify(AioContext *ctx)
284{
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200285 /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
286 * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
287 */
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200288 smp_mb();
Paolo Bonzinieabc9772015-07-21 16:07:51 +0200289 if (ctx->notify_me) {
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200290 event_notifier_set(&ctx->notifier);
Paolo Bonzini05e514b2015-07-21 16:07:53 +0200291 atomic_mb_set(&ctx->notified, true);
292 }
293}
294
295void aio_notify_accept(AioContext *ctx)
296{
297 if (atomic_xchg(&ctx->notified, false)) {
298 event_notifier_test_and_clear(&ctx->notifier);
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200299 }
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200300}
301
Alex Blighd5541d82013-08-21 16:02:50 +0100302static void aio_timerlist_notify(void *opaque)
303{
304 aio_notify(opaque);
305}
306
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100307static void aio_rfifolock_cb(void *opaque)
308{
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200309 AioContext *ctx = opaque;
310
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100311 /* Kick owner thread in case they are blocked in aio_poll() */
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200312 qemu_bh_schedule(ctx->notify_dummy_bh);
313}
314
315static void notify_dummy_bh(void *opaque)
316{
317 /* Do nothing, we were invoked just to force the event loop to iterate */
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100318}
319
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200320static void event_notifier_dummy_cb(EventNotifier *e)
321{
322}
323
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300324AioContext *aio_context_new(Error **errp)
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100325{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300326 int ret;
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200327 AioContext *ctx;
Fam Zheng37fcee52015-10-30 12:06:28 +0800328 Error *local_err = NULL;
329
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200330 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
Fam Zheng37fcee52015-10-30 12:06:28 +0800331 aio_context_setup(ctx, &local_err);
332 if (local_err) {
333 error_propagate(errp, local_err);
334 goto fail;
335 }
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300336 ret = event_notifier_init(&ctx->notifier, false);
337 if (ret < 0) {
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300338 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
Fam Zheng37fcee52015-10-30 12:06:28 +0800339 goto fail;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300340 }
Paolo Bonzinifcf5def2014-12-17 16:09:58 +0100341 g_source_set_can_recurse(&ctx->source, true);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300342 aio_set_event_notifier(ctx, &ctx->notifier,
Fam Zhengdca21ef2015-10-23 11:08:05 +0800343 false,
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300344 (EventNotifierHandler *)
Paolo Bonzini21a03d12015-07-21 16:07:52 +0200345 event_notifier_dummy_cb);
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100346 ctx->thread_pool = NULL;
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800347 qemu_mutex_init(&ctx->bh_lock);
Stefan Hajnoczida5e1de2015-06-03 10:15:33 +0100348 rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
Alex Blighd5541d82013-08-21 16:02:50 +0100349 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200350
Stefan Hajnoczica96ac42015-07-28 18:34:09 +0200351 ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
352
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200353 return ctx;
Fam Zheng37fcee52015-10-30 12:06:28 +0800354fail:
355 g_source_destroy(&ctx->source);
356 return NULL;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200357}
358
359void aio_context_ref(AioContext *ctx)
360{
361 g_source_ref(&ctx->source);
362}
363
364void aio_context_unref(AioContext *ctx)
365{
366 g_source_unref(&ctx->source);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100367}
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100368
369void aio_context_acquire(AioContext *ctx)
370{
371 rfifolock_lock(&ctx->lock);
372}
373
374void aio_context_release(AioContext *ctx)
375{
376 rfifolock_unlock(&ctx->lock);
377}