Skip to content

Commit b680b08

Browse files
committed
kernfs: use dumber locking for kernfs_find_and_get_node_by_ino()
kernfs_find_and_get_node_by_ino() uses RCU protection. It's currently a bit buggy because it can look up a node which hasn't been activated yet and thus may end up exposing a node that the kernfs user is still prepping. While it can be fixed by pushing it further in the current direction, it's already complicated and isn't clear whether the complexity is justified. The main use of kernfs_find_and_get_node_by_ino() is for exportfs operations. They aren't super hot and all the follow-up operations (e.g. mapping to path) use normal locking anyway. Let's switch to a dumber locking scheme and protect the lookup with kernfs_idr_lock. Signed-off-by: Tejun Heo <tj@kernel.org> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Namhyung Kim <namhyung@kernel.org>
1 parent db53c73 commit b680b08

2 files changed

Lines changed: 10 additions & 46 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,6 @@ void kernfs_put(struct kernfs_node *kn)
508508
struct kernfs_node *parent;
509509
struct kernfs_root *root;
510510

511-
/*
512-
* kernfs_node is freed with ->count 0, kernfs_find_and_get_node_by_ino
513-
* depends on this to filter reused stale node
514-
*/
515511
if (!kn || !atomic_dec_and_test(&kn->count))
516512
return;
517513
root = kernfs_root(kn);
@@ -646,11 +642,7 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
646642
kn->id.ino = ret;
647643
kn->id.generation = gen;
648644

649-
/*
650-
* set ino first. This RELEASE is paired with atomic_inc_not_zero in
651-
* kernfs_find_and_get_node_by_ino
652-
*/
653-
atomic_set_release(&kn->count, 1);
645+
atomic_set(&kn->count, 1);
654646
atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
655647
RB_CLEAR_NODE(&kn->rb);
656648

@@ -716,38 +708,19 @@ struct kernfs_node *kernfs_find_and_get_node_by_ino(struct kernfs_root *root,
716708
{
717709
struct kernfs_node *kn;
718710

719-
rcu_read_lock();
711+
spin_lock(&kernfs_idr_lock);
712+
720713
kn = idr_find(&root->ino_idr, ino);
721714
if (!kn)
722-
goto out;
715+
goto err_unlock;
723716

724-
/*
725-
* Since kernfs_node is freed in RCU, it's possible an old node for ino
726-
* is freed, but reused before RCU grace period. But a freed node (see
727-
* kernfs_put) or an incompletedly initialized node (see
728-
* __kernfs_new_node) should have 'count' 0. We can use this fact to
729-
* filter out such node.
730-
*/
731-
if (!atomic_inc_not_zero(&kn->count)) {
732-
kn = NULL;
733-
goto out;
734-
}
735-
736-
/*
737-
* The node could be a new node or a reused node. If it's a new node,
738-
* we are ok. If it's reused because of RCU (because of
739-
* SLAB_TYPESAFE_BY_RCU), the __kernfs_new_node always sets its 'ino'
740-
* before 'count'. So if 'count' is uptodate, 'ino' should be uptodate,
741-
* hence we can use 'ino' to filter stale node.
742-
*/
743-
if (kn->id.ino != ino)
744-
goto out;
745-
rcu_read_unlock();
717+
if (unlikely(!atomic_inc_not_zero(&kn->count)))
718+
goto err_unlock;
746719

720+
spin_unlock(&kernfs_idr_lock);
747721
return kn;
748-
out:
749-
rcu_read_unlock();
750-
kernfs_put(kn);
722+
err_unlock:
723+
spin_unlock(&kernfs_idr_lock);
751724
return NULL;
752725
}
753726

‎fs/kernfs/mount.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,9 @@ void kernfs_kill_sb(struct super_block *sb)
363363

364364
void __init kernfs_init(void)
365365
{
366-
367-
/*
368-
* the slab is freed in RCU context, so kernfs_find_and_get_node_by_ino
369-
* can access the slab lock free. This could introduce stale nodes,
370-
* please see how kernfs_find_and_get_node_by_ino filters out stale
371-
* nodes.
372-
*/
373366
kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
374367
sizeof(struct kernfs_node),
375-
0,
376-
SLAB_PANIC | SLAB_TYPESAFE_BY_RCU,
377-
NULL);
368+
0, SLAB_PANIC, NULL);
378369

379370
/* Creates slab cache for kernfs inode attributes */
380371
kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache",

0 commit comments

Comments
 (0)