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

From: Al Viro
Date: Thu Feb 22 2018 - 00:29:14 EST


On Tue, Feb 20, 2018 at 12:34:57AM +0100, John Ogness wrote:


> Implementation 3: The same as implementation 2 but using if's to
> support branch prediction. This approach is probably the most
> complicated to understand but will be the fastest.
>
> /*
> * Lock the inode. Might drop dentry->d_lock temporarily
> * which allows inode to change. Start over if that happens.
> */
> int ret = dentry_lock_inode(dentry);
> if (unlikely(ret != LOCK_FAST)) {
> if (ret == LOCK_FAILED)
> goto again;
> /*
> * Recheck refcount as it might have been
> * incremented while d_lock was dropped.
> */
> if (dentry->d_lockref.count != 1)
> goto drop_ref;
> }

Implementation 4: screw the tristate, move the loop inside dentry_lock_inode().

> If lock_parent() returns a non-NULL, it is returning
> dentry->d_parent. So the return value is really just a boolean and the
> locked parent is the parent of the dentry. The function is a little bit
> tricky because it could return NULL (lock failed) even if the dentry has
> a non-NULL d_parent. So any caller using a tristate return variation of
> lock_parent() must rely on the return value instead of a non-NULL
> dentry->d_parent.

dentry always has non-NULL ->d_parent; it might point to dentry itself, but
it's never NULL.

> if (!dentry->d_lockref.count) {
> - struct dentry *parent = lock_parent(dentry);
> + int ret = lock_parent(dentry);
> + parent = dentry->d_parent;
> if (likely(!dentry->d_lockref.count)) {
> __dentry_kill(dentry);
> dput(parent);

Broken. In IS_ROOT case you'll hit an extra dput() on dentry itself.
dput(NULL) is no-op; this, OTOH, isn't.