Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 1 | /* |
Paolo Bonzini | c2b38b2 | 2017-02-13 14:52:18 +0100 | [diff] [blame] | 2 | * Data plane event loop |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
Paolo Bonzini | c2b38b2 | 2017-02-13 14:52:18 +0100 | [diff] [blame] | 5 | * Copyright (c) 2009-2017 QEMU contributors |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 27 | #include "qapi/error.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 28 | #include "block/aio.h" |
Stefan Hajnoczi | 9b34277 | 2013-03-07 13:41:47 +0100 | [diff] [blame] | 29 | #include "block/thread-pool.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/main-loop.h" |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 31 | #include "qemu/atomic.h" |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 32 | #include "qemu/rcu_queue.h" |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 33 | #include "block/raw-aio.h" |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 34 | #include "qemu/coroutine_int.h" |
| 35 | #include "trace.h" |
Kevin Wolf | 9a1e948 | 2009-10-22 17:54:38 +0200 | [diff] [blame] | 36 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 37 | /***********************************************************/ |
| 38 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 39 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 40 | /* QEMUBH::flags values */ |
| 41 | enum { |
| 42 | /* Already enqueued and waiting for aio_bh_poll() */ |
| 43 | BH_PENDING = (1 << 0), |
| 44 | |
| 45 | /* Invoke the callback */ |
| 46 | BH_SCHEDULED = (1 << 1), |
| 47 | |
| 48 | /* Delete without invoking callback */ |
| 49 | BH_DELETED = (1 << 2), |
| 50 | |
| 51 | /* Delete after invoking callback */ |
| 52 | BH_ONESHOT = (1 << 3), |
| 53 | |
| 54 | /* Schedule periodically when the event loop is idle */ |
| 55 | BH_IDLE = (1 << 4), |
| 56 | }; |
| 57 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 58 | struct QEMUBH { |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 59 | AioContext *ctx; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 60 | QEMUBHFunc *cb; |
| 61 | void *opaque; |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 62 | QSLIST_ENTRY(QEMUBH) next; |
| 63 | unsigned flags; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 66 | /* Called concurrently from any thread */ |
| 67 | static void aio_bh_enqueue(QEMUBH *bh, unsigned new_flags) |
| 68 | { |
| 69 | AioContext *ctx = bh->ctx; |
| 70 | unsigned old_flags; |
| 71 | |
| 72 | /* |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 73 | * The memory barrier implicit in qatomic_fetch_or makes sure that: |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 74 | * 1. idle & any writes needed by the callback are done before the |
| 75 | * locations are read in the aio_bh_poll. |
| 76 | * 2. ctx is loaded before the callback has a chance to execute and bh |
| 77 | * could be freed. |
| 78 | */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 79 | old_flags = qatomic_fetch_or(&bh->flags, BH_PENDING | new_flags); |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 80 | if (!(old_flags & BH_PENDING)) { |
| 81 | QSLIST_INSERT_HEAD_ATOMIC(&ctx->bh_list, bh, next); |
| 82 | } |
| 83 | |
| 84 | aio_notify(ctx); |
| 85 | } |
| 86 | |
| 87 | /* Only called from aio_bh_poll() and aio_ctx_finalize() */ |
| 88 | static QEMUBH *aio_bh_dequeue(BHList *head, unsigned *flags) |
| 89 | { |
| 90 | QEMUBH *bh = QSLIST_FIRST_RCU(head); |
| 91 | |
| 92 | if (!bh) { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | QSLIST_REMOVE_HEAD(head, next); |
| 97 | |
| 98 | /* |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 99 | * The qatomic_and is paired with aio_bh_enqueue(). The implicit memory |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 100 | * barrier ensures that the callback sees all writes done by the scheduling |
| 101 | * thread. It also ensures that the scheduling thread sees the cleared |
| 102 | * flag before bh->cb has run, and thus will call aio_notify again if |
| 103 | * necessary. |
| 104 | */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 105 | *flags = qatomic_fetch_and(&bh->flags, |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 106 | ~(BH_PENDING | BH_SCHEDULED | BH_IDLE)); |
| 107 | return bh; |
| 108 | } |
| 109 | |
Paolo Bonzini | 5b8bb35 | 2016-10-03 18:14:15 +0200 | [diff] [blame] | 110 | void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque) |
| 111 | { |
| 112 | QEMUBH *bh; |
| 113 | bh = g_new(QEMUBH, 1); |
| 114 | *bh = (QEMUBH){ |
| 115 | .ctx = ctx, |
| 116 | .cb = cb, |
| 117 | .opaque = opaque, |
| 118 | }; |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 119 | aio_bh_enqueue(bh, BH_SCHEDULED | BH_ONESHOT); |
Paolo Bonzini | 5b8bb35 | 2016-10-03 18:14:15 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 122 | QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 123 | { |
| 124 | QEMUBH *bh; |
Paolo Bonzini | ee82310 | 2014-12-17 16:10:00 +0100 | [diff] [blame] | 125 | bh = g_new(QEMUBH, 1); |
| 126 | *bh = (QEMUBH){ |
| 127 | .ctx = ctx, |
| 128 | .cb = cb, |
| 129 | .opaque = opaque, |
| 130 | }; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 131 | return bh; |
| 132 | } |
| 133 | |
Pavel Dovgalyuk | df281b8 | 2015-09-17 19:24:50 +0300 | [diff] [blame] | 134 | void aio_bh_call(QEMUBH *bh) |
| 135 | { |
| 136 | bh->cb(bh->opaque); |
| 137 | } |
| 138 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 139 | /* Multiple occurrences of aio_bh_poll cannot be called concurrently. */ |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 140 | int aio_bh_poll(AioContext *ctx) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 141 | { |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 142 | BHListSlice slice; |
| 143 | BHListSlice *s; |
| 144 | int ret = 0; |
Kevin Wolf | 648fb0e | 2011-09-01 16:16:10 +0200 | [diff] [blame] | 145 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 146 | QSLIST_MOVE_ATOMIC(&slice.bh_list, &ctx->bh_list); |
| 147 | QSIMPLEQ_INSERT_TAIL(&ctx->bh_slice_list, &slice, next); |
| 148 | |
| 149 | while ((s = QSIMPLEQ_FIRST(&ctx->bh_slice_list))) { |
| 150 | QEMUBH *bh; |
| 151 | unsigned flags; |
| 152 | |
| 153 | bh = aio_bh_dequeue(&s->bh_list, &flags); |
| 154 | if (!bh) { |
| 155 | QSIMPLEQ_REMOVE_HEAD(&ctx->bh_slice_list, next); |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | if ((flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { |
Paolo Bonzini | 65c1b5b | 2016-10-27 12:49:06 +0200 | [diff] [blame] | 160 | /* Idle BHs don't count as progress */ |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 161 | if (!(flags & BH_IDLE)) { |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 162 | ret = 1; |
Stefan Hajnoczi | ca96ac4 | 2015-07-28 18:34:09 +0200 | [diff] [blame] | 163 | } |
Pavel Dovgalyuk | df281b8 | 2015-09-17 19:24:50 +0300 | [diff] [blame] | 164 | aio_bh_call(bh); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 165 | } |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 166 | if (flags & (BH_DELETED | BH_ONESHOT)) { |
| 167 | g_free(bh); |
Paolo Bonzini | 7d506c9 | 2017-01-12 19:08:00 +0100 | [diff] [blame] | 168 | } |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | void qemu_bh_schedule_idle(QEMUBH *bh) |
| 175 | { |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 176 | aio_bh_enqueue(bh, BH_SCHEDULED | BH_IDLE); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void qemu_bh_schedule(QEMUBH *bh) |
| 180 | { |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 181 | aio_bh_enqueue(bh, BH_SCHEDULED); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Liu Ping Fan | dcc772e | 2013-07-16 12:28:58 +0800 | [diff] [blame] | 184 | /* This func is async. |
| 185 | */ |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 186 | void qemu_bh_cancel(QEMUBH *bh) |
| 187 | { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 188 | qatomic_and(&bh->flags, ~BH_SCHEDULED); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Liu Ping Fan | dcc772e | 2013-07-16 12:28:58 +0800 | [diff] [blame] | 191 | /* This func is async.The bottom half will do the delete action at the finial |
| 192 | * end. |
| 193 | */ |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 194 | void qemu_bh_delete(QEMUBH *bh) |
| 195 | { |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 196 | aio_bh_enqueue(bh, BH_DELETED); |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 199 | static int64_t aio_compute_bh_timeout(BHList *head, int timeout) |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 200 | { |
| 201 | QEMUBH *bh; |
| 202 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 203 | QSLIST_FOREACH_RCU(bh, head, next) { |
| 204 | if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { |
| 205 | if (bh->flags & BH_IDLE) { |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 206 | /* idle bottom halves will be polled at least |
| 207 | * every 10ms */ |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 208 | timeout = 10000000; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 209 | } else { |
| 210 | /* non-idle bottom halves will be executed |
| 211 | * immediately */ |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 212 | return 0; |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
Kevin Wolf | 4f999d0 | 2009-10-22 17:54:37 +0200 | [diff] [blame] | 216 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 217 | return timeout; |
| 218 | } |
| 219 | |
| 220 | int64_t |
| 221 | aio_compute_timeout(AioContext *ctx) |
| 222 | { |
| 223 | BHListSlice *s; |
| 224 | int64_t deadline; |
| 225 | int timeout = -1; |
| 226 | |
| 227 | timeout = aio_compute_bh_timeout(&ctx->bh_list, timeout); |
| 228 | if (timeout == 0) { |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) { |
| 233 | timeout = aio_compute_bh_timeout(&s->bh_list, timeout); |
| 234 | if (timeout == 0) { |
| 235 | return 0; |
| 236 | } |
| 237 | } |
| 238 | |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 239 | deadline = timerlistgroup_deadline_ns(&ctx->tlg); |
Alex Bligh | 533a8cf | 2013-08-21 16:02:51 +0100 | [diff] [blame] | 240 | if (deadline == 0) { |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 241 | return 0; |
Alex Bligh | 533a8cf | 2013-08-21 16:02:51 +0100 | [diff] [blame] | 242 | } else { |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 243 | return qemu_soonest_timeout(timeout, deadline); |
Alex Bligh | 533a8cf | 2013-08-21 16:02:51 +0100 | [diff] [blame] | 244 | } |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 245 | } |
Alex Bligh | 533a8cf | 2013-08-21 16:02:51 +0100 | [diff] [blame] | 246 | |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 247 | static gboolean |
| 248 | aio_ctx_prepare(GSource *source, gint *timeout) |
| 249 | { |
| 250 | AioContext *ctx = (AioContext *) source; |
| 251 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 252 | qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) | 1); |
Paolo Bonzini | 5710a3e | 2020-04-07 10:07:46 -0400 | [diff] [blame] | 253 | |
| 254 | /* |
| 255 | * Write ctx->notify_me before computing the timeout |
| 256 | * (reading bottom half flags, etc.). Pairs with |
| 257 | * smp_mb in aio_notify(). |
| 258 | */ |
| 259 | smp_mb(); |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 260 | |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 261 | /* We assume there is no timeout already supplied */ |
| 262 | *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)); |
Paolo Bonzini | a3462c6 | 2014-07-09 11:53:08 +0200 | [diff] [blame] | 263 | |
| 264 | if (aio_prepare(ctx)) { |
| 265 | *timeout = 0; |
| 266 | } |
| 267 | |
Paolo Bonzini | 845ca10 | 2014-07-09 11:53:01 +0200 | [diff] [blame] | 268 | return *timeout == 0; |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static gboolean |
| 272 | aio_ctx_check(GSource *source) |
| 273 | { |
| 274 | AioContext *ctx = (AioContext *) source; |
| 275 | QEMUBH *bh; |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 276 | BHListSlice *s; |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 277 | |
Paolo Bonzini | 5710a3e | 2020-04-07 10:07:46 -0400 | [diff] [blame] | 278 | /* Finish computing the timeout before clearing the flag. */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 279 | qatomic_store_release(&ctx->notify_me, qatomic_read(&ctx->notify_me) & ~1); |
Paolo Bonzini | 05e514b | 2015-07-21 16:07:53 +0200 | [diff] [blame] | 280 | aio_notify_accept(ctx); |
Paolo Bonzini | 21a03d1 | 2015-07-21 16:07:52 +0200 | [diff] [blame] | 281 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 282 | QSLIST_FOREACH_RCU(bh, &ctx->bh_list, next) { |
| 283 | if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 284 | return true; |
Cao jin | 6977d90 | 2016-07-14 21:10:43 +0800 | [diff] [blame] | 285 | } |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 286 | } |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 287 | |
| 288 | QSIMPLEQ_FOREACH(s, &ctx->bh_slice_list, next) { |
| 289 | QSLIST_FOREACH_RCU(bh, &s->bh_list, next) { |
| 290 | if ((bh->flags & (BH_SCHEDULED | BH_DELETED)) == BH_SCHEDULED) { |
| 291 | return true; |
| 292 | } |
| 293 | } |
| 294 | } |
Alex Bligh | 533a8cf | 2013-08-21 16:02:51 +0100 | [diff] [blame] | 295 | return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static gboolean |
| 299 | aio_ctx_dispatch(GSource *source, |
| 300 | GSourceFunc callback, |
| 301 | gpointer user_data) |
| 302 | { |
| 303 | AioContext *ctx = (AioContext *) source; |
| 304 | |
| 305 | assert(callback == NULL); |
Paolo Bonzini | a153bf5 | 2017-02-13 14:52:33 +0100 | [diff] [blame] | 306 | aio_dispatch(ctx); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 310 | static void |
| 311 | aio_ctx_finalize(GSource *source) |
| 312 | { |
| 313 | AioContext *ctx = (AioContext *) source; |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 314 | QEMUBH *bh; |
| 315 | unsigned flags; |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 316 | |
Stefan Hajnoczi | 9b34277 | 2013-03-07 13:41:47 +0100 | [diff] [blame] | 317 | thread_pool_free(ctx->thread_pool); |
Stefan Hajnoczi | a076972 | 2015-07-28 18:34:08 +0200 | [diff] [blame] | 318 | |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 319 | #ifdef CONFIG_LINUX_AIO |
| 320 | if (ctx->linux_aio) { |
| 321 | laio_detach_aio_context(ctx->linux_aio, ctx); |
| 322 | laio_cleanup(ctx->linux_aio); |
| 323 | ctx->linux_aio = NULL; |
| 324 | } |
| 325 | #endif |
| 326 | |
Aarushi Mehta | fcb7a4a | 2020-01-20 14:18:49 +0000 | [diff] [blame] | 327 | #ifdef CONFIG_LINUX_IO_URING |
| 328 | if (ctx->linux_io_uring) { |
| 329 | luring_detach_aio_context(ctx->linux_io_uring, ctx); |
| 330 | luring_cleanup(ctx->linux_io_uring); |
| 331 | ctx->linux_io_uring = NULL; |
| 332 | } |
| 333 | #endif |
| 334 | |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 335 | assert(QSLIST_EMPTY(&ctx->scheduled_coroutines)); |
| 336 | qemu_bh_delete(ctx->co_schedule_bh); |
| 337 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 338 | /* There must be no aio_bh_poll() calls going on */ |
| 339 | assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list)); |
Stefan Hajnoczi | a076972 | 2015-07-28 18:34:08 +0200 | [diff] [blame] | 340 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 341 | while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) { |
Stefan Hajnoczi | a076972 | 2015-07-28 18:34:08 +0200 | [diff] [blame] | 342 | /* qemu_bh_delete() must have been called on BHs in this AioContext */ |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 343 | assert(flags & BH_DELETED); |
Stefan Hajnoczi | a076972 | 2015-07-28 18:34:08 +0200 | [diff] [blame] | 344 | |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 345 | g_free(bh); |
Stefan Hajnoczi | a076972 | 2015-07-28 18:34:08 +0200 | [diff] [blame] | 346 | } |
Stefan Hajnoczi | a076972 | 2015-07-28 18:34:08 +0200 | [diff] [blame] | 347 | |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 348 | aio_set_event_notifier(ctx, &ctx->notifier, false, NULL, NULL); |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 349 | event_notifier_cleanup(&ctx->notifier); |
Paolo Bonzini | 3fe7122 | 2016-10-27 12:49:08 +0200 | [diff] [blame] | 350 | qemu_rec_mutex_destroy(&ctx->lock); |
Paolo Bonzini | d7c99a1 | 2017-01-12 19:07:53 +0100 | [diff] [blame] | 351 | qemu_lockcnt_destroy(&ctx->list_lock); |
Alex Bligh | dae21b9 | 2013-08-21 16:02:49 +0100 | [diff] [blame] | 352 | timerlistgroup_deinit(&ctx->tlg); |
Jie Wang | cd0a6d2 | 2018-05-17 08:42:43 +0800 | [diff] [blame] | 353 | aio_context_destroy(ctx); |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 356 | static GSourceFuncs aio_source_funcs = { |
| 357 | aio_ctx_prepare, |
| 358 | aio_ctx_check, |
| 359 | aio_ctx_dispatch, |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 360 | aio_ctx_finalize |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | GSource *aio_get_g_source(AioContext *ctx) |
| 364 | { |
Stefan Hajnoczi | ba607ca | 2020-05-11 19:36:30 +0100 | [diff] [blame] | 365 | aio_context_use_g_source(ctx); |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 366 | g_source_ref(&ctx->source); |
| 367 | return &ctx->source; |
| 368 | } |
Paolo Bonzini | a915f4b | 2012-09-13 12:28:51 +0200 | [diff] [blame] | 369 | |
Stefan Hajnoczi | 9b34277 | 2013-03-07 13:41:47 +0100 | [diff] [blame] | 370 | ThreadPool *aio_get_thread_pool(AioContext *ctx) |
| 371 | { |
| 372 | if (!ctx->thread_pool) { |
| 373 | ctx->thread_pool = thread_pool_new(ctx); |
| 374 | } |
| 375 | return ctx->thread_pool; |
| 376 | } |
| 377 | |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 378 | #ifdef CONFIG_LINUX_AIO |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 379 | LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp) |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 380 | { |
| 381 | if (!ctx->linux_aio) { |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 382 | ctx->linux_aio = laio_init(errp); |
| 383 | if (ctx->linux_aio) { |
| 384 | laio_attach_aio_context(ctx->linux_aio, ctx); |
| 385 | } |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 386 | } |
| 387 | return ctx->linux_aio; |
| 388 | } |
Nishanth Aravamudan | ed6e216 | 2018-06-22 12:37:00 -0700 | [diff] [blame] | 389 | |
| 390 | LinuxAioState *aio_get_linux_aio(AioContext *ctx) |
| 391 | { |
| 392 | assert(ctx->linux_aio); |
| 393 | return ctx->linux_aio; |
| 394 | } |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 395 | #endif |
| 396 | |
Aarushi Mehta | fcb7a4a | 2020-01-20 14:18:49 +0000 | [diff] [blame] | 397 | #ifdef CONFIG_LINUX_IO_URING |
| 398 | LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp) |
| 399 | { |
| 400 | if (ctx->linux_io_uring) { |
| 401 | return ctx->linux_io_uring; |
| 402 | } |
| 403 | |
| 404 | ctx->linux_io_uring = luring_init(errp); |
| 405 | if (!ctx->linux_io_uring) { |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | luring_attach_aio_context(ctx->linux_io_uring, ctx); |
| 410 | return ctx->linux_io_uring; |
| 411 | } |
| 412 | |
| 413 | LuringState *aio_get_linux_io_uring(AioContext *ctx) |
| 414 | { |
| 415 | assert(ctx->linux_io_uring); |
| 416 | return ctx->linux_io_uring; |
| 417 | } |
| 418 | #endif |
| 419 | |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 420 | void aio_notify(AioContext *ctx) |
| 421 | { |
Stefan Hajnoczi | 601829f | 2020-08-06 14:18:01 +0100 | [diff] [blame] | 422 | /* |
| 423 | * Write e.g. bh->flags before writing ctx->notified. Pairs with smp_mb in |
| 424 | * aio_notify_accept. |
| 425 | */ |
| 426 | smp_wmb(); |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 427 | qatomic_set(&ctx->notified, true); |
Stefan Hajnoczi | 601829f | 2020-08-06 14:18:01 +0100 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Write ctx->notified before reading ctx->notify_me. Pairs |
Paolo Bonzini | 5710a3e | 2020-04-07 10:07:46 -0400 | [diff] [blame] | 431 | * with smp_mb in aio_ctx_prepare or aio_poll. |
Paolo Bonzini | eabc977 | 2015-07-21 16:07:51 +0200 | [diff] [blame] | 432 | */ |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 433 | smp_mb(); |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 434 | if (qatomic_read(&ctx->notify_me)) { |
Paolo Bonzini | 0ceb849 | 2014-07-07 15:18:04 +0200 | [diff] [blame] | 435 | event_notifier_set(&ctx->notifier); |
Paolo Bonzini | 05e514b | 2015-07-21 16:07:53 +0200 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | void aio_notify_accept(AioContext *ctx) |
| 440 | { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 441 | qatomic_set(&ctx->notified, false); |
Stefan Hajnoczi | 601829f | 2020-08-06 14:18:01 +0100 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Write ctx->notified before reading e.g. bh->flags. Pairs with smp_wmb |
| 445 | * in aio_notify. |
| 446 | */ |
| 447 | smp_mb(); |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Paolo Bonzini | 3f53bc6 | 2017-03-03 11:50:29 +0100 | [diff] [blame] | 450 | static void aio_timerlist_notify(void *opaque, QEMUClockType type) |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 451 | { |
| 452 | aio_notify(opaque); |
| 453 | } |
| 454 | |
Stefan Hajnoczi | 601829f | 2020-08-06 14:18:01 +0100 | [diff] [blame] | 455 | static void aio_context_notifier_cb(EventNotifier *e) |
Paolo Bonzini | 21a03d1 | 2015-07-21 16:07:52 +0200 | [diff] [blame] | 456 | { |
Stefan Hajnoczi | 601829f | 2020-08-06 14:18:01 +0100 | [diff] [blame] | 457 | AioContext *ctx = container_of(e, AioContext, notifier); |
| 458 | |
| 459 | event_notifier_test_and_clear(&ctx->notifier); |
Paolo Bonzini | 21a03d1 | 2015-07-21 16:07:52 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 462 | /* Returns true if aio_notify() was called (e.g. a BH was scheduled) */ |
Stefan Hajnoczi | c13be5a | 2020-08-06 14:18:00 +0100 | [diff] [blame] | 463 | static bool aio_context_notifier_poll(void *opaque) |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 464 | { |
| 465 | EventNotifier *e = opaque; |
| 466 | AioContext *ctx = container_of(e, AioContext, notifier); |
| 467 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 468 | return qatomic_read(&ctx->notified); |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 471 | static void co_schedule_bh_cb(void *opaque) |
| 472 | { |
| 473 | AioContext *ctx = opaque; |
| 474 | QSLIST_HEAD(, Coroutine) straight, reversed; |
| 475 | |
| 476 | QSLIST_MOVE_ATOMIC(&reversed, &ctx->scheduled_coroutines); |
| 477 | QSLIST_INIT(&straight); |
| 478 | |
| 479 | while (!QSLIST_EMPTY(&reversed)) { |
| 480 | Coroutine *co = QSLIST_FIRST(&reversed); |
| 481 | QSLIST_REMOVE_HEAD(&reversed, co_scheduled_next); |
| 482 | QSLIST_INSERT_HEAD(&straight, co, co_scheduled_next); |
| 483 | } |
| 484 | |
| 485 | while (!QSLIST_EMPTY(&straight)) { |
| 486 | Coroutine *co = QSLIST_FIRST(&straight); |
| 487 | QSLIST_REMOVE_HEAD(&straight, co_scheduled_next); |
| 488 | trace_aio_co_schedule_bh_cb(ctx, co); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 489 | aio_context_acquire(ctx); |
Jeff Cody | 6133b39 | 2017-11-17 22:27:09 -0500 | [diff] [blame] | 490 | |
| 491 | /* Protected by write barrier in qemu_aio_coroutine_enter */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 492 | qatomic_set(&co->scheduled, NULL); |
Sergio Lopez | 6808ae0 | 2018-09-05 11:33:51 +0200 | [diff] [blame] | 493 | qemu_aio_coroutine_enter(ctx, co); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 494 | aio_context_release(ctx); |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 498 | AioContext *aio_context_new(Error **errp) |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 499 | { |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 500 | int ret; |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 501 | AioContext *ctx; |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 502 | |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 503 | ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext)); |
Stefan Hajnoczi | 8c6b035 | 2020-02-21 09:39:51 +0000 | [diff] [blame] | 504 | QSLIST_INIT(&ctx->bh_list); |
| 505 | QSIMPLEQ_INIT(&ctx->bh_slice_list); |
Cao jin | 7e00346 | 2016-07-15 18:28:44 +0800 | [diff] [blame] | 506 | aio_context_setup(ctx); |
| 507 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 508 | ret = event_notifier_init(&ctx->notifier, false); |
| 509 | if (ret < 0) { |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 510 | error_setg_errno(errp, -ret, "Failed to initialize event notifier"); |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 511 | goto fail; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 512 | } |
Paolo Bonzini | fcf5def | 2014-12-17 16:09:58 +0100 | [diff] [blame] | 513 | g_source_set_can_recurse(&ctx->source, true); |
Paolo Bonzini | d7c99a1 | 2017-01-12 19:07:53 +0100 | [diff] [blame] | 514 | qemu_lockcnt_init(&ctx->list_lock); |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 515 | |
| 516 | ctx->co_schedule_bh = aio_bh_new(ctx, co_schedule_bh_cb, ctx); |
| 517 | QSLIST_INIT(&ctx->scheduled_coroutines); |
| 518 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 519 | aio_set_event_notifier(ctx, &ctx->notifier, |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 520 | false, |
Stefan Hajnoczi | 601829f | 2020-08-06 14:18:01 +0100 | [diff] [blame] | 521 | aio_context_notifier_cb, |
Stefan Hajnoczi | c13be5a | 2020-08-06 14:18:00 +0100 | [diff] [blame] | 522 | aio_context_notifier_poll); |
Paolo Bonzini | 0187f5c | 2016-07-04 18:33:20 +0200 | [diff] [blame] | 523 | #ifdef CONFIG_LINUX_AIO |
| 524 | ctx->linux_aio = NULL; |
| 525 | #endif |
Aarushi Mehta | fcb7a4a | 2020-01-20 14:18:49 +0000 | [diff] [blame] | 526 | |
| 527 | #ifdef CONFIG_LINUX_IO_URING |
| 528 | ctx->linux_io_uring = NULL; |
| 529 | #endif |
| 530 | |
Stefan Hajnoczi | 9b34277 | 2013-03-07 13:41:47 +0100 | [diff] [blame] | 531 | ctx->thread_pool = NULL; |
Paolo Bonzini | 3fe7122 | 2016-10-27 12:49:08 +0200 | [diff] [blame] | 532 | qemu_rec_mutex_init(&ctx->lock); |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 533 | timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx); |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 534 | |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 535 | ctx->poll_ns = 0; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 536 | ctx->poll_max_ns = 0; |
Stefan Hajnoczi | 82a4118 | 2016-12-01 19:26:51 +0000 | [diff] [blame] | 537 | ctx->poll_grow = 0; |
| 538 | ctx->poll_shrink = 0; |
Stefan Hajnoczi | 4a1cba3 | 2016-12-01 19:26:42 +0000 | [diff] [blame] | 539 | |
Paolo Bonzini | 2f4dc3c | 2012-09-24 18:44:14 +0200 | [diff] [blame] | 540 | return ctx; |
Fam Zheng | 37fcee5 | 2015-10-30 12:06:28 +0800 | [diff] [blame] | 541 | fail: |
| 542 | g_source_destroy(&ctx->source); |
| 543 | return NULL; |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 546 | void aio_co_schedule(AioContext *ctx, Coroutine *co) |
| 547 | { |
| 548 | trace_aio_co_schedule(ctx, co); |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 549 | const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL, |
Jeff Cody | 6133b39 | 2017-11-17 22:27:09 -0500 | [diff] [blame] | 550 | __func__); |
| 551 | |
| 552 | if (scheduled) { |
| 553 | fprintf(stderr, |
| 554 | "%s: Co-routine was already scheduled in '%s'\n", |
| 555 | __func__, scheduled); |
| 556 | abort(); |
| 557 | } |
| 558 | |
Stefan Hajnoczi | f0f8100 | 2019-07-23 20:06:23 +0100 | [diff] [blame] | 559 | /* The coroutine might run and release the last ctx reference before we |
| 560 | * invoke qemu_bh_schedule(). Take a reference to keep ctx alive until |
| 561 | * we're done. |
| 562 | */ |
| 563 | aio_context_ref(ctx); |
| 564 | |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 565 | QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines, |
| 566 | co, co_scheduled_next); |
| 567 | qemu_bh_schedule(ctx->co_schedule_bh); |
Stefan Hajnoczi | f0f8100 | 2019-07-23 20:06:23 +0100 | [diff] [blame] | 568 | |
| 569 | aio_context_unref(ctx); |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Kevin Wolf | 26b0b69 | 2020-10-05 17:58:52 +0200 | [diff] [blame] | 572 | typedef struct AioCoRescheduleSelf { |
| 573 | Coroutine *co; |
| 574 | AioContext *new_ctx; |
| 575 | } AioCoRescheduleSelf; |
| 576 | |
| 577 | static void aio_co_reschedule_self_bh(void *opaque) |
| 578 | { |
| 579 | AioCoRescheduleSelf *data = opaque; |
| 580 | aio_co_schedule(data->new_ctx, data->co); |
| 581 | } |
| 582 | |
| 583 | void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx) |
| 584 | { |
| 585 | AioContext *old_ctx = qemu_get_current_aio_context(); |
| 586 | |
| 587 | if (old_ctx != new_ctx) { |
| 588 | AioCoRescheduleSelf data = { |
| 589 | .co = qemu_coroutine_self(), |
| 590 | .new_ctx = new_ctx, |
| 591 | }; |
| 592 | /* |
| 593 | * We can't directly schedule the coroutine in the target context |
| 594 | * because this would be racy: The other thread could try to enter the |
| 595 | * coroutine before it has yielded in this one. |
| 596 | */ |
| 597 | aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data); |
| 598 | qemu_coroutine_yield(); |
| 599 | } |
| 600 | } |
| 601 | |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 602 | void aio_co_wake(struct Coroutine *co) |
| 603 | { |
| 604 | AioContext *ctx; |
| 605 | |
| 606 | /* Read coroutine before co->ctx. Matches smp_wmb in |
| 607 | * qemu_coroutine_enter. |
| 608 | */ |
| 609 | smp_read_barrier_depends(); |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 610 | ctx = qatomic_read(&co->ctx); |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 611 | |
Fam Zheng | 8865852 | 2017-04-10 20:07:35 +0800 | [diff] [blame] | 612 | aio_co_enter(ctx, co); |
| 613 | } |
| 614 | |
| 615 | void aio_co_enter(AioContext *ctx, struct Coroutine *co) |
| 616 | { |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 617 | if (ctx != qemu_get_current_aio_context()) { |
| 618 | aio_co_schedule(ctx, co); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | if (qemu_in_coroutine()) { |
| 623 | Coroutine *self = qemu_coroutine_self(); |
| 624 | assert(self != co); |
| 625 | QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next); |
| 626 | } else { |
| 627 | aio_context_acquire(ctx); |
Fam Zheng | 8865852 | 2017-04-10 20:07:35 +0800 | [diff] [blame] | 628 | qemu_aio_coroutine_enter(ctx, co); |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 629 | aio_context_release(ctx); |
| 630 | } |
| 631 | } |
| 632 | |
Paolo Bonzini | e3713e0 | 2012-09-24 14:57:41 +0200 | [diff] [blame] | 633 | void aio_context_ref(AioContext *ctx) |
| 634 | { |
| 635 | g_source_ref(&ctx->source); |
| 636 | } |
| 637 | |
| 638 | void aio_context_unref(AioContext *ctx) |
| 639 | { |
| 640 | g_source_unref(&ctx->source); |
Paolo Bonzini | f627aab | 2012-10-29 23:45:23 +0100 | [diff] [blame] | 641 | } |
Stefan Hajnoczi | 98563fc | 2014-03-03 11:30:04 +0100 | [diff] [blame] | 642 | |
| 643 | void aio_context_acquire(AioContext *ctx) |
| 644 | { |
Paolo Bonzini | 3fe7122 | 2016-10-27 12:49:08 +0200 | [diff] [blame] | 645 | qemu_rec_mutex_lock(&ctx->lock); |
Stefan Hajnoczi | 98563fc | 2014-03-03 11:30:04 +0100 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void aio_context_release(AioContext *ctx) |
| 649 | { |
Paolo Bonzini | 3fe7122 | 2016-10-27 12:49:08 +0200 | [diff] [blame] | 650 | qemu_rec_mutex_unlock(&ctx->lock); |
Stefan Hajnoczi | 98563fc | 2014-03-03 11:30:04 +0100 | [diff] [blame] | 651 | } |