forked from luck/tmp_suning_uos_patched
ddb20d1d3a
Modify __down_read_trylock() to optimize for an unlocked rwsem and make it generate slightly better code. Before this patch, down_read_trylock: 0x0000000000000000 <+0>: callq 0x5 <down_read_trylock+5> 0x0000000000000005 <+5>: jmp 0x18 <down_read_trylock+24> 0x0000000000000007 <+7>: lea 0x1(%rdx),%rcx 0x000000000000000b <+11>: mov %rdx,%rax 0x000000000000000e <+14>: lock cmpxchg %rcx,(%rdi) 0x0000000000000013 <+19>: cmp %rax,%rdx 0x0000000000000016 <+22>: je 0x23 <down_read_trylock+35> 0x0000000000000018 <+24>: mov (%rdi),%rdx 0x000000000000001b <+27>: test %rdx,%rdx 0x000000000000001e <+30>: jns 0x7 <down_read_trylock+7> 0x0000000000000020 <+32>: xor %eax,%eax 0x0000000000000022 <+34>: retq 0x0000000000000023 <+35>: mov %gs:0x0,%rax 0x000000000000002c <+44>: or $0x3,%rax 0x0000000000000030 <+48>: mov %rax,0x20(%rdi) 0x0000000000000034 <+52>: mov $0x1,%eax 0x0000000000000039 <+57>: retq After patch, down_read_trylock: 0x0000000000000000 <+0>: callq 0x5 <down_read_trylock+5> 0x0000000000000005 <+5>: xor %eax,%eax 0x0000000000000007 <+7>: lea 0x1(%rax),%rdx 0x000000000000000b <+11>: lock cmpxchg %rdx,(%rdi) 0x0000000000000010 <+16>: jne 0x29 <down_read_trylock+41> 0x0000000000000012 <+18>: mov %gs:0x0,%rax 0x000000000000001b <+27>: or $0x3,%rax 0x000000000000001f <+31>: mov %rax,0x20(%rdi) 0x0000000000000023 <+35>: mov $0x1,%eax 0x0000000000000028 <+40>: retq 0x0000000000000029 <+41>: test %rax,%rax 0x000000000000002c <+44>: jns 0x7 <down_read_trylock+7> 0x000000000000002e <+46>: xor %eax,%eax 0x0000000000000030 <+48>: retq By using a rwsem microbenchmark, the down_read_trylock() rate (with a load of 10 to lengthen the lock critical section) on a x86-64 system before and after the patch were: Before Patch After Patch # of Threads rlock rlock ------------ ----- ----- 1 14,496 14,716 2 8,644 8,453 4 6,799 6,983 8 5,664 7,190 On a ARM64 system, the performance results were: Before Patch After Patch # of Threads rlock rlock ------------ ----- ----- 1 23,676 24,488 2 7,697 9,502 4 4,945 3,440 8 2,641 1,603 For the uncontended case (1 thread), the new down_read_trylock() is a little bit faster. For the contended cases, the new down_read_trylock() perform pretty well in x86-64, but performance degrades at high contention level on ARM64. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> 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-4-longman@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
265 lines
7.2 KiB
C
265 lines
7.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/*
|
|
* The least significant 2 bits of the owner value has the following
|
|
* meanings when set.
|
|
* - RWSEM_READER_OWNED (bit 0): The rwsem is owned by readers
|
|
* - RWSEM_ANONYMOUSLY_OWNED (bit 1): The rwsem is anonymously owned,
|
|
* i.e. the owner(s) cannot be readily determined. It can be reader
|
|
* owned or the owning writer is indeterminate.
|
|
*
|
|
* When a writer acquires a rwsem, it puts its task_struct pointer
|
|
* into the owner field. It is cleared after an unlock.
|
|
*
|
|
* When a reader acquires a rwsem, it will also puts its task_struct
|
|
* pointer into the owner field with both the RWSEM_READER_OWNED and
|
|
* RWSEM_ANONYMOUSLY_OWNED bits set. On unlock, the owner field will
|
|
* largely be left untouched. So for a free or reader-owned rwsem,
|
|
* the owner value may contain information about the last reader that
|
|
* acquires the rwsem. The anonymous bit is set because that particular
|
|
* reader may or may not still own the lock.
|
|
*
|
|
* That information may be helpful in debugging cases where the system
|
|
* seems to hang on a reader owned rwsem especially if only one reader
|
|
* is involved. Ideally we would like to track all the readers that own
|
|
* a rwsem, but the overhead is simply too big.
|
|
*/
|
|
#define RWSEM_READER_OWNED (1UL << 0)
|
|
#define RWSEM_ANONYMOUSLY_OWNED (1UL << 1)
|
|
|
|
#ifdef CONFIG_DEBUG_RWSEMS
|
|
# define DEBUG_RWSEMS_WARN_ON(c) DEBUG_LOCKS_WARN_ON(c)
|
|
#else
|
|
# define DEBUG_RWSEMS_WARN_ON(c)
|
|
#endif
|
|
|
|
/*
|
|
* R/W semaphores originally for PPC using the stuff in lib/rwsem.c.
|
|
* Adapted largely from include/asm-i386/rwsem.h
|
|
* by Paul Mackerras <paulus@samba.org>.
|
|
*/
|
|
|
|
/*
|
|
* the semaphore definition
|
|
*/
|
|
#ifdef CONFIG_64BIT
|
|
# define RWSEM_ACTIVE_MASK 0xffffffffL
|
|
#else
|
|
# define RWSEM_ACTIVE_MASK 0x0000ffffL
|
|
#endif
|
|
|
|
#define RWSEM_ACTIVE_BIAS 0x00000001L
|
|
#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
|
|
#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
|
|
#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
|
|
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
|
/*
|
|
* All writes to owner are protected by WRITE_ONCE() to make sure that
|
|
* store tearing can't happen as optimistic spinners may read and use
|
|
* the owner value concurrently without lock. Read from owner, however,
|
|
* may not need READ_ONCE() as long as the pointer value is only used
|
|
* for comparison and isn't being dereferenced.
|
|
*/
|
|
static inline void rwsem_set_owner(struct rw_semaphore *sem)
|
|
{
|
|
WRITE_ONCE(sem->owner, current);
|
|
}
|
|
|
|
static inline void rwsem_clear_owner(struct rw_semaphore *sem)
|
|
{
|
|
WRITE_ONCE(sem->owner, NULL);
|
|
}
|
|
|
|
/*
|
|
* The task_struct pointer of the last owning reader will be left in
|
|
* the owner field.
|
|
*
|
|
* Note that the owner value just indicates the task has owned the rwsem
|
|
* previously, it may not be the real owner or one of the real owners
|
|
* anymore when that field is examined, so take it with a grain of salt.
|
|
*/
|
|
static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem,
|
|
struct task_struct *owner)
|
|
{
|
|
unsigned long val = (unsigned long)owner | RWSEM_READER_OWNED
|
|
| RWSEM_ANONYMOUSLY_OWNED;
|
|
|
|
WRITE_ONCE(sem->owner, (struct task_struct *)val);
|
|
}
|
|
|
|
static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
|
|
{
|
|
__rwsem_set_reader_owned(sem, current);
|
|
}
|
|
|
|
/*
|
|
* Return true if the a rwsem waiter can spin on the rwsem's owner
|
|
* and steal the lock, i.e. the lock is not anonymously owned.
|
|
* N.B. !owner is considered spinnable.
|
|
*/
|
|
static inline bool is_rwsem_owner_spinnable(struct task_struct *owner)
|
|
{
|
|
return !((unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED);
|
|
}
|
|
|
|
/*
|
|
* Return true if rwsem is owned by an anonymous writer or readers.
|
|
*/
|
|
static inline bool rwsem_has_anonymous_owner(struct task_struct *owner)
|
|
{
|
|
return (unsigned long)owner & RWSEM_ANONYMOUSLY_OWNED;
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG_RWSEMS
|
|
/*
|
|
* With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
|
|
* is a task pointer in owner of a reader-owned rwsem, it will be the
|
|
* real owner or one of the real owners. The only exception is when the
|
|
* unlock is done by up_read_non_owner().
|
|
*/
|
|
#define rwsem_clear_reader_owned rwsem_clear_reader_owned
|
|
static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
|
|
{
|
|
unsigned long val = (unsigned long)current | RWSEM_READER_OWNED
|
|
| RWSEM_ANONYMOUSLY_OWNED;
|
|
if (READ_ONCE(sem->owner) == (struct task_struct *)val)
|
|
cmpxchg_relaxed((unsigned long *)&sem->owner, val,
|
|
RWSEM_READER_OWNED | RWSEM_ANONYMOUSLY_OWNED);
|
|
}
|
|
#endif
|
|
|
|
#else
|
|
static inline void rwsem_set_owner(struct rw_semaphore *sem)
|
|
{
|
|
}
|
|
|
|
static inline void rwsem_clear_owner(struct rw_semaphore *sem)
|
|
{
|
|
}
|
|
|
|
static inline void __rwsem_set_reader_owned(struct rw_semaphore *sem,
|
|
struct task_struct *owner)
|
|
{
|
|
}
|
|
|
|
static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
|
|
{
|
|
}
|
|
#endif
|
|
|
|
#ifndef rwsem_clear_reader_owned
|
|
static inline void rwsem_clear_reader_owned(struct rw_semaphore *sem)
|
|
{
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* lock for reading
|
|
*/
|
|
static inline void __down_read(struct rw_semaphore *sem)
|
|
{
|
|
if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0))
|
|
rwsem_down_read_failed(sem);
|
|
}
|
|
|
|
static inline int __down_read_killable(struct rw_semaphore *sem)
|
|
{
|
|
if (unlikely(atomic_long_inc_return_acquire(&sem->count) <= 0)) {
|
|
if (IS_ERR(rwsem_down_read_failed_killable(sem)))
|
|
return -EINTR;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static inline int __down_read_trylock(struct rw_semaphore *sem)
|
|
{
|
|
/*
|
|
* Optimize for the case when the rwsem is not locked at all.
|
|
*/
|
|
long tmp = RWSEM_UNLOCKED_VALUE;
|
|
|
|
do {
|
|
if (atomic_long_try_cmpxchg_acquire(&sem->count, &tmp,
|
|
tmp + RWSEM_ACTIVE_READ_BIAS)) {
|
|
return 1;
|
|
}
|
|
} while (tmp >= 0);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* lock for writing
|
|
*/
|
|
static inline void __down_write(struct rw_semaphore *sem)
|
|
{
|
|
long tmp;
|
|
|
|
tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
|
|
&sem->count);
|
|
if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
|
|
rwsem_down_write_failed(sem);
|
|
}
|
|
|
|
static inline int __down_write_killable(struct rw_semaphore *sem)
|
|
{
|
|
long tmp;
|
|
|
|
tmp = atomic_long_add_return_acquire(RWSEM_ACTIVE_WRITE_BIAS,
|
|
&sem->count);
|
|
if (unlikely(tmp != RWSEM_ACTIVE_WRITE_BIAS))
|
|
if (IS_ERR(rwsem_down_write_failed_killable(sem)))
|
|
return -EINTR;
|
|
return 0;
|
|
}
|
|
|
|
static inline int __down_write_trylock(struct rw_semaphore *sem)
|
|
{
|
|
long tmp;
|
|
|
|
tmp = atomic_long_cmpxchg_acquire(&sem->count, RWSEM_UNLOCKED_VALUE,
|
|
RWSEM_ACTIVE_WRITE_BIAS);
|
|
return tmp == RWSEM_UNLOCKED_VALUE;
|
|
}
|
|
|
|
/*
|
|
* unlock after reading
|
|
*/
|
|
static inline void __up_read(struct rw_semaphore *sem)
|
|
{
|
|
long tmp;
|
|
|
|
tmp = atomic_long_dec_return_release(&sem->count);
|
|
if (unlikely(tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0))
|
|
rwsem_wake(sem);
|
|
}
|
|
|
|
/*
|
|
* unlock after writing
|
|
*/
|
|
static inline void __up_write(struct rw_semaphore *sem)
|
|
{
|
|
if (unlikely(atomic_long_sub_return_release(RWSEM_ACTIVE_WRITE_BIAS,
|
|
&sem->count) < 0))
|
|
rwsem_wake(sem);
|
|
}
|
|
|
|
/*
|
|
* downgrade write lock to read lock
|
|
*/
|
|
static inline void __downgrade_write(struct rw_semaphore *sem)
|
|
{
|
|
long tmp;
|
|
|
|
/*
|
|
* When downgrading from exclusive to shared ownership,
|
|
* anything inside the write-locked region cannot leak
|
|
* into the read side. In contrast, anything in the
|
|
* read-locked region is ok to be re-ordered into the
|
|
* write side. As such, rely on RELEASE semantics.
|
|
*/
|
|
tmp = atomic_long_add_return_release(-RWSEM_WAITING_BIAS, &sem->count);
|
|
if (tmp < 0)
|
|
rwsem_downgrade_wake(sem);
|
|
}
|