[PATCH v4 07/17] mm/huge_memory: move EOF trimming into the file split helper
From: Kairui Song via B4 Relay
Date: Mon Sep 07 2026 - 14:17:09 EST
From: Kairui Song <kasong@xxxxxxxxxxx>
Instead of receiving @end and @nr_shmem_dropped from the caller, the
file split helper now computes the EOF boundary and trims pages beyond
it itself, as this is only needed for file split. This drops the
redundant parameter passing and sanity check.
Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Reviewed-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
Reviewed-by: Yeoreum Yun <yeoreum.yun@xxxxxxx>
Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
---
mm/huge_memory.c | 41 +++++++++++++++++++----------------------
1 file changed, 19 insertions(+), 22 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 4ddef481cfdc..47ccf2326cfe 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4064,16 +4064,29 @@ static int __folio_freeze_split_file(struct folio *folio,
unsigned int new_order, struct page *split_at,
struct xa_state *xas, struct address_space *mapping,
bool do_lru, struct list_head *list,
- enum split_type split_type, pgoff_t end, int *nr_shmem_dropped)
+ enum split_type split_type)
{
struct folio *end_folio = folio_next(folio);
struct folio *new_folio, *next;
+ int nr_shmem_dropped = 0;
struct lruvec *lruvec;
+ pgoff_t end;
int ret;
/* Currently device private folios can only back anonymous memory. */
VM_WARN_ON_ONCE_FOLIO(folio_is_device_private(folio), folio);
+ /*
+ * The loop below may need to trim off pages beyond
+ * EOF: but on 32-bit, i_size_read() takes an irq-unsafe
+ * seqlock, which cannot be nested inside the page tree lock.
+ * So note end now: i_size itself may be changed at any moment,
+ * but folio lock is good enough to serialize the trimming.
+ */
+ end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
+ if (shmem_mapping(mapping))
+ end = shmem_fallocend(mapping->host, end);
+
xas_lock_irq(xas);
/*
@@ -4138,10 +4151,9 @@ static int __folio_freeze_split_file(struct folio *folio,
continue;
}
- VM_WARN_ON_ONCE(!nr_shmem_dropped);
/* Drop folio beyond EOF: ->index >= end */
- if (shmem_mapping(mapping) && nr_shmem_dropped)
- *nr_shmem_dropped += nr_pages;
+ if (shmem_mapping(mapping))
+ nr_shmem_dropped += nr_pages;
else if (folio_test_clear_dirty(new_folio))
folio_account_cleaned(new_folio,
inode_to_wb(mapping->host));
@@ -4162,6 +4174,8 @@ static int __folio_freeze_split_file(struct folio *folio,
fail:
xas_unlock_irq(xas);
+ if (nr_shmem_dropped)
+ shmem_uncharge(mapping->host, nr_shmem_dropped);
return ret;
}
@@ -4198,9 +4212,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
struct anon_vma *anon_vma = NULL;
int old_order = folio_order(folio);
struct folio *new_folio, *next;
- int nr_shmem_dropped = 0;
enum ttu_flags ttu_flags = 0;
- pgoff_t end = 0;
int ret;
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
@@ -4277,17 +4289,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
anon_vma = NULL;
i_mmap_lock_read(mapping);
-
- /*
- * __split_frozen_folio() may need to trim off pages beyond
- * EOF: but on 32-bit, i_size_read() takes an irq-unsafe
- * seqlock, which cannot be nested inside the page tree lock.
- * So note end now: i_size itself may be changed at any moment,
- * but folio lock is good enough to serialize the trimming.
- */
- end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
- if (shmem_mapping(mapping))
- end = shmem_fallocend(mapping->host, end);
}
/*
@@ -4306,11 +4307,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,
true, list, split_type);
else
ret = __folio_freeze_split_file(folio, new_order, split_at, &xas, mapping,
- true, list, split_type, end,
- &nr_shmem_dropped);
-
- if (nr_shmem_dropped)
- shmem_uncharge(mapping->host, nr_shmem_dropped);
+ true, list, split_type);
if (!ret && is_anon && !folio_is_device_private(folio))
ttu_flags = TTU_USE_SHARED_ZEROPAGE;
--
2.55.0