From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40DD4170A18 for ; Thu, 1 Aug 2024 10:50:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722509416; cv=none; b=MaOCeuD+RQw7jM0+S3t6cOaYDrH66JSoM1YQZTw75AQeDSVk336tqPgztWHrW1ecTV6Z+FXobKQAXwxA9Z64rydmP3jqKAeEN98ZL7xbgA4tmyai11Y9ggPhRAXerThApQPWtxpIglIqmorsEvuRu96+P/9FLMs03wMLug4b45E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722509416; c=relaxed/simple; bh=FzX9Oi8w1rb7TAtfeMJIxj7qYb3gZnjoJj39P4abMxM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WNqmGKFNm5wjAvGiBGiMrSQBWldg26E0cLll860aV5SnrgsyHJYBoQaLweY6qWprGqYSEotlg/NMYqW9/Zr4KJmgvfelJe92wDGiA1mClld6Tmv4rLrqvs8ZpJbKEpQ/akOls4yH0wAzlmSAu5tlmX1IAYDQMBaQY3kus5fgN+Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=PXjIONh8; arc=none smtp.client-ip=185.70.40.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="PXjIONh8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1722509412; x=1722768612; bh=gMdZocEy01zU7HSY4Jv0StclRtNQ2rOH9J2qCOvUQpg=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=PXjIONh8Kto9reGwq1qKvJxpdLNXG6+YJGSTPM0hkI8x6hnAGkal6M3w+pOY08trk a7tJETI3Z7ADOP9extEIdOR4i7/g9Wqf4snplZ3zD+rO7tSRk1nSYIk1jQBC/WgYQw P8oLR60X/Sdb8onIbnW5t9FUTWfSrsHUw4vdaKl1mlBv9I7L6+cJoNb5LsuV98BtRm pL1XyZOgDu4/+309UsZ0mdJJ2IX8nFEVNv4SDDY75gMAsX8Pjin38/RNQIdLmEAB06 yQyB3qAlaI/dNbg9osTkrdEIWj+QXhFcQvVAImYseAojVe8b7k9PBc7Y2PGRVPe0E2 zhyh/ishbRSBA== Date: Thu, 01 Aug 2024 10:50:06 +0000 To: Alice Ryhl From: Benno Lossin Cc: Miguel Ojeda , Andrew Morton , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Kees Cook Subject: Re: [PATCH v3 09/10] rust: list: support heterogeneous lists Message-ID: <4af5a8fa-c27c-4ee5-9d2f-67a46bd34635@proton.me> In-Reply-To: References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> <20240723-linked-list-v3-9-89db92c7dbf4@google.com> <2b548226-e323-466d-9f6d-762f6cbb5474@proton.me> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: e0c106a5abae8caf3adbab48a7e800a6c0333dfe Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 01.08.24 11:38, Alice Ryhl wrote: > On Thu, Aug 1, 2024 at 11:24=E2=80=AFAM Benno Lossin 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 wi= th trait objects. >>> +#[repr(C)] >>> +pub struct ListLinksSelfPtr { >>> + /// The `ListLinks` field inside this value. >>> + /// >>> + /// This is public so that it can be used with `impl_has_list_link= s!`. >>> + pub inner: ListLinks, >>> + self_ptr: UnsafeCell>, >> >> Why do you need `MaybeUninit`? >=20 > 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 Send for ListLinksSelfPtr= {} >>> +// SAFETY: The type is opaque so immutable references to a ListLinksSe= lfPtr are useless. Therefore, >>> +// it's okay to have immutable access to a ListLinks from several thre= ads at once. >>> +// >>> +// Note that `inner` being a public field does not prevent this type f= rom being opaque, since >>> +// `inner` is a opaque type. >>> +unsafe impl Sync for ListLinksSelfPtr= {} >> >> [...] >> >>> @@ -135,5 +178,91 @@ unsafe fn post_remove(me: *mut $crate::list::ListL= inks<$num>) -> *const Self { >>> } >>> } >>> }; >>> + >>> + ( >>> + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { >>> + using ListLinksSelfPtr; >>> + } $($rest:tt)* >>> + ) =3D> { >>> + // SAFETY: See GUARANTEES comment on each method. >>> + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> fo= r $t { >>> + // GUARANTEES: >>> + // This implementation of `ListItem` will not give out exc= lusive 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 `p= ost_remove` is called. >>> + // >>> + // Other invocations of `impl_list_item!` also cannot give= out exclusive access to the >>> + // same `ListLinks` because you can only implement `ListIt= em` once for each value of >>> + // `ID`, and the `ListLinks` fields only work with the spe= cified `ID`. >>> + unsafe fn prepare_to_insert(me: *const Self) -> *mut $crat= e::list::ListLinks<$num> { >>> + // SAFETY: The caller promises that `me` points at a v= alid value of type `Self`. >>> + let links_field =3D unsafe { >::view_links(me) }; >>> + >>> + let spoff =3D $crate::list::ListLinksSelfPtr::::LIST_LINKS_SELF_PTR_OFFSET; >>> + // SAFETY: The constant is equal to `offset_of!(ListLi= nksSelfPtr, self_ptr)`, so >>> + // the pointer stays in bounds of the allocation. >>> + let self_ptr =3D unsafe { (links_field as *const u8).a= dd(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 =3D links_field.cast::<$crate::list::ListLinksS= elfPtr>(); >> // SAFETY: ... >> let self_ptr =3D unsafe { ::core::ptr::addr_of_mut!((*links_self_fie= ld).self_ptr) }; >=20 > 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