When a hole ends before the first page boundary, shmem_fallocate() calculates unmap_end as -1. Casting it to u64 makes the range check succeed even though the hole contains no complete pages. For example, punching one byte at offset zero calls unmap_mapping_range() with a zero length, which means unmap to the end of the file. The file contents are preserved outside the hole, but unrelated mappings are removed and must fault back in. This can be reproduced with fallocate -p -o 0 -l 1 on a mapped tmpfs file. Keep unmap_end as the exclusive rounded end instead of subtracting one, and adjust shmem_falloc.next and the unmap length accordingly. A hole ending before the first page boundary then has an end of zero, so the unsigned range check skips the empty range. Retain the unsigned comparison so rounding the start near the maximum file offset continues to be handled correctly. Fixes: 83e4fa9c16e4 ("tmpfs: support fallocate FALLOC_FL_PUNCH_HOLE") Reviewed-by: Baolin Wang Signed-off-by: Hungyu Lin --- Changes in v2: - Use an exclusive unmap_end throughout, following David's suggestion. - Add Baolin's Reviewed-by. v1: https://lore.kernel.org/linux-mm/20261027171753.1485-1-dennylin0707@gmail.com/ mm/shmem.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index af21a89abb0f..ce92e74baeb7 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3845,7 +3845,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, if (mode & FALLOC_FL_PUNCH_HOLE) { struct address_space *mapping = file->f_mapping; loff_t unmap_start = round_up(offset, PAGE_SIZE); - loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; + loff_t unmap_end = round_down(offset + len, PAGE_SIZE); DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); /* protected by i_rwsem */ @@ -3856,14 +3856,14 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, shmem_falloc.waitq = &shmem_falloc_waitq; shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; - shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; + shmem_falloc.next = unmap_end >> PAGE_SHIFT; spin_lock(&inode->i_lock); WRITE_ONCE(inode->i_private, &shmem_falloc); spin_unlock(&inode->i_lock); if ((u64)unmap_end > (u64)unmap_start) unmap_mapping_range(mapping, unmap_start, - 1 + unmap_end - unmap_start, 0); + unmap_end - unmap_start, 0); shmem_truncate_range(inode, offset, offset + len - 1); /* No need to unmap again: hole-punching leaves COWed pages */ base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 -- 2.43.0