lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 03 Jul 2023 11:30:56 +0200
From:   "Andreas Hindborg (Samsung)" <nmi@...aspace.dk>
To:     Benno Lossin <benno.lossin@...ton.me>
Cc:     Miguel Ojeda <ojeda@...nel.org>,
        Wedson Almeida Filho <wedsonaf@...il.com>,
        Alex Gaynor <alex.gaynor@...il.com>,
        Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
        Björn Roy Baron <bjorn3_gh@...tonmail.com>,
        Alice Ryhl <aliceryhl@...gle.com>,
        rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
        patches@...ts.linux.dev
Subject: Re: [PATCH] rust: types: make `Opaque` be `!Unpin`


Benno Lossin <benno.lossin@...ton.me> writes:

> Adds a `PhantomPinned` field to `Opaque<T>`. This removes the last Rust
> guarantee: the assumption that the type `T` can be freely moved. This is
> not the case for many types from the C side (e.g. if they contain a
> `struct list_head`). This change removes the need to add a
> `PhantomPinned` field manually to Rust structs that contain C structs
> which must not be moved.
>
> Signed-off-by: Benno Lossin <benno.lossin@...ton.me>
> ---

Reviewed-by: Andreas Hindborg <a.hindborg@...sung.com>

> This patch depends on the patch that swaps `UnsafeCell` with
> `MaybeUninit` inside `Opaque` [1].
>
> [1]: https://lore.kernel.org/rust-for-linux/20230614115328.2825961-1-aliceryhl@google.com/
> ---
>  rust/kernel/types.rs | 19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
> index fb41635f1e1f..e664a2beef30 100644
> --- a/rust/kernel/types.rs
> +++ b/rust/kernel/types.rs
> @@ -6,7 +6,7 @@
>  use alloc::boxed::Box;
>  use core::{
>      cell::UnsafeCell,
> -    marker::PhantomData,
> +    marker::{PhantomData, PhantomPinned},
>      mem::MaybeUninit,
>      ops::{Deref, DerefMut},
>      ptr::NonNull,
> @@ -224,17 +224,26 @@ fn drop(&mut self) {
>  ///
>  /// This is meant to be used with FFI objects that are never interpreted by Rust code.
>  #[repr(transparent)]
> -pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>);
> +pub struct Opaque<T> {
> +    value: UnsafeCell<MaybeUninit<T>>,
> +    _pin: PhantomPinned,
> +}
>  
>  impl<T> Opaque<T> {
>      /// Creates a new opaque value.
>      pub const fn new(value: T) -> Self {
> -        Self(UnsafeCell::new(MaybeUninit::new(value)))
> +        Self {
> +            value: UnsafeCell::new(MaybeUninit::new(value)),
> +            _pin: PhantomPinned,
> +        }
>      }
>  
>      /// Creates an uninitialised value.
>      pub const fn uninit() -> Self {
> -        Self(UnsafeCell::new(MaybeUninit::uninit()))
> +        Self {
> +            value: UnsafeCell::new(MaybeUninit::uninit()),
> +            _pin: PhantomPinned,
> +        }
>      }
>  
>      /// Creates a pin-initializer from the given initializer closure.
> @@ -258,7 +267,7 @@ pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
>  
>      /// Returns a raw pointer to the opaque data.
>      pub fn get(&self) -> *mut T {
> -        UnsafeCell::get(&self.0).cast::<T>()
> +        UnsafeCell::get(&self.value).cast::<T>()
>      }
>  
>      /// Gets the value behind `this`.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