summaryrefslogtreecommitdiff
path: root/block/error-injection.c
blob: cfb83138960c866825837a47c1f4e3b7d4289ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2026 Christoph Hellwig.
 */
#include <linux/debugfs.h>
#include <linux/blkdev.h>
#include <linux/parser.h>
#include <linux/seq_file.h>
#include "blk.h"
#include "error-injection.h"

struct blk_error_inject {
	struct list_head		entry;
	sector_t			start;
	sector_t			end;
	enum req_op			op;
	blk_status_t			status;

	/* only inject every 1 / chance times */
	unsigned int			chance;
};

DEFINE_STATIC_KEY_FALSE(blk_error_injection_enabled);

bool __blk_error_inject(struct bio *bio)
{
	struct gendisk *disk = bio->bi_bdev->bd_disk;
	struct blk_error_inject *inj;

	rcu_read_lock();
	list_for_each_entry_rcu(inj, &disk->error_injection_list, entry) {
		if (bio_op(bio) != inj->op)
			continue;
		/*
		 * This never matches 0-sized bios like empty WRITEs with
		 * REQ_PREFLUSH or ZONE_RESET_ALL.  While adding a special case
		 * for them would be trivial, that means any WRITE rule would
		 * trigger for flushes.  So before we can make this work
		 * properly, we'll need to start using REQ_OP_FLUSH for pure
		 * flushes at the bio level like we already do in blk-mq.
		 */
		if (bio->bi_iter.bi_sector > inj->end ||
		    bio_end_sector(bio) <= inj->start)
			continue;
		if (inj->chance > 1 && (get_random_u32() % inj->chance) != 0)
			continue;

		pr_info_ratelimited("%pg: injecting %s error for %s at sector %llu:%u\n",
				disk->part0, blk_status_to_str(inj->status),
				blk_op_str(inj->op), bio->bi_iter.bi_sector,
				bio_sectors(bio));
		bio->bi_status = inj->status;
		rcu_read_unlock();
		bio_endio(bio);
		return true;
	}
	rcu_read_unlock();
	return false;
}

static int error_inject_add(struct gendisk *disk, enum req_op op,
		sector_t start, u64 nr_sectors, blk_status_t status,
		unsigned int chance)
{
	struct blk_error_inject *inj;
	int error = -EINVAL;

	if (op == REQ_OP_LAST)
		return -EINVAL;
	if (status == BLK_STS_OK)
		return -EINVAL;

	inj = kzalloc_obj(*inj);
	if (!inj)
		return -ENOMEM;

	if (nr_sectors) {
		if (U64_MAX - nr_sectors < start)
			goto out_free_inj;
		inj->end = start + nr_sectors - 1;
	} else {
		inj->end = U64_MAX;
	}

	inj->op = op;
	inj->start = start;
	inj->status = status;
	inj->chance = chance;

	pr_debug_ratelimited("%pg: adding %s injection for %s at sector %llu:%llu\n",
			disk->part0, blk_status_to_str(status),
			blk_op_str(op),
			start, nr_sectors);

	/*
	 * Add to the front of the list so that newer entries can partially
	 * override other entries.  This also intentionally allows duplicate
	 * entries as there is no real reason to reject them.
	 */
	mutex_lock(&disk->error_injection_lock);
	if (!disk_live(disk)) {
		mutex_unlock(&disk->error_injection_lock);
		error = -ENODEV;
		goto out_free_inj;
	}
	if (list_empty(&disk->error_injection_list))
		static_branch_inc(&blk_error_injection_enabled);
	list_add_rcu(&inj->entry, &disk->error_injection_list);
	set_bit(GD_ERROR_INJECT, &disk->state);
	mutex_unlock(&disk->error_injection_lock);
	return 0;

out_free_inj:
	kfree(inj);
	return error;
}

static void error_inject_removeall(struct gendisk *disk)
{
	struct blk_error_inject *inj;

	mutex_lock(&disk->error_injection_lock);
	if (test_and_clear_bit(GD_ERROR_INJECT, &disk->state))
		static_branch_dec(&blk_error_injection_enabled);
	while ((inj = list_first_entry_or_null(&disk->error_injection_list,
			struct blk_error_inject, entry))) {
		list_del_rcu(&inj->entry);
		kfree_rcu_mightsleep(inj);
	}
	mutex_unlock(&disk->error_injection_lock);
}

enum options {
	Opt_add			= (1u << 0),
	Opt_removeall		= (1u << 1),

	Opt_op			= (1u << 16),
	Opt_start		= (1u << 17),
	Opt_nr_sectors		= (1u << 18),
	Opt_status		= (1u << 19),
	Opt_chance		= (1u << 20),

