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> 53e3709597SAttilio Rao #include <sys/libkern.h> 54d7f687fcSJeff Roberson #include <sys/limits.h> 55a03ee000SJeff Roberson #include <sys/bus.h> 56a03ee000SJeff Roberson #include <sys/interrupt.h> 57d7f687fcSJeff Roberson 58d7f687fcSJeff Roberson #include <vm/uma.h> 59c0ae6688SJohn Baldwin #include <vm/vm.h> 60c0ae6688SJohn Baldwin #include <vm/vm_page.h> 61c0ae6688SJohn Baldwin #include <vm/vm_param.h> 62d7f687fcSJeff Roberson 63dea0ed66SBjoern A. Zeeb #ifdef DDB 64dea0ed66SBjoern A. Zeeb #include <ddb/ddb.h> 65dea0ed66SBjoern A. Zeeb #endif /* DDB */ 66dea0ed66SBjoern A. Zeeb 67d7f687fcSJeff Roberson /* 68d7f687fcSJeff Roberson * cpusets provide a mechanism for creating and manipulating sets of 69d7f687fcSJeff Roberson * processors for the purpose of constraining the scheduling of threads to 70d7f687fcSJeff Roberson * specific processors. 71d7f687fcSJeff Roberson * 72d7f687fcSJeff Roberson * Each process belongs to an identified set, by default this is set 1. Each 73d7f687fcSJeff Roberson * thread may further restrict the cpus it may run on to a subset of this 74d7f687fcSJeff Roberson * named set. This creates an anonymous set which other threads and processes 75d7f687fcSJeff Roberson * may not join by number. 76d7f687fcSJeff Roberson * 77d7f687fcSJeff Roberson * The named set is referred to herein as the 'base' set to avoid ambiguity. 78d7f687fcSJeff Roberson * This set is usually a child of a 'root' set while the anonymous set may 79d7f687fcSJeff Roberson * simply be referred to as a mask. In the syscall api these are referred to 80d7f687fcSJeff Roberson * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here. 81d7f687fcSJeff Roberson * 82d7f687fcSJeff Roberson * Threads inherit their set from their creator whether it be anonymous or 83d7f687fcSJeff Roberson * not. This means that anonymous sets are immutable because they may be 84d7f687fcSJeff Roberson * shared. To modify an anonymous set a new set is created with the desired 85d7f687fcSJeff Roberson * mask and the same parent as the existing anonymous set. This gives the 8693902625SJohn Baldwin * illusion of each thread having a private mask. 87d7f687fcSJeff Roberson * 88d7f687fcSJeff Roberson * Via the syscall apis a user may ask to retrieve or modify the root, base, 89d7f687fcSJeff Roberson * or mask that is discovered via a pid, tid, or setid. Modifying a set 90d7f687fcSJeff Roberson * modifies all numbered and anonymous child sets to comply with the new mask. 91d7f687fcSJeff Roberson * Modifying a pid or tid's mask applies only to that tid but must still 92d7f687fcSJeff Roberson * exist within the assigned parent set. 93d7f687fcSJeff Roberson * 9486855bf5SJohn Baldwin * A thread may not be assigned to a group separate from other threads in 95d7f687fcSJeff Roberson * the process. This is to remove ambiguity when the setid is queried with 96d7f687fcSJeff Roberson * a pid argument. There is no other technical limitation. 97d7f687fcSJeff Roberson * 98d7f687fcSJeff Roberson * This somewhat complex arrangement is intended to make it easy for 99d7f687fcSJeff Roberson * applications to query available processors and bind their threads to 100d7f687fcSJeff Roberson * specific processors while also allowing administrators to dynamically 101d7f687fcSJeff Roberson * reprovision by changing sets which apply to groups of processes. 102d7f687fcSJeff Roberson * 103d7f687fcSJeff Roberson * A simple application should not concern itself with sets at all and 104d7f687fcSJeff Roberson * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id 10593902625SJohn Baldwin * meaning 'curthread'. It may query available cpus for that tid with a 106d7f687fcSJeff Roberson * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...). 107d7f687fcSJeff Roberson */ 108d7f687fcSJeff Roberson static uma_zone_t cpuset_zone; 109d7f687fcSJeff Roberson static struct mtx cpuset_lock; 110d7f687fcSJeff Roberson static struct setlist cpuset_ids; 111d7f687fcSJeff Roberson static struct unrhdr *cpuset_unr; 11281198539SAlexander V. Chernikov static struct cpuset *cpuset_zero, *cpuset_default; 113a03ee000SJeff Roberson 114444528c0SDavid Xu /* Return the size of cpuset_t at the kernel level */ 11560aa2c85SJonathan Anderson SYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD | CTLFLAG_CAPRD, 116f0188618SHans Petter Selasky SYSCTL_NULL_INT_PTR, sizeof(cpuset_t), "sizeof(cpuset_t)"); 117444528c0SDavid Xu 118a03ee000SJeff Roberson cpuset_t *cpuset_root; 119c0ae6688SJohn Baldwin cpuset_t cpuset_domain[MAXMEMDOM]; 120d7f687fcSJeff Roberson 121d7f687fcSJeff Roberson /* 122d7f687fcSJeff Roberson * Acquire a reference to a cpuset, all pointers must be tracked with refs. 123d7f687fcSJeff Roberson */ 124d7f687fcSJeff Roberson struct cpuset * 125d7f687fcSJeff Roberson cpuset_ref(struct cpuset *set) 126d7f687fcSJeff Roberson { 127d7f687fcSJeff Roberson 128d7f687fcSJeff Roberson refcount_acquire(&set->cs_ref); 129d7f687fcSJeff Roberson return (set); 130d7f687fcSJeff Roberson } 131d7f687fcSJeff Roberson 132d7f687fcSJeff Roberson /* 1337a8f695aSBjoern A. Zeeb * Walks up the tree from 'set' to find the root. Returns the root 1347a8f695aSBjoern A. Zeeb * referenced. 1357a8f695aSBjoern A. Zeeb */ 1367a8f695aSBjoern A. Zeeb static struct cpuset * 1377a8f695aSBjoern A. Zeeb cpuset_refroot(struct cpuset *set) 1387a8f695aSBjoern A. Zeeb { 1397a8f695aSBjoern A. Zeeb 1407a8f695aSBjoern A. Zeeb for (; set->cs_parent != NULL; set = set->cs_parent) 1417a8f695aSBjoern A. Zeeb if (set->cs_flags & CPU_SET_ROOT) 1427a8f695aSBjoern A. Zeeb break; 1437a8f695aSBjoern A. Zeeb cpuset_ref(set); 1447a8f695aSBjoern A. Zeeb 1457a8f695aSBjoern A. Zeeb return (set); 1467a8f695aSBjoern A. Zeeb } 1477a8f695aSBjoern A. Zeeb 1487a8f695aSBjoern A. Zeeb /* 1497a8f695aSBjoern A. Zeeb * Find the first non-anonymous set starting from 'set'. Returns this set 1507a8f695aSBjoern A. Zeeb * referenced. May return the passed in set with an extra ref if it is 1517a8f695aSBjoern A. Zeeb * not anonymous. 1527a8f695aSBjoern A. Zeeb */ 1537a8f695aSBjoern A. Zeeb static struct cpuset * 1547a8f695aSBjoern A. Zeeb cpuset_refbase(struct cpuset *set) 1557a8f695aSBjoern A. Zeeb { 1567a8f695aSBjoern A. Zeeb 1577a8f695aSBjoern A. Zeeb if (set->cs_id == CPUSET_INVALID) 1587a8f695aSBjoern A. Zeeb set = set->cs_parent; 1597a8f695aSBjoern A. Zeeb cpuset_ref(set); 1607a8f695aSBjoern A. Zeeb 1617a8f695aSBjoern A. Zeeb return (set); 1627a8f695aSBjoern A. Zeeb } 1637a8f695aSBjoern A. Zeeb 1647a8f695aSBjoern A. Zeeb /* 16593902625SJohn Baldwin * Release a reference in a context where it is safe to allocate. 166d7f687fcSJeff Roberson */ 167d7f687fcSJeff Roberson void 168d7f687fcSJeff Roberson cpuset_rel(struct cpuset *set) 169d7f687fcSJeff Roberson { 170d7f687fcSJeff Roberson cpusetid_t id; 171d7f687fcSJeff Roberson 172d7f687fcSJeff Roberson if (refcount_release(&set->cs_ref) == 0) 173d7f687fcSJeff Roberson return; 174d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 175d7f687fcSJeff Roberson LIST_REMOVE(set, cs_siblings); 176d7f687fcSJeff Roberson id = set->cs_id; 177d7f687fcSJeff Roberson if (id != CPUSET_INVALID) 178d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 179d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 180d7f687fcSJeff Roberson cpuset_rel(set->cs_parent); 181d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 182d7f687fcSJeff Roberson if (id != CPUSET_INVALID) 183d7f687fcSJeff Roberson free_unr(cpuset_unr, id); 184d7f687fcSJeff Roberson } 185d7f687fcSJeff Roberson 186d7f687fcSJeff Roberson /* 187d7f687fcSJeff Roberson * Deferred release must be used when in a context that is not safe to 188d7f687fcSJeff Roberson * allocate/free. This places any unreferenced sets on the list 'head'. 189d7f687fcSJeff Roberson */ 190d7f687fcSJeff Roberson static void 191d7f687fcSJeff Roberson cpuset_rel_defer(struct setlist *head, struct cpuset *set) 192d7f687fcSJeff Roberson { 193d7f687fcSJeff Roberson 194d7f687fcSJeff Roberson if (refcount_release(&set->cs_ref) == 0) 195d7f687fcSJeff Roberson return; 196d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 197d7f687fcSJeff Roberson LIST_REMOVE(set, cs_siblings); 198d7f687fcSJeff Roberson if (set->cs_id != CPUSET_INVALID) 199d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 200d7f687fcSJeff Roberson LIST_INSERT_HEAD(head, set, cs_link); 201d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 202d7f687fcSJeff Roberson } 203d7f687fcSJeff Roberson 204d7f687fcSJeff Roberson /* 205d7f687fcSJeff Roberson * Complete a deferred release. Removes the set from the list provided to 206d7f687fcSJeff Roberson * cpuset_rel_defer. 207d7f687fcSJeff Roberson */ 208d7f687fcSJeff Roberson static void 209d7f687fcSJeff Roberson cpuset_rel_complete(struct cpuset *set) 210d7f687fcSJeff Roberson { 211d7f687fcSJeff Roberson LIST_REMOVE(set, cs_link); 212d7f687fcSJeff Roberson cpuset_rel(set->cs_parent); 213d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 214d7f687fcSJeff Roberson } 215d7f687fcSJeff Roberson 216d7f687fcSJeff Roberson /* 217d7f687fcSJeff Roberson * Find a set based on an id. Returns it with a ref. 218d7f687fcSJeff Roberson */ 219d7f687fcSJeff Roberson static struct cpuset * 220413628a7SBjoern A. Zeeb cpuset_lookup(cpusetid_t setid, struct thread *td) 221d7f687fcSJeff Roberson { 222d7f687fcSJeff Roberson struct cpuset *set; 223d7f687fcSJeff Roberson 224d7f687fcSJeff Roberson if (setid == CPUSET_INVALID) 225d7f687fcSJeff Roberson return (NULL); 226d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 227d7f687fcSJeff Roberson LIST_FOREACH(set, &cpuset_ids, cs_link) 228d7f687fcSJeff Roberson if (set->cs_id == setid) 229d7f687fcSJeff Roberson break; 230d7f687fcSJeff Roberson if (set) 231d7f687fcSJeff Roberson cpuset_ref(set); 232d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 233413628a7SBjoern A. Zeeb 234413628a7SBjoern A. Zeeb KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__)); 235413628a7SBjoern A. Zeeb if (set != NULL && jailed(td->td_ucred)) { 2360304c731SJamie Gritton struct cpuset *jset, *tset; 237413628a7SBjoern A. Zeeb 2380304c731SJamie Gritton jset = td->td_ucred->cr_prison->pr_cpuset; 2390304c731SJamie Gritton for (tset = set; tset != NULL; tset = tset->cs_parent) 2400304c731SJamie Gritton if (tset == jset) 2410304c731SJamie Gritton break; 2420304c731SJamie Gritton if (tset == NULL) { 243413628a7SBjoern A. Zeeb cpuset_rel(set); 244413628a7SBjoern A. Zeeb set = NULL; 245413628a7SBjoern A. Zeeb } 246413628a7SBjoern A. Zeeb } 247413628a7SBjoern A. Zeeb 248d7f687fcSJeff Roberson return (set); 249d7f687fcSJeff Roberson } 250d7f687fcSJeff Roberson 251d7f687fcSJeff Roberson /* 252d7f687fcSJeff Roberson * Create a set in the space provided in 'set' with the provided parameters. 253d7f687fcSJeff Roberson * The set is returned with a single ref. May return EDEADLK if the set 254d7f687fcSJeff Roberson * will have no valid cpu based on restrictions from the parent. 255d7f687fcSJeff Roberson */ 256d7f687fcSJeff Roberson static int 257e84c2db1SJohn Baldwin _cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask, 258d7f687fcSJeff Roberson cpusetid_t id) 259d7f687fcSJeff Roberson { 260d7f687fcSJeff Roberson 26173c40187SJeff Roberson if (!CPU_OVERLAP(&parent->cs_mask, mask)) 26273c40187SJeff Roberson return (EDEADLK); 263d7f687fcSJeff Roberson CPU_COPY(mask, &set->cs_mask); 264d7f687fcSJeff Roberson LIST_INIT(&set->cs_children); 265d7f687fcSJeff Roberson refcount_init(&set->cs_ref, 1); 266d7f687fcSJeff Roberson set->cs_flags = 0; 267d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 268e84c2db1SJohn Baldwin CPU_AND(&set->cs_mask, &parent->cs_mask); 269d7f687fcSJeff Roberson set->cs_id = id; 270d7f687fcSJeff Roberson set->cs_parent = cpuset_ref(parent); 271d7f687fcSJeff Roberson LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings); 272d7f687fcSJeff Roberson if (set->cs_id != CPUSET_INVALID) 273d7f687fcSJeff Roberson LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 274d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 275d7f687fcSJeff Roberson 27673c40187SJeff Roberson return (0); 277d7f687fcSJeff Roberson } 278d7f687fcSJeff Roberson 279d7f687fcSJeff Roberson /* 280d7f687fcSJeff Roberson * Create a new non-anonymous set with the requested parent and mask. May 281d7f687fcSJeff Roberson * return failures if the mask is invalid or a new number can not be 282d7f687fcSJeff Roberson * allocated. 283d7f687fcSJeff Roberson */ 284d7f687fcSJeff Roberson static int 285e84c2db1SJohn Baldwin cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask) 286d7f687fcSJeff Roberson { 287d7f687fcSJeff Roberson struct cpuset *set; 288d7f687fcSJeff Roberson cpusetid_t id; 289d7f687fcSJeff Roberson int error; 290d7f687fcSJeff Roberson 291d7f687fcSJeff Roberson id = alloc_unr(cpuset_unr); 292d7f687fcSJeff Roberson if (id == -1) 293d7f687fcSJeff Roberson return (ENFILE); 294d7f687fcSJeff Roberson *setp = set = uma_zalloc(cpuset_zone, M_WAITOK); 295d7f687fcSJeff Roberson error = _cpuset_create(set, parent, mask, id); 296d7f687fcSJeff Roberson if (error == 0) 297d7f687fcSJeff Roberson return (0); 298d7f687fcSJeff Roberson free_unr(cpuset_unr, id); 299d7f687fcSJeff Roberson uma_zfree(cpuset_zone, set); 300d7f687fcSJeff Roberson 301d7f687fcSJeff Roberson return (error); 302d7f687fcSJeff Roberson } 303d7f687fcSJeff Roberson 304d7f687fcSJeff Roberson /* 305d7f687fcSJeff Roberson * Recursively check for errors that would occur from applying mask to 306d7f687fcSJeff Roberson * the tree of sets starting at 'set'. Checks for sets that would become 307d7f687fcSJeff Roberson * empty as well as RDONLY flags. 308d7f687fcSJeff Roberson */ 309d7f687fcSJeff Roberson static int 310c9813d0aSJohn Baldwin cpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_mask) 311d7f687fcSJeff Roberson { 312d7f687fcSJeff Roberson struct cpuset *nset; 313d7f687fcSJeff Roberson cpuset_t newmask; 314d7f687fcSJeff Roberson int error; 315d7f687fcSJeff Roberson 316d7f687fcSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 317d7f687fcSJeff Roberson if (set->cs_flags & CPU_SET_RDONLY) 318d7f687fcSJeff Roberson return (EPERM); 319c9813d0aSJohn Baldwin if (check_mask) { 32073c40187SJeff Roberson if (!CPU_OVERLAP(&set->cs_mask, mask)) 32173c40187SJeff Roberson return (EDEADLK); 322d7f687fcSJeff Roberson CPU_COPY(&set->cs_mask, &newmask); 323d7f687fcSJeff Roberson CPU_AND(&newmask, mask); 324c9813d0aSJohn Baldwin } else 325c9813d0aSJohn Baldwin CPU_COPY(mask, &newmask); 32673c40187SJeff Roberson error = 0; 327d7f687fcSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 328c9813d0aSJohn Baldwin if ((error = cpuset_testupdate(nset, &newmask, 1)) != 0) 329d7f687fcSJeff Roberson break; 330d7f687fcSJeff Roberson return (error); 331d7f687fcSJeff Roberson } 332d7f687fcSJeff Roberson 333d7f687fcSJeff Roberson /* 334d7f687fcSJeff Roberson * Applies the mask 'mask' without checking for empty sets or permissions. 335d7f687fcSJeff Roberson */ 336d7f687fcSJeff Roberson static void 337d7f687fcSJeff Roberson cpuset_update(struct cpuset *set, cpuset_t *mask) 338d7f687fcSJeff Roberson { 339d7f687fcSJeff Roberson struct cpuset *nset; 340d7f687fcSJeff Roberson 341d7f687fcSJeff Roberson mtx_assert(&cpuset_lock, MA_OWNED); 342d7f687fcSJeff Roberson CPU_AND(&set->cs_mask, mask); 343d7f687fcSJeff Roberson LIST_FOREACH(nset, &set->cs_children, cs_siblings) 344d7f687fcSJeff Roberson cpuset_update(nset, &set->cs_mask); 345d7f687fcSJeff Roberson 346d7f687fcSJeff Roberson return; 347d7f687fcSJeff Roberson } 348d7f687fcSJeff Roberson 349d7f687fcSJeff Roberson /* 350d7f687fcSJeff Roberson * Modify the set 'set' to use a copy of the mask provided. Apply this new 351d7f687fcSJeff Roberson * mask to restrict all children in the tree. Checks for validity before 352d7f687fcSJeff Roberson * applying the changes. 353d7f687fcSJeff Roberson */ 354d7f687fcSJeff Roberson static int 355d7f687fcSJeff Roberson cpuset_modify(struct cpuset *set, cpuset_t *mask) 356d7f687fcSJeff Roberson { 35773c40187SJeff Roberson struct cpuset *root; 358d7f687fcSJeff Roberson int error; 359d7f687fcSJeff Roberson 360ba931c08SBjoern A. Zeeb error = priv_check(curthread, PRIV_SCHED_CPUSET); 361d7f687fcSJeff Roberson if (error) 362d7f687fcSJeff Roberson return (error); 36373c40187SJeff Roberson /* 3646aaa0b3cSBjoern A. Zeeb * In case we are called from within the jail 3656aaa0b3cSBjoern A. Zeeb * we do not allow modifying the dedicated root 3666aaa0b3cSBjoern A. Zeeb * cpuset of the jail but may still allow to 3676aaa0b3cSBjoern A. Zeeb * change child sets. 3686aaa0b3cSBjoern A. Zeeb */ 3696aaa0b3cSBjoern A. Zeeb if (jailed(curthread->td_ucred) && 3706aaa0b3cSBjoern A. Zeeb set->cs_flags & CPU_SET_ROOT) 3716aaa0b3cSBjoern A. Zeeb return (EPERM); 3726aaa0b3cSBjoern A. Zeeb /* 37373c40187SJeff Roberson * Verify that we have access to this set of 37473c40187SJeff Roberson * cpus. 37573c40187SJeff Roberson */ 37673c40187SJeff Roberson root = set->cs_parent; 37773c40187SJeff Roberson if (root && !CPU_SUBSET(&root->cs_mask, mask)) 37873c40187SJeff Roberson return (EINVAL); 379d7f687fcSJeff Roberson mtx_lock_spin(&cpuset_lock); 380c9813d0aSJohn Baldwin error = cpuset_testupdate(set, mask, 0); 381d7f687fcSJeff Roberson if (error) 382d7f687fcSJeff Roberson goto out; 383d7f687fcSJeff Roberson CPU_COPY(mask, &set->cs_mask); 384c9813d0aSJohn Baldwin cpuset_update(set, mask); 385d7f687fcSJeff Roberson out: 386d7f687fcSJeff Roberson mtx_unlock_spin(&cpuset_lock); 387d7f687fcSJeff Roberson 388d7f687fcSJeff Roberson return (error); 389d7f687fcSJeff Roberson } 390d7f687fcSJeff Roberson 391d7f687fcSJeff Roberson /* 392d7f687fcSJeff Roberson * Resolve the 'which' parameter of several cpuset apis. 393d7f687fcSJeff Roberson * 394d7f687fcSJeff Roberson * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 395d7f687fcSJeff Roberson * checks for permission via p_cansched(). 396d7f687fcSJeff Roberson * 397d7f687fcSJeff Roberson * For WHICH_SET returns a valid set with a new reference. 398d7f687fcSJeff Roberson * 399d7f687fcSJeff Roberson * -1 may be supplied for any argument to mean the current proc/thread or 400d7f687fcSJeff Roberson * the base set of the current thread. May fail with ESRCH/EPERM. 401d7f687fcSJeff Roberson */ 4025bbb2169SAdrian Chadd int 403d7f687fcSJeff Roberson cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 404d7f687fcSJeff Roberson struct cpuset **setp) 405d7f687fcSJeff Roberson { 406d7f687fcSJeff Roberson struct cpuset *set; 407d7f687fcSJeff Roberson struct thread *td; 408d7f687fcSJeff Roberson struct proc *p; 409d7f687fcSJeff Roberson int error; 410d7f687fcSJeff Roberson 411d7f687fcSJeff Roberson *pp = p = NULL; 412d7f687fcSJeff Roberson *tdp = td = NULL; 413d7f687fcSJeff Roberson *setp = set = NULL; 414d7f687fcSJeff Roberson switch (which) { 415d7f687fcSJeff Roberson case CPU_WHICH_PID: 416d7f687fcSJeff Roberson if (id == -1) { 417d7f687fcSJeff Roberson PROC_LOCK(curproc); 418d7f687fcSJeff Roberson p = curproc; 419d7f687fcSJeff Roberson break; 420d7f687fcSJeff Roberson } 421d7f687fcSJeff Roberson if ((p = pfind(id)) == NULL) 422d7f687fcSJeff Roberson return (ESRCH); 423d7f687fcSJeff Roberson break; 424d7f687fcSJeff Roberson case CPU_WHICH_TID: 425d7f687fcSJeff Roberson if (id == -1) { 426d7f687fcSJeff Roberson PROC_LOCK(curproc); 427d7f687fcSJeff Roberson p = curproc; 428d7f687fcSJeff Roberson td = curthread; 429d7f687fcSJeff Roberson break; 430d7f687fcSJeff Roberson } 43142fe684cSDavid Xu td = tdfind(id, -1); 432d7f687fcSJeff Roberson if (td == NULL) 433d7f687fcSJeff Roberson return (ESRCH); 43442fe684cSDavid Xu p = td->td_proc; 435d7f687fcSJeff Roberson break; 436d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 437d7f687fcSJeff Roberson if (id == -1) { 438d7f687fcSJeff Roberson thread_lock(curthread); 439a03ee000SJeff Roberson set = cpuset_refbase(curthread->td_cpuset); 440d7f687fcSJeff Roberson thread_unlock(curthread); 441d7f687fcSJeff Roberson } else 442413628a7SBjoern A. Zeeb set = cpuset_lookup(id, curthread); 443d7f687fcSJeff Roberson if (set) { 444d7f687fcSJeff Roberson *setp = set; 445d7f687fcSJeff Roberson return (0); 446d7f687fcSJeff Roberson } 447d7f687fcSJeff Roberson return (ESRCH); 448413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 449413628a7SBjoern A. Zeeb { 450413628a7SBjoern A. Zeeb /* Find `set' for prison with given id. */ 451413628a7SBjoern A. Zeeb struct prison *pr; 452413628a7SBjoern A. Zeeb 453413628a7SBjoern A. Zeeb sx_slock(&allprison_lock); 4540304c731SJamie Gritton pr = prison_find_child(curthread->td_ucred->cr_prison, id); 455413628a7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 456413628a7SBjoern A. Zeeb if (pr == NULL) 457413628a7SBjoern A. Zeeb return (ESRCH); 458413628a7SBjoern A. Zeeb cpuset_ref(pr->pr_cpuset); 4590304c731SJamie Gritton *setp = pr->pr_cpuset; 460413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 461413628a7SBjoern A. Zeeb return (0); 462413628a7SBjoern A. Zeeb } 4639b33b154SJeff Roberson case CPU_WHICH_IRQ: 464c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 4659b33b154SJeff Roberson return (0); 466d7f687fcSJeff Roberson default: 467d7f687fcSJeff Roberson return (EINVAL); 468d7f687fcSJeff Roberson } 469d7f687fcSJeff Roberson error = p_cansched(curthread, p); 470d7f687fcSJeff Roberson if (error) { 471d7f687fcSJeff Roberson PROC_UNLOCK(p); 472d7f687fcSJeff Roberson return (error); 473d7f687fcSJeff Roberson } 474d7f687fcSJeff Roberson if (td == NULL) 475d7f687fcSJeff Roberson td = FIRST_THREAD_IN_PROC(p); 476d7f687fcSJeff Roberson *pp = p; 477d7f687fcSJeff Roberson *tdp = td; 478d7f687fcSJeff Roberson return (0); 479d7f687fcSJeff Roberson } 480d7f687fcSJeff Roberson 481d7f687fcSJeff Roberson /* 482d7f687fcSJeff Roberson * Create an anonymous set with the provided mask in the space provided by 483d7f687fcSJeff Roberson * 'fset'. If the passed in set is anonymous we use its parent otherwise 484d7f687fcSJeff Roberson * the new set is a child of 'set'. 485d7f687fcSJeff Roberson */ 486d7f687fcSJeff Roberson static int 487e84c2db1SJohn Baldwin cpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask) 488d7f687fcSJeff Roberson { 489d7f687fcSJeff Roberson struct cpuset *parent; 490d7f687fcSJeff Roberson 491d7f687fcSJeff Roberson if (set->cs_id == CPUSET_INVALID) 492d7f687fcSJeff Roberson parent = set->cs_parent; 493d7f687fcSJeff Roberson else 494d7f687fcSJeff Roberson parent = set; 49573c40187SJeff Roberson if (!CPU_SUBSET(&parent->cs_mask, mask)) 496a03ee000SJeff Roberson return (EDEADLK); 497d7f687fcSJeff Roberson return (_cpuset_create(fset, parent, mask, CPUSET_INVALID)); 498d7f687fcSJeff Roberson } 499d7f687fcSJeff Roberson 500d7f687fcSJeff Roberson /* 501d7f687fcSJeff Roberson * Handle two cases for replacing the base set or mask of an entire process. 502d7f687fcSJeff Roberson * 503d7f687fcSJeff Roberson * 1) Set is non-null and mask is null. This reparents all anonymous sets 504d7f687fcSJeff Roberson * to the provided set and replaces all non-anonymous td_cpusets with the 505d7f687fcSJeff Roberson * provided set. 506d7f687fcSJeff Roberson * 2) Mask is non-null and set is null. This replaces or creates anonymous 507d7f687fcSJeff Roberson * sets for every thread with the existing base as a parent. 508d7f687fcSJeff Roberson * 509d7f687fcSJeff Roberson * This is overly complicated because we can't allocate while holding a 510d7f687fcSJeff Roberson * spinlock and spinlocks must be held while changing and examining thread 511d7f687fcSJeff Roberson * state. 512d7f687fcSJeff Roberson */ 513d7f687fcSJeff Roberson static int 514d7f687fcSJeff Roberson cpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask) 515d7f687fcSJeff Roberson { 516d7f687fcSJeff Roberson struct setlist freelist; 517d7f687fcSJeff Roberson struct setlist droplist; 51873c40187SJeff Roberson struct cpuset *tdset; 519d7f687fcSJeff Roberson struct cpuset *nset; 520d7f687fcSJeff Roberson struct thread *td; 521d7f687fcSJeff Roberson struct proc *p; 522d7f687fcSJeff Roberson int threads; 523d7f687fcSJeff Roberson int nfree; 524d7f687fcSJeff Roberson int error; 525d7f687fcSJeff Roberson /* 526d7f687fcSJeff Roberson * The algorithm requires two passes due to locking considerations. 527d7f687fcSJeff Roberson * 528d7f687fcSJeff Roberson * 1) Lookup the process and acquire the locks in the required order. 529d7f687fcSJeff Roberson * 2) If enough cpusets have not been allocated release the locks and 530d7f687fcSJeff Roberson * allocate them. Loop. 531d7f687fcSJeff Roberson */ 532d7f687fcSJeff Roberson LIST_INIT(&freelist); 533d7f687fcSJeff Roberson LIST_INIT(&droplist); 534d7f687fcSJeff Roberson nfree = 0; 535d7f687fcSJeff Roberson for (;;) { 536d7f687fcSJeff Roberson error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 537d7f687fcSJeff Roberson if (error) 538d7f687fcSJeff Roberson goto out; 539d7f687fcSJeff Roberson if (nfree >= p->p_numthreads) 540d7f687fcSJeff Roberson break; 541d7f687fcSJeff Roberson threads = p->p_numthreads; 542d7f687fcSJeff Roberson PROC_UNLOCK(p); 543d7f687fcSJeff Roberson for (; nfree < threads; nfree++) { 544d7f687fcSJeff Roberson nset = uma_zalloc(cpuset_zone, M_WAITOK); 545d7f687fcSJeff Roberson LIST_INSERT_HEAD(&freelist, nset, cs_link); 546d7f687fcSJeff Roberson } 547d7f687fcSJeff Roberson } 548d7f687fcSJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 549d7f687fcSJeff Roberson /* 550d7f687fcSJeff Roberson * Now that the appropriate locks are held and we have enough cpusets, 55173c40187SJeff Roberson * make sure the operation will succeed before applying changes. The 55273c40187SJeff Roberson * proc lock prevents td_cpuset from changing between calls. 553d7f687fcSJeff Roberson */ 554d7f687fcSJeff Roberson error = 0; 555d7f687fcSJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 55673c40187SJeff Roberson thread_lock(td); 55773c40187SJeff Roberson tdset = td->td_cpuset; 55873c40187SJeff Roberson /* 55973c40187SJeff Roberson * Verify that a new mask doesn't specify cpus outside of 56073c40187SJeff Roberson * the set the thread is a member of. 56173c40187SJeff Roberson */ 56273c40187SJeff Roberson if (mask) { 56373c40187SJeff Roberson if (tdset->cs_id == CPUSET_INVALID) 56473c40187SJeff Roberson tdset = tdset->cs_parent; 56573c40187SJeff Roberson if (!CPU_SUBSET(&tdset->cs_mask, mask)) 566a03ee000SJeff Roberson error = EDEADLK; 56773c40187SJeff Roberson /* 56873c40187SJeff Roberson * Verify that a new set won't leave an existing thread 56973c40187SJeff Roberson * mask without a cpu to run on. It can, however, restrict 57073c40187SJeff Roberson * the set. 57173c40187SJeff Roberson */ 57273c40187SJeff Roberson } else if (tdset->cs_id == CPUSET_INVALID) { 57373c40187SJeff Roberson if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask)) 574a03ee000SJeff Roberson error = EDEADLK; 57573c40187SJeff Roberson } 57673c40187SJeff Roberson thread_unlock(td); 57773c40187SJeff Roberson if (error) 57873c40187SJeff Roberson goto unlock_out; 57973c40187SJeff Roberson } 58073c40187SJeff Roberson /* 58173c40187SJeff Roberson * Replace each thread's cpuset while using deferred release. We 582374ae2a3SJeff Roberson * must do this because the thread lock must be held while operating 583374ae2a3SJeff Roberson * on the thread and this limits the type of operations allowed. 58473c40187SJeff Roberson */ 58573c40187SJeff Roberson FOREACH_THREAD_IN_PROC(p, td) { 586d7f687fcSJeff Roberson thread_lock(td); 587d7f687fcSJeff Roberson /* 588d7f687fcSJeff Roberson * If we presently have an anonymous set or are applying a 589d7f687fcSJeff Roberson * mask we must create an anonymous shadow set. That is 590d7f687fcSJeff Roberson * either parented to our existing base or the supplied set. 591d7f687fcSJeff Roberson * 592d7f687fcSJeff Roberson * If we have a base set with no anonymous shadow we simply 593d7f687fcSJeff Roberson * replace it outright. 594d7f687fcSJeff Roberson */ 595d7f687fcSJeff Roberson tdset = td->td_cpuset; 596d7f687fcSJeff Roberson if (tdset->cs_id == CPUSET_INVALID || mask) { 597d7f687fcSJeff Roberson nset = LIST_FIRST(&freelist); 598d7f687fcSJeff Roberson LIST_REMOVE(nset, cs_link); 599d7f687fcSJeff Roberson if (mask) 600d7f687fcSJeff Roberson error = cpuset_shadow(tdset, nset, mask); 601d7f687fcSJeff Roberson else 602d7f687fcSJeff Roberson error = _cpuset_create(nset, set, 603d7f687fcSJeff Roberson &tdset->cs_mask, CPUSET_INVALID); 604d7f687fcSJeff Roberson if (error) { 605d7f687fcSJeff Roberson LIST_INSERT_HEAD(&freelist, nset, cs_link); 606d7f687fcSJeff Roberson thread_unlock(td); 607d7f687fcSJeff Roberson break; 608d7f687fcSJeff Roberson } 609d7f687fcSJeff Roberson } else 610d7f687fcSJeff Roberson nset = cpuset_ref(set); 611d7f687fcSJeff Roberson cpuset_rel_defer(&droplist, tdset); 612d7f687fcSJeff Roberson td->td_cpuset = nset; 613d7f687fcSJeff Roberson sched_affinity(td); 614d7f687fcSJeff Roberson thread_unlock(td); 615d7f687fcSJeff Roberson } 61673c40187SJeff Roberson unlock_out: 617d7f687fcSJeff Roberson PROC_UNLOCK(p); 618d7f687fcSJeff Roberson out: 619d7f687fcSJeff Roberson while ((nset = LIST_FIRST(&droplist)) != NULL) 620d7f687fcSJeff Roberson cpuset_rel_complete(nset); 621d7f687fcSJeff Roberson while ((nset = LIST_FIRST(&freelist)) != NULL) { 622d7f687fcSJeff Roberson LIST_REMOVE(nset, cs_link); 623d7f687fcSJeff Roberson uma_zfree(cpuset_zone, nset); 624d7f687fcSJeff Roberson } 625d7f687fcSJeff Roberson return (error); 626d7f687fcSJeff Roberson } 627d7f687fcSJeff Roberson 628d7f687fcSJeff Roberson /* 62971a19bdcSAttilio Rao * Return a string representing a valid layout for a cpuset_t object. 63071a19bdcSAttilio Rao * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 63171a19bdcSAttilio Rao */ 63271a19bdcSAttilio Rao char * 63371a19bdcSAttilio Rao cpusetobj_strprint(char *buf, const cpuset_t *set) 63471a19bdcSAttilio Rao { 63571a19bdcSAttilio Rao char *tbuf; 63671a19bdcSAttilio Rao size_t i, bytesp, bufsiz; 63771a19bdcSAttilio Rao 63871a19bdcSAttilio Rao tbuf = buf; 63971a19bdcSAttilio Rao bytesp = 0; 64071a19bdcSAttilio Rao bufsiz = CPUSETBUFSIZ; 64171a19bdcSAttilio Rao 642d4a2ab8cSAttilio Rao for (i = 0; i < (_NCPUWORDS - 1); i++) { 64371a19bdcSAttilio Rao bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]); 64471a19bdcSAttilio Rao bufsiz -= bytesp; 64571a19bdcSAttilio Rao tbuf += bytesp; 64671a19bdcSAttilio Rao } 647d4a2ab8cSAttilio Rao snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]); 64871a19bdcSAttilio Rao return (buf); 64971a19bdcSAttilio Rao } 65071a19bdcSAttilio Rao 65171a19bdcSAttilio Rao /* 652e3709597SAttilio Rao * Build a valid cpuset_t object from a string representation. 653e3709597SAttilio Rao * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 654e3709597SAttilio Rao */ 655e3709597SAttilio Rao int 656e3709597SAttilio Rao cpusetobj_strscan(cpuset_t *set, const char *buf) 657e3709597SAttilio Rao { 658e3709597SAttilio Rao u_int nwords; 659e3709597SAttilio Rao int i, ret; 660e3709597SAttilio Rao 661e3709597SAttilio Rao if (strlen(buf) > CPUSETBUFSIZ - 1) 662e3709597SAttilio Rao return (-1); 663e3709597SAttilio Rao 664e3709597SAttilio Rao /* Allow to pass a shorter version of the mask when necessary. */ 665e3709597SAttilio Rao nwords = 1; 666e3709597SAttilio Rao for (i = 0; buf[i] != '\0'; i++) 667e3709597SAttilio Rao if (buf[i] == ',') 668e3709597SAttilio Rao nwords++; 669e3709597SAttilio Rao if (nwords > _NCPUWORDS) 670e3709597SAttilio Rao return (-1); 671e3709597SAttilio Rao 672e3709597SAttilio Rao CPU_ZERO(set); 673d4a2ab8cSAttilio Rao for (i = 0; i < (nwords - 1); i++) { 674e3709597SAttilio Rao ret = sscanf(buf, "%lx,", &set->__bits[i]); 675e3709597SAttilio Rao if (ret == 0 || ret == -1) 676e3709597SAttilio Rao return (-1); 677d4a2ab8cSAttilio Rao buf = strstr(buf, ","); 678e3709597SAttilio Rao if (buf == NULL) 679e3709597SAttilio Rao return (-1); 680e3709597SAttilio Rao buf++; 681e3709597SAttilio Rao } 682d4a2ab8cSAttilio Rao ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]); 683e3709597SAttilio Rao if (ret == 0 || ret == -1) 684e3709597SAttilio Rao return (-1); 685e3709597SAttilio Rao return (0); 686e3709597SAttilio Rao } 687e3709597SAttilio Rao 688e3709597SAttilio Rao /* 689d7f687fcSJeff Roberson * Apply an anonymous mask to a single thread. 690d7f687fcSJeff Roberson */ 691a03ee000SJeff Roberson int 692d7f687fcSJeff Roberson cpuset_setthread(lwpid_t id, cpuset_t *mask) 693d7f687fcSJeff Roberson { 694d7f687fcSJeff Roberson struct cpuset *nset; 695d7f687fcSJeff Roberson struct cpuset *set; 696d7f687fcSJeff Roberson struct thread *td; 697d7f687fcSJeff Roberson struct proc *p; 698d7f687fcSJeff Roberson int error; 699d7f687fcSJeff Roberson 700d7f687fcSJeff Roberson nset = uma_zalloc(cpuset_zone, M_WAITOK); 7018bd75bddSJeff Roberson error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 702d7f687fcSJeff Roberson if (error) 703d7f687fcSJeff Roberson goto out; 704a03ee000SJeff Roberson set = NULL; 705d7f687fcSJeff Roberson thread_lock(td); 706a03ee000SJeff Roberson error = cpuset_shadow(td->td_cpuset, nset, mask); 707d7f687fcSJeff Roberson if (error == 0) { 708a03ee000SJeff Roberson set = td->td_cpuset; 709d7f687fcSJeff Roberson td->td_cpuset = nset; 710d7f687fcSJeff Roberson sched_affinity(td); 711d7f687fcSJeff Roberson nset = NULL; 712d7f687fcSJeff Roberson } 713d7f687fcSJeff Roberson thread_unlock(td); 714d7f687fcSJeff Roberson PROC_UNLOCK(p); 715a03ee000SJeff Roberson if (set) 716a03ee000SJeff Roberson cpuset_rel(set); 717d7f687fcSJeff Roberson out: 718d7f687fcSJeff Roberson if (nset) 719d7f687fcSJeff Roberson uma_zfree(cpuset_zone, nset); 720d7f687fcSJeff Roberson return (error); 721d7f687fcSJeff Roberson } 722d7f687fcSJeff Roberson 723d7f687fcSJeff Roberson /* 72481198539SAlexander V. Chernikov * Apply new cpumask to the ithread. 72581198539SAlexander V. Chernikov */ 72681198539SAlexander V. Chernikov int 7277f7528fcSAdrian Chadd cpuset_setithread(lwpid_t id, int cpu) 72881198539SAlexander V. Chernikov { 72981198539SAlexander V. Chernikov struct cpuset *nset, *rset; 73081198539SAlexander V. Chernikov struct cpuset *parent, *old_set; 73181198539SAlexander V. Chernikov struct thread *td; 73281198539SAlexander V. Chernikov struct proc *p; 73381198539SAlexander V. Chernikov cpusetid_t cs_id; 73481198539SAlexander V. Chernikov cpuset_t mask; 73581198539SAlexander V. Chernikov int error; 73681198539SAlexander V. Chernikov 73781198539SAlexander V. Chernikov nset = uma_zalloc(cpuset_zone, M_WAITOK); 73881198539SAlexander V. Chernikov rset = uma_zalloc(cpuset_zone, M_WAITOK); 739c1d9ecf2SAlexander V. Chernikov cs_id = CPUSET_INVALID; 74081198539SAlexander V. Chernikov 74181198539SAlexander V. Chernikov CPU_ZERO(&mask); 74281198539SAlexander V. Chernikov if (cpu == NOCPU) 74381198539SAlexander V. Chernikov CPU_COPY(cpuset_root, &mask); 74481198539SAlexander V. Chernikov else 74581198539SAlexander V. Chernikov CPU_SET(cpu, &mask); 74681198539SAlexander V. Chernikov 74781198539SAlexander V. Chernikov error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &old_set); 748c1d9ecf2SAlexander V. Chernikov if (error != 0 || ((cs_id = alloc_unr(cpuset_unr)) == CPUSET_INVALID)) 74981198539SAlexander V. Chernikov goto out; 75081198539SAlexander V. Chernikov 751c1d9ecf2SAlexander V. Chernikov /* cpuset_which() returns with PROC_LOCK held. */ 75281198539SAlexander V. Chernikov old_set = td->td_cpuset; 75381198539SAlexander V. Chernikov 75481198539SAlexander V. Chernikov if (cpu == NOCPU) { 755c1d9ecf2SAlexander V. Chernikov 75681198539SAlexander V. Chernikov /* 75781198539SAlexander V. Chernikov * roll back to default set. We're not using cpuset_shadow() 75881198539SAlexander V. Chernikov * here because we can fail CPU_SUBSET() check. This can happen 75981198539SAlexander V. Chernikov * if default set does not contain all CPUs. 76081198539SAlexander V. Chernikov */ 76181198539SAlexander V. Chernikov error = _cpuset_create(nset, cpuset_default, &mask, 76281198539SAlexander V. Chernikov CPUSET_INVALID); 76381198539SAlexander V. Chernikov 76481198539SAlexander V. Chernikov goto applyset; 76581198539SAlexander V. Chernikov } 76681198539SAlexander V. Chernikov 76781198539SAlexander V. Chernikov if (old_set->cs_id == 1 || (old_set->cs_id == CPUSET_INVALID && 76881198539SAlexander V. Chernikov old_set->cs_parent->cs_id == 1)) { 769c1d9ecf2SAlexander V. Chernikov 770c1d9ecf2SAlexander V. Chernikov /* 771c1d9ecf2SAlexander V. Chernikov * Current set is either default (1) or 772c1d9ecf2SAlexander V. Chernikov * shadowed version of default set. 773c1d9ecf2SAlexander V. Chernikov * 774c1d9ecf2SAlexander V. Chernikov * Allocate new root set to be able to shadow it 775c1d9ecf2SAlexander V. Chernikov * with any mask. 776c1d9ecf2SAlexander V. Chernikov */ 77781198539SAlexander V. Chernikov error = _cpuset_create(rset, cpuset_zero, 77881198539SAlexander V. Chernikov &cpuset_zero->cs_mask, cs_id); 77981198539SAlexander V. Chernikov if (error != 0) { 78081198539SAlexander V. Chernikov PROC_UNLOCK(p); 78181198539SAlexander V. Chernikov goto out; 78281198539SAlexander V. Chernikov } 78381198539SAlexander V. Chernikov rset->cs_flags |= CPU_SET_ROOT; 78481198539SAlexander V. Chernikov parent = rset; 78581198539SAlexander V. Chernikov rset = NULL; 78681198539SAlexander V. Chernikov cs_id = CPUSET_INVALID; 78781198539SAlexander V. Chernikov } else { 78881198539SAlexander V. Chernikov /* Assume existing set was already allocated by previous call */ 789c1d9ecf2SAlexander V. Chernikov parent = old_set; 79081198539SAlexander V. Chernikov old_set = NULL; 79181198539SAlexander V. Chernikov } 79281198539SAlexander V. Chernikov 79381198539SAlexander V. Chernikov error = cpuset_shadow(parent, nset, &mask); 79481198539SAlexander V. Chernikov applyset: 79581198539SAlexander V. Chernikov if (error == 0) { 796c1d9ecf2SAlexander V. Chernikov thread_lock(td); 79781198539SAlexander V. Chernikov td->td_cpuset = nset; 79881198539SAlexander V. Chernikov sched_affinity(td); 79981198539SAlexander V. Chernikov thread_unlock(td); 800c1d9ecf2SAlexander V. Chernikov nset = NULL; 801c1d9ecf2SAlexander V. Chernikov } else 802c1d9ecf2SAlexander V. Chernikov old_set = NULL; 80381198539SAlexander V. Chernikov PROC_UNLOCK(p); 80481198539SAlexander V. Chernikov if (old_set != NULL) 80581198539SAlexander V. Chernikov cpuset_rel(old_set); 80681198539SAlexander V. Chernikov out: 80781198539SAlexander V. Chernikov if (nset != NULL) 80881198539SAlexander V. Chernikov uma_zfree(cpuset_zone, nset); 80981198539SAlexander V. Chernikov if (rset != NULL) 81081198539SAlexander V. Chernikov uma_zfree(cpuset_zone, rset); 81181198539SAlexander V. Chernikov if (cs_id != CPUSET_INVALID) 81281198539SAlexander V. Chernikov free_unr(cpuset_unr, cs_id); 81381198539SAlexander V. Chernikov return (error); 81481198539SAlexander V. Chernikov } 81581198539SAlexander V. Chernikov 81681198539SAlexander V. Chernikov 81781198539SAlexander V. Chernikov /* 818c0ae6688SJohn Baldwin * Creates system-wide cpusets and the cpuset for thread0 including two 819c0ae6688SJohn Baldwin * sets: 820d7f687fcSJeff Roberson * 821d7f687fcSJeff Roberson * 0 - The root set which should represent all valid processors in the 822d7f687fcSJeff Roberson * system. It is initially created with a mask of all processors 823d7f687fcSJeff Roberson * because we don't know what processors are valid until cpuset_init() 824d7f687fcSJeff Roberson * runs. This set is immutable. 825d7f687fcSJeff Roberson * 1 - The default set which all processes are a member of until changed. 826d7f687fcSJeff Roberson * This allows an administrator to move all threads off of given cpus to 827d7f687fcSJeff Roberson * dedicate them to high priority tasks or save power etc. 828d7f687fcSJeff Roberson */ 829d7f687fcSJeff Roberson struct cpuset * 830d7f687fcSJeff Roberson cpuset_thread0(void) 831d7f687fcSJeff Roberson { 832d7f687fcSJeff Roberson struct cpuset *set; 83362d70a81SJohn Baldwin int error, i; 834d7f687fcSJeff Roberson 835d7f687fcSJeff Roberson cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 836d7f687fcSJeff Roberson NULL, NULL, UMA_ALIGN_PTR, 0); 837d7f687fcSJeff Roberson mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 83881198539SAlexander V. Chernikov 839d7f687fcSJeff Roberson /* 840d7f687fcSJeff Roberson * Create the root system set for the whole machine. Doesn't use 841d7f687fcSJeff Roberson * cpuset_create() due to NULL parent. 842d7f687fcSJeff Roberson */ 843d7f687fcSJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 844f27aed53SAttilio Rao CPU_FILL(&set->cs_mask); 845d7f687fcSJeff Roberson LIST_INIT(&set->cs_children); 846d7f687fcSJeff Roberson LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 847d7f687fcSJeff Roberson set->cs_ref = 1; 848d7f687fcSJeff Roberson set->cs_flags = CPU_SET_ROOT; 849d7f687fcSJeff Roberson cpuset_zero = set; 850a03ee000SJeff Roberson cpuset_root = &set->cs_mask; 85181198539SAlexander V. Chernikov 852d7f687fcSJeff Roberson /* 853d7f687fcSJeff Roberson * Now derive a default, modifiable set from that to give out. 854d7f687fcSJeff Roberson */ 855d7f687fcSJeff Roberson set = uma_zalloc(cpuset_zone, M_WAITOK); 856d7f687fcSJeff Roberson error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1); 857d7f687fcSJeff Roberson KASSERT(error == 0, ("Error creating default set: %d\n", error)); 85881198539SAlexander V. Chernikov cpuset_default = set; 85981198539SAlexander V. Chernikov 860d7f687fcSJeff Roberson /* 861d7f687fcSJeff Roberson * Initialize the unit allocator. 0 and 1 are allocated above. 862d7f687fcSJeff Roberson */ 863d7f687fcSJeff Roberson cpuset_unr = new_unrhdr(2, INT_MAX, NULL); 864d7f687fcSJeff Roberson 86562d70a81SJohn Baldwin /* 86662d70a81SJohn Baldwin * If MD code has not initialized per-domain cpusets, place all 86762d70a81SJohn Baldwin * CPUs in domain 0. 86862d70a81SJohn Baldwin */ 86962d70a81SJohn Baldwin for (i = 0; i < MAXMEMDOM; i++) 87062d70a81SJohn Baldwin if (!CPU_EMPTY(&cpuset_domain[i])) 87162d70a81SJohn Baldwin goto domains_set; 872c0ae6688SJohn Baldwin CPU_COPY(&all_cpus, &cpuset_domain[0]); 87362d70a81SJohn Baldwin domains_set: 874c0ae6688SJohn Baldwin 875d7f687fcSJeff Roberson return (set); 876d7f687fcSJeff Roberson } 877d7f687fcSJeff Roberson 878d7f687fcSJeff Roberson /* 879413628a7SBjoern A. Zeeb * Create a cpuset, which would be cpuset_create() but 880413628a7SBjoern A. Zeeb * mark the new 'set' as root. 881413628a7SBjoern A. Zeeb * 88247479a8cSBjoern A. Zeeb * We are not going to reparent the td to it. Use cpuset_setproc_update_set() 88347479a8cSBjoern A. Zeeb * for that. 884413628a7SBjoern A. Zeeb * 885413628a7SBjoern A. Zeeb * In case of no error, returns the set in *setp locked with a reference. 886413628a7SBjoern A. Zeeb */ 887413628a7SBjoern A. Zeeb int 8880304c731SJamie Gritton cpuset_create_root(struct prison *pr, struct cpuset **setp) 889413628a7SBjoern A. Zeeb { 890413628a7SBjoern A. Zeeb struct cpuset *set; 891413628a7SBjoern A. Zeeb int error; 892413628a7SBjoern A. Zeeb 8930304c731SJamie Gritton KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__)); 894413628a7SBjoern A. Zeeb KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 895413628a7SBjoern A. Zeeb 8960304c731SJamie Gritton error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask); 897413628a7SBjoern A. Zeeb if (error) 898413628a7SBjoern A. Zeeb return (error); 899413628a7SBjoern A. Zeeb 900413628a7SBjoern A. Zeeb KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data", 901413628a7SBjoern A. Zeeb __func__, __LINE__)); 902413628a7SBjoern A. Zeeb 903413628a7SBjoern A. Zeeb /* Mark the set as root. */ 904413628a7SBjoern A. Zeeb set = *setp; 905413628a7SBjoern A. Zeeb set->cs_flags |= CPU_SET_ROOT; 906413628a7SBjoern A. Zeeb 907413628a7SBjoern A. Zeeb return (0); 908413628a7SBjoern A. Zeeb } 909413628a7SBjoern A. Zeeb 910413628a7SBjoern A. Zeeb int 911413628a7SBjoern A. Zeeb cpuset_setproc_update_set(struct proc *p, struct cpuset *set) 912413628a7SBjoern A. Zeeb { 913413628a7SBjoern A. Zeeb int error; 914413628a7SBjoern A. Zeeb 915413628a7SBjoern A. Zeeb KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 916413628a7SBjoern A. Zeeb KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 917413628a7SBjoern A. Zeeb 918413628a7SBjoern A. Zeeb cpuset_ref(set); 919413628a7SBjoern A. Zeeb error = cpuset_setproc(p->p_pid, set, NULL); 920413628a7SBjoern A. Zeeb if (error) 921413628a7SBjoern A. Zeeb return (error); 922413628a7SBjoern A. Zeeb cpuset_rel(set); 923413628a7SBjoern A. Zeeb return (0); 924413628a7SBjoern A. Zeeb } 925413628a7SBjoern A. Zeeb 926413628a7SBjoern A. Zeeb /* 927d7f687fcSJeff Roberson * This is called once the final set of system cpus is known. Modifies 92893902625SJohn Baldwin * the root set and all children and mark the root read-only. 929d7f687fcSJeff Roberson */ 930d7f687fcSJeff Roberson static void 931d7f687fcSJeff Roberson cpuset_init(void *arg) 932d7f687fcSJeff Roberson { 933d7f687fcSJeff Roberson cpuset_t mask; 934d7f687fcSJeff Roberson 93571a19bdcSAttilio Rao mask = all_cpus; 936d7f687fcSJeff Roberson if (cpuset_modify(cpuset_zero, &mask)) 937d7f687fcSJeff Roberson panic("Can't set initial cpuset mask.\n"); 938d7f687fcSJeff Roberson cpuset_zero->cs_flags |= CPU_SET_RDONLY; 939d7f687fcSJeff Roberson } 940d7f687fcSJeff Roberson SYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL); 941d7f687fcSJeff Roberson 942d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 943d7f687fcSJeff Roberson struct cpuset_args { 944d7f687fcSJeff Roberson cpusetid_t *setid; 945d7f687fcSJeff Roberson }; 946d7f687fcSJeff Roberson #endif 947d7f687fcSJeff Roberson int 9488451d0ddSKip Macy sys_cpuset(struct thread *td, struct cpuset_args *uap) 949d7f687fcSJeff Roberson { 950d7f687fcSJeff Roberson struct cpuset *root; 951d7f687fcSJeff Roberson struct cpuset *set; 952d7f687fcSJeff Roberson int error; 953d7f687fcSJeff Roberson 954d7f687fcSJeff Roberson thread_lock(td); 955a03ee000SJeff Roberson root = cpuset_refroot(td->td_cpuset); 956d7f687fcSJeff Roberson thread_unlock(td); 957d7f687fcSJeff Roberson error = cpuset_create(&set, root, &root->cs_mask); 958d7f687fcSJeff Roberson cpuset_rel(root); 959d7f687fcSJeff Roberson if (error) 960d7f687fcSJeff Roberson return (error); 961d7f687fcSJeff Roberson error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 962a03ee000SJeff Roberson if (error == 0) 963a03ee000SJeff Roberson error = cpuset_setproc(-1, set, NULL); 964d7f687fcSJeff Roberson cpuset_rel(set); 965d7f687fcSJeff Roberson return (error); 966d7f687fcSJeff Roberson } 967d7f687fcSJeff Roberson 968d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 969d7f687fcSJeff Roberson struct cpuset_setid_args { 970d7f687fcSJeff Roberson cpuwhich_t which; 971d7f687fcSJeff Roberson id_t id; 972d7f687fcSJeff Roberson cpusetid_t setid; 973d7f687fcSJeff Roberson }; 974d7f687fcSJeff Roberson #endif 975d7f687fcSJeff Roberson int 9768451d0ddSKip Macy sys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 977d7f687fcSJeff Roberson { 978ea2ebdc1SEdward Tomasz Napierala 979ea2ebdc1SEdward Tomasz Napierala return (kern_cpuset_setid(td, uap->which, uap->id, uap->setid)); 980ea2ebdc1SEdward Tomasz Napierala } 981ea2ebdc1SEdward Tomasz Napierala 982ea2ebdc1SEdward Tomasz Napierala int 983ea2ebdc1SEdward Tomasz Napierala kern_cpuset_setid(struct thread *td, cpuwhich_t which, 984ea2ebdc1SEdward Tomasz Napierala id_t id, cpusetid_t setid) 985ea2ebdc1SEdward Tomasz Napierala { 986d7f687fcSJeff Roberson struct cpuset *set; 987d7f687fcSJeff Roberson int error; 988d7f687fcSJeff Roberson 989d7f687fcSJeff Roberson /* 990d7f687fcSJeff Roberson * Presently we only support per-process sets. 991d7f687fcSJeff Roberson */ 992ea2ebdc1SEdward Tomasz Napierala if (which != CPU_WHICH_PID) 993d7f687fcSJeff Roberson return (EINVAL); 994ea2ebdc1SEdward Tomasz Napierala set = cpuset_lookup(setid, td); 995d7f687fcSJeff Roberson if (set == NULL) 996d7f687fcSJeff Roberson return (ESRCH); 997ea2ebdc1SEdward Tomasz Napierala error = cpuset_setproc(id, set, NULL); 998d7f687fcSJeff Roberson cpuset_rel(set); 999d7f687fcSJeff Roberson return (error); 1000d7f687fcSJeff Roberson } 1001d7f687fcSJeff Roberson 1002d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1003d7f687fcSJeff Roberson struct cpuset_getid_args { 1004d7f687fcSJeff Roberson cpulevel_t level; 1005d7f687fcSJeff Roberson cpuwhich_t which; 1006d7f687fcSJeff Roberson id_t id; 1007d7f687fcSJeff Roberson cpusetid_t *setid; 10082b69bb1fSKevin Lo }; 1009d7f687fcSJeff Roberson #endif 1010d7f687fcSJeff Roberson int 10118451d0ddSKip Macy sys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 1012d7f687fcSJeff Roberson { 1013ea2ebdc1SEdward Tomasz Napierala 1014ea2ebdc1SEdward Tomasz Napierala return (kern_cpuset_getid(td, uap->level, uap->which, uap->id, 1015ea2ebdc1SEdward Tomasz Napierala uap->setid)); 1016ea2ebdc1SEdward Tomasz Napierala } 1017ea2ebdc1SEdward Tomasz Napierala 1018ea2ebdc1SEdward Tomasz Napierala int 1019ea2ebdc1SEdward Tomasz Napierala kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which, 1020ea2ebdc1SEdward Tomasz Napierala id_t id, cpusetid_t *setid) 1021ea2ebdc1SEdward Tomasz Napierala { 1022d7f687fcSJeff Roberson struct cpuset *nset; 1023d7f687fcSJeff Roberson struct cpuset *set; 1024d7f687fcSJeff Roberson struct thread *ttd; 1025d7f687fcSJeff Roberson struct proc *p; 1026ea2ebdc1SEdward Tomasz Napierala cpusetid_t tmpid; 1027d7f687fcSJeff Roberson int error; 1028d7f687fcSJeff Roberson 1029ea2ebdc1SEdward Tomasz Napierala if (level == CPU_LEVEL_WHICH && which != CPU_WHICH_CPUSET) 1030d7f687fcSJeff Roberson return (EINVAL); 1031ea2ebdc1SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 1032d7f687fcSJeff Roberson if (error) 1033d7f687fcSJeff Roberson return (error); 1034ea2ebdc1SEdward Tomasz Napierala switch (which) { 1035d7f687fcSJeff Roberson case CPU_WHICH_TID: 1036d7f687fcSJeff Roberson case CPU_WHICH_PID: 1037d7f687fcSJeff Roberson thread_lock(ttd); 1038a03ee000SJeff Roberson set = cpuset_refbase(ttd->td_cpuset); 1039d7f687fcSJeff Roberson thread_unlock(ttd); 1040d7f687fcSJeff Roberson PROC_UNLOCK(p); 1041d7f687fcSJeff Roberson break; 1042d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1043413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1044d7f687fcSJeff Roberson break; 10459b33b154SJeff Roberson case CPU_WHICH_IRQ: 1046c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 10479b33b154SJeff Roberson return (EINVAL); 1048d7f687fcSJeff Roberson } 1049ea2ebdc1SEdward Tomasz Napierala switch (level) { 1050d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 1051a03ee000SJeff Roberson nset = cpuset_refroot(set); 1052d7f687fcSJeff Roberson cpuset_rel(set); 1053d7f687fcSJeff Roberson set = nset; 1054d7f687fcSJeff Roberson break; 1055d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 1056d7f687fcSJeff Roberson break; 1057d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 1058d7f687fcSJeff Roberson break; 1059d7f687fcSJeff Roberson } 1060ea2ebdc1SEdward Tomasz Napierala tmpid = set->cs_id; 1061d7f687fcSJeff Roberson cpuset_rel(set); 1062d7f687fcSJeff Roberson if (error == 0) 1063ea2ebdc1SEdward Tomasz Napierala error = copyout(&tmpid, setid, sizeof(id)); 1064d7f687fcSJeff Roberson 1065d7f687fcSJeff Roberson return (error); 1066d7f687fcSJeff Roberson } 1067d7f687fcSJeff Roberson 1068d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1069d7f687fcSJeff Roberson struct cpuset_getaffinity_args { 1070d7f687fcSJeff Roberson cpulevel_t level; 1071d7f687fcSJeff Roberson cpuwhich_t which; 10727f64829aSRuslan Ermilov id_t id; 10737f64829aSRuslan Ermilov size_t cpusetsize; 10747f64829aSRuslan Ermilov cpuset_t *mask; 1075d7f687fcSJeff Roberson }; 1076d7f687fcSJeff Roberson #endif 1077d7f687fcSJeff Roberson int 10788451d0ddSKip Macy sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 1079d7f687fcSJeff Roberson { 108096ee4310SEdward Tomasz Napierala 108196ee4310SEdward Tomasz Napierala return (kern_cpuset_getaffinity(td, uap->level, uap->which, 108296ee4310SEdward Tomasz Napierala uap->id, uap->cpusetsize, uap->mask)); 108396ee4310SEdward Tomasz Napierala } 108496ee4310SEdward Tomasz Napierala 108596ee4310SEdward Tomasz Napierala int 108696ee4310SEdward Tomasz Napierala kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, 108796ee4310SEdward Tomasz Napierala id_t id, size_t cpusetsize, cpuset_t *maskp) 108896ee4310SEdward Tomasz Napierala { 1089d7f687fcSJeff Roberson struct thread *ttd; 1090d7f687fcSJeff Roberson struct cpuset *nset; 1091d7f687fcSJeff Roberson struct cpuset *set; 1092d7f687fcSJeff Roberson struct proc *p; 1093d7f687fcSJeff Roberson cpuset_t *mask; 1094d7f687fcSJeff Roberson int error; 10957f64829aSRuslan Ermilov size_t size; 1096d7f687fcSJeff Roberson 109796ee4310SEdward Tomasz Napierala if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY) 1098d7f687fcSJeff Roberson return (ERANGE); 109996ee4310SEdward Tomasz Napierala size = cpusetsize; 1100d7f687fcSJeff Roberson mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 110196ee4310SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 1102d7f687fcSJeff Roberson if (error) 1103d7f687fcSJeff Roberson goto out; 110496ee4310SEdward Tomasz Napierala switch (level) { 1105d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 1106d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 110796ee4310SEdward Tomasz Napierala switch (which) { 1108d7f687fcSJeff Roberson case CPU_WHICH_TID: 1109d7f687fcSJeff Roberson case CPU_WHICH_PID: 1110d7f687fcSJeff Roberson thread_lock(ttd); 1111d7f687fcSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 1112d7f687fcSJeff Roberson thread_unlock(ttd); 1113d7f687fcSJeff Roberson break; 1114d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1115413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1116d7f687fcSJeff Roberson break; 11179b33b154SJeff Roberson case CPU_WHICH_IRQ: 1118*29dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 1119*29dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 1120c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 11219b33b154SJeff Roberson error = EINVAL; 11229b33b154SJeff Roberson goto out; 1123d7f687fcSJeff Roberson } 112496ee4310SEdward Tomasz Napierala if (level == CPU_LEVEL_ROOT) 1125a03ee000SJeff Roberson nset = cpuset_refroot(set); 1126d7f687fcSJeff Roberson else 1127a03ee000SJeff Roberson nset = cpuset_refbase(set); 1128d7f687fcSJeff Roberson CPU_COPY(&nset->cs_mask, mask); 1129d7f687fcSJeff Roberson cpuset_rel(nset); 1130d7f687fcSJeff Roberson break; 1131d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 113296ee4310SEdward Tomasz Napierala switch (which) { 1133d7f687fcSJeff Roberson case CPU_WHICH_TID: 1134d7f687fcSJeff Roberson thread_lock(ttd); 1135d7f687fcSJeff Roberson CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 1136d7f687fcSJeff Roberson thread_unlock(ttd); 1137d7f687fcSJeff Roberson break; 1138d7f687fcSJeff Roberson case CPU_WHICH_PID: 1139d7f687fcSJeff Roberson FOREACH_THREAD_IN_PROC(p, ttd) { 1140d7f687fcSJeff Roberson thread_lock(ttd); 1141d7f687fcSJeff Roberson CPU_OR(mask, &ttd->td_cpuset->cs_mask); 1142d7f687fcSJeff Roberson thread_unlock(ttd); 1143d7f687fcSJeff Roberson } 1144d7f687fcSJeff Roberson break; 1145d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1146413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1147d7f687fcSJeff Roberson CPU_COPY(&set->cs_mask, mask); 1148d7f687fcSJeff Roberson break; 11499b33b154SJeff Roberson case CPU_WHICH_IRQ: 1150*29dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 1151*29dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 1152*29dfb631SConrad Meyer error = intr_getaffinity(id, which, mask); 11539b33b154SJeff Roberson break; 1154c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 115596ee4310SEdward Tomasz Napierala if (id < 0 || id >= MAXMEMDOM) 1156c0ae6688SJohn Baldwin error = ESRCH; 1157c0ae6688SJohn Baldwin else 115896ee4310SEdward Tomasz Napierala CPU_COPY(&cpuset_domain[id], mask); 1159c0ae6688SJohn Baldwin break; 1160d7f687fcSJeff Roberson } 1161d7f687fcSJeff Roberson break; 1162d7f687fcSJeff Roberson default: 1163d7f687fcSJeff Roberson error = EINVAL; 1164d7f687fcSJeff Roberson break; 1165d7f687fcSJeff Roberson } 1166d7f687fcSJeff Roberson if (set) 1167d7f687fcSJeff Roberson cpuset_rel(set); 1168d7f687fcSJeff Roberson if (p) 1169d7f687fcSJeff Roberson PROC_UNLOCK(p); 1170d7f687fcSJeff Roberson if (error == 0) 117196ee4310SEdward Tomasz Napierala error = copyout(mask, maskp, size); 1172d7f687fcSJeff Roberson out: 1173d7f687fcSJeff Roberson free(mask, M_TEMP); 1174d7f687fcSJeff Roberson return (error); 1175d7f687fcSJeff Roberson } 1176d7f687fcSJeff Roberson 1177d7f687fcSJeff Roberson #ifndef _SYS_SYSPROTO_H_ 1178d7f687fcSJeff Roberson struct cpuset_setaffinity_args { 1179d7f687fcSJeff Roberson cpulevel_t level; 1180d7f687fcSJeff Roberson cpuwhich_t which; 11817f64829aSRuslan Ermilov id_t id; 11827f64829aSRuslan Ermilov size_t cpusetsize; 11837f64829aSRuslan Ermilov const cpuset_t *mask; 1184d7f687fcSJeff Roberson }; 1185d7f687fcSJeff Roberson #endif 1186d7f687fcSJeff Roberson int 11878451d0ddSKip Macy sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 1188d7f687fcSJeff Roberson { 118996ee4310SEdward Tomasz Napierala 119096ee4310SEdward Tomasz Napierala return (kern_cpuset_setaffinity(td, uap->level, uap->which, 119196ee4310SEdward Tomasz Napierala uap->id, uap->cpusetsize, uap->mask)); 119296ee4310SEdward Tomasz Napierala } 119396ee4310SEdward Tomasz Napierala 119496ee4310SEdward Tomasz Napierala int 119596ee4310SEdward Tomasz Napierala kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, 119696ee4310SEdward Tomasz Napierala id_t id, size_t cpusetsize, const cpuset_t *maskp) 119796ee4310SEdward Tomasz Napierala { 1198d7f687fcSJeff Roberson struct cpuset *nset; 1199d7f687fcSJeff Roberson struct cpuset *set; 1200d7f687fcSJeff Roberson struct thread *ttd; 1201d7f687fcSJeff Roberson struct proc *p; 1202d7f687fcSJeff Roberson cpuset_t *mask; 1203d7f687fcSJeff Roberson int error; 1204d7f687fcSJeff Roberson 120596ee4310SEdward Tomasz Napierala if (cpusetsize < sizeof(cpuset_t) || cpusetsize > CPU_MAXSIZE / NBBY) 1206d7f687fcSJeff Roberson return (ERANGE); 120796ee4310SEdward Tomasz Napierala mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO); 120896ee4310SEdward Tomasz Napierala error = copyin(maskp, mask, cpusetsize); 1209d7f687fcSJeff Roberson if (error) 1210d7f687fcSJeff Roberson goto out; 121173c40187SJeff Roberson /* 121273c40187SJeff Roberson * Verify that no high bits are set. 121373c40187SJeff Roberson */ 121496ee4310SEdward Tomasz Napierala if (cpusetsize > sizeof(cpuset_t)) { 121573c40187SJeff Roberson char *end; 121673c40187SJeff Roberson char *cp; 121773c40187SJeff Roberson 121873c40187SJeff Roberson end = cp = (char *)&mask->__bits; 121996ee4310SEdward Tomasz Napierala end += cpusetsize; 122073c40187SJeff Roberson cp += sizeof(cpuset_t); 122173c40187SJeff Roberson while (cp != end) 122273c40187SJeff Roberson if (*cp++ != 0) { 122373c40187SJeff Roberson error = EINVAL; 122473c40187SJeff Roberson goto out; 122573c40187SJeff Roberson } 122673c40187SJeff Roberson 122773c40187SJeff Roberson } 122896ee4310SEdward Tomasz Napierala switch (level) { 1229d7f687fcSJeff Roberson case CPU_LEVEL_ROOT: 1230d7f687fcSJeff Roberson case CPU_LEVEL_CPUSET: 123196ee4310SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 1232d7f687fcSJeff Roberson if (error) 1233d7f687fcSJeff Roberson break; 123496ee4310SEdward Tomasz Napierala switch (which) { 1235d7f687fcSJeff Roberson case CPU_WHICH_TID: 1236d7f687fcSJeff Roberson case CPU_WHICH_PID: 1237d7f687fcSJeff Roberson thread_lock(ttd); 1238d7f687fcSJeff Roberson set = cpuset_ref(ttd->td_cpuset); 1239d7f687fcSJeff Roberson thread_unlock(ttd); 1240c6440f72SJeff Roberson PROC_UNLOCK(p); 1241d7f687fcSJeff Roberson break; 1242d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1243413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 1244d7f687fcSJeff Roberson break; 12459b33b154SJeff Roberson case CPU_WHICH_IRQ: 1246*29dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 1247*29dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 1248c0ae6688SJohn Baldwin case CPU_WHICH_DOMAIN: 12499b33b154SJeff Roberson error = EINVAL; 12509b33b154SJeff Roberson goto out; 1251d7f687fcSJeff Roberson } 125296ee4310SEdward Tomasz Napierala if (level == CPU_LEVEL_ROOT) 1253a03ee000SJeff Roberson nset = cpuset_refroot(set); 1254d7f687fcSJeff Roberson else 1255a03ee000SJeff Roberson nset = cpuset_refbase(set); 1256d7f687fcSJeff Roberson error = cpuset_modify(nset, mask); 1257d7f687fcSJeff Roberson cpuset_rel(nset); 1258d7f687fcSJeff Roberson cpuset_rel(set); 1259d7f687fcSJeff Roberson break; 1260d7f687fcSJeff Roberson case CPU_LEVEL_WHICH: 126196ee4310SEdward Tomasz Napierala switch (which) { 1262d7f687fcSJeff Roberson case CPU_WHICH_TID: 126396ee4310SEdward Tomasz Napierala error = cpuset_setthread(id, mask); 1264d7f687fcSJeff Roberson break; 1265d7f687fcSJeff Roberson case CPU_WHICH_PID: 126696ee4310SEdward Tomasz Napierala error = cpuset_setproc(id, NULL, mask); 1267d7f687fcSJeff Roberson break; 1268d7f687fcSJeff Roberson case CPU_WHICH_CPUSET: 1269413628a7SBjoern A. Zeeb case CPU_WHICH_JAIL: 127096ee4310SEdward Tomasz Napierala error = cpuset_which(which, id, &p, &ttd, &set); 1271d7f687fcSJeff Roberson if (error == 0) { 1272d7f687fcSJeff Roberson error = cpuset_modify(set, mask); 1273d7f687fcSJeff Roberson cpuset_rel(set); 1274d7f687fcSJeff Roberson } 1275d7f687fcSJeff Roberson break; 12769b33b154SJeff Roberson case CPU_WHICH_IRQ: 1277*29dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 1278*29dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 1279*29dfb631SConrad Meyer error = intr_setaffinity(id, which, mask); 12809b33b154SJeff Roberson break; 1281d7f687fcSJeff Roberson default: 1282d7f687fcSJeff Roberson error = EINVAL; 1283d7f687fcSJeff Roberson break; 1284d7f687fcSJeff Roberson } 1285d7f687fcSJeff Roberson break; 1286d7f687fcSJeff Roberson default: 1287d7f687fcSJeff Roberson error = EINVAL; 1288d7f687fcSJeff Roberson break; 1289d7f687fcSJeff Roberson } 1290d7f687fcSJeff Roberson out: 1291d7f687fcSJeff Roberson free(mask, M_TEMP); 1292d7f687fcSJeff Roberson return (error); 1293d7f687fcSJeff Roberson } 1294dea0ed66SBjoern A. Zeeb 1295dea0ed66SBjoern A. Zeeb #ifdef DDB 1296cd32bd7aSJohn Baldwin void 1297cd32bd7aSJohn Baldwin ddb_display_cpuset(const cpuset_t *set) 1298dea0ed66SBjoern A. Zeeb { 1299dea0ed66SBjoern A. Zeeb int cpu, once; 1300dea0ed66SBjoern A. Zeeb 1301dea0ed66SBjoern A. Zeeb for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { 1302cd32bd7aSJohn Baldwin if (CPU_ISSET(cpu, set)) { 1303dea0ed66SBjoern A. Zeeb if (once == 0) { 1304dea0ed66SBjoern A. Zeeb db_printf("%d", cpu); 1305dea0ed66SBjoern A. Zeeb once = 1; 1306dea0ed66SBjoern A. Zeeb } else 1307dea0ed66SBjoern A. Zeeb db_printf(",%d", cpu); 1308dea0ed66SBjoern A. Zeeb } 1309dea0ed66SBjoern A. Zeeb } 1310cd32bd7aSJohn Baldwin if (once == 0) 1311cd32bd7aSJohn Baldwin db_printf("<none>"); 1312cd32bd7aSJohn Baldwin } 1313cd32bd7aSJohn Baldwin 1314cd32bd7aSJohn Baldwin DB_SHOW_COMMAND(cpusets, db_show_cpusets) 1315cd32bd7aSJohn Baldwin { 1316cd32bd7aSJohn Baldwin struct cpuset *set; 1317cd32bd7aSJohn Baldwin 1318cd32bd7aSJohn Baldwin LIST_FOREACH(set, &cpuset_ids, cs_link) { 1319cd32bd7aSJohn Baldwin db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 1320cd32bd7aSJohn Baldwin set, set->cs_id, set->cs_ref, set->cs_flags, 1321cd32bd7aSJohn Baldwin (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 1322cd32bd7aSJohn Baldwin db_printf(" mask="); 1323cd32bd7aSJohn Baldwin ddb_display_cpuset(&set->cs_mask); 1324dea0ed66SBjoern A. Zeeb db_printf("\n"); 1325dea0ed66SBjoern A. Zeeb if (db_pager_quit) 1326dea0ed66SBjoern A. Zeeb break; 1327dea0ed66SBjoern A. Zeeb } 1328dea0ed66SBjoern A. Zeeb } 1329dea0ed66SBjoern A. Zeeb #endif /* DDB */ 1330