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