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