rtmutex_common.h 6.7 KB

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