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

selftests/vm: make charge_reserved_hugetlb.sh work with existing cgroup setting

The hugetlb cgroup reservation test charge_reserved_hugetlb.sh assume
that no cgroup filesystems are mounted before running the test. That is
not true in many cases. As a result, the test fails to run. Fix that
by querying the current cgroup mount setting and using the existing
cgroup setup instead before attempting to freshly mount a cgroup
filesystem.

Similar change is also made for hugetlb_reparenting_test.sh as well,
though it still has problem if cgroup v2 isn't used.

The patched test scripts were run on a centos 8 based system to verify
that they ran properly.

Link: https://lkml.kernel.org/r/20220106201359.1646575-1-longman@redhat.com
Fixes: 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Signed-off-by: Waiman Long <longman@redhat.com>
Acked-by: Mina Almasry <almasrymina@google.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Waiman Long and committed by
Linus Torvalds
209376ed e9ea874a

+34 -23
+20 -14
tools/testing/selftests/vm/charge_reserved_hugetlb.sh
··· 24 24 reservation_usage_file=rsvd.current 25 25 fi 26 26 27 - cgroup_path=/dev/cgroup/memory 28 - if [[ ! -e $cgroup_path ]]; then 29 - mkdir -p $cgroup_path 30 - if [[ $cgroup2 ]]; then 27 + if [[ $cgroup2 ]]; then 28 + cgroup_path=$(mount -t cgroup2 | head -1 | awk -e '{print $3}') 29 + if [[ -z "$cgroup_path" ]]; then 30 + cgroup_path=/dev/cgroup/memory 31 31 mount -t cgroup2 none $cgroup_path 32 - else 32 + do_umount=1 33 + fi 34 + echo "+hugetlb" >$cgroup_path/cgroup.subtree_control 35 + else 36 + cgroup_path=$(mount -t cgroup | grep ",hugetlb" | awk -e '{print $3}') 37 + if [[ -z "$cgroup_path" ]]; then 38 + cgroup_path=/dev/cgroup/memory 33 39 mount -t cgroup memory,hugetlb $cgroup_path 40 + do_umount=1 34 41 fi 35 42 fi 36 - 37 - if [[ $cgroup2 ]]; then 38 - echo "+hugetlb" >/dev/cgroup/memory/cgroup.subtree_control 39 - fi 43 + export cgroup_path 40 44 41 45 function cleanup() { 42 46 if [[ $cgroup2 ]]; then ··· 112 108 113 109 function wait_for_hugetlb_memory_to_get_depleted() { 114 110 local cgroup="$1" 115 - local path="/dev/cgroup/memory/$cgroup/hugetlb.${MB}MB.$reservation_usage_file" 111 + local path="$cgroup_path/$cgroup/hugetlb.${MB}MB.$reservation_usage_file" 116 112 # Wait for hugetlbfs memory to get depleted. 117 113 while [ $(cat $path) != 0 ]; do 118 114 echo Waiting for hugetlb memory to get depleted. ··· 125 121 local cgroup="$1" 126 122 local size="$2" 127 123 128 - local path="/dev/cgroup/memory/$cgroup/hugetlb.${MB}MB.$reservation_usage_file" 124 + local path="$cgroup_path/$cgroup/hugetlb.${MB}MB.$reservation_usage_file" 129 125 # Wait for hugetlbfs memory to get written. 130 126 while [ $(cat $path) != $size ]; do 131 127 echo Waiting for hugetlb memory reservation to reach size $size. ··· 138 134 local cgroup="$1" 139 135 local size="$2" 140 136 141 - local path="/dev/cgroup/memory/$cgroup/hugetlb.${MB}MB.$fault_usage_file" 137 + local path="$cgroup_path/$cgroup/hugetlb.${MB}MB.$fault_usage_file" 142 138 # Wait for hugetlbfs memory to get written. 143 139 while [ $(cat $path) != $size ]; do 144 140 echo Waiting for hugetlb memory to reach size $size. ··· 578 574 done # populate 579 575 done # method 580 576 581 - umount $cgroup_path 582 - rmdir $cgroup_path 577 + if [[ $do_umount ]]; then 578 + umount $cgroup_path 579 + rmdir $cgroup_path 580 + fi
+13 -8
tools/testing/selftests/vm/hugetlb_reparenting_test.sh
··· 18 18 usage_file=current 19 19 fi 20 20 21 - CGROUP_ROOT='/dev/cgroup/memory' 22 - MNT='/mnt/huge/' 23 21 24 - if [[ ! -e $CGROUP_ROOT ]]; then 25 - mkdir -p $CGROUP_ROOT 26 - if [[ $cgroup2 ]]; then 22 + if [[ $cgroup2 ]]; then 23 + CGROUP_ROOT=$(mount -t cgroup2 | head -1 | awk -e '{print $3}') 24 + if [[ -z "$CGROUP_ROOT" ]]; then 25 + CGROUP_ROOT=/dev/cgroup/memory 27 26 mount -t cgroup2 none $CGROUP_ROOT 28 - sleep 1 29 - echo "+hugetlb +memory" >$CGROUP_ROOT/cgroup.subtree_control 30 - else 27 + do_umount=1 28 + fi 29 + echo "+hugetlb +memory" >$CGROUP_ROOT/cgroup.subtree_control 30 + else 31 + CGROUP_ROOT=$(mount -t cgroup | grep ",hugetlb" | awk -e '{print $3}') 32 + if [[ -z "$CGROUP_ROOT" ]]; then 33 + CGROUP_ROOT=/dev/cgroup/memory 31 34 mount -t cgroup memory,hugetlb $CGROUP_ROOT 35 + do_umount=1 32 36 fi 33 37 fi 38 + MNT='/mnt/huge/' 34 39 35 40 function get_machine_hugepage_size() { 36 41 hpz=$(grep -i hugepagesize /proc/meminfo)
+1 -1
tools/testing/selftests/vm/write_hugetlb_memory.sh
··· 14 14 reserve=$9 15 15 16 16 echo "Putting task in cgroup '$cgroup'" 17 - echo $$ > /dev/cgroup/memory/"$cgroup"/cgroup.procs 17 + echo $$ > ${cgroup_path:-/dev/cgroup/memory}/"$cgroup"/cgroup.procs 18 18 19 19 echo "Method is $method" 20 20