2 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
5 * Copyright (c) 2008 Nokia Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
46 #include <sys/refcount.h>
47 #include <sys/sched.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/cpuset.h>
52 #include <sys/queue.h>
53 #include <sys/libkern.h>
54 #include <sys/limits.h>
56 #include <sys/interrupt.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_param.h>
68 * cpusets provide a mechanism for creating and manipulating sets of
69 * processors for the purpose of constraining the scheduling of threads to
70 * specific processors.
72 * Each process belongs to an identified set, by default this is set 1. Each
73 * thread may further restrict the cpus it may run on to a subset of this
74 * named set. This creates an anonymous set which other threads and processes
75 * may not join by number.
77 * The named set is referred to herein as the 'base' set to avoid ambiguity.
78 * This set is usually a child of a 'root' set while the anonymous set may
79 * simply be referred to as a mask. In the syscall api these are referred to
80 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
82 * Threads inherit their set from their creator whether it be anonymous or
83 * not. This means that anonymous sets are immutable because they may be
84 * shared. To modify an anonymous set a new set is created with the desired
85 * mask and the same parent as the existing anonymous set. This gives the
86 * illusion of each thread having a private mask.
88 * Via the syscall apis a user may ask to retrieve or modify the root, base,
89 * or mask that is discovered via a pid, tid, or setid. Modifying a set
90 * modifies all numbered and anonymous child sets to comply with the new mask.
91 * Modifying a pid or tid's mask applies only to that tid but must still
92 * exist within the assigned parent set.
94 * A thread may not be assigned to a group separate from other threads in
95 * the process. This is to remove ambiguity when the setid is queried with
96 * a pid argument. There is no other technical limitation.
98 * This somewhat complex arrangement is intended to make it easy for
99 * applications to query available processors and bind their threads to
100 * specific processors while also allowing administrators to dynamically
101 * reprovision by changing sets which apply to groups of processes.
103 * A simple application should not concern itself with sets at all and
104 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
105 * meaning 'curthread'. It may query available cpus for that tid with a
106 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
108 static uma_zone_t cpuset_zone;
109 static struct mtx cpuset_lock;
110 static struct setlist cpuset_ids;
111 static struct unrhdr *cpuset_unr;
112 static struct cpuset *cpuset_zero, *cpuset_default;
114 /* Return the size of cpuset_t at the kernel level */
115 SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
116 SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
118 cpuset_t *cpuset_root;
119 cpuset_t cpuset_domain[MAXMEMDOM];
122 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
125 cpuset_ref(struct cpuset *set)
128 refcount_acquire(&set->cs_ref);
133 * Walks up the tree from 'set' to find the root. Returns the root
136 static struct cpuset *
137 cpuset_refroot(struct cpuset *set)
140 for (; set->cs_parent != NULL; set = set->cs_parent)
141 if (set->cs_flags & CPU_SET_ROOT)
149 * Find the first non-anonymous set starting from 'set'. Returns this set
150 * referenced. May return the passed in set with an extra ref if it is
153 static struct cpuset *
154 cpuset_refbase(struct cpuset *set)
157 if (set->cs_id == CPUSET_INVALID)
158 set = set->cs_parent;
165 * Release a reference in a context where it is safe to allocate.
168 cpuset_rel(struct cpuset *set)
172 if (refcount_release(&set->cs_ref) == 0)
174 mtx_lock_spin(&cpuset_lock);
175 LIST_REMOVE(set, cs_siblings);
177 if (id != CPUSET_INVALID)
178 LIST_REMOVE(set, cs_link);
179 mtx_unlock_spin(&cpuset_lock);
180 cpuset_rel(set->cs_parent);
181 uma_zfree(cpuset_zone, set);
182 if (id != CPUSET_INVALID)
183 free_unr(cpuset_unr, id);
187 * Deferred release must be used when in a context that is not safe to
188 * allocate/free. This places any unreferenced sets on the list 'head'.
191 cpuset_rel_defer(struct setlist *head, struct cpuset *set)
194 if (refcount_release(&set->cs_ref) == 0)
196 mtx_lock_spin(&cpuset_lock);
197 LIST_REMOVE(set, cs_siblings);
198 if (set->cs_id != CPUSET_INVALID)
199 LIST_REMOVE(set, cs_link);
200 LIST_INSERT_HEAD(head, set, cs_link);
201 mtx_unlock_spin(&cpuset_lock);
205 * Complete a deferred release. Removes the set from the list provided to
209 cpuset_rel_complete(struct cpuset *set)
211 LIST_REMOVE(set, cs_link);
212 cpuset_rel(set->cs_parent);
213 uma_zfree(cpuset_zone, set);
217 * Find a set based on an id. Returns it with a ref.
219 static struct cpuset *
220 cpuset_lookup(cpusetid_t setid, struct thread *td)
224 if (setid == CPUSET_INVALID)
226 mtx_lock_spin(&cpuset_lock);
227 LIST_FOREACH(set, &cpuset_ids, cs_link)
228 if (set->cs_id == setid)
232 mtx_unlock_spin(&cpuset_lock);
234 KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
235 if (set != NULL && jailed(td->td_ucred)) {
236 struct cpuset *jset, *tset;
238 jset = td->td_ucred->cr_prison->pr_cpuset;
239 for (tset = set; tset != NULL; tset = tset->cs_parent)
252 * Create a set in the space provided in 'set' with the provided parameters.
253 * The set is returned with a single ref. May return EDEADLK if the set
254 * will have no valid cpu based on restrictions from the parent.
257 _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
261 if (!CPU_OVERLAP(&parent->cs_mask, mask))
263 CPU_COPY(mask, &set->cs_mask);
264 LIST_INIT(&set->cs_children);
265 refcount_init(&set->cs_ref, 1);
267 mtx_lock_spin(&cpuset_lock);
268 CPU_AND(&set->cs_mask, &parent->cs_mask);
270 set->cs_parent = cpuset_ref(parent);
271 LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
272 if (set->cs_id != CPUSET_INVALID)
273 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
274 mtx_unlock_spin(&cpuset_lock);
280 * Create a new non-anonymous set with the requested parent and mask. May
281 * return failures if the mask is invalid or a new number can not be
285 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
291 id = alloc_unr(cpuset_unr);
294 *setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
295 error = _cpuset_create(set, parent, mask, id);
298 free_unr(cpuset_unr, id);
299 uma_zfree(cpuset_zone, set);
305 * Recursively check for errors that would occur from applying mask to
306 * the tree of sets starting at 'set'. Checks for sets that would become
307 * empty as well as RDONLY flags.
310 cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
316 mtx_assert(&cpuset_lock, MA_OWNED);
317 if (set->cs_flags & CPU_SET_RDONLY)
320 if (!CPU_OVERLAP(&set->cs_mask, mask))
322 CPU_COPY(&set->cs_mask, &newmask);
323 CPU_AND(&newmask, mask);
325 CPU_COPY(mask, &newmask);
327 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
328 if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
334 * Applies the mask 'mask' without checking for empty sets or permissions.
337 cpuset_update(struct cpuset *set, cpuset_t *mask)
341 mtx_assert(&cpuset_lock, MA_OWNED);
342 CPU_AND(&set->cs_mask, mask);
343 LIST_FOREACH(nset, &set->cs_children, cs_siblings)
344 cpuset_update(nset, &set->cs_mask);
350 * Modify the set 'set' to use a copy of the mask provided. Apply this new
351 * mask to restrict all children in the tree. Checks for validity before
352 * applying the changes.
355 cpuset_modify(struct cpuset *set, cpuset_t *mask)
360 error = priv_check(curthread, PRIV_SCHED_CPUSET);
364 * In case we are called from within the jail
365 * we do not allow modifying the dedicated root
366 * cpuset of the jail but may still allow to
369 if (jailed(curthread->td_ucred) &&
370 set->cs_flags & CPU_SET_ROOT)
373 * Verify that we have access to this set of
376 root = set->cs_parent;
377 if (root && !CPU_SUBSET(&root->cs_mask, mask))
379 mtx_lock_spin(&cpuset_lock);
380 error = cpuset_testupdate(set, mask, 0);
383 CPU_COPY(mask, &set->cs_mask);
384 cpuset_update(set, mask);
386 mtx_unlock_spin(&cpuset_lock);
392 * Resolve the 'which' parameter of several cpuset apis.
394 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also
395 * checks for permission via p_cansched().
397 * For WHICH_SET returns a valid set with a new reference.
399 * -1 may be supplied for any argument to mean the current proc/thread or
400 * the base set of the current thread. May fail with ESRCH/EPERM.
403 cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
404 struct cpuset **setp)
421 if ((p = pfind(id)) == NULL)
436 case CPU_WHICH_CPUSET:
438 thread_lock(curthread);
439 set = cpuset_refbase(curthread->td_cpuset);
440 thread_unlock(curthread);
442 set = cpuset_lookup(id, curthread);
450 /* Find `set' for prison with given id. */
453 sx_slock(&allprison_lock);
454 pr = prison_find_child(curthread->td_ucred->cr_prison, id);
455 sx_sunlock(&allprison_lock);
458 cpuset_ref(pr->pr_cpuset);
459 *setp = pr->pr_cpuset;
460 mtx_unlock(&pr->pr_mtx);
464 case CPU_WHICH_DOMAIN:
469 error = p_cansched(curthread, p);
475 td = FIRST_THREAD_IN_PROC(p);
482 * Create an anonymous set with the provided mask in the space provided by
483 * 'fset'. If the passed in set is anonymous we use its parent otherwise
484 * the new set is a child of 'set'.
487 cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
489 struct cpuset *parent;
491 if (set->cs_id == CPUSET_INVALID)
492 parent = set->cs_parent;
495 if (!CPU_SUBSET(&parent->cs_mask, mask))
497 return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
501 * Handle two cases for replacing the base set or mask of an entire process.
503 * 1) Set is non-null and mask is null. This reparents all anonymous sets
504 * to the provided set and replaces all non-anonymous td_cpusets with the
506 * 2) Mask is non-null and set is null. This replaces or creates anonymous
507 * sets for every thread with the existing base as a parent.
509 * This is overly complicated because we can't allocate while holding a
510 * spinlock and spinlocks must be held while changing and examining thread
514 cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
516 struct setlist freelist;
517 struct setlist droplist;
518 struct cpuset *tdset;
526 * The algorithm requires two passes due to locking considerations.
528 * 1) Lookup the process and acquire the locks in the required order.
529 * 2) If enough cpusets have not been allocated release the locks and
530 * allocate them. Loop.
532 LIST_INIT(&freelist);
533 LIST_INIT(&droplist);
536 error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
539 if (nfree >= p->p_numthreads)
541 threads = p->p_numthreads;
543 for (; nfree < threads; nfree++) {
544 nset = uma_zalloc(cpuset_zone, M_WAITOK);
545 LIST_INSERT_HEAD(&freelist, nset, cs_link);
548 PROC_LOCK_ASSERT(p, MA_OWNED);
550 * Now that the appropriate locks are held and we have enough cpusets,
551 * make sure the operation will succeed before applying changes. The
552 * proc lock prevents td_cpuset from changing between calls.
555 FOREACH_THREAD_IN_PROC(p, td) {
557 tdset = td->td_cpuset;
559 * Verify that a new mask doesn't specify cpus outside of
560 * the set the thread is a member of.
563 if (tdset->cs_id == CPUSET_INVALID)
564 tdset = tdset->cs_parent;
565 if (!CPU_SUBSET(&tdset->cs_mask, mask))
568 * Verify that a new set won't leave an existing thread
569 * mask without a cpu to run on. It can, however, restrict
572 } else if (tdset->cs_id == CPUSET_INVALID) {
573 if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
581 * Replace each thread's cpuset while using deferred release. We
582 * must do this because the thread lock must be held while operating
583 * on the thread and this limits the type of operations allowed.
585 FOREACH_THREAD_IN_PROC(p, td) {
588 * If we presently have an anonymous set or are applying a
589 * mask we must create an anonymous shadow set. That is
590 * either parented to our existing base or the supplied set.
592 * If we have a base set with no anonymous shadow we simply
593 * replace it outright.
595 tdset = td->td_cpuset;
596 if (tdset->cs_id == CPUSET_INVALID || mask) {
597 nset = LIST_FIRST(&freelist);
598 LIST_REMOVE(nset, cs_link);
600 error = cpuset_shadow(tdset, nset, mask);
602 error = _cpuset_create(nset, set,
603 &tdset->cs_mask, CPUSET_INVALID);
605 LIST_INSERT_HEAD(&freelist, nset, cs_link);
610 nset = cpuset_ref(set);
611 cpuset_rel_defer(&droplist, tdset);
612 td->td_cpuset = nset;
619 while ((nset = LIST_FIRST(&droplist)) != NULL)
620 cpuset_rel_complete(nset);
621 while ((nset = LIST_FIRST(&freelist)) != NULL) {
622 LIST_REMOVE(nset, cs_link);
623 uma_zfree(cpuset_zone, nset);
629 * Return a string representing a valid layout for a cpuset_t object.
630 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
633 cpusetobj_strprint(char *buf, const cpuset_t *set)
636 size_t i, bytesp, bufsiz;
640 bufsiz = CPUSETBUFSIZ;
642 for (i = 0; i < (_NCPUWORDS - 1); i++) {
643 bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
647 snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
652 * Build a valid cpuset_t object from a string representation.
653 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
656 cpusetobj_strscan(cpuset_t *set, const char *buf)
661 if (strlen(buf) > CPUSETBUFSIZ - 1)
664 /* Allow to pass a shorter version of the mask when necessary. */
666 for (i = 0; buf[i] != '\0'; i++)
669 if (nwords > _NCPUWORDS)
673 for (i = 0; i < (nwords - 1); i++) {
674 ret = sscanf(buf, "%lx,", &set->__bits[i]);
675 if (ret == 0 || ret == -1)
677 buf = strstr(buf, ",");
682 ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
683 if (ret == 0 || ret == -1)
689 * Apply an anonymous mask to a single thread.
692 cpuset_setthread(lwpid_t id, cpuset_t *mask)
700 nset = uma_zalloc(cpuset_zone, M_WAITOK);
701 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
706 error = cpuset_shadow(td->td_cpuset, nset, mask);
709 td->td_cpuset = nset;
719 uma_zfree(cpuset_zone, nset);
724 * Apply new cpumask to the ithread.
727 cpuset_setithread(lwpid_t id, int cpu)
729 struct cpuset *nset, *rset;
730 struct cpuset *parent, *old_set;
737 nset = uma_zalloc(cpuset_zone, M_WAITOK);
738 rset = uma_zalloc(cpuset_zone, M_WAITOK);
739 cs_id = CPUSET_INVALID;
743 CPU_COPY(cpuset_root, &mask);
747 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set);
748 if (error != 0 || ((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID))
751 /* cpuset_which() returns with PROC_LOCK held. */
752 old_set = td->td_cpuset;
757 * roll back to default set. We're not using cpuset_shadow()
758 * here because we can fail CPU_SUBSET() check. This can happen
759 * if default set does not contain all CPUs.
761 error = _cpuset_create(nset, cpuset_default, &mask,
767 if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID &&
768 old_set->cs_parent->cs_id == 1)) {
771 * Current set is either default (1) or
772 * shadowed version of default set.
774 * Allocate new root set to be able to shadow it
777 error = _cpuset_create(rset, cpuset_zero,
778 &cpuset_zero->cs_mask, cs_id);
783 rset->cs_flags |= CPU_SET_ROOT;
786 cs_id = CPUSET_INVALID;
788 /* Assume existing set was already allocated by previous call */
793 error = cpuset_shadow(parent, nset, &mask);
797 td->td_cpuset = nset;
808 uma_zfree(cpuset_zone, nset);
810 uma_zfree(cpuset_zone, rset);
811 if (cs_id != CPUSET_INVALID)
812 free_unr(cpuset_unr, cs_id);
818 * Creates system-wide cpusets and the cpuset for thread0 including two
821 * 0 - The root set which should represent all valid processors in the
822 * system. It is initially created with a mask of all processors
823 * because we don't know what processors are valid until cpuset_init()
824 * runs. This set is immutable.
825 * 1 - The default set which all processes are a member of until changed.
826 * This allows an administrator to move all threads off of given cpus to
827 * dedicate them to high priority tasks or save power etc.
835 cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
836 NULL, NULL, UMA_ALIGN_PTR, 0);
837 mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
840 * Create the root system set for the whole machine. Doesn't use
841 * cpuset_create() due to NULL parent.
843 set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
844 CPU_FILL(&set->cs_mask);
845 LIST_INIT(&set->cs_children);
846 LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
848 set->cs_flags = CPU_SET_ROOT;
850 cpuset_root = &set->cs_mask;
853 * Now derive a default, modifiable set from that to give out.
855 set = uma_zalloc(cpuset_zone, M_WAITOK);
856 error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
857 KASSERT(error == 0, ("Error creating default set: %d\n", error));
858 cpuset_default = set;
861 * Initialize the unit allocator. 0 and 1 are allocated above.
863 cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
866 * If MD code has not initialized per-domain cpusets, place all
869 for (i = 0; i < MAXMEMDOM; i++)
870 if (!CPU_EMPTY(&cpuset_domain[i]))
872 CPU_COPY(&all_cpus, &cpuset_domain[0]);
879 * Create a cpuset, which would be cpuset_create() but
880 * mark the new 'set' as root.
882 * We are not going to reparent the td to it. Use cpuset_setproc_update_set()
885 * In case of no error, returns the set in *setp locked with a reference.
888 cpuset_create_root(struct prison *pr, struct cpuset **setp)
893 KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
894 KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
896 error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
900 KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
901 __func__, __LINE__));
903 /* Mark the set as root. */
905 set->cs_flags |= CPU_SET_ROOT;
911 cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
915 KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
916 KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
919 error = cpuset_setproc(p->p_pid, set, NULL);
927 * This is called once the final set of system cpus is known. Modifies
928 * the root set and all children and mark the root read-only.
931 cpuset_init(void *arg)
936 if (cpuset_modify(cpuset_zero, &mask))
937 panic("Can't set initial cpuset mask.\n");
938 cpuset_zero->cs_flags |= CPU_SET_RDONLY;
940 SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
942 #ifndef _SYS_SYSPROTO_H_
948 sys_cpuset(struct thread *td, struct cpuset_args *uap)
955 root = cpuset_refroot(td->td_cpuset);
957 error = cpuset_create(&set, root, &root->cs_mask);
961 error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
963 error = cpuset_setproc(-1, set, NULL);
968 #ifndef _SYS_SYSPROTO_H_
969 struct cpuset_setid_args {
976 sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
979 return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
983 kern_cpuset_setid(struct thread *td, cpuwhich_t which,
984 id_t id, cpusetid_t setid)
990 * Presently we only support per-process sets.
992 if (which != CPU_WHICH_PID)
994 set = cpuset_lookup(setid, td);
997 error = cpuset_setproc(id, set, NULL);
1002 #ifndef _SYS_SYSPROTO_H_
1003 struct cpuset_getid_args {
1011 sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1014 return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1019 kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1020 id_t id, cpusetid_t *setid)
1022 struct cpuset *nset;
1029 if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1031 error = cpuset_which(which, id, &p, &ttd, &set);
1038 set = cpuset_refbase(ttd->td_cpuset);
1042 case CPU_WHICH_CPUSET:
1043 case CPU_WHICH_JAIL:
1046 case CPU_WHICH_DOMAIN:
1050 case CPU_LEVEL_ROOT:
1051 nset = cpuset_refroot(set);
1055 case CPU_LEVEL_CPUSET:
1057 case CPU_LEVEL_WHICH:
1063 error = copyout(&tmpid, setid, sizeof(id));
1068 #ifndef _SYS_SYSPROTO_H_
1069 struct cpuset_getaffinity_args {
1078 sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1081 return (kern_cpuset_getaffinity(td, uap->level, uap->which,
1082 uap->id, uap->cpusetsize, uap->mask));
1086 kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1087 id_t id, size_t cpusetsize, cpuset_t *maskp)
1090 struct cpuset *nset;
1097 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1100 mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1101 error = cpuset_which(which, id, &p, &ttd, &set);
1105 case CPU_LEVEL_ROOT:
1106 case CPU_LEVEL_CPUSET:
1111 set = cpuset_ref(ttd->td_cpuset);
1114 case CPU_WHICH_CPUSET:
1115 case CPU_WHICH_JAIL:
1118 case CPU_WHICH_INTRHANDLER:
1119 case CPU_WHICH_ITHREAD:
1120 case CPU_WHICH_DOMAIN:
1124 if (level == CPU_LEVEL_ROOT)
1125 nset = cpuset_refroot(set);
1127 nset = cpuset_refbase(set);
1128 CPU_COPY(&nset->cs_mask, mask);
1131 case CPU_LEVEL_WHICH:
1135 CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1139 FOREACH_THREAD_IN_PROC(p, ttd) {
1141 CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1145 case CPU_WHICH_CPUSET:
1146 case CPU_WHICH_JAIL:
1147 CPU_COPY(&set->cs_mask, mask);
1150 case CPU_WHICH_INTRHANDLER:
1151 case CPU_WHICH_ITHREAD:
1152 error = intr_getaffinity(id, which, mask);
1154 case CPU_WHICH_DOMAIN:
1155 if (id < 0 || id >= MAXMEMDOM)
1158 CPU_COPY(&cpuset_domain[id], mask);
1171 error = copyout(mask, maskp, size);
1177 #ifndef _SYS_SYSPROTO_H_
1178 struct cpuset_setaffinity_args {
1183 const cpuset_t *mask;
1187 sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1190 return (kern_cpuset_setaffinity(td, uap->level, uap->which,
1191 uap->id, uap->cpusetsize, uap->mask));
1195 kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1196 id_t id, size_t cpusetsize, const cpuset_t *maskp)
1198 struct cpuset *nset;
1205 if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY)
1207 mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1208 error = copyin(maskp, mask, cpusetsize);
1212 * Verify that no high bits are set.
1214 if (cpusetsize > sizeof(cpuset_t)) {
1218 end = cp = (char *)&mask->__bits;
1220 cp += sizeof(cpuset_t);
1229 case CPU_LEVEL_ROOT:
1230 case CPU_LEVEL_CPUSET:
1231 error = cpuset_which(which, id, &p, &ttd, &set);
1238 set = cpuset_ref(ttd->td_cpuset);
1242 case CPU_WHICH_CPUSET:
1243 case CPU_WHICH_JAIL:
1246 case CPU_WHICH_INTRHANDLER:
1247 case CPU_WHICH_ITHREAD:
1248 case CPU_WHICH_DOMAIN:
1252 if (level == CPU_LEVEL_ROOT)
1253 nset = cpuset_refroot(set);
1255 nset = cpuset_refbase(set);
1256 error = cpuset_modify(nset, mask);
1260 case CPU_LEVEL_WHICH:
1263 error = cpuset_setthread(id, mask);
1266 error = cpuset_setproc(id, NULL, mask);
1268 case CPU_WHICH_CPUSET:
1269 case CPU_WHICH_JAIL:
1270 error = cpuset_which(which, id, &p, &ttd, &set);
1272 error = cpuset_modify(set, mask);
1277 case CPU_WHICH_INTRHANDLER:
1278 case CPU_WHICH_ITHREAD:
1279 error = intr_setaffinity(id, which, mask);
1297 ddb_display_cpuset(const cpuset_t *set)
1301 for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1302 if (CPU_ISSET(cpu, set)) {
1304 db_printf("%d", cpu);
1307 db_printf(",%d", cpu);
1311 db_printf("<none>");
1314 DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1318 LIST_FOREACH(set, &cpuset_ids, cs_link) {
1319 db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1320 set, set->cs_id, set->cs_ref, set->cs_flags,
1321 (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1322 db_printf(" mask=");
1323 ddb_display_cpuset(&set->cs_mask);