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