Skip to contentSkip to conversation

rust: sync: Add dma_fence abstractions

RFC PATCH Superseded Philipp Stanner 6 participants 15 messages Sep 18, 2025 – Sep 28, 2025

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

RFC PATCH 1/1 rust: sync: Add dma_fence abstractions

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
- An implementation of PinInit<T, Error> for all driver data.
- A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
- Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Philipp

---
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/dma_fence.c        |  23 ++
 rust/helpers/helpers.c          |   1 +
 rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 +
 rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++
 6 files changed, 420 insertions(+)
 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs
rust/bindings/bindings_helper.h +1 -0
index 84d60635e8a9..107cb6b6f4a4 100644
@@ -48,6 +48,7 @@
 #include <linux/cred.h>
 #include <linux/device/faux.h>
 #include <linux/dma-mapping.h>
+#include <linux/dma-fence.h>
 #include <linux/errname.h>
 #include <linux/ethtool.h>
 #include <linux/file.h>
rust/helpers/dma_fence.c new file +23 -0
new file mode 100644
index 000000000000..a9fc4829bbae
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/dma-fence.h>
+
+void rust_helper_dma_fence_get(struct dma_fence *f)
+{
+	dma_fence_get(f);
+}
+
+void rust_helper_dma_fence_put(struct dma_fence *f)
+{
+	dma_fence_put(f);
+}
+
+bool rust_helper_dma_fence_begin_signalling(void)
+{
+	return dma_fence_begin_signalling();
+}
+
+void rust_helper_dma_fence_end_signalling(bool cookie)
+{
+	dma_fence_end_signalling(cookie);
+}
rust/helpers/helpers.c +1 -0
index 7cf7fe95e41d..99a7f7834c03 100644
@@ -20,6 +20,7 @@
 #include "cred.c"
 #include "device.c"
 #include "dma.c"
+#include "dma_fence.c"
 #include "drm.c"
 #include "err.c"
 #include "fs.c"
rust/helpers/spinlock.c +5 -0
index 42c4bf01a23e..017ac447ebbd 100644
@@ -16,6 +16,11 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
 #endif /* CONFIG_DEBUG_SPINLOCK */
 }
 
+void rust_helper_spin_lock_init(spinlock_t *lock)
+{
+	spin_lock_init(lock);
+}
+
 void rust_helper_spin_lock(spinlock_t *lock)
 {
 	spin_lock(lock);
rust/kernel/sync.rs +2 -0
index 00f9b558a3ad..6e59526020bc 100644
@@ -12,6 +12,7 @@
 mod arc;
 pub mod aref;
 pub mod completion;
+pub mod dma_fence;
 mod condvar;
 pub mod lock;
 mod locked_by;
@@ -20,6 +21,7 @@
 
 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use completion::Completion;
+pub use dma_fence::{DmaFence, DmaFenceCtx, DmaFenceNames, DmaFenceCb, DmaFenceCbFunc};
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
 pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
rust/kernel/sync/dma_fence.rs new file +388 -0

388 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#1 lore raw

Oh, now it gets funny.

Adding Tvrtko as well since he recently looked into making dma_fences more robust regarding context lifetime.

Quoted 59 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
- An implementation of PinInit<T, Error> for all driver data.
- A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
- Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Quoted 3 lines

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

I was hoping to remove that anyway. Maybe it's time to do so now.

Anyway I will need some time to look into it deeper.

Regards,
Christian.

Quoted 501 lines

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Philipp

---
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/dma_fence.c        |  23 ++
 rust/helpers/helpers.c          |   1 +
 rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 +
 rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++
 6 files changed, 420 insertions(+)
 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs
rust/bindings/bindings_helper.h +1 -0
index 84d60635e8a9..107cb6b6f4a4 100644
@@ -48,6 +48,7 @@
 #include <linux/cred.h>
 #include <linux/device/faux.h>
 #include <linux/dma-mapping.h>
+#include <linux/dma-fence.h>
 #include <linux/errname.h>
 #include <linux/ethtool.h>
 #include <linux/file.h>
rust/helpers/dma_fence.c new file +23 -0
new file mode 100644
index 000000000000..a9fc4829bbae
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/dma-fence.h>
+
+void rust_helper_dma_fence_get(struct dma_fence *f)
+{
+	dma_fence_get(f);
+}
+
+void rust_helper_dma_fence_put(struct dma_fence *f)
+{
+	dma_fence_put(f);
+}
+
+bool rust_helper_dma_fence_begin_signalling(void)
+{
+	return dma_fence_begin_signalling();
+}
+
+void rust_helper_dma_fence_end_signalling(bool cookie)
+{
+	dma_fence_end_signalling(cookie);
+}
rust/helpers/helpers.c +1 -0
index 7cf7fe95e41d..99a7f7834c03 100644
@@ -20,6 +20,7 @@
 #include "cred.c"
 #include "device.c"
 #include "dma.c"
+#include "dma_fence.c"
 #include "drm.c"
 #include "err.c"
 #include "fs.c"
rust/helpers/spinlock.c +5 -0
index 42c4bf01a23e..017ac447ebbd 100644
@@ -16,6 +16,11 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
 #endif /* CONFIG_DEBUG_SPINLOCK */
 }
 
