rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] rust: dma: make use of start_ptr() and start_ptr_mut()
@ 2025-11-03 19:06 Danilo Krummrich
  2025-11-03 19:06 ` [PATCH 2/2] rust: dma: use NonNull<T> instead of *mut T Danilo Krummrich
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Danilo Krummrich @ 2025-11-03 19:06 UTC (permalink / raw)
  To: abdiel.janulgue, daniel.almeida, robin.murphy, a.hindborg, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross
  Cc: linux-kernel, rust-for-linux, Danilo Krummrich

Using start_ptr() and start_ptr_mut() has the advantage that we inherit
the requirements the a mutable or immutable reference from those
methods.

Hence, use them instead of self.cpu_addr.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/dma.rs | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 4e0af3e1a3b9..f066cbb53cfa 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -505,7 +505,7 @@ pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result<&[T]> {
         //   data is also guaranteed by the safety requirements of the function.
         // - `offset + count` can't overflow since it is smaller than `self.count` and we've checked
         //   that `self.count` won't overflow early in the constructor.
-        Ok(unsafe { core::slice::from_raw_parts(self.cpu_addr.add(offset), count) })
+        Ok(unsafe { core::slice::from_raw_parts(self.start_ptr().add(offset), count) })
     }
 
     /// Performs the same functionality as [`CoherentAllocation::as_slice`], except that a mutable
@@ -525,7 +525,7 @@ pub unsafe fn as_slice_mut(&mut self, offset: usize, count: usize) -> Result<&mu
         //   data is also guaranteed by the safety requirements of the function.
         // - `offset + count` can't overflow since it is smaller than `self.count` and we've checked
         //   that `self.count` won't overflow early in the constructor.
-        Ok(unsafe { core::slice::from_raw_parts_mut(self.cpu_addr.add(offset), count) })
+        Ok(unsafe { core::slice::from_raw_parts_mut(self.start_ptr_mut().add(offset), count) })
     }
 
     /// Writes data to the region starting from `offset`. `offset` is in units of `T`, not the
@@ -557,7 +557,11 @@ pub unsafe fn write(&mut self, src: &[T], offset: usize) -> Result {
         // - `offset + count` can't overflow since it is smaller than `self.count` and we've checked
         //   that `self.count` won't overflow early in the constructor.
         unsafe {
-            core::ptr::copy_nonoverlapping(src.as_ptr(), self.cpu_addr.add(offset), src.len())
+            core::ptr::copy_nonoverlapping(
+                src.as_ptr(),
+                self.start_ptr_mut().add(offset),
+                src.len(),
+            )
         };
         Ok(())
     }
@@ -576,7 +580,7 @@ pub fn item_from_index(&self, offset: usize) -> Result<*mut T> {
         // and we've just checked that the range and index is within bounds.
         // - `offset` can't overflow since it is smaller than `self.count` and we've checked
         // that `self.count` won't overflow early in the constructor.
-        Ok(unsafe { self.cpu_addr.add(offset) })
+        Ok(unsafe { self.start_ptr().cast_mut().add(offset) })
     }
 
     /// Reads the value of `field` and ensures that its type is [`FromBytes`].
@@ -637,7 +641,7 @@ fn drop(&mut self) {
             bindings::dma_free_attrs(
                 self.dev.as_raw(),
                 size,
-                self.cpu_addr.cast(),
+                self.start_ptr_mut().cast(),
                 self.dma_handle,
                 self.dma_attrs.as_raw(),
             )
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-11-11  8:52 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-03 19:06 [PATCH 1/2] rust: dma: make use of start_ptr() and start_ptr_mut() Danilo Krummrich
2025-11-03 19:06 ` [PATCH 2/2] rust: dma: use NonNull<T> instead of *mut T Danilo Krummrich
2025-11-04  8:39   ` Alice Ryhl
2025-11-04  8:39 ` [PATCH 1/2] rust: dma: make use of start_ptr() and start_ptr_mut() Alice Ryhl
2025-11-04  9:13   ` Danilo Krummrich
2025-11-04 14:31     ` Alice Ryhl
2025-11-04 14:55       ` Danilo Krummrich
2025-11-04 10:44 ` Alexandre Courbot
2025-11-06  9:44 ` Danilo Krummrich
2025-11-06 11:33   ` Alice Ryhl
2025-11-11  8:51 ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).