1 ===========================================
2 Fault injection capabilities infrastructure
3 ===========================================
5 See also drivers/md/md-faulty.c and "every_nth" module option for scsi_debug.
8 Available fault injection capabilities
9 --------------------------------------
13 injects slab allocation failures. (kmalloc(), kmem_cache_alloc(), ...)
17 injects page allocation failures. (alloc_pages(), get_free_pages(), ...)
21 injects failures in user memory access functions. (copy_from_user(), get_user(), ...)
25 injects futex deadlock and uaddr fault errors.
29 injects kernel RPC client and server failures.
33 injects disk IO errors on devices permitted by setting
34 /sys/block/<device>/make-it-fail or
35 /sys/block/<device>/<partition>/make-it-fail. (submit_bio_noacct())
39 injects MMC data errors on devices permitted by setting
40 debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
44 injects error return on specific functions, which are marked by
45 ALLOW_ERROR_INJECTION() macro, by setting debugfs entries
46 under /sys/kernel/debug/fail_function. No boot option supported.
48 - NVMe fault injection
50 inject NVMe status code and retry flag on devices permitted by setting
51 debugfs entries under /sys/kernel/debug/nvme*/fault_inject. The default
52 status code is NVME_SC_INVALID_OPCODE with no retry. The status code and
53 retry flag can be set via the debugfs.
55 - Null test block driver fault injection
57 inject IO timeouts by setting config items under
58 /sys/kernel/config/nullb/<disk>/timeout_inject,
59 inject requeue requests by setting config items under
60 /sys/kernel/config/nullb/<disk>/requeue_inject, and
61 inject init_hctx() errors by setting config items under
62 /sys/kernel/config/nullb/<disk>/init_hctx_fault_inject.
64 Configure fault-injection capabilities behavior
65 -----------------------------------------------
70 fault-inject-debugfs kernel module provides some debugfs entries for runtime
71 configuration of fault-injection capabilities.
73 - /sys/kernel/debug/fail*/probability:
75 likelihood of failure injection, in percent.
79 Note that one-failure-per-hundred is a very high error rate
80 for some testcases. Consider setting probability=100 and configure
81 /sys/kernel/debug/fail*/interval for such testcases.
83 - /sys/kernel/debug/fail*/interval:
85 specifies the interval between failures, for calls to
86 should_fail() that pass all the other tests.
88 Note that if you enable this, by setting interval>1, you will
89 probably want to set probability=100.
91 - /sys/kernel/debug/fail*/times:
93 specifies how many times failures may happen at most. A value of -1
96 - /sys/kernel/debug/fail*/space:
98 specifies an initial resource "budget", decremented by "size"
99 on each call to should_fail(,size). Failure injection is
100 suppressed until "space" reaches zero.
102 - /sys/kernel/debug/fail*/verbose
104 Format: { 0 | 1 | 2 }
106 specifies the verbosity of the messages when failure is
107 injected. '0' means no messages; '1' will print only a single
108 log line per failure; '2' will print a call trace too -- useful
109 to debug the problems revealed by fault injection.
111 - /sys/kernel/debug/fail*/task-filter:
113 Format: { 'Y' | 'N' }
115 A value of 'N' disables filtering by process (default).
116 Any positive value limits failures to only processes indicated by
117 /proc/<pid>/make-it-fail==1.
119 - /sys/kernel/debug/fail*/require-start,
120 /sys/kernel/debug/fail*/require-end,
121 /sys/kernel/debug/fail*/reject-start,
122 /sys/kernel/debug/fail*/reject-end:
124 specifies the range of virtual addresses tested during
125 stacktrace walking. Failure is injected only if some caller
126 in the walked stacktrace lies within the required range, and
127 none lies within the rejected range.
128 Default required range is [0,ULONG_MAX) (whole of virtual address space).
129 Default rejected range is [0,0).
131 - /sys/kernel/debug/fail*/stacktrace-depth:
133 specifies the maximum stacktrace depth walked during search
134 for a caller within [require-start,require-end) OR
135 [reject-start,reject-end).
137 - /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
139 Format: { 'Y' | 'N' }
141 default is 'Y', setting it to 'N' will also inject failures into
142 highmem/user allocations (__GFP_HIGHMEM allocations).
144 - /sys/kernel/debug/failslab/cache-filter
145 Format: { 'Y' | 'N' }
147 default is 'N', setting it to 'Y' will only inject failures when
148 objects are requests from certain caches.
150 Select the cache by writing '1' to /sys/kernel/slab/<cache>/failslab:
152 - /sys/kernel/debug/failslab/ignore-gfp-wait:
153 - /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
155 Format: { 'Y' | 'N' }
157 default is 'Y', setting it to 'N' will also inject failures
158 into allocations that can sleep (__GFP_DIRECT_RECLAIM allocations).
160 - /sys/kernel/debug/fail_page_alloc/min-order:
162 specifies the minimum page allocation order to be injected
165 - /sys/kernel/debug/fail_futex/ignore-private:
167 Format: { 'Y' | 'N' }
169 default is 'N', setting it to 'Y' will disable failure injections
170 when dealing with private (address space) futexes.
172 - /sys/kernel/debug/fail_sunrpc/ignore-client-disconnect:
174 Format: { 'Y' | 'N' }
176 default is 'N', setting it to 'Y' will disable disconnect
177 injection on the RPC client.
179 - /sys/kernel/debug/fail_sunrpc/ignore-server-disconnect:
181 Format: { 'Y' | 'N' }
183 default is 'N', setting it to 'Y' will disable disconnect
184 injection on the RPC server.
186 - /sys/kernel/debug/fail_sunrpc/ignore-cache-wait:
188 Format: { 'Y' | 'N' }
190 default is 'N', setting it to 'Y' will disable cache wait
191 injection on the RPC server.
193 - /sys/kernel/debug/fail_function/inject:
195 Format: { 'function-name' | '!function-name' | '' }
197 specifies the target function of error injection by name.
198 If the function name leads '!' prefix, given function is
199 removed from injection list. If nothing specified ('')
200 injection list is cleared.
202 - /sys/kernel/debug/fail_function/injectable:
204 (read only) shows error injectable functions and what type of
205 error values can be specified. The error type will be one of
207 - NULL: retval must be 0.
208 - ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
209 - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
211 - /sys/kernel/debug/fail_function/<function-name>/retval:
213 specifies the "error" return value to inject to the given function.
214 This will be created when the user specifies a new injection entry.
215 Note that this file only accepts unsigned values. So, if you want to
216 use a negative errno, you better use 'printf' instead of 'echo', e.g.:
217 $ printf %#x -12 > retval
222 In order to inject faults while debugfs is not available (early boot time),
223 use the boot option::
230 mmc_core.fail_request=<interval>,<probability>,<space>,<times>
235 - /proc/<pid>/fail-nth,
236 /proc/self/task/<tid>/fail-nth:
238 Write to this file of integer N makes N-th call in the task fail.
239 Read from this file returns a integer value. A value of '0' indicates
240 that the fault setup with a previous write to this file was injected.
241 A positive integer N indicates that the fault wasn't yet injected.
242 Note that this file enables all types of faults (slab, futex, etc).
243 This setting takes precedence over all other generic debugfs settings
244 like probability, interval, times, etc. But per-capability settings
245 (e.g. fail_futex/ignore-private) take precedence over it.
247 This feature is intended for systematic testing of faults in a single
248 system call. See an example below.
251 Error Injectable Functions
252 --------------------------
254 This part is for the kernel developers considering to add a function to
255 ALLOW_ERROR_INJECTION() macro.
257 Requirements for the Error Injectable Functions
258 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
260 Since the function-level error injection forcibly changes the code path
261 and returns an error even if the input and conditions are proper, this can
262 cause unexpected kernel crash if you allow error injection on the function
263 which is NOT error injectable. Thus, you (and reviewers) must ensure;
265 - The function returns an error code if it fails, and the callers must check
266 it correctly (need to recover from it).
268 - The function does not execute any code which can change any state before
269 the first error return. The state includes global or local, or input
270 variable. For example, clear output address storage (e.g. `*ret = NULL`),
271 increments/decrements counter, set a flag, preempt/irq disable or get
272 a lock (if those are recovered before returning error, that will be OK.)
274 The first requirement is important, and it will result in that the release
275 (free objects) functions are usually harder to inject errors than allocate
276 functions. If errors of such release functions are not correctly handled
277 it will cause a memory leak easily (the caller will confuse that the object
278 has been released or corrupted.)
280 The second one is for the caller which expects the function should always
281 does something. Thus if the function error injection skips whole of the
282 function, the expectation is betrayed and causes an unexpected error.
284 Type of the Error Injectable Functions
285 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287 Each error injectable functions will have the error type specified by the
288 ALLOW_ERROR_INJECTION() macro. You have to choose it carefully if you add
289 a new error injectable function. If the wrong error type is chosen, the
290 kernel may crash because it may not be able to handle the error.
291 There are 4 types of errors defined in include/asm-generic/error-injection.h
294 This function will return `NULL` if it fails. e.g. return an allocated
298 This function will return an `-errno` error code if it fails. e.g. return
299 -EINVAL if the input is wrong. This will include the functions which will
300 return an address which encodes `-errno` by ERR_PTR() macro.
303 This function will return an `-errno` or `NULL` if it fails. If the caller
304 of this function checks the return value with IS_ERR_OR_NULL() macro, this
305 type will be appropriate.
308 This function will return `true` (non-zero positive value) if it fails.
310 If you specifies a wrong type, for example, EI_TYPE_ERRNO for the function
311 which returns an allocated object, it may cause a problem because the returned
312 value is not an object address and the caller can not access to the address.
315 How to add new fault injection capability
316 -----------------------------------------
318 - #include <linux/fault-inject.h>
320 - define the fault attributes
322 DECLARE_FAULT_ATTR(name);
324 Please see the definition of struct fault_attr in fault-inject.h
327 - provide a way to configure fault attributes
331 If you need to enable the fault injection capability from boot time, you can
332 provide boot option to configure it. There is a helper function for it:
334 setup_fault_attr(attr, str);
338 failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
341 fault_create_debugfs_attr(name, parent, attr);
345 If the scope of the fault injection capability is limited to a
346 single kernel module, it is better to provide module parameters to
347 configure the fault attributes.
349 - add a hook to insert failures
351 Upon should_fail() returning true, client code should inject a failure:
353 should_fail(attr, size);
358 - Inject slab allocation failures into module init/exit code::
363 echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
364 echo 10 > /sys/kernel/debug/$FAILTYPE/probability
365 echo 100 > /sys/kernel/debug/$FAILTYPE/interval
366 echo -1 > /sys/kernel/debug/$FAILTYPE/times
367 echo 0 > /sys/kernel/debug/$FAILTYPE/space
368 echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
369 echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
373 bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
378 echo "Usage: $0 modulename [ modulename ... ]"
385 faulty_system modprobe $m
388 faulty_system modprobe -r $m
391 ------------------------------------------------------------------------------
393 - Inject page allocation failures only for a specific module::
397 FAILTYPE=fail_page_alloc
402 echo "Usage: $0 <modulename>"
408 if [ ! -d /sys/module/$module/sections ]
410 echo Module $module is not loaded
414 cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
415 cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
417 echo N > /sys/kernel/debug/$FAILTYPE/task-filter
418 echo 10 > /sys/kernel/debug/$FAILTYPE/probability
419 echo 100 > /sys/kernel/debug/$FAILTYPE/interval
420 echo -1 > /sys/kernel/debug/$FAILTYPE/times
421 echo 0 > /sys/kernel/debug/$FAILTYPE/space
422 echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
423 echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
424 echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
425 echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
427 trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
429 echo "Injecting errors into the module $module... (interrupt to stop)"
432 ------------------------------------------------------------------------------
434 - Inject open_ctree error while btrfs mount::
439 dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
440 DEVICE=$(losetup --show -f testfile.img)
441 mkfs.btrfs -f $DEVICE
444 FAILTYPE=fail_function
446 echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
447 printf %#x -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
448 echo N > /sys/kernel/debug/$FAILTYPE/task-filter
449 echo 100 > /sys/kernel/debug/$FAILTYPE/probability
450 echo 0 > /sys/kernel/debug/$FAILTYPE/interval
451 echo -1 > /sys/kernel/debug/$FAILTYPE/times
452 echo 0 > /sys/kernel/debug/$FAILTYPE/space
453 echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
455 mount -t btrfs $DEVICE tmpmnt
464 echo > /sys/kernel/debug/$FAILTYPE/inject
470 ------------------------------------------------------------------------------
472 - Inject only skbuff allocation failures ::
474 # mark skbuff_head_cache as faulty
475 echo 1 > /sys/kernel/slab/skbuff_head_cache/failslab
476 # Turn on cache filter (off by default)
477 echo 1 > /sys/kernel/debug/failslab/cache-filter
478 # Turn on fault injection
479 echo 1 > /sys/kernel/debug/failslab/times
480 echo 1 > /sys/kernel/debug/failslab/probability
483 Tool to run command with failslab or fail_page_alloc
484 ----------------------------------------------------
485 In order to make it easier to accomplish the tasks mentioned above, we can use
486 tools/testing/fault-injection/failcmd.sh. Please run a command
487 "./tools/testing/fault-injection/failcmd.sh --help" for more information and
488 see the following examples.
492 Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
495 # ./tools/testing/fault-injection/failcmd.sh \
496 -- make -C tools/testing/selftests/ run_tests
498 Same as above except to specify 100 times failures at most instead of one time
501 # ./tools/testing/fault-injection/failcmd.sh --times=100 \
502 -- make -C tools/testing/selftests/ run_tests
504 Same as above except to inject page allocation failure instead of slab
507 # env FAILCMD_TYPE=fail_page_alloc \
508 ./tools/testing/fault-injection/failcmd.sh --times=100 \
509 -- make -C tools/testing/selftests/ run_tests
511 Systematic faults using fail-nth
512 ---------------------------------
514 The following code systematically faults 0-th, 1-st, 2-nd and so on
515 capabilities in the socketpair() system call::
517 #include <sys/types.h>
518 #include <sys/stat.h>
519 #include <sys/socket.h>
520 #include <sys/syscall.h>
530 int i, err, res, fail_nth, fds[2];
533 system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
534 sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
535 fail_nth = open(buf, O_RDWR);
537 sprintf(buf, "%d", i);
538 write(fail_nth, buf, strlen(buf));
539 res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
541 pread(fail_nth, buf, sizeof(buf), 0);
546 printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
556 1-th fault Y: res=-1/23
557 2-th fault Y: res=-1/23
558 3-th fault Y: res=-1/12
559 4-th fault Y: res=-1/12
560 5-th fault Y: res=-1/23
561 6-th fault Y: res=-1/23
562 7-th fault Y: res=-1/23
563 8-th fault Y: res=-1/12
564 9-th fault Y: res=-1/12
565 10-th fault Y: res=-1/12
566 11-th fault Y: res=-1/12
567 12-th fault Y: res=-1/12
568 13-th fault Y: res=-1/12
569 14-th fault Y: res=-1/12
570 15-th fault Y: res=-1/12
571 16-th fault N: res=0/12