Skip to contentSkip to conversation

drm/tyr: add debugfs support

PATCH Superseded Alvin Sun 6 participants 27 messages Mar 26 – May 25

Subsystems (6)
DRM drivers Rust RCU DRM Rust +2 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alvin Sun

author
#0 lore raw

cover drm/tyr: add debugfs support

This patchset contains:
- hazptr: Add hazard pointer abstraction to kernel::sync
- revocable: Introduce HazPtrRevocable, and adapt Gary Guo's downstream LazyRevocable implementation to use hazard pointers internally, enabling safe sharing of debugfs-root data across the tyr module and DRM driver
- drm: Add device and GEM helper functions to support debugfs
- tyr: Add debugfs infrastructure and interfaces to expose GEM/VM information

See the full dependency and commit history at [1].

---
Alvin Sun (11):
rust: sync: set_once: Rename InitError variants to fix clippy warning
rust: sync: add hazard pointer abstraction
rust: revocable: add HazPtrRevocable
rust: revocable: make LazyRevocable use HazPtrRevocable
rust: drm: add Device::primary_index()
rust: drm/gem: add GEM object query helpers for debugfs
rust: drm/gem/shmem: add resident_size() and madv() for debugfs drm/tyr: expose Vm gpuvm_core, gpuvm and va_range as pub(crate) drm/tyr: add debugfs infrastructure
drm/tyr: add vms and gpuvas debugfs interface
drm/tyr: add gems field and gems debugfs interface

Gary Guo (2):
rust: sync: support [pin_]init for SetOnce
rust: revocable: add lazily instantiated revocable variant

 drivers/gpu/drm/tyr/debugfs.rs  | 190 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tyr/driver.rs   |  18 +++-
 drivers/gpu/drm/tyr/fw.rs       |   7 +-
 drivers/gpu/drm/tyr/mmu.rs      |   6 +-
 drivers/gpu/drm/tyr/tyr.rs      |  39 +++++++-
 drivers/gpu/drm/tyr/vm.rs       |   6 +-
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/hazptr.c           |  13 +++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/drm/device.rs       |  10 ++
 rust/kernel/drm/gem/mod.rs      |  49 +++++++++-
 rust/kernel/drm/gem/shmem.rs    |  21 ++++
 rust/kernel/revocable.rs        | 209 +++++++++++++++++++++++++++++++++++++++-
 rust/kernel/sync.rs             |   1 +
 rust/kernel/sync/hazptr.rs      |  91 +++++++++++++++++
 rust/kernel/sync/set_once.rs    | 120 +++++++++++++++++++----
 16 files changed, 746 insertions(+), 36 deletions(-)

---
base-commit: 34cb4f916af10153c87fabaf6c34e4cafa170427
change-id: 20260320-b4-tyr-debugfs-3fcaff064297
prerequisite-change-id: 20260202-io-81fd368f7565:v2
prerequisite-patch-id: 74873afed2b65f5a11c33f0c0c54375c55079109
prerequisite-patch-id: ae853e8eb8d58c77881371960be4ae92755e83c6
prerequisite-patch-id: 0ab78b50648c7d8f66b83c32ed2af0ec3ede42a3
prerequisite-patch-id: 8d20a8db8cf4660c682ee91f3db04e640c144e33
prerequisite-patch-id: 299de9cd2789c19c22e2816f7c817d80d5a4f1db
prerequisite-patch-id: 661ee334905f359daa8fb8d808ed5f4a8085f5c9
prerequisite-change-id: 20260117-register-ccaba1d21713:v9
prerequisite-patch-id: e4cb34ef09bccb92e1c35435e346b3a0459d3cdf
prerequisite-patch-id: 2c3c42ae9f3f4544c088dffbe4a0527f28b1c135
prerequisite-patch-id: 9f40e176e1b67bada943e376950debabb3f196d5
prerequisite-patch-id: ccc86d3ab9f62883c90d1275c99720942bd2bcb3
prerequisite-patch-id: 216427f1fdf94755cca0ed0676c4290f85d45e5c
prerequisite-patch-id: 7e1ea4d184297dfaba1351a7cb23e3b6827fed46
prerequisite-patch-id: 29ff9ffe37669d23b5509bfefb0cc1f8950eaa7c
prerequisite-patch-id: e5723722ff970c585cbf559456fa4cda05f28952
prerequisite-patch-id: afdae76e12011bb158d2008b8cd1ffba084db0bb
prerequisite-patch-id: b0473d5c7af0bbeec6f6ec8dd36d8a09cc180fed
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 454c4fe7a7bcc65b2c9cc69dd5dd8e4c1d952297
prerequisite-patch-id: aff529cc205f5ab35518b8e4568e6e1319eecaa9
prerequisite-patch-id: 9f7d5c4a8ed052f5c5f2a28c862a9392d7a812cc
prerequisite-patch-id: ec9d104ba7c67488f19e6dd1a50d1ec4f682f333
prerequisite-patch-id: e7a844bd6254643d1f23827886b487c196ee9968
prerequisite-patch-id: 3c57ebf13497cf57c586b955d5ce90f2f9fd8cdc
prerequisite-patch-id: 1a327345ac71599ece0b2272fba81472f70c568c
prerequisite-change-id: 20251128-gpuvm-rust-b719cac27ad6:v5
prerequisite-patch-id: 6a8ddd47a2e1c769c65a942b3d9315e6ae5cae7e
prerequisite-patch-id: d4ee9fb80025c4fd8129c3477cc3dc36e13076cb
prerequisite-patch-id: 0ad35cd3dfcf8a8804e9fed8df3e5612cf07da69
prerequisite-patch-id: 275a900645e14169911ee00bab54553c84b48ad0
prerequisite-patch-id: 2a2de2f6c4fac065de5c459026daa2c32f340d22
prerequisite-patch-id: 9a721bc700da778f2554cf32917dda4807744520
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 6c622d1df9166ad081acba46237f8cecbf728356
prerequisite-patch-id: 769892e477c161e6c7a57519ec635b1455310f1d
prerequisite-patch-id: 3c5f5070ded87e95a8045083a55501fbbe9f6313
prerequisite-patch-id: 10fbd8695264e67421946dda4d39e29d5fe77c64

Best regards,

Signature · 1 line
Alvin Sun <[email protected]>
author
#1 lore raw

PATCH 1/13 rust: sync: support [pin_]init for `SetOnce`

From: Gary Guo <[email protected]>

Make SetOnce support initialization via impl Init or impl PinInit.

This adds a possible failing path if an initializer fails. In such case, the state is dropped back to 0 instead of keep increasing; so the monotonicity invariant is dropped. The order for initialization is upgraded from Relaxed to Acquire so it can observe the effect of a previously failed initialization.

---
 rust/kernel/sync/set_once.rs | 120 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 99 insertions(+), 21 deletions(-)
rust/kernel/sync/set_once.rs +99 -21
index 139cef05e935f..db9c5423fade3 100644
@@ -2,11 +2,23 @@
 
 //! A container that can be initialized at most once.
 
+use core::{
+    cell::UnsafeCell,
+    mem::MaybeUninit, //
+};
+use pin_init::{
+    Init,
+    PinInit, //
+};
+
 use super::atomic::{
-    ordering::{Acquire, Relaxed, Release},
-    Atomic,
+    ordering::{
+        Acquire,
+        Release, //
+    },
+    Atomic, //
 };
-use core::{cell::UnsafeCell, mem::MaybeUninit};
+use crate::prelude::*;
 
 /// A container that can be populated at most once. Thread safe.
 ///
@@ -15,13 +27,13 @@
 ///
 /// # Invariants
 ///
-/// - `init` may only increase in value.
 /// - `init` may only assume values in the range `0..=2`.
 /// - `init == 0` if and only if `value` is uninitialized.
 /// - `init == 1` if and only if there is exactly one thread with exclusive
 ///   access to `self.value`.
 /// - `init == 2` if and only if `value` is initialized and valid for shared
 ///   access.
+/// - once `init == 2`, it must remain so.
 ///
 /// # Example
 ///
@@ -51,6 +63,35 @@ fn default() -> Self {
     }
 }
 
+/// Error that can occur during initialization of `SetOnce`.
+#[derive(Debug)]
+pub enum InitError<E> {
+    /// The `Once` has already been initialized.
+    AlreadyInit,
+    /// The `Once` is being raced to initialize by another thread.
+    RacedInit,
+    /// Error occurs during initialization.
+    DuringInit(E),
+}
+
+impl<E> From<E> for InitError<E> {
+    #[inline]
+    fn from(err: E) -> Self {
+        InitError::DuringInit(err)
+    }
+}
+
+impl<E: Into<Error>> From<InitError<E>> for Error {
+    #[inline]
+    fn from(this: InitError<E>) -> Self {
+        match this {
+            InitError::AlreadyInit => EEXIST,
+            InitError::RacedInit => EBUSY,
+            InitError::DuringInit(e) => e.into(),
+        }
+    }
+}
+
 impl<T> SetOnce<T> {
     /// Create a new [`SetOnce`].
     ///
@@ -76,31 +117,68 @@ pub fn as_ref(&self) -> Option<&T> {
         }
     }
 
-    /// Populate the [`SetOnce`].
+    /// Populate the [`SetOnce`] with an initializer.
     ///
-    /// Returns `true` if the [`SetOnce`] was successfully populated.
-    pub fn populate(&self, value: T) -> bool {
+    /// Returns the initialized reference if the [`SetOnce`] was successfully populated.
+    pub fn init<E>(&self, init: impl Init<T, E>) -> Result<&T, InitError<E>> {
         // INVARIANT: If the swap succeeds:
-        //  - We increase `init`.
         //  - We write the valid value `1` to `init`.
+        //  - The previous value is not `2`, so it is valid to move to `1`.
         //  - Only one thread can succeed in this write, so we have exclusive access after the
         //    write.
-        if let Ok(0) = self.init.cmpxchg(0, 1, Relaxed) {
-            // SAFETY: By the type invariants of `Self`, the fact that we succeeded in writing `1`
-            // to `self.init` means we obtained exclusive access to `self.value`.
-            unsafe { core::ptr::write(self.value.get().cast(), value) };
-            // INVARIANT:
-            //  - We increase `init`.
-            //  - We write the valid value `2` to `init`.
-            //  - We release our exclusive access to `self.value` and it is now valid for shared
-            //    access.
-            self.init.store(2, Release);
-            true
-        } else {
-            false
+        match self.init.cmpxchg(0, 1, Acquire) {
+            Ok(_) => {
+                // SAFETY:
+                // - By the type invariants of `Self`, the fact that we succeeded in writing `1`
+                //   to `self.init` means we obtained exclusive access to `self.value`.
+                // - When `Err` is returned, we did not set `self.init` to `2` so the `Drop` is not
+                //   armed.
+                match unsafe { init.__init(self.value.get().cast()) } {
+                    Ok(()) => {
+                        // INVARIANT:
+                        //  - The previous value is `1`, so it is valid to move to `2`.
+                        //  - We write the valid value `2` to `init`.
+                        //  - We release our exclusive access to `self.value` and it is now valid
+                        //    for shared access.
+                        self.init.store(2, Release);
+                        // SAFETY: we have just initialized the value.
+                        Ok(unsafe { &*self.value.get().cast() })
+                    }
+                    Err(err) => {
+                        // INVARIANT:
+                        //  - The previous value is `1`, so it is valid to move to `0`.
+                        //  - We write the valid value `0` to `init`.
+                        //  - We release our exclusive access to `self.value` and it is now valid
+                        //    for shared access.
+                        self.init.store(0, Release);
+                        Err(err.into())
+                    }
+                }
+            }
+            Err(1) => Err(InitError::RacedInit),
+            Err(_) => Err(InitError::AlreadyInit),
         }
     }
 
+    /// Populate the [`SetOnce`] with a pinned initializer.
+    ///
+    /// Returns the initialized reference if the [`SetOnce`] was successfully populated.
+    pub fn pin_init<E>(self: Pin<&Self>, init: impl PinInit<T, E>) -> Result<&T, InitError<E>> {
+        // SAFETY:
+        // - `__pinned_init` satisfy all requirements of `init_from_closure`
+        // - calling `__pinned_init` require additional that the slot is pinned, which is satisfied
+        //   given `self: Pin<&Self>`.
+        self.get_ref()
+            .init(unsafe { pin_init::init_from_closure(|slot| init.__pinned_init(slot)) })
+    }
+
+    /// Populate the [`SetOnce`].
+    ///
+    /// Returns `true` if the [`SetOnce`] was successfully populated.
+    pub fn populate(&self, value: T) -> bool {
+        self.init(value).is_ok()
+    }
+
     /// Get a copy of the contained object.
     ///
     /// Returns [`None`] if the [`SetOnce`] is empty.
Signature · 1 line
2.43.0
#2 lore raw

Re: [01/13] rust: sync: support [pin_]init for `SetOnce`

Quoted 181 lines · Alvin Sun

From: Gary Guo <[email protected]>

Make SetOnce support initialization via impl Init or impl PinInit.

This adds a possible failing path if an initializer fails. In such case,
the state is dropped back to 0 instead of keep increasing; so the monotonicity
invariant is dropped. The order for initialization is upgraded from Relaxed
to Acquire so it can observe the effect of a previously failed initialization.

---
 rust/kernel/sync/set_once.rs | 120
+++++++++++++++++++++++++++++++++++--------

 1 file changed, 99 insertions(+), 21 deletions(-)
rust/kernel/sync/set_once.rs +0 -0
index 139cef05e935f..db9c5423fade3 100644
@@ -2,11 +2,23 @@


 //! A container that can be initialized at most once.

rust/kernel/sync/set_once.rs +9 -0
+use core::{
+    cell::UnsafeCell,
+    mem::MaybeUninit, //
+};
+use pin_init::{
+    Init,
+    PinInit, //
+};
+

 use super::atomic::{
-    ordering::{Acquire, Relaxed, Release},
-    Atomic,
+    ordering::{
+        Acquire,
+        Release, //
+    },
+    Atomic, //
 };

rust/kernel/sync/set_once.rs +1 -1
-use core::{cell::UnsafeCell, mem::MaybeUninit};
+use crate::prelude::*;


 /// A container that can be populated at most once. Thread safe. ///

rust/kernel/sync/set_once.rs +0 -1
@@ -15,13 +27,13 @@
  ///
  /// # Invariants
  ///
-/// - `init` may only increase in value.
  /// - `init` may only assume values in the range `0..=2`.
  /// - `init == 0` if and only if `value` is uninitialized.
  /// - `init == 1` if and only if there is exactly one thread with

exclusive
 ///   access to self.value.
 /// - init == 2 if and only if value is initialized and valid for shared
 ///   access.
+/// - once init == 2, it must remain so.
 ///
 /// # Example
 ///

rust/kernel/sync/set_once.rs +0 -0
@@ -51,6 +63,35 @@ fn default() -> Self {
      }
  }

rust/kernel/sync/set_once.rs +29 -0
+/// Error that can occur during initialization of `SetOnce`.
+#[derive(Debug)]
+pub enum InitError<E> {
+    /// The `Once` has already been initialized.
+    AlreadyInit,
+    /// The `Once` is being raced to initialize by another thread.
+    RacedInit,
+    /// Error occurs during initialization.
+    DuringInit(E),
+}
+
+impl<E> From<E> for InitError<E> {
+    #[inline]
+    fn from(err: E) -> Self {
+        InitError::DuringInit(err)
+    }
+}
+
+impl<E: Into<Error>> From<InitError<E>> for Error {
+    #[inline]
+    fn from(this: InitError<E>) -> Self {
+        match this {
+            InitError::AlreadyInit => EEXIST,
+            InitError::RacedInit => EBUSY,
+            InitError::DuringInit(e) => e.into(),
+        }
+    }
+}
+

 impl<T> SetOnce<T> {
     /// Create a new [SetOnce].
     ///

rust/kernel/sync/set_once.rs +0 -0
@@ -76,31 +117,68 @@ pub fn as_ref(&self) -> Option<&T> {
          }
      }


-    /// Populate the [SetOnce].
+    /// Populate the [SetOnce] with an initializer.
     ///
-    /// Returns true if the [SetOnce] was successfully populated.
-    pub fn populate(&self, value: T) -> bool {
+    /// Returns the initialized reference if the [SetOnce] was successfully populated.
+    pub fn init<E>(&self, init: impl Init<T, E>) -> Result<&T, InitError<E>> {
         // INVARIANT: If the swap succeeds:
-        //  - We increase init.
         //  - We write the valid value 1 to init.
+        //  - The previous value is not 2, so it is valid to move to 1.
         //  - Only one thread can succeed in this write, so we have exclusive access after the
         //    write.
-        if let Ok(0) = self.init.cmpxchg(0, 1, Relaxed) { -            // SAFETY: By the type invariants of Self, the fact that we succeeded in writing 1
-            // to self.init means we obtained exclusive access to self.value.
-            unsafe { core::ptr::write(self.value.get().cast(), value) };
-            // INVARIANT:
-            //  - We increase init.
-            //  - We write the valid value 2 to init. -            //  - We release our exclusive access to self.value and it is now valid for shared
-            //    access.
-            self.init.store(2, Release);
-            true
-        } else {
-            false
+        match self.init.cmpxchg(0, 1, Acquire) {
+            Ok(_) => {
+                // SAFETY:
+                // - By the type invariants of Self, the fact that we succeeded in writing 1
+                //   to self.init means we obtained exclusive access to self.value.
+                // - When Err is returned, we did not set self.init to 2 so the Drop is not
+                //   armed.
+                match unsafe { init.__init(self.value.get().cast()) } {
+                    Ok(()) => {
+                        // INVARIANT:
+                        //  - The previous value is 1, so it is valid to move to 2.
+                        //  - We write the valid value 2 to init.
+                        //  - We release our exclusive access to self.value and it is now valid
+                        //    for shared access.
+                        self.init.store(2, Release);
+                        // SAFETY: we have just initialized the value.
+                        Ok(unsafe { &*self.value.get().cast() }) +                    }
+                    Err(err) => {
+                        // INVARIANT:
+                        //  - The previous value is 1, so it is valid to move to 0.
+                        //  - We write the valid value 0 to init.
+                        //  - We release our exclusive access to self.value and it is now valid
+                        //    for shared access.
+                        self.init.store(0, Release);

Quoted 8 lines

+                        Err(err.into())
+                    }
+                }
+            }
+            Err(1) => Err(InitError::RacedInit), +            Err(_) => Err(InitError::AlreadyInit), }
     }

So: I've been struggling quite a bit trying to understand what the purpose of this is. The main problem is I don't see how callers are actually supposed to make use of this. Or at the very least, maybe this type should have a more appropriate name because the places it can be used are a lot more limited then they appear.

Assume for a moment that we have two threads, each of them notices that a SetOnce is empty and attempts to initialize it. This patch would of course make it so that one thread successfully initializes it, and the other gets InitError::RacedInit or InitError::AlreadyInit depending on luck.

Consider the following function I tried writing, where self.sg_res is SetOnce<Devres<SGTableMap<T>>>:

    fn get_sg_table<'a>(
        &'a self,
        dev: &'a device::Device<Bound>,
    ) -> Result<&'a Devres<SGTableMap<T>>> {
        if dev.as_raw() != self.dev().as_ref().as_raw() {
            return Err(EINVAL);
        }

        if let Some(sgt_res) = self.sgt_res.as_ref() {
            return Ok(sgt_res);
        }

        // SAFETY:
        // * `slot` only has a single field, which is initialized in this callback
        // * If any of the steps in this closure fail, we return Err(err)
        // * It is perfectly fine to move a Devres around after initialization
        // * T has no pinning variants to uphold.
        let init = unsafe {
            init_from_closure(|sgt_res| {
                *sgt_res = Devres::new(dev, SGTableMap::new(self))?;
                Ok(())
            })
        };

        match self.sgt_res.init(init) {
            Ok(res) => Ok(res),
            Err(InitError::DuringInit(e)) => Err(e),
            Err(InitError::AlreadyInit) => {
                // SAFETY: We know that `sgt_res` is populated now, so its safe to unwrap
                Ok(unsafe { self.sgt_res.as_ref().unwrap_unchecked() })
            }
            // We need to busy-wait until the other thread finishes init
            Err(InitError::RacedInit) => loop {
		// XXX: The below line will not work for reasons explained
		// later in this email
                if let Some(sgt_res) = self.sgt_res.as_ref() {
                    break Ok(sgt_res);
                }
            }, // (retry)
        }

There's a few issues. First, the most obvious one: there doesn't actually appear to be any way of actually handling raced initialization without busy-looping.

The other problem, which I mentioned in an XXX: comment in the above snipping, is that busy-waiting isn't even possible unless you busy-wait on .init() instead of .as_ref(). .init() just returns Some(…) or None, which means that if self.sgt_res.populate(…) actually fails during initialization we'll just busy-wait forever because we keep getting None.

So, the only correct way of busy-waiting is repeatedly calling .init() with a value until we get InitError::AlreadyInit, and then finally calling as_ref() to retrieve the value that got stored.

I have to assume that this is probably not the way that this API is intended to be used and as such, maybe this needs to be renamed or we need to rethink this a bit?

For what it's worth, I'm also looking at potentially making a thread- safe single-init container that avoids this issue entirely by just using a Mutex to protect initialization. Which would look something like this (I hate this naming, part of the reason I wrote this email :):

#[pin_data]
struct InitOnce<T: Send + Sync, E> {
#[pin]
inner: Mutex<Option<Result<T, E>>>
}

where the basic idea is that the Mutex is only actually used for protecting the initial population of the contents of Option<Result<T, E>>, and the actual contents of the mutex can be shared outside of the critical section. Soundness would be enforced on the latter condition by making it so that, similar to SetOnce, you can only really write to it a single time.

Quoted 29 lines


+    /// Populate the [SetOnce] with a pinned initializer. +    ///
+    /// Returns the initialized reference if the [SetOnce] was successfully populated.
+    pub fn pin_init<E>(self: Pin<&Self>, init: impl PinInit<T, E>) -

Quoted 1 line

Result<&T, InitError<E>> {

+        // SAFETY:
+        // - __pinned_init satisfy all requirements of init_from_closure
+        // - calling __pinned_init require additional that the slot is pinned, which is satisfied
+        //   given self: Pin<&Self>.
+        self.get_ref()
+            .init(unsafe { pin_init::init_from_closure(|slot| init.__pinned_init(slot)) })
+    }
+
+    /// Populate the [SetOnce].
+    ///
+    /// Returns true if the [SetOnce] was successfully populated.
+    pub fn populate(&self, value: T) -> bool {
+        self.init(value).is_ok()
+    }
+
     /// Get a copy of the contained object.
     ///
     /// Returns [None] if the [SetOnce] is empty.

author
#3 lore raw

PATCH 2/13 rust: revocable: add lazily instantiated revocable variant

In reply to Alvin Sun

From: Gary Guo <[email protected]>

Add a LazyRevocable type, an variant to Revocable where the data is initialized lazily. This type can be constructed as const and put into statics.

---
 rust/kernel/revocable.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)
rust/kernel/revocable.rs +79 -1
index 0f4ae673256d5..70733ff5961cd 100644
@@ -7,7 +7,12 @@
 
 use pin_init::Wrapper;
 
-use crate::{bindings, prelude::*, sync::rcu, types::Opaque};
+use crate::{
+    bindings,
+    prelude::*,
+    sync::{rcu, SetOnce},
+    types::Opaque,
+};
 use core::{
     marker::PhantomData,
     ops::Deref,
@@ -261,3 +266,76 @@ fn deref(&self) -> &Self::Target {
         unsafe { &*self.data_ref }
     }
 }
+
+/// A handle to perform revocation on a [`Revocable`].
+///
+/// The associated `Revocable` is revoked when the handle is dropped.
+pub struct RevokeHandle<'a, T>(&'a Revocable<T>);
+
+impl<'a, T> RevokeHandle<'a, T> {
+    /// Create a revoke-on-drop handle on an existing revocable.
+    pub fn new(revocable: &'a Revocable<T>) -> Self {
+        Self(revocable)
+    }
+
+    /// Dismiss the handle.
+    ///
+    /// This method consumes ownership without revoking the `Revocable`.
+    pub fn dismiss(self) {
+        core::mem::forget(self);
+    }
+}
+
+impl<'a, T> Drop for RevokeHandle<'a, T> {
+    fn drop(&mut self) {
+        self.0.revoke();
+    }
+}
+
+/// An object that is initialized and can become inaccessible at runtime.
+///
+/// [`Revocable`] is initialized at the beginning, and can be made inaccessible at runtime.
+/// `LazyRevocable` is uninitialized at the beginning, and can be initialized later; it can be
+/// revoked in a similar manner to [`Revocable`]. Once revoked, it cannot be initialized again.
+#[pin_data]
+pub struct LazyRevocable<T> {
+    #[pin]
+    once: SetOnce<Revocable<T>>,
+}
+
+impl<T> LazyRevocable<T> {
+    /// Creates a new lazy revocable instance.
+    ///
+    /// The instance starts uninitialized, where all accesses would fail.
+    pub const fn new() -> Self {
+        LazyRevocable {
+            once: SetOnce::new(),
+        }
+    }
+
+    /// Initialize a `LazyRevocable` and obtain a handle to revoke the content if successful.
+    ///
+    /// An error would be returned if the revocable has already been initialized.
+    pub fn init<E: Into<Error>>(
+        self: Pin<&Self>,
+        init: impl PinInit<T, E>,
+    ) -> Result<RevokeHandle<'_, T>> {
+        // SAFETY: `once` is structurally pinned.
+        let once = unsafe { self.map_unchecked(|x| &x.once) };
+        let revocable = once.pin_init(Revocable::new(init))?;
+        Ok(RevokeHandle::new(revocable))
+    }
+
+    /// Tries to access the revocable wrapped object.
+    ///
+    /// Returns `None` if the object has not been initialized, or it has been revoked and is therefore no longer accessible.
+    pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
+        self.once.as_ref()?.try_access()
+    }
+}
+
+impl<T> Default for LazyRevocable<T> {
+    fn default() -> Self {
+        Self::new()
+    }
+}
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 3/13 rust: sync: set_once: Rename InitError variants to fix clippy warning

