Skip to contentSkip to conversation

mm: shrink skip folio mapped by an exiting process

PATCH Superseded Zhiguo Jiang 3 participants 12 messages Jul 8, 2024 – Jul 9, 2024

Subsystems (4)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 mm: shrink skip folio mapped by an exiting process

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

 mm/rmap.c   | 13 +++++++++++++
 mm/vmscan.c |  7 ++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +13 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
 	int referenced = 0;
 	unsigned long start = address, ptes = 0;
 
+	/*
+	 * Skip the non-shared anonymous folio mapped solely by
+	 * the single exiting process, and release it directly
+	 * in the process exiting.
+	 */
+	if ((!atomic_read(&vma->vm_mm->mm_users) ||
+		test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+		folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+		!folio_likely_mapped_shared(folio)) {
+		pra->referenced = -1;
+		return false;
+	}
+
 	while (page_vma_mapped_walk(&pvmw)) {
 		address = pvmw.address;
 
mm/vmscan.c +6 -1
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references folio_check_references(struct folio *folio,
 	if (vm_flags & VM_LOCKED)
 		return FOLIOREF_ACTIVATE;
 
-	/* rmap lock contention: rotate */
+	/*
+	 * There are two cases to consider.
+	 * 1) Rmap lock contention: rotate.
+	 * 2) Skip the non-shared anonymous folio mapped solely by
+	 *    the single exiting process.
+	 */
 	if (referenced_ptes == -1)
 		return FOLIOREF_KEEP;
 
Signature · 1 line
2.39.0
#1 lore raw
Quoted 42 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

  mm/rmap.c   | 13 +++++++++++++
  mm/vmscan.c |  7 ++++++-
  2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
  	int referenced = 0;
  	unsigned long start = address, ptes = 0;

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 854

Quoted 10 lines
mm/rmap.c +9 -0
 
+	/*
+	 * Skip the non-shared anonymous folio mapped solely by
+	 * the single exiting process, and release it directly
+	 * in the process exiting.
+	 */
+	if ((!atomic_read(&vma->vm_mm->mm_users) ||
+		test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+		folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+		!folio_likely_mapped_shared(folio)) {

I'm currently working on moving all folio_likely_mapped_shared() under the PTL, where we are then sure that the folio is actually mapped by this process (e.g., no concurrent unmapping poisslbe).

Can we do the same here directly?

Signature · 3 lines
Cheers,

David / dhildenb
reviewer
#2 lore raw
Quoted 59 lines · David Hildenbrand
Quoted 42 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

  mm/rmap.c   | 13 +++++++++++++
  mm/vmscan.c |  7 ++++++-
  2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
      int referenced = 0;
      unsigned long start = address, ptes = 0;

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 854

Quoted 10 lines
mm/rmap.c +9 -0
 
+     /*
+      * Skip the non-shared anonymous folio mapped solely by
+      * the single exiting process, and release it directly
+      * in the process exiting.
+      */
+     if ((!atomic_read(&vma->vm_mm->mm_users) ||
+             test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+             folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+             !folio_likely_mapped_shared(folio)) {

I'm currently working on moving all folio_likely_mapped_shared() under the PTL, where we are then sure that the folio is actually mapped by this process (e.g., no concurrent unmapping poisslbe).

Quoted 1 line

Can we do the same here directly?

Implementing this is challenging because page_vma_mapped_walk() is responsible for
traversing the page table to acquire and release the PTL. This becomes particularly
complex with mTHP, as we may need to interrupt the page_vma_mapped_walk loop at the first PTE.

Quoted 6 lines

--
Cheers,

David / dhildenb

Thanks
Barry

#3 lore raw
Quoted 60 lines · Barry Song
Quoted 59 lines · David Hildenbrand
Quoted 42 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

   mm/rmap.c   | 13 +++++++++++++
   mm/vmscan.c |  7 ++++++-
   2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
       int referenced = 0;
       unsigned long start = address, ptes = 0;

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 854

Quoted 10 lines
mm/rmap.c +9 -0
 
+     /*
+      * Skip the non-shared anonymous folio mapped solely by
+      * the single exiting process, and release it directly
+      * in the process exiting.
+      */
+     if ((!atomic_read(&vma->vm_mm->mm_users) ||
+             test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+             folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+             !folio_likely_mapped_shared(folio)) {

I'm currently working on moving all folio_likely_mapped_shared() under the PTL, where we are then sure that the folio is actually mapped by this process (e.g., no concurrent unmapping poisslbe).

Quoted 8 lines
Quoted 1 line

Can we do the same here directly?

Implementing this is challenging because page_vma_mapped_walk() is responsible for
traversing the page table to acquire and release the PTL. This becomes particularly
complex with mTHP, as we may need to interrupt the page_vma_mapped_walk loop at the first PTE.

Why can't we perform the check under the PTL and bail out? I'm missing something important.

Signature · 3 lines
Cheers,

David / dhildenb
reviewer
#4 lore raw
Quoted 71 lines · David Hildenbrand
Quoted 60 lines · Barry Song
Quoted 59 lines · David Hildenbrand
Quoted 42 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

   mm/rmap.c   | 13 +++++++++++++
   mm/vmscan.c |  7 ++++++-
   2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
       int referenced = 0;
       unsigned long start = address, ptes = 0;

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 854

Quoted 10 lines
mm/rmap.c +9 -0
 
+     /*
+      * Skip the non-shared anonymous folio mapped solely by
+      * the single exiting process, and release it directly
+      * in the process exiting.
+      */
+     if ((!atomic_read(&vma->vm_mm->mm_users) ||
+             test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+             folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+             !folio_likely_mapped_shared(folio)) {

I'm currently working on moving all folio_likely_mapped_shared() under the PTL, where we are then sure that the folio is actually mapped by this process (e.g., no concurrent unmapping poisslbe).

Quoted 8 lines
Quoted 1 line

Can we do the same here directly?

Implementing this is challenging because page_vma_mapped_walk() is responsible for
traversing the page table to acquire and release the PTL. This becomes particularly
complex with mTHP, as we may need to interrupt the page_vma_mapped_walk loop at the first PTE.

Quoted 2 lines

Why can't we perform the check under the PTL and bail out? I'm missing something important.

You are right. we might use page_vma_mapped_walk_done() to bail out.

Quoted 6 lines

--
Cheers,

David / dhildenb

reviewer
#5 lore raw
In reply to Zhiguo Jiang
Quoted 49 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

 mm/rmap.c   | 13 +++++++++++++
 mm/vmscan.c |  7 ++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +5 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
        int referenced = 0;
        unsigned long start = address, ptes = 0;
 
+       /*
+        * Skip the non-shared anonymous folio mapped solely by
+        * the single exiting process, and release it directly
+        * in the process exiting.
+        */

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 860

Quoted 10 lines
mm/rmap.c +8 -0
+       if ((!atomic_read(&vma->vm_mm->mm_users) ||
+               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+               folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+               !folio_likely_mapped_shared(folio)) {
+               pra->referenced = -1;
+               return false;
+       }
+
        while (page_vma_mapped_walk(&pvmw)) {
                address = pvmw.address;

As David suggested, what about the below?

@@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio *folio,

                        continue;
                }

+ /*

+                * Skip the non-shared anonymous folio mapped solely by
+                * the single exiting process, and release it directly
+                * in the process exiting.
+                */

+ if ((!atomic_read(&vma->vm_mm->mm_users) ||
+ test_bit(MMF_OOM_SKIP,
&vma->vm_mm->flags)) &&
+ folio_test_anon(folio) &&
folio_test_swapbacked(folio) &&
+ !folio_likely_mapped_shared(folio)) {

+                       pra->referenced = -1;
+                       page_vma_mapped_walk_done(&pvmw);
+                       return false;
+               }
+
                if (pvmw.pte) {
                        if (lru_gen_enabled() &&
                            pte_young(ptep_get(pvmw.pte))) {

By the way, I am not convinced that using test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags) is
correct (I think it is wrong). For example, global_init can directly have it:

                if (is_global_init(p)) {
                        can_oom_reap = false;
                        set_bit(MMF_OOM_SKIP, &mm->flags);
                        pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
                                        task_pid_nr(victim), victim->comm,
                                        task_pid_nr(p), p->comm);
                        continue;
                }

And exit_mmap() automatically has MMF_OOM_SKIP.

What is the purpose of this check? Is there a better way to determine if a process is an
OOM target? What about check_stable_address_space() ?

Quoted 20 lines
mm/vmscan.c +6 -2
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references folio_check_references(struct folio *folio,
        if (vm_flags & VM_LOCKED)
                return FOLIOREF_ACTIVATE;
 
-       /* rmap lock contention: rotate */
+       /*
+        * There are two cases to consider.
+        * 1) Rmap lock contention: rotate.
+        * 2) Skip the non-shared anonymous folio mapped solely by
+        *    the single exiting process.
+        */
        if (referenced_ptes == -1)
                return FOLIOREF_KEEP;
 
--
Quoted 2 lines

2.39.0

Thanks
Barry

#6 lore raw
Quoted 44 lines · Barry Song
Quoted 43 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

  mm/rmap.c   | 13 +++++++++++++
  mm/vmscan.c |  7 ++++++-
  2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio *folio,
         int referenced = 0;
         unsigned long start = address, ptes = 0;
 
Quoted 15 lines
Quoted 5 lines
mm/rmap.c +5 -0
+       /*
+        * Skip the non-shared anonymous folio mapped solely by
+        * the single exiting process, and release it directly
+        * in the process exiting.
+        */

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 860

Quoted 10 lines
mm/rmap.c +8 -0
+       if ((!atomic_read(&vma->vm_mm->mm_users) ||
+               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+               folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+               !folio_likely_mapped_shared(folio)) {
+               pra->referenced = -1;
+               return false;
+       }
+
         while (page_vma_mapped_walk(&pvmw)) {
                 address = pvmw.address;

Sure, I agree with your modification suggestions. This way, using PTL indeed sure that the folio is mapped by this process.
Thanks

Quoted 39 lines

As David suggested, what about the below?

mm/rmap.c +7 -0
@@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio *folio,
                         continue;
                 }
 
+               /*
+                * Skip the non-shared anonymous folio mapped solely by
+                * the single exiting process, and release it directly
+                * in the process exiting.
+                */
+               if ((!atomic_read(&vma->vm_mm->mm_users) ||
+                                       test_bit(MMF_OOM_SKIP,

&vma->vm_mm->flags)) &&
+ folio_test_anon(folio) &&
folio_test_swapbacked(folio) &&

mm/rmap.c +6 -0
+                               !folio_likely_mapped_shared(folio)) {
+                       pra->referenced = -1;
+                       page_vma_mapped_walk_done(&pvmw);
+                       return false;
+               }
+
                 if (pvmw.pte) {
                         if (lru_gen_enabled() &&
                             pte_young(ptep_get(pvmw.pte))) {

By the way, I am not convinced that using test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags) is
correct (I think it is wrong). For example, global_init can directly have it:

                 if (is_global_init(p)) {
                         can_oom_reap = false;
                         set_bit(MMF_OOM_SKIP, &mm->flags);
                         pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n",
                                         task_pid_nr(victim), victim->comm,
                                         task_pid_nr(p), p->comm);
                         continue;
                 }
Quoted 5 lines

And exit_mmap() automatically has MMF_OOM_SKIP.

What is the purpose of this check? Is there a better way to determine if a process is an
OOM target? What about check_stable_address_space() ?

1.Sorry, I overlook the situation with if (is_global_init(p)), MMF_OOM_SKIP is indeed not suitable.

2.check_stable_address_space() can indicate oom_reaper, but it seems unable to identify the situation where the process exits normally. What about task_is_dying()? static inline bool task_is_dying(void) { return tsk_is_oom_victim(current) || fatal_signal_pending(current) || (current->flags & PF_EXITING); } Thanks

Quoted 25 lines
Quoted 21 lines
mm/vmscan.c +6 -2
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references folio_check_references(struct folio *folio,
         if (vm_flags & VM_LOCKED)
                 return FOLIOREF_ACTIVATE;
 
-       /* rmap lock contention: rotate */
+       /*
+        * There are two cases to consider.
+        * 1) Rmap lock contention: rotate.
+        * 2) Skip the non-shared anonymous folio mapped solely by
+        *    the single exiting process.
+        */
         if (referenced_ptes == -1)
                 return FOLIOREF_KEEP;
 
--
Quoted 2 lines

2.39.0

Thanks
Barry

#7 lore raw
Quoted 107 lines · zhiguojiang
Quoted 45 lines · Barry Song
Quoted 44 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

  mm/rmap.c   | 13 +++++++++++++
  mm/vmscan.c |  7 ++++++-

  2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio 
 *folio,
          int referenced = 0;
          unsigned long start = address, ptes = 0;
 
Quoted 16 lines
Quoted 8 lines
mm/rmap.c +8 -0
+       /*
+        * Skip the non-shared anonymous folio mapped solely by
+        * the single exiting process, and release it directly
+        * in the process exiting.
+        */
+       if ((!atomic_read(&vma->vm_mm->mm_users) ||
+               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+               folio_test_anon(folio) && 
Quoted 8 lines

folio_test_swapbacked(folio) &&
+               !folio_likely_mapped_shared(folio)) { +               pra->referenced = -1;
+               return false;
+       }
+
         while (page_vma_mapped_walk(&pvmw)) { address = pvmw.address;

Sure, I agree with your modification suggestions. This way, using PTL indeed sure that the folio is mapped by this process.
Thanks

Quoted 40 lines

As David suggested, what about the below?

mm/rmap.c +7 -0
@@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio 
 *folio,
                          continue;
                  }
 
+               /*
+                * Skip the non-shared anonymous folio mapped solely by
+                * the single exiting process, and release it directly
+                * in the process exiting.
+                */
+               if ((!atomic_read(&vma->vm_mm->mm_users) ||
+                                       test_bit(MMF_OOM_SKIP,

&vma->vm_mm->flags)) &&
+                               folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+ !folio_likely_mapped_shared(folio)) {
+                       pra->referenced = -1;
+                       page_vma_mapped_walk_done(&pvmw); +                       return false;
+               }
+
                 if (pvmw.pte) {
                         if (lru_gen_enabled() && pte_young(ptep_get(pvmw.pte))) {

By the way, I am not convinced that using test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags) is
correct (I think it is wrong).   For example, global_init can directly have it:
                 if (is_global_init(p)) {
                         can_oom_reap = false;
                         set_bit(MMF_OOM_SKIP, &mm->flags); pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n", task_pid_nr(victim), victim->comm, task_pid_nr(p), p->comm); continue;
                 }

Quoted 8 lines
Quoted 5 lines

And exit_mmap() automatically has MMF_OOM_SKIP.

What is the purpose of this check? Is there a better way to determine if a process is an
OOM target? What about check_stable_address_space() ?

1.Sorry, I overlook the situation with if (is_global_init(p)), MMF_OOM_SKIP is indeed not suitable.

2.check_stable_address_space() can indicate oom_reaper, but it seems unable to identify the situation where the process exits normally. What about task_is_dying()? static inline bool task_is_dying(void) { return tsk_is_oom_victim(current) || fatal_signal_pending(current) || (current->flags & PF_EXITING); } Thanks

We can migrate task_is_dying() from mm/memcontrol.c to include/linux/oom.h

Quoted 33 lines

static inline bool task_is_dying(void)
{
    return tsk_is_oom_victim(current) || fatal_signal_pending(current) || (current->flags & PF_EXITING);
}

Quoted 26 lines
Quoted 24 lines
mm/vmscan.c +6 -1
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references 
 folio_check_references(struct folio *folio,
          if (vm_flags & VM_LOCKED)
                  return FOLIOREF_ACTIVATE;
 
-       /* rmap lock contention: rotate */
+       /*
+        * There are two cases to consider.
+        * 1) Rmap lock contention: rotate.
+        * 2) Skip the non-shared anonymous folio mapped solely by
+        *    the single exiting process.
+        */
          if (referenced_ptes == -1)
                  return FOLIOREF_KEEP;
Signature · 2 lines
-- 
2.39.0

Thanks
Barry

reviewer
#8 lore raw
Quoted 124 lines · zhiguojiang <[email protected]> 于 2024年7月9日周二 00:25写道:
Quoted 121 lines · zhiguojiang
Quoted 47 lines · Barry Song
Quoted 45 lines · Zhiguo Jiang

The releasing process of the non-shared anonymous folio mapped solely by
an exiting process may go through two flows: 1) the anonymous folio is firstly is swaped-out into swapspace and transformed into a swp_entry in shrink_folio_list; 2) then the swp_entry is released in the process exiting flow. This will increase the cpu load of releasing a non-shared anonymous folio mapped solely by an exiting process, because the folio go through swap-out and the releasing the swapspace and swp_entry.

When system is low memory, it is more likely to occur, because more backend applidatuions will be killed.

The modification is that shrink skips the non-shared anonymous folio solely mapped by an exting process and the folio is only released directly in the process exiting flow, which will save swap-out time and alleviate the load of the process exiting.

---

Change log:
v4->v5:
1.Modify to skip non-shared anonymous folio only.
2.Update comments for pra->referenced = -1.
v3->v4:
1.Modify that the unshared folios mapped only in exiting task are skip. v2->v3:
Nothing.
v1->v2:
1.The VM_EXITING added in v1 patch is removed, because it will fail to compile in 32-bit system.

  mm/rmap.c   | 13 +++++++++++++
  mm/vmscan.c |  7 ++++++-
  2 files changed, 19 insertions(+), 1 deletion(-)
mm/rmap.c +0 -0
index 26806b49a86f..5b5281d71dbb
@@ -843,6 +843,19 @@ static bool folio_referenced_one(struct folio
 *folio,
         int referenced = 0;
         unsigned long start = address, ptes = 0;
 
Quoted 16 lines
Quoted 9 lines
mm/rmap.c +8 -0
+       /*
+        * Skip the non-shared anonymous folio mapped solely by
+        * the single exiting process, and release it directly
+        * in the process exiting.
+        */
+       if ((!atomic_read(&vma->vm_mm->mm_users) ||
+               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
+               folio_test_anon(folio) &&

folio_test_swapbacked(folio) &&

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 860

Quoted 7 lines
mm/rmap.c +5 -0
+               !folio_likely_mapped_shared(folio)) {
+               pra->referenced = -1;
+               return false;
+       }
+
         while (page_vma_mapped_walk(&pvmw)) {
                 address = pvmw.address;

Sure, I agree with your modification suggestions. This way, using PTL indeed sure
that the folio is mapped by this process.
Thanks

Quoted 43 lines

As David suggested, what about the below?

mm/rmap.c +7 -0
@@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio
 *folio,
                         continue;
                 }
 
+               /*
+                * Skip the non-shared anonymous folio mapped solely by
+                * the single exiting process, and release it directly
+                * in the process exiting.
+                */
+               if ((!atomic_read(&vma->vm_mm->mm_users) ||
+                                       test_bit(MMF_OOM_SKIP,

&vma->vm_mm->flags)) &&
+ folio_test_anon(folio) &&
folio_test_swapbacked(folio) &&
+ !folio_likely_mapped_shared(folio)) {

mm/rmap.c +5 -0
+                       pra->referenced = -1;
+                       page_vma_mapped_walk_done(&pvmw);
+                       return false;
+               }
+
                 if (pvmw.pte) {
                         if (lru_gen_enabled() &&
                             pte_young(ptep_get(pvmw.pte))) {

By the way, I am not convinced that using test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags) is
correct (I think it is wrong). For example, global_init can
directly have it:

                 if (is_global_init(p)) {
                         can_oom_reap = false;
                         set_bit(MMF_OOM_SKIP, &mm->flags);
                         pr_info("oom killer %d (%s) has mm pinned by
%d (%s)\n",
                                         task_pid_nr(victim),
victim->comm,
                                         task_pid_nr(p), p->comm);
                         continue;
                 }
Quoted 5 lines

And exit_mmap() automatically has MMF_OOM_SKIP.

What is the purpose of this check? Is there a better way to determine if a process is an
OOM target? What about check_stable_address_space() ?

1.Sorry, I overlook the situation with if (is_global_init(p)), MMF_OOM_SKIP is indeed not suitable.

Quoted 12 lines
Quoted 5 lines

2.check_stable_address_space() can indicate oom_reaper, but it seems unable to identify the situation where the process exits normally. What about task_is_dying()? static inline bool task_is_dying(void) { return tsk_is_oom_victim(current) || fatal_signal_pending(current) || (current->flags & PF_EXITING); } Thanks

We can migrate task_is_dying() from mm/memcontrol.c to include/linux/oom.h

Quoted 5 lines

static inline bool task_is_dying(void)

{
    return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
        (current->flags & PF_EXITING);
}

no. current is kswapd.

Quoted 29 lines
Quoted 27 lines
Quoted 26 lines
Quoted 22 lines
mm/vmscan.c +6 -2
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references
 folio_check_references(struct folio *folio,
         if (vm_flags & VM_LOCKED)
                 return FOLIOREF_ACTIVATE;
 
-       /* rmap lock contention: rotate */
+       /*
+        * There are two cases to consider.
+        * 1) Rmap lock contention: rotate.
+        * 2) Skip the non-shared anonymous folio mapped solely by
+        *    the single exiting process.
+        */
         if (referenced_ptes == -1)
                 return FOLIOREF_KEEP;
 
--
Quoted 2 lines

2.39.0

Thanks
Barry

#9 lore raw
Quoted 162 lines · Barry Song

zhiguojiang <[email protected]> 于 2024年7月9日周二 00:25写道:

    在 2024/7/8 20:17, zhiguojiang 写道:
    >
    >
    > 在 2024/7/8 19:02, Barry Song 写道:
    >> On Mon, Jul 8, 2024 at 9:04 PM Zhiguo Jiang <[email protected]>
    >> wrote:
    >>> The releasing process of the non-shared anonymous folio mapped
    >>> solely by
    >>> an exiting process may go through two flows: 1) the anonymous
    folio is
    >>> firstly is swaped-out into swapspace and transformed into a
    swp_entry
    >>> in shrink_folio_list; 2) then the swp_entry is released in the
    process
    >>> exiting flow. This will increase the cpu load of releasing a
    non-shared
    >>> anonymous folio mapped solely by an exiting process, because
    the folio
    >>> go through swap-out and the releasing the swapspace and swp_entry.
    >>>
    >>> When system is low memory, it is more likely to occur, because
    more
    >>> backend applidatuions will be killed.
    >>>
    >>> The modification is that shrink skips the non-shared anonymous
    folio
    >>> solely mapped by an exting process and the folio is only released
    >>> directly in the process exiting flow, which will save swap-out
    time
    >>> and alleviate the load of the process exiting.
    >>>
    >>> Signed-off-by: Zhiguo Jiang <[email protected]>
    >>> ---
    >>>
    >>> Change log:
    >>> v4->v5:
    >>> 1.Modify to skip non-shared anonymous folio only.
    >>> 2.Update comments for pra->referenced = -1.
    >>> v3->v4:
    >>> 1.Modify that the unshared folios mapped only in exiting task
    are skip.
    >>> v2->v3:
    >>> Nothing.
    >>> v1->v2:
    >>> 1.The VM_EXITING added in v1 patch is removed, because it will
    fail
    >>> to compile in 32-bit system.
    >>>
    >>>   mm/rmap.c   | 13 +++++++++++++
    >>>   mm/vmscan.c |  7 ++++++-
    >>>   2 files changed, 19 insertions(+), 1 deletion(-)
    >>>
    >>> diff --git a/mm/rmap.c b/mm/rmap.c
    >>> index 26806b49a86f..5b5281d71dbb
    >>> --- a/mm/rmap.c
    >>> +++ b/mm/rmap.c
    >>> @@ -843,6 +843,19 @@ static bool folio_referenced_one(struct
    folio
    >>> *folio,
    >>>          int referenced = 0;
    >>>          unsigned long start = address, ptes = 0;
    >>>
    >>> +       /*
    >>> +        * Skip the non-shared anonymous folio mapped solely by
    >>> +        * the single exiting process, and release it directly
    >>> +        * in the process exiting.
    >>> +        */
    >>> +       if ((!atomic_read(&vma->vm_mm->mm_users) ||
    >>> +               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
    >>> +               folio_test_anon(folio) &&
    >>> folio_test_swapbacked(folio) &&
    >>> + !folio_likely_mapped_shared(folio)) {
    >>> +               pra->referenced = -1;
    >>> +               return false;
    >>> +       }
    >>> +
    >>>          while (page_vma_mapped_walk(&pvmw)) {
    >>>                  address = pvmw.address;
    > Sure, I agree with your modification suggestions. This way,
    using PTL
    > indeed sure
    > that the folio is mapped by this process.
    > Thanks
    >> As David suggested, what about the below?
    >>
    >> @@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio
    >> *folio,
    >>                          continue;
    >>                  }
    >>
    >> +               /*
    >> +                * Skip the non-shared anonymous folio mapped
    solely by
    >> +                * the single exiting process, and release it
    directly
    >> +                * in the process exiting.
    >> +                */
    >> +               if ((!atomic_read(&vma->vm_mm->mm_users) ||
    >> + test_bit(MMF_OOM_SKIP,
    >> &vma->vm_mm->flags)) &&
    >> + folio_test_anon(folio) &&
    >> folio_test_swapbacked(folio) &&
    >> + !folio_likely_mapped_shared(folio)) {
    >> +                       pra->referenced = -1;
    >> + page_vma_mapped_walk_done(&pvmw);
    >> +                       return false;
    >> +               }
    >> +
    >>                  if (pvmw.pte) {
    >>                          if (lru_gen_enabled() &&
    >> pte_young(ptep_get(pvmw.pte))) {
    >>
    >>
    >> By the way, I am not convinced that using test_bit(MMF_OOM_SKIP,
    >> &vma->vm_mm->flags) is
    >> correct (I think it is wrong).   For example, global_init can
    >> directly have it:
    >>                  if (is_global_init(p)) {
    >>                          can_oom_reap = false;
    >>                          set_bit(MMF_OOM_SKIP, &mm->flags);
    >>                          pr_info("oom killer %d (%s) has mm
    pinned by
    >> %d (%s)\n",
    >> task_pid_nr(victim),
    >> victim->comm,
    >> task_pid_nr(p), p->comm);
    >>                          continue;
    >>                  }
    >>
    >> And exit_mmap() automatically has MMF_OOM_SKIP.
    >>
    >> What is the purpose of this check? Is there a better way to
    determine
    >> if a process is an
    >> OOM target? What about check_stable_address_space() ?
    > 1.Sorry, I overlook the situation with if (is_global_init(p)),
    > MMF_OOM_SKIP is indeed not suitable.
    >
    > 2.check_stable_address_space() can indicate oom_reaper, but it
    seems
    > unable to identify the situation where the process exits normally.
    > What about task_is_dying()? static inline bool
    task_is_dying(void) {
    > return tsk_is_oom_victim(current) ||
    fatal_signal_pending(current) ||
    > (current->flags & PF_EXITING); } Thanks
    We can migrate task_is_dying() from mm/memcontrol.c to
    include/linux/oom.h
    > static inline bool task_is_dying(void)
    > {
    >     return tsk_is_oom_victim(current) ||
    fatal_signal_pending(current) ||
    >         (current->flags & PF_EXITING);
    > }
Quoted 1 line

no. current is kswapd.

Hi Barry,

It seems feasible for check_stable_address_space() replacing MMF_OOM_SKIP. check_stable_address_space() can indicate oom kill, and !atomic_read(&vma->vm_mm->mm_users) can indicate the normal process exiting.

        /*
         * Skip the non-shared anonymous folio mapped solely by
         * the single exiting process, and release it directly
         * in the process exiting.
         */
        if ((!atomic_read(&vma->vm_mm->mm_users) || check_stable_address_space(vma->vm_mm)) && folio_test_anon(folio) && folio_test_swapbacked(folio) && !folio_likely_mapped_shared(folio)) { pra->referenced = -1; page_vma_mapped_walk_done(&pvmw);
            return false;
        }

Thanks
Zhiguo

Quoted 30 lines
Quoted 29 lines
Quoted 28 lines
Quoted 24 lines
mm/vmscan.c +6 -2
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references
 folio_check_references(struct folio *folio,
          if (vm_flags & VM_LOCKED)
                  return FOLIOREF_ACTIVATE;
 
-       /* rmap lock contention: rotate */
+       /*
+        * There are two cases to consider.
+        * 1) Rmap lock contention: rotate.
+        * 2) Skip the non-shared anonymous folio mapped solely by
+        *    the single exiting process.
+        */
          if (referenced_ptes == -1)
                  return FOLIOREF_KEEP;
 
--
Quoted 2 lines

2.39.0

Thanks
Barry

reviewer
#10 lore raw
Quoted 174 lines · zhiguojiang
Quoted 162 lines · Barry Song

zhiguojiang <[email protected]> 于 2024年7月9日周二 00:25写道:

    在 2024/7/8 20:17, zhiguojiang 写道:
    >
    >
    > 在 2024/7/8 19:02, Barry Song 写道:
    >> On Mon, Jul 8, 2024 at 9:04 PM Zhiguo Jiang <[email protected]>
    >> wrote:
    >>> The releasing process of the non-shared anonymous folio mapped
    >>> solely by
    >>> an exiting process may go through two flows: 1) the anonymous
    folio is
    >>> firstly is swaped-out into swapspace and transformed into a
    swp_entry
    >>> in shrink_folio_list; 2) then the swp_entry is released in the
    process
    >>> exiting flow. This will increase the cpu load of releasing a
    non-shared
    >>> anonymous folio mapped solely by an exiting process, because
    the folio
    >>> go through swap-out and the releasing the swapspace and swp_entry.
    >>>
    >>> When system is low memory, it is more likely to occur, because
    more
    >>> backend applidatuions will be killed.
    >>>
    >>> The modification is that shrink skips the non-shared anonymous
    folio
    >>> solely mapped by an exting process and the folio is only released
    >>> directly in the process exiting flow, which will save swap-out
    time
    >>> and alleviate the load of the process exiting.
    >>>
    >>> Signed-off-by: Zhiguo Jiang <[email protected]>
    >>> ---
    >>>
    >>> Change log:
    >>> v4->v5:
    >>> 1.Modify to skip non-shared anonymous folio only.
    >>> 2.Update comments for pra->referenced = -1.
    >>> v3->v4:
    >>> 1.Modify that the unshared folios mapped only in exiting task
    are skip.
    >>> v2->v3:
    >>> Nothing.
    >>> v1->v2:
    >>> 1.The VM_EXITING added in v1 patch is removed, because it will
    fail
    >>> to compile in 32-bit system.
    >>>
    >>>   mm/rmap.c   | 13 +++++++++++++
    >>>   mm/vmscan.c |  7 ++++++-
    >>>   2 files changed, 19 insertions(+), 1 deletion(-)
    >>>
    >>> diff --git a/mm/rmap.c b/mm/rmap.c
    >>> index 26806b49a86f..5b5281d71dbb
    >>> --- a/mm/rmap.c
    >>> +++ b/mm/rmap.c
    >>> @@ -843,6 +843,19 @@ static bool folio_referenced_one(struct
    folio
    >>> *folio,
    >>>          int referenced = 0;
    >>>          unsigned long start = address, ptes = 0;
    >>>
    >>> +       /*
    >>> +        * Skip the non-shared anonymous folio mapped solely by
    >>> +        * the single exiting process, and release it directly
    >>> +        * in the process exiting.
    >>> +        */
    >>> +       if ((!atomic_read(&vma->vm_mm->mm_users) ||
    >>> +               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
    >>> +               folio_test_anon(folio) &&
    >>> folio_test_swapbacked(folio) &&
    >>> + !folio_likely_mapped_shared(folio)) {
    >>> +               pra->referenced = -1;
    >>> +               return false;
    >>> +       }
    >>> +
    >>>          while (page_vma_mapped_walk(&pvmw)) {
    >>>                  address = pvmw.address;
    > Sure, I agree with your modification suggestions. This way,
    using PTL
    > indeed sure
    > that the folio is mapped by this process.
    > Thanks
    >> As David suggested, what about the below?
    >>
    >> @@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio
    >> *folio,
    >>                          continue;
    >>                  }
    >>
    >> +               /*
    >> +                * Skip the non-shared anonymous folio mapped
    solely by
    >> +                * the single exiting process, and release it
    directly
    >> +                * in the process exiting.
    >> +                */
    >> +               if ((!atomic_read(&vma->vm_mm->mm_users) ||
    >> + test_bit(MMF_OOM_SKIP,
    >> &vma->vm_mm->flags)) &&
    >> + folio_test_anon(folio) &&
    >> folio_test_swapbacked(folio) &&
    >> + !folio_likely_mapped_shared(folio)) {
    >> +                       pra->referenced = -1;
    >> + page_vma_mapped_walk_done(&pvmw);
    >> +                       return false;
    >> +               }
    >> +
    >>                  if (pvmw.pte) {
    >>                          if (lru_gen_enabled() &&
    >> pte_young(ptep_get(pvmw.pte))) {
    >>
    >>
    >> By the way, I am not convinced that using test_bit(MMF_OOM_SKIP,
    >> &vma->vm_mm->flags) is
    >> correct (I think it is wrong).   For example, global_init can
    >> directly have it:
    >>                  if (is_global_init(p)) {
    >>                          can_oom_reap = false;
    >>                          set_bit(MMF_OOM_SKIP, &mm->flags);
    >>                          pr_info("oom killer %d (%s) has mm
    pinned by
    >> %d (%s)\n",
    >> task_pid_nr(victim),
    >> victim->comm,
    >> task_pid_nr(p), p->comm);
    >>                          continue;
    >>                  }
    >>
    >> And exit_mmap() automatically has MMF_OOM_SKIP.
    >>
    >> What is the purpose of this check? Is there a better way to
    determine
    >> if a process is an
    >> OOM target? What about check_stable_address_space() ?
    > 1.Sorry, I overlook the situation with if (is_global_init(p)),
    > MMF_OOM_SKIP is indeed not suitable.
    >
    > 2.check_stable_address_space() can indicate oom_reaper, but it
    seems
    > unable to identify the situation where the process exits normally.
    > What about task_is_dying()? static inline bool
    task_is_dying(void) {
    > return tsk_is_oom_victim(current) ||
    fatal_signal_pending(current) ||
    > (current->flags & PF_EXITING); } Thanks
    We can migrate task_is_dying() from mm/memcontrol.c to
    include/linux/oom.h
    > static inline bool task_is_dying(void)
    > {
    >     return tsk_is_oom_victim(current) ||
    fatal_signal_pending(current) ||
    >         (current->flags & PF_EXITING);
    > }
Quoted 1 line

no. current is kswapd.

Hi Barry,

It seems feasible for check_stable_address_space() replacing MMF_OOM_SKIP. check_stable_address_space() can indicate oom kill, and !atomic_read(&vma->vm_mm->mm_users)
can indicate the normal process exiting.

Quoted 14 lines
         /*
          * Skip the non-shared anonymous folio mapped solely by
          * the single exiting process, and release it directly
          * in the process exiting.
          */
         if ((!atomic_read(&vma->vm_mm->mm_users) ||
             check_stable_address_space(vma->vm_mm)) &&
             folio_test_anon(folio) && folio_test_swapbacked(folio) &&
             !folio_likely_mapped_shared(folio)) {
             pra->referenced = -1;
             page_vma_mapped_walk_done(&pvmw);
             return false;
         }

Yes, + David, Willy (when you send a new version, please CC people who have participated and describe how you have addressed comments from those people.)

I also think we actually can remove "folio_test_anon(folio)".

So It could be,

@@ -883,6 +871,21 @@ static bool folio_referenced_one(struct folio *folio,

                        continue;
                }

+ /*

+                * Skip the non-shared swapbacked folio mapped solely by
+                * the exiting or OOM-reaped process. This avoids redundant
+                * swap-out followed by an immediate unmap.
+                */
+               if ((!atomic_read(&vma->vm_mm->mm_users) ||
+                    check_stable_address_space(vma->vm_mm)) &&
+                    folio_test_swapbacked(folio) &&
+                    !folio_likely_mapped_shared(folio)) {
+                       pra->referenced = -1;
+                       page_vma_mapped_walk_done(&pvmw);
+                       return false;
+               }
+
                if (pvmw.pte) {
                        if (lru_gen_enabled() &&
                            pte_young(ptep_get(pvmw.pte))) {
Quoted 33 lines

Thanks
Zhiguo

Quoted 30 lines
Quoted 29 lines
Quoted 28 lines
Quoted 24 lines
mm/vmscan.c +6 -2
index 0761f91b407f..bae7a8bf6b3d
@@ -863,7 +863,12 @@ static enum folio_references
 folio_check_references(struct folio *folio,
         if (vm_flags & VM_LOCKED)
                 return FOLIOREF_ACTIVATE;
 
-       /* rmap lock contention: rotate */
+       /*
+        * There are two cases to consider.
+        * 1) Rmap lock contention: rotate.
+        * 2) Skip the non-shared anonymous folio mapped solely by
+        *    the single exiting process.
+        */
         if (referenced_ptes == -1)
                 return FOLIOREF_KEEP;
 
--
Quoted 2 lines

2.39.0

Thanks
Barry

Thanks
Barry

#11 lore raw
Quoted 206 lines · Barry Song
Quoted 172 lines · zhiguojiang
Quoted 161 lines · Barry Song

zhiguojiang <[email protected]> 于 2024年7月9日周二 00:25写道:

     在 2024/7/8 20:17, zhiguojiang 写道:
     >
     >
     > 在 2024/7/8 19:02, Barry Song 写道:
     >> On Mon, Jul 8, 2024 at 9:04 PM Zhiguo Jiang <[email protected]>
     >> wrote:
     >>> The releasing process of the non-shared anonymous folio mapped
     >>> solely by
     >>> an exiting process may go through two flows: 1) the anonymous
     folio is
     >>> firstly is swaped-out into swapspace and transformed into a
     swp_entry
     >>> in shrink_folio_list; 2) then the swp_entry is released in the
     process
     >>> exiting flow. This will increase the cpu load of releasing a
     non-shared
     >>> anonymous folio mapped solely by an exiting process, because
     the folio
     >>> go through swap-out and the releasing the swapspace and swp_entry.
     >>>
     >>> When system is low memory, it is more likely to occur, because
     more
     >>> backend applidatuions will be killed.
     >>>
     >>> The modification is that shrink skips the non-shared anonymous
     folio
     >>> solely mapped by an exting process and the folio is only released
     >>> directly in the process exiting flow, which will save swap-out
     time
     >>> and alleviate the load of the process exiting.
     >>>
     >>> Signed-off-by: Zhiguo Jiang <[email protected]>
     >>> ---
     >>>
     >>> Change log:
     >>> v4->v5:
     >>> 1.Modify to skip non-shared anonymous folio only.
     >>> 2.Update comments for pra->referenced = -1.
     >>> v3->v4:
     >>> 1.Modify that the unshared folios mapped only in exiting task
     are skip.
     >>> v2->v3:
     >>> Nothing.
     >>> v1->v2:
     >>> 1.The VM_EXITING added in v1 patch is removed, because it will
     fail
     >>> to compile in 32-bit system.
     >>>
     >>>   mm/rmap.c   | 13 +++++++++++++
     >>>   mm/vmscan.c |  7 ++++++-
     >>>   2 files changed, 19 insertions(+), 1 deletion(-)
     >>>
     >>> diff --git a/mm/rmap.c b/mm/rmap.c
     >>> index 26806b49a86f..5b5281d71dbb
     >>> --- a/mm/rmap.c
     >>> +++ b/mm/rmap.c
     >>> @@ -843,6 +843,19 @@ static bool folio_referenced_one(struct
     folio
     >>> *folio,
     >>>          int referenced = 0;
     >>>          unsigned long start = address, ptes = 0;
     >>>
     >>> +       /*
     >>> +        * Skip the non-shared anonymous folio mapped solely by
     >>> +        * the single exiting process, and release it directly
     >>> +        * in the process exiting.
     >>> +        */
     >>> +       if ((!atomic_read(&vma->vm_mm->mm_users) ||
     >>> +               test_bit(MMF_OOM_SKIP, &vma->vm_mm->flags)) &&
     >>> +               folio_test_anon(folio) &&
     >>> folio_test_swapbacked(folio) &&
     >>> + !folio_likely_mapped_shared(folio)) {
     >>> +               pra->referenced = -1;
     >>> +               return false;
     >>> +       }
     >>> +
     >>>          while (page_vma_mapped_walk(&pvmw)) {
     >>>                  address = pvmw.address;
     > Sure, I agree with your modification suggestions. This way,
     using PTL
     > indeed sure
     > that the folio is mapped by this process.
     > Thanks
     >> As David suggested, what about the below?
     >>
     >> @@ -883,6 +870,21 @@ static bool folio_referenced_one(struct folio
     >> *folio,
     >>                          continue;
     >>                  }
     >>
     >> +               /*
     >> +                * Skip the non-shared anonymous folio mapped
     solely by
     >> +                * the single exiting process, and release it
     directly
     >> +                * in the process exiting.
     >> +                */
     >> +               if ((!atomic_read(&vma->vm_mm->mm_users) ||
     >> + test_bit(MMF_OOM_SKIP,
     >> &vma->vm_mm->flags)) &&
     >> + folio_test_anon(folio) &&
     >> folio_test_swapbacked(folio) &&
     >> + !folio_likely_mapped_shared(folio)) {
     >> +                       pra->referenced = -1;
     >> + page_vma_mapped_walk_done(&pvmw);
     >> +                       return false;
     >> +               }
     >> +
     >>                  if (pvmw.pte) {
     >>                          if (lru_gen_enabled() &&
     >> pte_young(ptep_get(pvmw.pte))) {
     >>
     >>
     >> By the way, I am not convinced that using test_bit(MMF_OOM_SKIP,
     >> &vma->vm_mm->flags) is
     >> correct (I think it is wrong).   For example, global_init can
     >> directly have it:
     >>                  if (is_global_init(p)) {
     >>                          can_oom_reap = false;
     >>                          set_bit(MMF_OOM_SKIP, &mm->flags);
     >>                          pr_info("oom killer %d (%s) has mm
     pinned by
     >> %d (%s)\n",
     >> task_pid_nr(victim),
     >> victim->comm,
     >> task_pid_nr(p), p->comm);
     >>                          continue;
     >>                  }
     >>
     >> And exit_mmap() automatically has MMF_OOM_SKIP.
     >>
     >> What is the purpose of this check? Is there a better way to
     determine
     >> if a process is an
     >> OOM target? What about check_stable_address_space() ?
     > 1.Sorry, I overlook the situation with if (is_global_init(p)),
     > MMF_OOM_SKIP is indeed not suitable.
     >
     > 2.check_stable_address_space() can indicate oom_reaper, but it
     seems
     > unable to identify the situation where the process exits normally.
     > What about task_is_dying()? static inline bool
     task_is_dying(void) {
     > return tsk_is_oom_victim(current) ||
     fatal_signal_pending(current) ||
     > (current->flags & PF_EXITING); } Thanks
     We can migrate task_is_dying() from mm/memcontrol.c to
     include/linux/oom.h
     > static inline bool task_is_dying(void)
     > {
     >     return tsk_is_oom_victim(current) ||
     fatal_signal_pending(current) ||
     >         (current->flags & PF_EXITING);
     > }
Quoted 1 line

no. current is kswapd.

Hi Barry,

It seems feasible for check_stable_address_space() replacing MMF_OOM_SKIP. check_stable_address_space() can indicate oom kill, and !atomic_read(&vma->vm_mm->mm_users)
can indicate the normal process exiting.

Quoted 14 lines
          /*
           * Skip the non-shared anonymous folio mapped solely by
           * the single exiting process, and release it directly
           * in the process exiting.
           */
          if ((!atomic_read(&vma->vm_mm->mm_users) ||
              check_stable_address_space(vma->vm_mm)) &&
              folio_test_anon(folio) && folio_test_swapbacked(folio) &&
              !folio_likely_mapped_shared(folio)) {
              pra->referenced = -1;
              page_vma_mapped_walk_done(&pvmw);
              return false;
          }

Yes, + David, Willy (when you send a new version, please CC people who have participated and describe how you have addressed comments from those people.)

I also think we actually can remove "folio_test_anon(folio)".

So It could be,

(quoted diff) +7 -0
@@ -883,6 +871,21 @@ static bool folio_referenced_one(struct folio *folio,
                         continue;
                 }
 
+               /*
+                * Skip the non-shared swapbacked folio mapped solely by
+                * the exiting or OOM-reaped process. This avoids redundant
+                * swap-out followed by an immediate unmap.
+                */
+               if ((!atomic_read(&vma->vm_mm->mm_users) ||
+                    check_stable_address_space(vma->vm_mm)) &&

Review static bool folio_referenced_one(struct folio *folio, · line 890

Quoted 10 lines
(quoted diff) +7 -0
+                    folio_test_swapbacked(folio) &&
+                    !folio_likely_mapped_shared(folio)) {
+                       pra->referenced = -1;
+                       page_vma_mapped_walk_done(&pvmw);
+                       return false;
+               }
+
                 if (pvmw.pte) {
                         if (lru_gen_enabled() &&
                             pte_young(ptep_get(pvmw.pte))) {

Ok,  update in patch v6:
https://lore.kernel.org/linux-mm/[email protected]/

Thanks

Quoted 34 lines
Quoted 32 lines

Thanks
Zhiguo

Quoted 29 lines
     >>
     >>
     >>> diff --git a/mm/vmscan.c b/mm/vmscan.c
     >>> index 0761f91b407f..bae7a8bf6b3d
     >>> --- a/mm/vmscan.c
     >>> +++ b/mm/vmscan.c
     >>> @@ -863,7 +863,12 @@ static enum folio_references
     >>> folio_check_references(struct folio *folio,
     >>>          if (vm_flags & VM_LOCKED)
     >>>                  return FOLIOREF_ACTIVATE;
     >>>
     >>> -       /* rmap lock contention: rotate */
     >>> +       /*
     >>> +        * There are two cases to consider.
     >>> +        * 1) Rmap lock contention: rotate.
     >>> +        * 2) Skip the non-shared anonymous folio mapped solely by
     >>> +        *    the single exiting process.
     >>> +        */
     >>>          if (referenced_ptes == -1)
     >>>                  return FOLIOREF_KEEP;
     >>>
     >>> --
     >>> 2.39.0
     >>>
     >> Thanks
     >> Barry
     >

Thanks
Barry