Skip to contentSkip to conversation

mm/rmap: Add NULL checks for rmap_walk_control callbacks

PATCH In review Ye Liu 3 participants 4 messages Jun 19, 2025

Subsystems (3)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH 1/1 mm/rmap: Add NULL checks for rmap_walk_control callbacks

From: Ye Liu <[email protected]>

Add NULL pointer checks for rmap_one callback in rmap_walk operations to prevent potential NULL pointer dereferences. Also clean up some code by removing redundant comments and caching folio_nr_pages().

---
 mm/ksm.c  |  2 +-
 mm/rmap.c | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)
mm/ksm.c +1 -1
index 18b3690bb69a..22ad069d1860 100644
@@ -3068,7 +3068,7 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc)
 			if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
 				continue;
 
-			if (!rwc->rmap_one(folio, vma, addr, rwc->arg)) {
+			if (rwc->rmap_one && !rwc->rmap_one(folio, vma, addr, rwc->arg)) {
 				anon_vma_unlock_read(anon_vma);
 				return;
 			}
mm/rmap.c +7 -7
index fb63d9256f09..17d43d104a0d 100644
@@ -1202,8 +1202,7 @@ int mapping_wrprotect_range(struct address_space *mapping, pgoff_t pgoff,
 	if (!mapping)
 		return 0;
 
-	__rmap_walk_file(/* folio = */NULL, mapping, pgoff, nr_pages, &rwc,
-			 /* locked = */false);
+	__rmap_walk_file(NULL, mapping, pgoff, nr_pages, &rwc, false);
 
 	return state.cleaned;
 }
@@ -2806,6 +2805,7 @@ static void rmap_walk_anon(struct folio *folio,
 	struct anon_vma *anon_vma;
 	pgoff_t pgoff_start, pgoff_end;
 	struct anon_vma_chain *avc;
+	unsigned long nr_pages;
 
 	if (locked) {
 		anon_vma = folio_anon_vma(folio);
@@ -2817,13 +2817,13 @@ static void rmap_walk_anon(struct folio *folio,
 	if (!anon_vma)
 		return;
 
+	nr_pages = folio_nr_pages(folio);
 	pgoff_start = folio_pgoff(folio);
-	pgoff_end = pgoff_start + folio_nr_pages(folio) - 1;
+	pgoff_end = pgoff_start + nr_pages - 1;
 	anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
 			pgoff_start, pgoff_end) {
 		struct vm_area_struct *vma = avc->vma;
-		unsigned long address = vma_address(vma, pgoff_start,
-				folio_nr_pages(folio));
+		unsigned long address = vma_address(vma, pgoff_start, nr_pages);
 
 		VM_BUG_ON_VMA(address == -EFAULT, vma);
 		cond_resched();
@@ -2831,7 +2831,7 @@ static void rmap_walk_anon(struct folio *folio,
 		if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
 			continue;
 
-		if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+		if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))
 			break;
 		if (rwc->done && rwc->done(folio))
 			break;
@@ -2894,7 +2894,7 @@ static void __rmap_walk_file(struct folio *folio, struct address_space *mapping,
 		if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
 			continue;
 
-		if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+		if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))
 			goto done;
 		if (rwc->done && rwc->done(folio))
 			goto done;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

Add NULL pointer checks for rmap_one callback in rmap_walk operations to prevent potential NULL pointer dereferences. Also clean up some code by removing redundant comments and caching folio_nr_pages().

No sorry this patch is no good, none of these changes add any value.

Quoted 10 lines
---
 mm/ksm.c  |  2 +-
 mm/rmap.c | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)
mm/ksm.c +0 -0
index 18b3690bb69a..22ad069d1860 100644

mm/ksm.c void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc) · line 3071

Quoted 5 lines
mm/ksm.c +1 -1
@@ -3068,7 +3068,7 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc)
 			if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
 				continue;
 
