This is a note to let you know that I've just added the patch titled rust: make `UnsafeCell` the outer type in `Opaque` to the 6.5-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: rust-make-unsafecell-the-outer-type-in-opaque.patch and it can be found in the queue-6.5 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. commit 8454e37da13e2e16b631683e9dbca6148aa5c5ee Author: Alice Ryhl <aliceryhl@xxxxxxxxxx> Date: Wed Jun 14 11:53:28 2023 +0000 rust: make `UnsafeCell` the outer type in `Opaque` [ Upstream commit 35cad617df2eeef8440a38e82bb2d81ae32ca50d ] When combining `UnsafeCell` with `MaybeUninit`, it is idiomatic to use `UnsafeCell` as the outer type. Intuitively, this is because a `MaybeUninit<T>` might not contain a `T`, but we always want the effect of the `UnsafeCell`, even if the inner value is uninitialized. Now, strictly speaking, this doesn't really make a difference. The compiler will always apply the `UnsafeCell` effect even if the inner value is uninitialized. But I think we should follow the convention here. Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx> Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx> Reviewed-by: Gary Guo <gary@xxxxxxxxxxx> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@xxxxxxxxx> Link: https://lore.kernel.org/r/20230614115328.2825961-1-aliceryhl@xxxxxxxxxx Signed-off-by: Miguel Ojeda <ojeda@xxxxxxxxxx> Stable-dep-of: 0b4e3b6f6b79 ("rust: types: make `Opaque` be `!Unpin`") Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index d479f8da8f381..c0b8bb1a75393 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -206,17 +206,17 @@ 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>(MaybeUninit<UnsafeCell<T>>); +pub struct Opaque<T>(UnsafeCell<MaybeUninit<T>>); impl<T> Opaque<T> { /// Creates a new opaque value. pub const fn new(value: T) -> Self { - Self(MaybeUninit::new(UnsafeCell::new(value))) + Self(UnsafeCell::new(MaybeUninit::new(value))) } /// Creates an uninitialised value. pub const fn uninit() -> Self { - Self(MaybeUninit::uninit()) + Self(UnsafeCell::new(MaybeUninit::uninit())) } /// Creates a pin-initializer from the given initializer closure. @@ -240,7 +240,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::raw_get(self.0.as_ptr()) + UnsafeCell::get(&self.0).cast::<T>() } /// Gets the value behind `this`. @@ -248,7 +248,7 @@ pub fn get(&self) -> *mut T { /// This function is useful to get access to the value without creating intermediate /// references. pub const fn raw_get(this: *const Self) -> *mut T { - UnsafeCell::raw_get(this.cast::<UnsafeCell<T>>()) + UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>() } }