Commit 1d25b84e authored by Imran Khan's avatar Imran Khan Committed by Greg Kroah-Hartman

kernfs: Replace global kernfs_open_file_mutex with hashed mutexes.

In current kernfs design a single mutex, kernfs_open_file_mutex, protects
the list of kernfs_open_file instances corresponding to a sysfs attribute.
So even if different tasks are opening or closing different sysfs files
they can contend on osq_lock of this mutex. The contention is more apparent
in large scale systems with few hundred CPUs where most of the CPUs have
running tasks that are opening, accessing or closing sysfs files at any
point of time.

Using hashed mutexes in place of a single global mutex, can significantly
reduce contention around global mutex and hence can provide better
scalability. Moreover as these hashed mutexes are not part of kernfs_node
objects we will not see any singnificant change in memory utilization of
kernfs based file systems like sysfs, cgroupfs etc.

Modify interface introduced in previous patch to make use of hashed
mutexes. Use kernfs_node address as hashing key.
Acked-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarImran Khan <imran.f.khan@oracle.com>
Link: https://lore.kernel.org/r/20220615021059.862643-5-imran.f.khan@oracle.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 41448c61
......@@ -18,19 +18,6 @@
#include "kernfs-internal.h"
/*
* There's one kernfs_open_file for each open file and one kernfs_open_node
* for each kernfs_node with one or more open files.
*
* kernfs_node->attr.open points to kernfs_open_node. attr.open is
* RCU protected.
*
* filp->private_data points to seq_file whose ->private points to
* kernfs_open_file. kernfs_open_files are chained at
* kernfs_open_node->files, which is protected by kernfs_open_file_mutex.
*/
static DEFINE_MUTEX(kernfs_open_file_mutex);
struct kernfs_open_node {
struct rcu_head rcu_head;
atomic_t event;
......@@ -51,7 +38,9 @@ static LLIST_HEAD(kernfs_notify_list);
static inline struct mutex *kernfs_open_file_mutex_ptr(struct kernfs_node *kn)
{
return &kernfs_open_file_mutex;
int idx = hash_ptr(kn, NR_KERNFS_LOCK_BITS);
return &kernfs_locks->open_file_mutex[idx];
}
static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node *kn)
......
......@@ -164,4 +164,8 @@ void kernfs_drain_open_files(struct kernfs_node *kn);
*/
extern const struct inode_operations kernfs_symlink_iops;
/*
* kernfs locks
*/
extern struct kernfs_global_locks *kernfs_locks;
#endif /* __KERNFS_INTERNAL_H */
......@@ -20,6 +20,7 @@
#include "kernfs-internal.h"
struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
struct kernfs_global_locks *kernfs_locks;
static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
{
......@@ -387,6 +388,22 @@ void kernfs_kill_sb(struct super_block *sb)
kfree(info);
}
static void __init kernfs_mutex_init(void)
{
int count;
for (count = 0; count < NR_KERNFS_LOCKS; count++)
mutex_init(&kernfs_locks->open_file_mutex[count]);
}
static void __init kernfs_lock_init(void)
{
kernfs_locks = kmalloc(sizeof(struct kernfs_global_locks), GFP_KERNEL);
WARN_ON(!kernfs_locks);
kernfs_mutex_init();
}
void __init kernfs_init(void)
{
kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
......@@ -397,4 +414,6 @@ void __init kernfs_init(void)
kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache",
sizeof(struct kernfs_iattrs),
0, SLAB_PANIC, NULL);
kernfs_lock_init();
}
......@@ -18,6 +18,7 @@
#include <linux/uidgid.h>
#include <linux/wait.h>
#include <linux/rwsem.h>
#include <linux/cache.h>
struct file;
struct dentry;
......@@ -34,6 +35,62 @@ struct kernfs_fs_context;
struct kernfs_open_node;
struct kernfs_iattrs;
/*
* NR_KERNFS_LOCK_BITS determines size (NR_KERNFS_LOCKS) of hash
* table of locks.
* Having a small hash table would impact scalability, since
* more and more kernfs_node objects will end up using same lock
* and having a very large hash table would waste memory.
*
* At the moment size of hash table of locks is being set based on
* the number of CPUs as follows:
*
* NR_CPU NR_KERNFS_LOCK_BITS NR_KERNFS_LOCKS
* 1 1 2
* 2-3 2 4
* 4-7 4 16
* 8-15 6 64
* 16-31 8 256
* 32 and more 10 1024
*
* The above relation between NR_CPU and number of locks is based
* on some internal experimentation which involved booting qemu
* with different values of smp, performing some sysfs operations
* on all CPUs and observing how increase in number of locks impacts
* completion time of these sysfs operations on each CPU.
*/
#ifdef CONFIG_SMP
#define NR_KERNFS_LOCK_BITS (2 * (ilog2(NR_CPUS < 32 ? NR_CPUS : 32)))
#else
#define NR_KERNFS_LOCK_BITS 1
#endif
#define NR_KERNFS_LOCKS (1 << NR_KERNFS_LOCK_BITS)
/*
* There's one kernfs_open_file for each open file and one kernfs_open_node
* for each kernfs_node with one or more open files.
*
* filp->private_data points to seq_file whose ->private points to
* kernfs_open_file.
*
* kernfs_open_files are chained at kernfs_open_node->files, which is
* protected by kernfs_global_locks.open_file_mutex[i].
*
* To reduce possible contention in sysfs access, arising due to single
* locks, use an array of locks (e.g. open_file_mutex) and use kernfs_node
* object address as hash keys to get the index of these locks.
*
* Hashed mutexes are safe to use here because operations using these don't
* rely on global exclusion.
*
* In future we intend to replace other global locks with hashed ones as well.
* kernfs_global_locks acts as a holder for all such hash tables.
*/
struct kernfs_global_locks {
struct mutex open_file_mutex[NR_KERNFS_LOCKS];
};
enum kernfs_node_type {
KERNFS_DIR = 0x0001,
KERNFS_FILE = 0x0002,
......
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