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

From: Alice Ryhl
Date: Wed Nov 29 2023 - 11:56:09 EST


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.

>> + 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);

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
fields are unnamed. However, the fields can still be accessed by index.

Alice