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