Skip to contentSkip to conversation

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

PATCH Superseded Andreas Hindborg 8 participants 36 messages Dec 13, 2024 – Jan 8, 2025

Subsystems (3)

v3 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
author reviewer
#3 lore raw

PATCH v3 2/4 rust: str: implement `strip_prefix` for `BStr`

In reply to Andreas Hindborg

Implement strip_prefix for BStr by deferring to slice::strip_prefix on the underlying &[u8].

---

It is also possible to get this method by implementing core::slice::SlicePattern for BStr. SlicePattern is unstable, so this seems more reasonable.

---
 rust/kernel/str.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
rust/kernel/str.rs +16 -0
index c441acf76ebd1f14919b6d233edffbbbbf944619..9c446ff1ad7adba7ca09a5ae9df00fd369a32899 100644
@@ -31,6 +31,22 @@ pub const fn from_bytes(bytes: &[u8]) -> &Self {
         // SAFETY: `BStr` is transparent to `[u8]`.
         unsafe { &*(bytes as *const [u8] as *const BStr) }
     }
+
+    /// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`].
+    ///
+    /// # Example
+    /// ```
+    /// use kernel::b_str;
+    /// assert_eq!(Some(b_str!("bar")), b_str!("foobar").strip_prefix(b_str!("foo")));
+    /// assert_eq!(None, b_str!("foobar").strip_prefix(b_str!("bar")));
+    /// assert_eq!(Some(b_str!("foobar")), b_str!("foobar").strip_prefix(b_str!("")));
+    /// assert_eq!(Some(b_str!("")), b_str!("foobar").strip_prefix(b_str!("foobar")));
+    /// ```
+    pub fn strip_prefix(&self, pattern: &Self) -> Option<&BStr> {
+        self.deref()
+            .strip_prefix(pattern.deref())
+            .map(Self::from_bytes)
+    }
 }
 
 impl fmt::Display for BStr {
Signature · 1 line
2.47.0

Andreas Hindborg

author reviewer
#0 lore raw

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

This series extends the module! macro with support module parameters. It also adds some string to integer parsing functions and updates BStr with a method to strip a string prefix.

This series stated out as code by Adam Bratschi-Kaye lifted from the original rust branch [1].

---
Changes since v2 [1]:
- 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

Changes since v1 [2]:
- 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.

---

---
Andreas Hindborg (4):
rust: str: implement PartialEq for BStr
rust: str: implement strip_prefix for BStr
rust: str: add radix prefixed integer parsing functions
rust: add parameter support to the module! macro

 rust/kernel/lib.rs           |   2 +
 rust/kernel/module_param.rs  | 238 +++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/str.rs           | 138 +++++++++++++++++++++++++
 rust/macros/helpers.rs       |  10 ++
 rust/macros/lib.rs           |  31 ++++++
 rust/macros/module.rs        | 188 ++++++++++++++++++++++++++++++----
 samples/rust/rust_minimal.rs |  10 ++
 scripts/Makefile.build       |   2 +-
 8 files changed, 600 insertions(+), 19 deletions(-)

---
base-commit: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4
change-id: 20241211-module-params-v3-ae7e5c8d8b5a

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
author reviewer
#4 lore raw

PATCH v3 3/4 rust: str: add radix prefixed integer parsing functions

Add the trait ParseInt for parsing string representations of integers where the string representations are optionally prefixed by a radix specifier. Implement the trait for the primitive integer types.

---
 rust/kernel/str.rs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
rust/kernel/str.rs +116 -0
index 9c446ff1ad7adba7ca09a5ae9df00fd369a32899..4da711f264397c7a1fce34edb69a71253d03911e 100644
@@ -914,3 +914,119 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 macro_rules! fmt {
     ($($f:tt)*) => ( core::format_args!($($f)*) )
 }
+
+pub mod parse_int {
+    //! Integer parsing functions for parsing signed and unsigned integers
+    //! potentially prefixed with `0x`, `0o`, or `0b`.
+
+    use crate::alloc::flags;
+    use crate::prelude::*;
+    use crate::str::BStr;
+
+    /// Trait that allows parsing a `&BStr` to an integer with a radix.
+    // This is required because the `from_str_radix` function on the primitive
+    // integer types is not part of any trait.
+    pub trait FromStrRadix: Sized {
+        /// Parse `src` to `Self` using radix `radix`.
+        fn from_str_radix(src: &BStr, radix: u32) -> Result<Self, crate::error::Error>;
+    }
+
+    /// Extract the radix from an integer literal optionally prefixed with
+    /// one of `0x`, `0X`, `0o`, `0O`, `0b`, `0B`, `0`.
+    fn strip_radix(src: &BStr) -> (u32, &BStr) {
+        if let Some(n) = src.strip_prefix(b_str!("0x")) {
+            (16, n)
+        } else if let Some(n) = src.strip_prefix(b_str!("0X")) {
+            (16, n)
+        } else if let Some(n) = src.strip_prefix(b_str!("0o")) {
+            (8, n)
+        } else if let Some(n) = src.strip_prefix(b_str!("0O")) {
+            (8, n)
+        } else if let Some(n) = src.strip_prefix(b_str!("0b")) {
+            (2, n)
+        } else if let Some(n) = src.strip_prefix(b_str!("0B")) {
+            (2, n)
+        } else if let Some(n) = src.strip_prefix(b_str!("0")) {
+            (8, n)
+        } else {
+            (10, src)
+        }
+    }
+
+    /// Trait for parsing string representations of integers.
+    ///
+    /// Strings beginning with `0x`, `0o`, or `0b` are parsed as hex, octal, or
+    /// binary respectively. Strings beginning with `0` otherwise are parsed as
+    /// octal. Anything else is parsed as decimal. A leading `+` or `-` is also
+    /// permitted. Any string parsed by [`kstrtol()`] or [`kstrtoul()`] will be
+    /// successfully parsed.
+    ///
+    /// [`kstrtol()`]: https://www.kernel.org/doc/html/latest/core-api/kernel-api.html#c.kstrtol
+    /// [`kstrtoul()`]: https://www.kernel.org/doc/html/latest/core-api/kernel-api.html#c.kstrtoul
+    ///
+    /// # Example
+    /// ```
+    /// use kernel::str::parse_int::ParseInt;
+    /// use kernel::b_str;
+    ///
+    /// assert_eq!(Ok(0xa2u8), u8::from_str(b_str!("0xa2")));
+    /// assert_eq!(Ok(-0xa2i32), i32::from_str(b_str!("-0xa2")));
+    ///
+    /// assert_eq!(Ok(-0o57i8), i8::from_str(b_str!("-0o57")));
+    /// assert_eq!(Ok(0o57i8), i8::from_str(b_str!("057")));
+    ///
+    /// assert_eq!(Ok(0b1001i16), i16::from_str(b_str!("0b1001")));
+    /// assert_eq!(Ok(-0b1001i16), i16::from_str(b_str!("-0b1001")));
+    ///
+    /// assert_eq!(Ok(127), i8::from_str(b_str!("127")));
+    /// assert!(i8::from_str(b_str!("128")).is_err());
+    /// assert_eq!(Ok(-128), i8::from_str(b_str!("-128")));
+    /// assert!(i8::from_str(b_str!("-129")).is_err());
+    /// assert_eq!(Ok(255), u8::from_str(b_str!("255")));
+    /// assert!(u8::from_str(b_str!("256")).is_err());
+    /// ```
+    pub trait ParseInt: FromStrRadix {
+        /// Parse a string according to the description in [`Self`].
+        fn from_str(src: &BStr) -> Result<Self> {
+            match src.iter().next() {
+                None => Err(EINVAL),
+                Some(sign @ b'-') | Some(sign @ b'+') => {
+                    let (radix, digits) = strip_radix(BStr::from_bytes(&src[1..]));
+                    let mut n_digits: KVec<u8> =
+                        KVec::with_capacity(digits.len() + 1, flags::GFP_KERNEL)?;
+                    n_digits.push(*sign, flags::GFP_KERNEL)?;
+                    n_digits.extend_from_slice(digits, flags::GFP_KERNEL)?;
+                    Self::from_str_radix(BStr::from_bytes(&n_digits), radix).map_err(|_| EINVAL)
+                }
+                Some(_) => {
+                    let (radix, digits) = strip_radix(src);
+                    Self::from_str_radix(digits, radix).map_err(|_| EINVAL)
+                }
+            }
+        }
+    }
+
+    macro_rules! impl_parse_int {
+        ($ty:ty) => {
+            impl FromStrRadix for $ty {
+                fn from_str_radix(src: &BStr, radix: u32) -> Result<Self, crate::error::Error> {
+                    <$ty>::from_str_radix(core::str::from_utf8(src).map_err(|_| EINVAL)?, radix)
+                        .map_err(|_| EINVAL)
+                }
+            }
+
+            impl ParseInt for $ty {}
+        };
+    }
+
+    impl_parse_int!(i8);
+    impl_parse_int!(u8);
+    impl_parse_int!(i16);
+    impl_parse_int!(u16);
+    impl_parse_int!(i32);
+    impl_parse_int!(u32);
+    impl_parse_int!(i64);
+    impl_parse_int!(u64);
+    impl_parse_int!(isize);
+    impl_parse_int!(usize);
+}
Signature · 1 line
2.47.0
author reviewer
#1 lore raw

PATCH v3 1/4 rust: str: implement `PartialEq` for `BStr`

In reply to Andreas Hindborg

Implement PartialEq for BStr by comparing underlying byte slices.

---
 rust/kernel/str.rs | 6 ++++++
 1 file changed, 6 insertions(+)
rust/kernel/str.rs +6 -0
index d04c12a1426d1c1edeb88325bcd9c63bf45f9b60..c441acf76ebd1f14919b6d233edffbbbbf944619 100644
@@ -106,6 +106,12 @@ fn deref(&self) -> &Self::Target {
     }
 }
 
+impl PartialEq for BStr {
+    fn eq(&self, other: &Self) -> bool {
+        self.deref().eq(other.deref())
+    }
+}
+
 /// Creates a new [`BStr`] from a string literal.
 ///
 /// `b_str!` converts the supplied string literal to byte string, so non-ASCII
Signature · 1 line
2.47.0
author reviewer
#5 lore raw

PATCH v3 4/4 rust: add parameter support to the `module!` macro

In reply to Andreas Hindborg

This patch includes changes required for Rust kernel modules to utilize module parameters. This code implements read only support for integer types without sysfs support.

---
 rust/kernel/lib.rs           |   2 +
 rust/kernel/module_param.rs  | 238 +++++++++++++++++++++++++++++++++++++++++++
 rust/macros/helpers.rs       |  10 ++
 rust/macros/lib.rs           |  31 ++++++
 rust/macros/module.rs        | 188 ++++++++++++++++++++++++++++++----
 samples/rust/rust_minimal.rs |  10 ++
 scripts/Makefile.build       |   2 +-
 7 files changed, 462 insertions(+), 19 deletions(-)
rust/kernel/lib.rs +2 -0
index e1065a7551a39e68d6379031d80d4be336e652a3..7670c47a967cdd7e4f63e00b8338c6042e391fee 100644
@@ -18,6 +18,7 @@
 #![feature(inline_const)]
 #![feature(lint_reasons)]
 #![feature(unsize)]
+#![feature(sync_unsafe_cell)]
 
 // Ensure conditional compilation based on the kernel configuration works;
 // otherwise we may silently break things like initcall handling.
@@ -46,6 +47,7 @@
 pub mod kunit;
 pub mod list;
 pub mod miscdevice;
+pub mod module_param;
 #[cfg(CONFIG_NET)]
 pub mod net;
 pub mod page;
rust/kernel/module_param.rs new file +238 -0

238 lines not on this page · raw message

rust/macros/helpers.rs +10 -0
index 563dcd2b7ace5e8322d0fddb409571cca2dd31ea..e4f2751e0f6bbd6a980ba57d630480f05682f59f 100644
@@ -107,6 +107,16 @@ pub(crate) struct Generics {
     pub(crate) ty_generics: Vec<TokenTree>,
 }
 
+/// Parse a token stream of the form `expected_name: "value",` and return the
+/// string in the position of "value". Panics on parse error.
+pub(crate) fn expect_string_field(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
+    assert_eq!(expect_ident(it), expected_name);
+    assert_eq!(expect_punct(it), ':');
+    let string = expect_string(it);
+    assert_eq!(expect_punct(it), ',');
+    string
+}
+
 /// Parses the given `TokenStream` into `Generics` and the rest.
 ///
 /// The generics are not present in the rest, but a where clause might remain.
rust/macros/lib.rs +31 -0
index 4ab94e44adfe3206faad159e81417ea41a35815b..a75d354e3eec49fd4066ac3dd96c4afda2606d50 100644
@@ -24,6 +24,30 @@
 /// The `type` argument should be a type which implements the [`Module`]
 /// trait. Also accepts various forms of kernel metadata.
 ///
+/// The `params` field describe module parameters. Each entry has the form
+///
+/// ```ignore
+/// parameter_name: type {
+///     default: default_value,
+///     description: "Description",
+/// }
+/// ```
+///
+/// `type` may be one of
+///
+/// - `i8`
+/// - `u8`
+/// - `i8`
+/// - `u8`
+/// - `i16`
+/// - `u16`
+/// - `i32`
+/// - `u32`
+/// - `i64`
+/// - `u64`
+/// - `isize`
+/// - `usize`
+///
 /// C header: [`include/linux/moduleparam.h`](srctree/include/linux/moduleparam.h)
 ///
 /// [`Module`]: ../kernel/trait.Module.html
@@ -40,6 +64,12 @@
 ///     description: "My very own kernel module!",
 ///     license: "GPL",
 ///     alias: ["alternate_module_name"],
+///     params: {
+///         my_parameter: i64 {
+///             default: 1,
+///             description: "This parameter has a default of 1",
+///         },
+///     },
 /// }
 ///
 /// struct MyModule(i32);
@@ -48,6 +78,7 @@
 ///     fn init(_module: &'static ThisModule) -> Result<Self> {
 ///         let foo: i32 = 42;
 ///         pr_info!("I contain:  {}\n", foo);
+///         pr_info!("i32 param is:  {}\n", module_parameters::my_parameter.read());
 ///         Ok(Self(foo))
 ///     }
 /// }
rust/macros/module.rs +170 -18

274 lines not on this page · raw message

samples/rust/rust_minimal.rs +10 -0
index 4aaf117bf8e3c0cc77e188b1ad0708e3650a6654..d999a77c6eb9a0db9acf5fa03d4feca5e6525fc8 100644
@@ -10,6 +10,12 @@
     author: "Rust for Linux Contributors",
     description: "Rust minimal sample",
     license: "GPL",
