[PATCH v6 2/5] fsnotify: update children flags only when parent starts watching children
From: NeilBrown
Date: Mon Nov 23 2026 - 17:14:38 EST
From: Amir Goldstein <amir73il@xxxxxxxxx>
We only need to set the PARENT_WATCHED flag on children when the parent
inode starts watching children. When the parent stops watching, false
positive PARENT_WATCHED flags are cleared lazily in __fsnotify_parent().
The recalc that observes the parent starting to watch children is not
necessarily the recalc of the mark that added the watch. It may be the
recalc of a concurrent remover in another group, or a recalc in a context
that cannot walk the children, such as dnotify event handling.
Record the need to update children flags in a connector flag when the
recalc observes the transition and let callers that are allowed to walk
the children consume the flag via fsnotify_conn_set_children_dentry_flags()
after mask recalc. This way, the caller that added the watch is
guaranteed to either update the children flags or find that another
caller has already done so.
Also avoid the update children walk with mark spinlock held. This was
only done by dnotify and can be easily avoided.
Fixes: 172e422ffea20 ("fsnotify: clear PARENT_WATCHED flags lazily")
Reviewed-by: Jan Kara <jack@xxxxxxx>
Signed-off-by: Amir Goldstein <amir73il@xxxxxxxxx>
Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
---
fs/notify/dnotify/dnotify.c | 6 +++
fs/notify/fanotify/fanotify_user.c | 4 +-
fs/notify/inotify/inotify_user.c | 2 +-
fs/notify/mark.c | 86 +++++++++++++++++++++---------
include/linux/fsnotify_backend.h | 6 +++
5 files changed, 77 insertions(+), 27 deletions(-)
diff --git a/fs/notify/dnotify/dnotify.c b/fs/notify/dnotify/dnotify.c
index 9fb73bafd41d..0afd2b039333 100644
--- a/fs/notify/dnotify/dnotify.c
+++ b/fs/notify/dnotify/dnotify.c
@@ -181,6 +181,11 @@ void dnotify_flush(struct file *filp, fl_owner_t id)
}
spin_unlock(&fsn_mark->lock);
+ /*
+ * We only remove mask bits here, but our recalc may be the first to
+ * observe another group starting to watch children.
+ */
+ fsnotify_conn_set_children_dentry_flags(fsn_mark->connector);
/* nothing else could have found us thanks to the dnotify_groups
mark_mutex */
@@ -380,6 +385,7 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned int arg)
dnotify_recalc_inode_mask(fsn_mark);
out:
spin_unlock(&fsn_mark->lock);
+ fsnotify_conn_set_children_dentry_flags(fsn_mark->connector);
if (destroy)
fsnotify_detach_mark(fsn_mark);
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 63c9759fc3b0..c064b0070c51 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -1264,7 +1264,7 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
umask, &destroy_mark);
if (removed & fsnotify_conn_mask(fsn_mark->connector))
- fsnotify_recalc_mask(fsn_mark->connector);
+ fsnotify_recalc_mask_and_update_children(fsn_mark->connector);
if (destroy_mark)
fsnotify_detach_mark(fsn_mark);
fsnotify_group_unlock(group);
@@ -1538,7 +1538,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,
recalc = fanotify_mark_add_to_mask(fsn_mark, mask, fan_flags);
if (recalc)
- fsnotify_recalc_mask(fsn_mark->connector);
+ fsnotify_recalc_mask_and_update_children(fsn_mark->connector);
out:
fsnotify_group_unlock(group);
diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c
index 5f19c24ec187..6018c2d717ac 100644
--- a/fs/notify/inotify/inotify_user.c
+++ b/fs/notify/inotify/inotify_user.c
@@ -562,7 +562,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
fsn_mark->flags |= inotify_arg_to_flags(arg);
spin_unlock(&fsn_mark->lock);
- fsnotify_recalc_mask(fsn_mark->connector);
+ fsnotify_recalc_mask_and_update_children(fsn_mark->connector);
/* return the wd */
ret = i_mark->wd;
diff --git a/fs/notify/mark.c b/fs/notify/mark.c
index cb07f42d3422..1658fbef61df 100644
--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -238,21 +238,37 @@ static struct inode *fsnotify_update_iref(struct fsnotify_mark_connector *conn,
return inode;
}
+static bool fsnotify_conn_watches_children(
+ struct fsnotify_mark_connector *conn)
+{
+ if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
+ return false;
+
+ return fsnotify_inode_watches_children(fsnotify_conn_inode(conn));
+}
+
/*
* Calculate mask of events for a list of marks.
*
+ * If the connector started watching children, flag it so that the next call
+ * to fsnotify_conn_set_children_dentry_flags() updates the children dentry
+ * flags. The caller that observes the transition may not be allowed to walk
+ * the children, so the update may be done by another caller.
+ *
* Return true if any of the attached marks want to hold an inode reference.
*/
static bool __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
{
u32 new_mask = 0;
bool want_iref = false;
+ bool watched_children;
struct fsnotify_mark *mark;
assert_spin_locked(&conn->lock);
/* We can get detached connector here when inode is getting unlinked. */
if (!fsnotify_valid_obj_type(conn->type))
return NULL;
+ watched_children = fsnotify_conn_watches_children(conn);
hlist_for_each_entry(mark, &conn->list, obj_list) {
if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED))
continue;
@@ -267,6 +283,13 @@ static bool __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
*/
WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask);
+ if (fsnotify_conn_watches_children(conn) != watched_children) {
+ if (watched_children)
+ conn->flags &= ~FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
+ else
+ conn->flags |= FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
+ }
+
return want_iref;
}
@@ -298,22 +321,30 @@ static void *fsnotify_recalc_mask_clear_iref(struct fsnotify_mark_connector *con
return fsnotify_update_iref(conn, want_iref);
}
-static bool fsnotify_conn_watches_children(
+/*
+ * Update children dentry PARENT_WATCHED flags if the connector started
+ * watching children since the last update. 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_conn_set_children_dentry_flags(
struct fsnotify_mark_connector *conn)
{
- if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
- return false;
-
- return fsnotify_inode_watches_children(fsnotify_conn_inode(conn));
-}
+ bool update_children;
-static void fsnotify_conn_set_children_dentry_flags(
- struct fsnotify_mark_connector *conn)
-{
- if (conn->type != FSNOTIFY_OBJ_TYPE_INODE)
+ if (!conn || conn->type != FSNOTIFY_OBJ_TYPE_INODE)
return;
- fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
+ spin_lock(&conn->lock);
+ update_children = conn->flags & FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
+ conn->flags &= ~FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN;
+ spin_unlock(&conn->lock);
+
+ if (update_children)
+ fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn));
}
/*
@@ -321,26 +352,33 @@ static void fsnotify_conn_set_children_dentry_flags(
* connector and connector->obj cannot disappear under us. Callers achieve
* this by holding a mark->lock or mark->group->mark_mutex for a mark on this
* list.
+ *
+ * Callers that may sleep should use fsnotify_recalc_mask_and_update_children()
+ * or call fsnotify_conn_set_children_dentry_flags() after dropping mark->lock.
*/
void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
{
- bool update_children;
-
if (!conn)
return;
spin_lock(&conn->lock);
- update_children = !fsnotify_conn_watches_children(conn);
fsnotify_recalc_mask_set_iref(conn);
- update_children &= fsnotify_conn_watches_children(conn);
spin_unlock(&conn->lock);
- /*
- * Set children's PARENT_WATCHED flags only if parent started watching.
- * When parent stops watching, we clear false positive PARENT_WATCHED
- * flags lazily in __fsnotify_parent().
- */
- if (update_children)
- fsnotify_conn_set_children_dentry_flags(conn);
+}
+
+/*
+ * Recalculate mask and update children dentry PARENT_WATCHED flags if the
+ * connector started watching children.
+ *
+ * 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)
+{
+ fsnotify_recalc_mask(conn);
+ fsnotify_conn_set_children_dentry_flags(conn);
}
/**
@@ -368,7 +406,7 @@ void fsnotify_modify_mark_mask(struct fsnotify_mark *mark, u32 set, u32 clear)
spin_unlock(&mark->lock);
if (recalc)
- fsnotify_recalc_mask(mark->connector);
+ fsnotify_recalc_mask_and_update_children(mark->connector);
}
EXPORT_SYMBOL_GPL(fsnotify_modify_mark_mask);
@@ -1007,7 +1045,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
if (ret)
goto err;
- fsnotify_recalc_mask(mark->connector);
+ fsnotify_recalc_mask_and_update_children(mark->connector);
return ret;
err:
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index faf21564380d..052d8674ad89 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -567,6 +567,7 @@ struct fsnotify_mark_connector {
#define FSNOTIFY_CONN_FLAG_IS_WATCHED 0x01
#define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02
#define FSNOTIFY_CONN_FLAG_DESTROYING 0x04
+#define FSNOTIFY_CONN_FLAG_UPDATE_CHILDREN 0x08
unsigned short flags; /* flags [lock] */
union {
/* Object pointer [lock] */
@@ -888,6 +889,11 @@ static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
/* Calculate mask of events for a list of marks */
extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
+/* Recalculate mask and update children dentry flags if needed */
+extern void fsnotify_recalc_mask_and_update_children(
+ struct fsnotify_mark_connector *conn);
+extern void fsnotify_conn_set_children_dentry_flags(
+ struct fsnotify_mark_connector *conn);
extern void fsnotify_init_mark(struct fsnotify_mark *mark,
struct fsnotify_group *group);
/* Find mark belonging to given group in the list of marks */
--
2.50.0.107.gf914562f5916.dirty