Re: [PATCH v13 2/7] rust: add dma coherent allocator abstraction.

From: Danilo Krummrich
Date: Fri Mar 21 2025 - 13:35:10 EST


On Fri, Mar 21, 2025 at 02:23:53PM -0300, Jason Gunthorpe wrote:
> On Fri, Mar 07, 2025 at 01:06:19PM +0200, Abdiel Janulgue wrote:
>
> > + // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`.
> > + let ret = unsafe {
> > + bindings::dma_alloc_attrs(
> > + dev.as_raw(),
> > + size,
> > + &mut dma_handle,
> > + gfp_flags.as_raw(),
> > + dma_attrs.as_raw(),
> > + )
>
> This is not the correct safety statement, the device must have a driver
> bound to call this function, a struct device reference is not
> sufficient.
>
> I belive Danilo was suggesting to ignore this unsafety for now, but if
> so it should be documented correctly.

If just landed patches [1], which are the foundation of addressing this issue.

With the next cycle, this will be ensured by the type system.

>
> Also think the use of devres here is going to be very problematic for
> drivers to use as I said in other emails. :(

In an earlier reply today in a different thread already gave you the link [2] of
what we landed, which, besides explaining the situation, also makes clear that
there is *no* Devres wrapper around a CoherentAllocation and why.

[1] https://lore.kernel.org/lkml/20250314160932.100165-1-dakr@xxxxxxxxxx/
[2] https://github.com/Rust-for-Linux/linux/blob/rust-next/rust/kernel/dma.rs#L120