Re: [PATCH 1/2] block: hold the disk while error injection delays a bio
From: Haris Iqbal
Date: Sat Dec 05 2026 - 07:59:21 EST
On 12/5/26 13:55, Md Haris Iqbal wrote:
A bio delayed by an error injection rule is resubmitted from a
workqueue, up to 600 seconds later. Nothing holds the disk during that
time: the bio has not entered the queue yet, so it holds no queue usage
counter and del_gendisk() does not wait for it. The comment claiming
that the submitter holds the bdev open is wrong: nvme multipath remaps
bios to the hidden namespace disk without opening it, so the disk can be
freed while the bio waits. For disks that don't own their queue, such
as sd and sr, the queue is no longer dying after del_gendisk(), and the
resubmitted bio reaches the driver after the device was removed.
Take a reference to the bdev and the driver module when delaying a bio,
and fail the bio instead of resubmitting it if the disk is dead by then.
The module reference is needed because dropping the last disk reference
can call the driver's ->free_disk().
Fixes: 6bd856cc9996 ("block: allow error injection rules to delay bios")
Signed-off-by: Md Haris Iqbal <haris.iqbal@xxxxxxxxx>
Ugh. Apologies for the mixup.
Please drop the patches marked */2 (this one, and another one), as they were an older batch and they got sent because I mistakenly used "send-email 000*"
---
block/error-injection.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/block/error-injection.c b/block/error-injection.c
index a6de52229ef3..51fba46990b2 100644
--- a/block/error-injection.c
+++ b/block/error-injection.c
@@ -33,10 +33,11 @@ struct blk_error_inject {
/*
* 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.
+ * can be removed while delayed bios are outstanding. Not every submitter holds
+ * the bdev open, so a delayed bio holds a reference to its bdev and to the
+ * driver module, and is failed instead of resubmitted if the disk is dead by
+ * then. 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;
@@ -62,21 +63,26 @@ 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;
+ struct block_device *bdev = bio->bi_bdev;
+ struct module *owner = bdev->bd_disk->fops->owner;
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);
+ if (test_bit(GD_DEAD, &bdev->bd_disk->state))
+ bio_io_error(bio);
+ else if (status != BLK_STS_OK)
+ blk_error_inject_fail(bio, status);
+ else
+ submit_bio_noacct_nocheck(bio, false);
+
+ put_device(&bdev->bd_device);
+ module_put(owner);
}
static bool blk_error_inject_delay(struct gendisk *disk, struct bio *bio,
@@ -98,6 +104,12 @@ static bool blk_error_inject_delay(struct gendisk *disk, struct bio *bio,
return false;
}
+ if (!try_module_get(disk->fops->owner)) {
+ kfree(d);
+ return false;
+ }
+ get_device(&bio->bi_bdev->bd_device);
+
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);