+void rust_helper_spin_lock_init(spinlock_t *lock)
+{
+	spin_lock_init(lock);
+}
+
 void rust_helper_spin_lock(spinlock_t *lock)
 {
 	spin_lock(lock);
rust/kernel/sync.rs +2 -0
index 00f9b558a3ad..6e59526020bc 100644
@@ -12,6 +12,7 @@
 mod arc;
 pub mod aref;
 pub mod completion;
+pub mod dma_fence;
 mod condvar;
 pub mod lock;
 mod locked_by;
@@ -20,6 +21,7 @@
 
 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use completion::Completion;
+pub use dma_fence::{DmaFence, DmaFenceCtx, DmaFenceNames, DmaFenceCb, DmaFenceCbFunc};
 pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
 pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
rust/kernel/sync/dma_fence.rs new file +388 -0
new file mode 100644
index 000000000000..a00bb10b2208
@@ -0,0 +1,388 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! DmaFence support.
+//!
+//! Reference: <https://docs.kernel.org/driver-api/dma-buf.html#c.dma_fence>
+//!
+//! C header: [`include/linux/dma-fence.h`](srctree/include/linux/dma-fence.h)
+
+use crate::{
+    bindings,
+    prelude::*,
+    str::CStr,
+    types::ForeignOwnable,
+    types::{ARef, AlwaysRefCounted, Opaque},
+};
+
+use core::{
+    ptr::{drop_in_place, NonNull},
+    sync::atomic::{AtomicU64, Ordering},
+};
+
+use kernel::sync::{Arc, ArcBorrow};
+
+/// The C dma_fence backend functions ask for certain name parameters at times. In order to avoid
+/// storing those names in [`DmaFence`] (there can be millions of fences at the same time, wasting
+/// much memory), the driver must provide associated constants which the C backend will access,
+/// ultimately.
+pub trait DmaFenceNames {
+    /// The driver's name.
+    const DRIVER_NAME: &CStr;
+    /// The name of the timeline the fence is associated with.
+    const TIMELINE_NAME: &CStr;
+}
+
+/// Defines the callback function the dma-fence backend will call once the fence gets signalled.
+pub trait DmaFenceCbFunc {
+    /// The callback function. `cb` is a container of the data which the driver passed in
+    /// [`DmaFence::register_callback`].
+    fn callback(cb: Pin<KBox<DmaFenceCb<Self>>>)
+    where
+        Self: Sized;
+}
+
+/// Container for driver data which the driver gets back in its callback once the fence gets
+/// signalled.
+#[pin_data]
+pub struct DmaFenceCb<T: DmaFenceCbFunc> {
+    /// C struct needed for the backend.
+    #[pin]
+    inner: Opaque<bindings::dma_fence_cb>,
+    /// Driver data.
+    #[pin]
+    pub data: T,
+}
+
+impl<T: DmaFenceCbFunc + 'static> DmaFenceCb<T> {
+    fn new(data: impl PinInit<T>) -> Result<Pin<KBox<Self>>> {
+        let cb = try_pin_init!(Self {
+            inner: Opaque::zeroed(), // This gets initialized by the C backend.
+            data <- data,
+        });
+
+        KBox::pin_init(cb, GFP_KERNEL)
+    }
+
+    /// Callback for the C dma_fence backend.
+    ///
+    /// # Safety
+    /// All data used and cast in this function was validly created by
+    /// [`DmaFence::register_callback`] and isn't modified by the C backend until this callback
+    /// here has run.
+    unsafe extern "C" fn callback(
+        _fence_ptr: *mut bindings::dma_fence,
+        cb_ptr: *mut bindings::dma_fence_cb,
+    ) {
+        let cb_ptr = Opaque::cast_from(cb_ptr);
+
+        // SAFETY: The constructor guarantees that `cb_ptr` is always `inner` of a DmaFenceCb.
+        let cb_ptr = unsafe { crate::container_of!(cb_ptr, Self, inner) }.cast_mut() as *mut c_void;
+        // SAFETY: `cp_ptr` is the heap memory of a Pin<Kbox<Self>> because it was created by
+        // invoking ForeignOwnable::into_foreign() on such an instance.
+        let cb = unsafe { <Pin<KBox<Self>> as ForeignOwnable>::from_foreign(cb_ptr) };
+
+        // Pass ownership back over to the driver.
+        T::callback(cb);
+    }
+}
+
+/// A dma-fence context. A fence context takes care of associating related fences with each other,
+/// providing each with raising sequence numbers and a common identifier.
+#[pin_data]
+pub struct DmaFenceCtx {
+    /// An opaque spinlock. Only ever passed to the C backend, never used by Rust.
+    #[pin]
+    lock: Opaque<bindings::spinlock_t>,
+    /// The fence context number.
+    nr: u64,
+    /// The sequence number for the next fence created.
+    seqno: AtomicU64,
+}
+
+impl DmaFenceCtx {
+    /// Create a new `DmaFenceCtx`.
+    pub fn new() -> Result<Arc<Self>> {
+        let ctx = pin_init!(Self {
+            // Feed in a non-Rust spinlock for now, since the Rust side never needs the lock.
+            lock <- Opaque::ffi_init(|slot: *mut bindings::spinlock| {
+                // SAFETY: `slot` is a valid pointer to an uninitialized `struct spinlock_t`.
+                unsafe { bindings::spin_lock_init(slot) };
+            }),
+            // SAFETY: `dma_fence_context_alloc()` merely works on a global atomic. Parameter `1`
+            // is the number of contexts we want to allocate.
+            nr: unsafe { bindings::dma_fence_context_alloc(1) },
+            seqno: AtomicU64::new(0),
+        });
+
+        Arc::pin_init(ctx, GFP_KERNEL)
+    }
+
+    fn get_new_fence_seqno(&self) -> u64 {
+        self.seqno.fetch_add(1, Ordering::Relaxed)
+    }
+}
+
+impl ArcBorrow<'_, DmaFenceCtx> {
+    /// Create a new fence, consuming `data`.
+    ///
+    /// The fence will increment the refcount of the fence context associated with this
+    /// [`DmaFenceCtx`].
+    pub fn new_fence<T: DmaFenceNames>(
+        &mut self,
+        data: impl PinInit<T>,
+    ) -> Result<ARef<DmaFence<T>>> {
+        let fctx: Arc<DmaFenceCtx> = (*self).into();
+        let seqno: u64 = fctx.get_new_fence_seqno();
+
+        // TODO: Should we reset seqno in case of failure?
+        // Pass `fctx` by value so that the fence will hold a reference to the DmaFenceCtx as long
+        // as it lives.
+        DmaFence::new(fctx, data, &self.lock, self.nr, seqno)
+    }
+}
+
+/// A synchronization primitive mainly for GPU drivers.
+///
+/// DmaFences are always reference counted. The typical use case is that one side registers
+/// callbacks on the fence which will perform a certain action (such as queueing work) once the
+/// other side signals the fence.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::c_str;
+/// use kernel::sync::{Arc, ArcBorrow, DmaFence, DmaFenceCtx, DmaFenceNames, DmaFenceCb, DmaFenceCbFunc};
+/// use core::sync::atomic::{self, AtomicBool};
+///
+/// static mut CHECKER: AtomicBool = AtomicBool::new(false);
+///
+/// struct CallbackData {
+///     i: u32,
+/// }
+///
+/// impl CallbackData {
+///     fn new() -> Self {
+///         Self { i: 9 }
+///     }
+/// }
+///
+/// impl DmaFenceCbFunc for CallbackData {
+///     fn callback(cb: Pin<KBox<DmaFenceCb<Self>>>) where Self: Sized {
+///         assert_eq!(cb.data.i, 9);
+///         // SAFETY: Just to have an easy way for testing. This cannot race with the checker
+///         // because the fence signalling callbacks are executed synchronously.
+///         unsafe { CHECKER.store(true, atomic::Ordering::Relaxed); }
+///     }
+/// }
+///
+/// struct DriverData {
+///     i: u32,
+/// }
+///
+/// impl DriverData {
+///     fn new() -> Self {
+///         Self { i: 5 }
+///     }
+/// }
+///
+/// impl DmaFenceNames for DriverData {
+///     const DRIVER_NAME: &CStr = c_str!("DUMMY_DRIVER");
+///     const TIMELINE_NAME: &CStr = c_str!("DUMMY_TIMELINE");
+/// }
+///
+/// let data = DriverData::new();
+/// let fctx = DmaFenceCtx::new()?;
+///
+/// let mut fence = fctx.as_arc_borrow().new_fence(data)?;
+///
+/// let cb_data = CallbackData::new();
+/// fence.register_callback(cb_data);
+/// // fence.begin_signalling();
+/// fence.signal()?;
+/// // Now check wehether the callback was actually executed.
+/// // SAFETY: `fence.signal()` above works sequentially. We just check here whether the signalling
+/// // actually did set the boolean correctly.
+/// unsafe { assert_eq!(CHECKER.load(atomic::Ordering::Relaxed), true); }
+///
+/// Ok::<(), Error>(())
+/// ```
+#[pin_data]
+pub struct DmaFence<T> {
+    /// The actual dma_fence passed to C.
+    #[pin]
+    inner: Opaque<bindings::dma_fence>,
+    /// User data.
+    #[pin]
+    data: T,
+    /// Marks whether the fence is currently in the signalling critical section.
+    signalling: bool,
+    /// A boolean needed for the C backend's lockdep guard.
+    signalling_cookie: bool,
+    /// A reference to the associated [`DmaFenceCtx`] so that it cannot be dropped while there are
+    /// still fences around.
+    fctx: Arc<DmaFenceCtx>,
+}
+
+// SAFETY: `DmaFence` is safe to be sent to any task.
+unsafe impl<T> Send for DmaFence<T> {}
+
+// SAFETY: `DmaFence` is safe to be accessed concurrently.
+unsafe impl<T> Sync for DmaFence<T> {}
+
+// SAFETY: These implement the C backends refcounting methods which are proven to work correctly.
+unsafe impl<T: DmaFenceNames> AlwaysRefCounted for DmaFence<T> {
+    fn inc_ref(&self) {
+        // SAFETY: `self.as_raw()` is a pointer to a valid `struct dma_fence`.
+        unsafe { bindings::dma_fence_get(self.as_raw()) }
+    }
+
+    /// # Safety
+    ///
+    /// `ptr`must be a valid pointer to a [`DmaFence`].
+    unsafe fn dec_ref(ptr: NonNull<Self>) {
+        // SAFETY: `ptr` is never a NULL pointer; and when `dec_ref()` is called
+        // the fence is by definition still valid.
+        let fence = unsafe { (*ptr.as_ptr()).inner.get() };
+
+        // SAFETY: Valid because `fence` was created validly above.
+        unsafe { bindings::dma_fence_put(fence) }
+    }
+}
+
+impl<T: DmaFenceNames> DmaFence<T> {
+    // TODO: There could be a subtle potential problem here? The LLVM compiler backend can create
+    // several versions of this constant. Their content would be identical, but their addresses
+    // different.
+    const OPS: bindings::dma_fence_ops = Self::ops_create();
+
+    /// Create an initializer for a new [`DmaFence`].
+    fn new(
+        fctx: Arc<DmaFenceCtx>,
+        data: impl PinInit<T>, // TODO: The driver data should implement PinInit<T, Error>
+        lock: &Opaque<bindings::spinlock_t>,
+        context: u64,
+        seqno: u64,
+    ) -> Result<ARef<Self>> {
+        let fence = pin_init!(Self {
+            inner <- Opaque::ffi_init(|slot: *mut bindings::dma_fence| {
+                let lock_ptr = &raw const (*lock);
+                // SAFETY: `slot` is a valid pointer to an uninitialized `struct dma_fence`.
+                // `lock_ptr` is a pointer to the spinlock of the fence context, which is shared
+                // among all the fences. This can't become a UAF because each fence takes a
+                // reference of the fence context.
+                unsafe { bindings::dma_fence_init(slot, &Self::OPS, Opaque::cast_into(lock_ptr), context, seqno) };
+            }),
+            data <- data,
+            signalling: false,
+            signalling_cookie: false,
+            fctx: fctx,
+        });
+
+        let b = KBox::pin_init(fence, GFP_KERNEL)?;
+
+        // SAFETY: We don't move the contents of `b` anywhere here. After unwrapping it, ARef will
+        // take care of preventing memory moves.
+        let rawptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(b) });
+
+        // SAFETY: `rawptr` was created validly above.
+        let aref = unsafe { ARef::from_raw(NonNull::new_unchecked(rawptr)) };
+
+        Ok(aref)
+    }
+
+    /// Mark the beginning of a DmaFence signalling critical section. Should be called once a fence
+    /// gets published.
+    ///
+    /// The signalling critical section is marked as finished automatically once the fence signals.
+    pub fn begin_signalling(&mut self) {
+        // FIXME: this needs to be mutable, obviously, but we can't borrow mutably. *sigh*
+        self.signalling = true;
+        // TODO: Should we warn if a user tries to do this several times for the same
+        // fence? And should we ignore the request if the fence is already signalled?
+
+        // SAFETY: `dma_fence_begin_signalling()` works on global lockdep data and calling it is
+        // always safe.
+        self.signalling_cookie = unsafe { bindings::dma_fence_begin_signalling() };
+    }
+
+    const fn ops_create() -> bindings::dma_fence_ops {
+        // SAFETY: Zeroing out memory on the stack is always safe.
+        let mut ops: bindings::dma_fence_ops = unsafe { core::mem::zeroed() };
+
+        ops.get_driver_name = Some(Self::get_driver_name);
+        ops.get_timeline_name = Some(Self::get_timeline_name);
+        ops.release = Some(Self::release);
+
+        ops
+    }
+
+    extern "C" fn get_driver_name(_ptr: *mut bindings::dma_fence) -> *const c_char {
+        T::DRIVER_NAME.as_ptr()
+    }
+
+    extern "C" fn get_timeline_name(_ptr: *mut bindings::dma_fence) -> *const c_char {
+        T::TIMELINE_NAME.as_ptr()
+    }
+
+    /// The release function called by the C backend once the refcount drops to 0. We use this to
+    /// drop the Rust dma-fence, too. Since [`DmaFence`] implements [`AlwaysRefCounted`], this is
+    /// perfectly safe and a convenient way to concile the two release mechanisms of C and Rust.
+    unsafe extern "C" fn release(ptr: *mut bindings::dma_fence) {
+        let ptr = Opaque::cast_from(ptr);
+
+        // SAFETY: The constructor guarantees that `ptr` is always the inner fence of a DmaFence.
+        let fence = unsafe { crate::container_of!(ptr, Self, inner) }.cast_mut();
+
+        // SAFETY: See above. Also, the release callback will only be called once, when the
+        // refcount drops to 0, and when that happens the fence is by definition still valid.
+        unsafe { drop_in_place(fence) };
+    }
+
+    /// Signal the fence. This will invoke all registered callbacks.
+    pub fn signal(&self) -> Result {
+        // SAFETY: `self` is refcounted.
+        let ret = unsafe { bindings::dma_fence_signal(self.as_raw()) };
+        if ret != 0 {
+            return Err(Error::from_errno(ret));
+        }
+
+        if self.signalling {
+            // SAFETY: `dma_fence_end_signalling()` works on global lockdep data. The only
+            // parameter is a boolean passed by value.
+            unsafe { bindings::dma_fence_end_signalling(self.signalling_cookie) };
+        }
+
+        Ok(())
+    }
+
+    /// Register a callback on a [`DmaFence`]. The callback will be invoked in the fence's
+    /// signalling path, i.e., critical section.
+    ///
+    /// Consumes `data`. `data` is passed back to the implemented callback function when the fence
+    /// gets signalled.
+    pub fn register_callback<U: DmaFenceCbFunc + 'static>(&self, data: impl PinInit<U>) -> Result {
+        let cb = DmaFenceCb::new(data)?;
+        let ptr = cb.into_foreign() as *mut DmaFenceCb<U>;
+        // SAFETY: `ptr` was created validly directly above.
+        let inner_cb = unsafe { (*ptr).inner.get() };
+
+        // SAFETY: `self.as_raw()` is valid because `self` is refcounted, `inner_cb` was created
+        // validly above and was turned into a ForeignOwnable, so it won't be dropped. `callback`
+        // has static life time.
+        let ret = unsafe {
+            bindings::dma_fence_add_callback(
+                self.as_raw(),
+                inner_cb,
+                Some(DmaFenceCb::<U>::callback),
+            )
+        };
+        if ret != 0 {
+            return Err(Error::from_errno(ret));
+        }
+        Ok(())
+    }
+
+    fn as_raw(&self) -> *mut bindings::dma_fence {
+        self.inner.get()
+    }
+}
#2 lore raw
Quoted 1 line · Christian König

Oh, now it gets funny.

For the Rust compiler maybe ^^'
dma_fence is a tough nut to crack because of all its callbacks and strict rules <.<

Quoted 63 lines

Adding Tvrtko as well since he recently looked into making dma_fences more robust regarding context lifetime.

Quoted 59 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
  - An implementation of PinInit<T, Error> for all driver data.
  - A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
  - Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Quoted 5 lines
Quoted 3 lines

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

I was hoping to remove that anyway. Maybe it's time to do so now.

Remove what, the spinlock pointer from struct dma_fence? Or were you trying to quote the section above about dma_fence_begin_signalling()?

If the lock were to be removed, would then the driver be responsible for locking through its fence context?

P.

Quoted 508 lines

Anyway I will need some time to look into it deeper.

Regards,
Christian.

Quoted 501 lines

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Philipp
---
 rust/bindings/bindings_helper.h |   1 + rust/helpers/dma_fence.c        |  23 ++ rust/helpers/helpers.c          |   1 + rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 + rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

 6 files changed, 420 insertions(+)

 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

rust/bindings/bindings_helper.h +1 -0
index 84d60635e8a9..107cb6b6f4a4 100644
@@ -48,6 +48,7 @@
  #include <linux/cred.h>
  #include <linux/device/faux.h>
  #include <linux/dma-mapping.h>
