optimize iomap simple dio reads v2
Subsystems (4)
Hi all,
this series remove a bit of dead code and then optimizes the iomap simple dio read path.
Changes since v2:
- combine the two previously sent patches into a series
- improve the commit log of the second patch
Diffstat:
fs/ext4/ext4.h | 3 --- fs/ext4/file.c | 35 ++++++++++++++++++++++++++++++++++- fs/ext4/inode.c | 3 +-- fs/iomap/direct-io.c | 33 ++++++++++++++------------------- fs/xfs/xfs_file.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- fs/xfs/xfs_iomap.c | 2 +- fs/xfs/xfs_iomap.h | 4 ---- include/linux/iomap.h | 37 ++++++++++++++++++------------------- 8 files changed, 116 insertions(+), 50 deletions(-)
PATCH 1/2 iomap: don't call sb_init_dio_done_wq from the simple dio read path
Since commit bcb0d867a601 ("iomap: use bio_complete_in_task for buffered simple dio read failures"), the simple read path doesn't ever use the per-sb dio aio workqueue, so don't initialize it.
--- fs/iomap/direct-io.c | 7 ------- 1 file changed, 7 deletions(-)
fs/iomap/direct-io.c +0 -7
@@ -1001,13 +1001,6 @@ ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter, goto out_dio_end; } -if (unlikely (!iomi->inode->i_sb->s_dio_done_wq && - !is_sync_kiocb (iocb))) { - ret =sb_init_dio_done_wq (iomi->inode->i_sb); -if (ret <0 ) - goto out_dio_end; - } -trace_iomap_dio_rw_begin (iocb, iter,0 ,0 ); bio = bio_alloc_bioset(iomi->iomap.bdev,
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/2] iomap: don't call sb_init_dio_done_wq from the simple dio read path
Sashiko has finished reviewing this 2-patch series:
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("iomap: don't use struct iomap_iter for simple dio reads"): 3 potential issues (2 Medium, 1 Low)
A detailed review has been sent as a separate reply to patch 2.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Re: [PATCH 1/2] iomap: don't call sb_init_dio_done_wq from the simple dio read path
In reply to Christoph HellwigQuoted 15 lines · Christoph Hellwig
Since commit
bcb0d867a601("iomap: usebio_complete_in_taskfor buffered simple dio read failures"), the simple read path doesn't ever use the per-sb dio aio workqueue, so don't initialize it.Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Fengnan Chang <[email protected]>--- fs/iomap/direct-io.c | 7 ------- 1 file changed, 7 deletions(-)fs/iomap/direct-io.c +0
-0
fs/iomap/direct-io.c ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter, · line 1010
Quoted 10 lines
fs/iomap/direct-io.c +0
-7@@ -1001,13 +1001,6 @@ ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter, goto out_dio_end; } -if (unlikely (!iomi->inode->i_sb->s_dio_done_wq && - !is_sync_kiocb (iocb))) { - ret =sb_init_dio_done_wq (iomi->inode->i_sb); -if (ret <0 ) - goto out_dio_end; - } -
Quoted 6 lines
fs/iomap/direct-io.c +0
-0trace_iomap_dio_rw_begin (iocb, iter,0 ,0 ); bio = bio_alloc_bioset(iomi->iomap.bdev,Signature · 1 line
2.53.0
PATCH 2/2 iomap: don't use struct iomap_iter for simple dio reads
In reply to Christoph HellwigSimple dio reads do not use the iter model, but instead always map kust a single extent without calling back into the file system at completion time.
Given that cacheline usage and zeroing costs where one of the main motivations behind the simple dio read code, try to optimize it by not using the relativly large struct iomap_iter, but only a plain undecored iomap and a hand-crafted callback.
The downside of this is a bit of code duplication for the file system callbacks, although even that has the upside of only inlining the smaller special-purpose version compared to the generic one.
Fengnan Chang tested this with the workloads that originally motived the simple dio read code, and the results are encouraging:
Single-core performance improves significantly:
ext4 / IOPOLL: +4.77% to +6.86%
ext4 / IRQ: +3.51% to +7.73%
XFS / IOPOLL: +4.77% to +12.03%
XFS / IRQ: +8.74% to +9.30%
perf stat reports a reduction by ~201 insns/IO on ext4 (5155.6 -> 4954.2) and by ~365 +insns/IO on XFS (5501.7 -> 5136.7).
--- fs/ext4/ext4.h | 3 --- fs/ext4/file.c | 35 ++++++++++++++++++++++++++++++- fs/ext4/inode.c | 3 +-- fs/iomap/direct-io.c | 26 ++++++++++++----------- fs/xfs/xfs_file.c | 49 ++++++++++++++++++++++++++++++++++++++++++- fs/xfs/xfs_iomap.c | 2 +- fs/xfs/xfs_iomap.h | 4 ---- include/linux/iomap.h | 37 ++++++++++++++++---------------- 8 files changed, 116 insertions(+), 43 deletions(-)
fs/ext4/ext4.h +0 -3
@@ -4371,9 +4371,6 @@ DECLARE_IOMAP_ITER_NEXT(ext4_iomap_buffered_da_write_next);extern const struct iomap_write_ops ext4_iomap_write_ops; -int ext4_iomap_begin (struct inode *inode,loff_t offset,loff_t length, -unsigned flags,struct iomap *iomap,struct iomap *srcmap); -static inline int ext4_buffer_uptodate (struct buffer_head *bh) {/*
fs/ext4/file.c +34 -1
@@ -66,6 +66,39 @@ static bool ext4_should_use_dio(struct kiocb *iocb, struct iov_iter *iter)return IS_ALIGNED (iocb->ki_pos |iov_iter_alignment (iter), dio_align); } +static int ext4_simple_begin (struct inode *inode,loff_t offset,loff_t length, +unsigned flags,struct iomap *iomap) +{ +u8 blkbits = inode->i_blkbits; +struct ext4_map_blocks map; +int ret; + +ASSERT (!(flags & ~(IOMAP_DIRECT |IOMAP_NOWAIT ))); + +if ((offset >> blkbits) >EXT4_MAX_LOGICAL_BLOCK ) +return -EINVAL ; +if (WARN_ON_ONCE (ext4_has_inline_data (inode))) +return -ERANGE ; + + map.m_lblk = offset >> blkbits; + map.m_len =min_t (loff_t, (offset + length -1 ) >> blkbits, +EXT4_MAX_LOGICAL_BLOCK ) - map.m_lblk +1 ; + ret =ext4_map_blocks (NULL , inode, &map,0 ); +if (ret <0 ) +return ret; + +if ((map.m_flags &EXT4_MAP_UNWRITTEN ) || + !(map.m_flags &EXT4_MAP_MAPPED )) +return -ENOTBLK ; + iomap->flags =0 ; + iomap->type =IOMAP_MAPPED ; + iomap->bdev = inode->i_sb->s_bdev; + iomap->addr = (u64 )map.m_pblk << blkbits; + iomap->offset =EXT4_LBLK_TO_B (inode, map.m_lblk); + iomap->length =EXT4_LBLK_TO_B (inode, map.m_len); +return 0 ; +} +static ssize_t ext4_dio_read_iter (struct kiocb *iocb,struct iov_iter *to) {ssize_t ret;
@@ -91,7 +124,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)return generic_file_read_iter (iocb, to); } - ret =iomap_dio_read_simple (iocb, to, ext4_iomap_begin); + ret =iomap_dio_read_simple (iocb, to, ext4_simple_begin);if (ret == -ENOTBLK ) ret =iomap_dio_rw (iocb, to, ext4_iomap_next,NULL ,0 ,NULL ,0 );inode_unlock_shared (inode);
fs/ext4/inode.c +1 -2
@@ -4012,8 +4012,7 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,return ret; } - -int ext4_iomap_begin (struct inode *inode,loff_t offset,loff_t length, +static int ext4_iomap_begin (struct inode *inode,loff_t offset,loff_t length,unsigned flags,struct iomap *iomap,struct iomap *srcmap) {int ret;
fs/iomap/direct-io.c +14 -12
@@ -980,30 +980,32 @@ static void iomap_dio_simple_end_io(struct bio *bio) }ssize_t __iomap_dio_read_simple (struct kiocb *iocb,struct iov_iter *iter, -struct iomap_iter *iomi) +unsigned flags,struct iomap *iomap) { -gfp_t gfp = (iomi->flags &IOMAP_NOWAIT ) ?GFP_NOWAIT :GFP_KERNEL ; +struct inode *inode =file_inode (iocb->ki_filp); +size_t count =iov_iter_count (iter); +gfp_t gfp = (flags &IOMAP_NOWAIT ) ?GFP_NOWAIT :GFP_KERNEL ;struct iomap_dio_simple *sr;unsigned int alignment;struct bio *bio;ssize_t ret; -if (iomi->iomap.type !=IOMAP_MAPPED || - iomi->iomap.offset + iomi->iomap.length < iomi->pos + iomi->len || - (iomi->iomap.flags &IOMAP_F_INTEGRITY )) { +if (iomap->type !=IOMAP_MAPPED || + iomap->offset + iomap->length < iocb->ki_pos + count || + (iomap->flags &IOMAP_F_INTEGRITY )) { ret = -ENOTBLK ; goto out_dio_end; } - alignment =iomap_dio_alignment (iomi->inode, iomi->iomap.bdev,0 ); -if ((iomi->pos | iomi->len) & (alignment -1 )) { + alignment =iomap_dio_alignment (inode, iomap->bdev,0 ); +if ((iocb->ki_pos | count) & (alignment -1 )) { ret = -EINVAL ; goto out_dio_end; }trace_iomap_dio_rw_begin (iocb, iter,0 ,0 ); - bio =bio_alloc_bioset (iomi->iomap.bdev, + bio =bio_alloc_bioset (iomap->bdev,bio_iov_vecs_to_alloc (iter,BIO_MAX_VECS ),REQ_OP_READ , gfp, &iomap_dio_simple_pool);if (!bio) {
@@ -1014,7 +1016,7 @@ ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter, sr->iocb = iocb; sr->dio_flags =0 ; - bio->bi_iter.bi_sector =iomap_sector (&iomi->iomap, iomi->pos); + bio->bi_iter.bi_sector =iomap_sector (iomap, iocb->ki_pos); bio->bi_ioprio = iocb->ki_ioprio; ret = bio_iov_iter_get_pages(bio, iter, BIO_MAX_SIZE,
@@ -1023,7 +1025,7 @@ ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter,if (unlikely (ret)) goto out_bio_put; -if (bio->bi_iter.bi_size != iomi->len) { +if (bio->bi_iter.bi_size != count) {iov_iter_revert (iter, bio->bi_iter.bi_size); ret = -ENOTBLK ; goto out_bio_release_pages;
@@ -1049,7 +1051,7 @@ ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter, } bio->bi_end_io = iomap_dio_simple_end_io;submit_bio (bio); -trace_iomap_dio_rw_queued (iomi->inode, iocb->ki_pos, iomi->len); +trace_iomap_dio_rw_queued (inode, iocb->ki_pos, count);return -EIOCBQUEUED ; out_bio_release_pages:
@@ -1057,7 +1059,7 @@ ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter, out_bio_put:bio_put (bio); out_dio_end: -inode_dio_end (iomi->inode); +inode_dio_end (inode);return ret; }EXPORT_SYMBOL_GPL (__iomap_dio_read_simple);
fs/xfs/xfs_file.c +48 -1
61 lines not on this page · raw message
fs/xfs/xfs_iomap.c +1 -1
@@ -2201,7 +2201,7 @@ xfs_buffered_write_iomap_end(DEFINE_IOMAP_ITER_NEXT_END (xfs_buffered_write_iomap_next, xfs_buffered_write_iomap_begin, xfs_buffered_write_iomap_end); -int +static int xfs_read_iomap_begin( struct inode *inode, loff_t offset,
fs/xfs/xfs_iomap.h +0 -4
@@ -66,10 +66,6 @@ xfs_aligned_fsb_count(return count_fsb; } -int xfs_read_iomap_begin (struct inode *inode,loff_t offset, -loff_t length,unsigned flags,struct iomap *iomap, -struct iomap *srcmap); -DECLARE_IOMAP_ITER_NEXT (xfs_buffered_write_iomap_next);DECLARE_IOMAP_ITER_NEXT (xfs_direct_write_iomap_next);DECLARE_IOMAP_ITER_NEXT (xfs_zoned_direct_write_iomap_next);
include/linux/iomap.h +18 -19
@@ -740,20 +740,20 @@ void iomap_dio_bio_end_io(struct bio *bio);* The caller can only provide an iomap begin handler, and the iterator * is never advanced. */ +typedef int (*iomap_simple_begin_fn )(struct inode *inode,loff_t pos, +loff_t length,unsigned flags,struct iomap *iomap);ssize_t __iomap_dio_read_simple (struct kiocb *iocb,struct iov_iter *iter, -struct iomap_iter *iomi); +unsigned flags,struct iomap *iomap); static __always_inline ssize_t iomap_dio_read_simple(struct kiocb *iocb, -struct iov_iter *iter,iomap_iter_begin_fn begin) +struct iov_iter *iter,iomap_simple_begin_fn begin) { -struct iomap_iter iomi = { - .inode =file_inode (iocb->ki_filp), - .pos = iocb->ki_pos, - .len =iov_iter_count (iter), - .flags =IOMAP_DIRECT , - }; +struct inode *inode =file_inode (iocb->ki_filp); +size_t count =iov_iter_count (iter); +unsigned flags =IOMAP_DIRECT ; +struct iomap iomap;ssize_t ret; -if (!iomi.len) +if (!count)return 0 ;/*
@@ -761,29 +761,28 @@ static __always_inline ssize_t iomap_dio_read_simple(struct kiocb *iocb,* early as it's the most common case to fail for typical direct IO * workloads. */ -if (iomi.len > iomi.inode->i_sb->s_blocksize) +if (count > inode->i_sb->s_blocksize)return -ENOTBLK ; -if (iocb->ki_pos + iomi.len >i_size_read (iomi.inode)) +if (iocb->ki_pos + count >i_size_read (inode))return -ENOTBLK ; -if (IS_ENCRYPTED (iomi.inode)) +if (IS_ENCRYPTED (inode))return -ENOTBLK ; - ret =kiocb_write_and_wait (iocb, iomi.len); + ret =kiocb_write_and_wait (iocb, count);if (ret)return ret;if (iocb->ki_flags &IOCB_NOWAIT ) - iomi.flags |=IOMAP_NOWAIT ; + flags |=IOMAP_NOWAIT ; -inode_dio_begin (iomi.inode); - ret =begin (iomi.inode, iomi.pos, iomi.len, iomi.flags, &iomi.iomap, - &iomi.srcmap); +inode_dio_begin (inode); + ret =begin (inode, iocb->ki_pos, count, flags, &iomap);if (ret) { -inode_dio_end (iomi.inode); +inode_dio_end (inode);return ret; } -return __iomap_dio_read_simple (iocb, iter, &iomi); +return __iomap_dio_read_simple (iocb, iter, flags, &iomap); }#ifdef CONFIG_SWAP
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/2] iomap: don't use struct iomap_iter for simple dio reads
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] ext4: making ext4_iomap_begin() static breaks CONFIG_EXT4_KUNIT_TESTS builds - [Medium] xfs…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] ext4: making ext4_iomap_begin() static breaks CONFIG_EXT4_KUNIT_TESTS builds
- [Medium] xfs: xfs_read_simple_begin() returns -ENOTBLK instead of -EAGAIN on NOWAIT
- [Low] iomap: iomap_dio_read_simple() comment omits uninitialized iomap contract
--
commit 5329ccff0d2b6f54203c2a7e79854425463b11e1
Author: Christoph Hellwig <[email protected]>
iomap: don't use struct iomap_iter for simple dio reads
This commit replaces struct iomap_iter in iomap_dio_read_simple() with a plain struct iomap and a dedicated iomap_simple_begin_fn callback, adding simple-read begin handlers in ext4 and XFS.
[ ... ]
Quoted 5 lines
fs/ext4/inode.c +0
-0
fs/ext4/inode.c static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, · line 4018
Quoted 9 lines
fs/ext4/inode.c +1
-2@@ -4012,8 +4012,7 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,return ret; } - -int ext4_iomap_begin (struct inode *inode,loff_t offset,loff_t length, +static int ext4_iomap_begin (struct inode *inode,loff_t offset,loff_t length,unsigned flags,struct iomap *iomap,struct iomap *srcmap) {int ret;
[Severity: Medium]
Does making ext4_iomap_begin() static break CONFIG_EXT4_KUNIT_TESTS builds?
fs/ext4/inode.c still has EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_iomap_begin), which expands to an extern declaration and conflicts with the static definition.
inode_test_iomap_and_atomic_writes() in fs/ext4/inode-test.c also calls ext4_iomap_begin() directly, so removing the prototype from fs/ext4/ext4.h and external linkage breaks compiling and linking ext4-test.o.
[ ... ]
Quoted 18 lines
fs/xfs/xfs_file.c +10
-0@@ -189,6 +189,53 @@ static const struct iomap_dio_ops xfs_dio_read_bounce_ops = { .bio_set = &iomap_ioend_bioset, }; +static int +xfs_read_simple_begin ( +struct inode *inode, +loff_t offset, +loff_t length, +unsigned flags, +struct iomap *iomap) +{ +struct xfs_inode *ip =XFS_I (inode); +struct xfs_mount *mp = ip->i_mount;
fs/xfs/xfs_file.c static const struct iomap_dio_ops xfs_dio_read_bounce_ops = { · line 211
Quoted 10 lines
fs/xfs/xfs_file.c +10
-0+xfs_fileoff_t offset_fsb =XFS_B_TO_FSBT (mp, offset); +struct xfs_bmbt_irec imap; +struct xfs_iext_cursor icur; + +ASSERT (!(flags & ~(IOMAP_DIRECT |IOMAP_NOWAIT ))); + +if (xfs_is_shutdown (mp)) +return -EIO ; +if (xfs_need_iread_extents (&ip->i_df)) +return -ENOTBLK ;
[Severity: Medium]
Should this return -EAGAIN instead of -ENOTBLK when IOMAP_NOWAIT is set?
When extents are not yet loaded on a nonblocking direct read, returning -ENOTBLK falls back to iomap_dio_rw(), where __iomap_dio_rw() allocates struct iomap_dio with GFP_KERNEL before xfs_ilock_for_iomap() fails with -EAGAIN.
Returning (flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOTBLK preserves the previous behavior and avoids a potentially sleeping allocation on IOCB_NOWAIT.
[ ... ]
Quoted 17 lines
include/linux/iomap.h +4
-2@@ -740,20 +740,20 @@ void iomap_dio_bio_end_io(struct bio *bio);* The caller can only provide an iomap begin handler, and the iterator * is never advanced. */ +typedef int (*iomap_simple_begin_fn )(struct inode *inode,loff_t pos, +loff_t length,unsigned flags,struct iomap *iomap);ssize_t __iomap_dio_read_simple (struct kiocb *iocb,struct iov_iter *iter, -struct iomap_iter *iomi); +unsigned flags,struct iomap *iomap); static __always_inline ssize_t iomap_dio_read_simple(struct kiocb *iocb, -struct iov_iter *iter,iomap_iter_begin_fn begin) +struct iov_iter *iter,iomap_simple_begin_fn begin) {
include/linux/iomap.h void iomap_dio_bio_end_io(struct bio *bio); · line 753
Quoted 10 lines
include/linux/iomap.h +4
-6-struct iomap_iter iomi = { - .inode =file_inode (iocb->ki_filp), - .pos = iocb->ki_pos, - .len =iov_iter_count (iter), - .flags =IOMAP_DIRECT , - }; +struct inode *inode =file_inode (iocb->ki_filp); +size_t count =iov_iter_count (iter); +unsigned flags =IOMAP_DIRECT ; +struct iomap iomap;
[Severity: Low]
Could the comment above iomap_simple_begin_fn document that iomap is uninitialized on entry?
The comment still mentions the removed iterator ("and the iterator is never advanced") and does not state that iomap_dio_read_simple() passes an uninitialized stack struct iomap, unlike iomap_iter.
A future caller assuming zero-initialization (e.g., omitting iomap->flags = 0) would expose uninitialized stack memory to IOMAP_F_INTEGRITY and IOMAP_F_ANON_WRITE checks in __iomap_dio_read_simple() and iomap_sector().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2