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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
// 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 <linux/workqueue.h>
#include "blk.h"
#include "error-injection.h"
/*
* Cap the delay so that a typo can't wedge a device for good. This is still
* well beyond the default hung task timeout, which is one of the things a
* delay is useful for triggering.
*/
#define BLK_ERROR_INJECT_MAX_DELAY_US (600 * USEC_PER_SEC)
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;
/* hold the bio for this long before submitting or failing it */
unsigned int delay_us;
};
/*
* A bio held by a delay rule. It does not point back at the rule, so rules
* can be removed while delayed bios are outstanding. The submitter holds the
* bdev open until the bio completes, so the disk and queue stay allocated.
* A delayed bio holds no queue usage counter reference, so it does not hold
* up a queue freeze either.
*/
struct blk_error_inject_delay {
struct delayed_work dwork;
struct bio *bio;
blk_status_t status;
};
static struct workqueue_struct *blk_error_inject_wq;
DEFINE_STATIC_KEY_FALSE(blk_error_injection_enabled);
static void blk_error_inject_fail(struct bio *bio, blk_status_t status)
{
pr_info_ratelimited("%pg: injecting %s error for %s at sector %llu:%u\n",
bio->bi_bdev->bd_disk->part0, blk_status_to_str(status),
blk_op_str(bio_op(bio)), bio->bi_iter.bi_sector,
bio_sectors(bio));
bio_endio_status(bio, status);
}
static void blk_error_inject_delay_work(struct work_struct *work)
{
struct blk_error_inject_delay *d = container_of(to_delayed_work(work),
struct blk_error_inject_delay, dwork);
struct bio *bio = d->bio;
blk_status_t status = d->status;
kfree(d);
if (status != BLK_STS_OK) {
blk_error_inject_fail(bio, status);
return;
}
/*
* Resubmit. The BIO_ERROR_INJECTED flag ensures that a bio that was
* delayed once skips error injection entirely from here on, including
* any other rule that covers it.
*/
submit_bio_noacct_nocheck(bio, false);
}
static bool blk_error_inject_delay(struct gendisk *disk, struct bio *bio,
blk_status_t status, unsigned int delay_us)
{
struct blk_error_inject_delay *d;
/* fail a bio that can't block, as a real queue would */
if (bio->bi_opf & REQ_NOWAIT) {
bio_wouldblock_error(bio);
return true;
}
d = kmalloc_obj(*d, GFP_NOIO);
if (!d) {
pr_warn_ratelimited("%pg: failed to delay %s at sector %llu:%u\n",
disk->part0, blk_op_str(bio_op(bio)),
bio->bi_iter.bi_sector, bio_sectors(bio));
return false;
}
pr_info_ratelimited("%pg: delaying %s at sector %llu:%u by %uus\n",
disk->part0, blk_op_str(bio_op(bio)),
bio->bi_iter.bi_sector, bio_sectors(bio), delay_us);
d->bio = bio;
d->status = status;
INIT_DELAYED_WORK(&d->dwork, blk_error_inject_delay_work);
queue_delayed_work(blk_error_inject_wq, &d->dwork,
usecs_to_jiffies(delay_us));
return true;
}
bool __blk_error_inject(struct bio *bio)
{
struct gendisk *disk = bio->bi_bdev->bd_disk;
struct blk_error_inject *inj;
blk_status_t status = BLK_STS_OK;
unsigned int delay_us = 0;
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;
status = inj->status;
delay_us = inj->delay_us;
bio_set_flag(bio, BIO_ERROR_INJECTED);
break;
}
rcu_read_unlock();
if (delay_us && blk_error_inject_delay(disk, bio, status, delay_us))
return true;
if (status == BLK_STS_OK)
return false;
blk_error_inject_fail(bio, status);
return true;
}
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, unsigned int delay_us)
{
struct blk_error_inject *inj;
int error = -EINVAL;
if (op == REQ_OP_LAST)
return -EINVAL;
if (status == BLK_STS_OK && !delay_us)
return -EINVAL;
if (delay_us > BLK_ERROR_INJECT_MAX_DELAY_US)
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;
inj->delay_us = delay_us;
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_delay_us = (1u << 21),
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_delay_us, "delay_us=%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;
bool found;
tag = match_strdup(args);
if (!tag)
return -ENOMEM;
found = tag_to_blk_status(tag, status);
if (!found)
pr_warn("invalid status '%s'\n", tag);
kfree(tag);
return found ? 0 : -EINVAL;
}
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, delay_us = 0;
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;
case Opt_delay_us:
error = match_uint(args, &delay_us);
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, delay_us);
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 op=%s,status=%s,chance=%u,delay_us=%u",
inj->start, inj->end,
blk_op_str(inj->op),
blk_status_to_tag(inj->status), inj->chance,
inj->delay_us);
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);
}
static int __init blk_error_injection_init_wq(void)
{
blk_error_inject_wq = alloc_workqueue("blk_error_inject",
WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
if (!blk_error_inject_wq)
panic("Failed to create blk_error_inject wq\n");
return 0;
}
subsys_initcall(blk_error_injection_init_wq);
|