xref: /freebsd/sys/kern/kern_cpuset.c (revision ea2ebdc19e1c9635d1f808c6f298db40d70f8f58)
1d7f687fcSJeff Roberson /*-
2d7f687fcSJeff Roberson  * Copyright (c) 2008,  Jeffrey Roberson <jeff@freebsd.org>
3d7f687fcSJeff Roberson  * All rights reserved.
4d7f687fcSJeff Roberson  *
53bc8c68dSJeff Roberson  * Copyright (c) 2008 Nokia Corporation
63bc8c68dSJeff Roberson  * All rights reserved.
73bc8c68dSJeff Roberson  *
8d7f687fcSJeff Roberson  * Redistribution and use in source and binary forms, with or without
9d7f687fcSJeff Roberson  * modification, are permitted provided that the following conditions
10d7f687fcSJeff Roberson  * are met:
11d7f687fcSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
12d7f687fcSJeff Roberson  *    notice unmodified, this list of conditions, and the following
13d7f687fcSJeff Roberson  *    disclaimer.
14d7f687fcSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
15d7f687fcSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
16d7f687fcSJeff Roberson  *    documentation and/or other materials provided with the distribution.
17d7f687fcSJeff Roberson  *
18d7f687fcSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19d7f687fcSJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20d7f687fcSJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21d7f687fcSJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22d7f687fcSJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23d7f687fcSJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24d7f687fcSJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25d7f687fcSJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26d7f687fcSJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27d7f687fcSJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28d7f687fcSJeff Roberson  *
29d7f687fcSJeff Roberson  */
30d7f687fcSJeff Roberson 
31d7f687fcSJeff Roberson #include <sys/cdefs.h>
32d7f687fcSJeff Roberson __FBSDID("$FreeBSD$");
33d7f687fcSJeff Roberson 
34dea0ed66SBjoern A. Zeeb #include "opt_ddb.h"
35dea0ed66SBjoern A. Zeeb 
36d7f687fcSJeff Roberson #include <sys/param.h>
37d7f687fcSJeff Roberson #include <sys/systm.h>
38d7f687fcSJeff Roberson #include <sys/sysproto.h>
390304c731SJamie Gritton #include <sys/jail.h>
40d7f687fcSJeff Roberson #include <sys/kernel.h>
41d7f687fcSJeff Roberson #include <sys/lock.h>
42d7f687fcSJeff Roberson #include <sys/malloc.h>
43d7f687fcSJeff Roberson #include <sys/mutex.h>
44d7f687fcSJeff Roberson #include <sys/priv.h>
45d7f687fcSJeff Roberson #include <sys/proc.h>
46d7f687fcSJeff Roberson #include <sys/refcount.h>
47d7f687fcSJeff Roberson #include <sys/sched.h>
48d7f687fcSJeff Roberson #include <sys/smp.h>
49d7f687fcSJeff Roberson #include <sys/syscallsubr.h>
50d7f687fcSJeff Roberson #include <sys/cpuset.h>
51d7f687fcSJeff Roberson #include <sys/sx.h>
52d7f687fcSJeff Roberson #include <sys/queue.h>
53e3709597SAttilio Rao #include <sys/libkern.h>
54d7f687fcSJeff Roberson #include <sys/limits.h>
55a03ee000SJeff Roberson #include <sys/bus.h>
56a03ee000SJeff Roberson #include <sys/interrupt.h>
57d7f687fcSJeff Roberson 
58d7f687fcSJeff Roberson #include <vm/uma.h>
59c0ae6688SJohn Baldwin #include <vm/vm.h>
60c0ae6688SJohn Baldwin #include <vm/vm_page.h>
61c0ae6688SJohn Baldwin #include <vm/vm_param.h>
62c0ae6688SJohn Baldwin #include <vm/vm_phys.h>
63d7f687fcSJeff Roberson 
64dea0ed66SBjoern A. Zeeb #ifdef DDB
65dea0ed66SBjoern A. Zeeb #include <ddb/ddb.h>
66dea0ed66SBjoern A. Zeeb #endif /* DDB */
67dea0ed66SBjoern A. Zeeb 
68d7f687fcSJeff Roberson /*
69d7f687fcSJeff Roberson  * cpusets provide a mechanism for creating and manipulating sets of
70d7f687fcSJeff Roberson  * processors for the purpose of constraining the scheduling of threads to
71d7f687fcSJeff Roberson  * specific processors.
72d7f687fcSJeff Roberson  *
73d7f687fcSJeff Roberson  * Each process belongs to an identified set, by default this is set 1.  Each
74d7f687fcSJeff Roberson  * thread may further restrict the cpus it may run on to a subset of this
75d7f687fcSJeff Roberson  * named set.  This creates an anonymous set which other threads and processes
76d7f687fcSJeff Roberson  * may not join by number.
77d7f687fcSJeff Roberson  *
78d7f687fcSJeff Roberson  * The named set is referred to herein as the 'base' set to avoid ambiguity.
79d7f687fcSJeff Roberson  * This set is usually a child of a 'root' set while the anonymous set may
80d7f687fcSJeff Roberson  * simply be referred to as a mask.  In the syscall api these are referred to
81d7f687fcSJeff Roberson  * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
82d7f687fcSJeff Roberson  *
83d7f687fcSJeff Roberson  * Threads inherit their set from their creator whether it be anonymous or
84d7f687fcSJeff Roberson  * not.  This means that anonymous sets are immutable because they may be
85d7f687fcSJeff Roberson  * shared.  To modify an anonymous set a new set is created with the desired
86d7f687fcSJeff Roberson  * mask and the same parent as the existing anonymous set.  This gives the
8793902625SJohn Baldwin  * illusion of each thread having a private mask.
88d7f687fcSJeff Roberson  *
89d7f687fcSJeff Roberson  * Via the syscall apis a user may ask to retrieve or modify the root, base,
90d7f687fcSJeff Roberson  * or mask that is discovered via a pid, tid, or setid.  Modifying a set
91d7f687fcSJeff Roberson  * modifies all numbered and anonymous child sets to comply with the new mask.
92d7f687fcSJeff Roberson  * Modifying a pid or tid's mask applies only to that tid but must still
93d7f687fcSJeff Roberson  * exist within the assigned parent set.
94d7f687fcSJeff Roberson  *
9586855bf5SJohn Baldwin  * A thread may not be assigned to a group separate from other threads in
96d7f687fcSJeff Roberson  * the process.  This is to remove ambiguity when the setid is queried with
97d7f687fcSJeff Roberson  * a pid argument.  There is no other technical limitation.
98d7f687fcSJeff Roberson  *
99d7f687fcSJeff Roberson  * This somewhat complex arrangement is intended to make it easy for
100d7f687fcSJeff Roberson  * applications to query available processors and bind their threads to
101d7f687fcSJeff Roberson  * specific processors while also allowing administrators to dynamically
102d7f687fcSJeff Roberson  * reprovision by changing sets which apply to groups of processes.
103d7f687fcSJeff Roberson  *
104d7f687fcSJeff Roberson  * A simple application should not concern itself with sets at all and
105d7f687fcSJeff Roberson  * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
10693902625SJohn Baldwin  * meaning 'curthread'.  It may query available cpus for that tid with a
107d7f687fcSJeff Roberson  * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
108d7f687fcSJeff Roberson  */
109d7f687fcSJeff Roberson static uma_zone_t cpuset_zone;
110d7f687fcSJeff Roberson static struct mtx cpuset_lock;
111d7f687fcSJeff Roberson static struct setlist cpuset_ids;
112d7f687fcSJeff Roberson static struct unrhdr *cpuset_unr;
11381198539SAlexander V. Chernikov static struct cpuset *cpuset_zero, *cpuset_default;
114a03ee000SJeff Roberson 
115444528c0SDavid Xu /* Return the size of cpuset_t at the kernel level */
11660aa2c85SJonathan Anderson SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD,
117f0188618SHans Petter Selasky     SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)");
118444528c0SDavid Xu 
119a03ee000SJeff Roberson cpuset_t *cpuset_root;
120c0ae6688SJohn Baldwin cpuset_t cpuset_domain[MAXMEMDOM];
121d7f687fcSJeff Roberson 
122d7f687fcSJeff Roberson /*
123d7f687fcSJeff Roberson  * Acquire a reference to a cpuset, all pointers must be tracked with refs.
124d7f687fcSJeff Roberson  */
125d7f687fcSJeff Roberson struct cpuset *
126d7f687fcSJeff Roberson cpuset_ref(struct cpuset *set)
127d7f687fcSJeff Roberson {
128d7f687fcSJeff Roberson 
129d7f687fcSJeff Roberson 	refcount_acquire(&set->cs_ref);
130d7f687fcSJeff Roberson 	return (set);
131d7f687fcSJeff Roberson }
132d7f687fcSJeff Roberson 
133d7f687fcSJeff Roberson /*
1347a8f695aSBjoern A. Zeeb  * Walks up the tree from 'set' to find the root.  Returns the root
1357a8f695aSBjoern A. Zeeb  * referenced.
1367a8f695aSBjoern A. Zeeb  */
1377a8f695aSBjoern A. Zeeb static struct cpuset *
1387a8f695aSBjoern A. Zeeb cpuset_refroot(struct cpuset *set)
1397a8f695aSBjoern A. Zeeb {
1407a8f695aSBjoern A. Zeeb 
1417a8f695aSBjoern A. Zeeb 	for (; set->cs_parent != NULL; set = set->cs_parent)
1427a8f695aSBjoern A. Zeeb 		if (set->cs_flags & CPU_SET_ROOT)
1437a8f695aSBjoern A. Zeeb 			break;
1447a8f695aSBjoern A. Zeeb 	cpuset_ref(set);
1457a8f695aSBjoern A. Zeeb 
1467a8f695aSBjoern A. Zeeb 	return (set);
1477a8f695aSBjoern A. Zeeb }
1487a8f695aSBjoern A. Zeeb 
1497a8f695aSBjoern A. Zeeb /*
1507a8f695aSBjoern A. Zeeb  * Find the first non-anonymous set starting from 'set'.  Returns this set
1517a8f695aSBjoern A. Zeeb  * referenced.  May return the passed in set with an extra ref if it is
1527a8f695aSBjoern A. Zeeb  * not anonymous.
1537a8f695aSBjoern A. Zeeb  */
1547a8f695aSBjoern A. Zeeb static struct cpuset *
1557a8f695aSBjoern A. Zeeb cpuset_refbase(struct cpuset *set)
1567a8f695aSBjoern A. Zeeb {
1577a8f695aSBjoern A. Zeeb 
1587a8f695aSBjoern A. Zeeb 	if (set->cs_id == CPUSET_INVALID)
1597a8f695aSBjoern A. Zeeb 		set = set->cs_parent;
1607a8f695aSBjoern A. Zeeb 	cpuset_ref(set);
1617a8f695aSBjoern A. Zeeb 
1627a8f695aSBjoern A. Zeeb 	return (set);
1637a8f695aSBjoern A. Zeeb }
1647a8f695aSBjoern A. Zeeb 
1657a8f695aSBjoern A. Zeeb /*
16693902625SJohn Baldwin  * Release a reference in a context where it is safe to allocate.
167d7f687fcSJeff Roberson  */
168d7f687fcSJeff Roberson void
169d7f687fcSJeff Roberson cpuset_rel(struct cpuset *set)
170d7f687fcSJeff Roberson {
171d7f687fcSJeff Roberson 	cpusetid_t id;
172d7f687fcSJeff Roberson 
173d7f687fcSJeff Roberson 	if (refcount_release(&set->cs_ref) == 0)
174d7f687fcSJeff Roberson 		return;
175d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
176d7f687fcSJeff Roberson 	LIST_REMOVE(set, cs_siblings);
177d7f687fcSJeff Roberson 	id = set->cs_id;
178d7f687fcSJeff Roberson 	if (id != CPUSET_INVALID)
179d7f687fcSJeff Roberson 		LIST_REMOVE(set, cs_link);
180d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
181d7f687fcSJeff Roberson 	cpuset_rel(set->cs_parent);
182d7f687fcSJeff Roberson 	uma_zfree(cpuset_zone, set);
183d7f687fcSJeff Roberson 	if (id != CPUSET_INVALID)
184d7f687fcSJeff Roberson 		free_unr(cpuset_unr, id);
185d7f687fcSJeff Roberson }
186d7f687fcSJeff Roberson 
187d7f687fcSJeff Roberson /*
188d7f687fcSJeff Roberson  * Deferred release must be used when in a context that is not safe to
189d7f687fcSJeff Roberson  * allocate/free.  This places any unreferenced sets on the list 'head'.
190d7f687fcSJeff Roberson  */
191d7f687fcSJeff Roberson static void
192d7f687fcSJeff Roberson cpuset_rel_defer(struct setlist *head, struct cpuset *set)
193d7f687fcSJeff Roberson {
194d7f687fcSJeff Roberson 
195d7f687fcSJeff Roberson 	if (refcount_release(&set->cs_ref) == 0)
196d7f687fcSJeff Roberson 		return;
197d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
198d7f687fcSJeff Roberson 	LIST_REMOVE(set, cs_siblings);
199d7f687fcSJeff Roberson 	if (set->cs_id != CPUSET_INVALID)
200d7f687fcSJeff Roberson 		LIST_REMOVE(set, cs_link);
201d7f687fcSJeff Roberson 	LIST_INSERT_HEAD(head, set, cs_link);
202d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
203d7f687fcSJeff Roberson }
204d7f687fcSJeff Roberson 
205d7f687fcSJeff Roberson /*
206d7f687fcSJeff Roberson  * Complete a deferred release.  Removes the set from the list provided to
207d7f687fcSJeff Roberson  * cpuset_rel_defer.
208d7f687fcSJeff Roberson  */
209d7f687fcSJeff Roberson static void
210d7f687fcSJeff Roberson cpuset_rel_complete(struct cpuset *set)
211d7f687fcSJeff Roberson {
212d7f687fcSJeff Roberson 	LIST_REMOVE(set, cs_link);
213d7f687fcSJeff Roberson 	cpuset_rel(set->cs_parent);
214d7f687fcSJeff Roberson 	uma_zfree(cpuset_zone, set);
215d7f687fcSJeff Roberson }
216d7f687fcSJeff Roberson 
217d7f687fcSJeff Roberson /*
218d7f687fcSJeff Roberson  * Find a set based on an id.  Returns it with a ref.
219d7f687fcSJeff Roberson  */
220d7f687fcSJeff Roberson static struct cpuset *
221413628a7SBjoern A. Zeeb cpuset_lookup(cpusetid_t setid, struct thread *td)
222d7f687fcSJeff Roberson {
223d7f687fcSJeff Roberson 	struct cpuset *set;
224d7f687fcSJeff Roberson 
225d7f687fcSJeff Roberson 	if (setid == CPUSET_INVALID)
226d7f687fcSJeff Roberson 		return (NULL);
227d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
228d7f687fcSJeff Roberson 	LIST_FOREACH(set, &cpuset_ids, cs_link)
229d7f687fcSJeff Roberson 		if (set->cs_id == setid)
230d7f687fcSJeff Roberson 			break;
231d7f687fcSJeff Roberson 	if (set)
232d7f687fcSJeff Roberson 		cpuset_ref(set);
233d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
234413628a7SBjoern A. Zeeb 
235413628a7SBjoern A. Zeeb 	KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
236413628a7SBjoern A. Zeeb 	if (set != NULL && jailed(td->td_ucred)) {
2370304c731SJamie Gritton 		struct cpuset *jset, *tset;
238413628a7SBjoern A. Zeeb 
2390304c731SJamie Gritton 		jset = td->td_ucred->cr_prison->pr_cpuset;
2400304c731SJamie Gritton 		for (tset = set; tset != NULL; tset = tset->cs_parent)
2410304c731SJamie Gritton 			if (tset == jset)
2420304c731SJamie Gritton 				break;
2430304c731SJamie Gritton 		if (tset == NULL) {
244413628a7SBjoern A. Zeeb 			cpuset_rel(set);
245413628a7SBjoern A. Zeeb 			set = NULL;
246413628a7SBjoern A. Zeeb 		}
247413628a7SBjoern A. Zeeb 	}
248413628a7SBjoern A. Zeeb 
249d7f687fcSJeff Roberson 	return (set);
250d7f687fcSJeff Roberson }
251d7f687fcSJeff Roberson 
252d7f687fcSJeff Roberson /*
253d7f687fcSJeff Roberson  * Create a set in the space provided in 'set' with the provided parameters.
254d7f687fcSJeff Roberson  * The set is returned with a single ref.  May return EDEADLK if the set
255d7f687fcSJeff Roberson  * will have no valid cpu based on restrictions from the parent.
256d7f687fcSJeff Roberson  */
257d7f687fcSJeff Roberson static int
258e84c2db1SJohn Baldwin _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
259d7f687fcSJeff Roberson     cpusetid_t id)
260d7f687fcSJeff Roberson {
261d7f687fcSJeff Roberson 
26273c40187SJeff Roberson 	if (!CPU_OVERLAP(&parent->cs_mask, mask))
26373c40187SJeff Roberson 		return (EDEADLK);
264d7f687fcSJeff Roberson 	CPU_COPY(mask, &set->cs_mask);
265d7f687fcSJeff Roberson 	LIST_INIT(&set->cs_children);
266d7f687fcSJeff Roberson 	refcount_init(&set->cs_ref, 1);
267d7f687fcSJeff Roberson 	set->cs_flags = 0;
268d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
269e84c2db1SJohn Baldwin 	CPU_AND(&set->cs_mask, &parent->cs_mask);
270d7f687fcSJeff Roberson 	set->cs_id = id;
271d7f687fcSJeff Roberson 	set->cs_parent = cpuset_ref(parent);
272d7f687fcSJeff Roberson 	LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
273d7f687fcSJeff Roberson 	if (set->cs_id != CPUSET_INVALID)
274d7f687fcSJeff Roberson 		LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
275d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
276d7f687fcSJeff Roberson 
27773c40187SJeff Roberson 	return (0);
278d7f687fcSJeff Roberson }
279d7f687fcSJeff Roberson 
280d7f687fcSJeff Roberson /*
281d7f687fcSJeff Roberson  * Create a new non-anonymous set with the requested parent and mask.  May
282d7f687fcSJeff Roberson  * return failures if the mask is invalid or a new number can not be
283d7f687fcSJeff Roberson  * allocated.
284d7f687fcSJeff Roberson  */
285d7f687fcSJeff Roberson static int
286e84c2db1SJohn Baldwin cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
287d7f687fcSJeff Roberson {
288d7f687fcSJeff Roberson 	struct cpuset *set;
289d7f687fcSJeff Roberson 	cpusetid_t id;
290d7f687fcSJeff Roberson 	int error;
291d7f687fcSJeff Roberson 
292d7f687fcSJeff Roberson 	id = alloc_unr(cpuset_unr);
293d7f687fcSJeff Roberson 	if (id == -1)
294d7f687fcSJeff Roberson 		return (ENFILE);
295d7f687fcSJeff Roberson 	*setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
296d7f687fcSJeff Roberson 	error = _cpuset_create(set, parent, mask, id);
297d7f687fcSJeff Roberson 	if (error == 0)
298d7f687fcSJeff Roberson 		return (0);
299d7f687fcSJeff Roberson 	free_unr(cpuset_unr, id);
300d7f687fcSJeff Roberson 	uma_zfree(cpuset_zone, set);
301d7f687fcSJeff Roberson 
302d7f687fcSJeff Roberson 	return (error);
303d7f687fcSJeff Roberson }
304d7f687fcSJeff Roberson 
305d7f687fcSJeff Roberson /*
306d7f687fcSJeff Roberson  * Recursively check for errors that would occur from applying mask to
307d7f687fcSJeff Roberson  * the tree of sets starting at 'set'.  Checks for sets that would become
308d7f687fcSJeff Roberson  * empty as well as RDONLY flags.
309d7f687fcSJeff Roberson  */
310d7f687fcSJeff Roberson static int
311c9813d0aSJohn Baldwin cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
312d7f687fcSJeff Roberson {
313d7f687fcSJeff Roberson 	struct cpuset *nset;
314d7f687fcSJeff Roberson 	cpuset_t newmask;
315d7f687fcSJeff Roberson 	int error;
316d7f687fcSJeff Roberson 
317d7f687fcSJeff Roberson 	mtx_assert(&cpuset_lock, MA_OWNED);
318d7f687fcSJeff Roberson 	if (set->cs_flags & CPU_SET_RDONLY)
319d7f687fcSJeff Roberson 		return (EPERM);
320c9813d0aSJohn Baldwin 	if (check_mask) {
32173c40187SJeff Roberson 		if (!CPU_OVERLAP(&set->cs_mask, mask))
32273c40187SJeff Roberson 			return (EDEADLK);
323d7f687fcSJeff Roberson 		CPU_COPY(&set->cs_mask, &newmask);
324d7f687fcSJeff Roberson 		CPU_AND(&newmask, mask);
325c9813d0aSJohn Baldwin 	} else
326c9813d0aSJohn Baldwin 		CPU_COPY(mask, &newmask);
32773c40187SJeff Roberson 	error = 0;
328d7f687fcSJeff Roberson 	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
329c9813d0aSJohn Baldwin 		if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
330d7f687fcSJeff Roberson 			break;
331d7f687fcSJeff Roberson 	return (error);
332d7f687fcSJeff Roberson }
333d7f687fcSJeff Roberson 
334d7f687fcSJeff Roberson /*
335d7f687fcSJeff Roberson  * Applies the mask 'mask' without checking for empty sets or permissions.
336d7f687fcSJeff Roberson  */
337d7f687fcSJeff Roberson static void
338d7f687fcSJeff Roberson cpuset_update(struct cpuset *set, cpuset_t *mask)
339d7f687fcSJeff Roberson {
340d7f687fcSJeff Roberson 	struct cpuset *nset;
341d7f687fcSJeff Roberson 
342d7f687fcSJeff Roberson 	mtx_assert(&cpuset_lock, MA_OWNED);
343d7f687fcSJeff Roberson 	CPU_AND(&set->cs_mask, mask);
344d7f687fcSJeff Roberson 	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
345d7f687fcSJeff Roberson 		cpuset_update(nset, &set->cs_mask);
346d7f687fcSJeff Roberson 
347d7f687fcSJeff Roberson 	return;
348d7f687fcSJeff Roberson }
349d7f687fcSJeff Roberson 
350d7f687fcSJeff Roberson /*
351d7f687fcSJeff Roberson  * Modify the set 'set' to use a copy of the mask provided.  Apply this new
352d7f687fcSJeff Roberson  * mask to restrict all children in the tree.  Checks for validity before
353d7f687fcSJeff Roberson  * applying the changes.
354d7f687fcSJeff Roberson  */
355d7f687fcSJeff Roberson static int
356d7f687fcSJeff Roberson cpuset_modify(struct cpuset *set, cpuset_t *mask)
357d7f687fcSJeff Roberson {
35873c40187SJeff Roberson 	struct cpuset *root;
359d7f687fcSJeff Roberson 	int error;
360d7f687fcSJeff Roberson 
361ba931c08SBjoern A. Zeeb 	error = priv_check(curthread, PRIV_SCHED_CPUSET);
362d7f687fcSJeff Roberson 	if (error)
363d7f687fcSJeff Roberson 		return (error);
36473c40187SJeff Roberson 	/*
3656aaa0b3cSBjoern A. Zeeb 	 * In case we are called from within the jail
3666aaa0b3cSBjoern A. Zeeb 	 * we do not allow modifying the dedicated root
3676aaa0b3cSBjoern A. Zeeb 	 * cpuset of the jail but may still allow to
3686aaa0b3cSBjoern A. Zeeb 	 * change child sets.
3696aaa0b3cSBjoern A. Zeeb 	 */
3706aaa0b3cSBjoern A. Zeeb 	if (jailed(curthread->td_ucred) &&
3716aaa0b3cSBjoern A. Zeeb 	    set->cs_flags & CPU_SET_ROOT)
3726aaa0b3cSBjoern A. Zeeb 		return (EPERM);
3736aaa0b3cSBjoern A. Zeeb 	/*
37473c40187SJeff Roberson 	 * Verify that we have access to this set of
37573c40187SJeff Roberson 	 * cpus.
37673c40187SJeff Roberson 	 */
37773c40187SJeff Roberson 	root = set->cs_parent;
37873c40187SJeff Roberson 	if (root && !CPU_SUBSET(&root->cs_mask, mask))
37973c40187SJeff Roberson 		return (EINVAL);
380d7f687fcSJeff Roberson 	mtx_lock_spin(&cpuset_lock);
381c9813d0aSJohn Baldwin 	error = cpuset_testupdate(set, mask, 0);
382d7f687fcSJeff Roberson 	if (error)
383d7f687fcSJeff Roberson 		goto out;
384d7f687fcSJeff Roberson 	CPU_COPY(mask, &set->cs_mask);
385c9813d0aSJohn Baldwin 	cpuset_update(set, mask);
386d7f687fcSJeff Roberson out:
387d7f687fcSJeff Roberson 	mtx_unlock_spin(&cpuset_lock);
388d7f687fcSJeff Roberson 
389d7f687fcSJeff Roberson 	return (error);
390d7f687fcSJeff Roberson }
391d7f687fcSJeff Roberson 
392d7f687fcSJeff Roberson /*
393d7f687fcSJeff Roberson  * Resolve the 'which' parameter of several cpuset apis.
394d7f687fcSJeff Roberson  *
395d7f687fcSJeff Roberson  * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid.  Also
396d7f687fcSJeff Roberson  * checks for permission via p_cansched().
397d7f687fcSJeff Roberson  *
398d7f687fcSJeff Roberson  * For WHICH_SET returns a valid set with a new reference.
399d7f687fcSJeff Roberson  *
400d7f687fcSJeff Roberson  * -1 may be supplied for any argument to mean the current proc/thread or
401d7f687fcSJeff Roberson  * the base set of the current thread.  May fail with ESRCH/EPERM.
402d7f687fcSJeff Roberson  */
4035bbb2169SAdrian Chadd int
404d7f687fcSJeff Roberson cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
405d7f687fcSJeff Roberson     struct cpuset **setp)
406d7f687fcSJeff Roberson {
407d7f687fcSJeff Roberson 	struct cpuset *set;
408d7f687fcSJeff Roberson 	struct thread *td;
409d7f687fcSJeff Roberson 	struct proc *p;
410d7f687fcSJeff Roberson 	int error;
411d7f687fcSJeff Roberson 
412d7f687fcSJeff Roberson 	*pp = p = NULL;
413d7f687fcSJeff Roberson 	*tdp = td = NULL;
414d7f687fcSJeff Roberson 	*setp = set = NULL;
415d7f687fcSJeff Roberson 	switch (which) {
416d7f687fcSJeff Roberson 	case CPU_WHICH_PID:
417d7f687fcSJeff Roberson 		if (id == -1) {
418d7f687fcSJeff Roberson 			PROC_LOCK(curproc);
419d7f687fcSJeff Roberson 			p = curproc;
420d7f687fcSJeff Roberson 			break;
421d7f687fcSJeff Roberson 		}
422d7f687fcSJeff Roberson 		if ((p = pfind(id)) == NULL)
423d7f687fcSJeff Roberson 			return (ESRCH);
424d7f687fcSJeff Roberson 		break;
425d7f687fcSJeff Roberson 	case CPU_WHICH_TID:
426d7f687fcSJeff Roberson 		if (id == -1) {
427d7f687fcSJeff Roberson 			PROC_LOCK(curproc);
428d7f687fcSJeff Roberson 			p = curproc;
429d7f687fcSJeff Roberson 			td = curthread;
430d7f687fcSJeff Roberson 			break;
431d7f687fcSJeff Roberson 		}
43242fe684cSDavid Xu 		td = tdfind(id, -1);
433d7f687fcSJeff Roberson 		if (td == NULL)
434d7f687fcSJeff Roberson 			return (ESRCH);
43542fe684cSDavid Xu 		p = td->td_proc;
436d7f687fcSJeff Roberson 		break;
437d7f687fcSJeff Roberson 	case CPU_WHICH_CPUSET:
438d7f687fcSJeff Roberson 		if (id == -1) {
439d7f687fcSJeff Roberson 			thread_lock(curthread);
440a03ee000SJeff Roberson 			set = cpuset_refbase(curthread->td_cpuset);
441d7f687fcSJeff Roberson 			thread_unlock(curthread);
442d7f687fcSJeff Roberson 		} else
443413628a7SBjoern A. Zeeb 			set = cpuset_lookup(id, curthread);
444d7f687fcSJeff Roberson 		if (set) {
445d7f687fcSJeff Roberson 			*setp = set;
446d7f687fcSJeff Roberson 			return (0);
447d7f687fcSJeff Roberson 		}
448d7f687fcSJeff Roberson 		return (ESRCH);
449413628a7SBjoern A. Zeeb 	case CPU_WHICH_JAIL:
450413628a7SBjoern A. Zeeb 	{
451413628a7SBjoern A. Zeeb 		/* Find `set' for prison with given id. */
452413628a7SBjoern A. Zeeb 		struct prison *pr;
453413628a7SBjoern A. Zeeb 
454413628a7SBjoern A. Zeeb 		sx_slock(&allprison_lock);
4550304c731SJamie Gritton 		pr = prison_find_child(curthread->td_ucred->cr_prison, id);
456413628a7SBjoern A. Zeeb 		sx_sunlock(&allprison_lock);
457413628a7SBjoern A. Zeeb 		if (pr == NULL)
458413628a7SBjoern A. Zeeb 			return (ESRCH);
459413628a7SBjoern A. Zeeb 		cpuset_ref(pr->pr_cpuset);
4600304c731SJamie Gritton 		*setp = pr->pr_cpuset;
461413628a7SBjoern A. Zeeb 		mtx_unlock(&pr->pr_mtx);
462413628a7SBjoern A. Zeeb 		return (0);
463413628a7SBjoern A. Zeeb 	}
4649b33b154SJeff Roberson 	case CPU_WHICH_IRQ:
465c0ae6688SJohn Baldwin 	case CPU_WHICH_DOMAIN:
4669b33b154SJeff Roberson 		return (0);
467d7f687fcSJeff Roberson 	default:
468d7f687fcSJeff Roberson 		return (EINVAL);
469d7f687fcSJeff Roberson 	}
470d7f687fcSJeff Roberson 	error = p_cansched(curthread, p);
471d7f687fcSJeff Roberson 	if (error) {
472d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
473d7f687fcSJeff Roberson 		return (error);
474d7f687fcSJeff Roberson 	}
475d7f687fcSJeff Roberson 	if (td == NULL)
476d7f687fcSJeff Roberson 		td = FIRST_THREAD_IN_PROC(p);
477d7f687fcSJeff Roberson 	*pp = p;
478d7f687fcSJeff Roberson 	*tdp = td;
479d7f687fcSJeff Roberson 	return (0);
480d7f687fcSJeff Roberson }
481d7f687fcSJeff Roberson 
482d7f687fcSJeff Roberson /*
483d7f687fcSJeff Roberson  * Create an anonymous set with the provided mask in the space provided by
484d7f687fcSJeff Roberson  * 'fset'.  If the passed in set is anonymous we use its parent otherwise
485d7f687fcSJeff Roberson  * the new set is a child of 'set'.
486d7f687fcSJeff Roberson  */
487d7f687fcSJeff Roberson static int
488e84c2db1SJohn Baldwin cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
489d7f687fcSJeff Roberson {
490d7f687fcSJeff Roberson 	struct cpuset *parent;
491d7f687fcSJeff Roberson 
492d7f687fcSJeff Roberson 	if (set->cs_id == CPUSET_INVALID)
493d7f687fcSJeff Roberson 		parent = set->cs_parent;
494d7f687fcSJeff Roberson 	else
495d7f687fcSJeff Roberson 		parent = set;
49673c40187SJeff Roberson 	if (!CPU_SUBSET(&parent->cs_mask, mask))
497a03ee000SJeff Roberson 		return (EDEADLK);
498d7f687fcSJeff Roberson 	return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
499d7f687fcSJeff Roberson }
500d7f687fcSJeff Roberson 
501d7f687fcSJeff Roberson /*
502d7f687fcSJeff Roberson  * Handle two cases for replacing the base set or mask of an entire process.
503d7f687fcSJeff Roberson  *
504d7f687fcSJeff Roberson  * 1) Set is non-null and mask is null.  This reparents all anonymous sets
505d7f687fcSJeff Roberson  *    to the provided set and replaces all non-anonymous td_cpusets with the
506d7f687fcSJeff Roberson  *    provided set.
507d7f687fcSJeff Roberson  * 2) Mask is non-null and set is null.  This replaces or creates anonymous
508d7f687fcSJeff Roberson  *    sets for every thread with the existing base as a parent.
509d7f687fcSJeff Roberson  *
510d7f687fcSJeff Roberson  * This is overly complicated because we can't allocate while holding a
511d7f687fcSJeff Roberson  * spinlock and spinlocks must be held while changing and examining thread
512d7f687fcSJeff Roberson  * state.
513d7f687fcSJeff Roberson  */
514d7f687fcSJeff Roberson static int
515d7f687fcSJeff Roberson cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
516d7f687fcSJeff Roberson {
517d7f687fcSJeff Roberson 	struct setlist freelist;
518d7f687fcSJeff Roberson 	struct setlist droplist;
51973c40187SJeff Roberson 	struct cpuset *tdset;
520d7f687fcSJeff Roberson 	struct cpuset *nset;
521d7f687fcSJeff Roberson 	struct thread *td;
522d7f687fcSJeff Roberson 	struct proc *p;
523d7f687fcSJeff Roberson 	int threads;
524d7f687fcSJeff Roberson 	int nfree;
525d7f687fcSJeff Roberson 	int error;
526d7f687fcSJeff Roberson 	/*
527d7f687fcSJeff Roberson 	 * The algorithm requires two passes due to locking considerations.
528d7f687fcSJeff Roberson 	 *
529d7f687fcSJeff Roberson 	 * 1) Lookup the process and acquire the locks in the required order.
530d7f687fcSJeff Roberson 	 * 2) If enough cpusets have not been allocated release the locks and
531d7f687fcSJeff Roberson 	 *    allocate them.  Loop.
532d7f687fcSJeff Roberson 	 */
533d7f687fcSJeff Roberson 	LIST_INIT(&freelist);
534d7f687fcSJeff Roberson 	LIST_INIT(&droplist);
535d7f687fcSJeff Roberson 	nfree = 0;
536d7f687fcSJeff Roberson 	for (;;) {
537d7f687fcSJeff Roberson 		error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
538d7f687fcSJeff Roberson 		if (error)
539d7f687fcSJeff Roberson 			goto out;
540d7f687fcSJeff Roberson 		if (nfree >= p->p_numthreads)
541d7f687fcSJeff Roberson 			break;
542d7f687fcSJeff Roberson 		threads = p->p_numthreads;
543d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
544d7f687fcSJeff Roberson 		for (; nfree < threads; nfree++) {
545d7f687fcSJeff Roberson 			nset = uma_zalloc(cpuset_zone, M_WAITOK);
546d7f687fcSJeff Roberson 			LIST_INSERT_HEAD(&freelist, nset, cs_link);
547d7f687fcSJeff Roberson 		}
548d7f687fcSJeff Roberson 	}
549d7f687fcSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
550d7f687fcSJeff Roberson 	/*
551d7f687fcSJeff Roberson 	 * Now that the appropriate locks are held and we have enough cpusets,
55273c40187SJeff Roberson 	 * make sure the operation will succeed before applying changes.  The
55373c40187SJeff Roberson 	 * proc lock prevents td_cpuset from changing between calls.
554d7f687fcSJeff Roberson 	 */
555d7f687fcSJeff Roberson 	error = 0;
556d7f687fcSJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td) {
55773c40187SJeff Roberson 		thread_lock(td);
55873c40187SJeff Roberson 		tdset = td->td_cpuset;
55973c40187SJeff Roberson 		/*
56073c40187SJeff Roberson 		 * Verify that a new mask doesn't specify cpus outside of
56173c40187SJeff Roberson 		 * the set the thread is a member of.
56273c40187SJeff Roberson 		 */
56373c40187SJeff Roberson 		if (mask) {
56473c40187SJeff Roberson 			if (tdset->cs_id == CPUSET_INVALID)
56573c40187SJeff Roberson 				tdset = tdset->cs_parent;
56673c40187SJeff Roberson 			if (!CPU_SUBSET(&tdset->cs_mask, mask))
567a03ee000SJeff Roberson 				error = EDEADLK;
56873c40187SJeff Roberson 		/*
56973c40187SJeff Roberson 		 * Verify that a new set won't leave an existing thread
57073c40187SJeff Roberson 		 * mask without a cpu to run on.  It can, however, restrict
57173c40187SJeff Roberson 		 * the set.
57273c40187SJeff Roberson 		 */
57373c40187SJeff Roberson 		} else if (tdset->cs_id == CPUSET_INVALID) {
57473c40187SJeff Roberson 			if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
575a03ee000SJeff Roberson 				error = EDEADLK;
57673c40187SJeff Roberson 		}
57773c40187SJeff Roberson 		thread_unlock(td);
57873c40187SJeff Roberson 		if (error)
57973c40187SJeff Roberson 			goto unlock_out;
58073c40187SJeff Roberson 	}
58173c40187SJeff Roberson 	/*
58273c40187SJeff Roberson 	 * Replace each thread's cpuset while using deferred release.  We
583374ae2a3SJeff Roberson 	 * must do this because the thread lock must be held while operating
584374ae2a3SJeff Roberson 	 * on the thread and this limits the type of operations allowed.
58573c40187SJeff Roberson 	 */
58673c40187SJeff Roberson 	FOREACH_THREAD_IN_PROC(p, td) {
587d7f687fcSJeff Roberson 		thread_lock(td);
588d7f687fcSJeff Roberson 		/*
589d7f687fcSJeff Roberson 		 * If we presently have an anonymous set or are applying a
590d7f687fcSJeff Roberson 		 * mask we must create an anonymous shadow set.  That is
591d7f687fcSJeff Roberson 		 * either parented to our existing base or the supplied set.
592d7f687fcSJeff Roberson 		 *
593d7f687fcSJeff Roberson 		 * If we have a base set with no anonymous shadow we simply
594d7f687fcSJeff Roberson 		 * replace it outright.
595d7f687fcSJeff Roberson 		 */
596d7f687fcSJeff Roberson 		tdset = td->td_cpuset;
597d7f687fcSJeff Roberson 		if (tdset->cs_id == CPUSET_INVALID || mask) {
598d7f687fcSJeff Roberson 			nset = LIST_FIRST(&freelist);
599d7f687fcSJeff Roberson 			LIST_REMOVE(nset, cs_link);
600d7f687fcSJeff Roberson 			if (mask)
601d7f687fcSJeff Roberson 				error = cpuset_shadow(tdset, nset, mask);
602d7f687fcSJeff Roberson 			else
603d7f687fcSJeff Roberson 				error = _cpuset_create(nset, set,
604d7f687fcSJeff Roberson 				    &tdset->cs_mask, CPUSET_INVALID);
605d7f687fcSJeff Roberson 			if (error) {
606d7f687fcSJeff Roberson 				LIST_INSERT_HEAD(&freelist, nset, cs_link);
607d7f687fcSJeff Roberson 				thread_unlock(td);
608d7f687fcSJeff Roberson 				break;
609d7f687fcSJeff Roberson 			}
610d7f687fcSJeff Roberson 		} else
611d7f687fcSJeff Roberson 			nset = cpuset_ref(set);
612d7f687fcSJeff Roberson 		cpuset_rel_defer(&droplist, tdset);
613d7f687fcSJeff Roberson 		td->td_cpuset = nset;
614d7f687fcSJeff Roberson 		sched_affinity(td);
615d7f687fcSJeff Roberson 		thread_unlock(td);
616d7f687fcSJeff Roberson 	}
61773c40187SJeff Roberson unlock_out:
618d7f687fcSJeff Roberson 	PROC_UNLOCK(p);
619d7f687fcSJeff Roberson out:
620d7f687fcSJeff Roberson 	while ((nset = LIST_FIRST(&droplist)) != NULL)
621d7f687fcSJeff Roberson 		cpuset_rel_complete(nset);
622d7f687fcSJeff Roberson 	while ((nset = LIST_FIRST(&freelist)) != NULL) {
623d7f687fcSJeff Roberson 		LIST_REMOVE(nset, cs_link);
624d7f687fcSJeff Roberson 		uma_zfree(cpuset_zone, nset);
625d7f687fcSJeff Roberson 	}
626d7f687fcSJeff Roberson 	return (error);
627d7f687fcSJeff Roberson }
628d7f687fcSJeff Roberson 
629d7f687fcSJeff Roberson /*
63071a19bdcSAttilio Rao  * Return a string representing a valid layout for a cpuset_t object.
63171a19bdcSAttilio Rao  * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
63271a19bdcSAttilio Rao  */
63371a19bdcSAttilio Rao char *
63471a19bdcSAttilio Rao cpusetobj_strprint(char *buf, const cpuset_t *set)
63571a19bdcSAttilio Rao {
63671a19bdcSAttilio Rao 	char *tbuf;
63771a19bdcSAttilio Rao 	size_t i, bytesp, bufsiz;
63871a19bdcSAttilio Rao 
63971a19bdcSAttilio Rao 	tbuf = buf;
64071a19bdcSAttilio Rao 	bytesp = 0;
64171a19bdcSAttilio Rao 	bufsiz = CPUSETBUFSIZ;
64271a19bdcSAttilio Rao 
643d4a2ab8cSAttilio Rao 	for (i = 0; i < (_NCPUWORDS - 1); i++) {
64471a19bdcSAttilio Rao 		bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
64571a19bdcSAttilio Rao 		bufsiz -= bytesp;
64671a19bdcSAttilio Rao 		tbuf += bytesp;
64771a19bdcSAttilio Rao 	}
648d4a2ab8cSAttilio Rao 	snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
64971a19bdcSAttilio Rao 	return (buf);
65071a19bdcSAttilio Rao }
65171a19bdcSAttilio Rao 
65271a19bdcSAttilio Rao /*
653e3709597SAttilio Rao  * Build a valid cpuset_t object from a string representation.
654e3709597SAttilio Rao  * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
655e3709597SAttilio Rao  */
656e3709597SAttilio Rao int
657e3709597SAttilio Rao cpusetobj_strscan(cpuset_t *set, const char *buf)
658e3709597SAttilio Rao {
659e3709597SAttilio Rao 	u_int nwords;
660e3709597SAttilio Rao 	int i, ret;
661e3709597SAttilio Rao 
662e3709597SAttilio Rao 	if (strlen(buf) > CPUSETBUFSIZ - 1)
663e3709597SAttilio Rao 		return (-1);
664e3709597SAttilio Rao 
665e3709597SAttilio Rao 	/* Allow to pass a shorter version of the mask when necessary. */
666e3709597SAttilio Rao 	nwords = 1;
667e3709597SAttilio Rao 	for (i = 0; buf[i] != '\0'; i++)
668e3709597SAttilio Rao 		if (buf[i] == ',')
669e3709597SAttilio Rao 			nwords++;
670e3709597SAttilio Rao 	if (nwords > _NCPUWORDS)
671e3709597SAttilio Rao 		return (-1);
672e3709597SAttilio Rao 
673e3709597SAttilio Rao 	CPU_ZERO(set);
674d4a2ab8cSAttilio Rao 	for (i = 0; i < (nwords - 1); i++) {
675e3709597SAttilio Rao 		ret = sscanf(buf, "%lx,", &set->__bits[i]);
676e3709597SAttilio Rao 		if (ret == 0 || ret == -1)
677e3709597SAttilio Rao 			return (-1);
678d4a2ab8cSAttilio Rao 		buf = strstr(buf, ",");
679e3709597SAttilio Rao 		if (buf == NULL)
680e3709597SAttilio Rao 			return (-1);
681e3709597SAttilio Rao 		buf++;
682e3709597SAttilio Rao 	}
683d4a2ab8cSAttilio Rao 	ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
684e3709597SAttilio Rao 	if (ret == 0 || ret == -1)
685e3709597SAttilio Rao 		return (-1);
686e3709597SAttilio Rao 	return (0);
687e3709597SAttilio Rao }
688e3709597SAttilio Rao 
689e3709597SAttilio Rao /*
690d7f687fcSJeff Roberson  * Apply an anonymous mask to a single thread.
691d7f687fcSJeff Roberson  */
692a03ee000SJeff Roberson int
693d7f687fcSJeff Roberson cpuset_setthread(lwpid_t id, cpuset_t *mask)
694d7f687fcSJeff Roberson {
695d7f687fcSJeff Roberson 	struct cpuset *nset;
696d7f687fcSJeff Roberson 	struct cpuset *set;
697d7f687fcSJeff Roberson 	struct thread *td;
698d7f687fcSJeff Roberson 	struct proc *p;
699d7f687fcSJeff Roberson 	int error;
700d7f687fcSJeff Roberson 
701d7f687fcSJeff Roberson 	nset = uma_zalloc(cpuset_zone, M_WAITOK);
7028bd75bddSJeff Roberson 	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
703d7f687fcSJeff Roberson 	if (error)
704d7f687fcSJeff Roberson 		goto out;
705a03ee000SJeff Roberson 	set = NULL;
706d7f687fcSJeff Roberson 	thread_lock(td);
707a03ee000SJeff Roberson 	error = cpuset_shadow(td->td_cpuset, nset, mask);
708d7f687fcSJeff Roberson 	if (error == 0) {
709a03ee000SJeff Roberson 		set = td->td_cpuset;
710d7f687fcSJeff Roberson 		td->td_cpuset = nset;
711d7f687fcSJeff Roberson 		sched_affinity(td);
712d7f687fcSJeff Roberson 		nset = NULL;
713d7f687fcSJeff Roberson 	}
714d7f687fcSJeff Roberson 	thread_unlock(td);
715d7f687fcSJeff Roberson 	PROC_UNLOCK(p);
716a03ee000SJeff Roberson 	if (set)
717a03ee000SJeff Roberson 		cpuset_rel(set);
718d7f687fcSJeff Roberson out:
719d7f687fcSJeff Roberson 	if (nset)
720d7f687fcSJeff Roberson 		uma_zfree(cpuset_zone, nset);
721d7f687fcSJeff Roberson 	return (error);
722d7f687fcSJeff Roberson }
723d7f687fcSJeff Roberson 
724d7f687fcSJeff Roberson /*
72581198539SAlexander V. Chernikov  * Apply new cpumask to the ithread.
72681198539SAlexander V. Chernikov  */
72781198539SAlexander V. Chernikov int
7287f7528fcSAdrian Chadd cpuset_setithread(lwpid_t id, int cpu)
72981198539SAlexander V. Chernikov {
73081198539SAlexander V. Chernikov 	struct cpuset *nset, *rset;
73181198539SAlexander V. Chernikov 	struct cpuset *parent, *old_set;
73281198539SAlexander V. Chernikov 	struct thread *td;
73381198539SAlexander V. Chernikov 	struct proc *p;
73481198539SAlexander V. Chernikov 	cpusetid_t cs_id;
73581198539SAlexander V. Chernikov 	cpuset_t mask;
73681198539SAlexander V. Chernikov 	int error;
73781198539SAlexander V. Chernikov 
73881198539SAlexander V. Chernikov 	nset = uma_zalloc(cpuset_zone, M_WAITOK);
73981198539SAlexander V. Chernikov 	rset = uma_zalloc(cpuset_zone, M_WAITOK);
740c1d9ecf2SAlexander V. Chernikov 	cs_id = CPUSET_INVALID;
74181198539SAlexander V. Chernikov 
74281198539SAlexander V. Chernikov 	CPU_ZERO(&mask);
74381198539SAlexander V. Chernikov 	if (cpu == NOCPU)
74481198539SAlexander V. Chernikov 		CPU_COPY(cpuset_root, &mask);
74581198539SAlexander V. Chernikov 	else
74681198539SAlexander V. Chernikov 		CPU_SET(cpu, &mask);
74781198539SAlexander V. Chernikov 
74881198539SAlexander V. Chernikov 	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set);
749c1d9ecf2SAlexander V. Chernikov 	if (error != 0 || ((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID))
75081198539SAlexander V. Chernikov 		goto out;
75181198539SAlexander V. Chernikov 
752c1d9ecf2SAlexander V. Chernikov 	/* cpuset_which() returns with PROC_LOCK held. */
75381198539SAlexander V. Chernikov 	old_set = td->td_cpuset;
75481198539SAlexander V. Chernikov 
75581198539SAlexander V. Chernikov 	if (cpu == NOCPU) {
756c1d9ecf2SAlexander V. Chernikov 
75781198539SAlexander V. Chernikov 		/*
75881198539SAlexander V. Chernikov 		 * roll back to default set. We're not using cpuset_shadow()
75981198539SAlexander V. Chernikov 		 * here because we can fail CPU_SUBSET() check. This can happen
76081198539SAlexander V. Chernikov 		 * if default set does not contain all CPUs.
76181198539SAlexander V. Chernikov 		 */
76281198539SAlexander V. Chernikov 		error = _cpuset_create(nset, cpuset_default, &mask,
76381198539SAlexander V. Chernikov 		    CPUSET_INVALID);
76481198539SAlexander V. Chernikov 
76581198539SAlexander V. Chernikov 		goto applyset;
76681198539SAlexander V. Chernikov 	}
76781198539SAlexander V. Chernikov 
76881198539SAlexander V. Chernikov 	if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID &&
76981198539SAlexander V. Chernikov 	    old_set->cs_parent->cs_id == 1)) {
770c1d9ecf2SAlexander V. Chernikov 
771c1d9ecf2SAlexander V. Chernikov 		/*
772c1d9ecf2SAlexander V. Chernikov 		 * Current set is either default (1) or
773c1d9ecf2SAlexander V. Chernikov 		 * shadowed version of default set.
774c1d9ecf2SAlexander V. Chernikov 		 *
775c1d9ecf2SAlexander V. Chernikov 		 * Allocate new root set to be able to shadow it
776c1d9ecf2SAlexander V. Chernikov 		 * with any mask.
777c1d9ecf2SAlexander V. Chernikov 		 */
77881198539SAlexander V. Chernikov 		error = _cpuset_create(rset, cpuset_zero,
77981198539SAlexander V. Chernikov 		    &cpuset_zero->cs_mask, cs_id);
78081198539SAlexander V. Chernikov 		if (error != 0) {
78181198539SAlexander V. Chernikov 			PROC_UNLOCK(p);
78281198539SAlexander V. Chernikov 			goto out;
78381198539SAlexander V. Chernikov 		}
78481198539SAlexander V. Chernikov 		rset->cs_flags |= CPU_SET_ROOT;
78581198539SAlexander V. Chernikov 		parent = rset;
78681198539SAlexander V. Chernikov 		rset = NULL;
78781198539SAlexander V. Chernikov 		cs_id = CPUSET_INVALID;
78881198539SAlexander V. Chernikov 	} else {
78981198539SAlexander V. Chernikov 		/* Assume existing set was already allocated by previous call */
790c1d9ecf2SAlexander V. Chernikov 		parent = old_set;
79181198539SAlexander V. Chernikov 		old_set = NULL;
79281198539SAlexander V. Chernikov 	}
79381198539SAlexander V. Chernikov 
79481198539SAlexander V. Chernikov 	error = cpuset_shadow(parent, nset, &mask);
79581198539SAlexander V. Chernikov applyset:
79681198539SAlexander V. Chernikov 	if (error == 0) {
797c1d9ecf2SAlexander V. Chernikov 		thread_lock(td);
79881198539SAlexander V. Chernikov 		td->td_cpuset = nset;
79981198539SAlexander V. Chernikov 		sched_affinity(td);
80081198539SAlexander V. Chernikov 		thread_unlock(td);
801c1d9ecf2SAlexander V. Chernikov 		nset = NULL;
802c1d9ecf2SAlexander V. Chernikov 	} else
803c1d9ecf2SAlexander V. Chernikov 		old_set = NULL;
80481198539SAlexander V. Chernikov 	PROC_UNLOCK(p);
80581198539SAlexander V. Chernikov 	if (old_set != NULL)
80681198539SAlexander V. Chernikov 		cpuset_rel(old_set);
80781198539SAlexander V. Chernikov out:
80881198539SAlexander V. Chernikov 	if (nset != NULL)
80981198539SAlexander V. Chernikov 		uma_zfree(cpuset_zone, nset);
81081198539SAlexander V. Chernikov 	if (rset != NULL)
81181198539SAlexander V. Chernikov 		uma_zfree(cpuset_zone, rset);
81281198539SAlexander V. Chernikov 	if (cs_id != CPUSET_INVALID)
81381198539SAlexander V. Chernikov 		free_unr(cpuset_unr, cs_id);
81481198539SAlexander V. Chernikov 	return (error);
81581198539SAlexander V. Chernikov }
81681198539SAlexander V. Chernikov 
81781198539SAlexander V. Chernikov 
81881198539SAlexander V. Chernikov /*
819c0ae6688SJohn Baldwin  * Creates system-wide cpusets and the cpuset for thread0 including two
820c0ae6688SJohn Baldwin  * sets:
821d7f687fcSJeff Roberson  *
822d7f687fcSJeff Roberson  * 0 - The root set which should represent all valid processors in the
823d7f687fcSJeff Roberson  *     system.  It is initially created with a mask of all processors
824d7f687fcSJeff Roberson  *     because we don't know what processors are valid until cpuset_init()
825d7f687fcSJeff Roberson  *     runs.  This set is immutable.
826d7f687fcSJeff Roberson  * 1 - The default set which all processes are a member of until changed.
827d7f687fcSJeff Roberson  *     This allows an administrator to move all threads off of given cpus to
828d7f687fcSJeff Roberson  *     dedicate them to high priority tasks or save power etc.
829d7f687fcSJeff Roberson  */
830d7f687fcSJeff Roberson struct cpuset *
831d7f687fcSJeff Roberson cpuset_thread0(void)
832d7f687fcSJeff Roberson {
833d7f687fcSJeff Roberson 	struct cpuset *set;
83462d70a81SJohn Baldwin 	int error, i;
835d7f687fcSJeff Roberson 
836d7f687fcSJeff Roberson 	cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
837d7f687fcSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
838d7f687fcSJeff Roberson 	mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
83981198539SAlexander V. Chernikov 
840d7f687fcSJeff Roberson 	/*
841d7f687fcSJeff Roberson 	 * Create the root system set for the whole machine.  Doesn't use
842d7f687fcSJeff Roberson 	 * cpuset_create() due to NULL parent.
843d7f687fcSJeff Roberson 	 */
844d7f687fcSJeff Roberson 	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
845f27aed53SAttilio Rao 	CPU_FILL(&set->cs_mask);
846d7f687fcSJeff Roberson 	LIST_INIT(&set->cs_children);
847d7f687fcSJeff Roberson 	LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
848d7f687fcSJeff Roberson 	set->cs_ref = 1;
849d7f687fcSJeff Roberson 	set->cs_flags = CPU_SET_ROOT;
850d7f687fcSJeff Roberson 	cpuset_zero = set;
851a03ee000SJeff Roberson 	cpuset_root = &set->cs_mask;
85281198539SAlexander V. Chernikov 
853d7f687fcSJeff Roberson 	/*
854d7f687fcSJeff Roberson 	 * Now derive a default, modifiable set from that to give out.
855d7f687fcSJeff Roberson 	 */
856d7f687fcSJeff Roberson 	set = uma_zalloc(cpuset_zone, M_WAITOK);
857d7f687fcSJeff Roberson 	error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
858d7f687fcSJeff Roberson 	KASSERT(error == 0, ("Error creating default set: %d\n", error));
85981198539SAlexander V. Chernikov 	cpuset_default = set;
86081198539SAlexander V. Chernikov 
861d7f687fcSJeff Roberson 	/*
862d7f687fcSJeff Roberson 	 * Initialize the unit allocator. 0 and 1 are allocated above.
863d7f687fcSJeff Roberson 	 */
864d7f687fcSJeff Roberson 	cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
865d7f687fcSJeff Roberson 
86662d70a81SJohn Baldwin 	/*
86762d70a81SJohn Baldwin 	 * If MD code has not initialized per-domain cpusets, place all
86862d70a81SJohn Baldwin 	 * CPUs in domain 0.
86962d70a81SJohn Baldwin 	 */
87062d70a81SJohn Baldwin 	for (i = 0; i < MAXMEMDOM; i++)
87162d70a81SJohn Baldwin 		if (!CPU_EMPTY(&cpuset_domain[i]))
87262d70a81SJohn Baldwin 			goto domains_set;
873c0ae6688SJohn Baldwin 	CPU_COPY(&all_cpus, &cpuset_domain[0]);
87462d70a81SJohn Baldwin domains_set:
875c0ae6688SJohn Baldwin 
876d7f687fcSJeff Roberson 	return (set);
877d7f687fcSJeff Roberson }
878d7f687fcSJeff Roberson 
879d7f687fcSJeff Roberson /*
880413628a7SBjoern A. Zeeb  * Create a cpuset, which would be cpuset_create() but
881413628a7SBjoern A. Zeeb  * mark the new 'set' as root.
882413628a7SBjoern A. Zeeb  *
88347479a8cSBjoern A. Zeeb  * We are not going to reparent the td to it.  Use cpuset_setproc_update_set()
88447479a8cSBjoern A. Zeeb  * for that.
885413628a7SBjoern A. Zeeb  *
886413628a7SBjoern A. Zeeb  * In case of no error, returns the set in *setp locked with a reference.
887413628a7SBjoern A. Zeeb  */
888413628a7SBjoern A. Zeeb int
8890304c731SJamie Gritton cpuset_create_root(struct prison *pr, struct cpuset **setp)
890413628a7SBjoern A. Zeeb {
891413628a7SBjoern A. Zeeb 	struct cpuset *set;
892413628a7SBjoern A. Zeeb 	int error;
893413628a7SBjoern A. Zeeb 
8940304c731SJamie Gritton 	KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
895413628a7SBjoern A. Zeeb 	KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
896413628a7SBjoern A. Zeeb 
8970304c731SJamie Gritton 	error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
898413628a7SBjoern A. Zeeb 	if (error)
899413628a7SBjoern A. Zeeb 		return (error);
900413628a7SBjoern A. Zeeb 
901413628a7SBjoern A. Zeeb 	KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
902413628a7SBjoern A. Zeeb 	    __func__, __LINE__));
903413628a7SBjoern A. Zeeb 
904413628a7SBjoern A. Zeeb 	/* Mark the set as root. */
905413628a7SBjoern A. Zeeb 	set = *setp;
906413628a7SBjoern A. Zeeb 	set->cs_flags |= CPU_SET_ROOT;
907413628a7SBjoern A. Zeeb 
908413628a7SBjoern A. Zeeb 	return (0);
909413628a7SBjoern A. Zeeb }
910413628a7SBjoern A. Zeeb 
911413628a7SBjoern A. Zeeb int
912413628a7SBjoern A. Zeeb cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
913413628a7SBjoern A. Zeeb {
914413628a7SBjoern A. Zeeb 	int error;
915413628a7SBjoern A. Zeeb 
916413628a7SBjoern A. Zeeb 	KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
917413628a7SBjoern A. Zeeb 	KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
918413628a7SBjoern A. Zeeb 
919413628a7SBjoern A. Zeeb 	cpuset_ref(set);
920413628a7SBjoern A. Zeeb 	error = cpuset_setproc(p->p_pid, set, NULL);
921413628a7SBjoern A. Zeeb 	if (error)
922413628a7SBjoern A. Zeeb 		return (error);
923413628a7SBjoern A. Zeeb 	cpuset_rel(set);
924413628a7SBjoern A. Zeeb 	return (0);
925413628a7SBjoern A. Zeeb }
926413628a7SBjoern A. Zeeb 
927413628a7SBjoern A. Zeeb /*
928d7f687fcSJeff Roberson  * This is called once the final set of system cpus is known.  Modifies
92993902625SJohn Baldwin  * the root set and all children and mark the root read-only.
930d7f687fcSJeff Roberson  */
931d7f687fcSJeff Roberson static void
932d7f687fcSJeff Roberson cpuset_init(void *arg)
933d7f687fcSJeff Roberson {
934d7f687fcSJeff Roberson 	cpuset_t mask;
935d7f687fcSJeff Roberson 
93671a19bdcSAttilio Rao 	mask = all_cpus;
937d7f687fcSJeff Roberson 	if (cpuset_modify(cpuset_zero, &mask))
938d7f687fcSJeff Roberson 		panic("Can't set initial cpuset mask.\n");
939d7f687fcSJeff Roberson 	cpuset_zero->cs_flags |= CPU_SET_RDONLY;
940d7f687fcSJeff Roberson }
941d7f687fcSJeff Roberson SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
942d7f687fcSJeff Roberson 
943d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
944d7f687fcSJeff Roberson struct cpuset_args {
945d7f687fcSJeff Roberson 	cpusetid_t	*setid;
946d7f687fcSJeff Roberson };
947d7f687fcSJeff Roberson #endif
948d7f687fcSJeff Roberson int
9498451d0ddSKip Macy sys_cpuset(struct thread *td, struct cpuset_args *uap)
950d7f687fcSJeff Roberson {
951d7f687fcSJeff Roberson 	struct cpuset *root;
952d7f687fcSJeff Roberson 	struct cpuset *set;
953d7f687fcSJeff Roberson 	int error;
954d7f687fcSJeff Roberson 
955d7f687fcSJeff Roberson 	thread_lock(td);
956a03ee000SJeff Roberson 	root = cpuset_refroot(td->td_cpuset);
957d7f687fcSJeff Roberson 	thread_unlock(td);
958d7f687fcSJeff Roberson 	error = cpuset_create(&set, root, &root->cs_mask);
959d7f687fcSJeff Roberson 	cpuset_rel(root);
960d7f687fcSJeff Roberson 	if (error)
961d7f687fcSJeff Roberson 		return (error);
962d7f687fcSJeff Roberson 	error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
963a03ee000SJeff Roberson 	if (error == 0)
964a03ee000SJeff Roberson 		error = cpuset_setproc(-1, set, NULL);
965d7f687fcSJeff Roberson 	cpuset_rel(set);
966d7f687fcSJeff Roberson 	return (error);
967d7f687fcSJeff Roberson }
968d7f687fcSJeff Roberson 
969d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
970d7f687fcSJeff Roberson struct cpuset_setid_args {
971d7f687fcSJeff Roberson 	cpuwhich_t	which;
972d7f687fcSJeff Roberson 	id_t		id;
973d7f687fcSJeff Roberson 	cpusetid_t	setid;
974d7f687fcSJeff Roberson };
975d7f687fcSJeff Roberson #endif
976d7f687fcSJeff Roberson int
9778451d0ddSKip Macy sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
978d7f687fcSJeff Roberson {
979*ea2ebdc1SEdward Tomasz Napierala 
980*ea2ebdc1SEdward Tomasz Napierala 	return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid));
981*ea2ebdc1SEdward Tomasz Napierala }
982*ea2ebdc1SEdward Tomasz Napierala 
983*ea2ebdc1SEdward Tomasz Napierala int
984*ea2ebdc1SEdward Tomasz Napierala kern_cpuset_setid(struct thread *td, cpuwhich_t which,
985*ea2ebdc1SEdward Tomasz Napierala     id_t id, cpusetid_t setid)
986*ea2ebdc1SEdward Tomasz Napierala {
987d7f687fcSJeff Roberson 	struct cpuset *set;
988d7f687fcSJeff Roberson 	int error;
989d7f687fcSJeff Roberson 
990d7f687fcSJeff Roberson 	/*
991d7f687fcSJeff Roberson 	 * Presently we only support per-process sets.
992d7f687fcSJeff Roberson 	 */
993*ea2ebdc1SEdward Tomasz Napierala 	if (which != CPU_WHICH_PID)
994d7f687fcSJeff Roberson 		return (EINVAL);
995*ea2ebdc1SEdward Tomasz Napierala 	set = cpuset_lookup(setid, td);
996d7f687fcSJeff Roberson 	if (set == NULL)
997d7f687fcSJeff Roberson 		return (ESRCH);
998*ea2ebdc1SEdward Tomasz Napierala 	error = cpuset_setproc(id, set, NULL);
999d7f687fcSJeff Roberson 	cpuset_rel(set);
1000d7f687fcSJeff Roberson 	return (error);
1001d7f687fcSJeff Roberson }
1002d7f687fcSJeff Roberson 
1003d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1004d7f687fcSJeff Roberson struct cpuset_getid_args {
1005d7f687fcSJeff Roberson 	cpulevel_t	level;
1006d7f687fcSJeff Roberson 	cpuwhich_t	which;
1007d7f687fcSJeff Roberson 	id_t		id;
1008d7f687fcSJeff Roberson 	cpusetid_t	*setid;
10092b69bb1fSKevin Lo };
1010d7f687fcSJeff Roberson #endif
1011d7f687fcSJeff Roberson int
10128451d0ddSKip Macy sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
1013d7f687fcSJeff Roberson {
1014*ea2ebdc1SEdward Tomasz Napierala 
1015*ea2ebdc1SEdward Tomasz Napierala 	return (kern_cpuset_getid(td, uap->level, uap->which, uap->id,
1016*ea2ebdc1SEdward Tomasz Napierala 	    uap->setid));
1017*ea2ebdc1SEdward Tomasz Napierala }
1018*ea2ebdc1SEdward Tomasz Napierala 
1019*ea2ebdc1SEdward Tomasz Napierala int
1020*ea2ebdc1SEdward Tomasz Napierala kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which,
1021*ea2ebdc1SEdward Tomasz Napierala     id_t id, cpusetid_t *setid)
1022*ea2ebdc1SEdward Tomasz Napierala {
1023d7f687fcSJeff Roberson 	struct cpuset *nset;
1024d7f687fcSJeff Roberson 	struct cpuset *set;
1025d7f687fcSJeff Roberson 	struct thread *ttd;
1026d7f687fcSJeff Roberson 	struct proc *p;
1027*ea2ebdc1SEdward Tomasz Napierala 	cpusetid_t tmpid;
1028d7f687fcSJeff Roberson 	int error;
1029d7f687fcSJeff Roberson 
1030*ea2ebdc1SEdward Tomasz Napierala 	if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET)
1031d7f687fcSJeff Roberson 		return (EINVAL);
1032*ea2ebdc1SEdward Tomasz Napierala 	error = cpuset_which(which, id, &p, &ttd, &set);
1033d7f687fcSJeff Roberson 	if (error)
1034d7f687fcSJeff Roberson 		return (error);
1035*ea2ebdc1SEdward Tomasz Napierala 	switch (which) {
1036d7f687fcSJeff Roberson 	case CPU_WHICH_TID:
1037d7f687fcSJeff Roberson 	case CPU_WHICH_PID:
1038d7f687fcSJeff Roberson 		thread_lock(ttd);
1039a03ee000SJeff Roberson 		set = cpuset_refbase(ttd->td_cpuset);
1040d7f687fcSJeff Roberson 		thread_unlock(ttd);
1041d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
1042d7f687fcSJeff Roberson 		break;
1043d7f687fcSJeff Roberson 	case CPU_WHICH_CPUSET:
1044413628a7SBjoern A. Zeeb 	case CPU_WHICH_JAIL:
1045d7f687fcSJeff Roberson 		break;
10469b33b154SJeff Roberson 	case CPU_WHICH_IRQ:
1047c0ae6688SJohn Baldwin 	case CPU_WHICH_DOMAIN:
10489b33b154SJeff Roberson 		return (EINVAL);
1049d7f687fcSJeff Roberson 	}
1050*ea2ebdc1SEdward Tomasz Napierala 	switch (level) {
1051d7f687fcSJeff Roberson 	case CPU_LEVEL_ROOT:
1052a03ee000SJeff Roberson 		nset = cpuset_refroot(set);
1053d7f687fcSJeff Roberson 		cpuset_rel(set);
1054d7f687fcSJeff Roberson 		set = nset;
1055d7f687fcSJeff Roberson 		break;
1056d7f687fcSJeff Roberson 	case CPU_LEVEL_CPUSET:
1057d7f687fcSJeff Roberson 		break;
1058d7f687fcSJeff Roberson 	case CPU_LEVEL_WHICH:
1059d7f687fcSJeff Roberson 		break;
1060d7f687fcSJeff Roberson 	}
1061*ea2ebdc1SEdward Tomasz Napierala 	tmpid = set->cs_id;
1062d7f687fcSJeff Roberson 	cpuset_rel(set);
1063d7f687fcSJeff Roberson 	if (error == 0)
1064*ea2ebdc1SEdward Tomasz Napierala 		error = copyout(&tmpid, setid, sizeof(id));
1065d7f687fcSJeff Roberson 
1066d7f687fcSJeff Roberson 	return (error);
1067d7f687fcSJeff Roberson }
1068d7f687fcSJeff Roberson 
1069d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1070d7f687fcSJeff Roberson struct cpuset_getaffinity_args {
1071d7f687fcSJeff Roberson 	cpulevel_t	level;
1072d7f687fcSJeff Roberson 	cpuwhich_t	which;
10737f64829aSRuslan Ermilov 	id_t		id;
10747f64829aSRuslan Ermilov 	size_t		cpusetsize;
10757f64829aSRuslan Ermilov 	cpuset_t	*mask;
1076d7f687fcSJeff Roberson };
1077d7f687fcSJeff Roberson #endif
1078d7f687fcSJeff Roberson int
10798451d0ddSKip Macy sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1080d7f687fcSJeff Roberson {
1081d7f687fcSJeff Roberson 	struct thread *ttd;
1082d7f687fcSJeff Roberson 	struct cpuset *nset;
1083d7f687fcSJeff Roberson 	struct cpuset *set;
1084d7f687fcSJeff Roberson 	struct proc *p;
1085d7f687fcSJeff Roberson 	cpuset_t *mask;
1086d7f687fcSJeff Roberson 	int error;
10877f64829aSRuslan Ermilov 	size_t size;
1088d7f687fcSJeff Roberson 
108973c40187SJeff Roberson 	if (uap->cpusetsize < sizeof(cpuset_t) ||
1090887aedc6SKonstantin Belousov 	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
1091d7f687fcSJeff Roberson 		return (ERANGE);
109273c40187SJeff Roberson 	size = uap->cpusetsize;
1093d7f687fcSJeff Roberson 	mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1094d7f687fcSJeff Roberson 	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1095d7f687fcSJeff Roberson 	if (error)
1096d7f687fcSJeff Roberson 		goto out;
1097d7f687fcSJeff Roberson 	switch (uap->level) {
1098d7f687fcSJeff Roberson 	case CPU_LEVEL_ROOT:
1099d7f687fcSJeff Roberson 	case CPU_LEVEL_CPUSET:
1100d7f687fcSJeff Roberson 		switch (uap->which) {
1101d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
1102d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
1103d7f687fcSJeff Roberson 			thread_lock(ttd);
1104d7f687fcSJeff Roberson 			set = cpuset_ref(ttd->td_cpuset);
1105d7f687fcSJeff Roberson 			thread_unlock(ttd);
1106d7f687fcSJeff Roberson 			break;
1107d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
1108413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
1109d7f687fcSJeff Roberson 			break;
11109b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
1111c0ae6688SJohn Baldwin 		case CPU_WHICH_DOMAIN:
11129b33b154SJeff Roberson 			error = EINVAL;
11139b33b154SJeff Roberson 			goto out;
1114d7f687fcSJeff Roberson 		}
1115d7f687fcSJeff Roberson 		if (uap->level == CPU_LEVEL_ROOT)
1116a03ee000SJeff Roberson 			nset = cpuset_refroot(set);
1117d7f687fcSJeff Roberson 		else
1118a03ee000SJeff Roberson 			nset = cpuset_refbase(set);
1119d7f687fcSJeff Roberson 		CPU_COPY(&nset->cs_mask, mask);
1120d7f687fcSJeff Roberson 		cpuset_rel(nset);
1121d7f687fcSJeff Roberson 		break;
1122d7f687fcSJeff Roberson 	case CPU_LEVEL_WHICH:
1123d7f687fcSJeff Roberson 		switch (uap->which) {
1124d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
1125d7f687fcSJeff Roberson 			thread_lock(ttd);
1126d7f687fcSJeff Roberson 			CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1127d7f687fcSJeff Roberson 			thread_unlock(ttd);
1128d7f687fcSJeff Roberson 			break;
1129d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
1130d7f687fcSJeff Roberson 			FOREACH_THREAD_IN_PROC(p, ttd) {
1131d7f687fcSJeff Roberson 				thread_lock(ttd);
1132d7f687fcSJeff Roberson 				CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1133d7f687fcSJeff Roberson 				thread_unlock(ttd);
1134d7f687fcSJeff Roberson 			}
1135d7f687fcSJeff Roberson 			break;
1136d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
1137413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
1138d7f687fcSJeff Roberson 			CPU_COPY(&set->cs_mask, mask);
1139d7f687fcSJeff Roberson 			break;
11409b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
11419b33b154SJeff Roberson 			error = intr_getaffinity(uap->id, mask);
11429b33b154SJeff Roberson 			break;
1143c0ae6688SJohn Baldwin 		case CPU_WHICH_DOMAIN:
114462d70a81SJohn Baldwin 			if (uap->id < 0 || uap->id >= MAXMEMDOM)
1145c0ae6688SJohn Baldwin 				error = ESRCH;
1146c0ae6688SJohn Baldwin 			else
1147c0ae6688SJohn Baldwin 				CPU_COPY(&cpuset_domain[uap->id], mask);
1148c0ae6688SJohn Baldwin 			break;
1149d7f687fcSJeff Roberson 		}
1150d7f687fcSJeff Roberson 		break;
1151d7f687fcSJeff Roberson 	default:
1152d7f687fcSJeff Roberson 		error = EINVAL;
1153d7f687fcSJeff Roberson 		break;
1154d7f687fcSJeff Roberson 	}
1155d7f687fcSJeff Roberson 	if (set)
1156d7f687fcSJeff Roberson 		cpuset_rel(set);
1157d7f687fcSJeff Roberson 	if (p)
1158d7f687fcSJeff Roberson 		PROC_UNLOCK(p);
1159d7f687fcSJeff Roberson 	if (error == 0)
1160d7f687fcSJeff Roberson 		error = copyout(mask, uap->mask, size);
1161d7f687fcSJeff Roberson out:
1162d7f687fcSJeff Roberson 	free(mask, M_TEMP);
1163d7f687fcSJeff Roberson 	return (error);
1164d7f687fcSJeff Roberson }
1165d7f687fcSJeff Roberson 
1166d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_
1167d7f687fcSJeff Roberson struct cpuset_setaffinity_args {
1168d7f687fcSJeff Roberson 	cpulevel_t	level;
1169d7f687fcSJeff Roberson 	cpuwhich_t	which;
11707f64829aSRuslan Ermilov 	id_t		id;
11717f64829aSRuslan Ermilov 	size_t		cpusetsize;
11727f64829aSRuslan Ermilov 	const cpuset_t	*mask;
1173d7f687fcSJeff Roberson };
1174d7f687fcSJeff Roberson #endif
1175d7f687fcSJeff Roberson int
11768451d0ddSKip Macy sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1177d7f687fcSJeff Roberson {
1178d7f687fcSJeff Roberson 	struct cpuset *nset;
1179d7f687fcSJeff Roberson 	struct cpuset *set;
1180d7f687fcSJeff Roberson 	struct thread *ttd;
1181d7f687fcSJeff Roberson 	struct proc *p;
1182d7f687fcSJeff Roberson 	cpuset_t *mask;
1183d7f687fcSJeff Roberson 	int error;
1184d7f687fcSJeff Roberson 
118573c40187SJeff Roberson 	if (uap->cpusetsize < sizeof(cpuset_t) ||
1186887aedc6SKonstantin Belousov 	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
1187d7f687fcSJeff Roberson 		return (ERANGE);
118873c40187SJeff Roberson 	mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
118973c40187SJeff Roberson 	error = copyin(uap->mask, mask, uap->cpusetsize);
1190d7f687fcSJeff Roberson 	if (error)
1191d7f687fcSJeff Roberson 		goto out;
119273c40187SJeff Roberson 	/*
119373c40187SJeff Roberson 	 * Verify that no high bits are set.
119473c40187SJeff Roberson 	 */
119573c40187SJeff Roberson 	if (uap->cpusetsize > sizeof(cpuset_t)) {
119673c40187SJeff Roberson 		char *end;
119773c40187SJeff Roberson 		char *cp;
119873c40187SJeff Roberson 
119973c40187SJeff Roberson 		end = cp = (char *)&mask->__bits;
120073c40187SJeff Roberson 		end += uap->cpusetsize;
120173c40187SJeff Roberson 		cp += sizeof(cpuset_t);
120273c40187SJeff Roberson 		while (cp != end)
120373c40187SJeff Roberson 			if (*cp++ != 0) {
120473c40187SJeff Roberson 				error = EINVAL;
120573c40187SJeff Roberson 				goto out;
120673c40187SJeff Roberson 			}
120773c40187SJeff Roberson 
120873c40187SJeff Roberson 	}
1209d7f687fcSJeff Roberson 	switch (uap->level) {
1210d7f687fcSJeff Roberson 	case CPU_LEVEL_ROOT:
1211d7f687fcSJeff Roberson 	case CPU_LEVEL_CPUSET:
1212d7f687fcSJeff Roberson 		error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1213d7f687fcSJeff Roberson 		if (error)
1214d7f687fcSJeff Roberson 			break;
1215d7f687fcSJeff Roberson 		switch (uap->which) {
1216d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
1217d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
1218d7f687fcSJeff Roberson 			thread_lock(ttd);
1219d7f687fcSJeff Roberson 			set = cpuset_ref(ttd->td_cpuset);
1220d7f687fcSJeff Roberson 			thread_unlock(ttd);
1221c6440f72SJeff Roberson 			PROC_UNLOCK(p);
1222d7f687fcSJeff Roberson 			break;
1223d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
1224413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
1225d7f687fcSJeff Roberson 			break;
12269b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
1227c0ae6688SJohn Baldwin 		case CPU_WHICH_DOMAIN:
12289b33b154SJeff Roberson 			error = EINVAL;
12299b33b154SJeff Roberson 			goto out;
1230d7f687fcSJeff Roberson 		}
1231d7f687fcSJeff Roberson 		if (uap->level == CPU_LEVEL_ROOT)
1232a03ee000SJeff Roberson 			nset = cpuset_refroot(set);
1233d7f687fcSJeff Roberson 		else
1234a03ee000SJeff Roberson 			nset = cpuset_refbase(set);
1235d7f687fcSJeff Roberson 		error = cpuset_modify(nset, mask);
1236d7f687fcSJeff Roberson 		cpuset_rel(nset);
1237d7f687fcSJeff Roberson 		cpuset_rel(set);
1238d7f687fcSJeff Roberson 		break;
1239d7f687fcSJeff Roberson 	case CPU_LEVEL_WHICH:
1240d7f687fcSJeff Roberson 		switch (uap->which) {
1241d7f687fcSJeff Roberson 		case CPU_WHICH_TID:
1242d7f687fcSJeff Roberson 			error = cpuset_setthread(uap->id, mask);
1243d7f687fcSJeff Roberson 			break;
1244d7f687fcSJeff Roberson 		case CPU_WHICH_PID:
1245d7f687fcSJeff Roberson 			error = cpuset_setproc(uap->id, NULL, mask);
1246d7f687fcSJeff Roberson 			break;
1247d7f687fcSJeff Roberson 		case CPU_WHICH_CPUSET:
1248413628a7SBjoern A. Zeeb 		case CPU_WHICH_JAIL:
1249413628a7SBjoern A. Zeeb 			error = cpuset_which(uap->which, uap->id, &p,
1250d7f687fcSJeff Roberson 			    &ttd, &set);
1251d7f687fcSJeff Roberson 			if (error == 0) {
1252d7f687fcSJeff Roberson 				error = cpuset_modify(set, mask);
1253d7f687fcSJeff Roberson 				cpuset_rel(set);
1254d7f687fcSJeff Roberson 			}
1255d7f687fcSJeff Roberson 			break;
12569b33b154SJeff Roberson 		case CPU_WHICH_IRQ:
12579b33b154SJeff Roberson 			error = intr_setaffinity(uap->id, mask);
12589b33b154SJeff Roberson 			break;
1259d7f687fcSJeff Roberson 		default:
1260d7f687fcSJeff Roberson 			error = EINVAL;
1261d7f687fcSJeff Roberson 			break;
1262d7f687fcSJeff Roberson 		}
1263d7f687fcSJeff Roberson 		break;
1264d7f687fcSJeff Roberson 	default:
1265d7f687fcSJeff Roberson 		error = EINVAL;
1266d7f687fcSJeff Roberson 		break;
1267d7f687fcSJeff Roberson 	}
1268d7f687fcSJeff Roberson out:
1269d7f687fcSJeff Roberson 	free(mask, M_TEMP);
1270d7f687fcSJeff Roberson 	return (error);
1271d7f687fcSJeff Roberson }
1272dea0ed66SBjoern A. Zeeb 
1273dea0ed66SBjoern A. Zeeb #ifdef DDB
1274cd32bd7aSJohn Baldwin void
1275cd32bd7aSJohn Baldwin ddb_display_cpuset(const cpuset_t *set)
1276dea0ed66SBjoern A. Zeeb {
1277dea0ed66SBjoern A. Zeeb 	int cpu, once;
1278dea0ed66SBjoern A. Zeeb 
1279dea0ed66SBjoern A. Zeeb 	for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1280cd32bd7aSJohn Baldwin 		if (CPU_ISSET(cpu, set)) {
1281dea0ed66SBjoern A. Zeeb 			if (once == 0) {
1282dea0ed66SBjoern A. Zeeb 				db_printf("%d", cpu);
1283dea0ed66SBjoern A. Zeeb 				once = 1;
1284dea0ed66SBjoern A. Zeeb 			} else
1285dea0ed66SBjoern A. Zeeb 				db_printf(",%d", cpu);
1286dea0ed66SBjoern A. Zeeb 		}
1287dea0ed66SBjoern A. Zeeb 	}
1288cd32bd7aSJohn Baldwin 	if (once == 0)
1289cd32bd7aSJohn Baldwin 		db_printf("<none>");
1290cd32bd7aSJohn Baldwin }
1291cd32bd7aSJohn Baldwin 
1292cd32bd7aSJohn Baldwin DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1293cd32bd7aSJohn Baldwin {
1294cd32bd7aSJohn Baldwin 	struct cpuset *set;
1295cd32bd7aSJohn Baldwin 
1296cd32bd7aSJohn Baldwin 	LIST_FOREACH(set, &cpuset_ids, cs_link) {
1297cd32bd7aSJohn Baldwin 		db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1298cd32bd7aSJohn Baldwin 		    set, set->cs_id, set->cs_ref, set->cs_flags,
1299cd32bd7aSJohn Baldwin 		    (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1300cd32bd7aSJohn Baldwin 		db_printf("  mask=");
1301cd32bd7aSJohn Baldwin 		ddb_display_cpuset(&set->cs_mask);
1302dea0ed66SBjoern A. Zeeb 		db_printf("\n");
1303dea0ed66SBjoern A. Zeeb 		if (db_pager_quit)
1304dea0ed66SBjoern A. Zeeb 			break;
1305dea0ed66SBjoern A. Zeeb 	}
1306dea0ed66SBjoern A. Zeeb }
1307dea0ed66SBjoern A. Zeeb #endif /* DDB */
1308