Re: [PATCH v2 4/6] fs/dcache: Avoid the try_lock loops in dentry_kill()

From: Al Viro
Date: Fri Feb 23 2018 - 00:46:36 EST


On Fri, Feb 23, 2018 at 03:12:14AM +0000, Al Viro wrote:

> failed:
> spin_unlock(&dentry->d_lock);
> spin_lock(&inode->i_lock);
> spin_lock(&dentry->d_lock);
> parent = lock_parent(dentry);

Hmm... Negative dentry case obviously is trickier - not to mention oopsen,
it might have become positive under us. Bugger... OTOH, it's not much
trickier - with negative dentry we can only fail on trying to lock the
parent, in which case we should just check that it's still negative before
killing it off. If it has gone positive on us, we'll just unlock the
parent and we are back to the normal "positive dentry, only ->d_lock held"
case. At most one retry there - once it's positive, it stays positive.
So,

static struct dentry *dentry_kill(struct dentry *dentry)
__releases(dentry->d_lock)
{
struct inode *inode = dentry->d_inode;
struct dentry *parent = NULL;

if (inode && unlikely(!spin_trylock(&inode->i_lock)))
goto no_locks;

if (!IS_ROOT(dentry)) {
parent = dentry->d_parent;
if (unlikely(!spin_trylock(&parent->d_lock))) {
if (inode) {
spin_unlock(&inode->i_lock);
goto no_locks;
}
goto need_parent;
}
}
kill_it:
__dentry_kill(dentry);
return parent;

no_locks: /* positive, only ->d_lock held */
spin_unlock(&dentry->d_lock);
spin_lock(&inode->i_lock);
spin_lock(&dentry->d_lock);
need_parent:
parent = lock_parent(dentry);
if (unlikely(dentry->d_lockref.count != 1 || retain_dentry(dentry))) {
/* we are keeping it, after all */
if (inode)
spin_unlock(&inode->i_lock);
spin_unlock(&dentry->d_lock);
if (parent)
spin_unlock(&parent->d_lock);
return NULL;
}
/* it should die */
if (inode) /* was positive, ->d_inode unchanged, locks held */
goto kill_it;
inode = dentry->d_inode; // READ_ONCE?
if (!inode) /* still negative, locks held */
goto kill_it;
/* negative became positive; it can't become negative again */
if (parent)
spin_unlock(&parent->d_lock);
goto no_locks; /* once */
}