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