Home | History | Annotate | Line # | Download | only in kern
sys_sched.c revision 1.5
      1  1.5  rmind /*	$NetBSD: sys_sched.c,v 1.5 2008/01/15 03:37:11 rmind 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.5  rmind  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  1.1     ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1     ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1     ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1     ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1     ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1     ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1     ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1     ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1     ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1     ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1     ad  */
     28  1.1     ad 
     29  1.5  rmind /*
     30  1.5  rmind  * TODO:
     31  1.5  rmind  *  - Handle pthread_setschedprio() as defined by POSIX;
     32  1.5  rmind  *  - Handle sched_yield() case for SCHED_FIFO as defined by POSIX;
     33  1.5  rmind  */
     34  1.5  rmind 
     35  1.1     ad #include <sys/cdefs.h>
     36  1.5  rmind __KERNEL_RCSID(0, "$NetBSD: sys_sched.c,v 1.5 2008/01/15 03:37:11 rmind Exp $");
     37  1.1     ad 
     38  1.1     ad #include <sys/param.h>
     39  1.5  rmind 
     40  1.5  rmind #include <sys/cpu.h>
     41  1.5  rmind #include <sys/kauth.h>
     42  1.5  rmind #include <sys/kmem.h>
     43  1.5  rmind #include <sys/lwp.h>
     44  1.5  rmind #include <sys/mutex.h>
     45  1.1     ad #include <sys/proc.h>
     46  1.5  rmind #include <sys/pset.h>
     47  1.5  rmind #include <sys/sched.h>
     48  1.1     ad #include <sys/syscallargs.h>
     49  1.5  rmind #include <sys/sysctl.h>
     50  1.5  rmind #include <sys/systm.h>
     51  1.5  rmind #include <sys/types.h>
     52  1.5  rmind #include <sys/unistd.h>
     53  1.5  rmind 
     54  1.5  rmind /*
     55  1.5  rmind  * Set scheduling parameters.
     56  1.5  rmind  */
     57  1.5  rmind int
     58  1.5  rmind sys__sched_setparam(struct lwp *l, const struct sys__sched_setparam_args *uap,
     59  1.5  rmind     register_t *retval)
     60  1.5  rmind {
     61  1.5  rmind 	/* {
     62  1.5  rmind 		syscallarg(pid_t) pid;
     63  1.5  rmind 		syscallarg(lwpid_t) lid;
     64  1.5  rmind 		syscallarg(const struct sched_param *) params;
     65  1.5  rmind 	} */
     66  1.5  rmind 	struct sched_param *sp;
     67  1.5  rmind 	struct proc *p;
     68  1.5  rmind 	struct lwp *t;
     69  1.5  rmind 	pid_t pid;
     70  1.5  rmind 	lwpid_t lid;
     71  1.5  rmind 	u_int lcnt;
     72  1.5  rmind 	pri_t pri;
     73  1.5  rmind 	int error;
     74  1.5  rmind 
     75  1.5  rmind 	/* Available only for super-user */
     76  1.5  rmind 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL))
     77  1.5  rmind 		return EACCES;
     78  1.5  rmind 
     79  1.5  rmind 	/* Get the parameters from the user-space */
     80  1.5  rmind 	sp = kmem_zalloc(sizeof(struct sched_param), KM_SLEEP);
     81  1.5  rmind 	error = copyin(SCARG(uap, params), sp, sizeof(struct sched_param));
     82  1.5  rmind 	if (error)
     83  1.5  rmind 		goto error;
     84  1.5  rmind 
     85  1.5  rmind 	/*
     86  1.5  rmind 	 * Validate scheduling class and priority.
     87  1.5  rmind 	 * Convert the user priority to the in-kernel value.
     88  1.5  rmind 	 */
     89  1.5  rmind 	pri = sp->sched_priority;
     90  1.5  rmind 	if (pri != PRI_NONE && (pri < SCHED_PRI_MIN || pri > SCHED_PRI_MAX)) {
     91  1.5  rmind 		error = EINVAL;
     92  1.5  rmind 		goto error;
     93  1.5  rmind 	}
     94  1.5  rmind 	switch (sp->sched_class) {
     95  1.5  rmind 	case SCHED_OTHER:
     96  1.5  rmind 		if (pri == PRI_NONE)
     97  1.5  rmind 			pri = PRI_USER;
     98  1.5  rmind 		else
     99  1.5  rmind 			pri += PRI_USER;
    100  1.5  rmind 		break;
    101  1.5  rmind 	case SCHED_RR:
    102  1.5  rmind 	case SCHED_FIFO:
    103  1.5  rmind 		if (pri == PRI_NONE)
    104  1.5  rmind 			pri = PRI_USER_RT;
    105  1.5  rmind 		else
    106  1.5  rmind 			pri += PRI_USER_RT;
    107  1.5  rmind 		break;
    108  1.5  rmind 	case SCHED_NONE:
    109  1.5  rmind 		break;
    110  1.5  rmind 	default:
    111  1.5  rmind 		error = EINVAL;
    112  1.5  rmind 		goto error;
    113  1.5  rmind 	}
    114  1.5  rmind 
    115  1.5  rmind 	/* Find the process */
    116  1.5  rmind 	pid = SCARG(uap, pid);
    117  1.5  rmind 	p = p_find(pid, PFIND_UNLOCK_FAIL);
    118  1.5  rmind 	if (p == NULL) {
    119  1.5  rmind 		error = ESRCH;
    120  1.5  rmind 		goto error;
    121  1.5  rmind 	}
    122  1.5  rmind 	mutex_enter(&p->p_smutex);
    123  1.5  rmind 	mutex_exit(&proclist_lock);
    124  1.5  rmind 
    125  1.5  rmind 	/* Disallow modification of system processes */
    126  1.5  rmind 	if (p->p_flag & PK_SYSTEM) {
    127  1.5  rmind 		mutex_exit(&p->p_smutex);
    128  1.5  rmind 		error = EACCES;
    129  1.5  rmind 		goto error;
    130  1.5  rmind 	}
    131  1.1     ad 
    132  1.5  rmind 	/* Find the LWP(s) */
    133  1.5  rmind 	lcnt = 0;
    134  1.5  rmind 	lid = SCARG(uap, lid);
    135  1.5  rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    136  1.5  rmind 		bool chpri;
    137  1.5  rmind 
    138  1.5  rmind 		if (lid && lid != t->l_lid)
    139  1.5  rmind 			continue;
    140  1.5  rmind 
    141  1.5  rmind 		/* Set the scheduling class */
    142  1.5  rmind 		lwp_lock(t);
    143  1.5  rmind 		if (sp->sched_class != SCHED_NONE) {
    144  1.5  rmind 			/*
    145  1.5  rmind 			 * Priority must be changed to get into the correct
    146  1.5  rmind 			 * priority range of the new scheduling class.
    147  1.5  rmind 			 */
    148  1.5  rmind 			chpri = (t->l_class != sp->sched_class);
    149  1.5  rmind 			t->l_class = sp->sched_class;
    150  1.5  rmind 		} else
    151  1.5  rmind 			chpri = false;
    152  1.5  rmind 
    153  1.5  rmind 		/* Change the priority */
    154  1.5  rmind 		if (sp->sched_priority != PRI_NONE || chpri)
    155  1.5  rmind 			lwp_changepri(t, pri);
    156  1.5  rmind 
    157  1.5  rmind 		lwp_unlock(t);
    158  1.5  rmind 		lcnt++;
    159  1.5  rmind 	}
    160  1.5  rmind 	mutex_exit(&p->p_smutex);
    161  1.5  rmind 	if (lcnt != 0)
    162  1.5  rmind 		*retval = lcnt;
    163  1.5  rmind 	else
    164  1.5  rmind 		error = ESRCH;
    165  1.5  rmind error:
    166  1.5  rmind 	kmem_free(sp, sizeof(struct sched_param));
    167  1.5  rmind 	return error;
    168  1.5  rmind }
    169  1.5  rmind 
    170  1.5  rmind /*
    171  1.5  rmind  * Get scheduling parameters.
    172  1.5  rmind  */
    173  1.5  rmind int
    174  1.5  rmind sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
    175  1.5  rmind     register_t *retval)
    176  1.5  rmind {
    177  1.5  rmind 	/* {
    178  1.5  rmind 		syscallarg(pid_t) pid;
    179  1.5  rmind 		syscallarg(lwpid_t) lid;
    180  1.5  rmind 		syscallarg(struct sched_param *) params;
    181  1.5  rmind 	} */
    182  1.5  rmind 	struct sched_param *sp;
    183  1.5  rmind 	struct lwp *t;
    184  1.5  rmind 	int error;
    185  1.5  rmind 
    186  1.5  rmind 	sp = kmem_zalloc(sizeof(struct sched_param), KM_SLEEP);
    187  1.5  rmind 
    188  1.5  rmind 	/* Locks the LWP */
    189  1.5  rmind 	t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
    190  1.5  rmind 	if (t == NULL) {
    191  1.5  rmind 		kmem_free(sp, sizeof(struct sched_param));
    192  1.5  rmind 		return ESRCH;
    193  1.5  rmind 	}
    194  1.5  rmind 	sp->sched_priority = t->l_priority;
    195  1.5  rmind 	sp->sched_class = t->l_class;
    196  1.5  rmind 	lwp_unlock(t);
    197  1.5  rmind 
    198  1.5  rmind 	switch (sp->sched_class) {
    199  1.5  rmind 	case SCHED_OTHER:
    200  1.5  rmind 		sp->sched_priority -= PRI_USER;
    201  1.5  rmind 		break;
    202  1.5  rmind 	case SCHED_RR:
    203  1.5  rmind 	case SCHED_FIFO:
    204  1.5  rmind 		sp->sched_priority -= PRI_USER_RT;
    205  1.5  rmind 		break;
    206  1.5  rmind 	}
    207  1.5  rmind 	error = copyout(sp, SCARG(uap, params), sizeof(struct sched_param));
    208  1.5  rmind 	kmem_free(sp, sizeof(struct sched_param));
    209  1.5  rmind 	return error;
    210  1.5  rmind }
    211  1.5  rmind 
    212  1.5  rmind /*
    213  1.5  rmind  * Set affinity.
    214  1.5  rmind  */
    215  1.5  rmind int
    216  1.5  rmind sys__sched_setaffinity(struct lwp *l,
    217  1.5  rmind     const struct sys__sched_setaffinity_args *uap, register_t *retval)
    218  1.5  rmind {
    219  1.5  rmind 	/* {
    220  1.5  rmind 		syscallarg(pid_t) pid;
    221  1.5  rmind 		syscallarg(lwpid_t) lid;
    222  1.5  rmind 		syscallarg(size_t) size;
    223  1.5  rmind 		syscallarg(void *) cpuset;
    224  1.5  rmind 	} */
    225  1.5  rmind 	cpuset_t *cpuset;
    226  1.5  rmind 	struct cpu_info *ci = NULL;
    227  1.5  rmind 	struct proc *p;
    228  1.5  rmind 	struct lwp *t;
    229  1.5  rmind 	CPU_INFO_ITERATOR cii;
    230  1.5  rmind 	lwpid_t lid;
    231  1.5  rmind 	u_int lcnt;
    232  1.5  rmind 	int error;
    233  1.5  rmind 
    234  1.5  rmind 	/* Available only for super-user */
    235  1.5  rmind 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL))
    236  1.5  rmind 		return EACCES;
    237  1.5  rmind 
    238  1.5  rmind 	if (SCARG(uap, size) <= 0)
    239  1.5  rmind 		return EINVAL;
    240  1.5  rmind 
    241  1.5  rmind 	/* Allocate the CPU set, and get it from userspace */
    242  1.5  rmind 	cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
    243  1.5  rmind 	error = copyin(SCARG(uap, cpuset), cpuset,
    244  1.5  rmind 	    min(SCARG(uap, size), sizeof(cpuset_t)));
    245  1.5  rmind 	if (error)
    246  1.5  rmind 		goto error;
    247  1.5  rmind 
    248  1.5  rmind 	/* Look for a CPU in the set */
    249  1.5  rmind 	for (CPU_INFO_FOREACH(cii, ci))
    250  1.5  rmind 		if (CPU_ISSET(cpu_index(ci), cpuset))
    251  1.5  rmind 			break;
    252  1.5  rmind 	if (ci == NULL) {
    253  1.5  rmind 		/* Empty set */
    254  1.5  rmind 		kmem_free(cpuset, sizeof(cpuset_t));
    255  1.5  rmind 		cpuset = NULL;
    256  1.5  rmind 	}
    257  1.5  rmind 
    258  1.5  rmind 	/* Find the process */
    259  1.5  rmind 	p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL);
    260  1.5  rmind 	if (p == NULL) {
    261  1.5  rmind 		error = ESRCH;
    262  1.5  rmind 		goto error;
    263  1.5  rmind 	}
    264  1.5  rmind 	mutex_enter(&p->p_smutex);
    265  1.5  rmind 	mutex_exit(&proclist_lock);
    266  1.5  rmind 
    267  1.5  rmind 	/* Disallow modification of system processes */
    268  1.5  rmind 	if (p->p_flag & PK_SYSTEM) {
    269  1.5  rmind 		mutex_exit(&p->p_smutex);
    270  1.5  rmind 		error = EACCES;
    271  1.5  rmind 		goto error;
    272  1.5  rmind 	}
    273  1.5  rmind 
    274  1.5  rmind 	/* Find the LWP(s) */
    275  1.5  rmind 	lcnt = 0;
    276  1.5  rmind 	lid = SCARG(uap, lid);
    277  1.5  rmind 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    278  1.5  rmind 		if (lid && lid != t->l_lid)
    279  1.5  rmind 			continue;
    280  1.5  rmind 		lwp_lock(t);
    281  1.5  rmind 		if (cpuset) {
    282  1.5  rmind 			/* Set the affinity flag and new CPU set */
    283  1.5  rmind 			t->l_flag |= LW_AFFINITY;
    284  1.5  rmind 			memcpy(&t->l_affinity, cpuset, sizeof(cpuset_t));
    285  1.5  rmind 			/* Migrate to another CPU, unlocks LWP */
    286  1.5  rmind 			lwp_migrate(t, ci);
    287  1.5  rmind 		} else {
    288  1.5  rmind 			/* Unset the affinity flag */
    289  1.5  rmind 			t->l_flag &= ~LW_AFFINITY;
    290  1.5  rmind 			lwp_unlock(t);
    291  1.5  rmind 		}
    292  1.5  rmind 		lcnt++;
    293  1.5  rmind 	}
    294  1.5  rmind 	mutex_exit(&p->p_smutex);
    295  1.5  rmind 	if (lcnt == 0)
    296  1.5  rmind 		error = ESRCH;
    297  1.5  rmind 	else
    298  1.5  rmind 		*retval = lcnt;
    299  1.5  rmind error:
    300  1.5  rmind 	if (cpuset != NULL)
    301  1.5  rmind 		kmem_free(cpuset, sizeof(cpuset_t));
    302  1.5  rmind 	return error;
    303  1.5  rmind }
    304  1.5  rmind 
    305  1.5  rmind /*
    306  1.5  rmind  * Get affinity.
    307  1.5  rmind  */
    308  1.5  rmind int
    309  1.5  rmind sys__sched_getaffinity(struct lwp *l,
    310  1.5  rmind     const struct sys__sched_getaffinity_args *uap, register_t *retval)
    311  1.5  rmind {
    312  1.5  rmind 	/* {
    313  1.5  rmind 		syscallarg(pid_t) pid;
    314  1.5  rmind 		syscallarg(lwpid_t) lid;
    315  1.5  rmind 		syscallarg(size_t) size;
    316  1.5  rmind 		syscallarg(void *) cpuset;
    317  1.5  rmind 	} */
    318  1.5  rmind 	struct lwp *t;
    319  1.5  rmind 	void *cpuset;
    320  1.5  rmind 	int error;
    321  1.5  rmind 
    322  1.5  rmind 	if (SCARG(uap, size) <= 0)
    323  1.5  rmind 		return EINVAL;
    324  1.5  rmind 
    325  1.5  rmind 	cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
    326  1.5  rmind 
    327  1.5  rmind 	/* Locks the LWP */
    328  1.5  rmind 	t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
    329  1.5  rmind 	if (t == NULL) {
    330  1.5  rmind 		kmem_free(cpuset, sizeof(cpuset_t));
    331  1.5  rmind 		return ESRCH;
    332  1.5  rmind 	}
    333  1.5  rmind 	if (t->l_flag & LW_AFFINITY)
    334  1.5  rmind 		memcpy(cpuset, &t->l_affinity, sizeof(cpuset_t));
    335  1.5  rmind 	lwp_unlock(t);
    336  1.5  rmind 
    337  1.5  rmind 	error = copyout(cpuset, SCARG(uap, cpuset),
    338  1.5  rmind 	    min(SCARG(uap, size), sizeof(cpuset_t)));
    339  1.5  rmind 
    340  1.5  rmind 	kmem_free(cpuset, sizeof(cpuset_t));
    341  1.5  rmind 	return error;
    342  1.5  rmind }
    343  1.5  rmind 
    344  1.5  rmind /*
    345  1.5  rmind  * Yield.
    346  1.5  rmind  */
    347  1.1     ad int
    348  1.4    dsl sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    349  1.1     ad {
    350  1.1     ad 
    351  1.1     ad 	yield();
    352  1.1     ad 	return 0;
    353  1.1     ad }
    354  1.5  rmind 
    355  1.5  rmind /*
    356  1.5  rmind  * Sysctl nodes and initialization.
    357  1.5  rmind  */
    358  1.5  rmind SYSCTL_SETUP(sysctl_sched_setup, "sysctl sched setup")
    359  1.5  rmind {
    360  1.5  rmind 	const struct sysctlnode *node = NULL;
    361  1.5  rmind 
    362  1.5  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    363  1.5  rmind 		CTLFLAG_PERMANENT,
    364  1.5  rmind 		CTLTYPE_NODE, "kern", NULL,
    365  1.5  rmind 		NULL, 0, NULL, 0,
    366  1.5  rmind 		CTL_KERN, CTL_EOL);
    367  1.5  rmind 	sysctl_createv(clog, 0, NULL, NULL,
    368  1.5  rmind 		CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    369  1.5  rmind 		CTLTYPE_INT, "posix_sched",
    370  1.5  rmind 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    371  1.5  rmind 			     "Process Scheduling option to which the "
    372  1.5  rmind 			     "system attempts to conform"),
    373  1.5  rmind 		NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
    374  1.5  rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    375  1.5  rmind 	sysctl_createv(clog, 0, NULL, &node,
    376  1.5  rmind 		CTLFLAG_PERMANENT,
    377  1.5  rmind 		CTLTYPE_NODE, "sched",
    378  1.5  rmind 		SYSCTL_DESCR("Scheduler options"),
    379  1.5  rmind 		NULL, 0, NULL, 0,
    380  1.5  rmind 		CTL_KERN, CTL_CREATE, CTL_EOL);
    381  1.5  rmind 
    382  1.5  rmind 	if (node == NULL)
    383  1.5  rmind 		return;
    384  1.5  rmind 
    385  1.5  rmind 	sysctl_createv(clog, 0, &node, NULL,
    386  1.5  rmind 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    387  1.5  rmind 		CTLTYPE_INT, "pri_min",
    388  1.5  rmind 		SYSCTL_DESCR("Minimal POSIX real-time priority"),
    389  1.5  rmind 		NULL, SCHED_PRI_MIN, NULL, 0,
    390  1.5  rmind 		CTL_CREATE, CTL_EOL);
    391  1.5  rmind 	sysctl_createv(clog, 0, &node, NULL,
    392  1.5  rmind 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    393  1.5  rmind 		CTLTYPE_INT, "pri_max",
    394  1.5  rmind 		SYSCTL_DESCR("Minimal POSIX real-time priority"),
    395  1.5  rmind 		NULL, SCHED_PRI_MAX, NULL, 0,
    396  1.5  rmind 		CTL_CREATE, CTL_EOL);
    397  1.5  rmind }
    398