* [PATCH v2] shmem: skip empty unmap ranges when punching holes
@ 2026-11-05 5:56 Hungyu Lin
2026-11-05 9:32 ` Baolin Wang
0 siblings, 1 reply; 2+ messages in thread
From: Hungyu Lin @ 2026-11-05 5:56 UTC (permalink / raw)
To: hughd; +Cc: david, baolin.wang, akpm, linux-mm, linux-kernel
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 <baolin.wang@linux.alibaba.com>
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v2] shmem: skip empty unmap ranges when punching holes
2026-11-05 5:56 [PATCH v2] shmem: skip empty unmap ranges when punching holes Hungyu Lin
@ 2026-11-05 9:32 ` Baolin Wang
0 siblings, 0 replies; 2+ messages in thread
From: Baolin Wang @ 2026-11-05 9:32 UTC (permalink / raw)
To: Hungyu Lin, hughd; +Cc: david, akpm, linux-mm, linux-kernel
On 11/5/26 1:56 PM, Hungyu Lin wrote:
> 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 <baolin.wang@linux.alibaba.com>
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
> ---
> Changes in v2:
> - Use an exclusive unmap_end throughout, following David's suggestion.
> - Add Baolin's Reviewed-by.
Thanks. This still looks good to me.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-05 9:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05 5:56 [PATCH v2] shmem: skip empty unmap ranges when punching holes Hungyu Lin
2026-11-05 9:32 ` Baolin Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®