xref: /linux-6.15/kernel/futex/futex.h (revision 85dc28fa)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FUTEX_H
3 #define _FUTEX_H
4 
5 #include <linux/futex.h>
6 #include <asm/futex.h>
7 
8 /*
9  * Futex flags used to encode options to functions and preserve them across
10  * restarts.
11  */
12 #ifdef CONFIG_MMU
13 # define FLAGS_SHARED		0x01
14 #else
15 /*
16  * NOMMU does not have per process address space. Let the compiler optimize
17  * code away.
18  */
19 # define FLAGS_SHARED		0x00
20 #endif
21 #define FLAGS_CLOCKRT		0x02
22 #define FLAGS_HAS_TIMEOUT	0x04
23 
24 #ifdef CONFIG_HAVE_FUTEX_CMPXCHG
25 #define futex_cmpxchg_enabled 1
26 #else
27 extern int  __read_mostly futex_cmpxchg_enabled;
28 #endif
29 
30 #ifdef CONFIG_FAIL_FUTEX
31 extern bool should_fail_futex(bool fshared);
32 #else
33 static inline bool should_fail_futex(bool fshared)
34 {
35 	return false;
36 }
37 #endif
38 
39 /*
40  * Hash buckets are shared by all the futex_keys that hash to the same
41  * location.  Each key may have multiple futex_q structures, one for each task
42  * waiting on a futex.
43  */
44 struct futex_hash_bucket {
45 	atomic_t waiters;
46 	spinlock_t lock;
47 	struct plist_head chain;
48 } ____cacheline_aligned_in_smp;
49 
50 /*
51  * Priority Inheritance state:
52  */
53 struct futex_pi_state {
54 	/*
55 	 * list of 'owned' pi_state instances - these have to be
56 	 * cleaned up in do_exit() if the task exits prematurely:
57 	 */
58 	struct list_head list;
59 
60 	/*
61 	 * The PI object:
62 	 */
63 	struct rt_mutex_base pi_mutex;
64 
65 	struct task_struct *owner;
66 	refcount_t refcount;
67 
68 	union futex_key key;
69 } __randomize_layout;
70 
71 /**
72  * struct futex_q - The hashed futex queue entry, one per waiting task
73  * @list:		priority-sorted list of tasks waiting on this futex
74  * @task:		the task waiting on the futex
75  * @lock_ptr:		the hash bucket lock
76  * @key:		the key the futex is hashed on
77  * @pi_state:		optional priority inheritance state
78  * @rt_waiter:		rt_waiter storage for use with requeue_pi
79  * @requeue_pi_key:	the requeue_pi target futex key
80  * @bitset:		bitset for the optional bitmasked wakeup
81  * @requeue_state:	State field for futex_requeue_pi()
82  * @requeue_wait:	RCU wait for futex_requeue_pi() (RT only)
83  *
84  * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so
85  * we can wake only the relevant ones (hashed queues may be shared).
86  *
87  * A futex_q has a woken state, just like tasks have TASK_RUNNING.
88  * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
89  * The order of wakeup is always to make the first condition true, then
90  * the second.
91  *
92  * PI futexes are typically woken before they are removed from the hash list via
93  * the rt_mutex code. See futex_unqueue_pi().
94  */
95 struct futex_q {
96 	struct plist_node list;
97 
98 	struct task_struct *task;
99 	spinlock_t *lock_ptr;
100 	union futex_key key;
101 	struct futex_pi_state *pi_state;
102 	struct rt_mutex_waiter *rt_waiter;
103 	union futex_key *requeue_pi_key;
104 	u32 bitset;
105 	atomic_t requeue_state;
106 #ifdef CONFIG_PREEMPT_RT
107 	struct rcuwait requeue_wait;
108 #endif
109 } __randomize_layout;
110 
111 extern const struct futex_q futex_q_init;
112 
113 enum futex_access {
114 	FUTEX_READ,
115 	FUTEX_WRITE
116 };
117 
118 extern int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
119 			 enum futex_access rw);
120 
121 extern struct futex_hash_bucket *futex_hash(union futex_key *key);
122 
123 extern struct hrtimer_sleeper *
124 futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout,
125 		  int flags, u64 range_ns);
126 
127 extern int fault_in_user_writeable(u32 __user *uaddr);
128 extern int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 uval, u32 newval);
129 extern int futex_get_value_locked(u32 *dest, u32 __user *from);
130 extern struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, union futex_key *key);
131 
132 extern void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb);
133 extern void futex_unqueue_pi(struct futex_q *q);
134 
135 extern void wait_for_owner_exiting(int ret, struct task_struct *exiting);
136 
137 extern struct futex_hash_bucket *futex_q_lock(struct futex_q *q);
138 extern void futex_q_unlock(struct futex_hash_bucket *hb);
139 
140 
141 extern int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
142 				union futex_key *key,
143 				struct futex_pi_state **ps,
144 				struct task_struct *task,
145 				struct task_struct **exiting,
146 				int set_waiters);
147 
148 extern int refill_pi_state_cache(void);
149 extern void get_pi_state(struct futex_pi_state *pi_state);
150 extern void put_pi_state(struct futex_pi_state *pi_state);
151 extern int fixup_pi_owner(u32 __user *uaddr, struct futex_q *q, int locked);
152 
153 /* syscalls */
154 
155 extern int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, u32
156 				 val, ktime_t *abs_time, u32 bitset, u32 __user
157 				 *uaddr2);
158 
159 extern int futex_requeue(u32 __user *uaddr1, unsigned int flags,
160 			 u32 __user *uaddr2, int nr_wake, int nr_requeue,
161 			 u32 *cmpval, int requeue_pi);
162 
163 extern int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
164 		      ktime_t *abs_time, u32 bitset);
165 
166 extern int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset);
167 
168 extern int futex_wake_op(u32 __user *uaddr1, unsigned int flags,
169 			 u32 __user *uaddr2, int nr_wake, int nr_wake2, int op);
170 
171 extern int futex_unlock_pi(u32 __user *uaddr, unsigned int flags);
172 
173 extern int futex_lock_pi(u32 __user *uaddr, unsigned int flags, ktime_t *time, int trylock);
174 
175 #endif /* _FUTEX_H */
176