As the generic rwsem-xadd code is using the appropriate acquire and
release versions of the atomic operations, the arch specific rwsem.h
files will not be that much faster than the generic code as long as the
atomic functions are properly implemented. So we can remove those arch
specific rwsem.h and stop building asm/rwsem.h to reduce maintenance
effort.
Currently, only x86, alpha and ia64 have implemented architecture
specific fast paths. I don't have access to alpha and ia64 systems for
testing, but they are legacy systems that are not likely to be updated
to the latest kernel anyway.
By using a rwsem microbenchmark, the total locking rates on a 4-socket
56-core 112-thread x86-64 system before and after the patch were as
follows (mixed means equal # of read and write locks):
Before Patch After Patch
# of Threads wlock rlock mixed wlock rlock mixed
------------ ----- ----- ----- ----- ----- -----
1 29,201 30,143 29,458 28,615 30,172 29,201
2 6,807 13,299 1,171 7,725 15,025 1,804
4 6,504 12,755 1,520 7,127 14,286 1,345
8 6,762 13,412 764 6,826 13,652 726
16 6,693 15,408 662 6,599 15,938 626
32 6,145 15,286 496 5,549 15,487 511
64 5,812 15,495 60 5,858 15,572 60
There were some run-to-run variations for the multi-thread tests. For
x86-64, using the generic C code fast path seems to be a little bit
faster than the assembly version with low lock contention. Looking at
the assembly version of the fast paths, there are assembly to/from C
code wrappers that save and restore all the callee-clobbered registers
(7 registers on x86-64). The assembly generated from the generic C
code doesn't need to do that. That may explain the slight performance
gain here.
The generic asm rwsem.h can also be merged into kernel/locking/rwsem.h
with no code change as no other code other than those under
kernel/locking needs to access the internal rwsem macros and functions.
Signed-off-by: Waiman Long <longman@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-c6x-dev@linux-c6x.org
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-riscv@lists.infradead.org
Cc: linux-um@lists.infradead.org
Cc: linux-xtensa@linux-xtensa.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: nios2-dev@lists.rocketboards.org
Cc: openrisc@lists.librecores.org
Cc: uclinux-h8-devel@lists.sourceforge.jp
Link: https://lkml.kernel.org/r/20190322143008.21313-2-longman@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
[kdrag0n: Dropped conflicting architecture changes]
Signed-off-by: Danny Lin <danny@kdrag0n.dev>
Signed-off-by: UtsavBalar1231 <utsavbalar1231@gmail.com>
206 lines
6.2 KiB
C
206 lines
6.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/* rwsem.h: R/W semaphores, public interface
|
|
*
|
|
* Written by David Howells (dhowells@redhat.com).
|
|
* Derived from asm-i386/semaphore.h
|
|
*/
|
|
|
|
#ifndef _LINUX_RWSEM_H
|
|
#define _LINUX_RWSEM_H
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/list.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/atomic.h>
|
|
#include <linux/err.h>
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
|
#include <linux/osq_lock.h>
|
|
#endif
|
|
|
|
struct rw_semaphore;
|
|
|
|
#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
|
|
#include <linux/rwsem-spinlock.h> /* use a generic implementation */
|
|
#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
|
|
#else
|
|
/* All arch specific implementations share the same struct */
|
|
struct rw_semaphore {
|
|
atomic_long_t count;
|
|
struct list_head wait_list;
|
|
raw_spinlock_t wait_lock;
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
|
struct optimistic_spin_queue osq; /* spinner MCS lock */
|
|
/*
|
|
* Write owner. Used as a speculative check to see
|
|
* if the owner is running on the cpu.
|
|
*/
|
|
struct task_struct *owner;
|
|
#endif
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
struct lockdep_map dep_map;
|
|
#endif
|
|
#ifdef CONFIG_RWSEM_PRIO_AWARE
|
|
/* count for waiters preempt to queue in wait list */
|
|
long m_count;
|
|
#endif
|
|
};
|
|
|
|
/*
|
|
* Setting bit 1 of the owner field but not bit 0 will indicate
|
|
* that the rwsem is writer-owned with an unknown owner.
|
|
*/
|
|
#define RWSEM_OWNER_UNKNOWN ((struct task_struct *)-2L)
|
|
|
|
extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
|
|
extern struct rw_semaphore *rwsem_down_read_failed_killable(struct rw_semaphore *sem);
|
|
extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
|
|
extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
|
|
extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
|
|
extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
|
|
|
|
/* In all implementations count != 0 means locked */
|
|
static inline int rwsem_is_locked(struct rw_semaphore *sem)
|
|
{
|
|
return atomic_long_read(&sem->count) != 0;
|
|
}
|
|
|
|
#define RWSEM_UNLOCKED_VALUE 0L
|
|
#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
|
|
#endif
|
|
|
|
/* Common initializer macros and functions */
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
|
|
#else
|
|
# define __RWSEM_DEP_MAP_INIT(lockname)
|
|
#endif
|
|
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
|
#define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
|
|
#else
|
|
#define __RWSEM_OPT_INIT(lockname)
|
|
#endif
|
|
|
|
#ifdef CONFIG_RWSEM_PRIO_AWARE
|
|
#define __RWSEM_PRIO_AWARE_INIT(lockname) .m_count = 0
|
|
#else
|
|
#define __RWSEM_PRIO_AWARE_INIT(lockname)
|
|
#endif
|
|
|
|
#define __RWSEM_INITIALIZER(name) \
|
|
{ __RWSEM_INIT_COUNT(name), \
|
|
.wait_list = LIST_HEAD_INIT((name).wait_list), \
|
|
.wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
|
|
__RWSEM_OPT_INIT(name) \
|
|
__RWSEM_DEP_MAP_INIT(name), \
|
|
__RWSEM_PRIO_AWARE_INIT(name) }
|
|
|
|
#define DECLARE_RWSEM(name) \
|
|
struct rw_semaphore name = __RWSEM_INITIALIZER(name)
|
|
|
|
extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
|
|
struct lock_class_key *key);
|
|
|
|
#define init_rwsem(sem) \
|
|
do { \
|
|
static struct lock_class_key __key; \
|
|
\
|
|
__init_rwsem((sem), #sem, &__key); \
|
|
} while (0)
|
|
|
|
/*
|
|
* This is the same regardless of which rwsem implementation that is being used.
|
|
* It is just a heuristic meant to be called by somebody alreadying holding the
|
|
* rwsem to see if somebody from an incompatible type is wanting access to the
|
|
* lock.
|
|
*/
|
|
static inline int rwsem_is_contended(struct rw_semaphore *sem)
|
|
{
|
|
return !list_empty(&sem->wait_list);
|
|
}
|
|
|
|
/*
|
|
* lock for reading
|
|
*/
|
|
extern void down_read(struct rw_semaphore *sem);
|
|
extern int __must_check down_read_killable(struct rw_semaphore *sem);
|
|
|
|
/*
|
|
* trylock for reading -- returns 1 if successful, 0 if contention
|
|
*/
|
|
extern int down_read_trylock(struct rw_semaphore *sem);
|
|
|
|
/*
|
|
* lock for writing
|
|
*/
|
|
extern void down_write(struct rw_semaphore *sem);
|
|
extern int __must_check down_write_killable(struct rw_semaphore *sem);
|
|
|
|
/*
|
|
* trylock for writing -- returns 1 if successful, 0 if contention
|
|
*/
|
|
extern int down_write_trylock(struct rw_semaphore *sem);
|
|
|
|
/*
|
|
* release a read lock
|
|
*/
|
|
extern void up_read(struct rw_semaphore *sem);
|
|
|
|
/*
|
|
* release a write lock
|
|
*/
|
|
extern void up_write(struct rw_semaphore *sem);
|
|
|
|
/*
|
|
* downgrade write lock to read lock
|
|
*/
|
|
extern void downgrade_write(struct rw_semaphore *sem);
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
/*
|
|
* nested locking. NOTE: rwsems are not allowed to recurse
|
|
* (which occurs if the same task tries to acquire the same
|
|
* lock instance multiple times), but multiple locks of the
|
|
* same lock class might be taken, if the order of the locks
|
|
* is always the same. This ordering rule can be expressed
|
|
* to lockdep via the _nested() APIs, but enumerating the
|
|
* subclasses that are used. (If the nesting relationship is
|
|
* static then another method for expressing nested locking is
|
|
* the explicit definition of lock class keys and the use of
|
|
* lockdep_set_class() at lock initialization time.
|
|
* See Documentation/locking/lockdep-design.txt for more details.)
|
|
*/
|
|
extern void down_read_nested(struct rw_semaphore *sem, int subclass);
|
|
extern void down_write_nested(struct rw_semaphore *sem, int subclass);
|
|
extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
|
|
extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
|
|
|
|
# define down_write_nest_lock(sem, nest_lock) \
|
|
do { \
|
|
typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
|
|
_down_write_nest_lock(sem, &(nest_lock)->dep_map); \
|
|
} while (0);
|
|
|
|
/*
|
|
* Take/release a lock when not the owner will release it.
|
|
*
|
|
* [ This API should be avoided as much as possible - the
|
|
* proper abstraction for this case is completions. ]
|
|
*/
|
|
extern void down_read_non_owner(struct rw_semaphore *sem);
|
|
extern void up_read_non_owner(struct rw_semaphore *sem);
|
|
#else
|
|
# define down_read_nested(sem, subclass) down_read(sem)
|
|
# define down_write_nest_lock(sem, nest_lock) down_write(sem)
|
|
# define down_write_nested(sem, subclass) down_write(sem)
|
|
# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
|
|
# define down_read_non_owner(sem) down_read(sem)
|
|
# define up_read_non_owner(sem) up_read(sem)
|
|
#endif
|
|
|
|
#endif /* _LINUX_RWSEM_H */
|