Home | History | Annotate | Line # | Download | only in kern
kern_sleepq.c revision 1.4.2.6
      1 /*	$NetBSD: kern_sleepq.c,v 1.4.2.6 2007/03/17 16:54:37 rmind 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.4.2.6 2007/03/17 16:54:37 rmind 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/cpu.h>
     55 #include <sys/pool.h>
     56 #include <sys/proc.h>
     57 #include <sys/resourcevar.h>
     58 #include <sys/sched.h>
     59 #include <sys/systm.h>
     60 #include <sys/sleepq.h>
     61 #ifdef KTRACE
     62 #include <sys/ktrace.h>
     63 #endif
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 int	sleepq_sigtoerror(struct lwp *, int);
     68 void	updatepri(struct lwp *);
     69 
     70 /* General purpose sleep table, used by ltsleep() and condition variables. */
     71 sleeptab_t	sleeptab;
     72 
     73 /*
     74  * sleeptab_init:
     75  *
     76  *	Initialize a sleep table.
     77  */
     78 void
     79 sleeptab_init(sleeptab_t *st)
     80 {
     81 	sleepq_t *sq;
     82 	int i;
     83 
     84 	for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
     85 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
     86 		sq = &st->st_queues[i].st_queue;
     87 		mutex_init(&st->st_queues[i].st_mutex, MUTEX_SPIN, IPL_SCHED);
     88 		sleepq_init(sq, &st->st_queues[i].st_mutex);
     89 #else
     90 		sq = &st->st_queues[i];
     91 		sleepq_init(sq, &sched_mutex);
     92 #endif
     93 	}
     94 }
     95 
     96 /*
     97  * sleepq_init:
     98  *
     99  *	Prepare a sleep queue for use.
    100  */
    101 void
    102 sleepq_init(sleepq_t *sq, kmutex_t *mtx)
    103 {
    104 
    105 	sq->sq_waiters = 0;
    106 	sq->sq_mutex = mtx;
    107 	TAILQ_INIT(&sq->sq_queue);
    108 }
    109 
    110 /*
    111  * sleepq_remove:
    112  *
    113  *	Remove an LWP from a sleep queue and wake it up.  Return non-zero if
    114  *	the LWP is swapped out; if so the caller needs to awaken the swapper
    115  *	to bring the LWP into memory.
    116  */
    117 int
    118 sleepq_remove(sleepq_t *sq, struct lwp *l)
    119 {
    120 	struct cpu_info *ci;
    121 
    122 	KASSERT(lwp_locked(l, sq->sq_mutex));
    123 	KASSERT(sq->sq_waiters > 0);
    124 
    125 	sq->sq_waiters--;
    126 	TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    127 
    128 #ifdef DIAGNOSTIC
    129 	if (sq->sq_waiters == 0)
    130 		KASSERT(TAILQ_FIRST(&sq->sq_queue) == NULL);
    131 	else
    132 		KASSERT(TAILQ_FIRST(&sq->sq_queue) != NULL);
    133 #endif
    134 
    135 	l->l_syncobj = &sched_syncobj;
    136 	l->l_wchan = NULL;
    137 	l->l_sleepq = NULL;
    138 	l->l_flag &= ~LW_SINTR;
    139 
    140 	/*
    141 	 * If not sleeping, the LWP must have been suspended.  Let whoever
    142 	 * holds it stopped set it running again.
    143 	 */
    144 	if (l->l_stat != LSSLEEP) {
    145 	 	KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
    146 		lwp_setlock(l, &sched_mutex);
    147 		return 0;
    148 	}
    149 
    150 	sched_lock(1);
    151 	lwp_setlock(l, &sched_mutex);
    152 
    153 	/*
    154 	 * If the LWP is still on the CPU, mark it as LSONPROC.  It may be
    155 	 * about to call mi_switch(), in which case it will yield.
    156 	 *
    157 	 * XXXSMP Will need to change for preemption.
    158 	 */
    159 	ci = l->l_cpu;
    160 #ifdef MULTIPROCESSOR
    161 	if (ci->ci_curlwp == l) {
    162 #else
    163 	if (l == curlwp) {
    164 #endif
    165 		l->l_stat = LSONPROC;
    166 		l->l_slptime = 0;
    167 		sched_unlock(1);
    168 		return 0;
    169 	}
    170 
    171 	/*
    172 	 * Set it running.  We'll try to get the last CPU that ran
    173 	 * this LWP to pick it up again.
    174 	 */
    175 	sched_setrunnable(l);
    176 	l->l_stat = LSRUN;
    177 	l->l_slptime = 0;
    178 	if ((l->l_flag & LW_INMEM) != 0) {
    179 		sched_enqueue(l, false);
    180 		if (lwp_eprio(l) < ci->ci_schedstate.spc_curpriority)
    181 			cpu_need_resched(ci, 0);
    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, struct lwp *l, syncobj_t *sobj)
    197 {
    198 	struct lwp *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 	struct lwp *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 	l->l_nvcsw++;
    232 
    233 	sq->sq_waiters++;
    234 	sleepq_insert(sq, l, sobj);
    235 }
    236 
    237 void
    238 sleepq_switch(int timo, int catch)
    239 {
    240 	struct lwp *l = curlwp;
    241 
    242 #ifdef KTRACE
    243 	if (KTRPOINT(l->l_proc, KTR_CSW))
    244 		ktrcsw(l, 1, 0);
    245 #endif
    246 
    247 	/*
    248 	 * If sleeping interruptably, check for pending signals, exits or
    249 	 * core dump events.
    250 	 */
    251 	if (catch) {
    252 		l->l_flag |= LW_SINTR;
    253 		if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    254 			l->l_sleeperr = EPASSTHROUGH;
    255 			/* lwp_unsleep() will release the lock */
    256 			lwp_unsleep(l);
    257 			return;
    258 		}
    259 		if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
    260 			l->l_flag &= ~LW_CANCELLED;
    261 			l->l_sleeperr = EINTR;
    262 			/* lwp_unsleep() will release the lock */
    263 			lwp_unsleep(l);
    264 			return;
    265 		}
    266 	}
    267 
    268 	if (timo)
    269 		callout_reset(&l->l_tsleep_ch, timo, sleepq_timeout, l);
    270 
    271 	mi_switch(l);
    272 	l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
    273 
    274 	/*
    275 	 * When we reach this point, the LWP and sleep queue are unlocked.
    276 	 */
    277 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    278 }
    279 
    280 /*
    281  * sleepq_block:
    282  *
    283  *	Enter an LWP into the sleep queue and prepare for sleep.  The sleep
    284  *	queue must already be locked, and any interlock (such as the kernel
    285  *	lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
    286  *
    287  * 	sleepq_block() may return early under exceptional conditions, for
    288  * 	example if the LWP's containing process is exiting.
    289  */
    290 void
    291 sleepq_block(sleepq_t *sq, pri_t pri, wchan_t wchan, const char *wmesg,
    292 	     int timo, int catch, syncobj_t *sobj)
    293 {
    294 
    295 	sleepq_enqueue(sq, pri, wchan, wmesg, sobj);
    296 	sleepq_switch(timo, catch);
    297 }
    298 
    299 /*
    300  * sleepq_unblock:
    301  *
    302  *	After any intermediate step such as updating statistics, re-acquire
    303  *	the kernel lock and record the switch for ktrace.  Note that we are
    304  *	no longer on the sleep queue at this point.
    305  *
    306  *	This is split out from sleepq_block() in expectation that at some
    307  *	point in the future, LWPs may awake on different kernel stacks than
    308  *	those they went asleep on.
    309  */
    310 int
    311 sleepq_unblock(int timo, int catch)
    312 {
    313 	int error, expired, sig;
    314 	struct proc *p;
    315 	struct lwp *l;
    316 
    317 	l = curlwp;
    318 	error = l->l_sleeperr;
    319 
    320 	if (timo) {
    321 		/*
    322 		 * Even if the callout appears to have fired, we need to
    323 		 * stop it in order to synchronise with other CPUs.
    324 		 */
    325 		expired = callout_expired(&l->l_tsleep_ch);
    326 		callout_stop(&l->l_tsleep_ch);
    327 		if (expired && error == 0)
    328 			error = EWOULDBLOCK;
    329 	}
    330 
    331 	if (catch && (error == 0 || error == EPASSTHROUGH)) {
    332 		l->l_sleeperr = 0;
    333 		p = l->l_proc;
    334 		if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
    335 			error = EINTR;
    336 		else if ((l->l_flag & LW_PENDSIG) != 0) {
    337 			KERNEL_LOCK(1, l);	/* XXXSMP pool_put() */
    338 			mutex_enter(&p->p_smutex);
    339 			if ((sig = issignal(l)) != 0)
    340 				error = sleepq_sigtoerror(l, sig);
    341 			mutex_exit(&p->p_smutex);
    342 			KERNEL_UNLOCK_LAST(l);
    343 		}
    344 		if (error == EPASSTHROUGH) {
    345 			/* Raced */
    346 			error = EINTR;
    347 		}
    348 	}
    349 
    350 #ifdef KTRACE
    351 	if (KTRPOINT(l->l_proc, KTR_CSW))
    352 		ktrcsw(l, 0, 0);
    353 #endif
    354 
    355 	KERNEL_LOCK(l->l_biglocks, l);
    356 	return error;
    357 }
    358 
    359 /*
    360  * sleepq_wake:
    361  *
    362  *	Wake zero or more LWPs blocked on a single wait channel.
    363  */
    364 void
    365 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
    366 {
    367 	struct lwp *l, *next;
    368 	int swapin = 0;
    369 
    370 	KASSERT(mutex_owned(sq->sq_mutex));
    371 
    372 	for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
    373 		KASSERT(l->l_sleepq == sq);
    374 		next = TAILQ_NEXT(l, l_sleepchain);
    375 		if (l->l_wchan != wchan)
    376 			continue;
    377 		swapin |= sleepq_remove(sq, l);
    378 		if (--expected == 0)
    379 			break;
    380 	}
    381 
    382 	sleepq_unlock(sq);
    383 
    384 	/*
    385 	 * If there are newly awakend threads that need to be swapped in,
    386 	 * then kick the swapper into action.
    387 	 */
    388 	if (swapin)
    389 		uvm_kick_scheduler();
    390 }
    391 
    392 /*
    393  * sleepq_unsleep:
    394  *
    395  *	Remove an LWP from its sleep queue and set it runnable again.
    396  *	sleepq_unsleep() is called with the LWP's mutex held, and will
    397  *	always release it.
    398  */
    399 void
    400 sleepq_unsleep(struct lwp *l)
    401 {
    402 	sleepq_t *sq = l->l_sleepq;
    403 	int swapin;
    404 
    405 	KASSERT(lwp_locked(l, NULL));
    406 	KASSERT(l->l_wchan != NULL);
    407 	KASSERT(l->l_mutex == sq->sq_mutex);
    408 
    409 	swapin = sleepq_remove(sq, l);
    410 	sleepq_unlock(sq);
    411 
    412 	if (swapin)
    413 		uvm_kick_scheduler();
    414 }
    415 
    416 /*
    417  * sleepq_timeout:
    418  *
    419  *	Entered via the callout(9) subsystem to time out an LWP that is on a
    420  *	sleep queue.
    421  */
    422 void
    423 sleepq_timeout(void *arg)
    424 {
    425 	struct lwp *l = arg;
    426 
    427 	/*
    428 	 * Lock the LWP.  Assuming it's still on the sleep queue, its
    429 	 * current mutex will also be the sleep queue mutex.
    430 	 */
    431 	lwp_lock(l);
    432 
    433 	if (l->l_wchan == NULL) {
    434 		/* Somebody beat us to it. */
    435 		lwp_unlock(l);
    436 		return;
    437 	}
    438 
    439 	lwp_unsleep(l);
    440 }
    441 
    442 /*
    443  * sleepq_sigtoerror:
    444  *
    445  *	Given a signal number, interpret and return an error code.
    446  */
    447 int
    448 sleepq_sigtoerror(struct lwp *l, int sig)
    449 {
    450 	struct proc *p = l->l_proc;
    451 	int error;
    452 
    453 	KASSERT(mutex_owned(&p->p_smutex));
    454 
    455 	/*
    456 	 * If this sleep was canceled, don't let the syscall restart.
    457 	 */
    458 	if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
    459 		error = EINTR;
    460 	else
    461 		error = ERESTART;
    462 
    463 	return error;
    464 }
    465 
    466 /*
    467  * sleepq_abort:
    468  *
    469  *	After a panic or during autoconfiguration, lower the interrupt
    470  *	priority level to give pending interrupts a chance to run, and
    471  *	then return.  Called if sleepq_dontsleep() returns non-zero, and
    472  *	always returns zero.
    473  */
    474 int
    475 sleepq_abort(kmutex_t *mtx, int unlock)
    476 {
    477 	extern int safepri;
    478 	int s;
    479 
    480 	s = splhigh();
    481 	splx(safepri);
    482 	splx(s);
    483 	if (mtx != NULL && unlock != 0)
    484 		mutex_exit(mtx);
    485 
    486 	return 0;
    487 }
    488 
    489 /*
    490  * sleepq_changepri:
    491  *
    492  *	Adjust the priority of an LWP residing on a sleepq.  This method
    493  *	will only alter the user priority; the effective priority is
    494  *	assumed to have been fixed at the time of insertion into the queue.
    495  */
    496 void
    497 sleepq_changepri(struct lwp *l, pri_t pri)
    498 {
    499 
    500 	KASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    501 	l->l_usrpri = pri;
    502 }
    503 
    504 void
    505 sleepq_lendpri(struct lwp *l, pri_t pri)
    506 {
    507 	sleepq_t *sq = l->l_sleepq;
    508 	pri_t opri;
    509 
    510 	KASSERT(lwp_locked(l, sq->sq_mutex));
    511 
    512 	opri = lwp_eprio(l);
    513 	l->l_inheritedprio = pri;
    514 
    515 	if (lwp_eprio(l) != opri &&
    516 	    (l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
    517 		TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    518 		sleepq_insert(sq, l, l->l_syncobj);
    519 	}
    520 }
    521