Home | History | Annotate | Line # | Download | only in kern
kern_turnstile.c revision 1.1.2.6
      1 /*	$NetBSD: kern_turnstile.c,v 1.1.2.6 2002/03/16 03:46:38 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 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.
      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  * Turnsiles are specialized sleep queues for use by locks.  Turnstiles
     41  * are described in detail in:
     42  *
     43  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  *	    Richard McDougall.
     45  *
     46  * Turnstiles are kept in a hash table.  There are likely to be many more
     47  * lock objects than there are threads.  Since a thread can block on only
     48  * one lock at a time, we only need one turnstile per thread, and so they
     49  * are allocated at thread creation time.
     50  *
     51  * When a thread decides it needs to block on a lock, it looks up the
     52  * active turnstile for that lock.  If no active turnstile exists, then
     53  * the process lends its turnstile to the lock.  If there is already
     54  * an active turnstile for the lock, the thread places its turnstile on
     55  * a list of free turnstiles, and references the active one instead.
     56  *
     57  * The act of looking up the turnstile acquires an interlock on the sleep
     58  * queue.  If a thread decides it doesn't need to block after all, then
     59  * this interlock must be released by explicitly aborting the turnstile
     60  * operation.
     61  *
     62  * When a thread is awakened, it needs to get its turnstile back.  If
     63  * there are still other threads waiting in the active turnstile, the
     64  * the thread grabs a free turnstile off the free list.  Otherwise, it
     65  * can take back the active turnstile from the lock (thus deactivating
     66  * the turnstile).
     67  *
     68  * Turnstiles are the place to do priority inheritence.  However, we do
     69  * not currently implement that.
     70  *
     71  * We also do not differentiate between the reader and writer queues,
     72  * although we currently provide for it in the API so that we can add
     73  * support for it later.
     74  *
     75  * XXX We currently have to interlock with the sched_lock.  The locking
     76  * order is:
     77  *
     78  *	turnstile chain -> sched_lock
     79  */
     80 
     81 #include <sys/cdefs.h>
     82 __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.2.6 2002/03/16 03:46:38 thorpej Exp $");
     83 
     84 #include <sys/param.h>
     85 #include <sys/simplelock.h>
     86 #include <sys/pool.h>
     87 #include <sys/proc.h>
     88 #include <sys/resourcevar.h>
     89 #include <sys/sched.h>
     90 #include <sys/systm.h>
     91 
     92 /*
     93  * Turnstile hash -- shift the lock object to eliminate the zero bits
     94  * of the address, and mask it off with the turnstile table's size.
     95  */
     96 #if LONG_BIT == 64
     97 #define	TURNSTILE_HASH_SHIFT	3
     98 #elif LONG_BIT == 32
     99 #define	TURNSTILE_HASH_SHIFT	2
    100 #else
    101 #error "Don't know how big your pointers are."
    102 #endif
    103 
    104 #define	TURNSTILE_HASH_SIZE	64	/* XXXJRT tune */
    105 #define	TURNSTILE_HASH_MASK	(TURNSTILE_HASH_SIZE - 1)
    106 
    107 #define	TURNSTILE_HASH(obj)						\
    108 	((((u_long)(obj)) >> TURNSTILE_HASH_SHIFT) & TURNSTILE_HASH_MASK)
    109 
    110 struct turnstile_chain {
    111 	__cpu_simple_lock_t tc_lock;	/* lock on hash chain */
    112 	int		    tc_oldspl;	/* saved spl of lock holder
    113 					   (only valid while tc_lock held) */
    114 	LIST_HEAD(, turnstile) tc_chain;/* turnstile chain */
    115 } turnstile_table[TURNSTILE_HASH_SIZE];
    116 
    117 #define	TURNSTILE_CHAIN(obj)						\
    118 	&turnstile_table[TURNSTILE_HASH(obj)]
    119 
    120 #define	TURNSTILE_CHAIN_LOCK(tc)					\
    121 do {									\
    122 	int _s_ = splsched();						\
    123 	__cpu_simple_lock(&(tc)->tc_lock);				\
    124 	(tc)->tc_oldspl = _s_;						\
    125 } while (/*CONSTCOND*/0)
    126 
    127 #define	TURNSTILE_CHAIN_UNLOCK(tc)					\
    128 do {									\
    129 	int _s_ = (tc)->tc_oldspl;					\
    130 	__cpu_simple_unlock(&(tc)->tc_lock);				\
    131 	splx(_s_);							\
    132 } while (/*CONSTCOND*/0)
    133 
    134 static const char turnstile_wmesg[] = "tstile";
    135 
    136 struct pool turnstile_pool;
    137 struct pool_cache turnstile_cache;
    138 
    139 int	turnstile_ctor(void *, void *, int);
    140 
    141 /*
    142  * turnstile_init:
    143  *
    144  *	Initialize the turnstile mechanism.
    145  */
    146 void
    147 turnstile_init(void)
    148 {
    149 	struct turnstile_chain *tc;
    150 	int i;
    151 
    152 	for (i = 0; i < TURNSTILE_HASH_SIZE; i++) {
    153 		tc = &turnstile_table[i];
    154 		__cpu_simple_lock_init(&tc->tc_lock);
    155 		LIST_INIT(&tc->tc_chain);
    156 	}
    157 
    158 	pool_init(&turnstile_pool, sizeof(struct turnstile), 0, 0, 0,
    159 	    "tspool", &pool_allocator_nointr);
    160 	pool_cache_init(&turnstile_cache, &turnstile_pool,
    161 	    turnstile_ctor, NULL, NULL);
    162 }
    163 
    164 /*
    165  * turnstile_ctor:
    166  *
    167  *	Constructor for turnstiles.
    168  */
    169 int
    170 turnstile_ctor(void *arg, void *obj, int flags)
    171 {
    172 	struct turnstile *ts = obj;
    173 
    174 	memset(ts, 0, sizeof(*ts));
    175 	return (0);
    176 }
    177 
    178 static void
    179 turnstile_remque(struct turnstile *ts, struct proc *p,
    180     struct turnstile_sleepq *tsq)
    181 {
    182 	struct proc **q = &tsq->tsq_q.sq_head;
    183 	struct turnstile *nts;
    184 
    185 	KASSERT(p->p_ts == ts);
    186 
    187 	/*
    188 	 * This process is no longer using the active turnstile.
    189 	 * Find an inactive one on the free list to give to it.
    190 	 */
    191 	if ((nts = ts->ts_free) != NULL) {
    192 		KASSERT(TS_WAITERS(ts) > 1);
    193 		p->p_ts = nts;
    194 		ts->ts_free = nts->ts_free;
    195 		nts->ts_free = NULL;
    196 	} else {
    197 		/*
    198 		 * If the free list is empty, this is the last
    199 		 * waiter.
    200 		 */
    201 		KASSERT(TS_WAITERS(ts) == 1);
    202 		LIST_REMOVE(ts, ts_chain);
    203 	}
    204 
    205 	tsq->tsq_waiters--;
    206 
    207 	*q = p->p_forw;
    208 	if (tsq->tsq_q.sq_tailp == &p->p_forw)
    209 		tsq->tsq_q.sq_tailp = q;
    210 
    211 	KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_waiters != 0 ||
    212 		ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head == NULL);
    213 	KASSERT(ts->ts_sleepq[TS_WRITER_Q].tsq_waiters != 0 ||
    214 		ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head == NULL);
    215 
    216 	KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_waiters == 0 ||
    217 		ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head != NULL);
    218 	KASSERT(ts->ts_sleepq[TS_WRITER_Q].tsq_waiters == 0 ||
    219 		ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head != NULL);
    220 }
    221 
    222 /*
    223  * turnstile_lookup:
    224  *
    225  *	Look up the turnstile for the specified lock object.  This
    226  *	acquires and holds the turnstile chain lock (sleep queue
    227  *	interlock).
    228  */
    229 struct turnstile *
    230 turnstile_lookup(void *lp)
    231 {
    232 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    233 	struct turnstile *ts;
    234 
    235 	TURNSTILE_CHAIN_LOCK(tc);
    236 
    237 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    238 		if (ts->ts_obj == lp)
    239 			return (ts);
    240 
    241 	/*
    242 	 * No turnstile yet for this lock.  No problem, turnstile_block()
    243 	 * handle this by fetching the turnstile from the blocking thread.
    244 	 */
    245 	return (NULL);
    246 }
    247 
    248 /*
    249  * turnstile_exit:
    250  *
    251  *	Abort a turnstile operation.
    252  */
    253 void
    254 turnstile_exit(void *lp)
    255 {
    256 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    257 
    258 	TURNSTILE_CHAIN_UNLOCK(tc);
    259 }
    260 
    261 /*
    262  * turnstile_block:
    263  *
    264  *	Block a thread on a lock object.
    265  */
    266 int
    267 turnstile_block(struct turnstile *ts, int rw, int pri, void *lp)
    268 {
    269 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    270 	struct proc *p = curproc;
    271 	struct turnstile *ots;
    272 	struct turnstile_sleepq *tsq;
    273 	struct slpque *qp;
    274 	int s;
    275 
    276 	KASSERT(p->p_ts != NULL);
    277 	KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
    278 
    279 	if (ts == NULL) {
    280 		/*
    281 		 * We are the first thread to wait for this lock;
    282 		 * lend our turnstile to it.
    283 		 */
    284 		ts = p->p_ts;
    285 		KASSERT(TS_WAITERS(ts) == 0);
    286 		KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head == NULL &&
    287 			ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head == NULL);
    288 		ts->ts_obj = lp;
    289 		LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
    290 	} else {
    291 		/*
    292 		 * Lock already has a turnstile.  Put our turnstile
    293 		 * onto the free list, and reference the existing
    294 		 * turnstile instead.
    295 		 */
    296 		ots = p->p_ts;
    297 		ots->ts_free = ts->ts_free;
    298 		ts->ts_free = ots;
    299 		p->p_ts = ts;
    300 	}
    301 
    302 #ifdef DIAGNOSTIC
    303 	if (p->p_stat != SONPROC)
    304 		panic("turnstile_block: p_stat %d != SONPROC", p->p_stat);
    305 	if (p->p_back != NULL)
    306 		panic("turnstile_block: p_back != NULL");
    307 #endif
    308 
    309 #ifdef KTRACE
    310 	if (KTRPOINT(p, KTR_CSW))
    311 		ktrcsw(p, 1, 0);
    312 #endif
    313 
    314 	/* XXXJRT PCATCH? */
    315 
    316 	p->p_wchan = lp;
    317 	p->p_wmesg = turnstile_wmesg;
    318 	p->p_slptime = 0;
    319 	p->p_priority = pri & PRIMASK;
    320 
    321 	tsq = &ts->ts_sleepq[rw];
    322 	qp = &tsq->tsq_q;
    323 
    324 	tsq->tsq_waiters++;
    325 
    326 	if (qp->sq_head == NULL)
    327 		qp->sq_head = p;
    328 	else
    329 		*qp->sq_tailp = p;
    330 	*(qp->sq_tailp = &p->p_forw) = NULL;
    331 
    332 	p->p_stat = SSLEEP;
    333 	p->p_stats->p_ru.ru_nvcsw++;
    334 
    335 	/*
    336 	 * XXX We currently need to interlock with sched_lock.
    337 	 * Note we're already at splsched().
    338 	 */
    339 	_SCHED_LOCK;
    340 
    341 	/*
    342 	 * We can now release the turnstile chain interlock; the
    343 	 * scheduler lock is held, so a thread can't get in to
    344 	 * do a turnstile_wakeup() before we do the switch.
    345 	 *
    346 	 * Note: we need to remember our old spl which is currently
    347 	 * stored in the turnstile chain, because we have to stay
    348 	 * st splsched while the sched_lock is held.
    349 	 */
    350 	s = tc->tc_oldspl;
    351 	__cpu_simple_unlock(&tc->tc_lock);
    352 
    353 	mi_switch(p);
    354 
    355 	SCHED_ASSERT_UNLOCKED();
    356 	splx(s);
    357 
    358 	/*
    359 	 * We are now back to the base spl level we were at when the
    360 	 * caller called turnstile_lookup().
    361 	 */
    362 
    363 	KDASSERT(p->p_cpu != NULL);
    364 	KDASSERT(p->p_cpu == curcpu());
    365 	p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
    366 
    367 	KDASSERT((p->p_flag & (P_SINTR|P_TIMEOUT)) == 0);
    368 
    369 #ifdef KTRACE
    370 	if (KTRPOINT(p, KTR_CSW))
    371 		ktrcsw(p, 0, 0);
    372 #endif
    373 
    374 	return (0);
    375 }
    376 
    377 /*
    378  * turnstile_wakeup:
    379  *
    380  *	Wake up the specified number of threads that are blocked
    381  *	in a turnstile.
    382  */
    383 void
    384 turnstile_wakeup(struct turnstile *ts, int rw, int count)
    385 {
    386 	struct turnstile_chain *tc = TURNSTILE_CHAIN(ts->ts_obj);
    387 	struct turnstile_sleepq *tsq;
    388 	struct proc *p;
    389 
    390 	KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
    391 
    392 	tsq = &ts->ts_sleepq[rw];
    393 
    394 	/* XXX We currently interlock with sched_lock. */
    395 	_SCHED_LOCK;
    396 
    397 	while (count-- > 0) {
    398 		p = tsq->tsq_q.sq_head;
    399 
    400 		KASSERT(p != NULL);
    401 
    402 		turnstile_remque(ts, p, tsq);
    403 
    404 		p->p_wchan = NULL;
    405 
    406 		if (p->p_stat == SSLEEP)
    407 			awaken(p);
    408 	}
    409 
    410 	_SCHED_UNLOCK;
    411 
    412 	TURNSTILE_CHAIN_UNLOCK(tc);
    413 }
    414