Path: blob/master/tools/testing/fault-injection/failcmd.sh
26285 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.02#3# NAME4# failcmd.sh - run a command with injecting slab/page allocation failures5#6# SYNOPSIS7# failcmd.sh --help8# failcmd.sh [<options>] command [arguments]9#10# DESCRIPTION11# Run command with injecting slab/page allocation failures by fault12# injection.13#14# NOTE: you need to run this script as root.15#1617usage()18{19cat >&2 <<EOF20Usage: $0 [options] command [arguments]2122OPTIONS23-p percent24--probability=percent25likelihood of failure injection, in percent.26Default value is 12728-t value29--times=value30specifies how many times failures may happen at most.31Default value is 13233--oom-kill-allocating-task=value34set /proc/sys/vm/oom_kill_allocating_task to specified value35before running the command.36Default value is 13738-h, --help39Display a usage message and exit4041--interval=value, --space=value, --verbose=value, --task-filter=value,42--stacktrace-depth=value, --require-start=value, --require-end=value,43--reject-start=value, --reject-end=value, --ignore-gfp-wait=value44See Documentation/fault-injection/fault-injection.rst for more45information4647failslab options:48--cache-filter=value4950fail_page_alloc options:51--ignore-gfp-highmem=value, --min-order=value5253ENVIRONMENT54FAILCMD_TYPE55The following values for FAILCMD_TYPE are recognized:5657failslab58inject slab allocation failures59fail_page_alloc60inject page allocation failures6162If FAILCMD_TYPE is not defined, then failslab is used.63EOF64}6566exit_if_not_hex() {67local value="$1"68if ! [[ $value =~ ^0x[0-9a-fA-F]+$ ]]; then69echo "Error: The provided value '$value' is not a valid hexadecimal number." >&270exit 171fi72}7374if [ $UID != 0 ]; then75echo must be run as root >&276exit 177fi7879DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3}'`8081if [ ! -d "$DEBUGFS" ]; then82echo debugfs is not mounted >&283exit 184fi8586FAILCMD_TYPE=${FAILCMD_TYPE:-failslab}87FAULTATTR=$DEBUGFS/$FAILCMD_TYPE8889if [ ! -d $FAULTATTR ]; then90echo $FAILCMD_TYPE is not available >&291exit 192fi9394LONGOPTS=probability:,interval:,times:,space:,verbose:,task-filter:95LONGOPTS=$LONGOPTS,stacktrace-depth:,require-start:,require-end:96LONGOPTS=$LONGOPTS,reject-start:,reject-end:,oom-kill-allocating-task:,help9798if [ $FAILCMD_TYPE = failslab ]; then99LONGOPTS=$LONGOPTS,ignore-gfp-wait:,cache-filter:100elif [ $FAILCMD_TYPE = fail_page_alloc ]; then101LONGOPTS=$LONGOPTS,ignore-gfp-wait:,ignore-gfp-highmem:,min-order:102fi103104TEMP=`getopt -o p:i:t:s:v:h --long $LONGOPTS -n 'failcmd.sh' -- "$@"`105106if [ $? != 0 ]; then107usage108exit 1109fi110111eval set -- "$TEMP"112113fault_attr_default()114{115echo N > $FAULTATTR/task-filter116echo 0 > $FAULTATTR/probability117echo 1 > $FAULTATTR/times118}119120fault_attr_default121122oom_kill_allocating_task_saved=`cat /proc/sys/vm/oom_kill_allocating_task`123124restore_values()125{126fault_attr_default127echo $oom_kill_allocating_task_saved \128> /proc/sys/vm/oom_kill_allocating_task129}130131#132# Default options133#134declare -i oom_kill_allocating_task=1135declare task_filter=Y136declare -i probability=1137declare -i times=1138139while true; do140case "$1" in141-p|--probability)142probability=$2143shift 2144;;145-i|--interval)146echo $2 > $FAULTATTR/interval147shift 2148;;149-t|--times)150times=$2151shift 2152;;153-s|--space)154echo $2 > $FAULTATTR/space155shift 2156;;157-v|--verbose)158echo $2 > $FAULTATTR/verbose159shift 2160;;161--task-filter)162task_filter=$2163shift 2164;;165--stacktrace-depth)166echo $2 > $FAULTATTR/stacktrace-depth167shift 2168;;169--require-start)170exit_if_not_hex "$2"171echo $2 > $FAULTATTR/require-start172shift 2173;;174--require-end)175exit_if_not_hex "$2"176echo $2 > $FAULTATTR/require-end177shift 2178;;179--reject-start)180exit_if_not_hex "$2"181echo $2 > $FAULTATTR/reject-start182shift 2183;;184--reject-end)185exit_if_not_hex "$2"186echo $2 > $FAULTATTR/reject-end187shift 2188;;189--oom-kill-allocating-task)190oom_kill_allocating_task=$2191shift 2192;;193--ignore-gfp-wait)194echo $2 > $FAULTATTR/ignore-gfp-wait195shift 2196;;197--cache-filter)198echo $2 > $FAULTATTR/cache_filter199shift 2200;;201--ignore-gfp-highmem)202echo $2 > $FAULTATTR/ignore-gfp-highmem203shift 2204;;205--min-order)206echo $2 > $FAULTATTR/min-order207shift 2208;;209-h|--help)210usage211exit 0212shift213;;214--)215shift216break217;;218esac219done220221[ -z "$1" ] && exit 0222223echo $oom_kill_allocating_task > /proc/sys/vm/oom_kill_allocating_task224echo $task_filter > $FAULTATTR/task-filter225echo $probability > $FAULTATTR/probability226echo $times > $FAULTATTR/times227228trap "restore_values" SIGINT SIGTERM EXIT229230cmd="echo 1 > /proc/self/make-it-fail && exec $@"231bash -c "$cmd"232233234