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 184C41AC442; Tue, 16 Jul 2024 14:29:43 +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=1721140183; cv=none; b=UqSkFudeo/e1sSeAzO9S2bVZcjRav8l7AEAj8n2Av0NR2mSF2MTrmddYfUJhMZZSfGr8uSpdLnx1fXYAiLU8ZW5NHdu/766ybLdC4DdLqPTks368J0/qY92m4HFf/GT8y3ToRRjSD5TiuFpLsKzuC6boTyp21wYu8F6JWdRL+XQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721140183; c=relaxed/simple; bh=3FXE5Ok1csVYIeTMkjs77bGKjWGS+bJQTjJSpmx8bgA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E1Ox+blofczc7BIgfoRoUP+zeUEJGVwot9RRMhCmvRlzvYnAh7RZCoHSqvy5HSfdFgZKNdLKTaayDQQajvzbmw66nFErreETkEAUt89MFQrKFC0qikPLig1ldqd2TvHP/B32heZvmW2NTljLE7mGoQtxbrjclPVzvFh6wI30gqw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=F4QyU72G; 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="F4QyU72G" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DCDEEC116B1; Tue, 16 Jul 2024 14:29:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721140183; bh=3FXE5Ok1csVYIeTMkjs77bGKjWGS+bJQTjJSpmx8bgA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F4QyU72GSBIfQnqa9imkzXE9M2cy4EP+M6NkkUPNpWKSbAz8BWR5bqjXMIgXmQg+5 F1lmu/SZpaZ6Q4V3e0detd0NlKOPN2jtMWc3TvWPpKkiqy6f+JCONa+qDPT5KCi7Gx MHlOXHj5aoxB+pZrCjEVHrxKQD0iqrf3zUMjgWuOslC/ok26RNEDGYj9ZiJr28Rupr oQYwmdaEs7r2cnsuYkq6mA/0sE4sScmQrTUjgntERW3/6kO+JunZndj/c2qg8C/lb1 R0gSNld5V9Thtb6salLcChFXGpLOPEpRkL1oy+8XTtZIBcD1Na3SU36gtEjj7QunWG HXbDTDa07bDOQ== 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 5.15 8/9] fs: better handle deep ancestor chains in is_subdir() Date: Tue, 16 Jul 2024 10:29:10 -0400 Message-ID: <20240716142920.2713829-8-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240716142920.2713829-1-sashal@kernel.org> References: <20240716142920.2713829-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 5.15.162 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 422c440b492a5..72ac6a03d7bea 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -3121,28 +3121,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