From 913a84cbdce651aea50a6e12117ee93ccbbdbf1c Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Thu, 30 Dec 2021 12:07:33 +0800
Subject: [PATCH 21/28] uadk: fix pthread_spin_init

pthread_spin_init() may fail with errors,
check return value for it.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 drv/hisi_qm_udrv.c | 16 ++++++++++++----
 wd_util.c          |  9 +++++++--
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/drv/hisi_qm_udrv.c b/drv/hisi_qm_udrv.c
index 8282c51..845fa46 100644
--- a/drv/hisi_qm_udrv.c
+++ b/drv/hisi_qm_udrv.c
@@ -298,7 +298,7 @@ static int hisi_qm_setup_info(struct hisi_qp *qp, struct hisi_qm_priv *config)
 	ret = hisi_qm_get_qfrs_offs(qp->h_ctx, q_info);
 	if (ret) {
 		WD_ERR("get dev qfrs offset fail.\n");
-		return ret;
+		goto err_out;
 	}
 
 	ret = hisi_qm_setup_db(qp->h_ctx, q_info);
@@ -323,7 +323,11 @@ static int hisi_qm_setup_info(struct hisi_qp *qp, struct hisi_qm_priv *config)
 		q_info->region_size[UACCE_QFRT_DUS] - sizeof(uint32_t);
 	q_info->ds_rx_base = q_info->ds_tx_base - sizeof(uint32_t);
 
-	pthread_spin_init(&q_info->lock, PTHREAD_PROCESS_SHARED);
+	ret = pthread_spin_init(&q_info->lock, PTHREAD_PROCESS_SHARED);
+	if (ret) {
+		WD_ERR("init qinfo lock fail\n");
+		goto err_out;
+	}
 
 	return 0;
 
@@ -560,7 +564,7 @@ static struct hisi_sgl *hisi_qm_align_sgl(const void *sgl, __u32 sge_num)
 handle_t hisi_qm_create_sglpool(__u32 sgl_num, __u32 sge_num)
 {
 	struct hisi_sgl_pool *sgl_pool;
-	int i;
+	int i, ret;
 
 	if (!sgl_num || !sge_num || sge_num > HISI_SGE_NUM_IN_SGL) {
 		WD_ERR("create sgl_pool failed, sgl_num=%u, sge_num=%u\n",
@@ -601,7 +605,11 @@ handle_t hisi_qm_create_sglpool(__u32 sgl_num, __u32 sge_num)
 	sgl_pool->sge_num = sge_num;
 	sgl_pool->depth = sgl_num;
 	sgl_pool->top = sgl_num;
-	pthread_spin_init(&sgl_pool->lock, PTHREAD_PROCESS_SHARED);
+	ret = pthread_spin_init(&sgl_pool->lock, PTHREAD_PROCESS_SHARED);
+	if (ret) {
+		WD_ERR("init sgl pool lock failed.\n");
+		goto err_out;
+	}
 
 	return (handle_t)sgl_pool;
 
diff --git a/wd_util.c b/wd_util.c
index 62f9359..83c77c4 100644
--- a/wd_util.c
+++ b/wd_util.c
@@ -68,7 +68,7 @@ int wd_init_ctx_config(struct wd_ctx_config_internal *in,
 		       struct wd_ctx_config *cfg)
 {
 	struct wd_ctx_internal *ctxs;
-	int i;
+	int i, ret;
 
 	if (!cfg->ctx_num) {
 		WD_ERR("invalid parameters, ctx_num is 0!\n");
@@ -93,7 +93,12 @@ int wd_init_ctx_config(struct wd_ctx_config_internal *in,
 		}
 
 		clone_ctx_to_internal(cfg->ctxs + i, ctxs + i);
-		pthread_spin_init(&ctxs[i].lock, PTHREAD_PROCESS_SHARED);
+		ret = pthread_spin_init(&ctxs[i].lock, PTHREAD_PROCESS_SHARED);
+		if (ret) {
+			WD_ERR("init ctxs lock failed!\n");
+			free(ctxs);
+			return ret;
+		}
 	}
 
 	in->ctxs = ctxs;
-- 
2.31.1

