1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _FUTEX_H 3 #define _FUTEX_H 4 5 #include <linux/futex.h> 6 #include <linux/rtmutex.h> 7 #include <linux/sched/wake_q.h> 8 #include <linux/compat.h> 9 #include <linux/uaccess.h> 10 #include <linux/cleanup.h> 11 12 #ifdef CONFIG_PREEMPT_RT 13 #include <linux/rcuwait.h> 14 #endif 15 16 #include <asm/futex.h> 17 18 /* 19 * Futex flags used to encode options to functions and preserve them across 20 * restarts. 21 */ 22 #define FLAGS_SIZE_8 0x0000 23 #define FLAGS_SIZE_16 0x0001 24 #define FLAGS_SIZE_32 0x0002 25 #define FLAGS_SIZE_64 0x0003 26 27 #define FLAGS_SIZE_MASK 0x0003 28 29 #ifdef CONFIG_MMU 30 # define FLAGS_SHARED 0x0010 31 #else 32 /* 33 * NOMMU does not have per process address space. Let the compiler optimize 34 * code away. 35 */ 36 # define FLAGS_SHARED 0x0000 37 #endif 38 #define FLAGS_CLOCKRT 0x0020 39 #define FLAGS_HAS_TIMEOUT 0x0040 40 #define FLAGS_NUMA 0x0080 41 #define FLAGS_STRICT 0x0100 42 #define FLAGS_MPOL 0x0200 43 #define FLAGS_ROBUST_UNLOCK 0x0400 44 #define FLAGS_ROBUST_LIST32 0x0800 45 46 /* FUTEX_ to FLAGS_ */ 47 static inline unsigned int futex_to_flags(unsigned int op) 48 { 49 unsigned int flags = FLAGS_SIZE_32; 50 51 if (!(op & FUTEX_PRIVATE_FLAG)) 52 flags |= FLAGS_SHARED; 53 54 if (op & FUTEX_CLOCK_REALTIME) 55 flags |= FLAGS_CLOCKRT; 56 57 if (op & FUTEX_ROBUST_UNLOCK) 58 flags |= FLAGS_ROBUST_UNLOCK; 59 60 if (op & FUTEX_ROBUST_LIST32) 61 flags |= FLAGS_ROBUST_LIST32; 62 63 return flags; 64 } 65 66 #define FUTEX2_VALID_MASK (FUTEX2_SIZE_MASK | FUTEX2_NUMA | FUTEX2_MPOL | FUTEX2_PRIVATE) 67 68 /* FUTEX2_ to FLAGS_ */ 69 static inline unsigned int futex2_to_flags(unsigned int flags2) 70 { 71 unsigned int flags = flags2 & FUTEX2_SIZE_MASK; 72 73 if (!(flags2 & FUTEX2_PRIVATE)) 74 flags |= FLAGS_SHARED; 75 76 if (flags2 & FUTEX2_NUMA) 77 flags |= FLAGS_NUMA; 78 79 if (flags2 & FUTEX2_MPOL) 80 flags |= FLAGS_MPOL; 81 82 return flags; 83 } 84 85 static inline unsigned int futex_size(unsigned int flags) 86 { 87 return 1 << (flags & FLAGS_SIZE_MASK); 88 } 89 90 static inline bool futex_flags_valid(unsigned int flags) 91 { 92 /* Only 64bit futexes for 64bit code */ 93 if (!IS_ENABLED(CONFIG_64BIT) || in_compat_syscall()) { 94 if ((flags & FLAGS_SIZE_MASK) == FLAGS_SIZE_64) 95 return false; 96 } 97 98 /* Only 32bit futexes are implemented -- for now */ 99 if ((flags & FLAGS_SIZE_MASK) != FLAGS_SIZE_32) 100 return false; 101 102 /* 103 * Must be able to represent both FUTEX_NO_NODE and every valid nodeid 104 * in a futex word. 105 */ 106 if (flags & FLAGS_NUMA) { 107 int bits = 8 * futex_size(flags); 108 u64 max = ~0ULL; 109 110 max >>= 64 - bits; 111 if (nr_node_ids >= max) 112 return false; 113 } 114 115 return true; 116 } 117 118 static inline bool futex_validate_input(unsigned int flags, u64 val) 119 { 120 int bits = 8 * futex_size(flags); 121 122 if (bits < 64 && (val >> bits)) 123 return false; 124 125 return true; 126 } 127 128 #ifdef CONFIG_FAIL_FUTEX 129 extern bool should_fail_futex(bool fshared); 130 #else 131 static inline bool should_fail_futex(bool fshared) 132 { 133 return false; 134 } 135 #endif 136 137 /* 138 * Hash buckets are shared by all the futex_keys that hash to the same 139 * location. Each key may have multiple futex_q structures, one for each task 140 * waiting on a futex. 141 */ 142 struct futex_hash_bucket { 143 atomic_t waiters; 144 spinlock_t lock; 145 struct plist_head chain; 146 struct futex_private_hash *priv; 147 } ____cacheline_aligned_in_smp; 148 149 /* 150 * Priority Inheritance state: 151 */ 152 struct futex_pi_state { 153 /* 154 * list of 'owned' pi_state instances - these have to be 155 * cleaned up in do_exit() if the task exits prematurely: 156 */ 157 struct list_head list; 158 159 /* 160 * The PI object: 161 */ 162 struct rt_mutex_base pi_mutex; 163 164 struct task_struct *owner; 165 refcount_t refcount; 166 167 union futex_key key; 168 } __randomize_layout; 169 170 struct futex_q; 171 typedef void (futex_wake_fn)(struct wake_q_head *wake_q, struct futex_q *q); 172 173 /** 174 * struct futex_q - The hashed futex queue entry, one per waiting task 175 * @list: priority-sorted list of tasks waiting on this futex 176 * @task: the task waiting on the futex 177 * @lock_ptr: the hash bucket lock 178 * @wake: the wake handler for this queue 179 * @wake_data: data associated with the wake handler 180 * @key: the key the futex is hashed on 181 * @pi_state: optional priority inheritance state 182 * @rt_waiter: rt_waiter storage for use with requeue_pi 183 * @requeue_pi_key: the requeue_pi target futex key 184 * @bitset: bitset for the optional bitmasked wakeup 185 * @requeue_state: State field for futex_requeue_pi() 186 * @drop_hb_ref: Waiter should drop the extra hash bucket reference if true 187 * @requeue_wait: RCU wait for futex_requeue_pi() (RT only) 188 * 189 * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so 190 * we can wake only the relevant ones (hashed queues may be shared). 191 * 192 * A futex_q has a woken state, just like tasks have TASK_RUNNING. 193 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. 194 * The order of wakeup is always to make the first condition true, then 195 * the second. 196 * 197 * PI futexes are typically woken before they are removed from the hash list via 198 * the rt_mutex code. See futex_unqueue_pi(). 199 */ 200 struct futex_q { 201 struct plist_node list; 202 203 struct task_struct *task; 204 spinlock_t *lock_ptr; 205 futex_wake_fn *wake; 206 void *wake_data; 207 union futex_key key; 208 struct futex_pi_state *pi_state; 209 struct rt_mutex_waiter *rt_waiter; 210 union futex_key *requeue_pi_key; 211 u32 bitset; 212 atomic_t requeue_state; 213 bool drop_hb_ref; 214 #ifdef CONFIG_PREEMPT_RT 215 struct rcuwait requeue_wait; 216 #endif 217 } __randomize_layout; 218 219 extern const struct futex_q futex_q_init; 220 221 enum futex_access { 222 FUTEX_READ, 223 FUTEX_WRITE 224 }; 225 226 extern int get_futex_key(u32 __user *uaddr, unsigned int flags, union futex_key *key, 227 enum futex_access rw); 228 extern void futex_q_lockptr_lock(struct futex_q *q) __acquires(q->lock_ptr); 229 extern struct hrtimer_sleeper * 230 futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout, 231 int flags, u64 range_ns); 232 233 extern struct futex_hash_bucket *futex_hash(union futex_key *key); 234 #ifdef CONFIG_FUTEX_PRIVATE_HASH 235 extern void futex_hash_get(struct futex_hash_bucket *hb); 236 extern void futex_hash_put(struct futex_hash_bucket *hb); 237 238 extern struct futex_private_hash *futex_private_hash(void); 239 extern void futex_private_hash_put(struct futex_private_hash *fph); 240 241 #else /* !CONFIG_FUTEX_PRIVATE_HASH */ 242 static inline void futex_hash_get(struct futex_hash_bucket *hb) { } 243 static inline void futex_hash_put(struct futex_hash_bucket *hb) { } 244 static inline struct futex_private_hash *futex_private_hash(void) { return NULL; } 245 static inline void futex_private_hash_put(struct futex_private_hash *fph) { } 246 #endif 247 248 DEFINE_CLASS(hb, struct futex_hash_bucket *, 249 if (_T) futex_hash_put(_T), 250 futex_hash(key), union futex_key *key); 251 252 DEFINE_CLASS(private_hash, struct futex_private_hash *, 253 if (_T) futex_private_hash_put(_T), 254 futex_private_hash(), void); 255 256 /** 257 * futex_match - Check whether two futex keys are equal 258 * @key1: Pointer to key1 259 * @key2: Pointer to key2 260 * 261 * Return 1 if two futex_keys are equal, 0 otherwise. 262 */ 263 static inline int futex_match(union futex_key *key1, union futex_key *key2) 264 { 265 return (key1 && key2 266 && key1->both.word == key2->both.word 267 && key1->both.ptr == key2->both.ptr 268 && key1->both.offset == key2->both.offset); 269 } 270 271 extern int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags, 272 struct futex_q *q, union futex_key *key2, 273 struct task_struct *task); 274 extern void futex_do_wait(struct futex_q *q, struct hrtimer_sleeper *timeout); 275 extern bool __futex_wake_mark(struct futex_q *q); 276 extern void futex_wake_mark(struct wake_q_head *wake_q, struct futex_q *q); 277 278 extern int fault_in_user_writeable(u32 __user *uaddr); 279 extern struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, union futex_key *key); 280 281 static inline int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 uval, u32 newval) 282 { 283 int ret; 284 285 pagefault_disable(); 286 ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval); 287 pagefault_enable(); 288 289 return ret; 290 } 291 292 /* Read from user memory with pagefaults disabled */ 293 static inline int futex_get_value_locked(u32 *dest, u32 __user *from) 294 { 295 guard(pagefault)(); 296 return get_user_inline(*dest, from); 297 } 298 299 extern void __futex_unqueue(struct futex_q *q); 300 extern void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb, 301 struct task_struct *task); 302 extern int futex_unqueue(struct futex_q *q); 303 304 /** 305 * futex_queue() - Enqueue the futex_q on the futex_hash_bucket 306 * @q: The futex_q to enqueue 307 * @hb: The destination hash bucket 308 * @task: Task queueing this futex 309 * 310 * The hb->lock must be held by the caller, and is released here. A call to 311 * futex_queue() is typically paired with exactly one call to futex_unqueue(). The 312 * exceptions involve the PI related operations, which may use futex_unqueue_pi() 313 * or nothing if the unqueue is done as part of the wake process and the unqueue 314 * state is implicit in the state of woken task (see futex_wait_requeue_pi() for 315 * an example). 316 * 317 * Note that @task may be NULL, for async usage of futexes. 318 */ 319 static inline void futex_queue(struct futex_q *q, struct futex_hash_bucket *hb, 320 struct task_struct *task) 321 __releases(&hb->lock) 322 __releases(q->lock_ptr) 323 { 324 __futex_queue(q, hb, task); 325 spin_unlock(&hb->lock); 326 __release(q->lock_ptr); 327 } 328 329 extern void futex_unqueue_pi(struct futex_q *q); 330 331 extern void wait_for_owner_exiting(int ret, struct task_struct *exiting); 332 333 /* 334 * Reflects a new waiter being added to the waitqueue. 335 */ 336 static inline void futex_hb_waiters_inc(struct futex_hash_bucket *hb) 337 { 338 #ifdef CONFIG_SMP 339 atomic_inc(&hb->waiters); 340 /* 341 * Full barrier (A), see the ordering comment above. 342 */ 343 smp_mb__after_atomic(); 344 #endif 345 } 346 347 /* 348 * Reflects a waiter being removed from the waitqueue by wakeup 349 * paths. 350 */ 351 static inline void futex_hb_waiters_dec(struct futex_hash_bucket *hb) 352 { 353 #ifdef CONFIG_SMP 354 atomic_dec(&hb->waiters); 355 #endif 356 } 357 358 static inline int futex_hb_waiters_pending(struct futex_hash_bucket *hb) 359 { 360 #ifdef CONFIG_SMP 361 /* 362 * Full barrier (B), see the ordering comment above. 363 */ 364 smp_mb(); 365 return atomic_read(&hb->waiters); 366 #else 367 return 1; 368 #endif 369 } 370 371 extern void futex_q_lock(struct futex_q *q, struct futex_hash_bucket *hb) 372 __acquires(&hb->lock) 373 __acquires(q->lock_ptr); 374 375 extern void futex_q_unlock(struct futex_hash_bucket *hb) 376 __releases(&hb->lock); 377 378 extern int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb, 379 union futex_key *key, 380 struct futex_pi_state **ps, 381 struct task_struct *task, 382 struct task_struct **exiting, 383 int set_waiters); 384 385 extern int refill_pi_state_cache(void); 386 extern void get_pi_state(struct futex_pi_state *pi_state); 387 extern void put_pi_state(struct futex_pi_state *pi_state); 388 extern int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked); 389 390 /* 391 * Express the locking dependencies for lockdep: 392 */ 393 static inline void 394 double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) 395 __acquires(&hb1->lock) 396 __acquires(&hb2->lock) 397 __no_context_analysis 398 { 399 if (hb1 > hb2) 400 swap(hb1, hb2); 401 402 spin_lock(&hb1->lock); 403 if (hb1 != hb2) 404 spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING); 405 } 406 407 static inline void 408 double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) 409 __releases(&hb1->lock) 410 __releases(&hb2->lock) 411 __no_context_analysis 412 { 413 spin_unlock(&hb1->lock); 414 if (hb1 != hb2) 415 spin_unlock(&hb2->lock); 416 } 417 418 /* syscalls */ 419 420 extern int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, u32 421 val, ktime_t *abs_time, u32 bitset, u32 __user 422 *uaddr2); 423 424 extern int futex_requeue(u32 __user *uaddr1, unsigned int flags1, 425 u32 __user *uaddr2, unsigned int flags2, 426 int nr_wake, int nr_requeue, 427 u32 *cmpval, int requeue_pi); 428 429 extern int __futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, 430 struct hrtimer_sleeper *to, u32 bitset); 431 432 extern int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, 433 ktime_t *abs_time, u32 bitset); 434 435 /** 436 * struct futex_vector - Auxiliary struct for futex_waitv() 437 * @w: Userspace provided data 438 * @q: Kernel side data 439 * 440 * Struct used to build an array with all data need for futex_waitv() 441 */ 442 struct futex_vector { 443 struct futex_waitv w; 444 struct futex_q q; 445 }; 446 447 extern int futex_parse_waitv(struct futex_vector *futexv, 448 struct futex_waitv __user *uwaitv, 449 unsigned int nr_futexes, futex_wake_fn *wake, 450 void *wake_data); 451 452 extern int futex_wait_multiple_setup(struct futex_vector *vs, int count, 453 int *woken); 454 455 extern int futex_unqueue_multiple(struct futex_vector *v, int count); 456 457 extern int futex_wait_multiple(struct futex_vector *vs, unsigned int count, 458 struct hrtimer_sleeper *to); 459 460 extern int futex_wake(u32 __user *uaddr, unsigned int flags, void __user *pop, 461 int nr_wake, u32 bitset); 462 463 extern int futex_wake_op(u32 __user *uaddr1, unsigned int flags, 464 u32 __user *uaddr2, int nr_wake, int nr_wake2, int op); 465 466 extern int futex_unlock_pi(u32 __user *uaddr, unsigned int flags, void __user *pop); 467 468 extern int futex_lock_pi(u32 __user *uaddr, unsigned int flags, ktime_t *time, int trylock); 469 470 bool futex_robust_list_clear_pending(void __user *pop, unsigned int flags); 471 472 #endif /* _FUTEX_H */ 473