14 #include <linux/export.h>
15 #include <linux/sched.h>
50 unsigned long val = (
unsigned long)owner;
52 if (rt_mutex_has_waiters(lock))
58 static inline void clear_rt_mutex_waiters(
struct rt_mutex *lock)
64 static void fixup_rt_mutex_waiters(
struct rt_mutex *lock)
66 if (!rt_mutex_has_waiters(lock))
67 clear_rt_mutex_waiters(lock);
74 #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
75 # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
76 static inline void mark_rt_mutex_waiters(
struct rt_mutex *lock)
78 unsigned long owner, *
p = (
unsigned long *) &lock->
owner;
85 # define rt_mutex_cmpxchg(l,c,n) (0)
86 static inline void mark_rt_mutex_waiters(
struct rt_mutex *lock)
101 if (
likely(!task_has_pi_waiters(task)))
104 return min(task_top_pi_waiter(task)->pi_list_entry.prio,
117 if (task->
prio != prio)
118 rt_mutex_setprio(task, prio);
130 static void rt_mutex_adjust_prio(
struct task_struct *task)
135 __rt_mutex_adjust_prio(task);
149 static int rt_mutex_adjust_prio_chain(
struct task_struct *task,
157 int detect_deadlock,
ret = 0,
depth = 0;
170 if (++
depth > max_lock_depth) {
177 if (prev_max != max_lock_depth) {
180 "task: %s (%d)\n", max_lock_depth,
181 top_task->
comm, task_pid_nr(top_task));
183 put_task_struct(task);
185 return deadlock_detect ? -
EDEADLK : 0;
193 waiter = task->pi_blocked_on;
206 if (orig_waiter && !rt_mutex_owner(orig_lock))
214 if (top_waiter && (!task_has_pi_waiters(task) ||
215 top_waiter != task_top_pi_waiter(task)))
233 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
236 ret = deadlock_detect ? -
EDEADLK : 0;
240 top_waiter = rt_mutex_top_waiter(lock);
249 if (!rt_mutex_owner(lock)) {
255 if (top_waiter != rt_mutex_top_waiter(lock))
260 put_task_struct(task);
263 task = rt_mutex_owner(lock);
267 if (waiter == rt_mutex_top_waiter(lock)) {
272 __rt_mutex_adjust_prio(task);
274 }
else if (top_waiter == waiter) {
277 waiter = rt_mutex_top_waiter(lock);
280 __rt_mutex_adjust_prio(task);
285 top_waiter = rt_mutex_top_waiter(lock);
288 if (!detect_deadlock && waiter != top_waiter)
296 put_task_struct(task);
332 mark_rt_mutex_waiters(lock);
334 if (rt_mutex_owner(lock))
343 if (rt_mutex_has_waiters(lock)) {
345 if (!waiter || waiter != rt_mutex_top_waiter(lock))
350 if (waiter || rt_mutex_has_waiters(lock)) {
359 task->pi_blocked_on =
NULL;
366 if (rt_mutex_has_waiters(lock)) {
367 top = rt_mutex_top_waiter(lock);
377 rt_mutex_set_owner(lock, task);
391 static int task_blocks_on_rt_mutex(
struct rt_mutex *lock,
399 int chain_walk = 0,
res;
402 __rt_mutex_adjust_prio(task);
409 if (rt_mutex_has_waiters(lock))
410 top_waiter = rt_mutex_top_waiter(lock);
413 task->pi_blocked_on = waiter;
420 if (waiter == rt_mutex_top_waiter(lock)) {
425 __rt_mutex_adjust_prio(owner);
426 if (owner->pi_blocked_on)
445 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
460 static void wakeup_next_waiter(
struct rt_mutex *lock)
467 waiter = rt_mutex_top_waiter(lock);
477 rt_mutex_set_owner(lock,
NULL);
490 static void remove_waiter(
struct rt_mutex *lock,
493 int first = (waiter == rt_mutex_top_waiter(lock));
512 if (rt_mutex_has_waiters(lock)) {
515 next = rt_mutex_top_waiter(lock);
518 __rt_mutex_adjust_prio(owner);
520 if (owner->pi_blocked_on)
536 rt_mutex_adjust_prio_chain(owner, 0, lock,
NULL,
current);
553 waiter = task->pi_blocked_on;
563 rt_mutex_adjust_prio_chain(task, 0,
NULL,
NULL, task);
585 if (try_to_take_rt_mutex(lock,
current, waiter))
596 if (timeout && !timeout->
task)
619 rt_mutex_slowlock(
struct rt_mutex *lock,
int state,
641 if (!hrtimer_active(&timeout->
timer))
645 ret = task_blocks_on_rt_mutex(lock, &waiter,
current, detect_deadlock);
648 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
653 remove_waiter(lock, &waiter);
659 fixup_rt_mutex_waiters(lock);
676 rt_mutex_slowtrylock(
struct rt_mutex *lock)
689 fixup_rt_mutex_waiters(lock);
701 rt_mutex_slowunlock(
struct rt_mutex *lock)
709 if (!rt_mutex_has_waiters(lock)) {
715 wakeup_next_waiter(lock);
730 rt_mutex_fastlock(
struct rt_mutex *lock,
int state,
732 int (*slowfn)(
struct rt_mutex *lock,
int state,
734 int detect_deadlock))
740 return slowfn(lock, state,
NULL, detect_deadlock);
744 rt_mutex_timed_fastlock(
struct rt_mutex *lock,
int state,
746 int (*slowfn)(
struct rt_mutex *lock,
int state,
748 int detect_deadlock))
754 return slowfn(lock, state, timeout, detect_deadlock);
758 rt_mutex_fasttrylock(
struct rt_mutex *lock,
759 int (*slowfn)(
struct rt_mutex *lock))
769 rt_mutex_fastunlock(
struct rt_mutex *lock,
770 void (*slowfn)(
struct rt_mutex *lock))
808 detect_deadlock, rt_mutex_slowlock);
834 detect_deadlock, rt_mutex_slowlock);
847 return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
858 rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
872 WARN_ON(rt_mutex_is_locked(lock));
873 #ifdef CONFIG_DEBUG_RT_MUTEXES
914 rt_mutex_set_owner(lock, proxy_owner);
930 rt_mutex_set_owner(lock,
NULL);
956 if (try_to_take_rt_mutex(lock, task,
NULL)) {
961 ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
963 if (ret && !rt_mutex_owner(lock)) {
974 remove_waiter(lock, waiter);
997 if (!rt_mutex_has_waiters(lock))
1000 return rt_mutex_top_waiter(lock)->task;
1022 int detect_deadlock)
1035 remove_waiter(lock, waiter);
1041 fixup_rt_mutex_waiters(lock);