Linux filesystem development
 help / color / mirror / Atom feed
* [PATCH v2] iomap: only poll direct I/O that is a single bio
@ 2026-12-02 12:19 Fengnan Chang
  2026-12-02 16:51 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Fengnan Chang @ 2026-12-02 12:19 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Christoph Hellwig, Darrick J . Wong, Ming Lei, Jens Axboe,
	John Garry, linux-xfs, linux-fsdevel, linux-ext4, Fengnan Chang

An io_uring IOPOLL ring sets IOCB_HIPRI, and iomap marks a bio
REQ_POLLED and stores it in iocb->private. A polled bio raises no
interrupt, and the caller can only poll it after __iomap_dio_rw()
returns. If __iomap_dio_rw() instead waits for the dio itself, nothing
polls the bio and the I/O stalls until the request times out. NVMe then
reaps the completion from its timeout handler, after 30 seconds by
default.

__iomap_dio_rw() already does not poll sync kiocbs, and a mapping that
needs completion work clears IOCB_HIPRI. But it can still wait for a
dio that has a polled bio:

- IOMAP_DIO_FORCE_WAIT: for example, an XFS unaligned write ending at
  or past i_size (such as 512 bytes at offset 7680 of an 8 KiB file,
  which needs neither completion work nor tail zeroing), or an ext4
  unaligned write over a written block followed by an unwritten block.
  The first bio is submitted as polled before the dio waits for it.

- A short I/O that sets wait_for_completion after submission: -EFAULT
  with IOMAP_DIO_PARTIAL (gfs2) and -ENOTBLK (gfs2 and f2fs direct
  writes to a hole). The bio for the first mapping may already be
  polled.

In addition, a non-extending sub-block write ending at an unaligned
i_size does not set IOMAP_DIO_COMP_WORK, so iomap_dio_bio_iter()
submits a second tail-zeroing bio via iomap_dio_zero() after the data
bio. Clearing IOCB_HIPRI before the loop in that case keeps the rule
simple and strict: a polled dio never submits more than one bio.

Move the single-bio polling check into iomap_dio_bio_iter_one(), keeping
iomap_dio_zero() out of the polling path completely: only poll a bio
that starts at iocb->ki_pos and carries all of iter->len, and only if
!dio->wait_for_completion. Once that bio is submitted the iteration is
done, so nothing can make __iomap_dio_rw() wait for it. Clear
IOCB_HIPRI in iomap_dio_bio_iter() when a sub-block write at or beyond
i_size will emit a tail-zeroing bio, initialize dio->wait_for_completion
up front, and drop the now-redundant IOCB_HIPRI clears.

Normal single-bio I/Os remain polled. Multi-mapping I/Os (such as
fragmented files or reads whose initial mapped extent is followed by a
hole or unmapped space past EOF) are no longer partially polled.

Reproduced with io_uring IOPOLL on null_blk with poll queues and on
NVMe:
- XFS: a 512-byte write at offset 7680 of an 8 KiB file
  (IOMAP_DIO_FORCE_WAIT, single bio).
- gfs2: an 8 KiB read of a file whose second block is a hole, into a
  buffer whose second page is not faulted in (-EFAULT with
  IOMAP_DIO_PARTIAL), and an 8 KiB write over a mapped block followed
  by a hole (-ENOTBLK).
- ext4: a 4 KiB write at offset 2 KiB over a written block followed by
  an unwritten one (IOMAP_DIO_FORCE_WAIT), and a 512-byte overwrite at
  offset 512 ending at an unaligned 1024-byte i_size (verified via
  submit_bio kprobe that neither data nor tail-zeroing bio sets
  REQ_POLLED).
Each case completes in sub-millisecond latency without timeouts and
reads back correctly. 4 KiB random read and write with io_uring IOPOLL
on ext4 stay polled with unchanged IOPS, and xfstests (-g aio -g rw
-g io_uring) shows no regressions on ext4, XFS, or gfs2.

Fixes: 9650b453a3d4 ("block: ignore RWF_HIPRI hint for sync dio")
Cc: stable@vger.kernel.org
Signed-off-by: Fengnan Chang <changfengnan@bytedance.com>
---
v2:
- Only poll a bio that starts at iocb->ki_pos and carries all of
  iter->len (rather than iomap_length(iter)), instead of clearing
  IOCB_HIPRI up front for IOMAP_DIO_FORCE_WAIT. This also covers the
  -EFAULT (IOMAP_DIO_PARTIAL) and -ENOTBLK cases Christian pointed out,
  where wait_for_completion is set after the iomap_iter() loop.
- Move the polling check into iomap_dio_bio_iter_one() as Christoph
  suggested so iomap_dio_zero() stays out of the polling path, while
  leaving the REQ_ATOMIC check untouched as John noted so it does not
  conflict with its pending removal.
