blob: 8c47847755a684b0c8f08a002ee322a854cc8898 [file] [log] [blame]
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -03001===========================================
Akinobu Mitade1ba092006-12-08 02:39:42 -08002Fault injection capabilities infrastructure
3===========================================
4
Masanari Iida1892ce42017-12-23 01:41:21 +09005See also drivers/md/md-faulty.c and "every_nth" module option for scsi_debug.
Akinobu Mitade1ba092006-12-08 02:39:42 -08006
7
8Available fault injection capabilities
9--------------------------------------
10
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030011- failslab
Akinobu Mitade1ba092006-12-08 02:39:42 -080012
13 injects slab allocation failures. (kmalloc(), kmem_cache_alloc(), ...)
14
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030015- fail_page_alloc
Akinobu Mitade1ba092006-12-08 02:39:42 -080016
17 injects page allocation failures. (alloc_pages(), get_free_pages(), ...)
18
Albert van der Linde2c739ce2020-10-15 20:13:46 -070019- fail_usercopy
20
21 injects failures in user memory access functions. (copy_from_user(), get_user(), ...)
22
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030023- fail_futex
Davidlohr Buesoab51fba2015-06-29 23:26:02 -070024
25 injects futex deadlock and uaddr fault errors.
26
Chuck Lever400edd82021-08-09 14:01:50 -040027- fail_sunrpc
28
29 injects kernel RPC client and server failures.
30
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030031- fail_make_request
Akinobu Mitade1ba092006-12-08 02:39:42 -080032
Don Mullis5d0ffa22006-12-08 02:39:50 -080033 injects disk IO errors on devices permitted by setting
Akinobu Mitade1ba092006-12-08 02:39:42 -080034 /sys/block/<device>/make-it-fail or
Christoph Hellwiged00aab2020-07-01 10:59:44 +020035 /sys/block/<device>/<partition>/make-it-fail. (submit_bio_noacct())
Akinobu Mitade1ba092006-12-08 02:39:42 -080036
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030037- fail_mmc_request
Per Forlin1e4cb222011-08-19 14:52:38 +020038
39 injects MMC data errors on devices permitted by setting
40 debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
41
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030042- fail_function
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +090043
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.
47
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030048- NVMe fault injection
Thomas Taicf4182f2018-02-08 13:38:30 -050049
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.
54
55
Akinobu Mitade1ba092006-12-08 02:39:42 -080056Configure fault-injection capabilities behavior
57-----------------------------------------------
58
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030059debugfs entries
60^^^^^^^^^^^^^^^
Akinobu Mitade1ba092006-12-08 02:39:42 -080061
62fault-inject-debugfs kernel module provides some debugfs entries for runtime
63configuration of fault-injection capabilities.
64
GeunSik Lim156f5a72009-06-02 15:01:37 +090065- /sys/kernel/debug/fail*/probability:
Akinobu Mitade1ba092006-12-08 02:39:42 -080066
67 likelihood of failure injection, in percent.
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030068
Akinobu Mitade1ba092006-12-08 02:39:42 -080069 Format: <percent>
70
Don Mullis5d0ffa22006-12-08 02:39:50 -080071 Note that one-failure-per-hundred is a very high error rate
72 for some testcases. Consider setting probability=100 and configure
GeunSik Lim156f5a72009-06-02 15:01:37 +090073 /sys/kernel/debug/fail*/interval for such testcases.
Akinobu Mitade1ba092006-12-08 02:39:42 -080074
GeunSik Lim156f5a72009-06-02 15:01:37 +090075- /sys/kernel/debug/fail*/interval:
Akinobu Mitade1ba092006-12-08 02:39:42 -080076
77 specifies the interval between failures, for calls to
78 should_fail() that pass all the other tests.
79
80 Note that if you enable this, by setting interval>1, you will
81 probably want to set probability=100.
82
GeunSik Lim156f5a72009-06-02 15:01:37 +090083- /sys/kernel/debug/fail*/times:
Akinobu Mitade1ba092006-12-08 02:39:42 -080084
Wolfram Sang005747522021-06-03 14:58:41 +020085 specifies how many times failures may happen at most. A value of -1
Akinobu Mita4662d8e2022-09-20 02:24:18 +090086 means "no limit".
Akinobu Mitade1ba092006-12-08 02:39:42 -080087
GeunSik Lim156f5a72009-06-02 15:01:37 +090088- /sys/kernel/debug/fail*/space:
Akinobu Mitade1ba092006-12-08 02:39:42 -080089
90 specifies an initial resource "budget", decremented by "size"
91 on each call to should_fail(,size). Failure injection is
92 suppressed until "space" reaches zero.
93
GeunSik Lim156f5a72009-06-02 15:01:37 +090094- /sys/kernel/debug/fail*/verbose
Akinobu Mitade1ba092006-12-08 02:39:42 -080095
96 Format: { 0 | 1 | 2 }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030097
Don Mullis5d0ffa22006-12-08 02:39:50 -080098 specifies the verbosity of the messages when failure is
99 injected. '0' means no messages; '1' will print only a single
100 log line per failure; '2' will print a call trace too -- useful
101 to debug the problems revealed by fault injection.
Akinobu Mitade1ba092006-12-08 02:39:42 -0800102
GeunSik Lim156f5a72009-06-02 15:01:37 +0900103- /sys/kernel/debug/fail*/task-filter:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800104
Don Mullis5d0ffa22006-12-08 02:39:50 -0800105 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300106
Don Mullis5d0ffa22006-12-08 02:39:50 -0800107 A value of 'N' disables filtering by process (default).
Akinobu Mitade1ba092006-12-08 02:39:42 -0800108 Any positive value limits failures to only processes indicated by
109 /proc/<pid>/make-it-fail==1.
110
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300111- /sys/kernel/debug/fail*/require-start,
112 /sys/kernel/debug/fail*/require-end,
113 /sys/kernel/debug/fail*/reject-start,
114 /sys/kernel/debug/fail*/reject-end:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800115
116 specifies the range of virtual addresses tested during
117 stacktrace walking. Failure is injected only if some caller
Akinobu Mita329409a2006-12-08 02:39:48 -0800118 in the walked stacktrace lies within the required range, and
119 none lies within the rejected range.
120 Default required range is [0,ULONG_MAX) (whole of virtual address space).
121 Default rejected range is [0,0).
Akinobu Mitade1ba092006-12-08 02:39:42 -0800122
GeunSik Lim156f5a72009-06-02 15:01:37 +0900123- /sys/kernel/debug/fail*/stacktrace-depth:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800124
125 specifies the maximum stacktrace depth walked during search
Don Mullis5d0ffa22006-12-08 02:39:50 -0800126 for a caller within [require-start,require-end) OR
127 [reject-start,reject-end).
Akinobu Mitade1ba092006-12-08 02:39:42 -0800128
GeunSik Lim156f5a72009-06-02 15:01:37 +0900129- /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800130
Don Mullis5d0ffa22006-12-08 02:39:50 -0800131 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300132
Don Mullis5d0ffa22006-12-08 02:39:50 -0800133 default is 'N', setting it to 'Y' won't inject failures into
Akinobu Mitade1ba092006-12-08 02:39:42 -0800134 highmem/user allocations.
135
GeunSik Lim156f5a72009-06-02 15:01:37 +0900136- /sys/kernel/debug/failslab/ignore-gfp-wait:
137- /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800138
Don Mullis5d0ffa22006-12-08 02:39:50 -0800139 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300140
Don Mullis5d0ffa22006-12-08 02:39:50 -0800141 default is 'N', setting it to 'Y' will inject failures
Akinobu Mitade1ba092006-12-08 02:39:42 -0800142 only into non-sleep allocations (GFP_ATOMIC allocations).
143
GeunSik Lim156f5a72009-06-02 15:01:37 +0900144- /sys/kernel/debug/fail_page_alloc/min-order:
Akinobu Mita54114992007-07-15 23:40:23 -0700145
146 specifies the minimum page allocation order to be injected
147 failures.
148
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700149- /sys/kernel/debug/fail_futex/ignore-private:
150
151 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300152
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700153 default is 'N', setting it to 'Y' will disable failure injections
154 when dealing with private (address space) futexes.
155
Chuck Lever400edd82021-08-09 14:01:50 -0400156- /sys/kernel/debug/fail_sunrpc/ignore-client-disconnect:
157
158 Format: { 'Y' | 'N' }
159
160 default is 'N', setting it to 'Y' will disable disconnect
161 injection on the RPC client.
162
163- /sys/kernel/debug/fail_sunrpc/ignore-server-disconnect:
164
165 Format: { 'Y' | 'N' }
166
167 default is 'N', setting it to 'Y' will disable disconnect
168 injection on the RPC server.
169
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900170- /sys/kernel/debug/fail_function/inject:
171
172 Format: { 'function-name' | '!function-name' | '' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300173
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900174 specifies the target function of error injection by name.
175 If the function name leads '!' prefix, given function is
176 removed from injection list. If nothing specified ('')
177 injection list is cleared.
178
179- /sys/kernel/debug/fail_function/injectable:
180
181 (read only) shows error injectable functions and what type of
182 error values can be specified. The error type will be one of
183 below;
184 - NULL: retval must be 0.
185 - ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
186 - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
187
Wolfram Sang005747522021-06-03 14:58:41 +0200188- /sys/kernel/debug/fail_function/<function-name>/retval:
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900189
Wolfram Sang005747522021-06-03 14:58:41 +0200190 specifies the "error" return value to inject to the given function.
191 This will be created when the user specifies a new injection entry.
192 Note that this file only accepts unsigned values. So, if you want to
193 use a negative errno, you better use 'printf' instead of 'echo', e.g.:
194 $ printf %#x -12 > retval
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900195
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300196Boot option
197^^^^^^^^^^^
Akinobu Mitade1ba092006-12-08 02:39:42 -0800198
199In order to inject faults while debugfs is not available (early boot time),
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300200use the boot option::
Akinobu Mitade1ba092006-12-08 02:39:42 -0800201
202 failslab=
203 fail_page_alloc=
Albert van der Linde2c739ce2020-10-15 20:13:46 -0700204 fail_usercopy=
Per Forlin1e4cb222011-08-19 14:52:38 +0200205 fail_make_request=
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700206 fail_futex=
Per Forlin199e3f42011-09-13 23:03:30 +0200207 mmc_core.fail_request=<interval>,<probability>,<space>,<times>
Akinobu Mitade1ba092006-12-08 02:39:42 -0800208
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300209proc entries
210^^^^^^^^^^^^
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700211
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300212- /proc/<pid>/fail-nth,
213 /proc/self/task/<tid>/fail-nth:
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700214
Akinobu Mita9049f2f2017-07-14 14:49:52 -0700215 Write to this file of integer N makes N-th call in the task fail.
Akinobu Mitabfc74092017-07-14 14:49:54 -0700216 Read from this file returns a integer value. A value of '0' indicates
217 that the fault setup with a previous write to this file was injected.
218 A positive integer N indicates that the fault wasn't yet injected.
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700219 Note that this file enables all types of faults (slab, futex, etc).
220 This setting takes precedence over all other generic debugfs settings
221 like probability, interval, times, etc. But per-capability settings
222 (e.g. fail_futex/ignore-private) take precedence over it.
223
224 This feature is intended for systematic testing of faults in a single
225 system call. See an example below.
226
Akinobu Mitade1ba092006-12-08 02:39:42 -0800227How to add new fault injection capability
228-----------------------------------------
229
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300230- #include <linux/fault-inject.h>
Akinobu Mitade1ba092006-12-08 02:39:42 -0800231
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300232- define the fault attributes
Akinobu Mitade1ba092006-12-08 02:39:42 -0800233
Laurent Gauthier2d879482019-01-05 00:08:34 +0100234 DECLARE_FAULT_ATTR(name);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800235
236 Please see the definition of struct fault_attr in fault-inject.h
237 for details.
238
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300239- provide a way to configure fault attributes
Akinobu Mitade1ba092006-12-08 02:39:42 -0800240
241- boot option
242
243 If you need to enable the fault injection capability from boot time, you can
Don Mullis5d0ffa22006-12-08 02:39:50 -0800244 provide boot option to configure it. There is a helper function for it:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800245
Don Mullis5d0ffa22006-12-08 02:39:50 -0800246 setup_fault_attr(attr, str);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800247
248- debugfs entries
249
Albert van der Linde2c739ce2020-10-15 20:13:46 -0700250 failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
Don Mullis5d0ffa22006-12-08 02:39:50 -0800251 Helper functions:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800252
Akinobu Mitadd48c082011-08-03 16:21:01 -0700253 fault_create_debugfs_attr(name, parent, attr);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800254
255- module parameters
256
257 If the scope of the fault injection capability is limited to a
258 single kernel module, it is better to provide module parameters to
259 configure the fault attributes.
260
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300261- add a hook to insert failures
Akinobu Mitade1ba092006-12-08 02:39:42 -0800262
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300263 Upon should_fail() returning true, client code should inject a failure:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800264
Don Mullis5d0ffa22006-12-08 02:39:50 -0800265 should_fail(attr, size);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800266
267Application Examples
268--------------------
269
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300270- Inject slab allocation failures into module init/exit code::
Akinobu Mitade1ba092006-12-08 02:39:42 -0800271
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300272 #!/bin/bash
Akinobu Mitade1ba092006-12-08 02:39:42 -0800273
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300274 FAILTYPE=failslab
275 echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
276 echo 10 > /sys/kernel/debug/$FAILTYPE/probability
277 echo 100 > /sys/kernel/debug/$FAILTYPE/interval
Akinobu Mita4662d8e2022-09-20 02:24:18 +0900278 echo -1 > /sys/kernel/debug/$FAILTYPE/times
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300279 echo 0 > /sys/kernel/debug/$FAILTYPE/space
280 echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
281 echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
Akinobu Mitade1ba092006-12-08 02:39:42 -0800282
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300283 faulty_system()
284 {
Akinobu Mita18584872007-07-15 23:40:24 -0700285 bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300286 }
Akinobu Mitade1ba092006-12-08 02:39:42 -0800287
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300288 if [ $# -eq 0 ]
289 then
Akinobu Mita18584872007-07-15 23:40:24 -0700290 echo "Usage: $0 modulename [ modulename ... ]"
291 exit 1
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300292 fi
Akinobu Mitade1ba092006-12-08 02:39:42 -0800293
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300294 for m in $*
295 do
Akinobu Mita18584872007-07-15 23:40:24 -0700296 echo inserting $m...
297 faulty_system modprobe $m
Akinobu Mitade1ba092006-12-08 02:39:42 -0800298
Akinobu Mita18584872007-07-15 23:40:24 -0700299 echo removing $m...
300 faulty_system modprobe -r $m
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300301 done
Akinobu Mitade1ba092006-12-08 02:39:42 -0800302
303------------------------------------------------------------------------------
304
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300305- Inject page allocation failures only for a specific module::
Akinobu Mitade1ba092006-12-08 02:39:42 -0800306
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300307 #!/bin/bash
Akinobu Mitade1ba092006-12-08 02:39:42 -0800308
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300309 FAILTYPE=fail_page_alloc
310 module=$1
Akinobu Mitade1ba092006-12-08 02:39:42 -0800311
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300312 if [ -z $module ]
313 then
Akinobu Mita18584872007-07-15 23:40:24 -0700314 echo "Usage: $0 <modulename>"
315 exit 1
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300316 fi
Akinobu Mitade1ba092006-12-08 02:39:42 -0800317
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300318 modprobe $module
Akinobu Mitade1ba092006-12-08 02:39:42 -0800319
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300320 if [ ! -d /sys/module/$module/sections ]
321 then
Akinobu Mita18584872007-07-15 23:40:24 -0700322 echo Module $module is not loaded
323 exit 1
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300324 fi
Akinobu Mita18584872007-07-15 23:40:24 -0700325
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300326 cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
327 cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
Akinobu Mita18584872007-07-15 23:40:24 -0700328
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300329 echo N > /sys/kernel/debug/$FAILTYPE/task-filter
330 echo 10 > /sys/kernel/debug/$FAILTYPE/probability
331 echo 100 > /sys/kernel/debug/$FAILTYPE/interval
Akinobu Mita4662d8e2022-09-20 02:24:18 +0900332 echo -1 > /sys/kernel/debug/$FAILTYPE/times
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300333 echo 0 > /sys/kernel/debug/$FAILTYPE/space
334 echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
335 echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
336 echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
337 echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
Akinobu Mita18584872007-07-15 23:40:24 -0700338
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300339 trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
Akinobu Mita18584872007-07-15 23:40:24 -0700340
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300341 echo "Injecting errors into the module $module... (interrupt to stop)"
342 sleep 1000000
Akinobu Mitade1ba092006-12-08 02:39:42 -0800343
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900344------------------------------------------------------------------------------
345
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300346- Inject open_ctree error while btrfs mount::
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900347
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300348 #!/bin/bash
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900349
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300350 rm -f testfile.img
351 dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
352 DEVICE=$(losetup --show -f testfile.img)
353 mkfs.btrfs -f $DEVICE
354 mkdir -p tmpmnt
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900355
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300356 FAILTYPE=fail_function
357 FAILFUNC=open_ctree
358 echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
Wolfram Sang005747522021-06-03 14:58:41 +0200359 printf %#x -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300360 echo N > /sys/kernel/debug/$FAILTYPE/task-filter
361 echo 100 > /sys/kernel/debug/$FAILTYPE/probability
362 echo 0 > /sys/kernel/debug/$FAILTYPE/interval
Akinobu Mita4662d8e2022-09-20 02:24:18 +0900363 echo -1 > /sys/kernel/debug/$FAILTYPE/times
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300364 echo 0 > /sys/kernel/debug/$FAILTYPE/space
365 echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900366
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300367 mount -t btrfs $DEVICE tmpmnt
368 if [ $? -ne 0 ]
369 then
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900370 echo "SUCCESS!"
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300371 else
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900372 echo "FAILED!"
373 umount tmpmnt
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300374 fi
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900375
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300376 echo > /sys/kernel/debug/$FAILTYPE/inject
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900377
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300378 rmdir tmpmnt
379 losetup -d $DEVICE
380 rm testfile.img
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900381
382
Akinobu Mitac24aa642012-07-30 14:43:20 -0700383Tool to run command with failslab or fail_page_alloc
384----------------------------------------------------
385In order to make it easier to accomplish the tasks mentioned above, we can use
386tools/testing/fault-injection/failcmd.sh. Please run a command
387"./tools/testing/fault-injection/failcmd.sh --help" for more information and
388see the following examples.
389
390Examples:
391
392Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300393allocation failure::
Akinobu Mitac24aa642012-07-30 14:43:20 -0700394
395 # ./tools/testing/fault-injection/failcmd.sh \
396 -- make -C tools/testing/selftests/ run_tests
397
398Same as above except to specify 100 times failures at most instead of one time
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300399at most by default::
Akinobu Mitac24aa642012-07-30 14:43:20 -0700400
401 # ./tools/testing/fault-injection/failcmd.sh --times=100 \
402 -- make -C tools/testing/selftests/ run_tests
403
404Same as above except to inject page allocation failure instead of slab
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300405allocation failure::
Akinobu Mitac24aa642012-07-30 14:43:20 -0700406
407 # env FAILCMD_TYPE=fail_page_alloc \
408 ./tools/testing/fault-injection/failcmd.sh --times=100 \
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300409 -- make -C tools/testing/selftests/ run_tests
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700410
411Systematic faults using fail-nth
412---------------------------------
413
414The following code systematically faults 0-th, 1-st, 2-nd and so on
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300415capabilities in the socketpair() system call::
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700416
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300417 #include <sys/types.h>
418 #include <sys/stat.h>
419 #include <sys/socket.h>
420 #include <sys/syscall.h>
421 #include <fcntl.h>
422 #include <unistd.h>
423 #include <string.h>
424 #include <stdlib.h>
425 #include <stdio.h>
426 #include <errno.h>
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700427
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300428 int main()
429 {
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700430 int i, err, res, fail_nth, fds[2];
431 char buf[128];
432
433 system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
434 sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
435 fail_nth = open(buf, O_RDWR);
Akinobu Mita9049f2f2017-07-14 14:49:52 -0700436 for (i = 1;; i++) {
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700437 sprintf(buf, "%d", i);
438 write(fail_nth, buf, strlen(buf));
439 res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
440 err = errno;
Akinobu Mitabfc74092017-07-14 14:49:54 -0700441 pread(fail_nth, buf, sizeof(buf), 0);
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700442 if (res == 0) {
443 close(fds[0]);
444 close(fds[1]);
445 }
Akinobu Mitabfc74092017-07-14 14:49:54 -0700446 printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
447 res, err);
448 if (atoi(buf))
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700449 break;
450 }
451 return 0;
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300452 }
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700453
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300454An example output::
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700455
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300456 1-th fault Y: res=-1/23
457 2-th fault Y: res=-1/23
458 3-th fault Y: res=-1/12
459 4-th fault Y: res=-1/12
460 5-th fault Y: res=-1/23
461 6-th fault Y: res=-1/23
462 7-th fault Y: res=-1/23
463 8-th fault Y: res=-1/12
464 9-th fault Y: res=-1/12
465 10-th fault Y: res=-1/12
466 11-th fault Y: res=-1/12
467 12-th fault Y: res=-1/12
468 13-th fault Y: res=-1/12
469 14-th fault Y: res=-1/12
470 15-th fault Y: res=-1/12
471 16-th fault N: res=0/12