rust: module_param: return value by copy from `value`
Subsystems (2)
Andreas Hindborg
reviewerPATCH 1/1 rust: module_param: return value by copy from `value`
For Copy parameter types it is more ergonomic to retrieve the parameter value by copy than through a shared reference. Change ModuleParamAccess::value to return T by copy when T: Copy, and rename the previous reference-returning accessor to value_ref. Update the in-tree caller in rust_minimal.
---
This change was suggested during review of the rust null block series [1].
[1] https://lore.kernel.org/r/[email protected]
--- rust/kernel/module_param.rs | 15 ++++++++++++++- samples/rust/rust_minimal.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-)
rust/kernel/module_param.rs +14 -1
@@ -132,9 +132,12 @@ pub const fn new(default: T) -> Self { }/// Get a shared reference to the parameter value. +/// +/// Returns a reference to the value supplied at module load time, or a +/// reference to the default value if the parameter has not been set. // Note: When sysfs access to parameters are enabled, we have to pass in a // held lock guard here. -pub fn value (&self) -> &T { +pub fn value_ref (&self) -> &T { self.value.as_ref ().unwrap_or (&self.default) }
@@ -146,6 +149,16 @@ pub const fn as_void_ptr(&self) -> *mut c_void { } } +impl <T :Copy >ModuleParamAccess <T > { +/// Get a copy of the parameter value. +/// +/// Returns the value supplied at module load time, or the default value +/// if the parameter has not been set. +pub fn value (&self) ->T { + self.value.copy ().unwrap_or (self.default) + } +} +#[doc(hidden)] /// Generate a static [`kernel_param_ops`](srctree/include/linux/moduleparam.h) struct. ///
samples/rust/rust_minimal.rs +1 -1
@@ -28,7 +28,7 @@ fn init(_module: &'static ThisModule) -> Result<Self> {pr_info! ("Am I built-in? {}\n" , !cfg! (MODULE ));pr_info! ("test_parameter: {}\n" , - *module_parameters::test_parameter.value () + module_parameters::test_parameter.value () );let mut numbers =KVec ::new ();
---
base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32
change-id: 20260529-modules-value-ref-e95a7ab94fdb
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing `#[inline]` annotations on small abstraction methods `value` and `value_ref` in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing #[inline] annotations on small abstraction methods value and value_ref in ModuleParamAccess. --
commit 6103e7f86e2528da9d35cfda1b1a7d24ab1f6516
Author: Andreas Hindborg <[email protected]>
rust: module_param: return value by copy from value
For Copy parameter types, this patch changes ModuleParamAccess::value to return by copy instead of by reference, improving ergonomics. It also renames the reference-returning accessor to value_ref and updates the caller in rust_minimal.
Quoted 5 lines
rust/kernel/module_param.rs +0
-0
rust/kernel/module_param.rs pub const fn new(default: T) -> Self { · line 140
Quoted 10 lines
rust/kernel/module_param.rs +4
-1@@ -132,9 +132,12 @@ pub const fn new(default: T) -> Self { }/// Get a shared reference to the parameter value. +/// +/// Returns a reference to the value supplied at module load time, or a +/// reference to the default value if the parameter has not been set. // Note: When sysfs access to parameters are enabled, we have to pass in a // held lock guard here. -pub fn value (&self) -> &T { +pub fn value_ref (&self) -> &T {
[Severity: Low]
This isn't a bug, but according to the Rust subsystem coding guidelines, functions in abstractions that are small or forward to binding calls should be annotated with #[inline]. Should value_ref() have an #[inline] annotation added here?
Quoted 2 lines
rust/kernel/module_param.rs +0
-0self.value.as_ref ().unwrap_or (&self.default) }
[ ... ]
rust/kernel/module_param.rs pub const fn as_void_ptr(&self) -> *mut c_void { · line 157
Quoted 10 lines
rust/kernel/module_param.rs +6
-0@@ -146,6 +149,16 @@ pub const fn as_void_ptr(&self) -> *mut c_void { } } +impl <T :Copy >ModuleParamAccess <T > { +/// Get a copy of the parameter value. +/// +/// Returns the value supplied at module load time, or the default value +/// if the parameter has not been set. +pub fn value (&self) ->T {
[Severity: Low]
This isn't a bug, but similar to the above, should this small abstraction method also be marked with #[inline] to allow compiler optimizations across crate boundaries?
Quoted 3 lines
rust/kernel/module_param.rs +3
-0+ self.value.copy ().unwrap_or (self.default) + } +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Gary Guo
reviewerQuoted 36 lines · Andreas Hindborg
For
Copyparameter types it is more ergonomic to retrieve the parameter value by copy than through a shared reference. ChangeModuleParamAccess::valueto returnTby copy whenT: Copy, and rename the previous reference-returning accessor tovalue_ref. Update the in-tree caller inrust_minimal.Suggested-by Alice Ryhl <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>---
This change was suggested during review of the rust null block series [1].[1] https://lore.kernel.org/r/[email protected]
--- rust/kernel/module_param.rs | 15 ++++++++++++++- samples/rust/rust_minimal.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-)rust/kernel/module_param.rs +4
-1@@ -132,9 +132,12 @@ pub const fn new(default: T) -> Self { }/// Get a shared reference to the parameter value. +/// +/// Returns a reference to the value supplied at module load time, or a +/// reference to the default value if the parameter has not been set. // Note: When sysfs access to parameters are enabled, we have to pass in a // held lock guard here. -pub fn value (&self) -> &T { +pub fn value_ref (&self) -> &T { self.value.as_ref ().unwrap_or (&self.default) }
rust/kernel/module_param.rs pub const fn as_void_ptr(&self) -> *mut c_void { · line 152
Quoted 4 lines
rust/kernel/module_param.rs +1
-0@@ -146,6 +149,16 @@ pub const fn as_void_ptr(&self) -> *mut c_void { } } +impl <T :Copy >ModuleParamAccess <T > {
Instead of adding a new impl block, this should just be moved to immediately after (or before) value_ref with a where T: Copy bound instead.
This makes related code close together for readers of the code.
Thanks,
Gary
Quoted 31 lines
rust/kernel/module_param.rs +9
-0+/// Get a copy of the parameter value. +/// +/// Returns the value supplied at module load time, or the default value +/// if the parameter has not been set. +pub fn value (&self) ->T { + self.value.copy ().unwrap_or (self.default) + } +} +#[doc(hidden)] /// Generate a static [`kernel_param_ops`](srctree/include/linux/moduleparam.h) struct. /// samples/rust/rust_minimal.rs +1
-2@@ -28,7 +28,7 @@ fn init(_module: &'static ThisModule) -> Result<Self> {pr_info! ("Am I built-in? {}\n" , !cfg! (MODULE ));pr_info! ("test_parameter: {}\n" , - *module_parameters::test_parameter.value () + module_parameters::test_parameter.value () );let mut numbers =KVec ::new (); ---base-commit:
7fd2df204f342fc17d1a0bfcd474b24232fb0f32
change-id: 20260529-modules-value-ref-e95a7ab94fdbBest regards,
Andreas Hindborg
reviewerQuoted 40 lines · Gary Guo
Quoted 39 lines · Andreas Hindborg
For
Copyparameter types it is more ergonomic to retrieve the parameter value by copy than through a shared reference. ChangeModuleParamAccess::valueto returnTby copy whenT: Copy, and rename the previous reference-returning accessor tovalue_ref. Update the in-tree caller inrust_minimal.Suggested-by Alice Ryhl <[email protected]>Signed-off-by Andreas Hindborg <[email protected]>---
This change was suggested during review of the rust null block series [1].[1] https://lore.kernel.org/r/[email protected]
--- rust/kernel/module_param.rs | 15 ++++++++++++++- samples/rust/rust_minimal.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-)rust/kernel/module_param.rs +4
-1@@ -132,9 +132,12 @@ pub const fn new(default: T) -> Self { }/// Get a shared reference to the parameter value. +/// +/// Returns a reference to the value supplied at module load time, or a +/// reference to the default value if the parameter has not been set. // Note: When sysfs access to parameters are enabled, we have to pass in a // held lock guard here. -pub fn value (&self) -> &T { +pub fn value_ref (&self) -> &T { self.value.as_ref ().unwrap_or (&self.default) }@@ -146,6 +149,16 @@ pub const fn as_void_ptr(&self) -> *mut c_void { } }
Quoted 6 lines
rust/kernel/module_param.rs pub const fn as_void_ptr(&self) -> *mut c_void { · line 152
Quoted 1 line
rust/kernel/module_param.rs +1
-0+impl <T :Copy >ModuleParamAccess <T > {Instead of adding a new impl block, this should just be moved to immediately after (or before)
value_refwith awhere T: Copybound instead.This makes related code close together for readers of the code.
Will do.
Best regards,
Andreas Hindborg