* [PATCH] iomap: disable I/O polling when direct I/O must wait
@ 2026-12-01 3:49 Fengnan Chang
2026-12-01 4:06 ` sashiko-bot
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Fengnan Chang @ 2026-12-01 3:49 UTC (permalink / raw)
To: Christian Brauner
Cc: Ming Lei, Christoph Hellwig, Darrick J . Wong, linux-xfs,
linux-fsdevel, linux-ext4, Fengnan Chang
With IOMAP_DIO_FORCE_WAIT, __iomap_dio_rw() waits for the bios of an
asynchronous kiocb in the submission path. That wait does not poll, and
the caller, such as io_uring, cannot poll before __iomap_dio_rw()
returns.
Such writes usually do not poll, because a mapping that needs
completion work clears IOCB_HIPRI. But IOCB_HIPRI is only cleared once
that mapping is reached, and a bio for an earlier mapping may already
have been submitted as polled.
For example, an ext4 unaligned write that covers a written block
followed by an unwritten block needs sub-block zeroing, so ext4 passes
IOMAP_DIO_FORCE_WAIT. The bio for the written block is submitted as
polled, and the task then waits for it without polling. On a poll
queue the write stalls until the request times out; NVMe then reaps
the completion from its timeout handler, after 30 seconds by default.
Like synchronous kiocbs, do not poll the bios if the caller will wait
for them: clear IOCB_HIPRI before submitting any bio.
Reproduced with an io_uring IOPOLL write of 4 KiB at offset 2 KiB to
such an ext4 file on a null_blk device with poll queues: the write
failed with -ETIMEDOUT after the request timeout. With this change it
completes and reads back correctly.
Fixes: 9650b453a3d4 ("block: ignore RWF_HIPRI hint for sync dio")
Signed-off-by: Fengnan Chang <changfengnan@bytedance.com>
---
fs/iomap/direct-io.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c
index f00b0841fdd08..1326cb0af0d32 100644
--- a/fs/iomap/direct-io.c
+++ b/fs/iomap/direct-io.c
@@ -733,6 +733,9 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
dio->submit.iter = iter;
dio->submit.waiter = current;
+ if (wait_for_completion)
+ iocb->ki_flags &= ~IOCB_HIPRI;
+
if (iocb->ki_flags & IOCB_NOWAIT)
iomi.flags |= IOMAP_NOWAIT;
--
2.39.5
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-01 3:49 [PATCH] iomap: disable I/O polling when direct I/O must wait Fengnan Chang @ 2026-12-01 4:06 ` sashiko-bot 2026-12-01 8:08 ` Christoph Hellwig ` (2 subsequent siblings) 3 siblings, 0 replies; 11+ messages in thread From: sashiko-bot @ 2026-12-01 4:06 UTC (permalink / raw) To: Fengnan Chang; +Cc: linux-ext4, tytso > With IOMAP_DIO_FORCE_WAIT, __iomap_dio_rw() waits for the bios of an > asynchronous kiocb in the submission path. That wait does not poll, and > the caller, such as io_uring, cannot poll before __iomap_dio_rw() > returns. > > Such writes usually do not poll, because a mapping that needs > completion work clears IOCB_HIPRI. But IOCB_HIPRI is only cleared once > that mapping is reached, and a bio for an earlier mapping may already > have been submitted as polled. > > For example, an ext4 unaligned write that covers a written block > followed by an unwritten block needs sub-block zeroing, so ext4 passes > IOMAP_DIO_FORCE_WAIT. The bio for the written block is submitted as > polled, and the task then waits for it without polling. On a poll > queue the write stalls until the request times out; NVMe then reaps > the completion from its timeout handler, after 30 seconds by default. > > Like synchronous kiocbs, do not poll the bios if the caller will wait > for them: clear IOCB_HIPRI before submitting any bio. > > Reproduced with an io_uring IOPOLL write of 4 KiB at offset 2 KiB to > such an ext4 file on a null_blk device with poll queues: the write > failed with -ETIMEDOUT after the request timeout. With this change it > completes and reads back correctly. > > Fixes: 9650b453a3d4 ("block: ignore RWF_HIPRI hint for sync dio") > Signed-off-by: Fengnan Chang <changfengnan@bytedance.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261201034922.203226-1-changfengnan@bytedance.com?part=1 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-01 3:49 [PATCH] iomap: disable I/O polling when direct I/O must wait Fengnan Chang 2026-12-01 4:06 ` sashiko-bot @ 2026-12-01 8:08 ` Christoph Hellwig 2026-12-01 8:48 ` Fengnan Chang 2026-12-01 13:25 ` Christian Brauner 2026-12-01 13:26 ` Christian Brauner 3 siblings, 1 reply; 11+ messages in thread From: Christoph Hellwig @ 2026-12-01 8:08 UTC (permalink / raw) To: Fengnan Chang Cc: Christian Brauner, Ming Lei, Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4 On Tue, Dec 01, 2026 at 03:49:22AM +0000, Fengnan Chang wrote: > With IOMAP_DIO_FORCE_WAIT, __iomap_dio_rw() waits for the bios of an > asynchronous kiocb in the submission path. That wait does not poll, and > the caller, such as io_uring, cannot poll before __iomap_dio_rw() > returns. > > Such writes usually do not poll, because a mapping that needs > completion work clears IOCB_HIPRI. But IOCB_HIPRI is only cleared once > that mapping is reached, and a bio for an earlier mapping may already > have been submitted as polled. > > For example, an ext4 unaligned write that covers a written block > followed by an unwritten block needs sub-block zeroing, so ext4 passes > IOMAP_DIO_FORCE_WAIT. The bio for the written block is submitted as > polled, and the task then waits for it without polling. On a poll > queue the write stalls until the request times out; NVMe then reaps > the completion from its timeout handler, after 30 seconds by default. > > Like synchronous kiocbs, do not poll the bios if the caller will wait > for them: clear IOCB_HIPRI before submitting any bio. > > Reproduced with an io_uring IOPOLL write of 4 KiB at offset 2 KiB to > such an ext4 file on a null_blk device with poll queues: the write > failed with -ETIMEDOUT after the request timeout. With this change it > completes and reads back correctly. Looks good: Reviewed-by: Christoph Hellwig <hch@lst.de> I've seen you've also posted various other polling fixes. Any chance you could try to wire up your tests in blktests or xfstests so that we get test coverage for these cases? ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-01 8:08 ` Christoph Hellwig @ 2026-12-01 8:48 ` Fengnan Chang 0 siblings, 0 replies; 11+ messages in thread From: Fengnan Chang @ 2026-12-01 8:48 UTC (permalink / raw) To: Christoph Hellwig, Fengnan Chang Cc: Christian Brauner, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4, tom.leiming 在 01/12/26 16:08, Christoph Hellwig 写道: > On Tue, Dec 01, 2026 at 03:49:22AM +0000, Fengnan Chang wrote: >> With IOMAP_DIO_FORCE_WAIT, __iomap_dio_rw() waits for the bios of an >> asynchronous kiocb in the submission path. That wait does not poll, and >> the caller, such as io_uring, cannot poll before __iomap_dio_rw() >> returns. >> >> Such writes usually do not poll, because a mapping that needs >> completion work clears IOCB_HIPRI. But IOCB_HIPRI is only cleared once >> that mapping is reached, and a bio for an earlier mapping may already >> have been submitted as polled. >> >> For example, an ext4 unaligned write that covers a written block >> followed by an unwritten block needs sub-block zeroing, so ext4 passes >> IOMAP_DIO_FORCE_WAIT. The bio for the written block is submitted as >> polled, and the task then waits for it without polling. On a poll >> queue the write stalls until the request times out; NVMe then reaps >> the completion from its timeout handler, after 30 seconds by default. >> >> Like synchronous kiocbs, do not poll the bios if the caller will wait >> for them: clear IOCB_HIPRI before submitting any bio. >> >> Reproduced with an io_uring IOPOLL write of 4 KiB at offset 2 KiB to >> such an ext4 file on a null_blk device with poll queues: the write >> failed with -ETIMEDOUT after the request timeout. With this change it >> completes and reads back correctly. > > Looks good: > > Reviewed-by: Christoph Hellwig <hch@lst.de> > > I've seen you've also posted various other polling fixes. Any chance > you could try to wire up your tests in blktests or xfstests so that we > get test coverage for these cases? Sure, I'll do it later. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-01 3:49 [PATCH] iomap: disable I/O polling when direct I/O must wait Fengnan Chang 2026-12-01 4:06 ` sashiko-bot 2026-12-01 8:08 ` Christoph Hellwig @ 2026-12-01 13:25 ` Christian Brauner 2026-12-01 13:26 ` Christian Brauner 3 siblings, 0 replies; 11+ messages in thread From: Christian Brauner @ 2026-12-01 13:25 UTC (permalink / raw) To: Fengnan Chang Cc: Ming Lei, Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4 On Tue, 01 Dec 2026 03:49:22 +0000, Fengnan Chang wrote: > With IOMAP_DIO_FORCE_WAIT, __iomap_dio_rw() waits for the bios of an > asynchronous kiocb in the submission path. That wait does not poll, and > the caller, such as io_uring, cannot poll before __iomap_dio_rw() > returns. > > Such writes usually do not poll, because a mapping that needs > completion work clears IOCB_HIPRI. But IOCB_HIPRI is only cleared once > that mapping is reached, and a bio for an earlier mapping may already > have been submitted as polled. > > [...] Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon. Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it. It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated. Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch. tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes [1/1] iomap: disable I/O polling when direct I/O must wait https://git.kernel.org/vfs/vfs/c/a302736c8c0a ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-01 3:49 [PATCH] iomap: disable I/O polling when direct I/O must wait Fengnan Chang ` (2 preceding siblings ...) 2026-12-01 13:25 ` Christian Brauner @ 2026-12-01 13:26 ` Christian Brauner 2026-12-02 1:56 ` Fengnan 3 siblings, 1 reply; 11+ messages in thread From: Christian Brauner @ 2026-12-01 13:26 UTC (permalink / raw) To: Fengnan Chang Cc: Ming Lei, Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4 On Tue, Dec 01, 2026 at 03:49:22AM +0000, Fengnan Chang wrote: > + if (wait_for_completion) > + iocb->ki_flags &= ~IOCB_HIPRI; One question. wait_for_completion can also flip to true after we've already submitted, no? * partial -EFAULT with IOMAP_DIO_PARTIAL * -ENOTBLK fallback In both cases it's set after the iomap_iter() loop. By then the first bio might have gone out with REQ_POLLED. Can't that end up in the same stall? I might misunderstand. -- ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-01 13:26 ` Christian Brauner @ 2026-12-02 1:56 ` Fengnan 2026-12-02 7:53 ` Fengnan 0 siblings, 1 reply; 11+ messages in thread From: Fengnan @ 2026-12-02 1:56 UTC (permalink / raw) To: Christian Brauner Cc: Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4, tom.leiming 在 01/12/26 21:26, Christian Brauner 写道: > On Tue, Dec 01, 2026 at 03:49:22AM +0000, Fengnan Chang wrote: >> + if (wait_for_completion) >> + iocb->ki_flags &= ~IOCB_HIPRI; > > One question. wait_for_completion can also flip to true after we've > already submitted, no? > > * partial -EFAULT with IOMAP_DIO_PARTIAL > * -ENOTBLK fallback > > In both cases it's set after the iomap_iter() loop. > > By then the first bio might have gone out with REQ_POLLED. Can't that > end up in the same stall? I might misunderstand. > Yes, you're right, thanks. Both the IOMAP_DIO_PARTIAL -EFAULT case and the -ENOTBLK fallback set wait_for_completion after the iomap_iter() loop, when the first bio may already have been submitted with REQ_POLLED, so they can hit the same stall. Clearing IOCB_HIPRI up front can't cover those. For v2 I plan to poll from the wait loop instead, i.e. call iocb_bio_iopoll() while iocb->private is set and fall back to blk_io_schedule() otherwise, similar to what the loop did before sync polling was removed. That covers the FORCE_WAIT case and both late paths. I'll send v2 after testing it. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-02 1:56 ` Fengnan @ 2026-12-02 7:53 ` Fengnan 2026-12-02 8:19 ` sashiko-bot 2026-12-02 8:20 ` Christoph Hellwig 0 siblings, 2 replies; 11+ messages in thread From: Fengnan @ 2026-12-02 7:53 UTC (permalink / raw) To: Christian Brauner Cc: Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4, tom.leiming 在 02/12/26 09:56, Fengnan 写道: > > > 在 01/12/26 21:26, Christian Brauner 写道: >> On Tue, Dec 01, 2026 at 03:49:22AM +0000, Fengnan Chang wrote: >>> + if (wait_for_completion) >>> + iocb->ki_flags &= ~IOCB_HIPRI; >> >> One question. wait_for_completion can also flip to true after we've >> already submitted, no? >> >> * partial -EFAULT with IOMAP_DIO_PARTIAL >> * -ENOTBLK fallback >> >> In both cases it's set after the iomap_iter() loop. >> >> By then the first bio might have gone out with REQ_POLLED. Can't that >> end up in the same stall? I might misunderstand. >> > Yes, you're right, thanks. Both the IOMAP_DIO_PARTIAL -EFAULT case and > the -ENOTBLK fallback set wait_for_completion after the iomap_iter() > loop, when the first bio may already have been submitted with > REQ_POLLED, so they can hit the same stall. > > Clearing IOCB_HIPRI up front can't cover those. For v2 I plan to poll > from the wait loop instead, i.e. call iocb_bio_iopoll() while > iocb->private is set and fall back to blk_io_schedule() otherwise, > similar to what the loop did before sync polling was removed. That > covers the FORCE_WAIT case and both late paths. I'll send v2 after > testing it. Hi Christian, A correction to my plan: polling from the wait loop doesn't work. blk_hctx_poll() gives up after two jiffies and no longer keeps the task running, so the waiter can sleep with nothing to wake it. I still saw 30 second stalls with it. Instead I'd only poll a bio that carries the whole request: diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c --- a/fs/iomap/direct-io.c +++ b/fs/iomap/direct-io.c @@ -68,8 +68,14 @@ static void iomap_dio_submit_bio(const struct iomap_iter *iter, atomic_inc(&dio->ref); - /* Sync dio can't be polled reliably */ - if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) { + /* + * Only poll a bio that starts at the beginning of the request and + * carries all of it, and only if the dio does not wait for completion. + * 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 ((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); } @@ -729,6 +735,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; Today, a bio is polled if IOCB_HIPRI is still set when it is submitted. For a multi-mapping I/O where the first mapping fits in one bio and does not need completion work, IOCB_HIPRI is only cleared after that mapping. So its bio can be submitted as polled even though a later mapping may fail with -EFAULT or -ENOTBLK, or the dio was going to wait anyway (IOMAP_DIO_FORCE_WAIT). With this change, a bio is polled only if: - the dio does not already wait for completion (sync kiocb or IOMAP_DIO_FORCE_WAIT), and - it starts at iocb->ki_pos and its size equals iter->len, i.e. it carries the whole request. Such a bio exhausts the iterator, so no other mapping follows and wait_for_completion cannot flip to true after submission. The existing IOCB_HIPRI clears stay, so mappings that need completion work are still not polled. Normal single-bio I/Os are polled as before. Besides IOMAP_DIO_FORCE_WAIT, these are no longer polled: - I/O that spans multiple mappings -- including fragmented files and reads whose initial mapped extent is followed by a hole or unmapped space past EOF (today the first mapping's bio may be polled while the rest complete by interrupt); - the tail-zeroing bio after a sub-block write ending at an unaligned EOF (today it is also marked REQ_POLLED and overwrites iocb->private, though note the data bio itself still matches pos == iocb->ki_pos && bi_size == iter->len if the filesystem did not set IOMAP_DIO_FORCE_WAIT). Does this look OK? If so I'll send it as v2. Thanks, Fengnan ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-02 7:53 ` Fengnan @ 2026-12-02 8:19 ` sashiko-bot 2026-12-02 8:20 ` Christoph Hellwig 1 sibling, 0 replies; 11+ messages in thread From: sashiko-bot @ 2026-12-02 8:19 UTC (permalink / raw) To: Fengnan; +Cc: linux-ext4, tytso Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] iomap: missing Signed-off-by trailer in commit message - [High] iomap: polled bio use-after-free when iomap_dio_bio_iter() zeroes EOF tail - [Low] iomap: raw mailing-list reply thread used as commit changelog - [Low] iomap: missing Fixes trailer for commit 9650b453a3d4 in changelog -- commit 4454ee3e347dd167e855abb07789ca87cc412b12 Author: Fengnan <changfengnan@bytedance.com> > iomap: disable I/O polling when direct I/O must wait > > 在 02/12/26 09:56, Fengnan 写道: > > > > > > 在 01/12/26 21:26, Christian Brauner 写道: > >> On Tue, Dec 01, 2026 at 03:49:22AM +0000, Fengnan Chang wrote: > >>> + if (wait_for_completion) > >>> + iocb->ki_flags &= ~IOCB_HIPRI; > >> > >> One question. wait_for_completion can also flip to true after we've > >> already submitted, no? > >> > >> * partial -EFAULT with IOMAP_DIO_PARTIAL > >> * -ENOTBLK fallback > >> > >> In both cases it's set after the iomap_iter() loop. > >> > >> By then the first bio might have gone out with REQ_POLLED. Can't that > >> end up in the same stall? I might misunderstand. > >> > > Yes, you're right, thanks. Both the IOMAP_DIO_PARTIAL -EFAULT case and > > the -ENOTBLK fallback set wait_for_completion after the iomap_iter() > > loop, when the first bio may already have been submitted with > > REQ_POLLED, so they can hit the same stall. > > > > Clearing IOCB_HIPRI up front can't cover those. For v2 I plan to poll > > from the wait loop instead, i.e. call iocb_bio_iopoll() while > > iocb->private is set and fall back to blk_io_schedule() otherwise, > > similar to what the loop did before sync polling was removed. That > > covers the FORCE_WAIT case and both late paths. I'll send v2 after > > testing it. > > Hi Christian, > > A correction to my plan: polling from the wait loop doesn't work. > blk_hctx_poll() gives up after two jiffies and no longer keeps the task > running, so the waiter can sleep with nothing to wake it. I still saw > 30 second stalls with it. > > Instead I'd only poll a bio that carries the whole request: [Severity: High] The commit message is missing a Signed-off-by: trailer. [Severity: Low] The commit message body is an unedited mailing-list reply thread rather than a standalone changelog explaining why __iomap_dio_rw() stalls when waiting on a REQ_POLLED bio and how restricting polling in iomap_dio_submit_bio() avoids it. [Severity: Low] This fixes the incomplete !is_sync_kiocb(iocb) check introduced when synchronous DIO polling was removed. Should it include: Fixes: 9650b453a3d4 ("block: ignore RWF_HIPRI hint for sync dio") > diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c > index f00b0841fdd08..6bc82a351cdbc 100644 > --- a/fs/iomap/direct-io.c > +++ b/fs/iomap/direct-io.c > @@ -68,8 +68,14 @@ static void iomap_dio_submit_bio(const struct iomap_iter *iter, > > atomic_inc(&dio->ref); > > - /* Sync dio can't be polled reliably */ > - if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) { > + /* > + * Only poll a bio that starts at the beginning of the request and > + * carries all of it, and only if the dio does not wait for completion. > + * 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 ((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); > } [Severity: High] Can iocb_bio_iopoll() hit a use-after-free on the bio stored in iocb->private when iomap_dio_bio_iter() submits a second bio to zero the EOF tail? __iomap_dio_rw() only sets IOMAP_DIO_COMP_WORK when iomi.pos + iomi.len > dio->i_size, whereas iomap_dio_bio_iter() zeroes the tail block whenever pos >= i_size_read(inode) and pos is not aligned to fs_block_size: fs/iomap/direct-io.c:iomap_dio_bio_iter() { ... if (need_zeroout || ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) { /* zero out from the end of the write to the end of the block */ pad = pos & (fs_block_size - 1); if (pad) iomap_dio_zero(iter, dio, pos, fs_block_size - pad); } ... } For a non-extending async direct write to an already-mapped extent ending exactly at an i_size that is aligned to the device logical block size but not to fs_block_size (for example on ext4 or gfs2), wait_for_completion is false and IOMAP_DIO_COMP_WORK is not set, so IOCB_HIPRI remains set. The data bio satisfies pos == iocb->ki_pos && bio->bi_iter.bi_size == iter->len, so iomap_dio_submit_bio() marks it REQ_POLLED and stores it in iocb->private, and iomap_dio_zero() then submits a second non-polled bio that increments dio->ref. CPU0 (io_uring iopoll) CPU1 (zero bio IRQ) ---------------------- ------------------- io_do_iopoll() iocb_bio_iopoll() bio_poll(iocb->private) __iomap_dio_bio_end_io() bio_put(bio) atomic_dec_and_test(&dio->ref) -> false (zero bio in flight) io_do_iopoll() iocb_bio_iopoll() bio_poll(iocb->private) <-- use-after-free __iomap_dio_bio_end_io() atomic_dec_and_test(&dio->ref) iomap_dio_done() When the polled data bio completes, __iomap_dio_bio_end_io() frees it via bio_put(), but atomic_dec_and_test(&dio->ref) is false while the tail-zeroing bio is still in flight. iomap_dio_done() does not run to complete the kiocb, leaving iocb->private pointing to the freed bio while subsequent iopoll passes continue polling it. -- Sashiko AI review · https://sashiko.dev/#/patchset/84dd8106-5b90-4245-a44c-59375382e54f@bytedance.com?part=1 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-02 7:53 ` Fengnan 2026-12-02 8:19 ` sashiko-bot @ 2026-12-02 8:20 ` Christoph Hellwig 2026-12-02 8:43 ` changfengnan 1 sibling, 1 reply; 11+ messages in thread From: Christoph Hellwig @ 2026-12-02 8:20 UTC (permalink / raw) To: Fengnan Cc: Christian Brauner, Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4, tom.leiming, John Garry On Wed, Dec 02, 2026 at 03:53:14PM +0800, Fengnan wrote: > diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c > --- a/fs/iomap/direct-io.c > +++ b/fs/iomap/direct-io.c > @@ -68,8 +68,14 @@ static void iomap_dio_submit_bio(const struct iomap_iter *iter, > > atomic_inc(&dio->ref); > > - /* Sync dio can't be polled reliably */ > - if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) { > + /* > + * Only poll a bio that starts at the beginning of the request and > + * carries all of it, and only if the dio does not wait for completion. > + * 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 ((iocb->ki_flags & IOCB_HIPRI) && !dio->wait_for_completion && > + pos == iocb->ki_pos && bio->bi_iter.bi_size == iter->len) { iomap_dio_bio_iter_one already has some of this just before the call to iomap_dio_submit_bio, and iomap_dio_bio_iter_one already has a slightly different check for a bio that covers the whole range for atomics a little bit above, which seems to miss pos check. Maybe consolidate the entire logic there, something like this (untested): diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c index d7105b5dba7d..750c2a445282 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; @@ -368,15 +361,26 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, bdev_dma_alignment(bio->bi_bdev), alignment - 1); if (unlikely(ret)) goto out_put_bio; - ret = bio->bi_iter.bi_size; - /* - * An atomic write bio must cover the complete length. If it doesn't, - * error out. - */ - if ((op & REQ_ATOMIC) && WARN_ON_ONCE(ret != iomap_length(iter))) { - ret = -EINVAL; - goto out_bio_release_pages; + if (pos == iocb->ki_pos && bio->bi_iter.bi_size == iomap_length(iter)) { + /* + * Only async bios can be polled, and polling is only supported + * for single bio per kiocb. + */ + if ((iocb->ki_flags & IOCB_HIPRI) && + !dio->wait_for_completion) { + bio->bi_opf |= REQ_POLLED; + WRITE_ONCE(iocb->private, bio); + } + } else { + /* + * An atomic write bio must cover the complete length. If it + * doesn't, error out. + */ + if (WARN_ON_ONCE(op & REQ_ATOMIC)) { + ret = -EINVAL; + goto out_bio_release_pages; + } } if (dio->flags & IOMAP_DIO_WRITE) @@ -385,11 +389,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, !(dio->flags & IOMAP_DIO_BOUNCE)) bio_set_pages_dirty(bio); - /* - * We can only poll for single bio I/Os. - */ - if (iov_iter_count(dio->submit.iter)) - dio->iocb->ki_flags &= ~IOCB_HIPRI; + ret = bio->bi_iter.bi_size; iomap_dio_submit_bio(iter, dio, bio, pos); return ret; ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] iomap: disable I/O polling when direct I/O must wait 2026-12-02 8:20 ` Christoph Hellwig @ 2026-12-02 8:43 ` changfengnan 0 siblings, 0 replies; 11+ messages in thread From: changfengnan @ 2026-12-02 8:43 UTC (permalink / raw) To: Christoph Hellwig Cc: Christian Brauner, Christoph Hellwig, Darrick J . Wong, linux-xfs, linux-fsdevel, linux-ext4, tom.leiming, John Garry > From: "Christoph Hellwig"<hch@lst.de> > Date: Wed, Dec 2, 2026, 16:20 > Subject: Re: [PATCH] iomap: disable I/O polling when direct I/O must wait > To: "Fengnan"<changfengnan@bytedance.com> > Cc: "Christian Brauner"<brauner@kernel.org>, "Christoph Hellwig"<hch@lst.de>, "Darrick J . Wong"<djwong@kernel.org>, <linux-xfs@vger.kernel.org>, <linux-fsdevel@vger.kernel.org>, <linux-ext4@vger.kernel.org>, <tom.leiming@gmail.com>, "John Garry"<john.garry@linux.dev> > On Wed, Dec 02, 2026 at 03:53:14PM +0800, Fengnan wrote: > > diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c > > --- a/fs/iomap/direct-io.c > > +++ b/fs/iomap/direct-io.c > > @@ -68,8 +68,14 @@ static void iomap_dio_submit_bio(const struct iomap_iter *iter, > > > > atomic_inc(&dio->ref); > > > > - /* Sync dio can't be polled reliably */ > > - if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) { > > + /* > > + * Only poll a bio that starts at the beginning of the request and > > + * carries all of it, and only if the dio does not wait for completion. > > + * 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 ((iocb->ki_flags & IOCB_HIPRI) && !dio->wait_for_completion && > > + pos == iocb->ki_pos && bio->bi_iter.bi_size == iter->len) { > > iomap_dio_bio_iter_one already has some of this just before the call > to iomap_dio_submit_bio, and iomap_dio_bio_iter_one already has a > slightly different check for a bio that covers the whole range for > atomics a little bit above, which seems to miss pos check. > > Maybe consolidate the entire logic there, something like this (untested): Moving this code into `iomap_dio_bio_iter_one()` seems much cleaner, I'll try to use this way. sashiko-bot also report a real problem, I'll fix it in V2. Thanks. > > diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c > index d7105b5dba7d..750c2a445282 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; > > @@ -368,15 +361,26 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, > bdev_dma_alignment(bio->bi_bdev), alignment - 1); > if (unlikely(ret)) > goto out_put_bio; > - ret = bio->bi_iter.bi_size; > > - /* > - * An atomic write bio must cover the complete length. If it doesn't, > - * error out. > - */ > - if ((op & REQ_ATOMIC) && WARN_ON_ONCE(ret != iomap_length(iter))) { > - ret = -EINVAL; > - goto out_bio_release_pages; > + if (pos == iocb->ki_pos && bio->bi_iter.bi_size == iomap_length(iter)) { > + /* > + * Only async bios can be polled, and polling is only supported > + * for single bio per kiocb. > + */ > + if ((iocb->ki_flags & IOCB_HIPRI) && > + !dio->wait_for_completion) { > + bio->bi_opf |= REQ_POLLED; > + WRITE_ONCE(iocb->private, bio); > + } > + } else { > + /* > + * An atomic write bio must cover the complete length. If it > + * doesn't, error out. > + */ > + if (WARN_ON_ONCE(op & REQ_ATOMIC)) { > + ret = -EINVAL; > + goto out_bio_release_pages; > + } > } > > if (dio->flags & IOMAP_DIO_WRITE) > @@ -385,11 +389,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, > !(dio->flags & IOMAP_DIO_BOUNCE)) > bio_set_pages_dirty(bio); > > - /* > - * We can only poll for single bio I/Os. > - */ > - if (iov_iter_count(dio->submit.iter)) > - dio->iocb->ki_flags &= ~IOCB_HIPRI; > + ret = bio->bi_iter.bi_size; > iomap_dio_submit_bio(iter, dio, bio, pos); > return ret; > ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-12-02 8:43 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-01 3:49 [PATCH] iomap: disable I/O polling when direct I/O must wait Fengnan Chang 2026-12-01 4:06 ` sashiko-bot 2026-12-01 8:08 ` Christoph Hellwig 2026-12-01 8:48 ` Fengnan Chang 2026-12-01 13:25 ` Christian Brauner 2026-12-01 13:26 ` Christian Brauner 2026-12-02 1:56 ` Fengnan 2026-12-02 7:53 ` Fengnan 2026-12-02 8:19 ` sashiko-bot 2026-12-02 8:20 ` Christoph Hellwig 2026-12-02 8:43 ` changfengnan
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.