1139804Simp/*- 2126324Sjhb * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org> 3126324Sjhb * All rights reserved. 4126324Sjhb * 5126324Sjhb * Redistribution and use in source and binary forms, with or without 6126324Sjhb * modification, are permitted provided that the following conditions 7126324Sjhb * are met: 8126324Sjhb * 1. Redistributions of source code must retain the above copyright 9126324Sjhb * notice, this list of conditions and the following disclaimer. 10126324Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11126324Sjhb * notice, this list of conditions and the following disclaimer in the 12126324Sjhb * documentation and/or other materials provided with the distribution. 13126324Sjhb * 14126324Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15126324Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16126324Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17126324Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18126324Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19126324Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20126324Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21126324Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22126324Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23126324Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24126324Sjhb * SUCH DAMAGE. 25126324Sjhb */ 26126324Sjhb 27126324Sjhb/* 28126324Sjhb * Implementation of sleep queues used to hold queue of threads blocked on 29126324Sjhb * a wait channel. Sleep queues different from turnstiles in that wait 30126324Sjhb * channels are not owned by anyone, so there is no priority propagation. 31126324Sjhb * Sleep queues can also provide a timeout and can also be interrupted by 32126324Sjhb * signals. That said, there are several similarities between the turnstile 33126324Sjhb * and sleep queue implementations. (Note: turnstiles were implemented 34126324Sjhb * first.) For example, both use a hash table of the same size where each 35126324Sjhb * bucket is referred to as a "chain" that contains both a spin lock and 36126324Sjhb * a linked list of queues. An individual queue is located by using a hash 37126324Sjhb * to pick a chain, locking the chain, and then walking the chain searching 38126324Sjhb * for the queue. This means that a wait channel object does not need to 39126324Sjhb * embed it's queue head just as locks do not embed their turnstile queue 40126324Sjhb * head. Threads also carry around a sleep queue that they lend to the 41126324Sjhb * wait channel when blocking. Just as in turnstiles, the queue includes 42126324Sjhb * a free list of the sleep queues of other threads blocked on the same 43126324Sjhb * wait channel in the case of multiple waiters. 44126324Sjhb * 45126324Sjhb * Some additional functionality provided by sleep queues include the 46126324Sjhb * ability to set a timeout. The timeout is managed using a per-thread 47126324Sjhb * callout that resumes a thread if it is asleep. A thread may also 48126324Sjhb * catch signals while it is asleep (aka an interruptible sleep). The 49126324Sjhb * signal code uses sleepq_abort() to interrupt a sleeping thread. Finally, 50126324Sjhb * sleep queues also provide some extra assertions. One is not allowed to 51126324Sjhb * mix the sleep/wakeup and cv APIs for a given wait channel. Also, one 52126324Sjhb * must consistently use the same lock to synchronize with a wait channel, 53126324Sjhb * though this check is currently only a warning for sleep/wakeup due to 54126324Sjhb * pre-existing abuse of that API. The same lock must also be held when 55126324Sjhb * awakening threads, though that is currently only enforced for condition 56126324Sjhb * variables. 57126324Sjhb */ 58126324Sjhb 59126324Sjhb#include <sys/cdefs.h> 60126324Sjhb__FBSDID("$FreeBSD: releng/11.0/sys/kern/subr_sleepqueue.c 302350 2016-07-05 18:47:17Z glebius $"); 61126324Sjhb 62154936Sjhb#include "opt_sleepqueue_profiling.h" 63154936Sjhb#include "opt_ddb.h" 64170640Sjeff#include "opt_sched.h" 65296973Scem#include "opt_stack.h" 66154936Sjhb 67126324Sjhb#include <sys/param.h> 68126324Sjhb#include <sys/systm.h> 69126324Sjhb#include <sys/lock.h> 70126324Sjhb#include <sys/kernel.h> 71126324Sjhb#include <sys/ktr.h> 72126324Sjhb#include <sys/mutex.h> 73126324Sjhb#include <sys/proc.h> 74177372Sjeff#include <sys/sbuf.h> 75126324Sjhb#include <sys/sched.h> 76235459Srstone#include <sys/sdt.h> 77126324Sjhb#include <sys/signalvar.h> 78126324Sjhb#include <sys/sleepqueue.h> 79296927Scem#include <sys/stack.h> 80131259Sjhb#include <sys/sysctl.h> 81126324Sjhb 82169666Sjeff#include <vm/uma.h> 83169666Sjeff 84154936Sjhb#ifdef DDB 85154936Sjhb#include <ddb/ddb.h> 86154936Sjhb#endif 87154936Sjhb 88296927Scem 89126324Sjhb/* 90248186Smav * Constants for the hash table of sleep queue chains. 91248186Smav * SC_TABLESIZE must be a power of two for SC_MASK to work properly. 92126324Sjhb */ 93248186Smav#define SC_TABLESIZE 256 /* Must be power of 2. */ 94126324Sjhb#define SC_MASK (SC_TABLESIZE - 1) 95126324Sjhb#define SC_SHIFT 8 96248186Smav#define SC_HASH(wc) ((((uintptr_t)(wc) >> SC_SHIFT) ^ (uintptr_t)(wc)) & \ 97248186Smav SC_MASK) 98126324Sjhb#define SC_LOOKUP(wc) &sleepq_chains[SC_HASH(wc)] 99165272Skmacy#define NR_SLEEPQS 2 100126324Sjhb/* 101126324Sjhb * There two different lists of sleep queues. Both lists are connected 102126324Sjhb * via the sq_hash entries. The first list is the sleep queue chain list 103126324Sjhb * that a sleep queue is on when it is attached to a wait channel. The 104126324Sjhb * second list is the free list hung off of a sleep queue that is attached 105126324Sjhb * to a wait channel. 106126324Sjhb * 107126324Sjhb * Each sleep queue also contains the wait channel it is attached to, the 108126324Sjhb * list of threads blocked on that wait channel, flags specific to the 109126324Sjhb * wait channel, and the lock used to synchronize with a wait channel. 110126324Sjhb * The flags are used to catch mismatches between the various consumers 111126324Sjhb * of the sleep queue API (e.g. sleep/wakeup and condition variables). 112126324Sjhb * The lock pointer is only used when invariants are enabled for various 113126324Sjhb * debugging checks. 114126324Sjhb * 115126324Sjhb * Locking key: 116126324Sjhb * c - sleep queue chain lock 117126324Sjhb */ 118126324Sjhbstruct sleepqueue { 119165272Skmacy TAILQ_HEAD(, thread) sq_blocked[NR_SLEEPQS]; /* (c) Blocked threads. */ 120200447Sattilio u_int sq_blockedcnt[NR_SLEEPQS]; /* (c) N. of blocked threads. */ 121126324Sjhb LIST_ENTRY(sleepqueue) sq_hash; /* (c) Chain and free list. */ 122126324Sjhb LIST_HEAD(, sleepqueue) sq_free; /* (c) Free queues. */ 123126324Sjhb void *sq_wchan; /* (c) Wait channel. */ 124201879Sattilio int sq_type; /* (c) Queue type. */ 125136445Sjhb#ifdef INVARIANTS 126164325Spjd struct lock_object *sq_lock; /* (c) Associated lock. */ 127126324Sjhb#endif 128126324Sjhb}; 129126324Sjhb 130126324Sjhbstruct sleepqueue_chain { 131126324Sjhb LIST_HEAD(, sleepqueue) sc_queues; /* List of sleep queues. */ 132126324Sjhb struct mtx sc_lock; /* Spin lock for this chain. */ 133131259Sjhb#ifdef SLEEPQUEUE_PROFILING 134131259Sjhb u_int sc_depth; /* Length of sc_queues. */ 135131259Sjhb u_int sc_max_depth; /* Max length of sc_queues. */ 136131259Sjhb#endif 137126324Sjhb}; 138126324Sjhb 139131259Sjhb#ifdef SLEEPQUEUE_PROFILING 140131259Sjhbu_int sleepq_max_depth; 141227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, sleepq, CTLFLAG_RD, 0, "sleepq profiling"); 142227309Sedstatic SYSCTL_NODE(_debug_sleepq, OID_AUTO, chains, CTLFLAG_RD, 0, 143131259Sjhb "sleepq chain stats"); 144131259SjhbSYSCTL_UINT(_debug_sleepq, OID_AUTO, max_depth, CTLFLAG_RD, &sleepq_max_depth, 145131259Sjhb 0, "maxmimum depth achieved of a single chain"); 146177372Sjeff 147177372Sjeffstatic void sleepq_profile(const char *wmesg); 148177372Sjeffstatic int prof_enabled; 149131259Sjhb#endif 150126324Sjhbstatic struct sleepqueue_chain sleepq_chains[SC_TABLESIZE]; 151169666Sjeffstatic uma_zone_t sleepq_zone; 152126324Sjhb 153126324Sjhb/* 154126324Sjhb * Prototypes for non-exported routines. 155126324Sjhb */ 156177085Sjeffstatic int sleepq_catch_signals(void *wchan, int pri); 157165272Skmacystatic int sleepq_check_signals(void); 158277528Shselaskystatic int sleepq_check_timeout(void); 159169666Sjeff#ifdef INVARIANTS 160169666Sjeffstatic void sleepq_dtor(void *mem, int size, void *arg); 161169666Sjeff#endif 162169666Sjeffstatic int sleepq_init(void *mem, int size, int flags); 163181334Sjhbstatic int sleepq_resume_thread(struct sleepqueue *sq, struct thread *td, 164169666Sjeff int pri); 165177085Sjeffstatic void sleepq_switch(void *wchan, int pri); 166126324Sjhbstatic void sleepq_timeout(void *arg); 167126324Sjhb 168235459SrstoneSDT_PROBE_DECLARE(sched, , , sleep); 169235459SrstoneSDT_PROBE_DECLARE(sched, , , wakeup); 170235459Srstone 171126324Sjhb/* 172267820Sattilio * Initialize SLEEPQUEUE_PROFILING specific sysctl nodes. 173267820Sattilio * Note that it must happen after sleepinit() has been fully executed, so 174267820Sattilio * it must happen after SI_SUB_KMEM SYSINIT() subsystem setup. 175126324Sjhb */ 176267820Sattilio#ifdef SLEEPQUEUE_PROFILING 177267820Sattiliostatic void 178267820Sattilioinit_sleepqueue_profiling(void) 179126324Sjhb{ 180267820Sattilio char chain_name[10]; 181131259Sjhb struct sysctl_oid *chain_oid; 182267820Sattilio u_int i; 183126324Sjhb 184126324Sjhb for (i = 0; i < SC_TABLESIZE; i++) { 185267820Sattilio snprintf(chain_name, sizeof(chain_name), "%u", i); 186131259Sjhb chain_oid = SYSCTL_ADD_NODE(NULL, 187131259Sjhb SYSCTL_STATIC_CHILDREN(_debug_sleepq_chains), OID_AUTO, 188131259Sjhb chain_name, CTLFLAG_RD, NULL, "sleepq chain stats"); 189131259Sjhb SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO, 190131259Sjhb "depth", CTLFLAG_RD, &sleepq_chains[i].sc_depth, 0, NULL); 191131259Sjhb SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO, 192131259Sjhb "max_depth", CTLFLAG_RD, &sleepq_chains[i].sc_max_depth, 0, 193131259Sjhb NULL); 194267820Sattilio } 195267820Sattilio} 196267820Sattilio 197267820SattilioSYSINIT(sleepqueue_profiling, SI_SUB_LOCK, SI_ORDER_ANY, 198267820Sattilio init_sleepqueue_profiling, NULL); 199131259Sjhb#endif 200267820Sattilio 201267820Sattilio/* 202267820Sattilio * Early initialization of sleep queues that is called from the sleepinit() 203267820Sattilio * SYSINIT. 204267820Sattilio */ 205267820Sattiliovoid 206267820Sattilioinit_sleepqueues(void) 207267820Sattilio{ 208267820Sattilio int i; 209267820Sattilio 210267820Sattilio for (i = 0; i < SC_TABLESIZE; i++) { 211267820Sattilio LIST_INIT(&sleepq_chains[i].sc_queues); 212267820Sattilio mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL, 213267820Sattilio MTX_SPIN | MTX_RECURSE); 214126324Sjhb } 215169666Sjeff sleepq_zone = uma_zcreate("SLEEPQUEUE", sizeof(struct sleepqueue), 216169666Sjeff#ifdef INVARIANTS 217169666Sjeff NULL, sleepq_dtor, sleepq_init, NULL, UMA_ALIGN_CACHE, 0); 218169666Sjeff#else 219169666Sjeff NULL, NULL, sleepq_init, NULL, UMA_ALIGN_CACHE, 0); 220169666Sjeff#endif 221169666Sjeff 222126324Sjhb thread0.td_sleepqueue = sleepq_alloc(); 223126324Sjhb} 224126324Sjhb 225126324Sjhb/* 226169666Sjeff * Get a sleep queue for a new thread. 227126324Sjhb */ 228126324Sjhbstruct sleepqueue * 229126324Sjhbsleepq_alloc(void) 230126324Sjhb{ 231126324Sjhb 232169666Sjeff return (uma_zalloc(sleepq_zone, M_WAITOK)); 233126324Sjhb} 234126324Sjhb 235126324Sjhb/* 236126324Sjhb * Free a sleep queue when a thread is destroyed. 237126324Sjhb */ 238126324Sjhbvoid 239126324Sjhbsleepq_free(struct sleepqueue *sq) 240126324Sjhb{ 241126324Sjhb 242169666Sjeff uma_zfree(sleepq_zone, sq); 243126324Sjhb} 244126324Sjhb 245126324Sjhb/* 246136445Sjhb * Lock the sleep queue chain associated with the specified wait channel. 247136445Sjhb */ 248136445Sjhbvoid 249136445Sjhbsleepq_lock(void *wchan) 250136445Sjhb{ 251136445Sjhb struct sleepqueue_chain *sc; 252136445Sjhb 253136445Sjhb sc = SC_LOOKUP(wchan); 254136445Sjhb mtx_lock_spin(&sc->sc_lock); 255136445Sjhb} 256136445Sjhb 257136445Sjhb/* 258126324Sjhb * Look up the sleep queue associated with a given wait channel in the hash 259136445Sjhb * table locking the associated sleep queue chain. If no queue is found in 260136445Sjhb * the table, NULL is returned. 261126324Sjhb */ 262126324Sjhbstruct sleepqueue * 263126324Sjhbsleepq_lookup(void *wchan) 264126324Sjhb{ 265126324Sjhb struct sleepqueue_chain *sc; 266126324Sjhb struct sleepqueue *sq; 267126324Sjhb 268126324Sjhb KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); 269126324Sjhb sc = SC_LOOKUP(wchan); 270136445Sjhb mtx_assert(&sc->sc_lock, MA_OWNED); 271126324Sjhb LIST_FOREACH(sq, &sc->sc_queues, sq_hash) 272126324Sjhb if (sq->sq_wchan == wchan) 273126324Sjhb return (sq); 274126324Sjhb return (NULL); 275126324Sjhb} 276126324Sjhb 277126324Sjhb/* 278126324Sjhb * Unlock the sleep queue chain associated with a given wait channel. 279126324Sjhb */ 280126324Sjhbvoid 281126324Sjhbsleepq_release(void *wchan) 282126324Sjhb{ 283126324Sjhb struct sleepqueue_chain *sc; 284126324Sjhb 285126324Sjhb sc = SC_LOOKUP(wchan); 286126324Sjhb mtx_unlock_spin(&sc->sc_lock); 287126324Sjhb} 288126324Sjhb 289126324Sjhb/* 290137277Sjhb * Places the current thread on the sleep queue for the specified wait 291126324Sjhb * channel. If INVARIANTS is enabled, then it associates the passed in 292126324Sjhb * lock with the sleepq to make sure it is held when that sleep queue is 293126324Sjhb * woken up. 294126324Sjhb */ 295126324Sjhbvoid 296165272Skmacysleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, int flags, 297165272Skmacy int queue) 298126324Sjhb{ 299126324Sjhb struct sleepqueue_chain *sc; 300136445Sjhb struct sleepqueue *sq; 301137277Sjhb struct thread *td; 302126324Sjhb 303126324Sjhb td = curthread; 304126324Sjhb sc = SC_LOOKUP(wchan); 305126324Sjhb mtx_assert(&sc->sc_lock, MA_OWNED); 306126324Sjhb MPASS(td->td_sleepqueue != NULL); 307126324Sjhb MPASS(wchan != NULL); 308165272Skmacy MPASS((queue >= 0) && (queue < NR_SLEEPQS)); 309126324Sjhb 310150177Sjhb /* If this thread is not allowed to sleep, die a horrible death. */ 311247588Sjhb KASSERT(td->td_no_sleeping == 0, 312247588Sjhb ("%s: td %p to sleep on wchan %p with sleeping prohibited", 313240423Sattilio __func__, td, wchan)); 314150177Sjhb 315136445Sjhb /* Look up the sleep queue associated with the wait channel 'wchan'. */ 316136445Sjhb sq = sleepq_lookup(wchan); 317136445Sjhb 318136445Sjhb /* 319136445Sjhb * If the wait channel does not already have a sleep queue, use 320136445Sjhb * this thread's sleep queue. Otherwise, insert the current thread 321136445Sjhb * into the sleep queue already in use by this wait channel. 322136445Sjhb */ 323126324Sjhb if (sq == NULL) { 324165272Skmacy#ifdef INVARIANTS 325165292Skmacy int i; 326165291Sache 327165292Skmacy sq = td->td_sleepqueue; 328200447Sattilio for (i = 0; i < NR_SLEEPQS; i++) { 329165292Skmacy KASSERT(TAILQ_EMPTY(&sq->sq_blocked[i]), 330200447Sattilio ("thread's sleep queue %d is not empty", i)); 331200447Sattilio KASSERT(sq->sq_blockedcnt[i] == 0, 332200447Sattilio ("thread's sleep queue %d count mismatches", i)); 333200447Sattilio } 334165272Skmacy KASSERT(LIST_EMPTY(&sq->sq_free), 335165272Skmacy ("thread's sleep queue has a non-empty free list")); 336165272Skmacy KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer")); 337165292Skmacy sq->sq_lock = lock; 338165272Skmacy#endif 339131259Sjhb#ifdef SLEEPQUEUE_PROFILING 340131259Sjhb sc->sc_depth++; 341131259Sjhb if (sc->sc_depth > sc->sc_max_depth) { 342131259Sjhb sc->sc_max_depth = sc->sc_depth; 343131259Sjhb if (sc->sc_max_depth > sleepq_max_depth) 344131259Sjhb sleepq_max_depth = sc->sc_max_depth; 345131259Sjhb } 346131259Sjhb#endif 347165292Skmacy sq = td->td_sleepqueue; 348126324Sjhb LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash); 349126324Sjhb sq->sq_wchan = wchan; 350201879Sattilio sq->sq_type = flags & SLEEPQ_TYPE; 351126324Sjhb } else { 352126324Sjhb MPASS(wchan == sq->sq_wchan); 353126488Sjhb MPASS(lock == sq->sq_lock); 354136445Sjhb MPASS((flags & SLEEPQ_TYPE) == sq->sq_type); 355126324Sjhb LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash); 356126324Sjhb } 357172155Sattilio thread_lock(td); 358165272Skmacy TAILQ_INSERT_TAIL(&sq->sq_blocked[queue], td, td_slpq); 359200447Sattilio sq->sq_blockedcnt[queue]++; 360126324Sjhb td->td_sleepqueue = NULL; 361165272Skmacy td->td_sqqueue = queue; 362126324Sjhb td->td_wchan = wchan; 363126324Sjhb td->td_wmesg = wmesg; 364155741Sdavidxu if (flags & SLEEPQ_INTERRUPTIBLE) { 365134013Sjhb td->td_flags |= TDF_SINTR; 366155741Sdavidxu td->td_flags &= ~TDF_SLEEPABORT; 367155741Sdavidxu } 368172155Sattilio thread_unlock(td); 369126324Sjhb} 370126324Sjhb 371126324Sjhb/* 372126324Sjhb * Sets a timeout that will remove the current thread from the specified 373126324Sjhb * sleep queue after timo ticks if the thread has not already been awakened. 374126324Sjhb */ 375126324Sjhbvoid 376247783Sdavidesleepq_set_timeout_sbt(void *wchan, sbintime_t sbt, sbintime_t pr, 377247783Sdavide int flags) 378126324Sjhb{ 379277528Shselasky struct sleepqueue_chain *sc; 380126324Sjhb struct thread *td; 381126324Sjhb 382126324Sjhb td = curthread; 383277528Shselasky sc = SC_LOOKUP(wchan); 384277528Shselasky mtx_assert(&sc->sc_lock, MA_OWNED); 385277528Shselasky MPASS(TD_ON_SLEEPQ(td)); 386277528Shselasky MPASS(td->td_sleepqueue == NULL); 387277528Shselasky MPASS(wchan != NULL); 388297466Sjhb if (cold) 389297466Sjhb panic("timed sleep before timers are working"); 390247783Sdavide callout_reset_sbt_on(&td->td_slpcallout, sbt, pr, 391247783Sdavide sleepq_timeout, td, PCPU_GET(cpuid), flags | C_DIRECT_EXEC); 392126324Sjhb} 393126324Sjhb 394126324Sjhb/* 395200447Sattilio * Return the number of actual sleepers for the specified queue. 396200447Sattilio */ 397200447Sattiliou_int 398200447Sattiliosleepq_sleepcnt(void *wchan, int queue) 399200447Sattilio{ 400200447Sattilio struct sleepqueue *sq; 401200447Sattilio 402200447Sattilio KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); 403200447Sattilio MPASS((queue >= 0) && (queue < NR_SLEEPQS)); 404200447Sattilio sq = sleepq_lookup(wchan); 405200447Sattilio if (sq == NULL) 406200447Sattilio return (0); 407200447Sattilio return (sq->sq_blockedcnt[queue]); 408200447Sattilio} 409200447Sattilio 410200447Sattilio/* 411126324Sjhb * Marks the pending sleep of the current thread as interruptible and 412126324Sjhb * makes an initial check for pending signals before putting a thread 413170294Sjeff * to sleep. Enters and exits with the thread lock held. Thread lock 414170294Sjeff * may have transitioned from the sleepq lock to a run lock. 415126324Sjhb */ 416155741Sdavidxustatic int 417177085Sjeffsleepq_catch_signals(void *wchan, int pri) 418126324Sjhb{ 419126324Sjhb struct sleepqueue_chain *sc; 420126324Sjhb struct sleepqueue *sq; 421126324Sjhb struct thread *td; 422126324Sjhb struct proc *p; 423155741Sdavidxu struct sigacts *ps; 424248470Sjhb int sig, ret; 425126324Sjhb 426126324Sjhb td = curthread; 427155741Sdavidxu p = curproc; 428126324Sjhb sc = SC_LOOKUP(wchan); 429126324Sjhb mtx_assert(&sc->sc_lock, MA_OWNED); 430126324Sjhb MPASS(wchan != NULL); 431211523Sdavidxu if ((td->td_pflags & TDP_WAKEUP) != 0) { 432211523Sdavidxu td->td_pflags &= ~TDP_WAKEUP; 433211523Sdavidxu ret = EINTR; 434211534Sdavidxu thread_lock(td); 435211523Sdavidxu goto out; 436211523Sdavidxu } 437211523Sdavidxu 438177375Sjeff /* 439177375Sjeff * See if there are any pending signals for this thread. If not 440177375Sjeff * we can switch immediately. Otherwise do the signal processing 441177375Sjeff * directly. 442177375Sjeff */ 443177375Sjeff thread_lock(td); 444177471Sjeff if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0) { 445177375Sjeff sleepq_switch(wchan, pri); 446177375Sjeff return (0); 447177375Sjeff } 448177375Sjeff thread_unlock(td); 449177375Sjeff mtx_unlock_spin(&sc->sc_lock); 450129241Sbde CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)", 451173601Sjulian (void *)td, (long)p->p_pid, td->td_name); 452126324Sjhb PROC_LOCK(p); 453155741Sdavidxu ps = p->p_sigacts; 454155741Sdavidxu mtx_lock(&ps->ps_mtx); 455248470Sjhb sig = cursig(td); 456302328Skib if (sig == -1) { 457155741Sdavidxu mtx_unlock(&ps->ps_mtx); 458302328Skib KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY")); 459302328Skib KASSERT(TD_SBDRY_INTR(td), 460302328Skib ("lost TDF_SERESTART of TDF_SEINTR")); 461302328Skib KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) != 462302328Skib (TDF_SEINTR | TDF_SERESTART), 463302328Skib ("both TDF_SEINTR and TDF_SERESTART")); 464302328Skib ret = TD_SBDRY_ERRNO(td); 465302328Skib } else if (sig == 0) { 466302328Skib mtx_unlock(&ps->ps_mtx); 467155741Sdavidxu ret = thread_suspend_check(1); 468155741Sdavidxu MPASS(ret == 0 || ret == EINTR || ret == ERESTART); 469155741Sdavidxu } else { 470155741Sdavidxu if (SIGISMEMBER(ps->ps_sigintr, sig)) 471155741Sdavidxu ret = EINTR; 472155741Sdavidxu else 473155741Sdavidxu ret = ERESTART; 474155741Sdavidxu mtx_unlock(&ps->ps_mtx); 475155741Sdavidxu } 476184667Sdavidxu /* 477184667Sdavidxu * Lock the per-process spinlock prior to dropping the PROC_LOCK 478184667Sdavidxu * to avoid a signal delivery race. PROC_LOCK, PROC_SLOCK, and 479209612Sjhb * thread_lock() are currently held in tdsendsignal(). 480184667Sdavidxu */ 481184667Sdavidxu PROC_SLOCK(p); 482170294Sjeff mtx_lock_spin(&sc->sc_lock); 483184667Sdavidxu PROC_UNLOCK(p); 484170294Sjeff thread_lock(td); 485184667Sdavidxu PROC_SUNLOCK(p); 486185502Sdavidxu if (ret == 0) { 487185502Sdavidxu sleepq_switch(wchan, pri); 488185502Sdavidxu return (0); 489185502Sdavidxu } 490211523Sdavidxuout: 491155936Sdavidxu /* 492155936Sdavidxu * There were pending signals and this thread is still 493155936Sdavidxu * on the sleep queue, remove it from the sleep queue. 494155936Sdavidxu */ 495170294Sjeff if (TD_ON_SLEEPQ(td)) { 496170294Sjeff sq = sleepq_lookup(wchan); 497181334Sjhb if (sleepq_resume_thread(sq, td, 0)) { 498181334Sjhb#ifdef INVARIANTS 499181334Sjhb /* 500181334Sjhb * This thread hasn't gone to sleep yet, so it 501181334Sjhb * should not be swapped out. 502181334Sjhb */ 503181334Sjhb panic("not waking up swapper"); 504181334Sjhb#endif 505181334Sjhb } 506170294Sjeff } 507170294Sjeff mtx_unlock_spin(&sc->sc_lock); 508170294Sjeff MPASS(td->td_lock != &sc->sc_lock); 509155741Sdavidxu return (ret); 510126324Sjhb} 511126324Sjhb 512126324Sjhb/* 513170294Sjeff * Switches to another thread if we are still asleep on a sleep queue. 514170294Sjeff * Returns with thread lock. 515126324Sjhb */ 516126324Sjhbstatic void 517177085Sjeffsleepq_switch(void *wchan, int pri) 518126324Sjhb{ 519126324Sjhb struct sleepqueue_chain *sc; 520175654Sjhb struct sleepqueue *sq; 521126324Sjhb struct thread *td; 522126324Sjhb 523126324Sjhb td = curthread; 524126324Sjhb sc = SC_LOOKUP(wchan); 525126324Sjhb mtx_assert(&sc->sc_lock, MA_OWNED); 526170294Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 527175654Sjhb 528175654Sjhb /* 529175654Sjhb * If we have a sleep queue, then we've already been woken up, so 530175654Sjhb * just return. 531175654Sjhb */ 532126324Sjhb if (td->td_sleepqueue != NULL) { 533126324Sjhb mtx_unlock_spin(&sc->sc_lock); 534126324Sjhb return; 535126324Sjhb } 536175654Sjhb 537175654Sjhb /* 538175654Sjhb * If TDF_TIMEOUT is set, then our sleep has been timed out 539175654Sjhb * already but we are still on the sleep queue, so dequeue the 540175654Sjhb * thread and return. 541175654Sjhb */ 542175654Sjhb if (td->td_flags & TDF_TIMEOUT) { 543175654Sjhb MPASS(TD_ON_SLEEPQ(td)); 544175654Sjhb sq = sleepq_lookup(wchan); 545181334Sjhb if (sleepq_resume_thread(sq, td, 0)) { 546181334Sjhb#ifdef INVARIANTS 547181334Sjhb /* 548181334Sjhb * This thread hasn't gone to sleep yet, so it 549181334Sjhb * should not be swapped out. 550181334Sjhb */ 551181334Sjhb panic("not waking up swapper"); 552181334Sjhb#endif 553181334Sjhb } 554175654Sjhb mtx_unlock_spin(&sc->sc_lock); 555175654Sjhb return; 556175654Sjhb } 557177372Sjeff#ifdef SLEEPQUEUE_PROFILING 558177372Sjeff if (prof_enabled) 559177372Sjeff sleepq_profile(td->td_wmesg); 560177372Sjeff#endif 561177085Sjeff MPASS(td->td_sleepqueue == NULL); 562177085Sjeff sched_sleep(td, pri); 563170294Sjeff thread_lock_set(td, &sc->sc_lock); 564235459Srstone SDT_PROBE0(sched, , , sleep); 565126324Sjhb TD_SET_SLEEPING(td); 566178272Sjeff mi_switch(SW_VOL | SWT_SLEEPQ, NULL); 567126324Sjhb KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING")); 568129241Sbde CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)", 569173600Sjulian (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name); 570126324Sjhb} 571126324Sjhb 572126324Sjhb/* 573126324Sjhb * Check to see if we timed out. 574126324Sjhb */ 575126324Sjhbstatic int 576277528Shselaskysleepq_check_timeout(void) 577126324Sjhb{ 578277528Shselasky struct thread *td; 579277528Shselasky 580277528Shselasky td = curthread; 581170294Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 582126324Sjhb 583126324Sjhb /* 584126324Sjhb * If TDF_TIMEOUT is set, we timed out. 585126324Sjhb */ 586126324Sjhb if (td->td_flags & TDF_TIMEOUT) { 587126324Sjhb td->td_flags &= ~TDF_TIMEOUT; 588126324Sjhb return (EWOULDBLOCK); 589126324Sjhb } 590277528Shselasky 591277528Shselasky /* 592277528Shselasky * If TDF_TIMOFAIL is set, the timeout ran after we had 593277528Shselasky * already been woken up. 594277528Shselasky */ 595277528Shselasky if (td->td_flags & TDF_TIMOFAIL) 596277528Shselasky td->td_flags &= ~TDF_TIMOFAIL; 597277528Shselasky 598277528Shselasky /* 599277528Shselasky * If callout_stop() fails, then the timeout is running on 600277528Shselasky * another CPU, so synchronize with it to avoid having it 601277528Shselasky * accidentally wake up a subsequent sleep. 602277528Shselasky */ 603302350Sglebius else if (_callout_stop_safe(&td->td_slpcallout, CS_EXECUTING, NULL) 604296320Skib == 0) { 605277528Shselasky td->td_flags |= TDF_TIMEOUT; 606277528Shselasky TD_SET_SLEEPING(td); 607277528Shselasky mi_switch(SW_INVOL | SWT_SLEEPQTIMO, NULL); 608277528Shselasky } 609126324Sjhb return (0); 610126324Sjhb} 611126324Sjhb 612126324Sjhb/* 613126324Sjhb * Check to see if we were awoken by a signal. 614126324Sjhb */ 615126324Sjhbstatic int 616126324Sjhbsleepq_check_signals(void) 617126324Sjhb{ 618126324Sjhb struct thread *td; 619126324Sjhb 620126324Sjhb td = curthread; 621170294Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 622126324Sjhb 623126324Sjhb /* We are no longer in an interruptible sleep. */ 624155741Sdavidxu if (td->td_flags & TDF_SINTR) 625246417Sjhb td->td_flags &= ~TDF_SINTR; 626126324Sjhb 627155741Sdavidxu if (td->td_flags & TDF_SLEEPABORT) { 628155741Sdavidxu td->td_flags &= ~TDF_SLEEPABORT; 629155741Sdavidxu return (td->td_intrval); 630155741Sdavidxu } 631155741Sdavidxu 632126324Sjhb return (0); 633126324Sjhb} 634126324Sjhb 635126324Sjhb/* 636126324Sjhb * Block the current thread until it is awakened from its sleep queue. 637126324Sjhb */ 638126324Sjhbvoid 639177085Sjeffsleepq_wait(void *wchan, int pri) 640126324Sjhb{ 641170294Sjeff struct thread *td; 642126324Sjhb 643170294Sjeff td = curthread; 644170294Sjeff MPASS(!(td->td_flags & TDF_SINTR)); 645170294Sjeff thread_lock(td); 646177085Sjeff sleepq_switch(wchan, pri); 647170294Sjeff thread_unlock(td); 648126324Sjhb} 649126324Sjhb 650126324Sjhb/* 651126324Sjhb * Block the current thread until it is awakened from its sleep queue 652126324Sjhb * or it is interrupted by a signal. 653126324Sjhb */ 654126324Sjhbint 655177085Sjeffsleepq_wait_sig(void *wchan, int pri) 656126324Sjhb{ 657155741Sdavidxu int rcatch; 658126324Sjhb int rval; 659126324Sjhb 660177085Sjeff rcatch = sleepq_catch_signals(wchan, pri); 661126324Sjhb rval = sleepq_check_signals(); 662170294Sjeff thread_unlock(curthread); 663155741Sdavidxu if (rcatch) 664155741Sdavidxu return (rcatch); 665126324Sjhb return (rval); 666126324Sjhb} 667126324Sjhb 668126324Sjhb/* 669126324Sjhb * Block the current thread until it is awakened from its sleep queue 670126324Sjhb * or it times out while waiting. 671126324Sjhb */ 672126324Sjhbint 673177085Sjeffsleepq_timedwait(void *wchan, int pri) 674126324Sjhb{ 675170294Sjeff struct thread *td; 676126324Sjhb int rval; 677126324Sjhb 678170294Sjeff td = curthread; 679170294Sjeff MPASS(!(td->td_flags & TDF_SINTR)); 680170294Sjeff thread_lock(td); 681177085Sjeff sleepq_switch(wchan, pri); 682277528Shselasky rval = sleepq_check_timeout(); 683170294Sjeff thread_unlock(td); 684170294Sjeff 685131249Sjhb return (rval); 686126324Sjhb} 687126324Sjhb 688126324Sjhb/* 689126324Sjhb * Block the current thread until it is awakened from its sleep queue, 690126324Sjhb * it is interrupted by a signal, or it times out waiting to be awakened. 691126324Sjhb */ 692126324Sjhbint 693177085Sjeffsleepq_timedwait_sig(void *wchan, int pri) 694126324Sjhb{ 695155741Sdavidxu int rcatch, rvalt, rvals; 696126324Sjhb 697177085Sjeff rcatch = sleepq_catch_signals(wchan, pri); 698277528Shselasky rvalt = sleepq_check_timeout(); 699126324Sjhb rvals = sleepq_check_signals(); 700277528Shselasky thread_unlock(curthread); 701155741Sdavidxu if (rcatch) 702155741Sdavidxu return (rcatch); 703155741Sdavidxu if (rvals) 704126324Sjhb return (rvals); 705155741Sdavidxu return (rvalt); 706126324Sjhb} 707126324Sjhb 708126324Sjhb/* 709201879Sattilio * Returns the type of sleepqueue given a waitchannel. 710201879Sattilio */ 711201879Sattilioint 712201879Sattiliosleepq_type(void *wchan) 713201879Sattilio{ 714201879Sattilio struct sleepqueue *sq; 715201879Sattilio int type; 716201879Sattilio 717201879Sattilio MPASS(wchan != NULL); 718201879Sattilio 719201879Sattilio sleepq_lock(wchan); 720201879Sattilio sq = sleepq_lookup(wchan); 721201879Sattilio if (sq == NULL) { 722201879Sattilio sleepq_release(wchan); 723201879Sattilio return (-1); 724201879Sattilio } 725201879Sattilio type = sq->sq_type; 726201879Sattilio sleepq_release(wchan); 727201879Sattilio return (type); 728201879Sattilio} 729201879Sattilio 730201879Sattilio/* 731145056Sjhb * Removes a thread from a sleep queue and makes it 732145056Sjhb * runnable. 733126324Sjhb */ 734181334Sjhbstatic int 735145056Sjhbsleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri) 736126324Sjhb{ 737126324Sjhb struct sleepqueue_chain *sc; 738126324Sjhb 739126324Sjhb MPASS(td != NULL); 740126324Sjhb MPASS(sq->sq_wchan != NULL); 741126324Sjhb MPASS(td->td_wchan == sq->sq_wchan); 742165272Skmacy MPASS(td->td_sqqueue < NR_SLEEPQS && td->td_sqqueue >= 0); 743170294Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 744126324Sjhb sc = SC_LOOKUP(sq->sq_wchan); 745126324Sjhb mtx_assert(&sc->sc_lock, MA_OWNED); 746126324Sjhb 747235459Srstone SDT_PROBE2(sched, , , wakeup, td, td->td_proc); 748235459Srstone 749126324Sjhb /* Remove the thread from the queue. */ 750200447Sattilio sq->sq_blockedcnt[td->td_sqqueue]--; 751165272Skmacy TAILQ_REMOVE(&sq->sq_blocked[td->td_sqqueue], td, td_slpq); 752126324Sjhb 753126324Sjhb /* 754126324Sjhb * Get a sleep queue for this thread. If this is the last waiter, 755126324Sjhb * use the queue itself and take it out of the chain, otherwise, 756126324Sjhb * remove a queue from the free list. 757126324Sjhb */ 758126324Sjhb if (LIST_EMPTY(&sq->sq_free)) { 759126324Sjhb td->td_sleepqueue = sq; 760126324Sjhb#ifdef INVARIANTS 761126324Sjhb sq->sq_wchan = NULL; 762126324Sjhb#endif 763131259Sjhb#ifdef SLEEPQUEUE_PROFILING 764131259Sjhb sc->sc_depth--; 765131259Sjhb#endif 766126324Sjhb } else 767126324Sjhb td->td_sleepqueue = LIST_FIRST(&sq->sq_free); 768126324Sjhb LIST_REMOVE(td->td_sleepqueue, sq_hash); 769126324Sjhb 770129188Sjhb td->td_wmesg = NULL; 771129188Sjhb td->td_wchan = NULL; 772246417Sjhb td->td_flags &= ~TDF_SINTR; 773129188Sjhb 774129241Sbde CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)", 775173600Sjulian (void *)td, (long)td->td_proc->p_pid, td->td_name); 776126324Sjhb 777126324Sjhb /* Adjust priority if requested. */ 778177085Sjeff MPASS(pri == 0 || (pri >= PRI_MIN && pri <= PRI_MAX)); 779217410Sjhb if (pri != 0 && td->td_priority > pri && 780217410Sjhb PRI_BASE(td->td_pri_class) == PRI_TIMESHARE) 781136439Sups sched_prio(td, pri); 782184653Sjhb 783184653Sjhb /* 784184653Sjhb * Note that thread td might not be sleeping if it is running 785184653Sjhb * sleepq_catch_signals() on another CPU or is blocked on its 786184653Sjhb * proc lock to check signals. There's no need to mark the 787184653Sjhb * thread runnable in that case. 788184653Sjhb */ 789184653Sjhb if (TD_IS_SLEEPING(td)) { 790184653Sjhb TD_CLR_SLEEPING(td); 791184653Sjhb return (setrunnable(td)); 792184653Sjhb } 793184653Sjhb return (0); 794126324Sjhb} 795126324Sjhb 796169666Sjeff#ifdef INVARIANTS 797126324Sjhb/* 798169666Sjeff * UMA zone item deallocator. 799169666Sjeff */ 800169666Sjeffstatic void 801169666Sjeffsleepq_dtor(void *mem, int size, void *arg) 802169666Sjeff{ 803169666Sjeff struct sleepqueue *sq; 804169666Sjeff int i; 805169666Sjeff 806169666Sjeff sq = mem; 807200447Sattilio for (i = 0; i < NR_SLEEPQS; i++) { 808169666Sjeff MPASS(TAILQ_EMPTY(&sq->sq_blocked[i])); 809200447Sattilio MPASS(sq->sq_blockedcnt[i] == 0); 810200447Sattilio } 811169666Sjeff} 812169666Sjeff#endif 813169666Sjeff 814169666Sjeff/* 815169666Sjeff * UMA zone item initializer. 816169666Sjeff */ 817169666Sjeffstatic int 818169666Sjeffsleepq_init(void *mem, int size, int flags) 819169666Sjeff{ 820169666Sjeff struct sleepqueue *sq; 821169666Sjeff int i; 822169666Sjeff 823169666Sjeff bzero(mem, size); 824169666Sjeff sq = mem; 825200447Sattilio for (i = 0; i < NR_SLEEPQS; i++) { 826169666Sjeff TAILQ_INIT(&sq->sq_blocked[i]); 827200447Sattilio sq->sq_blockedcnt[i] = 0; 828200447Sattilio } 829169666Sjeff LIST_INIT(&sq->sq_free); 830169666Sjeff return (0); 831169666Sjeff} 832169666Sjeff 833169666Sjeff/* 834126324Sjhb * Find the highest priority thread sleeping on a wait channel and resume it. 835126324Sjhb */ 836181334Sjhbint 837165272Skmacysleepq_signal(void *wchan, int flags, int pri, int queue) 838126324Sjhb{ 839126324Sjhb struct sleepqueue *sq; 840137277Sjhb struct thread *td, *besttd; 841181334Sjhb int wakeup_swapper; 842126324Sjhb 843126324Sjhb CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags); 844126324Sjhb KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); 845165272Skmacy MPASS((queue >= 0) && (queue < NR_SLEEPQS)); 846126324Sjhb sq = sleepq_lookup(wchan); 847170294Sjeff if (sq == NULL) 848181334Sjhb return (0); 849134013Sjhb KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE), 850126324Sjhb ("%s: mismatch between sleep/wakeup and cv_*", __func__)); 851129188Sjhb 852137277Sjhb /* 853137277Sjhb * Find the highest priority thread on the queue. If there is a 854137277Sjhb * tie, use the thread that first appears in the queue as it has 855137277Sjhb * been sleeping the longest since threads are always added to 856137277Sjhb * the tail of sleep queues. 857137277Sjhb */ 858137277Sjhb besttd = NULL; 859165272Skmacy TAILQ_FOREACH(td, &sq->sq_blocked[queue], td_slpq) { 860137277Sjhb if (besttd == NULL || td->td_priority < besttd->td_priority) 861137277Sjhb besttd = td; 862137277Sjhb } 863137277Sjhb MPASS(besttd != NULL); 864170294Sjeff thread_lock(besttd); 865181334Sjhb wakeup_swapper = sleepq_resume_thread(sq, besttd, pri); 866170294Sjeff thread_unlock(besttd); 867181334Sjhb return (wakeup_swapper); 868126324Sjhb} 869126324Sjhb 870126324Sjhb/* 871126324Sjhb * Resume all threads sleeping on a specified wait channel. 872126324Sjhb */ 873181334Sjhbint 874165272Skmacysleepq_broadcast(void *wchan, int flags, int pri, int queue) 875126324Sjhb{ 876126324Sjhb struct sleepqueue *sq; 877300109Smarkj struct thread *td; 878181334Sjhb int wakeup_swapper; 879126324Sjhb 880126324Sjhb CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags); 881126324Sjhb KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); 882165272Skmacy MPASS((queue >= 0) && (queue < NR_SLEEPQS)); 883126324Sjhb sq = sleepq_lookup(wchan); 884177085Sjeff if (sq == NULL) 885181334Sjhb return (0); 886134013Sjhb KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE), 887126324Sjhb ("%s: mismatch between sleep/wakeup and cv_*", __func__)); 888129188Sjhb 889145056Sjhb /* Resume all blocked threads on the sleep queue. */ 890181334Sjhb wakeup_swapper = 0; 891300109Smarkj while ((td = TAILQ_FIRST(&sq->sq_blocked[queue])) != NULL) { 892170294Sjeff thread_lock(td); 893300109Smarkj wakeup_swapper |= sleepq_resume_thread(sq, td, pri); 894170294Sjeff thread_unlock(td); 895170294Sjeff } 896181334Sjhb return (wakeup_swapper); 897126324Sjhb} 898126324Sjhb 899126324Sjhb/* 900126324Sjhb * Time sleeping threads out. When the timeout expires, the thread is 901126324Sjhb * removed from the sleep queue and made runnable if it is still asleep. 902126324Sjhb */ 903126324Sjhbstatic void 904126324Sjhbsleepq_timeout(void *arg) 905126324Sjhb{ 906277528Shselasky struct sleepqueue_chain *sc; 907277528Shselasky struct sleepqueue *sq; 908277528Shselasky struct thread *td; 909277528Shselasky void *wchan; 910277528Shselasky int wakeup_swapper; 911126324Sjhb 912277528Shselasky td = arg; 913277528Shselasky wakeup_swapper = 0; 914129241Sbde CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)", 915173600Sjulian (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name); 916126324Sjhb 917277528Shselasky /* 918277528Shselasky * First, see if the thread is asleep and get the wait channel if 919277528Shselasky * it is. 920277528Shselasky */ 921277528Shselasky thread_lock(td); 922277528Shselasky if (TD_IS_SLEEPING(td) && TD_ON_SLEEPQ(td)) { 923277528Shselasky wchan = td->td_wchan; 924277528Shselasky sc = SC_LOOKUP(wchan); 925277528Shselasky THREAD_LOCKPTR_ASSERT(td, &sc->sc_lock); 926277528Shselasky sq = sleepq_lookup(wchan); 927277528Shselasky MPASS(sq != NULL); 928277528Shselasky td->td_flags |= TDF_TIMEOUT; 929277528Shselasky wakeup_swapper = sleepq_resume_thread(sq, td, 0); 930277528Shselasky thread_unlock(td); 931277528Shselasky if (wakeup_swapper) 932277528Shselasky kick_proc0(); 933277528Shselasky return; 934277528Shselasky } 935277213Shselasky 936277528Shselasky /* 937277528Shselasky * If the thread is on the SLEEPQ but isn't sleeping yet, it 938277528Shselasky * can either be on another CPU in between sleepq_add() and 939277528Shselasky * one of the sleepq_*wait*() routines or it can be in 940277528Shselasky * sleepq_catch_signals(). 941277528Shselasky */ 942277213Shselasky if (TD_ON_SLEEPQ(td)) { 943277528Shselasky td->td_flags |= TDF_TIMEOUT; 944277528Shselasky thread_unlock(td); 945277528Shselasky return; 946277528Shselasky } 947175654Sjhb 948277528Shselasky /* 949277528Shselasky * Now check for the edge cases. First, if TDF_TIMEOUT is set, 950277528Shselasky * then the other thread has already yielded to us, so clear 951277528Shselasky * the flag and resume it. If TDF_TIMEOUT is not set, then the 952277528Shselasky * we know that the other thread is not on a sleep queue, but it 953277528Shselasky * hasn't resumed execution yet. In that case, set TDF_TIMOFAIL 954277528Shselasky * to let it know that the timeout has already run and doesn't 955277528Shselasky * need to be canceled. 956277528Shselasky */ 957277528Shselasky if (td->td_flags & TDF_TIMEOUT) { 958277528Shselasky MPASS(TD_IS_SLEEPING(td)); 959277528Shselasky td->td_flags &= ~TDF_TIMEOUT; 960277528Shselasky TD_CLR_SLEEPING(td); 961277528Shselasky wakeup_swapper = setrunnable(td); 962277528Shselasky } else 963277528Shselasky td->td_flags |= TDF_TIMOFAIL; 964170294Sjeff thread_unlock(td); 965181334Sjhb if (wakeup_swapper) 966181334Sjhb kick_proc0(); 967126324Sjhb} 968126324Sjhb 969126324Sjhb/* 970126324Sjhb * Resumes a specific thread from the sleep queue associated with a specific 971126324Sjhb * wait channel if it is on that queue. 972126324Sjhb */ 973126324Sjhbvoid 974126324Sjhbsleepq_remove(struct thread *td, void *wchan) 975126324Sjhb{ 976126324Sjhb struct sleepqueue *sq; 977181334Sjhb int wakeup_swapper; 978126324Sjhb 979126324Sjhb /* 980126324Sjhb * Look up the sleep queue for this wait channel, then re-check 981126324Sjhb * that the thread is asleep on that channel, if it is not, then 982126324Sjhb * bail. 983126324Sjhb */ 984126324Sjhb MPASS(wchan != NULL); 985136445Sjhb sleepq_lock(wchan); 986126324Sjhb sq = sleepq_lookup(wchan); 987170294Sjeff /* 988170294Sjeff * We can not lock the thread here as it may be sleeping on a 989170294Sjeff * different sleepq. However, holding the sleepq lock for this 990170294Sjeff * wchan can guarantee that we do not miss a wakeup for this 991170294Sjeff * channel. The asserts below will catch any false positives. 992170294Sjeff */ 993126324Sjhb if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) { 994126324Sjhb sleepq_release(wchan); 995126324Sjhb return; 996126324Sjhb } 997170294Sjeff /* Thread is asleep on sleep queue sq, so wake it up. */ 998170294Sjeff thread_lock(td); 999126324Sjhb MPASS(sq != NULL); 1000170294Sjeff MPASS(td->td_wchan == wchan); 1001181334Sjhb wakeup_swapper = sleepq_resume_thread(sq, td, 0); 1002170294Sjeff thread_unlock(td); 1003126324Sjhb sleepq_release(wchan); 1004181334Sjhb if (wakeup_swapper) 1005181334Sjhb kick_proc0(); 1006126324Sjhb} 1007126324Sjhb 1008126324Sjhb/* 1009129241Sbde * Abort a thread as if an interrupt had occurred. Only abort 1010129241Sbde * interruptible waits (unfortunately it isn't safe to abort others). 1011126324Sjhb */ 1012181334Sjhbint 1013155741Sdavidxusleepq_abort(struct thread *td, int intrval) 1014126324Sjhb{ 1015170294Sjeff struct sleepqueue *sq; 1016126324Sjhb void *wchan; 1017126324Sjhb 1018170294Sjeff THREAD_LOCK_ASSERT(td, MA_OWNED); 1019126324Sjhb MPASS(TD_ON_SLEEPQ(td)); 1020126324Sjhb MPASS(td->td_flags & TDF_SINTR); 1021155741Sdavidxu MPASS(intrval == EINTR || intrval == ERESTART); 1022126324Sjhb 1023126324Sjhb /* 1024126324Sjhb * If the TDF_TIMEOUT flag is set, just leave. A 1025126324Sjhb * timeout is scheduled anyhow. 1026126324Sjhb */ 1027126324Sjhb if (td->td_flags & TDF_TIMEOUT) 1028181334Sjhb return (0); 1029126324Sjhb 1030129241Sbde CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)", 1031173600Sjulian (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name); 1032170294Sjeff td->td_intrval = intrval; 1033170294Sjeff td->td_flags |= TDF_SLEEPABORT; 1034170294Sjeff /* 1035170294Sjeff * If the thread has not slept yet it will find the signal in 1036170294Sjeff * sleepq_catch_signals() and call sleepq_resume_thread. Otherwise 1037170294Sjeff * we have to do it here. 1038170294Sjeff */ 1039170294Sjeff if (!TD_IS_SLEEPING(td)) 1040181334Sjhb return (0); 1041126324Sjhb wchan = td->td_wchan; 1042170294Sjeff MPASS(wchan != NULL); 1043170294Sjeff sq = sleepq_lookup(wchan); 1044170294Sjeff MPASS(sq != NULL); 1045170294Sjeff 1046170294Sjeff /* Thread is asleep on sleep queue sq, so wake it up. */ 1047181334Sjhb return (sleepq_resume_thread(sq, td, 0)); 1048126324Sjhb} 1049154936Sjhb 1050296927Scem/* 1051296927Scem * Prints the stacks of all threads presently sleeping on wchan/queue to 1052296927Scem * the sbuf sb. Sets count_stacks_printed to the number of stacks actually 1053296927Scem * printed. Typically, this will equal the number of threads sleeping on the 1054296927Scem * queue, but may be less if sb overflowed before all stacks were printed. 1055296927Scem */ 1056296973Scem#ifdef STACK 1057296927Scemint 1058296927Scemsleepq_sbuf_print_stacks(struct sbuf *sb, void *wchan, int queue, 1059296927Scem int *count_stacks_printed) 1060296927Scem{ 1061296927Scem struct thread *td, *td_next; 1062296927Scem struct sleepqueue *sq; 1063296927Scem struct stack **st; 1064296927Scem struct sbuf **td_infos; 1065296927Scem int i, stack_idx, error, stacks_to_allocate; 1066296927Scem bool finished, partial_print; 1067296927Scem 1068296927Scem error = 0; 1069296927Scem finished = false; 1070296927Scem partial_print = false; 1071296927Scem 1072296927Scem KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); 1073296927Scem MPASS((queue >= 0) && (queue < NR_SLEEPQS)); 1074296927Scem 1075296927Scem stacks_to_allocate = 10; 1076296927Scem for (i = 0; i < 3 && !finished ; i++) { 1077296927Scem /* We cannot malloc while holding the queue's spinlock, so 1078296927Scem * we do our mallocs now, and hope it is enough. If it 1079296927Scem * isn't, we will free these, drop the lock, malloc more, 1080296927Scem * and try again, up to a point. After that point we will 1081296927Scem * give up and report ENOMEM. We also cannot write to sb 1082296927Scem * during this time since the client may have set the 1083296927Scem * SBUF_AUTOEXTEND flag on their sbuf, which could cause a 1084296927Scem * malloc as we print to it. So we defer actually printing 1085296927Scem * to sb until after we drop the spinlock. 1086296927Scem */ 1087296927Scem 1088296927Scem /* Where we will store the stacks. */ 1089296927Scem st = malloc(sizeof(struct stack *) * stacks_to_allocate, 1090296927Scem M_TEMP, M_WAITOK); 1091296927Scem for (stack_idx = 0; stack_idx < stacks_to_allocate; 1092296927Scem stack_idx++) 1093296927Scem st[stack_idx] = stack_create(); 1094296927Scem 1095296927Scem /* Where we will store the td name, tid, etc. */ 1096296927Scem td_infos = malloc(sizeof(struct sbuf *) * stacks_to_allocate, 1097296927Scem M_TEMP, M_WAITOK); 1098296927Scem for (stack_idx = 0; stack_idx < stacks_to_allocate; 1099296927Scem stack_idx++) 1100296927Scem td_infos[stack_idx] = sbuf_new(NULL, NULL, 1101296927Scem MAXCOMLEN + sizeof(struct thread *) * 2 + 40, 1102296927Scem SBUF_FIXEDLEN); 1103296927Scem 1104296927Scem sleepq_lock(wchan); 1105296927Scem sq = sleepq_lookup(wchan); 1106296927Scem if (sq == NULL) { 1107296927Scem /* This sleepq does not exist; exit and return ENOENT. */ 1108296927Scem error = ENOENT; 1109296927Scem finished = true; 1110296927Scem sleepq_release(wchan); 1111296927Scem goto loop_end; 1112296927Scem } 1113296927Scem 1114296927Scem stack_idx = 0; 1115296927Scem /* Save thread info */ 1116296927Scem TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq, 1117296927Scem td_next) { 1118296927Scem if (stack_idx >= stacks_to_allocate) 1119296927Scem goto loop_end; 1120296927Scem 1121296927Scem /* Note the td_lock is equal to the sleepq_lock here. */ 1122296927Scem stack_save_td(st[stack_idx], td); 1123296927Scem 1124296927Scem sbuf_printf(td_infos[stack_idx], "%d: %s %p", 1125296927Scem td->td_tid, td->td_name, td); 1126296927Scem 1127296927Scem ++stack_idx; 1128296927Scem } 1129296927Scem 1130296927Scem finished = true; 1131296927Scem sleepq_release(wchan); 1132296927Scem 1133296927Scem /* Print the stacks */ 1134296927Scem for (i = 0; i < stack_idx; i++) { 1135296927Scem sbuf_finish(td_infos[i]); 1136296927Scem sbuf_printf(sb, "--- thread %s: ---\n", sbuf_data(td_infos[i])); 1137296927Scem stack_sbuf_print(sb, st[i]); 1138296927Scem sbuf_printf(sb, "\n"); 1139296927Scem 1140296927Scem error = sbuf_error(sb); 1141296927Scem if (error == 0) 1142296927Scem *count_stacks_printed = stack_idx; 1143296927Scem } 1144296927Scem 1145296927Scemloop_end: 1146296927Scem if (!finished) 1147296927Scem sleepq_release(wchan); 1148296927Scem for (stack_idx = 0; stack_idx < stacks_to_allocate; 1149296927Scem stack_idx++) 1150296927Scem stack_destroy(st[stack_idx]); 1151296927Scem for (stack_idx = 0; stack_idx < stacks_to_allocate; 1152296927Scem stack_idx++) 1153296927Scem sbuf_delete(td_infos[stack_idx]); 1154296927Scem free(st, M_TEMP); 1155296927Scem free(td_infos, M_TEMP); 1156296927Scem stacks_to_allocate *= 10; 1157296927Scem } 1158296927Scem 1159296927Scem if (!finished && error == 0) 1160296927Scem error = ENOMEM; 1161296927Scem 1162296927Scem return (error); 1163296927Scem} 1164296973Scem#endif 1165296927Scem 1166177372Sjeff#ifdef SLEEPQUEUE_PROFILING 1167177372Sjeff#define SLEEPQ_PROF_LOCATIONS 1024 1168212750Smdf#define SLEEPQ_SBUFSIZE 512 1169177372Sjeffstruct sleepq_prof { 1170177372Sjeff LIST_ENTRY(sleepq_prof) sp_link; 1171177372Sjeff const char *sp_wmesg; 1172177372Sjeff long sp_count; 1173177372Sjeff}; 1174177372Sjeff 1175177372SjeffLIST_HEAD(sqphead, sleepq_prof); 1176177372Sjeff 1177177372Sjeffstruct sqphead sleepq_prof_free; 1178177372Sjeffstruct sqphead sleepq_hash[SC_TABLESIZE]; 1179177372Sjeffstatic struct sleepq_prof sleepq_profent[SLEEPQ_PROF_LOCATIONS]; 1180177372Sjeffstatic struct mtx sleepq_prof_lock; 1181177372SjeffMTX_SYSINIT(sleepq_prof_lock, &sleepq_prof_lock, "sleepq_prof", MTX_SPIN); 1182177372Sjeff 1183177372Sjeffstatic void 1184177372Sjeffsleepq_profile(const char *wmesg) 1185177372Sjeff{ 1186177372Sjeff struct sleepq_prof *sp; 1187177372Sjeff 1188177372Sjeff mtx_lock_spin(&sleepq_prof_lock); 1189177372Sjeff if (prof_enabled == 0) 1190177372Sjeff goto unlock; 1191177372Sjeff LIST_FOREACH(sp, &sleepq_hash[SC_HASH(wmesg)], sp_link) 1192177372Sjeff if (sp->sp_wmesg == wmesg) 1193177372Sjeff goto done; 1194177372Sjeff sp = LIST_FIRST(&sleepq_prof_free); 1195177372Sjeff if (sp == NULL) 1196177372Sjeff goto unlock; 1197177372Sjeff sp->sp_wmesg = wmesg; 1198177372Sjeff LIST_REMOVE(sp, sp_link); 1199177372Sjeff LIST_INSERT_HEAD(&sleepq_hash[SC_HASH(wmesg)], sp, sp_link); 1200177372Sjeffdone: 1201177372Sjeff sp->sp_count++; 1202177372Sjeffunlock: 1203177372Sjeff mtx_unlock_spin(&sleepq_prof_lock); 1204177372Sjeff return; 1205177372Sjeff} 1206177372Sjeff 1207177372Sjeffstatic void 1208177372Sjeffsleepq_prof_reset(void) 1209177372Sjeff{ 1210177372Sjeff struct sleepq_prof *sp; 1211177372Sjeff int enabled; 1212177372Sjeff int i; 1213177372Sjeff 1214177372Sjeff mtx_lock_spin(&sleepq_prof_lock); 1215177372Sjeff enabled = prof_enabled; 1216177372Sjeff prof_enabled = 0; 1217177372Sjeff for (i = 0; i < SC_TABLESIZE; i++) 1218177372Sjeff LIST_INIT(&sleepq_hash[i]); 1219177372Sjeff LIST_INIT(&sleepq_prof_free); 1220177372Sjeff for (i = 0; i < SLEEPQ_PROF_LOCATIONS; i++) { 1221177372Sjeff sp = &sleepq_profent[i]; 1222177372Sjeff sp->sp_wmesg = NULL; 1223177372Sjeff sp->sp_count = 0; 1224177372Sjeff LIST_INSERT_HEAD(&sleepq_prof_free, sp, sp_link); 1225177372Sjeff } 1226177372Sjeff prof_enabled = enabled; 1227177372Sjeff mtx_unlock_spin(&sleepq_prof_lock); 1228177372Sjeff} 1229177372Sjeff 1230177372Sjeffstatic int 1231177372Sjeffenable_sleepq_prof(SYSCTL_HANDLER_ARGS) 1232177372Sjeff{ 1233177372Sjeff int error, v; 1234177372Sjeff 1235177372Sjeff v = prof_enabled; 1236177372Sjeff error = sysctl_handle_int(oidp, &v, v, req); 1237177372Sjeff if (error) 1238177372Sjeff return (error); 1239177372Sjeff if (req->newptr == NULL) 1240177372Sjeff return (error); 1241177372Sjeff if (v == prof_enabled) 1242177372Sjeff return (0); 1243177372Sjeff if (v == 1) 1244177372Sjeff sleepq_prof_reset(); 1245177372Sjeff mtx_lock_spin(&sleepq_prof_lock); 1246177372Sjeff prof_enabled = !!v; 1247177372Sjeff mtx_unlock_spin(&sleepq_prof_lock); 1248177372Sjeff 1249177372Sjeff return (0); 1250177372Sjeff} 1251177372Sjeff 1252177372Sjeffstatic int 1253177372Sjeffreset_sleepq_prof_stats(SYSCTL_HANDLER_ARGS) 1254177372Sjeff{ 1255177372Sjeff int error, v; 1256177372Sjeff 1257177372Sjeff v = 0; 1258177372Sjeff error = sysctl_handle_int(oidp, &v, 0, req); 1259177372Sjeff if (error) 1260177372Sjeff return (error); 1261177372Sjeff if (req->newptr == NULL) 1262177372Sjeff return (error); 1263177372Sjeff if (v == 0) 1264177372Sjeff return (0); 1265177372Sjeff sleepq_prof_reset(); 1266177372Sjeff 1267177372Sjeff return (0); 1268177372Sjeff} 1269177372Sjeff 1270177372Sjeffstatic int 1271177372Sjeffdump_sleepq_prof_stats(SYSCTL_HANDLER_ARGS) 1272177372Sjeff{ 1273177372Sjeff struct sleepq_prof *sp; 1274177372Sjeff struct sbuf *sb; 1275177372Sjeff int enabled; 1276177372Sjeff int error; 1277177372Sjeff int i; 1278177372Sjeff 1279217916Smdf error = sysctl_wire_old_buffer(req, 0); 1280217916Smdf if (error != 0) 1281217916Smdf return (error); 1282212750Smdf sb = sbuf_new_for_sysctl(NULL, NULL, SLEEPQ_SBUFSIZE, req); 1283177372Sjeff sbuf_printf(sb, "\nwmesg\tcount\n"); 1284177372Sjeff enabled = prof_enabled; 1285177372Sjeff mtx_lock_spin(&sleepq_prof_lock); 1286177372Sjeff prof_enabled = 0; 1287177372Sjeff mtx_unlock_spin(&sleepq_prof_lock); 1288177372Sjeff for (i = 0; i < SC_TABLESIZE; i++) { 1289177372Sjeff LIST_FOREACH(sp, &sleepq_hash[i], sp_link) { 1290177372Sjeff sbuf_printf(sb, "%s\t%ld\n", 1291177372Sjeff sp->sp_wmesg, sp->sp_count); 1292177372Sjeff } 1293177372Sjeff } 1294177372Sjeff mtx_lock_spin(&sleepq_prof_lock); 1295177372Sjeff prof_enabled = enabled; 1296177372Sjeff mtx_unlock_spin(&sleepq_prof_lock); 1297177372Sjeff 1298212750Smdf error = sbuf_finish(sb); 1299177372Sjeff sbuf_delete(sb); 1300177372Sjeff return (error); 1301177372Sjeff} 1302177372Sjeff 1303177372SjeffSYSCTL_PROC(_debug_sleepq, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 1304177372Sjeff NULL, 0, dump_sleepq_prof_stats, "A", "Sleepqueue profiling statistics"); 1305177372SjeffSYSCTL_PROC(_debug_sleepq, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW, 1306177372Sjeff NULL, 0, reset_sleepq_prof_stats, "I", 1307177372Sjeff "Reset sleepqueue profiling statistics"); 1308177372SjeffSYSCTL_PROC(_debug_sleepq, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW, 1309177372Sjeff NULL, 0, enable_sleepq_prof, "I", "Enable sleepqueue profiling"); 1310177372Sjeff#endif 1311177372Sjeff 1312154936Sjhb#ifdef DDB 1313154936SjhbDB_SHOW_COMMAND(sleepq, db_show_sleepqueue) 1314154936Sjhb{ 1315154936Sjhb struct sleepqueue_chain *sc; 1316154936Sjhb struct sleepqueue *sq; 1317154944Simp#ifdef INVARIANTS 1318154936Sjhb struct lock_object *lock; 1319154944Simp#endif 1320154936Sjhb struct thread *td; 1321154936Sjhb void *wchan; 1322154936Sjhb int i; 1323154936Sjhb 1324154936Sjhb if (!have_addr) 1325154936Sjhb return; 1326154936Sjhb 1327154936Sjhb /* 1328154936Sjhb * First, see if there is an active sleep queue for the wait channel 1329154936Sjhb * indicated by the address. 1330154936Sjhb */ 1331154936Sjhb wchan = (void *)addr; 1332154936Sjhb sc = SC_LOOKUP(wchan); 1333154936Sjhb LIST_FOREACH(sq, &sc->sc_queues, sq_hash) 1334154936Sjhb if (sq->sq_wchan == wchan) 1335154936Sjhb goto found; 1336154936Sjhb 1337154936Sjhb /* 1338154936Sjhb * Second, see if there is an active sleep queue at the address 1339154936Sjhb * indicated. 1340154936Sjhb */ 1341154936Sjhb for (i = 0; i < SC_TABLESIZE; i++) 1342154936Sjhb LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) { 1343154936Sjhb if (sq == (struct sleepqueue *)addr) 1344154936Sjhb goto found; 1345154936Sjhb } 1346154936Sjhb 1347154936Sjhb db_printf("Unable to locate a sleep queue via %p\n", (void *)addr); 1348154936Sjhb return; 1349154936Sjhbfound: 1350154936Sjhb db_printf("Wait channel: %p\n", sq->sq_wchan); 1351201879Sattilio db_printf("Queue type: %d\n", sq->sq_type); 1352154936Sjhb#ifdef INVARIANTS 1353154936Sjhb if (sq->sq_lock) { 1354164325Spjd lock = sq->sq_lock; 1355154936Sjhb db_printf("Associated Interlock: %p - (%s) %s\n", lock, 1356154936Sjhb LOCK_CLASS(lock)->lc_name, lock->lo_name); 1357154936Sjhb } 1358154936Sjhb#endif 1359154936Sjhb db_printf("Blocked threads:\n"); 1360165272Skmacy for (i = 0; i < NR_SLEEPQS; i++) { 1361165272Skmacy db_printf("\nQueue[%d]:\n", i); 1362165272Skmacy if (TAILQ_EMPTY(&sq->sq_blocked[i])) 1363165272Skmacy db_printf("\tempty\n"); 1364165272Skmacy else 1365165272Skmacy TAILQ_FOREACH(td, &sq->sq_blocked[0], 1366165272Skmacy td_slpq) { 1367165272Skmacy db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td, 1368165272Skmacy td->td_tid, td->td_proc->p_pid, 1369180930Sjhb td->td_name); 1370165272Skmacy } 1371200447Sattilio db_printf("(expected: %u)\n", sq->sq_blockedcnt[i]); 1372165272Skmacy } 1373154936Sjhb} 1374157823Sjhb 1375157823Sjhb/* Alias 'show sleepqueue' to 'show sleepq'. */ 1376183054SsamDB_SHOW_ALIAS(sleepqueue, db_show_sleepqueue); 1377154936Sjhb#endif 1378