Home | History | Annotate | Line # | Download | only in kern
sys_sched.c revision 1.48
      1  1.48   thorpej /*	$NetBSD: sys_sched.c,v 1.48 2020/04/29 01:53:48 thorpej Exp $	*/
      2   1.1        ad 
      3   1.5     rmind /*
      4  1.36     rmind  * Copyright (c) 2008, 2011 Mindaugas Rasiukevicius <rmind at NetBSD org>
      5   1.1        ad  * All rights reserved.
      6   1.5     rmind  *
      7   1.1        ad  * Redistribution and use in source and binary forms, with or without
      8   1.1        ad  * modification, are permitted provided that the following conditions
      9   1.1        ad  * are met:
     10   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     11   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     12   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        ad  *    documentation and/or other materials provided with the distribution.
     15   1.1        ad  *
     16  1.16     rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.16     rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.16     rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.16     rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.16     rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.16     rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.16     rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.16     rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.16     rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.16     rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.16     rmind  * SUCH DAMAGE.
     27   1.1        ad  */
     28   1.1        ad 
     29   1.5     rmind /*
     30  1.17        ad  * System calls relating to the scheduler.
     31  1.17        ad  *
     32  1.31     rmind  * Lock order:
     33  1.31     rmind  *
     34  1.31     rmind  *	cpu_lock ->
     35  1.31     rmind  *	    proc_lock ->
     36  1.31     rmind  *		proc_t::p_lock ->
     37  1.31     rmind  *		    lwp_t::lwp_lock
     38  1.31     rmind  *
     39   1.5     rmind  * TODO:
     40   1.5     rmind  *  - Handle pthread_setschedprio() as defined by POSIX;
     41   1.5     rmind  */
     42   1.5     rmind 
     43   1.1        ad #include <sys/cdefs.h>
     44  1.48   thorpej __KERNEL_RCSID(0, "$NetBSD: sys_sched.c,v 1.48 2020/04/29 01:53:48 thorpej Exp $");
     45   1.1        ad 
     46   1.1        ad #include <sys/param.h>
     47   1.5     rmind 
     48   1.5     rmind #include <sys/cpu.h>
     49   1.5     rmind #include <sys/kauth.h>
     50   1.5     rmind #include <sys/kmem.h>
     51   1.5     rmind #include <sys/lwp.h>
     52   1.5     rmind #include <sys/mutex.h>
     53   1.1        ad #include <sys/proc.h>
     54   1.5     rmind #include <sys/pset.h>
     55   1.5     rmind #include <sys/sched.h>
     56   1.1        ad #include <sys/syscallargs.h>
     57   1.5     rmind #include <sys/sysctl.h>
     58   1.5     rmind #include <sys/systm.h>
     59   1.5     rmind #include <sys/types.h>
     60   1.5     rmind #include <sys/unistd.h>
     61   1.5     rmind 
     62  1.34      elad static struct sysctllog *sched_sysctl_log;
     63  1.34      elad static kauth_listener_t sched_listener;
     64  1.34      elad 
     65   1.5     rmind /*
     66   1.7     rmind  * Convert user priority or the in-kernel priority or convert the current
     67   1.7     rmind  * priority to the appropriate range according to the policy change.
     68   1.7     rmind  */
     69   1.7     rmind static pri_t
     70   1.7     rmind convert_pri(lwp_t *l, int policy, pri_t pri)
     71   1.7     rmind {
     72   1.7     rmind 
     73  1.29     rmind 	/* Convert user priority to the in-kernel */
     74   1.7     rmind 	if (pri != PRI_NONE) {
     75  1.29     rmind 		/* Only for real-time threads */
     76   1.7     rmind 		KASSERT(pri >= SCHED_PRI_MIN && pri <= SCHED_PRI_MAX);
     77  1.29     rmind 		KASSERT(policy != SCHED_OTHER);
     78  1.29     rmind 		return PRI_USER_RT + pri;
     79   1.7     rmind 	}
     80  1.29     rmind 
     81  1.29     rmind 	/* Neither policy, nor priority change */
     82   1.7     rmind 	if (l->l_class == policy)
     83   1.7     rmind 		return l->l_priority;
     84   1.7     rmind 
     85  1.29     rmind 	/* Time-sharing -> real-time */
     86   1.7     rmind 	if (l->l_class == SCHED_OTHER) {
     87   1.7     rmind 		KASSERT(policy == SCHED_FIFO || policy == SCHED_RR);
     88  1.29     rmind 		return PRI_USER_RT;
     89   1.7     rmind 	}
     90  1.29     rmind 
     91  1.29     rmind 	/* Real-time -> time-sharing */
     92   1.7     rmind 	if (policy == SCHED_OTHER) {
     93   1.7     rmind 		KASSERT(l->l_class == SCHED_FIFO || l->l_class == SCHED_RR);
     94  1.41      yamt 		/*
     95  1.41      yamt 		 * this is a bit arbitrary because the priority is dynamic
     96  1.41      yamt 		 * for SCHED_OTHER threads and will likely be changed by
     97  1.41      yamt 		 * the scheduler soon anyway.
     98  1.41      yamt 		 */
     99  1.29     rmind 		return l->l_priority - PRI_USER_RT;
    100   1.7     rmind 	}
    101  1.29     rmind 
    102  1.29     rmind 	/* Real-time -> real-time */
    103  1.29     rmind 	return l->l_priority;
    104   1.7     rmind }
    105   1.7     rmind 
    106   1.5     rmind int
    107  1.18      elad do_sched_setparam(pid_t pid, lwpid_t lid, int policy,
    108  1.18      elad     const struct sched_param *params)
    109   1.5     rmind {
    110   1.5     rmind 	struct proc *p;
    111   1.5     rmind 	struct lwp *t;
    112  1.18      elad 	pri_t pri;
    113   1.5     rmind 	u_int lcnt;
    114   1.5     rmind 	int error;
    115   1.5     rmind 
    116  1.18      elad 	error = 0;
    117  1.18      elad 
    118  1.18      elad 	pri = params->sched_priority;
    119   1.7     rmind 
    120   1.7     rmind 	/* If no parameters specified, just return (this should not happen) */
    121   1.7     rmind 	if (pri == PRI_NONE && policy == SCHED_NONE)
    122   1.7     rmind 		return 0;
    123   1.5     rmind 
    124   1.7     rmind 	/* Validate scheduling class */
    125   1.7     rmind 	if (policy != SCHED_NONE && (policy < SCHED_OTHER || policy > SCHED_RR))
    126   1.7     rmind 		return EINVAL;
    127   1.5     rmind 
    128   1.7     rmind 	/* Validate priority */
    129   1.7     rmind 	if (pri != PRI_NONE && (pri < SCHED_PRI_MIN || pri > SCHED_PRI_MAX))
    130   1.7     rmind 		return EINVAL;
    131   1.5     rmind 
    132  1.18      elad 	if (pid != 0) {
    133   1.7     rmind 		/* Find the process */
    134  1.20        ad 		mutex_enter(proc_lock);
    135  1.35     rmind 		p = proc_find(pid);
    136  1.20        ad 		if (p == NULL) {
    137  1.20        ad 			mutex_exit(proc_lock);
    138   1.7     rmind 			return ESRCH;
    139  1.20        ad 		}
    140  1.21        ad 		mutex_enter(p->p_lock);
    141  1.20        ad 		mutex_exit(proc_lock);
    142   1.7     rmind 		/* Disallow modification of system processes */
    143  1.17        ad 		if ((p->p_flag & PK_SYSTEM) != 0) {
    144  1.21        ad 			mutex_exit(p->p_lock);
    145   1.7     rmind 			return EPERM;
    146   1.7     rmind 		}
    147   1.7     rmind 	} else {
    148   1.7     rmind 		/* Use the calling process */
    149  1.18      elad 		p = curlwp->l_proc;
    150  1.21        ad 		mutex_enter(p->p_lock);
    151   1.5     rmind 	}
    152   1.1        ad 
    153   1.5     rmind 	/* Find the LWP(s) */
    154   1.5     rmind 	lcnt = 0;
    155   1.5     rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    156   1.7     rmind 		pri_t kpri;
    157  1.12      elad 		int lpolicy;
    158   1.5     rmind 
    159   1.5     rmind 		if (lid && lid != t->l_lid)
    160   1.5     rmind 			continue;
    161  1.29     rmind 
    162  1.15  drochner 		lcnt++;
    163   1.7     rmind 		lwp_lock(t);
    164  1.29     rmind 		lpolicy = (policy == SCHED_NONE) ? t->l_class : policy;
    165  1.29     rmind 
    166  1.29     rmind 		/* Disallow setting of priority for SCHED_OTHER threads */
    167  1.30     rmind 		if (lpolicy == SCHED_OTHER && pri != PRI_NONE) {
    168  1.29     rmind 			lwp_unlock(t);
    169  1.29     rmind 			error = EINVAL;
    170  1.29     rmind 			break;
    171  1.29     rmind 		}
    172   1.7     rmind 
    173  1.29     rmind 		/* Convert priority, if needed */
    174  1.12      elad 		kpri = convert_pri(t, lpolicy, pri);
    175  1.12      elad 
    176  1.12      elad 		/* Check the permission */
    177  1.18      elad 		error = kauth_authorize_process(kauth_cred_get(),
    178  1.12      elad 		    KAUTH_PROCESS_SCHEDULER_SETPARAM, p, t, KAUTH_ARG(lpolicy),
    179  1.12      elad 		    KAUTH_ARG(kpri));
    180  1.14      yamt 		if (error) {
    181  1.14      yamt 			lwp_unlock(t);
    182  1.12      elad 			break;
    183  1.14      yamt 		}
    184   1.5     rmind 
    185  1.29     rmind 		/* Set the scheduling class, change the priority */
    186  1.29     rmind 		t->l_class = lpolicy;
    187  1.29     rmind 		lwp_changepri(t, kpri);
    188   1.5     rmind 		lwp_unlock(t);
    189   1.5     rmind 	}
    190  1.21        ad 	mutex_exit(p->p_lock);
    191   1.7     rmind 	return (lcnt == 0) ? ESRCH : error;
    192   1.5     rmind }
    193   1.5     rmind 
    194   1.5     rmind /*
    195  1.18      elad  * Set scheduling parameters.
    196   1.5     rmind  */
    197   1.5     rmind int
    198  1.18      elad sys__sched_setparam(struct lwp *l, const struct sys__sched_setparam_args *uap,
    199   1.5     rmind     register_t *retval)
    200   1.5     rmind {
    201   1.5     rmind 	/* {
    202   1.5     rmind 		syscallarg(pid_t) pid;
    203   1.5     rmind 		syscallarg(lwpid_t) lid;
    204  1.18      elad 		syscallarg(int) policy;
    205  1.18      elad 		syscallarg(const struct sched_param *) params;
    206   1.5     rmind 	} */
    207  1.18      elad 	struct sched_param params;
    208  1.18      elad 	int error;
    209  1.18      elad 
    210  1.18      elad 	/* Get the parameters from the user-space */
    211  1.18      elad 	error = copyin(SCARG(uap, params), &params, sizeof(params));
    212  1.18      elad 	if (error)
    213  1.18      elad 		goto out;
    214  1.18      elad 
    215  1.18      elad 	error = do_sched_setparam(SCARG(uap, pid), SCARG(uap, lid),
    216  1.18      elad 	    SCARG(uap, policy), &params);
    217  1.31     rmind out:
    218  1.31     rmind 	return error;
    219  1.18      elad }
    220  1.18      elad 
    221  1.41      yamt /*
    222  1.41      yamt  * do_sched_getparam:
    223  1.41      yamt  *
    224  1.41      yamt  * if lid=0, returns the parameter of the first LWP in the process.
    225  1.41      yamt  */
    226  1.18      elad int
    227  1.18      elad do_sched_getparam(pid_t pid, lwpid_t lid, int *policy,
    228  1.18      elad     struct sched_param *params)
    229  1.18      elad {
    230  1.18      elad 	struct sched_param lparams;
    231   1.5     rmind 	struct lwp *t;
    232  1.18      elad 	int error, lpolicy;
    233   1.5     rmind 
    234  1.48   thorpej 	if (pid < 0 || lid < 0)
    235  1.48   thorpej 		return EINVAL;
    236  1.48   thorpej 
    237  1.41      yamt 	t = lwp_find2(pid, lid); /* acquire p_lock */
    238  1.21        ad 	if (t == NULL)
    239  1.21        ad 		return ESRCH;
    240  1.10      yamt 
    241  1.10      yamt 	/* Check the permission */
    242  1.18      elad 	error = kauth_authorize_process(kauth_cred_get(),
    243  1.11      elad 	    KAUTH_PROCESS_SCHEDULER_GETPARAM, t->l_proc, NULL, NULL, NULL);
    244  1.10      yamt 	if (error != 0) {
    245  1.21        ad 		mutex_exit(t->l_proc->p_lock);
    246  1.21        ad 		return error;
    247   1.5     rmind 	}
    248  1.10      yamt 
    249  1.21        ad 	lwp_lock(t);
    250  1.18      elad 	lparams.sched_priority = t->l_priority;
    251  1.18      elad 	lpolicy = t->l_class;
    252  1.41      yamt 	lwp_unlock(t);
    253  1.41      yamt 	mutex_exit(t->l_proc->p_lock);
    254   1.5     rmind 
    255  1.41      yamt 	/*
    256  1.41      yamt 	 * convert to the user-visible priority value.
    257  1.41      yamt 	 * it's an inversion of convert_pri().
    258  1.41      yamt 	 *
    259  1.41      yamt 	 * the SCHED_OTHER case is a bit arbitrary given that
    260  1.41      yamt 	 *	- we don't allow setting the priority.
    261  1.41      yamt 	 *	- the priority is dynamic.
    262  1.41      yamt 	 */
    263  1.18      elad 	switch (lpolicy) {
    264   1.5     rmind 	case SCHED_OTHER:
    265  1.18      elad 		lparams.sched_priority -= PRI_USER;
    266   1.5     rmind 		break;
    267   1.5     rmind 	case SCHED_RR:
    268   1.5     rmind 	case SCHED_FIFO:
    269  1.18      elad 		lparams.sched_priority -= PRI_USER_RT;
    270   1.5     rmind 		break;
    271   1.5     rmind 	}
    272  1.18      elad 
    273  1.18      elad 	if (policy != NULL)
    274  1.18      elad 		*policy = lpolicy;
    275  1.18      elad 
    276  1.18      elad 	if (params != NULL)
    277  1.18      elad 		*params = lparams;
    278  1.18      elad 
    279  1.18      elad 	return error;
    280  1.18      elad }
    281  1.18      elad 
    282  1.18      elad /*
    283  1.18      elad  * Get scheduling parameters.
    284  1.18      elad  */
    285  1.18      elad int
    286  1.18      elad sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
    287  1.18      elad     register_t *retval)
    288  1.18      elad {
    289  1.18      elad 	/* {
    290  1.18      elad 		syscallarg(pid_t) pid;
    291  1.18      elad 		syscallarg(lwpid_t) lid;
    292  1.18      elad 		syscallarg(int *) policy;
    293  1.18      elad 		syscallarg(struct sched_param *) params;
    294  1.18      elad 	} */
    295  1.18      elad 	struct sched_param params;
    296  1.18      elad 	int error, policy;
    297  1.18      elad 
    298  1.18      elad 	error = do_sched_getparam(SCARG(uap, pid), SCARG(uap, lid), &policy,
    299  1.18      elad 	    &params);
    300  1.18      elad 	if (error)
    301  1.18      elad 		goto out;
    302  1.18      elad 
    303  1.18      elad 	error = copyout(&params, SCARG(uap, params), sizeof(params));
    304  1.10      yamt 	if (error == 0 && SCARG(uap, policy) != NULL)
    305  1.10      yamt 		error = copyout(&policy, SCARG(uap, policy), sizeof(int));
    306  1.31     rmind out:
    307  1.31     rmind 	return error;
    308   1.5     rmind }
    309   1.5     rmind 
    310  1.31     rmind /*
    311  1.31     rmind  * Allocate the CPU set, and get it from userspace.
    312  1.31     rmind  */
    313  1.23  christos static int
    314  1.26  christos genkcpuset(kcpuset_t **dset, const cpuset_t *sset, size_t size)
    315  1.23  christos {
    316  1.36     rmind 	kcpuset_t *kset;
    317  1.23  christos 	int error;
    318  1.23  christos 
    319  1.42     rmind 	kcpuset_create(&kset, true);
    320  1.36     rmind 	error = kcpuset_copyin(sset, kset, size);
    321  1.36     rmind 	if (error) {
    322  1.36     rmind 		kcpuset_unuse(kset, NULL);
    323  1.36     rmind 	} else {
    324  1.36     rmind 		*dset = kset;
    325  1.36     rmind 	}
    326  1.23  christos 	return error;
    327  1.23  christos }
    328  1.23  christos 
    329   1.5     rmind /*
    330   1.5     rmind  * Set affinity.
    331   1.5     rmind  */
    332   1.5     rmind int
    333   1.5     rmind sys__sched_setaffinity(struct lwp *l,
    334   1.5     rmind     const struct sys__sched_setaffinity_args *uap, register_t *retval)
    335   1.5     rmind {
    336   1.5     rmind 	/* {
    337   1.5     rmind 		syscallarg(pid_t) pid;
    338   1.5     rmind 		syscallarg(lwpid_t) lid;
    339   1.5     rmind 		syscallarg(size_t) size;
    340  1.23  christos 		syscallarg(const cpuset_t *) cpuset;
    341   1.5     rmind 	} */
    342  1.36     rmind 	kcpuset_t *kcset, *kcpulst = NULL;
    343  1.32     rmind 	struct cpu_info *ici, *ci;
    344   1.5     rmind 	struct proc *p;
    345   1.5     rmind 	struct lwp *t;
    346   1.5     rmind 	CPU_INFO_ITERATOR cii;
    347  1.32     rmind 	bool alloff;
    348   1.5     rmind 	lwpid_t lid;
    349   1.5     rmind 	u_int lcnt;
    350   1.5     rmind 	int error;
    351   1.5     rmind 
    352  1.36     rmind 	error = genkcpuset(&kcset, SCARG(uap, cpuset), SCARG(uap, size));
    353  1.31     rmind 	if (error)
    354  1.23  christos 		return error;
    355   1.5     rmind 
    356  1.31     rmind 	/*
    357  1.32     rmind 	 * Traverse _each_ CPU to:
    358  1.32     rmind 	 *  - Check that CPUs in the mask have no assigned processor set.
    359  1.32     rmind 	 *  - Check that at least one CPU from the mask is online.
    360  1.32     rmind 	 *  - Find the first target CPU to migrate.
    361  1.31     rmind 	 *
    362  1.32     rmind 	 * To avoid the race with CPU online/offline calls and processor sets,
    363  1.32     rmind 	 * cpu_lock will be locked for the entire operation.
    364  1.31     rmind 	 */
    365  1.32     rmind 	ci = NULL;
    366  1.32     rmind 	alloff = false;
    367  1.31     rmind 	mutex_enter(&cpu_lock);
    368  1.32     rmind 	for (CPU_INFO_FOREACH(cii, ici)) {
    369  1.32     rmind 		struct schedstate_percpu *ispc;
    370  1.31     rmind 
    371  1.39     rmind 		if (!kcpuset_isset(kcset, cpu_index(ici))) {
    372  1.31     rmind 			continue;
    373  1.39     rmind 		}
    374  1.32     rmind 
    375  1.32     rmind 		ispc = &ici->ci_schedstate;
    376  1.32     rmind 		/* Check that CPU is not in the processor-set */
    377  1.32     rmind 		if (ispc->spc_psid != PS_NONE) {
    378  1.32     rmind 			error = EPERM;
    379  1.32     rmind 			goto out;
    380  1.32     rmind 		}
    381  1.32     rmind 		/* Skip offline CPUs */
    382  1.32     rmind 		if (ispc->spc_flags & SPCF_OFFLINE) {
    383  1.32     rmind 			alloff = true;
    384  1.31     rmind 			continue;
    385  1.24     rmind 		}
    386  1.32     rmind 		/* Target CPU to migrate */
    387  1.32     rmind 		if (ci == NULL) {
    388  1.32     rmind 			ci = ici;
    389  1.32     rmind 		}
    390  1.23  christos 	}
    391   1.5     rmind 	if (ci == NULL) {
    392  1.32     rmind 		if (alloff) {
    393  1.31     rmind 			/* All CPUs in the set are offline */
    394  1.31     rmind 			error = EPERM;
    395  1.31     rmind 			goto out;
    396  1.31     rmind 		}
    397   1.5     rmind 		/* Empty set */
    398  1.36     rmind 		kcpuset_unuse(kcset, &kcpulst);
    399  1.45   msaitoh 		kcset = NULL;
    400   1.5     rmind 	}
    401   1.5     rmind 
    402   1.7     rmind 	if (SCARG(uap, pid) != 0) {
    403   1.7     rmind 		/* Find the process */
    404  1.20        ad 		mutex_enter(proc_lock);
    405  1.35     rmind 		p = proc_find(SCARG(uap, pid));
    406   1.7     rmind 		if (p == NULL) {
    407  1.20        ad 			mutex_exit(proc_lock);
    408   1.7     rmind 			error = ESRCH;
    409  1.23  christos 			goto out;
    410   1.7     rmind 		}
    411  1.21        ad 		mutex_enter(p->p_lock);
    412  1.20        ad 		mutex_exit(proc_lock);
    413  1.17        ad 		/* Disallow modification of system processes. */
    414  1.17        ad 		if ((p->p_flag & PK_SYSTEM) != 0) {
    415  1.21        ad 			mutex_exit(p->p_lock);
    416  1.17        ad 			error = EPERM;
    417  1.23  christos 			goto out;
    418  1.17        ad 		}
    419   1.7     rmind 	} else {
    420   1.7     rmind 		/* Use the calling process */
    421   1.7     rmind 		p = l->l_proc;
    422  1.21        ad 		mutex_enter(p->p_lock);
    423   1.5     rmind 	}
    424   1.5     rmind 
    425  1.10      yamt 	/*
    426  1.10      yamt 	 * Check the permission.
    427  1.10      yamt 	 */
    428  1.11      elad 	error = kauth_authorize_process(l->l_cred,
    429  1.11      elad 	    KAUTH_PROCESS_SCHEDULER_SETAFFINITY, p, NULL, NULL, NULL);
    430  1.10      yamt 	if (error != 0) {
    431  1.21        ad 		mutex_exit(p->p_lock);
    432  1.23  christos 		goto out;
    433  1.10      yamt 	}
    434   1.5     rmind 
    435  1.37     rmind 	/* Iterate through LWP(s). */
    436   1.5     rmind 	lcnt = 0;
    437   1.5     rmind 	lid = SCARG(uap, lid);
    438   1.5     rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    439  1.37     rmind 		if (lid && lid != t->l_lid) {
    440   1.5     rmind 			continue;
    441  1.37     rmind 		}
    442   1.5     rmind 		lwp_lock(t);
    443  1.37     rmind 		/* No affinity for zombie LWPs. */
    444  1.27     rmind 		if (t->l_stat == LSZOMB) {
    445  1.27     rmind 			lwp_unlock(t);
    446  1.27     rmind 			continue;
    447  1.27     rmind 		}
    448  1.37     rmind 		/* First, release existing affinity, if any. */
    449  1.37     rmind 		if (t->l_affinity) {
    450  1.37     rmind 			kcpuset_unuse(t->l_affinity, &kcpulst);
    451  1.37     rmind 		}
    452  1.36     rmind 		if (kcset) {
    453  1.37     rmind 			/*
    454  1.37     rmind 			 * Hold a reference on affinity mask, assign mask to
    455  1.37     rmind 			 * LWP and migrate it to another CPU (unlocks LWP).
    456  1.37     rmind 			 */
    457  1.36     rmind 			kcpuset_use(kcset);
    458  1.36     rmind 			t->l_affinity = kcset;
    459   1.5     rmind 			lwp_migrate(t, ci);
    460   1.5     rmind 		} else {
    461  1.37     rmind 			/* Old affinity mask is released, just clear. */
    462  1.23  christos 			t->l_affinity = NULL;
    463   1.5     rmind 			lwp_unlock(t);
    464   1.5     rmind 		}
    465   1.5     rmind 		lcnt++;
    466   1.5     rmind 	}
    467  1.21        ad 	mutex_exit(p->p_lock);
    468  1.36     rmind 	if (lcnt == 0) {
    469   1.5     rmind 		error = ESRCH;
    470  1.36     rmind 	}
    471  1.23  christos out:
    472  1.31     rmind 	mutex_exit(&cpu_lock);
    473  1.36     rmind 
    474  1.36     rmind 	/*
    475  1.36     rmind 	 * Drop the initial reference (LWPs, if any, have the ownership now),
    476  1.36     rmind 	 * and destroy whatever is in the G/C list, if filled.
    477  1.36     rmind 	 */
    478  1.36     rmind 	if (kcset) {
    479  1.36     rmind 		kcpuset_unuse(kcset, &kcpulst);
    480  1.36     rmind 	}
    481  1.36     rmind 	if (kcpulst) {
    482  1.36     rmind 		kcpuset_destroy(kcpulst);
    483  1.36     rmind 	}
    484   1.5     rmind 	return error;
    485   1.5     rmind }
    486   1.5     rmind 
    487   1.5     rmind /*
    488   1.5     rmind  * Get affinity.
    489   1.5     rmind  */
    490   1.5     rmind int
    491   1.5     rmind sys__sched_getaffinity(struct lwp *l,
    492   1.5     rmind     const struct sys__sched_getaffinity_args *uap, register_t *retval)
    493   1.5     rmind {
    494   1.5     rmind 	/* {
    495   1.5     rmind 		syscallarg(pid_t) pid;
    496   1.5     rmind 		syscallarg(lwpid_t) lid;
    497   1.5     rmind 		syscallarg(size_t) size;
    498  1.23  christos 		syscallarg(cpuset_t *) cpuset;
    499   1.5     rmind 	} */
    500   1.5     rmind 	struct lwp *t;
    501  1.36     rmind 	kcpuset_t *kcset;
    502   1.5     rmind 	int error;
    503   1.5     rmind 
    504  1.48   thorpej 	if (SCARG(uap, pid) < 0 || SCARG(uap, lid) < 0)
    505  1.48   thorpej 		return EINVAL;
    506  1.48   thorpej 
    507  1.36     rmind 	error = genkcpuset(&kcset, SCARG(uap, cpuset), SCARG(uap, size));
    508  1.31     rmind 	if (error)
    509  1.23  christos 		return error;
    510   1.5     rmind 
    511  1.16     rmind 	/* Locks the LWP */
    512  1.16     rmind 	t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
    513   1.5     rmind 	if (t == NULL) {
    514  1.23  christos 		error = ESRCH;
    515  1.23  christos 		goto out;
    516   1.5     rmind 	}
    517  1.10      yamt 	/* Check the permission */
    518  1.11      elad 	if (kauth_authorize_process(l->l_cred,
    519  1.11      elad 	    KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
    520  1.21        ad 		mutex_exit(t->l_proc->p_lock);
    521  1.23  christos 		error = EPERM;
    522  1.23  christos 		goto out;
    523  1.10      yamt 	}
    524  1.21        ad 	lwp_lock(t);
    525  1.37     rmind 	if (t->l_affinity) {
    526  1.36     rmind 		kcpuset_copy(kcset, t->l_affinity);
    527  1.36     rmind 	} else {
    528  1.36     rmind 		kcpuset_zero(kcset);
    529  1.36     rmind 	}
    530   1.5     rmind 	lwp_unlock(t);
    531  1.21        ad 	mutex_exit(t->l_proc->p_lock);
    532   1.5     rmind 
    533  1.36     rmind 	error = kcpuset_copyout(kcset, SCARG(uap, cpuset), SCARG(uap, size));
    534  1.23  christos out:
    535  1.36     rmind 	kcpuset_unuse(kcset, NULL);
    536   1.5     rmind 	return error;
    537   1.5     rmind }
    538   1.5     rmind 
    539   1.5     rmind /*
    540  1.44  christos  * Priority protection for PTHREAD_PRIO_PROTECT. This is a weak
    541  1.44  christos  * analogue of priority inheritance: temp raise the priority
    542  1.44  christos  * of the caller when accessing a protected resource.
    543  1.44  christos  */
    544  1.44  christos int
    545  1.44  christos sys__sched_protect(struct lwp *l,
    546  1.44  christos     const struct sys__sched_protect_args *uap, register_t *retval)
    547  1.44  christos {
    548  1.44  christos         /* {
    549  1.44  christos                 syscallarg(int) priority;
    550  1.44  christos 		syscallarg(int *) opriority;
    551  1.44  christos         } */
    552  1.44  christos 	int error;
    553  1.44  christos 	pri_t pri;
    554  1.44  christos 
    555  1.44  christos 	KASSERT(l->l_inheritedprio == -1);
    556  1.44  christos 	KASSERT(l->l_auxprio == -1 || l->l_auxprio == l->l_protectprio);
    557  1.44  christos 
    558  1.44  christos 	pri = SCARG(uap, priority);
    559  1.44  christos 	error = 0;
    560  1.44  christos 	lwp_lock(l);
    561  1.44  christos 	if (pri == -1) {
    562  1.44  christos 		/* back out priority changes */
    563  1.44  christos 		switch(l->l_protectdepth) {
    564  1.44  christos 		case 0:
    565  1.44  christos 			error = EINVAL;
    566  1.44  christos 			break;
    567  1.44  christos 		case 1:
    568  1.44  christos 			l->l_protectdepth = 0;
    569  1.44  christos 			l->l_protectprio = -1;
    570  1.44  christos 			l->l_auxprio = -1;
    571  1.44  christos 			break;
    572  1.44  christos 		default:
    573  1.44  christos 			l->l_protectdepth--;
    574  1.45   msaitoh 			break;
    575  1.44  christos 		}
    576  1.44  christos 	} else if (pri < 0) {
    577  1.44  christos 		/* Just retrieve the current value, for debugging */
    578  1.46  christos 		if (l->l_protectprio == -1)
    579  1.44  christos 			error = ENOENT;
    580  1.44  christos 		else
    581  1.44  christos 			*retval = l->l_protectprio - PRI_USER_RT;
    582  1.44  christos 	} else if (__predict_false(pri < SCHED_PRI_MIN ||
    583  1.44  christos 	    pri > SCHED_PRI_MAX || l->l_priority > pri + PRI_USER_RT)) {
    584  1.44  christos 		/* must fail if existing priority is higher */
    585  1.44  christos 		error = EPERM;
    586  1.44  christos 	} else {
    587  1.44  christos 		/* play along but make no changes if not a realtime LWP. */
    588  1.44  christos 		l->l_protectdepth++;
    589  1.44  christos 		pri += PRI_USER_RT;
    590  1.44  christos 		if (__predict_true(l->l_class != SCHED_OTHER &&
    591  1.44  christos 		    pri > l->l_protectprio)) {
    592  1.44  christos 			l->l_protectprio = pri;
    593  1.44  christos 			l->l_auxprio = pri;
    594  1.44  christos 		}
    595  1.44  christos 	}
    596  1.44  christos 	lwp_unlock(l);
    597  1.44  christos 
    598  1.44  christos 	return error;
    599  1.44  christos }
    600  1.44  christos 
    601  1.44  christos /*
    602   1.5     rmind  * Yield.
    603   1.5     rmind  */
    604   1.1        ad int
    605   1.4       dsl sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    606   1.1        ad {
    607   1.1        ad 
    608   1.1        ad 	yield();
    609   1.1        ad 	return 0;
    610   1.1        ad }
    611   1.5     rmind 
    612   1.5     rmind /*
    613   1.5     rmind  * Sysctl nodes and initialization.
    614   1.5     rmind  */
    615  1.34      elad static void
    616  1.34      elad sysctl_sched_setup(struct sysctllog **clog)
    617   1.5     rmind {
    618   1.5     rmind 	const struct sysctlnode *node = NULL;
    619   1.5     rmind 
    620   1.5     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    621   1.5     rmind 		CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    622   1.5     rmind 		CTLTYPE_INT, "posix_sched",
    623   1.5     rmind 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    624   1.5     rmind 			     "Process Scheduling option to which the "
    625   1.5     rmind 			     "system attempts to conform"),
    626   1.5     rmind 		NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
    627   1.5     rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    628   1.5     rmind 	sysctl_createv(clog, 0, NULL, &node,
    629   1.5     rmind 		CTLFLAG_PERMANENT,
    630   1.5     rmind 		CTLTYPE_NODE, "sched",
    631   1.5     rmind 		SYSCTL_DESCR("Scheduler options"),
    632   1.5     rmind 		NULL, 0, NULL, 0,
    633   1.5     rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    634   1.5     rmind 
    635   1.5     rmind 	if (node == NULL)
    636   1.5     rmind 		return;
    637   1.5     rmind 
    638   1.5     rmind 	sysctl_createv(clog, 0, &node, NULL,
    639   1.5     rmind 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    640   1.5     rmind 		CTLTYPE_INT, "pri_min",
    641   1.5     rmind 		SYSCTL_DESCR("Minimal POSIX real-time priority"),
    642   1.5     rmind 		NULL, SCHED_PRI_MIN, NULL, 0,
    643   1.5     rmind 		CTL_CREATE, CTL_EOL);
    644   1.5     rmind 	sysctl_createv(clog, 0, &node, NULL,
    645   1.5     rmind 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    646   1.5     rmind 		CTLTYPE_INT, "pri_max",
    647  1.19     njoly 		SYSCTL_DESCR("Maximal POSIX real-time priority"),
    648   1.5     rmind 		NULL, SCHED_PRI_MAX, NULL, 0,
    649   1.5     rmind 		CTL_CREATE, CTL_EOL);
    650   1.5     rmind }
    651  1.34      elad 
    652  1.34      elad static int
    653  1.34      elad sched_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    654  1.34      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    655  1.34      elad {
    656  1.34      elad 	struct proc *p;
    657  1.34      elad 	int result;
    658  1.34      elad 
    659  1.34      elad 	result = KAUTH_RESULT_DEFER;
    660  1.34      elad 	p = arg0;
    661  1.34      elad 
    662  1.34      elad 	switch (action) {
    663  1.34      elad 	case KAUTH_PROCESS_SCHEDULER_GETPARAM:
    664  1.34      elad 		if (kauth_cred_uidmatch(cred, p->p_cred))
    665  1.34      elad 			result = KAUTH_RESULT_ALLOW;
    666  1.34      elad 		break;
    667  1.34      elad 
    668  1.34      elad 	case KAUTH_PROCESS_SCHEDULER_SETPARAM:
    669  1.34      elad 		if (kauth_cred_uidmatch(cred, p->p_cred)) {
    670  1.34      elad 			struct lwp *l;
    671  1.34      elad 			int policy;
    672  1.34      elad 			pri_t priority;
    673  1.34      elad 
    674  1.34      elad 			l = arg1;
    675  1.34      elad 			policy = (int)(unsigned long)arg2;
    676  1.34      elad 			priority = (pri_t)(unsigned long)arg3;
    677  1.34      elad 
    678  1.34      elad 			if ((policy == l->l_class ||
    679  1.34      elad 			    (policy != SCHED_FIFO && policy != SCHED_RR)) &&
    680  1.34      elad 			    priority <= l->l_priority)
    681  1.34      elad 				result = KAUTH_RESULT_ALLOW;
    682  1.34      elad 		}
    683  1.34      elad 
    684  1.34      elad 		break;
    685  1.34      elad 
    686  1.34      elad 	case KAUTH_PROCESS_SCHEDULER_GETAFFINITY:
    687  1.34      elad 		result = KAUTH_RESULT_ALLOW;
    688  1.34      elad 		break;
    689  1.34      elad 
    690  1.34      elad 	case KAUTH_PROCESS_SCHEDULER_SETAFFINITY:
    691  1.34      elad 		/* Privileged; we let the secmodel handle this. */
    692  1.34      elad 		break;
    693  1.34      elad 
    694  1.34      elad 	default:
    695  1.34      elad 		break;
    696  1.34      elad 	}
    697  1.34      elad 
    698  1.34      elad 	return result;
    699  1.34      elad }
    700  1.34      elad 
    701  1.34      elad void
    702  1.34      elad sched_init(void)
    703  1.34      elad {
    704  1.34      elad 
    705  1.34      elad 	sysctl_sched_setup(&sched_sysctl_log);
    706  1.34      elad 
    707  1.34      elad 	sched_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    708  1.34      elad 	    sched_listener_cb, NULL);
    709  1.34      elad }
    710