2011-06-30 19:56:46 +04:00
|
|
|
/*
|
|
|
|
* coroutine queues and locks
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2017-02-13 21:12:39 +03:00
|
|
|
*
|
|
|
|
* The lock-free mutex implementation is based on OSv
|
|
|
|
* (core/lfmutex.cc, include/lockfree/mutex.hh).
|
|
|
|
* Copyright (C) 2013 Cloudius Systems, Ltd.
|
2011-06-30 19:56:46 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2011-06-30 19:56:46 +04:00
|
|
|
#include "qemu-common.h"
|
2015-09-01 16:48:02 +03:00
|
|
|
#include "qemu/coroutine.h"
|
|
|
|
#include "qemu/coroutine_int.h"
|
2017-02-13 21:12:40 +03:00
|
|
|
#include "qemu/processor.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/queue.h"
|
2017-02-13 16:52:25 +03:00
|
|
|
#include "block/aio.h"
|
2011-06-30 19:56:46 +04:00
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
void qemu_co_queue_init(CoQueue *queue)
|
|
|
|
{
|
2016-07-04 20:09:59 +03:00
|
|
|
QSIMPLEQ_INIT(&queue->entries);
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
2018-02-03 18:39:33 +03:00
|
|
|
void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock)
|
2011-06-30 19:56:46 +04:00
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
2016-07-04 20:09:59 +03:00
|
|
|
QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
|
2017-02-13 21:12:43 +03:00
|
|
|
|
2018-02-03 18:39:33 +03:00
|
|
|
if (lock) {
|
|
|
|
qemu_lockable_unlock(lock);
|
2017-02-13 21:12:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* There is no race condition here. Other threads will call
|
|
|
|
* aio_co_schedule on our AioContext, which can reenter this
|
|
|
|
* coroutine but only after this yield and after the main loop
|
|
|
|
* has gone through the next iteration.
|
|
|
|
*/
|
2011-06-30 19:56:46 +04:00
|
|
|
qemu_coroutine_yield();
|
|
|
|
assert(qemu_in_coroutine());
|
2017-02-13 21:12:43 +03:00
|
|
|
|
|
|
|
/* TODO: OSv implements wait morphing here, where the wakeup
|
|
|
|
* primitive automatically places the woken coroutine on the
|
|
|
|
* mutex's queue. This avoids the thundering herd effect.
|
2018-02-03 18:39:33 +03:00
|
|
|
* This could be implemented for CoMutexes, but not really for
|
|
|
|
* other cases of QemuLockable.
|
2017-02-13 21:12:43 +03:00
|
|
|
*/
|
2018-02-03 18:39:33 +03:00
|
|
|
if (lock) {
|
|
|
|
qemu_lockable_lock(lock);
|
2017-02-13 21:12:43 +03:00
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 17:51:26 +04:00
|
|
|
/**
|
|
|
|
* qemu_co_queue_run_restart:
|
|
|
|
*
|
|
|
|
* Enter each coroutine that was previously marked for restart by
|
|
|
|
* qemu_co_queue_next() or qemu_co_queue_restart_all(). This function is
|
|
|
|
* invoked by the core coroutine code when the current coroutine yields or
|
|
|
|
* terminates.
|
|
|
|
*/
|
|
|
|
void qemu_co_queue_run_restart(Coroutine *co)
|
|
|
|
{
|
|
|
|
Coroutine *next;
|
coroutine-lock: do not touch coroutine after another one has been entered
Submission of requests on linux aio is a bit tricky and can lead to
requests completions on submission path:
44713c9e8547 ("linux-aio: Handle io_submit() failure gracefully")
0ed93d84edab ("linux-aio: process completions from ioq_submit()")
That means that any coroutine which has been yielded in order to wait
for completion can be resumed from submission path and be eventually
terminated (freed).
The following use-after-free crash was observed when IO throttling
was enabled:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7f5813dff700 (LWP 56417)]
virtqueue_unmap_sg (elem=0x7f5804009a30, len=1, vq=<optimized out>) at virtio.c:252
(gdb) bt
#0 virtqueue_unmap_sg (elem=0x7f5804009a30, len=1, vq=<optimized out>) at virtio.c:252
^^^^^^^^^^^^^^
remember the address
#1 virtqueue_fill (vq=0x5598b20d21b0, elem=0x7f5804009a30, len=1, idx=0) at virtio.c:282
#2 virtqueue_push (vq=0x5598b20d21b0, elem=elem@entry=0x7f5804009a30, len=<optimized out>) at virtio.c:308
#3 virtio_blk_req_complete (req=req@entry=0x7f5804009a30, status=status@entry=0 '\000') at virtio-blk.c:61
#4 virtio_blk_rw_complete (opaque=<optimized out>, ret=0) at virtio-blk.c:126
#5 blk_aio_complete (acb=0x7f58040068d0) at block-backend.c:923
#6 coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at coroutine-ucontext.c:78
(gdb) p * elem
$8 = {index = 77, out_num = 2, in_num = 1,
in_addr = 0x7f5804009ad8, out_addr = 0x7f5804009ae0,
in_sg = 0x0, out_sg = 0x7f5804009a50}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'in_sg' and 'out_sg' are invalid.
e.g. it is impossible that 'in_sg' is zero,
instead its value must be equal to:
(gdb) p/x 0x7f5804009ad8 + sizeof(elem->in_addr[0]) + 2 * sizeof(elem->out_addr[0])
$26 = 0x7f5804009af0
Seems 'elem' was corrupted. Meanwhile another thread raised an abort:
Thread 12 (Thread 0x7f57f2ffd700 (LWP 56426)):
#0 raise () from /lib/x86_64-linux-gnu/libc.so.6
#1 abort () from /lib/x86_64-linux-gnu/libc.so.6
#2 qemu_coroutine_enter (co=0x7f5804009af0) at qemu-coroutine.c:113
#3 qemu_co_queue_run_restart (co=0x7f5804009a30) at qemu-coroutine-lock.c:60
#4 qemu_coroutine_enter (co=0x7f5804009a30) at qemu-coroutine.c:119
^^^^^^^^^^^^^^^^^^
WTF?? this is equal to elem from crashed thread
#5 qemu_co_queue_run_restart (co=0x7f57e7f16ae0) at qemu-coroutine-lock.c:60
#6 qemu_coroutine_enter (co=0x7f57e7f16ae0) at qemu-coroutine.c:119
#7 qemu_co_queue_run_restart (co=0x7f5807e112a0) at qemu-coroutine-lock.c:60
#8 qemu_coroutine_enter (co=0x7f5807e112a0) at qemu-coroutine.c:119
#9 qemu_co_queue_run_restart (co=0x7f5807f17820) at qemu-coroutine-lock.c:60
#10 qemu_coroutine_enter (co=0x7f5807f17820) at qemu-coroutine.c:119
#11 qemu_co_queue_run_restart (co=0x7f57e7f18e10) at qemu-coroutine-lock.c:60
#12 qemu_coroutine_enter (co=0x7f57e7f18e10) at qemu-coroutine.c:119
#13 qemu_co_enter_next (queue=queue@entry=0x5598b1e742d0) at qemu-coroutine-lock.c:106
#14 timer_cb (blk=0x5598b1e74280, is_write=<optimized out>) at throttle-groups.c:419
Crash can be explained by access of 'co' object from the loop inside
qemu_co_queue_run_restart():
while ((next = QSIMPLEQ_FIRST(&co->co_queue_wakeup))) {
QSIMPLEQ_REMOVE_HEAD(&co->co_queue_wakeup, co_queue_next);
^^^^^^^^^^^^^^^^^^^^
on each iteration 'co' is accessed,
but 'co' can be already freed
qemu_coroutine_enter(next);
}
When 'next' coroutine is resumed (entered) it can in its turn resume
'co', and eventually free it. That's why we see 'co' (which was freed)
has the same address as 'elem' from the first backtrace.
The fix is obvious: use temporary queue and do not touch coroutine after
first qemu_coroutine_enter() is invoked.
The issue is quite rare and happens every ~12 hours on very high IO
and CPU load (building linux kernel with -j512 inside guest) when IO
throttling is enabled. With the fix applied guest is running ~35 hours
and is still alive so far.
Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 20170601160847.23720-1-roman.penyaev@profitbricks.com
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Fam Zheng <famz@redhat.com>
Cc: Stefan Hajnoczi <stefanha@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-devel@nongnu.org
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2017-06-01 19:08:47 +03:00
|
|
|
QSIMPLEQ_HEAD(, Coroutine) tmp_queue_wakeup =
|
|
|
|
QSIMPLEQ_HEAD_INITIALIZER(tmp_queue_wakeup);
|
2013-05-17 17:51:26 +04:00
|
|
|
|
|
|
|
trace_qemu_co_queue_run_restart(co);
|
coroutine-lock: do not touch coroutine after another one has been entered
Submission of requests on linux aio is a bit tricky and can lead to
requests completions on submission path:
44713c9e8547 ("linux-aio: Handle io_submit() failure gracefully")
0ed93d84edab ("linux-aio: process completions from ioq_submit()")
That means that any coroutine which has been yielded in order to wait
for completion can be resumed from submission path and be eventually
terminated (freed).
The following use-after-free crash was observed when IO throttling
was enabled:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7f5813dff700 (LWP 56417)]
virtqueue_unmap_sg (elem=0x7f5804009a30, len=1, vq=<optimized out>) at virtio.c:252
(gdb) bt
#0 virtqueue_unmap_sg (elem=0x7f5804009a30, len=1, vq=<optimized out>) at virtio.c:252
^^^^^^^^^^^^^^
remember the address
#1 virtqueue_fill (vq=0x5598b20d21b0, elem=0x7f5804009a30, len=1, idx=0) at virtio.c:282
#2 virtqueue_push (vq=0x5598b20d21b0, elem=elem@entry=0x7f5804009a30, len=<optimized out>) at virtio.c:308
#3 virtio_blk_req_complete (req=req@entry=0x7f5804009a30, status=status@entry=0 '\000') at virtio-blk.c:61
#4 virtio_blk_rw_complete (opaque=<optimized out>, ret=0) at virtio-blk.c:126
#5 blk_aio_complete (acb=0x7f58040068d0) at block-backend.c:923
#6 coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at coroutine-ucontext.c:78
(gdb) p * elem
$8 = {index = 77, out_num = 2, in_num = 1,
in_addr = 0x7f5804009ad8, out_addr = 0x7f5804009ae0,
in_sg = 0x0, out_sg = 0x7f5804009a50}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'in_sg' and 'out_sg' are invalid.
e.g. it is impossible that 'in_sg' is zero,
instead its value must be equal to:
(gdb) p/x 0x7f5804009ad8 + sizeof(elem->in_addr[0]) + 2 * sizeof(elem->out_addr[0])
$26 = 0x7f5804009af0
Seems 'elem' was corrupted. Meanwhile another thread raised an abort:
Thread 12 (Thread 0x7f57f2ffd700 (LWP 56426)):
#0 raise () from /lib/x86_64-linux-gnu/libc.so.6
#1 abort () from /lib/x86_64-linux-gnu/libc.so.6
#2 qemu_coroutine_enter (co=0x7f5804009af0) at qemu-coroutine.c:113
#3 qemu_co_queue_run_restart (co=0x7f5804009a30) at qemu-coroutine-lock.c:60
#4 qemu_coroutine_enter (co=0x7f5804009a30) at qemu-coroutine.c:119
^^^^^^^^^^^^^^^^^^
WTF?? this is equal to elem from crashed thread
#5 qemu_co_queue_run_restart (co=0x7f57e7f16ae0) at qemu-coroutine-lock.c:60
#6 qemu_coroutine_enter (co=0x7f57e7f16ae0) at qemu-coroutine.c:119
#7 qemu_co_queue_run_restart (co=0x7f5807e112a0) at qemu-coroutine-lock.c:60
#8 qemu_coroutine_enter (co=0x7f5807e112a0) at qemu-coroutine.c:119
#9 qemu_co_queue_run_restart (co=0x7f5807f17820) at qemu-coroutine-lock.c:60
#10 qemu_coroutine_enter (co=0x7f5807f17820) at qemu-coroutine.c:119
#11 qemu_co_queue_run_restart (co=0x7f57e7f18e10) at qemu-coroutine-lock.c:60
#12 qemu_coroutine_enter (co=0x7f57e7f18e10) at qemu-coroutine.c:119
#13 qemu_co_enter_next (queue=queue@entry=0x5598b1e742d0) at qemu-coroutine-lock.c:106
#14 timer_cb (blk=0x5598b1e74280, is_write=<optimized out>) at throttle-groups.c:419
Crash can be explained by access of 'co' object from the loop inside
qemu_co_queue_run_restart():
while ((next = QSIMPLEQ_FIRST(&co->co_queue_wakeup))) {
QSIMPLEQ_REMOVE_HEAD(&co->co_queue_wakeup, co_queue_next);
^^^^^^^^^^^^^^^^^^^^
on each iteration 'co' is accessed,
but 'co' can be already freed
qemu_coroutine_enter(next);
}
When 'next' coroutine is resumed (entered) it can in its turn resume
'co', and eventually free it. That's why we see 'co' (which was freed)
has the same address as 'elem' from the first backtrace.
The fix is obvious: use temporary queue and do not touch coroutine after
first qemu_coroutine_enter() is invoked.
The issue is quite rare and happens every ~12 hours on very high IO
and CPU load (building linux kernel with -j512 inside guest) when IO
throttling is enabled. With the fix applied guest is running ~35 hours
and is still alive so far.
Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 20170601160847.23720-1-roman.penyaev@profitbricks.com
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Fam Zheng <famz@redhat.com>
Cc: Stefan Hajnoczi <stefanha@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: qemu-devel@nongnu.org
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2017-06-01 19:08:47 +03:00
|
|
|
|
|
|
|
/* Because "co" has yielded, any coroutine that we wakeup can resume it.
|
|
|
|
* If this happens and "co" terminates, co->co_queue_wakeup becomes
|
|
|
|
* invalid memory. Therefore, use a temporary queue and do not touch
|
|
|
|
* the "co" coroutine as soon as you enter another one.
|
|
|
|
*
|
|
|
|
* In its turn resumed "co" can pupulate "co_queue_wakeup" queue with
|
|
|
|
* new coroutines to be woken up. The caller, who has resumed "co",
|
|
|
|
* will be responsible for traversing the same queue, which may cause
|
|
|
|
* a different wakeup order but not any missing wakeups.
|
|
|
|
*/
|
|
|
|
QSIMPLEQ_CONCAT(&tmp_queue_wakeup, &co->co_queue_wakeup);
|
|
|
|
|
|
|
|
while ((next = QSIMPLEQ_FIRST(&tmp_queue_wakeup))) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&tmp_queue_wakeup, co_queue_next);
|
coroutine: move entry argument to qemu_coroutine_create
In practice the entry argument is always known at creation time, and
it is confusing that sometimes qemu_coroutine_enter is used with a
non-NULL argument to re-enter a coroutine (this happens in
block/sheepdog.c and tests/test-coroutine.c). So pass the opaque value
at creation time, for consistency with e.g. aio_bh_new.
Mostly done with the following semantic patch:
@ entry1 @
expression entry, arg, co;
@@
- co = qemu_coroutine_create(entry);
+ co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry2 @
expression entry, arg;
identifier co;
@@
- Coroutine *co = qemu_coroutine_create(entry);
+ Coroutine *co = qemu_coroutine_create(entry, arg);
...
- qemu_coroutine_enter(co, arg);
+ qemu_coroutine_enter(co);
@ entry3 @
expression entry, arg;
@@
- qemu_coroutine_enter(qemu_coroutine_create(entry), arg);
+ qemu_coroutine_enter(qemu_coroutine_create(entry, arg));
@ reentry @
expression co;
@@
- qemu_coroutine_enter(co, NULL);
+ qemu_coroutine_enter(co);
except for the aforementioned few places where the semantic patch
stumbled (as expected) and for test_co_queue, which would otherwise
produce an uninitialized variable warning.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2016-07-04 20:10:01 +03:00
|
|
|
qemu_coroutine_enter(next);
|
2013-05-17 17:51:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:41:50 +04:00
|
|
|
static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
|
2011-06-30 19:56:46 +04:00
|
|
|
{
|
|
|
|
Coroutine *next;
|
2013-03-07 16:41:50 +04:00
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
if (QSIMPLEQ_EMPTY(&queue->entries)) {
|
2013-03-07 16:41:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
|
2017-02-13 16:52:25 +03:00
|
|
|
aio_co_wake(next);
|
2013-03-07 16:41:50 +04:00
|
|
|
if (single) {
|
|
|
|
break;
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
2013-03-07 16:41:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
|
2013-07-27 00:39:22 +04:00
|
|
|
bool coroutine_fn qemu_co_queue_next(CoQueue *queue)
|
2013-03-07 16:41:50 +04:00
|
|
|
{
|
2013-07-27 00:39:22 +04:00
|
|
|
assert(qemu_in_coroutine());
|
2013-03-07 16:41:50 +04:00
|
|
|
return qemu_co_queue_do_restart(queue, true);
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
2013-07-27 00:39:22 +04:00
|
|
|
void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)
|
2011-11-17 17:40:26 +04:00
|
|
|
{
|
2013-07-27 00:39:22 +04:00
|
|
|
assert(qemu_in_coroutine());
|
2013-03-07 16:41:50 +04:00
|
|
|
qemu_co_queue_do_restart(queue, false);
|
2011-11-17 17:40:26 +04:00
|
|
|
}
|
|
|
|
|
2018-02-03 18:39:34 +03:00
|
|
|
bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock)
|
2013-07-27 00:39:22 +04:00
|
|
|
{
|
|
|
|
Coroutine *next;
|
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
next = QSIMPLEQ_FIRST(&queue->entries);
|
2013-07-27 00:39:22 +04:00
|
|
|
if (!next) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:09:59 +03:00
|
|
|
QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
|
2018-02-03 18:39:34 +03:00
|
|
|
if (lock) {
|
|
|
|
qemu_lockable_unlock(lock);
|
|
|
|
}
|
|
|
|
aio_co_wake(next);
|
|
|
|
if (lock) {
|
|
|
|
qemu_lockable_lock(lock);
|
|
|
|
}
|
2013-07-27 00:39:22 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-30 19:56:46 +04:00
|
|
|
bool qemu_co_queue_empty(CoQueue *queue)
|
|
|
|
{
|
2016-07-04 20:09:59 +03:00
|
|
|
return QSIMPLEQ_FIRST(&queue->entries) == NULL;
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
2017-02-13 21:12:39 +03:00
|
|
|
/* The wait records are handled with a multiple-producer, single-consumer
|
|
|
|
* lock-free queue. There cannot be two concurrent pop_waiter() calls
|
|
|
|
* because pop_waiter() can only be called while mutex->handoff is zero.
|
|
|
|
* This can happen in three cases:
|
|
|
|
* - in qemu_co_mutex_unlock, before the hand-off protocol has started.
|
|
|
|
* In this case, qemu_co_mutex_lock will see mutex->handoff == 0 and
|
|
|
|
* not take part in the handoff.
|
|
|
|
* - in qemu_co_mutex_lock, if it steals the hand-off responsibility from
|
|
|
|
* qemu_co_mutex_unlock. In this case, qemu_co_mutex_unlock will fail
|
|
|
|
* the cmpxchg (it will see either 0 or the next sequence value) and
|
|
|
|
* exit. The next hand-off cannot begin until qemu_co_mutex_lock has
|
|
|
|
* woken up someone.
|
|
|
|
* - in qemu_co_mutex_unlock, if it takes the hand-off token itself.
|
|
|
|
* In this case another iteration starts with mutex->handoff == 0;
|
|
|
|
* a concurrent qemu_co_mutex_lock will fail the cmpxchg, and
|
|
|
|
* qemu_co_mutex_unlock will go back to case (1).
|
|
|
|
*
|
|
|
|
* The following functions manage this queue.
|
|
|
|
*/
|
|
|
|
typedef struct CoWaitRecord {
|
|
|
|
Coroutine *co;
|
|
|
|
QSLIST_ENTRY(CoWaitRecord) next;
|
|
|
|
} CoWaitRecord;
|
|
|
|
|
|
|
|
static void push_waiter(CoMutex *mutex, CoWaitRecord *w)
|
|
|
|
{
|
|
|
|
w->co = qemu_coroutine_self();
|
|
|
|
QSLIST_INSERT_HEAD_ATOMIC(&mutex->from_push, w, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void move_waiters(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
QSLIST_HEAD(, CoWaitRecord) reversed;
|
|
|
|
QSLIST_MOVE_ATOMIC(&reversed, &mutex->from_push);
|
|
|
|
while (!QSLIST_EMPTY(&reversed)) {
|
|
|
|
CoWaitRecord *w = QSLIST_FIRST(&reversed);
|
|
|
|
QSLIST_REMOVE_HEAD(&reversed, next);
|
|
|
|
QSLIST_INSERT_HEAD(&mutex->to_pop, w, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static CoWaitRecord *pop_waiter(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
CoWaitRecord *w;
|
|
|
|
|
|
|
|
if (QSLIST_EMPTY(&mutex->to_pop)) {
|
|
|
|
move_waiters(mutex);
|
|
|
|
if (QSLIST_EMPTY(&mutex->to_pop)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w = QSLIST_FIRST(&mutex->to_pop);
|
|
|
|
QSLIST_REMOVE_HEAD(&mutex->to_pop, next);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool has_waiters(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
return QSLIST_EMPTY(&mutex->to_pop) || QSLIST_EMPTY(&mutex->from_push);
|
|
|
|
}
|
|
|
|
|
2011-06-30 19:56:46 +04:00
|
|
|
void qemu_co_mutex_init(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
memset(mutex, 0, sizeof(*mutex));
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:12:40 +03:00
|
|
|
static void coroutine_fn qemu_co_mutex_wake(CoMutex *mutex, Coroutine *co)
|
|
|
|
{
|
|
|
|
/* Read co before co->ctx; pairs with smp_wmb() in
|
|
|
|
* qemu_coroutine_enter().
|
|
|
|
*/
|
|
|
|
smp_read_barrier_depends();
|
|
|
|
mutex->ctx = co->ctx;
|
|
|
|
aio_co_wake(co);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void coroutine_fn qemu_co_mutex_lock_slowpath(AioContext *ctx,
|
|
|
|
CoMutex *mutex)
|
2011-06-30 19:56:46 +04:00
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
2017-02-13 21:12:39 +03:00
|
|
|
CoWaitRecord w;
|
|
|
|
unsigned old_handoff;
|
2011-06-30 19:56:46 +04:00
|
|
|
|
|
|
|
trace_qemu_co_mutex_lock_entry(mutex, self);
|
2017-02-13 21:12:39 +03:00
|
|
|
w.co = self;
|
|
|
|
push_waiter(mutex, &w);
|
2011-06-30 19:56:46 +04:00
|
|
|
|
2017-02-13 21:12:39 +03:00
|
|
|
/* This is the "Responsibility Hand-Off" protocol; a lock() picks from
|
|
|
|
* a concurrent unlock() the responsibility of waking somebody up.
|
|
|
|
*/
|
|
|
|
old_handoff = atomic_mb_read(&mutex->handoff);
|
|
|
|
if (old_handoff &&
|
|
|
|
has_waiters(mutex) &&
|
|
|
|
atomic_cmpxchg(&mutex->handoff, old_handoff, 0) == old_handoff) {
|
|
|
|
/* There can be no concurrent pops, because there can be only
|
|
|
|
* one active handoff at a time.
|
|
|
|
*/
|
|
|
|
CoWaitRecord *to_wake = pop_waiter(mutex);
|
|
|
|
Coroutine *co = to_wake->co;
|
|
|
|
if (co == self) {
|
|
|
|
/* We got the lock ourselves! */
|
|
|
|
assert(to_wake == &w);
|
2017-02-13 21:12:40 +03:00
|
|
|
mutex->ctx = ctx;
|
2017-02-13 21:12:39 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:12:40 +03:00
|
|
|
qemu_co_mutex_wake(mutex, co);
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
2017-02-13 21:12:39 +03:00
|
|
|
qemu_coroutine_yield();
|
|
|
|
trace_qemu_co_mutex_lock_return(mutex, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)
|
|
|
|
{
|
2017-02-13 21:12:40 +03:00
|
|
|
AioContext *ctx = qemu_get_current_aio_context();
|
2017-02-13 21:12:39 +03:00
|
|
|
Coroutine *self = qemu_coroutine_self();
|
2017-02-13 21:12:40 +03:00
|
|
|
int waiters, i;
|
|
|
|
|
|
|
|
/* Running a very small critical section on pthread_mutex_t and CoMutex
|
|
|
|
* shows that pthread_mutex_t is much faster because it doesn't actually
|
|
|
|
* go to sleep. What happens is that the critical section is shorter
|
|
|
|
* than the latency of entering the kernel and thus FUTEX_WAIT always
|
|
|
|
* fails. With CoMutex there is no such latency but you still want to
|
|
|
|
* avoid wait and wakeup. So introduce it artificially.
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
retry_fast_path:
|
|
|
|
waiters = atomic_cmpxchg(&mutex->locked, 0, 1);
|
|
|
|
if (waiters != 0) {
|
|
|
|
while (waiters == 1 && ++i < 1000) {
|
|
|
|
if (atomic_read(&mutex->ctx) == ctx) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (atomic_read(&mutex->locked) == 0) {
|
|
|
|
goto retry_fast_path;
|
|
|
|
}
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
waiters = atomic_fetch_inc(&mutex->locked);
|
|
|
|
}
|
2017-02-13 21:12:39 +03:00
|
|
|
|
2017-02-13 21:12:40 +03:00
|
|
|
if (waiters == 0) {
|
2017-02-13 21:12:39 +03:00
|
|
|
/* Uncontended. */
|
|
|
|
trace_qemu_co_mutex_lock_uncontended(mutex, self);
|
2017-02-13 21:12:40 +03:00
|
|
|
mutex->ctx = ctx;
|
2017-02-13 21:12:39 +03:00
|
|
|
} else {
|
2017-02-13 21:12:40 +03:00
|
|
|
qemu_co_mutex_lock_slowpath(ctx, mutex);
|
2017-02-13 21:12:39 +03:00
|
|
|
}
|
2016-08-11 18:45:06 +03:00
|
|
|
mutex->holder = self;
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held++;
|
2011-06-30 19:56:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex)
|
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
|
|
|
trace_qemu_co_mutex_unlock_entry(mutex, self);
|
|
|
|
|
2017-02-13 21:12:39 +03:00
|
|
|
assert(mutex->locked);
|
2016-08-11 18:45:06 +03:00
|
|
|
assert(mutex->holder == self);
|
2011-06-30 19:56:46 +04:00
|
|
|
assert(qemu_in_coroutine());
|
|
|
|
|
2017-02-13 21:12:40 +03:00
|
|
|
mutex->ctx = NULL;
|
2016-08-11 18:45:06 +03:00
|
|
|
mutex->holder = NULL;
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held--;
|
2017-02-13 21:12:39 +03:00
|
|
|
if (atomic_fetch_dec(&mutex->locked) == 1) {
|
|
|
|
/* No waiting qemu_co_mutex_lock(). Pfew, that was easy! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
CoWaitRecord *to_wake = pop_waiter(mutex);
|
|
|
|
unsigned our_handoff;
|
|
|
|
|
|
|
|
if (to_wake) {
|
2017-02-13 21:12:40 +03:00
|
|
|
qemu_co_mutex_wake(mutex, to_wake->co);
|
2017-02-13 21:12:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some concurrent lock() is in progress (we know this because
|
|
|
|
* mutex->locked was >1) but it hasn't yet put itself on the wait
|
|
|
|
* queue. Pick a sequence number for the handoff protocol (not 0).
|
|
|
|
*/
|
|
|
|
if (++mutex->sequence == 0) {
|
|
|
|
mutex->sequence = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
our_handoff = mutex->sequence;
|
|
|
|
atomic_mb_set(&mutex->handoff, our_handoff);
|
|
|
|
if (!has_waiters(mutex)) {
|
|
|
|
/* The concurrent lock has not added itself yet, so it
|
|
|
|
* will be able to pick our handoff.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to do the handoff protocol ourselves; if somebody else has
|
|
|
|
* already taken it, however, we're done and they're responsible.
|
|
|
|
*/
|
|
|
|
if (atomic_cmpxchg(&mutex->handoff, our_handoff, 0) != our_handoff) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-30 19:56:46 +04:00
|
|
|
|
|
|
|
trace_qemu_co_mutex_unlock_return(mutex, self);
|
|
|
|
}
|
2011-08-02 10:32:51 +04:00
|
|
|
|
|
|
|
void qemu_co_rwlock_init(CoRwlock *lock)
|
|
|
|
{
|
|
|
|
memset(lock, 0, sizeof(*lock));
|
|
|
|
qemu_co_queue_init(&lock->queue);
|
2017-02-13 21:12:44 +03:00
|
|
|
qemu_co_mutex_init(&lock->mutex);
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_co_rwlock_rdlock(CoRwlock *lock)
|
|
|
|
{
|
2016-08-11 18:51:59 +03:00
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
2017-02-13 21:12:44 +03:00
|
|
|
qemu_co_mutex_lock(&lock->mutex);
|
|
|
|
/* For fairness, wait if a writer is in line. */
|
|
|
|
while (lock->pending_writer) {
|
|
|
|
qemu_co_queue_wait(&lock->queue, &lock->mutex);
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
|
|
|
lock->reader++;
|
2017-02-13 21:12:44 +03:00
|
|
|
qemu_co_mutex_unlock(&lock->mutex);
|
|
|
|
|
|
|
|
/* The rest of the read-side critical section is run without the mutex. */
|
2016-08-11 18:51:59 +03:00
|
|
|
self->locks_held++;
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_co_rwlock_unlock(CoRwlock *lock)
|
|
|
|
{
|
2016-08-11 18:51:59 +03:00
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
2011-08-02 10:32:51 +04:00
|
|
|
assert(qemu_in_coroutine());
|
2017-02-13 21:12:44 +03:00
|
|
|
if (!lock->reader) {
|
|
|
|
/* The critical section started in qemu_co_rwlock_wrlock. */
|
2011-11-17 17:40:26 +04:00
|
|
|
qemu_co_queue_restart_all(&lock->queue);
|
2011-08-02 10:32:51 +04:00
|
|
|
} else {
|
2017-02-13 21:12:44 +03:00
|
|
|
self->locks_held--;
|
|
|
|
|
|
|
|
qemu_co_mutex_lock(&lock->mutex);
|
2011-08-02 10:32:51 +04:00
|
|
|
lock->reader--;
|
|
|
|
assert(lock->reader >= 0);
|
|
|
|
/* Wakeup only one waiting writer */
|
|
|
|
if (!lock->reader) {
|
|
|
|
qemu_co_queue_next(&lock->queue);
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 21:12:44 +03:00
|
|
|
qemu_co_mutex_unlock(&lock->mutex);
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
|
|
|
|
2017-06-29 16:27:40 +03:00
|
|
|
void qemu_co_rwlock_downgrade(CoRwlock *lock)
|
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
|
|
|
/* lock->mutex critical section started in qemu_co_rwlock_wrlock or
|
|
|
|
* qemu_co_rwlock_upgrade.
|
|
|
|
*/
|
|
|
|
assert(lock->reader == 0);
|
|
|
|
lock->reader++;
|
|
|
|
qemu_co_mutex_unlock(&lock->mutex);
|
|
|
|
|
|
|
|
/* The rest of the read-side critical section is run without the mutex. */
|
|
|
|
self->locks_held++;
|
|
|
|
}
|
|
|
|
|
2011-08-02 10:32:51 +04:00
|
|
|
void qemu_co_rwlock_wrlock(CoRwlock *lock)
|
|
|
|
{
|
2017-02-13 21:12:44 +03:00
|
|
|
qemu_co_mutex_lock(&lock->mutex);
|
|
|
|
lock->pending_writer++;
|
|
|
|
while (lock->reader) {
|
|
|
|
qemu_co_queue_wait(&lock->queue, &lock->mutex);
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
2017-02-13 21:12:44 +03:00
|
|
|
lock->pending_writer--;
|
|
|
|
|
|
|
|
/* The rest of the write-side critical section is run with
|
|
|
|
* the mutex taken, so that lock->reader remains zero.
|
|
|
|
* There is no need to update self->locks_held.
|
|
|
|
*/
|
2011-08-02 10:32:51 +04:00
|
|
|
}
|
2017-06-29 16:27:40 +03:00
|
|
|
|
|
|
|
void qemu_co_rwlock_upgrade(CoRwlock *lock)
|
|
|
|
{
|
|
|
|
Coroutine *self = qemu_coroutine_self();
|
|
|
|
|
|
|
|
qemu_co_mutex_lock(&lock->mutex);
|
|
|
|
assert(lock->reader > 0);
|
|
|
|
lock->reader--;
|
|
|
|
lock->pending_writer++;
|
|
|
|
while (lock->reader) {
|
|
|
|
qemu_co_queue_wait(&lock->queue, &lock->mutex);
|
|
|
|
}
|
|
|
|
lock->pending_writer--;
|
|
|
|
|
|
|
|
/* The rest of the write-side critical section is run with
|
|
|
|
* the mutex taken, similar to qemu_co_rwlock_wrlock. Do
|
|
|
|
* not account for the lock twice in self->locks_held.
|
|
|
|
*/
|
|
|
|
self->locks_held--;
|
|
|
|
}
|