Home | History | Annotate | Line # | Download | only in kern
kern_sleepq.c revision 1.28.2.2
      1 /*	$NetBSD: kern_sleepq.c,v 1.28.2.2 2008/06/23 04:31:51 wrstuden Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Sleep queue implementation, used by turnstiles and general sleep/wakeup
     34  * interfaces.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.28.2.2 2008/06/23 04:31:51 wrstuden Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/kernel.h>
     42 #include <sys/cpu.h>
     43 #include <sys/pool.h>
     44 #include <sys/proc.h>
     45 #include <sys/resourcevar.h>
     46 #include <sys/sa.h>
     47 #include <sys/savar.h>
     48 #include <sys/sched.h>
     49 #include <sys/systm.h>
     50 #include <sys/sleepq.h>
     51 #include <sys/ktrace.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 int	sleepq_sigtoerror(lwp_t *, int);
     56 
     57 /* General purpose sleep table, used by ltsleep() and condition variables. */
     58 sleeptab_t	sleeptab;
     59 
     60 /*
     61  * sleeptab_init:
     62  *
     63  *	Initialize a sleep table.
     64  */
     65 void
     66 sleeptab_init(sleeptab_t *st)
     67 {
     68 	sleepq_t *sq;
     69 	int i;
     70 
     71 	for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
     72 		sq = &st->st_queues[i].st_queue;
     73 		mutex_init(&st->st_queues[i].st_mutex, MUTEX_DEFAULT,
     74 		    IPL_SCHED);
     75 		sleepq_init(sq);
     76 	}
     77 }
     78 
     79 /*
     80  * sleepq_init:
     81  *
     82  *	Prepare a sleep queue for use.
     83  */
     84 void
     85 sleepq_init(sleepq_t *sq)
     86 {
     87 
     88 	TAILQ_INIT(sq);
     89 }
     90 
     91 /*
     92  * sleepq_remove:
     93  *
     94  *	Remove an LWP from a sleep queue and wake it up.  Return non-zero if
     95  *	the LWP is swapped out; if so the caller needs to awaken the swapper
     96  *	to bring the LWP into memory.
     97  */
     98 int
     99 sleepq_remove(sleepq_t *sq, lwp_t *l)
    100 {
    101 	struct schedstate_percpu *spc;
    102 	struct cpu_info *ci;
    103 
    104 	KASSERT(lwp_locked(l, NULL));
    105 
    106 	TAILQ_REMOVE(sq, l, l_sleepchain);
    107 	l->l_syncobj = &sched_syncobj;
    108 	l->l_wchan = NULL;
    109 	l->l_sleepq = NULL;
    110 	l->l_flag &= ~LW_SINTR;
    111 
    112 	ci = l->l_cpu;
    113 	spc = &ci->ci_schedstate;
    114 
    115 	/*
    116 	 * If not sleeping, the LWP must have been suspended.  Let whoever
    117 	 * holds it stopped set it running again.
    118 	 */
    119 	if (l->l_stat != LSSLEEP) {
    120 		KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
    121 		lwp_setlock(l, spc->spc_lwplock);
    122 		return 0;
    123 	}
    124 
    125 	/*
    126 	 * If the LWP is still on the CPU, mark it as LSONPROC.  It may be
    127 	 * about to call mi_switch(), in which case it will yield.
    128 	 */
    129 	if ((l->l_pflag & LP_RUNNING) != 0) {
    130 		l->l_stat = LSONPROC;
    131 		l->l_slptime = 0;
    132 		lwp_setlock(l, spc->spc_lwplock);
    133 		return 0;
    134 	}
    135 
    136 	/* Update sleep time delta, call the wake-up handler of scheduler */
    137 	l->l_slpticksum += (hardclock_ticks - l->l_slpticks);
    138 	sched_wakeup(l);
    139 
    140 	/* Look for a CPU to wake up */
    141 	l->l_cpu = sched_takecpu(l);
    142 	ci = l->l_cpu;
    143 	spc = &ci->ci_schedstate;
    144 
    145 	/*
    146 	 * Set it running.
    147 	 */
    148 	spc_lock(ci);
    149 	lwp_setlock(l, spc->spc_mutex);
    150 	if (l->l_proc->p_sa != NULL)
    151 		sa_awaken(l);
    152 	sched_setrunnable(l);
    153 	l->l_stat = LSRUN;
    154 	l->l_slptime = 0;
    155 	if ((l->l_flag & LW_INMEM) != 0) {
    156 		sched_enqueue(l, false);
    157 		spc_unlock(ci);
    158 		return 0;
    159 	}
    160 	spc_unlock(ci);
    161 	return 1;
    162 }
    163 
    164 /*
    165  * sleepq_insert:
    166  *
    167  *	Insert an LWP into the sleep queue, optionally sorting by priority.
    168  */
    169 inline void
    170 sleepq_insert(sleepq_t *sq, lwp_t *l, syncobj_t *sobj)
    171 {
    172 	lwp_t *l2;
    173 	const int pri = lwp_eprio(l);
    174 
    175 	if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
    176 		TAILQ_FOREACH(l2, sq, l_sleepchain) {
    177 			if (lwp_eprio(l2) < pri) {
    178 				TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
    179 				return;
    180 			}
    181 		}
    182 	}
    183 
    184 	if ((sobj->sobj_flag & SOBJ_SLEEPQ_LIFO) != 0)
    185 		TAILQ_INSERT_HEAD(sq, l, l_sleepchain);
    186 	else
    187 		TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
    188 }
    189 
    190 /*
    191  * sleepq_enqueue:
    192  *
    193  *	Enter an LWP into the sleep queue and prepare for sleep.  The sleep
    194  *	queue must already be locked, and any interlock (such as the kernel
    195  *	lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
    196  */
    197 void
    198 sleepq_enqueue(sleepq_t *sq, wchan_t wchan, const char *wmesg, syncobj_t *sobj)
    199 {
    200 	lwp_t *l = curlwp;
    201 
    202 	KASSERT(lwp_locked(l, NULL));
    203 	KASSERT(l->l_stat == LSONPROC);
    204 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    205 
    206 	l->l_syncobj = sobj;
    207 	l->l_wchan = wchan;
    208 	l->l_sleepq = sq;
    209 	l->l_wmesg = wmesg;
    210 	l->l_slptime = 0;
    211 	l->l_stat = LSSLEEP;
    212 	l->l_sleeperr = 0;
    213 
    214 	sleepq_insert(sq, l, sobj);
    215 
    216 	/* Save the time when thread has slept */
    217 	l->l_slpticks = hardclock_ticks;
    218 	sched_slept(l);
    219 }
    220 
    221 /*
    222  * sleepq_block:
    223  *
    224  *	After any intermediate step such as releasing an interlock, switch.
    225  * 	sleepq_block() may return early under exceptional conditions, for
    226  * 	example if the LWP's containing process is exiting.
    227  */
    228 int
    229 sleepq_block(int timo, bool catch)
    230 {
    231 	int error = 0, sig;
    232 	struct proc *p;
    233 	lwp_t *l = curlwp;
    234 	bool early = false;
    235 
    236 	ktrcsw(1, 0);
    237 
    238 	/*
    239 	 * If sleeping interruptably, check for pending signals, exits or
    240 	 * core dump events.
    241 	 */
    242 	if (catch) {
    243 		l->l_flag |= LW_SINTR;
    244 		if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
    245 			l->l_flag &= ~LW_CANCELLED;
    246 			error = EINTR;
    247 			early = true;
    248 		} else if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))
    249 			early = true;
    250 	}
    251 
    252 	if (early) {
    253 		/* lwp_unsleep() will release the lock */
    254 		lwp_unsleep(l, true);
    255 	} else {
    256 		if (timo)
    257 			callout_schedule(&l->l_timeout_ch, timo);
    258 
    259 		if ((l->l_flag & LW_SA) != 0)
    260 			sa_switch(l);
    261 		else
    262 			mi_switch(l);
    263 
    264 		/* The LWP and sleep queue are now unlocked. */
    265 		if (timo) {
    266 			/*
    267 			 * Even if the callout appears to have fired, we need to
    268 			 * stop it in order to synchronise with other CPUs.
    269 			 */
    270 			if (callout_halt(&l->l_timeout_ch, NULL))
    271 				error = EWOULDBLOCK;
    272 		}
    273 	}
    274 
    275 	if (catch && error == 0) {
    276 		p = l->l_proc;
    277 		if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
    278 			error = EINTR;
    279 		else if ((l->l_flag & LW_PENDSIG) != 0) {
    280 			/*
    281 			 * Acquiring p_lock may cause us to recurse
    282 			 * through the sleep path and back into this
    283 			 * routine, but is safe because LWPs sleeping
    284 			 * on locks are non-interruptable.  We will
    285 			 * not recurse again.
    286 			 */
    287 			mutex_enter(p->p_lock);
    288 			if ((sig = issignal(l)) != 0)
    289 				error = sleepq_sigtoerror(l, sig);
    290 			mutex_exit(p->p_lock);
    291 		}
    292 	}
    293 
    294 	ktrcsw(0, 0);
    295 	if (__predict_false(l->l_biglocks != 0)) {
    296 		KERNEL_LOCK(l->l_biglocks, NULL);
    297 	}
    298 	return error;
    299 }
    300 
    301 /*
    302  * sleepq_wake:
    303  *
    304  *	Wake zero or more LWPs blocked on a single wait channel.
    305  */
    306 lwp_t *
    307 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
    308 {
    309 	lwp_t *l, *next;
    310 	int swapin = 0;
    311 
    312 	KASSERT(mutex_owned(mp));
    313 
    314 	for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
    315 		KASSERT(l->l_sleepq == sq);
    316 		KASSERT(l->l_mutex == mp);
    317 		next = TAILQ_NEXT(l, l_sleepchain);
    318 		if (l->l_wchan != wchan)
    319 			continue;
    320 		swapin |= sleepq_remove(sq, l);
    321 		if (--expected == 0)
    322 			break;
    323 	}
    324 
    325 	mutex_spin_exit(mp);
    326 
    327 	/*
    328 	 * If there are newly awakend threads that need to be swapped in,
    329 	 * then kick the swapper into action.
    330 	 */
    331 	if (swapin)
    332 		uvm_kick_scheduler();
    333 
    334 	return l;
    335 }
    336 
    337 /*
    338  * sleepq_unsleep:
    339  *
    340  *	Remove an LWP from its sleep queue and set it runnable again.
    341  *	sleepq_unsleep() is called with the LWP's mutex held, and will
    342  *	always release it.
    343  */
    344 u_int
    345 sleepq_unsleep(lwp_t *l, bool cleanup)
    346 {
    347 	sleepq_t *sq = l->l_sleepq;
    348 	kmutex_t *mp = l->l_mutex;
    349 	int swapin;
    350 
    351 	KASSERT(lwp_locked(l, mp));
    352 	KASSERT(l->l_wchan != NULL);
    353 
    354 	swapin = sleepq_remove(sq, l);
    355 
    356 	if (cleanup) {
    357 		mutex_spin_exit(mp);
    358 		if (swapin)
    359 			uvm_kick_scheduler();
    360 	}
    361 
    362 	return swapin;
    363 }
    364 
    365 /*
    366  * sleepq_timeout:
    367  *
    368  *	Entered via the callout(9) subsystem to time out an LWP that is on a
    369  *	sleep queue.
    370  */
    371 void
    372 sleepq_timeout(void *arg)
    373 {
    374 	lwp_t *l = arg;
    375 
    376 	/*
    377 	 * Lock the LWP.  Assuming it's still on the sleep queue, its
    378 	 * current mutex will also be the sleep queue mutex.
    379 	 */
    380 	lwp_lock(l);
    381 
    382 	if (l->l_wchan == NULL) {
    383 		/* Somebody beat us to it. */
    384 		lwp_unlock(l);
    385 		return;
    386 	}
    387 
    388 	lwp_unsleep(l, true);
    389 }
    390 
    391 /*
    392  * sleepq_sigtoerror:
    393  *
    394  *	Given a signal number, interpret and return an error code.
    395  */
    396 int
    397 sleepq_sigtoerror(lwp_t *l, int sig)
    398 {
    399 	struct proc *p = l->l_proc;
    400 	int error;
    401 
    402 	KASSERT(mutex_owned(p->p_lock));
    403 
    404 	/*
    405 	 * If this sleep was canceled, don't let the syscall restart.
    406 	 */
    407 	if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
    408 		error = EINTR;
    409 	else
    410 		error = ERESTART;
    411 
    412 	return error;
    413 }
    414 
    415 /*
    416  * sleepq_abort:
    417  *
    418  *	After a panic or during autoconfiguration, lower the interrupt
    419  *	priority level to give pending interrupts a chance to run, and
    420  *	then return.  Called if sleepq_dontsleep() returns non-zero, and
    421  *	always returns zero.
    422  */
    423 int
    424 sleepq_abort(kmutex_t *mtx, int unlock)
    425 {
    426 	extern int safepri;
    427 	int s;
    428 
    429 	s = splhigh();
    430 	splx(safepri);
    431 	splx(s);
    432 	if (mtx != NULL && unlock != 0)
    433 		mutex_exit(mtx);
    434 
    435 	return 0;
    436 }
    437 
    438 /*
    439  * sleepq_changepri:
    440  *
    441  *	Adjust the priority of an LWP residing on a sleepq.  This method
    442  *	will only alter the user priority; the effective priority is
    443  *	assumed to have been fixed at the time of insertion into the queue.
    444  */
    445 void
    446 sleepq_changepri(lwp_t *l, pri_t pri)
    447 {
    448 	sleepq_t *sq = l->l_sleepq;
    449 	pri_t opri;
    450 
    451 	KASSERT(lwp_locked(l, NULL));
    452 
    453 	opri = lwp_eprio(l);
    454 	l->l_priority = pri;
    455 
    456 	if (lwp_eprio(l) == opri) {
    457 		return;
    458 	}
    459 	if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0) {
    460 		return;
    461 	}
    462 
    463 	/*
    464 	 * Don't let the sleep queue become empty, even briefly.
    465 	 * cv_signal() and cv_broadcast() inspect it without the
    466 	 * sleep queue lock held and need to see a non-empty queue
    467 	 * head if there are waiters.
    468 	 */
    469 	if (TAILQ_FIRST(sq) == l && TAILQ_NEXT(l, l_sleepchain) == NULL) {
    470 		return;
    471 	}
    472 	TAILQ_REMOVE(sq, l, l_sleepchain);
    473 	sleepq_insert(sq, l, l->l_syncobj);
    474 }
    475 
    476 void
    477 sleepq_lendpri(lwp_t *l, pri_t pri)
    478 {
    479 	sleepq_t *sq = l->l_sleepq;
    480 	pri_t opri;
    481 
    482 	KASSERT(lwp_locked(l, NULL));
    483 
    484 	opri = lwp_eprio(l);
    485 	l->l_inheritedprio = pri;
    486 
    487 	if (lwp_eprio(l) == opri) {
    488 		return;
    489 	}
    490 	if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0) {
    491 		return;
    492 	}
    493 
    494 	/*
    495 	 * Don't let the sleep queue become empty, even briefly.
    496 	 * cv_signal() and cv_broadcast() inspect it without the
    497 	 * sleep queue lock held and need to see a non-empty queue
    498 	 * head if there are waiters.
    499 	 */
    500 	if (TAILQ_FIRST(sq) == l && TAILQ_NEXT(l, l_sleepchain) == NULL) {
    501 		return;
    502 	}
    503 	TAILQ_REMOVE(sq, l, l_sleepchain);
    504 	sleepq_insert(sq, l, l->l_syncobj);
    505 }
    506