[PATCH 2/3] mm: handle large folio when large folio in VM_LOCKED VMA range

From: Yin Fengwei
Date: Fri Jul 28 2023 - 03:12:09 EST


If large folio is in the range of VM_LOCKED VMA, it should be
mlocked to avoid being picked by page reclaim. Which may split
the large folio and then mlock each pages again.

Mlock this kind of large folio to prevent them being picked by
page reclaim.

For the large folio which cross the boundary of VM_LOCKED VMA,
we'd better not to mlock it. So if the system is under memory
pressure, this kind of large folio will be split and the pages
ouf of VM_LOCKED VMA can be reclaimed.

for page_add_anon_rmap() and page_add_file_rmap(), we only mlock
the folio if it's not large folio. The reason to do so is that
these functions can be called couple of times for a large folio
and each call just covered piece of large folio. If folio is
mlocked multiple time, the folio->mlock_count can be imbalance.
Delay the folio mlock to page reclaim phase. As only mlock folio
once for sure in page reclaim phase.

Signed-off-by: Yin Fengwei <fengwei.yin@xxxxxxxxx>
---
mm/internal.h | 18 +++++++++---------
mm/rmap.c | 27 ++++++++++++++++++++-------
2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index 63de32154a48..6c6fb1f3e4c1 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -662,14 +662,10 @@ folio_within_vma(struct folio *folio, struct vm_area_struct *vma, pte_t *pte)
* mlock is usually called at the end of page_add_*_rmap(), munlock at
* the end of page_remove_rmap(); but new anon folios are managed by
* folio_add_lru_vma() calling mlock_new_folio().
- *
- * @compound is used to include pmd mappings of THPs, but filter out
- * pte mappings of THPs, which cannot be consistently counted: a pte
- * mapping of the THP head cannot be distinguished by the page alone.
*/
void mlock_folio(struct folio *folio);
static inline void mlock_vma_folio(struct folio *folio,
- struct vm_area_struct *vma, bool compound)
+ struct vm_area_struct *vma, pte_t *pte)
{
/*
* The VM_SPECIAL check here serves two purposes.
@@ -680,16 +676,20 @@ static inline void mlock_vma_folio(struct folio *folio,
* still be set while VM_SPECIAL bits are added: so ignore it then.
*/
if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED) &&
- (compound || !folio_test_large(folio)))
+ folio_within_vma(folio, vma, pte))
mlock_folio(folio);
}

void munlock_folio(struct folio *folio);
static inline void munlock_vma_folio(struct folio *folio,
- struct vm_area_struct *vma, bool compound)
+ struct vm_area_struct *vma)
{
- if (unlikely(vma->vm_flags & VM_LOCKED) &&
- (compound || !folio_test_large(folio)))
+ /*
+ * To handle the case that a mlocked large folio is unmapped from VMA
+ * piece by piece, allow munlock the large folio which is partially
+ * mapped to VMA.
+ */
+ if (unlikely(vma->vm_flags & VM_LOCKED))
munlock_folio(folio);
}

diff --git a/mm/rmap.c b/mm/rmap.c
index 54124f18e0e4..1d8f048fbed8 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -798,6 +798,7 @@ struct folio_referenced_arg {
unsigned long vm_flags;
struct mem_cgroup *memcg;
};
+
/*
* arg: folio_referenced_arg will be passed
*/
@@ -811,10 +812,22 @@ static bool folio_referenced_one(struct folio *folio,
while (page_vma_mapped_walk(&pvmw)) {
address = pvmw.address;

- if ((vma->vm_flags & VM_LOCKED) &&
- (!folio_test_large(folio) || !pvmw.pte)) {
+ if (vma->vm_flags & VM_LOCKED) {
+ if (!folio_within_vma(folio, vma, pvmw.pte)) {
+ /*
+ * For large folio cross VMA boundaries, it's
+ * expected to be picked by page reclaim. But
+ * should skip reference of pages which are in
+ * the range of VM_LOCKED vma. As page reclaim
+ * should just count the reference of pages out
+ * the range of VM_LOCKED vma.
+ */
+ pra->mapcount--;
+ continue;
+ }
+
/* Restore the mlock which got missed */
- mlock_vma_folio(folio, vma, !pvmw.pte);
+ mlock_vma_folio(folio, vma, pvmw.pte);
page_vma_mapped_walk_done(&pvmw);
pra->vm_flags |= VM_LOCKED;
return false; /* To break the loop */
@@ -1253,7 +1266,7 @@ void page_add_anon_rmap(struct page *page, struct vm_area_struct *vma,
__page_check_anon_rmap(folio, page, vma, address);
}

- mlock_vma_folio(folio, vma, compound);
+ mlock_vma_folio(folio, vma, NULL);
}

/**
@@ -1344,7 +1357,7 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,
if (nr)
__lruvec_stat_mod_folio(folio, NR_FILE_MAPPED, nr);

- mlock_vma_folio(folio, vma, compound);
+ mlock_vma_folio(folio, vma, NULL);
}

/**
@@ -1383,7 +1396,7 @@ static void __remove_rmap_finish(struct folio *folio,
* it's only reliable while mapped.
*/

- munlock_vma_folio(folio, vma, compound);
+ munlock_vma_folio(folio, vma);
}

/**
@@ -1557,7 +1570,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
if (!(flags & TTU_IGNORE_MLOCK) &&
(vma->vm_flags & VM_LOCKED)) {
/* Restore the mlock which got missed */
- mlock_vma_folio(folio, vma, false);
+ mlock_vma_folio(folio, vma, pvmw.pte);
page_vma_mapped_walk_done(&pvmw);
ret = false;
break;
--
2.39.2