18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Ldisc rw semaphore
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * The ldisc semaphore is semantically a rw_semaphore but which enforces
68c2ecf20Sopenharmony_ci * an alternate policy, namely:
78c2ecf20Sopenharmony_ci *   1) Supports lock wait timeouts
88c2ecf20Sopenharmony_ci *   2) Write waiter has priority
98c2ecf20Sopenharmony_ci *   3) Downgrading is not supported
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Implementation notes:
128c2ecf20Sopenharmony_ci *   1) Upper half of semaphore count is a wait count (differs from rwsem
138c2ecf20Sopenharmony_ci *	in that rwsem normalizes the upper half to the wait bias)
148c2ecf20Sopenharmony_ci *   2) Lacks overflow checking
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * The generic counting was copied and modified from include/asm-generic/rwsem.h
178c2ecf20Sopenharmony_ci * by Paul Mackerras <paulus@samba.org>.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * The scheduling policy was copied and modified from lib/rwsem.c
208c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com).
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * This implementation incorporates the write lock stealing work of
238c2ecf20Sopenharmony_ci * Michel Lespinasse <walken@google.com>.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Copyright (C) 2013 Peter Hurley <peter@hurleysoftware.com>
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/list.h>
298c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
308c2ecf20Sopenharmony_ci#include <linux/atomic.h>
318c2ecf20Sopenharmony_ci#include <linux/tty.h>
328c2ecf20Sopenharmony_ci#include <linux/sched.h>
338c2ecf20Sopenharmony_ci#include <linux/sched/debug.h>
348c2ecf20Sopenharmony_ci#include <linux/sched/task.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64
388c2ecf20Sopenharmony_ci# define LDSEM_ACTIVE_MASK	0xffffffffL
398c2ecf20Sopenharmony_ci#else
408c2ecf20Sopenharmony_ci# define LDSEM_ACTIVE_MASK	0x0000ffffL
418c2ecf20Sopenharmony_ci#endif
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define LDSEM_UNLOCKED		0L
448c2ecf20Sopenharmony_ci#define LDSEM_ACTIVE_BIAS	1L
458c2ecf20Sopenharmony_ci#define LDSEM_WAIT_BIAS		(-LDSEM_ACTIVE_MASK-1)
468c2ecf20Sopenharmony_ci#define LDSEM_READ_BIAS		LDSEM_ACTIVE_BIAS
478c2ecf20Sopenharmony_ci#define LDSEM_WRITE_BIAS	(LDSEM_WAIT_BIAS + LDSEM_ACTIVE_BIAS)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct ldsem_waiter {
508c2ecf20Sopenharmony_ci	struct list_head list;
518c2ecf20Sopenharmony_ci	struct task_struct *task;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * Initialize an ldsem:
568c2ecf20Sopenharmony_ci */
578c2ecf20Sopenharmony_civoid __init_ldsem(struct ld_semaphore *sem, const char *name,
588c2ecf20Sopenharmony_ci		  struct lock_class_key *key)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_LOCK_ALLOC
618c2ecf20Sopenharmony_ci	/*
628c2ecf20Sopenharmony_ci	 * Make sure we are not reinitializing a held semaphore:
638c2ecf20Sopenharmony_ci	 */
648c2ecf20Sopenharmony_ci	debug_check_no_locks_freed((void *)sem, sizeof(*sem));
658c2ecf20Sopenharmony_ci	lockdep_init_map(&sem->dep_map, name, key, 0);
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci	atomic_long_set(&sem->count, LDSEM_UNLOCKED);
688c2ecf20Sopenharmony_ci	sem->wait_readers = 0;
698c2ecf20Sopenharmony_ci	raw_spin_lock_init(&sem->wait_lock);
708c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&sem->read_wait);
718c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&sem->write_wait);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void __ldsem_wake_readers(struct ld_semaphore *sem)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct ldsem_waiter *waiter, *next;
778c2ecf20Sopenharmony_ci	struct task_struct *tsk;
788c2ecf20Sopenharmony_ci	long adjust, count;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * Try to grant read locks to all readers on the read wait list.
828c2ecf20Sopenharmony_ci	 * Note the 'active part' of the count is incremented by
838c2ecf20Sopenharmony_ci	 * the number of readers before waking any processes up.
848c2ecf20Sopenharmony_ci	 */
858c2ecf20Sopenharmony_ci	adjust = sem->wait_readers * (LDSEM_ACTIVE_BIAS - LDSEM_WAIT_BIAS);
868c2ecf20Sopenharmony_ci	count = atomic_long_add_return(adjust, &sem->count);
878c2ecf20Sopenharmony_ci	do {
888c2ecf20Sopenharmony_ci		if (count > 0)
898c2ecf20Sopenharmony_ci			break;
908c2ecf20Sopenharmony_ci		if (atomic_long_try_cmpxchg(&sem->count, &count, count - adjust))
918c2ecf20Sopenharmony_ci			return;
928c2ecf20Sopenharmony_ci	} while (1);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	list_for_each_entry_safe(waiter, next, &sem->read_wait, list) {
958c2ecf20Sopenharmony_ci		tsk = waiter->task;
968c2ecf20Sopenharmony_ci		smp_store_release(&waiter->task, NULL);
978c2ecf20Sopenharmony_ci		wake_up_process(tsk);
988c2ecf20Sopenharmony_ci		put_task_struct(tsk);
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&sem->read_wait);
1018c2ecf20Sopenharmony_ci	sem->wait_readers = 0;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic inline int writer_trylock(struct ld_semaphore *sem)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	/*
1078c2ecf20Sopenharmony_ci	 * Only wake this writer if the active part of the count can be
1088c2ecf20Sopenharmony_ci	 * transitioned from 0 -> 1
1098c2ecf20Sopenharmony_ci	 */
1108c2ecf20Sopenharmony_ci	long count = atomic_long_add_return(LDSEM_ACTIVE_BIAS, &sem->count);
1118c2ecf20Sopenharmony_ci	do {
1128c2ecf20Sopenharmony_ci		if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS)
1138c2ecf20Sopenharmony_ci			return 1;
1148c2ecf20Sopenharmony_ci		if (atomic_long_try_cmpxchg(&sem->count, &count, count - LDSEM_ACTIVE_BIAS))
1158c2ecf20Sopenharmony_ci			return 0;
1168c2ecf20Sopenharmony_ci	} while (1);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic void __ldsem_wake_writer(struct ld_semaphore *sem)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct ldsem_waiter *waiter;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	waiter = list_entry(sem->write_wait.next, struct ldsem_waiter, list);
1248c2ecf20Sopenharmony_ci	wake_up_process(waiter->task);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * handle the lock release when processes blocked on it that can now run
1298c2ecf20Sopenharmony_ci * - if we come here from up_xxxx(), then:
1308c2ecf20Sopenharmony_ci *   - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
1318c2ecf20Sopenharmony_ci *   - the 'waiting part' of count (&0xffff0000) is -ve (and will still be so)
1328c2ecf20Sopenharmony_ci * - the spinlock must be held by the caller
1338c2ecf20Sopenharmony_ci * - woken process blocks are discarded from the list after having task zeroed
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cistatic void __ldsem_wake(struct ld_semaphore *sem)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	if (!list_empty(&sem->write_wait))
1388c2ecf20Sopenharmony_ci		__ldsem_wake_writer(sem);
1398c2ecf20Sopenharmony_ci	else if (!list_empty(&sem->read_wait))
1408c2ecf20Sopenharmony_ci		__ldsem_wake_readers(sem);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic void ldsem_wake(struct ld_semaphore *sem)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	unsigned long flags;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&sem->wait_lock, flags);
1488c2ecf20Sopenharmony_ci	__ldsem_wake(sem);
1498c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * wait for the read lock to be granted
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_cistatic struct ld_semaphore __sched *
1568c2ecf20Sopenharmony_cidown_read_failed(struct ld_semaphore *sem, long count, long timeout)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct ldsem_waiter waiter;
1598c2ecf20Sopenharmony_ci	long adjust = -LDSEM_ACTIVE_BIAS + LDSEM_WAIT_BIAS;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* set up my own style of waitqueue */
1628c2ecf20Sopenharmony_ci	raw_spin_lock_irq(&sem->wait_lock);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/*
1658c2ecf20Sopenharmony_ci	 * Try to reverse the lock attempt but if the count has changed
1668c2ecf20Sopenharmony_ci	 * so that reversing fails, check if there are are no waiters,
1678c2ecf20Sopenharmony_ci	 * and early-out if not
1688c2ecf20Sopenharmony_ci	 */
1698c2ecf20Sopenharmony_ci	do {
1708c2ecf20Sopenharmony_ci		if (atomic_long_try_cmpxchg(&sem->count, &count, count + adjust)) {
1718c2ecf20Sopenharmony_ci			count += adjust;
1728c2ecf20Sopenharmony_ci			break;
1738c2ecf20Sopenharmony_ci		}
1748c2ecf20Sopenharmony_ci		if (count > 0) {
1758c2ecf20Sopenharmony_ci			raw_spin_unlock_irq(&sem->wait_lock);
1768c2ecf20Sopenharmony_ci			return sem;
1778c2ecf20Sopenharmony_ci		}
1788c2ecf20Sopenharmony_ci	} while (1);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	list_add_tail(&waiter.list, &sem->read_wait);
1818c2ecf20Sopenharmony_ci	sem->wait_readers++;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	waiter.task = current;
1848c2ecf20Sopenharmony_ci	get_task_struct(current);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* if there are no active locks, wake the new lock owner(s) */
1878c2ecf20Sopenharmony_ci	if ((count & LDSEM_ACTIVE_MASK) == 0)
1888c2ecf20Sopenharmony_ci		__ldsem_wake(sem);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	raw_spin_unlock_irq(&sem->wait_lock);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* wait to be given the lock */
1938c2ecf20Sopenharmony_ci	for (;;) {
1948c2ecf20Sopenharmony_ci		set_current_state(TASK_UNINTERRUPTIBLE);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		if (!smp_load_acquire(&waiter.task))
1978c2ecf20Sopenharmony_ci			break;
1988c2ecf20Sopenharmony_ci		if (!timeout)
1998c2ecf20Sopenharmony_ci			break;
2008c2ecf20Sopenharmony_ci		timeout = schedule_timeout(timeout);
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	__set_current_state(TASK_RUNNING);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (!timeout) {
2068c2ecf20Sopenharmony_ci		/*
2078c2ecf20Sopenharmony_ci		 * Lock timed out but check if this task was just
2088c2ecf20Sopenharmony_ci		 * granted lock ownership - if so, pretend there
2098c2ecf20Sopenharmony_ci		 * was no timeout; otherwise, cleanup lock wait.
2108c2ecf20Sopenharmony_ci		 */
2118c2ecf20Sopenharmony_ci		raw_spin_lock_irq(&sem->wait_lock);
2128c2ecf20Sopenharmony_ci		if (waiter.task) {
2138c2ecf20Sopenharmony_ci			atomic_long_add_return(-LDSEM_WAIT_BIAS, &sem->count);
2148c2ecf20Sopenharmony_ci			sem->wait_readers--;
2158c2ecf20Sopenharmony_ci			list_del(&waiter.list);
2168c2ecf20Sopenharmony_ci			raw_spin_unlock_irq(&sem->wait_lock);
2178c2ecf20Sopenharmony_ci			put_task_struct(waiter.task);
2188c2ecf20Sopenharmony_ci			return NULL;
2198c2ecf20Sopenharmony_ci		}
2208c2ecf20Sopenharmony_ci		raw_spin_unlock_irq(&sem->wait_lock);
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return sem;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci * wait for the write lock to be granted
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_cistatic struct ld_semaphore __sched *
2308c2ecf20Sopenharmony_cidown_write_failed(struct ld_semaphore *sem, long count, long timeout)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct ldsem_waiter waiter;
2338c2ecf20Sopenharmony_ci	long adjust = -LDSEM_ACTIVE_BIAS;
2348c2ecf20Sopenharmony_ci	int locked = 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* set up my own style of waitqueue */
2378c2ecf20Sopenharmony_ci	raw_spin_lock_irq(&sem->wait_lock);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/*
2408c2ecf20Sopenharmony_ci	 * Try to reverse the lock attempt but if the count has changed
2418c2ecf20Sopenharmony_ci	 * so that reversing fails, check if the lock is now owned,
2428c2ecf20Sopenharmony_ci	 * and early-out if so.
2438c2ecf20Sopenharmony_ci	 */
2448c2ecf20Sopenharmony_ci	do {
2458c2ecf20Sopenharmony_ci		if (atomic_long_try_cmpxchg(&sem->count, &count, count + adjust))
2468c2ecf20Sopenharmony_ci			break;
2478c2ecf20Sopenharmony_ci		if ((count & LDSEM_ACTIVE_MASK) == LDSEM_ACTIVE_BIAS) {
2488c2ecf20Sopenharmony_ci			raw_spin_unlock_irq(&sem->wait_lock);
2498c2ecf20Sopenharmony_ci			return sem;
2508c2ecf20Sopenharmony_ci		}
2518c2ecf20Sopenharmony_ci	} while (1);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	list_add_tail(&waiter.list, &sem->write_wait);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	waiter.task = current;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	set_current_state(TASK_UNINTERRUPTIBLE);
2588c2ecf20Sopenharmony_ci	for (;;) {
2598c2ecf20Sopenharmony_ci		if (!timeout)
2608c2ecf20Sopenharmony_ci			break;
2618c2ecf20Sopenharmony_ci		raw_spin_unlock_irq(&sem->wait_lock);
2628c2ecf20Sopenharmony_ci		timeout = schedule_timeout(timeout);
2638c2ecf20Sopenharmony_ci		raw_spin_lock_irq(&sem->wait_lock);
2648c2ecf20Sopenharmony_ci		set_current_state(TASK_UNINTERRUPTIBLE);
2658c2ecf20Sopenharmony_ci		locked = writer_trylock(sem);
2668c2ecf20Sopenharmony_ci		if (locked)
2678c2ecf20Sopenharmony_ci			break;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (!locked)
2718c2ecf20Sopenharmony_ci		atomic_long_add_return(-LDSEM_WAIT_BIAS, &sem->count);
2728c2ecf20Sopenharmony_ci	list_del(&waiter.list);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/*
2758c2ecf20Sopenharmony_ci	 * In case of timeout, wake up every reader who gave the right of way
2768c2ecf20Sopenharmony_ci	 * to writer. Prevent separation readers into two groups:
2778c2ecf20Sopenharmony_ci	 * one that helds semaphore and another that sleeps.
2788c2ecf20Sopenharmony_ci	 * (in case of no contention with a writer)
2798c2ecf20Sopenharmony_ci	 */
2808c2ecf20Sopenharmony_ci	if (!locked && list_empty(&sem->write_wait))
2818c2ecf20Sopenharmony_ci		__ldsem_wake_readers(sem);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	raw_spin_unlock_irq(&sem->wait_lock);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	__set_current_state(TASK_RUNNING);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* lock wait may have timed out */
2888c2ecf20Sopenharmony_ci	if (!locked)
2898c2ecf20Sopenharmony_ci		return NULL;
2908c2ecf20Sopenharmony_ci	return sem;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic int __ldsem_down_read_nested(struct ld_semaphore *sem,
2968c2ecf20Sopenharmony_ci					   int subclass, long timeout)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	long count;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	count = atomic_long_add_return(LDSEM_READ_BIAS, &sem->count);
3038c2ecf20Sopenharmony_ci	if (count <= 0) {
3048c2ecf20Sopenharmony_ci		lock_contended(&sem->dep_map, _RET_IP_);
3058c2ecf20Sopenharmony_ci		if (!down_read_failed(sem, count, timeout)) {
3068c2ecf20Sopenharmony_ci			rwsem_release(&sem->dep_map, _RET_IP_);
3078c2ecf20Sopenharmony_ci			return 0;
3088c2ecf20Sopenharmony_ci		}
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci	lock_acquired(&sem->dep_map, _RET_IP_);
3118c2ecf20Sopenharmony_ci	return 1;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int __ldsem_down_write_nested(struct ld_semaphore *sem,
3158c2ecf20Sopenharmony_ci					    int subclass, long timeout)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	long count;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	count = atomic_long_add_return(LDSEM_WRITE_BIAS, &sem->count);
3228c2ecf20Sopenharmony_ci	if ((count & LDSEM_ACTIVE_MASK) != LDSEM_ACTIVE_BIAS) {
3238c2ecf20Sopenharmony_ci		lock_contended(&sem->dep_map, _RET_IP_);
3248c2ecf20Sopenharmony_ci		if (!down_write_failed(sem, count, timeout)) {
3258c2ecf20Sopenharmony_ci			rwsem_release(&sem->dep_map, _RET_IP_);
3268c2ecf20Sopenharmony_ci			return 0;
3278c2ecf20Sopenharmony_ci		}
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci	lock_acquired(&sem->dep_map, _RET_IP_);
3308c2ecf20Sopenharmony_ci	return 1;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci/*
3358c2ecf20Sopenharmony_ci * lock for reading -- returns 1 if successful, 0 if timed out
3368c2ecf20Sopenharmony_ci */
3378c2ecf20Sopenharmony_ciint __sched ldsem_down_read(struct ld_semaphore *sem, long timeout)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	might_sleep();
3408c2ecf20Sopenharmony_ci	return __ldsem_down_read_nested(sem, 0, timeout);
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/*
3448c2ecf20Sopenharmony_ci * trylock for reading -- returns 1 if successful, 0 if contention
3458c2ecf20Sopenharmony_ci */
3468c2ecf20Sopenharmony_ciint ldsem_down_read_trylock(struct ld_semaphore *sem)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	long count = atomic_long_read(&sem->count);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	while (count >= 0) {
3518c2ecf20Sopenharmony_ci		if (atomic_long_try_cmpxchg(&sem->count, &count, count + LDSEM_READ_BIAS)) {
3528c2ecf20Sopenharmony_ci			rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
3538c2ecf20Sopenharmony_ci			lock_acquired(&sem->dep_map, _RET_IP_);
3548c2ecf20Sopenharmony_ci			return 1;
3558c2ecf20Sopenharmony_ci		}
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci	return 0;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci/*
3618c2ecf20Sopenharmony_ci * lock for writing -- returns 1 if successful, 0 if timed out
3628c2ecf20Sopenharmony_ci */
3638c2ecf20Sopenharmony_ciint __sched ldsem_down_write(struct ld_semaphore *sem, long timeout)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	might_sleep();
3668c2ecf20Sopenharmony_ci	return __ldsem_down_write_nested(sem, 0, timeout);
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/*
3708c2ecf20Sopenharmony_ci * trylock for writing -- returns 1 if successful, 0 if contention
3718c2ecf20Sopenharmony_ci */
3728c2ecf20Sopenharmony_ciint ldsem_down_write_trylock(struct ld_semaphore *sem)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	long count = atomic_long_read(&sem->count);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	while ((count & LDSEM_ACTIVE_MASK) == 0) {
3778c2ecf20Sopenharmony_ci		if (atomic_long_try_cmpxchg(&sem->count, &count, count + LDSEM_WRITE_BIAS)) {
3788c2ecf20Sopenharmony_ci			rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
3798c2ecf20Sopenharmony_ci			lock_acquired(&sem->dep_map, _RET_IP_);
3808c2ecf20Sopenharmony_ci			return 1;
3818c2ecf20Sopenharmony_ci		}
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/*
3878c2ecf20Sopenharmony_ci * release a read lock
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_civoid ldsem_up_read(struct ld_semaphore *sem)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	long count;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	rwsem_release(&sem->dep_map, _RET_IP_);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	count = atomic_long_add_return(-LDSEM_READ_BIAS, &sem->count);
3968c2ecf20Sopenharmony_ci	if (count < 0 && (count & LDSEM_ACTIVE_MASK) == 0)
3978c2ecf20Sopenharmony_ci		ldsem_wake(sem);
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/*
4018c2ecf20Sopenharmony_ci * release a write lock
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_civoid ldsem_up_write(struct ld_semaphore *sem)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	long count;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	rwsem_release(&sem->dep_map, _RET_IP_);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	count = atomic_long_add_return(-LDSEM_WRITE_BIAS, &sem->count);
4108c2ecf20Sopenharmony_ci	if (count < 0)
4118c2ecf20Sopenharmony_ci		ldsem_wake(sem);
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_LOCK_ALLOC
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciint ldsem_down_read_nested(struct ld_semaphore *sem, int subclass, long timeout)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	might_sleep();
4208c2ecf20Sopenharmony_ci	return __ldsem_down_read_nested(sem, subclass, timeout);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ciint ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
4248c2ecf20Sopenharmony_ci			    long timeout)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	might_sleep();
4278c2ecf20Sopenharmony_ci	return __ldsem_down_write_nested(sem, subclass, timeout);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci#endif
431