fs/namei.c: keep track of nd->root refcount status

The rules for nd->root are messy:
	* if we have LOOKUP_ROOT, it doesn't contribute to refcounts
	* if we have LOOKUP_RCU, it doesn't contribute to refcounts
	* if nd->root.mnt is NULL, it doesn't contribute to refcounts
	* otherwise it does contribute

terminate_walk() needs to drop the references if they are contributing.
So everything else should be careful not to confuse it, leading to
rather convoluted code.

It's easier to keep track of whether we'd grabbed the reference(s)
explicitly.  Use a new flag for that.  Don't bother with zeroing
nd->root.mnt on unlazy failures and in terminate_walk - it's not
needed anymore (terminate_walk() won't care and the next path_init()
will zero nd->root in !LOOKUP_ROOT case anyway).

Resulting rules for nd->root refcounts are much simpler: they are
contributing iff LOOKUP_ROOT_GRABBED is set in nd->flags.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
Al Viro 2019-07-16 21:20:17 -04:00
parent ee594bfff3
commit 84a2bd3940
2 changed files with 16 additions and 26 deletions

View File

@ -596,14 +596,12 @@ static void terminate_walk(struct nameidata *nd)
path_put(&nd->path); path_put(&nd->path);
for (i = 0; i < nd->depth; i++) for (i = 0; i < nd->depth; i++)
path_put(&nd->stack[i].link); path_put(&nd->stack[i].link);
if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { if (nd->flags & LOOKUP_ROOT_GRABBED) {
path_put(&nd->root); path_put(&nd->root);
nd->root.mnt = NULL; nd->flags &= ~LOOKUP_ROOT_GRABBED;
} }
} else { } else {
nd->flags &= ~LOOKUP_RCU; nd->flags &= ~LOOKUP_RCU;
if (!(nd->flags & LOOKUP_ROOT))
nd->root.mnt = NULL;
rcu_read_unlock(); rcu_read_unlock();
} }
nd->depth = 0; nd->depth = 0;
@ -645,6 +643,7 @@ static bool legitimize_root(struct nameidata *nd)
{ {
if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT)) if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
return true; return true;
nd->flags |= LOOKUP_ROOT_GRABBED;
return legitimize_path(nd, &nd->root, nd->root_seq); return legitimize_path(nd, &nd->root, nd->root_seq);
} }
@ -678,21 +677,18 @@ static int unlazy_walk(struct nameidata *nd)
nd->flags &= ~LOOKUP_RCU; nd->flags &= ~LOOKUP_RCU;
if (unlikely(!legitimize_links(nd))) if (unlikely(!legitimize_links(nd)))
goto out2;
if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
goto out1; goto out1;
if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
goto out;
if (unlikely(!legitimize_root(nd))) if (unlikely(!legitimize_root(nd)))
goto out; goto out;
rcu_read_unlock(); rcu_read_unlock();
BUG_ON(nd->inode != parent->d_inode); BUG_ON(nd->inode != parent->d_inode);
return 0; return 0;
out2: out1:
nd->path.mnt = NULL; nd->path.mnt = NULL;
nd->path.dentry = NULL; nd->path.dentry = NULL;
out1:
if (!(nd->flags & LOOKUP_ROOT))
nd->root.mnt = NULL;
out: out:
rcu_read_unlock(); rcu_read_unlock();
return -ECHILD; return -ECHILD;
@ -732,21 +728,14 @@ static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned se
*/ */
if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
goto out; goto out;
if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) { if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
rcu_read_unlock(); goto out_dput;
dput(dentry);
goto drop_root_mnt;
}
/* /*
* Sequence counts matched. Now make sure that the root is * Sequence counts matched. Now make sure that the root is
* still valid and get it if required. * still valid and get it if required.
*/ */
if (unlikely(!legitimize_root(nd))) { if (unlikely(!legitimize_root(nd)))
rcu_read_unlock(); goto out_dput;
dput(dentry);
return -ECHILD;
}
rcu_read_unlock(); rcu_read_unlock();
return 0; return 0;
@ -756,9 +745,10 @@ out1:
nd->path.dentry = NULL; nd->path.dentry = NULL;
out: out:
rcu_read_unlock(); rcu_read_unlock();
drop_root_mnt: return -ECHILD;
if (!(nd->flags & LOOKUP_ROOT)) out_dput:
nd->root.mnt = NULL; rcu_read_unlock();
dput(dentry);
return -ECHILD; return -ECHILD;
} }
@ -822,6 +812,7 @@ static void set_root(struct nameidata *nd)
} while (read_seqcount_retry(&fs->seq, seq)); } while (read_seqcount_retry(&fs->seq, seq));
} else { } else {
get_fs_root(fs, &nd->root); get_fs_root(fs, &nd->root);
nd->flags |= LOOKUP_ROOT_GRABBED;
} }
} }
@ -1738,8 +1729,6 @@ static int pick_link(struct nameidata *nd, struct path *link,
nd->flags &= ~LOOKUP_RCU; nd->flags &= ~LOOKUP_RCU;
nd->path.mnt = NULL; nd->path.mnt = NULL;
nd->path.dentry = NULL; nd->path.dentry = NULL;
if (!(nd->flags & LOOKUP_ROOT))
nd->root.mnt = NULL;
rcu_read_unlock(); rcu_read_unlock();
} else if (likely(unlazy_walk(nd)) == 0) } else if (likely(unlazy_walk(nd)) == 0)
error = nd_alloc_stack(nd); error = nd_alloc_stack(nd);

View File

@ -37,6 +37,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};
#define LOOKUP_NO_REVAL 0x0080 #define LOOKUP_NO_REVAL 0x0080
#define LOOKUP_JUMPED 0x1000 #define LOOKUP_JUMPED 0x1000
#define LOOKUP_ROOT 0x2000 #define LOOKUP_ROOT 0x2000
#define LOOKUP_ROOT_GRABBED 0x0008
extern int path_pts(struct path *path); extern int path_pts(struct path *path);