fault-injection.txt 12 KB

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