Re: [PATCH v12 2/3] rust: add dma coherent allocator abstraction.
From: Andreas Hindborg
Date: Mon Mar 03 2025 - 06:36:32 EST
"Abdiel Janulgue" <abdiel.janulgue@xxxxxxxxx> writes:
> Add a simple dma coherent allocator rust abstraction. Based on
> Andreas Hindborg's dma abstractions from the rnvme driver, which
> was also based on earlier work by Wedson Almeida Filho.
>
> Nacked-by: Christoph Hellwig <hch@xxxxxx>
> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@xxxxxxxxx>
[...]
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 0640b7e115be..8f3808c8b7fe 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -13,6 +13,7 @@
> #include "build_bug.c"
> #include "cred.c"
> #include "device.c"
> +#include "dma.c"
> #include "err.c"
> #include "fs.c"
> #include "io.c"
> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
> new file mode 100644
> index 000000000000..b4dd5d411711
> --- /dev/null
> +++ b/rust/kernel/dma.rs
> @@ -0,0 +1,421 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Direct memory access (DMA).
> +//!
> +//! C header: [`include/linux/dma-mapping.h`](srctree/include/linux/dma-mapping.h)
> +
> +use crate::{
> + bindings, build_assert,
> + device::Device,
> + error::code::*,
> + error::Result,
> + transmute::{AsBytes, FromBytes},
> + types::ARef,
> +};
> +
> +/// Inform the kernel about the device's DMA addressing capabilities. This will set the mask for
> +/// both streaming and coherent APIs together.
> +pub fn dma_set_mask_and_coherent(dev: &Device, mask: u64) -> i32 {
> + // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
> + unsafe { bindings::dma_set_mask_and_coherent(dev.as_raw(), mask) }
> +}
> +
> +/// Same as `dma_set_mask_and_coherent`, but set the mask only for streaming mappings.
> +pub fn dma_set_mask(dev: &Device, mask: u64) -> i32 {
> + // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
> + unsafe { bindings::dma_set_mask(dev.as_raw(), mask) }
> +}
I'm rebasing some of the dma pool code I'm using for NVMe on top of
these patches, and I notice that these methods in the original code from
way back (besides being on Device) has these methods return `Result`:
pub fn dma_set_mask(&self, mask: u64) -> Result {
let dev = self.as_raw();
let ret = unsafe { bindings::dma_set_mask(dev as _, mask) };
if ret != 0 {
Err(Error::from_errno(ret))
} else {
Ok(())
}
}
Is there a reason for not returning a `Result` in this series?
Best regards,
Andreas Hindborg