Skip to contentSkip to conversation

mm/rmap: vm_flags including VM_EXEC can exit timely

PATCH In review Zhiguo Jiang 2 participants 6 messages Oct 24, 2023 – Oct 26, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/rmap: vm_flags including VM_EXEC can exit timely

When pra->vm_flags include VM_EXEC flag and folio is file detected in folio_referenced_one(), the folio referenced traversal process can be exited timely to reduce the detecting folio referenced time.

---
 mm/rmap.c | 2 ++
 1 file changed, 2 insertions(+)
 mode change 100644 => 100755 mm/rmap.c
mm/rmap.c +2 -0
index 7a27a2b41802..932f3b7e8521
@@ -884,6 +884,8 @@ static bool folio_referenced_one(struct folio *folio,
 	if (referenced) {
 		pra->referenced++;
 		pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+		if ((pra->vm_flags | VM_EXEC) && folio_is_file_lru(folio))
+			return false;
 	}
 
 	if (!pra->mapcount)
Signature · 1 line
2.39.0
#1 lore raw
Quoted 4 lines · Zhiguo Jiang

When pra->vm_flags include VM_EXEC flag and folio is file detected in folio_referenced_one(), the folio referenced traversal process can be exited timely to reduce the detecting folio referenced time.

Can you further elaborate what the logic behind that is?

Why can we stop here if we're dealing with a pagecache folio in an executable VMA?

Quoted 19 lines
---
  mm/rmap.c | 2 ++
  1 file changed, 2 insertions(+)

mode change 100644 => 100755 mm/rmap.c

mm/rmap.c +2 -0
index 7a27a2b41802..932f3b7e8521
@@ -884,6 +884,8 @@ static bool folio_referenced_one(struct folio *folio,
  	if (referenced) {
  		pra->referenced++;
  		pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+		if ((pra->vm_flags | VM_EXEC) && folio_is_file_lru(folio))
+			return false;
  	}
 
  	if (!pra->mapcount)
Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 10 lines · David Hildenbrand
Quoted 4 lines · Zhiguo Jiang

When pra->vm_flags include VM_EXEC flag and folio is file detected in folio_referenced_one(), the folio referenced traversal process can be exited timely to reduce the detecting folio referenced time.

Can you further elaborate what the logic behind that is?

Why can we stop here if we're dealing with a pagecache folio in an executable VMA?

Functions call flow:folio_referenced() --> rmap_walk() --> rmap_walk_ksm()/rmap_walk_anon()/rmap_walk_file() --> rwc->rmap_one() --> folio_referenced_one(). And folio_referenced() is called by two interfaces: folio_check_references() and shrink_active_list().

1. folio_check_references():
When (referenced_ptes > 0 && (vm_flags & VM_EXEC) && folio_is_file_lru(folio)) is detected in folio_check_references(), FOLIOREF_ACTIVATE will be returned and the folio will be added to the active file lru. So when VM_EXEC is detected in folio_referenced_one(), we can stop continuing to detect the reference relationship between this folio and other vmas, and exit directly to avoid unnecessary traversal.

2. shrink_active_list():
The shrink_active_list() is the same as the folio_check_references().

Quoted 20 lines
Quoted 19 lines

---
  mm/rmap.c | 2 ++

  1 file changed, 2 insertions(+)

  mode change 100644 => 100755 mm/rmap.c

mm/rmap.c +2 -0
index 7a27a2b41802..932f3b7e8521
@@ -884,6 +884,8 @@ static bool folio_referenced_one(struct folio 
 *folio,
       if (referenced) {
           pra->referenced++;
           pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+        if ((pra->vm_flags | VM_EXEC) && folio_is_file_lru(folio))
+            return false;
       }
         if (!pra->mapcount)
#3 lore raw
Quoted 29 lines · zhiguojiang
Quoted 10 lines · David Hildenbrand
Quoted 4 lines · Zhiguo Jiang

When pra->vm_flags include VM_EXEC flag and folio is file detected in folio_referenced_one(), the folio referenced traversal process can be exited timely to reduce the detecting folio referenced time.

Can you further elaborate what the logic behind that is?

Why can we stop here if we're dealing with a pagecache folio in an executable VMA?

Functions call flow:folio_referenced() --> rmap_walk() --> rmap_walk_ksm()/rmap_walk_anon()/rmap_walk_file() --> rwc->rmap_one() --> folio_referenced_one(). And folio_referenced() is called by two
interfaces: folio_check_references() and shrink_active_list().

1. folio_check_references():
When (referenced_ptes > 0 && (vm_flags & VM_EXEC) && folio_is_file_lru(folio)) is detected in folio_check_references(), FOLIOREF_ACTIVATE will be returned and the folio will be added to the active file lru. So when VM_EXEC is detected in folio_referenced_one(), we can stop continuing to detect the reference relationship between this folio and other vmas, and exit directly to avoid unnecessary traversal.

2. shrink_active_list():
The shrink_active_list() is the same as the folio_check_references().

Quoted 6 lines
Quoted 6 lines

---
  mm/rmap.c | 2 ++

  1 file changed, 2 insertions(+)

  mode change 100644 => 100755 mm/rmap.c

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
mm/rmap.c +0 -0
index 7a27a2b41802..932f3b7e8521
@@ -884,6 +884,8 @@ static bool folio_referenced_one(struct folio 
Quoted 8 lines

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

Quoted 8 lines
mm/rmap.c +2 -0
 *folio,
       if (referenced) {
           pra->referenced++;
           pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+        if ((pra->vm_flags | VM_EXEC) && folio_is_file_lru(folio))
+            return false;
       }
         if (!pra->mapcount)

Sorry, Patch mistake in writing, patch should be:
+        if ((pra->vm_flags & VM_EXEC) && folio_is_file_lru(folio)) +            return false;

Quoted 2 lines
Quoted 1 line
#4 lore raw
Quoted 31 lines · zhiguojiang
Quoted 28 lines · zhiguojiang
Quoted 11 lines · David Hildenbrand
Quoted 4 lines · Zhiguo Jiang

When pra->vm_flags include VM_EXEC flag and folio is file detected in folio_referenced_one(), the folio referenced traversal process can be exited timely to reduce the detecting folio referenced time.

Can you further elaborate what the logic behind that is?

Why can we stop here if we're dealing with a pagecache folio in an executable VMA?

Functions call flow:folio_referenced() --> rmap_walk() --> rmap_walk_ksm()/rmap_walk_anon()/rmap_walk_file() --> rwc->rmap_one() --> folio_referenced_one(). And folio_referenced() is called by two
interfaces: folio_check_references() and shrink_active_list().

1. folio_check_references():
When (referenced_ptes > 0 && (vm_flags & VM_EXEC) && folio_is_file_lru(folio)) is detected in folio_check_references(), FOLIOREF_ACTIVATE will be returned and the folio will be added to the active file lru. So when VM_EXEC is detected in folio_referenced_one(), we can stop continuing to detect the reference relationship between this folio and other vmas, and exit directly to avoid unnecessary traversal.

Quoted 3 lines
Quoted 3 lines

2. shrink_active_list():
The shrink_active_list() is the same as the folio_check_references().

Thanks, that all belongs into the patch description in some condensed form.

Should that "(vm_flags & VM_EXEC) && folio_is_file_lru(folio)" in all three places somehow be factored out in a function with a suitable name, so all these cases can be identified easily? Just a thought.

Then, add a comment to the code you're adding. There are plenty of comments for the other two cases you mentioned.

folio_referenced() documents:

"Quick test_and_clear_referenced for all mappings of a folio"

IIUC, you're code will stop doing that, as you break in the middle of processing some mappings, but not all.

Please describe why that is okay and add it to the patch description and update the function description.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
---
   mm/rmap.c | 2 ++
   1 file changed, 2 insertions(+)

  mode change 100644 => 100755 mm/rmap.c

Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
mm/rmap.c +0 -0
index 7a27a2b41802..932f3b7e8521
@@ -884,6 +884,8 @@ static bool folio_referenced_one(struct folio
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
mm/rmap.c +2 -0
 *folio,
       if (referenced) {
           pra->referenced++;
           pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+        if ((pra->vm_flags | VM_EXEC) && folio_is_file_lru(folio))
+            return false;
       }
         if (!pra->mapcount)

Sorry, Patch mistake in writing, patch should be:
+        if ((pra->vm_flags & VM_EXEC) && folio_is_file_lru(folio)) +            return false;

So this was not even properly tested? :/

Of course I have to ask: what's the net (performance) benefit of this change?

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw
Quoted 43 lines · David Hildenbrand
Quoted 31 lines · zhiguojiang
Quoted 28 lines · zhiguojiang
Quoted 11 lines · David Hildenbrand
Quoted 4 lines · Zhiguo Jiang

When pra->vm_flags include VM_EXEC flag and folio is file detected in folio_referenced_one(), the folio referenced traversal process can be exited timely to reduce the detecting folio referenced time.

Can you further elaborate what the logic behind that is?

Why can we stop here if we're dealing with a pagecache folio in an executable VMA?

Functions call flow:folio_referenced() --> rmap_walk() --> rmap_walk_ksm()/rmap_walk_anon()/rmap_walk_file() --> rwc->rmap_one() --> folio_referenced_one(). And folio_referenced() is called by two
interfaces: folio_check_references() and shrink_active_list().

1. folio_check_references():
When (referenced_ptes > 0 && (vm_flags & VM_EXEC) && folio_is_file_lru(folio)) is detected in folio_check_references(), FOLIOREF_ACTIVATE will be returned and the folio will be added to the active file lru. So when VM_EXEC is detected in folio_referenced_one(), we can stop continuing to detect the reference relationship between this folio and other vmas, and exit directly to avoid unnecessary traversal.

Quoted 3 lines
Quoted 3 lines

2. shrink_active_list():
The shrink_active_list() is the same as the folio_check_references().

Thanks, that all belongs into the patch description in some condensed form.

Should that "(vm_flags & VM_EXEC) && folio_is_file_lru(folio)" in all three places somehow be factored out in a function with a suitable name, so all these cases can be identified easily? Just a thought.

Then, add a comment to the code you're adding. There are plenty of comments for the other two cases you mentioned.

Quoted 7 lines

folio_referenced() documents:

    "Quick test_and_clear_referenced for all mappings of a folio"

IIUC, you're code will stop doing that, as you break in the middle of processing some mappings, but not all.

Thank you for review, this patch will indeed interrupt test_and_clear_referenced for all mappings of a folio. This patch can be ignore.

Quoted 29 lines

Please describe why that is okay and add it to the patch description and update the function description.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

---
   mm/rmap.c | 2 ++

   1 file changed, 2 insertions(+)

   mode change 100644 => 100755 mm/rmap.c

Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
mm/rmap.c +0 -0
index 7a27a2b41802..932f3b7e8521
@@ -884,6 +884,8 @@ static bool folio_referenced_one(struct folio
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
mm/rmap.c +2 -0
 *folio,
        if (referenced) {
            pra->referenced++;
            pra->vm_flags |= vma->vm_flags & ~VM_LOCKED;
+        if ((pra->vm_flags | VM_EXEC) && folio_is_file_lru(folio))
+            return false;
        }
          if (!pra->mapcount)

Sorry, Patch mistake in writing, patch should be:
+        if ((pra->vm_flags & VM_EXEC) && folio_is_file_lru(folio)) +            return false;

So this was not even properly tested? :/

Of course I have to ask: what's the net (performance) benefit of this change?