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 */ 27 struct rt_mutex_waiter { 28 struct rb_node tree_entry; 29 struct rb_node pi_tree_entry; 30 struct task_struct *task; 31 struct rt_mutex *lock; 32 int prio; 33 u64 deadline; 34 }; 35 36 /* 37 * Various helpers to access the waiters-tree: 38 */ 39 40 #ifdef CONFIG_RT_MUTEXES 41 42 static inline int rt_mutex_has_waiters(struct rt_mutex *lock) 43 { 44 return !RB_EMPTY_ROOT(&lock->waiters.rb_root); 45 } 46 47 static inline struct rt_mutex_waiter * 48 rt_mutex_top_waiter(struct rt_mutex *lock) 49 { 50 struct rb_node *leftmost = rb_first_cached(&lock->waiters); 51 struct rt_mutex_waiter *w = NULL; 52 53 if (leftmost) { 54 w = rb_entry(leftmost, struct rt_mutex_waiter, tree_entry); 55 BUG_ON(w->lock != lock); 56 } 57 return w; 58 } 59 60 static inline int task_has_pi_waiters(struct task_struct *p) 61 { 62 return !RB_EMPTY_ROOT(&p->pi_waiters.rb_root); 63 } 64 65 static inline struct rt_mutex_waiter * 66 task_top_pi_waiter(struct task_struct *p) 67 { 68 return rb_entry(p->pi_waiters.rb_leftmost, 69 struct rt_mutex_waiter, pi_tree_entry); 70 } 71 72 #else 73 74 static inline int rt_mutex_has_waiters(struct rt_mutex *lock) 75 { 76 return false; 77 } 78 79 static inline struct rt_mutex_waiter * 80 rt_mutex_top_waiter(struct rt_mutex *lock) 81 { 82 return NULL; 83 } 84 85 static inline int task_has_pi_waiters(struct task_struct *p) 86 { 87 return false; 88 } 89 90 static inline struct rt_mutex_waiter * 91 task_top_pi_waiter(struct task_struct *p) 92 { 93 return NULL; 94 } 95 96 #endif 97 98 /* 99 * lock->owner state tracking: 100 */ 101 #define RT_MUTEX_HAS_WAITERS 1UL 102 103 static inline struct task_struct *rt_mutex_owner(struct rt_mutex *lock) 104 { 105 unsigned long owner = (unsigned long) READ_ONCE(lock->owner); 106 107 return (struct task_struct *) (owner & ~RT_MUTEX_HAS_WAITERS); 108 } 109 110 /* 111 * Constants for rt mutex functions which have a selectable deadlock 112 * detection. 113 * 114 * RT_MUTEX_MIN_CHAINWALK: Stops the lock chain walk when there are 115 * no further PI adjustments to be made. 116 * 117 * RT_MUTEX_FULL_CHAINWALK: Invoke deadlock detection with a full 118 * walk of the lock chain. 119 */ 120 enum rtmutex_chainwalk { 121 RT_MUTEX_MIN_CHAINWALK, 122 RT_MUTEX_FULL_CHAINWALK, 123 }; 124 125 /* 126 * PI-futex support (proxy locking functions, etc.): 127 */ 128 extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock, 129 struct task_struct *proxy_owner); 130 extern void rt_mutex_proxy_unlock(struct rt_mutex *lock); 131 extern void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter); 132 extern int __rt_mutex_start_proxy_lock(struct rt_mutex *lock, 133 struct rt_mutex_waiter *waiter, 134 struct task_struct *task); 135 extern int rt_mutex_start_proxy_lock(struct rt_mutex *lock, 136 struct rt_mutex_waiter *waiter, 137 struct task_struct *task); 138 extern int rt_mutex_wait_proxy_lock(struct rt_mutex *lock, 139 struct hrtimer_sleeper *to, 140 struct rt_mutex_waiter *waiter); 141 extern bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock, 142 struct rt_mutex_waiter *waiter); 143 144 extern int rt_mutex_futex_trylock(struct rt_mutex *l); 145 extern int __rt_mutex_futex_trylock(struct rt_mutex *l); 146 147 extern void rt_mutex_futex_unlock(struct rt_mutex *lock); 148 extern bool __rt_mutex_futex_unlock(struct rt_mutex *lock, 149 struct wake_q_head *wqh); 150 151 extern void rt_mutex_postunlock(struct wake_q_head *wake_q); 152 153 #ifdef CONFIG_DEBUG_RT_MUTEXES 154 # include "rtmutex-debug.h" 155 #else 156 # include "rtmutex.h" 157 #endif 158 159 #endif 160