kern_sleepq.c revision 1.1.2.3 1 /* $NetBSD: kern_sleepq.c,v 1.1.2.3 2006/10/21 14:03:14 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 "opt_multiprocessor.h"
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.1.2.3 2006/10/21 14:03:14 ad Exp $");
48
49 #include <sys/param.h>
50 #include <sys/lock.h>
51 #include <sys/kernel.h>
52 #include <sys/pool.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sched.h>
56 #include <sys/systm.h>
57 #include <sys/sa.h>
58 #include <sys/savar.h>
59 #include <sys/sleepq.h>
60
61 int sleepq_sigtoerror(struct lwp *, int);
62 void updatepri(struct lwp *);
63 void sa_awaken(struct lwp *);
64
65 sleepq_t sleeptab[SLEEPTAB_HASH_SIZE];
66 #ifdef MULTIPROCESSOR
67 kmutex_t sleeptab_mutexes[SLEEPTAB_HASH_SIZE];
68 #else
69 kmutex_t sleeptab_mutex;
70 #endif
71
72 /*
73 * sleeptab_init:
74 *
75 * Initialize the general-purpose sleep queues.
76 */
77 void
78 sleeptab_init(void)
79 {
80 sleepq_t *sq;
81 int i;
82
83 #ifndef MULTIPROCESSOR
84 mutex_init(&sleeptab_mutex, MUTEX_SPIN, IPL_SCHED);
85 #endif
86
87 for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
88 sq = &sleeptab[i];
89 #ifdef MULTIPROCESSOR
90 mutex_init(&sleeptab_mutexes[i], MUTEX_SPIN, IPL_SCHED);
91 sleepq_init(&sleeptab[i], &sleeptab_mutexes[i]);
92 #else
93 sleepq_init(&sleeptab[i], &sleeptab_mutex);
94 #endif
95 }
96 }
97
98 /*
99 * sleepq_init:
100 *
101 * Prepare a sleep queue for use.
102 */
103 void
104 sleepq_init(sleepq_t *sq, kmutex_t *mtx)
105 {
106
107 sq->sq_waiters = 0;
108 sq->sq_mutex = mtx;
109 TAILQ_INIT(&sq->sq_queue);
110 }
111
112 /*
113 * sleepq_remove:
114 *
115 * Remove an LWP from a sleep queue and wake it up. Return non-zero if
116 * the LWP is swapped out; if so the caller needs to awaken the swapper
117 * to bring the LWP into memory.
118 */
119 int
120 sleepq_remove(sleepq_t *sq, struct lwp *l)
121 {
122
123 LOCK_ASSERT(lwp_locked(l, sq->sq_mutex));
124 KASSERT(sq->sq_waiters > 0);
125
126 l->l_wchan = NULL;
127 l->l_slptime = 0;
128 l->l_flag &= ~L_SINTR;
129
130 sq->sq_waiters--;
131 TAILQ_REMOVE(&sq->sq_queue, l, l_sleepq);
132
133 #ifdef DIAGNOSTIC
134 if (sq->sq_waiters == 0)
135 KASSERT(TAILQ_FIRST(&sq->sq_queue) == NULL);
136 else
137 KASSERT(TAILQ_FIRST(&sq->sq_queue) != NULL);
138 #endif
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 return 0;
147 }
148
149 if (l == curlwp) {
150 l->l_stat = LSONPROC;
151 lwp_setlock(l, &l->l_cpu->ci_sched_mutex);
152 return 0;
153 }
154
155 l->l_stat = LSRUN;
156
157 if (l->l_proc->p_sa)
158 sa_awaken(l);
159 if (l->l_slptime > 1)
160 updatepri(l);
161
162 /*
163 * Try to set the LWP running, and swap in its new mutex. Once
164 * we've done the swap, we can't touch the LWP again.
165 */
166 if ((l->l_flag & L_INMEM) != 0) {
167 /*
168 * Try to get the last CPU that ran this LWP to pick it up.
169 */
170 setrunqueue(l);
171 lwp_setlock(l, &sched_mutex);
172 cpu_need_resched(l->l_cpu);
173 return 0;
174 }
175
176 lwp_setlock(l, &lwp_mutex);
177 return 1;
178 }
179
180 /*
181 * sleepq_enter:
182 *
183 * Enter an LWP into the sleep queue and prepare for sleep. Any interlocking
184 * step such as releasing a mutex or checking for signals may be safely done
185 * by the caller once on the sleep queue.
186 */
187 void
188 sleepq_enter(sleepq_t *sq, int pri, wchan_t wchan, const char *wmesg, int timo,
189 int catch)
190 {
191 struct lwp *l = curlwp;
192
193 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
194
195 #ifdef KTRACE
196 if (KTRPOINT(p, KTR_CSW))
197 ktrcsw(l, 1, 0);
198 #endif
199
200 sq->sq_waiters++;
201 TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_sleepq);
202
203 /*
204 * Acquire the per-LWP mutex.
205 */
206 lwp_lock(l);
207
208 KASSERT(l->l_wchan == NULL);
209
210 l->l_wchan = wchan;
211 l->l_wmesg = wmesg;
212 l->l_slptime = 0;
213 l->l_priority = pri & PRIMASK;
214 l->l_flag &= ~L_CANCELLED;
215 if (catch)
216 l->l_flag |= L_SINTR;
217 if (l->l_stat == LSONPROC)
218 l->l_stat = LSSLEEP;
219 l->l_nvcsw++;
220
221 if (timo)
222 callout_reset(&l->l_tsleep_ch, timo, sleepq_timeout, l);
223
224 /*
225 * The LWP is now on the sleep queue. Release its old mutex and
226 * lend it ours for the duration of the sleep.
227 */
228 lwp_swaplock(l, sq->sq_mutex);
229 }
230
231 /*
232 * sleepq_block:
233 *
234 * The calling LWP has been entered into the sleep queue by
235 * sleepq_enter(), and now wants to block. sleepq_block() may return
236 * early under exceptional conditions, for example if the LWP's process
237 * is exiting. sleepq_block() must be called if sleepq_enter() has
238 * been called.
239 */
240 int
241 sleepq_block(sleepq_t *sq, int timo)
242 {
243 int error, flag, expired, sig;
244 struct lwp *l = curlwp;
245 struct proc *p;
246
247 LOCK_ASSERT(lwp_locked(l, sq->sq_mutex));
248
249 flag = l->l_flag;
250 error = 0;
251
252 /*
253 * If sleeping interruptably, check for pending signals, exits or
254 * core dump events.
255 */
256 if ((flag & L_SINTR) != 0) {
257 while ((l->l_flag & L_PENDSIG) != 0 && error == 0) {
258 lwp_unlock(l);
259 p = l->l_proc;
260 mutex_enter(&p->p_smutex);
261 if ((sig = issignal(l)) != 0)
262 error = sleepq_sigtoerror(l, sig);
263 mutex_exit(&p->p_smutex);
264 lwp_lock(l);
265 }
266
267 if (error == 0 && (l->l_flag & (L_WEXIT | L_WCORE)) != 0)
268 error = EINTR;
269
270 if (error != 0) {
271 /*
272 * If the LWP is on a sleep queue and we remove it,
273 * we will change its mutex and so we need to unlock
274 * the sleep queue. If it's off the sleep queue
275 * already, the unlock the LWP directly.
276 */
277 if (l->l_wchan != NULL) {
278 mutex_enter(&sched_mutex);
279 (void)sleepq_remove(sq, l);
280 mutex_exit(&sched_mutex);
281 mutex_exit(sq->sq_mutex);
282 } else
283 lwp_unlock(l);
284
285 goto out;
286 }
287 }
288
289 if (l->l_stat == LSONPROC) {
290 /*
291 * We may have decided not to switch away, and so removed
292 * ourself from the sleep queue.
293 */
294 lwp_unlock(l);
295 } else if ((flag & L_SA) != 0) {
296 sa_switch(l, sadata_upcall_alloc(0), SA_UPCALL_BLOCKED);
297 /* XXXAD verify sa_switch restores SPL. */
298 } else {
299 mi_switch(l, NULL);
300
301 l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
302 }
303
304 KASSERT(l->l_wchan == NULL);
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)
314 return EWOULDBLOCK;
315 }
316
317 if ((flag & L_SINTR) != 0) {
318 if ((l->l_flag & (L_CANCELLED | L_WEXIT | L_WCORE)) != 0)
319 error = EINTR;
320 else if ((l->l_flag & L_PENDSIG) != 0) {
321 p = l->l_proc;
322 mutex_enter(&p->p_smutex);
323 if ((sig = issignal(l)) != 0)
324 error = sleepq_sigtoerror(l, sig);
325 mutex_exit(&p->p_smutex);
326 }
327 }
328
329 out:
330 #ifdef KTRACE
331 if (KTRPOINT(p, KTR_CSW))
332 ktrcsw(l, 0, 0);
333 #endif
334 return error;
335 }
336
337 /*
338 * sleepq_wakeone:
339 *
340 * Remove one LWP from the sleep queue and wake it. We search among
341 * the higest priority LWPs waiting on a single wait channel, and pick
342 * the longest waiting one.
343 */
344 void
345 sleepq_wakeone(sleepq_t *sq, wchan_t wchan)
346 {
347 struct lwp *l, *bl;
348 int bpri, swapin;
349
350 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
351
352 swapin = 0;
353 bpri = MAXPRI;
354 bl = NULL;
355
356 TAILQ_FOREACH(l, &sq->sq_queue, l_sleepq) {
357 if (l->l_wchan != wchan || l->l_priority > bpri)
358 continue;
359 bl = l;
360 bpri = l->l_priority;
361 }
362
363 if (bl != NULL) {
364 mutex_enter(&sched_mutex);
365 swapin = sleepq_remove(sq, bl);
366 mutex_exit(&sched_mutex);
367 }
368
369 mutex_exit(sq->sq_mutex);
370
371 if (swapin)
372 wakeup(&proc0);
373 }
374
375 /*
376 * sleepq_wakeall:
377 *
378 * Wake all LWPs blocked on a single wait channel.
379 */
380 void
381 sleepq_wakeall(sleepq_t *sq, wchan_t wchan, u_int expected)
382 {
383 struct lwp *l, *next;
384 int swapin = 0;
385
386 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
387
388 mutex_enter(&sched_mutex);
389 for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
390 next = TAILQ_NEXT(l, l_sleepq);
391 if (l->l_wchan != wchan)
392 continue;
393 swapin |= sleepq_remove(sq, l);
394 if (--expected == 0)
395 break;
396 }
397 mutex_exit(&sched_mutex);
398
399 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
400 mutex_exit(sq->sq_mutex);
401
402 /*
403 * If there are newly awakend threads that need to be swapped in,
404 * then kick the swapper into action.
405 */
406 if (swapin)
407 wakeup(&proc0);
408 }
409
410 /*
411 * sleepq_unsleep:
412 *
413 * Remove an LWP from its sleep queue and set it runnable again.
414 * sleepq_unsleep() is called with the LWP's mutex held, and will
415 * always release it.
416 */
417 void
418 sleepq_unsleep(struct lwp *l)
419 {
420 sleepq_t *sq;
421 int swapin;
422
423 sq = &sleeptab[SLEEPTAB_HASH(l->l_wchan)];
424 KASSERT(l->l_wchan != NULL);
425 KASSERT(l->l_mutex == sq->sq_mutex);
426
427 mutex_enter(&sched_mutex);
428 swapin = sleepq_remove(sq, l);
429 mutex_exit(&sched_mutex);
430
431 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
432 mutex_exit(sq->sq_mutex);
433
434 if (swapin)
435 wakeup(&proc0);
436 }
437
438 /*
439 * sleepq_timeout:
440 *
441 * Entered via the callout(9) subsystem to time out an LWP that is on a
442 * sleep queue.
443 */
444 void
445 sleepq_timeout(void *arg)
446 {
447 struct lwp *l = arg;
448
449 /*
450 * Lock the LWP. Assuming it's still on the sleep queue, its
451 * current mutex will also be the sleep queue mutex.
452 */
453 lwp_lock(l);
454
455 if (l->l_wchan == NULL) {
456 /* Somebody beat us to it. */
457 lwp_unlock(l);
458 return;
459 }
460
461 sleepq_unsleep(arg);
462 }
463
464 /*
465 * sleepq_sigtoerror:
466 *
467 * Given a signal number, interpret and return an error code.
468 */
469 int
470 sleepq_sigtoerror(struct lwp *l, int sig)
471 {
472 struct proc *p = l->l_proc;
473 int error;
474
475 LOCK_ASSERT(mutex_owned(&p->p_smutex));
476
477 /*
478 * If this sleep was canceled, don't let the syscall restart.
479 */
480 if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
481 error = EINTR;
482 else
483 error = ERESTART;
484
485 return error;
486 }
487
488 /*
489 * sleepq_abort:
490 *
491 * After a panic or during autoconfiguration, lower the interrupt
492 * priority level to give pending interrupts a chance to run, and
493 * then return. Called if sleepq_dontsleep() returns non-zero, and
494 * always returns zero.
495 */
496 int
497 sleepq_abort(kmutex_t *mtx, int unlock)
498 {
499 extern int safepri;
500 int s;
501
502 s = splhigh();
503 splx(safepri);
504 splx(s);
505 if (mtx != NULL && unlock != 0)
506 mutex_exit(mtx);
507
508 return 0;
509 }
510