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

From: Alice Ryhl
Date: Tue Dec 12 2023 - 04:59:56 EST


On Fri, Dec 8, 2023 at 6:53 PM Benno Lossin <benno.lossin@xxxxxxxxx> wrote:
>
> On 12/6/23 12:59, Alice Ryhl wrote:
> > diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs
> > index 9bcbea04dac3..eeb291cc60db 100644
> > --- a/rust/bindings/lib.rs
> > +++ b/rust/bindings/lib.rs
> > @@ -51,3 +51,4 @@ mod bindings_helper {
> >
> > pub const GFP_KERNEL: gfp_t = BINDINGS_GFP_KERNEL;
> > pub const __GFP_ZERO: gfp_t = BINDINGS___GFP_ZERO;
> > +pub const POLLFREE: __poll_t = BINDINGS_POLLFREE;
>
> You are no longer using this constant, should this still exist?

Nice catch, thanks!

> > + 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?

> > + 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.

> > + //
> > + // Before the wait list is destroyed, the destructor of `PollCondVar` will clear
> > + // everything in the wait list, so the wait list is not used after it is freed.
> > + unsafe { qproc(file.as_ptr() as _, cv.wait_list.get(), self.0.get()) };
> > + }
> > + }
> > +}
> > +
> > +/// A wrapper around [`CondVar`] that makes it usable with [`PollTable`].
> > +///
> > +/// # Invariant
> > +///
> > +/// If `needs_synchronize_rcu` is false, then there is nothing registered with `register_wait`.
>
> Not able to find `needs_synchronize_rcu` anywhere else, should this be
> here?

Sorry, this shouldn't be there. It was something I experimented with,
but gave up on.

> > +#[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".

Alice