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