Skip to content

rust: extend `module!` macro with integer parameter support

Andreas Hindborg linux-kbuild linux-modules linux-next lkml rust-for-linux 16 versions – Applied

Status from this message: Applied, thanks!

Get series with b4
b4 am [email protected] change-id 20241211-module-params-v3-ae7e5c8d8b5a

Versions newest first

  1. v18 Latest7 patches 4 repliesApplied
  2. v177 patches 2 repliesSuperseded
  3. v167 patches 2 repliesSuperseded
Earlier versions · 13
VersionPostedPatchesRepliesStatus
v157 patches20 repliesSuperseded
v147 patches26 repliesSuperseded
v136 patches45 repliesSuperseded
v123 patches11 repliesSuperseded
v113 patches4 repliesSuperseded
v103 patches4 repliesSuperseded
v93 patches4 repliesSuperseded
v87 patches11 repliesSuperseded
v76 patches24 repliesSuperseded
v66 patches11 repliesSuperseded
v55 patches2 repliesSuperseded
v44 patches10 repliesSuperseded
v34 patches31 repliesSuperseded

Changes in v18 from the cover letter

- Rebase on Rust atomic patches (tip/master).
- Link to v17: https://lore.kernel.org/r/[email protected]

Patches in v18 Applied

#Subject and filesSizeRepliesApplied
1/7rust: sync: add `SetOnce`rust/kernel/sync.rs rust/kernel/sync/set_once.rs+127 -00 replies
2/7rust: str: add radix prefixed integer parsing functionsrust/kernel/str.rs rust/kernel/str/parse_int.rs+150 -00 replies
3/7rust: introduce module_param modulerust/kernel/lib.rs rust/kernel/module_param.rs+182 -00 replies
4/7rust: module: use a reference in macros::module::modulerust/macros/module.rs+8 -80 replies
5/7rust: module: update the module macro with module parameter supportrust/macros/helpers.rs rust/macros/lib.rs rust/macros/module.rs+224 -100 replies
6/7rust: samples: add a module parameter to the rust_minimal samplesamples/rust/rust_minimal.rs+10 -00 replies
7/7modules: add rust modules files to MAINTAINERSMAINTAINERS+2 -00 replies

Reviews trailers per patch

All 7 patches have a Reviewed-by or Acked-by.

PatchBenno LossinDaniel GomezAlice RyhlGreg Kroah-Hartman
SeriesT-b
1/7R-b R-b
2/7R-b T-b R-b
3/7R-b
4/7R-b
5/7R-b
6/7R-b
7/7A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v18 thread

Cover letter [PATCH v18 0/7] rust: extend `module!` macro with integer parameter support

Extend the module! macro with support module parameters. Also add some string to integer parsing functions.

Based on the original module parameter support by Miguel [1], later extended and generalized by Adam for more types [2][3]. Originally tracked at [4].

---
Changes in v18:
- Rebase on Rust atomic patches (tip/master).
- Link to v17: https://lore.kernel.org/r/[email protected]

Changes in v17:
- Fix drop impl of SetOnce so that it works with UnsafeCell<MaybeUninit<_>>.
- Slightly reword safety framework in SetOnce.
- Rebase on atomic series v6.
- Link to v16: https://lore.kernel.org/r/[email protected]

Changes in v16:
- Normalize imports in set_once.rs.
- Use UnsafeCell<MaybeUninit<T>> rather than Opaque<T> for SetOnce.
- Use regular load in drop of SetOnce.
- Update attribution paragraph in cover letter with details from Miguel.
- Remove stray TODO in set_once.rs
- Link to v15: https://lore.kernel.org/r/[email protected]

Changes in v15:
- Rebase on v6.16-rc5.
- Dedent code in module macro for better formatting.
- Rename OnceLock to SetOnce.
- Use "being initialized" rather than "being mutably accessed" when
describing initialization state of OnceLock.
- Use Relaxed ordering when transitioning to exclusive access in
OnceLock.
- Add drop implementation for OnceLock.
- Re-export OnceLock from kernel::sync module.
- Improve indentation of in macro code. Prefix cfg to ::core::cfg in
macro code.
- Use core::ptr::from_ref rather than as casts.
- Hide KernelParam instances behind const _: () blocks.
- Rename ModuleParamAccess::get to ModuleParamAccess::value.
- Rename RacyKernelParam to KernelParam.
- Remove ModuleParam::Value.
- Move copy implementation of OnceLock.
- Update safety comments and invariants of OnceLock.
- Link to v14: https://lore.kernel.org/r/[email protected]

Note: This series now depends on the atomics series [1].

[1] https://lore.kernel.org/all/[email protected]

Changes in v14:
- Remove unnecessary crate:: prefix from module_param::set_param.
- Make FromStrRadix safe again by moving unsafe blocks to macro implementation (thanks Benno).
- Use core::ptr::write in set_param and drop safety requirement regarding initialization.
- Add a TODO to use SyncUnsafeCell for ModuleParamAccess when available.
- Add a NOTE regarding Copy bound on ModuleParam.
- Remove 'static lifetime qualifier from ModuleParam::try_from_param_arg argument.
- Fix a typo in the safety requirements for set_param.
- Remove unused #[macro_export] attribute.
- Remove obsolete documentation for ModuleParam::try_from_param_arg.
- Make RacyKernelParam tuple field private.
- Introduce OnceLock and use that to synchronize population of parameter values.
- Link to v13: https://lore.kernel.org/r/[email protected]

Changes in v13:
- remove absolute path for ffi types.
- Split patch 2 into 4 separate patches.
- Overhaul safety framework for set_param.
- Remove generated docs for kernel_param_ops.
- Move parse_int to separate file.
- Rebase on v6.16-rc1
- Link to v12: https://lore.kernel.org/r/[email protected]

