From: Benno Lossin <benno.lossin@proton.me>
To: Alice Ryhl <aliceryhl@google.com>,
Miguel Ojeda <ojeda@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: "Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Marco Elver" <elver@google.com>,
"Kees Cook" <keescook@chromium.org>, "Coly Li" <colyli@suse.de>,
"Paolo Abeni" <pabeni@redhat.com>,
"Pierre Gondois" <pierre.gondois@arm.com>,
"Ingo Molnar" <mingo@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Wei Yang" <richard.weiyang@gmail.com>,
"Matthew Wilcox" <willy@infradead.org>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2 4/9] rust: list: add macro for implementing ListItem
Date: Mon, 27 May 2024 10:06:22 +0000 [thread overview]
Message-ID: <a3ea800c-4776-4890-8853-cd5704598a7f@proton.me> (raw)
In-Reply-To: <20240506-linked-list-v2-4-7b910840c91f@google.com>
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.
>
> The HasListLinks trait is only necessary if you are implementing
> ListItem using the impl_list_item macro.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> rust/kernel/list.rs | 3 ++
> rust/kernel/list/impl_list_item_mod.rs | 99 ++++++++++++++++++++++++++++++++++
> 2 files changed, 102 insertions(+)
>
> 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;
>
> +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, TryNewListArc,
> diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl_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<ID>` field at a fixed offset.
> +///
> +/// This trait is only used to help implement `ListItem` safely. If `ListItem` is implemented
> +/// manually, then this trait is not needed.
> +///
> +/// # Safety
> +///
> +/// All values of this type must have a `ListLinks<ID>` field at the given offset.
> +pub unsafe trait HasListLinks<const ID: u64 = 0> {
> + /// The offset of the `ListLinks` field.
> + const OFFSET: usize;
> +
> + /// Returns a pointer to the [`ListLinks<T, ID>`] field.
> + ///
> + /// # Safety
> + ///
> + /// The provided pointer must point at a valid struct of type `Self`.
> + ///
> + /// [`ListLinks<T, ID>`]: ListLinks
> + // We don't really need this method, but it's necessary for the implementation 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<ID> {
> + // SAFETY: The caller promises that the pointer is valid. The implementer promises that the
> + // `OFFSET` constant is correct.
> + unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut ListLinks<ID> }
> + }
> +}
> +
> +/// 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)* }
> + )*) => {$(
> + // SAFETY: The implementation of `raw_get_list_links` only compiles if the field has the
> + // right type.
> + unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)? for
> + $self $(<$($selfarg),*>)?
> + {
> + const OFFSET: usize = ::core::mem::offset_of!(Self, $($field).*) 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 dangling.
> + 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)*
> + ) => {
> + 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 {
> + <Self as $crate::list::HasListLinks<$num>>::raw_get_list_links(me.cast_mut())
> + }
> + }
> +
> + unsafe fn view_value(me: *mut $crate::list::ListLinks<$num>) -> *const Self {
> + let offset = <Self as $crate::list::HasListLinks<$num>>::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 { <Self as $crate::list::ListItem<$num>>::view_links(me) }
> + }
> +
> + unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) -> *const Self {
> + unsafe { <Self as $crate::list::ListItem<$num>>::view_value(me) }
> + }
> + }
> + };
> +}
> +pub use impl_list_item;
>
> --
> 2.45.0.rc1.225.g2a3ae87e7f-goog
>
next prev parent reply other threads:[~2024-05-27 10:06 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-06 9:53 [PATCH v2 0/9] Add Rust linked list for reference counted values Alice Ryhl
2024-05-06 9:53 ` [PATCH v2 1/9] rust: list: add ListArc Alice Ryhl
2024-05-27 9:25 ` Benno Lossin
2024-05-06 9:53 ` [PATCH v2 2/9] rust: list: add tracking for ListArc Alice Ryhl
2024-05-27 9:39 ` Benno Lossin
2024-05-06 9:53 ` [PATCH v2 3/9] rust: list: add struct with prev/next pointers Alice Ryhl
2024-05-27 9:58 ` Benno Lossin
2024-05-27 11:42 ` Alice Ryhl
2024-05-06 9:53 ` [PATCH v2 4/9] rust: list: add macro for implementing ListItem Alice Ryhl
2024-05-27 10:06 ` Benno Lossin [this message]
2024-05-06 9:53 ` [PATCH v2 5/9] rust: list: add List Alice Ryhl
2024-05-27 10:25 ` Benno Lossin
2024-05-06 9:53 ` [PATCH v2 6/9] rust: list: add iterators Alice Ryhl
2024-05-27 10:31 ` Benno Lossin
2024-05-06 9:53 ` [PATCH v2 7/9] rust: list: add cursor Alice Ryhl
2024-05-27 10:37 ` Benno Lossin
2024-05-06 9:53 ` [PATCH v2 8/9] rust: list: support heterogeneous lists Alice Ryhl
2024-05-27 10:46 ` Benno Lossin
2024-05-06 9:53 ` [PATCH v2 9/9] rust: list: add ListArcField Alice Ryhl
2024-05-27 10:50 ` Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a3ea800c-4776-4890-8853-cd5704598a7f@proton.me \
--to=benno.lossin@proton.me \
--cc=a.hindborg@samsung.com \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=colyli@suse.de \
--cc=elver@google.com \
--cc=gary@garyguo.net \
--cc=keescook@chromium.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=ojeda@kernel.org \
--cc=pabeni@redhat.com \
--cc=pierre.gondois@arm.com \
--cc=richard.weiyang@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®