Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

/* kernel/rwsem.c: R/W semaphores, public implementation
 *
 * Written by David Howells (dhowells@redhat.com).
 * Derived from asm-i386/semaphore.h
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/export.h>
#include <linux/rwsem.h>
#include <linux/atomic.h>

#include "rwsem.h"

/*
 * lock for reading
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
void __sched _down_read(struct rw_semaphore *sem)
#else
void __sched down_read(struct rw_semaphore *sem)
#endif
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);

	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
	rwsem_set_reader_owned(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_read);
#else
EXPORT_SYMBOL(down_read);
#endif

/*
 * trylock for reading -- returns 1 if successful, 0 if contention
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
int _down_read_trylock(struct rw_semaphore *sem)
#else
int down_read_trylock(struct rw_semaphore *sem)
#endif
{
	int ret = __down_read_trylock(sem);

	if (ret == 1) {
		rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
		rwsem_set_reader_owned(sem);
	}
	return ret;
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_read_trylock);
#else
EXPORT_SYMBOL(down_read_trylock);
#endif

/*
 * lock for writing
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
void __sched _down_write(struct rw_semaphore *sem)
#else
void __sched down_write(struct rw_semaphore *sem)
#endif
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);

	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
	rwsem_set_owner(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_write);
#else
EXPORT_SYMBOL(down_write);
#endif

/*
 * lock for writing
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
int __sched _down_write_killable(struct rw_semaphore *sem)
#else
int __sched down_write_killable(struct rw_semaphore *sem)
#endif
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);

	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
		rwsem_release(&sem->dep_map, 1, _RET_IP_);
		return -EINTR;
	}

	rwsem_set_owner(sem);
	return 0;
}
#ifdef CONFIG_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_write_killable);
#else
EXPORT_SYMBOL(down_write_killable);
#endif

/*
 * trylock for writing -- returns 1 if successful, 0 if contention
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
int _down_write_trylock(struct rw_semaphore *sem)
#else
int down_write_trylock(struct rw_semaphore *sem)
#endif
{
	int ret = __down_write_trylock(sem);

	if (ret == 1) {
		rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
		rwsem_set_owner(sem);
	}

	return ret;
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_write_trylock);
#else
EXPORT_SYMBOL(down_write_trylock);
#endif

/*
 * release a read lock
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
void _up_read(struct rw_semaphore *sem)
#else
void up_read(struct rw_semaphore *sem)
#endif
{
	rwsem_release(&sem->dep_map, 1, _RET_IP_);

	__up_read(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_up_read);
#else
EXPORT_SYMBOL(up_read);
#endif

/*
 * release a write lock
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
void _up_write(struct rw_semaphore *sem)
#else
void up_write(struct rw_semaphore *sem)
#endif
{
	rwsem_release(&sem->dep_map, 1, _RET_IP_);

	rwsem_clear_owner(sem);
	__up_write(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_up_write);
#else
EXPORT_SYMBOL(up_write);
#endif

/*
 * downgrade write lock to read lock
 */
#ifdef CONFIG_ESS_LOCK_ANALYSIS
void _downgrade_write(struct rw_semaphore *sem)
#else
void downgrade_write(struct rw_semaphore *sem)
#endif
{
	/*
	 * lockdep: a downgraded write will live on as a write
	 * dependency.
	 */
	rwsem_set_reader_owned(sem);
	__downgrade_write(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_downgrade_write);
#else
EXPORT_SYMBOL(downgrade_write);
#endif

#ifdef CONFIG_DEBUG_LOCK_ALLOC


#ifdef CONFIG_ESS_LOCK_ANALYSIS
void _down_read_nested(struct rw_semaphore *sem, int subclass)
#else
void down_read_nested(struct rw_semaphore *sem, int subclass)
#endif
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);

	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
	rwsem_set_reader_owned(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_read_nested);
#else
EXPORT_SYMBOL(down_read_nested);
#endif

#ifdef CONFIG_ESS_LOCK_ANALYSIS
void __down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
#else
void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
#endif
{
	might_sleep();
	rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);

	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
	rwsem_set_owner(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(__down_write_nest_lock);
#else
EXPORT_SYMBOL(_down_write_nest_lock);
#endif

#ifdef CONFIG_ESS_LOCK_ANALYSIS
void _down_read_non_owner(struct rw_semaphore *sem)
#else
void down_read_non_owner(struct rw_semaphore *sem)
#endif
{
	might_sleep();

	__down_read(sem);
}
#ifdef CONFIG_ESS_LOCK_ANAYLSIS
EXPORT_SYMBOL(_down_read_non_owner);
#else
EXPORT_SYMBOL(down_read_non_owner);
#endif

#ifdef CONFIG_ESS_LOCK_ANAYLSIS
void _down_write_nested(struct rw_semaphore *sem, int subclass)
#else
void down_write_nested(struct rw_semaphore *sem, int subclass)
#endif
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);

	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
	rwsem_set_owner(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_write_nested);
#else
EXPORT_SYMBOL(down_write_nested);
#endif


#ifdef CONFIG_ESS_LOCK_ANALYSIS
int __sched _down_write_killable_nested(struct rw_semaphore *sem, int subclass)
#else
int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
#endif
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);

	if (LOCK_CONTENDED_RETURN(sem, __down_write_trylock, __down_write_killable)) {
		rwsem_release(&sem->dep_map, 1, _RET_IP_);
		return -EINTR;
	}

	rwsem_set_owner(sem);
	return 0;
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_down_write_killable_nested);
#else
EXPORT_SYMBOL(down_write_killable_nested);
#endif

#ifdef CONFIG_ESS_LOCK_ANALYSIS
void _up_read_non_owner(struct rw_semaphore *sem)
#else
void up_read_non_owner(struct rw_semaphore *sem)
#endif
{
	__up_read(sem);
}
#ifdef CONFIG_ESS_LOCK_ANALYSIS
EXPORT_SYMBOL(_up_read_non_owner);
#else
EXPORT_SYMBOL(up_read_non_owner);
#endif

#endif