From 3a1432cfdb7c696d3acf97025e6d74bbf3e520dc Mon Sep 17 00:00:00 2001
From: Wenpeng Liang <liangwenpeng@huawei.com>
Date: Thu, 7 Dec 2023 09:48:00 +0800
Subject: [PATCH 78/80] libhns: Add
 pthread_spin_destroy()/pthread_mutex_destroy()

driver inclusion
category: bugfix
bugzilla: https://gitee.com/src-openeuler/rdma-core/issues/I8MF59

--------------------------------------------------------------------------

The functions pthread_spin_destroy()/pthread_mutex_destroy()
corresponds to pthread_spin_init()/pthread_mutex_init(). The
driver should call pthread_spin_destroy()/pthread_mutex_destroy()
to clean up resources before exiting.

Signed-off-by: Wenpeng Liang <liangwenpeng@huawei.com>
---
 providers/hns/hns_roce_u.c       | 61 +++++++++++++++++++++++++++-----
 providers/hns/hns_roce_u_hw_v2.c |  1 +
 providers/hns/hns_roce_u_verbs.c | 17 ++++++---
 3 files changed, 67 insertions(+), 12 deletions(-)

diff --git a/providers/hns/hns_roce_u.c b/providers/hns/hns_roce_u.c
index f30486f..dfcd798 100644
--- a/providers/hns/hns_roce_u.c
+++ b/providers/hns/hns_roce_u.c
@@ -346,6 +346,47 @@ static int query_dev_attr(struct hns_roce_context *context,
 	return 0;
 }
 
+static int hns_roce_init_context_lock(struct hns_roce_context *context)
+{
+	int ret;
+
+	ret = pthread_spin_init(&context->uar_lock, PTHREAD_PROCESS_PRIVATE);
+	if (ret)
+		return ret;
+
+	ret = pthread_mutex_init(&context->qp_table_mutex, NULL);
+	if (ret)
+		goto destroy_uar_lock;
+
+	ret = pthread_mutex_init(&context->srq_table_mutex, NULL);
+	if (ret)
+		goto destroy_qp_mutex;
+
+	ret = pthread_mutex_init(&context->db_list_mutex, NULL);
+	if (ret)
+		goto destroy_srq_mutex;
+
+	return 0;
+
+destroy_srq_mutex:
+	pthread_mutex_destroy(&context->srq_table_mutex);
+
+destroy_qp_mutex:
+	pthread_mutex_destroy(&context->qp_table_mutex);
+
+destroy_uar_lock:
+	pthread_spin_destroy(&context->uar_lock);
+	return ret;
+}
+
+static void hns_roce_destroy_context_lock(struct hns_roce_context *context)
+{
+	pthread_spin_destroy(&context->uar_lock);
+	pthread_mutex_destroy(&context->qp_table_mutex);
+	pthread_mutex_destroy(&context->srq_table_mutex);
+	pthread_mutex_destroy(&context->db_list_mutex);
+}
+
 static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 						    int cmd_fd,
 						    void *private_data)
@@ -365,7 +406,10 @@ static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 	ucontext_set_cmd(&cmd, ctx_attr);
 	if (ibv_cmd_get_context(&context->ibv_ctx, &cmd.ibv_cmd, sizeof(cmd),
 				&resp.ibv_resp, sizeof(resp)))
-		goto err_free;
+		goto err_ibv_cmd;
+
+	if (hns_roce_init_context_lock(context))
+		goto err_ibv_cmd;
 
 	hr_dev->congest_type = resp.congest_type;
 
@@ -383,23 +427,23 @@ static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 	context->qp_table_shift = calc_table_shift(resp.qp_tab_size,
 						   HNS_ROCE_QP_TABLE_BITS);
 	context->qp_table_mask = (1 << context->qp_table_shift) - 1;
-	pthread_mutex_init(&context->qp_table_mutex, NULL);
+
 	for (i = 0; i < HNS_ROCE_QP_TABLE_SIZE; ++i)
 		context->qp_table[i].refcnt = 0;
 
 	context->srq_table_shift = calc_table_shift(resp.srq_tab_size,
 						    HNS_ROCE_SRQ_TABLE_BITS);
 	context->srq_table_mask = (1 << context->srq_table_shift) - 1;
-	pthread_mutex_init(&context->srq_table_mutex, NULL);
+
 	for (i = 0; i < HNS_ROCE_SRQ_TABLE_SIZE; ++i)
 		context->srq_table[i].refcnt = 0;
 
 	if (query_dev_attr(context, hr_dev, &resp))
-		goto err_free;
+		goto err_query_dev;
 
 	if (init_dca_context(context, cmd_fd,
 			     &resp, ctx_attr, hr_dev->page_size))
