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