1d7f687fcSJeff Roberson /*- 2d7f687fcSJeff Roberson * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org> 3d7f687fcSJeff Roberson * All rights reserved. 4d7f687fcSJeff Roberson * 53bc8c68dSJeff Roberson * Copyright (c) 2008 Nokia Corporation 63bc8c68dSJeff Roberson * All rights reserved. 73bc8c68dSJeff Roberson * 8d7f687fcSJeff Roberson * Redistribution and use in source and binary forms, with or without 9d7f687fcSJeff Roberson * modification, are permitted provided that the following conditions 10d7f687fcSJeff Roberson * are met: 11d7f687fcSJeff Roberson * 1. Redistributions of source code must retain the above copyright 12d7f687fcSJeff Roberson * notice unmodified, this list of conditions, and the following 13d7f687fcSJeff Roberson * disclaimer. 14d7f687fcSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 15d7f687fcSJeff Roberson * notice, this list of conditions and the following disclaimer in the 16d7f687fcSJeff Roberson * documentation and/or other materials provided with the distribution. 17d7f687fcSJeff Roberson * 18d7f687fcSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19d7f687fcSJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20d7f687fcSJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21d7f687fcSJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22d7f687fcSJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23d7f687fcSJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24d7f687fcSJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25d7f687fcSJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26d7f687fcSJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27d7f687fcSJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28d7f687fcSJeff Roberson * 29d7f687fcSJeff Roberson */ 30d7f687fcSJeff Roberson 31d7f687fcSJeff Roberson #include <sys/cdefs.h> 32d7f687fcSJeff Roberson __FBSDID("$FreeBSD$"); 33d7f687fcSJeff Roberson 34dea0ed66SBjoern A. Zeeb #include "opt_ddb.h" 35dea0ed66SBjoern A. Zeeb 36d7f687fcSJeff Roberson #include <sys/param.h> 37d7f687fcSJeff Roberson #include <sys/systm.h> 38d7f687fcSJeff Roberson #include <sys/sysproto.h> 390304c731SJamie Gritton #include <sys/jail.h> 40d7f687fcSJeff Roberson #include <sys/kernel.h> 41d7f687fcSJeff Roberson #include <sys/lock.h> 42d7f687fcSJeff Roberson #include <sys/malloc.h> 43d7f687fcSJeff Roberson #include <sys/mutex.h> 44d7f687fcSJeff Roberson #include <sys/priv.h> 45d7f687fcSJeff Roberson #include <sys/proc.h> 46d7f687fcSJeff Roberson #include <sys/refcount.h> 47d7f687fcSJeff Roberson #include <sys/sched.h> 48d7f687fcSJeff Roberson #include <sys/smp.h> 49d7f687fcSJeff Roberson #include <sys/syscallsubr.h> 50d7f687fcSJeff Roberson #include <sys/cpuset.h> 51d7f687fcSJeff Roberson #include <sys/sx.h> 52d7f687fcSJeff Roberson #include <sys/queue.h> 53d7f687fcSJeff Roberson #include <sys/limits.h> 54a03ee000SJeff Roberson #include <sys/bus.h> 55a03ee000SJeff Roberson #include <sys/interrupt.h> 56d7f687fcSJeff Roberson 57d7f687fcSJeff Roberson #include <vm/uma.h> 58d7f687fcSJeff Roberson 59dea0ed66SBjoern A. Zeeb #ifdef DDB 60dea0ed66SBjoern A. Zeeb #include <ddb/ddb.h> 61dea0ed66SBjoern A. Zeeb #endif /* DDB */ 62dea0ed66SBjoern A. Zeeb 63d7f687fcSJeff Roberson /* 64d7f687fcSJeff Roberson * cpusets provide a mechanism for creating and manipulating sets of 65d7f687fcSJeff Roberson * processors for the purpose of constraining the scheduling of threads to 66d7f687fcSJeff Roberson * specific processors. 67d7f687fcSJeff Roberson * 68d7f687fcSJeff Roberson * Each process belongs to an identified set, by default this is set 1. Each 69d7f687fcSJeff Roberson * thread may further restrict the cpus it may run on to a subset of this 70d7f687fcSJeff Roberson * named set. This creates an anonymous set which other threads and processes 71d7f687fcSJeff Roberson * may not join by number. 72d7f687fcSJeff Roberson * 73d7f687fcSJeff Roberson * The named set is referred to herein as the 'base' set to avoid ambiguity. 74d7f687fcSJeff Roberson * This set is usually a child of a 'root' set while the anonymous set may 75d7f687fcSJeff Roberson * simply be referred to as a mask. In the syscall api these are referred to 76d7f687fcSJeff Roberson * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here. 77d7f687fcSJeff Roberson * 78d7f687fcSJeff Roberson * Threads inherit their set from their creator whether it be anonymous or 79d7f687fcSJeff Roberson * not. This means that anonymous sets are immutable because they may be 80d7f687fcSJeff Roberson * shared. To modify an anonymous set a new set is created with the desired 81d7f687fcSJeff Roberson * mask and the same parent as the existing anonymous set. This gives the 8293902625SJohn Baldwin * illusion of each thread having a private mask. 83d7f687fcSJeff Roberson * 84d7f687fcSJeff Roberson * Via the syscall apis a user may ask to retrieve or modify the root, base, 85d7f687fcSJeff Roberson * or mask that is discovered via a pid, tid, or setid. Modifying a set 86d7f687fcSJeff Roberson * modifies all numbered and anonymous child sets to comply with the new mask. 87d7f687fcSJeff Roberson * Modifying a pid or tid's mask applies only to that tid but must still 88d7f687fcSJeff Roberson * exist within the assigned parent set. 89d7f687fcSJeff Roberson * 9086855bf5SJohn Baldwin * A thread may not be assigned to a group separate from other threads in 91d7f687fcSJeff Roberson * the process. This is to remove ambiguity when the setid is queried with 92d7f687fcSJeff Roberson * a pid argument. There is no other technical limitation. 93d7f687fcSJeff Roberson * 94d7f687fcSJeff Roberson * This somewhat complex arrangement is intended to make it easy for 95d7f687fcSJeff Roberson * applications to query available processors and bind their threads to 96d7f687fcSJeff Roberson * specific processors while also allowing administrators to dynamically 97d7f687fcSJeff Roberson * reprovision by changing sets which apply to groups of processes. 98d7f687fcSJeff Roberson * 99d7f687fcSJeff Roberson * A simple application should not concern itself with sets at all and 100d7f687fcSJeff Roberson * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id 10193902625SJohn Baldwin * meaning 'curthread'. It may query available cpus for that tid with a 102d7f687fcSJeff Roberson * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...). 103d7f687fcSJeff Roberson */ 104d7f687fcSJeff Roberson static uma_zone_t cpuset_zone; 105d7f687fcSJeff Roberson static struct mtx cpuset_lock; 106d7f687fcSJeff Roberson static struct setlist cpuset_ids; 107d7f687fcSJeff Roberson static struct unrhdr *cpuset_unr; 108a03ee000SJeff Roberson static struct cpuset *cpuset_zero; 109a03ee000SJeff Roberson 110444528c0SDavid Xu /* Return the size of cpuset_t at the kernel level */ 111444528c0SDavid Xu SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD, 112444528c0SDavid Xu 0, sizeof(cpuset_t), "sizeof(cpuset_t)"); 113444528c0SDavid Xu 114a03ee000SJeff Roberson cpuset_t *cpuset_root; 115d7f687fcSJeff Roberson 116d7f687fcSJeff Roberson /* 117d7f687fcSJeff Roberson * Acquire a reference to a cpuset, all pointers must be tracked with refs. 118d7f687fcSJeff Roberson */ 119d7f687fcSJeff Roberson struct cpuset * 120d7f687fcSJeff Roberson cpuset_ref(struct cpuset *set) 121d7f687fcSJeff Roberson { 122d7f687fcSJeff Roberson 123d7f687fcSJeff Roberson refcount_acquire(&set->cs_ref); 124d7f687fcSJeff Roberson return (set); 125d7f687fcSJeff Roberson } 126d7f687fcSJeff Roberson 127d7f687fcSJeff Roberson /* 1287a8f695aSBjoern A. Zeeb * Walks up the tree from 'set' to find the root. Returns the root 1297a8f695aSBjoern A. Zeeb * referenced. 1307a8f695aSBjoern A. Zeeb */ 1317a8f695aSBjoern A. Zeeb static struct cpuset * 1327a8f695aSBjoern A. Zeeb cpuset_refroot(struct cpuset *set) 1337a8f695aSBjoern A. Zeeb { 1347a8f695aSBjoern A. Zeeb 1357a8f695aSBjoern A. Zeeb for (; set->cs_parent != NULL; set = set->cs_parent) 1367a8f695aSBjoern A. Zeeb if (set->cs_flags & CPU_SET_ROOT) 1377a8f695aSBjoern A. Zeeb break; 1387a8f695aSBjoern A. Zeeb cpuset_ref(set); 1397a8f695aSBjoern A. Zeeb 1407a8f695aSBjoern A. Zeeb return (set); 1417a8f695aSBjoern A. Zeeb } 1427a8f695aSBjoern A. Zeeb 1437a8f695aSBjoern A. Zeeb /* 1447a8f695aSBjoern A. Zeeb * Find the first non-anonymous set starting from 'set'. Returns this set 1457a8f695aSBjoern A. Zeeb * referenced. May return the passed in set with an extra ref if it is 1467a8f695aSBjoern A. Zeeb * not anonymous. 1477a8f695aSBjoern A. Zeeb */ 1487a8f695aSBjoern A. Zeeb static struct cpuset * 1497a8f695aSBjoern A. Zeeb cpuset_refbase(struct cpuset *set) 1507a8f695aSBjoern A. Zeeb { 1517a8f695aSBjoern A. Zeeb 1527a8f695aSBjoern A. Zeeb if (set->cs_id == CPUSET_INVALID) 1537a8f695aSBjoern A. Zeeb set = set->cs_parent; 1547a8f695aSBjoern A. Zeeb cpuset_ref(set); 1557a8f695aSBjoern A. Zeeb 1567a8f695aSBjoern A. Zeeb return (set); 1577a8f695aSBjoern A. Zeeb } 1587a8f695aSBjoern A. Zeeb 1597a8f695aSBjoern A. Zeeb /* 16093902625SJohn Baldwin * Release a reference in a context where it is safe to allocate. 161d7f687fcSJeff Roberson */ 162d7f687fcSJeff Roberson void 163d7f687fcSJeff Roberson cpuset_rel(struct cpuset *set) 164d7f687fcSJeff Roberson { 165d7f687fcSJeff Roberson cpusetid_t id; 166d7f687fcSJeff Roberson 167d7f687fcSJeff Roberson if (refcount_release(&set->cs_ref) == 0) 168d7f687fcSJeff Roberson return; 169d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 170d7f687fcSJeff Roberson LIST_REMOVE(set, cs_siblings); 171d7f687fcSJeff Roberson id = set->cs_id; 172d7f687fcSJeff Roberson if (id != CPUSET_INVALID) 173d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 174d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 175d7f687fcSJeff Roberson cpuset_rel(set->cs_parent); 176d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 177d7f687fcSJeff Roberson if (id != CPUSET_INVALID) 178d7f687fcSJeff Roberson free_unr(cpuset_unr, id); 179d7f687fcSJeff Roberson } 180d7f687fcSJeff Roberson 181d7f687fcSJeff Roberson /* 182d7f687fcSJeff Roberson * Deferred release must be used when in a context that is not safe to 183d7f687fcSJeff Roberson * allocate/free. This places any unreferenced sets on the list 'head'. 184d7f687fcSJeff Roberson */ 185d7f687fcSJeff Roberson static void 186d7f687fcSJeff Roberson cpuset_rel_defer(struct setlist *head, struct cpuset *set) 187d7f687fcSJeff Roberson { 188d7f687fcSJeff Roberson 189d7f687fcSJeff Roberson if (refcount_release(&set->cs_ref) == 0) 190d7f687fcSJeff Roberson return; 191d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 192d7f687fcSJeff Roberson LIST_REMOVE(set, cs_siblings); 193d7f687fcSJeff Roberson if (set->cs_id != CPUSET_INVALID) 194d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 195d7f687fcSJeff Roberson LIST_INSERT_HEAD(head, set, cs_link); 196d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 197d7f687fcSJeff Roberson } 198d7f687fcSJeff Roberson 199d7f687fcSJeff Roberson /* 200d7f687fcSJeff Roberson * Complete a deferred release. Removes the set from the list provided to 201d7f687fcSJeff Roberson * cpuset_rel_defer. 202d7f687fcSJeff Roberson */ 203d7f687fcSJeff Roberson static void 204d7f687fcSJeff Roberson cpuset_rel_complete(struct cpuset *set) 205d7f687fcSJeff Roberson { 206d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 207d7f687fcSJeff Roberson cpuset_rel(set->cs_parent); 208d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 209d7f687fcSJeff Roberson } 210d7f687fcSJeff Roberson 211d7f687fcSJeff Roberson /* 212d7f687fcSJeff Roberson * Find a set based on an id. Returns it with a ref. 213d7f687fcSJeff Roberson */ 214d7f687fcSJeff Roberson static struct cpuset * 215413628a7SBjoern A. Zeeb cpuset_lookup(cpusetid_t setid, struct thread *td) 216d7f687fcSJeff Roberson { 217d7f687fcSJeff Roberson struct cpuset *set; 218d7f687fcSJeff Roberson 219d7f687fcSJeff Roberson if (setid == CPUSET_INVALID) 220d7f687fcSJeff Roberson return (NULL); 221d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 222d7f687fcSJeff Roberson LIST_FOREACH(set, &cpuset_ids, cs_link) 223d7f687fcSJeff Roberson if (set->cs_id == setid) 224d7f687fcSJeff Roberson break; 225d7f687fcSJeff Roberson if (set) 226d7f687fcSJeff Roberson cpuset_ref(set); 227d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 228413628a7SBjoern A. Zeeb 229413628a7SBjoern A. Zeeb KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__)); 230413628a7SBjoern A. Zeeb if (set != NULL && jailed(td->td_ucred)) { 2310304c731SJamie Gritton struct cpuset *jset, *tset; 232413628a7SBjoern A. Zeeb 2330304c731SJamie Gritton jset = td->td_ucred->cr_prison->pr_cpuset; 2340304c731SJamie Gritton for (tset = set; tset != NULL; tset = tset->cs_parent) 2350304c731SJamie Gritton if (tset == jset) 2360304c731SJamie Gritton break; 2370304c731SJamie Gritton if (tset == NULL) { 238413628a7SBjoern A. Zeeb cpuset_rel(set); 239413628a7SBjoern A. Zeeb set = NULL; 240413628a7SBjoern A. Zeeb } 241413628a7SBjoern A. Zeeb } 242413628a7SBjoern A. Zeeb 243d7f687fcSJeff Roberson return (set); 244d7f687fcSJeff Roberson } 245d7f687fcSJeff Roberson 246d7f687fcSJeff Roberson /* 247d7f687fcSJeff Roberson * Create a set in the space provided in 'set' with the provided parameters. 248d7f687fcSJeff Roberson * The set is returned with a single ref. May return EDEADLK if the set 249d7f687fcSJeff Roberson * will have no valid cpu based on restrictions from the parent. 250d7f687fcSJeff Roberson */ 251d7f687fcSJeff Roberson static int 252e84c2db1SJohn Baldwin _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask, 253d7f687fcSJeff Roberson cpusetid_t id) 254d7f687fcSJeff Roberson { 255d7f687fcSJeff Roberson 25673c40187SJeff Roberson if (!CPU_OVERLAP(&parent->cs_mask, mask)) 25773c40187SJeff Roberson return (EDEADLK); 258d7f687fcSJeff Roberson CPU_COPY(mask, &set->cs_mask); 259d7f687fcSJeff Roberson LIST_INIT(&set->cs_children); 260d7f687fcSJeff Roberson refcount_init(&set->cs_ref, 1); 261d7f687fcSJeff Roberson set->cs_flags = 0; 262d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 263e84c2db1SJohn Baldwin CPU_AND(&set->cs_mask, &parent->cs_mask); 264d7f687fcSJeff Roberson set->cs_id = id; 265d7f687fcSJeff Roberson set->cs_parent = cpuset_ref(parent); 266d7f687fcSJeff Roberson LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings); 267d7f687fcSJeff Roberson if (set->cs_id != CPUSET_INVALID) 268d7f687fcSJeff Roberson LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 269d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 270d7f687fcSJeff Roberson 27173c40187SJeff Roberson return (0); 272d7f687fcSJeff Roberson } 273d7f687fcSJeff Roberson 274d7f687fcSJeff Roberson /* 275d7f687fcSJeff Roberson * Create a new non-anonymous set with the requested parent and mask. May 276d7f687fcSJeff Roberson * return failures if the mask is invalid or a new number can not be 277d7f687fcSJeff Roberson * allocated. 278d7f687fcSJeff Roberson */ 279d7f687fcSJeff Roberson static int 280e84c2db1SJohn Baldwin cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask) 281d7f687fcSJeff Roberson { 282d7f687fcSJeff Roberson struct cpuset *set; 283d7f687fcSJeff Roberson cpusetid_t id; 284d7f687fcSJeff Roberson int error; 285d7f687fcSJeff Roberson 286d7f687fcSJeff Roberson id = alloc_unr(cpuset_unr); 287d7f687fcSJeff Roberson if (id == -1) 288d7f687fcSJeff Roberson return (ENFILE); 289d7f687fcSJeff Roberson *setp = set = uma_zalloc(cpuset_zone, M_WAITOK); 290d7f687fcSJeff Roberson error = _cpuset_create(set, parent, mask, id); 291d7f687fcSJeff Roberson if (error == 0) 292d7f687fcSJeff Roberson return (0); 293d7f687fcSJeff Roberson free_unr(cpuset_unr, id); 294d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 295d7f687fcSJeff Roberson 296d7f687fcSJeff Roberson return (error); 297d7f687fcSJeff Roberson } 298d7f687fcSJeff Roberson 299d7f687fcSJeff Roberson /* 300d7f687fcSJeff Roberson * Recursively check for errors that would occur from applying mask to 301d7f687fcSJeff Roberson * the tree of sets starting at 'set'. Checks for sets that would become 302d7f687fcSJeff Roberson * empty as well as RDONLY flags. 303d7f687fcSJeff Roberson */ 304d7f687fcSJeff Roberson static int 305d7f687fcSJeff Roberson cpuset_testupdate(struct cpuset *set, cpuset_t *mask) 306d7f687fcSJeff Roberson { 307d7f687fcSJeff Roberson struct cpuset *nset; 308d7f687fcSJeff Roberson cpuset_t newmask; 309d7f687fcSJeff Roberson int error; 310d7f687fcSJeff Roberson 311d7f687fcSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 312d7f687fcSJeff Roberson if (set->cs_flags & CPU_SET_RDONLY) 313d7f687fcSJeff Roberson return (EPERM); 31473c40187SJeff Roberson if (!CPU_OVERLAP(&set->cs_mask, mask)) 31573c40187SJeff Roberson return (EDEADLK); 316d7f687fcSJeff Roberson CPU_COPY(&set->cs_mask, &newmask); 317d7f687fcSJeff Roberson CPU_AND(&newmask, mask); 31873c40187SJeff Roberson error = 0; 319d7f687fcSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 320d7f687fcSJeff Roberson if ((error = cpuset_testupdate(nset, &newmask)) != 0) 321d7f687fcSJeff Roberson break; 322d7f687fcSJeff Roberson return (error); 323d7f687fcSJeff Roberson } 324d7f687fcSJeff Roberson 325d7f687fcSJeff Roberson /* 326d7f687fcSJeff Roberson * Applies the mask 'mask' without checking for empty sets or permissions. 327d7f687fcSJeff Roberson */ 328d7f687fcSJeff Roberson static void 329d7f687fcSJeff Roberson cpuset_update(struct cpuset *set, cpuset_t *mask) 330d7f687fcSJeff Roberson { 331d7f687fcSJeff Roberson struct cpuset *nset; 332d7f687fcSJeff Roberson 333d7f687fcSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 334d7f687fcSJeff Roberson CPU_AND(&set->cs_mask, mask); 335d7f687fcSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 336d7f687fcSJeff Roberson cpuset_update(nset, &set->cs_mask); 337d7f687fcSJeff Roberson 338d7f687fcSJeff Roberson return; 339d7f687fcSJeff Roberson } 340d7f687fcSJeff Roberson 341d7f687fcSJeff Roberson /* 342d7f687fcSJeff Roberson * Modify the set 'set' to use a copy of the mask provided. Apply this new 343d7f687fcSJeff Roberson * mask to restrict all children in the tree. Checks for validity before 344d7f687fcSJeff Roberson * applying the changes. 345d7f687fcSJeff Roberson */ 346d7f687fcSJeff Roberson static int 347d7f687fcSJeff Roberson cpuset_modify(struct cpuset *set, cpuset_t *mask) 348d7f687fcSJeff Roberson { 34973c40187SJeff Roberson struct cpuset *root; 350d7f687fcSJeff Roberson int error; 351d7f687fcSJeff Roberson 352ba931c08SBjoern A. Zeeb error = priv_check(curthread, PRIV_SCHED_CPUSET); 353d7f687fcSJeff Roberson if (error) 354d7f687fcSJeff Roberson return (error); 35573c40187SJeff Roberson /* 3566aaa0b3cSBjoern A. Zeeb * In case we are called from within the jail 3576aaa0b3cSBjoern A. Zeeb * we do not allow modifying the dedicated root 3586aaa0b3cSBjoern A. Zeeb * cpuset of the jail but may still allow to 3596aaa0b3cSBjoern A. Zeeb * change child sets. 3606aaa0b3cSBjoern A. Zeeb */ 3616aaa0b3cSBjoern A. Zeeb if (jailed(curthread->td_ucred) && 3626aaa0b3cSBjoern A. Zeeb set->cs_flags & CPU_SET_ROOT) 3636aaa0b3cSBjoern A. Zeeb return (EPERM); 3646aaa0b3cSBjoern A. Zeeb /* 36573c40187SJeff Roberson * Verify that we have access to this set of 36673c40187SJeff Roberson * cpus. 36773c40187SJeff Roberson */ 36873c40187SJeff Roberson root = set->cs_parent; 36973c40187SJeff Roberson if (root && !CPU_SUBSET(&root->cs_mask, mask)) 37073c40187SJeff Roberson return (EINVAL); 371d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 372d7f687fcSJeff Roberson error = cpuset_testupdate(set, mask); 373d7f687fcSJeff Roberson if (error) 374d7f687fcSJeff Roberson goto out; 375d7f687fcSJeff Roberson cpuset_update(set, mask); 376d7f687fcSJeff Roberson CPU_COPY(mask, &set->cs_mask); 377d7f687fcSJeff Roberson out: 378d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 379d7f687fcSJeff Roberson 380d7f687fcSJeff Roberson return (error); 381d7f687fcSJeff Roberson } 382d7f687fcSJeff Roberson 383d7f687fcSJeff Roberson /* 384d7f687fcSJeff Roberson * Resolve the 'which' parameter of several cpuset apis. 385d7f687fcSJeff Roberson * 386d7f687fcSJeff Roberson * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 387d7f687fcSJeff Roberson * checks for permission via p_cansched(). 388d7f687fcSJeff Roberson * 389d7f687fcSJeff Roberson * For WHICH_SET returns a valid set with a new reference. 390d7f687fcSJeff Roberson * 391d7f687fcSJeff Roberson * -1 may be supplied for any argument to mean the current proc/thread or 392d7f687fcSJeff Roberson * the base set of the current thread. May fail with ESRCH/EPERM. 393d7f687fcSJeff Roberson */ 394d7f687fcSJeff Roberson static int 395d7f687fcSJeff Roberson cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 396d7f687fcSJeff Roberson struct cpuset **setp) 397d7f687fcSJeff Roberson { 398d7f687fcSJeff Roberson struct cpuset *set; 399d7f687fcSJeff Roberson struct thread *td; 400d7f687fcSJeff Roberson struct proc *p; 401d7f687fcSJeff Roberson int error; 402d7f687fcSJeff Roberson 403d7f687fcSJeff Roberson *pp = p = NULL; 404d7f687fcSJeff Roberson *tdp = td = NULL; 405d7f687fcSJeff Roberson *setp = set = NULL; 406d7f687fcSJeff Roberson switch (which) { 407d7f687fcSJeff Roberson case CPU_WHICH_PID: 408d7f687fcSJeff Roberson if (id == -1) { 409d7f687fcSJeff Roberson PROC_LOCK(curproc); 410d7f687fcSJeff Roberson p = curproc; 411d7f687fcSJeff Roberson break; 412d7f687fcSJeff Roberson } 413d7f687fcSJeff Roberson if ((p = pfind(id)) == NULL) 414d7f687fcSJeff Roberson return (ESRCH); 415d7f687fcSJeff Roberson break; 416d7f687fcSJeff Roberson case CPU_WHICH_TID: 417d7f687fcSJeff Roberson if (id == -1) { 418d7f687fcSJeff Roberson PROC_LOCK(curproc); 419d7f687fcSJeff Roberson p = curproc; 420d7f687fcSJeff Roberson td = curthread; 421d7f687fcSJeff Roberson break; 422d7f687fcSJeff Roberson } 42342fe684cSDavid Xu td = tdfind(id, -1); 424d7f687fcSJeff Roberson if (td == NULL) 425d7f687fcSJeff Roberson return (ESRCH); 42642fe684cSDavid Xu p = td->td_proc; 427d7f687fcSJeff Roberson break; 428d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 429d7f687fcSJeff Roberson if (id == -1) { 430d7f687fcSJeff Roberson thread_lock(curthread); 431a03ee000SJeff Roberson set = cpuset_refbase(curthread->td_cpuset); 432d7f687fcSJeff Roberson thread_unlock(curthread); 433d7f687fcSJeff Roberson } else 434413628a7SBjoern A. Zeeb set = cpuset_lookup(id, curthread); 435d7f687fcSJeff Roberson if (set) { 436d7f687fcSJeff Roberson *setp = set; 437d7f687fcSJeff Roberson return (0); 438d7f687fcSJeff Roberson } 439d7f687fcSJeff Roberson return (ESRCH); 440413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 441413628a7SBjoern A. Zeeb { 442413628a7SBjoern A. Zeeb /* Find `set' for prison with given id. */ 443413628a7SBjoern A. Zeeb struct prison *pr; 444413628a7SBjoern A. Zeeb 445413628a7SBjoern A. Zeeb sx_slock(&allprison_lock); 4460304c731SJamie Gritton pr = prison_find_child(curthread->td_ucred->cr_prison, id); 447413628a7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 448413628a7SBjoern A. Zeeb if (pr == NULL) 449413628a7SBjoern A. Zeeb return (ESRCH); 450413628a7SBjoern A. Zeeb cpuset_ref(pr->pr_cpuset); 4510304c731SJamie Gritton *setp = pr->pr_cpuset; 452413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 453413628a7SBjoern A. Zeeb return (0); 454413628a7SBjoern A. Zeeb } 4559b33b154SJeff Roberson case CPU_WHICH_IRQ: 4569b33b154SJeff Roberson return (0); 457d7f687fcSJeff Roberson default: 458d7f687fcSJeff Roberson return (EINVAL); 459d7f687fcSJeff Roberson } 460d7f687fcSJeff Roberson error = p_cansched(curthread, p); 461d7f687fcSJeff Roberson if (error) { 462d7f687fcSJeff Roberson PROC_UNLOCK(p); 463d7f687fcSJeff Roberson return (error); 464d7f687fcSJeff Roberson } 465d7f687fcSJeff Roberson if (td == NULL) 466d7f687fcSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 467d7f687fcSJeff Roberson *pp = p; 468d7f687fcSJeff Roberson *tdp = td; 469d7f687fcSJeff Roberson return (0); 470d7f687fcSJeff Roberson } 471d7f687fcSJeff Roberson 472d7f687fcSJeff Roberson /* 473d7f687fcSJeff Roberson * Create an anonymous set with the provided mask in the space provided by 474d7f687fcSJeff Roberson * 'fset'. If the passed in set is anonymous we use its parent otherwise 475d7f687fcSJeff Roberson * the new set is a child of 'set'. 476d7f687fcSJeff Roberson */ 477d7f687fcSJeff Roberson static int 478e84c2db1SJohn Baldwin cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask) 479d7f687fcSJeff Roberson { 480d7f687fcSJeff Roberson struct cpuset *parent; 481d7f687fcSJeff Roberson 482d7f687fcSJeff Roberson if (set->cs_id == CPUSET_INVALID) 483d7f687fcSJeff Roberson parent = set->cs_parent; 484d7f687fcSJeff Roberson else 485d7f687fcSJeff Roberson parent = set; 48673c40187SJeff Roberson if (!CPU_SUBSET(&parent->cs_mask, mask)) 487a03ee000SJeff Roberson return (EDEADLK); 488d7f687fcSJeff Roberson return (_cpuset_create(fset, parent, mask, CPUSET_INVALID)); 489d7f687fcSJeff Roberson } 490d7f687fcSJeff Roberson 491d7f687fcSJeff Roberson /* 492d7f687fcSJeff Roberson * Handle two cases for replacing the base set or mask of an entire process. 493d7f687fcSJeff Roberson * 494d7f687fcSJeff Roberson * 1) Set is non-null and mask is null. This reparents all anonymous sets 495d7f687fcSJeff Roberson * to the provided set and replaces all non-anonymous td_cpusets with the 496d7f687fcSJeff Roberson * provided set. 497d7f687fcSJeff Roberson * 2) Mask is non-null and set is null. This replaces or creates anonymous 498d7f687fcSJeff Roberson * sets for every thread with the existing base as a parent. 499d7f687fcSJeff Roberson * 500d7f687fcSJeff Roberson * This is overly complicated because we can't allocate while holding a 501d7f687fcSJeff Roberson * spinlock and spinlocks must be held while changing and examining thread 502d7f687fcSJeff Roberson * state. 503d7f687fcSJeff Roberson */ 504d7f687fcSJeff Roberson static int 505d7f687fcSJeff Roberson cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask) 506d7f687fcSJeff Roberson { 507d7f687fcSJeff Roberson struct setlist freelist; 508d7f687fcSJeff Roberson struct setlist droplist; 50973c40187SJeff Roberson struct cpuset *tdset; 510d7f687fcSJeff Roberson struct cpuset *nset; 511d7f687fcSJeff Roberson struct thread *td; 512d7f687fcSJeff Roberson struct proc *p; 513d7f687fcSJeff Roberson int threads; 514d7f687fcSJeff Roberson int nfree; 515d7f687fcSJeff Roberson int error; 516d7f687fcSJeff Roberson /* 517d7f687fcSJeff Roberson * The algorithm requires two passes due to locking considerations. 518d7f687fcSJeff Roberson * 519d7f687fcSJeff Roberson * 1) Lookup the process and acquire the locks in the required order. 520d7f687fcSJeff Roberson * 2) If enough cpusets have not been allocated release the locks and 521d7f687fcSJeff Roberson * allocate them. Loop. 522d7f687fcSJeff Roberson */ 523d7f687fcSJeff Roberson LIST_INIT(&freelist); 524d7f687fcSJeff Roberson LIST_INIT(&droplist); 525d7f687fcSJeff Roberson nfree = 0; 526d7f687fcSJeff Roberson for (;;) { 527d7f687fcSJeff Roberson error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 528d7f687fcSJeff Roberson if (error) 529d7f687fcSJeff Roberson goto out; 530d7f687fcSJeff Roberson if (nfree >= p->p_numthreads) 531d7f687fcSJeff Roberson break; 532d7f687fcSJeff Roberson threads = p->p_numthreads; 533d7f687fcSJeff Roberson PROC_UNLOCK(p); 534d7f687fcSJeff Roberson for (; nfree < threads; nfree++) { 535d7f687fcSJeff Roberson nset = uma_zalloc(cpuset_zone, M_WAITOK); 536d7f687fcSJeff Roberson LIST_INSERT_HEAD(&freelist, nset, cs_link); 537d7f687fcSJeff Roberson } 538d7f687fcSJeff Roberson } 539d7f687fcSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 540d7f687fcSJeff Roberson /* 541d7f687fcSJeff Roberson * Now that the appropriate locks are held and we have enough cpusets, 54273c40187SJeff Roberson * make sure the operation will succeed before applying changes. The 54373c40187SJeff Roberson * proc lock prevents td_cpuset from changing between calls. 544d7f687fcSJeff Roberson */ 545d7f687fcSJeff Roberson error = 0; 546d7f687fcSJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 54773c40187SJeff Roberson thread_lock(td); 54873c40187SJeff Roberson tdset = td->td_cpuset; 54973c40187SJeff Roberson /* 55073c40187SJeff Roberson * Verify that a new mask doesn't specify cpus outside of 55173c40187SJeff Roberson * the set the thread is a member of. 55273c40187SJeff Roberson */ 55373c40187SJeff Roberson if (mask) { 55473c40187SJeff Roberson if (tdset->cs_id == CPUSET_INVALID) 55573c40187SJeff Roberson tdset = tdset->cs_parent; 55673c40187SJeff Roberson if (!CPU_SUBSET(&tdset->cs_mask, mask)) 557a03ee000SJeff Roberson error = EDEADLK; 55873c40187SJeff Roberson /* 55973c40187SJeff Roberson * Verify that a new set won't leave an existing thread 56073c40187SJeff Roberson * mask without a cpu to run on. It can, however, restrict 56173c40187SJeff Roberson * the set. 56273c40187SJeff Roberson */ 56373c40187SJeff Roberson } else if (tdset->cs_id == CPUSET_INVALID) { 56473c40187SJeff Roberson if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask)) 565a03ee000SJeff Roberson error = EDEADLK; 56673c40187SJeff Roberson } 56773c40187SJeff Roberson thread_unlock(td); 56873c40187SJeff Roberson if (error) 56973c40187SJeff Roberson goto unlock_out; 57073c40187SJeff Roberson } 57173c40187SJeff Roberson /* 57273c40187SJeff Roberson * Replace each thread's cpuset while using deferred release. We 573374ae2a3SJeff Roberson * must do this because the thread lock must be held while operating 574374ae2a3SJeff Roberson * on the thread and this limits the type of operations allowed. 57573c40187SJeff Roberson */ 57673c40187SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 577d7f687fcSJeff Roberson thread_lock(td); 578d7f687fcSJeff Roberson /* 579d7f687fcSJeff Roberson * If we presently have an anonymous set or are applying a 580d7f687fcSJeff Roberson * mask we must create an anonymous shadow set. That is 581d7f687fcSJeff Roberson * either parented to our existing base or the supplied set. 582d7f687fcSJeff Roberson * 583d7f687fcSJeff Roberson * If we have a base set with no anonymous shadow we simply 584d7f687fcSJeff Roberson * replace it outright. 585d7f687fcSJeff Roberson */ 586d7f687fcSJeff Roberson tdset = td->td_cpuset; 587d7f687fcSJeff Roberson if (tdset->cs_id == CPUSET_INVALID || mask) { 588d7f687fcSJeff Roberson nset = LIST_FIRST(&freelist); 589d7f687fcSJeff Roberson LIST_REMOVE(nset, cs_link); 590d7f687fcSJeff Roberson if (mask) 591d7f687fcSJeff Roberson error = cpuset_shadow(tdset, nset, mask); 592d7f687fcSJeff Roberson else 593d7f687fcSJeff Roberson error = _cpuset_create(nset, set, 594d7f687fcSJeff Roberson &tdset->cs_mask, CPUSET_INVALID); 595d7f687fcSJeff Roberson if (error) { 596d7f687fcSJeff Roberson LIST_INSERT_HEAD(&freelist, nset, cs_link); 597d7f687fcSJeff Roberson thread_unlock(td); 598d7f687fcSJeff Roberson break; 599d7f687fcSJeff Roberson } 600d7f687fcSJeff Roberson } else 601d7f687fcSJeff Roberson nset = cpuset_ref(set); 602d7f687fcSJeff Roberson cpuset_rel_defer(&droplist, tdset); 603d7f687fcSJeff Roberson td->td_cpuset = nset; 604d7f687fcSJeff Roberson sched_affinity(td); 605d7f687fcSJeff Roberson thread_unlock(td); 606d7f687fcSJeff Roberson } 60773c40187SJeff Roberson unlock_out: 608d7f687fcSJeff Roberson PROC_UNLOCK(p); 609d7f687fcSJeff Roberson out: 610d7f687fcSJeff Roberson while ((nset = LIST_FIRST(&droplist)) != NULL) 611d7f687fcSJeff Roberson cpuset_rel_complete(nset); 612d7f687fcSJeff Roberson while ((nset = LIST_FIRST(&freelist)) != NULL) { 613d7f687fcSJeff Roberson LIST_REMOVE(nset, cs_link); 614d7f687fcSJeff Roberson uma_zfree(cpuset_zone, nset); 615d7f687fcSJeff Roberson } 616d7f687fcSJeff Roberson return (error); 617d7f687fcSJeff Roberson } 618d7f687fcSJeff Roberson 619d7f687fcSJeff Roberson /* 620d7f687fcSJeff Roberson * Apply an anonymous mask to a single thread. 621d7f687fcSJeff Roberson */ 622a03ee000SJeff Roberson int 623d7f687fcSJeff Roberson cpuset_setthread(lwpid_t id, cpuset_t *mask) 624d7f687fcSJeff Roberson { 625d7f687fcSJeff Roberson struct cpuset *nset; 626d7f687fcSJeff Roberson struct cpuset *set; 627d7f687fcSJeff Roberson struct thread *td; 628d7f687fcSJeff Roberson struct proc *p; 629d7f687fcSJeff Roberson int error; 630d7f687fcSJeff Roberson 631d7f687fcSJeff Roberson nset = uma_zalloc(cpuset_zone, M_WAITOK); 6328bd75bddSJeff Roberson error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 633d7f687fcSJeff Roberson if (error) 634d7f687fcSJeff Roberson goto out; 635a03ee000SJeff Roberson set = NULL; 636d7f687fcSJeff Roberson thread_lock(td); 637a03ee000SJeff Roberson error = cpuset_shadow(td->td_cpuset, nset, mask); 638d7f687fcSJeff Roberson if (error == 0) { 639a03ee000SJeff Roberson set = td->td_cpuset; 640d7f687fcSJeff Roberson td->td_cpuset = nset; 641d7f687fcSJeff Roberson sched_affinity(td); 642d7f687fcSJeff Roberson nset = NULL; 643d7f687fcSJeff Roberson } 644d7f687fcSJeff Roberson thread_unlock(td); 645d7f687fcSJeff Roberson PROC_UNLOCK(p); 646a03ee000SJeff Roberson if (set) 647a03ee000SJeff Roberson cpuset_rel(set); 648d7f687fcSJeff Roberson out: 649d7f687fcSJeff Roberson if (nset) 650d7f687fcSJeff Roberson uma_zfree(cpuset_zone, nset); 651d7f687fcSJeff Roberson return (error); 652d7f687fcSJeff Roberson } 653d7f687fcSJeff Roberson 654d7f687fcSJeff Roberson /* 655d7f687fcSJeff Roberson * Creates the cpuset for thread0. We make two sets: 656d7f687fcSJeff Roberson * 657d7f687fcSJeff Roberson * 0 - The root set which should represent all valid processors in the 658d7f687fcSJeff Roberson * system. It is initially created with a mask of all processors 659d7f687fcSJeff Roberson * because we don't know what processors are valid until cpuset_init() 660d7f687fcSJeff Roberson * runs. This set is immutable. 661d7f687fcSJeff Roberson * 1 - The default set which all processes are a member of until changed. 662d7f687fcSJeff Roberson * This allows an administrator to move all threads off of given cpus to 663d7f687fcSJeff Roberson * dedicate them to high priority tasks or save power etc. 664d7f687fcSJeff Roberson */ 665d7f687fcSJeff Roberson struct cpuset * 666d7f687fcSJeff Roberson cpuset_thread0(void) 667d7f687fcSJeff Roberson { 668d7f687fcSJeff Roberson struct cpuset *set; 669d7f687fcSJeff Roberson int error; 670d7f687fcSJeff Roberson 671d7f687fcSJeff Roberson cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 672d7f687fcSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 673d7f687fcSJeff Roberson mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 674d7f687fcSJeff Roberson /* 675d7f687fcSJeff Roberson * Create the root system set for the whole machine. Doesn't use 676d7f687fcSJeff Roberson * cpuset_create() due to NULL parent. 677d7f687fcSJeff Roberson */ 678d7f687fcSJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 679d7f687fcSJeff Roberson set->cs_mask.__bits[0] = -1; 680d7f687fcSJeff Roberson LIST_INIT(&set->cs_children); 681d7f687fcSJeff Roberson LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 682d7f687fcSJeff Roberson set->cs_ref = 1; 683d7f687fcSJeff Roberson set->cs_flags = CPU_SET_ROOT; 684d7f687fcSJeff Roberson cpuset_zero = set; 685a03ee000SJeff Roberson cpuset_root = &set->cs_mask; 686d7f687fcSJeff Roberson /* 687d7f687fcSJeff Roberson * Now derive a default, modifiable set from that to give out. 688d7f687fcSJeff Roberson */ 689d7f687fcSJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK); 690d7f687fcSJeff Roberson error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1); 691d7f687fcSJeff Roberson KASSERT(error == 0, ("Error creating default set: %d\n", error)); 692d7f687fcSJeff Roberson /* 693d7f687fcSJeff Roberson * Initialize the unit allocator. 0 and 1 are allocated above. 694d7f687fcSJeff Roberson */ 695d7f687fcSJeff Roberson cpuset_unr = new_unrhdr(2, INT_MAX, NULL); 696d7f687fcSJeff Roberson 697d7f687fcSJeff Roberson return (set); 698d7f687fcSJeff Roberson } 699d7f687fcSJeff Roberson 700d7f687fcSJeff Roberson /* 701413628a7SBjoern A. Zeeb * Create a cpuset, which would be cpuset_create() but 702413628a7SBjoern A. Zeeb * mark the new 'set' as root. 703413628a7SBjoern A. Zeeb * 70447479a8cSBjoern A. Zeeb * We are not going to reparent the td to it. Use cpuset_setproc_update_set() 70547479a8cSBjoern A. Zeeb * for that. 706413628a7SBjoern A. Zeeb * 707413628a7SBjoern A. Zeeb * In case of no error, returns the set in *setp locked with a reference. 708413628a7SBjoern A. Zeeb */ 709413628a7SBjoern A. Zeeb int 7100304c731SJamie Gritton cpuset_create_root(struct prison *pr, struct cpuset **setp) 711413628a7SBjoern A. Zeeb { 712413628a7SBjoern A. Zeeb struct cpuset *set; 713413628a7SBjoern A. Zeeb int error; 714413628a7SBjoern A. Zeeb 7150304c731SJamie Gritton KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__)); 716413628a7SBjoern A. Zeeb KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 717413628a7SBjoern A. Zeeb 7180304c731SJamie Gritton error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask); 719413628a7SBjoern A. Zeeb if (error) 720413628a7SBjoern A. Zeeb return (error); 721413628a7SBjoern A. Zeeb 722413628a7SBjoern A. Zeeb KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data", 723413628a7SBjoern A. Zeeb __func__, __LINE__)); 724413628a7SBjoern A. Zeeb 725413628a7SBjoern A. Zeeb /* Mark the set as root. */ 726413628a7SBjoern A. Zeeb set = *setp; 727413628a7SBjoern A. Zeeb set->cs_flags |= CPU_SET_ROOT; 728413628a7SBjoern A. Zeeb 729413628a7SBjoern A. Zeeb return (0); 730413628a7SBjoern A. Zeeb } 731413628a7SBjoern A. Zeeb 732413628a7SBjoern A. Zeeb int 733413628a7SBjoern A. Zeeb cpuset_setproc_update_set(struct proc *p, struct cpuset *set) 734413628a7SBjoern A. Zeeb { 735413628a7SBjoern A. Zeeb int error; 736413628a7SBjoern A. Zeeb 737413628a7SBjoern A. Zeeb KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 738413628a7SBjoern A. Zeeb KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 739413628a7SBjoern A. Zeeb 740413628a7SBjoern A. Zeeb cpuset_ref(set); 741413628a7SBjoern A. Zeeb error = cpuset_setproc(p->p_pid, set, NULL); 742413628a7SBjoern A. Zeeb if (error) 743413628a7SBjoern A. Zeeb return (error); 744413628a7SBjoern A. Zeeb cpuset_rel(set); 745413628a7SBjoern A. Zeeb return (0); 746413628a7SBjoern A. Zeeb } 747413628a7SBjoern A. Zeeb 748413628a7SBjoern A. Zeeb /* 749d7f687fcSJeff Roberson * This is called once the final set of system cpus is known. Modifies 75093902625SJohn Baldwin * the root set and all children and mark the root read-only. 751d7f687fcSJeff Roberson */ 752d7f687fcSJeff Roberson static void 753d7f687fcSJeff Roberson cpuset_init(void *arg) 754d7f687fcSJeff Roberson { 755d7f687fcSJeff Roberson cpuset_t mask; 756d7f687fcSJeff Roberson 757d7f687fcSJeff Roberson CPU_ZERO(&mask); 758d7f687fcSJeff Roberson #ifdef SMP 759d7f687fcSJeff Roberson mask.__bits[0] = all_cpus; 760d7f687fcSJeff Roberson #else 761d7f687fcSJeff Roberson mask.__bits[0] = 1; 762d7f687fcSJeff Roberson #endif 763d7f687fcSJeff Roberson if (cpuset_modify(cpuset_zero, &mask)) 764d7f687fcSJeff Roberson panic("Can't set initial cpuset mask.\n"); 765d7f687fcSJeff Roberson cpuset_zero->cs_flags |= CPU_SET_RDONLY; 766d7f687fcSJeff Roberson } 767d7f687fcSJeff Roberson SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL); 768d7f687fcSJeff Roberson 769d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 770d7f687fcSJeff Roberson struct cpuset_args { 771d7f687fcSJeff Roberson cpusetid_t *setid; 772d7f687fcSJeff Roberson }; 773d7f687fcSJeff Roberson #endif 774d7f687fcSJeff Roberson int 775d7f687fcSJeff Roberson cpuset(struct thread *td, struct cpuset_args *uap) 776d7f687fcSJeff Roberson { 777d7f687fcSJeff Roberson struct cpuset *root; 778d7f687fcSJeff Roberson struct cpuset *set; 779d7f687fcSJeff Roberson int error; 780d7f687fcSJeff Roberson 781d7f687fcSJeff Roberson thread_lock(td); 782a03ee000SJeff Roberson root = cpuset_refroot(td->td_cpuset); 783d7f687fcSJeff Roberson thread_unlock(td); 784d7f687fcSJeff Roberson error = cpuset_create(&set, root, &root->cs_mask); 785d7f687fcSJeff Roberson cpuset_rel(root); 786d7f687fcSJeff Roberson if (error) 787d7f687fcSJeff Roberson return (error); 788d7f687fcSJeff Roberson error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 789a03ee000SJeff Roberson if (error == 0) 790a03ee000SJeff Roberson error = cpuset_setproc(-1, set, NULL); 791d7f687fcSJeff Roberson cpuset_rel(set); 792d7f687fcSJeff Roberson return (error); 793d7f687fcSJeff Roberson } 794d7f687fcSJeff Roberson 795d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 796d7f687fcSJeff Roberson struct cpuset_setid_args { 797d7f687fcSJeff Roberson cpuwhich_t which; 798d7f687fcSJeff Roberson id_t id; 799d7f687fcSJeff Roberson cpusetid_t setid; 800d7f687fcSJeff Roberson }; 801d7f687fcSJeff Roberson #endif 802d7f687fcSJeff Roberson int 803d7f687fcSJeff Roberson cpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 804d7f687fcSJeff Roberson { 805d7f687fcSJeff Roberson struct cpuset *set; 806d7f687fcSJeff Roberson int error; 807d7f687fcSJeff Roberson 808d7f687fcSJeff Roberson /* 809d7f687fcSJeff Roberson * Presently we only support per-process sets. 810d7f687fcSJeff Roberson */ 811d7f687fcSJeff Roberson if (uap->which != CPU_WHICH_PID) 812d7f687fcSJeff Roberson return (EINVAL); 813413628a7SBjoern A. Zeeb set = cpuset_lookup(uap->setid, td); 814d7f687fcSJeff Roberson if (set == NULL) 815d7f687fcSJeff Roberson return (ESRCH); 816d7f687fcSJeff Roberson error = cpuset_setproc(uap->id, set, NULL); 817d7f687fcSJeff Roberson cpuset_rel(set); 818d7f687fcSJeff Roberson return (error); 819d7f687fcSJeff Roberson } 820d7f687fcSJeff Roberson 821d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 822d7f687fcSJeff Roberson struct cpuset_getid_args { 823d7f687fcSJeff Roberson cpulevel_t level; 824d7f687fcSJeff Roberson cpuwhich_t which; 825d7f687fcSJeff Roberson id_t id; 826d7f687fcSJeff Roberson cpusetid_t *setid; 827d7f687fcSJeff Roberson #endif 828d7f687fcSJeff Roberson int 829d7f687fcSJeff Roberson cpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 830d7f687fcSJeff Roberson { 831d7f687fcSJeff Roberson struct cpuset *nset; 832d7f687fcSJeff Roberson struct cpuset *set; 833d7f687fcSJeff Roberson struct thread *ttd; 834d7f687fcSJeff Roberson struct proc *p; 835d7f687fcSJeff Roberson cpusetid_t id; 836d7f687fcSJeff Roberson int error; 837d7f687fcSJeff Roberson 838d7f687fcSJeff Roberson if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET) 839d7f687fcSJeff Roberson return (EINVAL); 840d7f687fcSJeff Roberson error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 841d7f687fcSJeff Roberson if (error) 842d7f687fcSJeff Roberson return (error); 843d7f687fcSJeff Roberson switch (uap->which) { 844d7f687fcSJeff Roberson case CPU_WHICH_TID: 845d7f687fcSJeff Roberson case CPU_WHICH_PID: 846d7f687fcSJeff Roberson thread_lock(ttd); 847a03ee000SJeff Roberson set = cpuset_refbase(ttd->td_cpuset); 848d7f687fcSJeff Roberson thread_unlock(ttd); 849d7f687fcSJeff Roberson PROC_UNLOCK(p); 850d7f687fcSJeff Roberson break; 851d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 852413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 853d7f687fcSJeff Roberson break; 8549b33b154SJeff Roberson case CPU_WHICH_IRQ: 8559b33b154SJeff Roberson return (EINVAL); 856d7f687fcSJeff Roberson } 857d7f687fcSJeff Roberson switch (uap->level) { 858d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 859a03ee000SJeff Roberson nset = cpuset_refroot(set); 860d7f687fcSJeff Roberson cpuset_rel(set); 861d7f687fcSJeff Roberson set = nset; 862d7f687fcSJeff Roberson break; 863d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 864d7f687fcSJeff Roberson break; 865d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 866d7f687fcSJeff Roberson break; 867d7f687fcSJeff Roberson } 868d7f687fcSJeff Roberson id = set->cs_id; 869d7f687fcSJeff Roberson cpuset_rel(set); 870d7f687fcSJeff Roberson if (error == 0) 871d7f687fcSJeff Roberson error = copyout(&id, uap->setid, sizeof(id)); 872d7f687fcSJeff Roberson 873d7f687fcSJeff Roberson return (error); 874d7f687fcSJeff Roberson } 875d7f687fcSJeff Roberson 876d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 877d7f687fcSJeff Roberson struct cpuset_getaffinity_args { 878d7f687fcSJeff Roberson cpulevel_t level; 879d7f687fcSJeff Roberson cpuwhich_t which; 8807f64829aSRuslan Ermilov id_t id; 8817f64829aSRuslan Ermilov size_t cpusetsize; 8827f64829aSRuslan Ermilov cpuset_t *mask; 883d7f687fcSJeff Roberson }; 884d7f687fcSJeff Roberson #endif 885d7f687fcSJeff Roberson int 886d7f687fcSJeff Roberson cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 887d7f687fcSJeff Roberson { 888d7f687fcSJeff Roberson struct thread *ttd; 889d7f687fcSJeff Roberson struct cpuset *nset; 890d7f687fcSJeff Roberson struct cpuset *set; 891d7f687fcSJeff Roberson struct proc *p; 892d7f687fcSJeff Roberson cpuset_t *mask; 893d7f687fcSJeff Roberson int error; 8947f64829aSRuslan Ermilov size_t size; 895d7f687fcSJeff Roberson 89673c40187SJeff Roberson if (uap->cpusetsize < sizeof(cpuset_t) || 897887aedc6SKonstantin Belousov uap->cpusetsize > CPU_MAXSIZE / NBBY) 898d7f687fcSJeff Roberson return (ERANGE); 89973c40187SJeff Roberson size = uap->cpusetsize; 900d7f687fcSJeff Roberson mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 901d7f687fcSJeff Roberson error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 902d7f687fcSJeff Roberson if (error) 903d7f687fcSJeff Roberson goto out; 904d7f687fcSJeff Roberson switch (uap->level) { 905d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 906d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 907d7f687fcSJeff Roberson switch (uap->which) { 908d7f687fcSJeff Roberson case CPU_WHICH_TID: 909d7f687fcSJeff Roberson case CPU_WHICH_PID: 910d7f687fcSJeff Roberson thread_lock(ttd); 911d7f687fcSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 912d7f687fcSJeff Roberson thread_unlock(ttd); 913d7f687fcSJeff Roberson break; 914d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 915413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 916d7f687fcSJeff Roberson break; 9179b33b154SJeff Roberson case CPU_WHICH_IRQ: 9189b33b154SJeff Roberson error = EINVAL; 9199b33b154SJeff Roberson goto out; 920d7f687fcSJeff Roberson } 921d7f687fcSJeff Roberson if (uap->level == CPU_LEVEL_ROOT) 922a03ee000SJeff Roberson nset = cpuset_refroot(set); 923d7f687fcSJeff Roberson else 924a03ee000SJeff Roberson nset = cpuset_refbase(set); 925d7f687fcSJeff Roberson CPU_COPY(&nset->cs_mask, mask); 926d7f687fcSJeff Roberson cpuset_rel(nset); 927d7f687fcSJeff Roberson break; 928d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 929d7f687fcSJeff Roberson switch (uap->which) { 930d7f687fcSJeff Roberson case CPU_WHICH_TID: 931d7f687fcSJeff Roberson thread_lock(ttd); 932d7f687fcSJeff Roberson CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 933d7f687fcSJeff Roberson thread_unlock(ttd); 934d7f687fcSJeff Roberson break; 935d7f687fcSJeff Roberson case CPU_WHICH_PID: 936d7f687fcSJeff Roberson FOREACH_THREAD_IN_PROC(p, ttd) { 937d7f687fcSJeff Roberson thread_lock(ttd); 938d7f687fcSJeff Roberson CPU_OR(mask, &ttd->td_cpuset->cs_mask); 939d7f687fcSJeff Roberson thread_unlock(ttd); 940d7f687fcSJeff Roberson } 941d7f687fcSJeff Roberson break; 942d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 943413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 944d7f687fcSJeff Roberson CPU_COPY(&set->cs_mask, mask); 945d7f687fcSJeff Roberson break; 9469b33b154SJeff Roberson case CPU_WHICH_IRQ: 9479b33b154SJeff Roberson error = intr_getaffinity(uap->id, mask); 9489b33b154SJeff Roberson break; 949d7f687fcSJeff Roberson } 950d7f687fcSJeff Roberson break; 951d7f687fcSJeff Roberson default: 952d7f687fcSJeff Roberson error = EINVAL; 953d7f687fcSJeff Roberson break; 954d7f687fcSJeff Roberson } 955d7f687fcSJeff Roberson if (set) 956d7f687fcSJeff Roberson cpuset_rel(set); 957d7f687fcSJeff Roberson if (p) 958d7f687fcSJeff Roberson PROC_UNLOCK(p); 959d7f687fcSJeff Roberson if (error == 0) 960d7f687fcSJeff Roberson error = copyout(mask, uap->mask, size); 961d7f687fcSJeff Roberson out: 962d7f687fcSJeff Roberson free(mask, M_TEMP); 963d7f687fcSJeff Roberson return (error); 964d7f687fcSJeff Roberson } 965d7f687fcSJeff Roberson 966d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 967d7f687fcSJeff Roberson struct cpuset_setaffinity_args { 968d7f687fcSJeff Roberson cpulevel_t level; 969d7f687fcSJeff Roberson cpuwhich_t which; 9707f64829aSRuslan Ermilov id_t id; 9717f64829aSRuslan Ermilov size_t cpusetsize; 9727f64829aSRuslan Ermilov const cpuset_t *mask; 973d7f687fcSJeff Roberson }; 974d7f687fcSJeff Roberson #endif 975d7f687fcSJeff Roberson int 976d7f687fcSJeff Roberson cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 977d7f687fcSJeff Roberson { 978d7f687fcSJeff Roberson struct cpuset *nset; 979d7f687fcSJeff Roberson struct cpuset *set; 980d7f687fcSJeff Roberson struct thread *ttd; 981d7f687fcSJeff Roberson struct proc *p; 982d7f687fcSJeff Roberson cpuset_t *mask; 983d7f687fcSJeff Roberson int error; 984d7f687fcSJeff Roberson 98573c40187SJeff Roberson if (uap->cpusetsize < sizeof(cpuset_t) || 986887aedc6SKonstantin Belousov uap->cpusetsize > CPU_MAXSIZE / NBBY) 987d7f687fcSJeff Roberson return (ERANGE); 98873c40187SJeff Roberson mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO); 98973c40187SJeff Roberson error = copyin(uap->mask, mask, uap->cpusetsize); 990d7f687fcSJeff Roberson if (error) 991d7f687fcSJeff Roberson goto out; 99273c40187SJeff Roberson /* 99373c40187SJeff Roberson * Verify that no high bits are set. 99473c40187SJeff Roberson */ 99573c40187SJeff Roberson if (uap->cpusetsize > sizeof(cpuset_t)) { 99673c40187SJeff Roberson char *end; 99773c40187SJeff Roberson char *cp; 99873c40187SJeff Roberson 99973c40187SJeff Roberson end = cp = (char *)&mask->__bits; 100073c40187SJeff Roberson end += uap->cpusetsize; 100173c40187SJeff Roberson cp += sizeof(cpuset_t); 100273c40187SJeff Roberson while (cp != end) 100373c40187SJeff Roberson if (*cp++ != 0) { 100473c40187SJeff Roberson error = EINVAL; 100573c40187SJeff Roberson goto out; 100673c40187SJeff Roberson } 100773c40187SJeff Roberson 100873c40187SJeff Roberson } 1009d7f687fcSJeff Roberson switch (uap->level) { 1010d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 1011d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 1012d7f687fcSJeff Roberson error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 1013d7f687fcSJeff Roberson if (error) 1014d7f687fcSJeff Roberson break; 1015d7f687fcSJeff Roberson switch (uap->which) { 1016d7f687fcSJeff Roberson case CPU_WHICH_TID: 1017d7f687fcSJeff Roberson case CPU_WHICH_PID: 1018d7f687fcSJeff Roberson thread_lock(ttd); 1019d7f687fcSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 1020d7f687fcSJeff Roberson thread_unlock(ttd); 1021c6440f72SJeff Roberson PROC_UNLOCK(p); 1022d7f687fcSJeff Roberson break; 1023d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1024413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1025d7f687fcSJeff Roberson break; 10269b33b154SJeff Roberson case CPU_WHICH_IRQ: 10279b33b154SJeff Roberson error = EINVAL; 10289b33b154SJeff Roberson goto out; 1029d7f687fcSJeff Roberson } 1030d7f687fcSJeff Roberson if (uap->level == CPU_LEVEL_ROOT) 1031a03ee000SJeff Roberson nset = cpuset_refroot(set); 1032d7f687fcSJeff Roberson else 1033a03ee000SJeff Roberson nset = cpuset_refbase(set); 1034d7f687fcSJeff Roberson error = cpuset_modify(nset, mask); 1035d7f687fcSJeff Roberson cpuset_rel(nset); 1036d7f687fcSJeff Roberson cpuset_rel(set); 1037d7f687fcSJeff Roberson break; 1038d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 1039d7f687fcSJeff Roberson switch (uap->which) { 1040d7f687fcSJeff Roberson case CPU_WHICH_TID: 1041d7f687fcSJeff Roberson error = cpuset_setthread(uap->id, mask); 1042d7f687fcSJeff Roberson break; 1043d7f687fcSJeff Roberson case CPU_WHICH_PID: 1044d7f687fcSJeff Roberson error = cpuset_setproc(uap->id, NULL, mask); 1045d7f687fcSJeff Roberson break; 1046d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1047413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1048413628a7SBjoern A. Zeeb error = cpuset_which(uap->which, uap->id, &p, 1049d7f687fcSJeff Roberson &ttd, &set); 1050d7f687fcSJeff Roberson if (error == 0) { 1051d7f687fcSJeff Roberson error = cpuset_modify(set, mask); 1052d7f687fcSJeff Roberson cpuset_rel(set); 1053d7f687fcSJeff Roberson } 1054d7f687fcSJeff Roberson break; 10559b33b154SJeff Roberson case CPU_WHICH_IRQ: 10569b33b154SJeff Roberson error = intr_setaffinity(uap->id, mask); 10579b33b154SJeff Roberson break; 1058d7f687fcSJeff Roberson default: 1059d7f687fcSJeff Roberson error = EINVAL; 1060d7f687fcSJeff Roberson break; 1061d7f687fcSJeff Roberson } 1062d7f687fcSJeff Roberson break; 1063d7f687fcSJeff Roberson default: 1064d7f687fcSJeff Roberson error = EINVAL; 1065d7f687fcSJeff Roberson break; 1066d7f687fcSJeff Roberson } 1067d7f687fcSJeff Roberson out: 1068d7f687fcSJeff Roberson free(mask, M_TEMP); 1069d7f687fcSJeff Roberson return (error); 1070d7f687fcSJeff Roberson } 1071dea0ed66SBjoern A. Zeeb 1072dea0ed66SBjoern A. Zeeb #ifdef DDB 1073dea0ed66SBjoern A. Zeeb DB_SHOW_COMMAND(cpusets, db_show_cpusets) 1074dea0ed66SBjoern A. Zeeb { 1075dea0ed66SBjoern A. Zeeb struct cpuset *set; 1076dea0ed66SBjoern A. Zeeb int cpu, once; 1077dea0ed66SBjoern A. Zeeb 1078dea0ed66SBjoern A. Zeeb LIST_FOREACH(set, &cpuset_ids, cs_link) { 1079dea0ed66SBjoern A. Zeeb db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 1080dea0ed66SBjoern A. Zeeb set, set->cs_id, set->cs_ref, set->cs_flags, 1081dea0ed66SBjoern A. Zeeb (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 1082dea0ed66SBjoern A. Zeeb db_printf(" mask="); 1083dea0ed66SBjoern A. Zeeb for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { 1084dea0ed66SBjoern A. Zeeb if (CPU_ISSET(cpu, &set->cs_mask)) { 1085dea0ed66SBjoern A. Zeeb if (once == 0) { 1086dea0ed66SBjoern A. Zeeb db_printf("%d", cpu); 1087dea0ed66SBjoern A. Zeeb once = 1; 1088dea0ed66SBjoern A. Zeeb } else 1089dea0ed66SBjoern A. Zeeb db_printf(",%d", cpu); 1090dea0ed66SBjoern A. Zeeb } 1091dea0ed66SBjoern A. Zeeb } 1092dea0ed66SBjoern A. Zeeb db_printf("\n"); 1093dea0ed66SBjoern A. Zeeb if (db_pager_quit) 1094dea0ed66SBjoern A. Zeeb break; 1095dea0ed66SBjoern A. Zeeb } 1096dea0ed66SBjoern A. Zeeb } 1097dea0ed66SBjoern A. Zeeb #endif /* DDB */ 1098