From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796211648; b=tqy32nl+5rE2tJUkFiS/5LF2rPgmReWCh/orDsHfcuTTp1rZ/PxLILisGUqofzUA44x0 lYokJNTxAmDCaPY9U73E6Nbn6SpekyYZ2jIa9DL3cTz0QKByStzzRkzCwxsUwvvZRd8/h jIH8MIx5qApdyeyC6jinoVQ5NMeQJgx7R6DrLEL7HHDWC8XSmchfRiQ3Rjw3xA7xp3HG7 FlA2DLBfbqrH/VRksg0bsLqd4CKp6tOP7y774waWI7GPPwvlvKtsjkPCVfHV6807E1ZyY nycnd7I7N5YErHxIySSOzyYNNKqGND3h53J+1oflaWZ5APboEdeFzZAzMG3CKljDwOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796211648; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Nu1pJyQAFxBq3G/mcIrVNaWH55WCqlGC41gGNecmo8w=; b=0IsRi3fUNKI0uZTEesFqhKEP7t1PG61AYBRDDkWpMFkiIHPI+k/frQkQ4faNQUAGy/sE q3KMkIjXaaY5OFCu8U9Hz31gSfhbwbFk4Y4Lv2TdjButtVODPyc5Ckne2fHdg8fI1sS2M R8+bmDFAnTdv6XlwAhcjG4TcDqxlX/G5CTnQuVxCo/4Y/xHBW0B8UF0iu2lX/5q1IgR3Z BtbMqpNesfh3SAySHI/iEPw1yTGiPwd4ZYjkcCvZ6Zrhx/4cTa7HCsbiGWnWYAb6DMOwd e+RAD4Ew/pF+KR9K4xV1iPHrHnlcOiBb4vli+hdq/8DTG7Dx1x3SoPC22O2n6nIk3aA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=G3E6srg9; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A1DD606A325; Wed, 2 Dec 2026 11:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=G3E6srg9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91F311F00893; Wed, 2 Dec 2026 11:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796211648; bh=Nu1pJyQAFxBq3G/mcIrVNaWH55WCqlGC41gGNecmo8w=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=G3E6srg98Di3+4vd7Zirq+UtkfvH0/tTcm/2ZW1II8w6F1ZpDgFquEE6+c7y6mAM+ tSqZxcdgsrPuOSdUxbRfFfLlbAa0JOnvtyPeXg/qORGL/6jv9e9hdkNQ58v+WX5pg7 HTXbnAMOqqdGN54iOzBmfDasYhixUWpbFudboaeDepit7GixnuDjli7x+Vl5U8jVSn n2vcVVrBl+9xQZU0/GpOKXSplJiQkjTrfzqpCflE1bNriKbXepDZ2nU+F5m3yhAU/p DdFhiPFE4Vr8bq7C1+GHu2gqNr2DpiTBUTzIMUkJzWpDyc6cfw9isyZIIg4weP1kco iBEtbd/DF0zEg== From: Philipp Stanner To: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Matthew Brost , David Airlie , Simona Vetter , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Lyude Paul , "Joel Fernandes (NVIDIA)" , Boris Brezillon , Yuvan Shen Cc: rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, rcu@vger.kernel.org Subject: [PATCH v3 6/9] rust: DmaFence: Replace is_signaled() with status() Date: Wed, 2 Dec 2026 12:39:12 +0100 Message-ID: <20261202113914.1660411-8-phasta@kernel.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261202113914.1660411-2-phasta@kernel.org> References: <20261202113914.1660411-2-phasta@kernel.org> X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit As it turns out, our dma_fence abstractions lack an essential feature; a problem which is partially inherited from the C backend: Despite it being a common pattern in the kernel, you actually should not just check whether a fence was signaled and call it a day - it is important that you also check whether a fence was signaled *with an error*. To make things more complicated, an error can get modified at any point in time before a fence gets signaled. Thus, it must be ensured that the signaled state is always evaluated before the error code. Since the fence status is essential information that should not be ignored, replace Fence::is_signaled() with Fence::status(). Signed-off-by: Philipp Stanner --- rust/kernel/dma_buf/dma_fence.rs | 71 +++++++++++++++++--------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs index 41233e9c3934..71e202e1f7cf 100644 --- a/rust/kernel/dma_buf/dma_fence.rs +++ b/rust/kernel/dma_buf/dma_fence.rs @@ -29,7 +29,8 @@ ptr::{ drop_in_place, NonNull, // - }, // + }, + task::Poll, // }; use kernel::{ @@ -510,39 +511,34 @@ unsafe impl Send for Fence {} unsafe impl Sync for Fence {} impl Fence { - /// Check whether the fence was signaled at the moment of the function call. + /// Query a fence's error code and turn it into a [`Result`]. /// - /// Note that this can return `true` for a [`Fence`] whose [`DriverFence`] - /// has not yet been dropped. The reason is that the fence ops callbacks can - /// cause the fence to get signaled by the C backend. - #[inline] - pub fn is_signaled(&self) -> bool { - // We should not use `dma_fence_is_signaled_locked()` here, because - // according to the C backend's recommendations, that function is - // problematic and we should avoid calling that function with a lock - // held. + /// # Safety + /// + /// The caller must only call this on fences that are already signaled. It + /// must be ensured that this is ordered properly against checking of the + /// signaled status (fence->signaled bit). + unsafe fn __status(&self) -> Result { + // SAFETY: Safe by the function's safety requirements. + let res = unsafe { (*self.as_raw()).error }; - // SAFETY: Inner `fence` is valid because `self` is valid. - let ret = unsafe { bindings::dma_fence_is_signaled(self.as_raw()) }; + to_result(res) + } - // To be as robust as possible for the future we guarantee that an API - // caller can 100% rely on the signaling being completed (i.e., all - // fence callbacks ran), so we have to take the lock. - // - // The reason is that the C dma_fence backend currently does not - // carefully synchronize the `dma_fence_is_signaled()` function with the - // proper spinlock. This can lead to the function returning `true` while - // fence callbacks are still being executed. This can be mitigated by - // guarding the entire function with the spinlock. - // - // The fundamental reason is that the C backend currently does guard - // setting of the fence's signaled-bit with the fence's spinlock, but - // reading is done locklessly. - // - // See commit c8a5d5ea3ba6a. - let _ = self.lock(); + /// Query a fence's status at the moment of the function call. + /// + /// If it is signaled, return the fence's result, i.e., determine if it was + /// signaled with an error. + pub fn status(&self) -> Poll { + // SAFETY: `self` is valid. + if unsafe { bindings::dma_fence_is_signaled(self.as_raw()) } { + // SAFETY: The fence is signaled. The C backend ensures ordering. + let status = unsafe { self.__status() }; - ret + return Poll::Ready(status); + } + + Poll::Pending } /// Lock the fence. A helper only to be used internally in this module. @@ -670,7 +666,7 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> { /// FenceContext, /// FenceContextOps, /// FenceCallback, -/// FenceCallbackRegistration, +/// FenceCallbackRegistration, // /// }, /// str::CString, /// sync::aref::ARef, // @@ -722,9 +718,18 @@ struct DriverFenceData<'a, T: Send + Sync + FenceContextOps> { /// let cb_reg = KBox::pin_init(cb_reg, GFP_KERNEL)?; /// /// // TODO signalling guards -/// assert_eq!(waiting_fence.is_signaled(), false); +/// +/// let status = waiting_fence.status(); +/// if status.is_ready() { +/// return Err(ENOMSG); +/// } /// fence.signal(Ok(())); -/// assert_eq!(waiting_fence.is_signaled(), true); +/// let status = waiting_fence.status(); +/// if status.is_pending() { +/// return Err(ENOENT); +/// } +/// +/// status?; /// /// Ok::<(), Error>(()) /// ``` -- 2.56.0