Skip to contentSkip to conversation

rust: dma: require mutable reference for as_slice_mut() and write()

PATCH Applied Danilo Krummrich 5 participants 6 messages Jun 28, 2025 – Jul 1, 2025

Subsystems (2)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 rust: dma: require mutable reference for as_slice_mut() and write()

Given the safety requirements of as_slice_mut() and write() taking an immutable reference is technically not incorrect.

However, let's leverage the compiler's capabilities and require a mutable reference to ensure exclusive access.

This also fixes a clippy warning introduced with 1.88:

warning: mutable borrow from immutable input(s)

     --> rust/kernel/dma.rs:297:78
      |

297 | pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
| ^^^^^^^^

Fixes d37a39f607c4 ("rust: dma: add as_slice/write functions for CoherentAllocation")
---
 rust/kernel/dma.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/dma.rs +2 -2
index 25dfa0e6cc3c..2ac4c47aeed3 100644
@@ -294,7 +294,7 @@ pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result<&[T]> {
     ///   slice is live.
     /// * Callers must ensure that this call does not race with a read or write to the same region
     ///   while the returned slice is live.
-    pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
+    pub unsafe fn as_slice_mut(&mut self, offset: usize, count: usize) -> Result<&mut [T]> {
         self.validate_range(offset, count)?;
         // SAFETY:
         // - The pointer is valid due to type invariant on `CoherentAllocation`,
@@ -326,7 +326,7 @@ pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T
     /// unsafe { alloc.write(buf, 0)?; }
     /// # Ok::<(), Error>(()) }
     /// ```
-    pub unsafe fn write(&self, src: &[T], offset: usize) -> Result {
+    pub unsafe fn write(&mut self, src: &[T], offset: usize) -> Result {
         self.validate_range(offset, src.len())?;
         // SAFETY:
         // - The pointer is valid due to type invariant on `CoherentAllocation`

base-commit: c7e03c5cf06a90ff234ae3c628c6b74e5cba7426

Signature · 1 line
2.50.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Danilo Krummrich

Given the safety requirements of as_slice_mut() and write() taking an immutable reference is technically not incorrect.

However, let's leverage the compiler's capabilities and require a mutable reference to ensure exclusive access.

This also fixes a clippy warning introduced with 1.88:

Quoted 8 lines

warning: mutable borrow from immutable input(s)

     --> rust/kernel/dma.rs:297:78
      |

297 | pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
| ^^^^^^^^

Fixes d37a39f607c4 ("rust: dma: add as_slice/write functions for CoherentAllocation")
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Danilo Krummrich
Quoted 43 lines · Danilo Krummrich

Given the safety requirements of as_slice_mut() and write() taking an immutable reference is technically not incorrect.

However, let's leverage the compiler's capabilities and require a mutable reference to ensure exclusive access.

This also fixes a clippy warning introduced with 1.88:

   warning: mutable borrow from immutable input(s)
      --> rust/kernel/dma.rs:297:78
       |
   297 |     pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
       |                                                                              ^^^^^^^^
Fixes d37a39f607c4 ("rust: dma: add as_slice/write functions for CoherentAllocation")
---
  rust/kernel/dma.rs | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/dma.rs +2 -2
index 25dfa0e6cc3c..2ac4c47aeed3 100644
@@ -294,7 +294,7 @@ pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result<&[T]> {
      ///   slice is live.
      /// * Callers must ensure that this call does not race with a read or write to the same region
      ///   while the returned slice is live.
-    pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
+    pub unsafe fn as_slice_mut(&mut self, offset: usize, count: usize) -> Result<&mut [T]> {
          self.validate_range(offset, count)?;
          // SAFETY:
          // - The pointer is valid due to type invariant on `CoherentAllocation`,
@@ -326,7 +326,7 @@ pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T
      /// unsafe { alloc.write(buf, 0)?; }
      /// # Ok::<(), Error>(()) }
      /// ```
-    pub unsafe fn write(&self, src: &[T], offset: usize) -> Result {
+    pub unsafe fn write(&mut self, src: &[T], offset: usize) -> Result {
          self.validate_range(offset, src.len())?;
          // SAFETY:
          // - The pointer is valid due to type invariant on `CoherentAllocation`
Quoted 1 line

base-commit: c7e03c5cf06a90ff234ae3c628c6b74e5cba7426

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

Given the safety requirements of as_slice_mut() and write() taking an immutable reference is technically not incorrect.

However, let's leverage the compiler's capabilities and require a mutable reference to ensure exclusive access.

This also fixes a clippy warning introduced with 1.88:

Quoted 8 lines

warning: mutable borrow from immutable input(s)

     --> rust/kernel/dma.rs:297:78
      |

297 | pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
| ^^^^^^^^

Fixes d37a39f607c4 ("rust: dma: add as_slice/write functions for CoherentAllocation")

Best regards,
Andreas Hindborg

reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

Given the safety requirements of as_slice_mut() and write() taking an immutable reference is technically not incorrect.

However, let's leverage the compiler's capabilities and require a mutable reference to ensure exclusive access.

This also fixes a clippy warning introduced with 1.88:

Quoted 8 lines

warning: mutable borrow from immutable input(s)

     --> rust/kernel/dma.rs:297:78
      |

297 | pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
| ^^^^^^^^

Fixes d37a39f607c4 ("rust: dma: add as_slice/write functions for CoherentAllocation")

I'm not sure if as_slice_mut() is exactly the right API we want long-term, but this is a step in the right direction.

author maintainer Applied
#5 lore raw
In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

Given the safety requirements of as_slice_mut() and write() taking an immutable reference is technically not incorrect.

However, let's leverage the compiler's capabilities and require a mutable reference to ensure exclusive access.

This also fixes a clippy warning introduced with 1.88:

Quoted 8 lines

warning: mutable borrow from immutable input(s)

     --> rust/kernel/dma.rs:297:78
      |

297 | pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Result<&mut [T]> {
| ^^^^^^^^

Fixes d37a39f607c4 ("rust: dma: add as_slice/write functions for CoherentAllocation")

Applied to alloc-next, thanks!