mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections
@ 2026-11-27 18:30 Daniel Almeida
  2026-11-27 18:30 ` [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection Daniel Almeida
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:30 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner,
	Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, linaro-mm-sig, rust-for-linux, linux-kernel

dma_fence_begin_signalling() and dma_fence_end_signalling() mark the
code that the completion of a dma-fence depends on, so that lockdep can
check it against the dma-fence rules. However, up until now, it was not
possible to use the signalling tokens from Rust. Thus, add support for
it.

Patch 1 adds SignallingSection, a guard that begins a section when it
is created and ends it when it is dropped. Patch 2 makes
DriverFence::signal() take a &SignallingSection, so that its callers
have to be in one.

Patch 5 of the JobQueue series [1] changes the same signature to
signal(&self, res). I will rebase on top of it once it lands and add
the section to its callers then.

A follow-up series adds a workqueue whose work items run in a
signalling section. Tyr will use both.

This is based on drm-rust-next.

[1] https://lore.kernel.org/r/20261124122951.878199-2-phasta@kernel.org

---
Daniel Almeida (2):
      rust: dma_buf: dma_fence: add SignallingSection
      rust: dma_buf: dma_fence: require a SignallingSection in signal()

 rust/kernel/dma_buf/dma_fence.rs | 77 ++++++++++++++++++++++++++++++++++++++--
 rust/kernel/dma_buf/mod.rs       |  3 +-
 2 files changed, 76 insertions(+), 4 deletions(-)
---
base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
change-id: 20261127-dma-fence-signalling-cae3def9aa97

Best regards,
--  
Daniel Almeida <daniel.almeida@collabora.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection
  2026-11-27 18:30 [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Daniel Almeida
@ 2026-11-27 18:30 ` Daniel Almeida
  2026-11-27 21:06   ` Danilo Krummrich
  2026-11-27 18:30 ` [PATCH 2/2] rust: dma_buf: dma_fence: require a SignallingSection in signal() Daniel Almeida
  2026-11-27 18:59 ` [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Gary Guo
  2 siblings, 1 reply; 7+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:30 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner,
	Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, linaro-mm-sig, rust-for-linux, linux-kernel

dma_fence_begin_signalling() and dma_fence_end_signalling() mark the
code that the completion of a dma-fence depends on, so that lockdep can
check it against the dma-fence rules. However, up until now, it was not
possible to use the signalling tokens from Rust.

Thus, add SignallingSection, a guard that begins a section when it is
created and ends it when it is dropped. Code on the signalling path can
take a &SignallingSection to require that its caller opened one.

Use it in the DriverFence example, which has a TODO for it.

A later patch in this series makes DriverFence::signal() require one.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 rust/kernel/dma_buf/dma_fence.rs | 70 +++++++++++++++++++++++++++++++++++++++-
 rust/kernel/dma_buf/mod.rs       |  3 +-
 2 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
index a1d851656562..7ee87935d3be 100644
--- a/rust/kernel/dma_buf/dma_fence.rs
+++ b/rust/kernel/dma_buf/dma_fence.rs
@@ -17,6 +17,7 @@
     error::to_result,
     prelude::*,
     types::ForeignOwnable,
+    types::NotThreadSafe,
     types::Opaque, //
 };
 
@@ -589,6 +590,71 @@ unsafe fn dec_ref(ptr: NonNull<Self>) {
     }
 }
 
+/// A dma-fence signalling critical section on the current task.
+///
+/// All code needed to signal a [`DriverFence`] must run in such a section,
+/// from the point where its [`Fence`] becomes visible to other threads up to
+/// the point where it is signalled. With `CONFIG_PROVE_LOCKING`, the section
+/// is a [DMA Fence Signalling Annotation], which validates that code against
+/// the [DMA Fence Cross-Driver Contract]. Without `CONFIG_LOCKDEP`, the
+/// section has no effect.
+///
+/// A function that is part of the signalling path can take a
+/// `&SignallingSection` to require that its caller opened one. Sections nest,
+/// and only the outermost one is annotated.
+///
+/// [DMA Fence Signalling Annotation]: https://docs.kernel.org/driver-api/dma-buf.html#dma-fence-signalling-annotations
+/// [DMA Fence Cross-Driver Contract]: https://docs.kernel.org/driver-api/dma-buf.html#dma-fence-cross-driver-contract
+///
+/// # Examples
+///
+/// ```
+/// use kernel::dma_buf::SignallingSection;
+///
+/// // Code on the signalling path takes a section to require that its caller
+/// // opened one.
+/// fn on_signalling_path(_section: &SignallingSection) {}
+///
+/// let section = SignallingSection::begin();
+/// on_signalling_path(&section);
+/// ```
+///
+/// # Invariants
+///
+/// `cookie` was returned by `dma_fence_begin_signalling()` on the current task
+/// and has not been passed to `dma_fence_end_signalling()` yet.
+#[must_use = "the signalling section ends immediately when the value is unused"]
+pub struct SignallingSection {
+    cookie: bool,
+    _not_send: NotThreadSafe,
+}
+
+impl SignallingSection {
+    /// Begins a signalling section on the current task.
+    #[inline]
+    pub fn begin() -> Self {
+        // SAFETY: `dma_fence_begin_signalling()` has no preconditions.
+        let cookie = unsafe { bindings::dma_fence_begin_signalling() };
+
+        // INVARIANT: `cookie` was just returned by `dma_fence_begin_signalling()`
+        // on the current task.
+        Self {
+            cookie,
+            _not_send: NotThreadSafe,
+        }
+    }
+}
+
+impl Drop for SignallingSection {
+    #[inline]
+    fn drop(&mut self) {
+        // SAFETY: By the type invariants, `cookie` comes from
+        // `dma_fence_begin_signalling()` on the current task and has not been
+        // passed to `dma_fence_end_signalling()` yet.
+        unsafe { bindings::dma_fence_end_signalling(self.cookie) };
+    }
+}
+
 // Necessary to guarantee that `inner` always comes first and can be freed by C.
 // Also useful for using casts instead of container_of().
 #[repr(C)]
