Re: [PATCH v6 0/5] Fix easy bits of the negative dentry problem
From: Amir Goldstein
Date: Wed Nov 25 2026 - 02:48:11 EST
On Tue, Nov 24, 2026 at 5:26 PM Jan Kara <jack@xxxxxxx> wrote:
>
> On Tue 24-11-26 14:08:07, Amir Goldstein wrote:
> > On Tue, Nov 24, 2026 at 7:42 AM NeilBrown <neilb@xxxxxxxxxxx> wrote:
> > >
> > > On Tue, 24 Nov 2026, NeilBrown wrote:
> > > > This v6:
> > > > - fixes the autofs changes in 1/5
> > >
> > > Sashiko says this is racy. I think it is but the original code was just
> > > as racy. Maybe the race isn't relevant. I haven't properly understood
> > > it yet.
> > >
> > > > - updates d_duplicate() in 1/5
> > >
> > > The fix was seriously broken. Sorry.
> > >
> > > > - uses a new version of 2/5 from Amir
> > >
> > > Sashiko has something to say about this
> > >
> > > Can clearing FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN and dropping conn->lock
> > > before fsnotify_set_children_dentry_flags() finishes allow a concurrent
> > > watch-adding syscall to return to userspace before the children's
> > > DCACHE_FSNOTIFY_PARENT_WATCHED flags have been set?
> > >
> > > Thoughts?
> >
> > Sashiko is right, but this race is not new.
> > The same window exists upstream: if another thread's recalc observes
> > the 0->watching transition and does the walk, fanotify_mark() can return
> > to userspace before all children have DCACHE_FSNOTIFY_PARENT_WATCHED set.
> >
> > What changes with this series is that the walk is sleepable, so the
> > window is longer, and in some workloads it could go from rare to
> > plausible. OTOH, skipping negative dentries may shorten the walk.
> >
> > The more important question is whether a child can be left without
> > PARENT_WATCHED permanently. Upstream it can: the transition can be
> > observed by a recalc from the mark destroy path, which never walks.
> > With FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN that cannot happen.
> > Every transition sets the flag, and every caller that can add a watch
> > consumes it,
> > so a walk that started after the transition always runs and fixes the flags.
> > What remains is the transient window above.
>
> I agree this is preexisting issue. Let's not block this series with it.
preexisting, but sleepable children walks could make an
improbable race actually observable, because the scans are not
serialized and could take longer (I don't know how the scheduler
treats a dying process which has its marks removed).
Just wanted to make sure that the point is clear.
>
> > I think a proper fix for the transient window needs a sequence counter
> > of scans in the connector. Each caller would wait until a scan that
> > started after its transition has completed, no matter which thread ran it.
> >
> > I think we can live with this v6 patch for your series and deal with
> > the remaining race later if we want to, but I am not sure and would
> > like to hear what Jan thinks.
> >
> > If you want, I can implement the sequence counter for v7.
>
> No sure what exactly you have in mind but a sequence counter per inode
> looks like an overkill for this. A global one can get contended and
> a bit hairy to manage but let's see...
I had a patch with another conn flag _CHILDREN_WATCHED
set at the end of the walk, which covers the Sashiko race, but
leaves some other corner cases (see below).
Are you concerned over the size of the mark connector?
I think that connectors should be rare and scarce.
With workloads that frequently add/remove watch,
Sleepable children walks could cause a situation where
scan that started a few add/remove cycles ago complete out
or order with the most recent walk.
Only the completion of the most recent walk can guarantee
that all the children dentries are watched.
This is a concept version of the generational walks.
The _UPDATE_CHILDREN flag is redundant here
I only left it so the concept patch does not need to change recalc_mask
Let me know if you want me to post a patch on top of Neil's patch set
or in place of the v6 patch, or nothing at all.
Thanks,
Amir.
void fsnotify_conn_set_children_dentry_flags(
struct fsnotify_mark_connector *conn)
{
unsigned int gen;
bool update_children;
if (!conn || conn->type != FSNOTIFY_OBJ_TYPE_INODE)
return;
spin_lock(&conn->lock);
if (conn->flags & FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN) {
conn->flags &= ~FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
conn->children_gen++;
update_children = true;
} else {
/* Walk that consumed the update flag may still be in
progress */
update_children = conn->children_done != conn->children_gen &&
fsnotify_conn_watches_children(conn);
}
gen = conn->children_gen;
spin_unlock(&conn->lock);
if (!update_children)
return;
fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
spin_lock(&conn->lock);
/* Concurrent walks may complete out of order */
if ((int)(gen - conn->children_done) > 0)
conn->children_done = gen;
spin_unlock(&conn->lock);
}