futex.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <linux/compat.h>
  8. #ifdef CONFIG_PREEMPT_RT
  9. #include <linux/rcuwait.h>
  10. #endif
  11. #include <asm/futex.h>
  12. /*
  13. * Futex flags used to encode options to functions and preserve them across
  14. * restarts.
  15. */
  16. #define FLAGS_SIZE_8 0x0000
  17. #define FLAGS_SIZE_16 0x0001
  18. #define FLAGS_SIZE_32 0x0002
  19. #define FLAGS_SIZE_64 0x0003
  20. #define FLAGS_SIZE_MASK 0x0003
  21. #ifdef CONFIG_MMU
  22. # define FLAGS_SHARED 0x0010
  23. #else
  24. /*
  25. * NOMMU does not have per process address space. Let the compiler optimize
  26. * code away.
  27. */
  28. # define FLAGS_SHARED 0x0000
  29. #endif
  30. #define FLAGS_CLOCKRT 0x0020
  31. #define FLAGS_HAS_TIMEOUT 0x0040
  32. #define FLAGS_NUMA 0x0080
  33. #define FLAGS_STRICT 0x0100
  34. /* FUTEX_ to FLAGS_ */
  35. static inline unsigned int futex_to_flags(unsigned int op)
  36. {
  37. unsigned int flags = FLAGS_SIZE_32;
  38. if (!(op & FUTEX_PRIVATE_FLAG))
  39. flags |= FLAGS_SHARED;
  40. if (op & FUTEX_CLOCK_REALTIME)
  41. flags |= FLAGS_CLOCKRT;
  42. return flags;
  43. }
  44. #define FUTEX2_VALID_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE)
  45. /* FUTEX2_ to FLAGS_ */
  46. static inline unsigned int futex2_to_flags(unsigned int flags2)
  47. {
  48. unsigned int flags = flags2 & FUTEX2_SIZE_MASK;
  49. if (!(flags2 & FUTEX2_PRIVATE))
  50. flags |= FLAGS_SHARED;
  51. if (flags2 & FUTEX2_NUMA)
  52. flags |= FLAGS_NUMA;
  53. return flags;
  54. }
  55. static inline unsigned int futex_size(unsigned int flags)
  56. {
  57. return 1 << (flags & FLAGS_SIZE_MASK);
  58. }
  59. static inline bool futex_flags_valid(unsigned int flags)
  60. {
  61. /* Only 64bit futexes for 64bit code */
  62. if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) {
  63. if ((flags & FLAGS_SIZE_MASK) == FLAGS_SIZE_64)
  64. return false;
  65. }
  66. /* Only 32bit futexes are implemented -- for now */
  67. if ((flags & FLAGS_SIZE_MASK) != FLAGS_SIZE_32)
  68. return false;
  69. return true;
  70. }
  71. static inline bool futex_validate_input(unsigned int flags, u64 val)
  72. {
  73. int bits = 8 * futex_size(flags);
  74. if (bits < 64 && (val >> bits))
  75. return false;
  76. return true;
  77. }
  78. #ifdef CONFIG_FAIL_FUTEX
  79. extern bool should_fail_futex(bool fshared);
  80. #else
  81. static inline bool should_fail_futex(bool fshared)
  82. {
  83. return false;
  84. }
  85. #endif
  86. /*
  87. * Hash buckets are shared by all the futex_keys that hash to the same
  88. * location. Each key may have multiple futex_q structures, one for each task
  89. * waiting on a futex.
  90. */
  91. struct futex_hash_bucket {
  92. atomic_t waiters;
  93. spinlock_t lock;
  94. struct plist_head chain;
  95. } ____cacheline_aligned_in_smp;
  96. /*
  97. * Priority Inheritance state:
  98. */
  99. struct futex_pi_state {
  100. /*
  101. * list of 'owned' pi_state instances - these have to be
  102. * cleaned up in do_exit() if the task exits prematurely:
  103. */
  104. struct list_head list;
  105. /*
  106. * The PI object:
  107. */
  108. struct rt_mutex_base pi_mutex;
  109. struct task_struct *owner;
  110. refcount_t refcount;
  111. union futex_key key;
  112. } __randomize_layout;
  113. struct futex_q;
  114. typedef void (futex_wake_fn)(struct wake_q_head *wake_q, struct futex_q *q);
  115. /**
  116. * struct futex_q - The hashed futex queue entry, one per waiting task
  117. * @list: priority-sorted list of tasks waiting on this futex
  118. * @task: the task waiting on the futex
  119. * @lock_ptr: the hash bucket lock
  120. * @wake: the wake handler for this queue
  121. * @wake_data: data associated with the wake handler
  122. * @key: the key the futex is hashed on
  123. * @pi_state: optional priority inheritance state
  124. * @rt_waiter: rt_waiter storage for use with requeue_pi
  125. * @requeue_pi_key: the requeue_pi target futex key
  126. * @bitset: bitset for the optional bitmasked wakeup
  127. * @requeue_state: State field for futex_requeue_pi()
  128. * @requeue_wait: RCU wait for futex_requeue_pi() (RT only)
  129. *
  130. * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so
  131. * we can wake only the relevant ones (hashed queues may be shared).
  132. *
  133. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  134. * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
  135. * The order of wakeup is always to make the first condition true, then
  136. * the second.
  137. *
  138. * PI futexes are typically woken before they are removed from the hash list via
  139. * the rt_mutex code. See futex_unqueue_pi().
  140. */
  141. struct futex_q {
  142. struct plist_node list;
  143. struct task_struct *task;
  144. spinlock_t *lock_ptr;
  145. futex_wake_fn *wake;
  146. void *wake_data;
  147. union futex_key key;
  148. struct futex_pi_state *pi_state;
  149. struct rt_mutex_waiter *rt_waiter;
  150. union futex_key *requeue_pi_key;
  151. u32 bitset;
  152. atomic_t requeue_state;
  153. #ifdef CONFIG_PREEMPT_RT
  154. struct rcuwait requeue_wait;
  155. #endif
  156. } __randomize_layout;
  157. extern const struct futex_q futex_q_init;
  158. enum futex_access {
  159. FUTEX_READ,
  160. FUTEX_WRITE
  161. };
  162. extern int get_futex_key(u32 __user *uaddr, unsigned int flags, union futex_key *key,
  163. enum futex_access rw);
  164. extern struct hrtimer_sleeper *
  165. futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
  166. int flags, u64 range_ns);
  167. extern struct futex_hash_bucket *futex_hash(union futex_key *key);
  168. /**
  169. * futex_match - Check whether two futex keys are equal
  170. * @key1: Pointer to key1
  171. * @key2: Pointer to key2
  172. *
  173. * Return 1 if two futex_keys are equal, 0 otherwise.
  174. */
  175. static inline int futex_match(union futex_key *key1, union futex_key *key2)
  176. {
  177. return (key1 && key2
  178. && key1->both.word == key2->both.word
  179. && key1->both.ptr == key2->both.ptr
  180. && key1->both.offset == key2->both.offset);
  181. }
  182. extern int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
  183. struct futex_q *q, struct futex_hash_bucket **hb);
  184. extern void futex_wait_queue(struct futex_hash_bucket *hb, struct futex_q *q,
  185. struct hrtimer_sleeper *timeout);
  186. extern bool __futex_wake_mark(struct futex_q *q);
  187. extern void futex_wake_mark(struct wake_q_head *wake_q, struct futex_q *q);
  188. extern int fault_in_user_writeable(u32 __user *uaddr);
  189. extern int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 uval, u32 newval);
  190. extern int futex_get_value_locked(u32 *dest, u32 __user *from);
  191. extern struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, union futex_key *key);
  192. extern void __futex_unqueue(struct futex_q *q);
  193. extern void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb,
  194. struct task_struct *task);
  195. extern int futex_unqueue(struct futex_q *q);
  196. /**
  197. * futex_queue() - Enqueue the futex_q on the futex_hash_bucket
  198. * @q: The futex_q to enqueue
  199. * @hb: The destination hash bucket
  200. * @task: Task queueing this futex
  201. *
  202. * The hb->lock must be held by the caller, and is released here. A call to
  203. * futex_queue() is typically paired with exactly one call to futex_unqueue(). The
  204. * exceptions involve the PI related operations, which may use futex_unqueue_pi()
  205. * or nothing if the unqueue is done as part of the wake process and the unqueue
  206. * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
  207. * an example).
  208. *
  209. * Note that @task may be NULL, for async usage of futexes.
  210. */
  211. static inline void futex_queue(struct futex_q *q, struct futex_hash_bucket *hb,
  212. struct task_struct *task)
  213. __releases(&hb->lock)
  214. {
  215. __futex_queue(q, hb, task);
  216. spin_unlock(&hb->lock);
  217. }
  218. extern void futex_unqueue_pi(struct futex_q *q);
  219. extern void wait_for_owner_exiting(int ret, struct task_struct *exiting);
  220. /*
  221. * Reflects a new waiter being added to the waitqueue.
  222. */
  223. static inline void futex_hb_waiters_inc(struct futex_hash_bucket *hb)
  224. {
  225. #ifdef CONFIG_SMP
  226. atomic_inc(&hb->waiters);
  227. /*
  228. * Full barrier (A), see the ordering comment above.
  229. */
  230. smp_mb__after_atomic();
  231. #endif
  232. }
  233. /*
  234. * Reflects a waiter being removed from the waitqueue by wakeup
  235. * paths.
  236. */
  237. static inline void futex_hb_waiters_dec(struct futex_hash_bucket *hb)
  238. {
  239. #ifdef CONFIG_SMP
  240. atomic_dec(&hb->waiters);
  241. #endif
  242. }
  243. static inline int futex_hb_waiters_pending(struct futex_hash_bucket *hb)
  244. {
  245. #ifdef CONFIG_SMP
  246. /*
  247. * Full barrier (B), see the ordering comment above.
  248. */
  249. smp_mb();
  250. return atomic_read(&hb->waiters);
  251. #else
  252. return 1;
  253. #endif
  254. }
  255. extern struct futex_hash_bucket *futex_q_lock(struct futex_q *q);
  256. extern void futex_q_unlock(struct futex_hash_bucket *hb);
  257. extern int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
  258. union futex_key *key,
  259. struct futex_pi_state **ps,
  260. struct task_struct *task,
  261. struct task_struct **exiting,
  262. int set_waiters);
  263. extern int refill_pi_state_cache(void);
  264. extern void get_pi_state(struct futex_pi_state *pi_state);
  265. extern void put_pi_state(struct futex_pi_state *pi_state);
  266. extern int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked);
  267. /*
  268. * Express the locking dependencies for lockdep:
  269. */
  270. static inline void
  271. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  272. {
  273. if (hb1 > hb2)
  274. swap(hb1, hb2);
  275. spin_lock(&hb1->lock);
  276. if (hb1 != hb2)
  277. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  278. }
  279. static inline void
  280. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  281. {
  282. spin_unlock(&hb1->lock);
  283. if (hb1 != hb2)
  284. spin_unlock(&hb2->lock);
  285. }
  286. /* syscalls */
  287. extern int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, u32
  288. val, ktime_t *abs_time, u32 bitset, u32 __user
  289. *uaddr2);
  290. extern int futex_requeue(u32 __user *uaddr1, unsigned int flags1,
  291. u32 __user *uaddr2, unsigned int flags2,
  292. int nr_wake, int nr_requeue,
  293. u32 *cmpval, int requeue_pi);
  294. extern int __futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
  295. struct hrtimer_sleeper *to, u32 bitset);
  296. extern int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
  297. ktime_t *abs_time, u32 bitset);
  298. /**
  299. * struct futex_vector - Auxiliary struct for futex_waitv()
  300. * @w: Userspace provided data
  301. * @q: Kernel side data
  302. *
  303. * Struct used to build an array with all data need for futex_waitv()
  304. */
  305. struct futex_vector {
  306. struct futex_waitv w;
  307. struct futex_q q;
  308. };
  309. extern int futex_parse_waitv(struct futex_vector *futexv,
  310. struct futex_waitv __user *uwaitv,
  311. unsigned int nr_futexes, futex_wake_fn *wake,
  312. void *wake_data);
  313. extern int futex_wait_multiple_setup(struct futex_vector *vs, int count,
  314. int *woken);
  315. extern int futex_unqueue_multiple(struct futex_vector *v, int count);
  316. extern int futex_wait_multiple(struct futex_vector *vs, unsigned int count,
  317. struct hrtimer_sleeper *to);
  318. extern int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset);
  319. extern int futex_wake_op(u32 __user *uaddr1, unsigned int flags,
  320. u32 __user *uaddr2, int nr_wake, int nr_wake2, int op);
  321. extern int futex_unlock_pi(u32 __user *uaddr, unsigned int flags);
  322. extern int futex_lock_pi(u32 __user *uaddr, unsigned int flags, ktime_t *time, int trylock);
  323. #endif /* _FUTEX_H */