1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-or-later
3
4TIMEOUT=30
5
6DEBUFS_DIR=`cat /proc/mounts | grep debugfs | awk '{print $2}'`
7if [ ! -e "$DEBUFS_DIR" ]
8then
9	echo "debugfs not found, skipping" 1>&2
10	exit 4
11fi
12
13if [ ! -e "$DEBUFS_DIR/tracing/current_tracer" ]
14then
15	echo "Tracing files not found, skipping" 1>&2
16	exit 4
17fi
18
19
20echo "Testing for spurious faults when mapping kernel memory..."
21
22if grep -q "FUNCTION TRACING IS CORRUPTED" "$DEBUFS_DIR/tracing/trace"
23then
24	echo "FAILED: Ftrace already dead. Probably due to a spurious fault" 1>&2
25	exit 1
26fi
27
28dmesg -C
29START_TIME=`date +%s`
30END_TIME=`expr $START_TIME + $TIMEOUT`
31while [ `date +%s` -lt $END_TIME ]
32do
33	echo function > $DEBUFS_DIR/tracing/current_tracer
34	echo nop > $DEBUFS_DIR/tracing/current_tracer
35	if dmesg | grep -q 'ftrace bug'
36	then
37		break
38	fi
39done
40
41echo nop > $DEBUFS_DIR/tracing/current_tracer
42if dmesg | grep -q 'ftrace bug'
43then
44	echo "FAILED: Mapping kernel memory causes spurious faults" 1>&2
45	exit 1
46else
47	echo "OK: Mapping kernel memory does not cause spurious faults"
48	exit 0
49fi
50