Skip to contentSkip to conversation

Implement Send and Sync for clk

PATCH Applied Alice Ryhl 5 participants 20 messages Feb 23 – Apr 3

Subsystems (8)
DRM drivers Common clk Rust PWM +4 more subsystems
lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

v5 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 v5:
- Rebase on v7.0-rc1.
- Pick up Reviewed-by tags.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Pick up Reviewed-by tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

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: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20250904-clk-send-sync-3cfa7f4e1ce2

Best regards,

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

PATCH v5 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 4059aff34d096264bc7f3c8bfcb39eb3f42725be..7abbd0767d8cff9f2561d1783a9a1d774a96d1fc 100644
@@ -128,6 +128,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.53.0.345.g96ddfc5eaa-goog
author maintainer
#2 lore raw

PATCH v5 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 beeffe36b6cb068f2a87129872b37cbea4fb2010..e833e9f537b0225368ca10f5bc71b2612b0befa5 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.53.0.345.g96ddfc5eaa-goog
author maintainer
#3 lore raw

PATCH v5 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 b0e24ee724e4501b812e5f9c7904b483bfe5e2dc..6cd6fa3d298484cbd84b88cfdd3244217855fa05 100644
@@ -99,21 +99,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.53.0.345.g96ddfc5eaa-goog
maintainer
#4 lore raw
In reply to Alice Ryhl

Hi Stephen,

Quoted 22 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 v5:
- Rebase on v7.0-rc1.
- Pick up Reviewed-by tags.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Pick up Reviewed-by tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

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]

Quoted 4 lines

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

Alice has resent this over the past three cycles, but it hasn't been picked up yet. Please let me know if there are any concerns -- otherwise, I plan to take it through the drm-rust tree in a few days.

Thanks,
Danilo

#5 lore raw
Quoted 4 lines · Danilo Krummrich

Alice has resent this over the past three cycles, but it hasn't been picked up yet. Please let me know if there are any concerns -- otherwise, I plan to take it through the drm-rust tree in a few days.

+1, it will keep accumulating changes otherwise. Alice asked me about this one too; I can pick it up if needed too.

Cheers,
Miguel

#6 lore raw
In reply to Danilo Krummrich

Hi Danilo,

Quoted 25 lines · Danilo Krummrich

Hi Stephen,

Quoted 22 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 v5:
- Rebase on v7.0-rc1.
- Pick up Reviewed-by tags.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Pick up Reviewed-by tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

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]

Quoted 8 lines
Quoted 4 lines

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

Alice has resent this over the past three cycles, but it hasn't been picked up yet. Please let me know if there are any concerns -- otherwise, I plan to take it through the drm-rust tree in a few days.

Stephen hasn't really been active lately, but Brian (in Cc) has been trying to pick up the patches falling through the cracks. It looks like such a candidate to me.

Maxime

#7 lore raw

Hi Danilo and Alice,

Quoted 29 lines · Maxime Ripard
Quoted 28 lines · Danilo Krummrich
Quoted 26 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 v5:
- Rebase on v7.0-rc1.
- Pick up Reviewed-by tags.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Pick up Reviewed-by tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

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]

Quoted 7 lines
Quoted 3 lines

Alice has resent this over the past three cycles, but it hasn't been picked up yet. Please let me know if there are any concerns -- otherwise, I plan to take it through the drm-rust tree in a few days.

Stephen hasn't really been active lately, but Brian (in Cc) has been trying to pick up the patches falling through the cracks. It looks like such a candidate to me.

I'm planning to send another clk pull at the end of this week and I'll include this series.

Brian

#8 lore raw
Quoted 3 lines · Brian Masney

I'm planning to send another clk pull at the end of this week and I'll include this series.

Thanks Brian!

Cheers,
Miguel

#9 lore raw
In reply to Brian Masney

Hi Danilo / Alice / Miguel,

Quoted 34 lines · Brian Masney
Quoted 33 lines · Maxime Ripard
Quoted 31 lines · Danilo Krummrich
Quoted 22 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 v5:
- Rebase on v7.0-rc1.
- Pick up Reviewed-by tags.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Pick up Reviewed-by tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

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]

