[PATCH v4 2/5] rust: mm: add VmaRef::size() and VmaRef::pgoff()
From: Simon Song
Date: Fri Nov 27 2026 - 17:50:28 EST
The forthcoming VFIO PCI abstraction needs VMA page offsets and byte
lengths for mapping bounds checks. Keeping these queries on VmaRef
confines unsafe field access to the MM abstraction and avoids repeated
length calculations in callers.
Suggested-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Signed-off-by: Simon Song <xinmengs@xxxxxxxxxx>
---
rust/kernel/mm/virt.rs | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs
index 63eb730b0b055..b0210869b757a 100644
--- a/rust/kernel/mm/virt.rs
+++ b/rust/kernel/mm/virt.rs
@@ -92,6 +92,20 @@ pub fn end(&self) -> usize {
unsafe { (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.vm_end }
}
+ /// Returns the size of the virtual memory area in bytes.
+ #[inline]
+ pub fn size(&self) -> usize {
+ self.end() - self.start()
+ }
+
+ /// Returns the VMA's page offset (`vm_pgoff`).
+ #[inline]
+ pub fn pgoff(&self) -> usize {
+ // SAFETY: By `VmaRef`'s invariants, the VMA is valid and the mmap/VMA read lock prevents
+ // concurrent modification.
+ unsafe { (*self.as_ptr()).vm_pgoff }
+ }
+
/// Zap pages in the given page range.
///
/// This clears page table mappings for the range at the leaf level, leaving all other page
--
2.43.7