madvise09: Scale cgroup memory/swap limits dynamically with page size
PATCH 1/1 madvise09: Scale cgroup memory/swap limits dynamically with page size
On systems with 64KB page size (such as ppc64le), the mapped footprint of PAGES (128) is 128 * 64KB = 8MB. Since the cgroup MEM_LIMIT was hardcoded to 8MB, there was zero headroom left for the parent's process overhead, resulting in OOM when trying to fork the memory pressure process.
This caused the test to enter an infinite "Both children killed, retrying..." loop.
Fix this by scaling mem_limit and swap_limit dynamically based on the system page size at runtime. Also add a dynamic swap availability check in setup() using tst_available_swap() to skip gracefully on machines lacking sufficient swap space, and define BASE_SWAP_LIMIT for the static .min_swap_avail check.
--- testcases/kernel/syscalls/madvise/madvise09.c | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/madvise/madvise09.c +17 -7
@@ -54,8 +54,10 @@ static int swap_accounting_enabled;#define TOUCHED_PAGE1 0#define TOUCHED_PAGE2 10 -#define MEM_LIMIT (8 * 1024 * 1024) -#define SWAP_LIMIT (2 * MEM_LIMIT) +#define BASE_SWAP_LIMIT (16 * 1024 * 1024) + +static long mem_limit; +static long swap_limit;static void memory_pressure_child (void ) {
@@ -176,12 +178,12 @@ static void child(void) ptr[TOUCHED_PAGE1 * page_size] ='b' ; ptr[TOUCHED_PAGE2 * page_size] ='b' ; -SAFE_CG_PRINTF (tst_cg,"memory.max" ,"%d" ,MEM_LIMIT ); -tst_res (TINFO ,"Setting memory.max to %d bytes" ,MEM_LIMIT ); +SAFE_CG_PRINTF (tst_cg,"memory.max" ,"%ld" , mem_limit); +tst_res (TINFO ,"Setting memory.max to %ld bytes" , mem_limit);if (swap_accounting_enabled) { -SAFE_CG_PRINTF (tst_cg,"memory.swap.max" ,"%d" ,SWAP_LIMIT ); -tst_res (TINFO ,"Setting memory.swap.max to %d bytes" ,SWAP_LIMIT ); +SAFE_CG_PRINTF (tst_cg,"memory.swap.max" ,"%ld" , swap_limit); +tst_res (TINFO ,"Setting memory.swap.max to %ld bytes" , swap_limit); }else {tst_res (TINFO ,"memory.swap.max is unavailable, running without SWAP_LIMIT" ); }
@@ -284,6 +286,14 @@ static void setup(void)tst_res (TINFO ,"Swap accounting is disabled" ); page_size =getpagesize (); + + mem_limit =16 *PAGES * page_size; + swap_limit =2 * mem_limit; + +if (tst_available_swap () < swap_limit /1024 ) { +tst_brk (TCONF ,"System needs at least %ldMB free swap to run this test" , + swap_limit /TST_MB ); + } }static struct tst_test test = {
@@ -291,6 +301,6 @@ static struct tst_test test = { .test_all = run, .needs_root = 1, .forks_child = 1, - .min_swap_avail = SWAP_LIMIT / TST_MB, + .min_swap_avail = BASE_SWAP_LIMIT / TST_MB, .needs_cgroup_ctrls = (const char *const []){ "memory", NULL }, };
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
Quoted 5 lines · Wei Gao
madvise09: Scale cgroup memory/swap limits dynamically with page size
* o Set memory limits
* - memory.max = 8MB
* - memory.swap.max = 16MB
Now that mem_limit and swap_limit are derived from page_size at runtime, are these values still accurate for the description block? On a 64KB page system mem_limit becomes 128MB and swap_limit 256MB, but the doc comment still states the old fixed 8MB/16MB.
testcases/kernel/syscalls/madvise/madvise09.c static void setup(void) · line 296
Quoted 4 lines
testcases/kernel/syscalls/madvise/madvise09.c +4
-0+if (tst_available_swap () < swap_limit /1024 ) { +tst_brk (TCONF ,"System needs at least %ldMB free swap to run this test" , + swap_limit /TST_MB ); + }
This check runs unconditionally in setup(), but swap_limit is only ever written to memory.swap.max when swap_accounting_enabled is true; otherwise child() takes the "running without SWAP_LIMIT" path and never enforces it. Could this needlessly skip the test (TCONF) on systems with swap accounting disabled but with less free swap than swap_limit, even though swap_limit isn't used there?
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
PATCH v2 1/1 madvise09: Scale cgroup memory/swap limits dynamically with page size
In reply to Wei Gao via ltpOn systems with 64KB page size (such as ppc64le), the mapped footprint of PAGES (128) is 128 * 64KB = 8MB. Since the cgroup MEM_LIMIT was hardcoded to 8MB, there was zero headroom left for the parent's process overhead, resulting in OOM when trying to fork the memory pressure process.
This caused the test to enter an infinite "Both children killed, retrying..." loop.
Fix this by scaling mem_limit and swap_limit dynamically based on the system page size at runtime, and update the swap pre-check and doc comments to match.
---
Changes v1 -> v2:
- Update doc comment to reflect page-size based limit calculation.
- Guard tst_available_swap() check with swap_accounting_enabled to avoid unnecessary TCONF when swap accounting is off.
testcases/kernel/syscalls/madvise/madvise09.c | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-)
testcases/kernel/syscalls/madvise/madvise09.c +20 -10
@@ -17,11 +17,11 @@* o Write to some of the madvised pages again, these must not be freed * * o Set memory limits -* - memory.max = 8MB -* - memory.swap.max = 16MB +* - memory.max = 16 * PAGES * page_size (8MB on 4KB page size) +* - memory.swap.max = 2 * memory.max (16MB on 4KB page size) * * The reason for doubling the memory.max is to have safe margin -* for forking the memory hungy child etc. And the reason to setting +* for forking the memory hungry child etc. And the reason to setting * memory.swap.max to twice of that is to give the system chance * to try to free some memory before cgroup OOM kicks in and kills * the memory hungry child.
@@ -54,8 +54,10 @@ static int swap_accounting_enabled;#define TOUCHED_PAGE1 0#define TOUCHED_PAGE2 10 -#define MEM_LIMIT (8 * 1024 * 1024) -#define SWAP_LIMIT (2 * MEM_LIMIT) +#define BASE_SWAP_LIMIT (16 * 1024 * 1024) + +static long long mem_limit; +static long long swap_limit;static void memory_pressure_child (void ) {
@@ -176,12 +178,12 @@ static void child(void) ptr[TOUCHED_PAGE1 * page_size] ='b' ; ptr[TOUCHED_PAGE2 * page_size] ='b' ; -SAFE_CG_PRINTF (tst_cg,"memory.max" ,"%d" ,MEM_LIMIT ); -tst_res (TINFO ,"Setting memory.max to %d bytes" ,MEM_LIMIT ); +SAFE_CG_PRINTF (tst_cg,"memory.max" ,"%lld" , mem_limit); +tst_res (TINFO ,"Setting memory.max to %lld bytes" , mem_limit);if (swap_accounting_enabled) { -SAFE_CG_PRINTF (tst_cg,"memory.swap.max" ,"%d" ,SWAP_LIMIT ); -tst_res (TINFO ,"Setting memory.swap.max to %d bytes" ,SWAP_LIMIT ); +SAFE_CG_PRINTF (tst_cg,"memory.swap.max" ,"%lld" , swap_limit); +tst_res (TINFO ,"Setting memory.swap.max to %lld bytes" , swap_limit); }else {tst_res (TINFO ,"memory.swap.max is unavailable, running without SWAP_LIMIT" ); }
@@ -284,6 +286,14 @@ static void setup(void)tst_res (TINFO ,"Swap accounting is disabled" ); page_size =getpagesize (); + + mem_limit =16 *PAGES * page_size; + swap_limit =2 * mem_limit; + +if (swap_accounting_enabled && (tst_available_swap () < swap_limit /1024 )) { +tst_brk (TCONF ,"System needs at least %lldMB free swap to run this test" , + swap_limit /TST_MB ); + } }static struct tst_test test = {
@@ -291,6 +301,6 @@ static struct tst_test test = { .test_all = run, .needs_root = 1, .forks_child = 1, - .min_swap_avail = SWAP_LIMIT / TST_MB, + .min_swap_avail = BASE_SWAP_LIMIT / TST_MB, .needs_cgroup_ctrls = (const char *const []){ "memory", NULL }, };
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
Quoted 1 line · Wei Gao
madvise09: Scale cgroup memory/swap limits dynamically with page size
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
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
testcases/kernel/syscalls/madvise/madvise09.c static void setup(void)
Quoted 7 lines · Wei Gao via ltp
testcases/kernel/syscalls/madvise/madvise09.c +6
-0+ mem_limit =16 *PAGES * page_size; + swap_limit =2 * mem_limit; + +if (swap_accounting_enabled && (tst_available_swap () < swap_limit /1024 )) { +tst_brk (TCONF ,"System needs at least %lldMB free swap to run this test" , + swap_limit /TST_MB );
I suspect this available swap size comparison is duplicated with .min_swap_avail, and perhaps we can remove BASE_SWAP_LIMIT and .min_swap_avail from the test directly.
Quoted 8 lines
static struct
tst_testtest = {testcases/kernel/syscalls/madvise/madvise09.c +1
-1@@ -291,6 +301,6 @@ static struct tst_test test = { .test_all = run, .needs_root = 1, .forks_child = 1, - .min_swap_avail = SWAP_LIMIT / TST_MB, + .min_swap_avail = BASE_SWAP_LIMIT / TST_MB,
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 6 lines · Li Wang
Quoted 3 lines · Wei Gao via ltp
testcases/kernel/syscalls/madvise/madvise09.c +3
-0+ mem_limit =16 *PAGES * page_size; + swap_limit =2 * mem_limit; +
Quoted 7 lines
testcases/kernel/syscalls/madvise/madvise09.c static void setup(void) · line 295
Quoted 3 lines
testcases/kernel/syscalls/madvise/madvise09.c +3
-0+if (swap_accounting_enabled && (tst_available_swap () < swap_limit /1024 )) { +tst_brk (TCONF ,"System needs at least %lldMB free swap to run this test" , + swap_limit /TST_MB );I suspect this available swap size comparison is duplicated with .min_swap_avail, and perhaps we can remove
BASE_SWAP_LIMITand .min_swap_avail from the test directly.
Thanks, new patch sent.
Quoted 13 lines
Quoted 9 lines
static struct
tst_testtest = {testcases/kernel/syscalls/madvise/madvise09.c +1
-1@@ -291,6 +301,6 @@ static struct tst_test test = { .test_all = run, .needs_root = 1, .forks_child = 1, - .min_swap_avail = SWAP_LIMIT / TST_MB, + .min_swap_avail = BASE_SWAP_LIMIT / TST_MB,Signature · 2 lines
Regards, Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 1/1 madvise09: Scale cgroup memory/swap limits dynamically with page size
In reply to Wei Gao via ltpOn systems with 64KB page size (such as ppc64le), the mapped footprint of PAGES (128) is 128 * 64KB = 8MB. Since the cgroup MEM_LIMIT was hardcoded to 8MB, there was zero headroom left for the parent's process overhead, resulting in OOM when trying to fork the memory pressure process.
This caused the test to enter an infinite "Both children killed, retrying..." loop.
Fix this by scaling mem_limit and swap_limit dynamically based on the system page size at runtime, and update the swap pre-check and doc comments to match.
---v2->v3:
- Remove .min_swap_avail
testcases/kernel/syscalls/madvise/madvise09.c | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-)
testcases/kernel/syscalls/madvise/madvise09.c +17 -10
@@ -17,11 +17,11 @@* o Write to some of the madvised pages again, these must not be freed * * o Set memory limits -* - memory.max = 8MB -* - memory.swap.max = 16MB +* - memory.max = 16 * PAGES * page_size (8MB on 4KB page size) +* - memory.swap.max = 2 * memory.max (16MB on 4KB page size) * * The reason for doubling the memory.max is to have safe margin -* for forking the memory hungy child etc. And the reason to setting +* for forking the memory hungry child etc. And the reason to setting * memory.swap.max to twice of that is to give the system chance * to try to free some memory before cgroup OOM kicks in and kills * the memory hungry child.
@@ -54,8 +54,8 @@ static int swap_accounting_enabled;#define TOUCHED_PAGE1 0#define TOUCHED_PAGE2 10 -#define MEM_LIMIT (8 * 1024 * 1024) -#define SWAP_LIMIT (2 * MEM_LIMIT) +static long long mem_limit; +static long long swap_limit;static void memory_pressure_child (void ) {
@@ -176,12 +176,12 @@ static void child(void) ptr[TOUCHED_PAGE1 * page_size] ='b' ; ptr[TOUCHED_PAGE2 * page_size] ='b' ; -SAFE_CG_PRINTF (tst_cg,"memory.max" ,"%d" ,MEM_LIMIT ); -tst_res (TINFO ,"Setting memory.max to %d bytes" ,MEM_LIMIT ); +SAFE_CG_PRINTF (tst_cg,"memory.max" ,"%lld" , mem_limit); +tst_res (TINFO ,"Setting memory.max to %lld bytes" , mem_limit);if (swap_accounting_enabled) { -SAFE_CG_PRINTF (tst_cg,"memory.swap.max" ,"%d" ,SWAP_LIMIT ); -tst_res (TINFO ,"Setting memory.swap.max to %d bytes" ,SWAP_LIMIT ); +SAFE_CG_PRINTF (tst_cg,"memory.swap.max" ,"%lld" , swap_limit); +tst_res (TINFO ,"Setting memory.swap.max to %lld bytes" , swap_limit); }else {tst_res (TINFO ,"memory.swap.max is unavailable, running without SWAP_LIMIT" ); }
@@ -284,6 +284,14 @@ static void setup(void)tst_res (TINFO ,"Swap accounting is disabled" ); page_size =getpagesize (); + + mem_limit =16 *PAGES * page_size; + swap_limit =2 * mem_limit; + +if (tst_available_swap () < swap_limit /1024 ) { +tst_brk (TCONF ,"System needs at least %lldMB free swap to run this test" , + swap_limit /TST_MB ); + } }static struct tst_test test = {
@@ -291,6 +299,5 @@ static struct tst_test test = { .test_all = run, .needs_root = 1, .forks_child = 1, - .min_swap_avail = SWAP_LIMIT / TST_MB, .needs_cgroup_ctrls = (const char *const []){ "memory", NULL }, };
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
Quoted 1 line · Wei Gao
madvise09: Scale cgroup memory/swap limits dynamically with page size
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
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Patch merged, thanks!
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp