sys_sched.c revision 1.7 1 1.7 rmind /* $NetBSD: sys_sched.c,v 1.7 2008/01/26 17:55:29 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.7 rmind __KERNEL_RCSID(0, "$NetBSD: sys_sched.c,v 1.7 2008/01/26 17:55:29 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.7 rmind * Convert user priority or the in-kernel priority or convert the current
56 1.7 rmind * priority to the appropriate range according to the policy change.
57 1.7 rmind */
58 1.7 rmind static pri_t
59 1.7 rmind convert_pri(lwp_t *l, int policy, pri_t pri)
60 1.7 rmind {
61 1.7 rmind int delta = 0;
62 1.7 rmind
63 1.7 rmind if (policy == SCHED_NONE)
64 1.7 rmind policy = l->l_class;
65 1.7 rmind
66 1.7 rmind switch (policy) {
67 1.7 rmind case SCHED_OTHER:
68 1.7 rmind delta = PRI_USER;
69 1.7 rmind break;
70 1.7 rmind case SCHED_FIFO:
71 1.7 rmind case SCHED_RR:
72 1.7 rmind delta = PRI_USER_RT;
73 1.7 rmind break;
74 1.7 rmind default:
75 1.7 rmind panic("upri_to_kpri");
76 1.7 rmind }
77 1.7 rmind
78 1.7 rmind if (pri != PRI_NONE) {
79 1.7 rmind /* Convert user priority to the in-kernel */
80 1.7 rmind KASSERT(pri >= SCHED_PRI_MIN && pri <= SCHED_PRI_MAX);
81 1.7 rmind return pri + delta;
82 1.7 rmind }
83 1.7 rmind if (l->l_class == policy)
84 1.7 rmind return l->l_priority;
85 1.7 rmind
86 1.7 rmind /* Change the current priority to the appropriate range */
87 1.7 rmind if (l->l_class == SCHED_OTHER) {
88 1.7 rmind KASSERT(policy == SCHED_FIFO || policy == SCHED_RR);
89 1.7 rmind return l->l_priority + delta;
90 1.7 rmind }
91 1.7 rmind if (policy == SCHED_OTHER) {
92 1.7 rmind KASSERT(l->l_class == SCHED_FIFO || l->l_class == SCHED_RR);
93 1.7 rmind return l->l_priority - delta;
94 1.7 rmind }
95 1.7 rmind KASSERT(l->l_class != SCHED_OTHER && policy != SCHED_OTHER);
96 1.7 rmind return l->l_class;
97 1.7 rmind }
98 1.7 rmind
99 1.7 rmind /*
100 1.5 rmind * Set scheduling parameters.
101 1.5 rmind */
102 1.5 rmind int
103 1.5 rmind sys__sched_setparam(struct lwp *l, const struct sys__sched_setparam_args *uap,
104 1.5 rmind register_t *retval)
105 1.5 rmind {
106 1.5 rmind /* {
107 1.5 rmind syscallarg(pid_t) pid;
108 1.5 rmind syscallarg(lwpid_t) lid;
109 1.5 rmind syscallarg(const struct sched_param *) params;
110 1.5 rmind } */
111 1.5 rmind struct sched_param *sp;
112 1.5 rmind struct proc *p;
113 1.5 rmind struct lwp *t;
114 1.5 rmind lwpid_t lid;
115 1.5 rmind u_int lcnt;
116 1.7 rmind int policy;
117 1.5 rmind pri_t pri;
118 1.5 rmind int error;
119 1.5 rmind
120 1.5 rmind /* Available only for super-user */
121 1.5 rmind if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL))
122 1.7 rmind return EPERM;
123 1.5 rmind
124 1.5 rmind /* Get the parameters from the user-space */
125 1.5 rmind sp = kmem_zalloc(sizeof(struct sched_param), KM_SLEEP);
126 1.5 rmind error = copyin(SCARG(uap, params), sp, sizeof(struct sched_param));
127 1.7 rmind if (error) {
128 1.7 rmind kmem_free(sp, sizeof(struct sched_param));
129 1.7 rmind return error;
130 1.7 rmind }
131 1.7 rmind pri = sp->sched_priority;
132 1.7 rmind policy = sp->sched_class;
133 1.7 rmind kmem_free(sp, sizeof(struct sched_param));
134 1.7 rmind
135 1.7 rmind /* If no parameters specified, just return (this should not happen) */
136 1.7 rmind if (pri == PRI_NONE && policy == SCHED_NONE)
137 1.7 rmind return 0;
138 1.5 rmind
139 1.7 rmind /* Validate scheduling class */
140 1.7 rmind if (policy != SCHED_NONE && (policy < SCHED_OTHER || policy > SCHED_RR))
141 1.7 rmind return EINVAL;
142 1.5 rmind
143 1.7 rmind /* Validate priority */
144 1.7 rmind if (pri != PRI_NONE && (pri < SCHED_PRI_MIN || pri > SCHED_PRI_MAX))
145 1.7 rmind return EINVAL;
146 1.5 rmind
147 1.7 rmind if (SCARG(uap, pid) != 0) {
148 1.7 rmind /* Find the process */
149 1.7 rmind p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL);
150 1.7 rmind if (p == NULL)
151 1.7 rmind return ESRCH;
152 1.7 rmind mutex_enter(&p->p_smutex);
153 1.7 rmind mutex_exit(&proclist_lock);
154 1.7 rmind /* Disallow modification of system processes */
155 1.7 rmind if (p->p_flag & PK_SYSTEM) {
156 1.7 rmind mutex_exit(&p->p_smutex);
157 1.7 rmind return EPERM;
158 1.7 rmind }
159 1.7 rmind } else {
160 1.7 rmind /* Use the calling process */
161 1.7 rmind p = l->l_proc;
162 1.7 rmind mutex_enter(&p->p_smutex);
163 1.5 rmind }
164 1.1 ad
165 1.5 rmind /* Find the LWP(s) */
166 1.5 rmind lcnt = 0;
167 1.5 rmind lid = SCARG(uap, lid);
168 1.5 rmind LIST_FOREACH(t, &p->p_lwps, l_sibling) {
169 1.7 rmind pri_t kpri;
170 1.5 rmind
171 1.5 rmind if (lid && lid != t->l_lid)
172 1.5 rmind continue;
173 1.7 rmind KASSERT(pri != PRI_NONE || policy != SCHED_NONE);
174 1.7 rmind lwp_lock(t);
175 1.7 rmind
176 1.7 rmind /*
177 1.7 rmind * Note that, priority may need to be changed to get into
178 1.7 rmind * the correct priority range of the new scheduling class.
179 1.7 rmind */
180 1.7 rmind kpri = convert_pri(t, policy, pri);
181 1.5 rmind
182 1.5 rmind /* Set the scheduling class */
183 1.7 rmind if (policy != SCHED_NONE)
184 1.7 rmind t->l_class = policy;
185 1.5 rmind
186 1.5 rmind /* Change the priority */
187 1.7 rmind if (t->l_priority != kpri)
188 1.7 rmind lwp_changepri(t, kpri);
189 1.5 rmind
190 1.5 rmind lwp_unlock(t);
191 1.5 rmind lcnt++;
192 1.5 rmind }
193 1.5 rmind mutex_exit(&p->p_smutex);
194 1.7 rmind return (lcnt == 0) ? ESRCH : error;
195 1.5 rmind }
196 1.5 rmind
197 1.5 rmind /*
198 1.5 rmind * Get scheduling parameters.
199 1.5 rmind */
200 1.5 rmind int
201 1.5 rmind sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
202 1.5 rmind register_t *retval)
203 1.5 rmind {
204 1.5 rmind /* {
205 1.5 rmind syscallarg(pid_t) pid;
206 1.5 rmind syscallarg(lwpid_t) lid;
207 1.5 rmind syscallarg(struct sched_param *) params;
208 1.5 rmind } */
209 1.5 rmind struct sched_param *sp;
210 1.5 rmind struct lwp *t;
211 1.7 rmind lwpid_t lid;
212 1.5 rmind int error;
213 1.5 rmind
214 1.5 rmind sp = kmem_zalloc(sizeof(struct sched_param), KM_SLEEP);
215 1.5 rmind
216 1.7 rmind /* If not specified, use the first LWP */
217 1.7 rmind lid = SCARG(uap, lid) == 0 ? 1 : SCARG(uap, lid);
218 1.7 rmind
219 1.7 rmind if (SCARG(uap, pid) != 0) {
220 1.7 rmind /* Locks the LWP */
221 1.7 rmind t = lwp_find2(SCARG(uap, pid), lid);
222 1.7 rmind } else {
223 1.7 rmind struct proc *p = l->l_proc;
224 1.7 rmind /* Use the calling process */
225 1.7 rmind mutex_enter(&p->p_smutex);
226 1.7 rmind t = lwp_find(p, lid);
227 1.7 rmind if (t != NULL)
228 1.7 rmind lwp_lock(t);
229 1.7 rmind mutex_exit(&p->p_smutex);
230 1.7 rmind }
231 1.5 rmind if (t == NULL) {
232 1.5 rmind kmem_free(sp, sizeof(struct sched_param));
233 1.5 rmind return ESRCH;
234 1.5 rmind }
235 1.5 rmind sp->sched_priority = t->l_priority;
236 1.5 rmind sp->sched_class = t->l_class;
237 1.5 rmind lwp_unlock(t);
238 1.5 rmind
239 1.5 rmind switch (sp->sched_class) {
240 1.5 rmind case SCHED_OTHER:
241 1.5 rmind sp->sched_priority -= PRI_USER;
242 1.5 rmind break;
243 1.5 rmind case SCHED_RR:
244 1.5 rmind case SCHED_FIFO:
245 1.5 rmind sp->sched_priority -= PRI_USER_RT;
246 1.5 rmind break;
247 1.5 rmind }
248 1.5 rmind error = copyout(sp, SCARG(uap, params), sizeof(struct sched_param));
249 1.5 rmind kmem_free(sp, sizeof(struct sched_param));
250 1.5 rmind return error;
251 1.5 rmind }
252 1.5 rmind
253 1.5 rmind /*
254 1.5 rmind * Set affinity.
255 1.5 rmind */
256 1.5 rmind int
257 1.5 rmind sys__sched_setaffinity(struct lwp *l,
258 1.5 rmind const struct sys__sched_setaffinity_args *uap, register_t *retval)
259 1.5 rmind {
260 1.5 rmind /* {
261 1.5 rmind syscallarg(pid_t) pid;
262 1.5 rmind syscallarg(lwpid_t) lid;
263 1.5 rmind syscallarg(size_t) size;
264 1.5 rmind syscallarg(void *) cpuset;
265 1.5 rmind } */
266 1.5 rmind cpuset_t *cpuset;
267 1.5 rmind struct cpu_info *ci = NULL;
268 1.5 rmind struct proc *p;
269 1.5 rmind struct lwp *t;
270 1.5 rmind CPU_INFO_ITERATOR cii;
271 1.5 rmind lwpid_t lid;
272 1.5 rmind u_int lcnt;
273 1.5 rmind int error;
274 1.5 rmind
275 1.5 rmind /* Available only for super-user */
276 1.5 rmind if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL))
277 1.7 rmind return EPERM;
278 1.5 rmind
279 1.5 rmind if (SCARG(uap, size) <= 0)
280 1.5 rmind return EINVAL;
281 1.5 rmind
282 1.5 rmind /* Allocate the CPU set, and get it from userspace */
283 1.5 rmind cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
284 1.5 rmind error = copyin(SCARG(uap, cpuset), cpuset,
285 1.5 rmind min(SCARG(uap, size), sizeof(cpuset_t)));
286 1.5 rmind if (error)
287 1.5 rmind goto error;
288 1.5 rmind
289 1.5 rmind /* Look for a CPU in the set */
290 1.5 rmind for (CPU_INFO_FOREACH(cii, ci))
291 1.5 rmind if (CPU_ISSET(cpu_index(ci), cpuset))
292 1.5 rmind break;
293 1.5 rmind if (ci == NULL) {
294 1.5 rmind /* Empty set */
295 1.5 rmind kmem_free(cpuset, sizeof(cpuset_t));
296 1.5 rmind cpuset = NULL;
297 1.5 rmind }
298 1.5 rmind
299 1.7 rmind if (SCARG(uap, pid) != 0) {
300 1.7 rmind /* Find the process */
301 1.7 rmind p = p_find(SCARG(uap, pid), PFIND_UNLOCK_FAIL);
302 1.7 rmind if (p == NULL) {
303 1.7 rmind error = ESRCH;
304 1.7 rmind goto error;
305 1.7 rmind }
306 1.7 rmind mutex_enter(&p->p_smutex);
307 1.7 rmind mutex_exit(&proclist_lock);
308 1.7 rmind } else {
309 1.7 rmind /* Use the calling process */
310 1.7 rmind p = l->l_proc;
311 1.7 rmind mutex_enter(&p->p_smutex);
312 1.5 rmind }
313 1.5 rmind
314 1.5 rmind /* Disallow modification of system processes */
315 1.5 rmind if (p->p_flag & PK_SYSTEM) {
316 1.5 rmind mutex_exit(&p->p_smutex);
317 1.7 rmind error = EPERM;
318 1.5 rmind goto error;
319 1.5 rmind }
320 1.5 rmind
321 1.5 rmind /* Find the LWP(s) */
322 1.5 rmind lcnt = 0;
323 1.5 rmind lid = SCARG(uap, lid);
324 1.5 rmind LIST_FOREACH(t, &p->p_lwps, l_sibling) {
325 1.5 rmind if (lid && lid != t->l_lid)
326 1.5 rmind continue;
327 1.5 rmind lwp_lock(t);
328 1.5 rmind if (cpuset) {
329 1.5 rmind /* Set the affinity flag and new CPU set */
330 1.5 rmind t->l_flag |= LW_AFFINITY;
331 1.5 rmind memcpy(&t->l_affinity, cpuset, sizeof(cpuset_t));
332 1.5 rmind /* Migrate to another CPU, unlocks LWP */
333 1.5 rmind lwp_migrate(t, ci);
334 1.5 rmind } else {
335 1.5 rmind /* Unset the affinity flag */
336 1.5 rmind t->l_flag &= ~LW_AFFINITY;
337 1.5 rmind lwp_unlock(t);
338 1.5 rmind }
339 1.5 rmind lcnt++;
340 1.5 rmind }
341 1.5 rmind mutex_exit(&p->p_smutex);
342 1.5 rmind if (lcnt == 0)
343 1.5 rmind error = ESRCH;
344 1.5 rmind error:
345 1.5 rmind if (cpuset != NULL)
346 1.5 rmind kmem_free(cpuset, sizeof(cpuset_t));
347 1.5 rmind return error;
348 1.5 rmind }
349 1.5 rmind
350 1.5 rmind /*
351 1.5 rmind * Get affinity.
352 1.5 rmind */
353 1.5 rmind int
354 1.5 rmind sys__sched_getaffinity(struct lwp *l,
355 1.5 rmind const struct sys__sched_getaffinity_args *uap, register_t *retval)
356 1.5 rmind {
357 1.5 rmind /* {
358 1.5 rmind syscallarg(pid_t) pid;
359 1.5 rmind syscallarg(lwpid_t) lid;
360 1.5 rmind syscallarg(size_t) size;
361 1.5 rmind syscallarg(void *) cpuset;
362 1.5 rmind } */
363 1.5 rmind struct lwp *t;
364 1.5 rmind void *cpuset;
365 1.7 rmind lwpid_t lid;
366 1.5 rmind int error;
367 1.5 rmind
368 1.5 rmind if (SCARG(uap, size) <= 0)
369 1.5 rmind return EINVAL;
370 1.5 rmind
371 1.5 rmind cpuset = kmem_zalloc(sizeof(cpuset_t), KM_SLEEP);
372 1.5 rmind
373 1.7 rmind /* If not specified, use the first LWP */
374 1.7 rmind lid = SCARG(uap, lid) == 0 ? 1 : SCARG(uap, lid);
375 1.7 rmind
376 1.7 rmind if (SCARG(uap, pid) != 0) {
377 1.7 rmind /* Locks the LWP */
378 1.7 rmind t = lwp_find2(SCARG(uap, pid), lid);
379 1.7 rmind } else {
380 1.7 rmind struct proc *p = l->l_proc;
381 1.7 rmind /* Use the calling process */
382 1.7 rmind mutex_enter(&p->p_smutex);
383 1.7 rmind t = lwp_find(p, lid);
384 1.7 rmind if (t != NULL)
385 1.7 rmind lwp_lock(t);
386 1.7 rmind mutex_exit(&p->p_smutex);
387 1.7 rmind }
388 1.5 rmind if (t == NULL) {
389 1.5 rmind kmem_free(cpuset, sizeof(cpuset_t));
390 1.5 rmind return ESRCH;
391 1.5 rmind }
392 1.5 rmind if (t->l_flag & LW_AFFINITY)
393 1.5 rmind memcpy(cpuset, &t->l_affinity, sizeof(cpuset_t));
394 1.5 rmind lwp_unlock(t);
395 1.5 rmind
396 1.5 rmind error = copyout(cpuset, SCARG(uap, cpuset),
397 1.5 rmind min(SCARG(uap, size), sizeof(cpuset_t)));
398 1.5 rmind
399 1.5 rmind kmem_free(cpuset, sizeof(cpuset_t));
400 1.5 rmind return error;
401 1.5 rmind }
402 1.5 rmind
403 1.5 rmind /*
404 1.5 rmind * Yield.
405 1.5 rmind */
406 1.1 ad int
407 1.4 dsl sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
408 1.1 ad {
409 1.1 ad
410 1.1 ad yield();
411 1.1 ad return 0;
412 1.1 ad }
413 1.5 rmind
414 1.5 rmind /*
415 1.5 rmind * Sysctl nodes and initialization.
416 1.5 rmind */
417 1.5 rmind SYSCTL_SETUP(sysctl_sched_setup, "sysctl sched setup")
418 1.5 rmind {
419 1.5 rmind const struct sysctlnode *node = NULL;
420 1.5 rmind
421 1.5 rmind sysctl_createv(clog, 0, NULL, NULL,
422 1.5 rmind CTLFLAG_PERMANENT,
423 1.5 rmind CTLTYPE_NODE, "kern", NULL,
424 1.5 rmind NULL, 0, NULL, 0,
425 1.5 rmind CTL_KERN, CTL_EOL);
426 1.5 rmind sysctl_createv(clog, 0, NULL, NULL,
427 1.5 rmind CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
428 1.5 rmind CTLTYPE_INT, "posix_sched",
429 1.5 rmind SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
430 1.5 rmind "Process Scheduling option to which the "
431 1.5 rmind "system attempts to conform"),
432 1.5 rmind NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
433 1.5 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
434 1.5 rmind sysctl_createv(clog, 0, NULL, &node,
435 1.5 rmind CTLFLAG_PERMANENT,
436 1.5 rmind CTLTYPE_NODE, "sched",
437 1.5 rmind SYSCTL_DESCR("Scheduler options"),
438 1.5 rmind NULL, 0, NULL, 0,
439 1.5 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
440 1.5 rmind
441 1.5 rmind if (node == NULL)
442 1.5 rmind return;
443 1.5 rmind
444 1.5 rmind sysctl_createv(clog, 0, &node, NULL,
445 1.5 rmind CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
446 1.5 rmind CTLTYPE_INT, "pri_min",
447 1.5 rmind SYSCTL_DESCR("Minimal POSIX real-time priority"),
448 1.5 rmind NULL, SCHED_PRI_MIN, NULL, 0,
449 1.5 rmind CTL_CREATE, CTL_EOL);
450 1.5 rmind sysctl_createv(clog, 0, &node, NULL,
451 1.5 rmind CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
452 1.5 rmind CTLTYPE_INT, "pri_max",
453 1.5 rmind SYSCTL_DESCR("Minimal POSIX real-time priority"),
454 1.5 rmind NULL, SCHED_PRI_MAX, NULL, 0,
455 1.5 rmind CTL_CREATE, CTL_EOL);
456 1.5 rmind }
457