kern_turnstile.c revision 1.1.36.2 1 /* $NetBSD: kern_turnstile.c,v 1.1.36.2 2006/10/20 19:38:44 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 "opt_lockdebug.h"
71 #include "opt_multiprocessor.h"
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.36.2 2006/10/20 19:38:44 ad Exp $");
75
76 #include <sys/param.h>
77 #include <sys/lock.h>
78 #include <sys/pool.h>
79 #include <sys/proc.h>
80 #include <sys/sleepq.h>
81 #include <sys/systm.h>
82
83 #define TS_HASH_SIZE 64
84 #define TS_HASH_MASK (TS_HASH_SIZE - 1)
85 #define TS_HASH(obj) (((uintptr_t)(obj) >> 8) & TS_HASH_MASK)
86
87 tschain_t turnstile_tab[TS_HASH_SIZE];
88
89 struct pool turnstile_pool;
90 struct pool_cache turnstile_cache;
91
92 int turnstile_ctor(void *, void *, int);
93
94 extern turnstile_t turnstile0;
95
96 /*
97 * turnstile_init:
98 *
99 * Initialize the turnstile mechanism.
100 */
101 void
102 turnstile_init(void)
103 {
104 tschain_t *tc;
105 int i;
106
107 for (i = 0; i < TS_HASH_SIZE; i++) {
108 tc = &turnstile_tab[i];
109 mutex_init(&tc->tc_mutex, MUTEX_SPIN, IPL_SCHED);
110 LIST_INIT(&tc->tc_chain);
111 }
112
113 pool_init(&turnstile_pool, sizeof(turnstile_t), 0, 0, 0,
114 "tspool", &pool_allocator_nointr);
115 pool_cache_init(&turnstile_cache, &turnstile_pool,
116 turnstile_ctor, NULL, NULL);
117
118 (void)turnstile_ctor(NULL, &turnstile0, 0);
119 }
120
121 /*
122 * turnstile_ctor:
123 *
124 * Constructor for turnstiles.
125 */
126 int
127 turnstile_ctor(void *arg, void *obj, int flags)
128 {
129 turnstile_t *ts = obj;
130
131 memset(ts, 0, sizeof(*ts));
132 sleepq_init(&ts->ts_sleepq[TS_READER_Q], NULL);
133 sleepq_init(&ts->ts_sleepq[TS_WRITER_Q], NULL);
134 return (0);
135 }
136
137 /*
138 * turnstile_remove:
139 *
140 * Remove an LWP from a turnstile sleep queue and wake it.
141 */
142 static inline int
143 turnstile_remove(turnstile_t *ts, struct lwp *l, sleepq_t *sq)
144 {
145 turnstile_t *nts;
146
147 KASSERT(l->l_ts == ts);
148
149 /*
150 * This process is no longer using the active turnstile.
151 * Find an inactive one on the free list to give to it.
152 */
153 if ((nts = ts->ts_free) != NULL) {
154 KASSERT(TS_ALL_WAITERS(ts) > 1);
155 l->l_ts = nts;
156 ts->ts_free = nts->ts_free;
157 nts->ts_free = NULL;
158 } else {
159 /*
160 * If the free list is empty, this is the last
161 * waiter.
162 */
163 KASSERT(TS_ALL_WAITERS(ts) == 1);
164 LIST_REMOVE(ts, ts_chain);
165 }
166
167 return sleepq_remove(sq, l);
168 }
169
170 /*
171 * turnstile_lookup:
172 *
173 * Look up the turnstile for the specified lock. This acquires and
174 * holds the turnstile chain lock (sleep queue interlock).
175 */
176 turnstile_t *
177 turnstile_lookup(wchan_t obj)
178 {
179 turnstile_t *ts;
180 tschain_t *tc;
181
182 tc = &turnstile_tab[TS_HASH(obj)];
183
184 mutex_enter(&tc->tc_mutex);
185
186 LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
187 if (ts->ts_obj == obj)
188 return (ts);
189
190 /*
191 * No turnstile yet for this lock. No problem, turnstile_block()
192 * handles this by fetching the turnstile from the blocking thread.
193 */
194 return (NULL);
195 }
196
197 /*
198 * turnstile_exit:
199 *
200 * Abort a turnstile operation.
201 */
202 void
203 turnstile_exit(wchan_t obj)
204 {
205 tschain_t *tc;
206
207 tc = &turnstile_tab[TS_HASH(obj)];
208 mutex_exit(&tc->tc_mutex);
209 }
210
211 /*
212 * turnstile_block:
213 *
214 * Enter an object into the turnstile chain and prepare the current
215 * LWP for sleep.
216 */
217 void
218 turnstile_block(turnstile_t *ts, int q, int pri, wchan_t obj)
219 {
220 struct lwp *l;
221 turnstile_t *ots;
222 tschain_t *tc;
223 sleepq_t *sq;
224
225 KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
226
227 tc = &turnstile_tab[TS_HASH(obj)];
228 l = curlwp;
229
230 LOCK_ASSERT(mutex_owned(&tc->tc_mutex));
231 KASSERT(l != NULL && l->l_ts != NULL);
232
233 if (ts == NULL) {
234 /*
235 * We are the first thread to wait for this object;
236 * lend our turnstile to it.
237 */
238 ts = l->l_ts;
239 KASSERT(TS_ALL_WAITERS(ts) == 0);
240 KASSERT(TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) &&
241 TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
242 ts->ts_obj = obj;
243 LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
244 } else {
245 /*
246 * Object already has a turnstile. Put our turnstile
247 * onto the free list, and reference the existing
248 * turnstile instead.
249 */
250 ots = l->l_ts;
251 ots->ts_free = ts->ts_free;
252 ts->ts_free = ots;
253 l->l_ts = ts;
254
255 KASSERT(TS_ALL_WAITERS(ts) != 0);
256 KASSERT(!TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q].sq_queue) ||
257 !TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q].sq_queue));
258 }
259
260 sq = &ts->ts_sleepq[q];
261 sq->sq_mutex = &tc->tc_mutex;
262 sleepq_enter(sq, pri, obj, "tstile", 0, 0);
263 (void)sleepq_block(sq, 0);
264 }
265
266 /*
267 * turnstile_wakeup:
268 *
269 * Wake up the specified number of threads that are blocked
270 * in a turnstile.
271 */
272 void
273 turnstile_wakeup(turnstile_t *ts, int rw, int count, struct lwp *nl)
274 {
275 sleepq_t *sq;
276 tschain_t *tc;
277 struct lwp *l;
278 int swapin;
279
280 KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
281 KASSERT(count > 0);
282
283 swapin = 0;
284 tc = &turnstile_tab[TS_HASH(ts->ts_obj)];
285 sq = &ts->ts_sleepq[rw];
286
287 LOCK_ASSERT(mutex_owned(&tc->tc_mutex) && sq->sq_mutex == &tc->tc_mutex);
288
289 mutex_enter(&sched_mutex);
290 if (nl != NULL) {
291 #if defined(DEBUG) || defined(LOCKDEBUG)
292 TAILQ_FOREACH(l, &sq->sq_queue, l_sleepq) {
293 if (l == nl)
294 break;
295 }
296 if (l == NULL)
297 panic("turnstile_wakeup: nl not on sleepq");
298 #endif
299 swapin |= turnstile_remove(ts, nl, sq);
300 } else {
301 while (count-- > 0) {
302 l = TAILQ_FIRST(&sq->sq_queue);
303 KASSERT(l != NULL);
304 swapin |= turnstile_remove(ts, l, sq);
305 }
306 }
307 mutex_exit(&sched_mutex);
308 mutex_exit(&tc->tc_mutex);
309
310 /*
311 * If there are newly awakend threads that need to be swapped in,
312 * then kick the swapper into action.
313 */
314 if (swapin)
315 wakeup(&proc0);
316 }
317