Skip to content

Commit c0a3065

Browse files
GnurouDanilo Krummrich
authored andcommitted
rust: dma: expose the count and size of CoherentAllocation
These properties are very useful to have (and to be used by nova-core) and should be accessible, hence add them. Additionally, add type invariants for the size of an allocation. Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://lore.kernel.org/r/20250619-nova-frts-v6-2-ecf41ef99252@nvidia.com [ Slightly extend the commit message. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 14371e5 commit c0a3065

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

rust/kernel/dma.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ pub mod attrs {
114114
///
115115
/// # Invariants
116116
///
117-
/// For the lifetime of an instance of [`CoherentAllocation`], the `cpu_addr` is a valid pointer
118-
/// to an allocated region of coherent memory and `dma_handle` is the DMA address base of
119-
/// the region.
117+
/// - For the lifetime of an instance of [`CoherentAllocation`], the `cpu_addr` is a valid pointer
118+
/// to an allocated region of coherent memory and `dma_handle` is the DMA address base of the
119+
/// region.
120+
/// - The size in bytes of the allocation is equal to `size_of::<T> * count`.
121+
/// - `size_of::<T> * count` fits into a `usize`.
120122
// TODO
121123
//
122124
// DMA allocations potentially carry device resources (e.g.IOMMU mappings), hence for soundness
@@ -179,9 +181,12 @@ impl<T: AsBytes + FromBytes> CoherentAllocation<T> {
179181
if ret.is_null() {
180182
return Err(ENOMEM);
181183
}
182-
// INVARIANT: We just successfully allocated a coherent region which is accessible for
183-
// `count` elements, hence the cpu address is valid. We also hold a refcounted reference
184-
// to the device.
184+
// INVARIANT:
185+
// - We just successfully allocated a coherent region which is accessible for
186+
// `count` elements, hence the cpu address is valid. We also hold a refcounted reference
187+
// to the device.
188+
// - The allocated `size` is equal to `size_of::<T> * count`.
189+
// - The allocated `size` fits into a `usize`.
185190
Ok(Self {
186191
dev: dev.into(),
187192
dma_handle,
@@ -201,6 +206,21 @@ impl<T: AsBytes + FromBytes> CoherentAllocation<T> {
201206
CoherentAllocation::alloc_attrs(dev, count, gfp_flags, Attrs(0))
202207
}
203208

209+
/// Returns the number of elements `T` in this allocation.
210+
///
211+
/// Note that this is not the size of the allocation in bytes, which is provided by
212+
/// [`Self::size`].
213+
pub fn count(&self) -> usize {
214+
self.count
215+
}
216+
217+
/// Returns the size in bytes of this allocation.
218+
pub fn size(&self) -> usize {
219+
// INVARIANT: The type invariant of `Self` guarantees that `size_of::<T> * count` fits into
220+
// a `usize`.
221+
self.count * core::mem::size_of::<T>()
222+
}
223+
204224
/// Returns the base address to the allocated region in the CPU's virtual address space.
205225
pub fn start_ptr(&self) -> *const T {
206226
self.cpu_addr

0 commit comments

Comments
 (0)