sys_sched.c revision 1.16 1 /* $NetBSD: sys_sched.c,v 1.16 2008/02/22 22:32:49 rmind 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 * 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.16 2008/02/22 22:32:49 rmind 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), ¶m, 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 lcnt++;
166 KASSERT(pri != PRI_NONE || policy != SCHED_NONE);
167 lwp_lock(t);
168
169 if (policy == SCHED_NONE)
170 lpolicy = t->l_class;
171 else
172 lpolicy = policy;
173
174 /*
175 * Note that, priority may need to be changed to get into
176 * the correct priority range of the new scheduling class.
177 */
178 kpri = convert_pri(t, lpolicy, pri);
179
180 /* Check the permission */
181 error = kauth_authorize_process(l->l_cred,
182 KAUTH_PROCESS_SCHEDULER_SETPARAM, p, t, KAUTH_ARG(lpolicy),
183 KAUTH_ARG(kpri));
184 if (error) {
185 lwp_unlock(t);
186 break;
187 }
188
189 /* Set the scheduling class */
190 if (policy != SCHED_NONE)
191 t->l_class = policy;
192
193 /* Change the priority */
194 if (t->l_priority != kpri)
195 lwp_changepri(t, kpri);
196
197 lwp_unlock(t);
198 }
199 mutex_exit(&p->p_smutex);
200 return (lcnt == 0) ? ESRCH : error;
201 }
202
203 /*
204 * Get scheduling parameters.
205 */
206 int
207 sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
208 register_t *retval)
209 {
210 /* {
211 syscallarg(pid_t) pid;
212 syscallarg(lwpid_t) lid;
213 syscallarg(int *) policy;
214 syscallarg(struct sched_param *) params;
215 } */
216 struct sched_param param;
217 struct lwp *t;
218 int error, policy;
219
220 /* Locks the LWP */
221 t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
222 if (t == NULL)
223 return ESRCH;
224
225 /* Check the permission */
226 error = kauth_authorize_process(l->l_cred,
227 KAUTH_PROCESS_SCHEDULER_GETPARAM, t->l_proc, NULL, NULL, NULL);
228 if (error != 0) {
229 lwp_unlock(t);
230 return error;
231 }
232
233 param.sched_priority = t->l_priority;
234 policy = t->l_class;
235 lwp_unlock(t);
236
237 switch (policy) {
238 case SCHED_OTHER:
239 param.sched_priority -= PRI_USER;
240 break;
241 case SCHED_RR:
242 case SCHED_FIFO:
243 param.sched_priority -= PRI_USER_RT;
244 break;
245 }
246 error = copyout(¶m, SCARG(uap, params), sizeof(param));
247 if (error == 0 && SCARG(uap, policy) != NULL)
248 error = copyout(&policy, SCARG(uap, policy), sizeof(int));
249 return error;
250 }
251
252 /*
253 * Set affinity.
254 */
255 int
256 sys__sched_setaffinity(struct lwp *l,
257 const struct sys__sched_setaffinity_args *uap, register_t *retval)
258 {
259 /* {
260 syscallarg(pid_t) pid;
261 syscallarg(lwpid_t) lid;
262 syscallarg(size_t) size;
263 syscallarg(void *) cpuset;
264 } */
265 cpuset_t *cpuset;
266 struct cpu_info *ci = NULL;
267 struct proc *p;
268 struct lwp *t;
269 CPU_INFO_ITERATOR cii;
270 lwpid_t lid;
271 u_int lcnt;
272 int error;
273
274 /* Allocate the CPU set, and get it from userspace */
275 cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
276 error = copyin(SCARG(uap, cpuset), cpuset,
277 min(SCARG(uap, size), sizeof(cpuset_t)));
278 if (error)
279 goto error;
280
281 /* Look for a CPU in the set */
282 for (CPU_INFO_FOREACH(cii, ci))
283 if (CPU_ISSET(cpu_index(ci), cpuset))
284 break;
285 if (ci == NULL) {
286 /* Empty set */
287 kmem_free(cpuset, sizeof(cpuset_t));
288 cpuset = NULL;
289 }
290
291 if (SCARG(uap, pid) != 0) {
292 /* Find the process */
293 p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL);
294 if (p == NULL) {
295 error = ESRCH;
296 goto error;
297 }
298 mutex_enter(&p->p_smutex);
299 mutex_exit(&proclist_lock);
300 } else {
301 /* Use the calling process */
302 p = l->l_proc;
303 mutex_enter(&p->p_smutex);
304 }
305
306 /*
307 * Check the permission.
308 * Disallow modification of system processes.
309 */
310 error = kauth_authorize_process(l->l_cred,
311 KAUTH_PROCESS_SCHEDULER_SETAFFINITY, p, NULL, NULL, NULL);
312 if (error != 0) {
313 mutex_exit(&p->p_smutex);
314 goto error;
315 }
316 if ((p->p_flag & PK_SYSTEM) != 0) {
317 mutex_exit(&p->p_smutex);
318 error = EPERM;
319 goto error;
320 }
321
322 /* Find the LWP(s) */
323 lcnt = 0;
324 lid = SCARG(uap, lid);
325 LIST_FOREACH(t, &p->p_lwps, l_sibling) {
326 if (lid && lid != t->l_lid)
327 continue;
328 lwp_lock(t);
329 if (cpuset) {
330 /* Set the affinity flag and new CPU set */
331 t->l_flag |= LW_AFFINITY;
332 memcpy(&t->l_affinity, cpuset, sizeof(cpuset_t));
333 /* Migrate to another CPU, unlocks LWP */
334 lwp_migrate(t, ci);
335 } else {
336 /* Unset the affinity flag */
337 t->l_flag &= ~LW_AFFINITY;
338 lwp_unlock(t);
339 }
340 lcnt++;
341 }
342 mutex_exit(&p->p_smutex);
343 if (lcnt == 0)
344 error = ESRCH;
345 error:
346 if (cpuset != NULL)
347 kmem_free(cpuset, sizeof(cpuset_t));
348 return error;
349 }
350
351 /*
352 * Get affinity.
353 */
354 int
355 sys__sched_getaffinity(struct lwp *l,
356 const struct sys__sched_getaffinity_args *uap, register_t *retval)
357 {
358 /* {
359 syscallarg(pid_t) pid;
360 syscallarg(lwpid_t) lid;
361 syscallarg(size_t) size;
362 syscallarg(void *) cpuset;
363 } */
364 struct lwp *t;
365 void *cpuset;
366 int error;
367
368 if (SCARG(uap, size) <= 0)
369 return EINVAL;
370 cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
371
372 /* Locks the LWP */
373 t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
374 if (t == NULL) {
375 kmem_free(cpuset, sizeof(cpuset_t));
376 return ESRCH;
377 }
378 /* Check the permission */
379 if (kauth_authorize_process(l->l_cred,
380 KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
381 lwp_unlock(t);
382 kmem_free(cpuset, sizeof(cpuset_t));
383 return EPERM;
384 }
385 if (t->l_flag & LW_AFFINITY)
386 memcpy(cpuset, &t->l_affinity, sizeof(cpuset_t));
387 lwp_unlock(t);
388
389 error = copyout(cpuset, SCARG(uap, cpuset),
390 min(SCARG(uap, size), sizeof(cpuset_t)));
391
392 kmem_free(cpuset, sizeof(cpuset_t));
393 return error;
394 }
395
396 /*
397 * Yield.
398 */
399 int
400 sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
401 {
402
403 yield();
404 return 0;
405 }
406
407 /*
408 * Sysctl nodes and initialization.
409 */
410 SYSCTL_SETUP(sysctl_sched_setup, "sysctl sched setup")
411 {
412 const struct sysctlnode *node = NULL;
413
414 sysctl_createv(clog, 0, NULL, NULL,
415 CTLFLAG_PERMANENT,
416 CTLTYPE_NODE, "kern", NULL,
417 NULL, 0, NULL, 0,
418 CTL_KERN, CTL_EOL);
419 sysctl_createv(clog, 0, NULL, NULL,
420 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
421 CTLTYPE_INT, "posix_sched",
422 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
423 "Process Scheduling option to which the "
424 "system attempts to conform"),
425 NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
426 CTL_KERN, CTL_CREATE, CTL_EOL);
427 sysctl_createv(clog, 0, NULL, &node,
428 CTLFLAG_PERMANENT,
429 CTLTYPE_NODE, "sched",
430 SYSCTL_DESCR("Scheduler options"),
431 NULL, 0, NULL, 0,
432 CTL_KERN, CTL_CREATE, CTL_EOL);
433
434 if (node == NULL)
435 return;
436
437 sysctl_createv(clog, 0, &node, NULL,
438 CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
439 CTLTYPE_INT, "pri_min",
440 SYSCTL_DESCR("Minimal POSIX real-time priority"),
441 NULL, SCHED_PRI_MIN, NULL, 0,
442 CTL_CREATE, CTL_EOL);
443 sysctl_createv(clog, 0, &node, NULL,
444 CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
445 CTLTYPE_INT, "pri_max",
446 SYSCTL_DESCR("Minimal POSIX real-time priority"),
447 NULL, SCHED_PRI_MAX, NULL, 0,
448 CTL_CREATE, CTL_EOL);
449 }
450