Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement
@ 2026-05-25 15:33 Pavithra
  2026-05-25 17:12 ` [LTP] " linuxtestproject.agent
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Pavithra @ 2026-05-25 15:33 UTC (permalink / raw)
  To: ltp; +Cc: pavrampu

The test was failing with memory.current values much lower than expected:
   TFAIL: (A/B/C memory.current=6684672) ~= 34603008
   TFAIL: (A/B/D memory.current=5373952) ~= 17825792

Root cause:
Child processes allocating pagecache were exiting immediately after
allocation (via tst_reap_children()), causing the pagecache to be
freed before the test could measure memory.current values.

Fix:
Modified alloc_pagecache_in_child() to keep children alive during test:
- Added TEST_DONE checkpoint for child lifecycle coordination
- Parent waits for CHILD_IDLE checkpoint before proceeding
- Child signals CHILD_IDLE after allocation and fsync
- Child waits for TEST_DONE to keep memory allocated during test
- Modified cleanup_sub_groups() to wake waiting children before cleanup
- Changed alloc_anon_in_child() to use SAFE_WAITPID() for specific child
- Added num_children_spawned tracking for accurate cleanup

Signed-off-by: Sachin Sant <sachinp@linux.ibm.com>
Signed-off-by: Pavithra <pavrampu@linux.ibm.com>
---
v2:
  - Added num_children_spawned counter to track spawned children
  - Fixed cleanup_sub_groups() to wake correct number of children

Link to v2: https://lore.kernel.org/ltp/20260427171734.621893-1-pavrampu@linux.ibm.com/

v1:
  - Initial fix for child process lifecycle coordination
  - Added TEST_DONE and CHILD_IDLE checkpoint synchronization

Link to v1: https://lore.kernel.org/ltp/20260330050105.258630-1-pavrampu@linux.ibm.com/  
  
---
 .../kernel/controllers/memcg/memcontrol04.c   | 21 ++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/controllers/memcg/memcontrol04.c b/testcases/kernel/controllers/memcg/memcontrol04.c
index 715cc5bcd..15d8f891c 100644
--- a/testcases/kernel/controllers/memcg/memcontrol04.c
+++ b/testcases/kernel/controllers/memcg/memcontrol04.c
@@ -45,9 +45,11 @@
 static struct tst_cg_group *trunk_cg[3];
 static struct tst_cg_group *leaf_cg[4];
 static int fd = -1;
+static unsigned int num_children_spawned;
 
 enum checkpoints {
-	CHILD_IDLE
+	CHILD_IDLE,
+	TEST_DONE,
 };
 
 enum trunk_cg {
@@ -67,6 +69,12 @@ static void cleanup_sub_groups(void)
 {
 	size_t i;
 
+	if (num_children_spawned > 0) {
+		TST_CHECKPOINT_WAKE2(TEST_DONE, num_children_spawned);
+		tst_reap_children();
+		num_children_spawned = 0;
+	}
+
 	for (i = ARRAY_SIZE(leaf_cg); i > 0; i--) {
 		if (!leaf_cg[i - 1])
 			continue;
@@ -88,7 +96,7 @@ static void alloc_anon_in_child(const struct tst_cg_group *const cg,
 	const pid_t pid = SAFE_FORK();
 
 	if (pid) {
-		tst_reap_children();
+		SAFE_WAITPID(pid, NULL, 0);
 		return;
 	}
 
@@ -107,7 +115,8 @@ static void alloc_pagecache_in_child(const struct tst_cg_group *const cg,
 	const pid_t pid = SAFE_FORK();
 
 	if (pid) {
-		tst_reap_children();
+		num_children_spawned++;
+		TST_CHECKPOINT_WAIT(CHILD_IDLE);
 		return;
 	}
 
@@ -117,6 +126,11 @@ static void alloc_pagecache_in_child(const struct tst_cg_group *const cg,
 		getpid(), tst_cg_group_name(cg), size);
 	alloc_pagecache(fd, size);
 
+	SAFE_FSYNC(fd);
+
+	TST_CHECKPOINT_WAKE(CHILD_IDLE);
+	TST_CHECKPOINT_WAIT(TEST_DONE);
+
 	exit(0);
 }
 
@@ -125,6 +139,7 @@ static void test_memcg_low(void)
 	long c[4];
 	unsigned int i;
 
+	num_children_spawned = 0;
 	fd = SAFE_OPEN(TMPDIR"/tmpfile", O_RDWR | O_CREAT, 0600);
 	trunk_cg[A] = tst_cg_group_mk(tst_cg, "trunk_A");
 
-- 
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [LTP] memcg/memcontrol04: Fix race in pagecache allocation measurement
  2026-05-25 15:33 [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement Pavithra
@ 2026-05-25 17:12 ` linuxtestproject.agent
  2026-06-09  8:34 ` linuxtestproject.agent
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: linuxtestproject.agent @ 2026-05-25 17:12 UTC (permalink / raw)
  To: Pavithra; +Cc: ltp, LTP AI Reviewer

Hi Pavithra,

On Mon, 25 May 2026, Pavithra wrote:
> memcg/memcontrol04: Fix race in pagecache allocation measurement

> +static unsigned int num_children_spawned;

> +enum checkpoints {
> -	CHILD_IDLE
> +	CHILD_IDLE,
> +	TEST_DONE,
>  };

> +	if (num_children_spawned > 0) {
> +		TST_CHECKPOINT_WAKE2(TEST_DONE, num_children_spawned);
> +		tst_reap_children();
> +		num_children_spawned = 0;
> +	}

Correct. Waking all pagecache children before tst_reap_children() and
resetting the counter is safe since cleanup_sub_groups() is idempotent
(called both from test_memcg_low() and cleanup()).

> -		tst_reap_children();
> +		SAFE_WAITPID(pid, NULL, 0);

Good catch. The old tst_reap_children() would have reaped the still-
waiting pagecache children prematurely.

> +	num_children_spawned++;
> +	TST_CHECKPOINT_WAIT(CHILD_IDLE);

Sequential fork+wait pairs mean CHILD_IDLE is always at zero before
the next child is forked, so there's no ambiguity in who signals it.

> +	SAFE_FSYNC(fd);
> +
> +	TST_CHECKPOINT_WAKE(CHILD_IDLE);
> +	TST_CHECKPOINT_WAIT(TEST_DONE);

fsync before signalling the parent ensures the pagecache is durably
accounted before memory.current is read.

> +	num_children_spawned = 0;
>  	fd = SAFE_OPEN(TMPDIR"/tmpfile", O_RDWR | O_CREAT, 0600);

Explicit reset at the start of each test_all invocation is the right
place; handles repeated runs correctly.

Reviewed-by: LTP AI Reviewer <ltp-ai@noreply.github.com>

---
Note:

Our agent completed the review of the patch. The full review can be
found at: https://github.com/linux-test-project/ltp-agent/actions/runs/26411599009

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
```

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] memcg/memcontrol04: Fix race in pagecache allocation measurement
  2026-05-25 15:33 [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement Pavithra
  2026-05-25 17:12 ` [LTP] " linuxtestproject.agent
@ 2026-06-09  8:34 ` linuxtestproject.agent
  2026-06-09  8:34 ` linuxtestproject.agent
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: linuxtestproject.agent @ 2026-06-09  8:34 UTC (permalink / raw)
  To: Pavithra; +Cc: ltp

Hi Pavithra,

On Mon, 25 May 2026 21:03:52 +0530, Pavithra wrote:
> memcg/memcontrol04: Fix race in pagecache allocation measurement

Verdict: Reviewed

---
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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] memcg/memcontrol04: Fix race in pagecache allocation measurement
  2026-05-25 15:33 [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement Pavithra
  2026-05-25 17:12 ` [LTP] " linuxtestproject.agent
  2026-06-09  8:34 ` linuxtestproject.agent
@ 2026-06-09  8:34 ` linuxtestproject.agent
  2026-06-12  9:35 ` [LTP] [PATCH] [PATCH V2] " Andrea Cervesato via ltp
  2026-06-30  8:11 ` Andrea Cervesato via ltp
  4 siblings, 0 replies; 6+ messages in thread
From: linuxtestproject.agent @ 2026-06-09  8:34 UTC (permalink / raw)
  To: Pavithra; +Cc: ltp

Hi Pavithra,

On Mon, 25 May 2026 21:03:52 +0530, Pavithra wrote:
> memcg/memcontrol04: Fix race in pagecache allocation measurement

Verdict: Reviewed

---
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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement
  2026-05-25 15:33 [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement Pavithra
                   ` (2 preceding siblings ...)
  2026-06-09  8:34 ` linuxtestproject.agent
@ 2026-06-12  9:35 ` Andrea Cervesato via ltp
  2026-06-30  8:11 ` Andrea Cervesato via ltp
  4 siblings, 0 replies; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2026-06-12  9:35 UTC (permalink / raw)
  To: Pavithra; +Cc: pavrampu, ltp

Hi Pavithra,

> The test was failing with memory.current values much lower than expected:
>    TFAIL: (A/B/C memory.current=6684672) ~= 34603008
>    TFAIL: (A/B/D memory.current=5373952) ~= 17825792
> 
> Root cause:
> Child processes allocating pagecache were exiting immediately after
> allocation (via tst_reap_children()), causing the pagecache to be
> freed before the test could measure memory.current values.
> 
> Fix:
> Modified alloc_pagecache_in_child() to keep children alive during test:
> - Added TEST_DONE checkpoint for child lifecycle coordination
> - Parent waits for CHILD_IDLE checkpoint before proceeding
> - Child signals CHILD_IDLE after allocation and fsync
> - Child waits for TEST_DONE to keep memory allocated during test
> - Modified cleanup_sub_groups() to wake waiting children before cleanup
> - Changed alloc_anon_in_child() to use SAFE_WAITPID() for specific child
> - Added num_children_spawned tracking for accurate cleanup

This commit message is extremely verbose, please make it simpler the
next time. Something explaining the root cause it's there and it's ok,
but the Fix: section is self explained inside the diff code.


The rest LGTM.

Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com>

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement
  2026-05-25 15:33 [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement Pavithra
                   ` (3 preceding siblings ...)
  2026-06-12  9:35 ` [LTP] [PATCH] [PATCH V2] " Andrea Cervesato via ltp
@ 2026-06-30  8:11 ` Andrea Cervesato via ltp
  4 siblings, 0 replies; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2026-06-30  8:11 UTC (permalink / raw)
  To: Pavithra; +Cc: pavrampu, ltp

Merged, Thanks!

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-06-30  8:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-25 15:33 [LTP] [PATCH] [PATCH V2] memcg/memcontrol04: Fix race in pagecache allocation measurement Pavithra
2026-05-25 17:12 ` [LTP] " linuxtestproject.agent
2026-06-09  8:34 ` linuxtestproject.agent
2026-06-09  8:34 ` linuxtestproject.agent
2026-06-12  9:35 ` [LTP] [PATCH] [PATCH V2] " Andrea Cervesato via ltp
2026-06-30  8:11 ` Andrea Cervesato via ltp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox