rust: configfs: add procedural macro for declaring configfs attributes
Subsystems (5)
PATCH v4 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 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]
--- MAINTAINERS | 1 + drivers/block/rnull/configfs.rs | 2 +- rust/kernel/configfs.rs | 263 +--------------------------------------- rust/macros/configfs_attrs.rs | 135 +++++++++++++++++++++ rust/macros/helpers.rs | 139 +++++++++++++++++++++ rust/macros/lib.rs | 87 +++++++++++++ rust/macros/module.rs | 137 --------------------- samples/rust/rust_configfs.rs | 2 +- 8 files changed, 370 insertions(+), 396 deletions(-)
MAINTAINERS +1 -0
@@ -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
@@ -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 +135 -0
135 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
@@ -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]>
[email protected]
AI reviewThank 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] The new macro no longer prevents duplicate attribute names at compile time.
- [Low] The macro will panic at compile time if a raw identifier (e.g., r#type) is used as an attribute name.
Pre-existing issues:
- [High] The configfs_attrs! macro dynamically mutates a static array via AttributeList::add, causing a data race and undefined behavior when invoked concurrently at runtime. --
commit eb5933487fd2a34ea26fb0f106c056e03f633059
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
[ ... ]
rust/macros/configfs_attrs.rs line 85
Quoted 3 lines
rust/macros/configfs_attrs.rs new file +3
-0+let mut attrs =Vec ::new (); +for (attr_idx, (name, id))in cfs_attrs.attributes.iter ().enumerate () { +let name_with_attr =format_ident! ("{}_ATTR_{}" , name.to_string ().to_uppercase (), attr_idx);
[Severity: Medium]
Does this change remove compile-time duplicate name checking?
In the old macro, the generated static variables were named based only on the data type and attribute name (e.g., CONFIGURATION_MESSAGE_ATTR). If a user accidentally provided duplicate attribute names, the compiler would catch the duplicate static variable definition.
By appending attr_idx to the identifier here, duplicate attributes will compile successfully since the variable names are unique, but they will fail at runtime when the C configfs layer attempts to register them.
[Severity: Low]
Will this format_ident! call panic during compilation if a raw identifier (like r#type) is used as an attribute name?
When name is a raw identifier, name.to_string() yields "r#type", which becomes "R#TYPE" after to_uppercase(). Attempting to construct an identifier containing "R#" is invalid. Using name.unraw() might be needed to strip the prefix before processing.
Quoted 12 lines
rust/macros/configfs_attrs.rs new file +12
-0+ +let id:u64 =match id.base10_parse ::<u64 >() { +Ok (v) => v, +Err (_) => { +return syn::Error ::new (id.span (),"Could not parse attribute ID as a u64" ) + .to_compile_error (); + } + }; + + attrs.push (quote! { +static #name_with_attr: kernel::configfs::Attribute <#id , #data_ty , #data_ty > = +// SAFETY: We are expanding `configfs_attrs`.
rust/macros/configfs_attrs.rs line 107
Quoted 10 lines
rust/macros/configfs_attrs.rs new file +10
-0+unsafe { + kernel::configfs::Attribute ::new (kernel::c_str!(::core::stringify!(#name))) + }; + +// SAFETY: By design of this macro, the name of the variable we +// invoke the `add` method on below, is not visible outside of +// the macro expansion. The macro does not operate concurrently +// on this variable, and thus we have exclusive access to the +// variable. + unsafe { #data_attr_ident.add::<#attr_idx, #id, _>(&#name_with_attr) }
[Severity: High]
This is a pre-existing issue, but does this unsafe block introduce a data race?
The macro expands to a block containing a static DATA_ATTR_LIST and unsafe runtime statements (DATA_ATTR_LIST.add(...)) to populate it. Because this macro is typically evaluated at runtime inside the make_group callback (which handles mkdir syscalls), concurrent executions by multiple threads will perform unsynchronized writes to the shared static variable.
Concurrently modifying the same memory via interior mutability is undefined behavior in Rust.
Quoted 2 lines
rust/macros/configfs_attrs.rs new file +2
-0+ }); + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1