[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20231130091756.109655-1-aliceryhl@google.com>
Date: Thu, 30 Nov 2023 09:17:56 +0000
From: Alice Ryhl <aliceryhl@...gle.com>
To: brauner@...nel.org
Cc: a.hindborg@...sung.com, alex.gaynor@...il.com,
aliceryhl@...gle.com, arve@...roid.com, benno.lossin@...ton.me,
bjorn3_gh@...tonmail.com, boqun.feng@...il.com,
cmllamas@...gle.com, dan.j.williams@...el.com, dxu@...uu.xyz,
gary@...yguo.net, gregkh@...uxfoundation.org,
joel@...lfernandes.org, keescook@...omium.org,
linux-fsdevel@...r.kernel.org, linux-kernel@...r.kernel.org,
maco@...roid.com, ojeda@...nel.org, peterz@...radead.org,
rust-for-linux@...r.kernel.org, surenb@...gle.com,
tglx@...utronix.de, tkjos@...roid.com, viro@...iv.linux.org.uk,
wedsonaf@...il.com, willy@...radead.org
Subject: Re: [PATCH 4/7] rust: file: add `FileDescriptorReservation`
Christian Brauner <brauner@...nel.org> writes:
>>>> + /// Prevent values of this type from being moved to a different task.
>>>> + ///
>>>> + /// This is necessary because the C FFI calls assume that `current` is set to the task that
>>>> + /// owns the fd in question.
>>>> + _not_send_sync: PhantomData<*mut ()>,
>>>
>>> I don't fully understand this. Can you explain in a little more detail
>>> what you mean by this and how this works?
>>
>> Yeah, so, this has to do with the Rust trait `Send` that controls
>> whether it's okay for a value to get moved from one thread to another.
>> In this case, we don't want it to be `Send` so that it can't be moved to
>> another thread, since current might be different there.
>>
>> The `Send` trait is automatically applied to structs whenever *all*
>> fields of the struct are `Send`. So to ensure that a struct is not
>> `Send`, you add a field that is not `Send`.
>>
>> The `PhantomData` type used here is a special zero-sized type.
>> Basically, it says "pretend this struct has a field of type `*mut ()`,
>> but don't actually add the field". So for the purposes of `Send`, it has
>> a non-Send field, but since its wrapped in `PhantomData`, the field is
>> not there at runtime.
>
> This probably a stupid suggestion, question. But while PhantomData gives
> the right hint of what is happening I wouldn't mind if that was very
> explicitly called NoSendTrait or just add the explanatory comment. Yes,
> that's a lot of verbiage but you'd help us a lot.
I suppose we could add a typedef:
type NoSendTrait = PhantomData<*mut ()>;
and use that as the field type. The way I did it here is the "standard"
way of doing it, and if you look at code outside the kernel, you will
also find them using `PhantomData` like this. However, I don't mind
adding the typedef if you think it is helpful.
Alice
Powered by blists - more mailing lists