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

From: Jan Kara

Date: Mon Nov 09 2026 - 07:20:37 EST


On Wed 04-11-26 18:27:00, 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.
>
> Set unmap_end to zero for these small finite ranges so that the existing
> unsigned comparison skips the empty interval. Explicitly handle lend ==
> -1 and lend == LLONG_MAX with LLONG_MAX, preserving EOF handling and
> avoiding wrapping in the endpoint calculation at the largest inclusive
> end.
>
> Suggested-by: Jan Kara <jack@xxxxxxx>
> Fixes: 5a7203947a1d ("mm: teach truncate_inode_pages_range() to handle non page aligned ranges")
> Link: https://lore.kernel.org/r/20261027211505.22044-1-dennylin0707@xxxxxxxxx
> Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>

Nice! Feel free to add:

Reviewed-by: Jan Kara <jack@xxxxxxx>

Honza

> ---
> Changes in v2:
> - Compute unmap_end directly as suggested by Jan Kara, keeping the
> original condition for calling unmap_mapping_range().
> - Handle the EOF sentinel and LLONG_MAX explicitly.
>
> mm/truncate.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/mm/truncate.c b/mm/truncate.c
> index 733d2713a87..2c1f4a6c336 100644
> --- a/mm/truncate.c
> +++ b/mm/truncate.c
> @@ -945,7 +945,15 @@ void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
> {
> struct address_space *mapping = inode->i_mapping;
> loff_t unmap_start = round_up(lstart, PAGE_SIZE);
> - loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
> + loff_t unmap_end;
> +
> + 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;
> +
> /*
> * This rounding is currently just for example: unmap_mapping_range
> * expands its hole outwards, whereas we want it to contract the hole
>
> base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
> --
> 2.50.1 (Apple Git-155)
>
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR