rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: dma: require mutable reference for as_slice_mut() and write()
@ 2025-06-28 16:49 ` Danilo Krummrich
  2025-06-29  1:10   ` Alexandre Courbot
                     ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Danilo Krummrich @ 2025-06-28 16:49 UTC (permalink / raw)
  To: abdiel.janulgue, daniel.almeida, robin.murphy, a.hindborg, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, acourbot
  Cc: rust-for-linux, linux-kernel, 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")
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/dma.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 25dfa0e6cc3c..2ac4c47aeed3 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -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
-- 
2.50.0


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

* Re: [PATCH] rust: dma: require mutable reference for as_slice_mut() and write()
  2025-06-28 16:49 ` [PATCH] rust: dma: require mutable reference for as_slice_mut() and write() Danilo Krummrich
@ 2025-06-29  1:10   ` Alexandre Courbot
  2025-06-30  7:48   ` Abdiel Janulgue
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Alexandre Courbot @ 2025-06-29  1:10 UTC (permalink / raw)
  To: Danilo Krummrich, abdiel.janulgue, daniel.almeida, robin.murphy,
	a.hindborg, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel

On Sun Jun 29, 2025 at 1:49 AM JST, Danilo Krummrich wrote:
> 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")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>

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

* Re: [PATCH] rust: dma: require mutable reference for as_slice_mut() and write()
  2025-06-28 16:49 ` [PATCH] rust: dma: require mutable reference for as_slice_mut() and write() Danilo Krummrich
  2025-06-29  1:10   ` Alexandre Courbot
@ 2025-06-30  7:48   ` Abdiel Janulgue
  2025-06-30 10:33   ` Andreas Hindborg
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Abdiel Janulgue @ 2025-06-30  7:48 UTC (permalink / raw)
  To: Danilo Krummrich, daniel.almeida, robin.murphy, a.hindborg, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, acourbot
  Cc: rust-for-linux, linux-kernel



On 28/06/2025 19:49, Danilo Krummrich wrote:
> 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")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>   rust/kernel/dma.rs | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
> index 25dfa0e6cc3c..2ac4c47aeed3 100644
> --- a/rust/kernel/dma.rs
> +++ b/rust/kernel/dma.rs
> @@ -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

Reviewed-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>


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

* Re: [PATCH] rust: dma: require mutable reference for as_slice_mut() and write()
  2025-06-28 16:49 ` [PATCH] rust: dma: require mutable reference for as_slice_mut() and write() Danilo Krummrich
  2025-06-29  1:10   ` Alexandre Courbot
  2025-06-30  7:48   ` Abdiel Janulgue
@ 2025-06-30 10:33   ` Andreas Hindborg
  2025-07-01 10:24   ` Alice Ryhl
  2025-07-01 12:17   ` Danilo Krummrich
  4 siblings, 0 replies; 6+ messages in thread
From: Andreas Hindborg @ 2025-06-30 10:33 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: abdiel.janulgue, daniel.almeida, robin.murphy, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, aliceryhl, tmgross, acourbot,
	rust-for-linux, linux-kernel

"Danilo Krummrich" <dakr@kernel.org> writes:

> 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")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>


Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>


Best regards,
Andreas Hindborg




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

* Re: [PATCH] rust: dma: require mutable reference for as_slice_mut() and write()
  2025-06-28 16:49 ` [PATCH] rust: dma: require mutable reference for as_slice_mut() and write() Danilo Krummrich
                     ` (2 preceding siblings ...)
  2025-06-30 10:33   ` Andreas Hindborg
@ 2025-07-01 10:24   ` Alice Ryhl
  2025-07-01 12:17   ` Danilo Krummrich
  4 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2025-07-01 10:24 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: abdiel.janulgue, daniel.almeida, robin.murphy, a.hindborg, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, tmgross,
	acourbot, rust-for-linux, linux-kernel

On Sat, Jun 28, 2025 at 06:49:54PM +0200, Danilo Krummrich wrote:
> 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")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

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.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

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

* Re: [PATCH] rust: dma: require mutable reference for as_slice_mut() and write()
  2025-06-28 16:49 ` [PATCH] rust: dma: require mutable reference for as_slice_mut() and write() Danilo Krummrich
                     ` (3 preceding siblings ...)
  2025-07-01 10:24   ` Alice Ryhl
@ 2025-07-01 12:17   ` Danilo Krummrich
  4 siblings, 0 replies; 6+ messages in thread
From: Danilo Krummrich @ 2025-07-01 12:17 UTC (permalink / raw)
  To: abdiel.janulgue, daniel.almeida, robin.murphy, a.hindborg, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, aliceryhl,
	tmgross, acourbot
  Cc: rust-for-linux, linux-kernel

On Sat, Jun 28, 2025 at 06:49:54PM +0200, Danilo Krummrich wrote:
> 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")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Applied to alloc-next, thanks!

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

end of thread, other threads:[~2025-07-01 12:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <VIEdXN6CCu8vh_24Ew2HvHggO8rA-9Vg6BHi7y6kj3Wh2W-s1If1YITFfHta4MCYR3GQbYhffNc81E0AjI1AUg==@protonmail.internalid>
2025-06-28 16:49 ` [PATCH] rust: dma: require mutable reference for as_slice_mut() and write() Danilo Krummrich
2025-06-29  1:10   ` Alexandre Courbot
2025-06-30  7:48   ` Abdiel Janulgue
2025-06-30 10:33   ` Andreas Hindborg
2025-07-01 10:24   ` Alice Ryhl
2025-07-01 12:17   ` 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).