Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU coroutines |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2011 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> |
| 8 | * Kevin Wolf <kwolf@redhat.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 11 | * See the COPYING.LIB file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 16 | #include "trace.h" |
| 17 | #include "qemu-common.h" |
Stefan Hajnoczi | b84c458 | 2013-05-17 15:51:25 +0200 | [diff] [blame] | 18 | #include "qemu/thread.h" |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 19 | #include "qemu/atomic.h" |
Daniel P. Berrange | 10817bf | 2015-09-01 14:48:02 +0100 | [diff] [blame] | 20 | #include "qemu/coroutine.h" |
| 21 | #include "qemu/coroutine_int.h" |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 22 | #include "block/aio.h" |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 23 | |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 24 | enum { |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 25 | POOL_BATCH_SIZE = 64, |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /** Free list to speed up creation */ |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 29 | static QSLIST_HEAD(, Coroutine) release_pool = QSLIST_HEAD_INITIALIZER(pool); |
| 30 | static unsigned int release_pool_size; |
| 31 | static __thread QSLIST_HEAD(, Coroutine) alloc_pool = QSLIST_HEAD_INITIALIZER(pool); |
Peter Lieven | 51a2219 | 2014-12-02 12:05:50 +0100 | [diff] [blame] | 32 | static __thread unsigned int alloc_pool_size; |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 33 | static __thread Notifier coroutine_pool_cleanup_notifier; |
| 34 | |
| 35 | static void coroutine_pool_cleanup(Notifier *n, void *value) |
| 36 | { |
| 37 | Coroutine *co; |
| 38 | Coroutine *tmp; |
| 39 | |
| 40 | QSLIST_FOREACH_SAFE(co, &alloc_pool, pool_next, tmp) { |
| 41 | QSLIST_REMOVE_HEAD(&alloc_pool, pool_next); |
| 42 | qemu_coroutine_delete(co); |
| 43 | } |
| 44 | } |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 45 | |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 46 | Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque) |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 47 | { |
Stefan Hajnoczi | 70c60c0 | 2013-09-11 16:42:35 +0200 | [diff] [blame] | 48 | Coroutine *co = NULL; |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 49 | |
Stefan Hajnoczi | 70c60c0 | 2013-09-11 16:42:35 +0200 | [diff] [blame] | 50 | if (CONFIG_COROUTINE_POOL) { |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 51 | co = QSLIST_FIRST(&alloc_pool); |
| 52 | if (!co) { |
| 53 | if (release_pool_size > POOL_BATCH_SIZE) { |
| 54 | /* Slow path; a good place to register the destructor, too. */ |
| 55 | if (!coroutine_pool_cleanup_notifier.notify) { |
| 56 | coroutine_pool_cleanup_notifier.notify = coroutine_pool_cleanup; |
| 57 | qemu_thread_atexit_add(&coroutine_pool_cleanup_notifier); |
| 58 | } |
| 59 | |
| 60 | /* This is not exact; there could be a little skew between |
| 61 | * release_pool_size and the actual size of release_pool. But |
| 62 | * it is just a heuristic, it does not need to be perfect. |
| 63 | */ |
Peter Lieven | 51a2219 | 2014-12-02 12:05:50 +0100 | [diff] [blame] | 64 | alloc_pool_size = atomic_xchg(&release_pool_size, 0); |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 65 | QSLIST_MOVE_ATOMIC(&alloc_pool, &release_pool); |
| 66 | co = QSLIST_FIRST(&alloc_pool); |
| 67 | } |
Stefan Hajnoczi | 70c60c0 | 2013-09-11 16:42:35 +0200 | [diff] [blame] | 68 | } |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 69 | if (co) { |
| 70 | QSLIST_REMOVE_HEAD(&alloc_pool, pool_next); |
Peter Lieven | 51a2219 | 2014-12-02 12:05:50 +0100 | [diff] [blame] | 71 | alloc_pool_size--; |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 72 | } |
Stefan Hajnoczi | b84c458 | 2013-05-17 15:51:25 +0200 | [diff] [blame] | 73 | } |
Stefan Hajnoczi | b84c458 | 2013-05-17 15:51:25 +0200 | [diff] [blame] | 74 | |
| 75 | if (!co) { |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 76 | co = qemu_coroutine_new(); |
| 77 | } |
| 78 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 79 | co->entry = entry; |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 80 | co->entry_arg = opaque; |
Paolo Bonzini | 7d9c858 | 2016-07-04 19:09:59 +0200 | [diff] [blame] | 81 | QSIMPLEQ_INIT(&co->co_queue_wakeup); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 82 | return co; |
| 83 | } |
| 84 | |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 85 | static void coroutine_delete(Coroutine *co) |
| 86 | { |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 87 | co->caller = NULL; |
| 88 | |
Stefan Hajnoczi | 70c60c0 | 2013-09-11 16:42:35 +0200 | [diff] [blame] | 89 | if (CONFIG_COROUTINE_POOL) { |
Paolo Bonzini | 4d68e86 | 2014-12-02 12:05:48 +0100 | [diff] [blame] | 90 | if (release_pool_size < POOL_BATCH_SIZE * 2) { |
| 91 | QSLIST_INSERT_HEAD_ATOMIC(&release_pool, co, pool_next); |
| 92 | atomic_inc(&release_pool_size); |
Stefan Hajnoczi | 70c60c0 | 2013-09-11 16:42:35 +0200 | [diff] [blame] | 93 | return; |
| 94 | } |
Peter Lieven | 51a2219 | 2014-12-02 12:05:50 +0100 | [diff] [blame] | 95 | if (alloc_pool_size < POOL_BATCH_SIZE) { |
| 96 | QSLIST_INSERT_HEAD(&alloc_pool, co, pool_next); |
| 97 | alloc_pool_size++; |
| 98 | return; |
| 99 | } |
Paolo Bonzini | 4023978 | 2013-02-19 11:59:09 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | qemu_coroutine_delete(co); |
| 103 | } |
| 104 | |
Fam Zheng | ba9e75c | 2017-04-10 20:06:12 +0800 | [diff] [blame] | 105 | void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co) |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 106 | { |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 107 | QSIMPLEQ_HEAD(, Coroutine) pending = QSIMPLEQ_HEAD_INITIALIZER(pending); |
| 108 | Coroutine *from = qemu_coroutine_self(); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 109 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 110 | QSIMPLEQ_INSERT_TAIL(&pending, co, co_queue_next); |
Jeff Cody | 6133b39 | 2017-11-17 22:27:09 -0500 | [diff] [blame] | 111 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 112 | /* Run co and any queued coroutines */ |
| 113 | while (!QSIMPLEQ_EMPTY(&pending)) { |
| 114 | Coroutine *to = QSIMPLEQ_FIRST(&pending); |
| 115 | CoroutineAction ret; |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 116 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 117 | /* Cannot rely on the read barrier for to in aio_co_wake(), as there are |
| 118 | * callers outside of aio_co_wake() */ |
| 119 | const char *scheduled = atomic_mb_read(&to->scheduled); |
Jeff Cody | 6133b39 | 2017-11-17 22:27:09 -0500 | [diff] [blame] | 120 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 121 | QSIMPLEQ_REMOVE_HEAD(&pending, co_queue_next); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 122 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 123 | trace_qemu_aio_coroutine_enter(ctx, from, to, to->entry_arg); |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 124 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 125 | /* if the Coroutine has already been scheduled, entering it again will |
| 126 | * cause us to enter it twice, potentially even after the coroutine has |
| 127 | * been deleted */ |
| 128 | if (scheduled) { |
| 129 | fprintf(stderr, |
| 130 | "%s: Co-routine was already scheduled in '%s'\n", |
| 131 | __func__, scheduled); |
| 132 | abort(); |
| 133 | } |
Paolo Bonzini | 0c330a7 | 2017-02-13 14:52:19 +0100 | [diff] [blame] | 134 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 135 | if (to->caller) { |
| 136 | fprintf(stderr, "Co-routine re-entered recursively\n"); |
| 137 | abort(); |
| 138 | } |
Kevin Wolf | cd12bb5 | 2015-02-10 11:31:52 +0100 | [diff] [blame] | 139 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 140 | to->caller = from; |
| 141 | to->ctx = ctx; |
Kevin Wolf | cd12bb5 | 2015-02-10 11:31:52 +0100 | [diff] [blame] | 142 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 143 | /* Store to->ctx before anything that stores to. Matches |
| 144 | * barrier in aio_co_wake and qemu_co_mutex_wake. |
| 145 | */ |
| 146 | smp_wmb(); |
Roman Pen | 528f449 | 2017-06-01 18:08:47 +0200 | [diff] [blame] | 147 | |
Stefan Hajnoczi | c40a254 | 2018-03-22 15:28:33 +0000 | [diff] [blame] | 148 | ret = qemu_coroutine_switch(from, to, COROUTINE_ENTER); |
| 149 | |
| 150 | /* Queued coroutines are run depth-first; previously pending coroutines |
| 151 | * run after those queued more recently. |
| 152 | */ |
| 153 | QSIMPLEQ_PREPEND(&pending, &to->co_queue_wakeup); |
| 154 | |
| 155 | switch (ret) { |
| 156 | case COROUTINE_YIELD: |
| 157 | break; |
| 158 | case COROUTINE_TERMINATE: |
| 159 | assert(!to->locks_held); |
| 160 | trace_qemu_coroutine_terminate(to); |
| 161 | coroutine_delete(to); |
| 162 | break; |
| 163 | default: |
| 164 | abort(); |
| 165 | } |
Kevin Wolf | cd12bb5 | 2015-02-10 11:31:52 +0100 | [diff] [blame] | 166 | } |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Fam Zheng | ba9e75c | 2017-04-10 20:06:12 +0800 | [diff] [blame] | 169 | void qemu_coroutine_enter(Coroutine *co) |
| 170 | { |
| 171 | qemu_aio_coroutine_enter(qemu_get_current_aio_context(), co); |
| 172 | } |
| 173 | |
Kevin Wolf | 536fca7 | 2016-11-07 16:34:35 +0100 | [diff] [blame] | 174 | void qemu_coroutine_enter_if_inactive(Coroutine *co) |
| 175 | { |
| 176 | if (!qemu_coroutine_entered(co)) { |
| 177 | qemu_coroutine_enter(co); |
| 178 | } |
| 179 | } |
| 180 | |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 181 | void coroutine_fn qemu_coroutine_yield(void) |
| 182 | { |
| 183 | Coroutine *self = qemu_coroutine_self(); |
| 184 | Coroutine *to = self->caller; |
| 185 | |
| 186 | trace_qemu_coroutine_yield(self, to); |
| 187 | |
| 188 | if (!to) { |
| 189 | fprintf(stderr, "Co-routine is yielding to no one\n"); |
| 190 | abort(); |
| 191 | } |
| 192 | |
| 193 | self->caller = NULL; |
Kevin Wolf | 315a130 | 2015-02-10 11:17:53 +0100 | [diff] [blame] | 194 | qemu_coroutine_switch(self, to, COROUTINE_YIELD); |
Kevin Wolf | 00dccaf | 2011-01-17 16:08:14 +0000 | [diff] [blame] | 195 | } |
Stefan Hajnoczi | f643e46 | 2016-09-27 16:18:34 +0100 | [diff] [blame] | 196 | |
| 197 | bool qemu_coroutine_entered(Coroutine *co) |
| 198 | { |
| 199 | return co->caller; |
| 200 | } |
Kevin Wolf | aa1361d | 2018-08-17 18:54:18 +0200 | [diff] [blame] | 201 | |
| 202 | AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co) |
| 203 | { |
| 204 | return co->ctx; |
| 205 | } |