Home | History | Annotate | Line # | Download | only in kern
kern_sleepq.c revision 1.1.2.6
      1 /*	$NetBSD: kern_sleepq.c,v 1.1.2.6 2006/11/17 16:53:08 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.6 2006/11/17 16:53:08 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 	KASSERT(l->l_stat == LSSLEEP);
    125 
    126 	sq->sq_waiters--;
    127 	TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    128 
    129 #ifdef DIAGNOSTIC
    130 	if (sq->sq_waiters == 0)
    131 		KASSERT(TAILQ_FIRST(&sq->sq_queue) == NULL);
    132 	else
    133 		KASSERT(TAILQ_FIRST(&sq->sq_queue) != NULL);
    134 #endif
    135 
    136 	l->l_syncobj = &sched_syncobj;
    137 	l->l_wchan = NULL;
    138 	l->l_sleepq = NULL;
    139 	l->l_flag &= ~L_SINTR;
    140 
    141 	sched_lock(1);
    142 	lwp_setlock(l, &sched_mutex);
    143 
    144 	/*
    145 	 * If the LWP is still on the CPU, mark it as LSONPROC.  It may be
    146 	 * about to call mi_switch(), in which case it will yield.
    147 	 */
    148 	if ((ci = l->l_cpu) != NULL && ci->ci_curlwp == l) {
    149 		l->l_stat = LSONPROC;
    150 		l->l_slptime = 0;
    151 		sched_unlock(1);
    152 		return 0;
    153 	}
    154 
    155 	if (l->l_proc->p_sa)
    156 		sa_awaken(l);
    157 
    158 	/*
    159 	 * Set it running.  We'll try to get the last CPU that ran
    160 	 * this LWP to pick it up again.
    161 	 */
    162 	if (l->l_stat == LSSLEEP)
    163 		l->l_stat = LSRUN;
    164 	if (l->l_slptime > 1)
    165 		updatepri(l);
    166 	l->l_slptime = 0;
    167 	if ((l->l_flag & L_INMEM) != 0) {
    168 		setrunqueue(l);
    169 		if (l->l_priority < ci->ci_schedstate.spc_curpriority)
    170 			cpu_need_resched(ci);
    171 		sched_unlock(1);
    172 		return 0;
    173 	}
    174 
    175 	sched_unlock(1);
    176 	return 1;
    177 }
    178 
    179 /*
    180  * sleepq_insert:
    181  *
    182  *	Insert an LWP into the sleep queue, optionally sorting by priority.
    183  */
    184 static inline void
    185 sleepq_insert(sleepq_t *sq, struct lwp *l, int pri, syncobj_t *sobj)
    186 {
    187 	struct lwp *l2, *l3 = NULL;
    188 
    189 	if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
    190 		TAILQ_FOREACH(l2, &sq->sq_queue, l_sleepchain) {
    191 			l3 = l2;
    192 			if (l2->l_priority > pri)
    193 				break;
    194 		}
    195 	}
    196 
    197 	if (l3 == NULL)
    198 		TAILQ_INSERT_HEAD(&sq->sq_queue, l, l_sleepchain);
    199 	else
    200 		TAILQ_INSERT_BEFORE(l3, l, l_sleepchain);
    201 }
    202 
    203 /*
    204  * sleepq_enter:
    205  *
    206  *	Enter an LWP into the sleep queue and prepare for sleep.  Any interlocking
    207  * 	step such as releasing a mutex or checking for signals may be safely done
    208  *	by the caller once on the sleep queue.
    209  */
    210 void
    211 sleepq_enter(sleepq_t *sq, int pri, wchan_t wchan, const char *wmesg, int timo,
    212 	     int catch, syncobj_t *sobj)
    213 {
    214 	struct lwp *l = curlwp;
    215 
    216 	LOCK_ASSERT(mutex_owned(sq->sq_mutex));
    217 	KASSERT(l->l_stat == LSONPROC);
    218 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    219 
    220 #ifdef KTRACE
    221 	if (KTRPOINT(l->l_proc, KTR_CSW))
    222 		ktrcsw(l, 1, 0);
    223 #endif
    224 
    225 	sq->sq_waiters++;
    226 
    227 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    228 	/*
    229 	 * Acquire the per-LWP mutex and sort it into the sleep queue.  Once
    230 	 * we have that lock, we can release the kernel lock.  XXXSMP Not
    231 	 * yet, since checking for signals may call pool_put().  Otherwise
    232 	 * this is OK.
    233 	 */
    234 	lwp_lock(l);
    235 
    236 #ifdef notyet
    237 	l->l_biglocks = KERNEL_UNLOCK(0, l);
    238 #endif
    239 #endif
    240 
    241 	l->l_syncobj = sobj;
    242 	l->l_wchan = wchan;
    243 	l->l_sleepq = sq;
    244 	l->l_wmesg = wmesg;
    245 	l->l_slptime = 0;
    246 	l->l_priority = pri;
    247 	l->l_stat = LSSLEEP;
    248 	l->l_nvcsw++;
    249 
    250 	if (catch)
    251 		l->l_flag |= L_SINTR;
    252 
    253 	sleepq_insert(sq, l, pri, sobj);
    254 
    255 	if (timo)
    256 		callout_reset(&l->l_tsleep_ch, timo, sleepq_timeout, l);
    257 
    258 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    259 	/*
    260 	 * The LWP is now on the sleep queue.  Release its old mutex and
    261 	 * lend it ours for the duration of the sleep.
    262 	 */
    263 	lwp_unlock_to(l, sq->sq_mutex);
    264 #endif
    265 }
    266 
    267 /*
    268  * sleepq_block:
    269  *
    270  *	The calling LWP has been entered into the sleep queue by
    271  *	sleepq_enter(), and now wants to block.  sleepq_block() may return
    272  *	early under exceptional conditions, for example if the LWP's process
    273  *	is exiting.  sleepq_block() must be called if sleepq_enter() has
    274  *	been called.
    275  */
    276 int
    277 sleepq_block(sleepq_t *sq, int timo)
    278 {
    279 	int error, flag, expired, sig;
    280 	struct lwp *l = curlwp;
    281 	struct proc *p;
    282 
    283 	LOCK_ASSERT(lwp_locked(l, sq->sq_mutex));
    284 
    285 	p = l->l_proc;
    286 	flag = l->l_flag;
    287 	error = 0;
    288 
    289 	/*
    290 	 * If sleeping interruptably, check for pending signals, exits or
    291 	 * core dump events.
    292 	 */
    293 	if ((flag & L_SINTR) != 0) {
    294 		while ((l->l_flag & L_PENDSIG) != 0 && error == 0) {
    295 			lwp_unlock(l);
    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 			lwp_lock(l);
    301 		}
    302 
    303 		if ((l->l_flag & (L_CANCELLED | L_WEXIT | L_WCORE)) != 0) {
    304 			l->l_flag &= ~L_CANCELLED;
    305 			error = EINTR;
    306 		}
    307 
    308 		if (l->l_wchan != NULL) {
    309 			if (error != 0) {
    310 				KASSERT(l->l_stat == LSSLEEP);
    311 				sleepq_remove(sq, l);
    312 				mutex_exit(sq->sq_mutex);
    313 			}
    314 		} else {
    315 			KASSERT(l->l_stat == LSONPROC);
    316 			lwp_unlock(l);
    317 		}
    318 	}
    319 
    320 	if (l->l_stat == LSSLEEP) {
    321 		KASSERT(l->l_wchan != NULL);
    322 
    323 		if ((flag & L_SA) != 0)
    324 			sa_switch(l, sadata_upcall_alloc(0), SA_UPCALL_BLOCKED);
    325 		else {
    326 			mi_switch(l, NULL);
    327 			l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
    328 		}
    329 	}
    330 
    331 	/* When we reach this point, the LWP is unlocked. */
    332 
    333 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    334 
    335 	if (timo) {
    336 		/*
    337 		 * Even if the callout appears to have fired, we need to
    338 		 * stop it in order to synchronise with other CPUs.
    339 		 */
    340 		expired = callout_expired(&l->l_tsleep_ch);
    341 		callout_stop(&l->l_tsleep_ch);
    342 		if (expired && error == 0)
    343 			error = EWOULDBLOCK;
    344 	}
    345 
    346 	if (error == 0 && (flag & L_SINTR) != 0) {
    347 		if ((l->l_flag & (L_CANCELLED | L_WEXIT | L_WCORE)) != 0)
    348 			error = EINTR;
    349 		else if ((l->l_flag & L_PENDSIG) != 0) {
    350 			mutex_enter(&p->p_smutex);
    351 			if ((sig = issignal(l)) != 0)
    352 				error = sleepq_sigtoerror(l, sig);
    353 			mutex_exit(&p->p_smutex);
    354 		}
    355 	}
    356 
    357 	return error;
    358 }
    359 
    360 /*
    361  * sleepq_unblock:
    362  *
    363  *	After any intermediate step such as updating statistics, re-acquire
    364  *	the kernel lock and record the switch for ktrace.  Note that we are
    365  *	no longer on the sleep queue at this point.
    366  */
    367 void
    368 sleepq_unblock(void)
    369 {
    370 	struct lwp *l = curlwp;
    371 
    372 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    373 #ifdef notyet
    374 	/*
    375 	 * Re-acquire the kernel lock.  XXXSMP Let mi_switch() take care of
    376 	 * it, until we can release the lock in sleepq_block().
    377 	 */
    378 	KERNEL_LOCK(l->l_biglocks, l);
    379 #endif
    380 #endif
    381 
    382 #ifdef KTRACE
    383 	if (KTRPOINT(l->l_proc, KTR_CSW))
    384 		ktrcsw(l, 0, 0);
    385 #endif
    386 }
    387 
    388 /*
    389  * sleepq_wake:
    390  *
    391  *	Wake zero or more LWPs blocked on a single wait channel.
    392  */
    393 void
    394 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
    395 {
    396 	struct lwp *l, *next;
    397 	int swapin = 0;
    398 
    399 	LOCK_ASSERT(mutex_owned(sq->sq_mutex));
    400 
    401 	for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
    402 		KASSERT(l->l_sleepq == sq);
    403 		next = TAILQ_NEXT(l, l_sleepchain);
    404 		if (l->l_wchan != wchan)
    405 			continue;
    406 		swapin |= sleepq_remove(sq, l);
    407 		if (--expected == 0)
    408 			break;
    409 	}
    410 
    411 	LOCK_ASSERT(mutex_owned(sq->sq_mutex));
    412 	mutex_exit(sq->sq_mutex);
    413 
    414 	/*
    415 	 * If there are newly awakend threads that need to be swapped in,
    416 	 * then kick the swapper into action.
    417 	 */
    418 	if (swapin)
    419 		wakeup(&proc0);
    420 }
    421 
    422 /*
    423  * sleepq_unsleep:
    424  *
    425  *	Remove an LWP from its sleep queue and set it runnable again.
    426  *	sleepq_unsleep() is called with the LWP's mutex held, and will
    427  *	always release it.
    428  */
    429 void
    430 sleepq_unsleep(struct lwp *l)
    431 {
    432 	sleepq_t *sq = l->l_sleepq;
    433 	int swapin;
    434 
    435 	LOCK_ASSERT(lwp_locked(l, NULL));
    436 	KASSERT(l->l_wchan != NULL);
    437 	KASSERT(l->l_mutex == sq->sq_mutex);
    438 
    439 	swapin = sleepq_remove(sq, l);
    440 	mutex_exit(sq->sq_mutex);
    441 
    442 	if (swapin)
    443 		wakeup(&proc0);
    444 }
    445 
    446 /*
    447  * sleepq_timeout:
    448  *
    449  *	Entered via the callout(9) subsystem to time out an LWP that is on a
    450  *	sleep queue.
    451  */
    452 void
    453 sleepq_timeout(void *arg)
    454 {
    455 	struct lwp *l = arg;
    456 
    457 	/*
    458 	 * Lock the LWP.  Assuming it's still on the sleep queue, its
    459 	 * current mutex will also be the sleep queue mutex.
    460 	 */
    461 	lwp_lock(l);
    462 
    463 	if (l->l_wchan == NULL) {
    464 		/* Somebody beat us to it. */
    465 		lwp_unlock(l);
    466 		return;
    467 	}
    468 
    469 	sleepq_unsleep(l);
    470 }
    471 
    472 /*
    473  * sleepq_sigtoerror:
    474  *
    475  *	Given a signal number, interpret and return an error code.
    476  */
    477 int
    478 sleepq_sigtoerror(struct lwp *l, int sig)
    479 {
    480 	struct proc *p = l->l_proc;
    481 	int error;
    482 
    483 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    484 
    485 	/*
    486 	 * If this sleep was canceled, don't let the syscall restart.
    487 	 */
    488 	if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
    489 		error = EINTR;
    490 	else
    491 		error = ERESTART;
    492 
    493 	return error;
    494 }
    495 
    496 /*
    497  * sleepq_abort:
    498  *
    499  *	After a panic or during autoconfiguration, lower the interrupt
    500  *	priority level to give pending interrupts a chance to run, and
    501  *	then return.  Called if sleepq_dontsleep() returns non-zero, and
    502  *	always returns zero.
    503  */
    504 int
    505 sleepq_abort(kmutex_t *mtx, int unlock)
    506 {
    507 	extern int safepri;
    508 	int s;
    509 
    510 	s = splhigh();
    511 	splx(safepri);
    512 	splx(s);
    513 	if (mtx != NULL && unlock != 0)
    514 		mutex_exit(mtx);
    515 
    516 	return 0;
    517 }
    518 
    519 /*
    520  * sleepq_changepri:
    521  *
    522  *	Adjust the priority of an LWP residing on a sleepq.
    523  */
    524 void
    525 sleepq_changepri(struct lwp *l, int pri)
    526 {
    527 	sleepq_t *sq = l->l_sleepq;
    528 
    529 	KASSERT(lwp_locked(l, sq->sq_mutex));
    530 
    531 	TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    532 	sleepq_insert(sq, l, pri, l->l_syncobj);
    533 }
    534