Skip to contentSkip to conversation

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

PATCH Superseded Andreas Hindborg 3 participants 4 messages Feb 15 – May 13

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Andreas Hindborg

author maintainer
#0 lore raw

PATCH 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.

---
 drivers/block/rnull/configfs.rs |  1 +
 rust/helpers/configfs.c         | 16 ++++++++++++++++
 rust/helpers/helpers.c          |  1 +
 rust/kernel/configfs.rs         | 33 ++++++++++++++++++++++++++++++++-
 samples/rust/rust_configfs.rs   |  8 +++++++-
 5 files changed, 57 insertions(+), 2 deletions(-)
drivers/block/rnull/configfs.rs +1 -0
index 6713a6d92391d..ea38b27a9011c 100644
@@ -78,6 +78,7 @@ fn make_group(
                     name: name.try_into()?,
                 }),
             }),
+            core::iter::empty(),
         ))
     }
 }
rust/helpers/configfs.c new file +16 -0
new file mode 100644
index 0000000000000..7cec8ffcb093d
@@ -0,0 +1,16 @@
+// 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_helper void
+__rust_helper_configfs_remove_default_groups(struct config_group *group)
+{
+	configfs_remove_default_groups(group);
+}
rust/helpers/helpers.c +1 -0
index 79c72762ad9c4..613b41e8b781c 100644
@@ -19,6 +19,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 +32 -1
index 466fb7f407628..705f1647269d9 100644
@@ -242,12 +242,22 @@ unsafe fn container_of(group: *const bindings::config_group) -> *const Self {
 ///
 /// To add a subgroup to configfs, pass this type as `ctype` to
 /// [`crate::configfs_attrs`] when creating a group in [`GroupOperations::make_group`].
-#[pin_data]
+#[pin_data(PinnedDrop)]
 pub struct Group<Data> {
     #[pin]
     group: Opaque<bindings::config_group>,
     #[pin]
     data: Data,
+    default_groups: KVec<Arc<dyn CDefaultGroup>>,
+}
+
+#[pinned_drop]
+impl<Data> PinnedDrop for Group<Data> {
+    fn drop(self: Pin<&mut Self>) {
+        // SAFETY: We have exclusive access to `self` and we know the default groups are alive
+        // because we reference them through `self.default_groups`.
+        unsafe { bindings::configfs_remove_default_groups(self.group.get()) };
+    }
 }
 
 impl<Data> Group<Data> {
@@ -259,7 +269,13 @@ pub fn new(
         name: CString,
         item_type: &'static ItemType<Group<Data>, Data>,
         data: impl PinInit<Data, Error>,
+        default_groups: impl IntoIterator<Item = Arc<dyn CDefaultGroup>>,
     ) -> impl PinInit<Self, Error> {
+        let mut dg = KVec::new();
+        for group in default_groups {
+            dg.push(group, GFP_KERNEL).unwrap();
+        }
+
         try_pin_init!(Self {
             group <- pin_init::init_zeroed().chain(|v: &mut Opaque<bindings::config_group>| {
                 let place = v.get();
@@ -268,13 +284,28 @@ pub fn new(
                 unsafe {
                     bindings::config_group_init_type_name(place, name.cast(), item_type.as_ptr())
                 };
+
+                for default_group in &dg {
+                    // SAFETY: We keep the default groups alive until `Self` is dropped.
+                    unsafe { bindings::configfs_add_default_group(default_group.group_ptr(), place) }
+                }
                 Ok(())
             }),
             data <- data,
+            default_groups: dg,
         })
     }
 }
 
+/// A trait for default configfs groups added by C code.
+///
+/// Rust abstractions that work with C code that creates configfs groups can implement this trait to
+/// add the groups as default groups via the Rust configfs API.
+pub trait CDefaultGroup {
+    /// Return a raw pointer to the group definition.
+    fn group_ptr(&self) -> *mut bindings::config_group;
+}
+
 // SAFETY: `Group<Data>` embeds a field of type `bindings::config_group`
 // within the `group` field.
 unsafe impl<Data> HasGroup<Data> for Group<Data> {
samples/rust/rust_configfs.rs +7 -1
index 0ccc7553ef395..c65dda8bfacd2 100644
@@ -84,7 +84,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(),
+        ))
     }
 }
 
@@ -153,6 +158,7 @@ fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<GrandCh
             name.try_into()?,
             tpe,
             GrandChild::new(),
