From: Neil Horman Date: Thu, 12 Jun 2025 17:12:14 +0000 (-0400) Subject: update RCU to use the new thread-local key mgmt api X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2cb068fb2259fe8a92d8d67bff7486ded5a31cf2;p=thirdparty%2Fopenssl.git update RCU to use the new thread-local key mgmt api RCU stores a per-thread local structure per context-thread, making it necessecary to move them to the new api to avoid exhausting our OS level thread-local storage resources when creating lots of contexts Reviewed-by: Saša Nedvědický Reviewed-by: Tomas Mraz Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/27794) --- diff --git a/crypto/threads_pthread.c b/crypto/threads_pthread.c index 44d6ebe0923..fd87ccfa033 100644 --- a/crypto/threads_pthread.c +++ b/crypto/threads_pthread.c @@ -12,7 +12,9 @@ #include #include +#include #include "internal/cryptlib.h" +#include "internal/threads_common.h" #include "internal/rcu.h" #include "rcu_internal.h" @@ -292,29 +294,27 @@ static struct rcu_qp *get_hold_current_qp(struct rcu_lock_st *lock) static void ossl_rcu_free_local_data(void *arg) { OSSL_LIB_CTX *ctx = arg; - CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(ctx); - struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey); + struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx, NULL); OPENSSL_free(data); - CRYPTO_THREAD_set_local(lkey, NULL); } void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock) { struct rcu_thr_data *data; int i, available_qp = -1; - CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx); /* * we're going to access current_qp here so ask the * processor to fetch it */ - data = CRYPTO_THREAD_get_local(lkey); + data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx); if (data == NULL) { data = OPENSSL_zalloc(sizeof(*data)); OPENSSL_assert(data != NULL); - CRYPTO_THREAD_set_local(lkey, data); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx, data); ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data); } @@ -341,8 +341,7 @@ void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock) void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock) { int i; - CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx); - struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey); + struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx); uint64_t ret; assert(data != NULL); diff --git a/crypto/threads_win.c b/crypto/threads_win.c index 97b6d3eb739..c443adc27a5 100644 --- a/crypto/threads_win.c +++ b/crypto/threads_win.c @@ -31,6 +31,7 @@ #include #include "internal/common.h" #include "internal/thread_arch.h" +#include "internal/threads_common.h" #include "internal/rcu.h" #include "rcu_internal.h" @@ -221,10 +222,10 @@ static ossl_inline struct rcu_qp *get_hold_current_qp(CRYPTO_RCU_LOCK *lock) static void ossl_rcu_free_local_data(void *arg) { OSSL_LIB_CTX *ctx = arg; - CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(ctx); - struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey); + struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx); + + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx, NULL); OPENSSL_free(data); - CRYPTO_THREAD_set_local(lkey, NULL); } void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock) @@ -232,18 +233,17 @@ void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock) struct rcu_thr_data *data; int i; int available_qp = -1; - CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx); /* * we're going to access current_qp here so ask the * processor to fetch it */ - data = CRYPTO_THREAD_get_local(lkey); + data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx); if (data == NULL) { data = OPENSSL_zalloc(sizeof(*data)); OPENSSL_assert(data != NULL); - CRYPTO_THREAD_set_local(lkey, data); + CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx, data); ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data); } @@ -277,8 +277,7 @@ void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock) void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock) { - CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx); - struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey); + struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx); int i; LONG64 ret;