sys_sched.c revision 1.34 1 1.34 elad /* $NetBSD: sys_sched.c,v 1.34 2009/10/03 22:32:56 elad 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.16 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.16 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.16 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.16 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.16 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.16 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.16 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.16 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.16 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.16 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.16 rmind * SUCH DAMAGE.
27 1.1 ad */
28 1.1 ad
29 1.5 rmind /*
30 1.17 ad * System calls relating to the scheduler.
31 1.17 ad *
32 1.31 rmind * Lock order:
33 1.31 rmind *
34 1.31 rmind * cpu_lock ->
35 1.31 rmind * proc_lock ->
36 1.31 rmind * proc_t::p_lock ->
37 1.31 rmind * lwp_t::lwp_lock
38 1.31 rmind *
39 1.5 rmind * TODO:
40 1.5 rmind * - Handle pthread_setschedprio() as defined by POSIX;
41 1.5 rmind * - Handle sched_yield() case for SCHED_FIFO as defined by POSIX;
42 1.5 rmind */
43 1.5 rmind
44 1.1 ad #include <sys/cdefs.h>
45 1.34 elad __KERNEL_RCSID(0, "$NetBSD: sys_sched.c,v 1.34 2009/10/03 22:32:56 elad Exp $");
46 1.1 ad
47 1.1 ad #include <sys/param.h>
48 1.5 rmind
49 1.5 rmind #include <sys/cpu.h>
50 1.5 rmind #include <sys/kauth.h>
51 1.5 rmind #include <sys/kmem.h>
52 1.5 rmind #include <sys/lwp.h>
53 1.5 rmind #include <sys/mutex.h>
54 1.1 ad #include <sys/proc.h>
55 1.5 rmind #include <sys/pset.h>
56 1.28 wrstuden #include <sys/sa.h>
57 1.28 wrstuden #include <sys/savar.h>
58 1.5 rmind #include <sys/sched.h>
59 1.1 ad #include <sys/syscallargs.h>
60 1.5 rmind #include <sys/sysctl.h>
61 1.5 rmind #include <sys/systm.h>
62 1.5 rmind #include <sys/types.h>
63 1.5 rmind #include <sys/unistd.h>
64 1.5 rmind
65 1.28 wrstuden #include "opt_sa.h"
66 1.28 wrstuden
67 1.34 elad static struct sysctllog *sched_sysctl_log;
68 1.34 elad static kauth_listener_t sched_listener;
69 1.34 elad
70 1.5 rmind /*
71 1.7 rmind * Convert user priority or the in-kernel priority or convert the current
72 1.7 rmind * priority to the appropriate range according to the policy change.
73 1.7 rmind */
74 1.7 rmind static pri_t
75 1.7 rmind convert_pri(lwp_t *l, int policy, pri_t pri)
76 1.7 rmind {
77 1.7 rmind
78 1.29 rmind /* Convert user priority to the in-kernel */
79 1.7 rmind if (pri != PRI_NONE) {
80 1.29 rmind /* Only for real-time threads */
81 1.7 rmind KASSERT(pri >= SCHED_PRI_MIN && pri <= SCHED_PRI_MAX);
82 1.29 rmind KASSERT(policy != SCHED_OTHER);
83 1.29 rmind return PRI_USER_RT + pri;
84 1.7 rmind }
85 1.29 rmind
86 1.29 rmind /* Neither policy, nor priority change */
87 1.7 rmind if (l->l_class == policy)
88 1.7 rmind return l->l_priority;
89 1.7 rmind
90 1.29 rmind /* Time-sharing -> real-time */
91 1.7 rmind if (l->l_class == SCHED_OTHER) {
92 1.7 rmind KASSERT(policy == SCHED_FIFO || policy == SCHED_RR);
93 1.29 rmind return PRI_USER_RT;
94 1.7 rmind }
95 1.29 rmind
96 1.29 rmind /* Real-time -> time-sharing */
97 1.7 rmind if (policy == SCHED_OTHER) {
98 1.7 rmind KASSERT(l->l_class == SCHED_FIFO || l->l_class == SCHED_RR);
99 1.29 rmind return l->l_priority - PRI_USER_RT;
100 1.7 rmind }
101 1.29 rmind
102 1.29 rmind /* Real-time -> real-time */
103 1.29 rmind return l->l_priority;
104 1.7 rmind }
105 1.7 rmind
106 1.5 rmind int
107 1.18 elad do_sched_setparam(pid_t pid, lwpid_t lid, int policy,
108 1.18 elad const struct sched_param *params)
109 1.5 rmind {
110 1.5 rmind struct proc *p;
111 1.5 rmind struct lwp *t;
112 1.18 elad pri_t pri;
113 1.5 rmind u_int lcnt;
114 1.5 rmind int error;
115 1.5 rmind
116 1.18 elad error = 0;
117 1.18 elad
118 1.18 elad pri = params->sched_priority;
119 1.7 rmind
120 1.7 rmind /* If no parameters specified, just return (this should not happen) */
121 1.7 rmind if (pri == PRI_NONE && policy == SCHED_NONE)
122 1.7 rmind return 0;
123 1.5 rmind
124 1.7 rmind /* Validate scheduling class */
125 1.7 rmind if (policy != SCHED_NONE && (policy < SCHED_OTHER || policy > SCHED_RR))
126 1.7 rmind return EINVAL;
127 1.5 rmind
128 1.7 rmind /* Validate priority */
129 1.7 rmind if (pri != PRI_NONE && (pri < SCHED_PRI_MIN || pri > SCHED_PRI_MAX))
130 1.7 rmind return EINVAL;
131 1.5 rmind
132 1.18 elad if (pid != 0) {
133 1.7 rmind /* Find the process */
134 1.20 ad mutex_enter(proc_lock);
135 1.20 ad p = p_find(pid, PFIND_LOCKED);
136 1.20 ad if (p == NULL) {
137 1.20 ad mutex_exit(proc_lock);
138 1.7 rmind return ESRCH;
139 1.20 ad }
140 1.21 ad mutex_enter(p->p_lock);
141 1.20 ad mutex_exit(proc_lock);
142 1.7 rmind /* Disallow modification of system processes */
143 1.17 ad if ((p->p_flag & PK_SYSTEM) != 0) {
144 1.21 ad mutex_exit(p->p_lock);
145 1.7 rmind return EPERM;
146 1.7 rmind }
147 1.7 rmind } else {
148 1.7 rmind /* Use the calling process */
149 1.18 elad p = curlwp->l_proc;
150 1.21 ad mutex_enter(p->p_lock);
151 1.5 rmind }
152 1.1 ad
153 1.5 rmind /* Find the LWP(s) */
154 1.5 rmind lcnt = 0;
155 1.5 rmind LIST_FOREACH(t, &p->p_lwps, l_sibling) {
156 1.7 rmind pri_t kpri;
157 1.12 elad int lpolicy;
158 1.5 rmind
159 1.5 rmind if (lid && lid != t->l_lid)
160 1.5 rmind continue;
161 1.29 rmind
162 1.15 drochner lcnt++;
163 1.7 rmind lwp_lock(t);
164 1.29 rmind lpolicy = (policy == SCHED_NONE) ? t->l_class : policy;
165 1.29 rmind
166 1.29 rmind /* Disallow setting of priority for SCHED_OTHER threads */
167 1.30 rmind if (lpolicy == SCHED_OTHER && pri != PRI_NONE) {
168 1.29 rmind lwp_unlock(t);
169 1.29 rmind error = EINVAL;
170 1.29 rmind break;
171 1.29 rmind }
172 1.7 rmind
173 1.29 rmind /* Convert priority, if needed */
174 1.12 elad kpri = convert_pri(t, lpolicy, pri);
175 1.12 elad
176 1.12 elad /* Check the permission */
177 1.18 elad error = kauth_authorize_process(kauth_cred_get(),
178 1.12 elad KAUTH_PROCESS_SCHEDULER_SETPARAM, p, t, KAUTH_ARG(lpolicy),
179 1.12 elad KAUTH_ARG(kpri));
180 1.14 yamt if (error) {
181 1.14 yamt lwp_unlock(t);
182 1.12 elad break;
183 1.14 yamt }
184 1.5 rmind
185 1.29 rmind /* Set the scheduling class, change the priority */
186 1.29 rmind t->l_class = lpolicy;
187 1.29 rmind lwp_changepri(t, kpri);
188 1.5 rmind lwp_unlock(t);
189 1.5 rmind }
190 1.21 ad mutex_exit(p->p_lock);
191 1.7 rmind return (lcnt == 0) ? ESRCH : error;
192 1.5 rmind }
193 1.5 rmind
194 1.5 rmind /*
195 1.18 elad * Set scheduling parameters.
196 1.5 rmind */
197 1.5 rmind int
198 1.18 elad sys__sched_setparam(struct lwp *l, const struct sys__sched_setparam_args *uap,
199 1.5 rmind register_t *retval)
200 1.5 rmind {
201 1.5 rmind /* {
202 1.5 rmind syscallarg(pid_t) pid;
203 1.5 rmind syscallarg(lwpid_t) lid;
204 1.18 elad syscallarg(int) policy;
205 1.18 elad syscallarg(const struct sched_param *) params;
206 1.5 rmind } */
207 1.18 elad struct sched_param params;
208 1.18 elad int error;
209 1.18 elad
210 1.18 elad /* Get the parameters from the user-space */
211 1.18 elad error = copyin(SCARG(uap, params), ¶ms, sizeof(params));
212 1.18 elad if (error)
213 1.18 elad goto out;
214 1.18 elad
215 1.18 elad error = do_sched_setparam(SCARG(uap, pid), SCARG(uap, lid),
216 1.18 elad SCARG(uap, policy), ¶ms);
217 1.31 rmind out:
218 1.31 rmind return error;
219 1.18 elad }
220 1.18 elad
221 1.18 elad int
222 1.18 elad do_sched_getparam(pid_t pid, lwpid_t lid, int *policy,
223 1.18 elad struct sched_param *params)
224 1.18 elad {
225 1.18 elad struct sched_param lparams;
226 1.5 rmind struct lwp *t;
227 1.18 elad int error, lpolicy;
228 1.5 rmind
229 1.16 rmind /* Locks the LWP */
230 1.18 elad t = lwp_find2(pid, lid);
231 1.21 ad if (t == NULL)
232 1.21 ad return ESRCH;
233 1.10 yamt
234 1.10 yamt /* Check the permission */
235 1.18 elad error = kauth_authorize_process(kauth_cred_get(),
236 1.11 elad KAUTH_PROCESS_SCHEDULER_GETPARAM, t->l_proc, NULL, NULL, NULL);
237 1.10 yamt if (error != 0) {
238 1.21 ad mutex_exit(t->l_proc->p_lock);
239 1.21 ad return error;
240 1.5 rmind }
241 1.10 yamt
242 1.21 ad lwp_lock(t);
243 1.18 elad lparams.sched_priority = t->l_priority;
244 1.18 elad lpolicy = t->l_class;
245 1.5 rmind
246 1.18 elad switch (lpolicy) {
247 1.5 rmind case SCHED_OTHER:
248 1.18 elad lparams.sched_priority -= PRI_USER;
249 1.5 rmind break;
250 1.5 rmind case SCHED_RR:
251 1.5 rmind case SCHED_FIFO:
252 1.18 elad lparams.sched_priority -= PRI_USER_RT;
253 1.5 rmind break;
254 1.5 rmind }
255 1.18 elad
256 1.18 elad if (policy != NULL)
257 1.18 elad *policy = lpolicy;
258 1.18 elad
259 1.18 elad if (params != NULL)
260 1.18 elad *params = lparams;
261 1.18 elad
262 1.21 ad lwp_unlock(t);
263 1.21 ad mutex_exit(t->l_proc->p_lock);
264 1.18 elad return error;
265 1.18 elad }
266 1.18 elad
267 1.18 elad /*
268 1.18 elad * Get scheduling parameters.
269 1.18 elad */
270 1.18 elad int
271 1.18 elad sys__sched_getparam(struct lwp *l, const struct sys__sched_getparam_args *uap,
272 1.18 elad register_t *retval)
273 1.18 elad {
274 1.18 elad /* {
275 1.18 elad syscallarg(pid_t) pid;
276 1.18 elad syscallarg(lwpid_t) lid;
277 1.18 elad syscallarg(int *) policy;
278 1.18 elad syscallarg(struct sched_param *) params;
279 1.18 elad } */
280 1.18 elad struct sched_param params;
281 1.18 elad int error, policy;
282 1.18 elad
283 1.18 elad error = do_sched_getparam(SCARG(uap, pid), SCARG(uap, lid), &policy,
284 1.18 elad ¶ms);
285 1.18 elad if (error)
286 1.18 elad goto out;
287 1.18 elad
288 1.18 elad error = copyout(¶ms, SCARG(uap, params), sizeof(params));
289 1.10 yamt if (error == 0 && SCARG(uap, policy) != NULL)
290 1.10 yamt error = copyout(&policy, SCARG(uap, policy), sizeof(int));
291 1.31 rmind out:
292 1.31 rmind return error;
293 1.5 rmind }
294 1.5 rmind
295 1.31 rmind /*
296 1.31 rmind * Allocate the CPU set, and get it from userspace.
297 1.31 rmind */
298 1.23 christos static int
299 1.26 christos genkcpuset(kcpuset_t **dset, const cpuset_t *sset, size_t size)
300 1.23 christos {
301 1.23 christos int error;
302 1.23 christos
303 1.26 christos *dset = kcpuset_create();
304 1.26 christos error = kcpuset_copyin(sset, *dset, size);
305 1.26 christos if (error != 0)
306 1.26 christos kcpuset_unuse(*dset, NULL);
307 1.23 christos return error;
308 1.23 christos }
309 1.23 christos
310 1.5 rmind /*
311 1.5 rmind * Set affinity.
312 1.5 rmind */
313 1.5 rmind int
314 1.5 rmind sys__sched_setaffinity(struct lwp *l,
315 1.5 rmind const struct sys__sched_setaffinity_args *uap, register_t *retval)
316 1.5 rmind {
317 1.5 rmind /* {
318 1.5 rmind syscallarg(pid_t) pid;
319 1.5 rmind syscallarg(lwpid_t) lid;
320 1.5 rmind syscallarg(size_t) size;
321 1.23 christos syscallarg(const cpuset_t *) cpuset;
322 1.5 rmind } */
323 1.26 christos kcpuset_t *cpuset, *cpulst = NULL;
324 1.32 rmind struct cpu_info *ici, *ci;
325 1.5 rmind struct proc *p;
326 1.5 rmind struct lwp *t;
327 1.5 rmind CPU_INFO_ITERATOR cii;
328 1.32 rmind bool alloff;
329 1.5 rmind lwpid_t lid;
330 1.5 rmind u_int lcnt;
331 1.5 rmind int error;
332 1.5 rmind
333 1.31 rmind error = genkcpuset(&cpuset, SCARG(uap, cpuset), SCARG(uap, size));
334 1.31 rmind if (error)
335 1.23 christos return error;
336 1.5 rmind
337 1.31 rmind /*
338 1.32 rmind * Traverse _each_ CPU to:
339 1.32 rmind * - Check that CPUs in the mask have no assigned processor set.
340 1.32 rmind * - Check that at least one CPU from the mask is online.
341 1.32 rmind * - Find the first target CPU to migrate.
342 1.31 rmind *
343 1.32 rmind * To avoid the race with CPU online/offline calls and processor sets,
344 1.32 rmind * cpu_lock will be locked for the entire operation.
345 1.31 rmind */
346 1.32 rmind ci = NULL;
347 1.32 rmind alloff = false;
348 1.31 rmind mutex_enter(&cpu_lock);
349 1.32 rmind for (CPU_INFO_FOREACH(cii, ici)) {
350 1.32 rmind struct schedstate_percpu *ispc;
351 1.31 rmind
352 1.32 rmind if (kcpuset_isset(cpu_index(ici), cpuset) == 0)
353 1.31 rmind continue;
354 1.32 rmind
355 1.32 rmind ispc = &ici->ci_schedstate;
356 1.32 rmind /* Check that CPU is not in the processor-set */
357 1.32 rmind if (ispc->spc_psid != PS_NONE) {
358 1.32 rmind error = EPERM;
359 1.32 rmind goto out;
360 1.32 rmind }
361 1.32 rmind /* Skip offline CPUs */
362 1.32 rmind if (ispc->spc_flags & SPCF_OFFLINE) {
363 1.32 rmind alloff = true;
364 1.31 rmind continue;
365 1.24 rmind }
366 1.32 rmind /* Target CPU to migrate */
367 1.32 rmind if (ci == NULL) {
368 1.32 rmind ci = ici;
369 1.32 rmind }
370 1.23 christos }
371 1.5 rmind if (ci == NULL) {
372 1.32 rmind if (alloff) {
373 1.31 rmind /* All CPUs in the set are offline */
374 1.31 rmind error = EPERM;
375 1.31 rmind goto out;
376 1.31 rmind }
377 1.5 rmind /* Empty set */
378 1.33 rmind kcpuset_unuse(cpuset, &cpulst);
379 1.5 rmind cpuset = NULL;
380 1.5 rmind }
381 1.5 rmind
382 1.7 rmind if (SCARG(uap, pid) != 0) {
383 1.7 rmind /* Find the process */
384 1.20 ad mutex_enter(proc_lock);
385 1.20 ad p = p_find(SCARG(uap, pid), PFIND_LOCKED);
386 1.7 rmind if (p == NULL) {
387 1.20 ad mutex_exit(proc_lock);
388 1.7 rmind error = ESRCH;
389 1.23 christos goto out;
390 1.7 rmind }
391 1.21 ad mutex_enter(p->p_lock);
392 1.20 ad mutex_exit(proc_lock);
393 1.17 ad /* Disallow modification of system processes. */
394 1.17 ad if ((p->p_flag & PK_SYSTEM) != 0) {
395 1.21 ad mutex_exit(p->p_lock);
396 1.17 ad error = EPERM;
397 1.23 christos goto out;
398 1.17 ad }
399 1.7 rmind } else {
400 1.7 rmind /* Use the calling process */
401 1.7 rmind p = l->l_proc;
402 1.21 ad mutex_enter(p->p_lock);
403 1.5 rmind }
404 1.5 rmind
405 1.10 yamt /*
406 1.10 yamt * Check the permission.
407 1.10 yamt */
408 1.11 elad error = kauth_authorize_process(l->l_cred,
409 1.11 elad KAUTH_PROCESS_SCHEDULER_SETAFFINITY, p, NULL, NULL, NULL);
410 1.10 yamt if (error != 0) {
411 1.21 ad mutex_exit(p->p_lock);
412 1.23 christos goto out;
413 1.10 yamt }
414 1.5 rmind
415 1.28 wrstuden #ifdef KERN_SA
416 1.31 rmind /* Changing the affinity of a SA process is not supported */
417 1.28 wrstuden if ((p->p_sflag & (PS_SA | PS_WEXIT)) != 0 || p->p_sa != NULL) {
418 1.28 wrstuden mutex_exit(p->p_lock);
419 1.28 wrstuden error = EINVAL;
420 1.28 wrstuden goto out;
421 1.28 wrstuden }
422 1.28 wrstuden #endif
423 1.28 wrstuden
424 1.5 rmind /* Find the LWP(s) */
425 1.5 rmind lcnt = 0;
426 1.5 rmind lid = SCARG(uap, lid);
427 1.5 rmind LIST_FOREACH(t, &p->p_lwps, l_sibling) {
428 1.5 rmind if (lid && lid != t->l_lid)
429 1.5 rmind continue;
430 1.5 rmind lwp_lock(t);
431 1.27 rmind /* It is not allowed to set the affinity for zombie LWPs */
432 1.27 rmind if (t->l_stat == LSZOMB) {
433 1.27 rmind lwp_unlock(t);
434 1.27 rmind continue;
435 1.27 rmind }
436 1.5 rmind if (cpuset) {
437 1.5 rmind /* Set the affinity flag and new CPU set */
438 1.5 rmind t->l_flag |= LW_AFFINITY;
439 1.25 rmind kcpuset_use(cpuset);
440 1.23 christos if (t->l_affinity != NULL)
441 1.25 rmind kcpuset_unuse(t->l_affinity, &cpulst);
442 1.23 christos t->l_affinity = cpuset;
443 1.5 rmind /* Migrate to another CPU, unlocks LWP */
444 1.5 rmind lwp_migrate(t, ci);
445 1.5 rmind } else {
446 1.5 rmind /* Unset the affinity flag */
447 1.5 rmind t->l_flag &= ~LW_AFFINITY;
448 1.23 christos if (t->l_affinity != NULL)
449 1.25 rmind kcpuset_unuse(t->l_affinity, &cpulst);
450 1.23 christos t->l_affinity = NULL;
451 1.5 rmind lwp_unlock(t);
452 1.5 rmind }
453 1.5 rmind lcnt++;
454 1.5 rmind }
455 1.21 ad mutex_exit(p->p_lock);
456 1.5 rmind if (lcnt == 0)
457 1.5 rmind error = ESRCH;
458 1.23 christos out:
459 1.31 rmind mutex_exit(&cpu_lock);
460 1.5 rmind if (cpuset != NULL)
461 1.25 rmind kcpuset_unuse(cpuset, &cpulst);
462 1.26 christos kcpuset_destroy(cpulst);
463 1.5 rmind return error;
464 1.5 rmind }
465 1.5 rmind
466 1.5 rmind /*
467 1.5 rmind * Get affinity.
468 1.5 rmind */
469 1.5 rmind int
470 1.5 rmind sys__sched_getaffinity(struct lwp *l,
471 1.5 rmind const struct sys__sched_getaffinity_args *uap, register_t *retval)
472 1.5 rmind {
473 1.5 rmind /* {
474 1.5 rmind syscallarg(pid_t) pid;
475 1.5 rmind syscallarg(lwpid_t) lid;
476 1.5 rmind syscallarg(size_t) size;
477 1.23 christos syscallarg(cpuset_t *) cpuset;
478 1.5 rmind } */
479 1.5 rmind struct lwp *t;
480 1.26 christos kcpuset_t *cpuset;
481 1.5 rmind int error;
482 1.5 rmind
483 1.31 rmind error = genkcpuset(&cpuset, SCARG(uap, cpuset), SCARG(uap, size));
484 1.31 rmind if (error)
485 1.23 christos return error;
486 1.5 rmind
487 1.16 rmind /* Locks the LWP */
488 1.16 rmind t = lwp_find2(SCARG(uap, pid), SCARG(uap, lid));
489 1.5 rmind if (t == NULL) {
490 1.23 christos error = ESRCH;
491 1.23 christos goto out;
492 1.5 rmind }
493 1.10 yamt /* Check the permission */
494 1.11 elad if (kauth_authorize_process(l->l_cred,
495 1.11 elad KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
496 1.21 ad mutex_exit(t->l_proc->p_lock);
497 1.23 christos error = EPERM;
498 1.23 christos goto out;
499 1.10 yamt }
500 1.21 ad lwp_lock(t);
501 1.23 christos if (t->l_flag & LW_AFFINITY) {
502 1.23 christos KASSERT(t->l_affinity != NULL);
503 1.25 rmind kcpuset_copy(cpuset, t->l_affinity);
504 1.23 christos } else
505 1.26 christos kcpuset_zero(cpuset);
506 1.5 rmind lwp_unlock(t);
507 1.21 ad mutex_exit(t->l_proc->p_lock);
508 1.5 rmind
509 1.26 christos error = kcpuset_copyout(cpuset, SCARG(uap, cpuset), SCARG(uap, size));
510 1.23 christos out:
511 1.25 rmind kcpuset_unuse(cpuset, NULL);
512 1.5 rmind return error;
513 1.5 rmind }
514 1.5 rmind
515 1.5 rmind /*
516 1.5 rmind * Yield.
517 1.5 rmind */
518 1.1 ad int
519 1.4 dsl sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
520 1.1 ad {
521 1.1 ad
522 1.1 ad yield();
523 1.28 wrstuden #ifdef KERN_SA
524 1.28 wrstuden if (l->l_flag & LW_SA) {
525 1.28 wrstuden sa_preempt(l);
526 1.28 wrstuden }
527 1.28 wrstuden #endif
528 1.1 ad return 0;
529 1.1 ad }
530 1.5 rmind
531 1.5 rmind /*
532 1.5 rmind * Sysctl nodes and initialization.
533 1.5 rmind */
534 1.34 elad static void
535 1.34 elad sysctl_sched_setup(struct sysctllog **clog)
536 1.5 rmind {
537 1.5 rmind const struct sysctlnode *node = NULL;
538 1.5 rmind
539 1.5 rmind sysctl_createv(clog, 0, NULL, NULL,
540 1.5 rmind CTLFLAG_PERMANENT,
541 1.5 rmind CTLTYPE_NODE, "kern", NULL,
542 1.5 rmind NULL, 0, NULL, 0,
543 1.5 rmind CTL_KERN, CTL_EOL);
544 1.5 rmind sysctl_createv(clog, 0, NULL, NULL,
545 1.5 rmind CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
546 1.5 rmind CTLTYPE_INT, "posix_sched",
547 1.5 rmind SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
548 1.5 rmind "Process Scheduling option to which the "
549 1.5 rmind "system attempts to conform"),
550 1.5 rmind NULL, _POSIX_PRIORITY_SCHEDULING, NULL, 0,
551 1.5 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
552 1.5 rmind sysctl_createv(clog, 0, NULL, &node,
553 1.5 rmind CTLFLAG_PERMANENT,
554 1.5 rmind CTLTYPE_NODE, "sched",
555 1.5 rmind SYSCTL_DESCR("Scheduler options"),
556 1.5 rmind NULL, 0, NULL, 0,
557 1.5 rmind CTL_KERN, CTL_CREATE, CTL_EOL);
558 1.5 rmind
559 1.5 rmind if (node == NULL)
560 1.5 rmind return;
561 1.5 rmind
562 1.5 rmind sysctl_createv(clog, 0, &node, NULL,
563 1.5 rmind CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
564 1.5 rmind CTLTYPE_INT, "pri_min",
565 1.5 rmind SYSCTL_DESCR("Minimal POSIX real-time priority"),
566 1.5 rmind NULL, SCHED_PRI_MIN, NULL, 0,
567 1.5 rmind CTL_CREATE, CTL_EOL);
568 1.5 rmind sysctl_createv(clog, 0, &node, NULL,
569 1.5 rmind CTLFLAG_PERMANENT | CTLFLAG_IMMEDIATE,
570 1.5 rmind CTLTYPE_INT, "pri_max",
571 1.19 njoly SYSCTL_DESCR("Maximal POSIX real-time priority"),
572 1.5 rmind NULL, SCHED_PRI_MAX, NULL, 0,
573 1.5 rmind CTL_CREATE, CTL_EOL);
574 1.5 rmind }
575 1.34 elad
576 1.34 elad static int
577 1.34 elad sched_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
578 1.34 elad void *arg0, void *arg1, void *arg2, void *arg3)
579 1.34 elad {
580 1.34 elad struct proc *p;
581 1.34 elad int result;
582 1.34 elad
583 1.34 elad result = KAUTH_RESULT_DEFER;
584 1.34 elad p = arg0;
585 1.34 elad
586 1.34 elad switch (action) {
587 1.34 elad case KAUTH_PROCESS_SCHEDULER_GETPARAM:
588 1.34 elad if (kauth_cred_uidmatch(cred, p->p_cred))
589 1.34 elad result = KAUTH_RESULT_ALLOW;
590 1.34 elad break;
591 1.34 elad
592 1.34 elad case KAUTH_PROCESS_SCHEDULER_SETPARAM:
593 1.34 elad if (kauth_cred_uidmatch(cred, p->p_cred)) {
594 1.34 elad struct lwp *l;
595 1.34 elad int policy;
596 1.34 elad pri_t priority;
597 1.34 elad
598 1.34 elad l = arg1;
599 1.34 elad policy = (int)(unsigned long)arg2;
600 1.34 elad priority = (pri_t)(unsigned long)arg3;
601 1.34 elad
602 1.34 elad if ((policy == l->l_class ||
603 1.34 elad (policy != SCHED_FIFO && policy != SCHED_RR)) &&
604 1.34 elad priority <= l->l_priority)
605 1.34 elad result = KAUTH_RESULT_ALLOW;
606 1.34 elad }
607 1.34 elad
608 1.34 elad break;
609 1.34 elad
610 1.34 elad case KAUTH_PROCESS_SCHEDULER_GETAFFINITY:
611 1.34 elad result = KAUTH_RESULT_ALLOW;
612 1.34 elad break;
613 1.34 elad
614 1.34 elad case KAUTH_PROCESS_SCHEDULER_SETAFFINITY:
615 1.34 elad /* Privileged; we let the secmodel handle this. */
616 1.34 elad break;
617 1.34 elad
618 1.34 elad default:
619 1.34 elad break;
620 1.34 elad }
621 1.34 elad
622 1.34 elad return result;
623 1.34 elad }
624 1.34 elad
625 1.34 elad void
626 1.34 elad sched_init(void)
627 1.34 elad {
628 1.34 elad
629 1.34 elad sysctl_sched_setup(&sched_sysctl_log);
630 1.34 elad
631 1.34 elad sched_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
632 1.34 elad sched_listener_cb, NULL);
633 1.34 elad }
634