2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
7 * Copyright (c) 2008 Nokia Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
48 #include <sys/refcount.h>
49 #include <sys/sched.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/capsicum.h>
53 #include <sys/cpuset.h>
55 #include <sys/queue.h>
56 #include <sys/libkern.h>
57 #include <sys/limits.h>
59 #include <sys/interrupt.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_param.h>
71 * cpusets provide a mechanism for creating and manipulating sets of
72 * processors for the purpose of constraining the scheduling of threads to
73 * specific processors.
75 * Each process belongs to an identified set, by default this is set 1. Each
76 * thread may further restrict the cpus it may run on to a subset of this
77 * named set. This creates an anonymous set which other threads and processes
78 * may not join by number.
80 * The named set is referred to herein as the 'base' set to avoid ambiguity.
81 * This set is usually a child of a 'root' set while the anonymous set may
82 * simply be referred to as a mask. In the syscall api these are referred to
83 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
85 * Threads inherit their set from their creator whether it be anonymous or
86 * not. This means that anonymous sets are immutable because they may be
87 * shared. To modify an anonymous set a new set is created with the desired
88 * mask and the same parent as the existing anonymous set. This gives the
89 * illusion of each thread having a private mask.
91 * Via the syscall apis a user may ask to retrieve or modify the root, base,
92 * or mask that is discovered via a pid, tid, or setid. Modifying a set
93 * modifies all numbered and anonymous child sets to comply with the new mask.
94 * Modifying a pid or tid's mask applies only to that tid but must still
95 * exist within the assigned parent set.
97 * A thread may not be assigned to a group separate from other threads in
98 * the process. This is to remove ambiguity when the setid is queried with
99 * a pid argument. There is no other technical limitation.
101 * This somewhat complex arrangement is intended to make it easy for
102 * applications to query available processors and bind their threads to
103 * specific processors while also allowing administrators to dynamically
104 * reprovision by changing sets which apply to groups of processes.
106 * A simple application should not concern itself with sets at all and
107 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
108 * meaning 'curthread'. It may query available cpus for that tid with a
109 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
111 static uma_zone_t cpuset_zone;
112 static struct mtx cpuset_lock;
113 static struct setlist cpuset_ids;
114 static struct unrhdr *cpuset_unr;
115 static struct cpuset *cpuset_zero, *cpuset_default;
117 /* Return the size of cpuset_t at the kernel level */
118 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
119 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
121 cpuset_t *cpuset_root;
122 cpuset_t cpuset_domain[MAXMEMDOM];
125 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
128 cpuset_ref(struct cpuset *set)
131 refcount_acquire(&set->cs_ref);
136 * Walks up the tree from 'set' to find the root. Returns the root
139 static struct cpuset *
140 cpuset_refroot(struct cpuset *set)
143 for (; set->cs_parent != NULL; set = set->cs_parent)
144 if (set->cs_flags & CPU_SET_ROOT)
152 * Find the first non-anonymous set starting from 'set'. Returns this set
153 * referenced. May return the passed in set with an extra ref if it is
156 static struct cpuset *
157 cpuset_refbase(struct cpuset *set)
160 if (set->cs_id == CPUSET_INVALID)
161 set = set->cs_parent;
168 * Release a reference in a context where it is safe to allocate.
171 cpuset_rel(struct cpuset *set)
175 if (refcount_release(&set->cs_ref) == 0)
177 mtx_lock_spin(&cpuset_lock);
178 LIST_REMOVE(set, cs_siblings);
180 if (id != CPUSET_INVALID)
181 LIST_REMOVE(set, cs_link);
182 mtx_unlock_spin(&cpuset_lock);
183 cpuset_rel(set->cs_parent);
184 uma_zfree(cpuset_zone, set);
185 if (id != CPUSET_INVALID)
186 free_unr(cpuset_unr, id);
190 * Deferred release must be used when in a context that is not safe to
191 * allocate/free. This places any unreferenced sets on the list 'head'.
194 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
197 if (refcount_release(&set->cs_ref) == 0)
199 mtx_lock_spin(&cpuset_lock);
200 LIST_REMOVE(set, cs_siblings);
201 if (set->cs_id != CPUSET_INVALID)
202 LIST_REMOVE(set, cs_link);
203 LIST_INSERT_HEAD(head, set, cs_link);
204 mtx_unlock_spin(&cpuset_lock);
208 * Complete a deferred release. Removes the set from the list provided to
212 cpuset_rel_complete(struct cpuset *set)
214 LIST_REMOVE(set, cs_link);
215 cpuset_rel(set->cs_parent);
216 uma_zfree(cpuset_zone, set);
220 * Find a set based on an id. Returns it with a ref.
222 static struct cpuset *
223 cpuset_lookup(cpusetid_t setid, struct thread *td)
227 if (setid == CPUSET_INVALID)
229 mtx_lock_spin(&cpuset_lock);
230 LIST_FOREACH(set, &cpuset_ids, cs_link)
231 if (set->cs_id == setid)
235 mtx_unlock_spin(&cpuset_lock);
237 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
238 if (set != NULL && jailed(td->td_ucred)) {
239 struct cpuset *jset, *tset;
241 jset = td->td_ucred->cr_prison->pr_cpuset;
242 for (tset = set; tset != NULL; tset = tset->cs_parent)
255 * Create a set in the space provided in 'set' with the provided parameters.
256 * The set is returned with a single ref. May return EDEADLK if the set
257 * will have no valid cpu based on restrictions from the parent.
260 _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
264 if (!CPU_OVERLAP(&parent->cs_mask, mask))
266 CPU_COPY(mask, &set->cs_mask);
267 LIST_INIT(&set->cs_children);
268 refcount_init(&set->cs_ref, 1);
270 mtx_lock_spin(&cpuset_lock);
271 CPU_AND(&set->cs_mask, &parent->cs_mask);
273 set->cs_parent = cpuset_ref(parent);
274 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
275 if (set->cs_id != CPUSET_INVALID)
276 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
277 mtx_unlock_spin(&cpuset_lock);
283 * Create a new non-anonymous set with the requested parent and mask. May
284 * return failures if the mask is invalid or a new number can not be
288 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
294 id = alloc_unr(cpuset_unr);
297 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
298 error = _cpuset_create(set, parent, mask, id);
301 free_unr(cpuset_unr, id);
302 uma_zfree(cpuset_zone, set);
308 * Recursively check for errors that would occur from applying mask to
309 * the tree of sets starting at 'set'. Checks for sets that would become
310 * empty as well as RDONLY flags.
313 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
319 mtx_assert(&cpuset_lock, MA_OWNED);
320 if (set->cs_flags & CPU_SET_RDONLY)
323 if (!CPU_OVERLAP(&set->cs_mask, mask))
325 CPU_COPY(&set->cs_mask, &newmask);
326 CPU_AND(&newmask, mask);
328 CPU_COPY(mask, &newmask);
330 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
331 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
337 * Applies the mask 'mask' without checking for empty sets or permissions.
340 cpuset_update(struct cpuset *set, cpuset_t *mask)
344 mtx_assert(&cpuset_lock, MA_OWNED);
345 CPU_AND(&set->cs_mask, mask);
346 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
347 cpuset_update(nset, &set->cs_mask);
353 * Modify the set 'set' to use a copy of the mask provided. Apply this new
354 * mask to restrict all children in the tree. Checks for validity before
355 * applying the changes.
358 cpuset_modify(struct cpuset *set, cpuset_t *mask)
363 error = priv_check(curthread, PRIV_SCHED_CPUSET);
367 * In case we are called from within the jail
368 * we do not allow modifying the dedicated root
369 * cpuset of the jail but may still allow to
372 if (jailed(curthread->td_ucred) &&
373 set->cs_flags & CPU_SET_ROOT)
376 * Verify that we have access to this set of
379 root = set->cs_parent;
380 if (root && !CPU_SUBSET(&root->cs_mask, mask))
382 mtx_lock_spin(&cpuset_lock);
383 error = cpuset_testupdate(set, mask, 0);
386 CPU_COPY(mask, &set->cs_mask);
387 cpuset_update(set, mask);
389 mtx_unlock_spin(&cpuset_lock);
395 * Resolve the 'which' parameter of several cpuset apis.
397 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
398 * checks for permission via p_cansched().
400 * For WHICH_SET returns a valid set with a new reference.
402 * -1 may be supplied for any argument to mean the current proc/thread or
403 * the base set of the current thread. May fail with ESRCH/EPERM.
406 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
407 struct cpuset **setp)
424 if ((p = pfind(id)) == NULL)
439 case CPU_WHICH_CPUSET:
441 thread_lock(curthread);
442 set = cpuset_refbase(curthread->td_cpuset);
443 thread_unlock(curthread);
445 set = cpuset_lookup(id, curthread);
453 /* Find `set' for prison with given id. */
456 sx_slock(&allprison_lock);
457 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
458 sx_sunlock(&allprison_lock);
461 cpuset_ref(pr->pr_cpuset);
462 *setp = pr->pr_cpuset;
463 mtx_unlock(&pr->pr_mtx);
467 case CPU_WHICH_DOMAIN:
472 error = p_cansched(curthread, p);
478 td = FIRST_THREAD_IN_PROC(p);
485 * Create an anonymous set with the provided mask in the space provided by
486 * 'fset'. If the passed in set is anonymous we use its parent otherwise
487 * the new set is a child of 'set'.
490 cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
492 struct cpuset *parent;
494 if (set->cs_id == CPUSET_INVALID)
495 parent = set->cs_parent;
498 if (!CPU_SUBSET(&parent->cs_mask, mask))
500 return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
504 * Handle two cases for replacing the base set or mask of an entire process.
506 * 1) Set is non-null and mask is null. This reparents all anonymous sets
507 * to the provided set and replaces all non-anonymous td_cpusets with the
509 * 2) Mask is non-null and set is null. This replaces or creates anonymous
510 * sets for every thread with the existing base as a parent.
512 * This is overly complicated because we can't allocate while holding a
513 * spinlock and spinlocks must be held while changing and examining thread
517 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
519 struct setlist freelist;
520 struct setlist droplist;
521 struct cpuset *tdset;
530 * The algorithm requires two passes due to locking considerations.
532 * 1) Lookup the process and acquire the locks in the required order.
533 * 2) If enough cpusets have not been allocated release the locks and
534 * allocate them. Loop.
536 LIST_INIT(&freelist);
537 LIST_INIT(&droplist);
540 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
543 if (nfree >= p->p_numthreads)
545 threads = p->p_numthreads;
547 for (; nfree < threads; nfree++) {
548 nset = uma_zalloc(cpuset_zone, M_WAITOK);
549 LIST_INSERT_HEAD(&freelist, nset, cs_link);
552 PROC_LOCK_ASSERT(p, MA_OWNED);
554 * Now that the appropriate locks are held and we have enough cpusets,
555 * make sure the operation will succeed before applying changes. The
556 * proc lock prevents td_cpuset from changing between calls.
559 FOREACH_THREAD_IN_PROC(p, td) {
561 tdset = td->td_cpuset;
563 * Verify that a new mask doesn't specify cpus outside of
564 * the set the thread is a member of.
567 if (tdset->cs_id == CPUSET_INVALID)
568 tdset = tdset->cs_parent;
569 if (!CPU_SUBSET(&tdset->cs_mask, mask))
572 * Verify that a new set won't leave an existing thread
573 * mask without a cpu to run on. It can, however, restrict
576 } else if (tdset->cs_id == CPUSET_INVALID) {
577 if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
585 * Replace each thread's cpuset while using deferred release. We
586 * must do this because the thread lock must be held while operating
587 * on the thread and this limits the type of operations allowed.
589 FOREACH_THREAD_IN_PROC(p, td) {
592 * If we presently have an anonymous set or are applying a
593 * mask we must create an anonymous shadow set. That is
594 * either parented to our existing base or the supplied set.
596 * If we have a base set with no anonymous shadow we simply
597 * replace it outright.
599 tdset = td->td_cpuset;
600 if (tdset->cs_id == CPUSET_INVALID || mask) {
601 nset = LIST_FIRST(&freelist);
602 LIST_REMOVE(nset, cs_link);
604 error = cpuset_shadow(tdset, nset, mask);
606 error = _cpuset_create(nset, set,
607 &tdset->cs_mask, CPUSET_INVALID);
609 LIST_INSERT_HEAD(&freelist, nset, cs_link);
614 nset = cpuset_ref(set);
615 cpuset_rel_defer(&droplist, tdset);
616 td->td_cpuset = nset;
623 while ((nset = LIST_FIRST(&droplist)) != NULL)
624 cpuset_rel_complete(nset);
625 while ((nset = LIST_FIRST(&freelist)) != NULL) {
626 LIST_REMOVE(nset, cs_link);
627 uma_zfree(cpuset_zone, nset);
633 * Return a string representing a valid layout for a cpuset_t object.
634 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
637 cpusetobj_strprint(char *buf, const cpuset_t *set)
640 size_t i, bytesp, bufsiz;
644 bufsiz = CPUSETBUFSIZ;
646 for (i = 0; i < (_NCPUWORDS - 1); i++) {
647 bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
651 snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
656 * Build a valid cpuset_t object from a string representation.
657 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
660 cpusetobj_strscan(cpuset_t *set, const char *buf)
665 if (strlen(buf) > CPUSETBUFSIZ - 1)
668 /* Allow to pass a shorter version of the mask when necessary. */
670 for (i = 0; buf[i] != '\0'; i++)
673 if (nwords > _NCPUWORDS)
677 for (i = 0; i < (nwords - 1); i++) {
678 ret = sscanf(buf, "%lx,", &set->__bits[i]);
679 if (ret == 0 || ret == -1)
681 buf = strstr(buf, ",");
686 ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
687 if (ret == 0 || ret == -1)
693 * Apply an anonymous mask to a single thread.
696 cpuset_setthread(lwpid_t id, cpuset_t *mask)
704 nset = uma_zalloc(cpuset_zone, M_WAITOK);
705 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
710 error = cpuset_shadow(td->td_cpuset, nset, mask);
713 td->td_cpuset = nset;
723 uma_zfree(cpuset_zone, nset);
728 * Apply new cpumask to the ithread.
731 cpuset_setithread(lwpid_t id, int cpu)
733 struct cpuset *nset, *rset;
734 struct cpuset *parent, *old_set;
741 nset = uma_zalloc(cpuset_zone, M_WAITOK);
742 rset = uma_zalloc(cpuset_zone, M_WAITOK);
743 cs_id = CPUSET_INVALID;
747 CPU_COPY(cpuset_root, &mask);
751 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set);
752 if (error != 0 || ((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID))
755 /* cpuset_which() returns with PROC_LOCK held. */
756 old_set = td->td_cpuset;
761 * roll back to default set. We're not using cpuset_shadow()
762 * here because we can fail CPU_SUBSET() check. This can happen
763 * if default set does not contain all CPUs.
765 error = _cpuset_create(nset, cpuset_default, &mask,
771 if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID &&
772 old_set->cs_parent->cs_id == 1)) {
775 * Current set is either default (1) or
776 * shadowed version of default set.
778 * Allocate new root set to be able to shadow it
781 error = _cpuset_create(rset, cpuset_zero,
782 &cpuset_zero->cs_mask, cs_id);
787 rset->cs_flags |= CPU_SET_ROOT;
790 cs_id = CPUSET_INVALID;
792 /* Assume existing set was already allocated by previous call */
797 error = cpuset_shadow(parent, nset, &mask);
801 td->td_cpuset = nset;
812 uma_zfree(cpuset_zone, nset);
814 uma_zfree(cpuset_zone, rset);
815 if (cs_id != CPUSET_INVALID)
816 free_unr(cpuset_unr, cs_id);
822 * Creates system-wide cpusets and the cpuset for thread0 including two
825 * 0 - The root set which should represent all valid processors in the
826 * system. It is initially created with a mask of all processors
827 * because we don't know what processors are valid until cpuset_init()
828 * runs. This set is immutable.
829 * 1 - The default set which all processes are a member of until changed.
830 * This allows an administrator to move all threads off of given cpus to
831 * dedicate them to high priority tasks or save power etc.
839 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
840 NULL, NULL, UMA_ALIGN_PTR, 0);
841 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
844 * Create the root system set for the whole machine. Doesn't use
845 * cpuset_create() due to NULL parent.
847 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
848 CPU_FILL(&set->cs_mask);
849 LIST_INIT(&set->cs_children);
850 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
852 set->cs_flags = CPU_SET_ROOT;
854 cpuset_root = &set->cs_mask;
857 * Now derive a default, modifiable set from that to give out.
859 set = uma_zalloc(cpuset_zone, M_WAITOK);
860 error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
861 KASSERT(error == 0, ("Error creating default set: %d\n", error));
862 cpuset_default = set;
865 * Initialize the unit allocator. 0 and 1 are allocated above.
867 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
870 * If MD code has not initialized per-domain cpusets, place all
873 for (i = 0; i < MAXMEMDOM; i++)
874 if (!CPU_EMPTY(&cpuset_domain[i]))
876 CPU_COPY(&all_cpus, &cpuset_domain[0]);
883 * Create a cpuset, which would be cpuset_create() but
884 * mark the new 'set' as root.
886 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
889 * In case of no error, returns the set in *setp locked with a reference.
892 cpuset_create_root(struct prison *pr, struct cpuset **setp)
897 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
898 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
900 error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
904 KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
905 __func__, __LINE__));
907 /* Mark the set as root. */
909 set->cs_flags |= CPU_SET_ROOT;
915 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
919 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
920 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
923 error = cpuset_setproc(p->p_pid, set, NULL);
931 * This is called once the final set of system cpus is known. Modifies
932 * the root set and all children and mark the root read-only.
935 cpuset_init(void *arg)
940 if (cpuset_modify(cpuset_zero, &mask))
941 panic("Can't set initial cpuset mask.\n");
942 cpuset_zero->cs_flags |= CPU_SET_RDONLY;
944 SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
946 #ifndef _SYS_SYSPROTO_H_
952 sys_cpuset(struct thread *td, struct cpuset_args *uap)
959 root = cpuset_refroot(td->td_cpuset);
961 error = cpuset_create(&set, root, &root->cs_mask);
965 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
967 error = cpuset_setproc(-1, set, NULL);
972 #ifndef _SYS_SYSPROTO_H_
973 struct cpuset_setid_args {
980 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
983 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
987 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
988 id_t id, cpusetid_t setid)
994 * Presently we only support per-process sets.
996 if (which != CPU_WHICH_PID)
998 set = cpuset_lookup(setid, td);
1001 error = cpuset_setproc(id, set, NULL);
1006 #ifndef _SYS_SYSPROTO_H_
1007 struct cpuset_getid_args {
1015 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1018 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1023 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1024 id_t id, cpusetid_t *setid)
1026 struct cpuset *nset;
1033 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1035 error = cpuset_which(which, id, &p, &ttd, &set);
1042 set = cpuset_refbase(ttd->td_cpuset);
1046 case CPU_WHICH_CPUSET:
1047 case CPU_WHICH_JAIL:
1050 case CPU_WHICH_DOMAIN:
1054 case CPU_LEVEL_ROOT:
1055 nset = cpuset_refroot(set);
1059 case CPU_LEVEL_CPUSET:
1061 case CPU_LEVEL_WHICH:
1067 error = copyout(&tmpid, setid, sizeof(tmpid));
1072 #ifndef _SYS_SYSPROTO_H_
1073 struct cpuset_getaffinity_args {
1082 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1085 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
1086 uap->id, uap->cpusetsize, uap->mask));
1090 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1091 id_t id, size_t cpusetsize, cpuset_t *maskp)
1094 struct cpuset *nset;
1101 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1103 /* In Capability mode, you can only get your own CPU set. */
1104 if (IN_CAPABILITY_MODE(td)) {
1105 if (level != CPU_LEVEL_WHICH)
1107 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1113 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1114 error = cpuset_which(which, id, &p, &ttd, &set);
1118 case CPU_LEVEL_ROOT:
1119 case CPU_LEVEL_CPUSET:
1124 set = cpuset_ref(ttd->td_cpuset);
1127 case CPU_WHICH_CPUSET:
1128 case CPU_WHICH_JAIL:
1131 case CPU_WHICH_INTRHANDLER:
1132 case CPU_WHICH_ITHREAD:
1133 case CPU_WHICH_DOMAIN:
1137 if (level == CPU_LEVEL_ROOT)
1138 nset = cpuset_refroot(set);
1140 nset = cpuset_refbase(set);
1141 CPU_COPY(&nset->cs_mask, mask);
1144 case CPU_LEVEL_WHICH:
1148 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1152 FOREACH_THREAD_IN_PROC(p, ttd) {
1154 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1158 case CPU_WHICH_CPUSET:
1159 case CPU_WHICH_JAIL:
1160 CPU_COPY(&set->cs_mask, mask);
1163 case CPU_WHICH_INTRHANDLER:
1164 case CPU_WHICH_ITHREAD:
1165 error = intr_getaffinity(id, which, mask);
1167 case CPU_WHICH_DOMAIN:
1168 if (id < 0 || id >= MAXMEMDOM)
1171 CPU_COPY(&cpuset_domain[id], mask);
1184 error = copyout(mask, maskp, size);
1190 #ifndef _SYS_SYSPROTO_H_
1191 struct cpuset_setaffinity_args {
1196 const cpuset_t *mask;
1200 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1203 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
1204 uap->id, uap->cpusetsize, uap->mask));
1208 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1209 id_t id, size_t cpusetsize, const cpuset_t *maskp)
1211 struct cpuset *nset;
1218 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1220 /* In Capability mode, you can only set your own CPU set. */
1221 if (IN_CAPABILITY_MODE(td)) {
1222 if (level != CPU_LEVEL_WHICH)
1224 if (which != CPU_WHICH_TID && which != CPU_WHICH_PID)
1229 mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1230 error = copyin(maskp, mask, cpusetsize);
1234 * Verify that no high bits are set.
1236 if (cpusetsize > sizeof(cpuset_t)) {
1240 end = cp = (char *)&mask->__bits;
1242 cp += sizeof(cpuset_t);
1251 case CPU_LEVEL_ROOT:
1252 case CPU_LEVEL_CPUSET:
1253 error = cpuset_which(which, id, &p, &ttd, &set);
1260 set = cpuset_ref(ttd->td_cpuset);
1264 case CPU_WHICH_CPUSET:
1265 case CPU_WHICH_JAIL:
1268 case CPU_WHICH_INTRHANDLER:
1269 case CPU_WHICH_ITHREAD:
1270 case CPU_WHICH_DOMAIN:
1274 if (level == CPU_LEVEL_ROOT)
1275 nset = cpuset_refroot(set);
1277 nset = cpuset_refbase(set);
1278 error = cpuset_modify(nset, mask);
1282 case CPU_LEVEL_WHICH:
1285 error = cpuset_setthread(id, mask);
1288 error = cpuset_setproc(id, NULL, mask);
1290 case CPU_WHICH_CPUSET:
1291 case CPU_WHICH_JAIL:
1292 error = cpuset_which(which, id, &p, &ttd, &set);
1294 error = cpuset_modify(set, mask);
1299 case CPU_WHICH_INTRHANDLER:
1300 case CPU_WHICH_ITHREAD:
1301 error = intr_setaffinity(id, which, mask);
1319 ddb_display_cpuset(const cpuset_t *set)
1323 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1324 if (CPU_ISSET(cpu, set)) {
1326 db_printf("%d", cpu);
1329 db_printf(",%d", cpu);
1333 db_printf("<none>");
1336 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1340 LIST_FOREACH(set, &cpuset_ids, cs_link) {
1341 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1342 set, set->cs_id, set->cs_ref, set->cs_flags,
1343 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1344 db_printf(" mask=");
1345 ddb_display_cpuset(&set->cs_mask);