mm: Replace ->launder_folio() with flush and wait
Subsystems (15)
David Howells
maintainerRFC PATCH 1/1 mm: Replace ->launder_folio() with flush and wait
Here's a patch to have a go at getting rid of ->launder_folio(). Since it's failable and cannot guarantee that pages in the range are removed, I've tried to replace laundering with just flush-and-wait, dropping the folio lock around the I/O.
Are there any tests for launder_folio as triggering it is tricky?
David
---
mm: Replace ->launder_folio() with flush and wait
invalidate_inode_pages2_range() and its wrappers are frequently used to invalidate overlapping folios prior to and after doing direct I/O. This calls ->launder_folio() to flush dirty folios out to the backing store, keeping the folio lock across the I/O - presumably to prevent the folio from being redirtied and thereby prevent it from being removed.
However... If we're doing this prior to doing DIO on a file, there may be nothing preventing an mmapped write from recreating and redirtying the folio the moment it is removed from the mapping lest the kernel deadlock on doing DIO to/from a buffer mmapped from the target file.
Further, invalidate_inode_pages2_range() is permitted to fail - and half the callers don't even check to see if it *did* fail, probably not unreasonably.
In which case, there's no need to keep the folio lock and no need for a special op. Instead, drop the lock and flush the range covering the page to the end of the range to be invalidated (with a flag set in the wbc to say what we're doing) and then wait for it to complete.
That said, there are other places that use invalidate_inode_pages2_range() and most of those don't implement launder_page, so add a flag to only do this in those filesystems that implemented it: 9p, afs, cifs, fuse, nfs and orangefs.
--- Documentation/filesystems/locking.rst | 8 --- Documentation/filesystems/vfs.rst | 6 -- fs/9p/vfs_addr.c | 1 fs/9p/vfs_inode.c | 1 fs/afs/file.c | 1 fs/afs/inode.c | 1 fs/fuse/dir.c | 2 fs/fuse/file.c | 17 ------- fs/netfs/buffered_write.c | 76 ---------------------------------- fs/nfs/file.c | 23 ---------- fs/nfs/inode.c | 1 fs/nfs/nfstrace.h | 1 fs/orangefs/inode.c | 2 fs/smb/client/file.c | 23 ---------- fs/smb/client/inode.c | 1 include/linux/fs.h | 1 include/linux/netfs.h | 1 include/linux/pagemap.h | 6 ++ include/linux/writeback.h | 1 mm/truncate.c | 35 +++++++++------ 20 files changed, 36 insertions(+), 172 deletions(-)
Documentation/filesystems/locking.rst +0 -8
@@ -262,7 +262,6 @@ prototypes:: int (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct address_space *, struct folio *dst, struct folio *src, enum migrate_mode); - int (*launder_folio)(struct folio *); bool (*is_partially_uptodate)(struct folio *, size_t from, size_t count); int (*error_remove_folio)(struct address_space *, struct folio *); int (*swap_activate)(struct swap_info_struct *sis, struct file *f, sector_t *span)
@@ -288,7 +287,6 @@ release_folio: yes free_folio: yes direct_IO: migrate_folio: yes (both) -launder_folio: yes is_partially_uptodate: yes error_remove_folio: yes swap_activate: no
@@ -394,12 +392,6 @@ try_to_free_buffers(). ->free_folio() is called when the kernel has dropped the folio from the page cache. -->launder_folio() may be called prior to releasing a folio if -it is still found to be dirty. It returns zero if the folio was successfully -cleaned, or an error value if not. Note that in order to prevent the folio -getting mapped back in and redirtied, it needs to be kept locked -across the entire operation. - ->swap_activate() will be called to prepare the given file for swap. It should perform any validation and preparation necessary to ensure that writes can be performed with minimal memory allocation. It should call
Documentation/filesystems/vfs.rst +0 -6
@@ -818,7 +818,6 @@ cache in your filesystem. The following members are defined: ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct mapping *, struct folio *dst, struct folio *src, enum migrate_mode); - int (*launder_folio) (struct folio *); bool (*is_partially_uptodate) (struct folio *, size_t from, size_t count);
@@ -1012,11 +1011,6 @@ cache in your filesystem. The following members are defined: folio to this function. migrate_folio should transfer any private data across and update any references that it has to the folio. -``launder_folio`` - Called before freeing a folio - it writes back the dirty folio. - To prevent redirtying the folio, it is kept locked during the - whole operation. - ``is_partially_uptodate`` Called by the VM when reading a file through the pagecache when the underlying blocksize is smaller than the size of the folio.
fs/9p/vfs_addr.c +0 -1
@@ -141,7 +141,6 @@ const struct address_space_operations v9fs_addr_operations = { .dirty_folio = netfs_dirty_folio, .release_folio = netfs_release_folio, .invalidate_folio = netfs_invalidate_folio, - .launder_folio = netfs_launder_folio, .direct_IO = noop_direct_IO, .writepages = netfs_writepages, };
fs/9p/vfs_inode.c +1 -0
@@ -263,6 +263,7 @@ int v9fs_init_inode(struct v9fs_session_info *v9ses,simple_inode_init_ts (inode); inode->i_mapping->a_ops = &v9fs_addr_operations; inode->i_private =NULL ; +mapping_set_launder_folios (inode->i_mapping);switch (mode &S_IFMT ) {case S_IFIFO :
fs/afs/file.c +0 -1
@@ -54,7 +54,6 @@ const struct address_space_operations afs_file_aops = { .read_folio = netfs_read_folio, .readahead = netfs_readahead, .dirty_folio = netfs_dirty_folio, - .launder_folio = netfs_launder_folio, .release_folio = netfs_release_folio, .invalidate_folio = netfs_invalidate_folio, .migrate_folio = filemap_migrate_folio,
fs/afs/inode.c +1 -0
@@ -104,6 +104,7 @@ static int afs_inode_init_from_status(struct afs_operation *op, inode->i_fop = &afs_file_operations; inode->i_mapping->a_ops = &afs_file_aops;mapping_set_large_folios (inode->i_mapping); +mapping_set_launder_folios (inode->i_mapping);break ;case AFS_FTYPE_DIR : inode->i_mode =S_IFDIR | (status->mode &S_IALLUGO );
fs/fuse/dir.c +1 -1
@@ -1978,7 +1978,7 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,/* * Only call invalidate_inode_pages2() after removing -* FUSE_NOWRITE, otherwise fuse_launder_folio() would deadlock. +* FUSE_NOWRITE, otherwise writeback would deadlock. */ if ((is_truncate || !is_wb) &&S_ISREG (inode->i_mode) && oldsize != outarg.attr.size) {
fs/fuse/file.c +1 -16
@@ -2393,21 +2393,6 @@ static int fuse_write_end(struct file *file, struct address_space *mapping,return copied; } -static int fuse_launder_folio (struct folio *folio) -{ -int err =0 ; -if (folio_clear_dirty_for_io (folio)) { -struct inode *inode = folio->mapping->host; - -/* Serialize with pending writeback for the same page */ -fuse_wait_on_page_writeback (inode, folio->index); - err =fuse_writepage_locked (&folio->page); -if (!err) -fuse_wait_on_page_writeback (inode, folio->index); - } -return err; -} -/* * Write back dirty data/metadata now (there may not be any suitable * open files later for data)
@@ -3227,7 +3212,6 @@ static const struct address_space_operations fuse_file_aops = { .readahead = fuse_readahead, .writepage = fuse_writepage, .writepages = fuse_writepages, - .launder_folio = fuse_launder_folio, .dirty_folio = filemap_dirty_folio, .bmap = fuse_bmap, .direct_IO = fuse_direct_IO,
@@ -3241,6 +3225,7 @@ void fuse_init_file_inode(struct inode *inode, unsigned int flags) inode->i_fop = &fuse_file_operations; inode->i_data.a_ops = &fuse_file_aops; +mapping_set_launder_folios (inode->i_mapping);INIT_LIST_HEAD (&fi->write_files);INIT_LIST_HEAD (&fi->queued_writes);
fs/netfs/buffered_write.c +1 -75
85 lines not on this page · raw message
fs/nfs/file.c +0 -23
@@ -484,28 +484,6 @@ static void nfs_check_dirty_writeback(struct folio *folio, *dirty = true; } -/* -* Attempt to clear the private state associated with a page when an error -* occurs that requires the cached contents of an inode to be written back or -* destroyed -* - Called if either PG_private or fscache is set on the page -* - Caller holds page lock -* - Return 0 if successful, -error otherwise -*/ -static int nfs_launder_folio (struct folio *folio) -{ -struct inode *inode = folio->mapping->host; -int ret; - -dfprintk (PAGECACHE ,"NFS: launder_folio(%ld, %llu)\n" , - inode->i_ino,folio_pos (folio)); - -folio_wait_fscache (folio); - ret =nfs_wb_folio (inode, folio); -trace_nfs_launder_folio_done (inode, folio, ret); -return ret; -} -static int nfs_swap_activate (struct swap_info_struct *sis,struct file *file,sector_t *span) {
@@ -564,7 +542,6 @@ const struct address_space_operations nfs_file_aops = { .invalidate_folio = nfs_invalidate_folio, .release_folio = nfs_release_folio, .migrate_folio = nfs_migrate_folio, - .launder_folio = nfs_launder_folio, .is_dirty_writeback = nfs_check_dirty_writeback, .error_remove_folio = generic_error_remove_folio, .swap_activate = nfs_swap_activate,
fs/nfs/inode.c +1 -0
@@ -491,6 +491,7 @@ nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr) inode->i_fop =NFS_SB (sb)->nfs_client->rpc_ops->file_ops; inode->i_data.a_ops = &nfs_file_aops;nfs_inode_init_regular (nfsi); +mapping_set_launder_folios (inode->i_mapping); }else if (S_ISDIR (inode->i_mode)) { inode->i_op =NFS_SB (sb)->nfs_client->rpc_ops->dir_inode_ops; inode->i_fop = &nfs_dir_operations;
fs/nfs/nfstrace.h +0 -1
@@ -1039,7 +1039,6 @@ DEFINE_NFS_FOLIO_EVENT(nfs_writeback_folio);DEFINE_NFS_FOLIO_EVENT_DONE (nfs_writeback_folio_done);DEFINE_NFS_FOLIO_EVENT (nfs_invalidate_folio); -DEFINE_NFS_FOLIO_EVENT_DONE (nfs_launder_folio_done); TRACE_EVENT(nfs_aop_readahead, TP_PROTO(
fs/orangefs/inode.c +1 -1
@@ -626,7 +626,6 @@ static const struct address_space_operations orangefs_address_operations = { .invalidate_folio = orangefs_invalidate_folio, .release_folio = orangefs_release_folio, .free_folio = orangefs_free_folio, - .launder_folio = orangefs_launder_folio, .direct_IO = orangefs_direct_IO, };
@@ -980,6 +979,7 @@ static const struct inode_operations orangefs_file_inode_operations = {static int orangefs_init_iops (struct inode *inode) { inode->i_mapping->a_ops = &orangefs_address_operations; +mapping_set_launder_folios (inode->i_mapping);switch (inode->i_mode &S_IFMT ) {case S_IFREG :
fs/smb/client/file.c +0 -23
@@ -4858,27 +4858,6 @@ static void cifs_invalidate_folio(struct folio *folio, size_t offset,folio_wait_fscache (folio); } -static int cifs_launder_folio (struct folio *folio) -{ -int rc =0 ; -loff_t range_start =folio_pos (folio); -loff_t range_end = range_start +folio_size (folio); -struct writeback_control wbc = { - .sync_mode =WB_SYNC_ALL , - .nr_to_write =0 , - .range_start = range_start, - .range_end = range_end, - }; - -cifs_dbg (FYI ,"Launder page: %lu\n" , folio->index); - -if (folio_clear_dirty_for_io (folio)) - rc =cifs_writepage_locked (&folio->page, &wbc); - -folio_wait_fscache (folio); -return rc; -} -void cifs_oplock_break (struct work_struct *work) { struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
@@ -5057,7 +5036,6 @@ const struct address_space_operations cifs_addr_ops = { .release_folio = cifs_release_folio, .direct_IO = cifs_direct_io, .invalidate_folio = cifs_invalidate_folio, - .launder_folio = cifs_launder_folio, .migrate_folio = filemap_migrate_folio,/* * TODO: investigate and if useful we could add an is_dirty_writeback
@@ -5080,6 +5058,5 @@ const struct address_space_operations cifs_addr_ops_smallbuf = { .dirty_folio = netfs_dirty_folio, .release_folio = cifs_release_folio, .invalidate_folio = cifs_invalidate_folio, - .launder_folio = cifs_launder_folio, .migrate_folio = filemap_migrate_folio, };
fs/smb/client/inode.c +1 -0
@@ -56,6 +56,7 @@ static void cifs_set_ops(struct inode *inode) inode->i_data.a_ops = &cifs_addr_ops_smallbuf; else inode->i_data.a_ops = &cifs_addr_ops; +mapping_set_launder_folios (inode->i_mapping);break ;case S_IFDIR :if (IS_AUTOMOUNT (inode)) {
include/linux/fs.h +0 -1
@@ -432,7 +432,6 @@ struct address_space_operations {*/ int (*migrate_folio)(struct address_space *,struct folio *dst,struct folio *src,enum migrate_mode ); -int (*launder_folio)(struct folio *);bool (*is_partially_uptodate) (struct folio *,size_t from,size_t count);void (*is_dirty_writeback) (struct folio *,bool *dirty,bool *wb);
include/linux/netfs.h +0 -1
@@ -410,7 +410,6 @@ int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);void netfs_clear_inode_writeback (struct inode *inode,const void *aux);void netfs_invalidate_folio (struct folio *folio,size_t offset,size_t length);bool netfs_release_folio (struct folio *folio,gfp_t gfp); -int netfs_launder_folio (struct folio *folio);/* VMA operations API. */ vm_fault_t netfs_page_mkwrite (struct vm_fault *vmf,struct netfs_group *netfs_group);
include/linux/pagemap.h +6 -0
@@ -207,6 +207,7 @@ enum mapping_flags {AS_STABLE_WRITES ,/* must wait for writeback before modifying folio contents */ AS_UNMOVABLE ,/* The mapping cannot be moved, ever */ + AS_LAUNDER_FOLIOS, /* Use laundering in invalidate_inode_pages2*() */ };/**
@@ -323,6 +324,11 @@ static inline bool mapping_unmovable(struct address_space *mapping)return test_bit (AS_UNMOVABLE , &mapping->flags); } +static inline void mapping_set_launder_folios (struct address_space *mapping) +{ +set_bit (AS_LAUNDER_FOLIOS , &mapping->flags); +} +static inline gfp_t mapping_gfp_mask (struct address_space * mapping) {return mapping->gfp_mask;
include/linux/writeback.h +1 -0
@@ -62,6 +62,7 @@ struct writeback_control { unsigned for_reclaim:1; /* Invoked from the page allocator */ unsigned range_cyclic:1; /* range_start is cyclic */ unsigned for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ + unsigned for_launder:1; /* Called from invalidate_inode_pages2_range() */ unsigned unpinned_netfs_wb:1; /* Cleared I_PINNING_NETFS_WB *//*
mm/truncate.c +21 -14
@@ -586,15 +586,6 @@ static int invalidate_complete_folio2(struct address_space *mapping,return 0 ; } -static int folio_launder (struct address_space *mapping,struct folio *folio) -{ -if (!folio_test_dirty (folio)) -return 0 ; -if (folio->mapping != mapping || mapping->a_ops->launder_folio ==NULL ) -return 0 ; -return mapping->a_ops->launder_folio (folio); -} -/** * invalidate_inode_pages2_range - remove range of pages from an address_space * @mapping: the address_space
@@ -657,13 +648,29 @@ int invalidate_inode_pages2_range(struct address_space *mapping,unmap_mapping_folio (folio);BUG_ON (folio_mapped (folio)); - ret2 =folio_launder (mapping, folio); -if (ret2 ==0 ) { -if (!invalidate_complete_folio2 (mapping, folio)) +if (folio_test_dirty (folio) && +test_bit (AS_LAUNDER_FOLIOS , &mapping->flags)) { +struct writeback_control wbc = { + .sync_mode =WB_SYNC_ALL , + .nr_to_write =LLONG_MAX , + .range_start =folio_pos (folio), + .range_end = (end +1ULL ) *PAGE_SIZE -1 , + .for_launder = true, + }; + +folio_unlock (folio); + ret2 =filemap_fdatawrite_wbc (mapping, &wbc); +folio_lock (folio); +if (ret2 <0 ) + ret = ret2; +else if (!invalidate_complete_folio2 (mapping, folio) && + ret ==0 ) + ret2 = -EBUSY ; + }else { +if (!invalidate_complete_folio2 (mapping, folio) && + ret ==0 ) ret2 = -EBUSY ; } -if (ret2 <0 ) - ret = ret2;folio_unlock (folio); }folio_batch_remove_exceptionals (&fbatch);
Matthew Wilcox
maintainerQuoted 4 lines · David Howells
Here's a patch to have a go at getting rid of ->launder_folio(). Since it's failable and cannot guarantee that pages in the range are removed, I've tried to replace laundering with just flush-and-wait, dropping the folio lock around the I/O.
My sense is that ->launder_folio doesn't actually need to be replaced.
commit e3db7691e9f3dff3289f64e3d98583e28afe03db
Author: Trond Myklebust <[email protected]> Date: Wed Jan 10 23:15:39 2007 -0800
[PATCH] NFS: Fix race in nfs_release_page()
NFS: Fix race in nfs_release_page()
invalidate_inode_pages2() may find the dirty bit has been set on a page owing to the fact that the page may still be mapped after it was locked. Only after the call to unmap_mapping_range() are we sure that the page can no longer be dirtied.
In order to fix this, NFS has hooked the releasepage() method and tries to write the page out between the call to unmap_mapping_range() and the call to remove_mapping(). This, however leads to deadlocks in the page reclaim code, where the page may be locked without holding a reference to the inode or dentry.
Fix is to add a new address_space_operation, launder_page(), which will attempt to write out a dirty page without releasing the page lock.
I don't understand why this couldn't've been solved by page_mkwrite. NFS did later add nfs_vm_page_mkwrite in July 2007, and maybe it's just not needed any more? I haven't looked into it enough to make sure, but my belief is that we should be able to get rid of it.
David Howells
maintainerQuoted 10 lines · Matthew Wilcox
commit
e3db7691e9f3dff3289f64e3d98583e28afe03dbAuthor: Trond Myklebust <[email protected]> Date: Wed Jan 10 23:15:39 2007 -0800[PATCH] NFS: Fix race in
nfs_release_page()
...
invalidate_inode_pages2()may find the dirty bit has been set on a page owing to the fact that the page may still be mapped after it was locked. Only after the call tounmap_mapping_range()are we sure that the page can no longer be dirtied.
Is that last sentence even true? It evicts folios from the TLB and/or pagetables, but it doesn't actually trim any mmap made - in which case, userspace is perfectly at liberty to regenerate and dirty the folio the moment the folio is removed from the page tables. Otherwise DIO-to/from-mmap will deadlock.
Quoted 1 line
but my belief is that we should be able to get rid of it.
I think you're probably correct. The best we can do, I think, is to preface any call to invalidate_inode_pages2() with a flush-and-wait over the same range.
David
David Howells
maintainerQuoted 29 lines · Matthew Wilcox
Quoted 4 lines · David Howells
Here's a patch to have a go at getting rid of ->launder_folio(). Since it's failable and cannot guarantee that pages in the range are removed, I've tried to replace laundering with just flush-and-wait, dropping the folio lock around the I/O.
My sense is that ->launder_folio doesn't actually need to be replaced.
commit
e3db7691e9f3dff3289f64e3d98583e28afe03dbAuthor: Trond Myklebust <[email protected]> Date: Wed Jan 10 23:15:39 2007 -0800[PATCH] NFS: Fix race in
nfs_release_page()NFS: Fix race in
nfs_release_page()
invalidate_inode_pages2()may find the dirty bit has been set on a page owing to the fact that the page may still be mapped after it was locked. Only after the call tounmap_mapping_range()are we sure that the page can no longer be dirtied.
In order to fix this, NFS has hooked thereleasepage()method and tries to write the page out between the call tounmap_mapping_range()and the call toremove_mapping(). This, however leads to deadlocks in the page reclaim code, where the page may be locked without holding a reference to the inode or dentry.Fix is to add a new
address_space_operation,launder_page(), which will attempt to write out a dirty page without releasing the page lock.
Quoted 6 lines
Signed-off-by Trond Myklebust <[email protected]>I don't understand why this couldn't've been solved by
page_mkwrite. NFS did later addnfs_vm_page_mkwritein July 2007, and maybe it's just not needed any more? I haven't looked into it enough to make sure, but my belief is that we should be able to get rid of it.
Okay, it's slightly more complex than I thought - and I'm not sure all callers are actually using it correctly. There are some additional interesting cases I've found, beyond the pre-/post-DIO case:
(1) NFS relies on it to retry the write before stripping the pages in the case where a writeback error occurs. I think this can probably be dealt with by sticking a filemap_fdatawrite() call before the invalidation. I'm not sure if this would incur the deadlock with the page reclaim code of which Trond speaks.
(2) invalidate_inode_pages2() is used in some places to effect invalidation of the pagecache in the case where the server tells us that a third party modified the server copy of a file. What the right behaviour should be here, I'm not sure, but at the moment, any dirty data will get laundered back to the server. Possibly it should be simply invalidated locally or the user asked how they want to handle the divergence.
Some filesystems use invalidate_remote_inode() instead which seems to leave the dirty pages in place locally.
If it is desirous to save the dirty data, then filemap_fdatawrite() could be deployed before invalidating the pages.
(3) Fuse uses invalidate_inode_pages2() in fuse_do_setattr() to strip all the pages from an inode that has had its size changed, laundering any page that's still dirty. This would seem to be excessive, but maybe Miklos had a reason for doing it that way.
There are some places that should perhaps be using kiocb_invalidate_pages() and kiocb_invalidate_post_direct_write() instead - assuming Christoph has no objection to the latter function being exported.
David
Quoted 3 lines · David Howells
There are some places that should perhaps be using
kiocb_invalidate_pages()andkiocb_invalidate_post_direct_write()instead - assuming Christoph has no objection to the latter function being exported.
These are intended as helpers for direct I/O implementation, so I very much expected them to eventually get exported.
Miklos Szeredi
maintainerQuoted 6 lines · David Howells
(2)
invalidate_inode_pages2()is used in some places to effect invalidation of the pagecache in the case where the server tells us that a third party modified the server copy of a file. What the right behaviour should be here, I'm not sure, but at the moment, any dirty data will get laundered back to the server. Possibly it should be simply invalidated locally or the user asked how they want to handle the divergence.
Skipping ->launder_page will mean there's a window where the data *will* be lost, AFAICS.
Of course concurrent cached writes on different hosts against the same region (the size of which depends on how the caching is done) will conflict.
But if concurrent writes are to different regions, then they shouldn't be lost, no? Without the current ->launder_page thing I don't see how that could be guaranteed.
Thanks,
Miklos
David Howells
maintainerQuoted 8 lines · Miklos Szeredi
Quoted 7 lines
(2)
invalidate_inode_pages2()is used in some places to effect invalidation of the pagecache in the case where the server tells us that a third party modified the server copy of a file. What the right behaviour should be here, I'm not sure, but at the moment, any dirty data will get laundered back to the server. Possibly it should be simply invalidated locally or the user asked how they want to handle the divergence.
Quoted 6 lines
Skipping ->launder_page will mean there's a window where the data *will* be lost, AFAICS.
Of course concurrent cached writes on different hosts against the same region (the size of which depends on how the caching is done) will conflict.
Indeed. Depending on when you're using invalidate_inode_pages2() and co. and what circumstances you're using it for, you *are* going to suffer data loss.
For instance, if you have dirty data on the local host and get an invalidation notification from the server: if you write just your dirty data back, you may corrupt the file on the server, losing the third party changes; if you write back your entire copy of the file, you might avoid corrupting the file, but completely obliterate the third party changes; if you discard your changes, you lose those instead, but save the third party changes.
I'm working towards supporting disconnected operation where I'll need to add some sort of user interaction mechanism that will allow the user to say how they want to handle this.
Quoted 3 lines
But if concurrent writes are to different regions, then they shouldn't be lost, no? Without the current ->launder_page thing I don't see how that could be guaranteed.
Define "different regions". If they're not on the same folios, then why would they be lost by simply flushing the data before doing the invalidation? If they are on different parts of the same folio, all the above still apply when you flush the whole folio.
Now, you can mitigate the latter case by keeping track of which bytes changed, but that still allows you to corrupt the file by writing back just your particular changes.
And then there's the joker in the deck: mmap. The main advantage of invalidate_inode_pages2() is that it forcibly unmaps the page before laundering it. However, this doesn't prevent you then corrupting the upstream copy by writing the changes back.
What particular usage case of invalidate_inode_pages2() are you thinking of?
DIO read/write can only be best effort: flush, invalidate then do the DIO which may bring the buffers back in because they're mmapped. In which case doing a flush and a non-laundering invalidate that leaves dirty pages in place ought to be fine.
David
Miklos Szeredi
maintainerQuoted 1 line · David Howells
What particular usage case of
invalidate_inode_pages2()are you thinking of?
FUSE_NOTIFY_INVAL_INODE will trigger invalidate_inode_pages2_range() to clean up the cache.
The server is free to discard writes resulting from this invalidation and delay reads in the region until the invalidation finishes. This would no longer work with your change, since the mapping could silently be reinstated between the writeback and the removal from the cache due to the page being unlocked/relocked.
I'm not saying such a filesystem actually exists, but it's a theoretical possibility. And maybe there are cases which I haven't thought of.
Thanks,
Miklos
Miklos Szeredi
maintainerQuoted 5 lines · Miklos Szeredi
Quoted 1 line · David Howells
What particular usage case of
invalidate_inode_pages2()are you thinking of?
Quoted 8 lines
FUSE_NOTIFY_INVAL_INODEwill triggerinvalidate_inode_pages2_range()to clean up the cache.The server is free to discard writes resulting from this invalidation and delay reads in the region until the invalidation finishes. This would no longer work with your change, since the mapping could silently be reinstated between the writeback and the removal from the cache due to the page being unlocked/relocked.
This would also matter if a distributed filesystem wanted to implement
coherence even if there are mmaps. I.e. a client could get exclusive
access to a region by issuing FUSE_NOTIFY_INVAL_INODE on all other clients and blocking reads. With your change this would fail.
Again, this is purely theoretical, and without a way to differentiate between the read-only and write cases it has limited usefulness. Adding leases to fuse (which I plan to do) would make this much more useful.
Thanks,
Miklos
Quoted 20 lines · Miklos Szeredi
Quoted 5 lines · Miklos Szeredi
Quoted 1 line · David Howells
What particular usage case of
invalidate_inode_pages2()are you thinking of?Quoted 8 lines
FUSE_NOTIFY_INVAL_INODEwill triggerinvalidate_inode_pages2_range()to clean up the cache.The server is free to discard writes resulting from this invalidation and delay reads in the region until the invalidation finishes. This would no longer work with your change, since the mapping could silently be reinstated between the writeback and the removal from the cache due to the page being unlocked/relocked.
This would also matter if a distributed filesystem wanted to implement
coherence even if there are mmaps. I.e. a client could get exclusive
access to a region by issuingFUSE_NOTIFY_INVAL_INODEon all other clients and blocking reads. With your change this would fail.
Quoted 4 lines
Again, this is purely theoretical, and without a way to differentiate between the read-only and write cases it has limited usefulness. Adding leases to fuse (which I plan to do) would make this much more useful.
Thanks Miklos! Fyi, we are actually planning to extend fuse notifications from inode to page ranges.
Thanks,
Bernd
David Howells
maintainerRFC PATCH v2 1/1 mm: Kill ->launder_folio()
In reply to David Howellsinvalidate_inode_pages2_range() and its wrappers are frequently used to invalidate overlapping folios prior to and after doing direct I/O. This calls ->launder_folio() to flush dirty folios out to the backing store, keeping the folio lock across the I/O - presumably to prevent the folio from being redirtied and thereby prevent it from being removed.
However... If we're doing this prior to doing DIO on a file, there may be nothing preventing an mmapped write from recreating and redirtying the folio the moment it is removed from the mapping lest the kernel deadlock on doing DIO to/from a buffer mmapped from the target file.
Further, invalidate_inode_pages2_range() is permitted to fail - and half the callers don't even check to see if it *did* fail, probably not unreasonably.
In which case, there's no point doing the laundry there; better to call something like filemap_fdatawrite() beforehand. If mmap is going to interfere, we can't stop it.
There are some other cases in which this is used:
(1) In fuse_do_setattr(), when the size of a file is changed. Calling invalidate_inode_pages2() here is probably the wrong thing to do as the preceding truncate_pagecache() should do the appropriate page trimming and this would just seem to reduce performance for no good reason.
(2) In some network filesystems, when the server informs the client of a third-party modification to a file, the local pagecache is zapped with invalidate_inode_pages2() rather than invalidate_remote_inode(). The former writes back the dirty data whereas the latter retains it plus surrounding obsolete data in the same folio. Maybe this should be done by filemap_fdatawrite() followed by invalidate_inode_pages2().
Possibly, ->page_mkwrite() could be used to hold off mmap writes until remote invalidation-induced writeback is achieved.
(3) In NFS, this is used to attempt to save the data when some sort of fatal error occurs. It may be sufficient to do a filemap_fdatawrite() before calling invalidate_inode_pages2(). nfs_writepages() can observe the error state and do the laundering thing. Again, maybe, ->page_mkwrite() could be used to hold off mmap writes until the pagecache has been invalidated.
Note that this only affects 9p, afs, cifs, fuse, nfs and orangefs.
--- Documentation/filesystems/locking.rst | 8 -- Documentation/filesystems/vfs.rst | 6 - fs/9p/vfs_addr.c | 2 fs/afs/file.c | 1 fs/afs/internal.h | 1 fs/afs/write.c | 10 -- fs/ceph/file.c | 6 - fs/fuse/file.c | 16 ---- fs/netfs/buffered_write.c | 74 -------------------- fs/netfs/main.c | 1 fs/nfs/file.c | 23 ------ fs/nfs/inode.c | 4 - fs/nfs/nfstrace.h | 1 fs/orangefs/inode.c | 1 fs/smb/client/file.c | 122 ---------------------------------- include/linux/fs.h | 1 include/linux/netfs.h | 2 include/trace/events/netfs.h | 3 mm/truncate.c | 25 +----- 19 files changed, 14 insertions(+), 293 deletions(-)
Documentation/filesystems/locking.rst +0 -8
@@ -262,7 +262,6 @@ prototypes:: int (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct address_space *, struct folio *dst, struct folio *src, enum migrate_mode); - int (*launder_folio)(struct folio *); bool (*is_partially_uptodate)(struct folio *, size_t from, size_t count); int (*error_remove_folio)(struct address_space *, struct folio *); int (*swap_activate)(struct swap_info_struct *sis, struct file *f, sector_t *span)
@@ -288,7 +287,6 @@ release_folio: yes free_folio: yes direct_IO: migrate_folio: yes (both) -launder_folio: yes is_partially_uptodate: yes error_remove_folio: yes swap_activate: no
@@ -394,12 +392,6 @@ try_to_free_buffers(). ->free_folio() is called when the kernel has dropped the folio from the page cache. -->launder_folio() may be called prior to releasing a folio if -it is still found to be dirty. It returns zero if the folio was successfully -cleaned, or an error value if not. Note that in order to prevent the folio -getting mapped back in and redirtied, it needs to be kept locked -across the entire operation. - ->swap_activate() will be called to prepare the given file for swap. It should perform any validation and preparation necessary to ensure that writes can be performed with minimal memory allocation. It should call
Documentation/filesystems/vfs.rst +0 -6
@@ -818,7 +818,6 @@ cache in your filesystem. The following members are defined: ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct mapping *, struct folio *dst, struct folio *src, enum migrate_mode); - int (*launder_folio) (struct folio *); bool (*is_partially_uptodate) (struct folio *, size_t from, size_t count);
@@ -1012,11 +1011,6 @@ cache in your filesystem. The following members are defined: folio to this function. migrate_folio should transfer any private data across and update any references that it has to the folio. -``launder_folio`` - Called before freeing a folio - it writes back the dirty folio. - To prevent redirtying the folio, it is kept locked during the - whole operation. - ``is_partially_uptodate`` Called by the VM when reading a file through the pagecache when the underlying blocksize is smaller than the size of the folio.
fs/9p/vfs_addr.c +0 -2
@@ -89,7 +89,6 @@ static int v9fs_init_request(struct netfs_io_request *rreq, struct file *file)bool writing = (rreq->origin ==NETFS_READ_FOR_WRITE || rreq->origin ==NETFS_WRITEBACK || rreq->origin ==NETFS_WRITETHROUGH || - rreq->origin ==NETFS_LAUNDER_WRITE || rreq->origin ==NETFS_UNBUFFERED_WRITE || rreq->origin ==NETFS_DIO_WRITE );
@@ -141,7 +140,6 @@ const struct address_space_operations v9fs_addr_operations = { .dirty_folio = netfs_dirty_folio, .release_folio = netfs_release_folio, .invalidate_folio = netfs_invalidate_folio, - .launder_folio = netfs_launder_folio, .direct_IO = noop_direct_IO, .writepages = netfs_writepages, };
fs/afs/file.c +0 -1
@@ -54,7 +54,6 @@ const struct address_space_operations afs_file_aops = { .read_folio = netfs_read_folio, .readahead = netfs_readahead, .dirty_folio = netfs_dirty_folio, - .launder_folio = netfs_launder_folio, .release_folio = netfs_release_folio, .invalidate_folio = netfs_invalidate_folio, .migrate_folio = filemap_migrate_folio,
fs/afs/internal.h +0 -1
@@ -916,7 +916,6 @@ struct afs_operation {loff_t pos;loff_t size;loff_t i_size; -bool laundering;/* Laundering page, PG_writeback not set */ } store;struct {struct iattr *attr;
fs/afs/write.c +3 -7
@@ -75,8 +75,7 @@ static void afs_store_data_success(struct afs_operation *op) op->ctime = op->file[0 ].scb.status.mtime_client;afs_vnode_commit_status (op, &op->file[0 ]);if (!afs_op_error (op)) { -if (!op->store.laundering) -afs_pages_written_back (vnode, op->store.pos, op->store.size); +afs_pages_written_back (vnode, op->store.pos, op->store.size);afs_stat_v (vnode, n_stores);atomic_long_add (op->store.size, &afs_v2net (vnode)->n_store_bytes); }
@@ -91,8 +90,7 @@ static const struct afs_operation_ops afs_store_data_operation = {/* * write to a file */ -static int afs_store_data (struct afs_vnode *vnode,struct iov_iter *iter,loff_t pos, -bool laundering) +static int afs_store_data (struct afs_vnode *vnode,struct iov_iter *iter,loff_t pos) {struct afs_operation *op;struct afs_wb_key *wbk =NULL ;
@@ -123,7 +121,6 @@ static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t op->file[0 ].modification = true; op->store.pos = pos; op->store.size = size; - op->store.laundering = laundering; op->flags |=AFS_OPERATION_UNINTR ; op->ops = &afs_store_data_operation;
@@ -168,8 +165,7 @@ static void afs_upload_to_server(struct netfs_io_subrequest *subreq) subreq->rreq->debug_id, subreq->debug_index, subreq->io_iter.count);trace_netfs_sreq (subreq, netfs_sreq_trace_submit); - ret =afs_store_data (vnode, &subreq->io_iter, subreq->start, - subreq->rreq->origin ==NETFS_LAUNDER_WRITE ); + ret =afs_store_data (vnode, &subreq->io_iter, subreq->start);netfs_write_subrequest_terminated (subreq, ret <0 ? ret : subreq->len, false); }
fs/ceph/file.c +2 -4
@@ -1450,11 +1450,9 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,ceph_fscache_invalidate (inode, true); - ret2 =invalidate_inode_pages2_range (inode->i_mapping, - pos >>PAGE_SHIFT , - (pos + count -1 ) >>PAGE_SHIFT ); + ret2 =kiocb_invalidate_pages (iocb, count);if (ret2 <0 ) -doutc (cl,"invalidate_inode_pages2_range returned %d\n" , +doutc (cl,"kiocb_invalidate_pages returned %d\n" , ret2); flags =/* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE ;
fs/fuse/file.c +0 -16
@@ -2393,21 +2393,6 @@ static int fuse_write_end(struct file *file, struct address_space *mapping,return copied; } -static int fuse_launder_folio (struct folio *folio) -{ -int err =0 ; -if (folio_clear_dirty_for_io (folio)) { -struct inode *inode = folio->mapping->host; - -/* Serialize with pending writeback for the same page */ -fuse_wait_on_page_writeback (inode, folio->index); - err =fuse_writepage_locked (&folio->page); -if (!err) -fuse_wait_on_page_writeback (inode, folio->index); - } -return err; -} -/* * Write back dirty data/metadata now (there may not be any suitable * open files later for data)
@@ -3227,7 +3212,6 @@ static const struct address_space_operations fuse_file_aops = { .readahead = fuse_readahead, .writepage = fuse_writepage, .writepages = fuse_writepages, - .launder_folio = fuse_launder_folio, .dirty_folio = filemap_dirty_folio, .bmap = fuse_bmap, .direct_IO = fuse_direct_IO,
fs/netfs/buffered_write.c +0 -74
77 lines not on this page · raw message
fs/netfs/main.c +0 -1
@@ -33,7 +33,6 @@ static const char *netfs_origins[nr__netfs_io_origin] = { [NETFS_READ_FOR_WRITE] = "RW", [NETFS_WRITEBACK] = "WB", [NETFS_WRITETHROUGH] = "WT", - [NETFS_LAUNDER_WRITE] = "LW", [NETFS_UNBUFFERED_WRITE] = "UW", [NETFS_DIO_READ] = "DR", [NETFS_DIO_WRITE] = "DW",
fs/nfs/file.c +0 -23
@@ -484,28 +484,6 @@ static void nfs_check_dirty_writeback(struct folio *folio, *dirty = true; } -/* -* Attempt to clear the private state associated with a page when an error -* occurs that requires the cached contents of an inode to be written back or -* destroyed -* - Called if either PG_private or fscache is set on the page -* - Caller holds page lock -* - Return 0 if successful, -error otherwise -*/ -static int nfs_launder_folio (struct folio *folio) -{ -struct inode *inode = folio->mapping->host; -int ret; - -dfprintk (PAGECACHE ,"NFS: launder_folio(%ld, %llu)\n" , - inode->i_ino,folio_pos (folio)); - -folio_wait_fscache (folio); - ret =nfs_wb_folio (inode, folio); -trace_nfs_launder_folio_done (inode, folio, ret); -return ret; -} -static int nfs_swap_activate (struct swap_info_struct *sis,struct file *file,sector_t *span) {
@@ -564,7 +542,6 @@ const struct address_space_operations nfs_file_aops = { .invalidate_folio = nfs_invalidate_folio, .release_folio = nfs_release_folio, .migrate_folio = nfs_migrate_folio, - .launder_folio = nfs_launder_folio, .is_dirty_writeback = nfs_check_dirty_writeback, .error_remove_folio = generic_error_remove_folio, .swap_activate = nfs_swap_activate,
fs/nfs/inode.c +3 -1
@@ -1162,8 +1162,10 @@ void nfs_file_clear_open_context(struct file *filp)* We fatal error on write before. Try to writeback * every page again. */ -if (ctx->error <0 ) +if (ctx->error <0 ) { +filemap_fdatawrite (inode->i_mapping);invalidate_inode_pages2 (inode->i_mapping); + } filp->private_data =NULL ;put_nfs_open_context_sync (ctx); }
fs/nfs/nfstrace.h +0 -1
@@ -1039,7 +1039,6 @@ DEFINE_NFS_FOLIO_EVENT(nfs_writeback_folio);DEFINE_NFS_FOLIO_EVENT_DONE (nfs_writeback_folio_done);DEFINE_NFS_FOLIO_EVENT (nfs_invalidate_folio); -DEFINE_NFS_FOLIO_EVENT_DONE (nfs_launder_folio_done); TRACE_EVENT(nfs_aop_readahead, TP_PROTO(
fs/orangefs/inode.c +0 -1
@@ -626,7 +626,6 @@ static const struct address_space_operations orangefs_address_operations = { .invalidate_folio = orangefs_invalidate_folio, .release_folio = orangefs_release_folio, .free_folio = orangefs_free_folio, - .launder_folio = orangefs_launder_folio, .direct_IO = orangefs_direct_IO, };
fs/smb/client/file.c +0 -122
151 lines not on this page · raw message
include/linux/fs.h +0 -1
@@ -432,7 +432,6 @@ struct address_space_operations {*/ int (*migrate_folio)(struct address_space *,struct folio *dst,struct folio *src,enum migrate_mode ); -int (*launder_folio)(struct folio *);bool (*is_partially_uptodate) (struct folio *,size_t from,size_t count);void (*is_dirty_writeback) (struct folio *,bool *dirty,bool *wb);
include/linux/netfs.h +0 -2
@@ -232,7 +232,6 @@ enum netfs_io_origin {NETFS_READ_FOR_WRITE ,/* This read is to prepare a write */ NETFS_WRITEBACK ,/* This write was triggered by writepages */ NETFS_WRITETHROUGH ,/* This write was made by netfs_perform_write() */ -NETFS_LAUNDER_WRITE ,/* This is triggered by ->launder_folio() */ NETFS_UNBUFFERED_WRITE ,/* This is an unbuffered write */ NETFS_DIO_READ ,/* This is a direct I/O read */ NETFS_DIO_WRITE, /* This is a direct I/O write */
@@ -410,7 +409,6 @@ int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);void netfs_clear_inode_writeback (struct inode *inode,const void *aux);void netfs_invalidate_folio (struct folio *folio,size_t offset,size_t length);bool netfs_release_folio (struct folio *folio,gfp_t gfp); -int netfs_launder_folio (struct folio *folio);/* VMA operations API. */ vm_fault_t netfs_page_mkwrite (struct vm_fault *vmf,struct netfs_group *netfs_group);
include/trace/events/netfs.h +0 -3
@@ -25,7 +25,6 @@#define netfs_write_traces \ EM(netfs_write_trace_dio_write, "DIO-WRITE") \ - EM(netfs_write_trace_launder, "LAUNDER ") \ EM(netfs_write_trace_unbuffered_write, "UNB-WRITE") \ EM(netfs_write_trace_writeback, "WRITEBACK") \ E_(netfs_write_trace_writethrough, "WRITETHRU")
@@ -36,7 +35,6 @@ EM(NETFS_READ_FOR_WRITE, "RW") \ EM(NETFS_WRITEBACK, "WB") \ EM(NETFS_WRITETHROUGH, "WT") \ - EM(NETFS_LAUNDER_WRITE, "LW") \ EM(NETFS_UNBUFFERED_WRITE, "UW") \ EM(NETFS_DIO_READ, "DR") \E_ (NETFS_DIO_WRITE ,"DW" )
@@ -131,7 +129,6 @@ EM(netfs_folio_trace_end_copy, "end-copy") \ EM(netfs_folio_trace_filled_gaps, "filled-gaps") \ EM(netfs_folio_trace_kill, "kill") \ - EM(netfs_folio_trace_launder, "launder") \ EM(netfs_folio_trace_mkwrite, "mkwrite") \ EM(netfs_folio_trace_mkwrite_plus, "mkwrite+") \EM (netfs_folio_trace_read_gaps,"read-gaps" ) \
mm/truncate.c +6 -19
@@ -561,10 +561,10 @@ static int invalidate_complete_folio2(struct address_space *mapping, struct folio *folio) {if (folio->mapping != mapping) -return 0 ; +return -EBUSY ;if (!filemap_release_folio (folio,GFP_KERNEL )) -return 0 ; +return -EBUSY ;spin_lock (&mapping->host->i_lock);xa_lock_irq (&mapping->i_pages);
@@ -579,20 +579,11 @@ static int invalidate_complete_folio2(struct address_space *mapping,spin_unlock (&mapping->host->i_lock);filemap_free_folio (mapping, folio); -return 1 ; +return 0 ; failed:xa_unlock_irq (&mapping->i_pages);spin_unlock (&mapping->host->i_lock); -return 0 ; -} - -static int folio_launder (struct address_space *mapping,struct folio *folio) -{ -if (!folio_test_dirty (folio)) -return 0 ; -if (folio->mapping != mapping || mapping->a_ops->launder_folio ==NULL ) -return 0 ; -return mapping->a_ops->launder_folio (folio); +return -EBUSY ; }/**
@@ -657,12 +648,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,unmap_mapping_folio (folio);BUG_ON (folio_mapped (folio)); - ret2 =folio_launder (mapping, folio); -if (ret2 ==0 ) { -if (!invalidate_complete_folio2 (mapping, folio)) - ret2 = -EBUSY ; - } -if (ret2 <0 ) + ret2 =invalidate_complete_folio2 (mapping, folio); +if (ret2) ret = ret2;folio_unlock (folio); }