kern_sleepq.c revision 1.4.2.9 1 /* $NetBSD: kern_sleepq.c,v 1.4.2.9 2007/03/24 00:43:07 rmind 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.9 2007/03/24 00:43:07 rmind 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(struct lwp *, 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, ci->ci_schedstate.spc_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, struct lwp *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, struct lwp *l, syncobj_t *sobj)
190 {
191 struct lwp *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 struct lwp *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, int catch)
232 {
233 struct lwp *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, int 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, int catch)
305 {
306 int error, expired, sig;
307 struct proc *p;
308 struct lwp *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 void
358 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
359 {
360 struct lwp *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
385 /*
386 * sleepq_unsleep:
387 *
388 * Remove an LWP from its sleep queue and set it runnable again.
389 * sleepq_unsleep() is called with the LWP's mutex held, and will
390 * always release it.
391 */
392 void
393 sleepq_unsleep(struct lwp *l)
394 {
395 sleepq_t *sq = l->l_sleepq;
396 int swapin;
397
398 KASSERT(lwp_locked(l, NULL));
399 KASSERT(l->l_wchan != NULL);
400 KASSERT(l->l_mutex == sq->sq_mutex);
401
402 swapin = sleepq_remove(sq, l);
403 sleepq_unlock(sq);
404
405 if (swapin)
406 uvm_kick_scheduler();
407 }
408
409 /*
410 * sleepq_timeout:
411 *
412 * Entered via the callout(9) subsystem to time out an LWP that is on a
413 * sleep queue.
414 */
415 void
416 sleepq_timeout(void *arg)
417 {
418 struct lwp *l = arg;
419
420 /*
421 * Lock the LWP. Assuming it's still on the sleep queue, its
422 * current mutex will also be the sleep queue mutex.
423 */
424 lwp_lock(l);
425
426 if (l->l_wchan == NULL) {
427 /* Somebody beat us to it. */
428 lwp_unlock(l);
429 return;
430 }
431
432 lwp_unsleep(l);
433 }
434
435 /*
436 * sleepq_sigtoerror:
437 *
438 * Given a signal number, interpret and return an error code.
439 */
440 int
441 sleepq_sigtoerror(struct lwp *l, int sig)
442 {
443 struct proc *p = l->l_proc;
444 int error;
445
446 KASSERT(mutex_owned(&p->p_smutex));
447
448 /*
449 * If this sleep was canceled, don't let the syscall restart.
450 */
451 if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
452 error = EINTR;
453 else
454 error = ERESTART;
455
456 return error;
457 }
458
459 /*
460 * sleepq_abort:
461 *
462 * After a panic or during autoconfiguration, lower the interrupt
463 * priority level to give pending interrupts a chance to run, and
464 * then return. Called if sleepq_dontsleep() returns non-zero, and
465 * always returns zero.
466 */
467 int
468 sleepq_abort(kmutex_t *mtx, int unlock)
469 {
470 extern int safepri;
471 int s;
472
473 s = splhigh();
474 splx(safepri);
475 splx(s);
476 if (mtx != NULL && unlock != 0)
477 mutex_exit(mtx);
478
479 return 0;
480 }
481
482 /*
483 * sleepq_changepri:
484 *
485 * Adjust the priority of an LWP residing on a sleepq. This method
486 * will only alter the user priority; the effective priority is
487 * assumed to have been fixed at the time of insertion into the queue.
488 */
489 void
490 sleepq_changepri(struct lwp *l, pri_t pri)
491 {
492
493 KASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
494 l->l_usrpri = pri;
495 }
496
497 void
498 sleepq_lendpri(struct lwp *l, pri_t pri)
499 {
500 sleepq_t *sq = l->l_sleepq;
501 pri_t opri;
502
503 KASSERT(lwp_locked(l, sq->sq_mutex));
504
505 opri = lwp_eprio(l);
506 l->l_inheritedprio = pri;
507
508 if (lwp_eprio(l) != opri &&
509 (l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
510 TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
511 sleepq_insert(sq, l, l->l_syncobj);
512 }
513 }
514