Skip to content

Commit faa3cbc

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 f7c1078 commit faa3cbc

20 files changed

+284
-107
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
@@ -9,7 +9,7 @@ use alloc::boxed::Box;
99
use core::pin::Pin;
1010
use kernel::of::OfMatchTable;
1111
use kernel::prelude::*;
12-
use kernel::{cstr, platdev};
12+
use kernel::{c_str, platdev};
1313

1414
module! {
1515
type: RngModule,
@@ -25,7 +25,7 @@ struct RngModule {
2525

2626
impl KernelModule for RngModule {
2727
fn init() -> Result<Self> {
28-
let of_match_tbl = OfMatchTable::new(&cstr!("brcm,bcm2835-rng"))?;
28+
let of_match_tbl = OfMatchTable::new(&c_str!("brcm,bcm2835-rng"))?;
2929

3030
let pdev = platdev::Registration::new_pinned(
3131
cstr!("bcm2835-rng-rust"),

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;
@@ -72,7 +74,7 @@ pub mod user_ptr;
7274
pub use build_error::build_error;
7375

7476
pub use crate::error::{Error, Result};
75-
pub use crate::types::{CStr, Mode};
77+
pub use crate::types::Mode;
7678

7779
/// Page size defined in terms of the `PAGE_SHIFT` macro from C.
7880
///

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/of.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use alloc::boxed::Box;
99
use crate::{
1010
bindings, c_types,
1111
error::{Error, Result},
12+
str::CStr,
1213
types::PointerWrapper,
13-
CStr,
1414
};
1515

1616
use core::mem::transmute;
@@ -32,7 +32,7 @@ pub struct OfMatchTable(InnerTable);
3232

3333
impl OfMatchTable {
3434
/// Creates a [`OfMatchTable`] from a single `compatible` string.
35-
pub fn new(compatible: &CStr<'static>) -> Result<Self> {
35+
pub fn new(compatible: &'static CStr) -> Result<Self> {
3636
let tbl = Box::try_new([
3737
Self::new_of_device_id(compatible)?,
3838
bindings::of_device_id::default(),
@@ -43,7 +43,7 @@ impl OfMatchTable {
4343
Ok(Self(tbl))
4444
}
4545

46-
fn new_of_device_id(compatible: &CStr<'static>) -> Result<bindings::of_device_id> {
46+
fn new_of_device_id(compatible: &'static CStr) -> Result<bindings::of_device_id> {
4747
let mut buf = [0_u8; 128];
4848
if compatible.len() > buf.len() {
4949
return Err(Error::EINVAL);

rust/kernel/platdev.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{
1111
error::{Error, Result},
1212
of::OfMatchTable,
1313
pr_info,
14+
str::CStr,
1415
types::PointerWrapper,
15-
CStr,
1616
};
1717
use alloc::boxed::Box;
1818
use core::{marker::PhantomPinned, pin::Pin};
@@ -43,7 +43,7 @@ extern "C" fn remove_callback(_pdev: *mut bindings::platform_device) -> c_types:
4343
impl Registration {
4444
fn register(
4545
self: Pin<&mut Self>,
46-
name: CStr<'static>,
46+
name: &'static CStr,
4747
of_match_table: Option<OfMatchTable>,
4848
module: &'static crate::ThisModule,
4949
) -> Result {
@@ -53,7 +53,7 @@ impl Registration {
5353
// Already registered.
5454
return Err(Error::EINVAL);
5555
}
56-
this.pdrv.driver.name = name.as_ptr() as *const c_types::c_char;
56+
this.pdrv.driver.name = name.as_char_ptr();
5757
if let Some(tbl) = of_match_table {
5858
let ptr = tbl.into_pointer();
5959
this.of_table = Some(ptr);
@@ -82,7 +82,7 @@ impl Registration {
8282
///
8383
/// Returns a pinned heap-allocated representation of the registration.
8484
pub fn new_pinned(
85-
name: CStr<'static>,
85+
name: &'static CStr,
8686
of_match_tbl: Option<OfMatchTable>,
8787
module: &'static crate::ThisModule,
8888
) -> Result<Pin<Box<Self>>> {

0 commit comments

Comments
 (0)