Add support for parsing boolean module parameters in the Rust module! macro.
Currently, only integer types are supported by the `module_param!` macros. This patch implements the `ModuleParam` trait for `bool` by delegating the string parsing to the existing C implementation via `kstrtobool_bytes()`. It also wires up `PARAM_OPS_BOOL` so that the Rust parameter system correctly links to the C `param_ops_bool` structure. For demonstration and verification, a boolean parameter is added to `samples/rust/rust_minimal.rs`. Assisted-by: Codex:GPT-5 Signed-off-by: Wenzhao Liao <[email protected]> --- rust/kernel/module_param.rs | 9 ++++++++- rust/macros/lib.rs | 1 + rust/macros/module.rs | 1 + samples/rust/rust_minimal.rs | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/rust/kernel/module_param.rs b/rust/kernel/module_param.rs index 6a8a7a875643..04ce9eda6731 100644 --- a/rust/kernel/module_param.rs +++ b/rust/kernel/module_param.rs @@ -5,7 +5,7 @@ //! C header: [`include/linux/moduleparam.h`](srctree/include/linux/moduleparam.h) use crate::prelude::*; -use crate::str::BStr; +use crate::str::{kstrtobool_bytes, BStr}; use bindings; use kernel::sync::SetOnce; @@ -106,6 +106,12 @@ fn try_from_param_arg(arg: &BStr) -> Result<Self> { impl_int_module_param!(isize); impl_int_module_param!(usize); +impl ModuleParam for bool { + fn try_from_param_arg(arg: &BStr) -> Result<Self> { + kstrtobool_bytes(arg) + } +} + /// A wrapper for kernel parameters. /// /// This type is instantiated by the [`module!`] macro when module parameters are @@ -180,3 +186,4 @@ macro_rules! make_param_ops { make_param_ops!(PARAM_OPS_U64, u64); make_param_ops!(PARAM_OPS_ISIZE, isize); make_param_ops!(PARAM_OPS_USIZE, usize); +make_param_ops!(PARAM_OPS_BOOL, bool); diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index 0c36194d9971..95bc3f066b49 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -52,6 +52,7 @@ /// - [`u64`] /// - [`isize`] /// - [`usize`] +/// - [`bool`] /// /// C header: [`include/linux/moduleparam.h`](srctree/include/linux/moduleparam.h) /// diff --git a/rust/macros/module.rs b/rust/macros/module.rs index e16298e520c7..feafa0c1623c 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -197,6 +197,7 @@ fn param_ops_path(param_type: &str) -> Path { "u64" => parse_quote!(::kernel::module_param::PARAM_OPS_U64), "isize" => parse_quote!(::kernel::module_param::PARAM_OPS_ISIZE), "usize" => parse_quote!(::kernel::module_param::PARAM_OPS_USIZE), + "bool" => parse_quote!(::kernel::module_param::PARAM_OPS_BOOL), t => panic!("Unsupported parameter type {}", t), } } diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs index 8eb9583571d7..fedf5be1f713 100644 --- a/samples/rust/rust_minimal.rs +++ b/samples/rust/rust_minimal.rs @@ -15,6 +15,10 @@ default: 1, description: "This parameter has a default of 1", }, + test_bool_parameter: bool { + default: false, + description: "This boolean parameter defaults to false", + }, }, } @@ -30,6 +34,10 @@ fn init(_module: &'static ThisModule) -> Result<Self> { "test_parameter: {}\n", *module_parameters::test_parameter.value() ); + pr_info!( + "test_bool_parameter: {}\n", + *module_parameters::test_bool_parameter.value() + ); let mut numbers = KVec::new(); numbers.push(72, GFP_KERNEL)?; -- 2.34.1