+#include <linux/dma-fence.h>
  #include <linux/errname.h>
  #include <linux/ethtool.h>
  #include <linux/file.h>
rust/helpers/dma_fence.c new file +23 -0
new file mode 100644
index 000000000000..a9fc4829bbae
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/dma-fence.h>
+
+void rust_helper_dma_fence_get(struct dma_fence *f)
+{
+	dma_fence_get(f);
+}
+
+void rust_helper_dma_fence_put(struct dma_fence *f)
+{
+	dma_fence_put(f);
+}
+
+bool rust_helper_dma_fence_begin_signalling(void)
+{
+	return dma_fence_begin_signalling();
+}
+
+void rust_helper_dma_fence_end_signalling(bool cookie)
+{
+	dma_fence_end_signalling(cookie);
+}
rust/helpers/helpers.c +1 -0
index 7cf7fe95e41d..99a7f7834c03 100644
@@ -20,6 +20,7 @@
  #include "cred.c"
  #include "device.c"
  #include "dma.c"
+#include "dma_fence.c"
  #include "drm.c"
  #include "err.c"
  #include "fs.c"
rust/helpers/spinlock.c +0 -0
index 42c4bf01a23e..017ac447ebbd 100644
@@ -16,6 +16,11 @@ void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
  #endif /* CONFIG_DEBUG_SPINLOCK */
  }

rust/helpers/spinlock.c +5 -0
+void rust_helper_spin_lock_init(spinlock_t *lock)
+{
+	spin_lock_init(lock);
+}
+

 void rust_helper_spin_lock(spinlock_t *lock)
 {
  spin_lock(lock);

rust/kernel/sync.rs +1 -0
index 00f9b558a3ad..6e59526020bc 100644
@@ -12,6 +12,7 @@
  mod arc;
  pub mod aref;
  pub mod completion;
+pub mod dma_fence;
  mod condvar;
  pub mod lock;
  mod locked_by;
@@ -20,6 +21,7 @@


 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use completion::Completion;
+pub use dma_fence::{DmaFence, DmaFenceCtx, DmaFenceNames, DmaFenceCb, DmaFenceCbFunc}; pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
 pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy}; pub use lock::mutex::{new_mutex, Mutex, MutexGuard};

rust/kernel/sync/dma_fence.rs new file +388 -0
new file mode 100644
index 000000000000..a00bb10b2208
@@ -0,0 +1,388 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! DmaFence support.
+//!
+//! Reference: <https://docs.kernel.org/driver-api/dma-buf.html#c.dma_fence>
+//!
+//! C header: [`include/linux/dma-fence.h`](srctree/include/linux/dma-fence.h)
+
+use crate::{
+    bindings,
+    prelude::*,
+    str::CStr,
+    types::ForeignOwnable,
+    types::{ARef, AlwaysRefCounted, Opaque},
+};
+
+use core::{
+    ptr::{drop_in_place, NonNull},
+    sync::atomic::{AtomicU64, Ordering},
+};
+
+use kernel::sync::{Arc, ArcBorrow};
+
+/// The C dma_fence backend functions ask for certain name parameters at times. In order to avoid
+/// storing those names in [`DmaFence`] (there can be millions of fences at the same time, wasting
+/// much memory), the driver must provide associated constants which the C backend will access,
+/// ultimately.
+pub trait DmaFenceNames {
+    /// The driver's name.
+    const DRIVER_NAME: &CStr;
+    /// The name of the timeline the fence is associated with.
+    const TIMELINE_NAME: &CStr;
+}
+
+/// Defines the callback function the dma-fence backend will call once the fence gets signalled.
+pub trait DmaFenceCbFunc {
+    /// The callback function. `cb` is a container of the data which the driver passed in
+    /// [`DmaFence::register_callback`].
+    fn callback(cb: Pin<KBox<DmaFenceCb<Self>>>)
+    where
+        Self: Sized;
+}
+
+/// Container for driver data which the driver gets back in its callback once the fence gets
+/// signalled.
+#[pin_data]
+pub struct DmaFenceCb<T: DmaFenceCbFunc> {
+    /// C struct needed for the backend.
+    #[pin]
+    inner: Opaque<bindings::dma_fence_cb>,
+    /// Driver data.
+    #[pin]
+    pub data: T,
+}
+
+impl<T: DmaFenceCbFunc + 'static> DmaFenceCb<T> {
+    fn new(data: impl PinInit<T>) -> Result<Pin<KBox<Self>>> {
+        let cb = try_pin_init!(Self {
+            inner: Opaque::zeroed(), // This gets initialized by the C backend.
+            data <- data,
+        });
+
+        KBox::pin_init(cb, GFP_KERNEL)
+    }
+
+    /// Callback for the C dma_fence backend.
+    ///
+    /// # Safety
+    /// All data used and cast in this function was validly created by
+    /// [`DmaFence::register_callback`] and isn't modified by the C backend until this callback
+    /// here has run.
+    unsafe extern "C" fn callback(
+        _fence_ptr: *mut bindings::dma_fence,
+        cb_ptr: *mut bindings::dma_fence_cb,
+    ) {
+        let cb_ptr = Opaque::cast_from(cb_ptr);
+
+        // SAFETY: The constructor guarantees that `cb_ptr` is always `inner` of a DmaFenceCb.
+        let cb_ptr = unsafe { crate::container_of!(cb_ptr, Self, inner) }.cast_mut() as *mut c_void;
+        // SAFETY: `cp_ptr` is the heap memory of a Pin<Kbox<Self>> because it was created by
+        // invoking ForeignOwnable::into_foreign() on such an instance.
+        let cb = unsafe { <Pin<KBox<Self>> as ForeignOwnable>::from_foreign(cb_ptr) };
+
+        // Pass ownership back over to the driver.
+        T::callback(cb);
+    }
+}
+
+/// A dma-fence context. A fence context takes care of associating related fences with each other,
+/// providing each with raising sequence numbers and a common identifier.
+#[pin_data]
+pub struct DmaFenceCtx {
+    /// An opaque spinlock. Only ever passed to the C backend, never used by Rust.
+    #[pin]
+    lock: Opaque<bindings::spinlock_t>,
+    /// The fence context number.
+    nr: u64,
+    /// The sequence number for the next fence created.
+    seqno: AtomicU64,
+}
+
+impl DmaFenceCtx {
+    /// Create a new `DmaFenceCtx`.
+    pub fn new() -> Result<Arc<Self>> {
+        let ctx = pin_init!(Self {
+            // Feed in a non-Rust spinlock for now, since the Rust side never needs the lock.
+            lock <- Opaque::ffi_init(|slot: *mut bindings::spinlock| {
+                // SAFETY: `slot` is a valid pointer to an uninitialized `struct spinlock_t`.
+                unsafe { bindings::spin_lock_init(slot) };
+            }),
+            // SAFETY: `dma_fence_context_alloc()` merely works on a global atomic. Parameter `1`
+            // is the number of contexts we want to allocate.
+            nr: unsafe { bindings::dma_fence_context_alloc(1) },
+            seqno: AtomicU64::new(0),
+        });
+
+        Arc::pin_init(ctx, GFP_KERNEL)
+    }
+
+    fn get_new_fence_seqno(&self) -> u64 {
+        self.seqno.fetch_add(1, Ordering::Relaxed)
+    }
+}
+
+impl ArcBorrow<'_, DmaFenceCtx> {
+    /// Create a new fence, consuming `data`.
+    ///
+    /// The fence will increment the refcount of the fence context associated with this
+    /// [`DmaFenceCtx`].
+    pub fn new_fence<T: DmaFenceNames>(
+        &mut self,
+        data: impl PinInit<T>,
+    ) -> Result<ARef<DmaFence<T>>> {
+        let fctx: Arc<DmaFenceCtx> = (*self).into();
+        let seqno: u64 = fctx.get_new_fence_seqno();
+
+        // TODO: Should we reset seqno in case of failure?
+        // Pass `fctx` by value so that the fence will hold a reference to the DmaFenceCtx as long
+        // as it lives.
+        DmaFence::new(fctx, data, &self.lock, self.nr, seqno)
+    }
+}
+
+/// A synchronization primitive mainly for GPU drivers.
+///
+/// DmaFences are always reference counted. The typical use case is that one side registers
+/// callbacks on the fence which will perform a certain action (such as queueing work) once the
+/// other side signals the fence.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::c_str;
+/// use kernel::sync::{Arc, ArcBorrow, DmaFence, DmaFenceCtx, DmaFenceNames, DmaFenceCb, DmaFenceCbFunc};
+/// use core::sync::atomic::{self, AtomicBool};
+///
+/// static mut CHECKER: AtomicBool = AtomicBool::new(false);
+///
+/// struct CallbackData {
+///     i: u32,
+/// }
+///
+/// impl CallbackData {
+///     fn new() -> Self {
+///         Self { i: 9 }
+///     }
+/// }
+///
+/// impl DmaFenceCbFunc for CallbackData {
+///     fn callback(cb: Pin<KBox<DmaFenceCb<Self>>>) where Self: Sized {
+///         assert_eq!(cb.data.i, 9);
+///         // SAFETY: Just to have an easy way for testing. This cannot race with the checker
+///         // because the fence signalling callbacks are executed synchronously.
+///         unsafe { CHECKER.store(true, atomic::Ordering::Relaxed); }
+///     }
+/// }
+///
+/// struct DriverData {
+///     i: u32,
+/// }
+///
+/// impl DriverData {
+///     fn new() -> Self {
+///         Self { i: 5 }
+///     }
+/// }
+///
+/// impl DmaFenceNames for DriverData {
+///     const DRIVER_NAME: &CStr = c_str!("DUMMY_DRIVER");
+///     const TIMELINE_NAME: &CStr = c_str!("DUMMY_TIMELINE");
+/// }
+///
+/// let data = DriverData::new();
+/// let fctx = DmaFenceCtx::new()?;
+///
+/// let mut fence = fctx.as_arc_borrow().new_fence(data)?;
+///
+/// let cb_data = CallbackData::new();
+/// fence.register_callback(cb_data);
+/// // fence.begin_signalling();
+/// fence.signal()?;
+/// // Now check wehether the callback was actually executed.
+/// // SAFETY: `fence.signal()` above works sequentially. We just check here whether the signalling
+/// // actually did set the boolean correctly.
+/// unsafe { assert_eq!(CHECKER.load(atomic::Ordering::Relaxed), true); }
+///
+/// Ok::<(), Error>(())
+/// ```
+#[pin_data]
+pub struct DmaFence<T> {
+    /// The actual dma_fence passed to C.
+    #[pin]
+    inner: Opaque<bindings::dma_fence>,
+    /// User data.
+    #[pin]
+    data: T,
+    /// Marks whether the fence is currently in the signalling critical section.
+    signalling: bool,
+    /// A boolean needed for the C backend's lockdep guard.
+    signalling_cookie: bool,
+    /// A reference to the associated [`DmaFenceCtx`] so that it cannot be dropped while there are
+    /// still fences around.
+    fctx: Arc<DmaFenceCtx>,
+}
+
+// SAFETY: `DmaFence` is safe to be sent to any task.
+unsafe impl<T> Send for DmaFence<T> {}
+
+// SAFETY: `DmaFence` is safe to be accessed concurrently.
+unsafe impl<T> Sync for DmaFence<T> {}
+
+// SAFETY: These implement the C backends refcounting methods which are proven to work correctly.
+unsafe impl<T: DmaFenceNames> AlwaysRefCounted for DmaFence<T> {
+    fn inc_ref(&self) {
+        // SAFETY: `self.as_raw()` is a pointer to a valid `struct dma_fence`.
+        unsafe { bindings::dma_fence_get(self.as_raw()) }
+    }
+
+    /// # Safety
+    ///
+    /// `ptr`must be a valid pointer to a [`DmaFence`].
+    unsafe fn dec_ref(ptr: NonNull<Self>) {
+        // SAFETY: `ptr` is never a NULL pointer; and when `dec_ref()` is called
+        // the fence is by definition still valid.
+        let fence = unsafe { (*ptr.as_ptr()).inner.get() };
+
+        // SAFETY: Valid because `fence` was created validly above.
+        unsafe { bindings::dma_fence_put(fence) }
+    }
+}
+
+impl<T: DmaFenceNames> DmaFence<T> {
+    // TODO: There could be a subtle potential problem here? The LLVM compiler backend can create
+    // several versions of this constant. Their content would be identical, but their addresses
+    // different.
+    const OPS: bindings::dma_fence_ops = Self::ops_create();
+
+    /// Create an initializer for a new [`DmaFence`].
+    fn new(
+        fctx: Arc<DmaFenceCtx>,
+        data: impl PinInit<T>, // TODO: The driver data should implement PinInit<T, Error>
+        lock: &Opaque<bindings::spinlock_t>,
+        context: u64,
+        seqno: u64,
+    ) -> Result<ARef<Self>> {
+        let fence = pin_init!(Self {
+            inner <- Opaque::ffi_init(|slot: *mut bindings::dma_fence| {
+                let lock_ptr = &raw const (*lock);
+                // SAFETY: `slot` is a valid pointer to an uninitialized `struct dma_fence`.
+                // `lock_ptr` is a pointer to the spinlock of the fence context, which is shared
+                // among all the fences. This can't become a UAF because each fence takes a
+                // reference of the fence context.
+                unsafe { bindings::dma_fence_init(slot, &Self::OPS, Opaque::cast_into(lock_ptr), context, seqno) };
+            }),
+            data <- data,
+            signalling: false,
+            signalling_cookie: false,
+            fctx: fctx,
+        });
+
+        let b = KBox::pin_init(fence, GFP_KERNEL)?;
+
+        // SAFETY: We don't move the contents of `b` anywhere here. After unwrapping it, ARef will
+        // take care of preventing memory moves.
+        let rawptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(b) });
+
+        // SAFETY: `rawptr` was created validly above.
+        let aref = unsafe { ARef::from_raw(NonNull::new_unchecked(rawptr)) };
+
+        Ok(aref)
+    }
+
+    /// Mark the beginning of a DmaFence signalling critical section. Should be called once a fence
+    /// gets published.
+    ///
+    /// The signalling critical section is marked as finished automatically once the fence signals.
+    pub fn begin_signalling(&mut self) {
+        // FIXME: this needs to be mutable, obviously, but we can't borrow mutably. *sigh*
+        self.signalling = true;
+        // TODO: Should we warn if a user tries to do this several times for the same
+        // fence? And should we ignore the request if the fence is already signalled?
+
+        // SAFETY: `dma_fence_begin_signalling()` works on global lockdep data and calling it is
+        // always safe.
+        self.signalling_cookie = unsafe { bindings::dma_fence_begin_signalling() };
+    }
+
+    const fn ops_create() -> bindings::dma_fence_ops {
+        // SAFETY: Zeroing out memory on the stack is always safe.
+        let mut ops: bindings::dma_fence_ops = unsafe { core::mem::zeroed() };
+
+        ops.get_driver_name = Some(Self::get_driver_name);
+        ops.get_timeline_name = Some(Self::get_timeline_name);
+        ops.release = Some(Self::release);
+
+        ops
+    }
+
+    extern "C" fn get_driver_name(_ptr: *mut bindings::dma_fence) -> *const c_char {
+        T::DRIVER_NAME.as_ptr()
+    }
+
+    extern "C" fn get_timeline_name(_ptr: *mut bindings::dma_fence) -> *const c_char {
+        T::TIMELINE_NAME.as_ptr()
+    }
+
+    /// The release function called by the C backend once the refcount drops to 0. We use this to
+    /// drop the Rust dma-fence, too. Since [`DmaFence`] implements [`AlwaysRefCounted`], this is
+    /// perfectly safe and a convenient way to concile the two release mechanisms of C and Rust.
+    unsafe extern "C" fn release(ptr: *mut bindings::dma_fence) {
+        let ptr = Opaque::cast_from(ptr);
+
+        // SAFETY: The constructor guarantees that `ptr` is always the inner fence of a DmaFence.
+        let fence = unsafe { crate::container_of!(ptr, Self, inner) }.cast_mut();
+
+        // SAFETY: See above. Also, the release callback will only be called once, when the
+        // refcount drops to 0, and when that happens the fence is by definition still valid.
+        unsafe { drop_in_place(fence) };
+    }
+
+    /// Signal the fence. This will invoke all registered callbacks.
+    pub fn signal(&self) -> Result {
+        // SAFETY: `self` is refcounted.
+        let ret = unsafe { bindings::dma_fence_signal(self.as_raw()) };
+        if ret != 0 {
+            return Err(Error::from_errno(ret));
+        }
+
+        if self.signalling {
+            // SAFETY: `dma_fence_end_signalling()` works on global lockdep data. The only
+            // parameter is a boolean passed by value.
+            unsafe { bindings::dma_fence_end_signalling(self.signalling_cookie) };
+        }
+
+        Ok(())
+    }
+
+    /// Register a callback on a [`DmaFence`]. The callback will be invoked in the fence's
+    /// signalling path, i.e., critical section.
+    ///
+    /// Consumes `data`. `data` is passed back to the implemented callback function when the fence
+    /// gets signalled.
+    pub fn register_callback<U: DmaFenceCbFunc + 'static>(&self, data: impl PinInit<U>) -> Result {
+        let cb = DmaFenceCb::new(data)?;
+        let ptr = cb.into_foreign() as *mut DmaFenceCb<U>;
+        // SAFETY: `ptr` was created validly directly above.
+        let inner_cb = unsafe { (*ptr).inner.get() };
+
+        // SAFETY: `self.as_raw()` is valid because `self` is refcounted, `inner_cb` was created
+        // validly above and was turned into a ForeignOwnable, so it won't be dropped. `callback`
+        // has static life time.
+        let ret = unsafe {
+            bindings::dma_fence_add_callback(
+                self.as_raw(),
+                inner_cb,
+                Some(DmaFenceCb::<U>::callback),
+            )
+        };
+        if ret != 0 {
+            return Err(Error::from_errno(ret));
+        }
+        Ok(())
+    }
+
+    fn as_raw(&self) -> *mut bindings::dma_fence {
+        self.inner.get()
+    }
+}
#3 lore raw
In reply to Philipp Stanner

On Thu, Sep 18, 2025 at 02:30:59PM +0200, Philipp Stanner wrote:
[...]

Quoted 6 lines

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Quoted 7 lines

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring

The CHECKER static you mean? If so, it should be a static CHECKER instead of static mut CHECKER, also for future versions please use LKMM (Linux Kernel Memory Model) atomics [1] instead of Rust native atomics (you probably need to define CHECKER as Atomic<i32> because AtomicBool is not supported by LKMM and potentially sub-optimial in some cases).

Quoted 19 lines

test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Quoted 4 lines

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

You can use SpinLock<()> for this purpose, no need to add new bindings.

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/

Regards,
Boqun

Quoted 7 lines

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Philipp
---

[...]

#4 lore raw
Quoted 16 lines · Boqun Feng

On Thu, Sep 18, 2025 at 02:30:59PM +0200, Philipp Stanner wrote:
[...]

Quoted 6 lines

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Quoted 7 lines

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring

Quoted 6 lines

The CHECKER static you mean? If so, it should be a static CHECKER instead of static mut CHECKER, also for future versions please use LKMM (Linux Kernel Memory Model) atomics [1] instead of Rust native atomics (you probably need to define CHECKER as Atomic<i32> because AtomicBool is not supported by LKMM and potentially sub-optimial in some cases).

Thanks.

Thinking about it I realized that for that example code I don't even need atomics. So I'll drop them for now.

Quoted 20 lines
Quoted 20 lines

test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Quoted 7 lines
Quoted 4 lines

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

You can use SpinLock<()> for this purpose, no need to add new bindings.

