Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1956489lqb; Mon, 27 May 2024 03:17:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVePG9yaX6tZ/0O9Z/foA10WxEU3ZDJ9hrADh3TO1oEHQTj8hx3cqAvX4BmGj3injZwxxWlsJYmArZ1PGookVcdCzoFnB2Cl1VjDyanYw== X-Google-Smtp-Source: AGHT+IHe3xvpnLMi41hx7V0on9zI7M4VX8H4PGL0dE5I/H3O8CgAsprehG8fTCOjc+V41fqdiQtA X-Received: by 2002:a05:6359:63a7:b0:194:89d4:89db with SMTP id e5c5f4694b2df-197e521939fmr810983255d.18.1716805068258; Mon, 27 May 2024 03:17:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716805068; cv=pass; d=google.com; s=arc-20160816; b=du2s3FuuMwu2Hhz0uoqpLiGhL0Zhna7pu65NKLjoahTteJd8O5I7FG3akS0LKXnGuj 0K0TPOkd+SK4z9jiL/HPvKwio2ycIx6W3sfEni7PoIYUT8Pjk7ax0fnt4vILLXc3ezNE 6gWfUKPgwqPOBsVMIauLeqvkl2mY4V3TDRrTVaH6lVpTZjPYzS9tqH1ciPvedSmBU/2y W3AEhMbocFZW+Sz8yoD1KhqR8grQkPX7wDnNPoFEwudE+7DCDwqAgeNFP4n9vo+UF2k5 Jk1V6Cm3GVHku6gQ05f5UKJgRtF7u1J76roARUBVAEkS0rFktMmRphQiSIFk4qYz5uf2 WaZg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=hYiUOg3qu0xInrbrlGKtxB4IjvbTEiIJES/QFtLiWqk=; fh=1eN+strsLnX8cuvE+uTff7wY1KiEAf1jFO97IDCVf3U=; b=t3zk/2AELOCp5KRG2lsFb9Nz26XRSP+h81l/jm2Xkq7fbuMH+Frppa1skoFbKc6JKA Nquw6XgGWgLkIlurMC1CJB7GbywZcOqz0qn7YyPLZ4Qu3V1GrSJ9VM8rFNth+zfJIu+8 nOG+xMyFGOfAjlB1jWzayLBwJcp92cvDax2p0sIPtE872rSU4cbvAq51Mj2YzhWze7sF +vsq0s9OhxjhIKFEO1NX2h0JaCh3xRU+uRsRXcorRDxwuL2cm4YhMXN/utEw1mk9eKYo IdzqNt3s64IZ0veCQ1a6K8G5zi2jiaGZSWf5E+p/InD+oIhIfEtyvkV7jWx0i9tJC3XD flbQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=f2rrrjo2erahtl2gjjputfb44u.protonmail header.b=dnj8zF5q; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-190402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190402-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-682287f2050si6018855a12.442.2024.05.27.03.17.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 03:17:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=f2rrrjo2erahtl2gjjputfb44u.protonmail header.b=dnj8zF5q; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-190402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190402-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id F12F2B20322 for ; Mon, 27 May 2024 10:07:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD44C13AD0D; Mon, 27 May 2024 10:06:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="dnj8zF5q" Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (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 1258913B7A7 for ; Mon, 27 May 2024 10:06:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716804393; cv=none; b=pwAjLuRw3iG+CQsrEEaK3ScxF+RbuxNFCSKTX/Ol5d+TGQSpcsGWe8aZNMRQmN3IJIJaLWveu+nICpqab0NoPUQTvtm+RVoa25aXZCIaCVAcThiQ/K3nUocAfP2l0zIVlFWRZOHMQ4WJfZh7oamYxDWT8Xxe6iDSsEjMbnzxhO4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716804393; c=relaxed/simple; bh=WXMFmaCXyOHrCsBpiBVUwdLSoPEMW5xY3pxYv40bbh8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GwKzHbjvn5EhMdlmfMvxZZ1FAEzVsmeUYxV9GV0Wk5eWshOmObQ9HQTfOSo6az+PlibFxlLvs/mjMR2lcxkPNdMZdc1GMn7um31Xm/deC5QO71O6Q3gcsEQAJMxumUwgAt2NEUBVBjgIDRy1jaItlp2ytoDpTb/nTUNjrgXCCVk= 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=dnj8zF5q; arc=none smtp.client-ip=185.70.40.134 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=f2rrrjo2erahtl2gjjputfb44u.protonmail; t=1716804388; x=1717063588; bh=hYiUOg3qu0xInrbrlGKtxB4IjvbTEiIJES/QFtLiWqk=; 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=dnj8zF5q4gFWHa1KZ5saphR+PTdIniYhjfsQBUA3WlVGPTZIl5wu3G8Ns0MtOMUFK bvqTtmwScdEJahEZBJ391IcHAeqZVEUitEWx0QJCNc5nyRQJ1J8fSS0kmIdaPQoq85 1GtktxsUBq3FLDwMa4n5XqYYf0euwTsSQtxNp+T4p8Sxlis8eSUKZBjpGCQDE19dfh NMSckxYWnA1ZEKSCj0UifutS/b8cTRLOZGygZ4brFkLI9jH6fws2vyVJVllY6FDfmN MNqH6w7+xJwq4fPDmgPUfL4V83bWxYcb/yhDTqURAa/XUWbRu3e32CuewJiEx9+g+g 0mbohOqAR+Nsw== Date: Mon, 27 May 2024 10:06:22 +0000 To: Alice Ryhl , Miguel Ojeda , Andrew Morton From: Benno Lossin Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Marco Elver , Kees Cook , 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 Subject: Re: [PATCH v2 4/9] rust: list: add macro for implementing ListItem Message-ID: In-Reply-To: <20240506-linked-list-v2-4-7b910840c91f@google.com> References: <20240506-linked-list-v2-0-7b910840c91f@google.com> <20240506-linked-list-v2-4-7b910840c91f@google.com> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 001f9e86a32912698f89c7fb9185c64dbef96aed Precedence: bulk X-Mailing-List: linux-kernel@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 06.05.24 11:53, Alice Ryhl wrote: > Adds a macro for safely implementing the ListItem trait. As part of the > implementation of the macro, we also provide a HasListLinks trait > similar to the workqueue's HasWorkItem trait. >=20 > The HasListLinks trait is only necessary if you are implementing > ListItem using the impl_list_item macro. >=20 > Signed-off-by: Alice Ryhl > --- > rust/kernel/list.rs | 3 ++ > rust/kernel/list/impl_list_item_mod.rs | 99 ++++++++++++++++++++++++++++= ++++++ > 2 files changed, 102 insertions(+) >=20 > diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs > index b5cfbb96a392..f2eca542e090 100644 > --- a/rust/kernel/list.rs > +++ b/rust/kernel/list.rs > @@ -8,6 +8,9 @@ > use crate::types::Opaque; > use core::ptr; >=20 > +mod impl_list_item_mod; > +pub use self::impl_list_item_mod::{impl_has_list_links, impl_list_item, = HasListLinks}; > + > mod arc; > pub use self::arc::{ > impl_list_arc_safe, AtomicListArcTracker, ListArc, ListArcSafe, TryN= ewListArc, > diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/im= pl_list_item_mod.rs > new file mode 100644 > index 000000000000..3ff483be89d1 > --- /dev/null > +++ b/rust/kernel/list/impl_list_item_mod.rs > @@ -0,0 +1,99 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +// Copyright (C) 2024 Google LLC. > + > +//! Helpers for implementing list traits safely. > + > +use crate::list::ListLinks; > + > +/// Declares that this type has a `ListLinks` field at a fixed offse= t. > +/// > +/// This trait is only used to help implement `ListItem` safely. If `Lis= tItem` is implemented > +/// manually, then this trait is not needed. > +/// > +/// # Safety > +/// > +/// All values of this type must have a `ListLinks` field at the giv= en offset. > +pub unsafe trait HasListLinks { > + /// The offset of the `ListLinks` field. > + const OFFSET: usize; > + > + /// Returns a pointer to the [`ListLinks`] field. > + /// > + /// # Safety > + /// > + /// The provided pointer must point at a valid struct of type `Self`= . > + /// > + /// [`ListLinks`]: ListLinks > + // We don't really need this method, but it's necessary for the impl= ementation of > + // `impl_has_work!` to be correct. You could also check for correctness within the expansion of `impl_has_list_links` (by creating an invisible function there), so you don't need this function. It still could be useful for `dyn Trait` though. > + #[inline] > + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks { > + // SAFETY: The caller promises that the pointer is valid. The im= plementer promises that the > + // `OFFSET` constant is correct. > + unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut ListLinks } > + } > +} > + > +/// Implements the [`HasListLinks`] trait for the given type. > +#[macro_export] > +macro_rules! impl_has_list_links { > + ($(impl$(<$($implarg:ident),*>)? > + HasListLinks$(<$id:tt>)? > + for $self:ident $(<$($selfarg:ty),*>)? > + { self$(.$field:ident)* } > + )*) =3D> {$( > + // SAFETY: The implementation of `raw_get_list_links` only compi= les if the field has the > + // right type. > + unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>= )? for > + $self $(<$($selfarg),*>)? > + { > + const OFFSET: usize =3D ::core::mem::offset_of!(Self, $($fie= ld).*) as usize; AFAIK only a single field is supported on stable. > + > + #[inline] > + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate:= :list::ListLinks$(<$id>)? { > + // SAFETY: The caller promises that the pointer is not d= angling. > + unsafe { > + ::core::ptr::addr_of_mut!((*ptr)$(.$field)*) Note that this runs into the `Box` issue, but `offset_of` will fail to compile in that case, so it is fine. Do we want to note this in a comment? > + } > + } > + } > + )*}; > +} > +pub use impl_has_list_links; > + > +/// Implements the [`ListItem`] trait for the given type. > +/// > +/// Assumes that the type implements [`HasListLinks`]. > +/// > +/// [`ListItem`]: crate::list::ListItem > +#[macro_export] > +macro_rules! impl_list_item { > + ( > + impl$({$($generics:tt)*})? $crate::list::ListItem<$num:tt> for $= t:ty { > + using ListLinks; > + } $($rest:tt)* > + ) =3D> { > + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for = $t { Missing SAFETY comment. --- Cheers, Benno > + unsafe fn view_links(me: *const Self) -> *mut $crate::list::= ListLinks<$num> { > + unsafe { > + >::raw_get_= list_links(me.cast_mut()) > + } > + } > + > + unsafe fn view_value(me: *mut $crate::list::ListLinks<$num>)= -> *const Self { > + let offset =3D = >::OFFSET; > + unsafe { (me as *const u8).sub(offset) as *const Self } > + } > + > + unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate:= :list::ListLinks<$num> { > + unsafe { >::view_li= nks(me) } > + } > + > + unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>= ) -> *const Self { > + unsafe { >::view_va= lue(me) } > + } > + } > + }; > +} > +pub use impl_list_item; >=20 > -- > 2.45.0.rc1.225.g2a3ae87e7f-goog >=20