kern_synch.c revision 1.274.2.3 1 /* $NetBSD: kern_synch.c,v 1.274.2.3 2010/11/06 08:08:43 uebayasi Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2004, 2006, 2007, 2008, 2009
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.274.2.3 2010/11/06 08:08:43 uebayasi Exp $");
73
74 #include "opt_kstack.h"
75 #include "opt_perfctrs.h"
76 #include "opt_sa.h"
77 #include "opt_dtrace.h"
78
79 #define __MUTEX_PRIVATE
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/proc.h>
84 #include <sys/kernel.h>
85 #if defined(PERFCTRS)
86 #include <sys/pmc.h>
87 #endif
88 #include <sys/cpu.h>
89 #include <sys/resourcevar.h>
90 #include <sys/sched.h>
91 #include <sys/sa.h>
92 #include <sys/savar.h>
93 #include <sys/syscall_stats.h>
94 #include <sys/sleepq.h>
95 #include <sys/lockdebug.h>
96 #include <sys/evcnt.h>
97 #include <sys/intr.h>
98 #include <sys/lwpctl.h>
99 #include <sys/atomic.h>
100 #include <sys/simplelock.h>
101
102 #include <uvm/uvm_extern.h>
103
104 #include <dev/lockstat.h>
105
106 #include <sys/dtrace_bsd.h>
107 int dtrace_vtime_active=0;
108 dtrace_vtime_switch_func_t dtrace_vtime_switch_func;
109
110 static void sched_unsleep(struct lwp *, bool);
111 static void sched_changepri(struct lwp *, pri_t);
112 static void sched_lendpri(struct lwp *, pri_t);
113 static void resched_cpu(struct lwp *);
114
115 syncobj_t sleep_syncobj = {
116 SOBJ_SLEEPQ_SORTED,
117 sleepq_unsleep,
118 sleepq_changepri,
119 sleepq_lendpri,
120 syncobj_noowner,
121 };
122
123 syncobj_t sched_syncobj = {
124 SOBJ_SLEEPQ_SORTED,
125 sched_unsleep,
126 sched_changepri,
127 sched_lendpri,
128 syncobj_noowner,
129 };
130
131 unsigned sched_pstats_ticks;
132 kcondvar_t lbolt; /* once a second sleep address */
133
134 /* Preemption event counters */
135 static struct evcnt kpreempt_ev_crit;
136 static struct evcnt kpreempt_ev_klock;
137 static struct evcnt kpreempt_ev_immed;
138
139 /*
140 * During autoconfiguration or after a panic, a sleep will simply lower the
141 * priority briefly to allow interrupts, then return. The priority to be
142 * used (safepri) is machine-dependent, thus this value is initialized and
143 * maintained in the machine-dependent layers. This priority will typically
144 * be 0, or the lowest priority that is safe for use on the interrupt stack;
145 * it can be made higher to block network software interrupts after panics.
146 */
147 int safepri;
148
149 void
150 synch_init(void)
151 {
152
153 cv_init(&lbolt, "lbolt");
154
155 evcnt_attach_dynamic(&kpreempt_ev_crit, EVCNT_TYPE_MISC, NULL,
156 "kpreempt", "defer: critical section");
157 evcnt_attach_dynamic(&kpreempt_ev_klock, EVCNT_TYPE_MISC, NULL,
158 "kpreempt", "defer: kernel_lock");
159 evcnt_attach_dynamic(&kpreempt_ev_immed, EVCNT_TYPE_MISC, NULL,
160 "kpreempt", "immediate");
161 }
162
163 /*
164 * OBSOLETE INTERFACE
165 *
166 * General sleep call. Suspends the current LWP until a wakeup is
167 * performed on the specified identifier. The LWP will then be made
168 * runnable with the specified priority. Sleeps at most timo/hz seconds (0
169 * means no timeout). If pri includes PCATCH flag, signals are checked
170 * before and after sleeping, else signals are not checked. Returns 0 if
171 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
172 * signal needs to be delivered, ERESTART is returned if the current system
173 * call should be restarted if possible, and EINTR is returned if the system
174 * call should be interrupted by the signal (return EINTR).
175 *
176 * The interlock is held until we are on a sleep queue. The interlock will
177 * be locked before returning back to the caller unless the PNORELOCK flag
178 * is specified, in which case the interlock will always be unlocked upon
179 * return.
180 */
181 int
182 ltsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
183 volatile struct simplelock *interlock)
184 {
185 struct lwp *l = curlwp;
186 sleepq_t *sq;
187 kmutex_t *mp;
188 int error;
189
190 KASSERT((l->l_pflag & LP_INTR) == 0);
191 KASSERT(ident != &lbolt);
192
193 if (sleepq_dontsleep(l)) {
194 (void)sleepq_abort(NULL, 0);
195 if ((priority & PNORELOCK) != 0)
196 simple_unlock(interlock);
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
205 if (interlock != NULL) {
206 KASSERT(simple_lock_held(interlock));
207 simple_unlock(interlock);
208 }
209
210 error = sleepq_block(timo, priority & PCATCH);
211
212 if (interlock != NULL && (priority & PNORELOCK) == 0)
213 simple_lock(interlock);
214
215 return error;
216 }
217
218 int
219 mtsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
220 kmutex_t *mtx)
221 {
222 struct lwp *l = curlwp;
223 sleepq_t *sq;
224 kmutex_t *mp;
225 int error;
226
227 KASSERT((l->l_pflag & LP_INTR) == 0);
228 KASSERT(ident != &lbolt);
229
230 if (sleepq_dontsleep(l)) {
231 (void)sleepq_abort(mtx, (priority & PNORELOCK) != 0);
232 return 0;
233 }
234
235 l->l_kpriority = true;
236 sq = sleeptab_lookup(&sleeptab, ident, &mp);
237 sleepq_enter(sq, l, mp);
238 sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj);
239 mutex_exit(mtx);
240 error = sleepq_block(timo, priority & PCATCH);
241
242 if ((priority & PNORELOCK) == 0)
243 mutex_enter(mtx);
244
245 return error;
246 }
247
248 /*
249 * General sleep call for situations where a wake-up is not expected.
250 */
251 int
252 kpause(const char *wmesg, bool intr, int timo, kmutex_t *mtx)
253 {
254 struct lwp *l = curlwp;
255 kmutex_t *mp;
256 sleepq_t *sq;
257 int error;
258
259 KASSERT(!(timo == 0 && intr == false));
260
261 if (sleepq_dontsleep(l))
262 return sleepq_abort(NULL, 0);
263
264 if (mtx != NULL)
265 mutex_exit(mtx);
266 l->l_kpriority = true;
267 sq = sleeptab_lookup(&sleeptab, l, &mp);
268 sleepq_enter(sq, l, mp);
269 sleepq_enqueue(sq, l, wmesg, &sleep_syncobj);
270 error = sleepq_block(timo, intr);
271 if (mtx != NULL)
272 mutex_enter(mtx);
273
274 return error;
275 }
276
277 #ifdef KERN_SA
278 /*
279 * sa_awaken:
280 *
281 * We believe this lwp is an SA lwp. If it's yielding,
282 * let it know it needs to wake up.
283 *
284 * We are called and exit with the lwp locked. We are
285 * called in the middle of wakeup operations, so we need
286 * to not touch the locks at all.
287 */
288 void
289 sa_awaken(struct lwp *l)
290 {
291 /* LOCK_ASSERT(lwp_locked(l, NULL)); */
292
293 if (l == l->l_savp->savp_lwp && l->l_flag & LW_SA_YIELD)
294 l->l_flag &= ~LW_SA_IDLE;
295 }
296 #endif /* KERN_SA */
297
298 /*
299 * OBSOLETE INTERFACE
300 *
301 * Make all LWPs sleeping on the specified identifier runnable.
302 */
303 void
304 wakeup(wchan_t ident)
305 {
306 sleepq_t *sq;
307 kmutex_t *mp;
308
309 if (__predict_false(cold))
310 return;
311
312 sq = sleeptab_lookup(&sleeptab, ident, &mp);
313 sleepq_wake(sq, ident, (u_int)-1, mp);
314 }
315
316 /*
317 * OBSOLETE INTERFACE
318 *
319 * Make the highest priority LWP first in line on the specified
320 * identifier runnable.
321 */
322 void
323 wakeup_one(wchan_t ident)
324 {
325 sleepq_t *sq;
326 kmutex_t *mp;
327
328 if (__predict_false(cold))
329 return;
330
331 sq = sleeptab_lookup(&sleeptab, ident, &mp);
332 sleepq_wake(sq, ident, 1, mp);
333 }
334
335
336 /*
337 * General yield call. Puts the current LWP back on its run queue and
338 * performs a voluntary context switch. Should only be called when the
339 * current LWP explicitly requests it (eg sched_yield(2)).
340 */
341 void
342 yield(void)
343 {
344 struct lwp *l = curlwp;
345
346 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
347 lwp_lock(l);
348 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
349 KASSERT(l->l_stat == LSONPROC);
350 l->l_kpriority = false;
351 (void)mi_switch(l);
352 KERNEL_LOCK(l->l_biglocks, l);
353 }
354
355 /*
356 * General preemption call. Puts the current LWP back on its run queue
357 * and performs an involuntary context switch.
358 */
359 void
360 preempt(void)
361 {
362 struct lwp *l = curlwp;
363
364 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
365 lwp_lock(l);
366 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
367 KASSERT(l->l_stat == LSONPROC);
368 l->l_kpriority = false;
369 l->l_nivcsw++;
370 (void)mi_switch(l);
371 KERNEL_LOCK(l->l_biglocks, l);
372 }
373
374 /*
375 * Handle a request made by another agent to preempt the current LWP
376 * in-kernel. Usually called when l_dopreempt may be non-zero.
377 *
378 * Character addresses for lockstat only.
379 */
380 static char in_critical_section;
381 static char kernel_lock_held;
382 static char is_softint;
383 static char cpu_kpreempt_enter_fail;
384
385 bool
386 kpreempt(uintptr_t where)
387 {
388 uintptr_t failed;
389 lwp_t *l;
390 int s, dop, lsflag;
391
392 l = curlwp;
393 failed = 0;
394 while ((dop = l->l_dopreempt) != 0) {
395 if (l->l_stat != LSONPROC) {
396 /*
397 * About to block (or die), let it happen.
398 * Doesn't really count as "preemption has
399 * been blocked", since we're going to
400 * context switch.
401 */
402 l->l_dopreempt = 0;
403 return true;
404 }
405 if (__predict_false((l->l_flag & LW_IDLE) != 0)) {
406 /* Can't preempt idle loop, don't count as failure. */
407 l->l_dopreempt = 0;
408 return true;
409 }
410 if (__predict_false(l->l_nopreempt != 0)) {
411 /* LWP holds preemption disabled, explicitly. */
412 if ((dop & DOPREEMPT_COUNTED) == 0) {
413 kpreempt_ev_crit.ev_count++;
414 }
415 failed = (uintptr_t)&in_critical_section;
416 break;
417 }
418 if (__predict_false((l->l_pflag & LP_INTR) != 0)) {
419 /* Can't preempt soft interrupts yet. */
420 l->l_dopreempt = 0;
421 failed = (uintptr_t)&is_softint;
422 break;
423 }
424 s = splsched();
425 if (__predict_false(l->l_blcnt != 0 ||
426 curcpu()->ci_biglock_wanted != NULL)) {
427 /* Hold or want kernel_lock, code is not MT safe. */
428 splx(s);
429 if ((dop & DOPREEMPT_COUNTED) == 0) {
430 kpreempt_ev_klock.ev_count++;
431 }
432 failed = (uintptr_t)&kernel_lock_held;
433 break;
434 }
435 if (__predict_false(!cpu_kpreempt_enter(where, s))) {
436 /*
437 * It may be that the IPL is too high.
438 * kpreempt_enter() can schedule an
439 * interrupt to retry later.
440 */
441 splx(s);
442 failed = (uintptr_t)&cpu_kpreempt_enter_fail;
443 break;
444 }
445 /* Do it! */
446 if (__predict_true((dop & DOPREEMPT_COUNTED) == 0)) {
447 kpreempt_ev_immed.ev_count++;
448 }
449 lwp_lock(l);
450 mi_switch(l);
451 l->l_nopreempt++;
452 splx(s);
453
454 /* Take care of any MD cleanup. */
455 cpu_kpreempt_exit(where);
456 l->l_nopreempt--;
457 }
458
459 if (__predict_true(!failed)) {
460 return false;
461 }
462
463 /* Record preemption failure for reporting via lockstat. */
464 atomic_or_uint(&l->l_dopreempt, DOPREEMPT_COUNTED);
465 lsflag = 0;
466 LOCKSTAT_ENTER(lsflag);
467 if (__predict_false(lsflag)) {
468 if (where == 0) {
469 where = (uintptr_t)__builtin_return_address(0);
470 }
471 /* Preemption is on, might recurse, so make it atomic. */
472 if (atomic_cas_ptr_ni((void *)&l->l_pfailaddr, NULL,
473 (void *)where) == NULL) {
474 LOCKSTAT_START_TIMER(lsflag, l->l_pfailtime);
475 l->l_pfaillock = failed;
476 }
477 }
478 LOCKSTAT_EXIT(lsflag);
479 return true;
480 }
481
482 /*
483 * Return true if preemption is explicitly disabled.
484 */
485 bool
486 kpreempt_disabled(void)
487 {
488 const lwp_t *l = curlwp;
489
490 return l->l_nopreempt != 0 || l->l_stat == LSZOMB ||
491 (l->l_flag & LW_IDLE) != 0 || cpu_kpreempt_disabled();
492 }
493
494 /*
495 * Disable kernel preemption.
496 */
497 void
498 kpreempt_disable(void)
499 {
500
501 KPREEMPT_DISABLE(curlwp);
502 }
503
504 /*
505 * Reenable kernel preemption.
506 */
507 void
508 kpreempt_enable(void)
509 {
510
511 KPREEMPT_ENABLE(curlwp);
512 }
513
514 /*
515 * Compute the amount of time during which the current lwp was running.
516 *
517 * - update l_rtime unless it's an idle lwp.
518 */
519
520 void
521 updatertime(lwp_t *l, const struct bintime *now)
522 {
523
524 if (__predict_false(l->l_flag & LW_IDLE))
525 return;
526
527 /* rtime += now - stime */
528 bintime_add(&l->l_rtime, now);
529 bintime_sub(&l->l_rtime, &l->l_stime);
530 }
531
532 /*
533 * Select next LWP from the current CPU to run..
534 */
535 static inline lwp_t *
536 nextlwp(struct cpu_info *ci, struct schedstate_percpu *spc)
537 {
538 lwp_t *newl;
539
540 /*
541 * Let sched_nextlwp() select the LWP to run the CPU next.
542 * If no LWP is runnable, select the idle LWP.
543 *
544 * Note that spc_lwplock might not necessary be held, and
545 * new thread would be unlocked after setting the LWP-lock.
546 */
547 newl = sched_nextlwp();
548 if (newl != NULL) {
549 sched_dequeue(newl);
550 KASSERT(lwp_locked(newl, spc->spc_mutex));
551 KASSERT(newl->l_cpu == ci);
552 newl->l_stat = LSONPROC;
553 newl->l_pflag |= LP_RUNNING;
554 lwp_setlock(newl, spc->spc_lwplock);
555 } else {
556 newl = ci->ci_data.cpu_idlelwp;
557 newl->l_stat = LSONPROC;
558 newl->l_pflag |= LP_RUNNING;
559 }
560
561 /*
562 * Only clear want_resched if there are no pending (slow)
563 * software interrupts.
564 */
565 ci->ci_want_resched = ci->ci_data.cpu_softints;
566 spc->spc_flags &= ~SPCF_SWITCHCLEAR;
567 spc->spc_curpriority = lwp_eprio(newl);
568
569 return newl;
570 }
571
572 /*
573 * The machine independent parts of context switch.
574 *
575 * Returns 1 if another LWP was actually run.
576 */
577 int
578 mi_switch(lwp_t *l)
579 {
580 struct cpu_info *ci;
581 struct schedstate_percpu *spc;
582 struct lwp *newl;
583 int retval, oldspl;
584 struct bintime bt;
585 bool returning;
586
587 KASSERT(lwp_locked(l, NULL));
588 KASSERT(kpreempt_disabled());
589 LOCKDEBUG_BARRIER(l->l_mutex, 1);
590
591 kstack_check_magic(l);
592
593 binuptime(&bt);
594
595 KASSERT((l->l_pflag & LP_RUNNING) != 0);
596 KASSERT(l->l_cpu == curcpu());
597 ci = l->l_cpu;
598 spc = &ci->ci_schedstate;
599 returning = false;
600 newl = NULL;
601
602 /*
603 * If we have been asked to switch to a specific LWP, then there
604 * is no need to inspect the run queues. If a soft interrupt is
605 * blocking, then return to the interrupted thread without adjusting
606 * VM context or its start time: neither have been changed in order
607 * to take the interrupt.
608 */
609 if (l->l_switchto != NULL) {
610 if ((l->l_pflag & LP_INTR) != 0) {
611 returning = true;
612 softint_block(l);
613 if ((l->l_pflag & LP_TIMEINTR) != 0)
614 updatertime(l, &bt);
615 }
616 newl = l->l_switchto;
617 l->l_switchto = NULL;
618 }
619 #ifndef __HAVE_FAST_SOFTINTS
620 else if (ci->ci_data.cpu_softints != 0) {
621 /* There are pending soft interrupts, so pick one. */
622 newl = softint_picklwp();
623 newl->l_stat = LSONPROC;
624 newl->l_pflag |= LP_RUNNING;
625 }
626 #endif /* !__HAVE_FAST_SOFTINTS */
627
628 /* Count time spent in current system call */
629 if (!returning) {
630 SYSCALL_TIME_SLEEP(l);
631
632 /*
633 * XXXSMP If we are using h/w performance counters,
634 * save context.
635 */
636 #if PERFCTRS
637 if (PMC_ENABLED(l->l_proc)) {
638 pmc_save_context(l->l_proc);
639 }
640 #endif
641 updatertime(l, &bt);
642 }
643
644 /* Lock the runqueue */
645 KASSERT(l->l_stat != LSRUN);
646 mutex_spin_enter(spc->spc_mutex);
647
648 /*
649 * If on the CPU and we have gotten this far, then we must yield.
650 */
651 if (l->l_stat == LSONPROC && l != newl) {
652 KASSERT(lwp_locked(l, spc->spc_lwplock));
653 if ((l->l_flag & LW_IDLE) == 0) {
654 l->l_stat = LSRUN;
655 lwp_setlock(l, spc->spc_mutex);
656 sched_enqueue(l, true);
657 /* Handle migration case */
658 KASSERT(spc->spc_migrating == NULL);
659 if (l->l_target_cpu != NULL) {
660 spc->spc_migrating = l;
661 }
662 } else
663 l->l_stat = LSIDL;
664 }
665
666 /* Pick new LWP to run. */
667 if (newl == NULL) {
668 newl = nextlwp(ci, spc);
669 }
670
671 /* Items that must be updated with the CPU locked. */
672 if (!returning) {
673 /* Update the new LWP's start time. */
674 newl->l_stime = bt;
675
676 /*
677 * ci_curlwp changes when a fast soft interrupt occurs.
678 * We use cpu_onproc to keep track of which kernel or
679 * user thread is running 'underneath' the software
680 * interrupt. This is important for time accounting,
681 * itimers and forcing user threads to preempt (aston).
682 */
683 ci->ci_data.cpu_onproc = newl;
684 }
685
686 /*
687 * Preemption related tasks. Must be done with the current
688 * CPU locked.
689 */
690 cpu_did_resched(l);
691 l->l_dopreempt = 0;
692 if (__predict_false(l->l_pfailaddr != 0)) {
693 LOCKSTAT_FLAG(lsflag);
694 LOCKSTAT_ENTER(lsflag);
695 LOCKSTAT_STOP_TIMER(lsflag, l->l_pfailtime);
696 LOCKSTAT_EVENT_RA(lsflag, l->l_pfaillock, LB_NOPREEMPT|LB_SPIN,
697 1, l->l_pfailtime, l->l_pfailaddr);
698 LOCKSTAT_EXIT(lsflag);
699 l->l_pfailtime = 0;
700 l->l_pfaillock = 0;
701 l->l_pfailaddr = 0;
702 }
703
704 if (l != newl) {
705 struct lwp *prevlwp;
706
707 /* Release all locks, but leave the current LWP locked */
708 if (l->l_mutex == spc->spc_mutex) {
709 /*
710 * Drop spc_lwplock, if the current LWP has been moved
711 * to the run queue (it is now locked by spc_mutex).
712 */
713 mutex_spin_exit(spc->spc_lwplock);
714 } else {
715 /*
716 * Otherwise, drop the spc_mutex, we are done with the
717 * run queues.
718 */
719 mutex_spin_exit(spc->spc_mutex);
720 }
721
722 /*
723 * Mark that context switch is going to be performed
724 * for this LWP, to protect it from being switched
725 * to on another CPU.
726 */
727 KASSERT(l->l_ctxswtch == 0);
728 l->l_ctxswtch = 1;
729 l->l_ncsw++;
730 KASSERT((l->l_pflag & LP_RUNNING) != 0);
731 l->l_pflag &= ~LP_RUNNING;
732
733 /*
734 * Increase the count of spin-mutexes before the release
735 * of the last lock - we must remain at IPL_SCHED during
736 * the context switch.
737 */
738 oldspl = MUTEX_SPIN_OLDSPL(ci);
739 ci->ci_mtx_count--;
740 lwp_unlock(l);
741
742 /* Count the context switch on this CPU. */
743 ci->ci_data.cpu_nswtch++;
744
745 /* Update status for lwpctl, if present. */
746 if (l->l_lwpctl != NULL)
747 l->l_lwpctl->lc_curcpu = LWPCTL_CPU_NONE;
748
749 /*
750 * Save old VM context, unless a soft interrupt
751 * handler is blocking.
752 */
753 if (!returning)
754 pmap_deactivate(l);
755
756 /*
757 * We may need to spin-wait if 'newl' is still
758 * context switching on another CPU.
759 */
760 if (__predict_false(newl->l_ctxswtch != 0)) {
761 u_int count;
762 count = SPINLOCK_BACKOFF_MIN;
763 while (newl->l_ctxswtch)
764 SPINLOCK_BACKOFF(count);
765 }
766
767 /*
768 * If DTrace has set the active vtime enum to anything
769 * other than INACTIVE (0), then it should have set the
770 * function to call.
771 */
772 if (__predict_false(dtrace_vtime_active)) {
773 (*dtrace_vtime_switch_func)(newl);
774 }
775
776 /* Switch to the new LWP.. */
777 prevlwp = cpu_switchto(l, newl, returning);
778 ci = curcpu();
779
780 /*
781 * Switched away - we have new curlwp.
782 * Restore VM context and IPL.
783 */
784 pmap_activate(l);
785 uvm_emap_switch(l);
786
787 if (prevlwp != NULL) {
788 /* Normalize the count of the spin-mutexes */
789 ci->ci_mtx_count++;
790 /* Unmark the state of context switch */
791 membar_exit();
792 prevlwp->l_ctxswtch = 0;
793 }
794
795 /* Update status for lwpctl, if present. */
796 if (l->l_lwpctl != NULL) {
797 l->l_lwpctl->lc_curcpu = (int)cpu_index(ci);
798 l->l_lwpctl->lc_pctr++;
799 }
800
801 KASSERT(l->l_cpu == ci);
802 splx(oldspl);
803 retval = 1;
804 } else {
805 /* Nothing to do - just unlock and return. */
806 mutex_spin_exit(spc->spc_mutex);
807 lwp_unlock(l);
808 retval = 0;
809 }
810
811 KASSERT(l == curlwp);
812 KASSERT(l->l_stat == LSONPROC);
813
814 /*
815 * XXXSMP If we are using h/w performance counters, restore context.
816 * XXXSMP preemption problem.
817 */
818 #if PERFCTRS
819 if (PMC_ENABLED(l->l_proc)) {
820 pmc_restore_context(l->l_proc);
821 }
822 #endif
823 SYSCALL_TIME_WAKEUP(l);
824 LOCKDEBUG_BARRIER(NULL, 1);
825
826 return retval;
827 }
828
829 /*
830 * The machine independent parts of context switch to oblivion.
831 * Does not return. Call with the LWP unlocked.
832 */
833 void
834 lwp_exit_switchaway(lwp_t *l)
835 {
836 struct cpu_info *ci;
837 struct lwp *newl;
838 struct bintime bt;
839
840 ci = l->l_cpu;
841
842 KASSERT(kpreempt_disabled());
843 KASSERT(l->l_stat == LSZOMB || l->l_stat == LSIDL);
844 KASSERT(ci == curcpu());
845 LOCKDEBUG_BARRIER(NULL, 0);
846
847 kstack_check_magic(l);
848
849 /* Count time spent in current system call */
850 SYSCALL_TIME_SLEEP(l);
851 binuptime(&bt);
852 updatertime(l, &bt);
853
854 /* Must stay at IPL_SCHED even after releasing run queue lock. */
855 (void)splsched();
856
857 /*
858 * Let sched_nextlwp() select the LWP to run the CPU next.
859 * If no LWP is runnable, select the idle LWP.
860 *
861 * Note that spc_lwplock might not necessary be held, and
862 * new thread would be unlocked after setting the LWP-lock.
863 */
864 spc_lock(ci);
865 #ifndef __HAVE_FAST_SOFTINTS
866 if (ci->ci_data.cpu_softints != 0) {
867 /* There are pending soft interrupts, so pick one. */
868 newl = softint_picklwp();
869 newl->l_stat = LSONPROC;
870 newl->l_pflag |= LP_RUNNING;
871 } else
872 #endif /* !__HAVE_FAST_SOFTINTS */
873 {
874 newl = nextlwp(ci, &ci->ci_schedstate);
875 }
876
877 /* Update the new LWP's start time. */
878 newl->l_stime = bt;
879 l->l_pflag &= ~LP_RUNNING;
880
881 /*
882 * ci_curlwp changes when a fast soft interrupt occurs.
883 * We use cpu_onproc to keep track of which kernel or
884 * user thread is running 'underneath' the software
885 * interrupt. This is important for time accounting,
886 * itimers and forcing user threads to preempt (aston).
887 */
888 ci->ci_data.cpu_onproc = newl;
889
890 /*
891 * Preemption related tasks. Must be done with the current
892 * CPU locked.
893 */
894 cpu_did_resched(l);
895
896 /* Unlock the run queue. */
897 spc_unlock(ci);
898
899 /* Count the context switch on this CPU. */
900 ci->ci_data.cpu_nswtch++;
901
902 /* Update status for lwpctl, if present. */
903 if (l->l_lwpctl != NULL)
904 l->l_lwpctl->lc_curcpu = LWPCTL_CPU_EXITED;
905
906 /*
907 * We may need to spin-wait if 'newl' is still
908 * context switching on another CPU.
909 */
910 if (__predict_false(newl->l_ctxswtch != 0)) {
911 u_int count;
912 count = SPINLOCK_BACKOFF_MIN;
913 while (newl->l_ctxswtch)
914 SPINLOCK_BACKOFF(count);
915 }
916
917 /*
918 * If DTrace has set the active vtime enum to anything
919 * other than INACTIVE (0), then it should have set the
920 * function to call.
921 */
922 if (__predict_false(dtrace_vtime_active)) {
923 (*dtrace_vtime_switch_func)(newl);
924 }
925
926 /* Switch to the new LWP.. */
927 (void)cpu_switchto(NULL, newl, false);
928
929 for (;;) continue; /* XXX: convince gcc about "noreturn" */
930 /* NOTREACHED */
931 }
932
933 /*
934 * setrunnable: change LWP state to be runnable, placing it on the run queue.
935 *
936 * Call with the process and LWP locked. Will return with the LWP unlocked.
937 */
938 void
939 setrunnable(struct lwp *l)
940 {
941 struct proc *p = l->l_proc;
942 struct cpu_info *ci;
943
944 KASSERT((l->l_flag & LW_IDLE) == 0);
945 KASSERT(mutex_owned(p->p_lock));
946 KASSERT(lwp_locked(l, NULL));
947 KASSERT(l->l_mutex != l->l_cpu->ci_schedstate.spc_mutex);
948
949 switch (l->l_stat) {
950 case LSSTOP:
951 /*
952 * If we're being traced (possibly because someone attached us
953 * while we were stopped), check for a signal from the debugger.
954 */
955 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xstat != 0)
956 signotify(l);
957 p->p_nrlwps++;
958 break;
959 case LSSUSPENDED:
960 l->l_flag &= ~LW_WSUSPEND;
961 p->p_nrlwps++;
962 cv_broadcast(&p->p_lwpcv);
963 break;
964 case LSSLEEP:
965 KASSERT(l->l_wchan != NULL);
966 break;
967 default:
968 panic("setrunnable: lwp %p state was %d", l, l->l_stat);
969 }
970
971 #ifdef KERN_SA
972 if (l->l_proc->p_sa)
973 sa_awaken(l);
974 #endif /* KERN_SA */
975
976 /*
977 * If the LWP was sleeping interruptably, then it's OK to start it
978 * again. If not, mark it as still sleeping.
979 */
980 if (l->l_wchan != NULL) {
981 l->l_stat = LSSLEEP;
982 /* lwp_unsleep() will release the lock. */
983 lwp_unsleep(l, true);
984 return;
985 }
986
987 /*
988 * If the LWP is still on the CPU, mark it as LSONPROC. It may be
989 * about to call mi_switch(), in which case it will yield.
990 */
991 if ((l->l_pflag & LP_RUNNING) != 0) {
992 l->l_stat = LSONPROC;
993 l->l_slptime = 0;
994 lwp_unlock(l);
995 return;
996 }
997
998 /*
999 * Look for a CPU to run.
1000 * Set the LWP runnable.
1001 */
1002 ci = sched_takecpu(l);
1003 l->l_cpu = ci;
1004 spc_lock(ci);
1005 lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
1006 sched_setrunnable(l);
1007 l->l_stat = LSRUN;
1008 l->l_slptime = 0;
1009
1010 sched_enqueue(l, false);
1011 resched_cpu(l);
1012 lwp_unlock(l);
1013 }
1014
1015 /*
1016 * suspendsched:
1017 *
1018 * Convert all non-LW_SYSTEM LSSLEEP or LSRUN LWPs to LSSUSPENDED.
1019 */
1020 void
1021 suspendsched(void)
1022 {
1023 CPU_INFO_ITERATOR cii;
1024 struct cpu_info *ci;
1025 struct lwp *l;
1026 struct proc *p;
1027
1028 /*
1029 * We do this by process in order not to violate the locking rules.
1030 */
1031 mutex_enter(proc_lock);
1032 PROCLIST_FOREACH(p, &allproc) {
1033 mutex_enter(p->p_lock);
1034 if ((p->p_flag & PK_SYSTEM) != 0) {
1035 mutex_exit(p->p_lock);
1036 continue;
1037 }
1038
1039 p->p_stat = SSTOP;
1040
1041 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1042 if (l == curlwp)
1043 continue;
1044
1045 lwp_lock(l);
1046
1047 /*
1048 * Set L_WREBOOT so that the LWP will suspend itself
1049 * when it tries to return to user mode. We want to
1050 * try and get to get as many LWPs as possible to
1051 * the user / kernel boundary, so that they will
1052 * release any locks that they hold.
1053 */
1054 l->l_flag |= (LW_WREBOOT | LW_WSUSPEND);
1055
1056 if (l->l_stat == LSSLEEP &&
1057 (l->l_flag & LW_SINTR) != 0) {
1058 /* setrunnable() will release the lock. */
1059 setrunnable(l);
1060 continue;
1061 }
1062
1063 lwp_unlock(l);
1064 }
1065
1066 mutex_exit(p->p_lock);
1067 }
1068 mutex_exit(proc_lock);
1069
1070 /*
1071 * Kick all CPUs to make them preempt any LWPs running in user mode.
1072 * They'll trap into the kernel and suspend themselves in userret().
1073 */
1074 for (CPU_INFO_FOREACH(cii, ci)) {
1075 spc_lock(ci);
1076 cpu_need_resched(ci, RESCHED_IMMED);
1077 spc_unlock(ci);
1078 }
1079 }
1080
1081 /*
1082 * sched_unsleep:
1083 *
1084 * The is called when the LWP has not been awoken normally but instead
1085 * interrupted: for example, if the sleep timed out. Because of this,
1086 * it's not a valid action for running or idle LWPs.
1087 */
1088 static void
1089 sched_unsleep(struct lwp *l, bool cleanup)
1090 {
1091
1092 lwp_unlock(l);
1093 panic("sched_unsleep");
1094 }
1095
1096 static void
1097 resched_cpu(struct lwp *l)
1098 {
1099 struct cpu_info *ci = l->l_cpu;
1100
1101 KASSERT(lwp_locked(l, NULL));
1102 if (lwp_eprio(l) > ci->ci_schedstate.spc_curpriority)
1103 cpu_need_resched(ci, 0);
1104 }
1105
1106 static void
1107 sched_changepri(struct lwp *l, pri_t pri)
1108 {
1109
1110 KASSERT(lwp_locked(l, NULL));
1111
1112 if (l->l_stat == LSRUN) {
1113 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
1114 sched_dequeue(l);
1115 l->l_priority = pri;
1116 sched_enqueue(l, false);
1117 } else {
1118 l->l_priority = pri;
1119 }
1120 resched_cpu(l);
1121 }
1122
1123 static void
1124 sched_lendpri(struct lwp *l, pri_t pri)
1125 {
1126
1127 KASSERT(lwp_locked(l, NULL));
1128
1129 if (l->l_stat == LSRUN) {
1130 KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
1131 sched_dequeue(l);
1132 l->l_inheritedprio = pri;
1133 sched_enqueue(l, false);
1134 } else {
1135 l->l_inheritedprio = pri;
1136 }
1137 resched_cpu(l);
1138 }
1139
1140 struct lwp *
1141 syncobj_noowner(wchan_t wchan)
1142 {
1143
1144 return NULL;
1145 }
1146
1147 /* Decay 95% of proc::p_pctcpu in 60 seconds, ccpu = exp(-1/20) */
1148 const fixpt_t ccpu = 0.95122942450071400909 * FSCALE;
1149
1150 /*
1151 * Constants for averages over 1, 5 and 15 minutes when sampling at
1152 * 5 second intervals.
1153 */
1154 static const fixpt_t cexp[ ] = {
1155 0.9200444146293232 * FSCALE, /* exp(-1/12) */
1156 0.9834714538216174 * FSCALE, /* exp(-1/60) */
1157 0.9944598480048967 * FSCALE, /* exp(-1/180) */
1158 };
1159
1160 /*
1161 * sched_pstats:
1162 *
1163 * => Update process statistics and check CPU resource allocation.
1164 * => Call scheduler-specific hook to eventually adjust LWP priorities.
1165 * => Compute load average of a quantity on 1, 5 and 15 minute intervals.
1166 */
1167 void
1168 sched_pstats(void)
1169 {
1170 extern struct loadavg averunnable;
1171 struct loadavg *avg = &averunnable;
1172 const int clkhz = (stathz != 0 ? stathz : hz);
1173 static bool backwards = false;
1174 static u_int lavg_count = 0;
1175 struct proc *p;
1176 int nrun;
1177
1178 sched_pstats_ticks++;
1179 if (++lavg_count >= 5) {
1180 lavg_count = 0;
1181 nrun = 0;
1182 }
1183 mutex_enter(proc_lock);
1184 PROCLIST_FOREACH(p, &allproc) {
1185 struct lwp *l;
1186 struct rlimit *rlim;
1187 long runtm;
1188 int sig;
1189
1190 /* Increment sleep time (if sleeping), ignore overflow. */
1191 mutex_enter(p->p_lock);
1192 runtm = p->p_rtime.sec;
1193 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1194 fixpt_t lpctcpu;
1195 u_int lcpticks;
1196
1197 if (__predict_false((l->l_flag & LW_IDLE) != 0))
1198 continue;
1199 lwp_lock(l);
1200 runtm += l->l_rtime.sec;
1201 l->l_swtime++;
1202 sched_lwp_stats(l);
1203
1204 /* For load average calculation. */
1205 if (__predict_false(lavg_count == 0) &&
1206 (l->l_flag & (LW_SINTR | LW_SYSTEM)) == 0) {
1207 switch (l->l_stat) {
1208 case LSSLEEP:
1209 if (l->l_slptime > 1) {
1210 break;
1211 }
1212 case LSRUN:
1213 case LSONPROC:
1214 case LSIDL:
1215 nrun++;
1216 }
1217 }
1218 lwp_unlock(l);
1219
1220 l->l_pctcpu = (l->l_pctcpu * ccpu) >> FSHIFT;
1221 if (l->l_slptime != 0)
1222 continue;
1223
1224 lpctcpu = l->l_pctcpu;
1225 lcpticks = atomic_swap_uint(&l->l_cpticks, 0);
1226 lpctcpu += ((FSCALE - ccpu) *
1227 (lcpticks * FSCALE / clkhz)) >> FSHIFT;
1228 l->l_pctcpu = lpctcpu;
1229 }
1230 /* Calculating p_pctcpu only for ps(1) */
1231 p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
1232
1233 /*
1234 * Check if the process exceeds its CPU resource allocation.
1235 * If over max, kill it.
1236 */
1237 rlim = &p->p_rlimit[RLIMIT_CPU];
1238 sig = 0;
1239 if (__predict_false(runtm >= rlim->rlim_cur)) {
1240 if (runtm >= rlim->rlim_max)
1241 sig = SIGKILL;
1242 else {
1243 sig = SIGXCPU;
1244 if (rlim->rlim_cur < rlim->rlim_max)
1245 rlim->rlim_cur += 5;
1246 }
1247 }
1248 mutex_exit(p->p_lock);
1249 if (__predict_false(runtm < 0)) {
1250 if (!backwards) {
1251 backwards = true;
1252 printf("WARNING: negative runtime; "
1253 "monotonic clock has gone backwards\n");
1254 }
1255 } else if (__predict_false(sig)) {
1256 KASSERT((p->p_flag & PK_SYSTEM) == 0);
1257 psignal(p, sig);
1258 }
1259 }
1260 mutex_exit(proc_lock);
1261
1262 /* Load average calculation. */
1263 if (__predict_false(lavg_count == 0)) {
1264 int i;
1265 CTASSERT(__arraycount(cexp) == __arraycount(avg->ldavg));
1266 for (i = 0; i < __arraycount(cexp); i++) {
1267 avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
1268 nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
1269 }
1270 }
1271
1272 /* Lightning bolt. */
1273 cv_broadcast(&lbolt);
1274 }
1275