xref: /openbmc/linux/tools/testing/selftests/memfd/run_hugetlbfs_test.sh (revision 3eb66e91a25497065c5322b1268cbc3953642227)
1*8eecdd4dSShuah Khan (Samsung OSG)#!/bin/bash
2*8eecdd4dSShuah Khan (Samsung OSG)# please run as root
3*8eecdd4dSShuah Khan (Samsung OSG)
4*8eecdd4dSShuah Khan (Samsung OSG)# Kselftest framework requirement - SKIP code is 4.
5*8eecdd4dSShuah Khan (Samsung OSG)ksft_skip=4
6*8eecdd4dSShuah Khan (Samsung OSG)
7*8eecdd4dSShuah Khan (Samsung OSG)#
8*8eecdd4dSShuah Khan (Samsung OSG)# To test memfd_create with hugetlbfs, there needs to be hpages_test
9*8eecdd4dSShuah Khan (Samsung OSG)# huge pages free.  Attempt to allocate enough pages to test.
10*8eecdd4dSShuah Khan (Samsung OSG)#
11*8eecdd4dSShuah Khan (Samsung OSG)hpages_test=8
12*8eecdd4dSShuah Khan (Samsung OSG)
13*8eecdd4dSShuah Khan (Samsung OSG)#
14*8eecdd4dSShuah Khan (Samsung OSG)# Get count of free huge pages from /proc/meminfo
15*8eecdd4dSShuah Khan (Samsung OSG)#
16*8eecdd4dSShuah Khan (Samsung OSG)while read name size unit; do
17*8eecdd4dSShuah Khan (Samsung OSG)        if [ "$name" = "HugePages_Free:" ]; then
18*8eecdd4dSShuah Khan (Samsung OSG)                freepgs=$size
19*8eecdd4dSShuah Khan (Samsung OSG)        fi
20*8eecdd4dSShuah Khan (Samsung OSG)done < /proc/meminfo
21*8eecdd4dSShuah Khan (Samsung OSG)
22*8eecdd4dSShuah Khan (Samsung OSG)#
23*8eecdd4dSShuah Khan (Samsung OSG)# If not enough free huge pages for test, attempt to increase
24*8eecdd4dSShuah Khan (Samsung OSG)#
25*8eecdd4dSShuah Khan (Samsung OSG)if [ -n "$freepgs" ] && [ $freepgs -lt $hpages_test ]; then
26*8eecdd4dSShuah Khan (Samsung OSG)	nr_hugepgs=`cat /proc/sys/vm/nr_hugepages`
27*8eecdd4dSShuah Khan (Samsung OSG)	hpages_needed=`expr $hpages_test - $freepgs`
28*8eecdd4dSShuah Khan (Samsung OSG)
29*8eecdd4dSShuah Khan (Samsung OSG)	if [ $UID != 0 ]; then
30*8eecdd4dSShuah Khan (Samsung OSG)		echo "Please run memfd with hugetlbfs test as root"
31*8eecdd4dSShuah Khan (Samsung OSG)		exit $ksft_skip
32*8eecdd4dSShuah Khan (Samsung OSG)	fi
33*8eecdd4dSShuah Khan (Samsung OSG)
34*8eecdd4dSShuah Khan (Samsung OSG)	echo 3 > /proc/sys/vm/drop_caches
35*8eecdd4dSShuah Khan (Samsung OSG)	echo $(( $hpages_needed + $nr_hugepgs )) > /proc/sys/vm/nr_hugepages
36*8eecdd4dSShuah Khan (Samsung OSG)	while read name size unit; do
37*8eecdd4dSShuah Khan (Samsung OSG)		if [ "$name" = "HugePages_Free:" ]; then
38*8eecdd4dSShuah Khan (Samsung OSG)			freepgs=$size
39*8eecdd4dSShuah Khan (Samsung OSG)		fi
40*8eecdd4dSShuah Khan (Samsung OSG)	done < /proc/meminfo
41*8eecdd4dSShuah Khan (Samsung OSG)fi
42*8eecdd4dSShuah Khan (Samsung OSG)
43*8eecdd4dSShuah Khan (Samsung OSG)#
44*8eecdd4dSShuah Khan (Samsung OSG)# If still not enough huge pages available, exit.  But, give back any huge
45*8eecdd4dSShuah Khan (Samsung OSG)# pages potentially allocated above.
46*8eecdd4dSShuah Khan (Samsung OSG)#
47*8eecdd4dSShuah Khan (Samsung OSG)if [ $freepgs -lt $hpages_test ]; then
48*8eecdd4dSShuah Khan (Samsung OSG)	# nr_hugepgs non-zero only if we attempted to increase
49*8eecdd4dSShuah Khan (Samsung OSG)	if [ -n "$nr_hugepgs" ]; then
50*8eecdd4dSShuah Khan (Samsung OSG)		echo $nr_hugepgs > /proc/sys/vm/nr_hugepages
51*8eecdd4dSShuah Khan (Samsung OSG)	fi
52*8eecdd4dSShuah Khan (Samsung OSG)	printf "Not enough huge pages available (%d < %d)\n" \
53*8eecdd4dSShuah Khan (Samsung OSG)		$freepgs $needpgs
54*8eecdd4dSShuah Khan (Samsung OSG)	exit $ksft_skip
55*8eecdd4dSShuah Khan (Samsung OSG)fi
56*8eecdd4dSShuah Khan (Samsung OSG)
57*8eecdd4dSShuah Khan (Samsung OSG)#
58*8eecdd4dSShuah Khan (Samsung OSG)# Run the hugetlbfs test
59*8eecdd4dSShuah Khan (Samsung OSG)#
60*8eecdd4dSShuah Khan (Samsung OSG)./memfd_test hugetlbfs
61*8eecdd4dSShuah Khan (Samsung OSG)./run_fuse_test.sh hugetlbfs
62*8eecdd4dSShuah Khan (Samsung OSG)
63*8eecdd4dSShuah Khan (Samsung OSG)#
64*8eecdd4dSShuah Khan (Samsung OSG)# Give back any huge pages allocated for the test
65*8eecdd4dSShuah Khan (Samsung OSG)#
66*8eecdd4dSShuah Khan (Samsung OSG)if [ -n "$nr_hugepgs" ]; then
67*8eecdd4dSShuah Khan (Samsung OSG)	echo $nr_hugepgs > /proc/sys/vm/nr_hugepages
68*8eecdd4dSShuah Khan (Samsung OSG)fi
69