kern_turnstile.c revision 1.1.2.2 1 /* $NetBSD: kern_turnstile.c,v 1.1.2.2 2002/03/10 20:05:31 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. Since there are likely to
47 * be many more lock objects than there are threads. Since a thread
48 * can block on only one lock at a time, we only need one turnstile
49 * per thread, and so they 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.2 2002/03/10 20:05:31 thorpej Exp $");
83
84 #include <sys/param.h>
85 #include <sys/lock.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 LIST_HEAD(, turnstile) tc_chain;/* turnstile chain */
114 } turnstile_table[TURNSTILE_HASH_SIZE];
115
116 #define TURNSTILE_CHAIN(obj) \
117 &turnstile_table[TURNSTILE_HASH(obj)]
118
119 #define TURNSTILE_CHAIN_LOCK(tc) \
120 do { \
121 int _s_ = splsched(); \
122 __cpu_simple_lock(&(tc)->tc_lock); \
123 (tc)->tc_oldspl = _s_; \
124 } while (/*CONSTCOND*/0)
125
126 #define TURNSTILE_CHAIN_UNLOCK(tc) \
127 do { \
128 int _s_ = (tc)->tc_oldspl; \
129 __cpu_simple_unlock(&(tc)->tc_lock); \
130 splx(_s_); \
131 } while (/*CONSTCOND*/0)
132
133 static const char turnstile_wmesg[] = "tstile";
134
135 struct pool turnstile_pool;
136
137 /*
138 * turnstile_init:
139 *
140 * Initialize the turnstile mechanism.
141 */
142 void
143 turnstile_init(void)
144 {
145 struct turnstile_chain *tc;
146 int i;
147
148 for (i = 0; i < TURNSTILE_HASH_SIZE; i++) {
149 tc = &turnstile_table[i];
150 __cpu_simple_lock_init(&tc->tc_lock);
151 LIST_INIT(&tc->tc_chain);
152 }
153
154 pool_init(&turnstile_pool, sizeof(struct turnstile), 0, 0, 0,
155 "tspool", &pool_allocator_nointr);
156 }
157
158 static void
159 turnstile_remque(struct turnstile *ts, struct proc *p, struct slpque *qp)
160 {
161 struct proc **q = &qp->sq_head;
162 struct turnstile *nts;
163
164 KASSERT(p->p_ts == ts);
165
166 /*
167 * This process is no longer using the active turnstile.
168 * Find an inactive one on the free list to give to it.
169 */
170 if ((nts == ts->ts_free) != NULL) {
171 KASSERT(ts->ts_waiters > 1);
172 p->p_ts = nts;
173 ts->ts_free = nts->ts_free;
174 nts->ts_free = NULL;
175 } else {
176 /*
177 * If the free list is empty, this is the last
178 * waiter.
179 */
180 KASSERT(ts->ts_waiters == 1);
181 LIST_REMOVE(ts, ts_chain);
182 }
183
184 ts->ts_waiters--;
185
186 *q = p->p_forw;
187 if (qp->sq_tailp == &p->p_forw)
188 qp->sq_tailp = q;
189 }
190
191 /*
192 * turnstile_lookup:
193 *
194 * Look up the turnstile for the specified lock object. This
195 * acquires and holds the turnstile chain lock (sleep queue
196 * interlock).
197 */
198 struct turnstile *
199 turnstile_lookup(void *lp)
200 {
201 struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
202 struct turnstile *ts;
203
204 TURNSTILE_CHAIN_LOCK(tc);
205
206 LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
207 if (ts->ts_obj == lp)
208 return (ts);
209
210 /*
211 * No turnstile yet for this lock. No problem, turnstile_block()
212 * handle this by fetching the turnstile from the blocking thread.
213 */
214 return (NULL);
215 }
216
217 /*
218 * turnstile_exit:
219 *
220 * Abort a turnstile operation.
221 */
222 void
223 turnstile_exit(void *lp)
224 {
225 struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
226
227 TURNSTILE_CHAIN_UNLOCK(tc);
228 }
229
230 /*
231 * turnstile_block:
232 *
233 * Block a thread on a lock object.
234 */
235 int
236 turnstile_block(struct turnstile *ts, int rw, void *lp)
237 {
238 struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
239 struct proc *p = curproc;
240 struct turnstile *ots;
241 struct slpque *qp;
242 int s;
243
244 KASSERT(p->p_ts != NULL);
245
246 if (ts == NULL) {
247 /*
248 * We are the first thread to wait for this lock;
249 * lend our turnstile to it.
250 */
251 ts = p->p_ts;
252 KASSERT(ts->ts_waiters == 0);
253 KASSERT(ts->ts_sleepq.sq_head == NULL);
254 ts->ts_obj = lp;
255 LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
256 } else {
257 /*
258 * Lock already has a turnstile. Put our turnstile
259 * onto the free list, and reference the existing
260 * turnstile instead.
261 */
262 ots = p->p_ts;
263 ots->ts_free = ts->ts_free;
264 ts->ts_free = ots;
265 p->p_ts = ts;
266 }
267
268 #ifdef DIAGNOSTIC
269 if (p->p_stat != SONPROC)
270 panic("turnstile_block: p_stat %d != SONPROC", p->p_stat);
271 if (p->p_back != NULL)
272 panic("turnstile_block: p_back != NULL");
273 #endif
274
275 #ifdef KTRACE
276 if (KTRPOINT(p, KTR_CSW))
277 ktrcsw(p, 1, 0);
278 #endif
279
280 /* XXXJRT PCATCH? */
281
282 p->p_wchan = lp;
283 p->p_wmesg = turnstile_wmesg;
284 p->p_slptime = 0;
285 /* p->p_priority = XXXJRT */
286
287 ts->ts_waiters++;
288
289 qp = &ts->ts_sleepq;
290 if (qp->sq_head == NULL)
291 qp->sq_head = p;
292 else
293 *qp->sq_tailp = p;
294 *(qp->sq_tailp = &p->p_forw) = NULL;
295
296 p->p_stat = SSLEEP;
297 p->p_stats->p_ru.ru_nvcsw++;
298
299 /*
300 * XXX We currently need to interlock with sched_lock.
301 * Note we're already at splsched().
302 */
303 _SCHED_LOCK;
304
305 /*
306 * We can now release the turnstile chain interlock; the
307 * scheduler lock is held, so a thread can't get in to
308 * do a turnstile_wakeup() before we do the switch.
309 *
310 * Note: we need to remember our old spl which is currently
311 * stored in the turnstile chain, because we have to stay
312 * st splsched while the sched_lock is held.
313 */
314 s = tc->tc_oldspl;
315 __cpu_simple_unlock(&tc->tc_lock);
316
317 mi_switch(p);
318
319 SCHED_ASSERT_UNLOCKED();
320 splx(s);
321
322 /*
323 * We are now back to the base spl level we were at when the
324 * caller called turnstile_lookup().
325 */
326
327 KDASSERT(p->p_cpu != NULL);
328 KDASSERT(p->p_cpu == curcpu());
329 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
330
331 KDASSERT((p->p_flag & (P_SINTR|P_TIMEOUT)) == 0);
332
333 #ifdef KTRACE
334 if (KTRPOINT(p, KTR_CSW))
335 ktrcsw(p, 0, 0);
336 #endif
337
338 return (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(struct turnstile *ts, int rw, int count)
349 {
350 struct turnstile_chain *tc = TURNSTILE_CHAIN(ts->ts_obj);
351 struct slpque *qp = &ts->ts_sleepq;
352 struct proc *p;
353
354 /* XXX We currently interlock with sched_lock. */
355 _SCHED_LOCK;
356
357 while (count-- > 0) {
358 p = qp->sq_head;
359
360 KASSERT(p != NULL);
361
362 turnstile_remque(ts, p, qp);
363
364 p->p_wchan = NULL;
365
366 if (p->p_stat == SSLEEP)
367 awaken(p);
368 }
369
370 _SCHED_UNLOCK;
371
372 TURNSTILE_CHAIN_UNLOCK(tc);
373 }
374