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