[LTP] [PATCH v2 1/2] memcontrol: Add bind_to_current_cpu() helper

Wei Gao wegao@suse.com
Wed Sep 30 05:53:14 CEST 2026


Add bind_to_current_cpu() to memcontrol_common.h to allow memcg tests
to bind the test process to a single CPU.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 .../controllers/memcg/memcontrol_common.h     | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/testcases/kernel/controllers/memcg/memcontrol_common.h b/testcases/kernel/controllers/memcg/memcontrol_common.h
index 495f924a0..0f5021c0d 100644
--- a/testcases/kernel/controllers/memcg/memcontrol_common.h
+++ b/testcases/kernel/controllers/memcg/memcontrol_common.h
@@ -52,4 +52,31 @@ static inline void alloc_anon(const size_t size)
 	free(buf);
 }
 
+static inline void bind_to_current_cpu(void)
+{
+	int cpu = sched_getcpu();
+	size_t mask_size;
+	cpu_set_t *mask;
+
+	if (cpu < 0)
+		tst_brk(TBROK | TERRNO, "sched_getcpu() failed");
+
+	tst_res(TINFO, "Binding test process to CPU %d", cpu);
+
+	mask = CPU_ALLOC(cpu + 1);
+	if (!mask)
+		tst_brk(TBROK | TERRNO, "CPU_ALLOC() failed");
+
+	mask_size = CPU_ALLOC_SIZE(cpu + 1);
+	CPU_ZERO_S(mask_size, mask);
+	CPU_SET_S(cpu, mask_size, mask);
+
+	if (sched_setaffinity(0, mask_size, mask) < 0) {
+		CPU_FREE(mask);
+		tst_brk(TBROK | TERRNO, "sched_setaffinity() failed");
+	}
+
+	CPU_FREE(mask);
+}
+
 #endif /* MEMCONTROL_COMMON_H__ */
-- 
2.55.0



More information about the ltp mailing list