1 /* SPDX-License-Identifier: GPL-2.0 */
3 * RT Mutexes: blocking mutual exclusion locks with PI support
5 * started by Ingo Molnar and Thomas Gleixner:
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
10 * This file contains the private data structure and API definitions.
13 #ifndef __KERNEL_RTMUTEX_COMMON_H
14 #define __KERNEL_RTMUTEX_COMMON_H
16 #include <linux/rtmutex.h>
17 #include <linux/sched/wake_q.h>
20 * This is the control structure for tasks blocked on a rt_mutex,
21 * which is allocated on the kernel stack on of the blocked task.
23 * @tree_entry: pi node to enqueue into the mutex waiters tree
24 * @pi_tree_entry: pi node to enqueue into the mutex owner waiters tree
25 * @task: task reference to the blocked task
26 * @lock: Pointer to the rt_mutex on which the waiter blocks
27 * @prio: Priority of the waiter
28 * @deadline: Deadline of the waiter if applicable
30 struct rt_mutex_waiter {
31 struct rb_node tree_entry;
32 struct rb_node pi_tree_entry;
33 struct task_struct *task;
34 struct rt_mutex *lock;
40 * Must be guarded because this header is included from rcu/tree_plugin.h
43 #ifdef CONFIG_RT_MUTEXES
44 static inline int rt_mutex_has_waiters(struct rt_mutex *lock)
46 return !RB_EMPTY_ROOT(&lock->waiters.rb_root);
49 static inline struct rt_mutex_waiter *rt_mutex_top_waiter(struct rt_mutex *lock)
51 struct rb_node *leftmost = rb_first_cached(&lock->waiters);
52 struct rt_mutex_waiter *w = NULL;
55 w = rb_entry(leftmost, struct rt_mutex_waiter, tree_entry);
56 BUG_ON(w->lock != lock);
61 static inline int task_has_pi_waiters(struct task_struct *p)
63 return !RB_EMPTY_ROOT(&p->pi_waiters.rb_root);
66 static inline struct rt_mutex_waiter *task_top_pi_waiter(struct task_struct *p)
68 return rb_entry(p->pi_waiters.rb_leftmost, struct rt_mutex_waiter,
72 #define RT_MUTEX_HAS_WAITERS 1UL
74 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock)
76 unsigned long owner = (unsigned long) READ_ONCE(lock->owner);
78 return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS);
80 #else /* CONFIG_RT_MUTEXES */
81 /* Used in rcu/tree_plugin.h */
82 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock)
86 #endif /* !CONFIG_RT_MUTEXES */
89 * Constants for rt mutex functions which have a selectable deadlock
92 * RT_MUTEX_MIN_CHAINWALK: Stops the lock chain walk when there are
93 * no further PI adjustments to be made.
95 * RT_MUTEX_FULL_CHAINWALK: Invoke deadlock detection with a full
96 * walk of the lock chain.
98 enum rtmutex_chainwalk {
99 RT_MUTEX_MIN_CHAINWALK,
100 RT_MUTEX_FULL_CHAINWALK,
104 * PI-futex support (proxy locking functions, etc.):
106 extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
107 struct task_struct *proxy_owner);
108 extern void rt_mutex_proxy_unlock(struct rt_mutex *lock);
109 extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter);
110 extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
111 struct rt_mutex_waiter *waiter,
112 struct task_struct *task);
113 extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
114 struct rt_mutex_waiter *waiter,
115 struct task_struct *task);
116 extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
117 struct hrtimer_sleeper *to,
118 struct rt_mutex_waiter *waiter);
119 extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
120 struct rt_mutex_waiter *waiter);
122 extern int rt_mutex_futex_trylock(struct rt_mutex *l);
123 extern int __rt_mutex_futex_trylock(struct rt_mutex *l);
125 extern void rt_mutex_futex_unlock(struct rt_mutex *lock);
126 extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock,
127 struct wake_q_head *wqh);
129 extern void rt_mutex_postunlock(struct wake_q_head *wake_q);
131 #ifdef CONFIG_DEBUG_RT_MUTEXES
132 # include "rtmutex-debug.h"
134 # include "rtmutex.h"