kern_sleepq.c revision 1.52 1 /* $NetBSD: kern_sleepq.c,v 1.52 2019/11/21 18:56:55 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008, 2009, 2019 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Sleep queue implementation, used by turnstiles and general sleep/wakeup
34 * interfaces.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.52 2019/11/21 18:56:55 ad Exp $");
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/cpu.h>
43 #include <sys/intr.h>
44 #include <sys/pool.h>
45 #include <sys/proc.h>
46 #include <sys/resourcevar.h>
47 #include <sys/sched.h>
48 #include <sys/systm.h>
49 #include <sys/sleepq.h>
50 #include <sys/ktrace.h>
51
52 /*
53 * for sleepq_abort:
54 * During autoconfiguration or after a panic, a sleep will simply lower the
55 * priority briefly to allow interrupts, then return. The priority to be
56 * used (IPL_SAFEPRI) is machine-dependent, thus this value is initialized and
57 * maintained in the machine-dependent layers. This priority will typically
58 * be 0, or the lowest priority that is safe for use on the interrupt stack;
59 * it can be made higher to block network software interrupts after panics.
60 */
61 #ifndef IPL_SAFEPRI
62 #define IPL_SAFEPRI 0
63 #endif
64
65 static int sleepq_sigtoerror(lwp_t *, int);
66
67 /* General purpose sleep table, used by mtsleep() and condition variables. */
68 sleeptab_t sleeptab __cacheline_aligned;
69 kmutex_t *sleepq_locks[SLEEPTAB_HASH_SIZE] __read_mostly;
70
71 /*
72 * sleeptab_init:
73 *
74 * Initialize a sleep table.
75 */
76 void
77 sleeptab_init(sleeptab_t *st)
78 {
79 int i;
80
81 for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
82 sleepq_locks[i] = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
83 sleepq_init(&st->st_queue[i]);
84 }
85 }
86
87 /*
88 * sleepq_init:
89 *
90 * Prepare a sleep queue for use.
91 */
92 void
93 sleepq_init(sleepq_t *sq)
94 {
95
96 TAILQ_INIT(sq);
97 }
98
99 /*
100 * sleepq_remove:
101 *
102 * Remove an LWP from a sleep queue and wake it up.
103 */
104 void
105 sleepq_remove(sleepq_t *sq, lwp_t *l)
106 {
107 struct schedstate_percpu *spc;
108 struct cpu_info *ci;
109
110 KASSERT(lwp_locked(l, NULL));
111
112 TAILQ_REMOVE(sq, l, l_sleepchain);
113 l->l_syncobj = &sched_syncobj;
114 l->l_wchan = NULL;
115 l->l_sleepq = NULL;
116 l->l_flag &= ~LW_SINTR;
117
118 ci = l->l_cpu;
119 spc = &ci->ci_schedstate;
120
121 /*
122 * If not sleeping, the LWP must have been suspended. Let whoever
123 * holds it stopped set it running again.
124 */
125 if (l->l_stat != LSSLEEP) {
126 KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
127 lwp_setlock(l, spc->spc_lwplock);
128 return;
129 }
130
131 /*
132 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
133 * about to call mi_switch(), in which case it will yield.
134 */
135 if ((l->l_pflag & LP_RUNNING) != 0) {
136 l->l_stat = LSONPROC;
137 l->l_slptime = 0;
138 lwp_setlock(l, spc->spc_lwplock);
139 return;
140 }
141
142 /* Update sleep time delta, call the wake-up handler of scheduler */
143 l->l_slpticksum += (hardclock_ticks - l->l_slpticks);
144 sched_wakeup(l);
145
146 /* Look for a CPU to wake up */
147 l->l_cpu = sched_takecpu(l);
148 ci = l->l_cpu;
149 spc = &ci->ci_schedstate;
150
151 /*
152 * Set it running.
153 */
154 spc_lock(ci);
155 lwp_setlock(l, spc->spc_mutex);
156 sched_setrunnable(l);
157 l->l_stat = LSRUN;
158 l->l_slptime = 0;
159 sched_enqueue(l, false);
160 spc_unlock(ci);
161 }
162
163 /*
164 * sleepq_insert:
165 *
166 * Insert an LWP into the sleep queue, optionally sorting by priority.
167 */
168 static void
169 sleepq_insert(sleepq_t *sq, lwp_t *l, syncobj_t *sobj)
170 {
171
172 if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
173 lwp_t *l2;
174 const int pri = lwp_eprio(l);
175
176 TAILQ_FOREACH(l2, sq, l_sleepchain) {
177 if (lwp_eprio(l2) < pri) {
178 TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
179 return;
180 }
181 }
182 }
183
184 if ((sobj->sobj_flag & SOBJ_SLEEPQ_LIFO) != 0)
185 TAILQ_INSERT_HEAD(sq, l, l_sleepchain);
186 else
187 TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
188 }
189
190 /*
191 * sleepq_enqueue:
192 *
193 * Enter an LWP into the sleep queue and prepare for sleep. The sleep
194 * queue must already be locked, and any interlock (such as the kernel
195 * lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
196 */
197 void
198 sleepq_enqueue(sleepq_t *sq, wchan_t wchan, const char *wmesg, syncobj_t *sobj)
199 {
200 lwp_t *l = curlwp;
201
202 KASSERT(lwp_locked(l, NULL));
203 KASSERT(l->l_stat == LSONPROC);
204 KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
205
206 l->l_syncobj = sobj;
207 l->l_wchan = wchan;
208 l->l_sleepq = sq;
209 l->l_wmesg = wmesg;
210 l->l_slptime = 0;
211 l->l_stat = LSSLEEP;
212 l->l_sleeperr = 0;
213
214 sleepq_insert(sq, l, sobj);
215
216 /* Save the time when thread has slept */
217 l->l_slpticks = hardclock_ticks;
218 sched_slept(l);
219 }
220
221 /*
222 * sleepq_block:
223 *
224 * After any intermediate step such as releasing an interlock, switch.
225 * sleepq_block() may return early under exceptional conditions, for
226 * example if the LWP's containing process is exiting.
227 *
228 * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
229 */
230 int
231 sleepq_block(int timo, bool catch_p)
232 {
233 int error = 0, sig;
234 struct proc *p;
235 lwp_t *l = curlwp;
236 bool early = false;
237 int biglocks = l->l_biglocks;
238
239 ktrcsw(1, 0);
240
241 /*
242 * If sleeping interruptably, check for pending signals, exits or
243 * core dump events.
244 */
245 if (catch_p) {
246 l->l_flag |= LW_SINTR;
247 if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
248 l->l_flag &= ~LW_CANCELLED;
249 error = EINTR;
250 early = true;
251 } else if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))
252 early = true;
253 }
254
255 if (early) {
256 /* lwp_unsleep() will release the lock */
257 lwp_unsleep(l, true);
258 } else {
259 if (timo) {
260 callout_schedule(&l->l_timeout_ch, timo);
261 }
262 mi_switch(l);
263
264 /* The LWP and sleep queue are now unlocked. */
265 if (timo) {
266 /*
267 * Even if the callout appears to have fired, we
268 * need to stop it in order to synchronise with
269 * other CPUs. It's important that we do this in
270 * this LWP's context, and not during wakeup, in
271 * order to keep the callout & its cache lines
272 * co-located on the CPU with the LWP.
273 */
274 if (callout_halt(&l->l_timeout_ch, NULL))
275 error = EWOULDBLOCK;
276 }
277 }
278
279 if (catch_p && error == 0) {
280 p = l->l_proc;
281 if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
282 error = EINTR;
283 else if ((l->l_flag & LW_PENDSIG) != 0) {
284 /*
285 * Acquiring p_lock may cause us to recurse
286 * through the sleep path and back into this
287 * routine, but is safe because LWPs sleeping
288 * on locks are non-interruptable. We will
289 * not recurse again.
290 */
291 mutex_enter(p->p_lock);
292 if (((sig = sigispending(l, 0)) != 0 &&
293 (sigprop[sig] & SA_STOP) == 0) ||
294 (sig = issignal(l)) != 0)
295 error = sleepq_sigtoerror(l, sig);
296 mutex_exit(p->p_lock);
297 }
298 }
299
300 ktrcsw(0, 0);
301 if (__predict_false(biglocks != 0)) {
302 KERNEL_LOCK(biglocks, NULL);
303 }
304 return error;
305 }
306
307 /*
308 * sleepq_wake:
309 *
310 * Wake zero or more LWPs blocked on a single wait channel.
311 */
312 void
313 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
314 {
315 lwp_t *l, *next;
316
317 KASSERT(mutex_owned(mp));
318
319 for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
320 KASSERT(l->l_sleepq == sq);
321 KASSERT(l->l_mutex == mp);
322 next = TAILQ_NEXT(l, l_sleepchain);
323 if (l->l_wchan != wchan)
324 continue;
325 sleepq_remove(sq, l);
326 if (--expected == 0)
327 break;
328 }
329
330 mutex_spin_exit(mp);
331 }
332
333 /*
334 * sleepq_unsleep:
335 *
336 * Remove an LWP from its sleep queue and set it runnable again.
337 * sleepq_unsleep() is called with the LWP's mutex held, and will
338 * release it if "unlock" is true.
339 */
340 void
341 sleepq_unsleep(lwp_t *l, bool unlock)
342 {
343 sleepq_t *sq = l->l_sleepq;
344 kmutex_t *mp = l->l_mutex;
345
346 KASSERT(lwp_locked(l, mp));
347 KASSERT(l->l_wchan != NULL);
348
349 sleepq_remove(sq, l);
350 if (unlock) {
351 mutex_spin_exit(mp);
352 }
353 }
354
355 /*
356 * sleepq_timeout:
357 *
358 * Entered via the callout(9) subsystem to time out an LWP that is on a
359 * sleep queue.
360 */
361 void
362 sleepq_timeout(void *arg)
363 {
364 lwp_t *l = arg;
365
366 /*
367 * Lock the LWP. Assuming it's still on the sleep queue, its
368 * current mutex will also be the sleep queue mutex.
369 */
370 lwp_lock(l);
371
372 if (l->l_wchan == NULL) {
373 /* Somebody beat us to it. */
374 lwp_unlock(l);
375 return;
376 }
377
378 lwp_unsleep(l, true);
379 }
380
381 /*
382 * sleepq_sigtoerror:
383 *
384 * Given a signal number, interpret and return an error code.
385 */
386 static int
387 sleepq_sigtoerror(lwp_t *l, int sig)
388 {
389 struct proc *p = l->l_proc;
390 int error;
391
392 KASSERT(mutex_owned(p->p_lock));
393
394 /*
395 * If this sleep was canceled, don't let the syscall restart.
396 */
397 if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
398 error = EINTR;
399 else
400 error = ERESTART;
401
402 return error;
403 }
404
405 /*
406 * sleepq_abort:
407 *
408 * After a panic or during autoconfiguration, lower the interrupt
409 * priority level to give pending interrupts a chance to run, and
410 * then return. Called if sleepq_dontsleep() returns non-zero, and
411 * always returns zero.
412 */
413 int
414 sleepq_abort(kmutex_t *mtx, int unlock)
415 {
416 int s;
417
418 s = splhigh();
419 splx(IPL_SAFEPRI);
420 splx(s);
421 if (mtx != NULL && unlock != 0)
422 mutex_exit(mtx);
423
424 return 0;
425 }
426
427 /*
428 * sleepq_reinsert:
429 *
430 * Move the possition of the lwp in the sleep queue after a possible
431 * change of the lwp's effective priority.
432 */
433 static void
434 sleepq_reinsert(sleepq_t *sq, lwp_t *l)
435 {
436
437 KASSERT(l->l_sleepq == sq);
438 if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0) {
439 return;
440 }
441
442 /*
443 * Don't let the sleep queue become empty, even briefly.
444 * cv_signal() and cv_broadcast() inspect it without the
445 * sleep queue lock held and need to see a non-empty queue
446 * head if there are waiters.
447 */
448 if (TAILQ_FIRST(sq) == l && TAILQ_NEXT(l, l_sleepchain) == NULL) {
449 return;
450 }
451 TAILQ_REMOVE(sq, l, l_sleepchain);
452 sleepq_insert(sq, l, l->l_syncobj);
453 }
454
455 /*
456 * sleepq_changepri:
457 *
458 * Adjust the priority of an LWP residing on a sleepq.
459 */
460 void
461 sleepq_changepri(lwp_t *l, pri_t pri)
462 {
463 sleepq_t *sq = l->l_sleepq;
464
465 KASSERT(lwp_locked(l, NULL));
466
467 l->l_priority = pri;
468 sleepq_reinsert(sq, l);
469 }
470
471 /*
472 * sleepq_changepri:
473 *
474 * Adjust the lended priority of an LWP residing on a sleepq.
475 */
476 void
477 sleepq_lendpri(lwp_t *l, pri_t pri)
478 {
479 sleepq_t *sq = l->l_sleepq;
480
481 KASSERT(lwp_locked(l, NULL));
482
483 l->l_inheritedprio = pri;
484 l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
485 sleepq_reinsert(sq, l);
486 }
487