From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387392; b=jprDf/UW5/HtlPkUOsV7ivmxFGdXVXjnppzm9cJKaRomafWJS0iIeaeznXV+n+F+BjHS xkILHQfbY7VE07Z8laIzpdrcWtdiIp0xRMc/1/rwAt1qrdXwgixoJ2YwNKJ54kcHq7x1x Lv1RjXXEMXzIIY66XDMF/danWJLsz/nGr89Axgjytf2kQtAPYUwoVCjh9pPcG6dUM7uVT DhGFCqms4a8sS/0zgU/hGDwunZnOfMImVY1q7kmCf+K/x8onkuzlHcDl08RjyYDkGlGPN wsuBimuN2mjjEjaSAjLNVIDXr+DagWrH8TLiqfIss+9grcuQ9lS0geGDjy8ZVopDiZA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387392; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=VUPi9kwwKvrVFBDW/fIbhPN+kLST3yjwYsjmvadv6Pk=; b=Tj1/9BN4XlVDN9GjsMx8gfK9EuUPOivoUd2TYeEhoSfBGDx+JtfEGr4mdK33YaPHgGUt P44Xnf+wbByXlNZGoTJyYRQD0B/iuCZ7yniCnkHFz0rbHKwDYJRyJyiVFAirnXwQakD1t JRzv8LvBZ8ihwGpPFQSP4OwXCHzuWdxi1ODhpVia//CRSEbLWuWy3GLWl3+t3CjwaXVU3 DTlOamBsfdPzrgDyRcAdGfRi2cryzMStT0oprS8jwSg3mORGspbX4iOJ1EmjUfi6/pKXS kWBeTfhQTJr2Yij6G/2bjIeZwa8N4gBN5IbPllKIkiO0rhwdCPZuCffLTE+yGMS6sHg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cicO8VXa; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68A66606A322; Fri, 4 Dec 2026 12:29:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cicO8VXa Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F6421F000CE; Fri, 4 Dec 2026 12:29:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387391; bh=VUPi9kwwKvrVFBDW/fIbhPN+kLST3yjwYsjmvadv6Pk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cicO8VXaN+3kLjzrCxTM6TA6ykTEml0Dx4suRtRqKuCRej5FuADvBvp7eOKFX40i3 KvFcbl7UBJ5B7OgOoAGXZJwFxfd/mCnE7vdgvRm2obacMy9fNUvzHMsYFJJE9pQiaU V2TqiTQe/BuEdjgETkCM9Q5qet/+7VCaqkzsyraw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Amir Goldstein , Jan Kara , "Christian Brauner (Amutable)" , Sasha Levin , Daehyeon Ko <4ncienth@gmail.com> Subject: [PATCH 6.1 0224/1059] fs: make sure to call fsnotify_inoderemove() only once when inode is dead Date: Fri, 4 Dec 2026 18:55:55 +0900 Message-ID: <20261204100650.077582515@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Amir Goldstein [ Upstream commit 669bb5af15d41a21359d00a441490f87a88eb52a ] Daehyeon reported a race wherein fsnotify_removeinode() can be called when inode still has a live alias. This can lead to several undesired outcomes such as crashes in fsnotify code or avoidance of inode mark notifications. Check that inode is really "dead", meaning no nlink and no more aliases before calling fsnotify_inoderemove() for final cleanup of inode marks. Also do not allow open_by_handle() to add new aliases to a "dead" inode. Ideally, we'd call fsnotify_inoderemove() once last inode reference is dropped to make things simpler. Alas fsnotify inode marks currently hold inode references and thus this doesn't work. Once we teach fsnotify to avoid holding inode references, these games with dead inodes can go as well. Reported-and-tested-by: Daehyeon Ko <4ncienth@gmail.com> Closes: https://lore.kernel.org/linux-fsdevel/20260827045007.3831259-1-4ncienth@gmail.com/ CC: stable@vger.kernel.org Signed-off-by: Amir Goldstein Signed-off-by: Jan Kara Link: https://patch.msgid.link/20260902164924.1333135-2-jack@suse.cz Signed-off-by: Christian Brauner (Amutable) [ sashal: Moved the alias recheck from __d_obtain_alias() into __d_instantiate_anon() and adapted context lines to the older dcache (d_u.d_rcu, hlist_del_init of d_u.d_alias, no waiters). ] Signed-off-by: Sasha Levin --- fs/dcache.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index a03646aff093d..5065c16af16d4 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -381,6 +381,17 @@ static void dentry_free(struct dentry *dentry) call_rcu(&dentry->d_u.d_rcu, __d_free); } +/* + * If inode is unlinked and doesn't have any aliases (i.e., all fds pointing to + * it are closed), it is pretty much dead. Except that file handle lookup could + * still revive it which causes issues to fsnotify. So once inode reaches this + * state we make sure to block creating any new aliases. + */ +static bool inode_notify_dead(struct inode *inode) +{ + return !inode->i_nlink && hlist_empty(&inode->i_dentry); +} + /* * Release the dentry's inode, using the filesystem * d_iput() operation if defined. @@ -390,14 +401,16 @@ static void dentry_unlink_inode(struct dentry * dentry) __releases(dentry->d_inode->i_lock) { struct inode *inode = dentry->d_inode; + bool notify_dead; raw_write_seqcount_begin(&dentry->d_seq); __d_clear_type_and_inode(dentry); hlist_del_init(&dentry->d_u.d_alias); raw_write_seqcount_end(&dentry->d_seq); + notify_dead = inode_notify_dead(inode); spin_unlock(&dentry->d_lock); spin_unlock(&inode->i_lock); - if (!inode->i_nlink) + if (notify_dead) fsnotify_inoderemove(inode); if (dentry->d_op && dentry->d_op->d_iput) dentry->d_op->d_iput(dentry, inode); @@ -2096,7 +2109,10 @@ static struct dentry *__d_instantiate_anon(struct dentry *dentry, security_d_instantiate(dentry, inode); spin_lock(&inode->i_lock); - res = __d_find_any_alias(inode); + if (!inode_notify_dead(inode)) + res = __d_find_any_alias(inode); + else + res = ERR_PTR(-ESTALE); if (res) { spin_unlock(&inode->i_lock); dput(dentry); @@ -2143,7 +2159,12 @@ static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected) if (IS_ERR(inode)) return ERR_CAST(inode); - res = d_find_any_alias(inode); + spin_lock(&inode->i_lock); + if (!inode_notify_dead(inode)) + res = __d_find_any_alias(inode); /* existing alias? */ + else + res = ERR_PTR(-ESTALE); + spin_unlock(&inode->i_lock); if (res) goto out_iput; -- 2.53.0