2017-11-06 17:11:51 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-04-16 10:15:50 +00:00
|
|
|
/*
|
|
|
|
* Ldisc rw semaphore
|
|
|
|
*
|
|
|
|
* The ldisc semaphore is semantically a rw_semaphore but which enforces
|
|
|
|
* an alternate policy, namely:
|
|
|
|
* 1) Supports lock wait timeouts
|
|
|
|
* 2) Write waiter has priority
|
|
|
|
* 3) Downgrading is not supported
|
|
|
|
*
|
|
|
|
* Implementation notes:
|
|
|
|
* 1) Upper half of semaphore count is a wait count (differs from rwsem
|
|
|
|
* in that rwsem normalizes the upper half to the wait bias)
|
|
|
|
* 2) Lacks overflow checking
|
|
|
|
*
|
|
|
|
* The generic counting was copied and modified from include/asm-generic/rwsem.h
|
|
|
|
* by Paul Mackerras <paulus@samba.org>.
|
|
|
|
*
|
|
|
|
* The scheduling policy was copied and modified from lib/rwsem.c
|
|
|
|
* Written by David Howells (dhowells@redhat.com).
|
|
|
|
*
|
|
|
|
* This implementation incorporates the write lock stealing work of
|
|
|
|
* Michel Lespinasse <walken@google.com>.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Peter Hurley <peter@hurleysoftware.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/atomic.h>
|
|
|
|
#include <linux/tty.h>
|
|
|
|
#include <linux/sched.h>
|
2017-02-08 17:51:35 +00:00
|
|
|
#include <linux/sched/debug.h>
|
2017-02-05 14:30:50 +00:00
|
|
|
#include <linux/sched/task.h>
|
2013-04-16 10:15:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
#if BITS_PER_LONG == 64
|
|
|
|
# define LDSEM_ACTIVE_MASK 0xffffffffL
|
|
|
|
#else
|
|
|
|
# define LDSEM_ACTIVE_MASK 0x0000ffffL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LDSEM_UNLOCKED 0L
|
|
|
|
#define LDSEM_ACTIVE_BIAS 1L
|
|
|
|
#define LDSEM_WAIT_BIAS (-LDSEM_ACTIVE_MASK-1)
|
|
|
|
#define LDSEM_READ_BIAS LDSEM_ACTIVE_BIAS
|
|
|
|
#define LDSEM_WRITE_BIAS (LDSEM_WAIT_BIAS + LDSEM_ACTIVE_BIAS)
|
|
|
|
|
|
|
|
struct ldsem_waiter {
|
|
|
|
struct list_head list;
|
|
|
|
struct task_struct *task;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize an ldsem:
|
|
|
|
*/
|
|
|
|
void __init_ldsem(struct ld_semaphore *sem, const char *name,
|
|
|
|
struct lock_class_key *key)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
/*
|
|
|
|
* Make sure we are not reinitializing a held semaphore:
|
|
|
|
*/
|
|
|
|
debug_check_no_locks_freed((void *)sem, sizeof(*sem));
|
|
|
|
lockdep_init_map(&sem->dep_map, name, key, 0);
|
|
|
|
#endif
|
2018-06-05 14:53:34 +00:00
|
|
|
atomic_long_set(&sem->count, LDSEM_UNLOCKED);
|
2013-04-16 10:15:50 +00:00
|
|
|
sem->wait_readers = 0;
|
|
|
|
raw_spin_lock_init(&sem->wait_lock);
|
|
|
|
INIT_LIST_HEAD(&sem->read_wait);
|
|
|
|
INIT_LIST_HEAD(&sem->write_wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __ldsem_wake_readers(struct ld_semaphore *sem)
|
|
|
|
{
|
|
|
|
struct ldsem_waiter *waiter, *next;
|
|
|
|
struct task_struct *tsk;
|
|
|
|
long adjust, count;
|
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
/*
|
|
|
|
* Try to grant read locks to all readers on the read wait list.
|
2013-04-16 10:15:50 +00:00
|
|
|
* Note the 'active part' of the count is incremented by
|
|
|
|
* the number of readers before waking any processes up.
|
|
|
|
*/
|
|
|
|
adjust = sem->wait_readers * (LDSEM_ACTIVE_BIAS - LDSEM_WAIT_BIAS);
|
2018-06-05 14:53:34 +00:00
|
|
|
count = atomic_long_add_return(adjust, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
do {
|
|
|
|
if (count > 0)
|
|
|
|
break;
|
2018-06-05 14:53:34 +00:00
|
|
|
if (atomic_long_try_cmpxchg(&sem->count, &count, count - adjust))
|
2013-04-16 10:15:50 +00:00
|
|
|
return;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(waiter, next, &sem->read_wait, list) {
|
|
|
|
tsk = waiter->task;
|
2019-07-18 13:03:15 +00:00
|
|
|
smp_store_release(&waiter->task, NULL);
|
2013-04-16 10:15:50 +00:00
|
|
|
wake_up_process(tsk);
|
|
|
|
put_task_struct(tsk);
|
|
|
|
}
|
|
|
|
INIT_LIST_HEAD(&sem->read_wait);
|
|
|
|
sem->wait_readers = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int writer_trylock(struct ld_semaphore *sem)
|
|
|
|
{
|
2018-06-05 14:53:34 +00:00
|
|
|
/*
|
|
|
|
* Only wake this writer if the active part of the count can be
|
2013-04-16 10:15:50 +00:00
|
|
|
* transitioned from 0 -> 1
|
|
|
|
*/
|
2018-06-05 14:53:34 +00:00
|
|
|
long count = atomic_long_add_return(LDSEM_ACTIVE_BIAS, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
do {
|
|
|
|
if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS)
|
|
|
|
return 1;
|
2018-06-05 14:53:34 +00:00
|
|
|
if (atomic_long_try_cmpxchg(&sem->count, &count, count - LDSEM_ACTIVE_BIAS))
|
2013-04-16 10:15:50 +00:00
|
|
|
return 0;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __ldsem_wake_writer(struct ld_semaphore *sem)
|
|
|
|
{
|
|
|
|
struct ldsem_waiter *waiter;
|
|
|
|
|
|
|
|
waiter = list_entry(sem->write_wait.next, struct ldsem_waiter, list);
|
|
|
|
wake_up_process(waiter->task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle the lock release when processes blocked on it that can now run
|
|
|
|
* - if we come here from up_xxxx(), then:
|
|
|
|
* - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
|
|
|
|
* - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
|
|
|
|
* - the spinlock must be held by the caller
|
|
|
|
* - woken process blocks are discarded from the list after having task zeroed
|
|
|
|
*/
|
|
|
|
static void __ldsem_wake(struct ld_semaphore *sem)
|
|
|
|
{
|
|
|
|
if (!list_empty(&sem->write_wait))
|
|
|
|
__ldsem_wake_writer(sem);
|
|
|
|
else if (!list_empty(&sem->read_wait))
|
|
|
|
__ldsem_wake_readers(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ldsem_wake(struct ld_semaphore *sem)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
raw_spin_lock_irqsave(&sem->wait_lock, flags);
|
|
|
|
__ldsem_wake(sem);
|
|
|
|
raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait for the read lock to be granted
|
|
|
|
*/
|
|
|
|
static struct ld_semaphore __sched *
|
|
|
|
down_read_failed(struct ld_semaphore *sem, long count, long timeout)
|
|
|
|
{
|
|
|
|
struct ldsem_waiter waiter;
|
|
|
|
long adjust = -LDSEM_ACTIVE_BIAS + LDSEM_WAIT_BIAS;
|
|
|
|
|
|
|
|
/* set up my own style of waitqueue */
|
|
|
|
raw_spin_lock_irq(&sem->wait_lock);
|
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
/*
|
|
|
|
* Try to reverse the lock attempt but if the count has changed
|
2013-04-16 10:15:50 +00:00
|
|
|
* so that reversing fails, check if there are are no waiters,
|
2018-06-05 14:53:34 +00:00
|
|
|
* and early-out if not
|
|
|
|
*/
|
2013-04-16 10:15:50 +00:00
|
|
|
do {
|
2018-06-05 14:53:34 +00:00
|
|
|
if (atomic_long_try_cmpxchg(&sem->count, &count, count + adjust)) {
|
|
|
|
count += adjust;
|
2013-04-16 10:15:50 +00:00
|
|
|
break;
|
2018-06-05 14:53:34 +00:00
|
|
|
}
|
2013-04-16 10:15:50 +00:00
|
|
|
if (count > 0) {
|
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
list_add_tail(&waiter.list, &sem->read_wait);
|
|
|
|
sem->wait_readers++;
|
|
|
|
|
2017-01-03 21:43:12 +00:00
|
|
|
waiter.task = current;
|
|
|
|
get_task_struct(current);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
|
|
|
/* if there are no active locks, wake the new lock owner(s) */
|
|
|
|
if ((count & LDSEM_ACTIVE_MASK) == 0)
|
|
|
|
__ldsem_wake(sem);
|
|
|
|
|
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
|
|
|
|
/* wait to be given the lock */
|
|
|
|
for (;;) {
|
sched/core: Remove set_task_state()
This is a nasty interface and setting the state of a foreign task must
not be done. As of the following commit:
be628be0956 ("bcache: Make gc wakeup sane, remove set_task_state()")
... everyone in the kernel calls set_task_state() with current, allowing
the helper to be removed.
However, as the comment indicates, it is still around for those archs
where computing current is more expensive than using a pointer, at least
in theory. An important arch that is affected is arm64, however this has
been addressed now [1] and performance is up to par making no difference
with either calls.
Of all the callers, if any, it's the locking bits that would care most
about this -- ie: we end up passing a tsk pointer to a lot of the lock
slowpath, and setting ->state on that. The following numbers are based
on two tests: a custom ad-hoc microbenchmark that just measures
latencies (for ~65 million calls) between get_task_state() vs
get_current_state().
Secondly for a higher overview, an unlink microbenchmark was used,
which pounds on a single file with open, close,unlink combos with
increasing thread counts (up to 4x ncpus). While the workload is quite
unrealistic, it does contend a lot on the inode mutex or now rwsem.
[1] https://lkml.kernel.org/r/1483468021-8237-1-git-send-email-mark.rutland@arm.com
== 1. x86-64 ==
Avg runtime set_task_state(): 601 msecs
Avg runtime set_current_state(): 552 msecs
vanilla dirty
Hmean unlink1-processes-2 36089.26 ( 0.00%) 38977.33 ( 8.00%)
Hmean unlink1-processes-5 28555.01 ( 0.00%) 29832.55 ( 4.28%)
Hmean unlink1-processes-8 37323.75 ( 0.00%) 44974.57 ( 20.50%)
Hmean unlink1-processes-12 43571.88 ( 0.00%) 44283.01 ( 1.63%)
Hmean unlink1-processes-21 34431.52 ( 0.00%) 38284.45 ( 11.19%)
Hmean unlink1-processes-30 34813.26 ( 0.00%) 37975.17 ( 9.08%)
Hmean unlink1-processes-48 37048.90 ( 0.00%) 39862.78 ( 7.59%)
Hmean unlink1-processes-79 35630.01 ( 0.00%) 36855.30 ( 3.44%)
Hmean unlink1-processes-110 36115.85 ( 0.00%) 39843.91 ( 10.32%)
Hmean unlink1-processes-141 32546.96 ( 0.00%) 35418.52 ( 8.82%)
Hmean unlink1-processes-172 34674.79 ( 0.00%) 36899.21 ( 6.42%)
Hmean unlink1-processes-203 37303.11 ( 0.00%) 36393.04 ( -2.44%)
Hmean unlink1-processes-224 35712.13 ( 0.00%) 36685.96 ( 2.73%)
== 2. ppc64le ==
Avg runtime set_task_state(): 938 msecs
Avg runtime set_current_state: 940 msecs
vanilla dirty
Hmean unlink1-processes-2 19269.19 ( 0.00%) 30704.50 ( 59.35%)
Hmean unlink1-processes-5 20106.15 ( 0.00%) 21804.15 ( 8.45%)
Hmean unlink1-processes-8 17496.97 ( 0.00%) 17243.28 ( -1.45%)
Hmean unlink1-processes-12 14224.15 ( 0.00%) 17240.21 ( 21.20%)
Hmean unlink1-processes-21 14155.66 ( 0.00%) 15681.23 ( 10.78%)
Hmean unlink1-processes-30 14450.70 ( 0.00%) 15995.83 ( 10.69%)
Hmean unlink1-processes-48 16945.57 ( 0.00%) 16370.42 ( -3.39%)
Hmean unlink1-processes-79 15788.39 ( 0.00%) 14639.27 ( -7.28%)
Hmean unlink1-processes-110 14268.48 ( 0.00%) 14377.40 ( 0.76%)
Hmean unlink1-processes-141 14023.65 ( 0.00%) 16271.69 ( 16.03%)
Hmean unlink1-processes-172 13417.62 ( 0.00%) 16067.55 ( 19.75%)
Hmean unlink1-processes-203 15293.08 ( 0.00%) 15440.40 ( 0.96%)
Hmean unlink1-processes-234 13719.32 ( 0.00%) 16190.74 ( 18.01%)
Hmean unlink1-processes-265 16400.97 ( 0.00%) 16115.22 ( -1.74%)
Hmean unlink1-processes-296 14388.60 ( 0.00%) 16216.13 ( 12.70%)
Hmean unlink1-processes-320 15771.85 ( 0.00%) 15905.96 ( 0.85%)
x86-64 (known to be fast for get_current()/this_cpu_read_stable() caching)
and ppc64 (with paca) show similar improvements in the unlink microbenches.
The small delta for ppc64 (2ms), does not represent the gains on the unlink
runs. In the case of x86, there was a decent amount of variation in the
latency runs, but always within a 20 to 50ms increase), ppc was more constant.
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@stgolabs.net
Cc: mark.rutland@arm.com
Link: http://lkml.kernel.org/r/1483479794-14013-5-git-send-email-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-03 21:43:14 +00:00
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
2019-07-18 13:03:15 +00:00
|
|
|
if (!smp_load_acquire(&waiter.task))
|
2013-04-16 10:15:50 +00:00
|
|
|
break;
|
|
|
|
if (!timeout)
|
|
|
|
break;
|
|
|
|
timeout = schedule_timeout(timeout);
|
|
|
|
}
|
|
|
|
|
sched/core: Remove set_task_state()
This is a nasty interface and setting the state of a foreign task must
not be done. As of the following commit:
be628be0956 ("bcache: Make gc wakeup sane, remove set_task_state()")
... everyone in the kernel calls set_task_state() with current, allowing
the helper to be removed.
However, as the comment indicates, it is still around for those archs
where computing current is more expensive than using a pointer, at least
in theory. An important arch that is affected is arm64, however this has
been addressed now [1] and performance is up to par making no difference
with either calls.
Of all the callers, if any, it's the locking bits that would care most
about this -- ie: we end up passing a tsk pointer to a lot of the lock
slowpath, and setting ->state on that. The following numbers are based
on two tests: a custom ad-hoc microbenchmark that just measures
latencies (for ~65 million calls) between get_task_state() vs
get_current_state().
Secondly for a higher overview, an unlink microbenchmark was used,
which pounds on a single file with open, close,unlink combos with
increasing thread counts (up to 4x ncpus). While the workload is quite
unrealistic, it does contend a lot on the inode mutex or now rwsem.
[1] https://lkml.kernel.org/r/1483468021-8237-1-git-send-email-mark.rutland@arm.com
== 1. x86-64 ==
Avg runtime set_task_state(): 601 msecs
Avg runtime set_current_state(): 552 msecs
vanilla dirty
Hmean unlink1-processes-2 36089.26 ( 0.00%) 38977.33 ( 8.00%)
Hmean unlink1-processes-5 28555.01 ( 0.00%) 29832.55 ( 4.28%)
Hmean unlink1-processes-8 37323.75 ( 0.00%) 44974.57 ( 20.50%)
Hmean unlink1-processes-12 43571.88 ( 0.00%) 44283.01 ( 1.63%)
Hmean unlink1-processes-21 34431.52 ( 0.00%) 38284.45 ( 11.19%)
Hmean unlink1-processes-30 34813.26 ( 0.00%) 37975.17 ( 9.08%)
Hmean unlink1-processes-48 37048.90 ( 0.00%) 39862.78 ( 7.59%)
Hmean unlink1-processes-79 35630.01 ( 0.00%) 36855.30 ( 3.44%)
Hmean unlink1-processes-110 36115.85 ( 0.00%) 39843.91 ( 10.32%)
Hmean unlink1-processes-141 32546.96 ( 0.00%) 35418.52 ( 8.82%)
Hmean unlink1-processes-172 34674.79 ( 0.00%) 36899.21 ( 6.42%)
Hmean unlink1-processes-203 37303.11 ( 0.00%) 36393.04 ( -2.44%)
Hmean unlink1-processes-224 35712.13 ( 0.00%) 36685.96 ( 2.73%)
== 2. ppc64le ==
Avg runtime set_task_state(): 938 msecs
Avg runtime set_current_state: 940 msecs
vanilla dirty
Hmean unlink1-processes-2 19269.19 ( 0.00%) 30704.50 ( 59.35%)
Hmean unlink1-processes-5 20106.15 ( 0.00%) 21804.15 ( 8.45%)
Hmean unlink1-processes-8 17496.97 ( 0.00%) 17243.28 ( -1.45%)
Hmean unlink1-processes-12 14224.15 ( 0.00%) 17240.21 ( 21.20%)
Hmean unlink1-processes-21 14155.66 ( 0.00%) 15681.23 ( 10.78%)
Hmean unlink1-processes-30 14450.70 ( 0.00%) 15995.83 ( 10.69%)
Hmean unlink1-processes-48 16945.57 ( 0.00%) 16370.42 ( -3.39%)
Hmean unlink1-processes-79 15788.39 ( 0.00%) 14639.27 ( -7.28%)
Hmean unlink1-processes-110 14268.48 ( 0.00%) 14377.40 ( 0.76%)
Hmean unlink1-processes-141 14023.65 ( 0.00%) 16271.69 ( 16.03%)
Hmean unlink1-processes-172 13417.62 ( 0.00%) 16067.55 ( 19.75%)
Hmean unlink1-processes-203 15293.08 ( 0.00%) 15440.40 ( 0.96%)
Hmean unlink1-processes-234 13719.32 ( 0.00%) 16190.74 ( 18.01%)
Hmean unlink1-processes-265 16400.97 ( 0.00%) 16115.22 ( -1.74%)
Hmean unlink1-processes-296 14388.60 ( 0.00%) 16216.13 ( 12.70%)
Hmean unlink1-processes-320 15771.85 ( 0.00%) 15905.96 ( 0.85%)
x86-64 (known to be fast for get_current()/this_cpu_read_stable() caching)
and ppc64 (with paca) show similar improvements in the unlink microbenches.
The small delta for ppc64 (2ms), does not represent the gains on the unlink
runs. In the case of x86, there was a decent amount of variation in the
latency runs, but always within a 20 to 50ms increase), ppc was more constant.
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@stgolabs.net
Cc: mark.rutland@arm.com
Link: http://lkml.kernel.org/r/1483479794-14013-5-git-send-email-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-03 21:43:14 +00:00
|
|
|
__set_current_state(TASK_RUNNING);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
|
|
|
if (!timeout) {
|
2018-06-05 14:53:34 +00:00
|
|
|
/*
|
|
|
|
* Lock timed out but check if this task was just
|
2013-04-16 10:15:50 +00:00
|
|
|
* granted lock ownership - if so, pretend there
|
2018-06-05 14:53:34 +00:00
|
|
|
* was no timeout; otherwise, cleanup lock wait.
|
|
|
|
*/
|
2013-04-16 10:15:50 +00:00
|
|
|
raw_spin_lock_irq(&sem->wait_lock);
|
|
|
|
if (waiter.task) {
|
2018-06-05 14:53:34 +00:00
|
|
|
atomic_long_add_return(-LDSEM_WAIT_BIAS, &sem->count);
|
2018-11-01 00:24:52 +00:00
|
|
|
sem->wait_readers--;
|
2013-04-16 10:15:50 +00:00
|
|
|
list_del(&waiter.list);
|
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
put_task_struct(waiter.task);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait for the write lock to be granted
|
|
|
|
*/
|
|
|
|
static struct ld_semaphore __sched *
|
|
|
|
down_write_failed(struct ld_semaphore *sem, long count, long timeout)
|
|
|
|
{
|
|
|
|
struct ldsem_waiter waiter;
|
|
|
|
long adjust = -LDSEM_ACTIVE_BIAS;
|
|
|
|
int locked = 0;
|
|
|
|
|
|
|
|
/* set up my own style of waitqueue */
|
|
|
|
raw_spin_lock_irq(&sem->wait_lock);
|
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
/*
|
|
|
|
* Try to reverse the lock attempt but if the count has changed
|
2013-04-16 10:15:50 +00:00
|
|
|
* so that reversing fails, check if the lock is now owned,
|
2018-06-05 14:53:34 +00:00
|
|
|
* and early-out if so.
|
|
|
|
*/
|
2013-04-16 10:15:50 +00:00
|
|
|
do {
|
2018-06-05 14:53:34 +00:00
|
|
|
if (atomic_long_try_cmpxchg(&sem->count, &count, count + adjust))
|
2013-04-16 10:15:50 +00:00
|
|
|
break;
|
|
|
|
if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS) {
|
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
list_add_tail(&waiter.list, &sem->write_wait);
|
|
|
|
|
2017-01-03 21:43:12 +00:00
|
|
|
waiter.task = current;
|
2013-04-16 10:15:50 +00:00
|
|
|
|
sched/core: Remove set_task_state()
This is a nasty interface and setting the state of a foreign task must
not be done. As of the following commit:
be628be0956 ("bcache: Make gc wakeup sane, remove set_task_state()")
... everyone in the kernel calls set_task_state() with current, allowing
the helper to be removed.
However, as the comment indicates, it is still around for those archs
where computing current is more expensive than using a pointer, at least
in theory. An important arch that is affected is arm64, however this has
been addressed now [1] and performance is up to par making no difference
with either calls.
Of all the callers, if any, it's the locking bits that would care most
about this -- ie: we end up passing a tsk pointer to a lot of the lock
slowpath, and setting ->state on that. The following numbers are based
on two tests: a custom ad-hoc microbenchmark that just measures
latencies (for ~65 million calls) between get_task_state() vs
get_current_state().
Secondly for a higher overview, an unlink microbenchmark was used,
which pounds on a single file with open, close,unlink combos with
increasing thread counts (up to 4x ncpus). While the workload is quite
unrealistic, it does contend a lot on the inode mutex or now rwsem.
[1] https://lkml.kernel.org/r/1483468021-8237-1-git-send-email-mark.rutland@arm.com
== 1. x86-64 ==
Avg runtime set_task_state(): 601 msecs
Avg runtime set_current_state(): 552 msecs
vanilla dirty
Hmean unlink1-processes-2 36089.26 ( 0.00%) 38977.33 ( 8.00%)
Hmean unlink1-processes-5 28555.01 ( 0.00%) 29832.55 ( 4.28%)
Hmean unlink1-processes-8 37323.75 ( 0.00%) 44974.57 ( 20.50%)
Hmean unlink1-processes-12 43571.88 ( 0.00%) 44283.01 ( 1.63%)
Hmean unlink1-processes-21 34431.52 ( 0.00%) 38284.45 ( 11.19%)
Hmean unlink1-processes-30 34813.26 ( 0.00%) 37975.17 ( 9.08%)
Hmean unlink1-processes-48 37048.90 ( 0.00%) 39862.78 ( 7.59%)
Hmean unlink1-processes-79 35630.01 ( 0.00%) 36855.30 ( 3.44%)
Hmean unlink1-processes-110 36115.85 ( 0.00%) 39843.91 ( 10.32%)
Hmean unlink1-processes-141 32546.96 ( 0.00%) 35418.52 ( 8.82%)
Hmean unlink1-processes-172 34674.79 ( 0.00%) 36899.21 ( 6.42%)
Hmean unlink1-processes-203 37303.11 ( 0.00%) 36393.04 ( -2.44%)
Hmean unlink1-processes-224 35712.13 ( 0.00%) 36685.96 ( 2.73%)
== 2. ppc64le ==
Avg runtime set_task_state(): 938 msecs
Avg runtime set_current_state: 940 msecs
vanilla dirty
Hmean unlink1-processes-2 19269.19 ( 0.00%) 30704.50 ( 59.35%)
Hmean unlink1-processes-5 20106.15 ( 0.00%) 21804.15 ( 8.45%)
Hmean unlink1-processes-8 17496.97 ( 0.00%) 17243.28 ( -1.45%)
Hmean unlink1-processes-12 14224.15 ( 0.00%) 17240.21 ( 21.20%)
Hmean unlink1-processes-21 14155.66 ( 0.00%) 15681.23 ( 10.78%)
Hmean unlink1-processes-30 14450.70 ( 0.00%) 15995.83 ( 10.69%)
Hmean unlink1-processes-48 16945.57 ( 0.00%) 16370.42 ( -3.39%)
Hmean unlink1-processes-79 15788.39 ( 0.00%) 14639.27 ( -7.28%)
Hmean unlink1-processes-110 14268.48 ( 0.00%) 14377.40 ( 0.76%)
Hmean unlink1-processes-141 14023.65 ( 0.00%) 16271.69 ( 16.03%)
Hmean unlink1-processes-172 13417.62 ( 0.00%) 16067.55 ( 19.75%)
Hmean unlink1-processes-203 15293.08 ( 0.00%) 15440.40 ( 0.96%)
Hmean unlink1-processes-234 13719.32 ( 0.00%) 16190.74 ( 18.01%)
Hmean unlink1-processes-265 16400.97 ( 0.00%) 16115.22 ( -1.74%)
Hmean unlink1-processes-296 14388.60 ( 0.00%) 16216.13 ( 12.70%)
Hmean unlink1-processes-320 15771.85 ( 0.00%) 15905.96 ( 0.85%)
x86-64 (known to be fast for get_current()/this_cpu_read_stable() caching)
and ppc64 (with paca) show similar improvements in the unlink microbenches.
The small delta for ppc64 (2ms), does not represent the gains on the unlink
runs. In the case of x86, there was a decent amount of variation in the
latency runs, but always within a 20 to 50ms increase), ppc was more constant.
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@stgolabs.net
Cc: mark.rutland@arm.com
Link: http://lkml.kernel.org/r/1483479794-14013-5-git-send-email-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-03 21:43:14 +00:00
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
2013-04-16 10:15:50 +00:00
|
|
|
for (;;) {
|
|
|
|
if (!timeout)
|
|
|
|
break;
|
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
timeout = schedule_timeout(timeout);
|
|
|
|
raw_spin_lock_irq(&sem->wait_lock);
|
sched/core: Remove set_task_state()
This is a nasty interface and setting the state of a foreign task must
not be done. As of the following commit:
be628be0956 ("bcache: Make gc wakeup sane, remove set_task_state()")
... everyone in the kernel calls set_task_state() with current, allowing
the helper to be removed.
However, as the comment indicates, it is still around for those archs
where computing current is more expensive than using a pointer, at least
in theory. An important arch that is affected is arm64, however this has
been addressed now [1] and performance is up to par making no difference
with either calls.
Of all the callers, if any, it's the locking bits that would care most
about this -- ie: we end up passing a tsk pointer to a lot of the lock
slowpath, and setting ->state on that. The following numbers are based
on two tests: a custom ad-hoc microbenchmark that just measures
latencies (for ~65 million calls) between get_task_state() vs
get_current_state().
Secondly for a higher overview, an unlink microbenchmark was used,
which pounds on a single file with open, close,unlink combos with
increasing thread counts (up to 4x ncpus). While the workload is quite
unrealistic, it does contend a lot on the inode mutex or now rwsem.
[1] https://lkml.kernel.org/r/1483468021-8237-1-git-send-email-mark.rutland@arm.com
== 1. x86-64 ==
Avg runtime set_task_state(): 601 msecs
Avg runtime set_current_state(): 552 msecs
vanilla dirty
Hmean unlink1-processes-2 36089.26 ( 0.00%) 38977.33 ( 8.00%)
Hmean unlink1-processes-5 28555.01 ( 0.00%) 29832.55 ( 4.28%)
Hmean unlink1-processes-8 37323.75 ( 0.00%) 44974.57 ( 20.50%)
Hmean unlink1-processes-12 43571.88 ( 0.00%) 44283.01 ( 1.63%)
Hmean unlink1-processes-21 34431.52 ( 0.00%) 38284.45 ( 11.19%)
Hmean unlink1-processes-30 34813.26 ( 0.00%) 37975.17 ( 9.08%)
Hmean unlink1-processes-48 37048.90 ( 0.00%) 39862.78 ( 7.59%)
Hmean unlink1-processes-79 35630.01 ( 0.00%) 36855.30 ( 3.44%)
Hmean unlink1-processes-110 36115.85 ( 0.00%) 39843.91 ( 10.32%)
Hmean unlink1-processes-141 32546.96 ( 0.00%) 35418.52 ( 8.82%)
Hmean unlink1-processes-172 34674.79 ( 0.00%) 36899.21 ( 6.42%)
Hmean unlink1-processes-203 37303.11 ( 0.00%) 36393.04 ( -2.44%)
Hmean unlink1-processes-224 35712.13 ( 0.00%) 36685.96 ( 2.73%)
== 2. ppc64le ==
Avg runtime set_task_state(): 938 msecs
Avg runtime set_current_state: 940 msecs
vanilla dirty
Hmean unlink1-processes-2 19269.19 ( 0.00%) 30704.50 ( 59.35%)
Hmean unlink1-processes-5 20106.15 ( 0.00%) 21804.15 ( 8.45%)
Hmean unlink1-processes-8 17496.97 ( 0.00%) 17243.28 ( -1.45%)
Hmean unlink1-processes-12 14224.15 ( 0.00%) 17240.21 ( 21.20%)
Hmean unlink1-processes-21 14155.66 ( 0.00%) 15681.23 ( 10.78%)
Hmean unlink1-processes-30 14450.70 ( 0.00%) 15995.83 ( 10.69%)
Hmean unlink1-processes-48 16945.57 ( 0.00%) 16370.42 ( -3.39%)
Hmean unlink1-processes-79 15788.39 ( 0.00%) 14639.27 ( -7.28%)
Hmean unlink1-processes-110 14268.48 ( 0.00%) 14377.40 ( 0.76%)
Hmean unlink1-processes-141 14023.65 ( 0.00%) 16271.69 ( 16.03%)
Hmean unlink1-processes-172 13417.62 ( 0.00%) 16067.55 ( 19.75%)
Hmean unlink1-processes-203 15293.08 ( 0.00%) 15440.40 ( 0.96%)
Hmean unlink1-processes-234 13719.32 ( 0.00%) 16190.74 ( 18.01%)
Hmean unlink1-processes-265 16400.97 ( 0.00%) 16115.22 ( -1.74%)
Hmean unlink1-processes-296 14388.60 ( 0.00%) 16216.13 ( 12.70%)
Hmean unlink1-processes-320 15771.85 ( 0.00%) 15905.96 ( 0.85%)
x86-64 (known to be fast for get_current()/this_cpu_read_stable() caching)
and ppc64 (with paca) show similar improvements in the unlink microbenches.
The small delta for ppc64 (2ms), does not represent the gains on the unlink
runs. In the case of x86, there was a decent amount of variation in the
latency runs, but always within a 20 to 50ms increase), ppc was more constant.
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@stgolabs.net
Cc: mark.rutland@arm.com
Link: http://lkml.kernel.org/r/1483479794-14013-5-git-send-email-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-03 21:43:14 +00:00
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
2015-04-30 09:22:18 +00:00
|
|
|
locked = writer_trylock(sem);
|
|
|
|
if (locked)
|
2013-04-16 10:15:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!locked)
|
2018-06-05 14:53:34 +00:00
|
|
|
atomic_long_add_return(-LDSEM_WAIT_BIAS, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
list_del(&waiter.list);
|
2018-11-01 00:24:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of timeout, wake up every reader who gave the right of way
|
|
|
|
* to writer. Prevent separation readers into two groups:
|
|
|
|
* one that helds semaphore and another that sleeps.
|
|
|
|
* (in case of no contention with a writer)
|
|
|
|
*/
|
|
|
|
if (!locked && list_empty(&sem->write_wait))
|
|
|
|
__ldsem_wake_readers(sem);
|
|
|
|
|
2013-04-16 10:15:50 +00:00
|
|
|
raw_spin_unlock_irq(&sem->wait_lock);
|
|
|
|
|
sched/core: Remove set_task_state()
This is a nasty interface and setting the state of a foreign task must
not be done. As of the following commit:
be628be0956 ("bcache: Make gc wakeup sane, remove set_task_state()")
... everyone in the kernel calls set_task_state() with current, allowing
the helper to be removed.
However, as the comment indicates, it is still around for those archs
where computing current is more expensive than using a pointer, at least
in theory. An important arch that is affected is arm64, however this has
been addressed now [1] and performance is up to par making no difference
with either calls.
Of all the callers, if any, it's the locking bits that would care most
about this -- ie: we end up passing a tsk pointer to a lot of the lock
slowpath, and setting ->state on that. The following numbers are based
on two tests: a custom ad-hoc microbenchmark that just measures
latencies (for ~65 million calls) between get_task_state() vs
get_current_state().
Secondly for a higher overview, an unlink microbenchmark was used,
which pounds on a single file with open, close,unlink combos with
increasing thread counts (up to 4x ncpus). While the workload is quite
unrealistic, it does contend a lot on the inode mutex or now rwsem.
[1] https://lkml.kernel.org/r/1483468021-8237-1-git-send-email-mark.rutland@arm.com
== 1. x86-64 ==
Avg runtime set_task_state(): 601 msecs
Avg runtime set_current_state(): 552 msecs
vanilla dirty
Hmean unlink1-processes-2 36089.26 ( 0.00%) 38977.33 ( 8.00%)
Hmean unlink1-processes-5 28555.01 ( 0.00%) 29832.55 ( 4.28%)
Hmean unlink1-processes-8 37323.75 ( 0.00%) 44974.57 ( 20.50%)
Hmean unlink1-processes-12 43571.88 ( 0.00%) 44283.01 ( 1.63%)
Hmean unlink1-processes-21 34431.52 ( 0.00%) 38284.45 ( 11.19%)
Hmean unlink1-processes-30 34813.26 ( 0.00%) 37975.17 ( 9.08%)
Hmean unlink1-processes-48 37048.90 ( 0.00%) 39862.78 ( 7.59%)
Hmean unlink1-processes-79 35630.01 ( 0.00%) 36855.30 ( 3.44%)
Hmean unlink1-processes-110 36115.85 ( 0.00%) 39843.91 ( 10.32%)
Hmean unlink1-processes-141 32546.96 ( 0.00%) 35418.52 ( 8.82%)
Hmean unlink1-processes-172 34674.79 ( 0.00%) 36899.21 ( 6.42%)
Hmean unlink1-processes-203 37303.11 ( 0.00%) 36393.04 ( -2.44%)
Hmean unlink1-processes-224 35712.13 ( 0.00%) 36685.96 ( 2.73%)
== 2. ppc64le ==
Avg runtime set_task_state(): 938 msecs
Avg runtime set_current_state: 940 msecs
vanilla dirty
Hmean unlink1-processes-2 19269.19 ( 0.00%) 30704.50 ( 59.35%)
Hmean unlink1-processes-5 20106.15 ( 0.00%) 21804.15 ( 8.45%)
Hmean unlink1-processes-8 17496.97 ( 0.00%) 17243.28 ( -1.45%)
Hmean unlink1-processes-12 14224.15 ( 0.00%) 17240.21 ( 21.20%)
Hmean unlink1-processes-21 14155.66 ( 0.00%) 15681.23 ( 10.78%)
Hmean unlink1-processes-30 14450.70 ( 0.00%) 15995.83 ( 10.69%)
Hmean unlink1-processes-48 16945.57 ( 0.00%) 16370.42 ( -3.39%)
Hmean unlink1-processes-79 15788.39 ( 0.00%) 14639.27 ( -7.28%)
Hmean unlink1-processes-110 14268.48 ( 0.00%) 14377.40 ( 0.76%)
Hmean unlink1-processes-141 14023.65 ( 0.00%) 16271.69 ( 16.03%)
Hmean unlink1-processes-172 13417.62 ( 0.00%) 16067.55 ( 19.75%)
Hmean unlink1-processes-203 15293.08 ( 0.00%) 15440.40 ( 0.96%)
Hmean unlink1-processes-234 13719.32 ( 0.00%) 16190.74 ( 18.01%)
Hmean unlink1-processes-265 16400.97 ( 0.00%) 16115.22 ( -1.74%)
Hmean unlink1-processes-296 14388.60 ( 0.00%) 16216.13 ( 12.70%)
Hmean unlink1-processes-320 15771.85 ( 0.00%) 15905.96 ( 0.85%)
x86-64 (known to be fast for get_current()/this_cpu_read_stable() caching)
and ppc64 (with paca) show similar improvements in the unlink microbenches.
The small delta for ppc64 (2ms), does not represent the gains on the unlink
runs. In the case of x86, there was a decent amount of variation in the
latency runs, but always within a 20 to 50ms increase), ppc was more constant.
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@stgolabs.net
Cc: mark.rutland@arm.com
Link: http://lkml.kernel.org/r/1483479794-14013-5-git-send-email-dave@stgolabs.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-01-03 21:43:14 +00:00
|
|
|
__set_current_state(TASK_RUNNING);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
|
|
|
/* lock wait may have timed out */
|
|
|
|
if (!locked)
|
|
|
|
return NULL;
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-27 17:46:45 +00:00
|
|
|
static int __ldsem_down_read_nested(struct ld_semaphore *sem,
|
2013-04-16 10:15:50 +00:00
|
|
|
int subclass, long timeout)
|
|
|
|
{
|
|
|
|
long count;
|
|
|
|
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
count = atomic_long_add_return(LDSEM_READ_BIAS, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
if (count <= 0) {
|
2018-11-01 00:24:50 +00:00
|
|
|
lock_contended(&sem->dep_map, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
if (!down_read_failed(sem, count, timeout)) {
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_release(&sem->dep_map, 1, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 00:24:50 +00:00
|
|
|
lock_acquired(&sem->dep_map, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:46:46 +00:00
|
|
|
static int __ldsem_down_write_nested(struct ld_semaphore *sem,
|
2013-04-16 10:15:50 +00:00
|
|
|
int subclass, long timeout)
|
|
|
|
{
|
|
|
|
long count;
|
|
|
|
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
count = atomic_long_add_return(LDSEM_WRITE_BIAS, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
if ((count & LDSEM_ACTIVE_MASK) != LDSEM_ACTIVE_BIAS) {
|
2018-11-01 00:24:50 +00:00
|
|
|
lock_contended(&sem->dep_map, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
if (!down_write_failed(sem, count, timeout)) {
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_release(&sem->dep_map, 1, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 00:24:50 +00:00
|
|
|
lock_acquired(&sem->dep_map, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lock for reading -- returns 1 if successful, 0 if timed out
|
|
|
|
*/
|
|
|
|
int __sched ldsem_down_read(struct ld_semaphore *sem, long timeout)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
return __ldsem_down_read_nested(sem, 0, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trylock for reading -- returns 1 if successful, 0 if contention
|
|
|
|
*/
|
|
|
|
int ldsem_down_read_trylock(struct ld_semaphore *sem)
|
|
|
|
{
|
2018-06-05 14:53:34 +00:00
|
|
|
long count = atomic_long_read(&sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
|
|
|
while (count >= 0) {
|
2018-06-05 14:53:34 +00:00
|
|
|
if (atomic_long_try_cmpxchg(&sem->count, &count, count + LDSEM_READ_BIAS)) {
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
|
|
|
|
lock_acquired(&sem->dep_map, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lock for writing -- returns 1 if successful, 0 if timed out
|
|
|
|
*/
|
|
|
|
int __sched ldsem_down_write(struct ld_semaphore *sem, long timeout)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
return __ldsem_down_write_nested(sem, 0, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trylock for writing -- returns 1 if successful, 0 if contention
|
|
|
|
*/
|
|
|
|
int ldsem_down_write_trylock(struct ld_semaphore *sem)
|
|
|
|
{
|
2018-06-05 14:53:34 +00:00
|
|
|
long count = atomic_long_read(&sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
|
|
|
while ((count & LDSEM_ACTIVE_MASK) == 0) {
|
2018-06-05 14:53:34 +00:00
|
|
|
if (atomic_long_try_cmpxchg(&sem->count, &count, count + LDSEM_WRITE_BIAS)) {
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
|
|
|
|
lock_acquired(&sem->dep_map, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* release a read lock
|
|
|
|
*/
|
|
|
|
void ldsem_up_read(struct ld_semaphore *sem)
|
|
|
|
{
|
|
|
|
long count;
|
|
|
|
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_release(&sem->dep_map, 1, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
count = atomic_long_add_return(-LDSEM_READ_BIAS, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
if (count < 0 && (count & LDSEM_ACTIVE_MASK) == 0)
|
|
|
|
ldsem_wake(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* release a write lock
|
|
|
|
*/
|
|
|
|
void ldsem_up_write(struct ld_semaphore *sem)
|
|
|
|
{
|
|
|
|
long count;
|
|
|
|
|
2018-11-01 00:24:50 +00:00
|
|
|
rwsem_release(&sem->dep_map, 1, _RET_IP_);
|
2013-04-16 10:15:50 +00:00
|
|
|
|
2018-06-05 14:53:34 +00:00
|
|
|
count = atomic_long_add_return(-LDSEM_WRITE_BIAS, &sem->count);
|
2013-04-16 10:15:50 +00:00
|
|
|
if (count < 0)
|
|
|
|
ldsem_wake(sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
|
|
|
|
int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass, long timeout)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
return __ldsem_down_read_nested(sem, subclass, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
|
|
|
|
long timeout)
|
|
|
|
{
|
|
|
|
might_sleep();
|
|
|
|
return __ldsem_down_write_nested(sem, subclass, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|