1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * RT Mutexes: blocking mutual exclusion locks with PI support
4  *
5  * started by Ingo Molnar and Thomas Gleixner:
6  *
7  *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8  *  Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9  *
10  * This file contains the private data structure and API definitions.
11  */
12 
13 #ifndef __KERNEL_RTMUTEX_COMMON_H
14 #define __KERNEL_RTMUTEX_COMMON_H
15 
16 #include <linux/debug_locks.h>
17 #include <linux/rtmutex.h>
18 #include <linux/sched/wake_q.h>
19 
20 /*
21  * This is the control structure for tasks blocked on a rt_mutex,
22  * which is allocated on the kernel stack on of the blocked task.
23  *
24  * @tree_entry:		pi node to enqueue into the mutex waiters tree
25  * @pi_tree_entry:	pi node to enqueue into the mutex owner waiters tree
26  * @task:		task reference to the blocked task
27  * @lock:		Pointer to the rt_mutex on which the waiter blocks
28  * @wake_state:		Wakeup state to use (TASK_NORMAL or TASK_RTLOCK_WAIT)
29  * @prio:		Priority of the waiter
30  * @deadline:		Deadline of the waiter if applicable
31  * @ww_ctx:		WW context pointer
32  */
33 struct rt_mutex_waiter {
34 	struct rb_node		tree_entry;
35 	struct rb_node		pi_tree_entry;
36 	struct task_struct	*task;
37 	struct rt_mutex_base	*lock;
38 	unsigned int		wake_state;
39 	int			prio;
40 	u64			deadline;
41 	struct ww_acquire_ctx	*ww_ctx;
42 };
43 
44 /**
45  * rt_wake_q_head - Wrapper around regular wake_q_head to support
46  *		    "sleeping" spinlocks on RT
47  * @head:		The regular wake_q_head for sleeping lock variants
48  * @rtlock_task:	Task pointer for RT lock (spin/rwlock) wakeups
49  */
50 struct rt_wake_q_head {
51 	struct wake_q_head	head;
52 	struct task_struct	*rtlock_task;
53 };
54 
55 #define DEFINE_RT_WAKE_Q(name)						\
56 	struct rt_wake_q_head name = {					\
57 		.head		= WAKE_Q_HEAD_INITIALIZER(name.head),	\
58 		.rtlock_task	= NULL,					\
59 	}
60 
61 /*
62  * PI-futex support (proxy locking functions, etc.):
63  */
64 extern void rt_mutex_init_proxy_locked(struct rt_mutex_base *lock,
65 				       struct task_struct *proxy_owner);
66 extern void rt_mutex_proxy_unlock(struct rt_mutex_base *lock);
67 extern int __rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
68 				     struct rt_mutex_waiter *waiter,
69 				     struct task_struct *task);
70 extern int rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
71 				     struct rt_mutex_waiter *waiter,
72 				     struct task_struct *task);
73 extern int rt_mutex_wait_proxy_lock(struct rt_mutex_base *lock,
74 			       struct hrtimer_sleeper *to,
75 			       struct rt_mutex_waiter *waiter);
76 extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex_base *lock,
77 				 struct rt_mutex_waiter *waiter);
78 
79 extern int rt_mutex_futex_trylock(struct rt_mutex_base *l);
80 extern int __rt_mutex_futex_trylock(struct rt_mutex_base *l);
81 
82 extern void rt_mutex_futex_unlock(struct rt_mutex_base *lock);
83 extern bool __rt_mutex_futex_unlock(struct rt_mutex_base *lock,
84 				struct rt_wake_q_head *wqh);
85 
86 extern void rt_mutex_postunlock(struct rt_wake_q_head *wqh);
87 
88 /*
89  * Must be guarded because this header is included from rcu/tree_plugin.h
90  * unconditionally.
91  */
92 #ifdef CONFIG_RT_MUTEXES
93 static inline int rt_mutex_has_waiters(struct rt_mutex_base *lock)
94 {
95 	return !RB_EMPTY_ROOT(&lock->waiters.rb_root);
96 }
97 
98 /*
99  * Lockless speculative check whether @waiter is still the top waiter on
100  * @lock. This is solely comparing pointers and not derefencing the
101  * leftmost entry which might be about to vanish.
102  */
103 static inline bool rt_mutex_waiter_is_top_waiter(struct rt_mutex_base *lock,
104 						 struct rt_mutex_waiter *waiter)
105 {
106 	struct rb_node *leftmost = rb_first_cached(&lock->waiters);
107 
108 	return rb_entry(leftmost, struct rt_mutex_waiter, tree_entry) == waiter;
109 }
110 
111 static inline struct rt_mutex_waiter *rt_mutex_top_waiter(struct rt_mutex_base *lock)
112 {
113 	struct rb_node *leftmost = rb_first_cached(&lock->waiters);
114 	struct rt_mutex_waiter *w = NULL;
115 
116 	if (leftmost) {
117 		w = rb_entry(leftmost, struct rt_mutex_waiter, tree_entry);
118 		BUG_ON(w->lock != lock);
119 	}
120 	return w;
121 }
122 
123 static inline int task_has_pi_waiters(struct task_struct *p)
124 {
125 	return !RB_EMPTY_ROOT(&p->pi_waiters.rb_root);
126 }
127 
128 static inline struct rt_mutex_waiter *task_top_pi_waiter(struct task_struct *p)
129 {
130 	return rb_entry(p->pi_waiters.rb_leftmost, struct rt_mutex_waiter,
131 			pi_tree_entry);
132 }
133 
134 #define RT_MUTEX_HAS_WAITERS	1UL
135 
136 static inline struct task_struct *rt_mutex_owner(struct rt_mutex_base *lock)
137 {
138 	unsigned long owner = (unsigned long) READ_ONCE(lock->owner);
139 
140 	return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS);
141 }
142 
143 /*
144  * Constants for rt mutex functions which have a selectable deadlock
145  * detection.
146  *
147  * RT_MUTEX_MIN_CHAINWALK:	Stops the lock chain walk when there are
148  *				no further PI adjustments to be made.
149  *
150  * RT_MUTEX_FULL_CHAINWALK:	Invoke deadlock detection with a full
151  *				walk of the lock chain.
152  */
153 enum rtmutex_chainwalk {
154 	RT_MUTEX_MIN_CHAINWALK,
155 	RT_MUTEX_FULL_CHAINWALK,
156 };
157 
158 static inline void __rt_mutex_base_init(struct rt_mutex_base *lock)
159 {
160 	raw_spin_lock_init(&lock->wait_lock);
161 	lock->waiters = RB_ROOT_CACHED;
162 	lock->owner = NULL;
163 }
164 
165 /* Debug functions */
166 static inline void debug_rt_mutex_unlock(struct rt_mutex_base *lock)
167 {
168 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
169 		DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
170 }
171 
172 static inline void debug_rt_mutex_proxy_unlock(struct rt_mutex_base *lock)
173 {
174 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
175 		DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
176 }
177 
178 static inline void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
179 {
180 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
181 		memset(waiter, 0x11, sizeof(*waiter));
182 }
183 
184 static inline void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
185 {
186 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
187 		memset(waiter, 0x22, sizeof(*waiter));
188 }
189 
190 static inline void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
191 {
192 	debug_rt_mutex_init_waiter(waiter);
193 	RB_CLEAR_NODE(&waiter->pi_tree_entry);
194 	RB_CLEAR_NODE(&waiter->tree_entry);
195 	waiter->wake_state = TASK_NORMAL;
196 	waiter->task = NULL;
197 }
198 
199 static inline void rt_mutex_init_rtlock_waiter(struct rt_mutex_waiter *waiter)
200 {
201 	rt_mutex_init_waiter(waiter);
202 	waiter->wake_state = TASK_RTLOCK_WAIT;
203 }
204 
205 #else /* CONFIG_RT_MUTEXES */
206 /* Used in rcu/tree_plugin.h */
207 static inline struct task_struct *rt_mutex_owner(struct rt_mutex_base *lock)
208 {
209 	return NULL;
210 }
211 #endif  /* !CONFIG_RT_MUTEXES */
212 
213 #endif
214