[RFC PATCH v3 2/4] mm: improve large folio reuse for LRU-cached folios
From: Barry Song (Xiaomi)
Date: Tue Aug 18 2026 - 19:00:21 EST
Large folios may now reside in the per-CPU LRU cache. Before
attempting to reuse them, drain the local LRU cache, which
can still be beneficial in cases where the folios are likely
to remain in this CPU's LRU cache:
int main(int argc, char *argv[])
{
int i;
while (1) {
volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
for (int i = 0; i < SIZE / sizeof(int); i++)
p[i] = i;
madvise((void *)p, SIZE, MADV_PAGEOUT);
if (!fork())
_exit(0);
for (int i = 0; i < SIZE / sizeof(int); i++)
p[i] = i;
munmap((void *)p, SIZE);
}
return 0;
}
Signed-off-by: Barry Song (Xiaomi) <baohua@xxxxxxxxxx>
---
mm/memory.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/mm/memory.c b/mm/memory.c
index 4134ac607ee0..efdf82b3c418 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4275,6 +4275,12 @@ static bool __wp_can_reuse_large_anon_folio(struct folio *folio,
folio_unlock(folio);
}
+ if (folio_may_be_lru_cached(folio) && !folio_test_lru(folio)) {
+ if (folio_ref_count(folio) != folio_large_mapcount(folio) + 1)
+ return false;
+ lru_add_drain();
+ }
+
if (folio_large_mapcount(folio) != folio_ref_count(folio))
return false;
--
2.34.1