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