From 60cc5e8ead870777d36eed1242163f678de08226 Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Sat, 2 Apr 2022 09:47:10 +0800
Subject: [PATCH 112/183] uadk: some clean code for wd

1.remove unused header file
2.simplify code
3.add branch prediction to IO path

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 wd_cipher.c  |  1 -
 wd_mempool.c |  6 ++----
 wd_sched.c   | 39 +++++++++++++++++++++++++--------------
 3 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/wd_cipher.c b/wd_cipher.c
index 6d286f9..6cf1377 100644
--- a/wd_cipher.c
+++ b/wd_cipher.c
@@ -7,7 +7,6 @@
 #include <stdlib.h>
 #include <pthread.h>
 #include <sched.h>
-#include <numa.h>
 #include "wd_cipher.h"
 #include "wd_util.h"
 #include "include/drv/wd_cipher_drv.h"
diff --git a/wd_mempool.c b/wd_mempool.c
index 7701f63..03df19a 100644
--- a/wd_mempool.c
+++ b/wd_mempool.c
@@ -488,14 +488,12 @@ static int alloc_mem_one_need_multi(struct mempool *mp, struct blkpool *bp)
 	int mem_combined_num = bp->blk_size / mp->blk_size +
 				 (bp->blk_size % mp->blk_size ? 1 : 0);
 	int blk_num = bp->depth;
+	int ret = -WD_ENOMEM;
 	int pos = 0;
-	int ret;
 
 	wd_spinlock(&mp->lock);
-	if (check_mempool_real_size(mp, bp)) {
-		ret = -WD_ENOMEM;
+	if (check_mempool_real_size(mp, bp))
 		goto err_check_size;
-	}
 
 	while (blk_num > 0) {
 		ret = alloc_block_from_mempool(mp, bp, pos,
diff --git a/wd_sched.c b/wd_sched.c
index dfd390b..3a19780 100644
--- a/wd_sched.c
+++ b/wd_sched.c
@@ -180,7 +180,7 @@ static int session_poll_policy_rr(struct wd_sched_ctx *ctx, int numa_id,
 		end = region[SCHED_MODE_ASYNC][i].end;
 		ret = session_poll_region(ctx, begin, end, expect,
 					 count);
-		if (ret)
+		if (unlikely(ret))
 			return ret;
 	}
 
@@ -207,12 +207,12 @@ static int session_sched_poll_policy(handle_t sched_ctx,
 	__u16 i;
 	int ret;
 
-	if (!count || !ctx) {
+	if (unlikely(!count || !ctx)) {
 		WD_ERR("invalid: sched ctx is NULL or count is zero!\n");
 		return -WD_EINVAL;
 	}
 
-	if (ctx->numa_num > NUMA_NUM_NODES) {
+	if (unlikely(ctx->numa_num > NUMA_NUM_NODES)) {
 		WD_ERR("invalid: ctx's numa number is %u!\n", ctx->numa_num);
 		return -WD_EINVAL;
 	}
@@ -224,8 +224,7 @@ static int session_sched_poll_policy(handle_t sched_ctx,
 	 * package last time, it is more efficient. In most
 	 * bad situation, poll ends after MAX_POLL_TIMES loop.
 	 */
-	while (loop_time < MAX_POLL_TIMES) {
-		loop_time++;
+	while (++loop_time < MAX_POLL_TIMES) {
 		for (i = 0; i < ctx->numa_num;) {
 			/* If current numa is not valid, find next. */
 			if (!sched_info[i].valid) {
@@ -235,7 +234,7 @@ static int session_sched_poll_policy(handle_t sched_ctx,
 
 			last_count = *count;
 			ret = session_poll_policy_rr(ctx, i, expect, count);
-			if (ret)
+			if (unlikely(ret))
 				return ret;
 
 			if (expect == *count)
@@ -438,22 +437,34 @@ out:
 	return;
 }
 
+static int numa_num_check(__u16 numa_num)
+{
+	int max_node;
+
+	max_node = numa_max_node() + 1;
+	if (max_node <= 0) {
+		WD_ERR("invalid: numa max node is %d!\n", max_node);
+		return -WD_EINVAL;
+	}
+
+	if (!numa_num || numa_num > max_node) {
+		WD_ERR("invalid: numa number is %u!\n", numa_num);
+		return -WD_EINVAL;
+	}
+
+	return 0;
+}
+
 struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num,
 				   __u16 numa_num, user_poll_func func)
 {
 	struct wd_sched_info *sched_info;
 	struct wd_sched_ctx *sched_ctx;
 	struct wd_sched *sched;
-	int i, j, max_node;
-
-	max_node = numa_max_node() + 1;
-	if (max_node <= 0)
-		return NULL;
+	int i, j;
 
-	if (!numa_num || numa_num > max_node) {
-		WD_ERR("invalid: numa number is %u!\n", numa_num);
+	if (numa_num_check(numa_num))
 		return NULL;
-	}
 
 	if (sched_type >= SCHED_POLICY_BUTT || !type_num) {
 		WD_ERR("invalid: sched_type is %u or type_num is %u!\n",
-- 
2.27.0

