From: Matt Caswell Date: Tue, 22 Dec 2020 15:16:51 +0000 (+0000) Subject: Optimise OPENSSL_init_crypto to not need a lock when loading config X-Git-Tag: openssl-3.0.0-alpha10~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ae031148fde2b55238d56dcbe4ac05625382d970;p=thirdparty%2Fopenssl.git Optimise OPENSSL_init_crypto to not need a lock when loading config Most of the time we don't have any explicit settings when loading a config file. Therefore we optimise things so that we don't need to use a lock in that instance. Partially addresses performance issues in #13725 Reviewed-by: Dmitry Belyavskiy (Merged from https://github.com/openssl/openssl/pull/13731) --- diff --git a/crypto/init.c b/crypto/init.c index ba8706655b4..f1100df169b 100644 --- a/crypto/init.c +++ b/crypto/init.c @@ -233,8 +233,16 @@ static CRYPTO_ONCE config = CRYPTO_ONCE_STATIC_INIT; static int config_inited = 0; static const OPENSSL_INIT_SETTINGS *conf_settings = NULL; DEFINE_RUN_ONCE_STATIC(ossl_init_config) +{ + int ret = openssl_config_int(NULL); + + config_inited = 1; + return ret; +} +DEFINE_RUN_ONCE_STATIC_ALT(ossl_init_config_settings, ossl_init_config) { int ret = openssl_config_int(conf_settings); + config_inited = 1; return ret; } @@ -539,11 +547,18 @@ int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) if (opts & OPENSSL_INIT_LOAD_CONFIG) { int ret; - CRYPTO_THREAD_write_lock(init_lock); - conf_settings = settings; - ret = RUN_ONCE(&config, ossl_init_config); - conf_settings = NULL; - CRYPTO_THREAD_unlock(init_lock); + + if (settings == NULL) { + ret = RUN_ONCE(&config, ossl_init_config); + } else { + CRYPTO_THREAD_write_lock(init_lock); + conf_settings = settings; + ret = RUN_ONCE_ALT(&config, ossl_init_config_settings, + ossl_init_config); + conf_settings = NULL; + CRYPTO_THREAD_unlock(init_lock); + } + if (ret <= 0) return 0; }