mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] kernfs: switch global locks to per-fs lock
@ 2025-04-15 15:36 alexjlzheng
  2025-04-15 15:36 ` [PATCH v2 1/2] kernfs: switch global kernfs_idr_lock " alexjlzheng
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: alexjlzheng @ 2025-04-15 15:36 UTC (permalink / raw)
  To: gregkh, tj; +Cc: linux-kernel, Jinliang Zheng

From: Jinliang Zheng <alexjlzheng@tencent.com>

The kernfs implementation has big lock granularity so every kernfs-based
(e.g., sysfs, cgroup) fs are able to compete the locks. This patchset
switches the global locks to per-fs locks.

In fact, the implementation of global locks has not yet introduced
performance issues. But in the long run, more and more file systems will
be implemented based on the kernfs framework, so this optimization is
meaningful.

There are three global locks now, kernfs_idr_lock, kernfs_rename_lock
and kernfs_pr_cont_lock. We only switch kernfs_idr_lock and
kernfs_rename_lock here, because kernfs_pr_cont_lock is on a cold path.

Changelog:
v2: Only switch kernfs_idr_lock and kernfs_rename_lock to per-fs
v1: https://lore.kernel.org/all/20250411183109.6334-1-alexjlzheng@tencent.com/

Jinliang Zheng (2):
  kernfs: switch global kernfs_idr_lock to per-fs lock
  kernfs: switch global kernfs_rename_lock to per-fs lock

 fs/kernfs/dir.c             | 28 +++++++++++++++-------------
 fs/kernfs/kernfs-internal.h | 16 ++++++++++++----
 2 files changed, 27 insertions(+), 17 deletions(-)

-- 
2.49.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] kernfs: switch global kernfs_idr_lock to per-fs lock
  2025-04-15 15:36 [PATCH v2 0/2] kernfs: switch global locks to per-fs lock alexjlzheng
@ 2025-04-15 15:36 ` alexjlzheng
  2025-04-15 15:36 ` [PATCH v2 2/2] kernfs: switch global kernfs_rename_lock " alexjlzheng
  2025-04-20 15:02 ` [PATCH v2 0/2] kernfs: switch global locks " Jinliang Zheng
  2 siblings, 0 replies; 5+ messages in thread
From: alexjlzheng @ 2025-04-15 15:36 UTC (permalink / raw)
  To: gregkh, tj; +Cc: linux-kernel, Jinliang Zheng

From: Jinliang Zheng <alexjlzheng@tencent.com>

The kernfs implementation has big lock granularity(kernfs_idr_lock) so
every kernfs-based(e.g., sysfs, cgroup) fs are able to compete the lock.

This patch switches the global kernfs_idr_lock to per-fs lock, which
put the spinlock into kernfs_root.

Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
---
 fs/kernfs/dir.c             | 14 +++++++-------
 fs/kernfs/kernfs-internal.h |  1 +
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index fc70d72c3fe8..355d943ffe27 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -27,7 +27,6 @@ DEFINE_RWLOCK(kernfs_rename_lock);	/* kn->parent and ->name */
  */
 static DEFINE_SPINLOCK(kernfs_pr_cont_lock);
 static char kernfs_pr_cont_buf[PATH_MAX];	/* protected by pr_cont_lock */
-static DEFINE_SPINLOCK(kernfs_idr_lock);	/* root->ino_idr */
 
 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
 
@@ -584,9 +583,9 @@ void kernfs_put(struct kernfs_node *kn)
 	if (kernfs_type(kn) == KERNFS_LINK)
 		kernfs_put(kn->symlink.target_kn);
 
-	spin_lock(&kernfs_idr_lock);
+	spin_lock(&root->kernfs_idr_lock);
 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
-	spin_unlock(&kernfs_idr_lock);
+	spin_unlock(&root->kernfs_idr_lock);
 
 	call_rcu(&kn->rcu, kernfs_free_rcu);
 
@@ -639,13 +638,13 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 		goto err_out1;
 
 	idr_preload(GFP_KERNEL);
-	spin_lock(&kernfs_idr_lock);
+	spin_lock(&root->kernfs_idr_lock);
 	ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
 	if (ret >= 0 && ret < root->last_id_lowbits)
 		root->id_highbits++;
 	id_highbits = root->id_highbits;
 	root->last_id_lowbits = ret;
