Prevent ->map_pages from sleeping
Subsystems (7)
Matthew Wilcox (Oracle)
maintainerv2 cover Prevent ->map_pages from sleeping
In preparation for a larger patch series which will handle (some, easy) page faults protected only by RCU, change the two filesystems which have sleeping locks to not take them and hold the RCU lock around calls to ->map_page to prevent other filesystems from adding sleeping locks.
v2:
- Add tags from David Howells
- Go into more detail about the locking in the XFS patch
Matthew Wilcox (Oracle) (3):
xfs: Remove xfs_filemap_map_pages() wrapper
afs: Split afs_pagecache_valid() out of afs_validate()
mm: Hold the RCU read lock over calls to ->map_pages
Documentation/filesystems/locking.rst | 4 ++-- fs/afs/file.c | 14 ++------------ fs/afs/inode.c | 27 +++++++++++++++++++-------- fs/afs/internal.h | 1 + fs/xfs/xfs_file.c | 17 +---------------- mm/memory.c | 11 ++++++++--- 6 files changed, 33 insertions(+), 41 deletions(-)
Signature · 1 line
2.39.2
Matthew Wilcox (Oracle)
maintainerPATCH v2 1/3 xfs: Remove xfs_filemap_map_pages() wrapper
XFS doesn't actually need to be holding the XFS_MMAPLOCK_SHARED to do this. filemap_map_pages() cannot bring new folios into the page cache and the folio lock is taken during filemap_map_pages() which provides sufficient protection against a truncation or hole punch.
--- fs/xfs/xfs_file.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-)
fs/xfs/xfs_file.c +1 -16
@@ -1389,25 +1389,10 @@ xfs_filemap_pfn_mkwrite(return __xfs_filemap_fault (vmf,PE_SIZE_PTE , true); } -static vm_fault_t -xfs_filemap_map_pages ( -struct vm_fault *vmf, -pgoff_t start_pgoff, -pgoff_t end_pgoff) -{ -struct inode *inode =file_inode (vmf->vma->vm_file); -vm_fault_t ret; - -xfs_ilock (XFS_I (inode),XFS_MMAPLOCK_SHARED ); - ret =filemap_map_pages (vmf, start_pgoff, end_pgoff); -xfs_iunlock (XFS_I (inode),XFS_MMAPLOCK_SHARED ); -return ret; -} -static const struct vm_operations_struct xfs_file_vm_ops = { .fault = xfs_filemap_fault, .huge_fault = xfs_filemap_huge_fault, - .map_pages = xfs_filemap_map_pages, + .map_pages = filemap_map_pages, .page_mkwrite = xfs_filemap_page_mkwrite, .pfn_mkwrite = xfs_filemap_pfn_mkwrite, };
Signature · 1 line
2.39.2
Re: [PATCH v2 1/3] xfs: Remove xfs_filemap_map_pages() wrapper
Quoted 43 lines · Matthew Wilcox (Oracle)
XFS doesn't actually need to be holding the
XFS_MMAPLOCK_SHAREDto do this.filemap_map_pages()cannot bring new folios into the page cache and the folio lock is taken duringfilemap_map_pages()which provides sufficient protection against a truncation or hole punch.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>--- fs/xfs/xfs_file.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-)fs/xfs/xfs_file.c +1
-16@@ -1389,25 +1389,10 @@ xfs_filemap_pfn_mkwrite(return __xfs_filemap_fault (vmf,PE_SIZE_PTE , true); } -static vm_fault_t -xfs_filemap_map_pages ( -struct vm_fault *vmf, -pgoff_t start_pgoff, -pgoff_t end_pgoff) -{ -struct inode *inode =file_inode (vmf->vma->vm_file); -vm_fault_t ret; - -xfs_ilock (XFS_I (inode),XFS_MMAPLOCK_SHARED ); - ret =filemap_map_pages (vmf, start_pgoff, end_pgoff); -xfs_iunlock (XFS_I (inode),XFS_MMAPLOCK_SHARED ); -return ret; -} -static const struct vm_operations_struct xfs_file_vm_ops = { .fault = xfs_filemap_fault, .huge_fault = xfs_filemap_huge_fault, - .map_pages = xfs_filemap_map_pages, + .map_pages = filemap_map_pages, .page_mkwrite = xfs_filemap_page_mkwrite, .pfn_mkwrite = xfs_filemap_pfn_mkwrite, };Signature · 1 line
2.39.2
Looks fine.
Signature · 2 lines
Dave Chinner [email protected]
Matthew Wilcox (Oracle)
maintainerPATCH v2 2/3 afs: Split afs_pagecache_valid() out of afs_validate()
In reply to Matthew Wilcox (Oracle)For the map_pages() method, we need a test that does not sleep. The page fault handler will continue to call the fault() method where we can sleep and do the full revalidation there.
--- fs/afs/file.c | 14 ++------------ fs/afs/inode.c | 27 +++++++++++++++++++-------- fs/afs/internal.h | 1 + 3 files changed, 22 insertions(+), 20 deletions(-)
fs/afs/file.c +2 -12
@@ -569,20 +569,10 @@ static void afs_vm_close(struct vm_area_struct *vma)static vm_fault_t afs_vm_map_pages (struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff) {struct afs_vnode *vnode =AFS_FS_I (file_inode (vmf->vma->vm_file)); -struct afs_file *af = vmf->vma->vm_file->private_data; -switch (afs_validate (vnode, af->key)) { -case 0 : +if (afs_pagecache_valid (vnode))return filemap_map_pages (vmf, start_pgoff, end_pgoff); -case -ENOMEM : -return VM_FAULT_OOM ; -case -EINTR : -case -ERESTARTSYS : -return VM_FAULT_RETRY ; -case -ESTALE : -default : -return VM_FAULT_SIGBUS ; - } +return 0 ; }static ssize_t afs_file_read_iter (struct kiocb *iocb,struct iov_iter *iter)
fs/afs/inode.c +19 -8
@@ -667,6 +667,24 @@ bool afs_check_validity(struct afs_vnode *vnode)return false; } +/* +* Returns true if the pagecache is still valid. Does not sleep. +*/ +bool afs_pagecache_valid (struct afs_vnode *vnode) +{ +if (unlikely (test_bit (AFS_VNODE_DELETED , &vnode->flags))) { +if (vnode->netfs.inode.i_nlink) +clear_nlink (&vnode->netfs.inode); +return true; + } + +if (test_bit (AFS_VNODE_CB_PROMISED , &vnode->flags) && +afs_check_validity (vnode)) +return true; + +return false; +} +/* * validate a vnode/inode * - there are several things we need to check
@@ -684,14 +702,7 @@ int afs_validate(struct afs_vnode *vnode, struct key *key) vnode->fid.vid, vnode->fid.vnode, vnode->flags, key_serial(key)); -if (unlikely (test_bit (AFS_VNODE_DELETED , &vnode->flags))) { -if (vnode->netfs.inode.i_nlink) -clear_nlink (&vnode->netfs.inode); - goto valid; - } - -if (test_bit (AFS_VNODE_CB_PROMISED , &vnode->flags) && -afs_check_validity (vnode)) +if (afs_pagecache_valid (vnode)) goto valid;down_write (&vnode->validate_lock);
fs/afs/internal.h +1 -0
@@ -1171,6 +1171,7 @@ extern struct inode *afs_iget(struct afs_operation *, struct afs_vnode_param *);extern struct inode *afs_root_iget (struct super_block *,struct key *);extern bool afs_check_validity (struct afs_vnode *);extern int afs_validate (struct afs_vnode *,struct key *); +bool afs_pagecache_valid (struct afs_vnode *);extern int afs_getattr (struct mnt_idmap *idmap,const struct path *,struct kstat *,u32 ,unsigned int );extern int afs_setattr (struct mnt_idmap *idmap,struct dentry *,struct iattr *);
Signature · 1 line
2.39.2
Matthew Wilcox (Oracle)
maintainerPATCH v2 3/3 mm: Hold the RCU read lock over calls to ->map_pages
In reply to Matthew Wilcox (Oracle)Prevent filesystems from doing things which sleep in their map_pages method. This is in preparation for a pagefault path protected only by RCU.
--- Documentation/filesystems/locking.rst | 4 ++-- mm/memory.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-)
Documentation/filesystems/locking.rst +2 -2
@@ -645,7 +645,7 @@ ops mmap_lock PageLocked(page) open: yes close: yes fault: yes can return with page locked -map_pages: yes +map_pages: read page_mkwrite: yes can return with page locked pfn_mkwrite: yes access: yes
@@ -661,7 +661,7 @@ locked. The VM will unlock the page. ->map_pages() is called when VM asks to map easy accessible pages. Filesystem should find and map pages associated with offsets from "start_pgoff" -till "end_pgoff". ->map_pages() is called with page table locked and must +till "end_pgoff". ->map_pages() is called with the RCU lock held and must not block. If it's not possible to reach a page without blocking, filesystem should skip it. Filesystem should use set_pte_range() to setup page table entry. Pointer to entry associated with the page is passed in
mm/memory.c +8 -3
@@ -4461,6 +4461,7 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)/* The page offset of vmf->address within the VMA. */ pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff;pgoff_t from_pte, to_pte; +vm_fault_t ret;/* The PTE offset of the start address, clamped to the VMA. */ from_pte = max(ALIGN_DOWN(pte_off, nr_pages),
@@ -4476,9 +4477,13 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)return VM_FAULT_OOM ; } -return vmf->vma->vm_ops->map_pages (vmf, - vmf->pgoff + from_pte - pte_off, - vmf->pgoff + to_pte - pte_off); +rcu_read_lock (); + ret = vmf->vma->vm_ops->map_pages (vmf, + vmf->pgoff + from_pte - pte_off, + vmf->pgoff + to_pte - pte_off); +rcu_read_unlock (); + +return ret; }/* Return true if we should do read fault-around, false otherwise */
Signature · 1 line
2.39.2
Re: [PATCH v2 3/3] mm: Hold the RCU read lock over calls to ->map_pages
Quoted 48 lines · Matthew Wilcox (Oracle)
Prevent filesystems from doing things which sleep in their
map_pagesmethod. This is in preparation for a pagefault path protected only by RCU.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>--- Documentation/filesystems/locking.rst | 4 ++-- mm/memory.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-)Documentation/filesystems/locking.rst +2
-2@@ -645,7 +645,7 @@ ops mmap_lock PageLocked(page) open: yes close: yes fault: yes can return with page locked -map_pages: yes +map_pages: read page_mkwrite: yes can return with page locked pfn_mkwrite: yes access: yes@@ -661,7 +661,7 @@ locked. The VM will unlock the page. ->map_pages() is called when VM asks to map easy accessible pages. Filesystem should find and map pages associated with offsets from "start_pgoff" -till "end_pgoff". ->map_pages() is called with page table locked and must +till "end_pgoff". ->map_pages() is called with the RCU lock held and must not block. If it's not possible to reach a page without blocking, filesystem should skip it. Filesystem should use set_pte_range() to setup page table entry. Pointer to entry associated with the page is passed inmm/memory.c +1
-0@@ -4461,6 +4461,7 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)/* The page offset of vmf->address within the VMA. */ pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff;pgoff_t from_pte, to_pte; +vm_fault_t ret;/* The PTE offset of the start address, clamped to the VMA. */ from_pte = max(ALIGN_DOWN(pte_off, nr_pages),@@ -4476,9 +4477,13 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)return VM_FAULT_OOM ; }
mm/memory.c static vm_fault_t do_fault_around(struct vm_fault *vmf) · line 4486
Quoted 10 lines
mm/memory.c +7
-3-return vmf->vma->vm_ops->map_pages (vmf, - vmf->pgoff + from_pte - pte_off, - vmf->pgoff + to_pte - pte_off); +rcu_read_lock (); + ret = vmf->vma->vm_ops->map_pages (vmf, + vmf->pgoff + from_pte - pte_off, + vmf->pgoff + to_pte - pte_off); +rcu_read_unlock (); + +return ret;
Doesn't this mean that the rcu_read_lock/unlock can be removed from filemap_map_pages()? i.e. all callers are now already under rcu_read_lock(). Maybe WARN_ON_ONCE(!rcu_read_lock_held()) could be put in filemap_map_pages() if you are worried about callers not holding it...
Otherwise it looks fine.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Matthew Wilcox
maintainerRe: [PATCH v2 3/3] mm: Hold the RCU read lock over calls to ->map_pages
Quoted 17 lines · Dave Chinner
mm/memory.c static vm_fault_t do_fault_around(struct vm_fault *vmf) · line 4486
Quoted 10 lines · Matthew Wilcox (Oracle)
Prevent filesystems from doing things which sleep in their
map_pagesmethod. This is in preparation for a pagefault path protected only by RCU.mm/memory.c +7
-0+rcu_read_lock (); + ret = vmf->vma->vm_ops->map_pages (vmf, + vmf->pgoff + from_pte - pte_off, + vmf->pgoff + to_pte - pte_off); +rcu_read_unlock (); + +return ret;Doesn't this mean that the rcu_read_lock/unlock can be removed from
filemap_map_pages()? i.e. all callers are now already underrcu_read_lock(). Maybe WARN_ON_ONCE(!rcu_read_lock_held()) could be put infilemap_map_pages()if you are worried about callers not holding it...
Yes, it could now be removed. I wasn't too bothered because it's so cheap (either a noop, or an inc/dec of a CPU-local variable). I don't think we need the WARN becaause there's one embedded in the XArray code (must be holding the spinlock or the RCU read lock to iterate the XArray).