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 <[email protected]>
8  *  Copyright (C) 2006, Timesys Corp., Thomas Gleixner <[email protected]>
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/rtmutex.h>
17 #include <linux/sched/wake_q.h>
18 
19 /*
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.
22  *
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
29  */
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;
35 	int			prio;
36 	u64			deadline;
37 };
38 
39 /*
40  * Must be guarded because this header is included from rcu/tree_plugin.h
41  * unconditionally.
42  */
43 #ifdef CONFIG_RT_MUTEXES
44 static inline int rt_mutex_has_waiters(struct rt_mutex *lock)
45 {
46 	return !RB_EMPTY_ROOT(&lock->waiters.rb_root);
47 }
48 
49 static inline struct rt_mutex_waiter *rt_mutex_top_waiter(struct rt_mutex *lock)
50 {
51 	struct rb_node *leftmost = rb_first_cached(&lock->waiters);
52 	struct rt_mutex_waiter *w = NULL;
53 
54 	if (leftmost) {
55 		w = rb_entry(leftmost, struct rt_mutex_waiter, tree_entry);
56 		BUG_ON(w->lock != lock);
57 	}
58 	return w;
59 }
60 
61 static inline int task_has_pi_waiters(struct task_struct *p)
62 {
63 	return !RB_EMPTY_ROOT(&p->pi_waiters.rb_root);
64 }
65 
66 static inline struct rt_mutex_waiter *task_top_pi_waiter(struct task_struct *p)
67 {
68 	return rb_entry(p->pi_waiters.rb_leftmost, struct rt_mutex_waiter,
69 			pi_tree_entry);
70 }
71 
72 #define RT_MUTEX_HAS_WAITERS	1UL
73 
74 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock)
75 {
76 	unsigned long owner = (unsigned long) READ_ONCE(lock->owner);
77 
78 	return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS);
79 }
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)
83 {
84 	return NULL;
85 }
86 #endif  /* !CONFIG_RT_MUTEXES */
87 
88 /*
89  * Constants for rt mutex functions which have a selectable deadlock
90  * detection.
91  *
92  * RT_MUTEX_MIN_CHAINWALK:	Stops the lock chain walk when there are
93  *				no further PI adjustments to be made.
94  *
95  * RT_MUTEX_FULL_CHAINWALK:	Invoke deadlock detection with a full
96  *				walk of the lock chain.
97  */
98 enum rtmutex_chainwalk {
99 	RT_MUTEX_MIN_CHAINWALK,
100 	RT_MUTEX_FULL_CHAINWALK,
101 };
102 
103 static inline void __rt_mutex_basic_init(struct rt_mutex *lock)
104 {
105 	lock->owner = NULL;
106 	raw_spin_lock_init(&lock->wait_lock);
107 	lock->waiters = RB_ROOT_CACHED;
108 }
109 
110 /*
111  * PI-futex support (proxy locking functions, etc.):
112  */
113 extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
114 				       struct task_struct *proxy_owner);
115 extern void rt_mutex_proxy_unlock(struct rt_mutex *lock);
116 extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter);
117 extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
118 				     struct rt_mutex_waiter *waiter,
119 				     struct task_struct *task);
120 extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
121 				     struct rt_mutex_waiter *waiter,
122 				     struct task_struct *task);
123 extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
124 			       struct hrtimer_sleeper *to,
125 			       struct rt_mutex_waiter *waiter);
126 extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
127 				 struct rt_mutex_waiter *waiter);
128 
129 extern int rt_mutex_futex_trylock(struct rt_mutex *l);
130 extern int __rt_mutex_futex_trylock(struct rt_mutex *l);
131 
132 extern void rt_mutex_futex_unlock(struct rt_mutex *lock);
133 extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock,
134 				 struct wake_q_head *wqh);
135 
136 extern void rt_mutex_postunlock(struct wake_q_head *wake_q);
137 
138 #ifdef CONFIG_DEBUG_RT_MUTEXES
139 # include "rtmutex-debug.h"
140 #else
141 # include "rtmutex.h"
142 #endif
143 
144 #endif
145