From: Longfang Liu <liulongfang@huawei.com> Strengthen the initialization logic in wd_alg_init2_ by adding WD_INIT2_MAX_RETRY (32) retry limit across all 10 algorithm modules (aead, agg, cipher, comp, dh, digest, ecc, join_gather, rsa, udma) to prevent infinite retry loops when no device is available. Signed-off-by: Longfang Liu <liulongfang@huawei.com> --- include/wd_alg_common.h | 1 + wd_aead.c | 5 +++++ wd_cipher.c | 8 +++++++- wd_dh.c | 6 ++++++ wd_ecc.c | 6 ++++++ wd_join_gather.c | 6 ++++++ wd_rsa.c | 6 ++++++ 7 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index be4e3b5..05cc5c0 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,7 @@ extern "C" { #define MAX_STR_LEN 256 #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000 +#define WD_INIT2_MAX_RETRY 32 /* Key size of chiper */ #define MAX_CIPHER_KEY_SIZE 64 diff --git a/wd_aead.c b/wd_aead.c index c0983a6..ba3c116 100644 --- a/wd_aead.c +++ b/wd_aead.c @@ -816,6 +816,11 @@ int wd_aead_init2_(char *alg, __u32 sched_type, int task_type, goto out_uninit; while (ret != 0) { + if (try_cnt++ >= WD_INIT2_MAX_RETRY) { + WD_ERR("failed to init2 after %d retries.\n", + WD_INIT2_MAX_RETRY); + goto out_dlclose; + } memset(&wd_aead_setting.config, 0, sizeof(struct wd_ctx_config_internal)); /* Init ctx param and prepare for ctx request */ aead_ctx_params.ctx_set_num = aead_ctx_num; diff --git a/wd_cipher.c b/wd_cipher.c index e0670f5..5c605ca 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -502,6 +502,7 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_nums cipher_ctx_num[WD_CIPHER_DECRYPTION + 1] = {0}; struct wd_ctx_params cipher_ctx_params = {0}; int state, ret = -WD_EINVAL; + int try_cnt = 0; bool flag; if (!wd_cipher_atfork_registered) { @@ -530,6 +531,11 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, goto out_uninit; while (ret != 0) { + if (try_cnt++ >= WD_INIT2_MAX_RETRY) { + WD_ERR("failed to init2 after %d retries.\n", + WD_INIT2_MAX_RETRY); + goto out_dlclose; + } memset(&wd_cipher_setting.config, 0, sizeof(struct wd_ctx_config_internal)); @@ -557,7 +563,7 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, wd_ctx_param_uninit(&cipher_ctx_params); continue; } - WD_ERR("fail to init alg attrs.\n"); + WD_ERR("failed to init alg attrs.\n"); goto out_params_uninit; } } diff --git a/wd_dh.c b/wd_dh.c index 612e15f..8026fd6 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -240,6 +240,7 @@ 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 state, ret = -WD_EINVAL; + int try_cnt = 0; if (!wd_dh_atfork_registered) { if (pthread_atfork(NULL, NULL, wd_dh_clear_status) == 0) @@ -266,6 +267,11 @@ int wd_dh_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_param goto out_clear_init; while (ret) { + if (try_cnt++ >= WD_INIT2_MAX_RETRY) { + WD_ERR("failed to init2 after %d retries.\n", + WD_INIT2_MAX_RETRY); + goto out_driver; + } memset(&wd_dh_setting.config, 0, sizeof(struct wd_ctx_config_internal)); /* Init ctx param and prepare for ctx request */ dh_ctx_params.ctx_set_num = dh_ctx_num; diff --git a/wd_ecc.c b/wd_ecc.c index 2d1aa77..4b141c4 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -311,6 +311,7 @@ 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 state, ret = -WD_EINVAL; + int try_cnt = 0; bool flag; if (!wd_ecc_atfork_registered) { @@ -339,6 +340,11 @@ int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para goto out_clear_init; while (ret) { + if (try_cnt++ >= WD_INIT2_MAX_RETRY) { + WD_ERR("failed to init2 after %d retries.\n", + WD_INIT2_MAX_RETRY); + goto out_driver; + } memset(&wd_ecc_setting.config, 0, sizeof(struct wd_ctx_config_internal)); /* Init ctx param and prepare for ctx request */ diff --git a/wd_join_gather.c b/wd_join_gather.c index 8857c8f..e43dbf9 100644 --- a/wd_join_gather.c +++ b/wd_join_gather.c @@ -803,6 +803,7 @@ int wd_join_gather_init(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params join_gather_ctx_params = {0}; struct wd_ctx_nums join_gather_ctx_num = {0}; int ret = -WD_EINVAL; + int try_cnt = 0; int state; bool flag; @@ -832,6 +833,11 @@ int wd_join_gather_init(char *alg, __u32 sched_type, int task_type, goto out_uninit; while (ret != 0) { + if (try_cnt++ >= WD_INIT2_MAX_RETRY) { + WD_ERR("failed to init2 after %d retries.\n", + WD_INIT2_MAX_RETRY); + goto out_driver; + } memset(&wd_join_gather_setting.config, 0, sizeof(struct wd_ctx_config_internal)); join_gather_ctx_params.ctx_set_num = &join_gather_ctx_num; ret = wd_ctx_param_init(&join_gather_ctx_params, ctx_params, alg, diff --git a/wd_rsa.c b/wd_rsa.c index 754c28a..c1f2d9a 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -282,6 +282,7 @@ 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 state, ret = -WD_EINVAL; + int try_cnt = 0; if (!wd_rsa_atfork_registered) { if (pthread_atfork(NULL, NULL, wd_rsa_clear_status) == 0) @@ -308,6 +309,11 @@ int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_para goto out_clear_init; while (ret) { + if (try_cnt++ >= WD_INIT2_MAX_RETRY) { + WD_ERR("failed to init2 after %d retries.\n", + WD_INIT2_MAX_RETRY); + goto out_dlclose; + } memset(&wd_rsa_setting.config, 0, sizeof(struct wd_ctx_config_internal)); /* Init ctx param and prepare for ctx request */ -- 2.43.0