Skip to content

Commit bb8feca

Browse files
committed
cleanup
1 parent 2e1d96b commit bb8feca

File tree

8 files changed

+50
-36
lines changed

8 files changed

+50
-36
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ jobs:
99
timeout-minutes: 20
1010

1111
strategy:
12-
fail-fast: false
1312
matrix:
1413
arch: [x86_64, arm64]
1514
toolchain: [gcc, clang, llvm]
@@ -170,10 +169,10 @@ jobs:
170169
- run: "grep -F '] my_invbool: false' qemu-stdout.log"
171170
- run: "grep -F '] [2] my_invbool: true' qemu-stdout.log"
172171

173-
- run: "grep '\\] my_str: Ok(\"🦀mod\")\\s*$' qemu-stdout.log"
174-
- run: "grep '\\] \\[2\\] my_str: Ok(\"default str val\")\\s*$' qemu-stdout.log"
175-
- run: "grep '\\] \\[3\\] my_str: Ok(\"🦀mod\")\\s*$' qemu-stdout.log"
176-
- run: "grep '\\] \\[4\\] my_str: Ok(\"default str val\")\\s*$' qemu-stdout.log"
172+
- run: "grep '\\] my_str: 🦀mod\\s*$' qemu-stdout.log"
173+
- run: "grep '\\] \\[2\\] my_str: default str val\\s*$' qemu-stdout.log"
174+
- run: "grep '\\] \\[3\\] my_str: 🦀mod\\s*$' qemu-stdout.log"
175+
- run: "grep '\\] \\[4\\] my_str: default str val\\s*$' qemu-stdout.log"
177176

178177
- run: grep -F '] [3] Rust Example (exit)' qemu-stdout.log
179178
- run: grep -F '] [4] Rust Example (exit)' qemu-stdout.log

drivers/char/rust_example.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,37 @@ struct RustExample {
5656
}
5757

5858
#[cfg(CONFIG_SYSFS)]
59-
fn print_params() {
59+
fn print_params() -> KernelResult<()> {
6060
let lock = THIS_MODULE.kernel_param_lock();
6161
println!("Parameters:");
6262
println!(" my_bool: {}", my_bool.nonlocking_read());
6363
println!(" my_i32: {}", my_i32.locking_read(&lock));
6464
println!(
65-
" my_str: {:?}",
66-
core::str::from_utf8(my_str.locking_read(&lock))
65+
" my_str: {}",
66+
core::str::from_utf8(my_str.locking_read(&lock))?
6767
);
6868
println!(" my_invbool: {}", my_invbool.locking_read(&lock));
69+
Ok(())
6970
}
7071

7172
#[cfg(not(CONFIG_SYSFS))]
72-
fn print_params() {
73+
fn print_params() -> KernelResult<()> {
7374
println!("Parameters:");
7475
println!(" my_bool: {}", my_bool.nonlocking_read());
7576
println!(" my_i32: {}", my_i32.nonlocking_read());
7677
println!(
77-
" my_str: {:?}",
78-
core::str::from_utf8(my_str.nonlocking_read())
78+
" my_str: {}",
79+
core::str::from_utf8(my_str.nonlocking_read())?
7980
);
8081
println!(" my_invbool: {}", my_invbool.nonlocking_read());
82+
Ok(())
8183
}
8284

8385
impl KernelModule for RustExample {
8486
fn init() -> KernelResult<Self> {
8587
println!("Rust Example (init)");
8688
println!("Am I built-in? {}", !cfg!(MODULE));
87-
print_params();
89+
print_params()?;
8890

8991
// Including this large variable on the stack will trigger
9092
// stack probing on the supported archs.

drivers/char/rust_example_2.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@ struct RustExample2 {
4141
}
4242

4343
#[cfg(CONFIG_SYSFS)]
44-
fn print_params() {
44+
fn print_params() -> KernelResult<()> {
4545
let lock = THIS_MODULE.kernel_param_lock();
4646
println!("[2] Parameters:");
4747
println!("[2] my_bool: {}", my_bool.nonlocking_read());
4848
println!("[2] my_i32: {}", my_i32.locking_read(&lock));
4949
println!(
50-
"[2] my_str: {:?}",
51-
core::str::from_utf8(my_str.locking_read(&lock))
50+
"[2] my_str: {}",
51+
core::str::from_utf8(my_str.locking_read(&lock))?
5252
);
5353
println!("[2] my_invbool: {}", my_invbool.locking_read(&lock));
54+
Ok(())
5455
}
5556

5657
#[cfg(not(CONFIG_SYSFS))]
57-
fn print_params() {
58+
fn print_params() -> KernelResult<()> {
5859
println!("[2] Parameters:");
5960
println!("[2] my_bool: {}", my_bool.nonlocking_read());
6061
println!("[2] my_i32: {}", my_i32.nonlocking_read());
6162
println!(
62-
"[2] my_str: {:?}",
63-
core::str::from_utf8(my_str.nonlocking_read())
63+
"[2] my_str: {}",
64+
core::str::from_utf8(my_str.nonlocking_read())?
6465
);
6566
println!("[2] my_invbool: {}", my_invbool.nonlocking_read());
67+
Ok(())
6668
}
6769

6870
impl KernelModule for RustExample2 {
6971
fn init() -> KernelResult<Self> {
7072
println!("[2] Rust Example (init)");
7173
println!("[2] Am I built-in? {}", !cfg!(MODULE));
72-
print_params();
74+
print_params()?;
7375

7476
// Including this large variable on the stack will trigger
7577
// stack probing on the supported archs.

drivers/char/rust_example_3.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@ struct RustExample3 {
4141
}
4242

4343
#[cfg(CONFIG_SYSFS)]
44-
fn print_params() {
44+
fn print_params() -> KernelResult<()> {
4545
let lock = THIS_MODULE.kernel_param_lock();
4646
println!("[3] Parameters:");
4747
println!("[3] my_bool: {}", my_bool.nonlocking_read());
4848
println!("[3] my_i32: {}", my_i32.locking_read(&lock));
4949
println!(
50-
"[3] my_str: {:?}",
51-
core::str::from_utf8(my_str.locking_read(&lock))
50+
"[3] my_str: {}",
51+
core::str::from_utf8(my_str.locking_read(&lock))?
5252
);
5353
println!("[3] my_invbool: {}", my_invbool.locking_read(&lock));
54+
Ok(())
5455
}
5556

5657
#[cfg(not(CONFIG_SYSFS))]
57-
fn print_params() {
58+
fn print_params() -> KernelResult<()> {
5859
println!("[3] Parameters:");
5960
println!("[3] my_bool: {}", my_bool.nonlocking_read());
6061
println!("[3] my_i32: {}", my_i32.nonlocking_read());
6162
println!(
62-
"[3] my_str: {:?}",
63-
core::str::from_utf8(my_str.nonlocking_read())
63+
"[3] my_str: {}",
64+
core::str::from_utf8(my_str.nonlocking_read())?
6465
);
6566
println!("[3] my_invbool: {}", my_invbool.nonlocking_read());
67+
Ok(())
6668
}
6769

6870
impl KernelModule for RustExample3 {
6971
fn init() -> KernelResult<Self> {
7072
println!("[3] Rust Example (init)");
7173
println!("[3] Am I built-in? {}", !cfg!(MODULE));
72-
print_params();
74+
print_params()?;
7375

7476
// Including this large variable on the stack will trigger
7577
// stack probing on the supported archs.

drivers/char/rust_example_4.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@ struct RustExample4 {
4141
}
4242

4343
#[cfg(CONFIG_SYSFS)]
44-
fn print_params() {
44+
fn print_params() -> KernelResult<()> {
4545
let lock = THIS_MODULE.kernel_param_lock();
4646
println!("[4] Parameters:");
4747
println!("[4] my_bool: {}", my_bool.nonlocking_read());
4848
println!("[4] my_i32: {}", my_i32.locking_read(&lock));
4949
println!(
50-
"[4] my_str: {:?}",
51-
core::str::from_utf8(my_str.locking_read(&lock))
50+
"[4] my_str: {}",
51+
core::str::from_utf8(my_str.locking_read(&lock))?
5252
);
5353
println!("[4] my_invbool: {}", my_invbool.locking_read(&lock));
54+
Ok(())
5455
}
5556

5657
#[cfg(not(CONFIG_SYSFS))]
57-
fn print_params() {
58+
fn print_params() -> KernelResult<()> {
5859
println!("[4] Parameters:");
5960
println!("[4] my_bool: {}", my_bool.nonlocking_read());
6061
println!("[4] my_i32: {}", my_i32.nonlocking_read());
6162
println!(
62-
"[4] my_str: {:?}",
63-
core::str::from_utf8(my_str.nonlocking_read())
63+
"[4] my_str: {}",
64+
core::str::from_utf8(my_str.nonlocking_read())?
6465
);
6566
println!("[4] my_invbool: {}", my_invbool.nonlocking_read());
67+
Ok(())
6668
}
6769

6870
impl KernelModule for RustExample4 {
6971
fn init() -> KernelResult<Self> {
7072
println!("[4] Rust Example (init)");
7173
println!("[4] Am I built-in? {}", !cfg!(MODULE));
72-
print_params();
74+
print_params()?;
7375

7476
// Including this large variable on the stack will trigger
7577
// stack probing on the supported archs.

rust/kernel/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use core::num::TryFromIntError;
4+
use core::str::Utf8Error;
45

56
use alloc::alloc::AllocError;
67

@@ -31,6 +32,12 @@ impl From<TryFromIntError> for Error {
3132
}
3233
}
3334

35+
impl From<Utf8Error> for Error {
36+
fn from(_: Utf8Error) -> Error {
37+
Error::EINVAL
38+
}
39+
}
40+
3441
pub type KernelResult<T> = Result<T, Error>;
3542

3643
impl From<AllocError> for Error {

rust/kernel/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ impl ThisModule {
6666

6767
/// Scoped lock on the kernel parameters of `ThisModule`. Lock will be released
6868
/// when this struct is dropped.
69-
#[cfg(CONFIG_SYSFS)]
7069
pub struct KParamGuard<'a> {
7170
this_module: &'a ThisModule
7271
}

rust/module.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn permissions_are_readonly(perms: &str) -> bool {
199199
///
200200
/// impl KernelModule for MyKernelModule {
201201
/// fn init() -> KernelResult<Self> {
202-
/// // If `CONFIG_SYSFS` is enabled and the parameter is writeable, it must
202+
/// // If `CONFIG_SYSFS` is enabled and the parameter is writeable, then
203203
/// // the kparam lock must be taken to read the parameter:
204204
/// {
205205
/// let lock = THIS_MODULE.kernel_param_lock();
@@ -362,14 +362,15 @@ pub fn module(ts: TokenStream) -> TokenStream {
362362
let read_func = if permissions_are_readonly(&param_permissions) {
363363
format!(
364364
"
365+
{locking_read_func}
365366
{nonlocking_read_func}
366367
",
368+
locking_read_func = locking_read_func,
367369
nonlocking_read_func = nonlocking_read_func
368370
)
369371
} else {
370372
format!(
371373
"
372-
#[cfg(CONFIG_SYSFS)]
373374
{locking_read_func}
374375
#[cfg(not(CONFIG_SYSFS))]
375376
{nonlocking_read_func}

0 commit comments

Comments
 (0)