1 /* $OpenBSD: sys_futex.c,v 1.18 2021/05/26 18:11:59 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 2016-2017 Martin Pieuchot 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/proc.h> 22 #include <sys/kernel.h> 23 #include <sys/mount.h> 24 #include <sys/syscallargs.h> 25 #include <sys/pool.h> 26 #include <sys/time.h> 27 #include <sys/rwlock.h> 28 #include <sys/futex.h> 29 30 #ifdef KTRACE 31 #include <sys/ktrace.h> 32 #endif 33 34 #include <uvm/uvm.h> 35 36 /* 37 * Atomicity is only needed on MULTIPROCESSOR kernels. Fall back on 38 * copyin(9) until non-MULTIPROCESSOR architectures have a copyin32(9) 39 * implementation. 40 */ 41 #ifndef MULTIPROCESSOR 42 #define copyin32(uaddr, kaddr) copyin((uaddr), (kaddr), sizeof(uint32_t)) 43 #endif 44 45 /* 46 * Kernel representation of a futex. 47 */ 48 struct futex { 49 LIST_ENTRY(futex) ft_list; /* list of all futexes */ 50 TAILQ_HEAD(, proc) ft_threads; /* sleeping queue */ 51 struct uvm_object *ft_obj; /* UVM object */ 52 voff_t ft_off; /* UVM offset */ 53 unsigned int ft_refcnt; /* # of references */ 54 }; 55 56 /* Syscall helpers. */ 57 int futex_wait(uint32_t *, uint32_t, const struct timespec *, int); 58 int futex_wake(uint32_t *, uint32_t, int); 59 int futex_requeue(uint32_t *, uint32_t, uint32_t *, uint32_t, int); 60 61 /* Flags for futex_get(). */ 62 #define FT_CREATE 0x1 /* Create a futex if it doesn't exist. */ 63 #define FT_PRIVATE 0x2 /* Futex is process-private. */ 64 65 struct futex *futex_get(uint32_t *, int); 66 void futex_put(struct futex *); 67 68 /* 69 * The global futex lock serializes futex(2) calls so that no wakeup 70 * event is lost, and protects all futex lists and futex states. 71 */ 72 struct rwlock ftlock = RWLOCK_INITIALIZER("futex"); 73 static struct futex_list ftlist_shared = 74 LIST_HEAD_INITIALIZER(ftlist_shared); 75 struct pool ftpool; 76 77 78 void 79 futex_init(void) 80 { 81 pool_init(&ftpool, sizeof(struct futex), 0, IPL_NONE, 82 PR_WAITOK | PR_RWLOCK, "futexpl", NULL); 83 } 84 85 int 86 sys_futex(struct proc *p, void *v, register_t *retval) 87 { 88 struct sys_futex_args /* { 89 syscallarg(uint32_t *) f; 90 syscallarg(int) op; 91 syscallarg(inr) val; 92 syscallarg(const struct timespec *) timeout; 93 syscallarg(uint32_t *) g; 94 } */ *uap = v; 95 uint32_t *uaddr = SCARG(uap, f); 96 int op = SCARG(uap, op); 97 uint32_t val = SCARG(uap, val); 98 const struct timespec *timeout = SCARG(uap, timeout); 99 void *g = SCARG(uap, g); 100 int flags = 0; 101 int error = 0; 102 103 if (op & FUTEX_PRIVATE_FLAG) 104 flags |= FT_PRIVATE; 105 106 switch (op) { 107 case FUTEX_WAIT: 108 case FUTEX_WAIT_PRIVATE: 109 KERNEL_LOCK(); 110 rw_enter_write(&ftlock); 111 error = futex_wait(uaddr, val, timeout, flags); 112 rw_exit_write(&ftlock); 113 KERNEL_UNLOCK(); 114 break; 115 case FUTEX_WAKE: 116 case FUTEX_WAKE_PRIVATE: 117 rw_enter_write(&ftlock); 118 *retval = futex_wake(uaddr, val, flags); 119 rw_exit_write(&ftlock); 120 break; 121 case FUTEX_REQUEUE: 122 case FUTEX_REQUEUE_PRIVATE: 123 rw_enter_write(&ftlock); 124 *retval = futex_requeue(uaddr, val, g, (u_long)timeout, flags); 125 rw_exit_write(&ftlock); 126 break; 127 default: 128 error = ENOSYS; 129 break; 130 } 131 132 return error; 133 } 134 135 /* 136 * Return an existing futex matching userspace address ``uaddr''. 137 * 138 * If such futex does not exist and FT_CREATE is given, create it. 139 */ 140 struct futex * 141 futex_get(uint32_t *uaddr, int flags) 142 { 143 struct proc *p = curproc; 144 vm_map_t map = &p->p_vmspace->vm_map; 145 vm_map_entry_t entry; 146 struct uvm_object *obj = NULL; 147 voff_t off = (vaddr_t)uaddr; 148 struct futex *f; 149 struct futex_list *ftlist = &p->p_p->ps_ftlist; 150 151 rw_assert_wrlock(&ftlock); 152 153 if (!(flags & FT_PRIVATE)) { 154 vm_map_lock_read(map); 155 if (uvm_map_lookup_entry(map, (vaddr_t)uaddr, &entry) && 156 UVM_ET_ISOBJ(entry) && entry->object.uvm_obj && 157 entry->inheritance == MAP_INHERIT_SHARE) { 158 ftlist = &ftlist_shared; 159 obj = entry->object.uvm_obj; 160 off = entry->offset + ((vaddr_t)uaddr - entry->start); 161 } 162 vm_map_unlock_read(map); 163 } 164 165 LIST_FOREACH(f, ftlist, ft_list) { 166 if (f->ft_obj == obj && f->ft_off == off) { 167 f->ft_refcnt++; 168 break; 169 } 170 } 171 172 if ((f == NULL) && (flags & FT_CREATE)) { 173 /* 174 * We rely on the rwlock to ensure that no other thread 175 * create the same futex. 176 */ 177 f = pool_get(&ftpool, PR_WAITOK); 178 TAILQ_INIT(&f->ft_threads); 179 f->ft_obj = obj; 180 f->ft_off = off; 181 f->ft_refcnt = 1; 182 LIST_INSERT_HEAD(ftlist, f, ft_list); 183 } 184 185 return f; 186 } 187 188 /* 189 * Release a given futex. 190 */ 191 void 192 futex_put(struct futex *f) 193 { 194 rw_assert_wrlock(&ftlock); 195 196 KASSERT(f->ft_refcnt > 0); 197 198 --f->ft_refcnt; 199 if (f->ft_refcnt == 0) { 200 KASSERT(TAILQ_EMPTY(&f->ft_threads)); 201 LIST_REMOVE(f, ft_list); 202 pool_put(&ftpool, f); 203 } 204 } 205 206 /* 207 * Put the current thread on the sleep queue of the futex at address 208 * ``uaddr''. Let it sleep for the specified ``timeout'' time, or 209 * indefinitely if the argument is NULL. 210 */ 211 int 212 futex_wait(uint32_t *uaddr, uint32_t val, const struct timespec *timeout, 213 int flags) 214 { 215 struct proc *p = curproc; 216 struct futex *f; 217 uint64_t nsecs = INFSLP; 218 uint32_t cval; 219 int error; 220 221 /* 222 * After reading the value a race is still possible but 223 * we deal with it by serializing all futex syscalls. 224 */ 225 rw_assert_wrlock(&ftlock); 226 227 /* 228 * Read user space futex value 229 */ 230 if ((error = copyin32(uaddr, &cval))) 231 return error; 232 233 /* If the value changed, stop here. */ 234 if (cval != val) 235 return EAGAIN; 236 237 if (timeout != NULL) { 238 struct timespec ts; 239 240 if ((error = copyin(timeout, &ts, sizeof(ts)))) 241 return error; 242 #ifdef KTRACE 243 if (KTRPOINT(p, KTR_STRUCT)) 244 ktrreltimespec(p, &ts); 245 #endif 246 if (ts.tv_sec < 0 || !timespecisvalid(&ts)) 247 return EINVAL; 248 nsecs = MAX(1, MIN(TIMESPEC_TO_NSEC(&ts), MAXTSLP)); 249 } 250 251 f = futex_get(uaddr, flags | FT_CREATE); 252 TAILQ_INSERT_TAIL(&f->ft_threads, p, p_fut_link); 253 p->p_futex = f; 254 255 error = rwsleep_nsec(p, &ftlock, PWAIT|PCATCH, "fsleep", nsecs); 256 if (error == ERESTART) 257 error = ECANCELED; 258 else if (error == EWOULDBLOCK) { 259 /* A race occurred between a wakeup and a timeout. */ 260 if (p->p_futex == NULL) 261 error = 0; 262 else 263 error = ETIMEDOUT; 264 } 265 266 /* Remove ourself if we haven't been awaken. */ 267 if ((f = p->p_futex) != NULL) { 268 p->p_futex = NULL; 269 TAILQ_REMOVE(&f->ft_threads, p, p_fut_link); 270 futex_put(f); 271 } 272 273 return error; 274 } 275 276 /* 277 * Wakeup at most ``n'' sibling threads sleeping on a futex at address 278 * ``uaddr'' and requeue at most ``m'' sibling threads on a futex at 279 * address ``uaddr2''. 280 */ 281 int 282 futex_requeue(uint32_t *uaddr, uint32_t n, uint32_t *uaddr2, uint32_t m, 283 int flags) 284 { 285 struct futex *f, *g; 286 struct proc *p; 287 uint32_t count = 0; 288 289 rw_assert_wrlock(&ftlock); 290 291 f = futex_get(uaddr, flags); 292 if (f == NULL) 293 return 0; 294 295 while ((p = TAILQ_FIRST(&f->ft_threads)) != NULL && (count < (n + m))) { 296 p->p_futex = NULL; 297 TAILQ_REMOVE(&f->ft_threads, p, p_fut_link); 298 futex_put(f); 299 300 if (count < n) { 301 wakeup_one(p); 302 } else if (uaddr2 != NULL) { 303 g = futex_get(uaddr2, FT_CREATE); 304 TAILQ_INSERT_TAIL(&g->ft_threads, p, p_fut_link); 305 p->p_futex = g; 306 } 307 count++; 308 } 309 310 futex_put(f); 311 312 return count; 313 } 314 315 /* 316 * Wakeup at most ``n'' sibling threads sleeping on a futex at address 317 * ``uaddr''. 318 */ 319 int 320 futex_wake(uint32_t *uaddr, uint32_t n, int flags) 321 { 322 return futex_requeue(uaddr, n, NULL, 0, flags); 323 } 324