In reply to Alvin Sun

Fixes clippy warning:
warning: all variants have the same postfix: Init
--> rust/kernel/sync/set_once.rs:68:1

---
 rust/kernel/sync/set_once.rs | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
rust/kernel/sync/set_once.rs +9 -9
index db9c5423fade3..3af5538aae1d4 100644
@@ -67,17 +67,17 @@ fn default() -> Self {
 #[derive(Debug)]
 pub enum InitError<E> {
     /// The `Once` has already been initialized.
-    AlreadyInit,
+    Already,
     /// The `Once` is being raced to initialize by another thread.
-    RacedInit,
+    Raced,
     /// Error occurs during initialization.
-    DuringInit(E),
+    Inner(E),
 }
 
 impl<E> From<E> for InitError<E> {
     #[inline]
     fn from(err: E) -> Self {
-        InitError::DuringInit(err)
+        InitError::Inner(err)
     }
 }
 
@@ -85,9 +85,9 @@ impl<E: Into<Error>> From<InitError<E>> for Error {
     #[inline]
     fn from(this: InitError<E>) -> Self {
         match this {
-            InitError::AlreadyInit => EEXIST,
-            InitError::RacedInit => EBUSY,
-            InitError::DuringInit(e) => e.into(),
+            InitError::Already => EEXIST,
+            InitError::Raced => EBUSY,
+            InitError::Inner(e) => e.into(),
         }
     }
 }
@@ -155,8 +155,8 @@ pub fn init<E>(&self, init: impl Init<T, E>) -> Result<&T, InitError<E>> {
                     }
                 }
             }
-            Err(1) => Err(InitError::RacedInit),
-            Err(_) => Err(InitError::AlreadyInit),
+            Err(1) => Err(InitError::Raced),
+            Err(_) => Err(InitError::Already),
         }
     }
 
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH 03/13] rust: sync: set_once: Rename InitError variants to fix clippy warning

Quoted 3 lines · Alvin Sun

Fixes clippy warning:
warning: all variants have the same postfix: Init
--> rust/kernel/sync/set_once.rs:68:1

I am not sure that this makes the code look nicer :/

AlreadyInit is very clear on what's wrong, which InitError::Already is weird.

Perhaps just allow this warning.

Best,
Gary

Quoted 56 lines
---
 rust/kernel/sync/set_once.rs | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
rust/kernel/sync/set_once.rs +9 -9
index db9c5423fade3..3af5538aae1d4 100644
@@ -67,17 +67,17 @@ fn default() -> Self {
 #[derive(Debug)]
 pub enum InitError<E> {
     /// The `Once` has already been initialized.
-    AlreadyInit,
+    Already,
     /// The `Once` is being raced to initialize by another thread.
-    RacedInit,
+    Raced,
     /// Error occurs during initialization.
-    DuringInit(E),
+    Inner(E),
 }
 
 impl<E> From<E> for InitError<E> {
     #[inline]
     fn from(err: E) -> Self {
-        InitError::DuringInit(err)
+        InitError::Inner(err)
     }
 }
 
@@ -85,9 +85,9 @@ impl<E: Into<Error>> From<InitError<E>> for Error {
     #[inline]
     fn from(this: InitError<E>) -> Self {
         match this {
-            InitError::AlreadyInit => EEXIST,
-            InitError::RacedInit => EBUSY,
-            InitError::DuringInit(e) => e.into(),
+            InitError::Already => EEXIST,
+            InitError::Raced => EBUSY,
+            InitError::Inner(e) => e.into(),
         }
     }
 }
@@ -155,8 +155,8 @@ pub fn init<E>(&self, init: impl Init<T, E>) -> Result<&T, InitError<E>> {
                     }
                 }
             }
-            Err(1) => Err(InitError::RacedInit),
-            Err(_) => Err(InitError::AlreadyInit),
+            Err(1) => Err(InitError::Raced),
+            Err(_) => Err(InitError::Already),
         }
     }
 
author
#6 lore raw

Re: [PATCH 03/13] rust: sync: set_once: Rename InitError variants to fix clippy warning

Quoted 9 lines · Gary Guo
Quoted 3 lines · Alvin Sun

Fixes clippy warning:
warning: all variants have the same postfix: Init
--> rust/kernel/sync/set_once.rs:68:1

I am not sure that this makes the code look nicer :/

AlreadyInit is very clear on what's wrong, which InitError::Already is weird.

Perhaps just allow this warning.

Got it, that makes more sense. I was actually a bit confused by this clippy warning when I first saw it too, wasn't sure whether to just follow it or not. Now I know what to do - I'll drop this patch and allow the warning directly.

Best regards,
Alvin

Quoted 59 lines

Best,
Gary

Quoted 55 lines
---
  rust/kernel/sync/set_once.rs | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
rust/kernel/sync/set_once.rs +9 -9
index db9c5423fade3..3af5538aae1d4 100644
@@ -67,17 +67,17 @@ fn default() -> Self {
  #[derive(Debug)]
  pub enum InitError<E> {
      /// The `Once` has already been initialized.
-    AlreadyInit,
+    Already,
      /// The `Once` is being raced to initialize by another thread.
-    RacedInit,
+    Raced,
      /// Error occurs during initialization.
-    DuringInit(E),
+    Inner(E),
  }
 
  impl<E> From<E> for InitError<E> {
      #[inline]
      fn from(err: E) -> Self {
-        InitError::DuringInit(err)
+        InitError::Inner(err)
      }
  }
 
@@ -85,9 +85,9 @@ impl<E: Into<Error>> From<InitError<E>> for Error {
      #[inline]
      fn from(this: InitError<E>) -> Self {
          match this {
-            InitError::AlreadyInit => EEXIST,
-            InitError::RacedInit => EBUSY,
-            InitError::DuringInit(e) => e.into(),
+            InitError::Already => EEXIST,
+            InitError::Raced => EBUSY,
+            InitError::Inner(e) => e.into(),
          }
      }
  }
@@ -155,8 +155,8 @@ pub fn init<E>(&self, init: impl Init<T, E>) -> Result<&T, InitError<E>> {
                      }
                  }
              }
-            Err(1) => Err(InitError::RacedInit),
-            Err(_) => Err(InitError::AlreadyInit),
+            Err(1) => Err(InitError::Raced),
+            Err(_) => Err(InitError::Already),
          }
      }
 
#7 lore raw

Re: [PATCH 03/13] rust: sync: set_once: Rename InitError variants to fix clippy warning

In reply to Alvin Sun
Quoted 4 lines · Alvin Sun

Fixes clippy warning:
warning: all variants have the same postfix: Init
--> rust/kernel/sync/set_once.rs:68:1

Is this fixing an issue earlier in the series?

