rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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>, "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,
	"Kees Cook" <kees@kernel.org>
Subject: Re: [PATCH v3 06/10] rust: list: add List
Date: Thu, 01 Aug 2024 09:11:43 +0000	[thread overview]
Message-ID: <3e6a37d1-2460-42ce-8bc3-0b210759efa8@proton.me> (raw)
In-Reply-To: <20240723-linked-list-v3-6-89db92c7dbf4@google.com>

On 23.07.24 10:22, Alice Ryhl wrote:
> +    /// Add the provided item to the back of the list.
> +    pub fn push_back(&mut self, item: ListArc<T, ID>) {
> +        let raw_item = ListArc::into_raw(item);
> +        // SAFETY:
> +        // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`.
> +        // * If this requirement is violated, then the previous caller of `prepare_to_insert`
> +        //   violated the safety requirement that they can't give up ownership of the `ListArc`
> +        //   until they call `post_remove`.

I don't like this negative phrasing, what about "Since we have ownership
of the `ListArc`, `post_remove` must have been called after each
previous call to `prepare_to_insert`."?

> +        // * We own the `ListArc`.
> +        // * Removing items from this list is always done using `remove_internal_inner`, which
> +        //   calls `post_remove` before giving up ownership.
> +        let list_links = unsafe { T::prepare_to_insert(raw_item) };
> +        // SAFETY: We have not yet called `post_remove`, so `list_links` is still valid.
> +        let item = unsafe { ListLinks::fields(list_links) };
> +
> +        if self.first.is_null() {
> +            self.first = item;
> +            // SAFETY: The caller just gave us ownership of these fields.
> +            // INVARIANT: A linked list with one item should be cyclic.
> +            unsafe {
> +                (*item).next = item;
> +                (*item).prev = item;
> +            }
> +        } else {
> +            let next = self.first;
> +            // SAFETY: By the type invariant, this pointer is valid or null. We just checked that
> +            // it's not null, so it must be valid.
> +            let prev = unsafe { (*next).prev };
> +            // SAFETY: Pointers in a linked list are never dangling, and the caller just gave us
> +            // ownership of the fields on `item`.
> +            // INVARIANT: This correctly inserts `item` between `prev` and `next`.
> +            unsafe {
> +                (*item).next = next;
> +                (*item).prev = prev;
> +                (*prev).next = item;
> +                (*next).prev = item;
> +            }

You have this pattern several times, maybe make a function for this?

> +        }
> +    }
> +
> +    /// Add the provided item to the front of the list.
> +    pub fn push_front(&mut self, item: ListArc<T, ID>) {
> +        let raw_item = ListArc::into_raw(item);
> +        // SAFETY:
> +        // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`.
> +        // * If this requirement is violated, then the previous caller of `prepare_to_insert`
> +        //   violated the safety requirement that they can't give up ownership of the `ListArc`
> +        //   until they call `post_remove`.
> +        // * We own the `ListArc`.
> +        // * Removing items] from this list is always done using `remove_internal_inner`, which

Typo: "]".

> +        //   calls `post_remove` before giving up ownership.
> +        let list_links = unsafe { T::prepare_to_insert(raw_item) };
> +        // SAFETY: We have not yet called `post_remove`, so `list_links` is still valid.
> +        let item = unsafe { ListLinks::fields(list_links) };
> +
> +        if self.first.is_null() {
> +            // SAFETY: The caller just gave us ownership of these fields.
> +            // INVARIANT: A linked list with one item should be cyclic.
> +            unsafe {
> +                (*item).next = item;
> +                (*item).prev = item;
> +            }
> +        } else {
> +            let next = self.first;
> +            // SAFETY: We just checked that `next` is non-null.
> +            let prev = unsafe { (*next).prev };
> +            // SAFETY: Pointers in a linked list are never dangling, and the caller just gave us
> +            // ownership of the fields on `item`.
> +            // INVARIANT: This correctly inserts `item` between `prev` and `next`.
> +            unsafe {
> +                (*item).next = next;
> +                (*item).prev = prev;
> +                (*prev).next = item;
> +                (*next).prev = item;
> +            }
> +        }
> +        self.first = item;
> +    }
> +
> +    /// Removes the last item from this list.
> +    pub fn pop_back(&mut self) -> Option<ListArc<T, ID>> {
> +        if self.first.is_null() {
> +            return None;
> +        }
> +
> +        // SAFETY: We just checked that the list is not empty.

Additionally you need the type invariant that pointers in linked lists
are valid... This is a bit annoying, maybe in the future, we can have a
`ValidPtr` type that we could use here instead to avoid these
comments...

> +        let last = unsafe { (*self.first).prev };
> +        // SAFETY: The last item of this list is in this list.
> +        Some(unsafe { self.remove_internal(last) })
> +    }
> +
> +    /// Removes the first item from this list.
> +    pub fn pop_front(&mut self) -> Option<ListArc<T, ID>> {
> +        if self.first.is_null() {
> +            return None;
> +        }
> +
> +        // SAFETY: The first item of this list is in this list.
> +        Some(unsafe { self.remove_internal(self.first) })
> +    }
> +
> +    /// Removes the provided item from this list and returns it.
> +    ///
> +    /// This returns `None` if the item is not in the list. (Note that by the safety requirements,
> +    /// this means that the item is not in any list.)
> +    ///
> +    /// # Safety
> +    ///
> +    /// `item` must not be in a different linked list (with the same id).
> +    pub unsafe fn remove(&mut self, item: &T) -> Option<ListArc<T, ID>> {
> +        let mut item = unsafe { ListLinks::fields(T::view_links(item)) };
> +        // SAFETY: The user provided a reference, and reference are never dangling.
> +        //
> +        // As for why this is not a data race, there are two cases:
> +        //
> +        //  * If `item` is not in any list, then these fields are read-only and null.
> +        //  * If `item` is in this list, then we have exclusive access to these fields since we
> +        //    have a mutable reference to the list.
> +        //
> +        // In either case, there's no race.
> +        let ListLinksFields { next, prev } = unsafe { *item };
> +
> +        debug_assert_eq!(next.is_null(), prev.is_null());
> +        if !next.is_null() {
> +            // This is really a no-op, but this ensures that `item` is a raw pointer that was
> +            // obtained without going through a pointer->reference->pointer conversion rountrip.
> +            // This ensures that the list is valid under the more restrictive strict provenance
> +            // ruleset.
> +            //
> +            // SAFETY: We just checked that `next` is not null, and it's not dangling by the
> +            // list invariants.
> +            unsafe {
> +                debug_assert_eq!(item, (*next).prev);
> +                item = (*next).prev;
> +            }

How bad do you reckon is this for performance?

---
Cheers,
Benno

> +
> +            // SAFETY: We just checked that `item` is in a list, so the caller guarantees that it
> +            // is in this list. The pointers are in the right order.
> +            Some(unsafe { self.remove_internal_inner(item, next, prev) })
> +        } else {
> +            None
> +        }
> +    }


  reply	other threads:[~2024-08-01  9:11 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-23  8:22 [PATCH v3 00/10] Add Rust linked list for reference counted values Alice Ryhl
2024-07-23  8:22 ` [PATCH v3 01/10] rust: init: add `assert_pinned` macro Alice Ryhl
2024-07-23  8:22 ` [PATCH v3 02/10] rust: list: add ListArc Alice Ryhl
2024-07-31 16:47   ` Benno Lossin
2024-08-06 13:16     ` Alice Ryhl
2024-08-06 14:11       ` Benno Lossin
2024-08-06 14:14     ` Miguel Ojeda
2024-07-23  8:22 ` [PATCH v3 03/10] rust: list: add tracking for ListArc Alice Ryhl
2024-07-31 17:17   ` Benno Lossin
2024-07-23  8:22 ` [PATCH v3 04/10] rust: list: add struct with prev/next pointers Alice Ryhl
2024-07-31 18:41   ` Benno Lossin
2024-08-01  9:42     ` Alice Ryhl
2024-08-01 10:45       ` Benno Lossin
2024-08-01 12:51         ` Alice Ryhl
2024-08-01 13:46           ` Benno Lossin
2024-08-01 13:47             ` Alice Ryhl
2024-07-23  8:22 ` [PATCH v3 05/10] rust: list: add macro for implementing ListItem Alice Ryhl
2024-07-31 13:03   ` Alice Ryhl
2024-07-31 20:17   ` Benno Lossin
2024-07-23  8:22 ` [PATCH v3 06/10] rust: list: add List Alice Ryhl
2024-08-01  9:11   ` Benno Lossin [this message]
2024-08-01  9:40     ` Alice Ryhl
2024-08-01 10:48       ` Benno Lossin
2024-07-23  8:22 ` [PATCH v3 07/10] rust: list: add iterators Alice Ryhl
2024-07-23  8:22 ` [PATCH v3 08/10] rust: list: add cursor Alice Ryhl
2024-07-23  8:22 ` [PATCH v3 09/10] rust: list: support heterogeneous lists Alice Ryhl
2024-08-01  9:24   ` Benno Lossin
2024-08-01  9:38     ` Alice Ryhl
2024-08-01 10:50       ` Benno Lossin
2024-08-01 12:33         ` Alice Ryhl
2024-07-23  8:22 ` [PATCH v3 10/10] rust: list: add ListArcField Alice Ryhl

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=3e6a37d1-2460-42ce-8bc3-0b210759efa8@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=kees@kernel.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;
as well as URLs for NNTP newsgroup(s).