kern_cpuset.c revision 214337
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 214337 2010-10-25 13:13:16Z davidxu $"); 33176730Sjeff 34180358Sbz#include "opt_ddb.h" 35180358Sbz 36176730Sjeff#include <sys/param.h> 37176730Sjeff#include <sys/systm.h> 38176730Sjeff#include <sys/sysproto.h> 39192895Sjamie#include <sys/jail.h> 40176730Sjeff#include <sys/kernel.h> 41176730Sjeff#include <sys/lock.h> 42176730Sjeff#include <sys/malloc.h> 43176730Sjeff#include <sys/mutex.h> 44176730Sjeff#include <sys/priv.h> 45176730Sjeff#include <sys/proc.h> 46176730Sjeff#include <sys/refcount.h> 47176730Sjeff#include <sys/sched.h> 48176730Sjeff#include <sys/smp.h> 49176730Sjeff#include <sys/syscallsubr.h> 50176730Sjeff#include <sys/cpuset.h> 51176730Sjeff#include <sys/sx.h> 52176730Sjeff#include <sys/queue.h> 53176730Sjeff#include <sys/limits.h> 54177738Sjeff#include <sys/bus.h> 55177738Sjeff#include <sys/interrupt.h> 56176730Sjeff 57176730Sjeff#include <vm/uma.h> 58176730Sjeff 59180358Sbz#ifdef DDB 60180358Sbz#include <ddb/ddb.h> 61180358Sbz#endif /* DDB */ 62180358Sbz 63176730Sjeff/* 64176730Sjeff * cpusets provide a mechanism for creating and manipulating sets of 65176730Sjeff * processors for the purpose of constraining the scheduling of threads to 66176730Sjeff * specific processors. 67176730Sjeff * 68176730Sjeff * Each process belongs to an identified set, by default this is set 1. Each 69176730Sjeff * thread may further restrict the cpus it may run on to a subset of this 70176730Sjeff * named set. This creates an anonymous set which other threads and processes 71176730Sjeff * may not join by number. 72176730Sjeff * 73176730Sjeff * The named set is referred to herein as the 'base' set to avoid ambiguity. 74176730Sjeff * This set is usually a child of a 'root' set while the anonymous set may 75176730Sjeff * simply be referred to as a mask. In the syscall api these are referred to 76176730Sjeff * as the ROOT, CPUSET, and MASK levels where CPUSET is called 'base' here. 77176730Sjeff * 78176730Sjeff * Threads inherit their set from their creator whether it be anonymous or 79176730Sjeff * not. This means that anonymous sets are immutable because they may be 80176730Sjeff * shared. To modify an anonymous set a new set is created with the desired 81176730Sjeff * mask and the same parent as the existing anonymous set. This gives the 82198493Sjhb * illusion of each thread having a private mask. 83176730Sjeff * 84176730Sjeff * Via the syscall apis a user may ask to retrieve or modify the root, base, 85176730Sjeff * or mask that is discovered via a pid, tid, or setid. Modifying a set 86176730Sjeff * modifies all numbered and anonymous child sets to comply with the new mask. 87176730Sjeff * Modifying a pid or tid's mask applies only to that tid but must still 88176730Sjeff * exist within the assigned parent set. 89176730Sjeff * 90198495Sjhb * A thread may not be assigned to a group separate from other threads in 91176730Sjeff * the process. This is to remove ambiguity when the setid is queried with 92176730Sjeff * a pid argument. There is no other technical limitation. 93176730Sjeff * 94176730Sjeff * This somewhat complex arrangement is intended to make it easy for 95176730Sjeff * applications to query available processors and bind their threads to 96176730Sjeff * specific processors while also allowing administrators to dynamically 97176730Sjeff * reprovision by changing sets which apply to groups of processes. 98176730Sjeff * 99176730Sjeff * A simple application should not concern itself with sets at all and 100176730Sjeff * rather apply masks to its own threads via CPU_WHICH_TID and a -1 id 101198493Sjhb * meaning 'curthread'. It may query available cpus for that tid with a 102176730Sjeff * getaffinity call using (CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, ...). 103176730Sjeff */ 104176730Sjeffstatic uma_zone_t cpuset_zone; 105176730Sjeffstatic struct mtx cpuset_lock; 106176730Sjeffstatic struct setlist cpuset_ids; 107176730Sjeffstatic struct unrhdr *cpuset_unr; 108177738Sjeffstatic struct cpuset *cpuset_zero; 109176730Sjeff 110177738Sjeffcpuset_t *cpuset_root; 111177738Sjeff 112176730Sjeff/* 113176730Sjeff * Acquire a reference to a cpuset, all pointers must be tracked with refs. 114176730Sjeff */ 115176730Sjeffstruct cpuset * 116176730Sjeffcpuset_ref(struct cpuset *set) 117176730Sjeff{ 118176730Sjeff 119176730Sjeff refcount_acquire(&set->cs_ref); 120176730Sjeff return (set); 121176730Sjeff} 122176730Sjeff 123176730Sjeff/* 124180356Sbz * Walks up the tree from 'set' to find the root. Returns the root 125180356Sbz * referenced. 126180356Sbz */ 127180356Sbzstatic struct cpuset * 128180356Sbzcpuset_refroot(struct cpuset *set) 129180356Sbz{ 130180356Sbz 131180356Sbz for (; set->cs_parent != NULL; set = set->cs_parent) 132180356Sbz if (set->cs_flags & CPU_SET_ROOT) 133180356Sbz break; 134180356Sbz cpuset_ref(set); 135180356Sbz 136180356Sbz return (set); 137180356Sbz} 138180356Sbz 139180356Sbz/* 140180356Sbz * Find the first non-anonymous set starting from 'set'. Returns this set 141180356Sbz * referenced. May return the passed in set with an extra ref if it is 142180356Sbz * not anonymous. 143180356Sbz */ 144180356Sbzstatic struct cpuset * 145180356Sbzcpuset_refbase(struct cpuset *set) 146180356Sbz{ 147180356Sbz 148180356Sbz if (set->cs_id == CPUSET_INVALID) 149180356Sbz set = set->cs_parent; 150180356Sbz cpuset_ref(set); 151180356Sbz 152180356Sbz return (set); 153180356Sbz} 154180356Sbz 155180356Sbz/* 156198493Sjhb * Release a reference in a context where it is safe to allocate. 157176730Sjeff */ 158176730Sjeffvoid 159176730Sjeffcpuset_rel(struct cpuset *set) 160176730Sjeff{ 161176730Sjeff cpusetid_t id; 162176730Sjeff 163176730Sjeff if (refcount_release(&set->cs_ref) == 0) 164176730Sjeff return; 165176730Sjeff mtx_lock_spin(&cpuset_lock); 166176730Sjeff LIST_REMOVE(set, cs_siblings); 167176730Sjeff id = set->cs_id; 168176730Sjeff if (id != CPUSET_INVALID) 169176730Sjeff LIST_REMOVE(set, cs_link); 170176730Sjeff mtx_unlock_spin(&cpuset_lock); 171176730Sjeff cpuset_rel(set->cs_parent); 172176730Sjeff uma_zfree(cpuset_zone, set); 173176730Sjeff if (id != CPUSET_INVALID) 174176730Sjeff free_unr(cpuset_unr, id); 175176730Sjeff} 176176730Sjeff 177176730Sjeff/* 178176730Sjeff * Deferred release must be used when in a context that is not safe to 179176730Sjeff * allocate/free. This places any unreferenced sets on the list 'head'. 180176730Sjeff */ 181176730Sjeffstatic void 182176730Sjeffcpuset_rel_defer(struct setlist *head, struct cpuset *set) 183176730Sjeff{ 184176730Sjeff 185176730Sjeff if (refcount_release(&set->cs_ref) == 0) 186176730Sjeff return; 187176730Sjeff mtx_lock_spin(&cpuset_lock); 188176730Sjeff LIST_REMOVE(set, cs_siblings); 189176730Sjeff if (set->cs_id != CPUSET_INVALID) 190176730Sjeff LIST_REMOVE(set, cs_link); 191176730Sjeff LIST_INSERT_HEAD(head, set, cs_link); 192176730Sjeff mtx_unlock_spin(&cpuset_lock); 193176730Sjeff} 194176730Sjeff 195176730Sjeff/* 196176730Sjeff * Complete a deferred release. Removes the set from the list provided to 197176730Sjeff * cpuset_rel_defer. 198176730Sjeff */ 199176730Sjeffstatic void 200176730Sjeffcpuset_rel_complete(struct cpuset *set) 201176730Sjeff{ 202176730Sjeff LIST_REMOVE(set, cs_link); 203176730Sjeff cpuset_rel(set->cs_parent); 204176730Sjeff uma_zfree(cpuset_zone, set); 205176730Sjeff} 206176730Sjeff 207176730Sjeff/* 208176730Sjeff * Find a set based on an id. Returns it with a ref. 209176730Sjeff */ 210176730Sjeffstatic struct cpuset * 211185435Sbzcpuset_lookup(cpusetid_t setid, struct thread *td) 212176730Sjeff{ 213176730Sjeff struct cpuset *set; 214176730Sjeff 215176730Sjeff if (setid == CPUSET_INVALID) 216176730Sjeff return (NULL); 217176730Sjeff mtx_lock_spin(&cpuset_lock); 218176730Sjeff LIST_FOREACH(set, &cpuset_ids, cs_link) 219176730Sjeff if (set->cs_id == setid) 220176730Sjeff break; 221176730Sjeff if (set) 222176730Sjeff cpuset_ref(set); 223176730Sjeff mtx_unlock_spin(&cpuset_lock); 224185435Sbz 225185435Sbz KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__)); 226185435Sbz if (set != NULL && jailed(td->td_ucred)) { 227192895Sjamie struct cpuset *jset, *tset; 228185435Sbz 229192895Sjamie jset = td->td_ucred->cr_prison->pr_cpuset; 230192895Sjamie for (tset = set; tset != NULL; tset = tset->cs_parent) 231192895Sjamie if (tset == jset) 232192895Sjamie break; 233192895Sjamie if (tset == NULL) { 234185435Sbz cpuset_rel(set); 235185435Sbz set = NULL; 236185435Sbz } 237185435Sbz } 238185435Sbz 239176730Sjeff return (set); 240176730Sjeff} 241176730Sjeff 242176730Sjeff/* 243176730Sjeff * Create a set in the space provided in 'set' with the provided parameters. 244176730Sjeff * The set is returned with a single ref. May return EDEADLK if the set 245176730Sjeff * will have no valid cpu based on restrictions from the parent. 246176730Sjeff */ 247176730Sjeffstatic int 248176730Sjeff_cpuset_create(struct cpuset *set, struct cpuset *parent, cpuset_t *mask, 249176730Sjeff cpusetid_t id) 250176730Sjeff{ 251176730Sjeff 252176811Sjeff if (!CPU_OVERLAP(&parent->cs_mask, mask)) 253176811Sjeff return (EDEADLK); 254176730Sjeff CPU_COPY(mask, &set->cs_mask); 255176730Sjeff LIST_INIT(&set->cs_children); 256176730Sjeff refcount_init(&set->cs_ref, 1); 257176730Sjeff set->cs_flags = 0; 258176730Sjeff mtx_lock_spin(&cpuset_lock); 259176730Sjeff CPU_AND(mask, &parent->cs_mask); 260176811Sjeff set->cs_id = id; 261176811Sjeff set->cs_parent = cpuset_ref(parent); 262176811Sjeff LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings); 263176811Sjeff if (set->cs_id != CPUSET_INVALID) 264176811Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 265176730Sjeff mtx_unlock_spin(&cpuset_lock); 266176730Sjeff 267176811Sjeff return (0); 268176730Sjeff} 269176730Sjeff 270176730Sjeff/* 271176730Sjeff * Create a new non-anonymous set with the requested parent and mask. May 272176730Sjeff * return failures if the mask is invalid or a new number can not be 273176730Sjeff * allocated. 274176730Sjeff */ 275176730Sjeffstatic int 276176730Sjeffcpuset_create(struct cpuset **setp, struct cpuset *parent, cpuset_t *mask) 277176730Sjeff{ 278176730Sjeff struct cpuset *set; 279176730Sjeff cpusetid_t id; 280176730Sjeff int error; 281176730Sjeff 282176730Sjeff id = alloc_unr(cpuset_unr); 283176730Sjeff if (id == -1) 284176730Sjeff return (ENFILE); 285176730Sjeff *setp = set = uma_zalloc(cpuset_zone, M_WAITOK); 286176730Sjeff error = _cpuset_create(set, parent, mask, id); 287176730Sjeff if (error == 0) 288176730Sjeff return (0); 289176730Sjeff free_unr(cpuset_unr, id); 290176730Sjeff uma_zfree(cpuset_zone, set); 291176730Sjeff 292176730Sjeff return (error); 293176730Sjeff} 294176730Sjeff 295176730Sjeff/* 296176730Sjeff * Recursively check for errors that would occur from applying mask to 297176730Sjeff * the tree of sets starting at 'set'. Checks for sets that would become 298176730Sjeff * empty as well as RDONLY flags. 299176730Sjeff */ 300176730Sjeffstatic int 301176730Sjeffcpuset_testupdate(struct cpuset *set, cpuset_t *mask) 302176730Sjeff{ 303176730Sjeff struct cpuset *nset; 304176730Sjeff cpuset_t newmask; 305176730Sjeff int error; 306176730Sjeff 307176730Sjeff mtx_assert(&cpuset_lock, MA_OWNED); 308176730Sjeff if (set->cs_flags & CPU_SET_RDONLY) 309176730Sjeff return (EPERM); 310176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, mask)) 311176811Sjeff return (EDEADLK); 312176730Sjeff CPU_COPY(&set->cs_mask, &newmask); 313176730Sjeff CPU_AND(&newmask, mask); 314176811Sjeff error = 0; 315176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 316176730Sjeff if ((error = cpuset_testupdate(nset, &newmask)) != 0) 317176730Sjeff break; 318176730Sjeff return (error); 319176730Sjeff} 320176730Sjeff 321176730Sjeff/* 322176730Sjeff * Applies the mask 'mask' without checking for empty sets or permissions. 323176730Sjeff */ 324176730Sjeffstatic void 325176730Sjeffcpuset_update(struct cpuset *set, cpuset_t *mask) 326176730Sjeff{ 327176730Sjeff struct cpuset *nset; 328176730Sjeff 329176730Sjeff mtx_assert(&cpuset_lock, MA_OWNED); 330176730Sjeff CPU_AND(&set->cs_mask, mask); 331176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 332176730Sjeff cpuset_update(nset, &set->cs_mask); 333176730Sjeff 334176730Sjeff return; 335176730Sjeff} 336176730Sjeff 337176730Sjeff/* 338176730Sjeff * Modify the set 'set' to use a copy of the mask provided. Apply this new 339176730Sjeff * mask to restrict all children in the tree. Checks for validity before 340176730Sjeff * applying the changes. 341176730Sjeff */ 342176730Sjeffstatic int 343176730Sjeffcpuset_modify(struct cpuset *set, cpuset_t *mask) 344176730Sjeff{ 345176811Sjeff struct cpuset *root; 346176730Sjeff int error; 347176730Sjeff 348180098Sbz error = priv_check(curthread, PRIV_SCHED_CPUSET); 349176730Sjeff if (error) 350176730Sjeff return (error); 351176811Sjeff /* 352191639Sbz * In case we are called from within the jail 353191639Sbz * we do not allow modifying the dedicated root 354191639Sbz * cpuset of the jail but may still allow to 355191639Sbz * change child sets. 356191639Sbz */ 357191639Sbz if (jailed(curthread->td_ucred) && 358191639Sbz set->cs_flags & CPU_SET_ROOT) 359191639Sbz return (EPERM); 360191639Sbz /* 361176811Sjeff * Verify that we have access to this set of 362176811Sjeff * cpus. 363176811Sjeff */ 364176811Sjeff root = set->cs_parent; 365176811Sjeff if (root && !CPU_SUBSET(&root->cs_mask, mask)) 366176811Sjeff return (EINVAL); 367176730Sjeff mtx_lock_spin(&cpuset_lock); 368176730Sjeff error = cpuset_testupdate(set, mask); 369176730Sjeff if (error) 370176730Sjeff goto out; 371176730Sjeff cpuset_update(set, mask); 372176730Sjeff CPU_COPY(mask, &set->cs_mask); 373176730Sjeffout: 374176730Sjeff mtx_unlock_spin(&cpuset_lock); 375176730Sjeff 376176730Sjeff return (error); 377176730Sjeff} 378176730Sjeff 379176730Sjeff/* 380176730Sjeff * Resolve the 'which' parameter of several cpuset apis. 381176730Sjeff * 382176730Sjeff * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 383176730Sjeff * checks for permission via p_cansched(). 384176730Sjeff * 385176730Sjeff * For WHICH_SET returns a valid set with a new reference. 386176730Sjeff * 387176730Sjeff * -1 may be supplied for any argument to mean the current proc/thread or 388176730Sjeff * the base set of the current thread. May fail with ESRCH/EPERM. 389176730Sjeff */ 390176730Sjeffstatic int 391176730Sjeffcpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 392176730Sjeff struct cpuset **setp) 393176730Sjeff{ 394176730Sjeff struct cpuset *set; 395176730Sjeff struct thread *td; 396176730Sjeff struct proc *p; 397176730Sjeff int error; 398176730Sjeff 399176730Sjeff *pp = p = NULL; 400176730Sjeff *tdp = td = NULL; 401176730Sjeff *setp = set = NULL; 402176730Sjeff switch (which) { 403176730Sjeff case CPU_WHICH_PID: 404176730Sjeff if (id == -1) { 405176730Sjeff PROC_LOCK(curproc); 406176730Sjeff p = curproc; 407176730Sjeff break; 408176730Sjeff } 409176730Sjeff if ((p = pfind(id)) == NULL) 410176730Sjeff return (ESRCH); 411176730Sjeff break; 412176730Sjeff case CPU_WHICH_TID: 413176730Sjeff if (id == -1) { 414176730Sjeff PROC_LOCK(curproc); 415176730Sjeff p = curproc; 416176730Sjeff td = curthread; 417176730Sjeff break; 418176730Sjeff } 419214337Sdavidxu td = tdfind(id, -1); 420176730Sjeff if (td == NULL) 421176730Sjeff return (ESRCH); 422214337Sdavidxu p = td->td_proc; 423176730Sjeff break; 424176730Sjeff case CPU_WHICH_CPUSET: 425176730Sjeff if (id == -1) { 426176730Sjeff thread_lock(curthread); 427177738Sjeff set = cpuset_refbase(curthread->td_cpuset); 428176730Sjeff thread_unlock(curthread); 429176730Sjeff } else 430185435Sbz set = cpuset_lookup(id, curthread); 431176730Sjeff if (set) { 432176730Sjeff *setp = set; 433176730Sjeff return (0); 434176730Sjeff } 435176730Sjeff return (ESRCH); 436185435Sbz case CPU_WHICH_JAIL: 437185435Sbz { 438185435Sbz /* Find `set' for prison with given id. */ 439185435Sbz struct prison *pr; 440185435Sbz 441185435Sbz sx_slock(&allprison_lock); 442192895Sjamie pr = prison_find_child(curthread->td_ucred->cr_prison, id); 443185435Sbz sx_sunlock(&allprison_lock); 444185435Sbz if (pr == NULL) 445185435Sbz return (ESRCH); 446192895Sjamie cpuset_ref(pr->pr_cpuset); 447192895Sjamie *setp = pr->pr_cpuset; 448185435Sbz mtx_unlock(&pr->pr_mtx); 449192895Sjamie return (0); 450185435Sbz } 451178092Sjeff case CPU_WHICH_IRQ: 452178092Sjeff return (0); 453176730Sjeff default: 454176730Sjeff return (EINVAL); 455176730Sjeff } 456176730Sjeff error = p_cansched(curthread, p); 457176730Sjeff if (error) { 458176730Sjeff PROC_UNLOCK(p); 459176730Sjeff return (error); 460176730Sjeff } 461176730Sjeff if (td == NULL) 462176730Sjeff td = FIRST_THREAD_IN_PROC(p); 463176730Sjeff *pp = p; 464176730Sjeff *tdp = td; 465176730Sjeff return (0); 466176730Sjeff} 467176730Sjeff 468176730Sjeff/* 469176730Sjeff * Create an anonymous set with the provided mask in the space provided by 470176730Sjeff * 'fset'. If the passed in set is anonymous we use its parent otherwise 471176730Sjeff * the new set is a child of 'set'. 472176730Sjeff */ 473176730Sjeffstatic int 474176730Sjeffcpuset_shadow(struct cpuset *set, struct cpuset *fset, cpuset_t *mask) 475176730Sjeff{ 476176730Sjeff struct cpuset *parent; 477176730Sjeff 478176730Sjeff if (set->cs_id == CPUSET_INVALID) 479176730Sjeff parent = set->cs_parent; 480176730Sjeff else 481176730Sjeff parent = set; 482176811Sjeff if (!CPU_SUBSET(&parent->cs_mask, mask)) 483177738Sjeff return (EDEADLK); 484176730Sjeff return (_cpuset_create(fset, parent, mask, CPUSET_INVALID)); 485176730Sjeff} 486176730Sjeff 487176730Sjeff/* 488176730Sjeff * Handle two cases for replacing the base set or mask of an entire process. 489176730Sjeff * 490176730Sjeff * 1) Set is non-null and mask is null. This reparents all anonymous sets 491176730Sjeff * to the provided set and replaces all non-anonymous td_cpusets with the 492176730Sjeff * provided set. 493176730Sjeff * 2) Mask is non-null and set is null. This replaces or creates anonymous 494176730Sjeff * sets for every thread with the existing base as a parent. 495176730Sjeff * 496176730Sjeff * This is overly complicated because we can't allocate while holding a 497176730Sjeff * spinlock and spinlocks must be held while changing and examining thread 498176730Sjeff * state. 499176730Sjeff */ 500176730Sjeffstatic int 501176730Sjeffcpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask) 502176730Sjeff{ 503176730Sjeff struct setlist freelist; 504176730Sjeff struct setlist droplist; 505176811Sjeff struct cpuset *tdset; 506176730Sjeff struct cpuset *nset; 507176730Sjeff struct thread *td; 508176730Sjeff struct proc *p; 509176730Sjeff int threads; 510176730Sjeff int nfree; 511176730Sjeff int error; 512176730Sjeff /* 513176730Sjeff * The algorithm requires two passes due to locking considerations. 514176730Sjeff * 515176730Sjeff * 1) Lookup the process and acquire the locks in the required order. 516176730Sjeff * 2) If enough cpusets have not been allocated release the locks and 517176730Sjeff * allocate them. Loop. 518176730Sjeff */ 519176730Sjeff LIST_INIT(&freelist); 520176730Sjeff LIST_INIT(&droplist); 521176730Sjeff nfree = 0; 522176730Sjeff for (;;) { 523176730Sjeff error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 524176730Sjeff if (error) 525176730Sjeff goto out; 526176730Sjeff if (nfree >= p->p_numthreads) 527176730Sjeff break; 528176730Sjeff threads = p->p_numthreads; 529176730Sjeff PROC_UNLOCK(p); 530176730Sjeff for (; nfree < threads; nfree++) { 531176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 532176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 533176730Sjeff } 534176730Sjeff } 535176730Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 536176730Sjeff /* 537176730Sjeff * Now that the appropriate locks are held and we have enough cpusets, 538176811Sjeff * make sure the operation will succeed before applying changes. The 539176811Sjeff * proc lock prevents td_cpuset from changing between calls. 540176811Sjeff */ 541176811Sjeff error = 0; 542176811Sjeff FOREACH_THREAD_IN_PROC(p, td) { 543176811Sjeff thread_lock(td); 544176811Sjeff tdset = td->td_cpuset; 545176811Sjeff /* 546176811Sjeff * Verify that a new mask doesn't specify cpus outside of 547176811Sjeff * the set the thread is a member of. 548176811Sjeff */ 549176811Sjeff if (mask) { 550176811Sjeff if (tdset->cs_id == CPUSET_INVALID) 551176811Sjeff tdset = tdset->cs_parent; 552176811Sjeff if (!CPU_SUBSET(&tdset->cs_mask, mask)) 553177738Sjeff error = EDEADLK; 554176811Sjeff /* 555176811Sjeff * Verify that a new set won't leave an existing thread 556176811Sjeff * mask without a cpu to run on. It can, however, restrict 557176811Sjeff * the set. 558176811Sjeff */ 559176811Sjeff } else if (tdset->cs_id == CPUSET_INVALID) { 560176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask)) 561177738Sjeff error = EDEADLK; 562176811Sjeff } 563176811Sjeff thread_unlock(td); 564176811Sjeff if (error) 565176811Sjeff goto unlock_out; 566176811Sjeff } 567176811Sjeff /* 568176811Sjeff * Replace each thread's cpuset while using deferred release. We 569177368Sjeff * must do this because the thread lock must be held while operating 570177368Sjeff * on the thread and this limits the type of operations allowed. 571176730Sjeff */ 572176730Sjeff FOREACH_THREAD_IN_PROC(p, td) { 573176730Sjeff thread_lock(td); 574176730Sjeff /* 575176730Sjeff * If we presently have an anonymous set or are applying a 576176730Sjeff * mask we must create an anonymous shadow set. That is 577176730Sjeff * either parented to our existing base or the supplied set. 578176730Sjeff * 579176730Sjeff * If we have a base set with no anonymous shadow we simply 580176730Sjeff * replace it outright. 581176730Sjeff */ 582176730Sjeff tdset = td->td_cpuset; 583176730Sjeff if (tdset->cs_id == CPUSET_INVALID || mask) { 584176730Sjeff nset = LIST_FIRST(&freelist); 585176730Sjeff LIST_REMOVE(nset, cs_link); 586176730Sjeff if (mask) 587176730Sjeff error = cpuset_shadow(tdset, nset, mask); 588176730Sjeff else 589176730Sjeff error = _cpuset_create(nset, set, 590176730Sjeff &tdset->cs_mask, CPUSET_INVALID); 591176730Sjeff if (error) { 592176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 593176730Sjeff thread_unlock(td); 594176730Sjeff break; 595176730Sjeff } 596176730Sjeff } else 597176730Sjeff nset = cpuset_ref(set); 598176730Sjeff cpuset_rel_defer(&droplist, tdset); 599176730Sjeff td->td_cpuset = nset; 600176730Sjeff sched_affinity(td); 601176730Sjeff thread_unlock(td); 602176730Sjeff } 603176811Sjeffunlock_out: 604176730Sjeff PROC_UNLOCK(p); 605176730Sjeffout: 606176730Sjeff while ((nset = LIST_FIRST(&droplist)) != NULL) 607176730Sjeff cpuset_rel_complete(nset); 608176730Sjeff while ((nset = LIST_FIRST(&freelist)) != NULL) { 609176730Sjeff LIST_REMOVE(nset, cs_link); 610176730Sjeff uma_zfree(cpuset_zone, nset); 611176730Sjeff } 612176730Sjeff return (error); 613176730Sjeff} 614176730Sjeff 615176730Sjeff/* 616176730Sjeff * Apply an anonymous mask to a single thread. 617176730Sjeff */ 618177738Sjeffint 619176730Sjeffcpuset_setthread(lwpid_t id, cpuset_t *mask) 620176730Sjeff{ 621176730Sjeff struct cpuset *nset; 622176730Sjeff struct cpuset *set; 623176730Sjeff struct thread *td; 624176730Sjeff struct proc *p; 625176730Sjeff int error; 626176730Sjeff 627176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 628176821Sjeff error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 629176730Sjeff if (error) 630176730Sjeff goto out; 631177738Sjeff set = NULL; 632176730Sjeff thread_lock(td); 633177738Sjeff error = cpuset_shadow(td->td_cpuset, nset, mask); 634176730Sjeff if (error == 0) { 635177738Sjeff set = td->td_cpuset; 636176730Sjeff td->td_cpuset = nset; 637176730Sjeff sched_affinity(td); 638176730Sjeff nset = NULL; 639176730Sjeff } 640176730Sjeff thread_unlock(td); 641176730Sjeff PROC_UNLOCK(p); 642177738Sjeff if (set) 643177738Sjeff cpuset_rel(set); 644176730Sjeffout: 645176730Sjeff if (nset) 646176730Sjeff uma_zfree(cpuset_zone, nset); 647176730Sjeff return (error); 648176730Sjeff} 649176730Sjeff 650176730Sjeff/* 651176730Sjeff * Creates the cpuset for thread0. We make two sets: 652176730Sjeff * 653176730Sjeff * 0 - The root set which should represent all valid processors in the 654176730Sjeff * system. It is initially created with a mask of all processors 655176730Sjeff * because we don't know what processors are valid until cpuset_init() 656176730Sjeff * runs. This set is immutable. 657176730Sjeff * 1 - The default set which all processes are a member of until changed. 658176730Sjeff * This allows an administrator to move all threads off of given cpus to 659176730Sjeff * dedicate them to high priority tasks or save power etc. 660176730Sjeff */ 661176730Sjeffstruct cpuset * 662176730Sjeffcpuset_thread0(void) 663176730Sjeff{ 664176730Sjeff struct cpuset *set; 665176730Sjeff int error; 666176730Sjeff 667176730Sjeff cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 668176730Sjeff NULL, NULL, UMA_ALIGN_PTR, 0); 669176730Sjeff mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 670176730Sjeff /* 671176730Sjeff * Create the root system set for the whole machine. Doesn't use 672176730Sjeff * cpuset_create() due to NULL parent. 673176730Sjeff */ 674176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 675176730Sjeff set->cs_mask.__bits[0] = -1; 676176730Sjeff LIST_INIT(&set->cs_children); 677176730Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 678176730Sjeff set->cs_ref = 1; 679176730Sjeff set->cs_flags = CPU_SET_ROOT; 680176730Sjeff cpuset_zero = set; 681177738Sjeff cpuset_root = &set->cs_mask; 682176730Sjeff /* 683176730Sjeff * Now derive a default, modifiable set from that to give out. 684176730Sjeff */ 685176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK); 686176730Sjeff error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1); 687176730Sjeff KASSERT(error == 0, ("Error creating default set: %d\n", error)); 688176730Sjeff /* 689176730Sjeff * Initialize the unit allocator. 0 and 1 are allocated above. 690176730Sjeff */ 691176730Sjeff cpuset_unr = new_unrhdr(2, INT_MAX, NULL); 692176730Sjeff 693176730Sjeff return (set); 694176730Sjeff} 695176730Sjeff 696176730Sjeff/* 697185435Sbz * Create a cpuset, which would be cpuset_create() but 698185435Sbz * mark the new 'set' as root. 699185435Sbz * 700191403Sbz * We are not going to reparent the td to it. Use cpuset_setproc_update_set() 701191403Sbz * for that. 702185435Sbz * 703185435Sbz * In case of no error, returns the set in *setp locked with a reference. 704185435Sbz */ 705185435Sbzint 706192895Sjamiecpuset_create_root(struct prison *pr, struct cpuset **setp) 707185435Sbz{ 708185435Sbz struct cpuset *set; 709185435Sbz int error; 710185435Sbz 711192895Sjamie KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__)); 712185435Sbz KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 713185435Sbz 714192895Sjamie error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask); 715185435Sbz if (error) 716185435Sbz return (error); 717185435Sbz 718185435Sbz KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data", 719185435Sbz __func__, __LINE__)); 720185435Sbz 721185435Sbz /* Mark the set as root. */ 722185435Sbz set = *setp; 723185435Sbz set->cs_flags |= CPU_SET_ROOT; 724185435Sbz 725185435Sbz return (0); 726185435Sbz} 727185435Sbz 728185435Sbzint 729185435Sbzcpuset_setproc_update_set(struct proc *p, struct cpuset *set) 730185435Sbz{ 731185435Sbz int error; 732185435Sbz 733185435Sbz KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 734185435Sbz KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 735185435Sbz 736185435Sbz cpuset_ref(set); 737185435Sbz error = cpuset_setproc(p->p_pid, set, NULL); 738185435Sbz if (error) 739185435Sbz return (error); 740185435Sbz cpuset_rel(set); 741185435Sbz return (0); 742185435Sbz} 743185435Sbz 744185435Sbz/* 745176730Sjeff * This is called once the final set of system cpus is known. Modifies 746198493Sjhb * the root set and all children and mark the root read-only. 747176730Sjeff */ 748176730Sjeffstatic void 749176730Sjeffcpuset_init(void *arg) 750176730Sjeff{ 751176730Sjeff cpuset_t mask; 752176730Sjeff 753176730Sjeff CPU_ZERO(&mask); 754176730Sjeff#ifdef SMP 755176730Sjeff mask.__bits[0] = all_cpus; 756176730Sjeff#else 757176730Sjeff mask.__bits[0] = 1; 758176730Sjeff#endif 759176730Sjeff if (cpuset_modify(cpuset_zero, &mask)) 760176730Sjeff panic("Can't set initial cpuset mask.\n"); 761176730Sjeff cpuset_zero->cs_flags |= CPU_SET_RDONLY; 762176730Sjeff} 763176730SjeffSYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL); 764176730Sjeff 765176730Sjeff#ifndef _SYS_SYSPROTO_H_ 766176730Sjeffstruct cpuset_args { 767176730Sjeff cpusetid_t *setid; 768176730Sjeff}; 769176730Sjeff#endif 770176730Sjeffint 771176730Sjeffcpuset(struct thread *td, struct cpuset_args *uap) 772176730Sjeff{ 773176730Sjeff struct cpuset *root; 774176730Sjeff struct cpuset *set; 775176730Sjeff int error; 776176730Sjeff 777176730Sjeff thread_lock(td); 778177738Sjeff root = cpuset_refroot(td->td_cpuset); 779176730Sjeff thread_unlock(td); 780176730Sjeff error = cpuset_create(&set, root, &root->cs_mask); 781176730Sjeff cpuset_rel(root); 782176730Sjeff if (error) 783176730Sjeff return (error); 784177738Sjeff error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 785176730Sjeff if (error == 0) 786177738Sjeff error = cpuset_setproc(-1, set, NULL); 787176730Sjeff cpuset_rel(set); 788176730Sjeff return (error); 789176730Sjeff} 790176730Sjeff 791176730Sjeff#ifndef _SYS_SYSPROTO_H_ 792176730Sjeffstruct cpuset_setid_args { 793176730Sjeff cpuwhich_t which; 794176730Sjeff id_t id; 795176730Sjeff cpusetid_t setid; 796176730Sjeff}; 797176730Sjeff#endif 798176730Sjeffint 799176730Sjeffcpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 800176730Sjeff{ 801176730Sjeff struct cpuset *set; 802176730Sjeff int error; 803176730Sjeff 804176730Sjeff /* 805176730Sjeff * Presently we only support per-process sets. 806176730Sjeff */ 807176730Sjeff if (uap->which != CPU_WHICH_PID) 808176730Sjeff return (EINVAL); 809185435Sbz set = cpuset_lookup(uap->setid, td); 810176730Sjeff if (set == NULL) 811176730Sjeff return (ESRCH); 812176730Sjeff error = cpuset_setproc(uap->id, set, NULL); 813176730Sjeff cpuset_rel(set); 814176730Sjeff return (error); 815176730Sjeff} 816176730Sjeff 817176730Sjeff#ifndef _SYS_SYSPROTO_H_ 818176730Sjeffstruct cpuset_getid_args { 819176730Sjeff cpulevel_t level; 820176730Sjeff cpuwhich_t which; 821176730Sjeff id_t id; 822176730Sjeff cpusetid_t *setid; 823176730Sjeff#endif 824176730Sjeffint 825176730Sjeffcpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 826176730Sjeff{ 827176730Sjeff struct cpuset *nset; 828176730Sjeff struct cpuset *set; 829176730Sjeff struct thread *ttd; 830176730Sjeff struct proc *p; 831176730Sjeff cpusetid_t id; 832176730Sjeff int error; 833176730Sjeff 834176730Sjeff if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET) 835176730Sjeff return (EINVAL); 836176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 837176730Sjeff if (error) 838176730Sjeff return (error); 839176730Sjeff switch (uap->which) { 840176730Sjeff case CPU_WHICH_TID: 841176730Sjeff case CPU_WHICH_PID: 842176730Sjeff thread_lock(ttd); 843177738Sjeff set = cpuset_refbase(ttd->td_cpuset); 844176730Sjeff thread_unlock(ttd); 845176730Sjeff PROC_UNLOCK(p); 846176730Sjeff break; 847176730Sjeff case CPU_WHICH_CPUSET: 848185435Sbz case CPU_WHICH_JAIL: 849176730Sjeff break; 850178092Sjeff case CPU_WHICH_IRQ: 851178092Sjeff return (EINVAL); 852176730Sjeff } 853176730Sjeff switch (uap->level) { 854176730Sjeff case CPU_LEVEL_ROOT: 855177738Sjeff nset = cpuset_refroot(set); 856176730Sjeff cpuset_rel(set); 857176730Sjeff set = nset; 858176730Sjeff break; 859176730Sjeff case CPU_LEVEL_CPUSET: 860176730Sjeff break; 861176730Sjeff case CPU_LEVEL_WHICH: 862176730Sjeff break; 863176730Sjeff } 864176730Sjeff id = set->cs_id; 865176730Sjeff cpuset_rel(set); 866176730Sjeff if (error == 0) 867176730Sjeff error = copyout(&id, uap->setid, sizeof(id)); 868176730Sjeff 869176730Sjeff return (error); 870176730Sjeff} 871176730Sjeff 872176730Sjeff#ifndef _SYS_SYSPROTO_H_ 873176730Sjeffstruct cpuset_getaffinity_args { 874177597Sru cpulevel_t level; 875177597Sru cpuwhich_t which; 876177597Sru id_t id; 877177597Sru size_t cpusetsize; 878177597Sru cpuset_t *mask; 879176730Sjeff}; 880176730Sjeff#endif 881176730Sjeffint 882176730Sjeffcpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 883176730Sjeff{ 884176730Sjeff struct thread *ttd; 885176730Sjeff struct cpuset *nset; 886176730Sjeff struct cpuset *set; 887176730Sjeff struct proc *p; 888176730Sjeff cpuset_t *mask; 889176730Sjeff int error; 890177597Sru size_t size; 891176730Sjeff 892176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 893179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 894176730Sjeff return (ERANGE); 895176811Sjeff size = uap->cpusetsize; 896176730Sjeff mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 897176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 898176730Sjeff if (error) 899176730Sjeff goto out; 900176730Sjeff switch (uap->level) { 901176730Sjeff case CPU_LEVEL_ROOT: 902176730Sjeff case CPU_LEVEL_CPUSET: 903176730Sjeff switch (uap->which) { 904176730Sjeff case CPU_WHICH_TID: 905176730Sjeff case CPU_WHICH_PID: 906176730Sjeff thread_lock(ttd); 907176730Sjeff set = cpuset_ref(ttd->td_cpuset); 908176730Sjeff thread_unlock(ttd); 909176730Sjeff break; 910176730Sjeff case CPU_WHICH_CPUSET: 911185435Sbz case CPU_WHICH_JAIL: 912176730Sjeff break; 913178092Sjeff case CPU_WHICH_IRQ: 914178092Sjeff error = EINVAL; 915178092Sjeff goto out; 916176730Sjeff } 917176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 918177738Sjeff nset = cpuset_refroot(set); 919176730Sjeff else 920177738Sjeff nset = cpuset_refbase(set); 921176730Sjeff CPU_COPY(&nset->cs_mask, mask); 922176730Sjeff cpuset_rel(nset); 923176730Sjeff break; 924176730Sjeff case CPU_LEVEL_WHICH: 925176730Sjeff switch (uap->which) { 926176730Sjeff case CPU_WHICH_TID: 927176730Sjeff thread_lock(ttd); 928176730Sjeff CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 929176730Sjeff thread_unlock(ttd); 930176730Sjeff break; 931176730Sjeff case CPU_WHICH_PID: 932176730Sjeff FOREACH_THREAD_IN_PROC(p, ttd) { 933176730Sjeff thread_lock(ttd); 934176730Sjeff CPU_OR(mask, &ttd->td_cpuset->cs_mask); 935176730Sjeff thread_unlock(ttd); 936176730Sjeff } 937176730Sjeff break; 938176730Sjeff case CPU_WHICH_CPUSET: 939185435Sbz case CPU_WHICH_JAIL: 940176730Sjeff CPU_COPY(&set->cs_mask, mask); 941176730Sjeff break; 942178092Sjeff case CPU_WHICH_IRQ: 943178092Sjeff error = intr_getaffinity(uap->id, mask); 944178092Sjeff break; 945176730Sjeff } 946176730Sjeff break; 947176730Sjeff default: 948176730Sjeff error = EINVAL; 949176730Sjeff break; 950176730Sjeff } 951176730Sjeff if (set) 952176730Sjeff cpuset_rel(set); 953176730Sjeff if (p) 954176730Sjeff PROC_UNLOCK(p); 955176730Sjeff if (error == 0) 956176730Sjeff error = copyout(mask, uap->mask, size); 957176730Sjeffout: 958176730Sjeff free(mask, M_TEMP); 959176730Sjeff return (error); 960176730Sjeff} 961176730Sjeff 962176730Sjeff#ifndef _SYS_SYSPROTO_H_ 963176730Sjeffstruct cpuset_setaffinity_args { 964176730Sjeff cpulevel_t level; 965177597Sru cpuwhich_t which; 966177597Sru id_t id; 967177597Sru size_t cpusetsize; 968177597Sru const cpuset_t *mask; 969176730Sjeff}; 970176730Sjeff#endif 971176730Sjeffint 972176730Sjeffcpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 973176730Sjeff{ 974176730Sjeff struct cpuset *nset; 975176730Sjeff struct cpuset *set; 976176730Sjeff struct thread *ttd; 977176730Sjeff struct proc *p; 978176730Sjeff cpuset_t *mask; 979176730Sjeff int error; 980176730Sjeff 981176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 982179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 983176730Sjeff return (ERANGE); 984176811Sjeff mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO); 985176811Sjeff error = copyin(uap->mask, mask, uap->cpusetsize); 986176730Sjeff if (error) 987176730Sjeff goto out; 988176811Sjeff /* 989176811Sjeff * Verify that no high bits are set. 990176811Sjeff */ 991176811Sjeff if (uap->cpusetsize > sizeof(cpuset_t)) { 992176811Sjeff char *end; 993176811Sjeff char *cp; 994176811Sjeff 995176811Sjeff end = cp = (char *)&mask->__bits; 996176811Sjeff end += uap->cpusetsize; 997176811Sjeff cp += sizeof(cpuset_t); 998176811Sjeff while (cp != end) 999176811Sjeff if (*cp++ != 0) { 1000176811Sjeff error = EINVAL; 1001176811Sjeff goto out; 1002176811Sjeff } 1003176811Sjeff 1004176811Sjeff } 1005176730Sjeff switch (uap->level) { 1006176730Sjeff case CPU_LEVEL_ROOT: 1007176730Sjeff case CPU_LEVEL_CPUSET: 1008176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 1009176730Sjeff if (error) 1010176730Sjeff break; 1011176730Sjeff switch (uap->which) { 1012176730Sjeff case CPU_WHICH_TID: 1013176730Sjeff case CPU_WHICH_PID: 1014176730Sjeff thread_lock(ttd); 1015176730Sjeff set = cpuset_ref(ttd->td_cpuset); 1016176730Sjeff thread_unlock(ttd); 1017176880Sjeff PROC_UNLOCK(p); 1018176730Sjeff break; 1019176730Sjeff case CPU_WHICH_CPUSET: 1020185435Sbz case CPU_WHICH_JAIL: 1021176730Sjeff break; 1022178092Sjeff case CPU_WHICH_IRQ: 1023178092Sjeff error = EINVAL; 1024178092Sjeff goto out; 1025176730Sjeff } 1026176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 1027177738Sjeff nset = cpuset_refroot(set); 1028176730Sjeff else 1029177738Sjeff nset = cpuset_refbase(set); 1030176730Sjeff error = cpuset_modify(nset, mask); 1031176730Sjeff cpuset_rel(nset); 1032176730Sjeff cpuset_rel(set); 1033176730Sjeff break; 1034176730Sjeff case CPU_LEVEL_WHICH: 1035176730Sjeff switch (uap->which) { 1036176730Sjeff case CPU_WHICH_TID: 1037176730Sjeff error = cpuset_setthread(uap->id, mask); 1038176730Sjeff break; 1039176730Sjeff case CPU_WHICH_PID: 1040176730Sjeff error = cpuset_setproc(uap->id, NULL, mask); 1041176730Sjeff break; 1042176730Sjeff case CPU_WHICH_CPUSET: 1043185435Sbz case CPU_WHICH_JAIL: 1044185435Sbz error = cpuset_which(uap->which, uap->id, &p, 1045176730Sjeff &ttd, &set); 1046176730Sjeff if (error == 0) { 1047176730Sjeff error = cpuset_modify(set, mask); 1048176730Sjeff cpuset_rel(set); 1049176730Sjeff } 1050176730Sjeff break; 1051178092Sjeff case CPU_WHICH_IRQ: 1052178092Sjeff error = intr_setaffinity(uap->id, mask); 1053178092Sjeff break; 1054176730Sjeff default: 1055176730Sjeff error = EINVAL; 1056176730Sjeff break; 1057176730Sjeff } 1058176730Sjeff break; 1059176730Sjeff default: 1060176730Sjeff error = EINVAL; 1061176730Sjeff break; 1062176730Sjeff } 1063176730Sjeffout: 1064176730Sjeff free(mask, M_TEMP); 1065176730Sjeff return (error); 1066176730Sjeff} 1067180358Sbz 1068180358Sbz#ifdef DDB 1069180358SbzDB_SHOW_COMMAND(cpusets, db_show_cpusets) 1070180358Sbz{ 1071180358Sbz struct cpuset *set; 1072180358Sbz int cpu, once; 1073180358Sbz 1074180358Sbz LIST_FOREACH(set, &cpuset_ids, cs_link) { 1075180358Sbz db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 1076180358Sbz set, set->cs_id, set->cs_ref, set->cs_flags, 1077180358Sbz (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 1078180358Sbz db_printf(" mask="); 1079180358Sbz for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { 1080180358Sbz if (CPU_ISSET(cpu, &set->cs_mask)) { 1081180358Sbz if (once == 0) { 1082180358Sbz db_printf("%d", cpu); 1083180358Sbz once = 1; 1084180358Sbz } else 1085180358Sbz db_printf(",%d", cpu); 1086180358Sbz } 1087180358Sbz } 1088180358Sbz db_printf("\n"); 1089180358Sbz if (db_pager_quit) 1090180358Sbz break; 1091180358Sbz } 1092180358Sbz} 1093180358Sbz#endif /* DDB */ 1094