fault-injection.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. ===========================================
  2. Fault injection capabilities infrastructure
  3. ===========================================
  4. See also "every_nth" module option for scsi_debug.
  5. Available fault injection capabilities
  6. --------------------------------------
  7. - failslab
  8. injects slab allocation failures. (kmalloc(), kmem_cache_alloc(), ...)
  9. - fail_page_alloc
  10. injects page allocation failures. (alloc_pages(), get_free_pages(), ...)
  11. - fail_usercopy
  12. injects failures in user memory access functions. (copy_from_user(), get_user(), ...)
  13. - fail_futex
  14. injects futex deadlock and uaddr fault errors.
  15. - fail_sunrpc
  16. injects kernel RPC client and server failures.
  17. - fail_make_request
  18. injects disk IO errors on devices permitted by setting
  19. /sys/block/<device>/make-it-fail or
  20. /sys/block/<device>/<partition>/make-it-fail. (submit_bio_noacct())
  21. - fail_mmc_request
  22. injects MMC data errors on devices permitted by setting
  23. debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
  24. - fail_function
  25. injects error return on specific functions, which are marked by
  26. ALLOW_ERROR_INJECTION() macro, by setting debugfs entries
  27. under /sys/kernel/debug/fail_function. No boot option supported.
  28. - fail_skb_realloc
  29. inject skb (socket buffer) reallocation events into the network path. The
  30. primary goal is to identify and prevent issues related to pointer
  31. mismanagement in the network subsystem. By forcing skb reallocation at
  32. strategic points, this feature creates scenarios where existing pointers to
  33. skb headers become invalid.
  34. When the fault is injected and the reallocation is triggered, cached pointers
  35. to skb headers and data no longer reference valid memory locations. This
  36. deliberate invalidation helps expose code paths where proper pointer updating
  37. is neglected after a reallocation event.
  38. By creating these controlled fault scenarios, the system can catch instances
  39. where stale pointers are used, potentially leading to memory corruption or
  40. system instability.
  41. To select the interface to act on, write the network name to
  42. /sys/kernel/debug/fail_skb_realloc/devname.
  43. If this field is left empty (which is the default value), skb reallocation
  44. will be forced on all network interfaces.
  45. The effectiveness of this fault detection is enhanced when KASAN is
  46. enabled, as it helps identify invalid memory references and use-after-free
  47. (UAF) issues.
  48. - NVMe fault injection
  49. inject NVMe status code and retry flag on devices permitted by setting
  50. debugfs entries under /sys/kernel/debug/nvme*/fault_inject. The default
  51. status code is NVME_SC_INVALID_OPCODE with no retry. The status code and
  52. retry flag can be set via the debugfs.
  53. - Null test block driver fault injection
  54. inject IO timeouts by setting config items under
  55. /sys/kernel/config/nullb/<disk>/timeout_inject,
  56. inject requeue requests by setting config items under
  57. /sys/kernel/config/nullb/<disk>/requeue_inject, and
  58. inject init_hctx() errors by setting config items under
  59. /sys/kernel/config/nullb/<disk>/init_hctx_fault_inject.
  60. Configure fault-injection capabilities behavior
  61. -----------------------------------------------
  62. debugfs entries
  63. ^^^^^^^^^^^^^^^
  64. fault-inject-debugfs kernel module provides some debugfs entries for runtime
  65. configuration of fault-injection capabilities.
  66. - /sys/kernel/debug/fail*/probability:
  67. likelihood of failure injection, in percent.
  68. Format: <percent>
  69. Note that one-failure-per-hundred is a very high error rate
  70. for some testcases. Consider setting probability=100 and configure
  71. /sys/kernel/debug/fail*/interval for such testcases.
  72. - /sys/kernel/debug/fail*/interval:
  73. specifies the interval between failures, for calls to
  74. should_fail() that pass all the other tests.
  75. Note that if you enable this, by setting interval>1, you will
  76. probably want to set probability=100.
  77. - /sys/kernel/debug/fail*/times:
  78. specifies how many times failures may happen at most. A value of -1
  79. means "no limit".
  80. - /sys/kernel/debug/fail*/space:
  81. specifies an initial resource "budget", decremented by "size"
  82. on each call to should_fail(,size). Failure injection is
  83. suppressed until "space" reaches zero.
  84. - /sys/kernel/debug/fail*/verbose
  85. Format: { 0 | 1 | 2 }
  86. specifies the verbosity of the messages when failure is
  87. injected. '0' means no messages; '1' will print only a single
  88. log line per failure; '2' will print a call trace too -- useful
  89. to debug the problems revealed by fault injection.
  90. - /sys/kernel/debug/fail*/task-filter:
  91. Format: { 'Y' | 'N' }
  92. A value of 'N' disables filtering by process (default).
  93. Any positive value limits failures to only processes indicated by
  94. /proc/<pid>/make-it-fail==1.
  95. - /sys/kernel/debug/fail*/require-start,
  96. /sys/kernel/debug/fail*/require-end,
  97. /sys/kernel/debug/fail*/reject-start,
  98. /sys/kernel/debug/fail*/reject-end:
  99. specifies the range of virtual addresses tested during
  100. stacktrace walking. Failure is injected only if some caller
  101. in the walked stacktrace lies within the required range, and
  102. none lies within the rejected range.
  103. Default required range is [0,ULONG_MAX) (whole of virtual address space).
  104. Default rejected range is [0,0).
  105. - /sys/kernel/debug/fail*/stacktrace-depth:
  106. specifies the maximum stacktrace depth walked during search
  107. for a caller within [require-start,require-end) OR
  108. [reject-start,reject-end).
  109. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
  110. Format: { 'Y' | 'N' }
  111. default is 'Y', setting it to 'N' will also inject failures into
  112. highmem/user allocations (__GFP_HIGHMEM allocations).
  113. - /sys/kernel/debug/failslab/cache-filter
  114. Format: { 'Y' | 'N' }
  115. default is 'N', setting it to 'Y' will only inject failures when
  116. objects are requests from certain caches.
  117. Select the cache by writing '1' to /sys/kernel/slab/<cache>/failslab:
  118. - /sys/kernel/debug/failslab/ignore-gfp-wait:
  119. - /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
  120. Format: { 'Y' | 'N' }
  121. default is 'Y', setting it to 'N' will also inject failures
  122. into allocations that can sleep (__GFP_DIRECT_RECLAIM allocations).
  123. - /sys/kernel/debug/fail_page_alloc/min-order:
  124. specifies the minimum page allocation order to be injected
  125. failures.
  126. - /sys/kernel/debug/fail_futex/ignore-private:
  127. Format: { 'Y' | 'N' }
  128. default is 'N', setting it to 'Y' will disable failure injections
  129. when dealing with private (address space) futexes.
  130. - /sys/kernel/debug/fail_sunrpc/ignore-client-disconnect:
  131. Format: { 'Y' | 'N' }
  132. default is 'N', setting it to 'Y' will disable disconnect
  133. injection on the RPC client.
  134. - /sys/kernel/debug/fail_sunrpc/ignore-server-disconnect:
  135. Format: { 'Y' | 'N' }
  136. default is 'N', setting it to 'Y' will disable disconnect
  137. injection on the RPC server.
  138. - /sys/kernel/debug/fail_sunrpc/ignore-cache-wait:
  139. Format: { 'Y' | 'N' }
  140. default is 'N', setting it to 'Y' will disable cache wait
  141. injection on the RPC server.
  142. - /sys/kernel/debug/fail_function/inject:
  143. Format: { 'function-name' | '!function-name' | '' }
  144. specifies the target function of error injection by name.
  145. If the function name leads '!' prefix, given function is
  146. removed from injection list. If nothing specified ('')
  147. injection list is cleared.
  148. - /sys/kernel/debug/fail_function/injectable:
  149. (read only) shows error injectable functions and what type of
  150. error values can be specified. The error type will be one of
  151. below;
  152. - NULL: retval must be 0.
  153. - ERRNO: retval must be -1 to -MAX_ERRNO (-4096).
  154. - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096).
  155. - /sys/kernel/debug/fail_function/<function-name>/retval:
  156. specifies the "error" return value to inject to the given function.
  157. This will be created when the user specifies a new injection entry.
  158. Note that this file only accepts unsigned values. So, if you want to
  159. use a negative errno, you better use 'printf' instead of 'echo', e.g.:
  160. $ printf %#x -12 > retval
  161. - /sys/kernel/debug/fail_skb_realloc/devname:
  162. Specifies the network interface on which to force SKB reallocation. If
  163. left empty, SKB reallocation will be applied to all network interfaces.
  164. Example usage::
  165. # Force skb reallocation on eth0
  166. echo "eth0" > /sys/kernel/debug/fail_skb_realloc/devname
  167. # Clear the selection and force skb reallocation on all interfaces
  168. echo "" > /sys/kernel/debug/fail_skb_realloc/devname
  169. Boot option
  170. ^^^^^^^^^^^
  171. In order to inject faults while debugfs is not available (early boot time),
  172. use the boot option::
  173. failslab=
  174. fail_page_alloc=
  175. fail_usercopy=
  176. fail_make_request=
  177. fail_futex=
  178. fail_skb_realloc=
  179. mmc_core.fail_request=<interval>,<probability>,<space>,<times>
  180. proc entries
  181. ^^^^^^^^^^^^
  182. - /proc/<pid>/fail-nth,
  183. /proc/self/task/<tid>/fail-nth:
  184. Write to this file of integer N makes N-th call in the task fail.
  185. Read from this file returns a integer value. A value of '0' indicates
  186. that the fault setup with a previous write to this file was injected.
  187. A positive integer N indicates that the fault wasn't yet injected.
  188. Note that this file enables all types of faults (slab, futex, etc).
  189. This setting takes precedence over all other generic debugfs settings
  190. like probability, interval, times, etc. But per-capability settings
  191. (e.g. fail_futex/ignore-private) take precedence over it.
  192. This feature is intended for systematic testing of faults in a single
  193. system call. See an example below.
  194. Error Injectable Functions
  195. --------------------------
  196. This part is for the kernel developers considering to add a function to
  197. ALLOW_ERROR_INJECTION() macro.
  198. Requirements for the Error Injectable Functions
  199. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  200. Since the function-level error injection forcibly changes the code path
  201. and returns an error even if the input and conditions are proper, this can
  202. cause unexpected kernel crash if you allow error injection on the function
  203. which is NOT error injectable. Thus, you (and reviewers) must ensure;
  204. - The function returns an error code if it fails, and the callers must check
  205. it correctly (need to recover from it).
  206. - The function does not execute any code which can change any state before
  207. the first error return. The state includes global or local, or input
  208. variable. For example, clear output address storage (e.g. `*ret = NULL`),
  209. increments/decrements counter, set a flag, preempt/irq disable or get
  210. a lock (if those are recovered before returning error, that will be OK.)
  211. The first requirement is important, and it will result in that the release
  212. (free objects) functions are usually harder to inject errors than allocate
  213. functions. If errors of such release functions are not correctly handled
  214. it will cause a memory leak easily (the caller will confuse that the object
  215. has been released or corrupted.)
  216. The second one is for the caller which expects the function should always
  217. does something. Thus if the function error injection skips whole of the
  218. function, the expectation is betrayed and causes an unexpected error.
  219. Type of the Error Injectable Functions
  220. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  221. Each error injectable functions will have the error type specified by the
  222. ALLOW_ERROR_INJECTION() macro. You have to choose it carefully if you add
  223. a new error injectable function. If the wrong error type is chosen, the
  224. kernel may crash because it may not be able to handle the error.
  225. There are 4 types of errors defined in include/asm-generic/error-injection.h
  226. EI_ETYPE_NULL
  227. This function will return `NULL` if it fails. e.g. return an allocated
  228. object address.
  229. EI_ETYPE_ERRNO
  230. This function will return an `-errno` error code if it fails. e.g. return
  231. -EINVAL if the input is wrong. This will include the functions which will
  232. return an address which encodes `-errno` by ERR_PTR() macro.
  233. EI_ETYPE_ERRNO_NULL
  234. This function will return an `-errno` or `NULL` if it fails. If the caller
  235. of this function checks the return value with IS_ERR_OR_NULL() macro, this
  236. type will be appropriate.
  237. EI_ETYPE_TRUE
  238. This function will return `true` (non-zero positive value) if it fails.
  239. If you specifies a wrong type, for example, EI_TYPE_ERRNO for the function
  240. which returns an allocated object, it may cause a problem because the returned
  241. value is not an object address and the caller can not access to the address.
  242. How to add new fault injection capability
  243. -----------------------------------------
  244. - #include <linux/fault-inject.h>
  245. - define the fault attributes
  246. DECLARE_FAULT_ATTR(name);
  247. Please see the definition of struct fault_attr in fault-inject.h
  248. for details.
  249. - provide a way to configure fault attributes
  250. - boot option
  251. If you need to enable the fault injection capability from boot time, you can
  252. provide boot option to configure it. There is a helper function for it:
  253. setup_fault_attr(attr, str);
  254. - debugfs entries
  255. failslab, fail_page_alloc, fail_usercopy, and fail_make_request use this way.
  256. Helper functions:
  257. fault_create_debugfs_attr(name, parent, attr);
  258. - module parameters
  259. If the scope of the fault injection capability is limited to a
  260. single kernel module, it is better to provide module parameters to
  261. configure the fault attributes.
  262. - add a hook to insert failures
  263. Upon should_fail() returning true, client code should inject a failure:
  264. should_fail(attr, size);
  265. Application Examples
  266. --------------------
  267. - Inject slab allocation failures into module init/exit code::
  268. #!/bin/bash
  269. FAILTYPE=failslab
  270. echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
  271. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  272. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  273. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  274. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  275. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  276. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  277. faulty_system()
  278. {
  279. bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
  280. }
  281. if [ $# -eq 0 ]
  282. then
  283. echo "Usage: $0 modulename [ modulename ... ]"
  284. exit 1
  285. fi
  286. for m in $*
  287. do
  288. echo inserting $m...
  289. faulty_system modprobe $m
  290. echo removing $m...
  291. faulty_system modprobe -r $m
  292. done
  293. ------------------------------------------------------------------------------
  294. - Inject page allocation failures only for a specific module::
  295. #!/bin/bash
  296. FAILTYPE=fail_page_alloc
  297. module=$1
  298. if [ -z $module ]
  299. then
  300. echo "Usage: $0 <modulename>"
  301. exit 1
  302. fi
  303. modprobe $module
  304. if [ ! -d /sys/module/$module/sections ]
  305. then
  306. echo Module $module is not loaded
  307. exit 1
  308. fi
  309. cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
  310. cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
  311. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  312. echo 10 > /sys/kernel/debug/$FAILTYPE/probability
  313. echo 100 > /sys/kernel/debug/$FAILTYPE/interval
  314. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  315. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  316. echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
  317. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
  318. echo Y > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
  319. echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
  320. trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
  321. echo "Injecting errors into the module $module... (interrupt to stop)"
  322. sleep 1000000
  323. ------------------------------------------------------------------------------
  324. - Inject open_ctree error while btrfs mount::
  325. #!/bin/bash
  326. rm -f testfile.img
  327. dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1
  328. DEVICE=$(losetup --show -f testfile.img)
  329. mkfs.btrfs -f $DEVICE
  330. mkdir -p tmpmnt
  331. FAILTYPE=fail_function
  332. FAILFUNC=open_ctree
  333. echo $FAILFUNC > /sys/kernel/debug/$FAILTYPE/inject
  334. printf %#x -12 > /sys/kernel/debug/$FAILTYPE/$FAILFUNC/retval
  335. echo N > /sys/kernel/debug/$FAILTYPE/task-filter
  336. echo 100 > /sys/kernel/debug/$FAILTYPE/probability
  337. echo 0 > /sys/kernel/debug/$FAILTYPE/interval
  338. echo -1 > /sys/kernel/debug/$FAILTYPE/times
  339. echo 0 > /sys/kernel/debug/$FAILTYPE/space
  340. echo 1 > /sys/kernel/debug/$FAILTYPE/verbose
  341. mount -t btrfs $DEVICE tmpmnt
  342. if [ $? -ne 0 ]
  343. then
  344. echo "SUCCESS!"
  345. else
  346. echo "FAILED!"
  347. umount tmpmnt
  348. fi
  349. echo > /sys/kernel/debug/$FAILTYPE/inject
  350. rmdir tmpmnt
  351. losetup -d $DEVICE
  352. rm testfile.img
  353. ------------------------------------------------------------------------------
  354. - Inject only skbuff allocation failures ::
  355. # mark skbuff_head_cache as faulty
  356. echo 1 > /sys/kernel/slab/skbuff_head_cache/failslab
  357. # Turn on cache filter (off by default)
  358. echo 1 > /sys/kernel/debug/failslab/cache-filter
  359. # Turn on fault injection
  360. echo 1 > /sys/kernel/debug/failslab/times
  361. echo 1 > /sys/kernel/debug/failslab/probability
  362. Tool to run command with failslab or fail_page_alloc
  363. ----------------------------------------------------
  364. In order to make it easier to accomplish the tasks mentioned above, we can use
  365. tools/testing/fault-injection/failcmd.sh. Please run a command
  366. "./tools/testing/fault-injection/failcmd.sh --help" for more information and
  367. see the following examples.
  368. Examples:
  369. Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
  370. allocation failure::
  371. # ./tools/testing/fault-injection/failcmd.sh \
  372. -- make -C tools/testing/selftests/ run_tests
  373. Same as above except to specify 100 times failures at most instead of one time
  374. at most by default::
  375. # ./tools/testing/fault-injection/failcmd.sh --times=100 \
  376. -- make -C tools/testing/selftests/ run_tests
  377. Same as above except to inject page allocation failure instead of slab
  378. allocation failure::
  379. # env FAILCMD_TYPE=fail_page_alloc \
  380. ./tools/testing/fault-injection/failcmd.sh --times=100 \
  381. -- make -C tools/testing/selftests/ run_tests
  382. Systematic faults using fail-nth
  383. ---------------------------------
  384. The following code systematically faults 0-th, 1-st, 2-nd and so on
  385. capabilities in the socketpair() system call::
  386. #include <sys/types.h>
  387. #include <sys/stat.h>
  388. #include <sys/socket.h>
  389. #include <sys/syscall.h>
  390. #include <fcntl.h>
  391. #include <unistd.h>
  392. #include <string.h>
  393. #include <stdlib.h>
  394. #include <stdio.h>
  395. #include <errno.h>
  396. int main()
  397. {
  398. int i, err, res, fail_nth, fds[2];
  399. char buf[128];
  400. system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
  401. sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
  402. fail_nth = open(buf, O_RDWR);
  403. for (i = 1;; i++) {
  404. sprintf(buf, "%d", i);
  405. write(fail_nth, buf, strlen(buf));
  406. res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
  407. err = errno;
  408. pread(fail_nth, buf, sizeof(buf), 0);
  409. if (res == 0) {
  410. close(fds[0]);
  411. close(fds[1]);
  412. }
  413. printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
  414. res, err);
  415. if (atoi(buf))
  416. break;
  417. }
  418. return 0;
  419. }
  420. An example output::
  421. 1-th fault Y: res=-1/23
  422. 2-th fault Y: res=-1/23
  423. 3-th fault Y: res=-1/12
  424. 4-th fault Y: res=-1/12
  425. 5-th fault Y: res=-1/23
  426. 6-th fault Y: res=-1/23
  427. 7-th fault Y: res=-1/23
  428. 8-th fault Y: res=-1/12
  429. 9-th fault Y: res=-1/12
  430. 10-th fault Y: res=-1/12
  431. 11-th fault Y: res=-1/12
  432. 12-th fault Y: res=-1/12
  433. 13-th fault Y: res=-1/12
  434. 14-th fault Y: res=-1/12
  435. 15-th fault Y: res=-1/12
  436. 16-th fault N: res=0/12