run_vmtests.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Please run as root
  4. # Kselftest framework requirement - SKIP code is 4.
  5. ksft_skip=4
  6. count_total=0
  7. count_pass=0
  8. count_fail=0
  9. count_skip=0
  10. exitcode=0
  11. usage() {
  12. cat <<EOF
  13. usage: ${BASH_SOURCE[0]:-$0} [ options ]
  14. -a: run all tests, including extra ones (other than destructive ones)
  15. -t: specify specific categories to tests to run
  16. -h: display this message
  17. -n: disable TAP output
  18. -d: run destructive tests
  19. The default behavior is to run required tests only. If -a is specified,
  20. will run all tests.
  21. Alternatively, specific groups tests can be run by passing a string
  22. to the -t argument containing one or more of the following categories
  23. separated by spaces:
  24. - mmap
  25. tests for mmap(2)
  26. - gup_test
  27. tests for gup
  28. - userfaultfd
  29. tests for userfaultfd(2)
  30. - compaction
  31. a test for the patch "Allow compaction of unevictable pages"
  32. - mlock
  33. tests for mlock(2)
  34. - mremap
  35. tests for mremap(2)
  36. - hugevm
  37. tests for very large virtual address space
  38. - vmalloc
  39. vmalloc smoke tests
  40. - hmm
  41. hmm smoke tests
  42. - madv_populate
  43. test memadvise(2) MADV_POPULATE_{READ,WRITE} options
  44. - memfd_secret
  45. test memfd_secret(2)
  46. - process_mrelease
  47. test process_mrelease(2)
  48. - ksm
  49. ksm tests that do not require >=2 NUMA nodes
  50. - ksm_numa
  51. ksm tests that require >=2 NUMA nodes
  52. - pkey
  53. memory protection key tests
  54. - soft_dirty
  55. test soft dirty page bit semantics
  56. - pagemap
  57. test pagemap_scan IOCTL
  58. - cow
  59. test copy-on-write semantics
  60. - thp
  61. test transparent huge pages
  62. - hugetlb
  63. test hugetlbfs huge pages
  64. - migration
  65. invoke move_pages(2) to exercise the migration entry code
  66. paths in the kernel
  67. - mkdirty
  68. test handling of code that might set PTE/PMD dirty in
  69. read-only VMAs
  70. - mdwe
  71. test prctl(PR_SET_MDWE, ...)
  72. example: ./run_vmtests.sh -t "hmm mmap ksm"
  73. EOF
  74. exit 0
  75. }
  76. RUN_ALL=false
  77. RUN_DESTRUCTIVE=false
  78. TAP_PREFIX="# "
  79. while getopts "aht:n" OPT; do
  80. case ${OPT} in
  81. "a") RUN_ALL=true ;;
  82. "h") usage ;;
  83. "t") VM_SELFTEST_ITEMS=${OPTARG} ;;
  84. "n") TAP_PREFIX= ;;
  85. "d") RUN_DESTRUCTIVE=true ;;
  86. esac
  87. done
  88. shift $((OPTIND -1))
  89. # default behavior: run all tests
  90. VM_SELFTEST_ITEMS=${VM_SELFTEST_ITEMS:-default}
  91. test_selected() {
  92. if [ "$VM_SELFTEST_ITEMS" == "default" ]; then
  93. # If no VM_SELFTEST_ITEMS are specified, run all tests
  94. return 0
  95. fi
  96. # If test selected argument is one of the test items
  97. if [[ " ${VM_SELFTEST_ITEMS[*]} " =~ " ${1} " ]]; then
  98. return 0
  99. else
  100. return 1
  101. fi
  102. }
  103. run_gup_matrix() {
  104. # -t: thp=on, -T: thp=off, -H: hugetlb=on
  105. local hugetlb_mb=$(( needmem_KB / 1024 ))
  106. for huge in -t -T "-H -m $hugetlb_mb"; do
  107. # -u: gup-fast, -U: gup-basic, -a: pin-fast, -b: pin-basic, -L: pin-longterm
  108. for test_cmd in -u -U -a -b -L; do
  109. # -w: write=1, -W: write=0
  110. for write in -w -W; do
  111. # -S: shared
  112. for share in -S " "; do
  113. # -n: How many pages to fetch together? 512 is special
  114. # because it's default thp size (or 2M on x86), 123 to
  115. # just test partial gup when hit a huge in whatever form
  116. for num in "-n 1" "-n 512" "-n 123"; do
  117. CATEGORY="gup_test" run_test ./gup_test \
  118. $huge $test_cmd $write $share $num
  119. done
  120. done
  121. done
  122. done
  123. done
  124. }
  125. # get huge pagesize and freepages from /proc/meminfo
  126. while read -r name size unit; do
  127. if [ "$name" = "HugePages_Free:" ]; then
  128. freepgs="$size"
  129. fi
  130. if [ "$name" = "Hugepagesize:" ]; then
  131. hpgsize_KB="$size"
  132. fi
  133. done < /proc/meminfo
  134. # Simple hugetlbfs tests have a hardcoded minimum requirement of
  135. # huge pages totaling 256MB (262144KB) in size. The userfaultfd
  136. # hugetlb test requires a minimum of 2 * nr_cpus huge pages. Take
  137. # both of these requirements into account and attempt to increase
  138. # number of huge pages available.
  139. nr_cpus=$(nproc)
  140. uffd_min_KB=$((hpgsize_KB * nr_cpus * 2))
  141. hugetlb_min_KB=$((256 * 1024))
  142. if [[ $uffd_min_KB -gt $hugetlb_min_KB ]]; then
  143. needmem_KB=$uffd_min_KB
  144. else
  145. needmem_KB=$hugetlb_min_KB
  146. fi
  147. # set proper nr_hugepages
  148. if [ -n "$freepgs" ] && [ -n "$hpgsize_KB" ]; then
  149. nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)
  150. needpgs=$((needmem_KB / hpgsize_KB))
  151. tries=2
  152. while [ "$tries" -gt 0 ] && [ "$freepgs" -lt "$needpgs" ]; do
  153. lackpgs=$((needpgs - freepgs))
  154. echo 3 > /proc/sys/vm/drop_caches
  155. if ! echo $((lackpgs + nr_hugepgs)) > /proc/sys/vm/nr_hugepages; then
  156. echo "Please run this test as root"
  157. exit $ksft_skip
  158. fi
  159. while read -r name size unit; do
  160. if [ "$name" = "HugePages_Free:" ]; then
  161. freepgs=$size
  162. fi
  163. done < /proc/meminfo
  164. tries=$((tries - 1))
  165. done
  166. if [ "$freepgs" -lt "$needpgs" ]; then
  167. printf "Not enough huge pages available (%d < %d)\n" \
  168. "$freepgs" "$needpgs"
  169. fi
  170. else
  171. echo "no hugetlbfs support in kernel?"
  172. exit 1
  173. fi
  174. # filter 64bit architectures
  175. ARCH64STR="arm64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sparc64 x86_64"
  176. if [ -z "$ARCH" ]; then
  177. ARCH=$(uname -m 2>/dev/null | sed -e 's/aarch64.*/arm64/')
  178. fi
  179. VADDR64=0
  180. echo "$ARCH64STR" | grep "$ARCH" &>/dev/null && VADDR64=1
  181. tap_prefix() {
  182. sed -e "s/^/${TAP_PREFIX}/"
  183. }
  184. tap_output() {
  185. if [[ ! -z "$TAP_PREFIX" ]]; then
  186. read str
  187. echo $str
  188. fi
  189. }
  190. pretty_name() {
  191. echo "$*" | sed -e 's/^\(bash \)\?\.\///'
  192. }
  193. # Usage: run_test [test binary] [arbitrary test arguments...]
  194. run_test() {
  195. if test_selected ${CATEGORY}; then
  196. # On memory constrainted systems some tests can fail to allocate hugepages.
  197. # perform some cleanup before the test for a higher success rate.
  198. if [ ${CATEGORY} == "thp" ] | [ ${CATEGORY} == "hugetlb" ]; then
  199. echo 3 > /proc/sys/vm/drop_caches
  200. sleep 2
  201. echo 1 > /proc/sys/vm/compact_memory
  202. sleep 2
  203. fi
  204. local test=$(pretty_name "$*")
  205. local title="running $*"
  206. local sep=$(echo -n "$title" | tr "[:graph:][:space:]" -)
  207. printf "%s\n%s\n%s\n" "$sep" "$title" "$sep" | tap_prefix
  208. ("$@" 2>&1) | tap_prefix
  209. local ret=${PIPESTATUS[0]}
  210. count_total=$(( count_total + 1 ))
  211. if [ $ret -eq 0 ]; then
  212. count_pass=$(( count_pass + 1 ))
  213. echo "[PASS]" | tap_prefix
  214. echo "ok ${count_total} ${test}" | tap_output
  215. elif [ $ret -eq $ksft_skip ]; then
  216. count_skip=$(( count_skip + 1 ))
  217. echo "[SKIP]" | tap_prefix
  218. echo "ok ${count_total} ${test} # SKIP" | tap_output
  219. exitcode=$ksft_skip
  220. else
  221. count_fail=$(( count_fail + 1 ))
  222. echo "[FAIL]" | tap_prefix
  223. echo "not ok ${count_total} ${test} # exit=$ret" | tap_output
  224. exitcode=1
  225. fi
  226. fi # test_selected
  227. }
  228. echo "TAP version 13" | tap_output
  229. CATEGORY="hugetlb" run_test ./hugepage-mmap
  230. shmmax=$(cat /proc/sys/kernel/shmmax)
  231. shmall=$(cat /proc/sys/kernel/shmall)
  232. echo 268435456 > /proc/sys/kernel/shmmax
  233. echo 4194304 > /proc/sys/kernel/shmall
  234. CATEGORY="hugetlb" run_test ./hugepage-shm
  235. echo "$shmmax" > /proc/sys/kernel/shmmax
  236. echo "$shmall" > /proc/sys/kernel/shmall
  237. CATEGORY="hugetlb" run_test ./map_hugetlb
  238. CATEGORY="hugetlb" run_test ./hugepage-mremap
  239. CATEGORY="hugetlb" run_test ./hugepage-vmemmap
  240. CATEGORY="hugetlb" run_test ./hugetlb-madvise
  241. CATEGORY="hugetlb" run_test ./hugetlb_dio
  242. nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages)
  243. # For this test, we need one and just one huge page
  244. echo 1 > /proc/sys/vm/nr_hugepages
  245. CATEGORY="hugetlb" run_test ./hugetlb_fault_after_madv
  246. CATEGORY="hugetlb" run_test ./hugetlb_madv_vs_map
  247. # Restore the previous number of huge pages, since further tests rely on it
  248. echo "$nr_hugepages_tmp" > /proc/sys/vm/nr_hugepages
  249. if test_selected "hugetlb"; then
  250. echo "NOTE: These hugetlb tests provide minimal coverage. Use" | tap_prefix
  251. echo " https://github.com/libhugetlbfs/libhugetlbfs.git for" | tap_prefix
  252. echo " hugetlb regression testing." | tap_prefix
  253. fi
  254. CATEGORY="mmap" run_test ./map_fixed_noreplace
  255. if $RUN_ALL; then
  256. run_gup_matrix
  257. else
  258. # get_user_pages_fast() benchmark
  259. CATEGORY="gup_test" run_test ./gup_test -u
  260. # pin_user_pages_fast() benchmark
  261. CATEGORY="gup_test" run_test ./gup_test -a
  262. fi
  263. # Dump pages 0, 19, and 4096, using pin_user_pages:
  264. CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000
  265. CATEGORY="gup_test" run_test ./gup_longterm
  266. CATEGORY="userfaultfd" run_test ./uffd-unit-tests
  267. uffd_stress_bin=./uffd-stress
  268. CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16
  269. # Hugetlb tests require source and destination huge pages. Pass in half
  270. # the size of the free pages we have, which is used for *each*.
  271. # uffd-stress expects a region expressed in MiB, so we adjust
  272. # half_ufd_size_MB accordingly.
  273. half_ufd_size_MB=$(((freepgs * hpgsize_KB) / 1024 / 2))
  274. CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb "$half_ufd_size_MB" 32
  275. CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private "$half_ufd_size_MB" 32
  276. CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16
  277. CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem-private 20 16
  278. #cleanup
  279. echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages
  280. CATEGORY="compaction" run_test ./compaction_test
  281. if command -v sudo &> /dev/null;
  282. then
  283. CATEGORY="mlock" run_test sudo -u nobody ./on-fault-limit
  284. else
  285. echo "# SKIP ./on-fault-limit"
  286. fi
  287. CATEGORY="mmap" run_test ./map_populate
  288. CATEGORY="mlock" run_test ./mlock-random-test
  289. CATEGORY="mlock" run_test ./mlock2-tests
  290. CATEGORY="process_mrelease" run_test ./mrelease_test
  291. CATEGORY="mremap" run_test ./mremap_test
  292. CATEGORY="hugetlb" run_test ./thuge-gen
  293. CATEGORY="hugetlb" run_test ./charge_reserved_hugetlb.sh -cgroup-v2
  294. CATEGORY="hugetlb" run_test ./hugetlb_reparenting_test.sh -cgroup-v2
  295. if $RUN_DESTRUCTIVE; then
  296. nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages)
  297. enable_soft_offline=$(cat /proc/sys/vm/enable_soft_offline)
  298. echo 8 > /proc/sys/vm/nr_hugepages
  299. CATEGORY="hugetlb" run_test ./hugetlb-soft-offline
  300. echo "$nr_hugepages_tmp" > /proc/sys/vm/nr_hugepages
  301. echo "$enable_soft_offline" > /proc/sys/vm/enable_soft_offline
  302. CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison
  303. fi
  304. if [ $VADDR64 -ne 0 ]; then
  305. # set overcommit_policy as OVERCOMMIT_ALWAYS so that kernel
  306. # allows high virtual address allocation requests independent
  307. # of platform's physical memory.
  308. prev_policy=$(cat /proc/sys/vm/overcommit_memory)
  309. echo 1 > /proc/sys/vm/overcommit_memory
  310. CATEGORY="hugevm" run_test ./virtual_address_range
  311. echo $prev_policy > /proc/sys/vm/overcommit_memory
  312. # va high address boundary switch test
  313. ARCH_ARM64="arm64"
  314. prev_nr_hugepages=$(cat /proc/sys/vm/nr_hugepages)
  315. if [ "$ARCH" == "$ARCH_ARM64" ]; then
  316. echo 6 > /proc/sys/vm/nr_hugepages
  317. fi
  318. CATEGORY="hugevm" run_test bash ./va_high_addr_switch.sh
  319. if [ "$ARCH" == "$ARCH_ARM64" ]; then
  320. echo $prev_nr_hugepages > /proc/sys/vm/nr_hugepages
  321. fi
  322. fi # VADDR64
  323. # vmalloc stability smoke test
  324. CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke
  325. CATEGORY="mremap" run_test ./mremap_dontunmap
  326. CATEGORY="hmm" run_test bash ./test_hmm.sh smoke
  327. # MADV_POPULATE_READ and MADV_POPULATE_WRITE tests
  328. CATEGORY="madv_populate" run_test ./madv_populate
  329. if [ -x ./memfd_secret ]
  330. then
  331. (echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope 2>&1) | tap_prefix
  332. CATEGORY="memfd_secret" run_test ./memfd_secret
  333. fi
  334. # KSM KSM_MERGE_TIME_HUGE_PAGES test with size of 100
  335. CATEGORY="ksm" run_test ./ksm_tests -H -s 100
  336. # KSM KSM_MERGE_TIME test with size of 100
  337. CATEGORY="ksm" run_test ./ksm_tests -P -s 100
  338. # KSM MADV_MERGEABLE test with 10 identical pages
  339. CATEGORY="ksm" run_test ./ksm_tests -M -p 10
  340. # KSM unmerge test
  341. CATEGORY="ksm" run_test ./ksm_tests -U
  342. # KSM test with 10 zero pages and use_zero_pages = 0
  343. CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 0
  344. # KSM test with 10 zero pages and use_zero_pages = 1
  345. CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 1
  346. # KSM test with 2 NUMA nodes and merge_across_nodes = 1
  347. CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 1
  348. # KSM test with 2 NUMA nodes and merge_across_nodes = 0
  349. CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 0
  350. CATEGORY="ksm" run_test ./ksm_functional_tests
  351. # protection_keys tests
  352. nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)
  353. if [ -x ./protection_keys_32 ]
  354. then
  355. CATEGORY="pkey" run_test ./protection_keys_32
  356. fi
  357. if [ -x ./protection_keys_64 ]
  358. then
  359. CATEGORY="pkey" run_test ./protection_keys_64
  360. fi
  361. echo "$nr_hugepgs" > /proc/sys/vm/nr_hugepages
  362. if [ -x ./soft-dirty ]
  363. then
  364. CATEGORY="soft_dirty" run_test ./soft-dirty
  365. fi
  366. CATEGORY="pagemap" run_test ./pagemap_ioctl
  367. # COW tests
  368. CATEGORY="cow" run_test ./cow
  369. CATEGORY="thp" run_test ./khugepaged
  370. CATEGORY="thp" run_test ./khugepaged -s 2
  371. CATEGORY="thp" run_test ./transhuge-stress -d 20
  372. # Try to create XFS if not provided
  373. if [ -z "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
  374. if test_selected "thp"; then
  375. if grep xfs /proc/filesystems &>/dev/null; then
  376. XFS_IMG=$(mktemp /tmp/xfs_img_XXXXXX)
  377. SPLIT_HUGE_PAGE_TEST_XFS_PATH=$(mktemp -d /tmp/xfs_dir_XXXXXX)
  378. truncate -s 314572800 ${XFS_IMG}
  379. mkfs.xfs -q ${XFS_IMG}
  380. mount -o loop ${XFS_IMG} ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
  381. MOUNTED_XFS=1
  382. fi
  383. fi
  384. fi
  385. CATEGORY="thp" run_test ./split_huge_page_test ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
  386. if [ -n "${MOUNTED_XFS}" ]; then
  387. umount ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
  388. rmdir ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
  389. rm -f ${XFS_IMG}
  390. fi
  391. CATEGORY="migration" run_test ./migration
  392. CATEGORY="mkdirty" run_test ./mkdirty
  393. CATEGORY="mdwe" run_test ./mdwe_test
  394. echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix
  395. echo "1..${count_total}" | tap_output
  396. exit $exitcode