FAILED: patch "[PATCH] netfs: clear post-EOF pagecache when extending a file via" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 69bf14300b50631e7c2271289d347bbd1b19a06c
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026100343-halt-swimmer-cc9a@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 69bf14300b50631e7c2271289d347bbd1b19a06c Mon Sep 17 00:00:00 2001
From: Paulo Alcantara <[email protected]>
Date: Mon, 21 Sep 2026 11:17:26 -0300
Subject: [PATCH] netfs: clear post-EOF pagecache when extending a file via
write
Fix netfs to erase the contents of a hole created after the EOF by an ordinary write if dirty data has been previously left there by writes through an mmapped region. Neither the buffered nor the unbuffered/DIO write path clears that stale pagecache.
Zero the tail of the folio straddling the EOF before an extending write. That is the only folio that can hold data written past the EOF through an mmap, as pages wholly beyond the EOF can't be faulted in. The folio is zeroed rather than dropped so a concurrent extending write can't lose data.
Both write paths downgrade the i_rwsem to shared, so extending writes can run concurrently and the i_size read by the caller may be stale by the time the folio is locked. Re-read i_size under the folio lock and clamp the zeroed range up to it, so a racing write that already put data into the folio isn't clobbered.
Wait for any writeback on the folio to finish before zeroing it so that the pagecache isn't modified while it may still be read by the transport during transmission. Honour IOCB_NOWAIT by returning -EAGAIN rather than blocking on the folio lock, on writeback, or in folio_mkclean()'s rmap walk when the folio is mapped.
truncate_pagecache() can't be used here: it must be called with the i_rwsem held exclusively, but these write paths only hold it shared, and it would block unconditionally, breaking IOCB_NOWAIT.
Callers that hold i_rwsem exclusively for the whole resize (truncate, setattr, fallocate, clone) exclude any genuine concurrent buffered writer, so staleness can instead be decided from the folio's dirty state, as pagecache_isize_extended() already does for filesystems that serialise writes against truncate/setattr via a single i_rwsem. Export netfs_clear_stale_post_isize() helper to handle such case.
The helper is required by the CIFS client to fix generic/363.
Documentation/filesystems/netfs_library.rst +26 -0
@@ -451,6 +451,32 @@ one. The inode should be marked ``NETFS_ICTX_SINGLE_NO_UPLOAD`` if this API is to be used. The writeback function requires the buffer to be of ITER_FOLIOQ type. +Clearing Stale Post-EOF Pagecache +--------------------------------- + +When a file is extended, data left in the pagecache past the old EOF by a write +through an mmap must not be exposed as file content. Netfslib clears this on +its own write paths, and exports a helper so a filesystem can do the same from +a resize path (truncate, setattr, fallocate and the like) that holds the +inode's ``i_rwsem`` exclusively across the whole resize:: + + void netfs_clear_stale_post_isize(struct inode *inode, uoff_t from, + uoff_t to); + +This zeroes any such data within the ``[from, to)`` hole to be made, where +@from is the old EOF and @to is the new one, and the caller must have already +updated ``i_size`` to @to before calling it. Only the folio straddling @from +can hold data written past the EOF through an mmap, as pages wholly beyond the +EOF can't be faulted in, so the zeroing is limited to that folio. The folio is +zeroed rather than dropped so that a concurrent extending write can't lose +data. + +This overlaps with ``pagecache_isize_extended()`` but can't reuse it: that +helper is keyed on a sub-page block size and is a no-op when the block size is +``>= PAGE_SIZE`` (as on network filesystems), and it doesn't wait for +writeback. As both address the same problem, a change to one should probably +be reflected in the other to keep them in sync. + High-Level VM API ==================
fs/netfs/buffered_write.c +9 -0
@@ -469,6 +469,7 @@ ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *fr struct netfs_group *netfs_group) {struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file);ssize_t ret;trace_netfs_write_iter (iocb, from);
@@ -481,6 +482,14 @@ ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *frif (ret)return ret; +if (iocb->ki_pos >i_size_read (inode)) { + ret =netfs_clear_stale_pre_isize (inode,i_size_read (inode), + iocb->ki_pos, + iocb->ki_flags &IOCB_NOWAIT ); +if (ret) +return ret; + } +return netfs_perform_write (iocb, from, netfs_group); }EXPORT_SYMBOL (netfs_buffered_write_iter_locked);
fs/netfs/direct_write.c +9 -0
@@ -359,6 +359,15 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from) ret =file_update_time (file);if (ret <0 ) goto out; + +if (iocb->ki_pos >i_size_read (inode)) { + ret =netfs_clear_stale_pre_isize (inode,i_size_read (inode), + iocb->ki_pos, + iocb->ki_flags &IOCB_NOWAIT ); +if (ret <0 ) + goto out; + } +if (iocb->ki_flags &IOCB_NOWAIT ) {/* We could block if there are any pages in the range. */ ret = -EAGAIN ;
fs/netfs/internal.h +2 -0
@@ -80,6 +80,8 @@ ssize_t netfs_wait_for_write(struct netfs_io_request *rreq);void netfs_wait_for_paused_read (struct netfs_io_request *rreq);void netfs_wait_for_paused_write (struct netfs_io_request *rreq);void netfs_wait_for_put_ra_refs (struct netfs_io_request *rreq); +int netfs_clear_stale_pre_isize (struct inode *inode,uoff_t from, +uoff_t to,bool nowait);/* * objects.c
fs/netfs/misc.c +99 -0
108 lines not on this page · raw message
include/linux/netfs.h +2 -0
@@ -397,6 +397,8 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);ssize_t netfs_unbuffered_write_iter_locked (struct kiocb *iocb,struct iov_iter *iter,struct netfs_group *netfs_group);ssize_t netfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from); +void netfs_clear_stale_post_isize (struct inode *inode,uoff_t from, +uoff_t to);/* Single, monolithic object read/write API. */ void netfs_single_mark_inode_dirty (struct inode *inode);
Sasha Levin
maintainerPATCH 1/1 netfs: clear post-EOF pagecache when extending a file via write
From: Paulo Alcantara <[email protected]>
[ Upstream commit 69bf14300b50631e7c2271289d347bbd1b19a06c ]
Fix netfs to erase the contents of a hole created after the EOF by an ordinary write if dirty data has been previously left there by writes through an mmapped region. Neither the buffered nor the unbuffered/DIO write path clears that stale pagecache.
Zero the tail of the folio straddling the EOF before an extending write. That is the only folio that can hold data written past the EOF through an mmap, as pages wholly beyond the EOF can't be faulted in. The folio is zeroed rather than dropped so a concurrent extending write can't lose data.
Both write paths downgrade the i_rwsem to shared, so extending writes can run concurrently and the i_size read by the caller may be stale by the time the folio is locked. Re-read i_size under the folio lock and clamp the zeroed range up to it, so a racing write that already put data into the folio isn't clobbered.
Wait for any writeback on the folio to finish before zeroing it so that the pagecache isn't modified while it may still be read by the transport during transmission. Honour IOCB_NOWAIT by returning -EAGAIN rather than blocking on the folio lock, on writeback, or in folio_mkclean()'s rmap walk when the folio is mapped.
truncate_pagecache() can't be used here: it must be called with the i_rwsem held exclusively, but these write paths only hold it shared, and it would block unconditionally, breaking IOCB_NOWAIT.
Callers that hold i_rwsem exclusively for the whole resize (truncate, setattr, fallocate, clone) exclude any genuine concurrent buffered writer, so staleness can instead be decided from the folio's dirty state, as pagecache_isize_extended() already does for filesystems that serialise writes against truncate/setattr via a single i_rwsem. Export netfs_clear_stale_post_isize() helper to handle such case.
The helper is required by the CIFS client to fix generic/363.
--- Documentation/filesystems/netfs_library.rst | 26 ++++++ fs/netfs/buffered_write.c | 9 ++ fs/netfs/direct_write.c | 9 ++ fs/netfs/internal.h | 2 + fs/netfs/misc.c | 99 +++++++++++++++++++++ include/linux/netfs.h | 2 + 6 files changed, 147 insertions(+)
Documentation/filesystems/netfs_library.rst +26 -0
@@ -587,6 +587,32 @@ there isn't a read request structure as well, such as writing dirty data to the cache. +Clearing Stale Post-EOF Pagecache +--------------------------------- + +When a file is extended, data left in the pagecache past the old EOF by a write +through an mmap must not be exposed as file content. Netfslib clears this on +its own write paths, and exports a helper so a filesystem can do the same from +a resize path (truncate, setattr, fallocate and the like) that holds the +inode's ``i_rwsem`` exclusively across the whole resize:: + + void netfs_clear_stale_post_isize(struct inode *inode, unsigned long long from, + unsigned long long to); + +This zeroes any such data within the ``[from, to)`` hole to be made, where +@from is the old EOF and @to is the new one, and the caller must have already +updated ``i_size`` to @to before calling it. Only the folio straddling @from +can hold data written past the EOF through an mmap, as pages wholly beyond the +EOF can't be faulted in, so the zeroing is limited to that folio. The folio is +zeroed rather than dropped so that a concurrent extending write can't lose +data. + +This overlaps with ``pagecache_isize_extended()`` but can't reuse it: that +helper is keyed on a sub-page block size and is a no-op when the block size is +``>= PAGE_SIZE`` (as on network filesystems), and it doesn't wait for +writeback. As both address the same problem, a change to one should probably +be reflected in the other to keep them in sync. + API Function Reference ======================
fs/netfs/buffered_write.c +9 -0
@@ -465,6 +465,7 @@ ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *fr struct netfs_group *netfs_group) {struct file *file = iocb->ki_filp; +struct inode *inode =file_inode (file);ssize_t ret;trace_netfs_write_iter (iocb, from);
@@ -477,6 +478,14 @@ ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *frif (ret)return ret; +if (iocb->ki_pos >i_size_read (inode)) { + ret =netfs_clear_stale_pre_isize (inode,i_size_read (inode), + iocb->ki_pos, + iocb->ki_flags &IOCB_NOWAIT ); +if (ret) +return ret; + } +return netfs_perform_write (iocb, from, netfs_group); }EXPORT_SYMBOL (netfs_buffered_write_iter_locked);
fs/netfs/direct_write.c +9 -0
@@ -172,6 +172,15 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from) ret =file_update_time (file);if (ret <0 ) goto out; + +if (iocb->ki_pos >i_size_read (inode)) { + ret =netfs_clear_stale_pre_isize (inode,i_size_read (inode), + iocb->ki_pos, + iocb->ki_flags &IOCB_NOWAIT ); +if (ret <0 ) + goto out; + } +if (iocb->ki_flags &IOCB_NOWAIT ) {/* We could block if there are any pages in the range. */ ret = -EAGAIN ;
fs/netfs/internal.h +2 -0
@@ -64,6 +64,8 @@ int netfs_buffer_append_folio(struct netfs_io_request *rreq, struct folio *foliostruct folio_queue *netfs_delete_buffer_head (struct netfs_io_request *wreq);void netfs_clear_buffer (struct netfs_io_request *rreq);void netfs_reset_iter (struct netfs_io_subrequest *subreq); +int netfs_clear_stale_pre_isize (struct inode *inode,unsigned long long from, +unsigned long long to,bool nowait);/* * objects.c
fs/netfs/misc.c +99 -0
108 lines not on this page · raw message
include/linux/netfs.h +2 -0
@@ -409,6 +409,8 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);ssize_t netfs_unbuffered_write_iter_locked (struct kiocb *iocb,struct iov_iter *iter,struct netfs_group *netfs_group);ssize_t netfs_file_write_iter (struct kiocb *iocb,struct iov_iter *from); +void netfs_clear_stale_post_isize (struct inode *inode,unsigned long long from, +unsigned long long to);/* Address operations API */ struct readahead_control ;
Signature · 1 line
2.53.0