From: NeilBrown A proposed change will invert the lock ordering between d_alloc_parallel() and inode_lock() on the parent. When that happens it will not be safe to call d_alloc_parallel() while holding the parent lock - even shared. It is already *safe* to call d_alloc_parallel() without holding the parent lock - providing any filesystem that does so doesn't d_drop or d_hash in the middle of an operation. Those filesystems which use d_add_ci() don't d_drop(). We don't need to keep the parent lock held when d_add_ci() is run - the VFS doesn't need it as dentry is exclusively held due to DCACHE_PAR_LOOKUP and the filesystem has finished its work. So drop and reclaim the lock to avoid future deadlock. Signed-off-by: NeilBrown --- fs/dcache.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index 2ad39c1515b7..ed957f090247 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -2472,17 +2472,30 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, iput(inode); return found; } + /* + * We are holding parent lock and mustn't wait + * for a d_in_lookup() dentry. We can safely drop the + * parent lock and reclaim it as we have exclusive + * access to dentry as it is d_in_lookup() (so + * ->d_parent is stable) and we are near the end + * ->lookup() and will shortly drop the lock anyway. + * We cannot retake the lock while the new dentry is in-lookup + */ + inode_unlock_shared(d_inode(dentry->d_parent)); found = d_alloc_parallel(dentry->d_parent, name); if (IS_ERR(found) || !d_in_lookup(found)) { iput(inode); - return found; + goto out_relock; } res = d_splice_alias(inode, found); if (res) { d_lookup_done(found); dput(found); - return res; + found = res; } +out_relock: + d_lookup_done(dentry); + inode_lock_shared(d_inode(dentry->d_parent)); return found; } EXPORT_SYMBOL(d_add_ci); -- 2.50.0.107.gf914562f5916.dirty