Skip to content

Commit 6bc199b

Browse files
committed
rust: CStr overhaul
`CStr` is overhauled to make using it more similar to use a `str`. Signed-off-by: Gary Guo <[email protected]>
1 parent 8687614 commit 6bc199b

19 files changed

+282
-104
lines changed

drivers/android/rust_binder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use alloc::{boxed::Box, sync::Arc};
1111
use core::pin::Pin;
1212
use kernel::{
13-
cstr,
13+
c_str,
1414
io_buffer::IoBufferWriter,
1515
linked_list::{GetLinks, GetLinksWrapped, Links},
1616
miscdev::Registration,
@@ -111,7 +111,7 @@ impl KernelModule for BinderModule {
111111
let pinned_ctx = Context::new()?;
112112
let ctx = unsafe { Pin::into_inner_unchecked(pinned_ctx) };
113113
let reg = Registration::<Arc<Context>>::new_pinned::<process::Process>(
114-
cstr!("rust_binder"),
114+
c_str!("rust_binder"),
115115
None,
116116
ctx,
117117
)?;

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use alloc::boxed::Box;
99
use core::pin::Pin;
1010
use kernel::prelude::*;
11-
use kernel::{cstr, platdev};
11+
use kernel::{c_str, platdev};
1212

1313
module! {
1414
type: RngModule,
@@ -24,7 +24,7 @@ struct RngModule {
2424

2525
impl KernelModule for RngModule {
2626
fn init() -> Result<Self> {
27-
let pdev = platdev::Registration::new_pinned(cstr!("bcm2835-rng-rust"), &THIS_MODULE)?;
27+
let pdev = platdev::Registration::new_pinned(c_str!("bcm2835-rng-rust"), &THIS_MODULE)?;
2828

2929
Ok(RngModule { _pdev: pdev })
3030
}

rust/kernel/c_types.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,3 @@ mod c {
117117
}
118118

119119
pub use c::*;
120-
121-
/// Reads string until null byte is reached and returns slice excluding the
122-
/// terminating null.
123-
///
124-
/// # Safety
125-
///
126-
/// The data from the pointer until the null terminator must be valid for reads
127-
/// and not mutated for all of `'a`. The length of the string must also be less
128-
/// than `isize::MAX`. See the documentation on
129-
/// [`core::slice::from_raw_parts()`] for further details on safety of
130-
/// converting a pointer to a slice.
131-
pub unsafe fn c_string_bytes<'a>(ptr: *const crate::c_types::c_char) -> &'a [u8] {
132-
let length = crate::bindings::strlen(ptr) as usize;
133-
&core::slice::from_raw_parts(ptr as *const u8, length)
134-
}

rust/kernel/chrdev.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::bindings;
1717
use crate::c_types;
1818
use crate::error::{Error, Result};
1919
use crate::file_operations;
20-
use crate::types::CStr;
20+
use crate::str::CStr;
2121

2222
/// Character device.
2323
///
@@ -87,7 +87,7 @@ struct RegistrationInner<const N: usize> {
8787
///
8888
/// May contain up to a fixed number (`N`) of devices. Must be pinned.
8989
pub struct Registration<const N: usize> {
90-
name: CStr<'static>,
90+
name: &'static CStr,
9191
minors_start: u16,
9292
this_module: &'static crate::ThisModule,
9393
inner: Option<RegistrationInner<N>>,
@@ -104,7 +104,7 @@ impl<const N: usize> Registration<{ N }> {
104104
/// are going to pin the registration right away, call
105105
/// [`Self::new_pinned()`] instead.
106106
pub fn new(
107-
name: CStr<'static>,
107+
name: &'static CStr,
108108
minors_start: u16,
109109
this_module: &'static crate::ThisModule,
110110
) -> Self {
@@ -120,7 +120,7 @@ impl<const N: usize> Registration<{ N }> {
120120
///
121121
/// This does *not* register the device: see [`Self::register()`].
122122
pub fn new_pinned(
123-
name: CStr<'static>,
123+
name: &'static CStr,
124124
minors_start: u16,
125125
this_module: &'static crate::ThisModule,
126126
) -> Result<Pin<Box<Self>>> {
@@ -146,7 +146,7 @@ impl<const N: usize> Registration<{ N }> {
146146
&mut dev,
147147
this.minors_start.into(),
148148
N.try_into()?,
149-
this.name.as_ptr() as *const c_types::c_char,
149+
this.name.as_char_ptr(),
150150
)
151151
};
152152
if res != 0 {

rust/kernel/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const_fn,
2020
const_mut_refs,
2121
const_panic,
22+
const_raw_ptr_deref,
2223
try_reserve
2324
)]
2425
#![deny(clippy::complexity)]
@@ -44,6 +45,7 @@ pub mod file;
4445
pub mod file_operations;
4546
pub mod miscdev;
4647
pub mod pages;
48+
pub mod str;
4749

4850
pub mod linked_list;
4951
mod raw_list;
@@ -67,7 +69,7 @@ mod types;
6769
pub mod user_ptr;
6870

6971
pub use crate::error::{Error, Result};
70-
pub use crate::types::{CStr, Mode};
72+
pub use crate::types::Mode;
7173

7274
/// Page size defined in terms of the `PAGE_SHIFT` macro from C.
7375
///

rust/kernel/miscdev.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
//!
77
//! Reference: <https://www.kernel.org/doc/html/latest/driver-api/misc_devices.html>
88
9+
use crate::bindings;
910
use crate::error::{Error, Result};
1011
use crate::file_operations::{FileOpenAdapter, FileOpener, FileOperationsVtable};
11-
use crate::{bindings, c_types, CStr};
12+
use crate::str::CStr;
1213
use alloc::boxed::Box;
1314
use core::marker::PhantomPinned;
1415
use core::pin::Pin;
@@ -41,7 +42,7 @@ impl<T: Sync> Registration<T> {
4142
///
4243
/// Returns a pinned heap-allocated representation of the registration.
4344
pub fn new_pinned<F: FileOpener<T>>(
44-
name: CStr<'static>,
45+
name: &'static CStr,
4546
minor: Option<i32>,
4647
context: T,
4748
) -> Result<Pin<Box<Self>>> {
@@ -56,7 +57,7 @@ impl<T: Sync> Registration<T> {
5657
/// self-referential. If a minor is not given, the kernel allocates a new one if possible.
5758
pub fn register<F: FileOpener<T>>(
5859
self: Pin<&mut Self>,
59-
name: CStr<'static>,
60+
name: &'static CStr,
6061
minor: Option<i32>,
6162
) -> Result {
6263
// SAFETY: We must ensure that we never move out of `this`.
@@ -68,7 +69,7 @@ impl<T: Sync> Registration<T> {
6869

6970
// SAFETY: The adapter is compatible with `misc_register`.
7071
this.mdev.fops = unsafe { FileOperationsVtable::<Self, F>::build() };
71-
this.mdev.name = name.as_ptr() as *const c_types::c_char;
72+
this.mdev.name = name.as_char_ptr();
7273
this.mdev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32);
7374

7475
let ret = unsafe { bindings::misc_register(&mut this.mdev) };

rust/kernel/module_param.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! C header: [`include/linux/moduleparam.h`](../../../include/linux/moduleparam.h)
66
7+
use crate::str::CStr;
78
use core::fmt::Write;
89

910
/// Types that can be used for module parameters.
@@ -70,7 +71,7 @@ pub trait ModuleParam: core::fmt::Display + core::marker::Sized {
7071
let arg = if val.is_null() {
7172
None
7273
} else {
73-
Some(crate::c_types::c_string_bytes(val))
74+
Some(CStr::from_char_ptr(val).as_bytes())
7475
};
7576
match Self::try_from_param_arg(arg) {
7677
Some(new_value) => {

rust/kernel/platdev.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use crate::{
1010
bindings, c_types,
1111
error::{Error, Result},
12-
pr_info, CStr,
12+
pr_info,
13+
str::CStr,
1314
};
1415
use alloc::boxed::Box;
1516
use core::{marker::PhantomPinned, pin::Pin};
@@ -39,7 +40,7 @@ extern "C" fn remove_callback(_pdev: *mut bindings::platform_device) -> c_types:
3940
impl Registration {
4041
fn register(
4142
self: Pin<&mut Self>,
42-
name: CStr<'static>,
43+
name: &'static CStr,
4344
module: &'static crate::ThisModule,
4445
) -> Result {
4546
// SAFETY: We must ensure that we never move out of `this`.
@@ -48,7 +49,7 @@ impl Registration {
4849
// Already registered.
4950
return Err(Error::EINVAL);
5051
}
51-
this.pdrv.driver.name = name.as_ptr() as *const c_types::c_char;
52+
this.pdrv.driver.name = name.as_char_ptr();
5253
this.pdrv.probe = Some(probe_callback);
5354
this.pdrv.remove = Some(remove_callback);
5455
// SAFETY:
@@ -68,7 +69,7 @@ impl Registration {
6869
///
6970
/// Returns a pinned heap-allocated representation of the registration.
7071
pub fn new_pinned(
71-
name: CStr<'static>,
72+
name: &'static CStr,
7273
module: &'static crate::ThisModule,
7374
) -> Result<Pin<Box<Self>>> {
7475
let mut r = Pin::from(Box::try_new(Self::default())?);

0 commit comments

Comments
 (0)