Re: [PATCH 4/7] rust: file: add `FileDescriptorReservation`

From: Christian Brauner
Date: Thu Nov 30 2023 - 04:09:22 EST


On Wed, Nov 29, 2023 at 04:55:51PM +0000, Alice Ryhl wrote:
> Christian Brauner <brauner@xxxxxxxxxx> writes:
> > Can we follow the traditional file terminology, i.e.,
> > get_unused_fd_flags() and fd_install()? At least at the beginning this
> > might be quite helpful instead of having to mentally map new() and
> > commit() onto the C functions.
>
> Sure, I'll do that in the next version.
>
> >> + /// 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.

>
> >> + Ok(Self {
> >> + fd: fd as _,
> >
> > This is a cast to a u32?
>
> Yes.
>
> > Can you please draft a quick example how that return value would be
> > expected to be used by a caller? It's really not clear
>
> The most basic usage would look like this:
>
> // First, reserve the fd.
> let reservation = FileDescriptorReservation::new(O_CLOEXEC)?;
>
> // Then, somehow get a file to put in it.
> let file = get_file_using_fallible_operation()?;
>
> // Finally, commit it to the fd.
> reservation.commit(file);

Ok, the reason I asked was that I was confused about the PhantomData and
how that would figure into using the return value as I hadn't seen that
Ok(Self { }) syntax before. Thanks.

>
> In Rust Binder, reservations are used here:
> https://github.com/Darksonn/linux/blob/dca45e6c7848e024709b165a306cdbe88e5b086a/drivers/android/allocation.rs#L199-L210
> https://github.com/Darksonn/linux/blob/dca45e6c7848e024709b165a306cdbe88e5b086a/drivers/android/allocation.rs#L512-L541
>
> >> + pub fn commit(self, file: ARef<File>) {
> >> + // SAFETY: `self.fd` was previously returned by `get_unused_fd_flags`, and `file.ptr` is
> >> + // guaranteed to have an owned ref count by its type invariants.
> >> + unsafe { bindings::fd_install(self.fd, file.0.get()) };
> >
> > Why file.0.get()? Where did that come from?
>
> This gets a raw pointer to the C type.
>
> The `.0` part is a field access. `ARef` struct is a tuple struct, so its

Ah, there we go. It's a bit ugly tbh.

> fields are unnamed. However, the fields can still be accessed by index.