Re: [PATCH v4 1/3] rust: add UnsafePinned type
From: Christian Schrefl
Date: Sat May 17 2025 - 07:37:06 EST
Hi Benno,
On 13.05.25 10:51 PM, Benno Lossin wrote:
> On Sun May 11, 2025 at 8:21 PM CEST, Christian Schrefl wrote:
>> `UnsafePinned<T>` is useful for cases where a value might be shared with
>> C code but not directly used by it. In particular this is added for
>> storing additional data in the `MiscDeviceRegistration` which will be
>> shared between `fops->open` and the containing struct.
>>
>> Similar to `Opaque` but guarantees that the value is always initialized
>> and that the inner value is dropped when `UnsafePinned` is dropped.
>>
>> This was originally proposed for the IRQ abstractions [0] and is also
>> useful for other where the inner data may be aliased, but is always
>> valid and automatic `Drop` is desired.
>>
>> Since then the `UnsafePinned` type was added to upstream Rust [1] by Sky
>> as a unstable feature, therefore this patch implements the subset of the
>> upstream API for the `UnsafePinned` type required for additional data in
>> `MiscDeviceRegistration` and in the implementation of the `Opaque` type.
>>
>> Some differences to the upstream type definition are required in the
>> kernel implementation, because upstream type uses some compiler changes
>> to opt out of certain optimizations, this is documented in the
>> documentation and a comment on the `UnsafePinned` type.
>>
>> The documentation on is based on the upstream rust documentation with
>> minor modifications for the kernel implementation.
>>
>> Link: https://lore.kernel.org/rust-for-linux/CAH5fLgiOASgjoYKFz6kWwzLaH07DqP2ph+3YyCDh2+gYqGpABA@xxxxxxxxxxxxxx [0]
>> Link: https://github.com/rust-lang/rust/pull/137043 [1]
>> Suggested-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>> Reviewed-by: Gerald Wisböck <gerald.wisboeck@xxxxxxxxxxx>
>> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
>> Co-developed-by: Sky <sky@xxxxxxxx>
>> Signed-off-by: Sky <sky@xxxxxxxx>
>> Signed-off-by: Christian Schrefl <chrisi.schrefl@xxxxxxxxx>
>
> One nit below, with that fixed:
>
> Reviewed-by: Benno Lossin <lossin@xxxxxxxxxx>
>
>> ---
>> rust/kernel/types.rs | 6 ++
>> rust/kernel/types/unsafe_pinned.rs | 111 +++++++++++++++++++++++++++++++++++++
>> 2 files changed, 117 insertions(+)
>>
>> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
>> index 9d0471afc9648f2973235488b441eb109069adb1..705f420fdfbc4a576de1c4546578f2f04cdf615e 100644
>> --- a/rust/kernel/types.rs
>> +++ b/rust/kernel/types.rs
>> @@ -578,3 +581,6 @@ pub enum Either<L, R> {
>> /// [`NotThreadSafe`]: type@NotThreadSafe
>> #[allow(non_upper_case_globals)]
>> pub const NotThreadSafe: NotThreadSafe = PhantomData;
>> +
>> +mod unsafe_pinned;
>> +pub use unsafe_pinned::UnsafePinned;
>
> I would put `mod` to the top of the
Your sentence was cut off, I assume you mean:
> I would put `mod` to the top of the file.
I can do that, let me know if I should send a
new version or if this will be fixed when applying.
Cheers
Christian