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