kern_cpuset.c revision 185435
1176730Sjeff/*- 2176730Sjeff * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org> 3176730Sjeff * All rights reserved. 4177904Sjeff * 5177904Sjeff * Copyright (c) 2008 Nokia Corporation 6177904Sjeff * All rights reserved. 7176730Sjeff * 8176730Sjeff * Redistribution and use in source and binary forms, with or without 9176730Sjeff * modification, are permitted provided that the following conditions 10176730Sjeff * are met: 11176730Sjeff * 1. Redistributions of source code must retain the above copyright 12176730Sjeff * notice unmodified, this list of conditions, and the following 13176730Sjeff * disclaimer. 14176730Sjeff * 2. Redistributions in binary form must reproduce the above copyright 15176730Sjeff * notice, this list of conditions and the following disclaimer in the 16176730Sjeff * documentation and/or other materials provided with the distribution. 17176730Sjeff * 18176730Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19176730Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20176730Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21176730Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22176730Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23176730Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24176730Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25176730Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26176730Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27176730Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28176730Sjeff * 29176730Sjeff */ 30176730Sjeff 31176730Sjeff#include <sys/cdefs.h> 32176730Sjeff__FBSDID("$FreeBSD: head/sys/kern/kern_cpuset.c 185435 2008-11-29 14:32:14Z bz $"); 33176730Sjeff 34180358Sbz#include "opt_ddb.h" 35180358Sbz 36176730Sjeff#include <sys/param.h> 37176730Sjeff#include <sys/systm.h> 38176730Sjeff#include <sys/sysproto.h> 39176730Sjeff#include <sys/kernel.h> 40176730Sjeff#include <sys/lock.h> 41176730Sjeff#include <sys/malloc.h> 42176730Sjeff#include <sys/mutex.h> 43176730Sjeff#include <sys/priv.h> 44176730Sjeff#include <sys/proc.h> 45176730Sjeff#include <sys/refcount.h> 46176730Sjeff#include <sys/sched.h> 47176730Sjeff#include <sys/smp.h> 48176730Sjeff#include <sys/syscallsubr.h> 49176730Sjeff#include <sys/cpuset.h> 50176730Sjeff#include <sys/sx.h> 51176730Sjeff#include <sys/refcount.h> 52176730Sjeff#include <sys/queue.h> 53176730Sjeff#include <sys/limits.h> 54177738Sjeff#include <sys/bus.h> 55177738Sjeff#include <sys/interrupt.h> 56185435Sbz#include <sys/jail.h> /* Must come after sys/proc.h */ 57176730Sjeff 58176730Sjeff#include <vm/uma.h> 59176730Sjeff 60180358Sbz#ifdef DDB 61180358Sbz#include <ddb/ddb.h> 62180358Sbz#endif /* DDB */ 63180358Sbz 64176730Sjeff/* 65176730Sjeff * cpusets provide a mechanism for creating and manipulating sets of 66176730Sjeff * processors for the purpose of constraining the scheduling of threads to 67176730Sjeff * specific processors. 68176730Sjeff * 69176730Sjeff * Each process belongs to an identified set, by default this is set 1. Each 70176730Sjeff * thread may further restrict the cpus it may run on to a subset of this 71176730Sjeff * named set. This creates an anonymous set which other threads and processes 72176730Sjeff * may not join by number. 73176730Sjeff * 74176730Sjeff * The named set is referred to herein as the 'base' set to avoid ambiguity. 75176730Sjeff * This set is usually a child of a 'root' set while the anonymous set may 76176730Sjeff * simply be referred to as a mask. In the syscall api these are referred to 77176730Sjeff * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here. 78176730Sjeff * 79176730Sjeff * Threads inherit their set from their creator whether it be anonymous or 80176730Sjeff * not. This means that anonymous sets are immutable because they may be 81176730Sjeff * shared. To modify an anonymous set a new set is created with the desired 82176730Sjeff * mask and the same parent as the existing anonymous set. This gives the 83176730Sjeff * illusion of each thread having a private mask.A 84176730Sjeff * 85176730Sjeff * Via the syscall apis a user may ask to retrieve or modify the root, base, 86176730Sjeff * or mask that is discovered via a pid, tid, or setid. Modifying a set 87176730Sjeff * modifies all numbered and anonymous child sets to comply with the new mask. 88176730Sjeff * Modifying a pid or tid's mask applies only to that tid but must still 89176730Sjeff * exist within the assigned parent set. 90176730Sjeff * 91176730Sjeff * A thread may not be assigned to a a group seperate from other threads in 92176730Sjeff * the process. This is to remove ambiguity when the setid is queried with 93176730Sjeff * a pid argument. There is no other technical limitation. 94176730Sjeff * 95176730Sjeff * This somewhat complex arrangement is intended to make it easy for 96176730Sjeff * applications to query available processors and bind their threads to 97176730Sjeff * specific processors while also allowing administrators to dynamically 98176730Sjeff * reprovision by changing sets which apply to groups of processes. 99176730Sjeff * 100176730Sjeff * A simple application should not concern itself with sets at all and 101176730Sjeff * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id 102176730Sjeff * meaning 'curthread'. It may query availble cpus for that tid with a 103176730Sjeff * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...). 104176730Sjeff */ 105176730Sjeffstatic uma_zone_t cpuset_zone; 106176730Sjeffstatic struct mtx cpuset_lock; 107176730Sjeffstatic struct setlist cpuset_ids; 108176730Sjeffstatic struct unrhdr *cpuset_unr; 109177738Sjeffstatic struct cpuset *cpuset_zero; 110176730Sjeff 111177738Sjeffcpuset_t *cpuset_root; 112177738Sjeff 113176730Sjeff/* 114176730Sjeff * Acquire a reference to a cpuset, all pointers must be tracked with refs. 115176730Sjeff */ 116176730Sjeffstruct cpuset * 117176730Sjeffcpuset_ref(struct cpuset *set) 118176730Sjeff{ 119176730Sjeff 120176730Sjeff refcount_acquire(&set->cs_ref); 121176730Sjeff return (set); 122176730Sjeff} 123176730Sjeff 124176730Sjeff/* 125180356Sbz * Walks up the tree from 'set' to find the root. Returns the root 126180356Sbz * referenced. 127180356Sbz */ 128180356Sbzstatic struct cpuset * 129180356Sbzcpuset_refroot(struct cpuset *set) 130180356Sbz{ 131180356Sbz 132180356Sbz for (; set->cs_parent != NULL; set = set->cs_parent) 133180356Sbz if (set->cs_flags & CPU_SET_ROOT) 134180356Sbz break; 135180356Sbz cpuset_ref(set); 136180356Sbz 137180356Sbz return (set); 138180356Sbz} 139180356Sbz 140180356Sbz/* 141180356Sbz * Find the first non-anonymous set starting from 'set'. Returns this set 142180356Sbz * referenced. May return the passed in set with an extra ref if it is 143180356Sbz * not anonymous. 144180356Sbz */ 145180356Sbzstatic struct cpuset * 146180356Sbzcpuset_refbase(struct cpuset *set) 147180356Sbz{ 148180356Sbz 149180356Sbz if (set->cs_id == CPUSET_INVALID) 150180356Sbz set = set->cs_parent; 151180356Sbz cpuset_ref(set); 152180356Sbz 153180356Sbz return (set); 154180356Sbz} 155180356Sbz 156180356Sbz/* 157176730Sjeff * Release a reference in a context where it is safe to allocte. 158176730Sjeff */ 159176730Sjeffvoid 160176730Sjeffcpuset_rel(struct cpuset *set) 161176730Sjeff{ 162176730Sjeff cpusetid_t id; 163176730Sjeff 164176730Sjeff if (refcount_release(&set->cs_ref) == 0) 165176730Sjeff return; 166176730Sjeff mtx_lock_spin(&cpuset_lock); 167176730Sjeff LIST_REMOVE(set, cs_siblings); 168176730Sjeff id = set->cs_id; 169176730Sjeff if (id != CPUSET_INVALID) 170176730Sjeff LIST_REMOVE(set, cs_link); 171176730Sjeff mtx_unlock_spin(&cpuset_lock); 172176730Sjeff cpuset_rel(set->cs_parent); 173176730Sjeff uma_zfree(cpuset_zone, set); 174176730Sjeff if (id != CPUSET_INVALID) 175176730Sjeff free_unr(cpuset_unr, id); 176176730Sjeff} 177176730Sjeff 178176730Sjeff/* 179176730Sjeff * Deferred release must be used when in a context that is not safe to 180176730Sjeff * allocate/free. This places any unreferenced sets on the list 'head'. 181176730Sjeff */ 182176730Sjeffstatic void 183176730Sjeffcpuset_rel_defer(struct setlist *head, struct cpuset *set) 184176730Sjeff{ 185176730Sjeff 186176730Sjeff if (refcount_release(&set->cs_ref) == 0) 187176730Sjeff return; 188176730Sjeff mtx_lock_spin(&cpuset_lock); 189176730Sjeff LIST_REMOVE(set, cs_siblings); 190176730Sjeff if (set->cs_id != CPUSET_INVALID) 191176730Sjeff LIST_REMOVE(set, cs_link); 192176730Sjeff LIST_INSERT_HEAD(head, set, cs_link); 193176730Sjeff mtx_unlock_spin(&cpuset_lock); 194176730Sjeff} 195176730Sjeff 196176730Sjeff/* 197176730Sjeff * Complete a deferred release. Removes the set from the list provided to 198176730Sjeff * cpuset_rel_defer. 199176730Sjeff */ 200176730Sjeffstatic void 201176730Sjeffcpuset_rel_complete(struct cpuset *set) 202176730Sjeff{ 203176730Sjeff LIST_REMOVE(set, cs_link); 204176730Sjeff cpuset_rel(set->cs_parent); 205176730Sjeff uma_zfree(cpuset_zone, set); 206176730Sjeff} 207176730Sjeff 208176730Sjeff/* 209176730Sjeff * Find a set based on an id. Returns it with a ref. 210176730Sjeff */ 211176730Sjeffstatic struct cpuset * 212185435Sbzcpuset_lookup(cpusetid_t setid, struct thread *td) 213176730Sjeff{ 214176730Sjeff struct cpuset *set; 215176730Sjeff 216176730Sjeff if (setid == CPUSET_INVALID) 217176730Sjeff return (NULL); 218176730Sjeff mtx_lock_spin(&cpuset_lock); 219176730Sjeff LIST_FOREACH(set, &cpuset_ids, cs_link) 220176730Sjeff if (set->cs_id == setid) 221176730Sjeff break; 222176730Sjeff if (set) 223176730Sjeff cpuset_ref(set); 224176730Sjeff mtx_unlock_spin(&cpuset_lock); 225185435Sbz 226185435Sbz KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__)); 227185435Sbz if (set != NULL && jailed(td->td_ucred)) { 228185435Sbz struct cpuset *rset, *jset; 229185435Sbz struct prison *pr; 230185435Sbz 231185435Sbz rset = cpuset_refroot(set); 232185435Sbz 233185435Sbz pr = td->td_ucred->cr_prison; 234185435Sbz mtx_lock(&pr->pr_mtx); 235185435Sbz cpuset_ref(pr->pr_cpuset); 236185435Sbz jset = pr->pr_cpuset; 237185435Sbz mtx_unlock(&pr->pr_mtx); 238185435Sbz 239185435Sbz if (jset->cs_id != rset->cs_id) { 240185435Sbz cpuset_rel(set); 241185435Sbz set = NULL; 242185435Sbz } 243185435Sbz cpuset_rel(jset); 244185435Sbz cpuset_rel(rset); 245185435Sbz } 246185435Sbz 247176730Sjeff return (set); 248176730Sjeff} 249176730Sjeff 250176730Sjeff/* 251176730Sjeff * Create a set in the space provided in 'set' with the provided parameters. 252176730Sjeff * The set is returned with a single ref. May return EDEADLK if the set 253176730Sjeff * will have no valid cpu based on restrictions from the parent. 254176730Sjeff */ 255176730Sjeffstatic int 256176730Sjeff_cpuset_create(struct cpuset *set, struct cpuset *parent, cpuset_t *mask, 257176730Sjeff cpusetid_t id) 258176730Sjeff{ 259176730Sjeff 260176811Sjeff if (!CPU_OVERLAP(&parent->cs_mask, mask)) 261176811Sjeff return (EDEADLK); 262176730Sjeff CPU_COPY(mask, &set->cs_mask); 263176730Sjeff LIST_INIT(&set->cs_children); 264176730Sjeff refcount_init(&set->cs_ref, 1); 265176730Sjeff set->cs_flags = 0; 266176730Sjeff mtx_lock_spin(&cpuset_lock); 267176730Sjeff CPU_AND(mask, &parent->cs_mask); 268176811Sjeff set->cs_id = id; 269176811Sjeff set->cs_parent = cpuset_ref(parent); 270176811Sjeff LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings); 271176811Sjeff if (set->cs_id != CPUSET_INVALID) 272176811Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 273176730Sjeff mtx_unlock_spin(&cpuset_lock); 274176730Sjeff 275176811Sjeff return (0); 276176730Sjeff} 277176730Sjeff 278176730Sjeff/* 279176730Sjeff * Create a new non-anonymous set with the requested parent and mask. May 280176730Sjeff * return failures if the mask is invalid or a new number can not be 281176730Sjeff * allocated. 282176730Sjeff */ 283176730Sjeffstatic int 284176730Sjeffcpuset_create(struct cpuset **setp, struct cpuset *parent, cpuset_t *mask) 285176730Sjeff{ 286176730Sjeff struct cpuset *set; 287176730Sjeff cpusetid_t id; 288176730Sjeff int error; 289176730Sjeff 290176730Sjeff id = alloc_unr(cpuset_unr); 291176730Sjeff if (id == -1) 292176730Sjeff return (ENFILE); 293176730Sjeff *setp = set = uma_zalloc(cpuset_zone, M_WAITOK); 294176730Sjeff error = _cpuset_create(set, parent, mask, id); 295176730Sjeff if (error == 0) 296176730Sjeff return (0); 297176730Sjeff free_unr(cpuset_unr, id); 298176730Sjeff uma_zfree(cpuset_zone, set); 299176730Sjeff 300176730Sjeff return (error); 301176730Sjeff} 302176730Sjeff 303176730Sjeff/* 304176730Sjeff * Recursively check for errors that would occur from applying mask to 305176730Sjeff * the tree of sets starting at 'set'. Checks for sets that would become 306176730Sjeff * empty as well as RDONLY flags. 307176730Sjeff */ 308176730Sjeffstatic int 309176730Sjeffcpuset_testupdate(struct cpuset *set, cpuset_t *mask) 310176730Sjeff{ 311176730Sjeff struct cpuset *nset; 312176730Sjeff cpuset_t newmask; 313176730Sjeff int error; 314176730Sjeff 315176730Sjeff mtx_assert(&cpuset_lock, MA_OWNED); 316176730Sjeff if (set->cs_flags & CPU_SET_RDONLY) 317176730Sjeff return (EPERM); 318176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, mask)) 319176811Sjeff return (EDEADLK); 320176730Sjeff CPU_COPY(&set->cs_mask, &newmask); 321176730Sjeff CPU_AND(&newmask, mask); 322176811Sjeff error = 0; 323176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 324176730Sjeff if ((error = cpuset_testupdate(nset, &newmask)) != 0) 325176730Sjeff break; 326176730Sjeff return (error); 327176730Sjeff} 328176730Sjeff 329176730Sjeff/* 330176730Sjeff * Applies the mask 'mask' without checking for empty sets or permissions. 331176730Sjeff */ 332176730Sjeffstatic void 333176730Sjeffcpuset_update(struct cpuset *set, cpuset_t *mask) 334176730Sjeff{ 335176730Sjeff struct cpuset *nset; 336176730Sjeff 337176730Sjeff mtx_assert(&cpuset_lock, MA_OWNED); 338176730Sjeff CPU_AND(&set->cs_mask, mask); 339176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 340176730Sjeff cpuset_update(nset, &set->cs_mask); 341176730Sjeff 342176730Sjeff return; 343176730Sjeff} 344176730Sjeff 345176730Sjeff/* 346176730Sjeff * Modify the set 'set' to use a copy of the mask provided. Apply this new 347176730Sjeff * mask to restrict all children in the tree. Checks for validity before 348176730Sjeff * applying the changes. 349176730Sjeff */ 350176730Sjeffstatic int 351176730Sjeffcpuset_modify(struct cpuset *set, cpuset_t *mask) 352176730Sjeff{ 353176811Sjeff struct cpuset *root; 354176730Sjeff int error; 355176730Sjeff 356180098Sbz error = priv_check(curthread, PRIV_SCHED_CPUSET); 357176730Sjeff if (error) 358176730Sjeff return (error); 359176811Sjeff /* 360176811Sjeff * Verify that we have access to this set of 361176811Sjeff * cpus. 362176811Sjeff */ 363176811Sjeff root = set->cs_parent; 364176811Sjeff if (root && !CPU_SUBSET(&root->cs_mask, mask)) 365176811Sjeff return (EINVAL); 366176730Sjeff mtx_lock_spin(&cpuset_lock); 367176730Sjeff error = cpuset_testupdate(set, mask); 368176730Sjeff if (error) 369176730Sjeff goto out; 370176730Sjeff cpuset_update(set, mask); 371176730Sjeff CPU_COPY(mask, &set->cs_mask); 372176730Sjeffout: 373176730Sjeff mtx_unlock_spin(&cpuset_lock); 374176730Sjeff 375176730Sjeff return (error); 376176730Sjeff} 377176730Sjeff 378176730Sjeff/* 379176730Sjeff * Resolve the 'which' parameter of several cpuset apis. 380176730Sjeff * 381176730Sjeff * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 382176730Sjeff * checks for permission via p_cansched(). 383176730Sjeff * 384176730Sjeff * For WHICH_SET returns a valid set with a new reference. 385176730Sjeff * 386176730Sjeff * -1 may be supplied for any argument to mean the current proc/thread or 387176730Sjeff * the base set of the current thread. May fail with ESRCH/EPERM. 388176730Sjeff */ 389176730Sjeffstatic int 390176730Sjeffcpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 391176730Sjeff struct cpuset **setp) 392176730Sjeff{ 393176730Sjeff struct cpuset *set; 394176730Sjeff struct thread *td; 395176730Sjeff struct proc *p; 396176730Sjeff int error; 397176730Sjeff 398176730Sjeff *pp = p = NULL; 399176730Sjeff *tdp = td = NULL; 400176730Sjeff *setp = set = NULL; 401176730Sjeff switch (which) { 402176730Sjeff case CPU_WHICH_PID: 403176730Sjeff if (id == -1) { 404176730Sjeff PROC_LOCK(curproc); 405176730Sjeff p = curproc; 406176730Sjeff break; 407176730Sjeff } 408176730Sjeff if ((p = pfind(id)) == NULL) 409176730Sjeff return (ESRCH); 410176730Sjeff break; 411176730Sjeff case CPU_WHICH_TID: 412176730Sjeff if (id == -1) { 413176730Sjeff PROC_LOCK(curproc); 414176730Sjeff p = curproc; 415176730Sjeff td = curthread; 416176730Sjeff break; 417176730Sjeff } 418176730Sjeff sx_slock(&allproc_lock); 419176730Sjeff FOREACH_PROC_IN_SYSTEM(p) { 420176730Sjeff PROC_LOCK(p); 421176730Sjeff FOREACH_THREAD_IN_PROC(p, td) 422176730Sjeff if (td->td_tid == id) 423176730Sjeff break; 424176730Sjeff if (td != NULL) 425176730Sjeff break; 426176730Sjeff PROC_UNLOCK(p); 427176730Sjeff } 428176730Sjeff sx_sunlock(&allproc_lock); 429176730Sjeff if (td == NULL) 430176730Sjeff return (ESRCH); 431176730Sjeff break; 432176730Sjeff case CPU_WHICH_CPUSET: 433176730Sjeff if (id == -1) { 434176730Sjeff thread_lock(curthread); 435177738Sjeff set = cpuset_refbase(curthread->td_cpuset); 436176730Sjeff thread_unlock(curthread); 437176730Sjeff } else 438185435Sbz set = cpuset_lookup(id, curthread); 439176730Sjeff if (set) { 440176730Sjeff *setp = set; 441176730Sjeff return (0); 442176730Sjeff } 443176730Sjeff return (ESRCH); 444185435Sbz case CPU_WHICH_JAIL: 445185435Sbz { 446185435Sbz /* Find `set' for prison with given id. */ 447185435Sbz struct prison *pr; 448185435Sbz 449185435Sbz sx_slock(&allprison_lock); 450185435Sbz pr = prison_find(id); 451185435Sbz sx_sunlock(&allprison_lock); 452185435Sbz if (pr == NULL) 453185435Sbz return (ESRCH); 454185435Sbz if (jailed(curthread->td_ucred)) { 455185435Sbz if (curthread->td_ucred->cr_prison == pr) { 456185435Sbz cpuset_ref(pr->pr_cpuset); 457185435Sbz set = pr->pr_cpuset; 458185435Sbz } 459185435Sbz } else { 460185435Sbz cpuset_ref(pr->pr_cpuset); 461185435Sbz set = pr->pr_cpuset; 462185435Sbz } 463185435Sbz mtx_unlock(&pr->pr_mtx); 464185435Sbz if (set) { 465185435Sbz *setp = set; 466185435Sbz return (0); 467185435Sbz } 468185435Sbz return (ESRCH); 469185435Sbz } 470178092Sjeff case CPU_WHICH_IRQ: 471178092Sjeff return (0); 472176730Sjeff default: 473176730Sjeff return (EINVAL); 474176730Sjeff } 475176730Sjeff error = p_cansched(curthread, p); 476176730Sjeff if (error) { 477176730Sjeff PROC_UNLOCK(p); 478176730Sjeff return (error); 479176730Sjeff } 480176730Sjeff if (td == NULL) 481176730Sjeff td = FIRST_THREAD_IN_PROC(p); 482176730Sjeff *pp = p; 483176730Sjeff *tdp = td; 484176730Sjeff return (0); 485176730Sjeff} 486176730Sjeff 487176730Sjeff/* 488176730Sjeff * Create an anonymous set with the provided mask in the space provided by 489176730Sjeff * 'fset'. If the passed in set is anonymous we use its parent otherwise 490176730Sjeff * the new set is a child of 'set'. 491176730Sjeff */ 492176730Sjeffstatic int 493176730Sjeffcpuset_shadow(struct cpuset *set, struct cpuset *fset, cpuset_t *mask) 494176730Sjeff{ 495176730Sjeff struct cpuset *parent; 496176730Sjeff 497176730Sjeff if (set->cs_id == CPUSET_INVALID) 498176730Sjeff parent = set->cs_parent; 499176730Sjeff else 500176730Sjeff parent = set; 501176811Sjeff if (!CPU_SUBSET(&parent->cs_mask, mask)) 502177738Sjeff return (EDEADLK); 503176730Sjeff return (_cpuset_create(fset, parent, mask, CPUSET_INVALID)); 504176730Sjeff} 505176730Sjeff 506176730Sjeff/* 507176730Sjeff * Handle two cases for replacing the base set or mask of an entire process. 508176730Sjeff * 509176730Sjeff * 1) Set is non-null and mask is null. This reparents all anonymous sets 510176730Sjeff * to the provided set and replaces all non-anonymous td_cpusets with the 511176730Sjeff * provided set. 512176730Sjeff * 2) Mask is non-null and set is null. This replaces or creates anonymous 513176730Sjeff * sets for every thread with the existing base as a parent. 514176730Sjeff * 515176730Sjeff * This is overly complicated because we can't allocate while holding a 516176730Sjeff * spinlock and spinlocks must be held while changing and examining thread 517176730Sjeff * state. 518176730Sjeff */ 519176730Sjeffstatic int 520176730Sjeffcpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask) 521176730Sjeff{ 522176730Sjeff struct setlist freelist; 523176730Sjeff struct setlist droplist; 524176811Sjeff struct cpuset *tdset; 525176730Sjeff struct cpuset *nset; 526176730Sjeff struct thread *td; 527176730Sjeff struct proc *p; 528176730Sjeff int threads; 529176730Sjeff int nfree; 530176730Sjeff int error; 531176730Sjeff /* 532176730Sjeff * The algorithm requires two passes due to locking considerations. 533176730Sjeff * 534176730Sjeff * 1) Lookup the process and acquire the locks in the required order. 535176730Sjeff * 2) If enough cpusets have not been allocated release the locks and 536176730Sjeff * allocate them. Loop. 537176730Sjeff */ 538176730Sjeff LIST_INIT(&freelist); 539176730Sjeff LIST_INIT(&droplist); 540176730Sjeff nfree = 0; 541176730Sjeff for (;;) { 542176730Sjeff error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 543176730Sjeff if (error) 544176730Sjeff goto out; 545176730Sjeff if (nfree >= p->p_numthreads) 546176730Sjeff break; 547176730Sjeff threads = p->p_numthreads; 548176730Sjeff PROC_UNLOCK(p); 549176730Sjeff for (; nfree < threads; nfree++) { 550176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 551176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 552176730Sjeff } 553176730Sjeff } 554176730Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 555176730Sjeff /* 556176730Sjeff * Now that the appropriate locks are held and we have enough cpusets, 557176811Sjeff * make sure the operation will succeed before applying changes. The 558176811Sjeff * proc lock prevents td_cpuset from changing between calls. 559176811Sjeff */ 560176811Sjeff error = 0; 561176811Sjeff FOREACH_THREAD_IN_PROC(p, td) { 562176811Sjeff thread_lock(td); 563176811Sjeff tdset = td->td_cpuset; 564176811Sjeff /* 565176811Sjeff * Verify that a new mask doesn't specify cpus outside of 566176811Sjeff * the set the thread is a member of. 567176811Sjeff */ 568176811Sjeff if (mask) { 569176811Sjeff if (tdset->cs_id == CPUSET_INVALID) 570176811Sjeff tdset = tdset->cs_parent; 571176811Sjeff if (!CPU_SUBSET(&tdset->cs_mask, mask)) 572177738Sjeff error = EDEADLK; 573176811Sjeff /* 574176811Sjeff * Verify that a new set won't leave an existing thread 575176811Sjeff * mask without a cpu to run on. It can, however, restrict 576176811Sjeff * the set. 577176811Sjeff */ 578176811Sjeff } else if (tdset->cs_id == CPUSET_INVALID) { 579176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask)) 580177738Sjeff error = EDEADLK; 581176811Sjeff } 582176811Sjeff thread_unlock(td); 583176811Sjeff if (error) 584176811Sjeff goto unlock_out; 585176811Sjeff } 586176811Sjeff /* 587176811Sjeff * Replace each thread's cpuset while using deferred release. We 588177368Sjeff * must do this because the thread lock must be held while operating 589177368Sjeff * on the thread and this limits the type of operations allowed. 590176730Sjeff */ 591176730Sjeff FOREACH_THREAD_IN_PROC(p, td) { 592176730Sjeff thread_lock(td); 593176730Sjeff /* 594176730Sjeff * If we presently have an anonymous set or are applying a 595176730Sjeff * mask we must create an anonymous shadow set. That is 596176730Sjeff * either parented to our existing base or the supplied set. 597176730Sjeff * 598176730Sjeff * If we have a base set with no anonymous shadow we simply 599176730Sjeff * replace it outright. 600176730Sjeff */ 601176730Sjeff tdset = td->td_cpuset; 602176730Sjeff if (tdset->cs_id == CPUSET_INVALID || mask) { 603176730Sjeff nset = LIST_FIRST(&freelist); 604176730Sjeff LIST_REMOVE(nset, cs_link); 605176730Sjeff if (mask) 606176730Sjeff error = cpuset_shadow(tdset, nset, mask); 607176730Sjeff else 608176730Sjeff error = _cpuset_create(nset, set, 609176730Sjeff &tdset->cs_mask, CPUSET_INVALID); 610176730Sjeff if (error) { 611176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 612176730Sjeff thread_unlock(td); 613176730Sjeff break; 614176730Sjeff } 615176730Sjeff } else 616176730Sjeff nset = cpuset_ref(set); 617176730Sjeff cpuset_rel_defer(&droplist, tdset); 618176730Sjeff td->td_cpuset = nset; 619176730Sjeff sched_affinity(td); 620176730Sjeff thread_unlock(td); 621176730Sjeff } 622176811Sjeffunlock_out: 623176730Sjeff PROC_UNLOCK(p); 624176730Sjeffout: 625176730Sjeff while ((nset = LIST_FIRST(&droplist)) != NULL) 626176730Sjeff cpuset_rel_complete(nset); 627176730Sjeff while ((nset = LIST_FIRST(&freelist)) != NULL) { 628176730Sjeff LIST_REMOVE(nset, cs_link); 629176730Sjeff uma_zfree(cpuset_zone, nset); 630176730Sjeff } 631176730Sjeff return (error); 632176730Sjeff} 633176730Sjeff 634176730Sjeff/* 635176730Sjeff * Apply an anonymous mask to a single thread. 636176730Sjeff */ 637177738Sjeffint 638176730Sjeffcpuset_setthread(lwpid_t id, cpuset_t *mask) 639176730Sjeff{ 640176730Sjeff struct cpuset *nset; 641176730Sjeff struct cpuset *set; 642176730Sjeff struct thread *td; 643176730Sjeff struct proc *p; 644176730Sjeff int error; 645176730Sjeff 646176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 647176821Sjeff error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 648176730Sjeff if (error) 649176730Sjeff goto out; 650177738Sjeff set = NULL; 651176730Sjeff thread_lock(td); 652177738Sjeff error = cpuset_shadow(td->td_cpuset, nset, mask); 653176730Sjeff if (error == 0) { 654177738Sjeff set = td->td_cpuset; 655176730Sjeff td->td_cpuset = nset; 656176730Sjeff sched_affinity(td); 657176730Sjeff nset = NULL; 658176730Sjeff } 659176730Sjeff thread_unlock(td); 660176730Sjeff PROC_UNLOCK(p); 661177738Sjeff if (set) 662177738Sjeff cpuset_rel(set); 663176730Sjeffout: 664176730Sjeff if (nset) 665176730Sjeff uma_zfree(cpuset_zone, nset); 666176730Sjeff return (error); 667176730Sjeff} 668176730Sjeff 669176730Sjeff/* 670176730Sjeff * Creates the cpuset for thread0. We make two sets: 671176730Sjeff * 672176730Sjeff * 0 - The root set which should represent all valid processors in the 673176730Sjeff * system. It is initially created with a mask of all processors 674176730Sjeff * because we don't know what processors are valid until cpuset_init() 675176730Sjeff * runs. This set is immutable. 676176730Sjeff * 1 - The default set which all processes are a member of until changed. 677176730Sjeff * This allows an administrator to move all threads off of given cpus to 678176730Sjeff * dedicate them to high priority tasks or save power etc. 679176730Sjeff */ 680176730Sjeffstruct cpuset * 681176730Sjeffcpuset_thread0(void) 682176730Sjeff{ 683176730Sjeff struct cpuset *set; 684176730Sjeff int error; 685176730Sjeff 686176730Sjeff cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 687176730Sjeff NULL, NULL, UMA_ALIGN_PTR, 0); 688176730Sjeff mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 689176730Sjeff /* 690176730Sjeff * Create the root system set for the whole machine. Doesn't use 691176730Sjeff * cpuset_create() due to NULL parent. 692176730Sjeff */ 693176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 694176730Sjeff set->cs_mask.__bits[0] = -1; 695176730Sjeff LIST_INIT(&set->cs_children); 696176730Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 697176730Sjeff set->cs_ref = 1; 698176730Sjeff set->cs_flags = CPU_SET_ROOT; 699176730Sjeff cpuset_zero = set; 700177738Sjeff cpuset_root = &set->cs_mask; 701176730Sjeff /* 702176730Sjeff * Now derive a default, modifiable set from that to give out. 703176730Sjeff */ 704176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK); 705176730Sjeff error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1); 706176730Sjeff KASSERT(error == 0, ("Error creating default set: %d\n", error)); 707176730Sjeff /* 708176730Sjeff * Initialize the unit allocator. 0 and 1 are allocated above. 709176730Sjeff */ 710176730Sjeff cpuset_unr = new_unrhdr(2, INT_MAX, NULL); 711176730Sjeff 712176730Sjeff return (set); 713176730Sjeff} 714176730Sjeff 715176730Sjeff/* 716185435Sbz * Create a cpuset, which would be cpuset_create() but 717185435Sbz * mark the new 'set' as root. 718185435Sbz * 719185435Sbz * We are not going to reparent the td to it. Use cpuset_reparentproc() for that. 720185435Sbz * 721185435Sbz * In case of no error, returns the set in *setp locked with a reference. 722185435Sbz */ 723185435Sbzint 724185435Sbzcpuset_create_root(struct thread *td, struct cpuset **setp) 725185435Sbz{ 726185435Sbz struct cpuset *root; 727185435Sbz struct cpuset *set; 728185435Sbz int error; 729185435Sbz 730185435Sbz KASSERT(td != NULL, ("[%s:%d] invalid td", __func__, __LINE__)); 731185435Sbz KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 732185435Sbz 733185435Sbz thread_lock(td); 734185435Sbz root = cpuset_refroot(td->td_cpuset); 735185435Sbz thread_unlock(td); 736185435Sbz 737185435Sbz error = cpuset_create(setp, td->td_cpuset, &root->cs_mask); 738185435Sbz cpuset_rel(root); 739185435Sbz if (error) 740185435Sbz return (error); 741185435Sbz 742185435Sbz KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data", 743185435Sbz __func__, __LINE__)); 744185435Sbz 745185435Sbz /* Mark the set as root. */ 746185435Sbz set = *setp; 747185435Sbz set->cs_flags |= CPU_SET_ROOT; 748185435Sbz 749185435Sbz return (0); 750185435Sbz} 751185435Sbz 752185435Sbzint 753185435Sbzcpuset_setproc_update_set(struct proc *p, struct cpuset *set) 754185435Sbz{ 755185435Sbz int error; 756185435Sbz 757185435Sbz KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 758185435Sbz KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 759185435Sbz 760185435Sbz cpuset_ref(set); 761185435Sbz error = cpuset_setproc(p->p_pid, set, NULL); 762185435Sbz if (error) 763185435Sbz return (error); 764185435Sbz cpuset_rel(set); 765185435Sbz return (0); 766185435Sbz} 767185435Sbz 768185435Sbz/* 769176730Sjeff * This is called once the final set of system cpus is known. Modifies 770176730Sjeff * the root set and all children and mark the root readonly. 771176730Sjeff */ 772176730Sjeffstatic void 773176730Sjeffcpuset_init(void *arg) 774176730Sjeff{ 775176730Sjeff cpuset_t mask; 776176730Sjeff 777176730Sjeff CPU_ZERO(&mask); 778176730Sjeff#ifdef SMP 779176730Sjeff mask.__bits[0] = all_cpus; 780176730Sjeff#else 781176730Sjeff mask.__bits[0] = 1; 782176730Sjeff#endif 783176730Sjeff if (cpuset_modify(cpuset_zero, &mask)) 784176730Sjeff panic("Can't set initial cpuset mask.\n"); 785176730Sjeff cpuset_zero->cs_flags |= CPU_SET_RDONLY; 786176730Sjeff} 787176730SjeffSYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL); 788176730Sjeff 789176730Sjeff#ifndef _SYS_SYSPROTO_H_ 790176730Sjeffstruct cpuset_args { 791176730Sjeff cpusetid_t *setid; 792176730Sjeff}; 793176730Sjeff#endif 794176730Sjeffint 795176730Sjeffcpuset(struct thread *td, struct cpuset_args *uap) 796176730Sjeff{ 797176730Sjeff struct cpuset *root; 798176730Sjeff struct cpuset *set; 799176730Sjeff int error; 800176730Sjeff 801176730Sjeff thread_lock(td); 802177738Sjeff root = cpuset_refroot(td->td_cpuset); 803176730Sjeff thread_unlock(td); 804176730Sjeff error = cpuset_create(&set, root, &root->cs_mask); 805176730Sjeff cpuset_rel(root); 806176730Sjeff if (error) 807176730Sjeff return (error); 808177738Sjeff error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 809176730Sjeff if (error == 0) 810177738Sjeff error = cpuset_setproc(-1, set, NULL); 811176730Sjeff cpuset_rel(set); 812176730Sjeff return (error); 813176730Sjeff} 814176730Sjeff 815176730Sjeff#ifndef _SYS_SYSPROTO_H_ 816176730Sjeffstruct cpuset_setid_args { 817176730Sjeff cpuwhich_t which; 818176730Sjeff id_t id; 819176730Sjeff cpusetid_t setid; 820176730Sjeff}; 821176730Sjeff#endif 822176730Sjeffint 823176730Sjeffcpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 824176730Sjeff{ 825176730Sjeff struct cpuset *set; 826176730Sjeff int error; 827176730Sjeff 828176730Sjeff /* 829176730Sjeff * Presently we only support per-process sets. 830176730Sjeff */ 831176730Sjeff if (uap->which != CPU_WHICH_PID) 832176730Sjeff return (EINVAL); 833185435Sbz set = cpuset_lookup(uap->setid, td); 834176730Sjeff if (set == NULL) 835176730Sjeff return (ESRCH); 836176730Sjeff error = cpuset_setproc(uap->id, set, NULL); 837176730Sjeff cpuset_rel(set); 838176730Sjeff return (error); 839176730Sjeff} 840176730Sjeff 841176730Sjeff#ifndef _SYS_SYSPROTO_H_ 842176730Sjeffstruct cpuset_getid_args { 843176730Sjeff cpulevel_t level; 844176730Sjeff cpuwhich_t which; 845176730Sjeff id_t id; 846176730Sjeff cpusetid_t *setid; 847176730Sjeff#endif 848176730Sjeffint 849176730Sjeffcpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 850176730Sjeff{ 851176730Sjeff struct cpuset *nset; 852176730Sjeff struct cpuset *set; 853176730Sjeff struct thread *ttd; 854176730Sjeff struct proc *p; 855176730Sjeff cpusetid_t id; 856176730Sjeff int error; 857176730Sjeff 858176730Sjeff if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET) 859176730Sjeff return (EINVAL); 860176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 861176730Sjeff if (error) 862176730Sjeff return (error); 863176730Sjeff switch (uap->which) { 864176730Sjeff case CPU_WHICH_TID: 865176730Sjeff case CPU_WHICH_PID: 866176730Sjeff thread_lock(ttd); 867177738Sjeff set = cpuset_refbase(ttd->td_cpuset); 868176730Sjeff thread_unlock(ttd); 869176730Sjeff PROC_UNLOCK(p); 870176730Sjeff break; 871176730Sjeff case CPU_WHICH_CPUSET: 872185435Sbz case CPU_WHICH_JAIL: 873176730Sjeff break; 874178092Sjeff case CPU_WHICH_IRQ: 875178092Sjeff return (EINVAL); 876176730Sjeff } 877176730Sjeff switch (uap->level) { 878176730Sjeff case CPU_LEVEL_ROOT: 879177738Sjeff nset = cpuset_refroot(set); 880176730Sjeff cpuset_rel(set); 881176730Sjeff set = nset; 882176730Sjeff break; 883176730Sjeff case CPU_LEVEL_CPUSET: 884176730Sjeff break; 885176730Sjeff case CPU_LEVEL_WHICH: 886176730Sjeff break; 887176730Sjeff } 888176730Sjeff id = set->cs_id; 889176730Sjeff cpuset_rel(set); 890176730Sjeff if (error == 0) 891176730Sjeff error = copyout(&id, uap->setid, sizeof(id)); 892176730Sjeff 893176730Sjeff return (error); 894176730Sjeff} 895176730Sjeff 896176730Sjeff#ifndef _SYS_SYSPROTO_H_ 897176730Sjeffstruct cpuset_getaffinity_args { 898177597Sru cpulevel_t level; 899177597Sru cpuwhich_t which; 900177597Sru id_t id; 901177597Sru size_t cpusetsize; 902177597Sru cpuset_t *mask; 903176730Sjeff}; 904176730Sjeff#endif 905176730Sjeffint 906176730Sjeffcpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 907176730Sjeff{ 908176730Sjeff struct thread *ttd; 909176730Sjeff struct cpuset *nset; 910176730Sjeff struct cpuset *set; 911176730Sjeff struct proc *p; 912176730Sjeff cpuset_t *mask; 913176730Sjeff int error; 914177597Sru size_t size; 915176730Sjeff 916176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 917179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 918176730Sjeff return (ERANGE); 919176811Sjeff size = uap->cpusetsize; 920176730Sjeff mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 921176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 922176730Sjeff if (error) 923176730Sjeff goto out; 924176730Sjeff switch (uap->level) { 925176730Sjeff case CPU_LEVEL_ROOT: 926176730Sjeff case CPU_LEVEL_CPUSET: 927176730Sjeff switch (uap->which) { 928176730Sjeff case CPU_WHICH_TID: 929176730Sjeff case CPU_WHICH_PID: 930176730Sjeff thread_lock(ttd); 931176730Sjeff set = cpuset_ref(ttd->td_cpuset); 932176730Sjeff thread_unlock(ttd); 933176730Sjeff break; 934176730Sjeff case CPU_WHICH_CPUSET: 935185435Sbz case CPU_WHICH_JAIL: 936176730Sjeff break; 937178092Sjeff case CPU_WHICH_IRQ: 938178092Sjeff error = EINVAL; 939178092Sjeff goto out; 940176730Sjeff } 941176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 942177738Sjeff nset = cpuset_refroot(set); 943176730Sjeff else 944177738Sjeff nset = cpuset_refbase(set); 945176730Sjeff CPU_COPY(&nset->cs_mask, mask); 946176730Sjeff cpuset_rel(nset); 947176730Sjeff break; 948176730Sjeff case CPU_LEVEL_WHICH: 949176730Sjeff switch (uap->which) { 950176730Sjeff case CPU_WHICH_TID: 951176730Sjeff thread_lock(ttd); 952176730Sjeff CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 953176730Sjeff thread_unlock(ttd); 954176730Sjeff break; 955176730Sjeff case CPU_WHICH_PID: 956176730Sjeff FOREACH_THREAD_IN_PROC(p, ttd) { 957176730Sjeff thread_lock(ttd); 958176730Sjeff CPU_OR(mask, &ttd->td_cpuset->cs_mask); 959176730Sjeff thread_unlock(ttd); 960176730Sjeff } 961176730Sjeff break; 962176730Sjeff case CPU_WHICH_CPUSET: 963185435Sbz case CPU_WHICH_JAIL: 964176730Sjeff CPU_COPY(&set->cs_mask, mask); 965176730Sjeff break; 966178092Sjeff case CPU_WHICH_IRQ: 967178092Sjeff error = intr_getaffinity(uap->id, mask); 968178092Sjeff break; 969176730Sjeff } 970176730Sjeff break; 971176730Sjeff default: 972176730Sjeff error = EINVAL; 973176730Sjeff break; 974176730Sjeff } 975176730Sjeff if (set) 976176730Sjeff cpuset_rel(set); 977176730Sjeff if (p) 978176730Sjeff PROC_UNLOCK(p); 979176730Sjeff if (error == 0) 980176730Sjeff error = copyout(mask, uap->mask, size); 981176730Sjeffout: 982176730Sjeff free(mask, M_TEMP); 983176730Sjeff return (error); 984176730Sjeff} 985176730Sjeff 986176730Sjeff#ifndef _SYS_SYSPROTO_H_ 987176730Sjeffstruct cpuset_setaffinity_args { 988176730Sjeff cpulevel_t level; 989177597Sru cpuwhich_t which; 990177597Sru id_t id; 991177597Sru size_t cpusetsize; 992177597Sru const cpuset_t *mask; 993176730Sjeff}; 994176730Sjeff#endif 995176730Sjeffint 996176730Sjeffcpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 997176730Sjeff{ 998176730Sjeff struct cpuset *nset; 999176730Sjeff struct cpuset *set; 1000176730Sjeff struct thread *ttd; 1001176730Sjeff struct proc *p; 1002176730Sjeff cpuset_t *mask; 1003176730Sjeff int error; 1004176730Sjeff 1005176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 1006179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 1007176730Sjeff return (ERANGE); 1008176811Sjeff mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO); 1009176811Sjeff error = copyin(uap->mask, mask, uap->cpusetsize); 1010176730Sjeff if (error) 1011176730Sjeff goto out; 1012176811Sjeff /* 1013176811Sjeff * Verify that no high bits are set. 1014176811Sjeff */ 1015176811Sjeff if (uap->cpusetsize > sizeof(cpuset_t)) { 1016176811Sjeff char *end; 1017176811Sjeff char *cp; 1018176811Sjeff 1019176811Sjeff end = cp = (char *)&mask->__bits; 1020176811Sjeff end += uap->cpusetsize; 1021176811Sjeff cp += sizeof(cpuset_t); 1022176811Sjeff while (cp != end) 1023176811Sjeff if (*cp++ != 0) { 1024176811Sjeff error = EINVAL; 1025176811Sjeff goto out; 1026176811Sjeff } 1027176811Sjeff 1028176811Sjeff } 1029176730Sjeff switch (uap->level) { 1030176730Sjeff case CPU_LEVEL_ROOT: 1031176730Sjeff case CPU_LEVEL_CPUSET: 1032176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 1033176730Sjeff if (error) 1034176730Sjeff break; 1035176730Sjeff switch (uap->which) { 1036176730Sjeff case CPU_WHICH_TID: 1037176730Sjeff case CPU_WHICH_PID: 1038176730Sjeff thread_lock(ttd); 1039176730Sjeff set = cpuset_ref(ttd->td_cpuset); 1040176730Sjeff thread_unlock(ttd); 1041176880Sjeff PROC_UNLOCK(p); 1042176730Sjeff break; 1043176730Sjeff case CPU_WHICH_CPUSET: 1044185435Sbz case CPU_WHICH_JAIL: 1045176730Sjeff break; 1046178092Sjeff case CPU_WHICH_IRQ: 1047178092Sjeff error = EINVAL; 1048178092Sjeff goto out; 1049176730Sjeff } 1050176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 1051177738Sjeff nset = cpuset_refroot(set); 1052176730Sjeff else 1053177738Sjeff nset = cpuset_refbase(set); 1054176730Sjeff error = cpuset_modify(nset, mask); 1055176730Sjeff cpuset_rel(nset); 1056176730Sjeff cpuset_rel(set); 1057176730Sjeff break; 1058176730Sjeff case CPU_LEVEL_WHICH: 1059176730Sjeff switch (uap->which) { 1060176730Sjeff case CPU_WHICH_TID: 1061176730Sjeff error = cpuset_setthread(uap->id, mask); 1062176730Sjeff break; 1063176730Sjeff case CPU_WHICH_PID: 1064176730Sjeff error = cpuset_setproc(uap->id, NULL, mask); 1065176730Sjeff break; 1066176730Sjeff case CPU_WHICH_CPUSET: 1067185435Sbz case CPU_WHICH_JAIL: 1068185435Sbz error = cpuset_which(uap->which, uap->id, &p, 1069176730Sjeff &ttd, &set); 1070176730Sjeff if (error == 0) { 1071176730Sjeff error = cpuset_modify(set, mask); 1072176730Sjeff cpuset_rel(set); 1073176730Sjeff } 1074176730Sjeff break; 1075178092Sjeff case CPU_WHICH_IRQ: 1076178092Sjeff error = intr_setaffinity(uap->id, mask); 1077178092Sjeff break; 1078176730Sjeff default: 1079176730Sjeff error = EINVAL; 1080176730Sjeff break; 1081176730Sjeff } 1082176730Sjeff break; 1083176730Sjeff default: 1084176730Sjeff error = EINVAL; 1085176730Sjeff break; 1086176730Sjeff } 1087176730Sjeffout: 1088176730Sjeff free(mask, M_TEMP); 1089176730Sjeff return (error); 1090176730Sjeff} 1091180358Sbz 1092180358Sbz#ifdef DDB 1093180358SbzDB_SHOW_COMMAND(cpusets, db_show_cpusets) 1094180358Sbz{ 1095180358Sbz struct cpuset *set; 1096180358Sbz int cpu, once; 1097180358Sbz 1098180358Sbz LIST_FOREACH(set, &cpuset_ids, cs_link) { 1099180358Sbz db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 1100180358Sbz set, set->cs_id, set->cs_ref, set->cs_flags, 1101180358Sbz (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 1102180358Sbz db_printf(" mask="); 1103180358Sbz for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { 1104180358Sbz if (CPU_ISSET(cpu, &set->cs_mask)) { 1105180358Sbz if (once == 0) { 1106180358Sbz db_printf("%d", cpu); 1107180358Sbz once = 1; 1108180358Sbz } else 1109180358Sbz db_printf(",%d", cpu); 1110180358Sbz } 1111180358Sbz } 1112180358Sbz db_printf("\n"); 1113180358Sbz if (db_pager_quit) 1114180358Sbz break; 1115180358Sbz } 1116180358Sbz} 1117180358Sbz#endif /* DDB */ 1118