From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E5911A2544; Tue, 16 Jul 2024 14:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721140000; cv=none; b=ptGhTCGDgoCGNukt/6pgwiLaXgGGZP3RXGbMLdZ/fA4foJ8riiOU7Iy0tWod0zqBxsR6klKSCYbzw+DhCi6jqHF+6KT4zfRQqFnFxaiNCKfPC07D0TjuEIInziF7YB5kRYv0Xsa9nNR+qAsEal9IiCuzhoejcwRpWdhD3MqXJ9U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721140000; c=relaxed/simple; bh=MnGWsvST1Li4q23h4/0+i+m9oWC0LMX6t/9IiU4DRAA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fJJhTFE+fg49YnI9z4k4qAixObBv83FnDj8RQL7ZlV3Irjea5iAh5vv3di0DaM/1BcwLvcgPC5DqesOPpdkn4bgIVbCSCxf6RMmiCRdYB/WJWzgR7IUH7YOUrK3eJH1/Dg5ZDDPxGT1GpSiVsX3owN79RQEO9EewiBFTidkW3nY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uIemXVyZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uIemXVyZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6F0F2C4AF0D; Tue, 16 Jul 2024 14:26:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721140000; bh=MnGWsvST1Li4q23h4/0+i+m9oWC0LMX6t/9IiU4DRAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uIemXVyZ8Cd9Jn3bxNzYJuqpcbx23PXyBi6ovijoHAgq6R749FEJowFsaFSXy3zel tUlDa/4oJJ2Xxe9kJ8vxFWU3kCLFqxAE6VGtpjGicpond500J19ovS0hO1PUrTR0BN OvTqPG+qEUzu612JnFLkeDMzP+vw6r4NHabtpDmhuwUD6y1Wng8+HgfZ9J0SV+5U76 taXlye59jSx09W40eBDtipzWOBTThFdtSM2ELWcAY4eqxxuN8kAz796NXfhiS8judi RWZvAJTaTKQ+5Dxpz014INVJycqhQJjalmYiHQ0iZWAqbyvPwhs+3eu4rt09JjNVgV oadhc5egE4NvA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Christian Brauner , Jan Kara , Linus Torvalds , Sasha Levin , viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 6.9 19/22] fs: better handle deep ancestor chains in is_subdir() Date: Tue, 16 Jul 2024 10:24:26 -0400 Message-ID: <20240716142519.2712487-19-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240716142519.2712487-1-sashal@kernel.org> References: <20240716142519.2712487-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.9.9 Content-Transfer-Encoding: 8bit From: Christian Brauner [ Upstream commit 391b59b045004d5b985d033263ccba3e941a7740 ] Jan reported that 'cd ..' may take a long time in deep directory hierarchies under a bind-mount. If concurrent renames happen it is possible to livelock in is_subdir() because it will keep retrying. Change is_subdir() from simply retrying over and over to retry once and then acquire the rename lock to handle deep ancestor chains better. The list of alternatives to this approach were less then pleasant. Change the scope of rcu lock to cover the whole walk while at it. A big thanks to Jan and Linus. Both Jan and Linus had proposed effectively the same thing just that one version ended up being slightly more elegant. Reported-by: Jan Kara Signed-off-by: Linus Torvalds Signed-off-by: Christian Brauner Signed-off-by: Sasha Levin --- fs/dcache.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index 71a8e943a0fa5..e38d2d6a37e12 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -3029,28 +3029,25 @@ EXPORT_SYMBOL(d_splice_alias); bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry) { - bool result; + bool subdir; unsigned seq; if (new_dentry == old_dentry) return true; - do { - /* for restarting inner loop in case of seq retry */ - seq = read_seqbegin(&rename_lock); - /* - * Need rcu_readlock to protect against the d_parent trashing - * due to d_move - */ - rcu_read_lock(); - if (d_ancestor(old_dentry, new_dentry)) - result = true; - else - result = false; - rcu_read_unlock(); - } while (read_seqretry(&rename_lock, seq)); - - return result; + /* Access d_parent under rcu as d_move() may change it. */ + rcu_read_lock(); + seq = read_seqbegin(&rename_lock); + subdir = d_ancestor(old_dentry, new_dentry); + /* Try lockless once... */ + if (read_seqretry(&rename_lock, seq)) { + /* ...else acquire lock for progress even on deep chains. */ + read_seqlock_excl(&rename_lock); + subdir = d_ancestor(old_dentry, new_dentry); + read_sequnlock_excl(&rename_lock); + } + rcu_read_unlock(); + return subdir; } EXPORT_SYMBOL(is_subdir); -- 2.43.0