From ca3131392a8cb3209599dc6ca4c377460346b36a Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Mon, 27 Dec 2021 09:52:01 +0800
Subject: [PATCH 14/28] uadk: max numa number should not be fixed

numa_num_configured_nodes() returns the number
of memory nodes in the system, so use it as max
numa number is good.
when function is called in io path, use static array
as much as possible, so use a numa number macro.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 include/wd_sched.h                 |   5 +-
 include/wd_util.h                  |   6 +-
 test/hisi_sec_test/test_hisi_sec.c |   5 +-
 uadk_tool/sec_uadk_benchmark.c     |  13 ++--
 wd_sched.c                         |  39 +++++++----
 wd_util.c                          | 106 ++++++++++++++++++-----------
 6 files changed, 109 insertions(+), 65 deletions(-)

diff --git a/include/wd_sched.h b/include/wd_sched.h
index a008d57..78125f4 100644
--- a/include/wd_sched.h
+++ b/include/wd_sched.h
@@ -8,7 +8,6 @@
 #define SCHED_SAMPLE_h
 #include "wd_alg_common.h"
 
-#define MAX_NUMA_NUM	4
 #define INVALID_POS	0xFFFFFFFF
 
 /* The global policy type */
@@ -48,8 +47,8 @@ int wd_sched_rr_instance(const struct wd_sched *sched,
  * @func: The ctx poll function of user underlying operating.
  *
  */
-struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num, __u8 numa_num,
-				    user_poll_func func);
+struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num,
+				   __u16 numa_num, user_poll_func func);
 
 /**
  * wd_sched_rr_release - Release schedule memory.
diff --git a/include/wd_util.h b/include/wd_util.h
index 643309c..81f4ba8 100644
--- a/include/wd_util.h
+++ b/include/wd_util.h
@@ -59,12 +59,9 @@ struct wd_env_config_per_numa {
 };
 
 struct wd_env_config {
-	unsigned long numa_num;
 	struct wd_env_config_per_numa *config_per_numa;
 	/* Let's make it as a gobal config, not per numa */
 	bool enable_internal_poll;
-	__u8 disable_env;
-	__u8 op_type_num;
 	int (*alg_poll_ctx)(__u32, __u32, __u32 *);
 	void (*alg_uninit)(void);
 
@@ -74,6 +71,9 @@ struct wd_env_config {
 	struct wd_ctx_config *ctx_config;
 	const struct wd_config_variable *table;
 	__u32 table_size;
+	__u16 numa_num;
+	__u8 disable_env;
+	__u8 op_type_num;
 };
 
 struct wd_config_variable {
diff --git a/test/hisi_sec_test/test_hisi_sec.c b/test/hisi_sec_test/test_hisi_sec.c
index e1521f6..dda291d 100644
--- a/test/hisi_sec_test/test_hisi_sec.c
+++ b/test/hisi_sec_test/test_hisi_sec.c
@@ -10,6 +10,7 @@
 #include <sys/mman.h>
 #include <sys/time.h>
 #include <getopt.h>
+#include <numa.h>
 
 #include "test_hisi_sec.h"
 #include "wd_cipher.h"
@@ -493,7 +494,9 @@ static int init_ctx_config(int type, int mode)
 	if (list->dev->numa_id < 0)
 		list->dev->numa_id = 0;
 
-	g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, MAX_NUMA_NUM, wd_cipher_poll_ctx);
+	g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1,
+				    numa_max_node() + 1,
+				    wd_cipher_poll_ctx);
 	if (!g_sched) {
 		printf("Fail to alloc sched!\n");
 		goto out;
diff --git a/uadk_tool/sec_uadk_benchmark.c b/uadk_tool/sec_uadk_benchmark.c
index 21738ed..40ba227 100644
--- a/uadk_tool/sec_uadk_benchmark.c
+++ b/uadk_tool/sec_uadk_benchmark.c
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: Apache-2.0 */
 
+#include <numa.h>
 #include "uadk_benchmark.h"
 
 #include "sec_uadk_benchmark.h"
@@ -347,8 +348,12 @@ static int sec_uadk_param_parse(thread_data *tddata, struct acc_option *options)
 static int init_ctx_config(char *alg, int subtype, int mode)
 {
 	struct uacce_dev_list *list;
+	int i, max_node;
 	int ret = 0;
-	int i;
+
+	max_node = numa_max_node() + 1;
+	if (max_node <= 0)
+		return -EINVAL;
 
 	list = wd_get_accel_list(alg);
 	if (!list) {
@@ -369,13 +374,13 @@ static int init_ctx_config(char *alg, int subtype, int mode)
 
 	switch(subtype) {
 	case CIPHER_TYPE:
-		g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, MAX_NUMA_NUM, wd_cipher_poll_ctx);
+		g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, max_node, wd_cipher_poll_ctx);
 		break;
 	case AEAD_TYPE:
-		g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, MAX_NUMA_NUM, wd_aead_poll_ctx);
+		g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, max_node, wd_aead_poll_ctx);
 		break;
 	case DIGEST_TYPE:
