[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230715152958.527fba87.gary@garyguo.net>
Date: Sat, 15 Jul 2023 15:29:58 +0100
From: Gary Guo <gary@...yguo.net>
To: Asahi Lina <lina@...hilina.net>
Cc: Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Masahiro Yamada <masahiroy@...nel.org>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Nicolas Schier <nicolas@...sle.eu>, Tom Rix <trix@...hat.com>,
Daniel Vetter <daniel@...ll.ch>,
Hector Martin <marcan@...can.st>,
Sven Peter <sven@...npeter.dev>,
Alyssa Rosenzweig <alyssa@...enzweig.io>,
asahi@...ts.linux.dev, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-kbuild@...r.kernel.org,
llvm@...ts.linux.dev
Subject: Re: [PATCH RFC 02/11] rust: lock: Add Lock::pin_init()
On Fri, 14 Jul 2023 18:13:54 +0900
Asahi Lina <lina@...hilina.net> wrote:
> Allow initializing a lock using pin_init!(), instead of requiring
> the inner data to be passed through the stack.
>
> Signed-off-by: Asahi Lina <lina@...hilina.net>
> ---
> rust/kernel/sync/lock.rs | 30 +++++++++++++++++++++++++++++-
> rust/kernel/sync/lock/mutex.rs | 13 +++++++++++++
> 2 files changed, 42 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index a2216325632d..d493c5d19104 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -6,7 +6,9 @@
> //! spinlocks, raw spinlocks) to be provided with minimal effort.
>
> use super::LockClassKey;
> -use crate::{bindings, init::PinInit, pin_init, str::CStr, types::Opaque, types::ScopeGuard};
> +use crate::{
> + bindings, init::PinInit, pin_init, str::CStr, try_pin_init, types::Opaque, types::ScopeGuard,
> +};
> use core::{cell::UnsafeCell, marker::PhantomData, marker::PhantomPinned};
> use macros::pin_data;
>
> @@ -87,6 +89,7 @@ pub struct Lock<T: ?Sized, B: Backend> {
> _pin: PhantomPinned,
>
> /// The data protected by the lock.
> + #[pin]
> pub(crate) data: UnsafeCell<T>,
> }
>
> @@ -111,6 +114,31 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
> }),
> })
> }
> +
> + /// Constructs a new lock initialiser taking an initialiser.
> + pub fn pin_init<E>(
> + t: impl PinInit<T, E>,
> + name: &'static CStr,
> + key: &'static LockClassKey,
> + ) -> impl PinInit<Self, E>
> + where
> + E: core::convert::From<core::convert::Infallible>,
> + {
> + try_pin_init!(Self {
> + // SAFETY: We are just forwarding the initialization across a
> + // cast away from UnsafeCell, so the pin_init_from_closure and
> + // __pinned_init() requirements are in sync.
> + data <- unsafe { crate::init::pin_init_from_closure(move |slot: *mut UnsafeCell<T>| {
> + t.__pinned_init(slot as *mut T)
> + })},
> + _pin: PhantomPinned,
> + // SAFETY: `slot` is valid while the closure is called and both `name` and `key` have
> + // static lifetimes so they live indefinitely.
> + state <- Opaque::ffi_init(|slot| unsafe {
> + B::init(slot, name.as_char_ptr(), key.as_ptr())
> + }),
> + }? E)
> + }
I think you might be able to just modify the `new` function? We have a
blanket implementation
impl<T> Init<T, Infallible> for T
which makes any `T` also `impl PinInit`.
> }
>
> impl<T: ?Sized, B: Backend> Lock<T, B> {
> diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs
> index 923472f04af4..06fe685501b4 100644
> --- a/rust/kernel/sync/lock/mutex.rs
> +++ b/rust/kernel/sync/lock/mutex.rs
> @@ -18,6 +18,19 @@ macro_rules! new_mutex {
> };
> }
>
> +/// Creates a [`Mutex`] initialiser with the given name and a newly-created lock class,
> +/// given an initialiser for the inner type.
> +///
> +/// It uses the name if one is given, otherwise it generates one based on the file name and line
> +/// number.
> +#[macro_export]
> +macro_rules! new_mutex_pinned {
> + ($inner:expr $(, $name:literal)? $(,)?) => {
> + $crate::sync::Mutex::pin_init(
> + $inner, $crate::optional_name!($($name)?), $crate::static_lock_class!())
> + };
> +}
> +
> /// A mutual exclusion primitive.
> ///
> /// Exposes the kernel's [`struct mutex`]. When multiple threads attempt to lock the same mutex,
>
Powered by blists - more mailing lists