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