xref: /freebsd/sys/kern/kern_cpuset.c (revision 96c8b3e5)
1d7f687fcSJeff Roberson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4d7f687fcSJeff Roberson  * Copyright (c) 2008,  Jeffrey Roberson <jeff@freebsd.org>
5d7f687fcSJeff Roberson  * All rights reserved.
6d7f687fcSJeff Roberson  *
73bc8c68dSJeff Roberson  * Copyright (c) 2008 Nokia Corporation
83bc8c68dSJeff Roberson  * All rights reserved.
93bc8c68dSJeff Roberson  *
10d7f687fcSJeff Roberson  * Redistribution and use in source and binary forms, with or without
11d7f687fcSJeff Roberson  * modification, are permitted provided that the following conditions
12d7f687fcSJeff Roberson  * are met:
13d7f687fcSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
14d7f687fcSJeff Roberson  *    notice unmodified, this list of conditions, and the following
15d7f687fcSJeff Roberson  *    disclaimer.
16d7f687fcSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
17d7f687fcSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
18d7f687fcSJeff Roberson  *    documentation and/or other materials provided with the distribution.
19d7f687fcSJeff Roberson  *
20d7f687fcSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21d7f687fcSJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22d7f687fcSJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23d7f687fcSJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24d7f687fcSJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25d7f687fcSJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26d7f687fcSJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27d7f687fcSJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28d7f687fcSJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29d7f687fcSJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30d7f687fcSJeff Roberson  *
31d7f687fcSJeff Roberson  */
32d7f687fcSJeff Roberson 
33d7f687fcSJeff Roberson #include <sys/cdefs.h>
34dea0ed66SBjoern A. Zeeb #include "opt_ddb.h"
35586ed321SDmitry Chagin #include "opt_ktrace.h"
36dea0ed66SBjoern A. Zeeb 
37d7f687fcSJeff Roberson #include <sys/param.h>
38d7f687fcSJeff Roberson #include <sys/systm.h>
39e5818a53SJeff Roberson #include <sys/sysctl.h>
40e5818a53SJeff Roberson #include <sys/ctype.h>
41d7f687fcSJeff Roberson #include <sys/sysproto.h>
420304c731SJamie Gritton #include <sys/jail.h>
43d7f687fcSJeff Roberson #include <sys/kernel.h>
44d7f687fcSJeff Roberson #include <sys/lock.h>
45d7f687fcSJeff Roberson #include <sys/malloc.h>
46d7f687fcSJeff Roberson #include <sys/mutex.h>
47d7f687fcSJeff Roberson #include <sys/priv.h>
48d7f687fcSJeff Roberson #include <sys/proc.h>
49d7f687fcSJeff Roberson #include <sys/refcount.h>
50d7f687fcSJeff Roberson #include <sys/sched.h>
51d7f687fcSJeff Roberson #include <sys/smp.h>
52d7f687fcSJeff Roberson #include <sys/syscallsubr.h>
5347a57144SJustin Hibbits #include <sys/sysent.h>
54f299c47bSAllan Jude #include <sys/capsicum.h>
55d7f687fcSJeff Roberson #include <sys/cpuset.h>
563f289c3fSJeff Roberson #include <sys/domainset.h>
57d7f687fcSJeff Roberson #include <sys/sx.h>
58d7f687fcSJeff Roberson #include <sys/queue.h>
59e3709597SAttilio Rao #include <sys/libkern.h>
60d7f687fcSJeff Roberson #include <sys/limits.h>
61a03ee000SJeff Roberson #include <sys/bus.h>
62a03ee000SJeff Roberson #include <sys/interrupt.h>
633f289c3fSJeff Roberson #include <sys/vmmeter.h>
64586ed321SDmitry Chagin #include <sys/ktrace.h>
65d7f687fcSJeff Roberson 
66d7f687fcSJeff Roberson #include <vm/uma.h>
67c0ae6688SJohn Baldwin #include <vm/vm.h>
683f289c3fSJeff Roberson #include <vm/vm_object.h>
6930c5525bSAndrew Gallatin #include <vm/vm_page.h>
7030c5525bSAndrew Gallatin #include <vm/vm_pageout.h>
71e5818a53SJeff Roberson #include <vm/vm_extern.h>
7230c5525bSAndrew Gallatin #include <vm/vm_param.h>
7330c5525bSAndrew Gallatin #include <vm/vm_phys.h>
7430c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h>
75d7f687fcSJeff Roberson 
76dea0ed66SBjoern A. Zeeb #ifdef DDB
77dea0ed66SBjoern A. Zeeb #include <ddb/ddb.h>
78dea0ed66SBjoern A. Zeeb #endif /* DDB */
79dea0ed66SBjoern A. Zeeb 
80d7f687fcSJeff Roberson /*
81d7f687fcSJeff Roberson  * cpusets provide a mechanism for creating and manipulating sets of
82d7f687fcSJeff Roberson  * processors for the purpose of constraining the scheduling of threads to
83d7f687fcSJeff Roberson  * specific processors.
84d7f687fcSJeff Roberson  *
85d7f687fcSJeff Roberson  * Each process belongs to an identified set, by default this is set 1.  Each
86d7f687fcSJeff Roberson  * thread may further restrict the cpus it may run on to a subset of this
87d7f687fcSJeff Roberson  * named set.  This creates an anonymous set which other threads and processes
88d7f687fcSJeff Roberson  * may not join by number.
89d7f687fcSJeff Roberson  *
90d7f687fcSJeff Roberson  * The named set is referred to herein as the 'base' set to avoid ambiguity.
91d7f687fcSJeff Roberson  * This set is usually a child of a 'root' set while the anonymous set may
92d7f687fcSJeff Roberson  * simply be referred to as a mask.  In the syscall api these are referred to
93d7f687fcSJeff Roberson  * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
94d7f687fcSJeff Roberson  *
95d7f687fcSJeff Roberson  * Threads inherit their set from their creator whether it be anonymous or
96d7f687fcSJeff Roberson  * not.  This means that anonymous sets are immutable because they may be
97d7f687fcSJeff Roberson  * shared.  To modify an anonymous set a new set is created with the desired
98d7f687fcSJeff Roberson  * mask and the same parent as the existing anonymous set.  This gives the
9993902625SJohn Baldwin  * illusion of each thread having a private mask.
100d7f687fcSJeff Roberson  *
101d7f687fcSJeff Roberson  * Via the syscall apis a user may ask to retrieve or modify the root, base,
102d7f687fcSJeff Roberson  * or mask that is discovered via a pid, tid, or setid.  Modifying a set
103d7f687fcSJeff Roberson  * modifies all numbered and anonymous child sets to comply with the new mask.
104d7f687fcSJeff Roberson  * Modifying a pid or tid's mask applies only to that tid but must still
105d7f687fcSJeff Roberson  * exist within the assigned parent set.
106d7f687fcSJeff Roberson  *
10786855bf5SJohn Baldwin  * A thread may not be assigned to a group separate from other threads in
108d7f687fcSJeff Roberson  * the process.  This is to remove ambiguity when the setid is queried with
109d7f687fcSJeff Roberson  * a pid argument.  There is no other technical limitation.
110d7f687fcSJeff Roberson  *
111d7f687fcSJeff Roberson  * This somewhat complex arrangement is intended to make it easy for
112d7f687fcSJeff Roberson  * applications to query available processors and bind their threads to
113d7f687fcSJeff Roberson  * specific processors while also allowing administrators to dynamically
114d7f687fcSJeff Roberson  * reprovision by changing sets which apply to groups of processes.
115d7f687fcSJeff Roberson  *
116d7f687fcSJeff Roberson  * A simple application should not concern itself with sets at all and
117d7f687fcSJeff Roberson  * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
11893902625SJohn Baldwin  * meaning 'curthread'.  It may query available cpus for that tid with a
119d7f687fcSJeff Roberson  * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
120d7f687fcSJeff Roberson  */
121e5818a53SJeff Roberson 
122e5818a53SJeff Roberson LIST_HEAD(domainlist, domainset);
12329bb6c19SMark Johnston struct domainset __read_mostly domainset_firsttouch;
1249978bd99SMark Johnston struct domainset __read_mostly domainset_fixed[MAXMEMDOM];
12529bb6c19SMark Johnston struct domainset __read_mostly domainset_interleave;
126662e7fa8SMark Johnston struct domainset __read_mostly domainset_prefer[MAXMEMDOM];
127662e7fa8SMark Johnston struct domainset __read_mostly domainset_roundrobin;
128e5818a53SJeff Roberson 
129d7f687fcSJeff Roberson static uma_zone_t cpuset_zone;
1303f289c3fSJeff Roberson static uma_zone_t domainset_zone;
131d7f687fcSJeff Roberson static struct mtx cpuset_lock;
132d7f687fcSJeff Roberson static struct setlist cpuset_ids;
1333f289c3fSJeff Roberson static struct domainlist cpuset_domains;
134d7f687fcSJeff Roberson static struct unrhdr *cpuset_unr;
135e5818a53SJeff Roberson static struct cpuset *cpuset_zero, *cpuset_default, *cpuset_kernel;
13629bb6c19SMark Johnston static struct domainset *domainset0, *domainset2;
13701f74ccdSDmitry Chagin u_int cpusetsizemin = 1;
138a03ee000SJeff Roberson 
139444528c0SDavid Xu /* Return the size of cpuset_t at the kernel level */
14060aa2c85SJonathan Anderson SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
141f0188618SHans Petter Selasky     SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
142444528c0SDavid Xu 
14301f74ccdSDmitry Chagin /* Return the minimum size of cpuset_t allowed by the kernel */
14401f74ccdSDmitry Chagin SYSCTL_UINT(_kern_sched, OID_AUTO, cpusetsizemin,
14501f74ccdSDmitry Chagin     CTLFLAG_RD | CTLFLAG_CAPRD, &cpusetsizemin, 0,
14601f74ccdSDmitry Chagin     "The minimum size of cpuset_t allowed by the kernel");
14701f74ccdSDmitry Chagin 
148a03ee000SJeff Roberson cpuset_t *cpuset_root;
149c0ae6688SJohn Baldwin cpuset_t cpuset_domain[MAXMEMDOM];
150d7f687fcSJeff Roberson 
1512058f075SDmitry Chagin static int cpuset_which2(cpuwhich_t *, id_t, struct proc **, struct thread **,
1522058f075SDmitry Chagin     struct cpuset **);
1533f289c3fSJeff Roberson static int domainset_valid(const struct domainset *, const struct domainset *);
1543f289c3fSJeff Roberson 
1553f289c3fSJeff Roberson /*
1563f289c3fSJeff Roberson  * Find the first non-anonymous set starting from 'set'.
1573f289c3fSJeff Roberson  */
1583f289c3fSJeff Roberson static struct cpuset *
cpuset_getbase(struct cpuset * set)1593f289c3fSJeff Roberson cpuset_getbase(struct cpuset *set)
1603f289c3fSJeff Roberson {
1613f289c3fSJeff Roberson 
1623f289c3fSJeff Roberson 	if (set->cs_id == CPUSET_INVALID)
1633f289c3fSJeff Roberson 		set = set->cs_parent;
1643f289c3fSJeff Roberson 	return (set);
1653f289c3fSJeff Roberson }
1663f289c3fSJeff Roberson 
1673f289c3fSJeff Roberson /*
1683f289c3fSJeff Roberson  * Walks up the tree from 'set' to find the root.
1693f289c3fSJeff Roberson  */
1703f289c3fSJeff Roberson static struct cpuset *
cpuset_getroot(struct cpuset * set)1713f289c3fSJeff Roberson cpuset_getroot(struct cpuset *set)
1723f289c3fSJeff Roberson {
1733f289c3fSJeff Roberson 
1743f289c3fSJeff Roberson 	while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL)
1753f289c3fSJeff Roberson 		set = set->cs_parent;
1763f289c3fSJeff Roberson 	return (set);
1773f289c3fSJeff Roberson }
1783f289c3fSJeff Roberson 
179d7f687fcSJeff Roberson /*
180d7f687fcSJeff Roberson  * Acquire a reference to a cpuset, all pointers must be tracked with refs.
181d7f687fcSJeff Roberson  */
182d7f687fcSJeff Roberson struct cpuset *
cpuset_ref(struct cpuset * set)183d7f687fcSJeff Roberson cpuset_ref(struct cpuset *set)
184d7f687fcSJeff Roberson {
185d7f687fcSJeff Roberson 
186d7f687fcSJeff Roberson 	refcount_acquire(&set->cs_ref);
187d7f687fcSJeff Roberson 	return (set);
188d7f687fcSJeff Roberson }
189d7f687fcSJeff Roberson 
190d7f687fcSJeff Roberson /*
1917a8f695aSBjoern A. Zeeb  * Walks up the tree from 'set' to find the root.  Returns the root
1927a8f695aSBjoern A. Zeeb  * referenced.
1937a8f695aSBjoern A. Zeeb  */
1947a8f695aSBjoern A. Zeeb static struct cpuset *
cpuset_refroot(struct cpuset * set)1957a8f695aSBjoern A. Zeeb cpuset_refroot(struct cpuset *set)
1967a8f695aSBjoern A. Zeeb {
1977a8f695aSBjoern A. Zeeb 
1983f289c3fSJeff Roberson 	return (cpuset_ref(cpuset_getroot(set)));
1997a8f695aSBjoern A. Zeeb }
2007a8f695aSBjoern A. Zeeb 
2017a8f695aSBjoern A. Zeeb /*
2027a8f695aSBjoern A. Zeeb  * Find the first non-anonymous set starting from 'set'.  Returns this set
2037a8f695aSBjoern A. Zeeb  * referenced.  May return the passed in set with an extra ref if it is
2047a8f695aSBjoern A. Zeeb  * not anonymous.
2057a8f695aSBjoern A. Zeeb  */
2067a8f695aSBjoern A. Zeeb static struct cpuset *
cpuset_refbase(struct cpuset * set)2077a8f695aSBjoern A. Zeeb cpuset_refbase(struct cpuset *set)
2087a8f695aSBjoern A. Zeeb {
2097a8f695aSBjoern A. Zeeb 
2103f289c3fSJeff Roberson 	return (cpuset_ref(cpuset_getbase(set)));
2117a8f695aSBjoern A. Zeeb }
2127a8f695aSBjoern A. Zeeb 
2137a8f695aSBjoern A. Zeeb /*
21493902625SJohn Baldwin  * Release a reference in a context where it is safe to allocate.
215d7f687fcSJeff Roberson  */
216d7f687fcSJeff Roberson void
cpuset_rel(struct cpuset * set)217d7f687fcSJeff Roberson cpuset_rel(struct cpuset *set)
218d7f687fcSJeff Roberson {
219d7f687fcSJeff Roberson 	cpusetid_t id;
220d7f687fcSJeff Roberson 
221b2780e85SKyle Evans 	if (refcount_release_if_not_last(&set->cs_ref))
222d7f687fcSJeff Roberson 		return;
223d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
224b2780e85SKyle Evans 	if (!refcount_release(&set->cs_ref)) {
225b2780e85SKyle Evans 		mtx_unlock_spin(&cpuset_lock);
226b2780e85SKyle Evans 		return;
227b2780e85SKyle Evans 	}
228d7f687fcSJeff Roberson 	LIST_REMOVE(set, cs_siblings);
229d7f687fcSJeff Roberson 	id = set->cs_id;
230d7f687fcSJeff Roberson 	if (id != CPUSET_INVALID)
231d7f687fcSJeff Roberson 		LIST_REMOVE(set, cs_link);
232d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
233d7f687fcSJeff Roberson 	cpuset_rel(set->cs_parent);
234d7f687fcSJeff Roberson 	uma_zfree(cpuset_zone, set);
235d7f687fcSJeff Roberson 	if (id != CPUSET_INVALID)
236d7f687fcSJeff Roberson 		free_unr(cpuset_unr, id);
237d7f687fcSJeff Roberson }
238d7f687fcSJeff Roberson 
239d7f687fcSJeff Roberson /*
240d7f687fcSJeff Roberson  * Deferred release must be used when in a context that is not safe to
241d7f687fcSJeff Roberson  * allocate/free.  This places any unreferenced sets on the list 'head'.
242d7f687fcSJeff Roberson  */
243d7f687fcSJeff Roberson static void
cpuset_rel_defer(struct setlist * head,struct cpuset * set)244d7f687fcSJeff Roberson cpuset_rel_defer(struct setlist *head, struct cpuset *set)
245d7f687fcSJeff Roberson {
246d7f687fcSJeff Roberson 
247b2780e85SKyle Evans 	if (refcount_release_if_not_last(&set->cs_ref))
248d7f687fcSJeff Roberson 		return;
249d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
250b2780e85SKyle Evans 	if (!refcount_release(&set->cs_ref)) {
251b2780e85SKyle Evans 		mtx_unlock_spin(&cpuset_lock);
252b2780e85SKyle Evans 		return;
253b2780e85SKyle Evans 	}
254d7f687fcSJeff Roberson 	LIST_REMOVE(set, cs_siblings);
255d7f687fcSJeff Roberson 	if (set->cs_id != CPUSET_INVALID)
256d7f687fcSJeff Roberson 		LIST_REMOVE(set, cs_link);
257d7f687fcSJeff Roberson 	LIST_INSERT_HEAD(head, set, cs_link);
258d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
259d7f687fcSJeff Roberson }
260d7f687fcSJeff Roberson 
261d7f687fcSJeff Roberson /*
262d7f687fcSJeff Roberson  * Complete a deferred release.  Removes the set from the list provided to
263d7f687fcSJeff Roberson  * cpuset_rel_defer.
264d7f687fcSJeff Roberson  */
265d7f687fcSJeff Roberson static void
cpuset_rel_complete(struct cpuset * set)266d7f687fcSJeff Roberson cpuset_rel_complete(struct cpuset *set)
267d7f687fcSJeff Roberson {
2689c83dab9SKyle Evans 	cpusetid_t id;
2699c83dab9SKyle Evans 
2709c83dab9SKyle Evans 	id = set->cs_id;
271d7f687fcSJeff Roberson 	LIST_REMOVE(set, cs_link);
272d7f687fcSJeff Roberson 	cpuset_rel(set->cs_parent);
273d7f687fcSJeff Roberson 	uma_zfree(cpuset_zone, set);
2749c83dab9SKyle Evans 	if (id != CPUSET_INVALID)
2759c83dab9SKyle Evans 		free_unr(cpuset_unr, id);
276d7f687fcSJeff Roberson }
277d7f687fcSJeff Roberson 
278d7f687fcSJeff Roberson /*
279d7f687fcSJeff Roberson  * Find a set based on an id.  Returns it with a ref.
280d7f687fcSJeff Roberson  */
281d7f687fcSJeff Roberson static struct cpuset *
cpuset_lookup(cpusetid_t setid,struct thread * td)282413628a7SBjoern A. Zeeb cpuset_lookup(cpusetid_t setid, struct thread *td)
283d7f687fcSJeff Roberson {
284d7f687fcSJeff Roberson 	struct cpuset *set;
285d7f687fcSJeff Roberson 
286d7f687fcSJeff Roberson 	if (setid == CPUSET_INVALID)
287d7f687fcSJeff Roberson 		return (NULL);
288d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
289d7f687fcSJeff Roberson 	LIST_FOREACH(set, &cpuset_ids, cs_link)
290d7f687fcSJeff Roberson 		if (set->cs_id == setid)
291d7f687fcSJeff Roberson 			break;
292d7f687fcSJeff Roberson 	if (set)
293d7f687fcSJeff Roberson 		cpuset_ref(set);
294d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
295413628a7SBjoern A. Zeeb 
296413628a7SBjoern A. Zeeb 	KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
297413628a7SBjoern A. Zeeb 	if (set != NULL && jailed(td->td_ucred)) {
2980304c731SJamie Gritton 		struct cpuset *jset, *tset;
299413628a7SBjoern A. Zeeb 
3000304c731SJamie Gritton 		jset = td->td_ucred->cr_prison->pr_cpuset;
3010304c731SJamie Gritton 		for (tset = set; tset != NULL; tset = tset->cs_parent)
3020304c731SJamie Gritton 			if (tset == jset)
3030304c731SJamie Gritton 				break;
3040304c731SJamie Gritton 		if (tset == NULL) {
305413628a7SBjoern A. Zeeb 			cpuset_rel(set);
306413628a7SBjoern A. Zeeb 			set = NULL;
307413628a7SBjoern A. Zeeb 		}
308413628a7SBjoern A. Zeeb 	}
309413628a7SBjoern A. Zeeb 
310d7f687fcSJeff Roberson 	return (set);
311d7f687fcSJeff Roberson }
312d7f687fcSJeff Roberson 
313d7f687fcSJeff Roberson /*
31430b7c6f9SKyle Evans  * Initialize a set in the space provided in 'set' with the provided parameters.
315d7f687fcSJeff Roberson  * The set is returned with a single ref.  May return EDEADLK if the set
316d7f687fcSJeff Roberson  * will have no valid cpu based on restrictions from the parent.
317d7f687fcSJeff Roberson  */
318d7f687fcSJeff Roberson static int
cpuset_init(struct cpuset * set,struct cpuset * parent,const cpuset_t * mask,struct domainset * domain,cpusetid_t id)31930b7c6f9SKyle Evans cpuset_init(struct cpuset *set, struct cpuset *parent,
3203f289c3fSJeff Roberson     const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
321d7f687fcSJeff Roberson {
322d7f687fcSJeff Roberson 
3233f289c3fSJeff Roberson 	if (domain == NULL)
3243f289c3fSJeff Roberson 		domain = parent->cs_domain;
3253f289c3fSJeff Roberson 	if (mask == NULL)
3263f289c3fSJeff Roberson 		mask = &parent->cs_mask;
32773c40187SJeff Roberson 	if (!CPU_OVERLAP(&parent->cs_mask, mask))
32873c40187SJeff Roberson 		return (EDEADLK);
3293f289c3fSJeff Roberson 	/* The domain must be prepared ahead of time. */
3303f289c3fSJeff Roberson 	if (!domainset_valid(parent->cs_domain, domain))
3313f289c3fSJeff Roberson 		return (EDEADLK);
332d7f687fcSJeff Roberson 	CPU_COPY(mask, &set->cs_mask);
333d7f687fcSJeff Roberson 	LIST_INIT(&set->cs_children);
334d7f687fcSJeff Roberson 	refcount_init(&set->cs_ref, 1);
335d7f687fcSJeff Roberson 	set->cs_flags = 0;
336d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
3373f289c3fSJeff Roberson 	set->cs_domain = domain;
338e2650af1SStefan Eßer 	CPU_AND(&set->cs_mask, &set->cs_mask, &parent->cs_mask);
339d7f687fcSJeff Roberson 	set->cs_id = id;
340d7f687fcSJeff Roberson 	set->cs_parent = cpuset_ref(parent);
341d7f687fcSJeff Roberson 	LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
342d7f687fcSJeff Roberson 	if (set->cs_id != CPUSET_INVALID)
343d7f687fcSJeff Roberson 		LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
344d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
345d7f687fcSJeff Roberson 
34673c40187SJeff Roberson 	return (0);
347d7f687fcSJeff Roberson }
348d7f687fcSJeff Roberson 
349d7f687fcSJeff Roberson /*
350d7f687fcSJeff Roberson  * Create a new non-anonymous set with the requested parent and mask.  May
351d7f687fcSJeff Roberson  * return failures if the mask is invalid or a new number can not be
352d7f687fcSJeff Roberson  * allocated.
35329d04ea8SKyle Evans  *
35429d04ea8SKyle Evans  * If *setp is not NULL, then it will be used as-is.  The caller must take
35529d04ea8SKyle Evans  * into account that *setp will be inserted at the head of cpuset_ids and
35629d04ea8SKyle Evans  * plan any potentially conflicting cs_link usage accordingly.
357d7f687fcSJeff Roberson  */
358d7f687fcSJeff Roberson static int
cpuset_create(struct cpuset ** setp,struct cpuset * parent,const cpuset_t * mask)359e84c2db1SJohn Baldwin cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
360d7f687fcSJeff Roberson {
361d7f687fcSJeff Roberson 	struct cpuset *set;
362d7f687fcSJeff Roberson 	cpusetid_t id;
363d7f687fcSJeff Roberson 	int error;
36429d04ea8SKyle Evans 	bool dofree;
365d7f687fcSJeff Roberson 
366d7f687fcSJeff Roberson 	id = alloc_unr(cpuset_unr);
367d7f687fcSJeff Roberson 	if (id == -1)
368d7f687fcSJeff Roberson 		return (ENFILE);
36929d04ea8SKyle Evans 	dofree = (*setp == NULL);
37029d04ea8SKyle Evans 	if (*setp != NULL)
37129d04ea8SKyle Evans 		set = *setp;
37229d04ea8SKyle Evans 	else
3733f289c3fSJeff Roberson 		*setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
37430b7c6f9SKyle Evans 	error = cpuset_init(set, parent, mask, NULL, id);
375d7f687fcSJeff Roberson 	if (error == 0)
376d7f687fcSJeff Roberson 		return (0);
377d7f687fcSJeff Roberson 	free_unr(cpuset_unr, id);
37829d04ea8SKyle Evans 	if (dofree)
379d7f687fcSJeff Roberson 		uma_zfree(cpuset_zone, set);
380d7f687fcSJeff Roberson 
381d7f687fcSJeff Roberson 	return (error);
382d7f687fcSJeff Roberson }
383d7f687fcSJeff Roberson 
3843f289c3fSJeff Roberson static void
cpuset_freelist_add(struct setlist * list,int count)3853f289c3fSJeff Roberson cpuset_freelist_add(struct setlist *list, int count)
3863f289c3fSJeff Roberson {
3873f289c3fSJeff Roberson 	struct cpuset *set;
3883f289c3fSJeff Roberson 	int i;
3893f289c3fSJeff Roberson 
3903f289c3fSJeff Roberson 	for (i = 0; i < count; i++) {
3913f289c3fSJeff Roberson 		set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK);
3923f289c3fSJeff Roberson 		LIST_INSERT_HEAD(list, set, cs_link);
3933f289c3fSJeff Roberson 	}
3943f289c3fSJeff Roberson }
3953f289c3fSJeff Roberson 
3963f289c3fSJeff Roberson static void
cpuset_freelist_init(struct setlist * list,int count)3973f289c3fSJeff Roberson cpuset_freelist_init(struct setlist *list, int count)
3983f289c3fSJeff Roberson {
3993f289c3fSJeff Roberson 
4003f289c3fSJeff Roberson 	LIST_INIT(list);
4013f289c3fSJeff Roberson 	cpuset_freelist_add(list, count);
4023f289c3fSJeff Roberson }
4033f289c3fSJeff Roberson 
4043f289c3fSJeff Roberson static void
cpuset_freelist_free(struct setlist * list)4053f289c3fSJeff Roberson cpuset_freelist_free(struct setlist *list)
4063f289c3fSJeff Roberson {
4073f289c3fSJeff Roberson 	struct cpuset *set;
4083f289c3fSJeff Roberson 
4093f289c3fSJeff Roberson 	while ((set = LIST_FIRST(list)) != NULL) {
4103f289c3fSJeff Roberson 		LIST_REMOVE(set, cs_link);
4113f289c3fSJeff Roberson 		uma_zfree(cpuset_zone, set);
4123f289c3fSJeff Roberson 	}
4133f289c3fSJeff Roberson }
4143f289c3fSJeff Roberson 
4153f289c3fSJeff Roberson static void
domainset_freelist_add(struct domainlist * list,int count)4163f289c3fSJeff Roberson domainset_freelist_add(struct domainlist *list, int count)
4173f289c3fSJeff Roberson {
4183f289c3fSJeff Roberson 	struct domainset *set;
4193f289c3fSJeff Roberson 	int i;
4203f289c3fSJeff Roberson 
4213f289c3fSJeff Roberson 	for (i = 0; i < count; i++) {
4223f289c3fSJeff Roberson 		set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK);
4233f289c3fSJeff Roberson 		LIST_INSERT_HEAD(list, set, ds_link);
4243f289c3fSJeff Roberson 	}
4253f289c3fSJeff Roberson }
4263f289c3fSJeff Roberson 
4273f289c3fSJeff Roberson static void
domainset_freelist_init(struct domainlist * list,int count)4283f289c3fSJeff Roberson domainset_freelist_init(struct domainlist *list, int count)
4293f289c3fSJeff Roberson {
4303f289c3fSJeff Roberson 
4313f289c3fSJeff Roberson 	LIST_INIT(list);
4323f289c3fSJeff Roberson 	domainset_freelist_add(list, count);
4333f289c3fSJeff Roberson }
4343f289c3fSJeff Roberson 
4353f289c3fSJeff Roberson static void
domainset_freelist_free(struct domainlist * list)4363f289c3fSJeff Roberson domainset_freelist_free(struct domainlist *list)
4373f289c3fSJeff Roberson {
4383f289c3fSJeff Roberson 	struct domainset *set;
4393f289c3fSJeff Roberson 
4403f289c3fSJeff Roberson 	while ((set = LIST_FIRST(list)) != NULL) {
4413f289c3fSJeff Roberson 		LIST_REMOVE(set, ds_link);
4423f289c3fSJeff Roberson 		uma_zfree(domainset_zone, set);
4433f289c3fSJeff Roberson 	}
4443f289c3fSJeff Roberson }
4453f289c3fSJeff Roberson 
4463f289c3fSJeff Roberson /* Copy a domainset preserving mask and policy. */
4473f289c3fSJeff Roberson static void
domainset_copy(const struct domainset * from,struct domainset * to)4483f289c3fSJeff Roberson domainset_copy(const struct domainset *from, struct domainset *to)
4493f289c3fSJeff Roberson {
4503f289c3fSJeff Roberson 
4513f289c3fSJeff Roberson 	DOMAINSET_COPY(&from->ds_mask, &to->ds_mask);
4523f289c3fSJeff Roberson 	to->ds_policy = from->ds_policy;
4533f289c3fSJeff Roberson 	to->ds_prefer = from->ds_prefer;
4543f289c3fSJeff Roberson }
4553f289c3fSJeff Roberson 
4563f289c3fSJeff Roberson /* Return 1 if mask and policy are equal, otherwise 0. */
4573f289c3fSJeff Roberson static int
domainset_equal(const struct domainset * one,const struct domainset * two)4583f289c3fSJeff Roberson domainset_equal(const struct domainset *one, const struct domainset *two)
4593f289c3fSJeff Roberson {
4603f289c3fSJeff Roberson 
4613f289c3fSJeff Roberson 	return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 &&
4623f289c3fSJeff Roberson 	    one->ds_policy == two->ds_policy &&
4633f289c3fSJeff Roberson 	    one->ds_prefer == two->ds_prefer);
4643f289c3fSJeff Roberson }
4653f289c3fSJeff Roberson 
4663f289c3fSJeff Roberson /* Return 1 if child is a valid subset of parent. */
4673f289c3fSJeff Roberson static int
domainset_valid(const struct domainset * parent,const struct domainset * child)4683f289c3fSJeff Roberson domainset_valid(const struct domainset *parent, const struct domainset *child)
4693f289c3fSJeff Roberson {
4703f289c3fSJeff Roberson 	if (child->ds_policy != DOMAINSET_POLICY_PREFER)
4713f289c3fSJeff Roberson 		return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask));
4723f289c3fSJeff Roberson 	return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
4733f289c3fSJeff Roberson }
4743f289c3fSJeff Roberson 
4753f289c3fSJeff Roberson static int
domainset_restrict(const struct domainset * parent,const struct domainset * child)4763f289c3fSJeff Roberson domainset_restrict(const struct domainset *parent,
4773f289c3fSJeff Roberson     const struct domainset *child)
4783f289c3fSJeff Roberson {
4793f289c3fSJeff Roberson 	if (child->ds_policy != DOMAINSET_POLICY_PREFER)
4803f289c3fSJeff Roberson 		return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask));
4813f289c3fSJeff Roberson 	return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask));
4823f289c3fSJeff Roberson }
4833f289c3fSJeff Roberson 
4843f289c3fSJeff Roberson /*
4853f289c3fSJeff Roberson  * Lookup or create a domainset.  The key is provided in ds_mask and
4863f289c3fSJeff Roberson  * ds_policy.  If the domainset does not yet exist the storage in
4873f289c3fSJeff Roberson  * 'domain' is used to insert.  Otherwise this storage is freed to the
4883f289c3fSJeff Roberson  * domainset_zone and the existing domainset is returned.
4893f289c3fSJeff Roberson  */
4903f289c3fSJeff Roberson static struct domainset *
_domainset_create(struct domainset * domain,struct domainlist * freelist)4913f289c3fSJeff Roberson _domainset_create(struct domainset *domain, struct domainlist *freelist)
4923f289c3fSJeff Roberson {
4933f289c3fSJeff Roberson 	struct domainset *ndomain;
49444e4def7SMark Johnston 	int i, j;
4953f289c3fSJeff Roberson 
496b61f3142SMark Johnston 	KASSERT(domain->ds_cnt <= vm_ndomains,
497b61f3142SMark Johnston 	    ("invalid domain count in domainset %p", domain));
498b61f3142SMark Johnston 	KASSERT(domain->ds_policy != DOMAINSET_POLICY_PREFER ||
499b61f3142SMark Johnston 	    domain->ds_prefer < vm_ndomains,
500b61f3142SMark Johnston 	    ("invalid preferred domain in domains %p", domain));
501b61f3142SMark Johnston 
5023f289c3fSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
5033f289c3fSJeff Roberson 	LIST_FOREACH(ndomain, &cpuset_domains, ds_link)
5043f289c3fSJeff Roberson 		if (domainset_equal(ndomain, domain))
5053f289c3fSJeff Roberson 			break;
5063f289c3fSJeff Roberson 	/*
5073f289c3fSJeff Roberson 	 * If the domain does not yet exist we insert it and initialize
5083f289c3fSJeff Roberson 	 * various iteration helpers which are not part of the key.
5093f289c3fSJeff Roberson 	 */
5103f289c3fSJeff Roberson 	if (ndomain == NULL) {
5113f289c3fSJeff Roberson 		LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link);
5123f289c3fSJeff Roberson 		domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
51344e4def7SMark Johnston 		for (i = 0, j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
514e5818a53SJeff Roberson 			if (DOMAINSET_ISSET(i, &domain->ds_mask))
515e5818a53SJeff Roberson 				domain->ds_order[j++] = i;
5163f289c3fSJeff Roberson 	}
5173f289c3fSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
5183f289c3fSJeff Roberson 	if (ndomain == NULL)
5193f289c3fSJeff Roberson 		return (domain);
5203f289c3fSJeff Roberson 	if (freelist != NULL)
5213f289c3fSJeff Roberson 		LIST_INSERT_HEAD(freelist, domain, ds_link);
5223f289c3fSJeff Roberson 	else
5233f289c3fSJeff Roberson 		uma_zfree(domainset_zone, domain);
5243f289c3fSJeff Roberson 	return (ndomain);
5253f289c3fSJeff Roberson 
5263f289c3fSJeff Roberson }
5273f289c3fSJeff Roberson 
5283f289c3fSJeff Roberson /*
52930c5525bSAndrew Gallatin  * Are any of the domains in the mask empty?  If so, silently
530920239efSMark Johnston  * remove them and update the domainset accordingly.  If only empty
531920239efSMark Johnston  * domains are present, we must return failure.
53230c5525bSAndrew Gallatin  */
53330c5525bSAndrew Gallatin static bool
domainset_empty_vm(struct domainset * domain)53430c5525bSAndrew Gallatin domainset_empty_vm(struct domainset *domain)
53530c5525bSAndrew Gallatin {
5368e697504SMark Johnston 	domainset_t empty;
5378e697504SMark Johnston 	int i, j;
53830c5525bSAndrew Gallatin 
5398e697504SMark Johnston 	DOMAINSET_ZERO(&empty);
5408e697504SMark Johnston 	for (i = 0; i < vm_ndomains; i++)
5418e697504SMark Johnston 		if (VM_DOMAIN_EMPTY(i))
5428e697504SMark Johnston 			DOMAINSET_SET(i, &empty);
5438e697504SMark Johnston 	if (DOMAINSET_SUBSET(&empty, &domain->ds_mask))
5448e697504SMark Johnston 		return (true);
5458e697504SMark Johnston 
5468e697504SMark Johnston 	/* Remove empty domains from the set and recompute. */
5479825eadfSRyan Libby 	DOMAINSET_ANDNOT(&domain->ds_mask, &empty);
548920239efSMark Johnston 	domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask);
5498e697504SMark Johnston 	for (i = j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++)
550920239efSMark Johnston 		if (DOMAINSET_ISSET(i, &domain->ds_mask))
551920239efSMark Johnston 			domain->ds_order[j++] = i;
5528e697504SMark Johnston 
5538e697504SMark Johnston 	/* Convert a PREFER policy referencing an empty domain to RR. */
5548e697504SMark Johnston 	if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
5558e697504SMark Johnston 	    DOMAINSET_ISSET(domain->ds_prefer, &empty)) {
556920239efSMark Johnston 		domain->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
557920239efSMark Johnston 		domain->ds_prefer = -1;
558920239efSMark Johnston 	}
55930c5525bSAndrew Gallatin 
5608e697504SMark Johnston 	return (false);
56130c5525bSAndrew Gallatin }
56230c5525bSAndrew Gallatin 
56330c5525bSAndrew Gallatin /*
5643f289c3fSJeff Roberson  * Create or lookup a domainset based on the key held in 'domain'.
5653f289c3fSJeff Roberson  */
566e5818a53SJeff Roberson struct domainset *
domainset_create(const struct domainset * domain)5673f289c3fSJeff Roberson domainset_create(const struct domainset *domain)
5683f289c3fSJeff Roberson {
5693f289c3fSJeff Roberson 	struct domainset *ndomain;
5703f289c3fSJeff Roberson 
571e5818a53SJeff Roberson 	/*
572e5818a53SJeff Roberson 	 * Validate the policy.  It must specify a useable policy number with
573e5818a53SJeff Roberson 	 * only valid domains.  Preferred must include the preferred domain
574e5818a53SJeff Roberson 	 * in the mask.
575e5818a53SJeff Roberson 	 */
576e5818a53SJeff Roberson 	if (domain->ds_policy <= DOMAINSET_POLICY_INVALID ||
577e5818a53SJeff Roberson 	    domain->ds_policy > DOMAINSET_POLICY_MAX)
578e5818a53SJeff Roberson 		return (NULL);
579e5818a53SJeff Roberson 	if (domain->ds_policy == DOMAINSET_POLICY_PREFER &&
580e5818a53SJeff Roberson 	    !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask))
581e5818a53SJeff Roberson 		return (NULL);
58229bb6c19SMark Johnston 	if (!DOMAINSET_SUBSET(&domainset0->ds_mask, &domain->ds_mask))
583e5818a53SJeff Roberson 		return (NULL);
5843f289c3fSJeff Roberson 	ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO);
5853f289c3fSJeff Roberson 	domainset_copy(domain, ndomain);
5863f289c3fSJeff Roberson 	return _domainset_create(ndomain, NULL);
5873f289c3fSJeff Roberson }
5883f289c3fSJeff Roberson 
5893f289c3fSJeff Roberson /*
5903f289c3fSJeff Roberson  * Update thread domainset pointers.
5913f289c3fSJeff Roberson  */
5923f289c3fSJeff Roberson static void
domainset_notify(void)5933f289c3fSJeff Roberson domainset_notify(void)
5943f289c3fSJeff Roberson {
5953f289c3fSJeff Roberson 	struct thread *td;
5963f289c3fSJeff Roberson 	struct proc *p;
5973f289c3fSJeff Roberson 
5983f289c3fSJeff Roberson 	sx_slock(&allproc_lock);
5993f289c3fSJeff Roberson 	FOREACH_PROC_IN_SYSTEM(p) {
6003f289c3fSJeff Roberson 		PROC_LOCK(p);
6013f289c3fSJeff Roberson 		if (p->p_state == PRS_NEW) {
6023f289c3fSJeff Roberson 			PROC_UNLOCK(p);
6033f289c3fSJeff Roberson 			continue;
6043f289c3fSJeff Roberson 		}
6053f289c3fSJeff Roberson 		FOREACH_THREAD_IN_PROC(p, td) {
6063f289c3fSJeff Roberson 			thread_lock(td);
6073f289c3fSJeff Roberson 			td->td_domain.dr_policy = td->td_cpuset->cs_domain;
6083f289c3fSJeff Roberson 			thread_unlock(td);
6093f289c3fSJeff Roberson 		}
6103f289c3fSJeff Roberson 		PROC_UNLOCK(p);
6113f289c3fSJeff Roberson 	}
6123f289c3fSJeff Roberson 	sx_sunlock(&allproc_lock);
613e5818a53SJeff Roberson 	kernel_object->domain.dr_policy = cpuset_kernel->cs_domain;
6143f289c3fSJeff Roberson }
6153f289c3fSJeff Roberson 
6163f289c3fSJeff Roberson /*
6173f289c3fSJeff Roberson  * Create a new set that is a subset of a parent.
6183f289c3fSJeff Roberson  */
6193f289c3fSJeff Roberson static struct domainset *
domainset_shadow(const struct domainset * pdomain,const struct domainset * domain,struct domainlist * freelist)6203f289c3fSJeff Roberson domainset_shadow(const struct domainset *pdomain,
6213f289c3fSJeff Roberson     const struct domainset *domain, struct domainlist *freelist)
6223f289c3fSJeff Roberson {
6233f289c3fSJeff Roberson 	struct domainset *ndomain;
6243f289c3fSJeff Roberson 
6253f289c3fSJeff Roberson 	ndomain = LIST_FIRST(freelist);
6263f289c3fSJeff Roberson 	LIST_REMOVE(ndomain, ds_link);
6273f289c3fSJeff Roberson 
6283f289c3fSJeff Roberson 	/*
6293f289c3fSJeff Roberson 	 * Initialize the key from the request.
6303f289c3fSJeff Roberson 	 */
6313f289c3fSJeff Roberson 	domainset_copy(domain, ndomain);
6323f289c3fSJeff Roberson 
6333f289c3fSJeff Roberson 	/*
6343f289c3fSJeff Roberson 	 * Restrict the key by the parent.
6353f289c3fSJeff Roberson 	 */
6363f289c3fSJeff Roberson 	DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask);
6373f289c3fSJeff Roberson 
6383f289c3fSJeff Roberson 	return _domainset_create(ndomain, freelist);
6393f289c3fSJeff Roberson }
6403f289c3fSJeff Roberson 
641d7f687fcSJeff Roberson /*
642d7f687fcSJeff Roberson  * Recursively check for errors that would occur from applying mask to
643d7f687fcSJeff Roberson  * the tree of sets starting at 'set'.  Checks for sets that would become
644d7f687fcSJeff Roberson  * empty as well as RDONLY flags.
645d7f687fcSJeff Roberson  */
646d7f687fcSJeff Roberson static int
cpuset_testupdate(struct cpuset * set,cpuset_t * mask,int augment_mask)647f1b18a66SKyle Evans cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int augment_mask)
648d7f687fcSJeff Roberson {
649d7f687fcSJeff Roberson 	struct cpuset *nset;
650d7f687fcSJeff Roberson 	cpuset_t newmask;
651d7f687fcSJeff Roberson 	int error;
652d7f687fcSJeff Roberson 
653d7f687fcSJeff Roberson 	mtx_assert(&cpuset_lock, MA_OWNED);
654d7f687fcSJeff Roberson 	if (set->cs_flags & CPU_SET_RDONLY)
655d7f687fcSJeff Roberson 		return (EPERM);
656f1b18a66SKyle Evans 	if (augment_mask) {
657e2650af1SStefan Eßer 		CPU_AND(&newmask, &set->cs_mask, mask);
658c9813d0aSJohn Baldwin 	} else
659c9813d0aSJohn Baldwin 		CPU_COPY(mask, &newmask);
660f1b18a66SKyle Evans 
661f1b18a66SKyle Evans 	if (CPU_EMPTY(&newmask))
662f1b18a66SKyle Evans 		return (EDEADLK);
66373c40187SJeff Roberson 	error = 0;
664d7f687fcSJeff Roberson 	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
665c9813d0aSJohn Baldwin 		if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
666d7f687fcSJeff Roberson 			break;
667d7f687fcSJeff Roberson 	return (error);
668d7f687fcSJeff Roberson }
669d7f687fcSJeff Roberson 
670d7f687fcSJeff Roberson /*
671d7f687fcSJeff Roberson  * Applies the mask 'mask' without checking for empty sets or permissions.
672d7f687fcSJeff Roberson  */
673d7f687fcSJeff Roberson static void
cpuset_update(struct cpuset * set,cpuset_t * mask)674d7f687fcSJeff Roberson cpuset_update(struct cpuset *set, cpuset_t *mask)
675d7f687fcSJeff Roberson {
676d7f687fcSJeff Roberson 	struct cpuset *nset;
677d7f687fcSJeff Roberson 
678d7f687fcSJeff Roberson 	mtx_assert(&cpuset_lock, MA_OWNED);
679e2650af1SStefan Eßer 	CPU_AND(&set->cs_mask, &set->cs_mask, mask);
680d7f687fcSJeff Roberson 	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
681d7f687fcSJeff Roberson 		cpuset_update(nset, &set->cs_mask);
682d7f687fcSJeff Roberson 
683d7f687fcSJeff Roberson 	return;
684d7f687fcSJeff Roberson }
685d7f687fcSJeff Roberson 
686d7f687fcSJeff Roberson /*
687d7f687fcSJeff Roberson  * Modify the set 'set' to use a copy of the mask provided.  Apply this new
688d7f687fcSJeff Roberson  * mask to restrict all children in the tree.  Checks for validity before
689d7f687fcSJeff Roberson  * applying the changes.
690d7f687fcSJeff Roberson  */
691d7f687fcSJeff Roberson static int
cpuset_modify(struct cpuset * set,cpuset_t * mask)692d7f687fcSJeff Roberson cpuset_modify(struct cpuset *set, cpuset_t *mask)
693d7f687fcSJeff Roberson {
69473c40187SJeff Roberson 	struct cpuset *root;
695d7f687fcSJeff Roberson 	int error;
696d7f687fcSJeff Roberson 
697ba931c08SBjoern A. Zeeb 	error = priv_check(curthread, PRIV_SCHED_CPUSET);
698d7f687fcSJeff Roberson 	if (error)
699d7f687fcSJeff Roberson 		return (error);
70073c40187SJeff Roberson 	/*
70154a837c8SKyle Evans 	 * In case we are called from within the jail,
7026aaa0b3cSBjoern A. Zeeb 	 * we do not allow modifying the dedicated root
7036aaa0b3cSBjoern A. Zeeb 	 * cpuset of the jail but may still allow to
70454a837c8SKyle Evans 	 * change child sets, including subordinate jails'
70554a837c8SKyle Evans 	 * roots.
7066aaa0b3cSBjoern A. Zeeb 	 */
70754a837c8SKyle Evans 	if ((set->cs_flags & CPU_SET_ROOT) != 0 &&
70854a837c8SKyle Evans 	    jailed(curthread->td_ucred) &&
70954a837c8SKyle Evans 	    set == curthread->td_ucred->cr_prison->pr_cpuset)
7106aaa0b3cSBjoern A. Zeeb 		return (EPERM);
7116aaa0b3cSBjoern A. Zeeb 	/*
71273c40187SJeff Roberson 	 * Verify that we have access to this set of
71373c40187SJeff Roberson 	 * cpus.
71473c40187SJeff Roberson 	 */
71554a837c8SKyle Evans 	if ((set->cs_flags & (CPU_SET_ROOT | CPU_SET_RDONLY)) == CPU_SET_ROOT) {
71654a837c8SKyle Evans 		KASSERT(set->cs_parent != NULL,
71754a837c8SKyle Evans 		    ("jail.cpuset=%d is not a proper child of parent jail's root.",
71854a837c8SKyle Evans 		    set->cs_id));
71954a837c8SKyle Evans 
72054a837c8SKyle Evans 		/*
72154a837c8SKyle Evans 		 * cpuset_getroot() cannot work here due to how top-level jail
72254a837c8SKyle Evans 		 * roots are constructed.  Top-level jails are parented to
72354a837c8SKyle Evans 		 * thread0's cpuset (i.e. cpuset 1) rather than the system root.
72454a837c8SKyle Evans 		 */
72554a837c8SKyle Evans 		root = set->cs_parent;
72654a837c8SKyle Evans 	} else {
7273f289c3fSJeff Roberson 		root = cpuset_getroot(set);
72854a837c8SKyle Evans 	}
729d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
7303f289c3fSJeff Roberson 	if (root && !CPU_SUBSET(&root->cs_mask, mask)) {
7313f289c3fSJeff Roberson 		error = EINVAL;
7323f289c3fSJeff Roberson 		goto out;
7333f289c3fSJeff Roberson 	}
734c9813d0aSJohn Baldwin 	error = cpuset_testupdate(set, mask, 0);
735d7f687fcSJeff Roberson 	if (error)
736d7f687fcSJeff Roberson 		goto out;
737d7f687fcSJeff Roberson 	CPU_COPY(mask, &set->cs_mask);
738c9813d0aSJohn Baldwin 	cpuset_update(set, mask);
739d7f687fcSJeff Roberson out:
740d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
741d7f687fcSJeff Roberson 
742d7f687fcSJeff Roberson 	return (error);
743d7f687fcSJeff Roberson }
744d7f687fcSJeff Roberson 
745d7f687fcSJeff Roberson /*
7463f289c3fSJeff Roberson  * Recursively check for errors that would occur from applying mask to
7473f289c3fSJeff Roberson  * the tree of sets starting at 'set'.  Checks for sets that would become
7483f289c3fSJeff Roberson  * empty as well as RDONLY flags.
7493f289c3fSJeff Roberson  */
7503f289c3fSJeff Roberson static int
cpuset_testupdate_domain(struct cpuset * set,struct domainset * dset,struct domainset * orig,int * count,int augment_mask __unused)7513f289c3fSJeff Roberson cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset,
752f1b18a66SKyle Evans     struct domainset *orig, int *count, int augment_mask __unused)
7533f289c3fSJeff Roberson {
7543f289c3fSJeff Roberson 	struct cpuset *nset;
7553f289c3fSJeff Roberson 	struct domainset *domain;
7563f289c3fSJeff Roberson 	struct domainset newset;
7573f289c3fSJeff Roberson 	int error;
7583f289c3fSJeff Roberson 
7593f289c3fSJeff Roberson 	mtx_assert(&cpuset_lock, MA_OWNED);
7603f289c3fSJeff Roberson 	if (set->cs_flags & CPU_SET_RDONLY)
7613f289c3fSJeff Roberson 		return (EPERM);
7623f289c3fSJeff Roberson 	domain = set->cs_domain;
7633f289c3fSJeff Roberson 	domainset_copy(domain, &newset);
7643f289c3fSJeff Roberson 	if (!domainset_equal(domain, orig)) {
7653f289c3fSJeff Roberson 		if (!domainset_restrict(domain, dset))
7663f289c3fSJeff Roberson 			return (EDEADLK);
7673f289c3fSJeff Roberson 		DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask);
7683f289c3fSJeff Roberson 		/* Count the number of domains that are changing. */
7693f289c3fSJeff Roberson 		(*count)++;
7703f289c3fSJeff Roberson 	}
7713f289c3fSJeff Roberson 	error = 0;
7723f289c3fSJeff Roberson 	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
7733f289c3fSJeff Roberson 		if ((error = cpuset_testupdate_domain(nset, &newset, domain,
7743f289c3fSJeff Roberson 		    count, 1)) != 0)
7753f289c3fSJeff Roberson 			break;
7763f289c3fSJeff Roberson 	return (error);
7773f289c3fSJeff Roberson }
7783f289c3fSJeff Roberson 
7793f289c3fSJeff Roberson /*
7803f289c3fSJeff Roberson  * Applies the mask 'mask' without checking for empty sets or permissions.
7813f289c3fSJeff Roberson  */
7823f289c3fSJeff Roberson static void
cpuset_update_domain(struct cpuset * set,struct domainset * domain,struct domainset * orig,struct domainlist * domains)7833f289c3fSJeff Roberson cpuset_update_domain(struct cpuset *set, struct domainset *domain,
7843f289c3fSJeff Roberson     struct domainset *orig, struct domainlist *domains)
7853f289c3fSJeff Roberson {
7863f289c3fSJeff Roberson 	struct cpuset *nset;
7873f289c3fSJeff Roberson 
7883f289c3fSJeff Roberson 	mtx_assert(&cpuset_lock, MA_OWNED);
7893f289c3fSJeff Roberson 	/*
7903f289c3fSJeff Roberson 	 * If this domainset has changed from the parent we must calculate
7913f289c3fSJeff Roberson 	 * a new set.  Otherwise it simply inherits from the parent.  When
7923f289c3fSJeff Roberson 	 * we inherit from the parent we get a new mask and policy.  If the
7933f289c3fSJeff Roberson 	 * set is modified from the parent we keep the policy and only
7943f289c3fSJeff Roberson 	 * update the mask.
7953f289c3fSJeff Roberson 	 */
7963f289c3fSJeff Roberson 	if (set->cs_domain != orig) {
7973f289c3fSJeff Roberson 		orig = set->cs_domain;
7983f289c3fSJeff Roberson 		set->cs_domain = domainset_shadow(domain, orig, domains);
7993f289c3fSJeff Roberson 	} else
8003f289c3fSJeff Roberson 		set->cs_domain = domain;
8013f289c3fSJeff Roberson 	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
8023f289c3fSJeff Roberson 		cpuset_update_domain(nset, set->cs_domain, orig, domains);
8033f289c3fSJeff Roberson 
8043f289c3fSJeff Roberson 	return;
8053f289c3fSJeff Roberson }
8063f289c3fSJeff Roberson 
8073f289c3fSJeff Roberson /*
8083f289c3fSJeff Roberson  * Modify the set 'set' to use a copy the domainset provided.  Apply this new
8093f289c3fSJeff Roberson  * mask to restrict all children in the tree.  Checks for validity before
8103f289c3fSJeff Roberson  * applying the changes.
8113f289c3fSJeff Roberson  */
8123f289c3fSJeff Roberson static int
cpuset_modify_domain(struct cpuset * set,struct domainset * domain)8133f289c3fSJeff Roberson cpuset_modify_domain(struct cpuset *set, struct domainset *domain)
8143f289c3fSJeff Roberson {
8153f289c3fSJeff Roberson 	struct domainlist domains;
8163f289c3fSJeff Roberson 	struct domainset temp;
8173f289c3fSJeff Roberson 	struct domainset *dset;
8183f289c3fSJeff Roberson 	struct cpuset *root;
8193f289c3fSJeff Roberson 	int ndomains, needed;
8203f289c3fSJeff Roberson 	int error;
8213f289c3fSJeff Roberson 
8223f289c3fSJeff Roberson 	error = priv_check(curthread, PRIV_SCHED_CPUSET);
8233f289c3fSJeff Roberson 	if (error)
8243f289c3fSJeff Roberson 		return (error);
8253f289c3fSJeff Roberson 	/*
8263f289c3fSJeff Roberson 	 * In case we are called from within the jail
8273f289c3fSJeff Roberson 	 * we do not allow modifying the dedicated root
8283f289c3fSJeff Roberson 	 * cpuset of the jail but may still allow to
8293f289c3fSJeff Roberson 	 * change child sets.
8303f289c3fSJeff Roberson 	 */
8313f289c3fSJeff Roberson 	if (jailed(curthread->td_ucred) &&
8323f289c3fSJeff Roberson 	    set->cs_flags & CPU_SET_ROOT)
8333f289c3fSJeff Roberson 		return (EPERM);
8343f289c3fSJeff Roberson 	domainset_freelist_init(&domains, 0);
8353f289c3fSJeff Roberson 	domain = domainset_create(domain);
836e07e3fa3SKyle Evans 	ndomains = 0;
837e07e3fa3SKyle Evans 
8383f289c3fSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
839e07e3fa3SKyle Evans 	for (;;) {
840e07e3fa3SKyle Evans 		root = cpuset_getroot(set);
8413f289c3fSJeff Roberson 		dset = root->cs_domain;
8423f289c3fSJeff Roberson 		/*
8433f289c3fSJeff Roberson 		 * Verify that we have access to this set of domains.
8443f289c3fSJeff Roberson 		 */
84545cdd437SMark Johnston 		if (!domainset_valid(dset, domain)) {
8463f289c3fSJeff Roberson 			error = EINVAL;
8473f289c3fSJeff Roberson 			goto out;
8483f289c3fSJeff Roberson 		}
8493f289c3fSJeff Roberson 		/*
8503f289c3fSJeff Roberson 		 * If applying prefer we keep the current set as the fallback.
8513f289c3fSJeff Roberson 		 */
8523f289c3fSJeff Roberson 		if (domain->ds_policy == DOMAINSET_POLICY_PREFER)
8533f289c3fSJeff Roberson 			DOMAINSET_COPY(&set->cs_domain->ds_mask,
8543f289c3fSJeff Roberson 			    &domain->ds_mask);
8553f289c3fSJeff Roberson 		/*
8563f289c3fSJeff Roberson 		 * Determine whether we can apply this set of domains and
8573f289c3fSJeff Roberson 		 * how many new domain structures it will require.
8583f289c3fSJeff Roberson 		 */
8593f289c3fSJeff Roberson 		domainset_copy(domain, &temp);
8603f289c3fSJeff Roberson 		needed = 0;
8613f289c3fSJeff Roberson 		error = cpuset_testupdate_domain(set, &temp, set->cs_domain,
8623f289c3fSJeff Roberson 		    &needed, 0);
8633f289c3fSJeff Roberson 		if (error)
8643f289c3fSJeff Roberson 			goto out;
865e07e3fa3SKyle Evans 		if (ndomains >= needed)
866e07e3fa3SKyle Evans 			break;
867e07e3fa3SKyle Evans 
868e07e3fa3SKyle Evans 		/* Dropping the lock; we'll need to re-evaluate again. */
869e07e3fa3SKyle Evans 		mtx_unlock_spin(&cpuset_lock);
870e07e3fa3SKyle Evans 		domainset_freelist_add(&domains, needed - ndomains);
871e07e3fa3SKyle Evans 		ndomains = needed;
872e07e3fa3SKyle Evans 		mtx_lock_spin(&cpuset_lock);
873e07e3fa3SKyle Evans 	}
8743f289c3fSJeff Roberson 	dset = set->cs_domain;
8753f289c3fSJeff Roberson 	cpuset_update_domain(set, domain, dset, &domains);
8763f289c3fSJeff Roberson out:
8773f289c3fSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
8783f289c3fSJeff Roberson 	domainset_freelist_free(&domains);
8793f289c3fSJeff Roberson 	if (error == 0)
8803f289c3fSJeff Roberson 		domainset_notify();
8813f289c3fSJeff Roberson 
8823f289c3fSJeff Roberson 	return (error);
8833f289c3fSJeff Roberson }
8843f289c3fSJeff Roberson 
8853f289c3fSJeff Roberson /*
886d7f687fcSJeff Roberson  * Resolve the 'which' parameter of several cpuset apis.
887d7f687fcSJeff Roberson  *
888d7f687fcSJeff Roberson  * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid.  Also
889d7f687fcSJeff Roberson  * checks for permission via p_cansched().
890d7f687fcSJeff Roberson  *
891d7f687fcSJeff Roberson  * For WHICH_SET returns a valid set with a new reference.
892d7f687fcSJeff Roberson  *
893d7f687fcSJeff Roberson  * -1 may be supplied for any argument to mean the current proc/thread or
894d7f687fcSJeff Roberson  * the base set of the current thread.  May fail with ESRCH/EPERM.
895d7f687fcSJeff Roberson  */
8965bbb2169SAdrian Chadd int
cpuset_which(cpuwhich_t which,id_t id,struct proc ** pp,struct thread ** tdp,struct cpuset ** setp)897d7f687fcSJeff Roberson cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
898d7f687fcSJeff Roberson     struct cpuset **setp)
899d7f687fcSJeff Roberson {
900d7f687fcSJeff Roberson 	struct cpuset *set;
901d7f687fcSJeff Roberson 	struct thread *td;
902d7f687fcSJeff Roberson 	struct proc *p;
903d7f687fcSJeff Roberson 	int error;
904d7f687fcSJeff Roberson 
905d7f687fcSJeff Roberson 	*pp = p = NULL;
906d7f687fcSJeff Roberson 	*tdp = td = NULL;
907d7f687fcSJeff Roberson 	*setp = set = NULL;
908d7f687fcSJeff Roberson 	switch (which) {
909d7f687fcSJeff Roberson 	case CPU_WHICH_PID:
910d7f687fcSJeff Roberson 		if (id == -1) {
911d7f687fcSJeff Roberson 			PROC_LOCK(curproc);
912d7f687fcSJeff Roberson 			p = curproc;
913d7f687fcSJeff Roberson 			break;
914d7f687fcSJeff Roberson 		}
915d7f687fcSJeff Roberson 		if ((p = pfind(id)) == NULL)
916d7f687fcSJeff Roberson 			return (ESRCH);
917d7f687fcSJeff Roberson 		break;
918d7f687fcSJeff Roberson 	case CPU_WHICH_TID:
919d7f687fcSJeff Roberson 		if (id == -1) {
920d7f687fcSJeff Roberson 			PROC_LOCK(curproc);
921d7f687fcSJeff Roberson 			p = curproc;
922d7f687fcSJeff Roberson 			td = curthread;
923d7f687fcSJeff Roberson 			break;
924d7f687fcSJeff Roberson 		}
92542fe684cSDavid Xu 		td = tdfind(id, -1);
926d7f687fcSJeff Roberson 		if (td == NULL)
927d7f687fcSJeff Roberson 			return (ESRCH);
92842fe684cSDavid Xu 		p = td->td_proc;
929d7f687fcSJeff Roberson 		break;
930c21b080fSDmitry Chagin 	case CPU_WHICH_TIDPID:
931c21b080fSDmitry Chagin 		if (id == -1) {
932c21b080fSDmitry Chagin 			PROC_LOCK(curproc);
933c21b080fSDmitry Chagin 			td = curthread;
934c21b080fSDmitry Chagin 			p = curproc;
935c21b080fSDmitry Chagin 		} else if (id > PID_MAX) {
936c21b080fSDmitry Chagin 			td = tdfind(id, -1);
937c21b080fSDmitry Chagin 			if (td == NULL)
938c21b080fSDmitry Chagin 				return (ESRCH);
939c21b080fSDmitry Chagin 			p = td->td_proc;
940c21b080fSDmitry Chagin 		} else {
941c21b080fSDmitry Chagin 			p = pfind(id);
942c21b080fSDmitry Chagin 			if (p == NULL)
943c21b080fSDmitry Chagin 				return (ESRCH);
944c21b080fSDmitry Chagin 		}
945c21b080fSDmitry Chagin 		break;
946d7f687fcSJeff Roberson 	case CPU_WHICH_CPUSET:
947d7f687fcSJeff Roberson 		if (id == -1) {
948d7f687fcSJeff Roberson 			thread_lock(curthread);
949a03ee000SJeff Roberson 			set = cpuset_refbase(curthread->td_cpuset);
950d7f687fcSJeff Roberson 			thread_unlock(curthread);
951d7f687fcSJeff Roberson 		} else
952413628a7SBjoern A. Zeeb 			set = cpuset_lookup(id, curthread);
953d7f687fcSJeff Roberson 		if (set) {
954d7f687fcSJeff Roberson 			*setp = set;
955d7f687fcSJeff Roberson 			return (0);
956d7f687fcSJeff Roberson 		}
957d7f687fcSJeff Roberson 		return (ESRCH);
958413628a7SBjoern A. Zeeb 	case CPU_WHICH_JAIL:
959413628a7SBjoern A. Zeeb 	{
960413628a7SBjoern A. Zeeb 		/* Find `set' for prison with given id. */
961413628a7SBjoern A. Zeeb 		struct prison *pr;
962413628a7SBjoern A. Zeeb 
963413628a7SBjoern A. Zeeb 		sx_slock(&allprison_lock);
9640304c731SJamie Gritton 		pr = prison_find_child(curthread->td_ucred->cr_prison, id);
965413628a7SBjoern A. Zeeb 		sx_sunlock(&allprison_lock);
966413628a7SBjoern A. Zeeb 		if (pr == NULL)
967413628a7SBjoern A. Zeeb 			return (ESRCH);
968413628a7SBjoern A. Zeeb 		cpuset_ref(pr->pr_cpuset);
9690304c731SJamie Gritton 		*setp = pr->pr_cpuset;
970413628a7SBjoern A. Zeeb 		mtx_unlock(&pr->pr_mtx);
971413628a7SBjoern A. Zeeb 		return (0);
972413628a7SBjoern A. Zeeb 	}
9739b33b154SJeff Roberson 	case CPU_WHICH_IRQ:
974c0ae6688SJohn Baldwin 	case CPU_WHICH_DOMAIN:
9759b33b154SJeff Roberson 		return (0);
976d7f687fcSJeff Roberson 	default:
977d7f687fcSJeff Roberson 		return (EINVAL);
978d7f687fcSJeff Roberson 	}
979d7f687fcSJeff Roberson 	error = p_cansched(curthread, p);
980d7f687fcSJeff Roberson 	if (error) {
981d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
982d7f687fcSJeff Roberson 		return (error);
983d7f687fcSJeff Roberson 	}
984d7f687fcSJeff Roberson 	if (td == NULL)
985d7f687fcSJeff Roberson 		td = FIRST_THREAD_IN_PROC(p);
986d7f687fcSJeff Roberson 	*pp = p;
987d7f687fcSJeff Roberson 	*tdp = td;
988d7f687fcSJeff Roberson 	return (0);
989d7f687fcSJeff Roberson }
990d7f687fcSJeff Roberson 
991d7f687fcSJeff Roberson static int
cpuset_which2(cpuwhich_t * which,id_t id,struct proc ** pp,struct thread ** tdp,struct cpuset ** setp)9922058f075SDmitry Chagin cpuset_which2(cpuwhich_t *which, id_t id, struct proc **pp, struct thread **tdp,
9932058f075SDmitry Chagin     struct cpuset **setp)
9942058f075SDmitry Chagin {
9952058f075SDmitry Chagin 
9962058f075SDmitry Chagin 	if (*which == CPU_WHICH_TIDPID) {
9972058f075SDmitry Chagin 		if (id == -1 || id > PID_MAX)
9982058f075SDmitry Chagin 			*which = CPU_WHICH_TID;
9992058f075SDmitry Chagin 		else
10002058f075SDmitry Chagin 			*which = CPU_WHICH_PID;
10012058f075SDmitry Chagin 	}
10022058f075SDmitry Chagin 	return (cpuset_which(*which, id, pp, tdp, setp));
10032058f075SDmitry Chagin }
10042058f075SDmitry Chagin 
10052058f075SDmitry Chagin static int
cpuset_testshadow(struct cpuset * set,const cpuset_t * mask,const struct domainset * domain)10063f289c3fSJeff Roberson cpuset_testshadow(struct cpuset *set, const cpuset_t *mask,
10073f289c3fSJeff Roberson     const struct domainset *domain)
1008d7f687fcSJeff Roberson {
1009d7f687fcSJeff Roberson 	struct cpuset *parent;
10103f289c3fSJeff Roberson 	struct domainset *dset;
1011d7f687fcSJeff Roberson 
10123f289c3fSJeff Roberson 	parent = cpuset_getbase(set);
10133f289c3fSJeff Roberson 	/*
10143f289c3fSJeff Roberson 	 * If we are restricting a cpu mask it must be a subset of the
10153f289c3fSJeff Roberson 	 * parent or invalid CPUs have been specified.
10163f289c3fSJeff Roberson 	 */
10173f289c3fSJeff Roberson 	if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask))
10183f289c3fSJeff Roberson 		return (EINVAL);
10193f289c3fSJeff Roberson 
10203f289c3fSJeff Roberson 	/*
10213f289c3fSJeff Roberson 	 * If we are restricting a domain mask it must be a subset of the
10223f289c3fSJeff Roberson 	 * parent or invalid domains have been specified.
10233f289c3fSJeff Roberson 	 */
10243f289c3fSJeff Roberson 	dset = parent->cs_domain;
10253f289c3fSJeff Roberson 	if (domain != NULL && !domainset_valid(dset, domain))
10263f289c3fSJeff Roberson 		return (EINVAL);
10273f289c3fSJeff Roberson 
10283f289c3fSJeff Roberson 	return (0);
1029d7f687fcSJeff Roberson }
1030d7f687fcSJeff Roberson 
1031d7f687fcSJeff Roberson /*
10323f289c3fSJeff Roberson  * Create an anonymous set with the provided mask in the space provided by
10333f289c3fSJeff Roberson  * 'nset'.  If the passed in set is anonymous we use its parent otherwise
10343f289c3fSJeff Roberson  * the new set is a child of 'set'.
10353f289c3fSJeff Roberson  */
10363f289c3fSJeff Roberson static int
cpuset_shadow(struct cpuset * set,struct cpuset ** nsetp,const cpuset_t * mask,const struct domainset * domain,struct setlist * cpusets,struct domainlist * domains)10373f289c3fSJeff Roberson cpuset_shadow(struct cpuset *set, struct cpuset **nsetp,
10383f289c3fSJeff Roberson    const cpuset_t *mask, const struct domainset *domain,
10393f289c3fSJeff Roberson    struct setlist *cpusets, struct domainlist *domains)
10403f289c3fSJeff Roberson {
10413f289c3fSJeff Roberson 	struct cpuset *parent;
10423f289c3fSJeff Roberson 	struct cpuset *nset;
10433f289c3fSJeff Roberson 	struct domainset *dset;
10443f289c3fSJeff Roberson 	struct domainset *d;
10453f289c3fSJeff Roberson 	int error;
10463f289c3fSJeff Roberson 
10473f289c3fSJeff Roberson 	error = cpuset_testshadow(set, mask, domain);
10483f289c3fSJeff Roberson 	if (error)
10493f289c3fSJeff Roberson 		return (error);
10503f289c3fSJeff Roberson 
10513f289c3fSJeff Roberson 	parent = cpuset_getbase(set);
10523f289c3fSJeff Roberson 	dset = parent->cs_domain;
10533f289c3fSJeff Roberson 	if (mask == NULL)
10543f289c3fSJeff Roberson 		mask = &set->cs_mask;
10553f289c3fSJeff Roberson 	if (domain != NULL)
10563f289c3fSJeff Roberson 		d = domainset_shadow(dset, domain, domains);
10573f289c3fSJeff Roberson 	else
10583f289c3fSJeff Roberson 		d = set->cs_domain;
10593f289c3fSJeff Roberson 	nset = LIST_FIRST(cpusets);
106030b7c6f9SKyle Evans 	error = cpuset_init(nset, parent, mask, d, CPUSET_INVALID);
10613f289c3fSJeff Roberson 	if (error == 0) {
10623f289c3fSJeff Roberson 		LIST_REMOVE(nset, cs_link);
10633f289c3fSJeff Roberson 		*nsetp = nset;
10643f289c3fSJeff Roberson 	}
10653f289c3fSJeff Roberson 	return (error);
10663f289c3fSJeff Roberson }
10673f289c3fSJeff Roberson 
10683f289c3fSJeff Roberson static struct cpuset *
cpuset_update_thread(struct thread * td,struct cpuset * nset)10693f289c3fSJeff Roberson cpuset_update_thread(struct thread *td, struct cpuset *nset)
10703f289c3fSJeff Roberson {
10713f289c3fSJeff Roberson 	struct cpuset *tdset;
10723f289c3fSJeff Roberson 
10733f289c3fSJeff Roberson 	tdset = td->td_cpuset;
10743f289c3fSJeff Roberson 	td->td_cpuset = nset;
10753f289c3fSJeff Roberson 	td->td_domain.dr_policy = nset->cs_domain;
10763f289c3fSJeff Roberson 	sched_affinity(td);
10773f289c3fSJeff Roberson 
10783f289c3fSJeff Roberson 	return (tdset);
10793f289c3fSJeff Roberson }
10803f289c3fSJeff Roberson 
10813f289c3fSJeff Roberson static int
cpuset_setproc_test_maskthread(struct cpuset * tdset,cpuset_t * mask,struct domainset * domain)10823f289c3fSJeff Roberson cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask,
10833f289c3fSJeff Roberson     struct domainset *domain)
10843f289c3fSJeff Roberson {
10853f289c3fSJeff Roberson 	struct cpuset *parent;
10863f289c3fSJeff Roberson 
10873f289c3fSJeff Roberson 	parent = cpuset_getbase(tdset);
10883f289c3fSJeff Roberson 	if (mask == NULL)
10893f289c3fSJeff Roberson 		mask = &tdset->cs_mask;
10903f289c3fSJeff Roberson 	if (domain == NULL)
10913f289c3fSJeff Roberson 		domain = tdset->cs_domain;
10923f289c3fSJeff Roberson 	return cpuset_testshadow(parent, mask, domain);
10933f289c3fSJeff Roberson }
10943f289c3fSJeff Roberson 
10953f289c3fSJeff Roberson static int
cpuset_setproc_maskthread(struct cpuset * tdset,cpuset_t * mask,struct domainset * domain,struct cpuset ** nsetp,struct setlist * freelist,struct domainlist * domainlist)10963f289c3fSJeff Roberson cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask,
10973f289c3fSJeff Roberson     struct domainset *domain, struct cpuset **nsetp,
10983f289c3fSJeff Roberson     struct setlist *freelist, struct domainlist *domainlist)
10993f289c3fSJeff Roberson {
11003f289c3fSJeff Roberson 	struct cpuset *parent;
11013f289c3fSJeff Roberson 
11023f289c3fSJeff Roberson 	parent = cpuset_getbase(tdset);
11033f289c3fSJeff Roberson 	if (mask == NULL)
11043f289c3fSJeff Roberson 		mask = &tdset->cs_mask;
11053f289c3fSJeff Roberson 	if (domain == NULL)
11063f289c3fSJeff Roberson 		domain = tdset->cs_domain;
11073f289c3fSJeff Roberson 	return cpuset_shadow(parent, nsetp, mask, domain, freelist,
11083f289c3fSJeff Roberson 	    domainlist);
11093f289c3fSJeff Roberson }
11103f289c3fSJeff Roberson 
11113f289c3fSJeff Roberson static int
cpuset_setproc_setthread_mask(struct cpuset * tdset,struct cpuset * set,cpuset_t * mask,struct domainset * domain)11123f289c3fSJeff Roberson cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set,
11133f289c3fSJeff Roberson     cpuset_t *mask, struct domainset *domain)
11143f289c3fSJeff Roberson {
11153f289c3fSJeff Roberson 	struct cpuset *parent;
11163f289c3fSJeff Roberson 
11173f289c3fSJeff Roberson 	parent = cpuset_getbase(tdset);
11183f289c3fSJeff Roberson 
11193f289c3fSJeff Roberson 	/*
11203f289c3fSJeff Roberson 	 * If the thread restricted its mask then apply that same
11213f289c3fSJeff Roberson 	 * restriction to the new set, otherwise take it wholesale.
11223f289c3fSJeff Roberson 	 */
11233f289c3fSJeff Roberson 	if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) {
1124e2650af1SStefan Eßer 		CPU_AND(mask, &tdset->cs_mask, &set->cs_mask);
11253f289c3fSJeff Roberson 	} else
11263f289c3fSJeff Roberson 		CPU_COPY(&set->cs_mask, mask);
11273f289c3fSJeff Roberson 
11283f289c3fSJeff Roberson 	/*
11293f289c3fSJeff Roberson 	 * If the thread restricted the domain then we apply the
11303f289c3fSJeff Roberson 	 * restriction to the new set but retain the policy.
11313f289c3fSJeff Roberson 	 */
11323f289c3fSJeff Roberson 	if (tdset->cs_domain != parent->cs_domain) {
11333f289c3fSJeff Roberson 		domainset_copy(tdset->cs_domain, domain);
11343f289c3fSJeff Roberson 		DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask);
11353f289c3fSJeff Roberson 	} else
11363f289c3fSJeff Roberson 		domainset_copy(set->cs_domain, domain);
11373f289c3fSJeff Roberson 
11383f289c3fSJeff Roberson 	if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask))
11393f289c3fSJeff Roberson 		return (EDEADLK);
11403f289c3fSJeff Roberson 
11413f289c3fSJeff Roberson 	return (0);
11423f289c3fSJeff Roberson }
11433f289c3fSJeff Roberson 
11443f289c3fSJeff Roberson static int
cpuset_setproc_test_setthread(struct cpuset * tdset,struct cpuset * set)11453f289c3fSJeff Roberson cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set)
11463f289c3fSJeff Roberson {
11473f289c3fSJeff Roberson 	struct domainset domain;
11483f289c3fSJeff Roberson 	cpuset_t mask;
11493f289c3fSJeff Roberson 
11503f289c3fSJeff Roberson 	if (tdset->cs_id != CPUSET_INVALID)
11513f289c3fSJeff Roberson 		return (0);
11523f289c3fSJeff Roberson 	return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
11533f289c3fSJeff Roberson }
11543f289c3fSJeff Roberson 
11553f289c3fSJeff Roberson static int
cpuset_setproc_setthread(struct cpuset * tdset,struct cpuset * set,struct cpuset ** nsetp,struct setlist * freelist,struct domainlist * domainlist)11563f289c3fSJeff Roberson cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set,
11573f289c3fSJeff Roberson     struct cpuset **nsetp, struct setlist *freelist,
11583f289c3fSJeff Roberson     struct domainlist *domainlist)
11593f289c3fSJeff Roberson {
11603f289c3fSJeff Roberson 	struct domainset domain;
11613f289c3fSJeff Roberson 	cpuset_t mask;
11623f289c3fSJeff Roberson 	int error;
11633f289c3fSJeff Roberson 
11643f289c3fSJeff Roberson 	/*
11653f289c3fSJeff Roberson 	 * If we're replacing on a thread that has not constrained the
11663f289c3fSJeff Roberson 	 * original set we can simply accept the new set.
11673f289c3fSJeff Roberson 	 */
11683f289c3fSJeff Roberson 	if (tdset->cs_id != CPUSET_INVALID) {
11693f289c3fSJeff Roberson 		*nsetp = cpuset_ref(set);
11703f289c3fSJeff Roberson 		return (0);
11713f289c3fSJeff Roberson 	}
11723f289c3fSJeff Roberson 	error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain);
11733f289c3fSJeff Roberson 	if (error)
11743f289c3fSJeff Roberson 		return (error);
11753f289c3fSJeff Roberson 
1176dac521ebSKyle Evans 	return cpuset_shadow(set, nsetp, &mask, &domain, freelist,
11773f289c3fSJeff Roberson 	    domainlist);
11783f289c3fSJeff Roberson }
11793f289c3fSJeff Roberson 
1180d431dea5SKyle Evans static int
cpuset_setproc_newbase(struct thread * td,struct cpuset * set,struct cpuset * nroot,struct cpuset ** nsetp,struct setlist * cpusets,struct domainlist * domainlist)1181d431dea5SKyle Evans cpuset_setproc_newbase(struct thread *td, struct cpuset *set,
1182d431dea5SKyle Evans     struct cpuset *nroot, struct cpuset **nsetp,
1183d431dea5SKyle Evans     struct setlist *cpusets, struct domainlist *domainlist)
1184d431dea5SKyle Evans {
1185d431dea5SKyle Evans 	struct domainset ndomain;
1186d431dea5SKyle Evans 	cpuset_t nmask;
1187d431dea5SKyle Evans 	struct cpuset *pbase;
1188d431dea5SKyle Evans 	int error;
1189d431dea5SKyle Evans 
1190d431dea5SKyle Evans 	pbase = cpuset_getbase(td->td_cpuset);
1191d431dea5SKyle Evans 
1192d431dea5SKyle Evans 	/* Copy process mask, then further apply the new root mask. */
1193e2650af1SStefan Eßer 	CPU_AND(&nmask, &pbase->cs_mask, &nroot->cs_mask);
1194d431dea5SKyle Evans 
1195d431dea5SKyle Evans 	domainset_copy(pbase->cs_domain, &ndomain);
1196d431dea5SKyle Evans 	DOMAINSET_AND(&ndomain.ds_mask, &set->cs_domain->ds_mask);
1197d431dea5SKyle Evans 
1198d431dea5SKyle Evans 	/* Policy is too restrictive, will not work. */
1199d431dea5SKyle Evans 	if (CPU_EMPTY(&nmask) || DOMAINSET_EMPTY(&ndomain.ds_mask))
1200d431dea5SKyle Evans 		return (EDEADLK);
1201d431dea5SKyle Evans 
12023f289c3fSJeff Roberson 	/*
1203d431dea5SKyle Evans 	 * Remove pbase from the freelist in advance, it'll be pushed to
1204d431dea5SKyle Evans 	 * cpuset_ids on success.  We assume here that cpuset_create() will not
1205d431dea5SKyle Evans 	 * touch pbase on failure, and we just enqueue it back to the freelist
1206d431dea5SKyle Evans 	 * to remain in a consistent state.
1207d431dea5SKyle Evans 	 */
1208d431dea5SKyle Evans 	pbase = LIST_FIRST(cpusets);
1209d431dea5SKyle Evans 	LIST_REMOVE(pbase, cs_link);
1210d431dea5SKyle Evans 	error = cpuset_create(&pbase, set, &nmask);
1211d431dea5SKyle Evans 	if (error != 0) {
1212d431dea5SKyle Evans 		LIST_INSERT_HEAD(cpusets, pbase, cs_link);
1213d431dea5SKyle Evans 		return (error);
1214d431dea5SKyle Evans 	}
1215d431dea5SKyle Evans 
1216d431dea5SKyle Evans 	/* Duplicates some work from above... oh well. */
1217d431dea5SKyle Evans 	pbase->cs_domain = domainset_shadow(set->cs_domain, &ndomain,
1218d431dea5SKyle Evans 	    domainlist);
1219d431dea5SKyle Evans 	*nsetp = pbase;
1220d431dea5SKyle Evans 	return (0);
1221d431dea5SKyle Evans }
1222d431dea5SKyle Evans 
1223d431dea5SKyle Evans /*
1224d431dea5SKyle Evans  * Handle four cases for updating an entire process.
1225d7f687fcSJeff Roberson  *
1226d431dea5SKyle Evans  * 1) Set is non-null and the process is not rebasing onto a new root.  This
1227d431dea5SKyle Evans  *    reparents all anonymous sets to the provided set and replaces all
1228d431dea5SKyle Evans  *    non-anonymous td_cpusets with the provided set.
1229d431dea5SKyle Evans  * 2) Set is non-null and the process is rebasing onto a new root.  This
1230d431dea5SKyle Evans  *    creates a new base set if the process previously had its own base set,
1231d431dea5SKyle Evans  *    then reparents all anonymous sets either to that set or the provided set
1232d431dea5SKyle Evans  *    if one was not created.  Non-anonymous sets are similarly replaced.
1233d431dea5SKyle Evans  * 3) Mask is non-null.  This replaces or creates anonymous sets for every
12343f289c3fSJeff Roberson  *    thread with the existing base as a parent.
1235d431dea5SKyle Evans  * 4) domain is non-null.  This creates anonymous sets for every thread
12363f289c3fSJeff Roberson  *    and replaces the domain set.
1237d7f687fcSJeff Roberson  *
1238d7f687fcSJeff Roberson  * This is overly complicated because we can't allocate while holding a
1239d7f687fcSJeff Roberson  * spinlock and spinlocks must be held while changing and examining thread
1240d7f687fcSJeff Roberson  * state.
1241d7f687fcSJeff Roberson  */
1242d7f687fcSJeff Roberson static int
cpuset_setproc(pid_t pid,struct cpuset * set,cpuset_t * mask,struct domainset * domain,bool rebase)12433f289c3fSJeff Roberson cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask,
1244d431dea5SKyle Evans     struct domainset *domain, bool rebase)
1245d7f687fcSJeff Roberson {
1246d7f687fcSJeff Roberson 	struct setlist freelist;
1247d7f687fcSJeff Roberson 	struct setlist droplist;
12483f289c3fSJeff Roberson 	struct domainlist domainlist;
1249d431dea5SKyle Evans 	struct cpuset *base, *nset, *nroot, *tdroot;
1250d7f687fcSJeff Roberson 	struct thread *td;
1251d7f687fcSJeff Roberson 	struct proc *p;
1252d431dea5SKyle Evans 	int needed;
1253d7f687fcSJeff Roberson 	int nfree;
1254d7f687fcSJeff Roberson 	int error;
1255f299c47bSAllan Jude 
1256d7f687fcSJeff Roberson 	/*
1257d7f687fcSJeff Roberson 	 * The algorithm requires two passes due to locking considerations.
1258d7f687fcSJeff Roberson 	 *
1259d7f687fcSJeff Roberson 	 * 1) Lookup the process and acquire the locks in the required order.
1260d7f687fcSJeff Roberson 	 * 2) If enough cpusets have not been allocated release the locks and
1261d7f687fcSJeff Roberson 	 *    allocate them.  Loop.
1262d7f687fcSJeff Roberson 	 */
12633f289c3fSJeff Roberson 	cpuset_freelist_init(&freelist, 1);
12643f289c3fSJeff Roberson 	domainset_freelist_init(&domainlist, 1);
12653f289c3fSJeff Roberson 	nfree = 1;
1266d7f687fcSJeff Roberson 	LIST_INIT(&droplist);
1267d7f687fcSJeff Roberson 	nfree = 0;
1268d431dea5SKyle Evans 	base = set;
1269d431dea5SKyle Evans 	nroot = NULL;
1270d431dea5SKyle Evans 	if (set != NULL)
1271d431dea5SKyle Evans 		nroot = cpuset_getroot(set);
1272d7f687fcSJeff Roberson 	for (;;) {
1273d7f687fcSJeff Roberson 		error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
1274d7f687fcSJeff Roberson 		if (error)
1275d7f687fcSJeff Roberson 			goto out;
1276d431dea5SKyle Evans 		tdroot = cpuset_getroot(td->td_cpuset);
1277d431dea5SKyle Evans 		needed = p->p_numthreads;
1278d431dea5SKyle Evans 		if (set != NULL && rebase && tdroot != nroot)
1279d431dea5SKyle Evans 			needed++;
1280d431dea5SKyle Evans 		if (nfree >= needed)
1281d7f687fcSJeff Roberson 			break;
1282d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
1283d431dea5SKyle Evans 		if (nfree < needed) {
1284d431dea5SKyle Evans 			cpuset_freelist_add(&freelist, needed - nfree);
1285d431dea5SKyle Evans 			domainset_freelist_add(&domainlist, needed - nfree);
1286d431dea5SKyle Evans 			nfree = needed;
1287d7f687fcSJeff Roberson 		}
1288d7f687fcSJeff Roberson 	}
1289d7f687fcSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
1290d431dea5SKyle Evans 
1291d431dea5SKyle Evans 	/*
1292d431dea5SKyle Evans 	 * If we're changing roots and the root set is what has been specified
1293d431dea5SKyle Evans 	 * as the parent, then we'll check if the process was previously using
1294d431dea5SKyle Evans 	 * the root set and, if it wasn't, create a new base with the process's
1295d431dea5SKyle Evans 	 * mask applied to it.
129660c4ec80SKyle Evans 	 *
129760c4ec80SKyle Evans 	 * If the new root is incompatible with the existing mask, then we allow
129860c4ec80SKyle Evans 	 * the process to take on the new root if and only if they have
129960c4ec80SKyle Evans 	 * privilege to widen their mask anyways.  Unprivileged processes get
130060c4ec80SKyle Evans 	 * rejected with EDEADLK.
1301d431dea5SKyle Evans 	 */
1302d431dea5SKyle Evans 	if (set != NULL && rebase && nroot != tdroot) {
1303d431dea5SKyle Evans 		cpusetid_t base_id, root_id;
1304d431dea5SKyle Evans 
1305d431dea5SKyle Evans 		root_id = td->td_ucred->cr_prison->pr_cpuset->cs_id;
1306d431dea5SKyle Evans 		base_id = cpuset_getbase(td->td_cpuset)->cs_id;
1307d431dea5SKyle Evans 
1308d431dea5SKyle Evans 		if (base_id != root_id) {
1309d431dea5SKyle Evans 			error = cpuset_setproc_newbase(td, set, nroot, &base,
1310d431dea5SKyle Evans 			    &freelist, &domainlist);
131160c4ec80SKyle Evans 			if (error == EDEADLK &&
131260c4ec80SKyle Evans 			    priv_check(td, PRIV_SCHED_CPUSET) == 0)
131360c4ec80SKyle Evans 				error = 0;
1314d431dea5SKyle Evans 			if (error != 0)
1315d431dea5SKyle Evans 				goto unlock_out;
1316d431dea5SKyle Evans 		}
1317d431dea5SKyle Evans 	}
1318d431dea5SKyle Evans 
1319d7f687fcSJeff Roberson 	/*
1320d7f687fcSJeff Roberson 	 * Now that the appropriate locks are held and we have enough cpusets,
132173c40187SJeff Roberson 	 * make sure the operation will succeed before applying changes. The
132273c40187SJeff Roberson 	 * proc lock prevents td_cpuset from changing between calls.
1323d7f687fcSJeff Roberson 	 */
1324d7f687fcSJeff Roberson 	error = 0;
1325d7f687fcSJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td) {
132673c40187SJeff Roberson 		thread_lock(td);
13273f289c3fSJeff Roberson 		if (set != NULL)
13283f289c3fSJeff Roberson 			error = cpuset_setproc_test_setthread(td->td_cpuset,
1329d431dea5SKyle Evans 			    base);
13303f289c3fSJeff Roberson 		else
13313f289c3fSJeff Roberson 			error = cpuset_setproc_test_maskthread(td->td_cpuset,
13323f289c3fSJeff Roberson 			    mask, domain);
133373c40187SJeff Roberson 		thread_unlock(td);
133473c40187SJeff Roberson 		if (error)
133573c40187SJeff Roberson 			goto unlock_out;
133673c40187SJeff Roberson 	}
133773c40187SJeff Roberson 	/*
133873c40187SJeff Roberson 	 * Replace each thread's cpuset while using deferred release.  We
1339374ae2a3SJeff Roberson 	 * must do this because the thread lock must be held while operating
1340374ae2a3SJeff Roberson 	 * on the thread and this limits the type of operations allowed.
134173c40187SJeff Roberson 	 */
134273c40187SJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td) {
1343d7f687fcSJeff Roberson 		thread_lock(td);
13443f289c3fSJeff Roberson 		if (set != NULL)
1345d431dea5SKyle Evans 			error = cpuset_setproc_setthread(td->td_cpuset, base,
13463f289c3fSJeff Roberson 			    &nset, &freelist, &domainlist);
1347d7f687fcSJeff Roberson 		else
13483f289c3fSJeff Roberson 			error = cpuset_setproc_maskthread(td->td_cpuset, mask,
13493f289c3fSJeff Roberson 			    domain, &nset, &freelist, &domainlist);
1350d7f687fcSJeff Roberson 		if (error) {
1351d7f687fcSJeff Roberson 			thread_unlock(td);
1352d7f687fcSJeff Roberson 			break;
1353d7f687fcSJeff Roberson 		}
13543f289c3fSJeff Roberson 		cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset));
1355d7f687fcSJeff Roberson 		thread_unlock(td);
1356d7f687fcSJeff Roberson 	}
135773c40187SJeff Roberson unlock_out:
1358d7f687fcSJeff Roberson 	PROC_UNLOCK(p);
1359d7f687fcSJeff Roberson out:
1360d431dea5SKyle Evans 	if (base != NULL && base != set)
1361d431dea5SKyle Evans 		cpuset_rel(base);
1362d7f687fcSJeff Roberson 	while ((nset = LIST_FIRST(&droplist)) != NULL)
1363d7f687fcSJeff Roberson 		cpuset_rel_complete(nset);
13643f289c3fSJeff Roberson 	cpuset_freelist_free(&freelist);
13653f289c3fSJeff Roberson 	domainset_freelist_free(&domainlist);
1366d7f687fcSJeff Roberson 	return (error);
1367d7f687fcSJeff Roberson }
1368d7f687fcSJeff Roberson 
1369e5818a53SJeff Roberson static int
bitset_strprint(char * buf,size_t bufsiz,const struct bitset * set,int setlen)1370e5818a53SJeff Roberson bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen)
1371e5818a53SJeff Roberson {
1372e5818a53SJeff Roberson 	size_t bytes;
1373e5818a53SJeff Roberson 	int i, once;
1374e5818a53SJeff Roberson 	char *p;
1375e5818a53SJeff Roberson 
1376e5818a53SJeff Roberson 	once = 0;
1377e5818a53SJeff Roberson 	p = buf;
1378e5818a53SJeff Roberson 	for (i = 0; i < __bitset_words(setlen); i++) {
1379e5818a53SJeff Roberson 		if (once != 0) {
1380e5818a53SJeff Roberson 			if (bufsiz < 1)
1381e5818a53SJeff Roberson 				return (0);
1382e5818a53SJeff Roberson 			*p = ',';
1383e5818a53SJeff Roberson 			p++;
1384e5818a53SJeff Roberson 			bufsiz--;
1385e5818a53SJeff Roberson 		} else
1386e5818a53SJeff Roberson 			once = 1;
1387e5818a53SJeff Roberson 		if (bufsiz < sizeof(__STRING(ULONG_MAX)))
1388e5818a53SJeff Roberson 			return (0);
1389e5818a53SJeff Roberson 		bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]);
1390e5818a53SJeff Roberson 		p += bytes;
1391e5818a53SJeff Roberson 		bufsiz -= bytes;
1392e5818a53SJeff Roberson 	}
1393e5818a53SJeff Roberson 	return (p - buf);
1394e5818a53SJeff Roberson }
1395e5818a53SJeff Roberson 
1396e5818a53SJeff Roberson static int
bitset_strscan(struct bitset * set,int setlen,const char * buf)1397e5818a53SJeff Roberson bitset_strscan(struct bitset *set, int setlen, const char *buf)
1398e5818a53SJeff Roberson {
1399e5818a53SJeff Roberson 	int i, ret;
1400e5818a53SJeff Roberson 	const char *p;
1401e5818a53SJeff Roberson 
1402e5818a53SJeff Roberson 	BIT_ZERO(setlen, set);
1403e5818a53SJeff Roberson 	p = buf;
1404e5818a53SJeff Roberson 	for (i = 0; i < __bitset_words(setlen); i++) {
1405e5818a53SJeff Roberson 		if (*p == ',') {
1406e5818a53SJeff Roberson 			p++;
1407e5818a53SJeff Roberson 			continue;
1408e5818a53SJeff Roberson 		}
1409e5818a53SJeff Roberson 		ret = sscanf(p, "%lx", &set->__bits[i]);
1410e5818a53SJeff Roberson 		if (ret == 0 || ret == -1)
1411e5818a53SJeff Roberson 			break;
1412e5818a53SJeff Roberson 		while (isxdigit(*p))
1413e5818a53SJeff Roberson 			p++;
1414e5818a53SJeff Roberson 	}
1415e5818a53SJeff Roberson 	return (p - buf);
1416e5818a53SJeff Roberson }
1417e5818a53SJeff Roberson 
1418d7f687fcSJeff Roberson /*
141971a19bdcSAttilio Rao  * Return a string representing a valid layout for a cpuset_t object.
142071a19bdcSAttilio Rao  * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
142171a19bdcSAttilio Rao  */
142271a19bdcSAttilio Rao char *
cpusetobj_strprint(char * buf,const cpuset_t * set)142371a19bdcSAttilio Rao cpusetobj_strprint(char *buf, const cpuset_t *set)
142471a19bdcSAttilio Rao {
142571a19bdcSAttilio Rao 
1426e5818a53SJeff Roberson 	bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set,
1427e5818a53SJeff Roberson 	    CPU_SETSIZE);
142871a19bdcSAttilio Rao 	return (buf);
142971a19bdcSAttilio Rao }
143071a19bdcSAttilio Rao 
143171a19bdcSAttilio Rao /*
1432e3709597SAttilio Rao  * Build a valid cpuset_t object from a string representation.
1433e3709597SAttilio Rao  * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
1434e3709597SAttilio Rao  */
1435e3709597SAttilio Rao int
cpusetobj_strscan(cpuset_t * set,const char * buf)1436e3709597SAttilio Rao cpusetobj_strscan(cpuset_t *set, const char *buf)
1437e3709597SAttilio Rao {
1438e5818a53SJeff Roberson 	char p;
1439e3709597SAttilio Rao 
1440e3709597SAttilio Rao 	if (strlen(buf) > CPUSETBUFSIZ - 1)
1441e3709597SAttilio Rao 		return (-1);
1442e3709597SAttilio Rao 
1443e5818a53SJeff Roberson 	p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)];
1444e5818a53SJeff Roberson 	if (p != '\0')
1445e3709597SAttilio Rao 		return (-1);
1446e3709597SAttilio Rao 
1447e3709597SAttilio Rao 	return (0);
1448e3709597SAttilio Rao }
1449e3709597SAttilio Rao 
1450e3709597SAttilio Rao /*
1451e5818a53SJeff Roberson  * Handle a domainset specifier in the sysctl tree.  A poiner to a pointer to
1452e5818a53SJeff Roberson  * a domainset is in arg1.  If the user specifies a valid domainset the
1453e5818a53SJeff Roberson  * pointer is updated.
1454e5818a53SJeff Roberson  *
1455e5818a53SJeff Roberson  * Format is:
1456e5818a53SJeff Roberson  * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred
1457e5818a53SJeff Roberson  */
1458e5818a53SJeff Roberson int
sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)1459e5818a53SJeff Roberson sysctl_handle_domainset(SYSCTL_HANDLER_ARGS)
1460e5818a53SJeff Roberson {
1461e5818a53SJeff Roberson 	char buf[DOMAINSETBUFSIZ];
1462e5818a53SJeff Roberson 	struct domainset *dset;
1463e5818a53SJeff Roberson 	struct domainset key;
1464e5818a53SJeff Roberson 	int policy, prefer, error;
1465e5818a53SJeff Roberson 	char *p;
1466e5818a53SJeff Roberson 
1467e5818a53SJeff Roberson 	dset = *(struct domainset **)arg1;
1468e5818a53SJeff Roberson 	error = 0;
1469e5818a53SJeff Roberson 
1470e5818a53SJeff Roberson 	if (dset != NULL) {
1471e5818a53SJeff Roberson 		p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ,
1472e5818a53SJeff Roberson 		    (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE);
1473e5818a53SJeff Roberson 		sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer);
1474e5818a53SJeff Roberson 	} else
1475e5818a53SJeff Roberson 		sprintf(buf, "<NULL>");
1476e5818a53SJeff Roberson 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1477e5818a53SJeff Roberson 	if (error != 0 || req->newptr == NULL)
1478e5818a53SJeff Roberson 		return (error);
1479e5818a53SJeff Roberson 
1480e5818a53SJeff Roberson 	/*
1481e5818a53SJeff Roberson 	 * Read in and validate the string.
1482e5818a53SJeff Roberson 	 */
1483e5818a53SJeff Roberson 	memset(&key, 0, sizeof(key));
1484e5818a53SJeff Roberson 	p = &buf[bitset_strscan((struct bitset *)&key.ds_mask,
1485e5818a53SJeff Roberson 	    DOMAINSET_SETSIZE, buf)];
1486e5818a53SJeff Roberson 	if (p == buf)
1487e5818a53SJeff Roberson 		return (EINVAL);
1488e5818a53SJeff Roberson 	if (sscanf(p, ":%d:%d", &policy, &prefer) != 2)
1489e5818a53SJeff Roberson 		return (EINVAL);
1490e5818a53SJeff Roberson 	key.ds_policy = policy;
1491e5818a53SJeff Roberson 	key.ds_prefer = prefer;
1492e5818a53SJeff Roberson 
1493e5818a53SJeff Roberson 	/* Domainset_create() validates the policy.*/
1494e5818a53SJeff Roberson 	dset = domainset_create(&key);
1495e5818a53SJeff Roberson 	if (dset == NULL)
1496e5818a53SJeff Roberson 		return (EINVAL);
1497e5818a53SJeff Roberson 	*(struct domainset **)arg1 = dset;
1498e5818a53SJeff Roberson 
1499e5818a53SJeff Roberson 	return (error);
1500e5818a53SJeff Roberson }
1501e5818a53SJeff Roberson 
1502e5818a53SJeff Roberson /*
15033f289c3fSJeff Roberson  * Apply an anonymous mask or a domain to a single thread.
1504d7f687fcSJeff Roberson  */
15053f289c3fSJeff Roberson static int
_cpuset_setthread(lwpid_t id,cpuset_t * mask,struct domainset * domain)15063f289c3fSJeff Roberson _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain)
1507d7f687fcSJeff Roberson {
15083f289c3fSJeff Roberson 	struct setlist cpusets;
15093f289c3fSJeff Roberson 	struct domainlist domainlist;
1510d7f687fcSJeff Roberson 	struct cpuset *nset;
1511d7f687fcSJeff Roberson 	struct cpuset *set;
1512d7f687fcSJeff Roberson 	struct thread *td;
1513d7f687fcSJeff Roberson 	struct proc *p;
1514d7f687fcSJeff Roberson 	int error;
1515d7f687fcSJeff Roberson 
15163f289c3fSJeff Roberson 	cpuset_freelist_init(&cpusets, 1);
15173f289c3fSJeff Roberson 	domainset_freelist_init(&domainlist, domain != NULL);
15188bd75bddSJeff Roberson 	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
1519d7f687fcSJeff Roberson 	if (error)
1520d7f687fcSJeff Roberson 		goto out;
1521a03ee000SJeff Roberson 	set = NULL;
1522d7f687fcSJeff Roberson 	thread_lock(td);
15233f289c3fSJeff Roberson 	error = cpuset_shadow(td->td_cpuset, &nset, mask, domain,
15243f289c3fSJeff Roberson 	    &cpusets, &domainlist);
15253f289c3fSJeff Roberson 	if (error == 0)
15263f289c3fSJeff Roberson 		set = cpuset_update_thread(td, nset);
1527d7f687fcSJeff Roberson 	thread_unlock(td);
1528d7f687fcSJeff Roberson 	PROC_UNLOCK(p);
1529a03ee000SJeff Roberson 	if (set)
1530a03ee000SJeff Roberson 		cpuset_rel(set);
1531d7f687fcSJeff Roberson out:
15323f289c3fSJeff Roberson 	cpuset_freelist_free(&cpusets);
15333f289c3fSJeff Roberson 	domainset_freelist_free(&domainlist);
1534d7f687fcSJeff Roberson 	return (error);
1535d7f687fcSJeff Roberson }
1536d7f687fcSJeff Roberson 
1537d7f687fcSJeff Roberson /*
15383f289c3fSJeff Roberson  * Apply an anonymous mask to a single thread.
15393f289c3fSJeff Roberson  */
15403f289c3fSJeff Roberson int
cpuset_setthread(lwpid_t id,cpuset_t * mask)15413f289c3fSJeff Roberson cpuset_setthread(lwpid_t id, cpuset_t *mask)
15423f289c3fSJeff Roberson {
15433f289c3fSJeff Roberson 
15443f289c3fSJeff Roberson 	return _cpuset_setthread(id, mask, NULL);
15453f289c3fSJeff Roberson }
15463f289c3fSJeff Roberson 
15473f289c3fSJeff Roberson /*
154881198539SAlexander V. Chernikov  * Apply new cpumask to the ithread.
154981198539SAlexander V. Chernikov  */
155081198539SAlexander V. Chernikov int
cpuset_setithread(lwpid_t id,int cpu)15517f7528fcSAdrian Chadd cpuset_setithread(lwpid_t id, int cpu)
155281198539SAlexander V. Chernikov {
155381198539SAlexander V. Chernikov 	cpuset_t mask;
155481198539SAlexander V. Chernikov 
155581198539SAlexander V. Chernikov 	CPU_ZERO(&mask);
155681198539SAlexander V. Chernikov 	if (cpu == NOCPU)
155781198539SAlexander V. Chernikov 		CPU_COPY(cpuset_root, &mask);
155881198539SAlexander V. Chernikov 	else
155981198539SAlexander V. Chernikov 		CPU_SET(cpu, &mask);
1560e5818a53SJeff Roberson 	return _cpuset_setthread(id, &mask, NULL);
1561e5818a53SJeff Roberson }
1562c1d9ecf2SAlexander V. Chernikov 
156381198539SAlexander V. Chernikov /*
1564662e7fa8SMark Johnston  * Initialize static domainsets after NUMA information is available.  This is
1565920239efSMark Johnston  * called before memory allocators are initialized.
1566662e7fa8SMark Johnston  */
1567662e7fa8SMark Johnston void
domainset_init(void)1568662e7fa8SMark Johnston domainset_init(void)
1569662e7fa8SMark Johnston {
1570662e7fa8SMark Johnston 	struct domainset *dset;
1571662e7fa8SMark Johnston 	int i;
1572662e7fa8SMark Johnston 
157329bb6c19SMark Johnston 	dset = &domainset_firsttouch;
157429bb6c19SMark Johnston 	DOMAINSET_COPY(&all_domains, &dset->ds_mask);
157529bb6c19SMark Johnston 	dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH;
157629bb6c19SMark Johnston 	dset->ds_prefer = -1;
157729bb6c19SMark Johnston 	_domainset_create(dset, NULL);
157829bb6c19SMark Johnston 
157929bb6c19SMark Johnston 	dset = &domainset_interleave;
158029bb6c19SMark Johnston 	DOMAINSET_COPY(&all_domains, &dset->ds_mask);
158129bb6c19SMark Johnston 	dset->ds_policy = DOMAINSET_POLICY_INTERLEAVE;
158229bb6c19SMark Johnston 	dset->ds_prefer = -1;
158329bb6c19SMark Johnston 	_domainset_create(dset, NULL);
158429bb6c19SMark Johnston 
1585662e7fa8SMark Johnston 	dset = &domainset_roundrobin;
1586662e7fa8SMark Johnston 	DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1587662e7fa8SMark Johnston 	dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
1588662e7fa8SMark Johnston 	dset->ds_prefer = -1;
1589662e7fa8SMark Johnston 	_domainset_create(dset, NULL);
1590662e7fa8SMark Johnston 
1591662e7fa8SMark Johnston 	for (i = 0; i < vm_ndomains; i++) {
15929978bd99SMark Johnston 		dset = &domainset_fixed[i];
15939978bd99SMark Johnston 		DOMAINSET_ZERO(&dset->ds_mask);
15949978bd99SMark Johnston 		DOMAINSET_SET(i, &dset->ds_mask);
15959978bd99SMark Johnston 		dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN;
15969978bd99SMark Johnston 		_domainset_create(dset, NULL);
15979978bd99SMark Johnston 
1598662e7fa8SMark Johnston 		dset = &domainset_prefer[i];
1599662e7fa8SMark Johnston 		DOMAINSET_COPY(&all_domains, &dset->ds_mask);
1600662e7fa8SMark Johnston 		dset->ds_policy = DOMAINSET_POLICY_PREFER;
1601662e7fa8SMark Johnston 		dset->ds_prefer = i;
1602662e7fa8SMark Johnston 		_domainset_create(dset, NULL);
1603662e7fa8SMark Johnston 	}
1604662e7fa8SMark Johnston }
1605662e7fa8SMark Johnston 
1606662e7fa8SMark Johnston /*
160729bb6c19SMark Johnston  * Define the domainsets for cpuset 0, 1 and cpuset 2.
160881198539SAlexander V. Chernikov  */
16093f289c3fSJeff Roberson void
domainset_zero(void)16103f289c3fSJeff Roberson domainset_zero(void)
16113f289c3fSJeff Roberson {
1612920239efSMark Johnston 	struct domainset *dset, *tmp;
16133f289c3fSJeff Roberson 
16143f289c3fSJeff Roberson 	mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
16153f289c3fSJeff Roberson 
161629bb6c19SMark Johnston 	domainset0 = &domainset_firsttouch;
161729bb6c19SMark Johnston 	curthread->td_domain.dr_policy = domainset0;
1618e5818a53SJeff Roberson 
161929bb6c19SMark Johnston 	domainset2 = &domainset_interleave;
162029bb6c19SMark Johnston 	kernel_object->domain.dr_policy = domainset2;
1621662e7fa8SMark Johnston 
1622662e7fa8SMark Johnston 	/* Remove empty domains from the global policies. */
1623920239efSMark Johnston 	LIST_FOREACH_SAFE(dset, &cpuset_domains, ds_link, tmp)
1624920239efSMark Johnston 		if (domainset_empty_vm(dset))
1625920239efSMark Johnston 			LIST_REMOVE(dset, ds_link);
16263f289c3fSJeff Roberson }
162781198539SAlexander V. Chernikov 
162881198539SAlexander V. Chernikov /*
1629e5818a53SJeff Roberson  * Creates system-wide cpusets and the cpuset for thread0 including three
1630c0ae6688SJohn Baldwin  * sets:
1631d7f687fcSJeff Roberson  *
1632d7f687fcSJeff Roberson  * 0 - The root set which should represent all valid processors in the
163330b7c6f9SKyle Evans  *     system.  This set is immutable.
1634d7f687fcSJeff Roberson  * 1 - The default set which all processes are a member of until changed.
1635d7f687fcSJeff Roberson  *     This allows an administrator to move all threads off of given cpus to
1636d7f687fcSJeff Roberson  *     dedicate them to high priority tasks or save power etc.
1637e5818a53SJeff Roberson  * 2 - The kernel set which allows restriction and policy to be applied only
1638e5818a53SJeff Roberson  *     to kernel threads and the kernel_object.
1639d7f687fcSJeff Roberson  */
1640d7f687fcSJeff Roberson struct cpuset *
cpuset_thread0(void)1641d7f687fcSJeff Roberson cpuset_thread0(void)
1642d7f687fcSJeff Roberson {
1643d7f687fcSJeff Roberson 	struct cpuset *set;
164427a3c9d7SJeff Roberson 	int i;
1645a6c7423aSMatt Macy 	int error __unused;
1646d7f687fcSJeff Roberson 
1647d7f687fcSJeff Roberson 	cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
1648e5818a53SJeff Roberson 	    NULL, NULL, UMA_ALIGN_CACHE, 0);
16493f289c3fSJeff Roberson 	domainset_zone = uma_zcreate("domainset", sizeof(struct domainset),
1650e5818a53SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
165181198539SAlexander V. Chernikov 
1652d7f687fcSJeff Roberson 	/*
1653e5818a53SJeff Roberson 	 * Create the root system set (0) for the whole machine.  Doesn't use
1654d7f687fcSJeff Roberson 	 * cpuset_create() due to NULL parent.
1655d7f687fcSJeff Roberson 	 */
1656d7f687fcSJeff Roberson 	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
165727a3c9d7SJeff Roberson 	CPU_COPY(&all_cpus, &set->cs_mask);
1658d7f687fcSJeff Roberson 	LIST_INIT(&set->cs_children);
1659d7f687fcSJeff Roberson 	LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
16601a7bb896SMateusz Guzik 	refcount_init(&set->cs_ref, 1);
166127a3c9d7SJeff Roberson 	set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY;
166229bb6c19SMark Johnston 	set->cs_domain = domainset0;
1663d7f687fcSJeff Roberson 	cpuset_zero = set;
1664a03ee000SJeff Roberson 	cpuset_root = &set->cs_mask;
166581198539SAlexander V. Chernikov 
1666d7f687fcSJeff Roberson 	/*
1667e5818a53SJeff Roberson 	 * Now derive a default (1), modifiable set from that to give out.
1668d7f687fcSJeff Roberson 	 */
16693f289c3fSJeff Roberson 	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
167030b7c6f9SKyle Evans 	error = cpuset_init(set, cpuset_zero, NULL, NULL, 1);
1671d7f687fcSJeff Roberson 	KASSERT(error == 0, ("Error creating default set: %d\n", error));
167281198539SAlexander V. Chernikov 	cpuset_default = set;
1673e5818a53SJeff Roberson 	/*
1674e5818a53SJeff Roberson 	 * Create the kernel set (2).
1675e5818a53SJeff Roberson 	 */
1676e5818a53SJeff Roberson 	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
167730b7c6f9SKyle Evans 	error = cpuset_init(set, cpuset_zero, NULL, NULL, 2);
1678e5818a53SJeff Roberson 	KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
167929bb6c19SMark Johnston 	set->cs_domain = domainset2;
1680e5818a53SJeff Roberson 	cpuset_kernel = set;
168181198539SAlexander V. Chernikov 
1682d7f687fcSJeff Roberson 	/*
1683d7f687fcSJeff Roberson 	 * Initialize the unit allocator. 0 and 1 are allocated above.
1684d7f687fcSJeff Roberson 	 */
1685d57cd5ccSStephen J. Kiernan 	cpuset_unr = new_unrhdr(3, INT_MAX, NULL);
1686d7f687fcSJeff Roberson 
168727a3c9d7SJeff Roberson 	/*
168827a3c9d7SJeff Roberson 	 * If MD code has not initialized per-domain cpusets, place all
168927a3c9d7SJeff Roberson 	 * CPUs in domain 0.
169027a3c9d7SJeff Roberson 	 */
169127a3c9d7SJeff Roberson 	for (i = 0; i < MAXMEMDOM; i++)
169227a3c9d7SJeff Roberson 		if (!CPU_EMPTY(&cpuset_domain[i]))
169327a3c9d7SJeff Roberson 			goto domains_set;
169427a3c9d7SJeff Roberson 	CPU_COPY(&all_cpus, &cpuset_domain[0]);
169527a3c9d7SJeff Roberson domains_set:
169627a3c9d7SJeff Roberson 
1697e5818a53SJeff Roberson 	return (cpuset_default);
1698e5818a53SJeff Roberson }
1699e5818a53SJeff Roberson 
1700e5818a53SJeff Roberson void
cpuset_kernthread(struct thread * td)1701e5818a53SJeff Roberson cpuset_kernthread(struct thread *td)
1702e5818a53SJeff Roberson {
1703e5818a53SJeff Roberson 	struct cpuset *set;
1704e5818a53SJeff Roberson 
1705e5818a53SJeff Roberson 	thread_lock(td);
1706e5818a53SJeff Roberson 	set = td->td_cpuset;
1707e5818a53SJeff Roberson 	td->td_cpuset = cpuset_ref(cpuset_kernel);
1708e5818a53SJeff Roberson 	thread_unlock(td);
1709e5818a53SJeff Roberson 	cpuset_rel(set);
1710d7f687fcSJeff Roberson }
1711d7f687fcSJeff Roberson 
1712d7f687fcSJeff Roberson /*
1713413628a7SBjoern A. Zeeb  * Create a cpuset, which would be cpuset_create() but
1714413628a7SBjoern A. Zeeb  * mark the new 'set' as root.
1715413628a7SBjoern A. Zeeb  *
171647479a8cSBjoern A. Zeeb  * We are not going to reparent the td to it.  Use cpuset_setproc_update_set()
171747479a8cSBjoern A. Zeeb  * for that.
1718413628a7SBjoern A. Zeeb  *
1719413628a7SBjoern A. Zeeb  * In case of no error, returns the set in *setp locked with a reference.
1720413628a7SBjoern A. Zeeb  */
1721413628a7SBjoern A. Zeeb int
cpuset_create_root(struct prison * pr,struct cpuset ** setp)17220304c731SJamie Gritton cpuset_create_root(struct prison *pr, struct cpuset **setp)
1723413628a7SBjoern A. Zeeb {
1724413628a7SBjoern A. Zeeb 	struct cpuset *set;
1725413628a7SBjoern A. Zeeb 	int error;
1726413628a7SBjoern A. Zeeb 
17270304c731SJamie Gritton 	KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
1728413628a7SBjoern A. Zeeb 	KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
1729413628a7SBjoern A. Zeeb 
173029d04ea8SKyle Evans 	set = NULL;
173129d04ea8SKyle Evans 	error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
1732413628a7SBjoern A. Zeeb 	if (error)
1733413628a7SBjoern A. Zeeb 		return (error);
1734413628a7SBjoern A. Zeeb 
173529d04ea8SKyle Evans 	KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data",
1736413628a7SBjoern A. Zeeb 	    __func__, __LINE__));
1737413628a7SBjoern A. Zeeb 
1738413628a7SBjoern A. Zeeb 	/* Mark the set as root. */
1739413628a7SBjoern A. Zeeb 	set->cs_flags |= CPU_SET_ROOT;
174029d04ea8SKyle Evans 	*setp = set;
1741413628a7SBjoern A. Zeeb 
1742413628a7SBjoern A. Zeeb 	return (0);
1743413628a7SBjoern A. Zeeb }
1744413628a7SBjoern A. Zeeb 
1745413628a7SBjoern A. Zeeb int
cpuset_setproc_update_set(struct proc * p,struct cpuset * set)1746413628a7SBjoern A. Zeeb cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
1747413628a7SBjoern A. Zeeb {
1748413628a7SBjoern A. Zeeb 	int error;
1749413628a7SBjoern A. Zeeb 
1750413628a7SBjoern A. Zeeb 	KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
1751413628a7SBjoern A. Zeeb 	KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
1752413628a7SBjoern A. Zeeb 
1753413628a7SBjoern A. Zeeb 	cpuset_ref(set);
1754d431dea5SKyle Evans 	error = cpuset_setproc(p->p_pid, set, NULL, NULL, true);
1755413628a7SBjoern A. Zeeb 	if (error)
1756413628a7SBjoern A. Zeeb 		return (error);
1757413628a7SBjoern A. Zeeb 	cpuset_rel(set);
1758413628a7SBjoern A. Zeeb 	return (0);
1759413628a7SBjoern A. Zeeb }
1760413628a7SBjoern A. Zeeb 
17619eb997cbSMark Johnston /*
17629eb997cbSMark Johnston  * In Capability mode, the only accesses that are permitted are to the current
17639eb997cbSMark Johnston  * thread and process' CPU and domain sets.
17649eb997cbSMark Johnston  */
176596c8b3e5SJake Freeland static bool
cpuset_capmode_allowed(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id)176696c8b3e5SJake Freeland cpuset_capmode_allowed(struct thread *td, cpulevel_t level, cpuwhich_t which,
176796c8b3e5SJake Freeland     id_t id)
176896c8b3e5SJake Freeland {
176996c8b3e5SJake Freeland 	if (level != CPU_LEVEL_WHICH)
177096c8b3e5SJake Freeland 		return (false);
177196c8b3e5SJake Freeland 	if (which != CPU_WHICH_TID && which != CPU_WHICH_PID &&
177296c8b3e5SJake Freeland 	    which != CPU_WHICH_TIDPID)
177396c8b3e5SJake Freeland 		return (false);
177496c8b3e5SJake Freeland 	if (id != -1 && which == CPU_WHICH_TIDPID &&
177596c8b3e5SJake Freeland 	    id != td->td_tid && id != td->td_proc->p_pid)
177696c8b3e5SJake Freeland 		return (false);
177796c8b3e5SJake Freeland 	if (id != -1 &&
177896c8b3e5SJake Freeland 	    !(which == CPU_WHICH_TID && id == td->td_tid) &&
177996c8b3e5SJake Freeland 	    !(which == CPU_WHICH_PID && id == td->td_proc->p_pid))
178096c8b3e5SJake Freeland 		return (false);
178196c8b3e5SJake Freeland 	return (true);
178296c8b3e5SJake Freeland }
178396c8b3e5SJake Freeland 
178496c8b3e5SJake Freeland /*
178596c8b3e5SJake Freeland  * Check for capability violations and record them if ktrace(2) is active.
178696c8b3e5SJake Freeland  */
17879eb997cbSMark Johnston static int
cpuset_check_capabilities(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id)17889eb997cbSMark Johnston cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which,
17899eb997cbSMark Johnston     id_t id)
17909eb997cbSMark Johnston {
179196c8b3e5SJake Freeland 	if (IN_CAPABILITY_MODE(td) || CAP_TRACING(td)) {
179296c8b3e5SJake Freeland 		if (cpuset_capmode_allowed(td, level, which, id))
179396c8b3e5SJake Freeland 			return (0);
179496c8b3e5SJake Freeland 		if (CAP_TRACING(td))
179596c8b3e5SJake Freeland 			ktrcapfail(CAPFAIL_CPUSET, NULL);
179696c8b3e5SJake Freeland 		if (IN_CAPABILITY_MODE(td))
17979eb997cbSMark Johnston 			return (ECAPMODE);
17989eb997cbSMark Johnston 	}
17999eb997cbSMark Johnston 	return (0);
18009eb997cbSMark Johnston }
18019eb997cbSMark Johnston 
1802db79bf75SAlfredo Dal'Ava Junior #if defined(__powerpc__)
1803db79bf75SAlfredo Dal'Ava Junior /*
1804db79bf75SAlfredo Dal'Ava Junior  * TODO: At least powerpc64 and powerpc64le kernels panic with
1805db79bf75SAlfredo Dal'Ava Junior  * exception 0x480 (instruction segment exception) when copyin/copyout,
1806db79bf75SAlfredo Dal'Ava Junior  * are set as a function pointer in cpuset_copy_cb struct and called by
1807db79bf75SAlfredo Dal'Ava Junior  * an external module (like pfsync). Tip: copyin/copyout have an ifunc
1808db79bf75SAlfredo Dal'Ava Junior  * resolver function.
1809db79bf75SAlfredo Dal'Ava Junior  *
1810db79bf75SAlfredo Dal'Ava Junior  * Bisect of LLVM shows that the behavior changed on LLVM 10.0 with
1811db79bf75SAlfredo Dal'Ava Junior  * https://reviews.llvm.org/rGdc06b0bc9ad055d06535462d91bfc2a744b2f589
1812db79bf75SAlfredo Dal'Ava Junior  *
1813db79bf75SAlfredo Dal'Ava Junior  * This is a hack/workaround while problem is being discussed with LLVM
1814db79bf75SAlfredo Dal'Ava Junior  * community
1815db79bf75SAlfredo Dal'Ava Junior  */
1816db79bf75SAlfredo Dal'Ava Junior static int
cpuset_copyin(const void * uaddr,void * kaddr,size_t len)1817db79bf75SAlfredo Dal'Ava Junior cpuset_copyin(const void *uaddr, void *kaddr, size_t len)
1818db79bf75SAlfredo Dal'Ava Junior {
1819db79bf75SAlfredo Dal'Ava Junior 	return(copyin(uaddr, kaddr, len));
1820db79bf75SAlfredo Dal'Ava Junior }
1821db79bf75SAlfredo Dal'Ava Junior 
1822db79bf75SAlfredo Dal'Ava Junior static int
cpuset_copyout(const void * kaddr,void * uaddr,size_t len)1823db79bf75SAlfredo Dal'Ava Junior cpuset_copyout(const void *kaddr, void *uaddr, size_t len)
1824db79bf75SAlfredo Dal'Ava Junior {
1825db79bf75SAlfredo Dal'Ava Junior 	return(copyout(kaddr, uaddr, len));
1826db79bf75SAlfredo Dal'Ava Junior }
1827db79bf75SAlfredo Dal'Ava Junior 
1828db79bf75SAlfredo Dal'Ava Junior static const struct cpuset_copy_cb copy_set = {
1829db79bf75SAlfredo Dal'Ava Junior 	.cpuset_copyin = cpuset_copyin,
1830db79bf75SAlfredo Dal'Ava Junior 	.cpuset_copyout = cpuset_copyout
1831db79bf75SAlfredo Dal'Ava Junior };
1832db79bf75SAlfredo Dal'Ava Junior #else
183347a57144SJustin Hibbits static const struct cpuset_copy_cb copy_set = {
18344a3e5133SMark Johnston         .cpuset_copyin = copyin,
18354a3e5133SMark Johnston         .cpuset_copyout = copyout
183647a57144SJustin Hibbits };
1837db79bf75SAlfredo Dal'Ava Junior #endif
183847a57144SJustin Hibbits 
1839d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1840d7f687fcSJeff Roberson struct cpuset_args {
1841d7f687fcSJeff Roberson 	cpusetid_t	*setid;
1842d7f687fcSJeff Roberson };
1843d7f687fcSJeff Roberson #endif
1844d7f687fcSJeff Roberson int
sys_cpuset(struct thread * td,struct cpuset_args * uap)18458451d0ddSKip Macy sys_cpuset(struct thread *td, struct cpuset_args *uap)
1846d7f687fcSJeff Roberson {
1847d7f687fcSJeff Roberson 	struct cpuset *root;
1848d7f687fcSJeff Roberson 	struct cpuset *set;
1849d7f687fcSJeff Roberson 	int error;
1850d7f687fcSJeff Roberson 
1851d7f687fcSJeff Roberson 	thread_lock(td);
1852a03ee000SJeff Roberson 	root = cpuset_refroot(td->td_cpuset);
1853d7f687fcSJeff Roberson 	thread_unlock(td);
185429d04ea8SKyle Evans 	set = NULL;
1855d7f687fcSJeff Roberson 	error = cpuset_create(&set, root, &root->cs_mask);
1856d7f687fcSJeff Roberson 	cpuset_rel(root);
1857d7f687fcSJeff Roberson 	if (error)
1858d7f687fcSJeff Roberson 		return (error);
1859d7f687fcSJeff Roberson 	error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
1860a03ee000SJeff Roberson 	if (error == 0)
1861d431dea5SKyle Evans 		error = cpuset_setproc(-1, set, NULL, NULL, false);
1862d7f687fcSJeff Roberson 	cpuset_rel(set);
1863d7f687fcSJeff Roberson 	return (error);
1864d7f687fcSJeff Roberson }
1865d7f687fcSJeff Roberson 
1866d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1867d7f687fcSJeff Roberson struct cpuset_setid_args {
1868d7f687fcSJeff Roberson 	cpuwhich_t	which;
1869d7f687fcSJeff Roberson 	id_t		id;
1870d7f687fcSJeff Roberson 	cpusetid_t	setid;
1871d7f687fcSJeff Roberson };
1872d7f687fcSJeff Roberson #endif
1873d7f687fcSJeff Roberson int
sys_cpuset_setid(struct thread * td,struct cpuset_setid_args * uap)18748451d0ddSKip Macy sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
1875d7f687fcSJeff Roberson {
1876ea2ebdc1SEdward Tomasz Napierala 
1877ea2ebdc1SEdward Tomasz Napierala 	return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
1878ea2ebdc1SEdward Tomasz Napierala }
1879ea2ebdc1SEdward Tomasz Napierala 
1880ea2ebdc1SEdward Tomasz Napierala int
kern_cpuset_setid(struct thread * td,cpuwhich_t which,id_t id,cpusetid_t setid)1881ea2ebdc1SEdward Tomasz Napierala kern_cpuset_setid(struct thread *td, cpuwhich_t which,
1882ea2ebdc1SEdward Tomasz Napierala     id_t id, cpusetid_t setid)
1883ea2ebdc1SEdward Tomasz Napierala {
1884d7f687fcSJeff Roberson 	struct cpuset *set;
1885d7f687fcSJeff Roberson 	int error;
1886d7f687fcSJeff Roberson 
1887d7f687fcSJeff Roberson 	/*
1888d7f687fcSJeff Roberson 	 * Presently we only support per-process sets.
1889d7f687fcSJeff Roberson 	 */
1890ea2ebdc1SEdward Tomasz Napierala 	if (which != CPU_WHICH_PID)
1891d7f687fcSJeff Roberson 		return (EINVAL);
1892ea2ebdc1SEdward Tomasz Napierala 	set = cpuset_lookup(setid, td);
1893d7f687fcSJeff Roberson 	if (set == NULL)
1894d7f687fcSJeff Roberson 		return (ESRCH);
1895d431dea5SKyle Evans 	error = cpuset_setproc(id, set, NULL, NULL, false);
1896d7f687fcSJeff Roberson 	cpuset_rel(set);
1897d7f687fcSJeff Roberson 	return (error);
1898d7f687fcSJeff Roberson }
1899d7f687fcSJeff Roberson 
1900d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1901d7f687fcSJeff Roberson struct cpuset_getid_args {
1902d7f687fcSJeff Roberson 	cpulevel_t	level;
1903d7f687fcSJeff Roberson 	cpuwhich_t	which;
1904d7f687fcSJeff Roberson 	id_t		id;
1905d7f687fcSJeff Roberson 	cpusetid_t	*setid;
19062b69bb1fSKevin Lo };
1907d7f687fcSJeff Roberson #endif
1908d7f687fcSJeff Roberson int
sys_cpuset_getid(struct thread * td,struct cpuset_getid_args * uap)19098451d0ddSKip Macy sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1910d7f687fcSJeff Roberson {
1911ea2ebdc1SEdward Tomasz Napierala 
1912ea2ebdc1SEdward Tomasz Napierala 	return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1913ea2ebdc1SEdward Tomasz Napierala 	    uap->setid));
1914ea2ebdc1SEdward Tomasz Napierala }
1915ea2ebdc1SEdward Tomasz Napierala 
1916ea2ebdc1SEdward Tomasz Napierala int
kern_cpuset_getid(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,cpusetid_t * setid)1917ea2ebdc1SEdward Tomasz Napierala kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1918ea2ebdc1SEdward Tomasz Napierala     id_t id, cpusetid_t *setid)
1919ea2ebdc1SEdward Tomasz Napierala {
1920d7f687fcSJeff Roberson 	struct cpuset *nset;
1921d7f687fcSJeff Roberson 	struct cpuset *set;
1922d7f687fcSJeff Roberson 	struct thread *ttd;
1923d7f687fcSJeff Roberson 	struct proc *p;
1924ea2ebdc1SEdward Tomasz Napierala 	cpusetid_t tmpid;
1925d7f687fcSJeff Roberson 	int error;
1926d7f687fcSJeff Roberson 
1927ea2ebdc1SEdward Tomasz Napierala 	if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1928d7f687fcSJeff Roberson 		return (EINVAL);
1929ea2ebdc1SEdward Tomasz Napierala 	error = cpuset_which(which, id, &p, &ttd, &set);
1930d7f687fcSJeff Roberson 	if (error)
1931d7f687fcSJeff Roberson 		return (error);
1932ea2ebdc1SEdward Tomasz Napierala 	switch (which) {
1933d7f687fcSJeff Roberson 	case CPU_WHICH_TID:
1934d7f687fcSJeff Roberson 	case CPU_WHICH_PID:
19352058f075SDmitry Chagin 	case CPU_WHICH_TIDPID:
1936d7f687fcSJeff Roberson 		thread_lock(ttd);
1937a03ee000SJeff Roberson 		set = cpuset_refbase(ttd->td_cpuset);
1938d7f687fcSJeff Roberson 		thread_unlock(ttd);
1939d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
1940d7f687fcSJeff Roberson 		break;
1941d7f687fcSJeff Roberson 	case CPU_WHICH_CPUSET:
1942413628a7SBjoern A. Zeeb 	case CPU_WHICH_JAIL:
1943d7f687fcSJeff Roberson 		break;
19449b33b154SJeff Roberson 	case CPU_WHICH_IRQ:
1945c0ae6688SJohn Baldwin 	case CPU_WHICH_DOMAIN:
19469b33b154SJeff Roberson 		return (EINVAL);
1947d7f687fcSJeff Roberson 	}
1948ea2ebdc1SEdward Tomasz Napierala 	switch (level) {
1949d7f687fcSJeff Roberson 	case CPU_LEVEL_ROOT:
1950a03ee000SJeff Roberson 		nset = cpuset_refroot(set);
1951d7f687fcSJeff Roberson 		cpuset_rel(set);
1952d7f687fcSJeff Roberson 		set = nset;
1953d7f687fcSJeff Roberson 		break;
1954d7f687fcSJeff Roberson 	case CPU_LEVEL_CPUSET:
1955d7f687fcSJeff Roberson 		break;
1956d7f687fcSJeff Roberson 	case CPU_LEVEL_WHICH:
1957d7f687fcSJeff Roberson 		break;
1958d7f687fcSJeff Roberson 	}
1959ea2ebdc1SEdward Tomasz Napierala 	tmpid = set->cs_id;
1960d7f687fcSJeff Roberson 	cpuset_rel(set);
1961d7f687fcSJeff Roberson 	if (error == 0)
1962a1d0659cSJung-uk Kim 		error = copyout(&tmpid, setid, sizeof(tmpid));
1963d7f687fcSJeff Roberson 
1964d7f687fcSJeff Roberson 	return (error);
1965d7f687fcSJeff Roberson }
1966d7f687fcSJeff Roberson 
1967d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1968d7f687fcSJeff Roberson struct cpuset_getaffinity_args {
1969d7f687fcSJeff Roberson 	cpulevel_t	level;
1970d7f687fcSJeff Roberson 	cpuwhich_t	which;
19717f64829aSRuslan Ermilov 	id_t		id;
19727f64829aSRuslan Ermilov 	size_t		cpusetsize;
19737f64829aSRuslan Ermilov 	cpuset_t	*mask;
1974d7f687fcSJeff Roberson };
1975d7f687fcSJeff Roberson #endif
1976d7f687fcSJeff Roberson int
sys_cpuset_getaffinity(struct thread * td,struct cpuset_getaffinity_args * uap)19778451d0ddSKip Macy sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1978d7f687fcSJeff Roberson {
197996ee4310SEdward Tomasz Napierala 
1980d46174cdSDmitry Chagin 	return (user_cpuset_getaffinity(td, uap->level, uap->which,
198147a57144SJustin Hibbits 	    uap->id, uap->cpusetsize, uap->mask, &copy_set));
198296ee4310SEdward Tomasz Napierala }
198396ee4310SEdward Tomasz Napierala 
198496ee4310SEdward Tomasz Napierala int
kern_cpuset_getaffinity(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,size_t cpusetsize,cpuset_t * mask)198596ee4310SEdward Tomasz Napierala kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
1986d46174cdSDmitry Chagin     id_t id, size_t cpusetsize, cpuset_t *mask)
198796ee4310SEdward Tomasz Napierala {
1988d7f687fcSJeff Roberson 	struct thread *ttd;
1989d7f687fcSJeff Roberson 	struct cpuset *nset;
1990d7f687fcSJeff Roberson 	struct cpuset *set;
1991d7f687fcSJeff Roberson 	struct proc *p;
1992d7f687fcSJeff Roberson 	int error;
1993d7f687fcSJeff Roberson 
19949eb997cbSMark Johnston 	error = cpuset_check_capabilities(td, level, which, id);
19959eb997cbSMark Johnston 	if (error != 0)
19969eb997cbSMark Johnston 		return (error);
19972058f075SDmitry Chagin 	error = cpuset_which2(&which, id, &p, &ttd, &set);
1998d46174cdSDmitry Chagin 	if (error != 0)
1999d46174cdSDmitry Chagin 		return (error);
200096ee4310SEdward Tomasz Napierala 	switch (level) {
2001d7f687fcSJeff Roberson 	case CPU_LEVEL_ROOT:
2002d7f687fcSJeff Roberson 	case CPU_LEVEL_CPUSET:
200396ee4310SEdward Tomasz Napierala 		switch (which) {
2004d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
2005d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
2006d7f687fcSJeff Roberson 			thread_lock(ttd);
2007d7f687fcSJeff Roberson 			set = cpuset_ref(ttd->td_cpuset);
2008d7f687fcSJeff Roberson 			thread_unlock(ttd);
2009d7f687fcSJeff Roberson 			break;
2010d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
2011413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
2012d7f687fcSJeff Roberson 			break;
20139b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
201429dfb631SConrad Meyer 		case CPU_WHICH_INTRHANDLER:
201529dfb631SConrad Meyer 		case CPU_WHICH_ITHREAD:
2016c0ae6688SJohn Baldwin 		case CPU_WHICH_DOMAIN:
2017d46174cdSDmitry Chagin 			return (EINVAL);
2018d7f687fcSJeff Roberson 		}
201996ee4310SEdward Tomasz Napierala 		if (level == CPU_LEVEL_ROOT)
2020a03ee000SJeff Roberson 			nset = cpuset_refroot(set);
2021d7f687fcSJeff Roberson 		else
2022a03ee000SJeff Roberson 			nset = cpuset_refbase(set);
2023d7f687fcSJeff Roberson 		CPU_COPY(&nset->cs_mask, mask);
2024d7f687fcSJeff Roberson 		cpuset_rel(nset);
2025d7f687fcSJeff Roberson 		break;
2026d7f687fcSJeff Roberson 	case CPU_LEVEL_WHICH:
202796ee4310SEdward Tomasz Napierala 		switch (which) {
2028d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
2029d7f687fcSJeff Roberson 			thread_lock(ttd);
2030d7f687fcSJeff Roberson 			CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
2031d7f687fcSJeff Roberson 			thread_unlock(ttd);
2032d7f687fcSJeff Roberson 			break;
2033d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
2034d7f687fcSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, ttd) {
2035d7f687fcSJeff Roberson 				thread_lock(ttd);
2036e2650af1SStefan Eßer 				CPU_OR(mask, mask, &ttd->td_cpuset->cs_mask);
2037d7f687fcSJeff Roberson 				thread_unlock(ttd);
2038d7f687fcSJeff Roberson 			}
2039d7f687fcSJeff Roberson 			break;
2040d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
2041413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
2042d7f687fcSJeff Roberson 			CPU_COPY(&set->cs_mask, mask);
2043d7f687fcSJeff Roberson 			break;
20449b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
204529dfb631SConrad Meyer 		case CPU_WHICH_INTRHANDLER:
204629dfb631SConrad Meyer 		case CPU_WHICH_ITHREAD:
204729dfb631SConrad Meyer 			error = intr_getaffinity(id, which, mask);
20489b33b154SJeff Roberson 			break;
2049c0ae6688SJohn Baldwin 		case CPU_WHICH_DOMAIN:
205096ee4310SEdward Tomasz Napierala 			if (id < 0 || id >= MAXMEMDOM)
2051c0ae6688SJohn Baldwin 				error = ESRCH;
2052c0ae6688SJohn Baldwin 			else
205396ee4310SEdward Tomasz Napierala 				CPU_COPY(&cpuset_domain[id], mask);
2054c0ae6688SJohn Baldwin 			break;
2055d7f687fcSJeff Roberson 		}
2056d7f687fcSJeff Roberson 		break;
2057d7f687fcSJeff Roberson 	default:
2058d7f687fcSJeff Roberson 		error = EINVAL;
2059d7f687fcSJeff Roberson 		break;
2060d7f687fcSJeff Roberson 	}
2061d7f687fcSJeff Roberson 	if (set)
2062d7f687fcSJeff Roberson 		cpuset_rel(set);
2063d7f687fcSJeff Roberson 	if (p)
2064d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
2065f35093f8SDmitry Chagin 	if (error == 0) {
2066d46174cdSDmitry Chagin 		if (cpusetsize < howmany(CPU_FLS(mask), NBBY))
2067d46174cdSDmitry Chagin 			return (ERANGE);
2068d46174cdSDmitry Chagin #ifdef KTRACE
2069d46174cdSDmitry Chagin 		if (KTRPOINT(td, KTR_STRUCT))
2070d46174cdSDmitry Chagin 			ktrcpuset(mask, cpusetsize);
2071d46174cdSDmitry Chagin #endif
2072f35093f8SDmitry Chagin 	}
2073d46174cdSDmitry Chagin 	return (error);
2074d46174cdSDmitry Chagin }
2075d46174cdSDmitry Chagin 
2076d46174cdSDmitry Chagin int
user_cpuset_getaffinity(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,size_t cpusetsize,cpuset_t * maskp,const struct cpuset_copy_cb * cb)2077d46174cdSDmitry Chagin user_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2078d46174cdSDmitry Chagin     id_t id, size_t cpusetsize, cpuset_t *maskp, const struct cpuset_copy_cb *cb)
2079d46174cdSDmitry Chagin {
2080d46174cdSDmitry Chagin 	cpuset_t *mask;
2081d46174cdSDmitry Chagin 	size_t size;
2082d46174cdSDmitry Chagin 	int error;
2083d46174cdSDmitry Chagin 
2084d46174cdSDmitry Chagin 	mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
2085f35093f8SDmitry Chagin 	size = min(cpusetsize, sizeof(cpuset_t));
2086d46174cdSDmitry Chagin 	error = kern_cpuset_getaffinity(td, level, which, id, size, mask);
2087d46174cdSDmitry Chagin 	if (error == 0) {
20884a3e5133SMark Johnston 		error = cb->cpuset_copyout(mask, maskp, size);
2089f35093f8SDmitry Chagin 		if (error != 0)
2090f35093f8SDmitry Chagin 			goto out;
2091f35093f8SDmitry Chagin 		if (cpusetsize > size) {
2092f35093f8SDmitry Chagin 			char *end;
2093f35093f8SDmitry Chagin 			char *cp;
2094f35093f8SDmitry Chagin 			int rv;
2095f35093f8SDmitry Chagin 
2096f35093f8SDmitry Chagin 			end = cp = (char *)&maskp->__bits;
2097f35093f8SDmitry Chagin 			end += cpusetsize;
2098f35093f8SDmitry Chagin 			cp += size;
2099f35093f8SDmitry Chagin 			while (cp != end) {
2100f35093f8SDmitry Chagin 				rv = subyte(cp, 0);
2101f35093f8SDmitry Chagin 				if (rv == -1) {
2102f35093f8SDmitry Chagin 					error = EFAULT;
2103f35093f8SDmitry Chagin 					goto out;
2104f35093f8SDmitry Chagin 				}
2105f35093f8SDmitry Chagin 				cp++;
2106f35093f8SDmitry Chagin 			}
2107f35093f8SDmitry Chagin 		}
2108f35093f8SDmitry Chagin 	}
2109d7f687fcSJeff Roberson out:
2110d7f687fcSJeff Roberson 	free(mask, M_TEMP);
2111d7f687fcSJeff Roberson 	return (error);
2112d7f687fcSJeff Roberson }
2113d7f687fcSJeff Roberson 
2114d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
2115d7f687fcSJeff Roberson struct cpuset_setaffinity_args {
2116d7f687fcSJeff Roberson 	cpulevel_t	level;
2117d7f687fcSJeff Roberson 	cpuwhich_t	which;
21187f64829aSRuslan Ermilov 	id_t		id;
21197f64829aSRuslan Ermilov 	size_t		cpusetsize;
21207f64829aSRuslan Ermilov 	const cpuset_t	*mask;
2121d7f687fcSJeff Roberson };
2122d7f687fcSJeff Roberson #endif
2123d7f687fcSJeff Roberson int
sys_cpuset_setaffinity(struct thread * td,struct cpuset_setaffinity_args * uap)21248451d0ddSKip Macy sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
2125d7f687fcSJeff Roberson {
212696ee4310SEdward Tomasz Napierala 
2127f35093f8SDmitry Chagin 	return (user_cpuset_setaffinity(td, uap->level, uap->which,
212847a57144SJustin Hibbits 	    uap->id, uap->cpusetsize, uap->mask, &copy_set));
212996ee4310SEdward Tomasz Napierala }
213096ee4310SEdward Tomasz Napierala 
213196ee4310SEdward Tomasz Napierala int
kern_cpuset_setaffinity(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,cpuset_t * mask)213296ee4310SEdward Tomasz Napierala kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
2133f35093f8SDmitry Chagin     id_t id, cpuset_t *mask)
213496ee4310SEdward Tomasz Napierala {
2135d7f687fcSJeff Roberson 	struct cpuset *nset;
2136d7f687fcSJeff Roberson 	struct cpuset *set;
2137d7f687fcSJeff Roberson 	struct thread *ttd;
2138d7f687fcSJeff Roberson 	struct proc *p;
2139d7f687fcSJeff Roberson 	int error;
2140d7f687fcSJeff Roberson 
2141586ed321SDmitry Chagin #ifdef KTRACE
2142586ed321SDmitry Chagin 	if (KTRPOINT(td, KTR_STRUCT))
2143586ed321SDmitry Chagin 		ktrcpuset(mask, sizeof(cpuset_t));
2144586ed321SDmitry Chagin #endif
21459eb997cbSMark Johnston 	error = cpuset_check_capabilities(td, level, which, id);
21469eb997cbSMark Johnston 	if (error != 0)
21479eb997cbSMark Johnston 		return (error);
2148f35093f8SDmitry Chagin 	if (CPU_EMPTY(mask))
2149f35093f8SDmitry Chagin 		return (EDEADLK);
215096ee4310SEdward Tomasz Napierala 	switch (level) {
2151d7f687fcSJeff Roberson 	case CPU_LEVEL_ROOT:
2152d7f687fcSJeff Roberson 	case CPU_LEVEL_CPUSET:
215396ee4310SEdward Tomasz Napierala 		error = cpuset_which(which, id, &p, &ttd, &set);
2154d7f687fcSJeff Roberson 		if (error)
2155d7f687fcSJeff Roberson 			break;
215696ee4310SEdward Tomasz Napierala 		switch (which) {
2157d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
2158d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
21592058f075SDmitry Chagin 		case CPU_WHICH_TIDPID:
2160d7f687fcSJeff Roberson 			thread_lock(ttd);
2161d7f687fcSJeff Roberson 			set = cpuset_ref(ttd->td_cpuset);
2162d7f687fcSJeff Roberson 			thread_unlock(ttd);
2163c6440f72SJeff Roberson 			PROC_UNLOCK(p);
2164d7f687fcSJeff Roberson 			break;
2165d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
2166413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
2167d7f687fcSJeff Roberson 			break;
21689b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
216929dfb631SConrad Meyer 		case CPU_WHICH_INTRHANDLER:
217029dfb631SConrad Meyer 		case CPU_WHICH_ITHREAD:
2171c0ae6688SJohn Baldwin 		case CPU_WHICH_DOMAIN:
2172f35093f8SDmitry Chagin 			return (EINVAL);
2173d7f687fcSJeff Roberson 		}
217496ee4310SEdward Tomasz Napierala 		if (level == CPU_LEVEL_ROOT)
2175a03ee000SJeff Roberson 			nset = cpuset_refroot(set);
2176d7f687fcSJeff Roberson 		else
2177a03ee000SJeff Roberson 			nset = cpuset_refbase(set);
2178d7f687fcSJeff Roberson 		error = cpuset_modify(nset, mask);
2179d7f687fcSJeff Roberson 		cpuset_rel(nset);
2180d7f687fcSJeff Roberson 		cpuset_rel(set);
2181d7f687fcSJeff Roberson 		break;
2182d7f687fcSJeff Roberson 	case CPU_LEVEL_WHICH:
218396ee4310SEdward Tomasz Napierala 		switch (which) {
2184d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
218596ee4310SEdward Tomasz Napierala 			error = cpuset_setthread(id, mask);
2186d7f687fcSJeff Roberson 			break;
2187d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
2188d431dea5SKyle Evans 			error = cpuset_setproc(id, NULL, mask, NULL, false);
2189d7f687fcSJeff Roberson 			break;
2190c21b080fSDmitry Chagin 		case CPU_WHICH_TIDPID:
2191c21b080fSDmitry Chagin 			if (id > PID_MAX || id == -1)
2192c21b080fSDmitry Chagin 				error = cpuset_setthread(id, mask);
2193c21b080fSDmitry Chagin 			else
2194c21b080fSDmitry Chagin 				error = cpuset_setproc(id, NULL, mask, NULL,
2195c21b080fSDmitry Chagin 				    false);
2196c21b080fSDmitry Chagin 			break;
2197d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
2198413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
219996ee4310SEdward Tomasz Napierala 			error = cpuset_which(which, id, &p, &ttd, &set);
2200d7f687fcSJeff Roberson 			if (error == 0) {
2201d7f687fcSJeff Roberson 				error = cpuset_modify(set, mask);
2202d7f687fcSJeff Roberson 				cpuset_rel(set);
2203d7f687fcSJeff Roberson 			}
2204d7f687fcSJeff Roberson 			break;
22059b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
220629dfb631SConrad Meyer 		case CPU_WHICH_INTRHANDLER:
220729dfb631SConrad Meyer 		case CPU_WHICH_ITHREAD:
220829dfb631SConrad Meyer 			error = intr_setaffinity(id, which, mask);
22099b33b154SJeff Roberson 			break;
2210d7f687fcSJeff Roberson 		default:
2211d7f687fcSJeff Roberson 			error = EINVAL;
2212d7f687fcSJeff Roberson 			break;
2213d7f687fcSJeff Roberson 		}
2214d7f687fcSJeff Roberson 		break;
2215d7f687fcSJeff Roberson 	default:
2216d7f687fcSJeff Roberson 		error = EINVAL;
2217d7f687fcSJeff Roberson 		break;
2218d7f687fcSJeff Roberson 	}
2219f35093f8SDmitry Chagin 	return (error);
2220f35093f8SDmitry Chagin }
2221f35093f8SDmitry Chagin 
2222f35093f8SDmitry Chagin int
user_cpuset_setaffinity(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,size_t cpusetsize,const cpuset_t * maskp,const struct cpuset_copy_cb * cb)2223f35093f8SDmitry Chagin user_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which,
222447a57144SJustin Hibbits     id_t id, size_t cpusetsize, const cpuset_t *maskp, const struct cpuset_copy_cb *cb)
2225f35093f8SDmitry Chagin {
2226f35093f8SDmitry Chagin 	cpuset_t *mask;
2227f35093f8SDmitry Chagin 	int error;
2228f35093f8SDmitry Chagin 	size_t size;
2229f35093f8SDmitry Chagin 
2230f35093f8SDmitry Chagin 	size = min(cpusetsize, sizeof(cpuset_t));
2231f35093f8SDmitry Chagin 	mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);
22324a3e5133SMark Johnston 	error = cb->cpuset_copyin(maskp, mask, size);
2233f35093f8SDmitry Chagin 	if (error)
2234f35093f8SDmitry Chagin 		goto out;
2235f35093f8SDmitry Chagin 	/*
2236f35093f8SDmitry Chagin 	 * Verify that no high bits are set.
2237f35093f8SDmitry Chagin 	 */
2238f35093f8SDmitry Chagin 	if (cpusetsize > sizeof(cpuset_t)) {
2239f35093f8SDmitry Chagin 		const char *end, *cp;
2240f35093f8SDmitry Chagin 		int val;
2241f35093f8SDmitry Chagin 		end = cp = (const char *)&maskp->__bits;
2242f35093f8SDmitry Chagin 		end += cpusetsize;
2243f35093f8SDmitry Chagin 		cp += sizeof(cpuset_t);
2244f35093f8SDmitry Chagin 
2245f35093f8SDmitry Chagin 		while (cp != end) {
2246f35093f8SDmitry Chagin 			val = fubyte(cp);
2247f35093f8SDmitry Chagin 			if (val == -1) {
2248f35093f8SDmitry Chagin 				error = EFAULT;
2249f35093f8SDmitry Chagin 				goto out;
2250f35093f8SDmitry Chagin 			}
2251f35093f8SDmitry Chagin 			if (val != 0) {
2252f35093f8SDmitry Chagin 				error = EINVAL;
2253f35093f8SDmitry Chagin 				goto out;
2254f35093f8SDmitry Chagin 			}
2255f35093f8SDmitry Chagin 			cp++;
2256f35093f8SDmitry Chagin 		}
2257f35093f8SDmitry Chagin 	}
2258f35093f8SDmitry Chagin 	error = kern_cpuset_setaffinity(td, level, which, id, mask);
2259f35093f8SDmitry Chagin 
2260d7f687fcSJeff Roberson out:
2261d7f687fcSJeff Roberson 	free(mask, M_TEMP);
2262d7f687fcSJeff Roberson 	return (error);
2263d7f687fcSJeff Roberson }
2264dea0ed66SBjoern A. Zeeb 
22653f289c3fSJeff Roberson #ifndef _SYS_SYSPROTO_H_
22663f289c3fSJeff Roberson struct cpuset_getdomain_args {
22673f289c3fSJeff Roberson 	cpulevel_t	level;
22683f289c3fSJeff Roberson 	cpuwhich_t	which;
22693f289c3fSJeff Roberson 	id_t		id;
22703f289c3fSJeff Roberson 	size_t		domainsetsize;
22713f289c3fSJeff Roberson 	domainset_t	*mask;
22723f289c3fSJeff Roberson 	int 		*policy;
22733f289c3fSJeff Roberson };
22743f289c3fSJeff Roberson #endif
22753f289c3fSJeff Roberson int
sys_cpuset_getdomain(struct thread * td,struct cpuset_getdomain_args * uap)22763f289c3fSJeff Roberson sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap)
2277dea0ed66SBjoern A. Zeeb {
2278dea0ed66SBjoern A. Zeeb 
22793f289c3fSJeff Roberson 	return (kern_cpuset_getdomain(td, uap->level, uap->which,
228047a57144SJustin Hibbits 	    uap->id, uap->domainsetsize, uap->mask, uap->policy, &copy_set));
22813f289c3fSJeff Roberson }
22823f289c3fSJeff Roberson 
22833f289c3fSJeff Roberson int
kern_cpuset_getdomain(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,size_t domainsetsize,domainset_t * maskp,int * policyp,const struct cpuset_copy_cb * cb)22843f289c3fSJeff Roberson kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
228547a57144SJustin Hibbits     id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp,
228647a57144SJustin Hibbits     const struct cpuset_copy_cb *cb)
22873f289c3fSJeff Roberson {
22883f289c3fSJeff Roberson 	struct domainset outset;
22893f289c3fSJeff Roberson 	struct thread *ttd;
22903f289c3fSJeff Roberson 	struct cpuset *nset;
22913f289c3fSJeff Roberson 	struct cpuset *set;
22923f289c3fSJeff Roberson 	struct domainset *dset;
22933f289c3fSJeff Roberson 	struct proc *p;
22943f289c3fSJeff Roberson 	domainset_t *mask;
22953f289c3fSJeff Roberson 	int error;
22963f289c3fSJeff Roberson 
22973f289c3fSJeff Roberson 	if (domainsetsize < sizeof(domainset_t) ||
22983f289c3fSJeff Roberson 	    domainsetsize > DOMAINSET_MAXSIZE / NBBY)
22993f289c3fSJeff Roberson 		return (ERANGE);
23009eb997cbSMark Johnston 	error = cpuset_check_capabilities(td, level, which, id);
23019eb997cbSMark Johnston 	if (error != 0)
23029eb997cbSMark Johnston 		return (error);
23033f289c3fSJeff Roberson 	mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
23043f289c3fSJeff Roberson 	bzero(&outset, sizeof(outset));
23052058f075SDmitry Chagin 	error = cpuset_which2(&which, id, &p, &ttd, &set);
23063f289c3fSJeff Roberson 	if (error)
23073f289c3fSJeff Roberson 		goto out;
23083f289c3fSJeff Roberson 	switch (level) {
23093f289c3fSJeff Roberson 	case CPU_LEVEL_ROOT:
23103f289c3fSJeff Roberson 	case CPU_LEVEL_CPUSET:
23113f289c3fSJeff Roberson 		switch (which) {
23123f289c3fSJeff Roberson 		case CPU_WHICH_TID:
23133f289c3fSJeff Roberson 		case CPU_WHICH_PID:
23143f289c3fSJeff Roberson 			thread_lock(ttd);
23153f289c3fSJeff Roberson 			set = cpuset_ref(ttd->td_cpuset);
23163f289c3fSJeff Roberson 			thread_unlock(ttd);
23173f289c3fSJeff Roberson 			break;
23183f289c3fSJeff Roberson 		case CPU_WHICH_CPUSET:
23193f289c3fSJeff Roberson 		case CPU_WHICH_JAIL:
23203f289c3fSJeff Roberson 			break;
23213f289c3fSJeff Roberson 		case CPU_WHICH_IRQ:
23223f289c3fSJeff Roberson 		case CPU_WHICH_INTRHANDLER:
23233f289c3fSJeff Roberson 		case CPU_WHICH_ITHREAD:
23243f289c3fSJeff Roberson 		case CPU_WHICH_DOMAIN:
23253f289c3fSJeff Roberson 			error = EINVAL;
23263f289c3fSJeff Roberson 			goto out;
23273f289c3fSJeff Roberson 		}
23283f289c3fSJeff Roberson 		if (level == CPU_LEVEL_ROOT)
23293f289c3fSJeff Roberson 			nset = cpuset_refroot(set);
23303f289c3fSJeff Roberson 		else
23313f289c3fSJeff Roberson 			nset = cpuset_refbase(set);
23323f289c3fSJeff Roberson 		domainset_copy(nset->cs_domain, &outset);
23333f289c3fSJeff Roberson 		cpuset_rel(nset);
23343f289c3fSJeff Roberson 		break;
23353f289c3fSJeff Roberson 	case CPU_LEVEL_WHICH:
23363f289c3fSJeff Roberson 		switch (which) {
23373f289c3fSJeff Roberson 		case CPU_WHICH_TID:
23383f289c3fSJeff Roberson 			thread_lock(ttd);
23393f289c3fSJeff Roberson 			domainset_copy(ttd->td_cpuset->cs_domain, &outset);
23403f289c3fSJeff Roberson 			thread_unlock(ttd);
23413f289c3fSJeff Roberson 			break;
23423f289c3fSJeff Roberson 		case CPU_WHICH_PID:
23433f289c3fSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, ttd) {
23443f289c3fSJeff Roberson 				thread_lock(ttd);
23453f289c3fSJeff Roberson 				dset = ttd->td_cpuset->cs_domain;
23463f289c3fSJeff Roberson 				/* Show all domains in the proc. */
23473f289c3fSJeff Roberson 				DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask);
23483f289c3fSJeff Roberson 				/* Last policy wins. */
23493f289c3fSJeff Roberson 				outset.ds_policy = dset->ds_policy;
23503f289c3fSJeff Roberson 				outset.ds_prefer = dset->ds_prefer;
23513f289c3fSJeff Roberson 				thread_unlock(ttd);
23523f289c3fSJeff Roberson 			}
23533f289c3fSJeff Roberson 			break;
23543f289c3fSJeff Roberson 		case CPU_WHICH_CPUSET:
23553f289c3fSJeff Roberson 		case CPU_WHICH_JAIL:
23563f289c3fSJeff Roberson 			domainset_copy(set->cs_domain, &outset);
23573f289c3fSJeff Roberson 			break;
23583f289c3fSJeff Roberson 		case CPU_WHICH_IRQ:
23593f289c3fSJeff Roberson 		case CPU_WHICH_INTRHANDLER:
23603f289c3fSJeff Roberson 		case CPU_WHICH_ITHREAD:
23613f289c3fSJeff Roberson 		case CPU_WHICH_DOMAIN:
23623f289c3fSJeff Roberson 			error = EINVAL;
23633f289c3fSJeff Roberson 			break;
23643f289c3fSJeff Roberson 		}
23653f289c3fSJeff Roberson 		break;
23663f289c3fSJeff Roberson 	default:
23673f289c3fSJeff Roberson 		error = EINVAL;
23683f289c3fSJeff Roberson 		break;
23693f289c3fSJeff Roberson 	}
23703f289c3fSJeff Roberson 	if (set)
23713f289c3fSJeff Roberson 		cpuset_rel(set);
23723f289c3fSJeff Roberson 	if (p)
23733f289c3fSJeff Roberson 		PROC_UNLOCK(p);
23743f289c3fSJeff Roberson 	/*
23753f289c3fSJeff Roberson 	 * Translate prefer into a set containing only the preferred domain,
23763f289c3fSJeff Roberson 	 * not the entire fallback set.
23773f289c3fSJeff Roberson 	 */
23783f289c3fSJeff Roberson 	if (outset.ds_policy == DOMAINSET_POLICY_PREFER) {
23793f289c3fSJeff Roberson 		DOMAINSET_ZERO(&outset.ds_mask);
23803f289c3fSJeff Roberson 		DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask);
23813f289c3fSJeff Roberson 	}
23823f289c3fSJeff Roberson 	DOMAINSET_COPY(&outset.ds_mask, mask);
23833f289c3fSJeff Roberson 	if (error == 0)
23844a3e5133SMark Johnston 		error = cb->cpuset_copyout(mask, maskp, domainsetsize);
23853f289c3fSJeff Roberson 	if (error == 0)
2386dd51fec3SBrooks Davis 		if (suword32(policyp, outset.ds_policy) != 0)
2387dd51fec3SBrooks Davis 			error = EFAULT;
23883f289c3fSJeff Roberson out:
23893f289c3fSJeff Roberson 	free(mask, M_TEMP);
23903f289c3fSJeff Roberson 	return (error);
23913f289c3fSJeff Roberson }
23923f289c3fSJeff Roberson 
23933f289c3fSJeff Roberson #ifndef _SYS_SYSPROTO_H_
23943f289c3fSJeff Roberson struct cpuset_setdomain_args {
23953f289c3fSJeff Roberson 	cpulevel_t	level;
23963f289c3fSJeff Roberson 	cpuwhich_t	which;
23973f289c3fSJeff Roberson 	id_t		id;
23983f289c3fSJeff Roberson 	size_t		domainsetsize;
23993f289c3fSJeff Roberson 	domainset_t	*mask;
24003f289c3fSJeff Roberson 	int 		policy;
24013f289c3fSJeff Roberson };
24023f289c3fSJeff Roberson #endif
24033f289c3fSJeff Roberson int
sys_cpuset_setdomain(struct thread * td,struct cpuset_setdomain_args * uap)24043f289c3fSJeff Roberson sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap)
24053f289c3fSJeff Roberson {
24063f289c3fSJeff Roberson 
24073f289c3fSJeff Roberson 	return (kern_cpuset_setdomain(td, uap->level, uap->which,
240847a57144SJustin Hibbits 	    uap->id, uap->domainsetsize, uap->mask, uap->policy, &copy_set));
24093f289c3fSJeff Roberson }
24103f289c3fSJeff Roberson 
24113f289c3fSJeff Roberson int
kern_cpuset_setdomain(struct thread * td,cpulevel_t level,cpuwhich_t which,id_t id,size_t domainsetsize,const domainset_t * maskp,int policy,const struct cpuset_copy_cb * cb)24123f289c3fSJeff Roberson kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which,
241347a57144SJustin Hibbits     id_t id, size_t domainsetsize, const domainset_t *maskp, int policy,
241447a57144SJustin Hibbits     const struct cpuset_copy_cb *cb)
24153f289c3fSJeff Roberson {
24163f289c3fSJeff Roberson 	struct cpuset *nset;
24173f289c3fSJeff Roberson 	struct cpuset *set;
24183f289c3fSJeff Roberson 	struct thread *ttd;
24193f289c3fSJeff Roberson 	struct proc *p;
24203f289c3fSJeff Roberson 	struct domainset domain;
24213f289c3fSJeff Roberson 	domainset_t *mask;
24223f289c3fSJeff Roberson 	int error;
24233f289c3fSJeff Roberson 
24243f289c3fSJeff Roberson 	if (domainsetsize < sizeof(domainset_t) ||
24253f289c3fSJeff Roberson 	    domainsetsize > DOMAINSET_MAXSIZE / NBBY)
24263f289c3fSJeff Roberson 		return (ERANGE);
242770d66bcfSEric van Gyzen 	if (policy <= DOMAINSET_POLICY_INVALID ||
242870d66bcfSEric van Gyzen 	    policy > DOMAINSET_POLICY_MAX)
242970d66bcfSEric van Gyzen 		return (EINVAL);
24309eb997cbSMark Johnston 	error = cpuset_check_capabilities(td, level, which, id);
24319eb997cbSMark Johnston 	if (error != 0)
24329eb997cbSMark Johnston 		return (error);
24333f289c3fSJeff Roberson 	memset(&domain, 0, sizeof(domain));
24343f289c3fSJeff Roberson 	mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO);
24354a3e5133SMark Johnston 	error = cb->cpuset_copyin(maskp, mask, domainsetsize);
24363f289c3fSJeff Roberson 	if (error)
24373f289c3fSJeff Roberson 		goto out;
24383f289c3fSJeff Roberson 	/*
24393f289c3fSJeff Roberson 	 * Verify that no high bits are set.
24403f289c3fSJeff Roberson 	 */
24413f289c3fSJeff Roberson 	if (domainsetsize > sizeof(domainset_t)) {
24423f289c3fSJeff Roberson 		char *end;
24433f289c3fSJeff Roberson 		char *cp;
24443f289c3fSJeff Roberson 
24453f289c3fSJeff Roberson 		end = cp = (char *)&mask->__bits;
24463f289c3fSJeff Roberson 		end += domainsetsize;
24473f289c3fSJeff Roberson 		cp += sizeof(domainset_t);
24483f289c3fSJeff Roberson 		while (cp != end)
24493f289c3fSJeff Roberson 			if (*cp++ != 0) {
24503f289c3fSJeff Roberson 				error = EINVAL;
24513f289c3fSJeff Roberson 				goto out;
24523f289c3fSJeff Roberson 			}
24533f289c3fSJeff Roberson 	}
2454f1b18a66SKyle Evans 	if (DOMAINSET_EMPTY(mask)) {
2455f1b18a66SKyle Evans 		error = EDEADLK;
2456f1b18a66SKyle Evans 		goto out;
2457f1b18a66SKyle Evans 	}
24583f289c3fSJeff Roberson 	DOMAINSET_COPY(mask, &domain.ds_mask);
24593f289c3fSJeff Roberson 	domain.ds_policy = policy;
24603f289c3fSJeff Roberson 
246163cdd18eSMark Johnston 	/*
246263cdd18eSMark Johnston 	 * Sanitize the provided mask.
246363cdd18eSMark Johnston 	 */
246463cdd18eSMark Johnston 	if (!DOMAINSET_SUBSET(&all_domains, &domain.ds_mask)) {
246563cdd18eSMark Johnston 		error = EINVAL;
246663cdd18eSMark Johnston 		goto out;
246763cdd18eSMark Johnston 	}
246863cdd18eSMark Johnston 
24693f289c3fSJeff Roberson 	/* Translate preferred policy into a mask and fallback. */
24703f289c3fSJeff Roberson 	if (policy == DOMAINSET_POLICY_PREFER) {
24713f289c3fSJeff Roberson 		/* Only support a single preferred domain. */
247270d66bcfSEric van Gyzen 		if (DOMAINSET_COUNT(&domain.ds_mask) != 1) {
247370d66bcfSEric van Gyzen 			error = EINVAL;
247470d66bcfSEric van Gyzen 			goto out;
247570d66bcfSEric van Gyzen 		}
24763f289c3fSJeff Roberson 		domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1;
24773f289c3fSJeff Roberson 		/* This will be constrained by domainset_shadow(). */
247863cdd18eSMark Johnston 		DOMAINSET_COPY(&all_domains, &domain.ds_mask);
24793f289c3fSJeff Roberson 	}
24803f289c3fSJeff Roberson 
248130c5525bSAndrew Gallatin 	/*
248230c5525bSAndrew Gallatin 	 * When given an impossible policy, fall back to interleaving
248363cdd18eSMark Johnston 	 * across all domains.
248430c5525bSAndrew Gallatin 	 */
248530c5525bSAndrew Gallatin 	if (domainset_empty_vm(&domain))
248629bb6c19SMark Johnston 		domainset_copy(domainset2, &domain);
248730c5525bSAndrew Gallatin 
24883f289c3fSJeff Roberson 	switch (level) {
24893f289c3fSJeff Roberson 	case CPU_LEVEL_ROOT:
24903f289c3fSJeff Roberson 	case CPU_LEVEL_CPUSET:
24913f289c3fSJeff Roberson 		error = cpuset_which(which, id, &p, &ttd, &set);
24923f289c3fSJeff Roberson 		if (error)
24933f289c3fSJeff Roberson 			break;
24943f289c3fSJeff Roberson 		switch (which) {
24953f289c3fSJeff Roberson 		case CPU_WHICH_TID:
24963f289c3fSJeff Roberson 		case CPU_WHICH_PID:
24972058f075SDmitry Chagin 		case CPU_WHICH_TIDPID:
24983f289c3fSJeff Roberson 			thread_lock(ttd);
24993f289c3fSJeff Roberson 			set = cpuset_ref(ttd->td_cpuset);
25003f289c3fSJeff Roberson 			thread_unlock(ttd);
25013f289c3fSJeff Roberson 			PROC_UNLOCK(p);
25023f289c3fSJeff Roberson 			break;
25033f289c3fSJeff Roberson 		case CPU_WHICH_CPUSET:
25043f289c3fSJeff Roberson 		case CPU_WHICH_JAIL:
25053f289c3fSJeff Roberson 			break;
25063f289c3fSJeff Roberson 		case CPU_WHICH_IRQ:
25073f289c3fSJeff Roberson 		case CPU_WHICH_INTRHANDLER:
25083f289c3fSJeff Roberson 		case CPU_WHICH_ITHREAD:
25093f289c3fSJeff Roberson 		case CPU_WHICH_DOMAIN:
25103f289c3fSJeff Roberson 			error = EINVAL;
25113f289c3fSJeff Roberson 			goto out;
25123f289c3fSJeff Roberson 		}
25133f289c3fSJeff Roberson 		if (level == CPU_LEVEL_ROOT)
25143f289c3fSJeff Roberson 			nset = cpuset_refroot(set);
25153f289c3fSJeff Roberson 		else
25163f289c3fSJeff Roberson 			nset = cpuset_refbase(set);
25173f289c3fSJeff Roberson 		error = cpuset_modify_domain(nset, &domain);
25183f289c3fSJeff Roberson 		cpuset_rel(nset);
25193f289c3fSJeff Roberson 		cpuset_rel(set);
25203f289c3fSJeff Roberson 		break;
25213f289c3fSJeff Roberson 	case CPU_LEVEL_WHICH:
25223f289c3fSJeff Roberson 		switch (which) {
25233f289c3fSJeff Roberson 		case CPU_WHICH_TID:
25243f289c3fSJeff Roberson 			error = _cpuset_setthread(id, NULL, &domain);
25253f289c3fSJeff Roberson 			break;
25263f289c3fSJeff Roberson 		case CPU_WHICH_PID:
2527d431dea5SKyle Evans 			error = cpuset_setproc(id, NULL, NULL, &domain, false);
25283f289c3fSJeff Roberson 			break;
25292058f075SDmitry Chagin 		case CPU_WHICH_TIDPID:
25302058f075SDmitry Chagin 			if (id > PID_MAX || id == -1)
25312058f075SDmitry Chagin 				error = _cpuset_setthread(id, NULL, &domain);
25322058f075SDmitry Chagin 			else
25332058f075SDmitry Chagin 				error = cpuset_setproc(id, NULL, NULL, &domain,
25342058f075SDmitry Chagin 				    false);
25352058f075SDmitry Chagin 			break;
25363f289c3fSJeff Roberson 		case CPU_WHICH_CPUSET:
25373f289c3fSJeff Roberson 		case CPU_WHICH_JAIL:
25383f289c3fSJeff Roberson 			error = cpuset_which(which, id, &p, &ttd, &set);
25393f289c3fSJeff Roberson 			if (error == 0) {
25403f289c3fSJeff Roberson 				error = cpuset_modify_domain(set, &domain);
25413f289c3fSJeff Roberson 				cpuset_rel(set);
25423f289c3fSJeff Roberson 			}
25433f289c3fSJeff Roberson 			break;
25443f289c3fSJeff Roberson 		case CPU_WHICH_IRQ:
25453f289c3fSJeff Roberson 		case CPU_WHICH_INTRHANDLER:
25463f289c3fSJeff Roberson 		case CPU_WHICH_ITHREAD:
25473f289c3fSJeff Roberson 		default:
25483f289c3fSJeff Roberson 			error = EINVAL;
25493f289c3fSJeff Roberson 			break;
25503f289c3fSJeff Roberson 		}
25513f289c3fSJeff Roberson 		break;
25523f289c3fSJeff Roberson 	default:
25533f289c3fSJeff Roberson 		error = EINVAL;
25543f289c3fSJeff Roberson 		break;
25553f289c3fSJeff Roberson 	}
25563f289c3fSJeff Roberson out:
25573f289c3fSJeff Roberson 	free(mask, M_TEMP);
25583f289c3fSJeff Roberson 	return (error);
25593f289c3fSJeff Roberson }
25603f289c3fSJeff Roberson 
25613f289c3fSJeff Roberson #ifdef DDB
2562e5818a53SJeff Roberson 
25633f289c3fSJeff Roberson static void
ddb_display_bitset(const struct bitset * set,int size)25643f289c3fSJeff Roberson ddb_display_bitset(const struct bitset *set, int size)
25653f289c3fSJeff Roberson {
25663f289c3fSJeff Roberson 	int bit, once;
25673f289c3fSJeff Roberson 
25683f289c3fSJeff Roberson 	for (once = 0, bit = 0; bit < size; bit++) {
25693f289c3fSJeff Roberson 		if (CPU_ISSET(bit, set)) {
2570dea0ed66SBjoern A. Zeeb 			if (once == 0) {
25713f289c3fSJeff Roberson 				db_printf("%d", bit);
2572dea0ed66SBjoern A. Zeeb 				once = 1;
2573dea0ed66SBjoern A. Zeeb 			} else
25743f289c3fSJeff Roberson 				db_printf(",%d", bit);
2575dea0ed66SBjoern A. Zeeb 		}
2576dea0ed66SBjoern A. Zeeb 	}
2577cd32bd7aSJohn Baldwin 	if (once == 0)
2578cd32bd7aSJohn Baldwin 		db_printf("<none>");
2579cd32bd7aSJohn Baldwin }
2580cd32bd7aSJohn Baldwin 
25813f289c3fSJeff Roberson void
ddb_display_cpuset(const cpuset_t * set)25823f289c3fSJeff Roberson ddb_display_cpuset(const cpuset_t *set)
25833f289c3fSJeff Roberson {
25843f289c3fSJeff Roberson 	ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE);
25853f289c3fSJeff Roberson }
25863f289c3fSJeff Roberson 
25873f289c3fSJeff Roberson static void
ddb_display_domainset(const domainset_t * set)25883f289c3fSJeff Roberson ddb_display_domainset(const domainset_t *set)
25893f289c3fSJeff Roberson {
25903f289c3fSJeff Roberson 	ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE);
25913f289c3fSJeff Roberson }
25923f289c3fSJeff Roberson 
DB_SHOW_COMMAND_FLAGS(cpusets,db_show_cpusets,DB_CMD_MEMSAFE)2593c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(cpusets, db_show_cpusets, DB_CMD_MEMSAFE)
2594cd32bd7aSJohn Baldwin {
2595cd32bd7aSJohn Baldwin 	struct cpuset *set;
2596cd32bd7aSJohn Baldwin 
2597cd32bd7aSJohn Baldwin 	LIST_FOREACH(set, &cpuset_ids, cs_link) {
2598cd32bd7aSJohn Baldwin 		db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
25991a7bb896SMateusz Guzik 		    set, set->cs_id, refcount_load(&set->cs_ref), set->cs_flags,
2600cd32bd7aSJohn Baldwin 		    (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
26013f289c3fSJeff Roberson 		db_printf("  cpu mask=");
2602cd32bd7aSJohn Baldwin 		ddb_display_cpuset(&set->cs_mask);
2603dea0ed66SBjoern A. Zeeb 		db_printf("\n");
26043f289c3fSJeff Roberson 		db_printf("  domain policy %d prefer %d mask=",
26053f289c3fSJeff Roberson 		    set->cs_domain->ds_policy, set->cs_domain->ds_prefer);
26063f289c3fSJeff Roberson 		ddb_display_domainset(&set->cs_domain->ds_mask);
26073f289c3fSJeff Roberson 		db_printf("\n");
2608dea0ed66SBjoern A. Zeeb 		if (db_pager_quit)
2609dea0ed66SBjoern A. Zeeb 			break;
2610dea0ed66SBjoern A. Zeeb 	}
2611dea0ed66SBjoern A. Zeeb }
26123f289c3fSJeff Roberson 
DB_SHOW_COMMAND_FLAGS(domainsets,db_show_domainsets,DB_CMD_MEMSAFE)2613c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(domainsets, db_show_domainsets, DB_CMD_MEMSAFE)
26143f289c3fSJeff Roberson {
26153f289c3fSJeff Roberson 	struct domainset *set;
26163f289c3fSJeff Roberson 
26173f289c3fSJeff Roberson 	LIST_FOREACH(set, &cpuset_domains, ds_link) {
2618e5818a53SJeff Roberson 		db_printf("set=%p policy %d prefer %d cnt %d\n",
2619e5818a53SJeff Roberson 		    set, set->ds_policy, set->ds_prefer, set->ds_cnt);
26203f289c3fSJeff Roberson 		db_printf("  mask =");
26213f289c3fSJeff Roberson 		ddb_display_domainset(&set->ds_mask);
26223f289c3fSJeff Roberson 		db_printf("\n");
26233f289c3fSJeff Roberson 	}
26243f289c3fSJeff Roberson }
2625dea0ed66SBjoern A. Zeeb #endif /* DDB */
2626