2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
7 * Copyright (c) 2008 Nokia Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/ctype.h>
42 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/refcount.h>
51 #include <sys/sched.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/capsicum.h>
55 #include <sys/cpuset.h>
56 #include <sys/domainset.h>
58 #include <sys/queue.h>
59 #include <sys/libkern.h>
60 #include <sys/limits.h>
62 #include <sys/interrupt.h>
63 #include <sys/vmmeter.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_extern.h>
75 * cpusets provide a mechanism for creating and manipulating sets of
76 * processors for the purpose of constraining the scheduling of threads to
77 * specific processors.
79 * Each process belongs to an identified set, by default this is set 1. Each
80 * thread may further restrict the cpus it may run on to a subset of this
81 * named set. This creates an anonymous set which other threads and processes
82 * may not join by number.
84 * The named set is referred to herein as the 'base' set to avoid ambiguity.
85 * This set is usually a child of a 'root' set while the anonymous set may
86 * simply be referred to as a mask. In the syscall api these are referred to
87 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
89 * Threads inherit their set from their creator whether it be anonymous or
90 * not. This means that anonymous sets are immutable because they may be
91 * shared. To modify an anonymous set a new set is created with the desired
92 * mask and the same parent as the existing anonymous set. This gives the
93 * illusion of each thread having a private mask.
95 * Via the syscall apis a user may ask to retrieve or modify the root, base,
96 * or mask that is discovered via a pid, tid, or setid. Modifying a set
97 * modifies all numbered and anonymous child sets to comply with the new mask.
98 * Modifying a pid or tid's mask applies only to that tid but must still
99 * exist within the assigned parent set.
101 * A thread may not be assigned to a group separate from other threads in
102 * the process. This is to remove ambiguity when the setid is queried with
103 * a pid argument. There is no other technical limitation.
105 * This somewhat complex arrangement is intended to make it easy for
106 * applications to query available processors and bind their threads to
107 * specific processors while also allowing administrators to dynamically
108 * reprovision by changing sets which apply to groups of processes.
110 * A simple application should not concern itself with sets at all and
111 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
112 * meaning 'curthread'. It may query available cpus for that tid with a
113 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
116 LIST_HEAD(domainlist, domainset);
118 static uma_zone_t cpuset_zone;
119 static uma_zone_t domainset_zone;
120 static struct mtx cpuset_lock;
121 static struct setlist cpuset_ids;
122 static struct domainlist cpuset_domains;
123 static struct unrhdr *cpuset_unr;
124 static struct cpuset *cpuset_zero, *cpuset_default, *cpuset_kernel;
125 static struct domainset domainset0, domainset2;
127 /* Return the size of cpuset_t at the kernel level */
128 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
129 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
131 cpuset_t *cpuset_root;
132 cpuset_t cpuset_domain[MAXMEMDOM];
134 static int domainset_valid(const struct domainset *, const struct domainset *);
137 * Find the first non-anonymous set starting from 'set'.
139 static struct cpuset *
140 cpuset_getbase(struct cpuset *set)
143 if (set->cs_id == CPUSET_INVALID)
144 set = set->cs_parent;
149 * Walks up the tree from 'set' to find the root.
151 static struct cpuset *
152 cpuset_getroot(struct cpuset *set)
155 while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL)
156 set = set->cs_parent;
161 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
164 cpuset_ref(struct cpuset *set)
167 refcount_acquire(&set->cs_ref);
172 * Walks up the tree from 'set' to find the root. Returns the root
175 static struct cpuset *
176 cpuset_refroot(struct cpuset *set)
179 return (cpuset_ref(cpuset_getroot(set)));
183 * Find the first non-anonymous set starting from 'set'. Returns this set
184 * referenced. May return the passed in set with an extra ref if it is
187 static struct cpuset *
188 cpuset_refbase(struct cpuset *set)
191 return (cpuset_ref(cpuset_getbase(set)));
195 * Release a reference in a context where it is safe to allocate.
198 cpuset_rel(struct cpuset *set)
202 if (refcount_release(&set->cs_ref) == 0)
204 mtx_lock_spin(&cpuset_lock);
205 LIST_REMOVE(set, cs_siblings);
207 if (id != CPUSET_INVALID)
208 LIST_REMOVE(set, cs_link);
209 mtx_unlock_spin(&cpuset_lock);
210 cpuset_rel(set->cs_parent);
211 uma_zfree(cpuset_zone, set);
212 if (id != CPUSET_INVALID)
213 free_unr(cpuset_unr, id);
217 * Deferred release must be used when in a context that is not safe to
218 * allocate/free. This places any unreferenced sets on the list 'head'.
221 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
224 if (refcount_release(&set->cs_ref) == 0)
226 mtx_lock_spin(&cpuset_lock);
227 LIST_REMOVE(set, cs_siblings);
228 if (set->cs_id != CPUSET_INVALID)
229 LIST_REMOVE(set, cs_link);
230 LIST_INSERT_HEAD(head, set, cs_link);
231 mtx_unlock_spin(&cpuset_lock);
235 * Complete a deferred release. Removes the set from the list provided to
239 cpuset_rel_complete(struct cpuset *set)
241 LIST_REMOVE(set, cs_link);
242 cpuset_rel(set->cs_parent);
243 uma_zfree(cpuset_zone, set);
247 * Find a set based on an id. Returns it with a ref.
249 static struct cpuset *
250 cpuset_lookup(cpusetid_t setid, struct thread *td)
254 if (setid == CPUSET_INVALID)
256 mtx_lock_spin(&cpuset_lock);
257 LIST_FOREACH(set, &cpuset_ids, cs_link)
258 if (set->cs_id == setid)
262 mtx_unlock_spin(&cpuset_lock);
264 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
265 if (set != NULL && jailed(td->td_ucred)) {
266 struct cpuset *jset, *tset;
268 jset = td->td_ucred->cr_prison->pr_cpuset;
269 for (tset = set; tset != NULL; tset = tset->cs_parent)
282 * Create a set in the space provided in 'set' with the provided parameters.
283 * The set is returned with a single ref. May return EDEADLK if the set
284 * will have no valid cpu based on restrictions from the parent.
287 _cpuset_create(struct cpuset *set, struct cpuset *parent,
288 const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
292 domain = parent->cs_domain;
294 mask = &parent->cs_mask;
295 if (!CPU_OVERLAP(&parent->cs_mask, mask))
297 /* The domain must be prepared ahead of time. */
298 if (!domainset_valid(parent->cs_domain, domain))
300 CPU_COPY(mask, &set->cs_mask);
301 LIST_INIT(&set->cs_children);
302 refcount_init(&set->cs_ref, 1);
304 mtx_lock_spin(&cpuset_lock);
305 set->cs_domain = domain;
306 CPU_AND(&set->cs_mask, &parent->cs_mask);
308 set->cs_parent = cpuset_ref(parent);
309 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
310 if (set->cs_id != CPUSET_INVALID)
311 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
312 mtx_unlock_spin(&cpuset_lock);
318 * Create a new non-anonymous set with the requested parent and mask. May
319 * return failures if the mask is invalid or a new number can not be
323 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
329 id = alloc_unr(cpuset_unr);
332 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
333 error = _cpuset_create(set, parent, mask, NULL, id);
336 free_unr(cpuset_unr, id);
337 uma_zfree(cpuset_zone, set);
343 cpuset_freelist_add(struct setlist *list, int count)
348 for (i = 0; i < count; i++) {
349 set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
350 LIST_INSERT_HEAD(list, set, cs_link);
355 cpuset_freelist_init(struct setlist *list, int count)
359 cpuset_freelist_add(list, count);
363 cpuset_freelist_free(struct setlist *list)
367 while ((set = LIST_FIRST(list)) != NULL) {
368 LIST_REMOVE(set, cs_link);
369 uma_zfree(cpuset_zone, set);
374 domainset_freelist_add(struct domainlist *list, int count)
376 struct domainset *set;
379 for (i = 0; i < count; i++) {
380 set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
381 LIST_INSERT_HEAD(list, set, ds_link);
386 domainset_freelist_init(struct domainlist *list, int count)
390 domainset_freelist_add(list, count);
394 domainset_freelist_free(struct domainlist *list)
396 struct domainset *set;
398 while ((set = LIST_FIRST(list)) != NULL) {
399 LIST_REMOVE(set, ds_link);
400 uma_zfree(domainset_zone, set);
404 /* Copy a domainset preserving mask and policy. */
406 domainset_copy(const struct domainset *from, struct domainset *to)
409 DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
410 to->ds_policy = from->ds_policy;
411 to->ds_prefer = from->ds_prefer;
414 /* Return 1 if mask and policy are equal, otherwise 0. */
416 domainset_equal(const struct domainset *one, const struct domainset *two)
419 return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
420 one->ds_policy == two->ds_policy &&
421 one->ds_prefer == two->ds_prefer);
424 /* Return 1 if child is a valid subset of parent. */
426 domainset_valid(const struct domainset *parent, const struct domainset *child)
428 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
429 return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
430 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
434 domainset_restrict(const struct domainset *parent,
435 const struct domainset *child)
437 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
438 return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
439 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
443 * Lookup or create a domainset. The key is provided in ds_mask and
444 * ds_policy. If the domainset does not yet exist the storage in
445 * 'domain' is used to insert. Otherwise this storage is freed to the
446 * domainset_zone and the existing domainset is returned.
448 static struct domainset *
449 _domainset_create(struct domainset *domain, struct domainlist *freelist)
451 struct domainset *ndomain;
454 mtx_lock_spin(&cpuset_lock);
455 LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
456 if (domainset_equal(ndomain, domain))
459 * If the domain does not yet exist we insert it and initialize
460 * various iteration helpers which are not part of the key.
462 if (ndomain == NULL) {
463 LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
464 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
465 max = DOMAINSET_FLS(&domain->ds_mask) + 1;
466 for (i = 0, j = 0; i < max; i++)
467 if (DOMAINSET_ISSET(i, &domain->ds_mask))
468 domain->ds_order[j++] = i;
470 mtx_unlock_spin(&cpuset_lock);
473 if (freelist != NULL)
474 LIST_INSERT_HEAD(freelist, domain, ds_link);
476 uma_zfree(domainset_zone, domain);
482 * Create or lookup a domainset based on the key held in 'domain'.
485 domainset_create(const struct domainset *domain)
487 struct domainset *ndomain;
490 * Validate the policy. It must specify a useable policy number with
491 * only valid domains. Preferred must include the preferred domain
494 if (domain->ds_policy <= DOMAINSET_POLICY_INVALID ||
495 domain->ds_policy > DOMAINSET_POLICY_MAX)
497 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
498 !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask))
500 if (!DOMAINSET_SUBSET(&domainset0.ds_mask, &domain->ds_mask))
502 ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
503 domainset_copy(domain, ndomain);
504 return _domainset_create(ndomain, NULL);
508 * Update thread domainset pointers.
511 domainset_notify(void)
516 sx_slock(&allproc_lock);
517 FOREACH_PROC_IN_SYSTEM(p) {
519 if (p->p_state == PRS_NEW) {
523 FOREACH_THREAD_IN_PROC(p, td) {
525 td->td_domain.dr_policy = td->td_cpuset->cs_domain;
530 sx_sunlock(&allproc_lock);
531 kernel_object->domain.dr_policy = cpuset_kernel->cs_domain;
535 * Create a new set that is a subset of a parent.
537 static struct domainset *
538 domainset_shadow(const struct domainset *pdomain,
539 const struct domainset *domain, struct domainlist *freelist)
541 struct domainset *ndomain;
543 ndomain = LIST_FIRST(freelist);
544 LIST_REMOVE(ndomain, ds_link);
547 * Initialize the key from the request.
549 domainset_copy(domain, ndomain);
552 * Restrict the key by the parent.
554 DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
556 return _domainset_create(ndomain, freelist);
560 * Recursively check for errors that would occur from applying mask to
561 * the tree of sets starting at 'set'. Checks for sets that would become
562 * empty as well as RDONLY flags.
565 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
571 mtx_assert(&cpuset_lock, MA_OWNED);
572 if (set->cs_flags & CPU_SET_RDONLY)
575 if (!CPU_OVERLAP(&set->cs_mask, mask))
577 CPU_COPY(&set->cs_mask, &newmask);
578 CPU_AND(&newmask, mask);
580 CPU_COPY(mask, &newmask);
582 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
583 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
589 * Applies the mask 'mask' without checking for empty sets or permissions.
592 cpuset_update(struct cpuset *set, cpuset_t *mask)
596 mtx_assert(&cpuset_lock, MA_OWNED);
597 CPU_AND(&set->cs_mask, mask);
598 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
599 cpuset_update(nset, &set->cs_mask);
605 * Modify the set 'set' to use a copy of the mask provided. Apply this new
606 * mask to restrict all children in the tree. Checks for validity before
607 * applying the changes.
610 cpuset_modify(struct cpuset *set, cpuset_t *mask)
615 error = priv_check(curthread, PRIV_SCHED_CPUSET);
619 * In case we are called from within the jail
620 * we do not allow modifying the dedicated root
621 * cpuset of the jail but may still allow to
624 if (jailed(curthread->td_ucred) &&
625 set->cs_flags & CPU_SET_ROOT)
628 * Verify that we have access to this set of
631 root = cpuset_getroot(set);
632 mtx_lock_spin(&cpuset_lock);
633 if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
637 error = cpuset_testupdate(set, mask, 0);
640 CPU_COPY(mask, &set->cs_mask);
641 cpuset_update(set, mask);
643 mtx_unlock_spin(&cpuset_lock);
649 * Recursively check for errors that would occur from applying mask to
650 * the tree of sets starting at 'set'. Checks for sets that would become
651 * empty as well as RDONLY flags.
654 cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
655 struct domainset *orig, int *count, int check_mask)
658 struct domainset *domain;
659 struct domainset newset;
662 mtx_assert(&cpuset_lock, MA_OWNED);
663 if (set->cs_flags & CPU_SET_RDONLY)
665 domain = set->cs_domain;
666 domainset_copy(domain, &newset);
667 if (!domainset_equal(domain, orig)) {
668 if (!domainset_restrict(domain, dset))
670 DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
671 /* Count the number of domains that are changing. */
675 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
676 if ((error = cpuset_testupdate_domain(nset, &newset, domain,
683 * Applies the mask 'mask' without checking for empty sets or permissions.
686 cpuset_update_domain(struct cpuset *set, struct domainset *domain,
687 struct domainset *orig, struct domainlist *domains)
691 mtx_assert(&cpuset_lock, MA_OWNED);
693 * If this domainset has changed from the parent we must calculate
694 * a new set. Otherwise it simply inherits from the parent. When
695 * we inherit from the parent we get a new mask and policy. If the
696 * set is modified from the parent we keep the policy and only
699 if (set->cs_domain != orig) {
700 orig = set->cs_domain;
701 set->cs_domain = domainset_shadow(domain, orig, domains);
703 set->cs_domain = domain;
704 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
705 cpuset_update_domain(nset, set->cs_domain, orig, domains);
711 * Modify the set 'set' to use a copy the domainset provided. Apply this new
712 * mask to restrict all children in the tree. Checks for validity before
713 * applying the changes.
716 cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
718 struct domainlist domains;
719 struct domainset temp;
720 struct domainset *dset;
722 int ndomains, needed;
725 error = priv_check(curthread, PRIV_SCHED_CPUSET);
729 * In case we are called from within the jail
730 * we do not allow modifying the dedicated root
731 * cpuset of the jail but may still allow to
734 if (jailed(curthread->td_ucred) &&
735 set->cs_flags & CPU_SET_ROOT)
737 domainset_freelist_init(&domains, 0);
738 domain = domainset_create(domain);
739 ndomains = needed = 0;
741 if (ndomains < needed) {
742 domainset_freelist_add(&domains, needed - ndomains);
745 root = cpuset_getroot(set);
746 mtx_lock_spin(&cpuset_lock);
747 dset = root->cs_domain;
749 * Verify that we have access to this set of domains.
751 if (root && !domainset_valid(dset, domain)) {
756 * If applying prefer we keep the current set as the fallback.
758 if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
759 DOMAINSET_COPY(&set->cs_domain->ds_mask,
762 * Determine whether we can apply this set of domains and
763 * how many new domain structures it will require.
765 domainset_copy(domain, &temp);
767 error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
771 } while (ndomains < needed);
772 dset = set->cs_domain;
773 cpuset_update_domain(set, domain, dset, &domains);
775 mtx_unlock_spin(&cpuset_lock);
776 domainset_freelist_free(&domains);
784 * Resolve the 'which' parameter of several cpuset apis.
786 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
787 * checks for permission via p_cansched().
789 * For WHICH_SET returns a valid set with a new reference.
791 * -1 may be supplied for any argument to mean the current proc/thread or
792 * the base set of the current thread. May fail with ESRCH/EPERM.
795 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
796 struct cpuset **setp)
813 if ((p = pfind(id)) == NULL)
828 case CPU_WHICH_CPUSET:
830 thread_lock(curthread);
831 set = cpuset_refbase(curthread->td_cpuset);
832 thread_unlock(curthread);
834 set = cpuset_lookup(id, curthread);
842 /* Find `set' for prison with given id. */
845 sx_slock(&allprison_lock);
846 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
847 sx_sunlock(&allprison_lock);
850 cpuset_ref(pr->pr_cpuset);
851 *setp = pr->pr_cpuset;
852 mtx_unlock(&pr->pr_mtx);
856 case CPU_WHICH_DOMAIN:
861 error = p_cansched(curthread, p);
867 td = FIRST_THREAD_IN_PROC(p);
874 cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
875 const struct domainset *domain)
877 struct cpuset *parent;
878 struct domainset *dset;
880 parent = cpuset_getbase(set);
882 * If we are restricting a cpu mask it must be a subset of the
883 * parent or invalid CPUs have been specified.
885 if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
889 * If we are restricting a domain mask it must be a subset of the
890 * parent or invalid domains have been specified.
892 dset = parent->cs_domain;
893 if (domain != NULL && !domainset_valid(dset, domain))
900 * Create an anonymous set with the provided mask in the space provided by
901 * 'nset'. If the passed in set is anonymous we use its parent otherwise
902 * the new set is a child of 'set'.
905 cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
906 const cpuset_t *mask, const struct domainset *domain,
907 struct setlist *cpusets, struct domainlist *domains)
909 struct cpuset *parent;
911 struct domainset *dset;
915 error = cpuset_testshadow(set, mask, domain);
919 parent = cpuset_getbase(set);
920 dset = parent->cs_domain;
922 mask = &set->cs_mask;
924 d = domainset_shadow(dset, domain, domains);
927 nset = LIST_FIRST(cpusets);
928 error = _cpuset_create(nset, parent, mask, d, CPUSET_INVALID);
930 LIST_REMOVE(nset, cs_link);
936 static struct cpuset *
937 cpuset_update_thread(struct thread *td, struct cpuset *nset)
939 struct cpuset *tdset;
941 tdset = td->td_cpuset;
942 td->td_cpuset = nset;
943 td->td_domain.dr_policy = nset->cs_domain;
950 cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
951 struct domainset *domain)
953 struct cpuset *parent;
955 parent = cpuset_getbase(tdset);
957 mask = &tdset->cs_mask;
959 domain = tdset->cs_domain;
960 return cpuset_testshadow(parent, mask, domain);
964 cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
965 struct domainset *domain, struct cpuset **nsetp,
966 struct setlist *freelist, struct domainlist *domainlist)
968 struct cpuset *parent;
970 parent = cpuset_getbase(tdset);
972 mask = &tdset->cs_mask;
974 domain = tdset->cs_domain;
975 return cpuset_shadow(parent, nsetp, mask, domain, freelist,
980 cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
981 cpuset_t *mask, struct domainset *domain)
983 struct cpuset *parent;
985 parent = cpuset_getbase(tdset);
988 * If the thread restricted its mask then apply that same
989 * restriction to the new set, otherwise take it wholesale.
991 if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
992 CPU_COPY(&tdset->cs_mask, mask);
993 CPU_AND(mask, &set->cs_mask);
995 CPU_COPY(&set->cs_mask, mask);
998 * If the thread restricted the domain then we apply the
999 * restriction to the new set but retain the policy.
1001 if (tdset->cs_domain != parent->cs_domain) {
1002 domainset_copy(tdset->cs_domain, domain);
1003 DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
1005 domainset_copy(set->cs_domain, domain);
1007 if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
1014 cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
1016 struct domainset domain;
1019 if (tdset->cs_id != CPUSET_INVALID)
1021 return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1025 cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
1026 struct cpuset **nsetp, struct setlist *freelist,
1027 struct domainlist *domainlist)
1029 struct domainset domain;
1034 * If we're replacing on a thread that has not constrained the
1035 * original set we can simply accept the new set.
1037 if (tdset->cs_id != CPUSET_INVALID) {
1038 *nsetp = cpuset_ref(set);
1041 error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1045 return cpuset_shadow(tdset, nsetp, &mask, &domain, freelist,
1050 * Handle three cases for updating an entire process.
1052 * 1) Set is non-null. This reparents all anonymous sets to the provided
1053 * set and replaces all non-anonymous td_cpusets with the provided set.
1054 * 2) Mask is non-null. This replaces or creates anonymous sets for every
1055 * thread with the existing base as a parent.
1056 * 3) domain is non-null. This creates anonymous sets for every thread
1057 * and replaces the domain set.
1059 * This is overly complicated because we can't allocate while holding a
1060 * spinlock and spinlocks must be held while changing and examining thread
1064 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1065 struct domainset *domain)
1067 struct setlist freelist;
1068 struct setlist droplist;
1069 struct domainlist domainlist;
1070 struct cpuset *nset;
1078 * The algorithm requires two passes due to locking considerations.
1080 * 1) Lookup the process and acquire the locks in the required order.
1081 * 2) If enough cpusets have not been allocated release the locks and
1082 * allocate them. Loop.
1084 cpuset_freelist_init(&freelist, 1);
1085 domainset_freelist_init(&domainlist, 1);
1087 LIST_INIT(&droplist);
1090 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1093 if (nfree >= p->p_numthreads)
1095 threads = p->p_numthreads;
1097 if (nfree < threads) {
1098 cpuset_freelist_add(&freelist, threads - nfree);
1099 domainset_freelist_add(&domainlist, threads - nfree);
1103 PROC_LOCK_ASSERT(p, MA_OWNED);
1105 * Now that the appropriate locks are held and we have enough cpusets,
1106 * make sure the operation will succeed before applying changes. The
1107 * proc lock prevents td_cpuset from changing between calls.
1110 FOREACH_THREAD_IN_PROC(p, td) {
1113 error = cpuset_setproc_test_setthread(td->td_cpuset,
1116 error = cpuset_setproc_test_maskthread(td->td_cpuset,
1123 * Replace each thread's cpuset while using deferred release. We
1124 * must do this because the thread lock must be held while operating
1125 * on the thread and this limits the type of operations allowed.
1127 FOREACH_THREAD_IN_PROC(p, td) {
1130 error = cpuset_setproc_setthread(td->td_cpuset, set,
1131 &nset, &freelist, &domainlist);
1133 error = cpuset_setproc_maskthread(td->td_cpuset, mask,
1134 domain, &nset, &freelist, &domainlist);
1139 cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1145 while ((nset = LIST_FIRST(&droplist)) != NULL)
1146 cpuset_rel_complete(nset);
1147 cpuset_freelist_free(&freelist);
1148 domainset_freelist_free(&domainlist);
1153 bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen)
1161 for (i = 0; i < __bitset_words(setlen); i++) {
1170 if (bufsiz < sizeof(__STRING(ULONG_MAX)))
1172 bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]);
1180 bitset_strscan(struct bitset *set, int setlen, const char *buf)
1185 BIT_ZERO(setlen, set);
1187 for (i = 0; i < __bitset_words(setlen); i++) {
1192 ret = sscanf(p, "%lx", &set->__bits[i]);
1193 if (ret == 0 || ret == -1)
1195 while (isxdigit(*p))
1202 * Return a string representing a valid layout for a cpuset_t object.
1203 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1206 cpusetobj_strprint(char *buf, const cpuset_t *set)
1209 bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set,
1215 * Build a valid cpuset_t object from a string representation.
1216 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1219 cpusetobj_strscan(cpuset_t *set, const char *buf)
1223 if (strlen(buf) > CPUSETBUFSIZ - 1)
1226 p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)];
1234 * Handle a domainset specifier in the sysctl tree. A poiner to a pointer to
1235 * a domainset is in arg1. If the user specifies a valid domainset the
1236 * pointer is updated.
1239 * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred
1242 sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)
1244 char buf[DOMAINSETBUFSIZ];
1245 struct domainset *dset;
1246 struct domainset key;
1247 int policy, prefer, error;
1250 dset = *(struct domainset **)arg1;
1254 p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ,
1255 (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE);
1256 sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer);
1258 sprintf(buf, "<NULL>");
1259 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1260 if (error != 0 || req->newptr == NULL)
1264 * Read in and validate the string.
1266 memset(&key, 0, sizeof(key));
1267 p = &buf[bitset_strscan((struct bitset *)&key.ds_mask,
1268 DOMAINSET_SETSIZE, buf)];
1271 if (sscanf(p, ":%d:%d", &policy, &prefer) != 2)
1273 key.ds_policy = policy;
1274 key.ds_prefer = prefer;
1276 /* Domainset_create() validates the policy.*/
1277 dset = domainset_create(&key);
1280 *(struct domainset **)arg1 = dset;
1286 * Apply an anonymous mask or a domain to a single thread.
1289 _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1291 struct setlist cpusets;
1292 struct domainlist domainlist;
1293 struct cpuset *nset;
1299 cpuset_freelist_init(&cpusets, 1);
1300 domainset_freelist_init(&domainlist, domain != NULL);
1301 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1306 error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
1307 &cpusets, &domainlist);
1309 set = cpuset_update_thread(td, nset);
1315 cpuset_freelist_free(&cpusets);
1316 domainset_freelist_free(&domainlist);
1321 * Apply an anonymous mask to a single thread.
1324 cpuset_setthread(lwpid_t id, cpuset_t *mask)
1327 return _cpuset_setthread(id, mask, NULL);
1331 * Apply new cpumask to the ithread.
1334 cpuset_setithread(lwpid_t id, int cpu)
1340 CPU_COPY(cpuset_root, &mask);
1342 CPU_SET(cpu, &mask);
1343 return _cpuset_setthread(id, &mask, NULL);
1347 * Create the domainset for cpuset 0, 1 and cpuset 2.
1350 domainset_zero(void)
1352 struct domainset *dset;
1355 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
1358 DOMAINSET_ZERO(&dset->ds_mask);
1359 for (i = 0; i < vm_ndomains; i++)
1360 DOMAINSET_SET(i, &dset->ds_mask);
1361 dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH;
1362 dset->ds_prefer = -1;
1363 curthread->td_domain.dr_policy = _domainset_create(dset, NULL);
1365 domainset_copy(dset, &domainset2);
1366 domainset2.ds_policy = DOMAINSET_POLICY_INTERLEAVE;
1367 kernel_object->domain.dr_policy = _domainset_create(&domainset2, NULL);
1371 * Creates system-wide cpusets and the cpuset for thread0 including three
1374 * 0 - The root set which should represent all valid processors in the
1375 * system. It is initially created with a mask of all processors
1376 * because we don't know what processors are valid until cpuset_init()
1377 * runs. This set is immutable.
1378 * 1 - The default set which all processes are a member of until changed.
1379 * This allows an administrator to move all threads off of given cpus to
1380 * dedicate them to high priority tasks or save power etc.
1381 * 2 - The kernel set which allows restriction and policy to be applied only
1382 * to kernel threads and the kernel_object.
1385 cpuset_thread0(void)
1391 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1392 NULL, NULL, UMA_ALIGN_CACHE, 0);
1393 domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1394 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
1397 * Create the root system set (0) for the whole machine. Doesn't use
1398 * cpuset_create() due to NULL parent.
1400 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1401 CPU_COPY(&all_cpus, &set->cs_mask);
1402 LIST_INIT(&set->cs_children);
1403 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
1405 set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY;
1406 set->cs_domain = &domainset0;
1408 cpuset_root = &set->cs_mask;
1411 * Now derive a default (1), modifiable set from that to give out.
1413 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1414 error = _cpuset_create(set, cpuset_zero, NULL, NULL, 1);
1415 KASSERT(error == 0, ("Error creating default set: %d\n", error));
1416 cpuset_default = set;
1418 * Create the kernel set (2).
1420 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1421 error = _cpuset_create(set, cpuset_zero, NULL, NULL, 2);
1422 KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
1423 set->cs_domain = &domainset2;
1424 cpuset_kernel = set;
1427 * Initialize the unit allocator. 0 and 1 are allocated above.
1429 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
1432 * If MD code has not initialized per-domain cpusets, place all
1435 for (i = 0; i < MAXMEMDOM; i++)
1436 if (!CPU_EMPTY(&cpuset_domain[i]))
1438 CPU_COPY(&all_cpus, &cpuset_domain[0]);
1441 return (cpuset_default);
1445 cpuset_kernthread(struct thread *td)
1450 set = td->td_cpuset;
1451 td->td_cpuset = cpuset_ref(cpuset_kernel);
1457 * Create a cpuset, which would be cpuset_create() but
1458 * mark the new 'set' as root.
1460 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
1463 * In case of no error, returns the set in *setp locked with a reference.
1466 cpuset_create_root(struct prison *pr, struct cpuset **setp)
1471 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1472 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1474 error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1478 KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
1479 __func__, __LINE__));
1481 /* Mark the set as root. */
1483 set->cs_flags |= CPU_SET_ROOT;
1489 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1493 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1494 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1497 error = cpuset_setproc(p->p_pid, set, NULL, NULL);
1504 #ifndef _SYS_SYSPROTO_H_
1505 struct cpuset_args {
1510 sys_cpuset(struct thread *td, struct cpuset_args *uap)
1512 struct cpuset *root;
1517 root = cpuset_refroot(td->td_cpuset);
1519 error = cpuset_create(&set, root, &root->cs_mask);
1523 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1525 error = cpuset_setproc(-1, set, NULL, NULL);
1530 #ifndef _SYS_SYSPROTO_H_
1531 struct cpuset_setid_args {
1538 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1541 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1545 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1546 id_t id, cpusetid_t setid)
1552 * Presently we only support per-process sets.
1554 if (which != CPU_WHICH_PID)
1556 set = cpuset_lookup(setid, td);
1559 error = cpuset_setproc(id, set, NULL, NULL);
1564 #ifndef _SYS_SYSPROTO_H_
1565 struct cpuset_getid_args {
1573 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1576 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1581 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1582 id_t id, cpusetid_t *setid)
1584 struct cpuset *nset;
1591 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1593 error = cpuset_which(which, id, &p, &ttd, &set);
1600 set = cpuset_refbase(ttd->td_cpuset);
1604 case CPU_WHICH_CPUSET:
1605 case CPU_WHICH_JAIL:
1608 case CPU_WHICH_DOMAIN:
1612 case CPU_LEVEL_ROOT:
1613 nset = cpuset_refroot(set);
1617 case CPU_LEVEL_CPUSET:
1619 case CPU_LEVEL_WHICH:
1625 error = copyout(&tmpid, setid, sizeof(tmpid));
1630 #ifndef _SYS_SYSPROTO_H_
1631 struct cpuset_getaffinity_args {
1640 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1643 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
1644 uap->id, uap->cpusetsize, uap->mask));
1648 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1649 id_t id, size_t cpusetsize, cpuset_t *maskp)
1652 struct cpuset *nset;
1659 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1661 /* In Capability mode, you can only get your own CPU set. */
1662 if (IN_CAPABILITY_MODE(td)) {
1663 if (level != CPU_LEVEL_WHICH)
1665 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1671 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1672 error = cpuset_which(which, id, &p, &ttd, &set);
1676 case CPU_LEVEL_ROOT:
1677 case CPU_LEVEL_CPUSET:
1682 set = cpuset_ref(ttd->td_cpuset);
1685 case CPU_WHICH_CPUSET:
1686 case CPU_WHICH_JAIL:
1689 case CPU_WHICH_INTRHANDLER:
1690 case CPU_WHICH_ITHREAD:
1691 case CPU_WHICH_DOMAIN:
1695 if (level == CPU_LEVEL_ROOT)
1696 nset = cpuset_refroot(set);
1698 nset = cpuset_refbase(set);
1699 CPU_COPY(&nset->cs_mask, mask);
1702 case CPU_LEVEL_WHICH:
1706 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1710 FOREACH_THREAD_IN_PROC(p, ttd) {
1712 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1716 case CPU_WHICH_CPUSET:
1717 case CPU_WHICH_JAIL:
1718 CPU_COPY(&set->cs_mask, mask);
1721 case CPU_WHICH_INTRHANDLER:
1722 case CPU_WHICH_ITHREAD:
1723 error = intr_getaffinity(id, which, mask);
1725 case CPU_WHICH_DOMAIN:
1726 if (id < 0 || id >= MAXMEMDOM)
1729 CPU_COPY(&cpuset_domain[id], mask);
1742 error = copyout(mask, maskp, size);
1748 #ifndef _SYS_SYSPROTO_H_
1749 struct cpuset_setaffinity_args {
1754 const cpuset_t *mask;
1758 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1761 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
1762 uap->id, uap->cpusetsize, uap->mask));
1766 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1767 id_t id, size_t cpusetsize, const cpuset_t *maskp)
1769 struct cpuset *nset;
1776 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1778 /* In Capability mode, you can only set your own CPU set. */
1779 if (IN_CAPABILITY_MODE(td)) {
1780 if (level != CPU_LEVEL_WHICH)
1782 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1787 mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1788 error = copyin(maskp, mask, cpusetsize);
1792 * Verify that no high bits are set.
1794 if (cpusetsize > sizeof(cpuset_t)) {
1798 end = cp = (char *)&mask->__bits;
1800 cp += sizeof(cpuset_t);
1809 case CPU_LEVEL_ROOT:
1810 case CPU_LEVEL_CPUSET:
1811 error = cpuset_which(which, id, &p, &ttd, &set);
1818 set = cpuset_ref(ttd->td_cpuset);
1822 case CPU_WHICH_CPUSET:
1823 case CPU_WHICH_JAIL:
1826 case CPU_WHICH_INTRHANDLER:
1827 case CPU_WHICH_ITHREAD:
1828 case CPU_WHICH_DOMAIN:
1832 if (level == CPU_LEVEL_ROOT)
1833 nset = cpuset_refroot(set);
1835 nset = cpuset_refbase(set);
1836 error = cpuset_modify(nset, mask);
1840 case CPU_LEVEL_WHICH:
1843 error = cpuset_setthread(id, mask);
1846 error = cpuset_setproc(id, NULL, mask, NULL);
1848 case CPU_WHICH_CPUSET:
1849 case CPU_WHICH_JAIL:
1850 error = cpuset_which(which, id, &p, &ttd, &set);
1852 error = cpuset_modify(set, mask);
1857 case CPU_WHICH_INTRHANDLER:
1858 case CPU_WHICH_ITHREAD:
1859 error = intr_setaffinity(id, which, mask);
1875 #ifndef _SYS_SYSPROTO_H_
1876 struct cpuset_getdomain_args {
1880 size_t domainsetsize;
1886 sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
1889 return (kern_cpuset_getdomain(td, uap->level, uap->which,
1890 uap->id, uap->domainsetsize, uap->mask, uap->policy));
1894 kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
1895 id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp)
1897 struct domainset outset;
1899 struct cpuset *nset;
1901 struct domainset *dset;
1906 if (domainsetsize < sizeof(domainset_t) ||
1907 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
1909 /* In Capability mode, you can only get your own domain set. */
1910 if (IN_CAPABILITY_MODE(td)) {
1911 if (level != CPU_LEVEL_WHICH)
1913 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1918 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
1919 bzero(&outset, sizeof(outset));
1920 error = cpuset_which(which, id, &p, &ttd, &set);
1924 case CPU_LEVEL_ROOT:
1925 case CPU_LEVEL_CPUSET:
1930 set = cpuset_ref(ttd->td_cpuset);
1933 case CPU_WHICH_CPUSET:
1934 case CPU_WHICH_JAIL:
1937 case CPU_WHICH_INTRHANDLER:
1938 case CPU_WHICH_ITHREAD:
1939 case CPU_WHICH_DOMAIN:
1943 if (level == CPU_LEVEL_ROOT)
1944 nset = cpuset_refroot(set);
1946 nset = cpuset_refbase(set);
1947 domainset_copy(nset->cs_domain, &outset);
1950 case CPU_LEVEL_WHICH:
1954 domainset_copy(ttd->td_cpuset->cs_domain, &outset);
1958 FOREACH_THREAD_IN_PROC(p, ttd) {
1960 dset = ttd->td_cpuset->cs_domain;
1961 /* Show all domains in the proc. */
1962 DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
1963 /* Last policy wins. */
1964 outset.ds_policy = dset->ds_policy;
1965 outset.ds_prefer = dset->ds_prefer;
1969 case CPU_WHICH_CPUSET:
1970 case CPU_WHICH_JAIL:
1971 domainset_copy(set->cs_domain, &outset);
1974 case CPU_WHICH_INTRHANDLER:
1975 case CPU_WHICH_ITHREAD:
1976 case CPU_WHICH_DOMAIN:
1990 * Translate prefer into a set containing only the preferred domain,
1991 * not the entire fallback set.
1993 if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
1994 DOMAINSET_ZERO(&outset.ds_mask);
1995 DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
1997 DOMAINSET_COPY(&outset.ds_mask, mask);
1999 error = copyout(mask, maskp, domainsetsize);
2001 if (suword32(policyp, outset.ds_policy) != 0)
2008 #ifndef _SYS_SYSPROTO_H_
2009 struct cpuset_setdomain_args {
2013 size_t domainsetsize;
2019 sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
2022 return (kern_cpuset_setdomain(td, uap->level, uap->which,
2023 uap->id, uap->domainsetsize, uap->mask, uap->policy));
2027 kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2028 id_t id, size_t domainsetsize, const domainset_t *maskp, int policy)
2030 struct cpuset *nset;
2034 struct domainset domain;
2038 if (domainsetsize < sizeof(domainset_t) ||
2039 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2041 /* In Capability mode, you can only set your own CPU set. */
2042 if (IN_CAPABILITY_MODE(td)) {
2043 if (level != CPU_LEVEL_WHICH)
2045 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
2050 memset(&domain, 0, sizeof(domain));
2051 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2052 error = copyin(maskp, mask, domainsetsize);
2056 * Verify that no high bits are set.
2058 if (domainsetsize > sizeof(domainset_t)) {
2062 end = cp = (char *)&mask->__bits;
2063 end += domainsetsize;
2064 cp += sizeof(domainset_t);
2072 DOMAINSET_COPY(mask, &domain.ds_mask);
2073 domain.ds_policy = policy;
2074 if (policy <= DOMAINSET_POLICY_INVALID ||
2075 policy > DOMAINSET_POLICY_MAX)
2078 /* Translate preferred policy into a mask and fallback. */
2079 if (policy == DOMAINSET_POLICY_PREFER) {
2080 /* Only support a single preferred domain. */
2081 if (DOMAINSET_COUNT(&domain.ds_mask) != 1)
2083 domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
2084 /* This will be constrained by domainset_shadow(). */
2085 DOMAINSET_FILL(&domain.ds_mask);
2089 case CPU_LEVEL_ROOT:
2090 case CPU_LEVEL_CPUSET:
2091 error = cpuset_which(which, id, &p, &ttd, &set);
2098 set = cpuset_ref(ttd->td_cpuset);
2102 case CPU_WHICH_CPUSET:
2103 case CPU_WHICH_JAIL:
2106 case CPU_WHICH_INTRHANDLER:
2107 case CPU_WHICH_ITHREAD:
2108 case CPU_WHICH_DOMAIN:
2112 if (level == CPU_LEVEL_ROOT)
2113 nset = cpuset_refroot(set);
2115 nset = cpuset_refbase(set);
2116 error = cpuset_modify_domain(nset, &domain);
2120 case CPU_LEVEL_WHICH:
2123 error = _cpuset_setthread(id, NULL, &domain);
2126 error = cpuset_setproc(id, NULL, NULL, &domain);
2128 case CPU_WHICH_CPUSET:
2129 case CPU_WHICH_JAIL:
2130 error = cpuset_which(which, id, &p, &ttd, &set);
2132 error = cpuset_modify_domain(set, &domain);
2137 case CPU_WHICH_INTRHANDLER:
2138 case CPU_WHICH_ITHREAD:
2156 ddb_display_bitset(const struct bitset *set, int size)
2160 for (once = 0, bit = 0; bit < size; bit++) {
2161 if (CPU_ISSET(bit, set)) {
2163 db_printf("%d", bit);
2166 db_printf(",%d", bit);
2170 db_printf("<none>");
2174 ddb_display_cpuset(const cpuset_t *set)
2176 ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
2180 ddb_display_domainset(const domainset_t *set)
2182 ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
2185 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
2189 LIST_FOREACH(set, &cpuset_ids, cs_link) {
2190 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
2191 set, set->cs_id, set->cs_ref, set->cs_flags,
2192 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
2193 db_printf(" cpu mask=");
2194 ddb_display_cpuset(&set->cs_mask);
2196 db_printf(" domain policy %d prefer %d mask=",
2197 set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
2198 ddb_display_domainset(&set->cs_domain->ds_mask);
2205 DB_SHOW_COMMAND(domainsets, db_show_domainsets)
2207 struct domainset *set;
2209 LIST_FOREACH(set, &cpuset_domains, ds_link) {
2210 db_printf("set=%p policy %d prefer %d cnt %d\n",
2211 set, set->ds_policy, set->ds_prefer, set->ds_cnt);
2212 db_printf(" mask =");
2213 ddb_display_domainset(&set->ds_mask);