fault-injection.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. Configure fault-injection capabilities behavior
  34. -----------------------------------------------
  35. debugfs entries
  36. ^^^^^^^^^^^^^^^
  37. fault-inject-debugfs kernel module provides some debugfs entries for runtime
  38. configuration of fault-injection capabilities.
  39. - /sys/kernel/debug/fail*/probability:
  40. likelihood of failure injection, in percent.
  41. Format: <percent>
  42. Note that one-failure-per-hundred is a very high error rate
  43. for some testcases. Consider setting probability=100 and configure
  44. /sys/kernel/debug/fail*/interval for such testcases.
  45. - /sys/kernel/debug/fail*/interval:
  46. specifies the interval between failures, for calls to
  47. should_fail() that pass all the other tests.
  48. Note that if you enable this, by setting interval>1, you will
  49. probably want to set probability=100.
  50. - /sys/kernel/debug/fail*/times:
  51. specifies how many times failures may happen at most. A value of -1
  52. means "no limit".
  53. - /sys/kernel/debug/fail*/space:
  54. specifies an initial resource "budget", decremented by "size"
  55. on each call to should_fail(,size). Failure injection is
  56. suppressed until "space" reaches zero.
  57. - /sys/kernel/debug/fail*/verbose
  58. Format: { 0 | 1 | 2 }
  59. specifies the verbosity of the messages when failure is
  60. injected. '0' means no messages; '1' will print only a single
  61. log line per failure; '2' will print a call trace too -- useful
  62. to debug the problems revealed by fault injection.
  63. - /sys/kernel/debug/fail*/task-filter:
  64. Format: { 'Y' | 'N' }
  65. A value of 'N' disables filtering by process (default).
  66. Any positive value limits failures to only processes indicated by
  67. /proc/<pid>/make-it-fail==1.
  68. - /sys/kernel/debug/fail*/require-start,
  69. /sys/kernel/debug/fail*/require-end,
  70. /sys/kernel/debug/fail*/reject-start,
  71. /sys/kernel/debug/fail*/reject-end:
  72. specifies the range of virtual addresses tested during
  73. stacktrace walking. Failure is injected only if some caller
  74. in the walked stacktrace lies within the required range, and
  75. none lies within the rejected range.
  76. Default required range is [0,ULONG_MAX) (whole of virtual address space).
  77. Default rejected range is [0,0).
  78. - /sys/kernel/debug/fail*/stacktrace-depth:
  79. specifies the maximum stacktrace depth walked during search
  80. for a caller within [require-start,require-end) OR
  81. [reject-start,reject-end).
  82. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
  83. Format: { 'Y' | 'N' }
  84. default is 'Y', setting it to 'N' will also inject failures into
  85. highmem/user allocations (__GFP_HIGHMEM allocations).
  86. - /sys/kernel/debug/failslab/ignore-gfp-wait:
  87. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
  88. Format: { 'Y' | 'N' }
  89. default is 'Y', setting it to 'N' will also inject failures
  90. into allocations that can sleep (__GFP_DIRECT_RECLAIM allocations).
  91. - /sys/kernel/debug/fail_page_alloc/min-order:
  92. specifies the minimum page allocation order to be injected
  93. failures.
  94. - /sys/kernel/debug/fail_futex/ignore-private:
  95. Format: { 'Y' | 'N' }
  96. default is 'N', setting it to 'Y' will disable failure injections
  97. when dealing with private (address space) futexes.
  98. - /sys/kernel/debug/fail_sunrpc/ignore-client-disconnect:
  99. Format: { 'Y' | 'N' }
  100. default is 'N', setting it to 'Y' will disable disconnect
  101. injection on the RPC client.
  102. - /sys/kernel/debug/fail_sunrpc/ignore-server-disconnect:
  103. Format: { 'Y' | 'N' }
  104. default is 'N', setting it to 'Y' will disable disconnect
  105. injection on the RPC server.
  106. - /sys/kernel/debug/fail_sunrpc/ignore-cache-wait:
  107. Format: { 'Y' | 'N' }
  108. default is 'N', setting it to 'Y' will disable cache wait
  109. injection on the RPC server.
  110. - /sys/kernel/debug/fail_function/inject:
  111. Format: { 'function-name' | '!function-name' | '' }
  112. specifies the target function of error injection by name.
  113. If the function name leads '!' prefix, given function is
  114. removed from injection list. If nothing specified ('')
  115. injection list is cleared.
  116. - /sys/kernel/debug/fail_function/injectable:
  117. (read only) shows error injectable functions and what type of
  118. error values can be specified. The error type will be one of
  119. below;
  120. - NULL: retval must be 0.
  121. - ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
  122. - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
  123. - /sys/kernel/debug/fail_function/<function-name>/retval:
  124. specifies the "error" return value to inject to the given function.
  125. This will be created when the user specifies a new injection entry.
  126. Note that this file only accepts unsigned values. So, if you want to
  127. use a negative errno, you better use 'printf' instead of 'echo', e.g.:
  128. $ printf %#x -12 > retval
  129. Boot option
  130. ^^^^^^^^^^^
  131. In order to inject faults while debugfs is not available (early boot time),
  132. use the boot option::
  133. failslab=
  134. fail_page_alloc=
  135. fail_usercopy=
  136. fail_make_request=
  137. fail_futex=
  138. mmc_core.fail_request=<interval>,<probability>,<space>,<times>
  139. proc entries
  140. ^^^^^^^^^^^^
  141. - /proc/<pid>/fail-nth,
  142. /proc/self/task/<tid>/fail-nth:
  143. Write to this file of integer N makes N-th call in the task fail.
  144. Read from this file returns a integer value. A value of '0' indicates
  145. that the fault setup with a previous write to this file was injected.
  146. A positive integer N indicates that the fault wasn't yet injected.
  147. Note that this file enables all types of faults (slab, futex, etc).
  148. This setting takes precedence over all other generic debugfs settings
  149. like probability, interval, times, etc. But per-capability settings
  150. (e.g. fail_futex/ignore-private) take precedence over it.
  151. This feature is intended for systematic testing of faults in a single
  152. system call. See an example below.
  153. How to add new fault injection capability
  154. -----------------------------------------
  155. - #include <linux/fault-inject.h>
  156. - define the fault attributes
  157. DECLARE_FAULT_ATTR(name);
  158. Please see the definition of struct fault_attr in fault-inject.h
  159. for details.
  160. - provide a way to configure fault attributes
  161. - boot option
  162. If you need to enable the fault injection capability from boot time, you can
  163. provide boot option to configure it. There is a helper function for it:
  164. setup_fault_attr(attr, str);
  165. - debugfs entries
  166. failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
  167. Helper functions:
  168. fault_create_debugfs_attr(name, parent, attr);
  169. - module parameters
  170. If the scope of the fault injection capability is limited to a
  171. single kernel module, it is better to provide module parameters to
  172. configure the fault attributes.
  173. - add a hook to insert failures
  174. Upon should_fail() returning true, client code should inject a failure:
  175. should_fail(attr, size);
  176. Application Examples
  177. --------------------
  178. - Inject slab allocation failures into module init/exit code::
  179. #!/bin/bash
  180. FAILTYPE=failslab
  181. echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
  182. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  183. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  184. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  185. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  186. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  187. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  188. faulty_system()
  189. {
  190. bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
  191. }
  192. if [ $# -eq 0 ]
  193. then
  194. echo "Usage: $0 modulename [ modulename ... ]"
  195. exit 1
  196. fi
  197. for m in $*
  198. do
  199. echo inserting $m...
  200. faulty_system modprobe $m
  201. echo removing $m...
  202. faulty_system modprobe -r $m
  203. done
  204. ------------------------------------------------------------------------------
  205. - Inject page allocation failures only for a specific module::
  206. #!/bin/bash
  207. FAILTYPE=fail_page_alloc
  208. module=$1
  209. if [ -z $module ]
  210. then
  211. echo "Usage: $0 <modulename>"
  212. exit 1
  213. fi
  214. modprobe $module
  215. if [ ! -d /sys/module/$module/sections ]
  216. then
  217. echo Module $module is not loaded
  218. exit 1
  219. fi
  220. cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
  221. cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
  222. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  223. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  224. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  225. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  226. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  227. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  228. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  229. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
  230. echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
  231. trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
  232. echo "Injecting errors into the module $module... (interrupt to stop)"
  233. sleep 1000000
  234. ------------------------------------------------------------------------------
  235. - Inject open_ctree error while btrfs mount::
  236. #!/bin/bash
  237. rm -f testfile.img
  238. dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
  239. DEVICE=$(losetup --show -f testfile.img)
  240. mkfs.btrfs -f $DEVICE
  241. mkdir -p tmpmnt
  242. FAILTYPE=fail_function
  243. FAILFUNC=open_ctree
  244. echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
  245. printf %#x -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
  246. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  247. echo 100 > /sys/kernel/debug/$FAILTYPE/probability
  248. echo 0 > /sys/kernel/debug/$FAILTYPE/interval
  249. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  250. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  251. echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
  252. mount -t btrfs $DEVICE tmpmnt
  253. if [ $? -ne 0 ]
  254. then
  255. echo "SUCCESS!"
  256. else
  257. echo "FAILED!"
  258. umount tmpmnt
  259. fi
  260. echo > /sys/kernel/debug/$FAILTYPE/inject
  261. rmdir tmpmnt
  262. losetup -d $DEVICE
  263. rm testfile.img
  264. Tool to run command with failslab or fail_page_alloc
  265. ----------------------------------------------------
  266. In order to make it easier to accomplish the tasks mentioned above, we can use
  267. tools/testing/fault-injection/failcmd.sh. Please run a command
  268. "./tools/testing/fault-injection/failcmd.sh --help" for more information and
  269. see the following examples.
  270. Examples:
  271. Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
  272. allocation failure::
  273. # ./tools/testing/fault-injection/failcmd.sh \
  274. -- make -C tools/testing/selftests/ run_tests
  275. Same as above except to specify 100 times failures at most instead of one time
  276. at most by default::
  277. # ./tools/testing/fault-injection/failcmd.sh --times=100 \
  278. -- make -C tools/testing/selftests/ run_tests
  279. Same as above except to inject page allocation failure instead of slab
  280. allocation failure::
  281. # env FAILCMD_TYPE=fail_page_alloc \
  282. ./tools/testing/fault-injection/failcmd.sh --times=100 \
  283. -- make -C tools/testing/selftests/ run_tests
  284. Systematic faults using fail-nth
  285. ---------------------------------
  286. The following code systematically faults 0-th, 1-st, 2-nd and so on
  287. capabilities in the socketpair() system call::
  288. #include <sys/types.h>
  289. #include <sys/stat.h>
  290. #include <sys/socket.h>
  291. #include <sys/syscall.h>
  292. #include <fcntl.h>
  293. #include <unistd.h>
  294. #include <string.h>
  295. #include <stdlib.h>
  296. #include <stdio.h>
  297. #include <errno.h>
  298. int main()
  299. {
  300. int i, err, res, fail_nth, fds[2];
  301. char buf[128];
  302. system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
  303. sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
  304. fail_nth = open(buf, O_RDWR);
  305. for (i = 1;; i++) {
  306. sprintf(buf, "%d", i);
  307. write(fail_nth, buf, strlen(buf));
  308. res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
  309. err = errno;
  310. pread(fail_nth, buf, sizeof(buf), 0);
  311. if (res == 0) {
  312. close(fds[0]);
  313. close(fds[1]);
  314. }
  315. printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
  316. res, err);
  317. if (atoi(buf))
  318. break;
  319. }
  320. return 0;
  321. }
  322. An example output::
  323. 1-th fault Y: res=-1/23
  324. 2-th fault Y: res=-1/23
  325. 3-th fault Y: res=-1/12
  326. 4-th fault Y: res=-1/12
  327. 5-th fault Y: res=-1/23
  328. 6-th fault Y: res=-1/23
  329. 7-th fault Y: res=-1/23
  330. 8-th fault Y: res=-1/12
  331. 9-th fault Y: res=-1/12
  332. 10-th fault Y: res=-1/12
  333. 11-th fault Y: res=-1/12
  334. 12-th fault Y: res=-1/12
  335. 13-th fault Y: res=-1/12
  336. 14-th fault Y: res=-1/12
  337. 15-th fault Y: res=-1/12
  338. 16-th fault N: res=0/12