Re: [PATCH v5 2/5] fsnotify: update children flags only when adding mask bits

From: Jan Kara

Date: Mon Nov 23 2026 - 04:55:53 EST


On Mon 23-11-26 13:08:45, NeilBrown wrote:
> On Fri, 20 Nov 2026, Jan Kara wrote:
> > On Thu 19-11-26 13:57:02, NeilBrown wrote:
> > > From: Amir Goldstein <amir73il@xxxxxxxxx>
> > >
> > > Since we only ever set the PARENT_WATCHED flag from
> > > fsnotify_recalc_mask(), there is never a need to update children flags
> > > when removing mark mask bits only when adding them.
> > >
> > > Add a helper fsnotify_recalc_mask_and_update_children() to be called
> > > in cases where updating children flags may be needed (i.e. add mark).
> > >
> > > Also avoid update children walk with mark spinlock held.
> > > This was only done by dnotify and can be easily avoided.
> > >
> > > neilb: added update_children flag in fcntl_dirnotify() so we only
> > > do the update when needed.
> > >
> > > Fixes: 172e422ffea20 ("fsnotify: clear PARENT_WATCHED flags lazily")
> > > Signed-off-by: Amir Goldstein <amir73il@xxxxxxxxx>
> > > Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
> >
> > One question to Amir below. Otherwise I like it.
> >
> > > +/*
> > > + * Recalculate mask and update children dentry PARENT_WATCHED flags if the
> > > + * connector started watching children. When parent stops watching, we
> > > + * clear false positive PARENT_WATCHED flags lazily in __fsnotify_parent().
> > > + *
> > > + * The caller must make sure connector and connector->obj cannot disappear
> > > + * under us. This is achieved by holding mark->group->mark_mutex for a
> > > + * mark on this list. mark->lock must not be held by the caller.
> > > + */
> > > +void fsnotify_recalc_mask_and_update_children(
> > > + struct fsnotify_mark_connector *conn)
> > > +{
> > > + if (fsnotify_recalc_mask(conn))
> > > fsnotify_conn_set_children_dentry_flags(conn);
> > > }
> > >
> > > @@ -367,8 +383,8 @@ void fsnotify_modify_mark_mask(struct fsnotify_mark *mark, u32 set, u32 clear)
> > > recalc = true;
> > > spin_unlock(&mark->lock);
> > >
> > > - if (recalc)
> > > - fsnotify_recalc_mask(mark->connector);
> > > + if (recalc && fsnotify_recalc_mask(mark->connector) && set)
> >
> > Amir, why is the '&& set' check here? Under normal circumstances if
> > fsnotify_recalc_mask() return's true, 'set' is true as well. We can be
> > racing with other updates to the connector list of marks and in that case our
> > fsnotify_recalc_mask() call could be the first one that noticed the marks
> > attached to the inode are now watching children and is thus resposible for
> > setting children dentry flags even if 'set' was false. So I think it can do
> > only harm...
> >
> > And for similar reasons I believe fsnotify_recalc_mask() call in
> > fanotify_remove_mark() needs to be updated to
> > fsnotify_recalc_mask_and_update_children() as well. Standalone
> > fsnotify_recalc_mask() is simply never safe unless it is followed by the
> > update children later - perhaps make it __must_check?
>
> Hmmm.... I had thought that the group lock would prevent these races but
> I guess not. Maybe one mark can be in multiple groups???

Mark belongs to a single group. But fsnotify_recalc_mask() calculates mask
from all marks attached to an inode and different marks can belong to
different groups. So group->lock indeed doesn't protect us from parallel
changes to the list.

> Given that, I don't think I can form a sensible opinion on Amir's
> revised patch. It looks OK and I think I can see what it is trying to
> achieve and how it achieves it, but I'm less confident that I can see if
> that is the thing that needs to be achieved.
>
> So Jan - if you could review that so I can add your reviewed-by when I
> repost, that would be great.

Yes, the patch looks good to me now.

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