+    params: {
+        test_parameter: i64 {
+            default: 1,
+            description: "This parameter has a default of 1",
+        },
+    },
 }
 
 struct RustMinimal {
@@ -20,6 +26,10 @@ impl kernel::Module for RustMinimal {
     fn init(_module: &'static ThisModule) -> Result<Self> {
         pr_info!("Rust minimal sample (init)\n");
         pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
+        pr_info!(
+            "My parameter: {}\n",
+            *module_parameters::test_parameter.get()
+        );
 
         let mut numbers = KVec::new();
         numbers.push(72, GFP_KERNEL)?;
scripts/Makefile.build +1 -1
index c16e4cf54d770fd2cf585a65dc4ad9b6a6bd6e42..dabae1007afcb376218e65b022df72c494c8ab51 100644
@@ -213,7 +213,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
 # Compile Rust sources (.rs)
 # ---------------------------------------------------------------------------
 
-rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons
+rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,sync_unsafe_cell
 
 # `--out-dir` is required to avoid temporaries being created by `rustc` in the
 # current working directory, which may be not accessible in the out-of-tree
Signature · 1 line
2.47.0
#15 lore raw
In reply to Andreas Hindborg
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Please only really add module parameters if you can prove that you actually need a module parameter.

thanks,

greg k-h

#6 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Andreas Hindborg
Quoted 3 lines · Andreas Hindborg

This patch includes changes required for Rust kernel modules to utilize module parameters. This code implements read only support for integer types without sysfs support.

read-only is VERY limited, and as such, only good for boot options, which as I mentioned before, is not how any "modern" kernel driver should be doing things.

And no sysfs interaction? That's going to confuse the heck out of people wondering why the option they added doesn't show up in the same place it normally would if they did it in C, right? Not that I'm saying this should be done at all, just that this is going to be confusing right off the bat which is probably not a good idea.

Friends don't let friends add new module parameters to the kernel :)

thanks,

greg k-h

author reviewer
#16 lore raw
In reply to Greg KH
Quoted 11 lines · Greg KH
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Please only really add module parameters if you can prove that you actually need a module parameter.

I really need module parameters to make rust null block feature compatible with C null block.

Let's not block interfacing parts of the kernel because we decided that the way we (well not me, I was not around) did things in the 80's was less than stellar. I mean, we would get nowhere.

Best regards,
Andreas Hindborg

author reviewer
#34 lore raw
In reply to Andreas Hindborg

Hi Luis, Petr, Sami, Dani,

I just noticed that I failed in email and forgot to add you to the recipient list of this series. Do you want a resend, or is this sufficient?

Best regards,
Andreas Hindborg

Quoted 78 lines · Andreas Hindborg

This series extends the module! macro with support module parameters. It also adds some string to integer parsing functions and updates BStr with a method to strip a string prefix.

This series stated out as code by Adam Bratschi-Kaye lifted from the original rust branch [1].

---
Changes since v2 [1]:
- 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

Changes since v1 [2]:
- 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.

---

---
Andreas Hindborg (4):
rust: str: implement PartialEq for BStr
rust: str: implement strip_prefix for BStr
rust: str: add radix prefixed integer parsing functions
rust: add parameter support to the module! macro

 rust/kernel/lib.rs           |   2 +
 rust/kernel/module_param.rs  | 238 +++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/str.rs           | 138 +++++++++++++++++++++++++
 rust/macros/helpers.rs       |  10 ++
 rust/macros/lib.rs           |  31 ++++++
 rust/macros/module.rs        | 188 ++++++++++++++++++++++++++++++----
 samples/rust/rust_minimal.rs |  10 ++
 scripts/Makefile.build       |   2 +-
 8 files changed, 600 insertions(+), 19 deletions(-)

---
base-commit: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4
change-id: 20241211-module-params-v3-ae7e5c8d8b5a

Best regards,

author reviewer
#7 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Greg KH
Quoted 8 lines · Greg KH
Quoted 3 lines · Andreas Hindborg

This patch includes changes required for Rust kernel modules to utilize module parameters. This code implements read only support for integer types without sysfs support.

read-only is VERY limited, and as such, only good for boot options, which as I mentioned before, is not how any "modern" kernel driver should be doing things.

I only added what is required to get rust null block compatibility going. I did not want to add dead code - I heard that is frowned upon.

Quoted 5 lines

And no sysfs interaction? That's going to confuse the heck out of people wondering why the option they added doesn't show up in the same place it normally would if they did it in C, right? Not that I'm saying this should be done at all, just that this is going to be confusing right off the bat which is probably not a good idea.

No, these work the same way as their counter parts in C. They reuse the same C machinery. They just only allow the user to specify a subset of the permission flags.

The C null_blk parameters are actually configured to appear read-only in sysfs. I guess I should add that in a follow-up.

Quoted 1 line

Friends don't let friends add new module parameters to the kernel :)

OK, that makes sense. But I'm trying to build something that plugs in where we currently have a piece of C code that relies on module parameters.

Jens, would you be OK with Rust null block only providing configuration through configfs?

Best regards,
Andreas Hindborg

reviewer
#17 lore raw
In reply to Andreas Hindborg
Quoted 12 lines · Andreas Hindborg
Quoted 9 lines · Greg KH
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Quoted 5 lines
Quoted 2 lines

Please only really add module parameters if you can prove that you actually need a module parameter.

I really need module parameters to make rust null block feature compatible with C null block.

Instead of providing module parameters to Rust code, you could implement that part of Rust nullblk in C. That way, you discourage future Rust drivers from using module parameters without making it impossible to have them in Rust nullblk.

Alice

#8 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Andreas Hindborg
Quoted 2 lines · Andreas Hindborg

+#![feature(sync_unsafe_cell)]

Please mention this in the commit message, the status of the feature and justify the addition.

Quoted 1 line

+//! C header: [include/linux/moduleparam.h](../../../include/linux/moduleparam.h)

Please use srctree.

Quoted 1 line

+/// Newtype to make bindings::kernel_param Sync.

Please add intra-doc links where applicable, e.g. Sync here.

rust/kernel/module_param.rs line 85

Quoted 10 lines
rust/kernel/module_param.rs new file +10 -0
+unsafe extern "C" fn set_param<T>(
+    val: *const core::ffi::c_char,
+    param: *const crate::bindings::kernel_param,
+) -> core::ffi::c_int
+where
+    T: ModuleParam,
+{
+    // NOTE: If we start supporting arguments without values, val _is_ allowed
+    // to be null here.
+    assert!(!val.is_null());

Should this return an error instead?

Quoted 9 lines
rust/kernel/module_param.rs new file +9 -0
+/// Write a string representation of the current parameter value to `buf`.
+///
+/// # Safety
+///
+/// Must not be called.
+///
+/// # Note
+///
+/// This should not be called as we declare all parameters as read only.

rust/kernel/module_param.rs line 125

Quoted 10 lines
rust/kernel/module_param.rs new file +10 -0
+#[allow(clippy::extra_unused_type_parameters)]
+unsafe extern "C" fn get_param<T>(
+    _buf: *mut core::ffi::c_char,
+    _param: *const crate::bindings::kernel_param,
+) -> core::ffi::c_int
+where
+    T: ModuleParam,
+{
+    unreachable!("Parameters are not readable");
+}

Do we need this? Can't the ops callback be null?

Quoted 1 line

+/// The arg field of param must be an initialized instance of Self.

Self?

Quoted 1 line

+/// Generate a static [kernel_param_ops](../../../include/linux/moduleparam.h) struct.

srctree.

rust/macros/helpers.rs pub(crate) struct Generics { · line 111

Quoted 2 lines
rust/macros/helpers.rs +2 -0
+/// Parse a token stream of the form `expected_name: "value",` and return the
+/// string in the position of "value". Panics on parse error.

# Panics section.

Quoted 4 lines
rust/macros/lib.rs +4 -0
+/// `type` may be one of
+///
+/// - `i8`
+/// - `u8`

rust/macros/lib.rs line 49

Quoted 10 lines
rust/macros/lib.rs +10 -0
+/// - `i8`
+/// - `u8`
+/// - `i16`
+/// - `u16`
+/// - `i32`
+/// - `u32`
+/// - `i64`
+/// - `u64`
+/// - `isize`
+/// - `usize`

Can these be intra-doc links?

Thanks!

Cheers,
Miguel

author reviewer
#18 lore raw
In reply to Alice Ryhl
Quoted 16 lines · Alice Ryhl
Quoted 15 lines · Andreas Hindborg
Quoted 11 lines · Greg KH
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Please only really add module parameters if you can prove that you actually need a module parameter.

Quoted 7 lines
Quoted 2 lines

I really need module parameters to make rust null block feature compatible with C null block.

Instead of providing module parameters to Rust code, you could implement that part of Rust nullblk in C. That way, you discourage future Rust drivers from using module parameters without making it impossible to have them in Rust nullblk.

If the opinion of the community is really to phase out module parameters for all new drivers (is it?), I can maybe move the code in question into the Rust null_blk driver.

I was kind of looking forward to having zero unsafe code in the driver though.

On the other hand, rust null block might not be the only "rewrite in rust and keep compatibility" project to ever see the light of day.

Best regards,
Andreas Hindborg

author reviewer
#9 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Miguel Ojeda

Hi Miguel,

Quoted 6 lines · Miguel Ojeda
Quoted 2 lines · Andreas Hindborg

+#![feature(sync_unsafe_cell)]

Please mention this in the commit message, the status of the feature and justify the addition.

I forgot, thanks for pointing that out. Following the discussion of v2 of this series I can understand that mut static is discouraged and scheduled for removal. Interior mutability via SyncUnsafeCell provides the same functionality and it is my understanding that this feature is on track to be stabilized.

Quoted 4 lines
Quoted 2 lines

+//! C header: [include/linux/moduleparam.h](../../../include/linux/moduleparam.h)

Please use srctree.

Ok.

Quoted 4 lines
Quoted 2 lines

+/// Newtype to make bindings::kernel_param Sync.

Please add intra-doc links where applicable, e.g. Sync here.

Will do.

Quoted 13 lines

rust/kernel/module_param.rs line 85

Quoted 11 lines
rust/kernel/module_param.rs new file +10 -0
+unsafe extern "C" fn set_param<T>(
+    val: *const core::ffi::c_char,
+    param: *const crate::bindings::kernel_param,
+) -> core::ffi::c_int
+where
+    T: ModuleParam,
+{
+    // NOTE: If we start supporting arguments without values, val _is_ allowed
+    // to be null here.
+    assert!(!val.is_null());

Should this return an error instead?

Not sure. val being null not supposed to happen in the current configuration. It should be an unreachable state. So BUG is the right thing?

Quoted 14 lines
Quoted 14 lines
rust/kernel/module_param.rs new file +13 -0
+/// Write a string representation of the current parameter value to `buf`.
+///
+/// # Safety
+///
+/// Must not be called.
+///
+/// # Note
+///
+/// This should not be called as we declare all parameters as read only.
+#[allow(clippy::extra_unused_type_parameters)]
+unsafe extern "C" fn get_param<T>(
+    _buf: *mut core::ffi::c_char,
+    _param: *const crate::bindings::kernel_param,
Quoted 8 lines

rust/kernel/module_param.rs line 125

Quoted 6 lines
rust/kernel/module_param.rs new file +6 -0
+) -> core::ffi::c_int
+where
+    T: ModuleParam,
+{
+    unreachable!("Parameters are not readable");
+}

Do we need this? Can't the ops callback be null?

Not in the current configuration. The parameters can only be declared "read only". It should be impossible for anyone to call this function.

Quoted 4 lines
Quoted 2 lines

+/// The arg field of param must be an initialized instance of Self.

Self?

That whole line is wrong, thanks for spotting. It should read "arg must be an initialized instance of T. This function takes ownership of the T pointed to by arg.".

Quoted 4 lines
Quoted 2 lines

+/// Generate a static [kernel_param_ops](../../../include/linux/moduleparam.h) struct.

srctree.

👍

Quoted 5 lines

rust/macros/helpers.rs pub(crate) struct Generics { · line 111

Quoted 3 lines
rust/macros/helpers.rs +2 -0
+/// Parse a token stream of the form `expected_name: "value",` and return the
+/// string in the position of "value". Panics on parse error.

# Panics section.

Ok.

Quoted 17 lines
Quoted 5 lines
rust/macros/lib.rs +4 -0
+/// `type` may be one of
+///
+/// - `i8`
+/// - `u8`

rust/macros/lib.rs line 49

Quoted 10 lines
rust/macros/lib.rs +10 -0
+/// - `i8`
+/// - `u8`
+/// - `i16`
+/// - `u16`
+/// - `i32`
+/// - `u32`
+/// - `i64`
+/// - `u64`
+/// - `isize`
+/// - `usize`

Can these be intra-doc links?

Yes!

Thanks for the comments!

Best regards,
Andreas Hindborg

#20 lore raw
In reply to Andreas Hindborg
Quoted 11 lines · Andreas Hindborg
Quoted 9 lines · Greg KH
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Quoted 5 lines
Quoted 2 lines

Please only really add module parameters if you can prove that you actually need a module parameter.

I really need module parameters to make rust null block feature compatible with C null block.

Is that a requirement? That wasn't documented here :(

You should have put the user of these apis in the series as you have that code already in the tree, right?

Quoted 3 lines

Let's not block interfacing parts of the kernel because we decided that the way we (well not me, I was not around) did things in the 80's was less than stellar. I mean, we would get nowhere.

On the contrary, if we don't learn from our past mistakes, we will constantly keep making them and prevent others from "doing the right thing" by default.

I would strongly prefer that any driver not have any module parameters at all, as drivers don't work properly that way (again, they need to handle multiple devices, which does not work for a module parameter.)

That's why we created sysfs, configfs, and lots of other things, to learn from our past mistakes.

thanks,

greg k-h

author reviewer
#21 lore raw
Quoted 15 lines · Greg KH
Quoted 14 lines · Andreas Hindborg
Quoted 11 lines · Greg KH
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Please only really add module parameters if you can prove that you actually need a module parameter.

Quoted 7 lines
Quoted 2 lines

I really need module parameters to make rust null block feature compatible with C null block.

Is that a requirement? That wasn't documented here :(

You should have put the user of these apis in the series as you have that code already in the tree, right?

Sorry, my mistake. I'm trying to build a rust implementation of C null_blk, and one the bits I need for that is module parameters.

Quoted 9 lines
Quoted 4 lines

Let's not block interfacing parts of the kernel because we decided that the way we (well not me, I was not around) did things in the 80's was less than stellar. I mean, we would get nowhere.

On the contrary, if we don't learn from our past mistakes, we will constantly keep making them and prevent others from "doing the right thing" by default.

Quoted 6 lines

I would strongly prefer that any driver not have any module parameters at all, as drivers don't work properly that way (again, they need to handle multiple devices, which does not work for a module parameter.)

That's why we created sysfs, configfs, and lots of other things, to learn from our past mistakes.

OK. I understand. It makes sense even :) I wish I knew that this was a thing before I spent spare cycles fixing this up for v3 though.

I'm not getting a clear reading on the following, perhaps you can clarify:

- Is the community aligned on dropping module parameters for all new drivers?
- If so, was this decided upon at some point or is this a fluid decision that is just manifesting now?
- Does this ban of module parameters also cover cases where backwards compatibility is desirable?
- Can we merge this so I can move forward at my current projected course, or should I plan on dealing with not having this available?

Best regards,
Andreas Hindborg

#22 lore raw
Quoted 11 lines · Andreas Hindborg
Quoted 3 lines · Greg KH
Quoted 2 lines · Andreas Hindborg
Quoted 1 line · Greg KH

On Fri, Dec 13, 2024 at 12:30:45PM +0100, Andreas Hindborg wrote:

I'm not getting a clear reading on the following, perhaps you can clarify:

- Is the community aligned on dropping module parameters for all new drivers?
- If so, was this decided upon at some point or is this a fluid decision that is just manifesting now?

It's something that I've been saying in review comments of drivers for many many years now. Again, it was one of the main reasons we created configfs and sysfs all those decades ago, because module parameters just do not work properly for drivers in almost all cases.

Quoted 2 lines

- Does this ban of module parameters also cover cases where backwards compatibility is desirable?

No, we don't break existing kernel features, but if you are writing a new driver, don't add them and then there's no compatibility issue.

We don't normally allow "rewrites" of drivers, but if we do, yes, you would have to implement the old features if needed.

As you just seem to want to write an "example" block driver, no need to add the module option there, just do it right this time in how to properly configure things.

Quoted 2 lines

- Can we merge this so I can move forward at my current projected course, or should I plan on dealing with not having this available?

We generally do not want to merge apis without any real users, as it's hard to justify them, right? Also, we don't even know if they work properly or not.

thanks,

greg k-h

#10 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Andreas Hindborg
Quoted 4 lines · Andreas Hindborg

scheduled for removal. Interior mutability via SyncUnsafeCell provides the same functionality and it is my understanding that this feature is on track to be stabilized.

I am not sure about the last bit, but even if it is on track, we do not want to start using new language features or APIs that could potentially change.

And even if it is a feature that we are sure will not change, we should still let upstream Rust know before using it, since we are actively discussing with them the remaining unstable items that the kernel needs and they are putting the kernel in their roadmap.

So I suggest we mention it next week in the Rust/Rust for Linux meeting.

Quoted 2 lines

Not sure. val being null not supposed to happen in the current configuration. It should be an unreachable state. So BUG is the right thing?

Since you can easily return an error in this situation, I would say ideally a WARN_ON_ONCE + returning an error would be the best option, and covers you in case the rest changes and someone forgets to update this.

Quoted 2 lines

Not in the current configuration. The parameters can only be declared "read only". It should be impossible for anyone to call this function.

What I meant is, can you avoid writing the function to begin with, by leaving a null function pointer in the kernel_param_ops struct, i.e. None?

Thanks!

Cheers,
Miguel

maintainer
#35 lore raw
In reply to Andreas Hindborg
Quoted 6 lines · Andreas Hindborg

Hi Luis, Petr, Sami, Dani,

I just noticed that I failed in email and forgot to add you to the recipient list of this series. Do you want a resend, or is this sufficient?

If the patches didn't go to linux-modules, then yes, a resend would be good so that others subscribed to that list can also review. You can also wait for a v2. Up to you. I won't read it until I get it on my inbox.

Luis

author reviewer
#23 lore raw
In reply to Greg KH
Quoted 13 lines · Greg KH
Quoted 11 lines · Andreas Hindborg
Quoted 3 lines · Greg KH
Quoted 2 lines · Andreas Hindborg
Quoted 1 line · Greg KH

On Fri, Dec 13, 2024 at 12:30:45PM +0100, Andreas Hindborg wrote:

I'm not getting a clear reading on the following, perhaps you can clarify:

- Is the community aligned on dropping module parameters for all new drivers?
- If so, was this decided upon at some point or is this a fluid decision that is just manifesting now?

Quoted 4 lines

It's something that I've been saying in review comments of drivers for many many years now. Again, it was one of the main reasons we created configfs and sysfs all those decades ago, because module parameters just do not work properly for drivers in almost all cases.

Thinking a bit about this - are you sure there are no situations where module parameters is actually the right choice? I could imagine deeply embedded deployments, potentially with all required modules linked in to a static kernel image. It is probably desirable to be able to pass configuration to the modules via the kernel command line in this situation. If not for configuration in the field, then for a development situation.

Surely there are also situations in regular PC setups where it is desirable to pass configuration data to a module, so that it is available at load time. With configfs, module initialization becomes a two stage process of first loading and then configuring. That is not always what you would want.

Since module parameters actually do show up in sysfs as writable entries, when the right flags are passed, they do feel very similar to sysfs/configfs for simple use cases. What are the reasons they should not be usable for these simple use cases?

Quoted 14 lines
Quoted 3 lines

- Does this ban of module parameters also cover cases where backwards compatibility is desirable?

No, we don't break existing kernel features, but if you are writing a new driver, don't add them and then there's no compatibility issue.

We don't normally allow "rewrites" of drivers, but if we do, yes, you would have to implement the old features if needed.

As you just seem to want to write an "example" block driver, no need to add the module option there, just do it right this time in how to properly configure things.

Quoted 6 lines
Quoted 2 lines

- Can we merge this so I can move forward at my current projected course, or should I plan on dealing with not having this available?

We generally do not want to merge apis without any real users, as it's hard to justify them, right? Also, we don't even know if they work properly or not.

While null_blk is just a piece of testing infrastructure that you would not deploy in a production environment, it is still a "real user". I am sure we can agree on the importance of testing.

The exercise I am undertaking is to produce a drop in replacement of the existing C null_blk driver. If all goes well, we are considering to swap the C implementation for the Rust implementation in X number of years. Granted - a lot of things have to fall into place for that to happen, but that is the plan. This plan does not really work well if the two modules do not have the same interface.

I understand that you would like to phase out module parameters, but I don't think blocking their use from Rust is the right way to go about that task. If you really feel that module parameters have no place in new drivers, I would suggest that to be part of review process for each individual new driver - not at the stage of enabling module parameters for Rust in general.

Best regards,
Andreas Hindborg

#24 lore raw
Quoted 20 lines · Andreas Hindborg
Quoted 13 lines · Greg KH
Quoted 11 lines · Andreas Hindborg
Quoted 3 lines · Greg KH
Quoted 2 lines · Andreas Hindborg
Quoted 1 line · Greg KH

On Fri, Dec 13, 2024 at 12:30:45PM +0100, Andreas Hindborg wrote:

I'm not getting a clear reading on the following, perhaps you can clarify:

- Is the community aligned on dropping module parameters for all new drivers?
- If so, was this decided upon at some point or is this a fluid decision that is just manifesting now?

Quoted 4 lines

It's something that I've been saying in review comments of drivers for many many years now. Again, it was one of the main reasons we created configfs and sysfs all those decades ago, because module parameters just do not work properly for drivers in almost all cases.

Quoted 7 lines

Thinking a bit about this - are you sure there are no situations where module parameters is actually the right choice? I could imagine deeply embedded deployments, potentially with all required modules linked in to a static kernel image. It is probably desirable to be able to pass configuration to the modules via the kernel command line in this situation. If not for configuration in the field, then for a development situation.

I'm not saying "no situations at all", I'm saying "almost no situations need a module parameter, and almost NO driver needs one." That does not mean "none".

Quoted 5 lines

Surely there are also situations in regular PC setups where it is desirable to pass configuration data to a module, so that it is available at load time. With configfs, module initialization becomes a two stage process of first loading and then configuring. That is not always what you would want.

"regular" PC setups do not want module parameters either, they should all be dynamic busses and everything should "just work".

Quoted 4 lines

Since module parameters actually do show up in sysfs as writable entries, when the right flags are passed, they do feel very similar to sysfs/configfs for simple use cases. What are the reasons they should not be usable for these simple use cases?

Because they are almost never a good idea for a driver because drivers can control multiple devices and how do you show that in a single value?

For non-drivers, it's a different issue, but that's not what you are considering here :)

Quoted 24 lines
Quoted 13 lines
Quoted 2 lines

- Does this ban of module parameters also cover cases where backwards compatibility is desirable?

No, we don't break existing kernel features, but if you are writing a new driver, don't add them and then there's no compatibility issue.

We don't normally allow "rewrites" of drivers, but if we do, yes, you would have to implement the old features if needed.

As you just seem to want to write an "example" block driver, no need to add the module option there, just do it right this time in how to properly configure things.

Quoted 6 lines
Quoted 2 lines

- Can we merge this so I can move forward at my current projected course, or should I plan on dealing with not having this available?

We generally do not want to merge apis without any real users, as it's hard to justify them, right? Also, we don't even know if they work properly or not.

While null_blk is just a piece of testing infrastructure that you would not deploy in a production environment, it is still a "real user". I am sure we can agree on the importance of testing.

Quoted 6 lines

The exercise I am undertaking is to produce a drop in replacement of the existing C null_blk driver. If all goes well, we are considering to swap the C implementation for the Rust implementation in X number of years. Granted - a lot of things have to fall into place for that to happen, but that is the plan. This plan does not really work well if the two modules do not have the same interface.

Why do you have to have the same interface? Why not do it "properly" and make it use configfs that way you can have multiple devices and test them all at the same time?

As this is "just" a testing driver, there should not be any need to keep the same user/kernel api for setting things up, right?

Again, don't make the mistakes we have in the past, drivers should NOT be using module parameters.

Quoted 6 lines

I understand that you would like to phase out module parameters, but I don't think blocking their use from Rust is the right way to go about that task. If you really feel that module parameters have no place in new drivers, I would suggest that to be part of review process for each individual new driver - not at the stage of enabling module parameters for Rust in general.

I'm saying that module parameters do NOT belong in a driver, which is what you are wanting to do here. And as for adding new apis, please only do so when you have a real user, I don't see a real user for module parameters in rust just yet. If that changes, I'll reconsider my stance :)

thanks,

greg k-h

#25 lore raw
Quoted 11 lines · Greg KH
Quoted 6 lines · Andreas Hindborg

The exercise I am undertaking is to produce a drop in replacement of the existing C null_blk driver. If all goes well, we are considering to swap the C implementation for the Rust implementation in X number of years. Granted - a lot of things have to fall into place for that to happen, but that is the plan. This plan does not really work well if the two modules do not have the same interface.

Why do you have to have the same interface? Why not do it "properly" and make it use configfs that way you can have multiple devices and test them all at the same time?

Wait, null_blk already uses configfs, so just use that! I'd like to see the rust bindings for that api as that will be needed by lots of code.

thanks,

greg k-h

author reviewer
#27 lore raw
In reply to Greg KH
Quoted 4 lines · Greg KH
Quoted 3 lines · Andreas Hindborg
Quoted 1 line · Greg KH

On Fri, Dec 13, 2024 at 04:38:30PM +0100, Andreas Hindborg wrote:

[cut]

Quoted 19 lines
Quoted 12 lines
Quoted 7 lines
Quoted 3 lines

- Can we merge this so I can move forward at my current projected course, or should I plan on dealing with not having this available?

We generally do not want to merge apis without any real users, as it's hard to justify them, right? Also, we don't even know if they work properly or not.

While null_blk is just a piece of testing infrastructure that you would not deploy in a production environment, it is still a "real user". I am sure we can agree on the importance of testing.

Quoted 6 lines

The exercise I am undertaking is to produce a drop in replacement of the existing C null_blk driver. If all goes well, we are considering to swap the C implementation for the Rust implementation in X number of years. Granted - a lot of things have to fall into place for that to happen, but that is the plan. This plan does not really work well if the two modules do not have the same interface.

Quoted 6 lines

Why do you have to have the same interface? Why not do it "properly" and make it use configfs that way you can have multiple devices and test them all at the same time?

As this is "just" a testing driver, there should not be any need to keep the same user/kernel api for setting things up, right?

Because that would break all the users that use the old interface.

Quoted 11 lines

Again, don't make the mistakes we have in the past, drivers should NOT be using module parameters.

Quoted 6 lines

I understand that you would like to phase out module parameters, but I don't think blocking their use from Rust is the right way to go about that task. If you really feel that module parameters have no place in new drivers, I would suggest that to be part of review process for each individual new driver - not at the stage of enabling module parameters for Rust in general.

Quoted 4 lines

I'm saying that module parameters do NOT belong in a driver, which is what you are wanting to do here. And as for adding new apis, please only do so when you have a real user, I don't see a real user for module parameters in rust just yet. If that changes, I'll reconsider my stance :)

I guess we disagree about what is "real" and what is not.

In my view, null_blk is real, it is used by real people to do real work. They get real annoyed when the interface for their real tools change - thus making it more difficult to do this experiment.

Best regards,
Andreas Hindborg

author reviewer
#26 lore raw
In reply to Greg KH
Quoted 9 lines · Greg KH
Quoted 8 lines · Greg KH
Quoted 6 lines · Andreas Hindborg

The exercise I am undertaking is to produce a drop in replacement of the existing C null_blk driver. If all goes well, we are considering to swap the C implementation for the Rust implementation in X number of years. Granted - a lot of things have to fall into place for that to happen, but that is the plan. This plan does not really work well if the two modules do not have the same interface.

Quoted 6 lines
Quoted 3 lines

Why do you have to have the same interface? Why not do it "properly" and make it use configfs that way you can have multiple devices and test them all at the same time?

Wait, null_blk already uses configfs, so just use that! I'd like to see the rust bindings for that api as that will be needed by lots of code.

I intend to support both.

Best regards,
Andreas Hindborg

#28 lore raw
In reply to Andreas Hindborg
Quoted 12 lines · Andreas Hindborg
Quoted 11 lines
Quoted 6 lines

I understand that you would like to phase out module parameters, but I don't think blocking their use from Rust is the right way to go about that task. If you really feel that module parameters have no place in new drivers, I would suggest that to be part of review process for each individual new driver - not at the stage of enabling module parameters for Rust in general.

I'm saying that module parameters do NOT belong in a driver, which is what you are wanting to do here. And as for adding new apis, please only do so when you have a real user, I don't see a real user for module parameters in rust just yet. If that changes, I'll reconsider my stance :)

Quoted 5 lines

I guess we disagree about what is "real" and what is not.

In my view, null_blk is real, it is used by real people to do real work. They get real annoyed when the interface for their real tools change - thus making it more difficult to do this experiment.

I'd have to agree with that - yes, null_blk doesn't host any real applications, but it is the backbone of a lot of testing that blktests and others do. Hence it's very real in that sense, and the rust version of null_blk should provide and mimic how the C version works for ease of testing.

If this was a new driver where no prior art exists in terms of users and API, then I'd certainly agree with Greg. But that's not the case here.

Signature · 1 line
Jens Axboe
#29 lore raw
Quoted 19 lines · Jens Axboe
Quoted 12 lines · Andreas Hindborg
Quoted 11 lines
Quoted 6 lines

I understand that you would like to phase out module parameters, but I don't think blocking their use from Rust is the right way to go about that task. If you really feel that module parameters have no place in new drivers, I would suggest that to be part of review process for each individual new driver - not at the stage of enabling module parameters for Rust in general.

I'm saying that module parameters do NOT belong in a driver, which is what you are wanting to do here. And as for adding new apis, please only do so when you have a real user, I don't see a real user for module parameters in rust just yet. If that changes, I'll reconsider my stance :)

Quoted 5 lines

I guess we disagree about what is "real" and what is not.

In my view, null_blk is real, it is used by real people to do real work. They get real annoyed when the interface for their real tools change - thus making it more difficult to do this experiment.

Quoted 8 lines

I'd have to agree with that - yes, null_blk doesn't host any real applications, but it is the backbone of a lot of testing that blktests and others do. Hence it's very real in that sense, and the rust version of null_blk should provide and mimic how the C version works for ease of testing.

If this was a new driver where no prior art exists in terms of users and API, then I'd certainly agree with Greg. But that's not the case here.

Ok, so are you going to drop the C version and go with the rust version if it shows up? Surely you don't want duplicate drivers for the same thing in the tree, right?

thanks,

greg k-h

#30 lore raw
Quoted 26 lines · Greg KH
Quoted 25 lines · Jens Axboe
Quoted 12 lines · Andreas Hindborg
Quoted 11 lines
Quoted 6 lines

I understand that you would like to phase out module parameters, but I don't think blocking their use from Rust is the right way to go about that task. If you really feel that module parameters have no place in new drivers, I would suggest that to be part of review process for each individual new driver - not at the stage of enabling module parameters for Rust in general.

I'm saying that module parameters do NOT belong in a driver, which is what you are wanting to do here. And as for adding new apis, please only do so when you have a real user, I don't see a real user for module parameters in rust just yet. If that changes, I'll reconsider my stance :)

Quoted 5 lines

I guess we disagree about what is "real" and what is not.

In my view, null_blk is real, it is used by real people to do real work. They get real annoyed when the interface for their real tools change - thus making it more difficult to do this experiment.

I'd have to agree with that - yes, null_blk doesn't host any real applications, but it is the backbone of a lot of testing that blktests and others do. Hence it's very real in that sense, and the rust version of null_blk should provide and mimic how the C version works for ease of testing.

Quoted 6 lines
Quoted 2 lines

If this was a new driver where no prior art exists in terms of users and API, then I'd certainly agree with Greg. But that's not the case here.

Ok, so are you going to drop the C version and go with the rust version if it shows up? Surely you don't want duplicate drivers for the same thing in the tree, right?

Maybe at some point? The rust version is already there, it's just very limited compared to the C version so far. The point of the rust null_blk is to build out the API so that a real driver can get implemented as well. For now, I think the plan is to just have it be the rust playground on the block side.

Given that null_blk is the center piece of a lot of testing, it's not necessarily an issue to have duplicate implementations of the same driver. In fact it may be pretty useful for people coming from either side and wanting to compare implementations and how to do various things in either language. It's like an actually useful skeleton driver in that sense too.

Whether one or the other will be the only one in the tree in the future depends on a lot of other things that aren't necessarily driven or decided by the rust null_blk driver.

Signature · 1 line
Jens Axboe
#31 lore raw
Quoted 7 lines · Jens Axboe

Maybe at some point? The rust version is already there, it's just very limited compared to the C version so far. The point of the rust null_blk is to build out the API so that a real driver can get implemented as well. For now, I think the plan is to just have it be the rust playground on the block side.

Quoted 6 lines

Given that null_blk is the center piece of a lot of testing, it's not necessarily an issue to have duplicate implementations of the same driver. In fact it may be pretty useful for people coming from either side and wanting to compare implementations and how to do various things in either language. It's like an actually useful skeleton driver in that sense too.

Agreed. I would suggest to consider marking it as a Rust reference driver, since it is a prime candidate for it:

https://rust-for-linux.com/rust-reference-drivers

That way, it is clearer that the duplication is meant to build the abstractions and temporary in the long-term.

Then we can also easily track which ones are meant to be those, and Greg can get justifiably angry at you/us if the duplication isn't resolved when the right time comes... :)

Cheers,
Miguel

#32 lore raw
Quoted 4 lines · Miguel Ojeda

Agreed. I would suggest to consider marking it as a Rust reference driver, since it is a prime candidate for it:

Quoted 8 lines

https://rust-for-linux.com/rust-reference-drivers

That way, it is clearer that the duplication is meant to build the abstractions and temporary in the long-term.

Then we can also easily track which ones are meant to be those, and Greg can get justifiably angry at you/us if the duplication isn't resolved when the right time comes... :)

By the way, I half-jokingly suggested this elsewhere, but we could trivially allow module parameters only for particular modules, i.e. only allow to use the params key here if the name matches rnull (or if they set a special flag or whatever).

Yes, it is a hack, but it would give people pause when trying to use the feature, i.e. to think twice. And, to me, it makes sense to encode/acknowledge this kind of thing explicitly anyway.

So if that would unblock this and reduce the chance of repeating mistakes of the past, then we can easily do that too.

Cheers,
Miguel

#19 lore raw
In reply to Andreas Hindborg
Quoted 30 lines · Andreas Hindborg
Quoted 16 lines · Alice Ryhl
Quoted 15 lines · Andreas Hindborg
Quoted 11 lines · Greg KH
Quoted 1 line · Andreas Hindborg

This series extends the module! macro with support module parameters.

Eeek, why?

Module parameters are from the 1990's, back when we had no idea what we were doing and thought that a simple "one variable for a driver that controls multiple devices" was somehow a valid solution :)

Please only really add module parameters if you can prove that you actually need a module parameter.

Quoted 7 lines
Quoted 2 lines

I really need module parameters to make rust null block feature compatible with C null block.

Instead of providing module parameters to Rust code, you could implement that part of Rust nullblk in C. That way, you discourage future Rust drivers from using module parameters without making it impossible to have them in Rust nullblk.

If the opinion of the community is really to phase out module parameters for all new drivers (is it?), I can maybe move the code in question into the Rust null_blk driver.

Quoted 5 lines

I was kind of looking forward to having zero unsafe code in the driver though.

On the other hand, rust null block might not be the only "rewrite in rust and keep compatibility" project to ever see the light of day.

We still have tons of module parameters with the _unsafe annotations, because they're really convenient for debugging and testing. Yes they're hacks, yes you cannot use them for production, but they're useful.

So for drivers I'd say we definitely want to keep modules parameters around, they're just too convenient compared to debugfs, especially when it's things you have to set before the driver binds to the device. -Sima

Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#33 lore raw
In reply to Miguel Ojeda
Quoted 20 lines · Miguel Ojeda
Quoted 4 lines · Miguel Ojeda

Agreed. I would suggest to consider marking it as a Rust reference driver, since it is a prime candidate for it:

Quoted 8 lines

https://rust-for-linux.com/rust-reference-drivers

That way, it is clearer that the duplication is meant to build the abstractions and temporary in the long-term.

Then we can also easily track which ones are meant to be those, and Greg can get justifiably angry at you/us if the duplication isn't resolved when the right time comes... :)

By the way, I half-jokingly suggested this elsewhere, but we could trivially allow module parameters only for particular modules, i.e. only allow to use the params key here if the name matches rnull (or if they set a special flag or whatever).

Quoted 6 lines

Yes, it is a hack, but it would give people pause when trying to use the feature, i.e. to think twice. And, to me, it makes sense to encode/acknowledge this kind of thing explicitly anyway.

So if that would unblock this and reduce the chance of repeating mistakes of the past, then we can easily do that too.

This seems like a great idea. An allowlist of drivers that are allowed to use module parameters would encourage *new* drivers to not use them, and that allowlist can have a comment atop it saying "Only add your driver to this list if it needs to maintain an interface compatible with an existing driver in order to avoid breaking userspace. Otherwise, use configfs, sysfs, debugfs, or something else other than module parameters."

I wonder if we can implement such an allowlist for C modules, too. :)

author reviewer
#11 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Miguel Ojeda
Quoted 10 lines · Miguel Ojeda
Quoted 4 lines · Andreas Hindborg

scheduled for removal. Interior mutability via SyncUnsafeCell provides the same functionality and it is my understanding that this feature is on track to be stabilized.

I am not sure about the last bit, but even if it is on track, we do not want to start using new language features or APIs that could potentially change.

Quoted 6 lines

And even if it is a feature that we are sure will not change, we should still let upstream Rust know before using it, since we are actively discussing with them the remaining unstable items that the kernel needs and they are putting the kernel in their roadmap.

So I suggest we mention it next week in the Rust/Rust for Linux meeting.

I don't think we ever discussed this?

I was going to put this in the commit trailer for v4:

---
Version 3 of this patch enabled the unstable feature sync_unsafe_cell [1] to avoid static mut variables as suggested by Trevor Tross and Benno Lossin [2].

As we are moving closer to a new edition, it is now clear that static mut is not being deprecated in the 2024 edition as first anticipated [3].

Still, SyncUnsafeCell allows us to use safe code when referring to the parameter value:

```
{param_name}.as_mut_ptr().cast()
```

rather than unsafe code:

```
unsafe { addr_of_mut!(__{name}_{param_name}_value) }.cast()
```

Thus, this version (4) keeps the feature enabled.

[1] https://github.com/rust-lang/rust/issues/95439
[2] https://lore.kernel.org/all/CALNs47sqt==o+hM5M1b0vTayKH177naybg_KurcirXszYAa22A@mail.gmail.com/
[3] https://github.com/rust-lang/rust/issues/53639#issuecomment-2434023115
---

What do you think?

Quoted 8 lines
Quoted 3 lines

Not sure. val being null not supposed to happen in the current configuration. It should be an unreachable state. So BUG is the right thing?

Since you can easily return an error in this situation, I would say ideally a WARN_ON_ONCE + returning an error would be the best option, and covers you in case the rest changes and someone forgets to update this.

Returning an error and pr_warn! is doable. As far as I can tell, we do not have WARN_ON_ONCE yet?

Quoted 8 lines
Quoted 3 lines

Not in the current configuration. The parameters can only be declared "read only". It should be impossible for anyone to call this function.

What I meant is, can you avoid writing the function to begin with, by leaving a null function pointer in the kernel_param_ops struct, i.e. None?

It turns out we can!

Best regards,
Andreas Hindborg

reviewer
#12 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

Quoted 44 lines · Andreas Hindborg
Quoted 10 lines · Miguel Ojeda
Quoted 4 lines · Andreas Hindborg

scheduled for removal. Interior mutability via SyncUnsafeCell provides the same functionality and it is my understanding that this feature is on track to be stabilized.

I am not sure about the last bit, but even if it is on track, we do not want to start using new language features or APIs that could potentially change.

Quoted 6 lines

And even if it is a feature that we are sure will not change, we should still let upstream Rust know before using it, since we are actively discussing with them the remaining unstable items that the kernel needs and they are putting the kernel in their roadmap.

So I suggest we mention it next week in the Rust/Rust for Linux meeting.

I don't think we ever discussed this?

I was going to put this in the commit trailer for v4:

---
Version 3 of this patch enabled the unstable feature sync_unsafe_cell [1] to avoid static mut variables as suggested by Trevor Tross and Benno Lossin [2].

As we are moving closer to a new edition, it is now clear that static mut is not being deprecated in the 2024 edition as first anticipated [3].

Still, SyncUnsafeCell allows us to use safe code when referring to the parameter value:

```
{param_name}.as_mut_ptr().cast()
```

rather than unsafe code:

```
unsafe { addr_of_mut!(__{name}_{param_name}_value) }.cast()
```
Quoted 8 lines

Thus, this version (4) keeps the feature enabled.

[1] https://github.com/rust-lang/rust/issues/95439
[2] https://lore.kernel.org/all/CALNs47sqt==o+hM5M1b0vTayKH177naybg_KurcirXszYAa22A@mail.gmail.com/
[3] https://github.com/rust-lang/rust/issues/53639#issuecomment-2434023115
---

What do you think?

It's pretty easy to avoid needing this feature, so I don't think we should enable it.

Alice

#13 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

In reply to Andreas Hindborg
Quoted 2 lines · Andreas Hindborg

I don't think we ever discussed this?

I don't think so -- we discussed other things related to 2025H1 the last meeting. Perhaps you/we can bring it up in the next one?

Quoted 10 lines

Version 3 of this patch enabled the unstable feature sync_unsafe_cell [1] to avoid static mut variables as suggested by Trevor Tross and Benno Lossin [2].

As we are moving closer to a new edition, it is now clear that static mut is not being deprecated in the 2024 edition as first anticipated [3].

Still, SyncUnsafeCell allows us to use safe code when referring to the parameter value:

What do you think?

The justification seems fairly weak... Unless we are fairly confident the API will be stable (even if not stabilized right now), I am not sure why we would want to do this right now.

Can we provide our own SyncUnsafeCell instead in the meantime, if you want to keep the advantages?

Quoted 2 lines

Returning an error and pr_warn! is doable. As far as I can tell, we do not have WARN_ON_ONCE yet?

Please see https://lore.kernel.org/rust-for-linux/[email protected]/
in case it helps.

Quoted 1 line

It turns out we can!

That is what I expected :)

Thanks!

Cheers,
Miguel

author reviewer
#14 lore raw

Re: [PATCH v3 4/4] rust: add parameter support to the `module!` macro

Quoted 16 lines · Miguel Ojeda
Quoted 2 lines · Andreas Hindborg

I don't think we ever discussed this?

I don't think so -- we discussed other things related to 2025H1 the last meeting. Perhaps you/we can bring it up in the next one?

Quoted 8 lines

Version 3 of this patch enabled the unstable feature sync_unsafe_cell [1] to avoid static mut variables as suggested by Trevor Tross and Benno Lossin [2].

As we are moving closer to a new edition, it is now clear that static mut is not being deprecated in the 2024 edition as first anticipated [3].

Still, SyncUnsafeCell allows us to use safe code when referring to the parameter value:

Quoted 8 lines
Quoted 1 line

What do you think?

The justification seems fairly weak... Unless we are fairly confident the API will be stable (even if not stabilized right now), I am not sure why we would want to do this right now.

Can we provide our own SyncUnsafeCell instead in the meantime, if you want to keep the advantages?

I like having the shared ModuleParamAccess struct to encapsulate the access to the parameters rather than emitting an enum for every parameter instance. For that to work without unsafe code when the user accesses the parameter value, we need a non-mutable static that provides interior mutability.

I could implement Sync for ModuleParamAccess or I could add kernel::types::SyncUnsafeCell. Either way is fine for me.

Quoted 6 lines
Quoted 3 lines

Returning an error and pr_warn! is doable. As far as I can tell, we do not have WARN_ON_ONCE yet?

Please see https://lore.kernel.org/rust-for-linux/[email protected]/
in case it helps.

Cool, I can list that as a prerequisite.

Best regards,
Andreas Hindborg