Re: [RFC PATCH] fs: elide the smp_rmb fence in fd_install()

From: Jan Kara
Date: Thu Dec 05 2024 - 10:29:59 EST


On Thu 05-12-24 16:01:07, Mateusz Guzik wrote:
> On Thu, Dec 5, 2024 at 3:46 PM Jan Kara <jack@xxxxxxx> wrote:
> >
> > On Thu 05-12-24 13:03:32, Mateusz Guzik wrote:
> > > See the added commentary for reasoning.
> > >
> > > ->resize_in_progress handling is moved inside of expand_fdtable() for
> > > clarity.
> > >
> > > Whacks an actual fence on arm64.
> > >
> > > Signed-off-by: Mateusz Guzik <mjguzik@xxxxxxxxx>
> >
> > Hum, I don't think this works. What could happen now is:
> >
> > CPU1 CPU2
> > expand_fdtable() fd_install()
> > files->resize_in_progress = true;
> > ...
> > if (atomic_read(&files->count) > 1)
> > synchronize_rcu();
> > ...
> > rcu_assign_pointer(files->fdt, new_fdt);
> > if (cur_fdt != &files->fdtab)
> > call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
> >
> > rcu_read_lock_sched()
> >
> > fdt = rcu_dereference_sched(files->fdt);
> > /* Fetched old FD table - without
> > * smp_rmb() the read was reordered */
> > rcu_assign_pointer(files->fdt, new_fdt);
> > /*
> > * Publish everything before we unset ->resize_in_progress, see above
> > * for an explanation.
> > */
> > smp_wmb();
> > out:
> > files->resize_in_progress = false;
> > if (unlikely(files->resize_in_progress)) {
> > - false
> > rcu_assign_pointer(fdt->fd[fd], file);
> > - store in the old table - boom.
> >
>
> I don't believe this ordering is possible because of both
> synchronize_rcu and the fence before updating resize_in_progress.
>
> Any CPU which could try racing like that had to come in after the
> synchronize_rcu() call, meaning one of the 3 possibilities:
> - the flag is true and the fd table is old
> - the flag is true and the fd table is new
> - the flag is false and the fd table is new

I agree here.

> Suppose the CPU reordered loads of the flag and the fd table. There is
> no ordering in which it can see both the old table and the unset flag.

But I disagree here. If the reads are reordered, then the fd table read can
happen during the "flag is true and the fd table is old" state and the flag
read can happen later in "flag is false and the fd table is new" state.
Just as I outlined above...

Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR