kern_cpuset.c revision 214409
178556Sobrien/*-
278556Sobrien * Copyright (c) 2008,  Jeffrey Roberson <jeff@freebsd.org>
378556Sobrien * All rights reserved.
478556Sobrien *
578556Sobrien * Copyright (c) 2008 Nokia Corporation
678556Sobrien * All rights reserved.
778556Sobrien *
878556Sobrien * Redistribution and use in source and binary forms, with or without
978556Sobrien * modification, are permitted provided that the following conditions
1078556Sobrien * are met:
11146293Sobrien * 1. Redistributions of source code must retain the above copyright
1278556Sobrien *    notice unmodified, this list of conditions, and the following
1378556Sobrien *    disclaimer.
1478556Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1578556Sobrien *    notice, this list of conditions and the following disclaimer in the
1678556Sobrien *    documentation and/or other materials provided with the distribution.
1778556Sobrien *
1878556Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1978556Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2078556Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2178556Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2278556Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2378556Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2478556Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2578556Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2678556Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2778556Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2878556Sobrien *
2978556Sobrien */
3078556Sobrien
3178556Sobrien#include <sys/cdefs.h>
3278556Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_cpuset.c 214409 2010-10-27 02:32:54Z davidxu $");
3378556Sobrien
3478556Sobrien#include "opt_ddb.h"
3578556Sobrien
3678556Sobrien#include <sys/param.h>
3778556Sobrien#include <sys/systm.h>
3878556Sobrien#include <sys/sysproto.h>
3978556Sobrien#include <sys/jail.h>
4078556Sobrien#include <sys/kernel.h>
4178556Sobrien#include <sys/lock.h>
4278556Sobrien#include <sys/malloc.h>
4378556Sobrien#include <sys/mutex.h>
4478556Sobrien#include <sys/priv.h>
45146293Sobrien#include <sys/proc.h>
4678556Sobrien#include <sys/refcount.h>
4778556Sobrien#include <sys/sched.h>
4878556Sobrien#include <sys/smp.h>
4978556Sobrien#include <sys/syscallsubr.h>
5078556Sobrien#include <sys/cpuset.h>
5178556Sobrien#include <sys/sx.h>
5278556Sobrien#include <sys/queue.h>
5378556Sobrien#include <sys/limits.h>
5478556Sobrien#include <sys/bus.h>
5578556Sobrien#include <sys/interrupt.h>
5678556Sobrien
5778556Sobrien#include <vm/uma.h>
5878556Sobrien
5978556Sobrien#ifdef DDB
6078556Sobrien#include <ddb/ddb.h>
6178556Sobrien#endif /* DDB */
6278556Sobrien
6378556Sobrien/*
6478556Sobrien * cpusets provide a mechanism for creating and manipulating sets of
6578556Sobrien * processors for the purpose of constraining the scheduling of threads to
6678556Sobrien * specific processors.
6778556Sobrien *
6878556Sobrien * Each process belongs to an identified set, by default this is set 1.  Each
6978556Sobrien * thread may further restrict the cpus it may run on to a subset of this
7078556Sobrien * named set.  This creates an anonymous set which other threads and processes
7178556Sobrien * may not join by number.
7278556Sobrien *
7378556Sobrien * The named set is referred to herein as the 'base' set to avoid ambiguity.
7478556Sobrien * This set is usually a child of a 'root' set while the anonymous set may
7578556Sobrien * simply be referred to as a mask.  In the syscall api these are referred to
7678556Sobrien * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here.
7778556Sobrien *
7878556Sobrien * Threads inherit their set from their creator whether it be anonymous or
7978556Sobrien * not.  This means that anonymous sets are immutable because they may be
8078556Sobrien * shared.  To modify an anonymous set a new set is created with the desired
8178556Sobrien * mask and the same parent as the existing anonymous set.  This gives the
8278556Sobrien * illusion of each thread having a private mask.
8378556Sobrien *
8478556Sobrien * Via the syscall apis a user may ask to retrieve or modify the root, base,
8578556Sobrien * or mask that is discovered via a pid, tid, or setid.  Modifying a set
8678556Sobrien * modifies all numbered and anonymous child sets to comply with the new mask.
8778556Sobrien * Modifying a pid or tid's mask applies only to that tid but must still
8878556Sobrien * exist within the assigned parent set.
8978556Sobrien *
9078556Sobrien * A thread may not be assigned to a group separate from other threads in
9178556Sobrien * the process.  This is to remove ambiguity when the setid is queried with
9278556Sobrien * a pid argument.  There is no other technical limitation.
9378556Sobrien *
9478556Sobrien * This somewhat complex arrangement is intended to make it easy for
9578556Sobrien * applications to query available processors and bind their threads to
9678556Sobrien * specific processors while also allowing administrators to dynamically
9778556Sobrien * reprovision by changing sets which apply to groups of processes.
9878556Sobrien *
9978556Sobrien * A simple application should not concern itself with sets at all and
10078556Sobrien * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id
10178556Sobrien * meaning 'curthread'.  It may query available cpus for that tid with a
10278556Sobrien * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...).
10378556Sobrien */
10478556Sobrienstatic uma_zone_t cpuset_zone;
10578556Sobrienstatic struct mtx cpuset_lock;
10678556Sobrienstatic struct setlist cpuset_ids;
10778556Sobrienstatic struct unrhdr *cpuset_unr;
10878556Sobrienstatic struct cpuset *cpuset_zero;
10978556Sobrien
11078556Sobriencpuset_t *cpuset_root;
11178556Sobrien
11278556Sobrien/*
11378556Sobrien * Acquire a reference to a cpuset, all pointers must be tracked with refs.
11478556Sobrien */
11578556Sobrienstruct cpuset *
11678556Sobriencpuset_ref(struct cpuset *set)
11778556Sobrien{
11878556Sobrien
11978556Sobrien	refcount_acquire(&set->cs_ref);
12078556Sobrien	return (set);
12178556Sobrien}
12278556Sobrien
12378556Sobrien/*
12478556Sobrien * Walks up the tree from 'set' to find the root.  Returns the root
12578556Sobrien * referenced.
12678556Sobrien */
12778556Sobrienstatic struct cpuset *
12878556Sobriencpuset_refroot(struct cpuset *set)
12978556Sobrien{
13078556Sobrien
13178556Sobrien	for (; set->cs_parent != NULL; set = set->cs_parent)
13278556Sobrien		if (set->cs_flags & CPU_SET_ROOT)
13378556Sobrien			break;
13478556Sobrien	cpuset_ref(set);
13578556Sobrien
13678556Sobrien	return (set);
13778556Sobrien}
13878556Sobrien
13978556Sobrien/*
14078556Sobrien * Find the first non-anonymous set starting from 'set'.  Returns this set
14178556Sobrien * referenced.  May return the passed in set with an extra ref if it is
14278556Sobrien * not anonymous.
14378556Sobrien */
14478556Sobrienstatic struct cpuset *
14578556Sobriencpuset_refbase(struct cpuset *set)
14678556Sobrien{
14778556Sobrien
14878556Sobrien	if (set->cs_id == CPUSET_INVALID)
14978556Sobrien		set = set->cs_parent;
15078556Sobrien	cpuset_ref(set);
15178556Sobrien
15278556Sobrien	return (set);
15378556Sobrien}
15478556Sobrien
15578556Sobrien/*
15678556Sobrien * Release a reference in a context where it is safe to allocate.
15778556Sobrien */
15878556Sobrienvoid
15978556Sobriencpuset_rel(struct cpuset *set)
16078556Sobrien{
16178556Sobrien	cpusetid_t id;
16278556Sobrien
16378556Sobrien	if (refcount_release(&set->cs_ref) == 0)
16478556Sobrien		return;
16578556Sobrien	mtx_lock_spin(&cpuset_lock);
16678556Sobrien	LIST_REMOVE(set, cs_siblings);
16778556Sobrien	id = set->cs_id;
16878556Sobrien	if (id != CPUSET_INVALID)
16978556Sobrien		LIST_REMOVE(set, cs_link);
17078556Sobrien	mtx_unlock_spin(&cpuset_lock);
17178556Sobrien	cpuset_rel(set->cs_parent);
17278556Sobrien	uma_zfree(cpuset_zone, set);
17378556Sobrien	if (id != CPUSET_INVALID)
17478556Sobrien		free_unr(cpuset_unr, id);
17578556Sobrien}
17678556Sobrien
17778556Sobrien/*
17878556Sobrien * Deferred release must be used when in a context that is not safe to
17978556Sobrien * allocate/free.  This places any unreferenced sets on the list 'head'.
18078556Sobrien */
18178556Sobrienstatic void
18278556Sobriencpuset_rel_defer(struct setlist *head, struct cpuset *set)
18378556Sobrien{
18478556Sobrien
18578556Sobrien	if (refcount_release(&set->cs_ref) == 0)
18678556Sobrien		return;
18778556Sobrien	mtx_lock_spin(&cpuset_lock);
18878556Sobrien	LIST_REMOVE(set, cs_siblings);
18978556Sobrien	if (set->cs_id != CPUSET_INVALID)
19078556Sobrien		LIST_REMOVE(set, cs_link);
19178556Sobrien	LIST_INSERT_HEAD(head, set, cs_link);
19278556Sobrien	mtx_unlock_spin(&cpuset_lock);
19378556Sobrien}
19478556Sobrien
19578556Sobrien/*
19678556Sobrien * Complete a deferred release.  Removes the set from the list provided to
19778556Sobrien * cpuset_rel_defer.
19878556Sobrien */
19978556Sobrienstatic void
20078556Sobriencpuset_rel_complete(struct cpuset *set)
20178556Sobrien{
20278556Sobrien	LIST_REMOVE(set, cs_link);
20378556Sobrien	cpuset_rel(set->cs_parent);
20478556Sobrien	uma_zfree(cpuset_zone, set);
20578556Sobrien}
20678556Sobrien
20778556Sobrien/*
20878556Sobrien * Find a set based on an id.  Returns it with a ref.
20978556Sobrien */
21078556Sobrienstatic struct cpuset *
21178556Sobriencpuset_lookup(cpusetid_t setid, struct thread *td)
21278556Sobrien{
21378556Sobrien	struct cpuset *set;
21478556Sobrien
21578556Sobrien	if (setid == CPUSET_INVALID)
21678556Sobrien		return (NULL);
21778556Sobrien	mtx_lock_spin(&cpuset_lock);
21878556Sobrien	LIST_FOREACH(set, &cpuset_ids, cs_link)
21978556Sobrien		if (set->cs_id == setid)
22078556Sobrien			break;
22178556Sobrien	if (set)
22278556Sobrien		cpuset_ref(set);
22378556Sobrien	mtx_unlock_spin(&cpuset_lock);
22478556Sobrien
22578556Sobrien	KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__));
22678556Sobrien	if (set != NULL && jailed(td->td_ucred)) {
22778556Sobrien		struct cpuset *jset, *tset;
22878556Sobrien
22978556Sobrien		jset = td->td_ucred->cr_prison->pr_cpuset;
23078556Sobrien		for (tset = set; tset != NULL; tset = tset->cs_parent)
23178556Sobrien			if (tset == jset)
23278556Sobrien				break;
23378556Sobrien		if (tset == NULL) {
23478556Sobrien			cpuset_rel(set);
23578556Sobrien			set = NULL;
23678556Sobrien		}
23778556Sobrien	}
23878556Sobrien
23978556Sobrien	return (set);
24078556Sobrien}
24178556Sobrien
24278556Sobrien/*
24378556Sobrien * Create a set in the space provided in 'set' with the provided parameters.
24478556Sobrien * The set is returned with a single ref.  May return EDEADLK if the set
24578556Sobrien * will have no valid cpu based on restrictions from the parent.
24678556Sobrien */
24778556Sobrienstatic int
24878556Sobrien_cpuset_create(struct cpuset *set, struct cpuset *parent, cpuset_t *mask,
24978556Sobrien    cpusetid_t id)
25078556Sobrien{
25178556Sobrien
25278556Sobrien	if (!CPU_OVERLAP(&parent->cs_mask, mask))
25378556Sobrien		return (EDEADLK);
25478556Sobrien	CPU_COPY(mask, &set->cs_mask);
25578556Sobrien	LIST_INIT(&set->cs_children);
25678556Sobrien	refcount_init(&set->cs_ref, 1);
25778556Sobrien	set->cs_flags = 0;
25878556Sobrien	mtx_lock_spin(&cpuset_lock);
25978556Sobrien	CPU_AND(mask, &parent->cs_mask);
26078556Sobrien	set->cs_id = id;
26178556Sobrien	set->cs_parent = cpuset_ref(parent);
26278556Sobrien	LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings);
26378556Sobrien	if (set->cs_id != CPUSET_INVALID)
26478556Sobrien		LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
26578556Sobrien	mtx_unlock_spin(&cpuset_lock);
26678556Sobrien
26778556Sobrien	return (0);
26878556Sobrien}
26978556Sobrien
27078556Sobrien/*
27178556Sobrien * Create a new non-anonymous set with the requested parent and mask.  May
27278556Sobrien * return failures if the mask is invalid or a new number can not be
27378556Sobrien * allocated.
27478556Sobrien */
27578556Sobrienstatic int
27678556Sobriencpuset_create(struct cpuset **setp, struct cpuset *parent, cpuset_t *mask)
27778556Sobrien{
27878556Sobrien	struct cpuset *set;
27978556Sobrien	cpusetid_t id;
28078556Sobrien	int error;
28178556Sobrien
28278556Sobrien	id = alloc_unr(cpuset_unr);
28378556Sobrien	if (id == -1)
28478556Sobrien		return (ENFILE);
28578556Sobrien	*setp = set = uma_zalloc(cpuset_zone, M_WAITOK);
28678556Sobrien	error = _cpuset_create(set, parent, mask, id);
28778556Sobrien	if (error == 0)
28878556Sobrien		return (0);
28978556Sobrien	free_unr(cpuset_unr, id);
29078556Sobrien	uma_zfree(cpuset_zone, set);
29178556Sobrien
29278556Sobrien	return (error);
29378556Sobrien}
29478556Sobrien
29578556Sobrien/*
29678556Sobrien * Recursively check for errors that would occur from applying mask to
29778556Sobrien * the tree of sets starting at 'set'.  Checks for sets that would become
29878556Sobrien * empty as well as RDONLY flags.
29978556Sobrien */
30078556Sobrienstatic int
30178556Sobriencpuset_testupdate(struct cpuset *set, cpuset_t *mask)
30278556Sobrien{
30378556Sobrien	struct cpuset *nset;
30478556Sobrien	cpuset_t newmask;
30578556Sobrien	int error;
30678556Sobrien
30778556Sobrien	mtx_assert(&cpuset_lock, MA_OWNED);
30878556Sobrien	if (set->cs_flags & CPU_SET_RDONLY)
30978556Sobrien		return (EPERM);
31078556Sobrien	if (!CPU_OVERLAP(&set->cs_mask, mask))
31178556Sobrien		return (EDEADLK);
31278556Sobrien	CPU_COPY(&set->cs_mask, &newmask);
31378556Sobrien	CPU_AND(&newmask, mask);
31478556Sobrien	error = 0;
31578556Sobrien	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
31678556Sobrien		if ((error = cpuset_testupdate(nset, &newmask)) != 0)
31778556Sobrien			break;
31878556Sobrien	return (error);
31978556Sobrien}
32078556Sobrien
32178556Sobrien/*
32278556Sobrien * Applies the mask 'mask' without checking for empty sets or permissions.
32378556Sobrien */
32478556Sobrienstatic void
32578556Sobriencpuset_update(struct cpuset *set, cpuset_t *mask)
32678556Sobrien{
32778556Sobrien	struct cpuset *nset;
32878556Sobrien
32978556Sobrien	mtx_assert(&cpuset_lock, MA_OWNED);
33078556Sobrien	CPU_AND(&set->cs_mask, mask);
33178556Sobrien	LIST_FOREACH(nset, &set->cs_children, cs_siblings)
33278556Sobrien		cpuset_update(nset, &set->cs_mask);
33378556Sobrien
33478556Sobrien	return;
33578556Sobrien}
33678556Sobrien
33778556Sobrien/*
33878556Sobrien * Modify the set 'set' to use a copy of the mask provided.  Apply this new
33978556Sobrien * mask to restrict all children in the tree.  Checks for validity before
34078556Sobrien * applying the changes.
34178556Sobrien */
34278556Sobrienstatic int
34378556Sobriencpuset_modify(struct cpuset *set, cpuset_t *mask)
34478556Sobrien{
34578556Sobrien	struct cpuset *root;
34678556Sobrien	int error;
34778556Sobrien
34878556Sobrien	error = priv_check(curthread, PRIV_SCHED_CPUSET);
34978556Sobrien	if (error)
35078556Sobrien		return (error);
35178556Sobrien	/*
35278556Sobrien	 * In case we are called from within the jail
35378556Sobrien	 * we do not allow modifying the dedicated root
35478556Sobrien	 * cpuset of the jail but may still allow to
35578556Sobrien	 * change child sets.
35678556Sobrien	 */
35778556Sobrien	if (jailed(curthread->td_ucred) &&
35878556Sobrien	    set->cs_flags & CPU_SET_ROOT)
35978556Sobrien		return (EPERM);
36078556Sobrien	/*
36178556Sobrien	 * Verify that we have access to this set of
36278556Sobrien	 * cpus.
36378556Sobrien	 */
36478556Sobrien	root = set->cs_parent;
36578556Sobrien	if (root && !CPU_SUBSET(&root->cs_mask, mask))
36678556Sobrien		return (EINVAL);
36778556Sobrien	mtx_lock_spin(&cpuset_lock);
36878556Sobrien	error = cpuset_testupdate(set, mask);
36978556Sobrien	if (error)
37078556Sobrien		goto out;
37178556Sobrien	cpuset_update(set, mask);
37278556Sobrien	CPU_COPY(mask, &set->cs_mask);
37378556Sobrienout:
37478556Sobrien	mtx_unlock_spin(&cpuset_lock);
37578556Sobrien
37678556Sobrien	return (error);
37778556Sobrien}
37878556Sobrien
37978556Sobrien/*
38078556Sobrien * Resolve the 'which' parameter of several cpuset apis.
38178556Sobrien *
38278556Sobrien * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid.  Also
38378556Sobrien * checks for permission via p_cansched().
38478556Sobrien *
38578556Sobrien * For WHICH_SET returns a valid set with a new reference.
38678556Sobrien *
38778556Sobrien * -1 may be supplied for any argument to mean the current proc/thread or
38878556Sobrien * the base set of the current thread.  May fail with ESRCH/EPERM.
38978556Sobrien */
39078556Sobrienstatic int
39178556Sobriencpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp,
39278556Sobrien    struct cpuset **setp)
39378556Sobrien{
39478556Sobrien	struct cpuset *set;
39578556Sobrien	struct thread *td;
39678556Sobrien	struct proc *p;
39778556Sobrien	int error;
39878556Sobrien
39978556Sobrien	*pp = p = NULL;
40078556Sobrien	*tdp = td = NULL;
40178556Sobrien	*setp = set = NULL;
40278556Sobrien	switch (which) {
40378556Sobrien	case CPU_WHICH_PID:
40478556Sobrien		if (id == -1) {
40578556Sobrien			PROC_LOCK(curproc);
40678556Sobrien			p = curproc;
40778556Sobrien			break;
40878556Sobrien		}
40978556Sobrien		if ((p = pfind(id)) == NULL)
41078556Sobrien			return (ESRCH);
41178556Sobrien		break;
41278556Sobrien	case CPU_WHICH_TID:
41378556Sobrien		if (id == -1) {
41478556Sobrien			PROC_LOCK(curproc);
41578556Sobrien			p = curproc;
41678556Sobrien			td = curthread;
41778556Sobrien			break;
41878556Sobrien		}
41978556Sobrien		td = tdfind(id, -1);
42078556Sobrien		if (td == NULL)
42178556Sobrien			return (ESRCH);
42278556Sobrien		p = td->td_proc;
42378556Sobrien		break;
42478556Sobrien	case CPU_WHICH_CPUSET:
42578556Sobrien		if (id == -1) {
42678556Sobrien			thread_lock(curthread);
42778556Sobrien			set = cpuset_refbase(curthread->td_cpuset);
42878556Sobrien			thread_unlock(curthread);
42978556Sobrien		} else
43078556Sobrien			set = cpuset_lookup(id, curthread);
43178556Sobrien		if (set) {
43278556Sobrien			*setp = set;
43378556Sobrien			return (0);
43478556Sobrien		}
43578556Sobrien		return (ESRCH);
43678556Sobrien	case CPU_WHICH_JAIL:
43778556Sobrien	{
43878556Sobrien		/* Find `set' for prison with given id. */
43978556Sobrien		struct prison *pr;
44078556Sobrien
44178556Sobrien		sx_slock(&allprison_lock);
44278556Sobrien		pr = prison_find_child(curthread->td_ucred->cr_prison, id);
44378556Sobrien		sx_sunlock(&allprison_lock);
44478556Sobrien		if (pr == NULL)
44578556Sobrien			return (ESRCH);
44678556Sobrien		cpuset_ref(pr->pr_cpuset);
44778556Sobrien		*setp = pr->pr_cpuset;
44878556Sobrien		mtx_unlock(&pr->pr_mtx);
44978556Sobrien		return (0);
45078556Sobrien	}
45178556Sobrien	case CPU_WHICH_IRQ:
45278556Sobrien		return (0);
45378556Sobrien	default:
45478556Sobrien		return (EINVAL);
45578556Sobrien	}
45678556Sobrien	error = p_cansched(curthread, p);
45778556Sobrien	if (error) {
45878556Sobrien		PROC_UNLOCK(p);
45978556Sobrien		return (error);
46078556Sobrien	}
46178556Sobrien	if (td == NULL)
46278556Sobrien		td = FIRST_THREAD_IN_PROC(p);
46378556Sobrien	*pp = p;
46478556Sobrien	*tdp = td;
46578556Sobrien	return (0);
46678556Sobrien}
46778556Sobrien
46878556Sobrien/*
46978556Sobrien * Create an anonymous set with the provided mask in the space provided by
47078556Sobrien * 'fset'.  If the passed in set is anonymous we use its parent otherwise
47178556Sobrien * the new set is a child of 'set'.
47278556Sobrien */
47378556Sobrienstatic int
47478556Sobriencpuset_shadow(struct cpuset *set, struct cpuset *fset, cpuset_t *mask)
47578556Sobrien{
47678556Sobrien	struct cpuset *parent;
47778556Sobrien
47878556Sobrien	if (set->cs_id == CPUSET_INVALID)
47978556Sobrien		parent = set->cs_parent;
48078556Sobrien	else
48178556Sobrien		parent = set;
48278556Sobrien	if (!CPU_SUBSET(&parent->cs_mask, mask))
48378556Sobrien		return (EDEADLK);
48478556Sobrien	return (_cpuset_create(fset, parent, mask, CPUSET_INVALID));
48578556Sobrien}
48678556Sobrien
48778556Sobrien/*
48878556Sobrien * Handle two cases for replacing the base set or mask of an entire process.
48978556Sobrien *
49078556Sobrien * 1) Set is non-null and mask is null.  This reparents all anonymous sets
491146293Sobrien *    to the provided set and replaces all non-anonymous td_cpusets with the
492146293Sobrien *    provided set.
49378556Sobrien * 2) Mask is non-null and set is null.  This replaces or creates anonymous
49478556Sobrien *    sets for every thread with the existing base as a parent.
495146293Sobrien *
49678556Sobrien * This is overly complicated because we can't allocate while holding a
49778556Sobrien * spinlock and spinlocks must be held while changing and examining thread
49878556Sobrien * state.
49978556Sobrien */
50078556Sobrienstatic int
50178556Sobriencpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask)
50278556Sobrien{
50378556Sobrien	struct setlist freelist;
50478556Sobrien	struct setlist droplist;
50578556Sobrien	struct cpuset *tdset;
50678556Sobrien	struct cpuset *nset;
50778556Sobrien	struct thread *td;
50878556Sobrien	struct proc *p;
50978556Sobrien	int threads;
51078556Sobrien	int nfree;
51178556Sobrien	int error;
51278556Sobrien	/*
51378556Sobrien	 * The algorithm requires two passes due to locking considerations.
51478556Sobrien	 *
51578556Sobrien	 * 1) Lookup the process and acquire the locks in the required order.
51678556Sobrien	 * 2) If enough cpusets have not been allocated release the locks and
51778556Sobrien	 *    allocate them.  Loop.
51878556Sobrien	 */
51978556Sobrien	LIST_INIT(&freelist);
52078556Sobrien	LIST_INIT(&droplist);
52178556Sobrien	nfree = 0;
52278556Sobrien	for (;;) {
52378556Sobrien		error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset);
52478556Sobrien		if (error)
52578556Sobrien			goto out;
52678556Sobrien		if (nfree >= p->p_numthreads)
52778556Sobrien			break;
52878556Sobrien		threads = p->p_numthreads;
52978556Sobrien		PROC_UNLOCK(p);
53078556Sobrien		for (; nfree < threads; nfree++) {
53178556Sobrien			nset = uma_zalloc(cpuset_zone, M_WAITOK);
532146293Sobrien			LIST_INSERT_HEAD(&freelist, nset, cs_link);
53378556Sobrien		}
53478556Sobrien	}
53578556Sobrien	PROC_LOCK_ASSERT(p, MA_OWNED);
53678556Sobrien	/*
53778556Sobrien	 * Now that the appropriate locks are held and we have enough cpusets,
53878556Sobrien	 * make sure the operation will succeed before applying changes.  The
53978556Sobrien	 * proc lock prevents td_cpuset from changing between calls.
54078556Sobrien	 */
54178556Sobrien	error = 0;
54278556Sobrien	FOREACH_THREAD_IN_PROC(p, td) {
54378556Sobrien		thread_lock(td);
54478556Sobrien		tdset = td->td_cpuset;
54578556Sobrien		/*
54678556Sobrien		 * Verify that a new mask doesn't specify cpus outside of
54778556Sobrien		 * the set the thread is a member of.
54878556Sobrien		 */
54978556Sobrien		if (mask) {
55078556Sobrien			if (tdset->cs_id == CPUSET_INVALID)
55178556Sobrien				tdset = tdset->cs_parent;
55278556Sobrien			if (!CPU_SUBSET(&tdset->cs_mask, mask))
55378556Sobrien				error = EDEADLK;
55478556Sobrien		/*
55578556Sobrien		 * Verify that a new set won't leave an existing thread
55678556Sobrien		 * mask without a cpu to run on.  It can, however, restrict
55778556Sobrien		 * the set.
55878556Sobrien		 */
55978556Sobrien		} else if (tdset->cs_id == CPUSET_INVALID) {
56078556Sobrien			if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask))
56178556Sobrien				error = EDEADLK;
56278556Sobrien		}
56378556Sobrien		thread_unlock(td);
56478556Sobrien		if (error)
56578556Sobrien			goto unlock_out;
56678556Sobrien	}
56778556Sobrien	/*
56878556Sobrien	 * Replace each thread's cpuset while using deferred release.  We
56978556Sobrien	 * must do this because the thread lock must be held while operating
57078556Sobrien	 * on the thread and this limits the type of operations allowed.
57178556Sobrien	 */
57278556Sobrien	FOREACH_THREAD_IN_PROC(p, td) {
57378556Sobrien		thread_lock(td);
57478556Sobrien		/*
57578556Sobrien		 * If we presently have an anonymous set or are applying a
57678556Sobrien		 * mask we must create an anonymous shadow set.  That is
57778556Sobrien		 * either parented to our existing base or the supplied set.
57878556Sobrien		 *
57978556Sobrien		 * If we have a base set with no anonymous shadow we simply
58078556Sobrien		 * replace it outright.
58178556Sobrien		 */
58278556Sobrien		tdset = td->td_cpuset;
58378556Sobrien		if (tdset->cs_id == CPUSET_INVALID || mask) {
58478556Sobrien			nset = LIST_FIRST(&freelist);
58578556Sobrien			LIST_REMOVE(nset, cs_link);
58678556Sobrien			if (mask)
58778556Sobrien				error = cpuset_shadow(tdset, nset, mask);
58878556Sobrien			else
58978556Sobrien				error = _cpuset_create(nset, set,
59078556Sobrien				    &tdset->cs_mask, CPUSET_INVALID);
59178556Sobrien			if (error) {
59278556Sobrien				LIST_INSERT_HEAD(&freelist, nset, cs_link);
59378556Sobrien				thread_unlock(td);
59478556Sobrien				break;
59578556Sobrien			}
59678556Sobrien		} else
59778556Sobrien			nset = cpuset_ref(set);
59878556Sobrien		cpuset_rel_defer(&droplist, tdset);
59978556Sobrien		td->td_cpuset = nset;
60078556Sobrien		sched_affinity(td);
60178556Sobrien		thread_unlock(td);
60278556Sobrien	}
60378556Sobrienunlock_out:
60478556Sobrien	PROC_UNLOCK(p);
60578556Sobrienout:
60678556Sobrien	while ((nset = LIST_FIRST(&droplist)) != NULL)
60778556Sobrien		cpuset_rel_complete(nset);
60878556Sobrien	while ((nset = LIST_FIRST(&freelist)) != NULL) {
60978556Sobrien		LIST_REMOVE(nset, cs_link);
61078556Sobrien		uma_zfree(cpuset_zone, nset);
61178556Sobrien	}
61278556Sobrien	return (error);
61378556Sobrien}
61478556Sobrien
61578556Sobrien/*
61678556Sobrien * Apply an anonymous mask to a single thread.
61778556Sobrien */
61878556Sobrienint
61978556Sobriencpuset_setthread(lwpid_t id, cpuset_t *mask)
62078556Sobrien{
62178556Sobrien	struct cpuset *nset;
62278556Sobrien	struct cpuset *set;
62378556Sobrien	struct thread *td;
62478556Sobrien	struct proc *p;
62578556Sobrien	int error;
62678556Sobrien
62778556Sobrien	nset = uma_zalloc(cpuset_zone, M_WAITOK);
62878556Sobrien	error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set);
62978556Sobrien	if (error)
63078556Sobrien		goto out;
63178556Sobrien	set = NULL;
63278556Sobrien	thread_lock(td);
63378556Sobrien	error = cpuset_shadow(td->td_cpuset, nset, mask);
63478556Sobrien	if (error == 0) {
63578556Sobrien		set = td->td_cpuset;
63678556Sobrien		td->td_cpuset = nset;
63778556Sobrien		sched_affinity(td);
63878556Sobrien		nset = NULL;
63978556Sobrien	}
64078556Sobrien	thread_unlock(td);
64178556Sobrien	PROC_UNLOCK(p);
64278556Sobrien	if (set)
64378556Sobrien		cpuset_rel(set);
64478556Sobrienout:
64578556Sobrien	if (nset)
64678556Sobrien		uma_zfree(cpuset_zone, nset);
64778556Sobrien	return (error);
64878556Sobrien}
64978556Sobrien
65078556Sobrien/*
65178556Sobrien * Creates the cpuset for thread0.  We make two sets:
65278556Sobrien *
65378556Sobrien * 0 - The root set which should represent all valid processors in the
65478556Sobrien *     system.  It is initially created with a mask of all processors
65578556Sobrien *     because we don't know what processors are valid until cpuset_init()
656146293Sobrien *     runs.  This set is immutable.
657146293Sobrien * 1 - The default set which all processes are a member of until changed.
65878556Sobrien *     This allows an administrator to move all threads off of given cpus to
65978556Sobrien *     dedicate them to high priority tasks or save power etc.
66078556Sobrien */
66178556Sobrienstruct cpuset *
66278556Sobriencpuset_thread0(void)
66378556Sobrien{
66478556Sobrien	struct cpuset *set;
66578556Sobrien	int error;
66678556Sobrien
66778556Sobrien	cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL,
66890067Ssobomax	    NULL, NULL, UMA_ALIGN_PTR, 0);
66990067Ssobomax	mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE);
67090067Ssobomax	/*
67190067Ssobomax	 * Create the root system set for the whole machine.  Doesn't use
67278556Sobrien	 * cpuset_create() due to NULL parent.
67378556Sobrien	 */
67478556Sobrien	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
67578556Sobrien	set->cs_mask.__bits[0] = -1;
67678556Sobrien	LIST_INIT(&set->cs_children);
67778556Sobrien	LIST_INSERT_HEAD(&cpuset_ids, set, cs_link);
67878556Sobrien	set->cs_ref = 1;
67978556Sobrien	set->cs_flags = CPU_SET_ROOT;
68078556Sobrien	cpuset_zero = set;
68178556Sobrien	cpuset_root = &set->cs_mask;
68278556Sobrien	/*
68378556Sobrien	 * Now derive a default, modifiable set from that to give out.
68478556Sobrien	 */
68578556Sobrien	set = uma_zalloc(cpuset_zone, M_WAITOK);
68678556Sobrien	error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1);
68778556Sobrien	KASSERT(error == 0, ("Error creating default set: %d\n", error));
68878556Sobrien	/*
68978556Sobrien	 * Initialize the unit allocator. 0 and 1 are allocated above.
69078556Sobrien	 */
69178556Sobrien	cpuset_unr = new_unrhdr(2, INT_MAX, NULL);
69278556Sobrien
69378556Sobrien	return (set);
69478556Sobrien}
69578556Sobrien
69678556Sobrien/*
69778556Sobrien * Create a cpuset, which would be cpuset_create() but
69878556Sobrien * mark the new 'set' as root.
69978556Sobrien *
70078556Sobrien * We are not going to reparent the td to it.  Use cpuset_setproc_update_set()
70178556Sobrien * for that.
70278556Sobrien *
70378556Sobrien * In case of no error, returns the set in *setp locked with a reference.
70478556Sobrien */
70578556Sobrienint
70678556Sobriencpuset_create_root(struct prison *pr, struct cpuset **setp)
70778556Sobrien{
708146293Sobrien	struct cpuset *set;
70978556Sobrien	int error;
71078556Sobrien
71178556Sobrien	KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
71278556Sobrien	KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
71378556Sobrien
71478556Sobrien	error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
71578556Sobrien	if (error)
71678556Sobrien		return (error);
717
718	KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
719	    __func__, __LINE__));
720
721	/* Mark the set as root. */
722	set = *setp;
723	set->cs_flags |= CPU_SET_ROOT;
724
725	return (0);
726}
727
728int
729cpuset_setproc_update_set(struct proc *p, struct cpuset *set)
730{
731	int error;
732
733	KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__));
734	KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__));
735
736	cpuset_ref(set);
737	error = cpuset_setproc(p->p_pid, set, NULL);
738	if (error)
739		return (error);
740	cpuset_rel(set);
741	return (0);
742}
743
744/*
745 * This is called once the final set of system cpus is known.  Modifies
746 * the root set and all children and mark the root read-only.
747 */
748static void
749cpuset_init(void *arg)
750{
751	cpuset_t mask;
752
753	CPU_ZERO(&mask);
754#ifdef SMP
755	mask.__bits[0] = all_cpus;
756#else
757	mask.__bits[0] = 1;
758#endif
759	if (cpuset_modify(cpuset_zero, &mask))
760		panic("Can't set initial cpuset mask.\n");
761	cpuset_zero->cs_flags |= CPU_SET_RDONLY;
762}
763SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL);
764
765#ifndef _SYS_SYSPROTO_H_
766struct cpuset_args {
767	cpusetid_t	*setid;
768};
769#endif
770int
771cpuset(struct thread *td, struct cpuset_args *uap)
772{
773	struct cpuset *root;
774	struct cpuset *set;
775	int error;
776
777	thread_lock(td);
778	root = cpuset_refroot(td->td_cpuset);
779	thread_unlock(td);
780	error = cpuset_create(&set, root, &root->cs_mask);
781	cpuset_rel(root);
782	if (error)
783		return (error);
784	error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id));
785	if (error == 0)
786		error = cpuset_setproc(-1, set, NULL);
787	cpuset_rel(set);
788	return (error);
789}
790
791#ifndef _SYS_SYSPROTO_H_
792struct cpuset_setid_args {
793	cpuwhich_t	which;
794	id_t		id;
795	cpusetid_t	setid;
796};
797#endif
798int
799cpuset_setid(struct thread *td, struct cpuset_setid_args *uap)
800{
801	struct cpuset *set;
802	int error;
803
804	/*
805	 * Presently we only support per-process sets.
806	 */
807	if (uap->which != CPU_WHICH_PID)
808		return (EINVAL);
809	set = cpuset_lookup(uap->setid, td);
810	if (set == NULL)
811		return (ESRCH);
812	error = cpuset_setproc(uap->id, set, NULL);
813	cpuset_rel(set);
814	return (error);
815}
816
817#ifndef _SYS_SYSPROTO_H_
818struct cpuset_getid_args {
819	cpulevel_t	level;
820	cpuwhich_t	which;
821	id_t		id;
822	cpusetid_t	*setid;
823#endif
824int
825cpuset_getid(struct thread *td, struct cpuset_getid_args *uap)
826{
827	struct cpuset *nset;
828	struct cpuset *set;
829	struct thread *ttd;
830	struct proc *p;
831	cpusetid_t id;
832	int error;
833
834	if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET)
835		return (EINVAL);
836	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
837	if (error)
838		return (error);
839	switch (uap->which) {
840	case CPU_WHICH_TID:
841	case CPU_WHICH_PID:
842		thread_lock(ttd);
843		set = cpuset_refbase(ttd->td_cpuset);
844		thread_unlock(ttd);
845		PROC_UNLOCK(p);
846		break;
847	case CPU_WHICH_CPUSET:
848	case CPU_WHICH_JAIL:
849		break;
850	case CPU_WHICH_IRQ:
851		return (EINVAL);
852	}
853	switch (uap->level) {
854	case CPU_LEVEL_ROOT:
855		nset = cpuset_refroot(set);
856		cpuset_rel(set);
857		set = nset;
858		break;
859	case CPU_LEVEL_CPUSET:
860		break;
861	case CPU_LEVEL_WHICH:
862		break;
863	}
864	id = set->cs_id;
865	cpuset_rel(set);
866	if (error == 0)
867		error = copyout(&id, uap->setid, sizeof(id));
868
869	return (error);
870}
871
872#ifndef _SYS_SYSPROTO_H_
873struct cpuset_getaffinity_args {
874	cpulevel_t	level;
875	cpuwhich_t	which;
876	id_t		id;
877	size_t		cpusetsize;
878	cpuset_t	*mask;
879};
880#endif
881int
882cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap)
883{
884	struct thread *ttd;
885	struct cpuset *nset;
886	struct cpuset *set;
887	struct proc *p;
888	cpuset_t *mask;
889	int error;
890	size_t size;
891
892	if (uap->cpusetsize == 0) {
893		td->td_retval[0] = sizeof(cpuset_t);
894		return (0);
895	}
896	if (uap->cpusetsize < sizeof(cpuset_t) ||
897	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
898		return (ERANGE);
899	size = uap->cpusetsize;
900	mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
901	error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
902	if (error)
903		goto out;
904	switch (uap->level) {
905	case CPU_LEVEL_ROOT:
906	case CPU_LEVEL_CPUSET:
907		switch (uap->which) {
908		case CPU_WHICH_TID:
909		case CPU_WHICH_PID:
910			thread_lock(ttd);
911			set = cpuset_ref(ttd->td_cpuset);
912			thread_unlock(ttd);
913			break;
914		case CPU_WHICH_CPUSET:
915		case CPU_WHICH_JAIL:
916			break;
917		case CPU_WHICH_IRQ:
918			error = EINVAL;
919			goto out;
920		}
921		if (uap->level == CPU_LEVEL_ROOT)
922			nset = cpuset_refroot(set);
923		else
924			nset = cpuset_refbase(set);
925		CPU_COPY(&nset->cs_mask, mask);
926		cpuset_rel(nset);
927		break;
928	case CPU_LEVEL_WHICH:
929		switch (uap->which) {
930		case CPU_WHICH_TID:
931			thread_lock(ttd);
932			CPU_COPY(&ttd->td_cpuset->cs_mask, mask);
933			thread_unlock(ttd);
934			break;
935		case CPU_WHICH_PID:
936			FOREACH_THREAD_IN_PROC(p, ttd) {
937				thread_lock(ttd);
938				CPU_OR(mask, &ttd->td_cpuset->cs_mask);
939				thread_unlock(ttd);
940			}
941			break;
942		case CPU_WHICH_CPUSET:
943		case CPU_WHICH_JAIL:
944			CPU_COPY(&set->cs_mask, mask);
945			break;
946		case CPU_WHICH_IRQ:
947			error = intr_getaffinity(uap->id, mask);
948			break;
949		}
950		break;
951	default:
952		error = EINVAL;
953		break;
954	}
955	if (set)
956		cpuset_rel(set);
957	if (p)
958		PROC_UNLOCK(p);
959	if (error == 0)
960		error = copyout(mask, uap->mask, size);
961out:
962	free(mask, M_TEMP);
963	return (error);
964}
965
966#ifndef _SYS_SYSPROTO_H_
967struct cpuset_setaffinity_args {
968	cpulevel_t	level;
969	cpuwhich_t	which;
970	id_t		id;
971	size_t		cpusetsize;
972	const cpuset_t	*mask;
973};
974#endif
975int
976cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap)
977{
978	struct cpuset *nset;
979	struct cpuset *set;
980	struct thread *ttd;
981	struct proc *p;
982	cpuset_t *mask;
983	int error;
984
985	if (uap->cpusetsize < sizeof(cpuset_t) ||
986	    uap->cpusetsize > CPU_MAXSIZE / NBBY)
987		return (ERANGE);
988	mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO);
989	error = copyin(uap->mask, mask, uap->cpusetsize);
990	if (error)
991		goto out;
992	/*
993	 * Verify that no high bits are set.
994	 */
995	if (uap->cpusetsize > sizeof(cpuset_t)) {
996		char *end;
997		char *cp;
998
999		end = cp = (char *)&mask->__bits;
1000		end += uap->cpusetsize;
1001		cp += sizeof(cpuset_t);
1002		while (cp != end)
1003			if (*cp++ != 0) {
1004				error = EINVAL;
1005				goto out;
1006			}
1007
1008	}
1009	switch (uap->level) {
1010	case CPU_LEVEL_ROOT:
1011	case CPU_LEVEL_CPUSET:
1012		error = cpuset_which(uap->which, uap->id, &p, &ttd, &set);
1013		if (error)
1014			break;
1015		switch (uap->which) {
1016		case CPU_WHICH_TID:
1017		case CPU_WHICH_PID:
1018			thread_lock(ttd);
1019			set = cpuset_ref(ttd->td_cpuset);
1020			thread_unlock(ttd);
1021			PROC_UNLOCK(p);
1022			break;
1023		case CPU_WHICH_CPUSET:
1024		case CPU_WHICH_JAIL:
1025			break;
1026		case CPU_WHICH_IRQ:
1027			error = EINVAL;
1028			goto out;
1029		}
1030		if (uap->level == CPU_LEVEL_ROOT)
1031			nset = cpuset_refroot(set);
1032		else
1033			nset = cpuset_refbase(set);
1034		error = cpuset_modify(nset, mask);
1035		cpuset_rel(nset);
1036		cpuset_rel(set);
1037		break;
1038	case CPU_LEVEL_WHICH:
1039		switch (uap->which) {
1040		case CPU_WHICH_TID:
1041			error = cpuset_setthread(uap->id, mask);
1042			break;
1043		case CPU_WHICH_PID:
1044			error = cpuset_setproc(uap->id, NULL, mask);
1045			break;
1046		case CPU_WHICH_CPUSET:
1047		case CPU_WHICH_JAIL:
1048			error = cpuset_which(uap->which, uap->id, &p,
1049			    &ttd, &set);
1050			if (error == 0) {
1051				error = cpuset_modify(set, mask);
1052				cpuset_rel(set);
1053			}
1054			break;
1055		case CPU_WHICH_IRQ:
1056			error = intr_setaffinity(uap->id, mask);
1057			break;
1058		default:
1059			error = EINVAL;
1060			break;
1061		}
1062		break;
1063	default:
1064		error = EINVAL;
1065		break;
1066	}
1067out:
1068	free(mask, M_TEMP);
1069	return (error);
1070}
1071
1072#ifdef DDB
1073DB_SHOW_COMMAND(cpusets, db_show_cpusets)
1074{
1075	struct cpuset *set;
1076	int cpu, once;
1077
1078	LIST_FOREACH(set, &cpuset_ids, cs_link) {
1079		db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n",
1080		    set, set->cs_id, set->cs_ref, set->cs_flags,
1081		    (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0);
1082		db_printf("  mask=");
1083		for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) {
1084			if (CPU_ISSET(cpu, &set->cs_mask)) {
1085				if (once == 0) {
1086					db_printf("%d", cpu);
1087					once = 1;
1088				} else
1089					db_printf(",%d", cpu);
1090			}
1091		}
1092		db_printf("\n");
1093		if (db_pager_quit)
1094			break;
1095	}
1096}
1097#endif /* DDB */
1098