Rust for Linux List
 help / color / mirror / Atom feed
From: Philipp Stanner <phasta@kernel.org>
To: "Matthew Brost" <matthew.brost@intel.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Philipp Stanner" <phasta@kernel.org>,
	"Christian König" <ckoenig.leichtzumerken@gmail.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Tvrtko Ursulin" <tvrtko.ursulin@igalia.com>,
	"Paul E . McKenney" <paulmck@kernel.org>
Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org
Subject: [PATCH v2 06/10] rust: DmaFence: Replace call_rcu() with cond_synchronize_rcu()
Date: Tue, 24 Nov 2026 13:29:48 +0100	[thread overview]
Message-ID: <20261124122951.878199-8-phasta@kernel.org> (raw)
In-Reply-To: <20261124122951.878199-2-phasta@kernel.org>

The C dma_fence contract demands that a fence's data disappears no
earlier than 1 RCU grace period after the DriverFence was signaled. So
far, we achieved this with dropping the DriverFence's data in a deferred
manner with a manual, raw binding call to call_rcu().

However, this call_rcu() does not solve one problem: It is conceivable
that some driver data must not drop in atomic context; but call_rcu()'s
payload *can* be executed in atomic context.

Moreover, the current developments in drm::JobQueue, where a DriverFence
is always coupled 1:1 with a Job, allows for dropping job and fence
through a work item.

We can satisfy the grace period requirement in a safe yet performant
manner by using cond_synchronize_rcu(), which only waits for a grace
period if the specified grace period has not yet passed.

We can exactly identify the right grace period by setting a cookie in
DriverFence::signal().

Replace DriverFence::drop()'s call_rcu() with cond_synchronize_rcu().

An implication of this is that fences must for now drop in process
context until someone finds a more elegant solution. Document this
requirement.

Suggested-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 rust/kernel/dma_buf/dma_fence.rs | 133 +++++++++++++------------------
 1 file changed, 56 insertions(+), 77 deletions(-)

diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
index c04b68fe67fe..20dadbde4eb9 100644
--- a/rust/kernel/dma_buf/dma_fence.rs
+++ b/rust/kernel/dma_buf/dma_fence.rs
@@ -21,6 +21,7 @@
 };
 
 use core::{
+    cell::UnsafeCell,
     marker::PhantomData,
     mem::ManuallyDrop,
     ops::Deref,
@@ -42,7 +43,11 @@
             Atomic,
             Relaxed, //
         },
-        rcu::rcu_barrier, //
+        rcu::{
+            cond_synchronize_rcu,
+            get_state_synchronize_rcu,
+            synchronize_rcu, //
+        },
     }, //
 };
 
