kernfs: remove KERNFS_REMOVED

KERNFS_REMOVED is used to mark half-initialized and dying nodes so
that they don't show up in lookups and deny adding new nodes under or
renaming it; however, its role overlaps that of deactivation.

It's necessary to deny addition of new children while removal is in
progress; however, this role considerably intersects with deactivation
- KERNFS_REMOVED prevents new children while deactivation prevents new
file operations.  There's no reason to have them separate making
things more complex than necessary.

This patch removes KERNFS_REMOVED.

* Instead of KERNFS_REMOVED, each node now starts its life
  deactivated.  This means that we now use both atomic_add() and
  atomic_sub() on KN_DEACTIVATED_BIAS, which is INT_MIN.  The compiler
  generates an overflow warnings when negating INT_MIN as the negation
  can't be represented as a positive number.  Nothing is actually
  broken but let's bump BIAS by one to avoid the warnings for archs
  which negates the subtrahend..

* A new helper kernfs_active() which tests whether kn->active >= 0 is
  added for convenience and lockdep annotation.  All KERNFS_REMOVED
  tests are replaced with negated kernfs_active() tests.

* __kernfs_remove() is updated to deactivate, but not drain, all nodes
  in the subtree instead of setting KERNFS_REMOVED.  This removes
  deactivation from kernfs_deactivate(), which is now renamed to
  kernfs_drain().

* Sanity check on KERNFS_REMOVED in kernfs_put() is replaced with
  checks on the active ref.

* Some comment style updates in the affected area.

v2: Reordered before removal path restructuring.  kernfs_active()
    dropped and kernfs_get/put_active() used instead.  RB_EMPTY_NODE()
    used in the lookup paths.

v3: Reverted most of v2 except for creating a new node with
    KN_DEACTIVATED_BIAS.

Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Tejun Heo 2014-02-03 14:03:00 -05:00 committed by Greg Kroah-Hartman
parent 182fd64b66
commit 81c173cb5e
3 changed files with 37 additions and 33 deletions

View File

