fault-injection.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. - /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. /debug/fail*/interval for such testcases.
  25. - /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. - /debug/fail*/times:
  31. specifies how many times failures may happen at most.
  32. A value of -1 means "no limit".
  33. - /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. - /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. - /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. - /debug/fail*/require-start:
  49. - /debug/fail*/require-end:
  50. - /debug/fail*/reject-start:
  51. - /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. - /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. - /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. - /debug/failslab/ignore-gfp-wait:
  67. - /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. o Boot option
  72. In order to inject faults while debugfs is not available (early boot time),
  73. use the boot option:
  74. failslab=
  75. fail_page_alloc=
  76. fail_make_request=<interval>,<probability>,<space>,<times>
  77. How to add new fault injection capability
  78. -----------------------------------------
  79. o #include <linux/fault-inject.h>
  80. o define the fault attributes
  81. DECLARE_FAULT_INJECTION(name);
  82. Please see the definition of struct fault_attr in fault-inject.h
  83. for details.
  84. o provide a way to configure fault attributes
  85. - boot option
  86. If you need to enable the fault injection capability from boot time, you can
  87. provide boot option to configure it. There is a helper function for it:
  88. setup_fault_attr(attr, str);
  89. - debugfs entries
  90. failslab, fail_page_alloc, and fail_make_request use this way.
  91. Helper functions:
  92. init_fault_attr_entries(entries, attr, name);
  93. void cleanup_fault_attr_entries(entries);
  94. - module parameters
  95. If the scope of the fault injection capability is limited to a
  96. single kernel module, it is better to provide module parameters to
  97. configure the fault attributes.
  98. o add a hook to insert failures
  99. Upon should_fail() returning true, client code should inject a failure.
  100. should_fail(attr, size);
  101. Application Examples
  102. --------------------
  103. o inject slab allocation failures into module init/cleanup code
  104. ------------------------------------------------------------------------------
  105. #!/bin/bash
  106. FAILCMD=Documentation/fault-injection/failcmd.sh
  107. BLACKLIST="root_plug evbug"
  108. FAILNAME=failslab
  109. echo Y > /debug/$FAILNAME/task-filter
  110. echo 10 > /debug/$FAILNAME/probability
  111. echo 100 > /debug/$FAILNAME/interval
  112. echo -1 > /debug/$FAILNAME/times
  113. echo 2 > /debug/$FAILNAME/verbose
  114. echo 1 > /debug/$FAILNAME/ignore-gfp-wait
  115. blacklist()
  116. {
  117. echo $BLACKLIST | grep $1 > /dev/null 2>&1
  118. }
  119. oops()
  120. {
  121. dmesg | grep BUG > /dev/null 2>&1
  122. }
  123. find /lib/modules/`uname -r` -name '*.ko' -exec basename {} .ko \; |
  124. while read i
  125. do
  126. oops && exit 1
  127. if ! blacklist $i
  128. then
  129. echo inserting $i...
  130. bash $FAILCMD modprobe $i
  131. fi
  132. done
  133. lsmod | awk '{ if ($3 == 0) { print $1 } }' |
  134. while read i
  135. do
  136. oops && exit 1
  137. if ! blacklist $i
  138. then
  139. echo removing $i...
  140. bash $FAILCMD modprobe -r $i
  141. fi
  142. done
  143. ------------------------------------------------------------------------------
  144. o inject slab allocation failures only for a specific module
  145. ------------------------------------------------------------------------------
  146. #!/bin/bash
  147. FAILMOD=Documentation/fault-injection/failmodule.sh
  148. echo injecting errors into the module $1...
  149. modprobe $1
  150. bash $FAILMOD failslab $1 10
  151. echo 25 > /debug/failslab/probability
  152. ------------------------------------------------------------------------------