Skip to contentSkip to conversation

memcontrol03, memcontrol04: Set protection on parent cgroups to prevent kswapd over-reclaim

PATCH In review Wei Gao via ltp 2 participants 7 messages Oct 4 – Oct 12

lore
Share & tools
lore
#0 lore raw

cover memcontrol03, memcontrol04: Set protection on parent cgroups to prevent kswapd over-reclaim

Wei Gao (2):
memcontrol: Add helpers to set and restore parent memory knobs memcontrol03, memcontrol04: Set protection on parent cgroups to
prevent kswapd over-reclaim

 .../kernel/controllers/memcg/memcontrol03.c   | 20 +++++---
 .../kernel/controllers/memcg/memcontrol04.c   | 19 +++++---
 .../controllers/memcg/memcontrol_common.h     | 46 +++++++++++++++++++
 3 files changed, 71 insertions(+), 14 deletions(-)
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/2 memcontrol: Add helpers to set and restore parent memory knobs

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.

---
 .../controllers/memcg/memcontrol_common.h     | 46 +++++++++++++++++++
 1 file changed, 46 insertions(+)
testcases/kernel/controllers/memcg/memcontrol_common.h +46 -0
index 495f924a0..e4b12aff3 100644
@@ -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
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] memcontrol: Add helpers to set and restore parent memory knobs

Hi Wei,

Quoted 1 line · Wei Gao

memcontrol: Add helpers to set and restore parent memory knobs

--- [PATCH 2/2] ---

Review

Quoted 2 lines
(quoted diff) +2 -0
+	set_parent_memory_knob("memory.min", "50M");
+	set_parent_memory_knob("memory.low", "50M");

High severity: overlapping instances of the same test race on the shared ltp knob: X saves 0, Y saves X's 50M, X restores 0 while Y is active, and Y finally restores 50M instead of the original 0. This both removes an active test's protection and leaves a pre-existing ltp group modified. Serialize the complete shared-parent save/use/restore interval, not just the individual reads and writes.

Review

Quoted 3 lines

static void cleanup(void)
{

(quoted diff) +1 -0
+	restore_parent_memory_knob("memory.min");

High severity: the same problem applies to memory.low cleanup: the snapshots exist only in the forked test worker, which the framework kills without running this callback on timeout or interruption. The supervisor does not restore these knobs, so a pre-existing ltp group retains 50M. Move shared-parent bookkeeping and restoration into supervising cgroup cleanup, before cgroup teardown.

Quoted 3 lines

Set memory.min and memory.low on trunk_A and parent cgroups up through the hierarchy before allocating child pagecache, ensuring protection propagates down the tree to prevent kswapd over-reclaim under NUMA memory pressure,

Medium severity: the helper stops at ltp, so it does not establish this protection when the cgroup mount is rooted in a delegated subtree or namespace whose root has memory.min/low set to 0. Global reclaim still walks that real ancestor, and page_counter_calculate_protection() caps the descendants' effective protection to 0. Handle or report insufficient ancestor protection and document the restriction instead of claiming protection throughout arbitrary hierarchies.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 2/2 memcontrol03, memcontrol04: Set protection on parent cgroups to prevent kswapd over-reclaim

In reply to Wei Gao via ltp

On NUMA systems (observed on PowerPC), allocating memory can trigger node-level memory pressure, waking up kswapd when free pages fall below the low watermark on a node.

In cgroup v2, both hard protection (memory.min) and best-effort protection (memory.low) are hierarchical and calculated top-down: effective protection of descendant cgroups is bounded by their ancestors' protection values. When LTP runs inside an existing cgroup hierarchy, ancestor cgroups above the test root have memory.min and memory.low set to 0.

As a result, when kswapd performs node-level background reclaim from the root cgroup downwards, ancestor cgroups without protection provide no protection boundary. kswapd reclaims pages from descendant leaf cgroups regardless of their local memory.min / memory.low settings, causing memcontrol03 and memcontrol04 to fail with over-reclaim:

NUMA node info:
NUMA Node 2: CPUs=[2], MemTotal=765 MB, MemFree=610 MB NUMA Node 0: CPUs=[0], MemTotal=240 MB, MemFree=93 MB NUMA Node 3: CPUs=[3], MemTotal=750 MB, MemFree=419 MB NUMA Node 1: CPUs=[1], MemTotal=766 MB, MemFree=95 MB

Error info:
memcontrol03.c:519: TFAIL: Expect: (A/B/C memory.current=19070976) ~= 34603008

Set memory.min and memory.low on trunk_A and parent cgroups up through the hierarchy before allocating child pagecache, ensuring protection propagates down the tree to prevent kswapd over-reclaim under NUMA memory pressure, and restore the original values upon cleanup.

---
 .../kernel/controllers/memcg/memcontrol03.c   | 20 ++++++++++++-------
 .../kernel/controllers/memcg/memcontrol04.c   | 19 +++++++++++-------
 2 files changed, 25 insertions(+), 14 deletions(-)
testcases/kernel/controllers/memcg/memcontrol03.c +13 -7
index 25b84c24e..721ae0e46 100644
@@ -184,21 +184,24 @@ static void test_memcg_min(void)
 
 	trunk_cg[G] = tst_cg_group_mk(trunk_cg[A], "trunk_G");
 
-	for (i = 0; i < ARRAY_SIZE(leaf_cg); i++) {
+	for (i = 0; i < ARRAY_SIZE(leaf_cg); i++)
 		leaf_cg[i] = tst_cg_group_mk(trunk_cg[B],
 						 "leaf_%c", 'C' + i);
 
-		if (i == E)
-			continue;
-
-		alloc_pagecache_in_child(leaf_cg[i], MB(50));
-	}
-
+	SAFE_CG_PRINT(trunk_cg[A], "memory.min", "50M");
 	SAFE_CG_PRINT(trunk_cg[B], "memory.min", "50M");
 	SAFE_CG_PRINT(leaf_cg[C], "memory.min", "75M");
 	SAFE_CG_PRINT(leaf_cg[D], "memory.min", "25M");
 	SAFE_CG_PRINT(leaf_cg[E], "memory.min", "500M");
 	SAFE_CG_PRINT(leaf_cg[F], "memory.min", "0");
+	set_parent_memory_knob("memory.min", "50M");
+
+	for (i = 0; i < ARRAY_SIZE(leaf_cg); i++) {
+		if (i == E)
+			continue;
+
+		alloc_pagecache_in_child(leaf_cg[i], MB(50));
+	}
 
 	for (attempts = 0; attempts < 5; attempts++) {
 		SAFE_CG_SCANF(trunk_cg[B], "memory.current", "%ld", c);
@@ -223,6 +226,7 @@ static void test_memcg_min(void)
 		     "(A/B/D memory.current=%ld) ~= %d", c[1], MB(21));
 	TST_EXP_EXPR(values_close(c[2], 0, 1),
 		     "(A/B/E memory.current=%ld) ~= 0", c[2]);
+	tst_res(TINFO, "A/B/F memory.current=%ld", c[F]);
 
 	alloc_anon_in_child(trunk_cg[G], MB(170), 1);
 
@@ -231,12 +235,14 @@ static void test_memcg_min(void)
 		     "(A/B memory.current=%ld) ~= %d", c[0], MB(50));
 
 	cleanup_sub_groups();
