fault-injection.txt 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Fault injection capabilities infrastructure
===========================================

See also drivers/md/faulty.c and "every_nth" module option for scsi_debug.


Available fault injection capabilities
--------------------------------------

o failslab

  injects slab allocation failures. (kmalloc(), kmem_cache_alloc(), ...)

o fail_page_alloc

  injects page allocation failures. (alloc_pages(), get_free_pages(), ...)

18 19 20 21
o fail_futex

  injects futex deadlock and uaddr fault errors.

22 23
o fail_make_request

24
  injects disk IO errors on devices permitted by setting
25 26 27
  /sys/block/<device>/make-it-fail or
  /sys/block/<device>/<partition>/make-it-fail. (generic_make_request())

28 29 30 31 32
o fail_mmc_request

  injects MMC data errors on devices permitted by setting
  debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request

33 34 35 36 37 38 39 40
Configure fault-injection capabilities behavior
-----------------------------------------------

o debugfs entries

fault-inject-debugfs kernel module provides some debugfs entries for runtime
configuration of fault-injection capabilities.

41
- /sys/kernel/debug/fail*/probability:
42 43 44 45

	likelihood of failure injection, in percent.
	Format: <percent>

46 47
	Note that one-failure-per-hundred is a very high error rate
	for some testcases.  Consider setting probability=100 and configure
48
	/sys/kernel/debug/fail*/interval for such testcases.
49

50
- /sys/kernel/debug/fail*/interval:
51 52 53 54 55 56 57

	specifies the interval between failures, for calls to
	should_fail() that pass all the other tests.

	Note that if you enable this, by setting interval>1, you will
	probably want to set probability=100.

58
- /sys/kernel/debug/fail*/times:
59 60 61 62

	specifies how many times failures may happen at most.
	A value of -1 means "no limit".

63
- /sys/kernel/debug/fail*/space:
64 65 66 67 68

	specifies an initial resource "budget", decremented by "size"
	on each call to should_fail(,size).  Failure injection is
	suppressed until "space" reaches zero.

69
- /sys/kernel/debug/fail*/verbose
70 71

	Format: { 0 | 1 | 2 }
72 73 74 75
	specifies the verbosity of the messages when failure is
	injected.  '0' means no messages; '1' will print only a single
	log line per failure; '2' will print a call trace too -- useful
	to debug the problems revealed by fault injection.
76

77
- /sys/kernel/debug/fail*/task-filter:
78

79 80
	Format: { 'Y' | 'N' }
	A value of 'N' disables filtering by process (default).
81 82 83
	Any positive value limits failures to only processes indicated by
	/proc/<pid>/make-it-fail==1.

84 85 86 87
- /sys/kernel/debug/fail*/require-start:
- /sys/kernel/debug/fail*/require-end:
- /sys/kernel/debug/fail*/reject-start:
- /sys/kernel/debug/fail*/reject-end:
88 89 90

	specifies the range of virtual addresses tested during
	stacktrace walking.  Failure is injected only if some caller
91 92 93 94
	in the walked stacktrace lies within the required range, and
	none lies within the rejected range.
	Default required range is [0,ULONG_MAX) (whole of virtual address space).
	Default rejected range is [0,0).
95

96
- /sys/kernel/debug/fail*/stacktrace-depth:
97 98

	specifies the maximum stacktrace depth walked during search
99 100
	for a caller within [require-start,require-end) OR
	[reject-start,reject-end).
101

102
- /sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem:
103

104 105
	Format: { 'Y' | 'N' }
	default is 'N', setting it to 'Y' won't inject failures into
106 107
	highmem/user allocations.

108 109
- /sys/kernel/debug/failslab/ignore-gfp-wait:
- /sys/kernel/debug/fail_page_alloc/ignore-gfp-wait:
110

111 112
	Format: { 'Y' | 'N' }
	default is 'N', setting it to 'Y' will inject failures
113 114
	only into non-sleep allocations (GFP_ATOMIC allocations).

115
- /sys/kernel/debug/fail_page_alloc/min-order:
116 117 118 119

	specifies the minimum page allocation order to be injected
	failures.

120 121 122 123 124 125
- /sys/kernel/debug/fail_futex/ignore-private:

	Format: { 'Y' | 'N' }
	default is 'N', setting it to 'Y' will disable failure injections
	when dealing with private (address space) futexes.

126 127 128 129 130 131 132
o Boot option

In order to inject faults while debugfs is not available (early boot time),
use the boot option:

	failslab=
	fail_page_alloc=
133
	fail_make_request=
134
	fail_futex=
135
	mmc_core.fail_request=<interval>,<probability>,<space>,<times>
136

137 138 139 140
o proc entries

- /proc/self/task/<current-tid>/fail-nth:

141
	Write to this file of integer N makes N-th call in the task fail.
142 143 144
	Read from this file returns a integer value. A value of '0' indicates
	that the fault setup with a previous write to this file was injected.
	A positive integer N indicates that the fault wasn't yet injected.
145 146 147 148 149 150 151 152
	Note that this file enables all types of faults (slab, futex, etc).
	This setting takes precedence over all other generic debugfs settings
	like probability, interval, times, etc. But per-capability settings
	(e.g. fail_futex/ignore-private) take precedence over it.

	This feature is intended for systematic testing of faults in a single
	system call. See an example below.

153 154 155 156 157 158 159 160 161 162 163 164
How to add new fault injection capability
-----------------------------------------

o #include <linux/fault-inject.h>

o define the fault attributes

  DECLARE_FAULT_INJECTION(name);

  Please see the definition of struct fault_attr in fault-inject.h
  for details.

165
o provide a way to configure fault attributes
166 167 168 169

- boot option

  If you need to enable the fault injection capability from boot time, you can