If so, then I would suggest just fixing the patch that introduced the issue directly (since you are not the author, you can use square brackets between the Signed-off-bys to indicate what you changed. I agree with Gary that is may be simpler to just allow the warning.

If not, then please add a Fixes: tag pointing to the commit being fixed.

(By the way, if you do keep this patch, then please copy-paste more of the warning, i.e. it doesn't say what lint is being triggered. In addition, the imperative mood is what is expected in commit messages for the kernel.)

Thanks!

Cheers,
Miguel

author
#8 lore raw

Re: [PATCH 03/13] rust: sync: set_once: Rename InitError variants to fix clippy warning

Quoted 10 lines · Miguel Ojeda
Quoted 3 lines · Alvin Sun

Fixes clippy warning:
warning: all variants have the same postfix: Init
--> rust/kernel/sync/set_once.rs:68:1

Is this fixing an issue earlier in the series?

If so, then I would suggest just fixing the patch that introduced the issue directly (since you are not the author, you can use square brackets between the Signed-off-bys to indicate what you changed. I agree with Gary that is may be simpler to just allow the warning.

Agreed. This clippy warning already exists in Gary's original patch. I'll fix it there by allowing the warning directly, and add a square-bracket note between the Signed-off-bys describing the change.

Thanks,
Alvin

Quoted 12 lines

If not, then please add a Fixes: tag pointing to the commit being fixed.

(By the way, if you do keep this patch, then please copy-paste more of the warning, i.e. it doesn't say what lint is being triggered. In addition, the imperative mood is what is expected in commit messages for the kernel.)

Thanks!

Cheers,
Miguel

author
#9 lore raw

PATCH 4/13 rust: sync: add hazard pointer abstraction

In reply to Alvin Sun

Add C helpers and Rust wrappers for the kernel hazard pointer API, to be used by revocable and other code.

---
 rust/bindings/bindings_helper.h |  1 +
 rust/helpers/hazptr.c           | 13 ++++++
 rust/helpers/helpers.c          |  1 +
 rust/kernel/sync.rs             |  1 +
 rust/kernel/sync/hazptr.rs      | 91 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 107 insertions(+)
rust/bindings/bindings_helper.h +1 -0
index 9058b09a016ec..8ace50a3dc104 100644
@@ -60,6 +60,7 @@
 #include <linux/file.h>
 #include <linux/firmware.h>
 #include <linux/fs.h>
+#include <linux/hazptr.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/io-pgtable.h>
rust/helpers/hazptr.c new file +13 -0
new file mode 100644
index 0000000000000..aa53fbcd0da1d
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/hazptr.h>
+
+__rust_helper void *rust_helper_hazptr_acquire(struct hazptr_ctx *ctx, void *const *addr_p)
+{
+	return hazptr_acquire(ctx, addr_p);
+}
+
+__rust_helper void rust_helper_hazptr_release(struct hazptr_ctx *ctx, void *addr)
+{
+	hazptr_release(ctx, addr);
+}
rust/helpers/helpers.c +1 -0
index f1ac40b0bd1a4..ed8cb74d394a3 100644
@@ -34,6 +34,7 @@
 #include "err.c"
 #include "irq.c"
 #include "fs.c"
+#include "hazptr.c"
 #include "io.c"
 #include "jump_label.c"
 #include "kunit.c"
rust/kernel/sync.rs +1 -0
index 993dbf2caa0e3..45da761eaee05 100644
@@ -15,6 +15,7 @@
 pub mod barrier;
 pub mod completion;
 mod condvar;
+pub mod hazptr;
 pub mod lock;
 mod locked_by;
 pub mod poll;
rust/kernel/sync/hazptr.rs new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#10 lore raw

PATCH 5/13 rust: revocable: add HazPtrRevocable

In reply to Alvin Sun

Add hazard-pointer-based revocable type and related handle/guard.

---
 rust/kernel/revocable.rs | 127 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 124 insertions(+), 3 deletions(-)
rust/kernel/revocable.rs +124 -3
index 70733ff5961cd..eabb76ce92c43 100644
@@ -10,14 +10,25 @@
 use crate::{
     bindings,
     prelude::*,
-    sync::{rcu, SetOnce},
+    sync::{
+        hazptr,
+        hazptr::HazptrCtx,
+        rcu,
+        SetOnce, //
+    },
     types::Opaque,
 };
 use core::{
     marker::PhantomData,
     ops::Deref,
-    ptr::drop_in_place,
-    sync::atomic::{AtomicBool, Ordering},
+    ptr::{
+        addr_of,
+        drop_in_place, //
+    },
+    sync::atomic::{
+        AtomicBool,
+        Ordering, //
+    },
 };
 
 /// An object that can become inaccessible at runtime.
@@ -292,6 +303,116 @@ fn drop(&mut self) {
     }
 }
 
+/// Revocable protected by hazard pointer instead of RCU.
+#[pin_data(PinnedDrop)]
+pub struct HazPtrRevocable<T> {
+    #[pin]
+    data: Opaque<T>,
+    is_available: AtomicBool,
+}
+
+// SAFETY: HazPtrRevocable<T> only moves ownership of T across threads;
+// revocation/drop follow the hazptr protocol, so T: Send suffices.
+unsafe impl<T: Send> Send for HazPtrRevocable<T> {}
+
+// SAFETY: &HazPtrRevocable<T> may be shared across threads and yields
+// &T via hazptr guards; with T: Send + Sync such shared access is sound.
+unsafe impl<T: Sync + Send> Sync for HazPtrRevocable<T> {}
+
+impl<T> HazPtrRevocable<T> {
+    /// Creates a new hazard-pointer revocable instance.
+    pub fn new<E>(data_pin_init: impl PinInit<T, E>) -> impl PinInit<Self, E> {
+        try_pin_init!(Self {
+            data <- Opaque::pin_init(data_pin_init),
+            is_available: AtomicBool::new(true),
+        }? E)
+    }
+
+    /// Tries to access the wrapped object. Returns `None` if revoked.
+    ///
+    /// `ctx` is moved into the returned guard and released when the guard is dropped.
+    pub fn try_access<'a>(
+        &self,
+        ctx: Pin<&'a mut HazptrCtx>,
+    ) -> Option<HazPtrRevocableGuard<'a, T>> {
+        let data_ptr = self.data.get();
+        let guard = hazptr::acquire(ctx, addr_of!(data_ptr).cast())?;
+        if !self.is_available.load(Ordering::Relaxed) {
+            return None;
+        }
+        Some(HazPtrRevocableGuard::new(guard))
+    }
+
+    /// Revokes access and drops the wrapped object. Waits for readers via hazptr.
+    pub fn revoke(&self) -> bool {
+        let revoke = self.is_available.swap(false, Ordering::Relaxed);
+        if revoke {
+            hazptr::synchronize(self.data.get() as usize);
+            // SAFETY: `synchronize()` ensures no reader still holds the pointer,
+            // and `self.is_available` is false so no new reader can start, so
+            // `drop_in_place` is safe.
+            unsafe { drop_in_place(self.data.get()) };
+        }
+        revoke
+    }
+}
+
+#[pinned_drop]
+impl<T> PinnedDrop for HazPtrRevocable<T> {
+    fn drop(self: Pin<&mut Self>) {
+        // Drop only if the data hasn't been revoked yet (in which case it has already been
+        // dropped).
+        // SAFETY: We are not moving out of `p`, only dropping in place
+        let p = unsafe { self.get_unchecked_mut() };
+        if *p.is_available.get_mut() {
+            // SAFETY: We know `self.data` is valid because no other CPU has changed
+            // `is_available` to `false` yet, and no other CPU can do it anymore because this CPU
+            // holds the only reference (mutable) to `self` now.
+            unsafe { drop_in_place(p.data.get()) };
+        }
+    }
+}
+
+/// A handle to perform revocation on a [`HazPtrRevocable`]. Revokes when dropped.
+pub struct HazPtrRevokeHandle<'a, T>(&'a HazPtrRevocable<T>);
+
+impl<'a, T> HazPtrRevokeHandle<'a, T> {
+    /// Create a revoke-on-drop handle.
+    pub fn new(revocable: &'a HazPtrRevocable<T>) -> Self {
+        Self(revocable)
+    }
+
+    /// Dismiss the handle without revoking.
+    pub fn dismiss(self) {
+        core::mem::forget(self);
+    }
+}
+
+impl<T> Drop for HazPtrRevokeHandle<'_, T> {
+    fn drop(&mut self) {
+        self.0.revoke();
+    }
+}
+
+/// Guard for a [`HazPtrRevocable`].
+pub struct HazPtrRevocableGuard<'a, T> {
+    guard: hazptr::Guard<'a, T>,
+}
+
+impl<'a, T> HazPtrRevocableGuard<'a, T> {
+    fn new(guard: hazptr::Guard<'a, T>) -> Self {
+        Self { guard }
+    }
+}
+
+impl<T> Deref for HazPtrRevocableGuard<'_, T> {
+    type Target = T;
+
+    fn deref(&self) -> &Self::Target {
+        &self.guard
+    }
+}
+
 /// An object that is initialized and can become inaccessible at runtime.
 ///
 /// [`Revocable`] is initialized at the beginning, and can be made inaccessible at runtime.
Signature · 1 line
2.43.0
author
#11 lore raw

PATCH 6/13 rust: revocable: make LazyRevocable use HazPtrRevocable

In reply to Alvin Sun

Change LazyRevocable to use the existing HazPtrRevocable backend instead of the RCU-based Revocable. init() now returns HazPtrRevokeHandle and try_access(ctx) forwards to the hazptr-based implementation.

---
 rust/kernel/revocable.rs | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
rust/kernel/revocable.rs +11 -7
index eabb76ce92c43..e974c75a90ec0 100644
@@ -421,7 +421,7 @@ fn deref(&self) -> &Self::Target {
 #[pin_data]
 pub struct LazyRevocable<T> {
     #[pin]
-    once: SetOnce<Revocable<T>>,
+    once: SetOnce<HazPtrRevocable<T>>,
 }
 
 impl<T> LazyRevocable<T> {
@@ -440,18 +440,22 @@ pub const fn new() -> Self {
     pub fn init<E: Into<Error>>(
         self: Pin<&Self>,
         init: impl PinInit<T, E>,
-    ) -> Result<RevokeHandle<'_, T>> {
+    ) -> Result<HazPtrRevokeHandle<'_, T>> {
         // SAFETY: `once` is structurally pinned.
         let once = unsafe { self.map_unchecked(|x| &x.once) };
-        let revocable = once.pin_init(Revocable::new(init))?;
-        Ok(RevokeHandle::new(revocable))
+        let revocable = once.pin_init(HazPtrRevocable::new(init))?;
+        Ok(HazPtrRevokeHandle::new(revocable))
     }
 
     /// Tries to access the revocable wrapped object.
     ///
-    /// Returns `None` if the object has not been initialized, or it has been revoked and is therefore no longer accessible.
-    pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
-        self.once.as_ref()?.try_access()
+    /// Returns `None` if the object has not been initialized, or it has been revoked and is
+    /// therefore no longer accessible.
+    pub fn try_access<'a>(
+        &self,
+        ctx: Pin<&'a mut HazptrCtx>,
+    ) -> Option<HazPtrRevocableGuard<'a, T>> {
+        self.once.as_ref()?.try_access(ctx)
     }
 }
 
Signature · 1 line
2.43.0
author
#12 lore raw

PATCH 7/13 rust: drm: add Device::primary_index()

In reply to Alvin Sun

Add primary_index() to return the primary minor index (e.g. the N in /dev/dri/cardN). Needed by drivers to register per-device debugfs under a unique directory name.

