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