[LTP] [PATCH v3 2/3] numa: Move write_node_cpusets() into libs/numa

Pradeep Susarla pradeep.susarla@gmail.com
Sun Oct 4 23:15:34 CEST 2026


write_node_cpusets() lived in testcases/kernel/lib/numa_cpuset.c and was
built into libkerntest.a, while its declaration sat in numa_helper.h.
Move the function into libs/numa/tse_numa.c (libltpnuma.a) next to the
rest of the NUMA helpers and declare it in include/tse_numa.h, so that
the exposed header matches the libs/ code it belongs to.

Tests that call write_node_cpusets() (ksm02, ksm04, oom04, oom05) now
link -lltpnuma, matching ksm06 which already did. The buffer used to
build the per-CPU sysfs path is sized to fit the "/online" suffix to
avoid a -Wformat-truncation warning.

No functional changes intended.

Part of #1276.

Signed-off-by: Pradeep Susarla <pradeep.susarla@gmail.com>
---
 include/tse_numa.h                     | 13 ++++++
 libs/numa/tse_numa.c                   | 57 +++++++++++++++++++++++
 testcases/kernel/include/numa_helper.h |  4 --
 testcases/kernel/lib/numa_cpuset.c     | 64 --------------------------
 testcases/kernel/mem/ksm/Makefile      |  2 +
 testcases/kernel/mem/ksm/ksm_common.h  |  1 +
 testcases/kernel/mem/oom/Makefile      |  4 ++
 testcases/kernel/mem/oom/oom04.c       |  1 +
 testcases/kernel/mem/oom/oom05.c       |  1 +
 9 files changed, 79 insertions(+), 68 deletions(-)
 delete mode 100644 testcases/kernel/lib/numa_cpuset.c

diff --git a/include/tse_numa.h b/include/tse_numa.h
index ac6aab4..20da7d7 100644
--- a/include/tse_numa.h
+++ b/include/tse_numa.h
@@ -119,4 +119,17 @@ enum tse_numa_types {
  */
 struct tse_nodemap *tse_get_nodemap(int type, size_t min_mem_kb);
 
+struct tst_cg_group;
+
+/**
+ * write_node_cpusets() - Sets up the cpuset cgroup controller for a NUMA node.
+ *
+ * Writes the node id to cpuset.mems and the list of the node's online CPUs to
+ * cpuset.cpus. If the node has no CPUs, CPU 0 is used instead.
+ *
+ * @cg: Cgroup to configure.
+ * @nd: NUMA node id.
+ */
+void write_node_cpusets(const struct tst_cg_group *cg, long nd);
+
 #endif /* TSE_NUMA_H__ */
diff --git a/libs/numa/tse_numa.c b/libs/numa/tse_numa.c
index 835b458..3852e0c 100644
--- a/libs/numa/tse_numa.c
+++ b/libs/numa/tse_numa.c
@@ -15,6 +15,7 @@
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
 #include "tse_numa.h"
+#include "tst_cgroup.h"
 #include "lapi/numaif.h"
 
 void tse_nodemap_print_counters(struct tse_nodemap *nodes)
@@ -222,3 +223,59 @@ struct tse_nodemap *tse_get_nodemap(int type, size_t min_mem_kb)
 }
 
 #endif
+
+#define PATH_SYS_SYSTEM "/sys/devices/system"
+
+static void gather_node_cpus(char *cpus, long nd)
+{
+	int ncpus = 0;
+	int i;
+	long online;
+	char buf[BUFSIZ];
+	char path[BUFSIZ], path1[BUFSIZ + sizeof("/online")];
+
+	while (tst_path_exists(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus))
+		ncpus++;
+
+	for (i = 0; i < ncpus; i++) {
+		snprintf(path, BUFSIZ,
+			 PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i);
+		if (tst_path_exists("%s", path)) {
+			snprintf(path1, sizeof(path1), "%s/online", path);
+			/*
+			 * if there is no online knob, then the cpu cannot
+			 * be taken offline
+			 */
+			if (tst_path_exists("%s", path1)) {
+				SAFE_FILE_SCANF(path1, "%ld", &online);
+				if (online == 0)
+					continue;
+			}
+			sprintf(buf, "%d,", i);
+			strcat(cpus, buf);
+		}
+	}
+	/* Remove the trailing comma. */
+	cpus[strlen(cpus) - 1] = '\0';
+}
+
+void write_node_cpusets(const struct tst_cg_group *cg, long nd)
+{
+	char cpus[BUFSIZ] = "";
+
+	SAFE_CG_PRINTF(cg, "cpuset.mems", "%ld", nd);
+
+	gather_node_cpus(cpus, nd);
+	/*
+	 * If the 'nd' node doesn't contain any CPUs,
+	 * the first ID of CPU '0' will be used as
+	 * the value of cpuset.cpus.
+	 */
+	if (strlen(cpus) != 0) {
+		SAFE_CG_PRINT(cg, "cpuset.cpus", cpus);
+	} else {
+		tst_res(TINFO, "No CPUs in the node%ld; "
+				"using only CPU0", nd);
+		SAFE_CG_PRINT(cg, "cpuset.cpus", "0");
+	}
+}
diff --git a/testcases/kernel/include/numa_helper.h b/testcases/kernel/include/numa_helper.h
index e67ddaa..0d449c3 100644
--- a/testcases/kernel/include/numa_helper.h
+++ b/testcases/kernel/include/numa_helper.h
@@ -27,8 +27,6 @@
 # include <numaif.h>
 #endif
 
-struct tst_cg_group;
-
 #define NH_MEMS (1 << 0)
 #define NH_CPUS (1 << 1)
 
