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