From c2a5d19bd55488d582955d17043156325b49454e Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 25 Feb 2021 13:25:01 -0600 Subject: [PATCH] coro: protect first initialization of coroutine with new API (#3055) When libco starts, it might enter in a race condition if multiple threads are trying to initialize the 'co_swap' function, this check is done on every coroutine creation: ==346246== Possible data race during read of size 8 at 0x5CA890 by thread #5 ==346246== Locks held: none ==346246== at 0x48EFAE: co_create (amd64.c:132) ==346246== by 0x173035: flb_output_coro_create (flb_output.h:511) ==346246== by 0x173035: output_thread (flb_output_thread.c:281) ==346246== by 0x1889BE: step_callback (flb_worker.c:44) ==346246== by 0x4843B1A: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_helgrind-amd64-linux.so) ==346246== by 0x487E58F: start_thread (pthread_create.c:463) ==346246== by 0x4F47222: clone (clone.S:95) ==346246== ==346246== This conflicts with a previous write of size 8 by thread #4 ==346246== Locks held: none ==346246== at 0x48EFCB: co_create (amd64.c:134) ==346246== by 0x173035: flb_output_coro_create (flb_output.h:511) ==346246== by 0x173035: output_thread (flb_output_thread.c:281) ==346246== by 0x1889BE: step_callback (flb_worker.c:44) ==346246== by 0x4843B1A: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_helgrind-amd64-linux.so) ==346246== by 0x487E58F: start_thread (pthread_create.c:463) ==346246== by 0x4F47222: clone (clone.S:95) ==346246== Address 0x5ca890 is 0 bytes inside data symbol "co_swap" This patch introduce a new API for flb_coro interface that aims to be called inside every worker thread. The access to this first initialization is protected. No more race conditions on that piece of code has been seen with valgrind after the usage of this new function (next patches). Signed-off-by: Eduardo Silva --- include/fluent-bit/flb_coro.h | 2 ++ src/flb_coro.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/fluent-bit/flb_coro.h b/include/fluent-bit/flb_coro.h index 987a2164175..c00781135f7 100644 --- a/include/fluent-bit/flb_coro.h +++ b/include/fluent-bit/flb_coro.h @@ -91,6 +91,8 @@ static FLB_INLINE void flb_coro_destroy(struct flb_coro *coro) #define flb_coro_return(th) co_switch(th->caller) void flb_coro_init(); +void flb_coro_thread_init(); + struct flb_coro *flb_coro_get(); void flb_coro_set(struct flb_coro *coro); diff --git a/src/flb_coro.c b/src/flb_coro.c index fa887c53ecc..06bc865e979 100644 --- a/src/flb_coro.c +++ b/src/flb_coro.c @@ -24,9 +24,23 @@ FLB_TLS_DEFINE(struct flb_coro, flb_coro_key); +static pthread_mutex_t coro_mutex_init; + void flb_coro_init() { FLB_TLS_INIT(flb_coro_key); + pthread_mutex_init(&coro_mutex_init, NULL); +} + +void flb_coro_thread_init() +{ + size_t s; + cothread_t th; + + pthread_mutex_lock(&coro_mutex_init); + th = co_create(256, NULL, &s); + co_delete(th); + pthread_mutex_unlock(&coro_mutex_init); } struct flb_coro *flb_coro_get()