Skip to contentSkip to conversation

configfs: rust: add an API for adding default groups from C

PATCH New Andreas Hindborg 1 participant 1 message Nov 13

Subsystems (4)
lore
Share & tools
lore

Andreas Hindborg

author maintainer
#0 lore raw

PATCH v4 1/1 configfs: rust: add an API for adding default groups from C

Some C subsystems provide a feature to add configfs default groups to the configfs hierarchy of other drivers or subsystems. Rust abstractions for these subsystems will want a way to add these default groups via the configfs Rust API. So add infrastructure to make this possible.

---
Changes in v4:
- Fix the build for CONFIG_CONFIGFS_FS=m. The Rust module is built for
both y and m, but the #ifdef guarding the helpers is only true for
y, so the module was built without the helper it needs (Sashiko).
- Drop the helper for configfs_remove_default_groups. It is an exported function rather than a static inline, so Rust can call it directly, and a helper for it built into vmlinux would reference a symbol that lives in configfs.ko (Sashiko).
- Require Send and Sync for CDefaultGroup implementers. Group
carries manual Send and Sync implementations that bypass the compiler check on the new default_groups field, so an implementer that is
neither could be shared across threads through a Group (Sashiko).
- Mark the UnattachedGroup methods #[inline], as ListArc does for its equivalents (Sashiko).
- Use the kernel vertical import layout for the crate::alloc imports (Sashiko).
- Link to v3: https://msgid.link/[email protected]

Changes in v3:
- Acquire a reference on each default group before adding it to its parent group. configfs_add_default_group does not acquire one, but configfs_remove_default_groups releases one, so a default group reached
a zero reference count when its parent was dropped, while the caller
still owned it.
- Make CDefaultGroup an unsafe trait and document the requirements on implementers.
- Add UnattachedGroup, a token that grants permission to attach a group
as a default group exactly once, and take that in Group::new instead of
an Arc. A struct config_group is linked into its parent through a
single group_entry field, so attaching one group to two parents would corrupt both lists. The token cannot be cloned and Group::new consumes
it, so this is now rejected at compile time rather than left to the
caller.
- Allocate the group in UnattachedGroup::new, so that uniqueness follows
from construction. Obtain a handle to the group with clone_arc or into_arc, named after the ListArc accessors.
- Initialize data before group in Group::new, so that no fallible
step runs after the default groups have been attached.
- Drop default groups from the list of unsupported features in the module documentation.
- Link to v2: https://msgid.link/[email protected]

Changes in v2:
- Drop the spurious __ prefix on the symbol name of the configfs_remove_default_groups Rust helper (Alice).
- Wrap the body of Group::new in pin_init::pin_init_scope so that
allocation failure from KVec::push is propagated with ? instead
of panicking via unwrap (Danilo).
- Only build configfs rust helpers when configfs is enabled.
- Link to v1: https://msgid.link/[email protected]

---
 drivers/block/rnull/configfs.rs |   1 +
 rust/helpers/configfs.c         |  10 ++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/configfs.rs         | 198 ++++++++++++++++++++++++++++++++++++----
 samples/rust/rust_configfs.rs   |   8 +-
 5 files changed, 201 insertions(+), 17 deletions(-)
drivers/block/rnull/configfs.rs +1 -0
index 34ef96fa125f..2f5b722927e0 100644
@@ -88,6 +88,7 @@ fn make_group(
                     name: name.try_into()?,
                 }),
             }),
+            core::iter::empty(),
         ))
     }
 }
rust/helpers/configfs.c new file +10 -0
new file mode 100644
index 000000000000..cda1551ab048
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/configfs.h>
+
+__rust_helper void
+rust_helper_configfs_add_default_group(struct config_group *new_group,
+				       struct config_group *group)
+{
+	configfs_add_default_group(new_group, group);
+}
rust/helpers/helpers.c +1 -0
index d62c7be1ca7b..ab8d38b43117 100644
@@ -51,6 +51,7 @@
 #include "build_bug.c"
 #include "clk.c"
 #include "completion.c"
+#include "configfs.c"
 #include "cpu.c"
 #include "cpufreq.c"
 #include "cpumask.c"
rust/kernel/configfs.rs +182 -16

241 lines not on this page · raw message

samples/rust/rust_configfs.rs +7 -1
index a1bd9db6010d..ada378bc331b 100644
@@ -83,7 +83,12 @@ fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<Child>,
             ],
         };
 
-        Ok(configfs::Group::new(name.try_into()?, tpe, Child::new()))
+        Ok(configfs::Group::new(
+            name.try_into()?,
+            tpe,
+            Child::new(),
+            core::iter::empty(),
+        ))
     }
 }
 
@@ -152,6 +157,7 @@ fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<GrandCh
             name.try_into()?,
             tpe,
             GrandChild::new(),
+            core::iter::empty(),
         ))
     }
 }

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260215-configfs-c-default-groups-bdb0a44633a6

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>