fault-injection.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. fault_create_debugfs_attr(name, parent, attr);
  96. - module parameters
  97. If the scope of the fault injection capability is limited to a
  98. single kernel module, it is better to provide module parameters to
  99. configure the fault attributes.
  100. o add a hook to insert failures
  101. Upon should_fail() returning true, client code should inject a failure.
  102. should_fail(attr, size);
  103. Application Examples
  104. --------------------
  105. o Inject slab allocation failures into module init/exit code
  106. #!/bin/bash
  107. FAILTYPE=failslab
  108. echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
  109. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  110. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  111. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  112. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  113. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  114. echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  115. faulty_system()
  116. {
  117. bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
  118. }
  119. if [ $# -eq 0 ]
  120. then
  121. echo "Usage: $0 modulename [ modulename ... ]"
  122. exit 1
  123. fi
  124. for m in $*
  125. do
  126. echo inserting $m...
  127. faulty_system modprobe $m
  128. echo removing $m...
  129. faulty_system modprobe -r $m
  130. done
  131. ------------------------------------------------------------------------------
  132. o Inject page allocation failures only for a specific module
  133. #!/bin/bash
  134. FAILTYPE=fail_page_alloc
  135. module=$1
  136. if [ -z $module ]
  137. then
  138. echo "Usage: $0 <modulename>"
  139. exit 1
  140. fi
  141. modprobe $module
  142. if [ ! -d /sys/module/$module/sections ]
  143. then
  144. echo Module $module is not loaded
  145. exit 1
  146. fi
  147. cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
  148. cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
  149. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  150. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  151. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  152. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  153. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  154. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  155. echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  156. echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
  157. echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
  158. trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
  159. echo "Injecting errors into the module $module... (interrupt to stop)"
  160. sleep 1000000