coroutine-lock: polymorphic CoQueue
v3 cover coroutine-lock: polymorphic CoQueue
There are cases in which a queued coroutine must be restarted from non-coroutine context (with qemu_co_enter_next). In this cases, qemu_co_enter_next also needs to be thread-safe, but it cannot use a CoMutex and so cannot qemu_co_queue_wait. This happens in curl (which right now is rolling its own list of Coroutines) and will happen in Fam's NVMe driver as well.
This series extracts the idea of a polymorphic lockable object from my "scoped lock guard" proposal, and applies it to CoQueue. The implementation of QemuLockable is similar to C11 _Generic, but redone using the preprocessor and GCC builtins for compatibility.
In general, while a bit on the esoteric side, the functionality used to emulate _Generic is fairly old in GCC, and the builtins are already used by include/qemu/atomic.h; the series was tested with Fedora 27 (boot Damn Small Linux via http) and CentOS 6 (compiled only).
Paolo
v1->v2: fix typos and copyright year
v2->v3: add tests, fix brown paper bag bug, avoid -Waddress errors
Paolo Bonzini (5):
test-coroutine: add simple CoMutex test
lockable: add QemuLockable
coroutine-lock: convert CoQueue to use QemuLockable
coroutine-lock: make qemu_co_enter_next thread-safe
curl: convert to CoQueue
block/curl.c | 20 +++-------- fsdev/qemu-fsdev-throttle.c | 4 +-- include/qemu/compiler.h | 40 +++++++++++++++++++++ include/qemu/coroutine.h | 25 ++++++++----- include/qemu/lockable.h | 88 +++++++++++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 5 ++- include/qemu/typedefs.h | 4 +++ tests/test-coroutine.c | 75 ++++++++++++++++++++++++++++++++++++-- util/qemu-coroutine-lock.c | 22 ++++++++---- 9 files changed, 245 insertions(+), 38 deletions(-) create mode 100644 include/qemu/lockable.h
Signature · 1 line
2.14.3
PATCH v3 1/5 test-coroutine: add simple CoMutex test
In preparation for adding a similar test using QemuLockable, add a very simple testcase that has two interleaved calls to lock and unlock.
--- tests/test-coroutine.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-)
tests/test-coroutine.c +48 -2
@@ -175,7 +175,7 @@ static void coroutine_fn c1_fn(void *opaque)qemu_coroutine_enter (c2); } -static void test_co_queue (void ) +static void test_no_dangling_access (void ) {Coroutine *c1;Coroutine *c2;
@@ -195,6 +195,51 @@ static void test_co_queue(void) *c1 = tmp; } +static bool locked = false; +static int done; + +static void coroutine_fn mutex_fn(void *opaque) +{ +CoMutex *m = opaque; +qemu_co_mutex_lock (m); +assert (!locked); + locked = true; +qemu_coroutine_yield (); + locked = false; +qemu_co_mutex_unlock (m); + done++; +} + +static void do_test_co_mutex (CoroutineEntry *entry,void *opaque) +{ +Coroutine *c1 =qemu_coroutine_create (entry, opaque); +Coroutine *c2 =qemu_coroutine_create (entry, opaque); + + done =0 ; +qemu_coroutine_enter (c1); +g_assert (locked); +qemu_coroutine_enter (c2); + +/* Unlock queues c2. It is then started automatically when c1 yields or +* terminates. +*/ +qemu_coroutine_enter (c1); + g_assert_cmpint(done, ==, 1); +g_assert (locked); + +qemu_coroutine_enter (c2); + g_assert_cmpint(done, ==, 2); +g_assert (!locked); +} + +static void test_co_mutex (void ) +{ +CoMutex m; + +qemu_co_mutex_init (&m); +do_test_co_mutex (mutex_fn, &m); +} +/* * Check that creation, enter, and return work */
@@ -422,7 +467,7 @@ int main(int argc, char **argv)* crash, so skip it. */ if (CONFIG_COROUTINE_POOL ) { -g_test_add_func ("/basic/co_queue" , test_co_queue); +g_test_add_func ("/basic/no-dangling-access" , test_no_dangling_access); }g_test_add_func ("/basic/lifecycle" , test_lifecycle);
@@ -432,6 +477,7 @@ int main(int argc, char **argv)g_test_add_func ("/basic/entered" , test_entered);g_test_add_func ("/basic/in_coroutine" , test_in_coroutine);g_test_add_func ("/basic/order" , test_order); +g_test_add_func ("/locking/co-mutex" , test_co_mutex);if (g_test_perf ()) {g_test_add_func ("/perf/lifecycle" , perf_lifecycle);g_test_add_func ("/perf/nesting" , perf_nesting);
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 1/5] test-coroutine: add simple CoMutex test
Quoted 7 lines · Paolo Bonzini
In preparation for adding a similar test using QemuLockable, add a very simple testcase that has two interleaved calls to lock and unlock.
Signed-off-by Paolo Bonzini <[email protected]>--- tests/test-coroutine.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-)
PATCH v3 2/5 lockable: add QemuLockable
In reply to Paolo BonziniQemuLockable is a polymorphic lock type that takes an object and knows which function to use for locking and unlocking. The implementation could use C11 _Generic, but since the support is not very widespread I am instead using __builtin_choose_expr and __builtin_types_compatible_p, which are already used by include/qemu/atomic.h.
QemuLockable can be used to implement lock guards, or to pass around a lock in such a way that a function can release it and re-acquire it. The next patch will do this for CoQueue.
---
v2->v3: now it works :( Also, QEMU_MAKE_LOCKABLE(NULL) returns NULL.
The argument of QEMU_MAKE_LOCKABLE is expected to be a constant, so the test is optimized away.
include/qemu/compiler.h | 40 ++++++++++++++++++++++ include/qemu/coroutine.h | 4 +-- include/qemu/lockable.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 5 ++- include/qemu/typedefs.h | 4 +++ tests/test-coroutine.c | 25 ++++++++++++++ 6 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 include/qemu/lockable.h
include/qemu/compiler.h +40 -0
@@ -111,4 +111,44 @@#define GCC_FMT_ATTR (n, m)#endif +/* Implement C11 _Generic via GCC builtins. Example: +* +* QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x) +* +* The first argument is the discriminator. The last is the default value. +* The middle ones are tuples in "(type, expansion)" format. +*/ + +/* First, find out the number of generic cases. */ +#define QEMU_GENERIC (x, ...) \ + QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +/* There will be extra arguments, but they are not used. */ +#define QEMU_GENERIC_ (x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \ + QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) + +/* Two more helper macros, this time to extract items from a parenthesized +* list. +*/ +#define QEMU_FIRST_ (a, b) a +#define QEMU_SECOND_ (a, b) b + +/* ... and a final one for the common part of the "recursion". */ +#define QEMU_GENERIC_IF (x, type_then, else_) \ + __builtin_choose_expr(__builtin_types_compatible_p(x, \ + QEMU_FIRST_ type_then), \ + QEMU_SECOND_ type_then, else_) + +/* CPP poor man's "recursion". */ +#define QEMU_GENERIC1 (x, a0, ...) (a0) +#define QEMU_GENERIC2 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__)) +#define QEMU_GENERIC3 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__)) +#define QEMU_GENERIC4 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__)) +#define QEMU_GENERIC5 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__)) +#define QEMU_GENERIC6 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__)) +#define QEMU_GENERIC7 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__)) +#define QEMU_GENERIC8 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__)) +#define QEMU_GENERIC9 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__)) +#define QEMU_GENERIC10 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__)) +#endif /* COMPILER_H */
include/qemu/coroutine.h +2 -2
@@ -121,7 +121,7 @@ bool qemu_coroutine_entered(Coroutine *co);* Provides a mutex that can be used to synchronise coroutines */ struct CoWaitRecord ; -typedef struct CoMutex { +struct CoMutex {/* Count of pending lockers; 0 for a free mutex, 1 for an * uncontended mutex. */
@@ -142,7 +142,7 @@ typedef struct CoMutex {unsigned handoff, sequence;Coroutine *holder; -} CoMutex; +};/** * Initialises a CoMutex. This must be called before any other operation is used
include/qemu/lockable.h new file +88 -0
88 lines not on this page · raw message
include/qemu/thread.h +2 -3
@@ -4,7 +4,6 @@#include "qemu/processor.h" #include "qemu/atomic.h" -typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuSemaphore QemuSemaphore ;typedef struct QemuEvent QemuEvent ;
@@ -97,9 +96,9 @@ struct Notifier;void qemu_thread_atexit_add (struct Notifier *notifier);void qemu_thread_atexit_remove (struct Notifier *notifier); -typedef struct QemuSpin { +struct QemuSpin {int value; -}QemuSpin ; +};static inline void qemu_spin_init (QemuSpin *spin) {
include/qemu/typedefs.h +4 -0
@@ -19,6 +19,7 @@ typedef struct BusClass BusClass;typedef struct BusState BusState ;typedef struct Chardev Chardev ;typedef struct CompatProperty CompatProperty ; +typedef struct CoMutex CoMutex ;typedef struct CPUAddressSpace CPUAddressSpace ;typedef struct CPUState CPUState ;typedef struct DeviceListener DeviceListener ;
@@ -86,9 +87,12 @@ typedef struct QEMUBH QEMUBH;typedef struct QemuConsole QemuConsole ;typedef struct QemuDmaBuf QemuDmaBuf ;typedef struct QEMUFile QEMUFile ; +typedef struct QemuLockable QemuLockable ; +typedef struct QemuMutex QemuMutex ;typedef struct QemuOpt QemuOpt ;typedef struct QemuOpts QemuOpts ;typedef struct QemuOptsList QemuOptsList ; +typedef struct QemuSpin QemuSpin ;typedef struct QEMUSGList QEMUSGList ;typedef struct QEMUTimer QEMUTimer ;typedef struct QEMUTimerListGroup QEMUTimerListGroup ;
tests/test-coroutine.c +25 -0
@@ -14,6 +14,7 @@#include "qemu/osdep.h" #include "qemu/coroutine.h" #include "qemu/coroutine_int.h" +#include "qemu/lockable.h" /* * Check that qemu_in_coroutine() works
@@ -210,6 +211,18 @@ static void coroutine_fn mutex_fn(void *opaque) done++; } +static void coroutine_fn lockable_fn(void *opaque) +{ +QemuLockable *x = opaque; +qemu_lockable_lock (x); +assert (!locked); + locked = true; +qemu_coroutine_yield (); + locked = false; +qemu_lockable_unlock (x); + done++; +} +static void do_test_co_mutex (CoroutineEntry *entry,void *opaque) {Coroutine *c1 =qemu_coroutine_create (entry, opaque);
@@ -240,6 +253,17 @@ static void test_co_mutex(void)do_test_co_mutex (mutex_fn, &m); } +static void test_co_mutex_lockable (void ) +{ +CoMutex m; +CoMutex *null_pointer =NULL ; + +qemu_co_mutex_init (&m); +do_test_co_mutex (lockable_fn,QEMU_MAKE_LOCKABLE (&m)); + +g_assert (QEMU_MAKE_LOCKABLE (null_pointer) ==NULL ); +} +/* * Check that creation, enter, and return work */
@@ -478,6 +502,7 @@ int main(int argc, char **argv)g_test_add_func ("/basic/in_coroutine" , test_in_coroutine);g_test_add_func ("/basic/order" , test_order);g_test_add_func ("/locking/co-mutex" , test_co_mutex); +g_test_add_func ("/locking/co-mutex/lockable" , test_co_mutex_lockable);if (g_test_perf ()) {g_test_add_func ("/perf/lifecycle" , perf_lifecycle);g_test_add_func ("/perf/nesting" , perf_nesting);
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 2/5] lockable: add QemuLockable
Quoted 14 lines · Paolo Bonzini
QemuLockable is a polymorphic lock type that takes an object and knows which function to use for locking and unlocking. The implementation could use C11
_Generic, but since the support is not very widespread I am instead using__builtin_choose_exprand__builtin_types_compatible_p, which are already used byinclude/qemu/atomic.h.QemuLockable can be used to implement lock guards, or to pass around a lock in such a way that a function can release it and re-acquire it. The next patch will do this for CoQueue.
Signed-off-by Paolo Bonzini <[email protected]>---
include/qemu/lockable.h line 35
Quoted 5 lines
include/qemu/lockable.h new file +5
-0+ +static inline __attribute__((__always_inline__))QemuLockable * +qemu_make_lockable (void *x,QemuLockable *lockable) +{ +/* We cannot test this in a macro, otherwise we get * compiler
Spurious '*' ?
Quoted 5 lines
include/qemu/lockable.h new file +5
-0+* warnings like "the address of 'm' will always evaluate as 'true'". +*/ +return x ? lockable :NULL ; +} +
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 2/5] lockable: add QemuLockable
In reply to Paolo Bonziniinclude/qemu/lockable.h line 4
Quoted 10 lines · Paolo Bonzini
include/qemu/lockable.h new file +4
-0@@ -0,0 +1,88 @@ +/* +* Polymorphic locking functions (aka poor man templates) +* +* Copyright Red Hat, Inc. 2017
It's curious why then change you made in v2 (2017 - 2018) is reverted in v3 (similar to the commit message). I can fix this and other typos when applying.
Fam
Re: [Qemu-devel] [PATCH 2/5] lockable: add QemuLockable
In reply to Paolo BonziniQuoted 140 lines · Paolo Bonzini
QemuLockable is a polymorphic lock type that takes an object and knows which function to use for locking and unlocking. The implementation could use C11
_Generic, but since the support is not very widespread I am instead using__builtin_choose_exprand__builtin_types_compatible_p, which are already used byinclude/qemu/atomic.h.QemuLockable can be used to implement lock guards, or to pass around a lock in such a way that a function can release it and re-acquire it. The next patch will do this for CoQueue.
Signed-off-by Paolo Bonzini <[email protected]>---
v2->v3: now it works :( Also,QEMU_MAKE_LOCKABLE(NULL)returns NULL.
The argument ofQEMU_MAKE_LOCKABLEis expected to be a constant, so the test is optimized away.include/qemu/compiler.h | 40 ++++++++++++++++++++++ include/qemu/coroutine.h | 4 +-- include/qemu/lockable.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 5 ++- include/qemu/typedefs.h | 4 +++ tests/test-coroutine.c | 25 ++++++++++++++ 6 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 include/qemu/lockable.hinclude/qemu/compiler.h +40
-0@@ -111,4 +111,44 @@#define GCC_FMT_ATTR (n, m)#endif +/* Implement C11 _Generic via GCC builtins. Example: +* +* QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x) +* +* The first argument is the discriminator. The last is the default value. +* The middle ones are tuples in "(type, expansion)" format. +*/ + +/* First, find out the number of generic cases. */ +#define QEMU_GENERIC (x, ...) \ + QEMU_GENERIC_(typeof(x), __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +/* There will be extra arguments, but they are not used. */ +#define QEMU_GENERIC_ (x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, count, ...) \ + QEMU_GENERIC##count(x, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) + +/* Two more helper macros, this time to extract items from a parenthesized +* list. +*/ +#define QEMU_FIRST_ (a, b) a +#define QEMU_SECOND_ (a, b) b + +/* ... and a final one for the common part of the "recursion". */ +#define QEMU_GENERIC_IF (x, type_then, else_) \ + __builtin_choose_expr(__builtin_types_compatible_p(x, \ + QEMU_FIRST_ type_then), \ + QEMU_SECOND_ type_then, else_) + +/* CPP poor man's "recursion". */ +#define QEMU_GENERIC1 (x, a0, ...) (a0) +#define QEMU_GENERIC2 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__)) +#define QEMU_GENERIC3 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__)) +#define QEMU_GENERIC4 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__)) +#define QEMU_GENERIC5 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__)) +#define QEMU_GENERIC6 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__)) +#define QEMU_GENERIC7 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__)) +#define QEMU_GENERIC8 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__)) +#define QEMU_GENERIC9 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__)) +#define QEMU_GENERIC10 (x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__)) +#endif /* COMPILER_H */ include/qemu/coroutine.h +2
-2@@ -121,7 +121,7 @@ bool qemu_coroutine_entered(Coroutine *co);* Provides a mutex that can be used to synchronise coroutines */ struct CoWaitRecord ; -typedef struct CoMutex { +struct CoMutex {/* Count of pending lockers; 0 for a free mutex, 1 for an * uncontended mutex. */ @@ -142,7 +142,7 @@ typedef struct CoMutex {unsigned handoff, sequence;Coroutine *holder; -} CoMutex; +};/** * Initialises a CoMutex. This must be called before any other operation is used include/qemu/lockable.h new file +37
-0@@ -0,0 +1,88 @@ +/* +* Polymorphic locking functions (aka poor man templates) +* +* Copyright Red Hat, Inc. 2017 +* +* Author: Paolo Bonzini <[email protected]> +* +* This work is licensed under the terms of the GNU LGPL, version 2 or later. +* See the COPYING.LIB file in the top-level directory. +* +*/ + +#ifndef QEMU_LOCKABLE_H +#define QEMU_LOCKABLE_H + +#include "qemu/coroutine.h" +#include "qemu/thread.h" + +typedef void QemuLockUnlockFunc (void *); + +struct QemuLockable { +void *object; +QemuLockUnlockFunc *lock; +QemuLockUnlockFunc *unlock; +}; + +/* This function gives link-time errors if an invalid, non-NULL +* pointer type is passed to QEMU_MAKE_LOCKABLE. +*/ +void unknown_lock_type (void *); + +static inline __attribute__((__always_inline__))QemuLockable * +qemu_make_lockable (void *x,QemuLockable *lockable) +{ +/* We cannot test this in a macro, otherwise we get * compiler +* warnings like "the address of 'm' will always evaluate as 'true'". +*/
include/qemu/lockable.h line 47
Quoted 10 lines
include/qemu/lockable.h new file +10
-0+return x ? lockable :NULL ; +} + +/* Auxiliary macros to simplify QEMU_MAKE_LOCABLE. */ +#define QEMU_LOCK_FUNC (x) ((QemuLockUnlockFunc *) \ + QEMU_GENERIC(x, \ + (QemuMutex *, qemu_mutex_lock), \ + (CoMutex *, qemu_co_mutex_lock), \ + (QemuSpin *, qemu_spin_lock), \ + unknown_lock_type))
This optimization doesn't seem to work with --enable-gcov, I think without gcc -O the function is always referenced:
https://travis-ci.org/famz/qemu/jobs/333563215
Fam
Re: [Qemu-devel] [PATCH 2/5] lockable: add QemuLockable
In reply to Paolo BonziniQuoted 5 lines · Paolo Bonzini
include/qemu/lockable.h new file +5
-0+struct QemuLockable { +void *object; +QemuLockUnlockFunc *lock; +QemuLockUnlockFunc *unlock; +};
...
include/qemu/lockable.h line 64
Quoted 9 lines
include/qemu/lockable.h new file +9
-0+/* In C, compound literals have the lifetime of an automatic variable. +* In C++ it would be different, but then C++ wouldn't need QemuLockable +* either... +*/ +#define QEMU_MAKE_LOCKABLE_ (x) qemu_make_lockable((x), &(QemuLockable) { \ + .object = (x), \ + .lock = QEMU_LOCK_FUNC(x), \ + .unlock = QEMU_UNLOCK_FUNC(x), \ + })
After all these tricks we still end up with a struct containing function pointers. That's a little sad because the power of generics is specializing code at compile time.
IMO the generics usage here doesn't have a big pay-off. The generated code is more or less the same as without generics!
It makes me wonder if the API would be more maintainable with:
typedef struct {
QemuLockUnlockFunc *lock;
QemuLockUnlockFunc *unlock;
} LockableOps; extern const LockableOps co_mutex_lockable_ops; extern const LockableOps qemu_spin_lockable_ops;
...
The user passes in the appropriate LockableOps instance for their type and generics are not needed.
This approach means future changes do not require digging through the macros to understand how this stuff works.
Maybe I've missed something?
Stefan
Re: [Qemu-devel] [PATCH 2/5] lockable: add QemuLockable
Quoted 35 lines · Stefan Hajnoczi
Quoted 5 lines · Paolo Bonzini
include/qemu/lockable.h new file +5
-0+struct QemuLockable { +void *object; +QemuLockUnlockFunc *lock; +QemuLockUnlockFunc *unlock; +};...
include/qemu/lockable.h line 64
Quoted 9 lines
include/qemu/lockable.h new file +9
-0+/* In C, compound literals have the lifetime of an automatic variable. +* In C++ it would be different, but then C++ wouldn't need QemuLockable +* either... +*/ +#define QEMU_MAKE_LOCKABLE_ (x) qemu_make_lockable((x), &(QemuLockable) { \ + .object = (x), \ + .lock = QEMU_LOCK_FUNC(x), \ + .unlock = QEMU_UNLOCK_FUNC(x), \ + })After all these tricks we still end up with a struct containing function pointers. That's a little sad because the power of generics is specializing code at compile time.
IMO the generics usage here doesn't have a big pay-off. The generated code is more or less the same as without generics!
It makes me wonder if the API would be more maintainable with:
typedef struct { QemuLockUnlockFunc *lock; QemuLockUnlockFunc *unlock; } LockableOps;extern const LockableOps
co_mutex_lockable_ops; extern const LockableOpsqemu_spin_lockable_ops;
...
Quoted 5 lines
The user passes in the appropriate LockableOps instance for their type and generics are not needed.
This approach means future changes do not require digging through the macros to understand how this stuff works.
The difference between the two is that, once we use it for lock guards, the compiler is able to see constant function pointers and inline everything. If you use LockableOps, it doesn't.
Paolo
Quoted 4 lines
Maybe I've missed something?
Stefan
PATCH v3 3/5 coroutine-lock: convert CoQueue to use QemuLockable
In reply to Paolo BonziniThere are cases in which a queued coroutine must be restarted from non-coroutine context (with qemu_co_enter_next). In this cases, qemu_co_enter_next also needs to be thread-safe, but it cannot use a CoMutex and so cannot qemu_co_queue_wait. Use QemuLockable so that the CoQueue can interchangeably use CoMutex or QemuMutex.
--- include/qemu/coroutine.h | 6 +++++- util/qemu-coroutine-lock.c | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-)
include/qemu/coroutine.h +5 -1
@@ -183,7 +183,9 @@ void qemu_co_queue_init(CoQueue *queue);* caller of the coroutine. The mutex is unlocked during the wait and * locked again afterwards. */ -void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex); +#define qemu_co_queue_wait (queue, lock) \ + qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock);/** * Restarts the next coroutine in the CoQueue and removes it from the queue.
@@ -271,4 +273,6 @@ void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns);*/ void coroutine_fn yield_until_fd_readable(int fd); +#include "qemu/lockable.h" +#endif /* QEMU_COROUTINE_H */
util/qemu-coroutine-lock.c +7 -5
@@ -40,13 +40,13 @@ void qemu_co_queue_init(CoQueue *queue)QSIMPLEQ_INIT (&queue->entries); } -void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex) +void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock) {Coroutine *self =qemu_coroutine_self ();QSIMPLEQ_INSERT_TAIL (&queue->entries, self, co_queue_next); -if (mutex) { -qemu_co_mutex_unlock (mutex); +if (lock) { +qemu_lockable_unlock (lock); }/* There is no race condition here. Other threads will call
@@ -60,9 +60,11 @@ void coroutine_fn qemu_co_queue_wait(CoQueue *queue, CoMutex *mutex)/* 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. +* This could be implemented for CoMutexes, but not really for +* other cases of QemuLockable. */ -if (mutex) { -qemu_co_mutex_lock (mutex); +if (lock) { +qemu_lockable_lock (lock); } }
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 3/5] coroutine-lock: convert CoQueue to use QemuLockable
Quoted 11 lines · Paolo Bonzini
There are cases in which a queued coroutine must be restarted from non-coroutine context (with
qemu_co_enter_next). In this cases,qemu_co_enter_nextalso needs to be thread-safe, but it cannot use a CoMutex and so cannotqemu_co_queue_wait. Use QemuLockable so that the CoQueue can interchangeably use CoMutex or QemuMutex.Signed-off-by Paolo Bonzini <[email protected]>--- include/qemu/coroutine.h | 6 +++++- util/qemu-coroutine-lock.c | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-)
PATCH v3 4/5 coroutine-lock: make qemu_co_enter_next thread-safe
In reply to Paolo Bonziniqemu_co_queue_next does not need to release and re-acquire the mutex, because the queued coroutine does not run immediately. However, this does not hold for qemu_co_enter_next. Now that qemu_co_queue_wait can synchronize (via QemuLockable) with code that is not running in coroutine context, it's important that code using qemu_co_enter_next can easily use a standardized locking idiom.
First of all, qemu_co_enter_next must use aio_co_wake to restart the coroutine. Second, the function gains a second argument, a QemuLockable*, and the comments of qemu_co_queue_next and qemu_co_queue_restart_all are adjusted to clarify the difference.
--- fsdev/qemu-fsdev-throttle.c | 4 ++-- include/qemu/coroutine.h | 15 ++++++++++----- util/qemu-coroutine-lock.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 9 deletions(-)
fsdev/qemu-fsdev-throttle.c +2 -2
@@ -20,13 +20,13 @@static void fsdev_throttle_read_timer_cb (void *opaque) {FsThrottle *fst = opaque; -qemu_co_enter_next (&fst->throttled_reqs[false]); +qemu_co_enter_next (&fst->throttled_reqs[false],NULL ); }static void fsdev_throttle_write_timer_cb (void *opaque) {FsThrottle *fst = opaque; -qemu_co_enter_next (&fst->throttled_reqs[true]); +qemu_co_enter_next (&fst->throttled_reqs[true],NULL ); }void fsdev_throttle_parse_opts (QemuOpts *opts,FsThrottle *fst,Error **errp)
include/qemu/coroutine.h +10 -5
@@ -188,21 +188,26 @@ void qemu_co_queue_init(CoQueue *queue); void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock);/** -* Restarts the next coroutine in the CoQueue and removes it from the queue. +* Removes the next coroutine from the CoQueue; it will run as soon as the +* current one yields. * -* Returns true if a coroutine was restarted, false if the queue is empty. +* Returns true if a coroutine was removed, false if the queue is empty. */ bool coroutine_fn qemu_co_queue_next(CoQueue *queue);/** -* Restarts all coroutines in the CoQueue and leaves the queue empty. +* Empties the CoQueue; all coroutines in it will run in FIFO orer as soon +* as the current one yields. */ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue);/** -* Enter the next coroutine in the queue +* Immediately enter the next coroutine in the queue. Release the mutex +* while it runs. */ -bool qemu_co_enter_next (CoQueue *queue); +#define qemu_co_enter_next (queue, lock) \ + qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +bool qemu_co_enter_next_impl (CoQueue *queue,QemuLockable *lock);/** * Checks if the CoQueue is empty.
util/qemu-coroutine-lock.c +8 -2
@@ -132,7 +132,7 @@ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)qemu_co_queue_do_restart (queue, false); } -bool qemu_co_enter_next (CoQueue *queue) +bool qemu_co_enter_next_impl (CoQueue *queue,QemuLockable *lock) {Coroutine *next;
@@ -142,7 +142,13 @@ bool qemu_co_enter_next(CoQueue *queue) }QSIMPLEQ_REMOVE_HEAD (&queue->entries, co_queue_next); -qemu_coroutine_enter (next); +if (lock) { +qemu_lockable_unlock (lock); + } +aio_co_wake (next); +if (lock) { +qemu_lockable_lock (lock); + }return true; }
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 4/5] coroutine-lock: make qemu_co_enter_next thread-safe
Quoted 12 lines · Paolo Bonzini
qemu_co_queue_nextdoes not need to release and re-acquire the mutex, because the queued coroutine does not run immediately. However, this does not hold forqemu_co_enter_next. Now thatqemu_co_queue_waitcan synchronize (via QemuLockable) with code that is not running in coroutine context, it's important that code usingqemu_co_enter_nextcan easily use a standardized locking idiom.First of all,
qemu_co_enter_nextmust useaio_co_waketo restart the coroutine. Second, the function gains a second argument, a QemuLockable*, and the comments ofqemu_co_queue_nextandqemu_co_queue_restart_allare adjusted to clarify the difference.
Quoted 6 lines
Signed-off-by Paolo Bonzini <[email protected]>---
/**
- * Restarts all coroutines in the CoQueue and leaves the queue empty.
+ * Empties the CoQueue; all coroutines in it will run in FIFO orer as soon
s/orer/order/
Quoted 3 lines
+ * as the current one yields.
*/
voidcoroutine_fnqemu_co_queue_restart_all(CoQueue *queue);
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 4/5] coroutine-lock: make qemu_co_enter_next thread-safe
In reply to Paolo BonziniQuoted 57 lines · Paolo Bonzini
qemu_co_queue_nextdoes not need to release and re-acquire the mutex, because the queued coroutine does not run immediately. However, this does not hold forqemu_co_enter_next. Now thatqemu_co_queue_waitcan synchronize (via QemuLockable) with code that is not running in coroutine context, it's important that code usingqemu_co_enter_nextcan easily use a standardized locking idiom.First of all,
qemu_co_enter_nextmust useaio_co_waketo restart the coroutine. Second, the function gains a second argument, a QemuLockable*, and the comments ofqemu_co_queue_nextandqemu_co_queue_restart_allare adjusted to clarify the difference.Signed-off-by Paolo Bonzini <[email protected]>--- fsdev/qemu-fsdev-throttle.c | 4 ++-- include/qemu/coroutine.h | 15 ++++++++++----- util/qemu-coroutine-lock.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 9 deletions(-)fsdev/qemu-fsdev-throttle.c +2
-2@@ -20,13 +20,13 @@static void fsdev_throttle_read_timer_cb (void *opaque) {FsThrottle *fst = opaque; -qemu_co_enter_next (&fst->throttled_reqs[false]); +qemu_co_enter_next (&fst->throttled_reqs[false],NULL ); }static void fsdev_throttle_write_timer_cb (void *opaque) {FsThrottle *fst = opaque; -qemu_co_enter_next (&fst->throttled_reqs[true]); +qemu_co_enter_next (&fst->throttled_reqs[true],NULL ); }void fsdev_throttle_parse_opts (QemuOpts *opts,FsThrottle *fst,Error **errp)include/qemu/coroutine.h +3
-2@@ -188,21 +188,26 @@ void qemu_co_queue_init(CoQueue *queue); void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock);/** -* Restarts the next coroutine in the CoQueue and removes it from the queue. +* Removes the next coroutine from the CoQueue; it will run as soon as the +* current one yields. * -* Returns true if a coroutine was restarted, false if the queue is empty. +* Returns true if a coroutine was removed, false if the queue is empty. */ bool coroutine_fn qemu_co_queue_next(CoQueue *queue);/**
include/qemu/coroutine.h void qemu_co_queue_init(CoQueue *queue); · line 206
Quoted 10 lines
include/qemu/coroutine.h +4
-2-* Restarts all coroutines in the CoQueue and leaves the queue empty. +* Empties the CoQueue; all coroutines in it will run in FIFO orer as soon +* as the current one yields. */ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue);/** -* Enter the next coroutine in the queue +* Immediately enter the next coroutine in the queue. Release the mutex +* while it runs.
s/mutex/lock/
Is this doc comment correct? I see multiple cases due to aio_co_wake():
1. Called from coroutine context with current AioContext matching next coroutine's AioContext: arrange for next coroutine to be entered *after* we yield (not "immediately").
2. Called from non-coroutine context with current AioContext matching next coroutine's AioContext: immediately enter next coroutine.
3. Called from different AioContext than next coroutine: arrange for next coroutine to be entered at some point.
Quoted 41 lines
include/qemu/coroutine.h +3
-1*/ -bool qemu_co_enter_next (CoQueue *queue); +#define qemu_co_enter_next (queue, lock) \ + qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock)) +bool qemu_co_enter_next_impl (CoQueue *queue,QemuLockable *lock);/** * Checks if the CoQueue is empty. util/qemu-coroutine-lock.c +8
-2@@ -132,7 +132,7 @@ void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue)qemu_co_queue_do_restart (queue, false); } -bool qemu_co_enter_next (CoQueue *queue) +bool qemu_co_enter_next_impl (CoQueue *queue,QemuLockable *lock) {Coroutine *next;@@ -142,7 +142,13 @@ bool qemu_co_enter_next(CoQueue *queue) }QSIMPLEQ_REMOVE_HEAD (&queue->entries, co_queue_next); -qemu_coroutine_enter (next); +if (lock) { +qemu_lockable_unlock (lock); + } +aio_co_wake (next); +if (lock) { +qemu_lockable_lock (lock); + }return true; }Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 4/5] coroutine-lock: make qemu_co_enter_next thread-safe
Quoted 13 lines · Stefan Hajnoczi
Quoted 4 lines
/**
- * Enter the next coroutine in the queue
+ * Immediately enter the next coroutine in the queue. Release the mutex
+ * while it runs.s/mutex/lock/
Is this doc comment correct? I see multiple cases due to
aio_co_wake():1. Called from coroutine context with current AioContext matching next coroutine's AioContext: arrange for next coroutine to be entered *after* we yield (not "immediately").
Quoted 5 lines
2. Called from non-coroutine context with current AioContext matching next coroutine's AioContext: immediately enter next coroutine.
3. Called from different AioContext than next coroutine: arrange for next coroutine to be entered at some point.
Good point, it needs to be more specific. Here is the best I could come up with:
+ * Removes the next coroutine from the CoQueue, and wake it up.
* Returns true if a coroutine was removed, false if the queue is empty. */
bool coroutine_fn qemu_co_queue_next(CoQueue *queue);
...
+ * Empties the CoQueue; all coroutines are woken up. */
void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue);
...
+ * Removes the next coroutine from the CoQueue, and wake it up. Unlike
+ * qemu_co_queue_next, this function releases the lock during aio_co_wake
+ * because it is meant to be used outside coroutine context; in that case, the
+ * coroutine is entered immediately, before qemu_co_enter_next returns.
+ *
+ * If used in coroutine context, qemu_co_enter_next is equivalent to
+ * qemu_co_queue_next.
*/
#define qemu_co_enter_next(queue, lock) \
qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock))
PATCH v3 5/5 curl: convert to CoQueue
In reply to Paolo BonziniNow that CoQueues can use a QemuMutex for thread-safety, there is no need for curl to roll its own coroutine queue. Coroutines can be placed directly on the queue instead of using a list of CURLAIOCBs.
--- block/curl.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)
block/curl.c +4 -16
@@ -101,8 +101,6 @@ typedef struct CURLAIOCB {size_t start;size_t end; - -QSIMPLEQ_ENTRY (CURLAIOCB) next; }CURLAIOCB ; typedef struct CURLSocket {
@@ -138,7 +136,7 @@ typedef struct BDRVCURLState {bool accept_range;AioContext *aio_context;QemuMutex mutex; - QSIMPLEQ_HEAD(, CURLAIOCB) free_state_waitq; +CoQueue free_state_waitq;char *username;char *password;char *proxyusername;
@@ -538,7 +536,6 @@ static int curl_init_state(BDRVCURLState *s, CURLState *state)/* Called with s->mutex held. */ static void curl_clean_state (CURLState *s) { -CURLAIOCB *next;int j;for (j =0 ; j <CURL_NUM_ACB ; j++) {assert (!s->acb[j]);
@@ -556,13 +553,7 @@ static void curl_clean_state(CURLState *s) s->in_use =0 ; - next =QSIMPLEQ_FIRST (&s->s->free_state_waitq); -if (next) { -QSIMPLEQ_REMOVE_HEAD (&s->s->free_state_waitq, next); -qemu_mutex_unlock (&s->s->mutex); -aio_co_wake (next->co); -qemu_mutex_lock (&s->s->mutex); - } +qemu_co_enter_next (&s->s->free_state_waitq, &s->s->mutex); } static void curl_parse_filename(const char *filename, QDict *options,
@@ -784,7 +775,7 @@ static int curl_open(BlockDriverState *bs, QDict *options, int flags, }DPRINTF ("CURL: Opening %s\n" , file); -QSIMPLEQ_INIT (&s->free_state_waitq); +qemu_co_queue_init (&s->free_state_waitq); s->aio_context =bdrv_get_aio_context (bs); s->url =g_strdup (file);qemu_mutex_lock (&s->mutex);
@@ -888,10 +879,7 @@ static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb)if (state) {break ; } -QSIMPLEQ_INSERT_TAIL (&s->free_state_waitq, acb, next); -qemu_mutex_unlock (&s->mutex); -qemu_coroutine_yield (); -qemu_mutex_lock (&s->mutex); +qemu_co_queue_wait (&s->free_state_waitq, &s->mutex); }if (curl_init_state (s, state) <0 ) {
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 5/5] curl: convert to CoQueue
Quoted 8 lines · Paolo Bonzini
Now that CoQueues can use a QemuMutex for thread-safety, there is no need for curl to roll its own coroutine queue. Coroutines can be placed directly on the queue instead of using a list of CURLAIOCBs.
Signed-off-by Paolo Bonzini <[email protected]>--- block/curl.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)
Quoted 17 lines · Paolo Bonzini
There are cases in which a queued coroutine must be restarted from non-coroutine context (with
qemu_co_enter_next). In this cases,qemu_co_enter_nextalso needs to be thread-safe, but it cannot use a CoMutex and so cannotqemu_co_queue_wait. This happens in curl (which right now is rolling its own list of Coroutines) and will happen in Fam's NVMe driver as well.This series extracts the idea of a polymorphic lockable object from my "scoped lock guard" proposal, and applies it to CoQueue. The implementation of QemuLockable is similar to C11
_Generic, but redone using the preprocessor and GCC builtins for compatibility.In general, while a bit on the esoteric side, the functionality used to emulate
_Genericis fairly old in GCC, and the builtins are already used byinclude/qemu/atomic.h; the series was tested with Fedora 27 (boot Damn Small Linux via http) and CentOS 6 (compiled only).
Quoted 5 lines
Paolo
v1->v2: fix typos and copyright year
v2->v3: add tests, fix brown paper bag bug, avoid -Waddress errors
I spotted a few typos, but did not rigorously test it enough to leave R-b. At any rate, writing polymorphic code in C is an interesting exercise, so it was fun learning from this thread.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
This series seems to have some coding style problems. See output below for more information:
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v3 0/5] coroutine-lock: polymorphic CoQueue
=== TEST SCRIPT BEGIN ===
#!/bin/bash
BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0
git config --local diff.renamelimit 0
git config --local diff.renames True
commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do
echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
failed=1
echo
fi
n=$((n+1))done
exit $failed
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
a3f9362af5..2077fef91d master -> master
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'31cc3c8bd1 curl: convert to CoQueueb9796ab014 coroutine-lock: make qemu_co_enter_next thread-safe 0a41dcd114 coroutine-lock: convert CoQueue to use QemuLockable 70398398ad lockable: add QemuLockable695a0dcaa5 test-coroutine: add simple CoMutex test
=== OUTPUT BEGIN ===
Checking PATCH 1/5: test-coroutine: add simple CoMutex test...
ERROR: do not initialise statics to 0 or NULL
#29: FILE: tests/test-coroutine.c:198:
+static bool locked = false;
total: 1 errors, 0 warnings, 74 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 2/5: lockable: add QemuLockable...
WARNING: line over 80 characters
#58: FILE: include/qemu/compiler.h:144:
+#define QEMU_GENERIC2(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC1(x, __VA_ARGS__))
WARNING: line over 80 characters
#59: FILE: include/qemu/compiler.h:145:
+#define QEMU_GENERIC3(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC2(x, __VA_ARGS__))
WARNING: line over 80 characters
#60: FILE: include/qemu/compiler.h:146:
+#define QEMU_GENERIC4(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC3(x, __VA_ARGS__))
WARNING: line over 80 characters
#61: FILE: include/qemu/compiler.h:147:
+#define QEMU_GENERIC5(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC4(x, __VA_ARGS__))
WARNING: line over 80 characters
#62: FILE: include/qemu/compiler.h:148:
+#define QEMU_GENERIC6(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC5(x, __VA_ARGS__))
WARNING: line over 80 characters
#63: FILE: include/qemu/compiler.h:149:
+#define QEMU_GENERIC7(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC6(x, __VA_ARGS__))
WARNING: line over 80 characters
#64: FILE: include/qemu/compiler.h:150:
+#define QEMU_GENERIC8(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC7(x, __VA_ARGS__))
WARNING: line over 80 characters
#65: FILE: include/qemu/compiler.h:151:
+#define QEMU_GENERIC9(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC8(x, __VA_ARGS__))
WARNING: line over 80 characters
#66: FILE: include/qemu/compiler.h:152:
+#define QEMU_GENERIC10(x, a0, ...) QEMU_GENERIC_IF(x, a0, QEMU_GENERIC9(x, __VA_ARGS__))
total: 0 errors, 9 warnings, 234 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 3/5: coroutine-lock: convert CoQueue to use QemuLockable... Checking PATCH 4/5: coroutine-lock: make qemu_co_enter_next thread-safe... Checking PATCH 5/5: curl: convert to CoQueue...
=== OUTPUT END ===
Test command exited with code: 1
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]