170
  provide boot option to configure it. There is a helper function for it:
171

172
	setup_fault_attr(attr, str);
173 174 175 176

- debugfs entries

  failslab, fail_page_alloc, and fail_make_request use this way.
177
  Helper functions:
178

179
	fault_create_debugfs_attr(name, parent, attr);
180 181 182 183 184 185 186 187 188

- module parameters

  If the scope of the fault injection capability is limited to a
  single kernel module, it is better to provide module parameters to
  configure the fault attributes.

o add a hook to insert failures

189
  Upon should_fail() returning true, client code should inject a failure.
190

191
	should_fail(attr, size);
192 193 194 195

Application Examples
--------------------

196
o Inject slab allocation failures into module init/exit code
197 198 199

#!/bin/bash

200
FAILTYPE=failslab
201 202 203 204 205 206 207
echo Y > /sys/kernel/debug/$FAILTYPE/task-filter
echo 10 > /sys/kernel/debug/$FAILTYPE/probability
echo 100 > /sys/kernel/debug/$FAILTYPE/interval
echo -1 > /sys/kernel/debug/$FAILTYPE/times
echo 0 > /sys/kernel/debug/$FAILTYPE/space
echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
208

209
faulty_system()
210
{
211
	bash -c "echo 1 > /proc/self/make-it-fail && exec $*"
212 213
}

214 215 216 217 218 219 220 221 222 223
if [ $# -eq 0 ]
then
	echo "Usage: $0 modulename [ modulename ... ]"
	exit 1
fi

for m in $*
do
	echo inserting $m...
	faulty_system modprobe $m
224

225 226 227
	echo removing $m...
	faulty_system modprobe -r $m
done
228 229 230

------------------------------------------------------------------------------

231
o Inject page allocation failures only for a specific module
232 233 234

#!/bin/bash

235 236
FAILTYPE=fail_page_alloc
module=$1
237

238 239 240 241 242
if [ -z $module ]
then
	echo "Usage: $0 <modulename>"
	exit 1
fi
243

244
modprobe $module
245

246 247 248 249 250 251
if [ ! -d /sys/module/$module/sections ]
then
	echo Module $module is not loaded
	exit 1
fi

252 253
cat /sys/module/$module/sections/.text > /sys/kernel/debug/$FAILTYPE/require-start
cat /sys/module/$module/sections/.data > /sys/kernel/debug/$FAILTYPE/require-end
254

255 256 257 258 259 260 261 262 263
echo N > /sys/kernel/debug/$FAILTYPE/task-filter
echo 10 > /sys/kernel/debug/$FAILTYPE/probability
echo 100 > /sys/kernel/debug/$FAILTYPE/interval
echo -1 > /sys/kernel/debug/$FAILTYPE/times
echo 0 > /sys/kernel/debug/$FAILTYPE/space
echo 2 > /sys/kernel/debug/$FAILTYPE/verbose
echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-wait
echo 1 > /sys/kernel/debug/$FAILTYPE/ignore-gfp-highmem
echo 10 > /sys/kernel/debug/$FAILTYPE/stacktrace-depth
264

265
trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT
266 267 268

echo "Injecting errors into the module $module... (interrupt to stop)"
sleep 1000000
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
Tool to run command with failslab or fail_page_alloc
----------------------------------------------------
In order to make it easier to accomplish the tasks mentioned above, we can use
tools/testing/fault-injection/failcmd.sh.  Please run a command
"./tools/testing/fault-injection/failcmd.sh --help" for more information and
see the following examples.

Examples:

Run a command "make -C tools/testing/selftests/ run_tests" with injecting slab
allocation failure.

	# ./tools/testing/fault-injection/failcmd.sh \
		-- make -C tools/testing/selftests/ run_tests

Same as above except to specify 100 times failures at most instead of one time
at most by default.

	# ./tools/testing/fault-injection/failcmd.sh --times=100 \
		-- make -C tools/testing/selftests/ run_tests

Same as above except to inject page allocation failure instead of slab
allocation failure.

	# env FAILCMD_TYPE=fail_page_alloc \
		./tools/testing/fault-injection/failcmd.sh --times=100 \
                -- make -C tools/testing/selftests/ run_tests
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

Systematic faults using fail-nth
---------------------------------

The following code systematically faults 0-th, 1-st, 2-nd and so on
capabilities in the socketpair() system call.

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main()
{
	int i, err, res, fail_nth, fds[2];
	char buf[128];

	system("echo N > /sys/kernel/debug/failslab/ignore-gfp-wait");
	sprintf(buf, "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid));
	fail_nth = open(buf, O_RDWR);
323
	for (i = 1;; i++) {
324 325 326 327
		sprintf(buf, "%d", i);
		write(fail_nth, buf, strlen(buf));
		res = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);
		err = errno;
328
		pread(fail_nth, buf, sizeof(buf), 0);
329 330 331 332
		if (res == 0) {
			close(fds[0]);
			close(fds[1]);
		}
333 334 335
		printf("%d-th fault %c: res=%d/%d\n", i, atoi(buf) ? 'N' : 'Y',
			res, err);
		if (atoi(buf))
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
			break;
	}
	return 0;
}

An example output:

1-th fault Y: res=-1/23
2-th fault Y: res=-1/23
3-th fault Y: res=-1/12
4-th fault Y: res=-1/12
5-th fault Y: res=-1/23
6-th fault Y: res=-1/23
7-th fault Y: res=-1/23
8-th fault Y: res=-1/12
9-th fault Y: res=-1/12
10-th fault Y: res=-1/12
11-th fault Y: res=-1/12
12-th fault Y: res=-1/12
13-th fault Y: res=-1/12
14-th fault Y: res=-1/12
15-th fault Y: res=-1/12
16-th fault N: res=0/12