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
8mnt=./huge
9exitcode=0
10
11#get huge pagesize and freepages from /proc/meminfo
12while read name size unit; do
13	if [ "$name" = "HugePages_Free:" ]; then
14		freepgs=$size
15	fi
16	if [ "$name" = "Hugepagesize:" ]; then
17		hpgsize_KB=$size
18	fi
19done < /proc/meminfo
20
21# Simple hugetlbfs tests have a hardcoded minimum requirement of
22# huge pages totaling 256MB (262144KB) in size.  The userfaultfd
23# hugetlb test requires a minimum of 2 * nr_cpus huge pages.  Take
24# both of these requirements into account and attempt to increase
25# number of huge pages available.
26nr_cpus=$(nproc)
27hpgsize_MB=$((hpgsize_KB / 1024))
28half_ufd_size_MB=$((((nr_cpus * hpgsize_MB + 127) / 128) * 128))
29needmem_KB=$((half_ufd_size_MB * 2 * 1024))
30
31#set proper nr_hugepages
32if [ -n "$freepgs" ] && [ -n "$hpgsize_KB" ]; then
33	nr_hugepgs=`cat /proc/sys/vm/nr_hugepages`
34	needpgs=$((needmem_KB / hpgsize_KB))
35	tries=2
36	while [ $tries -gt 0 ] && [ $freepgs -lt $needpgs ]; do
37		lackpgs=$(( $needpgs - $freepgs ))
38		echo 3 > /proc/sys/vm/drop_caches
39		echo $(( $lackpgs + $nr_hugepgs )) > /proc/sys/vm/nr_hugepages
40		if [ $? -ne 0 ]; then
41			echo "Please run this test as root"
42			exit $ksft_skip
43		fi
44		while read name size unit; do
45			if [ "$name" = "HugePages_Free:" ]; then
46				freepgs=$size
47			fi
48		done < /proc/meminfo
49		tries=$((tries - 1))
50	done
51	if [ $freepgs -lt $needpgs ]; then
52		printf "Not enough huge pages available (%d < %d)\n" \
53		       $freepgs $needpgs
54		exit 1
55	fi
56else
57	echo "no hugetlbfs support in kernel?"
58	exit 1
59fi
60
61mkdir $mnt
62mount -t hugetlbfs none $mnt
63
64echo "---------------------"
65echo "running hugepage-mmap"
66echo "---------------------"
67./hugepage-mmap
68if [ $? -ne 0 ]; then
69	echo "[FAIL]"
70	exitcode=1
71else
72	echo "[PASS]"
73fi
74
75shmmax=`cat /proc/sys/kernel/shmmax`
76shmall=`cat /proc/sys/kernel/shmall`
77echo 268435456 > /proc/sys/kernel/shmmax
78echo 4194304 > /proc/sys/kernel/shmall
79echo "--------------------"
80echo "running hugepage-shm"
81echo "--------------------"
82./hugepage-shm
83if [ $? -ne 0 ]; then
84	echo "[FAIL]"
85	exitcode=1
86else
87	echo "[PASS]"
88fi
89echo $shmmax > /proc/sys/kernel/shmmax
90echo $shmall > /proc/sys/kernel/shmall
91
92echo "-------------------"
93echo "running map_hugetlb"
94echo "-------------------"
95./map_hugetlb
96if [ $? -ne 0 ]; then
97	echo "[FAIL]"
98	exitcode=1
99else
100	echo "[PASS]"
101fi
102
103echo "NOTE: The above hugetlb tests provide minimal coverage.  Use"
104echo "      https://github.com/libhugetlbfs/libhugetlbfs.git for"
105echo "      hugetlb regression testing."
106
107echo "-------------------"
108echo "running userfaultfd"
109echo "-------------------"
110./userfaultfd anon 128 32
111if [ $? -ne 0 ]; then
112	echo "[FAIL]"
113	exitcode=1
114else
115	echo "[PASS]"
116fi
117
118echo "---------------------------"
119echo "running userfaultfd_hugetlb"
120echo "---------------------------"
121# Test requires source and destination huge pages.  Size of source
122# (half_ufd_size_MB) is passed as argument to test.
123./userfaultfd hugetlb $half_ufd_size_MB 32 $mnt/ufd_test_file
124if [ $? -ne 0 ]; then
125	echo "[FAIL]"
126	exitcode=1
127else
128	echo "[PASS]"
129fi
130rm -f $mnt/ufd_test_file
131
132echo "-------------------------"
133echo "running userfaultfd_shmem"
134echo "-------------------------"
135./userfaultfd shmem 128 32
136if [ $? -ne 0 ]; then
137	echo "[FAIL]"
138	exitcode=1
139else
140	echo "[PASS]"
141fi
142
143#cleanup
144umount $mnt
145rm -rf $mnt
146echo $nr_hugepgs > /proc/sys/vm/nr_hugepages
147
148echo "-----------------------"
149echo "running compaction_test"
150echo "-----------------------"
151./compaction_test
152if [ $? -ne 0 ]; then
153	echo "[FAIL]"
154	exitcode=1
155else
156	echo "[PASS]"
157fi
158
159echo "----------------------"
160echo "running on-fault-limit"
161echo "----------------------"
162sudo -u nobody ./on-fault-limit
163if [ $? -ne 0 ]; then
164	echo "[FAIL]"
165	exitcode=1
166else
167	echo "[PASS]"
168fi
169
170echo "--------------------"
171echo "running map_populate"
172echo "--------------------"
173./map_populate
174if [ $? -ne 0 ]; then
175	echo "[FAIL]"
176	exitcode=1
177else
178	echo "[PASS]"
179fi
180
181echo "--------------------"
182echo "running mlock2-tests"
183echo "--------------------"
184./mlock2-tests
185if [ $? -ne 0 ]; then
186	echo "[FAIL]"
187	exitcode=1
188else
189	echo "[PASS]"
190fi
191
192echo "-----------------------------"
193echo "running virtual_address_range"
194echo "-----------------------------"
195./virtual_address_range
196if [ $? -ne 0 ]; then
197	echo "[FAIL]"
198	exitcode=1
199else
200	echo "[PASS]"
201fi
202
203echo "-----------------------------"
204echo "running virtual address 128TB switch test"
205echo "-----------------------------"
206./va_128TBswitch
207if [ $? -ne 0 ]; then
208    echo "[FAIL]"
209    exitcode=1
210else
211    echo "[PASS]"
212fi
213
214exit $exitcode
215