Re: [PATCH v6 0/5] Fix easy bits of the negative dentry problem
From: Jan Kara
Date: Thu Nov 26 2026 - 05:57:46 EST
On Wed 25-11-26 18:39:20, Amir Goldstein wrote:
> On Wed, Nov 25, 2026 at 12:34 PM Jan Kara <jack@xxxxxxx> wrote:
> >
> > On Wed 25-11-26 08:46:45, Amir Goldstein wrote:
> > > 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.
> >
> > Yes, the window can be larger now but it still shouldn't be a practical
> > problem unless there are several watchers placing marks on insanely large
> > dir in parallel. So I think we can live with the race for a while longer :)
> >
> > > > > 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).
> >
> > Yes, I was also concerned about those races when thinking about that
> > yesterday. Let's discuss below.
> >
> > > Are you concerned over the size of the mark connector?
> > > I think that connectors should be rare and scarce.
> >
> > Yes, I was concerned about the size of the connector. On a second thought
> > I tend to agree it wouldn't be a huge deal if we need it.
> >
> > > 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.
> >
> > Well, let's discuss the behavior of the walks and the flags. I imagine we
> > could have two bits like _UPDATE_CHILDREN and _UPDATE_CHIDREN_RUNNING.
> > When children flags need update we set _UPDATE_CHILDREN. When we see
> > _UPDATE_CHILDREN set, we call fsnotify_conn_set_children_dentry_flags().
> > That function checks _UPDATE_CHIDREN_RUNNING. If already set, it waits for
> > it to get cleared and returns. If not set, it sets _UPDATE_CHIDREN_RUNNING,
> > does the walk and clears both _UPDATE_CHILDREN and _UPDATE_CHIDREN_RUNNING.
> >
>
> Yeh, I think that mostly works, with a minor variation.
>
> If the RUNNING flag is already set, the function waits for it to get
> cleared and then checks the UPDATE flag again.
> If the UPDATE flag is set, it clears the UPDATE flag, sets RUNNING
> and starts the walk. At the end of the walk it clears only RUNNING,
> so every time UPDATE is set, a walk starts after it.
>
> Anyway, serializing the walks per inode is a positive outcome
> and better for our mental model.
I agree. The only thing to watch out is that if the user could fast enough
switch the inode between "children watched" and "no children watched" during
the walk, he could hold people waiting on RUNNING practically indefinitely.
And indeed with the example you mentioned below - one group with mark with
FS_EVENT_ON_CHILD | FS_CREATE, another group with mark with FS_OPEN - would
create a situation where no single mark is watching children but together
they create a mask which results in watching children. And by constantly
adding and removing these marks userspace can stall other waiters on
RUNNING flag without any bound (it depends only whether the victim task gets
scheduled and rechecks RUNNING flag before malicious task wakes up, clears
UPDATE, and sets RUNNING again).
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...
Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR