xref: /linux/tools/testing/selftests/mm/run_vmtests.sh (revision 90c132f8379b788a0482dbeabc884d3ae5d82205)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Please run as root
4
5# IMPORTANT: If you add a new test CATEGORY please add a simple wrapper
6# script so kunit knows to run it, and add it to the list below.
7# If you do not YOUR TESTS WILL NOT RUN IN THE CI.
8
9# Kselftest framework requirement - SKIP code is 4.
10ksft_skip=4
11
12count_total=0
13count_pass=0
14count_fail=0
15count_skip=0
16exitcode=0
17
18usage() {
19	cat <<EOF
20usage: ${BASH_SOURCE[0]:-$0} [ options ]
21
22  -a: run all tests, including extra ones (other than destructive ones)
23  -t: specify specific categories to tests to run
24  -h: display this message
25  -n: disable TAP output
26  -d: run destructive tests
27
28The default behavior is to run required tests only.  If -a is specified,
29will run all tests.
30
31Alternatively, specific groups tests can be run by passing a string
32to the -t argument containing one or more of the following categories
33separated by spaces:
34- mmap
35	tests for mmap(2)
36- gup_test
37	tests for gup
38- userfaultfd
39	tests for  userfaultfd(2)
40- compaction
41	a test for the patch "Allow compaction of unevictable pages"
42- mlock
43	tests for mlock(2)
44- mremap
45	tests for mremap(2)
46- hugevm
47	tests for very large virtual address space
48- vmalloc
49	vmalloc smoke tests
50- hmm
51	hmm smoke tests
52- madv_guard
53	test madvise(2) MADV_GUARD_INSTALL and MADV_GUARD_REMOVE options
54- madv_populate
55	test memadvise(2) MADV_POPULATE_{READ,WRITE} options
56- memfd_secret
57	test memfd_secret(2)
58- process_mrelease
59	test process_mrelease(2)
60- ksm
61	ksm tests that do not require >=2 NUMA nodes
62- ksm_numa
63	ksm tests that require >=2 NUMA nodes
64- pkey
65	memory protection key tests
66- soft_dirty
67	test soft dirty page bit semantics
68- pagemap
69	test pagemap_scan IOCTL
70- pfnmap
71	tests for VM_PFNMAP handling
72- process_madv
73	test for process_madv
74- cow
75	test copy-on-write semantics
76- thp
77	test transparent huge pages
78- hugetlb
79	test hugetlbfs huge pages
80- migration
81	invoke move_pages(2) to exercise the migration entry code
82	paths in the kernel
83- mkdirty
84	test handling of code that might set PTE/PMD dirty in
85	read-only VMAs
86- mdwe
87	test prctl(PR_SET_MDWE, ...)
88- page_frag
89	test handling of page fragment allocation and freeing
90- vma_merge
91	test VMA merge cases behave as expected
92- rmap
93	test rmap behaves as expected
94- memory-failure
95	test memory-failure behaves as expected
96
97example: ./run_vmtests.sh -t "hmm mmap ksm"
98EOF
99	exit 0
100}
101
102RUN_ALL=false
103RUN_DESTRUCTIVE=false
104TAP_PREFIX="# "
105
106while getopts "aht:nd" OPT; do
107	case ${OPT} in
108		"a") RUN_ALL=true ;;
109		"h") usage ;;
110		"t") VM_SELFTEST_ITEMS=${OPTARG} ;;
111		"n") TAP_PREFIX= ;;
112		"d") RUN_DESTRUCTIVE=true ;;
113	esac
114done
115shift $((OPTIND -1))
116
117# default behavior: run all tests
118VM_SELFTEST_ITEMS=${VM_SELFTEST_ITEMS:-default}
119
120test_selected() {
121	if [ "$VM_SELFTEST_ITEMS" == "default" ]; then
122		# If no VM_SELFTEST_ITEMS are specified, run all tests
123		return 0
124	fi
125	# If test selected argument is one of the test items
126	if [[ " ${VM_SELFTEST_ITEMS[*]} " =~ " ${1} " ]]; then
127	        return 0
128	else
129	        return 1
130	fi
131}
132
133run_gup_matrix() {
134    # -t: thp=on, -T: thp=off, -H: hugetlb=on
135    local hugetlb_mb=256
136
137    for huge in -t -T "-H -m $hugetlb_mb"; do
138        # -u: gup-fast, -U: gup-basic, -a: pin-fast, -b: pin-basic, -L: pin-longterm
139        for test_cmd in -u -U -a -b -L; do
140            # -w: write=1, -W: write=0
141            for write in -w -W; do
142                # -S: shared
143                for share in -S " "; do
144                    # -n: How many pages to fetch together?  512 is special
145                    # because it's default thp size (or 2M on x86), 123 to
146                    # just test partial gup when hit a huge in whatever form
147                    for num in "-n 1" "-n 512" "-n 123" "-n -1"; do
148                        CATEGORY="gup_test" run_test ./gup_test \
149                                $huge $test_cmd $write $share $num
150                    done
151                done
152            done
153        done
154    done
155}
156
157# filter 64bit architectures
158ARCH64STR="arm64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sparc64 x86_64"
159if [ -z "$ARCH" ]; then
160	ARCH=$(uname -m 2>/dev/null | sed -e 's/aarch64.*/arm64/')
161fi
162VADDR64=0
163echo "$ARCH64STR" | grep "$ARCH" &>/dev/null && VADDR64=1
164
165tap_prefix() {
166	sed -e "s/^/${TAP_PREFIX}/"
167}
168
169tap_output() {
170	if [[ ! -z "$TAP_PREFIX" ]]; then
171		read str
172		echo $str
173	fi
174}
175
176pretty_name() {
177	echo "$*" | sed -e 's/^\(bash \)\?\.\///'
178}
179
180# Usage: run_test [test binary] [arbitrary test arguments...]
181run_test() {
182	if test_selected ${CATEGORY}; then
183		local skip=0
184		local LOADED_HWPOISON_INJECT_MOD=0
185
186		# On memory constrainted systems some tests can fail to allocate hugepages.
187		# perform some cleanup before the test for a higher success rate.
188		if [ ${CATEGORY} == "thp" -o ${CATEGORY} == "hugetlb" ]; then
189			mem_kb=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
190			mem_Mb=$((mem_kb / 1024))
191
192			if (( $mem_Mb < 256 )); then
193				echo 3 > /proc/sys/vm/drop_caches
194				sleep 2
195				echo 1 > /proc/sys/vm/compact_memory
196				sleep 2
197			fi
198		fi
199
200		# Ensure hwpoison_inject is available for memory-failure tests
201		if [ "${CATEGORY}" = "memory-failure" ]; then
202			# Try to load hwpoison_inject if not present.
203			HWPOISON_DIR=/sys/kernel/debug/hwpoison/
204			if [ ! -d "$HWPOISON_DIR" ]; then
205				if ! modprobe -n hwpoison_inject > /dev/null 2>&1; then
206					echo "Module hwpoison_inject not found, skipping..." \
207						| tap_prefix
208					skip=1
209				else
210					modprobe hwpoison_inject > /dev/null 2>&1
211					LOADED_HWPOISON_INJECT_MOD=1
212					if [ ! -d "$HWPOISON_DIR" ]; then
213						echo "hwpoison debugfs interface not present" \
214							| tap_prefix
215						skip=1
216					fi
217				fi
218			fi
219
220		fi
221
222		local test=$(pretty_name "$*")
223		local title="running $*"
224		local sep=$(echo -n "$title" | tr "[:graph:][:space:]" -)
225		printf "%s\n%s\n%s\n" "$sep" "$title" "$sep" | tap_prefix
226
227		if [ $skip -eq 1 ]; then
228			local ret=$ksft_skip
229		else
230			("$@" 2>&1) | tap_prefix
231			local ret=${PIPESTATUS[0]}
232		fi
233
234		# Unload hwpoison_inject if we loaded it
235		if [ "${LOADED_HWPOISON_INJECT_MOD}" = "1" ]; then
236			modprobe -r hwpoison_inject > /dev/null 2>&1
237		fi
238
239		count_total=$(( count_total + 1 ))
240		if [ $ret -eq 0 ]; then
241			count_pass=$(( count_pass + 1 ))
242			echo "[PASS]" | tap_prefix
243			echo "ok ${count_total} ${test}" | tap_output
244		elif [ $ret -eq $ksft_skip ]; then
245			count_skip=$(( count_skip + 1 ))
246			echo "[SKIP]" | tap_prefix
247			echo "ok ${count_total} ${test} # SKIP" | tap_output
248			if [ $exitcode -eq 0 ]; then
249				exitcode=$ksft_skip
250			fi
251		else
252			count_fail=$(( count_fail + 1 ))
253			echo "[FAIL]" | tap_prefix
254			echo "not ok ${count_total} ${test} # exit=$ret" | tap_output
255			exitcode=1
256		fi
257	fi # test_selected
258}
259
260echo "TAP version 13" | tap_output
261
262CATEGORY="hugetlb" run_test ./hugetlb-mmap
263CATEGORY="hugetlb" run_test ./hugetlb-shm
264CATEGORY="hugetlb" run_test ./hugetlb-mremap
265CATEGORY="hugetlb" run_test ./hugetlb-vmemmap
266CATEGORY="hugetlb" run_test ./hugetlb-madvise
267CATEGORY="hugetlb" run_test ./hugetlb_dio
268CATEGORY="hugetlb" run_test ./hugetlb_fault_after_madv
269CATEGORY="hugetlb" run_test ./hugetlb_madv_vs_map
270
271if test_selected "hugetlb"; then
272	echo "NOTE: These hugetlb tests provide minimal coverage.  Use"	  | tap_prefix
273	echo "      https://github.com/libhugetlbfs/libhugetlbfs.git for" | tap_prefix
274	echo "      hugetlb regression testing."			  | tap_prefix
275fi
276
277CATEGORY="mmap" run_test ./map_fixed_noreplace
278
279if $RUN_ALL; then
280    run_gup_matrix
281else
282    # get_user_pages_fast() benchmark
283    CATEGORY="gup_test" run_test ./gup_test -u -n 1
284    CATEGORY="gup_test" run_test ./gup_test -u -n -1
285    # pin_user_pages_fast() benchmark
286    CATEGORY="gup_test" run_test ./gup_test -a -n 1
287    CATEGORY="gup_test" run_test ./gup_test -a -n -1
288fi
289# Dump pages 0, 19, and 4096, using pin_user_pages:
290CATEGORY="gup_test" run_test ./gup_test -ct -F 0x1 0 19 0x1000
291CATEGORY="gup_test" run_test ./gup_longterm
292
293CATEGORY="userfaultfd" run_test ./uffd-unit-tests
294uffd_stress_bin=./uffd-stress
295CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16
296CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb 128 32
297CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private 128 32
298CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16
299CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem-private 20 16
300CATEGORY="userfaultfd" run_test ./uffd-wp-mremap
301
302CATEGORY="compaction" run_test ./compaction_test
303
304if command -v sudo &> /dev/null && sudo -u nobody ls ./on-fault-limit >/dev/null;
305then
306	CATEGORY="mlock" run_test sudo -u nobody ./on-fault-limit
307else
308	echo "# SKIP ./on-fault-limit"
309fi
310
311CATEGORY="mmap" run_test ./map_populate
312CATEGORY="mmap" run_test ./droppable
313
314CATEGORY="mlock" run_test ./mlock-random-test
315
316CATEGORY="mlock" run_test ./mlock2-tests
317
318CATEGORY="process_mrelease" run_test ./mrelease_test
319
320CATEGORY="mremap" run_test ./mremap_test
321
322CATEGORY="hugetlb" run_test ./thuge-gen
323CATEGORY="hugetlb" run_test ./charge_reserved_hugetlb.sh -cgroup-v2
324CATEGORY="hugetlb" run_test ./hugetlb_reparenting_test.sh -cgroup-v2
325
326if $RUN_DESTRUCTIVE; then
327enable_soft_offline=$(cat /proc/sys/vm/enable_soft_offline)
328CATEGORY="hugetlb" run_test ./hugetlb-soft-offline
329echo "$enable_soft_offline" > /proc/sys/vm/enable_soft_offline
330CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison
331fi
332
333if [ $VADDR64 -ne 0 ]; then
334	# va high address boundary switch test
335	CATEGORY="hugevm" run_test bash ./va_high_addr_switch.sh
336fi # VADDR64
337
338# vmalloc stability smoke test
339CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke
340
341CATEGORY="mremap" run_test ./mremap_dontunmap
342
343CATEGORY="hmm" run_test bash ./test_hmm.sh smoke
344
345# MADV_GUARD_INSTALL and MADV_GUARD_REMOVE tests
346CATEGORY="madv_guard" run_test ./guard-regions
347
348# MADV_POPULATE_READ and MADV_POPULATE_WRITE tests
349CATEGORY="madv_populate" run_test ./madv_populate
350
351# PROCESS_MADV test
352CATEGORY="process_madv" run_test ./process_madv
353
354CATEGORY="vma_merge" run_test ./merge
355
356if [ -x ./memfd_secret ]
357then
358if [ -f /proc/sys/kernel/yama/ptrace_scope ]; then
359	(echo 0 > /proc/sys/kernel/yama/ptrace_scope 2>&1) | tap_prefix
360fi
361CATEGORY="memfd_secret" run_test ./memfd_secret
362fi
363
364# KSM KSM_MERGE_TIME_HUGE_PAGES test with size of 100
365CATEGORY="ksm" run_test ./ksm_tests -H -s 100
366# KSM KSM_MERGE_TIME test with size of 100
367CATEGORY="ksm" run_test ./ksm_tests -P -s 100
368# KSM MADV_MERGEABLE test with 10 identical pages
369CATEGORY="ksm" run_test ./ksm_tests -M -p 10
370# KSM unmerge test
371CATEGORY="ksm" run_test ./ksm_tests -U
372# KSM test with 10 zero pages and use_zero_pages = 0
373CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 0
374# KSM test with 10 zero pages and use_zero_pages = 1
375CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 1
376# KSM test with 2 NUMA nodes and merge_across_nodes = 1
377CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 1
378# KSM test with 2 NUMA nodes and merge_across_nodes = 0
379CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 0
380
381CATEGORY="ksm" run_test ./ksm_functional_tests
382
383# protection_keys tests
384if [ -x ./protection_keys_32 ]
385then
386	CATEGORY="pkey" run_test ./protection_keys_32
387fi
388
389if [ -x ./protection_keys_64 ]
390then
391	CATEGORY="pkey" run_test ./protection_keys_64
392fi
393
394if [ -x ./soft-dirty ]
395then
396	CATEGORY="soft_dirty" run_test ./soft-dirty
397fi
398
399CATEGORY="pagemap" run_test ./pagemap_ioctl
400
401CATEGORY="pfnmap" run_test ./pfnmap
402
403# COW tests
404CATEGORY="cow" run_test ./cow
405
406CATEGORY="thp" run_test ./khugepaged
407
408CATEGORY="thp" run_test ./khugepaged -s 2
409
410CATEGORY="thp" run_test ./khugepaged all:shmem
411
412CATEGORY="thp" run_test ./khugepaged -s 4 all:shmem
413
414# Try to create XFS if not provided
415if [ -z "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
416    if test_selected "thp"; then
417	if grep xfs /proc/filesystems &>/dev/null; then
418	    XFS_IMG=$(mktemp /tmp/xfs_img_XXXXXX)
419	    SPLIT_HUGE_PAGE_TEST_XFS_PATH=$(mktemp -d /tmp/xfs_dir_XXXXXX)
420	    truncate -s 314572800 ${XFS_IMG}
421	    mkfs.xfs -q ${XFS_IMG}
422	    mount -o loop ${XFS_IMG} ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
423	    MOUNTED_XFS=1
424	fi
425    fi
426fi
427
428if [ -n "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
429CATEGORY="thp" run_test ./khugepaged all:file ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
430elif test_selected thp; then
431	count_total=$(( count_total + 1 ))
432	count_skip=$(( count_skip + 1 ))
433	echo "[SKIP] ./khugepaged all:file" | tap_prefix
434fi
435
436CATEGORY="thp" run_test ./split_huge_page_test ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
437
438if [ -n "${MOUNTED_XFS}" ]; then
439    umount ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
440    rmdir ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
441    rm -f ${XFS_IMG}
442fi
443
444CATEGORY="thp" run_test ./transhuge-stress -d 20
445
446CATEGORY="thp" run_test ./folio_split_race_test
447
448CATEGORY="migration" run_test ./migration
449
450CATEGORY="mkdirty" run_test ./mkdirty
451
452CATEGORY="mdwe" run_test ./mdwe_test
453
454CATEGORY="page_frag" run_test ./test_page_frag.sh smoke
455
456CATEGORY="page_frag" run_test ./test_page_frag.sh aligned
457
458CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned
459
460CATEGORY="rmap" run_test ./rmap
461
462CATEGORY="memory-failure" run_test ./memory-failure
463
464echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix
465echo "1..${count_total}" | tap_output
466
467exit $exitcode
468