Home | History | Annotate | Line # | Download | only in kern
kern_turnstile.c revision 1.2
      1  1.2  ad /*	$NetBSD: kern_turnstile.c,v 1.2 2007/02/09 21:55:31 ad Exp $	*/
      2  1.2  ad 
      3  1.2  ad /*-
      4  1.2  ad  * Copyright (c) 2002, 2006, 2007 The NetBSD Foundation, Inc.
      5  1.2  ad  * All rights reserved.
      6  1.2  ad  *
      7  1.2  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  ad  * by Jason R. Thorpe and Andrew Doran.
      9  1.2  ad  *
     10  1.2  ad  * Redistribution and use in source and binary forms, with or without
     11  1.2  ad  * modification, are permitted provided that the following conditions
     12  1.2  ad  * are met:
     13  1.2  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.2  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  ad  *    documentation and/or other materials provided with the distribution.
     18  1.2  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.2  ad  *    must display the following acknowledgement:
     20  1.2  ad  *	This product includes software developed by the NetBSD
     21  1.2  ad  *	Foundation, Inc. and its contributors.
     22  1.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2  ad  *    contributors may be used to endorse or promote products derived
     24  1.2  ad  *    from this software without specific prior written permission.
     25  1.2  ad  *
     26  1.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2  ad  */
     38  1.2  ad 
     39  1.2  ad /*
     40  1.2  ad  * Turnstiles are described in detail in:
     41  1.2  ad  *
     42  1.2  ad  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     43  1.2  ad  *	    Richard McDougall.
     44  1.2  ad  *
     45  1.2  ad  * Turnstiles are kept in a hash table.  There are likely to be many more
     46  1.2  ad  * synchronisation objects than there are threads.  Since a thread can block
     47  1.2  ad  * on only one lock at a time, we only need one turnstile per thread, and
     48  1.2  ad  * so they are allocated at thread creation time.
     49  1.2  ad  *
     50  1.2  ad  * When a thread decides it needs to block on a lock, it looks up the
     51  1.2  ad  * active turnstile for that lock.  If no active turnstile exists, then
     52  1.2  ad  * the process lends its turnstile to the lock.  If there is already an
     53  1.2  ad  * active turnstile for the lock, the thread places its turnstile on a
     54  1.2  ad  * list of free turnstiles, and references the active one instead.
     55  1.2  ad  *
     56  1.2  ad  * The act of looking up the turnstile acquires an interlock on the sleep
     57  1.2  ad  * queue.  If a thread decides it doesn't need to block after all, then this
     58  1.2  ad  * interlock must be released by explicitly aborting the turnstile
     59  1.2  ad  * operation.
     60  1.2  ad  *
     61  1.2  ad  * When a thread is awakened, it needs to get its turnstile back.  If there
     62  1.2  ad  * are still other threads waiting in the active turnstile, the the thread
     63  1.2  ad  * grabs a free turnstile off the free list.  Otherwise, it can take back
     64  1.2  ad  * the active turnstile from the lock (thus deactivating the turnstile).
     65  1.2  ad  *
     66  1.2  ad  * Turnstiles are the place to do priority inheritence.  However, we do
     67  1.2  ad  * not currently implement that.
     68  1.2  ad  */
     69  1.2  ad 
     70  1.2  ad #include <sys/cdefs.h>
     71  1.2  ad __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.2 2007/02/09 21:55:31 ad Exp $");
     72  1.2  ad 
     73  1.2  ad #include "opt_lockdebug.h"
     74  1.2  ad #include "opt_multiprocessor.h"
     75  1.2  ad #include "opt_ktrace.h"
     76  1.2  ad #include "opt_ddb.h"
     77  1.2  ad 
     78  1.2  ad #include <sys/param.h>
     79  1.2  ad #include <sys/lock.h>
     80  1.2  ad #include <sys/pool.h>
     81  1.2  ad #include <sys/proc.h>
     82  1.2  ad #include <sys/sleepq.h>
     83  1.2  ad #include <sys/systm.h>
     84  1.2  ad 
     85  1.2  ad #define	TS_HASH_SIZE	64
     86  1.2  ad #define	TS_HASH_MASK	(TS_HASH_SIZE - 1)
     87  1.2  ad #define	TS_HASH(obj)	(((uintptr_t)(obj) >> 3) & TS_HASH_MASK)
     88  1.2  ad 
     89  1.2  ad tschain_t	turnstile_tab[TS_HASH_SIZE];
     90  1.2  ad 
     91  1.2  ad struct pool turnstile_pool;
     92  1.2  ad struct pool_cache turnstile_cache;
     93  1.2  ad 
     94  1.2  ad int	turnstile_ctor(void *, void *, int);
     95  1.2  ad void	turnstile_unsleep(struct lwp *);
     96  1.2  ad void	turnstile_changepri(struct lwp *, int);
     97  1.2  ad 
     98  1.2  ad extern turnstile_t turnstile0;
     99  1.2  ad 
    100  1.2  ad syncobj_t turnstile_syncobj = {
    101  1.2  ad 	SOBJ_SLEEPQ_FIFO,
    102  1.2  ad 	turnstile_unsleep,
    103  1.2  ad 	turnstile_changepri
    104  1.2  ad };
    105  1.2  ad 
    106  1.2  ad /*
    107  1.2  ad  * turnstile_init:
    108  1.2  ad  *
    109  1.2  ad  *	Initialize the turnstile mechanism.
    110  1.2  ad  */
    111  1.2  ad void
    112  1.2  ad turnstile_init(void)
    113  1.2  ad {
    114  1.2  ad 	tschain_t *tc;
    115  1.2  ad 	int i;
    116  1.2  ad 
    117  1.2  ad 	for (i = 0; i < TS_HASH_SIZE; i++) {
    118  1.2  ad 		tc = &turnstile_tab[i];
    119  1.2  ad 		LIST_INIT(&tc->tc_chain);
    120  1.2  ad #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    121  1.2  ad 		mutex_init(&tc->tc_mutexstore, MUTEX_SPIN, IPL_SCHED);
    122  1.2  ad 		tc->tc_mutex = &tc->tc_mutexstore;
    123  1.2  ad #else
    124  1.2  ad 		tc->tc_mutex = &sched_mutex;
    125  1.2  ad #endif
    126  1.2  ad 	}
    127  1.2  ad 
    128  1.2  ad 	pool_init(&turnstile_pool, sizeof(turnstile_t), 0, 0, 0,
    129  1.2  ad 	    "tstilepl", &pool_allocator_nointr);
    130  1.2  ad 	pool_cache_init(&turnstile_cache, &turnstile_pool,
    131  1.2  ad 	    turnstile_ctor, NULL, NULL);
    132  1.2  ad 
    133  1.2  ad 	(void)turnstile_ctor(NULL, &turnstile0, 0);
    134  1.2  ad }
    135  1.2  ad 
    136  1.2  ad /*
    137  1.2  ad  * turnstile_ctor:
    138  1.2  ad  *
    139  1.2  ad  *	Constructor for turnstiles.
    140  1.2  ad  */
    141  1.2  ad int
    142  1.2  ad turnstile_ctor(void *arg, void *obj, int flags)
    143  1.2  ad {
    144  1.2  ad 	turnstile_t *ts = obj;
    145  1.2  ad 
    146  1.2  ad 	memset(ts, 0, sizeof(*ts));
    147  1.2  ad 	sleepq_init(&ts->ts_sleepq[TS_READER_Q], NULL);
    148  1.2  ad 	sleepq_init(&ts->ts_sleepq[TS_WRITER_Q], NULL);
    149  1.2  ad 	return (0);
    150  1.2  ad }
    151  1.2  ad 
    152  1.2  ad /*
    153  1.2  ad  * turnstile_remove:
    154  1.2  ad  *
    155  1.2  ad  *	Remove an LWP from a turnstile sleep queue and wake it.
    156  1.2  ad  */
    157  1.2  ad static inline int
    158  1.2  ad turnstile_remove(turnstile_t *ts, struct lwp *l, sleepq_t *sq)
    159  1.2  ad {
    160  1.2  ad 	turnstile_t *nts;
    161  1.2  ad 
    162  1.2  ad 	KASSERT(l->l_ts == ts);
    163  1.2  ad 
    164  1.2  ad 	/*
    165  1.2  ad 	 * This process is no longer using the active turnstile.
    166  1.2  ad 	 * Find an inactive one on the free list to give to it.
    167  1.2  ad 	 */
    168  1.2  ad 	if ((nts = ts->ts_free) != NULL) {
    169  1.2  ad 		KASSERT(TS_ALL_WAITERS(ts) > 1);
    170  1.2  ad 		l->l_ts = nts;
    171  1.2  ad 		ts->ts_free = nts->ts_free;
    172  1.2  ad 		nts->ts_free = NULL;
    173  1.2  ad 	} else {
    174  1.2  ad 		/*
    175  1.2  ad 		 * If the free list is empty, this is the last
    176  1.2  ad 		 * waiter.
    177  1.2  ad 		 */
    178  1.2  ad 		KASSERT(TS_ALL_WAITERS(ts) == 1);
    179  1.2  ad 		LIST_REMOVE(ts, ts_chain);
    180  1.2  ad 	}
    181  1.2  ad 
    182  1.2  ad 	return sleepq_remove(sq, l);
    183  1.2  ad }
    184  1.2  ad 
    185  1.2  ad /*
    186  1.2  ad  * turnstile_lookup:
    187  1.2  ad  *
    188  1.2  ad  *	Look up the turnstile for the specified lock.  This acquires and
    189  1.2  ad  *	holds the turnstile chain lock (sleep queue interlock).
    190  1.2  ad  */
    191  1.2  ad turnstile_t *
    192  1.2  ad turnstile_lookup(wchan_t obj)
    193  1.2  ad {
    194  1.2  ad 	turnstile_t *ts;
    195  1.2  ad 	tschain_t *tc;
    196  1.2  ad 
    197  1.2  ad 	tc = &turnstile_tab[TS_HASH(obj)];
    198  1.2  ad 	mutex_spin_enter(tc->tc_mutex);
    199  1.2  ad 
    200  1.2  ad 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    201  1.2  ad 		if (ts->ts_obj == obj)
    202  1.2  ad 			return (ts);
    203  1.2  ad 
    204  1.2  ad 	/*
    205  1.2  ad 	 * No turnstile yet for this lock.  No problem, turnstile_block()
    206  1.2  ad 	 * handles this by fetching the turnstile from the blocking thread.
    207  1.2  ad 	 */
    208  1.2  ad 	return (NULL);
    209  1.2  ad }
    210  1.2  ad 
    211  1.2  ad /*
    212  1.2  ad  * turnstile_exit:
    213  1.2  ad  *
    214  1.2  ad  *	Abort a turnstile operation.
    215  1.2  ad  */
    216  1.2  ad void
    217  1.2  ad turnstile_exit(wchan_t obj)
    218  1.2  ad {
    219  1.2  ad 	tschain_t *tc;
    220  1.2  ad 
    221  1.2  ad 	tc = &turnstile_tab[TS_HASH(obj)];
    222  1.2  ad 	mutex_spin_exit(tc->tc_mutex);
    223  1.2  ad }
    224  1.2  ad 
    225  1.2  ad /*
    226  1.2  ad  * turnstile_block:
    227  1.2  ad  *
    228  1.2  ad  *	 Enter an object into the turnstile chain and prepare the current
    229  1.2  ad  *	 LWP for sleep.
    230  1.2  ad  */
    231  1.2  ad void
    232  1.2  ad turnstile_block(turnstile_t *ts, int q, wchan_t obj)
    233  1.2  ad {
    234  1.2  ad 	struct lwp *l;
    235  1.2  ad 	turnstile_t *ots;
    236  1.2  ad 	tschain_t *tc;
    237  1.2  ad 	sleepq_t *sq;
    238  1.2  ad 
    239  1.2  ad 	tc = &turnstile_tab[TS_HASH(obj)];
    240  1.2  ad 	l = curlwp;
    241  1.2  ad 
    242  1.2  ad 	KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
    243  1.2  ad 	KASSERT(mutex_owned(tc->tc_mutex));
    244  1.2  ad 	KASSERT(l != NULL && l->l_ts != NULL);
    245  1.2  ad 
    246  1.2  ad 	if (ts == NULL) {
    247  1.2  ad 		/*
    248  1.2  ad 		 * We are the first thread to wait for this object;
    249  1.2  ad 		 * lend our turnstile to it.
    250  1.2  ad 		 */
    251  1.2  ad 		ts = l->l_ts;
    252  1.2  ad 		KASSERT(TS_ALL_WAITERS(ts) == 0);
    253  1.2  ad 		KASSERT(TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) &&
    254  1.2  ad 			TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
    255  1.2  ad 		ts->ts_obj = obj;
    256  1.2  ad 		ts->ts_sleepq[TS_READER_Q].sq_mutex = tc->tc_mutex;
    257  1.2  ad 		ts->ts_sleepq[TS_WRITER_Q].sq_mutex = tc->tc_mutex;
    258  1.2  ad 		LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
    259  1.2  ad 	} else {
    260  1.2  ad 		/*
    261  1.2  ad 		 * Object already has a turnstile.  Put our turnstile
    262  1.2  ad 		 * onto the free list, and reference the existing
    263  1.2  ad 		 * turnstile instead.
    264  1.2  ad 		 */
    265  1.2  ad 		ots = l->l_ts;
    266  1.2  ad 		ots->ts_free = ts->ts_free;
    267  1.2  ad 		ts->ts_free = ots;
    268  1.2  ad 		l->l_ts = ts;
    269  1.2  ad 
    270  1.2  ad 		KASSERT(TS_ALL_WAITERS(ts) != 0);
    271  1.2  ad 		KASSERT(!TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) ||
    272  1.2  ad 			!TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
    273  1.2  ad 	}
    274  1.2  ad 
    275  1.2  ad 	sq = &ts->ts_sleepq[q];
    276  1.2  ad 	sleepq_enter(sq, l);
    277  1.2  ad 	sleepq_block(sq, sched_kpri(l), obj, "tstile", 0, 0,
    278  1.2  ad 	    &turnstile_syncobj);
    279  1.2  ad }
    280  1.2  ad 
    281  1.2  ad /*
    282  1.2  ad  * turnstile_wakeup:
    283  1.2  ad  *
    284  1.2  ad  *	Wake up the specified number of threads that are blocked
    285  1.2  ad  *	in a turnstile.
    286  1.2  ad  */
    287  1.2  ad void
    288  1.2  ad turnstile_wakeup(turnstile_t *ts, int q, int count, struct lwp *nl)
    289  1.2  ad {
    290  1.2  ad 	sleepq_t *sq;
    291  1.2  ad 	tschain_t *tc;
    292  1.2  ad 	struct lwp *l;
    293  1.2  ad 	int swapin;
    294  1.2  ad 
    295  1.2  ad 	tc = &turnstile_tab[TS_HASH(ts->ts_obj)];
    296  1.2  ad 	sq = &ts->ts_sleepq[q];
    297  1.2  ad 	swapin = 0;
    298  1.2  ad 
    299  1.2  ad 	KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
    300  1.2  ad 	KASSERT(count > 0 && count <= TS_WAITERS(ts, q));
    301  1.2  ad 	KASSERT(mutex_owned(tc->tc_mutex) && sq->sq_mutex == tc->tc_mutex);
    302  1.2  ad 
    303  1.2  ad 	if (nl != NULL) {
    304  1.2  ad #if defined(DEBUG) || defined(LOCKDEBUG)
    305  1.2  ad 		TAILQ_FOREACH(l, &sq->sq_queue, l_sleepchain) {
    306  1.2  ad 			if (l == nl)
    307  1.2  ad 				break;
    308  1.2  ad 		}
    309  1.2  ad 		if (l == NULL)
    310  1.2  ad 			panic("turnstile_wakeup: nl not on sleepq");
    311  1.2  ad #endif
    312  1.2  ad 		swapin |= turnstile_remove(ts, nl, sq);
    313  1.2  ad 	} else {
    314  1.2  ad 		while (count-- > 0) {
    315  1.2  ad 			l = TAILQ_FIRST(&sq->sq_queue);
    316  1.2  ad 			KASSERT(l != NULL);
    317  1.2  ad 			swapin |= turnstile_remove(ts, l, sq);
    318  1.2  ad 		}
    319  1.2  ad 	}
    320  1.2  ad 	mutex_spin_exit(tc->tc_mutex);
    321  1.2  ad 
    322  1.2  ad 	/*
    323  1.2  ad 	 * If there are newly awakend threads that need to be swapped in,
    324  1.2  ad 	 * then kick the swapper into action.
    325  1.2  ad 	 */
    326  1.2  ad 	if (swapin)
    327  1.2  ad 		wakeup(&proc0);
    328  1.2  ad }
    329  1.2  ad 
    330  1.2  ad /*
    331  1.2  ad  * turnstile_unsleep:
    332  1.2  ad  *
    333  1.2  ad  *	Remove an LWP from the turnstile.  This is called when the LWP has
    334  1.2  ad  *	not been awoken normally but instead interrupted: for example, if it
    335  1.2  ad  *	has received a signal.  It's not a valid action for turnstiles,
    336  1.2  ad  *	since LWPs blocking on a turnstile are not interruptable.
    337  1.2  ad  */
    338  1.2  ad void
    339  1.2  ad turnstile_unsleep(struct lwp *l)
    340  1.2  ad {
    341  1.2  ad 
    342  1.2  ad 	lwp_unlock(l);
    343  1.2  ad 	panic("turnstile_unsleep");
    344  1.2  ad }
    345  1.2  ad 
    346  1.2  ad /*
    347  1.2  ad  * turnstile_changepri:
    348  1.2  ad  *
    349  1.2  ad  *	Adjust the priority of an LWP residing on a turnstile.  Since we do
    350  1.2  ad  *	not yet do priority inheritance, we mostly ignore this action.
    351  1.2  ad  */
    352  1.2  ad void
    353  1.2  ad turnstile_changepri(struct lwp *l, int pri)
    354  1.2  ad {
    355  1.2  ad 
    356  1.2  ad 	/* LWPs on turnstiles always have kernel priority. */
    357  1.2  ad 	l->l_usrpri = pri;
    358  1.2  ad 	l->l_priority = sched_kpri(l);
    359  1.2  ad }
    360  1.2  ad 
    361  1.2  ad #if defined(LOCKDEBUG)
    362  1.2  ad /*
    363  1.2  ad  * turnstile_print:
    364  1.2  ad  *
    365  1.2  ad  *	Given the address of a lock object, print the contents of a
    366  1.2  ad  *	turnstile.
    367  1.2  ad  */
    368  1.2  ad void
    369  1.2  ad turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
    370  1.2  ad {
    371  1.2  ad 	turnstile_t *ts;
    372  1.2  ad 	tschain_t *tc;
    373  1.2  ad 	sleepq_t *rsq, *wsq;
    374  1.2  ad 	struct lwp *l;
    375  1.2  ad 
    376  1.2  ad 	tc = &turnstile_tab[TS_HASH(obj)];
    377  1.2  ad 
    378  1.2  ad 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    379  1.2  ad 		if (ts->ts_obj == obj)
    380  1.2  ad 			break;
    381  1.2  ad 
    382  1.2  ad 	(*pr)("Turnstile chain at %p with tc_mutex at %p.\n", tc, tc->tc_mutex);
    383  1.2  ad 	if (ts == NULL) {
    384  1.2  ad 		(*pr)("=> No active turnstile for this lock.\n");
    385  1.2  ad 		return;
    386  1.2  ad 	}
    387  1.2  ad 
    388  1.2  ad 	rsq = &ts->ts_sleepq[TS_READER_Q];
    389  1.2  ad 	wsq = &ts->ts_sleepq[TS_WRITER_Q];
    390  1.2  ad 
    391  1.2  ad 	(*pr)("=> Turnstile at %p (wrq=%p, rdq=%p).\n", ts, rsq, wsq);
    392  1.2  ad 
    393  1.2  ad 	(*pr)("=> %d waiting readers:", rsq->sq_waiters);
    394  1.2  ad 	TAILQ_FOREACH(l, &rsq->sq_queue, l_sleepchain) {
    395  1.2  ad 		(*pr)(" %p", l);
    396  1.2  ad 	}
    397  1.2  ad 	(*pr)("\n");
    398  1.2  ad 
    399  1.2  ad 	(*pr)("=> %d waiting writers:", wsq->sq_waiters);
    400  1.2  ad 	TAILQ_FOREACH(l, &wsq->sq_queue, l_sleepchain) {
    401  1.2  ad 		(*pr)(" %p", l);
    402  1.2  ad 	}
    403  1.2  ad 	(*pr)("\n");
    404  1.2  ad }
    405  1.2  ad #endif	/* LOCKDEBUG */
    406