2 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
5 * Copyright (c) 2008 Nokia Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
46 #include <sys/refcount.h>
47 #include <sys/sched.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/cpuset.h>
52 #include <sys/queue.h>
53 #include <sys/libkern.h>
54 #include <sys/limits.h>
56 #include <sys/interrupt.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_phys.h>
69 * cpusets provide a mechanism for creating and manipulating sets of
70 * processors for the purpose of constraining the scheduling of threads to
71 * specific processors.
73 * Each process belongs to an identified set, by default this is set 1. Each
74 * thread may further restrict the cpus it may run on to a subset of this
75 * named set. This creates an anonymous set which other threads and processes
76 * may not join by number.
78 * The named set is referred to herein as the 'base' set to avoid ambiguity.
79 * This set is usually a child of a 'root' set while the anonymous set may
80 * simply be referred to as a mask. In the syscall api these are referred to
81 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
83 * Threads inherit their set from their creator whether it be anonymous or
84 * not. This means that anonymous sets are immutable because they may be
85 * shared. To modify an anonymous set a new set is created with the desired
86 * mask and the same parent as the existing anonymous set. This gives the
87 * illusion of each thread having a private mask.
89 * Via the syscall apis a user may ask to retrieve or modify the root, base,
90 * or mask that is discovered via a pid, tid, or setid. Modifying a set
91 * modifies all numbered and anonymous child sets to comply with the new mask.
92 * Modifying a pid or tid's mask applies only to that tid but must still
93 * exist within the assigned parent set.
95 * A thread may not be assigned to a group separate from other threads in
96 * the process. This is to remove ambiguity when the setid is queried with
97 * a pid argument. There is no other technical limitation.
99 * This somewhat complex arrangement is intended to make it easy for
100 * applications to query available processors and bind their threads to
101 * specific processors while also allowing administrators to dynamically
102 * reprovision by changing sets which apply to groups of processes.
104 * A simple application should not concern itself with sets at all and
105 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
106 * meaning 'curthread'. It may query available cpus for that tid with a
107 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
109 static uma_zone_t cpuset_zone;
110 static struct mtx cpuset_lock;
111 static struct setlist cpuset_ids;
112 static struct unrhdr *cpuset_unr;
113 static struct cpuset *cpuset_zero, *cpuset_default;
115 /* Return the size of cpuset_t at the kernel level */
116 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
117 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
119 cpuset_t *cpuset_root;
120 cpuset_t cpuset_domain[MAXMEMDOM];
123 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
126 cpuset_ref(struct cpuset *set)
129 refcount_acquire(&set->cs_ref);
134 * Walks up the tree from 'set' to find the root. Returns the root
137 static struct cpuset *
138 cpuset_refroot(struct cpuset *set)
141 for (; set->cs_parent != NULL; set = set->cs_parent)
142 if (set->cs_flags & CPU_SET_ROOT)
150 * Find the first non-anonymous set starting from 'set'. Returns this set
151 * referenced. May return the passed in set with an extra ref if it is
154 static struct cpuset *
155 cpuset_refbase(struct cpuset *set)
158 if (set->cs_id == CPUSET_INVALID)
159 set = set->cs_parent;
166 * Release a reference in a context where it is safe to allocate.
169 cpuset_rel(struct cpuset *set)
173 if (refcount_release(&set->cs_ref) == 0)
175 mtx_lock_spin(&cpuset_lock);
176 LIST_REMOVE(set, cs_siblings);
178 if (id != CPUSET_INVALID)
179 LIST_REMOVE(set, cs_link);
180 mtx_unlock_spin(&cpuset_lock);
181 cpuset_rel(set->cs_parent);
182 uma_zfree(cpuset_zone, set);
183 if (id != CPUSET_INVALID)
184 free_unr(cpuset_unr, id);
188 * Deferred release must be used when in a context that is not safe to
189 * allocate/free. This places any unreferenced sets on the list 'head'.
192 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
195 if (refcount_release(&set->cs_ref) == 0)
197 mtx_lock_spin(&cpuset_lock);
198 LIST_REMOVE(set, cs_siblings);
199 if (set->cs_id != CPUSET_INVALID)
200 LIST_REMOVE(set, cs_link);
201 LIST_INSERT_HEAD(head, set, cs_link);
202 mtx_unlock_spin(&cpuset_lock);
206 * Complete a deferred release. Removes the set from the list provided to
210 cpuset_rel_complete(struct cpuset *set)
212 LIST_REMOVE(set, cs_link);
213 cpuset_rel(set->cs_parent);
214 uma_zfree(cpuset_zone, set);
218 * Find a set based on an id. Returns it with a ref.
220 static struct cpuset *
221 cpuset_lookup(cpusetid_t setid, struct thread *td)
225 if (setid == CPUSET_INVALID)
227 mtx_lock_spin(&cpuset_lock);
228 LIST_FOREACH(set, &cpuset_ids, cs_link)
229 if (set->cs_id == setid)
233 mtx_unlock_spin(&cpuset_lock);
235 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
236 if (set != NULL && jailed(td->td_ucred)) {
237 struct cpuset *jset, *tset;
239 jset = td->td_ucred->cr_prison->pr_cpuset;
240 for (tset = set; tset != NULL; tset = tset->cs_parent)
253 * Create a set in the space provided in 'set' with the provided parameters.
254 * The set is returned with a single ref. May return EDEADLK if the set
255 * will have no valid cpu based on restrictions from the parent.
258 _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
262 if (!CPU_OVERLAP(&parent->cs_mask, mask))
264 CPU_COPY(mask, &set->cs_mask);
265 LIST_INIT(&set->cs_children);
266 refcount_init(&set->cs_ref, 1);
268 mtx_lock_spin(&cpuset_lock);
269 CPU_AND(&set->cs_mask, &parent->cs_mask);
271 set->cs_parent = cpuset_ref(parent);
272 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
273 if (set->cs_id != CPUSET_INVALID)
274 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
275 mtx_unlock_spin(&cpuset_lock);
281 * Create a new non-anonymous set with the requested parent and mask. May
282 * return failures if the mask is invalid or a new number can not be
286 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
292 id = alloc_unr(cpuset_unr);
295 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
296 error = _cpuset_create(set, parent, mask, id);
299 free_unr(cpuset_unr, id);
300 uma_zfree(cpuset_zone, set);
306 * Recursively check for errors that would occur from applying mask to
307 * the tree of sets starting at 'set'. Checks for sets that would become
308 * empty as well as RDONLY flags.
311 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
317 mtx_assert(&cpuset_lock, MA_OWNED);
318 if (set->cs_flags & CPU_SET_RDONLY)
321 if (!CPU_OVERLAP(&set->cs_mask, mask))
323 CPU_COPY(&set->cs_mask, &newmask);
324 CPU_AND(&newmask, mask);
326 CPU_COPY(mask, &newmask);
328 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
329 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
335 * Applies the mask 'mask' without checking for empty sets or permissions.
338 cpuset_update(struct cpuset *set, cpuset_t *mask)
342 mtx_assert(&cpuset_lock, MA_OWNED);
343 CPU_AND(&set->cs_mask, mask);
344 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
345 cpuset_update(nset, &set->cs_mask);
351 * Modify the set 'set' to use a copy of the mask provided. Apply this new
352 * mask to restrict all children in the tree. Checks for validity before
353 * applying the changes.
356 cpuset_modify(struct cpuset *set, cpuset_t *mask)
361 error = priv_check(curthread, PRIV_SCHED_CPUSET);
365 * In case we are called from within the jail
366 * we do not allow modifying the dedicated root
367 * cpuset of the jail but may still allow to
370 if (jailed(curthread->td_ucred) &&
371 set->cs_flags & CPU_SET_ROOT)
374 * Verify that we have access to this set of
377 root = set->cs_parent;
378 if (root && !CPU_SUBSET(&root->cs_mask, mask))
380 mtx_lock_spin(&cpuset_lock);
381 error = cpuset_testupdate(set, mask, 0);
384 CPU_COPY(mask, &set->cs_mask);
385 cpuset_update(set, mask);
387 mtx_unlock_spin(&cpuset_lock);
393 * Resolve the 'which' parameter of several cpuset apis.
395 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
396 * checks for permission via p_cansched().
398 * For WHICH_SET returns a valid set with a new reference.
400 * -1 may be supplied for any argument to mean the current proc/thread or
401 * the base set of the current thread. May fail with ESRCH/EPERM.
404 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
405 struct cpuset **setp)
422 if ((p = pfind(id)) == NULL)
437 case CPU_WHICH_CPUSET:
439 thread_lock(curthread);
440 set = cpuset_refbase(curthread->td_cpuset);
441 thread_unlock(curthread);
443 set = cpuset_lookup(id, curthread);
451 /* Find `set' for prison with given id. */
454 sx_slock(&allprison_lock);
455 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
456 sx_sunlock(&allprison_lock);
459 cpuset_ref(pr->pr_cpuset);
460 *setp = pr->pr_cpuset;
461 mtx_unlock(&pr->pr_mtx);
465 case CPU_WHICH_DOMAIN:
470 error = p_cansched(curthread, p);
476 td = FIRST_THREAD_IN_PROC(p);
483 * Create an anonymous set with the provided mask in the space provided by
484 * 'fset'. If the passed in set is anonymous we use its parent otherwise
485 * the new set is a child of 'set'.
488 cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
490 struct cpuset *parent;
492 if (set->cs_id == CPUSET_INVALID)
493 parent = set->cs_parent;
496 if (!CPU_SUBSET(&parent->cs_mask, mask))
498 return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
502 * Handle two cases for replacing the base set or mask of an entire process.
504 * 1) Set is non-null and mask is null. This reparents all anonymous sets
505 * to the provided set and replaces all non-anonymous td_cpusets with the
507 * 2) Mask is non-null and set is null. This replaces or creates anonymous
508 * sets for every thread with the existing base as a parent.
510 * This is overly complicated because we can't allocate while holding a
511 * spinlock and spinlocks must be held while changing and examining thread
515 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
517 struct setlist freelist;
518 struct setlist droplist;
519 struct cpuset *tdset;
527 * The algorithm requires two passes due to locking considerations.
529 * 1) Lookup the process and acquire the locks in the required order.
530 * 2) If enough cpusets have not been allocated release the locks and
531 * allocate them. Loop.
533 LIST_INIT(&freelist);
534 LIST_INIT(&droplist);
537 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
540 if (nfree >= p->p_numthreads)
542 threads = p->p_numthreads;
544 for (; nfree < threads; nfree++) {
545 nset = uma_zalloc(cpuset_zone, M_WAITOK);
546 LIST_INSERT_HEAD(&freelist, nset, cs_link);
549 PROC_LOCK_ASSERT(p, MA_OWNED);
551 * Now that the appropriate locks are held and we have enough cpusets,
552 * make sure the operation will succeed before applying changes. The
553 * proc lock prevents td_cpuset from changing between calls.
556 FOREACH_THREAD_IN_PROC(p, td) {
558 tdset = td->td_cpuset;
560 * Verify that a new mask doesn't specify cpus outside of
561 * the set the thread is a member of.
564 if (tdset->cs_id == CPUSET_INVALID)
565 tdset = tdset->cs_parent;
566 if (!CPU_SUBSET(&tdset->cs_mask, mask))
569 * Verify that a new set won't leave an existing thread
570 * mask without a cpu to run on. It can, however, restrict
573 } else if (tdset->cs_id == CPUSET_INVALID) {
574 if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
582 * Replace each thread's cpuset while using deferred release. We
583 * must do this because the thread lock must be held while operating
584 * on the thread and this limits the type of operations allowed.
586 FOREACH_THREAD_IN_PROC(p, td) {
589 * If we presently have an anonymous set or are applying a
590 * mask we must create an anonymous shadow set. That is
591 * either parented to our existing base or the supplied set.
593 * If we have a base set with no anonymous shadow we simply
594 * replace it outright.
596 tdset = td->td_cpuset;
597 if (tdset->cs_id == CPUSET_INVALID || mask) {
598 nset = LIST_FIRST(&freelist);
599 LIST_REMOVE(nset, cs_link);
601 error = cpuset_shadow(tdset, nset, mask);
603 error = _cpuset_create(nset, set,
604 &tdset->cs_mask, CPUSET_INVALID);
606 LIST_INSERT_HEAD(&freelist, nset, cs_link);
611 nset = cpuset_ref(set);
612 cpuset_rel_defer(&droplist, tdset);
613 td->td_cpuset = nset;
620 while ((nset = LIST_FIRST(&droplist)) != NULL)
621 cpuset_rel_complete(nset);
622 while ((nset = LIST_FIRST(&freelist)) != NULL) {
623 LIST_REMOVE(nset, cs_link);
624 uma_zfree(cpuset_zone, nset);
630 * Return a string representing a valid layout for a cpuset_t object.
631 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
634 cpusetobj_strprint(char *buf, const cpuset_t *set)
637 size_t i, bytesp, bufsiz;
641 bufsiz = CPUSETBUFSIZ;
643 for (i = 0; i < (_NCPUWORDS - 1); i++) {
644 bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
648 snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
653 * Build a valid cpuset_t object from a string representation.
654 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
657 cpusetobj_strscan(cpuset_t *set, const char *buf)
662 if (strlen(buf) > CPUSETBUFSIZ - 1)
665 /* Allow to pass a shorter version of the mask when necessary. */
667 for (i = 0; buf[i] != '\0'; i++)
670 if (nwords > _NCPUWORDS)
674 for (i = 0; i < (nwords - 1); i++) {
675 ret = sscanf(buf, "%lx,", &set->__bits[i]);
676 if (ret == 0 || ret == -1)
678 buf = strstr(buf, ",");
683 ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
684 if (ret == 0 || ret == -1)
690 * Apply an anonymous mask to a single thread.
693 cpuset_setthread(lwpid_t id, cpuset_t *mask)
701 nset = uma_zalloc(cpuset_zone, M_WAITOK);
702 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
707 error = cpuset_shadow(td->td_cpuset, nset, mask);
710 td->td_cpuset = nset;
720 uma_zfree(cpuset_zone, nset);
725 * Apply new cpumask to the ithread.
728 cpuset_setithread(lwpid_t id, int cpu)
730 struct cpuset *nset, *rset;
731 struct cpuset *parent, *old_set;
738 nset = uma_zalloc(cpuset_zone, M_WAITOK);
739 rset = uma_zalloc(cpuset_zone, M_WAITOK);
740 cs_id = CPUSET_INVALID;
744 CPU_COPY(cpuset_root, &mask);
748 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set);
749 if (error != 0 || ((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID))
752 /* cpuset_which() returns with PROC_LOCK held. */
753 old_set = td->td_cpuset;
758 * roll back to default set. We're not using cpuset_shadow()
759 * here because we can fail CPU_SUBSET() check. This can happen
760 * if default set does not contain all CPUs.
762 error = _cpuset_create(nset, cpuset_default, &mask,
768 if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID &&
769 old_set->cs_parent->cs_id == 1)) {
772 * Current set is either default (1) or
773 * shadowed version of default set.
775 * Allocate new root set to be able to shadow it
778 error = _cpuset_create(rset, cpuset_zero,
779 &cpuset_zero->cs_mask, cs_id);
784 rset->cs_flags |= CPU_SET_ROOT;
787 cs_id = CPUSET_INVALID;
789 /* Assume existing set was already allocated by previous call */
794 error = cpuset_shadow(parent, nset, &mask);
798 td->td_cpuset = nset;
809 uma_zfree(cpuset_zone, nset);
811 uma_zfree(cpuset_zone, rset);
812 if (cs_id != CPUSET_INVALID)
813 free_unr(cpuset_unr, cs_id);
819 * Creates system-wide cpusets and the cpuset for thread0 including two
822 * 0 - The root set which should represent all valid processors in the
823 * system. It is initially created with a mask of all processors
824 * because we don't know what processors are valid until cpuset_init()
825 * runs. This set is immutable.
826 * 1 - The default set which all processes are a member of until changed.
827 * This allows an administrator to move all threads off of given cpus to
828 * dedicate them to high priority tasks or save power etc.
836 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
837 NULL, NULL, UMA_ALIGN_PTR, 0);
838 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
841 * Create the root system set for the whole machine. Doesn't use
842 * cpuset_create() due to NULL parent.
844 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
845 CPU_FILL(&set->cs_mask);
846 LIST_INIT(&set->cs_children);
847 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
849 set->cs_flags = CPU_SET_ROOT;
851 cpuset_root = &set->cs_mask;
854 * Now derive a default, modifiable set from that to give out.
856 set = uma_zalloc(cpuset_zone, M_WAITOK);
857 error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
858 KASSERT(error == 0, ("Error creating default set: %d\n", error));
859 cpuset_default = set;
862 * Initialize the unit allocator. 0 and 1 are allocated above.
864 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
867 * If MD code has not initialized per-domain cpusets, place all
870 for (i = 0; i < MAXMEMDOM; i++)
871 if (!CPU_EMPTY(&cpuset_domain[i]))
873 CPU_COPY(&all_cpus, &cpuset_domain[0]);
880 * Create a cpuset, which would be cpuset_create() but
881 * mark the new 'set' as root.
883 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
886 * In case of no error, returns the set in *setp locked with a reference.
889 cpuset_create_root(struct prison *pr, struct cpuset **setp)
894 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
895 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
897 error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
901 KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
902 __func__, __LINE__));
904 /* Mark the set as root. */
906 set->cs_flags |= CPU_SET_ROOT;
912 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
916 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
917 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
920 error = cpuset_setproc(p->p_pid, set, NULL);
928 * This is called once the final set of system cpus is known. Modifies
929 * the root set and all children and mark the root read-only.
932 cpuset_init(void *arg)
937 if (cpuset_modify(cpuset_zero, &mask))
938 panic("Can't set initial cpuset mask.\n");
939 cpuset_zero->cs_flags |= CPU_SET_RDONLY;
941 SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
943 #ifndef _SYS_SYSPROTO_H_
949 sys_cpuset(struct thread *td, struct cpuset_args *uap)
956 root = cpuset_refroot(td->td_cpuset);
958 error = cpuset_create(&set, root, &root->cs_mask);
962 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
964 error = cpuset_setproc(-1, set, NULL);
969 #ifndef _SYS_SYSPROTO_H_
970 struct cpuset_setid_args {
977 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
983 * Presently we only support per-process sets.
985 if (uap->which != CPU_WHICH_PID)
987 set = cpuset_lookup(uap->setid, td);
990 error = cpuset_setproc(uap->id, set, NULL);
995 #ifndef _SYS_SYSPROTO_H_
996 struct cpuset_getid_args {
1004 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1006 struct cpuset *nset;
1013 if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET)
1015 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1018 switch (uap->which) {
1022 set = cpuset_refbase(ttd->td_cpuset);
1026 case CPU_WHICH_CPUSET:
1027 case CPU_WHICH_JAIL:
1030 case CPU_WHICH_DOMAIN:
1033 switch (uap->level) {
1034 case CPU_LEVEL_ROOT:
1035 nset = cpuset_refroot(set);
1039 case CPU_LEVEL_CPUSET:
1041 case CPU_LEVEL_WHICH:
1047 error = copyout(&id, uap->setid, sizeof(id));
1052 #ifndef _SYS_SYSPROTO_H_
1053 struct cpuset_getaffinity_args {
1062 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1065 struct cpuset *nset;
1072 if (uap->cpusetsize < sizeof(cpuset_t) ||
1073 uap->cpusetsize > CPU_MAXSIZE / NBBY)
1075 size = uap->cpusetsize;
1076 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1077 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1080 switch (uap->level) {
1081 case CPU_LEVEL_ROOT:
1082 case CPU_LEVEL_CPUSET:
1083 switch (uap->which) {
1087 set = cpuset_ref(ttd->td_cpuset);
1090 case CPU_WHICH_CPUSET:
1091 case CPU_WHICH_JAIL:
1094 case CPU_WHICH_DOMAIN:
1098 if (uap->level == CPU_LEVEL_ROOT)
1099 nset = cpuset_refroot(set);
1101 nset = cpuset_refbase(set);
1102 CPU_COPY(&nset->cs_mask, mask);
1105 case CPU_LEVEL_WHICH:
1106 switch (uap->which) {
1109 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1113 FOREACH_THREAD_IN_PROC(p, ttd) {
1115 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1119 case CPU_WHICH_CPUSET:
1120 case CPU_WHICH_JAIL:
1121 CPU_COPY(&set->cs_mask, mask);
1124 error = intr_getaffinity(uap->id, mask);
1126 case CPU_WHICH_DOMAIN:
1127 if (uap->id < 0 || uap->id >= MAXMEMDOM)
1130 CPU_COPY(&cpuset_domain[uap->id], mask);
1143 error = copyout(mask, uap->mask, size);
1149 #ifndef _SYS_SYSPROTO_H_
1150 struct cpuset_setaffinity_args {
1155 const cpuset_t *mask;
1159 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1161 struct cpuset *nset;
1168 if (uap->cpusetsize < sizeof(cpuset_t) ||
1169 uap->cpusetsize > CPU_MAXSIZE / NBBY)
1171 mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1172 error = copyin(uap->mask, mask, uap->cpusetsize);
1176 * Verify that no high bits are set.
1178 if (uap->cpusetsize > sizeof(cpuset_t)) {
1182 end = cp = (char *)&mask->__bits;
1183 end += uap->cpusetsize;
1184 cp += sizeof(cpuset_t);
1192 switch (uap->level) {
1193 case CPU_LEVEL_ROOT:
1194 case CPU_LEVEL_CPUSET:
1195 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1198 switch (uap->which) {
1202 set = cpuset_ref(ttd->td_cpuset);
1206 case CPU_WHICH_CPUSET:
1207 case CPU_WHICH_JAIL:
1210 case CPU_WHICH_DOMAIN:
1214 if (uap->level == CPU_LEVEL_ROOT)
1215 nset = cpuset_refroot(set);
1217 nset = cpuset_refbase(set);
1218 error = cpuset_modify(nset, mask);
1222 case CPU_LEVEL_WHICH:
1223 switch (uap->which) {
1225 error = cpuset_setthread(uap->id, mask);
1228 error = cpuset_setproc(uap->id, NULL, mask);
1230 case CPU_WHICH_CPUSET:
1231 case CPU_WHICH_JAIL:
1232 error = cpuset_which(uap->which, uap->id, &p,
1235 error = cpuset_modify(set, mask);
1240 error = intr_setaffinity(uap->id, mask);
1258 ddb_display_cpuset(const cpuset_t *set)
1262 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1263 if (CPU_ISSET(cpu, set)) {
1265 db_printf("%d", cpu);
1268 db_printf(",%d", cpu);
1272 db_printf("<none>");
1275 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1279 LIST_FOREACH(set, &cpuset_ids, cs_link) {
1280 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1281 set, set->cs_id, set->cs_ref, set->cs_flags,
1282 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1283 db_printf(" mask=");
1284 ddb_display_cpuset(&set->cs_mask);