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