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_if_not_last(&set->cs_ref))
212 mtx_lock_spin(&cpuset_lock);
213 if (!refcount_release(&set->cs_ref)) {
214 mtx_unlock_spin(&cpuset_lock);
217 LIST_REMOVE(set, cs_siblings);
219 if (id != CPUSET_INVALID)
220 LIST_REMOVE(set, cs_link);
221 mtx_unlock_spin(&cpuset_lock);
222 cpuset_rel(set->cs_parent);
223 uma_zfree(cpuset_zone, set);
224 if (id != CPUSET_INVALID)
225 free_unr(cpuset_unr, id);
229 * Deferred release must be used when in a context that is not safe to
230 * allocate/free. This places any unreferenced sets on the list 'head'.
233 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
236 if (refcount_release_if_not_last(&set->cs_ref))
238 mtx_lock_spin(&cpuset_lock);
239 if (!refcount_release(&set->cs_ref)) {
240 mtx_unlock_spin(&cpuset_lock);
243 LIST_REMOVE(set, cs_siblings);
244 if (set->cs_id != CPUSET_INVALID)
245 LIST_REMOVE(set, cs_link);
246 LIST_INSERT_HEAD(head, set, cs_link);
247 mtx_unlock_spin(&cpuset_lock);
251 * Complete a deferred release. Removes the set from the list provided to
255 cpuset_rel_complete(struct cpuset *set)
260 LIST_REMOVE(set, cs_link);
261 cpuset_rel(set->cs_parent);
262 uma_zfree(cpuset_zone, set);
263 if (id != CPUSET_INVALID)
264 free_unr(cpuset_unr, id);
268 * Find a set based on an id. Returns it with a ref.
270 static struct cpuset *
271 cpuset_lookup(cpusetid_t setid, struct thread *td)
275 if (setid == CPUSET_INVALID)
277 mtx_lock_spin(&cpuset_lock);
278 LIST_FOREACH(set, &cpuset_ids, cs_link)
279 if (set->cs_id == setid)
283 mtx_unlock_spin(&cpuset_lock);
285 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
286 if (set != NULL && jailed(td->td_ucred)) {
287 struct cpuset *jset, *tset;
289 jset = td->td_ucred->cr_prison->pr_cpuset;
290 for (tset = set; tset != NULL; tset = tset->cs_parent)
303 * Initialize a set in the space provided in 'set' with the provided parameters.
304 * The set is returned with a single ref. May return EDEADLK if the set
305 * will have no valid cpu based on restrictions from the parent.
308 cpuset_init(struct cpuset *set, struct cpuset *parent,
309 const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
313 domain = parent->cs_domain;
315 mask = &parent->cs_mask;
316 if (!CPU_OVERLAP(&parent->cs_mask, mask))
318 /* The domain must be prepared ahead of time. */
319 if (!domainset_valid(parent->cs_domain, domain))
321 CPU_COPY(mask, &set->cs_mask);
322 LIST_INIT(&set->cs_children);
323 refcount_init(&set->cs_ref, 1);
325 mtx_lock_spin(&cpuset_lock);
326 set->cs_domain = domain;
327 CPU_AND(&set->cs_mask, &parent->cs_mask);
329 set->cs_parent = cpuset_ref(parent);
330 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
331 if (set->cs_id != CPUSET_INVALID)
332 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
333 mtx_unlock_spin(&cpuset_lock);
339 * Create a new non-anonymous set with the requested parent and mask. May
340 * return failures if the mask is invalid or a new number can not be
343 * If *setp is not NULL, then it will be used as-is. The caller must take
344 * into account that *setp will be inserted at the head of cpuset_ids and
345 * plan any potentially conflicting cs_link usage accordingly.
348 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
355 id = alloc_unr(cpuset_unr);
358 dofree = (*setp == NULL);
362 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
363 error = cpuset_init(set, parent, mask, NULL, id);
366 free_unr(cpuset_unr, id);
368 uma_zfree(cpuset_zone, set);
374 cpuset_freelist_add(struct setlist *list, int count)
379 for (i = 0; i < count; i++) {
380 set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
381 LIST_INSERT_HEAD(list, set, cs_link);
386 cpuset_freelist_init(struct setlist *list, int count)
390 cpuset_freelist_add(list, count);
394 cpuset_freelist_free(struct setlist *list)
398 while ((set = LIST_FIRST(list)) != NULL) {
399 LIST_REMOVE(set, cs_link);
400 uma_zfree(cpuset_zone, set);
405 domainset_freelist_add(struct domainlist *list, int count)
407 struct domainset *set;
410 for (i = 0; i < count; i++) {
411 set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
412 LIST_INSERT_HEAD(list, set, ds_link);
417 domainset_freelist_init(struct domainlist *list, int count)
421 domainset_freelist_add(list, count);
425 domainset_freelist_free(struct domainlist *list)
427 struct domainset *set;
429 while ((set = LIST_FIRST(list)) != NULL) {
430 LIST_REMOVE(set, ds_link);
431 uma_zfree(domainset_zone, set);
435 /* Copy a domainset preserving mask and policy. */
437 domainset_copy(const struct domainset *from, struct domainset *to)
440 DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
441 to->ds_policy = from->ds_policy;
442 to->ds_prefer = from->ds_prefer;
445 /* Return 1 if mask and policy are equal, otherwise 0. */
447 domainset_equal(const struct domainset *one, const struct domainset *two)
450 return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
451 one->ds_policy == two->ds_policy &&
452 one->ds_prefer == two->ds_prefer);
455 /* Return 1 if child is a valid subset of parent. */
457 domainset_valid(const struct domainset *parent, const struct domainset *child)
459 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
460 return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
461 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
465 domainset_restrict(const struct domainset *parent,
466 const struct domainset *child)
468 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
469 return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
470 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
474 * Lookup or create a domainset. The key is provided in ds_mask and
475 * ds_policy. If the domainset does not yet exist the storage in
476 * 'domain' is used to insert. Otherwise this storage is freed to the
477 * domainset_zone and the existing domainset is returned.
479 static struct domainset *
480 _domainset_create(struct domainset *domain, struct domainlist *freelist)
482 struct domainset *ndomain;
485 KASSERT(domain->ds_cnt <= vm_ndomains,
486 ("invalid domain count in domainset %p", domain));
487 KASSERT(domain->ds_policy != DOMAINSET_POLICY_PREFER ||
488 domain->ds_prefer < vm_ndomains,
489 ("invalid preferred domain in domains %p", domain));
491 mtx_lock_spin(&cpuset_lock);
492 LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
493 if (domainset_equal(ndomain, domain))
496 * If the domain does not yet exist we insert it and initialize
497 * various iteration helpers which are not part of the key.
499 if (ndomain == NULL) {
500 LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
501 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
502 for (i = 0, j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
503 if (DOMAINSET_ISSET(i, &domain->ds_mask))
504 domain->ds_order[j++] = i;
506 mtx_unlock_spin(&cpuset_lock);
509 if (freelist != NULL)
510 LIST_INSERT_HEAD(freelist, domain, ds_link);
512 uma_zfree(domainset_zone, domain);
518 * Are any of the domains in the mask empty? If so, silently
519 * remove them and update the domainset accordingly. If only empty
520 * domains are present, we must return failure.
523 domainset_empty_vm(struct domainset *domain)
528 DOMAINSET_ZERO(&empty);
529 for (i = 0; i < vm_ndomains; i++)
530 if (VM_DOMAIN_EMPTY(i))
531 DOMAINSET_SET(i, &empty);
532 if (DOMAINSET_SUBSET(&empty, &domain->ds_mask))
535 /* Remove empty domains from the set and recompute. */
536 DOMAINSET_ANDNOT(&domain->ds_mask, &empty);
537 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
538 for (i = j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
539 if (DOMAINSET_ISSET(i, &domain->ds_mask))
540 domain->ds_order[j++] = i;
542 /* Convert a PREFER policy referencing an empty domain to RR. */
543 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
544 DOMAINSET_ISSET(domain->ds_prefer, &empty)) {
545 domain->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
546 domain->ds_prefer = -1;
553 * Create or lookup a domainset based on the key held in 'domain'.
556 domainset_create(const struct domainset *domain)
558 struct domainset *ndomain;
561 * Validate the policy. It must specify a useable policy number with
562 * only valid domains. Preferred must include the preferred domain
565 if (domain->ds_policy <= DOMAINSET_POLICY_INVALID ||
566 domain->ds_policy > DOMAINSET_POLICY_MAX)
568 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
569 !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask))
571 if (!DOMAINSET_SUBSET(&domainset0.ds_mask, &domain->ds_mask))
573 ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
574 domainset_copy(domain, ndomain);
575 return _domainset_create(ndomain, NULL);
579 * Update thread domainset pointers.
582 domainset_notify(void)
587 sx_slock(&allproc_lock);
588 FOREACH_PROC_IN_SYSTEM(p) {
590 if (p->p_state == PRS_NEW) {
594 FOREACH_THREAD_IN_PROC(p, td) {
596 td->td_domain.dr_policy = td->td_cpuset->cs_domain;
601 sx_sunlock(&allproc_lock);
602 kernel_object->domain.dr_policy = cpuset_kernel->cs_domain;
606 * Create a new set that is a subset of a parent.
608 static struct domainset *
609 domainset_shadow(const struct domainset *pdomain,
610 const struct domainset *domain, struct domainlist *freelist)
612 struct domainset *ndomain;
614 ndomain = LIST_FIRST(freelist);
615 LIST_REMOVE(ndomain, ds_link);
618 * Initialize the key from the request.
620 domainset_copy(domain, ndomain);
623 * Restrict the key by the parent.
625 DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
627 return _domainset_create(ndomain, freelist);
631 * Recursively check for errors that would occur from applying mask to
632 * the tree of sets starting at 'set'. Checks for sets that would become
633 * empty as well as RDONLY flags.
636 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int augment_mask)
642 mtx_assert(&cpuset_lock, MA_OWNED);
643 if (set->cs_flags & CPU_SET_RDONLY)
646 CPU_COPY(&set->cs_mask, &newmask);
647 CPU_AND(&newmask, mask);
649 CPU_COPY(mask, &newmask);
651 if (CPU_EMPTY(&newmask))
654 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
655 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
661 * Applies the mask 'mask' without checking for empty sets or permissions.
664 cpuset_update(struct cpuset *set, cpuset_t *mask)
668 mtx_assert(&cpuset_lock, MA_OWNED);
669 CPU_AND(&set->cs_mask, mask);
670 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
671 cpuset_update(nset, &set->cs_mask);
677 * Modify the set 'set' to use a copy of the mask provided. Apply this new
678 * mask to restrict all children in the tree. Checks for validity before
679 * applying the changes.
682 cpuset_modify(struct cpuset *set, cpuset_t *mask)
687 error = priv_check(curthread, PRIV_SCHED_CPUSET);
691 * In case we are called from within the jail,
692 * we do not allow modifying the dedicated root
693 * cpuset of the jail but may still allow to
694 * change child sets, including subordinate jails'
697 if ((set->cs_flags & CPU_SET_ROOT) != 0 &&
698 jailed(curthread->td_ucred) &&
699 set == curthread->td_ucred->cr_prison->pr_cpuset)
702 * Verify that we have access to this set of
705 if ((set->cs_flags & (CPU_SET_ROOT | CPU_SET_RDONLY)) == CPU_SET_ROOT) {
706 KASSERT(set->cs_parent != NULL,
707 ("jail.cpuset=%d is not a proper child of parent jail's root.",
711 * cpuset_getroot() cannot work here due to how top-level jail
712 * roots are constructed. Top-level jails are parented to
713 * thread0's cpuset (i.e. cpuset 1) rather than the system root.
715 root = set->cs_parent;
717 root = cpuset_getroot(set);
719 mtx_lock_spin(&cpuset_lock);
720 if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
724 error = cpuset_testupdate(set, mask, 0);
727 CPU_COPY(mask, &set->cs_mask);
728 cpuset_update(set, mask);
730 mtx_unlock_spin(&cpuset_lock);
736 * Recursively check for errors that would occur from applying mask to
737 * the tree of sets starting at 'set'. Checks for sets that would become
738 * empty as well as RDONLY flags.
741 cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
742 struct domainset *orig, int *count, int augment_mask __unused)
745 struct domainset *domain;
746 struct domainset newset;
749 mtx_assert(&cpuset_lock, MA_OWNED);
750 if (set->cs_flags & CPU_SET_RDONLY)
752 domain = set->cs_domain;
753 domainset_copy(domain, &newset);
754 if (!domainset_equal(domain, orig)) {
755 if (!domainset_restrict(domain, dset))
757 DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
758 /* Count the number of domains that are changing. */
762 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
763 if ((error = cpuset_testupdate_domain(nset, &newset, domain,
770 * Applies the mask 'mask' without checking for empty sets or permissions.
773 cpuset_update_domain(struct cpuset *set, struct domainset *domain,
774 struct domainset *orig, struct domainlist *domains)
778 mtx_assert(&cpuset_lock, MA_OWNED);
780 * If this domainset has changed from the parent we must calculate
781 * a new set. Otherwise it simply inherits from the parent. When
782 * we inherit from the parent we get a new mask and policy. If the
783 * set is modified from the parent we keep the policy and only
786 if (set->cs_domain != orig) {
787 orig = set->cs_domain;
788 set->cs_domain = domainset_shadow(domain, orig, domains);
790 set->cs_domain = domain;
791 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
792 cpuset_update_domain(nset, set->cs_domain, orig, domains);
798 * Modify the set 'set' to use a copy the domainset provided. Apply this new
799 * mask to restrict all children in the tree. Checks for validity before
800 * applying the changes.
803 cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
805 struct domainlist domains;
806 struct domainset temp;
807 struct domainset *dset;
809 int ndomains, needed;
812 error = priv_check(curthread, PRIV_SCHED_CPUSET);
816 * In case we are called from within the jail
817 * we do not allow modifying the dedicated root
818 * cpuset of the jail but may still allow to
821 if (jailed(curthread->td_ucred) &&
822 set->cs_flags & CPU_SET_ROOT)
824 domainset_freelist_init(&domains, 0);
825 domain = domainset_create(domain);
828 mtx_lock_spin(&cpuset_lock);
830 root = cpuset_getroot(set);
831 dset = root->cs_domain;
833 * Verify that we have access to this set of domains.
835 if (!domainset_valid(dset, domain)) {
840 * If applying prefer we keep the current set as the fallback.
842 if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
843 DOMAINSET_COPY(&set->cs_domain->ds_mask,
846 * Determine whether we can apply this set of domains and
847 * how many new domain structures it will require.
849 domainset_copy(domain, &temp);
851 error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
855 if (ndomains >= needed)
858 /* Dropping the lock; we'll need to re-evaluate again. */
859 mtx_unlock_spin(&cpuset_lock);
860 domainset_freelist_add(&domains, needed - ndomains);
862 mtx_lock_spin(&cpuset_lock);
864 dset = set->cs_domain;
865 cpuset_update_domain(set, domain, dset, &domains);
867 mtx_unlock_spin(&cpuset_lock);
868 domainset_freelist_free(&domains);
876 * Resolve the 'which' parameter of several cpuset apis.
878 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
879 * checks for permission via p_cansched().
881 * For WHICH_SET returns a valid set with a new reference.
883 * -1 may be supplied for any argument to mean the current proc/thread or
884 * the base set of the current thread. May fail with ESRCH/EPERM.
887 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
888 struct cpuset **setp)
905 if ((p = pfind(id)) == NULL)
920 case CPU_WHICH_CPUSET:
922 thread_lock(curthread);
923 set = cpuset_refbase(curthread->td_cpuset);
924 thread_unlock(curthread);
926 set = cpuset_lookup(id, curthread);
934 /* Find `set' for prison with given id. */
937 sx_slock(&allprison_lock);
938 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
939 sx_sunlock(&allprison_lock);
942 cpuset_ref(pr->pr_cpuset);
943 *setp = pr->pr_cpuset;
944 mtx_unlock(&pr->pr_mtx);
948 case CPU_WHICH_DOMAIN:
953 error = p_cansched(curthread, p);
959 td = FIRST_THREAD_IN_PROC(p);
966 cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
967 const struct domainset *domain)
969 struct cpuset *parent;
970 struct domainset *dset;
972 parent = cpuset_getbase(set);
974 * If we are restricting a cpu mask it must be a subset of the
975 * parent or invalid CPUs have been specified.
977 if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
981 * If we are restricting a domain mask it must be a subset of the
982 * parent or invalid domains have been specified.
984 dset = parent->cs_domain;
985 if (domain != NULL && !domainset_valid(dset, domain))
992 * Create an anonymous set with the provided mask in the space provided by
993 * 'nset'. If the passed in set is anonymous we use its parent otherwise
994 * the new set is a child of 'set'.
997 cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
998 const cpuset_t *mask, const struct domainset *domain,
999 struct setlist *cpusets, struct domainlist *domains)
1001 struct cpuset *parent;
1002 struct cpuset *nset;
1003 struct domainset *dset;
1004 struct domainset *d;
1007 error = cpuset_testshadow(set, mask, domain);
1011 parent = cpuset_getbase(set);
1012 dset = parent->cs_domain;
1014 mask = &set->cs_mask;
1016 d = domainset_shadow(dset, domain, domains);
1019 nset = LIST_FIRST(cpusets);
1020 error = cpuset_init(nset, parent, mask, d, CPUSET_INVALID);
1022 LIST_REMOVE(nset, cs_link);
1028 static struct cpuset *
1029 cpuset_update_thread(struct thread *td, struct cpuset *nset)
1031 struct cpuset *tdset;
1033 tdset = td->td_cpuset;
1034 td->td_cpuset = nset;
1035 td->td_domain.dr_policy = nset->cs_domain;
1042 cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
1043 struct domainset *domain)
1045 struct cpuset *parent;
1047 parent = cpuset_getbase(tdset);
1049 mask = &tdset->cs_mask;
1051 domain = tdset->cs_domain;
1052 return cpuset_testshadow(parent, mask, domain);
1056 cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
1057 struct domainset *domain, struct cpuset **nsetp,
1058 struct setlist *freelist, struct domainlist *domainlist)
1060 struct cpuset *parent;
1062 parent = cpuset_getbase(tdset);
1064 mask = &tdset->cs_mask;
1066 domain = tdset->cs_domain;
1067 return cpuset_shadow(parent, nsetp, mask, domain, freelist,
1072 cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
1073 cpuset_t *mask, struct domainset *domain)
1075 struct cpuset *parent;
1077 parent = cpuset_getbase(tdset);
1080 * If the thread restricted its mask then apply that same
1081 * restriction to the new set, otherwise take it wholesale.
1083 if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
1084 CPU_COPY(&tdset->cs_mask, mask);
1085 CPU_AND(mask, &set->cs_mask);
1087 CPU_COPY(&set->cs_mask, mask);
1090 * If the thread restricted the domain then we apply the
1091 * restriction to the new set but retain the policy.
1093 if (tdset->cs_domain != parent->cs_domain) {
1094 domainset_copy(tdset->cs_domain, domain);
1095 DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
1097 domainset_copy(set->cs_domain, domain);
1099 if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
1106 cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
1108 struct domainset domain;
1111 if (tdset->cs_id != CPUSET_INVALID)
1113 return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1117 cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
1118 struct cpuset **nsetp, struct setlist *freelist,
1119 struct domainlist *domainlist)
1121 struct domainset domain;
1126 * If we're replacing on a thread that has not constrained the
1127 * original set we can simply accept the new set.
1129 if (tdset->cs_id != CPUSET_INVALID) {
1130 *nsetp = cpuset_ref(set);
1133 error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1137 return cpuset_shadow(set, nsetp, &mask, &domain, freelist,
1142 cpuset_setproc_newbase(struct thread *td, struct cpuset *set,
1143 struct cpuset *nroot, struct cpuset **nsetp,
1144 struct setlist *cpusets, struct domainlist *domainlist)
1146 struct domainset ndomain;
1148 struct cpuset *pbase;
1151 pbase = cpuset_getbase(td->td_cpuset);
1153 /* Copy process mask, then further apply the new root mask. */
1154 CPU_COPY(&pbase->cs_mask, &nmask);
1155 CPU_AND(&nmask, &nroot->cs_mask);
1157 domainset_copy(pbase->cs_domain, &ndomain);
1158 DOMAINSET_AND(&ndomain.ds_mask, &set->cs_domain->ds_mask);
1160 /* Policy is too restrictive, will not work. */
1161 if (CPU_EMPTY(&nmask) || DOMAINSET_EMPTY(&ndomain.ds_mask))
1165 * Remove pbase from the freelist in advance, it'll be pushed to
1166 * cpuset_ids on success. We assume here that cpuset_create() will not
1167 * touch pbase on failure, and we just enqueue it back to the freelist
1168 * to remain in a consistent state.
1170 pbase = LIST_FIRST(cpusets);
1171 LIST_REMOVE(pbase, cs_link);
1172 error = cpuset_create(&pbase, set, &nmask);
1174 LIST_INSERT_HEAD(cpusets, pbase, cs_link);
1178 /* Duplicates some work from above... oh well. */
1179 pbase->cs_domain = domainset_shadow(set->cs_domain, &ndomain,
1186 * Handle four cases for updating an entire process.
1188 * 1) Set is non-null and the process is not rebasing onto a new root. This
1189 * reparents all anonymous sets to the provided set and replaces all
1190 * non-anonymous td_cpusets with the provided set.
1191 * 2) Set is non-null and the process is rebasing onto a new root. This
1192 * creates a new base set if the process previously had its own base set,
1193 * then reparents all anonymous sets either to that set or the provided set
1194 * if one was not created. Non-anonymous sets are similarly replaced.
1195 * 3) Mask is non-null. This replaces or creates anonymous sets for every
1196 * thread with the existing base as a parent.
1197 * 4) domain is non-null. This creates anonymous sets for every thread
1198 * and replaces the domain set.
1200 * This is overly complicated because we can't allocate while holding a
1201 * spinlock and spinlocks must be held while changing and examining thread
1205 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1206 struct domainset *domain, bool rebase)
1208 struct setlist freelist;
1209 struct setlist droplist;
1210 struct domainlist domainlist;
1211 struct cpuset *base, *nset, *nroot, *tdroot;
1219 * The algorithm requires two passes due to locking considerations.
1221 * 1) Lookup the process and acquire the locks in the required order.
1222 * 2) If enough cpusets have not been allocated release the locks and
1223 * allocate them. Loop.
1225 cpuset_freelist_init(&freelist, 1);
1226 domainset_freelist_init(&domainlist, 1);
1228 LIST_INIT(&droplist);
1233 nroot = cpuset_getroot(set);
1235 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1238 tdroot = cpuset_getroot(td->td_cpuset);
1239 needed = p->p_numthreads;
1240 if (set != NULL && rebase && tdroot != nroot)
1242 if (nfree >= needed)
1245 if (nfree < needed) {
1246 cpuset_freelist_add(&freelist, needed - nfree);
1247 domainset_freelist_add(&domainlist, needed - nfree);
1251 PROC_LOCK_ASSERT(p, MA_OWNED);
1254 * If we're changing roots and the root set is what has been specified
1255 * as the parent, then we'll check if the process was previously using
1256 * the root set and, if it wasn't, create a new base with the process's
1257 * mask applied to it.
1259 * If the new root is incompatible with the existing mask, then we allow
1260 * the process to take on the new root if and only if they have
1261 * privilege to widen their mask anyways. Unprivileged processes get
1262 * rejected with EDEADLK.
1264 if (set != NULL && rebase && nroot != tdroot) {
1265 cpusetid_t base_id, root_id;
1267 root_id = td->td_ucred->cr_prison->pr_cpuset->cs_id;
1268 base_id = cpuset_getbase(td->td_cpuset)->cs_id;
1270 if (base_id != root_id) {
1271 error = cpuset_setproc_newbase(td, set, nroot, &base,
1272 &freelist, &domainlist);
1273 if (error == EDEADLK &&
1274 priv_check(td, PRIV_SCHED_CPUSET) == 0)
1282 * Now that the appropriate locks are held and we have enough cpusets,
1283 * make sure the operation will succeed before applying changes. The
1284 * proc lock prevents td_cpuset from changing between calls.
1287 FOREACH_THREAD_IN_PROC(p, td) {
1290 error = cpuset_setproc_test_setthread(td->td_cpuset,
1293 error = cpuset_setproc_test_maskthread(td->td_cpuset,
1300 * Replace each thread's cpuset while using deferred release. We
1301 * must do this because the thread lock must be held while operating
1302 * on the thread and this limits the type of operations allowed.
1304 FOREACH_THREAD_IN_PROC(p, td) {
1307 error = cpuset_setproc_setthread(td->td_cpuset, base,
1308 &nset, &freelist, &domainlist);
1310 error = cpuset_setproc_maskthread(td->td_cpuset, mask,
1311 domain, &nset, &freelist, &domainlist);
1316 cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1322 if (base != NULL && base != set)
1324 while ((nset = LIST_FIRST(&droplist)) != NULL)
1325 cpuset_rel_complete(nset);
1326 cpuset_freelist_free(&freelist);
1327 domainset_freelist_free(&domainlist);
1332 bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen)
1340 for (i = 0; i < __bitset_words(setlen); i++) {
1349 if (bufsiz < sizeof(__STRING(ULONG_MAX)))
1351 bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]);
1359 bitset_strscan(struct bitset *set, int setlen, const char *buf)
1364 BIT_ZERO(setlen, set);
1366 for (i = 0; i < __bitset_words(setlen); i++) {
1371 ret = sscanf(p, "%lx", &set->__bits[i]);
1372 if (ret == 0 || ret == -1)
1374 while (isxdigit(*p))
1381 * Return a string representing a valid layout for a cpuset_t object.
1382 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1385 cpusetobj_strprint(char *buf, const cpuset_t *set)
1388 bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set,
1394 * Build a valid cpuset_t object from a string representation.
1395 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1398 cpusetobj_strscan(cpuset_t *set, const char *buf)
1402 if (strlen(buf) > CPUSETBUFSIZ - 1)
1405 p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)];
1413 * Handle a domainset specifier in the sysctl tree. A poiner to a pointer to
1414 * a domainset is in arg1. If the user specifies a valid domainset the
1415 * pointer is updated.
1418 * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred
1421 sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)
1423 char buf[DOMAINSETBUFSIZ];
1424 struct domainset *dset;
1425 struct domainset key;
1426 int policy, prefer, error;
1429 dset = *(struct domainset **)arg1;
1433 p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ,
1434 (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE);
1435 sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer);
1437 sprintf(buf, "<NULL>");
1438 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1439 if (error != 0 || req->newptr == NULL)
1443 * Read in and validate the string.
1445 memset(&key, 0, sizeof(key));
1446 p = &buf[bitset_strscan((struct bitset *)&key.ds_mask,
1447 DOMAINSET_SETSIZE, buf)];
1450 if (sscanf(p, ":%d:%d", &policy, &prefer) != 2)
1452 key.ds_policy = policy;
1453 key.ds_prefer = prefer;
1455 /* Domainset_create() validates the policy.*/
1456 dset = domainset_create(&key);
1459 *(struct domainset **)arg1 = dset;
1465 * Apply an anonymous mask or a domain to a single thread.
1468 _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1470 struct setlist cpusets;
1471 struct domainlist domainlist;
1472 struct cpuset *nset;
1478 cpuset_freelist_init(&cpusets, 1);
1479 domainset_freelist_init(&domainlist, domain != NULL);
1480 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1485 error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
1486 &cpusets, &domainlist);
1488 set = cpuset_update_thread(td, nset);
1494 cpuset_freelist_free(&cpusets);
1495 domainset_freelist_free(&domainlist);
1500 * Apply an anonymous mask to a single thread.
1503 cpuset_setthread(lwpid_t id, cpuset_t *mask)
1506 return _cpuset_setthread(id, mask, NULL);
1510 * Apply new cpumask to the ithread.
1513 cpuset_setithread(lwpid_t id, int cpu)
1519 CPU_COPY(cpuset_root, &mask);
1521 CPU_SET(cpu, &mask);
1522 return _cpuset_setthread(id, &mask, NULL);
1526 * Initialize static domainsets after NUMA information is available. This is
1527 * called before memory allocators are initialized.
1530 domainset_init(void)
1532 struct domainset *dset;
1535 dset = &domainset_roundrobin;
1536 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1537 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1538 dset->ds_prefer = -1;
1539 _domainset_create(dset, NULL);
1541 for (i = 0; i < vm_ndomains; i++) {
1542 dset = &domainset_fixed[i];
1543 DOMAINSET_ZERO(&dset->ds_mask);
1544 DOMAINSET_SET(i, &dset->ds_mask);
1545 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1546 _domainset_create(dset, NULL);
1548 dset = &domainset_prefer[i];
1549 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1550 dset->ds_policy = DOMAINSET_POLICY_PREFER;
1551 dset->ds_prefer = i;
1552 _domainset_create(dset, NULL);
1557 * Create the domainset for cpuset 0, 1 and cpuset 2.
1560 domainset_zero(void)
1562 struct domainset *dset, *tmp;
1564 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
1567 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1568 dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH;
1569 dset->ds_prefer = -1;
1570 curthread->td_domain.dr_policy = _domainset_create(dset, NULL);
1572 domainset_copy(dset, &domainset2);
1573 domainset2.ds_policy = DOMAINSET_POLICY_INTERLEAVE;
1574 kernel_object->domain.dr_policy = _domainset_create(&domainset2, NULL);
1576 /* Remove empty domains from the global policies. */
1577 LIST_FOREACH_SAFE(dset, &cpuset_domains, ds_link, tmp)
1578 if (domainset_empty_vm(dset))
1579 LIST_REMOVE(dset, ds_link);
1583 * Creates system-wide cpusets and the cpuset for thread0 including three
1586 * 0 - The root set which should represent all valid processors in the
1587 * system. This set is immutable.
1588 * 1 - The default set which all processes are a member of until changed.
1589 * This allows an administrator to move all threads off of given cpus to
1590 * dedicate them to high priority tasks or save power etc.
1591 * 2 - The kernel set which allows restriction and policy to be applied only
1592 * to kernel threads and the kernel_object.
1595 cpuset_thread0(void)
1601 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1602 NULL, NULL, UMA_ALIGN_CACHE, 0);
1603 domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1604 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
1607 * Create the root system set (0) for the whole machine. Doesn't use
1608 * cpuset_create() due to NULL parent.
1610 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1611 CPU_COPY(&all_cpus, &set->cs_mask);
1612 LIST_INIT(&set->cs_children);
1613 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
1614 refcount_init(&set->cs_ref, 1);
1615 set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY;
1616 set->cs_domain = &domainset0;
1618 cpuset_root = &set->cs_mask;
1621 * Now derive a default (1), modifiable set from that to give out.
1623 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1624 error = cpuset_init(set, cpuset_zero, NULL, NULL, 1);
1625 KASSERT(error == 0, ("Error creating default set: %d\n", error));
1626 cpuset_default = set;
1628 * Create the kernel set (2).
1630 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1631 error = cpuset_init(set, cpuset_zero, NULL, NULL, 2);
1632 KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
1633 set->cs_domain = &domainset2;
1634 cpuset_kernel = set;
1637 * Initialize the unit allocator. 0 and 1 are allocated above.
1639 cpuset_unr = new_unrhdr(3, INT_MAX, NULL);
1642 * If MD code has not initialized per-domain cpusets, place all
1645 for (i = 0; i < MAXMEMDOM; i++)
1646 if (!CPU_EMPTY(&cpuset_domain[i]))
1648 CPU_COPY(&all_cpus, &cpuset_domain[0]);
1651 return (cpuset_default);
1655 cpuset_kernthread(struct thread *td)
1660 set = td->td_cpuset;
1661 td->td_cpuset = cpuset_ref(cpuset_kernel);
1667 * Create a cpuset, which would be cpuset_create() but
1668 * mark the new 'set' as root.
1670 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
1673 * In case of no error, returns the set in *setp locked with a reference.
1676 cpuset_create_root(struct prison *pr, struct cpuset **setp)
1681 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1682 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1685 error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1689 KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data",
1690 __func__, __LINE__));
1692 /* Mark the set as root. */
1693 set->cs_flags |= CPU_SET_ROOT;
1700 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1704 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1705 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1708 error = cpuset_setproc(p->p_pid, set, NULL, NULL, true);
1716 * In Capability mode, the only accesses that are permitted are to the current
1717 * thread and process' CPU and domain sets.
1720 cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which,
1723 if (IN_CAPABILITY_MODE(td)) {
1724 if (level != CPU_LEVEL_WHICH)
1726 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1729 !(which == CPU_WHICH_TID && id == td->td_tid) &&
1730 !(which == CPU_WHICH_PID && id == td->td_proc->p_pid))
1736 #ifndef _SYS_SYSPROTO_H_
1737 struct cpuset_args {
1742 sys_cpuset(struct thread *td, struct cpuset_args *uap)
1744 struct cpuset *root;
1749 root = cpuset_refroot(td->td_cpuset);
1752 error = cpuset_create(&set, root, &root->cs_mask);
1756 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1758 error = cpuset_setproc(-1, set, NULL, NULL, false);
1763 #ifndef _SYS_SYSPROTO_H_
1764 struct cpuset_setid_args {
1771 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1774 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1778 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1779 id_t id, cpusetid_t setid)
1785 * Presently we only support per-process sets.
1787 if (which != CPU_WHICH_PID)
1789 set = cpuset_lookup(setid, td);
1792 error = cpuset_setproc(id, set, NULL, NULL, false);
1797 #ifndef _SYS_SYSPROTO_H_
1798 struct cpuset_getid_args {
1806 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1809 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1814 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1815 id_t id, cpusetid_t *setid)
1817 struct cpuset *nset;
1824 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1826 error = cpuset_which(which, id, &p, &ttd, &set);
1833 set = cpuset_refbase(ttd->td_cpuset);
1837 case CPU_WHICH_CPUSET:
1838 case CPU_WHICH_JAIL:
1841 case CPU_WHICH_DOMAIN:
1845 case CPU_LEVEL_ROOT:
1846 nset = cpuset_refroot(set);
1850 case CPU_LEVEL_CPUSET:
1852 case CPU_LEVEL_WHICH:
1858 error = copyout(&tmpid, setid, sizeof(tmpid));
1863 #ifndef _SYS_SYSPROTO_H_
1864 struct cpuset_getaffinity_args {
1873 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1876 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
1877 uap->id, uap->cpusetsize, uap->mask));
1881 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1882 id_t id, size_t cpusetsize, cpuset_t *maskp)
1885 struct cpuset *nset;
1892 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1894 error = cpuset_check_capabilities(td, level, which, id);
1898 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1899 error = cpuset_which(which, id, &p, &ttd, &set);
1903 case CPU_LEVEL_ROOT:
1904 case CPU_LEVEL_CPUSET:
1909 set = cpuset_ref(ttd->td_cpuset);
1912 case CPU_WHICH_CPUSET:
1913 case CPU_WHICH_JAIL:
1916 case CPU_WHICH_INTRHANDLER:
1917 case CPU_WHICH_ITHREAD:
1918 case CPU_WHICH_DOMAIN:
1922 if (level == CPU_LEVEL_ROOT)
1923 nset = cpuset_refroot(set);
1925 nset = cpuset_refbase(set);
1926 CPU_COPY(&nset->cs_mask, mask);
1929 case CPU_LEVEL_WHICH:
1933 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1937 FOREACH_THREAD_IN_PROC(p, ttd) {
1939 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1943 case CPU_WHICH_CPUSET:
1944 case CPU_WHICH_JAIL:
1945 CPU_COPY(&set->cs_mask, mask);
1948 case CPU_WHICH_INTRHANDLER:
1949 case CPU_WHICH_ITHREAD:
1950 error = intr_getaffinity(id, which, mask);
1952 case CPU_WHICH_DOMAIN:
1953 if (id < 0 || id >= MAXMEMDOM)
1956 CPU_COPY(&cpuset_domain[id], mask);
1969 error = copyout(mask, maskp, size);
1975 #ifndef _SYS_SYSPROTO_H_
1976 struct cpuset_setaffinity_args {
1981 const cpuset_t *mask;
1985 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1988 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
1989 uap->id, uap->cpusetsize, uap->mask));
1993 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1994 id_t id, size_t cpusetsize, const cpuset_t *maskp)
1996 struct cpuset *nset;
2003 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
2005 error = cpuset_check_capabilities(td, level, which, id);
2008 mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
2009 error = copyin(maskp, mask, cpusetsize);
2013 * Verify that no high bits are set.
2015 if (cpusetsize > sizeof(cpuset_t)) {
2019 end = cp = (char *)&mask->__bits;
2021 cp += sizeof(cpuset_t);
2028 if (CPU_EMPTY(mask)) {
2033 case CPU_LEVEL_ROOT:
2034 case CPU_LEVEL_CPUSET:
2035 error = cpuset_which(which, id, &p, &ttd, &set);
2042 set = cpuset_ref(ttd->td_cpuset);
2046 case CPU_WHICH_CPUSET:
2047 case CPU_WHICH_JAIL:
2050 case CPU_WHICH_INTRHANDLER:
2051 case CPU_WHICH_ITHREAD:
2052 case CPU_WHICH_DOMAIN:
2056 if (level == CPU_LEVEL_ROOT)
2057 nset = cpuset_refroot(set);
2059 nset = cpuset_refbase(set);
2060 error = cpuset_modify(nset, mask);
2064 case CPU_LEVEL_WHICH:
2067 error = cpuset_setthread(id, mask);
2070 error = cpuset_setproc(id, NULL, mask, NULL, false);
2072 case CPU_WHICH_CPUSET:
2073 case CPU_WHICH_JAIL:
2074 error = cpuset_which(which, id, &p, &ttd, &set);
2076 error = cpuset_modify(set, mask);
2081 case CPU_WHICH_INTRHANDLER:
2082 case CPU_WHICH_ITHREAD:
2083 error = intr_setaffinity(id, which, mask);
2099 #ifndef _SYS_SYSPROTO_H_
2100 struct cpuset_getdomain_args {
2104 size_t domainsetsize;
2110 sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
2113 return (kern_cpuset_getdomain(td, uap->level, uap->which,
2114 uap->id, uap->domainsetsize, uap->mask, uap->policy));
2118 kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2119 id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp)
2121 struct domainset outset;
2123 struct cpuset *nset;
2125 struct domainset *dset;
2130 if (domainsetsize < sizeof(domainset_t) ||
2131 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2133 error = cpuset_check_capabilities(td, level, which, id);
2136 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2137 bzero(&outset, sizeof(outset));
2138 error = cpuset_which(which, id, &p, &ttd, &set);
2142 case CPU_LEVEL_ROOT:
2143 case CPU_LEVEL_CPUSET:
2148 set = cpuset_ref(ttd->td_cpuset);
2151 case CPU_WHICH_CPUSET:
2152 case CPU_WHICH_JAIL:
2155 case CPU_WHICH_INTRHANDLER:
2156 case CPU_WHICH_ITHREAD:
2157 case CPU_WHICH_DOMAIN:
2161 if (level == CPU_LEVEL_ROOT)
2162 nset = cpuset_refroot(set);
2164 nset = cpuset_refbase(set);
2165 domainset_copy(nset->cs_domain, &outset);
2168 case CPU_LEVEL_WHICH:
2172 domainset_copy(ttd->td_cpuset->cs_domain, &outset);
2176 FOREACH_THREAD_IN_PROC(p, ttd) {
2178 dset = ttd->td_cpuset->cs_domain;
2179 /* Show all domains in the proc. */
2180 DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
2181 /* Last policy wins. */
2182 outset.ds_policy = dset->ds_policy;
2183 outset.ds_prefer = dset->ds_prefer;
2187 case CPU_WHICH_CPUSET:
2188 case CPU_WHICH_JAIL:
2189 domainset_copy(set->cs_domain, &outset);
2192 case CPU_WHICH_INTRHANDLER:
2193 case CPU_WHICH_ITHREAD:
2194 case CPU_WHICH_DOMAIN:
2208 * Translate prefer into a set containing only the preferred domain,
2209 * not the entire fallback set.
2211 if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
2212 DOMAINSET_ZERO(&outset.ds_mask);
2213 DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
2215 DOMAINSET_COPY(&outset.ds_mask, mask);
2217 error = copyout(mask, maskp, domainsetsize);
2219 if (suword32(policyp, outset.ds_policy) != 0)
2226 #ifndef _SYS_SYSPROTO_H_
2227 struct cpuset_setdomain_args {
2231 size_t domainsetsize;
2237 sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
2240 return (kern_cpuset_setdomain(td, uap->level, uap->which,
2241 uap->id, uap->domainsetsize, uap->mask, uap->policy));
2245 kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2246 id_t id, size_t domainsetsize, const domainset_t *maskp, int policy)
2248 struct cpuset *nset;
2252 struct domainset domain;
2256 if (domainsetsize < sizeof(domainset_t) ||
2257 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2259 if (policy <= DOMAINSET_POLICY_INVALID ||
2260 policy > DOMAINSET_POLICY_MAX)
2262 error = cpuset_check_capabilities(td, level, which, id);
2265 memset(&domain, 0, sizeof(domain));
2266 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2267 error = copyin(maskp, mask, domainsetsize);
2271 * Verify that no high bits are set.
2273 if (domainsetsize > sizeof(domainset_t)) {
2277 end = cp = (char *)&mask->__bits;
2278 end += domainsetsize;
2279 cp += sizeof(domainset_t);
2286 if (DOMAINSET_EMPTY(mask)) {
2290 DOMAINSET_COPY(mask, &domain.ds_mask);
2291 domain.ds_policy = policy;
2294 * Sanitize the provided mask.
2296 if (!DOMAINSET_SUBSET(&all_domains, &domain.ds_mask)) {
2301 /* Translate preferred policy into a mask and fallback. */
2302 if (policy == DOMAINSET_POLICY_PREFER) {
2303 /* Only support a single preferred domain. */
2304 if (DOMAINSET_COUNT(&domain.ds_mask) != 1) {
2308 domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
2309 /* This will be constrained by domainset_shadow(). */
2310 DOMAINSET_COPY(&all_domains, &domain.ds_mask);
2314 * When given an impossible policy, fall back to interleaving
2315 * across all domains.
2317 if (domainset_empty_vm(&domain))
2318 domainset_copy(&domainset2, &domain);
2321 case CPU_LEVEL_ROOT:
2322 case CPU_LEVEL_CPUSET:
2323 error = cpuset_which(which, id, &p, &ttd, &set);
2330 set = cpuset_ref(ttd->td_cpuset);
2334 case CPU_WHICH_CPUSET:
2335 case CPU_WHICH_JAIL:
2338 case CPU_WHICH_INTRHANDLER:
2339 case CPU_WHICH_ITHREAD:
2340 case CPU_WHICH_DOMAIN:
2344 if (level == CPU_LEVEL_ROOT)
2345 nset = cpuset_refroot(set);
2347 nset = cpuset_refbase(set);
2348 error = cpuset_modify_domain(nset, &domain);
2352 case CPU_LEVEL_WHICH:
2355 error = _cpuset_setthread(id, NULL, &domain);
2358 error = cpuset_setproc(id, NULL, NULL, &domain, false);
2360 case CPU_WHICH_CPUSET:
2361 case CPU_WHICH_JAIL:
2362 error = cpuset_which(which, id, &p, &ttd, &set);
2364 error = cpuset_modify_domain(set, &domain);
2369 case CPU_WHICH_INTRHANDLER:
2370 case CPU_WHICH_ITHREAD:
2388 ddb_display_bitset(const struct bitset *set, int size)
2392 for (once = 0, bit = 0; bit < size; bit++) {
2393 if (CPU_ISSET(bit, set)) {
2395 db_printf("%d", bit);
2398 db_printf(",%d", bit);
2402 db_printf("<none>");
2406 ddb_display_cpuset(const cpuset_t *set)
2408 ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
2412 ddb_display_domainset(const domainset_t *set)
2414 ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
2417 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
2421 LIST_FOREACH(set, &cpuset_ids, cs_link) {
2422 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
2423 set, set->cs_id, refcount_load(&set->cs_ref), set->cs_flags,
2424 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
2425 db_printf(" cpu mask=");
2426 ddb_display_cpuset(&set->cs_mask);
2428 db_printf(" domain policy %d prefer %d mask=",
2429 set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
2430 ddb_display_domainset(&set->cs_domain->ds_mask);
2437 DB_SHOW_COMMAND(domainsets, db_show_domainsets)
2439 struct domainset *set;
2441 LIST_FOREACH(set, &cpuset_domains, ds_link) {
2442 db_printf("set=%p policy %d prefer %d cnt %d\n",
2443 set, set->ds_policy, set->ds_prefer, set->ds_cnt);
2444 db_printf(" mask =");
2445 ddb_display_domainset(&set->ds_mask);