463 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03# Please run as root4 5# Kselftest framework requirement - SKIP code is 4.6ksft_skip=47 8count_total=09count_pass=010count_fail=011count_skip=012exitcode=013 14usage() {15 cat <<EOF16usage: ${BASH_SOURCE[0]:-$0} [ options ]17 18 -a: run all tests, including extra ones (other than destructive ones)19 -t: specify specific categories to tests to run20 -h: display this message21 -n: disable TAP output22 -d: run destructive tests23 24The default behavior is to run required tests only. If -a is specified,25will run all tests.26 27Alternatively, specific groups tests can be run by passing a string28to the -t argument containing one or more of the following categories29separated by spaces:30- mmap31 tests for mmap(2)32- gup_test33 tests for gup34- userfaultfd35 tests for userfaultfd(2)36- compaction37 a test for the patch "Allow compaction of unevictable pages"38- mlock39 tests for mlock(2)40- mremap41 tests for mremap(2)42- hugevm43 tests for very large virtual address space44- vmalloc45 vmalloc smoke tests46- hmm47 hmm smoke tests48- madv_populate49 test memadvise(2) MADV_POPULATE_{READ,WRITE} options50- memfd_secret51 test memfd_secret(2)52- process_mrelease53 test process_mrelease(2)54- ksm55 ksm tests that do not require >=2 NUMA nodes56- ksm_numa57 ksm tests that require >=2 NUMA nodes58- pkey59 memory protection key tests60- soft_dirty61 test soft dirty page bit semantics62- pagemap63 test pagemap_scan IOCTL64- cow65 test copy-on-write semantics66- thp67 test transparent huge pages68- hugetlb69 test hugetlbfs huge pages70- migration71 invoke move_pages(2) to exercise the migration entry code72 paths in the kernel73- mkdirty74 test handling of code that might set PTE/PMD dirty in75 read-only VMAs76- mdwe77 test prctl(PR_SET_MDWE, ...)78 79example: ./run_vmtests.sh -t "hmm mmap ksm"80EOF81 exit 082}83 84RUN_ALL=false85RUN_DESTRUCTIVE=false86TAP_PREFIX="# "87 88while getopts "aht:n" OPT; do89 case ${OPT} in90 "a") RUN_ALL=true ;;91 "h") usage ;;92 "t") VM_SELFTEST_ITEMS=${OPTARG} ;;93 "n") TAP_PREFIX= ;;94 "d") RUN_DESTRUCTIVE=true ;;95 esac96done97shift $((OPTIND -1))98 99# default behavior: run all tests100VM_SELFTEST_ITEMS=${VM_SELFTEST_ITEMS:-default}101 102test_selected() {103 if [ "$VM_SELFTEST_ITEMS" == "default" ]; then104 # If no VM_SELFTEST_ITEMS are specified, run all tests105 return 0106 fi107 # If test selected argument is one of the test items108 if [[ " ${VM_SELFTEST_ITEMS[*]} " =~ " ${1} " ]]; then109 return 0110 else111 return 1112 fi113}114 115run_gup_matrix() {116 # -t: thp=on, -T: thp=off, -H: hugetlb=on117 local hugetlb_mb=$(( needmem_KB / 1024 ))118 119 for huge in -t -T "-H -m $hugetlb_mb"; do120 # -u: gup-fast, -U: gup-basic, -a: pin-fast, -b: pin-basic, -L: pin-longterm121 for test_cmd in -u -U -a -b -L; do122 # -w: write=1, -W: write=0123 for write in -w -W; do124 # -S: shared125 for share in -S " "; do126 # -n: How many pages to fetch together? 512 is special127 # because it's default thp size (or 2M on x86), 123 to128 # just test partial gup when hit a huge in whatever form129 for num in "-n 1" "-n 512" "-n 123"; do130 CATEGORY="gup_test" run_test ./gup_test \131 $huge $test_cmd $write $share $num132 done133 done134 done135 done136 done137}138 139# get huge pagesize and freepages from /proc/meminfo140while read -r name size unit; do141 if [ "$name" = "HugePages_Free:" ]; then142 freepgs="$size"143 fi144 if [ "$name" = "Hugepagesize:" ]; then145 hpgsize_KB="$size"146 fi147done < /proc/meminfo148 149# Simple hugetlbfs tests have a hardcoded minimum requirement of150# huge pages totaling 256MB (262144KB) in size. The userfaultfd151# hugetlb test requires a minimum of 2 * nr_cpus huge pages. Take152# both of these requirements into account and attempt to increase153# number of huge pages available.154nr_cpus=$(nproc)155uffd_min_KB=$((hpgsize_KB * nr_cpus * 2))156hugetlb_min_KB=$((256 * 1024))157if [[ $uffd_min_KB -gt $hugetlb_min_KB ]]; then158 needmem_KB=$uffd_min_KB159else160 needmem_KB=$hugetlb_min_KB161fi162 163# set proper nr_hugepages164if [ -n "$freepgs" ] && [ -n "$hpgsize_KB" ]; then165 nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)166 needpgs=$((needmem_KB / hpgsize_KB))167 tries=2168 while [ "$tries" -gt 0 ] && [ "$freepgs" -lt "$needpgs" ]; do169 lackpgs=$((needpgs - freepgs))170 echo 3 > /proc/sys/vm/drop_caches171 if ! echo $((lackpgs + nr_hugepgs)) > /proc/sys/vm/nr_hugepages; then172 echo "Please run this test as root"173 exit $ksft_skip174 fi175 while read -r name size unit; do176 if [ "$name" = "HugePages_Free:" ]; then177 freepgs=$size178 fi179 done < /proc/meminfo180 tries=$((tries - 1))181 done182 if [ "$freepgs" -lt "$needpgs" ]; then183 printf "Not enough huge pages available (%d < %d)\n" \184 "$freepgs" "$needpgs"185 fi186else187 echo "no hugetlbfs support in kernel?"188 exit 1189fi190 191# filter 64bit architectures192ARCH64STR="arm64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sparc64 x86_64"193if [ -z "$ARCH" ]; then194 ARCH=$(uname -m 2>/dev/null | sed -e 's/aarch64.*/arm64/')195fi196VADDR64=0197echo "$ARCH64STR" | grep "$ARCH" &>/dev/null && VADDR64=1198 199tap_prefix() {200 sed -e "s/^/${TAP_PREFIX}/"201}202 203tap_output() {204 if [[ ! -z "$TAP_PREFIX" ]]; then205 read str206 echo $str207 fi208}209 210pretty_name() {211 echo "$*" | sed -e 's/^\(bash \)\?\.\///'212}213 214# Usage: run_test [test binary] [arbitrary test arguments...]215run_test() {216 if test_selected ${CATEGORY}; then217 # On memory constrainted systems some tests can fail to allocate hugepages.218 # perform some cleanup before the test for a higher success rate.219 if [ ${CATEGORY} == "thp" ] | [ ${CATEGORY} == "hugetlb" ]; then220 echo 3 > /proc/sys/vm/drop_caches221 sleep 2222 echo 1 > /proc/sys/vm/compact_memory223 sleep 2224 fi225 226 local test=$(pretty_name "$*")227 local title="running $*"228 local sep=$(echo -n "$title" | tr "[:graph:][:space:]" -)229 printf "%s\n%s\n%s\n" "$sep" "$title" "$sep" | tap_prefix230 231 ("$@" 2>&1) | tap_prefix232 local ret=${PIPESTATUS[0]}233 count_total=$(( count_total + 1 ))234 if [ $ret -eq 0 ]; then235 count_pass=$(( count_pass + 1 ))236 echo "[PASS]" | tap_prefix237 echo "ok ${count_total} ${test}" | tap_output238 elif [ $ret -eq $ksft_skip ]; then239 count_skip=$(( count_skip + 1 ))240 echo "[SKIP]" | tap_prefix241 echo "ok ${count_total} ${test} # SKIP" | tap_output242 exitcode=$ksft_skip243 else244 count_fail=$(( count_fail + 1 ))245 echo "[FAIL]" | tap_prefix246 echo "not ok ${count_total} ${test} # exit=$ret" | tap_output247 exitcode=1248 fi249 fi # test_selected250}251 252echo "TAP version 13" | tap_output253 254CATEGORY="hugetlb" run_test ./hugepage-mmap255 256shmmax=$(cat /proc/sys/kernel/shmmax)257shmall=$(cat /proc/sys/kernel/shmall)258echo 268435456 > /proc/sys/kernel/shmmax259echo 4194304 > /proc/sys/kernel/shmall260CATEGORY="hugetlb" run_test ./hugepage-shm261echo "$shmmax" > /proc/sys/kernel/shmmax262echo "$shmall" > /proc/sys/kernel/shmall263 264CATEGORY="hugetlb" run_test ./map_hugetlb265CATEGORY="hugetlb" run_test ./hugepage-mremap266CATEGORY="hugetlb" run_test ./hugepage-vmemmap267CATEGORY="hugetlb" run_test ./hugetlb-madvise268CATEGORY="hugetlb" run_test ./hugetlb_dio269 270nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages)271# For this test, we need one and just one huge page272echo 1 > /proc/sys/vm/nr_hugepages273CATEGORY="hugetlb" run_test ./hugetlb_fault_after_madv274CATEGORY="hugetlb" run_test ./hugetlb_madv_vs_map275# Restore the previous number of huge pages, since further tests rely on it276echo "$nr_hugepages_tmp" > /proc/sys/vm/nr_hugepages277 278if test_selected "hugetlb"; then279 echo "NOTE: These hugetlb tests provide minimal coverage. Use" | tap_prefix280 echo " https://github.com/libhugetlbfs/libhugetlbfs.git for" | tap_prefix281 echo " hugetlb regression testing." | tap_prefix282fi283 284CATEGORY="mmap" run_test ./map_fixed_noreplace285 286if $RUN_ALL; then287 run_gup_matrix288else289 # get_user_pages_fast() benchmark290 CATEGORY="gup_test" run_test ./gup_test -u291 # pin_user_pages_fast() benchmark292 CATEGORY="gup_test" run_test ./gup_test -a293fi294# Dump pages 0, 19, and 4096, using pin_user_pages:295CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000296CATEGORY="gup_test" run_test ./gup_longterm297 298CATEGORY="userfaultfd" run_test ./uffd-unit-tests299uffd_stress_bin=./uffd-stress300CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16301# Hugetlb tests require source and destination huge pages. Pass in half302# the size of the free pages we have, which is used for *each*.303half_ufd_size_MB=$((freepgs / 2))304CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32305CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private "$half_ufd_size_MB" 32306CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16307CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem-private 20 16308 309#cleanup310echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages311 312CATEGORY="compaction" run_test ./compaction_test313 314if command -v sudo &> /dev/null;315then316 CATEGORY="mlock" run_test sudo -u nobody ./on-fault-limit317else318 echo "# SKIP ./on-fault-limit"319fi320 321CATEGORY="mmap" run_test ./map_populate322 323CATEGORY="mlock" run_test ./mlock-random-test324 325CATEGORY="mlock" run_test ./mlock2-tests326 327CATEGORY="process_mrelease" run_test ./mrelease_test328 329CATEGORY="mremap" run_test ./mremap_test330 331CATEGORY="hugetlb" run_test ./thuge-gen332CATEGORY="hugetlb" run_test ./charge_reserved_hugetlb.sh -cgroup-v2333CATEGORY="hugetlb" run_test ./hugetlb_reparenting_test.sh -cgroup-v2334if $RUN_DESTRUCTIVE; then335nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages)336enable_soft_offline=$(cat /proc/sys/vm/enable_soft_offline)337echo 8 > /proc/sys/vm/nr_hugepages338CATEGORY="hugetlb" run_test ./hugetlb-soft-offline339echo "$nr_hugepages_tmp" > /proc/sys/vm/nr_hugepages340echo "$enable_soft_offline" > /proc/sys/vm/enable_soft_offline341CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison342fi343 344if [ $VADDR64 -ne 0 ]; then345 346 # set overcommit_policy as OVERCOMMIT_ALWAYS so that kernel347 # allows high virtual address allocation requests independent348 # of platform's physical memory.349 350 prev_policy=$(cat /proc/sys/vm/overcommit_memory)351 echo 1 > /proc/sys/vm/overcommit_memory352 CATEGORY="hugevm" run_test ./virtual_address_range353 echo $prev_policy > /proc/sys/vm/overcommit_memory354 355 # va high address boundary switch test356 ARCH_ARM64="arm64"357 prev_nr_hugepages=$(cat /proc/sys/vm/nr_hugepages)358 if [ "$ARCH" == "$ARCH_ARM64" ]; then359 echo 6 > /proc/sys/vm/nr_hugepages360 fi361 CATEGORY="hugevm" run_test bash ./va_high_addr_switch.sh362 if [ "$ARCH" == "$ARCH_ARM64" ]; then363 echo $prev_nr_hugepages > /proc/sys/vm/nr_hugepages364 fi365fi # VADDR64366 367# vmalloc stability smoke test368CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke369 370CATEGORY="mremap" run_test ./mremap_dontunmap371 372CATEGORY="hmm" run_test bash ./test_hmm.sh smoke373 374# MADV_POPULATE_READ and MADV_POPULATE_WRITE tests375CATEGORY="madv_populate" run_test ./madv_populate376 377if [ -x ./memfd_secret ]378then379(echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope 2>&1) | tap_prefix380CATEGORY="memfd_secret" run_test ./memfd_secret381fi382 383# KSM KSM_MERGE_TIME_HUGE_PAGES test with size of 100384CATEGORY="ksm" run_test ./ksm_tests -H -s 100385# KSM KSM_MERGE_TIME test with size of 100386CATEGORY="ksm" run_test ./ksm_tests -P -s 100387# KSM MADV_MERGEABLE test with 10 identical pages388CATEGORY="ksm" run_test ./ksm_tests -M -p 10389# KSM unmerge test390CATEGORY="ksm" run_test ./ksm_tests -U391# KSM test with 10 zero pages and use_zero_pages = 0392CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 0393# KSM test with 10 zero pages and use_zero_pages = 1394CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 1395# KSM test with 2 NUMA nodes and merge_across_nodes = 1396CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 1397# KSM test with 2 NUMA nodes and merge_across_nodes = 0398CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 0399 400CATEGORY="ksm" run_test ./ksm_functional_tests401 402# protection_keys tests403nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)404if [ -x ./protection_keys_32 ]405then406 CATEGORY="pkey" run_test ./protection_keys_32407fi408 409if [ -x ./protection_keys_64 ]410then411 CATEGORY="pkey" run_test ./protection_keys_64412fi413echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages414 415if [ -x ./soft-dirty ]416then417 CATEGORY="soft_dirty" run_test ./soft-dirty418fi419 420CATEGORY="pagemap" run_test ./pagemap_ioctl421 422# COW tests423CATEGORY="cow" run_test ./cow424 425CATEGORY="thp" run_test ./khugepaged426 427CATEGORY="thp" run_test ./khugepaged -s 2428 429CATEGORY="thp" run_test ./transhuge-stress -d 20430 431# Try to create XFS if not provided432if [ -z "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then433 if test_selected "thp"; then434 if grep xfs /proc/filesystems &>/dev/null; then435 XFS_IMG=$(mktemp /tmp/xfs_img_XXXXXX)436 SPLIT_HUGE_PAGE_TEST_XFS_PATH=$(mktemp -d /tmp/xfs_dir_XXXXXX)437 truncate -s 314572800 ${XFS_IMG}438 mkfs.xfs -q ${XFS_IMG}439 mount -o loop ${XFS_IMG} ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}440 MOUNTED_XFS=1441 fi442 fi443fi444 445CATEGORY="thp" run_test ./split_huge_page_test ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}446 447if [ -n "${MOUNTED_XFS}" ]; then448 umount ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}449 rmdir ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}450 rm -f ${XFS_IMG}451fi452 453CATEGORY="migration" run_test ./migration454 455CATEGORY="mkdirty" run_test ./mkdirty456 457CATEGORY="mdwe" run_test ./mdwe_test458 459echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix460echo "1..${count_total}" | tap_output461 462exit $exitcode463