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