rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: devres: require T: Send for Devres
@ 2025-06-26 13:24 Danilo Krummrich
  2025-06-26 13:44 ` Benno Lossin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Danilo Krummrich @ 2025-06-26 13:24 UTC (permalink / raw)
  To: gregkh, rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel, Danilo Krummrich

Due to calling Revocable::revoke() from Devres::devres_callback() T may
be dropped from Devres::devres_callback() and hence must be Send.

Fix this by adding the corresponding bound to Devres and DevresInner.

Reported-by: Boqun Feng <boqun.feng@gmail.com>
Closes: https://lore.kernel.org/lkml/aFzI5L__OcB9hqdG@Mac.home/
Fixes: 76c01ded724b ("rust: add devres abstraction")
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/devres.rs | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
index 57502534d985..8ede607414fd 100644
--- a/rust/kernel/devres.rs
+++ b/rust/kernel/devres.rs
@@ -18,7 +18,7 @@
 };
 
 #[pin_data]
-struct DevresInner<T> {
+struct DevresInner<T: Send> {
     dev: ARef<Device>,
     callback: unsafe extern "C" fn(*mut c_void),
     #[pin]
@@ -95,9 +95,9 @@ struct DevresInner<T> {
 /// # Ok(())
 /// # }
 /// ```
-pub struct Devres<T>(Arc<DevresInner<T>>);
+pub struct Devres<T: Send>(Arc<DevresInner<T>>);
 
-impl<T> DevresInner<T> {
+impl<T: Send> DevresInner<T> {
     fn new(dev: &Device<Bound>, data: T, flags: Flags) -> Result<Arc<DevresInner<T>>> {
         let inner = Arc::pin_init(
             pin_init!( DevresInner {
@@ -175,7 +175,7 @@ fn remove_action(this: &Arc<Self>) -> bool {
     }
 }
 
-impl<T> Devres<T> {
+impl<T: Send> Devres<T> {
     /// Creates a new [`Devres`] instance of the given `data`. The `data` encapsulated within the
     /// returned `Devres` instance' `data` will be revoked once the device is detached.
     pub fn new(dev: &Device<Bound>, data: T, flags: Flags) -> Result<Self> {
@@ -247,7 +247,7 @@ pub fn try_access_with_guard<'a>(&'a self, guard: &'a rcu::Guard) -> Option<&'a
     }
 }
 
-impl<T> Drop for Devres<T> {
+impl<T: Send> Drop for Devres<T> {
     fn drop(&mut self) {
         // SAFETY: When `drop` runs, it is guaranteed that nobody is accessing the revocable data
         // anymore, hence it is safe not to wait for the grace period to finish.

base-commit: 86731a2a651e58953fc949573895f2fa6d456841
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: devres: require T: Send for Devres
  2025-06-26 13:24 [PATCH] rust: devres: require T: Send for Devres Danilo Krummrich
@ 2025-06-26 13:44 ` Benno Lossin
  2025-06-26 16:21 ` Boqun Feng
  2025-06-29 15:08 ` Danilo Krummrich
  2 siblings, 0 replies; 4+ messages in thread
From: Benno Lossin @ 2025-06-26 13:44 UTC (permalink / raw)
  To: Danilo Krummrich, gregkh, rafael, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, a.hindborg, aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel

On Thu Jun 26, 2025 at 3:24 PM CEST, Danilo Krummrich wrote:
> Due to calling Revocable::revoke() from Devres::devres_callback() T may
> be dropped from Devres::devres_callback() and hence must be Send.
>
> Fix this by adding the corresponding bound to Devres and DevresInner.
>
> Reported-by: Boqun Feng <boqun.feng@gmail.com>
> Closes: https://lore.kernel.org/lkml/aFzI5L__OcB9hqdG@Mac.home/
> Fixes: 76c01ded724b ("rust: add devres abstraction")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/devres.rs | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)

It would be enough to require `Send` on all `impl` blocks and not on the
struct itself. Normally, I recommend doing that, because then you can
have types like `Foo<Invalid>` existing when they come up in some weird
type. But in this case, I think it's fine to add it to the type
definition.

Reviewed-by: Benno Lossin <lossin@kernel.org>

---
Cheers,
Benno

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: devres: require T: Send for Devres
  2025-06-26 13:24 [PATCH] rust: devres: require T: Send for Devres Danilo Krummrich
  2025-06-26 13:44 ` Benno Lossin
@ 2025-06-26 16:21 ` Boqun Feng
  2025-06-29 15:08 ` Danilo Krummrich
  2 siblings, 0 replies; 4+ messages in thread
From: Boqun Feng @ 2025-06-26 16:21 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: gregkh, rafael, ojeda, alex.gaynor, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, rust-for-linux, linux-kernel

On Thu, Jun 26, 2025 at 03:24:46PM +0200, Danilo Krummrich wrote:
> Due to calling Revocable::revoke() from Devres::devres_callback() T may
> be dropped from Devres::devres_callback() and hence must be Send.
> 
> Fix this by adding the corresponding bound to Devres and DevresInner.
> 
> Reported-by: Boqun Feng <boqun.feng@gmail.com>
> Closes: https://lore.kernel.org/lkml/aFzI5L__OcB9hqdG@Mac.home/
> Fixes: 76c01ded724b ("rust: add devres abstraction")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Boqun Feng <boqun.fenng@gmail.com>

Thanks!

Regards,
Boqun

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: devres: require T: Send for Devres
  2025-06-26 13:24 [PATCH] rust: devres: require T: Send for Devres Danilo Krummrich
  2025-06-26 13:44 ` Benno Lossin
  2025-06-26 16:21 ` Boqun Feng
@ 2025-06-29 15:08 ` Danilo Krummrich
  2 siblings, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2025-06-29 15:08 UTC (permalink / raw)
  To: gregkh, rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross
  Cc: rust-for-linux, linux-kernel

On Thu, Jun 26, 2025 at 03:24:46PM +0200, Danilo Krummrich wrote:
> Due to calling Revocable::revoke() from Devres::devres_callback() T may
> be dropped from Devres::devres_callback() and hence must be Send.
> 
> Fix this by adding the corresponding bound to Devres and DevresInner.
> 
> Reported-by: Boqun Feng <boqun.feng@gmail.com>
> Closes: https://lore.kernel.org/lkml/aFzI5L__OcB9hqdG@Mac.home/
> Fixes: 76c01ded724b ("rust: add devres abstraction")
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Applied to driver-core-testing, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-29 15:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-26 13:24 [PATCH] rust: devres: require T: Send for Devres Danilo Krummrich
2025-06-26 13:44 ` Benno Lossin
2025-06-26 16:21 ` Boqun Feng
2025-06-29 15:08 ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).