Home | History | Annotate | Line # | Download | only in kern
kern_turnstile.c revision 1.6.2.7
      1 /*	$NetBSD: kern_turnstile.c,v 1.6.2.7 2007/09/01 12:56:48 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 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 Jason R. Thorpe and 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  * Turnstiles are described in detail in:
     41  *
     42  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     43  *	    Richard McDougall.
     44  *
     45  * Turnstiles are kept in a hash table.  There are likely to be many more
     46  * synchronisation objects than there are threads.  Since a thread can block
     47  * on only one lock at a time, we only need one turnstile per thread, and
     48  * so they are allocated at thread creation time.
     49  *
     50  * When a thread decides it needs to block on a lock, it looks up the
     51  * active turnstile for that lock.  If no active turnstile exists, then
     52  * the process lends its turnstile to the lock.  If there is already an
     53  * active turnstile for the lock, the thread places its turnstile on a
     54  * list of free turnstiles, and references the active one instead.
     55  *
     56  * The act of looking up the turnstile acquires an interlock on the sleep
     57  * queue.  If a thread decides it doesn't need to block after all, then this
     58  * interlock must be released by explicitly aborting the turnstile
     59  * operation.
     60  *
     61  * When a thread is awakened, it needs to get its turnstile back.  If there
     62  * are still other threads waiting in the active turnstile, the the thread
     63  * grabs a free turnstile off the free list.  Otherwise, it can take back
     64  * the active turnstile from the lock (thus deactivating the turnstile).
     65  *
     66  * Turnstiles are the place to do priority inheritence.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.6.2.7 2007/09/01 12:56:48 ad Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/lock.h>
     74 #include <sys/lockdebug.h>
     75 #include <sys/pool.h>
     76 #include <sys/proc.h>
     77 #include <sys/sleepq.h>
     78 #include <sys/systm.h>
     79 
     80 #include <uvm/uvm_extern.h>
     81 
     82 #define	TS_HASH_SIZE	64
     83 #define	TS_HASH_MASK	(TS_HASH_SIZE - 1)
     84 #define	TS_HASH(obj)	(((uintptr_t)(obj) >> 3) & TS_HASH_MASK)
     85 
     86 tschain_t	turnstile_tab[TS_HASH_SIZE];
     87 pool_cache_t	turnstile_cache;
     88 
     89 int	turnstile_ctor(void *, void *, int);
     90 
     91 extern turnstile_t turnstile0;
     92 
     93 /*
     94  * turnstile_init:
     95  *
     96  *	Initialize the turnstile mechanism.
     97  */
     98 void
     99 turnstile_init(void)
    100 {
    101 	tschain_t *tc;
    102 	int i;
    103 
    104 	for (i = 0; i < TS_HASH_SIZE; i++) {
    105 		tc = &turnstile_tab[i];
    106 		LIST_INIT(&tc->tc_chain);
    107 		mutex_init(&tc->tc_mutex, MUTEX_SPIN, IPL_SCHED);
    108 	}
    109 
    110 	turnstile_cache = pool_cache_init(sizeof(turnstile_t), 0, 0, 0,
    111 	    "tstilepl", NULL, IPL_NONE, turnstile_ctor, NULL, NULL);
    112 	KASSERT(turnstile_cache != NULL);
    113 
    114 	(void)turnstile_ctor(NULL, &turnstile0, 0);
    115 }
    116 
    117 /*
    118  * turnstile_ctor:
    119  *
    120  *	Constructor for turnstiles.
    121  */
    122 int
    123 turnstile_ctor(void *arg, void *obj, int flags)
    124 {
    125 	turnstile_t *ts = obj;
    126 
    127 	memset(ts, 0, sizeof(*ts));
    128 	sleepq_init(&ts->ts_sleepq[TS_READER_Q], NULL);
    129 	sleepq_init(&ts->ts_sleepq[TS_WRITER_Q], NULL);
    130 	return (0);
    131 }
    132 
    133 /*
    134  * turnstile_remove:
    135  *
    136  *	Remove an LWP from a turnstile sleep queue and wake it.
    137  */
    138 static inline void
    139 turnstile_remove(turnstile_t *ts, lwp_t *l, sleepq_t *sq)
    140 {
    141 	turnstile_t *nts;
    142 
    143 	KASSERT(l->l_ts == ts);
    144 
    145 	/*
    146 	 * This process is no longer using the active turnstile.
    147 	 * Find an inactive one on the free list to give to it.
    148 	 */
    149 	if ((nts = ts->ts_free) != NULL) {
    150 		KASSERT(TS_ALL_WAITERS(ts) > 1);
    151 		l->l_ts = nts;
    152 		ts->ts_free = nts->ts_free;
    153 		nts->ts_free = NULL;
    154 	} else {
    155 		/*
    156 		 * If the free list is empty, this is the last
    157 		 * waiter.
    158 		 */
    159 		KASSERT(TS_ALL_WAITERS(ts) == 1);
    160 		LIST_REMOVE(ts, ts_chain);
    161 	}
    162 
    163 	(void)sleepq_remove(sq, l);
    164 }
    165 
    166 /*
    167  * turnstile_lookup:
    168  *
    169  *	Look up the turnstile for the specified lock.  This acquires and
    170  *	holds the turnstile chain lock (sleep queue interlock).
    171  */
    172 turnstile_t *
    173 turnstile_lookup(wchan_t obj)
    174 {
    175 	turnstile_t *ts;
    176 	tschain_t *tc;
    177 
    178 	tc = &turnstile_tab[TS_HASH(obj)];
    179 	mutex_spin_enter(&tc->tc_mutex);
    180 
    181 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    182 		if (ts->ts_obj == obj)
    183 			return (ts);
    184 
    185 	/*
    186 	 * No turnstile yet for this lock.  No problem, turnstile_block()
    187 	 * handles this by fetching the turnstile from the blocking thread.
    188 	 */
    189 	return (NULL);
    190 }
    191 
    192 /*
    193  * turnstile_exit:
    194  *
    195  *	Abort a turnstile operation.
    196  */
    197 void
    198 turnstile_exit(wchan_t obj)
    199 {
    200 	tschain_t *tc;
    201 
    202 	tc = &turnstile_tab[TS_HASH(obj)];
    203 	mutex_spin_exit(&tc->tc_mutex);
    204 }
    205 
    206 /*
    207  * turnstile_block:
    208  *
    209  *	 Enter an object into the turnstile chain and prepare the current
    210  *	 LWP for sleep.
    211  */
    212 void
    213 turnstile_block(turnstile_t *ts, int q, wchan_t obj, syncobj_t *sobj)
    214 {
    215 	lwp_t *l;
    216 	lwp_t *cur; /* cached curlwp */
    217 	lwp_t *owner;
    218 	turnstile_t *ots;
    219 	tschain_t *tc;
    220 	sleepq_t *sq;
    221 	pri_t prio;
    222 
    223 	tc = &turnstile_tab[TS_HASH(obj)];
    224 	l = cur = curlwp;
    225 
    226 	KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
    227 	KASSERT(mutex_owned(&tc->tc_mutex));
    228 	KASSERT(l != NULL && l->l_ts != NULL);
    229 
    230 	if (ts == NULL) {
    231 		/*
    232 		 * We are the first thread to wait for this object;
    233 		 * lend our turnstile to it.
    234 		 */
    235 		ts = l->l_ts;
    236 		KASSERT(TS_ALL_WAITERS(ts) == 0);
    237 		KASSERT(TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) &&
    238 			TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
    239 		ts->ts_obj = obj;
    240 		ts->ts_inheritor = NULL;
    241 		ts->ts_sleepq[TS_READER_Q].sq_mutex = &tc->tc_mutex;
    242 		ts->ts_sleepq[TS_WRITER_Q].sq_mutex = &tc->tc_mutex;
    243 		LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
    244 	} else {
    245 		/*
    246 		 * Object already has a turnstile.  Put our turnstile
    247 		 * onto the free list, and reference the existing
    248 		 * turnstile instead.
    249 		 */
    250 		ots = l->l_ts;
    251 		ots->ts_free = ts->ts_free;
    252 		ts->ts_free = ots;
    253 		l->l_ts = ts;
    254 
    255 		KASSERT(ts->ts_obj == obj);
    256 		KASSERT(TS_ALL_WAITERS(ts) != 0);
    257 		KASSERT(!TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) ||
    258 			!TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
    259 	}
    260 
    261 	sq = &ts->ts_sleepq[q];
    262 	sleepq_enter(sq, l);
    263 	LOCKDEBUG_BARRIER(&tc->tc_mutex, 1);
    264 	l->l_priority = sched_kpri(l);
    265 	prio = lwp_eprio(l);
    266 	sleepq_enqueue(sq, prio, obj, "tstile", sobj);
    267 
    268 	/*
    269 	 * lend our priority to lwps on the blocking chain.
    270 	 */
    271 
    272 	for (;;) {
    273 		bool dolock;
    274 
    275 		if (l->l_wchan == NULL)
    276 			break;
    277 
    278 		owner = (*l->l_syncobj->sobj_owner)(l->l_wchan);
    279 		if (owner == NULL)
    280 			break;
    281 
    282 		KASSERT(l != owner);
    283 		KASSERT(cur != owner);
    284 
    285 		if (l->l_mutex != owner->l_mutex)
    286 			dolock = true;
    287 		else
    288 			dolock = false;
    289 		if (dolock && !lwp_trylock(owner)) {
    290 			/*
    291 			 * restart from curlwp.
    292 			 */
    293 			lwp_unlock(l);
    294 			l = cur;
    295 			lwp_lock(l);
    296 			prio = lwp_eprio(l);
    297 			continue;
    298 		}
    299 		if (prio <= lwp_eprio(owner)) {
    300 			if (dolock)
    301 				lwp_unlock(owner);
    302 			break;
    303 		}
    304 		ts = l->l_ts;
    305 		KASSERT(ts->ts_inheritor == owner || ts->ts_inheritor == NULL);
    306 		if (ts->ts_inheritor == NULL) {
    307 			ts->ts_inheritor = owner;
    308 			ts->ts_eprio = prio;
    309 			SLIST_INSERT_HEAD(&owner->l_pi_lenders, ts, ts_pichain);
    310 			lwp_lendpri(owner, prio);
    311 		} else if (prio > ts->ts_eprio) {
    312 			ts->ts_eprio = prio;
    313 			lwp_lendpri(owner, prio);
    314 		}
    315 		if (dolock)
    316 			lwp_unlock(l);
    317 		l = owner;
    318 	}
    319 	LOCKDEBUG_BARRIER(l->l_mutex, 1);
    320 	if (cur->l_mutex != l->l_mutex) {
    321 		lwp_unlock(l);
    322 		lwp_lock(cur);
    323 	}
    324 	LOCKDEBUG_BARRIER(cur->l_mutex, 1);
    325 
    326 	sleepq_block(0, false);
    327 }
    328 
    329 /*
    330  * turnstile_wakeup:
    331  *
    332  *	Wake up the specified number of threads that are blocked
    333  *	in a turnstile.
    334  */
    335 void
    336 turnstile_wakeup(turnstile_t *ts, int q, int count, lwp_t *nl)
    337 {
    338 	sleepq_t *sq;
    339 	tschain_t *tc;
    340 	lwp_t *l;
    341 
    342 	tc = &turnstile_tab[TS_HASH(ts->ts_obj)];
    343 	sq = &ts->ts_sleepq[q];
    344 
    345 	KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
    346 	KASSERT(count > 0 && count <= TS_WAITERS(ts, q));
    347 	KASSERT(mutex_owned(&tc->tc_mutex) && sq->sq_mutex == &tc->tc_mutex);
    348 	KASSERT(ts->ts_inheritor == curlwp || ts->ts_inheritor == NULL);
    349 
    350 	/*
    351 	 * restore inherited priority if necessary.
    352 	 */
    353 
    354 	if (ts->ts_inheritor != NULL) {
    355 		turnstile_t *iter;
    356 		turnstile_t *next;
    357 		turnstile_t *prev = NULL;
    358 		pri_t prio;
    359 		bool dolock;
    360 
    361 		ts->ts_inheritor = NULL;
    362 		l = curlwp;
    363 
    364 		dolock = l->l_mutex == &l->l_cpu->ci_schedstate.spc_lwplock;
    365 		if (dolock) {
    366 			lwp_lock(l);
    367 		}
    368 
    369 		/*
    370 		 * the following loop does two things.
    371 		 *
    372 		 * - remove ts from the list.
    373 		 *
    374 		 * - from the rest of the list, find the highest priority.
    375 		 */
    376 
    377 		prio = -1;
    378 		KASSERT(!SLIST_EMPTY(&l->l_pi_lenders));
    379 		for (iter = SLIST_FIRST(&l->l_pi_lenders);
    380 		    iter != NULL; iter = next) {
    381 			KASSERT(lwp_eprio(l) >= ts->ts_eprio);
    382 			next = SLIST_NEXT(iter, ts_pichain);
    383 			if (iter == ts) {
    384 				if (prev == NULL) {
    385 					SLIST_REMOVE_HEAD(&l->l_pi_lenders,
    386 					    ts_pichain);
    387 				} else {
    388 					SLIST_REMOVE_AFTER(prev, ts_pichain);
    389 				}
    390 			} else if (prio < iter->ts_eprio) {
    391 				prio = iter->ts_eprio;
    392 			}
    393 			prev = iter;
    394 		}
    395 
    396 		lwp_lendpri(l, prio);
    397 
    398 		if (dolock) {
    399 			lwp_unlock(l);
    400 		}
    401 	}
    402 
    403 	if (nl != NULL) {
    404 #if defined(DEBUG) || defined(LOCKDEBUG)
    405 		TAILQ_FOREACH(l, &sq->sq_queue, l_sleepchain) {
    406 			if (l == nl)
    407 				break;
    408 		}
    409 		if (l == NULL)
    410 			panic("turnstile_wakeup: nl not on sleepq");
    411 #endif
    412 		turnstile_remove(ts, nl, sq);
    413 	} else {
    414 		while (count-- > 0) {
    415 			l = TAILQ_FIRST(&sq->sq_queue);
    416 			KASSERT(l != NULL);
    417 			turnstile_remove(ts, l, sq);
    418 		}
    419 	}
    420 	mutex_spin_exit(&tc->tc_mutex);
    421 }
    422 
    423 /*
    424  * turnstile_unsleep:
    425  *
    426  *	Remove an LWP from the turnstile.  This is called when the LWP has
    427  *	not been awoken normally but instead interrupted: for example, if it
    428  *	has received a signal.  It's not a valid action for turnstiles,
    429  *	since LWPs blocking on a turnstile are not interruptable.
    430  */
    431 void
    432 turnstile_unsleep(lwp_t *l)
    433 {
    434 
    435 	lwp_unlock(l);
    436 	panic("turnstile_unsleep");
    437 }
    438 
    439 /*
    440  * turnstile_changepri:
    441  *
    442  *	Adjust the priority of an LWP residing on a turnstile.
    443  */
    444 void
    445 turnstile_changepri(lwp_t *l, pri_t pri)
    446 {
    447 
    448 	/* XXX priority inheritance */
    449 	sleepq_changepri(l, pri);
    450 }
    451 
    452 #if defined(LOCKDEBUG)
    453 /*
    454  * turnstile_print:
    455  *
    456  *	Given the address of a lock object, print the contents of a
    457  *	turnstile.
    458  */
    459 void
    460 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
    461 {
    462 	turnstile_t *ts;
    463 	tschain_t *tc;
    464 	sleepq_t *rsq, *wsq;
    465 	lwp_t *l;
    466 
    467 	tc = &turnstile_tab[TS_HASH(obj)];
    468 
    469 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    470 		if (ts->ts_obj == obj)
    471 			break;
    472 
    473 	(*pr)("Turnstile chain at %p.\n", tc);
    474 	if (ts == NULL) {
    475 		(*pr)("=> No active turnstile for this lock.\n");
    476 		return;
    477 	}
    478 
    479 	rsq = &ts->ts_sleepq[TS_READER_Q];
    480 	wsq = &ts->ts_sleepq[TS_WRITER_Q];
    481 
    482 	(*pr)("=> Turnstile at %p (wrq=%p, rdq=%p).\n", ts, rsq, wsq);
    483 
    484 	(*pr)("=> %d waiting readers:", rsq->sq_waiters);
    485 	TAILQ_FOREACH(l, &rsq->sq_queue, l_sleepchain) {
    486 		(*pr)(" %p", l);
    487 	}
    488 	(*pr)("\n");
    489 
    490 	(*pr)("=> %d waiting writers:", wsq->sq_waiters);
    491 	TAILQ_FOREACH(l, &wsq->sq_queue, l_sleepchain) {
    492 		(*pr)(" %p", l);
    493 	}
    494 	(*pr)("\n");
    495 }
    496 #endif	/* LOCKDEBUG */
    497