@ -22,6 +22,12 @@ DEFINE_MUTEX(kernfs_mutex);
#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
static bool kernfs_active(struct kernfs_node *kn)
{
lockdep_assert_held(&kernfs_mutex);
return atomic_read(&kn->active) >= 0;
}
static bool kernfs_lockdep(struct kernfs_node *kn) static bool kernfs_lockdep(struct kernfs_node *kn)
{ {
#ifdef CONFIG_DEBUG_LOCK_ALLOC #ifdef CONFIG_DEBUG_LOCK_ALLOC
@ -183,25 +189,20 @@ void kernfs_put_active(struct kernfs_node *kn)
} }
/** /**
* kernfs_deactivate - deactivate kernfs_node * kernfs_drain - drain kernfs_node
* @kn: kernfs_node to deactivate * @kn: kernfs_node to drain
* *
* Deny new active references, drain existing ones and nuke all * Drain existing usages and nuke all existing mmaps of @kn. Mutiple
* existing mmaps. Mutiple removers may invoke this function * removers may invoke this function concurrently on @kn and all will
* concurrently on @kn and all will return after deactivation and * return after draining is complete.
* draining are complete.
*/ */
static void kernfs_deactivate(struct kernfs_node *kn) static void kernfs_drain(struct kernfs_node *kn)
__releases(&kernfs_mutex) __acquires(&kernfs_mutex) __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
{ {
struct kernfs_root *root = kernfs_root(kn); struct kernfs_root *root = kernfs_root(kn);
lockdep_assert_held(&kernfs_mutex); lockdep_assert_held(&kernfs_mutex);
BUG_ON(!(kn->flags & KERNFS_REMOVED)); WARN_ON_ONCE(kernfs_active(kn));
/* only the first invocation on @kn should deactivate it */
if (atomic_read(&kn->active) >= 0)
atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
mutex_unlock(&kernfs_mutex); mutex_unlock(&kernfs_mutex);
@ -253,13 +254,15 @@ void kernfs_put(struct kernfs_node *kn)
return; return;
root = kernfs_root(kn); root = kernfs_root(kn);
repeat: repeat:
/* Moving/renaming is always done while holding reference. /*
* Moving/renaming is always done while holding reference.
* kn->parent won't change beneath us. * kn->parent won't change beneath us.
*/ */
parent = kn->parent; parent = kn->parent;
WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n", WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
parent ? parent->name : "", kn->name); "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
parent ? parent->name : "", kn->name, atomic_read(&kn->active));
if (kernfs_type(kn) == KERNFS_LINK) if (kernfs_type(kn) == KERNFS_LINK)
kernfs_put(kn->symlink.target_kn); kernfs_put(kn->symlink.target_kn);
@ -301,8 +304,8 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
kn = dentry->d_fsdata; kn = dentry->d_fsdata;
mutex_lock(&kernfs_mutex); mutex_lock(&kernfs_mutex);
/* The kernfs node has been deleted */ /* The kernfs node has been deactivated */
if (kn->flags & KERNFS_REMOVED) if (!kernfs_active(kn))
goto out_bad; goto out_bad;
/* The kernfs node has been moved? */ /* The kernfs node has been moved? */
@ -371,12 +374,12 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
kn->ino = ret; kn->ino = ret;
atomic_set(&kn->count, 1); atomic_set(&kn->count, 1);
atomic_set(&kn->active, 0); atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
RB_CLEAR_NODE(&kn->rb); RB_CLEAR_NODE(&kn->rb);
kn->name = name; kn->name = name;
kn->mode = mode; kn->mode = mode;
kn->flags = flags | KERNFS_REMOVED; kn->flags = flags;
return kn; return kn;
@ -432,7 +435,7 @@ int kernfs_add_one(struct kernfs_node *kn)
goto out_unlock; goto out_unlock;
ret = -ENOENT; ret = -ENOENT;
if (parent->flags & KERNFS_REMOVED) if (!kernfs_active(parent))
goto out_unlock; goto out_unlock;
kn->hash = kernfs_name_hash(kn->name, kn->ns); kn->hash = kernfs_name_hash(kn->name, kn->ns);
@ -449,7 +452,7 @@ int kernfs_add_one(struct kernfs_node *kn)
} }
/* Mark the entry added into directory tree */ /* Mark the entry added into directory tree */
kn->flags &= ~KERNFS_REMOVED; atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
ret = 0; ret = 0;
out_unlock: out_unlock:
mutex_unlock(&kernfs_mutex); mutex_unlock(&kernfs_mutex);
@ -549,7 +552,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
kn->flags &= ~KERNFS_REMOVED; atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
kn->priv = priv; kn->priv = priv;
kn->dir.root = root; kn->dir.root = root;
@ -763,24 +766,25 @@ static void __kernfs_remove(struct kernfs_node *kn)
pr_debug("kernfs %s: removing\n", kn->name); pr_debug("kernfs %s: removing\n", kn->name);
/* disable lookup and node creation under @kn */ /* prevent any new usage under @kn by deactivating all nodes */
pos = NULL; pos = NULL;
while ((pos = kernfs_next_descendant_post(pos, kn))) while ((pos = kernfs_next_descendant_post(pos, kn)))
pos->flags |= KERNFS_REMOVED; if (kernfs_active(pos))
atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
/* deactivate and unlink the subtree node-by-node */ /* deactivate and unlink the subtree node-by-node */
do { do {
pos = kernfs_leftmost_descendant(kn); pos = kernfs_leftmost_descendant(kn);
/* /*
* kernfs_deactivate() drops kernfs_mutex temporarily and * kernfs_drain() drops kernfs_mutex temporarily and @pos's
* @pos's base ref could have been put by someone else by * base ref could have been put by someone else by the time
* the time the function returns. Make sure it doesn't go * the function returns. Make sure it doesn't go away
* away underneath us. * underneath us.
*/ */
kernfs_get(pos); kernfs_get(pos);
kernfs_deactivate(pos); kernfs_drain(pos);
/* /*
* kernfs_unlink_sibling() succeeds once per node. Use it * kernfs_unlink_sibling() succeeds once per node. Use it
@ -865,7 +869,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
mutex_lock(&kernfs_mutex); mutex_lock(&kernfs_mutex);
error = -ENOENT; error = -ENOENT;
if ((kn->flags | new_parent->flags) & KERNFS_REMOVED) if (!kernfs_active(kn) || !kernfs_active(new_parent))
goto out; goto out;
error = 0; error = 0;
@ -925,7 +929,7 @@ static struct kernfs_node *kernfs_dir_pos(const void *ns,
struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
{ {
if (pos) { if (pos) {
int valid = !(pos->flags & KERNFS_REMOVED) && int valid = kernfs_active(pos) &&
pos->parent == parent && hash == pos->hash; pos->parent == parent && hash == pos->hash;
kernfs_put(pos); kernfs_put(pos);
if (!valid) if (!valid)

View File

@ -26,7 +26,8 @@ struct kernfs_iattrs {
struct simple_xattrs xattrs; struct simple_xattrs xattrs;
}; };
#define KN_DEACTIVATED_BIAS INT_MIN /* +1 to avoid triggering overflow warning when negating it */
#define KN_DEACTIVATED_BIAS (INT_MIN + 1)
/* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */ /* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */

View File

@ -38,7 +38,6 @@ enum kernfs_node_type {
#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK #define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
enum kernfs_node_flag { enum kernfs_node_flag {
KERNFS_REMOVED = 0x0010,
KERNFS_NS = 0x0020, KERNFS_NS = 0x0020,
KERNFS_HAS_SEQ_SHOW = 0x0040, KERNFS_HAS_SEQ_SHOW = 0x0040,
KERNFS_HAS_MMAP = 0x0080, KERNFS_HAS_MMAP = 0x0080,