@@ -189,6 +189,11 @@ fn permissions_are_readonly(perms: &str) -> bool {
189
189
/// params: {
190
190
/// my_i32: i32 {
191
191
/// default: 42,
192
+ /// permissions: 0o000,
193
+ /// description: b"Example of i32",
194
+ /// },
195
+ /// writeable_i32: i32 {
196
+ /// default: 42,
192
197
/// permissions: 0o644,
193
198
/// description: b"Example of i32",
194
199
/// },
@@ -199,15 +204,15 @@ fn permissions_are_readonly(perms: &str) -> bool {
199
204
///
200
205
/// impl KernelModule for MyKernelModule {
201
206
/// fn init() -> KernelResult<Self> {
202
- /// // If `CONFIG_SYSFS` is enabled and the parameter is writeable, then
203
- /// // the kparam lock must be taken to read the parameter:
207
+ /// // If the parameter is writeable, then the kparam lock must be
208
+ /// // taken to read the parameter:
204
209
/// {
205
210
/// let lock = THIS_MODULE.kernel_param_lock();
206
- /// println!("i32 param is: {}", my_i32.nonlocking_read( ));
211
+ /// println!("i32 param is: {}", writeable_i32.read(&lock ));
207
212
/// }
208
- /// // If `CONFIG_SYSFS` is not enabled or the parameter is read only, it
209
- /// // can be read without locking the kernel parameters:
210
- /// println!("i32 param is: {}", my_i32.nonlocking_read ());
213
+ /// // If the parameter is read only, it can be read without locking
214
+ /// // the kernel parameters:
215
+ /// println!("i32 param is: {}", my_i32.read ());
211
216
/// Ok(MyKernelModule)
212
217
/// }
213
218
/// }
@@ -305,10 +310,10 @@ pub fn module(ts: TokenStream) -> TokenStream {
305
310
"str" => format ! ( "b\" {}\0 \" as *const _ as *mut kernel::c_types::c_char" , param_default) ,
306
311
_ => param_default,
307
312
} ;
308
- let locking_read_func = match param_type. as_ref ( ) {
309
- "str" => format ! (
313
+ let read_func = match ( param_type. as_ref ( ) , permissions_are_readonly ( & param_permissions ) ) {
314
+ ( "str" , false ) => format ! (
310
315
"
311
- fn locking_read <'par: 'val, 'lck: 'val, 'val>(&'par self, lock: &'lck kernel::KParamGuard) -> &'val [u8] {{
316
+ fn read <'par: 'val, 'lck: 'val, 'val>(&'par self, lock: &'lck kernel::KParamGuard) -> &'val [u8] {{
312
317
// SAFETY: The pointer is provided either in `param_default` when building the module,
313
318
// or by the kernel through `param_set_charp`. Both will be valid C strings.
314
319
// Parameters are locked by `KParamGuard`.
@@ -320,22 +325,9 @@ pub fn module(ts: TokenStream) -> TokenStream {
320
325
name = name,
321
326
param_name = param_name,
322
327
) ,
323
- _ => format ! (
324
- "
325
- // SAFETY: Parameters are locked by `KParamGuard`.
326
- fn locking_read<'par: 'val, 'lck: 'val, 'val>(&'par self, lock: &'lck kernel::KParamGuard) -> &'val {param_type_internal} {{
327
- unsafe {{ &__{name}_{param_name}_value }}
328
- }}
329
- " ,
330
- name = name,
331
- param_name = param_name,
332
- param_type_internal = param_type_internal,
333
- ) ,
334
- } ;
335
- let nonlocking_read_func = match param_type. as_ref ( ) {
336
- "str" => format ! (
328
+ ( "str" , true ) => format ! (
337
329
"
338
- fn nonlocking_read (&self) -> &[u8] {{
330
+ fn read (&self) -> &[u8] {{
339
331
// SAFETY: The pointer is provided either in `param_default` when building the module,
340
332
// or by the kernel through `param_set_charp`. Both will be valid C strings.
341
333
// Parameters do not need to be locked because they are read only or sysfs is not enabled.
@@ -347,37 +339,28 @@ pub fn module(ts: TokenStream) -> TokenStream {
347
339
name = name,
348
340
param_name = param_name,
349
341
) ,
350
- _ => format ! (
342
+ ( _ , false ) => format ! (
351
343
"
352
- // SAFETY: Parameters do not need to be locked because they are read only or sysfs is not enabled .
353
- fn nonlocking_read(& self) -> &{param_type_internal} {{
344
+ // SAFETY: Parameters are locked by `KParamGuard` .
345
+ fn read<'par: 'val, 'lck: 'val, 'val>(&'par self, lock: &'lck kernel::KParamGuard ) -> &'val {param_type_internal} {{
354
346
unsafe {{ &__{name}_{param_name}_value }}
355
347
}}
356
348
" ,
357
349
name = name,
358
350
param_name = param_name,
359
351
param_type_internal = param_type_internal,
360
352
) ,
361
- } ;
362
- let read_func = if permissions_are_readonly ( & param_permissions) {
363
- format ! (
353
+ ( _, true ) => format ! (
364
354
"
365
- {locking_read_func}
366
- {nonlocking_read_func}
367
- " ,
368
- locking_read_func = locking_read_func,
369
- nonlocking_read_func = nonlocking_read_func
370
- )
371
- } else {
372
- format ! (
373
- "
374
- {locking_read_func}
375
- #[cfg(not(CONFIG_SYSFS))]
376
- {nonlocking_read_func}
355
+ // SAFETY: Parameters do not need to be locked because they are read only or sysfs is not enabled.
356
+ fn read(&self) -> &{param_type_internal} {{
357
+ unsafe {{ &__{name}_{param_name}_value }}
358
+ }}
377
359
" ,
378
- locking_read_func = locking_read_func,
379
- nonlocking_read_func = nonlocking_read_func,
380
- )
360
+ name = name,
361
+ param_name = param_name,
362
+ param_type_internal = param_type_internal,
363
+ ) ,
381
364
} ;
382
365
let kparam = format ! (
383
366
"
0 commit comments