xref: /linux/tools/testing/selftests/module/find_symbol.sh (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1
3# Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org>
4#
5# This is a stress test script for kallsyms through find_symbol()
6
7set -e
8
9# Kselftest framework requirement - SKIP code is 4.
10ksft_skip=4
11
12test_reqs()
13{
14	if ! which modprobe 2> /dev/null > /dev/null; then
15		echo "$0: You need modprobe installed" >&2
16		exit $ksft_skip
17	fi
18
19	if ! which kmod 2> /dev/null > /dev/null; then
20		echo "$0: You need kmod installed" >&2
21		exit $ksft_skip
22	fi
23
24	if ! which perf 2> /dev/null > /dev/null; then
25		echo "$0: You need perf installed" >&2
26		exit $ksft_skip
27	fi
28
29	uid=$(id -u)
30	if [ $uid -ne 0 ]; then
31		echo $msg must be run as root >&2
32		exit $ksft_skip
33	fi
34}
35
36load_mod()
37{
38	local STATS="-e duration_time"
39	STATS="$STATS -e user_time"
40	STATS="$STATS -e system_time"
41	STATS="$STATS -e page-faults"
42	local MOD=$1
43
44	local ARCH="$(uname -m)"
45	case "${ARCH}" in
46	x86_64)
47		perf stat $STATS $MODPROBE $MOD
48		;;
49	*)
50		time $MODPROBE $MOD
51		exit 1
52		;;
53	esac
54}
55
56remove_all()
57{
58	$MODPROBE -r test_kallsyms_b
59	for i in a b c d; do
60		$MODPROBE -r test_kallsyms_$i
61	done
62}
63test_reqs
64
65MODPROBE=$(</proc/sys/kernel/modprobe)
66
67remove_all
68load_mod test_kallsyms_b
69remove_all
70
71# Now pollute the namespace
72$MODPROBE test_kallsyms_c
73load_mod test_kallsyms_b
74
75# Now pollute the namespace with twice the number of symbols than the last time
76remove_all
77$MODPROBE test_kallsyms_c
78$MODPROBE test_kallsyms_d
79load_mod test_kallsyms_b
80
81exit 0
82