1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/mutex.h>
16#include <linux/sched.h>
17#include <linux/module.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.h>
20#include <linux/debug_locks.h>
21
22
23
24
25
26#ifdef CONFIG_DEBUG_MUTEXES
27# include "mutex-debug.h"
28# include <asm-generic/mutex-null.h>
29#else
30# include "mutex.h"
31# include <asm/mutex.h>
32#endif
33
34
35
36
37
38
39
40
41
42void
43__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
44{
45 atomic_set(&lock->count, 1);
46 spin_lock_init(&lock->wait_lock);
47 INIT_LIST_HEAD(&lock->wait_list);
48
49 debug_mutex_init(lock, name, key);
50}
51
52EXPORT_SYMBOL(__mutex_init);
53
54#ifndef CONFIG_DEBUG_LOCK_ALLOC
55
56
57
58
59
60
61static void noinline __sched
62__mutex_lock_slowpath(atomic_t *lock_count);
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85void inline __sched mutex_lock(struct mutex *lock)
86{
87 might_sleep();
88
89
90
91
92 __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
93}
94
95EXPORT_SYMBOL(mutex_lock);
96#endif
97
98static noinline void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
99
100
101
102
103
104
105
106
107
108
109
110
111void __sched mutex_unlock(struct mutex *lock)
112{
113
114
115
116
117 __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
118}
119
120EXPORT_SYMBOL(mutex_unlock);
121
122
123
124
125static inline int __sched
126__mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
127 unsigned long ip)
128{
129 struct task_struct *task = current;
130 struct mutex_waiter waiter;
131 unsigned int old_val;
132 unsigned long flags;
133
134 spin_lock_mutex(&lock->wait_lock, flags);
135
136 debug_mutex_lock_common(lock, &waiter);
137 mutex_acquire(&lock->dep_map, subclass, 0, ip);
138 debug_mutex_add_waiter(lock, &waiter, task_thread_info(task));
139
140
141 list_add_tail(&waiter.list, &lock->wait_list);
142 waiter.task = task;
143
144 old_val = atomic_xchg(&lock->count, -1);
145 if (old_val == 1)
146 goto done;
147
148 lock_contended(&lock->dep_map, ip);
149
150 for (;;) {
151
152
153
154
155
156
157
158
159
160 old_val = atomic_xchg(&lock->count, -1);
161 if (old_val == 1)
162 break;
163
164
165
166
167
168 if (unlikely((state == TASK_INTERRUPTIBLE &&
169 signal_pending(task)) ||
170 (state == TASK_KILLABLE &&
171 fatal_signal_pending(task)))) {
172 mutex_remove_waiter(lock, &waiter,
173 task_thread_info(task));
174 mutex_release(&lock->dep_map, 1, ip);
175 spin_unlock_mutex(&lock->wait_lock, flags);
176
177 debug_mutex_free_waiter(&waiter);
178 return -EINTR;
179 }
180 __set_task_state(task, state);
181
182
183 spin_unlock_mutex(&lock->wait_lock, flags);
184 schedule();
185 spin_lock_mutex(&lock->wait_lock, flags);
186 }
187
188done:
189 lock_acquired(&lock->dep_map);
190
191 mutex_remove_waiter(lock, &waiter, task_thread_info(task));
192 debug_mutex_set_owner(lock, task_thread_info(task));
193
194
195 if (likely(list_empty(&lock->wait_list)))
196 atomic_set(&lock->count, 0);
197
198 spin_unlock_mutex(&lock->wait_lock, flags);
199
200 debug_mutex_free_waiter(&waiter);
201
202 return 0;
203}
204
205#ifdef CONFIG_DEBUG_LOCK_ALLOC
206void __sched
207mutex_lock_nested(struct mutex *lock, unsigned int subclass)
208{
209 might_sleep();
210 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, _RET_IP_);
211}
212
213EXPORT_SYMBOL_GPL(mutex_lock_nested);
214
215int __sched
216mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
217{
218 might_sleep();
219 return __mutex_lock_common(lock, TASK_KILLABLE, subclass, _RET_IP_);
220}
221EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
222
223int __sched
224mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
225{
226 might_sleep();
227 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass, _RET_IP_);
228}
229
230EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
231#endif
232
233
234
235
236static inline void
237__mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
238{
239 struct mutex *lock = container_of(lock_count, struct mutex, count);
240 unsigned long flags;
241
242 spin_lock_mutex(&lock->wait_lock, flags);
243 mutex_release(&lock->dep_map, nested, _RET_IP_);
244 debug_mutex_unlock(lock);
245
246
247
248
249
250
251 if (__mutex_slowpath_needs_to_unlock())
252 atomic_set(&lock->count, 1);
253
254 if (!list_empty(&lock->wait_list)) {
255
256 struct mutex_waiter *waiter =
257 list_entry(lock->wait_list.next,
258 struct mutex_waiter, list);
259
260 debug_mutex_wake_waiter(lock, waiter);
261
262 wake_up_process(waiter->task);
263 }
264
265 debug_mutex_clear_owner(lock);
266
267 spin_unlock_mutex(&lock->wait_lock, flags);
268}
269
270
271
272
273static noinline void
274__mutex_unlock_slowpath(atomic_t *lock_count)
275{
276 __mutex_unlock_common_slowpath(lock_count, 1);
277}
278
279#ifndef CONFIG_DEBUG_LOCK_ALLOC
280
281
282
283
284static noinline int __sched
285__mutex_lock_killable_slowpath(atomic_t *lock_count);
286
287static noinline int __sched
288__mutex_lock_interruptible_slowpath(atomic_t *lock_count);
289
290
291
292
293
294
295
296
297
298
299
300
301int __sched mutex_lock_interruptible(struct mutex *lock)
302{
303 might_sleep();
304 return __mutex_fastpath_lock_retval
305 (&lock->count, __mutex_lock_interruptible_slowpath);
306}
307
308EXPORT_SYMBOL(mutex_lock_interruptible);
309
310int __sched mutex_lock_killable(struct mutex *lock)
311{
312 might_sleep();
313 return __mutex_fastpath_lock_retval
314 (&lock->count, __mutex_lock_killable_slowpath);
315}
316EXPORT_SYMBOL(mutex_lock_killable);
317
318static noinline void __sched
319__mutex_lock_slowpath(atomic_t *lock_count)
320{
321 struct mutex *lock = container_of(lock_count, struct mutex, count);
322
323 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, _RET_IP_);
324}
325
326static noinline int __sched
327__mutex_lock_killable_slowpath(atomic_t *lock_count)
328{
329 struct mutex *lock = container_of(lock_count, struct mutex, count);
330
331 return __mutex_lock_common(lock, TASK_KILLABLE, 0, _RET_IP_);
332}
333
334static noinline int __sched
335__mutex_lock_interruptible_slowpath(atomic_t *lock_count)
336{
337 struct mutex *lock = container_of(lock_count, struct mutex, count);
338
339 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, _RET_IP_);
340}
341#endif
342
343
344
345
346
347static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
348{
349 struct mutex *lock = container_of(lock_count, struct mutex, count);
350 unsigned long flags;
351 int prev;
352
353 spin_lock_mutex(&lock->wait_lock, flags);
354
355 prev = atomic_xchg(&lock->count, -1);
356 if (likely(prev == 1)) {
357 debug_mutex_set_owner(lock, current_thread_info());
358 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
359 }
360
361 if (likely(list_empty(&lock->wait_list)))
362 atomic_set(&lock->count, 0);
363
364 spin_unlock_mutex(&lock->wait_lock, flags);
365
366 return prev == 1;
367}
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383int __sched mutex_trylock(struct mutex *lock)
384{
385 return __mutex_fastpath_trylock(&lock->count,
386 __mutex_trylock_slowpath);
387}
388
389EXPORT_SYMBOL(mutex_trylock);
390