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

From: Amir Goldstein

Date: Fri Nov 20 2026 - 07:43:58 EST


On Fri, Nov 20, 2026 at 10:48 AM Jan Kara <jack@xxxxxxx> 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...

You're right. It's wrong.

>
> 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?

Yeh... neither dnotify_flush() nor dnotify_handle_event() check return
value now. I'll need to look into that.

Thanks,
Amir.