-		g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, MAX_NUMA_NUM, wd_digest_poll_ctx);
+		g_sched = wd_sched_rr_alloc(SCHED_POLICY_RR, 1, max_node, wd_digest_poll_ctx);
 		break;
 	default:
 		SEC_TST_PRT("Fail to parse alg subtype!\n");
diff --git a/wd_sched.c b/wd_sched.c
index 8ca309c..b310077 100644
--- a/wd_sched.c
+++ b/wd_sched.c
@@ -6,6 +6,7 @@
 
 #include <stdlib.h>
 #include <stdbool.h>
+#include <numa.h>
 #include "wd_sched.h"
 
 #define MAX_POLL_TIMES 1000
@@ -37,6 +38,8 @@ struct sched_key {
  * @begin: the start pos in ctxs of config.
  * @end: the end pos in ctxx of config.
  * @last: the last one which be distributed.
+ * @valid: the region used flag.
+ * @lock: lock the currentscheduling region.
  */
 struct sched_ctx_region {
 	__u32 begin;
@@ -64,14 +67,13 @@ struct wd_sched_info {
  * @policy: define the policy of the scheduler.
  * @numa_num: the max numa numbers of the scheduler.
  * @type_num: the max operation types of the scheduler.
- * @numa_id: current task's numa id
  * @poll_func: the task's poll operation function.
  * @sched_info: the context of the scheduler
  */
 struct wd_sched_ctx {
 	__u32 policy;
 	__u32 type_num;
-	__u8  numa_num;
+	__u16  numa_num;
 	user_poll_func poll_func;
 	struct wd_sched_info sched_info[0];
 };
@@ -200,11 +202,11 @@ static int session_sched_poll_policy(handle_t sched_ctx,
 {
 	struct wd_sched_ctx *ctx = (struct wd_sched_ctx *)sched_ctx;
 	struct wd_sched_info *sched_info;
-	int numa[MAX_NUMA_NUM];
+	__u16 numa[NUMA_NUM_NODES];
 	__u32 loop_time = 0;
 	__u32 last_count = 0;
-	__u8 tail = 0;
-	__u8 i;
+	__u16 tail = 0;
+	__u16 i;
 	int ret;
 
 	if (!sched_ctx || !count || !ctx) {
@@ -212,6 +214,11 @@ static int session_sched_poll_policy(handle_t sched_ctx,
 		return -EINVAL;
 	}
 
+	if (ctx->numa_num > NUMA_NUM_NODES) {
+		WD_ERR("ERROR: %s ctx numa num is invalid!\n", __FUNCTION__);
+		return -EINVAL;
+	}
+
 	sched_info = ctx->sched_info;
 	for (i = 0; i < ctx->numa_num; i++)
 		if (sched_info[i].valid)
@@ -412,13 +419,23 @@ out:
 	return;
 }
 
-struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num, __u8 numa_num,
-				    user_poll_func func)
+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;
+	int i, j, max_node;
+
+	max_node = numa_max_node() + 1;
+	if (max_node <= 0)
+		return NULL;
+
+	if (!numa_num || numa_num > max_node) {
+		WD_ERR("Error: %s numa number = %u!\n", __FUNCTION__,
+		       numa_num);
+		return NULL;
+	}
 
 	if (sched_type >= SCHED_POLICY_BUTT || !type_num) {
 		WD_ERR("Error: %s sched_type = %u or type_num = %u is invalid!\n",
@@ -426,12 +443,6 @@ struct wd_sched *wd_sched_rr_alloc(__u8 sched_type, __u8 type_num, __u8 numa_num
 		return NULL;
 	}
 
-	if (!numa_num) {
-		WD_ERR("Warning: %s set numa number as %d!\n", __FUNCTION__,
-		       MAX_NUMA_NUM);
-		numa_num = MAX_NUMA_NUM;
-	}
-
 	sched = calloc(1, sizeof(struct wd_sched));
 	if (!sched) {
 		WD_ERR("Error: %s wd_sched alloc error!\n", __FUNCTION__);
diff --git a/wd_util.c b/wd_util.c
index 3170f3c..3a1def5 100644
--- a/wd_util.c
+++ b/wd_util.c
@@ -357,26 +357,28 @@ static void wd_free_numa(struct wd_env_config *config)
  * @numa_dev_num: number of devices of the same type (like sec2) on each numa.
  * @numa_num: number of numa node that has this type of device.
  */
-static void wd_get_dev_numa(struct uacce_dev_list *head,
-			    int *numa_dev_num, __u8 *numa_num, __u8 size)
+static __u16 wd_get_dev_numa(struct uacce_dev_list *head,
+			     int *numa_dev_num, __u16 size)
 {
 	struct uacce_dev_list *list = head;
+	__u16 numa_num = 0;
 
 	while (list) {
 		if (list->dev->numa_id < 0) {
 			list->dev->numa_id = 0;
 		} else if (list->dev->numa_id >= size) {
 			WD_ERR("numa id is wrong(%d)\n", list->dev->numa_id);
-			*numa_num = 0;
-			return;
+			return 0;
 		}
 
 		if (!numa_dev_num[list->dev->numa_id])
-			(*numa_num)++;
+			numa_num++;
 
 		numa_dev_num[list->dev->numa_id]++;
 		list = list->next;
 	}
+
+	return numa_num;
 }
 
 static void wd_set_numa_dev(struct uacce_dev_list *head,
@@ -400,39 +402,18 @@ static void wd_set_numa_dev(struct uacce_dev_list *head,
 	}
 }
 
-static int wd_alloc_numa(struct wd_env_config *config,
-			 const struct wd_alg_ops *ops)
+static int wd_set_config_numa(struct wd_env_config *config,
+			      int *numa_dev_num, int max_node)
 {
 	struct wd_env_config_per_numa *config_numa;
-	struct uacce_dev_list *head;
-	int numa_dev_num[MAX_NUMA_NUM] = {0};
-	__u8 numa_num = 0;
-	int i, ret;
-
-	/* get uacce_dev */
-	head = wd_get_accel_list(ops->alg_name);
-	if (!head) {
-		WD_ERR("no device to support %s\n", ops->alg_name);
-		return -WD_ENODEV;
-	}
-
-	/* get numa num and device num of each numa from uacce_dev list */
-	wd_get_dev_numa(head, numa_dev_num, &numa_num, MAX_NUMA_NUM);
-	if (numa_num == 0 || numa_num > MAX_NUMA_NUM) {
-		WD_ERR("numa num err(%u)!\n", numa_num);
-		wd_free_list_accels(head);
-		return -WD_ENODEV;
-	}
+	int i;
 
-	config->numa_num = numa_num;
-	config->config_per_numa = calloc(numa_num, sizeof(*config_numa));
-	if (!config->config_per_numa) {
-		ret = -WD_ENOMEM;
-		goto free_list;
-	}
+	config->config_per_numa = calloc(config->numa_num, sizeof(*config_numa));
+	if (!config->config_per_numa)
+		return -WD_ENOMEM;
 
 	config_numa = config->config_per_numa;
-	for (i = 0; i < MAX_NUMA_NUM; i++) {
+	for (i = 0; i < max_node; i++) {
 		if (!numa_dev_num[i])
 			continue;
 
@@ -440,24 +421,65 @@ static int wd_alloc_numa(struct wd_env_config *config,
 		config_numa->dev = calloc(numa_dev_num[i],
 					  sizeof(struct uacce_dev));
 		if (!config_numa->dev) {
-			ret = -WD_ENOMEM;
-			goto free_mem;
+			/* free config_per_numa and all uacce dev */
+			wd_free_numa(config);
+			return -WD_ENOMEM;
 		}
 
 		config_numa->dev_num = 0;
 		config_numa++;
 	}
 
+	return 0;
+}
+
+static int wd_alloc_numa(struct wd_env_config *config,
+			 const struct wd_alg_ops *ops)
+{
+	struct uacce_dev_list *head;
+	int *numa_dev_num;
+	int ret, max_node;
+
+	max_node = numa_max_node() + 1;
+	if (max_node <= 0)
+		return -WD_EINVAL;
+
+	numa_dev_num = calloc(max_node, sizeof(int));
+	if (!numa_dev_num)
+		return -WD_ENOMEM;
+
+	/* get uacce_dev */
+	head = wd_get_accel_list(ops->alg_name);
+	if (!head) {
+		WD_ERR("no device to support %s\n", ops->alg_name);
+		ret = -WD_ENODEV;
+		goto free_numa_dev_num;
+	}
+
+	/* get numa num and device num of each numa from uacce_dev list */
+	config->numa_num = wd_get_dev_numa(head, numa_dev_num, max_node);
+	if (config->numa_num == 0 || config->numa_num > max_node) {
+		WD_ERR("numa num err(%u)!\n", config->numa_num);
+		ret = -WD_ENODEV;
+		goto free_list;
+	}
+
+	/* alloc and init config_per_numa and all uacce dev */
+	ret = wd_set_config_numa(config, numa_dev_num, max_node);
+	if (ret)
+		goto free_list;
+
 	/* set device and device num for config numa from uacce_dev list */
 	wd_set_numa_dev(head, config);
 	wd_free_list_accels(head);
+	free(numa_dev_num);
 
 	return 0;
 
-free_mem:
-	wd_free_numa(config);
 free_list:
 	wd_free_list_accels(head);
+free_numa_dev_num:
+	free(numa_dev_num);
 	return ret;
 }
 
@@ -981,18 +1003,22 @@ static int wd_sched_fill_table(struct wd_env_config_per_numa *config_numa,
 static int wd_init_sched_config(struct wd_env_config *config)
 {
 	struct wd_env_config_per_numa *config_numa;
+	int i, j, ret, max_node, type_num;
 	struct wd_sched *sched;
-	int type_num = config->op_type_num;
-	int i, j, ret;
 	void *func = NULL;
 
+	max_node = numa_max_node() + 1;
+	if (max_node <= 0)
+		return -WD_EINVAL;
+
 	if (!config->enable_internal_poll)
 		func = config->alg_poll_ctx;
 
 	config->internal_sched = false;
+	type_num = config->op_type_num;
 	if (!config->sched) {
 		config->sched = wd_sched_rr_alloc(SCHED_POLICY_RR, type_num,
-					   MAX_NUMA_NUM, func);
+						  max_node, func);
 		if (!config->sched)
 			return -WD_ENOMEM;
 
-- 
2.31.1

