public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Jens Axboe <axboe@kernel.dk>
Cc: Carlos Maiolino <cem@kernel.org>,
	linux-block@vger.kernel.org, linux-xfs@vger.kernel.org
Subject: [PATCH 2/4] block: factor out a bio_await helper
Date: Wed,  1 Apr 2026 16:03:57 +0200	[thread overview]
Message-ID: <20260401140433.125924-3-hch@lst.de> (raw)
In-Reply-To: <20260401140433.125924-1-hch@lst.de>

Add a new helper to wait for a bio and anything chained off it to
complete synchronous after kicking it.  This factors common code out
of submit_bio_wait and bio_await_chain and will also be useful for
file system code and thus is exported.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/bio.c         | 52 +++++++++++++++++++++++++++++++--------------
 include/linux/bio.h |  2 ++
 2 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/block/bio.c b/block/bio.c
index f5a058531cd9..c9410804c61a 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -1453,6 +1453,35 @@ static void bio_wait_end_io(struct bio *bio)
 	complete(bio->bi_private);
 }
 
+/**
+ * bio_await - call a function on a bio, and wait until it completes
+ * @bio:	the bio which describes the I/O
+ * @kick:	function called to "kick off" the bio
+ * @priv:	private data passed to @kick.
+ *
+ * Wait for the bio as well as any bio chained off it after executing the
+ * passed in callback @kick.  The wait for the bio is set up before calling
+ * @kick to ensure that the completion is captured.  If @kick is %NULL,
+ * submit_bio() is used instead to submit the bio.
+ *
+ * Note: this overrides the bi_private and bi_end_io fields in the bio.
+ */
+void bio_await(struct bio *bio, void *priv,
+	       void (*kick)(struct bio *bio, void *priv))
+{
+	DECLARE_COMPLETION_ONSTACK_MAP(done,
+			bio->bi_bdev->bd_disk->lockdep_map);
+
+	bio->bi_private = &done;
+	bio->bi_end_io = bio_wait_end_io;
+	if (kick)
+		kick(bio, priv);
+	else
+		submit_bio(bio);
+	blk_wait_io(&done);
+}
+EXPORT_SYMBOL_GPL(bio_await);
+
 /**
  * submit_bio_wait - submit a bio, and wait until it completes
  * @bio: The &struct bio which describes the I/O
@@ -1466,19 +1495,16 @@ static void bio_wait_end_io(struct bio *bio)
  */
 int submit_bio_wait(struct bio *bio)
 {
-	DECLARE_COMPLETION_ONSTACK_MAP(done,
-			bio->bi_bdev->bd_disk->lockdep_map);
-
-	bio->bi_private = &done;
-	bio->bi_end_io = bio_wait_end_io;
-	bio->bi_opf |= REQ_SYNC;
-	submit_bio(bio);
-	blk_wait_io(&done);
-
+	bio_await(bio, NULL, NULL);
 	return blk_status_to_errno(bio->bi_status);
 }
 EXPORT_SYMBOL(submit_bio_wait);
 
+static void bio_endio_cb(struct bio *bio, void *priv)
+{
+	bio_endio(bio);
+}
+
 /**
  * bdev_rw_virt - synchronously read into / write from kernel mapping
  * @bdev:	block device to access
@@ -1514,13 +1540,7 @@ EXPORT_SYMBOL_GPL(bdev_rw_virt);
  */
 void bio_await_chain(struct bio *bio)
 {
-	DECLARE_COMPLETION_ONSTACK_MAP(done,
-			bio->bi_bdev->bd_disk->lockdep_map);
-
-	bio->bi_private = &done;
-	bio->bi_end_io = bio_wait_end_io;
-	bio_endio(bio);
-	blk_wait_io(&done);
+	bio_await(bio, NULL, bio_endio_cb);
 	bio_put(bio);
 }
 
diff --git a/include/linux/bio.h b/include/linux/bio.h
index 984844d2870b..adcca9ad4eec 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -432,6 +432,8 @@ extern void bio_uninit(struct bio *);
 void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf);
 void bio_reuse(struct bio *bio, blk_opf_t opf);
 void bio_chain(struct bio *, struct bio *);
+void bio_await(struct bio *bio, void *priv,
+	       void (*kick)(struct bio *bio, void *priv));
 
 int __must_check bio_add_page(struct bio *bio, struct page *page, unsigned len,
 			      unsigned off);
-- 
2.47.3


  parent reply	other threads:[~2026-04-01 14:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01 14:03 refactor submit_bio_wait and bio await helpers Christoph Hellwig
2026-04-01 14:03 ` [PATCH 1/4] block: unify the synchronous bi_end_io callbacks Christoph Hellwig
2026-04-01 15:30   ` Bart Van Assche
2026-04-01 14:03 ` Christoph Hellwig [this message]
2026-04-01 15:35   ` [PATCH 2/4] block: factor out a bio_await helper Bart Van Assche
2026-04-06  5:50     ` Christoph Hellwig
2026-04-01 14:03 ` [PATCH 3/4] block: add a bio_submit_or_kill helper Christoph Hellwig
2026-04-01 15:42   ` Bart Van Assche
2026-04-06  5:51     ` Christoph Hellwig
2026-04-01 14:03 ` [PATCH 4/4] xfs: use bio_await in xfs_zone_gc_reset_sync Christoph Hellwig
  -- strict thread matches above, loose matches on Subject: below --
2026-04-06  5:57 refactor submit_bio_wait and bio await helpers v2 Christoph Hellwig
2026-04-06  5:57 ` [PATCH 2/4] block: factor out a bio_await helper Christoph Hellwig
2026-04-06  7:05   ` Damien Le Moal

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=20260401140433.125924-3-hch@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernel.dk \
    --cc=cem@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-xfs@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