From d827ac994867b2dd06d5723d62c6c618f0c5c77b Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Thu, 30 Dec 2021 20:34:51 +0800
Subject: [PATCH 25/28] uadk: env: fix wd_add_task_to_async_queue return value

wd_add_task_to_async_queue may fail with error code,
add return value check after call it.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 wd_aead.c   | 10 ++++++++--
 wd_cipher.c | 10 ++++++++--
 wd_comp.c   |  4 +++-
 wd_dh.c     |  6 ++++--
 wd_digest.c | 11 ++++++++---
 wd_ecc.c    |  6 ++++--
 wd_rsa.c    |  6 ++++--
 wd_util.c   | 20 +++++++++++++-------
 8 files changed, 52 insertions(+), 21 deletions(-)

diff --git a/wd_aead.c b/wd_aead.c
index f93f791..ebad440 100644
--- a/wd_aead.c
+++ b/wd_aead.c
@@ -596,11 +596,17 @@ int wd_do_aead_async(handle_t h_sess, struct wd_aead_req *req)
 		if (ret != -WD_EBUSY)
 			WD_ERR("failed to send BD, hw is err!\n");
 
-		wd_put_msg_to_pool(&wd_aead_setting.pool, idx, msg->tag);
+		goto fail_with_msg;
 	}
 
-	wd_add_task_to_async_queue(&wd_aead_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_aead_env_config, idx);
+	if (ret)
+		goto fail_with_msg;
+
+	return 0;
 
+fail_with_msg:
+	wd_put_msg_to_pool(&wd_aead_setting.pool, idx, msg->tag);
 	return ret;
 }
 
diff --git a/wd_cipher.c b/wd_cipher.c
index 9977765..9c1f98c 100644
--- a/wd_cipher.c
+++ b/wd_cipher.c
@@ -515,11 +515,17 @@ int wd_do_cipher_async(handle_t h_sess, struct wd_cipher_req *req)
 		if (ret != -WD_EBUSY)
 			WD_ERR("wd cipher async send err!\n");
 
-		wd_put_msg_to_pool(&wd_cipher_setting.pool, idx, msg->tag);
+		goto fail_with_msg;
 	}
 
-	wd_add_task_to_async_queue(&wd_cipher_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_cipher_env_config, idx);
+	if (ret)
+		goto fail_with_msg;
+
+	return 0;
 
+fail_with_msg:
+	wd_put_msg_to_pool(&wd_cipher_setting.pool, idx, msg->tag);
 	return ret;
 }
 
diff --git a/wd_comp.c b/wd_comp.c
index e34d590..fc355a9 100644
--- a/wd_comp.c
+++ b/wd_comp.c
@@ -724,7 +724,9 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req)
 
 	pthread_spin_unlock(&ctx->lock);
 
-	wd_add_task_to_async_queue(&wd_comp_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_comp_env_config, idx);
+	if (ret)
+		wd_put_msg_to_pool(&wd_comp_setting.pool, idx, msg->tag);
 
 	return ret;
 }
diff --git a/wd_dh.c b/wd_dh.c
index 177ffad..7b849ac 100644
--- a/wd_dh.c
+++ b/wd_dh.c
@@ -341,9 +341,11 @@ int wd_do_dh_async(handle_t sess, struct wd_dh_req *req)
 	}
 	pthread_spin_unlock(&ctx->lock);
 
-	wd_add_task_to_async_queue(&wd_dh_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_dh_env_config, idx);
+	if (ret)
+		goto fail_with_msg;
 
-	return ret;
+	return 0;
 
 fail_with_msg:
 	wd_put_msg_to_pool(&wd_dh_setting.pool, idx, mid);
diff --git a/wd_digest.c b/wd_digest.c
index bbb258f..2b3661d 100644
--- a/wd_digest.c
+++ b/wd_digest.c
@@ -420,13 +420,18 @@ int wd_do_digest_async(handle_t h_sess, struct wd_digest_req *req)
 		if (ret != -WD_EBUSY)
 			WD_ERR("failed to send BD, hw is err!\n");
 
-		wd_put_msg_to_pool(&wd_digest_setting.pool, idx, msg->tag);
-		return ret;
+		goto fail_with_msg;
 	}
 
-	wd_add_task_to_async_queue(&wd_digest_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_digest_env_config, idx);
+	if (ret)
+		goto fail_with_msg;
 
 	return 0;
+
+fail_with_msg:
+	wd_put_msg_to_pool(&wd_digest_setting.pool, idx, msg->tag);
+	return ret;
 }
 
 int wd_digest_poll_ctx(__u32 idx, __u32 expt, __u32 *count)
diff --git a/wd_ecc.c b/wd_ecc.c
index 8b625e4..e37fb39 100644
--- a/wd_ecc.c
+++ b/wd_ecc.c
@@ -2187,9 +2187,11 @@ int wd_do_ecc_async(handle_t sess, struct wd_ecc_req *req)
 	}
 	pthread_spin_unlock(&ctx->lock);
 
-	wd_add_task_to_async_queue(&wd_ecc_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_ecc_env_config, idx);
+	if (ret)
+		goto fail_with_msg;
 
-	return ret;
+	return 0;
 
 fail_with_msg:
 	wd_put_msg_to_pool(&wd_ecc_setting.pool, idx, mid);
diff --git a/wd_rsa.c b/wd_rsa.c
index 2831111..b6cc0d1 100644
--- a/wd_rsa.c
+++ b/wd_rsa.c
@@ -397,9 +397,11 @@ int wd_do_rsa_async(handle_t sess, struct wd_rsa_req *req)
 	if (ret)
 		goto fail_with_msg;
 
-	wd_add_task_to_async_queue(&wd_rsa_env_config, idx);
+	ret = wd_add_task_to_async_queue(&wd_rsa_env_config, idx);
+	if (ret)
+		goto fail_with_msg;
 
-	return ret;
+	return 0;
 
 fail_with_msg:
 	wd_put_msg_to_pool(&wd_rsa_setting.pool, idx, mid);
diff --git a/wd_util.c b/wd_util.c
index 49e1d66..e04747b 100644
--- a/wd_util.c
+++ b/wd_util.c
@@ -1108,17 +1108,20 @@ int wd_add_task_to_async_queue(struct wd_env_config *config, __u32 idx)
 {
 	struct async_task_queue *task_queue;
 	struct async_task *head, *task;
-	int prod;
+	int prod, ret;
 
 	if (!config->enable_internal_poll)
 		return 0;
 
 	task_queue = find_async_queue(config, idx);
 	if (!task_queue)
-		return 0;
+		return -WD_EINVAL;
 
-	if (sem_wait(&task_queue->empty_sem))
-		return 0;
+	ret = sem_wait(&task_queue->empty_sem);
+	if (ret) {
+		WD_ERR("failed to wait empty_sem!\n");
+		return ret;
+	}
 
 	pthread_mutex_lock(&task_queue->lock);
 
@@ -1135,10 +1138,13 @@ int wd_add_task_to_async_queue(struct wd_env_config *config, __u32 idx)
 
 	pthread_mutex_unlock(&task_queue->lock);
 
-	if (sem_post(&task_queue->full_sem))
-		return 0;
+	ret = sem_post(&task_queue->full_sem);
+	if (ret) {
+		WD_ERR("failed to post full_sem!\n");
+		return ret;
+	}
 
-	return 1;
+	return 0;
 }
 
 static void *async_poll_process_func(void *args)
-- 
2.31.1

