[LTP] [PATCH v1 1/2] memcontrol: Add helpers to set and restore parent memory knobs

Wei Gao wegao@suse.com
Sun Oct 4 16:33:26 CEST 2026


In cgroup v2, memory protection (memory.min and memory.low) is hierarchical
and evaluated top-down. The effective protection of descendant cgroups is
bounded by their parent and ancestor protection values. When LTP runs, the
test sandbox cgroups (tst_cg and its parent ltp) default to 0, which causes
background reclaim (such as kswapd under node-level memory pressure) to
reclaim memory from descendants regardless of local protection settings.

Add set_parent_memory_knob() and restore_parent_memory_knob() to
memcontrol_common.h to allow memcg tests to configure protection on both
Level 2 (tst_cg) and Level 1 (the shared ltp directory). The original values
are read and preserved before modification, and restored upon cleanup.

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

diff --git a/testcases/kernel/controllers/memcg/memcontrol_common.h b/testcases/kernel/controllers/memcg/memcontrol_common.h
index 495f924a0..e4b12aff3 100644
--- a/testcases/kernel/controllers/memcg/memcontrol_common.h
+++ b/testcases/kernel/controllers/memcg/memcontrol_common.h
@@ -10,10 +10,56 @@
 #include <stdio.h>
 
 #include "tst_test.h"
+#include "tst_safe_file_at.h"
 
 #define TMPDIR "mntdir"
 #define MB(x) (x << 20)
 
+static char orig_tst_val[64];
+static char orig_ltp_val[64];
+
+static inline void set_parent_memory_knob(const char *knob, const char *val)
+{
+	int tst_fd = tst_cg_group_unified_dir_fd(tst_cg);
+	int ltp_fd;
+
+	if (tst_fd < 0)
+		return;
+
+	ltp_fd = SAFE_OPENAT(tst_fd, "..", O_DIRECTORY | O_RDONLY);
+
+	/* 1. Read and save original values */
+	SAFE_CG_READ(tst_cg, knob, orig_tst_val, sizeof(orig_tst_val));
+	SAFE_FILE_READAT(ltp_fd, knob, orig_ltp_val, sizeof(orig_ltp_val));
+
+	/* 2. Apply new values to Level 2 (tst_cg) and Level 1 (ltp) */
+	SAFE_CG_PRINT(tst_cg, knob, val);
+	SAFE_FILE_PRINTFAT(ltp_fd, knob, "%s\n", val);
+
+	SAFE_CLOSE(ltp_fd);
+}
+
+static inline void restore_parent_memory_knob(const char *knob)
+{
+	int tst_fd, ltp_fd;
+
+	if (!orig_tst_val[0])
+		return;
+
+	tst_fd = tst_cg_group_unified_dir_fd(tst_cg);
+	if (tst_fd < 0)
+		return;
+
+	ltp_fd = SAFE_OPENAT(tst_fd, "..", O_DIRECTORY | O_RDONLY);
+
+	/* Restore original values */
+	SAFE_CG_PRINT(tst_cg, knob, orig_tst_val);
+	SAFE_FILE_PRINTFAT(ltp_fd, knob, "%s", orig_ltp_val);
+
+	SAFE_CLOSE(ltp_fd);
+	orig_tst_val[0] = '\0';
+}
+
 /*
  * Checks if two given values differ by less than err% of their
  * sum. An extra percent is added for every doubling of the page size
-- 
2.55.0



More information about the ltp mailing list