-	spin_unlock(&kernfs_idr_lock);
+	spin_unlock(&root->kernfs_idr_lock);
 	idr_preload_end();
 	if (ret < 0)
 		goto err_out2;
@@ -681,9 +680,9 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
 	return kn;
 
  err_out3:
-	spin_lock(&kernfs_idr_lock);
+	spin_lock(&root->kernfs_idr_lock);
 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
-	spin_unlock(&kernfs_idr_lock);
+	spin_unlock(&root->kernfs_idr_lock);
  err_out2:
 	kmem_cache_free(kernfs_node_cache, kn);
  err_out1:
@@ -989,6 +988,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
 		return ERR_PTR(-ENOMEM);
 
 	idr_init(&root->ino_idr);
+	spin_lock_init(&root->kernfs_idr_lock);
 	init_rwsem(&root->kernfs_rwsem);
 	init_rwsem(&root->kernfs_iattr_rwsem);
 	init_rwsem(&root->kernfs_supers_rwsem);
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index 40a2a9cd819d..24e9514565ac 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -40,6 +40,7 @@ struct kernfs_root {
 
 	/* private fields, do not use outside kernfs proper */
 	struct idr		ino_idr;
+	spinlock_t		kernfs_idr_lock;	/* root->ino_idr */
 	u32			last_id_lowbits;
 	u32			id_highbits;
 	struct kernfs_syscall_ops *syscall_ops;
-- 
2.49.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] kernfs: switch global kernfs_rename_lock to per-fs lock
  2025-04-15 15:36 [PATCH v2 0/2] kernfs: switch global locks to per-fs lock alexjlzheng
  2025-04-15 15:36 ` [PATCH v2 1/2] kernfs: switch global kernfs_idr_lock " alexjlzheng
@ 2025-04-15 15:36 ` alexjlzheng
  2025-04-20 15:02 ` [PATCH v2 0/2] kernfs: switch global locks " Jinliang Zheng
  2 siblings, 0 replies; 5+ messages in thread
From: alexjlzheng @ 2025-04-15 15:36 UTC (permalink / raw)
  To: gregkh, tj; +Cc: linux-kernel, Jinliang Zheng

From: Jinliang Zheng <alexjlzheng@tencent.com>

The kernfs implementation has big lock granularity(kernfs_rename_lock) so
every kernfs-based(e.g., sysfs, cgroup) fs are able to compete the lock.

This patch switches the global kernfs_rename_lock to per-fs lock, which
put the rwlock into kernfs_root.

Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
---
 fs/kernfs/dir.c             | 14 ++++++++------
 fs/kernfs/kernfs-internal.h | 15 +++++++++++----
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 355d943ffe27..d63a96786c9b 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -17,7 +17,6 @@
 
 #include "kernfs-internal.h"
 
-DEFINE_RWLOCK(kernfs_rename_lock);	/* kn->parent and ->name */
 /*
  * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to
  * call pr_cont() while holding rename_lock. Because sometimes pr_cont()
@@ -228,7 +227,7 @@ int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
 	if (to) {
 		root = kernfs_root(to);
 		if (!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)) {
-			guard(read_lock_irqsave)(&kernfs_rename_lock);
+			guard(read_lock_irqsave)(&root->kernfs_rename_lock);
 			return kernfs_path_from_node_locked(to, from, buf, buflen);
 		}
 	}
@@ -295,12 +294,14 @@ void pr_cont_kernfs_path(struct kernfs_node *kn)
 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
 {
 	struct kernfs_node *parent;
+	struct kernfs_root *root;
 	unsigned long flags;
 
-	read_lock_irqsave(&kernfs_rename_lock, flags);
+	root = kernfs_root(kn);
+	read_lock_irqsave(&root->kernfs_rename_lock, flags);
 	parent = kernfs_parent(kn);
 	kernfs_get(parent);
-	read_unlock_irqrestore(&kernfs_rename_lock, flags);
+	read_unlock_irqrestore(&root->kernfs_rename_lock, flags);
 
 	return parent;
 }
@@ -993,6 +994,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
 	init_rwsem(&root->kernfs_iattr_rwsem);
 	init_rwsem(&root->kernfs_supers_rwsem);
 	INIT_LIST_HEAD(&root->supers);
+	rwlock_init(&root->kernfs_rename_lock);
 
 	/*
 	 * On 64bit ino setups, id is ino.  On 32bit, low 32bits are ino.
@@ -1789,7 +1791,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
 	/* rename_lock protects ->parent accessors */
 	if (old_parent != new_parent) {
 		kernfs_get(new_parent);
-		write_lock_irq(&kernfs_rename_lock);
+		write_lock_irq(&root->kernfs_rename_lock);
 
 		rcu_assign_pointer(kn->__parent, new_parent);
 
@@ -1797,7 +1799,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
 		if (new_name)
 			rcu_assign_pointer(kn->name, new_name);
 
-		write_unlock_irq(&kernfs_rename_lock);
+		write_unlock_irq(&root->kernfs_rename_lock);
 		kernfs_put(old_parent);
 	} else {
 		/* name assignment is RCU protected, parent is the same */
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index 24e9514565ac..6061b6f70d2a 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -19,8 +19,6 @@
 #include <linux/kernfs.h>
 #include <linux/fs_context.h>
 
-extern rwlock_t kernfs_rename_lock;
-
 struct kernfs_iattrs {
 	kuid_t			ia_uid;
 	kgid_t			ia_gid;
@@ -53,6 +51,9 @@ struct kernfs_root {
 	struct rw_semaphore	kernfs_iattr_rwsem;
 	struct rw_semaphore	kernfs_supers_rwsem;
 
+	/* kn->parent and kn->name */
+	rwlock_t		kernfs_rename_lock;
+
 	struct rcu_head		rcu;
 };
 
@@ -108,6 +109,11 @@ static inline bool kernfs_root_is_locked(const struct kernfs_node *kn)
 	return lockdep_is_held(&kernfs_root(kn)->kernfs_rwsem);
 }
 
+static inline bool kernfs_rename_is_locked(const struct kernfs_node *kn)
+{
+	return lockdep_is_held(&kernfs_root(kn)->kernfs_rename_lock);
+}
+
 static inline const char *kernfs_rcu_name(const struct kernfs_node *kn)
 {
 	return rcu_dereference_check(kn->name, kernfs_root_is_locked(kn));
@@ -118,14 +124,15 @@ static inline struct kernfs_node *kernfs_parent(const struct kernfs_node *kn)
 	/*
 	 * The kernfs_node::__parent remains valid within a RCU section. The kn
 	 * can be reparented (and renamed) which changes the entry. This can be
-	 * avoided by locking kernfs_root::kernfs_rwsem or kernfs_rename_lock.
+	 * avoided by locking kernfs_root::kernfs_rwsem or
+	 * kernfs_root::kernfs_rename_lock.
 	 * Both locks can be used to obtain a reference on __parent. Once the
 	 * reference count reaches 0 then the node is about to be freed
 	 * and can not be renamed (or become a different parent) anymore.
 	 */
 	return rcu_dereference_check(kn->__parent,
 				     kernfs_root_is_locked(kn) ||
-				     lockdep_is_held(&kernfs_rename_lock) ||
+				     kernfs_rename_is_locked(kn) ||
 				     !atomic_read(&kn->count));
 }
 
-- 
2.49.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/2] kernfs: switch global locks to per-fs lock
  2025-04-15 15:36 [PATCH v2 0/2] kernfs: switch global locks to per-fs lock alexjlzheng
  2025-04-15 15:36 ` [PATCH v2 1/2] kernfs: switch global kernfs_idr_lock " alexjlzheng
  2025-04-15 15:36 ` [PATCH v2 2/2] kernfs: switch global kernfs_rename_lock " alexjlzheng
@ 2025-04-20 15:02 ` Jinliang Zheng
  2025-04-21  6:07   ` Greg KH
  2 siblings, 1 reply; 5+ messages in thread
From: Jinliang Zheng @ 2025-04-20 15:02 UTC (permalink / raw)
  To: alexjlzheng; +Cc: alexjlzheng, gregkh, linux-kernel, tj

On Tue, 15 Apr 2025 23:36:57 +0800, Jinliang Zheng wrote:
> From: Jinliang Zheng <alexjlzheng@tencent.com>
> 
> The kernfs implementation has big lock granularity so every kernfs-based
> (e.g., sysfs, cgroup) fs are able to compete the locks. This patchset
> switches the global locks to per-fs locks.
> 
> In fact, the implementation of global locks has not yet introduced
> performance issues. But in the long run, more and more file systems will
> be implemented based on the kernfs framework, so this optimization is
> meaningful.
> 
> There are three global locks now, kernfs_idr_lock, kernfs_rename_lock
> and kernfs_pr_cont_lock. We only switch kernfs_idr_lock and
> kernfs_rename_lock here, because kernfs_pr_cont_lock is on a cold path.
> 
> Changelog:
> v2: Only switch kernfs_idr_lock and kernfs_rename_lock to per-fs
> v1: https://lore.kernel.org/all/20250411183109.6334-1-alexjlzheng@tencent.com/
> 
> Jinliang Zheng (2):
>   kernfs: switch global kernfs_idr_lock to per-fs lock
>   kernfs: switch global kernfs_rename_lock to per-fs lock
> 
>  fs/kernfs/dir.c             | 28 +++++++++++++++-------------
>  fs/kernfs/kernfs-internal.h | 16 ++++++++++++----
>  2 files changed, 27 insertions(+), 17 deletions(-)

Ding Dong ~

thanks,
Jinliang Zheng. :)

> 
> -- 
> 2.49.0

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/2] kernfs: switch global locks to per-fs lock
  2025-04-20 15:02 ` [PATCH v2 0/2] kernfs: switch global locks " Jinliang Zheng
