From: Weili Qian <qianweili(a)huawei.com>
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 <qianweili(a)huawei.com>
---
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 e02b563..74dab39 100644
--- a/src/e_uadk.c
+++ b/src/e_uadk.c
@@ -42,6 +42,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;
@@ -218,12 +220,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 384e035..d210c3d 100644
--- a/src/uadk.h
+++ b/src/uadk.h
@@ -42,4 +42,9 @@ extern int uadk_e_bind_ecc(ENGINE *e);
extern void uadk_e_destroy_ecc(void);
extern int uadk_e_is_env_enabled(const char *alg_name);
extern int uadk_e_set_env(const char *var_name, int numa_id);
+extern void uadk_e_ecc_lock_init(void);
+extern void uadk_e_rsa_lock_init(void);
+extern void uadk_e_dh_lock_init(void);
+extern void uadk_e_cipher_lock_init(void);
+extern void uadk_e_digest_lock_init(void);
#endif
diff --git a/src/uadk_cipher.c b/src/uadk_cipher.c
index 569ee6e..0b99753 100644
--- a/src/uadk_cipher.c
+++ b/src/uadk_cipher.c
@@ -1066,8 +1066,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);
}
@@ -1150,3 +1148,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 40fb583..75c9e5a 100644
--- a/src/uadk_dh.c
+++ b/src/uadk_dh.c
@@ -896,8 +896,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());
}
@@ -907,3 +905,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 330a1c2..70f51d3 100644
--- a/src/uadk_digest.c
+++ b/src/uadk_digest.c
@@ -793,6 +793,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,
@@ -838,8 +843,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 f27e2f5..5300327 100644
--- a/src/uadk_pkey.c
+++ b/src/uadk_pkey.c
@@ -618,6 +618,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;
@@ -634,8 +639,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 2f78a37..1579539 100644
--- a/src/uadk_rsa.c
+++ b/src/uadk_rsa.c
@@ -1832,8 +1832,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());
}
@@ -1844,3 +1842,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.30.0