7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yuezhang Mo [ Upstream commit 1043264c3cfa43b0668f7472a749868c1e21749b ] When extending a file across a non-page-aligned EOF, the folio containing the old EOF must be marked RO so that subsequent mmap writes trigger ->page_mkwrite() and update valid_size. The straddling folio remains mapped to the same filesystem block after the file is extended. As a result, the existing writable PTE may remain in place, allowing mmap writes to bypass ->page_mkwrite() and leave valid_size unchanged. Furthermore, writes to the post-EOF range may occur between truncate_pagecache() and locking the folio. So the post-EOF range should be zeroed after marking the straddling folio RO, and using truncate_pagecache() to zero out the post-EOF range is redundant. Introduce exfat_pagecache_isize_extended() to mark the straddling folio RO and zero the post-EOF range after clearing writable mapping. This ensures that data written beyond the old EOF before the extension is not exposed as valid file data, and that subsequent mmap writes trigger ->page_mkwrite() to update valid_size. Fixes: 82a81a7352bc ("exfat: add iomap buffered I/O support") Cc: stable@vger.kernel.org Signed-off-by: Yuezhang Mo Signed-off-by: Namjae Jeon Signed-off-by: Sasha Levin --- fs/exfat/file.c | 72 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/fs/exfat/file.c b/fs/exfat/file.c index d24f0650bfc88..f106db945d8dd 100644 --- a/fs/exfat/file.c +++ b/fs/exfat/file.c @@ -17,11 +17,70 @@ #include #include #include +#include #include "exfat_raw.h" #include "exfat_fs.h" #include "iomap.h" +/* + * Unlike pagecache_isize_extended(), this function updates i_size while + * holding the straddling folio lock, and does not skip the straddling + * folio when the filesystem block size is >= PAGE_SIZE or when 'from' + * and 'to' fall within the same filesystem block. + * + * This ensures that the folio is marked RO and the post-EOF range is + * zeroed before readers or writeback can observe the updated i_size. + */ +static void exfat_pagecache_isize_extended(struct inode *inode, loff_t from, + loff_t to) +{ + struct folio *folio; + + if (!(from & (PAGE_SIZE - 1))) { + i_size_write(inode, to); + return; + } + + folio = filemap_lock_folio(inode->i_mapping, from >> PAGE_SHIFT); + + /* Folio not cached? Nothing to do */ + if (IS_ERR(folio)) { + i_size_write(inode, to); + return; + } + + /* + * See folio_clear_dirty_for_io() for details why folio_mark_dirty() + * is needed. + */ + if (folio_mkclean(folio)) + folio_mark_dirty(folio); + + /* + * Zero the post-EOF range before publishing the new size. Writeback + * normally does this, but the extension would expose the tail to + * readers before writeback gets a chance to zero it. + */ + if (folio_test_dirty(folio)) { + loff_t offset, end; + + offset = from - folio_pos(folio); + end = min_t(loff_t, to - folio_pos(folio), + folio_size(folio)); + folio_zero_segment(folio, offset, end); + } + + /* + * Publish the new size after zeroing the tail. Uptodate folios can + * be read without taking the folio lock. + */ + i_size_write(inode, to); + + folio_unlock(folio); + folio_put(folio); +} + static int exfat_cont_expand(struct inode *inode, loff_t size) { int ret; @@ -32,8 +91,6 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) struct exfat_chain clu; loff_t oldsize = i_size_read(inode); - truncate_pagecache(inode, oldsize); - ret = inode_newsize_ok(inode, size); if (ret) return ret; @@ -81,15 +138,12 @@ static int exfat_cont_expand(struct inode *inode, loff_t size) out: inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); - /* Expanded range not zeroed, do not update valid_size */ - i_size_write(inode, size); /* - * When extending file size, call truncate_pagecache() first, - * then update i_size, and call pagecache_isize_extended() - * to ensures the straddling folio is properly marked RO so - * page_mkwrite() is called and post-EOF area is zeroed. + * When extending file size, call exfat_pagecache_isize_extended() + * to update i_size and ensure the straddling folio is properly + * marked RO so page_mkwrite() is called and post-EOF area is zeroed. */ - pagecache_isize_extended(inode, oldsize, inode->i_size); + exfat_pagecache_isize_extended(inode, oldsize, size); inode->i_blocks = round_up(size, sbi->cluster_size) >> 9; mark_inode_dirty(inode); -- 2.53.0