rust: add procedural macro for declaring configfs attributes
Subsystems (5)
PATCH v3 1/1 rust: 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 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 | 251 ---------------------------------------- rust/macros/configfs_attrs.rs | 135 +++++++++++++++++++++ rust/macros/helpers.rs | 139 ++++++++++++++++++++++ rust/macros/lib.rs | 85 ++++++++++++++ rust/macros/module.rs | 137 ---------------------- samples/rust/rust_configfs.rs | 2 +- 8 files changed, 362 insertions(+), 390 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 +0 -251
254 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 +85 -0
94 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 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Concurrent execution of the generated configfs_attrs! block causes a data race on a shared static array. --
commit d9a7547ab4c8957df6d5328eba0e3a712923192f
Author: Malte Wechter <[email protected]>
rust: add procedural macro for declaring configfs attributes
This commit replaces the old declarative configfs_attrs macro with a new procedural macro to improve readability and maintainability.
Quoted 5 lines
rust/macros/configfs_attrs.rs new file +0
-0
[ ... ]
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);
[ ... ]
rust/macros/configfs_attrs.rs line 107
Quoted 13 lines
rust/macros/configfs_attrs.rs new file +13
-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))) + }; + +// 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 generated code cause a data race on the static attribute list array?
Because the generated attribute list is a static variable, its state is shared across all invocations of the block. If the macro is used inside a callback like make_group, every time a new directory is created via mkdir, the add method is executed at runtime, mutating the globally shared UnsafeCell-backed array via a raw pointer.
While make_group itself is serialized per-subsystem by su_mutex, concurrent VFS reads on attributes of previously created directories do not hold this mutex. Could make_group mutate the static array at the exact same time a concurrent attribute read accesses it?
Could this array be fully initialized statically at compile time instead?
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
Hi Malte,
Some quick notes...
Quoted 2 lines · Malte Wechter
+/// ```ignore
Empty /// before examples.
Quoted 1 line
+/// // This will extract "foo: <field>" into a variable named "foo".
` instead of "
i.e. please use Markdown
Quoted 1 line
+///```
Missing space indentation
rust/macros/lib.rs pub fn kunit_tests(attr: TokenStream, input: TokenStream) -> TokenStream { · line 512
Quoted 2 lines
rust/macros/lib.rs +2
-0+/// Expands the following output: +/// let item_type = {
Missing example block, both at the beginning and the end.
Please double-check by generating the docs and looking at how they appear in the browser.
The prefix of the title should likely be rust: configfs:.
Thanks!
Cheers,
Miguel
Quoted 23 lines · Miguel Ojeda
Hi Malte,
Some quick notes...
Quoted 1 line · Malte Wechter
+/// ```ignore
Empty /// before examples.
Quoted 1 line
+/// // This will extract "foo: <field>" into a variable named "foo".
` instead of "
i.e. please use Markdown
Quoted 1 line
+///```
Missing space indentation
Review
Quoted 2 lines
(quoted diff) +2
-0+/// Expands the following output: +/// let item_type = {Missing example block, both at the beginning and the end.
Please double-check by generating the docs and looking at how they appear in the browser.
Quoted 6 lines
The prefix of the title should likely be
rust: configfs:.Thanks!
Cheers,
Miguel
As of now doc strings are not generated for private items in the macros crate. I am moving the parse_ordered_fields! macro into macros/helpers.rs but this means the doc strings are not generated for the macro anymore. The parse_ordered_fields! macro is a larger helper function, and the doc strings are relevant and helpful for macro developers that wants to use it.
You can enable documenting private items:
rust/Makefile +0 -0
@@ -147,6 +147,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
OBJTREE=$(abspath $(objtree)) \
$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-scope=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
$(rustc_target_flags) -L$(objtree)/$(obj) \
+ --document-private-items \
-Zunstable-options --generate-link-to-definition \
--output $(rustdoc_output) \
--crate-name $(subst rustdoc-,,$@) \
But this enables _all_ private items to get rendered, which is not ideal. How should i proceed? Best regards,
Malte
Quoted 7 lines · Malte Wechter
As of now doc strings are not generated for private items in the macros crate. I am moving the
parse_ordered_fields!macro intomacros/helpers.rsbut this means the doc strings are not generated for the macro anymore. Theparse_ordered_fields!macro is a larger helper function, and the doc strings are relevant and helpful for macro developers that wants to use it.
If it is private, then it is what it is, don't worry about it -- developers can still read the source code.
But, yes, having a render of the private items is something I have wanted for a long time, but as a runtime toggle, so that it is easy to go from one to the other (and without having to have 2 entire copies of the docs).
Please the entry "Private documentation (perhaps as an extension of the private items/fields toggle)" I have at:
https://github.com/Rust-for-Linux/linux/issues/350
Upstream rustdoc implemented an MVP of the idea via CSS/JS in this draft PR:
https://github.com/rust-lang/rust/pull/141299
If you want to help on that, then you could try it and leave some feedback there! :)
Thanks!
Cheers,
Miguel
Quoted 24 lines · Miguel Ojeda
Quoted 6 lines · Malte Wechter
As of now doc strings are not generated for private items in the macros crate. I am moving the
parse_ordered_fields!macro intomacros/helpers.rsbut this means the doc strings are not generated for the macro anymore. Theparse_ordered_fields!macro is a larger helper function, and the doc strings are relevant and helpful for macro developers that wants to use it.If it is private, then it is what it is, don't worry about it -- developers can still read the source code.
But, yes, having a render of the private items is something I have wanted for a long time, but as a runtime toggle, so that it is easy to go from one to the other (and without having to have 2 entire copies of the docs).
Please the entry "Private documentation (perhaps as an extension of the private items/fields toggle)" I have at:
https://github.com/Rust-for-Linux/linux/issues/350Upstream
rustdocimplemented an MVP of the idea via CSS/JS in this draft PR:https://github.com/rust-lang/rust/pull/141299
Quoted 7 lines
If you want to help on that, then you could try it and leave some feedback there! :)
Thanks!
Cheers,
Miguel
I will leave it as it is then.
I will try and look at it if time permits :)
Best regards,
Malte