1d7f687fcSJeff Roberson /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 38a36da99SPedro F. Giffuni * 4d7f687fcSJeff Roberson * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org> 5d7f687fcSJeff Roberson * All rights reserved. 6d7f687fcSJeff Roberson * 73bc8c68dSJeff Roberson * Copyright (c) 2008 Nokia Corporation 83bc8c68dSJeff Roberson * All rights reserved. 93bc8c68dSJeff Roberson * 10d7f687fcSJeff Roberson * Redistribution and use in source and binary forms, with or without 11d7f687fcSJeff Roberson * modification, are permitted provided that the following conditions 12d7f687fcSJeff Roberson * are met: 13d7f687fcSJeff Roberson * 1. Redistributions of source code must retain the above copyright 14d7f687fcSJeff Roberson * notice unmodified, this list of conditions, and the following 15d7f687fcSJeff Roberson * disclaimer. 16d7f687fcSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 17d7f687fcSJeff Roberson * notice, this list of conditions and the following disclaimer in the 18d7f687fcSJeff Roberson * documentation and/or other materials provided with the distribution. 19d7f687fcSJeff Roberson * 20d7f687fcSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21d7f687fcSJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22d7f687fcSJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23d7f687fcSJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24d7f687fcSJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25d7f687fcSJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26d7f687fcSJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27d7f687fcSJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28d7f687fcSJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29d7f687fcSJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30d7f687fcSJeff Roberson * 31d7f687fcSJeff Roberson */ 32d7f687fcSJeff Roberson 33d7f687fcSJeff Roberson #include <sys/cdefs.h> 34d7f687fcSJeff Roberson __FBSDID("$FreeBSD$"); 35d7f687fcSJeff Roberson 36dea0ed66SBjoern A. Zeeb #include "opt_ddb.h" 37586ed321SDmitry Chagin #include "opt_ktrace.h" 38dea0ed66SBjoern A. Zeeb 39d7f687fcSJeff Roberson #include <sys/param.h> 40d7f687fcSJeff Roberson #include <sys/systm.h> 41e5818a53SJeff Roberson #include <sys/sysctl.h> 42e5818a53SJeff Roberson #include <sys/ctype.h> 43d7f687fcSJeff Roberson #include <sys/sysproto.h> 440304c731SJamie Gritton #include <sys/jail.h> 45d7f687fcSJeff Roberson #include <sys/kernel.h> 46d7f687fcSJeff Roberson #include <sys/lock.h> 47d7f687fcSJeff Roberson #include <sys/malloc.h> 48d7f687fcSJeff Roberson #include <sys/mutex.h> 49d7f687fcSJeff Roberson #include <sys/priv.h> 50d7f687fcSJeff Roberson #include <sys/proc.h> 51d7f687fcSJeff Roberson #include <sys/refcount.h> 52d7f687fcSJeff Roberson #include <sys/sched.h> 53d7f687fcSJeff Roberson #include <sys/smp.h> 54d7f687fcSJeff Roberson #include <sys/syscallsubr.h> 5547a57144SJustin Hibbits #include <sys/sysent.h> 56f299c47bSAllan Jude #include <sys/capsicum.h> 57d7f687fcSJeff Roberson #include <sys/cpuset.h> 583f289c3fSJeff Roberson #include <sys/domainset.h> 59d7f687fcSJeff Roberson #include <sys/sx.h> 60d7f687fcSJeff Roberson #include <sys/queue.h> 61e3709597SAttilio Rao #include <sys/libkern.h> 62d7f687fcSJeff Roberson #include <sys/limits.h> 63a03ee000SJeff Roberson #include <sys/bus.h> 64a03ee000SJeff Roberson #include <sys/interrupt.h> 653f289c3fSJeff Roberson #include <sys/vmmeter.h> 66586ed321SDmitry Chagin #include <sys/ktrace.h> 67d7f687fcSJeff Roberson 68d7f687fcSJeff Roberson #include <vm/uma.h> 69c0ae6688SJohn Baldwin #include <vm/vm.h> 703f289c3fSJeff Roberson #include <vm/vm_object.h> 7130c5525bSAndrew Gallatin #include <vm/vm_page.h> 7230c5525bSAndrew Gallatin #include <vm/vm_pageout.h> 73e5818a53SJeff Roberson #include <vm/vm_extern.h> 7430c5525bSAndrew Gallatin #include <vm/vm_param.h> 7530c5525bSAndrew Gallatin #include <vm/vm_phys.h> 7630c5525bSAndrew Gallatin #include <vm/vm_pagequeue.h> 77d7f687fcSJeff Roberson 78dea0ed66SBjoern A. Zeeb #ifdef DDB 79dea0ed66SBjoern A. Zeeb #include <ddb/ddb.h> 80dea0ed66SBjoern A. Zeeb #endif /* DDB */ 81dea0ed66SBjoern A. Zeeb 82d7f687fcSJeff Roberson /* 83d7f687fcSJeff Roberson * cpusets provide a mechanism for creating and manipulating sets of 84d7f687fcSJeff Roberson * processors for the purpose of constraining the scheduling of threads to 85d7f687fcSJeff Roberson * specific processors. 86d7f687fcSJeff Roberson * 87d7f687fcSJeff Roberson * Each process belongs to an identified set, by default this is set 1. Each 88d7f687fcSJeff Roberson * thread may further restrict the cpus it may run on to a subset of this 89d7f687fcSJeff Roberson * named set. This creates an anonymous set which other threads and processes 90d7f687fcSJeff Roberson * may not join by number. 91d7f687fcSJeff Roberson * 92d7f687fcSJeff Roberson * The named set is referred to herein as the 'base' set to avoid ambiguity. 93d7f687fcSJeff Roberson * This set is usually a child of a 'root' set while the anonymous set may 94d7f687fcSJeff Roberson * simply be referred to as a mask. In the syscall api these are referred to 95d7f687fcSJeff Roberson * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here. 96d7f687fcSJeff Roberson * 97d7f687fcSJeff Roberson * Threads inherit their set from their creator whether it be anonymous or 98d7f687fcSJeff Roberson * not. This means that anonymous sets are immutable because they may be 99d7f687fcSJeff Roberson * shared. To modify an anonymous set a new set is created with the desired 100d7f687fcSJeff Roberson * mask and the same parent as the existing anonymous set. This gives the 10193902625SJohn Baldwin * illusion of each thread having a private mask. 102d7f687fcSJeff Roberson * 103d7f687fcSJeff Roberson * Via the syscall apis a user may ask to retrieve or modify the root, base, 104d7f687fcSJeff Roberson * or mask that is discovered via a pid, tid, or setid. Modifying a set 105d7f687fcSJeff Roberson * modifies all numbered and anonymous child sets to comply with the new mask. 106d7f687fcSJeff Roberson * Modifying a pid or tid's mask applies only to that tid but must still 107d7f687fcSJeff Roberson * exist within the assigned parent set. 108d7f687fcSJeff Roberson * 10986855bf5SJohn Baldwin * A thread may not be assigned to a group separate from other threads in 110d7f687fcSJeff Roberson * the process. This is to remove ambiguity when the setid is queried with 111d7f687fcSJeff Roberson * a pid argument. There is no other technical limitation. 112d7f687fcSJeff Roberson * 113d7f687fcSJeff Roberson * This somewhat complex arrangement is intended to make it easy for 114d7f687fcSJeff Roberson * applications to query available processors and bind their threads to 115d7f687fcSJeff Roberson * specific processors while also allowing administrators to dynamically 116d7f687fcSJeff Roberson * reprovision by changing sets which apply to groups of processes. 117d7f687fcSJeff Roberson * 118d7f687fcSJeff Roberson * A simple application should not concern itself with sets at all and 119d7f687fcSJeff Roberson * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id 12093902625SJohn Baldwin * meaning 'curthread'. It may query available cpus for that tid with a 121d7f687fcSJeff Roberson * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...). 122d7f687fcSJeff Roberson */ 123e5818a53SJeff Roberson 124e5818a53SJeff Roberson LIST_HEAD(domainlist, domainset); 12529bb6c19SMark Johnston struct domainset __read_mostly domainset_firsttouch; 1269978bd99SMark Johnston struct domainset __read_mostly domainset_fixed[MAXMEMDOM]; 12729bb6c19SMark Johnston struct domainset __read_mostly domainset_interleave; 128662e7fa8SMark Johnston struct domainset __read_mostly domainset_prefer[MAXMEMDOM]; 129662e7fa8SMark Johnston struct domainset __read_mostly domainset_roundrobin; 130e5818a53SJeff Roberson 131d7f687fcSJeff Roberson static uma_zone_t cpuset_zone; 1323f289c3fSJeff Roberson static uma_zone_t domainset_zone; 133d7f687fcSJeff Roberson static struct mtx cpuset_lock; 134d7f687fcSJeff Roberson static struct setlist cpuset_ids; 1353f289c3fSJeff Roberson static struct domainlist cpuset_domains; 136d7f687fcSJeff Roberson static struct unrhdr *cpuset_unr; 137e5818a53SJeff Roberson static struct cpuset *cpuset_zero, *cpuset_default, *cpuset_kernel; 13829bb6c19SMark Johnston static struct domainset *domainset0, *domainset2; 13901f74ccdSDmitry Chagin u_int cpusetsizemin = 1; 140a03ee000SJeff Roberson 141444528c0SDavid Xu /* Return the size of cpuset_t at the kernel level */ 14260aa2c85SJonathan Anderson SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD, 143f0188618SHans Petter Selasky SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)"); 144444528c0SDavid Xu 14501f74ccdSDmitry Chagin /* Return the minimum size of cpuset_t allowed by the kernel */ 14601f74ccdSDmitry Chagin SYSCTL_UINT(_kern_sched, OID_AUTO, cpusetsizemin, 14701f74ccdSDmitry Chagin CTLFLAG_RD | CTLFLAG_CAPRD, &cpusetsizemin, 0, 14801f74ccdSDmitry Chagin "The minimum size of cpuset_t allowed by the kernel"); 14901f74ccdSDmitry Chagin 150a03ee000SJeff Roberson cpuset_t *cpuset_root; 151c0ae6688SJohn Baldwin cpuset_t cpuset_domain[MAXMEMDOM]; 152d7f687fcSJeff Roberson 1532058f075SDmitry Chagin static int cpuset_which2(cpuwhich_t *, id_t, struct proc **, struct thread **, 1542058f075SDmitry Chagin struct cpuset **); 1553f289c3fSJeff Roberson static int domainset_valid(const struct domainset *, const struct domainset *); 1563f289c3fSJeff Roberson 1573f289c3fSJeff Roberson /* 1583f289c3fSJeff Roberson * Find the first non-anonymous set starting from 'set'. 1593f289c3fSJeff Roberson */ 1603f289c3fSJeff Roberson static struct cpuset * 1613f289c3fSJeff Roberson cpuset_getbase(struct cpuset *set) 1623f289c3fSJeff Roberson { 1633f289c3fSJeff Roberson 1643f289c3fSJeff Roberson if (set->cs_id == CPUSET_INVALID) 1653f289c3fSJeff Roberson set = set->cs_parent; 1663f289c3fSJeff Roberson return (set); 1673f289c3fSJeff Roberson } 1683f289c3fSJeff Roberson 1693f289c3fSJeff Roberson /* 1703f289c3fSJeff Roberson * Walks up the tree from 'set' to find the root. 1713f289c3fSJeff Roberson */ 1723f289c3fSJeff Roberson static struct cpuset * 1733f289c3fSJeff Roberson cpuset_getroot(struct cpuset *set) 1743f289c3fSJeff Roberson { 1753f289c3fSJeff Roberson 1763f289c3fSJeff Roberson while ((set->cs_flags & CPU_SET_ROOT) == 0 && set->cs_parent != NULL) 1773f289c3fSJeff Roberson set = set->cs_parent; 1783f289c3fSJeff Roberson return (set); 1793f289c3fSJeff Roberson } 1803f289c3fSJeff Roberson 181d7f687fcSJeff Roberson /* 182d7f687fcSJeff Roberson * Acquire a reference to a cpuset, all pointers must be tracked with refs. 183d7f687fcSJeff Roberson */ 184d7f687fcSJeff Roberson struct cpuset * 185d7f687fcSJeff Roberson cpuset_ref(struct cpuset *set) 186d7f687fcSJeff Roberson { 187d7f687fcSJeff Roberson 188d7f687fcSJeff Roberson refcount_acquire(&set->cs_ref); 189d7f687fcSJeff Roberson return (set); 190d7f687fcSJeff Roberson } 191d7f687fcSJeff Roberson 192d7f687fcSJeff Roberson /* 1937a8f695aSBjoern A. Zeeb * Walks up the tree from 'set' to find the root. Returns the root 1947a8f695aSBjoern A. Zeeb * referenced. 1957a8f695aSBjoern A. Zeeb */ 1967a8f695aSBjoern A. Zeeb static struct cpuset * 1977a8f695aSBjoern A. Zeeb cpuset_refroot(struct cpuset *set) 1987a8f695aSBjoern A. Zeeb { 1997a8f695aSBjoern A. Zeeb 2003f289c3fSJeff Roberson return (cpuset_ref(cpuset_getroot(set))); 2017a8f695aSBjoern A. Zeeb } 2027a8f695aSBjoern A. Zeeb 2037a8f695aSBjoern A. Zeeb /* 2047a8f695aSBjoern A. Zeeb * Find the first non-anonymous set starting from 'set'. Returns this set 2057a8f695aSBjoern A. Zeeb * referenced. May return the passed in set with an extra ref if it is 2067a8f695aSBjoern A. Zeeb * not anonymous. 2077a8f695aSBjoern A. Zeeb */ 2087a8f695aSBjoern A. Zeeb static struct cpuset * 2097a8f695aSBjoern A. Zeeb cpuset_refbase(struct cpuset *set) 2107a8f695aSBjoern A. Zeeb { 2117a8f695aSBjoern A. Zeeb 2123f289c3fSJeff Roberson return (cpuset_ref(cpuset_getbase(set))); 2137a8f695aSBjoern A. Zeeb } 2147a8f695aSBjoern A. Zeeb 2157a8f695aSBjoern A. Zeeb /* 21693902625SJohn Baldwin * Release a reference in a context where it is safe to allocate. 217d7f687fcSJeff Roberson */ 218d7f687fcSJeff Roberson void 219d7f687fcSJeff Roberson cpuset_rel(struct cpuset *set) 220d7f687fcSJeff Roberson { 221d7f687fcSJeff Roberson cpusetid_t id; 222d7f687fcSJeff Roberson 223b2780e85SKyle Evans if (refcount_release_if_not_last(&set->cs_ref)) 224d7f687fcSJeff Roberson return; 225d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 226b2780e85SKyle Evans if (!refcount_release(&set->cs_ref)) { 227b2780e85SKyle Evans mtx_unlock_spin(&cpuset_lock); 228b2780e85SKyle Evans return; 229b2780e85SKyle Evans } 230d7f687fcSJeff Roberson LIST_REMOVE(set, cs_siblings); 231d7f687fcSJeff Roberson id = set->cs_id; 232d7f687fcSJeff Roberson if (id != CPUSET_INVALID) 233d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 234d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 235d7f687fcSJeff Roberson cpuset_rel(set->cs_parent); 236d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 237d7f687fcSJeff Roberson if (id != CPUSET_INVALID) 238d7f687fcSJeff Roberson free_unr(cpuset_unr, id); 239d7f687fcSJeff Roberson } 240d7f687fcSJeff Roberson 241d7f687fcSJeff Roberson /* 242d7f687fcSJeff Roberson * Deferred release must be used when in a context that is not safe to 243d7f687fcSJeff Roberson * allocate/free. This places any unreferenced sets on the list 'head'. 244d7f687fcSJeff Roberson */ 245d7f687fcSJeff Roberson static void 246d7f687fcSJeff Roberson cpuset_rel_defer(struct setlist *head, struct cpuset *set) 247d7f687fcSJeff Roberson { 248d7f687fcSJeff Roberson 249b2780e85SKyle Evans if (refcount_release_if_not_last(&set->cs_ref)) 250d7f687fcSJeff Roberson return; 251d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 252b2780e85SKyle Evans if (!refcount_release(&set->cs_ref)) { 253b2780e85SKyle Evans mtx_unlock_spin(&cpuset_lock); 254b2780e85SKyle Evans return; 255b2780e85SKyle Evans } 256d7f687fcSJeff Roberson LIST_REMOVE(set, cs_siblings); 257d7f687fcSJeff Roberson if (set->cs_id != CPUSET_INVALID) 258d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 259d7f687fcSJeff Roberson LIST_INSERT_HEAD(head, set, cs_link); 260d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 261d7f687fcSJeff Roberson } 262d7f687fcSJeff Roberson 263d7f687fcSJeff Roberson /* 264d7f687fcSJeff Roberson * Complete a deferred release. Removes the set from the list provided to 265d7f687fcSJeff Roberson * cpuset_rel_defer. 266d7f687fcSJeff Roberson */ 267d7f687fcSJeff Roberson static void 268d7f687fcSJeff Roberson cpuset_rel_complete(struct cpuset *set) 269d7f687fcSJeff Roberson { 2709c83dab9SKyle Evans cpusetid_t id; 2719c83dab9SKyle Evans 2729c83dab9SKyle Evans id = set->cs_id; 273d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 274d7f687fcSJeff Roberson cpuset_rel(set->cs_parent); 275d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 2769c83dab9SKyle Evans if (id != CPUSET_INVALID) 2779c83dab9SKyle Evans free_unr(cpuset_unr, id); 278d7f687fcSJeff Roberson } 279d7f687fcSJeff Roberson 280d7f687fcSJeff Roberson /* 281d7f687fcSJeff Roberson * Find a set based on an id. Returns it with a ref. 282d7f687fcSJeff Roberson */ 283d7f687fcSJeff Roberson static struct cpuset * 284413628a7SBjoern A. Zeeb cpuset_lookup(cpusetid_t setid, struct thread *td) 285d7f687fcSJeff Roberson { 286d7f687fcSJeff Roberson struct cpuset *set; 287d7f687fcSJeff Roberson 288d7f687fcSJeff Roberson if (setid == CPUSET_INVALID) 289d7f687fcSJeff Roberson return (NULL); 290d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 291d7f687fcSJeff Roberson LIST_FOREACH(set, &cpuset_ids, cs_link) 292d7f687fcSJeff Roberson if (set->cs_id == setid) 293d7f687fcSJeff Roberson break; 294d7f687fcSJeff Roberson if (set) 295d7f687fcSJeff Roberson cpuset_ref(set); 296d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 297413628a7SBjoern A. Zeeb 298413628a7SBjoern A. Zeeb KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__)); 299413628a7SBjoern A. Zeeb if (set != NULL && jailed(td->td_ucred)) { 3000304c731SJamie Gritton struct cpuset *jset, *tset; 301413628a7SBjoern A. Zeeb 3020304c731SJamie Gritton jset = td->td_ucred->cr_prison->pr_cpuset; 3030304c731SJamie Gritton for (tset = set; tset != NULL; tset = tset->cs_parent) 3040304c731SJamie Gritton if (tset == jset) 3050304c731SJamie Gritton break; 3060304c731SJamie Gritton if (tset == NULL) { 307413628a7SBjoern A. Zeeb cpuset_rel(set); 308413628a7SBjoern A. Zeeb set = NULL; 309413628a7SBjoern A. Zeeb } 310413628a7SBjoern A. Zeeb } 311413628a7SBjoern A. Zeeb 312d7f687fcSJeff Roberson return (set); 313d7f687fcSJeff Roberson } 314d7f687fcSJeff Roberson 315d7f687fcSJeff Roberson /* 31630b7c6f9SKyle Evans * Initialize a set in the space provided in 'set' with the provided parameters. 317d7f687fcSJeff Roberson * The set is returned with a single ref. May return EDEADLK if the set 318d7f687fcSJeff Roberson * will have no valid cpu based on restrictions from the parent. 319d7f687fcSJeff Roberson */ 320d7f687fcSJeff Roberson static int 32130b7c6f9SKyle Evans cpuset_init(struct cpuset *set, struct cpuset *parent, 3223f289c3fSJeff Roberson const cpuset_t *mask, struct domainset *domain, cpusetid_t id) 323d7f687fcSJeff Roberson { 324d7f687fcSJeff Roberson 3253f289c3fSJeff Roberson if (domain == NULL) 3263f289c3fSJeff Roberson domain = parent->cs_domain; 3273f289c3fSJeff Roberson if (mask == NULL) 3283f289c3fSJeff Roberson mask = &parent->cs_mask; 32973c40187SJeff Roberson if (!CPU_OVERLAP(&parent->cs_mask, mask)) 33073c40187SJeff Roberson return (EDEADLK); 3313f289c3fSJeff Roberson /* The domain must be prepared ahead of time. */ 3323f289c3fSJeff Roberson if (!domainset_valid(parent->cs_domain, domain)) 3333f289c3fSJeff Roberson return (EDEADLK); 334d7f687fcSJeff Roberson CPU_COPY(mask, &set->cs_mask); 335d7f687fcSJeff Roberson LIST_INIT(&set->cs_children); 336d7f687fcSJeff Roberson refcount_init(&set->cs_ref, 1); 337d7f687fcSJeff Roberson set->cs_flags = 0; 338d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 3393f289c3fSJeff Roberson set->cs_domain = domain; 340e2650af1SStefan Eßer CPU_AND(&set->cs_mask, &set->cs_mask, &parent->cs_mask); 341d7f687fcSJeff Roberson set->cs_id = id; 342d7f687fcSJeff Roberson set->cs_parent = cpuset_ref(parent); 343d7f687fcSJeff Roberson LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings); 344d7f687fcSJeff Roberson if (set->cs_id != CPUSET_INVALID) 345d7f687fcSJeff Roberson LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 346d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 347d7f687fcSJeff Roberson 34873c40187SJeff Roberson return (0); 349d7f687fcSJeff Roberson } 350d7f687fcSJeff Roberson 351d7f687fcSJeff Roberson /* 352d7f687fcSJeff Roberson * Create a new non-anonymous set with the requested parent and mask. May 353d7f687fcSJeff Roberson * return failures if the mask is invalid or a new number can not be 354d7f687fcSJeff Roberson * allocated. 35529d04ea8SKyle Evans * 35629d04ea8SKyle Evans * If *setp is not NULL, then it will be used as-is. The caller must take 35729d04ea8SKyle Evans * into account that *setp will be inserted at the head of cpuset_ids and 35829d04ea8SKyle Evans * plan any potentially conflicting cs_link usage accordingly. 359d7f687fcSJeff Roberson */ 360d7f687fcSJeff Roberson static int 361e84c2db1SJohn Baldwin cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask) 362d7f687fcSJeff Roberson { 363d7f687fcSJeff Roberson struct cpuset *set; 364d7f687fcSJeff Roberson cpusetid_t id; 365d7f687fcSJeff Roberson int error; 36629d04ea8SKyle Evans bool dofree; 367d7f687fcSJeff Roberson 368d7f687fcSJeff Roberson id = alloc_unr(cpuset_unr); 369d7f687fcSJeff Roberson if (id == -1) 370d7f687fcSJeff Roberson return (ENFILE); 37129d04ea8SKyle Evans dofree = (*setp == NULL); 37229d04ea8SKyle Evans if (*setp != NULL) 37329d04ea8SKyle Evans set = *setp; 37429d04ea8SKyle Evans else 3753f289c3fSJeff Roberson *setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 37630b7c6f9SKyle Evans error = cpuset_init(set, parent, mask, NULL, id); 377d7f687fcSJeff Roberson if (error == 0) 378d7f687fcSJeff Roberson return (0); 379d7f687fcSJeff Roberson free_unr(cpuset_unr, id); 38029d04ea8SKyle Evans if (dofree) 381d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 382d7f687fcSJeff Roberson 383d7f687fcSJeff Roberson return (error); 384d7f687fcSJeff Roberson } 385d7f687fcSJeff Roberson 3863f289c3fSJeff Roberson static void 3873f289c3fSJeff Roberson cpuset_freelist_add(struct setlist *list, int count) 3883f289c3fSJeff Roberson { 3893f289c3fSJeff Roberson struct cpuset *set; 3903f289c3fSJeff Roberson int i; 3913f289c3fSJeff Roberson 3923f289c3fSJeff Roberson for (i = 0; i < count; i++) { 3933f289c3fSJeff Roberson set = uma_zalloc(cpuset_zone, M_ZERO | M_WAITOK); 3943f289c3fSJeff Roberson LIST_INSERT_HEAD(list, set, cs_link); 3953f289c3fSJeff Roberson } 3963f289c3fSJeff Roberson } 3973f289c3fSJeff Roberson 3983f289c3fSJeff Roberson static void 3993f289c3fSJeff Roberson cpuset_freelist_init(struct setlist *list, int count) 4003f289c3fSJeff Roberson { 4013f289c3fSJeff Roberson 4023f289c3fSJeff Roberson LIST_INIT(list); 4033f289c3fSJeff Roberson cpuset_freelist_add(list, count); 4043f289c3fSJeff Roberson } 4053f289c3fSJeff Roberson 4063f289c3fSJeff Roberson static void 4073f289c3fSJeff Roberson cpuset_freelist_free(struct setlist *list) 4083f289c3fSJeff Roberson { 4093f289c3fSJeff Roberson struct cpuset *set; 4103f289c3fSJeff Roberson 4113f289c3fSJeff Roberson while ((set = LIST_FIRST(list)) != NULL) { 4123f289c3fSJeff Roberson LIST_REMOVE(set, cs_link); 4133f289c3fSJeff Roberson uma_zfree(cpuset_zone, set); 4143f289c3fSJeff Roberson } 4153f289c3fSJeff Roberson } 4163f289c3fSJeff Roberson 4173f289c3fSJeff Roberson static void 4183f289c3fSJeff Roberson domainset_freelist_add(struct domainlist *list, int count) 4193f289c3fSJeff Roberson { 4203f289c3fSJeff Roberson struct domainset *set; 4213f289c3fSJeff Roberson int i; 4223f289c3fSJeff Roberson 4233f289c3fSJeff Roberson for (i = 0; i < count; i++) { 4243f289c3fSJeff Roberson set = uma_zalloc(domainset_zone, M_ZERO | M_WAITOK); 4253f289c3fSJeff Roberson LIST_INSERT_HEAD(list, set, ds_link); 4263f289c3fSJeff Roberson } 4273f289c3fSJeff Roberson } 4283f289c3fSJeff Roberson 4293f289c3fSJeff Roberson static void 4303f289c3fSJeff Roberson domainset_freelist_init(struct domainlist *list, int count) 4313f289c3fSJeff Roberson { 4323f289c3fSJeff Roberson 4333f289c3fSJeff Roberson LIST_INIT(list); 4343f289c3fSJeff Roberson domainset_freelist_add(list, count); 4353f289c3fSJeff Roberson } 4363f289c3fSJeff Roberson 4373f289c3fSJeff Roberson static void 4383f289c3fSJeff Roberson domainset_freelist_free(struct domainlist *list) 4393f289c3fSJeff Roberson { 4403f289c3fSJeff Roberson struct domainset *set; 4413f289c3fSJeff Roberson 4423f289c3fSJeff Roberson while ((set = LIST_FIRST(list)) != NULL) { 4433f289c3fSJeff Roberson LIST_REMOVE(set, ds_link); 4443f289c3fSJeff Roberson uma_zfree(domainset_zone, set); 4453f289c3fSJeff Roberson } 4463f289c3fSJeff Roberson } 4473f289c3fSJeff Roberson 4483f289c3fSJeff Roberson /* Copy a domainset preserving mask and policy. */ 4493f289c3fSJeff Roberson static void 4503f289c3fSJeff Roberson domainset_copy(const struct domainset *from, struct domainset *to) 4513f289c3fSJeff Roberson { 4523f289c3fSJeff Roberson 4533f289c3fSJeff Roberson DOMAINSET_COPY(&from->ds_mask, &to->ds_mask); 4543f289c3fSJeff Roberson to->ds_policy = from->ds_policy; 4553f289c3fSJeff Roberson to->ds_prefer = from->ds_prefer; 4563f289c3fSJeff Roberson } 4573f289c3fSJeff Roberson 4583f289c3fSJeff Roberson /* Return 1 if mask and policy are equal, otherwise 0. */ 4593f289c3fSJeff Roberson static int 4603f289c3fSJeff Roberson domainset_equal(const struct domainset *one, const struct domainset *two) 4613f289c3fSJeff Roberson { 4623f289c3fSJeff Roberson 4633f289c3fSJeff Roberson return (DOMAINSET_CMP(&one->ds_mask, &two->ds_mask) == 0 && 4643f289c3fSJeff Roberson one->ds_policy == two->ds_policy && 4653f289c3fSJeff Roberson one->ds_prefer == two->ds_prefer); 4663f289c3fSJeff Roberson } 4673f289c3fSJeff Roberson 4683f289c3fSJeff Roberson /* Return 1 if child is a valid subset of parent. */ 4693f289c3fSJeff Roberson static int 4703f289c3fSJeff Roberson domainset_valid(const struct domainset *parent, const struct domainset *child) 4713f289c3fSJeff Roberson { 4723f289c3fSJeff Roberson if (child->ds_policy != DOMAINSET_POLICY_PREFER) 4733f289c3fSJeff Roberson return (DOMAINSET_SUBSET(&parent->ds_mask, &child->ds_mask)); 4743f289c3fSJeff Roberson return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask)); 4753f289c3fSJeff Roberson } 4763f289c3fSJeff Roberson 4773f289c3fSJeff Roberson static int 4783f289c3fSJeff Roberson domainset_restrict(const struct domainset *parent, 4793f289c3fSJeff Roberson const struct domainset *child) 4803f289c3fSJeff Roberson { 4813f289c3fSJeff Roberson if (child->ds_policy != DOMAINSET_POLICY_PREFER) 4823f289c3fSJeff Roberson return (DOMAINSET_OVERLAP(&parent->ds_mask, &child->ds_mask)); 4833f289c3fSJeff Roberson return (DOMAINSET_ISSET(child->ds_prefer, &parent->ds_mask)); 4843f289c3fSJeff Roberson } 4853f289c3fSJeff Roberson 4863f289c3fSJeff Roberson /* 4873f289c3fSJeff Roberson * Lookup or create a domainset. The key is provided in ds_mask and 4883f289c3fSJeff Roberson * ds_policy. If the domainset does not yet exist the storage in 4893f289c3fSJeff Roberson * 'domain' is used to insert. Otherwise this storage is freed to the 4903f289c3fSJeff Roberson * domainset_zone and the existing domainset is returned. 4913f289c3fSJeff Roberson */ 4923f289c3fSJeff Roberson static struct domainset * 4933f289c3fSJeff Roberson _domainset_create(struct domainset *domain, struct domainlist *freelist) 4943f289c3fSJeff Roberson { 4953f289c3fSJeff Roberson struct domainset *ndomain; 49644e4def7SMark Johnston int i, j; 4973f289c3fSJeff Roberson 498b61f3142SMark Johnston KASSERT(domain->ds_cnt <= vm_ndomains, 499b61f3142SMark Johnston ("invalid domain count in domainset %p", domain)); 500b61f3142SMark Johnston KASSERT(domain->ds_policy != DOMAINSET_POLICY_PREFER || 501b61f3142SMark Johnston domain->ds_prefer < vm_ndomains, 502b61f3142SMark Johnston ("invalid preferred domain in domains %p", domain)); 503b61f3142SMark Johnston 5043f289c3fSJeff Roberson mtx_lock_spin(&cpuset_lock); 5053f289c3fSJeff Roberson LIST_FOREACH(ndomain, &cpuset_domains, ds_link) 5063f289c3fSJeff Roberson if (domainset_equal(ndomain, domain)) 5073f289c3fSJeff Roberson break; 5083f289c3fSJeff Roberson /* 5093f289c3fSJeff Roberson * If the domain does not yet exist we insert it and initialize 5103f289c3fSJeff Roberson * various iteration helpers which are not part of the key. 5113f289c3fSJeff Roberson */ 5123f289c3fSJeff Roberson if (ndomain == NULL) { 5133f289c3fSJeff Roberson LIST_INSERT_HEAD(&cpuset_domains, domain, ds_link); 5143f289c3fSJeff Roberson domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask); 51544e4def7SMark Johnston for (i = 0, j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++) 516e5818a53SJeff Roberson if (DOMAINSET_ISSET(i, &domain->ds_mask)) 517e5818a53SJeff Roberson domain->ds_order[j++] = i; 5183f289c3fSJeff Roberson } 5193f289c3fSJeff Roberson mtx_unlock_spin(&cpuset_lock); 5203f289c3fSJeff Roberson if (ndomain == NULL) 5213f289c3fSJeff Roberson return (domain); 5223f289c3fSJeff Roberson if (freelist != NULL) 5233f289c3fSJeff Roberson LIST_INSERT_HEAD(freelist, domain, ds_link); 5243f289c3fSJeff Roberson else 5253f289c3fSJeff Roberson uma_zfree(domainset_zone, domain); 5263f289c3fSJeff Roberson return (ndomain); 5273f289c3fSJeff Roberson 5283f289c3fSJeff Roberson } 5293f289c3fSJeff Roberson 5303f289c3fSJeff Roberson /* 53130c5525bSAndrew Gallatin * Are any of the domains in the mask empty? If so, silently 532920239efSMark Johnston * remove them and update the domainset accordingly. If only empty 533920239efSMark Johnston * domains are present, we must return failure. 53430c5525bSAndrew Gallatin */ 53530c5525bSAndrew Gallatin static bool 53630c5525bSAndrew Gallatin domainset_empty_vm(struct domainset *domain) 53730c5525bSAndrew Gallatin { 5388e697504SMark Johnston domainset_t empty; 5398e697504SMark Johnston int i, j; 54030c5525bSAndrew Gallatin 5418e697504SMark Johnston DOMAINSET_ZERO(&empty); 5428e697504SMark Johnston for (i = 0; i < vm_ndomains; i++) 5438e697504SMark Johnston if (VM_DOMAIN_EMPTY(i)) 5448e697504SMark Johnston DOMAINSET_SET(i, &empty); 5458e697504SMark Johnston if (DOMAINSET_SUBSET(&empty, &domain->ds_mask)) 5468e697504SMark Johnston return (true); 5478e697504SMark Johnston 5488e697504SMark Johnston /* Remove empty domains from the set and recompute. */ 5499825eadfSRyan Libby DOMAINSET_ANDNOT(&domain->ds_mask, &empty); 550920239efSMark Johnston domain->ds_cnt = DOMAINSET_COUNT(&domain->ds_mask); 5518e697504SMark Johnston for (i = j = 0; i < DOMAINSET_FLS(&domain->ds_mask); i++) 552920239efSMark Johnston if (DOMAINSET_ISSET(i, &domain->ds_mask)) 553920239efSMark Johnston domain->ds_order[j++] = i; 5548e697504SMark Johnston 5558e697504SMark Johnston /* Convert a PREFER policy referencing an empty domain to RR. */ 5568e697504SMark Johnston if (domain->ds_policy == DOMAINSET_POLICY_PREFER && 5578e697504SMark Johnston DOMAINSET_ISSET(domain->ds_prefer, &empty)) { 558920239efSMark Johnston domain->ds_policy = DOMAINSET_POLICY_ROUNDROBIN; 559920239efSMark Johnston domain->ds_prefer = -1; 560920239efSMark Johnston } 56130c5525bSAndrew Gallatin 5628e697504SMark Johnston return (false); 56330c5525bSAndrew Gallatin } 56430c5525bSAndrew Gallatin 56530c5525bSAndrew Gallatin /* 5663f289c3fSJeff Roberson * Create or lookup a domainset based on the key held in 'domain'. 5673f289c3fSJeff Roberson */ 568e5818a53SJeff Roberson struct domainset * 5693f289c3fSJeff Roberson domainset_create(const struct domainset *domain) 5703f289c3fSJeff Roberson { 5713f289c3fSJeff Roberson struct domainset *ndomain; 5723f289c3fSJeff Roberson 573e5818a53SJeff Roberson /* 574e5818a53SJeff Roberson * Validate the policy. It must specify a useable policy number with 575e5818a53SJeff Roberson * only valid domains. Preferred must include the preferred domain 576e5818a53SJeff Roberson * in the mask. 577e5818a53SJeff Roberson */ 578e5818a53SJeff Roberson if (domain->ds_policy <= DOMAINSET_POLICY_INVALID || 579e5818a53SJeff Roberson domain->ds_policy > DOMAINSET_POLICY_MAX) 580e5818a53SJeff Roberson return (NULL); 581e5818a53SJeff Roberson if (domain->ds_policy == DOMAINSET_POLICY_PREFER && 582e5818a53SJeff Roberson !DOMAINSET_ISSET(domain->ds_prefer, &domain->ds_mask)) 583e5818a53SJeff Roberson return (NULL); 58429bb6c19SMark Johnston if (!DOMAINSET_SUBSET(&domainset0->ds_mask, &domain->ds_mask)) 585e5818a53SJeff Roberson return (NULL); 5863f289c3fSJeff Roberson ndomain = uma_zalloc(domainset_zone, M_WAITOK | M_ZERO); 5873f289c3fSJeff Roberson domainset_copy(domain, ndomain); 5883f289c3fSJeff Roberson return _domainset_create(ndomain, NULL); 5893f289c3fSJeff Roberson } 5903f289c3fSJeff Roberson 5913f289c3fSJeff Roberson /* 5923f289c3fSJeff Roberson * Update thread domainset pointers. 5933f289c3fSJeff Roberson */ 5943f289c3fSJeff Roberson static void 5953f289c3fSJeff Roberson domainset_notify(void) 5963f289c3fSJeff Roberson { 5973f289c3fSJeff Roberson struct thread *td; 5983f289c3fSJeff Roberson struct proc *p; 5993f289c3fSJeff Roberson 6003f289c3fSJeff Roberson sx_slock(&allproc_lock); 6013f289c3fSJeff Roberson FOREACH_PROC_IN_SYSTEM(p) { 6023f289c3fSJeff Roberson PROC_LOCK(p); 6033f289c3fSJeff Roberson if (p->p_state == PRS_NEW) { 6043f289c3fSJeff Roberson PROC_UNLOCK(p); 6053f289c3fSJeff Roberson continue; 6063f289c3fSJeff Roberson } 6073f289c3fSJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 6083f289c3fSJeff Roberson thread_lock(td); 6093f289c3fSJeff Roberson td->td_domain.dr_policy = td->td_cpuset->cs_domain; 6103f289c3fSJeff Roberson thread_unlock(td); 6113f289c3fSJeff Roberson } 6123f289c3fSJeff Roberson PROC_UNLOCK(p); 6133f289c3fSJeff Roberson } 6143f289c3fSJeff Roberson sx_sunlock(&allproc_lock); 615e5818a53SJeff Roberson kernel_object->domain.dr_policy = cpuset_kernel->cs_domain; 6163f289c3fSJeff Roberson } 6173f289c3fSJeff Roberson 6183f289c3fSJeff Roberson /* 6193f289c3fSJeff Roberson * Create a new set that is a subset of a parent. 6203f289c3fSJeff Roberson */ 6213f289c3fSJeff Roberson static struct domainset * 6223f289c3fSJeff Roberson domainset_shadow(const struct domainset *pdomain, 6233f289c3fSJeff Roberson const struct domainset *domain, struct domainlist *freelist) 6243f289c3fSJeff Roberson { 6253f289c3fSJeff Roberson struct domainset *ndomain; 6263f289c3fSJeff Roberson 6273f289c3fSJeff Roberson ndomain = LIST_FIRST(freelist); 6283f289c3fSJeff Roberson LIST_REMOVE(ndomain, ds_link); 6293f289c3fSJeff Roberson 6303f289c3fSJeff Roberson /* 6313f289c3fSJeff Roberson * Initialize the key from the request. 6323f289c3fSJeff Roberson */ 6333f289c3fSJeff Roberson domainset_copy(domain, ndomain); 6343f289c3fSJeff Roberson 6353f289c3fSJeff Roberson /* 6363f289c3fSJeff Roberson * Restrict the key by the parent. 6373f289c3fSJeff Roberson */ 6383f289c3fSJeff Roberson DOMAINSET_AND(&ndomain->ds_mask, &pdomain->ds_mask); 6393f289c3fSJeff Roberson 6403f289c3fSJeff Roberson return _domainset_create(ndomain, freelist); 6413f289c3fSJeff Roberson } 6423f289c3fSJeff Roberson 643d7f687fcSJeff Roberson /* 644d7f687fcSJeff Roberson * Recursively check for errors that would occur from applying mask to 645d7f687fcSJeff Roberson * the tree of sets starting at 'set'. Checks for sets that would become 646d7f687fcSJeff Roberson * empty as well as RDONLY flags. 647d7f687fcSJeff Roberson */ 648d7f687fcSJeff Roberson static int 649f1b18a66SKyle Evans cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int augment_mask) 650d7f687fcSJeff Roberson { 651d7f687fcSJeff Roberson struct cpuset *nset; 652d7f687fcSJeff Roberson cpuset_t newmask; 653d7f687fcSJeff Roberson int error; 654d7f687fcSJeff Roberson 655d7f687fcSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 656d7f687fcSJeff Roberson if (set->cs_flags & CPU_SET_RDONLY) 657d7f687fcSJeff Roberson return (EPERM); 658f1b18a66SKyle Evans if (augment_mask) { 659e2650af1SStefan Eßer CPU_AND(&newmask, &set->cs_mask, mask); 660c9813d0aSJohn Baldwin } else 661c9813d0aSJohn Baldwin CPU_COPY(mask, &newmask); 662f1b18a66SKyle Evans 663f1b18a66SKyle Evans if (CPU_EMPTY(&newmask)) 664f1b18a66SKyle Evans return (EDEADLK); 66573c40187SJeff Roberson error = 0; 666d7f687fcSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 667c9813d0aSJohn Baldwin if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0) 668d7f687fcSJeff Roberson break; 669d7f687fcSJeff Roberson return (error); 670d7f687fcSJeff Roberson } 671d7f687fcSJeff Roberson 672d7f687fcSJeff Roberson /* 673d7f687fcSJeff Roberson * Applies the mask 'mask' without checking for empty sets or permissions. 674d7f687fcSJeff Roberson */ 675d7f687fcSJeff Roberson static void 676d7f687fcSJeff Roberson cpuset_update(struct cpuset *set, cpuset_t *mask) 677d7f687fcSJeff Roberson { 678d7f687fcSJeff Roberson struct cpuset *nset; 679d7f687fcSJeff Roberson 680d7f687fcSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 681e2650af1SStefan Eßer CPU_AND(&set->cs_mask, &set->cs_mask, mask); 682d7f687fcSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 683d7f687fcSJeff Roberson cpuset_update(nset, &set->cs_mask); 684d7f687fcSJeff Roberson 685d7f687fcSJeff Roberson return; 686d7f687fcSJeff Roberson } 687d7f687fcSJeff Roberson 688d7f687fcSJeff Roberson /* 689d7f687fcSJeff Roberson * Modify the set 'set' to use a copy of the mask provided. Apply this new 690d7f687fcSJeff Roberson * mask to restrict all children in the tree. Checks for validity before 691d7f687fcSJeff Roberson * applying the changes. 692d7f687fcSJeff Roberson */ 693d7f687fcSJeff Roberson static int 694d7f687fcSJeff Roberson cpuset_modify(struct cpuset *set, cpuset_t *mask) 695d7f687fcSJeff Roberson { 69673c40187SJeff Roberson struct cpuset *root; 697d7f687fcSJeff Roberson int error; 698d7f687fcSJeff Roberson 699ba931c08SBjoern A. Zeeb error = priv_check(curthread, PRIV_SCHED_CPUSET); 700d7f687fcSJeff Roberson if (error) 701d7f687fcSJeff Roberson return (error); 70273c40187SJeff Roberson /* 70354a837c8SKyle Evans * In case we are called from within the jail, 7046aaa0b3cSBjoern A. Zeeb * we do not allow modifying the dedicated root 7056aaa0b3cSBjoern A. Zeeb * cpuset of the jail but may still allow to 70654a837c8SKyle Evans * change child sets, including subordinate jails' 70754a837c8SKyle Evans * roots. 7086aaa0b3cSBjoern A. Zeeb */ 70954a837c8SKyle Evans if ((set->cs_flags & CPU_SET_ROOT) != 0 && 71054a837c8SKyle Evans jailed(curthread->td_ucred) && 71154a837c8SKyle Evans set == curthread->td_ucred->cr_prison->pr_cpuset) 7126aaa0b3cSBjoern A. Zeeb return (EPERM); 7136aaa0b3cSBjoern A. Zeeb /* 71473c40187SJeff Roberson * Verify that we have access to this set of 71573c40187SJeff Roberson * cpus. 71673c40187SJeff Roberson */ 71754a837c8SKyle Evans if ((set->cs_flags & (CPU_SET_ROOT | CPU_SET_RDONLY)) == CPU_SET_ROOT) { 71854a837c8SKyle Evans KASSERT(set->cs_parent != NULL, 71954a837c8SKyle Evans ("jail.cpuset=%d is not a proper child of parent jail's root.", 72054a837c8SKyle Evans set->cs_id)); 72154a837c8SKyle Evans 72254a837c8SKyle Evans /* 72354a837c8SKyle Evans * cpuset_getroot() cannot work here due to how top-level jail 72454a837c8SKyle Evans * roots are constructed. Top-level jails are parented to 72554a837c8SKyle Evans * thread0's cpuset (i.e. cpuset 1) rather than the system root. 72654a837c8SKyle Evans */ 72754a837c8SKyle Evans root = set->cs_parent; 72854a837c8SKyle Evans } else { 7293f289c3fSJeff Roberson root = cpuset_getroot(set); 73054a837c8SKyle Evans } 731d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 7323f289c3fSJeff Roberson if (root && !CPU_SUBSET(&root->cs_mask, mask)) { 7333f289c3fSJeff Roberson error = EINVAL; 7343f289c3fSJeff Roberson goto out; 7353f289c3fSJeff Roberson } 736c9813d0aSJohn Baldwin error = cpuset_testupdate(set, mask, 0); 737d7f687fcSJeff Roberson if (error) 738d7f687fcSJeff Roberson goto out; 739d7f687fcSJeff Roberson CPU_COPY(mask, &set->cs_mask); 740c9813d0aSJohn Baldwin cpuset_update(set, mask); 741d7f687fcSJeff Roberson out: 742d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 743d7f687fcSJeff Roberson 744d7f687fcSJeff Roberson return (error); 745d7f687fcSJeff Roberson } 746d7f687fcSJeff Roberson 747d7f687fcSJeff Roberson /* 7483f289c3fSJeff Roberson * Recursively check for errors that would occur from applying mask to 7493f289c3fSJeff Roberson * the tree of sets starting at 'set'. Checks for sets that would become 7503f289c3fSJeff Roberson * empty as well as RDONLY flags. 7513f289c3fSJeff Roberson */ 7523f289c3fSJeff Roberson static int 7533f289c3fSJeff Roberson cpuset_testupdate_domain(struct cpuset *set, struct domainset *dset, 754f1b18a66SKyle Evans struct domainset *orig, int *count, int augment_mask __unused) 7553f289c3fSJeff Roberson { 7563f289c3fSJeff Roberson struct cpuset *nset; 7573f289c3fSJeff Roberson struct domainset *domain; 7583f289c3fSJeff Roberson struct domainset newset; 7593f289c3fSJeff Roberson int error; 7603f289c3fSJeff Roberson 7613f289c3fSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 7623f289c3fSJeff Roberson if (set->cs_flags & CPU_SET_RDONLY) 7633f289c3fSJeff Roberson return (EPERM); 7643f289c3fSJeff Roberson domain = set->cs_domain; 7653f289c3fSJeff Roberson domainset_copy(domain, &newset); 7663f289c3fSJeff Roberson if (!domainset_equal(domain, orig)) { 7673f289c3fSJeff Roberson if (!domainset_restrict(domain, dset)) 7683f289c3fSJeff Roberson return (EDEADLK); 7693f289c3fSJeff Roberson DOMAINSET_AND(&newset.ds_mask, &dset->ds_mask); 7703f289c3fSJeff Roberson /* Count the number of domains that are changing. */ 7713f289c3fSJeff Roberson (*count)++; 7723f289c3fSJeff Roberson } 7733f289c3fSJeff Roberson error = 0; 7743f289c3fSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 7753f289c3fSJeff Roberson if ((error = cpuset_testupdate_domain(nset, &newset, domain, 7763f289c3fSJeff Roberson count, 1)) != 0) 7773f289c3fSJeff Roberson break; 7783f289c3fSJeff Roberson return (error); 7793f289c3fSJeff Roberson } 7803f289c3fSJeff Roberson 7813f289c3fSJeff Roberson /* 7823f289c3fSJeff Roberson * Applies the mask 'mask' without checking for empty sets or permissions. 7833f289c3fSJeff Roberson */ 7843f289c3fSJeff Roberson static void 7853f289c3fSJeff Roberson cpuset_update_domain(struct cpuset *set, struct domainset *domain, 7863f289c3fSJeff Roberson struct domainset *orig, struct domainlist *domains) 7873f289c3fSJeff Roberson { 7883f289c3fSJeff Roberson struct cpuset *nset; 7893f289c3fSJeff Roberson 7903f289c3fSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 7913f289c3fSJeff Roberson /* 7923f289c3fSJeff Roberson * If this domainset has changed from the parent we must calculate 7933f289c3fSJeff Roberson * a new set. Otherwise it simply inherits from the parent. When 7943f289c3fSJeff Roberson * we inherit from the parent we get a new mask and policy. If the 7953f289c3fSJeff Roberson * set is modified from the parent we keep the policy and only 7963f289c3fSJeff Roberson * update the mask. 7973f289c3fSJeff Roberson */ 7983f289c3fSJeff Roberson if (set->cs_domain != orig) { 7993f289c3fSJeff Roberson orig = set->cs_domain; 8003f289c3fSJeff Roberson set->cs_domain = domainset_shadow(domain, orig, domains); 8013f289c3fSJeff Roberson } else 8023f289c3fSJeff Roberson set->cs_domain = domain; 8033f289c3fSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 8043f289c3fSJeff Roberson cpuset_update_domain(nset, set->cs_domain, orig, domains); 8053f289c3fSJeff Roberson 8063f289c3fSJeff Roberson return; 8073f289c3fSJeff Roberson } 8083f289c3fSJeff Roberson 8093f289c3fSJeff Roberson /* 8103f289c3fSJeff Roberson * Modify the set 'set' to use a copy the domainset provided. Apply this new 8113f289c3fSJeff Roberson * mask to restrict all children in the tree. Checks for validity before 8123f289c3fSJeff Roberson * applying the changes. 8133f289c3fSJeff Roberson */ 8143f289c3fSJeff Roberson static int 8153f289c3fSJeff Roberson cpuset_modify_domain(struct cpuset *set, struct domainset *domain) 8163f289c3fSJeff Roberson { 8173f289c3fSJeff Roberson struct domainlist domains; 8183f289c3fSJeff Roberson struct domainset temp; 8193f289c3fSJeff Roberson struct domainset *dset; 8203f289c3fSJeff Roberson struct cpuset *root; 8213f289c3fSJeff Roberson int ndomains, needed; 8223f289c3fSJeff Roberson int error; 8233f289c3fSJeff Roberson 8243f289c3fSJeff Roberson error = priv_check(curthread, PRIV_SCHED_CPUSET); 8253f289c3fSJeff Roberson if (error) 8263f289c3fSJeff Roberson return (error); 8273f289c3fSJeff Roberson /* 8283f289c3fSJeff Roberson * In case we are called from within the jail 8293f289c3fSJeff Roberson * we do not allow modifying the dedicated root 8303f289c3fSJeff Roberson * cpuset of the jail but may still allow to 8313f289c3fSJeff Roberson * change child sets. 8323f289c3fSJeff Roberson */ 8333f289c3fSJeff Roberson if (jailed(curthread->td_ucred) && 8343f289c3fSJeff Roberson set->cs_flags & CPU_SET_ROOT) 8353f289c3fSJeff Roberson return (EPERM); 8363f289c3fSJeff Roberson domainset_freelist_init(&domains, 0); 8373f289c3fSJeff Roberson domain = domainset_create(domain); 838e07e3fa3SKyle Evans ndomains = 0; 839e07e3fa3SKyle Evans 8403f289c3fSJeff Roberson mtx_lock_spin(&cpuset_lock); 841e07e3fa3SKyle Evans for (;;) { 842e07e3fa3SKyle Evans root = cpuset_getroot(set); 8433f289c3fSJeff Roberson dset = root->cs_domain; 8443f289c3fSJeff Roberson /* 8453f289c3fSJeff Roberson * Verify that we have access to this set of domains. 8463f289c3fSJeff Roberson */ 84745cdd437SMark Johnston if (!domainset_valid(dset, domain)) { 8483f289c3fSJeff Roberson error = EINVAL; 8493f289c3fSJeff Roberson goto out; 8503f289c3fSJeff Roberson } 8513f289c3fSJeff Roberson /* 8523f289c3fSJeff Roberson * If applying prefer we keep the current set as the fallback. 8533f289c3fSJeff Roberson */ 8543f289c3fSJeff Roberson if (domain->ds_policy == DOMAINSET_POLICY_PREFER) 8553f289c3fSJeff Roberson DOMAINSET_COPY(&set->cs_domain->ds_mask, 8563f289c3fSJeff Roberson &domain->ds_mask); 8573f289c3fSJeff Roberson /* 8583f289c3fSJeff Roberson * Determine whether we can apply this set of domains and 8593f289c3fSJeff Roberson * how many new domain structures it will require. 8603f289c3fSJeff Roberson */ 8613f289c3fSJeff Roberson domainset_copy(domain, &temp); 8623f289c3fSJeff Roberson needed = 0; 8633f289c3fSJeff Roberson error = cpuset_testupdate_domain(set, &temp, set->cs_domain, 8643f289c3fSJeff Roberson &needed, 0); 8653f289c3fSJeff Roberson if (error) 8663f289c3fSJeff Roberson goto out; 867e07e3fa3SKyle Evans if (ndomains >= needed) 868e07e3fa3SKyle Evans break; 869e07e3fa3SKyle Evans 870e07e3fa3SKyle Evans /* Dropping the lock; we'll need to re-evaluate again. */ 871e07e3fa3SKyle Evans mtx_unlock_spin(&cpuset_lock); 872e07e3fa3SKyle Evans domainset_freelist_add(&domains, needed - ndomains); 873e07e3fa3SKyle Evans ndomains = needed; 874e07e3fa3SKyle Evans mtx_lock_spin(&cpuset_lock); 875e07e3fa3SKyle Evans } 8763f289c3fSJeff Roberson dset = set->cs_domain; 8773f289c3fSJeff Roberson cpuset_update_domain(set, domain, dset, &domains); 8783f289c3fSJeff Roberson out: 8793f289c3fSJeff Roberson mtx_unlock_spin(&cpuset_lock); 8803f289c3fSJeff Roberson domainset_freelist_free(&domains); 8813f289c3fSJeff Roberson if (error == 0) 8823f289c3fSJeff Roberson domainset_notify(); 8833f289c3fSJeff Roberson 8843f289c3fSJeff Roberson return (error); 8853f289c3fSJeff Roberson } 8863f289c3fSJeff Roberson 8873f289c3fSJeff Roberson /* 888d7f687fcSJeff Roberson * Resolve the 'which' parameter of several cpuset apis. 889d7f687fcSJeff Roberson * 890d7f687fcSJeff Roberson * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 891d7f687fcSJeff Roberson * checks for permission via p_cansched(). 892d7f687fcSJeff Roberson * 893d7f687fcSJeff Roberson * For WHICH_SET returns a valid set with a new reference. 894d7f687fcSJeff Roberson * 895d7f687fcSJeff Roberson * -1 may be supplied for any argument to mean the current proc/thread or 896d7f687fcSJeff Roberson * the base set of the current thread. May fail with ESRCH/EPERM. 897d7f687fcSJeff Roberson */ 8985bbb2169SAdrian Chadd int 899d7f687fcSJeff Roberson cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 900d7f687fcSJeff Roberson struct cpuset **setp) 901d7f687fcSJeff Roberson { 902d7f687fcSJeff Roberson struct cpuset *set; 903d7f687fcSJeff Roberson struct thread *td; 904d7f687fcSJeff Roberson struct proc *p; 905d7f687fcSJeff Roberson int error; 906d7f687fcSJeff Roberson 907d7f687fcSJeff Roberson *pp = p = NULL; 908d7f687fcSJeff Roberson *tdp = td = NULL; 909d7f687fcSJeff Roberson *setp = set = NULL; 910d7f687fcSJeff Roberson switch (which) { 911d7f687fcSJeff Roberson case CPU_WHICH_PID: 912d7f687fcSJeff Roberson if (id == -1) { 913d7f687fcSJeff Roberson PROC_LOCK(curproc); 914d7f687fcSJeff Roberson p = curproc; 915d7f687fcSJeff Roberson break; 916d7f687fcSJeff Roberson } 917d7f687fcSJeff Roberson if ((p = pfind(id)) == NULL) 918d7f687fcSJeff Roberson return (ESRCH); 919d7f687fcSJeff Roberson break; 920d7f687fcSJeff Roberson case CPU_WHICH_TID: 921d7f687fcSJeff Roberson if (id == -1) { 922d7f687fcSJeff Roberson PROC_LOCK(curproc); 923d7f687fcSJeff Roberson p = curproc; 924d7f687fcSJeff Roberson td = curthread; 925d7f687fcSJeff Roberson break; 926d7f687fcSJeff Roberson } 92742fe684cSDavid Xu td = tdfind(id, -1); 928d7f687fcSJeff Roberson if (td == NULL) 929d7f687fcSJeff Roberson return (ESRCH); 93042fe684cSDavid Xu p = td->td_proc; 931d7f687fcSJeff Roberson break; 932c21b080fSDmitry Chagin case CPU_WHICH_TIDPID: 933c21b080fSDmitry Chagin if (id == -1) { 934c21b080fSDmitry Chagin PROC_LOCK(curproc); 935c21b080fSDmitry Chagin td = curthread; 936c21b080fSDmitry Chagin p = curproc; 937c21b080fSDmitry Chagin } else if (id > PID_MAX) { 938c21b080fSDmitry Chagin td = tdfind(id, -1); 939c21b080fSDmitry Chagin if (td == NULL) 940c21b080fSDmitry Chagin return (ESRCH); 941c21b080fSDmitry Chagin p = td->td_proc; 942c21b080fSDmitry Chagin } else { 943c21b080fSDmitry Chagin p = pfind(id); 944c21b080fSDmitry Chagin if (p == NULL) 945c21b080fSDmitry Chagin return (ESRCH); 946c21b080fSDmitry Chagin } 947c21b080fSDmitry Chagin break; 948d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 949d7f687fcSJeff Roberson if (id == -1) { 950d7f687fcSJeff Roberson thread_lock(curthread); 951a03ee000SJeff Roberson set = cpuset_refbase(curthread->td_cpuset); 952d7f687fcSJeff Roberson thread_unlock(curthread); 953d7f687fcSJeff Roberson } else 954413628a7SBjoern A. Zeeb set = cpuset_lookup(id, curthread); 955d7f687fcSJeff Roberson if (set) { 956d7f687fcSJeff Roberson *setp = set; 957d7f687fcSJeff Roberson return (0); 958d7f687fcSJeff Roberson } 959d7f687fcSJeff Roberson return (ESRCH); 960413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 961413628a7SBjoern A. Zeeb { 962413628a7SBjoern A. Zeeb /* Find `set' for prison with given id. */ 963413628a7SBjoern A. Zeeb struct prison *pr; 964413628a7SBjoern A. Zeeb 965413628a7SBjoern A. Zeeb sx_slock(&allprison_lock); 9660304c731SJamie Gritton pr = prison_find_child(curthread->td_ucred->cr_prison, id); 967413628a7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 968413628a7SBjoern A. Zeeb if (pr == NULL) 969413628a7SBjoern A. Zeeb return (ESRCH); 970413628a7SBjoern A. Zeeb cpuset_ref(pr->pr_cpuset); 9710304c731SJamie Gritton *setp = pr->pr_cpuset; 972413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 973413628a7SBjoern A. Zeeb return (0); 974413628a7SBjoern A. Zeeb } 9759b33b154SJeff Roberson case CPU_WHICH_IRQ: 976c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 9779b33b154SJeff Roberson return (0); 978d7f687fcSJeff Roberson default: 979d7f687fcSJeff Roberson return (EINVAL); 980d7f687fcSJeff Roberson } 981d7f687fcSJeff Roberson error = p_cansched(curthread, p); 982d7f687fcSJeff Roberson if (error) { 983d7f687fcSJeff Roberson PROC_UNLOCK(p); 984d7f687fcSJeff Roberson return (error); 985d7f687fcSJeff Roberson } 986d7f687fcSJeff Roberson if (td == NULL) 987d7f687fcSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 988d7f687fcSJeff Roberson *pp = p; 989d7f687fcSJeff Roberson *tdp = td; 990d7f687fcSJeff Roberson return (0); 991d7f687fcSJeff Roberson } 992d7f687fcSJeff Roberson 993d7f687fcSJeff Roberson static int 9942058f075SDmitry Chagin cpuset_which2(cpuwhich_t *which, id_t id, struct proc **pp, struct thread **tdp, 9952058f075SDmitry Chagin struct cpuset **setp) 9962058f075SDmitry Chagin { 9972058f075SDmitry Chagin 9982058f075SDmitry Chagin if (*which == CPU_WHICH_TIDPID) { 9992058f075SDmitry Chagin if (id == -1 || id > PID_MAX) 10002058f075SDmitry Chagin *which = CPU_WHICH_TID; 10012058f075SDmitry Chagin else 10022058f075SDmitry Chagin *which = CPU_WHICH_PID; 10032058f075SDmitry Chagin } 10042058f075SDmitry Chagin return (cpuset_which(*which, id, pp, tdp, setp)); 10052058f075SDmitry Chagin } 10062058f075SDmitry Chagin 10072058f075SDmitry Chagin static int 10083f289c3fSJeff Roberson cpuset_testshadow(struct cpuset *set, const cpuset_t *mask, 10093f289c3fSJeff Roberson const struct domainset *domain) 1010d7f687fcSJeff Roberson { 1011d7f687fcSJeff Roberson struct cpuset *parent; 10123f289c3fSJeff Roberson struct domainset *dset; 1013d7f687fcSJeff Roberson 10143f289c3fSJeff Roberson parent = cpuset_getbase(set); 10153f289c3fSJeff Roberson /* 10163f289c3fSJeff Roberson * If we are restricting a cpu mask it must be a subset of the 10173f289c3fSJeff Roberson * parent or invalid CPUs have been specified. 10183f289c3fSJeff Roberson */ 10193f289c3fSJeff Roberson if (mask != NULL && !CPU_SUBSET(&parent->cs_mask, mask)) 10203f289c3fSJeff Roberson return (EINVAL); 10213f289c3fSJeff Roberson 10223f289c3fSJeff Roberson /* 10233f289c3fSJeff Roberson * If we are restricting a domain mask it must be a subset of the 10243f289c3fSJeff Roberson * parent or invalid domains have been specified. 10253f289c3fSJeff Roberson */ 10263f289c3fSJeff Roberson dset = parent->cs_domain; 10273f289c3fSJeff Roberson if (domain != NULL && !domainset_valid(dset, domain)) 10283f289c3fSJeff Roberson return (EINVAL); 10293f289c3fSJeff Roberson 10303f289c3fSJeff Roberson return (0); 1031d7f687fcSJeff Roberson } 1032d7f687fcSJeff Roberson 1033d7f687fcSJeff Roberson /* 10343f289c3fSJeff Roberson * Create an anonymous set with the provided mask in the space provided by 10353f289c3fSJeff Roberson * 'nset'. If the passed in set is anonymous we use its parent otherwise 10363f289c3fSJeff Roberson * the new set is a child of 'set'. 10373f289c3fSJeff Roberson */ 10383f289c3fSJeff Roberson static int 10393f289c3fSJeff Roberson cpuset_shadow(struct cpuset *set, struct cpuset **nsetp, 10403f289c3fSJeff Roberson const cpuset_t *mask, const struct domainset *domain, 10413f289c3fSJeff Roberson struct setlist *cpusets, struct domainlist *domains) 10423f289c3fSJeff Roberson { 10433f289c3fSJeff Roberson struct cpuset *parent; 10443f289c3fSJeff Roberson struct cpuset *nset; 10453f289c3fSJeff Roberson struct domainset *dset; 10463f289c3fSJeff Roberson struct domainset *d; 10473f289c3fSJeff Roberson int error; 10483f289c3fSJeff Roberson 10493f289c3fSJeff Roberson error = cpuset_testshadow(set, mask, domain); 10503f289c3fSJeff Roberson if (error) 10513f289c3fSJeff Roberson return (error); 10523f289c3fSJeff Roberson 10533f289c3fSJeff Roberson parent = cpuset_getbase(set); 10543f289c3fSJeff Roberson dset = parent->cs_domain; 10553f289c3fSJeff Roberson if (mask == NULL) 10563f289c3fSJeff Roberson mask = &set->cs_mask; 10573f289c3fSJeff Roberson if (domain != NULL) 10583f289c3fSJeff Roberson d = domainset_shadow(dset, domain, domains); 10593f289c3fSJeff Roberson else 10603f289c3fSJeff Roberson d = set->cs_domain; 10613f289c3fSJeff Roberson nset = LIST_FIRST(cpusets); 106230b7c6f9SKyle Evans error = cpuset_init(nset, parent, mask, d, CPUSET_INVALID); 10633f289c3fSJeff Roberson if (error == 0) { 10643f289c3fSJeff Roberson LIST_REMOVE(nset, cs_link); 10653f289c3fSJeff Roberson *nsetp = nset; 10663f289c3fSJeff Roberson } 10673f289c3fSJeff Roberson return (error); 10683f289c3fSJeff Roberson } 10693f289c3fSJeff Roberson 10703f289c3fSJeff Roberson static struct cpuset * 10713f289c3fSJeff Roberson cpuset_update_thread(struct thread *td, struct cpuset *nset) 10723f289c3fSJeff Roberson { 10733f289c3fSJeff Roberson struct cpuset *tdset; 10743f289c3fSJeff Roberson 10753f289c3fSJeff Roberson tdset = td->td_cpuset; 10763f289c3fSJeff Roberson td->td_cpuset = nset; 10773f289c3fSJeff Roberson td->td_domain.dr_policy = nset->cs_domain; 10783f289c3fSJeff Roberson sched_affinity(td); 10793f289c3fSJeff Roberson 10803f289c3fSJeff Roberson return (tdset); 10813f289c3fSJeff Roberson } 10823f289c3fSJeff Roberson 10833f289c3fSJeff Roberson static int 10843f289c3fSJeff Roberson cpuset_setproc_test_maskthread(struct cpuset *tdset, cpuset_t *mask, 10853f289c3fSJeff Roberson struct domainset *domain) 10863f289c3fSJeff Roberson { 10873f289c3fSJeff Roberson struct cpuset *parent; 10883f289c3fSJeff Roberson 10893f289c3fSJeff Roberson parent = cpuset_getbase(tdset); 10903f289c3fSJeff Roberson if (mask == NULL) 10913f289c3fSJeff Roberson mask = &tdset->cs_mask; 10923f289c3fSJeff Roberson if (domain == NULL) 10933f289c3fSJeff Roberson domain = tdset->cs_domain; 10943f289c3fSJeff Roberson return cpuset_testshadow(parent, mask, domain); 10953f289c3fSJeff Roberson } 10963f289c3fSJeff Roberson 10973f289c3fSJeff Roberson static int 10983f289c3fSJeff Roberson cpuset_setproc_maskthread(struct cpuset *tdset, cpuset_t *mask, 10993f289c3fSJeff Roberson struct domainset *domain, struct cpuset **nsetp, 11003f289c3fSJeff Roberson struct setlist *freelist, struct domainlist *domainlist) 11013f289c3fSJeff Roberson { 11023f289c3fSJeff Roberson struct cpuset *parent; 11033f289c3fSJeff Roberson 11043f289c3fSJeff Roberson parent = cpuset_getbase(tdset); 11053f289c3fSJeff Roberson if (mask == NULL) 11063f289c3fSJeff Roberson mask = &tdset->cs_mask; 11073f289c3fSJeff Roberson if (domain == NULL) 11083f289c3fSJeff Roberson domain = tdset->cs_domain; 11093f289c3fSJeff Roberson return cpuset_shadow(parent, nsetp, mask, domain, freelist, 11103f289c3fSJeff Roberson domainlist); 11113f289c3fSJeff Roberson } 11123f289c3fSJeff Roberson 11133f289c3fSJeff Roberson static int 11143f289c3fSJeff Roberson cpuset_setproc_setthread_mask(struct cpuset *tdset, struct cpuset *set, 11153f289c3fSJeff Roberson cpuset_t *mask, struct domainset *domain) 11163f289c3fSJeff Roberson { 11173f289c3fSJeff Roberson struct cpuset *parent; 11183f289c3fSJeff Roberson 11193f289c3fSJeff Roberson parent = cpuset_getbase(tdset); 11203f289c3fSJeff Roberson 11213f289c3fSJeff Roberson /* 11223f289c3fSJeff Roberson * If the thread restricted its mask then apply that same 11233f289c3fSJeff Roberson * restriction to the new set, otherwise take it wholesale. 11243f289c3fSJeff Roberson */ 11253f289c3fSJeff Roberson if (CPU_CMP(&tdset->cs_mask, &parent->cs_mask) != 0) { 1126e2650af1SStefan Eßer CPU_AND(mask, &tdset->cs_mask, &set->cs_mask); 11273f289c3fSJeff Roberson } else 11283f289c3fSJeff Roberson CPU_COPY(&set->cs_mask, mask); 11293f289c3fSJeff Roberson 11303f289c3fSJeff Roberson /* 11313f289c3fSJeff Roberson * If the thread restricted the domain then we apply the 11323f289c3fSJeff Roberson * restriction to the new set but retain the policy. 11333f289c3fSJeff Roberson */ 11343f289c3fSJeff Roberson if (tdset->cs_domain != parent->cs_domain) { 11353f289c3fSJeff Roberson domainset_copy(tdset->cs_domain, domain); 11363f289c3fSJeff Roberson DOMAINSET_AND(&domain->ds_mask, &set->cs_domain->ds_mask); 11373f289c3fSJeff Roberson } else 11383f289c3fSJeff Roberson domainset_copy(set->cs_domain, domain); 11393f289c3fSJeff Roberson 11403f289c3fSJeff Roberson if (CPU_EMPTY(mask) || DOMAINSET_EMPTY(&domain->ds_mask)) 11413f289c3fSJeff Roberson return (EDEADLK); 11423f289c3fSJeff Roberson 11433f289c3fSJeff Roberson return (0); 11443f289c3fSJeff Roberson } 11453f289c3fSJeff Roberson 11463f289c3fSJeff Roberson static int 11473f289c3fSJeff Roberson cpuset_setproc_test_setthread(struct cpuset *tdset, struct cpuset *set) 11483f289c3fSJeff Roberson { 11493f289c3fSJeff Roberson struct domainset domain; 11503f289c3fSJeff Roberson cpuset_t mask; 11513f289c3fSJeff Roberson 11523f289c3fSJeff Roberson if (tdset->cs_id != CPUSET_INVALID) 11533f289c3fSJeff Roberson return (0); 11543f289c3fSJeff Roberson return cpuset_setproc_setthread_mask(tdset, set, &mask, &domain); 11553f289c3fSJeff Roberson } 11563f289c3fSJeff Roberson 11573f289c3fSJeff Roberson static int 11583f289c3fSJeff Roberson cpuset_setproc_setthread(struct cpuset *tdset, struct cpuset *set, 11593f289c3fSJeff Roberson struct cpuset **nsetp, struct setlist *freelist, 11603f289c3fSJeff Roberson struct domainlist *domainlist) 11613f289c3fSJeff Roberson { 11623f289c3fSJeff Roberson struct domainset domain; 11633f289c3fSJeff Roberson cpuset_t mask; 11643f289c3fSJeff Roberson int error; 11653f289c3fSJeff Roberson 11663f289c3fSJeff Roberson /* 11673f289c3fSJeff Roberson * If we're replacing on a thread that has not constrained the 11683f289c3fSJeff Roberson * original set we can simply accept the new set. 11693f289c3fSJeff Roberson */ 11703f289c3fSJeff Roberson if (tdset->cs_id != CPUSET_INVALID) { 11713f289c3fSJeff Roberson *nsetp = cpuset_ref(set); 11723f289c3fSJeff Roberson return (0); 11733f289c3fSJeff Roberson } 11743f289c3fSJeff Roberson error = cpuset_setproc_setthread_mask(tdset, set, &mask, &domain); 11753f289c3fSJeff Roberson if (error) 11763f289c3fSJeff Roberson return (error); 11773f289c3fSJeff Roberson 1178dac521ebSKyle Evans return cpuset_shadow(set, nsetp, &mask, &domain, freelist, 11793f289c3fSJeff Roberson domainlist); 11803f289c3fSJeff Roberson } 11813f289c3fSJeff Roberson 1182d431dea5SKyle Evans static int 1183d431dea5SKyle Evans cpuset_setproc_newbase(struct thread *td, struct cpuset *set, 1184d431dea5SKyle Evans struct cpuset *nroot, struct cpuset **nsetp, 1185d431dea5SKyle Evans struct setlist *cpusets, struct domainlist *domainlist) 1186d431dea5SKyle Evans { 1187d431dea5SKyle Evans struct domainset ndomain; 1188d431dea5SKyle Evans cpuset_t nmask; 1189d431dea5SKyle Evans struct cpuset *pbase; 1190d431dea5SKyle Evans int error; 1191d431dea5SKyle Evans 1192d431dea5SKyle Evans pbase = cpuset_getbase(td->td_cpuset); 1193d431dea5SKyle Evans 1194d431dea5SKyle Evans /* Copy process mask, then further apply the new root mask. */ 1195e2650af1SStefan Eßer CPU_AND(&nmask, &pbase->cs_mask, &nroot->cs_mask); 1196d431dea5SKyle Evans 1197d431dea5SKyle Evans domainset_copy(pbase->cs_domain, &ndomain); 1198d431dea5SKyle Evans DOMAINSET_AND(&ndomain.ds_mask, &set->cs_domain->ds_mask); 1199d431dea5SKyle Evans 1200d431dea5SKyle Evans /* Policy is too restrictive, will not work. */ 1201d431dea5SKyle Evans if (CPU_EMPTY(&nmask) || DOMAINSET_EMPTY(&ndomain.ds_mask)) 1202d431dea5SKyle Evans return (EDEADLK); 1203d431dea5SKyle Evans 12043f289c3fSJeff Roberson /* 1205d431dea5SKyle Evans * Remove pbase from the freelist in advance, it'll be pushed to 1206d431dea5SKyle Evans * cpuset_ids on success. We assume here that cpuset_create() will not 1207d431dea5SKyle Evans * touch pbase on failure, and we just enqueue it back to the freelist 1208d431dea5SKyle Evans * to remain in a consistent state. 1209d431dea5SKyle Evans */ 1210d431dea5SKyle Evans pbase = LIST_FIRST(cpusets); 1211d431dea5SKyle Evans LIST_REMOVE(pbase, cs_link); 1212d431dea5SKyle Evans error = cpuset_create(&pbase, set, &nmask); 1213d431dea5SKyle Evans if (error != 0) { 1214d431dea5SKyle Evans LIST_INSERT_HEAD(cpusets, pbase, cs_link); 1215d431dea5SKyle Evans return (error); 1216d431dea5SKyle Evans } 1217d431dea5SKyle Evans 1218d431dea5SKyle Evans /* Duplicates some work from above... oh well. */ 1219d431dea5SKyle Evans pbase->cs_domain = domainset_shadow(set->cs_domain, &ndomain, 1220d431dea5SKyle Evans domainlist); 1221d431dea5SKyle Evans *nsetp = pbase; 1222d431dea5SKyle Evans return (0); 1223d431dea5SKyle Evans } 1224d431dea5SKyle Evans 1225d431dea5SKyle Evans /* 1226d431dea5SKyle Evans * Handle four cases for updating an entire process. 1227d7f687fcSJeff Roberson * 1228d431dea5SKyle Evans * 1) Set is non-null and the process is not rebasing onto a new root. This 1229d431dea5SKyle Evans * reparents all anonymous sets to the provided set and replaces all 1230d431dea5SKyle Evans * non-anonymous td_cpusets with the provided set. 1231d431dea5SKyle Evans * 2) Set is non-null and the process is rebasing onto a new root. This 1232d431dea5SKyle Evans * creates a new base set if the process previously had its own base set, 1233d431dea5SKyle Evans * then reparents all anonymous sets either to that set or the provided set 1234d431dea5SKyle Evans * if one was not created. Non-anonymous sets are similarly replaced. 1235d431dea5SKyle Evans * 3) Mask is non-null. This replaces or creates anonymous sets for every 12363f289c3fSJeff Roberson * thread with the existing base as a parent. 1237d431dea5SKyle Evans * 4) domain is non-null. This creates anonymous sets for every thread 12383f289c3fSJeff Roberson * and replaces the domain set. 1239d7f687fcSJeff Roberson * 1240d7f687fcSJeff Roberson * This is overly complicated because we can't allocate while holding a 1241d7f687fcSJeff Roberson * spinlock and spinlocks must be held while changing and examining thread 1242d7f687fcSJeff Roberson * state. 1243d7f687fcSJeff Roberson */ 1244d7f687fcSJeff Roberson static int 12453f289c3fSJeff Roberson cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask, 1246d431dea5SKyle Evans struct domainset *domain, bool rebase) 1247d7f687fcSJeff Roberson { 1248d7f687fcSJeff Roberson struct setlist freelist; 1249d7f687fcSJeff Roberson struct setlist droplist; 12503f289c3fSJeff Roberson struct domainlist domainlist; 1251d431dea5SKyle Evans struct cpuset *base, *nset, *nroot, *tdroot; 1252d7f687fcSJeff Roberson struct thread *td; 1253d7f687fcSJeff Roberson struct proc *p; 1254d431dea5SKyle Evans int needed; 1255d7f687fcSJeff Roberson int nfree; 1256d7f687fcSJeff Roberson int error; 1257f299c47bSAllan Jude 1258d7f687fcSJeff Roberson /* 1259d7f687fcSJeff Roberson * The algorithm requires two passes due to locking considerations. 1260d7f687fcSJeff Roberson * 1261d7f687fcSJeff Roberson * 1) Lookup the process and acquire the locks in the required order. 1262d7f687fcSJeff Roberson * 2) If enough cpusets have not been allocated release the locks and 1263d7f687fcSJeff Roberson * allocate them. Loop. 1264d7f687fcSJeff Roberson */ 12653f289c3fSJeff Roberson cpuset_freelist_init(&freelist, 1); 12663f289c3fSJeff Roberson domainset_freelist_init(&domainlist, 1); 12673f289c3fSJeff Roberson nfree = 1; 1268d7f687fcSJeff Roberson LIST_INIT(&droplist); 1269d7f687fcSJeff Roberson nfree = 0; 1270d431dea5SKyle Evans base = set; 1271d431dea5SKyle Evans nroot = NULL; 1272d431dea5SKyle Evans if (set != NULL) 1273d431dea5SKyle Evans nroot = cpuset_getroot(set); 1274d7f687fcSJeff Roberson for (;;) { 1275d7f687fcSJeff Roberson error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 1276d7f687fcSJeff Roberson if (error) 1277d7f687fcSJeff Roberson goto out; 1278d431dea5SKyle Evans tdroot = cpuset_getroot(td->td_cpuset); 1279d431dea5SKyle Evans needed = p->p_numthreads; 1280d431dea5SKyle Evans if (set != NULL && rebase && tdroot != nroot) 1281d431dea5SKyle Evans needed++; 1282d431dea5SKyle Evans if (nfree >= needed) 1283d7f687fcSJeff Roberson break; 1284d7f687fcSJeff Roberson PROC_UNLOCK(p); 1285d431dea5SKyle Evans if (nfree < needed) { 1286d431dea5SKyle Evans cpuset_freelist_add(&freelist, needed - nfree); 1287d431dea5SKyle Evans domainset_freelist_add(&domainlist, needed - nfree); 1288d431dea5SKyle Evans nfree = needed; 1289d7f687fcSJeff Roberson } 1290d7f687fcSJeff Roberson } 1291d7f687fcSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 1292d431dea5SKyle Evans 1293d431dea5SKyle Evans /* 1294d431dea5SKyle Evans * If we're changing roots and the root set is what has been specified 1295d431dea5SKyle Evans * as the parent, then we'll check if the process was previously using 1296d431dea5SKyle Evans * the root set and, if it wasn't, create a new base with the process's 1297d431dea5SKyle Evans * mask applied to it. 129860c4ec80SKyle Evans * 129960c4ec80SKyle Evans * If the new root is incompatible with the existing mask, then we allow 130060c4ec80SKyle Evans * the process to take on the new root if and only if they have 130160c4ec80SKyle Evans * privilege to widen their mask anyways. Unprivileged processes get 130260c4ec80SKyle Evans * rejected with EDEADLK. 1303d431dea5SKyle Evans */ 1304d431dea5SKyle Evans if (set != NULL && rebase && nroot != tdroot) { 1305d431dea5SKyle Evans cpusetid_t base_id, root_id; 1306d431dea5SKyle Evans 1307d431dea5SKyle Evans root_id = td->td_ucred->cr_prison->pr_cpuset->cs_id; 1308d431dea5SKyle Evans base_id = cpuset_getbase(td->td_cpuset)->cs_id; 1309d431dea5SKyle Evans 1310d431dea5SKyle Evans if (base_id != root_id) { 1311d431dea5SKyle Evans error = cpuset_setproc_newbase(td, set, nroot, &base, 1312d431dea5SKyle Evans &freelist, &domainlist); 131360c4ec80SKyle Evans if (error == EDEADLK && 131460c4ec80SKyle Evans priv_check(td, PRIV_SCHED_CPUSET) == 0) 131560c4ec80SKyle Evans error = 0; 1316d431dea5SKyle Evans if (error != 0) 1317d431dea5SKyle Evans goto unlock_out; 1318d431dea5SKyle Evans } 1319d431dea5SKyle Evans } 1320d431dea5SKyle Evans 1321d7f687fcSJeff Roberson /* 1322d7f687fcSJeff Roberson * Now that the appropriate locks are held and we have enough cpusets, 132373c40187SJeff Roberson * make sure the operation will succeed before applying changes. The 132473c40187SJeff Roberson * proc lock prevents td_cpuset from changing between calls. 1325d7f687fcSJeff Roberson */ 1326d7f687fcSJeff Roberson error = 0; 1327d7f687fcSJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 132873c40187SJeff Roberson thread_lock(td); 13293f289c3fSJeff Roberson if (set != NULL) 13303f289c3fSJeff Roberson error = cpuset_setproc_test_setthread(td->td_cpuset, 1331d431dea5SKyle Evans base); 13323f289c3fSJeff Roberson else 13333f289c3fSJeff Roberson error = cpuset_setproc_test_maskthread(td->td_cpuset, 13343f289c3fSJeff Roberson mask, domain); 133573c40187SJeff Roberson thread_unlock(td); 133673c40187SJeff Roberson if (error) 133773c40187SJeff Roberson goto unlock_out; 133873c40187SJeff Roberson } 133973c40187SJeff Roberson /* 134073c40187SJeff Roberson * Replace each thread's cpuset while using deferred release. We 1341374ae2a3SJeff Roberson * must do this because the thread lock must be held while operating 1342374ae2a3SJeff Roberson * on the thread and this limits the type of operations allowed. 134373c40187SJeff Roberson */ 134473c40187SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 1345d7f687fcSJeff Roberson thread_lock(td); 13463f289c3fSJeff Roberson if (set != NULL) 1347d431dea5SKyle Evans error = cpuset_setproc_setthread(td->td_cpuset, base, 13483f289c3fSJeff Roberson &nset, &freelist, &domainlist); 1349d7f687fcSJeff Roberson else 13503f289c3fSJeff Roberson error = cpuset_setproc_maskthread(td->td_cpuset, mask, 13513f289c3fSJeff Roberson domain, &nset, &freelist, &domainlist); 1352d7f687fcSJeff Roberson if (error) { 1353d7f687fcSJeff Roberson thread_unlock(td); 1354d7f687fcSJeff Roberson break; 1355d7f687fcSJeff Roberson } 13563f289c3fSJeff Roberson cpuset_rel_defer(&droplist, cpuset_update_thread(td, nset)); 1357d7f687fcSJeff Roberson thread_unlock(td); 1358d7f687fcSJeff Roberson } 135973c40187SJeff Roberson unlock_out: 1360d7f687fcSJeff Roberson PROC_UNLOCK(p); 1361d7f687fcSJeff Roberson out: 1362d431dea5SKyle Evans if (base != NULL && base != set) 1363d431dea5SKyle Evans cpuset_rel(base); 1364d7f687fcSJeff Roberson while ((nset = LIST_FIRST(&droplist)) != NULL) 1365d7f687fcSJeff Roberson cpuset_rel_complete(nset); 13663f289c3fSJeff Roberson cpuset_freelist_free(&freelist); 13673f289c3fSJeff Roberson domainset_freelist_free(&domainlist); 1368d7f687fcSJeff Roberson return (error); 1369d7f687fcSJeff Roberson } 1370d7f687fcSJeff Roberson 1371e5818a53SJeff Roberson static int 1372e5818a53SJeff Roberson bitset_strprint(char *buf, size_t bufsiz, const struct bitset *set, int setlen) 1373e5818a53SJeff Roberson { 1374e5818a53SJeff Roberson size_t bytes; 1375e5818a53SJeff Roberson int i, once; 1376e5818a53SJeff Roberson char *p; 1377e5818a53SJeff Roberson 1378e5818a53SJeff Roberson once = 0; 1379e5818a53SJeff Roberson p = buf; 1380e5818a53SJeff Roberson for (i = 0; i < __bitset_words(setlen); i++) { 1381e5818a53SJeff Roberson if (once != 0) { 1382e5818a53SJeff Roberson if (bufsiz < 1) 1383e5818a53SJeff Roberson return (0); 1384e5818a53SJeff Roberson *p = ','; 1385e5818a53SJeff Roberson p++; 1386e5818a53SJeff Roberson bufsiz--; 1387e5818a53SJeff Roberson } else 1388e5818a53SJeff Roberson once = 1; 1389e5818a53SJeff Roberson if (bufsiz < sizeof(__STRING(ULONG_MAX))) 1390e5818a53SJeff Roberson return (0); 1391e5818a53SJeff Roberson bytes = snprintf(p, bufsiz, "%lx", set->__bits[i]); 1392e5818a53SJeff Roberson p += bytes; 1393e5818a53SJeff Roberson bufsiz -= bytes; 1394e5818a53SJeff Roberson } 1395e5818a53SJeff Roberson return (p - buf); 1396e5818a53SJeff Roberson } 1397e5818a53SJeff Roberson 1398e5818a53SJeff Roberson static int 1399e5818a53SJeff Roberson bitset_strscan(struct bitset *set, int setlen, const char *buf) 1400e5818a53SJeff Roberson { 1401e5818a53SJeff Roberson int i, ret; 1402e5818a53SJeff Roberson const char *p; 1403e5818a53SJeff Roberson 1404e5818a53SJeff Roberson BIT_ZERO(setlen, set); 1405e5818a53SJeff Roberson p = buf; 1406e5818a53SJeff Roberson for (i = 0; i < __bitset_words(setlen); i++) { 1407e5818a53SJeff Roberson if (*p == ',') { 1408e5818a53SJeff Roberson p++; 1409e5818a53SJeff Roberson continue; 1410e5818a53SJeff Roberson } 1411e5818a53SJeff Roberson ret = sscanf(p, "%lx", &set->__bits[i]); 1412e5818a53SJeff Roberson if (ret == 0 || ret == -1) 1413e5818a53SJeff Roberson break; 1414e5818a53SJeff Roberson while (isxdigit(*p)) 1415e5818a53SJeff Roberson p++; 1416e5818a53SJeff Roberson } 1417e5818a53SJeff Roberson return (p - buf); 1418e5818a53SJeff Roberson } 1419e5818a53SJeff Roberson 1420d7f687fcSJeff Roberson /* 142171a19bdcSAttilio Rao * Return a string representing a valid layout for a cpuset_t object. 142271a19bdcSAttilio Rao * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 142371a19bdcSAttilio Rao */ 142471a19bdcSAttilio Rao char * 142571a19bdcSAttilio Rao cpusetobj_strprint(char *buf, const cpuset_t *set) 142671a19bdcSAttilio Rao { 142771a19bdcSAttilio Rao 1428e5818a53SJeff Roberson bitset_strprint(buf, CPUSETBUFSIZ, (const struct bitset *)set, 1429e5818a53SJeff Roberson CPU_SETSIZE); 143071a19bdcSAttilio Rao return (buf); 143171a19bdcSAttilio Rao } 143271a19bdcSAttilio Rao 143371a19bdcSAttilio Rao /* 1434e3709597SAttilio Rao * Build a valid cpuset_t object from a string representation. 1435e3709597SAttilio Rao * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 1436e3709597SAttilio Rao */ 1437e3709597SAttilio Rao int 1438e3709597SAttilio Rao cpusetobj_strscan(cpuset_t *set, const char *buf) 1439e3709597SAttilio Rao { 1440e5818a53SJeff Roberson char p; 1441e3709597SAttilio Rao 1442e3709597SAttilio Rao if (strlen(buf) > CPUSETBUFSIZ - 1) 1443e3709597SAttilio Rao return (-1); 1444e3709597SAttilio Rao 1445e5818a53SJeff Roberson p = buf[bitset_strscan((struct bitset *)set, CPU_SETSIZE, buf)]; 1446e5818a53SJeff Roberson if (p != '\0') 1447e3709597SAttilio Rao return (-1); 1448e3709597SAttilio Rao 1449e3709597SAttilio Rao return (0); 1450e3709597SAttilio Rao } 1451e3709597SAttilio Rao 1452e3709597SAttilio Rao /* 1453e5818a53SJeff Roberson * Handle a domainset specifier in the sysctl tree. A poiner to a pointer to 1454e5818a53SJeff Roberson * a domainset is in arg1. If the user specifies a valid domainset the 1455e5818a53SJeff Roberson * pointer is updated. 1456e5818a53SJeff Roberson * 1457e5818a53SJeff Roberson * Format is: 1458e5818a53SJeff Roberson * hex mask word 0,hex mask word 1,...:decimal policy:decimal preferred 1459e5818a53SJeff Roberson */ 1460e5818a53SJeff Roberson int 1461e5818a53SJeff Roberson sysctl_handle_domainset(SYSCTL_HANDLER_ARGS) 1462e5818a53SJeff Roberson { 1463e5818a53SJeff Roberson char buf[DOMAINSETBUFSIZ]; 1464e5818a53SJeff Roberson struct domainset *dset; 1465e5818a53SJeff Roberson struct domainset key; 1466e5818a53SJeff Roberson int policy, prefer, error; 1467e5818a53SJeff Roberson char *p; 1468e5818a53SJeff Roberson 1469e5818a53SJeff Roberson dset = *(struct domainset **)arg1; 1470e5818a53SJeff Roberson error = 0; 1471e5818a53SJeff Roberson 1472e5818a53SJeff Roberson if (dset != NULL) { 1473e5818a53SJeff Roberson p = buf + bitset_strprint(buf, DOMAINSETBUFSIZ, 1474e5818a53SJeff Roberson (const struct bitset *)&dset->ds_mask, DOMAINSET_SETSIZE); 1475e5818a53SJeff Roberson sprintf(p, ":%d:%d", dset->ds_policy, dset->ds_prefer); 1476e5818a53SJeff Roberson } else 1477e5818a53SJeff Roberson sprintf(buf, "<NULL>"); 1478e5818a53SJeff Roberson error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 1479e5818a53SJeff Roberson if (error != 0 || req->newptr == NULL) 1480e5818a53SJeff Roberson return (error); 1481e5818a53SJeff Roberson 1482e5818a53SJeff Roberson /* 1483e5818a53SJeff Roberson * Read in and validate the string. 1484e5818a53SJeff Roberson */ 1485e5818a53SJeff Roberson memset(&key, 0, sizeof(key)); 1486e5818a53SJeff Roberson p = &buf[bitset_strscan((struct bitset *)&key.ds_mask, 1487e5818a53SJeff Roberson DOMAINSET_SETSIZE, buf)]; 1488e5818a53SJeff Roberson if (p == buf) 1489e5818a53SJeff Roberson return (EINVAL); 1490e5818a53SJeff Roberson if (sscanf(p, ":%d:%d", &policy, &prefer) != 2) 1491e5818a53SJeff Roberson return (EINVAL); 1492e5818a53SJeff Roberson key.ds_policy = policy; 1493e5818a53SJeff Roberson key.ds_prefer = prefer; 1494e5818a53SJeff Roberson 1495e5818a53SJeff Roberson /* Domainset_create() validates the policy.*/ 1496e5818a53SJeff Roberson dset = domainset_create(&key); 1497e5818a53SJeff Roberson if (dset == NULL) 1498e5818a53SJeff Roberson return (EINVAL); 1499e5818a53SJeff Roberson *(struct domainset **)arg1 = dset; 1500e5818a53SJeff Roberson 1501e5818a53SJeff Roberson return (error); 1502e5818a53SJeff Roberson } 1503e5818a53SJeff Roberson 1504e5818a53SJeff Roberson /* 15053f289c3fSJeff Roberson * Apply an anonymous mask or a domain to a single thread. 1506d7f687fcSJeff Roberson */ 15073f289c3fSJeff Roberson static int 15083f289c3fSJeff Roberson _cpuset_setthread(lwpid_t id, cpuset_t *mask, struct domainset *domain) 1509d7f687fcSJeff Roberson { 15103f289c3fSJeff Roberson struct setlist cpusets; 15113f289c3fSJeff Roberson struct domainlist domainlist; 1512d7f687fcSJeff Roberson struct cpuset *nset; 1513d7f687fcSJeff Roberson struct cpuset *set; 1514d7f687fcSJeff Roberson struct thread *td; 1515d7f687fcSJeff Roberson struct proc *p; 1516d7f687fcSJeff Roberson int error; 1517d7f687fcSJeff Roberson 15183f289c3fSJeff Roberson cpuset_freelist_init(&cpusets, 1); 15193f289c3fSJeff Roberson domainset_freelist_init(&domainlist, domain != NULL); 15208bd75bddSJeff Roberson error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 1521d7f687fcSJeff Roberson if (error) 1522d7f687fcSJeff Roberson goto out; 1523a03ee000SJeff Roberson set = NULL; 1524d7f687fcSJeff Roberson thread_lock(td); 15253f289c3fSJeff Roberson error = cpuset_shadow(td->td_cpuset, &nset, mask, domain, 15263f289c3fSJeff Roberson &cpusets, &domainlist); 15273f289c3fSJeff Roberson if (error == 0) 15283f289c3fSJeff Roberson set = cpuset_update_thread(td, nset); 1529d7f687fcSJeff Roberson thread_unlock(td); 1530d7f687fcSJeff Roberson PROC_UNLOCK(p); 1531a03ee000SJeff Roberson if (set) 1532a03ee000SJeff Roberson cpuset_rel(set); 1533d7f687fcSJeff Roberson out: 15343f289c3fSJeff Roberson cpuset_freelist_free(&cpusets); 15353f289c3fSJeff Roberson domainset_freelist_free(&domainlist); 1536d7f687fcSJeff Roberson return (error); 1537d7f687fcSJeff Roberson } 1538d7f687fcSJeff Roberson 1539d7f687fcSJeff Roberson /* 15403f289c3fSJeff Roberson * Apply an anonymous mask to a single thread. 15413f289c3fSJeff Roberson */ 15423f289c3fSJeff Roberson int 15433f289c3fSJeff Roberson cpuset_setthread(lwpid_t id, cpuset_t *mask) 15443f289c3fSJeff Roberson { 15453f289c3fSJeff Roberson 15463f289c3fSJeff Roberson return _cpuset_setthread(id, mask, NULL); 15473f289c3fSJeff Roberson } 15483f289c3fSJeff Roberson 15493f289c3fSJeff Roberson /* 155081198539SAlexander V. Chernikov * Apply new cpumask to the ithread. 155181198539SAlexander V. Chernikov */ 155281198539SAlexander V. Chernikov int 15537f7528fcSAdrian Chadd cpuset_setithread(lwpid_t id, int cpu) 155481198539SAlexander V. Chernikov { 155581198539SAlexander V. Chernikov cpuset_t mask; 155681198539SAlexander V. Chernikov 155781198539SAlexander V. Chernikov CPU_ZERO(&mask); 155881198539SAlexander V. Chernikov if (cpu == NOCPU) 155981198539SAlexander V. Chernikov CPU_COPY(cpuset_root, &mask); 156081198539SAlexander V. Chernikov else 156181198539SAlexander V. Chernikov CPU_SET(cpu, &mask); 1562e5818a53SJeff Roberson return _cpuset_setthread(id, &mask, NULL); 1563e5818a53SJeff Roberson } 1564c1d9ecf2SAlexander V. Chernikov 156581198539SAlexander V. Chernikov /* 1566662e7fa8SMark Johnston * Initialize static domainsets after NUMA information is available. This is 1567920239efSMark Johnston * called before memory allocators are initialized. 1568662e7fa8SMark Johnston */ 1569662e7fa8SMark Johnston void 1570662e7fa8SMark Johnston domainset_init(void) 1571662e7fa8SMark Johnston { 1572662e7fa8SMark Johnston struct domainset *dset; 1573662e7fa8SMark Johnston int i; 1574662e7fa8SMark Johnston 157529bb6c19SMark Johnston dset = &domainset_firsttouch; 157629bb6c19SMark Johnston DOMAINSET_COPY(&all_domains, &dset->ds_mask); 157729bb6c19SMark Johnston dset->ds_policy = DOMAINSET_POLICY_FIRSTTOUCH; 157829bb6c19SMark Johnston dset->ds_prefer = -1; 157929bb6c19SMark Johnston _domainset_create(dset, NULL); 158029bb6c19SMark Johnston 158129bb6c19SMark Johnston dset = &domainset_interleave; 158229bb6c19SMark Johnston DOMAINSET_COPY(&all_domains, &dset->ds_mask); 158329bb6c19SMark Johnston dset->ds_policy = DOMAINSET_POLICY_INTERLEAVE; 158429bb6c19SMark Johnston dset->ds_prefer = -1; 158529bb6c19SMark Johnston _domainset_create(dset, NULL); 158629bb6c19SMark Johnston 1587662e7fa8SMark Johnston dset = &domainset_roundrobin; 1588662e7fa8SMark Johnston DOMAINSET_COPY(&all_domains, &dset->ds_mask); 1589662e7fa8SMark Johnston dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN; 1590662e7fa8SMark Johnston dset->ds_prefer = -1; 1591662e7fa8SMark Johnston _domainset_create(dset, NULL); 1592662e7fa8SMark Johnston 1593662e7fa8SMark Johnston for (i = 0; i < vm_ndomains; i++) { 15949978bd99SMark Johnston dset = &domainset_fixed[i]; 15959978bd99SMark Johnston DOMAINSET_ZERO(&dset->ds_mask); 15969978bd99SMark Johnston DOMAINSET_SET(i, &dset->ds_mask); 15979978bd99SMark Johnston dset->ds_policy = DOMAINSET_POLICY_ROUNDROBIN; 15989978bd99SMark Johnston _domainset_create(dset, NULL); 15999978bd99SMark Johnston 1600662e7fa8SMark Johnston dset = &domainset_prefer[i]; 1601662e7fa8SMark Johnston DOMAINSET_COPY(&all_domains, &dset->ds_mask); 1602662e7fa8SMark Johnston dset->ds_policy = DOMAINSET_POLICY_PREFER; 1603662e7fa8SMark Johnston dset->ds_prefer = i; 1604662e7fa8SMark Johnston _domainset_create(dset, NULL); 1605662e7fa8SMark Johnston } 1606662e7fa8SMark Johnston } 1607662e7fa8SMark Johnston 1608662e7fa8SMark Johnston /* 160929bb6c19SMark Johnston * Define the domainsets for cpuset 0, 1 and cpuset 2. 161081198539SAlexander V. Chernikov */ 16113f289c3fSJeff Roberson void 16123f289c3fSJeff Roberson domainset_zero(void) 16133f289c3fSJeff Roberson { 1614920239efSMark Johnston struct domainset *dset, *tmp; 16153f289c3fSJeff Roberson 16163f289c3fSJeff Roberson mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 16173f289c3fSJeff Roberson 161829bb6c19SMark Johnston domainset0 = &domainset_firsttouch; 161929bb6c19SMark Johnston curthread->td_domain.dr_policy = domainset0; 1620e5818a53SJeff Roberson 162129bb6c19SMark Johnston domainset2 = &domainset_interleave; 162229bb6c19SMark Johnston kernel_object->domain.dr_policy = domainset2; 1623662e7fa8SMark Johnston 1624662e7fa8SMark Johnston /* Remove empty domains from the global policies. */ 1625920239efSMark Johnston LIST_FOREACH_SAFE(dset, &cpuset_domains, ds_link, tmp) 1626920239efSMark Johnston if (domainset_empty_vm(dset)) 1627920239efSMark Johnston LIST_REMOVE(dset, ds_link); 16283f289c3fSJeff Roberson } 162981198539SAlexander V. Chernikov 163081198539SAlexander V. Chernikov /* 1631e5818a53SJeff Roberson * Creates system-wide cpusets and the cpuset for thread0 including three 1632c0ae6688SJohn Baldwin * sets: 1633d7f687fcSJeff Roberson * 1634d7f687fcSJeff Roberson * 0 - The root set which should represent all valid processors in the 163530b7c6f9SKyle Evans * system. This set is immutable. 1636d7f687fcSJeff Roberson * 1 - The default set which all processes are a member of until changed. 1637d7f687fcSJeff Roberson * This allows an administrator to move all threads off of given cpus to 1638d7f687fcSJeff Roberson * dedicate them to high priority tasks or save power etc. 1639e5818a53SJeff Roberson * 2 - The kernel set which allows restriction and policy to be applied only 1640e5818a53SJeff Roberson * to kernel threads and the kernel_object. 1641d7f687fcSJeff Roberson */ 1642d7f687fcSJeff Roberson struct cpuset * 1643d7f687fcSJeff Roberson cpuset_thread0(void) 1644d7f687fcSJeff Roberson { 1645d7f687fcSJeff Roberson struct cpuset *set; 164627a3c9d7SJeff Roberson int i; 1647a6c7423aSMatt Macy int error __unused; 1648d7f687fcSJeff Roberson 1649d7f687fcSJeff Roberson cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 1650e5818a53SJeff Roberson NULL, NULL, UMA_ALIGN_CACHE, 0); 16513f289c3fSJeff Roberson domainset_zone = uma_zcreate("domainset", sizeof(struct domainset), 1652e5818a53SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0); 165381198539SAlexander V. Chernikov 1654d7f687fcSJeff Roberson /* 1655e5818a53SJeff Roberson * Create the root system set (0) for the whole machine. Doesn't use 1656d7f687fcSJeff Roberson * cpuset_create() due to NULL parent. 1657d7f687fcSJeff Roberson */ 1658d7f687fcSJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 165927a3c9d7SJeff Roberson CPU_COPY(&all_cpus, &set->cs_mask); 1660d7f687fcSJeff Roberson LIST_INIT(&set->cs_children); 1661d7f687fcSJeff Roberson LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 16621a7bb896SMateusz Guzik refcount_init(&set->cs_ref, 1); 166327a3c9d7SJeff Roberson set->cs_flags = CPU_SET_ROOT | CPU_SET_RDONLY; 166429bb6c19SMark Johnston set->cs_domain = domainset0; 1665d7f687fcSJeff Roberson cpuset_zero = set; 1666a03ee000SJeff Roberson cpuset_root = &set->cs_mask; 166781198539SAlexander V. Chernikov 1668d7f687fcSJeff Roberson /* 1669e5818a53SJeff Roberson * Now derive a default (1), modifiable set from that to give out. 1670d7f687fcSJeff Roberson */ 16713f289c3fSJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 167230b7c6f9SKyle Evans error = cpuset_init(set, cpuset_zero, NULL, NULL, 1); 1673d7f687fcSJeff Roberson KASSERT(error == 0, ("Error creating default set: %d\n", error)); 167481198539SAlexander V. Chernikov cpuset_default = set; 1675e5818a53SJeff Roberson /* 1676e5818a53SJeff Roberson * Create the kernel set (2). 1677e5818a53SJeff Roberson */ 1678e5818a53SJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 167930b7c6f9SKyle Evans error = cpuset_init(set, cpuset_zero, NULL, NULL, 2); 1680e5818a53SJeff Roberson KASSERT(error == 0, ("Error creating kernel set: %d\n", error)); 168129bb6c19SMark Johnston set->cs_domain = domainset2; 1682e5818a53SJeff Roberson cpuset_kernel = set; 168381198539SAlexander V. Chernikov 1684d7f687fcSJeff Roberson /* 1685d7f687fcSJeff Roberson * Initialize the unit allocator. 0 and 1 are allocated above. 1686d7f687fcSJeff Roberson */ 1687d57cd5ccSStephen J. Kiernan cpuset_unr = new_unrhdr(3, INT_MAX, NULL); 1688d7f687fcSJeff Roberson 168927a3c9d7SJeff Roberson /* 169027a3c9d7SJeff Roberson * If MD code has not initialized per-domain cpusets, place all 169127a3c9d7SJeff Roberson * CPUs in domain 0. 169227a3c9d7SJeff Roberson */ 169327a3c9d7SJeff Roberson for (i = 0; i < MAXMEMDOM; i++) 169427a3c9d7SJeff Roberson if (!CPU_EMPTY(&cpuset_domain[i])) 169527a3c9d7SJeff Roberson goto domains_set; 169627a3c9d7SJeff Roberson CPU_COPY(&all_cpus, &cpuset_domain[0]); 169727a3c9d7SJeff Roberson domains_set: 169827a3c9d7SJeff Roberson 1699e5818a53SJeff Roberson return (cpuset_default); 1700e5818a53SJeff Roberson } 1701e5818a53SJeff Roberson 1702e5818a53SJeff Roberson void 1703e5818a53SJeff Roberson cpuset_kernthread(struct thread *td) 1704e5818a53SJeff Roberson { 1705e5818a53SJeff Roberson struct cpuset *set; 1706e5818a53SJeff Roberson 1707e5818a53SJeff Roberson thread_lock(td); 1708e5818a53SJeff Roberson set = td->td_cpuset; 1709e5818a53SJeff Roberson td->td_cpuset = cpuset_ref(cpuset_kernel); 1710e5818a53SJeff Roberson thread_unlock(td); 1711e5818a53SJeff Roberson cpuset_rel(set); 1712d7f687fcSJeff Roberson } 1713d7f687fcSJeff Roberson 1714d7f687fcSJeff Roberson /* 1715413628a7SBjoern A. Zeeb * Create a cpuset, which would be cpuset_create() but 1716413628a7SBjoern A. Zeeb * mark the new 'set' as root. 1717413628a7SBjoern A. Zeeb * 171847479a8cSBjoern A. Zeeb * We are not going to reparent the td to it. Use cpuset_setproc_update_set() 171947479a8cSBjoern A. Zeeb * for that. 1720413628a7SBjoern A. Zeeb * 1721413628a7SBjoern A. Zeeb * In case of no error, returns the set in *setp locked with a reference. 1722413628a7SBjoern A. Zeeb */ 1723413628a7SBjoern A. Zeeb int 17240304c731SJamie Gritton cpuset_create_root(struct prison *pr, struct cpuset **setp) 1725413628a7SBjoern A. Zeeb { 1726413628a7SBjoern A. Zeeb struct cpuset *set; 1727413628a7SBjoern A. Zeeb int error; 1728413628a7SBjoern A. Zeeb 17290304c731SJamie Gritton KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__)); 1730413628a7SBjoern A. Zeeb KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 1731413628a7SBjoern A. Zeeb 173229d04ea8SKyle Evans set = NULL; 173329d04ea8SKyle Evans error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask); 1734413628a7SBjoern A. Zeeb if (error) 1735413628a7SBjoern A. Zeeb return (error); 1736413628a7SBjoern A. Zeeb 173729d04ea8SKyle Evans KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data", 1738413628a7SBjoern A. Zeeb __func__, __LINE__)); 1739413628a7SBjoern A. Zeeb 1740413628a7SBjoern A. Zeeb /* Mark the set as root. */ 1741413628a7SBjoern A. Zeeb set->cs_flags |= CPU_SET_ROOT; 174229d04ea8SKyle Evans *setp = set; 1743413628a7SBjoern A. Zeeb 1744413628a7SBjoern A. Zeeb return (0); 1745413628a7SBjoern A. Zeeb } 1746413628a7SBjoern A. Zeeb 1747413628a7SBjoern A. Zeeb int 1748413628a7SBjoern A. Zeeb cpuset_setproc_update_set(struct proc *p, struct cpuset *set) 1749413628a7SBjoern A. Zeeb { 1750413628a7SBjoern A. Zeeb int error; 1751413628a7SBjoern A. Zeeb 1752413628a7SBjoern A. Zeeb KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 1753413628a7SBjoern A. Zeeb KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 1754413628a7SBjoern A. Zeeb 1755413628a7SBjoern A. Zeeb cpuset_ref(set); 1756d431dea5SKyle Evans error = cpuset_setproc(p->p_pid, set, NULL, NULL, true); 1757413628a7SBjoern A. Zeeb if (error) 1758413628a7SBjoern A. Zeeb return (error); 1759413628a7SBjoern A. Zeeb cpuset_rel(set); 1760413628a7SBjoern A. Zeeb return (0); 1761413628a7SBjoern A. Zeeb } 1762413628a7SBjoern A. Zeeb 17639eb997cbSMark Johnston /* 17649eb997cbSMark Johnston * In Capability mode, the only accesses that are permitted are to the current 17659eb997cbSMark Johnston * thread and process' CPU and domain sets. 17669eb997cbSMark Johnston */ 17679eb997cbSMark Johnston static int 17689eb997cbSMark Johnston cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which, 17699eb997cbSMark Johnston id_t id) 17709eb997cbSMark Johnston { 17719eb997cbSMark Johnston if (IN_CAPABILITY_MODE(td)) { 17729eb997cbSMark Johnston if (level != CPU_LEVEL_WHICH) 17739eb997cbSMark Johnston return (ECAPMODE); 1774c21b080fSDmitry Chagin if (which != CPU_WHICH_TID && which != CPU_WHICH_PID && 1775c21b080fSDmitry Chagin which != CPU_WHICH_TIDPID) 1776c21b080fSDmitry Chagin return (ECAPMODE); 1777c21b080fSDmitry Chagin if (id != -1 && which == CPU_WHICH_TIDPID && 1778c21b080fSDmitry Chagin id != td->td_tid && id != td->td_proc->p_pid) 17799eb997cbSMark Johnston return (ECAPMODE); 178069b565d7SMark Johnston if (id != -1 && 178169b565d7SMark Johnston !(which == CPU_WHICH_TID && id == td->td_tid) && 178269b565d7SMark Johnston !(which == CPU_WHICH_PID && id == td->td_proc->p_pid)) 17839eb997cbSMark Johnston return (ECAPMODE); 17849eb997cbSMark Johnston } 17859eb997cbSMark Johnston return (0); 17869eb997cbSMark Johnston } 17879eb997cbSMark Johnston 1788db79bf75SAlfredo Dal'Ava Junior #if defined(__powerpc__) 1789db79bf75SAlfredo Dal'Ava Junior /* 1790db79bf75SAlfredo Dal'Ava Junior * TODO: At least powerpc64 and powerpc64le kernels panic with 1791db79bf75SAlfredo Dal'Ava Junior * exception 0x480 (instruction segment exception) when copyin/copyout, 1792db79bf75SAlfredo Dal'Ava Junior * are set as a function pointer in cpuset_copy_cb struct and called by 1793db79bf75SAlfredo Dal'Ava Junior * an external module (like pfsync). Tip: copyin/copyout have an ifunc 1794db79bf75SAlfredo Dal'Ava Junior * resolver function. 1795db79bf75SAlfredo Dal'Ava Junior * 1796db79bf75SAlfredo Dal'Ava Junior * Bisect of LLVM shows that the behavior changed on LLVM 10.0 with 1797db79bf75SAlfredo Dal'Ava Junior * https://reviews.llvm.org/rGdc06b0bc9ad055d06535462d91bfc2a744b2f589 1798db79bf75SAlfredo Dal'Ava Junior * 1799db79bf75SAlfredo Dal'Ava Junior * This is a hack/workaround while problem is being discussed with LLVM 1800db79bf75SAlfredo Dal'Ava Junior * community 1801db79bf75SAlfredo Dal'Ava Junior */ 1802db79bf75SAlfredo Dal'Ava Junior static int 1803db79bf75SAlfredo Dal'Ava Junior cpuset_copyin(const void *uaddr, void *kaddr, size_t len) 1804db79bf75SAlfredo Dal'Ava Junior { 1805db79bf75SAlfredo Dal'Ava Junior return(copyin(uaddr, kaddr, len)); 1806db79bf75SAlfredo Dal'Ava Junior } 1807db79bf75SAlfredo Dal'Ava Junior 1808db79bf75SAlfredo Dal'Ava Junior static int 1809db79bf75SAlfredo Dal'Ava Junior cpuset_copyout(const void *kaddr, void *uaddr, size_t len) 1810db79bf75SAlfredo Dal'Ava Junior { 1811db79bf75SAlfredo Dal'Ava Junior return(copyout(kaddr, uaddr, len)); 1812db79bf75SAlfredo Dal'Ava Junior } 1813db79bf75SAlfredo Dal'Ava Junior 1814db79bf75SAlfredo Dal'Ava Junior static const struct cpuset_copy_cb copy_set = { 1815db79bf75SAlfredo Dal'Ava Junior .cpuset_copyin = cpuset_copyin, 1816db79bf75SAlfredo Dal'Ava Junior .cpuset_copyout = cpuset_copyout 1817db79bf75SAlfredo Dal'Ava Junior }; 1818db79bf75SAlfredo Dal'Ava Junior #else 181947a57144SJustin Hibbits static const struct cpuset_copy_cb copy_set = { 18204a3e5133SMark Johnston .cpuset_copyin = copyin, 18214a3e5133SMark Johnston .cpuset_copyout = copyout 182247a57144SJustin Hibbits }; 1823db79bf75SAlfredo Dal'Ava Junior #endif 182447a57144SJustin Hibbits 1825d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1826d7f687fcSJeff Roberson struct cpuset_args { 1827d7f687fcSJeff Roberson cpusetid_t *setid; 1828d7f687fcSJeff Roberson }; 1829d7f687fcSJeff Roberson #endif 1830d7f687fcSJeff Roberson int 18318451d0ddSKip Macy sys_cpuset(struct thread *td, struct cpuset_args *uap) 1832d7f687fcSJeff Roberson { 1833d7f687fcSJeff Roberson struct cpuset *root; 1834d7f687fcSJeff Roberson struct cpuset *set; 1835d7f687fcSJeff Roberson int error; 1836d7f687fcSJeff Roberson 1837d7f687fcSJeff Roberson thread_lock(td); 1838a03ee000SJeff Roberson root = cpuset_refroot(td->td_cpuset); 1839d7f687fcSJeff Roberson thread_unlock(td); 184029d04ea8SKyle Evans set = NULL; 1841d7f687fcSJeff Roberson error = cpuset_create(&set, root, &root->cs_mask); 1842d7f687fcSJeff Roberson cpuset_rel(root); 1843d7f687fcSJeff Roberson if (error) 1844d7f687fcSJeff Roberson return (error); 1845d7f687fcSJeff Roberson error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 1846a03ee000SJeff Roberson if (error == 0) 1847d431dea5SKyle Evans error = cpuset_setproc(-1, set, NULL, NULL, false); 1848d7f687fcSJeff Roberson cpuset_rel(set); 1849d7f687fcSJeff Roberson return (error); 1850d7f687fcSJeff Roberson } 1851d7f687fcSJeff Roberson 1852d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1853d7f687fcSJeff Roberson struct cpuset_setid_args { 1854d7f687fcSJeff Roberson cpuwhich_t which; 1855d7f687fcSJeff Roberson id_t id; 1856d7f687fcSJeff Roberson cpusetid_t setid; 1857d7f687fcSJeff Roberson }; 1858d7f687fcSJeff Roberson #endif 1859d7f687fcSJeff Roberson int 18608451d0ddSKip Macy sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 1861d7f687fcSJeff Roberson { 1862ea2ebdc1SEdward Tomasz Napierala 1863ea2ebdc1SEdward Tomasz Napierala return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid)); 1864ea2ebdc1SEdward Tomasz Napierala } 1865ea2ebdc1SEdward Tomasz Napierala 1866ea2ebdc1SEdward Tomasz Napierala int 1867ea2ebdc1SEdward Tomasz Napierala kern_cpuset_setid(struct thread *td, cpuwhich_t which, 1868ea2ebdc1SEdward Tomasz Napierala id_t id, cpusetid_t setid) 1869ea2ebdc1SEdward Tomasz Napierala { 1870d7f687fcSJeff Roberson struct cpuset *set; 1871d7f687fcSJeff Roberson int error; 1872d7f687fcSJeff Roberson 1873d7f687fcSJeff Roberson /* 1874d7f687fcSJeff Roberson * Presently we only support per-process sets. 1875d7f687fcSJeff Roberson */ 1876ea2ebdc1SEdward Tomasz Napierala if (which != CPU_WHICH_PID) 1877d7f687fcSJeff Roberson return (EINVAL); 1878ea2ebdc1SEdward Tomasz Napierala set = cpuset_lookup(setid, td); 1879d7f687fcSJeff Roberson if (set == NULL) 1880d7f687fcSJeff Roberson return (ESRCH); 1881d431dea5SKyle Evans error = cpuset_setproc(id, set, NULL, NULL, false); 1882d7f687fcSJeff Roberson cpuset_rel(set); 1883d7f687fcSJeff Roberson return (error); 1884d7f687fcSJeff Roberson } 1885d7f687fcSJeff Roberson 1886d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1887d7f687fcSJeff Roberson struct cpuset_getid_args { 1888d7f687fcSJeff Roberson cpulevel_t level; 1889d7f687fcSJeff Roberson cpuwhich_t which; 1890d7f687fcSJeff Roberson id_t id; 1891d7f687fcSJeff Roberson cpusetid_t *setid; 18922b69bb1fSKevin Lo }; 1893d7f687fcSJeff Roberson #endif 1894d7f687fcSJeff Roberson int 18958451d0ddSKip Macy sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 1896d7f687fcSJeff Roberson { 1897ea2ebdc1SEdward Tomasz Napierala 1898ea2ebdc1SEdward Tomasz Napierala return (kern_cpuset_getid(td, uap->level, uap->which, uap->id, 1899ea2ebdc1SEdward Tomasz Napierala uap->setid)); 1900ea2ebdc1SEdward Tomasz Napierala } 1901ea2ebdc1SEdward Tomasz Napierala 1902ea2ebdc1SEdward Tomasz Napierala int 1903ea2ebdc1SEdward Tomasz Napierala kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which, 1904ea2ebdc1SEdward Tomasz Napierala id_t id, cpusetid_t *setid) 1905ea2ebdc1SEdward Tomasz Napierala { 1906d7f687fcSJeff Roberson struct cpuset *nset; 1907d7f687fcSJeff Roberson struct cpuset *set; 1908d7f687fcSJeff Roberson struct thread *ttd; 1909d7f687fcSJeff Roberson struct proc *p; 1910ea2ebdc1SEdward Tomasz Napierala cpusetid_t tmpid; 1911d7f687fcSJeff Roberson int error; 1912d7f687fcSJeff Roberson 1913ea2ebdc1SEdward Tomasz Napierala if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET) 1914d7f687fcSJeff Roberson return (EINVAL); 1915ea2ebdc1SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 1916d7f687fcSJeff Roberson if (error) 1917d7f687fcSJeff Roberson return (error); 1918ea2ebdc1SEdward Tomasz Napierala switch (which) { 1919d7f687fcSJeff Roberson case CPU_WHICH_TID: 1920d7f687fcSJeff Roberson case CPU_WHICH_PID: 19212058f075SDmitry Chagin case CPU_WHICH_TIDPID: 1922d7f687fcSJeff Roberson thread_lock(ttd); 1923a03ee000SJeff Roberson set = cpuset_refbase(ttd->td_cpuset); 1924d7f687fcSJeff Roberson thread_unlock(ttd); 1925d7f687fcSJeff Roberson PROC_UNLOCK(p); 1926d7f687fcSJeff Roberson break; 1927d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1928413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1929d7f687fcSJeff Roberson break; 19309b33b154SJeff Roberson case CPU_WHICH_IRQ: 1931c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 19329b33b154SJeff Roberson return (EINVAL); 1933d7f687fcSJeff Roberson } 1934ea2ebdc1SEdward Tomasz Napierala switch (level) { 1935d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 1936a03ee000SJeff Roberson nset = cpuset_refroot(set); 1937d7f687fcSJeff Roberson cpuset_rel(set); 1938d7f687fcSJeff Roberson set = nset; 1939d7f687fcSJeff Roberson break; 1940d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 1941d7f687fcSJeff Roberson break; 1942d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 1943d7f687fcSJeff Roberson break; 1944d7f687fcSJeff Roberson } 1945ea2ebdc1SEdward Tomasz Napierala tmpid = set->cs_id; 1946d7f687fcSJeff Roberson cpuset_rel(set); 1947d7f687fcSJeff Roberson if (error == 0) 1948a1d0659cSJung-uk Kim error = copyout(&tmpid, setid, sizeof(tmpid)); 1949d7f687fcSJeff Roberson 1950d7f687fcSJeff Roberson return (error); 1951d7f687fcSJeff Roberson } 1952d7f687fcSJeff Roberson 1953d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1954d7f687fcSJeff Roberson struct cpuset_getaffinity_args { 1955d7f687fcSJeff Roberson cpulevel_t level; 1956d7f687fcSJeff Roberson cpuwhich_t which; 19577f64829aSRuslan Ermilov id_t id; 19587f64829aSRuslan Ermilov size_t cpusetsize; 19597f64829aSRuslan Ermilov cpuset_t *mask; 1960d7f687fcSJeff Roberson }; 1961d7f687fcSJeff Roberson #endif 1962d7f687fcSJeff Roberson int 19638451d0ddSKip Macy sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 1964d7f687fcSJeff Roberson { 196596ee4310SEdward Tomasz Napierala 1966d46174cdSDmitry Chagin return (user_cpuset_getaffinity(td, uap->level, uap->which, 196747a57144SJustin Hibbits uap->id, uap->cpusetsize, uap->mask, ©_set)); 196896ee4310SEdward Tomasz Napierala } 196996ee4310SEdward Tomasz Napierala 197096ee4310SEdward Tomasz Napierala int 197196ee4310SEdward Tomasz Napierala kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, 1972d46174cdSDmitry Chagin id_t id, size_t cpusetsize, cpuset_t *mask) 197396ee4310SEdward Tomasz Napierala { 1974d7f687fcSJeff Roberson struct thread *ttd; 1975d7f687fcSJeff Roberson struct cpuset *nset; 1976d7f687fcSJeff Roberson struct cpuset *set; 1977d7f687fcSJeff Roberson struct proc *p; 1978d7f687fcSJeff Roberson int error; 1979d7f687fcSJeff Roberson 19809eb997cbSMark Johnston error = cpuset_check_capabilities(td, level, which, id); 19819eb997cbSMark Johnston if (error != 0) 19829eb997cbSMark Johnston return (error); 19832058f075SDmitry Chagin error = cpuset_which2(&which, id, &p, &ttd, &set); 1984d46174cdSDmitry Chagin if (error != 0) 1985d46174cdSDmitry Chagin return (error); 198696ee4310SEdward Tomasz Napierala switch (level) { 1987d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 1988d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 198996ee4310SEdward Tomasz Napierala switch (which) { 1990d7f687fcSJeff Roberson case CPU_WHICH_TID: 1991d7f687fcSJeff Roberson case CPU_WHICH_PID: 1992d7f687fcSJeff Roberson thread_lock(ttd); 1993d7f687fcSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 1994d7f687fcSJeff Roberson thread_unlock(ttd); 1995d7f687fcSJeff Roberson break; 1996d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1997413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1998d7f687fcSJeff Roberson break; 19999b33b154SJeff Roberson case CPU_WHICH_IRQ: 200029dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 200129dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 2002c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 2003d46174cdSDmitry Chagin return (EINVAL); 2004d7f687fcSJeff Roberson } 200596ee4310SEdward Tomasz Napierala if (level == CPU_LEVEL_ROOT) 2006a03ee000SJeff Roberson nset = cpuset_refroot(set); 2007d7f687fcSJeff Roberson else 2008a03ee000SJeff Roberson nset = cpuset_refbase(set); 2009d7f687fcSJeff Roberson CPU_COPY(&nset->cs_mask, mask); 2010d7f687fcSJeff Roberson cpuset_rel(nset); 2011d7f687fcSJeff Roberson break; 2012d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 201396ee4310SEdward Tomasz Napierala switch (which) { 2014d7f687fcSJeff Roberson case CPU_WHICH_TID: 2015d7f687fcSJeff Roberson thread_lock(ttd); 2016d7f687fcSJeff Roberson CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 2017d7f687fcSJeff Roberson thread_unlock(ttd); 2018d7f687fcSJeff Roberson break; 2019d7f687fcSJeff Roberson case CPU_WHICH_PID: 2020d7f687fcSJeff Roberson FOREACH_THREAD_IN_PROC(p, ttd) { 2021d7f687fcSJeff Roberson thread_lock(ttd); 2022e2650af1SStefan Eßer CPU_OR(mask, mask, &ttd->td_cpuset->cs_mask); 2023d7f687fcSJeff Roberson thread_unlock(ttd); 2024d7f687fcSJeff Roberson } 2025d7f687fcSJeff Roberson break; 2026d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 2027413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 2028d7f687fcSJeff Roberson CPU_COPY(&set->cs_mask, mask); 2029d7f687fcSJeff Roberson break; 20309b33b154SJeff Roberson case CPU_WHICH_IRQ: 203129dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 203229dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 203329dfb631SConrad Meyer error = intr_getaffinity(id, which, mask); 20349b33b154SJeff Roberson break; 2035c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 203696ee4310SEdward Tomasz Napierala if (id < 0 || id >= MAXMEMDOM) 2037c0ae6688SJohn Baldwin error = ESRCH; 2038c0ae6688SJohn Baldwin else 203996ee4310SEdward Tomasz Napierala CPU_COPY(&cpuset_domain[id], mask); 2040c0ae6688SJohn Baldwin break; 2041d7f687fcSJeff Roberson } 2042d7f687fcSJeff Roberson break; 2043d7f687fcSJeff Roberson default: 2044d7f687fcSJeff Roberson error = EINVAL; 2045d7f687fcSJeff Roberson break; 2046d7f687fcSJeff Roberson } 2047d7f687fcSJeff Roberson if (set) 2048d7f687fcSJeff Roberson cpuset_rel(set); 2049d7f687fcSJeff Roberson if (p) 2050d7f687fcSJeff Roberson PROC_UNLOCK(p); 2051f35093f8SDmitry Chagin if (error == 0) { 2052d46174cdSDmitry Chagin if (cpusetsize < howmany(CPU_FLS(mask), NBBY)) 2053d46174cdSDmitry Chagin return (ERANGE); 2054d46174cdSDmitry Chagin #ifdef KTRACE 2055d46174cdSDmitry Chagin if (KTRPOINT(td, KTR_STRUCT)) 2056d46174cdSDmitry Chagin ktrcpuset(mask, cpusetsize); 2057d46174cdSDmitry Chagin #endif 2058f35093f8SDmitry Chagin } 2059d46174cdSDmitry Chagin return (error); 2060d46174cdSDmitry Chagin } 2061d46174cdSDmitry Chagin 2062d46174cdSDmitry Chagin int 2063d46174cdSDmitry Chagin user_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, 2064d46174cdSDmitry Chagin id_t id, size_t cpusetsize, cpuset_t *maskp, const struct cpuset_copy_cb *cb) 2065d46174cdSDmitry Chagin { 2066d46174cdSDmitry Chagin cpuset_t *mask; 2067d46174cdSDmitry Chagin size_t size; 2068d46174cdSDmitry Chagin int error; 2069d46174cdSDmitry Chagin 2070d46174cdSDmitry Chagin mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO); 2071f35093f8SDmitry Chagin size = min(cpusetsize, sizeof(cpuset_t)); 2072d46174cdSDmitry Chagin error = kern_cpuset_getaffinity(td, level, which, id, size, mask); 2073d46174cdSDmitry Chagin if (error == 0) { 20744a3e5133SMark Johnston error = cb->cpuset_copyout(mask, maskp, size); 2075f35093f8SDmitry Chagin if (error != 0) 2076f35093f8SDmitry Chagin goto out; 2077f35093f8SDmitry Chagin if (cpusetsize > size) { 2078f35093f8SDmitry Chagin char *end; 2079f35093f8SDmitry Chagin char *cp; 2080f35093f8SDmitry Chagin int rv; 2081f35093f8SDmitry Chagin 2082f35093f8SDmitry Chagin end = cp = (char *)&maskp->__bits; 2083f35093f8SDmitry Chagin end += cpusetsize; 2084f35093f8SDmitry Chagin cp += size; 2085f35093f8SDmitry Chagin while (cp != end) { 2086f35093f8SDmitry Chagin rv = subyte(cp, 0); 2087f35093f8SDmitry Chagin if (rv == -1) { 2088f35093f8SDmitry Chagin error = EFAULT; 2089f35093f8SDmitry Chagin goto out; 2090f35093f8SDmitry Chagin } 2091f35093f8SDmitry Chagin cp++; 2092f35093f8SDmitry Chagin } 2093f35093f8SDmitry Chagin } 2094f35093f8SDmitry Chagin } 2095d7f687fcSJeff Roberson out: 2096d7f687fcSJeff Roberson free(mask, M_TEMP); 2097d7f687fcSJeff Roberson return (error); 2098d7f687fcSJeff Roberson } 2099d7f687fcSJeff Roberson 2100d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 2101d7f687fcSJeff Roberson struct cpuset_setaffinity_args { 2102d7f687fcSJeff Roberson cpulevel_t level; 2103d7f687fcSJeff Roberson cpuwhich_t which; 21047f64829aSRuslan Ermilov id_t id; 21057f64829aSRuslan Ermilov size_t cpusetsize; 21067f64829aSRuslan Ermilov const cpuset_t *mask; 2107d7f687fcSJeff Roberson }; 2108d7f687fcSJeff Roberson #endif 2109d7f687fcSJeff Roberson int 21108451d0ddSKip Macy sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 2111d7f687fcSJeff Roberson { 211296ee4310SEdward Tomasz Napierala 2113f35093f8SDmitry Chagin return (user_cpuset_setaffinity(td, uap->level, uap->which, 211447a57144SJustin Hibbits uap->id, uap->cpusetsize, uap->mask, ©_set)); 211596ee4310SEdward Tomasz Napierala } 211696ee4310SEdward Tomasz Napierala 211796ee4310SEdward Tomasz Napierala int 211896ee4310SEdward Tomasz Napierala kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, 2119f35093f8SDmitry Chagin id_t id, cpuset_t *mask) 212096ee4310SEdward Tomasz Napierala { 2121d7f687fcSJeff Roberson struct cpuset *nset; 2122d7f687fcSJeff Roberson struct cpuset *set; 2123d7f687fcSJeff Roberson struct thread *ttd; 2124d7f687fcSJeff Roberson struct proc *p; 2125d7f687fcSJeff Roberson int error; 2126d7f687fcSJeff Roberson 2127586ed321SDmitry Chagin #ifdef KTRACE 2128586ed321SDmitry Chagin if (KTRPOINT(td, KTR_STRUCT)) 2129586ed321SDmitry Chagin ktrcpuset(mask, sizeof(cpuset_t)); 2130586ed321SDmitry Chagin #endif 21319eb997cbSMark Johnston error = cpuset_check_capabilities(td, level, which, id); 21329eb997cbSMark Johnston if (error != 0) 21339eb997cbSMark Johnston return (error); 2134f35093f8SDmitry Chagin if (CPU_EMPTY(mask)) 2135f35093f8SDmitry Chagin return (EDEADLK); 213696ee4310SEdward Tomasz Napierala switch (level) { 2137d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 2138d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 213996ee4310SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 2140d7f687fcSJeff Roberson if (error) 2141d7f687fcSJeff Roberson break; 214296ee4310SEdward Tomasz Napierala switch (which) { 2143d7f687fcSJeff Roberson case CPU_WHICH_TID: 2144d7f687fcSJeff Roberson case CPU_WHICH_PID: 21452058f075SDmitry Chagin case CPU_WHICH_TIDPID: 2146d7f687fcSJeff Roberson thread_lock(ttd); 2147d7f687fcSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 2148d7f687fcSJeff Roberson thread_unlock(ttd); 2149c6440f72SJeff Roberson PROC_UNLOCK(p); 2150d7f687fcSJeff Roberson break; 2151d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 2152413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 2153d7f687fcSJeff Roberson break; 21549b33b154SJeff Roberson case CPU_WHICH_IRQ: 215529dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 215629dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 2157c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 2158f35093f8SDmitry Chagin return (EINVAL); 2159d7f687fcSJeff Roberson } 216096ee4310SEdward Tomasz Napierala if (level == CPU_LEVEL_ROOT) 2161a03ee000SJeff Roberson nset = cpuset_refroot(set); 2162d7f687fcSJeff Roberson else 2163a03ee000SJeff Roberson nset = cpuset_refbase(set); 2164d7f687fcSJeff Roberson error = cpuset_modify(nset, mask); 2165d7f687fcSJeff Roberson cpuset_rel(nset); 2166d7f687fcSJeff Roberson cpuset_rel(set); 2167d7f687fcSJeff Roberson break; 2168d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 216996ee4310SEdward Tomasz Napierala switch (which) { 2170d7f687fcSJeff Roberson case CPU_WHICH_TID: 217196ee4310SEdward Tomasz Napierala error = cpuset_setthread(id, mask); 2172d7f687fcSJeff Roberson break; 2173d7f687fcSJeff Roberson case CPU_WHICH_PID: 2174d431dea5SKyle Evans error = cpuset_setproc(id, NULL, mask, NULL, false); 2175d7f687fcSJeff Roberson break; 2176c21b080fSDmitry Chagin case CPU_WHICH_TIDPID: 2177c21b080fSDmitry Chagin if (id > PID_MAX || id == -1) 2178c21b080fSDmitry Chagin error = cpuset_setthread(id, mask); 2179c21b080fSDmitry Chagin else 2180c21b080fSDmitry Chagin error = cpuset_setproc(id, NULL, mask, NULL, 2181c21b080fSDmitry Chagin false); 2182c21b080fSDmitry Chagin break; 2183d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 2184413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 218596ee4310SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 2186d7f687fcSJeff Roberson if (error == 0) { 2187d7f687fcSJeff Roberson error = cpuset_modify(set, mask); 2188d7f687fcSJeff Roberson cpuset_rel(set); 2189d7f687fcSJeff Roberson } 2190d7f687fcSJeff Roberson break; 21919b33b154SJeff Roberson case CPU_WHICH_IRQ: 219229dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 219329dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 219429dfb631SConrad Meyer error = intr_setaffinity(id, which, mask); 21959b33b154SJeff Roberson break; 2196d7f687fcSJeff Roberson default: 2197d7f687fcSJeff Roberson error = EINVAL; 2198d7f687fcSJeff Roberson break; 2199d7f687fcSJeff Roberson } 2200d7f687fcSJeff Roberson break; 2201d7f687fcSJeff Roberson default: 2202d7f687fcSJeff Roberson error = EINVAL; 2203d7f687fcSJeff Roberson break; 2204d7f687fcSJeff Roberson } 2205f35093f8SDmitry Chagin return (error); 2206f35093f8SDmitry Chagin } 2207f35093f8SDmitry Chagin 2208f35093f8SDmitry Chagin int 2209f35093f8SDmitry Chagin user_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, 221047a57144SJustin Hibbits id_t id, size_t cpusetsize, const cpuset_t *maskp, const struct cpuset_copy_cb *cb) 2211f35093f8SDmitry Chagin { 2212f35093f8SDmitry Chagin cpuset_t *mask; 2213f35093f8SDmitry Chagin int error; 2214f35093f8SDmitry Chagin size_t size; 2215f35093f8SDmitry Chagin 2216f35093f8SDmitry Chagin size = min(cpusetsize, sizeof(cpuset_t)); 2217f35093f8SDmitry Chagin mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO); 22184a3e5133SMark Johnston error = cb->cpuset_copyin(maskp, mask, size); 2219f35093f8SDmitry Chagin if (error) 2220f35093f8SDmitry Chagin goto out; 2221f35093f8SDmitry Chagin /* 2222f35093f8SDmitry Chagin * Verify that no high bits are set. 2223f35093f8SDmitry Chagin */ 2224f35093f8SDmitry Chagin if (cpusetsize > sizeof(cpuset_t)) { 2225f35093f8SDmitry Chagin const char *end, *cp; 2226f35093f8SDmitry Chagin int val; 2227f35093f8SDmitry Chagin end = cp = (const char *)&maskp->__bits; 2228f35093f8SDmitry Chagin end += cpusetsize; 2229f35093f8SDmitry Chagin cp += sizeof(cpuset_t); 2230f35093f8SDmitry Chagin 2231f35093f8SDmitry Chagin while (cp != end) { 2232f35093f8SDmitry Chagin val = fubyte(cp); 2233f35093f8SDmitry Chagin if (val == -1) { 2234f35093f8SDmitry Chagin error = EFAULT; 2235f35093f8SDmitry Chagin goto out; 2236f35093f8SDmitry Chagin } 2237f35093f8SDmitry Chagin if (val != 0) { 2238f35093f8SDmitry Chagin error = EINVAL; 2239f35093f8SDmitry Chagin goto out; 2240f35093f8SDmitry Chagin } 2241f35093f8SDmitry Chagin cp++; 2242f35093f8SDmitry Chagin } 2243f35093f8SDmitry Chagin } 2244f35093f8SDmitry Chagin error = kern_cpuset_setaffinity(td, level, which, id, mask); 2245f35093f8SDmitry Chagin 2246d7f687fcSJeff Roberson out: 2247d7f687fcSJeff Roberson free(mask, M_TEMP); 2248d7f687fcSJeff Roberson return (error); 2249d7f687fcSJeff Roberson } 2250dea0ed66SBjoern A. Zeeb 22513f289c3fSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 22523f289c3fSJeff Roberson struct cpuset_getdomain_args { 22533f289c3fSJeff Roberson cpulevel_t level; 22543f289c3fSJeff Roberson cpuwhich_t which; 22553f289c3fSJeff Roberson id_t id; 22563f289c3fSJeff Roberson size_t domainsetsize; 22573f289c3fSJeff Roberson domainset_t *mask; 22583f289c3fSJeff Roberson int *policy; 22593f289c3fSJeff Roberson }; 22603f289c3fSJeff Roberson #endif 22613f289c3fSJeff Roberson int 22623f289c3fSJeff Roberson sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap) 2263dea0ed66SBjoern A. Zeeb { 2264dea0ed66SBjoern A. Zeeb 22653f289c3fSJeff Roberson return (kern_cpuset_getdomain(td, uap->level, uap->which, 226647a57144SJustin Hibbits uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set)); 22673f289c3fSJeff Roberson } 22683f289c3fSJeff Roberson 22693f289c3fSJeff Roberson int 22703f289c3fSJeff Roberson kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, 227147a57144SJustin Hibbits id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp, 227247a57144SJustin Hibbits const struct cpuset_copy_cb *cb) 22733f289c3fSJeff Roberson { 22743f289c3fSJeff Roberson struct domainset outset; 22753f289c3fSJeff Roberson struct thread *ttd; 22763f289c3fSJeff Roberson struct cpuset *nset; 22773f289c3fSJeff Roberson struct cpuset *set; 22783f289c3fSJeff Roberson struct domainset *dset; 22793f289c3fSJeff Roberson struct proc *p; 22803f289c3fSJeff Roberson domainset_t *mask; 22813f289c3fSJeff Roberson int error; 22823f289c3fSJeff Roberson 22833f289c3fSJeff Roberson if (domainsetsize < sizeof(domainset_t) || 22843f289c3fSJeff Roberson domainsetsize > DOMAINSET_MAXSIZE / NBBY) 22853f289c3fSJeff Roberson return (ERANGE); 22869eb997cbSMark Johnston error = cpuset_check_capabilities(td, level, which, id); 22879eb997cbSMark Johnston if (error != 0) 22889eb997cbSMark Johnston return (error); 22893f289c3fSJeff Roberson mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO); 22903f289c3fSJeff Roberson bzero(&outset, sizeof(outset)); 22912058f075SDmitry Chagin error = cpuset_which2(&which, id, &p, &ttd, &set); 22923f289c3fSJeff Roberson if (error) 22933f289c3fSJeff Roberson goto out; 22943f289c3fSJeff Roberson switch (level) { 22953f289c3fSJeff Roberson case CPU_LEVEL_ROOT: 22963f289c3fSJeff Roberson case CPU_LEVEL_CPUSET: 22973f289c3fSJeff Roberson switch (which) { 22983f289c3fSJeff Roberson case CPU_WHICH_TID: 22993f289c3fSJeff Roberson case CPU_WHICH_PID: 23003f289c3fSJeff Roberson thread_lock(ttd); 23013f289c3fSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 23023f289c3fSJeff Roberson thread_unlock(ttd); 23033f289c3fSJeff Roberson break; 23043f289c3fSJeff Roberson case CPU_WHICH_CPUSET: 23053f289c3fSJeff Roberson case CPU_WHICH_JAIL: 23063f289c3fSJeff Roberson break; 23073f289c3fSJeff Roberson case CPU_WHICH_IRQ: 23083f289c3fSJeff Roberson case CPU_WHICH_INTRHANDLER: 23093f289c3fSJeff Roberson case CPU_WHICH_ITHREAD: 23103f289c3fSJeff Roberson case CPU_WHICH_DOMAIN: 23113f289c3fSJeff Roberson error = EINVAL; 23123f289c3fSJeff Roberson goto out; 23133f289c3fSJeff Roberson } 23143f289c3fSJeff Roberson if (level == CPU_LEVEL_ROOT) 23153f289c3fSJeff Roberson nset = cpuset_refroot(set); 23163f289c3fSJeff Roberson else 23173f289c3fSJeff Roberson nset = cpuset_refbase(set); 23183f289c3fSJeff Roberson domainset_copy(nset->cs_domain, &outset); 23193f289c3fSJeff Roberson cpuset_rel(nset); 23203f289c3fSJeff Roberson break; 23213f289c3fSJeff Roberson case CPU_LEVEL_WHICH: 23223f289c3fSJeff Roberson switch (which) { 23233f289c3fSJeff Roberson case CPU_WHICH_TID: 23243f289c3fSJeff Roberson thread_lock(ttd); 23253f289c3fSJeff Roberson domainset_copy(ttd->td_cpuset->cs_domain, &outset); 23263f289c3fSJeff Roberson thread_unlock(ttd); 23273f289c3fSJeff Roberson break; 23283f289c3fSJeff Roberson case CPU_WHICH_PID: 23293f289c3fSJeff Roberson FOREACH_THREAD_IN_PROC(p, ttd) { 23303f289c3fSJeff Roberson thread_lock(ttd); 23313f289c3fSJeff Roberson dset = ttd->td_cpuset->cs_domain; 23323f289c3fSJeff Roberson /* Show all domains in the proc. */ 23333f289c3fSJeff Roberson DOMAINSET_OR(&outset.ds_mask, &dset->ds_mask); 23343f289c3fSJeff Roberson /* Last policy wins. */ 23353f289c3fSJeff Roberson outset.ds_policy = dset->ds_policy; 23363f289c3fSJeff Roberson outset.ds_prefer = dset->ds_prefer; 23373f289c3fSJeff Roberson thread_unlock(ttd); 23383f289c3fSJeff Roberson } 23393f289c3fSJeff Roberson break; 23403f289c3fSJeff Roberson case CPU_WHICH_CPUSET: 23413f289c3fSJeff Roberson case CPU_WHICH_JAIL: 23423f289c3fSJeff Roberson domainset_copy(set->cs_domain, &outset); 23433f289c3fSJeff Roberson break; 23443f289c3fSJeff Roberson case CPU_WHICH_IRQ: 23453f289c3fSJeff Roberson case CPU_WHICH_INTRHANDLER: 23463f289c3fSJeff Roberson case CPU_WHICH_ITHREAD: 23473f289c3fSJeff Roberson case CPU_WHICH_DOMAIN: 23483f289c3fSJeff Roberson error = EINVAL; 23493f289c3fSJeff Roberson break; 23503f289c3fSJeff Roberson } 23513f289c3fSJeff Roberson break; 23523f289c3fSJeff Roberson default: 23533f289c3fSJeff Roberson error = EINVAL; 23543f289c3fSJeff Roberson break; 23553f289c3fSJeff Roberson } 23563f289c3fSJeff Roberson if (set) 23573f289c3fSJeff Roberson cpuset_rel(set); 23583f289c3fSJeff Roberson if (p) 23593f289c3fSJeff Roberson PROC_UNLOCK(p); 23603f289c3fSJeff Roberson /* 23613f289c3fSJeff Roberson * Translate prefer into a set containing only the preferred domain, 23623f289c3fSJeff Roberson * not the entire fallback set. 23633f289c3fSJeff Roberson */ 23643f289c3fSJeff Roberson if (outset.ds_policy == DOMAINSET_POLICY_PREFER) { 23653f289c3fSJeff Roberson DOMAINSET_ZERO(&outset.ds_mask); 23663f289c3fSJeff Roberson DOMAINSET_SET(outset.ds_prefer, &outset.ds_mask); 23673f289c3fSJeff Roberson } 23683f289c3fSJeff Roberson DOMAINSET_COPY(&outset.ds_mask, mask); 23693f289c3fSJeff Roberson if (error == 0) 23704a3e5133SMark Johnston error = cb->cpuset_copyout(mask, maskp, domainsetsize); 23713f289c3fSJeff Roberson if (error == 0) 2372dd51fec3SBrooks Davis if (suword32(policyp, outset.ds_policy) != 0) 2373dd51fec3SBrooks Davis error = EFAULT; 23743f289c3fSJeff Roberson out: 23753f289c3fSJeff Roberson free(mask, M_TEMP); 23763f289c3fSJeff Roberson return (error); 23773f289c3fSJeff Roberson } 23783f289c3fSJeff Roberson 23793f289c3fSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 23803f289c3fSJeff Roberson struct cpuset_setdomain_args { 23813f289c3fSJeff Roberson cpulevel_t level; 23823f289c3fSJeff Roberson cpuwhich_t which; 23833f289c3fSJeff Roberson id_t id; 23843f289c3fSJeff Roberson size_t domainsetsize; 23853f289c3fSJeff Roberson domainset_t *mask; 23863f289c3fSJeff Roberson int policy; 23873f289c3fSJeff Roberson }; 23883f289c3fSJeff Roberson #endif 23893f289c3fSJeff Roberson int 23903f289c3fSJeff Roberson sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap) 23913f289c3fSJeff Roberson { 23923f289c3fSJeff Roberson 23933f289c3fSJeff Roberson return (kern_cpuset_setdomain(td, uap->level, uap->which, 239447a57144SJustin Hibbits uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set)); 23953f289c3fSJeff Roberson } 23963f289c3fSJeff Roberson 23973f289c3fSJeff Roberson int 23983f289c3fSJeff Roberson kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, 239947a57144SJustin Hibbits id_t id, size_t domainsetsize, const domainset_t *maskp, int policy, 240047a57144SJustin Hibbits const struct cpuset_copy_cb *cb) 24013f289c3fSJeff Roberson { 24023f289c3fSJeff Roberson struct cpuset *nset; 24033f289c3fSJeff Roberson struct cpuset *set; 24043f289c3fSJeff Roberson struct thread *ttd; 24053f289c3fSJeff Roberson struct proc *p; 24063f289c3fSJeff Roberson struct domainset domain; 24073f289c3fSJeff Roberson domainset_t *mask; 24083f289c3fSJeff Roberson int error; 24093f289c3fSJeff Roberson 24103f289c3fSJeff Roberson if (domainsetsize < sizeof(domainset_t) || 24113f289c3fSJeff Roberson domainsetsize > DOMAINSET_MAXSIZE / NBBY) 24123f289c3fSJeff Roberson return (ERANGE); 241370d66bcfSEric van Gyzen if (policy <= DOMAINSET_POLICY_INVALID || 241470d66bcfSEric van Gyzen policy > DOMAINSET_POLICY_MAX) 241570d66bcfSEric van Gyzen return (EINVAL); 24169eb997cbSMark Johnston error = cpuset_check_capabilities(td, level, which, id); 24179eb997cbSMark Johnston if (error != 0) 24189eb997cbSMark Johnston return (error); 24193f289c3fSJeff Roberson memset(&domain, 0, sizeof(domain)); 24203f289c3fSJeff Roberson mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO); 24214a3e5133SMark Johnston error = cb->cpuset_copyin(maskp, mask, domainsetsize); 24223f289c3fSJeff Roberson if (error) 24233f289c3fSJeff Roberson goto out; 24243f289c3fSJeff Roberson /* 24253f289c3fSJeff Roberson * Verify that no high bits are set. 24263f289c3fSJeff Roberson */ 24273f289c3fSJeff Roberson if (domainsetsize > sizeof(domainset_t)) { 24283f289c3fSJeff Roberson char *end; 24293f289c3fSJeff Roberson char *cp; 24303f289c3fSJeff Roberson 24313f289c3fSJeff Roberson end = cp = (char *)&mask->__bits; 24323f289c3fSJeff Roberson end += domainsetsize; 24333f289c3fSJeff Roberson cp += sizeof(domainset_t); 24343f289c3fSJeff Roberson while (cp != end) 24353f289c3fSJeff Roberson if (*cp++ != 0) { 24363f289c3fSJeff Roberson error = EINVAL; 24373f289c3fSJeff Roberson goto out; 24383f289c3fSJeff Roberson } 24393f289c3fSJeff Roberson } 2440f1b18a66SKyle Evans if (DOMAINSET_EMPTY(mask)) { 2441f1b18a66SKyle Evans error = EDEADLK; 2442f1b18a66SKyle Evans goto out; 2443f1b18a66SKyle Evans } 24443f289c3fSJeff Roberson DOMAINSET_COPY(mask, &domain.ds_mask); 24453f289c3fSJeff Roberson domain.ds_policy = policy; 24463f289c3fSJeff Roberson 244763cdd18eSMark Johnston /* 244863cdd18eSMark Johnston * Sanitize the provided mask. 244963cdd18eSMark Johnston */ 245063cdd18eSMark Johnston if (!DOMAINSET_SUBSET(&all_domains, &domain.ds_mask)) { 245163cdd18eSMark Johnston error = EINVAL; 245263cdd18eSMark Johnston goto out; 245363cdd18eSMark Johnston } 245463cdd18eSMark Johnston 24553f289c3fSJeff Roberson /* Translate preferred policy into a mask and fallback. */ 24563f289c3fSJeff Roberson if (policy == DOMAINSET_POLICY_PREFER) { 24573f289c3fSJeff Roberson /* Only support a single preferred domain. */ 245870d66bcfSEric van Gyzen if (DOMAINSET_COUNT(&domain.ds_mask) != 1) { 245970d66bcfSEric van Gyzen error = EINVAL; 246070d66bcfSEric van Gyzen goto out; 246170d66bcfSEric van Gyzen } 24623f289c3fSJeff Roberson domain.ds_prefer = DOMAINSET_FFS(&domain.ds_mask) - 1; 24633f289c3fSJeff Roberson /* This will be constrained by domainset_shadow(). */ 246463cdd18eSMark Johnston DOMAINSET_COPY(&all_domains, &domain.ds_mask); 24653f289c3fSJeff Roberson } 24663f289c3fSJeff Roberson 246730c5525bSAndrew Gallatin /* 246830c5525bSAndrew Gallatin * When given an impossible policy, fall back to interleaving 246963cdd18eSMark Johnston * across all domains. 247030c5525bSAndrew Gallatin */ 247130c5525bSAndrew Gallatin if (domainset_empty_vm(&domain)) 247229bb6c19SMark Johnston domainset_copy(domainset2, &domain); 247330c5525bSAndrew Gallatin 24743f289c3fSJeff Roberson switch (level) { 24753f289c3fSJeff Roberson case CPU_LEVEL_ROOT: 24763f289c3fSJeff Roberson case CPU_LEVEL_CPUSET: 24773f289c3fSJeff Roberson error = cpuset_which(which, id, &p, &ttd, &set); 24783f289c3fSJeff Roberson if (error) 24793f289c3fSJeff Roberson break; 24803f289c3fSJeff Roberson switch (which) { 24813f289c3fSJeff Roberson case CPU_WHICH_TID: 24823f289c3fSJeff Roberson case CPU_WHICH_PID: 24832058f075SDmitry Chagin case CPU_WHICH_TIDPID: 24843f289c3fSJeff Roberson thread_lock(ttd); 24853f289c3fSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 24863f289c3fSJeff Roberson thread_unlock(ttd); 24873f289c3fSJeff Roberson PROC_UNLOCK(p); 24883f289c3fSJeff Roberson break; 24893f289c3fSJeff Roberson case CPU_WHICH_CPUSET: 24903f289c3fSJeff Roberson case CPU_WHICH_JAIL: 24913f289c3fSJeff Roberson break; 24923f289c3fSJeff Roberson case CPU_WHICH_IRQ: 24933f289c3fSJeff Roberson case CPU_WHICH_INTRHANDLER: 24943f289c3fSJeff Roberson case CPU_WHICH_ITHREAD: 24953f289c3fSJeff Roberson case CPU_WHICH_DOMAIN: 24963f289c3fSJeff Roberson error = EINVAL; 24973f289c3fSJeff Roberson goto out; 24983f289c3fSJeff Roberson } 24993f289c3fSJeff Roberson if (level == CPU_LEVEL_ROOT) 25003f289c3fSJeff Roberson nset = cpuset_refroot(set); 25013f289c3fSJeff Roberson else 25023f289c3fSJeff Roberson nset = cpuset_refbase(set); 25033f289c3fSJeff Roberson error = cpuset_modify_domain(nset, &domain); 25043f289c3fSJeff Roberson cpuset_rel(nset); 25053f289c3fSJeff Roberson cpuset_rel(set); 25063f289c3fSJeff Roberson break; 25073f289c3fSJeff Roberson case CPU_LEVEL_WHICH: 25083f289c3fSJeff Roberson switch (which) { 25093f289c3fSJeff Roberson case CPU_WHICH_TID: 25103f289c3fSJeff Roberson error = _cpuset_setthread(id, NULL, &domain); 25113f289c3fSJeff Roberson break; 25123f289c3fSJeff Roberson case CPU_WHICH_PID: 2513d431dea5SKyle Evans error = cpuset_setproc(id, NULL, NULL, &domain, false); 25143f289c3fSJeff Roberson break; 25152058f075SDmitry Chagin case CPU_WHICH_TIDPID: 25162058f075SDmitry Chagin if (id > PID_MAX || id == -1) 25172058f075SDmitry Chagin error = _cpuset_setthread(id, NULL, &domain); 25182058f075SDmitry Chagin else 25192058f075SDmitry Chagin error = cpuset_setproc(id, NULL, NULL, &domain, 25202058f075SDmitry Chagin false); 25212058f075SDmitry Chagin break; 25223f289c3fSJeff Roberson case CPU_WHICH_CPUSET: 25233f289c3fSJeff Roberson case CPU_WHICH_JAIL: 25243f289c3fSJeff Roberson error = cpuset_which(which, id, &p, &ttd, &set); 25253f289c3fSJeff Roberson if (error == 0) { 25263f289c3fSJeff Roberson error = cpuset_modify_domain(set, &domain); 25273f289c3fSJeff Roberson cpuset_rel(set); 25283f289c3fSJeff Roberson } 25293f289c3fSJeff Roberson break; 25303f289c3fSJeff Roberson case CPU_WHICH_IRQ: 25313f289c3fSJeff Roberson case CPU_WHICH_INTRHANDLER: 25323f289c3fSJeff Roberson case CPU_WHICH_ITHREAD: 25333f289c3fSJeff Roberson default: 25343f289c3fSJeff Roberson error = EINVAL; 25353f289c3fSJeff Roberson break; 25363f289c3fSJeff Roberson } 25373f289c3fSJeff Roberson break; 25383f289c3fSJeff Roberson default: 25393f289c3fSJeff Roberson error = EINVAL; 25403f289c3fSJeff Roberson break; 25413f289c3fSJeff Roberson } 25423f289c3fSJeff Roberson out: 25433f289c3fSJeff Roberson free(mask, M_TEMP); 25443f289c3fSJeff Roberson return (error); 25453f289c3fSJeff Roberson } 25463f289c3fSJeff Roberson 25473f289c3fSJeff Roberson #ifdef DDB 2548e5818a53SJeff Roberson 25493f289c3fSJeff Roberson static void 25503f289c3fSJeff Roberson ddb_display_bitset(const struct bitset *set, int size) 25513f289c3fSJeff Roberson { 25523f289c3fSJeff Roberson int bit, once; 25533f289c3fSJeff Roberson 25543f289c3fSJeff Roberson for (once = 0, bit = 0; bit < size; bit++) { 25553f289c3fSJeff Roberson if (CPU_ISSET(bit, set)) { 2556dea0ed66SBjoern A. Zeeb if (once == 0) { 25573f289c3fSJeff Roberson db_printf("%d", bit); 2558dea0ed66SBjoern A. Zeeb once = 1; 2559dea0ed66SBjoern A. Zeeb } else 25603f289c3fSJeff Roberson db_printf(",%d", bit); 2561dea0ed66SBjoern A. Zeeb } 2562dea0ed66SBjoern A. Zeeb } 2563cd32bd7aSJohn Baldwin if (once == 0) 2564cd32bd7aSJohn Baldwin db_printf("<none>"); 2565cd32bd7aSJohn Baldwin } 2566cd32bd7aSJohn Baldwin 25673f289c3fSJeff Roberson void 25683f289c3fSJeff Roberson ddb_display_cpuset(const cpuset_t *set) 25693f289c3fSJeff Roberson { 25703f289c3fSJeff Roberson ddb_display_bitset((const struct bitset *)set, CPU_SETSIZE); 25713f289c3fSJeff Roberson } 25723f289c3fSJeff Roberson 25733f289c3fSJeff Roberson static void 25743f289c3fSJeff Roberson ddb_display_domainset(const domainset_t *set) 25753f289c3fSJeff Roberson { 25763f289c3fSJeff Roberson ddb_display_bitset((const struct bitset *)set, DOMAINSET_SETSIZE); 25773f289c3fSJeff Roberson } 25783f289c3fSJeff Roberson 2579c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(cpusets, db_show_cpusets, DB_CMD_MEMSAFE) 2580cd32bd7aSJohn Baldwin { 2581cd32bd7aSJohn Baldwin struct cpuset *set; 2582cd32bd7aSJohn Baldwin 2583cd32bd7aSJohn Baldwin LIST_FOREACH(set, &cpuset_ids, cs_link) { 2584cd32bd7aSJohn Baldwin db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 25851a7bb896SMateusz Guzik set, set->cs_id, refcount_load(&set->cs_ref), set->cs_flags, 2586cd32bd7aSJohn Baldwin (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 25873f289c3fSJeff Roberson db_printf(" cpu mask="); 2588cd32bd7aSJohn Baldwin ddb_display_cpuset(&set->cs_mask); 2589dea0ed66SBjoern A. Zeeb db_printf("\n"); 25903f289c3fSJeff Roberson db_printf(" domain policy %d prefer %d mask=", 25913f289c3fSJeff Roberson set->cs_domain->ds_policy, set->cs_domain->ds_prefer); 25923f289c3fSJeff Roberson ddb_display_domainset(&set->cs_domain->ds_mask); 25933f289c3fSJeff Roberson db_printf("\n"); 2594dea0ed66SBjoern A. Zeeb if (db_pager_quit) 2595dea0ed66SBjoern A. Zeeb break; 2596dea0ed66SBjoern A. Zeeb } 2597dea0ed66SBjoern A. Zeeb } 25983f289c3fSJeff Roberson 2599c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(domainsets, db_show_domainsets, DB_CMD_MEMSAFE) 26003f289c3fSJeff Roberson { 26013f289c3fSJeff Roberson struct domainset *set; 26023f289c3fSJeff Roberson 26033f289c3fSJeff Roberson LIST_FOREACH(set, &cpuset_domains, ds_link) { 2604e5818a53SJeff Roberson db_printf("set=%p policy %d prefer %d cnt %d\n", 2605e5818a53SJeff Roberson set, set->ds_policy, set->ds_prefer, set->ds_cnt); 26063f289c3fSJeff Roberson db_printf(" mask ="); 26073f289c3fSJeff Roberson ddb_display_domainset(&set->ds_mask); 26083f289c3fSJeff Roberson db_printf("\n"); 26093f289c3fSJeff Roberson } 26103f289c3fSJeff Roberson } 2611dea0ed66SBjoern A. Zeeb #endif /* DDB */ 2612