blob: 2be88cc9e9a28093937423fc7e39474c0cd825bb [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
Liu Ping Fandcc772e2013-07-16 12:28:58 +080062/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010063int aio_bh_poll(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +020064{
Kevin Wolf7887f622011-06-07 17:51:21 +020065 QEMUBH *bh, **bhp, *next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020066 int ret;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020067
Paolo Bonzinif627aab2012-10-29 23:45:23 +010068 ctx->walking_bh++;
Kevin Wolf4f999d02009-10-22 17:54:37 +020069
70 ret = 0;
Paolo Bonzinif627aab2012-10-29 23:45:23 +010071 for (bh = ctx->first_bh; bh; bh = next) {
Liu Ping Fandcc772e2013-07-16 12:28:58 +080072 /* Make sure that fetching bh happens before accessing its members */
73 smp_read_barrier_depends();
Kevin Wolf7887f622011-06-07 17:51:21 +020074 next = bh->next;
Kevin Wolf4f999d02009-10-22 17:54:37 +020075 if (!bh->deleted && bh->scheduled) {
76 bh->scheduled = 0;
Liu Ping Fandcc772e2013-07-16 12:28:58 +080077 /* Paired with write barrier in bh schedule to ensure reading for
78 * idle & callbacks coming after bh's scheduling.
79 */
80 smp_rmb();
Kevin Wolf4f999d02009-10-22 17:54:37 +020081 if (!bh->idle)
82 ret = 1;
83 bh->idle = 0;
84 bh->cb(bh->opaque);
85 }
86 }
87
Paolo Bonzinif627aab2012-10-29 23:45:23 +010088 ctx->walking_bh--;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020089
Kevin Wolf4f999d02009-10-22 17:54:37 +020090 /* remove deleted bhs */
Paolo Bonzinif627aab2012-10-29 23:45:23 +010091 if (!ctx->walking_bh) {
Liu Ping Fandcc772e2013-07-16 12:28:58 +080092 qemu_mutex_lock(&ctx->bh_lock);
Paolo Bonzinif627aab2012-10-29 23:45:23 +010093 bhp = &ctx->first_bh;
Kevin Wolf648fb0e2011-09-01 16:16:10 +020094 while (*bhp) {
95 bh = *bhp;
96 if (bh->deleted) {
97 *bhp = bh->next;
98 g_free(bh);
99 } else {
100 bhp = &bh->next;
101 }
102 }
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800103 qemu_mutex_unlock(&ctx->bh_lock);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200104 }
105
106 return ret;
107}
108
109void qemu_bh_schedule_idle(QEMUBH *bh)
110{
111 if (bh->scheduled)
112 return;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200113 bh->idle = 1;
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800114 /* Make sure that idle & any writes needed by the callback are done
115 * before the locations are read in the aio_bh_poll.
116 */
117 smp_wmb();
118 bh->scheduled = 1;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200119}
120
121void qemu_bh_schedule(QEMUBH *bh)
122{
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200123 AioContext *ctx;
124
Kevin Wolf4f999d02009-10-22 17:54:37 +0200125 if (bh->scheduled)
126 return;
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200127 ctx = bh->ctx;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200128 bh->idle = 0;
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200129 /* Make sure that:
130 * 1. idle & any writes needed by the callback are done before the
131 * locations are read in the aio_bh_poll.
132 * 2. ctx is loaded before scheduled is set and the callback has a chance
133 * to execute.
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800134 */
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200135 smp_mb();
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800136 bh->scheduled = 1;
Stefan Hajnoczi924fe122014-06-03 11:21:01 +0200137 aio_notify(ctx);
Kevin Wolf4f999d02009-10-22 17:54:37 +0200138}
139
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800140
141/* This func is async.
142 */
Kevin Wolf4f999d02009-10-22 17:54:37 +0200143void qemu_bh_cancel(QEMUBH *bh)
144{
145 bh->scheduled = 0;
146}
147
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800148/* This func is async.The bottom half will do the delete action at the finial
149 * end.
150 */
Kevin Wolf4f999d02009-10-22 17:54:37 +0200151void qemu_bh_delete(QEMUBH *bh)
152{
153 bh->scheduled = 0;
154 bh->deleted = 1;
155}
156
Paolo Bonzini845ca102014-07-09 11:53:01 +0200157int64_t
158aio_compute_timeout(AioContext *ctx)
Kevin Wolf4f999d02009-10-22 17:54:37 +0200159{
Paolo Bonzini845ca102014-07-09 11:53:01 +0200160 int64_t deadline;
161 int timeout = -1;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200162 QEMUBH *bh;
163
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100164 for (bh = ctx->first_bh; bh; bh = bh->next) {
Kevin Wolf4f999d02009-10-22 17:54:37 +0200165 if (!bh->deleted && bh->scheduled) {
166 if (bh->idle) {
167 /* idle bottom halves will be polled at least
168 * every 10ms */
Paolo Bonzini845ca102014-07-09 11:53:01 +0200169 timeout = 10000000;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200170 } else {
171 /* non-idle bottom halves will be executed
172 * immediately */
Paolo Bonzini845ca102014-07-09 11:53:01 +0200173 return 0;
Kevin Wolf4f999d02009-10-22 17:54:37 +0200174 }
175 }
176 }
Kevin Wolf4f999d02009-10-22 17:54:37 +0200177
Paolo Bonzini845ca102014-07-09 11:53:01 +0200178 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
Alex Bligh533a8cf2013-08-21 16:02:51 +0100179 if (deadline == 0) {
Paolo Bonzini845ca102014-07-09 11:53:01 +0200180 return 0;
Alex Bligh533a8cf2013-08-21 16:02:51 +0100181 } else {
Paolo Bonzini845ca102014-07-09 11:53:01 +0200182 return qemu_soonest_timeout(timeout, deadline);
Alex Bligh533a8cf2013-08-21 16:02:51 +0100183 }
Paolo Bonzini845ca102014-07-09 11:53:01 +0200184}
Alex Bligh533a8cf2013-08-21 16:02:51 +0100185
Paolo Bonzini845ca102014-07-09 11:53:01 +0200186static gboolean
187aio_ctx_prepare(GSource *source, gint *timeout)
188{
189 AioContext *ctx = (AioContext *) source;
190
191 /* We assume there is no timeout already supplied */
192 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
Paolo Bonzinia3462c62014-07-09 11:53:08 +0200193
194 if (aio_prepare(ctx)) {
195 *timeout = 0;
196 }
197
Paolo Bonzini845ca102014-07-09 11:53:01 +0200198 return *timeout == 0;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200199}
200
201static gboolean
202aio_ctx_check(GSource *source)
203{
204 AioContext *ctx = (AioContext *) source;
205 QEMUBH *bh;
206
207 for (bh = ctx->first_bh; bh; bh = bh->next) {
208 if (!bh->deleted && bh->scheduled) {
209 return true;
210 }
211 }
Alex Bligh533a8cf2013-08-21 16:02:51 +0100212 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200213}
214
215static gboolean
216aio_ctx_dispatch(GSource *source,
217 GSourceFunc callback,
218 gpointer user_data)
219{
220 AioContext *ctx = (AioContext *) source;
221
222 assert(callback == NULL);
Paolo Bonzinie4c7e2d2014-07-09 11:53:05 +0200223 aio_dispatch(ctx);
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200224 return true;
225}
226
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200227static void
228aio_ctx_finalize(GSource *source)
229{
230 AioContext *ctx = (AioContext *) source;
231
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100232 thread_pool_free(ctx->thread_pool);
Stefan Hajnoczif2e5dca2013-04-11 17:26:25 +0200233 aio_set_event_notifier(ctx, &ctx->notifier, NULL);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200234 event_notifier_cleanup(&ctx->notifier);
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100235 rfifolock_destroy(&ctx->lock);
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800236 qemu_mutex_destroy(&ctx->bh_lock);
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100237 g_array_free(ctx->pollfds, TRUE);
Alex Blighdae21b92013-08-21 16:02:49 +0100238 timerlistgroup_deinit(&ctx->tlg);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200239}
240
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200241static GSourceFuncs aio_source_funcs = {
242 aio_ctx_prepare,
243 aio_ctx_check,
244 aio_ctx_dispatch,
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200245 aio_ctx_finalize
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200246};
247
248GSource *aio_get_g_source(AioContext *ctx)
249{
250 g_source_ref(&ctx->source);
251 return &ctx->source;
252}
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200253
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100254ThreadPool *aio_get_thread_pool(AioContext *ctx)
255{
256 if (!ctx->thread_pool) {
257 ctx->thread_pool = thread_pool_new(ctx);
258 }
259 return ctx->thread_pool;
260}
261
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200262void aio_set_dispatching(AioContext *ctx, bool dispatching)
263{
264 ctx->dispatching = dispatching;
265 if (!dispatching) {
266 /* Write ctx->dispatching before reading e.g. bh->scheduled.
267 * Optimization: this is only needed when we're entering the "unsafe"
268 * phase where other threads must call event_notifier_set.
269 */
270 smp_mb();
271 }
272}
273
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200274void aio_notify(AioContext *ctx)
275{
Paolo Bonzini0ceb8492014-07-07 15:18:04 +0200276 /* Write e.g. bh->scheduled before reading ctx->dispatching. */
277 smp_mb();
278 if (!ctx->dispatching) {
279 event_notifier_set(&ctx->notifier);
280 }
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200281}
282
Alex Blighd5541d82013-08-21 16:02:50 +0100283static void aio_timerlist_notify(void *opaque)
284{
285 aio_notify(opaque);
286}
287
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100288static void aio_rfifolock_cb(void *opaque)
289{
290 /* Kick owner thread in case they are blocked in aio_poll() */
291 aio_notify(opaque);
292}
293
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300294AioContext *aio_context_new(Error **errp)
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100295{
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300296 int ret;
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200297 AioContext *ctx;
298 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300299 ret = event_notifier_init(&ctx->notifier, false);
300 if (ret < 0) {
301 g_source_destroy(&ctx->source);
302 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
303 return NULL;
304 }
Paolo Bonzinifcf5def2014-12-17 16:09:58 +0100305 g_source_set_can_recurse(&ctx->source, true);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300306 aio_set_event_notifier(ctx, &ctx->notifier,
307 (EventNotifierHandler *)
308 event_notifier_test_and_clear);
Stefan Hajnoczi6b5f8762013-02-20 11:28:32 +0100309 ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
Stefan Hajnoczi9b342772013-03-07 13:41:47 +0100310 ctx->thread_pool = NULL;
Liu Ping Fandcc772e2013-07-16 12:28:58 +0800311 qemu_mutex_init(&ctx->bh_lock);
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100312 rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
Alex Blighd5541d82013-08-21 16:02:50 +0100313 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
Paolo Bonzini2f4dc3c2012-09-24 18:44:14 +0200314
315 return ctx;
Paolo Bonzinie3713e02012-09-24 14:57:41 +0200316}
317
318void aio_context_ref(AioContext *ctx)
319{
320 g_source_ref(&ctx->source);
321}
322
323void aio_context_unref(AioContext *ctx)
324{
325 g_source_unref(&ctx->source);
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100326}
Stefan Hajnoczi98563fc2014-03-03 11:30:04 +0100327
328void aio_context_acquire(AioContext *ctx)
329{
330 rfifolock_lock(&ctx->lock);
331}
332
333void aio_context_release(AioContext *ctx)
334{
335 rfifolock_unlock(&ctx->lock);
336}