4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013, Joyent, Inc. All rights reserved.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/sysmacros.h>
35 #include <sys/session.h>
36 #include <sys/strsubr.h>
37 #include <sys/signal.h>
39 #include <sys/priocntl.h>
40 #include <sys/class.h>
42 #include <sys/procset.h>
43 #include <sys/debug.h>
45 #include <sys/tspriocntl.h>
46 #include <sys/iapriocntl.h>
48 #include <sys/errno.h>
49 #include <sys/cpuvar.h>
50 #include <sys/systm.h> /* for lbolt */
51 #include <sys/vtrace.h>
52 #include <sys/vmsystm.h>
53 #include <sys/schedctl.h>
54 #include <sys/tnf_probe.h>
55 #include <sys/atomic.h>
56 #include <sys/policy.h>
58 #include <sys/cpupart.h>
60 #include <vm/seg_kmem.h>
61 #include <sys/modctl.h>
62 #include <sys/cpucaps.h>
64 static pri_t
ts_init(id_t
, int, classfuncs_t
**);
66 static struct sclass csw
= {
72 static struct modlsched modlsched
= {
73 &mod_schedops
, "time sharing sched class", &csw
76 static struct modlinkage modlinkage
= {
77 MODREV_1
, (void *)&modlsched
, NULL
83 return (mod_install(&modlinkage
));
89 return (EBUSY
); /* don't remove TS for now */
93 _info(struct modinfo
*modinfop
)
95 return (mod_info(&modlinkage
, modinfop
));
99 * Class specific code for the time-sharing class
104 * Extern declarations for variables defined in the ts master file
108 pri_t ts_maxupri
= TSMAXUPRI
; /* max time-sharing user priority */
109 pri_t ts_maxumdpri
; /* maximum user mode ts priority */
111 pri_t ia_maxupri
= IAMAXUPRI
; /* max interactive user priority */
112 pri_t ia_boost
= IA_BOOST
; /* boost value for interactive */
114 tsdpent_t
*ts_dptbl
; /* time-sharing disp parameter table */
115 pri_t
*ts_kmdpris
; /* array of global pris used by ts procs when */
116 /* sleeping or running in kernel after sleep */
120 int ts_sleep_promote
= 1;
122 #define tsmedumdpri (ts_maxumdpri >> 1)
124 #define TS_NEWUMDPRI(tspp) \
127 pri = (tspp)->ts_cpupri + (tspp)->ts_upri + (tspp)->ts_boost; \
128 if (pri > ts_maxumdpri) \
129 (tspp)->ts_umdpri = ts_maxumdpri; \
131 (tspp)->ts_umdpri = 0; \
133 (tspp)->ts_umdpri = pri; \
134 ASSERT((tspp)->ts_umdpri >= 0 && (tspp)->ts_umdpri <= ts_maxumdpri); \
138 * The tsproc_t structures are kept in an array of circular doubly linked
139 * lists. A hash on the thread pointer is used to determine which list
140 * each thread should be placed. Each list has a dummy "head" which is
141 * never removed, so the list is never empty. ts_update traverses these
142 * lists to update the priorities of threads that have been waiting on
146 #define TS_LISTS 16 /* number of lists, must be power of 2 */
148 /* hash function, argument is a thread pointer */
149 #define TS_LIST_HASH(tp) (((uintptr_t)(tp) >> 9) & (TS_LISTS - 1))
151 /* iterate to the next list */
152 #define TS_LIST_NEXT(i) (((i) + 1) & (TS_LISTS - 1))
155 * Insert thread into the appropriate tsproc list.
157 #define TS_LIST_INSERT(tspp) \
159 int index = TS_LIST_HASH(tspp->ts_tp); \
160 kmutex_t *lockp = &ts_list_lock[index]; \
161 tsproc_t *headp = &ts_plisthead[index]; \
162 mutex_enter(lockp); \
163 tspp->ts_next = headp->ts_next; \
164 tspp->ts_prev = headp; \
165 headp->ts_next->ts_prev = tspp; \
166 headp->ts_next = tspp; \
171 * Remove thread from tsproc list.
173 #define TS_LIST_DELETE(tspp) \
175 int index = TS_LIST_HASH(tspp->ts_tp); \
176 kmutex_t *lockp = &ts_list_lock[index]; \
177 mutex_enter(lockp); \
178 tspp->ts_prev->ts_next = tspp->ts_next; \
179 tspp->ts_next->ts_prev = tspp->ts_prev; \
184 static int ts_admin(caddr_t
, cred_t
*);
185 static int ts_enterclass(kthread_t
*, id_t
, void *, cred_t
*, void *);
186 static int ts_fork(kthread_t
*, kthread_t
*, void *);
187 static int ts_getclinfo(void *);
188 static int ts_getclpri(pcpri_t
*);
189 static int ts_parmsin(void *);
190 static int ts_parmsout(void *, pc_vaparms_t
*);
191 static int ts_vaparmsin(void *, pc_vaparms_t
*);
192 static int ts_vaparmsout(void *, pc_vaparms_t
*);
193 static int ts_parmsset(kthread_t
*, void *, id_t
, cred_t
*);
194 static void ts_exit(kthread_t
*);
195 static int ts_donice(kthread_t
*, cred_t
*, int, int *);
196 static int ts_doprio(kthread_t
*, cred_t
*, int, int *);
197 static void ts_exitclass(void *);
198 static int ts_canexit(kthread_t
*, cred_t
*);
199 static void ts_forkret(kthread_t
*, kthread_t
*);
200 static void ts_nullsys();
201 static void ts_parmsget(kthread_t
*, void *);
202 static void ts_preempt(kthread_t
*);
203 static void ts_setrun(kthread_t
*);
204 static void ts_sleep(kthread_t
*);
205 static void ts_tick(kthread_t
*);
206 static void ts_trapret(kthread_t
*);
207 static void ts_update(void *);
208 static int ts_update_list(int);
209 static void ts_wakeup(kthread_t
*);
210 static pri_t
ts_globpri(kthread_t
*);
211 static void ts_yield(kthread_t
*);
212 extern tsdpent_t
*ts_getdptbl(void);
213 extern pri_t
*ts_getkmdpris(void);
214 extern pri_t
td_getmaxumdpri(void);
215 static int ts_alloc(void **, int);
216 static void ts_free(void *);
218 pri_t
ia_init(id_t
, int, classfuncs_t
**);
219 static int ia_getclinfo(void *);
220 static int ia_getclpri(pcpri_t
*);
221 static int ia_parmsin(void *);
222 static int ia_vaparmsin(void *, pc_vaparms_t
*);
223 static int ia_vaparmsout(void *, pc_vaparms_t
*);
224 static int ia_parmsset(kthread_t
*, void *, id_t
, cred_t
*);
225 static void ia_parmsget(kthread_t
*, void *);
226 static void ia_set_process_group(pid_t
, pid_t
, pid_t
);
228 static void ts_change_priority(kthread_t
*, tsproc_t
*);
230 extern pri_t ts_maxkmdpri
; /* maximum kernel mode ts priority */
231 static pri_t ts_maxglobpri
; /* maximum global priority used by ts class */
232 static kmutex_t ts_dptblock
; /* protects time sharing dispatch table */
233 static kmutex_t ts_list_lock
[TS_LISTS
]; /* protects tsproc lists */
234 static tsproc_t ts_plisthead
[TS_LISTS
]; /* dummy tsproc at head of lists */
236 static gid_t IA_gid
= 0;
238 static struct classfuncs ts_classfuncs
= {
239 /* class functions */
250 /* thread functions */
258 ts_nullsys
, /* stop */
260 ts_nullsys
, /* active */
261 ts_nullsys
, /* inactive */
270 ts_nullsys
, /* set_process_group */
276 * ia_classfuncs is used for interactive class threads; IA threads are stored
277 * on the same class list as TS threads, and most of the class functions are
278 * identical, but a few have different enough functionality to require their
281 static struct classfuncs ia_classfuncs
= {
282 /* class functions */
293 /* thread functions */
301 ts_nullsys
, /* stop */
303 ts_nullsys
, /* active */
304 ts_nullsys
, /* inactive */
313 ia_set_process_group
,
320 * Time sharing class initialization. Called by dispinit() at boot time.
321 * We can ignore the clparmsz argument since we know that the smallest
322 * possible parameter buffer is big enough for us.
326 ts_init(id_t cid
, int clparmsz
, classfuncs_t
**clfuncspp
)
329 extern pri_t
ts_getmaxumdpri(void);
331 ts_dptbl
= ts_getdptbl();
332 ts_kmdpris
= ts_getkmdpris();
333 ts_maxumdpri
= ts_getmaxumdpri();
334 ts_maxglobpri
= MAX(ts_kmdpris
[0], ts_dptbl
[ts_maxumdpri
].ts_globpri
);
337 * Initialize the tsproc lists.
339 for (i
= 0; i
< TS_LISTS
; i
++) {
340 ts_plisthead
[i
].ts_next
= ts_plisthead
[i
].ts_prev
=
345 * We're required to return a pointer to our classfuncs
346 * structure and the highest global priority value we use.
348 *clfuncspp
= &ts_classfuncs
;
349 return (ts_maxglobpri
);
354 * Interactive class scheduler initialization
358 ia_init(id_t cid
, int clparmsz
, classfuncs_t
**clfuncspp
)
361 * We're required to return a pointer to our classfuncs
362 * structure and the highest global priority value we use.
365 *clfuncspp
= &ia_classfuncs
;
366 return (ts_maxglobpri
);
371 * Get or reset the ts_dptbl values per the user's request.
374 ts_admin(caddr_t uaddr
, cred_t
*reqpcredp
)
382 if (get_udatamodel() == DATAMODEL_NATIVE
) {
383 if (copyin(uaddr
, &tsadmin
, sizeof (tsadmin_t
)))
386 #ifdef _SYSCALL32_IMPL
388 /* get tsadmin struct from ILP32 caller */
389 tsadmin32_t tsadmin32
;
390 if (copyin(uaddr
, &tsadmin32
, sizeof (tsadmin32_t
)))
393 (struct tsdpent
*)(uintptr_t)tsadmin32
.ts_dpents
;
394 tsadmin
.ts_ndpents
= tsadmin32
.ts_ndpents
;
395 tsadmin
.ts_cmd
= tsadmin32
.ts_cmd
;
397 #endif /* _SYSCALL32_IMPL */
399 tsdpsz
= (ts_maxumdpri
+ 1) * sizeof (tsdpent_t
);
401 switch (tsadmin
.ts_cmd
) {
403 tsadmin
.ts_ndpents
= ts_maxumdpri
+ 1;
405 if (get_udatamodel() == DATAMODEL_NATIVE
) {
406 if (copyout(&tsadmin
, uaddr
, sizeof (tsadmin_t
)))
409 #ifdef _SYSCALL32_IMPL
411 /* return tsadmin struct to ILP32 caller */
412 tsadmin32_t tsadmin32
;
413 tsadmin32
.ts_dpents
=
414 (caddr32_t
)(uintptr_t)tsadmin
.ts_dpents
;
415 tsadmin32
.ts_ndpents
= tsadmin
.ts_ndpents
;
416 tsadmin32
.ts_cmd
= tsadmin
.ts_cmd
;
417 if (copyout(&tsadmin32
, uaddr
, sizeof (tsadmin32_t
)))
420 #endif /* _SYSCALL32_IMPL */
424 userdpsz
= MIN(tsadmin
.ts_ndpents
* sizeof (tsdpent_t
),
426 if (copyout(ts_dptbl
, tsadmin
.ts_dpents
, userdpsz
))
429 tsadmin
.ts_ndpents
= userdpsz
/ sizeof (tsdpent_t
);
431 if (get_udatamodel() == DATAMODEL_NATIVE
) {
432 if (copyout(&tsadmin
, uaddr
, sizeof (tsadmin_t
)))
435 #ifdef _SYSCALL32_IMPL
437 /* return tsadmin struct to ILP32 callers */
438 tsadmin32_t tsadmin32
;
439 tsadmin32
.ts_dpents
=
440 (caddr32_t
)(uintptr_t)tsadmin
.ts_dpents
;
441 tsadmin32
.ts_ndpents
= tsadmin
.ts_ndpents
;
442 tsadmin32
.ts_cmd
= tsadmin
.ts_cmd
;
443 if (copyout(&tsadmin32
, uaddr
, sizeof (tsadmin32_t
)))
446 #endif /* _SYSCALL32_IMPL */
451 * We require that the requesting process has sufficient
452 * priveleges. We also require that the table supplied by
453 * the user exactly match the current ts_dptbl in size.
455 if (secpolicy_dispadm(reqpcredp
) != 0)
458 if (tsadmin
.ts_ndpents
* sizeof (tsdpent_t
) != tsdpsz
) {
463 * We read the user supplied table into a temporary buffer
464 * where it is validated before being copied over the
467 tmpdpp
= kmem_alloc(tsdpsz
, KM_SLEEP
);
468 if (copyin((caddr_t
)tsadmin
.ts_dpents
, (caddr_t
)tmpdpp
,
470 kmem_free(tmpdpp
, tsdpsz
);
473 for (i
= 0; i
< tsadmin
.ts_ndpents
; i
++) {
476 * Validate the user supplied values. All we are doing
477 * here is verifying that the values are within their
478 * allowable ranges and will not panic the system. We
479 * make no attempt to ensure that the resulting
480 * configuration makes sense or results in reasonable
483 if (tmpdpp
[i
].ts_quantum
<= 0) {
484 kmem_free(tmpdpp
, tsdpsz
);
487 if (tmpdpp
[i
].ts_tqexp
> ts_maxumdpri
||
488 tmpdpp
[i
].ts_tqexp
< 0) {
489 kmem_free(tmpdpp
, tsdpsz
);
492 if (tmpdpp
[i
].ts_slpret
> ts_maxumdpri
||
493 tmpdpp
[i
].ts_slpret
< 0) {
494 kmem_free(tmpdpp
, tsdpsz
);
497 if (tmpdpp
[i
].ts_maxwait
< 0) {
498 kmem_free(tmpdpp
, tsdpsz
);
501 if (tmpdpp
[i
].ts_lwait
> ts_maxumdpri
||
502 tmpdpp
[i
].ts_lwait
< 0) {
503 kmem_free(tmpdpp
, tsdpsz
);
509 * Copy the user supplied values over the current ts_dptbl
510 * values. The ts_globpri member is read-only so we don't
513 mutex_enter(&ts_dptblock
);
514 for (i
= 0; i
< tsadmin
.ts_ndpents
; i
++) {
515 ts_dptbl
[i
].ts_quantum
= tmpdpp
[i
].ts_quantum
;
516 ts_dptbl
[i
].ts_tqexp
= tmpdpp
[i
].ts_tqexp
;
517 ts_dptbl
[i
].ts_slpret
= tmpdpp
[i
].ts_slpret
;
518 ts_dptbl
[i
].ts_maxwait
= tmpdpp
[i
].ts_maxwait
;
519 ts_dptbl
[i
].ts_lwait
= tmpdpp
[i
].ts_lwait
;
521 mutex_exit(&ts_dptblock
);
522 kmem_free(tmpdpp
, tsdpsz
);
533 * Allocate a time-sharing class specific thread structure and
534 * initialize it with the parameters supplied. Also move the thread
535 * to specified time-sharing priority.
538 ts_enterclass(kthread_t
*t
, id_t cid
, void *parmsp
,
539 cred_t
*reqpcredp
, void *bufp
)
541 tsparms_t
*tsparmsp
= (tsparms_t
*)parmsp
;
545 static uint32_t tspexists
= 0; /* set on first occurrence of */
546 /* a time-sharing process */
548 tspp
= (tsproc_t
*)bufp
;
549 ASSERT(tspp
!= NULL
);
552 * Initialize the tsproc structure.
554 tspp
->ts_cpupri
= tsmedumdpri
;
557 * Check to make sure caller is either privileged or the
558 * window system. When the window system is converted
559 * to using privileges, the second check can go away.
561 if (reqpcredp
!= NULL
&& !groupmember(IA_gid
, reqpcredp
) &&
562 secpolicy_setpriority(reqpcredp
) != 0)
565 * Belongs to IA "class", so set appropriate flags.
566 * Mark as 'on' so it will not be a swap victim
569 tspp
->ts_flags
= TSIA
| TSIASET
;
570 tspp
->ts_boost
= ia_boost
;
576 if (tsparmsp
== NULL
) {
578 * Use default values.
580 tspp
->ts_uprilim
= tspp
->ts_upri
= 0;
581 tspp
->ts_nice
= NZERO
;
584 * Use supplied values.
586 if (tsparmsp
->ts_uprilim
== TS_NOCHANGE
)
589 if (tsparmsp
->ts_uprilim
> 0 &&
590 secpolicy_setpriority(reqpcredp
) != 0)
592 reqtsuprilim
= tsparmsp
->ts_uprilim
;
595 if (tsparmsp
->ts_upri
== TS_NOCHANGE
) {
596 reqtsupri
= reqtsuprilim
;
598 if (tsparmsp
->ts_upri
> 0 &&
599 secpolicy_setpriority(reqpcredp
) != 0)
602 * Set the user priority to the requested value
603 * or the upri limit, whichever is lower.
605 reqtsupri
= tsparmsp
->ts_upri
;
606 if (reqtsupri
> reqtsuprilim
)
607 reqtsupri
= reqtsuprilim
;
611 tspp
->ts_uprilim
= reqtsuprilim
;
612 tspp
->ts_upri
= reqtsupri
;
613 tspp
->ts_nice
= NZERO
- (NZERO
* reqtsupri
) / ts_maxupri
;
617 tspp
->ts_dispwait
= 0;
618 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
620 cpucaps_sc_init(&tspp
->ts_caps
);
623 * Reset priority. Process goes to a "user mode" priority
624 * here regardless of whether or not it has slept since
625 * entering the kernel.
627 thread_lock(t
); /* get dispatcher lock on thread */
628 t
->t_clfuncs
= &(sclass
[cid
].cl_funcs
->thread
);
630 t
->t_cldata
= (void *)tspp
;
631 t
->t_schedflag
&= ~TS_RUNQMATCH
;
632 ts_change_priority(t
, tspp
);
636 * Link new structure into tsproc list.
638 TS_LIST_INSERT(tspp
);
641 * If this is the first time-sharing thread to occur since
642 * boot we set up the initial call to ts_update() here.
643 * Use an atomic compare-and-swap since that's easier and
644 * faster than a mutex (but check with an ordinary load first
645 * since most of the time this will already be done).
647 if (tspexists
== 0 && atomic_cas_32(&tspexists
, 0, 1) == 0)
648 (void) timeout(ts_update
, NULL
, hz
);
655 * Free tsproc structure of thread.
658 ts_exitclass(void *procp
)
660 tsproc_t
*tspp
= (tsproc_t
*)procp
;
662 /* Remove tsproc_t structure from list */
663 TS_LIST_DELETE(tspp
);
664 kmem_free(tspp
, sizeof (tsproc_t
));
669 ts_canexit(kthread_t
*t
, cred_t
*cred
)
672 * A thread can always leave a TS/IA class
678 ts_fork(kthread_t
*t
, kthread_t
*ct
, void *bufp
)
680 tsproc_t
*ptspp
; /* ptr to parent's tsproc structure */
681 tsproc_t
*ctspp
; /* ptr to child's tsproc structure */
683 ASSERT(MUTEX_HELD(&ttoproc(t
)->p_lock
));
685 ctspp
= (tsproc_t
*)bufp
;
686 ASSERT(ctspp
!= NULL
);
687 ptspp
= (tsproc_t
*)t
->t_cldata
;
689 * Initialize child's tsproc structure.
692 ctspp
->ts_timeleft
= ts_dptbl
[ptspp
->ts_cpupri
].ts_quantum
;
693 ctspp
->ts_cpupri
= ptspp
->ts_cpupri
;
694 ctspp
->ts_boost
= ptspp
->ts_boost
;
695 ctspp
->ts_uprilim
= ptspp
->ts_uprilim
;
696 ctspp
->ts_upri
= ptspp
->ts_upri
;
698 ctspp
->ts_nice
= ptspp
->ts_nice
;
699 ctspp
->ts_dispwait
= 0;
700 ctspp
->ts_flags
= ptspp
->ts_flags
& ~(TSKPRI
| TSBACKQ
| TSRESTORE
);
702 cpucaps_sc_init(&ctspp
->ts_caps
);
706 * Link new structure into tsproc list.
708 ct
->t_cldata
= (void *)ctspp
;
709 TS_LIST_INSERT(ctspp
);
715 * Child is placed at back of dispatcher queue and parent gives
716 * up processor so that the child runs first after the fork.
717 * This allows the child immediately execing to break the multiple
718 * use of copy on write pages with no disk home. The parent will
719 * get to steal them back rather than uselessly copying them.
722 ts_forkret(kthread_t
*t
, kthread_t
*ct
)
724 proc_t
*pp
= ttoproc(t
);
725 proc_t
*cp
= ttoproc(ct
);
728 ASSERT(t
== curthread
);
729 ASSERT(MUTEX_HELD(&pidlock
));
732 * Grab the child's p_lock before dropping pidlock to ensure
733 * the process does not disappear before we set it running.
735 mutex_enter(&cp
->p_lock
);
737 mutex_exit(&cp
->p_lock
);
739 mutex_enter(&pp
->p_lock
);
740 mutex_exit(&pidlock
);
744 tspp
= (tsproc_t
*)(t
->t_cldata
);
745 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_tqexp
;
747 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
748 tspp
->ts_dispwait
= 0;
749 t
->t_pri
= ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
;
750 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
751 tspp
->ts_flags
&= ~TSKPRI
;
752 THREAD_TRANSITION(t
);
756 * Safe to drop p_lock now since since it is safe to change
757 * the scheduling class after this point.
759 mutex_exit(&pp
->p_lock
);
766 * Get information about the time-sharing class into the buffer
767 * pointed to by tsinfop. The maximum configured user priority
768 * is the only information we supply. ts_getclinfo() is called
769 * for TS threads, and ia_getclinfo() is called for IA threads.
772 ts_getclinfo(void *infop
)
774 tsinfo_t
*tsinfop
= (tsinfo_t
*)infop
;
775 tsinfop
->ts_maxupri
= ts_maxupri
;
780 ia_getclinfo(void *infop
)
782 iainfo_t
*iainfop
= (iainfo_t
*)infop
;
783 iainfop
->ia_maxupri
= ia_maxupri
;
789 * Return the user mode scheduling priority range.
792 ts_getclpri(pcpri_t
*pcprip
)
794 pcprip
->pc_clpmax
= ts_maxupri
;
795 pcprip
->pc_clpmin
= -ts_maxupri
;
801 ia_getclpri(pcpri_t
*pcprip
)
803 pcprip
->pc_clpmax
= ia_maxupri
;
804 pcprip
->pc_clpmin
= -ia_maxupri
;
815 * Get the time-sharing parameters of the thread pointed to by
816 * tsprocp into the buffer pointed to by tsparmsp. ts_parmsget()
817 * is called for TS threads, and ia_parmsget() is called for IA
821 ts_parmsget(kthread_t
*t
, void *parmsp
)
823 tsproc_t
*tspp
= (tsproc_t
*)t
->t_cldata
;
824 tsparms_t
*tsparmsp
= (tsparms_t
*)parmsp
;
826 tsparmsp
->ts_uprilim
= tspp
->ts_uprilim
;
827 tsparmsp
->ts_upri
= tspp
->ts_upri
;
831 ia_parmsget(kthread_t
*t
, void *parmsp
)
833 tsproc_t
*tspp
= (tsproc_t
*)t
->t_cldata
;
834 iaparms_t
*iaparmsp
= (iaparms_t
*)parmsp
;
836 iaparmsp
->ia_uprilim
= tspp
->ts_uprilim
;
837 iaparmsp
->ia_upri
= tspp
->ts_upri
;
838 if (tspp
->ts_flags
& TSIASET
)
839 iaparmsp
->ia_mode
= IA_SET_INTERACTIVE
;
841 iaparmsp
->ia_mode
= IA_INTERACTIVE_OFF
;
846 * Check the validity of the time-sharing parameters in the buffer
847 * pointed to by tsparmsp.
848 * ts_parmsin() is called for TS threads, and ia_parmsin() is called
852 ts_parmsin(void *parmsp
)
854 tsparms_t
*tsparmsp
= (tsparms_t
*)parmsp
;
856 * Check validity of parameters.
858 if ((tsparmsp
->ts_uprilim
> ts_maxupri
||
859 tsparmsp
->ts_uprilim
< -ts_maxupri
) &&
860 tsparmsp
->ts_uprilim
!= TS_NOCHANGE
)
863 if ((tsparmsp
->ts_upri
> ts_maxupri
||
864 tsparmsp
->ts_upri
< -ts_maxupri
) &&
865 tsparmsp
->ts_upri
!= TS_NOCHANGE
)
872 ia_parmsin(void *parmsp
)
874 iaparms_t
*iaparmsp
= (iaparms_t
*)parmsp
;
876 if ((iaparmsp
->ia_uprilim
> ia_maxupri
||
877 iaparmsp
->ia_uprilim
< -ia_maxupri
) &&
878 iaparmsp
->ia_uprilim
!= IA_NOCHANGE
) {
882 if ((iaparmsp
->ia_upri
> ia_maxupri
||
883 iaparmsp
->ia_upri
< -ia_maxupri
) &&
884 iaparmsp
->ia_upri
!= IA_NOCHANGE
) {
893 * Check the validity of the time-sharing parameters in the pc_vaparms_t
894 * structure vaparmsp and put them in the buffer pointed to by tsparmsp.
895 * pc_vaparms_t contains (key, value) pairs of parameter.
896 * ts_vaparmsin() is called for TS threads, and ia_vaparmsin() is called
897 * for IA threads. ts_vaparmsin() is the variable parameter version of
898 * ts_parmsin() and ia_vaparmsin() is the variable parameter version of
902 ts_vaparmsin(void *parmsp
, pc_vaparms_t
*vaparmsp
)
904 tsparms_t
*tsparmsp
= (tsparms_t
*)parmsp
;
908 pc_vaparm_t
*vpp
= &vaparmsp
->pc_parms
[0];
912 * TS_NOCHANGE (-32768) is outside of the range of values for
913 * ts_uprilim and ts_upri. If the structure tsparms_t is changed,
914 * TS_NOCHANGE should be replaced by a flag word (in the same manner
917 tsparmsp
->ts_uprilim
= TS_NOCHANGE
;
918 tsparmsp
->ts_upri
= TS_NOCHANGE
;
921 * Get the varargs parameter and check validity of parameters.
923 if (vaparmsp
->pc_vaparmscnt
> PC_VAPARMCNT
)
926 for (cnt
= 0; cnt
< vaparmsp
->pc_vaparmscnt
; cnt
++, vpp
++) {
928 switch (vpp
->pc_key
) {
932 tsparmsp
->ts_uprilim
= (pri_t
)vpp
->pc_parm
;
933 if (tsparmsp
->ts_uprilim
> ts_maxupri
||
934 tsparmsp
->ts_uprilim
< -ts_maxupri
)
941 tsparmsp
->ts_upri
= (pri_t
)vpp
->pc_parm
;
942 if (tsparmsp
->ts_upri
> ts_maxupri
||
943 tsparmsp
->ts_upri
< -ts_maxupri
)
952 if (vaparmsp
->pc_vaparmscnt
== 0) {
954 * Use default parameters.
956 tsparmsp
->ts_upri
= tsparmsp
->ts_uprilim
= 0;
963 ia_vaparmsin(void *parmsp
, pc_vaparms_t
*vaparmsp
)
965 iaparms_t
*iaparmsp
= (iaparms_t
*)parmsp
;
970 pc_vaparm_t
*vpp
= &vaparmsp
->pc_parms
[0];
973 * IA_NOCHANGE (-32768) is outside of the range of values for
974 * ia_uprilim, ia_upri and ia_mode. If the structure iaparms_t is
975 * changed, IA_NOCHANGE should be replaced by a flag word (in the
976 * same manner as in rt.c).
978 iaparmsp
->ia_uprilim
= IA_NOCHANGE
;
979 iaparmsp
->ia_upri
= IA_NOCHANGE
;
980 iaparmsp
->ia_mode
= IA_NOCHANGE
;
983 * Get the varargs parameter and check validity of parameters.
985 if (vaparmsp
->pc_vaparmscnt
> PC_VAPARMCNT
)
988 for (cnt
= 0; cnt
< vaparmsp
->pc_vaparmscnt
; cnt
++, vpp
++) {
990 switch (vpp
->pc_key
) {
994 iaparmsp
->ia_uprilim
= (pri_t
)vpp
->pc_parm
;
995 if (iaparmsp
->ia_uprilim
> ia_maxupri
||
996 iaparmsp
->ia_uprilim
< -ia_maxupri
)
1003 iaparmsp
->ia_upri
= (pri_t
)vpp
->pc_parm
;
1004 if (iaparmsp
->ia_upri
> ia_maxupri
||
1005 iaparmsp
->ia_upri
< -ia_maxupri
)
1012 iaparmsp
->ia_mode
= (int)vpp
->pc_parm
;
1013 if (iaparmsp
->ia_mode
!= IA_SET_INTERACTIVE
&&
1014 iaparmsp
->ia_mode
!= IA_INTERACTIVE_OFF
)
1023 if (vaparmsp
->pc_vaparmscnt
== 0) {
1025 * Use default parameters.
1027 iaparmsp
->ia_upri
= iaparmsp
->ia_uprilim
= 0;
1028 iaparmsp
->ia_mode
= IA_SET_INTERACTIVE
;
1035 * Nothing to do here but return success.
1039 ts_parmsout(void *parmsp
, pc_vaparms_t
*vaparmsp
)
1046 * Copy all selected time-sharing class parameters to the user.
1047 * The parameters are specified by a key.
1050 ts_vaparmsout(void *prmsp
, pc_vaparms_t
*vaparmsp
)
1052 tsparms_t
*tsprmsp
= (tsparms_t
*)prmsp
;
1056 pc_vaparm_t
*vpp
= &vaparmsp
->pc_parms
[0];
1058 ASSERT(MUTEX_NOT_HELD(&curproc
->p_lock
));
1060 if (vaparmsp
->pc_vaparmscnt
> PC_VAPARMCNT
)
1063 for (cnt
= 0; cnt
< vaparmsp
->pc_vaparmscnt
; cnt
++, vpp
++) {
1065 switch (vpp
->pc_key
) {
1069 if (copyout(&tsprmsp
->ts_uprilim
,
1070 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (pri_t
)))
1077 if (copyout(&tsprmsp
->ts_upri
,
1078 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (pri_t
)))
1092 * Copy all selected interactive class parameters to the user.
1093 * The parameters are specified by a key.
1096 ia_vaparmsout(void *prmsp
, pc_vaparms_t
*vaparmsp
)
1098 iaparms_t
*iaprmsp
= (iaparms_t
*)prmsp
;
1103 pc_vaparm_t
*vpp
= &vaparmsp
->pc_parms
[0];
1105 ASSERT(MUTEX_NOT_HELD(&curproc
->p_lock
));
1107 if (vaparmsp
->pc_vaparmscnt
> PC_VAPARMCNT
)
1110 for (cnt
= 0; cnt
< vaparmsp
->pc_vaparmscnt
; cnt
++, vpp
++) {
1112 switch (vpp
->pc_key
) {
1116 if (copyout(&iaprmsp
->ia_uprilim
,
1117 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (pri_t
)))
1124 if (copyout(&iaprmsp
->ia_upri
,
1125 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (pri_t
)))
1132 if (copyout(&iaprmsp
->ia_mode
,
1133 (caddr_t
)(uintptr_t)vpp
->pc_parm
, sizeof (int)))
1146 * Set the scheduling parameters of the thread pointed to by tsprocp
1147 * to those specified in the buffer pointed to by tsparmsp.
1148 * ts_parmsset() is called for TS threads, and ia_parmsset() is
1149 * called for IA threads.
1153 ts_parmsset(kthread_t
*tx
, void *parmsp
, id_t reqpcid
, cred_t
*reqpcredp
)
1158 tsparms_t
*tsparmsp
= (tsparms_t
*)parmsp
;
1159 tsproc_t
*tspp
= (tsproc_t
*)tx
->t_cldata
;
1161 ASSERT(MUTEX_HELD(&(ttoproc(tx
))->p_lock
));
1163 if (tsparmsp
->ts_uprilim
== TS_NOCHANGE
)
1164 reqtsuprilim
= tspp
->ts_uprilim
;
1166 reqtsuprilim
= tsparmsp
->ts_uprilim
;
1168 if (tsparmsp
->ts_upri
== TS_NOCHANGE
)
1169 reqtsupri
= tspp
->ts_upri
;
1171 reqtsupri
= tsparmsp
->ts_upri
;
1174 * Make sure the user priority doesn't exceed the upri limit.
1176 if (reqtsupri
> reqtsuprilim
)
1177 reqtsupri
= reqtsuprilim
;
1180 * Basic permissions enforced by generic kernel code
1181 * for all classes require that a thread attempting
1182 * to change the scheduling parameters of a target
1183 * thread be privileged or have a real or effective
1184 * UID matching that of the target thread. We are not
1185 * called unless these basic permission checks have
1186 * already passed. The time-sharing class requires in
1187 * addition that the calling thread be privileged if it
1188 * is attempting to raise the upri limit above its current
1189 * value This may have been checked previously but if our
1190 * caller passed us a non-NULL credential pointer we assume
1191 * it hasn't and we check it here.
1193 if (reqpcredp
!= NULL
&&
1194 reqtsuprilim
> tspp
->ts_uprilim
&&
1195 secpolicy_raisepriority(reqpcredp
) != 0)
1199 * Set ts_nice to the nice value corresponding to the user
1200 * priority we are setting. Note that setting the nice field
1201 * of the parameter struct won't affect upri or nice.
1203 nice
= NZERO
- (reqtsupri
* NZERO
) / ts_maxupri
;
1204 if (nice
>= 2 * NZERO
)
1205 nice
= 2 * NZERO
- 1;
1209 tspp
->ts_uprilim
= reqtsuprilim
;
1210 tspp
->ts_upri
= reqtsupri
;
1212 tspp
->ts_nice
= nice
;
1214 if ((tspp
->ts_flags
& TSKPRI
) != 0) {
1219 tspp
->ts_dispwait
= 0;
1220 ts_change_priority(tx
, tspp
);
1227 ia_parmsset(kthread_t
*tx
, void *parmsp
, id_t reqpcid
, cred_t
*reqpcredp
)
1229 tsproc_t
*tspp
= (tsproc_t
*)tx
->t_cldata
;
1230 iaparms_t
*iaparmsp
= (iaparms_t
*)parmsp
;
1232 pid_t pid
, pgid
, sid
;
1238 * Handle user priority changes
1240 if (iaparmsp
->ia_mode
== IA_NOCHANGE
)
1241 return (ts_parmsset(tx
, parmsp
, reqpcid
, reqpcredp
));
1244 * Check permissions for changing modes.
1247 if (reqpcredp
!= NULL
&& !groupmember(IA_gid
, reqpcredp
) &&
1248 secpolicy_raisepriority(reqpcredp
) != 0) {
1250 * Silently fail in case this is just a priocntl
1251 * call with upri and uprilim set to IA_NOCHANGE.
1256 ASSERT(MUTEX_HELD(&pidlock
));
1257 if ((p
= ttoproc(tx
)) == NULL
) {
1260 ASSERT(MUTEX_HELD(&p
->p_lock
));
1261 if (p
->p_stat
== SIDL
) {
1265 sid
= p
->p_sessp
->s_sid
;
1267 if (iaparmsp
->ia_mode
== IA_SET_INTERACTIVE
) {
1269 * session leaders must be turned on now so all processes
1270 * in the group controlling the tty will be turned on or off.
1271 * if the ia_mode is off for the session leader,
1272 * ia_set_process_group will return without setting the
1273 * processes in the group controlling the tty on.
1276 tspp
->ts_flags
|= TSIASET
;
1279 mutex_enter(&p
->p_sessp
->s_lock
);
1281 if ((pid
== sid
) && (p
->p_sessp
->s_vp
!= NULL
) &&
1282 ((stp
= p
->p_sessp
->s_vp
->v_stream
) != NULL
)) {
1283 if ((stp
->sd_pgidp
!= NULL
) && (stp
->sd_sidp
!= NULL
)) {
1284 pgid
= stp
->sd_pgidp
->pid_id
;
1286 mutex_exit(&p
->p_sessp
->s_lock
);
1287 if (iaparmsp
->ia_mode
==
1288 IA_SET_INTERACTIVE
) {
1295 TRACE_3(TR_FAC_IA
, TR_ACTIVE_CHAIN
,
1296 "active chain:pid %d gid %d %p",
1298 ia_set_process_group(sid
, off
, on
);
1302 mutex_exit(&p
->p_sessp
->s_lock
);
1306 if (iaparmsp
->ia_mode
== IA_SET_INTERACTIVE
) {
1307 tspp
->ts_flags
|= TSIASET
;
1308 tspp
->ts_boost
= ia_boost
;
1310 tspp
->ts_flags
&= ~TSIASET
;
1311 tspp
->ts_boost
= -ia_boost
;
1315 return (ts_parmsset(tx
, parmsp
, reqpcid
, reqpcredp
));
1319 ts_exit(kthread_t
*t
)
1325 * A thread could be exiting in between clock ticks,
1326 * so we need to calculate how much CPU time it used
1327 * since it was charged last time.
1329 * CPU caps are not enforced on exiting processes - it is
1330 * usually desirable to exit as soon as possible to free
1334 tspp
= (tsproc_t
*)t
->t_cldata
;
1335 (void) cpucaps_charge(t
, &tspp
->ts_caps
, CPUCAPS_CHARGE_ONLY
);
1341 * Return the global scheduling priority that would be assigned
1342 * to a thread entering the time-sharing class with the ts_upri.
1345 ts_globpri(kthread_t
*t
)
1350 ASSERT(MUTEX_HELD(&ttoproc(t
)->p_lock
));
1351 tspp
= (tsproc_t
*)t
->t_cldata
;
1352 tspri
= tsmedumdpri
+ tspp
->ts_upri
;
1353 if (tspri
> ts_maxumdpri
)
1354 tspri
= ts_maxumdpri
;
1357 return (ts_dptbl
[tspri
].ts_globpri
);
1361 * Arrange for thread to be placed in appropriate location
1362 * on dispatcher queue.
1364 * This is called with the current thread in TS_ONPROC and locked.
1367 ts_preempt(kthread_t
*t
)
1369 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1370 klwp_t
*lwp
= curthread
->t_lwp
;
1371 pri_t oldpri
= t
->t_pri
;
1373 ASSERT(t
== curthread
);
1374 ASSERT(THREAD_LOCK_HELD(curthread
));
1377 * If preempted in the kernel, make sure the thread has
1378 * a kernel priority if needed.
1380 if (!(tspp
->ts_flags
& TSKPRI
) && lwp
!= NULL
&& t
->t_kpri_req
) {
1381 tspp
->ts_flags
|= TSKPRI
;
1382 THREAD_CHANGE_PRI(t
, ts_kmdpris
[0]);
1383 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1384 t
->t_trapret
= 1; /* so ts_trapret will run */
1389 * This thread may be placed on wait queue by CPU Caps. In this case we
1390 * do not need to do anything until it is removed from the wait queue.
1391 * Do not enforce CPU caps on threads running at a kernel priority
1394 (void) cpucaps_charge(t
, &tspp
->ts_caps
,
1395 CPUCAPS_CHARGE_ENFORCE
);
1396 if (!(tspp
->ts_flags
& TSKPRI
) && CPUCAPS_ENFORCE(t
))
1401 * Check to see if we're doing "preemption control" here. If
1402 * we are, and if the user has requested that this thread not
1403 * be preempted, and if preemptions haven't been put off for
1404 * too long, let the preemption happen here but try to make
1405 * sure the thread is rescheduled as soon as possible. We do
1406 * this by putting it on the front of the highest priority run
1407 * queue in the TS class. If the preemption has been put off
1408 * for too long, clear the "nopreempt" bit and let the thread
1411 if (t
->t_schedctl
&& schedctl_get_nopreempt(t
)) {
1412 if (tspp
->ts_timeleft
> -SC_MAX_TICKS
) {
1413 DTRACE_SCHED1(schedctl__nopreempt
, kthread_t
*, t
);
1414 if (!(tspp
->ts_flags
& TSKPRI
)) {
1416 * If not already remembered, remember current
1417 * priority for restoration in ts_yield().
1419 if (!(tspp
->ts_flags
& TSRESTORE
)) {
1420 tspp
->ts_scpri
= t
->t_pri
;
1421 tspp
->ts_flags
|= TSRESTORE
;
1423 THREAD_CHANGE_PRI(t
, ts_maxumdpri
);
1425 schedctl_set_yield(t
, 1);
1429 if (tspp
->ts_flags
& TSRESTORE
) {
1430 THREAD_CHANGE_PRI(t
, tspp
->ts_scpri
);
1431 tspp
->ts_flags
&= ~TSRESTORE
;
1433 schedctl_set_nopreempt(t
, 0);
1434 DTRACE_SCHED1(schedctl__preempt
, kthread_t
*, t
);
1436 * Fall through and be preempted below.
1441 if ((tspp
->ts_flags
& (TSBACKQ
|TSKPRI
)) == TSBACKQ
) {
1442 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1443 tspp
->ts_dispwait
= 0;
1444 tspp
->ts_flags
&= ~TSBACKQ
;
1446 } else if ((tspp
->ts_flags
& (TSBACKQ
|TSKPRI
)) == (TSBACKQ
|TSKPRI
)) {
1447 tspp
->ts_flags
&= ~TSBACKQ
;
1454 TRACE_2(TR_FAC_DISP
, TR_PREEMPT
,
1455 "preempt:tid %p old pri %d", t
, oldpri
);
1459 ts_setrun(kthread_t
*t
)
1461 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1463 ASSERT(THREAD_LOCK_HELD(t
)); /* t should be in transition */
1465 if (tspp
->ts_dispwait
> ts_dptbl
[tspp
->ts_umdpri
].ts_maxwait
) {
1466 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_slpret
;
1468 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1469 tspp
->ts_dispwait
= 0;
1470 if ((tspp
->ts_flags
& TSKPRI
) == 0) {
1471 THREAD_CHANGE_PRI(t
,
1472 ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1473 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1477 tspp
->ts_flags
&= ~TSBACKQ
;
1479 if (tspp
->ts_flags
& TSIA
) {
1480 if (tspp
->ts_flags
& TSIASET
)
1485 if (t
->t_disp_time
!= ddi_get_lbolt())
1494 * Prepare thread for sleep. We reset the thread priority so it will
1495 * run at the kernel priority level when it wakes up.
1498 ts_sleep(kthread_t
*t
)
1500 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1502 pri_t old_pri
= t
->t_pri
;
1504 ASSERT(t
== curthread
);
1505 ASSERT(THREAD_LOCK_HELD(t
));
1508 * Account for time spent on CPU before going to sleep.
1510 (void) CPUCAPS_CHARGE(t
, &tspp
->ts_caps
, CPUCAPS_CHARGE_ENFORCE
);
1512 flags
= tspp
->ts_flags
;
1513 if (t
->t_kpri_req
) {
1514 tspp
->ts_flags
= flags
| TSKPRI
;
1515 THREAD_CHANGE_PRI(t
, ts_kmdpris
[0]);
1516 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1517 t
->t_trapret
= 1; /* so ts_trapret will run */
1519 } else if (tspp
->ts_dispwait
> ts_dptbl
[tspp
->ts_umdpri
].ts_maxwait
) {
1521 * If thread has blocked in the kernel (as opposed to
1522 * being merely preempted), recompute the user mode priority.
1524 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_slpret
;
1526 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1527 tspp
->ts_dispwait
= 0;
1529 THREAD_CHANGE_PRI(curthread
,
1530 ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1531 ASSERT(curthread
->t_pri
>= 0 &&
1532 curthread
->t_pri
<= ts_maxglobpri
);
1533 tspp
->ts_flags
= flags
& ~TSKPRI
;
1535 if (DISP_MUST_SURRENDER(curthread
))
1536 cpu_surrender(curthread
);
1537 } else if (flags
& TSKPRI
) {
1538 THREAD_CHANGE_PRI(curthread
,
1539 ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1540 ASSERT(curthread
->t_pri
>= 0 &&
1541 curthread
->t_pri
<= ts_maxglobpri
);
1542 tspp
->ts_flags
= flags
& ~TSKPRI
;
1544 if (DISP_MUST_SURRENDER(curthread
))
1545 cpu_surrender(curthread
);
1547 TRACE_2(TR_FAC_DISP
, TR_SLEEP
,
1548 "sleep:tid %p old pri %d", t
, old_pri
);
1552 * Check for time slice expiration. If time slice has expired
1553 * move thread to priority specified in tsdptbl for time slice expiration
1554 * and set runrun to cause preemption.
1557 ts_tick(kthread_t
*t
)
1559 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1560 bool call_cpu_surrender
= false;
1561 pri_t oldpri
= t
->t_pri
;
1563 ASSERT(MUTEX_HELD(&(ttoproc(t
))->p_lock
));
1568 * Keep track of thread's project CPU usage. Note that projects
1569 * get charged even when threads are running in the kernel.
1572 call_cpu_surrender
= cpucaps_charge(t
, &tspp
->ts_caps
,
1573 CPUCAPS_CHARGE_ENFORCE
) && !(tspp
->ts_flags
& TSKPRI
);
1576 if ((tspp
->ts_flags
& TSKPRI
) == 0) {
1577 if (--tspp
->ts_timeleft
<= 0) {
1581 * If we're doing preemption control and trying to
1582 * avoid preempting this thread, just note that
1583 * the thread should yield soon and let it keep
1584 * running (unless it's been a while).
1586 if (t
->t_schedctl
&& schedctl_get_nopreempt(t
)) {
1587 if (tspp
->ts_timeleft
> -SC_MAX_TICKS
) {
1588 DTRACE_SCHED1(schedctl__nopreempt
,
1590 schedctl_set_yield(t
, 1);
1591 thread_unlock_nopreempt(t
);
1595 tspp
->ts_flags
&= ~TSRESTORE
;
1596 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_tqexp
;
1598 tspp
->ts_dispwait
= 0;
1599 new_pri
= ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
;
1600 ASSERT(new_pri
>= 0 && new_pri
<= ts_maxglobpri
);
1602 * When the priority of a thread is changed,
1603 * it may be necessary to adjust its position
1604 * on a sleep queue or dispatch queue.
1605 * The function thread_change_pri accomplishes
1608 if (thread_change_pri(t
, new_pri
, 0)) {
1610 ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1612 call_cpu_surrender
= true;
1614 TRACE_2(TR_FAC_DISP
, TR_TICK
,
1615 "tick:tid %p old pri %d", t
, oldpri
);
1616 } else if (t
->t_state
== TS_ONPROC
&&
1617 t
->t_pri
< t
->t_disp_queue
->disp_maxrunpri
) {
1618 call_cpu_surrender
= true;
1622 if (call_cpu_surrender
) {
1623 tspp
->ts_flags
|= TSBACKQ
;
1627 thread_unlock_nopreempt(t
); /* clock thread can't be preempted */
1632 * If thread is currently at a kernel mode priority (has slept)
1633 * we assign it the appropriate user mode priority and time quantum
1634 * here. If we are lowering the thread's priority below that of
1635 * other runnable threads we will normally set runrun via cpu_surrender() to
1639 ts_trapret(kthread_t
*t
)
1641 tsproc_t
*tspp
= (tsproc_t
*)t
->t_cldata
;
1643 pri_t old_pri
= curthread
->t_pri
;
1645 ASSERT(THREAD_LOCK_HELD(t
));
1646 ASSERT(t
== curthread
);
1647 ASSERT(cp
->cpu_dispthread
== t
);
1648 ASSERT(t
->t_state
== TS_ONPROC
);
1651 if (tspp
->ts_dispwait
> ts_dptbl
[tspp
->ts_umdpri
].ts_maxwait
) {
1652 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_slpret
;
1654 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1655 tspp
->ts_dispwait
= 0;
1658 * If thread has blocked in the kernel (as opposed to
1659 * being merely preempted), recompute the user mode priority.
1661 THREAD_CHANGE_PRI(t
, ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1662 cp
->cpu_dispatch_pri
= DISP_PRIO(t
);
1663 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1664 tspp
->ts_flags
&= ~TSKPRI
;
1666 if (DISP_MUST_SURRENDER(t
))
1668 } else if (tspp
->ts_flags
& TSKPRI
) {
1670 * If thread has blocked in the kernel (as opposed to
1671 * being merely preempted), recompute the user mode priority.
1673 THREAD_CHANGE_PRI(t
, ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1674 cp
->cpu_dispatch_pri
= DISP_PRIO(t
);
1675 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1676 tspp
->ts_flags
&= ~TSKPRI
;
1678 if (DISP_MUST_SURRENDER(t
))
1682 TRACE_2(TR_FAC_DISP
, TR_TRAPRET
,
1683 "trapret:tid %p old pri %d", t
, old_pri
);
1688 * Update the ts_dispwait values of all time sharing threads that
1689 * are currently runnable at a user mode priority and bump the priority
1690 * if ts_dispwait exceeds ts_maxwait. Called once per second via
1691 * timeout which we reset here.
1693 * There are several lists of time sharing threads broken up by a hash on
1694 * the thread pointer. Each list has its own lock. This avoids blocking
1695 * all ts_enterclass, ts_fork, and ts_exitclass operations while ts_update
1696 * runs. ts_update traverses each list in turn.
1698 * If multiple threads have their priorities updated to the same value,
1699 * the system implicitly favors the one that is updated first (since it
1700 * winds up first on the run queue). To avoid this unfairness, the
1701 * traversal of threads starts at the list indicated by a marker. When
1702 * threads in more than one list have their priorities updated, the marker
1703 * is moved. This changes the order the threads will be placed on the run
1704 * queue the next time ts_update is called and preserves fairness over the
1705 * long run. The marker doesn't need to be protected by a lock since it's
1706 * only accessed by ts_update, which is inherently single-threaded (only
1707 * one instance can be running at a time).
1710 ts_update(void *arg
)
1713 int new_marker
= -1;
1714 static int ts_update_marker
;
1717 * Start with the ts_update_marker list, then do the rest.
1719 i
= ts_update_marker
;
1722 * If this is the first list after the current marker to
1723 * have threads with priorities updated, advance the marker
1724 * to this list for the next time ts_update runs.
1726 if (ts_update_list(i
) && new_marker
== -1 &&
1727 i
!= ts_update_marker
) {
1730 } while ((i
= TS_LIST_NEXT(i
)) != ts_update_marker
);
1732 /* advance marker for next ts_update call */
1733 if (new_marker
!= -1)
1734 ts_update_marker
= new_marker
;
1736 (void) timeout(ts_update
, arg
, hz
);
1740 * Updates priority for a list of threads. Returns 1 if the priority of
1741 * one of the threads was actually updated, 0 if none were for various
1742 * reasons (thread is no longer in the TS or IA class, isn't runnable,
1743 * hasn't waited long enough, has the preemption control no-preempt bit
1747 ts_update_list(int i
)
1753 mutex_enter(&ts_list_lock
[i
]);
1754 for (tspp
= ts_plisthead
[i
].ts_next
; tspp
!= &ts_plisthead
[i
];
1755 tspp
= tspp
->ts_next
) {
1758 * Lock the thread and verify state.
1762 * Skip the thread if it is no longer in the TS (or IA) class.
1764 if (tx
->t_clfuncs
!= &ts_classfuncs
.thread
&&
1765 tx
->t_clfuncs
!= &ia_classfuncs
.thread
)
1767 tspp
->ts_dispwait
++;
1768 if ((tspp
->ts_flags
& TSKPRI
) != 0)
1770 if (tspp
->ts_dispwait
<= ts_dptbl
[tspp
->ts_umdpri
].ts_maxwait
)
1772 if (tx
->t_schedctl
&& schedctl_get_nopreempt(tx
))
1774 if (tx
->t_state
!= TS_RUN
&& tx
->t_state
!= TS_WAIT
&&
1775 (tx
->t_state
!= TS_SLEEP
|| !ts_sleep_promote
)) {
1776 /* make next syscall/trap do CL_TRAPRET */
1781 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_lwait
;
1783 tspp
->ts_dispwait
= 0;
1787 * Only dequeue it if needs to move; otherwise it should
1788 * just round-robin here.
1790 if (tx
->t_pri
!= ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
) {
1791 pri_t oldpri
= tx
->t_pri
;
1792 ts_change_priority(tx
, tspp
);
1793 TRACE_2(TR_FAC_DISP
, TR_UPDATE
,
1794 "update:tid %p old pri %d", tx
, oldpri
);
1799 mutex_exit(&ts_list_lock
[i
]);
1805 * Processes waking up go to the back of their queue. We don't
1806 * need to assign a time quantum here because thread is still
1807 * at a kernel mode priority and the time slicing is not done
1808 * for threads running in the kernel after sleeping. The proper
1809 * time quantum will be assigned by ts_trapret before the thread
1810 * returns to user mode.
1813 ts_wakeup(kthread_t
*t
)
1815 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1817 ASSERT(THREAD_LOCK_HELD(t
));
1819 if (tspp
->ts_flags
& TSKPRI
) {
1820 tspp
->ts_flags
&= ~TSBACKQ
;
1821 if (tspp
->ts_flags
& TSIASET
)
1825 } else if (t
->t_kpri_req
) {
1827 * Give thread a priority boost if we were asked.
1829 tspp
->ts_flags
|= TSKPRI
;
1830 THREAD_CHANGE_PRI(t
, ts_kmdpris
[0]);
1832 t
->t_trapret
= 1; /* so that ts_trapret will run */
1835 if (tspp
->ts_dispwait
> ts_dptbl
[tspp
->ts_umdpri
].ts_maxwait
) {
1836 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_slpret
;
1839 ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1840 tspp
->ts_dispwait
= 0;
1841 THREAD_CHANGE_PRI(t
,
1842 ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1843 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1846 tspp
->ts_flags
&= ~TSBACKQ
;
1848 if (tspp
->ts_flags
& TSIA
) {
1849 if (tspp
->ts_flags
& TSIASET
)
1854 if (t
->t_disp_time
!= ddi_get_lbolt())
1864 * When a thread yields, put it on the back of the run queue.
1867 ts_yield(kthread_t
*t
)
1869 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1871 ASSERT(t
== curthread
);
1872 ASSERT(THREAD_LOCK_HELD(t
));
1875 * Collect CPU usage spent before yielding
1877 (void) CPUCAPS_CHARGE(t
, &tspp
->ts_caps
, CPUCAPS_CHARGE_ENFORCE
);
1880 * Clear the preemption control "yield" bit since the user is
1884 schedctl_set_yield(t
, 0);
1886 * If ts_preempt() artifically increased the thread's priority
1887 * to avoid preemption, restore the original priority now.
1889 if (tspp
->ts_flags
& TSRESTORE
) {
1890 THREAD_CHANGE_PRI(t
, tspp
->ts_scpri
);
1891 tspp
->ts_flags
&= ~TSRESTORE
;
1893 if (tspp
->ts_timeleft
<= 0) {
1895 * Time slice was artificially extended to avoid
1896 * preemption, so pretend we're preempting it now.
1898 DTRACE_SCHED1(schedctl__yield
, int, -tspp
->ts_timeleft
);
1899 tspp
->ts_cpupri
= ts_dptbl
[tspp
->ts_cpupri
].ts_tqexp
;
1901 tspp
->ts_timeleft
= ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
1902 tspp
->ts_dispwait
= 0;
1903 THREAD_CHANGE_PRI(t
, ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
);
1904 ASSERT(t
->t_pri
>= 0 && t
->t_pri
<= ts_maxglobpri
);
1906 tspp
->ts_flags
&= ~TSBACKQ
;
1912 * Increment the nice value of the specified thread by incr and
1913 * return the new value in *retvalp.
1916 ts_donice(kthread_t
*t
, cred_t
*cr
, int incr
, int *retvalp
)
1919 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1922 ASSERT(MUTEX_HELD(&(ttoproc(t
))->p_lock
));
1924 /* If there's no change to priority, just return current setting */
1927 *retvalp
= tspp
->ts_nice
- NZERO
;
1932 if ((incr
< 0 || incr
> 2 * NZERO
) &&
1933 secpolicy_raisepriority(cr
) != 0)
1937 * Specifying a nice increment greater than the upper limit of
1938 * 2 * NZERO - 1 will result in the thread's nice value being
1939 * set to the upper limit. We check for this before computing
1940 * the new value because otherwise we could get overflow
1941 * if a privileged process specified some ridiculous increment.
1943 if (incr
> 2 * NZERO
- 1)
1944 incr
= 2 * NZERO
- 1;
1946 newnice
= tspp
->ts_nice
+ incr
;
1947 if (newnice
>= 2 * NZERO
)
1948 newnice
= 2 * NZERO
- 1;
1949 else if (newnice
< 0)
1952 tsparms
.ts_uprilim
= tsparms
.ts_upri
=
1953 -((newnice
- NZERO
) * ts_maxupri
) / NZERO
;
1955 * Reset the uprilim and upri values of the thread.
1956 * Call ts_parmsset even if thread is interactive since we're
1957 * not changing mode.
1959 (void) ts_parmsset(t
, (void *)&tsparms
, (id_t
)0, (cred_t
*)NULL
);
1962 * Although ts_parmsset already reset ts_nice it may
1963 * not have been set to precisely the value calculated above
1964 * because ts_parmsset determines the nice value from the
1965 * user priority and we may have truncated during the integer
1966 * conversion from nice value to user priority and back.
1967 * We reset ts_nice to the value we calculated above.
1969 tspp
->ts_nice
= (char)newnice
;
1972 *retvalp
= newnice
- NZERO
;
1977 * Increment the priority of the specified thread by incr and
1978 * return the new value in *retvalp.
1981 ts_doprio(kthread_t
*t
, cred_t
*cr
, int incr
, int *retvalp
)
1984 tsproc_t
*tspp
= (tsproc_t
*)(t
->t_cldata
);
1987 ASSERT(MUTEX_HELD(&(ttoproc(t
))->p_lock
));
1989 /* If there's no change to the priority, just return current setting */
1991 *retvalp
= tspp
->ts_upri
;
1995 newpri
= tspp
->ts_upri
+ incr
;
1996 if (newpri
> ts_maxupri
|| newpri
< -ts_maxupri
)
2000 tsparms
.ts_uprilim
= tsparms
.ts_upri
= newpri
;
2002 * Reset the uprilim and upri values of the thread.
2003 * Call ts_parmsset even if thread is interactive since we're
2004 * not changing mode.
2006 return (ts_parmsset(t
, &tsparms
, 0, cr
));
2010 * ia_set_process_group marks foreground processes as interactive
2011 * and background processes as non-interactive iff the session
2012 * leader is interactive. This routine is called from two places:
2013 * strioctl:SPGRP when a new process group gets
2014 * control of the tty.
2015 * ia_parmsset-when the process in question is a session leader.
2016 * ia_set_process_group assumes that pidlock is held by the caller,
2017 * either strioctl or priocntlsys. If the caller is priocntlsys
2018 * (via ia_parmsset) then the p_lock of the session leader is held
2019 * and the code needs to be careful about acquiring other p_locks.
2022 ia_set_process_group(pid_t sid
, pid_t bg_pgid
, pid_t fg_pgid
)
2024 proc_t
*leader
, *fg
, *bg
;
2029 ASSERT(MUTEX_HELD(&pidlock
));
2032 * see if the session leader is interactive AND
2033 * if it is currently "on" AND controlling a tty
2034 * iff it is then make the processes in the foreground
2035 * group interactive and the processes in the background
2036 * group non-interactive.
2038 if ((leader
= (proc_t
*)prfind(sid
)) == NULL
) {
2041 if (leader
->p_stat
== SIDL
) {
2044 if ((tx
= proctot(leader
)) == NULL
) {
2048 * XXX do all the threads in the leader
2050 if (tx
->t_cid
!= ia_cid
) {
2053 tspp
= tx
->t_cldata
;
2055 * session leaders that are not interactive need not have
2056 * any processing done for them. They are typically shells
2057 * that do not have focus and are changing the process group
2058 * attatched to the tty, e.g. a process that is exiting
2060 mutex_enter(&leader
->p_sessp
->s_lock
);
2061 if (!(tspp
->ts_flags
& TSIASET
) ||
2062 (leader
->p_sessp
->s_vp
== NULL
) ||
2063 (leader
->p_sessp
->s_vp
->v_stream
== NULL
)) {
2064 mutex_exit(&leader
->p_sessp
->s_lock
);
2067 mutex_exit(&leader
->p_sessp
->s_lock
);
2070 * If we're already holding the leader's p_lock, we should use
2071 * mutex_tryenter instead of mutex_enter to avoid deadlocks from
2072 * lock ordering violations.
2074 if (mutex_owned(&leader
->p_lock
))
2080 * now look for all processes in the foreground group and
2081 * make them interactive
2083 for (fg
= (proc_t
*)pgfind(fg_pgid
); fg
!= NULL
; fg
= fg
->p_pglink
) {
2085 * if the process is SIDL it's begin forked, ignore it
2087 if (fg
->p_stat
== SIDL
) {
2091 * sesssion leaders must be turned on/off explicitly
2092 * not implicitly as happens to other members of
2093 * the process group.
2095 if (fg
->p_pid
== fg
->p_sessp
->s_sid
) {
2099 TRACE_1(TR_FAC_IA
, TR_GROUP_ON
,
2100 "group on:proc %p", fg
);
2103 if (mutex_tryenter(&fg
->p_lock
) == 0)
2106 mutex_enter(&fg
->p_lock
);
2109 if ((tx
= proctot(fg
)) == NULL
) {
2110 mutex_exit(&fg
->p_lock
);
2116 * if this thread is not interactive continue
2118 if (tx
->t_cid
!= ia_cid
) {
2122 tspp
= tx
->t_cldata
;
2123 tspp
->ts_flags
|= TSIASET
;
2124 tspp
->ts_boost
= ia_boost
;
2126 if ((tspp
->ts_flags
& TSKPRI
) != 0) {
2130 tspp
->ts_dispwait
= 0;
2131 ts_change_priority(tx
, tspp
);
2133 } while ((tx
= tx
->t_forw
) != fg
->p_tlist
);
2134 mutex_exit(&fg
->p_lock
);
2139 for (bg
= (proc_t
*)pgfind(bg_pgid
); bg
!= NULL
; bg
= bg
->p_pglink
) {
2140 if (bg
->p_stat
== SIDL
) {
2144 * sesssion leaders must be turned off explicitly
2145 * not implicitly as happens to other members of
2146 * the process group.
2148 if (bg
->p_pid
== bg
->p_sessp
->s_sid
) {
2152 TRACE_1(TR_FAC_IA
, TR_GROUP_OFF
,
2153 "group off:proc %p", bg
);
2156 if (mutex_tryenter(&bg
->p_lock
) == 0)
2159 mutex_enter(&bg
->p_lock
);
2162 if ((tx
= proctot(bg
)) == NULL
) {
2163 mutex_exit(&bg
->p_lock
);
2169 * if this thread is not interactive continue
2171 if (tx
->t_cid
!= ia_cid
) {
2175 tspp
= tx
->t_cldata
;
2176 tspp
->ts_flags
&= ~TSIASET
;
2177 tspp
->ts_boost
= -ia_boost
;
2179 if ((tspp
->ts_flags
& TSKPRI
) != 0) {
2184 tspp
->ts_dispwait
= 0;
2185 ts_change_priority(tx
, tspp
);
2187 } while ((tx
= tx
->t_forw
) != bg
->p_tlist
);
2188 mutex_exit(&bg
->p_lock
);
2194 ts_change_priority(kthread_t
*t
, tsproc_t
*tspp
)
2198 ASSERT(THREAD_LOCK_HELD(t
));
2199 new_pri
= ts_dptbl
[tspp
->ts_umdpri
].ts_globpri
;
2200 ASSERT(new_pri
>= 0 && new_pri
<= ts_maxglobpri
);
2201 tspp
->ts_flags
&= ~TSRESTORE
;
2202 t
->t_cpri
= tspp
->ts_upri
;
2203 if (t
== curthread
|| t
->t_state
== TS_ONPROC
) {
2204 /* curthread is always onproc */
2205 cpu_t
*cp
= t
->t_disp_queue
->disp_cpu
;
2206 THREAD_CHANGE_PRI(t
, new_pri
);
2207 if (t
== cp
->cpu_dispthread
)
2208 cp
->cpu_dispatch_pri
= DISP_PRIO(t
);
2209 if (DISP_MUST_SURRENDER(t
)) {
2210 tspp
->ts_flags
|= TSBACKQ
;
2214 ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
2219 frontq
= (tspp
->ts_flags
& TSIASET
) != 0;
2221 * When the priority of a thread is changed,
2222 * it may be necessary to adjust its position
2223 * on a sleep queue or dispatch queue.
2224 * The function thread_change_pri accomplishes
2227 if (thread_change_pri(t
, new_pri
, frontq
)) {
2229 * The thread was on a run queue. Reset
2230 * its CPU timeleft from the quantum
2231 * associated with the new priority.
2234 ts_dptbl
[tspp
->ts_cpupri
].ts_quantum
;
2236 tspp
->ts_flags
|= TSBACKQ
;
2242 ts_alloc(void **p
, int flag
)
2245 bufp
= kmem_alloc(sizeof (tsproc_t
), flag
);
2258 kmem_free(bufp
, sizeof (tsproc_t
));