Re: [PATCH 5/8] rust: dma: add methods to unsafely create reference from subview

From: Gary Guo

Date: Thu Mar 26 2026 - 10:51:29 EST


On Thu Mar 26, 2026 at 2:37 PM GMT, Andreas Hindborg wrote:
> "Gary Guo" <gary@xxxxxxxxxx> writes:
>
>> From: Gary Guo <gary@xxxxxxxxxxx>
>>
>> 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 unified owned by the CPU and get a reference.
>
> What do you mean by "unified owned by the CPU"?

Oops, this should be "uniquely owned".

>
>>
>> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
>> ---
>> rust/kernel/dma.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 56 insertions(+)
>>
>> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
>> index 779d4babab9a..ae2939abc166 100644
>> --- a/rust/kernel/dma.rs
>> +++ b/rust/kernel/dma.rs
>> @@ -12,6 +12,10 @@
>> Core, //
>> },
>> error::to_result,
>> + io::{
>> + Io,
>> + IoCapable, //
>> + },
>> prelude::*,
>> ptr::KnownSize,
>> sync::aref::ARef,
>> @@ -864,6 +868,58 @@ fn drop(&mut self) {
>> // can be sent to another thread.
>> unsafe impl<T: KnownSize + Send + ?Sized> Send 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> {
>
> Will this break orphan rule when we split kernel crate? Or will you put
> `io` and `dma` together in some core crate?

It will unless we mark `View` as fundamental. However, I expect IO and DMA be
under the same crate.

Best,
Gary

>
>> + /// 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();
>> + base.dma_handle() + offset as DmaAddress
>
> Do we need a // CAST: annotation?
>
>
> Best regards,
> Andreas Hindborg