gfs2: Fix mmap + page fault deadlocks
Subsystems (9)
Andreas Gruenbacher
maintainerv3 cover gfs2: Fix mmap + page fault deadlocks
Hi Linus et al.,
here's an update of my gfs2 mmap + page fault fixes (against -rc2). From my point of view,
* these two are ready and need to be looked at by Al:
iov_iter: Introduce fault_in_iov_iter helper
iov_iter: Introduce noio flag to disable page faults
* these two need to be reviewed by Christoph at least:
iomap: Fix iomap_dio_rw return value for user copies
iomap: Support restarting direct I/O requests after user copy failures
Thanks a lot,
Andreas
Andreas Gruenbacher (7):
iov_iter: Introduce fault_in_iov_iter helper
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
iov_iter: Introduce noio flag to disable page faults
gfs2: Fix mmap + page fault deadlocks for direct I/O
fs/gfs2/file.c | 77 ++++++++++++++++++++++++++++++++++++++++---- fs/iomap/direct-io.c | 13 ++++++-- include/linux/mm.h | 3 ++ include/linux/uio.h | 2 ++ lib/iov_iter.c | 62 ++++++++++++++++++++++++++++++++--- mm/gup.c | 68 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 211 insertions(+), 14 deletions(-)
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v3 1/7 iov_iter: Introduce fault_in_iov_iter helper
Introduce a new fault_in_iov_iter helper for manually faulting in an iterator. Other than fault_in_pages_writeable(), this function is non-destructive.
We'll use fault_in_iov_iter in gfs2 once we've determined that the iterator passed to .read_iter or .write_iter isn't in memory.
--- include/linux/mm.h | 3 ++ include/linux/uio.h | 1 + lib/iov_iter.c | 42 ++++++++++++++++++++++++++++ mm/gup.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+)
include/linux/mm.h +3 -0
@@ -1840,6 +1840,9 @@ int get_user_pages_fast(unsigned long start, int nr_pages,int pin_user_pages_fast (unsigned long start,int nr_pages,unsigned int gup_flags,struct page **pages); +unsigned long fault_in_user_pages (unsigned long start,unsigned long len, +bool write); +int account_locked_vm (struct mm_struct *mm,unsigned long pages,bool inc);int __account_locked_vm (struct mm_struct *mm,unsigned long pages,bool inc,struct task_struct *task,bool bypass_rlim);
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);int iov_iter_fault_in_readable (const struct iov_iter *i,size_t bytes); +size_t fault_in_iov_iter (const struct iov_iter *i);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 +42 -0
48 lines not on this page · raw message
mm/gup.c +68 -0
74 lines not on this page · raw message
Signature · 1 line
2.26.3
Re: [PATCH v3 1/7] iov_iter: Introduce fault_in_iov_iter helper
Quoted 3 lines · Andreas Gruenbacher
Introduce a new
fault_in_iov_iterhelper for manually faulting in an iterator. Other thanfault_in_pages_writeable(), this function is non-destructive.
Again, as I pointed out in the previous version, "Other than" is not sensible language.
You mean "Unlike".
Same issue in the comment:
Quoted 2 lines
+ * Other than
fault_in_pages_writeable(), this function is non-destructive even
+ * when faulting in pages for writing.
It really should be
"Unlike fault_in_pages_writeable(), this function .."
to parse correctly.
I understand what you mean, but only because I know what fault_in_pages_writeable() does and what the issue was.
And in a year or two, I might have forgotten, and wonder what you meant.
Linus
Re: [PATCH v3 1/7] iov_iter: Introduce fault_in_iov_iter helper
Am Sa., 24. Juli 2021 um 01:41 Uhr schrieb Linus Torvalds <[email protected]>:
Quoted 7 lines · Andreas Gruenbacher
Quoted 3 lines
Introduce a new
fault_in_iov_iterhelper for manually faulting in an iterator. Other thanfault_in_pages_writeable(), this function is non-destructive.Again, as I pointed out in the previous version, "Other than" is not sensible language.
Thanks for pointing this out a second time. I have no idea how I screwed up fixing that.
Andreas
Al Viro
maintainerRe: [PATCH v3 1/7] iov_iter: Introduce fault_in_iov_iter helper
In reply to Andreas GruenbacherQuoted 5 lines · Andreas Gruenbacher
Introduce a new
fault_in_iov_iterhelper for manually faulting in an iterator. Other thanfault_in_pages_writeable(), this function is non-destructive.We'll use
fault_in_iov_iterin gfs2 once we've determined that the iterator passed to .read_iter or .write_iter isn't in memory.
Hmm... I suspect that this is going to be much heavier for read access than the existing variant. Do we ever want it for anything other than writes?
Re: [PATCH v3 1/7] iov_iter: Introduce fault_in_iov_iter helper
Am Sa., 24. Juli 2021 um 03:53 Uhr schrieb Al Viro <[email protected]>:
Quoted 10 lines · Andreas Gruenbacher
Quoted 5 lines
Introduce a new
fault_in_iov_iterhelper for manually faulting in an iterator. Other thanfault_in_pages_writeable(), this function is non-destructive.We'll use
fault_in_iov_iterin gfs2 once we've determined that the iterator passed to .read_iter or .write_iter isn't in memory.Hmm... I suspect that this is going to be much heavier for read access than the existing variant. Do we ever want it for anything other than writes?
I don't know if it actually is slower when pages need to be faulted in, but I'm fine turning it into a write-only function.
Thanks,
Andreas
Jan Kara
reviewerRe: [PATCH v3 1/7] iov_iter: Introduce fault_in_iov_iter helper
In reply to Andreas GruenbacherQuoted 7 lines · Andreas Gruenbacher
Introduce a new
fault_in_iov_iterhelper for manually faulting in an iterator. Other thanfault_in_pages_writeable(), this function is non-destructive.We'll use
fault_in_iov_iterin gfs2 once we've determined that the iterator passed to .read_iter or .write_iter isn't in memory.Signed-off-by Andreas Gruenbacher <[email protected]>
...
Quoted 9 lines
mm/gup.c +9
-0+unsigned long fault_in_user_pages (unsigned long start,unsigned long len, +bool write) +{ +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma =NULL ; +unsigned long end, nstart, nend; +int locked =0 ; +int gup_flags; +
mm/gup.c static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, · line 1702
Quoted 10 lines
mm/gup.c +10
-0+/* +* FIXME: Make sure this function doesn't succeed for pages that cannot +* be accessed; otherwise we could end up in a loop trying to fault in +* and then access the pages. (It's okay if a page gets evicted and we +* need more than one retry.) +*/ + +/* +* FIXME: Are these the right FOLL_* flags? +*/
How about the FIXMEs here? I guess we should answer these questions before merging and remove the comments. Regarding the first FIXME I tend to agree that if we cannot fault in the first page, we should return the error rather than returning 0 as you do now. OTOH the caller can check for 0 and understand there's something wrong going on as well. But the error would be probably a bit clearer.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + gup_flags = FOLL_TOUCH | FOLL_POPULATE;
I don't think FOLL_POPULATE makes sense here. It makes sense only with FOLL_MLOCK and determines whether mlock(2) should fault in missing pages or not.
Honza
Quoted 41 lines
mm/gup.c +35
-0+if (write) + gup_flags |=FOLL_WRITE ; + + end =PAGE_ALIGN (start + len); +for (nstart = start &PAGE_MASK ; nstart < end; nstart = nend) { +unsigned long nr_pages; +long ret; + +if (!locked) { + 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 ; +if (nstart < vma->vm_start) + nstart = vma->vm_start; + nr_pages = (nend - nstart) /PAGE_SIZE ; + ret =__get_user_pages_locked (mm, nstart, nr_pages, +NULL ,NULL , &locked, gup_flags); +if (ret <=0 ) +break ; + nend = nstart + ret *PAGE_SIZE ; + } +if (locked) +mmap_read_unlock (mm); +if (nstart > start) +return min (nstart - start, len); +return 0 ; +} +/** * get_dump_page() - pin user page in memory while writing it to core dump * @addr: user address Signature · 1 line
2.26.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v3 1/7] iov_iter: Introduce fault_in_iov_iter helper
Quoted 7 lines · Jan Kara
Quoted 1 line · Andreas Gruenbacher
+
gup_flags=FOLL_TOUCH|FOLL_POPULATE;I don't think
FOLL_POPULATEmakes sense here. It makes sense only withFOLL_MLOCKand determines whether mlock(2) should fault in missing pages or not.
Yeah, it won't hurt, but FOLL_POPULATE doesn't actually do anything unless FOLL_MLOCK is set. It is, as you say, a magic flag just for mlock.
The only ones that should matter are FOLL_WRITE (for obvious reasons) and FOLL_TOUCH (to set the accessed and dirty bits, rather than just th protection bits)
Linus
Andreas Gruenbacher
maintainerPATCH v3 2/7 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 v3 3/7 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 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
fs/gfs2/file.c +23 -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 then we retry. +*/ +if (iocb->ki_flags &IOCB_DIRECT ) { ret =gfs2_file_direct_read (iocb, to, &gh);if (likely (ret != -ENOTBLK ))
@@ -864,13 +870,19 @@ 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 )) && +fault_in_iov_iter (to)) + goto retry; out_uninit:gfs2_holder_uninit (&gh);return written ? written : ret;
@@ -882,9 +894,20 @@ 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 then we 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 ) &&fault_in_iov_iter (from)) + goto retry;return ret; }
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v3 4/7 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 already returns when it gets an -EFAULT from bio_iov_iter_get_pages.
--- 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 ?: -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 ?: -EFAULT ; } static loff_t
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v3 5/7 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 and reset the iterator to the start position. This allows callers to deal with the failure and retry the operation.
In gfs2, we need to disable page faults while we're holding glocks to prevent deadlocks. This patch is the minimum solution I could find to make iomap_dio_rw work with page faults disabled. It's still expensive because any I/O that was carried out before hitting -EFAULT needs to be retried.
A possible improvement would be to add an IOMAP_DIO_FAULT_RETRY or similar flag that would allow iomap_dio_rw to return a short result when hitting -EFAULT. Callers could then retry only the rest of the request after dealing with the page fault.
Asynchronous requests turn into synchronous requests up to the point of the page fault in any case, but they could be retried asynchronously after dealing with the page fault. To make that work, the completion notification would have to include the bytes read or written before the page fault(s) as well, and we'd need an additional iomap_dio_rw argument for that.
--- fs/iomap/direct-io.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/iomap/direct-io.c +9 -0
@@ -561,6 +561,15 @@ __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 ) { +/* +* To allow retrying the request, fail +* synchronously and reset the iterator. +*/ + wait_for_completion = true; +iov_iter_revert (dio->submit.iter, dio->size); + } +/* magic error code to fall back to buffered I/O */ if (ret == -ENOTBLK ) { wait_for_completion = true;
Signature · 1 line
2.26.3
Jan Kara
reviewerRe: [PATCH v3 5/7] iomap: Support restarting direct I/O requests after user copy failures
Quoted 32 lines · Andreas Gruenbacher
In
__iomap_dio_rw, wheniomap_applyreturns an-EFAULTerror, complete the request synchronously and reset the iterator to the start position. This allows callers to deal with the failure and retry the operation.In gfs2, we need to disable page faults while we're holding glocks to prevent deadlocks. This patch is the minimum solution I could find to make
iomap_dio_rwwork with page faults disabled. It's still expensive because any I/O that was carried out before hitting-EFAULTneeds to be retried.A possible improvement would be to add an
IOMAP_DIO_FAULT_RETRYor similar flag that would allowiomap_dio_rwto return a short result when hitting-EFAULT. Callers could then retry only the rest of the request after dealing with the page fault.Asynchronous requests turn into synchronous requests up to the point of the page fault in any case, but they could be retried asynchronously after dealing with the page fault. To make that work, the completion notification would have to include the bytes read or written before the page fault(s) as well, and we'd need an additional
iomap_dio_rwargument for that.Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/iomap/direct-io.c | 9 +++++++++ 1 file changed, 9 insertions(+)fs/iomap/direct-io.c +0
-0@@ -561,6 +561,15 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, ret =iomap_apply (inode, pos, count, iomap_flags, ops, dio, iomap_dio_actor);
fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 572
Quoted 10 lines
fs/iomap/direct-io.c +9
-0if (ret <=0 ) { +if (ret == -EFAULT ) { +/* +* To allow retrying the request, fail +* synchronously and reset the iterator. +*/ + wait_for_completion = true; +iov_iter_revert (dio->submit.iter, dio->size); + } +
Hum, OK, but this means that if userspace submits large enough write, GFS2 will livelock trying to complete it? While other filesystems can just submit multiple smaller bios constructed in iomap_apply() (paging in different parts of the buffer) and thus complete the write?
Honza
Quoted 6 lines
fs/iomap/direct-io.c +0
-0/* magic error code to fall back to buffered I/O */ if (ret == -ENOTBLK ) { wait_for_completion = true;Signature · 1 line
2.26.3
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v3 5/7 iomap: Support restarting direct I/O requests after user copy failures
Jan Kara <[email protected]> schrieb am Mo., 26. Juli 2021, 19:21:
Quoted 52 lines · Andreas Gruenbacher
Quoted 1 line
In
__iomap_dio_rw, wheniomap_applyreturns an-EFAULTerror, completethe
Quoted 4 lines
request synchronously and reset the iterator to the start position. This allows callers to deal with the failure and retry the operation.
In gfs2, we need to disable page faults while we're holding glocks to
prevent
Quoted 2 lines
deadlocks. This patch is the minimum solution I could find to make
iomap_dio_rwwork with page faults disabled. It's still expensivebecause any
Quoted 3 lines
I/O that was carried out before hitting
-EFAULTneeds to be retried.A possible improvement would be to add an
IOMAP_DIO_FAULT_RETRYorsimilar flag
Quoted 1 line
that would allow
iomap_dio_rwto return a short result when hitting
-EFAULT.Quoted 1 line
Callers could then retry only the rest of the request after dealing with
the
Quoted 3 lines
page fault.
Asynchronous requests turn into synchronous requests up to the point of
the
Quoted 1 line
page fault in any case, but they could be retried asynchronously after
dealing
Quoted 1 line
with the page fault. To make that work, the completion notification
would have
Quoted 1 line
to include the bytes read or written before the page fault(s) as well,
and we'd
fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter
Quoted 12 lines
need an additional
iomap_dio_rwargument for that.Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/iomap/direct-io.c | 9 +++++++++ 1 file changed, 9 insertions(+)fs/iomap/direct-io.c +0
-0@@ -561,6 +561,15 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter*iter,
fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
Quoted 10 lines
fs/iomap/direct-io.c +7
-0ret =iomap_apply (inode, pos, count, iomap_flags, ops, dio, iomap_dio_actor);if (ret <=0 ) { +if (ret == -EFAULT ) { +/* +* To allow retrying the request, fail +* synchronously and reset the iterator. +*/ + wait_for_completion = true; + iov_iter_revert(dio->submit.iter,
Quoted 9 lines
dio->size);fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 572
Quoted 2 lines
fs/iomap/direct-io.c +2
-0+ } +Hum, OK, but this means that if userspace submits large enough write, GFS2 will livelock trying to complete it? While other filesystems can just submit multiple smaller bios constructed in
iomap_apply()(paging in different parts of the buffer) and thus complete the write?
No. First, this affects reads but not writes. We cannot just blindly repeat writes; when a page fault occurs in the middle of a write, the result will be a short write. For reads, the plan is to ads a flag to allow iomap_dio_rw to return a partial result when a page fault occurs. (Currently, it fails the entire request.) Then we can handle the page fault and complete the rest of the request.
The changes needed for that are simple on the iomap side, but we need to go through some gymnastics for handling the page fault without giving up the glock in the non-contended case. There will still be the potential for losing the lock and having to re-acquire it, in which case we'll actually have to repeat the entire read.
Thanks,
Andreas
Honza
Quoted 11 lines
Quoted 7 lines
fs/iomap/direct-io.c +0
-1/* magic error code to fall back to buffered I/O */ if (ret == -ENOTBLK ) { wait_for_completion = true; --2.26.3
--
Jan Kara <[email protected]>
SUSE Labs, CR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/cluster-devel/attachments/20210726/7a37c739/attachment.htm>
Jan Kara
reviewerRe: [PATCH v3 5/7] iomap: Support restarting direct I/O requests after user copy failures
Quoted 71 lines · Andreas Gr?nbacher
Jan Kara <[email protected]> schrieb am Mo., 26. Juli 2021, 19:21:
Quoted 52 lines · Andreas Gruenbacher
Quoted 1 line
In
__iomap_dio_rw, wheniomap_applyreturns an-EFAULTerror, completethe
Quoted 4 lines
request synchronously and reset the iterator to the start position. This allows callers to deal with the failure and retry the operation.
In gfs2, we need to disable page faults while we're holding glocks to
prevent
Quoted 2 lines
deadlocks. This patch is the minimum solution I could find to make
iomap_dio_rwwork with page faults disabled. It's still expensivebecause any
Quoted 3 lines
I/O that was carried out before hitting
-EFAULTneeds to be retried.A possible improvement would be to add an
IOMAP_DIO_FAULT_RETRYorsimilar flag
Quoted 1 line
that would allow
iomap_dio_rwto return a short result when hitting
-EFAULT.Quoted 1 line
Callers could then retry only the rest of the request after dealing with
the
Quoted 3 lines
page fault.
Asynchronous requests turn into synchronous requests up to the point of
the
Quoted 1 line
page fault in any case, but they could be retried asynchronously after
dealing
Quoted 1 line
with the page fault. To make that work, the completion notification
would have
Quoted 1 line
to include the bytes read or written before the page fault(s) as well,
and we'd
fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter
Quoted 12 lines
need an additional
iomap_dio_rwargument for that.Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/iomap/direct-io.c | 9 +++++++++ 1 file changed, 9 insertions(+)fs/iomap/direct-io.c +0
-0@@ -561,6 +561,15 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter*iter,
fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
Quoted 10 lines
fs/iomap/direct-io.c +7
-0ret =iomap_apply (inode, pos, count, iomap_flags, ops, dio, iomap_dio_actor);if (ret <=0 ) { +if (ret == -EFAULT ) { +/* +* To allow retrying the request, fail +* synchronously and reset the iterator. +*/ + wait_for_completion = true; + iov_iter_revert(dio->submit.iter,Quoted 9 lines
dio->size);fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 572
Quoted 2 lines
fs/iomap/direct-io.c +2
-0+ } +Hum, OK, but this means that if userspace submits large enough write, GFS2 will livelock trying to complete it? While other filesystems can just submit multiple smaller bios constructed in
iomap_apply()(paging in different parts of the buffer) and thus complete the write?No. First, this affects reads but not writes. We cannot just blindly repeat writes; when a page fault occurs in the middle of a write, the result will be a short write. For reads, the plan is to ads a flag to allow
iomap_dio_rwto return a partial result when a page fault occurs. (Currently, it fails the entire request.) Then we can handle the page fault and complete the rest of the request.
Quoted 5 lines
The changes needed for that are simple on the iomap side, but we need to go through some gymnastics for handling the page fault without giving up the glock in the non-contended case. There will still be the potential for losing the lock and having to re-acquire it, in which case we'll actually have to repeat the entire read.
I've missed you've already sent out v4 (I'm catching up after vacation so my mailbox is a bit of a mess). What's in there addresses my objection. I'm sorry for the noise.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Andreas Gruenbacher
maintainerPATCH v3 6/7 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
@@ -509,6 +509,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,
@@ -1519,13 +1520,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;
@@ -1641,15 +1646,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 =ZERO_SIZE_PTR ;
Signature · 1 line
2.26.3
Andreas Gruenbacher
maintainerPATCH v3 7/7 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 real page faults. Those manual page faults can be disabled with the iocb->noio flag.
--- fs/gfs2/file.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
fs/gfs2/file.c +33 -1
@@ -782,21 +782,41 @@ 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 then we retry. Other +* than in gfs2_file_read_iter, iomap_dio_rw can trigger implicit as +* well as manual page faults, and we need to disable both kinds +* separately. +*/ +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; +pagefault_disable (); + to->noio = true; ret =iomap_dio_rw (iocb, to, &gfs2_iomap_ops,NULL ,0 ); + to->noio = false; +pagefault_enable (); +gfs2_glock_dq (gh); +if (ret >0 ) + written += ret; +if (unlikely (ret == -EFAULT ) &&fault_in_iov_iter (to)) + goto retry; out_uninit:gfs2_holder_uninit (gh); -return ret; +return written ? written : ret; } static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
@@ -809,6 +829,12 @@ 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 then we retry. +*/ +/* * 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 +844,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 +853,16 @@ 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 ); + from->noio = false; +if (ret == -ENOTBLK ) ret =0 ; out:gfs2_glock_dq (gh); +if (unlikely (ret == -EFAULT ) &&fault_in_iov_iter (from)) + goto retry; out_uninit:gfs2_holder_uninit (gh);return ret;
Signature · 1 line
2.26.3
Jan Kara
reviewerRe: [PATCH v3 7/7] gfs2: Fix mmap + page fault deadlocks for direct I/O
Quoted 33 lines · Andreas Gruenbacher
Also 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_pagesfor grabbing page references and faulting in pages instead of triggering real page faults. Those manual page faults can be disabled with theiocb->noioflag.Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/gfs2/file.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)fs/gfs2/file.c +10
-0@@ -782,21 +782,41 @@ 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 then we retry. Other +* than in gfs2_file_read_iter, iomap_dio_rw can trigger implicit as +* well as manual page faults, and we need to disable both kinds +* separately. +*/ +
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to, · line 806
Quoted 10 lines
fs/gfs2/file.c +2
-0if (!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; +pagefault_disable ();
Is there any use in pagefault_disable() here? iomap_dio_rw() should not trigger any page faults anyway, should it?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v3 7/7 gfs2: Fix mmap + page fault deadlocks for direct I/O
Jan Kara <[email protected]> schrieb am Mo., 26. Juli 2021, 19:10:
Quoted 48 lines · Andreas Gruenbacher
Quoted 1 line
Also disable page faults during direct I/O requests and implement the
same kind
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb
Quoted 17 lines
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_pagesfor grabbing page references and faulting in pages instead of triggering real page faults. Those manual page faults can be disabled with theiocb->noioflag.Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/gfs2/file.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)fs/gfs2/file.c +0
-0@@ -782,21 +782,41 @@ static ssize_t gfs2_file_direct_read(struct kiocb*iocb, struct
iov_iter*to,fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
Quoted 9 lines
fs/gfs2/file.c +4
-0struct 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
fs/gfs2/file.c static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from, · line 835
Quoted 1 line
fs/gfs2/file.c +1
-0+* inode glock, fault in the pages manually, and then we retry. Other
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb · line 792
Quoted 1 line
fs/gfs2/file.c +1
-0+* than in gfs2_file_read_iter, iomap_dio_rw can trigger implicit as
Quoted 13 lines
fs/gfs2/file.c +5
-0+* well as manual page faults, and we need to disable both kinds +* separately. +*/ +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;
Quoted 5 lines
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to, · line 806
Quoted 1 line
fs/gfs2/file.c +1
-0+pagefault_disable ();Is there any use in
pagefault_disable()here?iomap_dio_rw()should not trigger any page faults anyway, should it?
It can trigger physical page faults when reading from holes.
Andreas
Quoted 5 lines
Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/cluster-devel/attachments/20210726/82195967/attachment.htm>
Jan Kara
reviewerRe: [PATCH v3 7/7] gfs2: Fix mmap + page fault deadlocks for direct I/O
Quoted 50 lines · Andreas Gr?nbacher
Jan Kara <[email protected]> schrieb am Mo., 26. Juli 2021, 19:10:
Quoted 48 lines · Andreas Gruenbacher
Quoted 1 line
Also disable page faults during direct I/O requests and implement the
same kind
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb
Quoted 17 lines
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_pagesfor grabbing page references and faulting in pages instead of triggering real page faults. Those manual page faults can be disabled with theiocb->noioflag.Signed-off-by Andreas Gruenbacher <[email protected]>--- fs/gfs2/file.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)fs/gfs2/file.c +0
-0@@ -782,21 +782,41 @@ static ssize_t gfs2_file_direct_read(struct kiocb*iocb, struct
iov_iter*to,fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
Quoted 9 lines
fs/gfs2/file.c +4
-0struct 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
fs/gfs2/file.c static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from, · line 835
Quoted 1 line
fs/gfs2/file.c +1
-0+* inode glock, fault in the pages manually, and then we retry. Other
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb · line 792
Quoted 1 line
fs/gfs2/file.c +1
-0+* than in gfs2_file_read_iter, iomap_dio_rw can trigger implicit as
Quoted 13 lines
fs/gfs2/file.c +5
-0+* well as manual page faults, and we need to disable both kinds +* separately. +*/ +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;
Quoted 7 lines
Quoted 5 lines
fs/gfs2/file.c static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to, · line 806
Quoted 1 line
fs/gfs2/file.c +1
-0+pagefault_disable ();Is there any use in
pagefault_disable()here?iomap_dio_rw()should not trigger any page faults anyway, should it?It can trigger physical page faults when reading from holes.
Aha, good point. Maybe even worth a comment at this site? Thanks for explanation!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR