Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # NAME
0005 #       failcmd.sh - run a command with injecting slab/page allocation failures
0006 #
0007 # SYNOPSIS
0008 #       failcmd.sh --help
0009 #       failcmd.sh [<options>] command [arguments]
0010 #
0011 # DESCRIPTION
0012 #       Run command with injecting slab/page allocation failures by fault
0013 #       injection.
0014 #
0015 #       NOTE: you need to run this script as root.
0016 #
0017 
0018 usage()
0019 {
0020         cat >&2 <<EOF
0021 Usage: $0 [options] command [arguments]
0022 
0023 OPTIONS
0024         -p percent
0025         --probability=percent
0026                 likelihood of failure injection, in percent.
0027                 Default value is 1
0028 
0029         -t value
0030         --times=value
0031                 specifies how many times failures may happen at most.
0032                 Default value is 1
0033 
0034         --oom-kill-allocating-task=value
0035                 set /proc/sys/vm/oom_kill_allocating_task to specified value
0036                 before running the command.
0037                 Default value is 1
0038 
0039         -h, --help
0040                 Display a usage message and exit
0041 
0042         --interval=value, --space=value, --verbose=value, --task-filter=value,
0043         --stacktrace-depth=value, --require-start=value, --require-end=value,
0044         --reject-start=value, --reject-end=value, --ignore-gfp-wait=value
0045                 See Documentation/fault-injection/fault-injection.rst for more
0046                 information
0047 
0048         failslab options:
0049         --cache-filter=value
0050 
0051         fail_page_alloc options:
0052         --ignore-gfp-highmem=value, --min-order=value
0053 
0054 ENVIRONMENT
0055         FAILCMD_TYPE
0056                 The following values for FAILCMD_TYPE are recognized:
0057 
0058                 failslab
0059                         inject slab allocation failures
0060                 fail_page_alloc
0061                         inject page allocation failures
0062 
0063                 If FAILCMD_TYPE is not defined, then failslab is used.
0064 EOF
0065 }
0066 
0067 if [ $UID != 0 ]; then
0068         echo must be run as root >&2
0069         exit 1
0070 fi
0071 
0072 DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3}'`
0073 
0074 if [ ! -d "$DEBUGFS" ]; then
0075         echo debugfs is not mounted >&2
0076         exit 1
0077 fi
0078 
0079 FAILCMD_TYPE=${FAILCMD_TYPE:-failslab}
0080 FAULTATTR=$DEBUGFS/$FAILCMD_TYPE
0081 
0082 if [ ! -d $FAULTATTR ]; then
0083         echo $FAILCMD_TYPE is not available >&2
0084         exit 1
0085 fi
0086 
0087 LONGOPTS=probability:,interval:,times:,space:,verbose:,task-filter:
0088 LONGOPTS=$LONGOPTS,stacktrace-depth:,require-start:,require-end:
0089 LONGOPTS=$LONGOPTS,reject-start:,reject-end:,oom-kill-allocating-task:,help
0090 
0091 if [ $FAILCMD_TYPE = failslab ]; then
0092         LONGOPTS=$LONGOPTS,ignore-gfp-wait:,cache-filter:
0093 elif [ $FAILCMD_TYPE = fail_page_alloc ]; then
0094         LONGOPTS=$LONGOPTS,ignore-gfp-wait:,ignore-gfp-highmem:,min-order:
0095 fi
0096 
0097 TEMP=`getopt -o p:i:t:s:v:h --long $LONGOPTS -n 'failcmd.sh' -- "$@"`
0098 
0099 if [ $? != 0 ]; then
0100         usage
0101         exit 1
0102 fi
0103 
0104 eval set -- "$TEMP"
0105 
0106 fault_attr_default()
0107 {
0108         echo N > $FAULTATTR/task-filter
0109         echo 0 > $FAULTATTR/probability
0110         echo 1 > $FAULTATTR/times
0111 }
0112 
0113 fault_attr_default
0114 
0115 oom_kill_allocating_task_saved=`cat /proc/sys/vm/oom_kill_allocating_task`
0116 
0117 restore_values()
0118 {
0119         fault_attr_default
0120         echo $oom_kill_allocating_task_saved \
0121                 > /proc/sys/vm/oom_kill_allocating_task
0122 }
0123 
0124 #
0125 # Default options
0126 #
0127 declare -i oom_kill_allocating_task=1
0128 declare task_filter=Y
0129 declare -i probability=1
0130 declare -i times=1
0131 
0132 while true; do
0133         case "$1" in
0134         -p|--probability)
0135                 probability=$2
0136                 shift 2
0137                 ;;
0138         -i|--interval)
0139                 echo $2 > $FAULTATTR/interval
0140                 shift 2
0141                 ;;
0142         -t|--times)
0143                 times=$2
0144                 shift 2
0145                 ;;
0146         -s|--space)
0147                 echo $2 > $FAULTATTR/space
0148                 shift 2
0149                 ;;
0150         -v|--verbose)
0151                 echo $2 > $FAULTATTR/verbose
0152                 shift 2
0153                 ;;
0154         --task-filter)
0155                 task_filter=$2
0156                 shift 2
0157                 ;;
0158         --stacktrace-depth)
0159                 echo $2 > $FAULTATTR/stacktrace-depth
0160                 shift 2
0161                 ;;
0162         --require-start)
0163                 echo $2 > $FAULTATTR/require-start
0164                 shift 2
0165                 ;;
0166         --require-end)
0167                 echo $2 > $FAULTATTR/require-end
0168                 shift 2
0169                 ;;
0170         --reject-start)
0171                 echo $2 > $FAULTATTR/reject-start
0172                 shift 2
0173                 ;;
0174         --reject-end)
0175                 echo $2 > $FAULTATTR/reject-end
0176                 shift 2
0177                 ;;
0178         --oom-kill-allocating-task)
0179                 oom_kill_allocating_task=$2
0180                 shift 2
0181                 ;;
0182         --ignore-gfp-wait)
0183                 echo $2 > $FAULTATTR/ignore-gfp-wait
0184                 shift 2
0185                 ;;
0186         --cache-filter)
0187                 echo $2 > $FAULTATTR/cache_filter
0188                 shift 2
0189                 ;;
0190         --ignore-gfp-highmem)
0191                 echo $2 > $FAULTATTR/ignore-gfp-highmem
0192                 shift 2
0193                 ;;
0194         --min-order)
0195                 echo $2 > $FAULTATTR/min-order
0196                 shift 2
0197                 ;;
0198         -h|--help)
0199                 usage
0200                 exit 0
0201                 shift
0202                 ;;
0203         --)
0204                 shift
0205                 break
0206                 ;;
0207         esac
0208 done
0209 
0210 [ -z "$1" ] && exit 0
0211 
0212 echo $oom_kill_allocating_task > /proc/sys/vm/oom_kill_allocating_task
0213 echo $task_filter > $FAULTATTR/task-filter
0214 echo $probability > $FAULTATTR/probability
0215 echo $times > $FAULTATTR/times
0216 
0217 trap "restore_values" SIGINT SIGTERM EXIT
0218 
0219 cmd="echo 1 > /proc/self/make-it-fail && exec $@"
0220 bash -c "$cmd"