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