Skip to contentSkip to conversation

rust: mm: warn once on invalid VMA range

PATCH In review Ivan Tverdokhleb 3 participants 3 messages Nov 26 – Dec 1

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

PATCH v3 1/1 rust: mm: warn once on invalid VMA range

Use debug_assert!() and pr_warn_once!() in
Vma::zap_vma_range() to report invalid ranges.

---
Changes in v2:
- Drop the warn_once!() macro and its KUnit test.
- Use debug_assert!() and pr_warn_once!() in Vma::zap_vma_range() as suggested by Miguel.

Changes in v3:
- Add #[cfg(CONFIG_DEBUG_VM)] in 'pr_warn_once!()' as suggested by Nicolás.

 rust/kernel/mm/virt.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
rust/kernel/mm/virt.rs +3 -1
index 63eb730b0..289511dc1 100644
@@ -115,8 +115,10 @@ pub fn end(&self) -> usize {
     #[inline]
     pub fn zap_vma_range(&self, address: usize, size: usize) {
         let (end, did_overflow) = address.overflowing_add(size);
+        debug_assert!(!did_overflow && address >= self.start() && self.end() >= end);
         if did_overflow || address < self.start() || self.end() < end {
-            // TODO: call WARN_ONCE once Rust version of it is added
+            #[cfg(CONFIG_DEBUG_VM)]
+            crate::pr_warn_once!("Attempt to zap invalid VMA range\n");
             return;
         }
 
Signature · 1 line
2.56.1
maintainer
#1 lore raw
Quoted 4 lines · Ivan Tverdokhleb

Use debug_assert!() and pr_warn_once!() in
Vma::zap_vma_range() to report invalid ranges.

(Minor nit: the struct is VmaRef, not Vma.)

rust/kernel/mm/virt.rs pub fn end(&self) -> usize { · line 123

Quoted 13 lines
rust/kernel/mm/virt.rs +3 -1
@@ -115,8 +115,10 @@ pub fn end(&self) -> usize {
     #[inline]
     pub fn zap_vma_range(&self, address: usize, size: usize) {
         let (end, did_overflow) = address.overflowing_add(size);
+        debug_assert!(!did_overflow && address >= self.start() && self.end() >= end);
         if did_overflow || address < self.start() || self.end() < end {
-            // TODO: call WARN_ONCE once Rust version of it is added
+            #[cfg(CONFIG_DEBUG_VM)]
+            crate::pr_warn_once!("Attempt to zap invalid VMA range\n");
             return;
         }

I agree with Gary's reply on v1 [1] that we actually want warn_on_once!() here rather than debug_assert!() + pr_warn_once!().

The TODO in zap_vma_range() specifically exists because we want a real WARN_ON_ONCE here. Fujita mentioned on v1 that he already has a warn_on_once!() patch that works across all architectures (including UML, ARM, and LoongArch), so I'd prefer to wait for warn_on_once!() and use that directly instead of this change.

[1] https://lore.kernel.org/r/[email protected]

Alice

#2 lore raw
Quoted 25 lines · On Mon, 30 Nov 2026 14:15:43 +0000 Alice Ryhl <[email protected]> wrote:
Quoted 4 lines · Ivan Tverdokhleb

Use debug_assert!() and pr_warn_once!() in
Vma::zap_vma_range() to report invalid ranges.

(Minor nit: the struct is VmaRef, not Vma.)

rust/kernel/mm/virt.rs pub fn end(&self) -> usize { · line 123

Quoted 13 lines
rust/kernel/mm/virt.rs +3 -1
@@ -115,8 +115,10 @@ pub fn end(&self) -> usize {
     #[inline]
     pub fn zap_vma_range(&self, address: usize, size: usize) {
         let (end, did_overflow) = address.overflowing_add(size);
+        debug_assert!(!did_overflow && address >= self.start() && self.end() >= end);
         if did_overflow || address < self.start() || self.end() < end {
-            // TODO: call WARN_ONCE once Rust version of it is added
+            #[cfg(CONFIG_DEBUG_VM)]
+            crate::pr_warn_once!("Attempt to zap invalid VMA range\n");
             return;
         }

I agree with Gary's reply on v1 [1] that we actually want warn_on_once!() here rather than debug_assert!() + pr_warn_once!().

Quoted 5 lines

The TODO in zap_vma_range() specifically exists because we want a real WARN_ON_ONCE here. Fujita mentioned on v1 that he already has a warn_on_once!() patch that works across all architectures (including UML, ARM, and LoongArch), so I'd prefer to wait for warn_on_once!() and use that directly instead of this change.

I posted warn_on_once!():

https://lore.kernel.org/rust-for-linux/[email protected]/

With the current Rust Binder, a bug in user space could trigger WARN_ON_ONCE() here. If a process unmaps the tail of its Binder mapping, the shrinker looks up the vma with inner.vma_addr and calls zap_vma_range() with an address outside the vma. I reproduced this on QEMU.

Shouldn't this be fixed first, so that only a kernel bug can trigger the warning?