-			if (!rwc->rmap_one(folio, vma, addr, rwc->arg)) {
+			if (rwc->rmap_one && !rwc->rmap_one(folio, vma, addr, rwc->arg)) {

It's convention that this will be set. If it's not set, a kernel developer did something wrong, so why the hell are we checking this every single time? And why if we are are we just fine to do nothing?

This is not a useful change sorry.

Quoted 8 lines
mm/ksm.c +0 -0
 				anon_vma_unlock_read(anon_vma);
 				return;
 			}
mm/rmap.c +0 -0
index fb63d9256f09..17d43d104a0d 100644

mm/rmap.c int mapping_wrprotect_range(struct address_space *mapping, pgoff_t pgoff, · line 1205

Quoted 6 lines
mm/rmap.c +1 -2
@@ -1202,8 +1202,7 @@ int mapping_wrprotect_range(struct address_space *mapping, pgoff_t pgoff,
 	if (!mapping)
 		return 0;
 
-	__rmap_walk_file(/* folio = */NULL, mapping, pgoff, nr_pages, &rwc,
-			 /* locked = */false);
+	__rmap_walk_file(NULL, mapping, pgoff, nr_pages, &rwc, false);

Please do not remove /* x = */ prefixes. This is not redundant in the slightest and aids readability so you know what's being referred to.

mm/rmap.c static void rmap_walk_anon(struct folio *folio, · line 2808

Quoted 8 lines
mm/rmap.c +1 -0
 	return state.cleaned;
 }
@@ -2806,6 +2805,7 @@ static void rmap_walk_anon(struct folio *folio,
 	struct anon_vma *anon_vma;
 	pgoff_t pgoff_start, pgoff_end;
 	struct anon_vma_chain *avc;
+	unsigned long nr_pages;

I don't think there's much value in adding this at this point to be honest. It's not expensive, the compiler knows what to do and there's two invocations.

There's a trade off with noise here.

Quoted 21 lines
mm/rmap.c +3 -3
 	if (locked) {
 		anon_vma = folio_anon_vma(folio);
@@ -2817,13 +2817,13 @@ static void rmap_walk_anon(struct folio *folio,
 	if (!anon_vma)
 		return;
 
+	nr_pages = folio_nr_pages(folio);
 	pgoff_start = folio_pgoff(folio);
-	pgoff_end = pgoff_start + folio_nr_pages(folio) - 1;
+	pgoff_end = pgoff_start + nr_pages - 1;
 	anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
 			pgoff_start, pgoff_end) {
 		struct vm_area_struct *vma = avc->vma;
-		unsigned long address = vma_address(vma, pgoff_start,
-				folio_nr_pages(folio));
+		unsigned long address = vma_address(vma, pgoff_start, nr_pages);
 
 		VM_BUG_ON_VMA(address == -EFAULT, vma);
 		cond_resched();

mm/rmap.c static void rmap_walk_anon(struct folio *folio, · line 2834

Quoted 5 lines
mm/rmap.c +1 -1
@@ -2831,7 +2831,7 @@ static void rmap_walk_anon(struct folio *folio,
 		if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
 			continue;
 
-		if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+		if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))

Same comment as with ksm.

mm/rmap.c static void __rmap_walk_file(struct folio *folio, struct address_space *mapping, · line 2897

Quoted 9 lines
mm/rmap.c +1 -1
 			break;
 		if (rwc->done && rwc->done(folio))
 			break;
@@ -2894,7 +2894,7 @@ static void __rmap_walk_file(struct folio *folio, struct address_space *mapping,
 		if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
 			continue;
 
-		if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+		if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))

Same comment as with ksm.

Quoted 6 lines
mm/rmap.c +0 -1
 			goto done;
 		if (rwc->done && rwc->done(folio))
 			goto done;
--

2.25.1

#2 lore raw
In reply to Ye Liu
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

Add NULL pointer checks for rmap_one callback in rmap_walk operations to prevent potential NULL pointer dereferences. Also clean up some code by removing redundant comments and caching folio_nr_pages().

---

Don't really see the point of this patch. The rmap_one call back will always be there as we need a way to define how to unmap/do the reverse map walk for one VMA at a time. And the folio_nr_pages() will probably get cached by the compiler anyways.

Quoted 74 lines
  mm/ksm.c  |  2 +-
  mm/rmap.c | 14 +++++++-------
  2 files changed, 8 insertions(+), 8 deletions(-)
mm/ksm.c +1 -1
index 18b3690bb69a..22ad069d1860 100644
@@ -3068,7 +3068,7 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc)
  			if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
  				continue;
 
-			if (!rwc->rmap_one(folio, vma, addr, rwc->arg)) {
+			if (rwc->rmap_one && !rwc->rmap_one(folio, vma, addr, rwc->arg)) {
  				anon_vma_unlock_read(anon_vma);
  				return;
  			}
mm/rmap.c +7 -7
index fb63d9256f09..17d43d104a0d 100644
@@ -1202,8 +1202,7 @@ int mapping_wrprotect_range(struct address_space *mapping, pgoff_t pgoff,
  	if (!mapping)
  		return 0;
 
-	__rmap_walk_file(/* folio = */NULL, mapping, pgoff, nr_pages, &rwc,
-			 /* locked = */false);
+	__rmap_walk_file(NULL, mapping, pgoff, nr_pages, &rwc, false);
 
  	return state.cleaned;
  }
@@ -2806,6 +2805,7 @@ static void rmap_walk_anon(struct folio *folio,
  	struct anon_vma *anon_vma;
  	pgoff_t pgoff_start, pgoff_end;
  	struct anon_vma_chain *avc;
+	unsigned long nr_pages;
 
  	if (locked) {
  		anon_vma = folio_anon_vma(folio);
@@ -2817,13 +2817,13 @@ static void rmap_walk_anon(struct folio *folio,
  	if (!anon_vma)
  		return;
 
+	nr_pages = folio_nr_pages(folio);
  	pgoff_start = folio_pgoff(folio);
-	pgoff_end = pgoff_start + folio_nr_pages(folio) - 1;
+	pgoff_end = pgoff_start + nr_pages - 1;
  	anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
  			pgoff_start, pgoff_end) {
  		struct vm_area_struct *vma = avc->vma;
-		unsigned long address = vma_address(vma, pgoff_start,
-				folio_nr_pages(folio));
+		unsigned long address = vma_address(vma, pgoff_start, nr_pages);
 
  		VM_BUG_ON_VMA(address == -EFAULT, vma);
  		cond_resched();
@@ -2831,7 +2831,7 @@ static void rmap_walk_anon(struct folio *folio,
  		if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
  			continue;
 
-		if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+		if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))
  			break;
  		if (rwc->done && rwc->done(folio))
  			break;
@@ -2894,7 +2894,7 @@ static void __rmap_walk_file(struct folio *folio, struct address_space *mapping,
  		if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
  			continue;
 
-		if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+		if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))
  			goto done;
  		if (rwc->done && rwc->done(folio))
  			goto done;
author
#3 lore raw

Hi Lorenzo and Dev,

Thanks for your feedback. I understand all your points and will drop this patch.

Best regards,
Ye Liu

Quoted 82 lines · Dev Jain
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

Add NULL pointer checks for rmap_one callback in rmap_walk operations to prevent potential NULL pointer dereferences. Also clean up some code by removing redundant comments and caching folio_nr_pages().

---

Don't really see the point of this patch. The rmap_one call back will always be there as we need a way to define how to unmap/do the reverse map walk for one VMA at a time. And the folio_nr_pages() will probably get cached by the compiler anyways.

Quoted 66 lines

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

  2 files changed, 8 insertions(+), 8 deletions(-)
mm/ksm.c +1 -0
index 18b3690bb69a..22ad069d1860 100644
@@ -3068,7 +3068,7 @@ void rmap_walk_ksm(struct folio *folio, struct rmap_walk_control *rwc)
               if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
                   continue;
   -            if (!rwc->rmap_one(folio, vma, addr, rwc->arg)) {
+            if (rwc->rmap_one && !rwc->rmap_one(folio, vma, addr, rwc->arg)) {
                   anon_vma_unlock_read(anon_vma);
                   return;
               }
mm/rmap.c +6 -4
index fb63d9256f09..17d43d104a0d 100644
@@ -1202,8 +1202,7 @@ int mapping_wrprotect_range(struct address_space *mapping, pgoff_t pgoff,
       if (!mapping)
           return 0;
   -    __rmap_walk_file(/* folio = */NULL, mapping, pgoff, nr_pages, &rwc,
-             /* locked = */false);
+    __rmap_walk_file(NULL, mapping, pgoff, nr_pages, &rwc, false);
         return state.cleaned;
   }
@@ -2806,6 +2805,7 @@ static void rmap_walk_anon(struct folio *folio,
       struct anon_vma *anon_vma;
       pgoff_t pgoff_start, pgoff_end;
       struct anon_vma_chain *avc;
+    unsigned long nr_pages;
         if (locked) {
           anon_vma = folio_anon_vma(folio);
@@ -2817,13 +2817,13 @@ static void rmap_walk_anon(struct folio *folio,
       if (!anon_vma)
           return;
   +    nr_pages = folio_nr_pages(folio);
       pgoff_start = folio_pgoff(folio);
-    pgoff_end = pgoff_start + folio_nr_pages(folio) - 1;
+    pgoff_end = pgoff_start + nr_pages - 1;
       anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
               pgoff_start, pgoff_end) {
           struct vm_area_struct *vma = avc->vma;
-        unsigned long address = vma_address(vma, pgoff_start,
-                folio_nr_pages(folio));
+        unsigned long address = vma_address(vma, pgoff_start, nr_pages);
             VM_BUG_ON_VMA(address == -EFAULT, vma);
           cond_resched();
@@ -2831,7 +2831,7 @@ static void rmap_walk_anon(struct folio *folio,
           if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
               continue;
   -        if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+        if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))
               break;
           if (rwc->done && rwc->done(folio))
               break;
@@ -2894,7 +2894,7 @@ static void __rmap_walk_file(struct folio *folio, struct address_space *mapping,
           if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
               continue;
   -        if (!rwc->rmap_one(folio, vma, address, rwc->arg))
+        if (rwc->rmap_one && !rwc->rmap_one(folio, vma, address, rwc->arg))
               goto done;
           if (rwc->done && rwc->done(folio))
               goto done;