libwd/0021-uadk-add-init-wait-timeout.patch
2023-09-28 09:15:32 +08:00

381 lines
10 KiB
Diff

From 9fe6d47a877510efc339a1d51c86915f42ea40e3 Mon Sep 17 00:00:00 2001
From: Shangbin Liu <liushangbin@hisilicon.com>
Date: Sat, 26 Aug 2023 11:06:00 +0800
Subject: [PATCH 21/26] uadk: add init wait timeout
Add init wait timeout judgment to prevent
deadlocks when making loopback calls from
the same thread.
Signed-off-by: Shangbin Liu <liushangbin@hisilicon.com>
---
include/wd_util.h | 7 ++++---
wd_aead.c | 16 +++++++---------
wd_cipher.c | 15 +++++++--------
wd_comp.c | 15 +++++++--------
wd_dh.c | 16 +++++++---------
wd_digest.c | 16 +++++++---------
wd_ecc.c | 15 +++++++--------
wd_rsa.c | 16 +++++++---------
wd_util.c | 18 +++++++++++-------
9 files changed, 64 insertions(+), 70 deletions(-)
diff --git a/include/wd_util.h b/include/wd_util.h
index cb07657..be9798c 100644
--- a/include/wd_util.h
+++ b/include/wd_util.h
@@ -387,10 +387,11 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched);
* if need initialization.
* @status: algorithm initialization status.
*
- * Return true if need initialization and false if initialized, otherwise will wait
- * last initialization result.
+ * Return 0 if need initialization.
+ * Return -WD_EEXIST if the algorithm has been initialized.
+ * Return -WD_ETIMEDOUT if wait timeout.
*/
-bool wd_alg_try_init(enum wd_status *status);
+int wd_alg_try_init(enum wd_status *status);
/**
* wd_alg_set_init() - Set the algorithm status as WD_INIT.
diff --git a/wd_aead.c b/wd_aead.c
index cd0812e..ed7b987 100644
--- a/wd_aead.c
+++ b/wd_aead.c
@@ -452,14 +452,13 @@ out_clear_ctx_config:
int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_aead_clear_status);
- flag = wd_alg_try_init(&wd_aead_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_aead_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -524,14 +523,13 @@ int wd_aead_init2_(char *alg, __u32 sched_type, int task_type,
{
struct wd_ctx_nums aead_ctx_num[WD_DIGEST_CIPHER_DECRYPTION + 1] = {0};
struct wd_ctx_params aead_ctx_params = {0};
- int ret = -WD_EINVAL;
- bool flag;
+ int state, ret = -WD_EINVAL;
pthread_atfork(NULL, NULL, wd_aead_clear_status);
- flag = wd_alg_try_init(&wd_aead_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_aead_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_cipher.c b/wd_cipher.c
index 8acadcd..e5ed3c2 100644
--- a/wd_cipher.c
+++ b/wd_cipher.c
@@ -353,14 +353,13 @@ static int wd_cipher_common_uninit(void)
int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_cipher_clear_status);
- flag = wd_alg_try_init(&wd_cipher_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_cipher_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -401,14 +400,14 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_p
{
struct wd_ctx_nums cipher_ctx_num[WD_CIPHER_DECRYPTION + 1] = {0};
struct wd_ctx_params cipher_ctx_params = {0};
- int ret = -WD_EINVAL;
+ int state, ret = -WD_EINVAL;
bool flag;
pthread_atfork(NULL, NULL, wd_cipher_clear_status);
- flag = wd_alg_try_init(&wd_cipher_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_cipher_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_comp.c b/wd_comp.c
index a23dee1..21c9928 100644
--- a/wd_comp.c
+++ b/wd_comp.c
@@ -173,14 +173,13 @@ static int wd_comp_uninit_nolock(void)
int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_comp_clear_status);
- flag = wd_alg_try_init(&wd_comp_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_comp_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -221,14 +220,14 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par
{
struct wd_ctx_nums comp_ctx_num[WD_DIR_MAX] = {0};
struct wd_ctx_params comp_ctx_params = {0};
- int ret = -WD_EINVAL;
+ int state, ret = -WD_EINVAL;
bool flag;
pthread_atfork(NULL, NULL, wd_comp_clear_status);
- flag = wd_alg_try_init(&wd_comp_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_comp_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_dh.c b/wd_dh.c
index 97df515..40a52e5 100644
--- a/wd_dh.c
+++ b/wd_dh.c
@@ -140,14 +140,13 @@ static int wd_dh_common_uninit(void)
int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_dh_clear_status);
- flag = wd_alg_try_init(&wd_dh_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_dh_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -188,14 +187,13 @@ int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_param
{
struct wd_ctx_nums dh_ctx_num[WD_DH_PHASE2] = {0};
struct wd_ctx_params dh_ctx_params = {0};
- int ret = -WD_EINVAL;
- bool flag;
+ int state, ret = -WD_EINVAL;
pthread_atfork(NULL, NULL, wd_dh_clear_status);
- flag = wd_alg_try_init(&wd_dh_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_dh_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_digest.c b/wd_digest.c
index 9dc4bb3..c8ccc8d 100644
--- a/wd_digest.c
+++ b/wd_digest.c
@@ -266,14 +266,13 @@ out_clear_ctx_config:
int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_digest_clear_status);
- flag = wd_alg_try_init(&wd_digest_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_digest_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -334,14 +333,13 @@ int wd_digest_init2_(char *alg, __u32 sched_type, int task_type,
{
struct wd_ctx_params digest_ctx_params = {0};
struct wd_ctx_nums digest_ctx_num = {0};
- int ret = -WD_EINVAL;
- bool flag;
+ int state, ret = -WD_EINVAL;
pthread_atfork(NULL, NULL, wd_digest_clear_status);
- flag = wd_alg_try_init(&wd_digest_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_digest_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_ecc.c b/wd_ecc.c
index 938e516..4323e54 100644
--- a/wd_ecc.c
+++ b/wd_ecc.c
@@ -203,14 +203,13 @@ static int wd_ecc_common_uninit(void)
int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_ecc_clear_status);
- flag = wd_alg_try_init(&wd_ecc_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_ecc_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -251,14 +250,14 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para
{
struct wd_ctx_nums ecc_ctx_num[WD_EC_OP_MAX] = {0};
struct wd_ctx_params ecc_ctx_params = {0};
- int ret = -WD_EINVAL;
+ int state, ret = -WD_EINVAL;
bool flag;
pthread_atfork(NULL, NULL, wd_ecc_clear_status);
- flag = wd_alg_try_init(&wd_ecc_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_ecc_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_rsa.c b/wd_rsa.c
index 2993637..1813676 100644
--- a/wd_rsa.c
+++ b/wd_rsa.c
@@ -178,14 +178,13 @@ static int wd_rsa_common_uninit(void)
int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
- bool flag;
int ret;
pthread_atfork(NULL, NULL, wd_rsa_clear_status);
- flag = wd_alg_try_init(&wd_rsa_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ ret = wd_alg_try_init(&wd_rsa_setting.status);
+ if (ret)
+ return ret;
ret = wd_init_param_check(config, sched);
if (ret)
@@ -226,14 +225,13 @@ int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para
{
struct wd_ctx_nums rsa_ctx_num[WD_RSA_GENKEY] = {0};
struct wd_ctx_params rsa_ctx_params = {0};
- int ret = -WD_EINVAL;
- bool flag;
+ int state, ret = -WD_EINVAL;
pthread_atfork(NULL, NULL, wd_rsa_clear_status);
- flag = wd_alg_try_init(&wd_rsa_setting.status);
- if (!flag)
- return -WD_EEXIST;
+ state = wd_alg_try_init(&wd_rsa_setting.status);
+ if (state)
+ return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
diff --git a/wd_util.c b/wd_util.c
index 937ee2a..cb89709 100644
--- a/wd_util.c
+++ b/wd_util.c
@@ -25,6 +25,8 @@
#define WD_INIT_SLEEP_UTIME 1000
#define WD_INIT_RETRY_TIMES 10000
+#define US2S(us) ((us) >> 20)
+#define WD_INIT_RETRY_TIMEOUT 3
#define DEF_DRV_LIB_FILE "libwd.so"
@@ -2322,10 +2324,10 @@ void wd_alg_drv_unbind(struct wd_alg_driver *drv)
wd_release_drv(drv);
}
-bool wd_alg_try_init(enum wd_status *status)
+int wd_alg_try_init(enum wd_status *status)
{
enum wd_status expected;
- int count = 0;
+ __u32 count = 0;
bool ret;
do {
@@ -2334,15 +2336,17 @@ bool wd_alg_try_init(enum wd_status *status)
__ATOMIC_RELAXED, __ATOMIC_RELAXED);
if (expected == WD_INIT) {
WD_ERR("The algorithm has been initialized!\n");
- return false;
+ return -WD_EEXIST;
}
usleep(WD_INIT_SLEEP_UTIME);
- if (!(++count % WD_INIT_RETRY_TIMES))
- WD_ERR("The algorithm initizalite has been waiting for %ds!\n",
- WD_INIT_SLEEP_UTIME * count / 1000000);
+
+ if (US2S(WD_INIT_SLEEP_UTIME * ++count) >= WD_INIT_RETRY_TIMEOUT) {
+ WD_ERR("The algorithm initialize wait timeout!\n");
+ return -WD_ETIMEDOUT;
+ }
} while (!ret);
- return true;
+ return 0;
}
static __u32 wd_get_ctx_numbers(struct wd_ctx_params ctx_params, int end)
--
2.25.1