-		goto err_free;
+		goto err_query_dev;
 
 	if (init_reset_context(context, cmd_fd, &resp, hr_dev->page_size))
 		goto reset_free;
@@ -407,8 +451,6 @@ static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 	if (hns_roce_mmap(hr_dev, context, cmd_fd))
 		goto uar_free;
 
-	pthread_spin_init(&context->uar_lock, PTHREAD_PROCESS_PRIVATE);
-
 	verbs_set_ops(&context->ibv_ctx, &hns_common_ops);
 	verbs_set_ops(&context->ibv_ctx, &hr_dev->u_hw->hw_ops);
 
@@ -419,7 +461,9 @@ uar_free:
 		munmap(context->reset_state, hr_dev->page_size);
 reset_free:
 	uninit_dca_context(context);
-err_free:
+err_query_dev:
+	hns_roce_destroy_context_lock(context);
+err_ibv_cmd:
 	verbs_uninit_context(&context->ibv_ctx);
 	free(context);
 	return NULL;
@@ -434,6 +478,7 @@ static void hns_roce_free_context(struct ibv_context *ibctx)
 	if (context->reset_state)
 		munmap(context->reset_state, hr_dev->page_size);
 	uninit_dca_context(context);
+	hns_roce_destroy_context_lock(context);
 	verbs_uninit_context(&context->ibv_ctx);
 	free(context);
 }
diff --git a/providers/hns/hns_roce_u_hw_v2.c b/providers/hns/hns_roce_u_hw_v2.c
index 68d7110..b2a8858 100644
--- a/providers/hns/hns_roce_u_hw_v2.c
+++ b/providers/hns/hns_roce_u_hw_v2.c
@@ -2049,6 +2049,7 @@ static int hns_roce_u_v2_destroy_qp(struct ibv_qp *ibqp)
 	hns_roce_unlock_cqs(to_hr_cq(ibqp->send_cq), to_hr_cq(ibqp->recv_cq));
 
 	hns_roce_free_qp_buf(qp, ctx);
+	hns_roce_qp_spinlock_destroy(qp);
 
 	free(qp);
 
diff --git a/providers/hns/hns_roce_u_verbs.c b/providers/hns/hns_roce_u_verbs.c
index fae6126..ba3fef6 100644
--- a/providers/hns/hns_roce_u_verbs.c
+++ b/providers/hns/hns_roce_u_verbs.c
@@ -803,6 +803,7 @@ int hns_roce_u_modify_cq(struct ibv_cq *cq, struct ibv_modify_cq_attr *attr)
 
 int hns_roce_u_destroy_cq(struct ibv_cq *cq)
 {
+	struct hns_roce_cq *hr_cq = to_hr_cq(cq);
 	int ret;
 
 	ret = ibv_cmd_destroy_cq(cq);
@@ -811,10 +812,13 @@ int hns_roce_u_destroy_cq(struct ibv_cq *cq)
 
 	hns_roce_uninit_cq_swc(to_hr_cq(cq));
 
-	hns_roce_free_db(to_hr_ctx(cq->context), to_hr_cq(cq)->db,
+	hns_roce_free_db(to_hr_ctx(cq->context), hr_cq->db,
 				   HNS_ROCE_CQ_TYPE_DB);
-	hns_roce_free_buf(&to_hr_cq(cq)->buf);
-	free(to_hr_cq(cq));
+	hns_roce_free_buf(&hr_cq->buf);
+
+	hns_roce_spinlock_destroy(&hr_cq->hr_lock);
+
+	free(hr_cq);
 
 	return ret;
 }
@@ -1071,7 +1075,7 @@ static struct ibv_srq *create_srq(struct ibv_context *context,
 
 	set_srq_param(context, srq, init_attr);
 	if (alloc_srq_buf(srq))
-		goto err_free_srq;
+		goto err_destroy_lock;
 
 	srq->rdb = hns_roce_alloc_db(hr_ctx, HNS_ROCE_SRQ_TYPE_DB);
 	if (!srq->rdb)
@@ -1102,6 +1106,9 @@ err_srq_db:
 err_srq_buf:
 	free_srq_buf(srq);
 
+err_destroy_lock:
+	hns_roce_spinlock_destroy(&srq->hr_lock);
+
 err_free_srq:
 	free(srq);
 
@@ -1191,6 +1198,8 @@ int hns_roce_u_destroy_srq(struct ibv_srq *ibv_srq)
 
 	hns_roce_free_db(ctx, srq->rdb, HNS_ROCE_SRQ_TYPE_DB);
 	free_srq_buf(srq);
+
+	hns_roce_spinlock_destroy(&srq->hr_lock);
 	free(srq);
 
 	return 0;
-- 
2.25.1

