fault-injection.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. ===========================================
  2. Fault injection capabilities infrastructure
  3. ===========================================
  4. See also drivers/md/md-faulty.c and "every_nth" module option for scsi_debug.
  5. Available fault injection capabilities
  6. --------------------------------------
  7. - failslab
  8. injects slab allocation failures. (kmalloc(), kmem_cache_alloc(), ...)
  9. - fail_page_alloc
  10. injects page allocation failures. (alloc_pages(), get_free_pages(), ...)
  11. - fail_usercopy
  12. injects failures in user memory access functions. (copy_from_user(), get_user(), ...)
  13. - fail_futex
  14. injects futex deadlock and uaddr fault errors.
  15. - fail_sunrpc
  16. injects kernel RPC client and server failures.
  17. - fail_make_request
  18. injects disk IO errors on devices permitted by setting
  19. /sys/block/<device>/make-it-fail or
  20. /sys/block/<device>/<partition>/make-it-fail. (submit_bio_noacct())
  21. - fail_mmc_request
  22. injects MMC data errors on devices permitted by setting
  23. debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
  24. - fail_function
  25. injects error return on specific functions, which are marked by
  26. ALLOW_ERROR_INJECTION() macro, by setting debugfs entries
  27. under /sys/kernel/debug/fail_function. No boot option supported.
  28. - NVMe fault injection
  29. inject NVMe status code and retry flag on devices permitted by setting
  30. debugfs entries under /sys/kernel/debug/nvme*/fault_inject. The default
  31. status code is NVME_SC_INVALID_OPCODE with no retry. The status code and
  32. retry flag can be set via the debugfs.
  33. - Null test block driver fault injection
  34. inject IO timeouts by setting config items under
  35. /sys/kernel/config/nullb/<disk>/timeout_inject,
  36. inject requeue requests by setting config items under
  37. /sys/kernel/config/nullb/<disk>/requeue_inject, and
  38. inject init_hctx() errors by setting config items under
  39. /sys/kernel/config/nullb/<disk>/init_hctx_fault_inject.
  40. Configure fault-injection capabilities behavior
  41. -----------------------------------------------
  42. debugfs entries
  43. ^^^^^^^^^^^^^^^
  44. fault-inject-debugfs kernel module provides some debugfs entries for runtime
  45. configuration of fault-injection capabilities.
  46. - /sys/kernel/debug/fail*/probability:
  47. likelihood of failure injection, in percent.
  48. Format: <percent>
  49. Note that one-failure-per-hundred is a very high error rate
  50. for some testcases. Consider setting probability=100 and configure
  51. /sys/kernel/debug/fail*/interval for such testcases.
  52. - /sys/kernel/debug/fail*/interval:
  53. specifies the interval between failures, for calls to
  54. should_fail() that pass all the other tests.
  55. Note that if you enable this, by setting interval>1, you will
  56. probably want to set probability=100.
  57. - /sys/kernel/debug/fail*/times:
  58. specifies how many times failures may happen at most. A value of -1
  59. means "no limit".
  60. - /sys/kernel/debug/fail*/space:
  61. specifies an initial resource "budget", decremented by "size"
  62. on each call to should_fail(,size). Failure injection is
  63. suppressed until "space" reaches zero.
  64. - /sys/kernel/debug/fail*/verbose
  65. Format: { 0 | 1 | 2 }
  66. specifies the verbosity of the messages when failure is
  67. injected. '0' means no messages; '1' will print only a single
  68. log line per failure; '2' will print a call trace too -- useful
  69. to debug the problems revealed by fault injection.
  70. - /sys/kernel/debug/fail*/task-filter:
  71. Format: { 'Y' | 'N' }
  72. A value of 'N' disables filtering by process (default).
  73. Any positive value limits failures to only processes indicated by
  74. /proc/<pid>/make-it-fail==1.
  75. - /sys/kernel/debug/fail*/require-start,
  76. /sys/kernel/debug/fail*/require-end,
  77. /sys/kernel/debug/fail*/reject-start,
  78. /sys/kernel/debug/fail*/reject-end:
  79. specifies the range of virtual addresses tested during
  80. stacktrace walking. Failure is injected only if some caller
  81. in the walked stacktrace lies within the required range, and
  82. none lies within the rejected range.
  83. Default required range is [0,ULONG_MAX) (whole of virtual address space).
  84. Default rejected range is [0,0).
  85. - /sys/kernel/debug/fail*/stacktrace-depth:
  86. specifies the maximum stacktrace depth walked during search
  87. for a caller within [require-start,require-end) OR
  88. [reject-start,reject-end).
  89. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
  90. Format: { 'Y' | 'N' }
  91. default is 'Y', setting it to 'N' will also inject failures into
  92. highmem/user allocations (__GFP_HIGHMEM allocations).
  93. - /sys/kernel/debug/failslab/cache-filter
  94. Format: { 'Y' | 'N' }
  95. default is 'N', setting it to 'Y' will only inject failures when
  96. objects are requests from certain caches.
  97. Select the cache by writing '1' to /sys/kernel/slab/<cache>/failslab:
  98. - /sys/kernel/debug/failslab/ignore-gfp-wait:
  99. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
  100. Format: { 'Y' | 'N' }
  101. default is 'Y', setting it to 'N' will also inject failures
  102. into allocations that can sleep (__GFP_DIRECT_RECLAIM allocations).
  103. - /sys/kernel/debug/fail_page_alloc/min-order:
  104. specifies the minimum page allocation order to be injected
  105. failures.
  106. - /sys/kernel/debug/fail_futex/ignore-private:
  107. Format: { 'Y' | 'N' }
  108. default is 'N', setting it to 'Y' will disable failure injections
  109. when dealing with private (address space) futexes.
  110. - /sys/kernel/debug/fail_sunrpc/ignore-client-disconnect:
  111. Format: { 'Y' | 'N' }
  112. default is 'N', setting it to 'Y' will disable disconnect
  113. injection on the RPC client.
  114. - /sys/kernel/debug/fail_sunrpc/ignore-server-disconnect:
  115. Format: { 'Y' | 'N' }
  116. default is 'N', setting it to 'Y' will disable disconnect
  117. injection on the RPC server.
  118. - /sys/kernel/debug/fail_sunrpc/ignore-cache-wait:
  119. Format: { 'Y' | 'N' }
  120. default is 'N', setting it to 'Y' will disable cache wait
  121. injection on the RPC server.
  122. - /sys/kernel/debug/fail_function/inject:
  123. Format: { 'function-name' | '!function-name' | '' }
  124. specifies the target function of error injection by name.
  125. If the function name leads '!' prefix, given function is
  126. removed from injection list. If nothing specified ('')
  127. injection list is cleared.
  128. - /sys/kernel/debug/fail_function/injectable:
  129. (read only) shows error injectable functions and what type of
  130. error values can be specified. The error type will be one of
  131. below;
  132. - NULL: retval must be 0.
  133. - ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
  134. - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
  135. - /sys/kernel/debug/fail_function/<function-name>/retval:
  136. specifies the "error" return value to inject to the given function.
  137. This will be created when the user specifies a new injection entry.
  138. Note that this file only accepts unsigned values. So, if you want to
  139. use a negative errno, you better use 'printf' instead of 'echo', e.g.:
  140. $ printf %#x -12 > retval
  141. Boot option
  142. ^^^^^^^^^^^
  143. In order to inject faults while debugfs is not available (early boot time),
  144. use the boot option::
  145. failslab=
  146. fail_page_alloc=
  147. fail_usercopy=
  148. fail_make_request=
  149. fail_futex=
  150. mmc_core.fail_request=<interval>,<probability>,<space>,<times>
  151. proc entries
  152. ^^^^^^^^^^^^
  153. - /proc/<pid>/fail-nth,
  154. /proc/self/task/<tid>/fail-nth:
  155. Write to this file of integer N makes N-th call in the task fail.
  156. Read from this file returns a integer value. A value of '0' indicates
  157. that the fault setup with a previous write to this file was injected.
  158. A positive integer N indicates that the fault wasn't yet injected.
  159. Note that this file enables all types of faults (slab, futex, etc).
  160. This setting takes precedence over all other generic debugfs settings
  161. like probability, interval, times, etc. But per-capability settings
  162. (e.g. fail_futex/ignore-private) take precedence over it.
  163. This feature is intended for systematic testing of faults in a single
  164. system call. See an example below.
  165. Error Injectable Functions
  166. --------------------------
  167. This part is for the kernel developers considering to add a function to
  168. ALLOW_ERROR_INJECTION() macro.
  169. Requirements for the Error Injectable Functions
  170. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  171. Since the function-level error injection forcibly changes the code path
  172. and returns an error even if the input and conditions are proper, this can
  173. cause unexpected kernel crash if you allow error injection on the function
  174. which is NOT error injectable. Thus, you (and reviewers) must ensure;
  175. - The function returns an error code if it fails, and the callers must check
  176. it correctly (need to recover from it).
  177. - The function does not execute any code which can change any state before
  178. the first error return. The state includes global or local, or input
  179. variable. For example, clear output address storage (e.g. `*ret = NULL`),
  180. increments/decrements counter, set a flag, preempt/irq disable or get
  181. a lock (if those are recovered before returning error, that will be OK.)
  182. The first requirement is important, and it will result in that the release
  183. (free objects) functions are usually harder to inject errors than allocate
  184. functions. If errors of such release functions are not correctly handled
  185. it will cause a memory leak easily (the caller will confuse that the object
  186. has been released or corrupted.)
  187. The second one is for the caller which expects the function should always
  188. does something. Thus if the function error injection skips whole of the
  189. function, the expectation is betrayed and causes an unexpected error.
  190. Type of the Error Injectable Functions
  191. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  192. Each error injectable functions will have the error type specified by the
  193. ALLOW_ERROR_INJECTION() macro. You have to choose it carefully if you add
  194. a new error injectable function. If the wrong error type is chosen, the
  195. kernel may crash because it may not be able to handle the error.
  196. There are 4 types of errors defined in include/asm-generic/error-injection.h
  197. EI_ETYPE_NULL
  198. This function will return `NULL` if it fails. e.g. return an allocated
  199. object address.
  200. EI_ETYPE_ERRNO
  201. This function will return an `-errno` error code if it fails. e.g. return
  202. -EINVAL if the input is wrong. This will include the functions which will
  203. return an address which encodes `-errno` by ERR_PTR() macro.
  204. EI_ETYPE_ERRNO_NULL
  205. This function will return an `-errno` or `NULL` if it fails. If the caller
  206. of this function checks the return value with IS_ERR_OR_NULL() macro, this
  207. type will be appropriate.
  208. EI_ETYPE_TRUE
  209. This function will return `true` (non-zero positive value) if it fails.
  210. If you specifies a wrong type, for example, EI_TYPE_ERRNO for the function
  211. which returns an allocated object, it may cause a problem because the returned
  212. value is not an object address and the caller can not access to the address.
  213. How to add new fault injection capability
  214. -----------------------------------------
  215. - #include <linux/fault-inject.h>
  216. - define the fault attributes
  217. DECLARE_FAULT_ATTR(name);
  218. Please see the definition of struct fault_attr in fault-inject.h
  219. for details.
  220. - provide a way to configure fault attributes
  221. - boot option
  222. If you need to enable the fault injection capability from boot time, you can
  223. provide boot option to configure it. There is a helper function for it:
  224. setup_fault_attr(attr, str);
  225. - debugfs entries
  226. failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
  227. Helper functions:
  228. fault_create_debugfs_attr(name, parent, attr);
  229. - module parameters
  230. If the scope of the fault injection capability is limited to a
  231. single kernel module, it is better to provide module parameters to
  232. configure the fault attributes.
  233. - add a hook to insert failures
  234. Upon should_fail() returning true, client code should inject a failure:
  235. should_fail(attr, size);
  236. Application Examples
  237. --------------------
  238. - Inject slab allocation failures into module init/exit code::
  239. #!/bin/bash
  240. FAILTYPE=failslab
  241. echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
  242. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  243. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  244. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  245. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  246. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  247. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  248. faulty_system()
  249. {
  250. bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
  251. }
  252. if [ $# -eq 0 ]
  253. then
  254. echo "Usage: $0 modulename [ modulename ... ]"
  255. exit 1
  256. fi
  257. for m in $*
  258. do
  259. echo inserting $m...
  260. faulty_system modprobe $m
  261. echo removing $m...
  262. faulty_system modprobe -r $m
  263. done
  264. ------------------------------------------------------------------------------
  265. - Inject page allocation failures only for a specific module::
  266. #!/bin/bash
  267. FAILTYPE=fail_page_alloc
  268. module=$1
  269. if [ -z $module ]
  270. then
  271. echo "Usage: $0 <modulename>"
  272. exit 1
  273. fi
  274. modprobe $module
  275. if [ ! -d /sys/module/$module/sections ]
  276. then
  277. echo Module $module is not loaded
  278. exit 1
  279. fi
  280. cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
  281. cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
  282. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  283. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  284. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  285. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  286. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  287. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  288. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  289. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
  290. echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
  291. trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
  292. echo "Injecting errors into the module $module... (interrupt to stop)"
  293. sleep 1000000
  294. ------------------------------------------------------------------------------
  295. - Inject open_ctree error while btrfs mount::
  296. #!/bin/bash
  297. rm -f testfile.img
  298. dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
  299. DEVICE=$(losetup --show -f testfile.img)
  300. mkfs.btrfs -f $DEVICE
  301. mkdir -p tmpmnt
  302. FAILTYPE=fail_function
  303. FAILFUNC=open_ctree
  304. echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
  305. printf %#x -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
  306. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  307. echo 100 > /sys/kernel/debug/$FAILTYPE/probability
  308. echo 0 > /sys/kernel/debug/$FAILTYPE/interval
  309. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  310. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  311. echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
  312. mount -t btrfs $DEVICE tmpmnt
  313. if [ $? -ne 0 ]
  314. then
  315. echo "SUCCESS!"
  316. else
  317. echo "FAILED!"
  318. umount tmpmnt
  319. fi
  320. echo > /sys/kernel/debug/$FAILTYPE/inject
  321. rmdir tmpmnt
  322. losetup -d $DEVICE
  323. rm testfile.img
  324. ------------------------------------------------------------------------------
  325. - Inject only skbuff allocation failures ::
  326. # mark skbuff_head_cache as faulty
  327. echo 1 > /sys/kernel/slab/skbuff_head_cache/failslab
  328. # Turn on cache filter (off by default)
  329. echo 1 > /sys/kernel/debug/failslab/cache-filter
  330. # Turn on fault injection
  331. echo 1 > /sys/kernel/debug/failslab/times
  332. echo 1 > /sys/kernel/debug/failslab/probability
  333. Tool to run command with failslab or fail_page_alloc
  334. ----------------------------------------------------
  335. In order to make it easier to accomplish the tasks mentioned above, we can use
  336. tools/testing/fault-injection/failcmd.sh. Please run a command
  337. "./tools/testing/fault-injection/failcmd.sh --help" for more information and
  338. see the following examples.
  339. Examples:
  340. Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
  341. allocation failure::
  342. # ./tools/testing/fault-injection/failcmd.sh \
  343. -- make -C tools/testing/selftests/ run_tests
  344. Same as above except to specify 100 times failures at most instead of one time
  345. at most by default::
  346. # ./tools/testing/fault-injection/failcmd.sh --times=100 \
  347. -- make -C tools/testing/selftests/ run_tests
  348. Same as above except to inject page allocation failure instead of slab
  349. allocation failure::
  350. # env FAILCMD_TYPE=fail_page_alloc \
  351. ./tools/testing/fault-injection/failcmd.sh --times=100 \
  352. -- make -C tools/testing/selftests/ run_tests
  353. Systematic faults using fail-nth
  354. ---------------------------------
  355. The following code systematically faults 0-th, 1-st, 2-nd and so on
  356. capabilities in the socketpair() system call::
  357. #include <sys/types.h>
  358. #include <sys/stat.h>
  359. #include <sys/socket.h>
  360. #include <sys/syscall.h>
  361. #include <fcntl.h>
  362. #include <unistd.h>
  363. #include <string.h>
  364. #include <stdlib.h>
  365. #include <stdio.h>
  366. #include <errno.h>
  367. int main()
  368. {
  369. int i, err, res, fail_nth, fds[2];
  370. char buf[128];
  371. system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
  372. sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
  373. fail_nth = open(buf, O_RDWR);
  374. for (i = 1;; i++) {
  375. sprintf(buf, "%d", i);
  376. write(fail_nth, buf, strlen(buf));
  377. res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
  378. err = errno;
  379. pread(fail_nth, buf, sizeof(buf), 0);
  380. if (res == 0) {
  381. close(fds[0]);
  382. close(fds[1]);
  383. }
  384. printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
  385. res, err);
  386. if (atoi(buf))
  387. break;
  388. }
  389. return 0;
  390. }
  391. An example output::
  392. 1-th fault Y: res=-1/23
  393. 2-th fault Y: res=-1/23
  394. 3-th fault Y: res=-1/12
  395. 4-th fault Y: res=-1/12
  396. 5-th fault Y: res=-1/23
  397. 6-th fault Y: res=-1/23
  398. 7-th fault Y: res=-1/23
  399. 8-th fault Y: res=-1/12
  400. 9-th fault Y: res=-1/12
  401. 10-th fault Y: res=-1/12
  402. 11-th fault Y: res=-1/12
  403. 12-th fault Y: res=-1/12
  404. 13-th fault Y: res=-1/12
  405. 14-th fault Y: res=-1/12
  406. 15-th fault Y: res=-1/12
  407. 16-th fault N: res=0/12