Skip to contentSkip to conversation

rust: devres: add 'static bound to Devres<T>

PATCH Applied Danilo Krummrich 4 participants 12 messages May 26 – May 28

Subsystems (2)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 rust: devres: add 'static bound to Devres<T>

Devres::new() registers a callback with the C devres subsystem via devres_node_add(). If the Devres is leaked (e.g. via core::mem::forget(), which is safe), its Drop impl never runs, and the devres release callback will revoke the inner Revocable on device unbind, which drops T in place. If T contains non-'static references, those may be dangling by that point.

Add a 'static bound to prevent storing types with borrowed data in Devres.

Fixes 76c01ded724b ("rust: add devres abstraction")
---
 rust/kernel/devres.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
rust/kernel/devres.rs +3 -3
index 9e5f93aed20c..2e258d31a45c 100644
@@ -122,7 +122,7 @@ struct Inner<T> {
 /// # Ok(())
 /// # }
 /// ```
-pub struct Devres<T: Send> {
+pub struct Devres<T: Send + 'static> {
     dev: ARef<Device>,
     inner: Arc<Inner<T>>,
 }
@@ -184,7 +184,7 @@ pub(super) unsafe fn devres_node_remove(
     }
 }
 
-impl<T: Send> Devres<T> {
+impl<T: Send + 'static> Devres<T> {
     /// Creates a new [`Devres`] instance of the given `data`.
     ///
     /// The `data` encapsulated within the returned `Devres` instance' `data` will be
@@ -349,7 +349,7 @@ unsafe impl<T: Send> Send for Devres<T> {}
 // SAFETY: `Devres` can be shared with any task, if `T: Sync`.
 unsafe impl<T: Send + Sync> Sync for Devres<T> {}
 
-impl<T: Send> Drop for Devres<T> {
+impl<T: Send + 'static> 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: 56785dcb2ef6d3cff82ac33f2e34db94377416a3

Signature · 1 line
2.54.0
author maintainer
#1 lore raw
Quoted 7 lines · Danilo Krummrich

Devres::new() registers a callback with the C devres subsystem via devres_node_add(). If the Devres is leaked (e.g. via core::mem::forget(), which is safe), its Drop impl never runs, and the devres release callback will revoke the inner Revocable on device unbind, which drops T in place. If T contains non-'static references, those may be dangling by that point.

Quoted 5 lines

Add a 'static bound to prevent storing types with borrowed data in Devres.

Fixes 76c01ded724b ("rust: add devres abstraction")
Reviewed-by on v1 1/1
#2 lore raw
In reply to Danilo Krummrich
Quoted 7 lines · Danilo Krummrich

Devres::new() registers a callback with the C devres subsystem via devres_node_add(). If the Devres is leaked (e.g. via core::mem::forget(), which is safe), its Drop impl never runs, and the devres release callback will revoke the inner Revocable on device unbind, which drops T in place. If T contains non-'static references, those may be dangling by that point.

Quoted 6 lines

Add a 'static bound to prevent storing types with borrowed data in Devres.

Fixes 76c01ded724b ("rust: add devres abstraction")

---

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Danilo Krummrich
Quoted 7 lines · Danilo Krummrich

Devres::new() registers a callback with the C devres subsystem via devres_node_add(). If the Devres is leaked (e.g. via core::mem::forget(), which is safe), its Drop impl never runs, and the devres release callback will revoke the inner Revocable on device unbind, which drops T in place. If T contains non-'static references, those may be dangling by that point.

Quoted 5 lines

Add a 'static bound to prevent storing types with borrowed data in Devres.

Fixes 76c01ded724b ("rust: add devres abstraction")
reviewer
#4 lore raw
In reply to Danilo Krummrich
Quoted 9 lines · Danilo Krummrich

Devres::new() registers a callback with the C devres subsystem via devres_node_add(). If the Devres is leaked (e.g. via core::mem::forget(), which is safe), its Drop impl never runs, and the devres release callback will revoke the inner Revocable on device unbind, which drops T in place. If T contains non-'static references, those may be dangling by that point.

Add a 'static bound to prevent storing types with borrowed data in Devres.

The bound should be added on Devres::new instead.

Best,
Gary

Quoted 40 lines
Fixes 76c01ded724b ("rust: add devres abstraction")
---
 rust/kernel/devres.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
rust/kernel/devres.rs +3 -3
index 9e5f93aed20c..2e258d31a45c 100644
@@ -122,7 +122,7 @@ struct Inner<T> {
 /// # Ok(())
 /// # }
 /// ```
-pub struct Devres<T: Send> {
+pub struct Devres<T: Send + 'static> {
     dev: ARef<Device>,
     inner: Arc<Inner<T>>,
 }
@@ -184,7 +184,7 @@ pub(super) unsafe fn devres_node_remove(
     }
 }
 
-impl<T: Send> Devres<T> {
+impl<T: Send + 'static> Devres<T> {
     /// Creates a new [`Devres`] instance of the given `data`.
     ///
     /// The `data` encapsulated within the returned `Devres` instance' `data` will be
@@ -349,7 +349,7 @@ unsafe impl<T: Send> Send for Devres<T> {}
 // SAFETY: `Devres` can be shared with any task, if `T: Sync`.
 unsafe impl<T: Send + Sync> Sync for Devres<T> {}
 
-impl<T: Send> Drop for Devres<T> {
+impl<T: Send + 'static> 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: 56785dcb2ef6d3cff82ac33f2e34db94377416a3

author maintainer
#5 lore raw
Quoted 5 lines · Gary Guo
Quoted 2 lines · Danilo Krummrich

Add a 'static bound to prevent storing types with borrowed data in Devres.

The bound should be added on Devres::new instead.

I did consider this as it is generally recommended to minimize bounds on structs.

However, a Devres<T> with non-'static T is semantically nonsensical, not just unconstructible, and I think type level bound represents that better.

reviewer
#6 lore raw
Quoted 5 lines · Danilo Krummrich
Quoted 4 lines · Gary Guo
Quoted 2 lines · Danilo Krummrich

Add a 'static bound to prevent storing types with borrowed data in Devres.

Quoted 7 lines
Quoted 1 line

The bound should be added on Devres::new instead.

I did consider this as it is generally recommended to minimize bounds on structs.

However, a Devres<T> with non-'static T is semantically nonsensical, not just unconstructible, and I think type level bound represents that better.

Technically Devres can contain references to the registration, which is known to outlive the bound device.

Best,
Gary

author maintainer
#7 lore raw
Quoted 8 lines · Gary Guo
Quoted 7 lines · Danilo Krummrich
Quoted 5 lines · Gary Guo
Quoted 2 lines · Danilo Krummrich

Add a 'static bound to prevent storing types with borrowed data in Devres.

The bound should be added on Devres::new instead.

Quoted 8 lines
Quoted 5 lines

I did consider this as it is generally recommended to minimize bounds on structs.

However, a Devres<T> with non-'static T is semantically nonsensical, not just unconstructible, and I think type level bound represents that better.

Technically Devres can contain references to the registration, which is known to outlive the bound device.

That's technically true, but how would you express "outlives the device" as a lifetime bound on Devres?

Additionally, if the borrowed data outlives the device bound, you don't need device-managed revocation for it in the first place.

reviewer
#8 lore raw
Quoted 12 lines · Danilo Krummrich
Quoted 11 lines · Gary Guo
Quoted 10 lines · Danilo Krummrich
Quoted 5 lines · Gary Guo
Quoted 2 lines · Danilo Krummrich

Add a 'static bound to prevent storing types with borrowed data in Devres.

The bound should be added on Devres::new instead.

I did consider this as it is generally recommended to minimize bounds on structs.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

However, a Devres<T> with non-'static T is semantically nonsensical, not just unconstructible, and I think type level bound represents that better.

Technically Devres can contain references to the registration, which is known to outlive the bound device.

That's technically true, but how would you express "outlives the device" as a lifetime bound on Devres?

Just having the lifetime would mean it outlives the device (lifetimes that cannot be guaranteed to outlive the device cannot be used in devres).

In a world with registration data, I would imagine all lifetime parameters on registration data are also valid for Devres.

Quoted 3 lines

Additionally, if the borrowed data outlives the device bound, you don't need device-managed revocation for it in the first place.

Resources that depend on both registration data and device resource perhaps?

Best,
Gary

author maintainer
#9 lore raw
Quoted 27 lines · Gary Guo
Quoted 12 lines · Danilo Krummrich
Quoted 11 lines · Gary Guo
Quoted 10 lines · Danilo Krummrich
Quoted 5 lines · Gary Guo
Quoted 2 lines · Danilo Krummrich

Add a 'static bound to prevent storing types with borrowed data in Devres.

The bound should be added on Devres::new instead.

I did consider this as it is generally recommended to minimize bounds on structs.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

However, a Devres<T> with non-'static T is semantically nonsensical, not just unconstructible, and I think type level bound represents that better.

Technically Devres can contain references to the registration, which is known to outlive the bound device.

That's technically true, but how would you express "outlives the device" as a lifetime bound on Devres?

Just having the lifetime would mean it outlives the device (lifetimes that cannot be guaranteed to outlive the device cannot be used in devres).

In a world with registration data, I would imagine all lifetime parameters on registration data are also valid for Devres.

[...]

Quoted 1 line

Resources that depend on both registration data and device resource perhaps?

You'd still need a separate constructor for this, and I'm still not convinced that this makes a lot of sense.

That said if you feel strongly about it, I don't mind moving the 'static bound to new() too much.

author maintainer
#10 lore raw
Quoted 2 lines · Danilo Krummrich

That said if you feel strongly about it, I don't mind moving the 'static bound to new() too much.

Keeping the bound on the struct for now, given that we agree to move it if it creeps to more places off-list.

author maintainer Applied
#11 lore raw
In reply to Danilo Krummrich
Quoted 1 line · Danilo Krummrich

[PATCH] rust: devres: add 'static bound to Devres<T>

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] rust: devres: add 'static bound to Devres<T>

      commit: 016267b521b1

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.