Skip to contentSkip to conversation

madvise09: restore cgroup v1 memory limits with numeric values

PATCH Superseded Li Wang 4 participants 12 messages Jul 30 – Jul 31

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/2 madvise09: restore cgroup v1 memory limits with numeric values

From: Li Wang <[email protected]>

madvise09 resets memory.max and memory.swap.max to "max" before retrying the child process, so the retried child does not inherit the strict memory limits from the previous run.

This works for cgroup v2, where "max" is the valid unlimited value. However, cgroup v1 memory.limit_in_bytes does not accept the string "max". When the LTP cgroup abstraction maps memory.max to the v1 memory limit knob, writing "max" can fail with EINVAL:

tst_test.c:1875: TINFO: Overall timeout per run is 0h 00m 30s madvise09.c:154: TBROK: vdprintf(18</sys/fs/cgroup/memory/ltp/test-27493>,

		   	  'memory.limit_in_bytes', '%s'<max>): EINVAL (22)

tst_test.c:478: TINFO: Child process reported TBROK killing the test tst_test.c:1938: TINFO: Killed the leftover descendant processes

Save the original cgroup v1 memory and swap limits during setup and restore those numeric values on retry. Keep using "max" for cgroup v2.

Fixes c118be931cd ("madvise09: Reset cgroup limits before retrying test")
---
 testcases/kernel/syscalls/madvise/madvise09.c | 29 ++++++++++++++-----
 1 file changed, 22 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/madvise/madvise09.c +22 -7
index 2fc1da581..2e792bd11 100644
@@ -54,8 +54,8 @@ static int swap_accounting_enabled;
 #define TOUCHED_PAGE1 0
 #define TOUCHED_PAGE2 10
 
-static long long mem_limit;
-static long long swap_limit;
+static long long mem_limit, ori_mem_limit;
+static long long swap_limit, ori_swap_limit;
 
 static void memory_pressure_child(void)
 {
@@ -151,9 +151,17 @@ static void child(void)
 	 * Otherwise, the retried child inherits the strict MEM_LIMIT from the previous
 	 * run, causing MADV_FREE pages to be dropped immediately before we touch them.
 	 */
-	SAFE_CG_PRINT(tst_cg, "memory.max", "max");
-	if (swap_accounting_enabled)
-		SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
+	if (TST_CG_VER_IS_V1(tst_cg, "memory")) {
+		SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);
+	} else {
+		SAFE_CG_PRINT(tst_cg, "memory.max", "max");
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
+	}
 
 	ptr = SAFE_MMAP(NULL, PAGES * page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@@ -278,10 +286,17 @@ retry:
 
 static void setup(void)
 {
-	if (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+	if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+		SAFE_CG_SCANF(tst_cg, "memory.max", "%lld", &ori_mem_limit);
+
+	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
 		swap_accounting_enabled = 1;
-	else
+
+		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+			SAFE_CG_SCANF(tst_cg, "memory.swap.max", "%lld", &ori_swap_limit);
+	} else {
 		tst_res(TINFO, "Swap accounting is disabled");
+	}
 
 	page_size = getpagesize();
 
Signature · 5 lines
2.55.0


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

PATCH 2/2 mailmap: enable [email protected] address

---
 .mailmap | 1 +
 1 file changed, 1 insertion(+)
.mailmap +1 -0
index d1e4419f0..35f54a937 100644
@@ -1,3 +1,4 @@
+Li Wang <[email protected]> <[email protected]>
 Li Wang <[email protected]> <[email protected]>
 Li Wang <[email protected]> <[email protected]>
 Petr Vorel <[email protected]> <[email protected]>
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
In reply to Li Wang

Hi Li,

Quoted 1 line · Li Wang

madvise09: restore cgroup v1 memory limits with numeric values

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

testcases/kernel/syscalls/madvise/madvise09.c static void child(void) · line 159

Quoted 6 lines
testcases/kernel/syscalls/madvise/madvise09.c +6 -0
+	if (TST_CG_VER_IS_V1(tst_cg, "memory")) {
+		SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);
+	} else {

Doesn't this hit EINVAL again on v1, just with a different value?

On the retry path the cgroup still carries the limits written by the previous child at the end of child():

memory.max = mem_limit = 16 * PAGES * page_size (8MB @4K)
memory.swap.max = swap_limit = 2 * mem_limit (16MB @4K)

On v1 these alias to memory.limit_in_bytes and memory.memsw.limit_in_bytes, and the kernel enforces memory.max <= memsw.max in mem_cgroup_resize_max() (mm/memcontrol-v1.c):

	limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
				   max <= memcg->memsw.max;
	if (!limits_invariant) {
		mutex_unlock(&memcg_max_mutex);
		ret = -EINVAL;
		break;
	}

ori_mem_limit is the v1 default (PAGE_COUNTER_MAX, ~8EB), which is well above the 16MB memsw limit still installed. So writing memory.max first should fail with EINVAL and SAFE_CG_PRINTF aborts with TBROK.

When raising the limits the writes have to go memsw first:

	if (swap_accounting_enabled)
		SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);

	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);

