kern_cpuset.c revision 222813
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 222813 2011-06-07 08:46:13Z attilio $"); 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> 53222813Sattilio#include <sys/libkern.h> 54176730Sjeff#include <sys/limits.h> 55177738Sjeff#include <sys/bus.h> 56177738Sjeff#include <sys/interrupt.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 83198493Sjhb * illusion of each thread having a private mask. 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 * 91198495Sjhb * A thread may not be assigned to a group separate 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 102198493Sjhb * meaning 'curthread'. It may query available 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 111214611Sdavidxu/* Return the size of cpuset_t at the kernel level */ 112214611SdavidxuSYSCTL_INT(_kern_sched, OID_AUTO, cpusetsize, CTLFLAG_RD, 113214611Sdavidxu 0, sizeof(cpuset_t), "sizeof(cpuset_t)"); 114214611Sdavidxu 115177738Sjeffcpuset_t *cpuset_root; 116177738Sjeff 117176730Sjeff/* 118176730Sjeff * Acquire a reference to a cpuset, all pointers must be tracked with refs. 119176730Sjeff */ 120176730Sjeffstruct cpuset * 121176730Sjeffcpuset_ref(struct cpuset *set) 122176730Sjeff{ 123176730Sjeff 124176730Sjeff refcount_acquire(&set->cs_ref); 125176730Sjeff return (set); 126176730Sjeff} 127176730Sjeff 128176730Sjeff/* 129180356Sbz * Walks up the tree from 'set' to find the root. Returns the root 130180356Sbz * referenced. 131180356Sbz */ 132180356Sbzstatic struct cpuset * 133180356Sbzcpuset_refroot(struct cpuset *set) 134180356Sbz{ 135180356Sbz 136180356Sbz for (; set->cs_parent != NULL; set = set->cs_parent) 137180356Sbz if (set->cs_flags & CPU_SET_ROOT) 138180356Sbz break; 139180356Sbz cpuset_ref(set); 140180356Sbz 141180356Sbz return (set); 142180356Sbz} 143180356Sbz 144180356Sbz/* 145180356Sbz * Find the first non-anonymous set starting from 'set'. Returns this set 146180356Sbz * referenced. May return the passed in set with an extra ref if it is 147180356Sbz * not anonymous. 148180356Sbz */ 149180356Sbzstatic struct cpuset * 150180356Sbzcpuset_refbase(struct cpuset *set) 151180356Sbz{ 152180356Sbz 153180356Sbz if (set->cs_id == CPUSET_INVALID) 154180356Sbz set = set->cs_parent; 155180356Sbz cpuset_ref(set); 156180356Sbz 157180356Sbz return (set); 158180356Sbz} 159180356Sbz 160180356Sbz/* 161198493Sjhb * Release a reference in a context where it is safe to allocate. 162176730Sjeff */ 163176730Sjeffvoid 164176730Sjeffcpuset_rel(struct cpuset *set) 165176730Sjeff{ 166176730Sjeff cpusetid_t id; 167176730Sjeff 168176730Sjeff if (refcount_release(&set->cs_ref) == 0) 169176730Sjeff return; 170176730Sjeff mtx_lock_spin(&cpuset_lock); 171176730Sjeff LIST_REMOVE(set, cs_siblings); 172176730Sjeff id = set->cs_id; 173176730Sjeff if (id != CPUSET_INVALID) 174176730Sjeff LIST_REMOVE(set, cs_link); 175176730Sjeff mtx_unlock_spin(&cpuset_lock); 176176730Sjeff cpuset_rel(set->cs_parent); 177176730Sjeff uma_zfree(cpuset_zone, set); 178176730Sjeff if (id != CPUSET_INVALID) 179176730Sjeff free_unr(cpuset_unr, id); 180176730Sjeff} 181176730Sjeff 182176730Sjeff/* 183176730Sjeff * Deferred release must be used when in a context that is not safe to 184176730Sjeff * allocate/free. This places any unreferenced sets on the list 'head'. 185176730Sjeff */ 186176730Sjeffstatic void 187176730Sjeffcpuset_rel_defer(struct setlist *head, struct cpuset *set) 188176730Sjeff{ 189176730Sjeff 190176730Sjeff if (refcount_release(&set->cs_ref) == 0) 191176730Sjeff return; 192176730Sjeff mtx_lock_spin(&cpuset_lock); 193176730Sjeff LIST_REMOVE(set, cs_siblings); 194176730Sjeff if (set->cs_id != CPUSET_INVALID) 195176730Sjeff LIST_REMOVE(set, cs_link); 196176730Sjeff LIST_INSERT_HEAD(head, set, cs_link); 197176730Sjeff mtx_unlock_spin(&cpuset_lock); 198176730Sjeff} 199176730Sjeff 200176730Sjeff/* 201176730Sjeff * Complete a deferred release. Removes the set from the list provided to 202176730Sjeff * cpuset_rel_defer. 203176730Sjeff */ 204176730Sjeffstatic void 205176730Sjeffcpuset_rel_complete(struct cpuset *set) 206176730Sjeff{ 207176730Sjeff LIST_REMOVE(set, cs_link); 208176730Sjeff cpuset_rel(set->cs_parent); 209176730Sjeff uma_zfree(cpuset_zone, set); 210176730Sjeff} 211176730Sjeff 212176730Sjeff/* 213176730Sjeff * Find a set based on an id. Returns it with a ref. 214176730Sjeff */ 215176730Sjeffstatic struct cpuset * 216185435Sbzcpuset_lookup(cpusetid_t setid, struct thread *td) 217176730Sjeff{ 218176730Sjeff struct cpuset *set; 219176730Sjeff 220176730Sjeff if (setid == CPUSET_INVALID) 221176730Sjeff return (NULL); 222176730Sjeff mtx_lock_spin(&cpuset_lock); 223176730Sjeff LIST_FOREACH(set, &cpuset_ids, cs_link) 224176730Sjeff if (set->cs_id == setid) 225176730Sjeff break; 226176730Sjeff if (set) 227176730Sjeff cpuset_ref(set); 228176730Sjeff mtx_unlock_spin(&cpuset_lock); 229185435Sbz 230185435Sbz KASSERT(td != NULL, ("[%s:%d] td is NULL", __func__, __LINE__)); 231185435Sbz if (set != NULL && jailed(td->td_ucred)) { 232192895Sjamie struct cpuset *jset, *tset; 233185435Sbz 234192895Sjamie jset = td->td_ucred->cr_prison->pr_cpuset; 235192895Sjamie for (tset = set; tset != NULL; tset = tset->cs_parent) 236192895Sjamie if (tset == jset) 237192895Sjamie break; 238192895Sjamie if (tset == NULL) { 239185435Sbz cpuset_rel(set); 240185435Sbz set = NULL; 241185435Sbz } 242185435Sbz } 243185435Sbz 244176730Sjeff return (set); 245176730Sjeff} 246176730Sjeff 247176730Sjeff/* 248176730Sjeff * Create a set in the space provided in 'set' with the provided parameters. 249176730Sjeff * The set is returned with a single ref. May return EDEADLK if the set 250176730Sjeff * will have no valid cpu based on restrictions from the parent. 251176730Sjeff */ 252176730Sjeffstatic int 253219399Sjhb_cpuset_create(struct cpuset *set, struct cpuset *parent, const cpuset_t *mask, 254176730Sjeff cpusetid_t id) 255176730Sjeff{ 256176730Sjeff 257176811Sjeff if (!CPU_OVERLAP(&parent->cs_mask, mask)) 258176811Sjeff return (EDEADLK); 259176730Sjeff CPU_COPY(mask, &set->cs_mask); 260176730Sjeff LIST_INIT(&set->cs_children); 261176730Sjeff refcount_init(&set->cs_ref, 1); 262176730Sjeff set->cs_flags = 0; 263176730Sjeff mtx_lock_spin(&cpuset_lock); 264219399Sjhb CPU_AND(&set->cs_mask, &parent->cs_mask); 265176811Sjeff set->cs_id = id; 266176811Sjeff set->cs_parent = cpuset_ref(parent); 267176811Sjeff LIST_INSERT_HEAD(&parent->cs_children, set, cs_siblings); 268176811Sjeff if (set->cs_id != CPUSET_INVALID) 269176811Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 270176730Sjeff mtx_unlock_spin(&cpuset_lock); 271176730Sjeff 272176811Sjeff return (0); 273176730Sjeff} 274176730Sjeff 275176730Sjeff/* 276176730Sjeff * Create a new non-anonymous set with the requested parent and mask. May 277176730Sjeff * return failures if the mask is invalid or a new number can not be 278176730Sjeff * allocated. 279176730Sjeff */ 280176730Sjeffstatic int 281219399Sjhbcpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask) 282176730Sjeff{ 283176730Sjeff struct cpuset *set; 284176730Sjeff cpusetid_t id; 285176730Sjeff int error; 286176730Sjeff 287176730Sjeff id = alloc_unr(cpuset_unr); 288176730Sjeff if (id == -1) 289176730Sjeff return (ENFILE); 290176730Sjeff *setp = set = uma_zalloc(cpuset_zone, M_WAITOK); 291176730Sjeff error = _cpuset_create(set, parent, mask, id); 292176730Sjeff if (error == 0) 293176730Sjeff return (0); 294176730Sjeff free_unr(cpuset_unr, id); 295176730Sjeff uma_zfree(cpuset_zone, set); 296176730Sjeff 297176730Sjeff return (error); 298176730Sjeff} 299176730Sjeff 300176730Sjeff/* 301176730Sjeff * Recursively check for errors that would occur from applying mask to 302176730Sjeff * the tree of sets starting at 'set'. Checks for sets that would become 303176730Sjeff * empty as well as RDONLY flags. 304176730Sjeff */ 305176730Sjeffstatic int 306176730Sjeffcpuset_testupdate(struct cpuset *set, cpuset_t *mask) 307176730Sjeff{ 308176730Sjeff struct cpuset *nset; 309176730Sjeff cpuset_t newmask; 310176730Sjeff int error; 311176730Sjeff 312176730Sjeff mtx_assert(&cpuset_lock, MA_OWNED); 313176730Sjeff if (set->cs_flags & CPU_SET_RDONLY) 314176730Sjeff return (EPERM); 315176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, mask)) 316176811Sjeff return (EDEADLK); 317176730Sjeff CPU_COPY(&set->cs_mask, &newmask); 318176730Sjeff CPU_AND(&newmask, mask); 319176811Sjeff error = 0; 320176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 321176730Sjeff if ((error = cpuset_testupdate(nset, &newmask)) != 0) 322176730Sjeff break; 323176730Sjeff return (error); 324176730Sjeff} 325176730Sjeff 326176730Sjeff/* 327176730Sjeff * Applies the mask 'mask' without checking for empty sets or permissions. 328176730Sjeff */ 329176730Sjeffstatic void 330176730Sjeffcpuset_update(struct cpuset *set, cpuset_t *mask) 331176730Sjeff{ 332176730Sjeff struct cpuset *nset; 333176730Sjeff 334176730Sjeff mtx_assert(&cpuset_lock, MA_OWNED); 335176730Sjeff CPU_AND(&set->cs_mask, mask); 336176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 337176730Sjeff cpuset_update(nset, &set->cs_mask); 338176730Sjeff 339176730Sjeff return; 340176730Sjeff} 341176730Sjeff 342176730Sjeff/* 343176730Sjeff * Modify the set 'set' to use a copy of the mask provided. Apply this new 344176730Sjeff * mask to restrict all children in the tree. Checks for validity before 345176730Sjeff * applying the changes. 346176730Sjeff */ 347176730Sjeffstatic int 348176730Sjeffcpuset_modify(struct cpuset *set, cpuset_t *mask) 349176730Sjeff{ 350176811Sjeff struct cpuset *root; 351176730Sjeff int error; 352176730Sjeff 353180098Sbz error = priv_check(curthread, PRIV_SCHED_CPUSET); 354176730Sjeff if (error) 355176730Sjeff return (error); 356176811Sjeff /* 357191639Sbz * In case we are called from within the jail 358191639Sbz * we do not allow modifying the dedicated root 359191639Sbz * cpuset of the jail but may still allow to 360191639Sbz * change child sets. 361191639Sbz */ 362191639Sbz if (jailed(curthread->td_ucred) && 363191639Sbz set->cs_flags & CPU_SET_ROOT) 364191639Sbz return (EPERM); 365191639Sbz /* 366176811Sjeff * Verify that we have access to this set of 367176811Sjeff * cpus. 368176811Sjeff */ 369176811Sjeff root = set->cs_parent; 370176811Sjeff if (root && !CPU_SUBSET(&root->cs_mask, mask)) 371176811Sjeff return (EINVAL); 372176730Sjeff mtx_lock_spin(&cpuset_lock); 373176730Sjeff error = cpuset_testupdate(set, mask); 374176730Sjeff if (error) 375176730Sjeff goto out; 376176730Sjeff cpuset_update(set, mask); 377176730Sjeff CPU_COPY(mask, &set->cs_mask); 378176730Sjeffout: 379176730Sjeff mtx_unlock_spin(&cpuset_lock); 380176730Sjeff 381176730Sjeff return (error); 382176730Sjeff} 383176730Sjeff 384176730Sjeff/* 385176730Sjeff * Resolve the 'which' parameter of several cpuset apis. 386176730Sjeff * 387176730Sjeff * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 388176730Sjeff * checks for permission via p_cansched(). 389176730Sjeff * 390176730Sjeff * For WHICH_SET returns a valid set with a new reference. 391176730Sjeff * 392176730Sjeff * -1 may be supplied for any argument to mean the current proc/thread or 393176730Sjeff * the base set of the current thread. May fail with ESRCH/EPERM. 394176730Sjeff */ 395176730Sjeffstatic int 396176730Sjeffcpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 397176730Sjeff struct cpuset **setp) 398176730Sjeff{ 399176730Sjeff struct cpuset *set; 400176730Sjeff struct thread *td; 401176730Sjeff struct proc *p; 402176730Sjeff int error; 403176730Sjeff 404176730Sjeff *pp = p = NULL; 405176730Sjeff *tdp = td = NULL; 406176730Sjeff *setp = set = NULL; 407176730Sjeff switch (which) { 408176730Sjeff case CPU_WHICH_PID: 409176730Sjeff if (id == -1) { 410176730Sjeff PROC_LOCK(curproc); 411176730Sjeff p = curproc; 412176730Sjeff break; 413176730Sjeff } 414176730Sjeff if ((p = pfind(id)) == NULL) 415176730Sjeff return (ESRCH); 416176730Sjeff break; 417176730Sjeff case CPU_WHICH_TID: 418176730Sjeff if (id == -1) { 419176730Sjeff PROC_LOCK(curproc); 420176730Sjeff p = curproc; 421176730Sjeff td = curthread; 422176730Sjeff break; 423176730Sjeff } 424214337Sdavidxu td = tdfind(id, -1); 425176730Sjeff if (td == NULL) 426176730Sjeff return (ESRCH); 427214337Sdavidxu p = td->td_proc; 428176730Sjeff break; 429176730Sjeff case CPU_WHICH_CPUSET: 430176730Sjeff if (id == -1) { 431176730Sjeff thread_lock(curthread); 432177738Sjeff set = cpuset_refbase(curthread->td_cpuset); 433176730Sjeff thread_unlock(curthread); 434176730Sjeff } else 435185435Sbz set = cpuset_lookup(id, curthread); 436176730Sjeff if (set) { 437176730Sjeff *setp = set; 438176730Sjeff return (0); 439176730Sjeff } 440176730Sjeff return (ESRCH); 441185435Sbz case CPU_WHICH_JAIL: 442185435Sbz { 443185435Sbz /* Find `set' for prison with given id. */ 444185435Sbz struct prison *pr; 445185435Sbz 446185435Sbz sx_slock(&allprison_lock); 447192895Sjamie pr = prison_find_child(curthread->td_ucred->cr_prison, id); 448185435Sbz sx_sunlock(&allprison_lock); 449185435Sbz if (pr == NULL) 450185435Sbz return (ESRCH); 451192895Sjamie cpuset_ref(pr->pr_cpuset); 452192895Sjamie *setp = pr->pr_cpuset; 453185435Sbz mtx_unlock(&pr->pr_mtx); 454192895Sjamie return (0); 455185435Sbz } 456178092Sjeff case CPU_WHICH_IRQ: 457178092Sjeff return (0); 458176730Sjeff default: 459176730Sjeff return (EINVAL); 460176730Sjeff } 461176730Sjeff error = p_cansched(curthread, p); 462176730Sjeff if (error) { 463176730Sjeff PROC_UNLOCK(p); 464176730Sjeff return (error); 465176730Sjeff } 466176730Sjeff if (td == NULL) 467176730Sjeff td = FIRST_THREAD_IN_PROC(p); 468176730Sjeff *pp = p; 469176730Sjeff *tdp = td; 470176730Sjeff return (0); 471176730Sjeff} 472176730Sjeff 473176730Sjeff/* 474176730Sjeff * Create an anonymous set with the provided mask in the space provided by 475176730Sjeff * 'fset'. If the passed in set is anonymous we use its parent otherwise 476176730Sjeff * the new set is a child of 'set'. 477176730Sjeff */ 478176730Sjeffstatic int 479219399Sjhbcpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask) 480176730Sjeff{ 481176730Sjeff struct cpuset *parent; 482176730Sjeff 483176730Sjeff if (set->cs_id == CPUSET_INVALID) 484176730Sjeff parent = set->cs_parent; 485176730Sjeff else 486176730Sjeff parent = set; 487176811Sjeff if (!CPU_SUBSET(&parent->cs_mask, mask)) 488177738Sjeff return (EDEADLK); 489176730Sjeff return (_cpuset_create(fset, parent, mask, CPUSET_INVALID)); 490176730Sjeff} 491176730Sjeff 492176730Sjeff/* 493176730Sjeff * Handle two cases for replacing the base set or mask of an entire process. 494176730Sjeff * 495176730Sjeff * 1) Set is non-null and mask is null. This reparents all anonymous sets 496176730Sjeff * to the provided set and replaces all non-anonymous td_cpusets with the 497176730Sjeff * provided set. 498176730Sjeff * 2) Mask is non-null and set is null. This replaces or creates anonymous 499176730Sjeff * sets for every thread with the existing base as a parent. 500176730Sjeff * 501176730Sjeff * This is overly complicated because we can't allocate while holding a 502176730Sjeff * spinlock and spinlocks must be held while changing and examining thread 503176730Sjeff * state. 504176730Sjeff */ 505176730Sjeffstatic int 506176730Sjeffcpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask) 507176730Sjeff{ 508176730Sjeff struct setlist freelist; 509176730Sjeff struct setlist droplist; 510176811Sjeff struct cpuset *tdset; 511176730Sjeff struct cpuset *nset; 512176730Sjeff struct thread *td; 513176730Sjeff struct proc *p; 514176730Sjeff int threads; 515176730Sjeff int nfree; 516176730Sjeff int error; 517176730Sjeff /* 518176730Sjeff * The algorithm requires two passes due to locking considerations. 519176730Sjeff * 520176730Sjeff * 1) Lookup the process and acquire the locks in the required order. 521176730Sjeff * 2) If enough cpusets have not been allocated release the locks and 522176730Sjeff * allocate them. Loop. 523176730Sjeff */ 524176730Sjeff LIST_INIT(&freelist); 525176730Sjeff LIST_INIT(&droplist); 526176730Sjeff nfree = 0; 527176730Sjeff for (;;) { 528176730Sjeff error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 529176730Sjeff if (error) 530176730Sjeff goto out; 531176730Sjeff if (nfree >= p->p_numthreads) 532176730Sjeff break; 533176730Sjeff threads = p->p_numthreads; 534176730Sjeff PROC_UNLOCK(p); 535176730Sjeff for (; nfree < threads; nfree++) { 536176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 537176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 538176730Sjeff } 539176730Sjeff } 540176730Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 541176730Sjeff /* 542176730Sjeff * Now that the appropriate locks are held and we have enough cpusets, 543176811Sjeff * make sure the operation will succeed before applying changes. The 544176811Sjeff * proc lock prevents td_cpuset from changing between calls. 545176811Sjeff */ 546176811Sjeff error = 0; 547176811Sjeff FOREACH_THREAD_IN_PROC(p, td) { 548176811Sjeff thread_lock(td); 549176811Sjeff tdset = td->td_cpuset; 550176811Sjeff /* 551176811Sjeff * Verify that a new mask doesn't specify cpus outside of 552176811Sjeff * the set the thread is a member of. 553176811Sjeff */ 554176811Sjeff if (mask) { 555176811Sjeff if (tdset->cs_id == CPUSET_INVALID) 556176811Sjeff tdset = tdset->cs_parent; 557176811Sjeff if (!CPU_SUBSET(&tdset->cs_mask, mask)) 558177738Sjeff error = EDEADLK; 559176811Sjeff /* 560176811Sjeff * Verify that a new set won't leave an existing thread 561176811Sjeff * mask without a cpu to run on. It can, however, restrict 562176811Sjeff * the set. 563176811Sjeff */ 564176811Sjeff } else if (tdset->cs_id == CPUSET_INVALID) { 565176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask)) 566177738Sjeff error = EDEADLK; 567176811Sjeff } 568176811Sjeff thread_unlock(td); 569176811Sjeff if (error) 570176811Sjeff goto unlock_out; 571176811Sjeff } 572176811Sjeff /* 573176811Sjeff * Replace each thread's cpuset while using deferred release. We 574177368Sjeff * must do this because the thread lock must be held while operating 575177368Sjeff * on the thread and this limits the type of operations allowed. 576176730Sjeff */ 577176730Sjeff FOREACH_THREAD_IN_PROC(p, td) { 578176730Sjeff thread_lock(td); 579176730Sjeff /* 580176730Sjeff * If we presently have an anonymous set or are applying a 581176730Sjeff * mask we must create an anonymous shadow set. That is 582176730Sjeff * either parented to our existing base or the supplied set. 583176730Sjeff * 584176730Sjeff * If we have a base set with no anonymous shadow we simply 585176730Sjeff * replace it outright. 586176730Sjeff */ 587176730Sjeff tdset = td->td_cpuset; 588176730Sjeff if (tdset->cs_id == CPUSET_INVALID || mask) { 589176730Sjeff nset = LIST_FIRST(&freelist); 590176730Sjeff LIST_REMOVE(nset, cs_link); 591176730Sjeff if (mask) 592176730Sjeff error = cpuset_shadow(tdset, nset, mask); 593176730Sjeff else 594176730Sjeff error = _cpuset_create(nset, set, 595176730Sjeff &tdset->cs_mask, CPUSET_INVALID); 596176730Sjeff if (error) { 597176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 598176730Sjeff thread_unlock(td); 599176730Sjeff break; 600176730Sjeff } 601176730Sjeff } else 602176730Sjeff nset = cpuset_ref(set); 603176730Sjeff cpuset_rel_defer(&droplist, tdset); 604176730Sjeff td->td_cpuset = nset; 605176730Sjeff sched_affinity(td); 606176730Sjeff thread_unlock(td); 607176730Sjeff } 608176811Sjeffunlock_out: 609176730Sjeff PROC_UNLOCK(p); 610176730Sjeffout: 611176730Sjeff while ((nset = LIST_FIRST(&droplist)) != NULL) 612176730Sjeff cpuset_rel_complete(nset); 613176730Sjeff while ((nset = LIST_FIRST(&freelist)) != NULL) { 614176730Sjeff LIST_REMOVE(nset, cs_link); 615176730Sjeff uma_zfree(cpuset_zone, nset); 616176730Sjeff } 617176730Sjeff return (error); 618176730Sjeff} 619176730Sjeff 620176730Sjeff/* 621222813Sattilio * Calculate the ffs() of the cpuset. 622222813Sattilio */ 623222813Sattilioint 624222813Sattiliocpusetobj_ffs(const cpuset_t *set) 625222813Sattilio{ 626222813Sattilio size_t i; 627222813Sattilio int cbit; 628222813Sattilio 629222813Sattilio cbit = 0; 630222813Sattilio for (i = 0; i < _NCPUWORDS; i++) { 631222813Sattilio if (set->__bits[i] != 0) { 632222813Sattilio cbit = ffsl(set->__bits[i]); 633222813Sattilio cbit += i * _NCPUBITS; 634222813Sattilio break; 635222813Sattilio } 636222813Sattilio } 637222813Sattilio return (cbit); 638222813Sattilio} 639222813Sattilio 640222813Sattilio/* 641222813Sattilio * Return a string representing a valid layout for a cpuset_t object. 642222813Sattilio * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 643222813Sattilio */ 644222813Sattiliochar * 645222813Sattiliocpusetobj_strprint(char *buf, const cpuset_t *set) 646222813Sattilio{ 647222813Sattilio char *tbuf; 648222813Sattilio size_t i, bytesp, bufsiz; 649222813Sattilio 650222813Sattilio tbuf = buf; 651222813Sattilio bytesp = 0; 652222813Sattilio bufsiz = CPUSETBUFSIZ; 653222813Sattilio 654222813Sattilio for (i = _NCPUWORDS - 1; i > 0; i--) { 655222813Sattilio bytesp = snprintf(tbuf, bufsiz, "%lx, ", set->__bits[i]); 656222813Sattilio bufsiz -= bytesp; 657222813Sattilio tbuf += bytesp; 658222813Sattilio } 659222813Sattilio snprintf(tbuf, bufsiz, "%lx", set->__bits[0]); 660222813Sattilio return (buf); 661222813Sattilio} 662222813Sattilio 663222813Sattilio/* 664222813Sattilio * Build a valid cpuset_t object from a string representation. 665222813Sattilio * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 666222813Sattilio */ 667222813Sattilioint 668222813Sattiliocpusetobj_strscan(cpuset_t *set, const char *buf) 669222813Sattilio{ 670222813Sattilio u_int nwords; 671222813Sattilio int i, ret; 672222813Sattilio 673222813Sattilio if (strlen(buf) > CPUSETBUFSIZ - 1) 674222813Sattilio return (-1); 675222813Sattilio 676222813Sattilio /* Allow to pass a shorter version of the mask when necessary. */ 677222813Sattilio nwords = 1; 678222813Sattilio for (i = 0; buf[i] != '\0'; i++) 679222813Sattilio if (buf[i] == ',') 680222813Sattilio nwords++; 681222813Sattilio if (nwords > _NCPUWORDS) 682222813Sattilio return (-1); 683222813Sattilio 684222813Sattilio CPU_ZERO(set); 685222813Sattilio for (i = nwords - 1; i > 0; i--) { 686222813Sattilio ret = sscanf(buf, "%lx, ", &set->__bits[i]); 687222813Sattilio if (ret == 0 || ret == -1) 688222813Sattilio return (-1); 689222813Sattilio buf = strstr(buf, " "); 690222813Sattilio if (buf == NULL) 691222813Sattilio return (-1); 692222813Sattilio buf++; 693222813Sattilio } 694222813Sattilio ret = sscanf(buf, "%lx", &set->__bits[0]); 695222813Sattilio if (ret == 0 || ret == -1) 696222813Sattilio return (-1); 697222813Sattilio return (0); 698222813Sattilio} 699222813Sattilio 700222813Sattilio/* 701176730Sjeff * Apply an anonymous mask to a single thread. 702176730Sjeff */ 703177738Sjeffint 704176730Sjeffcpuset_setthread(lwpid_t id, cpuset_t *mask) 705176730Sjeff{ 706176730Sjeff struct cpuset *nset; 707176730Sjeff struct cpuset *set; 708176730Sjeff struct thread *td; 709176730Sjeff struct proc *p; 710176730Sjeff int error; 711176730Sjeff 712176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 713176821Sjeff error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 714176730Sjeff if (error) 715176730Sjeff goto out; 716177738Sjeff set = NULL; 717176730Sjeff thread_lock(td); 718177738Sjeff error = cpuset_shadow(td->td_cpuset, nset, mask); 719176730Sjeff if (error == 0) { 720177738Sjeff set = td->td_cpuset; 721176730Sjeff td->td_cpuset = nset; 722176730Sjeff sched_affinity(td); 723176730Sjeff nset = NULL; 724176730Sjeff } 725176730Sjeff thread_unlock(td); 726176730Sjeff PROC_UNLOCK(p); 727177738Sjeff if (set) 728177738Sjeff cpuset_rel(set); 729176730Sjeffout: 730176730Sjeff if (nset) 731176730Sjeff uma_zfree(cpuset_zone, nset); 732176730Sjeff return (error); 733176730Sjeff} 734176730Sjeff 735176730Sjeff/* 736176730Sjeff * Creates the cpuset for thread0. We make two sets: 737176730Sjeff * 738176730Sjeff * 0 - The root set which should represent all valid processors in the 739176730Sjeff * system. It is initially created with a mask of all processors 740176730Sjeff * because we don't know what processors are valid until cpuset_init() 741176730Sjeff * runs. This set is immutable. 742176730Sjeff * 1 - The default set which all processes are a member of until changed. 743176730Sjeff * This allows an administrator to move all threads off of given cpus to 744176730Sjeff * dedicate them to high priority tasks or save power etc. 745176730Sjeff */ 746176730Sjeffstruct cpuset * 747176730Sjeffcpuset_thread0(void) 748176730Sjeff{ 749176730Sjeff struct cpuset *set; 750176730Sjeff int error; 751176730Sjeff 752176730Sjeff cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 753176730Sjeff NULL, NULL, UMA_ALIGN_PTR, 0); 754176730Sjeff mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 755176730Sjeff /* 756176730Sjeff * Create the root system set for the whole machine. Doesn't use 757176730Sjeff * cpuset_create() due to NULL parent. 758176730Sjeff */ 759176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 760222201Sattilio CPU_FILL(&set->cs_mask); 761176730Sjeff LIST_INIT(&set->cs_children); 762176730Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 763176730Sjeff set->cs_ref = 1; 764176730Sjeff set->cs_flags = CPU_SET_ROOT; 765176730Sjeff cpuset_zero = set; 766177738Sjeff cpuset_root = &set->cs_mask; 767176730Sjeff /* 768176730Sjeff * Now derive a default, modifiable set from that to give out. 769176730Sjeff */ 770176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK); 771176730Sjeff error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1); 772176730Sjeff KASSERT(error == 0, ("Error creating default set: %d\n", error)); 773176730Sjeff /* 774176730Sjeff * Initialize the unit allocator. 0 and 1 are allocated above. 775176730Sjeff */ 776176730Sjeff cpuset_unr = new_unrhdr(2, INT_MAX, NULL); 777176730Sjeff 778176730Sjeff return (set); 779176730Sjeff} 780176730Sjeff 781176730Sjeff/* 782185435Sbz * Create a cpuset, which would be cpuset_create() but 783185435Sbz * mark the new 'set' as root. 784185435Sbz * 785191403Sbz * We are not going to reparent the td to it. Use cpuset_setproc_update_set() 786191403Sbz * for that. 787185435Sbz * 788185435Sbz * In case of no error, returns the set in *setp locked with a reference. 789185435Sbz */ 790185435Sbzint 791192895Sjamiecpuset_create_root(struct prison *pr, struct cpuset **setp) 792185435Sbz{ 793185435Sbz struct cpuset *set; 794185435Sbz int error; 795185435Sbz 796192895Sjamie KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__)); 797185435Sbz KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 798185435Sbz 799192895Sjamie error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask); 800185435Sbz if (error) 801185435Sbz return (error); 802185435Sbz 803185435Sbz KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data", 804185435Sbz __func__, __LINE__)); 805185435Sbz 806185435Sbz /* Mark the set as root. */ 807185435Sbz set = *setp; 808185435Sbz set->cs_flags |= CPU_SET_ROOT; 809185435Sbz 810185435Sbz return (0); 811185435Sbz} 812185435Sbz 813185435Sbzint 814185435Sbzcpuset_setproc_update_set(struct proc *p, struct cpuset *set) 815185435Sbz{ 816185435Sbz int error; 817185435Sbz 818185435Sbz KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 819185435Sbz KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 820185435Sbz 821185435Sbz cpuset_ref(set); 822185435Sbz error = cpuset_setproc(p->p_pid, set, NULL); 823185435Sbz if (error) 824185435Sbz return (error); 825185435Sbz cpuset_rel(set); 826185435Sbz return (0); 827185435Sbz} 828185435Sbz 829185435Sbz/* 830176730Sjeff * This is called once the final set of system cpus is known. Modifies 831198493Sjhb * the root set and all children and mark the root read-only. 832176730Sjeff */ 833176730Sjeffstatic void 834176730Sjeffcpuset_init(void *arg) 835176730Sjeff{ 836176730Sjeff cpuset_t mask; 837176730Sjeff 838222813Sattilio mask = all_cpus; 839176730Sjeff if (cpuset_modify(cpuset_zero, &mask)) 840176730Sjeff panic("Can't set initial cpuset mask.\n"); 841176730Sjeff cpuset_zero->cs_flags |= CPU_SET_RDONLY; 842176730Sjeff} 843176730SjeffSYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL); 844176730Sjeff 845176730Sjeff#ifndef _SYS_SYSPROTO_H_ 846176730Sjeffstruct cpuset_args { 847176730Sjeff cpusetid_t *setid; 848176730Sjeff}; 849176730Sjeff#endif 850176730Sjeffint 851176730Sjeffcpuset(struct thread *td, struct cpuset_args *uap) 852176730Sjeff{ 853176730Sjeff struct cpuset *root; 854176730Sjeff struct cpuset *set; 855176730Sjeff int error; 856176730Sjeff 857176730Sjeff thread_lock(td); 858177738Sjeff root = cpuset_refroot(td->td_cpuset); 859176730Sjeff thread_unlock(td); 860176730Sjeff error = cpuset_create(&set, root, &root->cs_mask); 861176730Sjeff cpuset_rel(root); 862176730Sjeff if (error) 863176730Sjeff return (error); 864177738Sjeff error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 865176730Sjeff if (error == 0) 866177738Sjeff error = cpuset_setproc(-1, set, NULL); 867176730Sjeff cpuset_rel(set); 868176730Sjeff return (error); 869176730Sjeff} 870176730Sjeff 871176730Sjeff#ifndef _SYS_SYSPROTO_H_ 872176730Sjeffstruct cpuset_setid_args { 873176730Sjeff cpuwhich_t which; 874176730Sjeff id_t id; 875176730Sjeff cpusetid_t setid; 876176730Sjeff}; 877176730Sjeff#endif 878176730Sjeffint 879176730Sjeffcpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 880176730Sjeff{ 881176730Sjeff struct cpuset *set; 882176730Sjeff int error; 883176730Sjeff 884176730Sjeff /* 885176730Sjeff * Presently we only support per-process sets. 886176730Sjeff */ 887176730Sjeff if (uap->which != CPU_WHICH_PID) 888176730Sjeff return (EINVAL); 889185435Sbz set = cpuset_lookup(uap->setid, td); 890176730Sjeff if (set == NULL) 891176730Sjeff return (ESRCH); 892176730Sjeff error = cpuset_setproc(uap->id, set, NULL); 893176730Sjeff cpuset_rel(set); 894176730Sjeff return (error); 895176730Sjeff} 896176730Sjeff 897176730Sjeff#ifndef _SYS_SYSPROTO_H_ 898176730Sjeffstruct cpuset_getid_args { 899176730Sjeff cpulevel_t level; 900176730Sjeff cpuwhich_t which; 901176730Sjeff id_t id; 902176730Sjeff cpusetid_t *setid; 903176730Sjeff#endif 904176730Sjeffint 905176730Sjeffcpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 906176730Sjeff{ 907176730Sjeff struct cpuset *nset; 908176730Sjeff struct cpuset *set; 909176730Sjeff struct thread *ttd; 910176730Sjeff struct proc *p; 911176730Sjeff cpusetid_t id; 912176730Sjeff int error; 913176730Sjeff 914176730Sjeff if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET) 915176730Sjeff return (EINVAL); 916176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 917176730Sjeff if (error) 918176730Sjeff return (error); 919176730Sjeff switch (uap->which) { 920176730Sjeff case CPU_WHICH_TID: 921176730Sjeff case CPU_WHICH_PID: 922176730Sjeff thread_lock(ttd); 923177738Sjeff set = cpuset_refbase(ttd->td_cpuset); 924176730Sjeff thread_unlock(ttd); 925176730Sjeff PROC_UNLOCK(p); 926176730Sjeff break; 927176730Sjeff case CPU_WHICH_CPUSET: 928185435Sbz case CPU_WHICH_JAIL: 929176730Sjeff break; 930178092Sjeff case CPU_WHICH_IRQ: 931178092Sjeff return (EINVAL); 932176730Sjeff } 933176730Sjeff switch (uap->level) { 934176730Sjeff case CPU_LEVEL_ROOT: 935177738Sjeff nset = cpuset_refroot(set); 936176730Sjeff cpuset_rel(set); 937176730Sjeff set = nset; 938176730Sjeff break; 939176730Sjeff case CPU_LEVEL_CPUSET: 940176730Sjeff break; 941176730Sjeff case CPU_LEVEL_WHICH: 942176730Sjeff break; 943176730Sjeff } 944176730Sjeff id = set->cs_id; 945176730Sjeff cpuset_rel(set); 946176730Sjeff if (error == 0) 947176730Sjeff error = copyout(&id, uap->setid, sizeof(id)); 948176730Sjeff 949176730Sjeff return (error); 950176730Sjeff} 951176730Sjeff 952176730Sjeff#ifndef _SYS_SYSPROTO_H_ 953176730Sjeffstruct cpuset_getaffinity_args { 954177597Sru cpulevel_t level; 955177597Sru cpuwhich_t which; 956177597Sru id_t id; 957177597Sru size_t cpusetsize; 958177597Sru cpuset_t *mask; 959176730Sjeff}; 960176730Sjeff#endif 961176730Sjeffint 962176730Sjeffcpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 963176730Sjeff{ 964176730Sjeff struct thread *ttd; 965176730Sjeff struct cpuset *nset; 966176730Sjeff struct cpuset *set; 967176730Sjeff struct proc *p; 968176730Sjeff cpuset_t *mask; 969176730Sjeff int error; 970177597Sru size_t size; 971176730Sjeff 972176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 973179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 974176730Sjeff return (ERANGE); 975176811Sjeff size = uap->cpusetsize; 976176730Sjeff mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 977176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 978176730Sjeff if (error) 979176730Sjeff goto out; 980176730Sjeff switch (uap->level) { 981176730Sjeff case CPU_LEVEL_ROOT: 982176730Sjeff case CPU_LEVEL_CPUSET: 983176730Sjeff switch (uap->which) { 984176730Sjeff case CPU_WHICH_TID: 985176730Sjeff case CPU_WHICH_PID: 986176730Sjeff thread_lock(ttd); 987176730Sjeff set = cpuset_ref(ttd->td_cpuset); 988176730Sjeff thread_unlock(ttd); 989176730Sjeff break; 990176730Sjeff case CPU_WHICH_CPUSET: 991185435Sbz case CPU_WHICH_JAIL: 992176730Sjeff break; 993178092Sjeff case CPU_WHICH_IRQ: 994178092Sjeff error = EINVAL; 995178092Sjeff goto out; 996176730Sjeff } 997176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 998177738Sjeff nset = cpuset_refroot(set); 999176730Sjeff else 1000177738Sjeff nset = cpuset_refbase(set); 1001176730Sjeff CPU_COPY(&nset->cs_mask, mask); 1002176730Sjeff cpuset_rel(nset); 1003176730Sjeff break; 1004176730Sjeff case CPU_LEVEL_WHICH: 1005176730Sjeff switch (uap->which) { 1006176730Sjeff case CPU_WHICH_TID: 1007176730Sjeff thread_lock(ttd); 1008176730Sjeff CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 1009176730Sjeff thread_unlock(ttd); 1010176730Sjeff break; 1011176730Sjeff case CPU_WHICH_PID: 1012176730Sjeff FOREACH_THREAD_IN_PROC(p, ttd) { 1013176730Sjeff thread_lock(ttd); 1014176730Sjeff CPU_OR(mask, &ttd->td_cpuset->cs_mask); 1015176730Sjeff thread_unlock(ttd); 1016176730Sjeff } 1017176730Sjeff break; 1018176730Sjeff case CPU_WHICH_CPUSET: 1019185435Sbz case CPU_WHICH_JAIL: 1020176730Sjeff CPU_COPY(&set->cs_mask, mask); 1021176730Sjeff break; 1022178092Sjeff case CPU_WHICH_IRQ: 1023178092Sjeff error = intr_getaffinity(uap->id, mask); 1024178092Sjeff break; 1025176730Sjeff } 1026176730Sjeff break; 1027176730Sjeff default: 1028176730Sjeff error = EINVAL; 1029176730Sjeff break; 1030176730Sjeff } 1031176730Sjeff if (set) 1032176730Sjeff cpuset_rel(set); 1033176730Sjeff if (p) 1034176730Sjeff PROC_UNLOCK(p); 1035176730Sjeff if (error == 0) 1036176730Sjeff error = copyout(mask, uap->mask, size); 1037176730Sjeffout: 1038176730Sjeff free(mask, M_TEMP); 1039176730Sjeff return (error); 1040176730Sjeff} 1041176730Sjeff 1042176730Sjeff#ifndef _SYS_SYSPROTO_H_ 1043176730Sjeffstruct cpuset_setaffinity_args { 1044176730Sjeff cpulevel_t level; 1045177597Sru cpuwhich_t which; 1046177597Sru id_t id; 1047177597Sru size_t cpusetsize; 1048177597Sru const cpuset_t *mask; 1049176730Sjeff}; 1050176730Sjeff#endif 1051176730Sjeffint 1052176730Sjeffcpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 1053176730Sjeff{ 1054176730Sjeff struct cpuset *nset; 1055176730Sjeff struct cpuset *set; 1056176730Sjeff struct thread *ttd; 1057176730Sjeff struct proc *p; 1058176730Sjeff cpuset_t *mask; 1059176730Sjeff int error; 1060176730Sjeff 1061176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 1062179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 1063176730Sjeff return (ERANGE); 1064176811Sjeff mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO); 1065176811Sjeff error = copyin(uap->mask, mask, uap->cpusetsize); 1066176730Sjeff if (error) 1067176730Sjeff goto out; 1068176811Sjeff /* 1069176811Sjeff * Verify that no high bits are set. 1070176811Sjeff */ 1071176811Sjeff if (uap->cpusetsize > sizeof(cpuset_t)) { 1072176811Sjeff char *end; 1073176811Sjeff char *cp; 1074176811Sjeff 1075176811Sjeff end = cp = (char *)&mask->__bits; 1076176811Sjeff end += uap->cpusetsize; 1077176811Sjeff cp += sizeof(cpuset_t); 1078176811Sjeff while (cp != end) 1079176811Sjeff if (*cp++ != 0) { 1080176811Sjeff error = EINVAL; 1081176811Sjeff goto out; 1082176811Sjeff } 1083176811Sjeff 1084176811Sjeff } 1085176730Sjeff switch (uap->level) { 1086176730Sjeff case CPU_LEVEL_ROOT: 1087176730Sjeff case CPU_LEVEL_CPUSET: 1088176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 1089176730Sjeff if (error) 1090176730Sjeff break; 1091176730Sjeff switch (uap->which) { 1092176730Sjeff case CPU_WHICH_TID: 1093176730Sjeff case CPU_WHICH_PID: 1094176730Sjeff thread_lock(ttd); 1095176730Sjeff set = cpuset_ref(ttd->td_cpuset); 1096176730Sjeff thread_unlock(ttd); 1097176880Sjeff PROC_UNLOCK(p); 1098176730Sjeff break; 1099176730Sjeff case CPU_WHICH_CPUSET: 1100185435Sbz case CPU_WHICH_JAIL: 1101176730Sjeff break; 1102178092Sjeff case CPU_WHICH_IRQ: 1103178092Sjeff error = EINVAL; 1104178092Sjeff goto out; 1105176730Sjeff } 1106176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 1107177738Sjeff nset = cpuset_refroot(set); 1108176730Sjeff else 1109177738Sjeff nset = cpuset_refbase(set); 1110176730Sjeff error = cpuset_modify(nset, mask); 1111176730Sjeff cpuset_rel(nset); 1112176730Sjeff cpuset_rel(set); 1113176730Sjeff break; 1114176730Sjeff case CPU_LEVEL_WHICH: 1115176730Sjeff switch (uap->which) { 1116176730Sjeff case CPU_WHICH_TID: 1117176730Sjeff error = cpuset_setthread(uap->id, mask); 1118176730Sjeff break; 1119176730Sjeff case CPU_WHICH_PID: 1120176730Sjeff error = cpuset_setproc(uap->id, NULL, mask); 1121176730Sjeff break; 1122176730Sjeff case CPU_WHICH_CPUSET: 1123185435Sbz case CPU_WHICH_JAIL: 1124185435Sbz error = cpuset_which(uap->which, uap->id, &p, 1125176730Sjeff &ttd, &set); 1126176730Sjeff if (error == 0) { 1127176730Sjeff error = cpuset_modify(set, mask); 1128176730Sjeff cpuset_rel(set); 1129176730Sjeff } 1130176730Sjeff break; 1131178092Sjeff case CPU_WHICH_IRQ: 1132178092Sjeff error = intr_setaffinity(uap->id, mask); 1133178092Sjeff break; 1134176730Sjeff default: 1135176730Sjeff error = EINVAL; 1136176730Sjeff break; 1137176730Sjeff } 1138176730Sjeff break; 1139176730Sjeff default: 1140176730Sjeff error = EINVAL; 1141176730Sjeff break; 1142176730Sjeff } 1143176730Sjeffout: 1144176730Sjeff free(mask, M_TEMP); 1145176730Sjeff return (error); 1146176730Sjeff} 1147180358Sbz 1148180358Sbz#ifdef DDB 1149180358SbzDB_SHOW_COMMAND(cpusets, db_show_cpusets) 1150180358Sbz{ 1151180358Sbz struct cpuset *set; 1152180358Sbz int cpu, once; 1153180358Sbz 1154180358Sbz LIST_FOREACH(set, &cpuset_ids, cs_link) { 1155180358Sbz db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 1156180358Sbz set, set->cs_id, set->cs_ref, set->cs_flags, 1157180358Sbz (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 1158180358Sbz db_printf(" mask="); 1159180358Sbz for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { 1160180358Sbz if (CPU_ISSET(cpu, &set->cs_mask)) { 1161180358Sbz if (once == 0) { 1162180358Sbz db_printf("%d", cpu); 1163180358Sbz once = 1; 1164180358Sbz } else 1165180358Sbz db_printf(",%d", cpu); 1166180358Sbz } 1167180358Sbz } 1168180358Sbz db_printf("\n"); 1169180358Sbz if (db_pager_quit) 1170180358Sbz break; 1171180358Sbz } 1172180358Sbz} 1173180358Sbz#endif /* DDB */ 1174