xref: /linux/tools/testing/selftests/mm/run_vmtests.sh (revision b79d51e92e5f480b0d91ca272b4081493563699a)
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}
100
101RUN_ALL=false
102RUN_DESTRUCTIVE=false
103TAP_PREFIX="# "
104
105VM_SELFTEST_ITEMS="default"
106
107while getopts "aht:nd" OPT; do
108	case ${OPT} in
109		"a") RUN_ALL=true ;;
110		"h") usage; exit 0 ;;
111		"t") VM_SELFTEST_ITEMS=${OPTARG} ;;
112		"n") TAP_PREFIX= ;;
113		"d") RUN_DESTRUCTIVE=true ;;
114		"?") exit 1 ;;
115	esac
116done
117shift $((OPTIND -1))
118
119# Normalize whitespace so validation and test_selected() use the same names.
120read -r -a selected_categories <<< "${VM_SELFTEST_ITEMS//$'\n'/ }"
121VM_SELFTEST_ITEMS="${selected_categories[*]}"
122if [ -z "$VM_SELFTEST_ITEMS" ]; then
123	echo "No test categories specified" >&2
124	exit 1
125fi
126
127if [ "$VM_SELFTEST_ITEMS" != "default" ]; then
128	# Keep the documented category list as the source of valid names.
129	valid_categories=$(usage | sed -n 's/^- //p')
130	for category in "${selected_categories[@]}"; do
131		if ! grep -Fxq -- "$category" <<< "$valid_categories"; then
132			echo "Unknown test category: $category" >&2
133			exit 1
134		fi
135	done
136fi
137
138test_selected() {
139	if [ "$VM_SELFTEST_ITEMS" == "default" ]; then
140		# If no VM_SELFTEST_ITEMS are specified, run all tests
141		return 0
142	fi
143	# If test selected argument is one of the test items
144	if [[ " ${VM_SELFTEST_ITEMS[*]} " =~ " ${1} " ]]; then
145	        return 0
146	else
147	        return 1
148	fi
149}
150
151# filter 64bit architectures
152ARCH64STR="arm64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sparc64 x86_64"
153if [ -z "$ARCH" ]; then
154	ARCH=$(uname -m 2>/dev/null | sed -e 's/aarch64.*/arm64/')
155fi
156VADDR64=0
157echo "$ARCH64STR" | grep "$ARCH" &>/dev/null && VADDR64=1
158
159tap_prefix() {
160	sed -e "s/^/${TAP_PREFIX}/"
161}
162
163tap_output() {
164	if [[ ! -z "$TAP_PREFIX" ]]; then
165		read str
166		echo $str
167	fi
168}
169
170pretty_name() {
171	echo "$*" | sed -e 's/^\(bash \)\?\.\///'
172}
173
174# Usage: run_test [test binary] [arbitrary test arguments...]
175run_test() {
176	if test_selected ${CATEGORY}; then
177		local skip=0
178		local LOADED_HWPOISON_INJECT_MOD=0
179
180		# On memory constrainted systems some tests can fail to allocate hugepages.
181		# perform some cleanup before the test for a higher success rate.
182		if [ ${CATEGORY} == "thp" -o ${CATEGORY} == "hugetlb" ]; then
183			mem_kb=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
184			mem_Mb=$((mem_kb / 1024))
185
186			if (( $mem_Mb < 256 )); then
187				echo 3 > /proc/sys/vm/drop_caches
188				sleep 2
189				echo 1 > /proc/sys/vm/compact_memory
190				sleep 2
191			fi
192		fi
193
194		# Ensure hwpoison_inject is available for memory-failure tests
195		if [ "${CATEGORY}" = "memory-failure" ]; then
196			# Try to load hwpoison_inject if not present.
197			HWPOISON_DIR=/sys/kernel/debug/hwpoison/
198			if [ ! -d "$HWPOISON_DIR" ]; then
199				if ! modprobe -n hwpoison_inject > /dev/null 2>&1; then
200					echo "Module hwpoison_inject not found, skipping..." \
201						| tap_prefix
202					skip=1
203				else
204					modprobe hwpoison_inject > /dev/null 2>&1
205					LOADED_HWPOISON_INJECT_MOD=1
206					if [ ! -d "$HWPOISON_DIR" ]; then
207						echo "hwpoison debugfs interface not present" \
208							| tap_prefix
209						skip=1
210					fi
211				fi
212			fi
213
214		fi
215
216		local test=$(pretty_name "$*")
217		local title="running $*"
218		local sep=$(echo -n "$title" | tr "[:graph:][:space:]" -)
219		printf "%s\n%s\n%s\n" "$sep" "$title" "$sep" | tap_prefix
220
221		if [ $skip -eq 1 ]; then
222			local ret=$ksft_skip
223		else
224			("$@" 2>&1) | tap_prefix
225			local ret=${PIPESTATUS[0]}
226		fi
227
228		# Unload hwpoison_inject if we loaded it
229		if [ "${LOADED_HWPOISON_INJECT_MOD}" = "1" ]; then
230			modprobe -r hwpoison_inject > /dev/null 2>&1
231		fi
232
233		count_total=$(( count_total + 1 ))
234		if [ $ret -eq 0 ]; then
235			count_pass=$(( count_pass + 1 ))
236			echo "[PASS]" | tap_prefix
237			echo "ok ${count_total} ${test}" | tap_output
238		elif [ $ret -eq $ksft_skip ]; then
239			count_skip=$(( count_skip + 1 ))
240			echo "[SKIP]" | tap_prefix
241			echo "ok ${count_total} ${test} # SKIP" | tap_output
242			if [ $exitcode -eq 0 ]; then
243				exitcode=$ksft_skip
244			fi
245		else
246			count_fail=$(( count_fail + 1 ))
247			echo "[FAIL]" | tap_prefix
248			echo "not ok ${count_total} ${test} # exit=$ret" | tap_output
249			exitcode=1
250		fi
251	fi # test_selected
252}
253
254echo "TAP version 13" | tap_output
255
256CATEGORY="hugetlb" run_test ./hugetlb-mmap
257CATEGORY="hugetlb" run_test ./hugetlb-shm
258CATEGORY="hugetlb" run_test ./hugetlb-mremap
259CATEGORY="hugetlb" run_test ./hugetlb-madvise
260CATEGORY="hugetlb" run_test ./hugetlb_dio
261CATEGORY="hugetlb" run_test ./hugetlb_fault_after_madv
262CATEGORY="hugetlb" run_test ./hugetlb_madv_vs_map
263
264if test_selected "hugetlb"; then
265	echo "NOTE: These hugetlb tests provide minimal coverage.  Use"	  | tap_prefix
266	echo "      https://github.com/libhugetlbfs/libhugetlbfs.git for" | tap_prefix
267	echo "      hugetlb regression testing."			  | tap_prefix
268fi
269
270CATEGORY="mmap" run_test ./map_fixed_noreplace
271
272CATEGORY="gup_test" run_test ./gup
273CATEGORY="gup_test" run_test ./gup_longterm
274
275CATEGORY="userfaultfd" run_test ./uffd-unit-tests
276uffd_stress_bin=./uffd-stress
277CATEGORY="userfaultfd" run_test ${uffd_stress_bin} anon 20 16
278CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb 128 32
279CATEGORY="userfaultfd" run_test ${uffd_stress_bin} hugetlb-private 128 32
280CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem 20 16
281CATEGORY="userfaultfd" run_test ${uffd_stress_bin} shmem-private 20 16
282CATEGORY="userfaultfd" run_test ./uffd-wp-mremap
283
284CATEGORY="compaction" run_test ./compaction_test
285
286if command -v sudo &> /dev/null && sudo -u nobody ls ./on-fault-limit >/dev/null;
287then
288	CATEGORY="mlock" run_test sudo -u nobody sh -c ./on-fault-limit
289else
290	echo "# SKIP ./on-fault-limit"
291fi
292
293CATEGORY="mmap" run_test ./map_populate
294CATEGORY="mmap" run_test ./droppable
295
296CATEGORY="mlock" run_test ./mlock-random-test
297
298CATEGORY="mlock" run_test ./mlock2-tests
299
300CATEGORY="process_mrelease" run_test ./mrelease_test
301
302CATEGORY="mremap" run_test ./mremap_test
303
304CATEGORY="hugetlb" run_test ./thuge-gen
305CATEGORY="hugetlb" run_test ./charge_reserved_hugetlb.sh -cgroup-v2
306CATEGORY="hugetlb" run_test ./hugetlb_reparenting_test.sh -cgroup-v2
307
308if $RUN_DESTRUCTIVE; then
309enable_soft_offline=$(cat /proc/sys/vm/enable_soft_offline)
310CATEGORY="hugetlb" run_test ./hugetlb-soft-offline
311echo "$enable_soft_offline" > /proc/sys/vm/enable_soft_offline
312CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison
313fi
314
315if [ $VADDR64 -ne 0 ]; then
316	# va high address boundary switch test
317	CATEGORY="hugevm" run_test bash ./va_high_addr_switch.sh
318fi # VADDR64
319
320# vmalloc stability smoke test
321CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke
322
323CATEGORY="mremap" run_test ./mremap_dontunmap
324
325CATEGORY="hmm" run_test bash ./test_hmm.sh smoke
326
327# MADV_GUARD_INSTALL and MADV_GUARD_REMOVE tests
328CATEGORY="madv_guard" run_test ./guard-regions
329
330# MADV_POPULATE_READ and MADV_POPULATE_WRITE tests
331CATEGORY="madv_populate" run_test ./madv_populate
332
333# PROCESS_MADV test
334CATEGORY="process_madv" run_test ./process_madv
335
336CATEGORY="vma_merge" run_test ./merge
337
338if test_selected "memfd_secret" && [ -x ./memfd_secret ]
339then
340if [ -f /proc/sys/kernel/yama/ptrace_scope ]; then
341	(echo 0 > /proc/sys/kernel/yama/ptrace_scope 2>&1) | tap_prefix
342fi
343CATEGORY="memfd_secret" run_test ./memfd_secret
344fi
345
346# KSM KSM_MERGE_TIME_HUGE_PAGES test with size of 100
347CATEGORY="ksm" run_test ./ksm_tests -H -s 100
348# KSM KSM_MERGE_TIME test with size of 100
349CATEGORY="ksm" run_test ./ksm_tests -P -s 100
350# KSM MADV_MERGEABLE test with 10 identical pages
351CATEGORY="ksm" run_test ./ksm_tests -M -p 10
352# KSM unmerge test
353CATEGORY="ksm" run_test ./ksm_tests -U
354# KSM test with 10 zero pages and use_zero_pages = 0
355CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 0
356# KSM test with 10 zero pages and use_zero_pages = 1
357CATEGORY="ksm" run_test ./ksm_tests -Z -p 10 -z 1
358# KSM test with 2 NUMA nodes and merge_across_nodes = 1
359CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 1
360# KSM test with 2 NUMA nodes and merge_across_nodes = 0
361CATEGORY="ksm_numa" run_test ./ksm_tests -N -m 0
362
363CATEGORY="ksm" run_test ./ksm_functional_tests
364
365# protection_keys tests
366if [ -x ./protection_keys_32 ]
367then
368	CATEGORY="pkey" run_test ./protection_keys_32
369fi
370
371if [ -x ./protection_keys_64 ]
372then
373	CATEGORY="pkey" run_test ./protection_keys_64
374fi
375
376CATEGORY="soft_dirty" run_test ./soft-dirty
377
378CATEGORY="pagemap" run_test ./pagemap_ioctl
379
380CATEGORY="pfnmap" run_test ./pfnmap
381
382# COW tests
383CATEGORY="cow" run_test ./cow
384
385CATEGORY="thp" run_test ./folio_order_check
386
387CATEGORY="thp" run_test ./khugepaged
388
389CATEGORY="thp" run_test ./khugepaged -s 2
390
391CATEGORY="thp" run_test ./khugepaged all:shmem
392
393CATEGORY="thp" run_test ./khugepaged -s 4 all:shmem
394
395CATEGORY="thp" run_test ./khugepaged -c 4 mthp_khugepaged:anon
396
397# Try to create XFS if not provided
398if [ -z "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
399    if test_selected "thp"; then
400	if grep xfs /proc/filesystems &>/dev/null; then
401	    XFS_IMG=$(mktemp /tmp/xfs_img_XXXXXX)
402	    XFS_DIR=$(mktemp -d /tmp/xfs_dir_XXXXXX)
403	    truncate -s 314572800 ${XFS_IMG}
404	    if mkfs.xfs -q ${XFS_IMG} && mount -t xfs -o loop ${XFS_IMG} ${XFS_DIR}; then
405	        SPLIT_HUGE_PAGE_TEST_XFS_PATH=${XFS_DIR}
406	        MOUNTED_XFS=1
407	    else
408	        rmdir ${XFS_DIR}
409	        rm -f ${XFS_IMG}
410	    fi
411	fi
412    fi
413fi
414
415if [ -n "${SPLIT_HUGE_PAGE_TEST_XFS_PATH}" ]; then
416CATEGORY="thp" run_test ./khugepaged all:file ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
417elif test_selected thp; then
418	count_total=$(( count_total + 1 ))
419	count_skip=$(( count_skip + 1 ))
420	echo "[SKIP] ./khugepaged all:file" | tap_prefix
421fi
422
423CATEGORY="thp" run_test ./split_huge_page_test ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
424
425if [ -n "${MOUNTED_XFS}" ]; then
426    umount ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
427    rmdir ${SPLIT_HUGE_PAGE_TEST_XFS_PATH}
428    rm -f ${XFS_IMG}
429fi
430
431CATEGORY="thp" run_test ./transhuge-stress -d 20
432
433CATEGORY="thp" run_test ./folio_split_race_test
434
435CATEGORY="migration" run_test ./migration
436
437CATEGORY="mkdirty" run_test ./mkdirty
438
439CATEGORY="mdwe" run_test ./mdwe_test
440
441CATEGORY="page_frag" run_test ./test_page_frag.sh smoke
442
443CATEGORY="page_frag" run_test ./test_page_frag.sh aligned
444
445CATEGORY="page_frag" run_test ./test_page_frag.sh nonaligned
446
447CATEGORY="rmap" run_test ./rmap
448
449CATEGORY="memory-failure" run_test ./memory-failure
450
451echo "SUMMARY: PASS=${count_pass} SKIP=${count_skip} FAIL=${count_fail}" | tap_prefix
452echo "1..${count_total}" | tap_output
453
454exit $exitcode
455