rwsem.c 4.39 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9
/* 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>
10
#include <linux/sched.h>
11
#include <linux/sched/debug.h>
12
#include <linux/export.h>
13
#include <linux/rwsem.h>
Arun Sharma's avatar
Arun Sharma committed
14
#include <linux/atomic.h>
15

16
#include "rwsem.h"
17

18 19 20
/*
 * lock for reading
 */
21
void __sched down_read(struct rw_semaphore *sem)
22 23 24 25
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);

26
	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
27
	rwsem_set_reader_owned(sem);
28 29 30 31
}

EXPORT_SYMBOL(down_read);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
int __sched down_read_killable(struct rw_semaphore *sem)
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_);

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

	rwsem_set_reader_owned(sem);
	return 0;
}

EXPORT_SYMBOL(down_read_killable);

48 49 50 51 52 53 54
/*
 * trylock for reading -- returns 1 if successful, 0 if contention
 */
int down_read_trylock(struct rw_semaphore *sem)
{
	int ret = __down_read_trylock(sem);

55
	if (ret == 1) {
56
		rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
57 58
		rwsem_set_reader_owned(sem);
	}
59 60 61 62 63 64 65 66
	return ret;
}

EXPORT_SYMBOL(down_read_trylock);

/*
 * lock for writing
 */
67
void __sched down_write(struct rw_semaphore *sem)
68 69 70 71
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_);

72
	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
73
	rwsem_set_owner(sem);
74 75 76 77
}

EXPORT_SYMBOL(down_write);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * lock for writing
 */
int __sched down_write_killable(struct rw_semaphore *sem)
{
	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;
}

EXPORT_SYMBOL(down_write_killable);

97 98 99 100 101 102 103
/*
 * trylock for writing -- returns 1 if successful, 0 if contention
 */
int down_write_trylock(struct rw_semaphore *sem)
{
	int ret = __down_write_trylock(sem);

104
	if (ret == 1) {
105
		rwsem_acquire(&sem->dep_map, 0, 1, _RET_IP_);
106 107 108
		rwsem_set_owner(sem);
	}

109 110 111 112 113 114 115 116 117 118 119
	return ret;
}

EXPORT_SYMBOL(down_write_trylock);

/*
 * release a read lock
 */
void up_read(struct rw_semaphore *sem)
{
	rwsem_release(&sem->dep_map, 1, _RET_IP_);
120
	DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED);
121 122 123 124 125 126 127 128 129 130 131 132

	__up_read(sem);
}

EXPORT_SYMBOL(up_read);

/*
 * release a write lock
 */
void up_write(struct rw_semaphore *sem)
{
	rwsem_release(&sem->dep_map, 1, _RET_IP_);
133
	DEBUG_RWSEMS_WARN_ON(sem->owner != current);
134

135
	rwsem_clear_owner(sem);
136 137 138 139 140 141 142 143 144 145
	__up_write(sem);
}

EXPORT_SYMBOL(up_write);

/*
 * downgrade write lock to read lock
 */
void downgrade_write(struct rw_semaphore *sem)
{
146
	lock_downgrade(&sem->dep_map, _RET_IP_);
147
	DEBUG_RWSEMS_WARN_ON(sem->owner != current);
148

149
	rwsem_set_reader_owned(sem);
150 151 152 153
	__downgrade_write(sem);
}

EXPORT_SYMBOL(downgrade_write);
154 155 156 157 158 159 160 161

#ifdef CONFIG_DEBUG_LOCK_ALLOC

void down_read_nested(struct rw_semaphore *sem, int subclass)
{
	might_sleep();
	rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);

162
	LOCK_CONTENDED(sem, __down_read_trylock, __down_read);
163
	rwsem_set_reader_owned(sem);
164 165 166 167
}

EXPORT_SYMBOL(down_read_nested);

168 169 170 171 172 173
void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)
{
	might_sleep();
	rwsem_acquire_nest(&sem->dep_map, 0, 0, nest, _RET_IP_);

	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
174
	rwsem_set_owner(sem);
175 176 177 178
}

EXPORT_SYMBOL(_down_write_nest_lock);

179 180 181 182 183 184 185 186 187
void down_read_non_owner(struct rw_semaphore *sem)
{
	might_sleep();

	__down_read(sem);
}

EXPORT_SYMBOL(down_read_non_owner);

188 189 190 191 192
void down_write_nested(struct rw_semaphore *sem, int subclass)
{
	might_sleep();
	rwsem_acquire(&sem->dep_map, subclass, 0, _RET_IP_);

193
	LOCK_CONTENDED(sem, __down_write_trylock, __down_write);
194
	rwsem_set_owner(sem);
195 196 197 198
}

EXPORT_SYMBOL(down_write_nested);

Al Viro's avatar
Al Viro committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)
{
	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;
}

EXPORT_SYMBOL(down_write_killable_nested);

215 216
void up_read_non_owner(struct rw_semaphore *sem)
{
217
	DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_READER_OWNED);
218 219 220 221 222
	__up_read(sem);
}

EXPORT_SYMBOL(up_read_non_owner);

223
#endif