[PATCH v2] rust: configfs: skip unregister after failed registration
From: Younes Akhouayri via B4 Relay
Date: Tue Aug 18 2026 - 13:50:28 EST
From: Younes Akhouayri <git@xxxxxxxxx>
Subsystem::new() calls configfs_register_subsystem() in pin_chain(). If
registration returns an error, pin_chain() drops the Subsystem. PinnedDrop
then calls configfs_unregister_subsystem() even though registration failed.
Registering a duplicate name returns -EEXIST before ci_dentry is set.
configfs_unregister_subsystem() dereferences ci_dentry, causing a NULL
pointer dereference and kernel panic.
Move configfs_register_subsystem() to the end of try_pin_init!. If
registration fails, destroy su_mutex and return the error. PinnedDrop is
not called because initialization did not finish. Successful subsystems
are still unregistered by PinnedDrop.
Fixes: 446cafc295bf ("rust: configfs: introduce rust support for configfs")
Signed-off-by: Younes Akhouayri <git@xxxxxxxxx>
---
Changes in v2:
- Register the subsystem at the end of try_pin_init!.
- Destroy su_mutex when registration fails.
- Remove the registered flag.
- Link to v1: https://patch.msgid.link/20260818-fix-rust-configfs-registration-state-v1-v1-1-c929990bc8ef@xxxxxxxxx
---
rust/kernel/configfs.rs | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs
index cd082b83e9e7..770e0a022b42 100644
--- a/rust/kernel/configfs.rs
+++ b/rust/kernel/configfs.rs
@@ -173,12 +173,19 @@ pub fn new(
}
),
data <- data,
- })
- .pin_chain(|this| {
- crate::error::to_result(
- // SAFETY: We initialized `this.subsystem` according to C API contract above.
- unsafe { bindings::configfs_register_subsystem(this.subsystem.get()) },
- )
+ _: {
+ let result = crate::error::to_result(
+ // SAFETY: We initialized `subsystem` according to the C API contract above.
+ unsafe { bindings::configfs_register_subsystem(subsystem.get()) },
+ );
+ if result.is_err() {
+ // SAFETY: The mutex was initialized above and registration failed.
+ unsafe {
+ bindings::mutex_destroy(&raw mut (*subsystem.get()).su_mutex)
+ };
+ }
+ result?
+ }
})
}
}
---
base-commit: 47f27155f17498fccb1f222f79089642337498a9
change-id: 20260817-fix-rust-configfs-registration-state-v1-fa33fcc69673
Best regards,
--
Younes Akhouayri <git@xxxxxxxxx>