kern_synch.c revision 1.337 1 /* $NetBSD: kern_synch.c,v 1.337 2020/01/22 13:19:33 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2004, 2006, 2007, 2008, 2009, 2019
5 * The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center, by Charles M. Hannum, Andrew Doran and
11 * Daniel Sieger.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1982, 1986, 1990, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 * (c) UNIX System Laboratories, Inc.
39 * All or some portions of this file are derived from material licensed
40 * to the University of California by American Telephone and Telegraph
41 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
42 * the permission of UNIX System Laboratories, Inc.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.337 2020/01/22 13:19:33 ad Exp $");
73
74 #include "opt_kstack.h"
75 #include "opt_dtrace.h"
76
77 #define __MUTEX_PRIVATE
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/kernel.h>
83 #include <sys/cpu.h>
84 #include <sys/pserialize.h>
85 #include <sys/resourcevar.h>
86 #include <sys/sched.h>
87 #include <sys/syscall_stats.h>
88 #include <sys/sleepq.h>
89 #include <sys/lockdebug.h>
90 #include <sys/evcnt.h>
91 #include <sys/intr.h>
92 #include <sys/lwpctl.h>
93 #include <sys/atomic.h>
94 #include <sys/syslog.h>
95
96 #include <uvm/uvm_extern.h>
97
98 #include <dev/lockstat.h>
99
100 #include <sys/dtrace_bsd.h>
101 int dtrace_vtime_active=0;
102 dtrace_vtime_switch_func_t dtrace_vtime_switch_func;
103
104 static void sched_unsleep(struct lwp *, bool);
105 static void sched_changepri(struct lwp *, pri_t);
106 static void sched_lendpri(struct lwp *, pri_t);
107
108 syncobj_t sleep_syncobj = {
109 .sobj_flag = SOBJ_SLEEPQ_SORTED,
110 .sobj_unsleep = sleepq_unsleep,
111 .sobj_changepri = sleepq_changepri,
112 .sobj_lendpri = sleepq_lendpri,
113 .sobj_owner = syncobj_noowner,
114 };
115
116 syncobj_t sched_syncobj = {
117 .sobj_flag = SOBJ_SLEEPQ_SORTED,
118 .sobj_unsleep = sched_unsleep,
119 .sobj_changepri = sched_changepri,
120 .sobj_lendpri = sched_lendpri,
121 .sobj_owner = syncobj_noowner,
122 };
123
124 /* "Lightning bolt": once a second sleep address. */
125 kcondvar_t lbolt __cacheline_aligned;
126
127 u_int sched_pstats_ticks __cacheline_aligned;
128
129 /* Preemption event counters. */
130 static struct evcnt kpreempt_ev_crit __cacheline_aligned;
131 static struct evcnt kpreempt_ev_klock __cacheline_aligned;
132 static struct evcnt kpreempt_ev_immed __cacheline_aligned;
133
134 void
135 synch_init(void)
136 {
137
138 cv_init(&lbolt, "lbolt");
139
140 evcnt_attach_dynamic(&kpreempt_ev_crit, EVCNT_TYPE_MISC, NULL,
141 "kpreempt", "defer: critical section");
142 evcnt_attach_dynamic(&kpreempt_ev_klock, EVCNT_TYPE_MISC, NULL,
143 "kpreempt", "defer: kernel_lock");
144 evcnt_attach_dynamic(&kpreempt_ev_immed, EVCNT_TYPE_MISC, NULL,
145 "kpreempt", "immediate");
146 }
147
148 /*
149 * OBSOLETE INTERFACE
150 *
151 * General sleep call. Suspends the current LWP until a wakeup is
152 * performed on the specified identifier. The LWP will then be made
153 * runnable with the specified priority. Sleeps at most timo/hz seconds (0
154 * means no timeout). If pri includes PCATCH flag, signals are checked
155 * before and after sleeping, else signals are not checked. Returns 0 if
156 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
157 * signal needs to be delivered, ERESTART is returned if the current system
158 * call should be restarted if possible, and EINTR is returned if the system
159 * call should be interrupted by the signal (return EINTR).
160 */
161 int
162 tsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo)
163 {
164 struct lwp *l = curlwp;
165 sleepq_t *sq;
166 kmutex_t *mp;
167
168 KASSERT((l->l_pflag & LP_INTR) == 0);
169 KASSERT(ident != &lbolt);
170
171 if (sleepq_dontsleep(l)) {
172 (void)sleepq_abort(NULL, 0);
173 return 0;
174 }
175
176 l->l_kpriority = true;
177 sq = sleeptab_lookup(&sleeptab, ident, &mp);
178 sleepq_enter(sq, l, mp);
179 sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj);
180 return sleepq_block(timo, priority & PCATCH);
181 }
182
183 int
184 mtsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
185 kmutex_t *mtx)
186 {
187 struct lwp *l = curlwp;
188 sleepq_t *sq;
189 kmutex_t *mp;
190 int error;
191
192 KASSERT((l->l_pflag & LP_INTR) == 0);
193 KASSERT(ident != &lbolt);
194
195 if (sleepq_dontsleep(l)) {
196 (void)sleepq_abort(mtx, (priority & PNORELOCK) != 0);
197 return 0;
198 }
199
200 l->l_kpriority = true;
201 sq = sleeptab_lookup(&sleeptab, ident, &mp);
202 sleepq_enter(sq, l, mp);
203 sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj);
204 mutex_exit(mtx);
205 error = sleepq_block(timo, priority & PCATCH);
206
207 if ((priority & PNORELOCK) == 0)
208 mutex_enter(mtx);
209
210 return error;
211 }
212
213 /*
214 * General sleep call for situations where a wake-up is not expected.
215 */
216 int
217 kpause(const char *wmesg, bool intr, int timo, kmutex_t *mtx)
218 {
219 struct lwp *l = curlwp;
220 kmutex_t *mp;
221 sleepq_t *sq;
222 int error;
223
224 KASSERT(!(timo == 0 && intr == false));
225
226 if (sleepq_dontsleep(l))
227 return sleepq_abort(NULL, 0);
228
229 if (mtx != NULL)
230 mutex_exit(mtx);
231 l->l_kpriority = true;
232 sq = sleeptab_lookup(&sleeptab, l, &mp);
233 sleepq_enter(sq, l, mp);
234 sleepq_enqueue(sq, l, wmesg, &sleep_syncobj);
235 error = sleepq_block(timo, intr);
236 if (mtx != NULL)
237 mutex_enter(mtx);
238
239 return error;
240 }
241
242 /*
243 * OBSOLETE INTERFACE
244 *
245 * Make all LWPs sleeping on the specified identifier runnable.
246 */
247 void
248 wakeup(wchan_t ident)
249 {
250 sleepq_t *sq;
251 kmutex_t *mp;
252
253 if (__predict_false(cold))
254 return;
255
256 sq = sleeptab_lookup(&sleeptab, ident, &mp);
257 sleepq_wake(sq, ident, (u_int)-1, mp);
258 }
259
260 /*
261 * General yield call. Puts the current LWP back on its run queue and
262 * performs a voluntary context switch. Should only be called when the
263 * current LWP explicitly requests it (eg sched_yield(2)).
264 */
265 void
266 yield(void)
267 {
268 struct lwp *l = curlwp;
269
270 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
271 lwp_lock(l);
272
273 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
274 KASSERT(l->l_stat == LSONPROC);
275
276 /* Voluntary - ditch kpriority boost. */
277 l->l_kpriority = false;
278 spc_lock(l->l_cpu);
279 mi_switch(l);
280 KERNEL_LOCK(l->l_biglocks, l);
281 }
282
283 /*
284 * General preemption call. Puts the current LWP back on its run queue
285 * and performs an involuntary context switch.
286 */
287 void
288 preempt(void)
289 {
290 struct lwp *l = curlwp;
291
292 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
293 lwp_lock(l);
294
295 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
296 KASSERT(l->l_stat == LSONPROC);
297
298 /* Involuntary - keep kpriority boost. */
299 l->l_pflag |= LP_PREEMPTING;
300 spc_lock(l->l_cpu);
301 mi_switch(l);
302 KERNEL_LOCK(l->l_biglocks, l);
303 }
304
305 /*
306 * Handle a request made by another agent to preempt the current LWP
307 * in-kernel. Usually called when l_dopreempt may be non-zero.
308 *
309 * Character addresses for lockstat only.
310 */
311 static char kpreempt_is_disabled;
312 static char kernel_lock_held;
313 static char is_softint_lwp;
314 static char spl_is_raised;
315
316 bool
317 kpreempt(uintptr_t where)
318 {
319 uintptr_t failed;
320 lwp_t *l;
321 int s, dop, lsflag;
322
323 l = curlwp;
324 failed = 0;
325 while ((dop = l->l_dopreempt) != 0) {
326 if (l->l_stat != LSONPROC) {
327 /*
328 * About to block (or die), let it happen.
329 * Doesn't really count as "preemption has
330 * been blocked", since we're going to
331 * context switch.
332 */
333 atomic_swap_uint(&l->l_dopreempt, 0);
334 return true;
335 }
336 if (__predict_false((l->l_flag & LW_IDLE) != 0)) {
337 /* Can't preempt idle loop, don't count as failure. */
338 atomic_swap_uint(&l->l_dopreempt, 0);
339 return true;
340 }
341 if (__predict_false(l->l_nopreempt != 0)) {
342 /* LWP holds preemption disabled, explicitly. */
343 if ((dop & DOPREEMPT_COUNTED) == 0) {
344 kpreempt_ev_crit.ev_count++;
345 }
346 failed = (uintptr_t)&kpreempt_is_disabled;
347 break;
348 }
349 if (__predict_false((l->l_pflag & LP_INTR) != 0)) {
350 /* Can't preempt soft interrupts yet. */
351 atomic_swap_uint(&l->l_dopreempt, 0);
352 failed = (uintptr_t)&is_softint_lwp;
353 break;
354 }
355 s = splsched();
356 if (__predict_false(l->l_blcnt != 0)) {
357 /* Hold or want kernel_lock, code is not MT safe. */
358 splx(s);
359 if ((dop & DOPREEMPT_COUNTED) == 0) {
360 kpreempt_ev_klock.ev_count++;
361 }
362 failed = (uintptr_t)&kernel_lock_held;
363 break;
364 }
365 if (__predict_false(!cpu_kpreempt_enter(where, s))) {
366 /*
367 * It may be that the IPL is too high.
368 * kpreempt_enter() can schedule an
369 * interrupt to retry later.
370 */
371 splx(s);
372 failed = (uintptr_t)&spl_is_raised;
373 break;
374 }
375 /* Do it! */
376 if (__predict_true((dop & DOPREEMPT_COUNTED) == 0)) {
377 kpreempt_ev_immed.ev_count++;
378 }
379 lwp_lock(l);
380 /* Involuntary - keep kpriority boost. */
381 l->l_pflag |= LP_PREEMPTING;
382 spc_lock(l->l_cpu);
383 mi_switch(l);
384 l->l_nopreempt++;
385 splx(s);
386
387 /* Take care of any MD cleanup. */
388 cpu_kpreempt_exit(where);
389 l->l_nopreempt--;
390 }
391
392 if (__predict_true(!failed)) {
393 return false;
394 }
395
396 /* Record preemption failure for reporting via lockstat. */
397 atomic_or_uint(&l->l_dopreempt, DOPREEMPT_COUNTED);
398 lsflag = 0;
399 LOCKSTAT_ENTER(lsflag);
400 if (__predict_false(lsflag)) {
401 if (where == 0) {
402 where = (uintptr_t)__builtin_return_address(0);
403 }
404 /* Preemption is on, might recurse, so make it atomic. */
405 if (atomic_cas_ptr_ni((void *)&l->l_pfailaddr, NULL,
406 (void *)where) == NULL) {
407 LOCKSTAT_START_TIMER(lsflag, l->l_pfailtime);
408 l->l_pfaillock = failed;
409 }
410 }
411 LOCKSTAT_EXIT(lsflag);
412 return true;
413 }
414
415 /*
416 * Return true if preemption is explicitly disabled.
417 */
418 bool
419 kpreempt_disabled(void)
420 {
421 const lwp_t *l = curlwp;
422
423 return l->l_nopreempt != 0 || l->l_stat == LSZOMB ||
424 (l->l_flag & LW_IDLE) != 0 || (l->l_pflag & LP_INTR) != 0 ||
425 cpu_kpreempt_disabled();
426 }
427
428 /*
429 * Disable kernel preemption.
430 */
431 void
432 kpreempt_disable(void)
433 {
434
435 KPREEMPT_DISABLE(curlwp);
436 }
437
438 /*
439 * Reenable kernel preemption.
440 */
441 void
442 kpreempt_enable(void)
443 {
444
445 KPREEMPT_ENABLE(curlwp);
446 }
447
448 /*
449 * Compute the amount of time during which the current lwp was running.
450 *
451 * - update l_rtime unless it's an idle lwp.
452 */
453
454 void
455 updatertime(lwp_t *l, const struct bintime *now)
456 {
457
458 if (__predict_false(l->l_flag & LW_IDLE))
459 return;
460
461 /* rtime += now - stime */
462 bintime_add(&l->l_rtime, now);
463 bintime_sub(&l->l_rtime, &l->l_stime);
464 }
465
466 /*
467 * Select next LWP from the current CPU to run..
468 */
469 static inline lwp_t *
470 nextlwp(struct cpu_info *ci, struct schedstate_percpu *spc)
471 {
472 lwp_t *newl;
473
474 /*
475 * Let sched_nextlwp() select the LWP to run the CPU next.
476 * If no LWP is runnable, select the idle LWP.
477 *
478 * Note that spc_lwplock might not necessary be held, and
479 * new thread would be unlocked after setting the LWP-lock.
480 */
481 newl = sched_nextlwp();
482 if (newl != NULL) {
483 sched_dequeue(newl);
484 KASSERT(lwp_locked(newl, spc->spc_mutex));
485 KASSERT(newl->l_cpu == ci);
486 newl->l_stat = LSONPROC;
487 newl->l_flag |= LW_RUNNING;
488 lwp_setlock(newl, spc->spc_lwplock);
489 spc->spc_flags &= ~(SPCF_SWITCHCLEAR | SPCF_IDLE);
490 } else {
491 newl = ci->ci_data.cpu_idlelwp;
492 newl->l_stat = LSONPROC;
493 newl->l_flag |= LW_RUNNING;
494 spc->spc_flags = (spc->spc_flags & ~SPCF_SWITCHCLEAR) |
495 SPCF_IDLE;
496 }
497
498 /*
499 * Only clear want_resched if there are no pending (slow) software
500 * interrupts. We can do this without an atomic, because no new
501 * LWPs can appear in the queue due to our hold on spc_mutex, and
502 * the update to ci_want_resched will become globally visible before
503 * the release of spc_mutex becomes globally visible.
504 */
505 ci->ci_want_resched = ci->ci_data.cpu_softints;
506 spc->spc_curpriority = lwp_eprio(newl);
507
508 return newl;
509 }
510
511 /*
512 * The machine independent parts of context switch.
513 *
514 * NOTE: l->l_cpu is not changed in this routine, because an LWP never
515 * changes its own l_cpu (that would screw up curcpu on many ports and could
516 * cause all kinds of other evil stuff). l_cpu is always changed by some
517 * other actor, when it's known the LWP is not running (the LW_RUNNING flag
518 * is checked under lock).
519 */
520 void
521 mi_switch(lwp_t *l)
522 {
523 struct cpu_info *ci;
524 struct schedstate_percpu *spc;
525 struct lwp *newl;
526 int oldspl;
527 struct bintime bt;
528 bool returning;
529
530 KASSERT(lwp_locked(l, NULL));
531 KASSERT(kpreempt_disabled());
532 KASSERT(mutex_owned(curcpu()->ci_schedstate.spc_mutex));
533 KASSERTMSG(l->l_blcnt == 0, "kernel_lock leaked");
534
535 kstack_check_magic(l);
536
537 binuptime(&bt);
538
539 KASSERTMSG(l == curlwp, "l %p curlwp %p", l, curlwp);
540 KASSERT((l->l_flag & LW_RUNNING) != 0);
541 KASSERT(l->l_cpu == curcpu() || l->l_stat == LSRUN);
542 ci = curcpu();
543 spc = &ci->ci_schedstate;
544 returning = false;
545 newl = NULL;
546
547 /*
548 * If we have been asked to switch to a specific LWP, then there
549 * is no need to inspect the run queues. If a soft interrupt is
550 * blocking, then return to the interrupted thread without adjusting
551 * VM context or its start time: neither have been changed in order
552 * to take the interrupt.
553 */
554 if (l->l_switchto != NULL) {
555 if ((l->l_pflag & LP_INTR) != 0) {
556 returning = true;
557 softint_block(l);
558 if ((l->l_pflag & LP_TIMEINTR) != 0)
559 updatertime(l, &bt);
560 }
561 newl = l->l_switchto;
562 l->l_switchto = NULL;
563 }
564 #ifndef __HAVE_FAST_SOFTINTS
565 else if (ci->ci_data.cpu_softints != 0) {
566 /* There are pending soft interrupts, so pick one. */
567 newl = softint_picklwp();
568 newl->l_stat = LSONPROC;
569 newl->l_flag |= LW_RUNNING;
570 }
571 #endif /* !__HAVE_FAST_SOFTINTS */
572
573 /*
574 * If on the CPU and we have gotten this far, then we must yield.
575 */
576 if (l->l_stat == LSONPROC && l != newl) {
577 KASSERT(lwp_locked(l, spc->spc_lwplock));
578 KASSERT((l->l_flag & LW_IDLE) == 0);
579 l->l_stat = LSRUN;
580 lwp_setlock(l, spc->spc_mutex);
581 sched_enqueue(l);
582 sched_preempted(l);
583
584 /*
585 * Handle migration. Note that "migrating LWP" may
586 * be reset here, if interrupt/preemption happens
587 * early in idle LWP.
588 */
589 if (l->l_target_cpu != NULL && (l->l_pflag & LP_BOUND) == 0) {
590 KASSERT((l->l_pflag & LP_INTR) == 0);
591 spc->spc_migrating = l;
592 }
593 }
594
595 /* Pick new LWP to run. */
596 if (newl == NULL) {
597 newl = nextlwp(ci, spc);
598 }
599
600 /* Items that must be updated with the CPU locked. */
601 if (!returning) {
602 /* Count time spent in current system call */
603 SYSCALL_TIME_SLEEP(l);
604
605 updatertime(l, &bt);
606
607 /* Update the new LWP's start time. */
608 newl->l_stime = bt;
609
610 /*
611 * ci_curlwp changes when a fast soft interrupt occurs.
612 * We use ci_onproc to keep track of which kernel or
613 * user thread is running 'underneath' the software
614 * interrupt. This is important for time accounting,
615 * itimers and forcing user threads to preempt (aston).
616 */
617 ci->ci_onproc = newl;
618 }
619
620 /*
621 * Preemption related tasks. Must be done holding spc_mutex. Clear
622 * l_dopreempt without an atomic - it's only ever set non-zero by
623 * sched_resched_cpu() which also holds spc_mutex, and only ever
624 * cleared by the LWP itself (us) with atomics when not under lock.
625 */
626 l->l_dopreempt = 0;
627 if (__predict_false(l->l_pfailaddr != 0)) {
628 LOCKSTAT_FLAG(lsflag);
629 LOCKSTAT_ENTER(lsflag);
630 LOCKSTAT_STOP_TIMER(lsflag, l->l_pfailtime);
631 LOCKSTAT_EVENT_RA(lsflag, l->l_pfaillock, LB_NOPREEMPT|LB_SPIN,
632 1, l->l_pfailtime, l->l_pfailaddr);
633 LOCKSTAT_EXIT(lsflag);
634 l->l_pfailtime = 0;
635 l->l_pfaillock = 0;
636 l->l_pfailaddr = 0;
637 }
638
639 if (l != newl) {
640 struct lwp *prevlwp;
641
642 /* Release all locks, but leave the current LWP locked */
643 if (l->l_mutex == spc->spc_mutex) {
644 /*
645 * Drop spc_lwplock, if the current LWP has been moved
646 * to the run queue (it is now locked by spc_mutex).
647 */
648 mutex_spin_exit(spc->spc_lwplock);
649 } else {
650 /*
651 * Otherwise, drop the spc_mutex, we are done with the
652 * run queues.
653 */
654 mutex_spin_exit(spc->spc_mutex);
655 }
656
657 /* We're down to only one lock, so do debug checks. */
658 LOCKDEBUG_BARRIER(l->l_mutex, 1);
659
660 /* Count the context switch. */
661 CPU_COUNT(CPU_COUNT_NSWTCH, 1);
662 l->l_ncsw++;
663 if ((l->l_pflag & LP_PREEMPTING) != 0) {
664 l->l_nivcsw++;
665 l->l_pflag &= ~LP_PREEMPTING;
666 }
667
668 /*
669 * Increase the count of spin-mutexes before the release
670 * of the last lock - we must remain at IPL_SCHED after
671 * releasing the lock.
672 */
673 KASSERTMSG(ci->ci_mtx_count == -1,
674 "%s: cpu%u: ci_mtx_count (%d) != -1 "
675 "(block with spin-mutex held)",
676 __func__, cpu_index(ci), ci->ci_mtx_count);
677 oldspl = MUTEX_SPIN_OLDSPL(ci);
678 ci->ci_mtx_count = -2;
679
680 /* Update status for lwpctl, if present. */
681 if (l->l_lwpctl != NULL) {
682 l->l_lwpctl->lc_curcpu = (l->l_stat == LSZOMB ?
683 LWPCTL_CPU_EXITED : LWPCTL_CPU_NONE);
684 }
685
686 /*
687 * If curlwp is a soft interrupt LWP, there's nobody on the
688 * other side to unlock - we're returning into an assembly
689 * trampoline. Unlock now. This is safe because this is a
690 * kernel LWP and is bound to current CPU: the worst anyone
691 * else will do to it, is to put it back onto this CPU's run
692 * queue (and the CPU is busy here right now!).
693 */
694 if (returning) {
695 /* Keep IPL_SCHED after this; MD code will fix up. */
696 l->l_flag &= ~LW_RUNNING;
697 lwp_unlock(l);
698 } else {
699 /* A normal LWP: save old VM context. */
700 pmap_deactivate(l);
701 }
702
703 /*
704 * If DTrace has set the active vtime enum to anything
705 * other than INACTIVE (0), then it should have set the
706 * function to call.
707 */
708 if (__predict_false(dtrace_vtime_active)) {
709 (*dtrace_vtime_switch_func)(newl);
710 }
711
712 /*
713 * We must ensure not to come here from inside a read section.
714 */
715 KASSERT(pserialize_not_in_read_section());
716
717 /* Switch to the new LWP.. */
718 #ifdef MULTIPROCESSOR
719 KASSERT(curlwp == ci->ci_curlwp);
720 #endif
721 KASSERTMSG(l == curlwp, "l %p curlwp %p", l, curlwp);
722 prevlwp = cpu_switchto(l, newl, returning);
723 ci = curcpu();
724 #ifdef MULTIPROCESSOR
725 KASSERT(curlwp == ci->ci_curlwp);
726 #endif
727 KASSERTMSG(l == curlwp, "l %p curlwp %p prevlwp %p",
728 l, curlwp, prevlwp);
729 KASSERT(prevlwp != NULL);
730 KASSERT(l->l_cpu == ci);
731 KASSERT(ci->ci_mtx_count == -2);
732
733 /*
734 * Immediately mark the previous LWP as no longer running,
735 * and unlock it. We'll still be at IPL_SCHED afterwards.
736 */
737 KASSERT((prevlwp->l_flag & LW_RUNNING) != 0);
738 prevlwp->l_flag &= ~LW_RUNNING;
739 lwp_unlock(prevlwp);
740
741 /*
742 * Switched away - we have new curlwp.
743 * Restore VM context and IPL.
744 */
745 pmap_activate(l);
746 pcu_switchpoint(l);
747
748 /* Update status for lwpctl, if present. */
749 if (l->l_lwpctl != NULL) {
750 l->l_lwpctl->lc_curcpu = (int)cpu_index(ci);
751 l->l_lwpctl->lc_pctr++;
752 }
753
754 /*
755 * Normalize the spin mutex count and restore the previous
756 * SPL. Note that, unless the caller disabled preemption,
757 * we can be preempted at any time after this splx().
758 */
759 KASSERT(l->l_cpu == ci);
760 KASSERT(ci->ci_mtx_count == -1);
761 ci->ci_mtx_count = 0;
762 splx(oldspl);
763 } else {
764 /* Nothing to do - just unlock and return. */
765 mutex_spin_exit(spc->spc_mutex);
766 l->l_pflag &= ~LP_PREEMPTING;
767 lwp_unlock(l);
768 }
769
770 KASSERT(l == curlwp);
771 KASSERT(l->l_stat == LSONPROC);
772
773 SYSCALL_TIME_WAKEUP(l);
774 LOCKDEBUG_BARRIER(NULL, 1);
775 }
776
777 /*
778 * setrunnable: change LWP state to be runnable, placing it on the run queue.
779 *
780 * Call with the process and LWP locked. Will return with the LWP unlocked.
781 */
782 void
783 setrunnable(struct lwp *l)
784 {
785 struct proc *p = l->l_proc;
786 struct cpu_info *ci;
787 kmutex_t *oldlock;
788
789 KASSERT((l->l_flag & LW_IDLE) == 0);
790 KASSERT((l->l_flag & LW_DBGSUSPEND) == 0);
791 KASSERT(mutex_owned(p->p_lock));
792 KASSERT(lwp_locked(l, NULL));
793 KASSERT(l->l_mutex != l->l_cpu->ci_schedstate.spc_mutex);
794
795 switch (l->l_stat) {
796 case LSSTOP:
797 /*
798 * If we're being traced (possibly because someone attached us
799 * while we were stopped), check for a signal from the debugger.
800 */
801 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xsig != 0)
802 signotify(l);
803 p->p_nrlwps++;
804 break;
805 case LSSUSPENDED:
806 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
807 l->l_flag &= ~LW_WSUSPEND;
808 p->p_nrlwps++;
809 cv_broadcast(&p->p_lwpcv);
810 break;
811 case LSSLEEP:
812 KASSERT(l->l_wchan != NULL);
813 break;
814 case LSIDL:
815 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
816 break;
817 default:
818 panic("setrunnable: lwp %p state was %d", l, l->l_stat);
819 }
820
821 /*
822 * If the LWP was sleeping, start it again.
823 */
824 if (l->l_wchan != NULL) {
825 l->l_stat = LSSLEEP;
826 /* lwp_unsleep() will release the lock. */
827 lwp_unsleep(l, true);
828 return;
829 }
830
831 /*
832 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
833 * about to call mi_switch(), in which case it will yield.
834 */
835 if ((l->l_flag & LW_RUNNING) != 0) {
836 l->l_stat = LSONPROC;
837 l->l_slptime = 0;
838 lwp_unlock(l);
839 return;
840 }
841
842 /*
843 * Look for a CPU to run.
844 * Set the LWP runnable.
845 */
846 ci = sched_takecpu(l);
847 l->l_cpu = ci;
848 spc_lock(ci);
849 oldlock = lwp_setlock(l, l->l_cpu->ci_schedstate.spc_mutex);
850 sched_setrunnable(l);
851 l->l_stat = LSRUN;
852 l->l_slptime = 0;
853 sched_enqueue(l);
854 sched_resched_lwp(l, true);
855 /* SPC & LWP now unlocked. */
856 mutex_spin_exit(oldlock);
857 }
858
859 /*
860 * suspendsched:
861 *
862 * Convert all non-LW_SYSTEM LSSLEEP or LSRUN LWPs to LSSUSPENDED.
863 */
864 void
865 suspendsched(void)
866 {
867 CPU_INFO_ITERATOR cii;
868 struct cpu_info *ci;
869 struct lwp *l;
870 struct proc *p;
871
872 /*
873 * We do this by process in order not to violate the locking rules.
874 */
875 mutex_enter(proc_lock);
876 PROCLIST_FOREACH(p, &allproc) {
877 mutex_enter(p->p_lock);
878 if ((p->p_flag & PK_SYSTEM) != 0) {
879 mutex_exit(p->p_lock);
880 continue;
881 }
882
883 if (p->p_stat != SSTOP) {
884 if (p->p_stat != SZOMB && p->p_stat != SDEAD) {
885 p->p_pptr->p_nstopchild++;
886 p->p_waited = 0;
887 }
888 p->p_stat = SSTOP;
889 }
890
891 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
892 if (l == curlwp)
893 continue;
894
895 lwp_lock(l);
896
897 /*
898 * Set L_WREBOOT so that the LWP will suspend itself
899 * when it tries to return to user mode. We want to
900 * try and get to get as many LWPs as possible to
901 * the user / kernel boundary, so that they will
902 * release any locks that they hold.
903 */
904 l->l_flag |= (LW_WREBOOT | LW_WSUSPEND);
905
906 if (l->l_stat == LSSLEEP &&
907 (l->l_flag & LW_SINTR) != 0) {
908 /* setrunnable() will release the lock. */
909 setrunnable(l);
910 continue;
911 }
912
913 lwp_unlock(l);
914 }
915
916 mutex_exit(p->p_lock);
917 }
918 mutex_exit(proc_lock);
919
920 /*
921 * Kick all CPUs to make them preempt any LWPs running in user mode.
922 * They'll trap into the kernel and suspend themselves in userret().
923 *
924 * Unusually, we don't hold any other scheduler object locked, which
925 * would keep preemption off for sched_resched_cpu(), so disable it
926 * explicitly.
927 */
928 kpreempt_disable();
929 for (CPU_INFO_FOREACH(cii, ci)) {
930 spc_lock(ci);
931 sched_resched_cpu(ci, PRI_KERNEL, true);
932 /* spc now unlocked */
933 }
934 kpreempt_enable();
935 }
936
937 /*
938 * sched_unsleep:
939 *
940 * The is called when the LWP has not been awoken normally but instead
941 * interrupted: for example, if the sleep timed out. Because of this,
942 * it's not a valid action for running or idle LWPs.
943 */
944 static void
945 sched_unsleep(struct lwp *l, bool cleanup)
946 {
947
948 lwp_unlock(l);
949 panic("sched_unsleep");
950 }
951
952 static void
953 sched_changepri(struct lwp *l, pri_t pri)
954 {
955 struct schedstate_percpu *spc;
956 struct cpu_info *ci;
957
958 KASSERT(lwp_locked(l, NULL));
959
960 ci = l->l_cpu;
961 spc = &ci->ci_schedstate;
962
963 if (l->l_stat == LSRUN) {
964 KASSERT(lwp_locked(l, spc->spc_mutex));
965 sched_dequeue(l);
966 l->l_priority = pri;
967 sched_enqueue(l);
968 sched_resched_lwp(l, false);
969 } else if (l->l_stat == LSONPROC && l->l_class != SCHED_OTHER) {
970 /* On priority drop, only evict realtime LWPs. */
971 KASSERT(lwp_locked(l, spc->spc_lwplock));
972 l->l_priority = pri;
973 spc_lock(ci);
974 sched_resched_cpu(ci, spc->spc_maxpriority, true);
975 /* spc now unlocked */
976 } else {
977 l->l_priority = pri;
978 }
979 }
980
981 static void
982 sched_lendpri(struct lwp *l, pri_t pri)
983 {
984 struct schedstate_percpu *spc;
985 struct cpu_info *ci;
986
987 KASSERT(lwp_locked(l, NULL));
988
989 ci = l->l_cpu;
990 spc = &ci->ci_schedstate;
991
992 if (l->l_stat == LSRUN) {
993 KASSERT(lwp_locked(l, spc->spc_mutex));
994 sched_dequeue(l);
995 l->l_inheritedprio = pri;
996 l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
997 sched_enqueue(l);
998 sched_resched_lwp(l, false);
999 } else if (l->l_stat == LSONPROC && l->l_class != SCHED_OTHER) {
1000 /* On priority drop, only evict realtime LWPs. */
1001 KASSERT(lwp_locked(l, spc->spc_lwplock));
1002 l->l_inheritedprio = pri;
1003 l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
1004 spc_lock(ci);
1005 sched_resched_cpu(ci, spc->spc_maxpriority, true);
1006 /* spc now unlocked */
1007 } else {
1008 l->l_inheritedprio = pri;
1009 l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
1010 }
1011 }
1012
1013 struct lwp *
1014 syncobj_noowner(wchan_t wchan)
1015 {
1016
1017 return NULL;
1018 }
1019
1020 /* Decay 95% of proc::p_pctcpu in 60 seconds, ccpu = exp(-1/20) */
1021 const fixpt_t ccpu = 0.95122942450071400909 * FSCALE;
1022
1023 /*
1024 * Constants for averages over 1, 5 and 15 minutes when sampling at
1025 * 5 second intervals.
1026 */
1027 static const fixpt_t cexp[ ] = {
1028 0.9200444146293232 * FSCALE, /* exp(-1/12) */
1029 0.9834714538216174 * FSCALE, /* exp(-1/60) */
1030 0.9944598480048967 * FSCALE, /* exp(-1/180) */
1031 };
1032
1033 /*
1034 * sched_pstats:
1035 *
1036 * => Update process statistics and check CPU resource allocation.
1037 * => Call scheduler-specific hook to eventually adjust LWP priorities.
1038 * => Compute load average of a quantity on 1, 5 and 15 minute intervals.
1039 */
1040 void
1041 sched_pstats(void)
1042 {
1043 extern struct loadavg averunnable;
1044 struct loadavg *avg = &averunnable;
1045 const int clkhz = (stathz != 0 ? stathz : hz);
1046 static bool backwards = false;
1047 static u_int lavg_count = 0;
1048 struct proc *p;
1049 int nrun;
1050
1051 sched_pstats_ticks++;
1052 if (++lavg_count >= 5) {
1053 lavg_count = 0;
1054 nrun = 0;
1055 }
1056 mutex_enter(proc_lock);
1057 PROCLIST_FOREACH(p, &allproc) {
1058 struct lwp *l;
1059 struct rlimit *rlim;
1060 time_t runtm;
1061 int sig;
1062
1063 /* Increment sleep time (if sleeping), ignore overflow. */
1064 mutex_enter(p->p_lock);
1065 runtm = p->p_rtime.sec;
1066 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1067 fixpt_t lpctcpu;
1068 u_int lcpticks;
1069
1070 if (__predict_false((l->l_flag & LW_IDLE) != 0))
1071 continue;
1072 lwp_lock(l);
1073 runtm += l->l_rtime.sec;
1074 l->l_swtime++;
1075 sched_lwp_stats(l);
1076
1077 /* For load average calculation. */
1078 if (__predict_false(lavg_count == 0) &&
1079 (l->l_flag & (LW_SINTR | LW_SYSTEM)) == 0) {
1080 switch (l->l_stat) {
1081 case LSSLEEP:
1082 if (l->l_slptime > 1) {
1083 break;
1084 }
1085 /* FALLTHROUGH */
1086 case LSRUN:
1087 case LSONPROC:
1088 case LSIDL:
1089 nrun++;
1090 }
1091 }
1092 lwp_unlock(l);
1093
1094 l->l_pctcpu = (l->l_pctcpu * ccpu) >> FSHIFT;
1095 if (l->l_slptime != 0)
1096 continue;
1097
1098 lpctcpu = l->l_pctcpu;
1099 lcpticks = atomic_swap_uint(&l->l_cpticks, 0);
1100 lpctcpu += ((FSCALE - ccpu) *
1101 (lcpticks * FSCALE / clkhz)) >> FSHIFT;
1102 l->l_pctcpu = lpctcpu;
1103 }
1104 /* Calculating p_pctcpu only for ps(1) */
1105 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
1106
1107 if (__predict_false(runtm < 0)) {
1108 if (!backwards) {
1109 backwards = true;
1110 printf("WARNING: negative runtime; "
1111 "monotonic clock has gone backwards\n");
1112 }
1113 mutex_exit(p->p_lock);
1114 continue;
1115 }
1116
1117 /*
1118 * Check if the process exceeds its CPU resource allocation.
1119 * If over the hard limit, kill it with SIGKILL.
1120 * If over the soft limit, send SIGXCPU and raise
1121 * the soft limit a little.
1122 */
1123 rlim = &p->p_rlimit[RLIMIT_CPU];
1124 sig = 0;
1125 if (__predict_false(runtm >= rlim->rlim_cur)) {
1126 if (runtm >= rlim->rlim_max) {
1127 sig = SIGKILL;
1128 log(LOG_NOTICE,
1129 "pid %d, command %s, is killed: %s\n",
1130 p->p_pid, p->p_comm, "exceeded RLIMIT_CPU");
1131 uprintf("pid %d, command %s, is killed: %s\n",
1132 p->p_pid, p->p_comm, "exceeded RLIMIT_CPU");
1133 } else {
1134 sig = SIGXCPU;
1135 if (rlim->rlim_cur < rlim->rlim_max)
1136 rlim->rlim_cur += 5;
1137 }
1138 }
1139 mutex_exit(p->p_lock);
1140 if (__predict_false(sig)) {
1141 KASSERT((p->p_flag & PK_SYSTEM) == 0);
1142 psignal(p, sig);
1143 }
1144 }
1145
1146 /* Load average calculation. */
1147 if (__predict_false(lavg_count == 0)) {
1148 int i;
1149 CTASSERT(__arraycount(cexp) == __arraycount(avg->ldavg));
1150 for (i = 0; i < __arraycount(cexp); i++) {
1151 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
1152 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
1153 }
1154 }
1155
1156 /* Lightning bolt. */
1157 cv_broadcast(&lbolt);
1158
1159 mutex_exit(proc_lock);
1160 }
1161