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 <[email protected]> --- 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(§ion); +/// ``` +/// +/// # 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
