From: "Gary Guo" <gary@garyguo.net>
To: "Philipp Stanner" <phasta@kernel.org>,
"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>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"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>,
"Christian König" <christian.koenig@amd.com>,
"Matthew Brost" <matthew.brost@intel.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Paul E. McKenney" <paulmck@kernel.org>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Neeraj Upadhyay" <neeraj.upadhyay@kernel.org>,
"Joel Fernandes" <joelagnelf@nvidia.com>,
"Josh Triplett" <josh@joshtriplett.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
"Lai Jiangshan" <jiangshanlai@gmail.com>,
Zqiang <qiang.zhang@linux.dev>, "Lyude Paul" <lyude@redhat.com>,
"Joel Fernandes (NVIDIA)" <joel@joelfernandes.org>,
"Boris Brezillon" <boris.brezillon@collabora.com>,
"Yuvan Shen" <yuvi.shen@outlook.com>
Cc: <rust-for-linux@vger.kernel.org>, <linux-media@vger.kernel.org>,
<dri-devel@lists.freedesktop.org>, <rcu@vger.kernel.org>
Subject: Re: [PATCH v3 5/9] rust: DmaFence: Replace call_rcu() with RcuState
Date: Wed, 02 Dec 2026 17:12:16 +0000 [thread overview]
Message-ID: <DNAEXSDZQ15X.23NR08J6M1027@garyguo.net> (raw)
In-Reply-To: <20261202113914.1660411-7-phasta@kernel.org>
On Wed Dec 2, 2026 at 11:39 AM GMT, Philipp Stanner wrote:
> 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 through RcuState, 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 creating the RcuState
> 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.
>
> Adjust FenceContext's drop accordingly, replacing the rcu_barrier() with
> a synchronize_rcu().
>
> Suggested-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Philipp Stanner <phasta@kernel.org>
> ---
> rust/kernel/dma_buf/dma_fence.rs | 132 +++++++++++++------------------
> 1 file changed, 54 insertions(+), 78 deletions(-)
>
> diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
> index c04b68fe67fe..41233e9c3934 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,10 @@
> Atomic,
> Relaxed, //
> },
> - rcu::rcu_barrier, //
> + rcu::{
> + synchronize_rcu,
> + RcuState, //
> + }, //
> }, //
> };
>
> @@ -58,6 +62,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 +71,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 +155,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(None),
> data,
> };
>
> @@ -243,15 +246,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();
This condition is only true for !needs_drop?
I think this should be
if !needs_drop::<...>() {
synchronize_rcu();
}
> }
> }
>
> @@ -615,12 +613,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()`].
> + ///
> + /// Gets set when a [`DriverFence`] signals, `None` otherwise.
> + synchronize_rcu_cookie: UnsafeCell<Option<RcuState>>,
> + /// The API user's data.
> #[pin]
> data: T::FenceDataType,
> }
> @@ -659,6 +659,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 +833,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 +840,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 = Some(RcuState::new());
> }
> }
> }
> @@ -955,15 +961,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 +994,29 @@ 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.
> + let data = unsafe { self.data.as_ref() };
> + // SAFETY: Access is exclusive because we are dropping.
> + let rcu_cookie = unsafe { &mut *data.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 let Some(cookie) = rcu_cookie {
> + cookie.cond_synchronize();
> + } else {
> + synchronize_rcu();
> + }
> +
> + // SAFETY: Valid because `self` is valid. RCU above ensures that all
> + // accessors are gone.
> + unsafe { drop_in_place(&raw mut self.data) };
Sashiko's finding is valid here.
Best,
Gary
> +
> + // 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()) };
> }
> }
next prev parent reply other threads:[~2026-12-02 17:12 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 11:39 [PATCH v3 0/9] rust: Add drm::JobQueue Philipp Stanner
2026-12-02 11:39 ` [PATCH v3 1/9] rust: rcu: Add RcuState Philipp Stanner
2026-12-02 11:47 ` sashiko-bot
2026-12-02 17:06 ` Gary Guo
2026-12-02 11:39 ` [PATCH v3 2/9] rust: DmaFence: Implement Deref for FenceContext Philipp Stanner
2026-12-02 11:39 ` [PATCH v3 3/9] rust: DmaFence: Add ID accessor " Philipp Stanner
2026-12-02 11:39 ` [PATCH v3 4/9] rust: DmaFence: Don't drop on signal Philipp Stanner
2026-12-02 11:39 ` [PATCH v3 5/9] rust: DmaFence: Replace call_rcu() with RcuState Philipp Stanner
2026-12-02 11:49 ` sashiko-bot
2026-12-02 17:12 ` Gary Guo [this message]
2026-12-02 11:39 ` [PATCH v3 6/9] rust: DmaFence: Replace is_signaled() with status() Philipp Stanner
2026-12-02 11:54 ` sashiko-bot
2026-12-02 11:39 ` [PATCH v3 7/9] rust: DmaFence: Pass result in callbacks Philipp Stanner
2026-12-02 11:39 ` [PATCH v3 8/9] rust: drm: Add JobQueue Philipp Stanner
2026-12-02 12:04 ` sashiko-bot
2026-12-02 11:39 ` [PATCH v3 9/9] 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=DNAEXSDZQ15X.23NR08J6M1027@garyguo.net \
--to=gary@garyguo.net \
--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=boris.brezillon@collabora.com \
--cc=christian.koenig@amd.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=frederic@kernel.org \
--cc=jiangshanlai@gmail.com \
--cc=joel@joelfernandes.org \
--cc=joelagnelf@nvidia.com \
--cc=josh@joshtriplett.org \
--cc=linux-media@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=matthew.brost@intel.com \
--cc=neeraj.upadhyay@kernel.org \
--cc=ojeda@kernel.org \
--cc=paulmck@kernel.org \
--cc=phasta@kernel.org \
--cc=qiang.zhang@linux.dev \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.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=urezki@gmail.com \
--cc=work@onurozkan.dev \
--cc=yuvi.shen@outlook.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.