kern_sleepq.c revision 1.58 1 /* $NetBSD: kern_sleepq.c,v 1.58 2020/01/12 13:08:32 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.58 2020/01/12 13:08:32 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 sleepqlock_t sleepq_locks[SLEEPTAB_HASH_SIZE] __cacheline_aligned;
70
71 /*
72 * sleeptab_init:
73 *
74 * Initialize a sleep table.
75 */
76 void
77 sleeptab_init(sleeptab_t *st)
78 {
79 static bool again;
80 int i;
81
82 for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
83 if (!again) {
84 mutex_init(&sleepq_locks[i].lock, MUTEX_DEFAULT,
85 IPL_SCHED);
86 }
87 sleepq_init(&st->st_queue[i]);
88 }
89 again = true;
90 }
91
92 /*
93 * sleepq_init:
94 *
95 * Prepare a sleep queue for use.
96 */
97 void
98 sleepq_init(sleepq_t *sq)
99 {
100
101 TAILQ_INIT(sq);
102 }
103
104 /*
105 * sleepq_remove:
106 *
107 * Remove an LWP from a sleep queue and wake it up.
108 */
109 void
110 sleepq_remove(sleepq_t *sq, lwp_t *l)
111 {
112 struct schedstate_percpu *spc;
113 struct cpu_info *ci;
114
115 KASSERT(lwp_locked(l, NULL));
116
117 TAILQ_REMOVE(sq, l, l_sleepchain);
118 l->l_syncobj = &sched_syncobj;
119 l->l_wchan = NULL;
120 l->l_sleepq = NULL;
121 l->l_flag &= ~LW_SINTR;
122
123 ci = l->l_cpu;
124 spc = &ci->ci_schedstate;
125
126 /*
127 * If not sleeping, the LWP must have been suspended. Let whoever
128 * holds it stopped set it running again.
129 */
130 if (l->l_stat != LSSLEEP) {
131 KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
132 lwp_setlock(l, spc->spc_lwplock);
133 return;
134 }
135
136 /*
137 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
138 * about to call mi_switch(), in which case it will yield.
139 */
140 if ((l->l_flag & LW_RUNNING) != 0) {
141 l->l_stat = LSONPROC;
142 l->l_slptime = 0;
143 lwp_setlock(l, spc->spc_lwplock);
144 return;
145 }
146
147 /* Update sleep time delta, call the wake-up handler of scheduler */
148 l->l_slpticksum += (hardclock_ticks - l->l_slpticks);
149 sched_wakeup(l);
150
151 /* Look for a CPU to wake up */
152 l->l_cpu = sched_takecpu(l);
153 ci = l->l_cpu;
154 spc = &ci->ci_schedstate;
155
156 /*
157 * Set it running.
158 */
159 spc_lock(ci);
160 lwp_setlock(l, spc->spc_mutex);
161 sched_setrunnable(l);
162 l->l_stat = LSRUN;
163 l->l_slptime = 0;
164 sched_enqueue(l);
165 sched_resched_lwp(l, true);
166 /* LWP & SPC now unlocked, but we still hold sleep queue lock. */
167 }
168
169 /*
170 * sleepq_insert:
171 *
172 * Insert an LWP into the sleep queue, optionally sorting by priority.
173 */
174 static void
175 sleepq_insert(sleepq_t *sq, lwp_t *l, syncobj_t *sobj)
176 {
177
178 if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
179 lwp_t *l2;
180 const int pri = lwp_eprio(l);
181
182 TAILQ_FOREACH(l2, sq, l_sleepchain) {
183 if (lwp_eprio(l2) < pri) {
184 TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
185 return;
186 }
187 }
188 }
189
190 if ((sobj->sobj_flag & SOBJ_SLEEPQ_LIFO) != 0)
191 TAILQ_INSERT_HEAD(sq, l, l_sleepchain);
192 else
193 TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
194 }
195
196 /*
197 * sleepq_enqueue:
198 *
199 * Enter an LWP into the sleep queue and prepare for sleep. The sleep
200 * queue must already be locked, and any interlock (such as the kernel
201 * lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
202 */
203 void
204 sleepq_enqueue(sleepq_t *sq, wchan_t wchan, const char *wmesg, syncobj_t *sobj)
205 {
206 lwp_t *l = curlwp;
207
208 KASSERT(lwp_locked(l, NULL));
209 KASSERT(l->l_stat == LSONPROC);
210 KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
211
212 l->l_syncobj = sobj;
213 l->l_wchan = wchan;
214 l->l_sleepq = sq;
215 l->l_wmesg = wmesg;
216 l->l_slptime = 0;
217 l->l_stat = LSSLEEP;
218
219 sleepq_insert(sq, l, sobj);
220
221 /* Save the time when thread has slept */
222 l->l_slpticks = hardclock_ticks;
223 sched_slept(l);
224 }
225
226 /*
227 * sleepq_block:
228 *
229 * After any intermediate step such as releasing an interlock, switch.
230 * sleepq_block() may return early under exceptional conditions, for
231 * example if the LWP's containing process is exiting.
232 *
233 * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
234 */
235 int
236 sleepq_block(int timo, bool catch_p)
237 {
238 int error = 0, sig;
239 struct proc *p;
240 lwp_t *l = curlwp;
241 bool early = false;
242 int biglocks = l->l_biglocks;
243
244 ktrcsw(1, 0);
245
246 /*
247 * If sleeping interruptably, check for pending signals, exits or
248 * core dump events.
249 */
250 if (catch_p) {
251 l->l_flag |= LW_SINTR;
252 if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
253 l->l_flag &= ~LW_CANCELLED;
254 error = EINTR;
255 early = true;
256 } else if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))
257 early = true;
258 }
259
260 if (early) {
261 /* lwp_unsleep() will release the lock */
262 lwp_unsleep(l, true);
263 } else {
264 if (timo) {
265 callout_schedule(&l->l_timeout_ch, timo);
266 }
267 spc_lock(l->l_cpu);
268 mi_switch(l);
269
270 /* The LWP and sleep queue are now unlocked. */
271 if (timo) {
272 /*
273 * Even if the callout appears to have fired, we
274 * need to stop it in order to synchronise with
275 * other CPUs. It's important that we do this in
276 * this LWP's context, and not during wakeup, in
277 * order to keep the callout & its cache lines
278 * co-located on the CPU with the LWP.
279 */
280 if (callout_halt(&l->l_timeout_ch, NULL))
281 error = EWOULDBLOCK;
282 }
283 }
284
285 if (catch_p && error == 0) {
286 p = l->l_proc;
287 if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
288 error = EINTR;
289 else if ((l->l_flag & LW_PENDSIG) != 0) {
290 /*
291 * Acquiring p_lock may cause us to recurse
292 * through the sleep path and back into this
293 * routine, but is safe because LWPs sleeping
294 * on locks are non-interruptable. We will
295 * not recurse again.
296 */
297 mutex_enter(p->p_lock);
298 if (((sig = sigispending(l, 0)) != 0 &&
299 (sigprop[sig] & SA_STOP) == 0) ||
300 (sig = issignal(l)) != 0)
301 error = sleepq_sigtoerror(l, sig);
302 mutex_exit(p->p_lock);
303 }
304 }
305
306 ktrcsw(0, 0);
307 if (__predict_false(biglocks != 0)) {
308 KERNEL_LOCK(biglocks, NULL);
309 }
310 return error;
311 }
312
313 /*
314 * sleepq_wake:
315 *
316 * Wake zero or more LWPs blocked on a single wait channel.
317 */
318 void
319 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
320 {
321 lwp_t *l, *next;
322
323 KASSERT(mutex_owned(mp));
324
325 for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
326 KASSERT(l->l_sleepq == sq);
327 KASSERT(l->l_mutex == mp);
328 next = TAILQ_NEXT(l, l_sleepchain);
329 if (l->l_wchan != wchan)
330 continue;
331 sleepq_remove(sq, l);
332 if (--expected == 0)
333 break;
334 }
335
336 mutex_spin_exit(mp);
337 }
338
339 /*
340 * sleepq_unsleep:
341 *
342 * Remove an LWP from its sleep queue and set it runnable again.
343 * sleepq_unsleep() is called with the LWP's mutex held, and will
344 * release it if "unlock" is true.
345 */
346 void
347 sleepq_unsleep(lwp_t *l, bool unlock)
348 {
349 sleepq_t *sq = l->l_sleepq;
350 kmutex_t *mp = l->l_mutex;
351
352 KASSERT(lwp_locked(l, mp));
353 KASSERT(l->l_wchan != NULL);
354
355 sleepq_remove(sq, l);
356 if (unlock) {
357 mutex_spin_exit(mp);
358 }
359 }
360
361 /*
362 * sleepq_timeout:
363 *
364 * Entered via the callout(9) subsystem to time out an LWP that is on a
365 * sleep queue.
366 */
367 void
368 sleepq_timeout(void *arg)
369 {
370 lwp_t *l = arg;
371
372 /*
373 * Lock the LWP. Assuming it's still on the sleep queue, its
374 * current mutex will also be the sleep queue mutex.
375 */
376 lwp_lock(l);
377
378 if (l->l_wchan == NULL) {
379 /* Somebody beat us to it. */
380 lwp_unlock(l);
381 return;
382 }
383
384 lwp_unsleep(l, true);
385 }
386
387 /*
388 * sleepq_sigtoerror:
389 *
390 * Given a signal number, interpret and return an error code.
391 */
392 static int
393 sleepq_sigtoerror(lwp_t *l, int sig)
394 {
395 struct proc *p = l->l_proc;
396 int error;
397
398 KASSERT(mutex_owned(p->p_lock));
399
400 /*
401 * If this sleep was canceled, don't let the syscall restart.
402 */
403 if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
404 error = EINTR;
405 else
406 error = ERESTART;
407
408 return error;
409 }
410
411 /*
412 * sleepq_abort:
413 *
414 * After a panic or during autoconfiguration, lower the interrupt
415 * priority level to give pending interrupts a chance to run, and
416 * then return. Called if sleepq_dontsleep() returns non-zero, and
417 * always returns zero.
418 */
419 int
420 sleepq_abort(kmutex_t *mtx, int unlock)
421 {
422 int s;
423
424 s = splhigh();
425 splx(IPL_SAFEPRI);
426 splx(s);
427 if (mtx != NULL && unlock != 0)
428 mutex_exit(mtx);
429
430 return 0;
431 }
432
433 /*
434 * sleepq_reinsert:
435 *
436 * Move the possition of the lwp in the sleep queue after a possible
437 * change of the lwp's effective priority.
438 */
439 static void
440 sleepq_reinsert(sleepq_t *sq, lwp_t *l)
441 {
442
443 KASSERT(l->l_sleepq == sq);
444 if ((l->l_syncobj->sobj_flag & SOBJ_SLEEPQ_SORTED) == 0) {
445 return;
446 }
447
448 /*
449 * Don't let the sleep queue become empty, even briefly.
450 * cv_signal() and cv_broadcast() inspect it without the
451 * sleep queue lock held and need to see a non-empty queue
452 * head if there are waiters.
453 */
454 if (TAILQ_FIRST(sq) == l && TAILQ_NEXT(l, l_sleepchain) == NULL) {
455 return;
456 }
457 TAILQ_REMOVE(sq, l, l_sleepchain);
458 sleepq_insert(sq, l, l->l_syncobj);
459 }
460
461 /*
462 * sleepq_changepri:
463 *
464 * Adjust the priority of an LWP residing on a sleepq.
465 */
466 void
467 sleepq_changepri(lwp_t *l, pri_t pri)
468 {
469 sleepq_t *sq = l->l_sleepq;
470
471 KASSERT(lwp_locked(l, NULL));
472
473 l->l_priority = pri;
474 sleepq_reinsert(sq, l);
475 }
476
477 /*
478 * sleepq_changepri:
479 *
480 * Adjust the lended priority of an LWP residing on a sleepq.
481 */
482 void
483 sleepq_lendpri(lwp_t *l, pri_t pri)
484 {
485 sleepq_t *sq = l->l_sleepq;
486
487 KASSERT(lwp_locked(l, NULL));
488
489 l->l_inheritedprio = pri;
490 l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
491 sleepq_reinsert(sq, l);
492 }
493