Re: [PATCH] mm: skip empty finite ranges in truncate_pagecache_range

From: Jan Kara

Date: Wed Nov 04 2026 - 06:58:12 EST


On Tue 27-10-26 21:15:05, Hungyu Lin wrote:
> For a finite range ending before the first page boundary,
> truncate_pagecache_range() calculates unmap_end as -1. The unsigned
> comparison then treats the empty full-page interval as nonempty.
>
> For example, lstart == 0 and lend == 0 cause unmap_mapping_range() to be
> called with a zero length, meaning unmap to EOF. Punching one byte at
> offset zero in a mapped FUSE file can therefore remove mappings outside
> the hole, causing unnecessary page-table work and subsequent faults.
>
> Compare the exclusive rounded end against the rounded start, while
> explicitly preserving lend == -1 as the EOF sentinel. Perform the
> addition after converting to u64 so the largest inclusive end does not
> introduce a signed overflow in the comparison.
>
> Fixes: 5a7203947a1d ("mm: teach truncate_inode_pages_range() to handle non page aligned ranges")
> Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>

Hum, so I agree about the problem but the rounding logic in
truncate_pagecache_range() has more flaws AFAICS. For example if lend ==
LLONG_MAX, then unmap_end with overflow to huge negative value. So maybe
let's fix it all at once. Maybe something like:

if (lend == -1 || lend == LLONG_MAX)
unmap_end = LLONG_MAX;
else if (lend < PAGE_SIZE - 1)
unmap_end = 0;
else
unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;

And then you can leave the condition checking whether to call
unmap_mapping_range() untouched.

Honza

> ---
> mm/truncate.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/truncate.c b/mm/truncate.c
> index b58ba940be47..43c6cf99027f 100644
> --- a/mm/truncate.c
> +++ b/mm/truncate.c
> @@ -919,7 +919,7 @@ void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
> * once (before truncating pagecache), and without "even_cows" flag:
> * hole-punching should not remove private COWed pages from the hole.
> */
> - if ((u64)unmap_end > (u64)unmap_start)
> + if (lend == -1 || (u64)unmap_end + 1 > (u64)unmap_start)
> unmap_mapping_range(mapping, unmap_start,
> 1 + unmap_end - unmap_start, 0);
> truncate_inode_pages_range(mapping, lstart, lend);
> --
> 2.43.0
>
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR