Skip to content

rust: miscdev: support names created at runtime #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drivers/android/rust_binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct BinderModule {
impl KernelModule for BinderModule {
fn init(name: &'static CStr, _module: &'static kernel::ThisModule) -> Result<Self> {
let ctx = Context::new()?;
let reg = Registration::new_pinned(name, ctx)?;
let reg = Registration::new_pinned(fmt!("{name}"), ctx)?;
Ok(Self { _reg: reg })
}
}
4 changes: 2 additions & 2 deletions drivers/char/hw_random/bcm2835_rng_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Broadcom BCM2835 Random Number Generator support.
use kernel::{
c_str, device, file::File, file_operations::FileOperations, io_buffer::IoBufferWriter, miscdev,
device, file::File, file_operations::FileOperations, io_buffer::IoBufferWriter, miscdev,
module_platform_driver, of, platform, prelude::*, sync::Ref,
};

Expand Down Expand Up @@ -57,7 +57,7 @@ impl platform::Driver for RngDriver {
data.registrations()
.ok_or(Error::ENXIO)?
.as_pinned_mut()
.register(c_str!("rust_hwrng"), ())?;
.register(fmt!("rust_hwrng"), ())?;
Ok(data.into())
}
}
28 changes: 19 additions & 9 deletions rust/kernel/miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use crate::bindings;
use crate::error::{Error, Result};
use crate::file_operations::{FileOpenAdapter, FileOperations, FileOperationsVtable};
use crate::{device, str::CStr, KernelModule, ThisModule};
use crate::{device, str::CStr, str::CString, KernelModule, ThisModule};
use alloc::boxed::Box;
use core::marker::PhantomPinned;
use core::{mem::MaybeUninit, pin::Pin};
use core::{fmt, mem::MaybeUninit, pin::Pin};

/// Options which can be used to configure how a misc device is registered.
///
Expand All @@ -28,7 +28,7 @@ use core::{mem::MaybeUninit, pin::Pin};
/// .mode(0o600)
/// .minor(10)
/// .parent(parent)
/// .register(reg, c_str!("sample"), ())
/// .register(reg, fmt!("sample"), ())
/// }
/// ```
#[derive(Default)]
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<'a> Options<'a> {
pub fn register<T: FileOperations>(
&self,
reg: Pin<&mut Registration<T>>,
name: &'static CStr,
name: fmt::Arguments<'_>,
open_data: T::OpenData,
) -> Result {
reg.register_with_options(name, open_data, self)
Expand All @@ -83,7 +83,7 @@ impl<'a> Options<'a> {
/// configured options.
pub fn register_new<T: FileOperations>(
&self,
name: &'static CStr,
name: fmt::Arguments<'_>,
open_data: T::OpenData,
) -> Result<Pin<Box<Registration<T>>>> {
let mut r = Pin::from(Box::try_new(Registration::new())?);
Expand All @@ -100,6 +100,7 @@ impl<'a> Options<'a> {
pub struct Registration<T: FileOperations> {
registered: bool,
mdev: bindings::miscdevice,
name: Option<CString>,
_pin: PhantomPinned,

/// Context initialised on construction and made available to all file instances on
Expand All @@ -116,6 +117,7 @@ impl<T: FileOperations> Registration<T> {
Self {
registered: false,
mdev: bindings::miscdevice::default(),
name: None,
_pin: PhantomPinned,
open_data: MaybeUninit::uninit(),
}
Expand All @@ -124,15 +126,19 @@ impl<T: FileOperations> Registration<T> {
/// Registers a miscellaneous device.
///
/// Returns a pinned heap-allocated representation of the registration.
pub fn new_pinned(name: &'static CStr, open_data: T::OpenData) -> Result<Pin<Box<Self>>> {
pub fn new_pinned(name: fmt::Arguments<'_>, open_data: T::OpenData) -> Result<Pin<Box<Self>>> {
Options::new().register_new(name, open_data)
}

/// Registers a miscellaneous device with the rest of the kernel.
///
/// It must be pinned because the memory block that represents the registration is
/// self-referential.
pub fn register(self: Pin<&mut Self>, name: &'static CStr, open_data: T::OpenData) -> Result {
pub fn register(
self: Pin<&mut Self>,
name: fmt::Arguments<'_>,
open_data: T::OpenData,
) -> Result {
Options::new().register(self, name, open_data)
}

Expand All @@ -143,7 +149,7 @@ impl<T: FileOperations> Registration<T> {
/// self-referential.
pub fn register_with_options(
self: Pin<&mut Self>,
name: &'static CStr,
name: fmt::Arguments<'_>,
open_data: T::OpenData,
opts: &Options<'_>,
) -> Result {
Expand All @@ -154,6 +160,8 @@ impl<T: FileOperations> Registration<T> {
return Err(Error::EINVAL);
}

let name = CString::try_from_fmt(name)?;

// SAFETY: The adapter is compatible with `misc_register`.
this.mdev.fops = unsafe { FileOperationsVtable::<Self, T>::build() };
this.mdev.name = name.as_char_ptr();
Expand All @@ -179,6 +187,8 @@ impl<T: FileOperations> Registration<T> {
return Err(Error::from_kernel_errno(ret));
}

this.name = Some(name);

Ok(())
}
}
Expand Down Expand Up @@ -235,7 +245,7 @@ pub struct Module<T: FileOperations<OpenData = ()>> {
impl<T: FileOperations<OpenData = ()>> KernelModule for Module<T> {
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
Ok(Self {
_dev: Registration::new_pinned(name, ())?,
_dev: Registration::new_pinned(crate::fmt!("{name}"), ())?,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/rust/rust_miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl KernelModule for RustMiscdev {
let state = SharedState::try_new()?;

Ok(RustMiscdev {
_dev: miscdev::Registration::new_pinned(name, state)?,
_dev: miscdev::Registration::new_pinned(fmt!("{name}"), state)?,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/rust/rust_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl KernelModule for RustSemaphore {
mutex_init!(pinned, "Semaphore::inner");

Ok(Self {
_dev: Registration::new_pinned(name, sema.into())?,
_dev: Registration::new_pinned(fmt!("{name}"), sema.into())?,
})
}
}
Expand Down