[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <4af5a8fa-c27c-4ee5-9d2f-67a46bd34635@proton.me>
Date: Thu, 01 Aug 2024 10:50:06 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Andrew Morton <akpm@...ux-foundation.org>, Alex Gaynor <alex.gaynor@...il.com>, Wedson Almeida Filho <wedsonaf@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...sung.com>, Marco Elver <elver@...gle.com>, Coly Li <colyli@...e.de>, Paolo Abeni <pabeni@...hat.com>, Pierre Gondois <pierre.gondois@....com>, Ingo Molnar <mingo@...nel.org>, Jakub Kicinski <kuba@...nel.org>, Wei Yang <richard.weiyang@...il.com>, Matthew Wilcox <willy@...radead.org>, linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org, Kees Cook <kees@...nel.org>
Subject: Re: [PATCH v3 09/10] rust: list: support heterogeneous lists
On 01.08.24 11:38, Alice Ryhl wrote:
> On Thu, Aug 1, 2024 at 11:24 AM Benno Lossin <benno.lossin@...ton.me> wrote:
>>
>> On 23.07.24 10:22, Alice Ryhl wrote:
>>> @@ -181,6 +185,47 @@ unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self {
>>> }
>>> }
>>>
>>> +/// Similar to [`ListLinks`], but also contains a pointer to the full value.
>>> +///
>>> +/// This type can be used instead of [`ListLinks`] to support lists with trait objects.
>>> +#[repr(C)]
>>> +pub struct ListLinksSelfPtr<T: ?Sized, const ID: u64 = 0> {
>>> + /// The `ListLinks` field inside this value.
>>> + ///
>>> + /// This is public so that it can be used with `impl_has_list_links!`.
>>> + pub inner: ListLinks<ID>,
>>> + self_ptr: UnsafeCell<MaybeUninit<*const T>>,
>>
>> Why do you need `MaybeUninit`?
>
> Right now the constructor initializes it to MaybeUninit::zeroed().
> What would you initialize it to without MaybeUninit? Remember that the
> vtable pointer in a fat pointer has strict validity requirements.
Oh... I forgot about that, can you add a comment about that? Also why
not use `Opaque` in that case then?
>>> +}
>>> +
>>> +// SAFETY: The fields of a ListLinksSelfPtr can be moved across thread boundaries.
>>> +unsafe impl<T: ?Sized + Send, const ID: u64> Send for ListLinksSelfPtr<T, ID> {}
>>> +// SAFETY: The type is opaque so immutable references to a ListLinksSelfPtr are useless. Therefore,
>>> +// it's okay to have immutable access to a ListLinks from several threads at once.
>>> +//
>>> +// Note that `inner` being a public field does not prevent this type from being opaque, since
>>> +// `inner` is a opaque type.
>>> +unsafe impl<T: ?Sized + Sync, const ID: u64> Sync for ListLinksSelfPtr<T, ID> {}
>>
>> [...]
>>
>>> @@ -135,5 +178,91 @@ unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) -> *const Self {
>>> }
>>> }
>>> };
>>> +
>>> + (
>>> + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty {
>>> + using ListLinksSelfPtr;
>>> + } $($rest:tt)*
>>> + ) => {
>>> + // SAFETY: See GUARANTEES comment on each method.
>>> + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t {
>>> + // GUARANTEES:
>>> + // This implementation of `ListItem` will not give out exclusive access to the same
>>> + // `ListLinks` several times because calls to `prepare_to_insert` and `post_remove`
>>> + // must alternate and exclusive access is given up when `post_remove` is called.
>>> + //
>>> + // Other invocations of `impl_list_item!` also cannot give out exclusive access to the
>>> + // same `ListLinks` because you can only implement `ListItem` once for each value of
>>> + // `ID`, and the `ListLinks` fields only work with the specified `ID`.
>>> + unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate::list::ListLinks<$num> {
>>> + // SAFETY: The caller promises that `me` points at a valid value of type `Self`.
>>> + let links_field = unsafe { <Self as $crate::list::ListItem<$num>>::view_links(me) };
>>> +
>>> + let spoff = $crate::list::ListLinksSelfPtr::<Self, $num>::LIST_LINKS_SELF_PTR_OFFSET;
>>> + // SAFETY: The constant is equal to `offset_of!(ListLinksSelfPtr, self_ptr)`, so
>>> + // the pointer stays in bounds of the allocation.
>>> + let self_ptr = unsafe { (links_field as *const u8).add(spoff) }
>>> + as *const ::core::cell::UnsafeCell<*const Self>;
>>
>> A bit confused why you need to do it this way, can't you just do this?:
>>
>> let links_self_field = links_field.cast::<$crate::list::ListLinksSelfPtr>();
>> // SAFETY: ...
>> let self_ptr = unsafe { ::core::ptr::addr_of_mut!((*links_self_field).self_ptr) };
>
> If nothing else, the field is not public. I can't remember if there
> was another reason.
Oh yeah that's true, then you have to go via the offset.
---
Cheers,
Benno
Powered by blists - more mailing lists