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