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