@@ -654,6 +720,7 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> {
 ///         FenceContextOps,
 ///         FenceCallback,
 ///         FenceCallbackRegistration,
+///         SignallingSection,
 ///     },
 ///     str::CString,
 ///     sync::aref::ARef, //
@@ -704,9 +771,10 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> {
 /// let cb_reg = unsafe { FenceCallbackRegistration::new(&waiting_fence, cb_data) };
 /// let cb_reg = KBox::pin_init(cb_reg, GFP_KERNEL)?;
 ///
-/// // TODO signalling guards
+/// let section = SignallingSection::begin();
 /// assert_eq!(waiting_fence.is_signaled(), false);
 /// fence.signal(Ok(()));
+/// drop(section);
 /// assert_eq!(waiting_fence.is_signaled(), true);
 ///
 /// Ok::<(), Error>(())
diff --git a/rust/kernel/dma_buf/mod.rs b/rust/kernel/dma_buf/mod.rs
index 4764a828642e..8dda6d91b650 100644
--- a/rust/kernel/dma_buf/mod.rs
+++ b/rust/kernel/dma_buf/mod.rs
@@ -10,5 +10,6 @@
     FenceCallback,
     FenceCallbackRegistration,
     FenceContext,
-    FenceContextOps, //
+    FenceContextOps,
+    SignallingSection, //
 };

-- 
2.56.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] rust: dma_buf: dma_fence: require a SignallingSection in signal()
  2026-11-27 18:30 [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Daniel Almeida
  2026-11-27 18:30 ` [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection Daniel Almeida
@ 2026-11-27 18:30 ` Daniel Almeida
  2026-11-27 18:59 ` [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Gary Guo
  2 siblings, 0 replies; 7+ messages in thread
From: Daniel Almeida @ 2026-11-27 18:30 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner,
	Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, linaro-mm-sig, rust-for-linux, linux-kernel

DriverFence::signal() is where a fence's signalling path ends. The
dma-fence rules require the whole path, from the point where the fence
becomes visible to other threads up to the signal, to run in a
signalling section. A caller of signal() has most likely been running
on that path before the call.

Make DriverFence::signal() take a &SignallingSection, so that callers
have to open a section. This does not ensure that the section covers
the whole signalling path, as drivers can simply do:

    fence.signal(res, &SignallingSection::begin());

But at least it nudges drivers in the right direction and forces them
to consider signalling sections instead of ignoring them.

Update the DriverFence example accordingly.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 rust/kernel/dma_buf/dma_fence.rs | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
index 7ee87935d3be..39f9bb9d1be3 100644
--- a/rust/kernel/dma_buf/dma_fence.rs
+++ b/rust/kernel/dma_buf/dma_fence.rs
@@ -773,7 +773,7 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> {
 ///
 /// let section = SignallingSection::begin();
 /// assert_eq!(waiting_fence.is_signaled(), false);
-/// fence.signal(Ok(()));
+/// fence.signal(Ok(()), &section);
 /// drop(section);
 /// assert_eq!(waiting_fence.is_signaled(), true);
 ///
@@ -873,7 +873,10 @@ pub fn as_fence(&self) -> &Fence {
     }
 
     /// Signal the fence. This will invoke all registered callbacks.
-    pub fn signal(self, res: Result) {
+    ///
+    /// Takes a [`SignallingSection`] because all code needed to signal the
+    /// fence must run in one.
+    pub fn signal(self, res: Result, _section: &SignallingSection) {
         let fence = self.as_fence().lock();
 
         // SAFETY: `fence` is valid because `self` is valid. The lock must be

-- 
2.56.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections
  2026-11-27 18:30 [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Daniel Almeida
  2026-11-27 18:30 ` [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection Daniel Almeida
  2026-11-27 18:30 ` [PATCH 2/2] rust: dma_buf: dma_fence: require a SignallingSection in signal() Daniel Almeida
@ 2026-11-27 18:59 ` Gary Guo
  2026-11-27 20:11   ` Danilo Krummrich
  2 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2026-11-27 18:59 UTC (permalink / raw)
  To: Daniel Almeida, Sumit Semwal, Christian König,
	Philipp Stanner, Danilo Krummrich, Alice Ryhl, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, linaro-mm-sig, rust-for-linux, linux-kernel

On Fri Nov 27, 2026 at 6:30 PM GMT, Daniel Almeida wrote:
> dma_fence_begin_signalling() and dma_fence_end_signalling() mark the
> code that the completion of a dma-fence depends on, so that lockdep can
> check it against the dma-fence rules. However, up until now, it was not
> possible to use the signalling tokens from Rust. Thus, add support for
> it.
>
> Patch 1 adds SignallingSection, a guard that begins a section when it
> is created and ends it when it is dropped. Patch 2 makes
> DriverFence::signal() take a &SignallingSection, so that its callers
> have to be in one.

Do you need it being a guard? You can do things like

    let a = SignallingSection::begin();
    let b = SignallingSection::begin();
    drop(a);
    drop(b);

to break nesting. A closure-based approach wouldn't have this issue.

Best,
Gary

>
> Patch 5 of the JobQueue series [1] changes the same signature to
> signal(&self, res). I will rebase on top of it once it lands and add
> the section to its callers then.
>
> A follow-up series adds a workqueue whose work items run in a
> signalling section. Tyr will use both.
>
> This is based on drm-rust-next.
>
> [1] https://lore.kernel.org/r/20261124122951.878199-2-phasta@kernel.org
>
> ---
> Daniel Almeida (2):
>       rust: dma_buf: dma_fence: add SignallingSection
>       rust: dma_buf: dma_fence: require a SignallingSection in signal()
>
>  rust/kernel/dma_buf/dma_fence.rs | 77 ++++++++++++++++++++++++++++++++++++++--
>  rust/kernel/dma_buf/mod.rs       |  3 +-
>  2 files changed, 76 insertions(+), 4 deletions(-)
> ---
> base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
> change-id: 20261127-dma-fence-signalling-cae3def9aa97
>
> Best regards,
> --  
> Daniel Almeida <daniel.almeida@collabora.com>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections
  2026-11-27 18:59 ` [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Gary Guo
@ 2026-11-27 20:11   ` Danilo Krummrich
  2026-11-27 20:26     ` Danilo Krummrich
  0 siblings, 1 reply; 7+ messages in thread
From: Danilo Krummrich @ 2026-11-27 20:11 UTC (permalink / raw)
  To: Gary Guo
  Cc: Daniel Almeida, Sumit Semwal, Christian König,
	Philipp Stanner, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, linaro-mm-sig,
	rust-for-linux, linux-kernel

On Fri Nov 27, 2026 at 7:59 PM CET, Gary Guo wrote:
> Do you need it being a guard? You can do things like
>
>     let a = SignallingSection::begin();
>     let b = SignallingSection::begin();
>     drop(a);
>     drop(b);
>
> to break nesting. A closure-based approach wouldn't have this issue.

I also think that a closure-based design would be better.

Another nit, "signaling" is much more common in the kernel than "signalling".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections
  2026-11-27 20:11   ` Danilo Krummrich
@ 2026-11-27 20:26     ` Danilo Krummrich
  0 siblings, 0 replies; 7+ messages in thread
From: Danilo Krummrich @ 2026-11-27 20:26 UTC (permalink / raw)
  To: Gary Guo
  Cc: Daniel Almeida, Sumit Semwal, Christian König,
	Philipp Stanner, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, linaro-mm-sig,
	rust-for-linux, linux-kernel

On Fri Nov 27, 2026 at 9:11 PM CET, Danilo Krummrich wrote:
> On Fri Nov 27, 2026 at 7:59 PM CET, Gary Guo wrote:
>> Do you need it being a guard? You can do things like
>>
>>     let a = SignallingSection::begin();
>>     let b = SignallingSection::begin();
>>     drop(a);
>>     drop(b);
>>
>> to break nesting. A closure-based approach wouldn't have this issue.
>
> I also think that a closure-based design would be better.
>
> Another nit, "signaling" is much more common in the kernel than "signalling".

Well, while this is true, the API already inconsistent about this in exactly the
functions you are abstracting here, so nevermind. :)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection
  2026-11-27 18:30 ` [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection Daniel Almeida
@ 2026-11-27 21:06   ` Danilo Krummrich
  0 siblings, 0 replies; 7+ messages in thread
From: Danilo Krummrich @ 2026-11-27 21:06 UTC (permalink / raw)
  To: Daniel Almeida
  Cc: Sumit Semwal, Christian König, Philipp Stanner, Alice Ryhl,
	Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, linux-media, dri-devel,
	linaro-mm-sig, rust-for-linux, linux-kernel

On Fri Nov 27, 2026 at 7:30 PM CET, Daniel Almeida wrote:
> dma_fence_begin_signalling() and dma_fence_end_signalling() mark the
> code that the completion of a dma-fence depends on, so that lockdep can
> check it against the dma-fence rules. However, up until now, it was not
> possible to use the signalling tokens from Rust.
>
> Thus, add SignallingSection, a guard that begins a section when it is
> created and ends it when it is dropped. Code on the signalling path can
> take a &SignallingSection to require that its caller opened one.
>
> Use it in the DriverFence example, which has a TODO for it.
>
> A later patch in this series makes DriverFence::signal() require one.
>
> Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
> ---
>  rust/kernel/dma_buf/dma_fence.rs | 70 +++++++++++++++++++++++++++++++++++++++-
>  rust/kernel/dma_buf/mod.rs       |  3 +-
>  2 files changed, 71 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
> index a1d851656562..7ee87935d3be 100644
> --- a/rust/kernel/dma_buf/dma_fence.rs
> +++ b/rust/kernel/dma_buf/dma_fence.rs
> @@ -17,6 +17,7 @@
>      error::to_result,
>      prelude::*,
>      types::ForeignOwnable,
> +    types::NotThreadSafe,
>      types::Opaque, //
>  };
>  
> @@ -589,6 +590,71 @@ unsafe fn dec_ref(ptr: NonNull<Self>) {
>      }
>  }
>  
> +/// A dma-fence signalling critical section on the current task.
> +///
> +/// All code needed to signal a [`DriverFence`] must run in such a section,

Hm...this makes an annotation policy sound like an unconditional prerequisite
for signalling any DriverFence.

It is clarified below with "Without `CONFIG_LOCKDEP`, the section has no
effect.", but I'd probably first explain the annotation mechanism and then
explain what drivers are expected to do.

> +/// from the point where its [`Fence`] becomes visible to other threads up to
> +/// the point where it is signalled. With `CONFIG_PROVE_LOCKING`, the section

I'd add a bit more detail on what cases we need to protect once the fence
becomes visible to other threads or is published.

Especially the more subtle parts, such as indirect dependencies on memory
reclaim.

> +/// is a [DMA Fence Signalling Annotation], which validates that code against
> +/// the [DMA Fence Cross-Driver Contract]. Without `CONFIG_LOCKDEP`, the
> +/// section has no effect.
> +///
> +/// A function that is part of the signalling path can take a
> +/// `&SignallingSection` to require that its caller opened one. Sections nest,
> +/// and only the outermost one is annotated.
> +///
> +/// [DMA Fence Signalling Annotation]: https://docs.kernel.org/driver-api/dma-buf.html#dma-fence-signalling-annotations
> +/// [DMA Fence Cross-Driver Contract]: https://docs.kernel.org/driver-api/dma-buf.html#dma-fence-cross-driver-contract
> +///
> +/// # Examples
> +///
> +/// ```
> +/// use kernel::dma_buf::SignallingSection;
> +///
> +/// // Code on the signalling path takes a section to require that its caller
> +/// // opened one.
> +/// fn on_signalling_path(_section: &SignallingSection) {}
> +///
> +/// let section = SignallingSection::begin();
> +/// on_signalling_path(&section);
> +/// ```

