Linux Documentation
 help / color / mirror / Atom feed
From: Md Haris Iqbal <haris.iqbal@linux.dev>
To: Jens Axboe <axboe@kernel.dk>, linux-block@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Christoph Hellwig <hch@lst.de>,
	Jonathan Corbet <corbet@lwn.net>,
	linux-doc@vger.kernel.org, Md Haris Iqbal <haris.iqbal@linux.dev>
Subject: [PATCH 1/2] block: hold the disk while error injection delays a bio
Date: Sat,  5 Dec 2026 13:55:07 +0100	[thread overview]
Message-ID: <20261205125510.29717-3-haris.iqbal@linux.dev> (raw)
In-Reply-To: <20261205125510.29717-1-haris.iqbal@linux.dev>

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@linux.dev>
---
 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);
-- 
2.53.0


  parent reply	other threads:[~2026-12-05 12:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 12:55 [PATCH for-rc 0/3] block: error injection delay fixes Md Haris Iqbal
2026-12-05 12:55 ` [PATCH for-rc 1/3] block: fail a delayed bio if its disk was removed Md Haris Iqbal
2026-12-05 12:55 ` Md Haris Iqbal [this message]
2026-12-05 12:59   ` [PATCH 1/2] block: hold the disk while error injection delays a bio Haris Iqbal
2026-12-05 14:06     ` Jens Axboe
2026-12-05 12:55 ` [PATCH for-rc 2/3] block: clear BIO_ERROR_INJECTED when a bio changes device Md Haris Iqbal
2026-12-05 12:55 ` [PATCH 2/2] block: copy BIO_ERROR_INJECTED to the split in bio_split() Md Haris Iqbal
2026-12-05 12:59   ` Haris Iqbal
2026-12-05 12:55 ` [PATCH for-rc 3/3] " Md Haris Iqbal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205125510.29717-3-haris.iqbal@linux.dev \
    --to=haris.iqbal@linux.dev \
    --cc=axboe@kernel.dk \
    --cc=corbet@lwn.net \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox