From nobody Fri Oct 3 08:50:47 2025 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA5342EC55B for ; Wed, 3 Sep 2025 09:34:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756892070; cv=none; b=WBKJFlgkXSTf/WnYbw+dprUf4qHnqFcC8c212imJ28xmZ/uY9SAfBGUPbhjBOtHszJE4NAMh3LvNfW+0JzSTpbD+eTeZk6RW0l+a9V6qjJFe2jW55AyA51JEtc6lj/1OXnSsW5O4u4lnRX85Ae1mN2LrheF29xwc05Jc3+ImQhY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756892070; c=relaxed/simple; bh=mqiBnPPXMAPCJPBFrcXf+lHIv/LU5ou+i6Dp8Hv/Zk0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h2aiIxU2Crh5aspFH6n1AZoAtMrIQN8zM13SoVSgLRxS+ApuJxhDQeqlKDeQV1wlMoQvh49eIAI02x0rx2F6u0iZSGw++zIVzZVEzcRQOH5CVVQhMrVPQAKziFQLPWhlvIJuB9l9t+zqdg2fj8Dmk1TcWoQfyVrCcITR8ltuxg0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=drYgVRia; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="drYgVRia" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-772488c78bcso3645647b3a.1 for ; Wed, 03 Sep 2025 02:34:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1756892068; x=1757496868; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=H8+JgdG34uJkrarV50U4TwoOp7vca+o3Si3HJ74OVuk=; b=drYgVRia/t4mnF03Nm1XVhTmJyoQQ+5bZhxUMdg3hK5AKbaUktx9Scpo5svgMaDZuE 6CCSyOeWykrJpwUgIrBcI/94B6zZ1TW5DtHnEJG7IFgP9YuceB3waTgbb/IfxCBjE4WG OfoQDApnE6ZAwuyXfLFboQp+lhDnY47rdEQ7mGVglum6hQhHj+bYCHiy42HbDTId1K81 QBQ1lQr6rDJIeSsWcKXy2PkRk1jMvR62jHBeJ8sk9HqXNKG5F+QIVUmh1jCe99IjQ8x8 hz/C1MoFIZ4ayDIsA3W1bAa2XNQbmoIfa1Qc9dxhIAXqDIlN16UDqK39ERkxvm/I+iFA cmeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756892068; x=1757496868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H8+JgdG34uJkrarV50U4TwoOp7vca+o3Si3HJ74OVuk=; b=oqOK00KtDFMx1pryJtUlbiGrjhmh/hBHBCtnFw51ZfHqgMV7VzLgP+ogqMadoqPmoG I/rrqJJwI59Qhbqszi7HD4br9lHscBzYuXANnlksOJy1GGiJ6rlC06zQHrhjxMtSgHRy gSOtAICIBDAc4JDUmEs9/AqVWuusjpzszZMghd3RjWCvmfrPdMkLGSEcWigcdNqhEf7I Huez/EMrS4wxdr8jCTIR74LV+L+2orry85pKmA7tBfW/8zU1WDQ5/bH0C3i5nrPSn4pH ZseMAeIMgJZVZqBQjyW5IrvmMDBU4Jedoau2uPj2bgcloIOWwhcsPylxBUtzgFNFSWxC AetQ== X-Forwarded-Encrypted: i=1; AJvYcCUGWiNCbLAi+htbbQO7GYA2kjLFfMdN9GytYMvMSF8fTJqGWE+mPxiVET3W0juOBCCysohfcCMs4CpisnE=@vger.kernel.org X-Gm-Message-State: AOJu0YytEigciO4bFbT6JFxWdjHmpMIFjf/ppLS+5kJdSgf3xhDPk/so xryLMPsUqNfyQtSClQghmuIakAxtI3xDB1OuDs7zDb1YSCY0Qb0gEEpvlr5qSxd8Fic= X-Gm-Gg: ASbGncvvIaz7/jNxUo17C/Y1IIUX1+hXBYfv2rW3yMxVND+20QvQEQNXAQJxvbrWCGt Q+l01vXq4cospqRCe+q7AAdldGCBhIqfqLX6T/3/E7Dswfc9kGiPJhoOWdgYYCmfTI7jJYgrAkH YCOkcatuohsuayAnjwS4XBuY4gmW7ocRKsu5yFOyckxuW1Oh/4jQ8bfPM+6GQV/IYi/94iwQY9D NmRu1p7rMKQKKv6X9SPgomjAJWZSlzQJ4uLeqdp4WcZywsIP1OAevpiHVXZworALIfwymFHT0St NHJXK1lTr9JH4iAuVy0e13jhaYcgkwhQ0fi2y6afrjJppLR1F7Y1SgWxlePPXdqgcIFu5DNy4Jc 5tkEfirAK008Tg9feXbOw3r0m40Q1UfSwZX4l69KGIf6TnMPbM64nV4EXLKn+aQqFsGZ3MPef6k XJSgIdLVNPNQ== X-Google-Smtp-Source: AGHT+IEmp8gIpdAuYX7o1UeCnn/wvhN/DI1CzgzRc15udjdmJwcMX/9ME+0o5g/Bzt3J9gqcWZYAyA== X-Received: by 2002:a05:6a20:9148:b0:23f:fec8:9ab0 with SMTP id adf61e73a8af0-243d6e690ffmr23344873637.25.1756892068057; Wed, 03 Sep 2025 02:34:28 -0700 (PDT) Received: from H7GWF0W104.bytedance.net ([61.213.176.57]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a5014desm16171734b3a.92.2025.09.03.02.34.24 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 03 Sep 2025 02:34:27 -0700 (PDT) From: Diangang Li To: jack@suse.cz, amir73il@gmail.com, stephen.s.brennan@oracle.com, changfengnan@bytedance.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Sasha Levin , Diangang Li Subject: [RFC 1/1] fsnotify: clear PARENT_WATCHED flags lazily Date: Wed, 3 Sep 2025 17:34:13 +0800 Message-ID: <20250903093413.3434-2-lidiangang@bytedance.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250903093413.3434-1-lidiangang@bytedance.com> References: <20250903093413.3434-1-lidiangang@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Amir Goldstein commit 172e422ffea2 ("fsnotify: clear PARENT_WATCHED flags lazily") In some setups directories can have many (usually negative) dentries. Hence __fsnotify_update_child_dentry_flags() function can take a significant amount of time. Since the bulk of this function happens under inode->i_lock this causes a significant contention on the lock when we remove the watch from the directory as the __fsnotify_update_child_dentry_flags() call from fsnotify_recalc_mask() races with __fsnotify_update_child_dentry_flags() calls from __fsnotify_parent() happening on children. This can lead upto softlockup reports reported by users. Fix the problem by calling fsnotify_update_children_dentry_flags() to set PARENT_WATCHED flags only when parent starts watching children. When parent stops watching children, clear false positive PARENT_WATCHED flags lazily in __fsnotify_parent() for each accessed child. Suggested-by: Jan Kara Signed-off-by: Amir Goldstein Signed-off-by: Stephen Brennan Signed-off-by: Jan Kara Signed-off-by: Sasha Levin Signed-off-by: Diangang Li Reviewed-by: Amir Goldstein --- fs/notify/fsnotify.c | 31 +++++++++++++++++++++---------- fs/notify/fsnotify.h | 2 +- fs/notify/mark.c | 32 +++++++++++++++++++++++++++++--- include/linux/fsnotify_backend.h | 8 +++++--- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index f44e39c68328..d5757fd69f62 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -105,17 +105,13 @@ void fsnotify_sb_delete(struct super_block *sb) * parent cares. Thus when an event happens on a child it can quickly tel= l if * if there is a need to find a parent and send the event to the parent. */ -void __fsnotify_update_child_dentry_flags(struct inode *inode) +void fsnotify_set_children_dentry_flags(struct inode *inode) { struct dentry *alias; - int watched; =20 if (!S_ISDIR(inode->i_mode)) return; =20 - /* determine if the children should tell inode about their events */ - watched =3D fsnotify_inode_watches_children(inode); - spin_lock(&inode->i_lock); /* run all of the dentries associated with this inode. Since this is a * directory, there damn well better only be one item on this list */ @@ -131,10 +127,7 @@ void __fsnotify_update_child_dentry_flags(struct inode= *inode) continue; =20 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); - if (watched) - child->d_flags |=3D DCACHE_FSNOTIFY_PARENT_WATCHED; - else - child->d_flags &=3D ~DCACHE_FSNOTIFY_PARENT_WATCHED; + child->d_flags |=3D DCACHE_FSNOTIFY_PARENT_WATCHED; spin_unlock(&child->d_lock); } spin_unlock(&alias->d_lock); @@ -142,6 +135,24 @@ void __fsnotify_update_child_dentry_flags(struct inode= *inode) spin_unlock(&inode->i_lock); } =20 +/* + * Lazily clear false positive PARENT_WATCHED flag for child whose parent = had + * stopped watching children. + */ +static void fsnotify_clear_child_dentry_flag(struct inode *pinode, + struct dentry *dentry) +{ + spin_lock(&dentry->d_lock); + /* + * d_lock is a sufficient barrier to prevent observing a non-watched + * parent state from before the fsnotify_set_children_dentry_flags() + * or fsnotify_update_flags() call that had set PARENT_WATCHED. + */ + if (!fsnotify_inode_watches_children(pinode)) + dentry->d_flags &=3D ~DCACHE_FSNOTIFY_PARENT_WATCHED; + spin_unlock(&dentry->d_lock); +} + /* Notify this dentry's parent about a child's events. */ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u3= 2 mask) { @@ -159,7 +170,7 @@ int __fsnotify_parent(const struct path *path, struct d= entry *dentry, __u32 mask p_inode =3D parent->d_inode; =20 if (unlikely(!fsnotify_inode_watches_children(p_inode))) { - __fsnotify_update_child_dentry_flags(p_inode); + fsnotify_clear_child_dentry_flag(p_inode, dentry); } else if (p_inode->i_fsnotify_mask & mask & ALL_FSNOTIFY_EVENTS) { struct name_snapshot name; =20 diff --git a/fs/notify/fsnotify.h b/fs/notify/fsnotify.h index f3462828a0e2..0ac1f8e8e012 100644 --- a/fs/notify/fsnotify.h +++ b/fs/notify/fsnotify.h @@ -59,7 +59,7 @@ static inline void fsnotify_clear_marks_by_sb(struct supe= r_block *sb) * update the dentry->d_flags of all of inode's children to indicate if in= ode cares * about events that happen to its children. */ -extern void __fsnotify_update_child_dentry_flags(struct inode *inode); +extern void fsnotify_set_children_dentry_flags(struct inode *inode); =20 /* allocate and destroy and event holder to attach events to notification/= access queues */ extern struct fsnotify_event_holder *fsnotify_alloc_event_holder(void); diff --git a/fs/notify/mark.c b/fs/notify/mark.c index fdf8e03bf3df..ba4953b0917d 100644 --- a/fs/notify/mark.c +++ b/fs/notify/mark.c @@ -132,6 +132,24 @@ static void __fsnotify_recalc_mask(struct fsnotify_mar= k_connector *conn) *fsnotify_conn_mask_p(conn) =3D new_mask; } =20 +static bool fsnotify_conn_watches_children( + struct fsnotify_mark_connector *conn) +{ + if (conn->type !=3D FSNOTIFY_OBJ_TYPE_INODE) + return false; + + return fsnotify_inode_watches_children(fsnotify_conn_inode(conn)); +} + +static void fsnotify_conn_set_children_dentry_flags( + struct fsnotify_mark_connector *conn) +{ + if (conn->type !=3D FSNOTIFY_OBJ_TYPE_INODE) + return; + + fsnotify_set_children_dentry_flags(fsnotify_conn_inode(conn)); +} + /* * Calculate mask of events for a list of marks. The caller must make sure * connector and connector->obj cannot disappear under us. Callers achieve @@ -140,15 +158,23 @@ static void __fsnotify_recalc_mask(struct fsnotify_ma= rk_connector *conn) */ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) { + bool update_children; + if (!conn) return; =20 spin_lock(&conn->lock); + update_children =3D !fsnotify_conn_watches_children(conn); __fsnotify_recalc_mask(conn); + update_children &=3D fsnotify_conn_watches_children(conn); spin_unlock(&conn->lock); - if (conn->type =3D=3D FSNOTIFY_OBJ_TYPE_INODE) - __fsnotify_update_child_dentry_flags( - fsnotify_conn_inode(conn)); + /* + * 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); } =20 /* Free all connectors queued for freeing once SRCU period ends */ diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_back= end.h index 64cfb5446f4d..d7ced985ffee 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -360,12 +360,14 @@ extern u32 fsnotify_get_cookie(void); =20 static inline int fsnotify_inode_watches_children(struct inode *inode) { + __u32 parent_mask =3D READ_ONCE(inode->i_fsnotify_mask); + /* FS_EVENT_ON_CHILD is set if the inode may care */ - if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD)) + if (!(parent_mask & FS_EVENT_ON_CHILD)) return 0; /* this inode might care about child events, does it care about the * specific set of events that can happen on a child? */ - return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD; + return parent_mask & FS_EVENTS_POSS_ON_CHILD; } =20 /* @@ -379,7 +381,7 @@ static inline void fsnotify_update_flags(struct dentry = *dentry) /* * Serialisation of setting PARENT_WATCHED on the dentries is provided * by d_lock. If inotify_inode_watched changes after we have taken - * d_lock, the following __fsnotify_update_child_dentry_flags call will + * d_lock, the following fsnotify_set_children_dentry_flags call will * find our entry, so it will spin until we complete here, and update * us with the new state. */ --=20 2.39.5