From 0245d700da510dd54926eab3fb9377bde991024f Mon Sep 17 00:00:00 2001 From: Weili Qian Date: Tue, 26 Jul 2022 10:54:03 +0800 Subject: [PATCH 56/57] engine: initialize resources only once If multiple threads load the engine repeatedly, the poll_queue and global locks in the algorithm API are repeatedly initialized. As a result, tasks of other threads are abnormal. Therefore, this patch moves resource initialization to the function 'uadk_init' and only initialize once. Signed-off-by: Weili Qian --- src/e_uadk.c | 37 +++++++++++++++++++++++++++++++++---- src/uadk.h | 5 +++++ src/uadk_cipher.c | 7 +++++-- src/uadk_dh.c | 7 +++++-- src/uadk_digest.c | 7 +++++-- src/uadk_pkey.c | 7 +++++-- src/uadk_rsa.c | 6 ++++-- 7 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/e_uadk.c b/src/e_uadk.c index 23fe0f2..1b95284 100644 --- a/src/e_uadk.c +++ b/src/e_uadk.c @@ -43,6 +43,8 @@ static int uadk_digest; static int uadk_rsa; static int uadk_dh; static int uadk_ecc; +static int uadk_inited; +static pthread_mutex_t uadk_engine_mutex = PTHREAD_MUTEX_INITIALIZER; #ifdef KAE static int uadk_cipher_nosva; @@ -219,12 +221,41 @@ static int uadk_destroy(ENGINE *e) uadk_e_destroy_ecc(); if (uadk_dh) uadk_e_destroy_dh(); + + pthread_mutex_lock(&uadk_engine_mutex); + uadk_inited = 0; + pthread_mutex_unlock(&uadk_engine_mutex); + return 1; } - static int uadk_init(ENGINE *e) { + int ret; + + pthread_mutex_lock(&uadk_engine_mutex); + if (uadk_inited) { + pthread_mutex_unlock(&uadk_engine_mutex); + return 1; + } + + if (uadk_cipher || uadk_digest || uadk_rsa || uadk_dh || uadk_ecc) + async_module_init(); + + if (uadk_digest) + uadk_e_digest_lock_init(); + if (uadk_cipher) + uadk_e_cipher_lock_init(); + if (uadk_rsa) + uadk_e_rsa_lock_init(); + if (uadk_dh) + uadk_e_dh_lock_init(); + if (uadk_ecc) + uadk_e_ecc_lock_init(); + + uadk_inited = 1; + pthread_mutex_unlock(&uadk_engine_mutex); + return 1; } @@ -360,10 +391,8 @@ static int bind_fn(ENGINE *e, const char *id) #endif bind_fn_uadk_alg(e); - if (uadk_cipher || uadk_digest || uadk_rsa || uadk_dh || uadk_ecc) { - async_module_init(); + if (uadk_cipher || uadk_digest || uadk_rsa || uadk_dh || uadk_ecc) pthread_atfork(NULL, NULL, engine_init_child_at_fork_handler); - } ret = ENGINE_set_ctrl_function(e, uadk_engine_ctrl); if (ret != 1) { diff --git a/src/uadk.h b/src/uadk.h index 0188f0b..cd3447c 100644 --- a/src/uadk.h +++ b/src/uadk.h @@ -44,4 +44,9 @@ int uadk_e_bind_ecc(ENGINE *e); void uadk_e_destroy_ecc(void); int uadk_e_is_env_enabled(const char *alg_name); int uadk_e_set_env(const char *var_name, int numa_id); +void uadk_e_ecc_lock_init(void); +void uadk_e_rsa_lock_init(void); +void uadk_e_dh_lock_init(void); +void uadk_e_cipher_lock_init(void); +void uadk_e_digest_lock_init(void); #endif diff --git a/src/uadk_cipher.c b/src/uadk_cipher.c index 54d0a7d..c5bc7af 100644 --- a/src/uadk_cipher.c +++ b/src/uadk_cipher.c @@ -1076,8 +1076,6 @@ int uadk_e_bind_cipher(ENGINE *e) if (platform > KUNPENG920) bind_v3_cipher(); - pthread_spin_init(&engine.lock, PTHREAD_PROCESS_PRIVATE); - return ENGINE_set_ciphers(e, uadk_e_engine_ciphers); } @@ -1160,3 +1158,8 @@ void uadk_e_destroy_cipher(void) if (platform > KUNPENG920) destroy_v3_cipher(); } + +void uadk_e_cipher_lock_init(void) +{ + pthread_spin_init(&engine.lock, PTHREAD_PROCESS_PRIVATE); +} diff --git a/src/uadk_dh.c b/src/uadk_dh.c index cf319e5..37f84e9 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -902,8 +902,6 @@ static void uadk_e_delete_dh_meth(void) int uadk_e_bind_dh(ENGINE *e) { - pthread_spin_init(&g_dh_res.lock, PTHREAD_PROCESS_PRIVATE); - return ENGINE_set_DH(e, uadk_e_get_dh_methods()); } @@ -913,3 +911,8 @@ void uadk_e_destroy_dh(void) uadk_e_delete_dh_meth(); uadk_e_wd_dh_uninit(); } + +void uadk_e_dh_lock_init(void) +{ + pthread_spin_init(&g_dh_res.lock, PTHREAD_PROCESS_PRIVATE); +} diff --git a/src/uadk_digest.c b/src/uadk_digest.c index 853aa39..b2646cb 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -804,6 +804,11 @@ do { \ return 0; \ } while (0) +void uadk_e_digest_lock_init(void) +{ + pthread_spin_init(&engine.lock, PTHREAD_PROCESS_PRIVATE); +} + int uadk_e_bind_digest(ENGINE *e) { UADK_DIGEST_DESCR(md5, md5WithRSAEncryption, MD5_DIGEST_LENGTH, @@ -849,8 +854,6 @@ int uadk_e_bind_digest(ENGINE *e) uadk_e_digest_final, uadk_e_digest_cleanup, uadk_e_digest_copy); - pthread_spin_init(&engine.lock, PTHREAD_PROCESS_PRIVATE); - return ENGINE_set_digests(e, uadk_engine_digests); } diff --git a/src/uadk_pkey.c b/src/uadk_pkey.c index 9a3a725..211f1cc 100644 --- a/src/uadk_pkey.c +++ b/src/uadk_pkey.c @@ -615,6 +615,11 @@ static int uadk_ecc_bind_pmeth(ENGINE *e) return ENGINE_set_pkey_meths(e, get_pkey_meths); } +void uadk_e_ecc_lock_init(void) +{ + pthread_spin_init(&ecc_res.lock, PTHREAD_PROCESS_PRIVATE); +} + int uadk_e_bind_ecc(ENGINE *e) { int ret; @@ -631,8 +636,6 @@ int uadk_e_bind_ecc(ENGINE *e) return ret; } - pthread_spin_init(&ecc_res.lock, PTHREAD_PROCESS_PRIVATE); - return ret; } diff --git a/src/uadk_rsa.c b/src/uadk_rsa.c index 03f2ce5..ef1739d 100644 --- a/src/uadk_rsa.c +++ b/src/uadk_rsa.c @@ -1790,8 +1790,6 @@ static void uadk_e_delete_rsa_meth(void) */ int uadk_e_bind_rsa(ENGINE *e) { - pthread_spin_init(&g_rsa_res.lock, PTHREAD_PROCESS_PRIVATE); - return ENGINE_set_RSA(e, uadk_e_get_rsa_methods()); } @@ -1802,3 +1800,7 @@ void uadk_e_destroy_rsa(void) uadk_e_rsa_uninit(); } +void uadk_e_rsa_lock_init(void) +{ + pthread_spin_init(&g_rsa_res.lock, PTHREAD_PROCESS_PRIVATE); +} -- 2.27.0