kern_cpuset.c revision 251470
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 251470 2013-06-06 14:43:19Z jhb $"); 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 306251470Sjhbcpuset_testupdate(struct cpuset *set, cpuset_t *mask, int check_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); 315251470Sjhb if (check_mask) { 316251470Sjhb if (!CPU_OVERLAP(&set->cs_mask, mask)) 317251470Sjhb return (EDEADLK); 318251470Sjhb CPU_COPY(&set->cs_mask, &newmask); 319251470Sjhb CPU_AND(&newmask, mask); 320251470Sjhb } else 321251470Sjhb CPU_COPY(mask, &newmask); 322176811Sjeff error = 0; 323176730Sjeff LIST_FOREACH(nset, &set->cs_children, cs_siblings) 324251470Sjhb if ((error = cpuset_testupdate(nset, &newmask, 1)) != 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 /* 360191639Sbz * In case we are called from within the jail 361191639Sbz * we do not allow modifying the dedicated root 362191639Sbz * cpuset of the jail but may still allow to 363191639Sbz * change child sets. 364191639Sbz */ 365191639Sbz if (jailed(curthread->td_ucred) && 366191639Sbz set->cs_flags & CPU_SET_ROOT) 367191639Sbz return (EPERM); 368191639Sbz /* 369176811Sjeff * Verify that we have access to this set of 370176811Sjeff * cpus. 371176811Sjeff */ 372176811Sjeff root = set->cs_parent; 373176811Sjeff if (root && !CPU_SUBSET(&root->cs_mask, mask)) 374176811Sjeff return (EINVAL); 375176730Sjeff mtx_lock_spin(&cpuset_lock); 376251470Sjhb error = cpuset_testupdate(set, mask, 0); 377176730Sjeff if (error) 378176730Sjeff goto out; 379251470Sjhb CPU_COPY(mask, &set->cs_mask); 380176730Sjeff cpuset_update(set, mask); 381176730Sjeffout: 382176730Sjeff mtx_unlock_spin(&cpuset_lock); 383176730Sjeff 384176730Sjeff return (error); 385176730Sjeff} 386176730Sjeff 387176730Sjeff/* 388176730Sjeff * Resolve the 'which' parameter of several cpuset apis. 389176730Sjeff * 390176730Sjeff * For WHICH_PID and WHICH_TID return a locked proc and valid proc/tid. Also 391176730Sjeff * checks for permission via p_cansched(). 392176730Sjeff * 393176730Sjeff * For WHICH_SET returns a valid set with a new reference. 394176730Sjeff * 395176730Sjeff * -1 may be supplied for any argument to mean the current proc/thread or 396176730Sjeff * the base set of the current thread. May fail with ESRCH/EPERM. 397176730Sjeff */ 398176730Sjeffstatic int 399176730Sjeffcpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, 400176730Sjeff struct cpuset **setp) 401176730Sjeff{ 402176730Sjeff struct cpuset *set; 403176730Sjeff struct thread *td; 404176730Sjeff struct proc *p; 405176730Sjeff int error; 406176730Sjeff 407176730Sjeff *pp = p = NULL; 408176730Sjeff *tdp = td = NULL; 409176730Sjeff *setp = set = NULL; 410176730Sjeff switch (which) { 411176730Sjeff case CPU_WHICH_PID: 412176730Sjeff if (id == -1) { 413176730Sjeff PROC_LOCK(curproc); 414176730Sjeff p = curproc; 415176730Sjeff break; 416176730Sjeff } 417176730Sjeff if ((p = pfind(id)) == NULL) 418176730Sjeff return (ESRCH); 419176730Sjeff break; 420176730Sjeff case CPU_WHICH_TID: 421176730Sjeff if (id == -1) { 422176730Sjeff PROC_LOCK(curproc); 423176730Sjeff p = curproc; 424176730Sjeff td = curthread; 425176730Sjeff break; 426176730Sjeff } 427214337Sdavidxu td = tdfind(id, -1); 428176730Sjeff if (td == NULL) 429176730Sjeff return (ESRCH); 430214337Sdavidxu p = td->td_proc; 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); 450192895Sjamie pr = prison_find_child(curthread->td_ucred->cr_prison, id); 451185435Sbz sx_sunlock(&allprison_lock); 452185435Sbz if (pr == NULL) 453185435Sbz return (ESRCH); 454192895Sjamie cpuset_ref(pr->pr_cpuset); 455192895Sjamie *setp = pr->pr_cpuset; 456185435Sbz mtx_unlock(&pr->pr_mtx); 457192895Sjamie return (0); 458185435Sbz } 459178092Sjeff case CPU_WHICH_IRQ: 460178092Sjeff return (0); 461176730Sjeff default: 462176730Sjeff return (EINVAL); 463176730Sjeff } 464176730Sjeff error = p_cansched(curthread, p); 465176730Sjeff if (error) { 466176730Sjeff PROC_UNLOCK(p); 467176730Sjeff return (error); 468176730Sjeff } 469176730Sjeff if (td == NULL) 470176730Sjeff td = FIRST_THREAD_IN_PROC(p); 471176730Sjeff *pp = p; 472176730Sjeff *tdp = td; 473176730Sjeff return (0); 474176730Sjeff} 475176730Sjeff 476176730Sjeff/* 477176730Sjeff * Create an anonymous set with the provided mask in the space provided by 478176730Sjeff * 'fset'. If the passed in set is anonymous we use its parent otherwise 479176730Sjeff * the new set is a child of 'set'. 480176730Sjeff */ 481176730Sjeffstatic int 482219399Sjhbcpuset_shadow(struct cpuset *set, struct cpuset *fset, const cpuset_t *mask) 483176730Sjeff{ 484176730Sjeff struct cpuset *parent; 485176730Sjeff 486176730Sjeff if (set->cs_id == CPUSET_INVALID) 487176730Sjeff parent = set->cs_parent; 488176730Sjeff else 489176730Sjeff parent = set; 490176811Sjeff if (!CPU_SUBSET(&parent->cs_mask, mask)) 491177738Sjeff return (EDEADLK); 492176730Sjeff return (_cpuset_create(fset, parent, mask, CPUSET_INVALID)); 493176730Sjeff} 494176730Sjeff 495176730Sjeff/* 496176730Sjeff * Handle two cases for replacing the base set or mask of an entire process. 497176730Sjeff * 498176730Sjeff * 1) Set is non-null and mask is null. This reparents all anonymous sets 499176730Sjeff * to the provided set and replaces all non-anonymous td_cpusets with the 500176730Sjeff * provided set. 501176730Sjeff * 2) Mask is non-null and set is null. This replaces or creates anonymous 502176730Sjeff * sets for every thread with the existing base as a parent. 503176730Sjeff * 504176730Sjeff * This is overly complicated because we can't allocate while holding a 505176730Sjeff * spinlock and spinlocks must be held while changing and examining thread 506176730Sjeff * state. 507176730Sjeff */ 508176730Sjeffstatic int 509176730Sjeffcpuset_setproc(pid_t pid, struct cpuset *set, cpuset_t *mask) 510176730Sjeff{ 511176730Sjeff struct setlist freelist; 512176730Sjeff struct setlist droplist; 513176811Sjeff struct cpuset *tdset; 514176730Sjeff struct cpuset *nset; 515176730Sjeff struct thread *td; 516176730Sjeff struct proc *p; 517176730Sjeff int threads; 518176730Sjeff int nfree; 519176730Sjeff int error; 520176730Sjeff /* 521176730Sjeff * The algorithm requires two passes due to locking considerations. 522176730Sjeff * 523176730Sjeff * 1) Lookup the process and acquire the locks in the required order. 524176730Sjeff * 2) If enough cpusets have not been allocated release the locks and 525176730Sjeff * allocate them. Loop. 526176730Sjeff */ 527176730Sjeff LIST_INIT(&freelist); 528176730Sjeff LIST_INIT(&droplist); 529176730Sjeff nfree = 0; 530176730Sjeff for (;;) { 531176730Sjeff error = cpuset_which(CPU_WHICH_PID, pid, &p, &td, &nset); 532176730Sjeff if (error) 533176730Sjeff goto out; 534176730Sjeff if (nfree >= p->p_numthreads) 535176730Sjeff break; 536176730Sjeff threads = p->p_numthreads; 537176730Sjeff PROC_UNLOCK(p); 538176730Sjeff for (; nfree < threads; nfree++) { 539176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 540176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 541176730Sjeff } 542176730Sjeff } 543176730Sjeff PROC_LOCK_ASSERT(p, MA_OWNED); 544176730Sjeff /* 545176730Sjeff * Now that the appropriate locks are held and we have enough cpusets, 546176811Sjeff * make sure the operation will succeed before applying changes. The 547176811Sjeff * proc lock prevents td_cpuset from changing between calls. 548176811Sjeff */ 549176811Sjeff error = 0; 550176811Sjeff FOREACH_THREAD_IN_PROC(p, td) { 551176811Sjeff thread_lock(td); 552176811Sjeff tdset = td->td_cpuset; 553176811Sjeff /* 554176811Sjeff * Verify that a new mask doesn't specify cpus outside of 555176811Sjeff * the set the thread is a member of. 556176811Sjeff */ 557176811Sjeff if (mask) { 558176811Sjeff if (tdset->cs_id == CPUSET_INVALID) 559176811Sjeff tdset = tdset->cs_parent; 560176811Sjeff if (!CPU_SUBSET(&tdset->cs_mask, mask)) 561177738Sjeff error = EDEADLK; 562176811Sjeff /* 563176811Sjeff * Verify that a new set won't leave an existing thread 564176811Sjeff * mask without a cpu to run on. It can, however, restrict 565176811Sjeff * the set. 566176811Sjeff */ 567176811Sjeff } else if (tdset->cs_id == CPUSET_INVALID) { 568176811Sjeff if (!CPU_OVERLAP(&set->cs_mask, &tdset->cs_mask)) 569177738Sjeff error = EDEADLK; 570176811Sjeff } 571176811Sjeff thread_unlock(td); 572176811Sjeff if (error) 573176811Sjeff goto unlock_out; 574176811Sjeff } 575176811Sjeff /* 576176811Sjeff * Replace each thread's cpuset while using deferred release. We 577177368Sjeff * must do this because the thread lock must be held while operating 578177368Sjeff * on the thread and this limits the type of operations allowed. 579176730Sjeff */ 580176730Sjeff FOREACH_THREAD_IN_PROC(p, td) { 581176730Sjeff thread_lock(td); 582176730Sjeff /* 583176730Sjeff * If we presently have an anonymous set or are applying a 584176730Sjeff * mask we must create an anonymous shadow set. That is 585176730Sjeff * either parented to our existing base or the supplied set. 586176730Sjeff * 587176730Sjeff * If we have a base set with no anonymous shadow we simply 588176730Sjeff * replace it outright. 589176730Sjeff */ 590176730Sjeff tdset = td->td_cpuset; 591176730Sjeff if (tdset->cs_id == CPUSET_INVALID || mask) { 592176730Sjeff nset = LIST_FIRST(&freelist); 593176730Sjeff LIST_REMOVE(nset, cs_link); 594176730Sjeff if (mask) 595176730Sjeff error = cpuset_shadow(tdset, nset, mask); 596176730Sjeff else 597176730Sjeff error = _cpuset_create(nset, set, 598176730Sjeff &tdset->cs_mask, CPUSET_INVALID); 599176730Sjeff if (error) { 600176730Sjeff LIST_INSERT_HEAD(&freelist, nset, cs_link); 601176730Sjeff thread_unlock(td); 602176730Sjeff break; 603176730Sjeff } 604176730Sjeff } else 605176730Sjeff nset = cpuset_ref(set); 606176730Sjeff cpuset_rel_defer(&droplist, tdset); 607176730Sjeff td->td_cpuset = nset; 608176730Sjeff sched_affinity(td); 609176730Sjeff thread_unlock(td); 610176730Sjeff } 611176811Sjeffunlock_out: 612176730Sjeff PROC_UNLOCK(p); 613176730Sjeffout: 614176730Sjeff while ((nset = LIST_FIRST(&droplist)) != NULL) 615176730Sjeff cpuset_rel_complete(nset); 616176730Sjeff while ((nset = LIST_FIRST(&freelist)) != NULL) { 617176730Sjeff LIST_REMOVE(nset, cs_link); 618176730Sjeff uma_zfree(cpuset_zone, nset); 619176730Sjeff } 620176730Sjeff return (error); 621176730Sjeff} 622176730Sjeff 623176730Sjeff/* 624222813Sattilio * Calculate the ffs() of the cpuset. 625222813Sattilio */ 626222813Sattilioint 627222813Sattiliocpusetobj_ffs(const cpuset_t *set) 628222813Sattilio{ 629222813Sattilio size_t i; 630222813Sattilio int cbit; 631222813Sattilio 632222813Sattilio cbit = 0; 633222813Sattilio for (i = 0; i < _NCPUWORDS; i++) { 634222813Sattilio if (set->__bits[i] != 0) { 635222813Sattilio cbit = ffsl(set->__bits[i]); 636222813Sattilio cbit += i * _NCPUBITS; 637222813Sattilio break; 638222813Sattilio } 639222813Sattilio } 640222813Sattilio return (cbit); 641222813Sattilio} 642222813Sattilio 643222813Sattilio/* 644222813Sattilio * Return a string representing a valid layout for a cpuset_t object. 645222813Sattilio * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 646222813Sattilio */ 647222813Sattiliochar * 648222813Sattiliocpusetobj_strprint(char *buf, const cpuset_t *set) 649222813Sattilio{ 650222813Sattilio char *tbuf; 651222813Sattilio size_t i, bytesp, bufsiz; 652222813Sattilio 653222813Sattilio tbuf = buf; 654222813Sattilio bytesp = 0; 655222813Sattilio bufsiz = CPUSETBUFSIZ; 656222813Sattilio 657239923Sattilio for (i = 0; i < (_NCPUWORDS - 1); i++) { 658239923Sattilio bytesp = snprintf(tbuf, bufsiz, "%lx,", set->__bits[i]); 659222813Sattilio bufsiz -= bytesp; 660222813Sattilio tbuf += bytesp; 661222813Sattilio } 662239923Sattilio snprintf(tbuf, bufsiz, "%lx", set->__bits[_NCPUWORDS - 1]); 663222813Sattilio return (buf); 664222813Sattilio} 665222813Sattilio 666222813Sattilio/* 667222813Sattilio * Build a valid cpuset_t object from a string representation. 668222813Sattilio * It expects an incoming buffer at least sized as CPUSETBUFSIZ. 669222813Sattilio */ 670222813Sattilioint 671222813Sattiliocpusetobj_strscan(cpuset_t *set, const char *buf) 672222813Sattilio{ 673222813Sattilio u_int nwords; 674222813Sattilio int i, ret; 675222813Sattilio 676222813Sattilio if (strlen(buf) > CPUSETBUFSIZ - 1) 677222813Sattilio return (-1); 678222813Sattilio 679222813Sattilio /* Allow to pass a shorter version of the mask when necessary. */ 680222813Sattilio nwords = 1; 681222813Sattilio for (i = 0; buf[i] != '\0'; i++) 682222813Sattilio if (buf[i] == ',') 683222813Sattilio nwords++; 684222813Sattilio if (nwords > _NCPUWORDS) 685222813Sattilio return (-1); 686222813Sattilio 687222813Sattilio CPU_ZERO(set); 688239923Sattilio for (i = 0; i < (nwords - 1); i++) { 689239923Sattilio ret = sscanf(buf, "%lx,", &set->__bits[i]); 690222813Sattilio if (ret == 0 || ret == -1) 691222813Sattilio return (-1); 692239923Sattilio buf = strstr(buf, ","); 693222813Sattilio if (buf == NULL) 694222813Sattilio return (-1); 695222813Sattilio buf++; 696222813Sattilio } 697239923Sattilio ret = sscanf(buf, "%lx", &set->__bits[nwords - 1]); 698222813Sattilio if (ret == 0 || ret == -1) 699222813Sattilio return (-1); 700222813Sattilio return (0); 701222813Sattilio} 702222813Sattilio 703222813Sattilio/* 704176730Sjeff * Apply an anonymous mask to a single thread. 705176730Sjeff */ 706177738Sjeffint 707176730Sjeffcpuset_setthread(lwpid_t id, cpuset_t *mask) 708176730Sjeff{ 709176730Sjeff struct cpuset *nset; 710176730Sjeff struct cpuset *set; 711176730Sjeff struct thread *td; 712176730Sjeff struct proc *p; 713176730Sjeff int error; 714176730Sjeff 715176730Sjeff nset = uma_zalloc(cpuset_zone, M_WAITOK); 716176821Sjeff error = cpuset_which(CPU_WHICH_TID, id, &p, &td, &set); 717176730Sjeff if (error) 718176730Sjeff goto out; 719177738Sjeff set = NULL; 720176730Sjeff thread_lock(td); 721177738Sjeff error = cpuset_shadow(td->td_cpuset, nset, mask); 722176730Sjeff if (error == 0) { 723177738Sjeff set = td->td_cpuset; 724176730Sjeff td->td_cpuset = nset; 725176730Sjeff sched_affinity(td); 726176730Sjeff nset = NULL; 727176730Sjeff } 728176730Sjeff thread_unlock(td); 729176730Sjeff PROC_UNLOCK(p); 730177738Sjeff if (set) 731177738Sjeff cpuset_rel(set); 732176730Sjeffout: 733176730Sjeff if (nset) 734176730Sjeff uma_zfree(cpuset_zone, nset); 735176730Sjeff return (error); 736176730Sjeff} 737176730Sjeff 738176730Sjeff/* 739176730Sjeff * Creates the cpuset for thread0. We make two sets: 740176730Sjeff * 741176730Sjeff * 0 - The root set which should represent all valid processors in the 742176730Sjeff * system. It is initially created with a mask of all processors 743176730Sjeff * because we don't know what processors are valid until cpuset_init() 744176730Sjeff * runs. This set is immutable. 745176730Sjeff * 1 - The default set which all processes are a member of until changed. 746176730Sjeff * This allows an administrator to move all threads off of given cpus to 747176730Sjeff * dedicate them to high priority tasks or save power etc. 748176730Sjeff */ 749176730Sjeffstruct cpuset * 750176730Sjeffcpuset_thread0(void) 751176730Sjeff{ 752176730Sjeff struct cpuset *set; 753176730Sjeff int error; 754176730Sjeff 755176730Sjeff cpuset_zone = uma_zcreate("cpuset", sizeof(struct cpuset), NULL, NULL, 756176730Sjeff NULL, NULL, UMA_ALIGN_PTR, 0); 757176730Sjeff mtx_init(&cpuset_lock, "cpuset", NULL, MTX_SPIN | MTX_RECURSE); 758176730Sjeff /* 759176730Sjeff * Create the root system set for the whole machine. Doesn't use 760176730Sjeff * cpuset_create() due to NULL parent. 761176730Sjeff */ 762176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO); 763222201Sattilio CPU_FILL(&set->cs_mask); 764176730Sjeff LIST_INIT(&set->cs_children); 765176730Sjeff LIST_INSERT_HEAD(&cpuset_ids, set, cs_link); 766176730Sjeff set->cs_ref = 1; 767176730Sjeff set->cs_flags = CPU_SET_ROOT; 768176730Sjeff cpuset_zero = set; 769177738Sjeff cpuset_root = &set->cs_mask; 770176730Sjeff /* 771176730Sjeff * Now derive a default, modifiable set from that to give out. 772176730Sjeff */ 773176730Sjeff set = uma_zalloc(cpuset_zone, M_WAITOK); 774176730Sjeff error = _cpuset_create(set, cpuset_zero, &cpuset_zero->cs_mask, 1); 775176730Sjeff KASSERT(error == 0, ("Error creating default set: %d\n", error)); 776176730Sjeff /* 777176730Sjeff * Initialize the unit allocator. 0 and 1 are allocated above. 778176730Sjeff */ 779176730Sjeff cpuset_unr = new_unrhdr(2, INT_MAX, NULL); 780176730Sjeff 781176730Sjeff return (set); 782176730Sjeff} 783176730Sjeff 784176730Sjeff/* 785185435Sbz * Create a cpuset, which would be cpuset_create() but 786185435Sbz * mark the new 'set' as root. 787185435Sbz * 788191403Sbz * We are not going to reparent the td to it. Use cpuset_setproc_update_set() 789191403Sbz * for that. 790185435Sbz * 791185435Sbz * In case of no error, returns the set in *setp locked with a reference. 792185435Sbz */ 793185435Sbzint 794192895Sjamiecpuset_create_root(struct prison *pr, struct cpuset **setp) 795185435Sbz{ 796185435Sbz struct cpuset *set; 797185435Sbz int error; 798185435Sbz 799192895Sjamie KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__)); 800185435Sbz KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__)); 801185435Sbz 802192895Sjamie error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask); 803185435Sbz if (error) 804185435Sbz return (error); 805185435Sbz 806185435Sbz KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data", 807185435Sbz __func__, __LINE__)); 808185435Sbz 809185435Sbz /* Mark the set as root. */ 810185435Sbz set = *setp; 811185435Sbz set->cs_flags |= CPU_SET_ROOT; 812185435Sbz 813185435Sbz return (0); 814185435Sbz} 815185435Sbz 816185435Sbzint 817185435Sbzcpuset_setproc_update_set(struct proc *p, struct cpuset *set) 818185435Sbz{ 819185435Sbz int error; 820185435Sbz 821185435Sbz KASSERT(p != NULL, ("[%s:%d] invalid proc", __func__, __LINE__)); 822185435Sbz KASSERT(set != NULL, ("[%s:%d] invalid set", __func__, __LINE__)); 823185435Sbz 824185435Sbz cpuset_ref(set); 825185435Sbz error = cpuset_setproc(p->p_pid, set, NULL); 826185435Sbz if (error) 827185435Sbz return (error); 828185435Sbz cpuset_rel(set); 829185435Sbz return (0); 830185435Sbz} 831185435Sbz 832185435Sbz/* 833176730Sjeff * This is called once the final set of system cpus is known. Modifies 834198493Sjhb * the root set and all children and mark the root read-only. 835176730Sjeff */ 836176730Sjeffstatic void 837176730Sjeffcpuset_init(void *arg) 838176730Sjeff{ 839176730Sjeff cpuset_t mask; 840176730Sjeff 841222813Sattilio mask = all_cpus; 842176730Sjeff if (cpuset_modify(cpuset_zero, &mask)) 843176730Sjeff panic("Can't set initial cpuset mask.\n"); 844176730Sjeff cpuset_zero->cs_flags |= CPU_SET_RDONLY; 845176730Sjeff} 846176730SjeffSYSINIT(cpuset, SI_SUB_SMP, SI_ORDER_ANY, cpuset_init, NULL); 847176730Sjeff 848176730Sjeff#ifndef _SYS_SYSPROTO_H_ 849176730Sjeffstruct cpuset_args { 850176730Sjeff cpusetid_t *setid; 851176730Sjeff}; 852176730Sjeff#endif 853176730Sjeffint 854225617Skmacysys_cpuset(struct thread *td, struct cpuset_args *uap) 855176730Sjeff{ 856176730Sjeff struct cpuset *root; 857176730Sjeff struct cpuset *set; 858176730Sjeff int error; 859176730Sjeff 860176730Sjeff thread_lock(td); 861177738Sjeff root = cpuset_refroot(td->td_cpuset); 862176730Sjeff thread_unlock(td); 863176730Sjeff error = cpuset_create(&set, root, &root->cs_mask); 864176730Sjeff cpuset_rel(root); 865176730Sjeff if (error) 866176730Sjeff return (error); 867177738Sjeff error = copyout(&set->cs_id, uap->setid, sizeof(set->cs_id)); 868176730Sjeff if (error == 0) 869177738Sjeff error = cpuset_setproc(-1, set, NULL); 870176730Sjeff cpuset_rel(set); 871176730Sjeff return (error); 872176730Sjeff} 873176730Sjeff 874176730Sjeff#ifndef _SYS_SYSPROTO_H_ 875176730Sjeffstruct cpuset_setid_args { 876176730Sjeff cpuwhich_t which; 877176730Sjeff id_t id; 878176730Sjeff cpusetid_t setid; 879176730Sjeff}; 880176730Sjeff#endif 881176730Sjeffint 882225617Skmacysys_cpuset_setid(struct thread *td, struct cpuset_setid_args *uap) 883176730Sjeff{ 884176730Sjeff struct cpuset *set; 885176730Sjeff int error; 886176730Sjeff 887176730Sjeff /* 888176730Sjeff * Presently we only support per-process sets. 889176730Sjeff */ 890176730Sjeff if (uap->which != CPU_WHICH_PID) 891176730Sjeff return (EINVAL); 892185435Sbz set = cpuset_lookup(uap->setid, td); 893176730Sjeff if (set == NULL) 894176730Sjeff return (ESRCH); 895176730Sjeff error = cpuset_setproc(uap->id, set, NULL); 896176730Sjeff cpuset_rel(set); 897176730Sjeff return (error); 898176730Sjeff} 899176730Sjeff 900176730Sjeff#ifndef _SYS_SYSPROTO_H_ 901176730Sjeffstruct cpuset_getid_args { 902176730Sjeff cpulevel_t level; 903176730Sjeff cpuwhich_t which; 904176730Sjeff id_t id; 905176730Sjeff cpusetid_t *setid; 906228275Skevlo}; 907176730Sjeff#endif 908176730Sjeffint 909225617Skmacysys_cpuset_getid(struct thread *td, struct cpuset_getid_args *uap) 910176730Sjeff{ 911176730Sjeff struct cpuset *nset; 912176730Sjeff struct cpuset *set; 913176730Sjeff struct thread *ttd; 914176730Sjeff struct proc *p; 915176730Sjeff cpusetid_t id; 916176730Sjeff int error; 917176730Sjeff 918176730Sjeff if (uap->level == CPU_LEVEL_WHICH && uap->which != CPU_WHICH_CPUSET) 919176730Sjeff return (EINVAL); 920176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 921176730Sjeff if (error) 922176730Sjeff return (error); 923176730Sjeff switch (uap->which) { 924176730Sjeff case CPU_WHICH_TID: 925176730Sjeff case CPU_WHICH_PID: 926176730Sjeff thread_lock(ttd); 927177738Sjeff set = cpuset_refbase(ttd->td_cpuset); 928176730Sjeff thread_unlock(ttd); 929176730Sjeff PROC_UNLOCK(p); 930176730Sjeff break; 931176730Sjeff case CPU_WHICH_CPUSET: 932185435Sbz case CPU_WHICH_JAIL: 933176730Sjeff break; 934178092Sjeff case CPU_WHICH_IRQ: 935178092Sjeff return (EINVAL); 936176730Sjeff } 937176730Sjeff switch (uap->level) { 938176730Sjeff case CPU_LEVEL_ROOT: 939177738Sjeff nset = cpuset_refroot(set); 940176730Sjeff cpuset_rel(set); 941176730Sjeff set = nset; 942176730Sjeff break; 943176730Sjeff case CPU_LEVEL_CPUSET: 944176730Sjeff break; 945176730Sjeff case CPU_LEVEL_WHICH: 946176730Sjeff break; 947176730Sjeff } 948176730Sjeff id = set->cs_id; 949176730Sjeff cpuset_rel(set); 950176730Sjeff if (error == 0) 951176730Sjeff error = copyout(&id, uap->setid, sizeof(id)); 952176730Sjeff 953176730Sjeff return (error); 954176730Sjeff} 955176730Sjeff 956176730Sjeff#ifndef _SYS_SYSPROTO_H_ 957176730Sjeffstruct cpuset_getaffinity_args { 958177597Sru cpulevel_t level; 959177597Sru cpuwhich_t which; 960177597Sru id_t id; 961177597Sru size_t cpusetsize; 962177597Sru cpuset_t *mask; 963176730Sjeff}; 964176730Sjeff#endif 965176730Sjeffint 966225617Skmacysys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) 967176730Sjeff{ 968176730Sjeff struct thread *ttd; 969176730Sjeff struct cpuset *nset; 970176730Sjeff struct cpuset *set; 971176730Sjeff struct proc *p; 972176730Sjeff cpuset_t *mask; 973176730Sjeff int error; 974177597Sru size_t size; 975176730Sjeff 976176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 977179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 978176730Sjeff return (ERANGE); 979176811Sjeff size = uap->cpusetsize; 980176730Sjeff mask = malloc(size, M_TEMP, M_WAITOK | M_ZERO); 981176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 982176730Sjeff if (error) 983176730Sjeff goto out; 984176730Sjeff switch (uap->level) { 985176730Sjeff case CPU_LEVEL_ROOT: 986176730Sjeff case CPU_LEVEL_CPUSET: 987176730Sjeff switch (uap->which) { 988176730Sjeff case CPU_WHICH_TID: 989176730Sjeff case CPU_WHICH_PID: 990176730Sjeff thread_lock(ttd); 991176730Sjeff set = cpuset_ref(ttd->td_cpuset); 992176730Sjeff thread_unlock(ttd); 993176730Sjeff break; 994176730Sjeff case CPU_WHICH_CPUSET: 995185435Sbz case CPU_WHICH_JAIL: 996176730Sjeff break; 997178092Sjeff case CPU_WHICH_IRQ: 998178092Sjeff error = EINVAL; 999178092Sjeff goto out; 1000176730Sjeff } 1001176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 1002177738Sjeff nset = cpuset_refroot(set); 1003176730Sjeff else 1004177738Sjeff nset = cpuset_refbase(set); 1005176730Sjeff CPU_COPY(&nset->cs_mask, mask); 1006176730Sjeff cpuset_rel(nset); 1007176730Sjeff break; 1008176730Sjeff case CPU_LEVEL_WHICH: 1009176730Sjeff switch (uap->which) { 1010176730Sjeff case CPU_WHICH_TID: 1011176730Sjeff thread_lock(ttd); 1012176730Sjeff CPU_COPY(&ttd->td_cpuset->cs_mask, mask); 1013176730Sjeff thread_unlock(ttd); 1014176730Sjeff break; 1015176730Sjeff case CPU_WHICH_PID: 1016176730Sjeff FOREACH_THREAD_IN_PROC(p, ttd) { 1017176730Sjeff thread_lock(ttd); 1018176730Sjeff CPU_OR(mask, &ttd->td_cpuset->cs_mask); 1019176730Sjeff thread_unlock(ttd); 1020176730Sjeff } 1021176730Sjeff break; 1022176730Sjeff case CPU_WHICH_CPUSET: 1023185435Sbz case CPU_WHICH_JAIL: 1024176730Sjeff CPU_COPY(&set->cs_mask, mask); 1025176730Sjeff break; 1026178092Sjeff case CPU_WHICH_IRQ: 1027178092Sjeff error = intr_getaffinity(uap->id, mask); 1028178092Sjeff break; 1029176730Sjeff } 1030176730Sjeff break; 1031176730Sjeff default: 1032176730Sjeff error = EINVAL; 1033176730Sjeff break; 1034176730Sjeff } 1035176730Sjeff if (set) 1036176730Sjeff cpuset_rel(set); 1037176730Sjeff if (p) 1038176730Sjeff PROC_UNLOCK(p); 1039176730Sjeff if (error == 0) 1040176730Sjeff error = copyout(mask, uap->mask, size); 1041176730Sjeffout: 1042176730Sjeff free(mask, M_TEMP); 1043176730Sjeff return (error); 1044176730Sjeff} 1045176730Sjeff 1046176730Sjeff#ifndef _SYS_SYSPROTO_H_ 1047176730Sjeffstruct cpuset_setaffinity_args { 1048176730Sjeff cpulevel_t level; 1049177597Sru cpuwhich_t which; 1050177597Sru id_t id; 1051177597Sru size_t cpusetsize; 1052177597Sru const cpuset_t *mask; 1053176730Sjeff}; 1054176730Sjeff#endif 1055176730Sjeffint 1056225617Skmacysys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) 1057176730Sjeff{ 1058176730Sjeff struct cpuset *nset; 1059176730Sjeff struct cpuset *set; 1060176730Sjeff struct thread *ttd; 1061176730Sjeff struct proc *p; 1062176730Sjeff cpuset_t *mask; 1063176730Sjeff int error; 1064176730Sjeff 1065176811Sjeff if (uap->cpusetsize < sizeof(cpuset_t) || 1066179313Skib uap->cpusetsize > CPU_MAXSIZE / NBBY) 1067176730Sjeff return (ERANGE); 1068176811Sjeff mask = malloc(uap->cpusetsize, M_TEMP, M_WAITOK | M_ZERO); 1069176811Sjeff error = copyin(uap->mask, mask, uap->cpusetsize); 1070176730Sjeff if (error) 1071176730Sjeff goto out; 1072176811Sjeff /* 1073176811Sjeff * Verify that no high bits are set. 1074176811Sjeff */ 1075176811Sjeff if (uap->cpusetsize > sizeof(cpuset_t)) { 1076176811Sjeff char *end; 1077176811Sjeff char *cp; 1078176811Sjeff 1079176811Sjeff end = cp = (char *)&mask->__bits; 1080176811Sjeff end += uap->cpusetsize; 1081176811Sjeff cp += sizeof(cpuset_t); 1082176811Sjeff while (cp != end) 1083176811Sjeff if (*cp++ != 0) { 1084176811Sjeff error = EINVAL; 1085176811Sjeff goto out; 1086176811Sjeff } 1087176811Sjeff 1088176811Sjeff } 1089176730Sjeff switch (uap->level) { 1090176730Sjeff case CPU_LEVEL_ROOT: 1091176730Sjeff case CPU_LEVEL_CPUSET: 1092176730Sjeff error = cpuset_which(uap->which, uap->id, &p, &ttd, &set); 1093176730Sjeff if (error) 1094176730Sjeff break; 1095176730Sjeff switch (uap->which) { 1096176730Sjeff case CPU_WHICH_TID: 1097176730Sjeff case CPU_WHICH_PID: 1098176730Sjeff thread_lock(ttd); 1099176730Sjeff set = cpuset_ref(ttd->td_cpuset); 1100176730Sjeff thread_unlock(ttd); 1101176880Sjeff PROC_UNLOCK(p); 1102176730Sjeff break; 1103176730Sjeff case CPU_WHICH_CPUSET: 1104185435Sbz case CPU_WHICH_JAIL: 1105176730Sjeff break; 1106178092Sjeff case CPU_WHICH_IRQ: 1107178092Sjeff error = EINVAL; 1108178092Sjeff goto out; 1109176730Sjeff } 1110176730Sjeff if (uap->level == CPU_LEVEL_ROOT) 1111177738Sjeff nset = cpuset_refroot(set); 1112176730Sjeff else 1113177738Sjeff nset = cpuset_refbase(set); 1114176730Sjeff error = cpuset_modify(nset, mask); 1115176730Sjeff cpuset_rel(nset); 1116176730Sjeff cpuset_rel(set); 1117176730Sjeff break; 1118176730Sjeff case CPU_LEVEL_WHICH: 1119176730Sjeff switch (uap->which) { 1120176730Sjeff case CPU_WHICH_TID: 1121176730Sjeff error = cpuset_setthread(uap->id, mask); 1122176730Sjeff break; 1123176730Sjeff case CPU_WHICH_PID: 1124176730Sjeff error = cpuset_setproc(uap->id, NULL, mask); 1125176730Sjeff break; 1126176730Sjeff case CPU_WHICH_CPUSET: 1127185435Sbz case CPU_WHICH_JAIL: 1128185435Sbz error = cpuset_which(uap->which, uap->id, &p, 1129176730Sjeff &ttd, &set); 1130176730Sjeff if (error == 0) { 1131176730Sjeff error = cpuset_modify(set, mask); 1132176730Sjeff cpuset_rel(set); 1133176730Sjeff } 1134176730Sjeff break; 1135178092Sjeff case CPU_WHICH_IRQ: 1136178092Sjeff error = intr_setaffinity(uap->id, mask); 1137178092Sjeff break; 1138176730Sjeff default: 1139176730Sjeff error = EINVAL; 1140176730Sjeff break; 1141176730Sjeff } 1142176730Sjeff break; 1143176730Sjeff default: 1144176730Sjeff error = EINVAL; 1145176730Sjeff break; 1146176730Sjeff } 1147176730Sjeffout: 1148176730Sjeff free(mask, M_TEMP); 1149176730Sjeff return (error); 1150176730Sjeff} 1151180358Sbz 1152180358Sbz#ifdef DDB 1153180358SbzDB_SHOW_COMMAND(cpusets, db_show_cpusets) 1154180358Sbz{ 1155180358Sbz struct cpuset *set; 1156180358Sbz int cpu, once; 1157180358Sbz 1158180358Sbz LIST_FOREACH(set, &cpuset_ids, cs_link) { 1159180358Sbz db_printf("set=%p id=%-6u ref=%-6d flags=0x%04x parent id=%d\n", 1160180358Sbz set, set->cs_id, set->cs_ref, set->cs_flags, 1161180358Sbz (set->cs_parent != NULL) ? set->cs_parent->cs_id : 0); 1162180358Sbz db_printf(" mask="); 1163180358Sbz for (once = 0, cpu = 0; cpu < CPU_SETSIZE; cpu++) { 1164180358Sbz if (CPU_ISSET(cpu, &set->cs_mask)) { 1165180358Sbz if (once == 0) { 1166180358Sbz db_printf("%d", cpu); 1167180358Sbz once = 1; 1168180358Sbz } else 1169180358Sbz db_printf(",%d", cpu); 1170180358Sbz } 1171180358Sbz } 1172180358Sbz db_printf("\n"); 1173180358Sbz if (db_pager_quit) 1174180358Sbz break; 1175180358Sbz } 1176180358Sbz} 1177180358Sbz#endif /* DDB */ 1178