This script wraps other tests to check that HugeTLB statistics are restored to what they were before the test was run. Does not account HugeTLB statistics updated by other non-test processes running in the background while the test is running. Change-Id: I1d827656ef215fd85e368f4a3629f306e7f33f18 Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx> --- ...memfd_wrap_test_check_hugetlb_reporting.sh | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 tools/testing/selftests/kvm/guest_memfd_wrap_test_check_hugetlb_reporting.sh diff --git a/tools/testing/selftests/kvm/guest_memfd_wrap_test_check_hugetlb_reporting.sh b/tools/testing/selftests/kvm/guest_memfd_wrap_test_check_hugetlb_reporting.sh new file mode 100755 index 000000000000..475ec5c4ce1b --- /dev/null +++ b/tools/testing/selftests/kvm/guest_memfd_wrap_test_check_hugetlb_reporting.sh @@ -0,0 +1,95 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only +# +# Wrapper that runs test, checking that HugeTLB-related statistics have not +# changed before and after test. +# +# Example: +# ./guest_memfd_wrap_test_check_hugetlb_reporting.sh ./guest_memfd_test +# +# Example of combining this with ./guest_memfd_provide_hugetlb_cgroup_mount.sh: +# ./guest_memfd_provide_hugetlb_cgroup_mount.sh \ +# ./guest_memfd_wrap_test_check_hugetlb_reporting.sh \ +# ./guest_memfd_hugetlb_reporting_test +# +# Copyright (C) 2025, Google LLC. + +declare -A baseline + +hugetlb_sizes=( + "2048kB" + "1048576kB" +) + +statistics=( + "free_hugepages" + "nr_hugepages" + "nr_overcommit_hugepages" + "resv_hugepages" + "surplus_hugepages" +) + +cgroup_hugetlb_sizes=( + "2MB" + "1GB" +) + +cgroup_statistics=( + "limit_in_bytes" + "max_usage_in_bytes" + "usage_in_bytes" +) + +establish_statistics_baseline() { + for size in "${hugetlb_sizes[@]}"; do + + for statistic in "${statistics[@]}"; do + + local path="/sys/kernel/mm/hugepages/hugepages-${size}/${statistic}" + baseline["$path"]=$(cat "$path") + + done + + done + + if [ -n "$HUGETLB_CGROUP_PATH" ]; then + + for size in "${cgroup_hugetlb_sizes[@]}"; do + + for statistic in "${cgroup_statistics[@]}"; do + + local rsvd_path="${HUGETLB_CGROUP_PATH}/hugetlb.${size}.rsvd.${statistic}" + local path="${HUGETLB_CGROUP_PATH}/hugetlb.${size}.${statistic}" + + baseline["$rsvd_path"]=$(cat "$rsvd_path") + baseline["$path"]=$(cat "$path") + + done + + done + + fi +} + +assert_path_at_baseline() { + local path=$1 + + current=$(cat "$path") + expected=${baseline["$path"]} + if [ "$current" != "$expected" ]; then + echo "$path was $current instead of $expected" + fi +} + +assert_statistics_at_baseline() { + for path in "${!baseline[@]}"; do + assert_path_at_baseline $path + done +} + + +establish_statistics_baseline + +$@ + +assert_statistics_at_baseline -- 2.49.0.1045.g170613ef41-goog