xref: /linux/Documentation/fault-injection/fault-injection.rst (revision ed00aabd5eb9fb44d6aff1173234a2e911b9fead)
110ffebbeSMauro Carvalho Chehab===========================================
210ffebbeSMauro Carvalho ChehabFault injection capabilities infrastructure
310ffebbeSMauro Carvalho Chehab===========================================
410ffebbeSMauro Carvalho Chehab
510ffebbeSMauro Carvalho ChehabSee also drivers/md/md-faulty.c and "every_nth" module option for scsi_debug.
610ffebbeSMauro Carvalho Chehab
710ffebbeSMauro Carvalho Chehab
810ffebbeSMauro Carvalho ChehabAvailable fault injection capabilities
910ffebbeSMauro Carvalho Chehab--------------------------------------
1010ffebbeSMauro Carvalho Chehab
1110ffebbeSMauro Carvalho Chehab- failslab
1210ffebbeSMauro Carvalho Chehab
1310ffebbeSMauro Carvalho Chehab  injects slab allocation failures. (kmalloc(), kmem_cache_alloc(), ...)
1410ffebbeSMauro Carvalho Chehab
1510ffebbeSMauro Carvalho Chehab- fail_page_alloc
1610ffebbeSMauro Carvalho Chehab
1710ffebbeSMauro Carvalho Chehab  injects page allocation failures. (alloc_pages(), get_free_pages(), ...)
1810ffebbeSMauro Carvalho Chehab
1910ffebbeSMauro Carvalho Chehab- fail_futex
2010ffebbeSMauro Carvalho Chehab
2110ffebbeSMauro Carvalho Chehab  injects futex deadlock and uaddr fault errors.
2210ffebbeSMauro Carvalho Chehab
2310ffebbeSMauro Carvalho Chehab- fail_make_request
2410ffebbeSMauro Carvalho Chehab
2510ffebbeSMauro Carvalho Chehab  injects disk IO errors on devices permitted by setting
2610ffebbeSMauro Carvalho Chehab  /sys/block/<device>/make-it-fail or
27*ed00aabdSChristoph Hellwig  /sys/block/<device>/<partition>/make-it-fail. (submit_bio_noacct())
2810ffebbeSMauro Carvalho Chehab
2910ffebbeSMauro Carvalho Chehab- fail_mmc_request
3010ffebbeSMauro Carvalho Chehab
3110ffebbeSMauro Carvalho Chehab  injects MMC data errors on devices permitted by setting
3210ffebbeSMauro Carvalho Chehab  debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
3310ffebbeSMauro Carvalho Chehab
3410ffebbeSMauro Carvalho Chehab- fail_function
3510ffebbeSMauro Carvalho Chehab
3610ffebbeSMauro Carvalho Chehab  injects error return on specific functions, which are marked by
3710ffebbeSMauro Carvalho Chehab  ALLOW_ERROR_INJECTION() macro, by setting debugfs entries
3810ffebbeSMauro Carvalho Chehab  under /sys/kernel/debug/fail_function. No boot option supported.
3910ffebbeSMauro Carvalho Chehab
4010ffebbeSMauro Carvalho Chehab- NVMe fault injection
4110ffebbeSMauro Carvalho Chehab
4210ffebbeSMauro Carvalho Chehab  inject NVMe status code and retry flag on devices permitted by setting
4310ffebbeSMauro Carvalho Chehab  debugfs entries under /sys/kernel/debug/nvme*/fault_inject. The default
4410ffebbeSMauro Carvalho Chehab  status code is NVME_SC_INVALID_OPCODE with no retry. The status code and
4510ffebbeSMauro Carvalho Chehab  retry flag can be set via the debugfs.
4610ffebbeSMauro Carvalho Chehab
4710ffebbeSMauro Carvalho Chehab
4810ffebbeSMauro Carvalho ChehabConfigure fault-injection capabilities behavior
4910ffebbeSMauro Carvalho Chehab-----------------------------------------------
5010ffebbeSMauro Carvalho Chehab
5110ffebbeSMauro Carvalho Chehabdebugfs entries
5210ffebbeSMauro Carvalho Chehab^^^^^^^^^^^^^^^
5310ffebbeSMauro Carvalho Chehab
5410ffebbeSMauro Carvalho Chehabfault-inject-debugfs kernel module provides some debugfs entries for runtime
5510ffebbeSMauro Carvalho Chehabconfiguration of fault-injection capabilities.
5610ffebbeSMauro Carvalho Chehab
5710ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/probability:
5810ffebbeSMauro Carvalho Chehab
5910ffebbeSMauro Carvalho Chehab	likelihood of failure injection, in percent.
6010ffebbeSMauro Carvalho Chehab
6110ffebbeSMauro Carvalho Chehab	Format: <percent>
6210ffebbeSMauro Carvalho Chehab
6310ffebbeSMauro Carvalho Chehab	Note that one-failure-per-hundred is a very high error rate
6410ffebbeSMauro Carvalho Chehab	for some testcases.  Consider setting probability=100 and configure
6510ffebbeSMauro Carvalho Chehab	/sys/kernel/debug/fail*/interval for such testcases.
6610ffebbeSMauro Carvalho Chehab
6710ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/interval:
6810ffebbeSMauro Carvalho Chehab
6910ffebbeSMauro Carvalho Chehab	specifies the interval between failures, for calls to
7010ffebbeSMauro Carvalho Chehab	should_fail() that pass all the other tests.
7110ffebbeSMauro Carvalho Chehab
7210ffebbeSMauro Carvalho Chehab	Note that if you enable this, by setting interval>1, you will
7310ffebbeSMauro Carvalho Chehab	probably want to set probability=100.
7410ffebbeSMauro Carvalho Chehab
7510ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/times:
7610ffebbeSMauro Carvalho Chehab
7710ffebbeSMauro Carvalho Chehab	specifies how many times failures may happen at most.
7810ffebbeSMauro Carvalho Chehab	A value of -1 means "no limit".
7910ffebbeSMauro Carvalho Chehab
8010ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/space:
8110ffebbeSMauro Carvalho Chehab
8210ffebbeSMauro Carvalho Chehab	specifies an initial resource "budget", decremented by "size"
8310ffebbeSMauro Carvalho Chehab	on each call to should_fail(,size).  Failure injection is
8410ffebbeSMauro Carvalho Chehab	suppressed until "space" reaches zero.
8510ffebbeSMauro Carvalho Chehab
8610ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/verbose
8710ffebbeSMauro Carvalho Chehab
8810ffebbeSMauro Carvalho Chehab	Format: { 0 | 1 | 2 }
8910ffebbeSMauro Carvalho Chehab
9010ffebbeSMauro Carvalho Chehab	specifies the verbosity of the messages when failure is
9110ffebbeSMauro Carvalho Chehab	injected.  '0' means no messages; '1' will print only a single
9210ffebbeSMauro Carvalho Chehab	log line per failure; '2' will print a call trace too -- useful
9310ffebbeSMauro Carvalho Chehab	to debug the problems revealed by fault injection.
9410ffebbeSMauro Carvalho Chehab
9510ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/task-filter:
9610ffebbeSMauro Carvalho Chehab
9710ffebbeSMauro Carvalho Chehab	Format: { 'Y' | 'N' }
9810ffebbeSMauro Carvalho Chehab
9910ffebbeSMauro Carvalho Chehab	A value of 'N' disables filtering by process (default).
10010ffebbeSMauro Carvalho Chehab	Any positive value limits failures to only processes indicated by
10110ffebbeSMauro Carvalho Chehab	/proc/<pid>/make-it-fail==1.
10210ffebbeSMauro Carvalho Chehab
10310ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/require-start,
10410ffebbeSMauro Carvalho Chehab  /sys/kernel/debug/fail*/require-end,
10510ffebbeSMauro Carvalho Chehab  /sys/kernel/debug/fail*/reject-start,
10610ffebbeSMauro Carvalho Chehab  /sys/kernel/debug/fail*/reject-end:
10710ffebbeSMauro Carvalho Chehab
10810ffebbeSMauro Carvalho Chehab	specifies the range of virtual addresses tested during
10910ffebbeSMauro Carvalho Chehab	stacktrace walking.  Failure is injected only if some caller
11010ffebbeSMauro Carvalho Chehab	in the walked stacktrace lies within the required range, and
11110ffebbeSMauro Carvalho Chehab	none lies within the rejected range.
11210ffebbeSMauro Carvalho Chehab	Default required range is [0,ULONG_MAX) (whole of virtual address space).
11310ffebbeSMauro Carvalho Chehab	Default rejected range is [0,0).
11410ffebbeSMauro Carvalho Chehab
11510ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail*/stacktrace-depth:
11610ffebbeSMauro Carvalho Chehab
11710ffebbeSMauro Carvalho Chehab	specifies the maximum stacktrace depth walked during search
11810ffebbeSMauro Carvalho Chehab	for a caller within [require-start,require-end) OR
11910ffebbeSMauro Carvalho Chehab	[reject-start,reject-end).
12010ffebbeSMauro Carvalho Chehab
12110ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
12210ffebbeSMauro Carvalho Chehab
12310ffebbeSMauro Carvalho Chehab	Format: { 'Y' | 'N' }
12410ffebbeSMauro Carvalho Chehab
12510ffebbeSMauro Carvalho Chehab	default is 'N', setting it to 'Y' won't inject failures into
12610ffebbeSMauro Carvalho Chehab	highmem/user allocations.
12710ffebbeSMauro Carvalho Chehab
12810ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/failslab/ignore-gfp-wait:
12910ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
13010ffebbeSMauro Carvalho Chehab
13110ffebbeSMauro Carvalho Chehab	Format: { 'Y' | 'N' }
13210ffebbeSMauro Carvalho Chehab
13310ffebbeSMauro Carvalho Chehab	default is 'N', setting it to 'Y' will inject failures
13410ffebbeSMauro Carvalho Chehab	only into non-sleep allocations (GFP_ATOMIC allocations).
13510ffebbeSMauro Carvalho Chehab
13610ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_page_alloc/min-order:
13710ffebbeSMauro Carvalho Chehab
13810ffebbeSMauro Carvalho Chehab	specifies the minimum page allocation order to be injected
13910ffebbeSMauro Carvalho Chehab	failures.
14010ffebbeSMauro Carvalho Chehab
14110ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_futex/ignore-private:
14210ffebbeSMauro Carvalho Chehab
14310ffebbeSMauro Carvalho Chehab	Format: { 'Y' | 'N' }
14410ffebbeSMauro Carvalho Chehab
14510ffebbeSMauro Carvalho Chehab	default is 'N', setting it to 'Y' will disable failure injections
14610ffebbeSMauro Carvalho Chehab	when dealing with private (address space) futexes.
14710ffebbeSMauro Carvalho Chehab
14810ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_function/inject:
14910ffebbeSMauro Carvalho Chehab
15010ffebbeSMauro Carvalho Chehab	Format: { 'function-name' | '!function-name' | '' }
15110ffebbeSMauro Carvalho Chehab
15210ffebbeSMauro Carvalho Chehab	specifies the target function of error injection by name.
15310ffebbeSMauro Carvalho Chehab	If the function name leads '!' prefix, given function is
15410ffebbeSMauro Carvalho Chehab	removed from injection list. If nothing specified ('')
15510ffebbeSMauro Carvalho Chehab	injection list is cleared.
15610ffebbeSMauro Carvalho Chehab
15710ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_function/injectable:
15810ffebbeSMauro Carvalho Chehab
15910ffebbeSMauro Carvalho Chehab	(read only) shows error injectable functions and what type of
16010ffebbeSMauro Carvalho Chehab	error values can be specified. The error type will be one of
16110ffebbeSMauro Carvalho Chehab	below;
16210ffebbeSMauro Carvalho Chehab	- NULL:	retval must be 0.
16310ffebbeSMauro Carvalho Chehab	- ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
16410ffebbeSMauro Carvalho Chehab	- ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
16510ffebbeSMauro Carvalho Chehab
16610ffebbeSMauro Carvalho Chehab- /sys/kernel/debug/fail_function/<functiuon-name>/retval:
16710ffebbeSMauro Carvalho Chehab
16810ffebbeSMauro Carvalho Chehab	specifies the "error" return value to inject to the given
16910ffebbeSMauro Carvalho Chehab	function for given function. This will be created when
17010ffebbeSMauro Carvalho Chehab	user specifies new injection entry.
17110ffebbeSMauro Carvalho Chehab
17210ffebbeSMauro Carvalho ChehabBoot option
17310ffebbeSMauro Carvalho Chehab^^^^^^^^^^^
17410ffebbeSMauro Carvalho Chehab
17510ffebbeSMauro Carvalho ChehabIn order to inject faults while debugfs is not available (early boot time),
17610ffebbeSMauro Carvalho Chehabuse the boot option::
17710ffebbeSMauro Carvalho Chehab
17810ffebbeSMauro Carvalho Chehab	failslab=
17910ffebbeSMauro Carvalho Chehab	fail_page_alloc=
18010ffebbeSMauro Carvalho Chehab	fail_make_request=
18110ffebbeSMauro Carvalho Chehab	fail_futex=
18210ffebbeSMauro Carvalho Chehab	mmc_core.fail_request=<interval>,<probability>,<space>,<times>
18310ffebbeSMauro Carvalho Chehab
18410ffebbeSMauro Carvalho Chehabproc entries
18510ffebbeSMauro Carvalho Chehab^^^^^^^^^^^^
18610ffebbeSMauro Carvalho Chehab
18710ffebbeSMauro Carvalho Chehab- /proc/<pid>/fail-nth,
18810ffebbeSMauro Carvalho Chehab  /proc/self/task/<tid>/fail-nth:
18910ffebbeSMauro Carvalho Chehab
19010ffebbeSMauro Carvalho Chehab	Write to this file of integer N makes N-th call in the task fail.
19110ffebbeSMauro Carvalho Chehab	Read from this file returns a integer value. A value of '0' indicates
19210ffebbeSMauro Carvalho Chehab	that the fault setup with a previous write to this file was injected.
19310ffebbeSMauro Carvalho Chehab	A positive integer N indicates that the fault wasn't yet injected.
19410ffebbeSMauro Carvalho Chehab	Note that this file enables all types of faults (slab, futex, etc).
19510ffebbeSMauro Carvalho Chehab	This setting takes precedence over all other generic debugfs settings
19610ffebbeSMauro Carvalho Chehab	like probability, interval, times, etc. But per-capability settings
19710ffebbeSMauro Carvalho Chehab	(e.g. fail_futex/ignore-private) take precedence over it.
19810ffebbeSMauro Carvalho Chehab
19910ffebbeSMauro Carvalho Chehab	This feature is intended for systematic testing of faults in a single
20010ffebbeSMauro Carvalho Chehab	system call. See an example below.
20110ffebbeSMauro Carvalho Chehab
20210ffebbeSMauro Carvalho ChehabHow to add new fault injection capability
20310ffebbeSMauro Carvalho Chehab-----------------------------------------
20410ffebbeSMauro Carvalho Chehab
20510ffebbeSMauro Carvalho Chehab- #include <linux/fault-inject.h>
20610ffebbeSMauro Carvalho Chehab
20710ffebbeSMauro Carvalho Chehab- define the fault attributes
20810ffebbeSMauro Carvalho Chehab
20910ffebbeSMauro Carvalho Chehab  DECLARE_FAULT_ATTR(name);
21010ffebbeSMauro Carvalho Chehab
21110ffebbeSMauro Carvalho Chehab  Please see the definition of struct fault_attr in fault-inject.h
21210ffebbeSMauro Carvalho Chehab  for details.
21310ffebbeSMauro Carvalho Chehab
21410ffebbeSMauro Carvalho Chehab- provide a way to configure fault attributes
21510ffebbeSMauro Carvalho Chehab
21610ffebbeSMauro Carvalho Chehab- boot option
21710ffebbeSMauro Carvalho Chehab
21810ffebbeSMauro Carvalho Chehab  If you need to enable the fault injection capability from boot time, you can
21910ffebbeSMauro Carvalho Chehab  provide boot option to configure it. There is a helper function for it:
22010ffebbeSMauro Carvalho Chehab
22110ffebbeSMauro Carvalho Chehab	setup_fault_attr(attr, str);
22210ffebbeSMauro Carvalho Chehab
22310ffebbeSMauro Carvalho Chehab- debugfs entries
22410ffebbeSMauro Carvalho Chehab
22510ffebbeSMauro Carvalho Chehab  failslab, fail_page_alloc, and fail_make_request use this way.
22610ffebbeSMauro Carvalho Chehab  Helper functions:
22710ffebbeSMauro Carvalho Chehab
22810ffebbeSMauro Carvalho Chehab	fault_create_debugfs_attr(name, parent, attr);
22910ffebbeSMauro Carvalho Chehab
23010ffebbeSMauro Carvalho Chehab- module parameters
23110ffebbeSMauro Carvalho Chehab
23210ffebbeSMauro Carvalho Chehab  If the scope of the fault injection capability is limited to a
23310ffebbeSMauro Carvalho Chehab  single kernel module, it is better to provide module parameters to
23410ffebbeSMauro Carvalho Chehab  configure the fault attributes.
23510ffebbeSMauro Carvalho Chehab
23610ffebbeSMauro Carvalho Chehab- add a hook to insert failures
23710ffebbeSMauro Carvalho Chehab
23810ffebbeSMauro Carvalho Chehab  Upon should_fail() returning true, client code should inject a failure:
23910ffebbeSMauro Carvalho Chehab
24010ffebbeSMauro Carvalho Chehab	should_fail(attr, size);
24110ffebbeSMauro Carvalho Chehab
24210ffebbeSMauro Carvalho ChehabApplication Examples
24310ffebbeSMauro Carvalho Chehab--------------------
24410ffebbeSMauro Carvalho Chehab
24510ffebbeSMauro Carvalho Chehab- Inject slab allocation failures into module init/exit code::
24610ffebbeSMauro Carvalho Chehab
24710ffebbeSMauro Carvalho Chehab    #!/bin/bash
24810ffebbeSMauro Carvalho Chehab
24910ffebbeSMauro Carvalho Chehab    FAILTYPE=failslab
25010ffebbeSMauro Carvalho Chehab    echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
25110ffebbeSMauro Carvalho Chehab    echo 10 > /sys/kernel/debug/$FAILTYPE/probability
25210ffebbeSMauro Carvalho Chehab    echo 100 > /sys/kernel/debug/$FAILTYPE/interval
25310ffebbeSMauro Carvalho Chehab    echo -1 > /sys/kernel/debug/$FAILTYPE/times
25410ffebbeSMauro Carvalho Chehab    echo 0 > /sys/kernel/debug/$FAILTYPE/space
25510ffebbeSMauro Carvalho Chehab    echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
25610ffebbeSMauro Carvalho Chehab    echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
25710ffebbeSMauro Carvalho Chehab
25810ffebbeSMauro Carvalho Chehab    faulty_system()
25910ffebbeSMauro Carvalho Chehab    {
26010ffebbeSMauro Carvalho Chehab	bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
26110ffebbeSMauro Carvalho Chehab    }
26210ffebbeSMauro Carvalho Chehab
26310ffebbeSMauro Carvalho Chehab    if [ $# -eq 0 ]
26410ffebbeSMauro Carvalho Chehab    then
26510ffebbeSMauro Carvalho Chehab	echo "Usage: $0 modulename [ modulename ... ]"
26610ffebbeSMauro Carvalho Chehab	exit 1
26710ffebbeSMauro Carvalho Chehab    fi
26810ffebbeSMauro Carvalho Chehab
26910ffebbeSMauro Carvalho Chehab    for m in $*
27010ffebbeSMauro Carvalho Chehab    do
27110ffebbeSMauro Carvalho Chehab	echo inserting $m...
27210ffebbeSMauro Carvalho Chehab	faulty_system modprobe $m
27310ffebbeSMauro Carvalho Chehab
27410ffebbeSMauro Carvalho Chehab	echo removing $m...
27510ffebbeSMauro Carvalho Chehab	faulty_system modprobe -r $m
27610ffebbeSMauro Carvalho Chehab    done
27710ffebbeSMauro Carvalho Chehab
27810ffebbeSMauro Carvalho Chehab------------------------------------------------------------------------------
27910ffebbeSMauro Carvalho Chehab
28010ffebbeSMauro Carvalho Chehab- Inject page allocation failures only for a specific module::
28110ffebbeSMauro Carvalho Chehab
28210ffebbeSMauro Carvalho Chehab    #!/bin/bash
28310ffebbeSMauro Carvalho Chehab
28410ffebbeSMauro Carvalho Chehab    FAILTYPE=fail_page_alloc
28510ffebbeSMauro Carvalho Chehab    module=$1
28610ffebbeSMauro Carvalho Chehab
28710ffebbeSMauro Carvalho Chehab    if [ -z $module ]
28810ffebbeSMauro Carvalho Chehab    then
28910ffebbeSMauro Carvalho Chehab	echo "Usage: $0 <modulename>"
29010ffebbeSMauro Carvalho Chehab	exit 1
29110ffebbeSMauro Carvalho Chehab    fi
29210ffebbeSMauro Carvalho Chehab
29310ffebbeSMauro Carvalho Chehab    modprobe $module
29410ffebbeSMauro Carvalho Chehab
29510ffebbeSMauro Carvalho Chehab    if [ ! -d /sys/module/$module/sections ]
29610ffebbeSMauro Carvalho Chehab    then
29710ffebbeSMauro Carvalho Chehab	echo Module $module is not loaded
29810ffebbeSMauro Carvalho Chehab	exit 1
29910ffebbeSMauro Carvalho Chehab    fi
30010ffebbeSMauro Carvalho Chehab
30110ffebbeSMauro Carvalho Chehab    cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
30210ffebbeSMauro Carvalho Chehab    cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
30310ffebbeSMauro Carvalho Chehab
30410ffebbeSMauro Carvalho Chehab    echo N > /sys/kernel/debug/$FAILTYPE/task-filter
30510ffebbeSMauro Carvalho Chehab    echo 10 > /sys/kernel/debug/$FAILTYPE/probability
30610ffebbeSMauro Carvalho Chehab    echo 100 > /sys/kernel/debug/$FAILTYPE/interval
30710ffebbeSMauro Carvalho Chehab    echo -1 > /sys/kernel/debug/$FAILTYPE/times
30810ffebbeSMauro Carvalho Chehab    echo 0 > /sys/kernel/debug/$FAILTYPE/space
30910ffebbeSMauro Carvalho Chehab    echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
31010ffebbeSMauro Carvalho Chehab    echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
31110ffebbeSMauro Carvalho Chehab    echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
31210ffebbeSMauro Carvalho Chehab    echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
31310ffebbeSMauro Carvalho Chehab
31410ffebbeSMauro Carvalho Chehab    trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
31510ffebbeSMauro Carvalho Chehab
31610ffebbeSMauro Carvalho Chehab    echo "Injecting errors into the module $module... (interrupt to stop)"
31710ffebbeSMauro Carvalho Chehab    sleep 1000000
31810ffebbeSMauro Carvalho Chehab
31910ffebbeSMauro Carvalho Chehab------------------------------------------------------------------------------
32010ffebbeSMauro Carvalho Chehab
32110ffebbeSMauro Carvalho Chehab- Inject open_ctree error while btrfs mount::
32210ffebbeSMauro Carvalho Chehab
32310ffebbeSMauro Carvalho Chehab    #!/bin/bash
32410ffebbeSMauro Carvalho Chehab
32510ffebbeSMauro Carvalho Chehab    rm -f testfile.img
32610ffebbeSMauro Carvalho Chehab    dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
32710ffebbeSMauro Carvalho Chehab    DEVICE=$(losetup --show -f testfile.img)
32810ffebbeSMauro Carvalho Chehab    mkfs.btrfs -f $DEVICE
32910ffebbeSMauro Carvalho Chehab    mkdir -p tmpmnt
33010ffebbeSMauro Carvalho Chehab
33110ffebbeSMauro Carvalho Chehab    FAILTYPE=fail_function
33210ffebbeSMauro Carvalho Chehab    FAILFUNC=open_ctree
33310ffebbeSMauro Carvalho Chehab    echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
33410ffebbeSMauro Carvalho Chehab    echo -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
33510ffebbeSMauro Carvalho Chehab    echo N > /sys/kernel/debug/$FAILTYPE/task-filter
33610ffebbeSMauro Carvalho Chehab    echo 100 > /sys/kernel/debug/$FAILTYPE/probability
33710ffebbeSMauro Carvalho Chehab    echo 0 > /sys/kernel/debug/$FAILTYPE/interval
33810ffebbeSMauro Carvalho Chehab    echo -1 > /sys/kernel/debug/$FAILTYPE/times
33910ffebbeSMauro Carvalho Chehab    echo 0 > /sys/kernel/debug/$FAILTYPE/space
34010ffebbeSMauro Carvalho Chehab    echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
34110ffebbeSMauro Carvalho Chehab
34210ffebbeSMauro Carvalho Chehab    mount -t btrfs $DEVICE tmpmnt
34310ffebbeSMauro Carvalho Chehab    if [ $? -ne 0 ]
34410ffebbeSMauro Carvalho Chehab    then
34510ffebbeSMauro Carvalho Chehab	echo "SUCCESS!"
34610ffebbeSMauro Carvalho Chehab    else
34710ffebbeSMauro Carvalho Chehab	echo "FAILED!"
34810ffebbeSMauro Carvalho Chehab	umount tmpmnt
34910ffebbeSMauro Carvalho Chehab    fi
35010ffebbeSMauro Carvalho Chehab
35110ffebbeSMauro Carvalho Chehab    echo > /sys/kernel/debug/$FAILTYPE/inject
35210ffebbeSMauro Carvalho Chehab
35310ffebbeSMauro Carvalho Chehab    rmdir tmpmnt
35410ffebbeSMauro Carvalho Chehab    losetup -d $DEVICE
35510ffebbeSMauro Carvalho Chehab    rm testfile.img
35610ffebbeSMauro Carvalho Chehab
35710ffebbeSMauro Carvalho Chehab
35810ffebbeSMauro Carvalho ChehabTool to run command with failslab or fail_page_alloc
35910ffebbeSMauro Carvalho Chehab----------------------------------------------------
36010ffebbeSMauro Carvalho ChehabIn order to make it easier to accomplish the tasks mentioned above, we can use
36110ffebbeSMauro Carvalho Chehabtools/testing/fault-injection/failcmd.sh.  Please run a command
36210ffebbeSMauro Carvalho Chehab"./tools/testing/fault-injection/failcmd.sh --help" for more information and
36310ffebbeSMauro Carvalho Chehabsee the following examples.
36410ffebbeSMauro Carvalho Chehab
36510ffebbeSMauro Carvalho ChehabExamples:
36610ffebbeSMauro Carvalho Chehab
36710ffebbeSMauro Carvalho ChehabRun a command "make -C tools/testing/selftests/ run_tests" with injecting slab
36810ffebbeSMauro Carvalho Chehaballocation failure::
36910ffebbeSMauro Carvalho Chehab
37010ffebbeSMauro Carvalho Chehab	# ./tools/testing/fault-injection/failcmd.sh \
37110ffebbeSMauro Carvalho Chehab		-- make -C tools/testing/selftests/ run_tests
37210ffebbeSMauro Carvalho Chehab
37310ffebbeSMauro Carvalho ChehabSame as above except to specify 100 times failures at most instead of one time
37410ffebbeSMauro Carvalho Chehabat most by default::
37510ffebbeSMauro Carvalho Chehab
37610ffebbeSMauro Carvalho Chehab	# ./tools/testing/fault-injection/failcmd.sh --times=100 \
37710ffebbeSMauro Carvalho Chehab		-- make -C tools/testing/selftests/ run_tests
37810ffebbeSMauro Carvalho Chehab
37910ffebbeSMauro Carvalho ChehabSame as above except to inject page allocation failure instead of slab
38010ffebbeSMauro Carvalho Chehaballocation failure::
38110ffebbeSMauro Carvalho Chehab
38210ffebbeSMauro Carvalho Chehab	# env FAILCMD_TYPE=fail_page_alloc \
38310ffebbeSMauro Carvalho Chehab		./tools/testing/fault-injection/failcmd.sh --times=100 \
38410ffebbeSMauro Carvalho Chehab		-- make -C tools/testing/selftests/ run_tests
38510ffebbeSMauro Carvalho Chehab
38610ffebbeSMauro Carvalho ChehabSystematic faults using fail-nth
38710ffebbeSMauro Carvalho Chehab---------------------------------
38810ffebbeSMauro Carvalho Chehab
38910ffebbeSMauro Carvalho ChehabThe following code systematically faults 0-th, 1-st, 2-nd and so on
39010ffebbeSMauro Carvalho Chehabcapabilities in the socketpair() system call::
39110ffebbeSMauro Carvalho Chehab
39210ffebbeSMauro Carvalho Chehab  #include <sys/types.h>
39310ffebbeSMauro Carvalho Chehab  #include <sys/stat.h>
39410ffebbeSMauro Carvalho Chehab  #include <sys/socket.h>
39510ffebbeSMauro Carvalho Chehab  #include <sys/syscall.h>
39610ffebbeSMauro Carvalho Chehab  #include <fcntl.h>
39710ffebbeSMauro Carvalho Chehab  #include <unistd.h>
39810ffebbeSMauro Carvalho Chehab  #include <string.h>
39910ffebbeSMauro Carvalho Chehab  #include <stdlib.h>
40010ffebbeSMauro Carvalho Chehab  #include <stdio.h>
40110ffebbeSMauro Carvalho Chehab  #include <errno.h>
40210ffebbeSMauro Carvalho Chehab
40310ffebbeSMauro Carvalho Chehab  int main()
40410ffebbeSMauro Carvalho Chehab  {
40510ffebbeSMauro Carvalho Chehab	int i, err, res, fail_nth, fds[2];
40610ffebbeSMauro Carvalho Chehab	char buf[128];
40710ffebbeSMauro Carvalho Chehab
40810ffebbeSMauro Carvalho Chehab	system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
40910ffebbeSMauro Carvalho Chehab	sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
41010ffebbeSMauro Carvalho Chehab	fail_nth = open(buf, O_RDWR);
41110ffebbeSMauro Carvalho Chehab	for (i = 1;; i++) {
41210ffebbeSMauro Carvalho Chehab		sprintf(buf, "%d", i);
41310ffebbeSMauro Carvalho Chehab		write(fail_nth, buf, strlen(buf));
41410ffebbeSMauro Carvalho Chehab		res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
41510ffebbeSMauro Carvalho Chehab		err = errno;
41610ffebbeSMauro Carvalho Chehab		pread(fail_nth, buf, sizeof(buf), 0);
41710ffebbeSMauro Carvalho Chehab		if (res == 0) {
41810ffebbeSMauro Carvalho Chehab			close(fds[0]);
41910ffebbeSMauro Carvalho Chehab			close(fds[1]);
42010ffebbeSMauro Carvalho Chehab		}
42110ffebbeSMauro Carvalho Chehab		printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
42210ffebbeSMauro Carvalho Chehab			res, err);
42310ffebbeSMauro Carvalho Chehab		if (atoi(buf))
42410ffebbeSMauro Carvalho Chehab			break;
42510ffebbeSMauro Carvalho Chehab	}
42610ffebbeSMauro Carvalho Chehab	return 0;
42710ffebbeSMauro Carvalho Chehab  }
42810ffebbeSMauro Carvalho Chehab
42910ffebbeSMauro Carvalho ChehabAn example output::
43010ffebbeSMauro Carvalho Chehab
43110ffebbeSMauro Carvalho Chehab	1-th fault Y: res=-1/23
43210ffebbeSMauro Carvalho Chehab	2-th fault Y: res=-1/23
43310ffebbeSMauro Carvalho Chehab	3-th fault Y: res=-1/12
43410ffebbeSMauro Carvalho Chehab	4-th fault Y: res=-1/12
43510ffebbeSMauro Carvalho Chehab	5-th fault Y: res=-1/23
43610ffebbeSMauro Carvalho Chehab	6-th fault Y: res=-1/23
43710ffebbeSMauro Carvalho Chehab	7-th fault Y: res=-1/23
43810ffebbeSMauro Carvalho Chehab	8-th fault Y: res=-1/12
43910ffebbeSMauro Carvalho Chehab	9-th fault Y: res=-1/12
44010ffebbeSMauro Carvalho Chehab	10-th fault Y: res=-1/12
44110ffebbeSMauro Carvalho Chehab	11-th fault Y: res=-1/12
44210ffebbeSMauro Carvalho Chehab	12-th fault Y: res=-1/12
44310ffebbeSMauro Carvalho Chehab	13-th fault Y: res=-1/12
44410ffebbeSMauro Carvalho Chehab	14-th fault Y: res=-1/12
44510ffebbeSMauro Carvalho Chehab	15-th fault Y: res=-1/12
44610ffebbeSMauro Carvalho Chehab	16-th fault N: res=0/12
447