Home | History | Annotate | Line # | Download | only in kern
sys_sched.c revision 1.28
      1  1.28  wrstuden /*	$NetBSD: sys_sched.c,v 1.28 2008/10/15 06:51:20 wrstuden Exp $	*/
      2   1.1        ad 
      3   1.5     rmind /*
      4   1.5     rmind  * Copyright (c) 2008, 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.5     rmind  * TODO:
     33   1.5     rmind  *  - Handle pthread_setschedprio() as defined by POSIX;
     34   1.5     rmind  *  - Handle sched_yield() case for SCHED_FIFO as defined by POSIX;
     35   1.5     rmind  */
     36   1.5     rmind 
     37   1.1        ad #include <sys/cdefs.h>
     38  1.28  wrstuden __KERNEL_RCSID(0, "$NetBSD: sys_sched.c,v 1.28 2008/10/15 06:51:20 wrstuden Exp $");
     39   1.1        ad 
     40   1.1        ad #include <sys/param.h>
     41   1.5     rmind 
     42   1.5     rmind #include <sys/cpu.h>
     43   1.5     rmind #include <sys/kauth.h>
     44   1.5     rmind #include <sys/kmem.h>
     45   1.5     rmind #include <sys/lwp.h>
     46   1.5     rmind #include <sys/mutex.h>
     47   1.1        ad #include <sys/proc.h>
     48   1.5     rmind #include <sys/pset.h>
     49  1.28  wrstuden #include <sys/sa.h>
     50  1.28  wrstuden #include <sys/savar.h>
     51   1.5     rmind #include <sys/sched.h>
     52   1.1        ad #include <sys/syscallargs.h>
     53   1.5     rmind #include <sys/sysctl.h>
     54   1.5     rmind #include <sys/systm.h>
     55   1.5     rmind #include <sys/types.h>
     56   1.5     rmind #include <sys/unistd.h>
     57   1.5     rmind 
     58  1.28  wrstuden #include "opt_sa.h"
     59  1.28  wrstuden 
     60   1.5     rmind /*
     61   1.7     rmind  * Convert user priority or the in-kernel priority or convert the current
     62   1.7     rmind  * priority to the appropriate range according to the policy change.
     63   1.7     rmind  */
     64   1.7     rmind static pri_t
     65   1.7     rmind convert_pri(lwp_t *l, int policy, pri_t pri)
     66   1.7     rmind {
     67   1.7     rmind 	int delta = 0;
     68   1.7     rmind 
     69   1.7     rmind 	switch (policy) {
     70   1.7     rmind 	case SCHED_OTHER:
     71   1.7     rmind 		delta = PRI_USER;
     72   1.7     rmind 		break;
     73   1.7     rmind 	case SCHED_FIFO:
     74   1.7     rmind 	case SCHED_RR:
     75   1.7     rmind 		delta = PRI_USER_RT;
     76   1.7     rmind 		break;
     77   1.7     rmind 	default:
     78   1.7     rmind 		panic("upri_to_kpri");
     79   1.7     rmind 	}
     80   1.7     rmind 
     81   1.7     rmind 	if (pri != PRI_NONE) {
     82   1.7     rmind 		/* Convert user priority to the in-kernel */
     83   1.7     rmind 		KASSERT(pri >= SCHED_PRI_MIN && pri <= SCHED_PRI_MAX);
     84   1.7     rmind 		return pri + delta;
     85   1.7     rmind 	}
     86   1.7     rmind 	if (l->l_class == policy)
     87   1.7     rmind 		return l->l_priority;
     88   1.7     rmind 
     89   1.7     rmind 	/* Change the current priority to the appropriate range */
     90   1.7     rmind 	if (l->l_class == SCHED_OTHER) {
     91   1.7     rmind 		KASSERT(policy == SCHED_FIFO || policy == SCHED_RR);
     92  1.22        ad 		return delta;
     93   1.7     rmind 	}
     94   1.7     rmind 	if (policy == SCHED_OTHER) {
     95   1.7     rmind 		KASSERT(l->l_class == SCHED_FIFO || l->l_class == SCHED_RR);
     96   1.7     rmind 		return l->l_priority - delta;
     97   1.7     rmind 	}
     98   1.7     rmind 	KASSERT(l->l_class != SCHED_OTHER && policy != SCHED_OTHER);
     99   1.7     rmind 	return l->l_class;
    100   1.7     rmind }
    101   1.7     rmind 
    102   1.5     rmind int
    103  1.18      elad do_sched_setparam(pid_t pid, lwpid_t lid, int policy,
    104  1.18      elad     const struct sched_param *params)
    105   1.5     rmind {
    106   1.5     rmind 	struct proc *p;
    107   1.5     rmind 	struct lwp *t;
    108  1.18      elad 	pri_t pri;
    109   1.5     rmind 	u_int lcnt;
    110   1.5     rmind 	int error;
    111   1.5     rmind 
    112  1.18      elad 	error = 0;
    113  1.18      elad 
    114  1.18      elad 	pri = params->sched_priority;
    115   1.7     rmind 
    116   1.7     rmind 	/* If no parameters specified, just return (this should not happen) */
    117   1.7     rmind 	if (pri == PRI_NONE && policy == SCHED_NONE)
    118   1.7     rmind 		return 0;
    119   1.5     rmind 
    120   1.7     rmind 	/* Validate scheduling class */
    121   1.7     rmind 	if (policy != SCHED_NONE && (policy < SCHED_OTHER || policy > SCHED_RR))
    122   1.7     rmind 		return EINVAL;
    123   1.5     rmind 
    124   1.7     rmind 	/* Validate priority */
    125   1.7     rmind 	if (pri != PRI_NONE && (pri < SCHED_PRI_MIN || pri > SCHED_PRI_MAX))
    126   1.7     rmind 		return EINVAL;
    127   1.5     rmind 
    128  1.18      elad 	if (pid != 0) {
    129   1.7     rmind 		/* Find the process */
    130  1.20        ad 		mutex_enter(proc_lock);
    131  1.20        ad 		p = p_find(pid, PFIND_LOCKED);
    132  1.20        ad 		if (p == NULL) {
    133  1.20        ad 			mutex_exit(proc_lock);
    134   1.7     rmind 			return ESRCH;
    135  1.20        ad 		}
    136  1.21        ad 		mutex_enter(p->p_lock);
    137  1.20        ad 		mutex_exit(proc_lock);
    138   1.7     rmind 		/* Disallow modification of system processes */
    139  1.17        ad 		if ((p->p_flag & PK_SYSTEM) != 0) {
    140  1.21        ad 			mutex_exit(p->p_lock);
    141   1.7     rmind 			return EPERM;
    142   1.7     rmind 		}
    143   1.7     rmind 	} else {
    144   1.7     rmind 		/* Use the calling process */
    145  1.18      elad 		p = curlwp->l_proc;
    146  1.21        ad 		mutex_enter(p->p_lock);
    147   1.5     rmind 	}
    148   1.1        ad 
    149   1.5     rmind 	/* Find the LWP(s) */
    150   1.5     rmind 	lcnt = 0;
    151   1.5     rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    152   1.7     rmind 		pri_t kpri;
    153  1.12      elad 		int lpolicy;
    154   1.5     rmind 
    155   1.5     rmind 		if (lid && lid != t->l_lid)
    156   1.5     rmind 			continue;
    157  1.15  drochner 		lcnt++;
    158   1.7     rmind 		KASSERT(pri != PRI_NONE || policy != SCHED_NONE);
    159   1.7     rmind 		lwp_lock(t);
    160   1.7     rmind 
    161  1.12      elad 		if (policy == SCHED_NONE)
    162  1.13      yamt 			lpolicy = t->l_class;
    163  1.12      elad 		else
    164  1.12      elad 			lpolicy = policy;
    165  1.12      elad 
    166   1.7     rmind 		/*
    167   1.7     rmind 		 * Note that, priority may need to be changed to get into
    168   1.7     rmind 		 * the correct priority range of the new scheduling class.
    169   1.7     rmind 		 */
    170  1.12      elad 		kpri = convert_pri(t, lpolicy, pri);
    171  1.12      elad 
    172  1.12      elad 		/* Check the permission */
    173  1.18      elad 		error = kauth_authorize_process(kauth_cred_get(),
    174  1.12      elad 		    KAUTH_PROCESS_SCHEDULER_SETPARAM, p, t, KAUTH_ARG(lpolicy),
    175  1.12      elad 		    KAUTH_ARG(kpri));
    176  1.14      yamt 		if (error) {
    177  1.14      yamt 			lwp_unlock(t);
    178  1.12      elad 			break;
    179  1.14      yamt 		}
    180   1.5     rmind 
    181   1.5     rmind 		/* Set the scheduling class */
    182   1.7     rmind 		if (policy != SCHED_NONE)
    183   1.7     rmind 			t->l_class = policy;
    184   1.5     rmind 
    185   1.5     rmind 		/* Change the priority */
    186   1.7     rmind 		if (t->l_priority != kpri)
    187   1.7     rmind 			lwp_changepri(t, kpri);
    188   1.5     rmind 
    189   1.5     rmind 		lwp_unlock(t);
    190   1.5     rmind 	}
    191  1.21        ad 	mutex_exit(p->p_lock);
    192   1.7     rmind 	return (lcnt == 0) ? ESRCH : error;
    193   1.5     rmind }
    194   1.5     rmind 
    195   1.5     rmind /*
    196  1.18      elad  * Set scheduling parameters.
    197   1.5     rmind  */
    198   1.5     rmind int
    199  1.18      elad sys__sched_setparam(struct lwp *l, const struct sys__sched_setparam_args *uap,
    200   1.5     rmind     register_t *retval)
    201   1.5     rmind {
    202   1.5     rmind 	/* {
    203   1.5     rmind 		syscallarg(pid_t) pid;
    204   1.5     rmind 		syscallarg(lwpid_t) lid;
    205  1.18      elad 		syscallarg(int) policy;
    206  1.18      elad 		syscallarg(const struct sched_param *) params;
    207   1.5     rmind 	} */
    208  1.18      elad 	struct sched_param params;
    209  1.18      elad 	int error;
    210  1.18      elad 
    211  1.18      elad 	/* Get the parameters from the user-space */
    212  1.18      elad 	error = copyin(SCARG(uap, params), &params, sizeof(params));
    213  1.18      elad 	if (error)
    214  1.18      elad 		goto out;
    215  1.18      elad 
    216  1.18      elad 	error = do_sched_setparam(SCARG(uap, pid), SCARG(uap, lid),
    217  1.18      elad 	    SCARG(uap, policy), &params);
    218  1.18      elad 
    219  1.18      elad  out:
    220  1.18      elad 	return (error);
    221  1.18      elad }
    222  1.18      elad 
    223  1.18      elad int
    224  1.18      elad do_sched_getparam(pid_t pid, lwpid_t lid, int *policy,
    225  1.18      elad     struct sched_param *params)
    226  1.18      elad {
    227  1.18      elad 	struct sched_param lparams;
    228   1.5     rmind 	struct lwp *t;
    229  1.18      elad 	int error, lpolicy;
    230   1.5     rmind 
    231  1.16     rmind 	/* Locks the LWP */
    232  1.18      elad 	t = lwp_find2(pid, lid);
    233  1.21        ad 	if (t == NULL)
    234  1.21        ad 		return ESRCH;
    235  1.10      yamt 
    236  1.10      yamt 	/* Check the permission */
    237  1.18      elad 	error = kauth_authorize_process(kauth_cred_get(),
    238  1.11      elad 	    KAUTH_PROCESS_SCHEDULER_GETPARAM, t->l_proc, NULL, NULL, NULL);
    239  1.10      yamt 	if (error != 0) {
    240  1.21        ad 		mutex_exit(t->l_proc->p_lock);
    241  1.21        ad 		return error;
    242   1.5     rmind 	}
    243  1.10      yamt 
    244  1.21        ad 	lwp_lock(t);
    245  1.18      elad 	lparams.sched_priority = t->l_priority;
    246  1.18      elad 	lpolicy = t->l_class;
    247   1.5     rmind 
    248  1.18      elad 	switch (lpolicy) {
    249   1.5     rmind 	case SCHED_OTHER:
    250  1.18      elad 		lparams.sched_priority -= PRI_USER;
    251   1.5     rmind 		break;
    252   1.5     rmind 	case SCHED_RR:
    253   1.5     rmind 	case SCHED_FIFO:
    254  1.18      elad 		lparams.sched_priority -= PRI_USER_RT;
    255   1.5     rmind 		break;
    256   1.5     rmind 	}
    257  1.18      elad 
    258  1.18      elad 	if (policy != NULL)
    259  1.18      elad 		*policy = lpolicy;
    260  1.18      elad 
    261  1.18      elad 	if (params != NULL)
    262  1.18      elad 		*params = lparams;
    263  1.18      elad 
    264  1.21        ad 	lwp_unlock(t);
    265  1.21        ad 	mutex_exit(t->l_proc->p_lock);
    266  1.18      elad 	return error;
    267  1.18      elad }
    268  1.18      elad 
    269  1.18      elad /*
    270  1.18      elad  * Get scheduling parameters.
    271  1.18      elad  */
    272  1.18      elad int
    273  1.18      elad sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
    274  1.18      elad     register_t *retval)
    275  1.18      elad {
    276  1.18      elad 	/* {
    277  1.18      elad 		syscallarg(pid_t) pid;
    278  1.18      elad 		syscallarg(lwpid_t) lid;
    279  1.18      elad 		syscallarg(int *) policy;
    280  1.18      elad 		syscallarg(struct sched_param *) params;
    281  1.18      elad 	} */
    282  1.18      elad 	struct sched_param params;
    283  1.18      elad 	int error, policy;
    284  1.18      elad 
    285  1.18      elad 	error = do_sched_getparam(SCARG(uap, pid), SCARG(uap, lid), &policy,
    286  1.18      elad 	    &params);
    287  1.18      elad 	if (error)
    288  1.18      elad 		goto out;
    289  1.18      elad 
    290  1.18      elad 	error = copyout(&params, SCARG(uap, params), sizeof(params));
    291  1.10      yamt 	if (error == 0 && SCARG(uap, policy) != NULL)
    292  1.10      yamt 		error = copyout(&policy, SCARG(uap, policy), sizeof(int));
    293  1.18      elad 
    294  1.18      elad  out:
    295  1.18      elad 	return (error);
    296   1.5     rmind }
    297   1.5     rmind 
    298  1.23  christos /* Allocate the CPU set, and get it from userspace */
    299  1.23  christos static int
    300  1.26  christos genkcpuset(kcpuset_t **dset, const cpuset_t *sset, size_t size)
    301  1.23  christos {
    302  1.23  christos 	int error;
    303  1.23  christos 
    304  1.26  christos 	*dset = kcpuset_create();
    305  1.26  christos 	error = kcpuset_copyin(sset, *dset, size);
    306  1.26  christos 	if (error != 0)
    307  1.26  christos 		kcpuset_unuse(*dset, NULL);
    308  1.23  christos 	return error;
    309  1.23  christos }
    310  1.23  christos 
    311   1.5     rmind /*
    312   1.5     rmind  * Set affinity.
    313   1.5     rmind  */
    314   1.5     rmind int
    315   1.5     rmind sys__sched_setaffinity(struct lwp *l,
    316   1.5     rmind     const struct sys__sched_setaffinity_args *uap, register_t *retval)
    317   1.5     rmind {
    318   1.5     rmind 	/* {
    319   1.5     rmind 		syscallarg(pid_t) pid;
    320   1.5     rmind 		syscallarg(lwpid_t) lid;
    321   1.5     rmind 		syscallarg(size_t) size;
    322  1.23  christos 		syscallarg(const cpuset_t *) cpuset;
    323   1.5     rmind 	} */
    324  1.26  christos 	kcpuset_t *cpuset, *cpulst = NULL;
    325   1.5     rmind 	struct cpu_info *ci = NULL;
    326   1.5     rmind 	struct proc *p;
    327   1.5     rmind 	struct lwp *t;
    328   1.5     rmind 	CPU_INFO_ITERATOR cii;
    329   1.5     rmind 	lwpid_t lid;
    330   1.5     rmind 	u_int lcnt;
    331   1.5     rmind 	int error;
    332   1.5     rmind 
    333  1.26  christos 	if ((error = genkcpuset(&cpuset, SCARG(uap, cpuset), SCARG(uap, size))))
    334  1.23  christos 		return error;
    335   1.5     rmind 
    336   1.5     rmind 	/* Look for a CPU in the set */
    337  1.23  christos 	for (CPU_INFO_FOREACH(cii, ci)) {
    338  1.26  christos 		error = kcpuset_isset(cpu_index(ci), cpuset);
    339  1.24     rmind 		if (error) {
    340  1.24     rmind 			if (error == -1) {
    341  1.24     rmind 				error = E2BIG;
    342  1.24     rmind 				goto out;
    343  1.24     rmind 			}
    344   1.5     rmind 			break;
    345  1.24     rmind 		}
    346  1.23  christos 	}
    347  1.24     rmind 
    348   1.5     rmind 	if (ci == NULL) {
    349   1.5     rmind 		/* Empty set */
    350  1.25     rmind 		kcpuset_unuse(cpuset, NULL);
    351   1.5     rmind 		cpuset = NULL;
    352   1.5     rmind 	}
    353   1.5     rmind 
    354   1.7     rmind 	if (SCARG(uap, pid) != 0) {
    355   1.7     rmind 		/* Find the process */
    356  1.20        ad 		mutex_enter(proc_lock);
    357  1.20        ad 		p = p_find(SCARG(uap, pid), PFIND_LOCKED);
    358   1.7     rmind 		if (p == NULL) {
    359  1.20        ad 			mutex_exit(proc_lock);
    360   1.7     rmind 			error = ESRCH;
    361  1.23  christos 			goto out;
    362   1.7     rmind 		}
    363  1.21        ad 		mutex_enter(p->p_lock);
    364  1.20        ad 		mutex_exit(proc_lock);
    365  1.17        ad 		/* Disallow modification of system processes. */
    366  1.17        ad 		if ((p->p_flag & PK_SYSTEM) != 0) {
    367  1.21        ad 			mutex_exit(p->p_lock);
    368  1.17        ad 			error = EPERM;
    369  1.23  christos 			goto out;
    370  1.17        ad 		}
    371   1.7     rmind 	} else {
    372   1.7     rmind 		/* Use the calling process */
    373   1.7     rmind 		p = l->l_proc;
    374  1.21        ad 		mutex_enter(p->p_lock);
    375   1.5     rmind 	}
    376   1.5     rmind 
    377  1.10      yamt 	/*
    378  1.10      yamt 	 * Check the permission.
    379  1.10      yamt 	 */
    380  1.11      elad 	error = kauth_authorize_process(l->l_cred,
    381  1.11      elad 	    KAUTH_PROCESS_SCHEDULER_SETAFFINITY, p, NULL, NULL, NULL);
    382  1.10      yamt 	if (error != 0) {
    383  1.21        ad 		mutex_exit(p->p_lock);
    384  1.23  christos 		goto out;
    385  1.10      yamt 	}
    386   1.5     rmind 
    387  1.28  wrstuden #ifdef KERN_SA
    388  1.28  wrstuden 	/*
    389  1.28  wrstuden 	 * Don't permit changing the affinity of an SA process. The only
    390  1.28  wrstuden 	 * thing that would make sense wold be to set the affinity of
    391  1.28  wrstuden 	 * a VP and all threads running on it. But we don't support that
    392  1.28  wrstuden 	 * now, so just don't permit it.
    393  1.28  wrstuden 	 *
    394  1.28  wrstuden 	 * Test is here so that caller gets auth errors before SA
    395  1.28  wrstuden 	 * errors.
    396  1.28  wrstuden 	 */
    397  1.28  wrstuden 	if ((p->p_sflag & (PS_SA | PS_WEXIT)) != 0 || p->p_sa != NULL) {
    398  1.28  wrstuden 		mutex_exit(p->p_lock);
    399  1.28  wrstuden 		error = EINVAL;
    400  1.28  wrstuden 		goto out;
    401  1.28  wrstuden 	}
    402  1.28  wrstuden #endif
    403  1.28  wrstuden 
    404   1.5     rmind 	/* Find the LWP(s) */
    405   1.5     rmind 	lcnt = 0;
    406   1.5     rmind 	lid = SCARG(uap, lid);
    407   1.5     rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    408   1.5     rmind 		if (lid && lid != t->l_lid)
    409   1.5     rmind 			continue;
    410   1.5     rmind 		lwp_lock(t);
    411  1.27     rmind 		/* It is not allowed to set the affinity for zombie LWPs */
    412  1.27     rmind 		if (t->l_stat == LSZOMB) {
    413  1.27     rmind 			lwp_unlock(t);
    414  1.27     rmind 			continue;
    415  1.27     rmind 		}
    416   1.5     rmind 		if (cpuset) {
    417   1.5     rmind 			/* Set the affinity flag and new CPU set */
    418   1.5     rmind 			t->l_flag |= LW_AFFINITY;
    419  1.25     rmind 			kcpuset_use(cpuset);
    420  1.23  christos 			if (t->l_affinity != NULL)
    421  1.25     rmind 				kcpuset_unuse(t->l_affinity, &cpulst);
    422  1.23  christos 			t->l_affinity = cpuset;
    423   1.5     rmind 			/* Migrate to another CPU, unlocks LWP */
    424   1.5     rmind 			lwp_migrate(t, ci);
    425   1.5     rmind 		} else {
    426   1.5     rmind 			/* Unset the affinity flag */
    427   1.5     rmind 			t->l_flag &= ~LW_AFFINITY;
    428  1.23  christos 			if (t->l_affinity != NULL)
    429  1.25     rmind 				kcpuset_unuse(t->l_affinity, &cpulst);
    430  1.23  christos 			t->l_affinity = NULL;
    431   1.5     rmind 			lwp_unlock(t);
    432   1.5     rmind 		}
    433   1.5     rmind 		lcnt++;
    434   1.5     rmind 	}
    435  1.21        ad 	mutex_exit(p->p_lock);
    436   1.5     rmind 	if (lcnt == 0)
    437   1.5     rmind 		error = ESRCH;
    438  1.23  christos out:
    439   1.5     rmind 	if (cpuset != NULL)
    440  1.25     rmind 		kcpuset_unuse(cpuset, &cpulst);
    441  1.26  christos 	kcpuset_destroy(cpulst);
    442   1.5     rmind 	return error;
    443   1.5     rmind }
    444   1.5     rmind 
    445   1.5     rmind /*
    446   1.5     rmind  * Get affinity.
    447   1.5     rmind  */
    448   1.5     rmind int
    449   1.5     rmind sys__sched_getaffinity(struct lwp *l,
    450   1.5     rmind     const struct sys__sched_getaffinity_args *uap, register_t *retval)
    451   1.5     rmind {
    452   1.5     rmind 	/* {
    453   1.5     rmind 		syscallarg(pid_t) pid;
    454   1.5     rmind 		syscallarg(lwpid_t) lid;
    455   1.5     rmind 		syscallarg(size_t) size;
    456  1.23  christos 		syscallarg(cpuset_t *) cpuset;
    457   1.5     rmind 	} */
    458   1.5     rmind 	struct lwp *t;
    459  1.26  christos 	kcpuset_t *cpuset;
    460   1.5     rmind 	int error;
    461   1.5     rmind 
    462  1.26  christos 	if ((error = genkcpuset(&cpuset, SCARG(uap, cpuset), SCARG(uap, size))))
    463  1.23  christos 		return error;
    464   1.5     rmind 
    465  1.16     rmind 	/* Locks the LWP */
    466  1.16     rmind 	t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
    467   1.5     rmind 	if (t == NULL) {
    468  1.23  christos 		error = ESRCH;
    469  1.23  christos 		goto out;
    470   1.5     rmind 	}
    471  1.10      yamt 	/* Check the permission */
    472  1.11      elad 	if (kauth_authorize_process(l->l_cred,
    473  1.11      elad 	    KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
    474  1.21        ad 		mutex_exit(t->l_proc->p_lock);
    475  1.23  christos 		error = EPERM;
    476  1.23  christos 		goto out;
    477  1.10      yamt 	}
    478  1.21        ad 	lwp_lock(t);
    479  1.23  christos 	if (t->l_flag & LW_AFFINITY) {
    480  1.23  christos 		KASSERT(t->l_affinity != NULL);
    481  1.25     rmind 		kcpuset_copy(cpuset, t->l_affinity);
    482  1.23  christos 	} else
    483  1.26  christos 		kcpuset_zero(cpuset);
    484   1.5     rmind 	lwp_unlock(t);
    485  1.21        ad 	mutex_exit(t->l_proc->p_lock);
    486   1.5     rmind 
    487  1.26  christos 	error = kcpuset_copyout(cpuset, SCARG(uap, cpuset), SCARG(uap, size));
    488  1.23  christos out:
    489  1.25     rmind 	kcpuset_unuse(cpuset, NULL);
    490   1.5     rmind 	return error;
    491   1.5     rmind }
    492   1.5     rmind 
    493   1.5     rmind /*
    494   1.5     rmind  * Yield.
    495   1.5     rmind  */
    496   1.1        ad int
    497   1.4       dsl sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    498   1.1        ad {
    499   1.1        ad 
    500   1.1        ad 	yield();
    501  1.28  wrstuden #ifdef KERN_SA
    502  1.28  wrstuden 	if (l->l_flag & LW_SA) {
    503  1.28  wrstuden 		sa_preempt(l);
    504  1.28  wrstuden 	}
    505  1.28  wrstuden #endif
    506   1.1        ad 	return 0;
    507   1.1        ad }
    508   1.5     rmind 
    509   1.5     rmind /*
    510   1.5     rmind  * Sysctl nodes and initialization.
    511   1.5     rmind  */
    512   1.5     rmind SYSCTL_SETUP(sysctl_sched_setup, "sysctl sched setup")
    513   1.5     rmind {
    514   1.5     rmind 	const struct sysctlnode *node = NULL;
    515   1.5     rmind 
    516   1.5     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    517   1.5     rmind 		CTLFLAG_PERMANENT,
    518   1.5     rmind 		CTLTYPE_NODE, "kern", NULL,
    519   1.5     rmind 		NULL, 0, NULL, 0,
    520   1.5     rmind 		CTL_KERN, CTL_EOL);
    521   1.5     rmind 	sysctl_createv(clog, 0, NULL, NULL,
    522   1.5     rmind 		CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    523   1.5     rmind 		CTLTYPE_INT, "posix_sched",
    524   1.5     rmind 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    525   1.5     rmind 			     "Process Scheduling option to which the "
    526   1.5     rmind 			     "system attempts to conform"),
    527   1.5     rmind 		NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
    528   1.5     rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    529   1.5     rmind 	sysctl_createv(clog, 0, NULL, &node,
    530   1.5     rmind 		CTLFLAG_PERMANENT,
    531   1.5     rmind 		CTLTYPE_NODE, "sched",
    532   1.5     rmind 		SYSCTL_DESCR("Scheduler options"),
    533   1.5     rmind 		NULL, 0, NULL, 0,
    534   1.5     rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    535   1.5     rmind 
    536   1.5     rmind 	if (node == NULL)
    537   1.5     rmind 		return;
    538   1.5     rmind 
    539   1.5     rmind 	sysctl_createv(clog, 0, &node, NULL,
    540   1.5     rmind 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    541   1.5     rmind 		CTLTYPE_INT, "pri_min",
    542   1.5     rmind 		SYSCTL_DESCR("Minimal POSIX real-time priority"),
    543   1.5     rmind 		NULL, SCHED_PRI_MIN, NULL, 0,
    544   1.5     rmind 		CTL_CREATE, CTL_EOL);
    545   1.5     rmind 	sysctl_createv(clog, 0, &node, NULL,
    546   1.5     rmind 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    547   1.5     rmind 		CTLTYPE_INT, "pri_max",
    548  1.19     njoly 		SYSCTL_DESCR("Maximal POSIX real-time priority"),
    549   1.5     rmind 		NULL, SCHED_PRI_MAX, NULL, 0,
    550   1.5     rmind 		CTL_CREATE, CTL_EOL);
    551   1.5     rmind }
    552