Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftest/mm: make hugetlb_reparenting_test tolerant to async reparenting

In cgroup v2, memory and hugetlb usage reparenting is asynchronous. This
can cause test flakiness when immediately asserting usage after deleting a
child cgroup. To address this, add a helper function
`assert_with_retry()` that checks usage values with a timeout-based retry.
This improves test stability without relying on fixed sleep delays.

Also bump up the tolerance size to 7MB.

To avoid False Positives:
...
# Assert memory charged correctly for child only use.
# actual a = 11 MB
# expected a = 0 MB
# fail
# cleanup
# [FAIL]
not ok 11 hugetlb_reparenting_test.sh -cgroup-v2 # exit=1
# 0
# SUMMARY: PASS=10 SKIP=0 FAIL=1

Link: https://lkml.kernel.org/r/20250407084201.74492-1-liwang@redhat.com
Signed-off-by: Li Wang <liwang@redhat.com>
Tested-by: Donet Tom <donettom@linux.ibm.com>
Cc: Waiman Long <longman@redhat.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Kirill A. Shuemov <kirill.shutemov@linux.intel.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Li Wang and committed by
Andrew Morton
e487a5d5 ee414bd9

+41 -55
+41 -55
tools/testing/selftests/mm/hugetlb_reparenting_test.sh
··· 36 36 do_umount=1 37 37 fi 38 38 fi 39 - MNT='/mnt/huge/' 39 + MNT='/mnt/huge' 40 40 41 41 function get_machine_hugepage_size() { 42 42 hpz=$(grep -i hugepagesize /proc/meminfo) ··· 60 60 set -e 61 61 } 62 62 63 + function assert_with_retry() { 64 + local actual_path="$1" 65 + local expected="$2" 66 + local tolerance=$((7 * 1024 * 1024)) 67 + local timeout=20 68 + local interval=1 69 + local start_time 70 + local now 71 + local elapsed 72 + local actual 73 + 74 + start_time=$(date +%s) 75 + 76 + while true; do 77 + actual="$(cat "$actual_path")" 78 + 79 + if [[ $actual -ge $(($expected - $tolerance)) ]] && 80 + [[ $actual -le $(($expected + $tolerance)) ]]; then 81 + return 0 82 + fi 83 + 84 + now=$(date +%s) 85 + elapsed=$((now - start_time)) 86 + 87 + if [[ $elapsed -ge $timeout ]]; then 88 + echo "actual = $((${actual%% *} / 1024 / 1024)) MB" 89 + echo "expected = $((${expected%% *} / 1024 / 1024)) MB" 90 + cleanup 91 + exit 1 92 + fi 93 + 94 + sleep $interval 95 + done 96 + } 97 + 63 98 function assert_state() { 64 99 local expected_a="$1" 65 100 local expected_a_hugetlb="$2" ··· 105 70 expected_b="$3" 106 71 expected_b_hugetlb="$4" 107 72 fi 108 - local tolerance=$((5 * 1024 * 1024)) 109 73 110 - local actual_a 111 - actual_a="$(cat "$CGROUP_ROOT"/a/memory.$usage_file)" 112 - if [[ $actual_a -lt $(($expected_a - $tolerance)) ]] || 113 - [[ $actual_a -gt $(($expected_a + $tolerance)) ]]; then 114 - echo actual a = $((${actual_a%% *} / 1024 / 1024)) MB 115 - echo expected a = $((${expected_a%% *} / 1024 / 1024)) MB 116 - echo fail 74 + assert_with_retry "$CGROUP_ROOT/a/memory.$usage_file" "$expected_a" 75 + assert_with_retry "$CGROUP_ROOT/a/hugetlb.${MB}MB.$usage_file" "$expected_a_hugetlb" 117 76 118 - cleanup 119 - exit 1 120 - fi 121 - 122 - local actual_a_hugetlb 123 - actual_a_hugetlb="$(cat "$CGROUP_ROOT"/a/hugetlb.${MB}MB.$usage_file)" 124 - if [[ $actual_a_hugetlb -lt $(($expected_a_hugetlb - $tolerance)) ]] || 125 - [[ $actual_a_hugetlb -gt $(($expected_a_hugetlb + $tolerance)) ]]; then 126 - echo actual a hugetlb = $((${actual_a_hugetlb%% *} / 1024 / 1024)) MB 127 - echo expected a hugetlb = $((${expected_a_hugetlb%% *} / 1024 / 1024)) MB 128 - echo fail 129 - 130 - cleanup 131 - exit 1 132 - fi 133 - 134 - if [[ -z "$expected_b" || -z "$expected_b_hugetlb" ]]; then 135 - return 136 - fi 137 - 138 - local actual_b 139 - actual_b="$(cat "$CGROUP_ROOT"/a/b/memory.$usage_file)" 140 - if [[ $actual_b -lt $(($expected_b - $tolerance)) ]] || 141 - [[ $actual_b -gt $(($expected_b + $tolerance)) ]]; then 142 - echo actual b = $((${actual_b%% *} / 1024 / 1024)) MB 143 - echo expected b = $((${expected_b%% *} / 1024 / 1024)) MB 144 - echo fail 145 - 146 - cleanup 147 - exit 1 148 - fi 149 - 150 - local actual_b_hugetlb 151 - actual_b_hugetlb="$(cat "$CGROUP_ROOT"/a/b/hugetlb.${MB}MB.$usage_file)" 152 - if [[ $actual_b_hugetlb -lt $(($expected_b_hugetlb - $tolerance)) ]] || 153 - [[ $actual_b_hugetlb -gt $(($expected_b_hugetlb + $tolerance)) ]]; then 154 - echo actual b hugetlb = $((${actual_b_hugetlb%% *} / 1024 / 1024)) MB 155 - echo expected b hugetlb = $((${expected_b_hugetlb%% *} / 1024 / 1024)) MB 156 - echo fail 157 - 158 - cleanup 159 - exit 1 77 + if [[ -n "$expected_b" && -n "$expected_b_hugetlb" ]]; then 78 + assert_with_retry "$CGROUP_ROOT/a/b/memory.$usage_file" "$expected_b" 79 + assert_with_retry "$CGROUP_ROOT/a/b/hugetlb.${MB}MB.$usage_file" "$expected_b_hugetlb" 160 80 fi 161 81 } 162 82 ··· 165 175 cleanup 166 176 167 177 echo 168 - echo 169 178 echo Test charge, rmdir, uncharge 170 179 setup 171 180 echo mkdir ··· 184 195 185 196 echo done 186 197 echo 187 - echo 188 198 if [[ ! $cgroup2 ]]; then 189 199 echo "Test parent and child hugetlb usage" 190 200 setup ··· 200 212 assert_state 0 $(($size * 2)) 0 $size 201 213 202 214 rmdir "$CGROUP_ROOT"/a/b 203 - sleep 5 204 215 echo Assert memory reparent correctly. 205 216 assert_state 0 $(($size * 2)) 206 217 ··· 211 224 cleanup 212 225 fi 213 226 214 - echo 215 227 echo 216 228 echo "Test child only hugetlb usage" 217 229 echo setup