Skip to content

Commit 391b59b

Browse files
committed
fs: better handle deep ancestor chains in is_subdir()
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 <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 3cad1bc commit 391b59b

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

fs/dcache.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,28 +3029,25 @@ EXPORT_SYMBOL(d_splice_alias);
30293029

30303030
bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
30313031
{
3032-
bool result;
3032+
bool subdir;
30333033
unsigned seq;
30343034

30353035
if (new_dentry == old_dentry)
30363036
return true;
30373037

3038-
do {
3039-
/* for restarting inner loop in case of seq retry */
3040-
seq = read_seqbegin(&rename_lock);
3041-
/*
3042-
* Need rcu_readlock to protect against the d_parent trashing
3043-
* due to d_move
3044-
*/
3045-
rcu_read_lock();
3046-
if (d_ancestor(old_dentry, new_dentry))
3047-
result = true;
3048-
else
3049-
result = false;
3050-
rcu_read_unlock();
3051-
} while (read_seqretry(&rename_lock, seq));
3052-
3053-
return result;
3038+
/* Access d_parent under rcu as d_move() may change it. */
3039+
rcu_read_lock();
3040+
seq = read_seqbegin(&rename_lock);
3041+
subdir = d_ancestor(old_dentry, new_dentry);
3042+
/* Try lockless once... */
3043+
if (read_seqretry(&rename_lock, seq)) {
3044+
/* ...else acquire lock for progress even on deep chains. */
3045+
read_seqlock_excl(&rename_lock);
3046+
subdir = d_ancestor(old_dentry, new_dentry);
3047+
read_sequnlock_excl(&rename_lock);
3048+
}
3049+
rcu_read_unlock();
3050+
return subdir;
30543051
}
30553052
EXPORT_SYMBOL(is_subdir);
30563053

0 commit comments

Comments
 (0)