semaphore.c 7.23 KB
Newer Older
1
2
3
4
5
/*
 * Copyright (c) 2008 Intel Corporation
 * Author: Matthew Wilcox <willy@linux.intel.com>
 *
 * Distributed under the terms of the GNU GPL, version 2
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * This file implements counting semaphores.
 * A counting semaphore may be acquired 'n' times before sleeping.
 * See mutex.c for single-acquisition sleeping locks which enforce
 * rules which allow code to be debugged more easily.
 */

/*
 * Some notes on the implementation:
 *
 * The spinlock controls access to the other members of the semaphore.
 * down_trylock() and up() can be called from interrupt context, so we
 * have to disable interrupts when taking the lock.  It turns out various
 * parts of the kernel expect to be able to use down() on a semaphore in
 * interrupt context when they know it will succeed, so we have to use
 * irqsave variants for down(), down_interruptible() and down_killable()
 * too.
 *
 * The ->count variable represents how many more tasks can acquire this
 * semaphore.  If it's zero, there may be tasks waiting on the wait_list.
26
27
28
29
 */

#include <linux/compiler.h>
#include <linux/kernel.h>
30
#include <linux/export.h>
31
32
33
#include <linux/sched.h>
#include <linux/semaphore.h>
#include <linux/spinlock.h>
Ingo Molnar's avatar
Ingo Molnar committed
34
#include <linux/ftrace.h>
35
36
37

static noinline void __down(struct semaphore *sem);
static noinline int __down_interruptible(struct semaphore *sem);
Matthew Wilcox's avatar
Matthew Wilcox committed
38
static noinline int __down_killable(struct semaphore *sem);
39
static noinline int __down_timeout(struct semaphore *sem, long jiffies);
40
41
static noinline void __up(struct semaphore *sem);

42
43
44
45
46
47
48
49
50
51
52
/**
 * down - acquire the semaphore
 * @sem: the semaphore to be acquired
 *
 * Acquires the semaphore.  If no more tasks are allowed to acquire the
 * semaphore, calling this function will put the task to sleep until the
 * semaphore is released.
 *
 * Use of this function is deprecated, please use down_interruptible() or
 * down_killable() instead.
 */
