1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 #include <sys/types.h> 31 #include <sys/param.h> 32 #include <sys/sysmacros.h> 33 #include <sys/cred.h> 34 #include <sys/proc.h> 35 #include <sys/session.h> 36 #include <sys/strsubr.h> 37 #include <sys/signal.h> 38 #include <sys/user.h> 39 #include <sys/priocntl.h> 40 #include <sys/class.h> 41 #include <sys/disp.h> 42 #include <sys/procset.h> 43 #include <sys/debug.h> 44 #include <sys/ts.h> 45 #include <sys/tspriocntl.h> 46 #include <sys/iapriocntl.h> 47 #include <sys/kmem.h> 48 #include <sys/errno.h> 49 #include <sys/cpuvar.h> 50 #include <sys/systm.h> /* for lbolt */ 51 #include <sys/vtrace.h> 52 #include <sys/vmsystm.h> 53 #include <sys/schedctl.h> 54 #include <sys/tnf_probe.h> 55 #include <sys/atomic.h> 56 #include <sys/policy.h> 57 #include <sys/sdt.h> 58 #include <sys/cpupart.h> 59 #include <vm/rm.h> 60 #include <vm/seg_kmem.h> 61 #include <sys/modctl.h> 62 #include <sys/cpucaps.h> 63 64 static pri_t ts_init(id_t, int, classfuncs_t **); 65 66 static struct sclass csw = { 67 "TS", 68 ts_init, 69 0 70 }; 71 72 static struct modlsched modlsched = { 73 &mod_schedops, "time sharing sched class", &csw 74 }; 75 76 static struct modlinkage modlinkage = { 77 MODREV_1, (void *)&modlsched, NULL 78 }; 79 80 int 81 _init() 82 { 83 return (mod_install(&modlinkage)); 84 } 85 86 int 87 _fini() 88 { 89 return (EBUSY); /* don't remove TS for now */ 90 } 91 92 int 93 _info(struct modinfo *modinfop) 94 { 95 return (mod_info(&modlinkage, modinfop)); 96 } 97 98 /* 99 * Class specific code for the time-sharing class 100 */ 101 102 103 /* 104 * Extern declarations for variables defined in the ts master file 105 */ 106 #define TSMAXUPRI 60 107 108 pri_t ts_maxupri = TSMAXUPRI; /* max time-sharing user priority */ 109 pri_t ts_maxumdpri; /* maximum user mode ts priority */ 110 111 pri_t ia_maxupri = IAMAXUPRI; /* max interactive user priority */ 112 pri_t ia_boost = IA_BOOST; /* boost value for interactive */ 113 114 tsdpent_t *ts_dptbl; /* time-sharing disp parameter table */ 115 pri_t *ts_kmdpris; /* array of global pris used by ts procs when */ 116 /* sleeping or running in kernel after sleep */ 117 118 static id_t ia_cid; 119 120 int ts_sleep_promote = 1; 121 122 #define tsmedumdpri (ts_maxumdpri >> 1) 123 124 #define TS_NEWUMDPRI(tspp) \ 125 { \ 126 pri_t pri; \ 127 pri = (tspp)->ts_cpupri + (tspp)->ts_upri + (tspp)->ts_boost; \ 128 if (pri > ts_maxumdpri) \ 129 (tspp)->ts_umdpri = ts_maxumdpri; \ 130 else if (pri < 0) \ 131 (tspp)->ts_umdpri = 0; \ 132 else \ 133 (tspp)->ts_umdpri = pri; \ 134 ASSERT((tspp)->ts_umdpri >= 0 && (tspp)->ts_umdpri <= ts_maxumdpri); \ 135 } 136 137 /* 138 * The tsproc_t structures are kept in an array of circular doubly linked 139 * lists. A hash on the thread pointer is used to determine which list 140 * each thread should be placed. Each list has a dummy "head" which is 141 * never removed, so the list is never empty. ts_update traverses these 142 * lists to update the priorities of threads that have been waiting on 143 * the run queue. 144 */ 145 146 #define TS_LISTS 16 /* number of lists, must be power of 2 */ 147 148 /* hash function, argument is a thread pointer */ 149 #define TS_LIST_HASH(tp) (((uintptr_t)(tp) >> 9) & (TS_LISTS - 1)) 150 151 /* iterate to the next list */ 152 #define TS_LIST_NEXT(i) (((i) + 1) & (TS_LISTS - 1)) 153 154 /* 155 * Insert thread into the appropriate tsproc list. 156 */ 157 #define TS_LIST_INSERT(tspp) \ 158 { \ 159 int index = TS_LIST_HASH(tspp->ts_tp); \ 160 kmutex_t *lockp = &ts_list_lock[index]; \ 161 tsproc_t *headp = &ts_plisthead[index]; \ 162 mutex_enter(lockp); \ 163 tspp->ts_next = headp->ts_next; \ 164 tspp->ts_prev = headp; \ 165 headp->ts_next->ts_prev = tspp; \ 166 headp->ts_next = tspp; \ 167 mutex_exit(lockp); \ 168 } 169 170 /* 171 * Remove thread from tsproc list. 172 */ 173 #define TS_LIST_DELETE(tspp) \ 174 { \ 175 int index = TS_LIST_HASH(tspp->ts_tp); \ 176 kmutex_t *lockp = &ts_list_lock[index]; \ 177 mutex_enter(lockp); \ 178 tspp->ts_prev->ts_next = tspp->ts_next; \ 179 tspp->ts_next->ts_prev = tspp->ts_prev; \ 180 mutex_exit(lockp); \ 181 } 182 183 184 static int ts_admin(caddr_t, cred_t *); 185 static int ts_enterclass(kthread_t *, id_t, void *, cred_t *, void *); 186 static int ts_fork(kthread_t *, kthread_t *, void *); 187 static int ts_getclinfo(void *); 188 static int ts_getclpri(pcpri_t *); 189 static int ts_parmsin(void *); 190 static int ts_parmsout(void *, pc_vaparms_t *); 191 static int ts_vaparmsin(void *, pc_vaparms_t *); 192 static int ts_vaparmsout(void *, pc_vaparms_t *); 193 static int ts_parmsset(kthread_t *, void *, id_t, cred_t *); 194 static void ts_exit(kthread_t *); 195 static int ts_donice(kthread_t *, cred_t *, int, int *); 196 static int ts_doprio(kthread_t *, cred_t *, int, int *); 197 static void ts_exitclass(void *); 198 static int ts_canexit(kthread_t *, cred_t *); 199 static void ts_forkret(kthread_t *, kthread_t *); 200 static void ts_nullsys(); 201 static void ts_parmsget(kthread_t *, void *); 202 static void ts_preempt(kthread_t *); 203 static void ts_setrun(kthread_t *); 204 static void ts_sleep(kthread_t *); 205 static pri_t ts_swapin(kthread_t *, int); 206 static pri_t ts_swapout(kthread_t *, int); 207 static void ts_tick(kthread_t *); 208 static void ts_trapret(kthread_t *); 209 static void ts_update(void *); 210 static int ts_update_list(int); 211 static void ts_wakeup(kthread_t *); 212 static pri_t ts_globpri(kthread_t *); 213 static void ts_yield(kthread_t *); 214 extern tsdpent_t *ts_getdptbl(void); 215 extern pri_t *ts_getkmdpris(void); 216 extern pri_t td_getmaxumdpri(void); 217 static int ts_alloc(void **, int); 218 static void ts_free(void *); 219 220 pri_t ia_init(id_t, int, classfuncs_t **); 221 static int ia_getclinfo(void *); 222 static int ia_getclpri(pcpri_t *); 223 static int ia_parmsin(void *); 224 static int ia_vaparmsin(void *, pc_vaparms_t *); 225 static int ia_vaparmsout(void *, pc_vaparms_t *); 226 static int ia_parmsset(kthread_t *, void *, id_t, cred_t *); 227 static void ia_parmsget(kthread_t *, void *); 228 static void ia_set_process_group(pid_t, pid_t, pid_t); 229 230 static void ts_change_priority(kthread_t *, tsproc_t *); 231 232 extern pri_t ts_maxkmdpri; /* maximum kernel mode ts priority */ 233 static pri_t ts_maxglobpri; /* maximum global priority used by ts class */ 234 static kmutex_t ts_dptblock; /* protects time sharing dispatch table */ 235 static kmutex_t ts_list_lock[TS_LISTS]; /* protects tsproc lists */ 236 static tsproc_t ts_plisthead[TS_LISTS]; /* dummy tsproc at head of lists */ 237 238 static gid_t IA_gid = 0; 239 240 static struct classfuncs ts_classfuncs = { 241 /* class functions */ 242 ts_admin, 243 ts_getclinfo, 244 ts_parmsin, 245 ts_parmsout, 246 ts_vaparmsin, 247 ts_vaparmsout, 248 ts_getclpri, 249 ts_alloc, 250 ts_free, 251 252 /* thread functions */ 253 ts_enterclass, 254 ts_exitclass, 255 ts_canexit, 256 ts_fork, 257 ts_forkret, 258 ts_parmsget, 259 ts_parmsset, 260 ts_nullsys, /* stop */ 261 ts_exit, 262 ts_nullsys, /* active */ 263 ts_nullsys, /* inactive */ 264 ts_swapin, 265 ts_swapout, 266 ts_trapret, 267 ts_preempt, 268 ts_setrun, 269 ts_sleep, 270 ts_tick, 271 ts_wakeup, 272 ts_donice, 273 ts_globpri, 274 ts_nullsys, /* set_process_group */ 275 ts_yield, 276 ts_doprio, 277 }; 278 279 /* 280 * ia_classfuncs is used for interactive class threads; IA threads are stored 281 * on the same class list as TS threads, and most of the class functions are 282 * identical, but a few have different enough functionality to require their 283 * own functions. 284 */ 285 static struct classfuncs ia_classfuncs = { 286 /* class functions */ 287 ts_admin, 288 ia_getclinfo, 289 ia_parmsin, 290 ts_parmsout, 291 ia_vaparmsin, 292 ia_vaparmsout, 293 ia_getclpri, 294 ts_alloc, 295 ts_free, 296 297 /* thread functions */ 298 ts_enterclass, 299 ts_exitclass, 300 ts_canexit, 301 ts_fork, 302 ts_forkret, 303 ia_parmsget, 304 ia_parmsset, 305 ts_nullsys, /* stop */ 306 ts_exit, 307 ts_nullsys, /* active */ 308 ts_nullsys, /* inactive */ 309 ts_swapin, 310 ts_swapout, 311 ts_trapret, 312 ts_preempt, 313 ts_setrun, 314 ts_sleep, 315 ts_tick, 316 ts_wakeup, 317 ts_donice, 318 ts_globpri, 319 ia_set_process_group, 320 ts_yield, 321 ts_doprio, 322 }; 323 324 325 /* 326 * Time sharing class initialization. Called by dispinit() at boot time. 327 * We can ignore the clparmsz argument since we know that the smallest 328 * possible parameter buffer is big enough for us. 329 */ 330 /* ARGSUSED */ 331 static pri_t 332 ts_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp) 333 { 334 int i; 335 extern pri_t ts_getmaxumdpri(void); 336 337 ts_dptbl = ts_getdptbl(); 338 ts_kmdpris = ts_getkmdpris(); 339 ts_maxumdpri = ts_getmaxumdpri(); 340 ts_maxglobpri = MAX(ts_kmdpris[0], ts_dptbl[ts_maxumdpri].ts_globpri); 341 342 /* 343 * Initialize the tsproc lists. 344 */ 345 for (i = 0; i < TS_LISTS; i++) { 346 ts_plisthead[i].ts_next = ts_plisthead[i].ts_prev = 347 &ts_plisthead[i]; 348 } 349 350 /* 351 * We're required to return a pointer to our classfuncs 352 * structure and the highest global priority value we use. 353 */ 354 *clfuncspp = &ts_classfuncs; 355 return (ts_maxglobpri); 356 } 357 358 359 /* 360 * Interactive class scheduler initialization 361 */ 362 /* ARGSUSED */ 363 pri_t 364 ia_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp) 365 { 366 /* 367 * We're required to return a pointer to our classfuncs 368 * structure and the highest global priority value we use. 369 */ 370 ia_cid = cid; 371 *clfuncspp = &ia_classfuncs; 372 return (ts_maxglobpri); 373 } 374 375 376 /* 377 * Get or reset the ts_dptbl values per the user's request. 378 */ 379 static int 380 ts_admin(caddr_t uaddr, cred_t *reqpcredp) 381 { 382 tsadmin_t tsadmin; 383 tsdpent_t *tmpdpp; 384 int userdpsz; 385 int i; 386 size_t tsdpsz; 387 388 if (get_udatamodel() == DATAMODEL_NATIVE) { 389 if (copyin(uaddr, &tsadmin, sizeof (tsadmin_t))) 390 return (EFAULT); 391 } 392 #ifdef _SYSCALL32_IMPL 393 else { 394 /* get tsadmin struct from ILP32 caller */ 395 tsadmin32_t tsadmin32; 396 if (copyin(uaddr, &tsadmin32, sizeof (tsadmin32_t))) 397 return (EFAULT); 398 tsadmin.ts_dpents = 399 (struct tsdpent *)(uintptr_t)tsadmin32.ts_dpents; 400 tsadmin.ts_ndpents = tsadmin32.ts_ndpents; 401 tsadmin.ts_cmd = tsadmin32.ts_cmd; 402 } 403 #endif /* _SYSCALL32_IMPL */ 404 405 tsdpsz = (ts_maxumdpri + 1) * sizeof (tsdpent_t); 406 407 switch (tsadmin.ts_cmd) { 408 case TS_GETDPSIZE: 409 tsadmin.ts_ndpents = ts_maxumdpri + 1; 410 411 if (get_udatamodel() == DATAMODEL_NATIVE) { 412 if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t))) 413 return (EFAULT); 414 } 415 #ifdef _SYSCALL32_IMPL 416 else { 417 /* return tsadmin struct to ILP32 caller */ 418 tsadmin32_t tsadmin32; 419 tsadmin32.ts_dpents = 420 (caddr32_t)(uintptr_t)tsadmin.ts_dpents; 421 tsadmin32.ts_ndpents = tsadmin.ts_ndpents; 422 tsadmin32.ts_cmd = tsadmin.ts_cmd; 423 if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t))) 424 return (EFAULT); 425 } 426 #endif /* _SYSCALL32_IMPL */ 427 break; 428 429 case TS_GETDPTBL: 430 userdpsz = MIN(tsadmin.ts_ndpents * sizeof (tsdpent_t), 431 tsdpsz); 432 if (copyout(ts_dptbl, tsadmin.ts_dpents, userdpsz)) 433 return (EFAULT); 434 435 tsadmin.ts_ndpents = userdpsz / sizeof (tsdpent_t); 436 437 if (get_udatamodel() == DATAMODEL_NATIVE) { 438 if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t))) 439 return (EFAULT); 440 } 441 #ifdef _SYSCALL32_IMPL 442 else { 443 /* return tsadmin struct to ILP32 callers */ 444 tsadmin32_t tsadmin32; 445 tsadmin32.ts_dpents = 446 (caddr32_t)(uintptr_t)tsadmin.ts_dpents; 447 tsadmin32.ts_ndpents = tsadmin.ts_ndpents; 448 tsadmin32.ts_cmd = tsadmin.ts_cmd; 449 if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t))) 450 return (EFAULT); 451 } 452 #endif /* _SYSCALL32_IMPL */ 453 break; 454 455 case TS_SETDPTBL: 456 /* 457 * We require that the requesting process has sufficient 458 * priveleges. We also require that the table supplied by 459 * the user exactly match the current ts_dptbl in size. 460 */ 461 if (secpolicy_dispadm(reqpcredp) != 0) 462 return (EPERM); 463 464 if (tsadmin.ts_ndpents * sizeof (tsdpent_t) != tsdpsz) { 465 return (EINVAL); 466 } 467 468 /* 469 * We read the user supplied table into a temporary buffer 470 * where it is validated before being copied over the 471 * ts_dptbl. 472 */ 473 tmpdpp = kmem_alloc(tsdpsz, KM_SLEEP); 474 if (copyin((caddr_t)tsadmin.ts_dpents, (caddr_t)tmpdpp, 475 tsdpsz)) { 476 kmem_free(tmpdpp, tsdpsz); 477 return (EFAULT); 478 } 479 for (i = 0; i < tsadmin.ts_ndpents; i++) { 480 481 /* 482 * Validate the user supplied values. All we are doing 483 * here is verifying that the values are within their 484 * allowable ranges and will not panic the system. We 485 * make no attempt to ensure that the resulting 486 * configuration makes sense or results in reasonable 487 * performance. 488 */ 489 if (tmpdpp[i].ts_quantum <= 0) { 490 kmem_free(tmpdpp, tsdpsz); 491 return (EINVAL); 492 } 493 if (tmpdpp[i].ts_tqexp > ts_maxumdpri || 494 tmpdpp[i].ts_tqexp < 0) { 495 kmem_free(tmpdpp, tsdpsz); 496 return (EINVAL); 497 } 498 if (tmpdpp[i].ts_slpret > ts_maxumdpri || 499 tmpdpp[i].ts_slpret < 0) { 500 kmem_free(tmpdpp, tsdpsz); 501 return (EINVAL); 502 } 503 if (tmpdpp[i].ts_maxwait < 0) { 504 kmem_free(tmpdpp, tsdpsz); 505 return (EINVAL); 506 } 507 if (tmpdpp[i].ts_lwait > ts_maxumdpri || 508 tmpdpp[i].ts_lwait < 0) { 509 kmem_free(tmpdpp, tsdpsz); 510 return (EINVAL); 511 } 512 } 513 514 /* 515 * Copy the user supplied values over the current ts_dptbl 516 * values. The ts_globpri member is read-only so we don't 517 * overwrite it. 518 */ 519 mutex_enter(&ts_dptblock); 520 for (i = 0; i < tsadmin.ts_ndpents; i++) { 521 ts_dptbl[i].ts_quantum = tmpdpp[i].ts_quantum; 522 ts_dptbl[i].ts_tqexp = tmpdpp[i].ts_tqexp; 523 ts_dptbl[i].ts_slpret = tmpdpp[i].ts_slpret; 524 ts_dptbl[i].ts_maxwait = tmpdpp[i].ts_maxwait; 525 ts_dptbl[i].ts_lwait = tmpdpp[i].ts_lwait; 526 } 527 mutex_exit(&ts_dptblock); 528 kmem_free(tmpdpp, tsdpsz); 529 break; 530 531 default: 532 return (EINVAL); 533 } 534 return (0); 535 } 536 537 538 /* 539 * Allocate a time-sharing class specific thread structure and 540 * initialize it with the parameters supplied. Also move the thread 541 * to specified time-sharing priority. 542 */ 543 static int 544 ts_enterclass(kthread_t *t, id_t cid, void *parmsp, 545 cred_t *reqpcredp, void *bufp) 546 { 547 tsparms_t *tsparmsp = (tsparms_t *)parmsp; 548 tsproc_t *tspp; 549 pri_t reqtsuprilim; 550 pri_t reqtsupri; 551 static uint32_t tspexists = 0; /* set on first occurrence of */ 552 /* a time-sharing process */ 553 554 tspp = (tsproc_t *)bufp; 555 ASSERT(tspp != NULL); 556 557 /* 558 * Initialize the tsproc structure. 559 */ 560 tspp->ts_cpupri = tsmedumdpri; 561 if (cid == ia_cid) { 562 /* 563 * Check to make sure caller is either privileged or the 564 * window system. When the window system is converted 565 * to using privileges, the second check can go away. 566 */ 567 if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) && 568 secpolicy_setpriority(reqpcredp) != 0) 569 return (EPERM); 570 /* 571 * Belongs to IA "class", so set appropriate flags. 572 * Mark as 'on' so it will not be a swap victim 573 * while forking. 574 */ 575 tspp->ts_flags = TSIA | TSIASET; 576 tspp->ts_boost = ia_boost; 577 } else { 578 tspp->ts_flags = 0; 579 tspp->ts_boost = 0; 580 } 581 582 if (tsparmsp == NULL) { 583 /* 584 * Use default values. 585 */ 586 tspp->ts_uprilim = tspp->ts_upri = 0; 587 tspp->ts_nice = NZERO; 588 } else { 589 /* 590 * Use supplied values. 591 */ 592 if (tsparmsp->ts_uprilim == TS_NOCHANGE) 593 reqtsuprilim = 0; 594 else { 595 if (tsparmsp->ts_uprilim > 0 && 596 secpolicy_setpriority(reqpcredp) != 0) 597 return (EPERM); 598 reqtsuprilim = tsparmsp->ts_uprilim; 599 } 600 601 if (tsparmsp->ts_upri == TS_NOCHANGE) { 602 reqtsupri = reqtsuprilim; 603 } else { 604 if (tsparmsp->ts_upri > 0 && 605 secpolicy_setpriority(reqpcredp) != 0) 606 return (EPERM); 607 /* 608 * Set the user priority to the requested value 609 * or the upri limit, whichever is lower. 610 */ 611 reqtsupri = tsparmsp->ts_upri; 612 if (reqtsupri > reqtsuprilim) 613 reqtsupri = reqtsuprilim; 614 } 615 616 617 tspp->ts_uprilim = reqtsuprilim; 618 tspp->ts_upri = reqtsupri; 619 tspp->ts_nice = NZERO - (NZERO * reqtsupri) / ts_maxupri; 620 } 621 TS_NEWUMDPRI(tspp); 622 623 tspp->ts_dispwait = 0; 624 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 625 tspp->ts_tp = t; 626 cpucaps_sc_init(&tspp->ts_caps); 627 628 /* 629 * Reset priority. Process goes to a "user mode" priority 630 * here regardless of whether or not it has slept since 631 * entering the kernel. 632 */ 633 thread_lock(t); /* get dispatcher lock on thread */ 634 t->t_clfuncs = &(sclass[cid].cl_funcs->thread); 635 t->t_cid = cid; 636 t->t_cldata = (void *)tspp; 637 t->t_schedflag &= ~TS_RUNQMATCH; 638 ts_change_priority(t, tspp); 639 thread_unlock(t); 640 641 /* 642 * Link new structure into tsproc list. 643 */ 644 TS_LIST_INSERT(tspp); 645 646 /* 647 * If this is the first time-sharing thread to occur since 648 * boot we set up the initial call to ts_update() here. 649 * Use an atomic compare-and-swap since that's easier and 650 * faster than a mutex (but check with an ordinary load first 651 * since most of the time this will already be done). 652 */ 653 if (tspexists == 0 && cas32(&tspexists, 0, 1) == 0) 654 (void) timeout(ts_update, NULL, hz); 655 656 return (0); 657 } 658 659 660 /* 661 * Free tsproc structure of thread. 662 */ 663 static void 664 ts_exitclass(void *procp) 665 { 666 tsproc_t *tspp = (tsproc_t *)procp; 667 668 /* Remove tsproc_t structure from list */ 669 TS_LIST_DELETE(tspp); 670 kmem_free(tspp, sizeof (tsproc_t)); 671 } 672 673 /* ARGSUSED */ 674 static int 675 ts_canexit(kthread_t *t, cred_t *cred) 676 { 677 /* 678 * A thread can always leave a TS/IA class 679 */ 680 return (0); 681 } 682 683 static int 684 ts_fork(kthread_t *t, kthread_t *ct, void *bufp) 685 { 686 tsproc_t *ptspp; /* ptr to parent's tsproc structure */ 687 tsproc_t *ctspp; /* ptr to child's tsproc structure */ 688 689 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock)); 690 691 ctspp = (tsproc_t *)bufp; 692 ASSERT(ctspp != NULL); 693 ptspp = (tsproc_t *)t->t_cldata; 694 /* 695 * Initialize child's tsproc structure. 696 */ 697 thread_lock(t); 698 ctspp->ts_timeleft = ts_dptbl[ptspp->ts_cpupri].ts_quantum; 699 ctspp->ts_cpupri = ptspp->ts_cpupri; 700 ctspp->ts_boost = ptspp->ts_boost; 701 ctspp->ts_uprilim = ptspp->ts_uprilim; 702 ctspp->ts_upri = ptspp->ts_upri; 703 TS_NEWUMDPRI(ctspp); 704 ctspp->ts_nice = ptspp->ts_nice; 705 ctspp->ts_dispwait = 0; 706 ctspp->ts_flags = ptspp->ts_flags & ~(TSKPRI | TSBACKQ | TSRESTORE); 707 ctspp->ts_tp = ct; 708 cpucaps_sc_init(&ctspp->ts_caps); 709 thread_unlock(t); 710 711 /* 712 * Link new structure into tsproc list. 713 */ 714 ct->t_cldata = (void *)ctspp; 715 TS_LIST_INSERT(ctspp); 716 return (0); 717 } 718 719 720 /* 721 * Child is placed at back of dispatcher queue and parent gives 722 * up processor so that the child runs first after the fork. 723 * This allows the child immediately execing to break the multiple 724 * use of copy on write pages with no disk home. The parent will 725 * get to steal them back rather than uselessly copying them. 726 */ 727 static void 728 ts_forkret(kthread_t *t, kthread_t *ct) 729 { 730 proc_t *pp = ttoproc(t); 731 proc_t *cp = ttoproc(ct); 732 tsproc_t *tspp; 733 734 ASSERT(t == curthread); 735 ASSERT(MUTEX_HELD(&pidlock)); 736 737 /* 738 * Grab the child's p_lock before dropping pidlock to ensure 739 * the process does not disappear before we set it running. 740 */ 741 mutex_enter(&cp->p_lock); 742 mutex_exit(&pidlock); 743 continuelwps(cp); 744 mutex_exit(&cp->p_lock); 745 746 mutex_enter(&pp->p_lock); 747 continuelwps(pp); 748 mutex_exit(&pp->p_lock); 749 750 thread_lock(t); 751 tspp = (tsproc_t *)(t->t_cldata); 752 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp; 753 TS_NEWUMDPRI(tspp); 754 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 755 tspp->ts_dispwait = 0; 756 t->t_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri; 757 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 758 tspp->ts_flags &= ~TSKPRI; 759 THREAD_TRANSITION(t); 760 ts_setrun(t); 761 thread_unlock(t); 762 763 swtch(); 764 } 765 766 767 /* 768 * Get information about the time-sharing class into the buffer 769 * pointed to by tsinfop. The maximum configured user priority 770 * is the only information we supply. ts_getclinfo() is called 771 * for TS threads, and ia_getclinfo() is called for IA threads. 772 */ 773 static int 774 ts_getclinfo(void *infop) 775 { 776 tsinfo_t *tsinfop = (tsinfo_t *)infop; 777 tsinfop->ts_maxupri = ts_maxupri; 778 return (0); 779 } 780 781 static int 782 ia_getclinfo(void *infop) 783 { 784 iainfo_t *iainfop = (iainfo_t *)infop; 785 iainfop->ia_maxupri = ia_maxupri; 786 return (0); 787 } 788 789 790 /* 791 * Return the user mode scheduling priority range. 792 */ 793 static int 794 ts_getclpri(pcpri_t *pcprip) 795 { 796 pcprip->pc_clpmax = ts_maxupri; 797 pcprip->pc_clpmin = -ts_maxupri; 798 return (0); 799 } 800 801 802 static int 803 ia_getclpri(pcpri_t *pcprip) 804 { 805 pcprip->pc_clpmax = ia_maxupri; 806 pcprip->pc_clpmin = -ia_maxupri; 807 return (0); 808 } 809 810 811 static void 812 ts_nullsys() 813 {} 814 815 816 /* 817 * Get the time-sharing parameters of the thread pointed to by 818 * tsprocp into the buffer pointed to by tsparmsp. ts_parmsget() 819 * is called for TS threads, and ia_parmsget() is called for IA 820 * threads. 821 */ 822 static void 823 ts_parmsget(kthread_t *t, void *parmsp) 824 { 825 tsproc_t *tspp = (tsproc_t *)t->t_cldata; 826 tsparms_t *tsparmsp = (tsparms_t *)parmsp; 827 828 tsparmsp->ts_uprilim = tspp->ts_uprilim; 829 tsparmsp->ts_upri = tspp->ts_upri; 830 } 831 832 static void 833 ia_parmsget(kthread_t *t, void *parmsp) 834 { 835 tsproc_t *tspp = (tsproc_t *)t->t_cldata; 836 iaparms_t *iaparmsp = (iaparms_t *)parmsp; 837 838 iaparmsp->ia_uprilim = tspp->ts_uprilim; 839 iaparmsp->ia_upri = tspp->ts_upri; 840 if (tspp->ts_flags & TSIASET) 841 iaparmsp->ia_mode = IA_SET_INTERACTIVE; 842 else 843 iaparmsp->ia_mode = IA_INTERACTIVE_OFF; 844 } 845 846 847 /* 848 * Check the validity of the time-sharing parameters in the buffer 849 * pointed to by tsparmsp. 850 * ts_parmsin() is called for TS threads, and ia_parmsin() is called 851 * for IA threads. 852 */ 853 static int 854 ts_parmsin(void *parmsp) 855 { 856 tsparms_t *tsparmsp = (tsparms_t *)parmsp; 857 /* 858 * Check validity of parameters. 859 */ 860 if ((tsparmsp->ts_uprilim > ts_maxupri || 861 tsparmsp->ts_uprilim < -ts_maxupri) && 862 tsparmsp->ts_uprilim != TS_NOCHANGE) 863 return (EINVAL); 864 865 if ((tsparmsp->ts_upri > ts_maxupri || 866 tsparmsp->ts_upri < -ts_maxupri) && 867 tsparmsp->ts_upri != TS_NOCHANGE) 868 return (EINVAL); 869 870 return (0); 871 } 872 873 static int 874 ia_parmsin(void *parmsp) 875 { 876 iaparms_t *iaparmsp = (iaparms_t *)parmsp; 877 878 if ((iaparmsp->ia_uprilim > ia_maxupri || 879 iaparmsp->ia_uprilim < -ia_maxupri) && 880 iaparmsp->ia_uprilim != IA_NOCHANGE) { 881 return (EINVAL); 882 } 883 884 if ((iaparmsp->ia_upri > ia_maxupri || 885 iaparmsp->ia_upri < -ia_maxupri) && 886 iaparmsp->ia_upri != IA_NOCHANGE) { 887 return (EINVAL); 888 } 889 890 return (0); 891 } 892 893 894 /* 895 * Check the validity of the time-sharing parameters in the pc_vaparms_t 896 * structure vaparmsp and put them in the buffer pointed to by tsparmsp. 897 * pc_vaparms_t contains (key, value) pairs of parameter. 898 * ts_vaparmsin() is called for TS threads, and ia_vaparmsin() is called 899 * for IA threads. ts_vaparmsin() is the variable parameter version of 900 * ts_parmsin() and ia_vaparmsin() is the variable parameter version of 901 * ia_parmsin(). 902 */ 903 static int 904 ts_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp) 905 { 906 tsparms_t *tsparmsp = (tsparms_t *)parmsp; 907 int priflag = 0; 908 int limflag = 0; 909 uint_t cnt; 910 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0]; 911 912 913 /* 914 * TS_NOCHANGE (-32768) is outside of the range of values for 915 * ts_uprilim and ts_upri. If the structure tsparms_t is changed, 916 * TS_NOCHANGE should be replaced by a flag word (in the same manner 917 * as in rt.c). 918 */ 919 tsparmsp->ts_uprilim = TS_NOCHANGE; 920 tsparmsp->ts_upri = TS_NOCHANGE; 921 922 /* 923 * Get the varargs parameter and check validity of parameters. 924 */ 925 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT) 926 return (EINVAL); 927 928 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) { 929 930 switch (vpp->pc_key) { 931 case TS_KY_UPRILIM: 932 if (limflag++) 933 return (EINVAL); 934 tsparmsp->ts_uprilim = (pri_t)vpp->pc_parm; 935 if (tsparmsp->ts_uprilim > ts_maxupri || 936 tsparmsp->ts_uprilim < -ts_maxupri) 937 return (EINVAL); 938 break; 939 940 case TS_KY_UPRI: 941 if (priflag++) 942 return (EINVAL); 943 tsparmsp->ts_upri = (pri_t)vpp->pc_parm; 944 if (tsparmsp->ts_upri > ts_maxupri || 945 tsparmsp->ts_upri < -ts_maxupri) 946 return (EINVAL); 947 break; 948 949 default: 950 return (EINVAL); 951 } 952 } 953 954 if (vaparmsp->pc_vaparmscnt == 0) { 955 /* 956 * Use default parameters. 957 */ 958 tsparmsp->ts_upri = tsparmsp->ts_uprilim = 0; 959 } 960 961 return (0); 962 } 963 964 static int 965 ia_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp) 966 { 967 iaparms_t *iaparmsp = (iaparms_t *)parmsp; 968 int priflag = 0; 969 int limflag = 0; 970 int mflag = 0; 971 uint_t cnt; 972 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0]; 973 974 /* 975 * IA_NOCHANGE (-32768) is outside of the range of values for 976 * ia_uprilim, ia_upri and ia_mode. If the structure iaparms_t is 977 * changed, IA_NOCHANGE should be replaced by a flag word (in the 978 * same manner as in rt.c). 979 */ 980 iaparmsp->ia_uprilim = IA_NOCHANGE; 981 iaparmsp->ia_upri = IA_NOCHANGE; 982 iaparmsp->ia_mode = IA_NOCHANGE; 983 984 /* 985 * Get the varargs parameter and check validity of parameters. 986 */ 987 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT) 988 return (EINVAL); 989 990 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) { 991 992 switch (vpp->pc_key) { 993 case IA_KY_UPRILIM: 994 if (limflag++) 995 return (EINVAL); 996 iaparmsp->ia_uprilim = (pri_t)vpp->pc_parm; 997 if (iaparmsp->ia_uprilim > ia_maxupri || 998 iaparmsp->ia_uprilim < -ia_maxupri) 999 return (EINVAL); 1000 break; 1001 1002 case IA_KY_UPRI: 1003 if (priflag++) 1004 return (EINVAL); 1005 iaparmsp->ia_upri = (pri_t)vpp->pc_parm; 1006 if (iaparmsp->ia_upri > ia_maxupri || 1007 iaparmsp->ia_upri < -ia_maxupri) 1008 return (EINVAL); 1009 break; 1010 1011 case IA_KY_MODE: 1012 if (mflag++) 1013 return (EINVAL); 1014 iaparmsp->ia_mode = (int)vpp->pc_parm; 1015 if (iaparmsp->ia_mode != IA_SET_INTERACTIVE && 1016 iaparmsp->ia_mode != IA_INTERACTIVE_OFF) 1017 return (EINVAL); 1018 break; 1019 1020 default: 1021 return (EINVAL); 1022 } 1023 } 1024 1025 if (vaparmsp->pc_vaparmscnt == 0) { 1026 /* 1027 * Use default parameters. 1028 */ 1029 iaparmsp->ia_upri = iaparmsp->ia_uprilim = 0; 1030 iaparmsp->ia_mode = IA_SET_INTERACTIVE; 1031 } 1032 1033 return (0); 1034 } 1035 1036 /* 1037 * Nothing to do here but return success. 1038 */ 1039 /* ARGSUSED */ 1040 static int 1041 ts_parmsout(void *parmsp, pc_vaparms_t *vaparmsp) 1042 { 1043 return (0); 1044 } 1045 1046 1047 /* 1048 * Copy all selected time-sharing class parameters to the user. 1049 * The parameters are specified by a key. 1050 */ 1051 static int 1052 ts_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp) 1053 { 1054 tsparms_t *tsprmsp = (tsparms_t *)prmsp; 1055 int priflag = 0; 1056 int limflag = 0; 1057 uint_t cnt; 1058 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0]; 1059 1060 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 1061 1062 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT) 1063 return (EINVAL); 1064 1065 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) { 1066 1067 switch (vpp->pc_key) { 1068 case TS_KY_UPRILIM: 1069 if (limflag++) 1070 return (EINVAL); 1071 if (copyout(&tsprmsp->ts_uprilim, 1072 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t))) 1073 return (EFAULT); 1074 break; 1075 1076 case TS_KY_UPRI: 1077 if (priflag++) 1078 return (EINVAL); 1079 if (copyout(&tsprmsp->ts_upri, 1080 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t))) 1081 return (EFAULT); 1082 break; 1083 1084 default: 1085 return (EINVAL); 1086 } 1087 } 1088 1089 return (0); 1090 } 1091 1092 1093 /* 1094 * Copy all selected interactive class parameters to the user. 1095 * The parameters are specified by a key. 1096 */ 1097 static int 1098 ia_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp) 1099 { 1100 iaparms_t *iaprmsp = (iaparms_t *)prmsp; 1101 int priflag = 0; 1102 int limflag = 0; 1103 int mflag = 0; 1104 uint_t cnt; 1105 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0]; 1106 1107 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 1108 1109 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT) 1110 return (EINVAL); 1111 1112 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) { 1113 1114 switch (vpp->pc_key) { 1115 case IA_KY_UPRILIM: 1116 if (limflag++) 1117 return (EINVAL); 1118 if (copyout(&iaprmsp->ia_uprilim, 1119 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t))) 1120 return (EFAULT); 1121 break; 1122 1123 case IA_KY_UPRI: 1124 if (priflag++) 1125 return (EINVAL); 1126 if (copyout(&iaprmsp->ia_upri, 1127 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t))) 1128 return (EFAULT); 1129 break; 1130 1131 case IA_KY_MODE: 1132 if (mflag++) 1133 return (EINVAL); 1134 if (copyout(&iaprmsp->ia_mode, 1135 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int))) 1136 return (EFAULT); 1137 break; 1138 1139 default: 1140 return (EINVAL); 1141 } 1142 } 1143 return (0); 1144 } 1145 1146 1147 /* 1148 * Set the scheduling parameters of the thread pointed to by tsprocp 1149 * to those specified in the buffer pointed to by tsparmsp. 1150 * ts_parmsset() is called for TS threads, and ia_parmsset() is 1151 * called for IA threads. 1152 */ 1153 /* ARGSUSED */ 1154 static int 1155 ts_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp) 1156 { 1157 char nice; 1158 pri_t reqtsuprilim; 1159 pri_t reqtsupri; 1160 tsparms_t *tsparmsp = (tsparms_t *)parmsp; 1161 tsproc_t *tspp = (tsproc_t *)tx->t_cldata; 1162 1163 ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock)); 1164 1165 if (tsparmsp->ts_uprilim == TS_NOCHANGE) 1166 reqtsuprilim = tspp->ts_uprilim; 1167 else 1168 reqtsuprilim = tsparmsp->ts_uprilim; 1169 1170 if (tsparmsp->ts_upri == TS_NOCHANGE) 1171 reqtsupri = tspp->ts_upri; 1172 else 1173 reqtsupri = tsparmsp->ts_upri; 1174 1175 /* 1176 * Make sure the user priority doesn't exceed the upri limit. 1177 */ 1178 if (reqtsupri > reqtsuprilim) 1179 reqtsupri = reqtsuprilim; 1180 1181 /* 1182 * Basic permissions enforced by generic kernel code 1183 * for all classes require that a thread attempting 1184 * to change the scheduling parameters of a target 1185 * thread be privileged or have a real or effective 1186 * UID matching that of the target thread. We are not 1187 * called unless these basic permission checks have 1188 * already passed. The time-sharing class requires in 1189 * addition that the calling thread be privileged if it 1190 * is attempting to raise the upri limit above its current 1191 * value This may have been checked previously but if our 1192 * caller passed us a non-NULL credential pointer we assume 1193 * it hasn't and we check it here. 1194 */ 1195 if (reqpcredp != NULL && 1196 reqtsuprilim > tspp->ts_uprilim && 1197 secpolicy_setpriority(reqpcredp) != 0) 1198 return (EPERM); 1199 1200 /* 1201 * Set ts_nice to the nice value corresponding to the user 1202 * priority we are setting. Note that setting the nice field 1203 * of the parameter struct won't affect upri or nice. 1204 */ 1205 nice = NZERO - (reqtsupri * NZERO) / ts_maxupri; 1206 if (nice >= 2 * NZERO) 1207 nice = 2 * NZERO - 1; 1208 1209 thread_lock(tx); 1210 1211 tspp->ts_uprilim = reqtsuprilim; 1212 tspp->ts_upri = reqtsupri; 1213 TS_NEWUMDPRI(tspp); 1214 tspp->ts_nice = nice; 1215 1216 if ((tspp->ts_flags & TSKPRI) != 0) { 1217 thread_unlock(tx); 1218 return (0); 1219 } 1220 1221 tspp->ts_dispwait = 0; 1222 ts_change_priority(tx, tspp); 1223 thread_unlock(tx); 1224 return (0); 1225 } 1226 1227 1228 static int 1229 ia_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp) 1230 { 1231 tsproc_t *tspp = (tsproc_t *)tx->t_cldata; 1232 iaparms_t *iaparmsp = (iaparms_t *)parmsp; 1233 proc_t *p; 1234 pid_t pid, pgid, sid; 1235 pid_t on, off; 1236 struct stdata *stp; 1237 int sess_held; 1238 1239 /* 1240 * Handle user priority changes 1241 */ 1242 if (iaparmsp->ia_mode == IA_NOCHANGE) 1243 return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp)); 1244 1245 /* 1246 * Check permissions for changing modes. 1247 */ 1248 1249 if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) && 1250 secpolicy_setpriority(reqpcredp) != 0) { 1251 /* 1252 * Silently fail in case this is just a priocntl 1253 * call with upri and uprilim set to IA_NOCHANGE. 1254 */ 1255 return (0); 1256 } 1257 1258 ASSERT(MUTEX_HELD(&pidlock)); 1259 if ((p = ttoproc(tx)) == NULL) { 1260 return (0); 1261 } 1262 ASSERT(MUTEX_HELD(&p->p_lock)); 1263 if (p->p_stat == SIDL) { 1264 return (0); 1265 } 1266 pid = p->p_pid; 1267 sid = p->p_sessp->s_sid; 1268 pgid = p->p_pgrp; 1269 if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) { 1270 /* 1271 * session leaders must be turned on now so all processes 1272 * in the group controlling the tty will be turned on or off. 1273 * if the ia_mode is off for the session leader, 1274 * ia_set_process_group will return without setting the 1275 * processes in the group controlling the tty on. 1276 */ 1277 thread_lock(tx); 1278 tspp->ts_flags |= TSIASET; 1279 thread_unlock(tx); 1280 } 1281 mutex_enter(&p->p_sessp->s_lock); 1282 sess_held = 1; 1283 if ((pid == sid) && (p->p_sessp->s_vp != NULL) && 1284 ((stp = p->p_sessp->s_vp->v_stream) != NULL)) { 1285 if ((stp->sd_pgidp != NULL) && (stp->sd_sidp != NULL)) { 1286 pgid = stp->sd_pgidp->pid_id; 1287 sess_held = 0; 1288 mutex_exit(&p->p_sessp->s_lock); 1289 if (iaparmsp->ia_mode == 1290 IA_SET_INTERACTIVE) { 1291 off = 0; 1292 on = pgid; 1293 } else { 1294 off = pgid; 1295 on = 0; 1296 } 1297 TRACE_3(TR_FAC_IA, TR_ACTIVE_CHAIN, 1298 "active chain:pid %d gid %d %p", 1299 pid, pgid, p); 1300 ia_set_process_group(sid, off, on); 1301 } 1302 } 1303 if (sess_held) 1304 mutex_exit(&p->p_sessp->s_lock); 1305 1306 thread_lock(tx); 1307 1308 if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) { 1309 tspp->ts_flags |= TSIASET; 1310 tspp->ts_boost = ia_boost; 1311 } else { 1312 tspp->ts_flags &= ~TSIASET; 1313 tspp->ts_boost = -ia_boost; 1314 } 1315 thread_unlock(tx); 1316 1317 return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp)); 1318 } 1319 1320 static void 1321 ts_exit(kthread_t *t) 1322 { 1323 tsproc_t *tspp; 1324 1325 if (CPUCAPS_ON()) { 1326 /* 1327 * A thread could be exiting in between clock ticks, 1328 * so we need to calculate how much CPU time it used 1329 * since it was charged last time. 1330 * 1331 * CPU caps are not enforced on exiting processes - it is 1332 * usually desirable to exit as soon as possible to free 1333 * resources. 1334 */ 1335 thread_lock(t); 1336 tspp = (tsproc_t *)t->t_cldata; 1337 (void) cpucaps_charge(t, &tspp->ts_caps, CPUCAPS_CHARGE_ONLY); 1338 thread_unlock(t); 1339 } 1340 } 1341 1342 /* 1343 * Return the global scheduling priority that would be assigned 1344 * to a thread entering the time-sharing class with the ts_upri. 1345 */ 1346 static pri_t 1347 ts_globpri(kthread_t *t) 1348 { 1349 tsproc_t *tspp; 1350 pri_t tspri; 1351 1352 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock)); 1353 tspp = (tsproc_t *)t->t_cldata; 1354 tspri = tsmedumdpri + tspp->ts_upri; 1355 if (tspri > ts_maxumdpri) 1356 tspri = ts_maxumdpri; 1357 else if (tspri < 0) 1358 tspri = 0; 1359 return (ts_dptbl[tspri].ts_globpri); 1360 } 1361 1362 /* 1363 * Arrange for thread to be placed in appropriate location 1364 * on dispatcher queue. 1365 * 1366 * This is called with the current thread in TS_ONPROC and locked. 1367 */ 1368 static void 1369 ts_preempt(kthread_t *t) 1370 { 1371 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1372 klwp_t *lwp = curthread->t_lwp; 1373 pri_t oldpri = t->t_pri; 1374 1375 ASSERT(t == curthread); 1376 ASSERT(THREAD_LOCK_HELD(curthread)); 1377 1378 /* 1379 * If preempted in the kernel, make sure the thread has 1380 * a kernel priority if needed. 1381 */ 1382 if (!(tspp->ts_flags & TSKPRI) && lwp != NULL && t->t_kpri_req) { 1383 tspp->ts_flags |= TSKPRI; 1384 THREAD_CHANGE_PRI(t, ts_kmdpris[0]); 1385 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 1386 t->t_trapret = 1; /* so ts_trapret will run */ 1387 aston(t); 1388 } 1389 1390 /* 1391 * This thread may be placed on wait queue by CPU Caps. In this case we 1392 * do not need to do anything until it is removed from the wait queue. 1393 * Do not enforce CPU caps on threads running at a kernel priority 1394 */ 1395 if (CPUCAPS_ON()) { 1396 (void) cpucaps_charge(t, &tspp->ts_caps, 1397 CPUCAPS_CHARGE_ENFORCE); 1398 if (!(tspp->ts_flags & TSKPRI) && CPUCAPS_ENFORCE(t)) 1399 return; 1400 } 1401 1402 /* 1403 * If thread got preempted in the user-land then we know 1404 * it isn't holding any locks. Mark it as swappable. 1405 */ 1406 ASSERT(t->t_schedflag & TS_DONT_SWAP); 1407 if (lwp != NULL && lwp->lwp_state == LWP_USER) 1408 t->t_schedflag &= ~TS_DONT_SWAP; 1409 1410 /* 1411 * Check to see if we're doing "preemption control" here. If 1412 * we are, and if the user has requested that this thread not 1413 * be preempted, and if preemptions haven't been put off for 1414 * too long, let the preemption happen here but try to make 1415 * sure the thread is rescheduled as soon as possible. We do 1416 * this by putting it on the front of the highest priority run 1417 * queue in the TS class. If the preemption has been put off 1418 * for too long, clear the "nopreempt" bit and let the thread 1419 * be preempted. 1420 */ 1421 if (t->t_schedctl && schedctl_get_nopreempt(t)) { 1422 if (tspp->ts_timeleft > -SC_MAX_TICKS) { 1423 DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t); 1424 if (!(tspp->ts_flags & TSKPRI)) { 1425 /* 1426 * If not already remembered, remember current 1427 * priority for restoration in ts_yield(). 1428 */ 1429 if (!(tspp->ts_flags & TSRESTORE)) { 1430 tspp->ts_scpri = t->t_pri; 1431 tspp->ts_flags |= TSRESTORE; 1432 } 1433 THREAD_CHANGE_PRI(t, ts_maxumdpri); 1434 t->t_schedflag |= TS_DONT_SWAP; 1435 } 1436 schedctl_set_yield(t, 1); 1437 setfrontdq(t); 1438 goto done; 1439 } else { 1440 if (tspp->ts_flags & TSRESTORE) { 1441 THREAD_CHANGE_PRI(t, tspp->ts_scpri); 1442 tspp->ts_flags &= ~TSRESTORE; 1443 } 1444 schedctl_set_nopreempt(t, 0); 1445 DTRACE_SCHED1(schedctl__preempt, kthread_t *, t); 1446 TNF_PROBE_2(schedctl_preempt, "schedctl TS ts_preempt", 1447 /* CSTYLED */, tnf_pid, pid, ttoproc(t)->p_pid, 1448 tnf_lwpid, lwpid, t->t_tid); 1449 /* 1450 * Fall through and be preempted below. 1451 */ 1452 } 1453 } 1454 1455 if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == TSBACKQ) { 1456 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 1457 tspp->ts_dispwait = 0; 1458 tspp->ts_flags &= ~TSBACKQ; 1459 setbackdq(t); 1460 } else if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == (TSBACKQ|TSKPRI)) { 1461 tspp->ts_flags &= ~TSBACKQ; 1462 setbackdq(t); 1463 } else { 1464 setfrontdq(t); 1465 } 1466 1467 done: 1468 TRACE_2(TR_FAC_DISP, TR_PREEMPT, 1469 "preempt:tid %p old pri %d", t, oldpri); 1470 } 1471 1472 static void 1473 ts_setrun(kthread_t *t) 1474 { 1475 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1476 1477 ASSERT(THREAD_LOCK_HELD(t)); /* t should be in transition */ 1478 1479 if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) { 1480 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret; 1481 TS_NEWUMDPRI(tspp); 1482 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 1483 tspp->ts_dispwait = 0; 1484 if ((tspp->ts_flags & TSKPRI) == 0) { 1485 THREAD_CHANGE_PRI(t, 1486 ts_dptbl[tspp->ts_umdpri].ts_globpri); 1487 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 1488 } 1489 } 1490 1491 tspp->ts_flags &= ~TSBACKQ; 1492 1493 if (tspp->ts_flags & TSIA) { 1494 if (tspp->ts_flags & TSIASET) 1495 setfrontdq(t); 1496 else 1497 setbackdq(t); 1498 } else { 1499 if (t->t_disp_time != ddi_get_lbolt()) 1500 setbackdq(t); 1501 else 1502 setfrontdq(t); 1503 } 1504 } 1505 1506 1507 /* 1508 * Prepare thread for sleep. We reset the thread priority so it will 1509 * run at the kernel priority level when it wakes up. 1510 */ 1511 static void 1512 ts_sleep(kthread_t *t) 1513 { 1514 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1515 int flags; 1516 pri_t old_pri = t->t_pri; 1517 1518 ASSERT(t == curthread); 1519 ASSERT(THREAD_LOCK_HELD(t)); 1520 1521 /* 1522 * Account for time spent on CPU before going to sleep. 1523 */ 1524 (void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE); 1525 1526 flags = tspp->ts_flags; 1527 if (t->t_kpri_req) { 1528 tspp->ts_flags = flags | TSKPRI; 1529 THREAD_CHANGE_PRI(t, ts_kmdpris[0]); 1530 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 1531 t->t_trapret = 1; /* so ts_trapret will run */ 1532 aston(t); 1533 } else if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) { 1534 /* 1535 * If thread has blocked in the kernel (as opposed to 1536 * being merely preempted), recompute the user mode priority. 1537 */ 1538 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret; 1539 TS_NEWUMDPRI(tspp); 1540 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 1541 tspp->ts_dispwait = 0; 1542 1543 THREAD_CHANGE_PRI(curthread, 1544 ts_dptbl[tspp->ts_umdpri].ts_globpri); 1545 ASSERT(curthread->t_pri >= 0 && 1546 curthread->t_pri <= ts_maxglobpri); 1547 tspp->ts_flags = flags & ~TSKPRI; 1548 1549 if (DISP_MUST_SURRENDER(curthread)) 1550 cpu_surrender(curthread); 1551 } else if (flags & TSKPRI) { 1552 THREAD_CHANGE_PRI(curthread, 1553 ts_dptbl[tspp->ts_umdpri].ts_globpri); 1554 ASSERT(curthread->t_pri >= 0 && 1555 curthread->t_pri <= ts_maxglobpri); 1556 tspp->ts_flags = flags & ~TSKPRI; 1557 1558 if (DISP_MUST_SURRENDER(curthread)) 1559 cpu_surrender(curthread); 1560 } 1561 t->t_stime = ddi_get_lbolt(); /* time stamp for the swapper */ 1562 TRACE_2(TR_FAC_DISP, TR_SLEEP, 1563 "sleep:tid %p old pri %d", t, old_pri); 1564 } 1565 1566 1567 /* 1568 * Return Values: 1569 * 1570 * -1 if the thread is loaded or is not eligible to be swapped in. 1571 * 1572 * effective priority of the specified thread based on swapout time 1573 * and size of process (epri >= 0 , epri <= SHRT_MAX). 1574 */ 1575 /* ARGSUSED */ 1576 static pri_t 1577 ts_swapin(kthread_t *t, int flags) 1578 { 1579 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1580 long epri = -1; 1581 proc_t *pp = ttoproc(t); 1582 1583 ASSERT(THREAD_LOCK_HELD(t)); 1584 1585 /* 1586 * We know that pri_t is a short. 1587 * Be sure not to overrun its range. 1588 */ 1589 if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) { 1590 time_t swapout_time; 1591 1592 swapout_time = (ddi_get_lbolt() - t->t_stime) / hz; 1593 if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET))) 1594 epri = (long)DISP_PRIO(t) + swapout_time; 1595 else { 1596 /* 1597 * Threads which have been out for a long time, 1598 * have high user mode priority and are associated 1599 * with a small address space are more deserving 1600 */ 1601 epri = ts_dptbl[tspp->ts_umdpri].ts_globpri; 1602 ASSERT(epri >= 0 && epri <= ts_maxumdpri); 1603 epri += swapout_time - pp->p_swrss / nz(maxpgio)/2; 1604 } 1605 /* 1606 * Scale epri so SHRT_MAX/2 represents zero priority. 1607 */ 1608 epri += SHRT_MAX/2; 1609 if (epri < 0) 1610 epri = 0; 1611 else if (epri > SHRT_MAX) 1612 epri = SHRT_MAX; 1613 } 1614 return ((pri_t)epri); 1615 } 1616 1617 /* 1618 * Return Values 1619 * -1 if the thread isn't loaded or is not eligible to be swapped out. 1620 * 1621 * effective priority of the specified thread based on if the swapper 1622 * is in softswap or hardswap mode. 1623 * 1624 * Softswap: Return a low effective priority for threads 1625 * sleeping for more than maxslp secs. 1626 * 1627 * Hardswap: Return an effective priority such that threads 1628 * which have been in memory for a while and are 1629 * associated with a small address space are swapped 1630 * in before others. 1631 * 1632 * (epri >= 0 , epri <= SHRT_MAX). 1633 */ 1634 time_t ts_minrun = 2; /* XXX - t_pri becomes 59 within 2 secs */ 1635 time_t ts_minslp = 2; /* min time on sleep queue for hardswap */ 1636 1637 static pri_t 1638 ts_swapout(kthread_t *t, int flags) 1639 { 1640 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1641 long epri = -1; 1642 proc_t *pp = ttoproc(t); 1643 time_t swapin_time; 1644 1645 ASSERT(THREAD_LOCK_HELD(t)); 1646 1647 if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)) || 1648 (t->t_proc_flag & TP_LWPEXIT) || 1649 (t->t_state & (TS_ZOMB | TS_FREE | TS_STOPPED | 1650 TS_ONPROC | TS_WAIT)) || 1651 !(t->t_schedflag & TS_LOAD) || !SWAP_OK(t)) 1652 return (-1); 1653 1654 ASSERT(t->t_state & (TS_SLEEP | TS_RUN)); 1655 1656 /* 1657 * We know that pri_t is a short. 1658 * Be sure not to overrun its range. 1659 */ 1660 swapin_time = (ddi_get_lbolt() - t->t_stime) / hz; 1661 if (flags == SOFTSWAP) { 1662 if (t->t_state == TS_SLEEP && swapin_time > maxslp) { 1663 epri = 0; 1664 } else { 1665 return ((pri_t)epri); 1666 } 1667 } else { 1668 pri_t pri; 1669 1670 if ((t->t_state == TS_SLEEP && swapin_time > ts_minslp) || 1671 (t->t_state == TS_RUN && swapin_time > ts_minrun)) { 1672 pri = ts_dptbl[tspp->ts_umdpri].ts_globpri; 1673 ASSERT(pri >= 0 && pri <= ts_maxumdpri); 1674 epri = swapin_time - 1675 (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri; 1676 } else { 1677 return ((pri_t)epri); 1678 } 1679 } 1680 1681 /* 1682 * Scale epri so SHRT_MAX/2 represents zero priority. 1683 */ 1684 epri += SHRT_MAX/2; 1685 if (epri < 0) 1686 epri = 0; 1687 else if (epri > SHRT_MAX) 1688 epri = SHRT_MAX; 1689 1690 return ((pri_t)epri); 1691 } 1692 1693 /* 1694 * Check for time slice expiration. If time slice has expired 1695 * move thread to priority specified in tsdptbl for time slice expiration 1696 * and set runrun to cause preemption. 1697 */ 1698 static void 1699 ts_tick(kthread_t *t) 1700 { 1701 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1702 klwp_t *lwp; 1703 boolean_t call_cpu_surrender = B_FALSE; 1704 pri_t oldpri = t->t_pri; 1705 1706 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock)); 1707 1708 thread_lock(t); 1709 1710 /* 1711 * Keep track of thread's project CPU usage. Note that projects 1712 * get charged even when threads are running in the kernel. 1713 */ 1714 if (CPUCAPS_ON()) { 1715 call_cpu_surrender = cpucaps_charge(t, &tspp->ts_caps, 1716 CPUCAPS_CHARGE_ENFORCE) && !(tspp->ts_flags & TSKPRI); 1717 } 1718 1719 if ((tspp->ts_flags & TSKPRI) == 0) { 1720 if (--tspp->ts_timeleft <= 0) { 1721 pri_t new_pri; 1722 1723 /* 1724 * If we're doing preemption control and trying to 1725 * avoid preempting this thread, just note that 1726 * the thread should yield soon and let it keep 1727 * running (unless it's been a while). 1728 */ 1729 if (t->t_schedctl && schedctl_get_nopreempt(t)) { 1730 if (tspp->ts_timeleft > -SC_MAX_TICKS) { 1731 DTRACE_SCHED1(schedctl__nopreempt, 1732 kthread_t *, t); 1733 schedctl_set_yield(t, 1); 1734 thread_unlock_nopreempt(t); 1735 return; 1736 } 1737 1738 TNF_PROBE_2(schedctl_failsafe, 1739 "schedctl TS ts_tick", /* CSTYLED */, 1740 tnf_pid, pid, ttoproc(t)->p_pid, 1741 tnf_lwpid, lwpid, t->t_tid); 1742 } 1743 tspp->ts_flags &= ~TSRESTORE; 1744 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp; 1745 TS_NEWUMDPRI(tspp); 1746 tspp->ts_dispwait = 0; 1747 new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri; 1748 ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri); 1749 /* 1750 * When the priority of a thread is changed, 1751 * it may be necessary to adjust its position 1752 * on a sleep queue or dispatch queue. 1753 * The function thread_change_pri accomplishes 1754 * this. 1755 */ 1756 if (thread_change_pri(t, new_pri, 0)) { 1757 if ((t->t_schedflag & TS_LOAD) && 1758 (lwp = t->t_lwp) && 1759 lwp->lwp_state == LWP_USER) 1760 t->t_schedflag &= ~TS_DONT_SWAP; 1761 tspp->ts_timeleft = 1762 ts_dptbl[tspp->ts_cpupri].ts_quantum; 1763 } else { 1764 call_cpu_surrender = B_TRUE; 1765 } 1766 TRACE_2(TR_FAC_DISP, TR_TICK, 1767 "tick:tid %p old pri %d", t, oldpri); 1768 } else if (t->t_state == TS_ONPROC && 1769 t->t_pri < t->t_disp_queue->disp_maxrunpri) { 1770 call_cpu_surrender = B_TRUE; 1771 } 1772 } 1773 1774 if (call_cpu_surrender) { 1775 tspp->ts_flags |= TSBACKQ; 1776 cpu_surrender(t); 1777 } 1778 1779 thread_unlock_nopreempt(t); /* clock thread can't be preempted */ 1780 } 1781 1782 1783 /* 1784 * If thread is currently at a kernel mode priority (has slept) 1785 * we assign it the appropriate user mode priority and time quantum 1786 * here. If we are lowering the thread's priority below that of 1787 * other runnable threads we will normally set runrun via cpu_surrender() to 1788 * cause preemption. 1789 */ 1790 static void 1791 ts_trapret(kthread_t *t) 1792 { 1793 tsproc_t *tspp = (tsproc_t *)t->t_cldata; 1794 cpu_t *cp = CPU; 1795 pri_t old_pri = curthread->t_pri; 1796 1797 ASSERT(THREAD_LOCK_HELD(t)); 1798 ASSERT(t == curthread); 1799 ASSERT(cp->cpu_dispthread == t); 1800 ASSERT(t->t_state == TS_ONPROC); 1801 1802 t->t_kpri_req = 0; 1803 if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) { 1804 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret; 1805 TS_NEWUMDPRI(tspp); 1806 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 1807 tspp->ts_dispwait = 0; 1808 1809 /* 1810 * If thread has blocked in the kernel (as opposed to 1811 * being merely preempted), recompute the user mode priority. 1812 */ 1813 THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri); 1814 cp->cpu_dispatch_pri = DISP_PRIO(t); 1815 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 1816 tspp->ts_flags &= ~TSKPRI; 1817 1818 if (DISP_MUST_SURRENDER(t)) 1819 cpu_surrender(t); 1820 } else if (tspp->ts_flags & TSKPRI) { 1821 /* 1822 * If thread has blocked in the kernel (as opposed to 1823 * being merely preempted), recompute the user mode priority. 1824 */ 1825 THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri); 1826 cp->cpu_dispatch_pri = DISP_PRIO(t); 1827 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 1828 tspp->ts_flags &= ~TSKPRI; 1829 1830 if (DISP_MUST_SURRENDER(t)) 1831 cpu_surrender(t); 1832 } 1833 1834 /* 1835 * Swapout lwp if the swapper is waiting for this thread to 1836 * reach a safe point. 1837 */ 1838 if ((t->t_schedflag & TS_SWAPENQ) && !(tspp->ts_flags & TSIASET)) { 1839 thread_unlock(t); 1840 swapout_lwp(ttolwp(t)); 1841 thread_lock(t); 1842 } 1843 1844 TRACE_2(TR_FAC_DISP, TR_TRAPRET, 1845 "trapret:tid %p old pri %d", t, old_pri); 1846 } 1847 1848 1849 /* 1850 * Update the ts_dispwait values of all time sharing threads that 1851 * are currently runnable at a user mode priority and bump the priority 1852 * if ts_dispwait exceeds ts_maxwait. Called once per second via 1853 * timeout which we reset here. 1854 * 1855 * There are several lists of time sharing threads broken up by a hash on 1856 * the thread pointer. Each list has its own lock. This avoids blocking 1857 * all ts_enterclass, ts_fork, and ts_exitclass operations while ts_update 1858 * runs. ts_update traverses each list in turn. 1859 * 1860 * If multiple threads have their priorities updated to the same value, 1861 * the system implicitly favors the one that is updated first (since it 1862 * winds up first on the run queue). To avoid this unfairness, the 1863 * traversal of threads starts at the list indicated by a marker. When 1864 * threads in more than one list have their priorities updated, the marker 1865 * is moved. This changes the order the threads will be placed on the run 1866 * queue the next time ts_update is called and preserves fairness over the 1867 * long run. The marker doesn't need to be protected by a lock since it's 1868 * only accessed by ts_update, which is inherently single-threaded (only 1869 * one instance can be running at a time). 1870 */ 1871 static void 1872 ts_update(void *arg) 1873 { 1874 int i; 1875 int new_marker = -1; 1876 static int ts_update_marker; 1877 1878 /* 1879 * Start with the ts_update_marker list, then do the rest. 1880 */ 1881 i = ts_update_marker; 1882 do { 1883 /* 1884 * If this is the first list after the current marker to 1885 * have threads with priorities updated, advance the marker 1886 * to this list for the next time ts_update runs. 1887 */ 1888 if (ts_update_list(i) && new_marker == -1 && 1889 i != ts_update_marker) { 1890 new_marker = i; 1891 } 1892 } while ((i = TS_LIST_NEXT(i)) != ts_update_marker); 1893 1894 /* advance marker for next ts_update call */ 1895 if (new_marker != -1) 1896 ts_update_marker = new_marker; 1897 1898 (void) timeout(ts_update, arg, hz); 1899 } 1900 1901 /* 1902 * Updates priority for a list of threads. Returns 1 if the priority of 1903 * one of the threads was actually updated, 0 if none were for various 1904 * reasons (thread is no longer in the TS or IA class, isn't runnable, 1905 * hasn't waited long enough, has the preemption control no-preempt bit 1906 * set, etc.) 1907 */ 1908 static int 1909 ts_update_list(int i) 1910 { 1911 tsproc_t *tspp; 1912 kthread_t *tx; 1913 int updated = 0; 1914 1915 mutex_enter(&ts_list_lock[i]); 1916 for (tspp = ts_plisthead[i].ts_next; tspp != &ts_plisthead[i]; 1917 tspp = tspp->ts_next) { 1918 tx = tspp->ts_tp; 1919 /* 1920 * Lock the thread and verify state. 1921 */ 1922 thread_lock(tx); 1923 /* 1924 * Skip the thread if it is no longer in the TS (or IA) class. 1925 */ 1926 if (tx->t_clfuncs != &ts_classfuncs.thread && 1927 tx->t_clfuncs != &ia_classfuncs.thread) 1928 goto next; 1929 tspp->ts_dispwait++; 1930 if ((tspp->ts_flags & TSKPRI) != 0) 1931 goto next; 1932 if (tspp->ts_dispwait <= ts_dptbl[tspp->ts_umdpri].ts_maxwait) 1933 goto next; 1934 if (tx->t_schedctl && schedctl_get_nopreempt(tx)) 1935 goto next; 1936 if (tx->t_state != TS_RUN && tx->t_state != TS_WAIT && 1937 (tx->t_state != TS_SLEEP || !ts_sleep_promote)) { 1938 /* make next syscall/trap do CL_TRAPRET */ 1939 tx->t_trapret = 1; 1940 aston(tx); 1941 goto next; 1942 } 1943 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_lwait; 1944 TS_NEWUMDPRI(tspp); 1945 tspp->ts_dispwait = 0; 1946 updated = 1; 1947 1948 /* 1949 * Only dequeue it if needs to move; otherwise it should 1950 * just round-robin here. 1951 */ 1952 if (tx->t_pri != ts_dptbl[tspp->ts_umdpri].ts_globpri) { 1953 pri_t oldpri = tx->t_pri; 1954 ts_change_priority(tx, tspp); 1955 TRACE_2(TR_FAC_DISP, TR_UPDATE, 1956 "update:tid %p old pri %d", tx, oldpri); 1957 } 1958 next: 1959 thread_unlock(tx); 1960 } 1961 mutex_exit(&ts_list_lock[i]); 1962 1963 return (updated); 1964 } 1965 1966 /* 1967 * Processes waking up go to the back of their queue. We don't 1968 * need to assign a time quantum here because thread is still 1969 * at a kernel mode priority and the time slicing is not done 1970 * for threads running in the kernel after sleeping. The proper 1971 * time quantum will be assigned by ts_trapret before the thread 1972 * returns to user mode. 1973 */ 1974 static void 1975 ts_wakeup(kthread_t *t) 1976 { 1977 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 1978 1979 ASSERT(THREAD_LOCK_HELD(t)); 1980 1981 t->t_stime = ddi_get_lbolt(); /* time stamp for the swapper */ 1982 1983 if (tspp->ts_flags & TSKPRI) { 1984 tspp->ts_flags &= ~TSBACKQ; 1985 if (tspp->ts_flags & TSIASET) 1986 setfrontdq(t); 1987 else 1988 setbackdq(t); 1989 } else if (t->t_kpri_req) { 1990 /* 1991 * Give thread a priority boost if we were asked. 1992 */ 1993 tspp->ts_flags |= TSKPRI; 1994 THREAD_CHANGE_PRI(t, ts_kmdpris[0]); 1995 setbackdq(t); 1996 t->t_trapret = 1; /* so that ts_trapret will run */ 1997 aston(t); 1998 } else { 1999 if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) { 2000 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret; 2001 TS_NEWUMDPRI(tspp); 2002 tspp->ts_timeleft = 2003 ts_dptbl[tspp->ts_cpupri].ts_quantum; 2004 tspp->ts_dispwait = 0; 2005 THREAD_CHANGE_PRI(t, 2006 ts_dptbl[tspp->ts_umdpri].ts_globpri); 2007 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 2008 } 2009 2010 tspp->ts_flags &= ~TSBACKQ; 2011 2012 if (tspp->ts_flags & TSIA) { 2013 if (tspp->ts_flags & TSIASET) 2014 setfrontdq(t); 2015 else 2016 setbackdq(t); 2017 } else { 2018 if (t->t_disp_time != ddi_get_lbolt()) 2019 setbackdq(t); 2020 else 2021 setfrontdq(t); 2022 } 2023 } 2024 } 2025 2026 2027 /* 2028 * When a thread yields, put it on the back of the run queue. 2029 */ 2030 static void 2031 ts_yield(kthread_t *t) 2032 { 2033 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 2034 2035 ASSERT(t == curthread); 2036 ASSERT(THREAD_LOCK_HELD(t)); 2037 2038 /* 2039 * Collect CPU usage spent before yielding 2040 */ 2041 (void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE); 2042 2043 /* 2044 * Clear the preemption control "yield" bit since the user is 2045 * doing a yield. 2046 */ 2047 if (t->t_schedctl) 2048 schedctl_set_yield(t, 0); 2049 /* 2050 * If ts_preempt() artifically increased the thread's priority 2051 * to avoid preemption, restore the original priority now. 2052 */ 2053 if (tspp->ts_flags & TSRESTORE) { 2054 THREAD_CHANGE_PRI(t, tspp->ts_scpri); 2055 tspp->ts_flags &= ~TSRESTORE; 2056 } 2057 if (tspp->ts_timeleft <= 0) { 2058 /* 2059 * Time slice was artificially extended to avoid 2060 * preemption, so pretend we're preempting it now. 2061 */ 2062 DTRACE_SCHED1(schedctl__yield, int, -tspp->ts_timeleft); 2063 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp; 2064 TS_NEWUMDPRI(tspp); 2065 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum; 2066 tspp->ts_dispwait = 0; 2067 THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri); 2068 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri); 2069 } 2070 tspp->ts_flags &= ~TSBACKQ; 2071 setbackdq(t); 2072 } 2073 2074 2075 /* 2076 * Increment the nice value of the specified thread by incr and 2077 * return the new value in *retvalp. 2078 */ 2079 static int 2080 ts_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp) 2081 { 2082 int newnice; 2083 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 2084 tsparms_t tsparms; 2085 2086 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock)); 2087 2088 /* If there's no change to priority, just return current setting */ 2089 if (incr == 0) { 2090 if (retvalp) { 2091 *retvalp = tspp->ts_nice - NZERO; 2092 } 2093 return (0); 2094 } 2095 2096 if ((incr < 0 || incr > 2 * NZERO) && 2097 secpolicy_setpriority(cr) != 0) 2098 return (EPERM); 2099 2100 /* 2101 * Specifying a nice increment greater than the upper limit of 2102 * 2 * NZERO - 1 will result in the thread's nice value being 2103 * set to the upper limit. We check for this before computing 2104 * the new value because otherwise we could get overflow 2105 * if a privileged process specified some ridiculous increment. 2106 */ 2107 if (incr > 2 * NZERO - 1) 2108 incr = 2 * NZERO - 1; 2109 2110 newnice = tspp->ts_nice + incr; 2111 if (newnice >= 2 * NZERO) 2112 newnice = 2 * NZERO - 1; 2113 else if (newnice < 0) 2114 newnice = 0; 2115 2116 tsparms.ts_uprilim = tsparms.ts_upri = 2117 -((newnice - NZERO) * ts_maxupri) / NZERO; 2118 /* 2119 * Reset the uprilim and upri values of the thread. 2120 * Call ts_parmsset even if thread is interactive since we're 2121 * not changing mode. 2122 */ 2123 (void) ts_parmsset(t, (void *)&tsparms, (id_t)0, (cred_t *)NULL); 2124 2125 /* 2126 * Although ts_parmsset already reset ts_nice it may 2127 * not have been set to precisely the value calculated above 2128 * because ts_parmsset determines the nice value from the 2129 * user priority and we may have truncated during the integer 2130 * conversion from nice value to user priority and back. 2131 * We reset ts_nice to the value we calculated above. 2132 */ 2133 tspp->ts_nice = (char)newnice; 2134 2135 if (retvalp) 2136 *retvalp = newnice - NZERO; 2137 return (0); 2138 } 2139 2140 /* 2141 * Increment the priority of the specified thread by incr and 2142 * return the new value in *retvalp. 2143 */ 2144 static int 2145 ts_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp) 2146 { 2147 int newpri; 2148 tsproc_t *tspp = (tsproc_t *)(t->t_cldata); 2149 tsparms_t tsparms; 2150 2151 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock)); 2152 2153 /* If there's no change to the priority, just return current setting */ 2154 if (incr == 0) { 2155 *retvalp = tspp->ts_upri; 2156 return (0); 2157 } 2158 2159 newpri = tspp->ts_upri + incr; 2160 if (newpri > ts_maxupri || newpri < -ts_maxupri) 2161 return (EINVAL); 2162 2163 *retvalp = newpri; 2164 tsparms.ts_uprilim = tsparms.ts_upri = newpri; 2165 /* 2166 * Reset the uprilim and upri values of the thread. 2167 * Call ts_parmsset even if thread is interactive since we're 2168 * not changing mode. 2169 */ 2170 return (ts_parmsset(t, &tsparms, 0, cr)); 2171 } 2172 2173 /* 2174 * ia_set_process_group marks foreground processes as interactive 2175 * and background processes as non-interactive iff the session 2176 * leader is interactive. This routine is called from two places: 2177 * strioctl:SPGRP when a new process group gets 2178 * control of the tty. 2179 * ia_parmsset-when the process in question is a session leader. 2180 * ia_set_process_group assumes that pidlock is held by the caller, 2181 * either strioctl or priocntlsys. If the caller is priocntlsys 2182 * (via ia_parmsset) then the p_lock of the session leader is held 2183 * and the code needs to be careful about acquiring other p_locks. 2184 */ 2185 static void 2186 ia_set_process_group(pid_t sid, pid_t bg_pgid, pid_t fg_pgid) 2187 { 2188 proc_t *leader, *fg, *bg; 2189 tsproc_t *tspp; 2190 kthread_t *tx; 2191 int plocked = 0; 2192 2193 ASSERT(MUTEX_HELD(&pidlock)); 2194 2195 /* 2196 * see if the session leader is interactive AND 2197 * if it is currently "on" AND controlling a tty 2198 * iff it is then make the processes in the foreground 2199 * group interactive and the processes in the background 2200 * group non-interactive. 2201 */ 2202 if ((leader = (proc_t *)prfind(sid)) == NULL) { 2203 return; 2204 } 2205 if (leader->p_stat == SIDL) { 2206 return; 2207 } 2208 if ((tx = proctot(leader)) == NULL) { 2209 return; 2210 } 2211 /* 2212 * XXX do all the threads in the leader 2213 */ 2214 if (tx->t_cid != ia_cid) { 2215 return; 2216 } 2217 tspp = tx->t_cldata; 2218 /* 2219 * session leaders that are not interactive need not have 2220 * any processing done for them. They are typically shells 2221 * that do not have focus and are changing the process group 2222 * attatched to the tty, e.g. a process that is exiting 2223 */ 2224 mutex_enter(&leader->p_sessp->s_lock); 2225 if (!(tspp->ts_flags & TSIASET) || 2226 (leader->p_sessp->s_vp == NULL) || 2227 (leader->p_sessp->s_vp->v_stream == NULL)) { 2228 mutex_exit(&leader->p_sessp->s_lock); 2229 return; 2230 } 2231 mutex_exit(&leader->p_sessp->s_lock); 2232 2233 /* 2234 * If we're already holding the leader's p_lock, we should use 2235 * mutex_tryenter instead of mutex_enter to avoid deadlocks from 2236 * lock ordering violations. 2237 */ 2238 if (mutex_owned(&leader->p_lock)) 2239 plocked = 1; 2240 2241 if (fg_pgid == 0) 2242 goto skip; 2243 /* 2244 * now look for all processes in the foreground group and 2245 * make them interactive 2246 */ 2247 for (fg = (proc_t *)pgfind(fg_pgid); fg != NULL; fg = fg->p_pglink) { 2248 /* 2249 * if the process is SIDL it's begin forked, ignore it 2250 */ 2251 if (fg->p_stat == SIDL) { 2252 continue; 2253 } 2254 /* 2255 * sesssion leaders must be turned on/off explicitly 2256 * not implicitly as happens to other members of 2257 * the process group. 2258 */ 2259 if (fg->p_pid == fg->p_sessp->s_sid) { 2260 continue; 2261 } 2262 2263 TRACE_1(TR_FAC_IA, TR_GROUP_ON, 2264 "group on:proc %p", fg); 2265 2266 if (plocked) { 2267 if (mutex_tryenter(&fg->p_lock) == 0) 2268 continue; 2269 } else { 2270 mutex_enter(&fg->p_lock); 2271 } 2272 2273 if ((tx = proctot(fg)) == NULL) { 2274 mutex_exit(&fg->p_lock); 2275 continue; 2276 } 2277 do { 2278 thread_lock(tx); 2279 /* 2280 * if this thread is not interactive continue 2281 */ 2282 if (tx->t_cid != ia_cid) { 2283 thread_unlock(tx); 2284 continue; 2285 } 2286 tspp = tx->t_cldata; 2287 tspp->ts_flags |= TSIASET; 2288 tspp->ts_boost = ia_boost; 2289 TS_NEWUMDPRI(tspp); 2290 if ((tspp->ts_flags & TSKPRI) != 0) { 2291 thread_unlock(tx); 2292 continue; 2293 } 2294 tspp->ts_dispwait = 0; 2295 ts_change_priority(tx, tspp); 2296 thread_unlock(tx); 2297 } while ((tx = tx->t_forw) != fg->p_tlist); 2298 mutex_exit(&fg->p_lock); 2299 } 2300 skip: 2301 if (bg_pgid == 0) 2302 return; 2303 for (bg = (proc_t *)pgfind(bg_pgid); bg != NULL; bg = bg->p_pglink) { 2304 if (bg->p_stat == SIDL) { 2305 continue; 2306 } 2307 /* 2308 * sesssion leaders must be turned off explicitly 2309 * not implicitly as happens to other members of 2310 * the process group. 2311 */ 2312 if (bg->p_pid == bg->p_sessp->s_sid) { 2313 continue; 2314 } 2315 2316 TRACE_1(TR_FAC_IA, TR_GROUP_OFF, 2317 "group off:proc %p", bg); 2318 2319 if (plocked) { 2320 if (mutex_tryenter(&bg->p_lock) == 0) 2321 continue; 2322 } else { 2323 mutex_enter(&bg->p_lock); 2324 } 2325 2326 if ((tx = proctot(bg)) == NULL) { 2327 mutex_exit(&bg->p_lock); 2328 continue; 2329 } 2330 do { 2331 thread_lock(tx); 2332 /* 2333 * if this thread is not interactive continue 2334 */ 2335 if (tx->t_cid != ia_cid) { 2336 thread_unlock(tx); 2337 continue; 2338 } 2339 tspp = tx->t_cldata; 2340 tspp->ts_flags &= ~TSIASET; 2341 tspp->ts_boost = -ia_boost; 2342 TS_NEWUMDPRI(tspp); 2343 if ((tspp->ts_flags & TSKPRI) != 0) { 2344 thread_unlock(tx); 2345 continue; 2346 } 2347 2348 tspp->ts_dispwait = 0; 2349 ts_change_priority(tx, tspp); 2350 thread_unlock(tx); 2351 } while ((tx = tx->t_forw) != bg->p_tlist); 2352 mutex_exit(&bg->p_lock); 2353 } 2354 } 2355 2356 2357 static void 2358 ts_change_priority(kthread_t *t, tsproc_t *tspp) 2359 { 2360 pri_t new_pri; 2361 2362 ASSERT(THREAD_LOCK_HELD(t)); 2363 new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri; 2364 ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri); 2365 tspp->ts_flags &= ~TSRESTORE; 2366 t->t_cpri = tspp->ts_upri; 2367 if (t == curthread || t->t_state == TS_ONPROC) { 2368 /* curthread is always onproc */ 2369 cpu_t *cp = t->t_disp_queue->disp_cpu; 2370 THREAD_CHANGE_PRI(t, new_pri); 2371 if (t == cp->cpu_dispthread) 2372 cp->cpu_dispatch_pri = DISP_PRIO(t); 2373 if (DISP_MUST_SURRENDER(t)) { 2374 tspp->ts_flags |= TSBACKQ; 2375 cpu_surrender(t); 2376 } else { 2377 tspp->ts_timeleft = 2378 ts_dptbl[tspp->ts_cpupri].ts_quantum; 2379 } 2380 } else { 2381 int frontq; 2382 2383 frontq = (tspp->ts_flags & TSIASET) != 0; 2384 /* 2385 * When the priority of a thread is changed, 2386 * it may be necessary to adjust its position 2387 * on a sleep queue or dispatch queue. 2388 * The function thread_change_pri accomplishes 2389 * this. 2390 */ 2391 if (thread_change_pri(t, new_pri, frontq)) { 2392 /* 2393 * The thread was on a run queue. Reset 2394 * its CPU timeleft from the quantum 2395 * associated with the new priority. 2396 */ 2397 tspp->ts_timeleft = 2398 ts_dptbl[tspp->ts_cpupri].ts_quantum; 2399 } else { 2400 tspp->ts_flags |= TSBACKQ; 2401 } 2402 } 2403 } 2404 2405 static int 2406 ts_alloc(void **p, int flag) 2407 { 2408 void *bufp; 2409 bufp = kmem_alloc(sizeof (tsproc_t), flag); 2410 if (bufp == NULL) { 2411 return (ENOMEM); 2412 } else { 2413 *p = bufp; 2414 return (0); 2415 } 2416 } 2417 2418 static void 2419 ts_free(void *bufp) 2420 { 2421 if (bufp) 2422 kmem_free(bufp, sizeof (tsproc_t)); 2423 } 2424