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