1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * rtmutex API
4 */
5 #include <linux/spinlock.h>
6 #include <linux/export.h>
7
8 #define RT_MUTEX_BUILD_MUTEX
9 #include "rtmutex.c"
10
11 /*
12 * Max number of times we'll walk the boosting chain:
13 */
14 int max_lock_depth = 1024;
15
16 /*
17 * Debug aware fast / slowpath lock,trylock,unlock
18 *
19 * The atomic acquire/release ops are compiled away, when either the
20 * architecture does not support cmpxchg or when debugging is enabled.
21 */
__rt_mutex_lock_common(struct rt_mutex * lock,unsigned int state,struct lockdep_map * nest_lock,unsigned int subclass)22 static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
23 unsigned int state,
24 struct lockdep_map *nest_lock,
25 unsigned int subclass)
26 {
27 int ret;
28
29 might_sleep();
30 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, _RET_IP_);
31 ret = __rt_mutex_lock(&lock->rtmutex, state);
32 if (ret)
33 mutex_release(&lock->dep_map, _RET_IP_);
34 else
35 trace_android_vh_record_rtmutex_lock_starttime(lock, jiffies);
36 return ret;
37 }
38
rt_mutex_base_init(struct rt_mutex_base * rtb)39 void rt_mutex_base_init(struct rt_mutex_base *rtb)
40 {
41 __rt_mutex_base_init(rtb);
42 }
43 EXPORT_SYMBOL(rt_mutex_base_init);
44
45 #ifdef CONFIG_DEBUG_LOCK_ALLOC
46 /**
47 * rt_mutex_lock_nested - lock a rt_mutex
48 *
49 * @lock: the rt_mutex to be locked
50 * @subclass: the lockdep subclass
51 */
rt_mutex_lock_nested(struct rt_mutex * lock,unsigned int subclass)52 void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
53 {
54 __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
55 }
56 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
57
_rt_mutex_lock_nest_lock(struct rt_mutex * lock,struct lockdep_map * nest_lock)58 void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
59 {
60 __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
61 }
62 EXPORT_SYMBOL_GPL(_rt_mutex_lock_nest_lock);
63
64 #else /* !CONFIG_DEBUG_LOCK_ALLOC */
65
66 /**
67 * rt_mutex_lock - lock a rt_mutex
68 *
69 * @lock: the rt_mutex to be locked
70 */
rt_mutex_lock(struct rt_mutex * lock)71 void __sched rt_mutex_lock(struct rt_mutex *lock)
72 {
73 __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, 0);
74 }
75 EXPORT_SYMBOL_GPL(rt_mutex_lock);
76 #endif
77
78 /**
79 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
80 *
81 * @lock: the rt_mutex to be locked
82 *
83 * Returns:
84 * 0 on success
85 * -EINTR when interrupted by a signal
86 */
rt_mutex_lock_interruptible(struct rt_mutex * lock)87 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
88 {
89 return __rt_mutex_lock_common(lock, TASK_INTERRUPTIBLE, NULL, 0);
90 }
91 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
92
93 /**
94 * rt_mutex_lock_killable - lock a rt_mutex killable
95 *
96 * @lock: the rt_mutex to be locked
97 *
98 * Returns:
99 * 0 on success
100 * -EINTR when interrupted by a signal
101 */
rt_mutex_lock_killable(struct rt_mutex * lock)102 int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
103 {
104 return __rt_mutex_lock_common(lock, TASK_KILLABLE, NULL, 0);
105 }
106 EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
107
108 /**
109 * rt_mutex_trylock - try to lock a rt_mutex
110 *
111 * @lock: the rt_mutex to be locked
112 *
113 * This function can only be called in thread context. It's safe to call it
114 * from atomic regions, but not from hard or soft interrupt context.
115 *
116 * Returns:
117 * 1 on success
118 * 0 on contention
119 */
rt_mutex_trylock(struct rt_mutex * lock)120 int __sched rt_mutex_trylock(struct rt_mutex *lock)
121 {
122 int ret;
123
124 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
125 return 0;
126
127 ret = __rt_mutex_trylock(&lock->rtmutex);
128 if (ret) {
129 trace_android_vh_record_rtmutex_lock_starttime(lock, jiffies);
130 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
131 }
132
133 return ret;
134 }
135 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
136
137 /**
138 * rt_mutex_unlock - unlock a rt_mutex
139 *
140 * @lock: the rt_mutex to be unlocked
141 */
rt_mutex_unlock(struct rt_mutex * lock)142 void __sched rt_mutex_unlock(struct rt_mutex *lock)
143 {
144 trace_android_vh_record_rtmutex_lock_starttime(lock, 0);
145 mutex_release(&lock->dep_map, _RET_IP_);
146 __rt_mutex_unlock(&lock->rtmutex);
147 }
148 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
149
150 /*
151 * Futex variants, must not use fastpath.
152 */
rt_mutex_futex_trylock(struct rt_mutex_base * lock)153 int __sched rt_mutex_futex_trylock(struct rt_mutex_base *lock)
154 {
155 return rt_mutex_slowtrylock(lock);
156 }
157
__rt_mutex_futex_trylock(struct rt_mutex_base * lock)158 int __sched __rt_mutex_futex_trylock(struct rt_mutex_base *lock)
159 {
160 return __rt_mutex_slowtrylock(lock);
161 }
162
163 /**
164 * __rt_mutex_futex_unlock - Futex variant, that since futex variants
165 * do not use the fast-path, can be simple and will not need to retry.
166 *
167 * @lock: The rt_mutex to be unlocked
168 * @wqh: The wake queue head from which to get the next lock waiter
169 */
__rt_mutex_futex_unlock(struct rt_mutex_base * lock,struct rt_wake_q_head * wqh)170 bool __sched __rt_mutex_futex_unlock(struct rt_mutex_base *lock,
171 struct rt_wake_q_head *wqh)
172 {
173 lockdep_assert_held(&lock->wait_lock);
174
175 debug_rt_mutex_unlock(lock);
176
177 if (!rt_mutex_has_waiters(lock)) {
178 lock->owner = NULL;
179 return false; /* done */
180 }
181
182 /*
183 * We've already deboosted, mark_wakeup_next_waiter() will
184 * retain preempt_disabled when we drop the wait_lock, to
185 * avoid inversion prior to the wakeup. preempt_disable()
186 * therein pairs with rt_mutex_postunlock().
187 */
188 mark_wakeup_next_waiter(wqh, lock);
189
190 return true; /* call postunlock() */
191 }
192
rt_mutex_futex_unlock(struct rt_mutex_base * lock)193 void __sched rt_mutex_futex_unlock(struct rt_mutex_base *lock)
194 {
195 DEFINE_RT_WAKE_Q(wqh);
196 unsigned long flags;
197 bool postunlock;
198
199 raw_spin_lock_irqsave(&lock->wait_lock, flags);
200 postunlock = __rt_mutex_futex_unlock(lock, &wqh);
201 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
202
203 if (postunlock)
204 rt_mutex_postunlock(&wqh);
205 }
206
207 /**
208 * __rt_mutex_init - initialize the rt_mutex
209 *
210 * @lock: The rt_mutex to be initialized
211 * @name: The lock name used for debugging
212 * @key: The lock class key used for debugging
213 *
214 * Initialize the rt_mutex to unlocked state.
215 *
216 * Initializing of a locked rt_mutex is not allowed
217 */
__rt_mutex_init(struct rt_mutex * lock,const char * name,struct lock_class_key * key)218 void __sched __rt_mutex_init(struct rt_mutex *lock, const char *name,
219 struct lock_class_key *key)
220 {
221 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
222 __rt_mutex_base_init(&lock->rtmutex);
223 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
224 }
225 EXPORT_SYMBOL_GPL(__rt_mutex_init);
226
227 /**
228 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
229 * proxy owner
230 *
231 * @lock: the rt_mutex to be locked
232 * @proxy_owner:the task to set as owner
233 *
234 * No locking. Caller has to do serializing itself
235 *
236 * Special API call for PI-futex support. This initializes the rtmutex and
237 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
238 * possible at this point because the pi_state which contains the rtmutex
239 * is not yet visible to other tasks.
240 */
rt_mutex_init_proxy_locked(struct rt_mutex_base * lock,struct task_struct * proxy_owner)241 void __sched rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
242 struct task_struct *proxy_owner)
243 {
244 static struct lock_class_key pi_futex_key;
245
246 __rt_mutex_base_init(lock);
247 /*
248 * On PREEMPT_RT the futex hashbucket spinlock becomes 'sleeping'
249 * and rtmutex based. That causes a lockdep false positive, because
250 * some of the futex functions invoke spin_unlock(&hb->lock) with
251 * the wait_lock of the rtmutex associated to the pi_futex held.
252 * spin_unlock() in turn takes wait_lock of the rtmutex on which
253 * the spinlock is based, which makes lockdep notice a lock
254 * recursion. Give the futex/rtmutex wait_lock a separate key.
255 */
256 lockdep_set_class(&lock->wait_lock, &pi_futex_key);
257 rt_mutex_set_owner(lock, proxy_owner);
258 }
259
260 /**
261 * rt_mutex_proxy_unlock - release a lock on behalf of owner
262 *
263 * @lock: the rt_mutex to be locked
264 *
265 * No locking. Caller has to do serializing itself
266 *
267 * Special API call for PI-futex support. This just cleans up the rtmutex
268 * (debugging) state. Concurrent operations on this rt_mutex are not
269 * possible because it belongs to the pi_state which is about to be freed
270 * and it is not longer visible to other tasks.
271 */
rt_mutex_proxy_unlock(struct rt_mutex_base * lock)272 void __sched rt_mutex_proxy_unlock(struct rt_mutex_base *lock)
273 {
274 debug_rt_mutex_proxy_unlock(lock);
275 rt_mutex_clear_owner(lock);
276 }
277
278 /**
279 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
280 * @lock: the rt_mutex to take
281 * @waiter: the pre-initialized rt_mutex_waiter
282 * @task: the task to prepare
283 * @wake_q: the wake_q to wake tasks after we release the wait_lock
284 *
285 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
286 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
287 *
288 * NOTE: does _NOT_ remove the @waiter on failure; must either call
289 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
290 *
291 * Returns:
292 * 0 - task blocked on lock
293 * 1 - acquired the lock for task, caller should wake it up
294 * <0 - error
295 *
296 * Special API call for PI-futex support.
297 */
__rt_mutex_start_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task,struct wake_q_head * wake_q)298 int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
299 struct rt_mutex_waiter *waiter,
300 struct task_struct *task,
301 struct wake_q_head *wake_q)
302 {
303 int ret;
304
305 lockdep_assert_held(&lock->wait_lock);
306
307 if (try_to_take_rt_mutex(lock, task, NULL))
308 return 1;
309
310 /* We enforce deadlock detection for futexes */
311 ret = task_blocks_on_rt_mutex(lock, waiter, task, NULL,
312 RT_MUTEX_FULL_CHAINWALK, wake_q);
313
314 if (ret && !rt_mutex_owner(lock)) {
315 /*
316 * Reset the return value. We might have
317 * returned with -EDEADLK and the owner
318 * released the lock while we were walking the
319 * pi chain. Let the waiter sort it out.
320 */
321 ret = 0;
322 }
323
324 return ret;
325 }
326
327 /**
328 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
329 * @lock: the rt_mutex to take
330 * @waiter: the pre-initialized rt_mutex_waiter
331 * @task: the task to prepare
332 *
333 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
334 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
335 *
336 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
337 * on failure.
338 *
339 * Returns:
340 * 0 - task blocked on lock
341 * 1 - acquired the lock for task, caller should wake it up
342 * <0 - error
343 *
344 * Special API call for PI-futex support.
345 */
rt_mutex_start_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task)346 int __sched rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
347 struct rt_mutex_waiter *waiter,
348 struct task_struct *task)
349 {
350 int ret;
351 DEFINE_WAKE_Q(wake_q);
352
353 raw_spin_lock_irq(&lock->wait_lock);
354 ret = __rt_mutex_start_proxy_lock(lock, waiter, task, &wake_q);
355 if (unlikely(ret))
356 remove_waiter(lock, waiter);
357 preempt_disable();
358 raw_spin_unlock_irq(&lock->wait_lock);
359 wake_up_q(&wake_q);
360 preempt_enable();
361
362 return ret;
363 }
364
365 /**
366 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
367 * @lock: the rt_mutex we were woken on
368 * @to: the timeout, null if none. hrtimer should already have
369 * been started.
370 * @waiter: the pre-initialized rt_mutex_waiter
371 *
372 * Wait for the lock acquisition started on our behalf by
373 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
374 * rt_mutex_cleanup_proxy_lock().
375 *
376 * Returns:
377 * 0 - success
378 * <0 - error, one of -EINTR, -ETIMEDOUT
379 *
380 * Special API call for PI-futex support
381 */
rt_mutex_wait_proxy_lock(struct rt_mutex_base * lock,struct hrtimer_sleeper * to,struct rt_mutex_waiter * waiter)382 int __sched rt_mutex_wait_proxy_lock(struct rt_mutex_base *lock,
383 struct hrtimer_sleeper *to,
384 struct rt_mutex_waiter *waiter)
385 {
386 int ret;
387
388 raw_spin_lock_irq(&lock->wait_lock);
389 /* sleep on the mutex */
390 set_current_state(TASK_INTERRUPTIBLE);
391 ret = rt_mutex_slowlock_block(lock, NULL, TASK_INTERRUPTIBLE, to, waiter, NULL);
392 /*
393 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
394 * have to fix that up.
395 */
396 fixup_rt_mutex_waiters(lock, true);
397 raw_spin_unlock_irq(&lock->wait_lock);
398
399 return ret;
400 }
401
402 /**
403 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
404 * @lock: the rt_mutex we were woken on
405 * @waiter: the pre-initialized rt_mutex_waiter
406 *
407 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
408 * rt_mutex_wait_proxy_lock().
409 *
410 * Unless we acquired the lock; we're still enqueued on the wait-list and can
411 * in fact still be granted ownership until we're removed. Therefore we can
412 * find we are in fact the owner and must disregard the
413 * rt_mutex_wait_proxy_lock() failure.
414 *
415 * Returns:
416 * true - did the cleanup, we done.
417 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
418 * caller should disregards its return value.
419 *
420 * Special API call for PI-futex support
421 */
rt_mutex_cleanup_proxy_lock(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)422 bool __sched rt_mutex_cleanup_proxy_lock(struct rt_mutex_base *lock,
423 struct rt_mutex_waiter *waiter)
424 {
425 bool cleanup = false;
426
427 raw_spin_lock_irq(&lock->wait_lock);
428 /*
429 * Do an unconditional try-lock, this deals with the lock stealing
430 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
431 * sets a NULL owner.
432 *
433 * We're not interested in the return value, because the subsequent
434 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
435 * we will own the lock and it will have removed the waiter. If we
436 * failed the trylock, we're still not owner and we need to remove
437 * ourselves.
438 */
439 try_to_take_rt_mutex(lock, current, waiter);
440 /*
441 * Unless we're the owner; we're still enqueued on the wait_list.
442 * So check if we became owner, if not, take us off the wait_list.
443 */
444 if (rt_mutex_owner(lock) != current) {
445 remove_waiter(lock, waiter);
446 cleanup = true;
447 }
448 /*
449 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
450 * have to fix that up.
451 */
452 fixup_rt_mutex_waiters(lock, false);
453
454 raw_spin_unlock_irq(&lock->wait_lock);
455
456 return cleanup;
457 }
458
459 /*
460 * Recheck the pi chain, in case we got a priority setting
461 *
462 * Called from sched_setscheduler
463 */
rt_mutex_adjust_pi(struct task_struct * task)464 void __sched rt_mutex_adjust_pi(struct task_struct *task)
465 {
466 struct rt_mutex_waiter *waiter;
467 struct rt_mutex_base *next_lock;
468 unsigned long flags;
469
470 raw_spin_lock_irqsave(&task->pi_lock, flags);
471
472 waiter = task->pi_blocked_on;
473 if (!waiter || rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
474 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
475 return;
476 }
477 next_lock = waiter->lock;
478 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
479
480 /* gets dropped in rt_mutex_adjust_prio_chain()! */
481 get_task_struct(task);
482
483 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
484 next_lock, NULL, task);
485 }
486
487 /*
488 * Performs the wakeup of the top-waiter and re-enables preemption.
489 */
rt_mutex_postunlock(struct rt_wake_q_head * wqh)490 void __sched rt_mutex_postunlock(struct rt_wake_q_head *wqh)
491 {
492 rt_mutex_wake_up_q(wqh);
493 }
494
495 #ifdef CONFIG_DEBUG_RT_MUTEXES
rt_mutex_debug_task_free(struct task_struct * task)496 void rt_mutex_debug_task_free(struct task_struct *task)
497 {
498 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
499 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
500 }
501 #endif
502
503 #ifdef CONFIG_PREEMPT_RT
504 /* Mutexes */
__mutex_rt_init(struct mutex * mutex,const char * name,struct lock_class_key * key)505 void __mutex_rt_init(struct mutex *mutex, const char *name,
506 struct lock_class_key *key)
507 {
508 debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
509 lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP);
510 }
511 EXPORT_SYMBOL(__mutex_rt_init);
512
__mutex_lock_common(struct mutex * lock,unsigned int state,unsigned int subclass,struct lockdep_map * nest_lock,unsigned long ip)513 static __always_inline int __mutex_lock_common(struct mutex *lock,
514 unsigned int state,
515 unsigned int subclass,
516 struct lockdep_map *nest_lock,
517 unsigned long ip)
518 {
519 int ret;
520
521 might_sleep();
522 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
523 ret = __rt_mutex_lock(&lock->rtmutex, state);
524 if (ret)
525 mutex_release(&lock->dep_map, ip);
526 else
527 lock_acquired(&lock->dep_map, ip);
528 return ret;
529 }
530
531 #ifdef CONFIG_DEBUG_LOCK_ALLOC
mutex_lock_nested(struct mutex * lock,unsigned int subclass)532 void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass)
533 {
534 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
535 }
536 EXPORT_SYMBOL_GPL(mutex_lock_nested);
537
_mutex_lock_nest_lock(struct mutex * lock,struct lockdep_map * nest_lock)538 void __sched _mutex_lock_nest_lock(struct mutex *lock,
539 struct lockdep_map *nest_lock)
540 {
541 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest_lock, _RET_IP_);
542 }
543 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
544
mutex_lock_interruptible_nested(struct mutex * lock,unsigned int subclass)545 int __sched mutex_lock_interruptible_nested(struct mutex *lock,
546 unsigned int subclass)
547 {
548 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
549 }
550 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
551
mutex_lock_killable_nested(struct mutex * lock,unsigned int subclass)552 int __sched mutex_lock_killable_nested(struct mutex *lock,
553 unsigned int subclass)
554 {
555 return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
556 }
557 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
558
mutex_lock_io_nested(struct mutex * lock,unsigned int subclass)559 void __sched mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
560 {
561 int token;
562
563 might_sleep();
564
565 token = io_schedule_prepare();
566 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
567 io_schedule_finish(token);
568 }
569 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
570
571 #else /* CONFIG_DEBUG_LOCK_ALLOC */
572
mutex_lock(struct mutex * lock)573 void __sched mutex_lock(struct mutex *lock)
574 {
575 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
576 }
577 EXPORT_SYMBOL(mutex_lock);
578
mutex_lock_interruptible(struct mutex * lock)579 int __sched mutex_lock_interruptible(struct mutex *lock)
580 {
581 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
582 }
583 EXPORT_SYMBOL(mutex_lock_interruptible);
584
mutex_lock_killable(struct mutex * lock)585 int __sched mutex_lock_killable(struct mutex *lock)
586 {
587 return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
588 }
589 EXPORT_SYMBOL(mutex_lock_killable);
590
mutex_lock_io(struct mutex * lock)591 void __sched mutex_lock_io(struct mutex *lock)
592 {
593 int token = io_schedule_prepare();
594
595 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
596 io_schedule_finish(token);
597 }
598 EXPORT_SYMBOL(mutex_lock_io);
599 #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
600
mutex_trylock(struct mutex * lock)601 int __sched mutex_trylock(struct mutex *lock)
602 {
603 int ret;
604
605 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
606 return 0;
607
608 ret = __rt_mutex_trylock(&lock->rtmutex);
609 if (ret)
610 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
611
612 return ret;
613 }
614 EXPORT_SYMBOL(mutex_trylock);
615
mutex_unlock(struct mutex * lock)616 void __sched mutex_unlock(struct mutex *lock)
617 {
618 mutex_release(&lock->dep_map, _RET_IP_);
619 __rt_mutex_unlock(&lock->rtmutex);
620 }
621 EXPORT_SYMBOL(mutex_unlock);
622
623 #endif /* CONFIG_PREEMPT_RT */
624