Re: [PATCH v2 7/7] rust: file: add abstraction for `poll_table`

From: Benno Lossin
Date: Tue Dec 12 2023 - 12:01:47 EST


On 12/12/23 10:59, Alice Ryhl wrote:
> On Fri, Dec 8, 2023 at 6:53 PM Benno Lossin <benno.lossin@xxxxxxxxx> wrote:
>> On 12/6/23 12:59, Alice Ryhl wrote:
>>> + fn get_qproc(&self) -> bindings::poll_queue_proc {
>>> + let ptr = self.0.get();
>>> + // SAFETY: The `ptr` is valid because it originates from a reference, and the `_qproc`
>>> + // field is not modified concurrently with this call since we have an immutable reference.
>>
>> This needs an invariant on `PollTable` (i.e. `self.0` is valid).
>
> How would you phrase it?

- `self.0` contains a valid `bindings::poll_table`.
- `self.0` is only modified via references to `Self`.

>>> + unsafe { (*ptr)._qproc }
>>> + }
>>> +
>>> + /// Register this [`PollTable`] with the provided [`PollCondVar`], so that it can be notified
>>> + /// using the condition variable.
>>> + pub fn register_wait(&mut self, file: &File, cv: &PollCondVar) {
>>> + if let Some(qproc) = self.get_qproc() {
>>> + // SAFETY: The pointers to `self` and `file` are valid because they are references.
>>
>> What about cv.wait_list...
>
> I can add it to the list of things that are valid due to references.

Yes this is getting a bit tedious.

What if we create a newtype wrapping `Opaque<T>` with the invariant
that it contains a valid value? Then we could have a specially named
getter for which we would always assume that the returned pointer is
valid. And thus permit you to not mention it in the SAFETY comment?

[...]

>>> +#[pinned_drop]
>>> +impl PinnedDrop for PollCondVar {
>>> + fn drop(self: Pin<&mut Self>) {
>>> + // Clear anything registered using `register_wait`.
>>> + //
>>> + // SAFETY: The pointer points at a valid wait list.
>>
>> I was a bit confused by "wait list", since the C type is named
>> `wait_queue_head`, maybe just use the type name?
>
> I will update all instances of "wait list" to "wait_queue_head". It's
> because I incorrectly remembered the C type name to be "wait_list".

Maybe we should also change the name of the field on `CondVar`?

If you guys agree, I can open a good-first-issue, since it is a very
simple change.

--
Cheers,
Benno