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>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"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 04/10] rust: list: add struct with prev/next pointers
Date: Thu, 01 Aug 2024 13:46:29 +0000	[thread overview]
Message-ID: <ea3ef06b-8c4c-4883-867f-b68f2eb589af@proton.me> (raw)
In-Reply-To: <CAH5fLgjntr81+OFxzVqvb+zL4RqHCap9LZnNxNvN_gzF8AKrRg@mail.gmail.com>

On 01.08.24 14:51, Alice Ryhl wrote:
> On Thu, Aug 1, 2024 at 12:45 PM Benno Lossin <benno.lossin@proton.me> wrote:
>>
>> On 01.08.24 11:42, Alice Ryhl wrote:
>>> On Wed, Jul 31, 2024 at 8:41 PM Benno Lossin <benno.lossin@proton.me> wrote:
>>>>
>>>> On 23.07.24 10:22, Alice Ryhl wrote:
>>>>> +/// The prev/next pointers for an item in a linked list.
>>>>> +///
>>>>> +/// # Invariants
>>>>> +///
>>>>> +/// The fields are null if and only if this item is not in a list.
>>>>> +#[repr(transparent)]
>>>>> +pub struct ListLinks<const ID: u64 = 0> {
>>>>> +    #[allow(dead_code)]
>>>>> +    inner: Opaque<ListLinksFields>,
>>>>
>>>> Do you really need `Opaque`? Or would `UnsafeCell` be enough? (If it is
>>>> enough and you change this, be aware that `Opaque` is `!Unpin`, so if
>>>> you intend for `ListLinks` to also be `!Unpin`, then you need a
>>>> `PhantomPinned`)
>>>
>>> I need the `!Unpin` part for aliasing.
>>
>> Oh good point, do you mind adding a comment for that?
>>
>>>>> +}
>>>>> +
>>>>> +// SAFETY: The next/prev fields of a ListLinks can be moved across thread boundaries.
>>>>
>>>> Why? This is not a justification.
>>>
>>> What would you say?
>>
>> While trying to come up with a safety comment I thought about the
>> following: this impl does not depend on the type that is behind the
>> pointer (ie the type containing the `ListLinks`). Thus this `ListLinks`
>> will always implement `Send` even if the pointed-to value does not.
>> What we could do (and what definitely would be correct) is this:
>> `List` can only be used with `Send` types, then we could implement
>> `Send` for `ListLinks`. But I haven't actually come up with a problem,
>> so there might a more permissive solution.
>> Do you have a use-case where you need `!Send` types in a list?
>>
>> Here is a part of my reasoning: If the pointed-to value is `!Send`, then
>> the `List` item type must also be `!Send`. Thus all list operations take
>> place on the same thread (since the `List` will be `!Send`). Therefore
>> nobody can access the `prev`/`next` pointers from another thread.
>>
>> But this does not justify that `ListLinks` can be made `Send`. (although
>> there isn't actually a problem)

I think I confused myself. The paragraph above actually explains why we
are allowed to make `ListLinks: Send`. What do you think of the
following comment:

// SAFETY: The only way to access/modify the pointers inside of `ListLinks<ID>` is via holding the
// associated `ListArc<T, ID>`. Since that type correctly implements `Send`, it is impossible to
// move this an instance of this type to a different thread if the pointees are `!Send`.

> I don't think there's any reason to forbid lists with !Send types. The
> List just becomes !Send too.

Yes, but that doesn't explain why `ListLinks` is allowed to be `Send`.

---
Cheers,
Benno


  reply	other threads:[~2024-08-01 13:46 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 [this message]
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
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=ea3ef06b-8c4c-4883-867f-b68f2eb589af@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).