[PATCH] mm:vmscan: shrink skip folios in the exiting task

From: Zhiguo Jiang
Date: Wed Jan 24 2024 - 07:43:33 EST


If the shrinking folio is belong to the exiting task, this folio should
be freed in the task exit flow rather than being reclaimed in the shrink
flow, because the former takes less time.

If the folio which is belong to the exiting task is reclaimed in the
shrink flow, such as the anon folio, the anon folio needs to be first
written to the swap partition by swap-in in shrink flow, and then the
corresponding swap folio needs to be released in the task exiting flow.
As is well known, releasing a swap folio will task more time than
releasing directly an anon folio.

In the scenarios of the low memory system and mutil backed-applications,
the time-consuming problem caused by shrinking the exiting task's folios
will be more severe.

Signed-off-by: Zhiguo Jiang <justinjiang@xxxxxxxx>
---
include/linux/mm.h | 1 +
mm/rmap.c | 12 ++++++++++++
mm/vmscan.c | 4 ++++
3 files changed, 17 insertions(+)
mode change 100644 => 100755 include/linux/mm.h
mode change 100644 => 100755 mm/rmap.c

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 42652bc8ceca..67e84b7d1928
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -312,6 +312,7 @@ extern unsigned int kobjsize(const void *objp);
#define VM_HUGEPAGE 0x20000000 /* MADV_HUGEPAGE marked this vma */
#define VM_NOHUGEPAGE 0x40000000 /* MADV_NOHUGEPAGE marked this vma */
#define VM_MERGEABLE 0x80000000 /* KSM may merge identical pages */
+#define VM_EXITING 0x100000000 /* The task which this vma belongs to is exiting */

#ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS
#define VM_HIGH_ARCH_BIT_0 32 /* bit only usable on 64-bit architectures */
diff --git a/mm/rmap.c b/mm/rmap.c
index 1cf2bffa48ed..deb419fd095b
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -943,6 +943,18 @@ static bool invalid_folio_referenced_vma(struct vm_area_struct *vma, void *arg)
if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
return true;

+ /*
+ * Skip counting references of the folios in the exiting task, and
+ * these folios will be freeed in the task exit flow rather than being
+ * reclaimed in shrink flow, which is shorter time consumption.
+ */
+ if (likely(vma->vm_mm) &&
+ unlikely(!atomic_read(&vma->vm_mm->mm_users) ||
+ test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags))) {
+ pra->vm_flags |= VM_EXITING;
+ return true;
+ }
+
return false;
}

diff --git a/mm/vmscan.c b/mm/vmscan.c
index b3ed3c9c3e3d..40ea4128044c 100755
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -887,6 +887,10 @@ static enum folio_references folio_check_references(struct folio *folio,
return FOLIOREF_KEEP;
}

+ /* The folio belongs to the exiting task: rotate */
+ if (vm_flags & VM_EXITING)
+ return FOLIOREF_KEEP;
+
/* Reclaim if clean, defer dirty folios to writeback */
if (referenced_folio && folio_is_file_lru(folio))
return FOLIOREF_RECLAIM_CLEAN;
--
2.39.0