fault-injection.txt 6.5 KB

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