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