1 /* $NetBSD: sys_pset.c,v 1.21 2018/12/09 23:05:02 mlelstv Exp $ */ 2 3 /* 4 * Copyright (c) 2008, Mindaugas Rasiukevicius <rmind at NetBSD org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * Implementation of the Processor Sets. 31 * 32 * Locking 33 * The array of the processor-set structures and its members are protected 34 * by the global cpu_lock. Note that in scheduler, the very l_psid value 35 * might be used without lock held. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: sys_pset.c,v 1.21 2018/12/09 23:05:02 mlelstv Exp $"); 40 41 #include <sys/param.h> 42 43 #include <sys/cpu.h> 44 #include <sys/kauth.h> 45 #include <sys/kmem.h> 46 #include <sys/lwp.h> 47 #include <sys/mutex.h> 48 #include <sys/proc.h> 49 #include <sys/pset.h> 50 #include <sys/sched.h> 51 #include <sys/syscallargs.h> 52 #include <sys/sysctl.h> 53 #include <sys/systm.h> 54 #include <sys/types.h> 55 56 static pset_info_t ** psets; 57 static u_int psets_max; 58 static u_int psets_count; 59 static kauth_listener_t psets_listener; 60 61 static int psets_realloc(int); 62 static int psid_validate(psetid_t, bool); 63 static int kern_pset_create(psetid_t *); 64 static int kern_pset_destroy(psetid_t); 65 66 static int 67 psets_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 68 void *arg0, void *arg1, void *arg2, void *arg3) 69 { 70 psetid_t id; 71 enum kauth_system_req req; 72 int result; 73 74 result = KAUTH_RESULT_DEFER; 75 req = (enum kauth_system_req)arg0; 76 id = (psetid_t)(unsigned long)arg1; 77 78 if (action != KAUTH_SYSTEM_PSET) 79 return result; 80 81 if ((req == KAUTH_REQ_SYSTEM_PSET_ASSIGN) || 82 (req == KAUTH_REQ_SYSTEM_PSET_BIND)) { 83 if (id == PS_QUERY) 84 result = KAUTH_RESULT_ALLOW; 85 } 86 87 return result; 88 } 89 90 /* 91 * Initialization of the processor-sets. 92 */ 93 void 94 psets_init(void) 95 { 96 97 psets_max = uimax(maxcpus, 32); 98 psets = kmem_zalloc(psets_max * sizeof(void *), KM_SLEEP); 99 psets_count = 0; 100 101 psets_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM, 102 psets_listener_cb, NULL); 103 } 104 105 /* 106 * Reallocate the array of the processor-set structures. 107 */ 108 static int 109 psets_realloc(int new_psets_max) 110 { 111 pset_info_t **new_psets, **old_psets; 112 const u_int newsize = new_psets_max * sizeof(void *); 113 u_int i, oldsize; 114 115 if (new_psets_max < 1) 116 return EINVAL; 117 118 new_psets = kmem_zalloc(newsize, KM_SLEEP); 119 mutex_enter(&cpu_lock); 120 old_psets = psets; 121 oldsize = psets_max * sizeof(void *); 122 123 /* Check if we can lower the size of the array */ 124 if (new_psets_max < psets_max) { 125 for (i = new_psets_max; i < psets_max; i++) { 126 if (psets[i] == NULL) 127 continue; 128 mutex_exit(&cpu_lock); 129 kmem_free(new_psets, newsize); 130 return EBUSY; 131 } 132 } 133 134 /* Copy all pointers to the new array */ 135 memcpy(new_psets, psets, newsize); 136 psets_max = new_psets_max; 137 psets = new_psets; 138 mutex_exit(&cpu_lock); 139 140 kmem_free(old_psets, oldsize); 141 return 0; 142 } 143 144 /* 145 * Validate processor-set ID. 146 */ 147 static int 148 psid_validate(psetid_t psid, bool chkps) 149 { 150 151 KASSERT(mutex_owned(&cpu_lock)); 152 153 if (chkps && (psid == PS_NONE || psid == PS_QUERY || psid == PS_MYID)) 154 return 0; 155 if (psid <= 0 || psid > psets_max) 156 return EINVAL; 157 if (psets[psid - 1] == NULL) 158 return EINVAL; 159 if (psets[psid - 1]->ps_flags & PSET_BUSY) 160 return EBUSY; 161 162 return 0; 163 } 164 165 /* 166 * Create a processor-set. 167 */ 168 static int 169 kern_pset_create(psetid_t *psid) 170 { 171 pset_info_t *pi; 172 u_int i; 173 174 if (psets_count == psets_max) 175 return ENOMEM; 176 177 pi = kmem_zalloc(sizeof(pset_info_t), KM_SLEEP); 178 179 mutex_enter(&cpu_lock); 180 if (psets_count == psets_max) { 181 mutex_exit(&cpu_lock); 182 kmem_free(pi, sizeof(pset_info_t)); 183 return ENOMEM; 184 } 185 186 /* Find a free entry in the array */ 187 for (i = 0; i < psets_max; i++) 188 if (psets[i] == NULL) 189 break; 190 KASSERT(i != psets_max); 191 192 psets[i] = pi; 193 psets_count++; 194 mutex_exit(&cpu_lock); 195 196 *psid = i + 1; 197 return 0; 198 } 199 200 /* 201 * Destroy a processor-set. 202 */ 203 static int 204 kern_pset_destroy(psetid_t psid) 205 { 206 struct cpu_info *ci; 207 pset_info_t *pi; 208 struct lwp *l; 209 CPU_INFO_ITERATOR cii; 210 int error; 211 212 mutex_enter(&cpu_lock); 213 if (psid == PS_MYID) { 214 /* Use caller's processor-set ID */ 215 psid = curlwp->l_psid; 216 } 217 error = psid_validate(psid, false); 218 if (error) { 219 mutex_exit(&cpu_lock); 220 return error; 221 } 222 223 /* Release the processor-set from all CPUs */ 224 for (CPU_INFO_FOREACH(cii, ci)) { 225 struct schedstate_percpu *spc; 226 227 spc = &ci->ci_schedstate; 228 if (spc->spc_psid != psid) 229 continue; 230 spc->spc_psid = PS_NONE; 231 } 232 /* Mark that processor-set is going to be destroyed */ 233 pi = psets[psid - 1]; 234 pi->ps_flags |= PSET_BUSY; 235 mutex_exit(&cpu_lock); 236 237 /* Unmark the processor-set ID from each thread */ 238 mutex_enter(proc_lock); 239 LIST_FOREACH(l, &alllwp, l_list) { 240 /* Safe to check and set without lock held */ 241 if (l->l_psid != psid) 242 continue; 243 l->l_psid = PS_NONE; 244 } 245 mutex_exit(proc_lock); 246 247 /* Destroy the processor-set */ 248 mutex_enter(&cpu_lock); 249 psets[psid - 1] = NULL; 250 psets_count--; 251 mutex_exit(&cpu_lock); 252 253 kmem_free(pi, sizeof(pset_info_t)); 254 return 0; 255 } 256 257 /* 258 * General system calls for the processor-sets. 259 */ 260 261 int 262 sys_pset_create(struct lwp *l, const struct sys_pset_create_args *uap, 263 register_t *retval) 264 { 265 /* { 266 syscallarg(psetid_t) *psid; 267 } */ 268 psetid_t psid; 269 int error; 270 271 /* Available only for super-user */ 272 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET, 273 KAUTH_REQ_SYSTEM_PSET_CREATE, NULL, NULL, NULL)) 274 return EPERM; 275 276 error = kern_pset_create(&psid); 277 if (error) 278 return error; 279 280 error = copyout(&psid, SCARG(uap, psid), sizeof(psetid_t)); 281 if (error) 282 (void)kern_pset_destroy(psid); 283 284 return error; 285 } 286 287 int 288 sys_pset_destroy(struct lwp *l, const struct sys_pset_destroy_args *uap, 289 register_t *retval) 290 { 291 /* { 292 syscallarg(psetid_t) psid; 293 } */ 294 295 /* Available only for super-user */ 296 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET, 297 KAUTH_REQ_SYSTEM_PSET_DESTROY, 298 KAUTH_ARG(SCARG(uap, psid)), NULL, NULL)) 299 return EPERM; 300 301 return kern_pset_destroy(SCARG(uap, psid)); 302 } 303 304 int 305 sys_pset_assign(struct lwp *l, const struct sys_pset_assign_args *uap, 306 register_t *retval) 307 { 308 /* { 309 syscallarg(psetid_t) psid; 310 syscallarg(cpuid_t) cpuid; 311 syscallarg(psetid_t) *opsid; 312 } */ 313 struct cpu_info *ici, *ci = NULL; 314 struct schedstate_percpu *spc = NULL; 315 struct lwp *t; 316 psetid_t psid = SCARG(uap, psid), opsid = 0; 317 CPU_INFO_ITERATOR cii; 318 int error = 0, nnone = 0; 319 320 /* Available only for super-user, except the case of PS_QUERY */ 321 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET, 322 KAUTH_REQ_SYSTEM_PSET_ASSIGN, KAUTH_ARG(SCARG(uap, psid)), NULL, 323 NULL)) 324 return EPERM; 325 326 /* Find the target CPU */ 327 mutex_enter(&cpu_lock); 328 for (CPU_INFO_FOREACH(cii, ici)) { 329 struct schedstate_percpu *ispc; 330 ispc = &ici->ci_schedstate; 331 if (cpu_index(ici) == SCARG(uap, cpuid)) { 332 ci = ici; 333 spc = ispc; 334 } 335 nnone += (ispc->spc_psid == PS_NONE); 336 } 337 if (ci == NULL) { 338 mutex_exit(&cpu_lock); 339 return EINVAL; 340 } 341 error = psid_validate(psid, true); 342 if (error) { 343 mutex_exit(&cpu_lock); 344 return error; 345 } 346 opsid = spc->spc_psid; 347 switch (psid) { 348 case PS_QUERY: 349 break; 350 case PS_MYID: 351 psid = curlwp->l_psid; 352 /* FALLTHROUGH */ 353 default: 354 /* 355 * Just finish if old and new processor-sets are 356 * the same. 357 */ 358 if (spc->spc_psid == psid) 359 break; 360 /* 361 * Ensure at least one CPU stays in the default set, 362 * and that specified CPU is not offline. 363 */ 364 if (psid != PS_NONE && ((spc->spc_flags & SPCF_OFFLINE) || 365 (nnone == 1 && spc->spc_psid == PS_NONE))) { 366 mutex_exit(&cpu_lock); 367 return EBUSY; 368 } 369 mutex_enter(proc_lock); 370 /* 371 * Ensure that none of the threads are using affinity mask 372 * with this target CPU in it. 373 */ 374 LIST_FOREACH(t, &alllwp, l_list) { 375 if (t->l_affinity == NULL) { 376 continue; 377 } 378 lwp_lock(t); 379 if (t->l_affinity == NULL) { 380 lwp_unlock(t); 381 continue; 382 } 383 if (kcpuset_isset(t->l_affinity, cpu_index(ci))) { 384 lwp_unlock(t); 385 mutex_exit(proc_lock); 386 mutex_exit(&cpu_lock); 387 return EPERM; 388 } 389 lwp_unlock(t); 390 } 391 /* 392 * Set the processor-set ID. 393 * Migrate out any threads running on this CPU. 394 */ 395 spc->spc_psid = psid; 396 397 LIST_FOREACH(t, &alllwp, l_list) { 398 struct cpu_info *tci; 399 if (t->l_cpu != ci) 400 continue; 401 if (t->l_pflag & (LP_BOUND | LP_INTR)) 402 continue; 403 lwp_lock(t); 404 tci = sched_takecpu(t); 405 KASSERT(tci != ci); 406 lwp_migrate(t, tci); 407 } 408 mutex_exit(proc_lock); 409 break; 410 } 411 mutex_exit(&cpu_lock); 412 413 if (SCARG(uap, opsid) != NULL) 414 error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t)); 415 416 return error; 417 } 418 419 int 420 sys__pset_bind(struct lwp *l, const struct sys__pset_bind_args *uap, 421 register_t *retval) 422 { 423 /* { 424 syscallarg(idtype_t) idtype; 425 syscallarg(id_t) first_id; 426 syscallarg(id_t) second_id; 427 syscallarg(psetid_t) psid; 428 syscallarg(psetid_t) *opsid; 429 } */ 430 struct cpu_info *ci; 431 struct proc *p; 432 struct lwp *t; 433 id_t id1, id2; 434 pid_t pid = 0; 435 lwpid_t lid = 0; 436 psetid_t psid, opsid; 437 int error = 0, lcnt; 438 439 psid = SCARG(uap, psid); 440 441 /* Available only for super-user, except the case of PS_QUERY */ 442 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_PSET, 443 KAUTH_REQ_SYSTEM_PSET_BIND, KAUTH_ARG(SCARG(uap, psid)), NULL, 444 NULL)) 445 return EPERM; 446 447 mutex_enter(&cpu_lock); 448 error = psid_validate(psid, true); 449 if (error) { 450 mutex_exit(&cpu_lock); 451 return error; 452 } 453 if (psid == PS_MYID) 454 psid = curlwp->l_psid; 455 if (psid != PS_QUERY && psid != PS_NONE) 456 psets[psid - 1]->ps_flags |= PSET_BUSY; 457 mutex_exit(&cpu_lock); 458 459 /* 460 * Get PID and LID from the ID. 461 */ 462 p = l->l_proc; 463 id1 = SCARG(uap, first_id); 464 id2 = SCARG(uap, second_id); 465 466 switch (SCARG(uap, idtype)) { 467 case P_PID: 468 /* 469 * Process: 470 * First ID - PID; 471 * Second ID - ignored; 472 */ 473 pid = (id1 == P_MYID) ? p->p_pid : id1; 474 lid = 0; 475 break; 476 case P_LWPID: 477 /* 478 * Thread (LWP): 479 * First ID - LID; 480 * Second ID - PID; 481 */ 482 if (id1 == P_MYID) { 483 pid = p->p_pid; 484 lid = l->l_lid; 485 break; 486 } 487 lid = id1; 488 pid = (id2 == P_MYID) ? p->p_pid : id2; 489 break; 490 default: 491 error = EINVAL; 492 goto error; 493 } 494 495 /* Find the process */ 496 mutex_enter(proc_lock); 497 p = proc_find(pid); 498 if (p == NULL) { 499 mutex_exit(proc_lock); 500 error = ESRCH; 501 goto error; 502 } 503 mutex_enter(p->p_lock); 504 mutex_exit(proc_lock); 505 506 /* Disallow modification of the system processes */ 507 if (p->p_flag & PK_SYSTEM) { 508 mutex_exit(p->p_lock); 509 error = EPERM; 510 goto error; 511 } 512 513 /* Find the LWP(s) */ 514 lcnt = 0; 515 ci = NULL; 516 LIST_FOREACH(t, &p->p_lwps, l_sibling) { 517 if (lid && lid != t->l_lid) 518 continue; 519 /* 520 * Bind the thread to the processor-set, 521 * take some CPU and migrate. 522 */ 523 lwp_lock(t); 524 opsid = t->l_psid; 525 t->l_psid = psid; 526 ci = sched_takecpu(t); 527 /* Unlocks LWP */ 528 lwp_migrate(t, ci); 529 lcnt++; 530 } 531 mutex_exit(p->p_lock); 532 if (lcnt == 0) { 533 error = ESRCH; 534 goto error; 535 } 536 if (SCARG(uap, opsid)) 537 error = copyout(&opsid, SCARG(uap, opsid), sizeof(psetid_t)); 538 error: 539 if (psid != PS_QUERY && psid != PS_NONE) { 540 mutex_enter(&cpu_lock); 541 psets[psid - 1]->ps_flags &= ~PSET_BUSY; 542 mutex_exit(&cpu_lock); 543 } 544 return error; 545 } 546 547 /* 548 * Sysctl nodes and initialization. 549 */ 550 551 static int 552 sysctl_psets_max(SYSCTLFN_ARGS) 553 { 554 struct sysctlnode node; 555 int error, newsize; 556 557 node = *rnode; 558 node.sysctl_data = &newsize; 559 560 newsize = psets_max; 561 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 562 if (error || newp == NULL) 563 return error; 564 565 if (newsize <= 0) 566 return EINVAL; 567 568 sysctl_unlock(); 569 error = psets_realloc(newsize); 570 sysctl_relock(); 571 return error; 572 } 573 574 static int 575 sysctl_psets_list(SYSCTLFN_ARGS) 576 { 577 const size_t bufsz = 1024; 578 char *buf, tbuf[16]; 579 int i, error; 580 size_t len; 581 582 sysctl_unlock(); 583 buf = kmem_alloc(bufsz, KM_SLEEP); 584 snprintf(buf, bufsz, "%d:1", PS_NONE); /* XXX */ 585 586 mutex_enter(&cpu_lock); 587 for (i = 0; i < psets_max; i++) { 588 if (psets[i] == NULL) 589 continue; 590 snprintf(tbuf, sizeof(tbuf), ",%d:2", i + 1); /* XXX */ 591 strlcat(buf, tbuf, bufsz); 592 } 593 mutex_exit(&cpu_lock); 594 len = strlen(buf) + 1; 595 error = 0; 596 if (oldp != NULL) 597 error = copyout(buf, oldp, uimin(len, *oldlenp)); 598 *oldlenp = len; 599 kmem_free(buf, bufsz); 600 sysctl_relock(); 601 return error; 602 } 603 604 SYSCTL_SETUP(sysctl_pset_setup, "sysctl kern.pset subtree setup") 605 { 606 const struct sysctlnode *node = NULL; 607 608 sysctl_createv(clog, 0, NULL, &node, 609 CTLFLAG_PERMANENT, 610 CTLTYPE_NODE, "pset", 611 SYSCTL_DESCR("Processor-set options"), 612 NULL, 0, NULL, 0, 613 CTL_KERN, CTL_CREATE, CTL_EOL); 614 615 if (node == NULL) 616 return; 617 618 sysctl_createv(clog, 0, &node, NULL, 619 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 620 CTLTYPE_INT, "psets_max", 621 SYSCTL_DESCR("Maximal count of the processor-sets"), 622 sysctl_psets_max, 0, &psets_max, 0, 623 CTL_CREATE, CTL_EOL); 624 sysctl_createv(clog, 0, &node, NULL, 625 CTLFLAG_PERMANENT, 626 CTLTYPE_STRING, "list", 627 SYSCTL_DESCR("List of active sets"), 628 sysctl_psets_list, 0, NULL, 0, 629 CTL_CREATE, CTL_EOL); 630 } 631