Re: [PATCH v2] shmem: skip empty unmap ranges when punching holes
From: Baolin Wang
Date: Thu Nov 05 2026 - 04:37:25 EST
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@xxxxxxxxxxxxxxxxx>
Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
---
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.