Skip to content

Commit 81c173c

Browse files
htejungregkh
authored andcommitted
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>
1 parent 182fd64 commit 81c173c

3 files changed

Lines changed: 37 additions & 33 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ DEFINE_MUTEX(kernfs_mutex);
2222

2323
#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
2424

25+
static bool kernfs_active(struct kernfs_node *kn)
26+
{
27+
lockdep_assert_held(&kernfs_mutex);
28+
return atomic_read(&kn->active) >= 0;
29+
}
30+
2531
static bool kernfs_lockdep(struct kernfs_node *kn)
2632
{
2733
#ifdef CONFIG_DEBUG_LOCK_ALLOC
@@ -183,25 +189,20 @@ void kernfs_put_active(struct kernfs_node *kn)
183189
}
184190

185191
/**
186-
* kernfs_deactivate - deactivate kernfs_node
187-
* @kn: kernfs_node to deactivate
192+
* kernfs_drain - drain kernfs_node
193+
* @kn: kernfs_node to drain
188194
*
189-
* Deny new active references, drain existing ones and nuke all
190-
* existing mmaps. Mutiple removers may invoke this function
191-
* concurrently on @kn and all will return after deactivation and
192-
* draining are complete.
195+
* Drain existing usages and nuke all existing mmaps of @kn. Mutiple
196+
* removers may invoke this function concurrently on @kn and all will
197+
* return after draining is complete.
193198
*/
194-
static void kernfs_deactivate(struct kernfs_node *kn)
199+
static void kernfs_drain(struct kernfs_node *kn)
195200
__releases(&kernfs_mutex) __acquires(&kernfs_mutex)
196201
{
197202
struct kernfs_root *root = kernfs_root(kn);
198203

199204
lockdep_assert_held(&kernfs_mutex);
200-
BUG_ON(!(kn->flags & KERNFS_REMOVED));
201-
202-
/* only the first invocation on @kn should deactivate it */
203-
if (atomic_read(&kn->active) >= 0)
204-
atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
205+
WARN_ON_ONCE(kernfs_active(kn));
205206

206207
mutex_unlock(&kernfs_mutex);
207208

@@ -253,13 +254,15 @@ void kernfs_put(struct kernfs_node *kn)
253254
return;
254255
root = kernfs_root(kn);
255256
repeat:
256-
/* Moving/renaming is always done while holding reference.
257+
/*
258+
* Moving/renaming is always done while holding reference.
257259
* kn->parent won't change beneath us.
258260
*/
259261
parent = kn->parent;
260262

261-
WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
262-
parent ? parent->name : "", kn->name);
263+
WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
264+
"kernfs_put: %s/%s: released with incorrect active_ref %d\n",
265+
parent ? parent->name : "", kn->name, atomic_read(&kn->active));
263266

264267
if (kernfs_type(kn) == KERNFS_LINK)
265268
kernfs_put(kn->symlink.target_kn);
@@ -301,8 +304,8 @@ static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
301304
kn = dentry->d_fsdata;
302305
mutex_lock(&kernfs_mutex);
303306

304-
/* The kernfs node has been deleted */
305-
if (kn->flags & KERNFS_REMOVED)
307+
/* The kernfs node has been deactivated */
308+
if (!kernfs_active(kn))
306309
goto out_bad;
307310

308311
/* The kernfs node has been moved? */
@@ -371,12 +374,12 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
371374
kn->ino = ret;
372375

373376
atomic_set(&kn->count, 1);
374-
atomic_set(&kn->active, 0);
377+
atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
375378
RB_CLEAR_NODE(&kn->rb);
376379

377380
kn->name = name;
378381
kn->mode = mode;
379-
kn->flags = flags | KERNFS_REMOVED;
382+
kn->flags = flags;
380383

381384
return kn;
382385

@@ -432,7 +435,7 @@ int kernfs_add_one(struct kernfs_node *kn)
432435
goto out_unlock;
433436

434437
ret = -ENOENT;
435-
if (parent->flags & KERNFS_REMOVED)
438+
if (!kernfs_active(parent))
436439
goto out_unlock;
437440

438441
kn->hash = kernfs_name_hash(kn->name, kn->ns);
@@ -449,7 +452,7 @@ int kernfs_add_one(struct kernfs_node *kn)
449452
}
450453

451454
/* Mark the entry added into directory tree */
452-
kn->flags &= ~KERNFS_REMOVED;
455+
atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
453456
ret = 0;
454457
out_unlock:
455458
mutex_unlock(&kernfs_mutex);
@@ -549,7 +552,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
549552
return ERR_PTR(-ENOMEM);
550553
}
551554

552-
kn->flags &= ~KERNFS_REMOVED;
555+
atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
553556
kn->priv = priv;
554557
kn->dir.root = root;
555558

@@ -763,24 +766,25 @@ static void __kernfs_remove(struct kernfs_node *kn)
763766

764767
pr_debug("kernfs %s: removing\n", kn->name);
765768

766-
/* disable lookup and node creation under @kn */
769+
/* prevent any new usage under @kn by deactivating all nodes */
767770
pos = NULL;
768771
while ((pos = kernfs_next_descendant_post(pos, kn)))
769-
pos->flags |= KERNFS_REMOVED;
772+
if (kernfs_active(pos))
773+
atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
770774

771775
/* deactivate and unlink the subtree node-by-node */
772776
do {
773777
pos = kernfs_leftmost_descendant(kn);
774778

775779
/*
776-
* kernfs_deactivate() drops kernfs_mutex temporarily and
777-
* @pos's base ref could have been put by someone else by
778-
* the time the function returns. Make sure it doesn't go
779-
* away underneath us.
780+
* kernfs_drain() drops kernfs_mutex temporarily and @pos's
781+
* base ref could have been put by someone else by the time
782+
* the function returns. Make sure it doesn't go away
783+
* underneath us.
780784
*/
781785
kernfs_get(pos);
782786

783-
kernfs_deactivate(pos);
787+
kernfs_drain(pos);
784788

785789
/*
786790
* 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,
865869
mutex_lock(&kernfs_mutex);
866870

867871
error = -ENOENT;
868-
if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
872+
if (!kernfs_active(kn) || !kernfs_active(new_parent))
869873
goto out;
870874

871875
error = 0;
@@ -925,7 +929,7 @@ static struct kernfs_node *kernfs_dir_pos(const void *ns,
925929
struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
926930
{
927931
if (pos) {
928-
int valid = !(pos->flags & KERNFS_REMOVED) &&
932+
int valid = kernfs_active(pos) &&
929933
pos->parent == parent && hash == pos->hash;
930934
kernfs_put(pos);
931935
if (!valid)

‎fs/kernfs/kernfs-internal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ struct kernfs_iattrs {
2626
struct simple_xattrs xattrs;
2727
};
2828

29-
#define KN_DEACTIVATED_BIAS INT_MIN
29+
/* +1 to avoid triggering overflow warning when negating it */
30+
#define KN_DEACTIVATED_BIAS (INT_MIN + 1)
3031

3132
/* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */
3233

‎include/linux/kernfs.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ enum kernfs_node_type {
3838
#define KERNFS_FLAG_MASK ~KERNFS_TYPE_MASK
3939

4040
enum kernfs_node_flag {
41-
KERNFS_REMOVED = 0x0010,
4241
KERNFS_NS = 0x0020,
4342
KERNFS_HAS_SEQ_SHOW = 0x0040,
4443
KERNFS_HAS_MMAP = 0x0080,

0 commit comments

Comments
 (0)