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