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