Re: [PATCH v6 0/5] Fix easy bits of the negative dentry problem

From: Jan Kara

Date: Fri Nov 27 2026 - 09:04:02 EST


On Thu 26-11-26 13:24:42, Amir Goldstein wrote:
> On Thu, Nov 26, 2026 at 11:57 AM Jan Kara <jack@xxxxxxx> wrote:
> > So I guess we'll have to use the sequence counter in the end and we'll need
> > to be careful. Something like:
> >
> > spin_lock(&conn->lock);
> > gen = conn->scan_seqcount;
> > update_children = conn->flags & FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
> > /* Scan in progress? */
> > if (gen & 1) {
> > /*
> > * We need to wait for scan to complete even if
> > * UPDATE_CHILDREN is not set as somebody might have
> > * started a scan for us.
> > */
> > gen++;
> > wait_var_event_spinlock(&conn->scan_seqcount,
> > conn->scan_seqcount >= gen,
> > &conn->lock);
> > /*
> > * Somebody started the scan we need while we waited? Just
> > * wait for it to complete.
> > */
> > if (conn->scan_seqcount == gen + 1) {
> > update_children = false;
> > wait_var_event_spinlock(&conn->scan_seqcount,
> > conn->scan_seqcount >= gen + 2,
> > &conn->lock);
> > }
> > }
> > /* Scan we needed is guaranteed to be complete */
> > if (!update_children || conn->scan_seqcount >= gen + 2) {
> > spin_unlock(&conn->lock);
> > return;
> > }
> > /* We will do the scan */
> > conn->scan_seqcount++;
> > conn->flags &= ~FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
> > spin_unlock(&conn->lock);
> >
> > fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
> >
> > spin_lock(&conn->lock);
> > conn->scan_seqcount++;
> > wake_up_var_locked(&conn->scan_seqcount, &conn->lock);
> > spin_unlock(&conn->lock);
> >
> > > > > Only the completion of the most recent walk can guarantee
> > > > > that all the children dentries are watched.
> > > >
> > > > So I don't think that's an issue. Look, if someone places a mark which
> > > > watches children of an inode, we either end up updating the children flags
> > > > or we wait for _UPDATE_CHIDREN_RUNNING to clear. So any mark update from
> > > > userspace blocks, until the children walk is done and then they all return.
> > > > We hold the group lock during the wait so the mark that makes the parent
> > > > watch children is guaranteed to stay attached to the inode and subsequent
> > > > mask updates on the inode all result in the conclusion that children are
> > > > watched (so fsnotify_parent() doesn't clear the children flags while we are
> > > > still walking).
> > > >
> > > > One catch in the above argument are oneshot marks for inotify & dnotify.
> > > > Inotify is actually fine since it grabs group lock when removing mark after
> > > > the event was sent (so the above argument holds). Dnotify doesn't grab group
> > > > lock when removing oneshot mark and so breaks the argument. I think we
> > > > should just make dnotify grab that lock to make our life simpler.
> > > >
> > > > Thoughts? Counter examples?
> > > >
> > >
> > > My thought is that this makes my head explode..
> > > I rather have a simpler mental model if possible, which is less
> > > dependent on all the callers and other locks.
> >
> > I understand. Not sure if the above satisfies your wishes but I couldn't
> > make it simpler...
>
> It satisfies my concerns in that the logic is local and easy to follow.
>
> Maybe it will be easier to read with a helper target variable and aim for
> conn->scan_seqcount >= target, because the gen+2 arithmetics is a bit hard
> to follow for all the cases, but not sure if the end result will be better.
>
> Anyway, this is fine as far as I am concerned.
> I assume that you will send a patch following Neil's patches.

Yeah, once Neil's patches get to VFS tree, I can do these changes and send
a patch.

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