min_free_kbytes: Handle transient memory drops in check_monitor
PATCH 1/1 min_free_kbytes: Handle transient memory drops in check_monitor
High memory pressure can cause MemFree to temporarily drop below the min_free_kbytes threshold before the kernel reclaimer can catch up. This results in intermittent test failures, particularly observed on openQA aarch64 machines.
Implement a 1-second grace period with exponential backoff polling (from 1ms up to 512ms) in check_monitor() to allow the kernel time to reclaim memory. Also the global 'end' flag is reset for multi-iteration support (-i).
--- .../kernel/mem/tunable/min_free_kbytes.c | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-)
testcases/kernel/mem/tunable/min_free_kbytes.c +26 -10
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-or-later /* -* Copyright (c) Linux Test Project, 2012-2025 +* Copyright (c) Linux Test Project, 2012-2026 * Copyright (C) 2012-2017 Red Hat, Inc. */
@@ -53,6 +53,8 @@ static void min_free_kbytes_test(void)int pid, status;struct sigaction sa; + end =0 ; + sa.sa_handler = sighandler;if (sigemptyset (&sa.sa_mask) <0 )tst_brk (TBROK |TERRNO ,"sigemptyset" );
@@ -140,14 +142,13 @@ static void test_tune(unsigned long overcommit_policy) }else {if (WIFEXITED (status)) {if (WEXITSTATUS (status) !=0 ) { -tst_res (TFAIL ,"child unexpectedly " -"failed: %d" , status); +tst_res (TFAIL ,"child unexpectedly failed: %d" , + status); } }else if (!WIFSIGNALED (status) ||WTERMSIG (status) !=SIGKILL ) { -tst_res (TFAIL , -"child unexpectedly failed: %d" , - status); +tst_res (TFAIL ,"child unexpectedly failed: %d" , + status); } } }
@@ -183,18 +184,33 @@ static void check_monitor(void) {unsigned long tune;unsigned long memfree; +int i;while (!end) { memfree =SAFE_READ_MEMINFO ("MemFree:" ); tune =TST_SYS_CONF_LONG_GET (MIN_FREE_KBYTES );if (memfree < tune) { -tst_res (TINFO ,"MemFree is %lu kB, " -"min_free_kbytes is %lu kB" , memfree, tune); -tst_res (TFAIL ,"MemFree < min_free_kbytes" ); +/* +* Give it some time to reclaim. The kernel should keep +* MemFree above min_free_kbytes, but transient drops +* are possible under high pressure. +*/ +for (i =1 ; i <1024 ; i *=2 ) { +usleep (i *1000 ); + memfree =SAFE_READ_MEMINFO ("MemFree:" ); +if (memfree >= tune) +break ; + } + +if (memfree < tune) { +tst_res (TINFO ,"MemFree is %lu kB, min_free_kbytes is %lu kB" , + memfree, tune); +tst_res (TFAIL ,"MemFree < min_free_kbytes" ); + } } -sleep (2 ); +usleep (100000 ); } }
Signature · 5 lines
2.54.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
Quoted 11 lines
High memory pressure can cause MemFree to temporarily drop below the
min_free_kbytesthreshold before the kernel reclaimer can catch up. This results in intermittent test failures, particularly observed on openQA aarch64 machines.Implement a 1-second grace period with exponential backoff polling (from 1ms up to 512ms) in
check_monitor()to allow the kernel time to reclaim memory.Also the global 'end' flag is reset for multi-iteration support (-i).
IMHO this is not needed. Is it just wrong AI hint or you really spot it's needed?
Quoted 18 lines
Signed-off-by Wei Gao <[email protected]>--- .../kernel/mem/tunable/min_free_kbytes.c | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-)testcases/kernel/mem/tunable/min_free_kbytes.c +1
-1@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-or-later /* -* Copyright (c) Linux Test Project, 2012-2025 +* Copyright (c) Linux Test Project, 2012-2026 * Copyright (C) 2012-2017 Red Hat, Inc. */
testcases/kernel/mem/tunable/min_free_kbytes.c static void min_free_kbytes_test(void) · line 56
Quoted 4 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +1
-0@@ -53,6 +53,8 @@ static void min_free_kbytes_test(void)int pid, status;struct sigaction sa; + end =0 ;
Why I think it's not needed? Here you're in the parent. But the value is changed only in child (forked process), they run in a separate memory spaces (change in child is not visible for the parent).
In patch without it + updated commit message you may add:
Quoted 11 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +2
-2+ sa.sa_handler = sighandler;if (sigemptyset (&sa.sa_mask) <0 )tst_brk (TBROK |TERRNO ,"sigemptyset" );@@ -140,14 +142,13 @@ static void test_tune(unsigned long overcommit_policy) }else {if (WIFEXITED (status)) {if (WEXITSTATUS (status) !=0 ) { -tst_res (TFAIL ,"child unexpectedly " -"failed: %d" , status); + tst_res(TFAIL, "child unexpectedly failed: %d",
testcases/kernel/mem/tunable/min_free_kbytes.c static void test_tune(unsigned long overcommit_policy) · line 152
Quoted 10 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +3
-3+ status); } }else if (!WIFSIGNALED (status) ||WTERMSIG (status) !=SIGKILL ) { -tst_res (TFAIL , -"child unexpectedly failed: %d" , - status); +tst_res (TFAIL ,"child unexpectedly failed: %d" , + status); }
Unrelated cleanup, but thanks.
Quoted 39 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +19
-4} }@@ -183,18 +184,33 @@ static void check_monitor(void) {unsigned long tune;unsigned long memfree; +int i;while (!end) { memfree =SAFE_READ_MEMINFO ("MemFree:" ); tune =TST_SYS_CONF_LONG_GET (MIN_FREE_KBYTES );if (memfree < tune) { -tst_res (TINFO ,"MemFree is %lu kB, " -"min_free_kbytes is %lu kB" , memfree, tune); -tst_res (TFAIL ,"MemFree < min_free_kbytes" ); +/* +* Give it some time to reclaim. The kernel should keep +* MemFree above min_free_kbytes, but transient drops +* are possible under high pressure. +*/ +for (i =1 ; i <1024 ; i *=2 ) { +usleep (i *1000 ); + memfree =SAFE_READ_MEMINFO ("MemFree:" ); +if (memfree >= tune) +break ; + } + +if (memfree < tune) { +tst_res (TINFO ,"MemFree is %lu kB, min_free_kbytes is %lu kB" , + memfree, tune); +tst_res (TFAIL ,"MemFree < min_free_kbytes" ); + } } -sleep (2 ); +usleep (100000 ); } }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 7 lines · Petr Vorel
Hi Wei,
Quoted 4 lines
High memory pressure can cause MemFree to temporarily drop below the
min_free_kbytesthreshold before the kernel reclaimer can catch up. This results in intermittent test failures, particularly observed on openQA aarch64 machines.
Quoted 8 lines
Quoted 6 lines
Implement a 1-second grace period with exponential backoff polling (from 1ms up to 512ms) in
check_monitor()to allow the kernel time to reclaim memory.Also the global 'end' flag is reset for multi-iteration support (-i).
IMHO this is not needed. Is it just wrong AI hint or you really spot it's needed?
Yes, reset 'end' flag is not needed, AI indeed complain wrong hint firstly and later take this as defence programing.
Quoted 92 lines
Quoted 19 lines
Signed-off-by Wei Gao <[email protected]>--- .../kernel/mem/tunable/min_free_kbytes.c | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-)testcases/kernel/mem/tunable/min_free_kbytes.c +1
-1@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-or-later /* -* Copyright (c) Linux Test Project, 2012-2025 +* Copyright (c) Linux Test Project, 2012-2026 * Copyright (C) 2012-2017 Red Hat, Inc. */ testcases/kernel/mem/tunable/min_free_kbytes.c static void min_free_kbytes_test(void) · line 56
Quoted 4 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +1
-0@@ -53,6 +53,8 @@ static void min_free_kbytes_test(void)int pid, status;struct sigaction sa; + end =0 ;Why I think it's not needed? Here you're in the parent. But the value is changed only in child (forked process), they run in a separate memory spaces (change in child is not visible for the parent).
In patch without it + updated commit message you may add:
Reviewed-by Petr Vorel <[email protected]>Quoted 11 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +2
-2+ sa.sa_handler = sighandler;if (sigemptyset (&sa.sa_mask) <0 )tst_brk (TBROK |TERRNO ,"sigemptyset" );@@ -140,14 +142,13 @@ static void test_tune(unsigned long overcommit_policy) }else {if (WIFEXITED (status)) {if (WEXITSTATUS (status) !=0 ) { -tst_res (TFAIL ,"child unexpectedly " -"failed: %d" , status); + tst_res(TFAIL, "child unexpectedly failed: %d",testcases/kernel/mem/tunable/min_free_kbytes.c static void test_tune(unsigned long overcommit_policy) · line 152
Quoted 10 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +3
-3+ status); } }else if (!WIFSIGNALED (status) ||WTERMSIG (status) !=SIGKILL ) { -tst_res (TFAIL , -"child unexpectedly failed: %d" , - status); +tst_res (TFAIL ,"child unexpectedly failed: %d" , + status); }Unrelated cleanup, but thanks.
Quoted 39 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +19
-4} }@@ -183,18 +184,33 @@ static void check_monitor(void) {unsigned long tune;unsigned long memfree; +int i;while (!end) { memfree =SAFE_READ_MEMINFO ("MemFree:" ); tune =TST_SYS_CONF_LONG_GET (MIN_FREE_KBYTES );if (memfree < tune) { -tst_res (TINFO ,"MemFree is %lu kB, " -"min_free_kbytes is %lu kB" , memfree, tune); -tst_res (TFAIL ,"MemFree < min_free_kbytes" ); +/* +* Give it some time to reclaim. The kernel should keep +* MemFree above min_free_kbytes, but transient drops +* are possible under high pressure. +*/ +for (i =1 ; i <1024 ; i *=2 ) { +usleep (i *1000 ); + memfree =SAFE_READ_MEMINFO ("MemFree:" ); +if (memfree >= tune) +break ; + } + +if (memfree < tune) { +tst_res (TINFO ,"MemFree is %lu kB, min_free_kbytes is %lu kB" , + memfree, tune); +tst_res (TFAIL ,"MemFree < min_free_kbytes" ); + } } -sleep (2 ); +usleep (100000 ); } }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
...
Quoted 14 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +6
-3while (!end) { memfree =SAFE_READ_MEMINFO ("MemFree:" ); tune =TST_SYS_CONF_LONG_GET (MIN_FREE_KBYTES );if (memfree < tune) { -tst_res (TINFO ,"MemFree is %lu kB, " -"min_free_kbytes is %lu kB" , memfree, tune); -tst_res (TFAIL ,"MemFree < min_free_kbytes" ); +/* +* Give it some time to reclaim. The kernel should keep +* MemFree above min_free_kbytes, but transient drops +* are possible under high pressure. +*/ +for (i =1 ; i <1024 ; i *=2 ) {
testcases/kernel/mem/tunable/min_free_kbytes.c static void check_monitor(void) · line 209
Quoted 10 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +10
-0+usleep (i *1000 ); + memfree =SAFE_READ_MEMINFO ("MemFree:" ); +if (memfree >= tune) +break ; + } + +if (memfree < tune) { +tst_res (TINFO ,"MemFree is %lu kB, min_free_kbytes is %lu kB" , + memfree, tune); +tst_res (TFAIL ,"MemFree < min_free_kbytes" );
nit: TINFO message followed by TFAIL message is not optimal. I'd either always print TINFO:
tst_res(TINFO, "MemFree is %lu kB, min_free_kbytes is %lu kB",
memfree, tune);
if (memfree < tune) {
tst_res(TFAIL, "MemFree < min_free_kbytes");or combine info in the error message:
if (memfree < tune) {
tst_res(TFAIL, "MemFree %lu kB < min_free_kbytes %lu kB", memfree, tune);Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Wei,
testcases/kernel/mem/tunable/min_free_kbytes.c static void min_free_kbytes_test(void) · line 57
Quoted 6 lines · Wei Gao
min_free_kbytes: Handle transient memory drops incheck_monitortestcases/kernel/mem/tunable/min_free_kbytes.c +2
-0struct sigaction sa; + end =0 ; +
Good catch for multi-iteration support.
testcases/kernel/mem/tunable/min_free_kbytes.c static void check_monitor(void) · line 213
Quoted 2 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +1
-1-sleep (2 ); +usleep (100000 );
Changing from 2s to 100ms makes the monitor much more responsive.
testcases/kernel/mem/tunable/min_free_kbytes.c static void check_monitor(void) · line 204
Quoted 6 lines
testcases/kernel/mem/tunable/min_free_kbytes.c +6
-0+for (i =1 ; i <1024 ; i *=2 ) { +usleep (i *1000 ); + memfree =SAFE_READ_MEMINFO ("MemFree:" ); +if (memfree >= tune) +break ; + }
The exponential backoff is a good approach. Total wait time is ~1023ms (1+2+4+8+16+32+64+128+256+512), matching the commit message.
All good. Approved.
---
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