From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01D174A1E19; Fri, 4 Sep 2026 15:33:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788535984; cv=none; b=BSmAybFtv6eiIgQPOEM5zpKa4zDaY7c3lrTucJxNktYcRsXPbsgnLLFvB6HVXqapdFSTo5GMOHkZS9qSZf4iEdZ4jDQG2HdkKO5amcQEDXmPdYt7h0KNAyfxJMqwSDkrv9/BCSP5btD3saLjknJsxHLjeNpNk+UJSLLnc0lrLxk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788535984; c=relaxed/simple; bh=CnSAFD6rlndSqM250EuSHcBPlhVJ+BFLsyUu02VpByE=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:Cc:To:From: References:In-Reply-To; b=QBwzm0Nf8V/RsDKbsoJsy/wEWPj8yAO+O87Yx9VmnmjUsDsj1jEtJi+eRcwoW5dDgFu4NdCeLP78AKS8rFO4L5bLj7BCtguYNuqPELeBojlyCMzfzrEi7zHbwGRsMCAfdpFGRx81G9fpzHZKgnmZI2vhHOdcuYmdRKmaBA7EQEs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q9XCxnK+; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q9XCxnK+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C0281F00A3D; Fri, 4 Sep 2026 15:32:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788535982; bh=mHQYPGPdzlGxXMNKZnACiaB//jM8t7QwCLZn+xl/ipU=; h=Date:Subject:Cc:To:From:References:In-Reply-To; b=Q9XCxnK+Ao/JAb6ul5UFx2KsHLa7LxDBLiAEV5Xqa7+lWsX6U0sRbJ0GHgfuuTKx2 dnp+mOpB+kmvTDgq2cO5JoBAhVahscyDSVLD+to1VsZkJbCdJ501AGJjyQzsAiMXjf 1tn9pX5LGpqDjhn9SwTOooDx6nAA4sXg2CmZqaG5b1AM5kMNDSdEDzFFpzbucpSK5h dMHKcRChOCGvBaT/nNIYUu0/8hmbrxuGnacDL53b53cYzB7K1GtYfH6gRx1mOkZ6WY axoNRnNipTdv/hhdsXHrOoaTQ4oqXegu+ikptbRpK3dyvBMBYRifKzQWFJHChon0sm VkrdAlajKogCw== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Fri, 04 Sep 2026 17:32:55 +0200 Message-Id: Subject: Re: [PATCH v10 1/2] rust: Add dma_fence abstractions Cc: "Miguel Ojeda" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , "Daniel Almeida" , "Tamir Duberstein" , "Alexandre Courbot" , =?utf-8?q?Onur_=C3=96zkan?= , "Sumit Semwal" , =?utf-8?q?Christian_K=C3=B6nig?= , "Greg Kroah-Hartman" , "Yury Norov (NVIDIA)" , "Asahi Lina" , "Burak Emir" , "Lorenzo Stoakes" , "Joel Fernandes" , "FUJITA Tomonori" , "Boris Brezillon" , , , , , To: "Philipp Stanner" From: "Danilo Krummrich" References: <20260812072217.1971807-2-phasta@kernel.org> <20260812072217.1971807-3-phasta@kernel.org> In-Reply-To: <20260812072217.1971807-3-phasta@kernel.org> On Wed Aug 12, 2026 at 9:22 AM CEST, Philipp Stanner wrote: > Add abstractions for dma_fence in Rust. I was about to pick this up, but ended up with too many things to tweak. * Fix checkpatch.pl warnings, * take &CStr instead of CString in FenceContext::new(), * change DriverFenceAllocation::ops to &'static from *const, * remove # Safety comment on AlwaysRefCounted::dec_ref() impl, * use Opaque::zeroed() instead of __GFP_ZERO, * use kernel vertical import style. Here's the diff I suggest for a v11: diff --git a/rust/helpers/dma_fence.c b/rust/helpers/dma_fence.c index 0e08411098fa..549f6b6a7171 100644 --- a/rust/helpers/dma_fence.c +++ b/rust/helpers/dma_fence.c @@ -37,7 +37,8 @@ __rust_helper void rust_helper_dma_fence_lock_irqsave(str= uct dma_fence *f, unsig dma_fence_lock_irqsave(f, *flags); } -__rust_helper void rust_helper_dma_fence_unlock_irqrestore(struct dma_fenc= e *f, unsigned long *flags) +__rust_helper void rust_helper_dma_fence_unlock_irqrestore(struct dma_fenc= e *f, + unsigned long *flags) { dma_fence_unlock_irqrestore(f, *flags); } diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fen= ce.rs index f22f4a07c152..d82b69f68e61 100644 --- a/rust/kernel/dma_buf/dma_fence.rs +++ b/rust/kernel/dma_buf/dma_fence.rs @@ -102,21 +102,23 @@ impl<'a, T: Send + Sync + FenceContextOps> FenceConte= xt { /// Create a new `FenceContext`. pub fn new( initial_seqno: u64, - driver_name: CString, - timeline_name: CString, + driver_name: &CStr, + timeline_name: &CStr, data: impl PinInit, ) -> impl PinInit where Error: From, { + let driver_name =3D CString::try_from(driver_name); + let timeline_name =3D CString::try_from(timeline_name); try_pin_init!(Self { // SAFETY: `dma_fence_context_alloc()` merely works on a globa= l // atomic. Parameter `1` is the number of contexts we want to // allocate. nr: unsafe { bindings::dma_fence_context_alloc(1) }, seqno: Atomic::new(initial_seqno), - driver_name, - timeline_name, + driver_name: driver_name?, + timeline_name: timeline_name?, nr_of_unsignaled_fences: Atomic::new(0), data <- data, }) @@ -141,7 +143,7 @@ pub fn new_fence_allocation( rcu_head: Default::default(), // `inner` remains uninitialized until a `DriverFence` takes o= ver. inner: Fence { - inner: Opaque::uninit(), + inner: Opaque::zeroed(), }, fctx: self, data, @@ -154,7 +156,7 @@ pub fn new_fence_allocation( // // Hence, we need the manage the memory manually. It will be freed= by the // C backend automatically once the refcount within `Fence` drops = to 0. - let data =3D KBox::new(fence_data, GFP_KERNEL | __GFP_ZERO)?; + let data =3D KBox::new(fence_data, GFP_KERNEL)?; Ok(DriverFenceAllocation { data, @@ -212,7 +214,7 @@ fn drop(self: Pin<&mut Self>) { // Fence ops callbacks can be called on unsignaled fences. Since t= hese // callbacks can access the fence context and its data, it needs t= o be // guaranteed that a context only drops after all associated - // `DriverFence`s have been dropped. This is unlikely to ocurr, bu= t would + // `DriverFence`s have been dropped. This is unlikely to occur, bu= t would // result in silent UAF. Throw a panic to prevent that. // // TODO: @@ -568,9 +570,6 @@ fn inc_ref(&self) { unsafe { bindings::dma_fence_get(self.as_raw()) } } - /// # Safety - /// - /// `ptr`must be a valid pointer to a [`DriverFence`]. unsafe fn dec_ref(ptr: NonNull) { // SAFETY: `ptr` is never a NULL pointer; and when `dec_ref()` is = called // the fence is by definition still valid. @@ -640,15 +639,17 @@ struct DriverFenceData<'a, T: Send + Sync + FenceCont= extOps> { /// # Examples /// /// ``` -/// use kernel::dma_buf::{ -/// DriverFence, -/// FenceContext, -/// FenceContextOps, -/// FenceCallback, -/// FenceCallbackRegistration, // +/// use kernel::{ +/// dma_buf::{ +/// DriverFence, +/// FenceContext, +/// FenceContextOps, +/// FenceCallback, +/// FenceCallbackRegistration, +/// }, +/// str::CString, +/// sync::aref::ARef, /// }; -/// use kernel::str::CString; -/// use kernel::sync::aref::ARef; /// use core::fmt::Display; /// /// struct CallbackData { } @@ -674,18 +675,16 @@ struct DriverFenceData<'a, T: Send + Sync + FenceCont= extOps> { /// /// let fctx_data =3D FenceContextData::new(); /// -/// let driver_name =3D CString::try_from_fmt(fmt!("dummy_driver"))?; -/// let timeline_name =3D CString::try_from_fmt(fmt!("dummy_timeline"))?; -/// /// let mut fctx =3D KBox::pin_init( -/// FenceContext::new(0, driver_name, timeline_name, fctx_data), GFP_K= ERNEL)?; +/// FenceContext::new(0, c"dummy_driver", c"dummy_timeline", fctx_data= ), +/// GFP_KERNEL, +/// )?; /// /// struct FenceData { /// data: CString, /// } /// -/// let data =3D CString::try_from_fmt(fmt!("dummy_data"))?; -/// let fence_data =3D FenceData { data }; +/// let fence_data =3D FenceData { data: c"dummy_data".try_into()? }; /// /// let fence_alloc =3D fctx.new_fence_allocation(fence_data)?; /// let mut fence =3D fence_alloc.new_fence(); @@ -718,8 +717,8 @@ pub struct DriverFenceAllocation<'a, T: Send + Sync + F= enceContextOps> { /// Handed over to a [`DriverFence`], or deallocated once the /// [`DriverFenceAllocation`] drops. data: KBox>, - /// Pointer for the ops for the associated [`FenceContext`] - ops: *const bindings::dma_fence_ops, + /// Reference for the ops for the associated [`FenceContext`] + ops: &'static bindings::dma_fence_ops, } impl<'a, T: Send + Sync + FenceContextOps> DriverFenceAllocation<'a, T> {