Home | History | Annotate | Line # | Download | only in kern
kern_sleepq.c revision 1.7.2.13
      1 /*	$NetBSD: kern_sleepq.c,v 1.7.2.13 2007/10/09 13:44:28 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.13 2007/10/09 13:44:28 ad 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 	pri_t pri;
    111 
    112 	KASSERT(lwp_locked(l, sq->sq_mutex));
    113 	KASSERT(sq->sq_waiters > 0);
    114 
    115 	sq->sq_waiters--;
    116 	TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    117 
    118 #ifdef DIAGNOSTIC
    119 	if (sq->sq_waiters == 0)
    120 		KASSERT(TAILQ_FIRST(&sq->sq_queue) == NULL);
    121 	else
    122 		KASSERT(TAILQ_FIRST(&sq->sq_queue) != NULL);
    123 #endif
    124 
    125 	l->l_syncobj = &sched_syncobj;
    126 	l->l_wchan = NULL;
    127 	l->l_sleepq = NULL;
    128 	l->l_flag &= ~LW_SINTR;
    129 
    130 	ci = l->l_cpu;
    131 	spc = &ci->ci_schedstate;
    132 
    133 	/*
    134 	 * If not sleeping, the LWP must have been suspended.  Let whoever
    135 	 * holds it stopped set it running again.
    136 	 */
    137 	if (l->l_stat != LSSLEEP) {
    138 	 	KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
    139 		lwp_setlock(l, &spc->spc_lwplock);
    140 		return 0;
    141 	}
    142 
    143 	/*
    144 	 * If the LWP is still on the CPU, mark it as LSONPROC.  It may be
    145 	 * about to call mi_switch(), in which case it will yield.
    146 	 */
    147 	if ((l->l_flag & LW_RUNNING) != 0) {
    148 		l->l_stat = LSONPROC;
    149 		l->l_slptime = 0;
    150 		lwp_setlock(l, &spc->spc_lwplock);
    151 		return 0;
    152 	}
    153 
    154 	/*
    155 	 * Set it running.  We'll try to get the last CPU that ran
    156 	 * this LWP to pick it up again.
    157 	 */
    158 	spc_lock(ci);
    159 	lwp_setlock(l, spc->spc_mutex);
    160 	sched_setrunnable(l);
    161 	l->l_stat = LSRUN;
    162 	l->l_slptime = 0;
    163 	if ((l->l_flag & LW_INMEM) != 0) {
    164 		sched_enqueue(l, false);
    165 		if (lwp_eprio(l) >= PRI_KERNEL ||
    166 		    ci->ci_curlwp == ci->ci_data.cpu_idlelwp)
    167 			cpu_need_resched(ci, RESCHED_IMMED);
    168 		spc_unlock(ci);
    169 		return 0;
    170 	}
    171 	spc_unlock(ci);
    172 	return 1;
    173 }
    174 
    175 /*
    176  * sleepq_insert:
    177  *
    178  *	Insert an LWP into the sleep queue, optionally sorting by priority.
    179  */
    180 inline void
    181 sleepq_insert(sleepq_t *sq, lwp_t *l, syncobj_t *sobj)
    182 {
    183 	lwp_t *l2;
    184 	const int pri = lwp_eprio(l);
    185 
    186 	switch (sobj->sobj_flag) {
    187 	case SOBJ_SLEEPQ_SORTED:
    188 		TAILQ_FOREACH(l2, &sq->sq_queue, l_sleepchain) {
    189 			if (lwp_eprio(l2) < pri) {
    190 				TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
    191 				return;
    192 			}
    193 		}
    194 		/* FALLTHROUGH */
    195 	case SOBJ_SLEEPQ_FIFO:
    196 		TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_sleepchain);
    197 		break;
    198 	case SOBJ_SLEEPQ_LIFO:
    199 		TAILQ_INSERT_HEAD(&sq->sq_queue, l, l_sleepchain);
    200 		break;
    201 	}
    202 }
    203 
    204 /*
    205  * sleepq_enqueue:
    206  *
    207  *	Enter an LWP into the sleep queue and prepare for sleep.  The sleep
    208  *	queue must already be locked, and any interlock (such as the kernel
    209  *	lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
    210  */
    211 void
    212 sleepq_enqueue(sleepq_t *sq, pri_t pri, wchan_t wchan, const char *wmesg,
    213 	       syncobj_t *sobj)
    214 {
    215 	lwp_t *l = curlwp;
    216 
    217 	KASSERT(mutex_owned(sq->sq_mutex));
    218 	KASSERT(l->l_stat == LSONPROC);
    219 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    220 
    221 	l->l_syncobj = sobj;
    222 	l->l_wchan = wchan;
    223 	l->l_sleepq = sq;
    224 	l->l_wmesg = wmesg;
    225 	l->l_slptime = 0;
    226 	l->l_priority = pri;
    227 	l->l_stat = LSSLEEP;
    228 	l->l_sleeperr = 0;
    229 
    230 	sq->sq_waiters++;
    231 	sleepq_insert(sq, l, sobj);
    232 }
    233 
    234 /*
    235  * sleepq_block:
    236  *
    237  *	After any intermediate step such as releasing an interlock, switch.
    238  * 	sleepq_block() may return early under exceptional conditions, for
    239  * 	example if the LWP's containing process is exiting.
    240  */
    241 int
    242 sleepq_block(int timo, bool catch)
    243 {
    244 	int error = 0, sig;
    245 	struct proc *p;
    246 	lwp_t *l = curlwp;
    247 	bool early = false;
    248 
    249 	ktrcsw(1, 0);
    250 
    251 	/*
    252 	 * If sleeping interruptably, check for pending signals, exits or
    253 	 * core dump events.
    254 	 */
    255 	if (catch) {
    256 		l->l_flag |= LW_SINTR;
    257 		if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    258 			early = true;
    259 		}
    260 		if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
    261 			l->l_flag &= ~LW_CANCELLED;
    262 			early = true;
    263 		}
    264 	}
    265 
    266 	if (early) {
    267 		/* lwp_unsleep() will release the lock */
    268 		lwp_unsleep(l);
    269 	} else {
    270 		if (timo)
    271 			callout_schedule(&l->l_timeout_ch, timo);
    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 			if (callout_stop(&l->l_timeout_ch))
    285 				error = EWOULDBLOCK;
    286 		}
    287 	}
    288 
    289 	if (catch && error == 0) {
    290 		p = l->l_proc;
    291 		if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
    292 			error = EINTR;
    293 		else if ((l->l_flag & LW_PENDSIG) != 0) {
    294 			mutex_enter(&p->p_smutex);
    295 			if ((sig = issignal(l)) != 0)
    296 				error = sleepq_sigtoerror(l, sig);
    297 			mutex_exit(&p->p_smutex);
    298 		}
    299 	}
    300 
    301 	ktrcsw(0, 0);
    302 
    303 	KERNEL_LOCK(l->l_biglocks, l);
    304 	return error;
    305 }
    306 
    307 /*
    308  * sleepq_wake:
    309  *
    310  *	Wake zero or more LWPs blocked on a single wait channel.
    311  */
    312 lwp_t *
    313 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
    314 {
    315 	lwp_t *l, *next;
    316 	int swapin = 0;
    317 
    318 	KASSERT(mutex_owned(sq->sq_mutex));
    319 
    320 	for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
    321 		KASSERT(l->l_sleepq == sq);
    322 		next = TAILQ_NEXT(l, l_sleepchain);
    323 		if (l->l_wchan != wchan)
    324 			continue;
    325 		swapin |= sleepq_remove(sq, l);
    326 		if (--expected == 0)
    327 			break;
    328 	}
    329 
    330 	sleepq_unlock(sq);
    331 
    332 	/*
    333 	 * If there are newly awakend threads that need to be swapped in,
    334 	 * then kick the swapper into action.
    335 	 */
    336 	if (swapin)
    337 		uvm_kick_scheduler();
    338 
    339 	return l;
    340 }
    341 
    342 /*
    343  * sleepq_unsleep:
    344  *
    345  *	Remove an LWP from its sleep queue and set it runnable again.
    346  *	sleepq_unsleep() is called with the LWP's mutex held, and will
    347  *	always release it.
    348  */
    349 void
    350 sleepq_unsleep(lwp_t *l)
    351 {
    352 	sleepq_t *sq = l->l_sleepq;
    353 	int swapin;
    354 
    355 	KASSERT(lwp_locked(l, NULL));
    356 	KASSERT(l->l_wchan != NULL);
    357 	KASSERT(l->l_mutex == sq->sq_mutex);
    358 
    359 	swapin = sleepq_remove(sq, l);
    360 	sleepq_unlock(sq);
    361 
    362 	if (swapin)
    363 		uvm_kick_scheduler();
    364 }
    365 
    366 /*
    367  * sleepq_timeout:
    368  *
    369  *	Entered via the callout(9) subsystem to time out an LWP that is on a
    370  *	sleep queue.
    371  */
    372 void
    373 sleepq_timeout(void *arg)
    374 {
    375 	lwp_t *l = arg;
    376 
    377 	/*
    378 	 * Lock the LWP.  Assuming it's still on the sleep queue, its
    379 	 * current mutex will also be the sleep queue mutex.
    380 	 */
    381 	lwp_lock(l);
    382 
    383 	if (l->l_wchan == NULL) {
    384 		/* Somebody beat us to it. */
    385 		lwp_unlock(l);
    386 		return;
    387 	}
    388 
    389 	lwp_unsleep(l);
    390 }
    391 
    392 /*
    393  * sleepq_sigtoerror:
    394  *
    395  *	Given a signal number, interpret and return an error code.
    396  */
    397 int
    398 sleepq_sigtoerror(lwp_t *l, int sig)
    399 {
    400 	struct proc *p = l->l_proc;
    401 	int error;
    402 
    403 	KASSERT(mutex_owned(&p->p_smutex));
    404 
    405 	/*
    406 	 * If this sleep was canceled, don't let the syscall restart.
    407 	 */
    408 	if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
    409 		error = EINTR;
    410 	else
    411 		error = ERESTART;
    412 
    413 	return error;
    414 }
    415 
    416 /*
    417  * sleepq_abort:
    418  *
    419  *	After a panic or during autoconfiguration, lower the interrupt
    420  *	priority level to give pending interrupts a chance to run, and
    421  *	then return.  Called if sleepq_dontsleep() returns non-zero, and
    422  *	always returns zero.
    423  */
    424 int
    425 sleepq_abort(kmutex_t *mtx, int unlock)
    426 {
    427 	extern int safepri;
    428 	int s;
    429 
    430 	s = splhigh();
    431 	splx(safepri);
    432 	splx(s);
    433 	if (mtx != NULL && unlock != 0)
    434 		mutex_exit(mtx);
    435 
    436 	return 0;
    437 }
    438 
    439 /*
    440  * sleepq_changepri:
    441  *
    442  *	Adjust the priority of an LWP residing on a sleepq.  This method
    443  *	will only alter the user priority; the effective priority is
    444  *	assumed to have been fixed at the time of insertion into the queue.
    445  */
    446 void
    447 sleepq_changepri(lwp_t *l, pri_t pri)
    448 {
    449 	sleepq_t *sq = l->l_sleepq;
    450 	pri_t opri;
    451 
    452 	KASSERT(lwp_locked(l, sq->sq_mutex));
    453 
    454 	opri = lwp_eprio(l);
    455 	l->l_usrpri = pri;
    456 	l->l_priority = sched_kpri(l);
    457 
    458 	if (lwp_eprio(l) != opri) {
    459 		TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    460 		sleepq_insert(sq, l, l->l_syncobj);
    461 	}
    462 }
    463 
    464 void
    465 sleepq_lendpri(lwp_t *l, pri_t pri)
    466 {
    467 	sleepq_t *sq = l->l_sleepq;
    468 	pri_t opri;
    469 
    470 	KASSERT(lwp_locked(l, sq->sq_mutex));
    471 
    472 	opri = lwp_eprio(l);
    473 	l->l_inheritedprio = pri;
    474 
    475 	if (lwp_eprio(l) != opri &&
    476 	    (l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
    477 		TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    478 		sleepq_insert(sq, l, l->l_syncobj);
    479 	}
    480 }
    481