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>
34 __FBSDID("$FreeBSD$");
37 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
42 #include <sys/ctype.h>
43 #include <sys/sysproto.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/refcount.h>
52 #include <sys/sched.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysent.h>
56 #include <sys/capsicum.h>
57 #include <sys/cpuset.h>
58 #include <sys/domainset.h>
60 #include <sys/queue.h>
61 #include <sys/libkern.h>
62 #include <sys/limits.h>
64 #include <sys/interrupt.h>
65 #include <sys/vmmeter.h>
66 #include <sys/ktrace.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_pageout.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_phys.h>
76 #include <vm/vm_pagequeue.h>
83 * cpusets provide a mechanism for creating and manipulating sets of
84 * processors for the purpose of constraining the scheduling of threads to
85 * specific processors.
87 * Each process belongs to an identified set, by default this is set 1. Each
88 * thread may further restrict the cpus it may run on to a subset of this
89 * named set. This creates an anonymous set which other threads and processes
90 * may not join by number.
92 * The named set is referred to herein as the 'base' set to avoid ambiguity.
93 * This set is usually a child of a 'root' set while the anonymous set may
94 * simply be referred to as a mask. In the syscall api these are referred to
95 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
97 * Threads inherit their set from their creator whether it be anonymous or
98 * not. This means that anonymous sets are immutable because they may be
99 * shared. To modify an anonymous set a new set is created with the desired
100 * mask and the same parent as the existing anonymous set. This gives the
101 * illusion of each thread having a private mask.
103 * Via the syscall apis a user may ask to retrieve or modify the root, base,
104 * or mask that is discovered via a pid, tid, or setid. Modifying a set
105 * modifies all numbered and anonymous child sets to comply with the new mask.
106 * Modifying a pid or tid's mask applies only to that tid but must still
107 * exist within the assigned parent set.
109 * A thread may not be assigned to a group separate from other threads in
110 * the process. This is to remove ambiguity when the setid is queried with
111 * a pid argument. There is no other technical limitation.
113 * This somewhat complex arrangement is intended to make it easy for
114 * applications to query available processors and bind their threads to
115 * specific processors while also allowing administrators to dynamically
116 * reprovision by changing sets which apply to groups of processes.
118 * A simple application should not concern itself with sets at all and
119 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
120 * meaning 'curthread'. It may query available cpus for that tid with a
121 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
124 LIST_HEAD(domainlist, domainset);
125 struct domainset __read_mostly domainset_firsttouch;
126 struct domainset __read_mostly domainset_fixed[MAXMEMDOM];
127 struct domainset __read_mostly domainset_interleave;
128 struct domainset __read_mostly domainset_prefer[MAXMEMDOM];
129 struct domainset __read_mostly domainset_roundrobin;
131 static uma_zone_t cpuset_zone;
132 static uma_zone_t domainset_zone;
133 static struct mtx cpuset_lock;
134 static struct setlist cpuset_ids;
135 static struct domainlist cpuset_domains;
136 static struct unrhdr *cpuset_unr;
137 static struct cpuset *cpuset_zero, *cpuset_default, *cpuset_kernel;
138 static struct domainset *domainset0, *domainset2;
139 u_int cpusetsizemin = 1;
141 /* Return the size of cpuset_t at the kernel level */
142 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
143 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
145 /* Return the minimum size of cpuset_t allowed by the kernel */
146 SYSCTL_UINT(_kern_sched, OID_AUTO, cpusetsizemin,
147 CTLFLAG_RD | CTLFLAG_CAPRD, &cpusetsizemin, 0,
148 "The minimum size of cpuset_t allowed by the kernel");
150 cpuset_t *cpuset_root;
151 cpuset_t cpuset_domain[MAXMEMDOM];
153 static int cpuset_which2(cpuwhich_t *, id_t, struct proc **, struct thread **,
155 static int domainset_valid(const struct domainset *, const struct domainset *);
158 * Find the first non-anonymous set starting from 'set'.
160 static struct cpuset *
161 cpuset_getbase(struct cpuset *set)
164 if (set->cs_id == CPUSET_INVALID)
165 set = set->cs_parent;
170 * Walks up the tree from 'set' to find the root.
172 static struct cpuset *
173 cpuset_getroot(struct cpuset *set)
176 while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL)
177 set = set->cs_parent;
182 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
185 cpuset_ref(struct cpuset *set)
188 refcount_acquire(&set->cs_ref);
193 * Walks up the tree from 'set' to find the root. Returns the root
196 static struct cpuset *
197 cpuset_refroot(struct cpuset *set)
200 return (cpuset_ref(cpuset_getroot(set)));
204 * Find the first non-anonymous set starting from 'set'. Returns this set
205 * referenced. May return the passed in set with an extra ref if it is
208 static struct cpuset *
209 cpuset_refbase(struct cpuset *set)
212 return (cpuset_ref(cpuset_getbase(set)));
216 * Release a reference in a context where it is safe to allocate.
219 cpuset_rel(struct cpuset *set)
223 if (refcount_release_if_not_last(&set->cs_ref))
225 mtx_lock_spin(&cpuset_lock);
226 if (!refcount_release(&set->cs_ref)) {
227 mtx_unlock_spin(&cpuset_lock);
230 LIST_REMOVE(set, cs_siblings);
232 if (id != CPUSET_INVALID)
233 LIST_REMOVE(set, cs_link);
234 mtx_unlock_spin(&cpuset_lock);
235 cpuset_rel(set->cs_parent);
236 uma_zfree(cpuset_zone, set);
237 if (id != CPUSET_INVALID)
238 free_unr(cpuset_unr, id);
242 * Deferred release must be used when in a context that is not safe to
243 * allocate/free. This places any unreferenced sets on the list 'head'.
246 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
249 if (refcount_release_if_not_last(&set->cs_ref))
251 mtx_lock_spin(&cpuset_lock);
252 if (!refcount_release(&set->cs_ref)) {
253 mtx_unlock_spin(&cpuset_lock);
256 LIST_REMOVE(set, cs_siblings);
257 if (set->cs_id != CPUSET_INVALID)
258 LIST_REMOVE(set, cs_link);
259 LIST_INSERT_HEAD(head, set, cs_link);
260 mtx_unlock_spin(&cpuset_lock);
264 * Complete a deferred release. Removes the set from the list provided to
268 cpuset_rel_complete(struct cpuset *set)
273 LIST_REMOVE(set, cs_link);
274 cpuset_rel(set->cs_parent);
275 uma_zfree(cpuset_zone, set);
276 if (id != CPUSET_INVALID)
277 free_unr(cpuset_unr, id);
281 * Find a set based on an id. Returns it with a ref.
283 static struct cpuset *
284 cpuset_lookup(cpusetid_t setid, struct thread *td)
288 if (setid == CPUSET_INVALID)
290 mtx_lock_spin(&cpuset_lock);
291 LIST_FOREACH(set, &cpuset_ids, cs_link)
292 if (set->cs_id == setid)
296 mtx_unlock_spin(&cpuset_lock);
298 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
299 if (set != NULL && jailed(td->td_ucred)) {
300 struct cpuset *jset, *tset;
302 jset = td->td_ucred->cr_prison->pr_cpuset;
303 for (tset = set; tset != NULL; tset = tset->cs_parent)
316 * Initialize a set in the space provided in 'set' with the provided parameters.
317 * The set is returned with a single ref. May return EDEADLK if the set
318 * will have no valid cpu based on restrictions from the parent.
321 cpuset_init(struct cpuset *set, struct cpuset *parent,
322 const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
326 domain = parent->cs_domain;
328 mask = &parent->cs_mask;
329 if (!CPU_OVERLAP(&parent->cs_mask, mask))
331 /* The domain must be prepared ahead of time. */
332 if (!domainset_valid(parent->cs_domain, domain))
334 CPU_COPY(mask, &set->cs_mask);
335 LIST_INIT(&set->cs_children);
336 refcount_init(&set->cs_ref, 1);
338 mtx_lock_spin(&cpuset_lock);
339 set->cs_domain = domain;
340 CPU_AND(&set->cs_mask, &set->cs_mask, &parent->cs_mask);
342 set->cs_parent = cpuset_ref(parent);
343 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
344 if (set->cs_id != CPUSET_INVALID)
345 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
346 mtx_unlock_spin(&cpuset_lock);
352 * Create a new non-anonymous set with the requested parent and mask. May
353 * return failures if the mask is invalid or a new number can not be
356 * If *setp is not NULL, then it will be used as-is. The caller must take
357 * into account that *setp will be inserted at the head of cpuset_ids and
358 * plan any potentially conflicting cs_link usage accordingly.
361 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
368 id = alloc_unr(cpuset_unr);
371 dofree = (*setp == NULL);
375 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
376 error = cpuset_init(set, parent, mask, NULL, id);
379 free_unr(cpuset_unr, id);
381 uma_zfree(cpuset_zone, set);
387 cpuset_freelist_add(struct setlist *list, int count)
392 for (i = 0; i < count; i++) {
393 set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
394 LIST_INSERT_HEAD(list, set, cs_link);
399 cpuset_freelist_init(struct setlist *list, int count)
403 cpuset_freelist_add(list, count);
407 cpuset_freelist_free(struct setlist *list)
411 while ((set = LIST_FIRST(list)) != NULL) {
412 LIST_REMOVE(set, cs_link);
413 uma_zfree(cpuset_zone, set);
418 domainset_freelist_add(struct domainlist *list, int count)
420 struct domainset *set;
423 for (i = 0; i < count; i++) {
424 set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
425 LIST_INSERT_HEAD(list, set, ds_link);
430 domainset_freelist_init(struct domainlist *list, int count)
434 domainset_freelist_add(list, count);
438 domainset_freelist_free(struct domainlist *list)
440 struct domainset *set;
442 while ((set = LIST_FIRST(list)) != NULL) {
443 LIST_REMOVE(set, ds_link);
444 uma_zfree(domainset_zone, set);
448 /* Copy a domainset preserving mask and policy. */
450 domainset_copy(const struct domainset *from, struct domainset *to)
453 DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
454 to->ds_policy = from->ds_policy;
455 to->ds_prefer = from->ds_prefer;
458 /* Return 1 if mask and policy are equal, otherwise 0. */
460 domainset_equal(const struct domainset *one, const struct domainset *two)
463 return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
464 one->ds_policy == two->ds_policy &&
465 one->ds_prefer == two->ds_prefer);
468 /* Return 1 if child is a valid subset of parent. */
470 domainset_valid(const struct domainset *parent, const struct domainset *child)
472 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
473 return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
474 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
478 domainset_restrict(const struct domainset *parent,
479 const struct domainset *child)
481 if (child->ds_policy != DOMAINSET_POLICY_PREFER)
482 return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
483 return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
487 * Lookup or create a domainset. The key is provided in ds_mask and
488 * ds_policy. If the domainset does not yet exist the storage in
489 * 'domain' is used to insert. Otherwise this storage is freed to the
490 * domainset_zone and the existing domainset is returned.
492 static struct domainset *
493 _domainset_create(struct domainset *domain, struct domainlist *freelist)
495 struct domainset *ndomain;
498 KASSERT(domain->ds_cnt <= vm_ndomains,
499 ("invalid domain count in domainset %p", domain));
500 KASSERT(domain->ds_policy != DOMAINSET_POLICY_PREFER ||
501 domain->ds_prefer < vm_ndomains,
502 ("invalid preferred domain in domains %p", domain));
504 mtx_lock_spin(&cpuset_lock);
505 LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
506 if (domainset_equal(ndomain, domain))
509 * If the domain does not yet exist we insert it and initialize
510 * various iteration helpers which are not part of the key.
512 if (ndomain == NULL) {
513 LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
514 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
515 for (i = 0, j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
516 if (DOMAINSET_ISSET(i, &domain->ds_mask))
517 domain->ds_order[j++] = i;
519 mtx_unlock_spin(&cpuset_lock);
522 if (freelist != NULL)
523 LIST_INSERT_HEAD(freelist, domain, ds_link);
525 uma_zfree(domainset_zone, domain);
531 * Are any of the domains in the mask empty? If so, silently
532 * remove them and update the domainset accordingly. If only empty
533 * domains are present, we must return failure.
536 domainset_empty_vm(struct domainset *domain)
541 DOMAINSET_ZERO(&empty);
542 for (i = 0; i < vm_ndomains; i++)
543 if (VM_DOMAIN_EMPTY(i))
544 DOMAINSET_SET(i, &empty);
545 if (DOMAINSET_SUBSET(&empty, &domain->ds_mask))
548 /* Remove empty domains from the set and recompute. */
549 DOMAINSET_ANDNOT(&domain->ds_mask, &empty);
550 domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
551 for (i = j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
552 if (DOMAINSET_ISSET(i, &domain->ds_mask))
553 domain->ds_order[j++] = i;
555 /* Convert a PREFER policy referencing an empty domain to RR. */
556 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
557 DOMAINSET_ISSET(domain->ds_prefer, &empty)) {
558 domain->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
559 domain->ds_prefer = -1;
566 * Create or lookup a domainset based on the key held in 'domain'.
569 domainset_create(const struct domainset *domain)
571 struct domainset *ndomain;
574 * Validate the policy. It must specify a useable policy number with
575 * only valid domains. Preferred must include the preferred domain
578 if (domain->ds_policy <= DOMAINSET_POLICY_INVALID ||
579 domain->ds_policy > DOMAINSET_POLICY_MAX)
581 if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
582 !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask))
584 if (!DOMAINSET_SUBSET(&domainset0->ds_mask, &domain->ds_mask))
586 ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
587 domainset_copy(domain, ndomain);
588 return _domainset_create(ndomain, NULL);
592 * Update thread domainset pointers.
595 domainset_notify(void)
600 sx_slock(&allproc_lock);
601 FOREACH_PROC_IN_SYSTEM(p) {
603 if (p->p_state == PRS_NEW) {
607 FOREACH_THREAD_IN_PROC(p, td) {
609 td->td_domain.dr_policy = td->td_cpuset->cs_domain;
614 sx_sunlock(&allproc_lock);
615 kernel_object->domain.dr_policy = cpuset_kernel->cs_domain;
619 * Create a new set that is a subset of a parent.
621 static struct domainset *
622 domainset_shadow(const struct domainset *pdomain,
623 const struct domainset *domain, struct domainlist *freelist)
625 struct domainset *ndomain;
627 ndomain = LIST_FIRST(freelist);
628 LIST_REMOVE(ndomain, ds_link);
631 * Initialize the key from the request.
633 domainset_copy(domain, ndomain);
636 * Restrict the key by the parent.
638 DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
640 return _domainset_create(ndomain, freelist);
644 * Recursively check for errors that would occur from applying mask to
645 * the tree of sets starting at 'set'. Checks for sets that would become
646 * empty as well as RDONLY flags.
649 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int augment_mask)
655 mtx_assert(&cpuset_lock, MA_OWNED);
656 if (set->cs_flags & CPU_SET_RDONLY)
659 CPU_AND(&newmask, &set->cs_mask, mask);
661 CPU_COPY(mask, &newmask);
663 if (CPU_EMPTY(&newmask))
666 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
667 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
673 * Applies the mask 'mask' without checking for empty sets or permissions.
676 cpuset_update(struct cpuset *set, cpuset_t *mask)
680 mtx_assert(&cpuset_lock, MA_OWNED);
681 CPU_AND(&set->cs_mask, &set->cs_mask, mask);
682 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
683 cpuset_update(nset, &set->cs_mask);
689 * Modify the set 'set' to use a copy of the mask provided. Apply this new
690 * mask to restrict all children in the tree. Checks for validity before
691 * applying the changes.
694 cpuset_modify(struct cpuset *set, cpuset_t *mask)
699 error = priv_check(curthread, PRIV_SCHED_CPUSET);
703 * In case we are called from within the jail,
704 * we do not allow modifying the dedicated root
705 * cpuset of the jail but may still allow to
706 * change child sets, including subordinate jails'
709 if ((set->cs_flags & CPU_SET_ROOT) != 0 &&
710 jailed(curthread->td_ucred) &&
711 set == curthread->td_ucred->cr_prison->pr_cpuset)
714 * Verify that we have access to this set of
717 if ((set->cs_flags & (CPU_SET_ROOT | CPU_SET_RDONLY)) == CPU_SET_ROOT) {
718 KASSERT(set->cs_parent != NULL,
719 ("jail.cpuset=%d is not a proper child of parent jail's root.",
723 * cpuset_getroot() cannot work here due to how top-level jail
724 * roots are constructed. Top-level jails are parented to
725 * thread0's cpuset (i.e. cpuset 1) rather than the system root.
727 root = set->cs_parent;
729 root = cpuset_getroot(set);
731 mtx_lock_spin(&cpuset_lock);
732 if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
736 error = cpuset_testupdate(set, mask, 0);
739 CPU_COPY(mask, &set->cs_mask);
740 cpuset_update(set, mask);
742 mtx_unlock_spin(&cpuset_lock);
748 * Recursively check for errors that would occur from applying mask to
749 * the tree of sets starting at 'set'. Checks for sets that would become
750 * empty as well as RDONLY flags.
753 cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
754 struct domainset *orig, int *count, int augment_mask __unused)
757 struct domainset *domain;
758 struct domainset newset;
761 mtx_assert(&cpuset_lock, MA_OWNED);
762 if (set->cs_flags & CPU_SET_RDONLY)
764 domain = set->cs_domain;
765 domainset_copy(domain, &newset);
766 if (!domainset_equal(domain, orig)) {
767 if (!domainset_restrict(domain, dset))
769 DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
770 /* Count the number of domains that are changing. */
774 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
775 if ((error = cpuset_testupdate_domain(nset, &newset, domain,
782 * Applies the mask 'mask' without checking for empty sets or permissions.
785 cpuset_update_domain(struct cpuset *set, struct domainset *domain,
786 struct domainset *orig, struct domainlist *domains)
790 mtx_assert(&cpuset_lock, MA_OWNED);
792 * If this domainset has changed from the parent we must calculate
793 * a new set. Otherwise it simply inherits from the parent. When
794 * we inherit from the parent we get a new mask and policy. If the
795 * set is modified from the parent we keep the policy and only
798 if (set->cs_domain != orig) {
799 orig = set->cs_domain;
800 set->cs_domain = domainset_shadow(domain, orig, domains);
802 set->cs_domain = domain;
803 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
804 cpuset_update_domain(nset, set->cs_domain, orig, domains);
810 * Modify the set 'set' to use a copy the domainset provided. Apply this new
811 * mask to restrict all children in the tree. Checks for validity before
812 * applying the changes.
815 cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
817 struct domainlist domains;
818 struct domainset temp;
819 struct domainset *dset;
821 int ndomains, needed;
824 error = priv_check(curthread, PRIV_SCHED_CPUSET);
828 * In case we are called from within the jail
829 * we do not allow modifying the dedicated root
830 * cpuset of the jail but may still allow to
833 if (jailed(curthread->td_ucred) &&
834 set->cs_flags & CPU_SET_ROOT)
836 domainset_freelist_init(&domains, 0);
837 domain = domainset_create(domain);
840 mtx_lock_spin(&cpuset_lock);
842 root = cpuset_getroot(set);
843 dset = root->cs_domain;
845 * Verify that we have access to this set of domains.
847 if (!domainset_valid(dset, domain)) {
852 * If applying prefer we keep the current set as the fallback.
854 if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
855 DOMAINSET_COPY(&set->cs_domain->ds_mask,
858 * Determine whether we can apply this set of domains and
859 * how many new domain structures it will require.
861 domainset_copy(domain, &temp);
863 error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
867 if (ndomains >= needed)
870 /* Dropping the lock; we'll need to re-evaluate again. */
871 mtx_unlock_spin(&cpuset_lock);
872 domainset_freelist_add(&domains, needed - ndomains);
874 mtx_lock_spin(&cpuset_lock);
876 dset = set->cs_domain;
877 cpuset_update_domain(set, domain, dset, &domains);
879 mtx_unlock_spin(&cpuset_lock);
880 domainset_freelist_free(&domains);
888 * Resolve the 'which' parameter of several cpuset apis.
890 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
891 * checks for permission via p_cansched().
893 * For WHICH_SET returns a valid set with a new reference.
895 * -1 may be supplied for any argument to mean the current proc/thread or
896 * the base set of the current thread. May fail with ESRCH/EPERM.
899 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
900 struct cpuset **setp)
917 if ((p = pfind(id)) == NULL)
932 case CPU_WHICH_TIDPID:
937 } else if (id > PID_MAX) {
948 case CPU_WHICH_CPUSET:
950 thread_lock(curthread);
951 set = cpuset_refbase(curthread->td_cpuset);
952 thread_unlock(curthread);
954 set = cpuset_lookup(id, curthread);
962 /* Find `set' for prison with given id. */
965 sx_slock(&allprison_lock);
966 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
967 sx_sunlock(&allprison_lock);
970 cpuset_ref(pr->pr_cpuset);
971 *setp = pr->pr_cpuset;
972 mtx_unlock(&pr->pr_mtx);
976 case CPU_WHICH_DOMAIN:
981 error = p_cansched(curthread, p);
987 td = FIRST_THREAD_IN_PROC(p);
994 cpuset_which2(cpuwhich_t *which, id_t id, struct proc **pp, struct thread **tdp,
995 struct cpuset **setp)
998 if (*which == CPU_WHICH_TIDPID) {
999 if (id == -1 || id > PID_MAX)
1000 *which = CPU_WHICH_TID;
1002 *which = CPU_WHICH_PID;
1004 return (cpuset_which(*which, id, pp, tdp, setp));
1008 cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
1009 const struct domainset *domain)
1011 struct cpuset *parent;
1012 struct domainset *dset;
1014 parent = cpuset_getbase(set);
1016 * If we are restricting a cpu mask it must be a subset of the
1017 * parent or invalid CPUs have been specified.
1019 if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
1023 * If we are restricting a domain mask it must be a subset of the
1024 * parent or invalid domains have been specified.
1026 dset = parent->cs_domain;
1027 if (domain != NULL && !domainset_valid(dset, domain))
1034 * Create an anonymous set with the provided mask in the space provided by
1035 * 'nset'. If the passed in set is anonymous we use its parent otherwise
1036 * the new set is a child of 'set'.
1039 cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
1040 const cpuset_t *mask, const struct domainset *domain,
1041 struct setlist *cpusets, struct domainlist *domains)
1043 struct cpuset *parent;
1044 struct cpuset *nset;
1045 struct domainset *dset;
1046 struct domainset *d;
1049 error = cpuset_testshadow(set, mask, domain);
1053 parent = cpuset_getbase(set);
1054 dset = parent->cs_domain;
1056 mask = &set->cs_mask;
1058 d = domainset_shadow(dset, domain, domains);
1061 nset = LIST_FIRST(cpusets);
1062 error = cpuset_init(nset, parent, mask, d, CPUSET_INVALID);
1064 LIST_REMOVE(nset, cs_link);
1070 static struct cpuset *
1071 cpuset_update_thread(struct thread *td, struct cpuset *nset)
1073 struct cpuset *tdset;
1075 tdset = td->td_cpuset;
1076 td->td_cpuset = nset;
1077 td->td_domain.dr_policy = nset->cs_domain;
1084 cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
1085 struct domainset *domain)
1087 struct cpuset *parent;
1089 parent = cpuset_getbase(tdset);
1091 mask = &tdset->cs_mask;
1093 domain = tdset->cs_domain;
1094 return cpuset_testshadow(parent, mask, domain);
1098 cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
1099 struct domainset *domain, struct cpuset **nsetp,
1100 struct setlist *freelist, struct domainlist *domainlist)
1102 struct cpuset *parent;
1104 parent = cpuset_getbase(tdset);
1106 mask = &tdset->cs_mask;
1108 domain = tdset->cs_domain;
1109 return cpuset_shadow(parent, nsetp, mask, domain, freelist,
1114 cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
1115 cpuset_t *mask, struct domainset *domain)
1117 struct cpuset *parent;
1119 parent = cpuset_getbase(tdset);
1122 * If the thread restricted its mask then apply that same
1123 * restriction to the new set, otherwise take it wholesale.
1125 if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
1126 CPU_AND(mask, &tdset->cs_mask, &set->cs_mask);
1128 CPU_COPY(&set->cs_mask, mask);
1131 * If the thread restricted the domain then we apply the
1132 * restriction to the new set but retain the policy.
1134 if (tdset->cs_domain != parent->cs_domain) {
1135 domainset_copy(tdset->cs_domain, domain);
1136 DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
1138 domainset_copy(set->cs_domain, domain);
1140 if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
1147 cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
1149 struct domainset domain;
1152 if (tdset->cs_id != CPUSET_INVALID)
1154 return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1158 cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
1159 struct cpuset **nsetp, struct setlist *freelist,
1160 struct domainlist *domainlist)
1162 struct domainset domain;
1167 * If we're replacing on a thread that has not constrained the
1168 * original set we can simply accept the new set.
1170 if (tdset->cs_id != CPUSET_INVALID) {
1171 *nsetp = cpuset_ref(set);
1174 error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
1178 return cpuset_shadow(set, nsetp, &mask, &domain, freelist,
1183 cpuset_setproc_newbase(struct thread *td, struct cpuset *set,
1184 struct cpuset *nroot, struct cpuset **nsetp,
1185 struct setlist *cpusets, struct domainlist *domainlist)
1187 struct domainset ndomain;
1189 struct cpuset *pbase;
1192 pbase = cpuset_getbase(td->td_cpuset);
1194 /* Copy process mask, then further apply the new root mask. */
1195 CPU_AND(&nmask, &pbase->cs_mask, &nroot->cs_mask);
1197 domainset_copy(pbase->cs_domain, &ndomain);
1198 DOMAINSET_AND(&ndomain.ds_mask, &set->cs_domain->ds_mask);
1200 /* Policy is too restrictive, will not work. */
1201 if (CPU_EMPTY(&nmask) || DOMAINSET_EMPTY(&ndomain.ds_mask))
1205 * Remove pbase from the freelist in advance, it'll be pushed to
1206 * cpuset_ids on success. We assume here that cpuset_create() will not
1207 * touch pbase on failure, and we just enqueue it back to the freelist
1208 * to remain in a consistent state.
1210 pbase = LIST_FIRST(cpusets);
1211 LIST_REMOVE(pbase, cs_link);
1212 error = cpuset_create(&pbase, set, &nmask);
1214 LIST_INSERT_HEAD(cpusets, pbase, cs_link);
1218 /* Duplicates some work from above... oh well. */
1219 pbase->cs_domain = domainset_shadow(set->cs_domain, &ndomain,
1226 * Handle four cases for updating an entire process.
1228 * 1) Set is non-null and the process is not rebasing onto a new root. This
1229 * reparents all anonymous sets to the provided set and replaces all
1230 * non-anonymous td_cpusets with the provided set.
1231 * 2) Set is non-null and the process is rebasing onto a new root. This
1232 * creates a new base set if the process previously had its own base set,
1233 * then reparents all anonymous sets either to that set or the provided set
1234 * if one was not created. Non-anonymous sets are similarly replaced.
1235 * 3) Mask is non-null. This replaces or creates anonymous sets for every
1236 * thread with the existing base as a parent.
1237 * 4) domain is non-null. This creates anonymous sets for every thread
1238 * and replaces the domain set.
1240 * This is overly complicated because we can't allocate while holding a
1241 * spinlock and spinlocks must be held while changing and examining thread
1245 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1246 struct domainset *domain, bool rebase)
1248 struct setlist freelist;
1249 struct setlist droplist;
1250 struct domainlist domainlist;
1251 struct cpuset *base, *nset, *nroot, *tdroot;
1259 * The algorithm requires two passes due to locking considerations.
1261 * 1) Lookup the process and acquire the locks in the required order.
1262 * 2) If enough cpusets have not been allocated release the locks and
1263 * allocate them. Loop.
1265 cpuset_freelist_init(&freelist, 1);
1266 domainset_freelist_init(&domainlist, 1);
1268 LIST_INIT(&droplist);
1273 nroot = cpuset_getroot(set);
1275 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1278 tdroot = cpuset_getroot(td->td_cpuset);
1279 needed = p->p_numthreads;
1280 if (set != NULL && rebase && tdroot != nroot)
1282 if (nfree >= needed)
1285 if (nfree < needed) {
1286 cpuset_freelist_add(&freelist, needed - nfree);
1287 domainset_freelist_add(&domainlist, needed - nfree);
1291 PROC_LOCK_ASSERT(p, MA_OWNED);
1294 * If we're changing roots and the root set is what has been specified
1295 * as the parent, then we'll check if the process was previously using
1296 * the root set and, if it wasn't, create a new base with the process's
1297 * mask applied to it.
1299 * If the new root is incompatible with the existing mask, then we allow
1300 * the process to take on the new root if and only if they have
1301 * privilege to widen their mask anyways. Unprivileged processes get
1302 * rejected with EDEADLK.
1304 if (set != NULL && rebase && nroot != tdroot) {
1305 cpusetid_t base_id, root_id;
1307 root_id = td->td_ucred->cr_prison->pr_cpuset->cs_id;
1308 base_id = cpuset_getbase(td->td_cpuset)->cs_id;
1310 if (base_id != root_id) {
1311 error = cpuset_setproc_newbase(td, set, nroot, &base,
1312 &freelist, &domainlist);
1313 if (error == EDEADLK &&
1314 priv_check(td, PRIV_SCHED_CPUSET) == 0)
1322 * Now that the appropriate locks are held and we have enough cpusets,
1323 * make sure the operation will succeed before applying changes. The
1324 * proc lock prevents td_cpuset from changing between calls.
1327 FOREACH_THREAD_IN_PROC(p, td) {
1330 error = cpuset_setproc_test_setthread(td->td_cpuset,
1333 error = cpuset_setproc_test_maskthread(td->td_cpuset,
1340 * Replace each thread's cpuset while using deferred release. We
1341 * must do this because the thread lock must be held while operating
1342 * on the thread and this limits the type of operations allowed.
1344 FOREACH_THREAD_IN_PROC(p, td) {
1347 error = cpuset_setproc_setthread(td->td_cpuset, base,
1348 &nset, &freelist, &domainlist);
1350 error = cpuset_setproc_maskthread(td->td_cpuset, mask,
1351 domain, &nset, &freelist, &domainlist);
1356 cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1362 if (base != NULL && base != set)
1364 while ((nset = LIST_FIRST(&droplist)) != NULL)
1365 cpuset_rel_complete(nset);
1366 cpuset_freelist_free(&freelist);
1367 domainset_freelist_free(&domainlist);
1372 bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen)
1380 for (i = 0; i < __bitset_words(setlen); i++) {
1389 if (bufsiz < sizeof(__STRING(ULONG_MAX)))
1391 bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]);
1399 bitset_strscan(struct bitset *set, int setlen, const char *buf)
1404 BIT_ZERO(setlen, set);
1406 for (i = 0; i < __bitset_words(setlen); i++) {
1411 ret = sscanf(p, "%lx", &set->__bits[i]);
1412 if (ret == 0 || ret == -1)
1414 while (isxdigit(*p))
1421 * Return a string representing a valid layout for a cpuset_t object.
1422 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1425 cpusetobj_strprint(char *buf, const cpuset_t *set)
1428 bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set,
1434 * Build a valid cpuset_t object from a string representation.
1435 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1438 cpusetobj_strscan(cpuset_t *set, const char *buf)
1442 if (strlen(buf) > CPUSETBUFSIZ - 1)
1445 p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)];
1453 * Handle a domainset specifier in the sysctl tree. A poiner to a pointer to
1454 * a domainset is in arg1. If the user specifies a valid domainset the
1455 * pointer is updated.
1458 * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred
1461 sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)
1463 char buf[DOMAINSETBUFSIZ];
1464 struct domainset *dset;
1465 struct domainset key;
1466 int policy, prefer, error;
1469 dset = *(struct domainset **)arg1;
1473 p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ,
1474 (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE);
1475 sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer);
1477 sprintf(buf, "<NULL>");
1478 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1479 if (error != 0 || req->newptr == NULL)
1483 * Read in and validate the string.
1485 memset(&key, 0, sizeof(key));
1486 p = &buf[bitset_strscan((struct bitset *)&key.ds_mask,
1487 DOMAINSET_SETSIZE, buf)];
1490 if (sscanf(p, ":%d:%d", &policy, &prefer) != 2)
1492 key.ds_policy = policy;
1493 key.ds_prefer = prefer;
1495 /* Domainset_create() validates the policy.*/
1496 dset = domainset_create(&key);
1499 *(struct domainset **)arg1 = dset;
1505 * Apply an anonymous mask or a domain to a single thread.
1508 _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1510 struct setlist cpusets;
1511 struct domainlist domainlist;
1512 struct cpuset *nset;
1518 cpuset_freelist_init(&cpusets, 1);
1519 domainset_freelist_init(&domainlist, domain != NULL);
1520 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1525 error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
1526 &cpusets, &domainlist);
1528 set = cpuset_update_thread(td, nset);
1534 cpuset_freelist_free(&cpusets);
1535 domainset_freelist_free(&domainlist);
1540 * Apply an anonymous mask to a single thread.
1543 cpuset_setthread(lwpid_t id, cpuset_t *mask)
1546 return _cpuset_setthread(id, mask, NULL);
1550 * Apply new cpumask to the ithread.
1553 cpuset_setithread(lwpid_t id, int cpu)
1559 CPU_COPY(cpuset_root, &mask);
1561 CPU_SET(cpu, &mask);
1562 return _cpuset_setthread(id, &mask, NULL);
1566 * Initialize static domainsets after NUMA information is available. This is
1567 * called before memory allocators are initialized.
1570 domainset_init(void)
1572 struct domainset *dset;
1575 dset = &domainset_firsttouch;
1576 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1577 dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH;
1578 dset->ds_prefer = -1;
1579 _domainset_create(dset, NULL);
1581 dset = &domainset_interleave;
1582 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1583 dset->ds_policy = DOMAINSET_POLICY_INTERLEAVE;
1584 dset->ds_prefer = -1;
1585 _domainset_create(dset, NULL);
1587 dset = &domainset_roundrobin;
1588 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1589 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1590 dset->ds_prefer = -1;
1591 _domainset_create(dset, NULL);
1593 for (i = 0; i < vm_ndomains; i++) {
1594 dset = &domainset_fixed[i];
1595 DOMAINSET_ZERO(&dset->ds_mask);
1596 DOMAINSET_SET(i, &dset->ds_mask);
1597 dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1598 _domainset_create(dset, NULL);
1600 dset = &domainset_prefer[i];
1601 DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1602 dset->ds_policy = DOMAINSET_POLICY_PREFER;
1603 dset->ds_prefer = i;
1604 _domainset_create(dset, NULL);
1609 * Define the domainsets for cpuset 0, 1 and cpuset 2.
1612 domainset_zero(void)
1614 struct domainset *dset, *tmp;
1616 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
1618 domainset0 = &domainset_firsttouch;
1619 curthread->td_domain.dr_policy = domainset0;
1621 domainset2 = &domainset_interleave;
1622 kernel_object->domain.dr_policy = domainset2;
1624 /* Remove empty domains from the global policies. */
1625 LIST_FOREACH_SAFE(dset, &cpuset_domains, ds_link, tmp)
1626 if (domainset_empty_vm(dset))
1627 LIST_REMOVE(dset, ds_link);
1631 * Creates system-wide cpusets and the cpuset for thread0 including three
1634 * 0 - The root set which should represent all valid processors in the
1635 * system. This set is immutable.
1636 * 1 - The default set which all processes are a member of until changed.
1637 * This allows an administrator to move all threads off of given cpus to
1638 * dedicate them to high priority tasks or save power etc.
1639 * 2 - The kernel set which allows restriction and policy to be applied only
1640 * to kernel threads and the kernel_object.
1643 cpuset_thread0(void)
1649 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1650 NULL, NULL, UMA_ALIGN_CACHE, 0);
1651 domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1652 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
1655 * Create the root system set (0) for the whole machine. Doesn't use
1656 * cpuset_create() due to NULL parent.
1658 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1659 CPU_COPY(&all_cpus, &set->cs_mask);
1660 LIST_INIT(&set->cs_children);
1661 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
1662 refcount_init(&set->cs_ref, 1);
1663 set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY;
1664 set->cs_domain = domainset0;
1666 cpuset_root = &set->cs_mask;
1669 * Now derive a default (1), modifiable set from that to give out.
1671 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1672 error = cpuset_init(set, cpuset_zero, NULL, NULL, 1);
1673 KASSERT(error == 0, ("Error creating default set: %d\n", error));
1674 cpuset_default = set;
1676 * Create the kernel set (2).
1678 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
1679 error = cpuset_init(set, cpuset_zero, NULL, NULL, 2);
1680 KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
1681 set->cs_domain = domainset2;
1682 cpuset_kernel = set;
1685 * Initialize the unit allocator. 0 and 1 are allocated above.
1687 cpuset_unr = new_unrhdr(3, INT_MAX, NULL);
1690 * If MD code has not initialized per-domain cpusets, place all
1693 for (i = 0; i < MAXMEMDOM; i++)
1694 if (!CPU_EMPTY(&cpuset_domain[i]))
1696 CPU_COPY(&all_cpus, &cpuset_domain[0]);
1699 return (cpuset_default);
1703 cpuset_kernthread(struct thread *td)
1708 set = td->td_cpuset;
1709 td->td_cpuset = cpuset_ref(cpuset_kernel);
1715 * Create a cpuset, which would be cpuset_create() but
1716 * mark the new 'set' as root.
1718 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
1721 * In case of no error, returns the set in *setp locked with a reference.
1724 cpuset_create_root(struct prison *pr, struct cpuset **setp)
1729 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1730 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1733 error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1737 KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data",
1738 __func__, __LINE__));
1740 /* Mark the set as root. */
1741 set->cs_flags |= CPU_SET_ROOT;
1748 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1752 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1753 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1756 error = cpuset_setproc(p->p_pid, set, NULL, NULL, true);
1764 * In Capability mode, the only accesses that are permitted are to the current
1765 * thread and process' CPU and domain sets.
1768 cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which,
1771 if (IN_CAPABILITY_MODE(td)) {
1772 if (level != CPU_LEVEL_WHICH)
1774 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID &&
1775 which != CPU_WHICH_TIDPID)
1777 if (id != -1 && which == CPU_WHICH_TIDPID &&
1778 id != td->td_tid && id != td->td_proc->p_pid)
1781 !(which == CPU_WHICH_TID && id == td->td_tid) &&
1782 !(which == CPU_WHICH_PID && id == td->td_proc->p_pid))
1788 #if defined(__powerpc__)
1790 * TODO: At least powerpc64 and powerpc64le kernels panic with
1791 * exception 0x480 (instruction segment exception) when copyin/copyout,
1792 * are set as a function pointer in cpuset_copy_cb struct and called by
1793 * an external module (like pfsync). Tip: copyin/copyout have an ifunc
1794 * resolver function.
1796 * Bisect of LLVM shows that the behavior changed on LLVM 10.0 with
1797 * https://reviews.llvm.org/rGdc06b0bc9ad055d06535462d91bfc2a744b2f589
1799 * This is a hack/workaround while problem is being discussed with LLVM
1803 cpuset_copyin(const void *uaddr, void *kaddr, size_t len)
1805 return(copyin(uaddr, kaddr, len));
1809 cpuset_copyout(const void *kaddr, void *uaddr, size_t len)
1811 return(copyout(kaddr, uaddr, len));
1814 static const struct cpuset_copy_cb copy_set = {
1815 .cpuset_copyin = cpuset_copyin,
1816 .cpuset_copyout = cpuset_copyout
1819 static const struct cpuset_copy_cb copy_set = {
1820 .cpuset_copyin = copyin,
1821 .cpuset_copyout = copyout
1825 #ifndef _SYS_SYSPROTO_H_
1826 struct cpuset_args {
1831 sys_cpuset(struct thread *td, struct cpuset_args *uap)
1833 struct cpuset *root;
1838 root = cpuset_refroot(td->td_cpuset);
1841 error = cpuset_create(&set, root, &root->cs_mask);
1845 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1847 error = cpuset_setproc(-1, set, NULL, NULL, false);
1852 #ifndef _SYS_SYSPROTO_H_
1853 struct cpuset_setid_args {
1860 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1863 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1867 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1868 id_t id, cpusetid_t setid)
1874 * Presently we only support per-process sets.
1876 if (which != CPU_WHICH_PID)
1878 set = cpuset_lookup(setid, td);
1881 error = cpuset_setproc(id, set, NULL, NULL, false);
1886 #ifndef _SYS_SYSPROTO_H_
1887 struct cpuset_getid_args {
1895 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1898 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1903 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1904 id_t id, cpusetid_t *setid)
1906 struct cpuset *nset;
1913 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1915 error = cpuset_which(which, id, &p, &ttd, &set);
1921 case CPU_WHICH_TIDPID:
1923 set = cpuset_refbase(ttd->td_cpuset);
1927 case CPU_WHICH_CPUSET:
1928 case CPU_WHICH_JAIL:
1931 case CPU_WHICH_DOMAIN:
1935 case CPU_LEVEL_ROOT:
1936 nset = cpuset_refroot(set);
1940 case CPU_LEVEL_CPUSET:
1942 case CPU_LEVEL_WHICH:
1948 error = copyout(&tmpid, setid, sizeof(tmpid));
1953 #ifndef _SYS_SYSPROTO_H_
1954 struct cpuset_getaffinity_args {
1963 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1966 return (user_cpuset_getaffinity(td, uap->level, uap->which,
1967 uap->id, uap->cpusetsize, uap->mask, ©_set));
1971 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1972 id_t id, size_t cpusetsize, cpuset_t *mask)
1975 struct cpuset *nset;
1980 error = cpuset_check_capabilities(td, level, which, id);
1983 error = cpuset_which2(&which, id, &p, &ttd, &set);
1987 case CPU_LEVEL_ROOT:
1988 case CPU_LEVEL_CPUSET:
1993 set = cpuset_ref(ttd->td_cpuset);
1996 case CPU_WHICH_CPUSET:
1997 case CPU_WHICH_JAIL:
2000 case CPU_WHICH_INTRHANDLER:
2001 case CPU_WHICH_ITHREAD:
2002 case CPU_WHICH_DOMAIN:
2005 if (level == CPU_LEVEL_ROOT)
2006 nset = cpuset_refroot(set);
2008 nset = cpuset_refbase(set);
2009 CPU_COPY(&nset->cs_mask, mask);
2012 case CPU_LEVEL_WHICH:
2016 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
2020 FOREACH_THREAD_IN_PROC(p, ttd) {
2022 CPU_OR(mask, mask, &ttd->td_cpuset->cs_mask);
2026 case CPU_WHICH_CPUSET:
2027 case CPU_WHICH_JAIL:
2028 CPU_COPY(&set->cs_mask, mask);
2031 case CPU_WHICH_INTRHANDLER:
2032 case CPU_WHICH_ITHREAD:
2033 error = intr_getaffinity(id, which, mask);
2035 case CPU_WHICH_DOMAIN:
2036 if (id < 0 || id >= MAXMEMDOM)
2039 CPU_COPY(&cpuset_domain[id], mask);
2052 if (cpusetsize < howmany(CPU_FLS(mask), NBBY))
2055 if (KTRPOINT(td, KTR_STRUCT))
2056 ktrcpuset(mask, cpusetsize);
2063 user_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2064 id_t id, size_t cpusetsize, cpuset_t *maskp, const struct cpuset_copy_cb *cb)
2070 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
2071 size = min(cpusetsize, sizeof(cpuset_t));
2072 error = kern_cpuset_getaffinity(td, level, which, id, size, mask);
2074 error = cb->cpuset_copyout(mask, maskp, size);
2077 if (cpusetsize > size) {
2082 end = cp = (char *)&maskp->__bits;
2100 #ifndef _SYS_SYSPROTO_H_
2101 struct cpuset_setaffinity_args {
2106 const cpuset_t *mask;
2110 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
2113 return (user_cpuset_setaffinity(td, uap->level, uap->which,
2114 uap->id, uap->cpusetsize, uap->mask, ©_set));
2118 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2119 id_t id, cpuset_t *mask)
2121 struct cpuset *nset;
2128 if (KTRPOINT(td, KTR_STRUCT))
2129 ktrcpuset(mask, sizeof(cpuset_t));
2131 error = cpuset_check_capabilities(td, level, which, id);
2134 if (CPU_EMPTY(mask))
2137 case CPU_LEVEL_ROOT:
2138 case CPU_LEVEL_CPUSET:
2139 error = cpuset_which(which, id, &p, &ttd, &set);
2145 case CPU_WHICH_TIDPID:
2147 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:
2160 if (level == CPU_LEVEL_ROOT)
2161 nset = cpuset_refroot(set);
2163 nset = cpuset_refbase(set);
2164 error = cpuset_modify(nset, mask);
2168 case CPU_LEVEL_WHICH:
2171 error = cpuset_setthread(id, mask);
2174 error = cpuset_setproc(id, NULL, mask, NULL, false);
2176 case CPU_WHICH_TIDPID:
2177 if (id > PID_MAX || id == -1)
2178 error = cpuset_setthread(id, mask);
2180 error = cpuset_setproc(id, NULL, mask, NULL,
2183 case CPU_WHICH_CPUSET:
2184 case CPU_WHICH_JAIL:
2185 error = cpuset_which(which, id, &p, &ttd, &set);
2187 error = cpuset_modify(set, mask);
2192 case CPU_WHICH_INTRHANDLER:
2193 case CPU_WHICH_ITHREAD:
2194 error = intr_setaffinity(id, which, mask);
2209 user_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2210 id_t id, size_t cpusetsize, const cpuset_t *maskp, const struct cpuset_copy_cb *cb)
2216 size = min(cpusetsize, sizeof(cpuset_t));
2217 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
2218 error = cb->cpuset_copyin(maskp, mask, size);
2222 * Verify that no high bits are set.
2224 if (cpusetsize > sizeof(cpuset_t)) {
2225 const char *end, *cp;
2227 end = cp = (const char *)&maskp->__bits;
2229 cp += sizeof(cpuset_t);
2244 error = kern_cpuset_setaffinity(td, level, which, id, mask);
2251 #ifndef _SYS_SYSPROTO_H_
2252 struct cpuset_getdomain_args {
2256 size_t domainsetsize;
2262 sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
2265 return (kern_cpuset_getdomain(td, uap->level, uap->which,
2266 uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set));
2270 kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2271 id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp,
2272 const struct cpuset_copy_cb *cb)
2274 struct domainset outset;
2276 struct cpuset *nset;
2278 struct domainset *dset;
2283 if (domainsetsize < sizeof(domainset_t) ||
2284 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2286 error = cpuset_check_capabilities(td, level, which, id);
2289 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2290 bzero(&outset, sizeof(outset));
2291 error = cpuset_which2(&which, id, &p, &ttd, &set);
2295 case CPU_LEVEL_ROOT:
2296 case CPU_LEVEL_CPUSET:
2301 set = cpuset_ref(ttd->td_cpuset);
2304 case CPU_WHICH_CPUSET:
2305 case CPU_WHICH_JAIL:
2308 case CPU_WHICH_INTRHANDLER:
2309 case CPU_WHICH_ITHREAD:
2310 case CPU_WHICH_DOMAIN:
2314 if (level == CPU_LEVEL_ROOT)
2315 nset = cpuset_refroot(set);
2317 nset = cpuset_refbase(set);
2318 domainset_copy(nset->cs_domain, &outset);
2321 case CPU_LEVEL_WHICH:
2325 domainset_copy(ttd->td_cpuset->cs_domain, &outset);
2329 FOREACH_THREAD_IN_PROC(p, ttd) {
2331 dset = ttd->td_cpuset->cs_domain;
2332 /* Show all domains in the proc. */
2333 DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
2334 /* Last policy wins. */
2335 outset.ds_policy = dset->ds_policy;
2336 outset.ds_prefer = dset->ds_prefer;
2340 case CPU_WHICH_CPUSET:
2341 case CPU_WHICH_JAIL:
2342 domainset_copy(set->cs_domain, &outset);
2345 case CPU_WHICH_INTRHANDLER:
2346 case CPU_WHICH_ITHREAD:
2347 case CPU_WHICH_DOMAIN:
2361 * Translate prefer into a set containing only the preferred domain,
2362 * not the entire fallback set.
2364 if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
2365 DOMAINSET_ZERO(&outset.ds_mask);
2366 DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
2368 DOMAINSET_COPY(&outset.ds_mask, mask);
2370 error = cb->cpuset_copyout(mask, maskp, domainsetsize);
2372 if (suword32(policyp, outset.ds_policy) != 0)
2379 #ifndef _SYS_SYSPROTO_H_
2380 struct cpuset_setdomain_args {
2384 size_t domainsetsize;
2390 sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
2393 return (kern_cpuset_setdomain(td, uap->level, uap->which,
2394 uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set));
2398 kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
2399 id_t id, size_t domainsetsize, const domainset_t *maskp, int policy,
2400 const struct cpuset_copy_cb *cb)
2402 struct cpuset *nset;
2406 struct domainset domain;
2410 if (domainsetsize < sizeof(domainset_t) ||
2411 domainsetsize > DOMAINSET_MAXSIZE / NBBY)
2413 if (policy <= DOMAINSET_POLICY_INVALID ||
2414 policy > DOMAINSET_POLICY_MAX)
2416 error = cpuset_check_capabilities(td, level, which, id);
2419 memset(&domain, 0, sizeof(domain));
2420 mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
2421 error = cb->cpuset_copyin(maskp, mask, domainsetsize);
2425 * Verify that no high bits are set.
2427 if (domainsetsize > sizeof(domainset_t)) {
2431 end = cp = (char *)&mask->__bits;
2432 end += domainsetsize;
2433 cp += sizeof(domainset_t);
2440 if (DOMAINSET_EMPTY(mask)) {
2444 DOMAINSET_COPY(mask, &domain.ds_mask);
2445 domain.ds_policy = policy;
2448 * Sanitize the provided mask.
2450 if (!DOMAINSET_SUBSET(&all_domains, &domain.ds_mask)) {
2455 /* Translate preferred policy into a mask and fallback. */
2456 if (policy == DOMAINSET_POLICY_PREFER) {
2457 /* Only support a single preferred domain. */
2458 if (DOMAINSET_COUNT(&domain.ds_mask) != 1) {
2462 domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
2463 /* This will be constrained by domainset_shadow(). */
2464 DOMAINSET_COPY(&all_domains, &domain.ds_mask);
2468 * When given an impossible policy, fall back to interleaving
2469 * across all domains.
2471 if (domainset_empty_vm(&domain))
2472 domainset_copy(domainset2, &domain);
2475 case CPU_LEVEL_ROOT:
2476 case CPU_LEVEL_CPUSET:
2477 error = cpuset_which(which, id, &p, &ttd, &set);
2483 case CPU_WHICH_TIDPID:
2485 set = cpuset_ref(ttd->td_cpuset);
2489 case CPU_WHICH_CPUSET:
2490 case CPU_WHICH_JAIL:
2493 case CPU_WHICH_INTRHANDLER:
2494 case CPU_WHICH_ITHREAD:
2495 case CPU_WHICH_DOMAIN:
2499 if (level == CPU_LEVEL_ROOT)
2500 nset = cpuset_refroot(set);
2502 nset = cpuset_refbase(set);
2503 error = cpuset_modify_domain(nset, &domain);
2507 case CPU_LEVEL_WHICH:
2510 error = _cpuset_setthread(id, NULL, &domain);
2513 error = cpuset_setproc(id, NULL, NULL, &domain, false);
2515 case CPU_WHICH_TIDPID:
2516 if (id > PID_MAX || id == -1)
2517 error = _cpuset_setthread(id, NULL, &domain);
2519 error = cpuset_setproc(id, NULL, NULL, &domain,
2522 case CPU_WHICH_CPUSET:
2523 case CPU_WHICH_JAIL:
2524 error = cpuset_which(which, id, &p, &ttd, &set);
2526 error = cpuset_modify_domain(set, &domain);
2531 case CPU_WHICH_INTRHANDLER:
2532 case CPU_WHICH_ITHREAD:
2550 ddb_display_bitset(const struct bitset *set, int size)
2554 for (once = 0, bit = 0; bit < size; bit++) {
2555 if (CPU_ISSET(bit, set)) {
2557 db_printf("%d", bit);
2560 db_printf(",%d", bit);
2564 db_printf("<none>");
2568 ddb_display_cpuset(const cpuset_t *set)
2570 ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
2574 ddb_display_domainset(const domainset_t *set)
2576 ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
2579 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
2583 LIST_FOREACH(set, &cpuset_ids, cs_link) {
2584 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
2585 set, set->cs_id, refcount_load(&set->cs_ref), set->cs_flags,
2586 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
2587 db_printf(" cpu mask=");
2588 ddb_display_cpuset(&set->cs_mask);
2590 db_printf(" domain policy %d prefer %d mask=",
2591 set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
2592 ddb_display_domainset(&set->cs_domain->ds_mask);
2599 DB_SHOW_COMMAND(domainsets, db_show_domainsets)
2601 struct domainset *set;
2603 LIST_FOREACH(set, &cpuset_domains, ds_link) {
2604 db_printf("set=%p policy %d prefer %d cnt %d\n",
2605 set, set->ds_policy, set->ds_prefer, set->ds_cnt);
2606 db_printf(" mask =");
2607 ddb_display_domainset(&set->ds_mask);