futex.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _FUTEX_H
  3. #define _FUTEX_H
  4. #include <linux/futex.h>
  5. #include <linux/rtmutex.h>
  6. #include <linux/sched/wake_q.h>
  7. #ifdef CONFIG_PREEMPT_RT
  8. #include <linux/rcuwait.h>
  9. #endif
  10. #include <asm/futex.h>
  11. /*
  12. * Futex flags used to encode options to functions and preserve them across
  13. * restarts.
  14. */
  15. #ifdef CONFIG_MMU
  16. # define FLAGS_SHARED 0x01
  17. #else
  18. /*
  19. * NOMMU does not have per process address space. Let the compiler optimize
  20. * code away.
  21. */
  22. # define FLAGS_SHARED 0x00
  23. #endif
  24. #define FLAGS_CLOCKRT 0x02
  25. #define FLAGS_HAS_TIMEOUT 0x04
  26. #ifdef CONFIG_FAIL_FUTEX
  27. extern bool should_fail_futex(bool fshared);
  28. #else
  29. static inline bool should_fail_futex(bool fshared)
  30. {
  31. return false;
  32. }
  33. #endif
  34. /*
  35. * Hash buckets are shared by all the futex_keys that hash to the same
  36. * location. Each key may have multiple futex_q structures, one for each task
  37. * waiting on a futex.
  38. */
  39. struct futex_hash_bucket {
  40. atomic_t waiters;
  41. spinlock_t lock;
  42. struct plist_head chain;
  43. } ____cacheline_aligned_in_smp;
  44. /*
  45. * Priority Inheritance state:
  46. */
  47. struct futex_pi_state {
  48. /*
  49. * list of 'owned' pi_state instances - these have to be
  50. * cleaned up in do_exit() if the task exits prematurely:
  51. */
  52. struct list_head list;
  53. /*
  54. * The PI object:
  55. */
  56. struct rt_mutex_base pi_mutex;
  57. struct task_struct *owner;
  58. refcount_t refcount;
  59. union futex_key key;
  60. } __randomize_layout;
  61. /**
  62. * struct futex_q - The hashed futex queue entry, one per waiting task
  63. * @list: priority-sorted list of tasks waiting on this futex
  64. * @task: the task waiting on the futex
  65. * @lock_ptr: the hash bucket lock
  66. * @key: the key the futex is hashed on
  67. * @pi_state: optional priority inheritance state
  68. * @rt_waiter: rt_waiter storage for use with requeue_pi
  69. * @requeue_pi_key: the requeue_pi target futex key
  70. * @bitset: bitset for the optional bitmasked wakeup
  71. * @requeue_state: State field for futex_requeue_pi()
  72. * @requeue_wait: RCU wait for futex_requeue_pi() (RT only)
  73. *
  74. * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so
  75. * we can wake only the relevant ones (hashed queues may be shared).
  76. *
  77. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  78. * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
  79. * The order of wakeup is always to make the first condition true, then
  80. * the second.
  81. *
  82. * PI futexes are typically woken before they are removed from the hash list via
  83. * the rt_mutex code. See futex_unqueue_pi().
  84. */
  85. struct futex_q {
  86. struct plist_node list;
  87. struct task_struct *task;
  88. spinlock_t *lock_ptr;
  89. union futex_key key;
  90. struct futex_pi_state *pi_state;
  91. struct rt_mutex_waiter *rt_waiter;
  92. union futex_key *requeue_pi_key;
  93. u32 bitset;
  94. atomic_t requeue_state;
  95. #ifdef CONFIG_PREEMPT_RT
  96. struct rcuwait requeue_wait;
  97. #endif
  98. } __randomize_layout;
  99. extern const struct futex_q futex_q_init;
  100. enum futex_access {
  101. FUTEX_READ,
  102. FUTEX_WRITE
  103. };
  104. extern int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
  105. enum futex_access rw);
  106. extern struct hrtimer_sleeper *
  107. futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
  108. int flags, u64 range_ns);
  109. extern struct futex_hash_bucket *futex_hash(union futex_key *key);
  110. /**
  111. * futex_match - Check whether two futex keys are equal
  112. * @key1: Pointer to key1
  113. * @key2: Pointer to key2
  114. *
  115. * Return 1 if two futex_keys are equal, 0 otherwise.
  116. */
  117. static inline int futex_match(union futex_key *key1, union futex_key *key2)
  118. {
  119. return (key1 && key2
  120. && key1->both.word == key2->both.word
  121. && key1->both.ptr == key2->both.ptr
  122. && key1->both.offset == key2->both.offset);
  123. }
  124. extern int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
  125. struct futex_q *q, struct futex_hash_bucket **hb);
  126. extern void futex_wait_queue(struct futex_hash_bucket *hb, struct futex_q *q,
  127. struct hrtimer_sleeper *timeout);
  128. extern void futex_wake_mark(struct wake_q_head *wake_q, struct futex_q *q);
  129. extern int fault_in_user_writeable(u32 __user *uaddr);
  130. extern int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 uval, u32 newval);
  131. extern int futex_get_value_locked(u32 *dest, u32 __user *from);
  132. extern struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, union futex_key *key);
  133. extern void __futex_unqueue(struct futex_q *q);
  134. extern void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb);
  135. extern int futex_unqueue(struct futex_q *q);
  136. /**
  137. * futex_queue() - Enqueue the futex_q on the futex_hash_bucket
  138. * @q: The futex_q to enqueue
  139. * @hb: The destination hash bucket
  140. *
  141. * The hb->lock must be held by the caller, and is released here. A call to
  142. * futex_queue() is typically paired with exactly one call to futex_unqueue(). The
  143. * exceptions involve the PI related operations, which may use futex_unqueue_pi()
  144. * or nothing if the unqueue is done as part of the wake process and the unqueue
  145. * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
  146. * an example).
  147. */
  148. static inline void futex_queue(struct futex_q *q, struct futex_hash_bucket *hb)
  149. __releases(&hb->lock)
  150. {
  151. __futex_queue(q, hb);
  152. spin_unlock(&hb->lock);
  153. }
  154. extern void futex_unqueue_pi(struct futex_q *q);
  155. extern void wait_for_owner_exiting(int ret, struct task_struct *exiting);
  156. /*
  157. * Reflects a new waiter being added to the waitqueue.
  158. */
  159. static inline void futex_hb_waiters_inc(struct futex_hash_bucket *hb)
  160. {
  161. #ifdef CONFIG_SMP
  162. atomic_inc(&hb->waiters);
  163. /*
  164. * Full barrier (A), see the ordering comment above.
  165. */
  166. smp_mb__after_atomic();
  167. #endif
  168. }
  169. /*
  170. * Reflects a waiter being removed from the waitqueue by wakeup
  171. * paths.
  172. */
  173. static inline void futex_hb_waiters_dec(struct futex_hash_bucket *hb)
  174. {
  175. #ifdef CONFIG_SMP
  176. atomic_dec(&hb->waiters);
  177. #endif
  178. }
  179. static inline int futex_hb_waiters_pending(struct futex_hash_bucket *hb)
  180. {
  181. #ifdef CONFIG_SMP
  182. /*
  183. * Full barrier (B), see the ordering comment above.
  184. */
  185. smp_mb();
  186. return atomic_read(&hb->waiters);
  187. #else
  188. return 1;
  189. #endif
  190. }
  191. extern struct futex_hash_bucket *futex_q_lock(struct futex_q *q);
  192. extern void futex_q_unlock(struct futex_hash_bucket *hb);
  193. extern int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
  194. union futex_key *key,
  195. struct futex_pi_state **ps,
  196. struct task_struct *task,
  197. struct task_struct **exiting,
  198. int set_waiters);
  199. extern int refill_pi_state_cache(void);
  200. extern void get_pi_state(struct futex_pi_state *pi_state);
  201. extern void put_pi_state(struct futex_pi_state *pi_state);
  202. extern int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked);
  203. /*
  204. * Express the locking dependencies for lockdep:
  205. */
  206. static inline void
  207. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  208. {
  209. if (hb1 > hb2)
  210. swap(hb1, hb2);
  211. spin_lock(&hb1->lock);
  212. if (hb1 != hb2)
  213. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  214. }
  215. static inline void
  216. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  217. {
  218. spin_unlock(&hb1->lock);
  219. if (hb1 != hb2)
  220. spin_unlock(&hb2->lock);
  221. }
  222. /* syscalls */
  223. extern int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, u32
  224. val, ktime_t *abs_time, u32 bitset, u32 __user
  225. *uaddr2);
  226. extern int futex_requeue(u32 __user *uaddr1, unsigned int flags,
  227. u32 __user *uaddr2, int nr_wake, int nr_requeue,
  228. u32 *cmpval, int requeue_pi);
  229. extern int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
  230. ktime_t *abs_time, u32 bitset);
  231. /**
  232. * struct futex_vector - Auxiliary struct for futex_waitv()
  233. * @w: Userspace provided data
  234. * @q: Kernel side data
  235. *
  236. * Struct used to build an array with all data need for futex_waitv()
  237. */
  238. struct futex_vector {
  239. struct futex_waitv w;
  240. struct futex_q q;
  241. };
  242. extern int futex_wait_multiple(struct futex_vector *vs, unsigned int count,
  243. struct hrtimer_sleeper *to);
  244. extern int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset);
  245. extern int futex_wake_op(u32 __user *uaddr1, unsigned int flags,
  246. u32 __user *uaddr2, int nr_wake, int nr_wake2, int op);
  247. extern int futex_unlock_pi(u32 __user *uaddr, unsigned int flags);
  248. extern int futex_lock_pi(u32 __user *uaddr, unsigned int flags, ktime_t *time, int trylock);
  249. #endif /* _FUTEX_H */