kern_sleepq.c revision 1.1.2.11 1 /* $NetBSD: kern_sleepq.c,v 1.1.2.11 2007/01/30 13:51:41 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by 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 * Sleep queue implementation, used by turnstiles and general sleep/wakeup
41 * interfaces.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.1.2.11 2007/01/30 13:51:41 ad Exp $");
46
47 #include "opt_multiprocessor.h"
48 #include "opt_lockdebug.h"
49 #include "opt_ktrace.h"
50
51 #include <sys/param.h>
52 #include <sys/lock.h>
53 #include <sys/kernel.h>
54 #include <sys/pool.h>
55 #include <sys/proc.h>
56 #include <sys/resourcevar.h>
57 #include <sys/sched.h>
58 #include <sys/systm.h>
59 #include <sys/sleepq.h>
60
61 #ifdef KTRACE
62 #include <sys/ktrace.h>
63 #endif
64
65 int sleepq_sigtoerror(struct lwp *, int);
66 void updatepri(struct lwp *);
67 void sa_awaken(struct lwp *);
68
69 /* General purpose sleep table, used by ltsleep() and condition variables. */
70 sleeptab_t sleeptab;
71
72 /*
73 * sleeptab_init:
74 *
75 * Initialize a sleep table.
76 */
77 void
78 sleeptab_init(sleeptab_t *st)
79 {
80 sleepq_t *sq;
81 int i;
82
83 for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
84 sq = &st->st_queues[i];
85 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
86 mutex_init(&st->st_mutexes[i], MUTEX_SPIN, IPL_SCHED);
87 sleepq_init(sq, &st->st_mutexes[i]);
88 #else
89 sleepq_init(sq, &sched_mutex);
90 #endif
91 }
92 }
93
94 /*
95 * sleepq_init:
96 *
97 * Prepare a sleep queue for use.
98 */
99 void
100 sleepq_init(sleepq_t *sq, kmutex_t *mtx)
101 {
102
103 sq->sq_waiters = 0;
104 sq->sq_mutex = mtx;
105 TAILQ_INIT(&sq->sq_queue);
106 }
107
108 /*
109 * sleepq_remove:
110 *
111 * Remove an LWP from a sleep queue and wake it up. Return non-zero if
112 * the LWP is swapped out; if so the caller needs to awaken the swapper
113 * to bring the LWP into memory.
114 */
115 int
116 sleepq_remove(sleepq_t *sq, struct lwp *l)
117 {
118 struct cpu_info *ci;
119
120 LOCK_ASSERT(lwp_locked(l, sq->sq_mutex));
121 KASSERT(sq->sq_waiters > 0);
122
123 sq->sq_waiters--;
124 TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
125
126 #ifdef DIAGNOSTIC
127 if (sq->sq_waiters == 0)
128 KASSERT(TAILQ_FIRST(&sq->sq_queue) == NULL);
129 else
130 KASSERT(TAILQ_FIRST(&sq->sq_queue) != NULL);
131 #endif
132
133 l->l_syncobj = &sched_syncobj;
134 l->l_wchan = NULL;
135 l->l_sleepq = NULL;
136 l->l_flag &= ~L_SINTR;
137
138 /*
139 * If not sleeping, the LWP must have been suspended. Let whoever
140 * holds it stopped set it running again.
141 */
142 if (l->l_stat != LSSLEEP) {
143 KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
144 lwp_setlock(l, &sched_mutex);
145 return 0;
146 }
147
148 sched_lock(1);
149 lwp_setlock(l, &sched_mutex);
150
151 /*
152 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
153 * about to call mi_switch(), in which case it will yield.
154 *
155 * XXXSMP Will need to change for preemption.
156 */
157 ci = l->l_cpu;
158 #ifdef MULTIPROCESSOR
159 if (ci->ci_curlwp == l) {
160 #else
161 if (l == curlwp) {
162 #endif
163 l->l_stat = LSONPROC;
164 l->l_slptime = 0;
165 sched_unlock(1);
166 return 0;
167 }
168
169 /*
170 * Set it running. We'll try to get the last CPU that ran
171 * this LWP to pick it up again.
172 */
173 if (l->l_slptime > 1)
174 updatepri(l);
175 l->l_stat = LSRUN;
176 l->l_slptime = 0;
177 if ((l->l_flag & L_INMEM) != 0) {
178 setrunqueue(l);
179 if (l->l_priority < ci->ci_schedstate.spc_curpriority)
180 cpu_need_resched(ci);
181 sched_unlock(1);
182 return 0;
183 }
184
185 sched_unlock(1);
186 return 1;
187 }
188
189 /*
190 * sleepq_insert:
191 *
192 * Insert an LWP into the sleep queue, optionally sorting by priority.
193 */
194 static inline void
195 sleepq_insert(sleepq_t *sq, struct lwp *l, int pri, syncobj_t *sobj)
196 {
197 struct lwp *l2;
198
199 if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
200 TAILQ_FOREACH(l2, &sq->sq_queue, l_sleepchain) {
201 if (l2->l_priority > pri) {
202 TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
203 return;
204 }
205 }
206 }
207
208 TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_sleepchain);
209 }
210
211 /*
212 * sleepq_block:
213 *
214 * Enter an LWP into the sleep queue and prepare for sleep. The sleep
215 * queue must already be locked, and any interlock (such as the kernel
216 * lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
217 *
218 * sleepq_block() may return early under exceptional conditions, for
219 * example if the LWP's containing process is exiting.
220 */
221 void
222 sleepq_block(sleepq_t *sq, int pri, wchan_t wchan, const char *wmesg, int timo,
223 int catch, syncobj_t *sobj)
224 {
225 struct lwp *l = curlwp;
226
227 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
228 KASSERT(l->l_stat == LSONPROC);
229 KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
230
231 #ifdef KTRACE
232 if (KTRPOINT(l->l_proc, KTR_CSW))
233 ktrcsw(l, 1, 0);
234 #endif
235
236 l->l_syncobj = sobj;
237 l->l_wchan = wchan;
238 l->l_sleepq = sq;
239 l->l_wmesg = wmesg;
240 l->l_slptime = 0;
241 l->l_priority = pri;
242 l->l_stat = LSSLEEP;
243 l->l_sleeperr = 0;
244 l->l_nvcsw++;
245
246 sq->sq_waiters++;
247 sleepq_insert(sq, l, pri, sobj);
248
249 /*
250 * If sleeping interruptably, check for pending signals, exits or
251 * core dump events.
252 */
253 if (catch) {
254 l->l_flag |= L_SINTR;
255 if ((l->l_flag & L_PENDSIG) != 0 && sigispending(l, 0)) {
256 l->l_sleeperr = EPASSTHROUGH;
257 /* lwp_unsleep() will release the lock */
258 lwp_unsleep(l);
259 return;
260 }
261 if ((l->l_flag & (L_CANCELLED|L_WEXIT|L_WCORE)) != 0) {
262 l->l_flag &= ~L_CANCELLED;
263 l->l_sleeperr = EINTR;
264 /* lwp_unsleep() will release the lock */
265 lwp_unsleep(l);
266 return;
267 }
268 }
269
270 if (timo)
271 callout_reset(&l->l_tsleep_ch, timo, sleepq_timeout, l);
272
273 mi_switch(l, NULL);
274 l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
275
276 /*
277 * When we reach this point, the LWP and sleep queue are unlocked.
278 */
279 KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
280 }
281
282 /*
283 * sleepq_unblock:
284 *
285 * After any intermediate step such as updating statistics, re-acquire
286 * the kernel lock and record the switch for ktrace. Note that we are
287 * no longer on the sleep queue at this point.
288 *
289 * This is split out from sleepq_block() in expectation that at some
290 * point in the future, LWPs may awake on different kernel stacks than
291 * those they went asleep on.
292 */
293 int
294 sleepq_unblock(int timo, int catch)
295 {
296 int error, expired, sig;
297 struct proc *p;
298 struct lwp *l;
299
300 l = curlwp;
301 error = l->l_sleeperr;
302
303 if (timo) {
304 /*
305 * Even if the callout appears to have fired, we need to
306 * stop it in order to synchronise with other CPUs.
307 */
308 expired = callout_expired(&l->l_tsleep_ch);
309 callout_stop(&l->l_tsleep_ch);
310 if (expired && error == 0)
311 error = EWOULDBLOCK;
312 }
313
314 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
315 /*
316 * Re-acquire the kernel lock. XXXSMP This can come after the
317 * ktrace stuff below once its safe to allocate memory unlocked.
318 */
319 KERNEL_LOCK(l->l_biglocks, l);
320 #endif
321
322 if (catch && (error == 0 || error == EPASSTHROUGH)) {
323 l->l_sleeperr = 0;
324 p = l->l_proc;
325 if ((l->l_flag & (L_CANCELLED | L_WEXIT | L_WCORE)) != 0)
326 error = EINTR;
327 else if ((l->l_flag & L_PENDSIG) != 0) {
328 mutex_enter(&p->p_smutex);
329 if ((sig = issignal(l)) != 0)
330 error = sleepq_sigtoerror(l, sig);
331 mutex_exit(&p->p_smutex);
332 }
333 if (error == EPASSTHROUGH) {
334 /* Raced */
335 error = EINTR;
336 }
337 }
338
339 #ifdef KTRACE
340 if (KTRPOINT(l->l_proc, KTR_CSW))
341 ktrcsw(l, 0, 0);
342 #endif
343
344 return error;
345 }
346
347 /*
348 * sleepq_wake:
349 *
350 * Wake zero or more LWPs blocked on a single wait channel.
351 */
352 void
353 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
354 {
355 struct lwp *l, *next;
356 int swapin = 0;
357
358 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
359
360 for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
361 KASSERT(l->l_sleepq == sq);
362 next = TAILQ_NEXT(l, l_sleepchain);
363 if (l->l_wchan != wchan)
364 continue;
365 swapin |= sleepq_remove(sq, l);
366 if (--expected == 0)
367 break;
368 }
369
370 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
371 sleepq_unlock(sq);
372
373 /*
374 * If there are newly awakend threads that need to be swapped in,
375 * then kick the swapper into action.
376 */
377 if (swapin)
378 wakeup(&proc0);
379 }
380
381 /*
382 * sleepq_unsleep:
383 *
384 * Remove an LWP from its sleep queue and set it runnable again.
385 * sleepq_unsleep() is called with the LWP's mutex held, and will
386 * always release it.
387 */
388 void
389 sleepq_unsleep(struct lwp *l)
390 {
391 sleepq_t *sq = l->l_sleepq;
392 int swapin;
393
394 LOCK_ASSERT(lwp_locked(l, NULL));
395 KASSERT(l->l_wchan != NULL);
396 KASSERT(l->l_mutex == sq->sq_mutex);
397
398 swapin = sleepq_remove(sq, l);
399 sleepq_unlock(sq);
400
401 if (swapin)
402 wakeup(&proc0);
403 }
404
405 /*
406 * sleepq_timeout:
407 *
408 * Entered via the callout(9) subsystem to time out an LWP that is on a
409 * sleep queue.
410 */
411 void
412 sleepq_timeout(void *arg)
413 {
414 struct lwp *l = arg;
415
416 /*
417 * Lock the LWP. Assuming it's still on the sleep queue, its
418 * current mutex will also be the sleep queue mutex.
419 */
420 lwp_lock(l);
421
422 if (l->l_wchan == NULL) {
423 /* Somebody beat us to it. */
424 lwp_unlock(l);
425 return;
426 }
427
428 lwp_unsleep(l);
429 }
430
431 /*
432 * sleepq_sigtoerror:
433 *
434 * Given a signal number, interpret and return an error code.
435 */
436 int
437 sleepq_sigtoerror(struct lwp *l, int sig)
438 {
439 struct proc *p = l->l_proc;
440 int error;
441
442 LOCK_ASSERT(mutex_owned(&p->p_smutex));
443
444 /*
445 * If this sleep was canceled, don't let the syscall restart.
446 */
447 if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
448 error = EINTR;
449 else
450 error = ERESTART;
451
452 return error;
453 }
454
455 /*
456 * sleepq_abort:
457 *
458 * After a panic or during autoconfiguration, lower the interrupt
459 * priority level to give pending interrupts a chance to run, and
460 * then return. Called if sleepq_dontsleep() returns non-zero, and
461 * always returns zero.
462 */
463 int
464 sleepq_abort(kmutex_t *mtx, int unlock)
465 {
466 extern int safepri;
467 int s;
468
469 s = splhigh();
470 splx(safepri);
471 splx(s);
472 if (mtx != NULL && unlock != 0)
473 mutex_exit(mtx);
474
475 return 0;
476 }
477
478 /*
479 * sleepq_changepri:
480 *
481 * Adjust the priority of an LWP residing on a sleepq.
482 */
483 void
484 sleepq_changepri(struct lwp *l, int pri)
485 {
486 sleepq_t *sq = l->l_sleepq;
487
488 KASSERT(lwp_locked(l, sq->sq_mutex));
489
490 if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0)
491 return;
492
493 TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
494 sleepq_insert(sq, l, pri, l->l_syncobj);
495 }
496