Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: dma: add Coherent:init() and Coherent::init_with_attrs()

Analogous to Coherent::zeroed() and Coherent::zeroed_with_attrs(), add
Coherent:init() and Coherent::init_with_attrs() which both take an impl
Init<T, E> argument initializing the DMA coherent memory.

Compared to CoherentInit, Coherent::init() is a one-shot constructor
that runs an Init closure and immediately exposes the DMA handle,
whereas CoherentInit is a multi-stage initializer that provides safe
&mut T access by withholding the DMA address until converted to
Coherent.

Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20260320194626.36263-6-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+38
+38
rust/kernel/dma.rs
··· 715 715 Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0)) 716 716 } 717 717 718 + /// Same as [`Coherent::zeroed_with_attrs`], but instead of a zero-initialization the memory is 719 + /// initialized with `init`. 720 + pub fn init_with_attrs<E>( 721 + dev: &device::Device<Bound>, 722 + gfp_flags: kernel::alloc::Flags, 723 + dma_attrs: Attrs, 724 + init: impl Init<T, E>, 725 + ) -> Result<Self> 726 + where 727 + Error: From<E>, 728 + { 729 + let dmem = Self::alloc_with_attrs(dev, gfp_flags, dma_attrs)?; 730 + let ptr = dmem.as_mut_ptr(); 731 + 732 + // SAFETY: 733 + // - `ptr` is valid, properly aligned, and points to exclusively owned memory. 734 + // - If `__init` fails, `self` is dropped, which safely frees the underlying `Coherent`'s 735 + // DMA memory. `T: AsBytes + FromBytes` ensures there are no complex `Drop` requirements 736 + // we are bypassing. 737 + unsafe { init.__init(ptr)? }; 738 + 739 + Ok(dmem) 740 + } 741 + 742 + /// Same as [`Coherent::zeroed`], but instead of a zero-initialization the memory is initialized 743 + /// with `init`. 744 + #[inline] 745 + pub fn init<E>( 746 + dev: &device::Device<Bound>, 747 + gfp_flags: kernel::alloc::Flags, 748 + init: impl Init<T, E>, 749 + ) -> Result<Self> 750 + where 751 + Error: From<E>, 752 + { 753 + Self::init_with_attrs(dev, gfp_flags, Attrs(0), init) 754 + } 755 + 718 756 /// Allocates a region of `[T; len]` of coherent memory. 719 757 fn alloc_slice_with_attrs( 720 758 dev: &device::Device<Bound>,