From 26661c4ae49d1c4c6914d8aba4e8d32db609d24b Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Thu, 10 Mar 2022 20:03:16 +0800
Subject: [PATCH 092/109] uadk: v1: fix del ctx

It is better to check whether the number of CTX
is greater than 0 before del ctx.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 v1/wd_aead.c   | 11 +++++------
 v1/wd_cipher.c | 12 ++++++------
 v1/wd_comp.c   | 13 +++++++------
 v1/wd_digest.c | 11 +++++------
 4 files changed, 23 insertions(+), 24 deletions(-)

diff --git a/v1/wd_aead.c b/v1/wd_aead.c
index ec537b3..cf358bf 100644
--- a/v1/wd_aead.c
+++ b/v1/wd_aead.c
@@ -685,16 +685,15 @@ void wcrypto_del_aead_ctx(void *ctx)
 	qinfo = ctxt->q->qinfo;
 	wd_uninit_cookie_pool(&ctxt->pool);
 	wd_spinlock(&qinfo->qlock);
-	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, ctxt->ctx_id - 1,
-		WD_MAX_CTX_NUM);
-	qinfo->ctx_num--;
-	if (!qinfo->ctx_num)
-		memset(&qinfo->br, 0, sizeof(qinfo->br));
-	if (qinfo->ctx_num < 0) {
+	if (qinfo->ctx_num <= 0) {
 		wd_unspinlock(&qinfo->qlock);
 		WD_ERR("fail to delete aead ctx!\n");
 		return;
 	}
+	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, ctxt->ctx_id - 1,
+		WD_MAX_CTX_NUM);
+	if (!(--qinfo->ctx_num))
+		memset(&qinfo->br, 0, sizeof(qinfo->br));
 	wd_unspinlock(&qinfo->qlock);
 	del_ctx_key(ctxt);
 	free(ctx);
diff --git a/v1/wd_cipher.c b/v1/wd_cipher.c
index 921c464..8eb6a6f 100644
--- a/v1/wd_cipher.c
+++ b/v1/wd_cipher.c
@@ -553,16 +553,16 @@ void wcrypto_del_cipher_ctx(void *ctx)
 	qinfo = c_ctx->q->qinfo;
 	wd_uninit_cookie_pool(&c_ctx->pool);
 	wd_spinlock(&qinfo->qlock);
-	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, c_ctx->ctx_id - 1,
-		WD_MAX_CTX_NUM);
-	qinfo->ctx_num--;
-	if (!qinfo->ctx_num)
-		memset(&qinfo->br, 0, sizeof(qinfo->br));
-	if (qinfo->ctx_num < 0) {
+	if (qinfo->ctx_num <= 0) {
 		wd_unspinlock(&qinfo->qlock);
 		WD_ERR("error:repeat del cipher ctx!\n");
 		return;
 	}
+	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, c_ctx->ctx_id - 1,
+		WD_MAX_CTX_NUM);
+	if (!(--qinfo->ctx_num))
+		memset(&qinfo->br, 0, sizeof(qinfo->br));
+
 	wd_unspinlock(&qinfo->qlock);
 	del_ctx_key(c_ctx);
 	free(ctx);
diff --git a/v1/wd_comp.c b/v1/wd_comp.c
index 32d89c8..4ce79bd 100644
--- a/v1/wd_comp.c
+++ b/v1/wd_comp.c
@@ -339,16 +339,17 @@ void wcrypto_del_comp_ctx(void *ctx)
 
 	wd_uninit_cookie_pool(&cctx->pool);
 	wd_spinlock(&qinfo->qlock);
-	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, cctx->ctx_id -1,
-		WD_MAX_CTX_NUM);
-	qinfo->ctx_num--;
-	if (!qinfo->ctx_num) {
-		memset(&qinfo->br, 0, sizeof(qinfo->br));
-	} else if (qinfo->ctx_num < 0) {
+	if (qinfo->ctx_num <= 0) {
 		wd_unspinlock(&qinfo->qlock);
 		WD_ERR("error: repeat delete compress ctx!\n");
 		return;
 	}
+
+	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, cctx->ctx_id -1,
+		WD_MAX_CTX_NUM);
+	if (!(--qinfo->ctx_num))
+		memset(&qinfo->br, 0, sizeof(qinfo->br));
+
 	wd_unspinlock(&qinfo->qlock);
 
 	free(cctx);
diff --git a/v1/wd_digest.c b/v1/wd_digest.c
index c84e71d..5acb660 100644
--- a/v1/wd_digest.c
+++ b/v1/wd_digest.c
@@ -472,16 +472,15 @@ void wcrypto_del_digest_ctx(void *ctx)
 	qinfo = d_ctx->q->qinfo;
 	wd_uninit_cookie_pool(&d_ctx->pool);
 	wd_spinlock(&qinfo->qlock);
-	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, d_ctx->ctx_id - 1,
-		WD_MAX_CTX_NUM);
-	qinfo->ctx_num--;
-	if (!qinfo->ctx_num)
-		memset(&qinfo->br, 0, sizeof(qinfo->br));
-	if (qinfo->ctx_num < 0) {
+	if (qinfo->ctx_num <= 0) {
 		wd_unspinlock(&qinfo->qlock);
 		WD_ERR("error: repeat del digest ctx!\n");
 		return;
 	}
+	wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, d_ctx->ctx_id - 1,
+		WD_MAX_CTX_NUM);
+	if (!(--qinfo->ctx_num))
+		memset(&qinfo->br, 0, sizeof(qinfo->br));
 	wd_unspinlock(&qinfo->qlock);
 	del_ctx_key(d_ctx);
 	free(ctx);
-- 
2.27.0

