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