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