---
 rust/kernel/drm/device.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/drm/device.rs +10 -0
index d7bb338efb9d8..268ac02e800fd 100644
@@ -336,6 +336,16 @@ pub(crate) unsafe fn assume_ctx<NewCtx: DeviceContext>(&self) -> &Device<T, NewC
     }
 }
 
+impl<T: drm::Driver> Device<T, Registered> {
+    /// Returns the primary minor index (the number in `/dev/dri/cardN`).
+    #[inline]
+    pub fn primary_index(&self) -> u32 {
+        // SAFETY: `self.as_raw()` is guaranteed to be a valid pointer to a `struct drm_device`.
+        let index = unsafe { (*(*self.as_raw()).primary).index };
+        index as u32
+    }
+}
+
 impl<T: drm::Driver> Deref for Device<T, Registered> {
     type Target = T::Data;
 
Signature · 1 line
2.43.0
author
#13 lore raw

PATCH 8/13 rust: drm/gem: add GEM object query helpers for debugfs

In reply to Alvin Sun

Add name(), refcount(), is_imported(), is_exported(), and mmap_offset() to BaseObject so drivers can expose GEM state in debugfs.

---
 rust/kernel/drm/gem/mod.rs | 49 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)
rust/kernel/drm/gem/mod.rs +46 -3
index 276ba3c53475d..b6188c8873ece 100644
@@ -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
+    }
+
+    /// 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>
Signature · 1 line
2.43.0
maintainer
#14 lore raw

Re: [PATCH 08/13] rust: drm/gem: add GEM object query helpers for debugfs

Quoted 40 lines · Alvin Sun

Add name(), refcount(), is_imported(), is_exported(), and mmap_offset() to BaseObject so drivers can expose GEM state in debugfs.

---
 rust/kernel/drm/gem/mod.rs | 49 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)
rust/kernel/drm/gem/mod.rs +14 -3
index 276ba3c53475d..b6188c8873ece 100644
@@ -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 }
+    }
+

