rust: make pin-init its own crate

Rename relative paths inside of the crate to still refer to the same
items, also rename paths inside of the kernel crate and adjust the build
system to build the crate.

[ Remove the `expect` (and thus the `lint_reasons` feature) since
  the tree now uses `quote!` from `rust/macros/export.rs`. Remove the
  `TokenStream` import removal, since it is now used as well.

  In addition, temporarily (i.e. just for this commit) use an `--extern
  force:alloc` to prevent an unknown `new_uninit` error in the `rustdoc`
  target. For context, please see a similar case in:

      https://lore.kernel.org/lkml/20240422090644.525520-1-ojeda@kernel.org/

  And adjusted the message above. - Miguel ]

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://lore.kernel.org/r/20250308110339.2997091-16-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
Benno Lossin
2025-03-08 11:05:09 +00:00
committed by Miguel Ojeda
parent d7659acca7
commit dbd5058ba6
28 changed files with 164 additions and 153 deletions

View File

@@ -15,9 +15,9 @@
use core::ptr::NonNull;
use core::result::Result;
use crate::init::{InPlaceWrite, Init, PinInit, ZeroableOption};
use crate::init_ext::InPlaceInit;
use crate::init::InPlaceInit;
use crate::types::ForeignOwnable;
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
/// The kernel's [`Box`] type -- a heap allocation for a single value of type `T`.
///

View File

@@ -10,12 +10,11 @@
bindings,
block::mq::{operations::OperationsVTable, request::RequestDataWrapper, Operations},
error,
prelude::PinInit,
try_pin_init,
prelude::try_pin_init,
types::Opaque,
};
use core::{convert::TryInto, marker::PhantomData};
use macros::{pin_data, pinned_drop};
use pin_init::{pin_data, pinned_drop, PinInit};
/// A wrapper for the C `struct blk_mq_tag_set`.
///

View File

@@ -6,9 +6,9 @@
//! register using the [`Registration`] class.
use crate::error::{Error, Result};
use crate::{device, init::PinInit, of, str::CStr, try_pin_init, types::Opaque, ThisModule};
use crate::{device, of, str::CStr, try_pin_init, types::Opaque, ThisModule};
use core::pin::Pin;
use macros::{pin_data, pinned_drop};
use pin_init::{pin_data, pinned_drop, PinInit};
/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
/// Amba, etc.) to provide the corresponding subsystem specific implementation to register /
@@ -114,7 +114,7 @@ struct DriverModule {
impl $crate::InPlaceModule for DriverModule {
fn init(
module: &'static $crate::ThisModule
) -> impl $crate::init::PinInit<Self, $crate::error::Error> {
) -> impl ::pin_init::PinInit<Self, $crate::error::Error> {
$crate::try_pin_init!(Self {
_driver <- $crate::driver::Registration::new(
<Self as $crate::ModuleMetadata>::NAME,

View File

@@ -23,7 +23,7 @@
//!
//! [`Opaque<T>`]: crate::types::Opaque
//! [`Opaque::ffi_init`]: crate::types::Opaque::ffi_init
//! [`pin_init!`]: crate::pin_init
//! [`pin_init!`]: pin_init::pin_init
//!
//! # Examples
//!
@@ -137,8 +137,8 @@
use crate::{
alloc::{AllocError, Flags},
error::{self, Error},
init::{init_from_closure, pin_init_from_closure, Init, PinInit},
};
use pin_init::{init_from_closure, pin_init_from_closure, Init, PinInit};
/// Smart pointer that can initialize memory in-place.
pub trait InPlaceInit<T>: Sized {
@@ -205,7 +205,8 @@ fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
/// # Examples
///
/// ```rust
/// use kernel::{init::zeroed, error::Error};
/// use kernel::error::Error;
/// use pin_init::zeroed;
/// struct BigBuf {
/// big: KBox<[u8; 1024 * 1024 * 1024]>,
/// small: [u8; 1024 * 1024],
@@ -222,7 +223,7 @@ fn init<E>(init: impl Init<T, E>, flags: Flags) -> error::Result<Self>
/// ```
///
/// [`Infallible`]: core::convert::Infallible
/// [`init!`]: crate::init!
/// [`init!`]: pin_init::init
/// [`try_pin_init!`]: crate::try_pin_init!
/// [`Error`]: crate::error::Error
#[macro_export]
@@ -230,14 +231,14 @@ macro_rules! try_init {
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}) => {
$crate::_try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
$($fields)*
}? $crate::error::Error)
};
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}? $err:ty) => {
$crate::_try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
::pin_init::try_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
$($fields)*
}? $err)
};
@@ -262,7 +263,8 @@ macro_rules! try_init {
///
/// ```rust
/// # #![feature(new_uninit)]
/// use kernel::{init::zeroed, error::Error};
/// use kernel::error::Error;
/// use pin_init::zeroed;
/// #[pin_data]
/// struct BigBuf {
/// big: KBox<[u8; 1024 * 1024 * 1024]>,
@@ -282,21 +284,21 @@ macro_rules! try_init {
/// ```
///
/// [`Infallible`]: core::convert::Infallible
/// [`pin_init!`]: crate::pin_init
/// [`pin_init!`]: pin_init::pin_init
/// [`Error`]: crate::error::Error
#[macro_export]
macro_rules! try_pin_init {
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}) => {
$crate::_try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
$($fields)*
}? $crate::error::Error)
};
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}? $err:ty) => {
$crate::_try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
::pin_init::try_pin_init!($(&$this in)? $t $(::<$($generics),* $(,)?>)? {
$($fields)*
}? $err)
};

