From: Fengnan Chang With the ->iomap_next() model, iomap_dio_simple() will run a second, full iomap_iter() just to finish the range. For DIO reads on filesystems with no ->iomap_end() work (ext4, xfs, exfat, ntfs) this is a no-op, but still costs an extra indirect ->iomap_next() call and the iomap_iter() frames on every I/O. Add IOMAP_DIO_NO_IOMAP_END so such callers can promise there is no ->iomap_end() work and no folio batch, letting iomap_dio_simple() skip the finishing iomap_iter() on both the success and error-exit paths. Opt in the ext4, xfs, exfat and ntfs direct read paths; filesystems with real read-path ->iomap_end() work keep the existing behaviour. Suggested-by: Christoph Hellwig Signed-off-by: Fengnan Chang Signed-off-by: Joanne Koong --- fs/exfat/file.c | 4 ++-- fs/ext4/file.c | 3 ++- fs/iomap/direct-io.c | 16 +++++++++++++--- fs/iomap/trace.h | 3 ++- fs/ntfs/file.c | 4 ++-- fs/xfs/xfs_file.c | 2 +- include/linux/iomap.h | 8 ++++++++ 7 files changed, 30 insertions(+), 10 deletions(-) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index 5fc13378d35f..3a278c95239e 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -809,8 +809,8 @@ static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) if (iocb->ki_flags & IOCB_DIRECT) { file_accessed(iocb->ki_filp); - ret = iomap_dio_rw(iocb, iter, &exfat_iomap_ops, NULL, 0, - NULL, 0); + ret = iomap_dio_rw(iocb, iter, &exfat_iomap_ops, NULL, + IOMAP_DIO_NO_IOMAP_END, NULL, 0); } else { ret = generic_file_read_iter(iocb, iter); } diff --git a/fs/ext4/file.c b/fs/ext4/file.c index eb1a323962b1..7bd5de67ce78 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -91,7 +91,8 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) return generic_file_read_iter(iocb, to); } - ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL, 0, NULL, 0); + ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL, + IOMAP_DIO_NO_IOMAP_END, NULL, 0); inode_unlock_shared(inode); file_accessed(iocb->ki_filp); diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c index a89c7c697c63..fcdae3ef6dd0 100644 --- a/fs/iomap/direct-io.c +++ b/fs/iomap/direct-io.c @@ -1040,7 +1040,9 @@ iomap_dio_simple_finish(struct iomap_iter *iomi, const struct iomap_ops *ops, * @dio_flags must not request IOMAP_DIO_FORCE_WAIT, IOMAP_DIO_PARTIAL, or * IOMAP_DIO_BOUNCE: this path does not support forced waiting, partial direct * I/O, or bouncing. The range must also stay within i_size and encrypted - * inodes must use the generic iomap direct I/O path. + * inodes must use the generic iomap direct I/O path. IOMAP_DIO_NO_IOMAP_END is + * a fast-path optimization the caller can set if there is no work that needs to + * be finished after a mapping. * * -ENOTBLK is the private sentinel returned by iomap_dio_simple() when it * decides the request does not fit the fast path. In that case we proceed to @@ -1154,7 +1156,14 @@ iomap_dio_simple(struct kiocb *iocb, struct iov_iter *iter, WRITE_ONCE(iocb->private, bio); } - iomap_dio_simple_finish(&iomi, ops, count, count); + /* + * The finishing iomap_iter() only runs the filesystem's iomap_end() + * work and releases any attached folio batch. When the caller promised + * neither applies (IOMAP_DIO_NO_IOMAP_END), skip it to avoid an extra + * iomap_next() call on the hot read path. + */ + if (!(dio_flags & IOMAP_DIO_NO_IOMAP_END)) + iomap_dio_simple_finish(&iomi, ops, count, count); if (!wait_for_completion) { bio->bi_end_io = iomap_dio_simple_end_io; @@ -1171,7 +1180,8 @@ iomap_dio_simple(struct kiocb *iocb, struct iov_iter *iter, out_bio_put: bio_put(bio); out_iomap_end: - iomap_dio_simple_finish(&iomi, ops, count, 0); + if (!(dio_flags & IOMAP_DIO_NO_IOMAP_END)) + iomap_dio_simple_finish(&iomi, ops, count, 0); inode_dio_end(inode); return ret; } diff --git a/fs/iomap/trace.h b/fs/iomap/trace.h index e1ea8392cf47..e43ca419b13a 100644 --- a/fs/iomap/trace.h +++ b/fs/iomap/trace.h @@ -127,7 +127,8 @@ DEFINE_RANGE_EVENT(iomap_zero_iter); {IOMAP_DIO_FORCE_WAIT, "DIO_FORCE_WAIT" }, \ {IOMAP_DIO_OVERWRITE_ONLY, "DIO_OVERWRITE_ONLY" }, \ {IOMAP_DIO_PARTIAL, "DIO_PARTIAL" }, \ - {IOMAP_DIO_FSBLOCK_ALIGNED, "DIO_FSBLOCK_ALIGNED" } + {IOMAP_DIO_FSBLOCK_ALIGNED, "DIO_FSBLOCK_ALIGNED" }, \ + {IOMAP_DIO_NO_IOMAP_END, "DIO_NO_IOMAP_END" } DECLARE_EVENT_CLASS(iomap_class, TP_PROTO(struct inode *inode, struct iomap *iomap), diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index 6a7b638e523d..2e72069c593a 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -458,8 +458,8 @@ static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) } file_accessed(iocb->ki_filp); - ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, 0, - NULL, 0); + ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, + IOMAP_DIO_NO_IOMAP_END, NULL, 0); } else { ret = generic_file_read_iter(iocb, to); } diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 845a97c9b063..8b8de8064e31 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -251,7 +251,7 @@ xfs_file_dio_read( struct iov_iter *to) { struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp)); - unsigned int dio_flags = 0; + unsigned int dio_flags = IOMAP_DIO_NO_IOMAP_END; const struct iomap_dio_ops *dio_ops = NULL; ssize_t ret; diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 74d79b963533..8662bd528e9e 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -674,6 +674,14 @@ struct iomap_dio_ops { */ #define IOMAP_DIO_BOUNCE (1 << 4) +/* + * The caller's ->iomap_next() has no ->iomap_end() work to run and this I/O + * does not use a folio batch, so the iomap_dio_simple() fast path may skip the + * finishing iomap_iter() call entirely. Only meaningful on the simple read + * fast path; ignored by the generic __iomap_dio_rw() slow path. + */ +#define IOMAP_DIO_NO_IOMAP_END (1 << 5) + ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, const struct iomap_ops *ops, const struct iomap_dio_ops *dops, unsigned int dio_flags, void *private, size_t done_before); -- 2.52.0