Re: [PATCH 6/7] rust: file: add `DeferredFdCloser`

From: Alice Ryhl
Date: Fri Dec 01 2023 - 06:35:46 EST


Benno Lossin <benno.lossin@xxxxxxxxx> writes:
>> + // SAFETY: The `inner` pointer points at a valid and fully initialized task work that is
>> + // ready to be scheduled.
>> + unsafe { bindings::task_work_add(current, inner, TWA_RESUME) };
>
> I am a bit confused, when does `do_close_fd` actually run? Does
> `TWA_RESUME` mean that `inner` is scheduled to run after the current
> task has been completed?

When the current syscall returns to userspace.

>> + // SAFETY: This function is an implementation detail of `close_fd`, so its safety comments
>> + // should be read in extension of that method.
>> + unsafe extern "C" fn do_close_fd(inner: *mut bindings::callback_head) {
>> + // SAFETY: In `close_fd` we use this method together with a pointer that originates from a
>> + // `Box<DeferredFdCloserInner>`, and we have just been given ownership of that allocation.
>> + let inner = unsafe { Box::from_raw(inner as *mut DeferredFdCloserInner) };
>
> In order for this call to be sound, `inner` must be an exclusive
> pointer (including any possible references into the `callback_head`).
> Is this the case?

Yes, when this is called, it's been removed from the linked list of task
work. That's why we can kfree it.

>> + // SAFETY: Since `DeferredFdCloserInner` is `#[repr(C)]`, casting the pointers gives a
>> + // pointer to the `twork` field.
>> + let inner = Box::into_raw(self.inner) as *mut bindings::callback_head;
>
> Here you can just use `.cast::<...>()`.

Will do.

Alice