#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.366 2023/11/22 13:18:48 riastradh Exp $");
#include "opt_kstack.h"
#include "opt_ddb.h"
#include "opt_dtrace.h"
#define __MUTEX_PRIVATE
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/cpu.h>
#include <sys/dtrace_bsd.h>
#include <sys/evcnt.h>
#include <sys/intr.h>
#include <sys/kernel.h>
#include <sys/lockdebug.h>
#include <sys/lwpctl.h>
#include <sys/proc.h>
#include <sys/pserialize.h>
#include <sys/resource.h>
#include <sys/resourcevar.h>
#include <sys/rwlock.h>
#include <sys/sched.h>
#include <sys/sleepq.h>
#include <sys/syncobj.h>
#include <sys/syscall_stats.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <dev/lockstat.h>
int dtrace_vtime_active=0;
dtrace_vtime_switch_func_t dtrace_vtime_switch_func;
#ifdef DDB
#include <ddb/ddb.h>
#endif
static void sched_unsleep(struct lwp *, bool);
static void sched_changepri(struct lwp *, pri_t);
static void sched_lendpri(struct lwp *, pri_t);
syncobj_t sleep_syncobj = {
.sobj_name = "sleep",
.sobj_flag = SOBJ_SLEEPQ_SORTED,
.sobj_boostpri = PRI_KERNEL,
.sobj_unsleep = sleepq_unsleep,
.sobj_changepri = sleepq_changepri,
.sobj_lendpri = sleepq_lendpri,
.sobj_owner = syncobj_noowner,
};
syncobj_t sched_syncobj = {
.sobj_name = "sched",
.sobj_flag = SOBJ_SLEEPQ_SORTED,
.sobj_boostpri = PRI_USER,
.sobj_unsleep = sched_unsleep,
.sobj_changepri = sched_changepri,
.sobj_lendpri = sched_lendpri,
.sobj_owner = syncobj_noowner,
};
syncobj_t kpause_syncobj = {
.sobj_name = "kpause",
.sobj_flag = SOBJ_SLEEPQ_NULL,
.sobj_boostpri = PRI_KERNEL,
.sobj_unsleep = sleepq_unsleep,
.sobj_changepri = sleepq_changepri,
.sobj_lendpri = sleepq_lendpri,
.sobj_owner = syncobj_noowner,
};
kcondvar_t lbolt __cacheline_aligned;
u_int sched_pstats_ticks __cacheline_aligned;
static struct evcnt kpreempt_ev_crit __cacheline_aligned;
static struct evcnt kpreempt_ev_klock __cacheline_aligned;
static struct evcnt kpreempt_ev_immed __cacheline_aligned;
void
synch_init(void)
{
cv_init(&lbolt, "lbolt");
evcnt_attach_dynamic(&kpreempt_ev_crit, EVCNT_TYPE_MISC, NULL,
"kpreempt", "defer: critical section");
evcnt_attach_dynamic(&kpreempt_ev_klock, EVCNT_TYPE_MISC, NULL,
"kpreempt", "defer: kernel_lock");
evcnt_attach_dynamic(&kpreempt_ev_immed, EVCNT_TYPE_MISC, NULL,
"kpreempt", "immediate");
}
int
tsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo)
{
struct lwp *l = curlwp;
sleepq_t *sq;
kmutex_t *mp;
bool catch_p;
int nlocks;
KASSERT((l->l_pflag & LP_INTR) == 0);
KASSERT(ident != &lbolt);
if (sleepq_dontsleep(l)) {
(void)sleepq_abort(NULL, 0);
return 0;
}
catch_p = priority & PCATCH;
sq = sleeptab_lookup(&sleeptab, ident, &mp);
nlocks = sleepq_enter(sq, l, mp);
sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj, catch_p);
return sleepq_block(timo, catch_p, &sleep_syncobj, nlocks);
}
int
mtsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
kmutex_t *mtx)
{
struct lwp *l = curlwp;
sleepq_t *sq;
kmutex_t *mp;
bool catch_p;
int error, nlocks;
KASSERT((l->l_pflag & LP_INTR) == 0);
KASSERT(ident != &lbolt);
if (sleepq_dontsleep(l)) {
(void)sleepq_abort(mtx, (priority & PNORELOCK) != 0);
return 0;
}
catch_p = priority & PCATCH;
sq = sleeptab_lookup(&sleeptab, ident, &mp);
nlocks = sleepq_enter(sq, l, mp);
sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj, catch_p);
mutex_exit(mtx);
error = sleepq_block(timo, catch_p, &sleep_syncobj, nlocks);
if ((priority & PNORELOCK) == 0)
mutex_enter(mtx);
return error;
}
int
kpause(const char *wmesg, bool intr, int timo, kmutex_t *mtx)
{
struct lwp *l = curlwp;
int error, nlocks;
KASSERTMSG(timo != 0 || intr, "wmesg=%s intr=%s timo=%d mtx=%p",
wmesg, intr ? "true" : "false", timo, mtx);
if (sleepq_dontsleep(l))
return sleepq_abort(NULL, 0);
if (mtx != NULL)
mutex_exit(mtx);
nlocks = sleepq_enter(NULL, l, NULL);
sleepq_enqueue(NULL, l, wmesg, &kpause_syncobj, intr);
error = sleepq_block(timo, intr, &kpause_syncobj, nlocks);
if (mtx != NULL)
mutex_enter(mtx);
return error;
}
void
wakeup(wchan_t ident)
{
sleepq_t *sq;
kmutex_t *mp;
if (__predict_false(cold))
return;
sq = sleeptab_lookup(&sleeptab, ident, &mp);
sleepq_wake(sq, ident, (u_int)-1, mp);
}
void
yield(void)
{
struct lwp *l = curlwp;
int nlocks;
KERNEL_UNLOCK_ALL(l, &nlocks);
lwp_lock(l);
KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
KASSERT(l->l_stat == LSONPROC);
spc_lock(l->l_cpu);
mi_switch(l);
KERNEL_LOCK(nlocks, l);
}
void
preempt(void)
{
struct lwp *l = curlwp;
int nlocks;
KERNEL_UNLOCK_ALL(l, &nlocks);
lwp_lock(l);
KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
KASSERT(l->l_stat == LSONPROC);
spc_lock(l->l_cpu);
l->l_pflag |= LP_PREEMPTING;
mi_switch(l);
KERNEL_LOCK(nlocks, l);
}
inline bool
preempt_needed(void)
{
lwp_t *l = curlwp;
int needed;
KPREEMPT_DISABLE(l);
needed = l->l_cpu->ci_want_resched;
KPREEMPT_ENABLE(l);
return (needed != 0);
}
void
preempt_point(void)
{
if (__predict_false(preempt_needed())) {
preempt();
}
}
static char kpreempt_is_disabled;
static char kernel_lock_held;
static char is_softint_lwp;
static char spl_is_raised;
bool
kpreempt(uintptr_t where)
{
uintptr_t failed;
lwp_t *l;
int s, dop, lsflag;
l = curlwp;
failed = 0;
while ((dop = l->l_dopreempt) != 0) {
if (l->l_stat != LSONPROC) {
atomic_swap_uint(&l->l_dopreempt, 0);
return true;
}
KASSERT((l->l_flag & LW_IDLE) == 0);
if (__predict_false(l->l_nopreempt != 0)) {
if ((dop & DOPREEMPT_COUNTED) == 0) {
kpreempt_ev_crit.ev_count++;
}
failed = (uintptr_t)&kpreempt_is_disabled;
break;
}
if (__predict_false((l->l_pflag & LP_INTR) != 0)) {
atomic_swap_uint(&l->l_dopreempt, 0);
failed = (uintptr_t)&is_softint_lwp;
break;
}
s = splsched();
if (__predict_false(l->l_blcnt != 0 ||
curcpu()->ci_biglock_wanted != NULL)) {
splx(s);
if ((dop & DOPREEMPT_COUNTED) == 0) {
kpreempt_ev_klock.ev_count++;
}
failed = (uintptr_t)&kernel_lock_held;
break;
}
if (__predict_false(!cpu_kpreempt_enter(where, s))) {
splx(s);
failed = (uintptr_t)&spl_is_raised;
break;
}
if (__predict_true((dop & DOPREEMPT_COUNTED) == 0)) {
kpreempt_ev_immed.ev_count++;
}
lwp_lock(l);
l->l_pflag |= LP_PREEMPTING;
spc_lock(l->l_cpu);
mi_switch(l);
l->l_nopreempt++;
splx(s);
cpu_kpreempt_exit(where);
l->l_nopreempt--;
}
if (__predict_true(!failed)) {
return false;
}
atomic_or_uint(&l->l_dopreempt, DOPREEMPT_COUNTED);
lsflag = 0;
LOCKSTAT_ENTER(lsflag);
if (__predict_false(lsflag)) {
if (where == 0) {
where = (uintptr_t)__builtin_return_address(0);
}
if (atomic_cas_ptr_ni((void *)&l->l_pfailaddr, NULL,
(void *)where) == NULL) {
LOCKSTAT_START_TIMER(lsflag, l->l_pfailtime);
l->l_pfaillock = failed;
}
}
LOCKSTAT_EXIT(lsflag);
return true;
}
bool
kpreempt_disabled(void)
{
const lwp_t *l = curlwp;
return l->l_nopreempt != 0 || l->l_stat == LSZOMB ||
(l->l_flag & LW_IDLE) != 0 || (l->l_pflag & LP_INTR) != 0 ||
cpu_kpreempt_disabled();
}
void
kpreempt_disable(void)
{
KPREEMPT_DISABLE(curlwp);
}
void
kpreempt_enable(void)
{
KPREEMPT_ENABLE(curlwp);
}
void
updatertime(lwp_t *l, const struct bintime *now)
{
static bool backwards = false;
if (__predict_false(l->l_flag & LW_IDLE))
return;
if (__predict_false(bintimecmp(now, &l->l_stime, <)) && !backwards) {
char caller[128];
#ifdef DDB
db_symstr(caller, sizeof(caller),
(db_expr_t)(intptr_t)__builtin_return_address(0),
DB_STGY_PROC);
#else
snprintf(caller, sizeof(caller), "%p",
__builtin_return_address(0));
#endif
backwards = true;
printf("WARNING: lwp %ld (%s%s%s) flags 0x%x:"
" timecounter went backwards"
" from (%jd + 0x%016"PRIx64"/2^64) sec"
" to (%jd + 0x%016"PRIx64"/2^64) sec"
" in %s\n",
(long)l->l_lid,
l->l_proc->p_comm,
l->l_name ? " " : "",
l->l_name ? l->l_name : "",
l->l_pflag,
(intmax_t)l->l_stime.sec, l->l_stime.frac,
(intmax_t)now->sec, now->frac,
caller);
}
bintime_add(&l->l_rtime, now);
bintime_sub(&l->l_rtime, &l->l_stime);
}
static inline lwp_t *
nextlwp(struct cpu_info *ci, struct schedstate_percpu *spc)
{
lwp_t *newl;
newl = sched_nextlwp();
if (newl != NULL) {
sched_dequeue(newl);
KASSERT(lwp_locked(newl, spc->spc_mutex));
KASSERT(newl->l_cpu == ci);
newl->l_stat = LSONPROC;
newl->l_pflag |= LP_RUNNING;
newl->l_boostpri = PRI_NONE;
spc->spc_curpriority = lwp_eprio(newl);
spc->spc_flags &= ~(SPCF_SWITCHCLEAR | SPCF_IDLE);
lwp_setlock(newl, spc->spc_lwplock);
} else {
newl = ci->ci_data.cpu_idlelwp;
newl->l_pflag |= LP_RUNNING;
spc->spc_curpriority = PRI_IDLE;
spc->spc_flags = (spc->spc_flags & ~SPCF_SWITCHCLEAR) |
SPCF_IDLE;
}
if (ci->ci_data.cpu_softints == 0)
ci->ci_want_resched = 0;
return newl;
}
void
mi_switch(lwp_t *l)
{
struct cpu_info *ci;
struct schedstate_percpu *spc;
struct lwp *newl;
kmutex_t *lock;
int oldspl;
struct bintime bt;
bool returning;
KASSERT(lwp_locked(l, NULL));
KASSERT(kpreempt_disabled());
KASSERT(mutex_owned(curcpu()->ci_schedstate.spc_mutex));
KASSERTMSG(l->l_blcnt == 0, "kernel_lock leaked");
kstack_check_magic(l);
binuptime(&bt);
KASSERTMSG(l == curlwp, "l %p curlwp %p", l, curlwp);
KASSERT((l->l_pflag & LP_RUNNING) != 0);
KASSERT(l->l_cpu == curcpu() || l->l_stat == LSRUN);
ci = curcpu();
spc = &ci->ci_schedstate;
returning = false;
newl = NULL;
if (l->l_switchto != NULL) {
if ((l->l_pflag & LP_INTR) != 0) {
returning = true;
softint_block(l);
if ((l->l_pflag & LP_TIMEINTR) != 0)
updatertime(l, &bt);
}
newl = l->l_switchto;
l->l_switchto = NULL;
}
#ifndef __HAVE_FAST_SOFTINTS
else if (ci->ci_data.cpu_softints != 0) {
newl = softint_picklwp();
newl->l_stat = LSONPROC;
newl->l_pflag |= LP_RUNNING;
}
#endif
if (l->l_stat == LSONPROC && l != newl) {
KASSERT(lwp_locked(l, spc->spc_lwplock));
KASSERT((l->l_flag & LW_IDLE) == 0);
l->l_stat = LSRUN;
lwp_setlock(l, spc->spc_mutex);
sched_enqueue(l);
sched_preempted(l);
if (l->l_target_cpu != NULL && (l->l_pflag & LP_BOUND) == 0) {
KASSERT((l->l_pflag & LP_INTR) == 0);
spc->spc_migrating = l;
}
}
if (newl == NULL) {
newl = nextlwp(ci, spc);
}
if (!returning) {
SYSCALL_TIME_SLEEP(l);
updatertime(l, &bt);
newl->l_stime = bt;
ci->ci_onproc = newl;
}
l->l_dopreempt = 0;
if (__predict_false(l->l_pfailaddr != 0)) {
LOCKSTAT_FLAG(lsflag);
LOCKSTAT_ENTER(lsflag);
LOCKSTAT_STOP_TIMER(lsflag, l->l_pfailtime);
LOCKSTAT_EVENT_RA(lsflag, l->l_pfaillock, LB_NOPREEMPT|LB_SPIN,
1, l->l_pfailtime, l->l_pfailaddr);
LOCKSTAT_EXIT(lsflag);
l->l_pfailtime = 0;
l->l_pfaillock = 0;
l->l_pfailaddr = 0;
}
if (l != newl) {
struct lwp *prevlwp;
if (l->l_mutex == spc->spc_mutex) {
mutex_spin_exit(spc->spc_lwplock);
} else {
mutex_spin_exit(spc->spc_mutex);
}
LOCKDEBUG_BARRIER(l->l_mutex, 1);
CPU_COUNT(CPU_COUNT_NSWTCH, 1);
if ((l->l_pflag & LP_PREEMPTING) != 0) {
l->l_ru.ru_nivcsw++;
l->l_pflag &= ~LP_PREEMPTING;
} else {
l->l_ru.ru_nvcsw++;
}
KASSERTMSG(ci->ci_mtx_count == -1,
"%s: cpu%u: ci_mtx_count (%d) != -1 "
"(block with spin-mutex held)",
__func__, cpu_index(ci), ci->ci_mtx_count);
oldspl = MUTEX_SPIN_OLDSPL(ci);
ci->ci_mtx_count = -2;
if (l->l_lwpctl != NULL) {
l->l_lwpctl->lc_curcpu = (l->l_stat == LSZOMB ?
LWPCTL_CPU_EXITED : LWPCTL_CPU_NONE);
}
if (returning) {
l->l_pflag &= ~LP_RUNNING;
lwp_unlock(l);
} else {
pmap_deactivate(l);
}
if (__predict_false(dtrace_vtime_active)) {
(*dtrace_vtime_switch_func)(newl);
}
KASSERT(pserialize_not_in_read_section());
#ifdef MULTIPROCESSOR
KASSERT(curlwp == ci->ci_curlwp);
#endif
KASSERTMSG(l == curlwp, "l %p curlwp %p", l, curlwp);
prevlwp = cpu_switchto(l, newl, returning);
ci = curcpu();
#ifdef MULTIPROCESSOR
KASSERT(curlwp == ci->ci_curlwp);
#endif
KASSERTMSG(l == curlwp, "l %p curlwp %p prevlwp %p",
l, curlwp, prevlwp);
KASSERT(prevlwp != NULL);
KASSERT(l->l_cpu == ci);
KASSERT(ci->ci_mtx_count == -2);
KASSERT((prevlwp->l_pflag & LP_RUNNING) != 0);
lock = prevlwp->l_mutex;
if (__predict_false(prevlwp->l_stat == LSZOMB)) {
atomic_store_release(&prevlwp->l_pflag,
prevlwp->l_pflag & ~LP_RUNNING);
} else {
prevlwp->l_pflag &= ~LP_RUNNING;
}
mutex_spin_exit(lock);
pmap_activate(l);
pcu_switchpoint(l);
if (l->l_lwpctl != NULL) {
l->l_lwpctl->lc_curcpu = (int)cpu_index(ci);
l->l_lwpctl->lc_pctr++;
}
KASSERT(l->l_cpu == ci);
KASSERT(ci->ci_mtx_count == -1);
ci->ci_mtx_count = 0;
splx(oldspl);
} else {
mutex_spin_exit(spc->spc_mutex);
l->l_pflag &= ~LP_PREEMPTING;
lwp_unlock(l);
}
KASSERT(l == curlwp);
KASSERT(l->l_stat == LSONPROC || (l->l_flag & LW_IDLE) != 0);
SYSCALL_TIME_WAKEUP(l);
LOCKDEBUG_BARRIER(NULL, 1);
}
void
setrunnable(struct lwp *l)
{
struct proc *p = l->l_proc;
struct cpu_info *ci;
kmutex_t *oldlock;
KASSERT((l->l_flag & LW_IDLE) == 0);
KASSERT((l->l_flag & LW_DBGSUSPEND) == 0);
KASSERT(mutex_owned(p->p_lock));
KASSERT(lwp_locked(l, NULL));
KASSERT(l->l_mutex != l->l_cpu->ci_schedstate.spc_mutex);
switch (l->l_stat) {
case LSSTOP:
if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xsig != 0)
signotify(l);
p->p_nrlwps++;
break;
case LSSUSPENDED:
KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
l->l_flag &= ~LW_WSUSPEND;
p->p_nrlwps++;
cv_broadcast(&p->p_lwpcv);
break;
case LSSLEEP:
KASSERT(l->l_wchan != NULL);
break;
case LSIDL:
KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
break;
default:
panic("setrunnable: lwp %p state was %d", l, l->l_stat);
}
if (l->l_wchan != NULL) {
l->l_stat = LSSLEEP;
lwp_unsleep(l, true);
return;
}
if ((l->l_pflag & LP_RUNNING) != 0) {
l->l_stat = LSONPROC;
l->l_slptime = 0;
lwp_unlock(l);
return;
}
ci = sched_takecpu(l);
l->l_cpu = ci;
spc_lock(ci);
oldlock = lwp_setlock(l, l->l_cpu->ci_schedstate.spc_mutex);
sched_setrunnable(l);
l->l_stat = LSRUN;
l->l_slptime = 0;
sched_enqueue(l);
sched_resched_lwp(l, true);
mutex_spin_exit(oldlock);
}
void
suspendsched(void)
{
CPU_INFO_ITERATOR cii;
struct cpu_info *ci;
struct lwp *l;
struct proc *p;
mutex_enter(&proc_lock);
PROCLIST_FOREACH(p, &allproc) {
mutex_enter(p->p_lock);
if ((p->p_flag & PK_SYSTEM) != 0) {
mutex_exit(p->p_lock);
continue;
}
if (p->p_stat != SSTOP) {
if (p->p_stat != SZOMB && p->p_stat != SDEAD) {
p->p_pptr->p_nstopchild++;
p->p_waited = 0;
}
p->p_stat = SSTOP;
}
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
if (l == curlwp)
continue;
lwp_lock(l);
l->l_flag |= (LW_WREBOOT | LW_WSUSPEND);
if (l->l_stat == LSSLEEP &&
(l->l_flag & LW_SINTR) != 0) {
setrunnable(l);
continue;
}
lwp_unlock(l);
}
mutex_exit(p->p_lock);
}
mutex_exit(&proc_lock);
kpreempt_disable();
for (CPU_INFO_FOREACH(cii, ci)) {
spc_lock(ci);
sched_resched_cpu(ci, PRI_KERNEL, true);
}
kpreempt_enable();
}
static void
sched_unsleep(struct lwp *l, bool cleanup)
{
lwp_unlock(l);
panic("sched_unsleep");
}
static void
sched_changepri(struct lwp *l, pri_t pri)
{
struct schedstate_percpu *spc;
struct cpu_info *ci;
KASSERT(lwp_locked(l, NULL));
ci = l->l_cpu;
spc = &ci->ci_schedstate;
if (l->l_stat == LSRUN) {
KASSERT(lwp_locked(l, spc->spc_mutex));
sched_dequeue(l);
l->l_priority = pri;
sched_enqueue(l);
sched_resched_lwp(l, false);
} else if (l->l_stat == LSONPROC && l->l_class != SCHED_OTHER) {
KASSERT(lwp_locked(l, spc->spc_lwplock));
l->l_priority = pri;
spc_lock(ci);
sched_resched_cpu(ci, spc->spc_maxpriority, true);
} else {
l->l_priority = pri;
}
}
static void
sched_lendpri(struct lwp *l, pri_t pri)
{
struct schedstate_percpu *spc;
struct cpu_info *ci;
KASSERT(lwp_locked(l, NULL));
ci = l->l_cpu;
spc = &ci->ci_schedstate;
if (l->l_stat == LSRUN) {
KASSERT(lwp_locked(l, spc->spc_mutex));
sched_dequeue(l);
l->l_inheritedprio = pri;
l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
sched_enqueue(l);
sched_resched_lwp(l, false);
} else if (l->l_stat == LSONPROC && l->l_class != SCHED_OTHER) {
KASSERT(lwp_locked(l, spc->spc_lwplock));
l->l_inheritedprio = pri;
l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
spc_lock(ci);
sched_resched_cpu(ci, spc->spc_maxpriority, true);
} else {
l->l_inheritedprio = pri;
l->l_auxprio = MAX(l->l_inheritedprio, l->l_protectprio);
}
}
struct lwp *
syncobj_noowner(wchan_t wchan)
{
return NULL;
}
const fixpt_t ccpu = 0.95122942450071400909 * FSCALE;
static const fixpt_t cexp[ ] = {
0.9200444146293232 * FSCALE,
0.9834714538216174 * FSCALE,
0.9944598480048967 * FSCALE,
};
void
sched_pstats(void)
{
struct loadavg *avg = &averunnable;
const int clkhz = (stathz != 0 ? stathz : hz);
static bool backwardslwp = false;
static bool backwardsproc = false;
static u_int lavg_count = 0;
struct proc *p;
int nrun;
sched_pstats_ticks++;
if (++lavg_count >= 5) {
lavg_count = 0;
nrun = 0;
}
mutex_enter(&proc_lock);
PROCLIST_FOREACH(p, &allproc) {
struct lwp *l;
struct rlimit *rlim;
time_t runtm;
int sig;
mutex_enter(p->p_lock);
runtm = p->p_rtime.sec;
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
fixpt_t lpctcpu;
u_int lcpticks;
if (__predict_false((l->l_flag & LW_IDLE) != 0))
continue;
lwp_lock(l);
if (__predict_false(l->l_rtime.sec < 0) &&
!backwardslwp) {
backwardslwp = true;
printf("WARNING: lwp %ld (%s%s%s): "
"negative runtime: "
"(%jd + 0x%016"PRIx64"/2^64) sec\n",
(long)l->l_lid,
l->l_proc->p_comm,
l->l_name ? " " : "",
l->l_name ? l->l_name : "",
(intmax_t)l->l_rtime.sec,
l->l_rtime.frac);
}
runtm += l->l_rtime.sec;
l->l_swtime++;
sched_lwp_stats(l);
if (__predict_false(lavg_count == 0) &&
(l->l_flag & (LW_SINTR | LW_SYSTEM)) == 0) {
switch (l->l_stat) {
case LSSLEEP:
if (l->l_slptime > 1) {
break;
}
case LSRUN:
case LSONPROC:
case LSIDL:
nrun++;
}
}
lwp_unlock(l);
l->l_pctcpu = (l->l_pctcpu * ccpu) >> FSHIFT;
if (l->l_slptime != 0)
continue;
lpctcpu = l->l_pctcpu;
lcpticks = atomic_swap_uint(&l->l_cpticks, 0);
lpctcpu += ((FSCALE - ccpu) *
(lcpticks * FSCALE / clkhz)) >> FSHIFT;
l->l_pctcpu = lpctcpu;
}
p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
if (__predict_false(runtm < 0)) {
if (!backwardsproc) {
backwardsproc = true;
printf("WARNING: pid %ld (%s): "
"negative runtime; "
"monotonic clock has gone backwards\n",
(long)p->p_pid, p->p_comm);
}
mutex_exit(p->p_lock);
continue;
}
rlim = &p->p_rlimit[RLIMIT_CPU];
sig = 0;
if (__predict_false(runtm >= rlim->rlim_cur)) {
if (runtm >= rlim->rlim_max) {
sig = SIGKILL;
log(LOG_NOTICE,
"pid %d, command %s, is killed: %s\n",
p->p_pid, p->p_comm, "exceeded RLIMIT_CPU");
uprintf("pid %d, command %s, is killed: %s\n",
p->p_pid, p->p_comm, "exceeded RLIMIT_CPU");
} else {
sig = SIGXCPU;
if (rlim->rlim_cur < rlim->rlim_max)
rlim->rlim_cur += 5;
}
}
mutex_exit(p->p_lock);
if (__predict_false(sig)) {
KASSERT((p->p_flag & PK_SYSTEM) == 0);
psignal(p, sig);
}
}
if (__predict_false(lavg_count == 0)) {
int i;
CTASSERT(__arraycount(cexp) == __arraycount(avg->ldavg));
for (i = 0; i < __arraycount(cexp); i++) {
avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
}
}
cv_broadcast(&lbolt);
mutex_exit(&proc_lock);
}