[PATCH v2] rust: configfs: skip unregister after failed registration

Younes Akhouayri via B4 Relay <[email protected]>
Newsgroups org.kernel.vger.rust-for-linux,org.kernel.feeds.b4-sent,org.kernel.vger.linux-kernel
Message-ID <20260818-fix-rust-configfs-registration-state-v1-v2-1-9acedd3070f7@younes.io>
From: Younes Akhouayri <[email protected]>

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 <[email protected]>
---
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@younes.io
---
 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 <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.