Commit 72b5d5ae authored by Yushan Zhou's avatar Yushan Zhou Committed by Greg Kroah-Hartman

kernfs: fix potential NULL dereference in __kernfs_remove

When lockdep is enabled, lockdep_assert_held_write would
cause potential NULL pointer dereference.

Fix the following smatch warnings:

fs/kernfs/dir.c:1353 __kernfs_remove() warn: variable dereferenced before check 'kn' (see line 1346)

Fixes: 393c3714 ("kernfs: switch global kernfs_rwsem lock to per-fs lock")
Signed-off-by: default avatarYushan Zhou <katrinzhou@tencent.com>
Link: https://lore.kernel.org/r/20220630082512.3482581-1-zys.zljxml@gmail.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent c882716b
...@@ -1343,14 +1343,17 @@ static void __kernfs_remove(struct kernfs_node *kn) ...@@ -1343,14 +1343,17 @@ static void __kernfs_remove(struct kernfs_node *kn)
{ {
struct kernfs_node *pos; struct kernfs_node *pos;
/* Short-circuit if non-root @kn has already finished removal. */
if (!kn)
return;
lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
/* /*
* Short-circuit if non-root @kn has already finished removal.
* This is for kernfs_remove_self() which plays with active ref * This is for kernfs_remove_self() which plays with active ref
* after removal. * after removal.
*/ */
if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb))) if (kn->parent && RB_EMPTY_NODE(&kn->rb))
return; return;
pr_debug("kernfs %s: removing\n", kn->name); pr_debug("kernfs %s: removing\n", kn->name);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment