Skip to contentSkip to conversation

vma: Detect infinite loop in vma tree

PATCH Applied Liam R. Howlett 5 participants 6 messages Oct 31, 2024 – Nov 1, 2024

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

PATCH v2 1/1 vma: Detect infinite loop in vma tree

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

The variance of +10 is to allow for the debugging output to be more useful for nearly correct counts. In the event of more than 10 over the map_count, the count will be set to -1 for easier identification of a potential infinite loop.

Note that the mmap lock is held to ensure a consistent tree state during the validation process.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vma.c +4 -1
index 68138e8c153e..283e6bc4884f 100644
@@ -615,7 +615,10 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count + 10) {
+			i = -1;
+			break;
+		}
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 22 lines · On Thu, 31 Oct 2024 15:36:08 -0400 "Liam R. Howlett" <[email protected]>…

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

The variance of +10 is to allow for the debugging output to be more useful for nearly correct counts. In the event of more than 10 over the map_count, the count will be set to -1 for easier identification of a potential infinite loop.

Note that the mmap lock is held to ensure a consistent tree state during the validation process.

...

+++ b/mm/vma.c

(quoted diff) +0 -0
@@ -615,7 +615,10 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);

Review void validate_mm(struct mm_struct *mm) · line 624

Quoted 10 lines
(quoted diff) +4 -1
 		}
 #endif
-		i++;
+		if (++i > mm->map_count + 10) {
+			i = -1;
+			break;
+		}
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);

It might be helpful to tell readers what's going on here?

mm/vma.c renamed from mm/vma.c~vma-detect-infinite-loop-in-vma-tree-fix +1 -0
@@ -615,6 +615,7 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
+		/* Check for a infinite loop */
 		if (++i > mm->map_count + 10) {
 			i = -1;
 			break;

_

#2 lore raw
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Thu, 31 Oct 2024 15:36:08 -0400 "Liam R. Howlett" <[email protected]>…

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

The variance of +10 is to allow for the debugging output to be more useful for nearly correct counts. In the event of more than 10 over the map_count, the count will be set to -1 for easier identification of a potential infinite loop.

Note that the mmap lock is held to ensure a consistent tree state during the validation process.

...

Quoted 15 lines

Review void validate_mm(struct mm_struct *mm) · line 624

Quoted 13 lines

+++ b/mm/vma.c

(quoted diff) +4 -1
@@ -615,7 +615,10 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count + 10) {
+			i = -1;
+			break;
+		}
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);

It might be helpful to tell readers what's going on here?

Sounds good. I guess I should have waited longer for a v2.

Quoted 13 lines
mm/vma.c renamed from mm/vma.c~vma-detect-infinite-loop-in-vma-tree-fix +1 -0
@@ -615,6 +615,7 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
+		/* Check for a infinite loop */
 		if (++i > mm->map_count + 10) {
 			i = -1;
 			break;

_

Reviewed-by on v2 1/1
#3 lore raw
In reply to Liam R. Howlett
Quoted 31 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

The variance of +10 is to allow for the debugging output to be more useful for nearly correct counts. In the event of more than 10 over the map_count, the count will be set to -1 for easier identification of a potential infinite loop.

Note that the mmap lock is held to ensure a consistent tree state during the validation process.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
  mm/vma.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
mm/vma.c +0 -0
index 68138e8c153e..283e6bc4884f 100644
@@ -615,7 +615,10 @@ void validate_mm(struct mm_struct *mm)
  			anon_vma_unlock_read(anon_vma);

mm/vma.c void validate_mm(struct mm_struct *mm) · line 624

Quoted 10 lines
mm/vma.c +4 -1
  		}
  #endif
-		i++;
+		if (++i > mm->map_count + 10) {
+			i = -1;
+			break;
+		}
  	}
  	if (i != mm->map_count) {
  		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 1/1
#4 lore raw
In reply to Liam R. Howlett
Quoted 14 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

The variance of +10 is to allow for the debugging output to be more useful for nearly correct counts. In the event of more than 10 over the map_count, the count will be set to -1 for easier identification of a potential infinite loop.

Quoted 7 lines

Note that the mmap lock is held to ensure a consistent tree state during the validation process.

Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
Quoted 20 lines
---
 mm/vma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vma.c +4 -1
index 68138e8c153e..283e6bc4884f 100644
@@ -615,7 +615,10 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count + 10) {
+			i = -1;
+			break;
+		}
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
Reviewed-by on v2 1/1
#5 lore raw
In reply to Liam R. Howlett
Quoted 10 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

There have been no reported infinite loops in the tree, but checking the detection of an infinite loop during validation is simple enough. Add the detection to the validate_mm() function so that error reports are clear and don't just report stalls.

This does not protect against internal maple tree issues, but it does detect too many vmas being returned from the tree.

Quoted 8 lines

The variance of +10 is to allow for the debugging output to be more useful for nearly correct counts. In the event of more than 10 over the map_count, the count will be set to -1 for easier identification of a potential infinite loop.

Note that the mmap lock is held to ensure a consistent tree state during the validation process.

Quoted 26 lines
Cc Lorenzo Stoakes <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Jann Horn <[email protected]>
---
 mm/vma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vma.c +4 -2
index 68138e8c153e..283e6bc4884f 100644
@@ -615,7 +615,10 @@ void validate_mm(struct mm_struct *mm)
 			anon_vma_unlock_read(anon_vma);
 		}
 #endif
-		i++;
+		if (++i > mm->map_count + 10) {
+			i = -1;
+			break;
+		}
 	}
 	if (i != mm->map_count) {
 		pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
--

2.43.0