No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / sys_pset.c
bloba4a8a8171c8496c47d63cb0938dd1e480858d48e
1 /* $NetBSD: sys_pset.c,v 1.12 2009/03/03 21:55:06 rmind Exp $ */
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.
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.
30 * Implementation of the Processor Sets.
32 * Locking
33 * The array of the processor-set structures and its members are protected
34 * by the global cpu_lock. Note that in scheduler, the very l_psid value
35 * might be used without lock held.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sys_pset.c,v 1.12 2009/03/03 21:55:06 rmind Exp $");
41 #include <sys/param.h>
43 #include <sys/cpu.h>
44 #include <sys/kauth.h>
45 #include <sys/kmem.h>
46 #include <sys/lwp.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/pset.h>
50 #include <sys/sched.h>
51 #include <sys/syscallargs.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/types.h>
56 static pset_info_t ** psets;
57 static u_int psets_max;
58 static u_int psets_count;
59 static kauth_listener_t psets_listener;
61 static int psets_realloc(int);
62 static int psid_validate(psetid_t, bool);
63 static int kern_pset_create(psetid_t *);
64 static int kern_pset_destroy(psetid_t);
66 static int
67 psets_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
68 void *arg0, void *arg1, void *arg2, void *arg3)
70 psetid_t id;
71 enum kauth_system_req req;
72 int result;
74 result = KAUTH_RESULT_DEFER;
75 req = (enum kauth_system_req)arg0;
76 id = (psetid_t)(unsigned long)arg1;
78 if (action != KAUTH_SYSTEM_PSET)
79 return result;
81 if ((req == KAUTH_REQ_SYSTEM_PSET_ASSIGN) ||
82 (req == KAUTH_REQ_SYSTEM_PSET_BIND)) {
83 if (id == PS_QUERY)
84 result = KAUTH_RESULT_ALLOW;
87 return result;
91 * Initialization of the processor-sets.
93 void
94 psets_init(void)
97 psets_max = max(MAXCPUS, 32);
98 psets = kmem_zalloc(psets_max * sizeof(void *), KM_SLEEP);
99 psets_count = 0;
101 psets_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
102 psets_listener_cb, NULL);
106 * Reallocate the array of the processor-set structures.
108 static int
109 psets_realloc(int new_psets_max)
111 pset_info_t **new_psets, **old_psets;
112 const u_int newsize = new_psets_max * sizeof(void *);
113 u_int i, oldsize;
115 if (new_psets_max < 1)
116 return EINVAL;
118 new_psets = kmem_zalloc(newsize, KM_SLEEP);
119 mutex_enter(&cpu_lock);
120 old_psets = psets;
121 oldsize = psets_max * sizeof(void *);
123 /* Check if we can lower the size of the array */
124 if (new_psets_max < psets_max) {
125 for (i = new_psets_max; i < psets_max; i++) {
126 if (psets[i] == NULL)
127 continue;
128 mutex_exit(&cpu_lock);
129 kmem_free(new_psets, newsize);
130 return EBUSY;
134 /* Copy all pointers to the new array */
135 memcpy(new_psets, psets, newsize);
136 psets_max = new_psets_max;
137 psets = new_psets;
138 mutex_exit(&cpu_lock);
140 kmem_free(old_psets, oldsize);
141 return 0;
145 * Validate processor-set ID.
147 static int
148 psid_validate(psetid_t psid, bool chkps)
151 KASSERT(mutex_owned(&cpu_lock));
153 if (chkps && (psid == PS_NONE || psid == PS_QUERY || psid == PS_MYID))
154 return 0;
155 if (psid <= 0 || psid > psets_max)
156 return EINVAL;
157 if (psets[psid - 1] == NULL)
158 return EINVAL;
159 if (psets[psid - 1]->ps_flags & PSET_BUSY)
160 return EBUSY;
162 return 0;
166 * Create a processor-set.
168 static int
169 kern_pset_create(psetid_t *psid)
171 pset_info_t *pi;
172 u_int i;
174 if (psets_count == psets_max)
175 return ENOMEM;
177 pi = kmem_zalloc(sizeof(pset_info_t), KM_SLEEP);
179 mutex_enter(&cpu_lock);
180 if (psets_count == psets_max) {
181 mutex_exit(&cpu_lock);
182 kmem_free(pi, sizeof(pset_info_t));
183 return ENOMEM;
186 /* Find a free entry in the array */
187 for (i = 0; i < psets_max; i++)
188 if (psets[i] == NULL)
189 break;
190 KASSERT(i != psets_max);
192 psets[i] = pi;
193 psets_count++;
194 mutex_exit(&cpu_lock);
196 *psid = i + 1;
197 return 0;
201 * Destroy a processor-set.
203 static int
204 kern_pset_destroy(psetid_t psid)
206 struct cpu_info *ci;
207 pset_info_t *pi;
208 struct lwp *l;
209 CPU_INFO_ITERATOR cii;
210 int error;
212 mutex_enter(&cpu_lock);
213 if (psid == PS_MYID) {
214 /* Use caller's processor-set ID */
215 psid = curlwp->l_psid;
217 error = psid_validate(psid, false);
218 if (error) {
219 mutex_exit(&cpu_lock);
220 return error;
223 /* Release the processor-set from all CPUs */
224 for (CPU_INFO_FOREACH(cii, ci)) {
225 struct schedstate_percpu *spc;
227 spc = &ci->ci_schedstate;
228 if (spc->spc_psid != psid)
229 continue;
230 spc->spc_psid = PS_NONE;
232 /* Mark that processor-set is going to be destroyed */
233 pi = psets[psid - 1];
234 pi->ps_flags |= PSET_BUSY;
235 mutex_exit(&cpu_lock);
237 /* Unmark the processor-set ID from each thread */
238 mutex_enter(proc_lock);
239 LIST_FOREACH(l, &alllwp, l_list) {
240 /* Safe to check and set without lock held */
241 if (l->l_psid != psid)
242 continue;
243 l->l_psid = PS_NONE;
245 mutex_exit(proc_lock);
247 /* Destroy the processor-set */
248 mutex_enter(&cpu_lock);
249 psets[psid - 1] = NULL;
250 psets_count--;
251 mutex_exit(&cpu_lock);
253 kmem_free(pi, sizeof(pset_info_t));
254 return 0;
258 * General system calls for the processor-sets.
262 sys_pset_create(struct lwp *l, const struct sys_pset_create_args *uap,
263 register_t *retval)
265 /* {
266 syscallarg(psetid_t) *psid;
267 } */
268 psetid_t psid;
269 int error;
271 /* Available only for super-user */
272 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
273 KAUTH_REQ_SYSTEM_PSET_CREATE, NULL, NULL, NULL))
274 return EPERM;
276 error = kern_pset_create(&psid);
277 if (error)
278 return error;
280 error = copyout(&psid, SCARG(uap, psid), sizeof(psetid_t));
281 if (error)
282 (void)kern_pset_destroy(psid);
284 return error;
288 sys_pset_destroy(struct lwp *l, const struct sys_pset_destroy_args *uap,
289 register_t *retval)
291 /* {
292 syscallarg(psetid_t) psid;
293 } */
295 /* Available only for super-user */
296 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
297 KAUTH_REQ_SYSTEM_PSET_DESTROY,
298 KAUTH_ARG(SCARG(uap, psid)), NULL, NULL))
299 return EPERM;
301 return kern_pset_destroy(SCARG(uap, psid));
305 sys_pset_assign(struct lwp *l, const struct sys_pset_assign_args *uap,
306 register_t *retval)
308 /* {
309 syscallarg(psetid_t) psid;
310 syscallarg(cpuid_t) cpuid;
311 syscallarg(psetid_t) *opsid;
312 } */
313 struct cpu_info *ici, *ci = NULL;
314 struct schedstate_percpu *spc = NULL;
315 struct lwp *t;
316 psetid_t psid = SCARG(uap, psid), opsid = 0;
317 CPU_INFO_ITERATOR cii;
318 int error = 0, nnone = 0;
320 /* Available only for super-user, except the case of PS_QUERY */
321 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
322 KAUTH_REQ_SYSTEM_PSET_ASSIGN, KAUTH_ARG(SCARG(uap, psid)), NULL,
323 NULL))
324 return EPERM;
326 /* Find the target CPU */
327 mutex_enter(&cpu_lock);
328 for (CPU_INFO_FOREACH(cii, ici)) {
329 struct schedstate_percpu *ispc;
330 ispc = &ici->ci_schedstate;
331 if (cpu_index(ici) == SCARG(uap, cpuid)) {
332 ci = ici;
333 spc = ispc;
335 nnone += (ispc->spc_psid == PS_NONE);
337 if (ci == NULL) {
338 mutex_exit(&cpu_lock);
339 return EINVAL;
341 error = psid_validate(psid, true);
342 if (error) {
343 mutex_exit(&cpu_lock);
344 return error;
346 opsid = spc->spc_psid;
347 switch (psid) {
348 case PS_QUERY:
349 break;
350 case PS_MYID:
351 psid = curlwp->l_psid;
352 /* FALLTHROUGH */
353 default:
355 * Ensure at least one CPU stays in the default set,
356 * and that specified CPU is not offline.
358 if (psid != PS_NONE && ((spc->spc_flags & SPCF_OFFLINE) ||
359 (nnone == 1 && spc->spc_psid == PS_NONE))) {
360 mutex_exit(&cpu_lock);
361 return EBUSY;
363 mutex_enter(proc_lock);
365 * Ensure that none of the threads are using affinity mask
366 * with this target CPU in it.
368 LIST_FOREACH(t, &alllwp, l_list) {
369 if ((t->l_flag & LW_AFFINITY) == 0)
370 continue;
371 lwp_lock(t);
372 if ((t->l_flag & LW_AFFINITY) == 0) {
373 lwp_unlock(t);
374 continue;
376 if (kcpuset_isset(cpu_index(ci), t->l_affinity)) {
377 lwp_unlock(t);
378 mutex_exit(proc_lock);
379 mutex_exit(&cpu_lock);
380 return EPERM;
384 * Set the processor-set ID.
385 * Migrate out any threads running on this CPU.
387 spc->spc_psid = psid;
389 LIST_FOREACH(t, &alllwp, l_list) {
390 struct cpu_info *tci;
391 if (t->l_cpu != ci)
392 continue;
393 if (t->l_pflag & (LP_BOUND | LP_INTR))
394 continue;
395 lwp_lock(t);
396 tci = sched_takecpu(t);
397 KASSERT(tci != ci);
398 lwp_migrate(t, tci);
400 mutex_exit(proc_lock);
401 break;
403 mutex_exit(&cpu_lock);
405 if (SCARG(uap, opsid) != NULL)
406 error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t));
408 return error;
412 sys__pset_bind(struct lwp *l, const struct sys__pset_bind_args *uap,
413 register_t *retval)
415 /* {
416 syscallarg(idtype_t) idtype;
417 syscallarg(id_t) first_id;
418 syscallarg(id_t) second_id;
419 syscallarg(psetid_t) psid;
420 syscallarg(psetid_t) *opsid;
421 } */
422 struct cpu_info *ci;
423 struct proc *p;
424 struct lwp *t;
425 id_t id1, id2;
426 pid_t pid = 0;
427 lwpid_t lid = 0;
428 psetid_t psid, opsid;
429 int error = 0, lcnt;
431 psid = SCARG(uap, psid);
433 /* Available only for super-user, except the case of PS_QUERY */
434 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET,
435 KAUTH_REQ_SYSTEM_PSET_BIND, KAUTH_ARG(SCARG(uap, psid)), NULL,
436 NULL))
437 return EPERM;
439 mutex_enter(&cpu_lock);
440 error = psid_validate(psid, true);
441 if (error) {
442 mutex_exit(&cpu_lock);
443 return error;
445 if (psid == PS_MYID)
446 psid = curlwp->l_psid;
447 if (psid != PS_QUERY && psid != PS_NONE)
448 psets[psid - 1]->ps_flags |= PSET_BUSY;
449 mutex_exit(&cpu_lock);
452 * Get PID and LID from the ID.
454 p = l->l_proc;
455 id1 = SCARG(uap, first_id);
456 id2 = SCARG(uap, second_id);
458 switch (SCARG(uap, idtype)) {
459 case P_PID:
461 * Process:
462 * First ID - PID;
463 * Second ID - ignored;
465 pid = (id1 == P_MYID) ? p->p_pid : id1;
466 lid = 0;
467 break;
468 case P_LWPID:
470 * Thread (LWP):
471 * First ID - LID;
472 * Second ID - PID;
474 if (id1 == P_MYID) {
475 pid = p->p_pid;
476 lid = l->l_lid;
477 break;
479 lid = id1;
480 pid = (id2 == P_MYID) ? p->p_pid : id2;
481 break;
482 default:
483 error = EINVAL;
484 goto error;
487 /* Find the process */
488 mutex_enter(proc_lock);
489 p = p_find(pid, PFIND_LOCKED);
490 if (p == NULL) {
491 mutex_exit(proc_lock);
492 error = ESRCH;
493 goto error;
495 mutex_enter(p->p_lock);
496 mutex_exit(proc_lock);
498 /* Disallow modification of the system processes */
499 if (p->p_flag & PK_SYSTEM) {
500 mutex_exit(p->p_lock);
501 error = EPERM;
502 goto error;
505 /* Find the LWP(s) */
506 lcnt = 0;
507 ci = NULL;
508 LIST_FOREACH(t, &p->p_lwps, l_sibling) {
509 if (lid && lid != t->l_lid)
510 continue;
512 * Bind the thread to the processor-set,
513 * take some CPU and migrate.
515 lwp_lock(t);
516 opsid = t->l_psid;
517 t->l_psid = psid;
518 ci = sched_takecpu(t);
519 /* Unlocks LWP */
520 lwp_migrate(t, ci);
521 lcnt++;
523 mutex_exit(p->p_lock);
524 if (lcnt == 0) {
525 error = ESRCH;
526 goto error;
528 if (SCARG(uap, opsid))
529 error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t));
530 error:
531 if (psid != PS_QUERY && psid != PS_NONE) {
532 mutex_enter(&cpu_lock);
533 psets[psid - 1]->ps_flags &= ~PSET_BUSY;
534 mutex_exit(&cpu_lock);
536 return error;
540 * Sysctl nodes and initialization.
543 static int
544 sysctl_psets_max(SYSCTLFN_ARGS)
546 struct sysctlnode node;
547 int error, newsize;
549 node = *rnode;
550 node.sysctl_data = &newsize;
552 newsize = psets_max;
553 error = sysctl_lookup(SYSCTLFN_CALL(&node));
554 if (error || newp == NULL)
555 return error;
557 if (newsize <= 0)
558 return EINVAL;
560 sysctl_unlock();
561 error = psets_realloc(newsize);
562 sysctl_relock();
563 return error;
566 static int
567 sysctl_psets_list(SYSCTLFN_ARGS)
569 const size_t bufsz = 1024;
570 char *buf, tbuf[16];
571 int i, error;
572 size_t len;
574 sysctl_unlock();
575 buf = kmem_alloc(bufsz, KM_SLEEP);
576 snprintf(buf, bufsz, "%d:1", PS_NONE); /* XXX */
578 mutex_enter(&cpu_lock);
579 for (i = 0; i < psets_max; i++) {
580 if (psets[i] == NULL)
581 continue;
582 snprintf(tbuf, sizeof(tbuf), ",%d:2", i + 1); /* XXX */
583 strlcat(buf, tbuf, bufsz);
585 mutex_exit(&cpu_lock);
586 len = strlen(buf) + 1;
587 error = 0;
588 if (oldp != NULL)
589 error = copyout(buf, oldp, min(len, *oldlenp));
590 *oldlenp = len;
591 kmem_free(buf, bufsz);
592 sysctl_relock();
593 return error;
596 SYSCTL_SETUP(sysctl_pset_setup, "sysctl kern.pset subtree setup")
598 const struct sysctlnode *node = NULL;
600 sysctl_createv(clog, 0, NULL, NULL,
601 CTLFLAG_PERMANENT,
602 CTLTYPE_NODE, "kern", NULL,
603 NULL, 0, NULL, 0,
604 CTL_KERN, CTL_EOL);
605 sysctl_createv(clog, 0, NULL, &node,
606 CTLFLAG_PERMANENT,
607 CTLTYPE_NODE, "pset",
608 SYSCTL_DESCR("Processor-set options"),
609 NULL, 0, NULL, 0,
610 CTL_KERN, CTL_CREATE, CTL_EOL);
612 if (node == NULL)
613 return;
615 sysctl_createv(clog, 0, &node, NULL,
616 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
617 CTLTYPE_INT, "psets_max",
618 SYSCTL_DESCR("Maximal count of the processor-sets"),
619 sysctl_psets_max, 0, &psets_max, 0,
620 CTL_CREATE, CTL_EOL);
621 sysctl_createv(clog, 0, &node, NULL,
622 CTLFLAG_PERMANENT,
623 CTLTYPE_STRING, "list",
624 SYSCTL_DESCR("List of active sets"),
625 sysctl_psets_list, 0, NULL, 0,
626 CTL_CREATE, CTL_EOL);