rust/kernel/drm/gem/mod.rs fn size(&self) -> usize { · line 200

Quoted 10 lines
rust/kernel/drm/gem/mod.rs +10 -0
+    /// 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?

Quoted 28 lines
rust/kernel/drm/gem/mod.rs +22 -0
+
+    /// 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>
Signature · 2 lines
-- 
2.43.0
author
#15 lore raw

Re: [PATCH 08/13] rust: drm/gem: add GEM object query helpers for debugfs

Quoted 44 lines · Danilo Krummrich
Quoted 43 lines · Alvin Sun

Add name(), refcount(), is_imported(), is_exported(), and mmap_offset() to BaseObject so drivers can expose GEM state in debugfs.

---
  rust/kernel/drm/gem/mod.rs | 49 +++++++++++++++++++++++++++++++++++++++++++---
  1 file changed, 46 insertions(+), 3 deletions(-)
rust/kernel/drm/gem/mod.rs +17 -3
index 276ba3c53475d..b6188c8873ece 100644
@@ -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 {
Quoted 8 lines

rust/kernel/drm/gem/mod.rs fn size(&self) -> usize { · line 200

Quoted 7 lines
rust/kernel/drm/gem/mod.rs +7 -0
+        // 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

I'm just following the pattern of Panthor's debugfs output.

Quoted 4 lines

object reference counts, it motivates abuse.

Can't we just prove a common Debug impl for GEM objects instead of providing all those accessors?

A generic Debug impl for GemObject? Other drivers like Nova might need different output in a different style.

How about a dedicated debugfs data structure for GEM Object instead? Say, a get_debugfs_info() method that handles synchronization internally and avoids exposing unnecessary interfaces?

Quoted 29 lines
Quoted 29 lines
rust/kernel/drm/gem/mod.rs +22 -0
+
+    /// 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>
Signature · 2 lines
-- 
2.43.0
maintainer
#16 lore raw

Re: [PATCH 08/13] rust: drm/gem: add GEM object query helpers for debugfs

Quoted 3 lines · Alvin Sun

A generic Debug impl for GemObject? Other drivers like Nova might need different
output in a different style.

Why is any of Tyr or Nova special here?

There's already a lot of common debugfs code in DRM [1] (which is per DRM device), including some GEM info.

If that is not enough, why can't we extend it / add another entry?

Quoted 3 lines

How about a dedicated debugfs data structure for GEM Object instead? Say, a get_debugfs_info() method that handles synchronization internally and avoids exposing unnecessary interfaces?

It would need to be guarded with a debugfs config, otherwise we'd still expose information a driver should never touch directly, which can easily become a bit messy.

But again, why do we expect any of those drivers to be special? I think this can just be common code.

[1] https://elixir.bootlin.com/linux/v7.0.8/source/drivers/gpu/drm/drm_debugfs.c

author
#17 lore raw

PATCH 9/13 rust: drm/gem/shmem: add resident_size() and madv() for debugfs

In reply to Alvin Sun

Expose resident size and madvise state of shmem GEM objects so drivers can show per-BO memory usage and reclaimable size in debugfs.

---
 rust/kernel/drm/gem/shmem.rs | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
rust/kernel/drm/gem/shmem.rs +21 -0
index 065bdd78d1ba6..ba25a4a832db6 100644
@@ -196,6 +196,27 @@ extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
         let _ = unsafe { KBox::from_raw(this) };
     }
 
+    /// Returns the resident size of this object.
+    #[inline]
+    pub fn resident_size(&self) -> usize {
+        // SAFETY: `self.as_raw_shmem()` is guaranteed to be a valid pointer to a
+        // drm_gem_shmem_object.
+        if unsafe { (*self.as_raw_shmem()).pages }.is_null() {
+            0
+        } else {
+            // SAFETY: `self.as_raw()` is guaranteed to be a valid pointer to a drm_gem_object.
+            unsafe { (*self.as_raw()).size }
+        }
+    }
+
+    /// Returns the state for madvise of this object.
+    #[inline]
+    pub fn madv(&self) -> i32 {
+        // SAFETY: `self.as_raw_shmem()` is guaranteed to be a valid pointer to a
+        // drm_gem_shmem_object.
+        unsafe { (*self.as_raw_shmem()).madv }
+    }
+
     /// Creates (if necessary) and returns an immutable reference to a scatter-gather table of DMA
     /// pages for this object.
     ///
Signature · 1 line
2.43.0
author
#18 lore raw

PATCH 10/13 drm/tyr: expose Vm gpuvm_core, gpuvm and va_range as pub(crate)

In reply to Alvin Sun

Allow the debugfs module to read VM and GPU VA state for the gpuvas file.

---
 drivers/gpu/drm/tyr/vm.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/tyr/vm.rs +3 -3
index 936c3049812b3..ccf4493065a49 100644
@@ -306,12 +306,12 @@ pub(crate) struct Vm {
     pdev: ARef<platform::Device>,
     /// DRM GPUVM core for managing virtual address space.
     #[pin]
-    gpuvm_core: Mutex<GpuVmCore<GpuVmData>>,
+    pub(crate) gpuvm_core: Mutex<GpuVmCore<GpuVmData>>,
     /// Non-core part of the GPUVM. Can be used for stuff that doesn't modify the
     /// internal mapping tree, like GpuVm::obtain()
-    gpuvm: ARef<GpuVm<GpuVmData>>,
+    pub(crate) gpuvm: ARef<GpuVm<GpuVmData>>,
     /// VA range for this VM.
-    va_range: Range<u64>,
+    pub(crate) va_range: Range<u64>,
 }
 
 impl Vm {
Signature · 1 line
2.43.0
author
#19 lore raw

PATCH 11/13 drm/tyr: add debugfs infrastructure

In reply to Alvin Sun

Add module debugfs root, per-device directory and TyrDebugFSData; wire into driver and call debugfs_init.

---
 drivers/gpu/drm/tyr/debugfs.rs | 46 ++++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tyr/driver.rs  | 11 +++++++++-
 drivers/gpu/drm/tyr/tyr.rs     | 39 ++++++++++++++++++++++++++++++++---
 3 files changed, 92 insertions(+), 4 deletions(-)
drivers/gpu/drm/tyr/debugfs.rs new file +46 -0
new file mode 100644
index 0000000000000..254ecef43ea9a
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0 or MIT
+
+//! Debugfs support for the Tyr DRM driver.
+
+use core::pin;
+
+use kernel::{
+    debugfs,
+    device::Core,
+    drm,
+    platform,
+    prelude::*,
+    revocable::LazyRevocable,
+    str::CString,
+    sync::{
+        hazptr::HazptrCtx,
+        Arc,
+        ArcBorrow, //
+    }, //
+};
+
+use crate::driver::TyrDrmDriver;
+
+pub(crate) static DEBUGFS_ROOT: LazyRevocable<debugfs::Dir> = LazyRevocable::new();
+
+/// Per-device debugfs data.
+pub(crate) struct TyrDebugFSData {}
+
+/// Registers per-device debugfs directory under the module's debugfs root.
+pub(crate) fn debugfs_init(
+    ddev: &drm::Device<TyrDrmDriver>,
+    pdev: &platform::Device<Core>,
+    debugfs_data: ArcBorrow<'_, TyrDebugFSData>,
+) -> Result {
+    let idx = ddev.primary_index();
+    let dir_name = CString::try_from_fmt(fmt!("{}", idx))?;
+    let ctx = pin::pin!(HazptrCtx::new());
+    let root_dir = DEBUGFS_ROOT.try_access(ctx).ok_or_else(|| {
+        pr_err!("DEBUGFS_ROOT is not set");
+        ENOENT
+    })?;
+    let debugfs_data: Arc<TyrDebugFSData> = debugfs_data.into();
+    let scope_init = root_dir.scope(debugfs_data, &dir_name, |_data, _dir| {});
+
+    kernel::devres::register(pdev.as_ref(), scope_init, GFP_KERNEL)
+}
drivers/gpu/drm/tyr/driver.rs +10 -1
index 593f71c550e08..c8c929fda06ac 100644
@@ -38,6 +38,7 @@
 };
 
 use crate::{
+    debugfs::TyrDebugFSData,
     file::TyrDrmFileData,
     fw::Firmware,
     gem::BoData,
@@ -75,6 +76,9 @@ pub(crate) struct TyrDrmDeviceData {
     ///
     /// This is mainly queried by userspace, i.e.: Mesa.
     pub(crate) gpu_info: GpuInfo,
+
+    /// Per-device debugfs data.
+    pub(crate) debugfs_data: Arc<TyrDebugFSData>,
 }
 
 // Both `Clk` and `Regulator` do not implement `Send` or `Sync`, but they
@@ -150,6 +154,8 @@ fn probe(
         let platform: ARef<platform::Device> = pdev.into();
 
         let mmu = Mmu::new(pdev, iomem.as_arc_borrow(), &gpu_info)?;
+        let debugfs_data = Arc::new(TyrDebugFSData {}, GFP_KERNEL)?;
+        let debugfs_data_clone = debugfs_data.clone();
 
         let firmware = Firmware::new(
             pdev,
@@ -174,9 +180,12 @@ fn probe(
                     _sram: sram_regulator,
                 }),
                 gpu_info,
+                debugfs_data: debugfs_data_clone,
         });
-
         let ddev = Registration::new_foreign_owned(uninit_ddev, pdev.as_ref(), data, 0)?;
+
+        crate::debugfs::debugfs_init(ddev, pdev, debugfs_data.as_arc_borrow())?;
+
         let driver = TyrPlatformDriverData {
             _device: ddev.into(),
         };
drivers/gpu/drm/tyr/tyr.rs +36 -3
index 18b0668bb2178..cda4955db4dc9 100644
@@ -5,8 +5,20 @@
 //! The name "Tyr" is inspired by Norse mythology, reflecting Arm's tradition of
 //! naming their GPUs after Nordic mythological figures and places.
 
-use crate::driver::TyrPlatformDriverData;
+use crate::{
+    debugfs::DEBUGFS_ROOT,
+    driver::TyrPlatformDriverData, //
+};
+use kernel::{
+    driver::Registration,
+    error,
+    platform,
+    prelude::*,
+    revocable::HazPtrRevokeHandle,
+    InPlaceModule, //
+};
 
+mod debugfs;
 mod driver;
 mod file;
 mod fw;
@@ -17,8 +29,29 @@
 mod slot;
 mod vm;
 
-kernel::module_platform_driver! {
-    type: TyrPlatformDriverData,
+pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
+
+#[pin_data]
+struct TyrModule {
+    _debugfs_root: HazPtrRevokeHandle<'static, kernel::debugfs::Dir>,
+    #[pin]
+    _driver: Registration<platform::Adapter<TyrPlatformDriverData>>,
+}
+
+impl InPlaceModule for TyrModule {
+    fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, error::Error> {
+        let dir = kernel::debugfs::Dir::new(kernel::c_str!("tyr"));
+        let debugfs_root_handle = Pin::static_ref(&DEBUGFS_ROOT).init(dir);
+
+        try_pin_init!(Self {
+            _driver <- Registration::new(MODULE_NAME, module),
+            _debugfs_root <- debugfs_root_handle,
+        })
+    }
+}
+
+module! {
+    type: TyrModule,
     name: "tyr",
     authors: ["The Tyr driver authors"],
     description: "Arm Mali Tyr DRM driver",
Signature · 1 line
2.43.0
maintainer
#20 lore raw

Re: [PATCH 11/13] drm/tyr: add debugfs infrastructure

drivers/gpu/drm/tyr/tyr.rs line 43

Quoted 10 lines · Alvin Sun
drivers/gpu/drm/tyr/tyr.rs +10 -0
+#[pin_data]
+struct TyrModule {
+    _debugfs_root: HazPtrRevokeHandle<'static, kernel::debugfs::Dir>,
+    #[pin]
+    _driver: Registration<platform::Adapter<TyrPlatformDriverData>>,
+}
+
+impl InPlaceModule for TyrModule {
+    fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, error::Error> {
+        let dir = kernel::debugfs::Dir::new(kernel::c_str!("tyr"));

Why does Tyr need its own debugfs directory to expose DRM data structures? DRM already has a common debugfs directory per DRM device in /sys/kernel/debug/dri/, which you should attach to instead if you want to expose driver specific files.

author
#21 lore raw

Re: [PATCH 11/13] drm/tyr: add debugfs infrastructure

Quoted 14 lines · Danilo Krummrich

drivers/gpu/drm/tyr/tyr.rs line 43

Quoted 10 lines · Alvin Sun
drivers/gpu/drm/tyr/tyr.rs +10 -0
+#[pin_data]
+struct TyrModule {
+    _debugfs_root: HazPtrRevokeHandle<'static, kernel::debugfs::Dir>,
+    #[pin]
+    _driver: Registration<platform::Adapter<TyrPlatformDriverData>>,
+}
+
+impl InPlaceModule for TyrModule {
+    fn init(module: &'static kernel::ThisModule) -> impl PinInit<Self, error::Error> {
+        let dir = kernel::debugfs::Dir::new(kernel::c_str!("tyr"));

Why does Tyr need its own debugfs directory to expose DRM data structures? DRM already has a common debugfs directory per DRM device in /sys/kernel/debug/dri/, which you should attach to instead if you want to expose driver specific files.

Hi Danilo,

Sorry for the late reply, I was busy with the fops owner patch series last week.

Thank you for the detailed review and suggestions. I've reworked the implementation based on your feedback:

- Instead of creating a separate tyr debugfs directory, the code now hooks into the DRM core's per-device debugfs root under /sys/kernel/debug/dri/ via a debugfs_init callback.
- The duplicated show_vm() code is eliminated — the gpuvas file now reuses the existing drm_debugfs_gpuva_info() through a safe Rust wrapper, rather than re-implementing the VA space dump.

Note that the GEM debugfs interface is not yet implemented — I'd like to get your feedback on the overall approach first before continuing with that part.

Please review the updated series [1] and let me know your thoughts.

[1] https://gitlab.freedesktop.org/alvinsun039/linux/-/commits/tyr-debugfs-v2

Best regards,
Alvin

author
#22 lore raw

PATCH 12/13 drm/tyr: add vms and gpuvas debugfs interface

In reply to Alvin Sun

Add vms list to Mmu and vms field to TyrDebugFSData; populate vms in Firmware::new and register gpuvas read-only file under debugfs per-device directory.

---
 drivers/gpu/drm/tyr/debugfs.rs | 59 +++++++++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/tyr/driver.rs  |  5 +++-
 drivers/gpu/drm/tyr/fw.rs      |  5 +++-
 drivers/gpu/drm/tyr/mmu.rs     |  6 ++++-
 4 files changed, 69 insertions(+), 6 deletions(-)
drivers/gpu/drm/tyr/debugfs.rs +56 -3
index 254ecef43ea9a..edbdb83a5b132 100644
@@ -8,6 +8,9 @@
     debugfs,
     device::Core,
     drm,
+    drm::gem::IntoGEMObject,
+    fmt,
+    fmt::Write,
     platform,
     prelude::*,
     revocable::LazyRevocable,
@@ -15,16 +18,64 @@
     sync::{
         hazptr::HazptrCtx,
         Arc,
-        ArcBorrow, //
+        ArcBorrow,
+        Mutex, //
     }, //
 };
 
 use crate::driver::TyrDrmDriver;
+use crate::vm::Vm;
 
 pub(crate) static DEBUGFS_ROOT: LazyRevocable<debugfs::Dir> = LazyRevocable::new();
 
 /// Per-device debugfs data.
-pub(crate) struct TyrDebugFSData {}
+#[pin_data]
+pub(crate) struct TyrDebugFSData {
+    #[pin]
+    pub(crate) vms: Mutex<KVec<Arc<Vm>>>,
+}
+
+/// Writes VM debug information for the "gpuvas" debugfs file.
+fn show_vm(vm: &Vm, f: &mut impl Write) -> core::fmt::Result {
+    writeln!(
+        f,
+        "DRM GPU VA space ({:?}) [0x{:016x};0x{:016x}]",
+        vm.gpuvm.name(),
+        vm.va_range.start,
+        vm.va_range.end,
+    )?;
+
+    let kva = vm.gpuvm.kernel_alloc_va();
+    writeln!(
+        f,
+        "Kernel reserved node [0x{:016x};0x{:016x}]",
+        kva.addr(),
+        kva.addr() + kva.length(),
+    )?;
+
+    writeln!(f, " VAs | start              | range              | end                | object             | object offset")?;
+    writeln!(f, "-------------------------------------------------------------------------------------------------------------")?;
+    for va in vm.gpuvm_core.lock().va_mappings() {
+        f.write_fmt(fmt!(
+            "     | 0x{:016x} | 0x{:016x} | 0x{:016x} | {:18p} | 0x{:016x}\n",
+            va.addr(),
+            va.length(),
+            va.addr() + va.length(),
+            va.obj().as_raw(),
+            va.gem_offset(),
+        ))?;
+    }
+    Ok(())
+}
+
+fn show_gpuvas(data: &Arc<TyrDebugFSData>, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
+    let vms = data.vms.lock();
+    for vm in vms.iter() {
+        show_vm(vm, f)?;
+        writeln!(f)?;
+    }
+    Ok(())
+}
 
 /// Registers per-device debugfs directory under the module's debugfs root.
 pub(crate) fn debugfs_init(
@@ -40,7 +91,9 @@ pub(crate) fn debugfs_init(
         ENOENT
     })?;
     let debugfs_data: Arc<TyrDebugFSData> = debugfs_data.into();
-    let scope_init = root_dir.scope(debugfs_data, &dir_name, |_data, _dir| {});
+    let scope_init = root_dir.scope(debugfs_data, &dir_name, |data, dir| {
+        dir.read_callback_file(c"gpuvas", data, &show_gpuvas);
+    });
 
     kernel::devres::register(pdev.as_ref(), scope_init, GFP_KERNEL)
 }
drivers/gpu/drm/tyr/driver.rs +4 -1
index c8c929fda06ac..e1d5e908de876 100644
@@ -154,7 +154,9 @@ fn probe(
         let platform: ARef<platform::Device> = pdev.into();
 
         let mmu = Mmu::new(pdev, iomem.as_arc_borrow(), &gpu_info)?;
-        let debugfs_data = Arc::new(TyrDebugFSData {}, GFP_KERNEL)?;
+        let debugfs_data = Arc::pin_init(try_pin_init!(TyrDebugFSData {
+            vms <- new_mutex!(KVec::new()),
+        }), GFP_KERNEL)?;
         let debugfs_data_clone = debugfs_data.clone();
 
         let firmware = Firmware::new(
@@ -163,6 +165,7 @@ fn probe(
             &uninit_ddev,
             mmu.as_arc_borrow(),
             &gpu_info,
+            debugfs_data.as_arc_borrow(),
         )?;
 
         firmware.boot()?;
drivers/gpu/drm/tyr/fw.rs +4 -1
index b62e5ed69c4d4..c46320bb54516 100644
@@ -37,6 +37,7 @@
 };
 
 use crate::{
+    debugfs::TyrDebugFSData,
     driver::{
         IoMem,
         TyrDrmDevice, //
@@ -200,6 +201,7 @@ pub(crate) fn new(
         ddev: &TyrDrmDevice<Uninit>,
         mmu: ArcBorrow<'_, Mmu>,
         gpu_info: &GpuInfo,
+        debugfs_data: ArcBorrow<'_, TyrDebugFSData>,
     ) -> Result<Arc<Firmware>> {
         let vm = Vm::new(pdev, ddev, mmu, gpu_info)?;
 
@@ -238,11 +240,12 @@ pub(crate) fn new(
             Firmware {
                 pdev: pdev.into(),
                 iomem: iomem.into(),
-                vm,
+                vm: vm.clone(),
                 sections,
             },
             GFP_KERNEL,
         )?;
+        debugfs_data.vms.lock().push(vm, GFP_KERNEL)?;
 
         Ok(firmware)
     }
drivers/gpu/drm/tyr/mmu.rs +5 -1
index 52a6bbbb179a2..d5e6af4b804e4 100644
@@ -35,7 +35,8 @@
         VmAsData, //
     },
     regs::MAX_AS_REGISTERS,
-    slot::SlotManager, //
+    slot::SlotManager,
+    vm, //
 };
 
 pub(crate) mod address_space;
@@ -51,6 +52,8 @@
 /// threads. Methods may block if another thread holds the lock.
 #[pin_data]
 pub(crate) struct Mmu {
+    #[pin]
+    pub(crate) vms: Mutex<KVec<Arc<vm::Vm>>>,
     /// Manages the allocation of hardware MMU slots to GPU address spaces.
     ///
     /// Tracks which address spaces are currently active in hardware slots and
@@ -75,6 +78,7 @@ pub(crate) fn new(
         let as_manager = AddressSpaceManager::new(pdev, iomem, gpu_info.as_present)?;
         let mmu_init = try_pin_init!(Self{
             as_manager <- new_mutex!(SlotManager::new(as_manager, slot_count)?),
+            vms <- new_mutex!(KVec::new()),
         });
         Arc::pin_init(mmu_init, GFP_KERNEL)
     }
Signature · 1 line
2.43.0
maintainer
#23 lore raw

Re: [PATCH 12/13] drm/tyr: add vms and gpuvas debugfs interface

Quoted 22 lines · Alvin Sun
drivers/gpu/drm/tyr/debugfs.rs +22 -0
+/// Writes VM debug information for the "gpuvas" debugfs file.
+fn show_vm(vm: &Vm, f: &mut impl Write) -> core::fmt::Result {
+    writeln!(
+        f,
+        "DRM GPU VA space ({:?}) [0x{:016x};0x{:016x}]",
+        vm.gpuvm.name(),
+        vm.va_range.start,
+        vm.va_range.end,
+    )?;
+
+    let kva = vm.gpuvm.kernel_alloc_va();
+    writeln!(
+        f,
+        "Kernel reserved node [0x{:016x};0x{:016x}]",
+        kva.addr(),
+        kva.addr() + kva.length(),
+    )?;
+
+    writeln!(f, " VAs | start              | range              | end                | object             | object offset")?;
+    writeln!(f, "-------------------------------------------------------------------------------------------------------------")?;
+    for va in vm.gpuvm_core.lock().va_mappings() {
+        f.write_fmt(fmt!(

drivers/gpu/drm/tyr/debugfs.rs line 69

Quoted 10 lines
drivers/gpu/drm/tyr/debugfs.rs +10 -0
+            "     | 0x{:016x} | 0x{:016x} | 0x{:016x} | {:18p} | 0x{:016x}\n",
+            va.addr(),
+            va.length(),
+            va.addr() + va.length(),
+            va.obj().as_raw(),
+            va.gem_offset(),
+        ))?;
+    }
+    Ok(())
+}

Why is this code duplicated? The implementation already exists in [1].

[1] https://elixir.bootlin.com/linux/v7.0.8/source/drivers/gpu/drm/drm_debugfs.c#L209

author
#24 lore raw

PATCH 13/13 drm/tyr: add gems field and gems debugfs interface

In reply to Alvin Sun

Add gems field to TyrDebugFSData; populate gems in Firmware::new and register gems read-only file under debugfs per-device directory.

---
 drivers/gpu/drm/tyr/debugfs.rs | 93 +++++++++++++++++++++++++++++++++++++++++-
 drivers/gpu/drm/tyr/driver.rs  | 10 +++--
 drivers/gpu/drm/tyr/fw.rs      |  2 +
 3 files changed, 101 insertions(+), 4 deletions(-)
drivers/gpu/drm/tyr/debugfs.rs +92 -1
index edbdb83a5b132..d0e9e268aeac2 100644
@@ -8,14 +8,19 @@
     debugfs,
     device::Core,
     drm,
-    drm::gem::IntoGEMObject,
+    drm::gem::{
+        BaseObject,
+        IntoGEMObject, //
+    },
     fmt,
     fmt::Write,
+    impl_flags,
     platform,
     prelude::*,
     revocable::LazyRevocable,
     str::CString,
     sync::{
+        aref::ARef,
         hazptr::HazptrCtx,
         Arc,
         ArcBorrow,
@@ -24,15 +29,100 @@
 };
 
 use crate::driver::TyrDrmDriver;
+use crate::gem::Bo;
 use crate::vm::Vm;
 
 pub(crate) static DEBUGFS_ROOT: LazyRevocable<debugfs::Dir> = LazyRevocable::new();
 
+impl_flags!(
+    #[derive(Debug, Clone, Default, Copy, PartialEq, Eq)]
+    struct BoStateFlags(u32);
+
+    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
+    enum BoStateFlag {
+        Imported = 1 << 0,
+        Exported = 1 << 1,
+    }
+);
+
+/// Writes per-BO debug information for the "gems" debugfs file.
+fn show_bo(bo: &Bo, f: &mut impl Write) -> core::fmt::Result {
+    let name: i32 = bo.name();
+    let refcount: u32 = bo.refcount();
+    let size: usize = bo.size();
+    let resident_size: usize = bo.resident_size();
+    let mmap_offset = bo.mmap_offset();
+    let mut gem_state_flags = BoStateFlags::default();
+
+    if bo.is_imported() {
+        gem_state_flags |= BoStateFlag::Imported;
+    }
+    if bo.is_exported() {
+        gem_state_flags |= BoStateFlag::Exported;
+    }
+
+    writeln!(
+        f,
+        "{:<16}{:<16}{:<16}{:<16}0x{:<16x}0x{:<8x}",
+        name,
+        refcount,
+        size,
+        resident_size,
+        mmap_offset,
+        u32::from(gem_state_flags),
+    )
+}
+
+fn show_gems(data: &Arc<TyrDebugFSData>, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
+    writeln!(
+        f,
+        "GEM state flags: {:?} (0x{:x}), {:?} (0x{:x})",
+        BoStateFlag::Imported,
+        BoStateFlag::Imported as u32,
+        BoStateFlag::Exported,
+        BoStateFlag::Exported as u32,
+    )?;
+    writeln!(
+        f,
+        "global-name     refcount        size            resident-size   file-offset       state",
+    )?;
+    writeln!(
+        f,
+        "--------------------------------------------------------------------------------------------",
+    )?;
+
+    let mut total_size: usize = 0;
+    let mut total_resident: usize = 0;
+    let mut total_reclaimable: usize = 0;
+    let gems = data.gems.lock();
+    for bo in gems.iter() {
+        total_size += bo.size();
+        total_resident += bo.resident_size();
+        if bo.madv() > 0 {
+            total_reclaimable += bo.resident_size();
+        }
+        show_bo(bo, f)?;
+    }
+
+    writeln!(
+        f,
+        "============================================================================================",
+    )?;
+    writeln!(
+        f,
+        "Total size: {}, Total resident: {}, Total reclaimable: {}",
+        total_size, total_resident, total_reclaimable,
+    )?;
+    Ok(())
+}
+
 /// Per-device debugfs data.
 #[pin_data]
 pub(crate) struct TyrDebugFSData {
     #[pin]
     pub(crate) vms: Mutex<KVec<Arc<Vm>>>,
+    #[pin]
+    pub(crate) gems: Mutex<KVec<ARef<Bo>>>,
 }
 
 /// Writes VM debug information for the "gpuvas" debugfs file.
@@ -93,6 +183,7 @@ pub(crate) fn debugfs_init(
     let debugfs_data: Arc<TyrDebugFSData> = debugfs_data.into();
     let scope_init = root_dir.scope(debugfs_data, &dir_name, |data, dir| {
         dir.read_callback_file(c"gpuvas", data, &show_gpuvas);
+        dir.read_callback_file(c"gems", data, &show_gems);
     });
 
     kernel::devres::register(pdev.as_ref(), scope_init, GFP_KERNEL)
drivers/gpu/drm/tyr/driver.rs +7 -3
index e1d5e908de876..a6d8760070d44 100644
@@ -154,9 +154,13 @@ fn probe(
         let platform: ARef<platform::Device> = pdev.into();
 
         let mmu = Mmu::new(pdev, iomem.as_arc_borrow(), &gpu_info)?;
-        let debugfs_data = Arc::pin_init(try_pin_init!(TyrDebugFSData {
-            vms <- new_mutex!(KVec::new()),
-        }), GFP_KERNEL)?;
+        let debugfs_data = Arc::pin_init(
+            try_pin_init!(TyrDebugFSData {
+                vms <- new_mutex!(KVec::new()),
+                gems <- new_mutex!(KVec::new()),
+            }),
+            GFP_KERNEL,
+        )?;
         let debugfs_data_clone = debugfs_data.clone();
 
         let firmware = Firmware::new(
drivers/gpu/drm/tyr/fw.rs +2 -0
index c46320bb54516..4d5efa79a7348 100644
@@ -222,6 +222,8 @@ pub(crate) fn new(
                 parsed.vm_map_flags,
             )?;
 
+            debugfs_data.gems.lock().push(mem.bo.clone(), GFP_KERNEL)?;
+
             let section_start = parsed.data_range.start as usize;
             let section_end = parsed.data_range.end as usize;
             let mut data = KVec::new();
Signature · 1 line
2.43.0
maintainer
#25 lore raw
In reply to Alvin Sun

Hi Alvin,

[cc rcu list and Mathieu]

Quoted 10 lines · Alvin Sun

This patchset contains:
- hazptr: Add hazard pointer abstraction to kernel::sync
- revocable: Introduce HazPtrRevocable, and adapt Gary Guo's downstream LazyRevocable implementation to use hazard pointers internally, enabling safe sharing of debugfs-root data across the tyr module and DRM driver
- drm: Add device and GEM helper functions to support debugfs
- tyr: Add debugfs infrastructure and interfaces to expose GEM/VM information

Quoted 2 lines

See the full dependency and commit history at [1].

Thank you for the nice work!

I know the context of why you're doing this because we had some discussion on zulip, but maybe most of people on the list don't. So could you add a "why" or "motivation" part in the cover letter for the next time? It's about syncing with everyone on the same page so that they can review with the context ;-) Thanks!

Also since this is about hazptr and RCU, could you Cc [email protected] and Mathieu the next time? Thanks!

Regards,
Boqun

Quoted 86 lines

---
Alvin Sun (11):
rust: sync: set_once: Rename InitError variants to fix clippy warning
rust: sync: add hazard pointer abstraction
rust: revocable: add HazPtrRevocable
rust: revocable: make LazyRevocable use HazPtrRevocable
rust: drm: add Device::primary_index()
rust: drm/gem: add GEM object query helpers for debugfs
rust: drm/gem/shmem: add resident_size() and madv() for debugfs drm/tyr: expose Vm gpuvm_core, gpuvm and va_range as pub(crate) drm/tyr: add debugfs infrastructure
drm/tyr: add vms and gpuvas debugfs interface
drm/tyr: add gems field and gems debugfs interface

Gary Guo (2):
rust: sync: support [pin_]init for SetOnce
rust: revocable: add lazily instantiated revocable variant

 drivers/gpu/drm/tyr/debugfs.rs  | 190 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tyr/driver.rs   |  18 +++-
 drivers/gpu/drm/tyr/fw.rs       |   7 +-
 drivers/gpu/drm/tyr/mmu.rs      |   6 +-
 drivers/gpu/drm/tyr/tyr.rs      |  39 +++++++-
 drivers/gpu/drm/tyr/vm.rs       |   6 +-
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/hazptr.c           |  13 +++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/drm/device.rs       |  10 ++
 rust/kernel/drm/gem/mod.rs      |  49 +++++++++-
 rust/kernel/drm/gem/shmem.rs    |  21 ++++
 rust/kernel/revocable.rs        | 209 +++++++++++++++++++++++++++++++++++++++-
 rust/kernel/sync.rs             |   1 +
 rust/kernel/sync/hazptr.rs      |  91 +++++++++++++++++
 rust/kernel/sync/set_once.rs    | 120 +++++++++++++++++++----
 16 files changed, 746 insertions(+), 36 deletions(-)

---
base-commit: 34cb4f916af10153c87fabaf6c34e4cafa170427
change-id: 20260320-b4-tyr-debugfs-3fcaff064297
prerequisite-change-id: 20260202-io-81fd368f7565:v2
prerequisite-patch-id: 74873afed2b65f5a11c33f0c0c54375c55079109
prerequisite-patch-id: ae853e8eb8d58c77881371960be4ae92755e83c6
prerequisite-patch-id: 0ab78b50648c7d8f66b83c32ed2af0ec3ede42a3
prerequisite-patch-id: 8d20a8db8cf4660c682ee91f3db04e640c144e33
prerequisite-patch-id: 299de9cd2789c19c22e2816f7c817d80d5a4f1db
prerequisite-patch-id: 661ee334905f359daa8fb8d808ed5f4a8085f5c9
prerequisite-change-id: 20260117-register-ccaba1d21713:v9
prerequisite-patch-id: e4cb34ef09bccb92e1c35435e346b3a0459d3cdf
prerequisite-patch-id: 2c3c42ae9f3f4544c088dffbe4a0527f28b1c135
prerequisite-patch-id: 9f40e176e1b67bada943e376950debabb3f196d5
prerequisite-patch-id: ccc86d3ab9f62883c90d1275c99720942bd2bcb3
prerequisite-patch-id: 216427f1fdf94755cca0ed0676c4290f85d45e5c
prerequisite-patch-id: 7e1ea4d184297dfaba1351a7cb23e3b6827fed46
prerequisite-patch-id: 29ff9ffe37669d23b5509bfefb0cc1f8950eaa7c
prerequisite-patch-id: e5723722ff970c585cbf559456fa4cda05f28952
prerequisite-patch-id: afdae76e12011bb158d2008b8cd1ffba084db0bb
prerequisite-patch-id: b0473d5c7af0bbeec6f6ec8dd36d8a09cc180fed
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 454c4fe7a7bcc65b2c9cc69dd5dd8e4c1d952297
prerequisite-patch-id: aff529cc205f5ab35518b8e4568e6e1319eecaa9
prerequisite-patch-id: 9f7d5c4a8ed052f5c5f2a28c862a9392d7a812cc
prerequisite-patch-id: ec9d104ba7c67488f19e6dd1a50d1ec4f682f333
prerequisite-patch-id: e7a844bd6254643d1f23827886b487c196ee9968
prerequisite-patch-id: 3c57ebf13497cf57c586b955d5ce90f2f9fd8cdc
prerequisite-patch-id: 1a327345ac71599ece0b2272fba81472f70c568c
prerequisite-change-id: 20251128-gpuvm-rust-b719cac27ad6:v5
prerequisite-patch-id: 6a8ddd47a2e1c769c65a942b3d9315e6ae5cae7e
prerequisite-patch-id: d4ee9fb80025c4fd8129c3477cc3dc36e13076cb
prerequisite-patch-id: 0ad35cd3dfcf8a8804e9fed8df3e5612cf07da69
prerequisite-patch-id: 275a900645e14169911ee00bab54553c84b48ad0
prerequisite-patch-id: 2a2de2f6c4fac065de5c459026daa2c32f340d22
prerequisite-patch-id: 9a721bc700da778f2554cf32917dda4807744520
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 6c622d1df9166ad081acba46237f8cecbf728356
prerequisite-patch-id: 769892e477c161e6c7a57519ec635b1455310f1d
prerequisite-patch-id: 3c5f5070ded87e95a8045083a55501fbbe9f6313
prerequisite-patch-id: 10fbd8695264e67421946dda4d39e29d5fe77c64

Best regards,

Signature · 1 line
Alvin Sun <[email protected]>
author
#26 lore raw
Quoted 19 lines · Boqun Feng

Hi Alvin,

[cc rcu list and Mathieu]

Quoted 10 lines · Alvin Sun

This patchset contains:
- hazptr: Add hazard pointer abstraction to kernel::sync
- revocable: Introduce HazPtrRevocable, and adapt Gary Guo's downstream LazyRevocable implementation to use hazard pointers internally, enabling safe sharing of debugfs-root data across the tyr module and DRM driver
- drm: Add device and GEM helper functions to support debugfs
- tyr: Add debugfs infrastructure and interfaces to expose GEM/VM information

Quoted 2 lines

See the full dependency and commit history at [1].

Thank you for the nice work!

Quoted 8 lines

I know the context of why you're doing this because we had some discussion on zulip, but maybe most of people on the list don't. So could you add a "why" or "motivation" part in the cover letter for the next time? It's about syncing with everyone on the same page so that they can review with the context ;-) Thanks!

Also since this is about hazptr and RCU, could you Cc [email protected] and Mathieu the next time? Thanks!

Thanks for the reminder! I'll add the motivation section to the cover letter and CC the correct lists/people in the next version.

Best regards,
Alvin

Quoted 89 lines

Regards,
Boqun

Quoted 85 lines

---
Alvin Sun (11):
rust: sync: set_once: Rename InitError variants to fix clippy warning
rust: sync: add hazard pointer abstraction
rust: revocable: add HazPtrRevocable
rust: revocable: make LazyRevocable use HazPtrRevocable
rust: drm: add Device::primary_index()
rust: drm/gem: add GEM object query helpers for debugfs
rust: drm/gem/shmem: add resident_size() and madv() for debugfs drm/tyr: expose Vm gpuvm_core, gpuvm and va_range as pub(crate) drm/tyr: add debugfs infrastructure
drm/tyr: add vms and gpuvas debugfs interface
drm/tyr: add gems field and gems debugfs interface

Gary Guo (2):
rust: sync: support [pin_]init for SetOnce
rust: revocable: add lazily instantiated revocable variant

  drivers/gpu/drm/tyr/debugfs.rs  | 190 ++++++++++++++++++++++++++++++++++++
  drivers/gpu/drm/tyr/driver.rs   |  18 +++-
  drivers/gpu/drm/tyr/fw.rs       |   7 +-
  drivers/gpu/drm/tyr/mmu.rs      |   6 +-
  drivers/gpu/drm/tyr/tyr.rs      |  39 +++++++-
  drivers/gpu/drm/tyr/vm.rs       |   6 +-
  rust/bindings/bindings_helper.h |   1 +
  rust/helpers/hazptr.c           |  13 +++
  rust/helpers/helpers.c          |   1 +
  rust/kernel/drm/device.rs       |  10 ++
  rust/kernel/drm/gem/mod.rs      |  49 +++++++++-
  rust/kernel/drm/gem/shmem.rs    |  21 ++++
  rust/kernel/revocable.rs        | 209 +++++++++++++++++++++++++++++++++++++++-
  rust/kernel/sync.rs             |   1 +
  rust/kernel/sync/hazptr.rs      |  91 +++++++++++++++++
  rust/kernel/sync/set_once.rs    | 120 +++++++++++++++++++----
  16 files changed, 746 insertions(+), 36 deletions(-)

---
base-commit: 34cb4f916af10153c87fabaf6c34e4cafa170427
change-id: 20260320-b4-tyr-debugfs-3fcaff064297
prerequisite-change-id: 20260202-io-81fd368f7565:v2
prerequisite-patch-id: 74873afed2b65f5a11c33f0c0c54375c55079109
prerequisite-patch-id: ae853e8eb8d58c77881371960be4ae92755e83c6
prerequisite-patch-id: 0ab78b50648c7d8f66b83c32ed2af0ec3ede42a3
prerequisite-patch-id: 8d20a8db8cf4660c682ee91f3db04e640c144e33
prerequisite-patch-id: 299de9cd2789c19c22e2816f7c817d80d5a4f1db
prerequisite-patch-id: 661ee334905f359daa8fb8d808ed5f4a8085f5c9
prerequisite-change-id: 20260117-register-ccaba1d21713:v9
prerequisite-patch-id: e4cb34ef09bccb92e1c35435e346b3a0459d3cdf
prerequisite-patch-id: 2c3c42ae9f3f4544c088dffbe4a0527f28b1c135
prerequisite-patch-id: 9f40e176e1b67bada943e376950debabb3f196d5
prerequisite-patch-id: ccc86d3ab9f62883c90d1275c99720942bd2bcb3
prerequisite-patch-id: 216427f1fdf94755cca0ed0676c4290f85d45e5c
prerequisite-patch-id: 7e1ea4d184297dfaba1351a7cb23e3b6827fed46
prerequisite-patch-id: 29ff9ffe37669d23b5509bfefb0cc1f8950eaa7c
prerequisite-patch-id: e5723722ff970c585cbf559456fa4cda05f28952
prerequisite-patch-id: afdae76e12011bb158d2008b8cd1ffba084db0bb
prerequisite-patch-id: b0473d5c7af0bbeec6f6ec8dd36d8a09cc180fed
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 454c4fe7a7bcc65b2c9cc69dd5dd8e4c1d952297
prerequisite-patch-id: aff529cc205f5ab35518b8e4568e6e1319eecaa9
prerequisite-patch-id: 9f7d5c4a8ed052f5c5f2a28c862a9392d7a812cc
prerequisite-patch-id: ec9d104ba7c67488f19e6dd1a50d1ec4f682f333
prerequisite-patch-id: e7a844bd6254643d1f23827886b487c196ee9968
prerequisite-patch-id: 3c57ebf13497cf57c586b955d5ce90f2f9fd8cdc
prerequisite-patch-id: 1a327345ac71599ece0b2272fba81472f70c568c
prerequisite-change-id: 20251128-gpuvm-rust-b719cac27ad6:v5
prerequisite-patch-id: 6a8ddd47a2e1c769c65a942b3d9315e6ae5cae7e
prerequisite-patch-id: d4ee9fb80025c4fd8129c3477cc3dc36e13076cb
prerequisite-patch-id: 0ad35cd3dfcf8a8804e9fed8df3e5612cf07da69
prerequisite-patch-id: 275a900645e14169911ee00bab54553c84b48ad0
prerequisite-patch-id: 2a2de2f6c4fac065de5c459026daa2c32f340d22
prerequisite-patch-id: 9a721bc700da778f2554cf32917dda4807744520
prerequisite-message-id: <[email protected]>
prerequisite-patch-id: 6c622d1df9166ad081acba46237f8cecbf728356
prerequisite-patch-id: 769892e477c161e6c7a57519ec635b1455310f1d
prerequisite-patch-id: 3c5f5070ded87e95a8045083a55501fbbe9f6313
prerequisite-patch-id: 10fbd8695264e67421946dda4d39e29d5fe77c64

Best regards,

Signature · 1 line
Alvin Sun <[email protected]>