Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / kvm / x86_64 / nx_huge_pages_test.sh
blobcaad084b8bfdb66ad11873fbfcdc9c9b3407ebee
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0-only */
4 # Wrapper script which performs setup and cleanup for nx_huge_pages_test.
5 # Makes use of root privileges to set up huge pages and KVM module parameters.
7 # Copyright (C) 2022, Google LLC.
9 set -e
11 NX_HUGE_PAGES=$(cat /sys/module/kvm/parameters/nx_huge_pages)
12 NX_HUGE_PAGES_RECOVERY_RATIO=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio)
13 NX_HUGE_PAGES_RECOVERY_PERIOD=$(cat /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms)
14 HUGE_PAGES=$(cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages)
16 # If we're already root, the host might not have sudo.
17 if [ $(whoami) == "root" ]; then
18 function do_sudo () {
19 "$@"
21 else
22 function do_sudo () {
23 sudo "$@"
27 set +e
29 function sudo_echo () {
30 echo "$1" | do_sudo tee -a "$2" > /dev/null
33 NXECUTABLE="$(dirname $0)/nx_huge_pages_test"
35 sudo_echo test /dev/null || exit 4 # KSFT_SKIP=4
38 set -e
40 sudo_echo 1 /sys/module/kvm/parameters/nx_huge_pages
41 sudo_echo 1 /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio
42 sudo_echo 100 /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms
43 sudo_echo "$(( $HUGE_PAGES + 3 ))" /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
45 # Test with reboot permissions
46 if [ $(whoami) == "root" ] || sudo setcap cap_sys_boot+ep $NXECUTABLE 2> /dev/null; then
47 echo Running test with CAP_SYS_BOOT enabled
48 $NXECUTABLE -t 887563923 -p 100 -r
49 test $(whoami) == "root" || sudo setcap cap_sys_boot-ep $NXECUTABLE
50 else
51 echo setcap failed, skipping nx_huge_pages_test with CAP_SYS_BOOT enabled
54 # Test without reboot permissions
55 if [ $(whoami) != "root" ] ; then
56 echo Running test with CAP_SYS_BOOT disabled
57 $NXECUTABLE -t 887563923 -p 100
58 else
59 echo Running as root, skipping nx_huge_pages_test with CAP_SYS_BOOT disabled
62 RET=$?
64 sudo_echo "$NX_HUGE_PAGES" /sys/module/kvm/parameters/nx_huge_pages
65 sudo_echo "$NX_HUGE_PAGES_RECOVERY_RATIO" /sys/module/kvm/parameters/nx_huge_pages_recovery_ratio
66 sudo_echo "$NX_HUGE_PAGES_RECOVERY_PERIOD" /sys/module/kvm/parameters/nx_huge_pages_recovery_period_ms
67 sudo_echo "$HUGE_PAGES" /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
69 exit $RET