btrfs direct-io using iomap
Subsystems (7)
v10 cover btrfs direct-io using iomap
This is an effort to use iomap for direct I/O in btrfs. This would change the call from __blockdev_direct_io() to iomap_dio_rw(). These are remanants of the series which was revoked due to page invalidation errors and adds patches in iomap for proper buffered fallback.
The main objective is to lose the buffer head and use bio defined by iomap code, and hopefully to use more of generic-FS codebase.
These patches are based and tested on vanilla. I have tested it against xfstests.
The tree is available at
https://github.com/goldwynr/linux/tree/btrfs-iomap-dio
Changes since v1
- Incorporated back the efficiency change for inode locking
- Review comments about coding style and git comments
- Merge related patches into one
- Direct read to go through btrfs_direct_IO()
- Removal of no longer used function dio_end_io()
Changes since v2
- aligning iomap offset/length to the position/length of I/O
- Removed btrfs_dio_data
- Removed BTRFS_INODE_READDIO_NEED_LOCK
- Re-incorporating write efficiency changes caused lockdep_assert() in iomap to be triggered, remove that code.
Changes since v3
- Fixed freeze on generic/095. Use iomap_end() to account for failed/incomplete dio instead of btrfs_dio_data
Changes since v4
- moved lockdep_assert_held() to functions calling iomap_dio_rw() This may be called immidiately after calling inode lock and may feel not required, but it seems important.
- Removed comments which are no longer required
- Changed commit comments to make them more appropriate
Changes since v5
- restore inode_dio_wait() in truncate
- Removed lockdep_assert_held() near callers
Changes since v6
- Fixed hangs due to underlying device failures
- Removed the patch to wait while releasing pages
Changes since v7
- Moved reservation into btrfs iomap begin()/end() for correct reservation cleanup in case of device error.
- Merged patches switch to iomap, and adding btrfs_iomap_end() for easier bisection. The switch to iomap would fail in case of dio after buffered writes.
Changes since v8
- Added a flag to iomap_dio_rw() to return zero for buffered fallback
Changes since v10
- flag name changes for iomap_dio_rw()
--
Goldwyn
PATCH v10 1/6 iomap: Convert wait_for_completion to flags
From: Goldwyn Rodrigues <[email protected]>
Convert wait_for_completion boolean to flags so we can pass more flags to iomap_dio_rw()
--- fs/ext4/file.c | 11 +++++++++-- fs/gfs2/file.c | 14 ++++++++++---- fs/iomap/direct-io.c | 3 ++- fs/xfs/xfs_file.c | 15 +++++++++++---- fs/zonefs/super.c | 16 ++++++++++++---- include/linux/iomap.h | 11 ++++++++++- 6 files changed, 54 insertions(+), 16 deletions(-)
fs/ext4/file.c +9 -2
@@ -53,6 +53,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) {ssize_t ret;struct inode *inode =file_inode (iocb->ki_filp); +int flags =0 ;if (iocb->ki_flags &IOCB_NOWAIT ) {if (!inode_trylock_shared (inode))
@@ -74,8 +75,11 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)return generic_file_read_iter (iocb, to); } +if (is_sync_kiocb (iocb)) + flags |=IOMAP_DIO_RWF_SYNCIO ; + ret =iomap_dio_rw (iocb, to, &ext4_iomap_ops,NULL , -is_sync_kiocb (iocb)); + flags);inode_unlock_shared (inode);file_accessed (iocb->ki_filp);
@@ -457,6 +461,7 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)const struct iomap_ops *iomap_ops = &ext4_iomap_ops;bool extend = false, unaligned_io = false;bool ilock_shared = true; +int flags =0 ;/* * We initially start with shared inode lock unless it is
@@ -540,10 +545,12 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)ext4_journal_stop (handle); } +if (is_sync_kiocb (iocb) || unaligned_io || extend) + flags |=IOMAP_DIO_RWF_SYNCIO ;if (ilock_shared) iomap_ops = &ext4_iomap_overwrite_ops; ret =iomap_dio_rw (iocb, from, iomap_ops, &ext4_dio_write_ops, -is_sync_kiocb (iocb) || unaligned_io || extend); + flags);if (extend) ret =ext4_handle_inode_extension (inode, offset, ret, count);
fs/gfs2/file.c +10 -4
@@ -767,6 +767,7 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to)size_t count =iov_iter_count (to);struct gfs2_holder gh;ssize_t ret; +int flags =0 ;if (!count)return 0 ;/* skip atime */
@@ -776,8 +777,10 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to)if (ret) goto out_uninit; - ret =iomap_dio_rw (iocb, to, &gfs2_iomap_ops,NULL , -is_sync_kiocb (iocb)); +if (is_sync_kiocb (iocb)) + flags |=IOMAP_DIO_RWF_SYNCIO ; + + ret =iomap_dio_rw (iocb, to, &gfs2_iomap_ops,NULL , flags);gfs2_glock_dq (&gh); out_uninit:
@@ -794,6 +797,7 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)loff_t offset = iocb->ki_pos;struct gfs2_holder gh;ssize_t ret; +int flags =0 ;/* * Deferred lock, even if its a write, since we do no allocation on
@@ -812,8 +816,10 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)if (offset + len >i_size_read (&ip->i_inode)) goto out; - ret =iomap_dio_rw (iocb, from, &gfs2_iomap_ops,NULL , -is_sync_kiocb (iocb)); +if (is_sync_kiocb (iocb)) + flags |=IOMAP_DIO_RWF_SYNCIO ; + + ret =iomap_dio_rw (iocb, from, &gfs2_iomap_ops,NULL , flags); out:gfs2_glock_dq (&gh);
fs/iomap/direct-io.c +2 -1
@@ -405,7 +405,7 @@ iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -bool wait_for_completion) +int dio_flags) {struct address_space *mapping = iocb->ki_filp->f_mapping;struct inode *inode =file_inode (iocb->ki_filp);
@@ -415,6 +415,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,unsigned int flags =IOMAP_DIRECT ;struct blk_plug plug;struct iomap_dio *dio; +bool wait_for_completion = dio_flags &IOMAP_DIO_RWF_SYNCIO ;if (!count)return 0 ;
fs/xfs/xfs_file.c +11 -4
@@ -169,6 +169,8 @@ xfs_file_dio_aio_read(struct xfs_inode *ip =XFS_I (file_inode (iocb->ki_filp));size_t count =iov_iter_count (to);ssize_t ret; +int flags =0 ; +trace_xfs_file_direct_read (ip, count, iocb->ki_pos);
@@ -183,8 +185,11 @@ xfs_file_dio_aio_read( }else {xfs_ilock (ip,XFS_IOLOCK_SHARED ); } - ret =iomap_dio_rw (iocb, to, &xfs_read_iomap_ops,NULL , -is_sync_kiocb (iocb)); + +if (is_sync_kiocb (iocb)) + flags |=IOMAP_DIO_RWF_SYNCIO ; + + ret =iomap_dio_rw (iocb, to, &xfs_read_iomap_ops,NULL , flags);xfs_iunlock (ip,XFS_IOLOCK_SHARED );return ret;
@@ -483,6 +488,7 @@ xfs_file_dio_aio_write(int iolock;size_t count =iov_iter_count (from);struct xfs_buftarg *target =xfs_inode_buftarg (ip); +int flags =0 ;/* DIO must be aligned to device logical sector size */ if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
@@ -546,9 +552,10 @@ xfs_file_dio_aio_write(* If unaligned, this is the only IO in-flight. Wait on it before we * release the iolock to prevent subsequent overlapping IO. */ +if (is_sync_kiocb (iocb) || unaligned_io) + flags |=IOMAP_DIO_RWF_SYNCIO ; ret =iomap_dio_rw (iocb, from, &xfs_direct_write_iomap_ops, - &xfs_dio_write_ops, -is_sync_kiocb (iocb) || unaligned_io); + &xfs_dio_write_ops, flags); out:xfs_iunlock (ip, iolock);
fs/zonefs/super.c +12 -4
@@ -670,6 +670,7 @@ static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from)bool append = false;size_t count;ssize_t ret; +int flags =0 ;/* * For async direct IOs to sequential zone files, refuse IOCB_NOWAIT
@@ -711,11 +712,15 @@ static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from) append = sync; } -if (append) +if (append) { ret =zonefs_file_dio_append (iocb, from); -else + }else { +if (is_sync_kiocb (iocb)) + flags |=IOMAP_DIO_RWF_SYNCIO ; + ret =iomap_dio_rw (iocb, from, &zonefs_iomap_ops, - &zonefs_write_dio_ops, sync); + &zonefs_write_dio_ops, flags); + }if (zi->i_ztype ==ZONEFS_ZTYPE_SEQ && (ret >0 || ret == -EIOCBQUEUED )) {if (ret >0 )
@@ -814,6 +819,7 @@ static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)struct super_block *sb = inode->i_sb;loff_t isize;ssize_t ret; +int flags =0 ;/* Offline zones cannot be read */ if (unlikely (IS_IMMUTABLE (inode) && !(inode->i_mode &0777 )))
@@ -848,8 +854,10 @@ static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) goto inode_unlock; }file_accessed (iocb->ki_filp); +if (is_sync_kiocb (iocb)) + flags |=IOMAP_DIO_RWF_SYNCIO ; ret =iomap_dio_rw (iocb, to, &zonefs_iomap_ops, - &zonefs_read_dio_ops,is_sync_kiocb (iocb)); + &zonefs_read_dio_ops, flags); }else { ret =generic_file_read_iter (iocb, to);if (ret == -EIO )
include/linux/iomap.h +10 -1
@@ -255,9 +255,18 @@ struct iomap_dio_ops { struct bio *bio, loff_t file_offset); }; +/* +* Flags to pass iomap_dio_rw() +*/ + +/* +* Wait for completion of DIO +*/ +#define IOMAP_DIO_RWF_SYNCIO (1 << 0) +ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -bool wait_for_completion); +int flags);int iomap_dio_iopoll (struct kiocb *kiocb,bool spin);#ifdef CONFIG_SWAP
Signature · 1 line
2.26.2
Re: [PATCH 1/6] iomap: Convert wait_for_completion to flags
fs/iomap/direct-io.c iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length, · line 408
Quoted 13 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
Convert
wait_for_completionboolean to flags so we can pass more flags toiomap_dio_rw()fs/iomap/direct-io.c +1
-1@@ -405,7 +405,7 @@ iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -bool wait_for_completion) +int dio_flags)
I think flags should be an unsigned type as it's for bitwise operations.
Re: [PATCH 1/6] iomap: Convert wait_for_completion to flags
In reply to Goldwyn RodriguesQuoted 5 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
Convert
wait_for_completionboolean to flags so we can pass more flags toiomap_dio_rw()
Quoted 8 lines
Signed-off-by Goldwyn Rodrigues <[email protected]>--- fs/ext4/file.c | 11 +++++++++-- fs/gfs2/file.c | 14 ++++++++++---- fs/iomap/direct-io.c | 3 ++- fs/xfs/xfs_file.c | 15 +++++++++++---- fs/zonefs/super.c | 16 ++++++++++++---- include/linux/iomap.h | 11 ++++++++++-
Though it's an API change I think you should CC all involved subsystems' mailinglists too. I don't see GFS2 or zonefs.
Re: [PATCH 1/6] iomap: Convert wait_for_completion to flags
Quoted 2 lines · David Sterba
Though it's an API change I think you should CC all involved subsystems' mailinglists too. I don't see GFS2 or zonefs.
zonefs doesn't have a list on it's own. We're using fsdevel. But both Damien and me are subscribed to the btrfs list as well so no biggie for zonefs.
Re: [PATCH 1/6] iomap: Convert wait_for_completion to flags
In reply to Goldwyn RodriguesQuoted 4 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
Convert
wait_for_completionboolean to flags so we can pass more flags toiomap_dio_rw()
FYI, when I did this a while ago for a stalled patch series I used a single namespace for the flags passed to iomap_dio_rw and the flags store in dio->flag, which at that point seemed a bit cleaner to me:
http://git.infradead.org/users/hch/xfs.git/commitdiff/1733619fefab1b0020d3ab91ebd0a72ccec982af
PATCH v10 2/6 iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
In reply to Goldwyn RodriguesFrom: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag IOMAP_DIO_RWF_NO_STALE_PAGECACHE to indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.
--- fs/iomap/direct-io.c | 8 +++++++- include/linux/iomap.h | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
fs/iomap/direct-io.c +7 -1
@@ -484,8 +484,14 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,*/ ret =invalidate_inode_pages2_range (mapping, pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) +if (ret) { +if (dio_flags &IOMAP_DIO_RWF_NO_STALE_PAGECACHE ) { +if (ret == -EBUSY ) + ret =0 ; + goto out_free_dio; + }dio_warn_stale_pagecache (iocb->ki_filp); + } ret =0 ; if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
include/linux/iomap.h +14 -0
@@ -262,7 +262,21 @@ struct iomap_dio_ops {/* * Wait for completion of DIO */ +#define IOMAP_DIO_RWF_SYNCIO (1 << 0) +/* +* Direct IO will attempt to keep the page cache coherent by +* invalidating the inode's page cache over the range of the DIO. +* That can fail if something else is actively using the page cache. +* If this happens and the DIO continues, the data in the page +* cache will become stale. +* +* Set this flag if you want the DIO to abort without issuing any IO +* or error if it fails to invalidate the page cache successfully. +* This allows the IO submitter to fallback to buffered IO to resubmit +* IO +*/ +#define IOMAP_DIO_RWF_NO_STALE_PAGECACHE (1 << 1) ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
Signature · 1 line
2.26.2
always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>
I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Adding all the relevant lists.
Quoted 53 lines
--- fs/iomap/direct-io.c | 8 +++++++- include/linux/iomap.h | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)fs/iomap/direct-io.c +7
-1@@ -484,8 +484,14 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,*/ ret =invalidate_inode_pages2_range (mapping, pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) +if (ret) { +if (dio_flags &IOMAP_DIO_RWF_NO_STALE_PAGECACHE ) { +if (ret == -EBUSY ) + ret =0 ; + goto out_free_dio; + }dio_warn_stale_pagecache (iocb->ki_filp); + } ret =0 ; if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&include/linux/iomap.h +14
-0@@ -262,7 +262,21 @@ struct iomap_dio_ops {/* * Wait for completion of DIO */ +#define IOMAP_DIO_RWF_SYNCIO (1 << 0) +/* +* Direct IO will attempt to keep the page cache coherent by +* invalidating the inode's page cache over the range of the DIO. +* That can fail if something else is actively using the page cache. +* If this happens and the DIO continues, the data in the page +* cache will become stale. +* +* Set this flag if you want the DIO to abort without issuing any IO +* or error if it fails to invalidate the page cache successfully. +* This allows the IO submitter to fallback to buffered IO to resubmit +* IO +*/ +#define IOMAP_DIO_RWF_NO_STALE_PAGECACHE (1 << 1) ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, const struct iomap_ops *ops, const struct iomap_dio_ops *dops,Signature · 1 line
2.26.2
---end quoted text---
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>
Quoted 6 lines
I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Adding all the relevant lists.
Since no one responded so far, let me see if I can stir the cauldron :)
What error should be returned in case of such an error? I think the userspace process must be immediately informed if it in unable to invalidate the page cache and complete the direct I/O. Currently, the iomap code treats this as a writeback error and continues with the direct I/O and the userspace process comes to know only during file closure.
If such a change is incorporated, are the current userspace applications prepared for it?
Signature · 1 line
Goldwyn
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 16 lines · Goldwyn Rodrigues
Quoted 15 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Quoted 5 lines
Quoted 1 line
Adding all the relevant lists.
Since no one responded so far, let me see if I can stir the cauldron :)
What error should be returned in case of such an error? I think the
Christoph's message is ambiguous. I don't know if he means "fail the I/O with an error" or "satisfy the I/O through the page cache". I'm strongly in favour of the latter. Indeed, I'm in favour of not invalidating the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 19 lines · Matthew Wilcox
Quoted 18 lines · Goldwyn Rodrigues
Quoted 16 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Adding all the relevant lists.
Quoted 7 lines
Quoted 3 lines
Since no one responded so far, let me see if I can stir the cauldron :)
What error should be returned in case of such an error? I think the
Christoph's message is ambiguous. I don't know if he means "fail the I/O with an error" or "satisfy the I/O through the page cache". I'm strongly in favour of the latter.
Same here. Sorry if my previous mail was unclear.
Quoted 6 lines
Indeed, I'm in favour of not invalidating
the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.
Something like that, yes.
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 30 lines · Christoph Hellwig
Quoted 19 lines · Matthew Wilcox
Quoted 18 lines · Goldwyn Rodrigues
Quoted 16 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Adding all the relevant lists.
Quoted 7 lines
Quoted 3 lines
Since no one responded so far, let me see if I can stir the cauldron :)
What error should be returned in case of such an error? I think the
Christoph's message is ambiguous. I don't know if he means "fail the I/O with an error" or "satisfy the I/O through the page cache". I'm strongly in favour of the latter.
Same here. Sorry if my previous mail was unclear.
Quoted 8 lines
Quoted 6 lines
Indeed, I'm in favour of not invalidating
the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.Something like that, yes.
So are we really willing to take the performance regression that occurs from copying out of the page cache consuming lots more CPU than an actual direct IO read? Or that direct IO writes suddenly serialise because there are page cache pages and now we have to do buffered IO?
Direct IO should be a deterministic, zero-copy IO path to/from storage. Using the CPU to copy data during direct IO is the complete opposite of the intended functionality, not to mention the behaviour that many applications have been careful designed and tuned for.
Hence I think that forcing iomap to use cached pages for DIO is a non-starter. I have no problems with providing infrastructure that allows filesystems to -opt in- to using buffered IO for the direct IO path. However, the change in IO behaviour caused by unpredicably switching between direct IO and buffered IO (e.g. suddening DIO writes serialise -all IO-) will cause unacceptible performance regressions for many applications and be -very difficult to diagnose- in production systems.
IOWs, we need to let the individual filesystems decide how they want to use the page cache for direct IO. Just because we have new direct IO infrastructure (i.e. iomap) it does not mean we can just make wholesale changes to the direct IO path behaviour...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner david at fromorbit.com
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 17 lines · Dave Chinner
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Matthew Wilcox
Indeed, I'm in favour of not invalidating
the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.Something like that, yes.
So are we really willing to take the performance regression that occurs from copying out of the page cache consuming lots more CPU than an actual direct IO read? Or that direct IO writes suddenly serialise because there are page cache pages and now we have to do buffered IO?
Quoted 4 lines
Direct IO should be a deterministic, zero-copy IO path to/from storage. Using the CPU to copy data during direct IO is the complete opposite of the intended functionality, not to mention the behaviour that many applications have been careful designed and tuned for.
Direct I/O isn't deterministic though. If the file isn't shared, then it works great, but as soon as you get mixed buffered and direct I/O, everything is already terrible. Direct I/Os perform pagecache lookups already, but instead of using the data that we found in the cache, we (if it's dirty) write it back, wait for the write to complete, remove the page from the pagecache and then perform another I/O to get the data that we just wrote out! And then the app that's using buffered I/O has to read it back in again.
Nobody's proposing changing Direct I/O to exclusively work through the pagecache. The proposal is to behave less weirdly when there's already data in the pagecache.
I have had an objection raised off-list. In a scenario with a block device shared between two systems and an application which does direct I/O, everything is normally fine. If one of the systems uses tar to back up the contents of the block device then the application on that system will no longer see the writes from the other system because there's nothing to invalidate the pagecache on the first system.
Unfortunately, this is in direct conflict with the performance problem caused by some little arsewipe deciding to do:
$ while true; do dd if=/lib/x86_64-linux-gnu/libc-2.30.so iflag=direct of=/dev/null; done
... doesn't hurt me because my root filesystem is on ext4 which doesn't purge the cache. But anything using iomap gets all the pages for libc kicked out of the cache, and that's a lot of fun.
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 8 lines · Matthew Wilcox
Direct I/O isn't deterministic though. If the file isn't shared, then it works great, but as soon as you get mixed buffered and direct I/O, everything is already terrible. Direct I/Os perform pagecache lookups already, but instead of using the data that we found in the cache, we (if it's dirty) write it back, wait for the write to complete, remove the page from the pagecache and then perform another I/O to get the data that we just wrote out! And then the app that's using buffered I/O has to read it back in again.
Mostly agreed. That being said I suspect invalidating clean cache might still be a good idea. The original idea was mostly on how to deal with invalidation failures of any kind, but falling back for any kind of dirty cache also makes at least some sense.
Quoted 6 lines
I have had an objection raised off-list. In a scenario with a block device shared between two systems and an application which does direct I/O, everything is normally fine. If one of the systems uses tar to back up the contents of the block device then the application on that system will no longer see the writes from the other system because there's nothing to invalidate the pagecache on the first system.
Err, WTF? If someone access shared block devices with random applications all bets are off anyway.
Quoted 9 lines
Unfortunately, this is in direct conflict with the performance problem caused by some little arsewipe deciding to do:
$ while true; do dd if=/lib/x86_64-linux-gnu/libc-2.30.so iflag=direct of=/dev/null; done... doesn't hurt me because my root filesystem is on ext4 which doesn't purge the cache. But anything using iomap gets all the pages for libc kicked out of the cache, and that's a lot of fun.
ext4 uses iomap..
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Matthew Wilcox
Direct I/O isn't deterministic though. If the file isn't shared, then it works great, but as soon as you get mixed buffered and direct I/O, everything is already terrible. Direct I/Os perform pagecache lookups already, but instead of using the data that we found in the cache, we (if it's dirty) write it back, wait for the write to complete, remove the page from the pagecache and then perform another I/O to get the data that we just wrote out! And then the app that's using buffered I/O has to read it back in again.
Quoted 4 lines
Mostly agreed. That being said I suspect invalidating clean cache might still be a good idea. The original idea was mostly on how to deal with invalidation failures of any kind, but falling back for any kind of dirty cache also makes at least some sense.
That's certainly the btrfs problem that needs to be solved, but I think it's all part of the directio misdesign.
Quoted 9 lines
Quoted 6 lines
I have had an objection raised off-list. In a scenario with a block device shared between two systems and an application which does direct I/O, everything is normally fine. If one of the systems uses tar to back up the contents of the block device then the application on that system will no longer see the writes from the other system because there's nothing to invalidate the pagecache on the first system.
Err, WTF? If someone access shared block devices with random applications all bets are off anyway.
That doesn't mean that customers don't do it. It is, of course, not recommended, but we suspect people do it anyway. Because it does work, unfortunately. I'd be open to making this exact situation deterministically not work (eg disallowing mixing O_DIRECT and non-O_DIRECT openers of block devices), but making it suddenly non-deterministically give you old data is a non-starter.
Quoted 10 lines
Quoted 8 lines
Unfortunately, this is in direct conflict with the performance problem caused by some little arsewipe deciding to do:
$ while true; do dd if=/lib/x86_64-linux-gnu/libc-2.30.so iflag=direct of=/dev/null; done... doesn't hurt me because my root filesystem is on ext4 which doesn't purge the cache. But anything using iomap gets all the pages for libc kicked out of the cache, and that's a lot of fun.
ext4 uses iomap..
I happen to be running an older kernel that doesn't on this laptop ;-)
Re: [Cluster-devel] always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
In reply to Christoph HellwigHi,
Quoted 14 lines · Christoph Hellwig
Quoted 8 lines · Matthew Wilcox
Direct I/O isn't deterministic though. If the file isn't shared, then it works great, but as soon as you get mixed buffered and direct I/O, everything is already terrible. Direct I/Os perform pagecache lookups already, but instead of using the data that we found in the cache, we (if it's dirty) write it back, wait for the write to complete, remove the page from the pagecache and then perform another I/O to get the data that we just wrote out! And then the app that's using buffered I/O has to read it back in again.
Mostly agreed. That being said I suspect invalidating clean cache might still be a good idea. The original idea was mostly on how to deal with invalidation failures of any kind, but falling back for any kind of dirty cache also makes at least some sense.
Quoted 8 lines
Quoted 6 lines
I have had an objection raised off-list. In a scenario with a block device shared between two systems and an application which does direct I/O, everything is normally fine. If one of the systems uses tar to back up the contents of the block device then the application on that system will no longer see the writes from the other system because there's nothing to invalidate the pagecache on the first system.
Err, WTF? If someone access shared block devices with random applications all bets are off anyway.
On GFS2 the locking should take care of that. Not 100% sure about OCFS2 without looking, but I'm fairly sure that they have a similar arrangement. So this shouldn't be a problem unless there is an additional cluster fs that I'm not aware of that they are using in this case. It would be good to confirm which fs they are using,
Steve.
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
In reply to Matthew WilcoxQuoted 18 lines · Matthew Wilcox
Quoted 17 lines · Dave Chinner
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Matthew Wilcox
Indeed, I'm in favour of not invalidating
the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.Something like that, yes.
So are we really willing to take the performance regression that occurs from copying out of the page cache consuming lots more CPU than an actual direct IO read? Or that direct IO writes suddenly serialise because there are page cache pages and now we have to do buffered IO?
Quoted 6 lines
Quoted 4 lines
Direct IO should be a deterministic, zero-copy IO path to/from storage. Using the CPU to copy data during direct IO is the complete opposite of the intended functionality, not to mention the behaviour that many applications have been careful designed and tuned for.
Direct I/O isn't deterministic though.
When all the application is doing is direct IO, it is as deterministic as the underlying storage behaviour. This is the best we can possibly do from the perspective of the filesystem, and this is largely what Direct IO requires from the filesystem.
Direct IO starts from delegating all responsibility for IO synchronisation data coherency and integrity to userspace, and then follows up by requiring the filesystem and kernel to stay out of the IO path except where it is absolutely necessary to read or write data to/from the underlying storage hardware. Serving Direct IO from the page cache violates the second of these requirements.
Quoted 3 lines
If the file isn't shared, then
it works great, but as soon as you get mixed buffered and direct I/O, everything is already terrible.
Right, but that's *the rare exception* for applications using direct IO, not the common fast path. It is the slow path where -shit has already gone wrong on the production machine-, and it most definitely does not change the DIO requirements that the filesystem needs to provide userspace via the direct IO path.
Optimising the slow exception path is fine if it does not affect the guarantees we try to provide through the common/fast path. If it is does affect behaviour of the fast path, then we must be able to either turn it off or provide our own alternative implementation that does not have that cost.
Quoted 6 lines
Direct I/Os perform pagecache lookups
already, but instead of using the data that we found in the cache, we (if it's dirty) write it back, wait for the write to complete, remove the page from the pagecache and then perform another I/O to get the data that we just wrote out! And then the app that's using buffered I/O has to read it back in again.
Yup, that's because we have a history going back 20+ years of people finding performance regressions in applications using direct IO when we leave incorrectly left pages in the page cache rather than invalidating them and continuing to do direct IO.
Quoted 3 lines
Nobody's proposing changing Direct I/O to exclusively work through the pagecache. The proposal is to behave less weirdly when there's already data in the pagecache.
No, the proposal it to make direct IO behave *less deterministically* if there is data in the page cache.
e.g. Instead of having a predicatable submission CPU overhead and read latency of 100us for your data, this proposal makes the claim that it is always better to burn 10x the IO submission CPU for a single IO to copy the data and give that specific IO 10x lower latency than it is to submit 10 async IOs to keep the IO pipeline full.
What it fails to take into account is that in spending that CPU time to copy the data, we haven't submitted 10 other IOs and so the actual in-flight IO for the application has decreased. If performance comes from keeping the IO pipeline as close to 100% full as possible, then copying the data out of the page cache will cause performance regressions.
i.e. Hit 5 page cache pages in 5 IOs in a row, and the IO queue depth craters because we've only fulfilled 5 complete IOs instead of submitting 50 entire IOs. This is the hidden cost of synchronous IO via CPU data copying vs async IO via hardware offload, and if we take that into account we must look at future hardware performance trends to determine if this cost is going to increase or decrease in future.
That is: CPUs are not getting faster anytime soon. IO subsystems are still deep in the "performance doubles every 2 years" part of the technology curve (pcie 3.0->4.0 just happened, 4->5 is a year away, 5->6 is 3-4 years away, etc). Hence our reality is that we are deep within a performance trend curve that tells us synchronous CPU operations are not getting faster, but IO bandwidth and IOPS are going to increase massively over the next 5-10 years. Hence putting (already expensive) synchronous CPU operations in the asynchronous zero-data-touch IO fast path is -exactly the wrong direction to be moving-.
This is simple math. The gap between IO latency and bandwidth and CPU addressable memory latency and bandwidth is closing all the time, and the closer that gap gets the less sense it makes to use CPU addressable memory for buffering syscall based read and write IO. We are not quite yet at the cross-over point, but we really aren't that far from it.
Quoted 6 lines
I have had an objection raised off-list. In a scenario with a block device shared between two systems and an application which does direct I/O, everything is normally fine. If one of the systems uses tar to back up the contents of the block device then the application on that system will no longer see the writes from the other system because there's nothing to invalidate the pagecache on the first system.
I'm sorry you have to deal with that. :(
Back in the world of local filesystems, sharing block device across systems without using a clustered filesystem to maintain storage device level data coherency across those multiple machines is not supported in any way, shape or form, direct IO or not.
Quoted 4 lines
Unfortunately, this is in direct conflict with the performance problem caused by some little arsewipe deciding to do:
$ while true; do dd if=/lib/x86_64-linux-gnu/libc-2.30.so iflag=direct of=/dev/null; done
This has come up in the past, and I'm pretty sure I sent a patch to stop iomap from invalidating the cache on DIO reads because the same data is on disk as is in memory and it solves this whole problem. I cannot find that patch in the archives - I must have sent it inline to the discussion as I'm about to do again.
Yeah, now it comes back to me - the context was about using RWF_NOWAIT to detect page cache residency for page cache timing attacks and I mentioned doing exactly the above as a mechanism to demand trigger invalidation of the mapped page cache. The solution was to only invalidate the page cache on DIO writes, but we didn't do it straight away because I needed to audit the XFS code to determine if there were still real reasons it was necessary.
The patch is attached below. The DIO read path is unchanged except for the fact it skips the invalidation. i.e. the dio read still goes to disk, but we can leave the data in memory because it is the same as what was on disk. This does not perturb DIO behaviour by inserting synchronous page cache copies or exclusive inode locking into the async DIO path and so will not cause DIO performance regressions. However, it does allow mmap() and read() to be served from cache at the same time as we issue overlapping DIO reads.
Cheers,
Dave.
Signature · 56 lines
Dave Chinner david at fromorbit.com iomap: Only invalidate page cache pages on direct IO writes From: Dave Chinner <[email protected]> The historic requirement for XFS to invalidate cached pages on direct IO reads has been lost in the twisty pages of history - it was inherited from Irix, which implemented page cache invalidation on read as a method of working around problems synchronising page cache state with uncached IO. XFS has carried this ever since. In the initial linux ports it was necessary to get mmap and DIO to play "ok" together and not immediately corrupt data. This was the state of play until the linux kernel had infrastructure to track unwritten extents and synchronise page faults with allocations and unwritten extent conversions (->page_mkwrite infrastructure). IOws, the page cache invalidation on DIO read was necessary to prevent trivial data corruptions. This didn't solve all the problems, though. There were peformance problems if we didn't invalidate the entire page cache over the file on read - we couldn't easily determine if the cached pages were over the range of the IO, and invalidation required taking a serialising lock (i_mutex) on the inode. This serialising lock was an issue for XFS, as it was the only exclusive lock in the direct Io read path. Hence if there were any cached pages, we'd just invalidate the entire file in one go so that subsequent IOs didn't need to take the serialising lock. This was a problem that prevented ranged invalidation from being particularly useful for avoiding the remaining coherency issues. This was solved with the conversion of i_mutex to i_rwsem and the conversion of the XFS inode IO lock to use i_rwsem. Hence we could now just do ranged invalidation and the performance problem went away. However, page cache invalidation was still needed to serialise sub-page/sub-block zeroing via direct IO against buffered IO because bufferhead state attached to the cached page could get out of whack when direct IOs were issued. We've removed bufferheads from the XFS code, and we don't carry any extent state on the cached pages anymore, and so this problem has gone away, too. IOWs, it would appear that we don't have any good reason to be invalidating the page cache on DIO reads anymore. Hence remove the invalidation on read because it is unnecessary overhead, not needed to maintain coherency between mmap/buffered access and direct IO anymore, and prevents anyone from using direct IO reads from intentionally invalidating the page cache of a file. Signed-off-by: Dave Chinner <[email protected]> --- fs/iomap/direct-io.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
fs/iomap/direct-io.c +17 -16
@@ -475,23 +475,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,if (ret) goto out_free_dio; -/* -* Try to invalidate cache pages for the range we're direct -* writing. If this invalidation fails, tough, the write will -* still work, but racing two incompatible write paths is a -* pretty crazy thing to do, so we don't support it 100%. -*/ - ret =invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) -dio_warn_stale_pagecache (iocb->ki_filp); - ret =0 ; +if (iov_iter_rw (iter) ==WRITE ) { +/* +* Try to invalidate cache pages for the range we're direct +* writing. If this invalidation fails, tough, the write will +* still work, but racing two incompatible write paths is a +* pretty crazy thing to do, so we don't support it 100%. +*/ + ret =invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +if (ret) +dio_warn_stale_pagecache (iocb->ki_filp); + ret =0 ; -if (iov_iter_rw (iter) ==WRITE && !wait_for_completion && - !inode->i_sb->s_dio_done_wq) { - ret =sb_init_dio_done_wq (inode->i_sb); -if (ret <0 ) - goto out_free_dio; +if (!wait_for_completion && + !inode->i_sb->s_dio_done_wq) { + ret =sb_init_dio_done_wq (inode->i_sb); +if (ret <0 ) + goto out_free_dio; }inode_dio_begin (inode);
Darrick J. Wong
Reviewed-byRe: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 131 lines · Dave Chinner
Quoted 18 lines · Matthew Wilcox
Quoted 17 lines · Dave Chinner
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Matthew Wilcox
Indeed, I'm in favour of not invalidating
the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.Something like that, yes.
So are we really willing to take the performance regression that occurs from copying out of the page cache consuming lots more CPU than an actual direct IO read? Or that direct IO writes suddenly serialise because there are page cache pages and now we have to do buffered IO?
Quoted 6 lines
Quoted 4 lines
Direct IO should be a deterministic, zero-copy IO path to/from storage. Using the CPU to copy data during direct IO is the complete opposite of the intended functionality, not to mention the behaviour that many applications have been careful designed and tuned for.
Direct I/O isn't deterministic though.
When all the application is doing is direct IO, it is as deterministic as the underlying storage behaviour. This is the best we can possibly do from the perspective of the filesystem, and this is largely what Direct IO requires from the filesystem.
Direct IO starts from delegating all responsibility for IO synchronisation data coherency and integrity to userspace, and then follows up by requiring the filesystem and kernel to stay out of the IO path except where it is absolutely necessary to read or write data to/from the underlying storage hardware. Serving Direct IO from the page cache violates the second of these requirements.
Quoted 3 lines
If the file isn't shared, then
it works great, but as soon as you get mixed buffered and direct I/O, everything is already terrible.Right, but that's *the rare exception* for applications using direct IO, not the common fast path. It is the slow path where -shit has already gone wrong on the production machine-, and it most definitely does not change the DIO requirements that the filesystem needs to provide userspace via the direct IO path.
Optimising the slow exception path is fine if it does not affect the guarantees we try to provide through the common/fast path. If it is does affect behaviour of the fast path, then we must be able to either turn it off or provide our own alternative implementation that does not have that cost.
Quoted 6 lines
Direct I/Os perform pagecache lookups
already, but instead of using the data that we found in the cache, we (if it's dirty) write it back, wait for the write to complete, remove the page from the pagecache and then perform another I/O to get the data that we just wrote out! And then the app that's using buffered I/O has to read it back in again.Yup, that's because we have a history going back 20+ years of people finding performance regressions in applications using direct IO when we leave incorrectly left pages in the page cache rather than invalidating them and continuing to do direct IO.
Quoted 3 lines
Nobody's proposing changing Direct I/O to exclusively work through the pagecache. The proposal is to behave less weirdly when there's already data in the pagecache.
No, the proposal it to make direct IO behave *less deterministically* if there is data in the page cache.
e.g. Instead of having a predicatable submission CPU overhead and read latency of 100us for your data, this proposal makes the claim that it is always better to burn 10x the IO submission CPU for a single IO to copy the data and give that specific IO 10x lower latency than it is to submit 10 async IOs to keep the IO pipeline full.
What it fails to take into account is that in spending that CPU time to copy the data, we haven't submitted 10 other IOs and so the actual in-flight IO for the application has decreased. If performance comes from keeping the IO pipeline as close to 100% full as possible, then copying the data out of the page cache will cause performance regressions.
i.e. Hit 5 page cache pages in 5 IOs in a row, and the IO queue depth craters because we've only fulfilled 5 complete IOs instead of submitting 50 entire IOs. This is the hidden cost of synchronous IO via CPU data copying vs async IO via hardware offload, and if we take that into account we must look at future hardware performance trends to determine if this cost is going to increase or decrease in future.
That is: CPUs are not getting faster anytime soon. IO subsystems are still deep in the "performance doubles every 2 years" part of the technology curve (pcie 3.0->4.0 just happened, 4->5 is a year away, 5->6 is 3-4 years away, etc). Hence our reality is that we are deep within a performance trend curve that tells us synchronous CPU operations are not getting faster, but IO bandwidth and IOPS are going to increase massively over the next 5-10 years. Hence putting (already expensive) synchronous CPU operations in the asynchronous zero-data-touch IO fast path is -exactly the wrong direction to be moving-.
This is simple math. The gap between IO latency and bandwidth and CPU addressable memory latency and bandwidth is closing all the time, and the closer that gap gets the less sense it makes to use CPU addressable memory for buffering syscall based read and write IO. We are not quite yet at the cross-over point, but we really aren't that far from it.
Quoted 6 lines
I have had an objection raised off-list. In a scenario with a block device shared between two systems and an application which does direct I/O, everything is normally fine. If one of the systems uses tar to back up the contents of the block device then the application on that system will no longer see the writes from the other system because there's nothing to invalidate the pagecache on the first system.
I'm sorry you have to deal with that. :(
Back in the world of local filesystems, sharing block device across systems without using a clustered filesystem to maintain storage device level data coherency across those multiple machines is not supported in any way, shape or form, direct IO or not.
Quoted 3 lines
Unfortunately, this is in direct conflict with the performance problem caused by some little arsewipe deciding to do:
Quoted 7 lines
Quoted 1 line
$ while true; do dd if=/lib/x86_64-linux-gnu/libc-2.30.so iflag=direct of=/dev/null; doneThis has come up in the past, and I'm pretty sure I sent a patch to stop iomap from invalidating the cache on DIO reads because the same data is on disk as is in memory and it solves this whole problem. I cannot find that patch in the archives - I must have sent it inline to the discussion as I'm about to do again.
And, um, it'll get buried in the archives again. :(
Though I guess I could just suck it in and see what happens, and maybe "the mists of time" won't be an issue this time around. Particularly since I was the one asking you about this very hunk of code last night on irc. :P
Oh well, onto the review...
Quoted 17 lines
Yeah, now it comes back to me - the context was about using
RWF_NOWAITto detect page cache residency for page cache timing attacks and I mentioned doing exactly the above as a mechanism to demand trigger invalidation of the mapped page cache. The solution was to only invalidate the page cache on DIO writes, but we didn't do it straight away because I needed to audit the XFS code to determine if there were still real reasons it was necessary.The patch is attached below. The DIO read path is unchanged except for the fact it skips the invalidation. i.e. the dio read still goes to disk, but we can leave the data in memory because it is the same as what was on disk. This does not perturb DIO behaviour by inserting synchronous page cache copies or exclusive inode locking into the async DIO path and so will not cause DIO performance regressions. However, it does allow
mmap()andread()to be served from cache at the same time as we issue overlapping DIO reads.
Quoted 16 lines
Cheers,
Dave.
Signature · 12 lines
Dave Chinner david at fromorbit.com iomap: Only invalidate page cache pages on direct IO writes From: Dave Chinner <[email protected]> The historic requirement for XFS to invalidate cached pages on direct IO reads has been lost in the twisty pages of history - it was inherited from Irix, which implemented page cache invalidation on read as a method of working around problems synchronising page cache state with uncached IO.
Urk.
Quoted 49 lines
XFS has carried this ever since. In the initial linux ports it was necessary to get mmap and DIO to play "ok" together and not immediately corrupt data. This was the state of play until the linux kernel had infrastructure to track unwritten extents and synchronise page faults with allocations and unwritten extent conversions (->page_mkwrite infrastructure). IOws, the page cache invalidation on DIO read was necessary to prevent trivial data corruptions. This didn't solve all the problems, though.
There were peformance problems if we didn't invalidate the entire page cache over the file on read - we couldn't easily determine if the cached pages were over the range of the IO, and invalidation required taking a serialising lock (
i_mutex) on the inode. This serialising lock was an issue for XFS, as it was the only exclusive lock in the direct Io read path.Hence if there were any cached pages, we'd just invalidate the entire file in one go so that subsequent IOs didn't need to take the serialising lock. This was a problem that prevented ranged invalidation from being particularly useful for avoiding the remaining coherency issues. This was solved with the conversion of
i_mutextoi_rwsemand the conversion of the XFS inode IO lock to usei_rwsem. Hence we could now just do ranged invalidation and the performance problem went away.However, page cache invalidation was still needed to serialise sub-page/sub-block zeroing via direct IO against buffered IO because bufferhead state attached to the cached page could get out of whack when direct IOs were issued. We've removed bufferheads from the XFS code, and we don't carry any extent state on the cached pages anymore, and so this problem has gone away, too.
IOWs, it would appear that we don't have any good reason to be invalidating the page cache on DIO reads anymore. Hence remove the invalidation on read because it is unnecessary overhead, not needed to maintain coherency between mmap/buffered access and direct IO anymore, and prevents anyone from using direct IO reads from intentionally invalidating the page cache of a file.
Signed-off-by Dave Chinner <[email protected]>--- fs/iomap/direct-io.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)fs/iomap/direct-io.c +0
-0
fs/iomap/direct-io.c iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 482
Quoted 8 lines
fs/iomap/direct-io.c +0
-5@@ -475,23 +475,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,if (ret) goto out_free_dio; -/* -* Try to invalidate cache pages for the range we're direct -* writing. If this invalidation fails, tough, the write will -* still work, but racing two incompatible write paths is a -* pretty crazy thing to do, so we don't support it 100%.
I always wondered about the repeated use of 'write' in this comment despite the lack of any sort of WRITE check logic. Seems fine to me, let's throw it on the fstests pile and see what happens.
--D
Quoted 32 lines
fs/iomap/direct-io.c +17
-11-*/ - ret =invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) -dio_warn_stale_pagecache (iocb->ki_filp); - ret =0 ; +if (iov_iter_rw (iter) ==WRITE ) { +/* +* Try to invalidate cache pages for the range we're direct +* writing. If this invalidation fails, tough, the write will +* still work, but racing two incompatible write paths is a +* pretty crazy thing to do, so we don't support it 100%. +*/ + ret =invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +if (ret) +dio_warn_stale_pagecache (iocb->ki_filp); + ret =0 ; -if (iov_iter_rw (iter) ==WRITE && !wait_for_completion && - !inode->i_sb->s_dio_done_wq) { - ret =sb_init_dio_done_wq (inode->i_sb); -if (ret <0 ) - goto out_free_dio; +if (!wait_for_completion && + !inode->i_sb->s_dio_done_wq) { + ret =sb_init_dio_done_wq (inode->i_sb); +if (ret <0 ) + goto out_free_dio; }inode_dio_begin (inode);
Matthew Wilcox
Reviewed-byRe: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 75 lines · Darrick J. Wong
Quoted 9 lines · Dave Chinner
iomap: Only invalidate page cache pages on direct IO writes
From: Dave Chinner <[email protected]>
The historic requirement for XFS to invalidate cached pages on direct IO reads has been lost in the twisty pages of history - it was inherited from Irix, which implemented page cache invalidation on read as a method of working around problems synchronising page cache state with uncached IO.
Urk.
Quoted 49 lines
XFS has carried this ever since. In the initial linux ports it was necessary to get mmap and DIO to play "ok" together and not immediately corrupt data. This was the state of play until the linux kernel had infrastructure to track unwritten extents and synchronise page faults with allocations and unwritten extent conversions (->page_mkwrite infrastructure). IOws, the page cache invalidation on DIO read was necessary to prevent trivial data corruptions. This didn't solve all the problems, though.
There were peformance problems if we didn't invalidate the entire page cache over the file on read - we couldn't easily determine if the cached pages were over the range of the IO, and invalidation required taking a serialising lock (
i_mutex) on the inode. This serialising lock was an issue for XFS, as it was the only exclusive lock in the direct Io read path.Hence if there were any cached pages, we'd just invalidate the entire file in one go so that subsequent IOs didn't need to take the serialising lock. This was a problem that prevented ranged invalidation from being particularly useful for avoiding the remaining coherency issues. This was solved with the conversion of
i_mutextoi_rwsemand the conversion of the XFS inode IO lock to usei_rwsem. Hence we could now just do ranged invalidation and the performance problem went away.However, page cache invalidation was still needed to serialise sub-page/sub-block zeroing via direct IO against buffered IO because bufferhead state attached to the cached page could get out of whack when direct IOs were issued. We've removed bufferheads from the XFS code, and we don't carry any extent state on the cached pages anymore, and so this problem has gone away, too.
IOWs, it would appear that we don't have any good reason to be invalidating the page cache on DIO reads anymore. Hence remove the invalidation on read because it is unnecessary overhead, not needed to maintain coherency between mmap/buffered access and direct IO anymore, and prevents anyone from using direct IO reads from intentionally invalidating the page cache of a file.
Signed-off-by Dave Chinner <[email protected]>--- fs/iomap/direct-io.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)fs/iomap/direct-io.c +0
-0fs/iomap/direct-io.c iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 482
Quoted 8 lines
fs/iomap/direct-io.c +0
-5@@ -475,23 +475,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,if (ret) goto out_free_dio; -/* -* Try to invalidate cache pages for the range we're direct -* writing. If this invalidation fails, tough, the write will -* still work, but racing two incompatible write paths is a -* pretty crazy thing to do, so we don't support it 100%. I always wondered about the repeated use of 'write' in this comment despite the lack of any sort of WRITE check logic. Seems fine to me, let's throw it on the fstests pile and see what happens.
Quoted 1 line
Reviewed-by Darrick J. Wong <[email protected]>
Quoted 34 lines
--D
Quoted 32 lines
fs/iomap/direct-io.c +17
-11-*/ - ret =invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) -dio_warn_stale_pagecache (iocb->ki_filp); - ret =0 ; +if (iov_iter_rw (iter) ==WRITE ) { +/* +* Try to invalidate cache pages for the range we're direct +* writing. If this invalidation fails, tough, the write will +* still work, but racing two incompatible write paths is a +* pretty crazy thing to do, so we don't support it 100%. +*/ + ret =invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +if (ret) +dio_warn_stale_pagecache (iocb->ki_filp); + ret =0 ; -if (iov_iter_rw (iter) ==WRITE && !wait_for_completion && - !inode->i_sb->s_dio_done_wq) { - ret =sb_init_dio_done_wq (inode->i_sb); -if (ret <0 ) - goto out_free_dio; +if (!wait_for_completion && + !inode->i_sb->s_dio_done_wq) { + ret =sb_init_dio_done_wq (inode->i_sb); +if (ret <0 ) + goto out_free_dio; }inode_dio_begin (inode);
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 101 lines · Matthew Wilcox
Quoted 75 lines · Darrick J. Wong
Quoted 9 lines · Dave Chinner
iomap: Only invalidate page cache pages on direct IO writes
From: Dave Chinner <[email protected]>
The historic requirement for XFS to invalidate cached pages on direct IO reads has been lost in the twisty pages of history - it was inherited from Irix, which implemented page cache invalidation on read as a method of working around problems synchronising page cache state with uncached IO.
Urk.
Quoted 49 lines
XFS has carried this ever since. In the initial linux ports it was necessary to get mmap and DIO to play "ok" together and not immediately corrupt data. This was the state of play until the linux kernel had infrastructure to track unwritten extents and synchronise page faults with allocations and unwritten extent conversions (->page_mkwrite infrastructure). IOws, the page cache invalidation on DIO read was necessary to prevent trivial data corruptions. This didn't solve all the problems, though.
There were peformance problems if we didn't invalidate the entire page cache over the file on read - we couldn't easily determine if the cached pages were over the range of the IO, and invalidation required taking a serialising lock (
i_mutex) on the inode. This serialising lock was an issue for XFS, as it was the only exclusive lock in the direct Io read path.Hence if there were any cached pages, we'd just invalidate the entire file in one go so that subsequent IOs didn't need to take the serialising lock. This was a problem that prevented ranged invalidation from being particularly useful for avoiding the remaining coherency issues. This was solved with the conversion of
i_mutextoi_rwsemand the conversion of the XFS inode IO lock to usei_rwsem. Hence we could now just do ranged invalidation and the performance problem went away.However, page cache invalidation was still needed to serialise sub-page/sub-block zeroing via direct IO against buffered IO because bufferhead state attached to the cached page could get out of whack when direct IOs were issued. We've removed bufferheads from the XFS code, and we don't carry any extent state on the cached pages anymore, and so this problem has gone away, too.
IOWs, it would appear that we don't have any good reason to be invalidating the page cache on DIO reads anymore. Hence remove the invalidation on read because it is unnecessary overhead, not needed to maintain coherency between mmap/buffered access and direct IO anymore, and prevents anyone from using direct IO reads from intentionally invalidating the page cache of a file.
Signed-off-by Dave Chinner <[email protected]>--- fs/iomap/direct-io.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)fs/iomap/direct-io.c +0
-0fs/iomap/direct-io.c iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 482
Quoted 8 lines
fs/iomap/direct-io.c +0
-5@@ -475,23 +475,24 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,if (ret) goto out_free_dio; -/* -* Try to invalidate cache pages for the range we're direct -* writing. If this invalidation fails, tough, the write will -* still work, but racing two incompatible write paths is a -* pretty crazy thing to do, so we don't support it 100%. I always wondered about the repeated use of 'write' in this comment despite the lack of any sort of WRITE check logic. Seems fine to me, let's throw it on the fstests pile and see what happens.
Quoted 1 line
Reviewed-by Darrick J. Wong <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>Quoted 21 lines
--D
Quoted 19 lines
fs/iomap/direct-io.c +12
-6-*/ - ret =invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) -dio_warn_stale_pagecache (iocb->ki_filp); - ret =0 ; +if (iov_iter_rw (iter) ==WRITE ) { +/* +* Try to invalidate cache pages for the range we're direct +* writing. If this invalidation fails, tough, the write will +* still work, but racing two incompatible write paths is a +* pretty crazy thing to do, so we don't support it 100%. +*/ + ret =invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +if (ret) +dio_warn_stale_pagecache (iocb->ki_filp); + ret =0 ;
Quoted 10 lines
Quoted 10 lines
fs/iomap/direct-io.c iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 495
Quoted 10 lines
fs/iomap/direct-io.c +5
-5-if (iov_iter_rw (iter) ==WRITE && !wait_for_completion && - !inode->i_sb->s_dio_done_wq) { - ret =sb_init_dio_done_wq (inode->i_sb); -if (ret <0 ) - goto out_free_dio; +if (!wait_for_completion && + !inode->i_sb->s_dio_done_wq) { + ret =sb_init_dio_done_wq (inode->i_sb); +if (ret <0 ) + goto out_free_dio;
...and yes I did add in the closing brace here. :P
--D
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
fs/iomap/direct-io.c +0
-0}inode_dio_begin (inode);
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 22 lines · Darrick J. Wong
Quoted 21 lines · Matthew Wilcox
Quoted 20 lines · Darrick J. Wong
Quoted 19 lines · Dave Chinner
fs/iomap/direct-io.c +12
-6-*/ - ret =invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); -if (ret) -dio_warn_stale_pagecache (iocb->ki_filp); - ret =0 ; +if (iov_iter_rw (iter) ==WRITE ) { +/* +* Try to invalidate cache pages for the range we're direct +* writing. If this invalidation fails, tough, the write will +* still work, but racing two incompatible write paths is a +* pretty crazy thing to do, so we don't support it 100%. +*/ + ret =invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end >>PAGE_SHIFT ); +if (ret) +dio_warn_stale_pagecache (iocb->ki_filp); + ret =0 ;
Quoted 12 lines
Quoted 10 lines
Quoted 10 lines
fs/iomap/direct-io.c iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 495
Quoted 10 lines
fs/iomap/direct-io.c +5
-5-if (iov_iter_rw (iter) ==WRITE && !wait_for_completion && - !inode->i_sb->s_dio_done_wq) { - ret =sb_init_dio_done_wq (inode->i_sb); -if (ret <0 ) - goto out_free_dio; +if (!wait_for_completion && + !inode->i_sb->s_dio_done_wq) { + ret =sb_init_dio_done_wq (inode->i_sb); +if (ret <0 ) + goto out_free_dio;...and yes I did add in the closing brace here. :P
Doh! I forgot to refresh the patch after fixing that. :/
Thanks!
Cheers,
Dave.
Signature · 2 lines
Dave Chinner david at fromorbit.com
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
This looks sane - slightly updated version below to not bother with the ret and a few tidyups.
That being said and to get back to the discussion in this thread:
I think it would be saner to give up on direct I/O in case of the invalidation failure. I've cooked up a patch on top of this one (for which I had a few trivial cleanups). It is still under testing, but I'll send the two out in a new thread.
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
In reply to Dave ChinnerQuoted 40 lines · Dave Chinner
Quoted 4 lines
Nobody's proposing changing Direct I/O to exclusively work through the pagecache. The proposal is to behave less weirdly when there's already data in the pagecache.
No, the proposal it to make direct IO behave *less deterministically* if there is data in the page cache.
e.g. Instead of having a predicatable submission CPU overhead and read latency of 100us for your data, this proposal makes the claim that it is always better to burn 10x the IO submission CPU for a single IO to copy the data and give that specific IO 10x lower latency than it is to submit 10 async IOs to keep the IO pipeline full.
What it fails to take into account is that in spending that CPU time to copy the data, we haven't submitted 10 other IOs and so the actual in-flight IO for the application has decreased. If performance comes from keeping the IO pipeline as close to 100% full as possible, then copying the data out of the page cache will cause performance regressions.
i.e. Hit 5 page cache pages in 5 IOs in a row, and the IO queue depth craters because we've only fulfilled 5 complete IOs instead of submitting 50 entire IOs. This is the hidden cost of synchronous IO via CPU data copying vs async IO via hardware offload, and if we take that into account we must look at future hardware performance trends to determine if this cost is going to increase or decrease in future.
That is: CPUs are not getting faster anytime soon. IO subsystems are still deep in the "performance doubles every 2 years" part of the technology curve (pcie 3.0->4.0 just happened, 4->5 is a year away, 5->6 is 3-4 years away, etc). Hence our reality is that we are deep within a performance trend curve that tells us synchronous CPU operations are not getting faster, but IO bandwidth and IOPS are going to increase massively over the next 5-10 years. Hence putting (already expensive) synchronous CPU operations in the asynchronous zero-data-touch IO fast path is -exactly the wrong direction to be moving-.
Quoted 8 lines
This is simple math. The gap between IO latency and bandwidth and CPU addressable memory latency and bandwidth is closing all the time, and the closer that gap gets the less sense it makes to use CPU addressable memory for buffering syscall based read and write IO. We are not quite yet at the cross-over point, but we really aren't that far from it.
My use-case supports this. The application uses AIO+DIO, but backup may bring pages into page cache. For me, it is best to ignore page cache (as long as it's clean, which it is for backup) and serve from disk as usual.
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
In reply to Matthew WilcoxQuoted 21 lines · Matthew Wilcox
Quoted 20 lines · Goldwyn Rodrigues
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>Quoted 6 lines
I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Adding all the relevant lists.
Since no one responded so far, let me see if I can stir the cauldron :)
Quoted 7 lines
Quoted 1 line
What error should be returned in case of such an error? I think the
Christoph's message is ambiguous. I don't know if he means "fail the I/O with an error" or "satisfy the I/O through the page cache". I'm strongly in favour of the latter. Indeed, I'm in favour of not invalidating the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently
That indeed would make reads faster. How about if the pages are dirty during DIO reads?
Should a direct I/O read be responsible for making sure that the dirty pages are written back. Technically direct I/O reads is that we are reading from the device.
Quoted 3 lines
cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.
Yes, that makes sense.
If this is implemented, what would be the difference between O_DIRECT and O_DSYNC, if any?
Signature · 1 line
Goldwyn
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 30 lines · Goldwyn Rodrigues
Quoted 21 lines · Matthew Wilcox
Quoted 20 lines · Goldwyn Rodrigues
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>Quoted 6 lines
I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Adding all the relevant lists.
Since no one responded so far, let me see if I can stir the cauldron :)
Quoted 7 lines
Quoted 1 line
What error should be returned in case of such an error? I think the
Christoph's message is ambiguous. I don't know if he means "fail the I/O with an error" or "satisfy the I/O through the page cache". I'm strongly in favour of the latter. Indeed, I'm in favour of not invalidating the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently
Quoted 5 lines
That indeed would make reads faster. How about if the pages are dirty during DIO reads?
Should a direct I/O read be responsible for making sure that the dirty pages are written back. Technically direct I/O reads is that we are reading from the device.
The filemap_write_and_wait_range should persist that data, right?
Quoted 7 lines
Quoted 3 lines
cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.
Yes, that makes sense.
If this is implemented, what would be the difference betweenO_DIRECTandO_DSYNC, if any?
Presumably a direct write would proceed as it does today if there's no pagecache at all?
--D
Quoted 2 lines
Signature · 1 line
Goldwyn
Re: always fall back to buffered I/O after invalidation failures, was: Re: [PATCH 2/6] iomap: IOMAP_DIO_RWF_NO_STALE_PAGECACHE return if page invalidation fails
Quoted 31 lines · Darrick J. Wong
Quoted 30 lines · Goldwyn Rodrigues
Quoted 28 lines · Matthew Wilcox
Quoted 16 lines · Goldwyn Rodrigues
Quoted 15 lines · Christoph Hellwig
Quoted 8 lines · Goldwyn Rodrigues
From: Goldwyn Rodrigues <[email protected]>
For direct I/O, add the flag
IOMAP_DIO_RWF_NO_STALE_PAGECACHEto indicate that if the page invalidation fails, return back control to the filesystem so it may fallback to buffered mode.Reviewed-by Darrick J. Wong <[email protected]>Signed-off-by Goldwyn Rodrigues <[email protected]>I'd like to start a discussion of this shouldn't really be the default behavior. If we have page cache that can't be invalidated it actually makes a whole lot of sense to not do direct I/O, avoid the warnings, etc.
Quoted 5 lines
Quoted 1 line
Adding all the relevant lists.
Since no one responded so far, let me see if I can stir the cauldron :)
What error should be returned in case of such an error? I think the
Christoph's message is ambiguous. I don't know if he means "fail the I/O with an error" or "satisfy the I/O through the page cache". I'm strongly in favour of the latter. Indeed, I'm in favour of not invalidating the page cache at all for direct I/O. For reads, I think the page cache should be used to satisfy any portion of the read which is currently
Quoted 7 lines
Quoted 5 lines
That indeed would make reads faster. How about if the pages are dirty during DIO reads?
Should a direct I/O read be responsible for making sure that the dirty pages are written back. Technically direct I/O reads is that we are reading from the device.The
filemap_write_and_wait_rangeshould persist that data, right?
Right. filemap_write_and_wait_range() would not make sense for writes though.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
cached. For writes, I think we should write into the page cache pages which currently exist, and then force those pages to be written back, but left in cache.
Quoted 7 lines
Quoted 3 lines
Yes, that makes sense.
If this is implemented, what would be the difference betweenO_DIRECTandO_DSYNC, if any?Presumably a direct write would proceed as it does today if there's no pagecache at all?
Yes, correct. Just that it would leave pages in the cache instead of invalidating it after DIO write is complete.
Signature · 1 line
Goldwyn
PATCH v10 3/6 btrfs: switch to iomap_dio_rw() for dio
In reply to Goldwyn RodriguesFrom: Goldwyn Rodrigues <[email protected]>
Switch from __blockdev_direct_IO() to iomap_dio_rw(). Rename btrfs_get_blocks_direct() to btrfs_dio_iomap_begin() and use it as iomap_begin() for iomap direct I/O functions. This function allocates and locks all the blocks required for the I/O. btrfs_submit_direct() is used as the submit_io() hook for direct I/O ops.
Since we need direct I/O reads to go through iomap_dio_rw(), we change file_operations.read_iter() to a btrfs_file_read_iter() which calls btrfs_direct_IO() for direct reads and falls back to generic_file_buffered_read() for incomplete reads and buffered reads.
We don't need address_space.direct_IO() anymore so set it to noop. Similarly, we don't need flags used in __blockdev_direct_IO(). iomap is capable of direct I/O reads from a hole, so we don't need to return -ENOENT.
BTRFS direct I/O is now done under i_rwsem, shared in case of reads and exclusive in case of writes. This guards against simultaneous truncates.
Use iomap->iomap_end() to check for failed or incomplete direct I/O:
- for writes, call __endio_write_update_ordered()
- for reads, unlock extents
btrfs_dio_data is now hooked in iomap->private and not current->journal_info. It carries the reservation variable and the amount of data submitted, so we can calculate the amount of data to call __endio_write_update_ordered in case of an error.
This patch removes last use of struct buffer_head from btrfs.
--- fs/btrfs/Kconfig | 1 + fs/btrfs/ctree.h | 1 + fs/btrfs/file.c | 21 +++- fs/btrfs/inode.c | 317 ++++++++++++++++++++++------------------------- 4 files changed, 170 insertions(+), 170 deletions(-)
fs/btrfs/Kconfig +1 -0
@@ -14,6 +14,7 @@ config BTRFS_FS select LZO_DECOMPRESS select ZSTD_COMPRESS select ZSTD_DECOMPRESS + select FS_IOMAP select RAID6_PQ select XOR_BLOCKS select SRCU
fs/btrfs/ctree.h +1 -0
@@ -2935,6 +2935,7 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end);void btrfs_writepage_endio_finish_ordered (struct page *page,u64 start,u64 end,int uptodate);extern const struct dentry_operations btrfs_dentry_operations; +ssize_t btrfs_direct_IO (struct kiocb *iocb,struct iov_iter *iter);/* ioctl.c */ long btrfs_ioctl (struct file *file,unsigned int cmd,unsigned long arg);
fs/btrfs/file.c +19 -2
@@ -1835,7 +1835,7 @@ static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)loff_t endbyte;int err; - written =generic_file_direct_write (iocb, from); + written =btrfs_direct_IO (iocb, from);if (written <0 || !iov_iter_count (from))return written;
@@ -3504,9 +3504,26 @@ static int btrfs_file_open(struct inode *inode, struct file *filp)return generic_file_open (inode, filp); } +static ssize_t btrfs_file_read_iter (struct kiocb *iocb,struct iov_iter *to) +{ +ssize_t ret =0 ; + +if (iocb->ki_flags &IOCB_DIRECT ) { +struct inode *inode =file_inode (iocb->ki_filp); + +inode_lock_shared (inode); + ret =btrfs_direct_IO (iocb, to); +inode_unlock_shared (inode); +if (ret <0 ) +return ret; + } + +return generic_file_buffered_read (iocb, to, ret); +} +const struct file_operations btrfs_file_operations = { .llseek = btrfs_file_llseek, - .read_iter = generic_file_read_iter, + .read_iter = btrfs_file_read_iter, .splice_read = generic_file_splice_read, .write_iter = btrfs_file_write_iter, .mmap = btrfs_file_mmap,
fs/btrfs/inode.c +149 -168
495 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH v10 4/6 fs: remove dio_end_io()
In reply to Goldwyn RodriguesFrom: Goldwyn Rodrigues <[email protected]>
Since we removed the last user of dio_end_io(), remove the helper function dio_end_io().
--- fs/direct-io.c | 19 ------------------- include/linux/fs.h | 2 -- 2 files changed, 21 deletions(-)
fs/direct-io.c +0 -19
@@ -386,25 +386,6 @@ static void dio_bio_end_io(struct bio *bio)spin_unlock_irqrestore (&dio->bio_lock, flags); } -/** -* dio_end_io - handle the end io action for the given bio -* @bio: The direct io bio thats being completed -* -* This is meant to be called by any filesystem that uses their own dio_submit_t -* so that the DIO specific endio actions are dealt with after the filesystem -* has done it's completion work. -*/ -void dio_end_io (struct bio *bio) -{ -struct dio *dio = bio->bi_private; - -if (dio->is_async) -dio_bio_end_aio (bio); -else -dio_bio_end_io (bio); -} -EXPORT_SYMBOL_GPL (dio_end_io); - static inline void dio_bio_alloc(struct dio *dio, struct dio_submit *sdio, struct block_device *bdev,
include/linux/fs.h +0 -2
@@ -3202,8 +3202,6 @@ enum { DIO_SKIP_HOLES = 0x02, }; -void dio_end_io (struct bio *bio); - ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode, struct block_device *bdev, struct iov_iter *iter, get_block_t get_block,
Signature · 1 line
2.26.2
PATCH v10 5/6 btrfs: remove BTRFS_INODE_READDIO_NEED_LOCK
In reply to Goldwyn RodriguesFrom: Goldwyn Rodrigues <[email protected]>
Since we now perform direct reads using i_rwsem, we can remove this inode flag used to co-ordinate unlocked reads.
The truncate call takes i_rwsem. This means it is correctly synchronized with concurrent direct reads.
--- fs/btrfs/btrfs_inode.h | 18 ------------------ fs/btrfs/inode.c | 3 --- 2 files changed, 21 deletions(-)
fs/btrfs/btrfs_inode.h +0 -18
@@ -28,7 +28,6 @@ enum {BTRFS_INODE_NEEDS_FULL_SYNC ,BTRFS_INODE_COPY_EVERYTHING ,BTRFS_INODE_IN_DELALLOC_LIST , -BTRFS_INODE_READDIO_NEED_LOCK ,BTRFS_INODE_HAS_PROPS , BTRFS_INODE_SNAPSHOT_FLUSH, };
@@ -313,23 +312,6 @@ struct btrfs_dio_private {u8 csums[]; }; -/* -* Disable DIO read nolock optimization, so new dio readers will be forced -* to grab i_mutex. It is used to avoid the endless truncate due to -* nonlocked dio read. -*/ -static inline void btrfs_inode_block_unlocked_dio (struct btrfs_inode *inode) -{ -set_bit (BTRFS_INODE_READDIO_NEED_LOCK , &inode->runtime_flags); -smp_mb (); -} - -static inline void btrfs_inode_resume_unlocked_dio (struct btrfs_inode *inode) -{ -smp_mb__before_atomic (); -clear_bit (BTRFS_INODE_READDIO_NEED_LOCK , &inode->runtime_flags); -} -/* Array of bytes with variable length, hexadecimal format 0x1234 */ #define CSUM_FMT "0x%*phN"#define CSUM_FMT_VALUE (size, bytes) size, bytes
fs/btrfs/inode.c +0 -3
@@ -4835,10 +4835,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)truncate_setsize (inode, newsize); -/* Disable nonlocked read DIO to avoid the endless truncate */ -btrfs_inode_block_unlocked_dio (BTRFS_I (inode));inode_dio_wait (inode); -btrfs_inode_resume_unlocked_dio (BTRFS_I (inode)); ret =btrfs_truncate (inode, newsize == oldsize);if (ret && inode->i_nlink) {
Signature · 1 line
2.26.2
PATCH v10 6/6 btrfs: split btrfs_direct_IO to read and write part
In reply to Goldwyn RodriguesFrom: Goldwyn Rodrigues <[email protected]>
The read and write versions don't have anything in common except for the call to iomap_dio_rw. So split this function, and merge each half into its only caller.
Originally proposed by Christoph Hellwig <[email protected]>
--- fs/btrfs/ctree.h | 4 +- fs/btrfs/file.c | 95 +++++++++++++++++++++++++++++++++++++++++++----- fs/btrfs/inode.c | 82 +---------------------------------------- 3 files changed, 90 insertions(+), 91 deletions(-)
fs/btrfs/ctree.h +3 -1
@@ -28,6 +28,7 @@#include <linux/dynamic_debug.h> #include <linux/refcount.h> #include <linux/crc32c.h> +#include <linux/iomap.h> #include "extent-io-tree.h" #include "extent_io.h" #include "extent_map.h"
@@ -2935,7 +2936,8 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end);void btrfs_writepage_endio_finish_ordered (struct page *page,u64 start,u64 end,int uptodate);extern const struct dentry_operations btrfs_dentry_operations; -ssize_t btrfs_direct_IO (struct kiocb *iocb,struct iov_iter *iter); +extern const struct iomap_ops btrfs_dio_iomap_ops; +extern const struct iomap_dio_ops btrfs_dops;/* ioctl.c */ long btrfs_ioctl (struct file *file,unsigned int cmd,unsigned long arg);
fs/btrfs/file.c +85 -10
129 lines not on this page · raw message
fs/btrfs/inode.c +2 -80
101 lines not on this page · raw message
Signature · 1 line
2.26.2