1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/compiler.h>
29#include <linux/kernel.h>
30#include <linux/export.h>
31#include <linux/sched.h>
32#include <linux/sched/debug.h>
33#include <linux/semaphore.h>
34#include <linux/spinlock.h>
35#include <linux/ftrace.h>
36
37static noinline void __down(struct semaphore *sem);
38static noinline int __down_interruptible(struct semaphore *sem);
39static noinline int __down_killable(struct semaphore *sem);
40static noinline int __down_timeout(struct semaphore *sem, long timeout);
41static noinline void __up(struct semaphore *sem);
42
43
44
45
46
47
48
49
50
51
52
53
54void down(struct semaphore *sem)
55{
56 unsigned long flags;
57
58 raw_spin_lock_irqsave(&sem->lock, flags);
59 if (likely(sem->count > 0))
60 sem->count--;
61 else
62 __down(sem);
63 raw_spin_unlock_irqrestore(&sem->lock, flags);
64}
65EXPORT_SYMBOL(down);
66
67
68
69
70
71
72
73
74
75
76int down_interruptible(struct semaphore *sem)
77{
78 unsigned long flags;
79 int result = 0;
80
81 raw_spin_lock_irqsave(&sem->lock, flags);
82 if (likely(sem->count > 0))
83 sem->count--;
84 else
85 result = __down_interruptible(sem);
86 raw_spin_unlock_irqrestore(&sem->lock, flags);
87
88 return result;
89}
90EXPORT_SYMBOL(down_interruptible);
91
92
93
94
95
96
97
98
99
100
101
102int down_killable(struct semaphore *sem)
103{
104 unsigned long flags;
105 int result = 0;
106
107 raw_spin_lock_irqsave(&sem->lock, flags);
108 if (likely(sem->count > 0))
109 sem->count--;
110 else
111 result = __down_killable(sem);
112 raw_spin_unlock_irqrestore(&sem->lock, flags);
113
114 return result;
115}
116EXPORT_SYMBOL(down_killable);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131int down_trylock(struct semaphore *sem)
132{
133 unsigned long flags;
134 int count;
135
136 raw_spin_lock_irqsave(&sem->lock, flags);
137 count = sem->count - 1;
138 if (likely(count >= 0))
139 sem->count = count;
140 raw_spin_unlock_irqrestore(&sem->lock, flags);
141
142 return (count < 0);
143}
144EXPORT_SYMBOL(down_trylock);
145
146
147
148
149
150
151
152
153
154
155
156int down_timeout(struct semaphore *sem, long timeout)
157{
158 unsigned long flags;
159 int result = 0;
160
161 raw_spin_lock_irqsave(&sem->lock, flags);
162 if (likely(sem->count > 0))
163 sem->count--;
164 else
165 result = __down_timeout(sem, timeout);
166 raw_spin_unlock_irqrestore(&sem->lock, flags);
167
168 return result;
169}
170EXPORT_SYMBOL(down_timeout);
171
172
173
174
175
176
177
178
179void up(struct semaphore *sem)
180{
181 unsigned long flags;
182
183 raw_spin_lock_irqsave(&sem->lock, flags);
184 if (likely(list_empty(&sem->wait_list)))
185 sem->count++;
186 else
187 __up(sem);
188 raw_spin_unlock_irqrestore(&sem->lock, flags);
189}
190EXPORT_SYMBOL(up);
191
192
193
194struct semaphore_waiter {
195 struct list_head list;
196 struct task_struct *task;
197 bool up;
198};
199
200
201
202
203
204
205static inline int __sched __down_common(struct semaphore *sem, long state,
206 long timeout)
207{
208 struct semaphore_waiter waiter;
209
210 list_add_tail(&waiter.list, &sem->wait_list);
211 waiter.task = current;
212 waiter.up = false;
213
214 for (;;) {
215 if (signal_pending_state(state, current))
216 goto interrupted;
217 if (unlikely(timeout <= 0))
218 goto timed_out;
219 __set_current_state(state);
220 raw_spin_unlock_irq(&sem->lock);
221 timeout = schedule_timeout(timeout);
222 raw_spin_lock_irq(&sem->lock);
223 if (waiter.up)
224 return 0;
225 }
226
227 timed_out:
228 list_del(&waiter.list);
229 return -ETIME;
230
231 interrupted:
232 list_del(&waiter.list);
233 return -EINTR;
234}
235
236static noinline void __sched __down(struct semaphore *sem)
237{
238 __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
239}
240
241static noinline int __sched __down_interruptible(struct semaphore *sem)
242{
243 return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
244}
245
246static noinline int __sched __down_killable(struct semaphore *sem)
247{
248 return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
249}
250
251static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
252{
253 return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
254}
255
256static noinline void __sched __up(struct semaphore *sem)
257{
258 struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
259 struct semaphore_waiter, list);
260 list_del(&waiter->list);
261 waiter->up = true;
262 wake_up_process(waiter->task);
263}
264