rtmutex_common.h 4.2 KB

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