filemap: prefault writes starting beyond EOF
Subsystems (4)
PATCH 1/1 filemap: prefault writes starting beyond EOF
Since prefaulting moved to the slow write path, a buffered write can reach ->write_begin() and ->write_end() before discovering that its source buffer is unreadable. Several filesystems can extend i_size in these callbacks even when no data was copied. For example, a one-byte pwrite() at offset 4096 into an empty file can fail with EFAULT but leave a 4096-byte file.
Prefault the source before ->write_begin() when the write position is beyond EOF, and return EFAULT if no source bytes can be made readable. Overwrites and appends continue to skip this prefault. Keep the existing slow-path faulting for forward progress after a failed atomic copy.
This also leaves intentional zero-length ->write_end() calls used by generic_cont_expand_simple() unchanged. As with the previous prefaulting behavior, a concurrent userspace unmap can still race with the later copy.
---
mm/filemap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/filemap.c +10 -0
@@ -4389,6 +4389,16 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)break ; } +/* +* Some filesystems can extend i_size even when nothing was copied. +* Prefault writes beyond EOF to catch unreadable source buffers. +*/ +if (unlikely (pos >i_size_read (mapping->host)) && +fault_in_iov_iter_readable (i, bytes) == bytes) { + status = -EFAULT ; +break ; + } + status = a_ops->write_begin (iocb, mapping, pos, bytes, &folio, &fsdata);if (unlikely (status <0 ))
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0
Quoted 15 lines · Hungyu Lin
Since prefaulting moved to the slow write path, a buffered write can reach ->write_begin() and ->write_end() before discovering that its source buffer is unreadable. Several filesystems can extend
i_sizein these callbacks even when no data was copied. For example, a one-bytepwrite()at offset 4096 into an empty file can fail with EFAULT but leave a 4096-byte file.Prefault the source before ->write_begin() when the write position is beyond EOF, and return EFAULT if no source bytes can be made readable. Overwrites and appends continue to skip this prefault. Keep the existing slow-path faulting for forward progress after a failed atomic copy.
This also leaves intentional zero-length ->write_end() calls used by
generic_cont_expand_simple()unchanged. As with the previous prefaulting behavior, a concurrent userspace unmap can still race with the later copy.
Quoted 7 lines
Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")Reported-by Kiryl Shutsemau <[email protected]>Suggested-by Kiryl Shutsemau <[email protected]>Signed-off-by Hungyu Lin <[email protected]>
You found the bug, I only pointed out that it is not tmpfs-specific. Please drop Reported-by and Closes; Suggested-by and Link are enough.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 5 lines · Hungyu Lin
Since prefaulting moved to the slow write path, a buffered write can reach ->write_begin() and ->write_end() before discovering that its source buffer is unreadable. Several filesystems can extend
i_sizein these callbacks even when no data was copied. For example, a one-bytepwrite()at offset 4096 into an empty file can fail with EFAULT but leave a 4096-byte file.
I've been following this discussion rather passively on the other thread, so I'll ask my stupid question: Why is this a problem? Yes, it's a bit of weird behavior, but system calls are generally not expected to behave super sanely in the case of bad input/weird errors.
Quoted 9 lines
Prefault the source before ->write_begin() when the write position is beyond EOF, and return EFAULT if no source bytes can be made readable. Overwrites and appends continue to skip this prefault. Keep the existing slow-path faulting for forward progress after a failed atomic copy.
This also leaves intentional zero-length ->write_end() calls used by
generic_cont_expand_simple()unchanged. As with the previous prefaulting behavior, a concurrent userspace unmap can still race with the later copy.
Ok, so it's still racy? What do you do if you can't fault it in at all? You still get a hole?
Quoted 18 lines
Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")Reported-by Kiryl Shutsemau <[email protected]>Suggested-by Kiryl Shutsemau <[email protected]>Signed-off-by Hungyu Lin <[email protected]>---
mm/filemap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/filemap.c +0
-0
mm/filemap.c ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i) · line 4397
Quoted 9 lines
mm/filemap.c +6
-0@@ -4389,6 +4389,16 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)break ; } +/* +* Some filesystems can extend i_size even when nothing was copied. +* Prefault writes beyond EOF to catch unreadable source buffers. +*/ +if (unlikely (pos >i_size_read (mapping->host)) && +fault_in_iov_iter_readable (i, bytes) == bytes) {
I say this doesn't matter, and if you pass insane inputs you get insane outputs. Even more so considering this is racy, e.g races against munmap(), mremap(), mprotect(), memory poisoning, and other things like truncated memory mapped files sending out a SIGBUS (or uffd) :)
I don't think it makes to add random prefault loops in hopes this makes for saner semantics.
Signature · 1 line
Pedro
Quoted 11 lines · Pedro Falcato
Quoted 5 lines · Hungyu Lin
Since prefaulting moved to the slow write path, a buffered write can reach ->write_begin() and ->write_end() before discovering that its source buffer is unreadable. Several filesystems can extend
i_sizein these callbacks even when no data was copied. For example, a one-bytepwrite()at offset 4096 into an empty file can fail with EFAULT but leave a 4096-byte file.I've been following this discussion rather passively on the other thread, so I'll ask my stupid question: Why is this a problem? Yes, it's a bit of weird behavior, but system calls are generally not expected to behave super sanely in the case of bad input/weird errors.
The input does not have to be bad. Write from an mmap() of a file that another process has truncated: write() fails with EFAULT and the destination file has grown anyway. I see it on ext4 and tmpfs. Before 665575cff098 the file was left alone
Quoted 6 lines
Quoted 6 lines
Prefault the source before ->write_begin() when the write position is beyond EOF, and return EFAULT if no source bytes can be made readable. Overwrites and appends continue to skip this prefault. Keep the existing slow-path faulting for forward progress after a failed atomic copy.
Quoted 6 lines
Quoted 3 lines
This also leaves intentional zero-length ->write_end() calls used by
generic_cont_expand_simple()unchanged. As with the previous prefaulting behavior, a concurrent userspace unmap can still race with the later copy.Ok, so it's still racy? What do you do if you can't fault it in at all? You still get a hole?
Only if the buffer goes away between the prefault and the copy, e.g. another thread munmap()s it mid-write. That is userspace racing with itself, and every write had that window before 665575cff098.
Quoted 20 lines
Quoted 20 lines
Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")Reported-by Kiryl Shutsemau <[email protected]>Suggested-by Kiryl Shutsemau <[email protected]>Signed-off-by Hungyu Lin <[email protected]>---
mm/filemap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/filemap.c +0
-0@@ -4389,6 +4389,16 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)break ; }
Quoted 9 lines
mm/filemap.c ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i) · line 4397
Quoted 6 lines
mm/filemap.c +6
-0+/* +* Some filesystems can extend i_size even when nothing was copied. +* Prefault writes beyond EOF to catch unreadable source buffers. +*/ +if (unlikely (pos >i_size_read (mapping->host)) && +fault_in_iov_iter_readable (i, bytes) == bytes) {I say this doesn't matter, and if you pass insane inputs you get insane outputs.
As I mentioned above, it can happen with pretty legitimate input.
Quoted 6 lines
Even more so considering this is racy, e.g races
againstmunmap(),mremap(),mprotect(), memory poisoning, and other things like truncated memory mapped files sending out a SIGBUS (or uffd) :)I don't think it makes to add random prefault loops in hopes this makes for saner semantics.
A buffer that is already unmapped, PROT_NONE, poisoned or beyond EOF of its file fails the prefault, and the file is not touched. Only the races in your list get through.
It is one prefault, not a loop, and only for writes that start beyond EOF. Overwrites and appends keep the fast path.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 15 lines · Hungyu Lin
Since prefaulting moved to the slow write path, a buffered write can reach ->write_begin() and ->write_end() before discovering that its source buffer is unreadable. Several filesystems can extend
i_sizein these callbacks even when no data was copied. For example, a one-bytepwrite()at offset 4096 into an empty file can fail with EFAULT but leave a 4096-byte file.Prefault the source before ->write_begin() when the write position is beyond EOF, and return EFAULT if no source bytes can be made readable. Overwrites and appends continue to skip this prefault. Keep the existing slow-path faulting for forward progress after a failed atomic copy.
This also leaves intentional zero-length ->write_end() calls used by
generic_cont_expand_simple()unchanged. As with the previous prefaulting behavior, a concurrent userspace unmap can still race with the later copy.
Quoted 7 lines
Fixes 665575cff098 ("filemap: move prefaulting out of hot write path")Reported-by Kiryl Shutsemau <[email protected]>Suggested-by Kiryl Shutsemau <[email protected]>Signed-off-by Hungyu Lin <[email protected]>
Looks good! Feel free to add:
Honza
Quoted 31 lines
---
mm/filemap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)mm/filemap.c +10
-0@@ -4389,6 +4389,16 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)break ; } +/* +* Some filesystems can extend i_size even when nothing was copied. +* Prefault writes beyond EOF to catch unreadable source buffers. +*/ +if (unlikely (pos >i_size_read (mapping->host)) && +fault_in_iov_iter_readable (i, bytes) == bytes) { + status = -EFAULT ; +break ; + } + status = a_ops->write_begin (iocb, mapping, pos, bytes, &folio, &fsdata);if (unlikely (status <0 ))base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
[merged mm-hotfixes-stable] filemap-prefault-writes-starting-beyond-eof.patch removed from -mm tree
In reply to Hungyu LinThe quilt patch titled Subject: filemap: prefault writes starting beyond EOF has been removed from the -mm tree. Its filename was filemap-prefault-writes-starting-beyond-eof.patch
The quilt patch titled
Subject: filemap: prefault writes starting beyond EOF
has been removed from the -mm tree. Its filename was
filemap-prefault-writes-starting-beyond-eof.patch
This patch was dropped because it was merged into the mm-hotfixes-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: Hungyu Lin <[email protected]>
Subject: filemap: prefault writes starting beyond EOF
Date: Wed, 04 Nov 2026 07:10:02 +0000
Since prefaulting moved to the slow write path, a buffered write can reach ->write_begin() and ->write_end() before discovering that its source buffer is unreadable. Several filesystems can extend i_size in these callbacks even when no data was copied. For example, a one-byte pwrite() at offset 4096 into an empty file can fail with EFAULT but leave a 4096-byte file.
Prefault the source before ->write_begin() when the write position is beyond EOF, and return EFAULT if no source bytes can be made readable. Overwrites and appends continue to skip this prefault. Keep the existing slow-path faulting for forward progress after a failed atomic copy.
This also leaves intentional zero-length ->write_end() calls used by generic_cont_expand_simple() unchanged. As with the previous prefaulting behavior, a concurrent userspace unmap can still race with the later copy.
mm/filemap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
mm/filemap.c renamed from mm/filemap.c~filemap-prefault-writes-starting-beyond-eof +10 -0
@@ -4404,6 +4404,16 @@ retry:break ; } +/* +* Some filesystems can extend i_size even when nothing was copied. +* Prefault writes beyond EOF to catch unreadable source buffers. +*/ +if (unlikely (pos >i_size_read (mapping->host)) && +fault_in_iov_iter_readable (i, bytes) == bytes) { + status = -EFAULT ; +break ; + } + status = a_ops->write_begin (iocb, mapping, pos, bytes, &folio, &fsdata);if (unlikely (status <0 ))
_
Patches currently in -mm which might be from [email protected] are
mm-page-writeback-avoid-overflow-when-reporting-bdi-byte-limits.patch mm-skip-empty-finite-ranges-in-truncate_pagecache_range.patch hugetlbfs-report-inode-limits-without-a-memory-subpool.patch delayacct-preserve-delay-extrema-when-aggregating-taskstats.patch shmem-clamp-negative-used-block-samples-in-statfs.patch