rust: devres: fix leaking call to devm_add_action()
Subsystems (3)
Danilo Krummrich
maintainerPATCH 1/1 rust: devres: fix leaking call to devm_add_action()
When the data argument of Devres::new() is Err(), we leak the preceding call to devm_add_action().
In order to fix this, call devm_add_action() in a unit type initializer in try_pin_init!() after the initializers of all other fields.
--- rust/kernel/devres.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
rust/kernel/devres.rs +10 -7
@@ -119,6 +119,7 @@ pub struct Devres<T: Send> {// impls can be removed. #[pin] inner: Opaque<Inner<T>>, + _add_action: (), }impl <T :Send >Devres <T > {
@@ -140,7 +141,15 @@ pub fn new<'a, E>( dev: dev.into(), callback,// INVARIANT: `inner` is properly initialized. - inner <- { + inner <- Opaque::pin_init(try_pin_init!(Inner { + devm <- Completion::new(), + revoke <- Completion::new(), + data <- Revocable::new(data), + })), + // TODO: Replace with "initializer code blocks" [1] once available. + // + // [1] https://github.com/Rust-for-Linux/pin-init/pull/69 + _add_action: {// SAFETY: `this` is a valid pointer to uninitialized memory. let inner =unsafe { &raw mut (*this.as_ptr ()).inner };
@@ -153,12 +162,6 @@ pub fn new<'a, E>(to_result (unsafe { bindings::devm_add_action (dev.as_raw (),Some (callback), inner.cast ()) })?; - -Opaque ::pin_init (try_pin_init! (Inner { - devm <-Completion ::new (), - revoke <-Completion ::new (), - data <-Revocable ::new (data), - })) }, }) }
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
Signature · 1 line
2.50.1
Benno Lossin
reviewerQuoted 43 lines · Danilo Krummrich
When the data argument of
Devres::new()isErr(), we leak the preceding call todevm_add_action().In order to fix this, call
devm_add_action()in a unit type initializer in try_pin_init!() after the initializers of all other fields.Fixes f5d3ef25d238 ("rust: devres: get rid of Devres' inner Arc")Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/devres.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)rust/kernel/devres.rs +10
-1@@ -119,6 +119,7 @@ pub struct Devres<T: Send> {// impls can be removed. #[pin] inner: Opaque<Inner<T>>, + _add_action: (), }impl <T :Send >Devres <T > {@@ -140,7 +141,15 @@ pub fn new<'a, E>( dev: dev.into(), callback,// INVARIANT: `inner` is properly initialized. - inner <- { + inner <- Opaque::pin_init(try_pin_init!(Inner { + devm <- Completion::new(), + revoke <- Completion::new(), + data <- Revocable::new(data), + })), + // TODO: Replace with "initializer code blocks" [1] once available. + // + // [1] https://github.com/Rust-for-Linux/pin-init/pull/69 + _add_action: {// SAFETY: `this` is a valid pointer to uninitialized memory. let inner =unsafe { &raw mut (*this.as_ptr ()).inner };
rust/kernel/devres.rs pub fn new<'a, E>( · line 164
Quoted 3 lines
rust/kernel/devres.rs +0
-0@@ -153,12 +162,6 @@ pub fn new<'a, E>(to_result (unsafe { bindings::devm_add_action (dev.as_raw (),Some (callback), inner.cast ()) })?;
I have some bad news, I think this is also wrong: if the devm_add_action fails, we never drop the contents of inner, since the destructor of Opaque does nothing and we never finished construction of Devres, so its Drop will never be called.
One solution would be to use pin_chain on the initializer for Inner (not opaque). Another one would be to not use opaque, UnsafePinned actually looks like the better fit for this use-case.
This also made me re-think Opaque::pin_init. It seems wrong and probably shouldn't exist, as Opaque violates the drop guarantee required by pinned data. So it cannot structurally pin the data inside.
---
Cheers,
Benno
Quoted 11 lines
rust/kernel/devres.rs +0
-6- -Opaque ::pin_init (try_pin_init! (Inner { - devm <-Completion ::new (), - revoke <-Completion ::new (), - data <-Revocable ::new (data), - })) }, }) }base-commit:
8f5ae30d69d7543eee0d70083daf4de8fe15d585
Danilo Krummrich
maintainerQuoted 43 lines · Benno Lossin
Quoted 42 lines · Danilo Krummrich
When the data argument of
Devres::new()isErr(), we leak the preceding call todevm_add_action().In order to fix this, call
devm_add_action()in a unit type initializer in try_pin_init!() after the initializers of all other fields.Fixes f5d3ef25d238 ("rust: devres: get rid of Devres' inner Arc")Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/devres.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)rust/kernel/devres.rs +10
-1@@ -119,6 +119,7 @@ pub struct Devres<T: Send> {// impls can be removed. #[pin] inner: Opaque<Inner<T>>, + _add_action: (), }impl <T :Send >Devres <T > {@@ -140,7 +141,15 @@ pub fn new<'a, E>( dev: dev.into(), callback,// INVARIANT: `inner` is properly initialized. - inner <- { + inner <- Opaque::pin_init(try_pin_init!(Inner { + devm <- Completion::new(), + revoke <- Completion::new(), + data <- Revocable::new(data), + })), + // TODO: Replace with "initializer code blocks" [1] once available. + // + // [1] https://github.com/Rust-for-Linux/pin-init/pull/69 + _add_action: {// SAFETY: `this` is a valid pointer to uninitialized memory. let inner =unsafe { &raw mut (*this.as_ptr ()).inner };
Quoted 9 lines
rust/kernel/devres.rs pub fn new<'a, E>( · line 164
Quoted 4 lines
rust/kernel/devres.rs +0
-0@@ -153,12 +162,6 @@ pub fn new<'a, E>(to_result (unsafe { bindings::devm_add_action (dev.as_raw (),Some (callback), inner.cast ()) })?;I have some bad news, I think this is also wrong: if the
devm_add_actionfails, we never drop the contents ofinner, since the destructor ofOpaquedoes nothing and we never finished construction ofDevres, so itsDropwill never be called.
Good catch! For some reason I already had UnsafePinned in mind, but it's still a TODO to replace the Opaque with UnsafePinned.
Quoted 3 lines
One solution would be to use
pin_chainon the initializer forInner(not opaque). Another one would be to not use opaque,UnsafePinnedactually looks like the better fit for this use-case.
Yeah, the problem should go away with UnsafePinned. Maybe, until we have it, we can just do the following:
rust/kernel/devres.rs +3 -0
@@ -161,6 +161,9 @@ pub fn new<'a, E>(// live at least as long as the returned `impl PinInit<Self, Error>`. to_result (unsafe { bindings::devm_add_action (dev.as_raw (),Some (callback), inner.cast ()) + }).inspect_err (|_| { +// SAFETY: `inner` is valid for dropping. +unsafe { core::ptr::drop_in_place (inner) }; })?; }, })
Benno Lossin
reviewerQuoted 14 lines · Danilo Krummrich
Quoted 4 lines · Benno Lossin
On Mon Aug 11, 2025 at 11:44 PM CEST, Danilo Krummrich wrote:
One solution would be to usepin_chainon the initializer forInner(not opaque). Another one would be to not use opaque,UnsafePinnedactually looks like the better fit for this use-case.Yeah, the problem should go away with UnsafePinned. Maybe, until we have it, we can just do the following:
rust/kernel/devres.rs +0
-0
rust/kernel/devres.rs pub fn new<'a, E>( · line 166
Quoted 6 lines
rust/kernel/devres.rs +3
-0@@ -161,6 +161,9 @@ pub fn new<'a, E>(// live at least as long as the returned `impl PinInit<Self, Error>`. to_result (unsafe { bindings::devm_add_action (dev.as_raw (),Some (callback), inner.cast ()) + }).inspect_err (|_| { +// SAFETY: `inner` is valid for dropping. +unsafe { core::ptr::drop_in_place (inner) };
Yeah that works too. Though I'd add a comment & improve the safety comment.
---
Cheers,
Benno