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