* [PATCH 1/2] drm/gpuvm: add name(), RawGpuVa and kernel_alloc_va() for debugfs
2026-03-17 12:03 [PATCH 0/2] drm/gpuvm: add debugfs utilities for GPU VA state dumping Alvin Sun
@ 2026-03-17 12:03 ` Alvin Sun
2026-03-17 12:03 ` [PATCH 2/2] drm/gpuvm: add GpuVaIter and va_mappings() " Alvin Sun
1 sibling, 0 replies; 3+ messages in thread
From: Alvin Sun @ 2026-03-17 12:03 UTC (permalink / raw)
To: Danilo Krummrich, Matthew Brost, Thomas Hellström,
Alice Ryhl, David Airlie, Simona Vetter, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross
Cc: dri-devel, rust-for-linux, Alvin Sun
Expose GPU VM name and kernel-reserved VA so drivers can dump GPU VA
state in debugfs.
Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
---
rust/kernel/drm/gpuvm/mod.rs | 14 ++++++++++++++
rust/kernel/drm/gpuvm/va.rs | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/rust/kernel/drm/gpuvm/mod.rs b/rust/kernel/drm/gpuvm/mod.rs
index 20e512842dfc6..3186df9f740cf 100644
--- a/rust/kernel/drm/gpuvm/mod.rs
+++ b/rust/kernel/drm/gpuvm/mod.rs
@@ -165,6 +165,13 @@ pub fn as_raw(&self) -> *mut bindings::drm_gpuvm {
self.vm.get()
}
+ /// Returns the name of this GpuVm.
+ #[inline]
+ pub fn name(&self) -> &CStr {
+ // SAFETY: The `name` field is immutable and points to a NUL-terminated string.
+ unsafe { CStr::from_char_ptr((*self.as_raw()).name) }
+ }
+
/// The start of the VA space.
#[inline]
pub fn va_start(&self) -> u64 {
@@ -188,6 +195,13 @@ pub fn va_range(&self) -> Range<u64> {
Range { start, end }
}
+ /// Returns the kernel-allocated VA for this GpuVm.
+ #[inline]
+ pub fn kernel_alloc_va(&self) -> &RawGpuVa {
+ // SAFETY: The `self.as_raw()` is guaranteed to be a valid pointer to a drm_gpuvm.
+ unsafe { RawGpuVa::from_raw(&raw mut (*self.as_raw()).kernel_alloc_node) }
+ }
+
/// Get or create the [`GpuVmBo`] for this gem object.
#[inline]
pub fn obtain(
diff --git a/rust/kernel/drm/gpuvm/va.rs b/rust/kernel/drm/gpuvm/va.rs
index a31122ff22282..8b8fd500b3c5f 100644
--- a/rust/kernel/drm/gpuvm/va.rs
+++ b/rust/kernel/drm/gpuvm/va.rs
@@ -81,6 +81,43 @@ pub fn vm_bo(&self) -> &GpuVmBo<T> {
}
}
+/// Represents that a range of a GEM object is mapped in the kernel.
+#[repr(transparent)]
+pub struct RawGpuVa(Opaque<bindings::drm_gpuva>);
+
+impl RawGpuVa {
+ /// Access this [`RawGpuVa`] from a raw pointer.
+ ///
+ /// # Safety
+ ///
+ /// For the duration of `'a`, the pointer must reference a valid `drm_gpuva`.
+ #[inline]
+ pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuva) -> &'a Self {
+ // SAFETY: `drm_gpuva` and `RawGpuVa` have the same layout.
+ unsafe { &*(ptr.cast()) }
+ }
+
+ /// Returns a raw pointer to underlying C value.
+ #[inline]
+ pub fn as_raw(&self) -> *mut bindings::drm_gpuva {
+ self.0.get()
+ }
+
+ /// Returns the address of this mapping in the GPU virtual address space.
+ #[inline]
+ pub fn addr(&self) -> u64 {
+ // SAFETY: `self.as_raw()` is guaranteed to be a valid pointer to a `drm_gpuva`.
+ unsafe { (*self.as_raw()).va.addr }
+ }
+
+ /// Returns the length of this mapping.
+ #[inline]
+ pub fn length(&self) -> u64 {
+ // SAFETY: `self.as_raw()` is guaranteed to be a valid pointer to a `drm_gpuva`.
+ unsafe { (*self.as_raw()).va.range }
+ }
+}
+
/// A pre-allocated [`GpuVa`] object.
///
/// # Invariants
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] drm/gpuvm: add GpuVaIter and va_mappings() for debugfs
2026-03-17 12:03 [PATCH 0/2] drm/gpuvm: add debugfs utilities for GPU VA state dumping Alvin Sun
2026-03-17 12:03 ` [PATCH 1/2] drm/gpuvm: add name(), RawGpuVa and kernel_alloc_va() for debugfs Alvin Sun
@ 2026-03-17 12:03 ` Alvin Sun
1 sibling, 0 replies; 3+ messages in thread
From: Alvin Sun @ 2026-03-17 12:03 UTC (permalink / raw)
To: Danilo Krummrich, Matthew Brost, Thomas Hellström,
Alice Ryhl, David Airlie, Simona Vetter, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross
Cc: dri-devel, rust-for-linux, Alvin Sun
Add an iterator over VA mappings so drivers can walk and dump GPU VA
state in debugfs.
Signed-off-by: Alvin Sun <alvin.sun@linux.dev>
---
rust/kernel/drm/gpuvm/mod.rs | 6 +++++
rust/kernel/drm/gpuvm/va.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/rust/kernel/drm/gpuvm/mod.rs b/rust/kernel/drm/gpuvm/mod.rs
index 3186df9f740cf..41b99d468f9d9 100644
--- a/rust/kernel/drm/gpuvm/mod.rs
+++ b/rust/kernel/drm/gpuvm/mod.rs
@@ -301,6 +301,12 @@ pub fn data(&mut self) -> &mut T {
// SAFETY: By the type invariants we may access `core`.
unsafe { &mut *self.0.data.get() }
}
+
+ /// Returns an iterator over the VA mappings in this GpuVm.
+ #[inline]
+ pub fn va_mappings(&self) -> GpuVaIter<'_, T> {
+ GpuVaIter::new(&self.0)
+ }
}
impl<T: DriverGpuVm> Deref for GpuVmCore<T> {
diff --git a/rust/kernel/drm/gpuvm/va.rs b/rust/kernel/drm/gpuvm/va.rs
index 8b8fd500b3c5f..3210047f880e3 100644
--- a/rust/kernel/drm/gpuvm/va.rs
+++ b/rust/kernel/drm/gpuvm/va.rs
@@ -1,6 +1,14 @@
// SPDX-License-Identifier: GPL-2.0 OR MIT
use super::*;
+use crate::interop::list::{
+ CList,
+ CListIter, //
+};
+use core::{
+ marker::PhantomData,
+ mem::offset_of, //
+};
/// Represents that a range of a GEM object is mapped in this [`GpuVm`] instance.
///
@@ -118,6 +126,51 @@ pub fn length(&self) -> u64 {
}
}
+const DRM_GPUVA_LIST_ENTRY_OFFSET: usize = offset_of!(bindings::drm_gpuva, rb.entry);
+type RawGpuVaIter<'a> = CListIter<'a, RawGpuVa, DRM_GPUVA_LIST_ENTRY_OFFSET>;
+
+/// An iterator over the VA mappings in a [`GpuVm`].
+pub struct GpuVaIter<'a, T: DriverGpuVm> {
+ raw_gpuva_iter: RawGpuVaIter<'a>,
+ kern_gpuva_ptr: *mut bindings::drm_gpuva,
+ _marker: PhantomData<&'a T>,
+}
+
+impl<'a, T: DriverGpuVm> GpuVaIter<'a, T> {
+ #[inline]
+ pub(crate) fn new(gpuvm: &'a GpuVm<T>) -> Self {
+ // SAFETY: `gpuvm` is valid.
+ let head = unsafe { &raw mut (*gpuvm.as_raw()).rb.list };
+ // SAFETY: `head` is a valid pointer to a drm_gpuva list head.
+ let clist = unsafe { CList::<RawGpuVa, DRM_GPUVA_LIST_ENTRY_OFFSET>::from_raw(head) };
+ let kern_gpuva_ptr = gpuvm.kernel_alloc_va().as_raw();
+
+ Self {
+ raw_gpuva_iter: clist.iter(),
+ kern_gpuva_ptr,
+ _marker: PhantomData,
+ }
+ }
+}
+
+impl<'a, T: DriverGpuVm> Iterator for GpuVaIter<'a, T> {
+ type Item = &'a GpuVa<T>;
+
+ #[inline]
+ fn next(&mut self) -> Option<Self::Item> {
+ let mut curr = self.raw_gpuva_iter.next()?;
+
+ if curr.as_raw() == self.kern_gpuva_ptr {
+ // Skip kernel reserved node.
+ curr = self.raw_gpuva_iter.next()?;
+ }
+
+ // SAFETY: We have skipped the kernel reserved node, all remaining
+ // entries are valid GpuVa<T> instances.
+ Some(unsafe { GpuVa::from_raw(curr.as_raw()) })
+ }
+}
+
/// A pre-allocated [`GpuVa`] object.
///
/// # Invariants
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread