sys/kern/kern_umtx.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | /* * Copyright (c) 2003,2004,2010,2017 The DragonFly Project. * All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> and David Xu <davidxu@freebsd.org> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * This module implements userland mutex helper functions. umtx_sleep() * handling blocking and umtx_wakeup() handles wakeups. The sleep/wakeup * functions operate on user addresses. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/cdefs.h> #include <sys/kernel.h> #include <sys/sysmsg.h> #include <sys/sysent.h> #include <sys/syscall.h> #include <sys/sysctl.h> #include <sys/module.h> #include <sys/thread.h> #include <sys/proc.h> #include <cpu/lwbuf.h> #include <vm/vm.h> #include <vm/vm_param.h> #include <sys/lock.h> #include <vm/pmap.h> #include <vm/vm_map.h> #include <vm/vm_object.h> #include <vm/vm_page.h> #include <vm/vm_pager.h> #include <vm/vm_pageout.h> #include <vm/vm_extern.h> #include <vm/vm_kern.h> #include <vm/vm_page2.h> /* * Improve umtx performance by polling for 4000nS before going to sleep. * This can avoid many IPIs in typical pthreads mutex situations. */ #ifdef _RDTSC_SUPPORTED_ static int umtx_delay = 4000; /* nS */ SYSCTL_INT(_kern, OID_AUTO, umtx_delay, CTLFLAG_RW, &umtx_delay, 0, ""); #endif static int umtx_timeout_max = 2000000; /* microseconds */ SYSCTL_INT(_kern, OID_AUTO, umtx_timeout_max, CTLFLAG_RW, &umtx_timeout_max, 0, ""); /* * If the contents of the userland-supplied pointer matches the specified * value enter an interruptable sleep for up to <timeout> microseconds. * If the contents does not match then return immediately. * * Returns 0 if we slept and were woken up, -1 and EWOULDBLOCK if we slept * and timed out, and EBUSY if the contents of the pointer already does * not match the specified value. A timeout of 0 indicates an unlimited sleep. * EINTR is returned if the call was interrupted by a signal (even if * the signal specifies that the system call should restart). * * This function interlocks against call to umtx_wakeup. It does NOT interlock * against changes in *ptr. However, it does not have to. The standard use * of *ptr is to differentiate between an uncontested and a contested mutex * and call umtx_wakeup when releasing a contested mutex. Therefore we can * safely race against changes in *ptr as long as we are properly interlocked * against the umtx_wakeup() call. * * For performance reasons, we do not try to track the underlying page for * mapping changes. Instead, the timeout is capped at kern.umtx_timeout_max * (default 1 second) and the caller is expected to retry. The kernel * will wake all umtx_sleep()s if the process fork()s, but not if it vfork()s. * Other mapping changes must be caught by the timeout. * * umtx_sleep { const int *ptr, int value, int timeout } */ int sys_umtx_sleep(struct sysmsg *sysmsg, const struct umtx_sleep_args *uap) { void *waddr; void *uptr; int offset; int timeout; int error; int value; int fail_counter; thread_t td; volatile const int *ptr = uap->ptr; if (uap->timeout < 0) return (EINVAL); td = curthread; uptr = __DEQUALIFY(void *, ptr); if ((vm_offset_t)uptr & (sizeof(int) - 1)) return EFAULT; offset = (vm_offset_t)uptr & PAGE_MASK; /* * Resolve the physical address. We allow the case where there are * sometimes discontinuities (causing a 2 second retry timeout). */ retry_on_discontinuity: fail_counter = 10000; do { if (--fail_counter == 0) { kprintf("umtx_sleep() (X): ERROR Discontinuity %p (%s %d/%d)\n", uptr, td->td_comm, (int)td->td_proc->p_pid, (int)td->td_lwp->lwp_tid); return EINVAL; } value = fuwordadd32(uptr, 0); waddr = (void *)(intptr_t)uservtophys((intptr_t)uptr); } while (waddr == (void *)(intptr_t)-1 && value != -1); if (value == -1 && waddr == (void *)(intptr_t)-1) { kprintf("umtx_sleep() (A): WARNING can't translate %p (%s %d/%d)\n", uptr, td->td_comm, (int)td->td_proc->p_pid, (int)td->td_lwp->lwp_tid); return EINVAL; } error = EBUSY; if (value == uap->value) { #ifdef _RDTSC_SUPPORTED_ /* * Poll a little while before sleeping, most mutexes are * short-lived. */ if (umtx_delay) { int64_t tsc_target; int good = 0; tsc_target = tsc_get_target(umtx_delay); while (tsc_test_target(tsc_target) == 0) { cpu_lfence(); if (fuwordadd32(uptr, 0) != uap->value) { good = 1; break; } cpu_pause(); } if (good) { error = EBUSY; goto done; } } #endif /* * Calculate the timeout. This will be acccurate to within ~2 ticks. * uap->timeout is in microseconds. */ timeout = umtx_timeout_max; if (uap->timeout && uap->timeout < timeout) timeout = uap->timeout; timeout = (timeout / 1000000) * hz + ((timeout % 1000000) * hz + 999999) / 1000000; /* * Wake us up if the memory location COWs while we are sleeping. * Use a critical section to tighten up the interlock. Also, * tsleep_remove() requires the caller be in a critical section. */ crit_enter(); /* * We must interlock just before sleeping. If we interlock before * registration the lock operations done by the registration can * interfere with it. * * We cannot leave our interlock hanging on return because this * will interfere with umtx_wakeup() calls with limited wakeup * counts. */ tsleep_interlock(waddr, PCATCH | PDOMAIN_UMTX); /* * Check physical address changed */ cpu_lfence(); if ((void *)(intptr_t)uservtophys((intptr_t)uptr) != waddr) { crit_exit(); goto retry_on_discontinuity; } /* * Re-read value */ value = fuwordadd32(uptr, 0); if (value == uap->value) { error = tsleep(waddr, PCATCH | PINTERLOCKED | PDOMAIN_UMTX, "umtxsl", timeout); } else { error = EBUSY; } crit_exit(); /* Always break out in case of signal, even if restartable */ if (error == ERESTART) error = EINTR; } else { error = EBUSY; } done: return(error); } /* * umtx_wakeup { const int *ptr, int count } * * Wakeup the specified number of processes held in umtx_sleep() on the * specified user address. A count of 0 wakes up all waiting processes. */ int sys_umtx_wakeup(struct sysmsg *sysmsg, const struct umtx_wakeup_args *uap) { int offset; int error; int fail_counter; int32_t value; void *waddr; void *uptr; volatile const int *ptr = uap->ptr; thread_t td; td = curthread; /* * WARNING! We can only use vm_fault_page*() for reading data. We * cannot use it for writing data because there is no pmap * interlock to protect against flushes/pageouts. */ cpu_mfence(); if ((vm_offset_t)ptr & (sizeof(int) - 1)) return EFAULT; offset = (vm_offset_t)ptr & PAGE_MASK; uptr = __DEQUALIFY(void *, ptr); fail_counter = 10000; do { if (--fail_counter == 0) { kprintf("umtx_wakeup() (X): ERROR Discontinuity " "%p (%s %d/%d)\n", uptr, td->td_comm, (int)td->td_proc->p_pid, (int)td->td_lwp->lwp_tid); return EINVAL; } value = fuwordadd32(uptr, 0); waddr = (void *)(intptr_t)uservtophys((intptr_t)uptr); } while (waddr == (void *)(intptr_t)-1 && value != -1); if (value == -1 && waddr == (void *)(intptr_t)-1) { kprintf("umtx_wakeup() (A): WARNING can't translate %p (%s %d/%d)\n", uptr, td->td_comm, (int)td->td_proc->p_pid, (int)td->td_lwp->lwp_tid); return EINVAL; } if (uap->count == 1) { wakeup_domain_one(waddr, PDOMAIN_UMTX); } else { /* XXX wakes them all up for now */ wakeup_domain(waddr, PDOMAIN_UMTX); } error = 0; return(error); } |