Home | History | Annotate | Line # | Download | only in kern
sys_sched.c revision 1.12
      1 /*	$NetBSD: sys_sched.c,v 1.12 2008/02/17 19:22:35 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * TODO:
     31  *  - Handle pthread_setschedprio() as defined by POSIX;
     32  *  - Handle sched_yield() case for SCHED_FIFO as defined by POSIX;
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: sys_sched.c,v 1.12 2008/02/17 19:22:35 elad Exp $");
     37 
     38 #include <sys/param.h>
     39 
     40 #include <sys/cpu.h>
     41 #include <sys/kauth.h>
     42 #include <sys/kmem.h>
     43 #include <sys/lwp.h>
     44 #include <sys/mutex.h>
     45 #include <sys/proc.h>
     46 #include <sys/pset.h>
     47 #include <sys/sched.h>
     48 #include <sys/syscallargs.h>
     49 #include <sys/sysctl.h>
     50 #include <sys/systm.h>
     51 #include <sys/types.h>
     52 #include <sys/unistd.h>
     53 
     54 /*
     55  * Convert user priority or the in-kernel priority or convert the current
     56  * priority to the appropriate range according to the policy change.
     57  */
     58 static pri_t
     59 convert_pri(lwp_t *l, int policy, pri_t pri)
     60 {
     61 	int delta = 0;
     62 
     63 	switch (policy) {
     64 	case SCHED_OTHER:
     65 		delta = PRI_USER;
     66 		break;
     67 	case SCHED_FIFO:
     68 	case SCHED_RR:
     69 		delta = PRI_USER_RT;
     70 		break;
     71 	default:
     72 		panic("upri_to_kpri");
     73 	}
     74 
     75 	if (pri != PRI_NONE) {
     76 		/* Convert user priority to the in-kernel */
     77 		KASSERT(pri >= SCHED_PRI_MIN && pri <= SCHED_PRI_MAX);
     78 		return pri + delta;
     79 	}
     80 	if (l->l_class == policy)
     81 		return l->l_priority;
     82 
     83 	/* Change the current priority to the appropriate range */
     84 	if (l->l_class == SCHED_OTHER) {
     85 		KASSERT(policy == SCHED_FIFO || policy == SCHED_RR);
     86 		return l->l_priority + delta;
     87 	}
     88 	if (policy == SCHED_OTHER) {
     89 		KASSERT(l->l_class == SCHED_FIFO || l->l_class == SCHED_RR);
     90 		return l->l_priority - delta;
     91 	}
     92 	KASSERT(l->l_class != SCHED_OTHER && policy != SCHED_OTHER);
     93 	return l->l_class;
     94 }
     95 
     96 /*
     97  * Set scheduling parameters.
     98  */
     99 int
    100 sys__sched_setparam(struct lwp *l, const struct sys__sched_setparam_args *uap,
    101     register_t *retval)
    102 {
    103 	/* {
    104 		syscallarg(pid_t) pid;
    105 		syscallarg(lwpid_t) lid;
    106 		syscallarg(int) policy;
    107 		syscallarg(const struct sched_param *) params;
    108 	} */
    109 	struct sched_param param;
    110 	struct proc *p;
    111 	struct lwp *t;
    112 	lwpid_t lid;
    113 	u_int lcnt;
    114 	int policy;
    115 	pri_t pri;
    116 	int error;
    117 
    118 	/* Get the parameters from the user-space */
    119 	error = copyin(SCARG(uap, params), &param, sizeof(param));
    120 	if (error) {
    121 		return error;
    122 	}
    123 	pri = param.sched_priority;
    124 	policy = SCARG(uap, policy);
    125 
    126 	/* If no parameters specified, just return (this should not happen) */
    127 	if (pri == PRI_NONE && policy == SCHED_NONE)
    128 		return 0;
    129 
    130 	/* Validate scheduling class */
    131 	if (policy != SCHED_NONE && (policy < SCHED_OTHER || policy > SCHED_RR))
    132 		return EINVAL;
    133 
    134 	/* Validate priority */
    135 	if (pri != PRI_NONE && (pri < SCHED_PRI_MIN || pri > SCHED_PRI_MAX))
    136 		return EINVAL;
    137 
    138 	if (SCARG(uap, pid) != 0) {
    139 		/* Find the process */
    140 		p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL);
    141 		if (p == NULL)
    142 			return ESRCH;
    143 		mutex_enter(&p->p_smutex);
    144 		mutex_exit(&proclist_lock);
    145 		/* Disallow modification of system processes */
    146 		if (p->p_flag & PK_SYSTEM) {
    147 			mutex_exit(&p->p_smutex);
    148 			return EPERM;
    149 		}
    150 	} else {
    151 		/* Use the calling process */
    152 		p = l->l_proc;
    153 		mutex_enter(&p->p_smutex);
    154 	}
    155 
    156 	/* Find the LWP(s) */
    157 	lcnt = 0;
    158 	lid = SCARG(uap, lid);
    159 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    160 		pri_t kpri;
    161 		int lpolicy;
    162 
    163 		if (lid && lid != t->l_lid)
    164 			continue;
    165 		KASSERT(pri != PRI_NONE || policy != SCHED_NONE);
    166 		lwp_lock(t);
    167 
    168 		if (policy == SCHED_NONE)
    169 			lpolicy = l->l_class;
    170 		else
    171 			lpolicy = policy;
    172 
    173 		/*
    174 		 * Note that, priority may need to be changed to get into
    175 		 * the correct priority range of the new scheduling class.
    176 		 */
    177 		kpri = convert_pri(t, lpolicy, pri);
    178 
    179 		/* Check the permission */
    180 		error = kauth_authorize_process(l->l_cred,
    181 		    KAUTH_PROCESS_SCHEDULER_SETPARAM, p, t, KAUTH_ARG(lpolicy),
    182 		    KAUTH_ARG(kpri));
    183 		if (error)
    184 			break;
    185 
    186 		/* Set the scheduling class */
    187 		if (policy != SCHED_NONE)
    188 			t->l_class = policy;
    189 
    190 		/* Change the priority */
    191 		if (t->l_priority != kpri)
    192 			lwp_changepri(t, kpri);
    193 
    194 		lwp_unlock(t);
    195 		lcnt++;
    196 	}
    197 	mutex_exit(&p->p_smutex);
    198 	return (lcnt == 0) ? ESRCH : error;
    199 }
    200 
    201 /*
    202  * Get scheduling parameters.
    203  */
    204 int
    205 sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
    206     register_t *retval)
    207 {
    208 	/* {
    209 		syscallarg(pid_t) pid;
    210 		syscallarg(lwpid_t) lid;
    211 		syscallarg(int *) policy;
    212 		syscallarg(struct sched_param *) params;
    213 	} */
    214 	struct sched_param param;
    215 	struct lwp *t;
    216 	lwpid_t lid;
    217 	int error, policy;
    218 
    219 	/* If not specified, use the first LWP */
    220 	lid = SCARG(uap, lid) == 0 ? 1 : SCARG(uap, lid);
    221 
    222 	if (SCARG(uap, pid) != 0) {
    223 		/* Locks the LWP */
    224 		t = lwp_find2(SCARG(uap, pid), lid);
    225 	} else {
    226 		struct proc *p = l->l_proc;
    227 		/* Use the calling process */
    228 		mutex_enter(&p->p_smutex);
    229 		t = lwp_find(p, lid);
    230 		if (t != NULL)
    231 			lwp_lock(t);
    232 		mutex_exit(&p->p_smutex);
    233 	}
    234 	if (t == NULL) {
    235 		error = ESRCH;
    236 		goto error;
    237 	}
    238 
    239 	/* Check the permission */
    240 	error = kauth_authorize_process(l->l_cred,
    241 	    KAUTH_PROCESS_SCHEDULER_GETPARAM, t->l_proc, NULL, NULL, NULL);
    242 	if (error != 0) {
    243 		lwp_unlock(t);
    244 		goto error;
    245 	}
    246 
    247 	param.sched_priority = t->l_priority;
    248 	policy = t->l_class;
    249 	lwp_unlock(t);
    250 
    251 	switch (policy) {
    252 	case SCHED_OTHER:
    253 		param.sched_priority -= PRI_USER;
    254 		break;
    255 	case SCHED_RR:
    256 	case SCHED_FIFO:
    257 		param.sched_priority -= PRI_USER_RT;
    258 		break;
    259 	}
    260 	error = copyout(&param, SCARG(uap, params), sizeof(param));
    261 	if (error == 0 && SCARG(uap, policy) != NULL)
    262 		error = copyout(&policy, SCARG(uap, policy), sizeof(int));
    263 error:
    264 	return error;
    265 }
    266 
    267 /*
    268  * Set affinity.
    269  */
    270 int
    271 sys__sched_setaffinity(struct lwp *l,
    272     const struct sys__sched_setaffinity_args *uap, register_t *retval)
    273 {
    274 	/* {
    275 		syscallarg(pid_t) pid;
    276 		syscallarg(lwpid_t) lid;
    277 		syscallarg(size_t) size;
    278 		syscallarg(void *) cpuset;
    279 	} */
    280 	cpuset_t *cpuset;
    281 	struct cpu_info *ci = NULL;
    282 	struct proc *p;
    283 	struct lwp *t;
    284 	CPU_INFO_ITERATOR cii;
    285 	lwpid_t lid;
    286 	u_int lcnt;
    287 	int error;
    288 
    289 	/* Allocate the CPU set, and get it from userspace */
    290 	cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
    291 	error = copyin(SCARG(uap, cpuset), cpuset,
    292 	    min(SCARG(uap, size), sizeof(cpuset_t)));
    293 	if (error)
    294 		goto error;
    295 
    296 	/* Look for a CPU in the set */
    297 	for (CPU_INFO_FOREACH(cii, ci))
    298 		if (CPU_ISSET(cpu_index(ci), cpuset))
    299 			break;
    300 	if (ci == NULL) {
    301 		/* Empty set */
    302 		kmem_free(cpuset, sizeof(cpuset_t));
    303 		cpuset = NULL;
    304 	}
    305 
    306 	if (SCARG(uap, pid) != 0) {
    307 		/* Find the process */
    308 		p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL);
    309 		if (p == NULL) {
    310 			error = ESRCH;
    311 			goto error;
    312 		}
    313 		mutex_enter(&p->p_smutex);
    314 		mutex_exit(&proclist_lock);
    315 	} else {
    316 		/* Use the calling process */
    317 		p = l->l_proc;
    318 		mutex_enter(&p->p_smutex);
    319 	}
    320 
    321 	/*
    322 	 * Check the permission.
    323 	 * Disallow modification of system processes.
    324 	 */
    325 	error = kauth_authorize_process(l->l_cred,
    326 	    KAUTH_PROCESS_SCHEDULER_SETAFFINITY, p, NULL, NULL, NULL);
    327 	if (error != 0) {
    328 		mutex_exit(&p->p_smutex);
    329 		goto error;
    330 	}
    331 	if ((p->p_flag & PK_SYSTEM) != 0) {
    332 		mutex_exit(&p->p_smutex);
    333 		error = EPERM;
    334 		goto error;
    335 	}
    336 
    337 	/* Find the LWP(s) */
    338 	lcnt = 0;
    339 	lid = SCARG(uap, lid);
    340 	LIST_FOREACH(t, &p->p_lwps, l_sibling) {
    341 		if (lid && lid != t->l_lid)
    342 			continue;
    343 		lwp_lock(t);
    344 		if (cpuset) {
    345 			/* Set the affinity flag and new CPU set */
    346 			t->l_flag |= LW_AFFINITY;
    347 			memcpy(&t->l_affinity, cpuset, sizeof(cpuset_t));
    348 			/* Migrate to another CPU, unlocks LWP */
    349 			lwp_migrate(t, ci);
    350 		} else {
    351 			/* Unset the affinity flag */
    352 			t->l_flag &= ~LW_AFFINITY;
    353 			lwp_unlock(t);
    354 		}
    355 		lcnt++;
    356 	}
    357 	mutex_exit(&p->p_smutex);
    358 	if (lcnt == 0)
    359 		error = ESRCH;
    360 error:
    361 	if (cpuset != NULL)
    362 		kmem_free(cpuset, sizeof(cpuset_t));
    363 	return error;
    364 }
    365 
    366 /*
    367  * Get affinity.
    368  */
    369 int
    370 sys__sched_getaffinity(struct lwp *l,
    371     const struct sys__sched_getaffinity_args *uap, register_t *retval)
    372 {
    373 	/* {
    374 		syscallarg(pid_t) pid;
    375 		syscallarg(lwpid_t) lid;
    376 		syscallarg(size_t) size;
    377 		syscallarg(void *) cpuset;
    378 	} */
    379 	struct lwp *t;
    380 	void *cpuset;
    381 	lwpid_t lid;
    382 	int error;
    383 
    384 	if (SCARG(uap, size) <= 0)
    385 		return EINVAL;
    386 	cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
    387 
    388 	/* If not specified, use the first LWP */
    389 	lid = SCARG(uap, lid) == 0 ? 1 : SCARG(uap, lid);
    390 
    391 	if (SCARG(uap, pid) != 0) {
    392 		/* Locks the LWP */
    393 		t = lwp_find2(SCARG(uap, pid), lid);
    394 	} else {
    395 		struct proc *p = l->l_proc;
    396 		/* Use the calling process */
    397 		mutex_enter(&p->p_smutex);
    398 		t = lwp_find(p, lid);
    399 		if (t != NULL)
    400 			lwp_lock(t);
    401 		mutex_exit(&p->p_smutex);
    402 	}
    403 	if (t == NULL) {
    404 		kmem_free(cpuset, sizeof(cpuset_t));
    405 		return ESRCH;
    406 	}
    407 	/* Check the permission */
    408 	if (kauth_authorize_process(l->l_cred,
    409 	    KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
    410 		lwp_unlock(t);
    411 		kmem_free(cpuset, sizeof(cpuset_t));
    412 		return EPERM;
    413 	}
    414 	if (t->l_flag & LW_AFFINITY)
    415 		memcpy(cpuset, &t->l_affinity, sizeof(cpuset_t));
    416 	lwp_unlock(t);
    417 
    418 	error = copyout(cpuset, SCARG(uap, cpuset),
    419 	    min(SCARG(uap, size), sizeof(cpuset_t)));
    420 
    421 	kmem_free(cpuset, sizeof(cpuset_t));
    422 	return error;
    423 }
    424 
    425 /*
    426  * Yield.
    427  */
    428 int
    429 sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    430 {
    431 
    432 	yield();
    433 	return 0;
    434 }
    435 
    436 /*
    437  * Sysctl nodes and initialization.
    438  */
    439 SYSCTL_SETUP(sysctl_sched_setup, "sysctl sched setup")
    440 {
    441 	const struct sysctlnode *node = NULL;
    442 
    443 	sysctl_createv(clog, 0, NULL, NULL,
    444 		CTLFLAG_PERMANENT,
    445 		CTLTYPE_NODE, "kern", NULL,
    446 		NULL, 0, NULL, 0,
    447 		CTL_KERN, CTL_EOL);
    448 	sysctl_createv(clog, 0, NULL, NULL,
    449 		CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    450 		CTLTYPE_INT, "posix_sched",
    451 		SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    452 			     "Process Scheduling option to which the "
    453 			     "system attempts to conform"),
    454 		NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
    455 		CTL_KERN, CTL_CREATE, CTL_EOL);
    456 	sysctl_createv(clog, 0, NULL, &node,
    457 		CTLFLAG_PERMANENT,
    458 		CTLTYPE_NODE, "sched",
    459 		SYSCTL_DESCR("Scheduler options"),
    460 		NULL, 0, NULL, 0,
    461 		CTL_KERN, CTL_CREATE, CTL_EOL);
    462 
    463 	if (node == NULL)
    464 		return;
    465 
    466 	sysctl_createv(clog, 0, &node, NULL,
    467 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    468 		CTLTYPE_INT, "pri_min",
    469 		SYSCTL_DESCR("Minimal POSIX real-time priority"),
    470 		NULL, SCHED_PRI_MIN, NULL, 0,
    471 		CTL_CREATE, CTL_EOL);
    472 	sysctl_createv(clog, 0, &node, NULL,
    473 		CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
    474 		CTLTYPE_INT, "pri_max",
    475 		SYSCTL_DESCR("Minimal POSIX real-time priority"),
    476 		NULL, SCHED_PRI_MAX, NULL, 0,
    477 		CTL_CREATE, CTL_EOL);
    478 }
    479