[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260130-coherent-array-v1-7-bcd672dacc70@nvidia.com>
Date: Fri, 30 Jan 2026 17:34:10 +0900
From: Eliot Courtney <ecourtney@...dia.com>
To: Danilo Krummrich <dakr@...nel.org>,
Alexandre Courbot <acourbot@...dia.com>, Alice Ryhl <aliceryhl@...gle.com>,
David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
Abdiel Janulgue <abdiel.janulgue@...il.com>,
Daniel Almeida <daniel.almeida@...labora.com>,
Robin Murphy <robin.murphy@....com>,
Andreas Hindborg <a.hindborg@...nel.org>, Miguel Ojeda <ojeda@...nel.org>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Trevor Gross <tmgross@...ch.edu>
Cc: nouveau@...ts.freedesktop.org, dri-devel@...ts.freedesktop.org,
linux-kernel@...r.kernel.org, driver-core@...ts.linux.dev,
rust-for-linux@...r.kernel.org, Eliot Courtney <ecourtney@...dia.com>
Subject: [PATCH 7/9] rust: dma: implement decay from CoherentArray to
CoherentSlice
Implement Deref, DerefMut, AsRef, AsMut, From for various methods
of decaying CoherentArray to CoherentSlice. This is so statically
sized CoherentArrays can be used as if they were CoherentSlices by
code that doesn't care about knowing the compile time size.
This also helps avoid having to annotate static sizes on types all
the time.
Signed-off-by: Eliot Courtney <ecourtney@...dia.com>
---
rust/kernel/dma.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 46 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index f3920f74583a..25da678c863b 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -12,7 +12,11 @@
sync::aref::ARef,
transmute::{AsBytes, FromBytes},
};
-use core::{marker::PhantomData, ptr::NonNull};
+use core::{
+ marker::PhantomData,
+ ops::{Deref, DerefMut},
+ ptr::NonNull, //
+};
/// DMA address type.
///
@@ -389,7 +393,8 @@ impl<const N: usize> AllocationSize for StaticSize<N> {}
/// # Allocation size
///
/// [`CoherentAllocation`] is generic over an [`AllocationSize`], which lets it record a compile
-/// time known size (in number of elements of `T`).
+/// time known size (in number of elements of `T`). A statically sized [`CoherentAllocation`] can
+/// decay to a runtime sized one via deref coercion.
// TODO
//
// DMA allocations potentially carry device resources (e.g.IOMMU mappings), hence for soundness
@@ -402,6 +407,7 @@ impl<const N: usize> AllocationSize for StaticSize<N> {}
//
// Hence, find a way to revoke the device resources of a `CoherentAllocation`, but not the
// entire `CoherentAllocation` including the allocated memory itself.
+#[repr(C)]
pub struct CoherentAllocation<T: AsBytes + FromBytes, Size: AllocationSize = RuntimeSize> {
dev: ARef<device::Device>,
dma_handle: DmaAddress,
@@ -857,6 +863,44 @@ unsafe impl<T: AsBytes + FromBytes + Send, Size: AllocationSize> Send
{
}
+impl<T: AsBytes + FromBytes, const N: usize> Deref for CoherentArray<T, N> {
+ type Target = CoherentSlice<T>;
+
+ fn deref(&self) -> &Self::Target {
+ // SAFETY: `CoherentArray<T, N>` and `CoherentSlice<T>` are both `CoherentAllocation<T, S>`
+ // with different `S: AllocationSize` marker types. Since `AllocationSize` is only stored as
+ // `PhantomData<S>` (a ZST) and CoherentAllocation<T, S> is `repr(C)`, both types have
+ // identical memory layouts.
+ unsafe { &*core::ptr::from_ref(self).cast::<CoherentSlice<T>>() }
+ }
+}
+
+impl<T: AsBytes + FromBytes, const N: usize> DerefMut for CoherentArray<T, N> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ // SAFETY: Same as `Deref::deref`.
+ unsafe { &mut *core::ptr::from_mut(self).cast::<CoherentSlice<T>>() }
+ }
+}
+
+impl<T: AsBytes + FromBytes, const N: usize> AsRef<CoherentSlice<T>> for CoherentArray<T, N> {
+ fn as_ref(&self) -> &CoherentSlice<T> {
+ self
+ }
+}
+
+impl<T: AsBytes + FromBytes, const N: usize> AsMut<CoherentSlice<T>> for CoherentArray<T, N> {
+ fn as_mut(&mut self) -> &mut CoherentSlice<T> {
+ self
+ }
+}
+
+impl<T: AsBytes + FromBytes, const N: usize> From<CoherentArray<T, N>> for CoherentSlice<T> {
+ fn from(array: CoherentArray<T, N>) -> Self {
+ // SAFETY: Same as `Deref::deref`.
+ unsafe { core::mem::transmute(array) }
+ }
+}
+
/// Reads a field of an item from an allocated region of structs.
///
/// # Examples
--
2.52.0
Powered by blists - more mailing lists