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