Quoted 4 lines

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

Alice has resent this over the past three cycles, but it hasn't been picked up yet. Please let me know if there are any concerns -- otherwise, I plan to take it through the drm-rust tree in a few days.

Quoted 6 lines
Quoted 3 lines

Stephen hasn't really been active lately, but Brian (in Cc) has been trying to pick up the patches falling through the cracks. It looks like such a candidate to me.

I'm planning to send another clk pull at the end of this week and I'll include this series.

Stephen came on the list and started to merge this things this week. I was going to collect up more patches for him if he wasn't around, and intended to include this. However, since he's starting to collect things, I'm not going to send him a clk pull today.

It's up to you if you want to send this in via drm-rust or wait for Stephen.

Brian

#10 lore raw
Quoted 7 lines · Brian Masney

Stephen came on the list and started to merge this things this week. I was going to collect up more patches for him if he wasn't around, and intended to include this. However, since he's starting to collect things, I'm not going to send him a clk pull today.

It's up to you if you want to send this in via drm-rust or wait for Stephen.

I guess it depends if he plans to pick it up this cycle or not.

If he doesn't, then it may be best to just go forward with your Acked-bys -- Danilo can pick it up if DRM needs it in their branch, otherwise I can pick it up.

Thanks!

Cheers,
Miguel

#11 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 6 lines · Brian Masney

Stephen came on the list and started to merge this things this week. I was going to collect up more patches for him if he wasn't around, and intended to include this. However, since he's starting to collect things, I'm not going to send him a clk pull today.

Quoted 7 lines
Quoted 1 line

It's up to you if you want to send this in via drm-rust or wait for Stephen.

I guess it depends if he plans to pick it up this cycle or not.

If he doesn't, then it may be best to just go forward with your Acked-bys -- Danilo can pick it up if DRM needs it in their branch, otherwise I can pick it up.

I am not a clk maintainer, so I can't leave an Acked-by for you to pick this up unfortunately. I've been quite active in the clk subsystem though the last 6 months or so.

Brian

maintainer
#12 lore raw
Quoted 3 lines · Brian Masney

I am not a clk maintainer, so I can't leave an Acked-by for you to pick this up unfortunately. I've been quite active in the clk subsystem though the last 6 months or so.

I'm not involved in the CLK subsystem, but maybe it would be a good chance to offer stepping up as co-maintainer (also given that Michael seems to be inactive for a couple of years now). :)

#13 lore raw
Quoted 8 lines · Danilo Krummrich
Quoted 3 lines · Brian Masney

I am not a clk maintainer, so I can't leave an Acked-by for you to pick this up unfortunately. I've been quite active in the clk subsystem though the last 6 months or so.

I'm not involved in the CLK subsystem, but maybe it would be a good chance to offer stepping up as co-maintainer (also given that Michael seems to be inactive for a couple of years now). :)

I mentioned to Stephen at the last LPC in Tokyo that I am interested in becoming a clk co-maintainer. I've sent him some git pulls for some of my work the last few development cycles, and he's pulled them. For this development cycle, I collected up some patches from others that I feel are ready, however I didn't hear back from Stephen yet:

https://lore.kernel.org/linux-clk/[email protected]/

He separately pulled some of that work via the posted patches into clk-next this week. I don't know if there was something he didn't like in my git pull.

To be honest, I initially helped with reviews trying to free up time for him to give feedback about my work to address some long-standing issues related to clk scaling that affect DRM and sound:

