rwsem-spinlock.c 7.65 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9
/* rwsem-spinlock.c: R/W semaphores: contention handling functions for
 * generic spinlock implementation
 *
 * Copyright (c) 2001   David Howells (dhowells@redhat.com).
 * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
 * - Derived also from comments by Linus
 */
#include <linux/rwsem.h>
10
#include <linux/sched/signal.h>
11
#include <linux/sched/debug.h>
12
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
13

14 15 16 17 18
enum rwsem_waiter_type {
	RWSEM_WAITING_FOR_WRITE,
	RWSEM_WAITING_FOR_READ
};

Linus Torvalds's avatar
Linus Torvalds committed
19 20 21
struct rwsem_waiter {
	struct list_head list;
	struct task_struct *task;
22
	enum rwsem_waiter_type type;
Linus Torvalds's avatar
Linus Torvalds committed
23 24
};

25 26 27 28 29
int rwsem_is_locked(struct rw_semaphore *sem)
{
	int ret = 1;
	unsigned long flags;

30
	if (raw_spin_trylock_irqsave(&sem->wait_lock, flags)) {
31
		ret = (sem->count != 0);
32
		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
33 34 35 36 37
	}
	return ret;
}
EXPORT_SYMBOL(rwsem_is_locked);

Linus Torvalds's avatar
Linus Torvalds committed
38 39 40
/*
 * initialise the semaphore
 */
41 42
void __init_rwsem(struct rw_semaphore *sem, const char *name,
		  struct lock_class_key *key)
Linus Torvalds's avatar
Linus Torvalds committed
43
{
44 45 46 47 48
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	/*
	 * Make sure we are not reinitializing a held semaphore:
	 */
	debug_check_no_locks_freed((void *)sem, sizeof(*sem));
49
	lockdep_init_map(&sem->dep_map, name, key, 0);
50
#endif
51
	sem->count = 0;
52
	raw_spin_lock_init(&sem->wait_lock);
Linus Torvalds's avatar
Linus Torvalds committed
53 54
	INIT_LIST_HEAD(&sem->wait_list);
}
55
EXPORT_SYMBOL(__init_rwsem);
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

/*
 * handle the lock release when processes blocked on it that can now run
 * - if we come here, then:
 *   - the 'active count' _reached_ zero
 *   - the 'waiting count' is non-zero
 * - the spinlock must be held by the caller
 * - woken process blocks are discarded from the list after having task zeroed
 * - writers are only woken if wakewrite is non-zero
 */
static inline struct rw_semaphore *
__rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
{
	struct rwsem_waiter *waiter;
	struct task_struct *tsk;
	int woken;

	waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);

75
	if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
76 77 78 79
		if (wakewrite)
			/* Wake up a writer. Note that we do not grant it the
			 * lock - it will have to acquire it when it runs. */
			wake_up_process(waiter->task);
Linus Torvalds's avatar
Linus Torvalds committed
80 81 82 83 84
		goto out;
	}

	/* grant an infinite number of read locks to the front of the queue */
	woken = 0;
85
	do {
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89
		struct list_head *next = waiter->list.next;

		list_del(&waiter->list);
		tsk = waiter->task;
90 91 92 93 94 95 96
		/*
		 * Make sure we do not wakeup the next reader before
		 * setting the nil condition to grant the next reader;
		 * otherwise we could miss the wakeup on the other
		 * side and end up sleeping again. See the pairing
		 * in rwsem_down_read_failed().
		 */
97
		smp_mb();
Linus Torvalds's avatar
Linus Torvalds committed
98 99 100 101
		waiter->task = NULL;
		wake_up_process(tsk);
		put_task_struct(tsk);
		woken++;
102
		if (next == &sem->wait_list)
Linus Torvalds's avatar
Linus Torvalds committed
103 104
			break;
		waiter = list_entry(next, struct rwsem_waiter, list);
105
	} while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds's avatar
Linus Torvalds committed
106

107
	sem->count += woken;
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121

 out:
	return sem;
}

/*
 * wake a single writer
 */
static inline struct rw_semaphore *
__rwsem_wake_one_writer(struct rw_semaphore *sem)
{
	struct rwsem_waiter *waiter;

	waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
122
	wake_up_process(waiter->task);
Linus Torvalds's avatar
Linus Torvalds committed
123 124 125 126 127 128 129

	return sem;
}

/*
 * get a read lock on the semaphore
 */
130
int __sched __down_read_common(struct rw_semaphore *sem, int state)
Linus Torvalds's avatar
Linus Torvalds committed
131 132
{
	struct rwsem_waiter waiter;
133
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
134

135
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
136

137
	if (sem->count >= 0 && list_empty(&sem->wait_list)) {
Linus Torvalds's avatar
Linus Torvalds committed
138
		/* granted */
139
		sem->count++;
140
		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143 144
		goto out;
	}

	/* set up my own style of waitqueue */
145
	waiter.task = current;
146
	waiter.type = RWSEM_WAITING_FOR_READ;
147
	get_task_struct(current);
Linus Torvalds's avatar
Linus Torvalds committed
148 149 150 151 152 153 154

	list_add_tail(&waiter.list, &sem->wait_list);

	/* wait to be given the lock */
	for (;;) {
		if (!waiter.task)
			break;
155 156 157 158
		if (signal_pending_state(state, current))
			goto out_nolock;
		set_current_state(state);
		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
159
		schedule();
160
		raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
161 162
	}

163
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
164
 out:
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	return 0;

out_nolock:
	/*
	 * We didn't take the lock, so that there is a writer, which
	 * is owner or the first waiter of the sem. If it's a waiter,
	 * it will be woken by current owner. Not need to wake anybody.
	 */
	list_del(&waiter.list);
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
	return -EINTR;
}

void __sched __down_read(struct rw_semaphore *sem)
{
	__down_read_common(sem, TASK_UNINTERRUPTIBLE);
}

int __sched __down_read_killable(struct rw_semaphore *sem)
{
	return __down_read_common(sem, TASK_KILLABLE);
Linus Torvalds's avatar
Linus Torvalds committed
186 187 188 189 190
}

/*
 * trylock for reading -- returns 1 if successful, 0 if contention
 */
191
int __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194 195 196
{
	unsigned long flags;
	int ret = 0;


197
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
198

199
	if (sem->count >= 0 && list_empty(&sem->wait_list)) {
Linus Torvalds's avatar
Linus Torvalds committed
200
		/* granted */
201
		sem->count++;
Linus Torvalds's avatar
Linus Torvalds committed
202 203 204
		ret = 1;
	}

205
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209 210 211 212

	return ret;
}

/*
 * get a write lock on the semaphore
 */
213
int __sched __down_write_common(struct rw_semaphore *sem, int state)
Linus Torvalds's avatar
Linus Torvalds committed
214 215
{
	struct rwsem_waiter waiter;
216
	unsigned long flags;
217
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
218

219
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
220 221

	/* set up my own style of waitqueue */
222
	waiter.task = current;
223
	waiter.type = RWSEM_WAITING_FOR_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
224 225
	list_add_tail(&waiter.list, &sem->wait_list);

226
	/* wait for someone to release the lock */
Linus Torvalds's avatar
Linus Torvalds committed
227
	for (;;) {
228 229 230 231 232 233
		/*
		 * That is the key to support write lock stealing: allows the
		 * task already on CPU to get the lock soon rather than put
		 * itself into sleep and waiting for system woke it or someone
		 * else in the head of the wait list up.
		 */
234
		if (sem->count == 0)
Linus Torvalds's avatar
Linus Torvalds committed
235
			break;
236 237 238
		if (signal_pending_state(state, current))
			goto out_nolock;

239
		set_current_state(state);
240 241 242
		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
		schedule();
		raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
243
	}
244
	/* got the lock */
245
	sem->count = -1;
246
	list_del(&waiter.list);
Linus Torvalds's avatar
Linus Torvalds committed
247

248
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
249 250

	return ret;
251 252 253

out_nolock:
	list_del(&waiter.list);
254 255
	if (!list_empty(&sem->wait_list) && sem->count >= 0)
		__rwsem_do_wake(sem, 0);
256 257 258
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);

	return -EINTR;
259 260 261 262 263 264 265 266 267 268
}

void __sched __down_write(struct rw_semaphore *sem)
{
	__down_write_common(sem, TASK_UNINTERRUPTIBLE);
}

int __sched __down_write_killable(struct rw_semaphore *sem)
{
	return __down_write_common(sem, TASK_KILLABLE);
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273
}

/*
 * trylock for writing -- returns 1 if successful, 0 if contention
 */
274
int __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
275 276 277 278
{
	unsigned long flags;
	int ret = 0;

279
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
280

281
	if (sem->count == 0) {
282
		/* got the lock */
283
		sem->count = -1;
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286
		ret = 1;
	}

287
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290 291 292 293 294

	return ret;
}

/*
 * release a read lock on the semaphore
 */
295
void __up_read(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
296 297 298
{
	unsigned long flags;

299
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
300

301
	if (--sem->count == 0 && !list_empty(&sem->wait_list))
Linus Torvalds's avatar
Linus Torvalds committed
302 303
		sem = __rwsem_wake_one_writer(sem);

304
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307 308 309
}

/*
 * release a write lock on the semaphore
 */
310
void __up_write(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313
{
	unsigned long flags;

314
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
315

316
	sem->count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
317 318 319
	if (!list_empty(&sem->wait_list))
		sem = __rwsem_do_wake(sem, 1);

320
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324 325 326
}

/*
 * downgrade a write lock into a read lock
 * - just wake up any readers at the front of the queue
 */
327
void __downgrade_write(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
328 329 330
{
	unsigned long flags;

331
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
332

333
	sem->count = 1;
Linus Torvalds's avatar
Linus Torvalds committed
334 335 336
	if (!list_empty(&sem->wait_list))
		sem = __rwsem_do_wake(sem, 0);

337
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
338 339
}