@ 2025-04-21  6:07   ` Greg KH
  0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2025-04-21  6:07 UTC (permalink / raw)
  To: Jinliang Zheng; +Cc: alexjlzheng, linux-kernel, tj

On Sun, Apr 20, 2025 at 11:02:44PM +0800, Jinliang Zheng wrote:
> On Tue, 15 Apr 2025 23:36:57 +0800, Jinliang Zheng wrote:
> > From: Jinliang Zheng <alexjlzheng@tencent.com>
> > 
> > The kernfs implementation has big lock granularity so every kernfs-based
> > (e.g., sysfs, cgroup) fs are able to compete the locks. This patchset
> > switches the global locks to per-fs locks.
> > 
> > In fact, the implementation of global locks has not yet introduced
> > performance issues. But in the long run, more and more file systems will
> > be implemented based on the kernfs framework, so this optimization is
> > meaningful.
> > 
> > There are three global locks now, kernfs_idr_lock, kernfs_rename_lock
> > and kernfs_pr_cont_lock. We only switch kernfs_idr_lock and
> > kernfs_rename_lock here, because kernfs_pr_cont_lock is on a cold path.
> > 
> > Changelog:
> > v2: Only switch kernfs_idr_lock and kernfs_rename_lock to per-fs
> > v1: https://lore.kernel.org/all/20250411183109.6334-1-alexjlzheng@tencent.com/
> > 
> > Jinliang Zheng (2):
> >   kernfs: switch global kernfs_idr_lock to per-fs lock
> >   kernfs: switch global kernfs_rename_lock to per-fs lock
> > 
> >  fs/kernfs/dir.c             | 28 +++++++++++++++-------------
> >  fs/kernfs/kernfs-internal.h | 16 ++++++++++++----
> >  2 files changed, 27 insertions(+), 17 deletions(-)
> 
> Ding Dong ~

You ask this 5 days after submitting the last version, and that includes
3 of those days being a national holiday here (4 if you include today).

Please relax, I'll get to them "soon", only start to worry if it's been
more than 2 weeks.

In the meantime, to help out, please take the time to review other patch
submissions on the mailing lists.  To not do so and just expect others
to only review yours is very unbalanced.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-21  6:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-15 15:36 [PATCH v2 0/2] kernfs: switch global locks to per-fs lock alexjlzheng
2025-04-15 15:36 ` [PATCH v2 1/2] kernfs: switch global kernfs_idr_lock " alexjlzheng
2025-04-15 15:36 ` [PATCH v2 2/2] kernfs: switch global kernfs_rename_lock " alexjlzheng
2025-04-20 15:02 ` [PATCH v2 0/2] kernfs: switch global locks " Jinliang Zheng
2025-04-21  6:07   ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®