+            core::iter::empty(),
         ))
     }
 }

---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-configfs-c-default-groups-bdb0a44633a6

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
reviewer
#1 lore raw
Quoted 35 lines · Andreas Hindborg

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.

---
 drivers/block/rnull/configfs.rs |  1 +
 rust/helpers/configfs.c         | 16 ++++++++++++++++
 rust/helpers/helpers.c          |  1 +
 rust/kernel/configfs.rs         | 33 ++++++++++++++++++++++++++++++++-
 samples/rust/rust_configfs.rs   |  8 +++++++-
 5 files changed, 57 insertions(+), 2 deletions(-)
drivers/block/rnull/configfs.rs +1 -0
index 6713a6d92391d..ea38b27a9011c 100644
@@ -78,6 +78,7 @@ fn make_group(
                     name: name.try_into()?,
                 }),
             }),
+            core::iter::empty(),
         ))
     }
 }
rust/helpers/configfs.c new file +3 -0
new file mode 100644
index 0000000000000..7cec8ffcb093d
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/configfs.h>

rust/helpers/configfs.c line 13

Quoted 10 lines
rust/helpers/configfs.c new file +10 -0
+
+__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_helper void
+__rust_helper_configfs_remove_default_groups(struct config_group *group)

Only the annotation has the two __ prefix, not the symbol name.

rust/helpers/configfs.c