+	restore_parent_memory_knob("memory.min");
 	SAFE_CLOSE(fd);
 	SAFE_UNLINK(TMPDIR"/tmpfile");
 }
 
 static void cleanup(void)
 {
+	restore_parent_memory_knob("memory.min");
 	cleanup_sub_groups();
 	if (fd > -1)
 		SAFE_CLOSE(fd);
testcases/kernel/controllers/memcg/memcontrol04.c +12 -7
index f1e136df4..10de95bec 100644
@@ -157,21 +157,24 @@ static void test_memcg_low(void)
 
 	trunk_cg[G] = tst_cg_group_mk(trunk_cg[A], "trunk_G");
 
-	for (i = 0; i < ARRAY_SIZE(leaf_cg); i++) {
+	for (i = 0; i < ARRAY_SIZE(leaf_cg); i++)
 		leaf_cg[i] = tst_cg_group_mk(trunk_cg[B],
 						 "leaf_%c", 'C' + i);
 
-		if (i == E)
-			continue;
-
-		alloc_pagecache_in_child(leaf_cg[i], MB(50));
-	}
-
+	SAFE_CG_PRINT(trunk_cg[A], "memory.low", "50M");
 	SAFE_CG_PRINT(trunk_cg[B], "memory.low", "50M");
 	SAFE_CG_PRINT(leaf_cg[C], "memory.low", "75M");
 	SAFE_CG_PRINT(leaf_cg[D], "memory.low", "25M");
 	SAFE_CG_PRINT(leaf_cg[E], "memory.low", "500M");
 	SAFE_CG_PRINT(leaf_cg[F], "memory.low", "0");
+	set_parent_memory_knob("memory.low", "50M");
+
+	for (i = 0; i < ARRAY_SIZE(leaf_cg); i++) {
+		if (i == E)
+			continue;
+
+		alloc_pagecache_in_child(leaf_cg[i], MB(50));
+	}
 
 	alloc_anon_in_child(trunk_cg[G], MB(148));
 
@@ -230,12 +233,14 @@ static void test_memcg_low(void)
 	}
 
 	cleanup_sub_groups();
