On Thu Mar 26, 2026 at 7:53 AM CET, Alvin Sun wrote:
> Add name(), refcount(), is_imported(), is_exported(), and mmap_offset()
> to BaseObject so drivers can expose GEM state in debugfs.
>
> Signed-off-by: Alvin Sun <[email protected]>
> ---
> rust/kernel/drm/gem/mod.rs | 49
> +++++++++++++++++++++++++++++++++++++++++++---
> 1 file changed, 46 insertions(+), 3 deletions(-)
>
> diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> index 276ba3c53475d..b6188c8873ece 100644
> --- a/rust/kernel/drm/gem/mod.rs
> +++ b/rust/kernel/drm/gem/mod.rs
> @@ -21,9 +21,13 @@
> },
> error::to_result,
> prelude::*,
> - sync::aref::{
> - ARef,
> - AlwaysRefCounted, //
> + sync::{
> + aref::{
> + ARef,
> + AlwaysRefCounted, //
> + },
> + atomic::Relaxed,
> + Refcount, //
> },
> types::Opaque,
> };
> @@ -177,6 +181,45 @@ fn size(&self) -> usize {
> unsafe { (*self.as_raw()).size }
> }
>
> + /// Returns the name of the object.
> + #[inline]
> + fn name(&self) -> i32 {
> + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid
> `struct drm_gem_object`.
> + unsafe { (*self.as_raw()).name }
> + }
> +
> + /// Returns the reference count of the object.
> + #[inline]
> + fn refcount(&self) -> u32 {
> + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid
> `struct drm_gem_object`.
> + let raw_refcount = unsafe { &raw mut (*self.as_raw()).refcount
> }.cast::<Refcount>();
> + // SAFETY: `raw_refcount` has the same layout as `Refcount`.
> + let refcount = unsafe { &*raw_refcount };
> +
> + refcount.as_atomic().load(Relaxed) as u32
> + }
How is this reference count useful in debugfs? In any case, please don't expose
object reference counts, it motivates abuse.
Can't we just prove a common Debug impl for GEM objects instead of providing all
those accessors?
> +
> + /// Returns true if the object is imported.
> + #[inline]
> + fn is_imported(&self) -> bool {
> + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid
> `struct drm_gem_object`.
> + !unsafe { (*self.as_raw()).import_attach }.is_null()
> + }
> +
> + /// Returns true if the object is exported.
> + #[inline]
> + fn is_exported(&self) -> bool {
> + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid
> `struct drm_gem_object`.
> + !unsafe { (*self.as_raw()).dma_buf }.is_null()
> + }
> +
> + /// Returns the offset for mmap, or 0 if no offset has been allocated.
> + #[inline]
> + fn mmap_offset(&self) -> u64 {
> + // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid
> `struct drm_gem_object`.
> + unsafe { (*self.as_raw()).vma_node.vm_node.start }
> + }
> +
> /// Creates a new handle for the object associated with a given `File`
> /// (or returns an existing one).
> fn create_handle<D, F>(&self, file: &drm::File<F>) -> Result<u32>
>
> --
> 2.43.0