iomap: avoid memset iomap when iter is done
Subsystems (2)
PATCH v2 1/1 iomap: avoid memset iomap when iter is done
When iomap_iter() finishes its iteration (returns <= 0), it is no longer necessary to memset the entire iomap and srcmap structures.
In high-IOPS scenarios (like 4k randread NVMe polling with io_uring), where the majority of I/Os complete in a single extent map, this wasted memory write bandwidth, as the caller will just discard the iterator. Use this command to test:
taskset -c 30 ./t/io_uring -p1 -d512 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1 /mnt/testfile
IOPS improve about 5% on ext4 and XFS.
However, we MUST still call iomap_iter_reset_iomap() to release the folio_batch if IOMAP_F_FOLIO_BATCH is set, otherwise we leak page references. Therefore, split the cleanup logic: always release the folio_batch, but skip the memset() when ret <= 0.
--- fs/iomap/iter.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/iomap/iter.c +6 -6
@@ -6,17 +6,13 @@#include <linux/iomap.h> #include "trace.h" -static inline void iomap_iter_reset_iomap (struct iomap_iter *iter) +static inline void iomap_iter_clean_fbatch (struct iomap_iter *iter) {if (iter->iomap.flags &IOMAP_F_FOLIO_BATCH ) {folio_batch_release (iter->fbatch);folio_batch_reinit (iter->fbatch); iter->iomap.flags &= ~IOMAP_F_FOLIO_BATCH ; } - - iter->status =0 ; -memset (&iter->iomap,0 ,sizeof (iter->iomap)); -memset (&iter->srcmap,0 ,sizeof (iter->srcmap)); }/* Advance the current iterator position and decrement the remaining length */
@@ -102,10 +98,14 @@ int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops) ret =0 ; else ret =1 ; -iomap_iter_reset_iomap (iter); +iomap_iter_clean_fbatch (iter); + iter->status =0 ;if (ret <=0 )return ret; +memset (&iter->iomap,0 ,sizeof (iter->iomap)); +memset (&iter->srcmap,0 ,sizeof (iter->srcmap)); + begin: ret = ops->iomap_begin (iter->inode, iter->pos, iter->len, iter->flags, &iter->iomap, &iter->srcmap);
Signature · 1 line
2.39.5 (Apple Git-154)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 13 lines · 在 2026/4/20 15:32, [email protected] 写道:
Sashiko AI review found 1 potential issue(s):
- [High] Early return paths iniomap_iter()bypassfolio_batchcleanup, leaking page references. --commit
20686db0394fe5c92c4d207b4ec3987147d0e1ab
Author: Fengnan Chang <[email protected]>iomap: avoid memset iomap when iter is done
This commit splits the cleanup logic in
iomap_iter()to avoid wasted memory writes by skipping the memset of iomap and srcmap when iteration finishes, while extracting thefolio_batchrelease intoiomap_iter_clean_fbatch().
Quoted 5 lines
Quoted 4 lines
However, we MUST still call
iomap_iter_reset_iomap()to release thefolio_batchifIOMAP_F_FOLIO_BATCHis set, otherwise we leak page references. Therefore, split the cleanup logic: always release thefolio_batch, but skip thememset()when ret <= 0.Does this actually always release the
folio_batch?
Just skip memset, always release the folio_batch.
Quoted 50 lines
Quoted 5 lines
fs/iomap/iter.c +0
-0[ ... ]
Quoted 6 lines
fs/iomap/iter.c +1
-1@@ -102,10 +98,14 @@ int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops) ret =0 ; else ret =1 ; -iomap_iter_reset_iomap (iter); +iomap_iter_clean_fbatch (iter);fs/iomap/iter.c int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops) · line 111
Quoted 10 lines
fs/iomap/iter.c +4
-0+ iter->status =0 ;if (ret <=0 )return ret; +memset (&iter->iomap,0 ,sizeof (iter->iomap)); +memset (&iter->srcmap,0 ,sizeof (iter->srcmap)); + begin: ret = ops->iomap_begin (iter->inode, iter->pos, iter->len, iter->flags, &iter->iomap, &iter->srcmap);If
ops->iomap_begin()populatesiter->fbatchand setsIOMAP_F_FOLIO_BATCH(for example, viaiomap_fill_dirty_folios()inxfs_buffered_write_iomap_begin()) but subsequently fails, the next lines in the function are:if (ret < 0) return ret;This returns directly and bypasses the
iomap_iter_clean_fbatch()call.There is also a similar early return at the start of
iomap_iter()before the new cleanup logic:if (ops->iomap_end) { ret = ops->iomap_end(iter->inode, iter->iter_start_pos, iomap_length_trim(iter, iter->iter_start_pos, olen), advanced, iter->flags, &iter->iomap); if (ret < 0 && !advanced) return ret;If
ops->iomap_end()fails without advancing, this early return also skips the cleanup.Since callers like
iomap_zero_range()allocate the batch on the stack and rely oniomap_iter()for cleanup, could these error paths leak folio references when transient I/O errors occur?
Looks good:
Quoted 11 lines · Fengnan Chang
When
iomap_iter()finishes its iteration (returns <= 0), it is no longer necessary to memset the entire iomap and srcmap structures.In high-IOPS scenarios (like 4k randread NVMe polling with
io_uring), where the majority of I/Os complete in a single extent map, this wasted memory write bandwidth, as the caller will just discard the iterator. Use this command to test:
taskset -c 30 ./t/io_uring -p1 -d512 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1/mnt/testfile
IOPS improve about 5% on ext4 and XFS.
Quoted 7 lines
However, we MUST still call
iomap_iter_reset_iomap()to release thefolio_batchifIOMAP_F_FOLIO_BATCHis set, otherwise we leak page references. Therefore, split the cleanup logic: always release thefolio_batch, but skip thememset()when ret <= 0.Signed-off-by Fengnan Chang <[email protected]>---
Quoted 46 lines
fs/iomap/iter.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)fs/iomap/iter.c +6
-6@@ -6,17 +6,13 @@#include <linux/iomap.h> #include "trace.h" -static inline void iomap_iter_reset_iomap (struct iomap_iter *iter) +static inline void iomap_iter_clean_fbatch (struct iomap_iter *iter) {if (iter->iomap.flags &IOMAP_F_FOLIO_BATCH ) {folio_batch_release (iter->fbatch);folio_batch_reinit (iter->fbatch); iter->iomap.flags &= ~IOMAP_F_FOLIO_BATCH ; } - - iter->status =0 ; -memset (&iter->iomap,0 ,sizeof (iter->iomap)); -memset (&iter->srcmap,0 ,sizeof (iter->srcmap)); }/* Advance the current iterator position and decrement the remaining length */ @@ -102,10 +98,14 @@ int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops) ret =0 ; else ret =1 ; -iomap_iter_reset_iomap (iter); +iomap_iter_clean_fbatch (iter); + iter->status =0 ;if (ret <=0 )return ret; +memset (&iter->iomap,0 ,sizeof (iter->iomap)); +memset (&iter->srcmap,0 ,sizeof (iter->srcmap)); + begin: ret = ops->iomap_begin (iter->inode, iter->pos, iter->len, iter->flags, &iter->iomap, &iter->srcmap);Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 11 lines · Fengnan Chang
When
iomap_iter()finishes its iteration (returns <= 0), it is no longer necessary to memset the entire iomap and srcmap structures.In high-IOPS scenarios (like 4k randread NVMe polling with
io_uring), where the majority of I/Os complete in a single extent map, this wasted memory write bandwidth, as the caller will just discard the iterator. Use this command to test:
taskset -c 30 ./t/io_uring -p1 -d512 -b4096 -s32 -c32 -F1 -B1 -R1 -X1 -n1 -P1/mnt/testfile
IOPS improve about 5% on ext4 and XFS.
Quoted 1 line
[...]
Applied to the vfs-7.2.iomap branch of the vfs/vfs.git tree. Patches in the vfs-7.2.iomap 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: master
[1/1] iomap: avoid memset iomap when iter is done
https://git.kernel.org/vfs/vfs/c/72fa5c7e5c81