rust: DmaFence: Don't drop on signal
Subsystems (4)
Philipp Stanner
maintainerPATCH 1/1 rust: DmaFence: Don't drop on signal
The original design for DmaFence intended to comply as strictly as possible with the "dma_fence contract", which, according to some, says that a fence must only be signaled once. Thus, the implementation was made so that a DriverFence drops on signal.
While extending JobQueue's capabilities, it was discovered that this characteristic causes - unnecessary - trouble: fence-data must be dropped in an RCU-deferred manner because the C backend demands that. However, fences are often signaled in atomic context (e.g., interrupt handler) where you don't want to drop, but signal as quickly as possible.
Actually, there is no real hard reason to enforce that a fence can only get signaled once, because the C backend is robust against multiple signal attempts.
What is decisive is that a) all fences signal and b) they only drop no earlier than 1 RCU grace period after signaling.
Don't drop a DriverFence on signal().
--- rust/kernel/dma_buf/dma_fence.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
rust/kernel/dma_buf/dma_fence.rs +6 -5
@@ -805,7 +805,7 @@ pub fn as_fence(&self) -> &Fence { }/// Signal the fence. This will invoke all registered callbacks. -pub fn signal (self, res:Result ) { +pub fn signal (&self, res:Result ) {let fence = self.as_fence ().lock ();// SAFETY: `fence` is valid because `self` is valid. The lock must be
@@ -816,14 +816,15 @@ 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 ); +// 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. -let fctx =unsafe { self.data.as_ref ().fctx }; -let _ = fctx.nr_of_unsignaled_fences.fetch_sub (1 ,Relaxed ); } }
base-commit: 3497a403264b094ebd61b8b755165d56e7d9dc37
Signature · 1 line
2.56.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] rust: dma_fence: premature unsignaled fence count decrement in signal() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rust: dma_fence: premature unsignaled fence count decrement in signal() --
commit d89d19611f93fbf6fde4c15619608d55fb454178
Author: Philipp Stanner <[email protected]>
rust: DmaFence: Don't drop on signal
This commit changes DriverFence::signal() to take &self instead of consuming self by value so that signaling a fence in atomic context does not immediately drop it, and moves the nr_of_unsignaled_fences decrement inside the unsignaled check.
Quoted 22 lines
rust/kernel/dma_buf/dma_fence.rs +6
-1@@ -805,7 +805,7 @@ pub fn as_fence(&self) -> &Fence { }/// Signal the fence. This will invoke all registered callbacks. -pub fn signal (self, res:Result ) { +pub fn signal (&self, res:Result ) {let fence = self.as_fence ().lock ();// SAFETY: `fence` is valid because `self` is valid. The lock must be @@ -816,14 +816,15 @@ 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 ); +
rust/kernel/dma_buf/dma_fence.rs pub fn signal(self, res: Result) { · line 829
Quoted 10 lines
rust/kernel/dma_buf/dma_fence.rs +0
-4// 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. -let fctx =unsafe { self.data.as_ref ().fctx }; -let _ = fctx.nr_of_unsignaled_fences.fetch_sub (1 ,Relaxed ); } }
[Severity: High]
Can decrementing fctx.nr_of_unsignaled_fences in DriverFence::signal() before DriverFence::drop() runs lead to a use-after-free of FenceContext?
Previously, DriverFence::signal(self, res) consumed self by value, so decrementing nr_of_unsignaled_fences was immediately followed by DriverFence::drop(), which queues the drop_driver_fence_data() callback via call_rcu():
rust/kernel/dma_buf/dma_fence.rs:DriverFence::drop() {
...
unsafe { bindings::call_rcu(rcu_head_ptr, Some(drop_driver_fence_data::<T>)) };
}In FenceContext::drop(), the check on nr_of_unsignaled_fences and the subsequent rcu_barrier() rely on all associated DriverFence instances having already been dropped and having queued their call_rcu() callbacks:
rust/kernel/dma_buf/dma_fence.rs:FenceContext::drop() {
if self.nr_of_unsignaled_fences.load(Relaxed) != 0 {
panic!("Forgotten fences in FenceContext.");
}
...
rcu_barrier();
}With DriverFence::signal(&self, res) no longer consuming self, fctx.nr_of_unsignaled_fences is decremented before DriverFence::drop() runs and before bindings::dma_fence_signal_locked() is called.
If a DriverFence is signaled via DriverFence::signal() and then leaked with core::mem::forget(), or held as a foreign pointer via ForeignOwnable::into_foreign() and signaled via DriverFenceBorrow::signal(), nr_of_unsignaled_fences can reach 0 before DriverFence::drop() has queued call_rcu().
When FenceContext::drop() then runs:
1. The nr_of_unsignaled_fences check passes even though DriverFence::drop() has not run.
2. rcu_barrier() only waits for already-queued call_rcu() callbacks, so it can return without waiting for a grace period after dma_fence_signal_locked(), or before a foreign-owned DriverFence is later reclaimed via DriverFence::from_foreign() and dropped.
3. For a foreign-owned DriverFence, decrementing nr_of_unsignaled_fences with Relaxed ordering before dma_fence_signal_locked() also allows FenceContext::drop() to free fctx.driver_name and fctx.timeline_name while dma_fence_signal_locked() (via trace_dma_fence_signaled()) or concurrent readers in dma_fence_driver_name() and dma_fence_timeline_name() are still accessing the FenceContext through FenceContext::get_driver_name() and FenceContext::get_timeline_name().
Should nr_of_unsignaled_fences only be decremented in DriverFence::drop() (or tracked until drop) so that FenceContext::drop() cannot complete before all DriverFence instances have queued their call_rcu() callbacks?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
rust/kernel/dma_buf/dma_fence.rs pub fn signal(self, res: Result) { · line 823
Quoted 5 lines · Philipp Stanner
rust/kernel/dma_buf/dma_fence.rs +5
-0+ + // 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); +
When I think about it we probably want to address this in a series (preparing for JQ upstreaming) where we also have my proposed DriverFence::drop(synchronize_rcu). Otherwise this patch would need both synchronize_rcu() and rcu_barrier() for formal correctness when the FenceContext drops, which is just overkill.
If only we could fully lock the fence, all this would just go away. But currently that's what it is.
So forget about this patch. I send sth new soonish.
P.