https://lore.kernel.org/linux-clk/[email protected]/
The kunit tests in patch 2 and 4 clearly show the issues. (I'm working on some fixes to this series right now based on feedback from Sashiko.)

I've become more interested in the clk subsystem as I learn more about it. There are other improvements that I am willing to make to the clk subsystem, however the current situation has been quite challenging to be honest. I will help with the linux-clk subsystem for the long term if there is the possibility of me eventually becoming a co-maintainer. Stephen can have veto power over what goes in.

Brian

#14 lore raw
Quoted 3 lines · Brian Masney

I mentioned to Stephen at the last LPC in Tokyo that I am interested in becoming a clk co-maintainer.

Thanks for the context, that sounds great -- more maintainers and reviewers are needed in the kernel.

But, of course, it is up to Stephen.

Another possibility Stephen could perhaps like if he doesn't want a co-maintainer for everything is having a sub-maintainer for the Rust side, possibly in a sub-entry (e.g. "COMMON CLK FRAMEWORK [RUST]", i.e. with the usual square brackets we use for that), assuming you are interested in that.

It is an approach that we have seen works well, because it is usually a win-win for maintainers that cannot commit time to review new Rust stuff, i.e. they get more people in their subsystem, they can use the opportunity to learn Rust (assuming they don't know it yet) and they can assess how often Rust changes are needed, yet they still control the changes (e.g. in cases a change on both sides needs to happen at once).

[ In any case, I don't know the context behind all this -- if I can help with anything, please let me know. ]

Thanks!

Cheers,
Miguel

#15 lore raw
In reply to Brian Masney
Quoted 4 lines · Brian Masney

I am not a clk maintainer, so I can't leave an Acked-by for you to pick this up unfortunately. I've been quite active in the clk subsystem though the last 6 months or so.

In that case, I could take your Acked-by as an active contributor of clk if you are willing to provide it, i.e. with the "# Suffix" notation I introduced a year ago:

https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by

cd9123eeb224 ("docs: submitting-patches: clarify Acked-by and
introduce "# Suffix"")

i.e. Acked-bys can nowadays be provided by other stakeholders apart from maintainers, e.g. people with domain knowledge, userspace reviewers of uAPI patches, key users and so on.

Especially so since you offered to be a co-maintainer long term, so I think having your Acked-by is way better that having no tag at all :)

Cheers,
Miguel

Acked-by
#16 lore raw
Quoted 14 lines · Brian Masney
Quoted 4 lines

I am not a clk maintainer, so I can't leave an Acked-by for you to pick this up unfortunately. I've been quite active in the clk subsystem though the last 6 months or so.

In that case, I could take your Acked-by as an active contributor of clk if you are willing to provide it, i.e. with the "# Suffix" notation I introduced a year ago:

https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by

cd9123eeb224 ("docs: submitting-patches: clarify Acked-by and
introduce "# Suffix"")

Quoted 6 lines

i.e. Acked-bys can nowadays be provided by other stakeholders apart from maintainers, e.g. people with domain knowledge, userspace reviewers of uAPI patches, key users and so on.

Especially so since you offered to be a co-maintainer long term, so I think having your Acked-by is way better that having no tag at all :)

Sure...

author maintainer
#17 lore raw
In reply to Miguel Ojeda
Quoted 7 lines · Miguel Ojeda
Quoted 6 lines · Brian Masney

Stephen came on the list and started to merge this things this week. I was going to collect up more patches for him if he wasn't around, and intended to include this. However, since he's starting to collect things, I'm not going to send him a clk pull today.

Quoted 7 lines
Quoted 1 line

It's up to you if you want to send this in via drm-rust or wait for Stephen.

I guess it depends if he plans to pick it up this cycle or not.

If he doesn't, then it may be best to just go forward with your Acked-bys -- Danilo can pick it up if DRM needs it in their branch, otherwise I can pick it up.

The drm-rust-next branch has closed now that -rc6 is out. If we don't hear from Stephen, do you mind picking it up Miguel?

Alice

#18 lore raw
Quoted 3 lines · Alice Ryhl

The drm-rust-next branch has closed now that -rc6 is out. If we don't hear from Stephen, do you mind picking it up Miguel?

Yeah, I will pick them -- I talked with Danilo about it a few days ago.

Cheers,
Miguel

Applied
#19 lore raw
In reply to Alice Ryhl
Quoted 3 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.

Applied to rust-next -- thanks everyone!

(Given we heard nothing this week either)

Cheers,
Miguel