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