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
61#filter 64bit architectures
62ARCH64STR="arm64 ia64 mips64 parisc64 ppc64 ppc64le riscv64 s390x sh64 sparc64 x86_64"
63if [ -z $ARCH ]; then
64  ARCH=`uname -m 2>/dev/null | sed -e 's/aarch64.*/arm64/'`
65fi
66VADDR64=0
67echo "$ARCH64STR" | grep $ARCH && VADDR64=1
68
69mkdir $mnt
70mount -t hugetlbfs none $mnt
71
72echo "---------------------"
73echo "running hugepage-mmap"
74echo "---------------------"
75./hugepage-mmap
76if [ $? -ne 0 ]; then
77	echo "[FAIL]"
78	exitcode=1
79else
80	echo "[PASS]"
81fi
82
83shmmax=`cat /proc/sys/kernel/shmmax`
84shmall=`cat /proc/sys/kernel/shmall`
85echo 268435456 > /proc/sys/kernel/shmmax
86echo 4194304 > /proc/sys/kernel/shmall
87echo "--------------------"
88echo "running hugepage-shm"
89echo "--------------------"
90./hugepage-shm
91if [ $? -ne 0 ]; then
92	echo "[FAIL]"
93	exitcode=1
94else
95	echo "[PASS]"
96fi
97echo $shmmax > /proc/sys/kernel/shmmax
98echo $shmall > /proc/sys/kernel/shmall
99
100echo "-------------------"
101echo "running map_hugetlb"
102echo "-------------------"
103./map_hugetlb
104if [ $? -ne 0 ]; then
105	echo "[FAIL]"
106	exitcode=1
107else
108	echo "[PASS]"
109fi
110
111echo "-----------------------"
112echo "running hugepage-mremap"
113echo "-----------------------"
114./hugepage-mremap
115if [ $? -ne 0 ]; then
116	echo "[FAIL]"
117	exitcode=1
118else
119	echo "[PASS]"
120fi
121
122echo "NOTE: The above hugetlb tests provide minimal coverage.  Use"
123echo "      https://github.com/libhugetlbfs/libhugetlbfs.git for"
124echo "      hugetlb regression testing."
125
126echo "---------------------------"
127echo "running map_fixed_noreplace"
128echo "---------------------------"
129./map_fixed_noreplace
130if [ $? -ne 0 ]; then
131	echo "[FAIL]"
132	exitcode=1
133else
134	echo "[PASS]"
135fi
136
137echo "------------------------------------------------------"
138echo "running: gup_test -u # get_user_pages_fast() benchmark"
139echo "------------------------------------------------------"
140./gup_test -u
141if [ $? -ne 0 ]; then
142	echo "[FAIL]"
143	exitcode=1
144else
145	echo "[PASS]"
146fi
147
148echo "------------------------------------------------------"
149echo "running: gup_test -a # pin_user_pages_fast() benchmark"
150echo "------------------------------------------------------"
151./gup_test -a
152if [ $? -ne 0 ]; then
153	echo "[FAIL]"
154	exitcode=1
155else
156	echo "[PASS]"
157fi
158
159echo "------------------------------------------------------------"
160echo "# Dump pages 0, 19, and 4096, using pin_user_pages:"
161echo "running: gup_test -ct -F 0x1 0 19 0x1000 # dump_page() test"
162echo "------------------------------------------------------------"
163./gup_test -ct -F 0x1 0 19 0x1000
164if [ $? -ne 0 ]; then
165	echo "[FAIL]"
166	exitcode=1
167else
168	echo "[PASS]"
169fi
170
171echo "-------------------"
172echo "running userfaultfd"
173echo "-------------------"
174./userfaultfd anon 20 16
175if [ $? -ne 0 ]; then
176	echo "[FAIL]"
177	exitcode=1
178else
179	echo "[PASS]"
180fi
181
182echo "---------------------------"
183echo "running userfaultfd_hugetlb"
184echo "---------------------------"
185# Test requires source and destination huge pages.  Size of source
186# (half_ufd_size_MB) is passed as argument to test.
187./userfaultfd hugetlb $half_ufd_size_MB 32 $mnt/ufd_test_file
188if [ $? -ne 0 ]; then
189	echo "[FAIL]"
190	exitcode=1
191else
192	echo "[PASS]"
193fi
194rm -f $mnt/ufd_test_file
195
196echo "-------------------------"
197echo "running userfaultfd_shmem"
198echo "-------------------------"
199./userfaultfd shmem 20 16
200if [ $? -ne 0 ]; then
201	echo "[FAIL]"
202	exitcode=1
203else
204	echo "[PASS]"
205fi
206
207#cleanup
208umount $mnt
209rm -rf $mnt
210echo $nr_hugepgs > /proc/sys/vm/nr_hugepages
211
212echo "-----------------------"
213echo "running compaction_test"
214echo "-----------------------"
215./compaction_test
216if [ $? -ne 0 ]; then
217	echo "[FAIL]"
218	exitcode=1
219else
220	echo "[PASS]"
221fi
222
223echo "----------------------"
224echo "running on-fault-limit"
225echo "----------------------"
226sudo -u nobody ./on-fault-limit
227if [ $? -ne 0 ]; then
228	echo "[FAIL]"
229	exitcode=1
230else
231	echo "[PASS]"
232fi
233
234echo "--------------------"
235echo "running map_populate"
236echo "--------------------"
237./map_populate
238if [ $? -ne 0 ]; then
239	echo "[FAIL]"
240	exitcode=1
241else
242	echo "[PASS]"
243fi
244
245echo "-------------------------"
246echo "running mlock-random-test"
247echo "-------------------------"
248./mlock-random-test
249if [ $? -ne 0 ]; then
250	echo "[FAIL]"
251	exitcode=1
252else
253	echo "[PASS]"
254fi
255
256echo "--------------------"
257echo "running mlock2-tests"
258echo "--------------------"
259./mlock2-tests
260if [ $? -ne 0 ]; then
261	echo "[FAIL]"
262	exitcode=1
263else
264	echo "[PASS]"
265fi
266
267echo "-------------------"
268echo "running mremap_test"
269echo "-------------------"
270./mremap_test
271if [ $? -ne 0 ]; then
272	echo "[FAIL]"
273	exitcode=1
274else
275	echo "[PASS]"
276fi
277
278echo "-----------------"
279echo "running thuge-gen"
280echo "-----------------"
281./thuge-gen
282if [ $? -ne 0 ]; then
283	echo "[FAIL]"
284	exitcode=1
285else
286	echo "[PASS]"
287fi
288
289if [ $VADDR64 -ne 0 ]; then
290echo "-----------------------------"
291echo "running virtual_address_range"
292echo "-----------------------------"
293./virtual_address_range
294if [ $? -ne 0 ]; then
295	echo "[FAIL]"
296	exitcode=1
297else
298	echo "[PASS]"
299fi
300
301echo "-----------------------------"
302echo "running virtual address 128TB switch test"
303echo "-----------------------------"
304./va_128TBswitch
305if [ $? -ne 0 ]; then
306    echo "[FAIL]"
307    exitcode=1
308else
309    echo "[PASS]"
310fi
311fi # VADDR64
312
313echo "------------------------------------"
314echo "running vmalloc stability smoke test"
315echo "------------------------------------"
316./test_vmalloc.sh smoke
317ret_val=$?
318
319if [ $ret_val -eq 0 ]; then
320	echo "[PASS]"
321elif [ $ret_val -eq $ksft_skip ]; then
322	 echo "[SKIP]"
323	 exitcode=$ksft_skip
324else
325	echo "[FAIL]"
326	exitcode=1
327fi
328
329echo "------------------------------------"
330echo "running MREMAP_DONTUNMAP smoke test"
331echo "------------------------------------"
332./mremap_dontunmap
333ret_val=$?
334
335if [ $ret_val -eq 0 ]; then
336	echo "[PASS]"
337elif [ $ret_val -eq $ksft_skip ]; then
338	 echo "[SKIP]"
339	 exitcode=$ksft_skip
340else
341	echo "[FAIL]"
342	exitcode=1
343fi
344
345echo "running HMM smoke test"
346echo "------------------------------------"
347./test_hmm.sh smoke
348ret_val=$?
349
350if [ $ret_val -eq 0 ]; then
351	echo "[PASS]"
352elif [ $ret_val -eq $ksft_skip ]; then
353	echo "[SKIP]"
354	exitcode=$ksft_skip
355else
356	echo "[FAIL]"
357	exitcode=1
358fi
359
360echo "--------------------------------------------------------"
361echo "running MADV_POPULATE_READ and MADV_POPULATE_WRITE tests"
362echo "--------------------------------------------------------"
363./madv_populate
364ret_val=$?
365
366if [ $ret_val -eq 0 ]; then
367	echo "[PASS]"
368elif [ $ret_val -eq $ksft_skip ]; then
369	echo "[SKIP]"
370	exitcode=$ksft_skip
371else
372	echo "[FAIL]"
373	exitcode=1
374fi
375
376echo "running memfd_secret test"
377echo "------------------------------------"
378./memfd_secret
379ret_val=$?
380
381if [ $ret_val -eq 0 ]; then
382	echo "[PASS]"
383elif [ $ret_val -eq $ksft_skip ]; then
384	echo "[SKIP]"
385	exitcode=$ksft_skip
386else
387	echo "[FAIL]"
388	exitcode=1
389fi
390
391echo "-------------------------------------------------------"
392echo "running KSM MADV_MERGEABLE test with 10 identical pages"
393echo "-------------------------------------------------------"
394./ksm_tests -M -p 10
395ret_val=$?
396
397if [ $ret_val -eq 0 ]; then
398	echo "[PASS]"
399elif [ $ret_val -eq $ksft_skip ]; then
400	 echo "[SKIP]"
401	 exitcode=$ksft_skip
402else
403	echo "[FAIL]"
404	exitcode=1
405fi
406
407echo "------------------------"
408echo "running KSM unmerge test"
409echo "------------------------"
410./ksm_tests -U
411ret_val=$?
412
413if [ $ret_val -eq 0 ]; then
414	echo "[PASS]"
415elif [ $ret_val -eq $ksft_skip ]; then
416	 echo "[SKIP]"
417	 exitcode=$ksft_skip
418else
419	echo "[FAIL]"
420	exitcode=1
421fi
422
423echo "----------------------------------------------------------"
424echo "running KSM test with 10 zero pages and use_zero_pages = 0"
425echo "----------------------------------------------------------"
426./ksm_tests -Z -p 10 -z 0
427ret_val=$?
428
429if [ $ret_val -eq 0 ]; then
430	echo "[PASS]"
431elif [ $ret_val -eq $ksft_skip ]; then
432	 echo "[SKIP]"
433	 exitcode=$ksft_skip
434else
435	echo "[FAIL]"
436	exitcode=1
437fi
438
439echo "----------------------------------------------------------"
440echo "running KSM test with 10 zero pages and use_zero_pages = 1"
441echo "----------------------------------------------------------"
442./ksm_tests -Z -p 10 -z 1
443ret_val=$?
444
445if [ $ret_val -eq 0 ]; then
446	echo "[PASS]"
447elif [ $ret_val -eq $ksft_skip ]; then
448	 echo "[SKIP]"
449	 exitcode=$ksft_skip
450else
451	echo "[FAIL]"
452	exitcode=1
453fi
454
455echo "-------------------------------------------------------------"
456echo "running KSM test with 2 NUMA nodes and merge_across_nodes = 1"
457echo "-------------------------------------------------------------"
458./ksm_tests -N -m 1
459ret_val=$?
460
461if [ $ret_val -eq 0 ]; then
462	echo "[PASS]"
463elif [ $ret_val -eq $ksft_skip ]; then
464	 echo "[SKIP]"
465	 exitcode=$ksft_skip
466else
467	echo "[FAIL]"
468	exitcode=1
469fi
470
471echo "-------------------------------------------------------------"
472echo "running KSM test with 2 NUMA nodes and merge_across_nodes = 0"
473echo "-------------------------------------------------------------"
474./ksm_tests -N -m 0
475ret_val=$?
476
477if [ $ret_val -eq 0 ]; then
478	echo "[PASS]"
479elif [ $ret_val -eq $ksft_skip ]; then
480	 echo "[SKIP]"
481	 exitcode=$ksft_skip
482else
483	echo "[FAIL]"
484	exitcode=1
485fi
486
487exit $exitcode
488
489exit $exitcode
490