2 # SPDX-License-Identifier: GPL-2.0
4 # Kselftest framework requirement - SKIP code is 4.
9 if [[ $(id -u) -ne 0 ]]; then
10 echo "This test must be run as root. Skipping..."
14 usage_file=usage_in_bytes
16 if [[ "$1" == "-cgroup-v2" ]]; then
22 if [[ $cgroup2 ]]; then
23 CGROUP_ROOT=$(mount -t cgroup2 | head -1 | awk '{print $3}')
24 if [[ -z "$CGROUP_ROOT" ]]; then
25 CGROUP_ROOT=/dev/cgroup/memory
26 mount -t cgroup2 none $CGROUP_ROOT
29 echo "+hugetlb +memory" >$CGROUP_ROOT/cgroup.subtree_control
31 CGROUP_ROOT=$(mount -t cgroup | grep ",hugetlb" | awk '{print $3}')
32 if [[ -z "$CGROUP_ROOT" ]]; then
33 CGROUP_ROOT=/dev/cgroup/memory
34 mount -t cgroup memory,hugetlb $CGROUP_ROOT
40 function get_machine_hugepage_size() {
41 hpz=$(grep -i hugepagesize /proc/meminfo)
47 MB=$(get_machine_hugepage_size)
52 rm -rf "$MNT"/* 2>/dev/null
53 umount "$MNT" 2>/dev/null
54 rmdir "$MNT" 2>/dev/null
55 rmdir "$CGROUP_ROOT"/a/b 2>/dev/null
56 rmdir "$CGROUP_ROOT"/a 2>/dev/null
57 rmdir "$CGROUP_ROOT"/test1 2>/dev/null
58 echo 0 >/proc/sys/vm/nr_hugepages
62 function assert_state() {
64 local expected_a_hugetlb="$2"
66 local expected_b_hugetlb=""
68 if [ ! -z ${3:-} ] && [ ! -z ${4:-} ]; then
70 expected_b_hugetlb="$4"
72 local tolerance=$((5 * 1024 * 1024))
75 actual_a="$(cat "$CGROUP_ROOT"/a/memory.$usage_file)"
76 if [[ $actual_a -lt $(($expected_a - $tolerance)) ]] ||
77 [[ $actual_a -gt $(($expected_a + $tolerance)) ]]; then
78 echo actual a = $((${actual_a%% *} / 1024 / 1024)) MB
79 echo expected a = $((${expected_a%% *} / 1024 / 1024)) MB
86 local actual_a_hugetlb
87 actual_a_hugetlb="$(cat "$CGROUP_ROOT"/a/hugetlb.${MB}MB.$usage_file)"
88 if [[ $actual_a_hugetlb -lt $(($expected_a_hugetlb - $tolerance)) ]] ||
89 [[ $actual_a_hugetlb -gt $(($expected_a_hugetlb + $tolerance)) ]]; then
90 echo actual a hugetlb = $((${actual_a_hugetlb%% *} / 1024 / 1024)) MB
91 echo expected a hugetlb = $((${expected_a_hugetlb%% *} / 1024 / 1024)) MB
98 if [[ -z "$expected_b" || -z "$expected_b_hugetlb" ]]; then
103 actual_b="$(cat "$CGROUP_ROOT"/a/b/memory.$usage_file)"
104 if [[ $actual_b -lt $(($expected_b - $tolerance)) ]] ||
105 [[ $actual_b -gt $(($expected_b + $tolerance)) ]]; then
106 echo actual b = $((${actual_b%% *} / 1024 / 1024)) MB
107 echo expected b = $((${expected_b%% *} / 1024 / 1024)) MB
114 local actual_b_hugetlb
115 actual_b_hugetlb="$(cat "$CGROUP_ROOT"/a/b/hugetlb.${MB}MB.$usage_file)"
116 if [[ $actual_b_hugetlb -lt $(($expected_b_hugetlb - $tolerance)) ]] ||
117 [[ $actual_b_hugetlb -gt $(($expected_b_hugetlb + $tolerance)) ]]; then
118 echo actual b hugetlb = $((${actual_b_hugetlb%% *} / 1024 / 1024)) MB
119 echo expected b hugetlb = $((${expected_b_hugetlb%% *} / 1024 / 1024)) MB
128 echo 100 >/proc/sys/vm/nr_hugepages
129 mkdir "$CGROUP_ROOT"/a
131 if [[ $cgroup2 ]]; then
132 echo "+hugetlb +memory" >$CGROUP_ROOT/a/cgroup.subtree_control
134 echo 0 >$CGROUP_ROOT/a/cpuset.mems
135 echo 0 >$CGROUP_ROOT/a/cpuset.cpus
138 mkdir "$CGROUP_ROOT"/a/b
140 if [[ ! $cgroup2 ]]; then
141 echo 0 >$CGROUP_ROOT/a/b/cpuset.mems
142 echo 0 >$CGROUP_ROOT/a/b/cpuset.cpus
146 mount -t hugetlbfs none "$MNT"
154 if [[ $cgroup2 ]]; then
155 echo $$ >$CGROUP_ROOT/$cgroup/cgroup.procs
157 echo 0 >$CGROUP_ROOT/$cgroup/cpuset.mems
158 echo 0 >$CGROUP_ROOT/$cgroup/cpuset.cpus
159 echo $$ >"$CGROUP_ROOT/$cgroup/tasks"
161 ./write_to_hugetlbfs -p "$path" -s "$size" -m 0 -o
162 if [[ $cgroup2 ]]; then
163 echo $$ >$CGROUP_ROOT/cgroup.procs
165 echo $$ >"$CGROUP_ROOT/tasks"
172 size=$((${MB} * 1024 * 1024 * 25)) # 50MB = 25 * 2MB hugepages.
178 echo Test charge, rmdir, uncharge
181 mkdir $CGROUP_ROOT/test1
184 write_hugetlbfs test1 "$MNT"/test $size
187 rmdir $CGROUP_ROOT/test1
188 mkdir $CGROUP_ROOT/test1
198 if [[ ! $cgroup2 ]]; then
199 echo "Test parent and child hugetlb usage"
203 write_hugetlbfs a "$MNT"/test $size
205 echo Assert memory charged correctly for parent use.
206 assert_state 0 $size 0 0
208 write_hugetlbfs a/b "$MNT"/test2 $size
210 echo Assert memory charged correctly for child use.
211 assert_state 0 $(($size * 2)) 0 $size
213 rmdir "$CGROUP_ROOT"/a/b
215 echo Assert memory reparent correctly.
216 assert_state 0 $(($size * 2))
220 echo Assert memory uncharged correctly.
228 echo "Test child only hugetlb usage"
233 write_hugetlbfs a/b "$MNT"/test2 $size
235 echo Assert memory charged correctly for child only use.
236 assert_state 0 $(($size)) 0 $size
238 rmdir "$CGROUP_ROOT"/a/b
239 echo Assert memory reparent correctly.
244 echo Assert memory uncharged correctly.