[PATCH v2 07/11] rust: dma: add methods to unsafely create reference from subview

From: Gary Guo

Date: Tue Apr 21 2026 - 10:59:08 EST


Implement `Io` for `Coherent`, so now `dma::Coherent` can be used with I/O
projections.

This allows the `as_ref()` and `as_mut()` API to be used in smaller region
than the whole DMA allocation itself. For example, if a ring buffer is
shared between GPU and CPU, users may now use the `io_project!` API to
obtain a view of the buffer that is uniquely owned by the CPU and get a
reference.

Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/kernel/dma.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)

diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 642ccff465c8..92db0e58f364 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -14,6 +14,7 @@
},
error::to_result,
fs::file,
+ io::Io,
prelude::*,
ptr::KnownSize,
sync::aref::ARef,
@@ -989,6 +990,59 @@ unsafe impl<T: KnownSize + Send + ?Sized> Send for Coherent<T> {}
// The safe methods only return metadata or raw pointers whose use requires `unsafe`.
unsafe impl<T: KnownSize + ?Sized + AsBytes + FromBytes + Sync> Sync for Coherent<T> {}

+impl<T: ?Sized + KnownSize> Io for Coherent<T> {
+ type Type = T;
+
+ #[inline]
+ fn as_ptr(&self) -> *mut Self::Type {
+ self.as_mut_ptr()
+ }
+}
+
+impl<'a, B: ?Sized + KnownSize, T: ?Sized> crate::io::View<'a, Coherent<B>, T> {
+ /// Returns a DMA handle which may be given to the device as the DMA address base of
+ /// the region.
+ #[inline]
+ pub fn dma_handle(&self) -> DmaAddress {
+ let base = self.io();
+ let offset = self.as_ptr().addr() - base.as_ptr().addr();
+ // CAST: The offseted DMA address can never overflow.
+ base.dma_handle() + offset as DmaAddress
+ }
+
+ /// Returns a reference to the data in the region.
+ ///
+ /// # Safety
+ ///
+ /// * Callers must ensure that the device does not read/write to/from memory while the returned
+ /// slice is live.
+ /// * Callers must ensure that this call does not race with a write to the same region while
+ /// the returned slice is live.
+ #[inline]
+ pub unsafe fn as_ref(self) -> &'a T {
+ let ptr = self.as_ptr();
+ // SAFETY: pointer is aligned and valid per type invariant of `View`. Aliasing rule is
+ // satisfied per safety requirement.
+ unsafe { &*ptr }
+ }
+
+ /// Returns a mutable reference to the data in the region.
+ ///
+ /// # Safety
+ ///
+ /// * Callers must ensure that the device does not read/write to/from memory while the returned
+ /// slice is live.
+ /// * Callers must ensure that this call does not race with a read or write to the same region
+ /// while the returned slice is live.
+ #[inline]
+ pub unsafe fn as_mut(self) -> &'a mut T {
+ let ptr = self.as_ptr();
+ // SAFETY: pointer is aligned and valid per type invariant of `View`. Aliasing rule is
+ // satisfied per safety requirement.
+ unsafe { &mut *ptr }
+ }
+}
+
impl<T: KnownSize + AsBytes + ?Sized> debugfs::BinaryWriter for Coherent<T> {
fn write_to_slice(
&self,

--
2.51.2