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