Hm...I'm not sure the example is overly useful. What about an example that shows
how it catches a violation? I'm usually not a fan of this, but in this case it
kinda makes sense.

> @@ -704,9 +771,10 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> {
>  /// let cb_reg = unsafe { FenceCallbackRegistration::new(&waiting_fence, cb_data) };
>  /// let cb_reg = KBox::pin_init(cb_reg, GFP_KERNEL)?;
>  ///
> -/// // TODO signalling guards
> +/// let section = SignallingSection::begin();
>  /// assert_eq!(waiting_fence.is_signaled(), false);
>  /// fence.signal(Ok(()));
> +/// drop(section);
>  /// assert_eq!(waiting_fence.is_signaled(), true);

I think that's a separate patch, or actually the subsequent patch is a natural
fit.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-27 21:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 18:30 [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Daniel Almeida
2026-11-27 18:30 ` [PATCH 1/2] rust: dma_buf: dma_fence: add SignallingSection Daniel Almeida
2026-11-27 21:06   ` Danilo Krummrich
2026-11-27 18:30 ` [PATCH 2/2] rust: dma_buf: dma_fence: require a SignallingSection in signal() Daniel Almeida
2026-11-27 18:59 ` [PATCH 0/2] rust: dma_buf: add dma-fence signalling sections Gary Guo
2026-11-27 20:11   ` Danilo Krummrich
2026-11-27 20:26     ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®