1/*-
2 * Copyright (c) 2008,  Jeffrey Roberson <jeff@freebsd.org>
3 * All rights reserved.
4 *
5 * Copyright (c) 2008 Nokia Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice unmodified, this list of conditions, and the following
13 *    disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include "opt_ddb.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/sysproto.h>
39#include <sys/jail.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/mutex.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/refcount.h>
47#include <sys/sched.h>
48#include <sys/smp.h>
49#include <sys/syscallsubr.h>
50#include <sys/cpuset.h>
51#include <sys/sx.h>
52#include <sys/queue.h>
53#include <sys/libkern.h>
54#include <sys/limits.h>
55#include <sys/bus.h>
56#include <sys/interrupt.h>
57
58#include <vm/uma.h>
59
60#ifdef DDB
61#include <ddb/ddb.h>
62#endif /* DDB */
63
64/*
65 * cpusets provide a mechanism for creating and manipulating sets of
66 * processors for the purpose of constraining the scheduling of threads to
67 * specific processors.
68 *
69 * Each process belongs to an identified set, by default this is set 1.  Each
70 * thread may further restrict the cpus it may run on to a subset of this
71 * named set.  This creates an anonymous set which other threads and processes
72 * may not join by number.
73 *
74 * The named set is referred to herein as the 'base' set to avoid ambiguity.
75 * This set is usually a child of a 'root' set while the anonymous set may
76 * simply be referred to as a mask.  In the syscall api these are referred to
77 * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
78 *
79 * Threads inherit their set from their creator whether it be anonymous or
80 * not.  This means that anonymous sets are immutable because they may be
81 * shared.  To modify an anonymous set a new set is created with the desired
82 * mask and the same parent as the existing anonymous set.  This gives the
83 * illusion of each thread having a private mask.
84 *
85 * Via the syscall apis a user may ask to retrieve or modify the root, base,
86 * or mask that is discovered via a pid, tid, or setid.  Modifying a set
87 * modifies all numbered and anonymous child sets to comply with the new mask.
88 * Modifying a pid or tid's mask applies only to that tid but must still
89 * exist within the assigned parent set.
90 *
91 * A thread may not be assigned to a group separate from other threads in
92 * the process.  This is to remove ambiguity when the setid is queried with
93 * a pid argument.  There is no other technical limitation.
94 *
95 * This somewhat complex arrangement is intended to make it easy for
96 * applications to query available processors and bind their threads to
97 * specific processors while also allowing administrators to dynamically
98 * reprovision by changing sets which apply to groups of processes.
99 *
100 * A simple application should not concern itself with sets at all and
101 * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
102 * meaning 'curthread'.  It may query available cpus for that tid with a
103 * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
104 */
105static uma_zone_t cpuset_zone;
106static struct mtx cpuset_lock;
107static struct setlist cpuset_ids;
108static struct unrhdr *cpuset_unr;
109static struct cpuset *cpuset_zero;
110
111/* Return the size of cpuset_t at the kernel level */
112SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD,
113	0, sizeof(cpuset_t), "sizeof(cpuset_t)");
114
115cpuset_t *cpuset_root;
116
117/*
118 * Acquire a reference to a cpuset, all pointers must be tracked with refs.
119 */
120struct cpuset *
121cpuset_ref(struct cpuset *set)
122{
123
124	refcount_acquire(&set->cs_ref);
125	return (set);
126}
127
128/*
129 * Walks up the tree from 'set' to find the root.  Returns the root
130 * referenced.
131 */
132static struct cpuset *
133cpuset_refroot(struct cpuset *set)
134{
135
136	for (; set->cs_parent != NULL; set = set->cs_parent)
137		if (set->cs_flags & CPU_SET_ROOT)
138			break;
139	cpuset_ref(set);
140
141	return (set);
142}
143
144/*
145 * Find the first non-anonymous set starting from 'set'.  Returns this set
146 * referenced.  May return the passed in set with an extra ref if it is
147 * not anonymous.
148 */
149static struct cpuset *
150cpuset_refbase(struct cpuset *set)
151{
152
153	if (set->cs_id == CPUSET_INVALID)
154		set = set->cs_parent;
155	cpuset_ref(set);
156
157	return (set);
158}
159
160/*
161 * Release a reference in a context where it is safe to allocate.
162 */
163void
164cpuset_rel(struct cpuset *set)
165{
166	cpusetid_t id;
167
168	if (refcount_release(&set->cs_ref) == 0)
169		return;
170	mtx_lock_spin(&cpuset_lock);
171	LIST_REMOVE(set, cs_siblings);
172	id = set->cs_id;
173	if (id != CPUSET_INVALID)
174		LIST_REMOVE(set, cs_link);
175	mtx_unlock_spin(&cpuset_lock);
176	cpuset_rel(set->cs_parent);
177	uma_zfree(cpuset_zone, set);
178	if (id != CPUSET_INVALID)
179		free_unr(cpuset_unr, id);
180}
181
182/*
183 * Deferred release must be used when in a context that is not safe to
184 * allocate/free.  This places any unreferenced sets on the list 'head'.
185 */
186static void
187cpuset_rel_defer(struct setlist *head, struct cpuset *set)
188{
189
190	if (refcount_release(&set->cs_ref) == 0)
191		return;
192	mtx_lock_spin(&cpuset_lock);
193	LIST_REMOVE(set, cs_siblings);
194	if (set->cs_id != CPUSET_INVALID)
195		LIST_REMOVE(set, cs_link);
196	LIST_INSERT_HEAD(head, set, cs_link);
197	mtx_unlock_spin(&cpuset_lock);
198}
199
200/*
201 * Complete a deferred release.  Removes the set from the list provided to
202 * cpuset_rel_defer.
203 */
204static void
205cpuset_rel_complete(struct cpuset *set)
206{
207	LIST_REMOVE(set, cs_link);
208	cpuset_rel(set->cs_parent);
209	uma_zfree(cpuset_zone, set);
210}
211
212/*
213 * Find a set based on an id.  Returns it with a ref.
214 */
215static struct cpuset *
216cpuset_lookup(cpusetid_t setid, struct thread *td)
217{
218	struct cpuset *set;
219
220	if (setid == CPUSET_INVALID)
221		return (NULL);
222	mtx_lock_spin(&cpuset_lock);
223	LIST_FOREACH(set, &cpuset_ids, cs_link)
224		if (set->cs_id == setid)
225			break;
226	if (set)
227		cpuset_ref(set);
228	mtx_unlock_spin(&cpuset_lock);
229
230	KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
231	if (set != NULL && jailed(td->td_ucred)) {
232		struct cpuset *jset, *tset;
233
234		jset = td->td_ucred->cr_prison->pr_cpuset;
235		for (tset = set; tset != NULL; tset = tset->cs_parent)
236			if (tset == jset)
237				break;
238		if (tset == NULL) {
239			cpuset_rel(set);
240			set = NULL;
241		}
242	}
243
244	return (set);
245}
246
247/*
248 * Create a set in the space provided in 'set' with the provided parameters.
249 * The set is returned with a single ref.  May return EDEADLK if the set
250 * will have no valid cpu based on restrictions from the parent.
251 */
252static int
253_cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask,
254    cpusetid_t id)
255{
256
257	if (!CPU_OVERLAP(&parent->cs_mask, mask))
258		return (EDEADLK);
259	CPU_COPY(mask, &set->cs_mask);
260	LIST_INIT(&set->cs_children);
261	refcount_init(&set->cs_ref, 1);
262	set->cs_flags = 0;
263	mtx_lock_spin(&cpuset_lock);
264	CPU_AND(&set->cs_mask, &parent->cs_mask);
265	set->cs_id = id;
266	set->cs_parent = cpuset_ref(parent);
267	LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
268	if (set->cs_id != CPUSET_INVALID)
269		LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
270	mtx_unlock_spin(&cpuset_lock);
271
272	return (0);
273}
274
275/*
276 * Create a new non-anonymous set with the requested parent and mask.  May
277 * return failures if the mask is invalid or a new number can not be
278 * allocated.
279 */
280static int
281cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
282{
283	struct cpuset *set;
284	cpusetid_t id;
285	int error;
286
287	id = alloc_unr(cpuset_unr);
288	if (id == -1)
289		return (ENFILE);
290	*setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
291	error = _cpuset_create(set, parent, mask, id);
292	if (error == 0)
293		return (0);
294	free_unr(cpuset_unr, id);
295	uma_zfree(cpuset_zone, set);
296
297	return (error);
298}
299
300/*
301 * Recursively check for errors that would occur from applying mask to
302 * the tree of sets starting at 'set'.  Checks for sets that would become
303 * empty as well as RDONLY flags.
304 */
305static int
306cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask)
307{
308	struct cpuset *nset;
309	cpuset_t newmask;
310	int error;
311
312	mtx_assert(&cpuset_lock, MA_OWNED);
313	if (set->cs_flags & CPU_SET_RDONLY)
314		return (EPERM);
315	if (check_mask) {
316		if (!CPU_OVERLAP(&set->cs_mask, mask))
317			return (EDEADLK);
318		CPU_COPY(&set->cs_mask, &newmask);
319		CPU_AND(&newmask, mask);
320	} else
321		CPU_COPY(mask, &newmask);
322	error = 0;
323	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
324		if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0)
325			break;
326	return (error);
327}
328
329/*
330 * Applies the mask 'mask' without checking for empty sets or permissions.
331 */
332static void
333cpuset_update(struct cpuset *set, cpuset_t *mask)
334{
335	struct cpuset *nset;
336
337	mtx_assert(&cpuset_lock, MA_OWNED);
338	CPU_AND(&set->cs_mask, mask);
339	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
340		cpuset_update(nset, &set->cs_mask);
341
342	return;
343}
344
345/*
346 * Modify the set 'set' to use a copy of the mask provided.  Apply this new
347 * mask to restrict all children in the tree.  Checks for validity before
348 * applying the changes.
349 */
350static int
351cpuset_modify(struct cpuset *set, cpuset_t *mask)
352{
353	struct cpuset *root;
354	int error;
355
356	error = priv_check(curthread, PRIV_SCHED_CPUSET);
357	if (error)
358		return (error);
359	/*
360	 * In case we are called from within the jail
361	 * we do not allow modifying the dedicated root
362	 * cpuset of the jail but may still allow to
363	 * change child sets.
364	 */
365	if (jailed(curthread->td_ucred) &&
366	    set->cs_flags & CPU_SET_ROOT)
367		return (EPERM);
368	/*
369	 * Verify that we have access to this set of
370	 * cpus.
371	 */
372	root = set->cs_parent;
373	if (root && !CPU_SUBSET(&root->cs_mask, mask))
374		return (EINVAL);
375	mtx_lock_spin(&cpuset_lock);
376	error = cpuset_testupdate(set, mask, 0);
377	if (error)
378		goto out;
379	CPU_COPY(mask, &set->cs_mask);
380	cpuset_update(set, mask);
381out:
382	mtx_unlock_spin(&cpuset_lock);
383
384	return (error);
385}
386
387/*
388 * Resolve the 'which' parameter of several cpuset apis.
389 *
390 * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid.  Also
391 * checks for permission via p_cansched().
392 *
393 * For WHICH_SET returns a valid set with a new reference.
394 *
395 * -1 may be supplied for any argument to mean the current proc/thread or
396 * the base set of the current thread.  May fail with ESRCH/EPERM.
397 */
398static int
399cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
400    struct cpuset **setp)
401{
402	struct cpuset *set;
403	struct thread *td;
404	struct proc *p;
405	int error;
406
407	*pp = p = NULL;
408	*tdp = td = NULL;
409	*setp = set = NULL;
410	switch (which) {
411	case CPU_WHICH_PID:
412		if (id == -1) {
413			PROC_LOCK(curproc);
414			p = curproc;
415			break;
416		}
417		if ((p = pfind(id)) == NULL)
418			return (ESRCH);
419		break;
420	case CPU_WHICH_TID:
421		if (id == -1) {
422			PROC_LOCK(curproc);
423			p = curproc;
424			td = curthread;
425			break;
426		}
427		td = tdfind(id, -1);
428		if (td == NULL)
429			return (ESRCH);
430		p = td->td_proc;
431		break;
432	case CPU_WHICH_CPUSET:
433		if (id == -1) {
434			thread_lock(curthread);
435			set = cpuset_refbase(curthread->td_cpuset);
436			thread_unlock(curthread);
437		} else
438			set = cpuset_lookup(id, curthread);
439		if (set) {
440			*setp = set;
441			return (0);
442		}
443		return (ESRCH);
444	case CPU_WHICH_JAIL:
445	{
446		/* Find `set' for prison with given id. */
447		struct prison *pr;
448
449		sx_slock(&allprison_lock);
450		pr = prison_find_child(curthread->td_ucred->cr_prison, id);
451		sx_sunlock(&allprison_lock);
452		if (pr == NULL)
453			return (ESRCH);
454		cpuset_ref(pr->pr_cpuset);
455		*setp = pr->pr_cpuset;
456		mtx_unlock(&pr->pr_mtx);
457		return (0);
458	}
459	case CPU_WHICH_IRQ:
460		return (0);
461	default:
462		return (EINVAL);
463	}
464	error = p_cansched(curthread, p);
465	if (error) {
466		PROC_UNLOCK(p);
467		return (error);
468	}
469	if (td == NULL)
470		td = FIRST_THREAD_IN_PROC(p);
471	*pp = p;
472	*tdp = td;
473	return (0);
474}
475
476/*
477 * Create an anonymous set with the provided mask in the space provided by
478 * 'fset'.  If the passed in set is anonymous we use its parent otherwise
479 * the new set is a child of 'set'.
480 */
481static int
482cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask)
483{
484	struct cpuset *parent;
485
486	if (set->cs_id == CPUSET_INVALID)
487		parent = set->cs_parent;
488	else
489		parent = set;
490	if (!CPU_SUBSET(&parent->cs_mask, mask))
491		return (EDEADLK);
492	return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
493}
494
495/*
496 * Handle two cases for replacing the base set or mask of an entire process.
497 *
498 * 1) Set is non-null and mask is null.  This reparents all anonymous sets
499 *    to the provided set and replaces all non-anonymous td_cpusets with the
500 *    provided set.
501 * 2) Mask is non-null and set is null.  This replaces or creates anonymous
502 *    sets for every thread with the existing base as a parent.
503 *
504 * This is overly complicated because we can't allocate while holding a
505 * spinlock and spinlocks must be held while changing and examining thread
506 * state.
507 */
508static int
509cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
510{
511	struct setlist freelist;
512	struct setlist droplist;
513	struct cpuset *tdset;
514	struct cpuset *nset;
515	struct thread *td;
516	struct proc *p;
517	int threads;
518	int nfree;
519	int error;
520	/*
521	 * The algorithm requires two passes due to locking considerations.
522	 *
523	 * 1) Lookup the process and acquire the locks in the required order.
524	 * 2) If enough cpusets have not been allocated release the locks and
525	 *    allocate them.  Loop.
526	 */
527	LIST_INIT(&freelist);
528	LIST_INIT(&droplist);
529	nfree = 0;
530	for (;;) {
531		error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
532		if (error)
533			goto out;
534		if (nfree >= p->p_numthreads)
535			break;
536		threads = p->p_numthreads;
537		PROC_UNLOCK(p);
538		for (; nfree < threads; nfree++) {
539			nset = uma_zalloc(cpuset_zone, M_WAITOK);
540			LIST_INSERT_HEAD(&freelist, nset, cs_link);
541		}
542	}
543	PROC_LOCK_ASSERT(p, MA_OWNED);
544	/*
545	 * Now that the appropriate locks are held and we have enough cpusets,
546	 * make sure the operation will succeed before applying changes.  The
547	 * proc lock prevents td_cpuset from changing between calls.
548	 */
549	error = 0;
550	FOREACH_THREAD_IN_PROC(p, td) {
551		thread_lock(td);
552		tdset = td->td_cpuset;
553		/*
554		 * Verify that a new mask doesn't specify cpus outside of
555		 * the set the thread is a member of.
556		 */
557		if (mask) {
558			if (tdset->cs_id == CPUSET_INVALID)
559				tdset = tdset->cs_parent;
560			if (!CPU_SUBSET(&tdset->cs_mask, mask))
561				error = EDEADLK;
562		/*
563		 * Verify that a new set won't leave an existing thread
564		 * mask without a cpu to run on.  It can, however, restrict
565		 * the set.
566		 */
567		} else if (tdset->cs_id == CPUSET_INVALID) {
568			if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
569				error = EDEADLK;
570		}
571		thread_unlock(td);
572		if (error)
573			goto unlock_out;
574	}
575	/*
576	 * Replace each thread's cpuset while using deferred release.  We
577	 * must do this because the thread lock must be held while operating
578	 * on the thread and this limits the type of operations allowed.
579	 */
580	FOREACH_THREAD_IN_PROC(p, td) {
581		thread_lock(td);
582		/*
583		 * If we presently have an anonymous set or are applying a
584		 * mask we must create an anonymous shadow set.  That is
585		 * either parented to our existing base or the supplied set.
586		 *
587		 * If we have a base set with no anonymous shadow we simply
588		 * replace it outright.
589		 */
590		tdset = td->td_cpuset;
591		if (tdset->cs_id == CPUSET_INVALID || mask) {
592			nset = LIST_FIRST(&freelist);
593			LIST_REMOVE(nset, cs_link);
594			if (mask)
595				error = cpuset_shadow(tdset, nset, mask);
596			else
597				error = _cpuset_create(nset, set,
598				    &tdset->cs_mask, CPUSET_INVALID);
599			if (error) {
600				LIST_INSERT_HEAD(&freelist, nset, cs_link);
601				thread_unlock(td);
602				break;
603			}
604		} else
605			nset = cpuset_ref(set);
606		cpuset_rel_defer(&droplist, tdset);
607		td->td_cpuset = nset;
608		sched_affinity(td);
609		thread_unlock(td);
610	}
611unlock_out:
612	PROC_UNLOCK(p);
613out:
614	while ((nset = LIST_FIRST(&droplist)) != NULL)
615		cpuset_rel_complete(nset);
616	while ((nset = LIST_FIRST(&freelist)) != NULL) {
617		LIST_REMOVE(nset, cs_link);
618		uma_zfree(cpuset_zone, nset);
619	}
620	return (error);
621}
622
623/*
624 * Calculate the ffs() of the cpuset.
625 */
626int
627cpusetobj_ffs(const cpuset_t *set)
628{
629	size_t i;
630	int cbit;
631
632	cbit = 0;
633	for (i = 0; i < _NCPUWORDS; i++) {
634		if (set->__bits[i] != 0) {
635			cbit = ffsl(set->__bits[i]);
636			cbit += i * _NCPUBITS;
637			break;
638		}
639	}
640	return (cbit);
641}
642
643/*
644 * Return a string representing a valid layout for a cpuset_t object.
645 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
646 */
647char *
648cpusetobj_strprint(char *buf, const cpuset_t *set)
649{
650	char *tbuf;
651	size_t i, bytesp, bufsiz;
652
653	tbuf = buf;
654	bytesp = 0;
655	bufsiz = CPUSETBUFSIZ;
656
657	for (i = _NCPUWORDS - 1; i > 0; i--) {
658		bytesp = snprintf(tbuf, bufsiz, "%lx, ", set->__bits[i]);
659		bufsiz -= bytesp;
660		tbuf += bytesp;
661	}
662	snprintf(tbuf, bufsiz, "%lx", set->__bits[0]);
663	return (buf);
664}
665
666/*
667 * Build a valid cpuset_t object from a string representation.
668 * It expects an incoming buffer at least sized as CPUSETBUFSIZ.
669 */
670int
671cpusetobj_strscan(cpuset_t *set, const char *buf)
672{
673	u_int nwords;
674	int i, ret;
675
676	if (strlen(buf) > CPUSETBUFSIZ - 1)
677		return (-1);
678
679	/* Allow to pass a shorter version of the mask when necessary. */
680	nwords = 1;
681	for (i = 0; buf[i] != '\0'; i++)
682		if (buf[i] == ',')
683			nwords++;
684	if (nwords > _NCPUWORDS)
685		return (-1);
686
687	CPU_ZERO(set);
688	for (i = nwords - 1; i > 0; i--) {
689		ret = sscanf(buf, "%lx, ", &set->__bits[i]);
690		if (ret == 0 || ret == -1)
691			return (-1);
692		buf = strstr(buf, " ");
693		if (buf == NULL)
694			return (-1);
695		buf++;
696	}
697	ret = sscanf(buf, "%lx", &set->__bits[0]);
698	if (ret == 0 || ret == -1)
699		return (-1);
700	return (0);
701}
702
703/*
704 * Apply an anonymous mask to a single thread.
705 */
706int
707cpuset_setthread(lwpid_t id, cpuset_t *mask)
708{
709	struct cpuset *nset;
710	struct cpuset *set;
711	struct thread *td;
712	struct proc *p;
713	int error;
714
715	nset = uma_zalloc(cpuset_zone, M_WAITOK);
716	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
717	if (error)
718		goto out;
719	set = NULL;
720	thread_lock(td);
721	error = cpuset_shadow(td->td_cpuset, nset, mask);
722	if (error == 0) {
723		set = td->td_cpuset;
724		td->td_cpuset = nset;
725		sched_affinity(td);
726		nset = NULL;
727	}
728	thread_unlock(td);
729	PROC_UNLOCK(p);
730	if (set)
731		cpuset_rel(set);
732out:
733	if (nset)
734		uma_zfree(cpuset_zone, nset);
735	return (error);
736}
737
738/*
739 * Creates the cpuset for thread0.  We make two sets:
740 *
741 * 0 - The root set which should represent all valid processors in the
742 *     system.  It is initially created with a mask of all processors
743 *     because we don't know what processors are valid until cpuset_init()
744 *     runs.  This set is immutable.
745 * 1 - The default set which all processes are a member of until changed.
746 *     This allows an administrator to move all threads off of given cpus to
747 *     dedicate them to high priority tasks or save power etc.
748 */
749struct cpuset *
750cpuset_thread0(void)
751{
752	struct cpuset *set;
753	int error;
754
755	cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
756	    NULL, NULL, UMA_ALIGN_PTR, 0);
757	mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
758	/*
759	 * Create the root system set for the whole machine.  Doesn't use
760	 * cpuset_create() due to NULL parent.
761	 */
762	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
763	CPU_FILL(&set->cs_mask);
764	LIST_INIT(&set->cs_children);
765	LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
766	set->cs_ref = 1;
767	set->cs_flags = CPU_SET_ROOT;
768	cpuset_zero = set;
769	cpuset_root = &set->cs_mask;
770	/*
771	 * Now derive a default, modifiable set from that to give out.
772	 */
773	set = uma_zalloc(cpuset_zone, M_WAITOK);
774	error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
775	KASSERT(error == 0, ("Error creating default set: %d\n", error));
776	/*
777	 * Initialize the unit allocator. 0 and 1 are allocated above.
778	 */
779	cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
780
781	return (set);
782}
783
784/*
785 * Create a cpuset, which would be cpuset_create() but
786 * mark the new 'set' as root.
787 *
788 * We are not going to reparent the td to it.  Use cpuset_setproc_update_set()
789 * for that.
790 *
791 * In case of no error, returns the set in *setp locked with a reference.
792 */
793int
794cpuset_create_root(struct prison *pr, struct cpuset **setp)
795{
796	struct cpuset *set;
797	int error;
798
799	KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
800	KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
801
802	error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
803	if (error)
804		return (error);
805
806	KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
807	    __func__, __LINE__));
808
809	/* Mark the set as root. */
810	set = *setp;
811	set->cs_flags |= CPU_SET_ROOT;
812
813	return (0);
814}
815
816int
817cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
818{
819	int error;
820
821	KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
822	KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
823
824	cpuset_ref(set);
825	error = cpuset_setproc(p->p_pid, set, NULL);
826	if (error)
827		return (error);
828	cpuset_rel(set);
829	return (0);
830}
831
832/*
833 * This is called once the final set of system cpus is known.  Modifies
834 * the root set and all children and mark the root read-only.
835 */
836static void
837cpuset_init(void *arg)
838{
839	cpuset_t mask;
840
841	mask = all_cpus;
842	if (cpuset_modify(cpuset_zero, &mask))
843		panic("Can't set initial cpuset mask.\n");
844	cpuset_zero->cs_flags |= CPU_SET_RDONLY;
845}
846SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
847
848#ifndef _SYS_SYSPROTO_H_
849struct cpuset_args {
850	cpusetid_t	*setid;
851};
852#endif
853int
854sys_cpuset(struct thread *td, struct cpuset_args *uap)
855{
856	struct cpuset *root;
857	struct cpuset *set;
858	int error;
859
860	thread_lock(td);
861	root = cpuset_refroot(td->td_cpuset);
862	thread_unlock(td);
863	error = cpuset_create(&set, root, &root->cs_mask);
864	cpuset_rel(root);
865	if (error)
866		return (error);
867	error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
868	if (error == 0)
869		error = cpuset_setproc(-1, set, NULL);
870	cpuset_rel(set);
871	return (error);
872}
873
874#ifndef _SYS_SYSPROTO_H_
875struct cpuset_setid_args {
876	cpuwhich_t	which;
877	id_t		id;
878	cpusetid_t	setid;
879};
880#endif
881int
882sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
883{
884	struct cpuset *set;
885	int error;
886
887	/*
888	 * Presently we only support per-process sets.
889	 */
890	if (uap->which != CPU_WHICH_PID)
891		return (EINVAL);
892	set = cpuset_lookup(uap->setid, td);
893	if (set == NULL)
894		return (ESRCH);
895	error = cpuset_setproc(uap->id, set, NULL);
896	cpuset_rel(set);
897	return (error);
898}
899
900#ifndef _SYS_SYSPROTO_H_
901struct cpuset_getid_args {
902	cpulevel_t	level;
903	cpuwhich_t	which;
904	id_t		id;
905	cpusetid_t	*setid;
906#endif
907int
908sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
909{
910	struct cpuset *nset;
911	struct cpuset *set;
912	struct thread *ttd;
913	struct proc *p;
914	cpusetid_t id;
915	int error;
916
917	if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET)
918		return (EINVAL);
919	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
920	if (error)
921		return (error);
922	switch (uap->which) {
923	case CPU_WHICH_TID:
924	case CPU_WHICH_PID:
925		thread_lock(ttd);
926		set = cpuset_refbase(ttd->td_cpuset);
927		thread_unlock(ttd);
928		PROC_UNLOCK(p);
929		break;
930	case CPU_WHICH_CPUSET:
931	case CPU_WHICH_JAIL:
932		break;
933	case CPU_WHICH_IRQ:
934		return (EINVAL);
935	}
936	switch (uap->level) {
937	case CPU_LEVEL_ROOT:
938		nset = cpuset_refroot(set);
939		cpuset_rel(set);
940		set = nset;
941		break;
942	case CPU_LEVEL_CPUSET:
943		break;
944	case CPU_LEVEL_WHICH:
945		break;
946	}
947	id = set->cs_id;
948	cpuset_rel(set);
949	if (error == 0)
950		error = copyout(&id, uap->setid, sizeof(id));
951
952	return (error);
953}
954
955#ifndef _SYS_SYSPROTO_H_
956struct cpuset_getaffinity_args {
957	cpulevel_t	level;
958	cpuwhich_t	which;
959	id_t		id;
960	size_t		cpusetsize;
961	cpuset_t	*mask;
962};
963#endif
964int
965sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
966{
967	struct thread *ttd;
968	struct cpuset *nset;
969	struct cpuset *set;
970	struct proc *p;
971	cpuset_t *mask;
972	int error;
973	size_t size;
974
975	if (uap->cpusetsize < sizeof(cpuset_t) ||
976	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
977		return (ERANGE);
978	size = uap->cpusetsize;
979	mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
980	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
981	if (error)
982		goto out;
983	switch (uap->level) {
984	case CPU_LEVEL_ROOT:
985	case CPU_LEVEL_CPUSET:
986		switch (uap->which) {
987		case CPU_WHICH_TID:
988		case CPU_WHICH_PID:
989			thread_lock(ttd);
990			set = cpuset_ref(ttd->td_cpuset);
991			thread_unlock(ttd);
992			break;
993		case CPU_WHICH_CPUSET:
994		case CPU_WHICH_JAIL:
995			break;
996		case CPU_WHICH_IRQ:
997			error = EINVAL;
998			goto out;
999		}
1000		if (uap->level == CPU_LEVEL_ROOT)
1001			nset = cpuset_refroot(set);
1002		else
1003			nset = cpuset_refbase(set);
1004		CPU_COPY(&nset->cs_mask, mask);
1005		cpuset_rel(nset);
1006		break;
1007	case CPU_LEVEL_WHICH:
1008		switch (uap->which) {
1009		case CPU_WHICH_TID:
1010			thread_lock(ttd);
1011			CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
1012			thread_unlock(ttd);
1013			break;
1014		case CPU_WHICH_PID:
1015			FOREACH_THREAD_IN_PROC(p, ttd) {
1016				thread_lock(ttd);
1017				CPU_OR(mask, &ttd->td_cpuset->cs_mask);
1018				thread_unlock(ttd);
1019			}
1020			break;
1021		case CPU_WHICH_CPUSET:
1022		case CPU_WHICH_JAIL:
1023			CPU_COPY(&set->cs_mask, mask);
1024			break;
1025		case CPU_WHICH_IRQ:
1026			error = intr_getaffinity(uap->id, mask);
1027			break;
1028		}
1029		break;
1030	default:
1031		error = EINVAL;
1032		break;
1033	}
1034	if (set)
1035		cpuset_rel(set);
1036	if (p)
1037		PROC_UNLOCK(p);
1038	if (error == 0)
1039		error = copyout(mask, uap->mask, size);
1040out:
1041	free(mask, M_TEMP);
1042	return (error);
1043}
1044
1045#ifndef _SYS_SYSPROTO_H_
1046struct cpuset_setaffinity_args {
1047	cpulevel_t	level;
1048	cpuwhich_t	which;
1049	id_t		id;
1050	size_t		cpusetsize;
1051	const cpuset_t	*mask;
1052};
1053#endif
1054int
1055sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
1056{
1057	struct cpuset *nset;
1058	struct cpuset *set;
1059	struct thread *ttd;
1060	struct proc *p;
1061	cpuset_t *mask;
1062	int error;
1063
1064	if (uap->cpusetsize < sizeof(cpuset_t) ||
1065	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
1066		return (ERANGE);
1067	mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
1068	error = copyin(uap->mask, mask, uap->cpusetsize);
1069	if (error)
1070		goto out;
1071	/*
1072	 * Verify that no high bits are set.
1073	 */
1074	if (uap->cpusetsize > sizeof(cpuset_t)) {
1075		char *end;
1076		char *cp;
1077
1078		end = cp = (char *)&mask->__bits;
1079		end += uap->cpusetsize;
1080		cp += sizeof(cpuset_t);
1081		while (cp != end)
1082			if (*cp++ != 0) {
1083				error = EINVAL;
1084				goto out;
1085			}
1086
1087	}
1088	switch (uap->level) {
1089	case CPU_LEVEL_ROOT:
1090	case CPU_LEVEL_CPUSET:
1091		error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1092		if (error)
1093			break;
1094		switch (uap->which) {
1095		case CPU_WHICH_TID:
1096		case CPU_WHICH_PID:
1097			thread_lock(ttd);
1098			set = cpuset_ref(ttd->td_cpuset);
1099			thread_unlock(ttd);
1100			PROC_UNLOCK(p);
1101			break;
1102		case CPU_WHICH_CPUSET:
1103		case CPU_WHICH_JAIL:
1104			break;
1105		case CPU_WHICH_IRQ:
1106			error = EINVAL;
1107			goto out;
1108		}
1109		if (uap->level == CPU_LEVEL_ROOT)
1110			nset = cpuset_refroot(set);
1111		else
1112			nset = cpuset_refbase(set);
1113		error = cpuset_modify(nset, mask);
1114		cpuset_rel(nset);
1115		cpuset_rel(set);
1116		break;
1117	case CPU_LEVEL_WHICH:
1118		switch (uap->which) {
1119		case CPU_WHICH_TID:
1120			error = cpuset_setthread(uap->id, mask);
1121			break;
1122		case CPU_WHICH_PID:
1123			error = cpuset_setproc(uap->id, NULL, mask);
1124			break;
1125		case CPU_WHICH_CPUSET:
1126		case CPU_WHICH_JAIL:
1127			error = cpuset_which(uap->which, uap->id, &p,
1128			    &ttd, &set);
1129			if (error == 0) {
1130				error = cpuset_modify(set, mask);
1131				cpuset_rel(set);
1132			}
1133			break;
1134		case CPU_WHICH_IRQ:
1135			error = intr_setaffinity(uap->id, mask);
1136			break;
1137		default:
1138			error = EINVAL;
1139			break;
1140		}
1141		break;
1142	default:
1143		error = EINVAL;
1144		break;
1145	}
1146out:
1147	free(mask, M_TEMP);
1148	return (error);
1149}
1150
1151#ifdef DDB
1152void
1153ddb_display_cpuset(const cpuset_t *set)
1154{
1155	int cpu, once;
1156
1157	for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1158		if (CPU_ISSET(cpu, set)) {
1159			if (once == 0) {
1160				db_printf("%d", cpu);
1161				once = 1;
1162			} else
1163				db_printf(",%d", cpu);
1164		}
1165	}
1166	if (once == 0)
1167		db_printf("<none>");
1168}
1169
1170DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1171{
1172	struct cpuset *set;
1173
1174	LIST_FOREACH(set, &cpuset_ids, cs_link) {
1175		db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1176		    set, set->cs_id, set->cs_ref, set->cs_flags,
1177		    (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1178		db_printf("  mask=");
1179		ddb_display_cpuset(&set->cs_mask);
1180		db_printf("\n");
1181		if (db_pager_quit)
1182			break;
1183	}
1184}
1185#endif /* DDB */
1186