[PATCH v2 04/10] mm/madvise: factor the cold and pageout folio split

From: Gregory Price (Meta)

Date: Wed Oct 14 2026 - 14:32:12 EST


Both the huge-PMD and PTE paths of madvise_lru_pmd_entry() split a folio
they hold locked and referenced, then drop the lock and the reference,
with the same three calls open-coded at each site.

Move that sequence into madvise_lru_split_folio(), which returns the
result of split_folio().

No functional change intended.

Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
mm/madvise.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index d3edc056ef57..1bb4f9f0e73a 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -397,6 +397,16 @@ static bool madvise_lru_folio_is_filtered(struct folio *folio,
(pageout_anon_only && !folio_test_anon(folio));
}

+/* Split a locked and referenced folio, then drop the lock and reference. */
+static int madvise_lru_split_folio(struct folio *folio)
+{
+ int err = split_folio(folio);
+
+ folio_unlock(folio);
+ folio_put(folio);
+ return err;
+}
+
static int madvise_lru_pmd_entry(pmd_t *pmd, unsigned long addr,
unsigned long end, struct mm_walk *walk)
{
@@ -445,16 +455,11 @@ static int madvise_lru_pmd_entry(pmd_t *pmd, unsigned long addr,
goto huge_unlock;

if (next - addr != HPAGE_PMD_SIZE) {
- int err;
-
if (!folio_trylock(folio))
goto huge_unlock;
folio_get(folio);
spin_unlock(ptl);
- err = split_folio(folio);
- folio_unlock(folio);
- folio_put(folio);
- if (!err)
+ if (!madvise_lru_split_folio(folio))
goto regular_folio;
return 0;
}
@@ -526,9 +531,7 @@ static int madvise_lru_pmd_entry(pmd_t *pmd, unsigned long addr,
lazy_mmu_mode_disable();
pte_unmap_unlock(start_pte, ptl);
start_pte = NULL;
- err = split_folio(folio);
- folio_unlock(folio);
- folio_put(folio);
+ err = madvise_lru_split_folio(folio);
start_pte = pte =
pte_offset_map_lock(mm, pmd, addr, &ptl);
if (!start_pte)
--
2.56.0