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