sys_sched.c revision 1.20 1 /* $NetBSD: sys_sched.c,v 1.20 2008/04/24 15:35:30 ad 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.20 2008/04/24 15:35:30 ad 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 l->l_priority + 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_smutex);
133 mutex_exit(proc_lock);
134 /* Disallow modification of system processes */
135 if ((p->p_flag & PK_SYSTEM) != 0) {
136 mutex_exit(&p->p_smutex);
137 return EPERM;
138 }
139 } else {
140 /* Use the calling process */
141 p = curlwp->l_proc;
142 mutex_enter(&p->p_smutex);
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_smutex);
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), ¶ms, sizeof(params));
209 if (error)
210 goto out;
211
212 error = do_sched_setparam(SCARG(uap, pid), SCARG(uap, lid),
213 SCARG(uap, policy), ¶ms);
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 error = ESRCH;
231 goto out;
232 }
233
234 /* Check the permission */
235 error = kauth_authorize_process(kauth_cred_get(),
236 KAUTH_PROCESS_SCHEDULER_GETPARAM, t->l_proc, NULL, NULL, NULL);
237 if (error != 0) {
238 lwp_unlock(t);
239 goto out;
240 }
241
242 lparams.sched_priority = t->l_priority;
243 lpolicy = t->l_class;
244 lwp_unlock(t);
245
246 switch (lpolicy) {
247 case SCHED_OTHER:
248 lparams.sched_priority -= PRI_USER;
249 break;
250 case SCHED_RR:
251 case SCHED_FIFO:
252 lparams.sched_priority -= PRI_USER_RT;
253 break;
254 }
255
256 if (policy != NULL)
257 *policy = lpolicy;
258
259 if (params != NULL)
260 *params = lparams;
261
262 out:
263 return error;
264 }
265
266 /*
267 * Get scheduling parameters.
268 */
269 int
270 sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
271 register_t *retval)
272 {
273 /* {
274 syscallarg(pid_t) pid;
275 syscallarg(lwpid_t) lid;
276 syscallarg(int *) policy;
277 syscallarg(struct sched_param *) params;
278 } */
279 struct sched_param params;
280 int error, policy;
281
282 error = do_sched_getparam(SCARG(uap, pid), SCARG(uap, lid), &policy,
283 ¶ms);
284 if (error)
285 goto out;
286
287 error = copyout(¶ms, SCARG(uap, params), sizeof(params));
288 if (error == 0 && SCARG(uap, policy) != NULL)
289 error = copyout(&policy, SCARG(uap, policy), sizeof(int));
290
291 out:
292 return (error);
293 }
294
295 /*
296 * Set affinity.
297 */
298 int
299 sys__sched_setaffinity(struct lwp *l,
300 const struct sys__sched_setaffinity_args *uap, register_t *retval)
301 {
302 /* {
303 syscallarg(pid_t) pid;
304 syscallarg(lwpid_t) lid;
305 syscallarg(size_t) size;
306 syscallarg(void *) cpuset;
307 } */
308 cpuset_t *cpuset;
309 struct cpu_info *ci = NULL;
310 struct proc *p;
311 struct lwp *t;
312 CPU_INFO_ITERATOR cii;
313 lwpid_t lid;
314 u_int lcnt;
315 int error;
316
317 /* Allocate the CPU set, and get it from userspace */
318 cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
319 error = copyin(SCARG(uap, cpuset), cpuset,
320 min(SCARG(uap, size), sizeof(cpuset_t)));
321 if (error)
322 goto error;
323
324 /* Look for a CPU in the set */
325 for (CPU_INFO_FOREACH(cii, ci))
326 if (CPU_ISSET(cpu_index(ci), cpuset))
327 break;
328 if (ci == NULL) {
329 /* Empty set */
330 kmem_free(cpuset, sizeof(cpuset_t));
331 cpuset = NULL;
332 }
333
334 if (SCARG(uap, pid) != 0) {
335 /* Find the process */
336 mutex_enter(proc_lock);
337 p = p_find(SCARG(uap, pid), PFIND_LOCKED);
338 if (p == NULL) {
339 mutex_exit(proc_lock);
340 error = ESRCH;
341 goto error;
342 }
343 mutex_enter(&p->p_smutex);
344 mutex_exit(proc_lock);
345 /* Disallow modification of system processes. */
346 if ((p->p_flag & PK_SYSTEM) != 0) {
347 mutex_exit(&p->p_smutex);
348 error = EPERM;
349 goto error;
350 }
351 } else {
352 /* Use the calling process */
353 p = l->l_proc;
354 mutex_enter(&p->p_smutex);
355 }
356
357 /*
358 * Check the permission.
359 */
360 error = kauth_authorize_process(l->l_cred,
361 KAUTH_PROCESS_SCHEDULER_SETAFFINITY, p, NULL, NULL, NULL);
362 if (error != 0) {
363 mutex_exit(&p->p_smutex);
364 goto error;
365 }
366
367 /* Find the LWP(s) */
368 lcnt = 0;
369 lid = SCARG(uap, lid);
370 LIST_FOREACH(t, &p->p_lwps, l_sibling) {
371 if (lid && lid != t->l_lid)
372 continue;
373 lwp_lock(t);
374 if (cpuset) {
375 /* Set the affinity flag and new CPU set */
376 t->l_flag |= LW_AFFINITY;
377 memcpy(&t->l_affinity, cpuset, sizeof(cpuset_t));
378 /* Migrate to another CPU, unlocks LWP */
379 lwp_migrate(t, ci);
380 } else {
381 /* Unset the affinity flag */
382 t->l_flag &= ~LW_AFFINITY;
383 lwp_unlock(t);
384 }
385 lcnt++;
386 }
387 mutex_exit(&p->p_smutex);
388 if (lcnt == 0)
389 error = ESRCH;
390 error:
391 if (cpuset != NULL)
392 kmem_free(cpuset, sizeof(cpuset_t));
393 return error;
394 }
395
396 /*
397 * Get affinity.
398 */
399 int
400 sys__sched_getaffinity(struct lwp *l,
401 const struct sys__sched_getaffinity_args *uap, register_t *retval)
402 {
403 /* {
404 syscallarg(pid_t) pid;
405 syscallarg(lwpid_t) lid;
406 syscallarg(size_t) size;
407 syscallarg(void *) cpuset;
408 } */
409 struct lwp *t;
410 void *cpuset;
411 int error;
412
413 if (SCARG(uap, size) <= 0)
414 return EINVAL;
415 cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
416
417 /* Locks the LWP */
418 t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
419 if (t == NULL) {
420 kmem_free(cpuset, sizeof(cpuset_t));
421 return ESRCH;
422 }
423 /* Check the permission */
424 if (kauth_authorize_process(l->l_cred,
425 KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
426 lwp_unlock(t);
427 kmem_free(cpuset, sizeof(cpuset_t));
428 return EPERM;
429 }
430 if (t->l_flag & LW_AFFINITY)
431 memcpy(cpuset, &t->l_affinity, sizeof(cpuset_t));
432 lwp_unlock(t);
433
434 error = copyout(cpuset, SCARG(uap, cpuset),
435 min(SCARG(uap, size), sizeof(cpuset_t)));
436
437 kmem_free(cpuset, sizeof(cpuset_t));
438 return error;
439 }
440
441 /*
442 * Yield.
443 */
444 int
445 sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
446 {
447
448 yield();
449 return 0;
450 }
451
452 /*
453 * Sysctl nodes and initialization.
454 */
455 SYSCTL_SETUP(sysctl_sched_setup, "sysctl sched setup")
456 {
457 const struct sysctlnode *node = NULL;
458
459 sysctl_createv(clog, 0, NULL, NULL,
460 CTLFLAG_PERMANENT,
461 CTLTYPE_NODE, "kern", NULL,
462 NULL, 0, NULL, 0,
463 CTL_KERN, CTL_EOL);
464 sysctl_createv(clog, 0, NULL, NULL,
465 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
466 CTLTYPE_INT, "posix_sched",
467 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
468 "Process Scheduling option to which the "
469 "system attempts to conform"),
470 NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
471 CTL_KERN, CTL_CREATE, CTL_EOL);
472 sysctl_createv(clog, 0, NULL, &node,
473 CTLFLAG_PERMANENT,
474 CTLTYPE_NODE, "sched",
475 SYSCTL_DESCR("Scheduler options"),
476 NULL, 0, NULL, 0,
477 CTL_KERN, CTL_CREATE, CTL_EOL);
478
479 if (node == NULL)
480 return;
481
482 sysctl_createv(clog, 0, &node, NULL,
483 CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
484 CTLTYPE_INT, "pri_min",
485 SYSCTL_DESCR("Minimal POSIX real-time priority"),
486 NULL, SCHED_PRI_MIN, NULL, 0,
487 CTL_CREATE, CTL_EOL);
488 sysctl_createv(clog, 0, &node, NULL,
489 CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
490 CTLTYPE_INT, "pri_max",
491 SYSCTL_DESCR("Maximal POSIX real-time priority"),
492 NULL, SCHED_PRI_MAX, NULL, 0,
493 CTL_CREATE, CTL_EOL);
494 }
495