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