kern_cpuset.c revision 267716
1176730Sjeff/*-
2176730Sjeff * Copyright (c) 2008,  Jeffrey Roberson <jeff@freebsd.org>
3176730Sjeff * All rights reserved.
4177904Sjeff *
5177904Sjeff * Copyright (c) 2008 Nokia Corporation
6177904Sjeff * All rights reserved.
7176730Sjeff *
8176730Sjeff * Redistribution and use in source and binary forms, with or without
9176730Sjeff * modification, are permitted provided that the following conditions
10176730Sjeff * are met:
11176730Sjeff * 1. Redistributions of source code must retain the above copyright
12176730Sjeff *    notice unmodified, this list of conditions, and the following
13176730Sjeff *    disclaimer.
14176730Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15176730Sjeff *    notice, this list of conditions and the following disclaimer in the
16176730Sjeff *    documentation and/or other materials provided with the distribution.
17176730Sjeff *
18176730Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19176730Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20176730Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21176730Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22176730Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23176730Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24176730Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25176730Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26176730Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27176730Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28176730Sjeff *
29176730Sjeff */
30176730Sjeff
31176730Sjeff#include <sys/cdefs.h>
32176730Sjeff__FBSDID("$FreeBSD: head/sys/kern/kern_cpuset.c 267716 2014-06-22 11:32:23Z melifaro $");
33176730Sjeff
34180358Sbz#include "opt_ddb.h"
35180358Sbz
36176730Sjeff#include <sys/param.h>
37176730Sjeff#include <sys/systm.h>
38176730Sjeff#include <sys/sysproto.h>
39192895Sjamie#include <sys/jail.h>
40176730Sjeff#include <sys/kernel.h>
41176730Sjeff#include <sys/lock.h>
42176730Sjeff#include <sys/malloc.h>
43176730Sjeff#include <sys/mutex.h>
44176730Sjeff#include <sys/priv.h>
45176730Sjeff#include <sys/proc.h>
46176730Sjeff#include <sys/refcount.h>
47176730Sjeff#include <sys/sched.h>
48176730Sjeff#include <sys/smp.h>
49176730Sjeff#include <sys/syscallsubr.h>
50176730Sjeff#include <sys/cpuset.h>
51176730Sjeff#include <sys/sx.h>
52176730Sjeff#include <sys/queue.h>
53222813Sattilio#include <sys/libkern.h>
54176730Sjeff#include <sys/limits.h>
55177738Sjeff#include <sys/bus.h>
56177738Sjeff#include <sys/interrupt.h>
57176730Sjeff
58176730Sjeff#include <vm/uma.h>
59176730Sjeff
60180358Sbz#ifdef DDB
61180358Sbz#include <ddb/ddb.h>
62180358Sbz#endif /* DDB */
63180358Sbz
64176730Sjeff/*
65176730Sjeff * cpusets provide a mechanism for creating and manipulating sets of
66176730Sjeff * processors for the purpose of constraining the scheduling of threads to
67176730Sjeff * specific processors.
68176730Sjeff *
69176730Sjeff * Each process belongs to an identified set, by default this is set 1.  Each
70176730Sjeff * thread may further restrict the cpus it may run on to a subset of this
71176730Sjeff * named set.  This creates an anonymous set which other threads and processes
72176730Sjeff * may not join by number.
73176730Sjeff *
74176730Sjeff * The named set is referred to herein as the 'base' set to avoid ambiguity.
75176730Sjeff * This set is usually a child of a 'root' set while the anonymous set may
76176730Sjeff * simply be referred to as a mask.  In the syscall api these are referred to
77176730Sjeff * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
78176730Sjeff *
79176730Sjeff * Threads inherit their set from their creator whether it be anonymous or
80176730Sjeff * not.  This means that anonymous sets are immutable because they may be
81176730Sjeff * shared.  To modify an anonymous set a new set is created with the desired
82176730Sjeff * mask and the same parent as the existing anonymous set.  This gives the
83198493Sjhb * illusion of each thread having a private mask.
84176730Sjeff *
85176730Sjeff * Via the syscall apis a user may ask to retrieve or modify the root, base,
86176730Sjeff * or mask that is discovered via a pid, tid, or setid.  Modifying a set
87176730Sjeff * modifies all numbered and anonymous child sets to comply with the new mask.
88176730Sjeff * Modifying a pid or tid's mask applies only to that tid but must still
89176730Sjeff * exist within the assigned parent set.
90176730Sjeff *
91198495Sjhb * A thread may not be assigned to a group separate from other threads in
92176730Sjeff * the process.  This is to remove ambiguity when the setid is queried with
93176730Sjeff * a pid argument.  There is no other technical limitation.
94176730Sjeff *
95176730Sjeff * This somewhat complex arrangement is intended to make it easy for
96176730Sjeff * applications to query available processors and bind their threads to
97176730Sjeff * specific processors while also allowing administrators to dynamically
98176730Sjeff * reprovision by changing sets which apply to groups of processes.
99176730Sjeff *
100176730Sjeff * A simple application should not concern itself with sets at all and
101176730Sjeff * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
102198493Sjhb * meaning 'curthread'.  It may query available cpus for that tid with a
103176730Sjeff * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
104176730Sjeff */
105176730Sjeffstatic uma_zone_t cpuset_zone;
106176730Sjeffstatic struct mtx cpuset_lock;
107176730Sjeffstatic struct setlist cpuset_ids;
108176730Sjeffstatic struct unrhdr *cpuset_unr;
109267716Smelifarostatic struct cpuset *cpuset_zero, *cpuset_default;
110176730Sjeff
111214611Sdavidxu/* Return the size of cpuset_t at the kernel level */
112214611SdavidxuSYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD,
113214611Sdavidxu	0, sizeof(cpuset_t), "sizeof(cpuset_t)");
114214611Sdavidxu
115177738Sjeffcpuset_t *cpuset_root;
116177738Sjeff
117176730Sjeff/*
118176730Sjeff * Acquire a reference to a cpuset, all pointers must be tracked with refs.
119176730Sjeff */
120176730Sjeffstruct cpuset *
121176730Sjeffcpuset_ref(struct cpuset *set)
122176730Sjeff{
123176730Sjeff
124176730Sjeff	refcount_acquire(&set->cs_ref);
125176730Sjeff	return (set);
126176730Sjeff}
127176730Sjeff
128176730Sjeff/*
129180356Sbz * Walks up the tree from 'set' to find the root.  Returns the root
130180356Sbz * referenced.
131180356Sbz */
132180356Sbzstatic struct cpuset *
133180356Sbzcpuset_refroot(struct cpuset *set)
134180356Sbz{
135180356Sbz
136180356Sbz	for (; set->cs_parent != NULL; set = set->cs_parent)
137180356Sbz		if (set->cs_flags & CPU_SET_ROOT)
138180356Sbz			break;
139180356Sbz	cpuset_ref(set);
140180356Sbz
141180356Sbz	return (set);
142180356Sbz}
143180356Sbz
144180356Sbz/*
145180356Sbz * Find the first non-anonymous set starting from 'set'.  Returns this set
146180356Sbz * referenced.  May return the passed in set with an extra ref if it is
147180356Sbz * not anonymous.
148180356Sbz */
149180356Sbzstatic struct cpuset *
150180356Sbzcpuset_refbase(struct cpuset *set)
151180356Sbz{
152180356Sbz
153180356Sbz	if (set->cs_id == CPUSET_INVALID)
154180356Sbz		set = set->cs_parent;
155180356Sbz	cpuset_ref(set);
156180356Sbz
157180356Sbz	return (set);
158180356Sbz}
159180356Sbz
160180356Sbz/*
161198493Sjhb * Release a reference in a context where it is safe to allocate.
162176730Sjeff */
163176730Sjeffvoid
164176730Sjeffcpuset_rel(struct cpuset *set)
165176730Sjeff{
166176730Sjeff	cpusetid_t id;
167176730Sjeff
168176730Sjeff	if (refcount_release(&set->cs_ref) == 0)
169176730Sjeff		return;
170176730Sjeff	mtx_lock_spin(&cpuset_lock);
171176730Sjeff	LIST_REMOVE(set, cs_siblings);
172176730Sjeff	id = set->cs_id;
173176730Sjeff	if (id != CPUSET_INVALID)
174176730Sjeff		LIST_REMOVE(set, cs_link);
175176730Sjeff	mtx_unlock_spin(&cpuset_lock);
176176730Sjeff	cpuset_rel(set->cs_parent);
177176730Sjeff	uma_zfree(cpuset_zone, set);
178176730Sjeff	if (id != CPUSET_INVALID)
179176730Sjeff		free_unr(cpuset_unr, id);
180176730Sjeff}
181176730Sjeff
182176730Sjeff/*
183176730Sjeff * Deferred release must be used when in a context that is not safe to
184176730Sjeff * allocate/free.  This places any unreferenced sets on the list 'head'.
185176730Sjeff */
186176730Sjeffstatic void
187176730Sjeffcpuset_rel_defer(struct setlist *head, struct cpuset *set)
188176730Sjeff{
189176730Sjeff
190176730Sjeff	if (refcount_release(&set->cs_ref) == 0)
191176730Sjeff		return;
192176730Sjeff	mtx_lock_spin(&cpuset_lock);
193176730Sjeff	LIST_REMOVE(set, cs_siblings);
194176730Sjeff	if (set->cs_id != CPUSET_INVALID)
195176730Sjeff		LIST_REMOVE(set, cs_link);
196176730Sjeff	LIST_INSERT_HEAD(head, set, cs_link);
197176730Sjeff	mtx_unlock_spin(&cpuset_lock);
198176730Sjeff}
199176730Sjeff
200176730Sjeff/*
201176730Sjeff * Complete a deferred release.  Removes the set from the list provided to
202176730Sjeff * cpuset_rel_defer.
203176730Sjeff */
204176730Sjeffstatic void
205176730Sjeffcpuset_rel_complete(struct cpuset *set)
206176730Sjeff{
207176730Sjeff	LIST_REMOVE(set, cs_link);
208176730Sjeff	cpuset_rel(set->cs_parent);
209176730Sjeff	uma_zfree(cpuset_zone, set);
210176730Sjeff}
211176730Sjeff
212176730Sjeff/*
213176730Sjeff * Find a set based on an id.  Returns it with a ref.
214176730Sjeff */
215176730Sjeffstatic struct cpuset *
216185435Sbzcpuset_lookup(cpusetid_t setid, struct thread *td)
217176730Sjeff{
218176730Sjeff	struct cpuset *set;
219176730Sjeff
220176730Sjeff	if (setid == CPUSET_INVALID)
221176730Sjeff		return (NULL);
222176730Sjeff	mtx_lock_spin(&cpuset_lock);
223176730Sjeff	LIST_FOREACH(set, &cpuset_ids, cs_link)
224176730Sjeff		if (set->cs_id == setid)
225176730Sjeff			break;
226176730Sjeff	if (set)
227176730Sjeff		cpuset_ref(set);
228176730Sjeff	mtx_unlock_spin(&cpuset_lock);
229185435Sbz
230185435Sbz	KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
231185435Sbz	if (set != NULL && jailed(td->td_ucred)) {
232192895Sjamie		struct cpuset *jset, *tset;
233185435Sbz
234192895Sjamie		jset = td->td_ucred->cr_prison->pr_cpuset;
235192895Sjamie		for (tset = set; tset != NULL; tset = tset->cs_parent)
236192895Sjamie			if (tset == jset)
237192895Sjamie				break;
238192895Sjamie		if (tset == NULL) {
239185435Sbz			cpuset_rel(set);
240185435Sbz			set = NULL;
241185435Sbz		}
242185435Sbz	}
243185435Sbz
244176730Sjeff	return (set);
245176730Sjeff}
246176730Sjeff
247176730Sjeff/*
248176730Sjeff * Create a set in the space provided in 'set' with the provided parameters.
249176730Sjeff * The set is returned with a single ref.  May return EDEADLK if the set
250176730Sjeff * will have no valid cpu based on restrictions from the parent.
251176730Sjeff */
252176730Sjeffstatic int
253219399Sjhb_cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
254176730Sjeff    cpusetid_t id)
255176730Sjeff{
256176730Sjeff
257176811Sjeff	if (!CPU_OVERLAP(&parent->cs_mask, mask))
258176811Sjeff		return (EDEADLK);
259176730Sjeff	CPU_COPY(mask, &set->cs_mask);
260176730Sjeff	LIST_INIT(&set->cs_children);
261176730Sjeff	refcount_init(&set->cs_ref, 1);
262176730Sjeff	set->cs_flags = 0;
263176730Sjeff	mtx_lock_spin(&cpuset_lock);
264219399Sjhb	CPU_AND(&set->cs_mask, &parent->cs_mask);
265176811Sjeff	set->cs_id = id;
266176811Sjeff	set->cs_parent = cpuset_ref(parent);
267176811Sjeff	LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
268176811Sjeff	if (set->cs_id != CPUSET_INVALID)
269176811Sjeff		LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
270176730Sjeff	mtx_unlock_spin(&cpuset_lock);
271176730Sjeff
272176811Sjeff	return (0);
273176730Sjeff}
274176730Sjeff
275176730Sjeff/*
276176730Sjeff * Create a new non-anonymous set with the requested parent and mask.  May
277176730Sjeff * return failures if the mask is invalid or a new number can not be
278176730Sjeff * allocated.
279176730Sjeff */
280176730Sjeffstatic int
281219399Sjhbcpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
282176730Sjeff{
283176730Sjeff	struct cpuset *set;
284176730Sjeff	cpusetid_t id;
285176730Sjeff	int error;
286176730Sjeff
287176730Sjeff	id = alloc_unr(cpuset_unr);
288176730Sjeff	if (id == -1)
289176730Sjeff		return (ENFILE);
290176730Sjeff	*setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
291176730Sjeff	error = _cpuset_create(set, parent, mask, id);
292176730Sjeff	if (error == 0)
293176730Sjeff		return (0);
294176730Sjeff	free_unr(cpuset_unr, id);
295176730Sjeff	uma_zfree(cpuset_zone, set);
296176730Sjeff
297176730Sjeff	return (error);
298176730Sjeff}
299176730Sjeff
300176730Sjeff/*
301176730Sjeff * Recursively check for errors that would occur from applying mask to
302176730Sjeff * the tree of sets starting at 'set'.  Checks for sets that would become
303176730Sjeff * empty as well as RDONLY flags.
304176730Sjeff */
305176730Sjeffstatic int
306251470Sjhbcpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
307176730Sjeff{
308176730Sjeff	struct cpuset *nset;
309176730Sjeff	cpuset_t newmask;
310176730Sjeff	int error;
311176730Sjeff
312176730Sjeff	mtx_assert(&cpuset_lock, MA_OWNED);
313176730Sjeff	if (set->cs_flags & CPU_SET_RDONLY)
314176730Sjeff		return (EPERM);
315251470Sjhb	if (check_mask) {
316251470Sjhb		if (!CPU_OVERLAP(&set->cs_mask, mask))
317251470Sjhb			return (EDEADLK);
318251470Sjhb		CPU_COPY(&set->cs_mask, &newmask);
319251470Sjhb		CPU_AND(&newmask, mask);
320251470Sjhb	} else
321251470Sjhb		CPU_COPY(mask, &newmask);
322176811Sjeff	error = 0;
323176730Sjeff	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
324251470Sjhb		if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
325176730Sjeff			break;
326176730Sjeff	return (error);
327176730Sjeff}
328176730Sjeff
329176730Sjeff/*
330176730Sjeff * Applies the mask 'mask' without checking for empty sets or permissions.
331176730Sjeff */
332176730Sjeffstatic void
333176730Sjeffcpuset_update(struct cpuset *set, cpuset_t *mask)
334176730Sjeff{
335176730Sjeff	struct cpuset *nset;
336176730Sjeff
337176730Sjeff	mtx_assert(&cpuset_lock, MA_OWNED);
338176730Sjeff	CPU_AND(&set->cs_mask, mask);
339176730Sjeff	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
340176730Sjeff		cpuset_update(nset, &set->cs_mask);
341176730Sjeff
342176730Sjeff	return;
343176730Sjeff}
344176730Sjeff
345176730Sjeff/*
346176730Sjeff * Modify the set 'set' to use a copy of the mask provided.  Apply this new
347176730Sjeff * mask to restrict all children in the tree.  Checks for validity before
348176730Sjeff * applying the changes.
349176730Sjeff */
350176730Sjeffstatic int
351176730Sjeffcpuset_modify(struct cpuset *set, cpuset_t *mask)
352176730Sjeff{
353176811Sjeff	struct cpuset *root;
354176730Sjeff	int error;
355176730Sjeff
356180098Sbz	error = priv_check(curthread, PRIV_SCHED_CPUSET);
357176730Sjeff	if (error)
358176730Sjeff		return (error);
359176811Sjeff	/*
360191639Sbz	 * In case we are called from within the jail
361191639Sbz	 * we do not allow modifying the dedicated root
362191639Sbz	 * cpuset of the jail but may still allow to
363191639Sbz	 * change child sets.
364191639Sbz	 */
365191639Sbz	if (jailed(curthread->td_ucred) &&
366191639Sbz	    set->cs_flags & CPU_SET_ROOT)
367191639Sbz		return (EPERM);
368191639Sbz	/*
369176811Sjeff	 * Verify that we have access to this set of
370176811Sjeff	 * cpus.
371176811Sjeff	 */
372176811Sjeff	root = set->cs_parent;
373176811Sjeff	if (root && !CPU_SUBSET(&root->cs_mask, mask))
374176811Sjeff		return (EINVAL);
375176730Sjeff	mtx_lock_spin(&cpuset_lock);
376251470Sjhb	error = cpuset_testupdate(set, mask, 0);
377176730Sjeff	if (error)
378176730Sjeff		goto out;
379251470Sjhb	CPU_COPY(mask, &set->cs_mask);
380176730Sjeff	cpuset_update(set, mask);
381176730Sjeffout:
382176730Sjeff	mtx_unlock_spin(&cpuset_lock);
383176730Sjeff
384176730Sjeff	return (error);
385176730Sjeff}
386176730Sjeff
387176730Sjeff/*
388176730Sjeff * Resolve the 'which' parameter of several cpuset apis.
389176730Sjeff *
390176730Sjeff * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid.  Also
391176730Sjeff * checks for permission via p_cansched().
392176730Sjeff *
393176730Sjeff * For WHICH_SET returns a valid set with a new reference.
394176730Sjeff *
395176730Sjeff * -1 may be supplied for any argument to mean the current proc/thread or
396176730Sjeff * the base set of the current thread.  May fail with ESRCH/EPERM.
397176730Sjeff */
398176730Sjeffstatic int
399176730Sjeffcpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
400176730Sjeff    struct cpuset **setp)
401176730Sjeff{
402176730Sjeff	struct cpuset *set;
403176730Sjeff	struct thread *td;
404176730Sjeff	struct proc *p;
405176730Sjeff	int error;
406176730Sjeff
407176730Sjeff	*pp = p = NULL;
408176730Sjeff	*tdp = td = NULL;
409176730Sjeff	*setp = set = NULL;
410176730Sjeff	switch (which) {
411176730Sjeff	case CPU_WHICH_PID:
412176730Sjeff		if (id == -1) {
413176730Sjeff			PROC_LOCK(curproc);
414176730Sjeff			p = curproc;
415176730Sjeff			break;
416176730Sjeff		}
417176730Sjeff		if ((p = pfind(id)) == NULL)
418176730Sjeff			return (ESRCH);
419176730Sjeff		break;
420176730Sjeff	case CPU_WHICH_TID:
421176730Sjeff		if (id == -1) {
422176730Sjeff			PROC_LOCK(curproc);
423176730Sjeff			p = curproc;
424176730Sjeff			td = curthread;
425176730Sjeff			break;
426176730Sjeff		}
427214337Sdavidxu		td = tdfind(id, -1);
428176730Sjeff		if (td == NULL)
429176730Sjeff			return (ESRCH);
430214337Sdavidxu		p = td->td_proc;
431176730Sjeff		break;
432176730Sjeff	case CPU_WHICH_CPUSET:
433176730Sjeff		if (id == -1) {
434176730Sjeff			thread_lock(curthread);
435177738Sjeff			set = cpuset_refbase(curthread->td_cpuset);
436176730Sjeff			thread_unlock(curthread);
437176730Sjeff		} else
438185435Sbz			set = cpuset_lookup(id, curthread);
439176730Sjeff		if (set) {
440176730Sjeff			*setp = set;
441176730Sjeff			return (0);
442176730Sjeff		}
443176730Sjeff		return (ESRCH);
444185435Sbz	case CPU_WHICH_JAIL:
445185435Sbz	{
446185435Sbz		/* Find `set' for prison with given id. */
447185435Sbz		struct prison *pr;
448185435Sbz
449185435Sbz		sx_slock(&allprison_lock);
450192895Sjamie		pr = prison_find_child(curthread->td_ucred->cr_prison, id);
451185435Sbz		sx_sunlock(&allprison_lock);
452185435Sbz		if (pr == NULL)
453185435Sbz			return (ESRCH);
454192895Sjamie		cpuset_ref(pr->pr_cpuset);
455192895Sjamie		*setp = pr->pr_cpuset;
456185435Sbz		mtx_unlock(&pr->pr_mtx);
457192895Sjamie		return (0);
458185435Sbz	}
459178092Sjeff	case CPU_WHICH_IRQ:
460178092Sjeff		return (0);
461176730Sjeff	default:
462176730Sjeff		return (EINVAL);
463176730Sjeff	}
464176730Sjeff	error = p_cansched(curthread, p);
465176730Sjeff	if (error) {
466176730Sjeff		PROC_UNLOCK(p);
467176730Sjeff		return (error);
468176730Sjeff	}
469176730Sjeff	if (td == NULL)
470176730Sjeff		td = FIRST_THREAD_IN_PROC(p);
471176730Sjeff	*pp = p;
472176730Sjeff	*tdp = td;
473176730Sjeff	return (0);
474176730Sjeff}
475176730Sjeff
476176730Sjeff/*
477176730Sjeff * Create an anonymous set with the provided mask in the space provided by
478176730Sjeff * 'fset'.  If the passed in set is anonymous we use its parent otherwise
479176730Sjeff * the new set is a child of 'set'.
480176730Sjeff */
481176730Sjeffstatic int
482219399Sjhbcpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
483176730Sjeff{
484176730Sjeff	struct cpuset *parent;
485176730Sjeff
486176730Sjeff	if (set->cs_id == CPUSET_INVALID)
487176730Sjeff		parent = set->cs_parent;
488176730Sjeff	else
489176730Sjeff		parent = set;
490176811Sjeff	if (!CPU_SUBSET(&parent->cs_mask, mask))
491177738Sjeff		return (EDEADLK);
492176730Sjeff	return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
493176730Sjeff}
494176730Sjeff
495176730Sjeff/*
496176730Sjeff * Handle two cases for replacing the base set or mask of an entire process.
497176730Sjeff *
498176730Sjeff * 1) Set is non-null and mask is null.  This reparents all anonymous sets
499176730Sjeff *    to the provided set and replaces all non-anonymous td_cpusets with the
500176730Sjeff *    provided set.
501176730Sjeff * 2) Mask is non-null and set is null.  This replaces or creates anonymous
502176730Sjeff *    sets for every thread with the existing base as a parent.
503176730Sjeff *
504176730Sjeff * This is overly complicated because we can't allocate while holding a
505176730Sjeff * spinlock and spinlocks must be held while changing and examining thread
506176730Sjeff * state.
507176730Sjeff */
508176730Sjeffstatic int
509176730Sjeffcpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
510176730Sjeff{
511176730Sjeff	struct setlist freelist;
512176730Sjeff	struct setlist droplist;
513176811Sjeff	struct cpuset *tdset;
514176730Sjeff	struct cpuset *nset;
515176730Sjeff	struct thread *td;
516176730Sjeff	struct proc *p;
517176730Sjeff	int threads;
518176730Sjeff	int nfree;
519176730Sjeff	int error;
520176730Sjeff	/*
521176730Sjeff	 * The algorithm requires two passes due to locking considerations.
522176730Sjeff	 *
523176730Sjeff	 * 1) Lookup the process and acquire the locks in the required order.
524176730Sjeff	 * 2) If enough cpusets have not been allocated release the locks and
525176730Sjeff	 *    allocate them.  Loop.
526176730Sjeff	 */
527176730Sjeff	LIST_INIT(&freelist);
528176730Sjeff	LIST_INIT(&droplist);
529176730Sjeff	nfree = 0;
530176730Sjeff	for (;;) {
531176730Sjeff		error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
532176730Sjeff		if (error)
533176730Sjeff			goto out;
534176730Sjeff		if (nfree >= p->p_numthreads)
535176730Sjeff			break;
536176730Sjeff		threads = p->p_numthreads;
537176730Sjeff		PROC_UNLOCK(p);
538176730Sjeff		for (; nfree < threads; nfree++) {
539176730Sjeff			nset = uma_zalloc(cpuset_zone, M_WAITOK);
540176730Sjeff			LIST_INSERT_HEAD(&freelist, nset, cs_link);
541176730Sjeff		}
542176730Sjeff	}
543176730Sjeff	PROC_LOCK_ASSERT(p, MA_OWNED);
544176730Sjeff	/*
545176730Sjeff	 * Now that the appropriate locks are held and we have enough cpusets,
546176811Sjeff	 * make sure the operation will succeed before applying changes.  The
547176811Sjeff	 * proc lock prevents td_cpuset from changing between calls.
548176811Sjeff	 */
549176811Sjeff	error = 0;
550176811Sjeff	FOREACH_THREAD_IN_PROC(p, td) {
551176811Sjeff		thread_lock(td);
552176811Sjeff		tdset = td->td_cpuset;
553176811Sjeff		/*
554176811Sjeff		 * Verify that a new mask doesn't specify cpus outside of
555176811Sjeff		 * the set the thread is a member of.
556176811Sjeff		 */
557176811Sjeff		if (mask) {
558176811Sjeff			if (tdset->cs_id == CPUSET_INVALID)
559176811Sjeff				tdset = tdset->cs_parent;
560176811Sjeff			if (!CPU_SUBSET(&tdset->cs_mask, mask))
561177738Sjeff				error = EDEADLK;
562176811Sjeff		/*
563176811Sjeff		 * Verify that a new set won't leave an existing thread
564176811Sjeff		 * mask without a cpu to run on.  It can, however, restrict
565176811Sjeff		 * the set.
566176811Sjeff		 */
567176811Sjeff		} else if (tdset->cs_id == CPUSET_INVALID) {
568176811Sjeff			if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
569177738Sjeff				error = EDEADLK;
570176811Sjeff		}
571176811Sjeff		thread_unlock(td);
572176811Sjeff		if (error)
573176811Sjeff			goto unlock_out;
574176811Sjeff	}
575176811Sjeff	/*
576176811Sjeff	 * Replace each thread's cpuset while using deferred release.  We
577177368Sjeff	 * must do this because the thread lock must be held while operating
578177368Sjeff	 * on the thread and this limits the type of operations allowed.
579176730Sjeff	 */
580176730Sjeff	FOREACH_THREAD_IN_PROC(p, td) {
581176730Sjeff		thread_lock(td);
582176730Sjeff		/*
583176730Sjeff		 * If we presently have an anonymous set or are applying a
584176730Sjeff		 * mask we must create an anonymous shadow set.  That is
585176730Sjeff		 * either parented to our existing base or the supplied set.
586176730Sjeff		 *
587176730Sjeff		 * If we have a base set with no anonymous shadow we simply
588176730Sjeff		 * replace it outright.
589176730Sjeff		 */
590176730Sjeff		tdset = td->td_cpuset;
591176730Sjeff		if (tdset->cs_id == CPUSET_INVALID || mask) {
592176730Sjeff			nset = LIST_FIRST(&freelist);
593176730Sjeff			LIST_REMOVE(nset, cs_link);
594176730Sjeff			if (mask)
595176730Sjeff				error = cpuset_shadow(tdset, nset, mask);
596176730Sjeff			else
597176730Sjeff				error = _cpuset_create(nset, set,
598176730Sjeff				    &tdset->cs_mask, CPUSET_INVALID);
599176730Sjeff			if (error) {
600176730Sjeff				LIST_INSERT_HEAD(&freelist, nset, cs_link);
601176730Sjeff				thread_unlock(td);
602176730Sjeff				break;
603176730Sjeff			}
604176730Sjeff		} else
605176730Sjeff			nset = cpuset_ref(set);
606176730Sjeff		cpuset_rel_defer(&droplist, tdset);
607176730Sjeff		td->td_cpuset = nset;
608176730Sjeff		sched_affinity(td);
609176730Sjeff		thread_unlock(td);
610176730Sjeff	}
611176811Sjeffunlock_out:
612176730Sjeff	PROC_UNLOCK(p);
613176730Sjeffout:
614176730Sjeff	while ((nset = LIST_FIRST(&droplist)) != NULL)
615176730Sjeff		cpuset_rel_complete(nset);
616176730Sjeff	while ((nset = LIST_FIRST(&freelist)) != NULL) {
617176730Sjeff		LIST_REMOVE(nset, cs_link);
618176730Sjeff		uma_zfree(cpuset_zone, nset);
619176730Sjeff	}
620176730Sjeff	return (error);
621176730Sjeff}
622176730Sjeff
623176730Sjeff/*
624222813Sattilio * Return a string representing a valid layout for a cpuset_t object.
625222813Sattilio * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
626222813Sattilio */
627222813Sattiliochar *
628222813Sattiliocpusetobj_strprint(char *buf, const cpuset_t *set)
629222813Sattilio{
630222813Sattilio	char *tbuf;
631222813Sattilio	size_t i, bytesp, bufsiz;
632222813Sattilio
633222813Sattilio	tbuf = buf;
634222813Sattilio	bytesp = 0;
635222813Sattilio	bufsiz = CPUSETBUFSIZ;
636222813Sattilio
637239923Sattilio	for (i = 0; i < (_NCPUWORDS - 1); i++) {
638239923Sattilio		bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]);
639222813Sattilio		bufsiz -= bytesp;
640222813Sattilio		tbuf += bytesp;
641222813Sattilio	}
642239923Sattilio	snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]);
643222813Sattilio	return (buf);
644222813Sattilio}
645222813Sattilio
646222813Sattilio/*
647222813Sattilio * Build a valid cpuset_t object from a string representation.
648222813Sattilio * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
649222813Sattilio */
650222813Sattilioint
651222813Sattiliocpusetobj_strscan(cpuset_t *set, const char *buf)
652222813Sattilio{
653222813Sattilio	u_int nwords;
654222813Sattilio	int i, ret;
655222813Sattilio
656222813Sattilio	if (strlen(buf) > CPUSETBUFSIZ - 1)
657222813Sattilio		return (-1);
658222813Sattilio
659222813Sattilio	/* Allow to pass a shorter version of the mask when necessary. */
660222813Sattilio	nwords = 1;
661222813Sattilio	for (i = 0; buf[i] != '\0'; i++)
662222813Sattilio		if (buf[i] == ',')
663222813Sattilio			nwords++;
664222813Sattilio	if (nwords > _NCPUWORDS)
665222813Sattilio		return (-1);
666222813Sattilio
667222813Sattilio	CPU_ZERO(set);
668239923Sattilio	for (i = 0; i < (nwords - 1); i++) {
669239923Sattilio		ret = sscanf(buf, "%lx,", &set->__bits[i]);
670222813Sattilio		if (ret == 0 || ret == -1)
671222813Sattilio			return (-1);
672239923Sattilio		buf = strstr(buf, ",");
673222813Sattilio		if (buf == NULL)
674222813Sattilio			return (-1);
675222813Sattilio		buf++;
676222813Sattilio	}
677239923Sattilio	ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]);
678222813Sattilio	if (ret == 0 || ret == -1)
679222813Sattilio		return (-1);
680222813Sattilio	return (0);
681222813Sattilio}
682222813Sattilio
683222813Sattilio/*
684176730Sjeff * Apply an anonymous mask to a single thread.
685176730Sjeff */
686177738Sjeffint
687176730Sjeffcpuset_setthread(lwpid_t id, cpuset_t *mask)
688176730Sjeff{
689176730Sjeff	struct cpuset *nset;
690176730Sjeff	struct cpuset *set;
691176730Sjeff	struct thread *td;
692176730Sjeff	struct proc *p;
693176730Sjeff	int error;
694176730Sjeff
695176730Sjeff	nset = uma_zalloc(cpuset_zone, M_WAITOK);
696176821Sjeff	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
697176730Sjeff	if (error)
698176730Sjeff		goto out;
699177738Sjeff	set = NULL;
700176730Sjeff	thread_lock(td);
701177738Sjeff	error = cpuset_shadow(td->td_cpuset, nset, mask);
702176730Sjeff	if (error == 0) {
703177738Sjeff		set = td->td_cpuset;
704176730Sjeff		td->td_cpuset = nset;
705176730Sjeff		sched_affinity(td);
706176730Sjeff		nset = NULL;
707176730Sjeff	}
708176730Sjeff	thread_unlock(td);
709176730Sjeff	PROC_UNLOCK(p);
710177738Sjeff	if (set)
711177738Sjeff		cpuset_rel(set);
712176730Sjeffout:
713176730Sjeff	if (nset)
714176730Sjeff		uma_zfree(cpuset_zone, nset);
715176730Sjeff	return (error);
716176730Sjeff}
717176730Sjeff
718176730Sjeff/*
719267716Smelifaro * Apply new cpumask to the ithread.
720267716Smelifaro */
721267716Smelifaroint
722267716Smelifarocpuset_setithread(lwpid_t id, u_char cpu)
723267716Smelifaro{
724267716Smelifaro	struct cpuset *nset, *rset;
725267716Smelifaro	struct cpuset *parent, *old_set;
726267716Smelifaro	struct thread *td;
727267716Smelifaro	struct proc *p;
728267716Smelifaro	cpusetid_t cs_id;
729267716Smelifaro	cpuset_t mask;
730267716Smelifaro	int error;
731267716Smelifaro
732267716Smelifaro	nset = uma_zalloc(cpuset_zone, M_WAITOK);
733267716Smelifaro	rset = uma_zalloc(cpuset_zone, M_WAITOK);
734267716Smelifaro
735267716Smelifaro	CPU_ZERO(&mask);
736267716Smelifaro	if (cpu == NOCPU)
737267716Smelifaro		CPU_COPY(cpuset_root, &mask);
738267716Smelifaro	else
739267716Smelifaro		CPU_SET(cpu, &mask);
740267716Smelifaro
741267716Smelifaro	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set);
742267716Smelifaro	if (((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID) || error != 0)
743267716Smelifaro		goto out;
744267716Smelifaro
745267716Smelifaro	thread_lock(td);
746267716Smelifaro	old_set = td->td_cpuset;
747267716Smelifaro
748267716Smelifaro	if (cpu == NOCPU) {
749267716Smelifaro		/*
750267716Smelifaro		 * roll back to default set. We're not using cpuset_shadow()
751267716Smelifaro		 * here because we can fail CPU_SUBSET() check. This can happen
752267716Smelifaro		 * if default set does not contain all CPUs.
753267716Smelifaro		 */
754267716Smelifaro		error = _cpuset_create(nset, cpuset_default, &mask,
755267716Smelifaro		    CPUSET_INVALID);
756267716Smelifaro
757267716Smelifaro		goto applyset;
758267716Smelifaro	}
759267716Smelifaro
760267716Smelifaro	if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID &&
761267716Smelifaro	    old_set->cs_parent->cs_id == 1)) {
762267716Smelifaro		/* Default mask, we need to use new root set */
763267716Smelifaro		error = _cpuset_create(rset, cpuset_zero,
764267716Smelifaro		    &cpuset_zero->cs_mask, cs_id);
765267716Smelifaro		if (error != 0) {
766267716Smelifaro			PROC_UNLOCK(p);
767267716Smelifaro			goto out;
768267716Smelifaro		}
769267716Smelifaro		rset->cs_flags |= CPU_SET_ROOT;
770267716Smelifaro		parent = rset;
771267716Smelifaro		rset = NULL;
772267716Smelifaro		cs_id = CPUSET_INVALID;
773267716Smelifaro	} else {
774267716Smelifaro		/* Assume existing set was already allocated by previous call */
775267716Smelifaro		parent = td->td_cpuset;
776267716Smelifaro		old_set = NULL;
777267716Smelifaro	}
778267716Smelifaro
779267716Smelifaro	error = cpuset_shadow(parent, nset, &mask);
780267716Smelifaroapplyset:
781267716Smelifaro	if (error == 0) {
782267716Smelifaro		td->td_cpuset = nset;
783267716Smelifaro		sched_affinity(td);
784267716Smelifaro		nset = NULL;
785267716Smelifaro	}
786267716Smelifaro	thread_unlock(td);
787267716Smelifaro	PROC_UNLOCK(p);
788267716Smelifaro	if (old_set != NULL)
789267716Smelifaro		cpuset_rel(old_set);
790267716Smelifaroout:
791267716Smelifaro	if (nset != NULL)
792267716Smelifaro		uma_zfree(cpuset_zone, nset);
793267716Smelifaro	if (rset != NULL)
794267716Smelifaro		uma_zfree(cpuset_zone, rset);
795267716Smelifaro	if (cs_id != CPUSET_INVALID)
796267716Smelifaro		free_unr(cpuset_unr, cs_id);
797267716Smelifaro	return (error);
798267716Smelifaro}
799267716Smelifaro
800267716Smelifaro
801267716Smelifaro/*
802176730Sjeff * Creates the cpuset for thread0.  We make two sets:
803176730Sjeff *
804176730Sjeff * 0 - The root set which should represent all valid processors in the
805176730Sjeff *     system.  It is initially created with a mask of all processors
806176730Sjeff *     because we don't know what processors are valid until cpuset_init()
807176730Sjeff *     runs.  This set is immutable.
808176730Sjeff * 1 - The default set which all processes are a member of until changed.
809176730Sjeff *     This allows an administrator to move all threads off of given cpus to
810176730Sjeff *     dedicate them to high priority tasks or save power etc.
811176730Sjeff */
812176730Sjeffstruct cpuset *
813176730Sjeffcpuset_thread0(void)
814176730Sjeff{
815176730Sjeff	struct cpuset *set;
816176730Sjeff	int error;
817176730Sjeff
818176730Sjeff	cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
819176730Sjeff	    NULL, NULL, UMA_ALIGN_PTR, 0);
820176730Sjeff	mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
821267716Smelifaro
822176730Sjeff	/*
823176730Sjeff	 * Create the root system set for the whole machine.  Doesn't use
824176730Sjeff	 * cpuset_create() due to NULL parent.
825176730Sjeff	 */
826176730Sjeff	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
827222201Sattilio	CPU_FILL(&set->cs_mask);
828176730Sjeff	LIST_INIT(&set->cs_children);
829176730Sjeff	LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
830176730Sjeff	set->cs_ref = 1;
831176730Sjeff	set->cs_flags = CPU_SET_ROOT;
832176730Sjeff	cpuset_zero = set;
833177738Sjeff	cpuset_root = &set->cs_mask;
834267716Smelifaro
835176730Sjeff	/*
836176730Sjeff	 * Now derive a default, modifiable set from that to give out.
837176730Sjeff	 */
838176730Sjeff	set = uma_zalloc(cpuset_zone, M_WAITOK);
839176730Sjeff	error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
840176730Sjeff	KASSERT(error == 0, ("Error creating default set: %d\n", error));
841267716Smelifaro	cpuset_default = set;
842267716Smelifaro
843176730Sjeff	/*
844176730Sjeff	 * Initialize the unit allocator. 0 and 1 are allocated above.
845176730Sjeff	 */
846176730Sjeff	cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
847176730Sjeff
848176730Sjeff	return (set);
849176730Sjeff}
850176730Sjeff
851176730Sjeff/*
852185435Sbz * Create a cpuset, which would be cpuset_create() but
853185435Sbz * mark the new 'set' as root.
854185435Sbz *
855191403Sbz * We are not going to reparent the td to it.  Use cpuset_setproc_update_set()
856191403Sbz * for that.
857185435Sbz *
858185435Sbz * In case of no error, returns the set in *setp locked with a reference.
859185435Sbz */
860185435Sbzint
861192895Sjamiecpuset_create_root(struct prison *pr, struct cpuset **setp)
862185435Sbz{
863185435Sbz	struct cpuset *set;
864185435Sbz	int error;
865185435Sbz
866192895Sjamie	KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
867185435Sbz	KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
868185435Sbz
869192895Sjamie	error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
870185435Sbz	if (error)
871185435Sbz		return (error);
872185435Sbz
873185435Sbz	KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
874185435Sbz	    __func__, __LINE__));
875185435Sbz
876185435Sbz	/* Mark the set as root. */
877185435Sbz	set = *setp;
878185435Sbz	set->cs_flags |= CPU_SET_ROOT;
879185435Sbz
880185435Sbz	return (0);
881185435Sbz}
882185435Sbz
883185435Sbzint
884185435Sbzcpuset_setproc_update_set(struct proc *p, struct cpuset *set)
885185435Sbz{
886185435Sbz	int error;
887185435Sbz
888185435Sbz	KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
889185435Sbz	KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
890185435Sbz
891185435Sbz	cpuset_ref(set);
892185435Sbz	error = cpuset_setproc(p->p_pid, set, NULL);
893185435Sbz	if (error)
894185435Sbz		return (error);
895185435Sbz	cpuset_rel(set);
896185435Sbz	return (0);
897185435Sbz}
898185435Sbz
899185435Sbz/*
900176730Sjeff * This is called once the final set of system cpus is known.  Modifies
901198493Sjhb * the root set and all children and mark the root read-only.
902176730Sjeff */
903176730Sjeffstatic void
904176730Sjeffcpuset_init(void *arg)
905176730Sjeff{
906176730Sjeff	cpuset_t mask;
907176730Sjeff
908222813Sattilio	mask = all_cpus;
909176730Sjeff	if (cpuset_modify(cpuset_zero, &mask))
910176730Sjeff		panic("Can't set initial cpuset mask.\n");
911176730Sjeff	cpuset_zero->cs_flags |= CPU_SET_RDONLY;
912176730Sjeff}
913176730SjeffSYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
914176730Sjeff
915176730Sjeff#ifndef _SYS_SYSPROTO_H_
916176730Sjeffstruct cpuset_args {
917176730Sjeff	cpusetid_t	*setid;
918176730Sjeff};
919176730Sjeff#endif
920176730Sjeffint
921225617Skmacysys_cpuset(struct thread *td, struct cpuset_args *uap)
922176730Sjeff{
923176730Sjeff	struct cpuset *root;
924176730Sjeff	struct cpuset *set;
925176730Sjeff	int error;
926176730Sjeff
927176730Sjeff	thread_lock(td);
928177738Sjeff	root = cpuset_refroot(td->td_cpuset);
929176730Sjeff	thread_unlock(td);
930176730Sjeff	error = cpuset_create(&set, root, &root->cs_mask);
931176730Sjeff	cpuset_rel(root);
932176730Sjeff	if (error)
933176730Sjeff		return (error);
934177738Sjeff	error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
935176730Sjeff	if (error == 0)
936177738Sjeff		error = cpuset_setproc(-1, set, NULL);
937176730Sjeff	cpuset_rel(set);
938176730Sjeff	return (error);
939176730Sjeff}
940176730Sjeff
941176730Sjeff#ifndef _SYS_SYSPROTO_H_
942176730Sjeffstruct cpuset_setid_args {
943176730Sjeff	cpuwhich_t	which;
944176730Sjeff	id_t		id;
945176730Sjeff	cpusetid_t	setid;
946176730Sjeff};
947176730Sjeff#endif
948176730Sjeffint
949225617Skmacysys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
950176730Sjeff{
951176730Sjeff	struct cpuset *set;
952176730Sjeff	int error;
953176730Sjeff
954176730Sjeff	/*
955176730Sjeff	 * Presently we only support per-process sets.
956176730Sjeff	 */
957176730Sjeff	if (uap->which != CPU_WHICH_PID)
958176730Sjeff		return (EINVAL);
959185435Sbz	set = cpuset_lookup(uap->setid, td);
960176730Sjeff	if (set == NULL)
961176730Sjeff		return (ESRCH);
962176730Sjeff	error = cpuset_setproc(uap->id, set, NULL);
963176730Sjeff	cpuset_rel(set);
964176730Sjeff	return (error);
965176730Sjeff}
966176730Sjeff
967176730Sjeff#ifndef _SYS_SYSPROTO_H_
968176730Sjeffstruct cpuset_getid_args {
969176730Sjeff	cpulevel_t	level;
970176730Sjeff	cpuwhich_t	which;
971176730Sjeff	id_t		id;
972176730Sjeff	cpusetid_t	*setid;
973228275Skevlo};
974176730Sjeff#endif
975176730Sjeffint
976225617Skmacysys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
977176730Sjeff{
978176730Sjeff	struct cpuset *nset;
979176730Sjeff	struct cpuset *set;
980176730Sjeff	struct thread *ttd;
981176730Sjeff	struct proc *p;
982176730Sjeff	cpusetid_t id;
983176730Sjeff	int error;
984176730Sjeff
985176730Sjeff	if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET)
986176730Sjeff		return (EINVAL);
987176730Sjeff	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
988176730Sjeff	if (error)
989176730Sjeff		return (error);
990176730Sjeff	switch (uap->which) {
991176730Sjeff	case CPU_WHICH_TID:
992176730Sjeff	case CPU_WHICH_PID:
993176730Sjeff		thread_lock(ttd);
994177738Sjeff		set = cpuset_refbase(ttd->td_cpuset);
995176730Sjeff		thread_unlock(ttd);
996176730Sjeff		PROC_UNLOCK(p);
997176730Sjeff		break;
998176730Sjeff	case CPU_WHICH_CPUSET:
999185435Sbz	case CPU_WHICH_JAIL:
1000176730Sjeff		break;
1001178092Sjeff	case CPU_WHICH_IRQ:
1002178092Sjeff		return (EINVAL);
1003176730Sjeff	}
1004176730Sjeff	switch (uap->level) {
1005176730Sjeff	case CPU_LEVEL_ROOT:
1006177738Sjeff		nset = cpuset_refroot(set);
1007176730Sjeff		cpuset_rel(set);
1008176730Sjeff		set = nset;
1009176730Sjeff		break;
1010176730Sjeff	case CPU_LEVEL_CPUSET:
1011176730Sjeff		break;
1012176730Sjeff	case CPU_LEVEL_WHICH:
1013176730Sjeff		break;
1014176730Sjeff	}
1015176730Sjeff	id = set->cs_id;
1016176730Sjeff	cpuset_rel(set);
1017176730Sjeff	if (error == 0)
1018176730Sjeff		error = copyout(&id, uap->setid, sizeof(id));
1019176730Sjeff
1020176730Sjeff	return (error);
1021176730Sjeff}
1022176730Sjeff
1023176730Sjeff#ifndef _SYS_SYSPROTO_H_
1024176730Sjeffstruct cpuset_getaffinity_args {
1025177597Sru	cpulevel_t	level;
1026177597Sru	cpuwhich_t	which;
1027177597Sru	id_t		id;
1028177597Sru	size_t		cpusetsize;
1029177597Sru	cpuset_t	*mask;
1030176730Sjeff};
1031176730Sjeff#endif
1032176730Sjeffint
1033225617Skmacysys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
1034176730Sjeff{
1035176730Sjeff	struct thread *ttd;
1036176730Sjeff	struct cpuset *nset;
1037176730Sjeff	struct cpuset *set;
1038176730Sjeff	struct proc *p;
1039176730Sjeff	cpuset_t *mask;
1040176730Sjeff	int error;
1041177597Sru	size_t size;
1042176730Sjeff
1043176811Sjeff	if (uap->cpusetsize < sizeof(cpuset_t) ||
1044179313Skib	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
1045176730Sjeff		return (ERANGE);
1046176811Sjeff	size = uap->cpusetsize;
1047176730Sjeff	mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
1048176730Sjeff	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1049176730Sjeff	if (error)
1050176730Sjeff		goto out;
1051176730Sjeff	switch (uap->level) {
1052176730Sjeff	case CPU_LEVEL_ROOT:
1053176730Sjeff	case CPU_LEVEL_CPUSET:
1054176730Sjeff		switch (uap->which) {
1055176730Sjeff		case CPU_WHICH_TID:
1056176730Sjeff		case CPU_WHICH_PID:
1057176730Sjeff			thread_lock(ttd);
1058176730Sjeff			set = cpuset_ref(ttd->td_cpuset);
1059176730Sjeff			thread_unlock(ttd);
1060176730Sjeff			break;
1061176730Sjeff		case CPU_WHICH_CPUSET:
1062185435Sbz		case CPU_WHICH_JAIL:
1063176730Sjeff			break;
1064178092Sjeff		case CPU_WHICH_IRQ:
1065178092Sjeff			error = EINVAL;
1066178092Sjeff			goto out;
1067176730Sjeff		}
1068176730Sjeff		if (uap->level == CPU_LEVEL_ROOT)
1069177738Sjeff			nset = cpuset_refroot(set);
1070176730Sjeff		else
1071177738Sjeff			nset = cpuset_refbase(set);
1072176730Sjeff		CPU_COPY(&nset->cs_mask, mask);
1073176730Sjeff		cpuset_rel(nset);
1074176730Sjeff		break;
1075176730Sjeff	case CPU_LEVEL_WHICH:
1076176730Sjeff		switch (uap->which) {
1077176730Sjeff		case CPU_WHICH_TID:
1078176730Sjeff			thread_lock(ttd);
1079176730Sjeff			CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1080176730Sjeff			thread_unlock(ttd);
1081176730Sjeff			break;
1082176730Sjeff		case CPU_WHICH_PID:
1083176730Sjeff			FOREACH_THREAD_IN_PROC(p, ttd) {
1084176730Sjeff				thread_lock(ttd);
1085176730Sjeff				CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1086176730Sjeff				thread_unlock(ttd);
1087176730Sjeff			}
1088176730Sjeff			break;
1089176730Sjeff		case CPU_WHICH_CPUSET:
1090185435Sbz		case CPU_WHICH_JAIL:
1091176730Sjeff			CPU_COPY(&set->cs_mask, mask);
1092176730Sjeff			break;
1093178092Sjeff		case CPU_WHICH_IRQ:
1094178092Sjeff			error = intr_getaffinity(uap->id, mask);
1095178092Sjeff			break;
1096176730Sjeff		}
1097176730Sjeff		break;
1098176730Sjeff	default:
1099176730Sjeff		error = EINVAL;
1100176730Sjeff		break;
1101176730Sjeff	}
1102176730Sjeff	if (set)
1103176730Sjeff		cpuset_rel(set);
1104176730Sjeff	if (p)
1105176730Sjeff		PROC_UNLOCK(p);
1106176730Sjeff	if (error == 0)
1107176730Sjeff		error = copyout(mask, uap->mask, size);
1108176730Sjeffout:
1109176730Sjeff	free(mask, M_TEMP);
1110176730Sjeff	return (error);
1111176730Sjeff}
1112176730Sjeff
1113176730Sjeff#ifndef _SYS_SYSPROTO_H_
1114176730Sjeffstruct cpuset_setaffinity_args {
1115176730Sjeff	cpulevel_t	level;
1116177597Sru	cpuwhich_t	which;
1117177597Sru	id_t		id;
1118177597Sru	size_t		cpusetsize;
1119177597Sru	const cpuset_t	*mask;
1120176730Sjeff};
1121176730Sjeff#endif
1122176730Sjeffint
1123225617Skmacysys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1124176730Sjeff{
1125176730Sjeff	struct cpuset *nset;
1126176730Sjeff	struct cpuset *set;
1127176730Sjeff	struct thread *ttd;
1128176730Sjeff	struct proc *p;
1129176730Sjeff	cpuset_t *mask;
1130176730Sjeff	int error;
1131176730Sjeff
1132176811Sjeff	if (uap->cpusetsize < sizeof(cpuset_t) ||
1133179313Skib	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
1134176730Sjeff		return (ERANGE);
1135176811Sjeff	mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1136176811Sjeff	error = copyin(uap->mask, mask, uap->cpusetsize);
1137176730Sjeff	if (error)
1138176730Sjeff		goto out;
1139176811Sjeff	/*
1140176811Sjeff	 * Verify that no high bits are set.
1141176811Sjeff	 */
1142176811Sjeff	if (uap->cpusetsize > sizeof(cpuset_t)) {
1143176811Sjeff		char *end;
1144176811Sjeff		char *cp;
1145176811Sjeff
1146176811Sjeff		end = cp = (char *)&mask->__bits;
1147176811Sjeff		end += uap->cpusetsize;
1148176811Sjeff		cp += sizeof(cpuset_t);
1149176811Sjeff		while (cp != end)
1150176811Sjeff			if (*cp++ != 0) {
1151176811Sjeff				error = EINVAL;
1152176811Sjeff				goto out;
1153176811Sjeff			}
1154176811Sjeff
1155176811Sjeff	}
1156176730Sjeff	switch (uap->level) {
1157176730Sjeff	case CPU_LEVEL_ROOT:
1158176730Sjeff	case CPU_LEVEL_CPUSET:
1159176730Sjeff		error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1160176730Sjeff		if (error)
1161176730Sjeff			break;
1162176730Sjeff		switch (uap->which) {
1163176730Sjeff		case CPU_WHICH_TID:
1164176730Sjeff		case CPU_WHICH_PID:
1165176730Sjeff			thread_lock(ttd);
1166176730Sjeff			set = cpuset_ref(ttd->td_cpuset);
1167176730Sjeff			thread_unlock(ttd);
1168176880Sjeff			PROC_UNLOCK(p);
1169176730Sjeff			break;
1170176730Sjeff		case CPU_WHICH_CPUSET:
1171185435Sbz		case CPU_WHICH_JAIL:
1172176730Sjeff			break;
1173178092Sjeff		case CPU_WHICH_IRQ:
1174178092Sjeff			error = EINVAL;
1175178092Sjeff			goto out;
1176176730Sjeff		}
1177176730Sjeff		if (uap->level == CPU_LEVEL_ROOT)
1178177738Sjeff			nset = cpuset_refroot(set);
1179176730Sjeff		else
1180177738Sjeff			nset = cpuset_refbase(set);
1181176730Sjeff		error = cpuset_modify(nset, mask);
1182176730Sjeff		cpuset_rel(nset);
1183176730Sjeff		cpuset_rel(set);
1184176730Sjeff		break;
1185176730Sjeff	case CPU_LEVEL_WHICH:
1186176730Sjeff		switch (uap->which) {
1187176730Sjeff		case CPU_WHICH_TID:
1188176730Sjeff			error = cpuset_setthread(uap->id, mask);
1189176730Sjeff			break;
1190176730Sjeff		case CPU_WHICH_PID:
1191176730Sjeff			error = cpuset_setproc(uap->id, NULL, mask);
1192176730Sjeff			break;
1193176730Sjeff		case CPU_WHICH_CPUSET:
1194185435Sbz		case CPU_WHICH_JAIL:
1195185435Sbz			error = cpuset_which(uap->which, uap->id, &p,
1196176730Sjeff			    &ttd, &set);
1197176730Sjeff			if (error == 0) {
1198176730Sjeff				error = cpuset_modify(set, mask);
1199176730Sjeff				cpuset_rel(set);
1200176730Sjeff			}
1201176730Sjeff			break;
1202178092Sjeff		case CPU_WHICH_IRQ:
1203178092Sjeff			error = intr_setaffinity(uap->id, mask);
1204178092Sjeff			break;
1205176730Sjeff		default:
1206176730Sjeff			error = EINVAL;
1207176730Sjeff			break;
1208176730Sjeff		}
1209176730Sjeff		break;
1210176730Sjeff	default:
1211176730Sjeff		error = EINVAL;
1212176730Sjeff		break;
1213176730Sjeff	}
1214176730Sjeffout:
1215176730Sjeff	free(mask, M_TEMP);
1216176730Sjeff	return (error);
1217176730Sjeff}
1218180358Sbz
1219180358Sbz#ifdef DDB
1220252209Sjhbvoid
1221252209Sjhbddb_display_cpuset(const cpuset_t *set)
1222252209Sjhb{
1223252209Sjhb	int cpu, once;
1224252209Sjhb
1225252209Sjhb	for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1226252209Sjhb		if (CPU_ISSET(cpu, set)) {
1227252209Sjhb			if (once == 0) {
1228252209Sjhb				db_printf("%d", cpu);
1229252209Sjhb				once = 1;
1230252209Sjhb			} else
1231252209Sjhb				db_printf(",%d", cpu);
1232252209Sjhb		}
1233252209Sjhb	}
1234252209Sjhb	if (once == 0)
1235252209Sjhb		db_printf("<none>");
1236252209Sjhb}
1237252209Sjhb
1238180358SbzDB_SHOW_COMMAND(cpusets, db_show_cpusets)
1239180358Sbz{
1240180358Sbz	struct cpuset *set;
1241180358Sbz
1242180358Sbz	LIST_FOREACH(set, &cpuset_ids, cs_link) {
1243180358Sbz		db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1244180358Sbz		    set, set->cs_id, set->cs_ref, set->cs_flags,
1245180358Sbz		    (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1246180358Sbz		db_printf("  mask=");
1247252209Sjhb		ddb_display_cpuset(&set->cs_mask);
1248180358Sbz		db_printf("\n");
1249180358Sbz		if (db_pager_quit)
1250180358Sbz			break;
1251180358Sbz	}
1252180358Sbz}
1253180358Sbz#endif /* DDB */
1254