Skip to contentSkip to conversation

rust: configfs: add procedural macro for declaring configfs attributes

PATCH New Malte Wechter 2 participants 2 messages Jun 30

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 rust: configfs: add procedural macro for declaring configfs attributes

Implement configfs_attrs! as a procedural macro using syn, this improves readability and maintainability. Remove the old macro and replace all uses with the new macro. Add the new macro implementation file to MAINTAINERS.

---
Changes in v5:
- Assert during parsing that attribute names are unique (sashiko).
- Update safety comment to include that calls to make_group are serialized on the C side (sashiko)
- Add support for raw identifiers as attribute names (sashiko).
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Update link path to configfs_attr macro in configfs.rs
- Fix doc strings for configfs_attr in macros/lib.rs
- Fix doc strings for parse_ordered_fields in macros/helpers.rs
- Update title prefix to rust: configfs:
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Remove 'make_static_ident' function, make names for static variables simpler
- Move 'parse_ordered_fields' macro from module.rs into helpers
- Use 'parse_ordered_fields' macro for parsing instead of doing it ad-hoc
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add a try_parse helper function to macros/helpers.rs
- Fix bug where 'child' configuration gets dropped if trailing comma is missing (sashiko)
- Link to v1: https://lore.kernel.org/r/[email protected]

To: Andreas Hindborg <[email protected]>
To: Boqun Feng <[email protected]>
To: Jens Axboe <[email protected]>
To: Breno Leitao <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
To: Luis Chamberlain <[email protected]>
To: Petr Pavlu <[email protected]>
To: Daniel Gomez <[email protected]>
To: Sami Tolvanen <[email protected]>
To: Aaron Tomlin <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 MAINTAINERS                     |   1 +
 drivers/block/rnull/configfs.rs |   2 +-
 rust/kernel/configfs.rs         | 263 +---------------------------------------
 rust/macros/configfs_attrs.rs   | 149 +++++++++++++++++++++++
 rust/macros/helpers.rs          | 139 +++++++++++++++++++++
 rust/macros/lib.rs              |  87 +++++++++++++
 rust/macros/module.rs           | 137 ---------------------
 samples/rust/rust_configfs.rs   |   2 +-
 8 files changed, 384 insertions(+), 396 deletions(-)
MAINTAINERS +1 -0
index 2fb1c75afd16..45f7a1ec93b4 100644
@@ -6464,6 +6464,7 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/a.hindborg/linux.git config
 F:	fs/configfs/
 F:	include/linux/configfs.h
 F:	rust/kernel/configfs.rs
+F:	rust/macros/configfs_attrs.rs
 F:	samples/configfs/
 F:	samples/rust/rust_configfs.rs
 
drivers/block/rnull/configfs.rs +1 -1
index 7c2eb5c0b722..f28ec69d7984 100644
@@ -4,8 +4,8 @@
 use kernel::{
     block::mq::gen_disk::{GenDisk, GenDiskBuilder},
     configfs::{self, AttributeOperations},
-    configfs_attrs,
     fmt::{self, Write as _},
+    macros::configfs_attrs,
     new_mutex,
     page::PAGE_SIZE,
     prelude::*,
rust/kernel/configfs.rs +6 -257

301 lines not on this page · raw message

rust/macros/configfs_attrs.rs new file +149 -0

149 lines not on this page · raw message

rust/macros/helpers.rs +139 -0

142 lines not on this page · raw message

rust/macros/lib.rs +87 -0

96 lines not on this page · raw message

rust/macros/module.rs +0 -137

143 lines not on this page · raw message

samples/rust/rust_configfs.rs +1 -1
index a1bd9db6010d..876462f7789d 100644
@@ -4,7 +4,7 @@
 
 use kernel::alloc::flags;
 use kernel::configfs;
-use kernel::configfs::configfs_attrs;
+use kernel::macros::configfs_attrs;
 use kernel::new_mutex;
 use kernel::page::PAGE_SIZE;
 use kernel::prelude::*;

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260417-configfs-syn-191e07130027

Best regards,

Signature · 1 line
Malte Wechter <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [Medium] Raw identifiers generate incorrect configfs attribute names with an 'r#' prefix.
- [Low] The new macro strictly requires a trailing comma after the last field, reverting a usability fix and breaking compatibility with existing macro invocations.

Pre-existing issues:
- [High] Concurrent configfs directory creations of the same type will cause a data race on the static DATA_ATTR_LIST array. --

commit 884f1e9ac3af92759b06d060216464edd49fb53f
Author: Malte Wechter <[email protected]>

rust: configfs: add procedural macro for declaring configfs attributes

Implement configfs_attrs! as a procedural macro using syn, this improves readability and maintainability. Remove the old macro and replace all uses with the new macro. Add the new macro implementation file to MAINTAINERS.

Quoted 5 lines
rust/macros/configfs_attrs.rs new file +0 -0
new file mode 100644
index 0000000000000..f829a42cece88

[ ... ]

rust/macros/configfs_attrs.rs line 112

Quoted 5 lines
rust/macros/configfs_attrs.rs new file +5 -0
+        attrs.push(quote! {
+        static #name_with_attr: kernel::configfs::Attribute<#id, #data_ty, #data_ty> =
+            // SAFETY: We are expanding `configfs_attrs`.
+            unsafe {
+              kernel::configfs::Attribute::new(kernel::c_str!(::core::stringify!(#name)))
             ^^^^

[Severity: Medium]
Will this generate incorrect configfs attribute names with an "r#" prefix if a raw identifier is used? The macro uses unraw() for the Rust static names earlier, but seems to pass the raw string into stringify!() here.

Quoted 4 lines
rust/macros/configfs_attrs.rs new file +4 -0
+            };
+
+          // SAFETY: By design of this macro, the name of the variable we
+          // invoke the `add` method on below, is not visible outside of

[ ... ]

rust/macros/configfs_attrs.rs line 121

Quoted 2 lines
rust/macros/configfs_attrs.rs new file +2 -0
+          // by per subsystem mutex.
+          unsafe { #data_attr_ident.add::<#attr_idx, #id, _>(&#name_with_attr) }
             ^^^^

[Severity: High]
This is a pre-existing issue, but does this code cause a data race? If multiple threads call make_group concurrently (for example, creating directories in different subsystems that share the same configuration struct), they would execute this add() concurrently and mutate the static DATA_ATTR_LIST array without synchronization, leading to undefined behavior.

Quoted 7 lines
rust/macros/configfs_attrs.rs new file +2 -0
+        });
+    }
rust/macros/helpers.rs +0 -0
index d18fbf4daa0a5..305dcbddf7974 100644

[ ... ]

rust/macros/helpers.rs pub(crate) fn file() -> String { · line 127

Quoted 13 lines
rust/macros/helpers.rs +13 -0
+            match &*key.to_string() {
+                $(
+                    stringify!($key) => $name = Some($parser),
+                )*
+                _ => {
+                    Err(Error::new_spanned(
+                        &key,
+                        format!(r#"unknown key "{key}". Valid keys are: {EXPECTED_KEYS:?}."#),
+                    ))?
+                }
+            }
+
+            $input.parse::<Token![,]>()?;
             ^^^^

[Severity: Low]
Does this strictly require a trailing comma after the last field? The previous declarative macro allowed an optional trailing comma, so this strict requirement might break compatibility with existing macro invocations.

Quoted 2 lines
rust/macros/helpers.rs +2 -0
+            seen_keys.push(key);
+        }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1