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