Skip to contentSkip to conversation

rust: clk: implement Send and Sync

PATCH Superseded Alice Ryhl 4 participants 7 messages Sep 4, 2025 – Sep 10, 2025

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alice Ryhl

author reviewer
#0 lore raw

PATCH 1/1 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. See e.g. [1] for the kind of workaround that drivers currently need due to lacking this annotation.

---
I'm not sure if there was already sent a patch for this. I recall being told that one had been sent, but I could not find it. Maybe I mixed it up with the regulator change, so now I'm sending a change for clk.

---
 rust/kernel/clk.rs | 7 +++++++
 1 file changed, 7 insertions(+)
rust/kernel/clk.rs +7 -0
index 1e6c8c42fb3a321951e275101848b35e1ae5c2a8..0a290202da69669d670ddad2b6762a1d5f1d912e 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.
         ///

---
base-commit: 1b237f190eb3d36f52dffe07a40b5eb210280e00
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Alice Ryhl

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. See e.g. [1] for the kind of workaround that drivers currently need due to lacking this annotation.

Quoted 8 lines

---
I'm not sure if there was already sent a patch for this. I recall being told that one had been sent, but I could not find it. Maybe I mixed it up with the regulator change, so now I'm sending a change for clk.

---
 rust/kernel/clk.rs | 7 +++++++
 1 file changed, 7 insertions(+)
Signature · 1 line
viresh
#2 lore raw
In reply to Alice Ryhl

Hi Alice, good catch, this is indeed missing.

Quoted 41 lines · Alice Ryhl

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. See e.g. [1] for the kind of workaround that drivers currently need due to lacking this annotation.

---
I'm not sure if there was already sent a patch for this. I recall being told that one had been sent, but I could not find it. Maybe I mixed it up with the regulator change, so now I'm sending a change for clk.
---
rust/kernel/clk.rs | 7 +++++++

1 file changed, 7 insertions(+)
rust/kernel/clk.rs +7 -1
index 1e6c8c42fb3a321951e275101848b35e1ae5c2a8..0a290202da69669d670ddad2b6762a1d5f1d912e 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.
        ///
 
---

base-commit: 1b237f190eb3d36f52dffe07a40b5eb210280e00
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Quoted 4 lines

Best regards,

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

Can you base your change on top of [0]? Otherwise it will become stale rather quickly, as this introduces new types, i.e.: Clk<Unprepared>, Clk<Prepared> etc.

I will push out a new version today.

[0]: https://lore.kernel.org/rust-for-linux/[email protected]/

author reviewer
#3 lore raw
Quoted 7 lines · Daniel Almeida

Can you base your change on top of [0]? Otherwise it will become stale rather quickly, as this introduces new types, i.e.: Clk<Unprepared>, Clk<Prepared> etc.

I will push out a new version today.

[0]: https://lore.kernel.org/rust-for-linux/[email protected]/

I would suggest the reverse. If your series is based on top of mine, then this patch can land today regardless of how long it takes to finish the clk refactor.

Alice

reviewer Reviewed-by
#4 lore raw
Quoted 12 lines · Alice Ryhl
Quoted 6 lines · Daniel Almeida

Can you base your change on top of [0]? Otherwise it will become stale rather quickly, as this introduces new types, i.e.: Clk<Unprepared>, Clk<Prepared> etc.

I will push out a new version today.

Quoted 8 lines
Quoted 1 line

[0]: https://lore.kernel.org/rust-for-linux/[email protected]/

I would suggest the reverse. If your series is based on top of mine, then this patch can land today regardless of how long it takes to finish the clk refactor.

Alice

True,

Ok then:

reviewer Reviewed-by on v1 1/1
#5 lore raw
In reply to Alice Ryhl
Quoted 7 lines · Alice Ryhl

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. See e.g. [1] for the kind of workaround that drivers currently need due to lacking this annotation.

#6 lore raw
In reply to Alice Ryhl
Quoted 43 lines · Alice Ryhl

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. See e.g. [1] for the kind of workaround that drivers currently need due to lacking this annotation.

---
I'm not sure if there was already sent a patch for this. I recall being told that one had been sent, but I could not find it. Maybe I mixed it up with the regulator change, so now I'm sending a change for clk.
---
rust/kernel/clk.rs | 7 +++++++

1 file changed, 7 insertions(+)
rust/kernel/clk.rs +7 -1
index 1e6c8c42fb3a321951e275101848b35e1ae5c2a8..0a290202da69669d670ddad2b6762a1d5f1d912e 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.
        ///
 
---

base-commit: 1b237f190eb3d36f52dffe07a40b5eb210280e00
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Quoted 5 lines

Best regards,

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

I’ve included this on my series here [0], as it would otherwise conflict with the type-state work.

I did not see anyone against the type-state patch itself, so perhaps both can go together.

— Daniel

[0]: https://lore.kernel.org/rust-for-linux/[email protected]/T/#t