Skip to contentSkip to conversation

kernfs: fix missing kernfs_idr_lock to remove an ID from the IDR

PATCH Superseded Muchun Song 3 participants 3 messages May 16, 2023 – May 23, 2023 kernfs

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernfs: fix missing kernfs_idr_lock to remove an ID from the IDR

The root->ino_idr is supposed to be protected by kernfs_idr_lock, fix it.

Fixes 488dee96bb62 ("kernfs: allow creating kernfs objects with arbitrary uid/gid")
---
 fs/kernfs/dir.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/kernfs/dir.c +9 -4
index 45b6919903e6..84a2cd196076 100644
@@ -525,6 +525,13 @@ void kernfs_get(struct kernfs_node *kn)
 }
 EXPORT_SYMBOL_GPL(kernfs_get);
 
+static void kernfs_idr_remove(struct kernfs_root *root, struct kernfs_node *kn)
+{
+	spin_lock(&kernfs_idr_lock);
+	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
+	spin_unlock(&kernfs_idr_lock);
+}
+
 /**
  * kernfs_put - put a reference count on a kernfs_node
  * @kn: the target kernfs_node
@@ -559,9 +566,7 @@ void kernfs_put(struct kernfs_node *kn)
 		simple_xattrs_free(&kn->iattr->xattrs);
 		kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
 	}
-	spin_lock(&kernfs_idr_lock);
-	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
-	spin_unlock(&kernfs_idr_lock);
+	kernfs_idr_remove(root, kn);
 	kmem_cache_free(kernfs_node_cache, kn);
 
 	kn = parent;
@@ -655,7 +660,7 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	return kn;
 
  err_out3:
-	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
+	kernfs_idr_remove(root, kn);
  err_out2:
 	kmem_cache_free(kernfs_node_cache, kn);
  err_out1:
Signature · 1 line
2.11.0
maintainer
#1 lore raw

fs/kernfs/dir.c static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, · line 663

Quoted 6 lines · Muchun Song
fs/kernfs/dir.c +1 -1
@@ -655,7 +660,7 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	return kn;
 
  err_out3:
-	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
+	kernfs_idr_remove(root, kn);

Good catch but I'd just open code the locking here.

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 13 lines · Tejun Heo

fs/kernfs/dir.c static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, · line 663

Quoted 6 lines · Muchun Song
fs/kernfs/dir.c +1 -1
@@ -655,7 +660,7 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 return kn;
 
 err_out3:
- idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
+ kernfs_idr_remove(root, kn);

Good catch but I'd just open code the locking here.

OK. I'll send v2.

Thanks.

Quoted 5 lines

Thanks.

Signature · 1 line
tejun