The dma_fence C backend needs a spinlock pointer, given to it by the driver (so Rust code).

How do I pass a SpinLock<()> to a C function? AFAICS SpinLock doesn't implement as_raw(), so I'd have to implement it, wouldn't I?

Or rather, as it looks, I'd have to implement it for SpinLockBackend?

P.

Quoted 14 lines

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/

Regards,
Boqun

Quoted 7 lines

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Philipp
---

[...]

#5 lore raw

On Fri, Sep 26, 2025 at 10:48:06AM +0200, Philipp Stanner wrote:
[..]

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

Quoted 9 lines
Quoted 2 lines

You can use SpinLock<()> for this purpose, no need to add new bindings.

The dma_fence C backend needs a spinlock pointer, given to it by the driver (so Rust code).

How do I pass a SpinLock<()> to a C function? AFAICS SpinLock doesn't implement as_raw(), so I'd have to implement it, wouldn't I?

Technically you can use a &raw to get the address of the spinlock and just use it, since SpinLock<()> is repr(C), so it's transparent to a spinlock_t. So you don't have to ;-)

However, while we are at it, it makes sense that we do it right.

Quoted 2 lines

Or rather, as it looks, I'd have to implement it for SpinLockBackend?

I think the below should work, we already have a special from_raw() for Lock<()>, it makes sense to have a special as_raw() as well.

Let me know if you want me to send a proper patch, or feel free to add one in your patchset ;-)

rust/kernel/sync/lock.rs +7 -0
index 27202beef90c..78f3287a1372 100644
@@ -160,6 +160,13 @@ pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self {
         //   `B::State`.
         unsafe { &*ptr.cast() }
     }
+
+    /// Obtains the raw pointer from a [`Lock`].
+    ///
+    /// This can be useful for working with a lock user outside Rust.
+    pub fn as_raw(&self) -> *mut B::State {
+        self.state.get()
+    }
 }
 
 impl<T: ?Sized, B: Backend> Lock<T, B> {

Regards,
Boqun

Quoted 18 lines

P.

Quoted 14 lines

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/

Regards,
Boqun

Quoted 7 lines

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Philipp
---

[...]

#6 lore raw
In reply to Philipp Stanner
Quoted 68 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
- An implementation of PinInit<T, Error> for all driver data.
- A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
- Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Quoted 8 lines

Philipp

---
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/dma_fence.c        |  23 ++
 rust/helpers/helpers.c          |   1 +
 rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 +
 rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

Regards,
Boqun

Quoted 4 lines
 6 files changed, 420 insertions(+)
 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

[...]

#7 lore raw
Quoted 83 lines · Boqun Feng
Quoted 68 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
  - An implementation of PinInit<T, Error> for all driver data.
  - A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
  - Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Quoted 8 lines

Philipp
---
 rust/bindings/bindings_helper.h |   1 + rust/helpers/dma_fence.c        |  23 ++ rust/helpers/helpers.c          |   1 + rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 + rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

Quoted 5 lines

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

@Christian König's opinion would be valuable, too.

I'm not super convinced of that because dma_fence has not really much to do with DMA. They're not very different from completions and are a mechanism to synchronize consumers and producers.

Actually, before f54d1867005c3 they were just called "fence" and then renamed to "dma_fence" because someone wanted that name.

Anyways, I don't have strong objections and mostly care about having them available somewhere.

P.

Quoted 9 lines

Regards,
Boqun

Quoted 4 lines
 6 files changed, 420 insertions(+)

 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

[...]

#8 lore raw

On Sat, Sep 27, 2025 at 11:01:38AM +0200, Philipp Stanner wrote:
[..]

Quoted 19 lines
Quoted 13 lines
Quoted 7 lines

---
 rust/bindings/bindings_helper.h |   1 + rust/helpers/dma_fence.c        |  23 ++ rust/helpers/helpers.c          |   1 + rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 + rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

Quoted 5 lines

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

Quoted 9 lines

@Christian König's opinion would be valuable, too.

I'm not super convinced of that because dma_fence has not really much to do with DMA. They're not very different from completions and are a mechanism to synchronize consumers and producers.

Actually, before f54d1867005c3 they were just called "fence" and then renamed to "dma_fence" because someone wanted that name.

Well, dma-fence.c lives in drivers/dma-buf/, and IIUC it will only be built in CONFIG_DMA_SHARED_BUFFER is selected. They are enough evidences to show that dma_fence is not considered as a general fence. Of course, the implementation of dma_fence may not be tied to any DMA functionality, but before we make it a general fence in Rust, we need to at least change it in C as well.

Regards,
Boqun

Quoted 16 lines

Anyways, I don't have strong objections and mostly care about having them available somewhere.

P.

Quoted 9 lines

Regards,
Boqun

Quoted 4 lines
 6 files changed, 420 insertions(+)

 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

[...]

maintainer
#9 lore raw
In reply to Philipp Stanner
Quoted 84 lines · Philipp Stanner
Quoted 83 lines · Boqun Feng
Quoted 68 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
  - An implementation of PinInit<T, Error> for all driver data.
  - A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
  - Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Quoted 8 lines

Philipp
---
 rust/bindings/bindings_helper.h |   1 + rust/helpers/dma_fence.c        |  23 ++ rust/helpers/helpers.c          |   1 + rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 + rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

Quoted 7 lines
Quoted 5 lines

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

@Christian König's opinion would be valuable, too.

Oh yes, please don't mix dma_fences into SW synchronization, it's a HW synchronization primitive.

Quoted 3 lines

I'm not super convinced of that because dma_fence has not really much to do with DMA. They're not very different from completions and are a mechanism to synchronize consumers and producers.

That we mixed up fences and completion events caused a lot of trouble in the past.

It's astonishing how often Sima and I had to reject peoples ideas to use the dma_fence as SW sync.

Quoted 2 lines

Actually, before f54d1867005c3 they were just called "fence" and then renamed to "dma_fence" because someone wanted that name.

That was also partially done to make sure that people understand that this is not for inter SW sync.

On the other hand the rename didn't helped much :/

Regards,
Christian.

Quoted 17 lines

Anyways, I don't have strong objections and mostly care about having them available somewhere.

P.

Quoted 9 lines

Regards,
Boqun

Quoted 4 lines
 6 files changed, 420 insertions(+)

 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

[...]

#10 lore raw
Quoted 89 lines · Christian König
Quoted 88 lines · Philipp Stanner
Quoted 87 lines · Boqun Feng
Quoted 68 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
  - An implementation of PinInit<T, Error> for all driver data.
  - A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
  - Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Quoted 8 lines

Philipp
---
 rust/bindings/bindings_helper.h |   1 + rust/helpers/dma_fence.c        |  23 ++ rust/helpers/helpers.c          |   1 + rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 + rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

@Christian König's opinion would be valuable, too.

Oh yes, please don't mix dma_fences into SW synchronization, it's a HW synchronization primitive.

So do you agree that it should be regarded as a part of DMA_BUF?

Quoted 7 lines
Quoted 4 lines

I'm not super convinced of that because dma_fence has not really much to do with DMA. They're not very different from completions and are a mechanism to synchronize consumers and producers.

That we mixed up fences and completion events caused a lot of trouble in the past.

Quoted 8 lines

It's astonishing how often Sima and I had to reject peoples ideas to use the dma_fence as SW sync.

Quoted 2 lines

Actually, before f54d1867005c3 they were just called "fence" and then renamed to "dma_fence" because someone wanted that name.

That was also partially done to make sure that people understand that this is not for inter SW sync.

On the other hand the rename didn't helped much :/

gpu_fence might have been appropriate?

Grüße
P.

Quoted 22 lines

Regards,
Christian.

Quoted 17 lines

Anyways, I don't have strong objections and mostly care about having them available somewhere.

P.

Quoted 9 lines

Regards,
Boqun

Quoted 4 lines
 6 files changed, 420 insertions(+)

 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

[...]

maintainer
#11 lore raw
Quoted 90 lines · Philipp Stanner
Quoted 89 lines · Christian König
Quoted 88 lines · Philipp Stanner
Quoted 87 lines · Boqun Feng
Quoted 68 lines · Philipp Stanner

dma_fence is a synchronization mechanism which is needed by virtually all GPU drivers.

A dma_fence offers many features, among which the most important ones are registering callbacks (for example to kick off a work item) which get executed once a fence gets signalled.

dma_fence has a number of callbacks. Only the two most basic ones (get_driver_name(), get_timeline_name() are abstracted since they are enough to enable the basic functionality.

Callbacks in Rust are registered by passing driver data which implements the Rust callback trait, whose function will be called by the C backend.

dma_fence's are always refcounted, so implement AlwaysRefcounted for them. Once a reference drops to zero, the C backend calls a release function, where we implement drop_in_place() to conveniently marry that C-cleanup mechanism with Rust's ownership concepts.

This patch provides basic functionality, but is still missing:
  - An implementation of PinInit<T, Error> for all driver data.
  - A clever implementation for working dma_fence_begin_signalling() guards. See the corresponding TODO in the code.
  - Additional useful helper functions such as dma_fence_is_signaled(). These _should_ be relatively trivial to implement, though.

---
So. ¡Hola!

This is a highly WIP RFC. It's obviously at many places not yet conforming very well to Rust's standards.

Nevertheless, it has progressed enough that I want to request comments from the community.

There are a number of TODOs in the code to which I need input.

Notably, it seems (half-)illegal to use a shared static reference to an Atomic, which I currently use for the dma_fence unit test / docstring test. I'm willing to rework that if someone suggests how. (Still, shouldn't changing a global Atomic always be legal? It can race, of course. But that's kind of the point of an atomic)

What I want comments on the most is the design of the callbacks. I think it's a great opportunity to provide Rust drivers with rust-only callbacks, so that they don't have to bother about the C functions.

dma_fence wise, only the most basic callbacks currently get implemented. For Nova, AFAICS, we don't need much more than signalling fences and registering callbacks.

Another, solvable, issue I'm having is designing the dma_fence_begin_signallin() abstractions. There are TODOs about that in the code. That should ideally be robust and not racy. So we might want some sort of synchronized (locked?) way for using that abstraction.

Regarding the manually created spinlock of mine: I so far never need that spinlock anywhere in Rust and wasn't sure what's then the best way to pass a "raw" spinlock to C.

So much from my side. Hope to hear from you.

(I've compiled and tested this with the unit test on the current -rc3)

Quoted 8 lines

Philipp
---
 rust/bindings/bindings_helper.h |   1 + rust/helpers/dma_fence.c        |  23 ++ rust/helpers/helpers.c          |   1 + rust/helpers/spinlock.c         |   5 +
 rust/kernel/sync.rs             |   2 + rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

@Christian König's opinion would be valuable, too.

Oh yes, please don't mix dma_fences into SW synchronization, it's a HW synchronization primitive.

So do you agree that it should be regarded as a part of DMA_BUF?

That works for me.

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

I'm not super convinced of that because dma_fence has not really much to do with DMA. They're not very different from completions and are a mechanism to synchronize consumers and producers.

That we mixed up fences and completion events caused a lot of trouble in the past.

It's astonishing how often Sima and I had to reject peoples ideas to use the dma_fence as SW sync.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Actually, before f54d1867005c3 they were just called "fence" and then renamed to "dma_fence" because someone wanted that name.

That was also partially done to make sure that people understand that this is not for inter SW sync.

On the other hand the rename didn't helped much :/

gpu_fence might have been appropriate?

No, it's also used for think like RDMA, V4L etc...

There was also a proposal for HW to HW synchronization at some point, but we abandoned that after we found how problematic HW to SW sync already is.

Regards,
Christian.

Quoted 29 lines

Grüße
P.

Quoted 22 lines

Regards,
Christian.

Quoted 17 lines

Anyways, I don't have strong objections and mostly care about having them available somewhere.

P.

Quoted 9 lines

Regards,
Boqun

Quoted 4 lines
 6 files changed, 420 insertions(+)

 create mode 100644 rust/helpers/dma_fence.c
 create mode 100644 rust/kernel/sync/dma_fence.rs

[...]

#12 lore raw
In reply to Boqun Feng
Quoted 2 lines · Boqun Feng

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

Either way sounds OK.

More generally, one thing to consider nowadays is whether we will want something to eventually live in its own crate etc., but for this I don't think it applies and being in the kernel crate is fine.

By the way, should Gustavo and -media be Cc'd?

Thanks!

Cheers,
Miguel

#13 lore raw
Quoted 10 lines · Miguel Ojeda
Quoted 2 lines · Boqun Feng

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

Either way sounds OK.

More generally, one thing to consider nowadays is whether we will want something to eventually live in its own crate etc., but for this I don't think it applies and being in the kernel crate is fine.

Being in the kernel crate is fine to me as well assuming dma-buf is also in the kernel crate, but I think it's not fine to put it in kernel::sync (see my reply to Philipp) as it is.

IMO, we have a few other issues of file hierarchy as well, so I'm trying to avoid more.

For example:

- It may actually make more sense to revocable.rs in kernel::sync.
- device_id.rs and devres.rs may be better in kernel::device?
- maybe we should create a kernel::bus and put auxiliary.rs, faux.rs, pci.rs and platform.rs into it?
- cpumask.rs could be in kernel::cpu?

Regards,
Boqun

Quoted 6 lines

By the way, should Gustavo and -media be Cc'd?

Thanks!

Cheers,
Miguel

maintainer
#14 lore raw
In reply to Boqun Feng
Quoted 4 lines · Boqun Feng

I missed this part, and I don't think kernel::sync is where dma_fence should be, as kernel::sync is mostly for the basic synchronization between threads/irqs. dma_fence is probably better to be grouped with dma-buf and other dma related primitives. Maybe in kernel::dma? Like:

I agree, kernel::sync may be a bit misleading.

Quoted 5 lines

rust/kernel/dma.rs
rust/kernel/dma/dma_buf.rs
rust/kernel/dma/dma_fence.rs

Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?

To me it depends on if we want to maintain the code under a global DMA MAPPING & SCATTERLIST API entry or if it will just be added to the DMA BUFFER SHARING FRAMEWORK entry.

In case of the latter I'd go for rust/kernel/dma-buf/, analogous to drivers/dma-buf/. (Though, I'm not sure why this is under drivers/, there's nothing like dma-buf drivers; I think kernel/dma/ would have been the better fit.)

In any case, the target tree would probably be the drm-rust tree.