kern_turnstile.c revision 1.1.36.6 1 /* $NetBSD: kern_turnstile.c,v 1.1.36.6 2007/01/27 14:00:02 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2006 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. However, we do
67 * not currently implement that.
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.36.6 2007/01/27 14:00:02 ad Exp $");
72
73 #include "opt_lockdebug.h"
74 #include "opt_multiprocessor.h"
75 #include "opt_ktrace.h"
76
77 #include <sys/param.h>
78 #include <sys/lock.h>
79 #include <sys/pool.h>
80 #include <sys/proc.h>
81 #include <sys/sleepq.h>
82 #include <sys/systm.h>
83
84 #define TS_HASH_SIZE 64
85 #define TS_HASH_MASK (TS_HASH_SIZE - 1)
86 #define TS_HASH(obj) (((uintptr_t)(obj) >> 3) & TS_HASH_MASK)
87
88 tschain_t turnstile_tab[TS_HASH_SIZE];
89
90 struct pool turnstile_pool;
91 struct pool_cache turnstile_cache;
92 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
93 kmutex_t turnstile_mutexes[TS_HASH_SIZE];
94 #endif
95
96 int turnstile_ctor(void *, void *, int);
97 void turnstile_unsleep(struct lwp *);
98 void turnstile_changepri(struct lwp *, int);
99
100 extern turnstile_t turnstile0;
101
102 syncobj_t turnstile_syncobj = {
103 SOBJ_SLEEPQ_FIFO,
104 turnstile_unsleep,
105 turnstile_changepri
106 };
107
108 /*
109 * turnstile_init:
110 *
111 * Initialize the turnstile mechanism.
112 */
113 void
114 turnstile_init(void)
115 {
116 tschain_t *tc;
117 int i;
118
119 for (i = 0; i < TS_HASH_SIZE; i++) {
120 tc = &turnstile_tab[i];
121 LIST_INIT(&tc->tc_chain);
122 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
123 mutex_init(&turnstile_mutexes[i], MUTEX_SPIN, IPL_SCHED);
124 tc->tc_mutex = &turnstile_mutexes[i];
125 #else
126 tc->tc_mutex = &sched_mutex;
127 #endif
128 }
129
130 pool_init(&turnstile_pool, sizeof(turnstile_t), 0, 0, 0,
131 "tstilepl", &pool_allocator_nointr);
132 pool_cache_init(&turnstile_cache, &turnstile_pool,
133 turnstile_ctor, NULL, NULL);
134
135 (void)turnstile_ctor(NULL, &turnstile0, 0);
136 }
137
138 /*
139 * turnstile_ctor:
140 *
141 * Constructor for turnstiles.
142 */
143 int
144 turnstile_ctor(void *arg, void *obj, int flags)
145 {
146 turnstile_t *ts = obj;
147
148 memset(ts, 0, sizeof(*ts));
149 sleepq_init(&ts->ts_sleepq[TS_READER_Q], NULL);
150 sleepq_init(&ts->ts_sleepq[TS_WRITER_Q], NULL);
151 return (0);
152 }
153
154 /*
155 * turnstile_remove:
156 *
157 * Remove an LWP from a turnstile sleep queue and wake it.
158 */
159 static inline int
160 turnstile_remove(turnstile_t *ts, struct lwp *l, sleepq_t *sq)
161 {
162 turnstile_t *nts;
163
164 KASSERT(l->l_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_ALL_WAITERS(ts) > 1);
172 l->l_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_ALL_WAITERS(ts) == 1);
181 LIST_REMOVE(ts, ts_chain);
182 }
183
184 return sleepq_remove(sq, l);
185 }
186
187 /*
188 * turnstile_lookup:
189 *
190 * Look up the turnstile for the specified lock. This acquires and
191 * holds the turnstile chain lock (sleep queue interlock).
192 */
193 turnstile_t *
194 turnstile_lookup(wchan_t obj)
195 {
196 turnstile_t *ts;
197 tschain_t *tc;
198
199 tc = &turnstile_tab[TS_HASH(obj)];
200
201 mutex_spin_enter(tc->tc_mutex);
202
203 LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
204 if (ts->ts_obj == obj)
205 return (ts);
206
207 /*
208 * No turnstile yet for this lock. No problem, turnstile_block()
209 * handles this by fetching the turnstile from the blocking thread.
210 */
211 return (NULL);
212 }
213
214 /*
215 * turnstile_exit:
216 *
217 * Abort a turnstile operation.
218 */
219 void
220 turnstile_exit(wchan_t obj)
221 {
222 tschain_t *tc;
223
224 tc = &turnstile_tab[TS_HASH(obj)];
225 mutex_spin_exit(tc->tc_mutex);
226 }
227
228 /*
229 * turnstile_block:
230 *
231 * Enter an object into the turnstile chain and prepare the current
232 * LWP for sleep.
233 */
234 void
235 turnstile_block(turnstile_t *ts, int q, int pri, wchan_t obj)
236 {
237 struct lwp *l;
238 turnstile_t *ots;
239 tschain_t *tc;
240 sleepq_t *sq;
241
242 KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
243
244 tc = &turnstile_tab[TS_HASH(obj)];
245 l = curlwp;
246
247 LOCK_ASSERT(mutex_owned(tc->tc_mutex));
248 KASSERT(l != NULL && l->l_ts != NULL);
249
250 if (ts == NULL) {
251 /*
252 * We are the first thread to wait for this object;
253 * lend our turnstile to it.
254 */
255 ts = l->l_ts;
256 KASSERT(TS_ALL_WAITERS(ts) == 0);
257 KASSERT(TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) &&
258 TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
259 ts->ts_obj = obj;
260 ts->ts_sleepq[TS_READER_Q].sq_mutex = tc->tc_mutex;
261 ts->ts_sleepq[TS_WRITER_Q].sq_mutex = tc->tc_mutex;
262 LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
263 } else {
264 /*
265 * Object already has a turnstile. Put our turnstile
266 * onto the free list, and reference the existing
267 * turnstile instead.
268 */
269 ots = l->l_ts;
270 ots->ts_free = ts->ts_free;
271 ts->ts_free = ots;
272 l->l_ts = ts;
273
274 KASSERT(TS_ALL_WAITERS(ts) != 0);
275 KASSERT(!TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) ||
276 !TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
277 }
278
279 sq = &ts->ts_sleepq[q];
280 sleepq_enter(sq, l);
281 sleepq_block(sq, pri, obj, "tstile", 0, 0, &turnstile_syncobj);
282 }
283
284 /*
285 * turnstile_wakeup:
286 *
287 * Wake up the specified number of threads that are blocked
288 * in a turnstile.
289 */
290 void
291 turnstile_wakeup(turnstile_t *ts, int rw, int count, struct lwp *nl)
292 {
293 sleepq_t *sq;
294 tschain_t *tc;
295 struct lwp *l;
296 int swapin;
297
298 KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
299 KASSERT(count > 0);
300
301 swapin = 0;
302 tc = &turnstile_tab[TS_HASH(ts->ts_obj)];
303 sq = &ts->ts_sleepq[rw];
304
305 LOCK_ASSERT(mutex_owned(tc->tc_mutex) && sq->sq_mutex == tc->tc_mutex);
306
307 if (nl != NULL) {
308 #if defined(DEBUG) || defined(LOCKDEBUG)
309 TAILQ_FOREACH(l, &sq->sq_queue, l_sleepchain) {
310 if (l == nl)
311 break;
312 }
313 if (l == NULL)
314 panic("turnstile_wakeup: nl not on sleepq");
315 #endif
316 swapin |= turnstile_remove(ts, nl, sq);
317 } else {
318 while (count-- > 0) {
319 l = TAILQ_FIRST(&sq->sq_queue);
320 KASSERT(l != NULL);
321 swapin |= turnstile_remove(ts, l, sq);
322 }
323 }
324 mutex_spin_exit(tc->tc_mutex);
325
326 /*
327 * If there are newly awakend threads that need to be swapped in,
328 * then kick the swapper into action.
329 */
330 if (swapin)
331 wakeup(&proc0);
332 }
333
334 /*
335 * turnstile_unsleep:
336 *
337 * Remove an LWP from the turnstile. This is called when the LWP has
338 * not been awoken normally but instead interrupted: for example, if it
339 * has received a signal. It's not a valid action for turnstiles,
340 * since LWPs blocking on a turnstile are not interupptable.
341 */
342 void
343 turnstile_unsleep(struct lwp *l)
344 {
345
346 lwp_unlock(l);
347 panic("turnstile_unsleep");
348 }
349
350 /*
351 * turnstile_changepri:
352 *
353 * Adjust the priority of an LWP residing on a turnstile. Since we do
354 * not yet do priority inheritance, we currently ignore this action.
355 */
356 void
357 turnstile_changepri(struct lwp *l, int pri)
358 {
359
360 (void)l;
361 (void)pri;
362 }
363