[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAEmX-ZUbPqNUBj2-xAf=5uGE_GWAQQ79akADZCDxNsmx7Lf9VQ@mail.gmail.com>
Date: Tue, 4 Feb 2025 10:54:09 -0600
From: Thomas Hampton <thomas.j.hampton@...il.com>
To: Abdiel Janulgue <abdiel.janulgue@...il.com>, daniel.almeida@...labora.com,
aliceryhl@...gle.com, robin.murphy@....com, rust-for-linux@...r.kernel.org
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>, Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>,
Valentin Obst <kernel@...entinobst.de>, open list <linux-kernel@...r.kernel.org>,
Christoph Hellwig <hch@....de>, Marek Szyprowski <m.szyprowski@...sung.com>, airlied@...hat.com,
"open list:DMA MAPPING HELPERS" <iommu@...ts.linux.dev>
Subject: Re: [PATCH v8 2/2] rust: add dma coherent allocator abstraction.
On 1/8/2025 6:27 AM, Abdiel Janulgue wrote:
> + /// Returns the base address, dma handle, attributes and the size of the allocated region.
> + /// The caller takes ownership of the returned resources, i.e., will have the responsibility
> + /// in calling `bindings::dma_free_attrs`.
> + pub fn into_parts(self) -> (*mut T, bindings::dma_addr_t, usize, usize) {
> + let size = self.count * core::mem::size_of::<T>();
> + let ret = (
> + self.cpu_addr,
> + self.dma_handle,
> + self.dma_attrs.as_raw(),
> + size,
> + );
> + // Drop the device's reference count associated with this object. This is needed as no
> + // destructor will be called on this object once this function returns.
> + // SAFETY: the device pointer is still valid as of this point due to the type invariants
> + // on `CoherentAllocation`.
> + unsafe { bindings::put_device(self.dev.as_raw()) }
> + core::mem::forget(self);
> + ret
> + }
Would it be feasible and prudent to avoid exposing `pub fn
into_parts`, `pub fn first_ptr`, `pub fn first_ptr_mut`?
It seems like some sort of functional access method might be better
for keeping future Rust code maintainable and Rusty.
For example, making client code from:
> {
> let ctrl_id = unsafe { &*(id.first_ptr() as *const NvmeIdCtrl) };
> number_of_namespaces = ctrl_id.nn.into();
> mdts = ctrl_id.mdts;
> }
Into something like,
pub fn expose(self, op: F)
where F: Fn(*const T) {
op(self.cpu_addr);
}
let number_of_namespaces;
let mdts;
id.expose(|data| {
let ctrl_id = &*(data as *const NvmeIdCtrl);
number_of_namespaces = ctrl_id.nn.into();
mdts = ctrl_id.mdts;
});
And to avoid code docs like:
> /// The caller <...> will have the responsibility in calling `bindings::dma_free_attrs`.
Powered by blists - more mailing lists