Home | History | Annotate | Line # | Download | only in kern
kern_sleepq.c revision 1.1.2.10
      1 /*	$NetBSD: kern_sleepq.c,v 1.1.2.10 2007/01/27 14:00:02 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 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.1.2.10 2007/01/27 14:00:02 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/sa.h>
     60 #include <sys/savar.h>
     61 #include <sys/sleepq.h>
     62 
     63 #ifdef KTRACE
     64 #include <sys/ktrace.h>
     65 #endif
     66 
     67 int	sleepq_sigtoerror(struct lwp *, int);
     68 void	updatepri(struct lwp *);
     69 void	sa_awaken(struct lwp *);
     70 
     71 /* General purpose sleep table, used by ltsleep() and condition variables. */
     72 sleeptab_t	sleeptab;
     73 
     74 /*
     75  * sleeptab_init:
     76  *
     77  *	Initialize a sleep table.
     78  */
     79 void
     80 sleeptab_init(sleeptab_t *st)
     81 {
     82 	sleepq_t *sq;
     83 	int i;
     84 
     85 	for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
     86 		sq = &st->st_queues[i];
     87 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
     88 		mutex_init(&st->st_mutexes[i], MUTEX_SPIN, IPL_SCHED);
     89 		sleepq_init(sq, &st->st_mutexes[i]);
     90 #else
     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 	LOCK_ASSERT(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 &= ~L_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 	if (l->l_proc->p_sa != NULL)
    176 		sa_awaken(l);
    177 	if (l->l_slptime > 1)
    178 		updatepri(l);
    179 	l->l_stat = LSRUN;
    180 	l->l_slptime = 0;
    181 	if ((l->l_flag & L_INMEM) != 0) {
    182 		setrunqueue(l);
    183 		if (l->l_priority < ci->ci_schedstate.spc_curpriority)
    184 			cpu_need_resched(ci);
    185 		sched_unlock(1);
    186 		return 0;
    187 	}
    188 
    189 	sched_unlock(1);
    190 	return 1;
    191 }
    192 
    193 /*
    194  * sleepq_insert:
    195  *
    196  *	Insert an LWP into the sleep queue, optionally sorting by priority.
    197  */
    198 static inline void
    199 sleepq_insert(sleepq_t *sq, struct lwp *l, int pri, syncobj_t *sobj)
    200 {
    201 	struct lwp *l2;
    202 
    203 	if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
    204 		TAILQ_FOREACH(l2, &sq->sq_queue, l_sleepchain) {
    205 			if (l2->l_priority > pri) {
    206 				TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
    207 				return;
    208 			}
    209 		}
    210 	}
    211 
    212 	TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_sleepchain);
    213 }
    214 
    215 /*
    216  * sleepq_block:
    217  *
    218  *	Enter an LWP into the sleep queue and prepare for sleep.  The sleep
    219  *	queue must already be locked, and any interlock (such as the kernel
    220  *	lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
    221  *
    222  * 	sleepq_block() may return early under exceptional conditions, for
    223  * 	example if the LWP's containing process is exiting.
    224  */
    225 void
    226 sleepq_block(sleepq_t *sq, int pri, wchan_t wchan, const char *wmesg, int timo,
    227 	     int catch, syncobj_t *sobj)
    228 {
    229 	struct lwp *l = curlwp;
    230 
    231 	LOCK_ASSERT(mutex_owned(sq->sq_mutex));
    232 	KASSERT(l->l_stat == LSONPROC);
    233 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    234 
    235 #ifdef KTRACE
    236 	if (KTRPOINT(l->l_proc, KTR_CSW))
    237 		ktrcsw(l, 1, 0);
    238 #endif
    239 
    240 	l->l_syncobj = sobj;
    241 	l->l_wchan = wchan;
    242 	l->l_sleepq = sq;
    243 	l->l_wmesg = wmesg;
    244 	l->l_slptime = 0;
    245 	l->l_priority = pri;
    246 	l->l_stat = LSSLEEP;
    247 	l->l_sleeperr = 0;
    248 	l->l_nvcsw++;
    249 
    250 	sq->sq_waiters++;
    251 	sleepq_insert(sq, l, pri, sobj);
    252 
    253 	/*
    254 	 * If sleeping interruptably, check for pending signals, exits or
    255 	 * core dump events.
    256 	 */
    257 	if (catch) {
    258 		l->l_flag |= L_SINTR;
    259 		if ((l->l_flag & L_PENDSIG) != 0 && sigispending(l, 0)) {
    260 			l->l_sleeperr = EPASSTHROUGH;
    261 			/* lwp_unsleep() will release the lock */
    262 			lwp_unsleep(l);
    263 			return;
    264 		}
    265 		if ((l->l_flag & (L_CANCELLED|L_WEXIT|L_WCORE)) != 0) {
    266 			l->l_flag &= ~L_CANCELLED;
    267 			l->l_sleeperr = EINTR;
    268 			/* lwp_unsleep() will release the lock */
    269 			lwp_unsleep(l);
    270 			return;
    271 		}
    272 	}
    273 
    274 	if (timo)
    275 		callout_reset(&l->l_tsleep_ch, timo, sleepq_timeout, l);
    276 
    277 	if ((l->l_flag & L_SA) != 0) {
    278 		/* XXXAD Allocating memory while on sleep queue */
    279 		sa_switch(l, sadata_upcall_alloc(0), SA_UPCALL_BLOCKED);
    280 	} else {
    281 		mi_switch(l, NULL);
    282 		l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
    283 	}
    284 
    285 	/*
    286 	 * When we reach this point, the LWP and sleep queue are unlocked.
    287 	 */
    288 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    289 }
    290 
    291 /*
    292  * sleepq_unblock:
    293  *
    294  *	After any intermediate step such as updating statistics, re-acquire
    295  *	the kernel lock and record the switch for ktrace.  Note that we are
    296  *	no longer on the sleep queue at this point.
    297  *
    298  *	This is split out from sleepq_block() in expectation that at some
    299  *	point in the future, LWPs may awake on different kernel stacks than
    300  *	those they went asleep on.
    301  */
    302 int
    303 sleepq_unblock(int timo, int catch)
    304 {
    305 	int error, expired, sig;
    306 	struct proc *p;
    307 	struct lwp *l;
    308 
    309 	l = curlwp;
    310 	error = l->l_sleeperr;
    311 
    312 	if (timo) {
    313 		/*
    314 		 * Even if the callout appears to have fired, we need to
    315 		 * stop it in order to synchronise with other CPUs.
    316 		 */
    317 		expired = callout_expired(&l->l_tsleep_ch);
    318 		callout_stop(&l->l_tsleep_ch);
    319 		if (expired && error == 0)
    320 			error = EWOULDBLOCK;
    321 	}
    322 
    323 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    324 	/*
    325 	 * Re-acquire the kernel lock.  XXXSMP This can come after the
    326 	 * ktrace stuff below once its safe to allocate memory unlocked.
    327 	 */
    328 	KERNEL_LOCK(l->l_biglocks, l);
    329 #endif
    330 
    331 	if (catch && (error == 0 || error == EPASSTHROUGH)) {
    332 		l->l_sleeperr = 0;
    333 		p = l->l_proc;
    334 		if ((l->l_flag & (L_CANCELLED | L_WEXIT | L_WCORE)) != 0)
    335 			error = EINTR;
    336 		else if ((l->l_flag & L_PENDSIG) != 0) {
    337 			mutex_enter(&p->p_smutex);
    338 			if ((sig = issignal(l)) != 0)
    339 				error = sleepq_sigtoerror(l, sig);
    340 			mutex_exit(&p->p_smutex);
    341 		}
    342 		if (error == EPASSTHROUGH) {
    343 			/* Raced */
    344 			error = EINTR;
    345 		}
    346 	}
    347 
    348 #ifdef KTRACE
    349 	if (KTRPOINT(l->l_proc, KTR_CSW))
    350 		ktrcsw(l, 0, 0);
    351 #endif
    352 
    353 	return error;
    354 }
    355 
    356 /*
    357  * sleepq_wake:
    358  *
    359  *	Wake zero or more LWPs blocked on a single wait channel.
    360  */
    361 void
    362 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
    363 {
    364 	struct lwp *l, *next;
    365 	int swapin = 0;
    366 
    367 	LOCK_ASSERT(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 	LOCK_ASSERT(mutex_owned(sq->sq_mutex));
    380 	sleepq_unlock(sq);
    381 
    382 	/*
    383 	 * If there are newly awakend threads that need to be swapped in,
    384 	 * then kick the swapper into action.
    385 	 */
    386 	if (swapin)
    387 		wakeup(&proc0);
    388 }
    389 
    390 /*
    391  * sleepq_unsleep:
    392  *
    393  *	Remove an LWP from its sleep queue and set it runnable again.
    394  *	sleepq_unsleep() is called with the LWP's mutex held, and will
    395  *	always release it.
    396  */
    397 void
    398 sleepq_unsleep(struct lwp *l)
    399 {
    400 	sleepq_t *sq = l->l_sleepq;
    401 	int swapin;
    402 
    403 	LOCK_ASSERT(lwp_locked(l, NULL));
    404 	KASSERT(l->l_wchan != NULL);
    405 	KASSERT(l->l_mutex == sq->sq_mutex);
    406 
    407 	swapin = sleepq_remove(sq, l);
    408 	sleepq_unlock(sq);
    409 
    410 	if (swapin)
    411 		wakeup(&proc0);
    412 }
    413 
    414 /*
    415  * sleepq_timeout:
    416  *
    417  *	Entered via the callout(9) subsystem to time out an LWP that is on a
    418  *	sleep queue.
    419  */
    420 void
    421 sleepq_timeout(void *arg)
    422 {
    423 	struct lwp *l = arg;
    424 
    425 	/*
    426 	 * Lock the LWP.  Assuming it's still on the sleep queue, its
    427 	 * current mutex will also be the sleep queue mutex.
    428 	 */
    429 	lwp_lock(l);
    430 
    431 	if (l->l_wchan == NULL) {
    432 		/* Somebody beat us to it. */
    433 		lwp_unlock(l);
    434 		return;
    435 	}
    436 
    437 	lwp_unsleep(l);
    438 }
    439 
    440 /*
    441  * sleepq_sigtoerror:
    442  *
    443  *	Given a signal number, interpret and return an error code.
    444  */
    445 int
    446 sleepq_sigtoerror(struct lwp *l, int sig)
    447 {
    448 	struct proc *p = l->l_proc;
    449 	int error;
    450 
    451 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    452 
    453 	/*
    454 	 * If this sleep was canceled, don't let the syscall restart.
    455 	 */
    456 	if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
    457 		error = EINTR;
    458 	else
    459 		error = ERESTART;
    460 
    461 	return error;
    462 }
    463 
    464 /*
    465  * sleepq_abort:
    466  *
    467  *	After a panic or during autoconfiguration, lower the interrupt
    468  *	priority level to give pending interrupts a chance to run, and
    469  *	then return.  Called if sleepq_dontsleep() returns non-zero, and
    470  *	always returns zero.
    471  */
    472 int
    473 sleepq_abort(kmutex_t *mtx, int unlock)
    474 {
    475 	extern int safepri;
    476 	int s;
    477 
    478 	s = splhigh();
    479 	splx(safepri);
    480 	splx(s);
    481 	if (mtx != NULL && unlock != 0)
    482 		mutex_exit(mtx);
    483 
    484 	return 0;
    485 }
    486 
    487 /*
    488  * sleepq_changepri:
    489  *
    490  *	Adjust the priority of an LWP residing on a sleepq.
    491  */
    492 void
    493 sleepq_changepri(struct lwp *l, int pri)
    494 {
    495 	sleepq_t *sq = l->l_sleepq;
    496 
    497 	KASSERT(lwp_locked(l, sq->sq_mutex));
    498 
    499 	if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0)
    500 		return;
    501 
    502 	TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    503 	sleepq_insert(sq, l, pri, l->l_syncobj);
    504 }
    505