+	restore_parent_memory_knob("memory.low");
 	SAFE_CLOSE(fd);
 	SAFE_UNLINK(TMPDIR"/tmpfile");
 }
 
 static void cleanup(void)
 {
+	restore_parent_memory_knob("memory.low");
 	cleanup_sub_groups();
 	if (fd > -1)
 		SAFE_CLOSE(fd);
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hello Wei.

Quoted 9 lines · Wei Gao

On NUMA systems (observed on PowerPC), allocating memory can trigger node-level memory pressure, waking up kswapd when free pages fall below the low watermark on a node.

In cgroup v2, both hard protection (memory.min) and best-effort protection (memory.low) are hierarchical and calculated top-down: effective protection of descendant cgroups is bounded by their ancestors' protection values. When LTP runs inside an existing cgroup hierarchy, ancestor cgroups above the test root have memory.min and memory.low set to 0.

Note that this is expected behavior of protection and the test is meant to work "relatively" with its own pressure source and target [1].

Quoted 5 lines

As a result, when kswapd performs node-level background reclaim from the root cgroup downwards, ancestor cgroups without protection provide no protection boundary. kswapd reclaims pages from descendant leaf cgroups regardless of their local memory.min / memory.low settings, causing memcontrol03 and memcontrol04 to fail with over-reclaim:

Generally, this is not specific to this test -- it is because it the whole host is overcommitted (edit: as I see the NUMA info below it could be perhaps that only individual nodes are too small).

So I wonder could this be ensured at higher level -- something that sets up the environment of the individual runners (e.g. reduce parallelism, partition the system with memory limits or as a variant configure generic memory protection)?

WDYT?
Michal

[1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#reclaim-protection

Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 27 lines · Michal Koutný

Hello Wei.

Quoted 9 lines · Wei Gao

On NUMA systems (observed on PowerPC), allocating memory can trigger node-level memory pressure, waking up kswapd when free pages fall below the low watermark on a node.

In cgroup v2, both hard protection (memory.min) and best-effort protection (memory.low) are hierarchical and calculated top-down: effective protection of descendant cgroups is bounded by their ancestors' protection values. When LTP runs inside an existing cgroup hierarchy, ancestor cgroups above the test root have memory.min and memory.low set to 0.

Note that this is expected behavior of protection and the test is meant to work "relatively" with its own pressure source and target [1].

Quoted 5 lines

As a result, when kswapd performs node-level background reclaim from the root cgroup downwards, ancestor cgroups without protection provide no protection boundary. kswapd reclaims pages from descendant leaf cgroups regardless of their local memory.min / memory.low settings, causing memcontrol03 and memcontrol04 to fail with over-reclaim:

Generally, this is not specific to this test -- it is because it the whole host is overcommitted (edit: as I see the NUMA info below it could be perhaps that only individual nodes are too small).

Quoted 6 lines

So I wonder could this be ensured at higher level -- something that sets up the environment of the individual runners (e.g. reduce parallelism, partition the system with memory limits or as a variant configure generic memory protection)?

WDYT?

Protecting this solely from the external runner is tricky since LTP builds a 3-level cgroup hierarchy:
- /sys/fs/cgroup/ltp (Level 1)
- tst_cg (/sys/fs/cgroup/ltp/ltp-testxxxx, Level 2 — trunk_A's parent)
- trunk_cg[A] (Level 3 — trunk_B's parent)
Every level in this chain needs memory.low = 50M. Even if an external runner sets it at /sys/fs/cgroup/ltp, tst_cg or trunk_A will break the protection chain.

Another option is make sure enough memory to the test environment so kswapd doesn't get triggered at all. I guess we can do this to avoid issue?

Quoted 4 lines

Michal

[1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#reclaim-protection

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
Quoted 7 lines · Wei Gao

Protecting this solely from the external runner is tricky since LTP builds a 3-level cgroup hierarchy:
- /sys/fs/cgroup/ltp (Level 1)
- tst_cg (/sys/fs/cgroup/ltp/ltp-testxxxx, Level 2 — trunk_A's parent)
- trunk_cg[A] (Level 3 — trunk_B's parent)
Every level in this chain needs memory.low = 50M. Even if an external runner sets it at /sys/fs/cgroup/ltp, tst_cg or trunk_A will break the protection chain.

I see. (Note that this needn't be the same 50M as inside the test. The value at this level would denote some protection sufficient for test's stability.)

Quoted 2 lines

Another option is make sure enough memory to the test environment so kswapd doesn't get triggered at all. I guess we can do this to avoid issue?

That sounds even better (or limit amount of tests running in parallel wrt available amounts), memory.low on the high level is best-effort in the end.

Michal

Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp