rwsem-spinlock.c 6.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 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>
#include <linux/sched.h>
10
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
11

12 13 14 15 16
enum rwsem_waiter_type {
	RWSEM_WAITING_FOR_WRITE,
	RWSEM_WAITING_FOR_READ
};

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

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

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

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

/*
 * 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);

73
	if (waiter->type == RWSEM_WAITING_FOR_WRITE) {
74 75 76 77
		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
78 79 80 81 82
		goto out;
	}

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

		list_del(&waiter->list);
		tsk = waiter->task;
88
		smp_mb();
Linus Torvalds's avatar
Linus Torvalds committed
89 90 91 92
		waiter->task = NULL;
		wake_up_process(tsk);
		put_task_struct(tsk);
		woken++;
93
		if (next == &sem->wait_list)
Linus Torvalds's avatar
Linus Torvalds committed
94 95
			break;
		waiter = list_entry(next, struct rwsem_waiter, list);
96
	} while (waiter->type != RWSEM_WAITING_FOR_WRITE);
Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	sem->activity += woken;

 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);
113
	wake_up_process(waiter->task);
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119 120

	return sem;
}

/*
 * get a read lock on the semaphore
 */
121
void __sched __down_read(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
122 123 124
{
	struct rwsem_waiter waiter;
	struct task_struct *tsk;
125
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
126

127
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131

	if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
		/* granted */
		sem->activity++;
132
		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
133 134 135 136 137 138 139 140
		goto out;
	}

	tsk = current;
	set_task_state(tsk, TASK_UNINTERRUPTIBLE);

	/* set up my own style of waitqueue */
	waiter.task = tsk;
141
	waiter.type = RWSEM_WAITING_FOR_READ;
Linus Torvalds's avatar
Linus Torvalds committed
142 143 144 145 146
	get_task_struct(tsk);

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

	/* we don't need to touch the semaphore struct anymore */
147
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
148 149 150 151 152 153 154 155 156 157 158

	/* wait to be given the lock */
	for (;;) {
		if (!waiter.task)
			break;
		schedule();
		set_task_state(tsk, TASK_UNINTERRUPTIBLE);
	}

	tsk->state = TASK_RUNNING;
 out:
159
	;
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164
}

/*
 * trylock for reading -- returns 1 if successful, 0 if contention
 */
165
int __down_read_trylock(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
166 167 168 169 170
{
	unsigned long flags;
	int ret = 0;


171
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178

	if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
		/* granted */
		sem->activity++;
		ret = 1;
	}

179
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184 185 186

	return ret;
}

/*
 * get a write lock on the semaphore
 */
187
void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
Linus Torvalds's avatar
Linus Torvalds committed
188 189 190
{
	struct rwsem_waiter waiter;
	struct task_struct *tsk;
191
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
192

193
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
194 195

	/* set up my own style of waitqueue */
196
	tsk = current;
Linus Torvalds's avatar
Linus Torvalds committed
197
	waiter.task = tsk;
198
	waiter.type = RWSEM_WAITING_FOR_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
199 200
	list_add_tail(&waiter.list, &sem->wait_list);

201
	/* wait for someone to release the lock */
Linus Torvalds's avatar
Linus Torvalds committed
202
	for (;;) {
203 204 205 206 207 208 209
		/*
		 * 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.
		 */
		if (sem->activity == 0)
Linus Torvalds's avatar
Linus Torvalds committed
210 211
			break;
		set_task_state(tsk, TASK_UNINTERRUPTIBLE);
212 213 214
		raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
		schedule();
		raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
215
	}
216 217 218
	/* got the lock */
	sem->activity = -1;
	list_del(&waiter.list);
Linus Torvalds's avatar
Linus Torvalds committed
219

220
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
221 222
}

223
void __sched __down_write(struct rw_semaphore *sem)
224 225 226 227
{
	__down_write_nested(sem, 0);
}

Linus Torvalds's avatar
Linus Torvalds committed
228 229 230
/*
 * trylock for writing -- returns 1 if successful, 0 if contention
 */
231
int __down_write_trylock(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235
{
	unsigned long flags;
	int ret = 0;

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

238 239
	if (sem->activity == 0) {
		/* got the lock */
Linus Torvalds's avatar
Linus Torvalds committed
240 241 242 243
		sem->activity = -1;
		ret = 1;
	}

244
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248 249 250 251

	return ret;
}

/*
 * release a read lock on the semaphore
 */
252
void __up_read(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
253 254 255
{
	unsigned long flags;

256
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
257 258 259 260

	if (--sem->activity == 0 && !list_empty(&sem->wait_list))
		sem = __rwsem_wake_one_writer(sem);

261
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
262 263 264 265 266
}

/*
 * release a write lock on the semaphore
 */
267
void __up_write(struct rw_semaphore *sem)
Linus Torvalds's avatar
Linus Torvalds committed
268 269 270
{
	unsigned long flags;

271
	raw_spin_lock_irqsave(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274 275 276

	sem->activity = 0;
	if (!list_empty(&sem->wait_list))
		sem = __rwsem_do_wake(sem, 1);

277
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
278 279 280 281 282 283
}

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

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

	sem->activity = 1;
	if (!list_empty(&sem->wait_list))
		sem = __rwsem_do_wake(sem, 0);

294
	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
295 296
}