Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH 1/2] rust: mm: add VmaRef range query helpers
@ 2026-08-11  3:20 liujinlong
  0 siblings, 0 replies; 4+ messages in thread
From: liujinlong @ 2026-08-11  3:20 UTC (permalink / raw)
  To: rust-for-linux
  Cc: Miguel Ojeda, Wedson Almeida Filho, rust-for-linux, liujinlong

From: liujinlong <liujinlong@kylinos.cn>

Add helpers on VmaRef to query the virtual address span and test
address membership:

  len(), is_empty()       - byte span and zero-length check
  contains()              - single-address membership in
                           [vm_start, vm_end)
  contains_range()        - sub-range membership, returns false
                           on overflow
  is_page_aligned()       - PAGE_SIZE alignment for one address
  is_page_aligned_range() - PAGE_SIZE alignment for addr and size

is_page_aligned() and is_page_aligned_range() are associated
functions rather than methods, since they do not reference any VMA
state.  They live here instead of a separate utility module to
keep the page-alignment helpers close to the range-checking code
that often uses them.

Signed-off-by: liujinlong <liujinlong@kylinos.cn>
---
 rust/kernel/mm/virt.rs | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs
index 63eb730b0b05..5fdc3c616ef3 100644
--- a/rust/kernel/mm/virt.rs
+++ b/rust/kernel/mm/virt.rs
@@ -18,7 +18,7 @@
     bindings,
     error::{code::EINVAL, to_result, Result},
     mm::MmWithUser,
-    page::Page,
+    page::{Page, PAGE_SIZE},
     types::Opaque,
 };
 
@@ -92,6 +92,45 @@ pub fn end(&self) -> usize {
         unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_end }
     }
 
+    /// Size in bytes (`vm_end - vm_start`).
+    #[inline]
+    pub fn len(&self) -> usize {
+        self.end() - self.start()
+    }
+
+    /// True if `vm_start == vm_end`.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        self.start() == self.end()
+    }
+
+    /// True if `addr` lies in `[vm_start, vm_end)`.
+    #[inline]
+    pub fn contains(&self, addr: usize) -> bool {
+        self.start() <= addr && addr < self.end()
+    }
+
+    /// True if `[addr, addr + size)` lies in `[vm_start, vm_end)`; false on overflow.
+    #[inline]
+    pub fn contains_range(&self, addr: usize, size: usize) -> bool {
+        let Some(end) = addr.checked_add(size) else {
+            return false;
+        };
+        self.start() <= addr && end <= self.end()
+    }
+
+    /// True if `addr` is `PAGE_SIZE`-aligned.
+    #[inline]
+    pub fn is_page_aligned(addr: usize) -> bool {
+        addr % PAGE_SIZE == 0
+    }
+
+    /// True if `addr` and `size` are both page-aligned.
+    #[inline]
+    pub fn is_page_aligned_range(addr: usize, size: usize) -> bool {
+        Self::is_page_aligned(addr) && Self::is_page_aligned(size)
+    }
+
     /// Zap pages in the given page range.
     ///
     /// This clears page table mappings for the range at the leaf level, leaving all other page
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 1/2] rust: mm: add VmaRef range query helpers
@ 2026-08-11  3:26 liujinlong
  2026-08-11  3:26 ` [PATCH 2/2] rust: mm: refactor zap_vma_range() to use contains_range() liujinlong
  2026-08-11 12:12 ` [PATCH 1/2] rust: mm: add VmaRef range query helpers Alexandre Courbot
  0 siblings, 2 replies; 4+ messages in thread
From: liujinlong @ 2026-08-11  3:26 UTC (permalink / raw)
  To: rust-for-linux; +Cc: Miguel Ojeda, Wedson Almeida Filho, liujinlong

From: liujinlong <liujinlong@kylinos.cn>

Add helpers on VmaRef to query the virtual address span and test
address membership:

  len(), is_empty()       - byte span and zero-length check
  contains()              - single-address membership in
                           [vm_start, vm_end)
  contains_range()        - sub-range membership, returns false
                           on overflow
  is_page_aligned()       - PAGE_SIZE alignment for one address
  is_page_aligned_range() - PAGE_SIZE alignment for addr and size

is_page_aligned() and is_page_aligned_range() are associated
functions rather than methods, since they do not reference any VMA
state.  They live here instead of a separate utility module to
keep the page-alignment helpers close to the range-checking code
that often uses them.

Signed-off-by: liujinlong <liujinlong@kylinos.cn>
---
 rust/kernel/mm/virt.rs | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs
index 63eb730b0b05..5fdc3c616ef3 100644
--- a/rust/kernel/mm/virt.rs
+++ b/rust/kernel/mm/virt.rs
@@ -18,7 +18,7 @@
     bindings,
     error::{code::EINVAL, to_result, Result},
     mm::MmWithUser,
-    page::Page,
+    page::{Page, PAGE_SIZE},
     types::Opaque,
 };
 
@@ -92,6 +92,45 @@ pub fn end(&self) -> usize {
         unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_end }
     }
 
