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/sysproto.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
48 #include <sys/refcount.h>
49 #include <sys/sched.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/capsicum.h>
53 #include <sys/cpuset.h>
54 #include <sys/domainset.h>
56 #include <sys/queue.h>
57 #include <sys/libkern.h>
58 #include <sys/limits.h>
60 #include <sys/interrupt.h>
61 #include <sys/vmmeter.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_phys.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, ...).
115 static uma_zone_t cpuset_zone;
116 static uma_zone_t domainset_zone;
117 static struct mtx cpuset_lock;
118 static struct setlist cpuset_ids;
119 static struct domainlist cpuset_domains;
120 static struct unrhdr *cpuset_unr;
121 static struct cpuset *cpuset_zero, *cpuset_default;
123 /* Return the size of cpuset_t at the kernel level */
124 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
125 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
127 cpuset_t *cpuset_root;
128 cpuset_t cpuset_domain[MAXMEMDOM];
130 static int domainset_valid(const struct domainset *, const struct domainset *);
133 * Find the first non-anonymous set starting from 'set'.
135 static struct cpuset *
136 cpuset_getbase(struct cpuset *set)
139 if (set->cs_id == CPUSET_INVALID)
140 set = set->cs_parent;
145 * Walks up the tree from 'set' to find the root.
147 static struct cpuset *
148 cpuset_getroot(struct cpuset *set)
151 while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL)
152 set = set->cs_parent;
157 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
160 cpuset_ref(struct cpuset *set)
163 refcount_acquire(&set->cs_ref);
168 * Walks up the tree from 'set' to find the root. Returns the root
171 static struct cpuset *
172 cpuset_refroot(struct cpuset *set)
175 return (cpuset_ref(cpuset_getroot(set)));
179 * Find the first non-anonymous set starting from 'set'. Returns this set
180 * referenced. May return the passed in set with an extra ref if it is
183 static struct cpuset *
184 cpuset_refbase(struct cpuset *set)
187 return (cpuset_ref(cpuset_getbase(set)));
191 * Release a reference in a context where it is safe to allocate.
194 cpuset_rel(struct cpuset *set)
198 if (refcount_release(&set->cs_ref) == 0)
200 mtx_lock_spin(&cpuset_lock);
201 LIST_REMOVE(set, cs_siblings);
203 if (id != CPUSET_INVALID)
204 LIST_REMOVE(set, cs_link);
205 mtx_unlock_spin(&cpuset_lock);
206 cpuset_rel(set->cs_parent);
207 uma_zfree(cpuset_zone, set);
208 if (id != CPUSET_INVALID)
209 free_unr(cpuset_unr, id);
213 * Deferred release must be used when in a context that is not safe to
214 * allocate/free. This places any unreferenced sets on the list 'head'.
217 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
220 if (refcount_release(&set->cs_ref) == 0)
222 mtx_lock_spin(&cpuset_lock);
223 LIST_REMOVE(set, cs_siblings);
224 if (set->cs_id != CPUSET_INVALID)
225 LIST_REMOVE(set, cs_link);
226 LIST_INSERT_HEAD(head, set, cs_link);
227 mtx_unlock_spin(&cpuset_lock);
231 * Complete a deferred release. Removes the set from the list provided to
235 cpuset_rel_complete(struct cpuset *set)
237 LIST_REMOVE(set, cs_link);
238 cpuset_rel(set->cs_parent);
239 uma_zfree(cpuset_zone, set);
243 * Find a set based on an id. Returns it with a ref.
245 static struct cpuset *
246 cpuset_lookup(cpusetid_t setid, struct thread *td)
250 if (setid == CPUSET_INVALID)
252 mtx_lock_spin(&cpuset_lock);
253 LIST_FOREACH(set, &cpuset_ids, cs_link)
254 if (set->cs_id == setid)
258 mtx_unlock_spin(&cpuset_lock);
260 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
261 if (set != NULL && jailed(td->td_ucred)) {
262 struct cpuset *jset, *tset;
264 jset = td->td_ucred->cr_prison->pr_cpuset;
265 for (tset = set; tset != NULL; tset = tset->cs_parent)
278 * Create a set in the space provided in 'set' with the provided parameters.
279 * The set is returned with a single ref. May return EDEADLK if the set
280 * will have no valid cpu based on restrictions from the parent.
283 _cpuset_create(struct cpuset *set, struct cpuset *parent,
284 const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
288 domain = parent->cs_domain;
290 mask = &parent->cs_mask;
291 if (!CPU_OVERLAP(&parent->cs_mask, mask))
293 /* The domain must be prepared ahead of time. */
294 if (!domainset_valid(parent->cs_domain, domain))
296 CPU_COPY(mask, &set->cs_mask);
297 LIST_INIT(&set->cs_children);
298 refcount_init(&set->cs_ref, 1);
300 mtx_lock_spin(&cpuset_lock);
301 set->cs_domain = domain;
302 CPU_AND(&set->cs_mask, &parent->cs_mask);
304 set->cs_parent = cpuset_ref(parent);
305 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
306 if (set->cs_id != CPUSET_INVALID)
307 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
308 mtx_unlock_spin(&cpuset_lock);
314 * Create a new non-anonymous set with the requested parent and mask. May
315 * return failures if the mask is invalid or a new number can not be
319 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
325 id = alloc_unr(cpuset_unr);
328 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
329 error = _cpuset_create(set, parent, mask, NULL, id);
332 free_unr(cpuset_unr, id);
333 uma_zfree(cpuset_zone, set);
339 cpuset_freelist_add(struct setlist *list, int count)
344 for (i = 0; i < count; i++) {
345 set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
346 LIST_INSERT_HEAD(list, set, cs_link);
351 cpuset_freelist_init(struct setlist *list, int count)
355 cpuset_freelist_add(list, count);
359 cpuset_freelist_free(struct setlist *list)
363 while ((set = LIST_FIRST(list)) != NULL) {
364 LIST_REMOVE(set, cs_link);
365 uma_zfree(cpuset_zone, set);
370 domainset_freelist_add(struct domainlist *list, int count)
372 struct domainset *set;
375 for (i = 0; i < count; i++) {
376 set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
377 LIST_INSERT_HEAD(list, set, ds_link);
382 domainset_freelist_init(struct domainlist *list, int count)
386 domainset_freelist_add(list, count);
390 domainset_freelist_free(struct domainlist *list)
392 struct domainset *set;
394 while ((set = LIST_FIRST(list)) != NULL) {
395 LIST_REMOVE(set, ds_link);
396 uma_zfree(domainset_zone, set);
400 /* Copy a domainset preserving mask and policy. */
402 domainset_copy(const struct domainset *from, struct domainset *to)
405 DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
406 to->ds_policy = from->ds_policy;
407 to->ds_prefer = from->ds_prefer;
410 /* Return 1 if mask and policy are equal, otherwise 0. */
412 domainset_equal(const struct domainset *one, const struct domainset *two)
415 return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
416 one->ds_policy == two->ds_policy &&
417 one->ds_prefer == two->ds_prefer);
420 /* Return 1 if child is a valid subset of parent. */
422 domainset_valid(const struct domainset *parent, const struct domainset *child)
424 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
425 return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
426 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
430 domainset_restrict(const struct domainset *parent,
431 const struct domainset *child)
433 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
434 return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
435 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
439 * Lookup or create a domainset. The key is provided in ds_mask and
440 * ds_policy. If the domainset does not yet exist the storage in
441 * 'domain' is used to insert. Otherwise this storage is freed to the
442 * domainset_zone and the existing domainset is returned.
444 static struct domainset *
445 _domainset_create(struct domainset *domain, struct domainlist *freelist)
447 struct domainset *ndomain;
449 mtx_lock_spin(&cpuset_lock);
450 LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
451 if (domainset_equal(ndomain, domain))
454 * If the domain does not yet exist we insert it and initialize
455 * various iteration helpers which are not part of the key.
457 if (ndomain == NULL) {
458 LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
459 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
460 domain->ds_max = DOMAINSET_FLS(&domain->ds_mask) + 1;
462 mtx_unlock_spin(&cpuset_lock);
465 if (freelist != NULL)
466 LIST_INSERT_HEAD(freelist, domain, ds_link);
468 uma_zfree(domainset_zone, domain);
474 * Create or lookup a domainset based on the key held in 'domain'.
476 static struct domainset *
477 domainset_create(const struct domainset *domain)
479 struct domainset *ndomain;
481 ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
482 domainset_copy(domain, ndomain);
483 return _domainset_create(ndomain, NULL);
487 * Update thread domainset pointers.
490 domainset_notify(void)
495 sx_slock(&allproc_lock);
496 FOREACH_PROC_IN_SYSTEM(p) {
498 if (p->p_state == PRS_NEW) {
502 FOREACH_THREAD_IN_PROC(p, td) {
504 td->td_domain.dr_policy = td->td_cpuset->cs_domain;
509 sx_sunlock(&allproc_lock);
510 kernel_object->domain.dr_policy = cpuset_default->cs_domain;
514 * Create a new set that is a subset of a parent.
516 static struct domainset *
517 domainset_shadow(const struct domainset *pdomain,
518 const struct domainset *domain, struct domainlist *freelist)
520 struct domainset *ndomain;
522 ndomain = LIST_FIRST(freelist);
523 LIST_REMOVE(ndomain, ds_link);
526 * Initialize the key from the request.
528 domainset_copy(domain, ndomain);
531 * Restrict the key by the parent.
533 DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
535 return _domainset_create(ndomain, freelist);
539 * Recursively check for errors that would occur from applying mask to
540 * the tree of sets starting at 'set'. Checks for sets that would become
541 * empty as well as RDONLY flags.
544 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
550 mtx_assert(&cpuset_lock, MA_OWNED);
551 if (set->cs_flags & CPU_SET_RDONLY)
554 if (!CPU_OVERLAP(&set->cs_mask, mask))
556 CPU_COPY(&set->cs_mask, &newmask);
557 CPU_AND(&newmask, mask);
559 CPU_COPY(mask, &newmask);
561 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
562 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
568 * Applies the mask 'mask' without checking for empty sets or permissions.
571 cpuset_update(struct cpuset *set, cpuset_t *mask)
575 mtx_assert(&cpuset_lock, MA_OWNED);
576 CPU_AND(&set->cs_mask, mask);
577 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
578 cpuset_update(nset, &set->cs_mask);
584 * Modify the set 'set' to use a copy of the mask provided. Apply this new
585 * mask to restrict all children in the tree. Checks for validity before
586 * applying the changes.
589 cpuset_modify(struct cpuset *set, cpuset_t *mask)
594 error = priv_check(curthread, PRIV_SCHED_CPUSET);
598 * In case we are called from within the jail
599 * we do not allow modifying the dedicated root
600 * cpuset of the jail but may still allow to
603 if (jailed(curthread->td_ucred) &&
604 set->cs_flags & CPU_SET_ROOT)
607 * Verify that we have access to this set of
610 root = cpuset_getroot(set);
611 mtx_lock_spin(&cpuset_lock);
612 if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
616 error = cpuset_testupdate(set, mask, 0);
619 CPU_COPY(mask, &set->cs_mask);
620 cpuset_update(set, mask);
622 mtx_unlock_spin(&cpuset_lock);
628 * Recursively check for errors that would occur from applying mask to
629 * the tree of sets starting at 'set'. Checks for sets that would become
630 * empty as well as RDONLY flags.
633 cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
634 struct domainset *orig, int *count, int check_mask)
637 struct domainset *domain;
638 struct domainset newset;
641 mtx_assert(&cpuset_lock, MA_OWNED);
642 if (set->cs_flags & CPU_SET_RDONLY)
644 domain = set->cs_domain;
645 domainset_copy(domain, &newset);
646 if (!domainset_equal(domain, orig)) {
647 if (!domainset_restrict(domain, dset))
649 DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
650 /* Count the number of domains that are changing. */
654 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
655 if ((error = cpuset_testupdate_domain(nset, &newset, domain,
662 * Applies the mask 'mask' without checking for empty sets or permissions.
665 cpuset_update_domain(struct cpuset *set, struct domainset *domain,
666 struct domainset *orig, struct domainlist *domains)
670 mtx_assert(&cpuset_lock, MA_OWNED);
672 * If this domainset has changed from the parent we must calculate
673 * a new set. Otherwise it simply inherits from the parent. When
674 * we inherit from the parent we get a new mask and policy. If the
675 * set is modified from the parent we keep the policy and only
678 if (set->cs_domain != orig) {
679 orig = set->cs_domain;
680 set->cs_domain = domainset_shadow(domain, orig, domains);
682 set->cs_domain = domain;
683 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
684 cpuset_update_domain(nset, set->cs_domain, orig, domains);
690 * Modify the set 'set' to use a copy the domainset provided. Apply this new
691 * mask to restrict all children in the tree. Checks for validity before
692 * applying the changes.
695 cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
697 struct domainlist domains;
698 struct domainset temp;
699 struct domainset *dset;
701 int ndomains, needed;
704 error = priv_check(curthread, PRIV_SCHED_CPUSET);
708 * In case we are called from within the jail
709 * we do not allow modifying the dedicated root
710 * cpuset of the jail but may still allow to
713 if (jailed(curthread->td_ucred) &&
714 set->cs_flags & CPU_SET_ROOT)
716 domainset_freelist_init(&domains, 0);
717 domain = domainset_create(domain);
718 ndomains = needed = 0;
720 if (ndomains < needed) {
721 domainset_freelist_add(&domains, needed - ndomains);
724 root = cpuset_getroot(set);
725 mtx_lock_spin(&cpuset_lock);
726 dset = root->cs_domain;
728 * Verify that we have access to this set of domains.
730 if (root && !domainset_valid(dset, domain)) {
735 * If applying prefer we keep the current set as the fallback.
737 if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
738 DOMAINSET_COPY(&set->cs_domain->ds_mask,
741 * Determine whether we can apply this set of domains and
742 * how many new domain structures it will require.
744 domainset_copy(domain, &temp);
746 error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
750 } while (ndomains < needed);
751 dset = set->cs_domain;
752 cpuset_update_domain(set, domain, dset, &domains);
754 mtx_unlock_spin(&cpuset_lock);
755 domainset_freelist_free(&domains);
763 * Resolve the 'which' parameter of several cpuset apis.
765 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
766 * checks for permission via p_cansched().
768 * For WHICH_SET returns a valid set with a new reference.
770 * -1 may be supplied for any argument to mean the current proc/thread or
771 * the base set of the current thread. May fail with ESRCH/EPERM.
774 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
775 struct cpuset **setp)
792 if ((p = pfind(id)) == NULL)
807 case CPU_WHICH_CPUSET:
809 thread_lock(curthread);
810 set = cpuset_refbase(curthread->td_cpuset);
811 thread_unlock(curthread);
813 set = cpuset_lookup(id, curthread);
821 /* Find `set' for prison with given id. */
824 sx_slock(&allprison_lock);
825 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
826 sx_sunlock(&allprison_lock);
829 cpuset_ref(pr->pr_cpuset);
830 *setp = pr->pr_cpuset;
831 mtx_unlock(&pr->pr_mtx);
835 case CPU_WHICH_DOMAIN:
840 error = p_cansched(curthread, p);
846 td = FIRST_THREAD_IN_PROC(p);
853 cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
854 const struct domainset *domain)
856 struct cpuset *parent;
857 struct domainset *dset;
859 parent = cpuset_getbase(set);
861 * If we are restricting a cpu mask it must be a subset of the
862 * parent or invalid CPUs have been specified.
864 if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
868 * If we are restricting a domain mask it must be a subset of the
869 * parent or invalid domains have been specified.
871 dset = parent->cs_domain;
872 if (domain != NULL && !domainset_valid(dset, domain))
879 * Create an anonymous set with the provided mask in the space provided by
880 * 'nset'. If the passed in set is anonymous we use its parent otherwise
881 * the new set is a child of 'set'.
884 cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
885 const cpuset_t *mask, const struct domainset *domain,
886 struct setlist *cpusets, struct domainlist *domains)
888 struct cpuset *parent;
890 struct domainset *dset;
894 error = cpuset_testshadow(set, mask, domain);
898 parent = cpuset_getbase(set);
899 dset = parent->cs_domain;
901 mask = &set->cs_mask;
903 d = domainset_shadow(dset, domain, domains);
906 nset = LIST_FIRST(cpusets);
907 error = _cpuset_create(nset, parent, mask, d, CPUSET_INVALID);
909 LIST_REMOVE(nset, cs_link);
915 static struct cpuset *
916 cpuset_update_thread(struct thread *td, struct cpuset *nset)
918 struct cpuset *tdset;
920 tdset = td->td_cpuset;
921 td->td_cpuset = nset;
922 td->td_domain.dr_policy = nset->cs_domain;
929 cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
930 struct domainset *domain)
932 struct cpuset *parent;
934 parent = cpuset_getbase(tdset);
936 mask = &tdset->cs_mask;
938 domain = tdset->cs_domain;
939 return cpuset_testshadow(parent, mask, domain);
943 cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
944 struct domainset *domain, struct cpuset **nsetp,
945 struct setlist *freelist, struct domainlist *domainlist)
947 struct cpuset *parent;
949 parent = cpuset_getbase(tdset);
951 mask = &tdset->cs_mask;
953 domain = tdset->cs_domain;
954 return cpuset_shadow(parent, nsetp, mask, domain, freelist,
959 cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
960 cpuset_t *mask, struct domainset *domain)
962 struct cpuset *parent;
964 parent = cpuset_getbase(tdset);
967 * If the thread restricted its mask then apply that same
968 * restriction to the new set, otherwise take it wholesale.
970 if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
971 CPU_COPY(&tdset->cs_mask, mask);
972 CPU_AND(mask, &set->cs_mask);
974 CPU_COPY(&set->cs_mask, mask);
977 * If the thread restricted the domain then we apply the
978 * restriction to the new set but retain the policy.
980 if (tdset->cs_domain != parent->cs_domain) {
981 domainset_copy(tdset->cs_domain, domain);
982 DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
984 domainset_copy(set->cs_domain, domain);
986 if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
993 cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
995 struct domainset domain;
998 if (tdset->cs_id != CPUSET_INVALID)
1000 return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1004 cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
1005 struct cpuset **nsetp, struct setlist *freelist,
1006 struct domainlist *domainlist)
1008 struct domainset domain;
1013 * If we're replacing on a thread that has not constrained the
1014 * original set we can simply accept the new set.
1016 if (tdset->cs_id != CPUSET_INVALID) {
1017 *nsetp = cpuset_ref(set);
1020 error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1024 return cpuset_shadow(tdset, nsetp, &mask, &domain, freelist,
1029 * Handle three cases for updating an entire process.
1031 * 1) Set is non-null. This reparents all anonymous sets to the provided
1032 * set and replaces all non-anonymous td_cpusets with the provided set.
1033 * 2) Mask is non-null. This replaces or creates anonymous sets for every
1034 * thread with the existing base as a parent.
1035 * 3) domain is non-null. This creates anonymous sets for every thread
1036 * and replaces the domain set.
1038 * This is overly complicated because we can't allocate while holding a
1039 * spinlock and spinlocks must be held while changing and examining thread
1043 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1044 struct domainset *domain)
1046 struct setlist freelist;
1047 struct setlist droplist;
1048 struct domainlist domainlist;
1049 struct cpuset *nset;
1057 * The algorithm requires two passes due to locking considerations.
1059 * 1) Lookup the process and acquire the locks in the required order.
1060 * 2) If enough cpusets have not been allocated release the locks and
1061 * allocate them. Loop.
1063 cpuset_freelist_init(&freelist, 1);
1064 domainset_freelist_init(&domainlist, 1);
1066 LIST_INIT(&droplist);
1069 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1072 if (nfree >= p->p_numthreads)
1074 threads = p->p_numthreads;
1076 if (nfree < threads) {
1077 cpuset_freelist_add(&freelist, threads - nfree);
1078 domainset_freelist_add(&domainlist, threads - nfree);
1082 PROC_LOCK_ASSERT(p, MA_OWNED);
1084 * Now that the appropriate locks are held and we have enough cpusets,
1085 * make sure the operation will succeed before applying changes. The
1086 * proc lock prevents td_cpuset from changing between calls.
1089 FOREACH_THREAD_IN_PROC(p, td) {
1092 error = cpuset_setproc_test_setthread(td->td_cpuset,
1095 error = cpuset_setproc_test_maskthread(td->td_cpuset,
1102 * Replace each thread's cpuset while using deferred release. We
1103 * must do this because the thread lock must be held while operating
1104 * on the thread and this limits the type of operations allowed.
1106 FOREACH_THREAD_IN_PROC(p, td) {
1109 error = cpuset_setproc_setthread(td->td_cpuset, set,
1110 &nset, &freelist, &domainlist);
1112 error = cpuset_setproc_maskthread(td->td_cpuset, mask,
1113 domain, &nset, &freelist, &domainlist);
1118 cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1124 while ((nset = LIST_FIRST(&droplist)) != NULL)
1125 cpuset_rel_complete(nset);
1126 cpuset_freelist_free(&freelist);
1127 domainset_freelist_free(&domainlist);
1132 * Return a string representing a valid layout for a cpuset_t object.
1133 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1136 cpusetobj_strprint(char *buf, const cpuset_t *set)
1139 size_t i, bytesp, bufsiz;
1143 bufsiz = CPUSETBUFSIZ;
1145 for (i = 0; i < (_NCPUWORDS - 1); i++) {
1146 bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
1150 snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
1155 * Build a valid cpuset_t object from a string representation.
1156 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1159 cpusetobj_strscan(cpuset_t *set, const char *buf)
1164 if (strlen(buf) > CPUSETBUFSIZ - 1)
1167 /* Allow to pass a shorter version of the mask when necessary. */
1169 for (i = 0; buf[i] != '\0'; i++)
1172 if (nwords > _NCPUWORDS)
1176 for (i = 0; i < (nwords - 1); i++) {
1177 ret = sscanf(buf, "%lx,", &set->__bits[i]);
1178 if (ret == 0 || ret == -1)
1180 buf = strstr(buf, ",");
1185 ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
1186 if (ret == 0 || ret == -1)
1192 * Apply an anonymous mask or a domain to a single thread.
1195 _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1197 struct setlist cpusets;
1198 struct domainlist domainlist;
1199 struct cpuset *nset;
1205 cpuset_freelist_init(&cpusets, 1);
1206 domainset_freelist_init(&domainlist, domain != NULL);
1207 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1212 error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
1213 &cpusets, &domainlist);
1215 set = cpuset_update_thread(td, nset);
1221 cpuset_freelist_free(&cpusets);
1222 domainset_freelist_free(&domainlist);
1227 * Apply an anonymous mask to a single thread.
1230 cpuset_setthread(lwpid_t id, cpuset_t *mask)
1233 return _cpuset_setthread(id, mask, NULL);
1237 * Apply new cpumask to the ithread.
1240 cpuset_setithread(lwpid_t id, int cpu)
1242 struct setlist cpusets;
1243 struct cpuset *nset, *rset;
1244 struct cpuset *parent, *old_set;
1251 cpuset_freelist_init(&cpusets, 1);
1252 rset = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1253 cs_id = CPUSET_INVALID;
1257 CPU_COPY(cpuset_root, &mask);
1259 CPU_SET(cpu, &mask);
1261 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set);
1262 if (error != 0 || ((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID))
1265 /* cpuset_which() returns with PROC_LOCK held. */
1266 old_set = td->td_cpuset;
1269 nset = LIST_FIRST(&cpusets);
1270 LIST_REMOVE(nset, cs_link);
1273 * roll back to default set. We're not using cpuset_shadow()
1274 * here because we can fail CPU_SUBSET() check. This can happen
1275 * if default set does not contain all CPUs.
1277 error = _cpuset_create(nset, cpuset_default, &mask, NULL,
1283 if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID &&
1284 old_set->cs_parent->cs_id == 1)) {
1287 * Current set is either default (1) or
1288 * shadowed version of default set.
1290 * Allocate new root set to be able to shadow it
1293 error = _cpuset_create(rset, cpuset_zero,
1294 &cpuset_zero->cs_mask, NULL, cs_id);
1299 rset->cs_flags |= CPU_SET_ROOT;
1302 cs_id = CPUSET_INVALID;
1304 /* Assume existing set was already allocated by previous call */
1309 error = cpuset_shadow(parent, &nset, &mask, NULL, &cpusets, NULL);
1313 old_set = cpuset_update_thread(td, nset);
1318 if (old_set != NULL)
1319 cpuset_rel(old_set);
1321 cpuset_freelist_free(&cpusets);
1323 uma_zfree(cpuset_zone, rset);
1324 if (cs_id != CPUSET_INVALID)
1325 free_unr(cpuset_unr, cs_id);
1329 static struct domainset domainset0;
1332 domainset_zero(void)
1334 struct domainset *dset;
1337 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
1340 DOMAINSET_ZERO(&dset->ds_mask);
1341 for (i = 0; i < vm_ndomains; i++)
1342 DOMAINSET_SET(i, &dset->ds_mask);
1343 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1344 dset->ds_prefer = -1;
1345 curthread->td_domain.dr_policy = _domainset_create(dset, NULL);
1346 kernel_object->domain.dr_policy = curthread->td_domain.dr_policy;
1350 * Creates system-wide cpusets and the cpuset for thread0 including two
1353 * 0 - The root set which should represent all valid processors in the
1354 * system. It is initially created with a mask of all processors
1355 * because we don't know what processors are valid until cpuset_init()
1356 * runs. This set is immutable.
1357 * 1 - The default set which all processes are a member of until changed.
1358 * This allows an administrator to move all threads off of given cpus to
1359 * dedicate them to high priority tasks or save power etc.
1362 cpuset_thread0(void)
1367 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1368 NULL, NULL, UMA_ALIGN_PTR, 0);
1369 domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1370 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1373 * Create the root system set for the whole machine. Doesn't use
1374 * cpuset_create() due to NULL parent.
1376 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1377 CPU_FILL(&set->cs_mask);
1378 LIST_INIT(&set->cs_children);
1379 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
1381 set->cs_flags = CPU_SET_ROOT;
1382 set->cs_domain = &domainset0;
1384 cpuset_root = &set->cs_mask;
1387 * Now derive a default, modifiable set from that to give out.
1389 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1390 error = _cpuset_create(set, cpuset_zero, NULL, NULL, 1);
1391 KASSERT(error == 0, ("Error creating default set: %d\n", error));
1392 cpuset_default = set;
1395 * Initialize the unit allocator. 0 and 1 are allocated above.
1397 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
1403 * Create a cpuset, which would be cpuset_create() but
1404 * mark the new 'set' as root.
1406 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
1409 * In case of no error, returns the set in *setp locked with a reference.
1412 cpuset_create_root(struct prison *pr, struct cpuset **setp)
1417 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1418 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1420 error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1424 KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
1425 __func__, __LINE__));
1427 /* Mark the set as root. */
1429 set->cs_flags |= CPU_SET_ROOT;
1435 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1439 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1440 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1443 error = cpuset_setproc(p->p_pid, set, NULL, NULL);
1451 * This is called once the final set of system cpus is known. Modifies
1452 * the root set and all children and mark the root read-only.
1455 cpuset_init(void *arg)
1461 if (cpuset_modify(cpuset_zero, &mask))
1462 panic("Can't set initial cpuset mask.\n");
1463 cpuset_zero->cs_flags |= CPU_SET_RDONLY;
1466 * If MD code has not initialized per-domain cpusets, place all
1469 for (i = 0; i < MAXMEMDOM; i++)
1470 if (!CPU_EMPTY(&cpuset_domain[i]))
1472 CPU_COPY(&all_cpus, &cpuset_domain[0]);
1476 SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
1478 #ifndef _SYS_SYSPROTO_H_
1479 struct cpuset_args {
1484 sys_cpuset(struct thread *td, struct cpuset_args *uap)
1486 struct cpuset *root;
1491 root = cpuset_refroot(td->td_cpuset);
1493 error = cpuset_create(&set, root, &root->cs_mask);
1497 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1499 error = cpuset_setproc(-1, set, NULL, NULL);
1504 #ifndef _SYS_SYSPROTO_H_
1505 struct cpuset_setid_args {
1512 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1515 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1519 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1520 id_t id, cpusetid_t setid)
1526 * Presently we only support per-process sets.
1528 if (which != CPU_WHICH_PID)
1530 set = cpuset_lookup(setid, td);
1533 error = cpuset_setproc(id, set, NULL, NULL);
1538 #ifndef _SYS_SYSPROTO_H_
1539 struct cpuset_getid_args {
1547 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1550 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1555 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1556 id_t id, cpusetid_t *setid)
1558 struct cpuset *nset;
1565 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1567 error = cpuset_which(which, id, &p, &ttd, &set);
1574 set = cpuset_refbase(ttd->td_cpuset);
1578 case CPU_WHICH_CPUSET:
1579 case CPU_WHICH_JAIL:
1582 case CPU_WHICH_DOMAIN:
1586 case CPU_LEVEL_ROOT:
1587 nset = cpuset_refroot(set);
1591 case CPU_LEVEL_CPUSET:
1593 case CPU_LEVEL_WHICH:
1599 error = copyout(&tmpid, setid, sizeof(tmpid));
1604 #ifndef _SYS_SYSPROTO_H_
1605 struct cpuset_getaffinity_args {
1614 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1617 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
1618 uap->id, uap->cpusetsize, uap->mask));
1622 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1623 id_t id, size_t cpusetsize, cpuset_t *maskp)
1626 struct cpuset *nset;
1633 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1635 /* In Capability mode, you can only get your own CPU set. */
1636 if (IN_CAPABILITY_MODE(td)) {
1637 if (level != CPU_LEVEL_WHICH)
1639 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1645 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1646 error = cpuset_which(which, id, &p, &ttd, &set);
1650 case CPU_LEVEL_ROOT:
1651 case CPU_LEVEL_CPUSET:
1656 set = cpuset_ref(ttd->td_cpuset);
1659 case CPU_WHICH_CPUSET:
1660 case CPU_WHICH_JAIL:
1663 case CPU_WHICH_INTRHANDLER:
1664 case CPU_WHICH_ITHREAD:
1665 case CPU_WHICH_DOMAIN:
1669 if (level == CPU_LEVEL_ROOT)
1670 nset = cpuset_refroot(set);
1672 nset = cpuset_refbase(set);
1673 CPU_COPY(&nset->cs_mask, mask);
1676 case CPU_LEVEL_WHICH:
1680 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1684 FOREACH_THREAD_IN_PROC(p, ttd) {
1686 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1690 case CPU_WHICH_CPUSET:
1691 case CPU_WHICH_JAIL:
1692 CPU_COPY(&set->cs_mask, mask);
1695 case CPU_WHICH_INTRHANDLER:
1696 case CPU_WHICH_ITHREAD:
1697 error = intr_getaffinity(id, which, mask);
1699 case CPU_WHICH_DOMAIN:
1700 if (id < 0 || id >= MAXMEMDOM)
1703 CPU_COPY(&cpuset_domain[id], mask);
1716 error = copyout(mask, maskp, size);
1722 #ifndef _SYS_SYSPROTO_H_
1723 struct cpuset_setaffinity_args {
1728 const cpuset_t *mask;
1732 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1735 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
1736 uap->id, uap->cpusetsize, uap->mask));
1740 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1741 id_t id, size_t cpusetsize, const cpuset_t *maskp)
1743 struct cpuset *nset;
1750 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1752 /* In Capability mode, you can only set your own CPU set. */
1753 if (IN_CAPABILITY_MODE(td)) {
1754 if (level != CPU_LEVEL_WHICH)
1756 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1761 mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1762 error = copyin(maskp, mask, cpusetsize);
1766 * Verify that no high bits are set.
1768 if (cpusetsize > sizeof(cpuset_t)) {
1772 end = cp = (char *)&mask->__bits;
1774 cp += sizeof(cpuset_t);
1783 case CPU_LEVEL_ROOT:
1784 case CPU_LEVEL_CPUSET:
1785 error = cpuset_which(which, id, &p, &ttd, &set);
1792 set = cpuset_ref(ttd->td_cpuset);
1796 case CPU_WHICH_CPUSET:
1797 case CPU_WHICH_JAIL:
1800 case CPU_WHICH_INTRHANDLER:
1801 case CPU_WHICH_ITHREAD:
1802 case CPU_WHICH_DOMAIN:
1806 if (level == CPU_LEVEL_ROOT)
1807 nset = cpuset_refroot(set);
1809 nset = cpuset_refbase(set);
1810 error = cpuset_modify(nset, mask);
1814 case CPU_LEVEL_WHICH:
1817 error = cpuset_setthread(id, mask);
1820 error = cpuset_setproc(id, NULL, mask, NULL);
1822 case CPU_WHICH_CPUSET:
1823 case CPU_WHICH_JAIL:
1824 error = cpuset_which(which, id, &p, &ttd, &set);
1826 error = cpuset_modify(set, mask);
1831 case CPU_WHICH_INTRHANDLER:
1832 case CPU_WHICH_ITHREAD:
1833 error = intr_setaffinity(id, which, mask);
1849 #ifndef _SYS_SYSPROTO_H_
1850 struct cpuset_getdomain_args {
1854 size_t domainsetsize;
1860 sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
1863 return (kern_cpuset_getdomain(td, uap->level, uap->which,
1864 uap->id, uap->domainsetsize, uap->mask, uap->policy));
1868 kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
1869 id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp)
1871 struct domainset outset;
1873 struct cpuset *nset;
1875 struct domainset *dset;
1880 if (domainsetsize < sizeof(domainset_t) ||
1881 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
1883 /* In Capability mode, you can only get your own domain set. */
1884 if (IN_CAPABILITY_MODE(td)) {
1885 if (level != CPU_LEVEL_WHICH)
1887 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1892 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
1893 bzero(&outset, sizeof(outset));
1894 error = cpuset_which(which, id, &p, &ttd, &set);
1898 case CPU_LEVEL_ROOT:
1899 case CPU_LEVEL_CPUSET:
1904 set = cpuset_ref(ttd->td_cpuset);
1907 case CPU_WHICH_CPUSET:
1908 case CPU_WHICH_JAIL:
1911 case CPU_WHICH_INTRHANDLER:
1912 case CPU_WHICH_ITHREAD:
1913 case CPU_WHICH_DOMAIN:
1917 if (level == CPU_LEVEL_ROOT)
1918 nset = cpuset_refroot(set);
1920 nset = cpuset_refbase(set);
1921 domainset_copy(nset->cs_domain, &outset);
1924 case CPU_LEVEL_WHICH:
1928 domainset_copy(ttd->td_cpuset->cs_domain, &outset);
1932 FOREACH_THREAD_IN_PROC(p, ttd) {
1934 dset = ttd->td_cpuset->cs_domain;
1935 /* Show all domains in the proc. */
1936 DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
1937 /* Last policy wins. */
1938 outset.ds_policy = dset->ds_policy;
1939 outset.ds_prefer = dset->ds_prefer;
1943 case CPU_WHICH_CPUSET:
1944 case CPU_WHICH_JAIL:
1945 domainset_copy(set->cs_domain, &outset);
1948 case CPU_WHICH_INTRHANDLER:
1949 case CPU_WHICH_ITHREAD:
1950 case CPU_WHICH_DOMAIN:
1964 * Translate prefer into a set containing only the preferred domain,
1965 * not the entire fallback set.
1967 if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
1968 DOMAINSET_ZERO(&outset.ds_mask);
1969 DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
1971 DOMAINSET_COPY(&outset.ds_mask, mask);
1973 error = copyout(mask, maskp, domainsetsize);
1975 if (suword32(policyp, outset.ds_policy) != 0)
1982 #ifndef _SYS_SYSPROTO_H_
1983 struct cpuset_setdomain_args {
1987 size_t domainsetsize;
1993 sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
1996 return (kern_cpuset_setdomain(td, uap->level, uap->which,
1997 uap->id, uap->domainsetsize, uap->mask, uap->policy));
2001 kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2002 id_t id, size_t domainsetsize, const domainset_t *maskp, int policy)
2004 struct cpuset *nset;
2008 struct domainset domain;
2012 if (domainsetsize < sizeof(domainset_t) ||
2013 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2015 /* In Capability mode, you can only set your own CPU set. */
2016 if (IN_CAPABILITY_MODE(td)) {
2017 if (level != CPU_LEVEL_WHICH)
2019 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
2024 memset(&domain, 0, sizeof(domain));
2025 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2026 error = copyin(maskp, mask, domainsetsize);
2030 * Verify that no high bits are set.
2032 if (domainsetsize > sizeof(domainset_t)) {
2036 end = cp = (char *)&mask->__bits;
2037 end += domainsetsize;
2038 cp += sizeof(domainset_t);
2046 DOMAINSET_COPY(mask, &domain.ds_mask);
2047 domain.ds_policy = policy;
2048 if (policy <= DOMAINSET_POLICY_INVALID ||
2049 policy > DOMAINSET_POLICY_MAX)
2052 /* Translate preferred policy into a mask and fallback. */
2053 if (policy == DOMAINSET_POLICY_PREFER) {
2054 /* Only support a single preferred domain. */
2055 if (DOMAINSET_COUNT(&domain.ds_mask) != 1)
2057 domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
2058 /* This will be constrained by domainset_shadow(). */
2059 DOMAINSET_FILL(&domain.ds_mask);
2063 case CPU_LEVEL_ROOT:
2064 case CPU_LEVEL_CPUSET:
2065 error = cpuset_which(which, id, &p, &ttd, &set);
2072 set = cpuset_ref(ttd->td_cpuset);
2076 case CPU_WHICH_CPUSET:
2077 case CPU_WHICH_JAIL:
2080 case CPU_WHICH_INTRHANDLER:
2081 case CPU_WHICH_ITHREAD:
2082 case CPU_WHICH_DOMAIN:
2086 if (level == CPU_LEVEL_ROOT)
2087 nset = cpuset_refroot(set);
2089 nset = cpuset_refbase(set);
2090 error = cpuset_modify_domain(nset, &domain);
2094 case CPU_LEVEL_WHICH:
2097 error = _cpuset_setthread(id, NULL, &domain);
2100 error = cpuset_setproc(id, NULL, NULL, &domain);
2102 case CPU_WHICH_CPUSET:
2103 case CPU_WHICH_JAIL:
2104 error = cpuset_which(which, id, &p, &ttd, &set);
2106 error = cpuset_modify_domain(set, &domain);
2111 case CPU_WHICH_INTRHANDLER:
2112 case CPU_WHICH_ITHREAD:
2128 BITSET_DEFINE(bitset, 1);
2130 ddb_display_bitset(const struct bitset *set, int size)
2134 for (once = 0, bit = 0; bit < size; bit++) {
2135 if (CPU_ISSET(bit, set)) {
2137 db_printf("%d", bit);
2140 db_printf(",%d", bit);
2144 db_printf("<none>");
2148 ddb_display_cpuset(const cpuset_t *set)
2150 ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
2154 ddb_display_domainset(const domainset_t *set)
2156 ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
2159 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
2163 LIST_FOREACH(set, &cpuset_ids, cs_link) {
2164 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
2165 set, set->cs_id, set->cs_ref, set->cs_flags,
2166 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
2167 db_printf(" cpu mask=");
2168 ddb_display_cpuset(&set->cs_mask);
2170 db_printf(" domain policy %d prefer %d mask=",
2171 set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
2172 ddb_display_domainset(&set->cs_domain->ds_mask);
2179 DB_SHOW_COMMAND(domainsets, db_show_domainsets)
2181 struct domainset *set;
2183 LIST_FOREACH(set, &cpuset_domains, ds_link) {
2184 db_printf("set=%p policy %d prefer %d cnt %d max %d\n",
2185 set, set->ds_policy, set->ds_prefer, set->ds_cnt,
2187 db_printf(" mask =");
2188 ddb_display_domainset(&set->ds_mask);