gfs2: Fix mmap + page fault deadlocks
Subsystems (22)
Andreas Gruenbacher
maintainerv5 cover gfs2: Fix mmap + page fault deadlocks
Hi all,
here's another update on top of v5.14-rc4. There seems to be a bug in get_user_pages_fast when called with FOLL_FAST_ONLY; please see below.
Changes:
* Change fault_in_pages_{readable,writeable} to return the number of bytes that should be accessible instead of failing outright when part of the requested region cannot be faulted in. Change iov_iter_fault_in_readable to those same semantics.
* Add fault_in_iov_iter_writeable for safely faulting in pages for writing without modifying the pages.
With this patch queue, fstest generic/208 (aio-dio-invalidate-failure.c) endlessly spins in gfs2_file_direct_write. It looks as if there's a bug in get_user_pages_fast when called with FOLL_FAST_ONLY:
(1) The test case performs an aio write into a 32 MB buffer.
(2) The buffer is initially not in memory, so when iomap_dio_rw() -> ... -> bio_iov_iter_get_pages() is called with the iter->noio flag set, we get to get_user_pages_fast() with FOLL_FAST_ONLY set. get_user_pages_fast() returns 0, which causes bio_iov_iter_get_pages to return -EFAULT.
(3) Then gfs2_file_direct_write faults in the entire buffer with fault_in_iov_iter_readable(), which succeeds.
(4) With the buffer in memory, we retry the iomap_dio_rw() -> ... -> bio_iov_iter_get_pages() -> ... -> get_user_pages_fast(). This should succeed now, but get_user_pages_fast() still returns 0.
(5) Thus we end up in step (3) again.
The buffered writes generic/208 performs are unrelated to this hang.
Apart from the generic/208 hang, gfs2 still needs a better strategy for faulting in more reasonable chunks of memory at a time and for resuming requests after faulting in pages. We've got some of the pieces in place for safely allowing that, but more work remains to be done.
For immediate consideration by Al Viro:
iov_iter: Fix iov_iter_get_pages{,_alloc} page fault return value
For immediate consideration by Paul Mackerras:
powerpc/kvm: Fix kvm_use_magic_page
Thanks,
Andreas
Andreas Gruenbacher (12):
iov_iter: Fix iov_iter_get_pages{,_alloc} page fault return value powerpc/kvm: Fix kvm_use_magic_page
Turn fault_in_pages_{readable,writeable} into
fault_in_{readable,writeable} Turn iov_iter_fault_in_readable into fault_in_iov_iter_readable
iov_iter: Introduce fault_in_iov_iter_writeable
gfs2: Add wrapper for iomap_file_buffered_write
gfs2: Fix mmap + page fault deadlocks for buffered I/O
iomap: Fix iomap_dio_rw return value for user copies
iomap: Support restarting direct I/O requests after user copy failures
iomap: Add done_before argument to iomap_dio_rw
iov_iter: Introduce noio flag to disable page faults
gfs2: Fix mmap + page fault deadlocks for direct I/O
arch/powerpc/kernel/kvm.c | 3 +- arch/powerpc/kernel/signal_32.c | 4 +- arch/powerpc/kernel/signal_64.c | 2 +- arch/x86/kernel/fpu/signal.c | 8 +- drivers/gpu/drm/armada/armada_gem.c | 7 +- fs/btrfs/file.c | 8 +- fs/btrfs/ioctl.c | 7 +- fs/ext4/file.c | 5 +- fs/f2fs/file.c | 6 +- fs/fuse/file.c | 2 +- fs/gfs2/file.c | 95 ++++++++++++++++++++--- fs/iomap/buffered-io.c | 2 +- fs/iomap/direct-io.c | 28 +++++-- fs/ntfs/file.c | 2 +- fs/xfs/xfs_file.c | 6 +- fs/zonefs/super.c | 4 +- include/linux/iomap.h | 11 ++- include/linux/pagemap.h | 58 +------------- include/linux/uio.h | 4 +- lib/iov_iter.c | 107 ++++++++++++++++++++------ mm/filemap.c | 4 +- mm/gup.c | 113 ++++++++++++++++++++++++++++ 22 files changed, 360 insertions(+), 126 deletions(-)
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 1/12 iov_iter: Fix iov_iter_get_pages{,_alloc} page fault return value
Both iov_iter_get_pages and iov_iter_get_pages_alloc return the number of bytes of the iovec they could get the pages for. When they cannot get any pages, they're supposed to return 0, but when the start of the iovec isn't page aligned, the calculation goes wrong and they return a negative value. Fix both functions.
In addition, change iov_iter_get_pages_alloc to return NULL in that case to prevent resource leaks.
It seems that the cifs and nfs filesystems don't handle the zero case very well. Could the maintainers please have a look?
--- lib/iov_iter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/iov_iter.c +3 -2
@@ -1484,7 +1484,7 @@ ssize_t iov_iter_get_pages(struct iov_iter *i, res =get_user_pages_fast (addr, n,iov_iter_rw (i) !=WRITE ?FOLL_WRITE :0 , pages); -if (unlikely (res <0 )) +if (unlikely (res <=0 ))return res;return (res == n ? len : res *PAGE_SIZE ) - *start; }
@@ -1608,8 +1608,9 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,return -ENOMEM ; res =get_user_pages_fast (addr, n,iov_iter_rw (i) !=WRITE ?FOLL_WRITE :0 , p); -if (unlikely (res <0 )) { +if (unlikely (res <=0 )) {kvfree (p); + *pages =NULL ;return res; } *pages = p;
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 2/12 powerpc/kvm: Fix kvm_use_magic_page
In reply to Andreas GruenbacherWhen switching from __get_user to fault_in_pages_readable, commit 9f9eae5ce717 broke kvm_use_magic_page: fault_in_pages_readable returns 0 on success. Fix that.
--- arch/powerpc/kernel/kvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/kvm.c +1 -1
@@ -669,7 +669,7 @@ static void __init kvm_use_magic_page(void)on_each_cpu (kvm_map_magic_page, &features,1 );/* Quick self-test to see if the mapping works */ -if (!fault_in_pages_readable ((const char *)KVM_MAGIC_PAGE ,sizeof (u32))) { +if (fault_in_pages_readable ((const char *)KVM_MAGIC_PAGE ,sizeof (u32))) { kvm_patching_worked = false;return ; }
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 3/12 Turn fault_in_pages_{readable,writeable} into fault_in_{readable,writeable}
In reply to Andreas GruenbacherTurn fault_in_pages_{readable,writeable} into versions that return the number of bytes faulted in instead of returning a non-zero value when any of the requested pages couldn't be faulted in. This supports the existing users that require all pages to be faulted in, but also new users that are happy if any pages can be faulted in.
Neither of these functions is entirely trivial and it doesn't seem useful to inline them, so move them to mm/gup.c.
Rename the functions to fault_in_{readable,writeable} to make sure that code that uses them can be fixed instead of breaking silently.
--- arch/powerpc/kernel/kvm.c | 3 +- arch/powerpc/kernel/signal_32.c | 4 +- arch/powerpc/kernel/signal_64.c | 2 +- arch/x86/kernel/fpu/signal.c | 8 ++-- drivers/gpu/drm/armada/armada_gem.c | 7 ++-- fs/btrfs/ioctl.c | 7 ++-- include/linux/pagemap.h | 57 ++--------------------------- lib/iov_iter.c | 12 +++--- mm/filemap.c | 2 +- mm/gup.c | 52 ++++++++++++++++++++++++++ 10 files changed, 78 insertions(+), 76 deletions(-)
arch/powerpc/kernel/kvm.c +2 -1
@@ -669,7 +669,8 @@ static void __init kvm_use_magic_page(void)on_each_cpu (kvm_map_magic_page, &features,1 );/* Quick self-test to see if the mapping works */ -if (fault_in_pages_readable ((const char *)KVM_MAGIC_PAGE ,sizeof (u32))) { +if (fault_in_readable ((const char __user *)KVM_MAGIC_PAGE , +sizeof (u32)) !=sizeof (u32)) { kvm_patching_worked = false;return ; }
arch/powerpc/kernel/signal_32.c +2 -2
@@ -1048,7 +1048,7 @@ SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,if (new_ctx ==NULL )return 0 ;if (!access_ok (new_ctx, ctx_size) || -fault_in_pages_readable ((u8 __user *)new_ctx, ctx_size)) +fault_in_readable ((char __user *)new_ctx, ctx_size) != ctx_size)return -EFAULT ;/*
@@ -1237,7 +1237,7 @@ SYSCALL_DEFINE3(debug_setcontext, struct ucontext __user *, ctx,#endif if (!access_ok (ctx,sizeof (*ctx)) || -fault_in_pages_readable ((u8 __user *)ctx,sizeof (*ctx))) +fault_in_readable ((char __user *)ctx,sizeof (*ctx)) !=sizeof (*ctx))return -EFAULT ;/*
arch/powerpc/kernel/signal_64.c +1 -1
@@ -688,7 +688,7 @@ SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,if (new_ctx ==NULL )return 0 ;if (!access_ok (new_ctx, ctx_size) || -fault_in_pages_readable ((u8 __user *)new_ctx, ctx_size)) +fault_in_readable ((char __user *)new_ctx, ctx_size) != ctx_size)return -EFAULT ;/*
arch/x86/kernel/fpu/signal.c +4 -4
@@ -205,7 +205,8 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)fpregs_unlock ();if (ret) { -if (!fault_in_pages_writeable (buf_fx, fpu_user_xstate_size)) +if (fault_in_writeable (buf_fx, fpu_user_xstate_size) == + fpu_user_xstate_size) goto retry;return -EFAULT ; }
@@ -278,10 +279,9 @@ static int restore_fpregs_from_user(void __user *buf, u64 xrestore,if (ret != -EFAULT )return -EINVAL ; - ret =fault_in_pages_readable (buf, size); -if (!ret) +if (fault_in_readable (buf, size) == size) goto retry; -return ret; +return -EFAULT ; }/*
drivers/gpu/drm/armada/armada_gem.c +3 -4
@@ -336,7 +336,7 @@ int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,struct drm_armada_gem_pwrite *args = data;struct armada_gem_object *dobj;char __user *ptr; -int ret; +int ret =0 ;DRM_DEBUG_DRIVER ("handle %u off %u size %u ptr 0x%llx\n" , args->handle, args->offset, args->size, args->ptr);
@@ -349,9 +349,8 @@ int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data,if (!access_ok (ptr, args->size))return -EFAULT ; - ret =fault_in_pages_readable (ptr, args->size); -if (ret) -return ret; +if (fault_in_readable (ptr, args->size) != args->size) +return -EFAULT ; dobj =armada_gem_object_lookup (file, args->handle);if (dobj ==NULL )
fs/btrfs/ioctl.c +4 -3
@@ -2244,9 +2244,10 @@ static noinline int search_ioctl(struct inode *inode, key.offset = sk->min_offset;while (1 ) { - ret =fault_in_pages_writeable (ubuf + sk_offset, - *buf_size - sk_offset); -if (ret) +size_t size = *buf_size - sk_offset; + + ret = -EFAULT ; +if (fault_in_writeable (ubuf + sk_offset, size) != size)break ; ret =btrfs_search_forward (root, &key, path, sk->min_transid);
include/linux/pagemap.h +3 -54
64 lines not on this page · raw message
lib/iov_iter.c +6 -6
@@ -191,7 +191,8 @@ static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t b buf = iov->iov_base + skip; copy =min (bytes, iov->iov_len - skip); -if (IS_ENABLED (CONFIG_HIGHMEM ) && !fault_in_pages_writeable (buf, copy)) { +if (IS_ENABLED (CONFIG_HIGHMEM ) && +fault_in_writeable (buf, copy) == copy) { kaddr =kmap_atomic (page); from = kaddr + offset;
@@ -275,7 +276,8 @@ static size_t copy_page_from_iter_iovec(struct page *page, size_t offset, size_t buf = iov->iov_base + skip; copy =min (bytes, iov->iov_len - skip); -if (IS_ENABLED (CONFIG_HIGHMEM ) && !fault_in_pages_readable (buf, copy)) { +if (IS_ENABLED (CONFIG_HIGHMEM ) && +fault_in_readable (buf, copy) == copy) { kaddr =kmap_atomic (page); to = kaddr + offset;
@@ -446,13 +448,11 @@ int iov_iter_fault_in_readable(const struct iov_iter *i, size_t bytes) bytes = i->count;for (p = i->iov, skip = i->iov_offset; bytes; p++, skip =0 ) {size_t len =min (bytes, p->iov_len - skip); -int err;if (unlikely (!len))continue ; - err =fault_in_pages_readable (p->iov_base + skip, len); -if (unlikely (err)) -return err; +if (fault_in_readable (p->iov_base + skip, len) != len) +return -EFAULT ; bytes -= len; } }
mm/filemap.c +1 -1
@@ -88,7 +88,7 @@* ->lock_page (access_process_vm) * * ->i_mutex (generic_perform_write) -* ->mmap_lock (fault_in_pages_readable->do_page_fault) +* ->mmap_lock (fault_in_readable->do_page_fault) * * bdi->wb.list_lock * sb_lock (fs/fs-writeback.c)
mm/gup.c +52 -0
58 lines not on this page · raw message
Signature · 1 line
2.26.3
Re: [PATCH v5 03/12] Turn fault_in_pages_{readable,writeable} into fault_in_{readable,writeable}
Quoted 4 lines · Andreas Gruenbacher
Turn fault_in_pages_{readable,writeable} into versions that return the number of bytes faulted in instead of returning a non-zero value when any of the requested pages couldn't be faulted in.
Ugh. This ends up making most users have horribly nasty conditionals.
I think I suggested (or maybe that was just my internal voice and I never actually vocalized it) using the same semantics as "copy_to/from_user()" for fault_in_pages_{readable|writable}().
Namely to return the number of bytes *not* faulted in.
That makes it trivial to test "did I get everything" - becasue zero means "nothing failed" and remains the "complete success" marker.
And it still allows for the (much less common) case of "ok, I need to know about partial failures".
So instead of this horror:
- if (!fault_in_pages_writeable(buf_fx, fpu_user_xstate_size))
+ if (fault_in_writeable(buf_fx, fpu_user_xstate_size) ==
+ fpu_user_xstate_size)
you'd just have
- if (!fault_in_pages_writeable(buf_fx, fpu_user_xstate_size))
+ if (!fault_in_writeable(buf_fx, fpu_user_xstate_size))
because zero would continue to be a marker of success.
Linus
Al Viro
maintainerRe: [PATCH v5 03/12] Turn fault_in_pages_{readable,writeable} into fault_in_{readable,writeable}
In reply to Andreas GruenbacherQuoted 11 lines · Andreas Gruenbacher
Turn fault_in_pages_{readable,writeable} into versions that return the number of bytes faulted in instead of returning a non-zero value when any of the requested pages couldn't be faulted in. This supports the existing users that require all pages to be faulted in, but also new users that are happy if any pages can be faulted in.
Neither of these functions is entirely trivial and it doesn't seem useful to inline them, so move them to
mm/gup.c.Rename the functions to fault_in_{readable,writeable} to make sure that code that uses them can be fixed instead of breaking silently.
Sigh... We'd already discussed that; it's bloody pointless. Making short fault-in return success - absolutely. Reporting exact number of bytes is not going to be of any use to callers.
Please, don't.
Andreas Gruenbacher
maintainerRe: [PATCH v5 03/12] Turn fault_in_pages_{readable,writeable} into fault_in_{readable,writeable}
Quoted 10 lines · Al Viro
Quoted 9 lines · Andreas Gruenbacher
Turn fault_in_pages_{readable,writeable} into versions that return the number of bytes faulted in instead of returning a non-zero value when any of the requested pages couldn't be faulted in. This supports the existing users that require all pages to be faulted in, but also new users that are happy if any pages can be faulted in.
Neither of these functions is entirely trivial and it doesn't seem useful to inline them, so move them to
mm/gup.c.
Quoted 6 lines
Quoted 2 lines
Rename the functions to fault_in_{readable,writeable} to make sure that code that uses them can be fixed instead of breaking silently.
Sigh... We'd already discussed that; it's bloody pointless. Making short fault-in return success - absolutely. Reporting exact number of bytes is not going to be of any use to callers.
I'm not actually convinced that you're right about this. Let's see what we'll end up with; we can always strip things down in the end.
Thanks,
Andreas
Andreas Gruenbacher
maintainerPATCH v5 4/12 Turn iov_iter_fault_in_readable into fault_in_iov_iter_readable
In reply to Andreas GruenbacherTurn iov_iter_fault_in_readable into a function that returns the number of bytes faulted in instead of returning a non-zero value when any of the requested pages couldn't be faulted in. This supports the existing users that require all pages to be faulted in, but also new users that are happy if any pages can be faulted in.
Rename iov_iter_fault_in_readable to an unfortunately clumsy fault_in_iov_iter_readable to make sure that code that uses it can be fixed instead of breaking silently.
Fix up the existing users.
--- fs/btrfs/file.c | 3 ++- fs/f2fs/file.c | 6 +++--- fs/fuse/file.c | 2 +- fs/iomap/buffered-io.c | 2 +- fs/ntfs/file.c | 2 +- include/linux/uio.h | 2 +- lib/iov_iter.c | 33 ++++++++++++++++++++++----------- mm/filemap.c | 2 +- 8 files changed, 32 insertions(+), 20 deletions(-)
fs/btrfs/file.c +2 -1
@@ -1698,7 +1698,8 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,* Fault pages before locking them in prepare_pages * to avoid recursive lock */ -if (unlikely (iov_iter_fault_in_readable (i, write_bytes))) { +if (unlikely (fault_in_iov_iter_readable (i, write_bytes) != + write_bytes)) { ret = -EFAULT ;break ; }
fs/f2fs/file.c +3 -3
@@ -4255,16 +4255,16 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) ret =generic_write_checks (iocb, from);if (ret >0 ) { +size_t count =iov_iter_count (from);bool preallocated = false;size_t target_size =0 ;int err; -if (iov_iter_fault_in_readable (from,iov_iter_count (from))) +if (fault_in_iov_iter_readable (from, count) != count)set_inode_flag (inode,FI_NO_PREALLOC );if ((iocb->ki_flags &IOCB_NOWAIT )) { -if (!f2fs_overwrite_io (inode, iocb->ki_pos, -iov_iter_count (from)) || +if (!f2fs_overwrite_io (inode, iocb->ki_pos, count) ||f2fs_has_inline_data (inode) ||f2fs_force_buffered_io (inode, iocb, from)) {clear_inode_flag (inode,FI_NO_PREALLOC );
fs/fuse/file.c +1 -1
@@ -1160,7 +1160,7 @@ static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, again: err = -EFAULT ; -if (iov_iter_fault_in_readable (ii, bytes)) +if (fault_in_iov_iter_readable (ii, bytes) != bytes)break ; err = -ENOMEM ;
fs/iomap/buffered-io.c +1 -1
@@ -749,7 +749,7 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* same page as we're writing to, without it being marked * up-to-date. */ -if (unlikely (iov_iter_fault_in_readable (i, bytes))) { +if (unlikely (fault_in_iov_iter_readable (i, bytes) != bytes)) { status = -EFAULT ;break ; }
fs/ntfs/file.c +1 -1
@@ -1829,7 +1829,7 @@ static ssize_t ntfs_perform_write(struct file *file, struct iov_iter *i,* pages being swapped out between us bringing them into memory * and doing the actual copying. */ -if (unlikely (iov_iter_fault_in_readable (i, bytes))) { +if (unlikely (fault_in_iov_iter_readable (i, bytes) != bytes)) { status = -EFAULT ;break ; }
include/linux/uio.h +1 -1
@@ -119,7 +119,7 @@ size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, struct iov_iter *i);void iov_iter_advance (struct iov_iter *i,size_t bytes);void iov_iter_revert (struct iov_iter *i,size_t bytes); -int iov_iter_fault_in_readable (const struct iov_iter *i,size_t bytes); +size_t fault_in_iov_iter_readable (const struct iov_iter *i,size_t bytes);size_t iov_iter_single_seg_count (const struct iov_iter *i);size_t copy_page_to_iter (struct page *page,size_t offset,size_t bytes,struct iov_iter *i);
lib/iov_iter.c +22 -11
@@ -432,33 +432,44 @@ static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t by }/* +* fault_in_iov_iter_readable - fault in iov iterator for reading +* @i: iterator +* @size: maximum length +* * Fault in one or more iovecs of the given iov_iter, to a maximum length of -* bytes. For each iovec, fault in each page that constitutes the iovec. +* @size. For each iovec, fault in each page that constitutes the iovec. +* +* Returns the number of bytes faulted in, or 0 if no bytes could be faulted in +* (i.e., because the address is invalid). * -* Return 0 on success, or non-zero if the memory could not be accessed (i.e. -* because it is an invalid address). +* Always returns the number of avaliable bytes for non-user space iterators. */ -int iov_iter_fault_in_readable (const struct iov_iter *i,size_t bytes) +size_t fault_in_iov_iter_readable (const struct iov_iter *i,size_t size) { +if (size > i->count) + size = i->count; +if (iter_is_iovec (i)) {const struct iovec *p; +size_t bytes = size;size_t skip; -if (bytes > i->count) - bytes = i->count;for (p = i->iov, skip = i->iov_offset; bytes; p++, skip =0 ) {size_t len =min (bytes, p->iov_len - skip); +size_t ret;if (unlikely (!len))continue ; -if (fault_in_readable (p->iov_base + skip, len) != len) -return -EFAULT ; - bytes -= len; + ret =fault_in_readable (p->iov_base + skip, len); + bytes -= ret; +if (ret != len) +break ; } +return size - bytes; } -return 0 ; +return size; } -EXPORT_SYMBOL (iov_iter_fault_in_readable); +EXPORT_SYMBOL (fault_in_iov_iter_readable); void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov, unsigned long nr_segs,
mm/filemap.c +1 -1
@@ -3643,7 +3643,7 @@ ssize_t generic_perform_write(struct file *file,* same page as we're writing to, without it being marked * up-to-date. */ -if (unlikely (iov_iter_fault_in_readable (i, bytes))) { +if (unlikely (fault_in_iov_iter_readable (i, bytes) != bytes)) { status = -EFAULT ;break ; }
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 5/12 iov_iter: Introduce fault_in_iov_iter_writeable
In reply to Andreas GruenbacherIntroduce a new fault_in_iov_iter_writeable helper for safely faulting in an iterator for writing. Uses get_user_pages() to fault in the pages without actually writing to them, which would be destructive.
We'll use fault_in_iov_iter_writeable in gfs2 once we've determined that the iterator passed to .read_iter isn't in memory.
--- include/linux/pagemap.h | 1 + include/linux/uio.h | 1 + lib/iov_iter.c | 41 +++++++++++++++++++++++++++ mm/gup.c | 61 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+)
include/linux/pagemap.h +1 -0
@@ -737,6 +737,7 @@ extern void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter);* Fault in userspace address range. */ size_t fault_in_writeable (char __user *uaddr,size_t size); +size_t fault_in_safe_writeable (const char __user *uaddr,size_t size);size_t fault_in_readable (const char __user *uaddr,size_t size); int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
include/linux/uio.h +1 -0
@@ -120,6 +120,7 @@ size_t copy_page_from_iter_atomic(struct page *page, unsigned offset,void iov_iter_advance (struct iov_iter *i,size_t bytes);void iov_iter_revert (struct iov_iter *i,size_t bytes);size_t fault_in_iov_iter_readable (const struct iov_iter *i,size_t bytes); +size_t fault_in_iov_iter_writeable (const struct iov_iter *i,size_t bytes);size_t iov_iter_single_seg_count (const struct iov_iter *i);size_t copy_page_to_iter (struct page *page,size_t offset,size_t bytes,struct iov_iter *i);
lib/iov_iter.c +41 -0
47 lines not on this page · raw message
mm/gup.c +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerRe: [PATCH v5 05/12] iov_iter: Introduce fault_in_iov_iter_writeable
Quoted 132 lines · Andreas Gruenbacher
Introduce a new
fault_in_iov_iter_writeablehelper for safely faulting in an iterator for writing. Usesget_user_pages()to fault in the pages without actually writing to them, which would be destructive.We'll use
fault_in_iov_iter_writeablein gfs2 once we've determined that the iterator passed to .read_iter isn't in memory.Signed-off-by Andreas Gruenbacher <[email protected]>--- include/linux/pagemap.h | 1 + include/linux/uio.h | 1 + lib/iov_iter.c | 41 +++++++++++++++++++++++++++ mm/gup.c | 61 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+)include/linux/pagemap.h +1
-0@@ -737,6 +737,7 @@ extern void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter);* Fault in userspace address range. */ size_t fault_in_writeable (char __user *uaddr,size_t size); +size_t fault_in_safe_writeable (const char __user *uaddr,size_t size);size_t fault_in_readable (const char __user *uaddr,size_t size); int add_to_page_cache_locked(struct page *page, struct address_space *mapping,include/linux/uio.h +1
-0@@ -120,6 +120,7 @@ size_t copy_page_from_iter_atomic(struct page *page, unsigned offset,void iov_iter_advance (struct iov_iter *i,size_t bytes);void iov_iter_revert (struct iov_iter *i,size_t bytes);size_t fault_in_iov_iter_readable (const struct iov_iter *i,size_t bytes); +size_t fault_in_iov_iter_writeable (const struct iov_iter *i,size_t bytes);size_t iov_iter_single_seg_count (const struct iov_iter *i);size_t copy_page_to_iter (struct page *page,size_t offset,size_t bytes,struct iov_iter *i);lib/iov_iter.c +41
-0@@ -471,6 +471,47 @@ size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) }EXPORT_SYMBOL (fault_in_iov_iter_readable); +/* +* fault_in_iov_iter_writeable - fault in iov iterator for writing +* @i: iterator +* @size: maximum length +* +* Faults in the iterator using get_user_pages(), i.e., without triggering +* hardware page faults. This is primarily useful when we know that some or +* all of the pages in @i aren't in memory. +* +* Returns the number of bytes faulted in, or 0 if no bytes could be faulted in +* (i.e., because the address is invalid). +* +* Always returns the number of avaliable bytes for non-user space iterators. +*/ +size_t fault_in_iov_iter_writeable (const struct iov_iter *i,size_t size) +{ +if (size > i->count) + size = i->count; + +if (iter_is_iovec (i)) { +const struct iovec *p; +size_t bytes = size; +size_t skip; + +for (p = i->iov, skip = i->iov_offset; bytes; p++, skip =0 ) { +size_t len =min (bytes, p->iov_len - skip); +size_t ret; + +if (unlikely (!len)) +continue ; + ret =fault_in_safe_writeable (p->iov_base + skip, len); + bytes -= ret; +if (ret != len) +break ; + } +return size - bytes; + } +return size; +} +EXPORT_SYMBOL (fault_in_iov_iter_writeable); +void iov_iter_init (struct iov_iter *i,unsigned int direction,const struct iovec *iov,unsigned long nr_segs,size_t count)mm/gup.c +33
-0@@ -1694,6 +1694,67 @@ size_t fault_in_writeable(char __user *uaddr, size_t size) }EXPORT_SYMBOL (fault_in_writeable); +/** +* fault_in_safe_writeable - fault in an address range for writing +* @uaddr: start of address range +* @size: length of address range +* +* Faults in an address range using get_user_pages, i.e., without triggering +* hardware page faults. This is primarily useful when we know that some or +* all of the pages in the address range aren't in memory. +* +* Other than fault_in_writeable(), this function is non-destructive. +* +* Note that we don't pin or otherwise hold the pages referenced that we fault +* in. There's no guarantee that they'll stay in memory for any duration of +* time. +* +* Returns the number of bytes faulted in from @uaddr. +*/ +size_t fault_in_safe_writeable (const char __user *uaddr,size_t size) +{ +unsigned long start = (unsigned long )uaddr; +unsigned long end, nstart, nend; +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma =NULL ; +int locked =0 ; + +/* FIXME: Protect against overflow! */ + + end =PAGE_ALIGN (start + size); +for (nstart = start &PAGE_MASK ; nstart < end; nstart = nend) { +unsigned long nr_pages; +long ret; + +if (!locked) {
mm/gup.c size_t fault_in_writeable(char __user *uaddr, size_t size) · line 1739
Quoted 10 lines
mm/gup.c +10
-0+ locked =1 ; +mmap_read_lock (mm); + vma =find_vma (mm, nstart); + }else if (nstart >= vma->vm_end) + vma = vma->vm_next; +if (!vma || vma->vm_start >= end) +break ; + nend =min (end, vma->vm_end); +if (vma->vm_flags & (VM_IO |VM_PFNMAP )) +continue ;
Shouldn't we disallow read()ing into those kinds of vmas? If we skip over them here and then the actual write results in -EFAULT, we'll end up in a loop.
mm/gup.c size_t fault_in_writeable(char __user *uaddr, size_t size) · line 1741
Quoted 2 lines
mm/gup.c +2
-0+if (nstart < vma->vm_start) + nstart = vma->vm_start;
Likewise, shouldn't we fail for memory ranges not covered by a vma?
Quoted 20 lines
mm/gup.c +16
-1+ nr_pages = (nend - nstart) /PAGE_SIZE ; + ret =__get_user_pages_locked (mm, nstart, nr_pages, +NULL ,NULL , &locked, +FOLL_TOUCH |FOLL_WRITE ); +if (ret <=0 ) +break ; + nend = nstart + ret *PAGE_SIZE ; + } +if (locked) +mmap_read_unlock (mm); +if (nstart > start) +return min (nstart - start, size); +return 0 ; +} +EXPORT_SYMBOL (fault_in_safe_writeable); +size_t fault_in_readable (const char __user *uaddr,size_t size) {const char __user *start = uaddr, *end; --
Quoted 2 lines
2.26.3
Thanks,
Andreas
Andreas Gruenbacher
maintainerPATCH v5 6/12 gfs2: Add wrapper for iomap_file_buffered_write
In reply to Andreas GruenbacherAdd a wrapper around iomap_file_buffered_write. We'll add code for when the operation needs to be retried here later.
--- fs/gfs2/file.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
fs/gfs2/file.c +14 -6
@@ -876,6 +876,18 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)return written ? written : ret; } +static ssize_t gfs2_file_buffered_write (struct kiocb *iocb,struct iov_iter *from) +{ +struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file); +ssize_t ret; + + current->backing_dev_info =inode_to_bdi (inode); + ret =iomap_file_buffered_write (iocb, from, &gfs2_iomap_ops); + current->backing_dev_info =NULL ; +return ret; +} +/** * gfs2_file_write_iter - Perform a write to a file * @iocb: The io context
@@ -927,9 +939,7 @@ static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from) goto out_unlock; iocb->ki_flags |=IOCB_DSYNC ; - current->backing_dev_info =inode_to_bdi (inode); - buffered =iomap_file_buffered_write (iocb, from, &gfs2_iomap_ops); - current->backing_dev_info =NULL ; + buffered =gfs2_file_buffered_write (iocb, from);if (unlikely (buffered <=0 )) {if (!ret) ret = buffered;
@@ -951,9 +961,7 @@ static ssize_t gfs2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (!ret || ret2 >0 ) ret += ret2; }else { - current->backing_dev_info =inode_to_bdi (inode); - ret =iomap_file_buffered_write (iocb, from, &gfs2_iomap_ops); - current->backing_dev_info =NULL ; + ret =gfs2_file_buffered_write (iocb, from);if (likely (ret >0 )) { iocb->ki_pos += ret; ret =generic_write_sync (iocb, ret);
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 7/12 gfs2: Fix mmap + page fault deadlocks for buffered I/O
In reply to Andreas GruenbacherIn the .read_iter and .write_iter file operations, we're accessing user-space memory while holding the inodes glock. There's a possibility that the memory is mapped to the same file, in which case we'd recurse on the same glock.
More complex scenarios can involve multiple glocks, processes, and even cluster nodes.
Avoids these kinds of problems by disabling page faults while holding a glock. If a page fault occurs, we either end up with a partial read or write, or with -EFAULT if nothing could be read or written. In that case, we drop the glock, fault in the requested pages manually, and repeat the operation.
This locking problem in gfs2 was originally reported by Jan Kara. Linus came up with the proposal to disable page faults. Many thanks to Al Viro and Matthew Wilcox for their feedback as well.
--- fs/gfs2/file.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
fs/gfs2/file.c +26 -0
@@ -843,6 +843,12 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)size_t written =0 ;ssize_t ret; +/* +* In this function, we disable page faults when we're holding the +* inode glock while doing I/O. If a page fault occurs, we drop the +* inode glock, fault in the pages manually, and retry. +*/ +if (iocb->ki_flags &IOCB_DIRECT ) { ret =gfs2_file_direct_read (iocb, to, &gh);if (likely (ret != -ENOTBLK ))
@@ -864,13 +870,20 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to) } ip =GFS2_I (iocb->ki_filp->f_mapping->host);gfs2_holder_init (ip->i_gl,LM_ST_SHARED ,0 , &gh); +retry: ret =gfs2_glock_nq (&gh);if (ret) goto out_uninit; +pagefault_disable (); ret =generic_file_read_iter (iocb, to); +pagefault_enable ();if (ret >0 ) written += ret;gfs2_glock_dq (&gh); +if (unlikely (iov_iter_count (to) && (ret >0 || ret == -EFAULT )) && +iter_is_iovec (to) && +fault_in_iov_iter_writeable (to,SIZE_MAX ) !=0 ) + goto retry; out_uninit:gfs2_holder_uninit (&gh);return written ? written : ret;
@@ -882,9 +895,22 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb, struct iov_iter *frostruct inode *inode =file_inode (file);ssize_t ret; +/* +* In this function, we disable page faults when we're holding the +* inode glock while doing I/O. If a page fault occurs, we drop the +* inode glock, fault in the pages manually, and retry. +*/ + +retry: current->backing_dev_info =inode_to_bdi (inode); +pagefault_disable (); ret =iomap_file_buffered_write (iocb, from, &gfs2_iomap_ops); +pagefault_enable (); current->backing_dev_info =NULL ; +if (unlikely (ret == -EFAULT ) && +iter_is_iovec (from) && +fault_in_iov_iter_readable (from,SIZE_MAX ) !=0 ) + goto retry;return ret; }
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 8/12 iomap: Fix iomap_dio_rw return value for user copies
In reply to Andreas GruenbacherWhen a user copy fails in one of the helpers of iomap_dio_rw, fail with -EFAULT instead of returning 0. This matches what iomap_dio_bio_actor returns when it gets an -EFAULT from bio_iov_iter_get_pages. With these changes, iomap_dio_actor consistently fails with -EFAULT when a user page cannot be faulted in.
--- fs/iomap/direct-io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/iomap/direct-io.c +2 -2
@@ -370,7 +370,7 @@ iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio) { length =iov_iter_zero (length, dio->submit.iter); dio->size += length; -return length; +return length ? length : -EFAULT ; } static loff_t
@@ -397,7 +397,7 @@ iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length, copied =copy_to_iter (iomap->inline_data + pos, length, iter); } dio->size += copied; -return copied; +return copied ? copied : -EFAULT ; } static loff_t
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 9/12 iomap: Support restarting direct I/O requests after user copy failures
In reply to Andreas GruenbacherIn iomap_dio_rw, when iomap_apply returns an -EFAULT error, complete the request synchronously. Either return a partial result (when the IOMAP_DIO_FAULT_RETRY flag is set and the caller is thus prepared to handle partial results), or reset the iterator to the start and fail to allow restarting the request.
--- fs/iomap/direct-io.c | 13 +++++++++++++ include/linux/iomap.h | 7 +++++++ 2 files changed, 20 insertions(+)
fs/iomap/direct-io.c +13 -0
@@ -561,6 +561,19 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, ret =iomap_apply (inode, pos, count, iomap_flags, ops, dio, iomap_dio_actor);if (ret <=0 ) { +if (ret == -EFAULT ) { +/* +* Finish synchronously and revert the iterator +* when failing the request to allow a retry. +*/ + wait_for_completion = true; +if (dio->size && + (dio_flags &IOMAP_DIO_PARTIAL )) + ret =0 ; +else +iov_iter_revert (iter, dio->size); + } +/* magic error code to fall back to buffered I/O */ if (ret == -ENOTBLK ) { wait_for_completion = true;
include/linux/iomap.h +7 -0
@@ -267,6 +267,13 @@ struct iomap_dio_ops {*/ #define IOMAP_DIO_OVERWRITE_ONLY (1 << 1) +/* +* When a page fault occurs, return a partial synchronous result and allow +* the caller to retry the rest of the operation after dealing with the page +* fault. +*/ +#define IOMAP_DIO_PARTIAL (1 << 2) +ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops,unsigned int dio_flags);
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 10/12 iomap: Add done_before argument to iomap_dio_rw
In reply to Andreas GruenbacherAdd a done_before argument to iomap_dio_rw that indicates how much of the request has already been transferred. When the request succeeds, we report that done_before additional bytes were tranferred. This is useful for finishing a request asynchronously when part of the request has already been completed synchronously.
We'll use that to allow iomap_dio_rw to be used with page faults disabled: when a page fault occurs while submitting a request, we synchronously complete the part of the request that has already been submitted. The caller can then take care of the page fault and call iomap_dio_rw again for the rest of the request, passing in the number of bytes already tranferred.
--- fs/btrfs/file.c | 5 +++-- fs/ext4/file.c | 5 +++-- fs/gfs2/file.c | 4 ++-- fs/iomap/direct-io.c | 11 ++++++++--- fs/xfs/xfs_file.c | 6 +++--- fs/zonefs/super.c | 4 ++-- include/linux/iomap.h | 4 ++-- 7 files changed, 23 insertions(+), 16 deletions(-)
fs/btrfs/file.c +3 -2
@@ -1946,7 +1946,7 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) } dio =__iomap_dio_rw (iocb, from, &btrfs_dio_iomap_ops, &btrfs_dio_ops, -0 ); +0 ,0 );btrfs_inode_unlock (inode, ilock_flags);
@@ -3638,7 +3638,8 @@ static ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)return 0 ;btrfs_inode_lock (inode,BTRFS_ILOCK_SHARED ); - ret =iomap_dio_rw (iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops,0 ); + ret =iomap_dio_rw (iocb, to, &btrfs_dio_iomap_ops, &btrfs_dio_ops, +0 ,0 );btrfs_inode_unlock (inode,BTRFS_ILOCK_SHARED );return ret; }
fs/ext4/file.c +3 -2
@@ -74,7 +74,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)return generic_file_read_iter (iocb, to); } - ret =iomap_dio_rw (iocb, to, &ext4_iomap_ops,NULL ,0 ); + ret =iomap_dio_rw (iocb, to, &ext4_iomap_ops,NULL ,0 ,0 );inode_unlock_shared (inode);file_accessed (iocb->ki_filp);
@@ -566,7 +566,8 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)if (ilock_shared) iomap_ops = &ext4_iomap_overwrite_ops; ret =iomap_dio_rw (iocb, from, iomap_ops, &ext4_dio_write_ops, - (unaligned_io || extend) ?IOMAP_DIO_FORCE_WAIT :0 ); + (unaligned_io || extend) ?IOMAP_DIO_FORCE_WAIT :0 , +0 );if (ret == -ENOTBLK ) ret =0 ;
fs/gfs2/file.c +2 -2
@@ -792,7 +792,7 @@ 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 ,0 ); + ret =iomap_dio_rw (iocb, to, &gfs2_iomap_ops,NULL ,0 ,0 );gfs2_glock_dq (gh); out_uninit:gfs2_holder_uninit (gh);
@@ -826,7 +826,7 @@ 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 ,0 ); + ret =iomap_dio_rw (iocb, from, &gfs2_iomap_ops,NULL ,0 ,0 );if (ret == -ENOTBLK ) ret =0 ; out:
fs/iomap/direct-io.c +8 -3
@@ -31,6 +31,7 @@ struct iomap_dio {atomic_t ref;unsigned flags;int error; +size_t done_before;bool wait_for_completion;union {
@@ -126,6 +127,9 @@ ssize_t iomap_dio_complete(struct iomap_dio *dio)if (ret >0 && (dio->flags &IOMAP_DIO_NEED_SYNC )) ret =generic_write_sync (iocb, ret); +if (ret >=0 ) + ret += dio->done_before; +kfree (dio);return ret;
@@ -450,7 +454,7 @@ iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,struct iomap_dio *__iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -unsigned int dio_flags) +unsigned int dio_flags,size_t done_before) {struct address_space *mapping = iocb->ki_filp->f_mapping;struct inode *inode =file_inode (iocb->ki_filp);
@@ -477,6 +481,7 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, dio->dops = dops; dio->error =0 ; dio->flags =0 ; + dio->done_before = done_before; dio->submit.iter = iter; dio->submit.waiter = current;
@@ -655,11 +660,11 @@ EXPORT_SYMBOL_GPL(__iomap_dio_rw);ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -unsigned int dio_flags) +unsigned int dio_flags,size_t done_before) {struct iomap_dio *dio; - dio =__iomap_dio_rw (iocb, iter, ops, dops, dio_flags); + dio =__iomap_dio_rw (iocb, iter, ops, dops, dio_flags, done_before);if (IS_ERR_OR_NULL (dio))return PTR_ERR_OR_ZERO (dio);return iomap_dio_complete (dio);
fs/xfs/xfs_file.c +3 -3
@@ -259,7 +259,7 @@ xfs_file_dio_read( ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED );if (ret)return ret; - ret =iomap_dio_rw (iocb, to, &xfs_read_iomap_ops,NULL ,0 ); + ret =iomap_dio_rw (iocb, to, &xfs_read_iomap_ops,NULL ,0 ,0 );xfs_iunlock (ip,XFS_IOLOCK_SHARED );return ret;
@@ -569,7 +569,7 @@ xfs_file_dio_write_aligned( }trace_xfs_file_direct_write (iocb, from); ret =iomap_dio_rw (iocb, from, &xfs_direct_write_iomap_ops, - &xfs_dio_write_ops,0 ); + &xfs_dio_write_ops,0 ,0 ); out_unlock:if (iolock)xfs_iunlock (ip, iolock);
@@ -647,7 +647,7 @@ xfs_file_dio_write_unaligned(trace_xfs_file_direct_write (iocb, from); ret =iomap_dio_rw (iocb, from, &xfs_direct_write_iomap_ops, - &xfs_dio_write_ops, flags); + &xfs_dio_write_ops, flags,0 );/* * Retry unaligned I/O with exclusive blocking semantics if the DIO
fs/zonefs/super.c +2 -2
@@ -864,7 +864,7 @@ static ssize_t zonefs_file_dio_write(struct kiocb *iocb, struct iov_iter *from) ret =zonefs_file_dio_append (iocb, from); else ret =iomap_dio_rw (iocb, from, &zonefs_iomap_ops, - &zonefs_write_dio_ops,0 ); + &zonefs_write_dio_ops,0 ,0 );if (zi->i_ztype ==ZONEFS_ZTYPE_SEQ && (ret >0 || ret == -EIOCBQUEUED )) {if (ret >0 )
@@ -999,7 +999,7 @@ static ssize_t zonefs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) }file_accessed (iocb->ki_filp); ret =iomap_dio_rw (iocb, to, &zonefs_iomap_ops, - &zonefs_read_dio_ops,0 ); + &zonefs_read_dio_ops,0 ,0 ); }else { ret =generic_file_read_iter (iocb, to);if (ret == -EIO )
include/linux/iomap.h +2 -2
@@ -276,10 +276,10 @@ struct iomap_dio_ops {ssize_t iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -unsigned int dio_flags); +unsigned int dio_flags,size_t done_before);struct iomap_dio *__iomap_dio_rw (struct kiocb *iocb,struct iov_iter *iter,const struct iomap_ops *ops,const struct iomap_dio_ops *dops, -unsigned int dio_flags); +unsigned int dio_flags,size_t done_before);ssize_t iomap_dio_complete (struct iomap_dio *dio);int iomap_dio_iopoll (struct kiocb *kiocb,bool spin);
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 11/12 iov_iter: Introduce noio flag to disable page faults
In reply to Andreas GruenbacherIntroduce a new noio flag to indicate to get_user_pages to use the FOLL_FAST_ONLY flag. This will cause get_user_pages to fail when it would otherwise fault in a page.
Currently, the noio flag is only checked in iov_iter_get_pages and iov_iter_get_pages_alloc. This is enough for iomaop_dio_rw, but it may make sense to check for this flag in other contexts as well.
--- include/linux/uio.h | 1 + lib/iov_iter.c | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-)
include/linux/uio.h +1 -0
@@ -29,6 +29,7 @@ enum iter_type {struct iov_iter {u8 iter_type; +bool noio;bool data_source;size_t iov_offset;size_t count;
lib/iov_iter.c +15 -5
@@ -519,6 +519,7 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction,WARN_ON (direction & ~(READ |WRITE )); *i = (struct iov_iter ) { .iter_type =ITER_IOVEC , + .noio = false, .data_source = direction, .iov = iov, .nr_segs = nr_segs,
@@ -1529,13 +1530,17 @@ ssize_t iov_iter_get_pages(struct iov_iter *i,return 0 ;if (likely (iter_is_iovec (i))) { +unsigned int gup_flags =0 ;unsigned long addr; +if (iov_iter_rw (i) !=WRITE ) + gup_flags |=FOLL_WRITE ; +if (i->noio) + gup_flags |=FOLL_FAST_ONLY ; + addr =first_iovec_segment (i, &len, start, maxsize, maxpages); n =DIV_ROUND_UP (len,PAGE_SIZE ); - res =get_user_pages_fast (addr, n, -iov_iter_rw (i) !=WRITE ?FOLL_WRITE :0 , - pages); + res =get_user_pages_fast (addr, n, gup_flags, pages);if (unlikely (res <=0 ))return res;return (res == n ? len : res *PAGE_SIZE ) - *start;
@@ -1651,15 +1656,20 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,return 0 ;if (likely (iter_is_iovec (i))) { +unsigned int gup_flags =0 ;unsigned long addr; +if (iov_iter_rw (i) !=WRITE ) + gup_flags |=FOLL_WRITE ; +if (i->noio) + gup_flags |=FOLL_FAST_ONLY ; + addr =first_iovec_segment (i, &len, start, maxsize, ~0U ); n =DIV_ROUND_UP (len,PAGE_SIZE ); p =get_pages_array (n);if (!p)return -ENOMEM ; - res =get_user_pages_fast (addr, n, -iov_iter_rw (i) !=WRITE ?FOLL_WRITE :0 , p); + res =get_user_pages_fast (addr, n, gup_flags, p);if (unlikely (res <=0 )) {kvfree (p); *pages =NULL ;
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v5 12/12 gfs2: Fix mmap + page fault deadlocks for direct I/O
In reply to Andreas GruenbacherAlso disable page faults during direct I/O requests and implement the same kind of retry logic as in the buffered I/O case.
Direct I/O requests differ from buffered I/O requests in that they use bio_iov_iter_get_pages for grabbing page references and faulting in pages instead of triggering physical page faults. Those manual page faults can be disabled with the new iocb->noio flag.
--- fs/gfs2/file.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-)
fs/gfs2/file.c +45 -2
@@ -782,21 +782,47 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,struct file *file = iocb->ki_filp;struct gfs2_inode *ip =GFS2_I (file->f_mapping->host);size_t count =iov_iter_count (to); +size_t written =0 ;ssize_t ret; +/* +* In this function, we disable page faults when we're holding the +* inode glock while doing I/O. If a page fault occurs, we drop the +* inode glock, fault in the pages manually, and retry. +* +* Unlike generic_file_read_iter, for reads, iomap_dio_rw can trigger +* physical as well as manual page faults, and we need to disable both +* kinds. +*/ +if (!count)return 0 ;/* skip atime */ gfs2_holder_init (ip->i_gl,LM_ST_DEFERRED ,0 , gh); +retry: ret =gfs2_glock_nq (gh);if (ret) goto out_uninit; - ret =iomap_dio_rw (iocb, to, &gfs2_iomap_ops,NULL ,0 ,0 ); +pagefault_disable (); + to->noio = true; + ret =iomap_dio_rw (iocb, to, &gfs2_iomap_ops,NULL , +IOMAP_DIO_PARTIAL , written); + to->noio = false; +pagefault_enable (); +gfs2_glock_dq (gh); +if (ret >0 ) + written = ret; +if (unlikely (iov_iter_count (to) && (ret >0 || ret == -EFAULT )) && +iter_is_iovec (to) && +fault_in_iov_iter_writeable (to,SIZE_MAX ) !=0 ) + goto retry; out_uninit:gfs2_holder_uninit (gh); -return ret; +if (ret <0 ) +return ret; +return written; } static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
@@ -809,6 +835,15 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,loff_t offset = iocb->ki_pos;ssize_t ret; +/* +* In this function, we disable page faults when we're holding the +* inode glock while doing I/O. If a page fault occurs, we drop the +* inode glock, fault in the pages manually, and retry. +* +* For writes, iomap_dio_rw only triggers manual page faults, so we +* don't need to disable physical ones. +*/ +/* * Deferred lock, even if its a write, since we do no allocation on * this path. All we need to change is the atime, and this lock mode
@@ -818,6 +853,7 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,* VFS does. */ gfs2_holder_init (ip->i_gl,LM_ST_DEFERRED ,0 , gh); +retry: ret =gfs2_glock_nq (gh);if (ret) goto out_uninit;
@@ -826,11 +862,18 @@ 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; + from->noio = true; ret =iomap_dio_rw (iocb, from, &gfs2_iomap_ops,NULL ,0 ,0 ); + from->noio = false; +if (ret == -ENOTBLK ) ret =0 ; out:gfs2_glock_dq (gh); +if (unlikely (ret == -EFAULT ) && +iter_is_iovec (from) && +fault_in_iov_iter_readable (from, len) == len) + goto retry; out_uninit:gfs2_holder_uninit (gh);return ret;
Signature · 1 line
2.26.3
Quoted 14 lines · Andreas Gruenbacher
With this patch queue, fstest generic/208 (
aio-dio-invalidate-failure.c) endlessly spins ingfs2_file_direct_write. It looks as if there's a bug inget_user_pages_fastwhen called withFOLL_FAST_ONLY:(1) The test case performs an aio write into a 32 MB buffer.
(2) The buffer is initially not in memory, so when
iomap_dio_rw()-> ... ->bio_iov_iter_get_pages()is called with theiter->noioflag set, we get toget_user_pages_fast()withFOLL_FAST_ONLYset.get_user_pages_fast()returns 0, which causesbio_iov_iter_get_pagesto return-EFAULT.
Quoted 6 lines
(3) Then
gfs2_file_direct_writefaults in the entire buffer withfault_in_iov_iter_readable(), which succeeds.(4) With the buffer in memory, we retry the
iomap_dio_rw()-> ... ->bio_iov_iter_get_pages()-> ... ->get_user_pages_fast(). This should succeed now, butget_user_pages_fast()still returns 0.
Hmm. Have you tried to figure out why that "still returns 0" happens?
One option - for debugging only - would be to introduce a new flag to get_user_pages_fast() tyhat says "print out reason if failed" and make the retry (but not the original one) have that flag set.
There are a couple of things of note when it comes to "get_user_pages_fast()":
(a) some architectures don't even enable it
(b) it can be very picky about the page table contents, and wants the
accessed bit to already be set (or the dirty bit, in the case of a write).
but (a) shouldn't be an issue on any common platform and (b) shouldn't be an issue with fault_in_iov_iter_readable() that actually does a __get_user() so it will access through the page tables.
(It might be more of an issue with fault_in_iov_iter_writable() due to walking the page tables by hand - if we don't do the proper access/dirty setting, I could see get_user_pages_fast() failing).
Anyway, for reason (a) I do think that eventually we should probably introduce FOLL_NOFAULT, and allow the full "slow" page table walk - just not calling down to handle_mm_fault() if it fails.
But (a) should be a non-issue in your test environment, and so it would be interesting to hear what it is that fails. Because scanning through the patches, they all _look_ fine to me (apart from the one comment about return values, which is more about being consistent with copy_to/from_user() and making the code simpler - not about correctness)
Linus
Andreas Gruenbacher
maintainerQuoted 13 lines · Linus Torvalds
Quoted 12 lines · Andreas Gruenbacher
With this patch queue, fstest generic/208 (
aio-dio-invalidate-failure.c) endlessly spins ingfs2_file_direct_write. It looks as if there's a bug inget_user_pages_fastwhen called withFOLL_FAST_ONLY:(1) The test case performs an aio write into a 32 MB buffer.
(2) The buffer is initially not in memory, so when
iomap_dio_rw()-> ... ->bio_iov_iter_get_pages()is called with theiter->noioflag set, we get toget_user_pages_fast()withFOLL_FAST_ONLYset.get_user_pages_fast()returns 0, which causesbio_iov_iter_get_pagesto return-EFAULT.
Quoted 8 lines
Quoted 6 lines
(3) Then
gfs2_file_direct_writefaults in the entire buffer withfault_in_iov_iter_readable(), which succeeds.(4) With the buffer in memory, we retry the
iomap_dio_rw()-> ... ->bio_iov_iter_get_pages()-> ... ->get_user_pages_fast(). This should succeed now, butget_user_pages_fast()still returns 0.Hmm. Have you tried to figure out why that "still returns 0" happens?
The call stack is:
gup_pte_rangegup_pmd_rangegup_pud_rangegup_p4d_rangegup_pgd_rangelockless_pages_from_mminternal_get_user_pages_fastget_user_pages_fastiov_iter_get_pages__bio_iov_iter_get_pagesbio_iov_iter_get_pagesiomap_dio_bio_actoriomap_dio_actoriomap_applyiomap_dio_rwgfs2_file_direct_write
In gup_pte_range, pte_special(pte) is true and so we return 0.
Quoted 24 lines
One option - for debugging only - would be to introduce a new flag to
get_user_pages_fast()that says "print out reason if failed" and make the retry (but not the original one) have that flag set.There are a couple of things of note when it comes to "
get_user_pages_fast()":(a) some architectures don't even enable it
(b) it can be very picky about the page table contents, and wants the
accessed bit to already be set (or the dirty bit, in the case of a write).but (a) shouldn't be an issue on any common platform and (b) shouldn't be an issue with
fault_in_iov_iter_readable()that actually does a__get_user()so it will access through the page tables.(It might be more of an issue with
fault_in_iov_iter_writable()due to walking the page tables by hand - if we don't do the proper access/dirty setting, I could seeget_user_pages_fast()failing).Anyway, for reason (a) I do think that eventually we should probably introduce
FOLL_NOFAULT, and allow the full "slow" page table walk - just not calling down tohandle_mm_fault()if it fails.
Quoted 9 lines
But (a) should be a non-issue in your test environment, and so it would be interesting to hear what it is that fails. Because scanning through the patches, they all _look_ fine to me (apart from the one comment about return values, which is more about being consistent with copy_to/from_user() and making the code simpler - not about correctness)
Linus
Thanks,
Andreas
[ Sorry for the delay, I was on the road and this fell through the cracks ]
Quoted 25 lines · Andreas Gruenbacher
Quoted 2 lines · Linus Torvalds
Hmm. Have you tried to figure out why that "still returns 0" happens?
The call stack is:
gup_pte_rangegup_pmd_rangegup_pud_rangegup_p4d_rangegup_pgd_rangelockless_pages_from_mminternal_get_user_pages_fastget_user_pages_fastiov_iter_get_pages__bio_iov_iter_get_pagesbio_iov_iter_get_pagesiomap_dio_bio_actoriomap_dio_actoriomap_applyiomap_dio_rwgfs2_file_direct_write
Quoted 1 line
In
gup_pte_range,pte_special(pte)is true and so we return 0.
Ok, so that is indeed something that the fast-case can't handle, because some of the special code wants to have the mm_lock so that it can look at the vma flags (eg "vm_normal_page()" and friends.
That said, some of these cases even the full GUP won't ever handle, simply because a mapping doesn't necessarily even _have_ a 'struct page' associated with it if it's a VM_IO mapping.
So it turns out that you can't just always do fault_in_iov_iter_readable() and then assume that you can do iov_iter_get_pages() and repeat until successful.
We could certainly make get_user_pages_fast() handle a few more cases, but I get the feeling that we need to have separate error cases for EFAULT - no page exists - and the "page exists, but cannot be mapped as a 'struct page'" case.
I also do still think that even regardless of that, we want to just add a FOLL_NOFAULT flag that just disables calling handle_mm_fault(), and then you can use the regular get_user_pages().
That at least gives us the full _normal_ page handling stuff.
Linus
Andreas Gruenbacher
maintainerQuoted 1 line · Linus Torvalds
[ Sorry for the delay, I was on the road and this fell through the cracks ]
No harm done, I was busy enough implementing your previous suggestions.
Quoted 37 lines
Quoted 25 lines · Andreas Gruenbacher
Quoted 2 lines · Linus Torvalds
Hmm. Have you tried to figure out why that "still returns 0" happens?
The call stack is:
gup_pte_rangegup_pmd_rangegup_pud_rangegup_p4d_rangegup_pgd_rangelockless_pages_from_mminternal_get_user_pages_fastget_user_pages_fastiov_iter_get_pages__bio_iov_iter_get_pagesbio_iov_iter_get_pagesiomap_dio_bio_actoriomap_dio_actoriomap_applyiomap_dio_rwgfs2_file_direct_writeQuoted 1 line
In
gup_pte_range,pte_special(pte)is true and so we return 0.Ok, so that is indeed something that the fast-case can't handle, because some of the special code wants to have the
mm_lockso that it can look at the vma flags (eg "vm_normal_page()" and friends.That said, some of these cases even the full GUP won't ever handle, simply because a mapping doesn't necessarily even _have_ a 'struct page' associated with it if it's a
VM_IOmapping.
Quoted 8 lines
So it turns out that you can't just always do
fault_in_iov_iter_readable()and then assume that you can doiov_iter_get_pages()and repeat until successful.We could certainly make
get_user_pages_fast()handle a few more cases, but I get the feeling that we need to have separate error cases for EFAULT - no page exists - and the "page exists, but cannot be mapped as a 'struct page'" case.
Hmm, what if GUP is made to skip VM_IO vmas without adding anything to the pages array? That would match fault_in_iov_iter_writeable, which is modeled after __mm_populate and which skips VM_IO and VM_PFNMAP vmas.
The other strategy I've added is to scale back the page fault windows to a single page if faulting in multiple pages didn't help, and to give up if the I/O operation still fails after that. So pathological cases won't loop indefinitely anymore at least.
Quoted 5 lines
I also do still think that even regardless of that, we want to just add a
FOLL_NOFAULTflag that just disables callinghandle_mm_fault(), and then you can use the regularget_user_pages().That at least gives us the full _normal_ page handling stuff.
And it does fix the generic/208 failure.
Thanks,
Andreas
Quoted 5 lines · Andreas Gruenbacher
Hmm, what if GUP is made to skip
VM_IOvmas without adding anything to the pages array? That would matchfault_in_iov_iter_writeable, which is modeled after__mm_populateand which skipsVM_IOandVM_PFNMAPvmas.
I don't understand what you mean.. GUP already skips VM_IO (and VM_PFNMAP) pages. It just returns EFAULT.
We could make it return another error. We already have DAX and FOLL_LONGTERM returning -EOPNOTSUPP.
Of course, I think some code ends up always just returning "number of pages looked up" and might return 0 for "no pages" rather than the error for the first page.
So we may end up having interfaces that then lose that explanation error code, but I didn't check.
But we couldn't make it just say "skip them and try later addresses", if that is what you meant. THAT makes no sense - that would just make GUP look up some other address than what was asked for.
Quoted 7 lines
Quoted 5 lines
I also do still think that even regardless of that, we want to just add a
FOLL_NOFAULTflag that just disables callinghandle_mm_fault(), and then you can use the regularget_user_pages().That at least gives us the full _normal_ page handling stuff.
And it does fix the generic/208 failure.
Good. So I think the approach is usable, even if we might have corner cases left.
So I think the remaining issue is exactly things like VM_IO and VM_PFNMAP. Do the fstests have test-cases for things like this? It _is_ quite specialized, it might be a good idea to have that.
Of course, doing direct-IO from special memory regions with zerocopy might be something special people actually want to do. But I think we've had that VM_IO flag testing there basically forever, so I don't think it has ever worked (for some definition of "ever").
Linus
Andreas Gruenbacher
maintainerQuoted 18 lines · Linus Torvalds
Quoted 5 lines · Andreas Gruenbacher
Hmm, what if GUP is made to skip
VM_IOvmas without adding anything to the pages array? That would matchfault_in_iov_iter_writeable, which is modeled after__mm_populateand which skipsVM_IOandVM_PFNMAPvmas.I don't understand what you mean.. GUP already skips
VM_IO(andVM_PFNMAP) pages. It just returns EFAULT.We could make it return another error. We already have DAX and
FOLL_LONGTERMreturning-EOPNOTSUPP.Of course, I think some code ends up always just returning "number of pages looked up" and might return 0 for "no pages" rather than the error for the first page.
Quoted 6 lines
So we may end up having interfaces that then lose that explanation error code, but I didn't check.
But we couldn't make it just say "skip them and try later addresses", if that is what you meant. THAT makes no sense - that would just make GUP look up some other address than what was asked for.
get_user_pages has a start and a nr_pages argument, which specifies an address range from start to start + nr_pages * PAGE_SIZE. If pages != NULL, it adds a pointer to that array for each PAGE_SIZE subpage. I was thinking of skipping over VM_IO vmas in that process, so when the range starts in a mappable vma, runs into a VM_IO vma, and ends in a mappable vma, the pages in the pages array would be discontiguous; they would only cover the mappable vmas. But that would make it difficult to make sense of what's in the pages array. So scratch that idea.
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
I also do still think that even regardless of that, we want to just add a
FOLL_NOFAULTflag that just disables callinghandle_mm_fault(), and then you can use the regularget_user_pages().That at least gives us the full _normal_ page handling stuff.
And it does fix the generic/208 failure.
Good. So I think the approach is usable, even if we might have corner cases left.
Quoted 8 lines
So I think the remaining issue is exactly things like
VM_IOandVM_PFNMAP. Do the fstests have test-cases for things like this? It _is_ quite specialized, it might be a good idea to have that.Of course, doing direct-IO from special memory regions with zerocopy might be something special people actually want to do. But I think we've had that
VM_IOflag testing there basically forever, so I don't think it has ever worked (for some definition of "ever").
The v6 patch queue should handle those cases acceptably well for now, but I don't think we have tests covering that at all.
Thanks,
Andreas