53
54
55
56
void down(struct semaphore *sem)
{
	unsigned long flags;

57
	raw_spin_lock_irqsave(&sem->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
58
59
60
	if (likely(sem->count > 0))
		sem->count--;
	else
61
		__down(sem);
62
	raw_spin_unlock_irqrestore(&sem->lock, flags);
63
64
65
}
EXPORT_SYMBOL(down);

66
67
68
69
70
71
72
73
74
/**
 * down_interruptible - acquire the semaphore unless interrupted
 * @sem: the semaphore to be acquired
 *
 * Attempts to acquire the semaphore.  If no more tasks are allowed to
 * acquire the semaphore, calling this function will put the task to sleep.
 * If the sleep is interrupted by a signal, this function will return -EINTR.
 * If the semaphore is successfully acquired, this function returns 0.
 */
75
76
77
78
79
int down_interruptible(struct semaphore *sem)
{
	unsigned long flags;
	int result = 0;

80
	raw_spin_lock_irqsave(&sem->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
81
	if (likely(sem->count > 0))
Ingo Molnar's avatar
Ingo Molnar committed
82
		sem->count--;
Linus Torvalds's avatar
Linus Torvalds committed
83
84
	else
		result = __down_interruptible(sem);
85
	raw_spin_unlock_irqrestore(&sem->lock, flags);
86
87
88
89
90

	return result;
}
EXPORT_SYMBOL(down_interruptible);

91
92
93
94
95
96
97
98
99
100
/**
 * down_killable - acquire the semaphore unless killed
 * @sem: the semaphore to be acquired
 *
 * Attempts to acquire the semaphore.  If no more tasks are allowed to
 * acquire the semaphore, calling this function will put the task to sleep.
 * If the sleep is interrupted by a fatal signal, this function will return
 * -EINTR.  If the semaphore is successfully acquired, this function returns
 * 0.
 */
Matthew Wilcox's avatar
Matthew Wilcox committed
101
102
103
104
105
int down_killable(struct semaphore *sem)
{
	unsigned long flags;
	int result = 0;

106
	raw_spin_lock_irqsave(&sem->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
107
	if (likely(sem->count > 0))
Ingo Molnar's avatar
Ingo Molnar committed
108
		sem->count--;
Linus Torvalds's avatar
Linus Torvalds committed
109
110
	else
		result = __down_killable(sem);
111
	raw_spin_unlock_irqrestore(&sem->lock, flags);
Matthew Wilcox's avatar
Matthew Wilcox committed
112
113
114
115
116

	return result;
}
EXPORT_SYMBOL(down_killable);

117
118
119
120
121
/**
 * down_trylock - try to acquire the semaphore, without waiting
 * @sem: the semaphore to be acquired
 *
 * Try to acquire the semaphore atomically.  Returns 0 if the mutex has
122
 * been acquired successfully or 1 if it it cannot be acquired.
123
124
125
126
127
128
129
130
131
132
133
134
 *
 * NOTE: This return value is inverted from both spin_trylock and
 * mutex_trylock!  Be careful about this when converting code.
 *
 * Unlike mutex_trylock, this function can be used from interrupt context,
 * and the semaphore can be released by any task or interrupt.
 */
int down_trylock(struct semaphore *sem)
{
	unsigned long flags;
	int count;

135
	raw_spin_lock_irqsave(&sem->lock, flags);
136
137
138
	count = sem->count - 1;
	if (likely(count >= 0))
		sem->count = count;
139
	raw_spin_unlock_irqrestore(&sem->lock, flags);
140
141
142
143
144

	return (count < 0);
}
EXPORT_SYMBOL(down_trylock);

145
146
147
148
149
150
151
152
153
154
/**
 * down_timeout - acquire the semaphore within a specified time
 * @sem: the semaphore to be acquired
 * @jiffies: how long to wait before failing
 *
 * Attempts to acquire the semaphore.  If no more tasks are allowed to
 * acquire the semaphore, calling this function will put the task to sleep.
 * If the semaphore is not released within the specified number of jiffies,
 * this function returns -ETIME.  It returns 0 if the semaphore was acquired.
 */
155
156
157
158
159
int down_timeout(struct semaphore *sem, long jiffies)
{
	unsigned long flags;
	int result = 0;

160
	raw_spin_lock_irqsave(&sem->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
161
	if (likely(sem->count > 0))
Ingo Molnar's avatar
Ingo Molnar committed
162
		sem->count--;
Linus Torvalds's avatar
Linus Torvalds committed
163
164
	else
		result = __down_timeout(sem, jiffies);
165
	raw_spin_unlock_irqrestore(&sem->lock, flags);
166
167
168
169
170

	return result;
}
EXPORT_SYMBOL(down_timeout);

171
172
173
174
175
176
177
/**
 * up - release the semaphore
 * @sem: the semaphore to release
 *
 * Release the semaphore.  Unlike mutexes, up() may be called from any
 * context and even by tasks which have never called down().
 */
178
179
180
181
void up(struct semaphore *sem)
{
	unsigned long flags;

182
	raw_spin_lock_irqsave(&sem->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
183
184
185
	if (likely(list_empty(&sem->wait_list)))
		sem->count++;
	else
186
		__up(sem);
187
	raw_spin_unlock_irqrestore(&sem->lock, flags);
188
189
190
191
192
193
194
195
}
EXPORT_SYMBOL(up);

/* Functions for the contended case */

struct semaphore_waiter {
	struct list_head list;
	struct task_struct *task;
Linus Torvalds's avatar
Linus Torvalds committed
196
	int up;
197
198
199
};

/*
200
201
202
 * Because this function is inlined, the 'state' parameter will be
 * constant, and thus optimised away by the compiler.  Likewise the
 * 'timeout' parameter for the cases without timeouts.
203
 */
204
205
static inline int __sched __down_common(struct semaphore *sem, long state,
								long timeout)
206
207
208
209
{
	struct task_struct *task = current;
	struct semaphore_waiter waiter;

Ingo Molnar's avatar
Ingo Molnar committed
210
	list_add_tail(&waiter.list, &sem->wait_list);
Linus Torvalds's avatar
Linus Torvalds committed
211
212
	waiter.task = task;
	waiter.up = 0;
213
214

	for (;;) {
215
		if (signal_pending_state(state, task))
Linus Torvalds's avatar
Linus Torvalds committed
216
217
218
			goto interrupted;
		if (timeout <= 0)
			goto timed_out;
219
		__set_task_state(task, state);
220
		raw_spin_unlock_irq(&sem->lock);
221
		timeout = schedule_timeout(timeout);
222
		raw_spin_lock_irq(&sem->lock);
Linus Torvalds's avatar
Linus Torvalds committed
223
224
		if (waiter.up)
			return 0;
225
226
	}

Linus Torvalds's avatar
Linus Torvalds committed
227
228
229
230
231
 timed_out:
	list_del(&waiter.list);
	return -ETIME;

 interrupted:
232
	list_del(&waiter.list);
Linus Torvalds's avatar
Linus Torvalds committed
233
	return -EINTR;
234
235
236
237
}

static noinline void __sched __down(struct semaphore *sem)
{
238
	__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
239
240
241
242
}

static noinline int __sched __down_interruptible(struct semaphore *sem)
{
243
	return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
244
245
}

Matthew Wilcox's avatar
Matthew Wilcox committed
246
247
static noinline int __sched __down_killable(struct semaphore *sem)
{
248
249
250
251
252
253
	return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
}

static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies)
{
	return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies);
Matthew Wilcox's avatar
Matthew Wilcox committed
254
255
}

256
257
static noinline void __sched __up(struct semaphore *sem)
{
258
259
	struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
						struct semaphore_waiter, list);
Linus Torvalds's avatar
Linus Torvalds committed
260
261
	list_del(&waiter->list);
	waiter->up = 1;
262
	wake_up_process(waiter->task);
263
}