fault-injection.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. Fault injection capabilities infrastructure
  2. ===========================================
  3. See also drivers/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. Configure fault-injection capabilities behavior
  20. -----------------------------------------------
  21. o debugfs entries
  22. fault-inject-debugfs kernel module provides some debugfs entries for runtime
  23. configuration of fault-injection capabilities.
  24. - /sys/kernel/debug/fail*/probability:
  25. likelihood of failure injection, in percent.
  26. Format: <percent>
  27. Note that one-failure-per-hundred is a very high error rate
  28. for some testcases. Consider setting probability=100 and configure
  29. /sys/kernel/debug/fail*/interval for such testcases.
  30. - /sys/kernel/debug/fail*/interval:
  31. specifies the interval between failures, for calls to
  32. should_fail() that pass all the other tests.
  33. Note that if you enable this, by setting interval>1, you will
  34. probably want to set probability=100.
  35. - /sys/kernel/debug/fail*/times:
  36. specifies how many times failures may happen at most.
  37. A value of -1 means "no limit".
  38. - /sys/kernel/debug/fail*/space:
  39. specifies an initial resource "budget", decremented by "size"
  40. on each call to should_fail(,size). Failure injection is
  41. suppressed until "space" reaches zero.
  42. - /sys/kernel/debug/fail*/verbose
  43. Format: { 0 | 1 | 2 }
  44. specifies the verbosity of the messages when failure is
  45. injected. '0' means no messages; '1' will print only a single
  46. log line per failure; '2' will print a call trace too -- useful
  47. to debug the problems revealed by fault injection.
  48. - /sys/kernel/debug/fail*/task-filter:
  49. Format: { 'Y' | 'N' }
  50. A value of 'N' disables filtering by process (default).
  51. Any positive value limits failures to only processes indicated by
  52. /proc/<pid>/make-it-fail==1.
  53. - /sys/kernel/debug/fail*/require-start:
  54. - /sys/kernel/debug/fail*/require-end:
  55. - /sys/kernel/debug/fail*/reject-start:
  56. - /sys/kernel/debug/fail*/reject-end:
  57. specifies the range of virtual addresses tested during
  58. stacktrace walking. Failure is injected only if some caller
  59. in the walked stacktrace lies within the required range, and
  60. none lies within the rejected range.
  61. Default required range is [0,ULONG_MAX) (whole of virtual address space).
  62. Default rejected range is [0,0).
  63. - /sys/kernel/debug/fail*/stacktrace-depth:
  64. specifies the maximum stacktrace depth walked during search
  65. for a caller within [require-start,require-end) OR
  66. [reject-start,reject-end).
  67. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
  68. Format: { 'Y' | 'N' }
  69. default is 'N', setting it to 'Y' won't inject failures into
  70. highmem/user allocations.
  71. - /sys/kernel/debug/failslab/ignore-gfp-wait:
  72. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
  73. Format: { 'Y' | 'N' }
  74. default is 'N', setting it to 'Y' will inject failures
  75. only into non-sleep allocations (GFP_ATOMIC allocations).
  76. - /sys/kernel/debug/fail_page_alloc/min-order:
  77. specifies the minimum page allocation order to be injected
  78. failures.
  79. - /sys/kernel/debug/fail_futex/ignore-private:
  80. Format: { 'Y' | 'N' }
  81. default is 'N', setting it to 'Y' will disable failure injections
  82. when dealing with private (address space) futexes.
  83. o Boot option
  84. In order to inject faults while debugfs is not available (early boot time),
  85. use the boot option:
  86. failslab=
  87. fail_page_alloc=
  88. fail_make_request=
  89. fail_futex=
  90. mmc_core.fail_request=<interval>,<probability>,<space>,<times>
  91. How to add new fault injection capability
  92. -----------------------------------------
  93. o #include <linux/fault-inject.h>
  94. o define the fault attributes
  95. DECLARE_FAULT_INJECTION(name);
  96. Please see the definition of struct fault_attr in fault-inject.h
  97. for details.
  98. o provide a way to configure fault attributes
  99. - boot option
  100. If you need to enable the fault injection capability from boot time, you can
  101. provide boot option to configure it. There is a helper function for it:
  102. setup_fault_attr(attr, str);
  103. - debugfs entries
  104. failslab, fail_page_alloc, and fail_make_request use this way.
  105. Helper functions:
  106. fault_create_debugfs_attr(name, parent, attr);
  107. - module parameters
  108. If the scope of the fault injection capability is limited to a
  109. single kernel module, it is better to provide module parameters to
  110. configure the fault attributes.
  111. o add a hook to insert failures
  112. Upon should_fail() returning true, client code should inject a failure.
  113. should_fail(attr, size);
  114. Application Examples
  115. --------------------
  116. o Inject slab allocation failures into module init/exit code
  117. #!/bin/bash
  118. FAILTYPE=failslab
  119. echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
  120. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  121. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  122. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  123. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  124. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  125. echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  126. faulty_system()
  127. {
  128. bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
  129. }
  130. if [ $# -eq 0 ]
  131. then
  132. echo "Usage: $0 modulename [ modulename ... ]"
  133. exit 1
  134. fi
  135. for m in $*
  136. do
  137. echo inserting $m...
  138. faulty_system modprobe $m
  139. echo removing $m...
  140. faulty_system modprobe -r $m
  141. done
  142. ------------------------------------------------------------------------------
  143. o Inject page allocation failures only for a specific module
  144. #!/bin/bash
  145. FAILTYPE=fail_page_alloc
  146. module=$1
  147. if [ -z $module ]
  148. then
  149. echo "Usage: $0 <modulename>"
  150. exit 1
  151. fi
  152. modprobe $module
  153. if [ ! -d /sys/module/$module/sections ]
  154. then
  155. echo Module $module is not loaded
  156. exit 1
  157. fi
  158. cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
  159. cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
  160. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  161. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  162. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  163. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  164. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  165. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  166. echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  167. echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
  168. echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
  169. trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
  170. echo "Injecting errors into the module $module... (interrupt to stop)"
  171. sleep 1000000
  172. Tool to run command with failslab or fail_page_alloc
  173. ----------------------------------------------------
  174. In order to make it easier to accomplish the tasks mentioned above, we can use
  175. tools/testing/fault-injection/failcmd.sh. Please run a command
  176. "./tools/testing/fault-injection/failcmd.sh --help" for more information and
  177. see the following examples.
  178. Examples:
  179. Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
  180. allocation failure.
  181. # ./tools/testing/fault-injection/failcmd.sh \
  182. -- make -C tools/testing/selftests/ run_tests
  183. Same as above except to specify 100 times failures at most instead of one time
  184. at most by default.
  185. # ./tools/testing/fault-injection/failcmd.sh --times=100 \
  186. -- make -C tools/testing/selftests/ run_tests
  187. Same as above except to inject page allocation failure instead of slab
  188. allocation failure.
  189. # env FAILCMD_TYPE=fail_page_alloc \
  190. ./tools/testing/fault-injection/failcmd.sh --times=100 \
  191. -- make -C tools/testing/selftests/ run_tests