The lowering sequence further down stays as it is, since lowering memory.max first keeps the invariant.

Review

Quoted 2 lines
(quoted diff) +0 -0
 	/*
 	 * Reset cgroup memory limits to default ("max") in case this is a retry run.

The comment above the hunk still says the limits are reset to "max", which is now only true for the v2 branch. Could it be reworded to cover both cases?

testcases/kernel/syscalls/madvise/madvise09.c retry: · line 296

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise09.c +7 -2
-	if (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+	if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+		SAFE_CG_SCANF(tst_cg, "memory.max", "%lld", &ori_mem_limit);
+
+	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
 		swap_accounting_enabled = 1;
-	else
+
+		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+			SAFE_CG_SCANF(tst_cg, "memory.swap.max", "%lld", &ori_swap_limit);

TST_CG_VER_IS_V1() expands to a tst_cg_ver() call and is evaluated three times across setup() and child(). Would storing it once in a static (e.g. "static int cg_v1;") be simpler, given child() needs it anyway?

Minor: LTP usually spells such saved values "orig_*", so orig_mem_limit and orig_swap_limit would match the rest of the tree.

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

Quoted 1 line

+Li Wang <[email protected]> <[email protected]>

Patch 1/2 is already signed off with [email protected], so would it make sense to order this one first in the series?

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
author
#8 lore raw
Quoted 41 lines · On Thu, Jul 30, 2026 at 08:29:01AM +0000, [email protected]…

Hi Li,

Quoted 1 line · Li Wang

madvise09: restore cgroup v1 memory limits with numeric values

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

testcases/kernel/syscalls/madvise/madvise09.c static void child(void) · line 159

Quoted 6 lines
testcases/kernel/syscalls/madvise/madvise09.c +6 -0
+	if (TST_CG_VER_IS_V1(tst_cg, "memory")) {
+		SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);
+	} else {

Doesn't this hit EINVAL again on v1, just with a different value?

On the retry path the cgroup still carries the limits written by the previous child at the end of child():

memory.max = mem_limit = 16 * PAGES * page_size (8MB @4K)
memory.swap.max = swap_limit = 2 * mem_limit (16MB @4K)

On v1 these alias to memory.limit_in_bytes and memory.memsw.limit_in_bytes, and the kernel enforces memory.max <= memsw.max in mem_cgroup_resize_max() (mm/memcontrol-v1.c):

	limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
				   max <= memcg->memsw.max;
	if (!limits_invariant) {
		mutex_unlock(&memcg_max_mutex);
		ret = -EINVAL;
		break;
	}

ori_mem_limit is the v1 default (PAGE_COUNTER_MAX, ~8EB), which is well above the 16MB memsw limit still installed. So writing memory.max first should fail with EINVAL and SAFE_CG_PRINTF aborts with TBROK.

When raising the limits the writes have to go memsw first:

Quoted 7 lines
	if (swap_accounting_enabled)
		SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);

	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);

The lowering sequence further down stays as it is, since lowering memory.max first keeps the invariant.

Yes, good point. On cgroup v1 the retry path is raising both limits back to their original values, and raising memory.limit_in_bytes before memory.memsw.limit_in_bytes can violate the v1 invariant memory.limit_in_bytes <= memory.memsw.limit_in_bytes while the old memsw limit is still installed.

I will fix this in V2.

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +0 -0
 	/*
 	 * Reset cgroup memory limits to default ("max") in case this is a retry run.

The comment above the hunk still says the limits are reset to "max", which is now only true for the v2 branch. Could it be reworded to cover both cases?

Sure, this can be rewriten.

Quoted 15 lines

testcases/kernel/syscalls/madvise/madvise09.c retry: · line 296

Quoted 11 lines
testcases/kernel/syscalls/madvise/madvise09.c +7 -2
-	if (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+	if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+		SAFE_CG_SCANF(tst_cg, "memory.max", "%lld", &ori_mem_limit);
+
+	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
 		swap_accounting_enabled = 1;
-	else
+
+		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+			SAFE_CG_SCANF(tst_cg, "memory.swap.max", "%lld", &ori_swap_limit);

TST_CG_VER_IS_V1() expands to a tst_cg_ver() call and is evaluated three times across setup() and child(). Would storing it once in a static (e.g. "static int cg_v1;") be simpler, given child() needs it anyway?

Hmm, this is a little bit nitpicking.

Quoted 2 lines

Minor: LTP usually spells such saved values "orig_*", so orig_mem_limit and orig_swap_limit would match the rest of the tree.

Sure, orig_* looks better.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by on v1 2/2
#3 lore raw

Re: [LTP] [PATCH 2/2] mailmap: enable [email protected] address

In reply to Li Wang

Hi Li,

.mailmap line 1

Quoted 11 lines
---
 .mailmap | 1 +
 1 file changed, 1 insertion(+)
.mailmap +1 -0
index d1e4419f0..35f54a937 100644
@@ -1,3 +1,4 @@
+Li Wang <[email protected]> <[email protected]>

Congratulation to a new job :).

Kind regards,
Petr

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

Re: [LTP] [PATCH 2/2] mailmap: enable [email protected] address

Quoted 16 lines · Petr Vorel

Hi Li,

.mailmap line 1

Quoted 11 lines
---
 .mailmap | 1 +
 1 file changed, 1 insertion(+)
.mailmap +1 -0
index d1e4419f0..35f54a937 100644
@@ -1,3 +1,4 @@
+Li Wang <[email protected]> <[email protected]>
Quoted 1 line

Congratulation to a new job :).

Thank you Petr, Andrea.

Yes, I have started a new job and will still be working on Linux quality related area, with a continued focus on LTP :).

Just sent out v2 but forgot to include both of your Acked-by tags; I will add them when merging.

Signature · 5 lines
Regards,
Li Wang

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

Re: [LTP] [PATCH 2/2] mailmap: enable [email protected] address

Quoted 3 lines

Yes, I have started a new job and will still be working on Linux quality related area, with a continued focus on LTP :).

That sounds fantastic! Thanks for continuing LTP maintenance. It's really appreciate :-)

And congratulations for your new career!

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw
In reply to Li Wang

Hi Li,

Quoted 30 lines

static void memory_pressure_child(void)
{

testcases/kernel/syscalls/madvise/madvise09.c +13 -4
@@ -151,9 +151,17 @@ static void child(void)
 	 * Otherwise, the retried child inherits the strict MEM_LIMIT from the previous
 	 * run, causing MADV_FREE pages to be dropped immediately before we touch them.
 	 */
