blob: 31ecfe44e5b454631e6163df80ab103499af66c5 [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
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030027- fail_make_request
Akinobu Mitade1ba092006-12-08 02:39:42 -080028
Don Mullis5d0ffa22006-12-08 02:39:50 -080029 injects disk IO errors on devices permitted by setting
Akinobu Mitade1ba092006-12-08 02:39:42 -080030 /sys/block/<device>/make-it-fail or
Christoph Hellwiged00aab2020-07-01 10:59:44 +020031 /sys/block/<device>/<partition>/make-it-fail. (submit_bio_noacct())
Akinobu Mitade1ba092006-12-08 02:39:42 -080032
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030033- fail_mmc_request
Per Forlin1e4cb222011-08-19 14:52:38 +020034
35 injects MMC data errors on devices permitted by setting
36 debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
37
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030038- fail_function
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +090039
40 injects error return on specific functions, which are marked by
41 ALLOW_ERROR_INJECTION() macro, by setting debugfs entries
42 under /sys/kernel/debug/fail_function. No boot option supported.
43
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030044- NVMe fault injection
Thomas Taicf4182f2018-02-08 13:38:30 -050045
46 inject NVMe status code and retry flag on devices permitted by setting
47 debugfs entries under /sys/kernel/debug/nvme*/fault_inject. The default
48 status code is NVME_SC_INVALID_OPCODE with no retry. The status code and
49 retry flag can be set via the debugfs.
50
51
Akinobu Mitade1ba092006-12-08 02:39:42 -080052Configure fault-injection capabilities behavior
53-----------------------------------------------
54
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030055debugfs entries
56^^^^^^^^^^^^^^^
Akinobu Mitade1ba092006-12-08 02:39:42 -080057
58fault-inject-debugfs kernel module provides some debugfs entries for runtime
59configuration of fault-injection capabilities.
60
GeunSik Lim156f5a72009-06-02 15:01:37 +090061- /sys/kernel/debug/fail*/probability:
Akinobu Mitade1ba092006-12-08 02:39:42 -080062
63 likelihood of failure injection, in percent.
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030064
Akinobu Mitade1ba092006-12-08 02:39:42 -080065 Format: <percent>
66
Don Mullis5d0ffa22006-12-08 02:39:50 -080067 Note that one-failure-per-hundred is a very high error rate
68 for some testcases. Consider setting probability=100 and configure
GeunSik Lim156f5a72009-06-02 15:01:37 +090069 /sys/kernel/debug/fail*/interval for such testcases.
Akinobu Mitade1ba092006-12-08 02:39:42 -080070
GeunSik Lim156f5a72009-06-02 15:01:37 +090071- /sys/kernel/debug/fail*/interval:
Akinobu Mitade1ba092006-12-08 02:39:42 -080072
73 specifies the interval between failures, for calls to
74 should_fail() that pass all the other tests.
75
76 Note that if you enable this, by setting interval>1, you will
77 probably want to set probability=100.
78
GeunSik Lim156f5a72009-06-02 15:01:37 +090079- /sys/kernel/debug/fail*/times:
Akinobu Mitade1ba092006-12-08 02:39:42 -080080
81 specifies how many times failures may happen at most.
82 A value of -1 means "no limit".
83
GeunSik Lim156f5a72009-06-02 15:01:37 +090084- /sys/kernel/debug/fail*/space:
Akinobu Mitade1ba092006-12-08 02:39:42 -080085
86 specifies an initial resource "budget", decremented by "size"
87 on each call to should_fail(,size). Failure injection is
88 suppressed until "space" reaches zero.
89
GeunSik Lim156f5a72009-06-02 15:01:37 +090090- /sys/kernel/debug/fail*/verbose
Akinobu Mitade1ba092006-12-08 02:39:42 -080091
92 Format: { 0 | 1 | 2 }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -030093
Don Mullis5d0ffa22006-12-08 02:39:50 -080094 specifies the verbosity of the messages when failure is
95 injected. '0' means no messages; '1' will print only a single
96 log line per failure; '2' will print a call trace too -- useful
97 to debug the problems revealed by fault injection.
Akinobu Mitade1ba092006-12-08 02:39:42 -080098
GeunSik Lim156f5a72009-06-02 15:01:37 +090099- /sys/kernel/debug/fail*/task-filter:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800100
Don Mullis5d0ffa22006-12-08 02:39:50 -0800101 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300102
Don Mullis5d0ffa22006-12-08 02:39:50 -0800103 A value of 'N' disables filtering by process (default).
Akinobu Mitade1ba092006-12-08 02:39:42 -0800104 Any positive value limits failures to only processes indicated by
105 /proc/<pid>/make-it-fail==1.
106
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300107- /sys/kernel/debug/fail*/require-start,
108 /sys/kernel/debug/fail*/require-end,
109 /sys/kernel/debug/fail*/reject-start,
110 /sys/kernel/debug/fail*/reject-end:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800111
112 specifies the range of virtual addresses tested during
113 stacktrace walking. Failure is injected only if some caller
Akinobu Mita329409a2006-12-08 02:39:48 -0800114 in the walked stacktrace lies within the required range, and
115 none lies within the rejected range.
116 Default required range is [0,ULONG_MAX) (whole of virtual address space).
117 Default rejected range is [0,0).
Akinobu Mitade1ba092006-12-08 02:39:42 -0800118
GeunSik Lim156f5a72009-06-02 15:01:37 +0900119- /sys/kernel/debug/fail*/stacktrace-depth:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800120
121 specifies the maximum stacktrace depth walked during search
Don Mullis5d0ffa22006-12-08 02:39:50 -0800122 for a caller within [require-start,require-end) OR
123 [reject-start,reject-end).
Akinobu Mitade1ba092006-12-08 02:39:42 -0800124
GeunSik Lim156f5a72009-06-02 15:01:37 +0900125- /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800126
Don Mullis5d0ffa22006-12-08 02:39:50 -0800127 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300128
Don Mullis5d0ffa22006-12-08 02:39:50 -0800129 default is 'N', setting it to 'Y' won't inject failures into
Akinobu Mitade1ba092006-12-08 02:39:42 -0800130 highmem/user allocations.
131
GeunSik Lim156f5a72009-06-02 15:01:37 +0900132- /sys/kernel/debug/failslab/ignore-gfp-wait:
133- /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800134
Don Mullis5d0ffa22006-12-08 02:39:50 -0800135 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300136
Don Mullis5d0ffa22006-12-08 02:39:50 -0800137 default is 'N', setting it to 'Y' will inject failures
Akinobu Mitade1ba092006-12-08 02:39:42 -0800138 only into non-sleep allocations (GFP_ATOMIC allocations).
139
GeunSik Lim156f5a72009-06-02 15:01:37 +0900140- /sys/kernel/debug/fail_page_alloc/min-order:
Akinobu Mita54114992007-07-15 23:40:23 -0700141
142 specifies the minimum page allocation order to be injected
143 failures.
144
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700145- /sys/kernel/debug/fail_futex/ignore-private:
146
147 Format: { 'Y' | 'N' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300148
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700149 default is 'N', setting it to 'Y' will disable failure injections
150 when dealing with private (address space) futexes.
151
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900152- /sys/kernel/debug/fail_function/inject:
153
154 Format: { 'function-name' | '!function-name' | '' }
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300155
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900156 specifies the target function of error injection by name.
157 If the function name leads '!' prefix, given function is
158 removed from injection list. If nothing specified ('')
159 injection list is cleared.
160
161- /sys/kernel/debug/fail_function/injectable:
162
163 (read only) shows error injectable functions and what type of
164 error values can be specified. The error type will be one of
165 below;
166 - NULL: retval must be 0.
167 - ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
168 - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
169
170- /sys/kernel/debug/fail_function/<functiuon-name>/retval:
171
172 specifies the "error" return value to inject to the given
173 function for given function. This will be created when
174 user specifies new injection entry.
175
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300176Boot option
177^^^^^^^^^^^
Akinobu Mitade1ba092006-12-08 02:39:42 -0800178
179In order to inject faults while debugfs is not available (early boot time),
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300180use the boot option::
Akinobu Mitade1ba092006-12-08 02:39:42 -0800181
182 failslab=
183 fail_page_alloc=
Albert van der Linde2c739ce2020-10-15 20:13:46 -0700184 fail_usercopy=
Per Forlin1e4cb222011-08-19 14:52:38 +0200185 fail_make_request=
Davidlohr Buesoab51fba2015-06-29 23:26:02 -0700186 fail_futex=
Per Forlin199e3f42011-09-13 23:03:30 +0200187 mmc_core.fail_request=<interval>,<probability>,<space>,<times>
Akinobu Mitade1ba092006-12-08 02:39:42 -0800188
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300189proc entries
190^^^^^^^^^^^^
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700191
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300192- /proc/<pid>/fail-nth,
193 /proc/self/task/<tid>/fail-nth:
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700194
Akinobu Mita9049f2f2017-07-14 14:49:52 -0700195 Write to this file of integer N makes N-th call in the task fail.
Akinobu Mitabfc74092017-07-14 14:49:54 -0700196 Read from this file returns a integer value. A value of '0' indicates
197 that the fault setup with a previous write to this file was injected.
198 A positive integer N indicates that the fault wasn't yet injected.
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700199 Note that this file enables all types of faults (slab, futex, etc).
200 This setting takes precedence over all other generic debugfs settings
201 like probability, interval, times, etc. But per-capability settings
202 (e.g. fail_futex/ignore-private) take precedence over it.
203
204 This feature is intended for systematic testing of faults in a single
205 system call. See an example below.
206
Akinobu Mitade1ba092006-12-08 02:39:42 -0800207How to add new fault injection capability
208-----------------------------------------
209
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300210- #include <linux/fault-inject.h>
Akinobu Mitade1ba092006-12-08 02:39:42 -0800211
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300212- define the fault attributes
Akinobu Mitade1ba092006-12-08 02:39:42 -0800213
Laurent Gauthier2d879482019-01-05 00:08:34 +0100214 DECLARE_FAULT_ATTR(name);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800215
216 Please see the definition of struct fault_attr in fault-inject.h
217 for details.
218
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300219- provide a way to configure fault attributes
Akinobu Mitade1ba092006-12-08 02:39:42 -0800220
221- boot option
222
223 If you need to enable the fault injection capability from boot time, you can
Don Mullis5d0ffa22006-12-08 02:39:50 -0800224 provide boot option to configure it. There is a helper function for it:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800225
Don Mullis5d0ffa22006-12-08 02:39:50 -0800226 setup_fault_attr(attr, str);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800227
228- debugfs entries
229
Albert van der Linde2c739ce2020-10-15 20:13:46 -0700230 failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
Don Mullis5d0ffa22006-12-08 02:39:50 -0800231 Helper functions:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800232
Akinobu Mitadd48c082011-08-03 16:21:01 -0700233 fault_create_debugfs_attr(name, parent, attr);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800234
235- module parameters
236
237 If the scope of the fault injection capability is limited to a
238 single kernel module, it is better to provide module parameters to
239 configure the fault attributes.
240
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300241- add a hook to insert failures
Akinobu Mitade1ba092006-12-08 02:39:42 -0800242
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300243 Upon should_fail() returning true, client code should inject a failure:
Akinobu Mitade1ba092006-12-08 02:39:42 -0800244
Don Mullis5d0ffa22006-12-08 02:39:50 -0800245 should_fail(attr, size);
Akinobu Mitade1ba092006-12-08 02:39:42 -0800246
247Application Examples
248--------------------
249
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300250- Inject slab allocation failures into module init/exit code::
Akinobu Mitade1ba092006-12-08 02:39:42 -0800251
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300252 #!/bin/bash
Akinobu Mitade1ba092006-12-08 02:39:42 -0800253
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300254 FAILTYPE=failslab
255 echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
256 echo 10 > /sys/kernel/debug/$FAILTYPE/probability
257 echo 100 > /sys/kernel/debug/$FAILTYPE/interval
258 echo -1 > /sys/kernel/debug/$FAILTYPE/times
259 echo 0 > /sys/kernel/debug/$FAILTYPE/space
260 echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
261 echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
Akinobu Mitade1ba092006-12-08 02:39:42 -0800262
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300263 faulty_system()
264 {
Akinobu Mita18584872007-07-15 23:40:24 -0700265 bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300266 }
Akinobu Mitade1ba092006-12-08 02:39:42 -0800267
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300268 if [ $# -eq 0 ]
269 then
Akinobu Mita18584872007-07-15 23:40:24 -0700270 echo "Usage: $0 modulename [ modulename ... ]"
271 exit 1
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300272 fi
Akinobu Mitade1ba092006-12-08 02:39:42 -0800273
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300274 for m in $*
275 do
Akinobu Mita18584872007-07-15 23:40:24 -0700276 echo inserting $m...
277 faulty_system modprobe $m
Akinobu Mitade1ba092006-12-08 02:39:42 -0800278
Akinobu Mita18584872007-07-15 23:40:24 -0700279 echo removing $m...
280 faulty_system modprobe -r $m
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300281 done
Akinobu Mitade1ba092006-12-08 02:39:42 -0800282
283------------------------------------------------------------------------------
284
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300285- Inject page allocation failures only for a specific module::
Akinobu Mitade1ba092006-12-08 02:39:42 -0800286
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300287 #!/bin/bash
Akinobu Mitade1ba092006-12-08 02:39:42 -0800288
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300289 FAILTYPE=fail_page_alloc
290 module=$1
Akinobu Mitade1ba092006-12-08 02:39:42 -0800291
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300292 if [ -z $module ]
293 then
Akinobu Mita18584872007-07-15 23:40:24 -0700294 echo "Usage: $0 <modulename>"
295 exit 1
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300296 fi
Akinobu Mitade1ba092006-12-08 02:39:42 -0800297
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300298 modprobe $module
Akinobu Mitade1ba092006-12-08 02:39:42 -0800299
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300300 if [ ! -d /sys/module/$module/sections ]
301 then
Akinobu Mita18584872007-07-15 23:40:24 -0700302 echo Module $module is not loaded
303 exit 1
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300304 fi
Akinobu Mita18584872007-07-15 23:40:24 -0700305
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300306 cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
307 cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
Akinobu Mita18584872007-07-15 23:40:24 -0700308
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300309 echo N > /sys/kernel/debug/$FAILTYPE/task-filter
310 echo 10 > /sys/kernel/debug/$FAILTYPE/probability
311 echo 100 > /sys/kernel/debug/$FAILTYPE/interval
312 echo -1 > /sys/kernel/debug/$FAILTYPE/times
313 echo 0 > /sys/kernel/debug/$FAILTYPE/space
314 echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
315 echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
316 echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
317 echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
Akinobu Mita18584872007-07-15 23:40:24 -0700318
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300319 trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
Akinobu Mita18584872007-07-15 23:40:24 -0700320
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300321 echo "Injecting errors into the module $module... (interrupt to stop)"
322 sleep 1000000
Akinobu Mitade1ba092006-12-08 02:39:42 -0800323
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900324------------------------------------------------------------------------------
325
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300326- Inject open_ctree error while btrfs mount::
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900327
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300328 #!/bin/bash
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900329
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300330 rm -f testfile.img
331 dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
332 DEVICE=$(losetup --show -f testfile.img)
333 mkfs.btrfs -f $DEVICE
334 mkdir -p tmpmnt
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900335
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300336 FAILTYPE=fail_function
337 FAILFUNC=open_ctree
338 echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
339 echo -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
340 echo N > /sys/kernel/debug/$FAILTYPE/task-filter
341 echo 100 > /sys/kernel/debug/$FAILTYPE/probability
342 echo 0 > /sys/kernel/debug/$FAILTYPE/interval
343 echo -1 > /sys/kernel/debug/$FAILTYPE/times
344 echo 0 > /sys/kernel/debug/$FAILTYPE/space
345 echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900346
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300347 mount -t btrfs $DEVICE tmpmnt
348 if [ $? -ne 0 ]
349 then
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900350 echo "SUCCESS!"
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300351 else
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900352 echo "FAILED!"
353 umount tmpmnt
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300354 fi
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900355
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300356 echo > /sys/kernel/debug/$FAILTYPE/inject
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900357
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300358 rmdir tmpmnt
359 losetup -d $DEVICE
360 rm testfile.img
Masami Hiramatsu4b1a29a2018-01-13 02:56:03 +0900361
362
Akinobu Mitac24aa642012-07-30 14:43:20 -0700363Tool to run command with failslab or fail_page_alloc
364----------------------------------------------------
365In order to make it easier to accomplish the tasks mentioned above, we can use
366tools/testing/fault-injection/failcmd.sh. Please run a command
367"./tools/testing/fault-injection/failcmd.sh --help" for more information and
368see the following examples.
369
370Examples:
371
372Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300373allocation failure::
Akinobu Mitac24aa642012-07-30 14:43:20 -0700374
375 # ./tools/testing/fault-injection/failcmd.sh \
376 -- make -C tools/testing/selftests/ run_tests
377
378Same as above except to specify 100 times failures at most instead of one time
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300379at most by default::
Akinobu Mitac24aa642012-07-30 14:43:20 -0700380
381 # ./tools/testing/fault-injection/failcmd.sh --times=100 \
382 -- make -C tools/testing/selftests/ run_tests
383
384Same as above except to inject page allocation failure instead of slab
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300385allocation failure::
Akinobu Mitac24aa642012-07-30 14:43:20 -0700386
387 # env FAILCMD_TYPE=fail_page_alloc \
388 ./tools/testing/fault-injection/failcmd.sh --times=100 \
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300389 -- make -C tools/testing/selftests/ run_tests
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700390
391Systematic faults using fail-nth
392---------------------------------
393
394The following code systematically faults 0-th, 1-st, 2-nd and so on
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300395capabilities in the socketpair() system call::
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700396
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300397 #include <sys/types.h>
398 #include <sys/stat.h>
399 #include <sys/socket.h>
400 #include <sys/syscall.h>
401 #include <fcntl.h>
402 #include <unistd.h>
403 #include <string.h>
404 #include <stdlib.h>
405 #include <stdio.h>
406 #include <errno.h>
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700407
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300408 int main()
409 {
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700410 int i, err, res, fail_nth, fds[2];
411 char buf[128];
412
413 system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
414 sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
415 fail_nth = open(buf, O_RDWR);
Akinobu Mita9049f2f2017-07-14 14:49:52 -0700416 for (i = 1;; i++) {
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700417 sprintf(buf, "%d", i);
418 write(fail_nth, buf, strlen(buf));
419 res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
420 err = errno;
Akinobu Mitabfc74092017-07-14 14:49:54 -0700421 pread(fail_nth, buf, sizeof(buf), 0);
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700422 if (res == 0) {
423 close(fds[0]);
424 close(fds[1]);
425 }
Akinobu Mitabfc74092017-07-14 14:49:54 -0700426 printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
427 res, err);
428 if (atoi(buf))
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700429 break;
430 }
431 return 0;
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300432 }
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700433
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300434An example output::
Dmitry Vyukove41d58182017-07-12 14:34:35 -0700435
Mauro Carvalho Chehab10ffebb2019-06-12 14:52:44 -0300436 1-th fault Y: res=-1/23
437 2-th fault Y: res=-1/23
438 3-th fault Y: res=-1/12
439 4-th fault Y: res=-1/12
440 5-th fault Y: res=-1/23
441 6-th fault Y: res=-1/23
442 7-th fault Y: res=-1/23
443 8-th fault Y: res=-1/12
444 9-th fault Y: res=-1/12
445 10-th fault Y: res=-1/12
446 11-th fault Y: res=-1/12
447 12-th fault Y: res=-1/12
448 13-th fault Y: res=-1/12
449 14-th fault Y: res=-1/12
450 15-th fault Y: res=-1/12
451 16-th fault N: res=0/12