@@ -55,6 +53,4 @@ int get_allowed_nodes(int flag, int count, ...);
 void nh_dump_nodes(void);
 int is_numa(void (*cleanup_fn)(void), int flag, int min_nodes);
 
-void write_node_cpusets(const struct tst_cg_group *cg, long nd);
-
 #endif /* NUMA_HELPER_H */
diff --git a/testcases/kernel/lib/numa_cpuset.c b/testcases/kernel/lib/numa_cpuset.c
deleted file mode 100644
index 088d2d4..0000000
--- a/testcases/kernel/lib/numa_cpuset.c
+++ /dev/null
@@ -1,64 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-/*
- * Copyright (c) Linux Test Project, 2011-2021
- * Copyright (c) Cyril Hrubis <chrubis@suse.cz> 2024
- */
-
-#define TST_NO_DEFAULT_MAIN
-#include "tst_test.h"
-#include "tst_cgroup.h"
-#include "numa_helper.h"
-
-static void gather_node_cpus(char *cpus, long nd)
-{
-	int ncpus = 0;
-	int i;
-	long online;
-	char buf[BUFSIZ];
-	char path[BUFSIZ], path1[BUFSIZ];
-
-	while (tst_path_exists(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus))
-		ncpus++;
-
-	for (i = 0; i < ncpus; i++) {
-		snprintf(path, BUFSIZ,
-			 PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i);
-		if (tst_path_exists("%s", path)) {
-			snprintf(path1, BUFSIZ, "%s/online", path);
-			/*
-			 * if there is no online knob, then the cpu cannot
-			 * be taken offline
-			 */
-			if (tst_path_exists("%s", path1)) {
-				SAFE_FILE_SCANF(path1, "%ld", &online);
-				if (online == 0)
-					continue;
-			}
-			sprintf(buf, "%d,", i);
-			strcat(cpus, buf);
-		}
-	}
-	/* Remove the trailing comma. */
-	cpus[strlen(cpus) - 1] = '\0';
-}
-
-void write_node_cpusets(const struct tst_cg_group *cg, long nd)
-{
-	char cpus[BUFSIZ] = "";
-
-	SAFE_CG_PRINTF(cg, "cpuset.mems", "%ld", nd);
-
-	gather_node_cpus(cpus, nd);
-	/*
-	 * If the 'nd' node doesn't contain any CPUs,
-	 * the first ID of CPU '0' will be used as
-	 * the value of cpuset.cpus.
-	 */
-	if (strlen(cpus) != 0) {
-		SAFE_CG_PRINT(cg, "cpuset.cpus", cpus);
-	} else {
-		tst_res(TINFO, "No CPUs in the node%ld; "
-				"using only CPU0", nd);
-		SAFE_CG_PRINT(cg, "cpuset.cpus", "0");
-	}
-}
diff --git a/testcases/kernel/mem/ksm/Makefile b/testcases/kernel/mem/ksm/Makefile
index 2af02a2..5af7289 100644
--- a/testcases/kernel/mem/ksm/Makefile
+++ b/testcases/kernel/mem/ksm/Makefile
@@ -4,6 +4,8 @@
 top_srcdir		?= ../../../..
 
 LTPLIBS = numa
+ksm02: LTPLDLIBS = -lltpnuma
+ksm04: LTPLDLIBS = -lltpnuma
 ksm06: LTPLDLIBS = -lltpnuma
 
 include $(top_srcdir)/include/mk/testcases.mk
diff --git a/testcases/kernel/mem/ksm/ksm_common.h b/testcases/kernel/mem/ksm/ksm_common.h
index d677b22..cb89b5e 100644
--- a/testcases/kernel/mem/ksm/ksm_common.h
+++ b/testcases/kernel/mem/ksm/ksm_common.h
@@ -13,6 +13,7 @@
 #include "tst_test.h"
 #include "ksm_helper.h"
 #include "numa_helper.h"
+#include "tse_numa.h"
 #include "ksm_test.h"
 
 #define DEFAULT_MEMSIZE 128
diff --git a/testcases/kernel/mem/oom/Makefile b/testcases/kernel/mem/oom/Makefile
index db621b9..dc038f8 100644
--- a/testcases/kernel/mem/oom/Makefile
+++ b/testcases/kernel/mem/oom/Makefile
@@ -18,6 +18,10 @@
 
 top_srcdir		?= ../../../..
 
+LTPLIBS = numa
+oom04: LTPLDLIBS = -lltpnuma
+oom05: LTPLDLIBS = -lltpnuma
+
 include $(top_srcdir)/include/mk/testcases.mk
 
 include $(top_srcdir)/testcases/kernel/include/lib.mk
diff --git a/testcases/kernel/mem/oom/oom04.c b/testcases/kernel/mem/oom/oom04.c
index b879e68..394a138 100644
--- a/testcases/kernel/mem/oom/oom04.c
+++ b/testcases/kernel/mem/oom/oom04.c
@@ -19,6 +19,7 @@
 
 #include "tst_test.h"
 #include "numa_helper.h"
+#include "tse_numa.h"
 #include "oom.h"
 
 #ifdef HAVE_NUMA_V2
diff --git a/testcases/kernel/mem/oom/oom05.c b/testcases/kernel/mem/oom/oom05.c
index 998bd86..93c0bc3 100644
--- a/testcases/kernel/mem/oom/oom05.c
+++ b/testcases/kernel/mem/oom/oom05.c
@@ -19,6 +19,7 @@
 
 #include "tst_test.h"
 #include "numa_helper.h"
+#include "tse_numa.h"
 #include "oom.h"
 
 #ifdef HAVE_NUMA_V2
-- 
2.43.0



More information about the ltp mailing list