fault-injection.txt 6.4 KB

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