Skip to contentSkip to conversation

rust: devres: initialize Devres::inner::data last

PATCH In review Danilo Krummrich 2 participants 5 messages Jul 14, 2025 – Jul 15, 2025

Subsystems (2)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 rust: devres: initialize Devres::inner::data last

Users may want to access the Devres object from callbacks registered through the initialization of Devres::inner::data.

For those accesses to be valid, Devres::inner::data must be initialized last [1].

Credit to Boqun for spotting this [2].

---
 rust/kernel/devres.rs | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
rust/kernel/devres.rs +8 -8
index a8a2d24cac24..152a89b78943 100644
@@ -137,14 +137,10 @@ pub fn new<'a, E>(
         let callback = Self::devres_callback;
 
         try_pin_init!(&this in Self {
-            // INVARIANT: `inner` is properly initialized.
-            inner <- Opaque::pin_init(try_pin_init!(Inner {
-                data <- Revocable::new(data),
-                devm <- Completion::new(),
-                revoke <- Completion::new(),
-            })),
+            dev: dev.into(),
             callback,
-            dev: {
+            // INVARIANT: `inner` is properly initialized.
+            inner <- {
                 // SAFETY: `this` is a valid pointer to uninitialized memory.
                 let inner = unsafe { &raw mut (*this.as_ptr()).inner };
 
@@ -158,7 +154,11 @@ pub fn new<'a, E>(
                     bindings::devm_add_action(dev.as_raw(), Some(callback), inner.cast())
                 })?;
 
-                dev.into()
+                Opaque::pin_init(try_pin_init!(Inner {
+                    devm <- Completion::new(),
+                    revoke <- Completion::new(),
+                    data <- Revocable::new(data),
+                }))
             },
         })
     }

base-commit: 3964d07dd821efe9680e90c51c86661a98e60a0f
prerequisite-patch-id: 12a88f7c46bec668e4f71ec0d94b1782d9446d0b

Signature · 1 line
2.50.0
reviewer
#1 lore raw
Quoted 6 lines · Danilo Krummrich

Users may want to access the Devres object from callbacks registered through the initialization of Devres::inner::data.

For those accesses to be valid, Devres::inner::data must be initialized last [1].

Quoted 7 lines

Credit to Boqun for spotting this [2].

---
base-commit: 3964d07dd821efe9680e90c51c86661a98e60a0f

I couldn't find this commit. Where does this apply?

Alice

author maintainer
#2 lore raw
Quoted 9 lines · Alice Ryhl
Quoted 8 lines · Danilo Krummrich

Users may want to access the Devres object from callbacks registered through the initialization of Devres::inner::data.

For those accesses to be valid, Devres::inner::data must be initialized last [1].

Credit to Boqun for spotting this [2].

Quoted 7 lines
Quoted 5 lines

---
base-commit: 3964d07dd821efe9680e90c51c86661a98e60a0f

I couldn't find this commit. Where does this apply?

The commit is in driver-core-next.

reviewer Reviewed-by
#3 lore raw
Quoted 17 lines · Danilo Krummrich
Quoted 15 lines · Alice Ryhl
Quoted 13 lines · Danilo Krummrich

Users may want to access the Devres object from callbacks registered through the initialization of Devres::inner::data.

For those accesses to be valid, Devres::inner::data must be initialized last [1].

Credit to Boqun for spotting this [2].

---
base-commit: 3964d07dd821efe9680e90c51c86661a98e60a0f

Quoted 3 lines
Quoted 1 line

I couldn't find this commit. Where does this apply?

The commit is in driver-core-next.

author maintainer
#4 lore raw
In reply to Danilo Krummrich
Quoted 11 lines · Danilo Krummrich

Users may want to access the Devres object from callbacks registered through the initialization of Devres::inner::data.

For those accesses to be valid, Devres::inner::data must be initialized last [1].

Credit to Boqun for spotting this [2].

Applied to driver-core-testing, thanks!