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>
65 * cpusets provide a mechanism for creating and manipulating sets of
66 * processors for the purpose of constraining the scheduling of threads to
67 * specific processors.
69 * Each process belongs to an identified set, by default this is set 1. Each
70 * thread may further restrict the cpus it may run on to a subset of this
71 * named set. This creates an anonymous set which other threads and processes
72 * may not join by number.
74 * The named set is referred to herein as the 'base' set to avoid ambiguity.
75 * This set is usually a child of a 'root' set while the anonymous set may
76 * simply be referred to as a mask. In the syscall api these are referred to
77 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
79 * Threads inherit their set from their creator whether it be anonymous or
80 * not. This means that anonymous sets are immutable because they may be
81 * shared. To modify an anonymous set a new set is created with the desired
82 * mask and the same parent as the existing anonymous set. This gives the
83 * illusion of each thread having a private mask.
85 * Via the syscall apis a user may ask to retrieve or modify the root, base,
86 * or mask that is discovered via a pid, tid, or setid. Modifying a set
87 * modifies all numbered and anonymous child sets to comply with the new mask.
88 * Modifying a pid or tid's mask applies only to that tid but must still
89 * exist within the assigned parent set.
91 * A thread may not be assigned to a group separate from other threads in
92 * the process. This is to remove ambiguity when the setid is queried with
93 * a pid argument. There is no other technical limitation.
95 * This somewhat complex arrangement is intended to make it easy for
96 * applications to query available processors and bind their threads to
97 * specific processors while also allowing administrators to dynamically
98 * reprovision by changing sets which apply to groups of processes.
100 * A simple application should not concern itself with sets at all and
101 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
102 * meaning 'curthread'. It may query available cpus for that tid with a
103 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
105 static uma_zone_t cpuset_zone;
106 static struct mtx cpuset_lock;
107 static struct setlist cpuset_ids;
108 static struct unrhdr *cpuset_unr;
109 static struct cpuset *cpuset_zero;
111 /* Return the size of cpuset_t at the kernel level */
112 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD,
113 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
115 cpuset_t *cpuset_root;
118 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
121 cpuset_ref(struct cpuset *set)
124 refcount_acquire(&set->cs_ref);
129 * Walks up the tree from 'set' to find the root. Returns the root
132 static struct cpuset *
133 cpuset_refroot(struct cpuset *set)
136 for (; set->cs_parent != NULL; set = set->cs_parent)
137 if (set->cs_flags & CPU_SET_ROOT)
145 * Find the first non-anonymous set starting from 'set'. Returns this set
146 * referenced. May return the passed in set with an extra ref if it is
149 static struct cpuset *
150 cpuset_refbase(struct cpuset *set)
153 if (set->cs_id == CPUSET_INVALID)
154 set = set->cs_parent;
161 * Release a reference in a context where it is safe to allocate.
164 cpuset_rel(struct cpuset *set)
168 if (refcount_release(&set->cs_ref) == 0)
170 mtx_lock_spin(&cpuset_lock);
171 LIST_REMOVE(set, cs_siblings);
173 if (id != CPUSET_INVALID)
174 LIST_REMOVE(set, cs_link);
175 mtx_unlock_spin(&cpuset_lock);
176 cpuset_rel(set->cs_parent);
177 uma_zfree(cpuset_zone, set);
178 if (id != CPUSET_INVALID)
179 free_unr(cpuset_unr, id);
183 * Deferred release must be used when in a context that is not safe to
184 * allocate/free. This places any unreferenced sets on the list 'head'.
187 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
190 if (refcount_release(&set->cs_ref) == 0)
192 mtx_lock_spin(&cpuset_lock);
193 LIST_REMOVE(set, cs_siblings);
194 if (set->cs_id != CPUSET_INVALID)
195 LIST_REMOVE(set, cs_link);
196 LIST_INSERT_HEAD(head, set, cs_link);
197 mtx_unlock_spin(&cpuset_lock);
201 * Complete a deferred release. Removes the set from the list provided to
205 cpuset_rel_complete(struct cpuset *set)
207 LIST_REMOVE(set, cs_link);
208 cpuset_rel(set->cs_parent);
209 uma_zfree(cpuset_zone, set);
213 * Find a set based on an id. Returns it with a ref.
215 static struct cpuset *
216 cpuset_lookup(cpusetid_t setid, struct thread *td)
220 if (setid == CPUSET_INVALID)
222 mtx_lock_spin(&cpuset_lock);
223 LIST_FOREACH(set, &cpuset_ids, cs_link)
224 if (set->cs_id == setid)
228 mtx_unlock_spin(&cpuset_lock);
230 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
231 if (set != NULL && jailed(td->td_ucred)) {
232 struct cpuset *jset, *tset;
234 jset = td->td_ucred->cr_prison->pr_cpuset;
235 for (tset = set; tset != NULL; tset = tset->cs_parent)
248 * Create a set in the space provided in 'set' with the provided parameters.
249 * The set is returned with a single ref. May return EDEADLK if the set
250 * will have no valid cpu based on restrictions from the parent.
253 _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
257 if (!CPU_OVERLAP(&parent->cs_mask, mask))
259 CPU_COPY(mask, &set->cs_mask);
260 LIST_INIT(&set->cs_children);
261 refcount_init(&set->cs_ref, 1);
263 mtx_lock_spin(&cpuset_lock);
264 CPU_AND(&set->cs_mask, &parent->cs_mask);
266 set->cs_parent = cpuset_ref(parent);
267 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
268 if (set->cs_id != CPUSET_INVALID)
269 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
270 mtx_unlock_spin(&cpuset_lock);
276 * Create a new non-anonymous set with the requested parent and mask. May
277 * return failures if the mask is invalid or a new number can not be
281 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
287 id = alloc_unr(cpuset_unr);
290 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
291 error = _cpuset_create(set, parent, mask, id);
294 free_unr(cpuset_unr, id);
295 uma_zfree(cpuset_zone, set);
301 * Recursively check for errors that would occur from applying mask to
302 * the tree of sets starting at 'set'. Checks for sets that would become
303 * empty as well as RDONLY flags.
306 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
312 mtx_assert(&cpuset_lock, MA_OWNED);
313 if (set->cs_flags & CPU_SET_RDONLY)
316 if (!CPU_OVERLAP(&set->cs_mask, mask))
318 CPU_COPY(&set->cs_mask, &newmask);
319 CPU_AND(&newmask, mask);
321 CPU_COPY(mask, &newmask);
323 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
324 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
330 * Applies the mask 'mask' without checking for empty sets or permissions.
333 cpuset_update(struct cpuset *set, cpuset_t *mask)
337 mtx_assert(&cpuset_lock, MA_OWNED);
338 CPU_AND(&set->cs_mask, mask);
339 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
340 cpuset_update(nset, &set->cs_mask);
346 * Modify the set 'set' to use a copy of the mask provided. Apply this new
347 * mask to restrict all children in the tree. Checks for validity before
348 * applying the changes.
351 cpuset_modify(struct cpuset *set, cpuset_t *mask)
356 error = priv_check(curthread, PRIV_SCHED_CPUSET);
360 * In case we are called from within the jail
361 * we do not allow modifying the dedicated root
362 * cpuset of the jail but may still allow to
365 if (jailed(curthread->td_ucred) &&
366 set->cs_flags & CPU_SET_ROOT)
369 * Verify that we have access to this set of
372 root = set->cs_parent;
373 if (root && !CPU_SUBSET(&root->cs_mask, mask))
375 mtx_lock_spin(&cpuset_lock);
376 error = cpuset_testupdate(set, mask, 0);
379 CPU_COPY(mask, &set->cs_mask);
380 cpuset_update(set, mask);
382 mtx_unlock_spin(&cpuset_lock);
388 * Resolve the 'which' parameter of several cpuset apis.
390 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
391 * checks for permission via p_cansched().
393 * For WHICH_SET returns a valid set with a new reference.
395 * -1 may be supplied for any argument to mean the current proc/thread or
396 * the base set of the current thread. May fail with ESRCH/EPERM.
399 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
400 struct cpuset **setp)
417 if ((p = pfind(id)) == NULL)
432 case CPU_WHICH_CPUSET:
434 thread_lock(curthread);
435 set = cpuset_refbase(curthread->td_cpuset);
436 thread_unlock(curthread);
438 set = cpuset_lookup(id, curthread);
446 /* Find `set' for prison with given id. */
449 sx_slock(&allprison_lock);
450 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
451 sx_sunlock(&allprison_lock);
454 cpuset_ref(pr->pr_cpuset);
455 *setp = pr->pr_cpuset;
456 mtx_unlock(&pr->pr_mtx);
464 error = p_cansched(curthread, p);
470 td = FIRST_THREAD_IN_PROC(p);
477 * Create an anonymous set with the provided mask in the space provided by
478 * 'fset'. If the passed in set is anonymous we use its parent otherwise
479 * the new set is a child of 'set'.
482 cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
484 struct cpuset *parent;
486 if (set->cs_id == CPUSET_INVALID)
487 parent = set->cs_parent;
490 if (!CPU_SUBSET(&parent->cs_mask, mask))
492 return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
496 * Handle two cases for replacing the base set or mask of an entire process.
498 * 1) Set is non-null and mask is null. This reparents all anonymous sets
499 * to the provided set and replaces all non-anonymous td_cpusets with the
501 * 2) Mask is non-null and set is null. This replaces or creates anonymous
502 * sets for every thread with the existing base as a parent.
504 * This is overly complicated because we can't allocate while holding a
505 * spinlock and spinlocks must be held while changing and examining thread
509 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
511 struct setlist freelist;
512 struct setlist droplist;
513 struct cpuset *tdset;
521 * The algorithm requires two passes due to locking considerations.
523 * 1) Lookup the process and acquire the locks in the required order.
524 * 2) If enough cpusets have not been allocated release the locks and
525 * allocate them. Loop.
527 LIST_INIT(&freelist);
528 LIST_INIT(&droplist);
531 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
534 if (nfree >= p->p_numthreads)
536 threads = p->p_numthreads;
538 for (; nfree < threads; nfree++) {
539 nset = uma_zalloc(cpuset_zone, M_WAITOK);
540 LIST_INSERT_HEAD(&freelist, nset, cs_link);
543 PROC_LOCK_ASSERT(p, MA_OWNED);
545 * Now that the appropriate locks are held and we have enough cpusets,
546 * make sure the operation will succeed before applying changes. The
547 * proc lock prevents td_cpuset from changing between calls.
550 FOREACH_THREAD_IN_PROC(p, td) {
552 tdset = td->td_cpuset;
554 * Verify that a new mask doesn't specify cpus outside of
555 * the set the thread is a member of.
558 if (tdset->cs_id == CPUSET_INVALID)
559 tdset = tdset->cs_parent;
560 if (!CPU_SUBSET(&tdset->cs_mask, mask))
563 * Verify that a new set won't leave an existing thread
564 * mask without a cpu to run on. It can, however, restrict
567 } else if (tdset->cs_id == CPUSET_INVALID) {
568 if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
576 * Replace each thread's cpuset while using deferred release. We
577 * must do this because the thread lock must be held while operating
578 * on the thread and this limits the type of operations allowed.
580 FOREACH_THREAD_IN_PROC(p, td) {
583 * If we presently have an anonymous set or are applying a
584 * mask we must create an anonymous shadow set. That is
585 * either parented to our existing base or the supplied set.
587 * If we have a base set with no anonymous shadow we simply
588 * replace it outright.
590 tdset = td->td_cpuset;
591 if (tdset->cs_id == CPUSET_INVALID || mask) {
592 nset = LIST_FIRST(&freelist);
593 LIST_REMOVE(nset, cs_link);
595 error = cpuset_shadow(tdset, nset, mask);
597 error = _cpuset_create(nset, set,
598 &tdset->cs_mask, CPUSET_INVALID);
600 LIST_INSERT_HEAD(&freelist, nset, cs_link);
605 nset = cpuset_ref(set);
606 cpuset_rel_defer(&droplist, tdset);
607 td->td_cpuset = nset;
614 while ((nset = LIST_FIRST(&droplist)) != NULL)
615 cpuset_rel_complete(nset);
616 while ((nset = LIST_FIRST(&freelist)) != NULL) {
617 LIST_REMOVE(nset, cs_link);
618 uma_zfree(cpuset_zone, nset);
624 * Return a string representing a valid layout for a cpuset_t object.
625 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
628 cpusetobj_strprint(char *buf, const cpuset_t *set)
631 size_t i, bytesp, bufsiz;
635 bufsiz = CPUSETBUFSIZ;
637 for (i = 0; i < (_NCPUWORDS - 1); i++) {
638 bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
642 snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
647 * Build a valid cpuset_t object from a string representation.
648 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
651 cpusetobj_strscan(cpuset_t *set, const char *buf)
656 if (strlen(buf) > CPUSETBUFSIZ - 1)
659 /* Allow to pass a shorter version of the mask when necessary. */
661 for (i = 0; buf[i] != '\0'; i++)
664 if (nwords > _NCPUWORDS)
668 for (i = 0; i < (nwords - 1); i++) {
669 ret = sscanf(buf, "%lx,", &set->__bits[i]);
670 if (ret == 0 || ret == -1)
672 buf = strstr(buf, ",");
677 ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
678 if (ret == 0 || ret == -1)
684 * Apply an anonymous mask to a single thread.
687 cpuset_setthread(lwpid_t id, cpuset_t *mask)
695 nset = uma_zalloc(cpuset_zone, M_WAITOK);
696 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
701 error = cpuset_shadow(td->td_cpuset, nset, mask);
704 td->td_cpuset = nset;
714 uma_zfree(cpuset_zone, nset);
719 * Creates the cpuset for thread0. We make two sets:
721 * 0 - The root set which should represent all valid processors in the
722 * system. It is initially created with a mask of all processors
723 * because we don't know what processors are valid until cpuset_init()
724 * runs. This set is immutable.
725 * 1 - The default set which all processes are a member of until changed.
726 * This allows an administrator to move all threads off of given cpus to
727 * dedicate them to high priority tasks or save power etc.
735 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
736 NULL, NULL, UMA_ALIGN_PTR, 0);
737 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
739 * Create the root system set for the whole machine. Doesn't use
740 * cpuset_create() due to NULL parent.
742 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
743 CPU_FILL(&set->cs_mask);
744 LIST_INIT(&set->cs_children);
745 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
747 set->cs_flags = CPU_SET_ROOT;
749 cpuset_root = &set->cs_mask;
751 * Now derive a default, modifiable set from that to give out.
753 set = uma_zalloc(cpuset_zone, M_WAITOK);
754 error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
755 KASSERT(error == 0, ("Error creating default set: %d\n", error));
757 * Initialize the unit allocator. 0 and 1 are allocated above.
759 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
765 * Create a cpuset, which would be cpuset_create() but
766 * mark the new 'set' as root.
768 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
771 * In case of no error, returns the set in *setp locked with a reference.
774 cpuset_create_root(struct prison *pr, struct cpuset **setp)
779 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
780 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
782 error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
786 KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
787 __func__, __LINE__));
789 /* Mark the set as root. */
791 set->cs_flags |= CPU_SET_ROOT;
797 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
801 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
802 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
805 error = cpuset_setproc(p->p_pid, set, NULL);
813 * This is called once the final set of system cpus is known. Modifies
814 * the root set and all children and mark the root read-only.
817 cpuset_init(void *arg)
822 if (cpuset_modify(cpuset_zero, &mask))
823 panic("Can't set initial cpuset mask.\n");
824 cpuset_zero->cs_flags |= CPU_SET_RDONLY;
826 SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
828 #ifndef _SYS_SYSPROTO_H_
834 sys_cpuset(struct thread *td, struct cpuset_args *uap)
841 root = cpuset_refroot(td->td_cpuset);
843 error = cpuset_create(&set, root, &root->cs_mask);
847 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
849 error = cpuset_setproc(-1, set, NULL);
854 #ifndef _SYS_SYSPROTO_H_
855 struct cpuset_setid_args {
862 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
868 * Presently we only support per-process sets.
870 if (uap->which != CPU_WHICH_PID)
872 set = cpuset_lookup(uap->setid, td);
875 error = cpuset_setproc(uap->id, set, NULL);
880 #ifndef _SYS_SYSPROTO_H_
881 struct cpuset_getid_args {
889 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
898 if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET)
900 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
903 switch (uap->which) {
907 set = cpuset_refbase(ttd->td_cpuset);
911 case CPU_WHICH_CPUSET:
917 switch (uap->level) {
919 nset = cpuset_refroot(set);
923 case CPU_LEVEL_CPUSET:
925 case CPU_LEVEL_WHICH:
931 error = copyout(&id, uap->setid, sizeof(id));
936 #ifndef _SYS_SYSPROTO_H_
937 struct cpuset_getaffinity_args {
946 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
956 if (uap->cpusetsize < sizeof(cpuset_t) ||
957 uap->cpusetsize > CPU_MAXSIZE / NBBY)
959 size = uap->cpusetsize;
960 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
961 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
964 switch (uap->level) {
966 case CPU_LEVEL_CPUSET:
967 switch (uap->which) {
971 set = cpuset_ref(ttd->td_cpuset);
974 case CPU_WHICH_CPUSET:
981 if (uap->level == CPU_LEVEL_ROOT)
982 nset = cpuset_refroot(set);
984 nset = cpuset_refbase(set);
985 CPU_COPY(&nset->cs_mask, mask);
988 case CPU_LEVEL_WHICH:
989 switch (uap->which) {
992 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
996 FOREACH_THREAD_IN_PROC(p, ttd) {
998 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1002 case CPU_WHICH_CPUSET:
1003 case CPU_WHICH_JAIL:
1004 CPU_COPY(&set->cs_mask, mask);
1007 error = intr_getaffinity(uap->id, mask);
1020 error = copyout(mask, uap->mask, size);
1026 #ifndef _SYS_SYSPROTO_H_
1027 struct cpuset_setaffinity_args {
1032 const cpuset_t *mask;
1036 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1038 struct cpuset *nset;
1045 if (uap->cpusetsize < sizeof(cpuset_t) ||
1046 uap->cpusetsize > CPU_MAXSIZE / NBBY)
1048 mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1049 error = copyin(uap->mask, mask, uap->cpusetsize);
1053 * Verify that no high bits are set.
1055 if (uap->cpusetsize > sizeof(cpuset_t)) {
1059 end = cp = (char *)&mask->__bits;
1060 end += uap->cpusetsize;
1061 cp += sizeof(cpuset_t);
1069 switch (uap->level) {
1070 case CPU_LEVEL_ROOT:
1071 case CPU_LEVEL_CPUSET:
1072 error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1075 switch (uap->which) {
1079 set = cpuset_ref(ttd->td_cpuset);
1083 case CPU_WHICH_CPUSET:
1084 case CPU_WHICH_JAIL:
1090 if (uap->level == CPU_LEVEL_ROOT)
1091 nset = cpuset_refroot(set);
1093 nset = cpuset_refbase(set);
1094 error = cpuset_modify(nset, mask);
1098 case CPU_LEVEL_WHICH:
1099 switch (uap->which) {
1101 error = cpuset_setthread(uap->id, mask);
1104 error = cpuset_setproc(uap->id, NULL, mask);
1106 case CPU_WHICH_CPUSET:
1107 case CPU_WHICH_JAIL:
1108 error = cpuset_which(uap->which, uap->id, &p,
1111 error = cpuset_modify(set, mask);
1116 error = intr_setaffinity(uap->id, mask);
1134 ddb_display_cpuset(const cpuset_t *set)
1138 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1139 if (CPU_ISSET(cpu, set)) {
1141 db_printf("%d", cpu);
1144 db_printf(",%d", cpu);
1148 db_printf("<none>");
1151 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1155 LIST_FOREACH(set, &cpuset_ids, cs_link) {
1156 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1157 set, set->cs_id, set->cs_ref, set->cs_flags,
1158 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1159 db_printf(" mask=");
1160 ddb_display_cpuset(&set->cs_mask);