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 884A01586F5; Tue, 23 Jul 2024 18:44:56 +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=1721760296; cv=none; b=gRCFHMGTKzkbASWqVpFJNToDzjpuMLD5/EQpY/Hx1BkVn+Cgi+MB4lTBmM8g9A4v+aCP42VlEBZ44cJbxxGJG8NRSGFnrx0wSZJZVZdfOy3IW9Zvt1iLZVLi4ojdp5sNI4K2kiGR/Qzriybx/vQoIDrLEeRRV8rhgi+e+ZSqKzs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721760296; c=relaxed/simple; bh=St5q47khe3wfIh39CrHZRpFxWKNDleMZq4id3ubzi30=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p7ej3ykaXYmERkWJCm+JRPiZqxeusPx3pLILoLR2niWbwH37d3MIbT6lF+mJWhmFk4jx2pXbtBIBZG/eKnMPL3MenV4XxI+afEoaaVtFfOwhxfodaja1pvhlY22+I6mv4bljnBrFeQe68jcgerFlgm+iuiDjZww/VnyXyAKzdJM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=RRUgsz4c; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="RRUgsz4c" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F443C4AF09; Tue, 23 Jul 2024 18:44:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1721760296; bh=St5q47khe3wfIh39CrHZRpFxWKNDleMZq4id3ubzi30=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RRUgsz4cbY3+ve/uQ8eMESLDLJtF3wZ4JsXQGzccFBncBTA8tnC5ClPfvvUNA8Q4f YchBvcDNbqR3yAFW3PWurQAeeH3p3ekU+FvwgVURC0NHnNnPyDvaYKh7na3DBx3n2A Brw3SPE9VHkVZ3qsUgzcK0wUPgmxBFeBT5/gTQ/4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Jan Kara , Linus Torvalds , Christian Brauner , Sasha Levin Subject: [PATCH 6.9 140/163] fs: better handle deep ancestor chains in is_subdir() Date: Tue, 23 Jul 2024 20:24:29 +0200 Message-ID: <20240723180148.882113899@linuxfoundation.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723180143.461739294@linuxfoundation.org> References: <20240723180143.461739294@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.9-stable review patch. If anyone has any objections, please let me know. ------------------ 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 66515fbc9dd70..4c144519aa709 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -3035,28 +3035,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