Quoted 6 lines
rust/helpers/configfs.c new file +6 -0
+#[pinned_drop]
+impl<Data> PinnedDrop for Group<Data> {
+    fn drop(self: Pin<&mut Self>) {
+        // SAFETY: We have exclusive access to `self` and we know the default groups are alive
+        // because we reference them through `self.default_groups`.
+        unsafe { bindings::configfs_remove_default_groups(self.group.get()) };

Why isn't this here already?

rust/helpers/configfs.c pub fn new( · line 272

Quoted 9 lines
rust/helpers/configfs.c new file +2 -0
+    }
 }
 
 impl<Data> Group<Data> {
@@ -259,7 +269,13 @@ pub fn new(
         name: CString,
         item_type: &'static ItemType<Group<Data>, Data>,
         data: impl PinInit<Data, Error>,
+        default_groups: impl IntoIterator<Item = Arc<dyn CDefaultGroup>>,

Honestly, I'd just take a KVec here.

Quoted 10 lines
rust/helpers/configfs.c new file +5 -0
     ) -> impl PinInit<Self, Error> {
+        let mut dg = KVec::new();
+        for group in default_groups {
+            dg.push(group, GFP_KERNEL).unwrap();
+        }
+
         try_pin_init!(Self {
             group <- pin_init::init_zeroed().chain(|v: &mut Opaque<bindings::config_group>| {
                 let place = v.get();

rust/helpers/configfs.c pub fn new( · line 291

Quoted 8 lines
rust/helpers/configfs.c new file +5 -0
@@ -268,13 +284,28 @@ pub fn new(
                 unsafe {
                     bindings::config_group_init_type_name(place, name.cast(), item_type.as_ptr())
                 };
+
+                for default_group in &dg {
+                    // SAFETY: We keep the default groups alive until `Self` is dropped.
+                    unsafe { bindings::configfs_add_default_group(default_group.group_ptr(), place) }
+                }

Do these not need to be removed in drop?

Alice

#3 lore raw
In reply to Andreas Hindborg

rust/kernel/configfs.rs pub fn new( · line 276

Quoted 9 lines · Andreas Hindborg
rust/kernel/configfs.rs +4 -0
@@ -259,7 +269,13 @@ pub fn new(
         name: CString,
         item_type: &'static ItemType<Group<Data>, Data>,
         data: impl PinInit<Data, Error>,
+        default_groups: impl IntoIterator<Item = Arc<dyn CDefaultGroup>>,
     ) -> impl PinInit<Self, Error> {
+        let mut dg = KVec::new();
+        for group in default_groups {
+            dg.push(group, GFP_KERNEL).unwrap();

We should not panic the kernel on allocation failure.

If you wrap the function body into pin_init::pin_init_scope() you can just use the '?' operator instead.

Quoted 21 lines
rust/kernel/configfs.rs +8 -0
+        }
+
         try_pin_init!(Self {
             group <- pin_init::init_zeroed().chain(|v: &mut Opaque<bindings::config_group>| {
                 let place = v.get();
@@ -268,13 +284,28 @@ pub fn new(
                 unsafe {
                     bindings::config_group_init_type_name(place, name.cast(), item_type.as_ptr())
                 };
+
+                for default_group in &dg {
+                    // SAFETY: We keep the default groups alive until `Self` is dropped.
+                    unsafe { bindings::configfs_add_default_group(default_group.group_ptr(), place) }
+                }
                 Ok(())
             }),
             data <- data,
+            default_groups: dg,
         })
     }
 }
author maintainer
#2 lore raw
In reply to Alice Ryhl
Quoted 40 lines · Alice Ryhl
Quoted 39 lines · Andreas Hindborg

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.

---
 drivers/block/rnull/configfs.rs |  1 +
 rust/helpers/configfs.c         | 16 ++++++++++++++++
 rust/helpers/helpers.c          |  1 +
 rust/kernel/configfs.rs         | 33 ++++++++++++++++++++++++++++++++-
 samples/rust/rust_configfs.rs   |  8 +++++++-
 5 files changed, 57 insertions(+), 2 deletions(-)
drivers/block/rnull/configfs.rs +1 -0
index 6713a6d92391d..ea38b27a9011c 100644
@@ -78,6 +78,7 @@ fn make_group(
                     name: name.try_into()?,
                 }),
             }),
+            core::iter::empty(),
         ))
     }
 }
rust/helpers/configfs.c new file +7 -0
new file mode 100644
index 0000000000000..7cec8ffcb093d
@@ -0,0 +1,16 @@
+// 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)
Quoted 8 lines

rust/helpers/configfs.c line 13

Quoted 6 lines
rust/helpers/configfs.c new file +6 -0
+{
+	configfs_add_default_group(new_group, group);
+}
+
+__rust_helper void
+__rust_helper_configfs_remove_default_groups(struct config_group *group)

Only the annotation has the two __ prefix, not the symbol name.

Hmm, odd that the bindings generate correct anyway.

Quoted 9 lines

rust/helpers/configfs.c

Quoted 7 lines
rust/helpers/configfs.c new file +6 -0
+#[pinned_drop]
+impl<Data> PinnedDrop for Group<Data> {
+    fn drop(self: Pin<&mut Self>) {
+        // SAFETY: We have exclusive access to `self` and we know the default groups are alive
+        // because we reference them through `self.default_groups`.
+        unsafe { bindings::configfs_remove_default_groups(self.group.get()) };

Why isn't this here already?

What do you mean. The drop of default groups is not there already, because it was not previously possible to add default groups. So there would be nothing to drop.

Quoted 4 lines
Quoted 4 lines

+ }
}

Quoted 8 lines

rust/helpers/configfs.c pub fn new( · line 272

Quoted 6 lines

impl<Data> Group<Data> {

rust/helpers/configfs.c new file +1 -0
@@ -259,7 +269,13 @@ pub fn new(
         name: CString,
         item_type: &'static ItemType<Group<Data>, Data>,
         data: impl PinInit<Data, Error>,
+        default_groups: impl IntoIterator<Item = Arc<dyn CDefaultGroup>>,

Honestly, I'd just take a KVec here.

I was trying to be fancy. Is it not more flexible to take an iterator? What are the pros/cons?

Quoted 13 lines
Quoted 13 lines
rust/helpers/configfs.c new file +5 -0
     ) -> impl PinInit<Self, Error> {
+        let mut dg = KVec::new();
+        for group in default_groups {
+            dg.push(group, GFP_KERNEL).unwrap();
+        }
+
         try_pin_init!(Self {
             group <- pin_init::init_zeroed().chain(|v: &mut Opaque<bindings::config_group>| {
                 let place = v.get();
@@ -268,13 +284,28 @@ pub fn new(
                 unsafe {
                     bindings::config_group_init_type_name(place, name.cast(), item_type.as_ptr())
Quoted 8 lines

rust/helpers/configfs.c pub fn new( · line 291

Quoted 6 lines
rust/helpers/configfs.c new file +5 -0
                 };
+
+                for default_group in &dg {
+                    // SAFETY: We keep the default groups alive until `Self` is dropped.
+                    unsafe { bindings::configfs_add_default_group(default_group.group_ptr(), place) }
+                }

Do these not need to be removed in drop?

Yes, I added the drop logic above. It is the hunk you asked why is it not already here?

Best regards,
Andreas Hindborg