-	SAFE_CG_PRINT(tst_cg, "memory.max", "max");
-	if (swap_accounting_enabled)
-		SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
+	if (TST_CG_VER_IS_V1(tst_cg, "memory")) {
+		SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);
+	} else {
+		SAFE_CG_PRINT(tst_cg, "memory.max", "max");
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
+	}
 
 	ptr = SAFE_MMAP(NULL, PAGES * page_size, PROT_READ | PROT_WRITE,
 			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@@ -278,10 +286,17 @@ retry:
 
 static void setup(void)
 {
-	if (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+	if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+		SAFE_CG_SCANF(tst_cg, "memory.max", "%lld", &ori_mem_limit);

testcases/kernel/syscalls/madvise/madvise09.c retry: · line 299

Quoted 10 lines
testcases/kernel/syscalls/madvise/madvise09.c +7 -1
+
+	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
 		swap_accounting_enabled = 1;
-	else
+
+		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+			SAFE_CG_SCANF(tst_cg, "memory.swap.max", "%lld", &ori_swap_limit);
+	} else {
 		tst_res(TINFO, "Swap accounting is disabled");
+	}

I see a pattern inside text function and setup(), maybe we can create a function that reset memory.max and memory.swap.max in one place.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#10 lore raw
Quoted 23 lines · Andrea Cervesato
Quoted 23 lines
testcases/kernel/syscalls/madvise/madvise09.c +17 -2
+	if (TST_CG_VER_IS_V1(tst_cg, "memory")) {
+		SAFE_CG_PRINTF(tst_cg, "memory.max", "%lld", ori_mem_limit);
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lld", ori_swap_limit);
+	} else {
+		SAFE_CG_PRINT(tst_cg, "memory.max", "max");
+
+		if (swap_accounting_enabled)
+			SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
+	}
 
 static void setup(void)
 {
-	if (SAFE_CG_HAS(tst_cg, "memory.swap.max"))
+	if (TST_CG_VER_IS_V1(tst_cg, "memory"))
+		SAFE_CG_SCANF(tst_cg, "memory.max", "%lld", &ori_mem_limit);
+
+	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
 		swap_accounting_enabled = 1;
-	else
+
+		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
Quoted 8 lines

testcases/kernel/syscalls/madvise/madvise09.c retry: · line 299

Quoted 4 lines
testcases/kernel/syscalls/madvise/madvise09.c +3 -0
+			SAFE_CG_SCANF(tst_cg, "memory.swap.max", "%lld", &ori_swap_limit);
+	} else {
 		tst_res(TINFO, "Swap accounting is disabled");
+	}

I see a pattern inside text function and setup(), maybe we can create a function that reset memory.max and memory.swap.max in one place.

Not exactly, the logic in setup (save) and test (restore) are not identical, one reads the other writes.

Or, did I miss anything there?

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by Andrea Cervesato
#11 lore raw

Hi Li,

Quoted 4 lines

Not exactly, the logic in setup (save) and test (restore) are not identical, one reads the other writes.

Or, did I miss anything there?

Uops :-) feel free to add:

and merge then.

Thanks,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

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

Re: [LTP] [PATCH 2/2] mailmap: enable [email protected] address

In reply to Andrea Cervesato via ltp

Hi Li, all,

Quoted 2 lines

That sounds fantastic! Thanks for continuing LTP maintenance. It's really appreciate :-)

That's a great news, Li. Thank you!

Kind regards,
Petr

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