Skip to contentSkip to conversation

mm/damon/vaddr: drop redundant mmap_read_lock from damon_va_three_regions

PATCH In review Cunlong Li 3 participants 5 messages Jun 7 – Jun 8

Subsystems (2)
lore
Share & tools
lore

Cunlong Li

author
#0 lore raw

PATCH 1/1 mm/damon/vaddr: drop redundant mmap_read_lock from damon_va_three_regions

__damon_va_three_regions() already walks VMAs under rcu_read_lock(), so the outer mmap_read_lock() is no longer needed.

---

---
 mm/damon/vaddr.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
mm/damon/vaddr.c +3 -4
index b069dbc7e3d2..27e34596c43b 100644
@@ -77,7 +77,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
 	struct damon_addr_range first_gap = {0}, second_gap = {0};
 	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma, *prev = NULL;
-	unsigned long start;
+	unsigned long start = 0, last_vma_end = 0;
 
 	/*
 	 * Find the two biggest gaps so that first_gap > second_gap > others.
@@ -104,6 +104,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
 		}
 next:
 		prev = vma;
+		last_vma_end = vma->vm_end;
 	}
 	rcu_read_unlock();
 
@@ -120,7 +121,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
 	regions[1].start = ALIGN(first_gap.end, DAMON_MIN_REGION_SZ);
 	regions[1].end = ALIGN(second_gap.start, DAMON_MIN_REGION_SZ);
 	regions[2].start = ALIGN(second_gap.end, DAMON_MIN_REGION_SZ);
-	regions[2].end = ALIGN(prev->vm_end, DAMON_MIN_REGION_SZ);
+	regions[2].end = ALIGN(last_vma_end, DAMON_MIN_REGION_SZ);
 
 	return 0;
 }
@@ -140,9 +141,7 @@ static int damon_va_three_regions(struct damon_target *t,
 	if (!mm)
 		return -EINVAL;
 
-	mmap_read_lock(mm);
 	rc = __damon_va_three_regions(mm, regions);
-	mmap_read_unlock(mm);
 
 	mmput(mm);
 	return rc;

---
base-commit: 8e65320d91cdc3b241d4b94855c88459b91abf66
change-id: 20260607-rcu-24b324e0f076

Best regards,

Signature · 1 line
Cunlong Li <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Unsafe lockless VMA iteration exposes SLAB_TYPESAFE_BY_RCU freelist pointer corruption

Pre-existing issues:
- [High] Missing check_stable_address_space() validation during external mm_struct VMA iteration --

commit fc1954fba1bc736b5f26aad51449f15c4774293c
Author: Cunlong Li <[email protected]>

mm/damon/vaddr: drop redundant mmap_read_lock from damon_va_three_regions

__damon_va_three_regions() already walks VMAs under rcu_read_lock(), so the outer mmap_read_lock() is no longer needed.

mm/damon/vaddr.c static int __damon_va_three_regions(struct mm_struct *mm, · line 78

Quoted 7 lines
mm/damon/vaddr.c +0 -0
index b069dbc7e3d25..27e34596c43b8 100644
@@ -77,7 +77,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
 	struct damon_addr_range first_gap = {0}, second_gap = {0};
 	VMA_ITERATOR(vmi, mm, 0);

[Severity: High]
This isn't a bug introduced by this patch, but is it safe to iterate over the VMAs of an external mm_struct here without first checking check_stable_address_space(mm)?

If the target address space becomes unstable (for example, due to an OOM reap or a failed dup_mmap()), the maple tree may contain XA_ZERO_ENTRY markers and the mm is flagged MMF_UNSTABLE.

Without validating the address space, could the maple tree iterator return XA_ZERO_ENTRY as a VMA pointer and lead to a kernel panic when vma->vm_start is dereferenced?

Quoted 6 lines
mm/damon/vaddr.c +1 -1
 	struct vm_area_struct *vma, *prev = NULL;
-	unsigned long start;
+	unsigned long start = 0, last_vma_end = 0;
 
 	/*
 	 * Find the two biggest gaps so that first_gap > second_gap > others.

[ ... ]

mm/damon/vaddr.c static int damon_va_three_regions(struct damon_target *t, · line 145

Quoted 7 lines
mm/damon/vaddr.c +0 -2
@@ -140,9 +141,7 @@ static int damon_va_three_regions(struct damon_target *t,
 	if (!mm)
 		return -EINVAL;
 
-	mmap_read_lock(mm);
 	rc = __damon_va_three_regions(mm, regions);
-	mmap_read_unlock(mm);

[Severity: High]
Does removing this mmap_read_lock() expose the VMA iteration to SLAB_TYPESAFE_BY_RCU freelist pointer corruption?

Since the VMA slab cache is created with SLAB_TYPESAFE_BY_RCU, a VMA can be freed while an RCU reader is still accessing it. When this happens, the SLUB allocator overwrites the first word of the structure with its freelist pointer (vm_freeptr).

Because vm_freeptr is in a union with vm_start and vm_end, locklessly reading vma->vm_start from a recently freed VMA in __damon_va_three_regions() will yield an unvalidated kernel freelist pointer instead of a valid address.

Could subtracting prev->vm_end from this corrupted pointer cause massive arithmetic underflows, tricking DAMON into tracking bogus memory regions?

Quoted 4 lines
mm/damon/vaddr.c +0 -0
 	mmput(mm);
 	return rc;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
Quoted 9 lines · On Sun, 07 Jun 2026 14:00:16 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Unsafe lockless VMA iteration exposes SLAB_TYPESAFE_BY_RCU freelist pointer corruption

Pre-existing issues:
- [High] Missing check_stable_address_space() validation during external mm_struct VMA iteration --

Quoted 7 lines

commit fc1954fba1bc736b5f26aad51449f15c4774293c
Author: Cunlong Li <[email protected]>

mm/damon/vaddr: drop redundant mmap_read_lock from damon_va_three_regions

__damon_va_three_regions() already walks VMAs under rcu_read_lock(), so the outer mmap_read_lock() is no longer needed.

Is this correct? The rcu_read_lock() is for protecting only vma traversal. mmap_read_lock() is required for reading the entire VMAs of the snapshot. Am I missing something?

Quoted 14 lines

mm/damon/vaddr.c static int __damon_va_three_regions(struct mm_struct *mm, · line 78

Quoted 8 lines
mm/damon/vaddr.c +0 -0
index b069dbc7e3d25..27e34596c43b8 100644
@@ -77,7 +77,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
 	struct damon_addr_range first_gap = {0}, second_gap = {0};
 	VMA_ITERATOR(vmi, mm, 0);

[Severity: High]
This isn't a bug introduced by this patch, but is it safe to iterate over the VMAs of an external mm_struct here without first checking check_stable_address_space(mm)?

Quoted 7 lines

If the target address space becomes unstable (for example, due to an OOM reap or a failed dup_mmap()), the maple tree may contain XA_ZERO_ENTRY markers and the mm is flagged MMF_UNSTABLE.

Without validating the address space, could the maple tree iterator return XA_ZERO_ENTRY as a VMA pointer and lead to a kernel panic when vma->vm_start is dereferenced?

We hold mmap_read_lock(), so I believe it is safe. Correct me if I'm wrong.

Quoted 27 lines
Quoted 7 lines
mm/damon/vaddr.c +1 -1
 	struct vm_area_struct *vma, *prev = NULL;
-	unsigned long start;
+	unsigned long start = 0, last_vma_end = 0;
 
 	/*
 	 * Find the two biggest gaps so that first_gap > second_gap > others.

[ ... ]

mm/damon/vaddr.c static int damon_va_three_regions(struct damon_target *t, · line 145

Quoted 7 lines
mm/damon/vaddr.c +0 -2
@@ -140,9 +141,7 @@ static int damon_va_three_regions(struct damon_target *t,
 	if (!mm)
 		return -EINVAL;
 
-	mmap_read_lock(mm);
 	rc = __damon_va_three_regions(mm, regions);
-	mmap_read_unlock(mm);

[Severity: High]
Does removing this mmap_read_lock() expose the VMA iteration to SLAB_TYPESAFE_BY_RCU freelist pointer corruption?

Since the VMA slab cache is created with SLAB_TYPESAFE_BY_RCU, a VMA can be freed while an RCU reader is still accessing it. When this happens, the SLUB allocator overwrites the first word of the structure with its freelist pointer (vm_freeptr).

Quoted 7 lines

Because vm_freeptr is in a union with vm_start and vm_end, locklessly reading vma->vm_start from a recently freed VMA in __damon_va_three_regions() will yield an unvalidated kernel freelist pointer instead of a valid address.

Could subtracting prev->vm_end from this corrupted pointer cause massive arithmetic underflows, tricking DAMON into tracking bogus memory regions?

Sounds like making sense to me. Cunlong, what do you think?

Thanks,
SJ

[...]

maintainer
#3 lore raw
In reply to Cunlong Li

Hello Cunlong,

Quoted 2 lines · On Sun, 07 Jun 2026 21:48:52 +0800 Cunlong Li <[email protected]> wrote:

__damon_va_three_regions() already walks VMAs under rcu_read_lock(), so the outer mmap_read_lock() is no longer needed.

The function aims to read a snapshot of the virtual address space. So I think mmap_read_lock() is still required. Please let me know if I'm missing something.

Thanks,
SJ

[...]

author
#4 lore raw
Quoted 12 lines · SeongJae Park

Hello Cunlong,

Quoted 2 lines · On Sun, 07 Jun 2026 21:48:52 +0800 Cunlong Li <[email protected]> wrote:

__damon_va_three_regions() already walks VMAs under rcu_read_lock(), so the outer mmap_read_lock() is no longer needed.

The function aims to read a snapshot of the virtual address space. So I think mmap_read_lock() is still required. Please let me know if I'm missing something.

Quoted 4 lines

Thanks,
SJ

[...]

Yes — I understand this path is meant to obtain a snapshot of the VMA layout.

My thinking was that an exact snapshot may not be required here, and the RCU-protected traversal inside __damon_va_three_regions() already makes the walk safe, so the outer mmap_read_lock() might be redundant.

But, if we want a consistent VMA layout here, mmap_read_lock() is the right mechanism and rcu_read_lock() alone is not enough. I'll drop the patch :)

Thanks,
Cunlong