kern_sleepq.c revision 1.1.2.1 1 /* $NetBSD: kern_sleepq.c,v 1.1.2.1 2006/10/20 19:38: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 "opt_multiprocessor.h"
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.1.2.1 2006/10/20 19:38:44 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 (void)sleepq_remove(sq, l);
279 mutex_exit(sq->sq_mutex);
280 } else
281 lwp_unlock(l);
282
283 goto out;
284 }
285 }
286
287 if (l->l_stat == LSONPROC) {
288 /*
289 * We may have decided not to switch away, and so removed
290 * ourself from the sleep queue.
291 */
292 lwp_unlock(l);
293 } else if ((flag & L_SA) != 0) {
294 sa_switch(l, sadata_upcall_alloc(0), SA_UPCALL_BLOCKED);
295 /* XXXAD verify sa_switch restores SPL. */
296 } else {
297 mi_switch(l, NULL);
298
299 l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
300 }
301
302 KASSERT(l->l_wchan == NULL);
303
304 if (timo) {
305 /*
306 * Even if the callout appears to have fired, we need to
307 * stop it in order to synchronise with other CPUs.
308 */
309 expired = callout_expired(&l->l_tsleep_ch);
310 callout_stop(&l->l_tsleep_ch);
311 if (expired)
312 return EWOULDBLOCK;
313 }
314
315 if ((flag & L_SINTR) != 0) {
316 if ((l->l_flag & (L_CANCELLED | L_WEXIT | L_WCORE)) != 0)
317 error = EINTR;
318 else if ((l->l_flag & L_PENDSIG) != 0) {
319 p = l->l_proc;
320 mutex_enter(&p->p_smutex);
321 if ((sig = issignal(l)) != 0)
322 error = sleepq_sigtoerror(l, sig);
323 mutex_exit(&p->p_smutex);
324 }
325 }
326
327 out:
328 #ifdef KTRACE
329 if (KTRPOINT(p, KTR_CSW))
330 ktrcsw(l, 0, 0);
331 #endif
332 return error;
333 }
334
335 /*
336 * sleepq_wakeone:
337 *
338 * Remove one LWP from the sleep queue and wake it. We search among
339 * the higest priority LWPs waiting on a single wait channel, and pick
340 * the longest waiting one.
341 */
342 void
343 sleepq_wakeone(sleepq_t *sq, wchan_t wchan)
344 {
345 struct lwp *l, *bl;
346 int bpri, swapin;
347
348 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
349
350 swapin = 0;
351 bpri = MAXPRI;
352 bl = NULL;
353
354 TAILQ_FOREACH(l, &sq->sq_queue, l_sleepq) {
355 if (l->l_wchan != wchan || l->l_priority > bpri)
356 continue;
357 bl = l;
358 bpri = l->l_priority;
359 }
360
361 if (bl != NULL) {
362 mutex_enter(&sched_mutex);
363 swapin = sleepq_remove(sq, bl);
364 mutex_exit(&sched_mutex);
365 }
366
367 mutex_exit(sq->sq_mutex);
368
369 if (swapin)
370 wakeup(&proc0);
371 }
372
373 /*
374 * sleepq_wakeall:
375 *
376 * Wake all LWPs blocked on a single wait channel.
377 */
378 void
379 sleepq_wakeall(sleepq_t *sq, wchan_t wchan, u_int expected)
380 {
381 struct lwp *l, *next;
382 int swapin = 0;
383
384 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
385
386 mutex_enter(&sched_mutex);
387 for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
388 next = TAILQ_NEXT(l, l_sleepq);
389 if (l->l_wchan != wchan)
390 continue;
391 swapin |= sleepq_remove(sq, l);
392 if (--expected == 0)
393 break;
394 }
395 mutex_exit(&sched_mutex);
396
397 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
398 mutex_exit(sq->sq_mutex);
399
400 /*
401 * If there are newly awakend threads that need to be swapped in,
402 * then kick the swapper into action.
403 */
404 if (swapin)
405 wakeup(&proc0);
406 }
407
408 /*
409 * sleepq_unsleep:
410 *
411 * Remove an LWP from its sleep queue and set it runnable again.
412 * sleepq_unsleep() is called with the LWP's mutex held, and will
413 * always release it.
414 */
415 void
416 sleepq_unsleep(struct lwp *l)
417 {
418 sleepq_t *sq;
419 int swapin;
420
421 sq = &sleeptab[SLEEPTAB_HASH(l->l_wchan)];
422 KASSERT(l->l_wchan != NULL);
423 KASSERT(l->l_mutex == sq->sq_mutex);
424
425 mutex_enter(&sched_mutex);
426 swapin = sleepq_remove(sq, l);
427 mutex_exit(&sched_mutex);
428
429 LOCK_ASSERT(mutex_owned(sq->sq_mutex));
430 mutex_exit(sq->sq_mutex);
431
432 if (swapin)
433 wakeup(&proc0);
434 }
435
436 /*
437 * sleepq_timeout:
438 *
439 * Entered via the callout(9) subsystem to time out an LWP that is on a
440 * sleep queue.
441 */
442 void
443 sleepq_timeout(void *arg)
444 {
445 struct lwp *l = arg;
446
447 /*
448 * Lock the LWP. Assuming it's still on the sleep queue, its
449 * current mutex will also be the sleep queue mutex.
450 */
451 lwp_lock(l);
452
453 if (l->l_wchan == NULL) {
454 /* Somebody beat us to it. */
455 lwp_unlock(l);
456 return;
457 }
458
459 sleepq_unsleep(arg);
460 }
461
462 /*
463 * sleepq_sigtoerror:
464 *
465 * Given a signal number, interpret and return an error code.
466 */
467 int
468 sleepq_sigtoerror(struct lwp *l, int sig)
469 {
470 struct proc *p;
471 int error;
472
473 /*
474 * If this sleep was canceled, don't let the syscall restart.
475 */
476 p = l->l_proc;
477 mutex_enter(&p->p_smutex);
478 if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
479 error = EINTR;
480 else
481 error = ERESTART;
482 mutex_exit(&p->p_smutex);
483
484 return error;
485 }
486
487 /*
488 * sleepq_abort:
489 *
490 * After a panic or during autoconfiguration, lower the interrupt
491 * priority level to give pending interrupts a chance to run, and
492 * then return. Called if sleepq_dontsleep() returns non-zero, and
493 * always returns zero.
494 */
495 int
496 sleepq_abort(kmutex_t *mtx, int unlock)
497 {
498 extern int safepri;
499 int s;
500
501 s = splhigh();
502 splx(safepri);
503 splx(s);
504 if (mtx != NULL && unlock != 0)
505 mutex_exit(mtx);
506
507 return 0;
508 }
509