Skip to content

RFC iomap: fix race between iomap_set_range_uptodate and folio_end_read

Sasha Levin gfs2 linux-block linux-doc linux-fsdevel linux-xfs lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest1 patch 20 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/1iomap: fix race between iomap_set_range_uptodate and folio_end_readfs/fuse/dev.c fs/fuse/file.c fs/iomap/buffered-io.c +1 more+68 -820 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 0/1] iomap: fix race between iomap_set_range_uptodate and folio_end_read

Hi Joanne,

While testing with your FUSE iomap patchset that recently landed upstream, I ran into a warning in ifs_free() where the folio's uptodate flag didn't match the ifs per-block uptodate bitmap. The warning was triggered during FUSE-based filesystem unmount when running the LTP writev03 test.

After some investigation, I believe the root cause is a race condition that has existed since commit 7a4847e54cc1 ("iomap: use folio_end_read()") but was difficult to trigger until now. The issue is that folio_end_read() uses XOR semantics to set the uptodate bit, so if iomap_set_range_uptodate() calls folio_mark_uptodate() while a read is in progress, the subsequent folio_end_read() will XOR and clear the uptodate bit.

The FUSE iomap enablement seems to have created the right conditions to expose this race - likely due to different file extent patterns in FUSE-based filesystems (like NTFS-3G) compared to native filesystems like XFS/ext4.

The fix checks read_bytes_pending under the state_lock in iomap_set_range_uptodate() and skips calling folio_mark_uptodate() if a read is in progress, letting the read completion path handle it.

I'm not very familiar with the iomap internals, so I'd really appreciate your review and feedback on whether this approach is correct.

Thanks,
Sasha

Sasha Levin (1):
iomap: fix race between iomap_set_range_uptodate and folio_end_read

 fs/fuse/dev.c          |  3 +-
 fs/fuse/file.c         |  6 ++--
 fs/iomap/buffered-io.c | 65 +++++++++++++++++++++++++++++++++++++++---
 include/linux/iomap.h  |  2 ++
 4 files changed, 68 insertions(+), 8 deletions(-)
Signature · 1 line
2.51.0