+    /// Size in bytes (`vm_end - vm_start`).
+    #[inline]
+    pub fn len(&self) -> usize {
+        self.end() - self.start()
+    }
+
+    /// True if `vm_start == vm_end`.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        self.start() == self.end()
+    }
+
+    /// True if `addr` lies in `[vm_start, vm_end)`.
+    #[inline]
+    pub fn contains(&self, addr: usize) -> bool {
+        self.start() <= addr && addr < self.end()
+    }
+
+    /// True if `[addr, addr + size)` lies in `[vm_start, vm_end)`; false on overflow.
+    #[inline]
+    pub fn contains_range(&self, addr: usize, size: usize) -> bool {
+        let Some(end) = addr.checked_add(size) else {
+            return false;
+        };
+        self.start() <= addr && end <= self.end()
+    }
+
+    /// True if `addr` is `PAGE_SIZE`-aligned.
+    #[inline]
+    pub fn is_page_aligned(addr: usize) -> bool {
+        addr % PAGE_SIZE == 0
+    }
+
+    /// True if `addr` and `size` are both page-aligned.
+    #[inline]
+    pub fn is_page_aligned_range(addr: usize, size: usize) -> bool {
+        Self::is_page_aligned(addr) && Self::is_page_aligned(size)
+    }
+
     /// Zap pages in the given page range.
     ///
     /// This clears page table mappings for the range at the leaf level, leaving all other page
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] rust: mm: refactor zap_vma_range() to use contains_range()
  2026-08-11  3:26 [PATCH 1/2] rust: mm: add VmaRef range query helpers liujinlong
@ 2026-08-11  3:26 ` liujinlong
  2026-08-11 12:12 ` [PATCH 1/2] rust: mm: add VmaRef range query helpers Alexandre Courbot
  1 sibling, 0 replies; 4+ messages in thread
From: liujinlong @ 2026-08-11  3:26 UTC (permalink / raw)
  To: rust-for-linux; +Cc: Miguel Ojeda, Wedson Almeida Filho, liujinlong

From: liujinlong <liujinlong@kylinos.cn>

Open-code the overflow and range check in zap_vma_range() with the
contains_range() helper added in the previous patch.  Drop two local
variables and three lines of hand-rolled arithmetic in the process.

No functional change.

Signed-off-by: liujinlong <liujinlong@kylinos.cn>
---
 rust/kernel/mm/virt.rs | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs
index 5fdc3c616ef3..363c10f976f4 100644
--- a/rust/kernel/mm/virt.rs
+++ b/rust/kernel/mm/virt.rs
@@ -153,15 +153,14 @@ pub fn is_page_aligned_range(addr: usize, size: usize) -> bool {
     /// we must only assume that the leaf level is cleared.
     #[inline]
     pub fn zap_vma_range(&self, address: usize, size: usize) {
-        let (end, did_overflow) = address.overflowing_add(size);
-        if did_overflow || address < self.start() || self.end() < end {
+        if !self.contains_range(address, size) {
             // TODO: call WARN_ONCE once Rust version of it is added
             return;
         }
 
         // SAFETY: By the type invariants, the caller has read access to this VMA, which is
         // sufficient for this method call. This method has no requirements on the vma flags. The
-        // address range is checked to be within the vma.
+        // address range is checked to be within the vma via `contains_range`.
         unsafe { bindings::zap_vma_range(self.as_ptr(), address, size) };
     }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] rust: mm: add VmaRef range query helpers
  2026-08-11  3:26 [PATCH 1/2] rust: mm: add VmaRef range query helpers liujinlong
  2026-08-11  3:26 ` [PATCH 2/2] rust: mm: refactor zap_vma_range() to use contains_range() liujinlong
@ 2026-08-11 12:12 ` Alexandre Courbot
  1 sibling, 0 replies; 4+ messages in thread
From: Alexandre Courbot @ 2026-08-11 12:12 UTC (permalink / raw)
  To: liujinlong; +Cc: rust-for-linux, Miguel Ojeda, Wedson Almeida Filho, liujinlong

On Tue Aug 11, 2026 at 12:26 PM JST, liujinlong wrote:
> From: liujinlong <liujinlong@kylinos.cn>
>
> Add helpers on VmaRef to query the virtual address span and test
> address membership:
>
>   len(), is_empty()       - byte span and zero-length check
>   contains()              - single-address membership in
>                            [vm_start, vm_end)
>   contains_range()        - sub-range membership, returns false
>                            on overflow
>   is_page_aligned()       - PAGE_SIZE alignment for one address
>   is_page_aligned_range() - PAGE_SIZE alignment for addr and size
>
> is_page_aligned() and is_page_aligned_range() are associated
> functions rather than methods, since they do not reference any VMA
> state.  They live here instead of a separate utility module to
> keep the page-alignment helpers close to the range-checking code
> that often uses them.
>
> Signed-off-by: liujinlong <liujinlong@kylinos.cn>

I guess a more useful implementation would be to just return a `Range`
of `start..end` and benefit from all its methods, which cover most of
those defined in this patch.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-11 12:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-11  3:26 [PATCH 1/2] rust: mm: add VmaRef range query helpers liujinlong
2026-08-11  3:26 ` [PATCH 2/2] rust: mm: refactor zap_vma_range() to use contains_range() liujinlong
2026-08-11 12:12 ` [PATCH 1/2] rust: mm: add VmaRef range query helpers Alexandre Courbot
  -- strict thread matches above, loose matches on Subject: below --
2026-08-11  3:20 liujinlong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox