]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-coroutine-lock.c
coroutine-lock: do not touch coroutine after another one has been entered
[mirror_qemu.git] / util / qemu-coroutine-lock.c
CommitLineData
b96e9247
KW
1/*
2 * coroutine queues and locks
3 *
4 * Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
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.
fed20a70
PB
23 *
24 * The lock-free mutex implementation is based on OSv
25 * (core/lfmutex.cc, include/lockfree/mutex.hh).
26 * Copyright (C) 2013 Cloudius Systems, Ltd.
b96e9247
KW
27 */
28
aafd7584 29#include "qemu/osdep.h"
b96e9247 30#include "qemu-common.h"
10817bf0
DB
31#include "qemu/coroutine.h"
32#include "qemu/coroutine_int.h"
480cff63 33#include "qemu/processor.h"
1de7afc9 34#include "qemu/queue.h"
a9d92355 35#include "block/aio.h"
b96e9247
KW
36#include "trace.h"
37
b96e9247
KW
38void qemu_co_queue_init(CoQueue *queue)
39{
7d9c8581 40 QSIMPLEQ_INIT(&queue->entries);
b96e9247
KW
41}
42
1ace7cea 43void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex)
b96e9247
KW
44{
45 Coroutine *self = qemu_coroutine_self();
7d9c8581 46 QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
1ace7cea
PB
47
48 if (mutex) {
49 qemu_co_mutex_unlock(mutex);
50 }
51
52 /* There is no race condition here. Other threads will call
53 * aio_co_schedule on our AioContext, which can reenter this
54 * coroutine but only after this yield and after the main loop
55 * has gone through the next iteration.
56 */
b96e9247
KW
57 qemu_coroutine_yield();
58 assert(qemu_in_coroutine());
1ace7cea
PB
59
60 /* TODO: OSv implements wait morphing here, where the wakeup
61 * primitive automatically places the woken coroutine on the
62 * mutex's queue. This avoids the thundering herd effect.
63 */
64 if (mutex) {
65 qemu_co_mutex_lock(mutex);
66 }
b96e9247
KW
67}
68
02ffb504
SH
69/**
70 * qemu_co_queue_run_restart:
71 *
72 * Enter each coroutine that was previously marked for restart by
73 * qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
74 * invoked by the core coroutine code when the current coroutine yields or
75 * terminates.
76 */
77void qemu_co_queue_run_restart(Coroutine *co)
78{
79 Coroutine *next;
528f449f
RP
80 QSIMPLEQ_HEAD(, Coroutine) tmp_queue_wakeup =
81 QSIMPLEQ_HEAD_INITIALIZER(tmp_queue_wakeup);
02ffb504
SH
82
83 trace_qemu_co_queue_run_restart(co);
528f449f
RP
84
85 /* Because "co" has yielded, any coroutine that we wakeup can resume it.
86 * If this happens and "co" terminates, co->co_queue_wakeup becomes
87 * invalid memory. Therefore, use a temporary queue and do not touch
88 * the "co" coroutine as soon as you enter another one.
89 *
90 * In its turn resumed "co" can pupulate "co_queue_wakeup" queue with
91 * new coroutines to be woken up. The caller, who has resumed "co",
92 * will be responsible for traversing the same queue, which may cause
93 * a different wakeup order but not any missing wakeups.
94 */
95 QSIMPLEQ_CONCAT(&tmp_queue_wakeup, &co->co_queue_wakeup);
96
97 while ((next = QSIMPLEQ_FIRST(&tmp_queue_wakeup))) {
98 QSIMPLEQ_REMOVE_HEAD(&tmp_queue_wakeup, co_queue_next);
0b8b8753 99 qemu_coroutine_enter(next);
02ffb504
SH
100 }
101}
102
28f08246 103static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
b96e9247 104{
b96e9247 105 Coroutine *next;
28f08246 106
7d9c8581 107 if (QSIMPLEQ_EMPTY(&queue->entries)) {
28f08246
SH
108 return false;
109 }
b96e9247 110
7d9c8581
PB
111 while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
112 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
a9d92355 113 aio_co_wake(next);
28f08246
SH
114 if (single) {
115 break;
116 }
b96e9247 117 }
28f08246
SH
118 return true;
119}
b96e9247 120
b681a1c7 121bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
28f08246 122{
b681a1c7 123 assert(qemu_in_coroutine());
28f08246 124 return qemu_co_queue_do_restart(queue, true);
b96e9247
KW
125}
126
b681a1c7 127void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
e8ee5e4c 128{
b681a1c7 129 assert(qemu_in_coroutine());
28f08246 130 qemu_co_queue_do_restart(queue, false);
e8ee5e4c
SH
131}
132
b681a1c7
BC
133bool qemu_co_enter_next(CoQueue *queue)
134{
135 Coroutine *next;
136
7d9c8581 137 next = QSIMPLEQ_FIRST(&queue->entries);
b681a1c7
BC
138 if (!next) {
139 return false;
140 }
141
7d9c8581 142 QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
0b8b8753 143 qemu_coroutine_enter(next);
b681a1c7
BC
144 return true;
145}
146
b96e9247
KW
147bool qemu_co_queue_empty(CoQueue *queue)
148{
7d9c8581 149 return QSIMPLEQ_FIRST(&queue->entries) == NULL;
b96e9247
KW
150}
151
fed20a70
PB
152/* The wait records are handled with a multiple-producer, single-consumer
153 * lock-free queue. There cannot be two concurrent pop_waiter() calls
154 * because pop_waiter() can only be called while mutex->handoff is zero.
155 * This can happen in three cases:
156 * - in qemu_co_mutex_unlock, before the hand-off protocol has started.
157 * In this case, qemu_co_mutex_lock will see mutex->handoff == 0 and
158 * not take part in the handoff.
159 * - in qemu_co_mutex_lock, if it steals the hand-off responsibility from
160 * qemu_co_mutex_unlock. In this case, qemu_co_mutex_unlock will fail
161 * the cmpxchg (it will see either 0 or the next sequence value) and
162 * exit. The next hand-off cannot begin until qemu_co_mutex_lock has
163 * woken up someone.
164 * - in qemu_co_mutex_unlock, if it takes the hand-off token itself.
165 * In this case another iteration starts with mutex->handoff == 0;
166 * a concurrent qemu_co_mutex_lock will fail the cmpxchg, and
167 * qemu_co_mutex_unlock will go back to case (1).
168 *
169 * The following functions manage this queue.
170 */
171typedef struct CoWaitRecord {
172 Coroutine *co;
173 QSLIST_ENTRY(CoWaitRecord) next;
174} CoWaitRecord;
175
176static void push_waiter(CoMutex *mutex, CoWaitRecord *w)
177{
178 w->co = qemu_coroutine_self();
179 QSLIST_INSERT_HEAD_ATOMIC(&mutex->from_push, w, next);
180}
181
182static void move_waiters(CoMutex *mutex)
183{
184 QSLIST_HEAD(, CoWaitRecord) reversed;
185 QSLIST_MOVE_ATOMIC(&reversed, &mutex->from_push);
186 while (!QSLIST_EMPTY(&reversed)) {
187 CoWaitRecord *w = QSLIST_FIRST(&reversed);
188 QSLIST_REMOVE_HEAD(&reversed, next);
189 QSLIST_INSERT_HEAD(&mutex->to_pop, w, next);
190 }
191}
192
193static CoWaitRecord *pop_waiter(CoMutex *mutex)
194{
195 CoWaitRecord *w;
196
197 if (QSLIST_EMPTY(&mutex->to_pop)) {
198 move_waiters(mutex);
199 if (QSLIST_EMPTY(&mutex->to_pop)) {
200 return NULL;
201 }
202 }
203 w = QSLIST_FIRST(&mutex->to_pop);
204 QSLIST_REMOVE_HEAD(&mutex->to_pop, next);
205 return w;
206}
207
208static bool has_waiters(CoMutex *mutex)
209{
210 return QSLIST_EMPTY(&mutex->to_pop) || QSLIST_EMPTY(&mutex->from_push);
211}
212
b96e9247
KW
213void qemu_co_mutex_init(CoMutex *mutex)
214{
215 memset(mutex, 0, sizeof(*mutex));
b96e9247
KW
216}
217
480cff63
PB
218static void coroutine_fn qemu_co_mutex_wake(CoMutex *mutex, Coroutine *co)
219{
220 /* Read co before co->ctx; pairs with smp_wmb() in
221 * qemu_coroutine_enter().
222 */
223 smp_read_barrier_depends();
224 mutex->ctx = co->ctx;
225 aio_co_wake(co);
226}
227
228static void coroutine_fn qemu_co_mutex_lock_slowpath(AioContext *ctx,
229 CoMutex *mutex)
b96e9247
KW
230{
231 Coroutine *self = qemu_coroutine_self();
fed20a70
PB
232 CoWaitRecord w;
233 unsigned old_handoff;
b96e9247
KW
234
235 trace_qemu_co_mutex_lock_entry(mutex, self);
fed20a70
PB
236 w.co = self;
237 push_waiter(mutex, &w);
b96e9247 238
fed20a70
PB
239 /* This is the "Responsibility Hand-Off" protocol; a lock() picks from
240 * a concurrent unlock() the responsibility of waking somebody up.
241 */
242 old_handoff = atomic_mb_read(&mutex->handoff);
243 if (old_handoff &&
244 has_waiters(mutex) &&
245 atomic_cmpxchg(&mutex->handoff, old_handoff, 0) == old_handoff) {
246 /* There can be no concurrent pops, because there can be only
247 * one active handoff at a time.
248 */
249 CoWaitRecord *to_wake = pop_waiter(mutex);
250 Coroutine *co = to_wake->co;
251 if (co == self) {
252 /* We got the lock ourselves! */
253 assert(to_wake == &w);
480cff63 254 mutex->ctx = ctx;
fed20a70
PB
255 return;
256 }
257
480cff63 258 qemu_co_mutex_wake(mutex, co);
b96e9247
KW
259 }
260
fed20a70
PB
261 qemu_coroutine_yield();
262 trace_qemu_co_mutex_lock_return(mutex, self);
263}
264
265void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
266{
480cff63 267 AioContext *ctx = qemu_get_current_aio_context();
fed20a70 268 Coroutine *self = qemu_coroutine_self();
480cff63
PB
269 int waiters, i;
270
271 /* Running a very small critical section on pthread_mutex_t and CoMutex
272 * shows that pthread_mutex_t is much faster because it doesn't actually
273 * go to sleep. What happens is that the critical section is shorter
274 * than the latency of entering the kernel and thus FUTEX_WAIT always
275 * fails. With CoMutex there is no such latency but you still want to
276 * avoid wait and wakeup. So introduce it artificially.
277 */
278 i = 0;
279retry_fast_path:
280 waiters = atomic_cmpxchg(&mutex->locked, 0, 1);
281 if (waiters != 0) {
282 while (waiters == 1 && ++i < 1000) {
283 if (atomic_read(&mutex->ctx) == ctx) {
284 break;
285 }
286 if (atomic_read(&mutex->locked) == 0) {
287 goto retry_fast_path;
288 }
289 cpu_relax();
290 }
291 waiters = atomic_fetch_inc(&mutex->locked);
292 }
fed20a70 293
480cff63 294 if (waiters == 0) {
fed20a70
PB
295 /* Uncontended. */
296 trace_qemu_co_mutex_lock_uncontended(mutex, self);
480cff63 297 mutex->ctx = ctx;
fed20a70 298 } else {
480cff63 299 qemu_co_mutex_lock_slowpath(ctx, mutex);
fed20a70 300 }
0e438cdc 301 mutex->holder = self;
1b7f01d9 302 self->locks_held++;
b96e9247
KW
303}
304
305void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
306{
307 Coroutine *self = qemu_coroutine_self();
308
309 trace_qemu_co_mutex_unlock_entry(mutex, self);
310
fed20a70 311 assert(mutex->locked);
0e438cdc 312 assert(mutex->holder == self);
b96e9247
KW
313 assert(qemu_in_coroutine());
314
480cff63 315 mutex->ctx = NULL;
0e438cdc 316 mutex->holder = NULL;
1b7f01d9 317 self->locks_held--;
fed20a70
PB
318 if (atomic_fetch_dec(&mutex->locked) == 1) {
319 /* No waiting qemu_co_mutex_lock(). Pfew, that was easy! */
320 return;
321 }
322
323 for (;;) {
324 CoWaitRecord *to_wake = pop_waiter(mutex);
325 unsigned our_handoff;
326
327 if (to_wake) {
480cff63 328 qemu_co_mutex_wake(mutex, to_wake->co);
fed20a70
PB
329 break;
330 }
331
332 /* Some concurrent lock() is in progress (we know this because
333 * mutex->locked was >1) but it hasn't yet put itself on the wait
334 * queue. Pick a sequence number for the handoff protocol (not 0).
335 */
336 if (++mutex->sequence == 0) {
337 mutex->sequence = 1;
338 }
339
340 our_handoff = mutex->sequence;
341 atomic_mb_set(&mutex->handoff, our_handoff);
342 if (!has_waiters(mutex)) {
343 /* The concurrent lock has not added itself yet, so it
344 * will be able to pick our handoff.
345 */
346 break;
347 }
348
349 /* Try to do the handoff protocol ourselves; if somebody else has
350 * already taken it, however, we're done and they're responsible.
351 */
352 if (atomic_cmpxchg(&mutex->handoff, our_handoff, 0) != our_handoff) {
353 break;
354 }
355 }
b96e9247
KW
356
357 trace_qemu_co_mutex_unlock_return(mutex, self);
358}
12888904
AK
359
360void qemu_co_rwlock_init(CoRwlock *lock)
361{
362 memset(lock, 0, sizeof(*lock));
363 qemu_co_queue_init(&lock->queue);
a7b91d35 364 qemu_co_mutex_init(&lock->mutex);
12888904
AK
365}
366
367void qemu_co_rwlock_rdlock(CoRwlock *lock)
368{
1b7f01d9
KW
369 Coroutine *self = qemu_coroutine_self();
370
a7b91d35
PB
371 qemu_co_mutex_lock(&lock->mutex);
372 /* For fairness, wait if a writer is in line. */
373 while (lock->pending_writer) {
374 qemu_co_queue_wait(&lock->queue, &lock->mutex);
12888904
AK
375 }
376 lock->reader++;
a7b91d35
PB
377 qemu_co_mutex_unlock(&lock->mutex);
378
379 /* The rest of the read-side critical section is run without the mutex. */
1b7f01d9 380 self->locks_held++;
12888904
AK
381}
382
383void qemu_co_rwlock_unlock(CoRwlock *lock)
384{
1b7f01d9
KW
385 Coroutine *self = qemu_coroutine_self();
386
12888904 387 assert(qemu_in_coroutine());
a7b91d35
PB
388 if (!lock->reader) {
389 /* The critical section started in qemu_co_rwlock_wrlock. */
e8ee5e4c 390 qemu_co_queue_restart_all(&lock->queue);
12888904 391 } else {
a7b91d35
PB
392 self->locks_held--;
393
394 qemu_co_mutex_lock(&lock->mutex);
12888904
AK
395 lock->reader--;
396 assert(lock->reader >= 0);
397 /* Wakeup only one waiting writer */
398 if (!lock->reader) {
399 qemu_co_queue_next(&lock->queue);
400 }
401 }
a7b91d35 402 qemu_co_mutex_unlock(&lock->mutex);
12888904
AK
403}
404
405void qemu_co_rwlock_wrlock(CoRwlock *lock)
406{
a7b91d35
PB
407 qemu_co_mutex_lock(&lock->mutex);
408 lock->pending_writer++;
409 while (lock->reader) {
410 qemu_co_queue_wait(&lock->queue, &lock->mutex);
12888904 411 }
a7b91d35
PB
412 lock->pending_writer--;
413
414 /* The rest of the write-side critical section is run with
415 * the mutex taken, so that lock->reader remains zero.
416 * There is no need to update self->locks_held.
417 */
12888904 418}