[PATCH RFC 06/13] mm/huge_memory: move EOF trimming into the file split helper

From: Kairui Song via B4 Relay

Date: Fri Aug 07 2026 - 17:20:08 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.

Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx>
---
mm/huge_memory.c | 42 +++++++++++++++++++-----------------------
1 file changed, 19 insertions(+), 23 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index ca9430a6f8d1..72f5d0d24127 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -4031,14 +4031,26 @@ static int __folio_freeze_split_unmapped_anon(struct folio *folio, unsigned int
static int __folio_freeze_split_unmapped_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)
+ struct list_head *list, 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 = 0;
int ret;

+ /*
+ *__split_unmapped_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);
+
xas_lock_irq(xas);

/*
@@ -4102,10 +4114,9 @@ static int __folio_freeze_split_unmapped_file(struct folio *folio, unsigned int
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));
@@ -4126,6 +4137,8 @@ static int __folio_freeze_split_unmapped_file(struct folio *folio, unsigned int

fail:
xas_unlock_irq(xas);
+ if (nr_shmem_dropped)
+ shmem_uncharge(mapping->host, nr_shmem_dropped);
return ret;
}

@@ -4162,9 +4175,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);
@@ -4241,17 +4252,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order,

anon_vma = NULL;
i_mmap_lock_read(mapping);
-
- /*
- *__split_unmapped_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);
}

/*
@@ -4267,16 +4267,12 @@ static int __folio_split(struct folio *folio, unsigned int new_order,

if (!is_anon) {
ret = __folio_freeze_split_unmapped_file(folio, new_order, split_at, &xas, mapping,
- true, list, split_type, end,
- &nr_shmem_dropped);
+ true, list, split_type);
} else {
ret = __folio_freeze_split_unmapped_anon(folio, new_order, split_at, true,
list, split_type);
}

- if (nr_shmem_dropped)
- shmem_uncharge(mapping->host, nr_shmem_dropped);
-
if (!ret && is_anon && !folio_is_device_private(folio))
ttu_flags = TTU_USE_SHARED_ZEROPAGE;


--
2.55.0