View File

@@ -50,11 +50,7 @@
#[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
pub mod firmware;
pub mod fs;
#[path = "../pin-init/src/lib.rs"]
pub mod init;
// momentarily use the name `init_ext` and set the path manually
#[path = "init.rs"]
pub mod init_ext;
pub mod io;
pub mod ioctl;
pub mod jump_label;
@@ -116,11 +112,11 @@ pub trait InPlaceModule: Sync + Send {
/// Creates an initialiser for the module.
///
/// It is called when the module is loaded.
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error>;
fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error>;
}
impl<T: Module> InPlaceModule for T {
fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, error::Error> {
let initer = move |slot: *mut Self| {
let m = <Self as Module>::init(module)?;
@@ -130,7 +126,7 @@ fn init(module: &'static ThisModule) -> impl init::PinInit<Self, error::Error> {
};
// SAFETY: On success, `initer` always fully initialises an instance of `Self`.
unsafe { init::pin_init_from_closure(initer) }
unsafe { pin_init::pin_init_from_closure(initer) }
}
}

View File

@@ -4,12 +4,12 @@
//! A linked list implementation.
use crate::init::PinInit;
use crate::sync::ArcBorrow;
use crate::types::Opaque;
use core::iter::{DoubleEndedIterator, FusedIterator};
use core::marker::PhantomData;
use core::ptr;
use pin_init::PinInit;
mod impl_list_item_mod;
pub use self::impl_list_item_mod::{

View File

@@ -17,7 +17,9 @@
pub use crate::alloc::{flags::*, Box, KBox, KVBox, KVVec, KVec, VBox, VVec, Vec};
#[doc(no_inline)]
pub use macros::{export, module, pin_data, pinned_drop, vtable, Zeroable};
pub use macros::{export, module, vtable};
pub use pin_init::{init, pin_data, pin_init, pinned_drop, InPlaceWrite, Init, PinInit, Zeroable};
pub use super::{build_assert, build_error};
@@ -28,7 +30,7 @@
pub use super::{dev_alert, dev_crit, dev_dbg, dev_emerg, dev_err, dev_info, dev_notice, dev_warn};
pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn};
pub use super::{init, pin_init, try_init, try_pin_init};
pub use super::{try_init, try_pin_init};
pub use super::static_assert;
@@ -36,7 +38,6 @@
pub use super::{str::CStr, ThisModule};
pub use super::init::{InPlaceWrite, Init, PinInit};
pub use super::init_ext::InPlaceInit;
pub use super::init::InPlaceInit;
pub use super::current;

View File

@@ -19,8 +19,7 @@
use crate::{
alloc::{AllocError, Flags, KBox},
bindings,
init::{self, InPlaceWrite, Init, PinInit},
init_ext::InPlaceInit,
init::InPlaceInit,
try_init,
types::{ForeignOwnable, Opaque},
};
@@ -33,7 +32,7 @@
pin::Pin,
ptr::NonNull,
};
use macros::pin_data;
use pin_init::{self, pin_data, InPlaceWrite, Init, PinInit};
mod std_vendor;
@@ -738,7 +737,7 @@ pub fn new_uninit(flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError>
try_init!(ArcInner {
// SAFETY: There are no safety requirements for this FFI call.
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
data <- init::uninit::<T, AllocError>(),
data <- pin_init::uninit::<T, AllocError>(),
}? AllocError),
flags,
)?;