@@ -58,6 +63,8 @@ pub trait FenceContextOps {
 /// 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.
+///
+/// A FenceContext must drop in non-atomic context.
 #[pin_data(PinnedDrop)]
 pub struct FenceContext<T: FenceContextOps + Send + Sync> {
     /// The fence context number.
@@ -65,7 +72,7 @@ pub struct FenceContext<T: FenceContextOps + Send + Sync> {
     /// The sequence number for the next fence created.
     seqno: Atomic<u64>,
     // The name parameters can be accessed by the dma_fence backend_ops. UAF
-    // errors are prevented by the `call_rcu()` in `drop_driver_fence_data()`.
+    // errors are prevented by `synchronize_rcu()`.
     /// The name of the driver this FenceContext's fences belong to.
     driver_name: CString,
     /// The name of the timeline this FenceContext's fences belong to.
@@ -149,20 +156,17 @@ fn next_seqno(&self) -> u64 {
     ///
     /// This is needed because many times, creation of a [`DriverFence`] must not
     /// fail, and allocating might deadlock in some situations.
-    ///
-    /// The `data` you pass here must not perform any operations that are illegal
-    /// in atomic context in its [`Drop`] implementation.
     pub fn new_fence_allocation(
         &self,
         data: T::FenceDataType,
     ) -> Result<DriverFenceAllocation<'_, T>> {
         let fence_data = DriverFenceData {
-            rcu_head: Default::default(),
             // `inner` remains uninitialized until a `DriverFence` takes over.
             inner: Fence {
                 inner: Opaque::uninit(),
             },
             fctx: self,
+            synchronize_rcu_cookie: UnsafeCell::new(0),
             data,
         };
 
@@ -243,15 +247,10 @@ fn drop(self: Pin<&mut Self>) {
             panic!("Forgotten fences in FenceContext.");
         }
 
-        // Ensure that the driver cannot unload while there are still dma_fence
-        // callbacks running. At the same time, the RCU barrier addresses the
-        // problem inherited by the C backend, in which backend ops callbacks
-        // might be accessing the fence while it is being signaled (or shortly
-        // after). This could cause UAF access on the fence context's
-        // `fctx.driver_name` and `fctx.timeline_name`.
-        //
-        // Wait for the RCU callbacks in `DriverFence::drop`.
-        rcu_barrier();
+        // If a DriverFence signals without doing a synchronize_rcu(), and a
+        // fctx drops immediately afterwards, then backend_ops still accessing
+        // the fctx might run into UAF if we don't wait for them here.
+        synchronize_rcu();
     }
 }
 
@@ -615,12 +614,14 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> {
     // necessary so that the C backend can free the allocation (coming from our
     // Rust code) with kfree_rcu().
     inner: Fence,
-    /// Callback head for dropping this in a deferred manner through RCU.
-    rcu_head: bindings::callback_head,
     /// Reference to access the FenceContext.
     fctx: &'a FenceContext<T>,
-    /// The API user's data. It is essential that the data only performs
-    /// operations legal in atomic context in its [`Drop`] implementation.
+    /// A cookie that helps with reducing the number of costly calls to
+    /// `synchronize_rcu()` in [`DriverFence::drop()`].
+    ///
+    /// This [`UnsafeCell`] is protected by `inner`'s spinlock.
+    synchronize_rcu_cookie: UnsafeCell<usize>,
+    /// The API user's data.
     #[pin]
     data: T::FenceDataType,
 }
@@ -659,6 +660,8 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> {
 ///
 /// In the unlikely case of such violations, a panic is thrown.
 ///
+/// A DriverFence must drop in non-atomic context.
+///
 /// # Examples
 ///
 /// ```
@@ -831,7 +834,6 @@ pub fn signal(&self, res: Result) {
                 // must not have been signaled yet, which we check directly above.
                 unsafe { bindings::dma_fence_set_error(fence.as_raw(), err.to_errno()) };
             }
-
             // SAFETY: `self.data` is valid because `self` is valid.
             let fctx = unsafe { self.data.as_ref().fctx };
             let _ = fctx.nr_of_unsignaled_fences.fetch_sub(1, Relaxed);
@@ -839,6 +841,11 @@ pub fn signal(&self, res: Result) {
             // SAFETY: `fence` is valid because `self` is valid. The lock must
             // be held, which we acquired above.
             unsafe { bindings::dma_fence_signal_locked(fence.as_raw()) };
+
+            // SAFETY: `self.data` is valid because `self` is valid.
+            // The lock guarantees exclusivity.
+            let rcu_cookie = unsafe { &mut *self.data.as_ref().synchronize_rcu_cookie.get() };
+            *rcu_cookie = get_state_synchronize_rcu();
         }
     }
 }
@@ -955,15 +962,15 @@ fn drop(&mut self) {
         // dma_fence_is_signaled_locked() because the C backend wants to get rid
         // of the latter.
 
-        // SAFETY: `guard` is valid until the `call_rcu()` below.
+        // SAFETY: `guard` is valid.
         let signaled: bool = unsafe { bindings::dma_fence_test_signaled_flag(guard.as_raw()) };
         if !signaled {
             pr_err!("DriverFence drops unsignaled. Danger of memory corruption!\n");
-            // SAFETY: `guard` is valid until the `call_rcu()` below. The fence
-            // must not have been signaled yet, which we check directly above.
+            // SAFETY: `guard` is valid. The fence must not have been signaled
+            // yet, which we check directly above.
             unsafe { bindings::dma_fence_set_error(guard.as_raw(), ECANCELED.to_errno()) };
-            // SAFETY: `guard` is valid until the `call_rcu()` below. The lock
-            // must be held, which we acquired above.
+            // SAFETY: `guard` is valid. The lock must be held, which we
+            // acquired above.
             unsafe { bindings::dma_fence_signal_locked(guard.as_raw()) };
 
             // SAFETY: `self.data` is valid because `self` is valid.
@@ -988,59 +995,31 @@ fn drop(&mut self) {
             return;
         }
 
-        // SAFETY: Valid because `self` is valid.
-        let rcu_head_ptr = unsafe { &raw mut (*self.data.as_ptr()).rcu_head };
+        // SAFETY: `self` is valid. No one can access the cookie anymore because
+        // we are dropping.
+        let rcu_cookie = unsafe { *self.data.as_ref().synchronize_rcu_cookie.get() };
 
-        // SAFETY: `call_rcu()` is always safe to be called. `rcu_head_ptr` was
-        // created validly above. The module must perform a `synchronize_rcu()`
-        // or `rcu_barrier()` call to guard against module unload.
-        unsafe { bindings::call_rcu(rcu_head_ptr, Some(drop_driver_fence_data::<T>)) };
-    }
-}
+        // Make sure none of the fence backend_ops can access fence-data anymore.
+        //
+        // TODO:
+        // This would not be necessary if the C dma_fence backend were using a
+        // spinlock to properly synchronize its signaled state. Fix it in C and
+        // then remove synchronize_rcu().
+        if rcu_cookie != 0 {
+            // SAFETY: The if-else checks whether the cookie was validly
+            // initialized when this fence signaled.
+            unsafe { cond_synchronize_rcu(rcu_cookie) };
+        } else {
+            // The user never signaled this fence. We don't know the grace period.
+            synchronize_rcu();
+        }
 
-// TODO:
-// The entire call_rcu() mechanism in the drop above and the code below would be
-// unnecessary if C's dma_fence_signal() could be reworked in a way that after it
-// ran, the caller knows that no fence_ops callbacks can be running anymore.
-// In other words, if the dma_fence backend would use its spinlock for full
-// synchronization.
-//
-// Then we could move the drop_in_place() and dma_fence_put() upwards into the
-// drop() implementation and call it a day.
+        // SAFETY: Valid because `self` is valid. RCU above ensures that all
+        // accessors are gone.
+        unsafe { drop_in_place(&raw mut self.data) };
 
-/// Finally really drop this `DriverFence<T>`
-///
-/// # Safety
-///
-/// `head` references the `rcu_head` field of an `DriverFenceData<T>`. All
-/// accessors to that `DriverFenceData<T>` must be gone by now. This must be
-/// ensured by signalling the associated `DriverFence<T>` and then waiting
-/// for a grace period until calling this function here.
-unsafe extern "C" fn drop_driver_fence_data<T: Send + Sync + FenceContextOps>(
-    head: *mut bindings::callback_head,
-) {
-    // SAFETY: Caller provides a pointer to the `rcu_head` field of a `DriverFenceData<C>`.
-    let fence_data = unsafe { container_of!(head, DriverFenceData<'_, T>, rcu_head) };
-
-    // SAFETY: `fence_data` was created validly above. All the fence's data will
-    // only drop below, but the raw pointer to the raw C `dma_fence` remains
-    // valid because the reference count is only decremented at the end of the
-    // function.
-    let fence = unsafe { (*fence_data).inner.inner.get() };
-
-    // SAFETY: `fence_data` was created validly above. The user has already
-    // dropped the only conventional accessor to the user data, the `DriverFence`,
-    // one grace period ago. All accessors are gone now.
-    unsafe { drop_in_place(&raw mut (*fence_data).data) };
-
-    // The inner `Fence` explicitly does not get dropped because there may be
-    // many more users / consumers, each holding their own reference.
-
-    // SAFETY: Once a `DriverFence` is initialized, the inner `fence` is valid
-    // and initialized. It is valid until the refcount drops to 0, which can
-    // earliest happen once we drop the `DriverFence`'s reference here.
-    unsafe { bindings::dma_fence_put(fence) };
-
-    // The actual memory the data associated with a `DriverFence` lives in
-    // gets freed by the C dma_fence backend once the fence's refcount reaches 0.
+        // SAFETY: Valid because `self` is valid. This is the initial reference
+        // from when this `DriverFence` was created.
+        unsafe { bindings::dma_fence_put(self.as_raw()) };
+    }
 }
-- 
2.56.0


  parent reply	other threads:[~2026-11-24 12:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 12:29 [PATCH v2 00/10] rust: Add drm::JobQueue Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 01/10] rust: rcu: Add get_state_synchronize_rcu() Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 02/10] rust: rcu: Add cond_synchronize_rcu() Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 03/10] rust: DmaFence: Implement Deref for FenceContext Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 04/10] rust: DmaFence: Add ID accessor " Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 05/10] rust: DmaFence: Don't drop on signal Philipp Stanner
2026-11-24 12:29 ` Philipp Stanner [this message]
2026-11-24 12:29 ` [PATCH v2 07/10] rust: DmaFence: Replace is_signaled() with status() Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 08/10] rust: DmaFence: Pass result in callbacks Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 09/10] rust: drm: Add JobQueue Philipp Stanner
2026-11-24 12:29 ` [PATCH v2 10/10] MAINTAINERS: Add drm::JobQueue to drm_sched's entry Philipp Stanner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124122951.878199-8-phasta@kernel.org \
    --to=phasta@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=ckoenig.leichtzumerken@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=ojeda@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=tvrtko.ursulin@igalia.com \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox