2 * SPDX-License-Identifier: BSD-2-Clause
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>
35 #include "opt_ktrace.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysctl.h>
40 #include <sys/ctype.h>
41 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
49 #include <sys/refcount.h>
50 #include <sys/sched.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysent.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>
64 #include <sys/ktrace.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pageout.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_param.h>
73 #include <vm/vm_phys.h>
74 #include <vm/vm_pagequeue.h>
81 * cpusets provide a mechanism for creating and manipulating sets of
82 * processors for the purpose of constraining the scheduling of threads to
83 * specific processors.
85 * Each process belongs to an identified set, by default this is set 1. Each
86 * thread may further restrict the cpus it may run on to a subset of this
87 * named set. This creates an anonymous set which other threads and processes
88 * may not join by number.
90 * The named set is referred to herein as the 'base' set to avoid ambiguity.
91 * This set is usually a child of a 'root' set while the anonymous set may
92 * simply be referred to as a mask. In the syscall api these are referred to
93 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
95 * Threads inherit their set from their creator whether it be anonymous or
96 * not. This means that anonymous sets are immutable because they may be
97 * shared. To modify an anonymous set a new set is created with the desired
98 * mask and the same parent as the existing anonymous set. This gives the
99 * illusion of each thread having a private mask.
101 * Via the syscall apis a user may ask to retrieve or modify the root, base,
102 * or mask that is discovered via a pid, tid, or setid. Modifying a set
103 * modifies all numbered and anonymous child sets to comply with the new mask.
104 * Modifying a pid or tid's mask applies only to that tid but must still
105 * exist within the assigned parent set.
107 * A thread may not be assigned to a group separate from other threads in
108 * the process. This is to remove ambiguity when the setid is queried with
109 * a pid argument. There is no other technical limitation.
111 * This somewhat complex arrangement is intended to make it easy for
112 * applications to query available processors and bind their threads to
113 * specific processors while also allowing administrators to dynamically
114 * reprovision by changing sets which apply to groups of processes.
116 * A simple application should not concern itself with sets at all and
117 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
118 * meaning 'curthread'. It may query available cpus for that tid with a
119 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
122 LIST_HEAD(domainlist, domainset);
123 struct domainset __read_mostly domainset_firsttouch;
124 struct domainset __read_mostly domainset_fixed[MAXMEMDOM];
125 struct domainset __read_mostly domainset_interleave;
126 struct domainset __read_mostly domainset_prefer[MAXMEMDOM];
127 struct domainset __read_mostly domainset_roundrobin;
129 static uma_zone_t cpuset_zone;
130 static uma_zone_t domainset_zone;
131 static struct mtx cpuset_lock;
132 static struct setlist cpuset_ids;
133 static struct domainlist cpuset_domains;
134 static struct unrhdr *cpuset_unr;
135 static struct cpuset *cpuset_zero, *cpuset_default, *cpuset_kernel;
136 static struct domainset *domainset0, *domainset2;
137 u_int cpusetsizemin = 1;
139 /* Return the size of cpuset_t at the kernel level */
140 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
141 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
143 /* Return the minimum size of cpuset_t allowed by the kernel */
144 SYSCTL_UINT(_kern_sched, OID_AUTO, cpusetsizemin,
145 CTLFLAG_RD | CTLFLAG_CAPRD, &cpusetsizemin, 0,
146 "The minimum size of cpuset_t allowed by the kernel");
148 cpuset_t *cpuset_root;
149 cpuset_t cpuset_domain[MAXMEMDOM];
151 static int cpuset_which2(cpuwhich_t *, id_t, struct proc **, struct thread **,
153 static int domainset_valid(const struct domainset *, const struct domainset *);
156 * Find the first non-anonymous set starting from 'set'.
158 static struct cpuset *
159 cpuset_getbase(struct cpuset *set)
162 if (set->cs_id == CPUSET_INVALID)
163 set = set->cs_parent;
168 * Walks up the tree from 'set' to find the root.
170 static struct cpuset *
171 cpuset_getroot(struct cpuset *set)
174 while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL)
175 set = set->cs_parent;
180 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
183 cpuset_ref(struct cpuset *set)
186 refcount_acquire(&set->cs_ref);
191 * Walks up the tree from 'set' to find the root. Returns the root
194 static struct cpuset *
195 cpuset_refroot(struct cpuset *set)
198 return (cpuset_ref(cpuset_getroot(set)));
202 * Find the first non-anonymous set starting from 'set'. Returns this set
203 * referenced. May return the passed in set with an extra ref if it is
206 static struct cpuset *
207 cpuset_refbase(struct cpuset *set)
210 return (cpuset_ref(cpuset_getbase(set)));
214 * Release a reference in a context where it is safe to allocate.
217 cpuset_rel(struct cpuset *set)
221 if (refcount_release_if_not_last(&set->cs_ref))
223 mtx_lock_spin(&cpuset_lock);
224 if (!refcount_release(&set->cs_ref)) {
225 mtx_unlock_spin(&cpuset_lock);
228 LIST_REMOVE(set, cs_siblings);
230 if (id != CPUSET_INVALID)
231 LIST_REMOVE(set, cs_link);
232 mtx_unlock_spin(&cpuset_lock);
233 cpuset_rel(set->cs_parent);
234 uma_zfree(cpuset_zone, set);
235 if (id != CPUSET_INVALID)
236 free_unr(cpuset_unr, id);
240 * Deferred release must be used when in a context that is not safe to
241 * allocate/free. This places any unreferenced sets on the list 'head'.
244 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
247 if (refcount_release_if_not_last(&set->cs_ref))
249 mtx_lock_spin(&cpuset_lock);
250 if (!refcount_release(&set->cs_ref)) {
251 mtx_unlock_spin(&cpuset_lock);
254 LIST_REMOVE(set, cs_siblings);
255 if (set->cs_id != CPUSET_INVALID)
256 LIST_REMOVE(set, cs_link);
257 LIST_INSERT_HEAD(head, set, cs_link);
258 mtx_unlock_spin(&cpuset_lock);
262 * Complete a deferred release. Removes the set from the list provided to
266 cpuset_rel_complete(struct cpuset *set)
271 LIST_REMOVE(set, cs_link);
272 cpuset_rel(set->cs_parent);
273 uma_zfree(cpuset_zone, set);
274 if (id != CPUSET_INVALID)
275 free_unr(cpuset_unr, id);
279 * Find a set based on an id. Returns it with a ref.
281 static struct cpuset *
282 cpuset_lookup(cpusetid_t setid, struct thread *td)
286 if (setid == CPUSET_INVALID)
288 mtx_lock_spin(&cpuset_lock);
289 LIST_FOREACH(set, &cpuset_ids, cs_link)
290 if (set->cs_id == setid)
294 mtx_unlock_spin(&cpuset_lock);
296 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
297 if (set != NULL && jailed(td->td_ucred)) {
298 struct cpuset *jset, *tset;
300 jset = td->td_ucred->cr_prison->pr_cpuset;
301 for (tset = set; tset != NULL; tset = tset->cs_parent)
314 * Initialize a set in the space provided in 'set' with the provided parameters.
315 * The set is returned with a single ref. May return EDEADLK if the set
316 * will have no valid cpu based on restrictions from the parent.
319 cpuset_init(struct cpuset *set, struct cpuset *parent,
320 const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
324 domain = parent->cs_domain;
326 mask = &parent->cs_mask;
327 if (!CPU_OVERLAP(&parent->cs_mask, mask))
329 /* The domain must be prepared ahead of time. */
330 if (!domainset_valid(parent->cs_domain, domain))
332 CPU_COPY(mask, &set->cs_mask);
333 LIST_INIT(&set->cs_children);
334 refcount_init(&set->cs_ref, 1);
336 mtx_lock_spin(&cpuset_lock);
337 set->cs_domain = domain;
338 CPU_AND(&set->cs_mask, &set->cs_mask, &parent->cs_mask);
340 set->cs_parent = cpuset_ref(parent);
341 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
342 if (set->cs_id != CPUSET_INVALID)
343 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
344 mtx_unlock_spin(&cpuset_lock);
350 * Create a new non-anonymous set with the requested parent and mask. May
351 * return failures if the mask is invalid or a new number can not be
354 * If *setp is not NULL, then it will be used as-is. The caller must take
355 * into account that *setp will be inserted at the head of cpuset_ids and
356 * plan any potentially conflicting cs_link usage accordingly.
359 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
366 id = alloc_unr(cpuset_unr);
369 dofree = (*setp == NULL);
373 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
374 error = cpuset_init(set, parent, mask, NULL, id);
377 free_unr(cpuset_unr, id);
379 uma_zfree(cpuset_zone, set);
385 cpuset_freelist_add(struct setlist *list, int count)
390 for (i = 0; i < count; i++) {
391 set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
392 LIST_INSERT_HEAD(list, set, cs_link);
397 cpuset_freelist_init(struct setlist *list, int count)
401 cpuset_freelist_add(list, count);
405 cpuset_freelist_free(struct setlist *list)
409 while ((set = LIST_FIRST(list)) != NULL) {
410 LIST_REMOVE(set, cs_link);
411 uma_zfree(cpuset_zone, set);
416 domainset_freelist_add(struct domainlist *list, int count)
418 struct domainset *set;
421 for (i = 0; i < count; i++) {
422 set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
423 LIST_INSERT_HEAD(list, set, ds_link);
428 domainset_freelist_init(struct domainlist *list, int count)
432 domainset_freelist_add(list, count);
436 domainset_freelist_free(struct domainlist *list)
438 struct domainset *set;
440 while ((set = LIST_FIRST(list)) != NULL) {
441 LIST_REMOVE(set, ds_link);
442 uma_zfree(domainset_zone, set);
446 /* Copy a domainset preserving mask and policy. */
448 domainset_copy(const struct domainset *from, struct domainset *to)
451 DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
452 to->ds_policy = from->ds_policy;
453 to->ds_prefer = from->ds_prefer;
456 /* Return 1 if mask and policy are equal, otherwise 0. */
458 domainset_equal(const struct domainset *one, const struct domainset *two)
461 return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
462 one->ds_policy == two->ds_policy &&
463 one->ds_prefer == two->ds_prefer);
466 /* Return 1 if child is a valid subset of parent. */
468 domainset_valid(const struct domainset *parent, const struct domainset *child)
470 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
471 return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
472 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
476 domainset_restrict(const struct domainset *parent,
477 const struct domainset *child)
479 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
480 return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
481 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
485 * Lookup or create a domainset. The key is provided in ds_mask and
486 * ds_policy. If the domainset does not yet exist the storage in
487 * 'domain' is used to insert. Otherwise this storage is freed to the
488 * domainset_zone and the existing domainset is returned.
490 static struct domainset *
491 _domainset_create(struct domainset *domain, struct domainlist *freelist)
493 struct domainset *ndomain;
496 KASSERT(domain->ds_cnt <= vm_ndomains,
497 ("invalid domain count in domainset %p", domain));
498 KASSERT(domain->ds_policy != DOMAINSET_POLICY_PREFER ||
499 domain->ds_prefer < vm_ndomains,
500 ("invalid preferred domain in domains %p", domain));
502 mtx_lock_spin(&cpuset_lock);
503 LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
504 if (domainset_equal(ndomain, domain))
507 * If the domain does not yet exist we insert it and initialize
508 * various iteration helpers which are not part of the key.
510 if (ndomain == NULL) {
511 LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
512 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
513 for (i = 0, j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
514 if (DOMAINSET_ISSET(i, &domain->ds_mask))
515 domain->ds_order[j++] = i;
517 mtx_unlock_spin(&cpuset_lock);
520 if (freelist != NULL)
521 LIST_INSERT_HEAD(freelist, domain, ds_link);
523 uma_zfree(domainset_zone, domain);
529 * Are any of the domains in the mask empty? If so, silently
530 * remove them and update the domainset accordingly. If only empty
531 * domains are present, we must return failure.
534 domainset_empty_vm(struct domainset *domain)
539 DOMAINSET_ZERO(&empty);
540 for (i = 0; i < vm_ndomains; i++)
541 if (VM_DOMAIN_EMPTY(i))
542 DOMAINSET_SET(i, &empty);
543 if (DOMAINSET_SUBSET(&empty, &domain->ds_mask))
546 /* Remove empty domains from the set and recompute. */
547 DOMAINSET_ANDNOT(&domain->ds_mask, &empty);
548 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
549 for (i = j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
550 if (DOMAINSET_ISSET(i, &domain->ds_mask))
551 domain->ds_order[j++] = i;
553 /* Convert a PREFER policy referencing an empty domain to RR. */
554 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
555 DOMAINSET_ISSET(domain->ds_prefer, &empty)) {
556 domain->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
557 domain->ds_prefer = -1;
564 * Create or lookup a domainset based on the key held in 'domain'.
567 domainset_create(const struct domainset *domain)
569 struct domainset *ndomain;
572 * Validate the policy. It must specify a useable policy number with
573 * only valid domains. Preferred must include the preferred domain
576 if (domain->ds_policy <= DOMAINSET_POLICY_INVALID ||
577 domain->ds_policy > DOMAINSET_POLICY_MAX)
579 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
580 !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask))
582 if (!DOMAINSET_SUBSET(&domainset0->ds_mask, &domain->ds_mask))
584 ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
585 domainset_copy(domain, ndomain);
586 return _domainset_create(ndomain, NULL);
590 * Update thread domainset pointers.
593 domainset_notify(void)
598 sx_slock(&allproc_lock);
599 FOREACH_PROC_IN_SYSTEM(p) {
601 if (p->p_state == PRS_NEW) {
605 FOREACH_THREAD_IN_PROC(p, td) {
607 td->td_domain.dr_policy = td->td_cpuset->cs_domain;
612 sx_sunlock(&allproc_lock);
613 kernel_object->domain.dr_policy = cpuset_kernel->cs_domain;
617 * Create a new set that is a subset of a parent.
619 static struct domainset *
620 domainset_shadow(const struct domainset *pdomain,
621 const struct domainset *domain, struct domainlist *freelist)
623 struct domainset *ndomain;
625 ndomain = LIST_FIRST(freelist);
626 LIST_REMOVE(ndomain, ds_link);
629 * Initialize the key from the request.
631 domainset_copy(domain, ndomain);
634 * Restrict the key by the parent.
636 DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
638 return _domainset_create(ndomain, freelist);
642 * Recursively check for errors that would occur from applying mask to
643 * the tree of sets starting at 'set'. Checks for sets that would become
644 * empty as well as RDONLY flags.
647 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int augment_mask)
653 mtx_assert(&cpuset_lock, MA_OWNED);
654 if (set->cs_flags & CPU_SET_RDONLY)
657 CPU_AND(&newmask, &set->cs_mask, mask);
659 CPU_COPY(mask, &newmask);
661 if (CPU_EMPTY(&newmask))
664 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
665 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
671 * Applies the mask 'mask' without checking for empty sets or permissions.
674 cpuset_update(struct cpuset *set, cpuset_t *mask)
678 mtx_assert(&cpuset_lock, MA_OWNED);
679 CPU_AND(&set->cs_mask, &set->cs_mask, mask);
680 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
681 cpuset_update(nset, &set->cs_mask);
687 * Modify the set 'set' to use a copy of the mask provided. Apply this new
688 * mask to restrict all children in the tree. Checks for validity before
689 * applying the changes.
692 cpuset_modify(struct cpuset *set, cpuset_t *mask)
697 error = priv_check(curthread, PRIV_SCHED_CPUSET);
701 * In case we are called from within the jail,
702 * we do not allow modifying the dedicated root
703 * cpuset of the jail but may still allow to
704 * change child sets, including subordinate jails'
707 if ((set->cs_flags & CPU_SET_ROOT) != 0 &&
708 jailed(curthread->td_ucred) &&
709 set == curthread->td_ucred->cr_prison->pr_cpuset)
712 * Verify that we have access to this set of
715 if ((set->cs_flags & (CPU_SET_ROOT | CPU_SET_RDONLY)) == CPU_SET_ROOT) {
716 KASSERT(set->cs_parent != NULL,
717 ("jail.cpuset=%d is not a proper child of parent jail's root.",
721 * cpuset_getroot() cannot work here due to how top-level jail
722 * roots are constructed. Top-level jails are parented to
723 * thread0's cpuset (i.e. cpuset 1) rather than the system root.
725 root = set->cs_parent;
727 root = cpuset_getroot(set);
729 mtx_lock_spin(&cpuset_lock);
730 if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
734 error = cpuset_testupdate(set, mask, 0);
737 CPU_COPY(mask, &set->cs_mask);
738 cpuset_update(set, mask);
740 mtx_unlock_spin(&cpuset_lock);
746 * Recursively check for errors that would occur from applying mask to
747 * the tree of sets starting at 'set'. Checks for sets that would become
748 * empty as well as RDONLY flags.
751 cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
752 struct domainset *orig, int *count, int augment_mask __unused)
755 struct domainset *domain;
756 struct domainset newset;
759 mtx_assert(&cpuset_lock, MA_OWNED);
760 if (set->cs_flags & CPU_SET_RDONLY)
762 domain = set->cs_domain;
763 domainset_copy(domain, &newset);
764 if (!domainset_equal(domain, orig)) {
765 if (!domainset_restrict(domain, dset))
767 DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
768 /* Count the number of domains that are changing. */
772 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
773 if ((error = cpuset_testupdate_domain(nset, &newset, domain,
780 * Applies the mask 'mask' without checking for empty sets or permissions.
783 cpuset_update_domain(struct cpuset *set, struct domainset *domain,
784 struct domainset *orig, struct domainlist *domains)
788 mtx_assert(&cpuset_lock, MA_OWNED);
790 * If this domainset has changed from the parent we must calculate
791 * a new set. Otherwise it simply inherits from the parent. When
792 * we inherit from the parent we get a new mask and policy. If the
793 * set is modified from the parent we keep the policy and only
796 if (set->cs_domain != orig) {
797 orig = set->cs_domain;
798 set->cs_domain = domainset_shadow(domain, orig, domains);
800 set->cs_domain = domain;
801 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
802 cpuset_update_domain(nset, set->cs_domain, orig, domains);
808 * Modify the set 'set' to use a copy the domainset provided. Apply this new
809 * mask to restrict all children in the tree. Checks for validity before
810 * applying the changes.
813 cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
815 struct domainlist domains;
816 struct domainset temp;
817 struct domainset *dset;
819 int ndomains, needed;
822 error = priv_check(curthread, PRIV_SCHED_CPUSET);
826 * In case we are called from within the jail
827 * we do not allow modifying the dedicated root
828 * cpuset of the jail but may still allow to
831 if (jailed(curthread->td_ucred) &&
832 set->cs_flags & CPU_SET_ROOT)
834 domainset_freelist_init(&domains, 0);
835 domain = domainset_create(domain);
838 mtx_lock_spin(&cpuset_lock);
840 root = cpuset_getroot(set);
841 dset = root->cs_domain;
843 * Verify that we have access to this set of domains.
845 if (!domainset_valid(dset, domain)) {
850 * If applying prefer we keep the current set as the fallback.
852 if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
853 DOMAINSET_COPY(&set->cs_domain->ds_mask,
856 * Determine whether we can apply this set of domains and
857 * how many new domain structures it will require.
859 domainset_copy(domain, &temp);
861 error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
865 if (ndomains >= needed)
868 /* Dropping the lock; we'll need to re-evaluate again. */
869 mtx_unlock_spin(&cpuset_lock);
870 domainset_freelist_add(&domains, needed - ndomains);
872 mtx_lock_spin(&cpuset_lock);
874 dset = set->cs_domain;
875 cpuset_update_domain(set, domain, dset, &domains);
877 mtx_unlock_spin(&cpuset_lock);
878 domainset_freelist_free(&domains);
886 * Resolve the 'which' parameter of several cpuset apis.
888 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
889 * checks for permission via p_cansched().
891 * For WHICH_SET returns a valid set with a new reference.
893 * -1 may be supplied for any argument to mean the current proc/thread or
894 * the base set of the current thread. May fail with ESRCH/EPERM.
897 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
898 struct cpuset **setp)
915 if ((p = pfind(id)) == NULL)
930 case CPU_WHICH_TIDPID:
935 } else if (id > PID_MAX) {
946 case CPU_WHICH_CPUSET:
948 thread_lock(curthread);
949 set = cpuset_refbase(curthread->td_cpuset);
950 thread_unlock(curthread);
952 set = cpuset_lookup(id, curthread);
960 /* Find `set' for prison with given id. */
963 sx_slock(&allprison_lock);
964 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
965 sx_sunlock(&allprison_lock);
968 cpuset_ref(pr->pr_cpuset);
969 *setp = pr->pr_cpuset;
970 mtx_unlock(&pr->pr_mtx);
974 case CPU_WHICH_DOMAIN:
979 error = p_cansched(curthread, p);
985 td = FIRST_THREAD_IN_PROC(p);
992 cpuset_which2(cpuwhich_t *which, id_t id, struct proc **pp, struct thread **tdp,
993 struct cpuset **setp)
996 if (*which == CPU_WHICH_TIDPID) {
997 if (id == -1 || id > PID_MAX)
998 *which = CPU_WHICH_TID;
1000 *which = CPU_WHICH_PID;
1002 return (cpuset_which(*which, id, pp, tdp, setp));
1006 cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
1007 const struct domainset *domain)
1009 struct cpuset *parent;
1010 struct domainset *dset;
1012 parent = cpuset_getbase(set);
1014 * If we are restricting a cpu mask it must be a subset of the
1015 * parent or invalid CPUs have been specified.
1017 if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
1021 * If we are restricting a domain mask it must be a subset of the
1022 * parent or invalid domains have been specified.
1024 dset = parent->cs_domain;
1025 if (domain != NULL && !domainset_valid(dset, domain))
1032 * Create an anonymous set with the provided mask in the space provided by
1033 * 'nset'. If the passed in set is anonymous we use its parent otherwise
1034 * the new set is a child of 'set'.
1037 cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
1038 const cpuset_t *mask, const struct domainset *domain,
1039 struct setlist *cpusets, struct domainlist *domains)
1041 struct cpuset *parent;
1042 struct cpuset *nset;
1043 struct domainset *dset;
1044 struct domainset *d;
1047 error = cpuset_testshadow(set, mask, domain);
1051 parent = cpuset_getbase(set);
1052 dset = parent->cs_domain;
1054 mask = &set->cs_mask;
1056 d = domainset_shadow(dset, domain, domains);
1059 nset = LIST_FIRST(cpusets);
1060 error = cpuset_init(nset, parent, mask, d, CPUSET_INVALID);
1062 LIST_REMOVE(nset, cs_link);
1068 static struct cpuset *
1069 cpuset_update_thread(struct thread *td, struct cpuset *nset)
1071 struct cpuset *tdset;
1073 tdset = td->td_cpuset;
1074 td->td_cpuset = nset;
1075 td->td_domain.dr_policy = nset->cs_domain;
1082 cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
1083 struct domainset *domain)
1085 struct cpuset *parent;
1087 parent = cpuset_getbase(tdset);
1089 mask = &tdset->cs_mask;
1091 domain = tdset->cs_domain;
1092 return cpuset_testshadow(parent, mask, domain);
1096 cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
1097 struct domainset *domain, struct cpuset **nsetp,
1098 struct setlist *freelist, struct domainlist *domainlist)
1100 struct cpuset *parent;
1102 parent = cpuset_getbase(tdset);
1104 mask = &tdset->cs_mask;
1106 domain = tdset->cs_domain;
1107 return cpuset_shadow(parent, nsetp, mask, domain, freelist,
1112 cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
1113 cpuset_t *mask, struct domainset *domain)
1115 struct cpuset *parent;
1117 parent = cpuset_getbase(tdset);
1120 * If the thread restricted its mask then apply that same
1121 * restriction to the new set, otherwise take it wholesale.
1123 if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
1124 CPU_AND(mask, &tdset->cs_mask, &set->cs_mask);
1126 CPU_COPY(&set->cs_mask, mask);
1129 * If the thread restricted the domain then we apply the
1130 * restriction to the new set but retain the policy.
1132 if (tdset->cs_domain != parent->cs_domain) {
1133 domainset_copy(tdset->cs_domain, domain);
1134 DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
1136 domainset_copy(set->cs_domain, domain);
1138 if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
1145 cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
1147 struct domainset domain;
1150 if (tdset->cs_id != CPUSET_INVALID)
1152 return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1156 cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
1157 struct cpuset **nsetp, struct setlist *freelist,
1158 struct domainlist *domainlist)
1160 struct domainset domain;
1165 * If we're replacing on a thread that has not constrained the
1166 * original set we can simply accept the new set.
1168 if (tdset->cs_id != CPUSET_INVALID) {
1169 *nsetp = cpuset_ref(set);
1172 error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1176 return cpuset_shadow(set, nsetp, &mask, &domain, freelist,
1181 cpuset_setproc_newbase(struct thread *td, struct cpuset *set,
1182 struct cpuset *nroot, struct cpuset **nsetp,
1183 struct setlist *cpusets, struct domainlist *domainlist)
1185 struct domainset ndomain;
1187 struct cpuset *pbase;
1190 pbase = cpuset_getbase(td->td_cpuset);
1192 /* Copy process mask, then further apply the new root mask. */
1193 CPU_AND(&nmask, &pbase->cs_mask, &nroot->cs_mask);
1195 domainset_copy(pbase->cs_domain, &ndomain);
1196 DOMAINSET_AND(&ndomain.ds_mask, &set->cs_domain->ds_mask);
1198 /* Policy is too restrictive, will not work. */
1199 if (CPU_EMPTY(&nmask) || DOMAINSET_EMPTY(&ndomain.ds_mask))
1203 * Remove pbase from the freelist in advance, it'll be pushed to
1204 * cpuset_ids on success. We assume here that cpuset_create() will not
1205 * touch pbase on failure, and we just enqueue it back to the freelist
1206 * to remain in a consistent state.
1208 pbase = LIST_FIRST(cpusets);
1209 LIST_REMOVE(pbase, cs_link);
1210 error = cpuset_create(&pbase, set, &nmask);
1212 LIST_INSERT_HEAD(cpusets, pbase, cs_link);
1216 /* Duplicates some work from above... oh well. */
1217 pbase->cs_domain = domainset_shadow(set->cs_domain, &ndomain,
1224 * Handle four cases for updating an entire process.
1226 * 1) Set is non-null and the process is not rebasing onto a new root. This
1227 * reparents all anonymous sets to the provided set and replaces all
1228 * non-anonymous td_cpusets with the provided set.
1229 * 2) Set is non-null and the process is rebasing onto a new root. This
1230 * creates a new base set if the process previously had its own base set,
1231 * then reparents all anonymous sets either to that set or the provided set
1232 * if one was not created. Non-anonymous sets are similarly replaced.
1233 * 3) Mask is non-null. This replaces or creates anonymous sets for every
1234 * thread with the existing base as a parent.
1235 * 4) domain is non-null. This creates anonymous sets for every thread
1236 * and replaces the domain set.
1238 * This is overly complicated because we can't allocate while holding a
1239 * spinlock and spinlocks must be held while changing and examining thread
1243 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1244 struct domainset *domain, bool rebase)
1246 struct setlist freelist;
1247 struct setlist droplist;
1248 struct domainlist domainlist;
1249 struct cpuset *base, *nset, *nroot, *tdroot;
1257 * The algorithm requires two passes due to locking considerations.
1259 * 1) Lookup the process and acquire the locks in the required order.
1260 * 2) If enough cpusets have not been allocated release the locks and
1261 * allocate them. Loop.
1263 cpuset_freelist_init(&freelist, 1);
1264 domainset_freelist_init(&domainlist, 1);
1266 LIST_INIT(&droplist);
1271 nroot = cpuset_getroot(set);
1273 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1276 tdroot = cpuset_getroot(td->td_cpuset);
1277 needed = p->p_numthreads;
1278 if (set != NULL && rebase && tdroot != nroot)
1280 if (nfree >= needed)
1283 if (nfree < needed) {
1284 cpuset_freelist_add(&freelist, needed - nfree);
1285 domainset_freelist_add(&domainlist, needed - nfree);
1289 PROC_LOCK_ASSERT(p, MA_OWNED);
1292 * If we're changing roots and the root set is what has been specified
1293 * as the parent, then we'll check if the process was previously using
1294 * the root set and, if it wasn't, create a new base with the process's
1295 * mask applied to it.
1297 * If the new root is incompatible with the existing mask, then we allow
1298 * the process to take on the new root if and only if they have
1299 * privilege to widen their mask anyways. Unprivileged processes get
1300 * rejected with EDEADLK.
1302 if (set != NULL && rebase && nroot != tdroot) {
1303 cpusetid_t base_id, root_id;
1305 root_id = td->td_ucred->cr_prison->pr_cpuset->cs_id;
1306 base_id = cpuset_getbase(td->td_cpuset)->cs_id;
1308 if (base_id != root_id) {
1309 error = cpuset_setproc_newbase(td, set, nroot, &base,
1310 &freelist, &domainlist);
1311 if (error == EDEADLK &&
1312 priv_check(td, PRIV_SCHED_CPUSET) == 0)
1320 * Now that the appropriate locks are held and we have enough cpusets,
1321 * make sure the operation will succeed before applying changes. The
1322 * proc lock prevents td_cpuset from changing between calls.
1325 FOREACH_THREAD_IN_PROC(p, td) {
1328 error = cpuset_setproc_test_setthread(td->td_cpuset,
1331 error = cpuset_setproc_test_maskthread(td->td_cpuset,
1338 * Replace each thread's cpuset while using deferred release. We
1339 * must do this because the thread lock must be held while operating
1340 * on the thread and this limits the type of operations allowed.
1342 FOREACH_THREAD_IN_PROC(p, td) {
1345 error = cpuset_setproc_setthread(td->td_cpuset, base,
1346 &nset, &freelist, &domainlist);
1348 error = cpuset_setproc_maskthread(td->td_cpuset, mask,
1349 domain, &nset, &freelist, &domainlist);
1354 cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1360 if (base != NULL && base != set)
1362 while ((nset = LIST_FIRST(&droplist)) != NULL)
1363 cpuset_rel_complete(nset);
1364 cpuset_freelist_free(&freelist);
1365 domainset_freelist_free(&domainlist);
1370 bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen)
1378 for (i = 0; i < __bitset_words(setlen); i++) {
1387 if (bufsiz < sizeof(__STRING(ULONG_MAX)))
1389 bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]);
1397 bitset_strscan(struct bitset *set, int setlen, const char *buf)
1402 BIT_ZERO(setlen, set);
1404 for (i = 0; i < __bitset_words(setlen); i++) {
1409 ret = sscanf(p, "%lx", &set->__bits[i]);
1410 if (ret == 0 || ret == -1)
1412 while (isxdigit(*p))
1419 * Return a string representing a valid layout for a cpuset_t object.
1420 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1423 cpusetobj_strprint(char *buf, const cpuset_t *set)
1426 bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set,
1432 * Build a valid cpuset_t object from a string representation.
1433 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1436 cpusetobj_strscan(cpuset_t *set, const char *buf)
1440 if (strlen(buf) > CPUSETBUFSIZ - 1)
1443 p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)];
1451 * Handle a domainset specifier in the sysctl tree. A poiner to a pointer to
1452 * a domainset is in arg1. If the user specifies a valid domainset the
1453 * pointer is updated.
1456 * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred
1459 sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)
1461 char buf[DOMAINSETBUFSIZ];
1462 struct domainset *dset;
1463 struct domainset key;
1464 int policy, prefer, error;
1467 dset = *(struct domainset **)arg1;
1471 p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ,
1472 (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE);
1473 sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer);
1475 sprintf(buf, "<NULL>");
1476 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1477 if (error != 0 || req->newptr == NULL)
1481 * Read in and validate the string.
1483 memset(&key, 0, sizeof(key));
1484 p = &buf[bitset_strscan((struct bitset *)&key.ds_mask,
1485 DOMAINSET_SETSIZE, buf)];
1488 if (sscanf(p, ":%d:%d", &policy, &prefer) != 2)
1490 key.ds_policy = policy;
1491 key.ds_prefer = prefer;
1493 /* Domainset_create() validates the policy.*/
1494 dset = domainset_create(&key);
1497 *(struct domainset **)arg1 = dset;
1503 * Apply an anonymous mask or a domain to a single thread.
1506 _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1508 struct setlist cpusets;
1509 struct domainlist domainlist;
1510 struct cpuset *nset;
1516 cpuset_freelist_init(&cpusets, 1);
1517 domainset_freelist_init(&domainlist, domain != NULL);
1518 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1523 error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
1524 &cpusets, &domainlist);
1526 set = cpuset_update_thread(td, nset);
1532 cpuset_freelist_free(&cpusets);
1533 domainset_freelist_free(&domainlist);
1538 * Apply an anonymous mask to a single thread.
1541 cpuset_setthread(lwpid_t id, cpuset_t *mask)
1544 return _cpuset_setthread(id, mask, NULL);
1548 * Apply new cpumask to the ithread.
1551 cpuset_setithread(lwpid_t id, int cpu)
1557 CPU_COPY(cpuset_root, &mask);
1559 CPU_SET(cpu, &mask);
1560 return _cpuset_setthread(id, &mask, NULL);
1564 * Initialize static domainsets after NUMA information is available. This is
1565 * called before memory allocators are initialized.
1568 domainset_init(void)
1570 struct domainset *dset;
1573 dset = &domainset_firsttouch;
1574 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1575 dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH;
1576 dset->ds_prefer = -1;
1577 _domainset_create(dset, NULL);
1579 dset = &domainset_interleave;
1580 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1581 dset->ds_policy = DOMAINSET_POLICY_INTERLEAVE;
1582 dset->ds_prefer = -1;
1583 _domainset_create(dset, NULL);
1585 dset = &domainset_roundrobin;
1586 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1587 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1588 dset->ds_prefer = -1;
1589 _domainset_create(dset, NULL);
1591 for (i = 0; i < vm_ndomains; i++) {
1592 dset = &domainset_fixed[i];
1593 DOMAINSET_ZERO(&dset->ds_mask);
1594 DOMAINSET_SET(i, &dset->ds_mask);
1595 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1596 _domainset_create(dset, NULL);
1598 dset = &domainset_prefer[i];
1599 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1600 dset->ds_policy = DOMAINSET_POLICY_PREFER;
1601 dset->ds_prefer = i;
1602 _domainset_create(dset, NULL);
1607 * Define the domainsets for cpuset 0, 1 and cpuset 2.
1610 domainset_zero(void)
1612 struct domainset *dset, *tmp;
1614 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
1616 domainset0 = &domainset_firsttouch;
1617 curthread->td_domain.dr_policy = domainset0;
1619 domainset2 = &domainset_interleave;
1620 kernel_object->domain.dr_policy = domainset2;
1622 /* Remove empty domains from the global policies. */
1623 LIST_FOREACH_SAFE(dset, &cpuset_domains, ds_link, tmp)
1624 if (domainset_empty_vm(dset))
1625 LIST_REMOVE(dset, ds_link);
1629 * Creates system-wide cpusets and the cpuset for thread0 including three
1632 * 0 - The root set which should represent all valid processors in the
1633 * system. This set is immutable.
1634 * 1 - The default set which all processes are a member of until changed.
1635 * This allows an administrator to move all threads off of given cpus to
1636 * dedicate them to high priority tasks or save power etc.
1637 * 2 - The kernel set which allows restriction and policy to be applied only
1638 * to kernel threads and the kernel_object.
1641 cpuset_thread0(void)
1647 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1648 NULL, NULL, UMA_ALIGN_CACHE, 0);
1649 domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1650 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
1653 * Create the root system set (0) for the whole machine. Doesn't use
1654 * cpuset_create() due to NULL parent.
1656 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1657 CPU_COPY(&all_cpus, &set->cs_mask);
1658 LIST_INIT(&set->cs_children);
1659 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
1660 refcount_init(&set->cs_ref, 1);
1661 set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY;
1662 set->cs_domain = domainset0;
1664 cpuset_root = &set->cs_mask;
1667 * Now derive a default (1), modifiable set from that to give out.
1669 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1670 error = cpuset_init(set, cpuset_zero, NULL, NULL, 1);
1671 KASSERT(error == 0, ("Error creating default set: %d\n", error));
1672 cpuset_default = set;
1674 * Create the kernel set (2).
1676 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1677 error = cpuset_init(set, cpuset_zero, NULL, NULL, 2);
1678 KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
1679 set->cs_domain = domainset2;
1680 cpuset_kernel = set;
1683 * Initialize the unit allocator. 0 and 1 are allocated above.
1685 cpuset_unr = new_unrhdr(3, INT_MAX, NULL);
1688 * If MD code has not initialized per-domain cpusets, place all
1691 for (i = 0; i < MAXMEMDOM; i++)
1692 if (!CPU_EMPTY(&cpuset_domain[i]))
1694 CPU_COPY(&all_cpus, &cpuset_domain[0]);
1697 return (cpuset_default);
1701 cpuset_kernthread(struct thread *td)
1706 set = td->td_cpuset;
1707 td->td_cpuset = cpuset_ref(cpuset_kernel);
1713 * Create a cpuset, which would be cpuset_create() but
1714 * mark the new 'set' as root.
1716 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
1719 * In case of no error, returns the set in *setp locked with a reference.
1722 cpuset_create_root(struct prison *pr, struct cpuset **setp)
1727 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1728 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1731 error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1735 KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data",
1736 __func__, __LINE__));
1738 /* Mark the set as root. */
1739 set->cs_flags |= CPU_SET_ROOT;
1746 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1750 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1751 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1754 error = cpuset_setproc(p->p_pid, set, NULL, NULL, true);
1762 * In Capability mode, the only accesses that are permitted are to the current
1763 * thread and process' CPU and domain sets.
1766 cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which,
1769 if (IN_CAPABILITY_MODE(td)) {
1770 if (level != CPU_LEVEL_WHICH)
1772 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID &&
1773 which != CPU_WHICH_TIDPID)
1775 if (id != -1 && which == CPU_WHICH_TIDPID &&
1776 id != td->td_tid && id != td->td_proc->p_pid)
1779 !(which == CPU_WHICH_TID && id == td->td_tid) &&
1780 !(which == CPU_WHICH_PID && id == td->td_proc->p_pid))
1786 #if defined(__powerpc__)
1788 * TODO: At least powerpc64 and powerpc64le kernels panic with
1789 * exception 0x480 (instruction segment exception) when copyin/copyout,
1790 * are set as a function pointer in cpuset_copy_cb struct and called by
1791 * an external module (like pfsync). Tip: copyin/copyout have an ifunc
1792 * resolver function.
1794 * Bisect of LLVM shows that the behavior changed on LLVM 10.0 with
1795 * https://reviews.llvm.org/rGdc06b0bc9ad055d06535462d91bfc2a744b2f589
1797 * This is a hack/workaround while problem is being discussed with LLVM
1801 cpuset_copyin(const void *uaddr, void *kaddr, size_t len)
1803 return(copyin(uaddr, kaddr, len));
1807 cpuset_copyout(const void *kaddr, void *uaddr, size_t len)
1809 return(copyout(kaddr, uaddr, len));
1812 static const struct cpuset_copy_cb copy_set = {
1813 .cpuset_copyin = cpuset_copyin,
1814 .cpuset_copyout = cpuset_copyout
1817 static const struct cpuset_copy_cb copy_set = {
1818 .cpuset_copyin = copyin,
1819 .cpuset_copyout = copyout
1823 #ifndef _SYS_SYSPROTO_H_
1824 struct cpuset_args {
1829 sys_cpuset(struct thread *td, struct cpuset_args *uap)
1831 struct cpuset *root;
1836 root = cpuset_refroot(td->td_cpuset);
1839 error = cpuset_create(&set, root, &root->cs_mask);
1843 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1845 error = cpuset_setproc(-1, set, NULL, NULL, false);
1850 #ifndef _SYS_SYSPROTO_H_
1851 struct cpuset_setid_args {
1858 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1861 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1865 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1866 id_t id, cpusetid_t setid)
1872 * Presently we only support per-process sets.
1874 if (which != CPU_WHICH_PID)
1876 set = cpuset_lookup(setid, td);
1879 error = cpuset_setproc(id, set, NULL, NULL, false);
1884 #ifndef _SYS_SYSPROTO_H_
1885 struct cpuset_getid_args {
1893 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1896 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1901 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1902 id_t id, cpusetid_t *setid)
1904 struct cpuset *nset;
1911 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1913 error = cpuset_which(which, id, &p, &ttd, &set);
1919 case CPU_WHICH_TIDPID:
1921 set = cpuset_refbase(ttd->td_cpuset);
1925 case CPU_WHICH_CPUSET:
1926 case CPU_WHICH_JAIL:
1929 case CPU_WHICH_DOMAIN:
1933 case CPU_LEVEL_ROOT:
1934 nset = cpuset_refroot(set);
1938 case CPU_LEVEL_CPUSET:
1940 case CPU_LEVEL_WHICH:
1946 error = copyout(&tmpid, setid, sizeof(tmpid));
1951 #ifndef _SYS_SYSPROTO_H_
1952 struct cpuset_getaffinity_args {
1961 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1964 return (user_cpuset_getaffinity(td, uap->level, uap->which,
1965 uap->id, uap->cpusetsize, uap->mask, ©_set));
1969 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1970 id_t id, size_t cpusetsize, cpuset_t *mask)
1973 struct cpuset *nset;
1978 error = cpuset_check_capabilities(td, level, which, id);
1981 error = cpuset_which2(&which, id, &p, &ttd, &set);
1985 case CPU_LEVEL_ROOT:
1986 case CPU_LEVEL_CPUSET:
1991 set = cpuset_ref(ttd->td_cpuset);
1994 case CPU_WHICH_CPUSET:
1995 case CPU_WHICH_JAIL:
1998 case CPU_WHICH_INTRHANDLER:
1999 case CPU_WHICH_ITHREAD:
2000 case CPU_WHICH_DOMAIN:
2003 if (level == CPU_LEVEL_ROOT)
2004 nset = cpuset_refroot(set);
2006 nset = cpuset_refbase(set);
2007 CPU_COPY(&nset->cs_mask, mask);
2010 case CPU_LEVEL_WHICH:
2014 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
2018 FOREACH_THREAD_IN_PROC(p, ttd) {
2020 CPU_OR(mask, mask, &ttd->td_cpuset->cs_mask);
2024 case CPU_WHICH_CPUSET:
2025 case CPU_WHICH_JAIL:
2026 CPU_COPY(&set->cs_mask, mask);
2029 case CPU_WHICH_INTRHANDLER:
2030 case CPU_WHICH_ITHREAD:
2031 error = intr_getaffinity(id, which, mask);
2033 case CPU_WHICH_DOMAIN:
2034 if (id < 0 || id >= MAXMEMDOM)
2037 CPU_COPY(&cpuset_domain[id], mask);
2050 if (cpusetsize < howmany(CPU_FLS(mask), NBBY))
2053 if (KTRPOINT(td, KTR_STRUCT))
2054 ktrcpuset(mask, cpusetsize);
2061 user_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2062 id_t id, size_t cpusetsize, cpuset_t *maskp, const struct cpuset_copy_cb *cb)
2068 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
2069 size = min(cpusetsize, sizeof(cpuset_t));
2070 error = kern_cpuset_getaffinity(td, level, which, id, size, mask);
2072 error = cb->cpuset_copyout(mask, maskp, size);
2075 if (cpusetsize > size) {
2080 end = cp = (char *)&maskp->__bits;
2098 #ifndef _SYS_SYSPROTO_H_
2099 struct cpuset_setaffinity_args {
2104 const cpuset_t *mask;
2108 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
2111 return (user_cpuset_setaffinity(td, uap->level, uap->which,
2112 uap->id, uap->cpusetsize, uap->mask, ©_set));
2116 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2117 id_t id, cpuset_t *mask)
2119 struct cpuset *nset;
2126 if (KTRPOINT(td, KTR_STRUCT))
2127 ktrcpuset(mask, sizeof(cpuset_t));
2129 error = cpuset_check_capabilities(td, level, which, id);
2132 if (CPU_EMPTY(mask))
2135 case CPU_LEVEL_ROOT:
2136 case CPU_LEVEL_CPUSET:
2137 error = cpuset_which(which, id, &p, &ttd, &set);
2143 case CPU_WHICH_TIDPID:
2145 set = cpuset_ref(ttd->td_cpuset);
2149 case CPU_WHICH_CPUSET:
2150 case CPU_WHICH_JAIL:
2153 case CPU_WHICH_INTRHANDLER:
2154 case CPU_WHICH_ITHREAD:
2155 case CPU_WHICH_DOMAIN:
2158 if (level == CPU_LEVEL_ROOT)
2159 nset = cpuset_refroot(set);
2161 nset = cpuset_refbase(set);
2162 error = cpuset_modify(nset, mask);
2166 case CPU_LEVEL_WHICH:
2169 error = cpuset_setthread(id, mask);
2172 error = cpuset_setproc(id, NULL, mask, NULL, false);
2174 case CPU_WHICH_TIDPID:
2175 if (id > PID_MAX || id == -1)
2176 error = cpuset_setthread(id, mask);
2178 error = cpuset_setproc(id, NULL, mask, NULL,
2181 case CPU_WHICH_CPUSET:
2182 case CPU_WHICH_JAIL:
2183 error = cpuset_which(which, id, &p, &ttd, &set);
2185 error = cpuset_modify(set, mask);
2190 case CPU_WHICH_INTRHANDLER:
2191 case CPU_WHICH_ITHREAD:
2192 error = intr_setaffinity(id, which, mask);
2207 user_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2208 id_t id, size_t cpusetsize, const cpuset_t *maskp, const struct cpuset_copy_cb *cb)
2214 size = min(cpusetsize, sizeof(cpuset_t));
2215 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
2216 error = cb->cpuset_copyin(maskp, mask, size);
2220 * Verify that no high bits are set.
2222 if (cpusetsize > sizeof(cpuset_t)) {
2223 const char *end, *cp;
2225 end = cp = (const char *)&maskp->__bits;
2227 cp += sizeof(cpuset_t);
2242 error = kern_cpuset_setaffinity(td, level, which, id, mask);
2249 #ifndef _SYS_SYSPROTO_H_
2250 struct cpuset_getdomain_args {
2254 size_t domainsetsize;
2260 sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
2263 return (kern_cpuset_getdomain(td, uap->level, uap->which,
2264 uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set));
2268 kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2269 id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp,
2270 const struct cpuset_copy_cb *cb)
2272 struct domainset outset;
2274 struct cpuset *nset;
2276 struct domainset *dset;
2281 if (domainsetsize < sizeof(domainset_t) ||
2282 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2284 error = cpuset_check_capabilities(td, level, which, id);
2287 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2288 bzero(&outset, sizeof(outset));
2289 error = cpuset_which2(&which, id, &p, &ttd, &set);
2293 case CPU_LEVEL_ROOT:
2294 case CPU_LEVEL_CPUSET:
2299 set = cpuset_ref(ttd->td_cpuset);
2302 case CPU_WHICH_CPUSET:
2303 case CPU_WHICH_JAIL:
2306 case CPU_WHICH_INTRHANDLER:
2307 case CPU_WHICH_ITHREAD:
2308 case CPU_WHICH_DOMAIN:
2312 if (level == CPU_LEVEL_ROOT)
2313 nset = cpuset_refroot(set);
2315 nset = cpuset_refbase(set);
2316 domainset_copy(nset->cs_domain, &outset);
2319 case CPU_LEVEL_WHICH:
2323 domainset_copy(ttd->td_cpuset->cs_domain, &outset);
2327 FOREACH_THREAD_IN_PROC(p, ttd) {
2329 dset = ttd->td_cpuset->cs_domain;
2330 /* Show all domains in the proc. */
2331 DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
2332 /* Last policy wins. */
2333 outset.ds_policy = dset->ds_policy;
2334 outset.ds_prefer = dset->ds_prefer;
2338 case CPU_WHICH_CPUSET:
2339 case CPU_WHICH_JAIL:
2340 domainset_copy(set->cs_domain, &outset);
2343 case CPU_WHICH_INTRHANDLER:
2344 case CPU_WHICH_ITHREAD:
2345 case CPU_WHICH_DOMAIN:
2359 * Translate prefer into a set containing only the preferred domain,
2360 * not the entire fallback set.
2362 if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
2363 DOMAINSET_ZERO(&outset.ds_mask);
2364 DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
2366 DOMAINSET_COPY(&outset.ds_mask, mask);
2368 error = cb->cpuset_copyout(mask, maskp, domainsetsize);
2370 if (suword32(policyp, outset.ds_policy) != 0)
2377 #ifndef _SYS_SYSPROTO_H_
2378 struct cpuset_setdomain_args {
2382 size_t domainsetsize;
2388 sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
2391 return (kern_cpuset_setdomain(td, uap->level, uap->which,
2392 uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set));
2396 kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2397 id_t id, size_t domainsetsize, const domainset_t *maskp, int policy,
2398 const struct cpuset_copy_cb *cb)
2400 struct cpuset *nset;
2404 struct domainset domain;
2408 if (domainsetsize < sizeof(domainset_t) ||
2409 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2411 if (policy <= DOMAINSET_POLICY_INVALID ||
2412 policy > DOMAINSET_POLICY_MAX)
2414 error = cpuset_check_capabilities(td, level, which, id);
2417 memset(&domain, 0, sizeof(domain));
2418 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2419 error = cb->cpuset_copyin(maskp, mask, domainsetsize);
2423 * Verify that no high bits are set.
2425 if (domainsetsize > sizeof(domainset_t)) {
2429 end = cp = (char *)&mask->__bits;
2430 end += domainsetsize;
2431 cp += sizeof(domainset_t);
2438 if (DOMAINSET_EMPTY(mask)) {
2442 DOMAINSET_COPY(mask, &domain.ds_mask);
2443 domain.ds_policy = policy;
2446 * Sanitize the provided mask.
2448 if (!DOMAINSET_SUBSET(&all_domains, &domain.ds_mask)) {
2453 /* Translate preferred policy into a mask and fallback. */
2454 if (policy == DOMAINSET_POLICY_PREFER) {
2455 /* Only support a single preferred domain. */
2456 if (DOMAINSET_COUNT(&domain.ds_mask) != 1) {
2460 domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
2461 /* This will be constrained by domainset_shadow(). */
2462 DOMAINSET_COPY(&all_domains, &domain.ds_mask);
2466 * When given an impossible policy, fall back to interleaving
2467 * across all domains.
2469 if (domainset_empty_vm(&domain))
2470 domainset_copy(domainset2, &domain);
2473 case CPU_LEVEL_ROOT:
2474 case CPU_LEVEL_CPUSET:
2475 error = cpuset_which(which, id, &p, &ttd, &set);
2481 case CPU_WHICH_TIDPID:
2483 set = cpuset_ref(ttd->td_cpuset);
2487 case CPU_WHICH_CPUSET:
2488 case CPU_WHICH_JAIL:
2491 case CPU_WHICH_INTRHANDLER:
2492 case CPU_WHICH_ITHREAD:
2493 case CPU_WHICH_DOMAIN:
2497 if (level == CPU_LEVEL_ROOT)
2498 nset = cpuset_refroot(set);
2500 nset = cpuset_refbase(set);
2501 error = cpuset_modify_domain(nset, &domain);
2505 case CPU_LEVEL_WHICH:
2508 error = _cpuset_setthread(id, NULL, &domain);
2511 error = cpuset_setproc(id, NULL, NULL, &domain, false);
2513 case CPU_WHICH_TIDPID:
2514 if (id > PID_MAX || id == -1)
2515 error = _cpuset_setthread(id, NULL, &domain);
2517 error = cpuset_setproc(id, NULL, NULL, &domain,
2520 case CPU_WHICH_CPUSET:
2521 case CPU_WHICH_JAIL:
2522 error = cpuset_which(which, id, &p, &ttd, &set);
2524 error = cpuset_modify_domain(set, &domain);
2529 case CPU_WHICH_INTRHANDLER:
2530 case CPU_WHICH_ITHREAD:
2548 ddb_display_bitset(const struct bitset *set, int size)
2552 for (once = 0, bit = 0; bit < size; bit++) {
2553 if (CPU_ISSET(bit, set)) {
2555 db_printf("%d", bit);
2558 db_printf(",%d", bit);
2562 db_printf("<none>");
2566 ddb_display_cpuset(const cpuset_t *set)
2568 ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
2572 ddb_display_domainset(const domainset_t *set)
2574 ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
2577 DB_SHOW_COMMAND_FLAGS(cpusets, db_show_cpusets, DB_CMD_MEMSAFE)
2581 LIST_FOREACH(set, &cpuset_ids, cs_link) {
2582 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
2583 set, set->cs_id, refcount_load(&set->cs_ref), set->cs_flags,
2584 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
2585 db_printf(" cpu mask=");
2586 ddb_display_cpuset(&set->cs_mask);
2588 db_printf(" domain policy %d prefer %d mask=",
2589 set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
2590 ddb_display_domainset(&set->cs_domain->ds_mask);
2597 DB_SHOW_COMMAND_FLAGS(domainsets, db_show_domainsets, DB_CMD_MEMSAFE)
2599 struct domainset *set;
2601 LIST_FOREACH(set, &cpuset_domains, ds_link) {
2602 db_printf("set=%p policy %d prefer %d cnt %d\n",
2603 set, set->ds_policy, set->ds_prefer, set->ds_cnt);
2604 db_printf(" mask =");
2605 ddb_display_domainset(&set->ds_mask);