View File

@@ -8,8 +8,6 @@
use super::{lock::Backend, lock::Guard, LockClassKey};
use crate::{
ffi::{c_int, c_long},
init::PinInit,
pin_init,
str::CStr,
task::{MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE, TASK_NORMAL, TASK_UNINTERRUPTIBLE},
time::Jiffies,
@@ -17,7 +15,7 @@
};
use core::marker::PhantomPinned;
use core::ptr;
use macros::pin_data;
use pin_init::{pin_data, pin_init, PinInit};
/// Creates a [`CondVar`] initialiser with the given name and a newly-created lock class.
#[macro_export]

View File

@@ -7,13 +7,11 @@
use super::LockClassKey;
use crate::{
init::PinInit,
pin_init,
str::CStr,
types::{NotThreadSafe, Opaque, ScopeGuard},
};
use core::{cell::UnsafeCell, marker::PhantomPinned};
use macros::pin_data;
use pin_init::{pin_data, pin_init, PinInit};
pub mod mutex;
pub mod spinlock;

View File

@@ -26,7 +26,7 @@ macro_rules! new_mutex {
/// Since it may block, [`Mutex`] needs to be used with care in atomic contexts.
///
/// Instances of [`Mutex`] need a lock class and to be pinned. The recommended way to create such
/// instances is with the [`pin_init`](crate::pin_init) and [`new_mutex`] macros.
/// instances is with the [`pin_init`](pin_init::pin_init) and [`new_mutex`] macros.
///
/// # Examples
///

View File

@@ -24,7 +24,7 @@ macro_rules! new_spinlock {
/// unlocked, at which point another CPU will be allowed to make progress.
///
/// Instances of [`SpinLock`] need a lock class and to be pinned. The recommended way to create such
/// instances is with the [`pin_init`](crate::pin_init) and [`new_spinlock`] macros.
/// instances is with the [`pin_init`](pin_init::pin_init) and [`new_spinlock`] macros.
///
/// # Examples
///

View File

@@ -2,7 +2,6 @@
//! Kernel types.
use crate::init::{self, PinInit, Zeroable};
use core::{
cell::UnsafeCell,
marker::{PhantomData, PhantomPinned},
@@ -10,6 +9,7 @@
ops::{Deref, DerefMut},
ptr::NonNull,
};
use pin_init::{PinInit, Zeroable};
/// Used to transfer ownership to and from foreign (non-Rust) languages.
///
@@ -336,7 +336,7 @@ pub fn pin_init(slot: impl PinInit<T>) -> impl PinInit<Self> {
// - `ptr` is a valid pointer to uninitialized memory,
// - `slot` is not accessed on error; the call is infallible,
// - `slot` is pinned in memory.
let _ = unsafe { init::PinInit::<T>::__pinned_init(slot, ptr) };
let _ = unsafe { PinInit::<T>::__pinned_init(slot, ptr) };
})
}
@@ -352,7 +352,7 @@ pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
// initialize the `T`.
unsafe {
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
pin_init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
init_func(Self::raw_get(slot));
Ok(())
})
@@ -372,7 +372,9 @@ pub fn try_ffi_init<E>(
) -> impl PinInit<Self, E> {
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
// initialize the `T`.
unsafe { init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::raw_get(slot))) }
unsafe {
pin_init::pin_init_from_closure::<_, E>(move |slot| init_func(Self::raw_get(slot)))
}
}
/// Returns a raw pointer to the opaque data.