Skip to contentSkip to conversation

rust: module_param: return copy from value() for Copy types

PATCH In review Andreas Hindborg 3 participants 4 messages Mar 23

Subsystems (2)
lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH 1/1 rust: module_param: return copy from value() for Copy types

Rename the existing value() method to value_ref() which returns a shared reference to the parameter value, and add a new value() method on ModuleParamAccess<T> where T: Copy that returns the value by copy.

This provides a more ergonomic API for the common case where the parameter type implements Copy, avoiding the need to explicitly dereference the return value at call sites.

Currently value_ref() has no in-tree callers, but it will be needed when support for non-Copy parameter types such as arrays and strings is added.

---
This change was suggested at [1].

---
 rust/kernel/module_param.rs  | 11 ++++++++++-
 samples/rust/rust_minimal.rs |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)
rust/kernel/module_param.rs +10 -1
index 6a8a7a875643..5dcfe2ba87a1 100644
@@ -134,7 +134,7 @@ pub const fn new(default: T) -> Self {
     /// Get a shared reference to the parameter value.
     // 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 +146,15 @@ pub const fn as_void_ptr(&self) -> *mut c_void {
     }
 }
 
+impl<T: Copy> ModuleParamAccess<T> {
+    /// Get a copy of the parameter value.
+    // Note: When sysfs access to parameters are enabled, we have to pass in a
+    // held lock guard here.
+    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
index 8eb9583571d7..60d03df6cd80 100644
@@ -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: c369299895a591d96745d6492d4888259b004a9e
change-id: 20260323-module-value-ref-5884b5ae6b2a

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andreas Hindborg

Rename the existing value() method to value_ref() which returns a shared reference to the parameter value, and add a new value() method on ModuleParamAccess<T> where T: Copy that returns the value by copy.

This provides a more ergonomic API for the common case where the parameter type implements Copy, avoiding the need to explicitly dereference the return value at call sites.

Quoted 5 lines

Currently value_ref() has no in-tree callers, but it will be needed when support for non-Copy parameter types such as arrays and strings is added.

reviewer
#2 lore raw
In reply to Andreas Hindborg
Quoted 37 lines · Andreas Hindborg

Rename the existing value() method to value_ref() which returns a shared reference to the parameter value, and add a new value() method on ModuleParamAccess<T> where T: Copy that returns the value by copy.

This provides a more ergonomic API for the common case where the parameter type implements Copy, avoiding the need to explicitly dereference the return value at call sites.

Currently value_ref() has no in-tree callers, but it will be needed when support for non-Copy parameter types such as arrays and strings is added.

---
This change was suggested at [1].

---
 rust/kernel/module_param.rs  | 11 ++++++++++-
 samples/rust/rust_minimal.rs |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)
rust/kernel/module_param.rs +1 -1
index 6a8a7a875643..5dcfe2ba87a1 100644
@@ -134,7 +134,7 @@ pub const fn new(default: T) -> Self {
     /// Get a shared reference to the parameter value.
     // 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 153

Quoted 8 lines
rust/kernel/module_param.rs +5 -0
@@ -146,6 +146,15 @@ pub const fn as_void_ptr(&self) -> *mut c_void {
     }
 }
 
+impl<T: Copy> ModuleParamAccess<T> {
+    /// Get a copy of the parameter value.
+    // Note: When sysfs access to parameters are enabled, we have to pass in a
+    // held lock guard here.
+    pub fn value(&self) -> T {

It's better to keep this close to value_ref in the same impl block. The `T:
Copy` bound doesn't need to be on the impl block, it can be on the item itself with

    pub fn value(&self) -> T where T: Copy

Best,
Gary

Quoted 26 lines
rust/kernel/module_param.rs +4 -0
+        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
index 8eb9583571d7..60d03df6cd80 100644
@@ -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: c369299895a591d96745d6492d4888259b004a9e
change-id: 20260323-module-value-ref-5884b5ae6b2a

Best regards,

author reviewer
#3 lore raw
Quoted 51 lines · Gary Guo
Quoted 37 lines · Andreas Hindborg

Rename the existing value() method to value_ref() which returns a shared reference to the parameter value, and add a new value() method on ModuleParamAccess<T> where T: Copy that returns the value by copy.

This provides a more ergonomic API for the common case where the parameter type implements Copy, avoiding the need to explicitly dereference the return value at call sites.

Currently value_ref() has no in-tree callers, but it will be needed when support for non-Copy parameter types such as arrays and strings is added.

---
This change was suggested at [1].

---
 rust/kernel/module_param.rs  | 11 ++++++++++-
 samples/rust/rust_minimal.rs |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)
rust/kernel/module_param.rs +1 -1
index 6a8a7a875643..5dcfe2ba87a1 100644
@@ -134,7 +134,7 @@ pub const fn new(default: T) -> Self {
     /// Get a shared reference to the parameter value.
     // 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 153

Quoted 8 lines
rust/kernel/module_param.rs +5 -0
@@ -146,6 +146,15 @@ pub const fn as_void_ptr(&self) -> *mut c_void {
     }
 }
 
+impl<T: Copy> ModuleParamAccess<T> {
+    /// Get a copy of the parameter value.
+    // Note: When sysfs access to parameters are enabled, we have to pass in a
+    // held lock guard here.
+    pub fn value(&self) -> T {

It's better to keep this close to value_ref in the same impl block. The `T:
Copy` bound doesn't need to be on the impl block, it can be on the item itself with

Quoted 1 line
    pub fn value(&self) -> T where T: Copy

Cool, I'll do that.

Best regards,
Andreas Hindborg