fs: Various I_DIRTY_* state handling fixes
Subsystems (11)
Jan Kara
reviewerv3 cover fs: Various I_DIRTY_* state handling fixes
Hello,
this series is a result of my audit of I_DIRTY_* state handling fixes. Over the years some pointless code or code with smaller or larger bugs has accumulated in filesystems around handling of inode dirty state. In particular testing for I_DIRTY_PAGES is very subtle (and racy without holding i_lock), some places forget that I_DIRTY_TIME also means inode has dirty metadata etc. So this patch set removes direct checks of dirtiness state from filesystems and either change them to directly testing PAGECACHE_TAG_DIRTY or to a helper function which performs appropriate dirtiness check reliably. I think this has gathered enough feedback to enter VFS tree now.
Changes since v2:
* Added Reviewed-by tags
* Improved commit messages
Changes since v1:
* Fixed orangefs checking of inode dirtiness
* Added Reviewed-by & Acked-by tags
* Added Fixes tags
* Made gfs2_fsync() conversion more bug-to-bug compatible
Honza
Previous versions:
Jan Kara
reviewerPATCH v3 1/13 bdev: Drop pointless loop from blkdev_flush_mapping()
bdev_write_inode() is really odd loop calling write_inode_now(). The bdev doesn't have any pages by now and if there are any dirty bits in the inode state, they are stale. Just call write_inode_now() once to clear them.
--- block/bdev.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-)
block/bdev.c +6 -19
@@ -60,24 +60,6 @@ struct block_device *file_bdev(struct file *bdev_file) }EXPORT_SYMBOL (file_bdev); -static void bdev_write_inode (struct block_device *bdev) -{ -struct inode *inode =BD_INODE (bdev); -int ret; - -spin_lock (&inode->i_lock); -while (inode_state_read (inode) &I_DIRTY ) { -spin_unlock (&inode->i_lock); - ret =write_inode_now (inode, true); -if (ret) -pr_warn_ratelimited ( -"VFS: Dirty inode writeback failed for block device %pg (err=%d).\n" , - bdev, ret); -spin_lock (&inode->i_lock); - } -spin_unlock (&inode->i_lock); -} -/* Kill _all_ buffers and pagecache , dirty or not.. */ static void kill_bdev (struct block_device *bdev) {
@@ -755,7 +737,12 @@ static void blkdev_flush_mapping(struct block_device *bdev)WARN_ON_ONCE (bdev->bd_holders);sync_blockdev (bdev);kill_bdev (bdev); -bdev_write_inode (bdev); +/* +* All data is written out and evicted but inode can have some stale +* I_DIRTY bits set. Call write_inode_now() to get rid of them so that +* the inode can be evicted. +*/ +write_inode_now (BD_INODE (bdev),1 ); }static void blkdev_put_whole (struct block_device *bdev)
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 2/13 xfs: Drop pointless I_DIRTY_PAGES check in xfs_can_free_cowblocks()
In reply to Jan KaraPAGECACHE_TAG_DIRTY is the authoritative source of information about any folio being dirty in the mapping. I_DIRTY_PAGES is just an approximation used by writeback code that can get stale under certain situations. So just rely on the authoritative check.
--- fs/xfs/xfs_reflink.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/xfs/xfs_reflink.h +1 -2
@@ -17,8 +17,7 @@ xfs_can_free_cowblocks(struct xfs_inode *ip) {struct inode *inode =VFS_I (ip); -if ((inode_state_read_once (inode) &I_DIRTY_PAGES ) || -mapping_tagged (inode->i_mapping,PAGECACHE_TAG_DIRTY ) || +if (mapping_tagged (inode->i_mapping,PAGECACHE_TAG_DIRTY ) ||mapping_tagged (inode->i_mapping,PAGECACHE_TAG_WRITEBACK ) ||atomic_read (&inode->i_dio_count))return false;
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 3/13 orangefs: Check PAGECACHE_TAG_DIRTY instead of I_DIRTY_PAGES inode state
In reply to Jan KaraPAGECACHE_TAG_DIRTY is the authoritative source of dirty folios present
in the mapping. Use that instead of looking at I_DIRTY_PAGES inode state which can get stale for example when folios are written directly with filemap_fdatawrite(), which is how orangefs_flush() and orangefs_fsync() write the mapping. So a getattr keeps serving the cached inode after the folios are clean.
The dirty tag alone is not enough. folio_start_writeback() clears it and sets PAGECACHE_TAG_WRITEBACK before wait_for_direct_io() sends the write. OrangeFS writepages does not return until that call finishes, so I_DIRTY_PAGES still covers the round trip today. A getattr that looks only at the dirty tag stores the server's old size while the write is in flight. orangefs_writepage_locked() then reads that size and shortens the write.
Hold the cached attributes while either tag is set.
--- fs/orangefs/orangefs-utils.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
fs/orangefs/orangefs-utils.c +17 -3
@@ -233,6 +233,20 @@ static int orangefs_inode_is_stale(struct inode *inode,return 0 ; } +/* +* Dirty and writeback tags, not I_DIRTY_PAGES. filemap_fdatawrite() +* leaves that flag set after the folios are clean. folio_start_writeback() +* clears the dirty tag before wait_for_direct_io() returns, so the +* writeback tag is what covers the server write. +*/ +static bool orangefs_writeback_pending (struct inode *inode) +{ +struct address_space *mapping = inode->i_mapping; + +return mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ) || +mapping_tagged (mapping,PAGECACHE_TAG_WRITEBACK ); +} +int orangefs_inode_getattr (struct inode *inode,int flags) {struct orangefs_inode_s *orangefs_inode =ORANGEFS_I (inode);
@@ -247,7 +261,7 @@ int orangefs_inode_getattr(struct inode *inode, int flags)spin_lock (&inode->i_lock);/* Must have all the attributes in the mask and be within cache time. */ if ((!flags &&time_before (jiffies, orangefs_inode->getattr_time)) || - orangefs_inode->attr_valid ||inode_state_read (inode) &I_DIRTY_PAGES ) { + orangefs_inode->attr_valid ||orangefs_writeback_pending (inode)) {if (orangefs_inode->attr_valid) {spin_unlock (&inode->i_lock);write_inode_now (inode,1 );
@@ -281,13 +295,13 @@ int orangefs_inode_getattr(struct inode *inode, int flags)spin_lock (&inode->i_lock);/* Must have all the attributes in the mask and be within cache time. */ if ((!flags &&time_before (jiffies, orangefs_inode->getattr_time)) || - orangefs_inode->attr_valid ||inode_state_read (inode) &I_DIRTY_PAGES ) { + orangefs_inode->attr_valid ||orangefs_writeback_pending (inode)) {if (orangefs_inode->attr_valid) {spin_unlock (&inode->i_lock);write_inode_now (inode,1 ); goto again2; } -if (inode_state_read (inode) &I_DIRTY_PAGES ) { +if (orangefs_writeback_pending (inode)) { ret =0 ; goto out_unlock; }
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 4/13 fs: Provide inode_is_dirty() helper
In reply to Jan KaraProvide inode_is_dirty() helper for use by filesystems (mostly fsync methods).
--- include/linux/fs.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/fs.h +11 -0
@@ -2217,6 +2217,17 @@ static inline void mark_inode_dirty_sync(struct inode *inode)__mark_inode_dirty (inode,I_DIRTY_SYNC ); } +/* +* Check whether inode itself needs to be written out either at all or for +* the purposes of datasync if @datasync is set. +*/ +static inline bool inode_is_dirty (struct inode *inode,bool datasync) +{ +int mask = datasync ?I_DIRTY_DATASYNC : (I_DIRTY_INODE |I_DIRTY_TIME ); + +return inode_state_read_once (inode) & mask; +} +static inline void set_inode_metadata_writeback (struct inode *inode) {spin_lock (&inode->i_lock);
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 5/13 jfs: Use inode_is_dirty() in jfs_fsync()
In reply to Jan KaraUse inode_is_dirty() helper in jfs_fsync() instead of explicit i_state check. This slightly changes behavior in that we no longer trigger inode commit if the inode is clean except it still has I_DIRTY_PAGES set after data writeout but that is actually a desirable behavior because commit is pointless if only folios are dirty.
--- fs/jfs/file.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/jfs/file.c +1 -2
@@ -27,8 +27,7 @@ int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)return rc;inode_lock (inode); -if (!(inode_state_read_once (inode) &I_DIRTY_ALL ) || - (datasync && !(inode_state_read_once (inode) &I_DIRTY_DATASYNC ))) { +if (!inode_is_dirty (inode, datasync)) {/* Make sure committed changes hit the disk */ jfs_flush_journal (JFS_SBI (inode->i_sb)->log,1 );inode_unlock (inode);
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 6/13 gfs2: Avoid racy dirtiness check in gfs2_fsync()
In reply to Jan KaraCheck for I_DIRTY_PAGES state in gfs2_fsync() is racy because __writeback_single_inode() unconditionally clears the flag and reinstates it only later based on PAGECACHE_TAG_DIRTY tag. If gfs2_fsync() unlocked read of i_state hits in between, it will not see I_DIRTY_PAGES although there may be dirty folios in the mapping. Check for PAGECACHE_TAG_DIRTY instead and use inode_is_dirty() helper for inode metadata dirtiness check.
Using the helper effectively reinstates checking of I_DIRTY_TIME bit that was removed in 3aac630b5ce1 ("gfs2: don't worry about I_DIRTY_TIME in gfs2_fsync()"). Since vfs_fsync_range() calls sync_lazytime(), checking I_DIRTY_TIME is indeed unnecessary but it is harmless as well and using the helper is more robust in the long run.
--- fs/gfs2/file.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
fs/gfs2/file.c +3 -7
@@ -749,7 +749,7 @@ static int gfs2_fsync(struct file *file, loff_t start, loff_t end, {struct address_space *mapping = file->f_mapping;struct inode *inode = mapping->host; -int sync_state =inode_state_read_once (inode) &I_DIRTY ; +bool dirty_data =mapping_tagged (inode->i_mapping,PAGECACHE_TAG_DIRTY );struct gfs2_glock *gl =gfs2_inode_glock (inode);struct gfs2_inode *ip =GFS2_I (inode);int ret =0 , ret1 =0 ;
@@ -760,12 +760,8 @@ static int gfs2_fsync(struct file *file, loff_t start, loff_t end,return ret1; } -if (!gfs2_is_jdata (ip)) - sync_state &= ~I_DIRTY_PAGES ; -if (datasync) - sync_state &= ~I_DIRTY_SYNC ; - -if (sync_state) { +if (inode_is_dirty (inode, datasync) || + (gfs2_is_jdata (ip) && dirty_data)) { ret =sync_inode_metadata (inode,1 );if (ret)return ret;
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 7/13 fuse: Use inode_is_dirty() check in fuse_evict_inode()
In reply to Jan KaraUse inode_is_dirty() helper instead of explicit i_state check. This will result in additionally warning if we have unwritten timestamp updates when lazytime is enabled but that is actually desirable.
--- fs/fuse/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/fuse/inode.c +1 -1
@@ -143,7 +143,7 @@ static void fuse_evict_inode(struct inode *inode)struct fuse_inode *fi =get_fuse_inode (inode);/* Will write inode on close/munmap and in all other dirtiers */ -WARN_ON (inode_state_read_once (inode) &I_DIRTY_INODE ); +WARN_ON (inode_is_dirty (inode, false));if (IS_DAX (inode))dax_break_layout_final (inode);
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 8/13 fs: Provide inode_or_folios_dirty() helper
In reply to Jan KaraProvide a helper function checking whether inode itself or some of its folios are dirty. Also returns true when only the timestamps are dirty. We'll use it in filesystems instead of direct i_state checks.
--- include/linux/fs.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/linux/fs.h +7 -0
@@ -2228,6 +2228,13 @@ static inline bool inode_is_dirty(struct inode *inode, bool datasync)return inode_state_read_once (inode) & mask; } +/* Check whether anything in the inode itself or its folios need writing */ +static inline bool inode_or_folios_dirty (struct inode *inode) +{ +return inode_state_read_once (inode) &I_DIRTY_ALL || +mapping_tagged (inode->i_mapping,PAGECACHE_TAG_DIRTY ); +} +static inline void set_inode_metadata_writeback (struct inode *inode) {spin_lock (&inode->i_lock);
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 9/13 gfs2: Fix dirtiness checks in gfs2_write_inode() and evict_linked_inode()
In reply to Jan KaraThe checks are bogus because they check against inode->i_flags instead of inode->i_state. Use the inode_or_folios_dirty() helper which actually checks what was intended.
--- fs/gfs2/super.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/gfs2/super.c +3 -2
@@ -460,7 +460,7 @@ static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)mark_inode_dirty_sync (inode); else {spin_lock (&inode->i_lock); -if (!(inode->i_flags &I_DIRTY )) +if (!inode_or_folios_dirty (inode))gfs2_ordered_del_inode (ip);spin_unlock (&inode->i_lock); }
@@ -1418,7 +1418,8 @@ static int evict_linked_inode(struct inode *inode, struct gfs2_holder *gh)struct address_space *metamapping =gfs2_glock2aspace (gl);int ret; -if (!(test_bit (GLF_DIRTY , &gl->gl_flags) || inode->i_flags &I_DIRTY )) +if (!(test_bit (GLF_DIRTY , &gl->gl_flags) || +inode_or_folios_dirty (inode))) goto clean;/* The inode glock must be held exclusively and be instantiated. */
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 10/13 afs: Use inode_or_folios_dirty() in afs_evict_inode()
In reply to Jan KaraUse the helper instead of direct inode I_DIRTY check to abstract details of inode dirtiness handling. This also properly triggers when only timestamps are dirty and lazytime is enabled (although I'm not sure if it really matters for this particular case in AFS).
--- fs/afs/inode.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
fs/afs/inode.c +2 -4
@@ -667,10 +667,8 @@ void afs_evict_inode(struct inode *inode) ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode); -if ((S_ISDIR (inode->i_mode) || -S_ISLNK (inode->i_mode)) && - (inode_state_read_once (inode) &I_DIRTY ) && - !sbi->dyn_root) { +if ((S_ISDIR (inode->i_mode) ||S_ISLNK (inode->i_mode)) && +inode_or_folios_dirty (inode) && !sbi->dyn_root) {struct writeback_control wbc = { .sync_mode =WB_SYNC_ALL , .for_sync = true,
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 11/13 nfsd: Use inode_or_folios_dirty() for dirtiness check in wait_for_concurrent_writes()
In reply to Jan KaraThe check for dirtiness in wait_for_concurrent_writes() is racy because I_DIRTY_PAGES flag can be temporarily cleared by __writeback_single_inode(). Use inode_or_folios_dirty() helper to avoid that. This also makes sure inode timestamps properly persisted when lazytime is enabled.
--- fs/nfsd/vfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/vfs.c +1 -1
@@ -1249,7 +1249,7 @@ static int wait_for_concurrent_writes(struct file *file)dprintk ("nfsd: write resume %d\n" ,task_pid_nr (current)); } -if (inode_state_read_once (inode) &I_DIRTY ) { +if (inode_or_folios_dirty (inode)) {dprintk ("nfsd: write sync %d\n" ,task_pid_nr (current)); err =vfs_fsync (file,0 ); }
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 12/13 fscrypt: Use inode_or_folios_dirty() instead of explicit state check
In reply to Jan KaraUse inode_or_folios_dirty() helper instead of explicit inode state check. No functional change.
--- fs/crypto/keysetup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/crypto/keysetup.c +1 -1
@@ -839,7 +839,7 @@ int fscrypt_drop_inode(struct inode *inode)* userspace is still using the files, inodes can be dirtied between * then and now. We mustn't lose any writes, so skip dirty inodes here. */ -if (inode_state_read (inode) &I_DIRTY_ALL ) +if (inode_or_folios_dirty (inode))return 0 ;/*
Signature · 1 line
2.53.0
Jan Kara
reviewerPATCH v3 13/13 fs: Use inode_or_folios_dirty() in __inode_lru_list_add()
In reply to Jan KaraUse inode_or_folios_dirty() helper instead of explicit i_state check in __inode_lru_list_add(). No functional change.
--- fs/inode.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/inode.c +3 -1
@@ -552,7 +552,9 @@ static void __inode_lru_list_add(struct inode *inode, bool rotate) {lockdep_assert_held (&inode->i_lock); -if (inode_state_read (inode) & (I_DIRTY_ALL |I_SYNC |I_FREEING |I_WILL_FREE )) +if (inode_state_read (inode) & (I_SYNC |I_FREEING |I_WILL_FREE )) +return ; +if (inode_or_folios_dirty (inode))return ;if (icount_read (inode))return ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 13/13] fs: Use inode_or_folios_dirty() in __inode_lru_list_add()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] fs: UAF of inode->i_mapping in __inode_lru_list_add() after dev_dax teardown --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] fs: UAF of inode->i_mapping in __inode_lru_list_add() after dev_dax teardown
--
commit dba191eb05adecdae3b365e1982438c7882c5c1d
Author: Jan Kara <[email protected]>
fs: Use inode_or_folios_dirty() in __inode_lru_list_add()
This commit replaces the I_DIRTY_ALL check in __inode_lru_list_add() with a call to inode_or_folios_dirty().
fs/inode.c static void __inode_lru_list_add(struct inode *inode, bool rotate) · line 558
Quoted 13 lines
fs/inode.c +3
-1@@ -552,7 +552,9 @@ static void __inode_lru_list_add(struct inode *inode, bool rotate) {lockdep_assert_held (&inode->i_lock); -if (inode_state_read (inode) & (I_DIRTY_ALL |I_SYNC |I_FREEING |I_WILL_FREE )) +if (inode_state_read (inode) & (I_SYNC |I_FREEING |I_WILL_FREE )) +return ; +if (inode_or_folios_dirty (inode))return ;
[Severity: High]
Can inode_or_folios_dirty() cause a use-after-free on inode->i_mapping when a device-dax character special file inode is added to the LRU after dev_dax teardown?
Unlike mapping_shrinkable(&inode->i_data) below and the other LRU and eviction paths in fs/inode.c, inode_or_folios_dirty() dereferences inode->i_mapping via mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) whenever I_DIRTY_ALL is clear.
When a device-dax character device is opened, dax_open() redirects the special file's inode->i_mapping to &dax_dev->inode.i_data, which is normally only reset to &inode->i_data in evict() -> cd_forget() if inode->i_cdev is still non-NULL. If the dev_dax instance is unbound or deleted while the special file inode remains cached, cdev_purge() clears inode->i_cdev without restoring inode->i_mapping, and dev_dax_release() -> put_dax() -> dax_free_inode() frees dax_dev. When the last reference to the special file inode is later dropped in iput_final() -> __inode_lru_list_add(), inode_or_folios_dirty() reads from the freed dax_dev->inode.i_data mapping.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13