Lockless update of reference count protected by spinlock
Subsystems (5)
v4 cover Lockless update of reference count protected by spinlock
v3->v4:
- Replace helper function access to d_lock and d_count by using macros to redefine the old d_lock name to the spinlock and new d_refcount name to the reference count. This greatly reduces the size of this patchset from 25 to 12 and make it easier to review.v2->v3:
- Completely revamp the packaging by adding a new lockref data structure that combines the spinlock with the reference count. Helper functions are also added to manipulate the new data structure. That results in modifying over 50 files, but the changes were trivial in most of them.
- Change initial spinlock wait to use a timeout.
- Force 64-bit alignment of the spinlock & reference count structure.
- Add a new way to use the combo by using a new union and helper functions.
v1->v2:
- Add one more layer of indirection to LOCK_WITH_REFCOUNT macro.
- Add __LINUX_SPINLOCK_REFCOUNT_H protection to spinlock_refcount.h.
- Add some generic get/put macros into spinlock_refcount.h.
This patchset supports a generic mechanism to atomically update a reference count that is protected by a spinlock without actually acquiring the lock itself. If the update doesn't succeeed, the caller will have to acquire the lock and update the reference count in the the old way. This will help in situation where there is a lot of spinlock contention because of frequent reference count update.
The d_lock and d_count fields of the struct dentry in dcache.h was modified to use the new lockref data structure and the d_lock name is now a macro to the actual spinlock. The d_count name, however, cannot be reused as it has collision elsewhere in the kernel. So a new d_refcount name is now used for the reference count.
This patch set causes significant performance improvement in the short workload of the AIM7 benchmark on a 8-socket x86-64 machine with 80 cores.
patch 1: Introduce the new lockref data structure
patch 2: Enable x86 architecture to use the feature
patches 3-11: Rename all the d_count references to d_refcount
patch 12: Change the dentry structure to use the lockref
structure to improve performance for high contention cases
Thank to Thomas Gleixner, Andi Kleen and Linus for their valuable input in shaping this patchset.
Waiman Long (12):
spinlock: A new lockref structure for lockless update of refcount
spinlock: Enable x86 architecture to do lockless refcount update
dcache: rename d_count field of dentry to d_refcount
auto-fs: rename d_count field of dentry to d_refcount
ceph-fs: rename d_count field of dentry to d_refcount
coda-fs: rename d_count field of dentry to d_refcount
config-fs: rename d_count field of dentry to d_refcount
ecrypt-fs: rename d_count field of dentry to d_refcount file locking: rename d_count field of dentry to d_refcount
nfs: rename d_count field of dentry to d_refcount
nilfs2: rename d_count field of dentry to d_refcount
dcache: Enable lockless update of refcount in dentry structure
arch/x86/Kconfig | 3 + arch/x86/include/asm/spinlock_refcount.h | 1 + fs/autofs4/expire.c | 8 +- fs/autofs4/root.c | 2 +- fs/ceph/inode.c | 4 +- fs/ceph/mds_client.c | 2 +- fs/coda/dir.c | 4 +- fs/configfs/dir.c | 2 +- fs/dcache.c | 72 +++++----- fs/ecryptfs/inode.c | 2 +- fs/locks.c | 2 +- fs/namei.c | 6 +- fs/nfs/dir.c | 8 +- fs/nfs/unlink.c | 2 +- fs/nilfs2/super.c | 2 +- include/asm-generic/spinlock_refcount.h | 46 ++++++ include/linux/dcache.h | 21 ++-- include/linux/spinlock_refcount.h | 107 ++++++++++++++ kernel/Kconfig.locks | 5 + lib/Makefile | 2 + lib/spinlock_refcount.c | 229 ++++++++++++++++++++++++++++++ 21 files changed, 466 insertions(+), 64 deletions(-) create mode 100644 arch/x86/include/asm/spinlock_refcount.h create mode 100644 include/asm-generic/spinlock_refcount.h create mode 100644 include/linux/spinlock_refcount.h create mode 100644 lib/spinlock_refcount.c
PATCH v4 1/12 spinlock: A new lockref structure for lockless update of refcount
This patch introduces a new set of spinlock_refcount.h header files to be included by kernel codes that want to do a faster lockless update of reference count protected by a spinlock.
The new lockref structure consists of just the spinlock and the reference count data. Helper functions are defined in the new <linux/spinlock_refcount.h> header file to access the content of the new structure. There is a generic structure defined for all architecture, but each architecture can also optionally define its own structure and use its own helper functions.
Two new config parameters are introduced:
1. SPINLOCK_REFCOUNT
2. ARCH_SPINLOCK_REFCOUNT
The first one is defined in the kernel/Kconfig.locks which is used to enable or disable the faster lockless reference count update optimization. The second one has to be defined in each of the architecture's Kconfig file to enable the optimization for that architecture. Therefore, each architecture has to opt-in for this optimization or it won't get it. This allows each architecture plenty of time to test it out before deciding to use it or replace it with a better architecture specific solution.
This optimization won't work for non-SMP system or when spinlock debugging is turned on. As a result, it is turned off each any of them is true. It also won't work for full preempt-RT and so should be turned on in this case.
The current patch allows 3 levels of access to the new lockref structure:
1. The lockless update optimization is turned off (SPINLOCK_REFCOUNT=n).
2. The lockless update optimization is turned on and the generic version is used (SPINLOCK_REFCOUNT=y and ARCH_SPINLOCK_REFCOUNT=y).
3. The lockless update optimization is turned on and the architecture provides its own version.
To maximize the chance of doing lockless update in the generic version, the inlined __lockref_add_unless() function will wait for a certain amount of time if the lock is not free before trying to do the update.
The new code also attempts to do lockless atomic update twice before falling back to the old code path of acquiring a lock before doing the update. It is because there will still be some fair amount of contention with only one attempt.
--- include/asm-generic/spinlock_refcount.h | 46 ++++++ include/linux/spinlock_refcount.h | 107 ++++++++++++++ kernel/Kconfig.locks | 5 + lib/Makefile | 2 + lib/spinlock_refcount.c | 229 +++++++++++++++++++++++++++++++ 5 files changed, 389 insertions(+), 0 deletions(-) create mode 100644 include/asm-generic/spinlock_refcount.h create mode 100644 include/linux/spinlock_refcount.h create mode 100644 lib/spinlock_refcount.c
include/asm-generic/spinlock_refcount.h new file +46 -0
@@ -0,0 +1,46 @@ +/* +* Spinlock with reference count combo +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* (c) Copyright 2013 Hewlett-Packard Development Company, L.P. +* +* Authors: Waiman Long <[email protected]> +*/ +#ifndef __ASM_GENERIC_SPINLOCK_REFCOUNT_H +#define __ASM_GENERIC_SPINLOCK_REFCOUNT_H + +/* +* The lockref structure defines a combined spinlock with reference count +* data structure to be embedded in a larger structure. The combined data +* structure is always 8-byte aligned. So proper placement of this structure +* in the larger embedding data structure is needed to ensure that there is +* no hole in it. +*/ +struct __aligned(sizeof(u64)) lockref { +union { +u64 __lock_count; +struct { +unsigned int refcnt;/* Reference count */ +spinlock_t lock; + }; + }; +}; + +/* +* Struct lockref helper functions +*/ +extern void lockref_get (struct lockref *lockcnt); +extern int lockref_put (struct lockref *lockcnt); +extern int lockref_get_not_zero (struct lockref *lockcnt); +extern int lockref_put_or_locked (struct lockref *lockcnt); + +#endif /* __ASM_GENERIC_SPINLOCK_REFCOUNT_H */
include/linux/spinlock_refcount.h new file +107 -0
107 lines not on this page · raw message
kernel/Kconfig.locks +5 -0
@@ -223,3 +223,8 @@ endif config MUTEX_SPIN_ON_OWNER def_bool y depends on SMP && !DEBUG_MUTEXES + +config SPINLOCK_REFCOUNT + def_bool y + depends on ARCH_SPINLOCK_REFCOUNT && SMP + depends on !GENERIC_LOCKBREAK && !DEBUG_SPINLOCK && !DEBUG_LOCK_ALLOC
lib/Makefile +2 -0
@@ -179,3 +179,5 @@ quiet_cmd_build_OID_registry = GEN $@ clean-files += oid_registry_data.c obj-$(CONFIG_UCS2_STRING) += ucs2_string.o + +obj-$(CONFIG_SPINLOCK_REFCOUNT) += spinlock_refcount.o
lib/spinlock_refcount.c new file +229 -0
229 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v4 2/12 spinlock: Enable x86 architecture to do lockless refcount update
In reply to Waiman LongThere are two steps to enable each architecture to do lockless reference count update:
1. Define the ARCH_SPINLOCK_REFCOUNT config parameter in its Kconfig file.
2. Add a <asm/spinlock_refcount.h> architecture specific header file.
This is done for the x86 architecture to use the generic version available.
--- arch/x86/Kconfig | 3 +++ arch/x86/include/asm/spinlock_refcount.h | 1 + 2 files changed, 4 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/spinlock_refcount.h
arch/x86/Kconfig +3 -0
@@ -253,6 +253,9 @@ config ARCH_CPU_PROBE_RELEASE config ARCH_SUPPORTS_UPROBES def_bool y +config ARCH_SPINLOCK_REFCOUNT + def_bool y + source "init/Kconfig" source "kernel/Kconfig.freezer"
arch/x86/include/asm/spinlock_refcount.h new file +1 -0
@@ -0,0 +1 @@ +#include <asm-generic/spinlock_refcount.h>
Signature · 1 line
1.7.1
PATCH v4 3/12 dcache: rename d_count field of dentry to d_refcount
In reply to Waiman LongBefore converting the d_lock and d_count field of the dentry data structure to the new lockref structure, we need to consider the implication of such a change. All current references of d_count and d_lock have to be changed accordingly.
One way to minimize the changes is to redefine the original field names as macros to the new names. For d_lock, it is possible to do so saving a lot of changes as this name is not used anywhere else in the kernel. For d_count, this is not possible as this name is used somewhere else in the kernel for different things.
Therefore, we need to change the d_count field name to a different one which is unique in the kernel source code. The new name will be d_refcount.
--- fs/dcache.c | 54 ++++++++++++++++++++++++------------------------ fs/namei.c | 6 ++-- include/linux/dcache.h | 6 ++-- 3 files changed, 33 insertions(+), 33 deletions(-)
fs/dcache.c +27 -27
@@ -54,7 +54,7 @@* - d_flags * - d_name * - d_lru -* - d_count +* - d_refcount * - d_unhashed() * - d_parent and d_subdirs * - childrens' d_child and d_parent
@@ -229,7 +229,7 @@ static void __d_free(struct rcu_head *head)*/ static void d_free (struct dentry *dentry) { -BUG_ON (dentry->d_count); +BUG_ON (dentry->d_refcount);this_cpu_dec (nr_dentry);if (dentry->d_op && dentry->d_op->d_release) dentry->d_op->d_release (dentry);
@@ -467,7 +467,7 @@ relock: }if (ref) - dentry->d_count--; + dentry->d_refcount--;/* * inform the fs via d_prune that this dentry is about to be * unhashed and destroyed.
@@ -513,12 +513,12 @@ void dput(struct dentry *dentry)return ; repeat: -if (dentry->d_count ==1 ) +if (dentry->d_refcount ==1 )might_sleep ();spin_lock (&dentry->d_lock); -BUG_ON (!dentry->d_count); -if (dentry->d_count >1 ) { - dentry->d_count--; +BUG_ON (!dentry->d_refcount); +if (dentry->d_refcount >1 ) { + dentry->d_refcount--;spin_unlock (&dentry->d_lock);return ; }
@@ -535,7 +535,7 @@ repeat: dentry->d_flags |=DCACHE_REFERENCED ;dentry_lru_add (dentry); - dentry->d_count--; + dentry->d_refcount--;spin_unlock (&dentry->d_lock);return ;
@@ -590,7 +590,7 @@ int d_invalidate(struct dentry * dentry)* We also need to leave mountpoints alone, * directory or not. */ -if (dentry->d_count >1 && dentry->d_inode) { +if (dentry->d_refcount >1 && dentry->d_inode) {if (S_ISDIR (dentry->d_inode->i_mode) ||d_mountpoint (dentry)) {spin_unlock (&dentry->d_lock);return -EBUSY ;
@@ -606,7 +606,7 @@ EXPORT_SYMBOL(d_invalidate);/* This must be called with d_lock held */ static inline void __dget_dlock (struct dentry *dentry) { - dentry->d_count++; + dentry->d_refcount++; }static inline void __dget (struct dentry *dentry)
@@ -634,8 +634,8 @@ repeat: goto repeat; }rcu_read_unlock (); -BUG_ON (!ret->d_count); - ret->d_count++; +BUG_ON (!ret->d_refcount); + ret->d_refcount++;spin_unlock (&ret->d_lock);return ret; }
@@ -718,7 +718,7 @@ restart:spin_lock (&inode->i_lock);hlist_for_each_entry (dentry, &inode->i_dentry, d_alias) {spin_lock (&dentry->d_lock); -if (!dentry->d_count) { +if (!dentry->d_refcount) {__dget_dlock (dentry);__d_drop (dentry);spin_unlock (&dentry->d_lock);
@@ -734,7 +734,7 @@ EXPORT_SYMBOL(d_prune_aliases);/* * Try to throw away a dentry - free the inode, dput the parent. -* Requires dentry->d_lock is held, and dentry->d_count == 0. +* Requires dentry->d_lock is held, and dentry->d_refcount == 0. * Releases dentry->d_lock. * * This may fail if locks cannot be acquired no problem, just try again.
@@ -764,8 +764,8 @@ static void try_prune_one_dentry(struct dentry *dentry) dentry = parent;while (dentry) {spin_lock (&dentry->d_lock); -if (dentry->d_count >1 ) { - dentry->d_count--; +if (dentry->d_refcount >1 ) { + dentry->d_refcount--;spin_unlock (&dentry->d_lock);return ; }
@@ -793,7 +793,7 @@ static void shrink_dentry_list(struct list_head *list)* the LRU because of laziness during lookup. Do not free * it - just keep it off the LRU list. */ -if (dentry->d_count) { +if (dentry->d_refcount) {dentry_lru_del (dentry);spin_unlock (&dentry->d_lock);continue ;
@@ -913,7 +913,7 @@ static void shrink_dcache_for_umount_subtree(struct dentry *dentry)dentry_lru_del (dentry);__d_shrink (dentry); -if (dentry->d_count !=0 ) { +if (dentry->d_refcount !=0 ) {printk (KERN_ERR "BUG: Dentry %p{i=%lx,n=%s}" " still in use (%d)"
@@ -922,7 +922,7 @@ static void shrink_dcache_for_umount_subtree(struct dentry *dentry) dentry->d_inode ? dentry->d_inode->i_ino :0UL , dentry->d_name.name, - dentry->d_count, + dentry->d_refcount, dentry->d_sb->s_type->name, dentry->d_sb->s_id);BUG ();
@@ -933,7 +933,7 @@ static void shrink_dcache_for_umount_subtree(struct dentry *dentry)list_del (&dentry->d_u.d_child); }else { parent = dentry->d_parent; - parent->d_count--; + parent->d_refcount--;list_del (&dentry->d_u.d_child); }
@@ -981,7 +981,7 @@ void shrink_dcache_for_umount(struct super_block *sb) dentry = sb->s_root; sb->s_root =NULL ; - dentry->d_count--; + dentry->d_refcount--;shrink_dcache_for_umount_subtree (dentry);while (!hlist_bl_empty (&sb->s_anon)) {
@@ -1147,7 +1147,7 @@ resume:* loop in shrink_dcache_parent() might not make any progress * and loop forever. */ -if (dentry->d_count) { +if (dentry->d_refcount) {dentry_lru_del (dentry); }else if (!(dentry->d_flags &DCACHE_SHRINK_LIST )) {dentry_lru_move_list (dentry, dispose);
@@ -1269,7 +1269,7 @@ struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)smp_wmb (); dentry->d_name.name = dname; - dentry->d_count =1 ; + dentry->d_refcount =1 ; dentry->d_flags =0 ;spin_lock_init (&dentry->d_lock);seqcount_init (&dentry->d_seq);
@@ -1970,7 +1970,7 @@ struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name) goto next; } - dentry->d_count++; + dentry->d_refcount++; found = dentry;spin_unlock (&dentry->d_lock);break ;
@@ -2069,7 +2069,7 @@ again:spin_lock (&dentry->d_lock); inode = dentry->d_inode; isdir =S_ISDIR (inode->i_mode); -if (dentry->d_count ==1 ) { +if (dentry->d_refcount ==1 ) {if (!spin_trylock (&inode->i_lock)) {spin_unlock (&dentry->d_lock);cpu_relax ();
@@ -2937,7 +2937,7 @@ resume: }if (!(dentry->d_flags &DCACHE_GENOCIDE )) { dentry->d_flags |=DCACHE_GENOCIDE ; - dentry->d_count--; + dentry->d_refcount--; }spin_unlock (&dentry->d_lock); }
@@ -2945,7 +2945,7 @@ resume:struct dentry *child = this_parent;if (!(this_parent->d_flags &DCACHE_GENOCIDE )) { this_parent->d_flags |=DCACHE_GENOCIDE ; - this_parent->d_count--; + this_parent->d_refcount--; } this_parent =try_to_ascend (this_parent, locked, seq);if (!this_parent)
fs/namei.c +3 -3
@@ -536,8 +536,8 @@ static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)* a reference at this point. */ BUG_ON (!IS_ROOT (dentry) && dentry->d_parent != parent); -BUG_ON (!parent->d_count); - parent->d_count++; +BUG_ON (!parent->d_refcount); + parent->d_refcount++;spin_unlock (&dentry->d_lock); }spin_unlock (&parent->d_lock);
@@ -3280,7 +3280,7 @@ void dentry_unhash(struct dentry *dentry) {shrink_dcache_parent (dentry);spin_lock (&dentry->d_lock); -if (dentry->d_count ==1 ) +if (dentry->d_refcount ==1 )__d_drop (dentry);spin_unlock (&dentry->d_lock); }
include/linux/dcache.h +3 -3
@@ -112,7 +112,7 @@ struct dentry {unsigned char d_iname[DNAME_INLINE_LEN ];/* small names */ /* Ref lookup also touches following */ -unsigned int d_count;/* protected by d_lock */ +unsigned int d_refcount;/* protected by d_lock */ spinlock_t d_lock;/* per dentry lock */ const struct dentry_operations *d_op;struct super_block *d_sb;/* The root of the dentry tree */
@@ -319,7 +319,7 @@ static inline int __d_rcu_to_refcount(struct dentry *dentry, unsigned seq)assert_spin_locked (&dentry->d_lock);if (!read_seqcount_retry (&dentry->d_seq, seq)) { ret =1 ; - dentry->d_count++; + dentry->d_refcount++; }return ret;
@@ -352,7 +352,7 @@ extern char *dentry_path(struct dentry *, char *, int);static inline struct dentry *dget_dlock (struct dentry *dentry) {if (dentry) - dentry->d_count++; + dentry->d_refcount++;return dentry; }
Signature · 1 line
1.7.1