From: Shangbin Liu liushangbin@hisilicon.com
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 144ba59..a2d3ec3 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -384,10 +384,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 7c3f160..33a319c 100644 --- a/wd_aead.c +++ b/wd_aead.c @@ -453,14 +453,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) @@ -525,14 +524,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 adb6496..52d3b79 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -354,14 +354,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) @@ -402,14 +401,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 93c3031..f532d9e 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -175,14 +175,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) @@ -223,14 +222,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; - 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 50a1532..b33aca3 100644 --- a/wd_dh.c +++ b/wd_dh.c @@ -141,14 +141,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) @@ -189,14 +188,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 c5dbeca..c58f8ff 100644 --- a/wd_digest.c +++ b/wd_digest.c @@ -267,14 +267,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) @@ -335,14 +334,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 9f83fea..16bbfc5 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -207,14 +207,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) @@ -255,14 +254,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 19a590f..8520eb0 100644 --- a/wd_rsa.c +++ b/wd_rsa.c @@ -182,14 +182,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) @@ -230,14 +229,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 ef67f1f..cab25e8 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"
@@ -2298,10 +2300,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 { @@ -2310,15 +2312,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)