Skip to contentSkip to conversation

mm: skip empty finite ranges in truncate_pagecache_range

PATCH Superseded Hungyu Lin 2 participants 4 messages Oct 27 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 mm: skip empty finite ranges in truncate_pagecache_range

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")
---
 mm/truncate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/truncate.c +1 -1
index b58ba940be47..43c6cf99027f 100644
@@ -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);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 9 lines · Hungyu Lin

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.

Quoted 7 lines

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")

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

Quoted 20 lines
---
 mm/truncate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/truncate.c +1 -1
index b58ba940be47..43c6cf99027f 100644
@@ -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);
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#2 lore raw

PATCH v2 1/1 mm: skip empty finite ranges in truncate_pagecache_range

In reply to Hungyu Lin

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.

Fixes 5a7203947a1d ("mm: teach truncate_inode_pages_range() to handle non page aligned ranges")

---
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(-)
mm/truncate.c +9 -1
index 733d2713a87..2c1f4a6c336 100644
@@ -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

Signature · 1 line
2.50.1 (Apple Git-155)
maintainer Reviewed-by on v2 1/1
#3 lore raw
Quoted 15 lines · Hungyu Lin

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.

Quoted 4 lines
Fixes 5a7203947a1d ("mm: teach truncate_inode_pages_range() to handle non page aligned ranges")

Nice! Feel free to add:

Honza

Quoted 35 lines

---
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(-)
mm/truncate.c +9 -1
index 733d2713a87..2c1f4a6c336 100644
@@ -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

Signature · 1 line
2.50.1 (Apple Git-155)
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR