Skip to contentSkip to conversation

promote mapped executable folios after first usage for MGLRU

PATCH Superseded Baolin Wang 2 participants 6 messages Jul 16

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Baolin Wang

author reviewer
#0 lore raw

v2 cover promote mapped executable folios after first usage for MGLRU

Now MGLRU's protection of mapped executable file folios is less reliable. Follow the classical LRU's logic, promoting mapped executable file folios after their first usage to give executable code a better chance to stay in memory and improve workload performance (See patch 2 for more details).

Changes from v1:
- Add a new patch to clean up vma flags as preparation.
- Add a new helper to check exec file folios (per Kaisui).
- Promote exec file folios in lru_gen_set_refs (Per Sashiko).

Baolin Wang (2):
mm: vmscan: convert folio_referenced() to use vma_flags_t
mm: mglru: promote mapped executable folios after first usage

 include/linux/mm_inline.h |  6 +++++
 include/linux/rmap.h      |  7 +++---
 mm/rmap.c                 | 19 +++++++++------
 mm/vmscan.c               | 51 +++++++++++++++++++++++----------------
 4 files changed, 50 insertions(+), 33 deletions(-)
Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH v2 1/2 mm: vmscan: convert folio_referenced() to use vma_flags_t

Replace use of the legacy vm_flags_t flags with vma_flags_t values for folio_referenced() and related logic. This is also a preparation for the following changes.

No functional changes.

---
 include/linux/rmap.h |  7 +++----
 mm/rmap.c            | 19 +++++++++++--------
 mm/vmscan.c          | 14 +++++++-------
 3 files changed, 21 insertions(+), 19 deletions(-)
include/linux/rmap.h +3 -4
index 8dc0871e5f00..e090aa09825d 100644
@@ -843,7 +843,7 @@ static inline int folio_try_share_anon_rmap_pmd(struct folio *folio,
  * Called from mm/vmscan.c to handle paging out
  */
 int folio_referenced(struct folio *, int is_locked,
-			struct mem_cgroup *memcg, vm_flags_t *vm_flags);
+		struct mem_cgroup *memcg, vma_flags_t *vma_flags);
 
 void try_to_migrate(struct folio *folio, enum ttu_flags flags);
 void try_to_unmap(struct folio *, enum ttu_flags flags);
@@ -975,10 +975,9 @@ struct anon_vma *folio_lock_anon_vma_read(const struct folio *folio,
 #define anon_vma_prepare(vma)	(0)
 
 static inline int folio_referenced(struct folio *folio, int is_locked,
-				  struct mem_cgroup *memcg,
-				  vm_flags_t *vm_flags)
+		struct mem_cgroup *memcg, vma_flags_t *vma_flags)
 {
-	*vm_flags = 0;
+	vma_flags_clear_all(vma_flags);
 	return 0;
 }
 
mm/rmap.c +11 -8
index e854679553b9..4085540b6a25 100644
@@ -907,7 +907,7 @@ pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address)
 struct folio_referenced_arg {
 	int mapcount;
 	int referenced;
-	vm_flags_t vm_flags;
+	vma_flags_t vma_flags;
 	struct mem_cgroup *memcg;
 };
 
@@ -926,7 +926,7 @@ static bool folio_referenced_one(struct folio *folio,
 		address = pvmw.address;
 		nr = 1;
 
-		if (vma->vm_flags & VM_LOCKED) {
+		if (vma_test(vma, VMA_LOCKED_BIT)) {
 			ptes++;
 			pra->mapcount--;
 
@@ -947,7 +947,7 @@ static bool folio_referenced_one(struct folio *folio,
 			/* Restore the mlock which got missed */
 			mlock_vma_folio(folio, vma);
 			page_vma_mapped_walk_done(&pvmw);
-			pra->vm_flags |= VM_LOCKED;
+			vma_flags_set(&pra->vma_flags, VMA_LOCKED_BIT);
 			return false; /* To break the loop */
 		}
 
@@ -1015,8 +1015,11 @@ static bool folio_referenced_one(struct folio *folio,
 		referenced++;
 
 	if (referenced) {
+		vma_flags_t vma_flags = vma->flags;
+
 		pra->referenced++;
-		pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+		vma_flags_clear(&vma_flags, VMA_LOCKED_BIT);
+		vma_flags_set_mask(&pra->vma_flags, vma_flags);
 	}
 
 	if (!pra->mapcount)
@@ -1054,7 +1057,7 @@ static bool invalid_folio_referenced_vma(struct vm_area_struct *vma, void *arg)
  * @folio: The folio to test.
  * @is_locked: Caller holds lock on the folio.
  * @memcg: target memory cgroup
- * @vm_flags: A combination of all the vma->vm_flags which referenced the folio.
+ * @vma_flags: A combination of all the vma->flags which referenced the folio.
  *
  * Quick test_and_clear_referenced for all mappings of a folio,
  *
@@ -1062,7 +1065,7 @@ static bool invalid_folio_referenced_vma(struct vm_area_struct *vma, void *arg)
  * the function bailed out due to rmap lock contention.
  */
 int folio_referenced(struct folio *folio, int is_locked,
-		     struct mem_cgroup *memcg, vm_flags_t *vm_flags)
+		struct mem_cgroup *memcg, vma_flags_t *vma_flags)
 {
 	bool we_locked = false;
 	struct folio_referenced_arg pra = {
@@ -1078,7 +1081,7 @@ int folio_referenced(struct folio *folio, int is_locked,
 	};
 
 	VM_WARN_ON_ONCE_FOLIO(folio_is_zone_device(folio), folio);
-	*vm_flags = 0;
+	vma_flags_clear_all(vma_flags);
 	if (!pra.mapcount)
 		return 0;
 
@@ -1092,7 +1095,7 @@ int folio_referenced(struct folio *folio, int is_locked,
 	}
 
 	rmap_walk(folio, &rwc);
-	*vm_flags = pra.vm_flags;
+	vma_flags_set_mask(vma_flags, pra.vma_flags);
 
 	if (we_locked)
 		folio_unlock(folio);
mm/vmscan.c +7 -7
index 986dde8e7429..de62899c108d 100644
@@ -861,16 +861,16 @@ static enum folio_references folio_check_references(struct folio *folio,
 						  struct scan_control *sc)
 {
 	int referenced_ptes, referenced_folio;
-	vm_flags_t vm_flags;
+	vma_flags_t vma_flags;
 
 	referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup,
-					   &vm_flags);
+					   &vma_flags);
 
 	/*
 	 * The supposedly reclaimable folio was found to be in a VM_LOCKED vma.
 	 * Let the folio, now marked Mlocked, be moved to the unevictable list.
 	 */
-	if (vm_flags & VM_LOCKED)
+	if (vma_flags_test(&vma_flags, VMA_LOCKED_BIT))
 		return FOLIOREF_ACTIVATE;
 
 	/*
@@ -914,7 +914,7 @@ static enum folio_references folio_check_references(struct folio *folio,
 		/*
 		 * Activate file-backed executable folios after first usage.
 		 */
-		if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio))
+		if (vma_flags_test(&vma_flags, VMA_EXEC_BIT) && folio_is_file_lru(folio))
 			return FOLIOREF_ACTIVATE;
 
 		return FOLIOREF_KEEP;
@@ -2065,7 +2065,7 @@ static void shrink_active_list(unsigned long nr_to_scan,
 {
 	unsigned long nr_taken;
 	unsigned long nr_scanned;
-	vm_flags_t vm_flags;
+	vma_flags_t vma_flags;
 	LIST_HEAD(l_hold);	/* The folios which were snipped off */
 	LIST_HEAD(l_active);
 	LIST_HEAD(l_inactive);
@@ -2109,7 +2109,7 @@ static void shrink_active_list(unsigned long nr_to_scan,
 
 		/* Referenced or rmap lock contention: rotate */
 		if (folio_referenced(folio, 0, sc->target_mem_cgroup,
-				     &vm_flags) != 0) {
+				     &vma_flags) != 0) {
 			/*
 			 * Identify referenced, file-backed active folios and
 			 * give them one more trip around the active list. So
@@ -2119,7 +2119,7 @@ static void shrink_active_list(unsigned long nr_to_scan,
 			 * IO, plus JVM can create lots of anon VM_EXEC folios,
 			 * so we ignore them here.
 			 */
-			if ((vm_flags & VM_EXEC) && folio_is_file_lru(folio)) {
+			if (vma_flags_test(&vma_flags, VMA_EXEC_BIT) && folio_is_file_lru(folio)) {
 				nr_rotated += folio_nr_pages(folio);
 				list_add(&folio->lru, &l_active);
 				continue;
Signature · 1 line
2.47.3
maintainer Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] mm: vmscan: convert folio_referenced() to use vma_flags_t

Quoted 8 lines · Baolin Wang

Replace use of the legacy vm_flags_t flags with vma_flags_t values for folio_referenced() and related logic. This is also a preparation for the following changes.

No functional changes.

---

Signature · 3 lines
Cheers,

David
author reviewer
#3 lore raw

PATCH v2 2/2 mm: mglru: promote mapped executable folios after first usage

In reply to Baolin Wang

Classical LRU protects mapped executable file folios through commit 8cab4754d24a0 ("vmscan: make mapped executable pages the first class citizen") and commit c909e99364c8 ("vmscan: activate executable pages after first usage"), giving executable code a better chance to stay in memory, avoiding IO thrashing and improving workload performance.

However, MGLRU's protection of mapped executable file folios is less reliable. Although shrink_folio_list() checks references, the access flag of mapped executable file folios may have already been checked and cleared by lru_gen_look_around() or walk_mm(). Additionally, folio_update_gen() or lru_gen_set_refs() only sets the 'PG_referenced' flag for mapped executable file folios, which causes shrink_folio_list() to ignore the first usage of these mapped executable file folios and reclaim them easily.

Follow the classical LRU's logic, promoting mapped executable file folios after their first usage in folio_update_gen() and lru_gen_set_refs(), giving executable code a better chance to stay in memory.

On my 32-core Arm machine, with the memcg limit set to 2G, running 'make -j32' to build kernel showed some improvement in sys time.

base patched
9248.543s 7861.579s

---
 include/linux/mm_inline.h |  6 ++++++
 mm/vmscan.c               | 41 ++++++++++++++++++++++++---------------
 2 files changed, 31 insertions(+), 16 deletions(-)
include/linux/mm_inline.h +6 -0
index b5c4dc0f3fe3..30fdf1056312 100644
@@ -30,6 +30,12 @@ static inline int folio_is_file_lru(const struct folio *folio)
 	return !folio_test_swapbacked(folio);
 }
 
+static inline bool folio_is_exec_file(const struct folio *folio,
+		const vma_flags_t *vma_flags)
+{
+	return vma_flags_test(vma_flags, VMA_EXEC_BIT) && folio_is_file_lru(folio);
+}
+
 static __always_inline void __update_lru_size(struct lruvec *lruvec,
 				enum lru_list lru, enum zone_type zid,
 				long nr_pages)
mm/vmscan.c +25 -16

141 lines not on this page · raw message

Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH v2 2/2] mm: mglru: promote mapped executable folios after first usage

Quoted 36 lines · Baolin Wang

Classical LRU protects mapped executable file folios through commit 8cab4754d24a0 ("vmscan: make mapped executable pages the first class citizen") and commit c909e99364c8 ("vmscan: activate executable pages after first usage"), giving executable code a better chance to stay in memory, avoiding IO thrashing and improving workload performance.

However, MGLRU's protection of mapped executable file folios is less reliable. Although shrink_folio_list() checks references, the access flag of mapped executable file folios may have already been checked and cleared by lru_gen_look_around() or walk_mm(). Additionally, folio_update_gen() or lru_gen_set_refs() only sets the 'PG_referenced' flag for mapped executable file folios, which causes shrink_folio_list() to ignore the first usage of these mapped executable file folios and reclaim them easily.

Follow the classical LRU's logic, promoting mapped executable file folios after their first usage in folio_update_gen() and lru_gen_set_refs(), giving executable code a better chance to stay in memory.

On my 32-core Arm machine, with the memcg limit set to 2G, running 'make -j32' to build kernel showed some improvement in sys time.

base patched
9248.543s 7861.579s

---
 include/linux/mm_inline.h |  6 ++++++
 mm/vmscan.c               | 41 ++++++++++++++++++++++++---------------
 2 files changed, 31 insertions(+), 16 deletions(-)
include/linux/mm_inline.h +0 -0
index b5c4dc0f3fe3..30fdf1056312 100644

include/linux/mm_inline.h static inline int folio_is_file_lru(const struct folio *folio) · line 37

Quoted 8 lines
include/linux/mm_inline.h +5 -0
@@ -30,6 +30,12 @@ static inline int folio_is_file_lru(const struct folio *folio)
 	return !folio_test_swapbacked(folio);
 }
 
+static inline bool folio_is_exec_file(const struct folio *folio,
+		const vma_flags_t *vma_flags)
+{
+	return vma_flags_test(vma_flags, VMA_EXEC_BIT) && folio_is_file_lru(folio);
+}

I don't quite like a generic folio_is* helper to consume vma flags. Whether it is executable depends on the VMA. When called from a non-exec VMA it would just give a wrong answer.

Not sure if something like "is_executable_file_folio()"/"is_exec_file_folio" would be better, to distinguish from the actualy folio_is* helpers that are independent of context.

Also, can this helper just go to vmscan.c?

Signature · 3 lines
Cheers,

David
author reviewer
#5 lore raw

Re: [PATCH v2 2/2] mm: mglru: promote mapped executable folios after first usage

Quoted 50 lines · David Hildenbrand (Arm)
Quoted 36 lines · Baolin Wang

Classical LRU protects mapped executable file folios through commit 8cab4754d24a0 ("vmscan: make mapped executable pages the first class citizen") and commit c909e99364c8 ("vmscan: activate executable pages after first usage"), giving executable code a better chance to stay in memory, avoiding IO thrashing and improving workload performance.

However, MGLRU's protection of mapped executable file folios is less reliable. Although shrink_folio_list() checks references, the access flag of mapped executable file folios may have already been checked and cleared by lru_gen_look_around() or walk_mm(). Additionally, folio_update_gen() or lru_gen_set_refs() only sets the 'PG_referenced' flag for mapped executable file folios, which causes shrink_folio_list() to ignore the first usage of these mapped executable file folios and reclaim them easily.

Follow the classical LRU's logic, promoting mapped executable file folios after their first usage in folio_update_gen() and lru_gen_set_refs(), giving executable code a better chance to stay in memory.

On my 32-core Arm machine, with the memcg limit set to 2G, running 'make -j32' to build kernel showed some improvement in sys time.

base patched
9248.543s 7861.579s

---
  include/linux/mm_inline.h |  6 ++++++
  mm/vmscan.c               | 41 ++++++++++++++++++++++++---------------
  2 files changed, 31 insertions(+), 16 deletions(-)
include/linux/mm_inline.h +0 -0
index b5c4dc0f3fe3..30fdf1056312 100644

include/linux/mm_inline.h static inline int folio_is_file_lru(const struct folio *folio) · line 37

Quoted 8 lines
include/linux/mm_inline.h +5 -0
@@ -30,6 +30,12 @@ static inline int folio_is_file_lru(const struct folio *folio)
  	return !folio_test_swapbacked(folio);
  }
 
+static inline bool folio_is_exec_file(const struct folio *folio,
+		const vma_flags_t *vma_flags)
+{
+	return vma_flags_test(vma_flags, VMA_EXEC_BIT) && folio_is_file_lru(folio);
+}

I don't quite like a generic folio_is* helper to consume vma flags. Whether it is executable depends on the VMA. When called from a non-exec VMA it would just give a wrong answer.

Quoted 3 lines

Not sure if something like "is_executable_file_folio()"/"is_exec_file_folio" would be better, to distinguish from the actualy folio_is* helpers that are independent of context.

OK. 'is_exec_file_folio' sounds better to me.

Quoted 1 line

Also, can this helper just go to vmscan.c?

Yes. I originally expected this helper might be used by other callers, but for now it's only used in vmscan.c, so will do.

Thanks for taking a look.