Skip to content

Commit

Permalink
kernfs: Replace global kernfs_open_file_mutex with hashed mutexes.
Browse files Browse the repository at this point in the history
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: Tejun Heo <[email protected]>
Signed-off-by: Imran Khan <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
imran-kn authored and gregkh committed Jun 27, 2022
1 parent 41448c6 commit 1d25b84
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
17 changes: 3 additions & 14 deletions fs/kernfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions fs/kernfs/kernfs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
19 changes: 19 additions & 0 deletions fs/kernfs/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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",
Expand All @@ -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();
}
57 changes: 57 additions & 0 deletions include/linux/kernfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <linux/uidgid.h>
#include <linux/wait.h>
#include <linux/rwsem.h>
#include <linux/cache.h>

struct file;
struct dentry;
Expand All @@ -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,
Expand Down

0 comments on commit 1d25b84

Please sign in to comment.