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