Merge tag 'mips_6.14' of git://git.kernel.org/pub/scm/linux/kernel/git/mips/linux
[linux.git] / tools / testing / fault-injection / failcmd.sh
blobc4f2432750f44d0229f7bb90396d9962c1c7671a
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # NAME
5 # failcmd.sh - run a command with injecting slab/page allocation failures
7 # SYNOPSIS
8 # failcmd.sh --help
9 # failcmd.sh [<options>] command [arguments]
11 # DESCRIPTION
12 # Run command with injecting slab/page allocation failures by fault
13 # injection.
15 # NOTE: you need to run this script as root.
18 usage()
20 cat >&2 <<EOF
21 Usage: $0 [options] command [arguments]
23 OPTIONS
24 -p percent
25 --probability=percent
26 likelihood of failure injection, in percent.
27 Default value is 1
29 -t value
30 --times=value
31 specifies how many times failures may happen at most.
32 Default value is 1
34 --oom-kill-allocating-task=value
35 set /proc/sys/vm/oom_kill_allocating_task to specified value
36 before running the command.
37 Default value is 1
39 -h, --help
40 Display a usage message and exit
42 --interval=value, --space=value, --verbose=value, --task-filter=value,
43 --stacktrace-depth=value, --require-start=value, --require-end=value,
44 --reject-start=value, --reject-end=value, --ignore-gfp-wait=value
45 See Documentation/fault-injection/fault-injection.rst for more
46 information
48 failslab options:
49 --cache-filter=value
51 fail_page_alloc options:
52 --ignore-gfp-highmem=value, --min-order=value
54 ENVIRONMENT
55 FAILCMD_TYPE
56 The following values for FAILCMD_TYPE are recognized:
58 failslab
59 inject slab allocation failures
60 fail_page_alloc
61 inject page allocation failures
63 If FAILCMD_TYPE is not defined, then failslab is used.
64 EOF
67 exit_if_not_hex() {
68 local value="$1"
69 if ! [[ $value =~ ^0x[0-9a-fA-F]+$ ]]; then
70 echo "Error: The provided value '$value' is not a valid hexadecimal number." >&2
71 exit 1
75 if [ $UID != 0 ]; then
76 echo must be run as root >&2
77 exit 1
80 DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3}'`
82 if [ ! -d "$DEBUGFS" ]; then
83 echo debugfs is not mounted >&2
84 exit 1
87 FAILCMD_TYPE=${FAILCMD_TYPE:-failslab}
88 FAULTATTR=$DEBUGFS/$FAILCMD_TYPE
90 if [ ! -d $FAULTATTR ]; then
91 echo $FAILCMD_TYPE is not available >&2
92 exit 1
95 LONGOPTS=probability:,interval:,times:,space:,verbose:,task-filter:
96 LONGOPTS=$LONGOPTS,stacktrace-depth:,require-start:,require-end:
97 LONGOPTS=$LONGOPTS,reject-start:,reject-end:,oom-kill-allocating-task:,help
99 if [ $FAILCMD_TYPE = failslab ]; then
100 LONGOPTS=$LONGOPTS,ignore-gfp-wait:,cache-filter:
101 elif [ $FAILCMD_TYPE = fail_page_alloc ]; then
102 LONGOPTS=$LONGOPTS,ignore-gfp-wait:,ignore-gfp-highmem:,min-order:
105 TEMP=`getopt -o p:i:t:s:v:h --long $LONGOPTS -n 'failcmd.sh' -- "$@"`
107 if [ $? != 0 ]; then
108 usage
109 exit 1
112 eval set -- "$TEMP"
114 fault_attr_default()
116 echo N > $FAULTATTR/task-filter
117 echo 0 > $FAULTATTR/probability
118 echo 1 > $FAULTATTR/times
121 fault_attr_default
123 oom_kill_allocating_task_saved=`cat /proc/sys/vm/oom_kill_allocating_task`
125 restore_values()
127 fault_attr_default
128 echo $oom_kill_allocating_task_saved \
129 > /proc/sys/vm/oom_kill_allocating_task
133 # Default options
135 declare -i oom_kill_allocating_task=1
136 declare task_filter=Y
137 declare -i probability=1
138 declare -i times=1
140 while true; do
141 case "$1" in
142 -p|--probability)
143 probability=$2
144 shift 2
146 -i|--interval)
147 echo $2 > $FAULTATTR/interval
148 shift 2
150 -t|--times)
151 times=$2
152 shift 2
154 -s|--space)
155 echo $2 > $FAULTATTR/space
156 shift 2
158 -v|--verbose)
159 echo $2 > $FAULTATTR/verbose
160 shift 2
162 --task-filter)
163 task_filter=$2
164 shift 2
166 --stacktrace-depth)
167 echo $2 > $FAULTATTR/stacktrace-depth
168 shift 2
170 --require-start)
171 exit_if_not_hex "$2"
172 echo $2 > $FAULTATTR/require-start
173 shift 2
175 --require-end)
176 exit_if_not_hex "$2"
177 echo $2 > $FAULTATTR/require-end
178 shift 2
180 --reject-start)
181 exit_if_not_hex "$2"
182 echo $2 > $FAULTATTR/reject-start
183 shift 2
185 --reject-end)
186 exit_if_not_hex "$2"
187 echo $2 > $FAULTATTR/reject-end
188 shift 2
190 --oom-kill-allocating-task)
191 oom_kill_allocating_task=$2
192 shift 2
194 --ignore-gfp-wait)
195 echo $2 > $FAULTATTR/ignore-gfp-wait
196 shift 2
198 --cache-filter)
199 echo $2 > $FAULTATTR/cache_filter
200 shift 2
202 --ignore-gfp-highmem)
203 echo $2 > $FAULTATTR/ignore-gfp-highmem
204 shift 2
206 --min-order)
207 echo $2 > $FAULTATTR/min-order
208 shift 2
210 -h|--help)
211 usage
212 exit 0
213 shift
216 shift
217 break
219 esac
220 done
222 [ -z "$1" ] && exit 0
224 echo $oom_kill_allocating_task > /proc/sys/vm/oom_kill_allocating_task
225 echo $task_filter > $FAULTATTR/task-filter
226 echo $probability > $FAULTATTR/probability
227 echo $times > $FAULTATTR/times
229 trap "restore_values" SIGINT SIGTERM EXIT
231 cmd="echo 1 > /proc/self/make-it-fail && exec $@"
232 bash -c "$cmd"