index 4f476411a9a2d..98b37e4c6d43c 100644 --- b/mm/filemap.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff -puN mm/filemap.c~generic_perform_write-1 mm/filemap.c --- a/mm/filemap.c~generic_perform_write-1 2025-01-27 09:53:13.219120969 -0800 +++ b/mm/filemap.c 2025-01-27 12:28:40.333920434 -0800 @@ -4027,17 +4027,6 @@ retry: bytes = min(chunk - offset, bytes); balance_dirty_pages_ratelimited(mapping); - /* - * Bring in the user page that we will copy from _first_. - * Otherwise there's a nasty deadlock on copying from the - * same page as we're writing to, without it being marked - * up-to-date. - */ - if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) { - status = -EFAULT; - break; - } - if (fatal_signal_pending(current)) { status = -EINTR; break; @@ -4055,6 +4044,11 @@ retry: if (mapping_writably_mapped(mapping)) flush_dcache_folio(folio); + /* + * This needs to be atomic because actually handling page + * faults on 'i' can deadlock if the copy targets a + * userspace mapping of 'folio'. + */ copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); flush_dcache_folio(folio); @@ -4080,6 +4074,15 @@ retry: bytes = copied; goto retry; } + /* + * 'folio' is now unlocked and faults on it can be + * handled. Ensure forward progress by trying to + * fault it in now. + */ + if (fault_in_iov_iter_readable(i, bytes) == bytes) { + status = -EFAULT; + break; + } } else { pos += status; written += status; _