- Clear IOCB_HIPRI in iomap_dio_bio_iter() when a sub-block write at
  or beyond i_size will submit a tail-zeroing bio (prompted by
  sashiko-bot), so a polled dio strictly submits only one bio.
- Drop the two now-redundant IOCB_HIPRI clears and initialize
  dio->wait_for_completion up front.

v1: https://lore.kernel.org/all/20261201034922.203226-1-changfengnan@bytedance.com/

 fs/iomap/direct-io.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c
index f00b0841fdd08..7ea29eb1f070d 100644
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -64,16 +64,8 @@ static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter,
 static void iomap_dio_submit_bio(const struct iomap_iter *iter,
 		struct iomap_dio *dio, struct bio *bio, loff_t pos)
 {
-	struct kiocb *iocb = dio->iocb;
-
 	atomic_inc(&dio->ref);
 
-	/* Sync dio can't be polled reliably */
-	if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) {
-		bio->bi_opf |= REQ_POLLED;
-		WRITE_ONCE(iocb->private, bio);
-	}
-
 	if (dio->dops && dio->dops->submit_io) {
 		dio->dops->submit_io(iter, bio, pos);
 		return;
@@ -342,6 +334,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
 		struct iomap_dio *dio, loff_t pos, unsigned int alignment,
 		blk_opf_t op)
 {
+	struct kiocb *iocb = dio->iocb;
 	unsigned int maxsize = iomap_max_bio_size(&iter->iomap);
 	unsigned int nr_vecs;
 	struct bio *bio;
@@ -356,7 +349,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
 	fscrypt_set_bio_crypt_ctx(bio, iter->inode, pos, GFP_KERNEL);
 	bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
 	bio->bi_write_hint = iter->inode->i_write_hint;
-	bio->bi_ioprio = dio->iocb->ki_ioprio;
+	bio->bi_ioprio = iocb->ki_ioprio;
 	bio->bi_private = dio;
 	bio->bi_end_io = iomap_dio_bio_end_io;
 
@@ -386,10 +379,16 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
 		bio_set_pages_dirty(bio);
 
 	/*
-	 * We can only poll for single bio I/Os.
+	 * Only async bios can be polled, and polling is only supported
+	 * for a single bio covering the entire request.  Once that bio
+	 * is submitted the iteration is done, so a short I/O can't make
+	 * __iomap_dio_rw() wait for it later.
 	 */
-	if (iov_iter_count(dio->submit.iter))
-		dio->iocb->ki_flags &= ~IOCB_HIPRI;
+	if ((iocb->ki_flags & IOCB_HIPRI) && !dio->wait_for_completion &&
+	    pos == iocb->ki_pos && bio->bi_iter.bi_size == iter->len) {
+		bio->bi_opf |= REQ_POLLED;
+		WRITE_ONCE(iocb->private, bio);
+	}
 	iomap_dio_submit_bio(iter, dio, bio, pos);
 	return ret;
 
@@ -531,9 +530,14 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
 	/*
 	 * The rules for polled IO completions follow the guidelines as the
 	 * ones we set for inline and deferred completions. If none of those
-	 * are available for this IO, clear the polled flag.
+	 * are available for this IO, or if a sub-block write at or beyond EOF
+	 * will submit a tail-zeroing bio (matching the check below), clear the
+	 * polled flag.
 	 */
-	if (dio->flags & IOMAP_DIO_COMP_WORK)
+	if ((dio->flags & IOMAP_DIO_COMP_WORK) ||
+	    ((dio->flags & IOMAP_DIO_WRITE) &&
+	     ((pos + length) & (fs_block_size - 1)) &&
+	     pos + length >= i_size_read(inode)))
 		dio->iocb->ki_flags &= ~IOCB_HIPRI;
 
 	if (need_zeroout) {
@@ -729,6 +733,7 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	dio->error = 0;
 	dio->flags = dio_flags & (IOMAP_DIO_FSBLOCK_ALIGNED | IOMAP_DIO_BOUNCE);
 	dio->done_before = done_before;
+	dio->wait_for_completion = wait_for_completion;
 
 	dio->submit.iter = iter;
 	dio->submit.waiter = current;
@@ -819,15 +824,9 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	inode_dio_begin(inode);
 
 	blk_start_plug(&plug);
-	while ((ret = iomap_iter(&iomi, next)) > 0) {
+	while ((ret = iomap_iter(&iomi, next)) > 0)
 		iomi.status = iomap_dio_iter(&iomi, dio);
 
-		/*
-		 * We can only poll for single bio I/Os.
-		 */
-		iocb->ki_flags &= ~IOCB_HIPRI;
-	}
-
 	blk_finish_plug(&plug);
 
 	/*
-- 
2.39.5

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02 16:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:19 [PATCH v2] iomap: only poll direct I/O that is a single bio Fengnan Chang
2026-12-02 16:51 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox