Skip to contentSkip to conversation

Implement Send and Sync for clk

PATCH Superseded Alice Ryhl 3 participants 6 messages Dec 18, 2025 – Jan 7

Subsystems (9)
DRM drivers Common clk RISC-V Rust +5 more subsystems

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

v3 cover Implement Send and Sync for clk

The Clk type is thread-safe, so let's mark it as thread-safe in the type system. This lets us get rid of hacks in drivers.

For Stephen's clk tree.

---
Changes in v3:
- Rebase on v6.19-rc1.
- Pick up tags.
- Add fix for pwm driver as well.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebase on v6.18-rc1.
- Add patch to tyr driver.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Alice Ryhl (3):
rust: clk: implement Send and Sync
tyr: remove impl Send/Sync for TyrData
pwm: th1520: remove impl Send/Sync for Th1520PwmDriverData

 drivers/gpu/drm/tyr/driver.rs | 12 ------------
 drivers/pwm/pwm_th1520.rs     | 15 ---------------
 rust/kernel/clk.rs            |  7 +++++++
 3 files changed, 7 insertions(+), 27 deletions(-)

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/3 rust: clk: implement Send and Sync

These traits are required for drivers to embed the Clk type in their own data structures because driver data structures are usually required to be Send. Since the Clk type is thread-safe, implement the relevant traits.

---
 rust/kernel/clk.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/clk.rs +7 -0
index c1cfaeaa36a22be2e3180b1e9142bb608ab276ea..d192fbd97861212d738d24510eebcd99c9177f2c 100644
@@ -129,6 +129,13 @@ mod common_clk {
     #[repr(transparent)]
     pub struct Clk(*mut bindings::clk);
 
+    // SAFETY: It is safe to call `clk_put` on another thread than where `clk_get` was called.
+    unsafe impl Send for Clk {}
+
+    // SAFETY: It is safe to call any combination of the `&self` methods in parallel, as the
+    // methods are synchronized internally.
+    unsafe impl Sync for Clk {}
+
     impl Clk {
         /// Gets [`Clk`] corresponding to a [`Device`] and a connection id.
         ///
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author maintainer
#2 lore raw

PATCH v3 2/3 tyr: remove impl Send/Sync for TyrData

In reply to Alice Ryhl

Now that clk implements Send and Sync, we no longer need to manually implement these traits for TyrData. Thus remove the implementations.

The comment also mentions the regulator. However, the regulator had the traits added in commit 9a200cbdb543 ("rust: regulator: implement Send and Sync for Regulator<T>"), which is already in mainline.

---
 drivers/gpu/drm/tyr/driver.rs | 12 ------------
 1 file changed, 12 deletions(-)
drivers/gpu/drm/tyr/driver.rs +0 -12
index 0389c558c0367522471ea78fcf72a6b58c4a3650..09711fb7fe0b1c83b72bffba06f5a76c53244f4d 100644
@@ -53,18 +53,6 @@ pub(crate) struct TyrData {
     pub(crate) gpu_info: GpuInfo,
 }
 
-// Both `Clk` and `Regulator` do not implement `Send` or `Sync`, but they
-// should. There are patches on the mailing list to address this, but they have
-// not landed yet.
-//
-// For now, add this workaround so that this patch compiles with the promise
-// that it will be removed in a future patch.
-//
-// SAFETY: This will be removed in a future patch.
-unsafe impl Send for TyrData {}
-// SAFETY: This will be removed in a future patch.
-unsafe impl Sync for TyrData {}
-
 fn issue_soft_reset(dev: &Device<Bound>, iomem: &Devres<IoMem>) -> Result {
     regs::GPU_CMD.write(dev, iomem, regs::GPU_CMD_SOFT_RESET)?;
 
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author maintainer
#3 lore raw

PATCH v3 3/3 pwm: th1520: remove impl Send/Sync for Th1520PwmDriverData

In reply to Alice Ryhl

Now that clk implements Send and Sync, we no longer need to manually implement these traits for Th1520PwmDriverData. Thus remove the implementations.

---
 drivers/pwm/pwm_th1520.rs | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/pwm/pwm_th1520.rs +0 -15
index e3b7e77356fc2492077c519073e861beb3e44df9..043dc4dbc6232020195c7b73fad302bbb69652df 100644
@@ -97,21 +97,6 @@ struct Th1520PwmDriverData {
     clk: Clk,
 }
 
-// This `unsafe` implementation is a temporary necessity because the underlying `kernel::clk::Clk`
-// type does not yet expose `Send` and `Sync` implementations. This block should be removed
-// as soon as the clock abstraction provides these guarantees directly.
-// TODO: Remove those unsafe impl's when Clk will support them itself.
-
-// SAFETY: The `devres` framework requires the driver's private data to be `Send` and `Sync`.
-// We can guarantee this because the PWM core synchronizes all callbacks, preventing concurrent
-// access to the contained `iomem` and `clk` resources.
-unsafe impl Send for Th1520PwmDriverData {}
-
-// SAFETY: The same reasoning applies as for `Send`. The PWM core's synchronization
-// guarantees that it is safe for multiple threads to have shared access (`&self`)
-// to the driver data during callbacks.
-unsafe impl Sync for Th1520PwmDriverData {}
-
 impl pwm::PwmOps for Th1520PwmDriverData {
     type WfHw = Th1520WfHw;
 
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
maintainer Reviewed-by on the v3 series
#4 lore raw
In reply to Alice Ryhl
Quoted 32 lines · Alice Ryhl

The Clk type is thread-safe, so let's mark it as thread-safe in the type system. This lets us get rid of hacks in drivers.

For Stephen's clk tree.

---
Changes in v3:
- Rebase on v6.19-rc1.
- Pick up tags.
- Add fix for pwm driver as well.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebase on v6.18-rc1.
- Add patch to tyr driver.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Alice Ryhl (3):
rust: clk: implement Send and Sync
tyr: remove impl Send/Sync for TyrData
pwm: th1520: remove impl Send/Sync for Th1520PwmDriverData

drivers/gpu/drm/tyr/driver.rs | 12 ------------
drivers/pwm/pwm_th1520.rs     | 15 ---------------
rust/kernel/clk.rs            |  7 +++++++
3 files changed, 7 insertions(+), 27 deletions(-)
Quoted 8 lines

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>

I thought this had landed already.

I’ll resume my overhaul of clk .rs during this cycle by the way.

For patch 3:

reviewer Reviewed-by on the v3 series
#5 lore raw
In reply to Alice Ryhl
Quoted 6 lines · On Thu, 18 Dec 2025 13:27:39 +0000 Alice Ryhl <[email protected]> wrote:

The Clk type is thread-safe, so let's mark it as thread-safe in the type system. This lets us get rid of hacks in drivers.

For Stephen's clk tree.

For the series:

Best,
Gary

Quoted 27 lines

---
Changes in v3:
- Rebase on v6.19-rc1.
- Pick up tags.
- Add fix for pwm driver as well.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebase on v6.18-rc1.
- Add patch to tyr driver.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Alice Ryhl (3):
rust: clk: implement Send and Sync
tyr: remove impl Send/Sync for TyrData
pwm: th1520: remove impl Send/Sync for Th1520PwmDriverData

 drivers/gpu/drm/tyr/driver.rs | 12 ------------
 drivers/pwm/pwm_th1520.rs     | 15 ---------------
 rust/kernel/clk.rs            |  7 +++++++
 3 files changed, 7 insertions(+), 27 deletions(-)

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Best regards,