xref: /linux/tools/testing/selftests/mm/va_high_addr_switch.sh (revision 8804d970fab45726b3c7cd7f240b31122aa94219)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Copyright (C) 2022 Adam Sindelar (Meta) <adam@wowsignal.io>
5#
6# This is a test for mmap behavior with 5-level paging. This script wraps the
7# real test to check that the kernel is configured to support at least 5
8# pagetable levels.
9
10# Kselftest framework requirement - SKIP code is 4.
11ksft_skip=4
12orig_nr_hugepages=0
13
14skip()
15{
16	echo "$1"
17	exit $ksft_skip
18}
19
20check_supported_x86_64()
21{
22	local config="/proc/config.gz"
23	[[ -f "${config}" ]] || config="/boot/config-$(uname -r)"
24	[[ -f "${config}" ]] || skip "Cannot find kernel config in /proc or /boot"
25
26	# gzip -dcfq automatically handles both compressed and plaintext input.
27	# See man 1 gzip under '-f'.
28	local pg_table_levels=$(gzip -dcfq "${config}" | grep PGTABLE_LEVELS | cut -d'=' -f 2)
29
30	local cpu_supports_pl5=$(awk '/^flags/ {if (/la57/) {print 0;}
31		else {print 1}; exit}' /proc/cpuinfo 2>/dev/null)
32
33	if [[ "${pg_table_levels}" -lt 5 ]]; then
34		skip "$0: PGTABLE_LEVELS=${pg_table_levels}, must be >= 5 to run this test"
35	elif [[ "${cpu_supports_pl5}" -ne 0 ]]; then
36		skip "$0: CPU does not have the necessary la57 flag to support page table level 5"
37	fi
38}
39
40check_supported_ppc64()
41{
42	local config="/proc/config.gz"
43	[[ -f "${config}" ]] || config="/boot/config-$(uname -r)"
44	[[ -f "${config}" ]] || skip "Cannot find kernel config in /proc or /boot"
45
46	local pg_table_levels=$(gzip -dcfq "${config}" | grep PGTABLE_LEVELS | cut -d'=' -f 2)
47	if [[ "${pg_table_levels}" -lt 5 ]]; then
48		skip "$0: PGTABLE_LEVELS=${pg_table_levels}, must be >= 5 to run this test"
49	fi
50
51	local mmu_support=$(grep -m1 "mmu" /proc/cpuinfo | awk '{print $3}')
52	if [[ "$mmu_support" != "radix" ]]; then
53		skip "$0: System does not use Radix MMU, required for 5-level paging"
54	fi
55
56	local hugepages_total=$(awk '/HugePages_Total/ {print $2}' /proc/meminfo)
57	if [[ "${hugepages_total}" -eq 0 ]]; then
58		skip "$0: HugePages are not enabled, required for some tests"
59	fi
60}
61
62check_test_requirements()
63{
64	# The test supports x86_64 and powerpc64. We currently have no useful
65	# eligibility check for powerpc64, and the test itself will reject other
66	# architectures.
67	case `uname -m` in
68		"x86_64")
69			check_supported_x86_64
70		;;
71		"ppc64le"|"ppc64")
72			check_supported_ppc64
73		;;
74		*)
75			return 0
76		;;
77	esac
78}
79
80save_nr_hugepages()
81{
82	orig_nr_hugepages=$(cat /proc/sys/vm/nr_hugepages)
83}
84
85restore_nr_hugepages()
86{
87	echo "$orig_nr_hugepages" > /proc/sys/vm/nr_hugepages
88}
89
90setup_nr_hugepages()
91{
92	local needpgs=$1
93	while read -r name size unit; do
94		if [ "$name" = "HugePages_Free:" ]; then
95			freepgs="$size"
96			break
97		fi
98	done < /proc/meminfo
99	if [ "$freepgs" -ge "$needpgs" ]; then
100		return
101	fi
102	local hpgs=$((orig_nr_hugepages + needpgs))
103	echo $hpgs > /proc/sys/vm/nr_hugepages
104
105	local nr_hugepgs=$(cat /proc/sys/vm/nr_hugepages)
106	if [ "$nr_hugepgs" != "$hpgs" ]; then
107		restore_nr_hugepages
108		skip "$0: no enough hugepages for testing"
109	fi
110}
111
112check_test_requirements
113save_nr_hugepages
114# 4 keep_mapped pages, and one for tmp usage
115setup_nr_hugepages 5
116./va_high_addr_switch --run-hugetlb
117restore_nr_hugepages
118