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