Changes in v12:
- Assign through pointer rather than using core::ptr::replace.
- Prevent a potential use-after-free during module teardown.
- Link to v11: https://lore.kernel.org/r/[email protected]

Changes in v11:
- Apply a few nits from Miguel.
- Link to v10: https://lore.kernel.org/r/[email protected]

Changes in v10:
- Apply fixups from Miguel:
- Add integer type suffixes to assert! in tests.
- Fix links to docs.kernel.org.
- Applyy markdown and intra-doc links where possible.
- Change to /// for mod docs.
- Slightly reword a comment.
- Pluralize "Examples" section name.
- Hide uses in example.
- Removed #[expect] for the rusttest target.
- Link to v9: https://lore.kernel.org/r/[email protected]

Changes in v9:
- Remove UB when parsing the minimum integer values.
- Make FromStr trait unsafe, since wrong implementations can cause UB.
- Drop patches that were applied to rust-next.
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Change print statement in sample to better communicate parameter name.
- Use imperative mode in commit messages.
- Remove prefix path from EINVAL.
- Change try_from_param_arg to accept &BStr rather than &[u8].
- Parse integers without 128 bit integer types.
- Seal trait FromStrRadix.
- Strengthen safety requirement of set_param.
- Remove comment about Display and PAGE_SIZE.
- Add note describing why ModuleParamAccess is pub.
- Typo and grammar fixes for documentation.
- Update MAINTAINERS with rust module files.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Remove dependency on pr_warn_once patches, replace with TODO.
- Rework ParseInt::from_str to avoid allocating.
- Add a comment explaining how we parse "0".
- Change trait bound on Index impl for BStr to match std library approach.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Fix a bug that prevented parsing of negative default values for
parameters in the module! macro.
- Fix a bug that prevented parsing zero in strip_radix. Also add a
test case for this.
- Add AsRef<BStr> for [u8] and BStr.
- Use impl AsRef<BStr> as type of prefix in BStr::strip_prefix.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Fix a typo in a safety comment in set_param.
- Use a match statement in parse_int::strip_radix.
- Add an implementation of Index for BStr.
- Fix a logic inversion bug where parameters would not be parsed.
- Use kernel::ffi::c_char in set_param rather than the one in core.
- Use kernel::c_str! rather than c"..." literal in module macro.
- Rebase on v6.14-rc1.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Add module maintainers to Cc list (sorry)
- Add a few missing [doc_links]
- Add panic section to expect_string_field
- Fix a typo in safety requirement of module_params::free
- Change assert! to pr_warn_once! in module_params::set_param
- Remove module_params::get_param and install null pointer instead
- Remove use of the unstable feature sync_unsafe_cell
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- use SyncUnsafeCell rather than static mut and simplify parameter access
- remove Display bound from ModuleParam
- automatically generate documentation for PARAM_OPS_.*
- remove as *const _ as *mut_ phrasing
- inline parameter name in struct instantiation in emit_params
- move RacyKernelParam out of macro template
- use C string literals rather than byte string literals with explicit null
- template out __{name}_{param_name} in emit_param
- indent template in emit_params
- use let-else expression in emit_params to get rid of an indentation level
- document expect_string_field
- move invication of impl_int_module_param to be closer to macro def
- move attributes after docs in make_param_ops
- rename impl_module_param to impl_int_module_param`
- use ty instead of ident in impl_parse_int
- use BStr instead of &str for string manipulation
- move string parsing functions to seperate patch and add examples, fix bugs
- degrade comment about future support from doc comment to regular comment
- remove std lib path from Sized marker
- update documentation for trait ModuleParam
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Remove support for params without values (NOARG_ALLOWED).
- Improve documentation for try_from_param_arg.
- Use prelude import.
- Refactor try_from_param_arg to return Result.
- Refactor ParseInt::from_str to return Result.
- Move C callable functions out of ModuleParam trait.
- Rename literal string field parser to expect_string_field.
- Move parameter parsing from generation to parsing stage.
- Use absolute type paths in macro code.
- Inline kparamand read_func values.
- Resolve TODO regarding alignment attributes.
- Remove unnecessary unsafe blocks in macro code.
- Improve error message for unrecognized parameter types.
- Do not use self receiver when reading parameter value.
- Add parameter documentation to module! macro.
- Use empty enum for parameter type.
- Use addr_of_mut to get address of parameter value variable.
- Enabled building of docs for for module_param module.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
Andreas Hindborg (7):
rust: sync: add SetOnce
rust: str: add radix prefixed integer parsing functions
rust: introduce module_param module
rust: module: use a reference in macros::module::module
rust: module: update the module macro with module parameter support
rust: samples: add a module parameter to the rust_minimal sample
modules: add rust modules files to MAINTAINERS

 MAINTAINERS                  |   2 +
 rust/kernel/lib.rs           |   1 +
 rust/kernel/module_param.rs  | 181 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/str.rs           |   2 +
 rust/kernel/str/parse_int.rs | 148 +++++++++++++++++++++++++++++++++
 rust/kernel/sync.rs          |   2 +
 rust/kernel/sync/set_once.rs | 125 ++++++++++++++++++++++++++++
 rust/macros/helpers.rs       |  25 ++++++
 rust/macros/lib.rs           |  31 +++++++
 rust/macros/module.rs        | 194 +++++++++++++++++++++++++++++++++++++++----
 samples/rust/rust_minimal.rs |  10 +++
 11 files changed, 703 insertions(+), 18 deletions(-)

---
base-commit: 103265a1a936cfe910c9ac0f0ab153f7dac818ba
change-id: 20241211-module-params-v3-ae7e5c8d8b5a

Best regards,

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