	Opt_invalid,
};

static const match_table_t opt_tokens = {
	{ Opt_add,			"add",			},
	{ Opt_removeall,		"removeall",		},
	{ Opt_op,			"op=%s",		},
	{ Opt_start,			"start=%u"		},
	{ Opt_nr_sectors,		"nr_sectors=%u"		},
	{ Opt_status,			"status=%s"		},
	{ Opt_chance,			"chance=%u"		},
	{ Opt_invalid,			NULL,			},
};

static int match_op(substring_t *args, enum req_op *op)
{
	const char *tag;

	tag = match_strdup(args);
	if (!tag)
		return -ENOMEM;
	*op = str_to_blk_op(tag);
	if (*op == REQ_OP_LAST)
		pr_warn("invalid op '%s'\n", tag);
	kfree(tag);
	return 0;
}

static int match_status(substring_t *args, blk_status_t *status)
{
	const char *tag;

	tag = match_strdup(args);
	if (!tag)
		return -ENOMEM;
	*status = tag_to_blk_status(tag);
	if (!*status)
		pr_warn("invalid status '%s'\n", tag);
	kfree(tag);
	return 0;
}

static ssize_t blk_error_injection_parse_options(struct gendisk *disk,
		char *options)
{
	enum { Unset, Add, Removeall } action = Unset;
	unsigned int option_mask = 0, chance = 1;
	enum req_op op = REQ_OP_LAST;
	u64 start = 0, nr_sectors = 0;
	blk_status_t status = BLK_STS_OK;
	substring_t args[MAX_OPT_ARGS];
	char *p;

	while ((p = strsep(&options, ",\n")) != NULL) {
		int error = 0;
		ssize_t token;

		if (!*p)
			continue;
		token = match_token(p, opt_tokens, args);
		option_mask |= token;
		switch (token) {
		case Opt_add:
			if (action != Unset)
				return -EINVAL;
			action = Add;
			break;
		case Opt_removeall:
			if (action != Unset)
				return -EINVAL;
			action = Removeall;
			break;
		case Opt_op:
			error = match_op(args, &op);
			break;
		case Opt_start:
			error = match_u64(args, &start);
			break;
		case Opt_nr_sectors:
			error = match_u64(args, &nr_sectors);
			break;
		case Opt_status:
			error = match_status(args, &status);
			break;
		case Opt_chance:
			error = match_uint(args, &chance);
			if (!error && chance == 0)
				error = -EINVAL;
			break;
		default:
			pr_warn("unknown parameter or missing value '%s'\n", p);
			error = -EINVAL;
		}
		if (error)
			return error;
	}

	switch (action) {
	case Add:
		return error_inject_add(disk, op, start, nr_sectors, status,
				chance);
	case Removeall:
		if (option_mask & ~Opt_removeall)
			return -EINVAL;
		error_inject_removeall(disk);
		return 0;
	default:
		return -EINVAL;
	}
}

static ssize_t blk_error_injection_write(struct file *file,
		const char __user *ubuf, size_t count, loff_t *pos)
{
	struct gendisk *disk = file_inode(file)->i_private;
	char *options;
	int error;

	options = memdup_user_nul(ubuf, count);
	if (IS_ERR(options))
		return PTR_ERR(options);
	error = blk_error_injection_parse_options(disk, options);
	kfree(options);

	if (error)
		return error;
	return count;
}

static int blk_error_injection_show(struct seq_file *s, void *private)
{
	struct gendisk *disk = s->private;
	struct blk_error_inject *inj;

	rcu_read_lock();
	list_for_each_entry_rcu(inj, &disk->error_injection_list, entry) {
		seq_printf(s, "%llu:%llu status=%s,chance=%u",
			inj->start, inj->end,
			blk_status_to_tag(inj->status), inj->chance);
		seq_putc(s, '\n');
	}
	rcu_read_unlock();
	return 0;
}

static int blk_error_injection_open(struct inode *inode, struct file *file)
{
	return single_open(file, blk_error_injection_show, inode->i_private);
}

static int blk_error_injection_release(struct inode *inode, struct file *file)
{
	return single_release(inode, file);
}

static const struct file_operations blk_error_injection_fops = {
	.owner		= THIS_MODULE,
	.write		= blk_error_injection_write,
	.read		= seq_read,
	.open		= blk_error_injection_open,
	.release	= blk_error_injection_release,
};

void blk_error_injection_init(struct gendisk *disk)
{
	debugfs_create_file("error_injection", 0600, disk->queue->debugfs_dir,
			disk, &blk_error_injection_fops);
}

void blk_error_injection_exit(struct gendisk *disk)
{
	error_inject_removeall(disk);
}