1 // SPDX-License-Identifier: GPL-2.0-only
5 #include <linux/spinlock.h>
6 #include <linux/export.h>
8 #define RT_MUTEX_BUILD_MUTEX
12 * Max number of times we'll walk the boosting chain:
14 int max_lock_depth = 1024;
17 * Debug aware fast / slowpath lock,trylock,unlock
19 * The atomic acquire/release ops are compiled away, when either the
20 * architecture does not support cmpxchg or when debugging is enabled.
22 static __always_inline int __rt_mutex_lock_common(struct rt_mutex *lock,
24 struct lockdep_map *nest_lock,
25 unsigned int subclass)
30 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, _RET_IP_);
31 ret = __rt_mutex_lock(&lock->rtmutex, state);
33 mutex_release(&lock->dep_map, _RET_IP_);
37 void rt_mutex_base_init(struct rt_mutex_base *rtb)
39 __rt_mutex_base_init(rtb);
41 EXPORT_SYMBOL(rt_mutex_base_init);
43 #ifdef CONFIG_DEBUG_LOCK_ALLOC
45 * rt_mutex_lock_nested - lock a rt_mutex
47 * @lock: the rt_mutex to be locked
48 * @subclass: the lockdep subclass
50 void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
52 __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, subclass);
54 EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
56 void __sched _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock)
58 __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, nest_lock, 0);
60 EXPORT_SYMBOL_GPL(_rt_mutex_lock_nest_lock);
62 #else /* !CONFIG_DEBUG_LOCK_ALLOC */
65 * rt_mutex_lock - lock a rt_mutex
67 * @lock: the rt_mutex to be locked
69 void __sched rt_mutex_lock(struct rt_mutex *lock)
71 __rt_mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, NULL, 0);
73 EXPORT_SYMBOL_GPL(rt_mutex_lock);
77 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
79 * @lock: the rt_mutex to be locked
83 * -EINTR when interrupted by a signal
85 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
87 return __rt_mutex_lock_common(lock, TASK_INTERRUPTIBLE, NULL, 0);
89 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
92 * rt_mutex_lock_killable - lock a rt_mutex killable
94 * @lock: the rt_mutex to be locked
98 * -EINTR when interrupted by a signal
100 int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
102 return __rt_mutex_lock_common(lock, TASK_KILLABLE, NULL, 0);
104 EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
107 * rt_mutex_trylock - try to lock a rt_mutex
109 * @lock: the rt_mutex to be locked
111 * This function can only be called in thread context. It's safe to call it
112 * from atomic regions, but not from hard or soft interrupt context.
118 int __sched rt_mutex_trylock(struct rt_mutex *lock)
122 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
125 ret = __rt_mutex_trylock(&lock->rtmutex);
127 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
131 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
134 * rt_mutex_unlock - unlock a rt_mutex
136 * @lock: the rt_mutex to be unlocked
138 void __sched rt_mutex_unlock(struct rt_mutex *lock)
140 mutex_release(&lock->dep_map, _RET_IP_);
141 __rt_mutex_unlock(&lock->rtmutex);
143 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
146 * Futex variants, must not use fastpath.
148 int __sched rt_mutex_futex_trylock(struct rt_mutex_base *lock)
150 return rt_mutex_slowtrylock(lock);
153 int __sched __rt_mutex_futex_trylock(struct rt_mutex_base *lock)
155 return __rt_mutex_slowtrylock(lock);
159 * __rt_mutex_futex_unlock - Futex variant, that since futex variants
160 * do not use the fast-path, can be simple and will not need to retry.
162 * @lock: The rt_mutex to be unlocked
163 * @wqh: The wake queue head from which to get the next lock waiter
165 bool __sched __rt_mutex_futex_unlock(struct rt_mutex_base *lock,
166 struct rt_wake_q_head *wqh)
168 lockdep_assert_held(&lock->wait_lock);
170 debug_rt_mutex_unlock(lock);
172 if (!rt_mutex_has_waiters(lock)) {
174 return false; /* done */
178 * We've already deboosted, mark_wakeup_next_waiter() will
179 * retain preempt_disabled when we drop the wait_lock, to
180 * avoid inversion prior to the wakeup. preempt_disable()
181 * therein pairs with rt_mutex_postunlock().
183 mark_wakeup_next_waiter(wqh, lock);
185 return true; /* call postunlock() */
188 void __sched rt_mutex_futex_unlock(struct rt_mutex_base *lock)
190 DEFINE_RT_WAKE_Q(wqh);
194 raw_spin_lock_irqsave(&lock->wait_lock, flags);
195 postunlock = __rt_mutex_futex_unlock(lock, &wqh);
196 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
199 rt_mutex_postunlock(&wqh);
203 * __rt_mutex_init - initialize the rt_mutex
205 * @lock: The rt_mutex to be initialized
206 * @name: The lock name used for debugging
207 * @key: The lock class key used for debugging
209 * Initialize the rt_mutex to unlocked state.
211 * Initializing of a locked rt_mutex is not allowed
213 void __sched __rt_mutex_init(struct rt_mutex *lock, const char *name,
214 struct lock_class_key *key)
216 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
217 __rt_mutex_base_init(&lock->rtmutex);
218 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
220 EXPORT_SYMBOL_GPL(__rt_mutex_init);
223 * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
226 * @lock: the rt_mutex to be locked
227 * @proxy_owner:the task to set as owner
229 * No locking. Caller has to do serializing itself
231 * Special API call for PI-futex support. This initializes the rtmutex and
232 * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
233 * possible at this point because the pi_state which contains the rtmutex
234 * is not yet visible to other tasks.
236 void __sched rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
237 struct task_struct *proxy_owner)
239 static struct lock_class_key pi_futex_key;
241 __rt_mutex_base_init(lock);
243 * On PREEMPT_RT the futex hashbucket spinlock becomes 'sleeping'
244 * and rtmutex based. That causes a lockdep false positive, because
245 * some of the futex functions invoke spin_unlock(&hb->lock) with
246 * the wait_lock of the rtmutex associated to the pi_futex held.
247 * spin_unlock() in turn takes wait_lock of the rtmutex on which
248 * the spinlock is based, which makes lockdep notice a lock
249 * recursion. Give the futex/rtmutex wait_lock a separate key.
251 lockdep_set_class(&lock->wait_lock, &pi_futex_key);
252 rt_mutex_set_owner(lock, proxy_owner);
256 * rt_mutex_proxy_unlock - release a lock on behalf of owner
258 * @lock: the rt_mutex to be locked
260 * No locking. Caller has to do serializing itself
262 * Special API call for PI-futex support. This just cleans up the rtmutex
263 * (debugging) state. Concurrent operations on this rt_mutex are not
264 * possible because it belongs to the pi_state which is about to be freed
265 * and it is not longer visible to other tasks.
267 void __sched rt_mutex_proxy_unlock(struct rt_mutex_base *lock)
269 debug_rt_mutex_proxy_unlock(lock);
270 rt_mutex_clear_owner(lock);
274 * __rt_mutex_start_proxy_lock() - Start lock acquisition for another task
275 * @lock: the rt_mutex to take
276 * @waiter: the pre-initialized rt_mutex_waiter
277 * @task: the task to prepare
279 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
280 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
282 * NOTE: does _NOT_ remove the @waiter on failure; must either call
283 * rt_mutex_wait_proxy_lock() or rt_mutex_cleanup_proxy_lock() after this.
286 * 0 - task blocked on lock
287 * 1 - acquired the lock for task, caller should wake it up
290 * Special API call for PI-futex support.
292 int __sched __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
293 struct rt_mutex_waiter *waiter,
294 struct task_struct *task)
298 lockdep_assert_held(&lock->wait_lock);
300 if (try_to_take_rt_mutex(lock, task, NULL))
303 /* We enforce deadlock detection for futexes */
304 ret = task_blocks_on_rt_mutex(lock, waiter, task, NULL,
305 RT_MUTEX_FULL_CHAINWALK);
307 if (ret && !rt_mutex_owner(lock)) {
309 * Reset the return value. We might have
310 * returned with -EDEADLK and the owner
311 * released the lock while we were walking the
312 * pi chain. Let the waiter sort it out.
321 * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
322 * @lock: the rt_mutex to take
323 * @waiter: the pre-initialized rt_mutex_waiter
324 * @task: the task to prepare
326 * Starts the rt_mutex acquire; it enqueues the @waiter and does deadlock
327 * detection. It does not wait, see rt_mutex_wait_proxy_lock() for that.
329 * NOTE: unlike __rt_mutex_start_proxy_lock this _DOES_ remove the @waiter
333 * 0 - task blocked on lock
334 * 1 - acquired the lock for task, caller should wake it up
337 * Special API call for PI-futex support.
339 int __sched rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
340 struct rt_mutex_waiter *waiter,
341 struct task_struct *task)
345 raw_spin_lock_irq(&lock->wait_lock);
346 ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
348 remove_waiter(lock, waiter);
349 raw_spin_unlock_irq(&lock->wait_lock);
355 * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
356 * @lock: the rt_mutex we were woken on
357 * @to: the timeout, null if none. hrtimer should already have
359 * @waiter: the pre-initialized rt_mutex_waiter
361 * Wait for the lock acquisition started on our behalf by
362 * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
363 * rt_mutex_cleanup_proxy_lock().
367 * <0 - error, one of -EINTR, -ETIMEDOUT
369 * Special API call for PI-futex support
371 int __sched rt_mutex_wait_proxy_lock(struct rt_mutex_base *lock,
372 struct hrtimer_sleeper *to,
373 struct rt_mutex_waiter *waiter)
377 raw_spin_lock_irq(&lock->wait_lock);
378 /* sleep on the mutex */
379 set_current_state(TASK_INTERRUPTIBLE);
380 ret = rt_mutex_slowlock_block(lock, NULL, TASK_INTERRUPTIBLE, to, waiter);
382 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
383 * have to fix that up.
385 fixup_rt_mutex_waiters(lock, true);
386 raw_spin_unlock_irq(&lock->wait_lock);
392 * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
393 * @lock: the rt_mutex we were woken on
394 * @waiter: the pre-initialized rt_mutex_waiter
396 * Attempt to clean up after a failed __rt_mutex_start_proxy_lock() or
397 * rt_mutex_wait_proxy_lock().
399 * Unless we acquired the lock; we're still enqueued on the wait-list and can
400 * in fact still be granted ownership until we're removed. Therefore we can
401 * find we are in fact the owner and must disregard the
402 * rt_mutex_wait_proxy_lock() failure.
405 * true - did the cleanup, we done.
406 * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
407 * caller should disregards its return value.
409 * Special API call for PI-futex support
411 bool __sched rt_mutex_cleanup_proxy_lock(struct rt_mutex_base *lock,
412 struct rt_mutex_waiter *waiter)
414 bool cleanup = false;
416 raw_spin_lock_irq(&lock->wait_lock);
418 * Do an unconditional try-lock, this deals with the lock stealing
419 * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
422 * We're not interested in the return value, because the subsequent
423 * test on rt_mutex_owner() will infer that. If the trylock succeeded,
424 * we will own the lock and it will have removed the waiter. If we
425 * failed the trylock, we're still not owner and we need to remove
428 try_to_take_rt_mutex(lock, current, waiter);
430 * Unless we're the owner; we're still enqueued on the wait_list.
431 * So check if we became owner, if not, take us off the wait_list.
433 if (rt_mutex_owner(lock) != current) {
434 remove_waiter(lock, waiter);
438 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
439 * have to fix that up.
441 fixup_rt_mutex_waiters(lock, false);
443 raw_spin_unlock_irq(&lock->wait_lock);
449 * Recheck the pi chain, in case we got a priority setting
451 * Called from sched_setscheduler
453 void __sched rt_mutex_adjust_pi(struct task_struct *task)
455 struct rt_mutex_waiter *waiter;
456 struct rt_mutex_base *next_lock;
459 raw_spin_lock_irqsave(&task->pi_lock, flags);
461 waiter = task->pi_blocked_on;
462 if (!waiter || rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
463 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
466 next_lock = waiter->lock;
467 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
469 /* gets dropped in rt_mutex_adjust_prio_chain()! */
470 get_task_struct(task);
472 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
473 next_lock, NULL, task);
477 * Performs the wakeup of the top-waiter and re-enables preemption.
479 void __sched rt_mutex_postunlock(struct rt_wake_q_head *wqh)
481 rt_mutex_wake_up_q(wqh);
484 #ifdef CONFIG_DEBUG_RT_MUTEXES
485 void rt_mutex_debug_task_free(struct task_struct *task)
487 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
488 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
492 #ifdef CONFIG_PREEMPT_RT
494 void __mutex_rt_init(struct mutex *mutex, const char *name,
495 struct lock_class_key *key)
497 debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
498 lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP);
500 EXPORT_SYMBOL(__mutex_rt_init);
502 static __always_inline int __mutex_lock_common(struct mutex *lock,
504 unsigned int subclass,
505 struct lockdep_map *nest_lock,
511 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
512 ret = __rt_mutex_lock(&lock->rtmutex, state);
514 mutex_release(&lock->dep_map, ip);
516 lock_acquired(&lock->dep_map, ip);
520 #ifdef CONFIG_DEBUG_LOCK_ALLOC
521 void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass)
523 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
525 EXPORT_SYMBOL_GPL(mutex_lock_nested);
527 void __sched _mutex_lock_nest_lock(struct mutex *lock,
528 struct lockdep_map *nest_lock)
530 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest_lock, _RET_IP_);
532 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
534 int __sched mutex_lock_interruptible_nested(struct mutex *lock,
535 unsigned int subclass)
537 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
539 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
541 int __sched mutex_lock_killable_nested(struct mutex *lock,
542 unsigned int subclass)
544 return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
546 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
548 void __sched mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
554 token = io_schedule_prepare();
555 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
556 io_schedule_finish(token);
558 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
560 #else /* CONFIG_DEBUG_LOCK_ALLOC */
562 void __sched mutex_lock(struct mutex *lock)
564 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
566 EXPORT_SYMBOL(mutex_lock);
568 int __sched mutex_lock_interruptible(struct mutex *lock)
570 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
572 EXPORT_SYMBOL(mutex_lock_interruptible);
574 int __sched mutex_lock_killable(struct mutex *lock)
576 return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
578 EXPORT_SYMBOL(mutex_lock_killable);
580 void __sched mutex_lock_io(struct mutex *lock)
582 int token = io_schedule_prepare();
584 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
585 io_schedule_finish(token);
587 EXPORT_SYMBOL(mutex_lock_io);
588 #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
590 int __sched mutex_trylock(struct mutex *lock)
594 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES) && WARN_ON_ONCE(!in_task()))
597 ret = __rt_mutex_trylock(&lock->rtmutex);
599 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
603 EXPORT_SYMBOL(mutex_trylock);
605 void __sched mutex_unlock(struct mutex *lock)
607 mutex_release(&lock->dep_map, _RET_IP_);
608 __rt_mutex_unlock(&lock->rtmutex);
610 EXPORT_SYMBOL(mutex_unlock);
612 #endif /* CONFIG_PREEMPT_RT */