Skip to contentSkip to conversation

rust: dma: add `Send` implementation for `CoherentAllocation`

PATCH Applied Miguel Ojeda 3 participants 4 messages Mar 24, 2025

Subsystems (3)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 rust: dma: add `Send` implementation for `CoherentAllocation`

From: Danilo Krummrich <[email protected]>

Stephen found a future build failure in linux-next [1]:

    error[E0277]: `*mut MyStruct` cannot be sent between threads safely
      --> samples/rust/rust_dma.rs:47:22
       |
    47 | impl pci::Driver for DmaSampleDriver {
       |                      ^^^^^^^^^^^^^^^ `*mut MyStruct` cannot be sent between threads safely

It is caused by the interaction between commit 935e1d90bf6f ("rust: pci:
require Send for Driver trait implementers") from the driver-core tree, which fixes a missing concurrency requirement, and commit 9901addae63b ("samples: rust: add Rust dma test sample driver") which adds a sample that does not satisfy that requirement.

Add a Send implementation to CoherentAllocation, which allows the sample (and other future users) to satisfy it.

---
 rust/kernel/dma.rs | 4 ++++
 1 file changed, 4 insertions(+)
rust/kernel/dma.rs +4 -0
index 9d00f9c49f47..18de693c4924 100644
@@ -301,6 +301,10 @@ fn drop(&mut self) {
     }
 }
 
+// SAFETY: It is safe to send a `CoherentAllocation` to another thread if `T`
+// can be send to another thread.
+unsafe impl<T: AsBytes + FromBytes + Send> Send for CoherentAllocation<T> {}
+
 /// Reads a field of an item from an allocated region of structs.
 ///
 /// # Examples
Signature · 1 line
2.49.0
#1 lore raw
Quoted 17 lines · Miguel Ojeda

From: Danilo Krummrich <[email protected]>

Stephen found a future build failure in linux-next [1]:

    error[E0277]: `*mut MyStruct` cannot be sent between threads safely
      --> samples/rust/rust_dma.rs:47:22
       |
    47 | impl pci::Driver for DmaSampleDriver {
       |                      ^^^^^^^^^^^^^^^ `*mut MyStruct` cannot be sent between threads safely

It is caused by the interaction between commit 935e1d90bf6f ("rust: pci:
require Send for Driver trait implementers") from the driver-core tree, which fixes a missing concurrency requirement, and commit 9901addae63b ("samples: rust: add Rust dma test sample driver") which adds a sample that does not satisfy that requirement.

Quoted 7 lines

Add a Send implementation to CoherentAllocation, which allows the sample (and other future users) to satisfy it.

If nobody shouts in the next few hours, I will apply it to rust-next to clean the build failure for tomorrow's linux-next.

Abdiel: if you can give an Acked-by, that would be great.

Thanks!

Cheers,
Miguel

Reviewed-by on v1 1/1
#2 lore raw
In reply to Miguel Ojeda
Quoted 32 lines · Miguel Ojeda

From: Danilo Krummrich <[email protected]>

Stephen found a future build failure in linux-next [1]:

    error[E0277]: `*mut MyStruct` cannot be sent between threads safely
      --> samples/rust/rust_dma.rs:47:22
       |
    47 | impl pci::Driver for DmaSampleDriver {
       |                      ^^^^^^^^^^^^^^^ `*mut MyStruct` cannot be sent between threads safely

It is caused by the interaction between commit 935e1d90bf6f ("rust: pci:
require Send for Driver trait implementers") from the driver-core tree, which fixes a missing concurrency requirement, and commit 9901addae63b ("samples: rust: add Rust dma test sample driver") which adds a sample that does not satisfy that requirement.

Add a Send implementation to CoherentAllocation, which allows the sample (and other future users) to satisfy it.

---
 rust/kernel/dma.rs | 4 ++++
 1 file changed, 4 insertions(+)
rust/kernel/dma.rs +0 -0
index 9d00f9c49f47..18de693c4924 100644

rust/kernel/dma.rs fn drop(&mut self) { · line 305

Quoted 5 lines
rust/kernel/dma.rs +2 -0
@@ -301,6 +301,10 @@ fn drop(&mut self) {
     }
 }
 
+// SAFETY: It is safe to send a `CoherentAllocation` to another thread if `T`
+// can be send to another thread.

s/can be send/can be sent

Regards,
Boqun

Quoted 8 lines
rust/kernel/dma.rs +2 -0
+unsafe impl<T: AsBytes + FromBytes + Send> Send for CoherentAllocation<T> {}
+
 /// Reads a field of an item from an allocated region of structs.
 ///
 /// # Examples
Signature · 1 line
2.49.0
Applied
#3 lore raw
In reply to Miguel Ojeda
Quoted 17 lines · Miguel Ojeda

From: Danilo Krummrich <[email protected]>

Stephen found a future build failure in linux-next [1]:

    error[E0277]: `*mut MyStruct` cannot be sent between threads safely
      --> samples/rust/rust_dma.rs:47:22
       |
    47 | impl pci::Driver for DmaSampleDriver {
       |                      ^^^^^^^^^^^^^^^ `*mut MyStruct` cannot be sent between threads safely

It is caused by the interaction between commit 935e1d90bf6f ("rust: pci:
require Send for Driver trait implementers") from the driver-core tree, which fixes a missing concurrency requirement, and commit 9901addae63b ("samples: rust: add Rust dma test sample driver") which adds a sample that does not satisfy that requirement.

Quoted 7 lines

Add a Send implementation to CoherentAllocation, which allows the sample (and other future users) to satisfy it.

Applied to rust-next -- thanks everyone!

[ Added number to Closes. Fix typo spotted by Boqun. - Miguel ]

Cheers,
Miguel