[PATCH] mm: huge_memory: add folio_mark_accessed() when zapping file THP
From: Baolin Wang
Date: Wed Apr 09 2025 - 05:39:24 EST
When investigating performance issues during file folio unmap, I noticed some
behavioral differences in handling non-PMD-sized folios and PMD-sized folios.
For non-PMD-sized file folios, it will call folio_mark_accessed() to mark the
folio as having seen activity, but this is not done for PMD-sized folios.
This might not cause obvious issues, but a potential problem could be that,
it might lead to reclaim hot file folios under memory pressure, as quoted
from Johannes:
"
Sometimes file contents are only accessed through relatively short-lived
mappings. But they can nevertheless be accessed a lot and be hot. It's
important to not lose that information on unmap, and end up kicking out a
frequently used cache page.
"
Therefore, we should also add folio_mark_accessed() for PMD-sized file
folios when unmapping.
Signed-off-by: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Acked-by: Johannes Weiner <hannes@xxxxxxxxxxx>
Acked-by: Zi Yan <ziy@xxxxxxxxxx>
---
Changes from RFC:
- Update the commit message, per Johannes.
- Collect Acked tags from Johannes and Zi. Thanks.
---
mm/huge_memory.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 2a47682d1ab7..955781b4e946 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2260,6 +2260,10 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
zap_deposited_table(tlb->mm, pmd);
add_mm_counter(tlb->mm, mm_counter_file(folio),
-HPAGE_PMD_NR);
+
+ if (flush_needed && pmd_young(orig_pmd) &&
+ likely(vma_has_recency(vma)))
+ folio_mark_accessed(folio);
}
spin_unlock(ptl);
--
2.43.5