[LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat()

Li Wang li.wang@linux.dev
Fri Nov 20 09:56:34 CET 2026


> Reading memory.stat does not always return up-to-date values: the
> kernel only flushes the pending stats updates synchronously when they
> exceed a threshold (MEMCG_CHARGE_BATCH * num_online_cpus()), otherwise
> the updates are applied by the periodic async flusher, so the values
> can be stale for up to 2 seconds.
> 
> The tests only touch 65 pages (260KiB), which may generate fewer
> stat updates than the threshold on machines with multiple CPUs, so
> the checks right after the memory has been touched may read stale
> values and fail, e.g. on 7.3.0 with 4 CPUs:
> 
>     memcg_stat_rss 1 TFAIL: rss is 0, 266240 expected
> 
> Poll for the expected value with a 3s timeout instead, which follows
> the polling pattern already used by signal_memcg_process() in this
> file.

Patch merged, with some minor polishing:

--- a/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
+++ b/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
@@ -160,15 +160,17 @@ shmmax_cleanup()
 check_mem_stat()
 {
        local item_size
-       local cms_iter
+       local loops=30

-       for cms_iter in $(seq 30); do
+       while [ $loops -gt 0 ]; do
                if [ -e $1 ]; then
                        item_size=$(cat $1)
                else
                        item_size=$(grep -w $1 memory.stat | cut -d " " -f 2)
                fi

+               [ -z "$item_size" ] && break
+
                if [ "$3" ]; then
                        if [ $item_size -ge $2 ] && [ $item_size -le $3 ]; then
                                tst_res TPASS "$1 is ${2}-${3} as expected"
@@ -179,7 +181,8 @@ check_mem_stat()
                        return
                fi

-               tst_sleep 100ms
+               loops=$((loops - 1))
+               [ $loops -gt 0 ] && tst_sleep 100ms
        done

        if [ "$3" ]; then


-- 
Regards,
Li Wang


More information about the ltp mailing list