Skip to contentSkip to conversation

rust: pci: impl Send + Sync for pci::Device

PATCH In review Danilo Krummrich 3 participants 12 messages Mar 18, 2025 – Mar 19, 2025

Subsystems (4)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/2 rust: pci: impl Send + Sync for pci::Device

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

---
 rust/kernel/pci.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/pci.rs +7 -0
index 0ac6cef74f81..0d09ae34a64d 100644
@@ -465,3 +465,10 @@ fn as_ref(&self) -> &device::Device {
         unsafe { device::Device::as_ref(dev) }
     }
 }
+
+// SAFETY: A `Device` is always reference-counted and can be released from any thread.
+unsafe impl Send for Device {}
+
+// SAFETY: `Device` can be shared among threads because all methods of `Device`
+// (i.e. `Device<Normal>) are thread safe.
+unsafe impl Sync for Device {}

base-commit: 4d320e30ee04c25c660eca2bb33e846ebb71a79a

Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 2/2 rust: platform: impl Send + Sync for platform::Device

Commit 4d320e30ee04 ("rust: platform: fix unrestricted &mut platform::Device") changed the definition of platform::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

---
 rust/kernel/platform.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/platform.rs +7 -0
index c77c9f2e9aea..2811ca53d8b6 100644
@@ -233,3 +233,10 @@ fn as_ref(&self) -> &device::Device {
         unsafe { device::Device::as_ref(dev) }
     }
 }
+
+// SAFETY: A `Device` is always reference-counted and can be released from any thread.
+unsafe impl Send for Device {}
+
+// SAFETY: `Device` can be shared among threads because all methods of `Device`
+// (i.e. `Device<Normal>) are thread safe.
+unsafe impl Sync for Device {}
Signature · 1 line
2.48.1
reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] rust: platform: impl Send + Sync for platform::Device

Quoted 18 lines · Danilo Krummrich

Commit 4d320e30ee04 ("rust: platform: fix unrestricted &mut platform::Device") changed the definition of platform::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

---
 rust/kernel/platform.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/platform.rs +0 -0
index c77c9f2e9aea..2811ca53d8b6 100644

rust/kernel/platform.rs fn as_ref(&self) -> &device::Device { · line 242

Quoted 10 lines
rust/kernel/platform.rs +7 -0
@@ -233,3 +233,10 @@ fn as_ref(&self) -> &device::Device {
         unsafe { device::Device::as_ref(dev) }
     }
 }
+
+// SAFETY: A `Device` is always reference-counted and can be released from any thread.
+unsafe impl Send for Device {}
+
+// SAFETY: `Device` can be shared among threads because all methods of `Device`
+// (i.e. `Device<Normal>) are thread safe.
+unsafe impl Sync for Device {}

Same as for 1/2:

  # git am --show-current-patch=diff | patch -p1
  patching file rust/kernel/platform.rs
  Hunk #1 succeeded at 198 with fuzz 1 (offset -35 lines).

Best regards,
Andreas Hindborg

reviewer Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] rust: platform: impl Send + Sync for platform::Device

In reply to Danilo Krummrich
Quoted 9 lines · Danilo Krummrich

Commit 4d320e30ee04 ("rust: platform: fix unrestricted &mut platform::Device") changed the definition of platform::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

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

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

---
 rust/kernel/pci.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/pci.rs +7 -0
index 0ac6cef74f81..0d09ae34a64d 100644
@@ -465,3 +465,10 @@ fn as_ref(&self) -> &device::Device {
         unsafe { device::Device::as_ref(dev) }
     }
 }
+
+// SAFETY: A `Device` is always reference-counted and can be released from any thread.
+unsafe impl Send for Device {}
+
+// SAFETY: `Device` can be shared among threads because all methods of `Device`
+// (i.e. `Device<Normal>) are thread safe.
+unsafe impl Sync for Device {}
Quoted 1 line

base-commit: 4d320e30ee04c25c660eca2bb33e846ebb71a79a

I can't find the base-commit and the patch does not apply clean on v6.14-rc7:

patching file rust/kernel/pci.rs
Hunk #1 succeeded at 432 with fuzz 1 (offset -33 lines).

Otherwise looks good. You might want to rebase?

Best regards,
Andreas Hindborg

author maintainer
#5 lore raw
Quoted 31 lines · Andreas Hindborg
Quoted 29 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

---
 rust/kernel/pci.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/pci.rs +7 -0
index 0ac6cef74f81..0d09ae34a64d 100644
@@ -465,3 +465,10 @@ fn as_ref(&self) -> &device::Device {
         unsafe { device::Device::as_ref(dev) }
     }
 }
+
+// SAFETY: A `Device` is always reference-counted and can be released from any thread.
+unsafe impl Send for Device {}
+
+// SAFETY: `Device` can be shared among threads because all methods of `Device`
+// (i.e. `Device<Normal>) are thread safe.
+unsafe impl Sync for Device {}
Quoted 8 lines
Quoted 1 line

base-commit: 4d320e30ee04c25c660eca2bb33e846ebb71a79a

I can't find the base-commit and the patch does not apply clean on v6.14-rc7:

patching file rust/kernel/pci.rs
Hunk #1 succeeded at 432 with fuzz 1 (offset -33 lines).

Otherwise looks good. You might want to rebase?

This is intentional, the base commit is from the driver-core tree, where also commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") landed.

Quoted 7 lines

Best regards,
Andreas Hindborg

reviewer Reviewed-by on v1 1/2
#6 lore raw
In reply to Danilo Krummrich
Quoted 9 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

I have a question related to this ... does the Driver trait need to require T: Send?

The change itself LGTM, so:

Alice

author maintainer
#7 lore raw
Quoted 5 lines · Alice Ryhl
Quoted 4 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

Quoted 8 lines
Quoted 5 lines

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

I have a question related to this ... does the Driver trait need to require T: Send?

The driver trait does not have a generic, it doesn't need one. But I think I still get what you're asking.

The driver trait never owns a shared reference of the device, it only ever gives out a reference that the driver core guarantees to be valid.

Quoted 5 lines

The change itself LGTM, so:

Alice

reviewer
#8 lore raw
Quoted 10 lines · Danilo Krummrich
Quoted 9 lines · Alice Ryhl
Quoted 8 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I have a question related to this ... does the Driver trait need to require T: Send?

The driver trait does not have a generic, it doesn't need one. But I think I still get what you're asking.

Right I mean, should it be:

trait Driver: Send + Sync { 
    ...
}
Quoted 8 lines

The driver trait never owns a shared reference of the device, it only ever gives out a reference that the driver core guarantees to be valid.

Quoted 5 lines

The change itself LGTM, so:

Alice

author maintainer
#9 lore raw
Quoted 11 lines · Alice Ryhl
Quoted 10 lines · Danilo Krummrich
Quoted 9 lines · Alice Ryhl
Quoted 8 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I have a question related to this ... does the Driver trait need to require T: Send?

The driver trait does not have a generic, it doesn't need one. But I think I still get what you're asking.

Turns out I did not. :)

Quoted 5 lines

Right I mean, should it be:

trait Driver: Send + Sync {
    ...
}

Yes, you're absolutely right with this, thanks for pointing this out.

Quoted 9 lines
Quoted 9 lines

The driver trait never owns a shared reference of the device, it only ever gives out a reference that the driver core guarantees to be valid.

Quoted 5 lines

The change itself LGTM, so:

Alice

author maintainer
#10 lore raw
Quoted 22 lines · Danilo Krummrich
Quoted 11 lines · Alice Ryhl
Quoted 10 lines · Danilo Krummrich
Quoted 9 lines · Alice Ryhl
Quoted 8 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I have a question related to this ... does the Driver trait need to require T: Send?

The driver trait does not have a generic, it doesn't need one. But I think I still get what you're asking.

Turns out I did not. :)

Quoted 7 lines
Quoted 5 lines

Right I mean, should it be:

trait Driver: Send + Sync {
    ...
}

Yes, you're absolutely right with this, thanks for pointing this out.

Just to clarify, the reason we need Sync is that we want to be able to access the driver's private data from an IRQ handler. Otherwise, we can only ever safely access the driver's private data from bus callbacks, which should be synchronized by the device' mutex.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

The driver trait never owns a shared reference of the device, it only ever gives out a reference that the driver core guarantees to be valid.

Quoted 5 lines

The change itself LGTM, so:

Alice

author maintainer
#11 lore raw
Quoted 29 lines · Danilo Krummrich
Quoted 28 lines · Danilo Krummrich
Quoted 11 lines · Alice Ryhl
Quoted 10 lines · Danilo Krummrich
Quoted 9 lines · Alice Ryhl
Quoted 8 lines · Danilo Krummrich

Commit 7b948a2af6b5 ("rust: pci: fix unrestricted &mut pci::Device") changed the definition of pci::Device and discarded the implicitly derived Send and Sync traits.

This isn't required by upstream code yet, and hence did not cause any issues. However, it is relied on by upcoming drivers, hence add it back in.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I have a question related to this ... does the Driver trait need to require T: Send?

The driver trait does not have a generic, it doesn't need one. But I think I still get what you're asking.

Turns out I did not. :)

Quoted 5 lines

Right I mean, should it be:

trait Driver: Send + Sync {
    ...
}
Quoted 6 lines
Quoted 1 line

Yes, you're absolutely right with this, thanks for pointing this out.

Just to clarify, the reason we need Sync is that we want to be able to access the driver's private data from an IRQ handler. Otherwise, we can only ever safely access the driver's private data from bus callbacks, which should be synchronized by the device' mutex.

On the other hand, that's up to the IRQ handler abstraction. So, we really should be good with only requiring Send.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

The driver trait never owns a shared reference of the device, it only ever gives out a reference that the driver core guarantees to be valid.

Quoted 5 lines

The change itself LGTM, so:

Alice