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_page.h>
69 #include <vm/vm_pageout.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_param.h>
72 #include <vm/vm_phys.h>
73 #include <vm/vm_pagequeue.h>
80 * cpusets provide a mechanism for creating and manipulating sets of
81 * processors for the purpose of constraining the scheduling of threads to
82 * specific processors.
84 * Each process belongs to an identified set, by default this is set 1. Each
85 * thread may further restrict the cpus it may run on to a subset of this
86 * named set. This creates an anonymous set which other threads and processes
87 * may not join by number.
89 * The named set is referred to herein as the 'base' set to avoid ambiguity.
90 * This set is usually a child of a 'root' set while the anonymous set may
91 * simply be referred to as a mask. In the syscall api these are referred to
92 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
94 * Threads inherit their set from their creator whether it be anonymous or
95 * not. This means that anonymous sets are immutable because they may be
96 * shared. To modify an anonymous set a new set is created with the desired
97 * mask and the same parent as the existing anonymous set. This gives the
98 * illusion of each thread having a private mask.
100 * Via the syscall apis a user may ask to retrieve or modify the root, base,
101 * or mask that is discovered via a pid, tid, or setid. Modifying a set
102 * modifies all numbered and anonymous child sets to comply with the new mask.
103 * Modifying a pid or tid's mask applies only to that tid but must still
104 * exist within the assigned parent set.
106 * A thread may not be assigned to a group separate from other threads in
107 * the process. This is to remove ambiguity when the setid is queried with
108 * a pid argument. There is no other technical limitation.
110 * This somewhat complex arrangement is intended to make it easy for
111 * applications to query available processors and bind their threads to
112 * specific processors while also allowing administrators to dynamically
113 * reprovision by changing sets which apply to groups of processes.
115 * A simple application should not concern itself with sets at all and
116 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
117 * meaning 'curthread'. It may query available cpus for that tid with a
118 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
121 LIST_HEAD(domainlist, domainset);
122 struct domainset __read_mostly domainset_fixed[MAXMEMDOM];
123 struct domainset __read_mostly domainset_prefer[MAXMEMDOM];
124 struct domainset __read_mostly domainset_roundrobin;
126 static uma_zone_t cpuset_zone;
127 static uma_zone_t domainset_zone;
128 static struct mtx cpuset_lock;
129 static struct setlist cpuset_ids;
130 static struct domainlist cpuset_domains;
131 static struct unrhdr *cpuset_unr;
132 static struct cpuset *cpuset_zero, *cpuset_default, *cpuset_kernel;
133 static struct domainset domainset0, domainset2;
135 /* Return the size of cpuset_t at the kernel level */
136 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
137 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
139 cpuset_t *cpuset_root;
140 cpuset_t cpuset_domain[MAXMEMDOM];
142 static int domainset_valid(const struct domainset *, const struct domainset *);
145 * Find the first non-anonymous set starting from 'set'.
147 static struct cpuset *
148 cpuset_getbase(struct cpuset *set)
151 if (set->cs_id == CPUSET_INVALID)
152 set = set->cs_parent;
157 * Walks up the tree from 'set' to find the root.
159 static struct cpuset *
160 cpuset_getroot(struct cpuset *set)
163 while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL)
164 set = set->cs_parent;
169 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
172 cpuset_ref(struct cpuset *set)
175 refcount_acquire(&set->cs_ref);
180 * Walks up the tree from 'set' to find the root. Returns the root
183 static struct cpuset *
184 cpuset_refroot(struct cpuset *set)
187 return (cpuset_ref(cpuset_getroot(set)));
191 * Find the first non-anonymous set starting from 'set'. Returns this set
192 * referenced. May return the passed in set with an extra ref if it is
195 static struct cpuset *
196 cpuset_refbase(struct cpuset *set)
199 return (cpuset_ref(cpuset_getbase(set)));
203 * Release a reference in a context where it is safe to allocate.
206 cpuset_rel(struct cpuset *set)
210 if (refcount_release(&set->cs_ref) == 0)
212 mtx_lock_spin(&cpuset_lock);
213 LIST_REMOVE(set, cs_siblings);
215 if (id != CPUSET_INVALID)
216 LIST_REMOVE(set, cs_link);
217 mtx_unlock_spin(&cpuset_lock);
218 cpuset_rel(set->cs_parent);
219 uma_zfree(cpuset_zone, set);
220 if (id != CPUSET_INVALID)
221 free_unr(cpuset_unr, id);
225 * Deferred release must be used when in a context that is not safe to
226 * allocate/free. This places any unreferenced sets on the list 'head'.
229 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
232 if (refcount_release(&set->cs_ref) == 0)
234 mtx_lock_spin(&cpuset_lock);
235 LIST_REMOVE(set, cs_siblings);
236 if (set->cs_id != CPUSET_INVALID)
237 LIST_REMOVE(set, cs_link);
238 LIST_INSERT_HEAD(head, set, cs_link);
239 mtx_unlock_spin(&cpuset_lock);
243 * Complete a deferred release. Removes the set from the list provided to
247 cpuset_rel_complete(struct cpuset *set)
249 LIST_REMOVE(set, cs_link);
250 cpuset_rel(set->cs_parent);
251 uma_zfree(cpuset_zone, set);
255 * Find a set based on an id. Returns it with a ref.
257 static struct cpuset *
258 cpuset_lookup(cpusetid_t setid, struct thread *td)
262 if (setid == CPUSET_INVALID)
264 mtx_lock_spin(&cpuset_lock);
265 LIST_FOREACH(set, &cpuset_ids, cs_link)
266 if (set->cs_id == setid)
270 mtx_unlock_spin(&cpuset_lock);
272 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
273 if (set != NULL && jailed(td->td_ucred)) {
274 struct cpuset *jset, *tset;
276 jset = td->td_ucred->cr_prison->pr_cpuset;
277 for (tset = set; tset != NULL; tset = tset->cs_parent)
290 * Create a set in the space provided in 'set' with the provided parameters.
291 * The set is returned with a single ref. May return EDEADLK if the set
292 * will have no valid cpu based on restrictions from the parent.
295 _cpuset_create(struct cpuset *set, struct cpuset *parent,
296 const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
300 domain = parent->cs_domain;
302 mask = &parent->cs_mask;
303 if (!CPU_OVERLAP(&parent->cs_mask, mask))
305 /* The domain must be prepared ahead of time. */
306 if (!domainset_valid(parent->cs_domain, domain))
308 CPU_COPY(mask, &set->cs_mask);
309 LIST_INIT(&set->cs_children);
310 refcount_init(&set->cs_ref, 1);
312 mtx_lock_spin(&cpuset_lock);
313 set->cs_domain = domain;
314 CPU_AND(&set->cs_mask, &parent->cs_mask);
316 set->cs_parent = cpuset_ref(parent);
317 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
318 if (set->cs_id != CPUSET_INVALID)
319 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
320 mtx_unlock_spin(&cpuset_lock);
326 * Create a new non-anonymous set with the requested parent and mask. May
327 * return failures if the mask is invalid or a new number can not be
330 * If *setp is not NULL, then it will be used as-is. The caller must take
331 * into account that *setp will be inserted at the head of cpuset_ids and
332 * plan any potentially conflicting cs_link usage accordingly.
335 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
342 id = alloc_unr(cpuset_unr);
345 dofree = (*setp == NULL);
349 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
350 error = _cpuset_create(set, parent, mask, NULL, id);
353 free_unr(cpuset_unr, id);
355 uma_zfree(cpuset_zone, set);
361 cpuset_freelist_add(struct setlist *list, int count)
366 for (i = 0; i < count; i++) {
367 set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
368 LIST_INSERT_HEAD(list, set, cs_link);
373 cpuset_freelist_init(struct setlist *list, int count)
377 cpuset_freelist_add(list, count);
381 cpuset_freelist_free(struct setlist *list)
385 while ((set = LIST_FIRST(list)) != NULL) {
386 LIST_REMOVE(set, cs_link);
387 uma_zfree(cpuset_zone, set);
392 domainset_freelist_add(struct domainlist *list, int count)
394 struct domainset *set;
397 for (i = 0; i < count; i++) {
398 set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
399 LIST_INSERT_HEAD(list, set, ds_link);
404 domainset_freelist_init(struct domainlist *list, int count)
408 domainset_freelist_add(list, count);
412 domainset_freelist_free(struct domainlist *list)
414 struct domainset *set;
416 while ((set = LIST_FIRST(list)) != NULL) {
417 LIST_REMOVE(set, ds_link);
418 uma_zfree(domainset_zone, set);
422 /* Copy a domainset preserving mask and policy. */
424 domainset_copy(const struct domainset *from, struct domainset *to)
427 DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
428 to->ds_policy = from->ds_policy;
429 to->ds_prefer = from->ds_prefer;
432 /* Return 1 if mask and policy are equal, otherwise 0. */
434 domainset_equal(const struct domainset *one, const struct domainset *two)
437 return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
438 one->ds_policy == two->ds_policy &&
439 one->ds_prefer == two->ds_prefer);
442 /* Return 1 if child is a valid subset of parent. */
444 domainset_valid(const struct domainset *parent, const struct domainset *child)
446 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
447 return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
448 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
452 domainset_restrict(const struct domainset *parent,
453 const struct domainset *child)
455 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
456 return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
457 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
461 * Lookup or create a domainset. The key is provided in ds_mask and
462 * ds_policy. If the domainset does not yet exist the storage in
463 * 'domain' is used to insert. Otherwise this storage is freed to the
464 * domainset_zone and the existing domainset is returned.
466 static struct domainset *
467 _domainset_create(struct domainset *domain, struct domainlist *freelist)
469 struct domainset *ndomain;
472 KASSERT(domain->ds_cnt <= vm_ndomains,
473 ("invalid domain count in domainset %p", domain));
474 KASSERT(domain->ds_policy != DOMAINSET_POLICY_PREFER ||
475 domain->ds_prefer < vm_ndomains,
476 ("invalid preferred domain in domains %p", domain));
478 mtx_lock_spin(&cpuset_lock);
479 LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
480 if (domainset_equal(ndomain, domain))
483 * If the domain does not yet exist we insert it and initialize
484 * various iteration helpers which are not part of the key.
486 if (ndomain == NULL) {
487 LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
488 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
489 for (i = 0, j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
490 if (DOMAINSET_ISSET(i, &domain->ds_mask))
491 domain->ds_order[j++] = i;
493 mtx_unlock_spin(&cpuset_lock);
496 if (freelist != NULL)
497 LIST_INSERT_HEAD(freelist, domain, ds_link);
499 uma_zfree(domainset_zone, domain);
505 * Are any of the domains in the mask empty? If so, silently
506 * remove them and update the domainset accordingly. If only empty
507 * domains are present, we must return failure.
510 domainset_empty_vm(struct domainset *domain)
515 DOMAINSET_ZERO(&empty);
516 for (i = 0; i < vm_ndomains; i++)
517 if (VM_DOMAIN_EMPTY(i))
518 DOMAINSET_SET(i, &empty);
519 if (DOMAINSET_SUBSET(&empty, &domain->ds_mask))
522 /* Remove empty domains from the set and recompute. */
523 DOMAINSET_ANDNOT(&domain->ds_mask, &empty);
524 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
525 for (i = j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
526 if (DOMAINSET_ISSET(i, &domain->ds_mask))
527 domain->ds_order[j++] = i;
529 /* Convert a PREFER policy referencing an empty domain to RR. */
530 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
531 DOMAINSET_ISSET(domain->ds_prefer, &empty)) {
532 domain->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
533 domain->ds_prefer = -1;
540 * Create or lookup a domainset based on the key held in 'domain'.
543 domainset_create(const struct domainset *domain)
545 struct domainset *ndomain;
548 * Validate the policy. It must specify a useable policy number with
549 * only valid domains. Preferred must include the preferred domain
552 if (domain->ds_policy <= DOMAINSET_POLICY_INVALID ||
553 domain->ds_policy > DOMAINSET_POLICY_MAX)
555 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
556 !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask))
558 if (!DOMAINSET_SUBSET(&domainset0.ds_mask, &domain->ds_mask))
560 ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
561 domainset_copy(domain, ndomain);
562 return _domainset_create(ndomain, NULL);
566 * Update thread domainset pointers.
569 domainset_notify(void)
574 sx_slock(&allproc_lock);
575 FOREACH_PROC_IN_SYSTEM(p) {
577 if (p->p_state == PRS_NEW) {
581 FOREACH_THREAD_IN_PROC(p, td) {
583 td->td_domain.dr_policy = td->td_cpuset->cs_domain;
588 sx_sunlock(&allproc_lock);
589 kernel_object->domain.dr_policy = cpuset_kernel->cs_domain;
593 * Create a new set that is a subset of a parent.
595 static struct domainset *
596 domainset_shadow(const struct domainset *pdomain,
597 const struct domainset *domain, struct domainlist *freelist)
599 struct domainset *ndomain;
601 ndomain = LIST_FIRST(freelist);
602 LIST_REMOVE(ndomain, ds_link);
605 * Initialize the key from the request.
607 domainset_copy(domain, ndomain);
610 * Restrict the key by the parent.
612 DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
614 return _domainset_create(ndomain, freelist);
618 * Recursively check for errors that would occur from applying mask to
619 * the tree of sets starting at 'set'. Checks for sets that would become
620 * empty as well as RDONLY flags.
623 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
629 mtx_assert(&cpuset_lock, MA_OWNED);
630 if (set->cs_flags & CPU_SET_RDONLY)
633 if (!CPU_OVERLAP(&set->cs_mask, mask))
635 CPU_COPY(&set->cs_mask, &newmask);
636 CPU_AND(&newmask, mask);
638 CPU_COPY(mask, &newmask);
640 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
641 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
647 * Applies the mask 'mask' without checking for empty sets or permissions.
650 cpuset_update(struct cpuset *set, cpuset_t *mask)
654 mtx_assert(&cpuset_lock, MA_OWNED);
655 CPU_AND(&set->cs_mask, mask);
656 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
657 cpuset_update(nset, &set->cs_mask);
663 * Modify the set 'set' to use a copy of the mask provided. Apply this new
664 * mask to restrict all children in the tree. Checks for validity before
665 * applying the changes.
668 cpuset_modify(struct cpuset *set, cpuset_t *mask)
673 error = priv_check(curthread, PRIV_SCHED_CPUSET);
677 * In case we are called from within the jail
678 * we do not allow modifying the dedicated root
679 * cpuset of the jail but may still allow to
682 if (jailed(curthread->td_ucred) &&
683 set->cs_flags & CPU_SET_ROOT)
686 * Verify that we have access to this set of
689 root = cpuset_getroot(set);
690 mtx_lock_spin(&cpuset_lock);
691 if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
695 error = cpuset_testupdate(set, mask, 0);
698 CPU_COPY(mask, &set->cs_mask);
699 cpuset_update(set, mask);
701 mtx_unlock_spin(&cpuset_lock);
707 * Recursively check for errors that would occur from applying mask to
708 * the tree of sets starting at 'set'. Checks for sets that would become
709 * empty as well as RDONLY flags.
712 cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
713 struct domainset *orig, int *count, int check_mask)
716 struct domainset *domain;
717 struct domainset newset;
720 mtx_assert(&cpuset_lock, MA_OWNED);
721 if (set->cs_flags & CPU_SET_RDONLY)
723 domain = set->cs_domain;
724 domainset_copy(domain, &newset);
725 if (!domainset_equal(domain, orig)) {
726 if (!domainset_restrict(domain, dset))
728 DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
729 /* Count the number of domains that are changing. */
733 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
734 if ((error = cpuset_testupdate_domain(nset, &newset, domain,
741 * Applies the mask 'mask' without checking for empty sets or permissions.
744 cpuset_update_domain(struct cpuset *set, struct domainset *domain,
745 struct domainset *orig, struct domainlist *domains)
749 mtx_assert(&cpuset_lock, MA_OWNED);
751 * If this domainset has changed from the parent we must calculate
752 * a new set. Otherwise it simply inherits from the parent. When
753 * we inherit from the parent we get a new mask and policy. If the
754 * set is modified from the parent we keep the policy and only
757 if (set->cs_domain != orig) {
758 orig = set->cs_domain;
759 set->cs_domain = domainset_shadow(domain, orig, domains);
761 set->cs_domain = domain;
762 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
763 cpuset_update_domain(nset, set->cs_domain, orig, domains);
769 * Modify the set 'set' to use a copy the domainset provided. Apply this new
770 * mask to restrict all children in the tree. Checks for validity before
771 * applying the changes.
774 cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
776 struct domainlist domains;
777 struct domainset temp;
778 struct domainset *dset;
780 int ndomains, needed;
783 error = priv_check(curthread, PRIV_SCHED_CPUSET);
787 * In case we are called from within the jail
788 * we do not allow modifying the dedicated root
789 * cpuset of the jail but may still allow to
792 if (jailed(curthread->td_ucred) &&
793 set->cs_flags & CPU_SET_ROOT)
795 domainset_freelist_init(&domains, 0);
796 domain = domainset_create(domain);
797 ndomains = needed = 0;
799 if (ndomains < needed) {
800 domainset_freelist_add(&domains, needed - ndomains);
803 root = cpuset_getroot(set);
804 mtx_lock_spin(&cpuset_lock);
805 dset = root->cs_domain;
807 * Verify that we have access to this set of domains.
809 if (!domainset_valid(dset, domain)) {
814 * If applying prefer we keep the current set as the fallback.
816 if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
817 DOMAINSET_COPY(&set->cs_domain->ds_mask,
820 * Determine whether we can apply this set of domains and
821 * how many new domain structures it will require.
823 domainset_copy(domain, &temp);
825 error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
829 } while (ndomains < needed);
830 dset = set->cs_domain;
831 cpuset_update_domain(set, domain, dset, &domains);
833 mtx_unlock_spin(&cpuset_lock);
834 domainset_freelist_free(&domains);
842 * Resolve the 'which' parameter of several cpuset apis.
844 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
845 * checks for permission via p_cansched().
847 * For WHICH_SET returns a valid set with a new reference.
849 * -1 may be supplied for any argument to mean the current proc/thread or
850 * the base set of the current thread. May fail with ESRCH/EPERM.
853 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
854 struct cpuset **setp)
871 if ((p = pfind(id)) == NULL)
886 case CPU_WHICH_CPUSET:
888 thread_lock(curthread);
889 set = cpuset_refbase(curthread->td_cpuset);
890 thread_unlock(curthread);
892 set = cpuset_lookup(id, curthread);
900 /* Find `set' for prison with given id. */
903 sx_slock(&allprison_lock);
904 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
905 sx_sunlock(&allprison_lock);
908 cpuset_ref(pr->pr_cpuset);
909 *setp = pr->pr_cpuset;
910 mtx_unlock(&pr->pr_mtx);
914 case CPU_WHICH_DOMAIN:
919 error = p_cansched(curthread, p);
925 td = FIRST_THREAD_IN_PROC(p);
932 cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
933 const struct domainset *domain)
935 struct cpuset *parent;
936 struct domainset *dset;
938 parent = cpuset_getbase(set);
940 * If we are restricting a cpu mask it must be a subset of the
941 * parent or invalid CPUs have been specified.
943 if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
947 * If we are restricting a domain mask it must be a subset of the
948 * parent or invalid domains have been specified.
950 dset = parent->cs_domain;
951 if (domain != NULL && !domainset_valid(dset, domain))
958 * Create an anonymous set with the provided mask in the space provided by
959 * 'nset'. If the passed in set is anonymous we use its parent otherwise
960 * the new set is a child of 'set'.
963 cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
964 const cpuset_t *mask, const struct domainset *domain,
965 struct setlist *cpusets, struct domainlist *domains)
967 struct cpuset *parent;
969 struct domainset *dset;
973 error = cpuset_testshadow(set, mask, domain);
977 parent = cpuset_getbase(set);
978 dset = parent->cs_domain;
980 mask = &set->cs_mask;
982 d = domainset_shadow(dset, domain, domains);
985 nset = LIST_FIRST(cpusets);
986 error = _cpuset_create(nset, parent, mask, d, CPUSET_INVALID);
988 LIST_REMOVE(nset, cs_link);
994 static struct cpuset *
995 cpuset_update_thread(struct thread *td, struct cpuset *nset)
997 struct cpuset *tdset;
999 tdset = td->td_cpuset;
1000 td->td_cpuset = nset;
1001 td->td_domain.dr_policy = nset->cs_domain;
1008 cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
1009 struct domainset *domain)
1011 struct cpuset *parent;
1013 parent = cpuset_getbase(tdset);
1015 mask = &tdset->cs_mask;
1017 domain = tdset->cs_domain;
1018 return cpuset_testshadow(parent, mask, domain);
1022 cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
1023 struct domainset *domain, struct cpuset **nsetp,
1024 struct setlist *freelist, struct domainlist *domainlist)
1026 struct cpuset *parent;
1028 parent = cpuset_getbase(tdset);
1030 mask = &tdset->cs_mask;
1032 domain = tdset->cs_domain;
1033 return cpuset_shadow(parent, nsetp, mask, domain, freelist,
1038 cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
1039 cpuset_t *mask, struct domainset *domain)
1041 struct cpuset *parent;
1043 parent = cpuset_getbase(tdset);
1046 * If the thread restricted its mask then apply that same
1047 * restriction to the new set, otherwise take it wholesale.
1049 if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
1050 CPU_COPY(&tdset->cs_mask, mask);
1051 CPU_AND(mask, &set->cs_mask);
1053 CPU_COPY(&set->cs_mask, mask);
1056 * If the thread restricted the domain then we apply the
1057 * restriction to the new set but retain the policy.
1059 if (tdset->cs_domain != parent->cs_domain) {
1060 domainset_copy(tdset->cs_domain, domain);
1061 DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
1063 domainset_copy(set->cs_domain, domain);
1065 if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
1072 cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
1074 struct domainset domain;
1077 if (tdset->cs_id != CPUSET_INVALID)
1079 return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1083 cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
1084 struct cpuset **nsetp, struct setlist *freelist,
1085 struct domainlist *domainlist)
1087 struct domainset domain;
1092 * If we're replacing on a thread that has not constrained the
1093 * original set we can simply accept the new set.
1095 if (tdset->cs_id != CPUSET_INVALID) {
1096 *nsetp = cpuset_ref(set);
1099 error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1103 return cpuset_shadow(set, nsetp, &mask, &domain, freelist,
1108 * Handle three cases for updating an entire process.
1110 * 1) Set is non-null. This reparents all anonymous sets to the provided
1111 * set and replaces all non-anonymous td_cpusets with the provided set.
1112 * 2) Mask is non-null. This replaces or creates anonymous sets for every
1113 * thread with the existing base as a parent.
1114 * 3) domain is non-null. This creates anonymous sets for every thread
1115 * and replaces the domain set.
1117 * This is overly complicated because we can't allocate while holding a
1118 * spinlock and spinlocks must be held while changing and examining thread
1122 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1123 struct domainset *domain)
1125 struct setlist freelist;
1126 struct setlist droplist;
1127 struct domainlist domainlist;
1128 struct cpuset *nset;
1136 * The algorithm requires two passes due to locking considerations.
1138 * 1) Lookup the process and acquire the locks in the required order.
1139 * 2) If enough cpusets have not been allocated release the locks and
1140 * allocate them. Loop.
1142 cpuset_freelist_init(&freelist, 1);
1143 domainset_freelist_init(&domainlist, 1);
1145 LIST_INIT(&droplist);
1148 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1151 if (nfree >= p->p_numthreads)
1153 threads = p->p_numthreads;
1155 if (nfree < threads) {
1156 cpuset_freelist_add(&freelist, threads - nfree);
1157 domainset_freelist_add(&domainlist, threads - nfree);
1161 PROC_LOCK_ASSERT(p, MA_OWNED);
1163 * Now that the appropriate locks are held and we have enough cpusets,
1164 * make sure the operation will succeed before applying changes. The
1165 * proc lock prevents td_cpuset from changing between calls.
1168 FOREACH_THREAD_IN_PROC(p, td) {
1171 error = cpuset_setproc_test_setthread(td->td_cpuset,
1174 error = cpuset_setproc_test_maskthread(td->td_cpuset,
1181 * Replace each thread's cpuset while using deferred release. We
1182 * must do this because the thread lock must be held while operating
1183 * on the thread and this limits the type of operations allowed.
1185 FOREACH_THREAD_IN_PROC(p, td) {
1188 error = cpuset_setproc_setthread(td->td_cpuset, set,
1189 &nset, &freelist, &domainlist);
1191 error = cpuset_setproc_maskthread(td->td_cpuset, mask,
1192 domain, &nset, &freelist, &domainlist);
1197 cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1203 while ((nset = LIST_FIRST(&droplist)) != NULL)
1204 cpuset_rel_complete(nset);
1205 cpuset_freelist_free(&freelist);
1206 domainset_freelist_free(&domainlist);
1211 bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen)
1219 for (i = 0; i < __bitset_words(setlen); i++) {
1228 if (bufsiz < sizeof(__STRING(ULONG_MAX)))
1230 bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]);
1238 bitset_strscan(struct bitset *set, int setlen, const char *buf)
1243 BIT_ZERO(setlen, set);
1245 for (i = 0; i < __bitset_words(setlen); i++) {
1250 ret = sscanf(p, "%lx", &set->__bits[i]);
1251 if (ret == 0 || ret == -1)
1253 while (isxdigit(*p))
1260 * Return a string representing a valid layout for a cpuset_t object.
1261 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1264 cpusetobj_strprint(char *buf, const cpuset_t *set)
1267 bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set,
1273 * Build a valid cpuset_t object from a string representation.
1274 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1277 cpusetobj_strscan(cpuset_t *set, const char *buf)
1281 if (strlen(buf) > CPUSETBUFSIZ - 1)
1284 p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)];
1292 * Handle a domainset specifier in the sysctl tree. A poiner to a pointer to
1293 * a domainset is in arg1. If the user specifies a valid domainset the
1294 * pointer is updated.
1297 * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred
1300 sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)
1302 char buf[DOMAINSETBUFSIZ];
1303 struct domainset *dset;
1304 struct domainset key;
1305 int policy, prefer, error;
1308 dset = *(struct domainset **)arg1;
1312 p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ,
1313 (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE);
1314 sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer);
1316 sprintf(buf, "<NULL>");
1317 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1318 if (error != 0 || req->newptr == NULL)
1322 * Read in and validate the string.
1324 memset(&key, 0, sizeof(key));
1325 p = &buf[bitset_strscan((struct bitset *)&key.ds_mask,
1326 DOMAINSET_SETSIZE, buf)];
1329 if (sscanf(p, ":%d:%d", &policy, &prefer) != 2)
1331 key.ds_policy = policy;
1332 key.ds_prefer = prefer;
1334 /* Domainset_create() validates the policy.*/
1335 dset = domainset_create(&key);
1338 *(struct domainset **)arg1 = dset;
1344 * Apply an anonymous mask or a domain to a single thread.
1347 _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1349 struct setlist cpusets;
1350 struct domainlist domainlist;
1351 struct cpuset *nset;
1357 cpuset_freelist_init(&cpusets, 1);
1358 domainset_freelist_init(&domainlist, domain != NULL);
1359 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1364 error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
1365 &cpusets, &domainlist);
1367 set = cpuset_update_thread(td, nset);
1373 cpuset_freelist_free(&cpusets);
1374 domainset_freelist_free(&domainlist);
1379 * Apply an anonymous mask to a single thread.
1382 cpuset_setthread(lwpid_t id, cpuset_t *mask)
1385 return _cpuset_setthread(id, mask, NULL);
1389 * Apply new cpumask to the ithread.
1392 cpuset_setithread(lwpid_t id, int cpu)
1398 CPU_COPY(cpuset_root, &mask);
1400 CPU_SET(cpu, &mask);
1401 return _cpuset_setthread(id, &mask, NULL);
1405 * Initialize static domainsets after NUMA information is available. This is
1406 * called before memory allocators are initialized.
1409 domainset_init(void)
1411 struct domainset *dset;
1414 dset = &domainset_roundrobin;
1415 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1416 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1417 dset->ds_prefer = -1;
1418 _domainset_create(dset, NULL);
1420 for (i = 0; i < vm_ndomains; i++) {
1421 dset = &domainset_fixed[i];
1422 DOMAINSET_ZERO(&dset->ds_mask);
1423 DOMAINSET_SET(i, &dset->ds_mask);
1424 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1425 _domainset_create(dset, NULL);
1427 dset = &domainset_prefer[i];
1428 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1429 dset->ds_policy = DOMAINSET_POLICY_PREFER;
1430 dset->ds_prefer = i;
1431 _domainset_create(dset, NULL);
1436 * Create the domainset for cpuset 0, 1 and cpuset 2.
1439 domainset_zero(void)
1441 struct domainset *dset, *tmp;
1443 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
1446 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1447 dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH;
1448 dset->ds_prefer = -1;
1449 curthread->td_domain.dr_policy = _domainset_create(dset, NULL);
1451 domainset_copy(dset, &domainset2);
1452 domainset2.ds_policy = DOMAINSET_POLICY_INTERLEAVE;
1453 kernel_object->domain.dr_policy = _domainset_create(&domainset2, NULL);
1455 /* Remove empty domains from the global policies. */
1456 LIST_FOREACH_SAFE(dset, &cpuset_domains, ds_link, tmp)
1457 if (domainset_empty_vm(dset))
1458 LIST_REMOVE(dset, ds_link);
1462 * Creates system-wide cpusets and the cpuset for thread0 including three
1465 * 0 - The root set which should represent all valid processors in the
1466 * system. It is initially created with a mask of all processors
1467 * because we don't know what processors are valid until cpuset_init()
1468 * runs. This set is immutable.
1469 * 1 - The default set which all processes are a member of until changed.
1470 * This allows an administrator to move all threads off of given cpus to
1471 * dedicate them to high priority tasks or save power etc.
1472 * 2 - The kernel set which allows restriction and policy to be applied only
1473 * to kernel threads and the kernel_object.
1476 cpuset_thread0(void)
1482 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1483 NULL, NULL, UMA_ALIGN_CACHE, 0);
1484 domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1485 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
1488 * Create the root system set (0) for the whole machine. Doesn't use
1489 * cpuset_create() due to NULL parent.
1491 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1492 CPU_COPY(&all_cpus, &set->cs_mask);
1493 LIST_INIT(&set->cs_children);
1494 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
1495 refcount_init(&set->cs_ref, 1);
1496 set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY;
1497 set->cs_domain = &domainset0;
1499 cpuset_root = &set->cs_mask;
1502 * Now derive a default (1), modifiable set from that to give out.
1504 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1505 error = _cpuset_create(set, cpuset_zero, NULL, NULL, 1);
1506 KASSERT(error == 0, ("Error creating default set: %d\n", error));
1507 cpuset_default = set;
1509 * Create the kernel set (2).
1511 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1512 error = _cpuset_create(set, cpuset_zero, NULL, NULL, 2);
1513 KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
1514 set->cs_domain = &domainset2;
1515 cpuset_kernel = set;
1518 * Initialize the unit allocator. 0 and 1 are allocated above.
1520 cpuset_unr = new_unrhdr(3, INT_MAX, NULL);
1523 * If MD code has not initialized per-domain cpusets, place all
1526 for (i = 0; i < MAXMEMDOM; i++)
1527 if (!CPU_EMPTY(&cpuset_domain[i]))
1529 CPU_COPY(&all_cpus, &cpuset_domain[0]);
1532 return (cpuset_default);
1536 cpuset_kernthread(struct thread *td)
1541 set = td->td_cpuset;
1542 td->td_cpuset = cpuset_ref(cpuset_kernel);
1548 * Create a cpuset, which would be cpuset_create() but
1549 * mark the new 'set' as root.
1551 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
1554 * In case of no error, returns the set in *setp locked with a reference.
1557 cpuset_create_root(struct prison *pr, struct cpuset **setp)
1562 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1563 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1566 error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1570 KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data",
1571 __func__, __LINE__));
1573 /* Mark the set as root. */
1574 set->cs_flags |= CPU_SET_ROOT;
1581 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1585 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1586 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1589 error = cpuset_setproc(p->p_pid, set, NULL, NULL);
1597 * In Capability mode, the only accesses that are permitted are to the current
1598 * thread and process' CPU and domain sets.
1601 cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which,
1604 if (IN_CAPABILITY_MODE(td)) {
1605 if (level != CPU_LEVEL_WHICH)
1607 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1610 !(which == CPU_WHICH_TID && id == td->td_tid) &&
1611 !(which == CPU_WHICH_PID && id == td->td_proc->p_pid))
1617 #ifndef _SYS_SYSPROTO_H_
1618 struct cpuset_args {
1623 sys_cpuset(struct thread *td, struct cpuset_args *uap)
1625 struct cpuset *root;
1630 root = cpuset_refroot(td->td_cpuset);
1633 error = cpuset_create(&set, root, &root->cs_mask);
1637 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1639 error = cpuset_setproc(-1, set, NULL, NULL);
1644 #ifndef _SYS_SYSPROTO_H_
1645 struct cpuset_setid_args {
1652 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1655 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1659 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1660 id_t id, cpusetid_t setid)
1666 * Presently we only support per-process sets.
1668 if (which != CPU_WHICH_PID)
1670 set = cpuset_lookup(setid, td);
1673 error = cpuset_setproc(id, set, NULL, NULL);
1678 #ifndef _SYS_SYSPROTO_H_
1679 struct cpuset_getid_args {
1687 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1690 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1695 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1696 id_t id, cpusetid_t *setid)
1698 struct cpuset *nset;
1705 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1707 error = cpuset_which(which, id, &p, &ttd, &set);
1714 set = cpuset_refbase(ttd->td_cpuset);
1718 case CPU_WHICH_CPUSET:
1719 case CPU_WHICH_JAIL:
1722 case CPU_WHICH_DOMAIN:
1726 case CPU_LEVEL_ROOT:
1727 nset = cpuset_refroot(set);
1731 case CPU_LEVEL_CPUSET:
1733 case CPU_LEVEL_WHICH:
1739 error = copyout(&tmpid, setid, sizeof(tmpid));
1744 #ifndef _SYS_SYSPROTO_H_
1745 struct cpuset_getaffinity_args {
1754 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1757 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
1758 uap->id, uap->cpusetsize, uap->mask));
1762 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1763 id_t id, size_t cpusetsize, cpuset_t *maskp)
1766 struct cpuset *nset;
1773 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1775 error = cpuset_check_capabilities(td, level, which, id);
1779 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1780 error = cpuset_which(which, id, &p, &ttd, &set);
1784 case CPU_LEVEL_ROOT:
1785 case CPU_LEVEL_CPUSET:
1790 set = cpuset_ref(ttd->td_cpuset);
1793 case CPU_WHICH_CPUSET:
1794 case CPU_WHICH_JAIL:
1797 case CPU_WHICH_INTRHANDLER:
1798 case CPU_WHICH_ITHREAD:
1799 case CPU_WHICH_DOMAIN:
1803 if (level == CPU_LEVEL_ROOT)
1804 nset = cpuset_refroot(set);
1806 nset = cpuset_refbase(set);
1807 CPU_COPY(&nset->cs_mask, mask);
1810 case CPU_LEVEL_WHICH:
1814 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1818 FOREACH_THREAD_IN_PROC(p, ttd) {
1820 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1824 case CPU_WHICH_CPUSET:
1825 case CPU_WHICH_JAIL:
1826 CPU_COPY(&set->cs_mask, mask);
1829 case CPU_WHICH_INTRHANDLER:
1830 case CPU_WHICH_ITHREAD:
1831 error = intr_getaffinity(id, which, mask);
1833 case CPU_WHICH_DOMAIN:
1834 if (id < 0 || id >= MAXMEMDOM)
1837 CPU_COPY(&cpuset_domain[id], mask);
1850 error = copyout(mask, maskp, size);
1856 #ifndef _SYS_SYSPROTO_H_
1857 struct cpuset_setaffinity_args {
1862 const cpuset_t *mask;
1866 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1869 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
1870 uap->id, uap->cpusetsize, uap->mask));
1874 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1875 id_t id, size_t cpusetsize, const cpuset_t *maskp)
1877 struct cpuset *nset;
1884 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1886 error = cpuset_check_capabilities(td, level, which, id);
1889 mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1890 error = copyin(maskp, mask, cpusetsize);
1894 * Verify that no high bits are set.
1896 if (cpusetsize > sizeof(cpuset_t)) {
1900 end = cp = (char *)&mask->__bits;
1902 cp += sizeof(cpuset_t);
1910 case CPU_LEVEL_ROOT:
1911 case CPU_LEVEL_CPUSET:
1912 error = cpuset_which(which, id, &p, &ttd, &set);
1919 set = cpuset_ref(ttd->td_cpuset);
1923 case CPU_WHICH_CPUSET:
1924 case CPU_WHICH_JAIL:
1927 case CPU_WHICH_INTRHANDLER:
1928 case CPU_WHICH_ITHREAD:
1929 case CPU_WHICH_DOMAIN:
1933 if (level == CPU_LEVEL_ROOT)
1934 nset = cpuset_refroot(set);
1936 nset = cpuset_refbase(set);
1937 error = cpuset_modify(nset, mask);
1941 case CPU_LEVEL_WHICH:
1944 error = cpuset_setthread(id, mask);
1947 error = cpuset_setproc(id, NULL, mask, NULL);
1949 case CPU_WHICH_CPUSET:
1950 case CPU_WHICH_JAIL:
1951 error = cpuset_which(which, id, &p, &ttd, &set);
1953 error = cpuset_modify(set, mask);
1958 case CPU_WHICH_INTRHANDLER:
1959 case CPU_WHICH_ITHREAD:
1960 error = intr_setaffinity(id, which, mask);
1976 #ifndef _SYS_SYSPROTO_H_
1977 struct cpuset_getdomain_args {
1981 size_t domainsetsize;
1987 sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
1990 return (kern_cpuset_getdomain(td, uap->level, uap->which,
1991 uap->id, uap->domainsetsize, uap->mask, uap->policy));
1995 kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
1996 id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp)
1998 struct domainset outset;
2000 struct cpuset *nset;
2002 struct domainset *dset;
2007 if (domainsetsize < sizeof(domainset_t) ||
2008 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2010 error = cpuset_check_capabilities(td, level, which, id);
2013 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2014 bzero(&outset, sizeof(outset));
2015 error = cpuset_which(which, id, &p, &ttd, &set);
2019 case CPU_LEVEL_ROOT:
2020 case CPU_LEVEL_CPUSET:
2025 set = cpuset_ref(ttd->td_cpuset);
2028 case CPU_WHICH_CPUSET:
2029 case CPU_WHICH_JAIL:
2032 case CPU_WHICH_INTRHANDLER:
2033 case CPU_WHICH_ITHREAD:
2034 case CPU_WHICH_DOMAIN:
2038 if (level == CPU_LEVEL_ROOT)
2039 nset = cpuset_refroot(set);
2041 nset = cpuset_refbase(set);
2042 domainset_copy(nset->cs_domain, &outset);
2045 case CPU_LEVEL_WHICH:
2049 domainset_copy(ttd->td_cpuset->cs_domain, &outset);
2053 FOREACH_THREAD_IN_PROC(p, ttd) {
2055 dset = ttd->td_cpuset->cs_domain;
2056 /* Show all domains in the proc. */
2057 DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
2058 /* Last policy wins. */
2059 outset.ds_policy = dset->ds_policy;
2060 outset.ds_prefer = dset->ds_prefer;
2064 case CPU_WHICH_CPUSET:
2065 case CPU_WHICH_JAIL:
2066 domainset_copy(set->cs_domain, &outset);
2069 case CPU_WHICH_INTRHANDLER:
2070 case CPU_WHICH_ITHREAD:
2071 case CPU_WHICH_DOMAIN:
2085 * Translate prefer into a set containing only the preferred domain,
2086 * not the entire fallback set.
2088 if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
2089 DOMAINSET_ZERO(&outset.ds_mask);
2090 DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
2092 DOMAINSET_COPY(&outset.ds_mask, mask);
2094 error = copyout(mask, maskp, domainsetsize);
2096 if (suword32(policyp, outset.ds_policy) != 0)
2103 #ifndef _SYS_SYSPROTO_H_
2104 struct cpuset_setdomain_args {
2108 size_t domainsetsize;
2114 sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
2117 return (kern_cpuset_setdomain(td, uap->level, uap->which,
2118 uap->id, uap->domainsetsize, uap->mask, uap->policy));
2122 kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2123 id_t id, size_t domainsetsize, const domainset_t *maskp, int policy)
2125 struct cpuset *nset;
2129 struct domainset domain;
2133 if (domainsetsize < sizeof(domainset_t) ||
2134 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2136 if (policy <= DOMAINSET_POLICY_INVALID ||
2137 policy > DOMAINSET_POLICY_MAX)
2139 error = cpuset_check_capabilities(td, level, which, id);
2142 memset(&domain, 0, sizeof(domain));
2143 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2144 error = copyin(maskp, mask, domainsetsize);
2148 * Verify that no high bits are set.
2150 if (domainsetsize > sizeof(domainset_t)) {
2154 end = cp = (char *)&mask->__bits;
2155 end += domainsetsize;
2156 cp += sizeof(domainset_t);
2163 DOMAINSET_COPY(mask, &domain.ds_mask);
2164 domain.ds_policy = policy;
2167 * Sanitize the provided mask.
2169 if (!DOMAINSET_SUBSET(&all_domains, &domain.ds_mask)) {
2174 /* Translate preferred policy into a mask and fallback. */
2175 if (policy == DOMAINSET_POLICY_PREFER) {
2176 /* Only support a single preferred domain. */
2177 if (DOMAINSET_COUNT(&domain.ds_mask) != 1) {
2181 domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
2182 /* This will be constrained by domainset_shadow(). */
2183 DOMAINSET_COPY(&all_domains, &domain.ds_mask);
2187 * When given an impossible policy, fall back to interleaving
2188 * across all domains.
2190 if (domainset_empty_vm(&domain))
2191 domainset_copy(&domainset2, &domain);
2194 case CPU_LEVEL_ROOT:
2195 case CPU_LEVEL_CPUSET:
2196 error = cpuset_which(which, id, &p, &ttd, &set);
2203 set = cpuset_ref(ttd->td_cpuset);
2207 case CPU_WHICH_CPUSET:
2208 case CPU_WHICH_JAIL:
2211 case CPU_WHICH_INTRHANDLER:
2212 case CPU_WHICH_ITHREAD:
2213 case CPU_WHICH_DOMAIN:
2217 if (level == CPU_LEVEL_ROOT)
2218 nset = cpuset_refroot(set);
2220 nset = cpuset_refbase(set);
2221 error = cpuset_modify_domain(nset, &domain);
2225 case CPU_LEVEL_WHICH:
2228 error = _cpuset_setthread(id, NULL, &domain);
2231 error = cpuset_setproc(id, NULL, NULL, &domain);
2233 case CPU_WHICH_CPUSET:
2234 case CPU_WHICH_JAIL:
2235 error = cpuset_which(which, id, &p, &ttd, &set);
2237 error = cpuset_modify_domain(set, &domain);
2242 case CPU_WHICH_INTRHANDLER:
2243 case CPU_WHICH_ITHREAD:
2261 ddb_display_bitset(const struct bitset *set, int size)
2265 for (once = 0, bit = 0; bit < size; bit++) {
2266 if (CPU_ISSET(bit, set)) {
2268 db_printf("%d", bit);
2271 db_printf(",%d", bit);
2275 db_printf("<none>");
2279 ddb_display_cpuset(const cpuset_t *set)
2281 ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
2285 ddb_display_domainset(const domainset_t *set)
2287 ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
2290 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
2294 LIST_FOREACH(set, &cpuset_ids, cs_link) {
2295 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
2296 set, set->cs_id, refcount_load(&set->cs_ref), set->cs_flags,
2297 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
2298 db_printf(" cpu mask=");
2299 ddb_display_cpuset(&set->cs_mask);
2301 db_printf(" domain policy %d prefer %d mask=",
2302 set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
2303 ddb_display_domainset(&set->cs_domain->ds_mask);
2310 DB_SHOW_COMMAND(domainsets, db_show_domainsets)
2312 struct domainset *set;
2314 LIST_FOREACH(set, &cpuset_domains, ds_link) {
2315 db_printf("set=%p policy %d prefer %d cnt %d\n",
2316 set, set->ds_policy, set->ds_prefer, set->ds_cnt);
2317 db_printf(" mask =");
2318 ddb_display_domainset(&set->ds_mask);