xref: /titanic_51/usr/src/uts/common/disp/fss.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/project.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/fss.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/fsspriocntl.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
52*7c478bd9Sstevel@tonic-gate #include <vm/rm.h>
53*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * FSS Data Structures:
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  *                 fsszone
62*7c478bd9Sstevel@tonic-gate  *                  -----           -----
63*7c478bd9Sstevel@tonic-gate  *  -----          |     |         |     |
64*7c478bd9Sstevel@tonic-gate  * |     |-------->|     |<------->|     |<---->...
65*7c478bd9Sstevel@tonic-gate  * |     |          -----           -----
66*7c478bd9Sstevel@tonic-gate  * |     |          ^    ^            ^
67*7c478bd9Sstevel@tonic-gate  * |     |---       |     \            \
68*7c478bd9Sstevel@tonic-gate  *  -----    |      |      \            \
69*7c478bd9Sstevel@tonic-gate  * fsspset   |      |       \            \
70*7c478bd9Sstevel@tonic-gate  *           |      |        \            \
71*7c478bd9Sstevel@tonic-gate  *           |    -----       -----       -----
72*7c478bd9Sstevel@tonic-gate  *            -->|     |<--->|     |<--->|     |
73*7c478bd9Sstevel@tonic-gate  *               |     |     |     |     |     |
74*7c478bd9Sstevel@tonic-gate  *                -----       -----       -----
75*7c478bd9Sstevel@tonic-gate  *               fssproj
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * That is, fsspsets contain a list of fsszone's that are currently active in
79*7c478bd9Sstevel@tonic-gate  * the pset, and a list of fssproj's, corresponding to projects with runnable
80*7c478bd9Sstevel@tonic-gate  * threads on the pset.  fssproj's in turn point to the fsszone which they
81*7c478bd9Sstevel@tonic-gate  * are a member of.
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * An fssproj_t is removed when there are no threads in it.
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  * An fsszone_t is removed when there are no projects with threads in it.
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * Projects in a zone compete with each other for cpu time, receiving cpu
88*7c478bd9Sstevel@tonic-gate  * allocation within a zone proportional to fssproj->fssp_shares
89*7c478bd9Sstevel@tonic-gate  * (project.cpu-shares); at a higher level zones compete with each other,
90*7c478bd9Sstevel@tonic-gate  * receiving allocation in a pset proportional to fsszone->fssz_shares
91*7c478bd9Sstevel@tonic-gate  * (zone.cpu-shares).  See fss_decay_usage() for the precise formula.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static pri_t fss_init(id_t, int, classfuncs_t **);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static struct sclass fss = {
97*7c478bd9Sstevel@tonic-gate 	"FSS",
98*7c478bd9Sstevel@tonic-gate 	fss_init,
99*7c478bd9Sstevel@tonic-gate 	0
100*7c478bd9Sstevel@tonic-gate };
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_schedops;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
106*7c478bd9Sstevel@tonic-gate  */
107*7c478bd9Sstevel@tonic-gate static struct modlsched modlsched = {
108*7c478bd9Sstevel@tonic-gate 	&mod_schedops, "fair share scheduling class", &fss
109*7c478bd9Sstevel@tonic-gate };
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
112*7c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlsched, NULL
113*7c478bd9Sstevel@tonic-gate };
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate #define	FSS_MAXUPRI	60
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate  * The fssproc_t structures are kept in an array of circular doubly linked
119*7c478bd9Sstevel@tonic-gate  * lists.  A hash on the thread pointer is used to determine which list each
120*7c478bd9Sstevel@tonic-gate  * thread should be placed in.  Each list has a dummy "head" which is never
121*7c478bd9Sstevel@tonic-gate  * removed, so the list is never empty.  fss_update traverses these lists to
122*7c478bd9Sstevel@tonic-gate  * update the priorities of threads that have been waiting on the run queue.
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate #define	FSS_LISTS		16 /* number of lists, must be power of 2 */
125*7c478bd9Sstevel@tonic-gate #define	FSS_LIST_HASH(t)	(((uintptr_t)(t) >> 9) & (FSS_LISTS - 1))
126*7c478bd9Sstevel@tonic-gate #define	FSS_LIST_NEXT(i)	(((i) + 1) & (FSS_LISTS - 1))
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate #define	FSS_LIST_INSERT(fssproc)				\
129*7c478bd9Sstevel@tonic-gate {								\
130*7c478bd9Sstevel@tonic-gate 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
131*7c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &fss_listlock[index];			\
132*7c478bd9Sstevel@tonic-gate 	fssproc_t *headp = &fss_listhead[index];		\
133*7c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);					\
134*7c478bd9Sstevel@tonic-gate 	fssproc->fss_next = headp->fss_next;			\
135*7c478bd9Sstevel@tonic-gate 	fssproc->fss_prev = headp;				\
136*7c478bd9Sstevel@tonic-gate 	headp->fss_next->fss_prev = fssproc;			\
137*7c478bd9Sstevel@tonic-gate 	headp->fss_next = fssproc;				\
138*7c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);					\
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate #define	FSS_LIST_DELETE(fssproc)				\
142*7c478bd9Sstevel@tonic-gate {								\
143*7c478bd9Sstevel@tonic-gate 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
144*7c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &fss_listlock[index];			\
145*7c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);					\
146*7c478bd9Sstevel@tonic-gate 	fssproc->fss_prev->fss_next = fssproc->fss_next;	\
147*7c478bd9Sstevel@tonic-gate 	fssproc->fss_next->fss_prev = fssproc->fss_prev;	\
148*7c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);					\
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate #define	FSS_TICK_COST	1000	/* tick cost for threads with nice level = 0 */
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate  * Decay rate percentages are based on n/128 rather than n/100 so  that
155*7c478bd9Sstevel@tonic-gate  * calculations can avoid having to do an integer divide by 100 (divide
156*7c478bd9Sstevel@tonic-gate  * by FSS_DECAY_BASE == 128 optimizes to an arithmetic shift).
157*7c478bd9Sstevel@tonic-gate  *
158*7c478bd9Sstevel@tonic-gate  * FSS_DECAY_MIN	=  83/128 ~= 65%
159*7c478bd9Sstevel@tonic-gate  * FSS_DECAY_MAX	= 108/128 ~= 85%
160*7c478bd9Sstevel@tonic-gate  * FSS_DECAY_USG	=  96/128 ~= 75%
161*7c478bd9Sstevel@tonic-gate  */
162*7c478bd9Sstevel@tonic-gate #define	FSS_DECAY_MIN	83	/* fsspri decay pct for threads w/ nice -20 */
163*7c478bd9Sstevel@tonic-gate #define	FSS_DECAY_MAX	108	/* fsspri decay pct for threads w/ nice +19 */
164*7c478bd9Sstevel@tonic-gate #define	FSS_DECAY_USG	96	/* fssusage decay pct for projects */
165*7c478bd9Sstevel@tonic-gate #define	FSS_DECAY_BASE	128	/* base for decay percentages above */
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate #define	FSS_NICE_MIN	0
168*7c478bd9Sstevel@tonic-gate #define	FSS_NICE_MAX	(2 * NZERO - 1)
169*7c478bd9Sstevel@tonic-gate #define	FSS_NICE_RANGE	(FSS_NICE_MAX - FSS_NICE_MIN + 1)
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate static int	fss_nice_tick[FSS_NICE_RANGE];
172*7c478bd9Sstevel@tonic-gate static int	fss_nice_decay[FSS_NICE_RANGE];
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate static pri_t	fss_maxupri = FSS_MAXUPRI; /* maximum FSS user priority */
175*7c478bd9Sstevel@tonic-gate static pri_t	fss_maxumdpri; /* maximum user mode fss priority */
176*7c478bd9Sstevel@tonic-gate static pri_t	fss_maxglobpri;	/* maximum global priority used by fss class */
177*7c478bd9Sstevel@tonic-gate static pri_t	fss_minglobpri;	/* minimum global priority */
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate static fssproc_t fss_listhead[FSS_LISTS];
180*7c478bd9Sstevel@tonic-gate static kmutex_t	fss_listlock[FSS_LISTS];
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate static fsspset_t *fsspsets;
183*7c478bd9Sstevel@tonic-gate static kmutex_t fsspsets_lock;	/* protects fsspsets */
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate static id_t	fss_cid;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate static time_t	fss_minrun = 2;	/* t_pri becomes 59 within 2 secs */
188*7c478bd9Sstevel@tonic-gate static time_t	fss_minslp = 2;	/* min time on sleep queue for hardswap */
189*7c478bd9Sstevel@tonic-gate static int	fss_quantum = 11;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate static void	fss_newpri(fssproc_t *);
192*7c478bd9Sstevel@tonic-gate static void	fss_update(void *);
193*7c478bd9Sstevel@tonic-gate static int	fss_update_list(int);
194*7c478bd9Sstevel@tonic-gate static void	fss_change_priority(kthread_t *, fssproc_t *);
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate static int	fss_admin(caddr_t, cred_t *);
197*7c478bd9Sstevel@tonic-gate static int	fss_getclinfo(void *);
198*7c478bd9Sstevel@tonic-gate static int	fss_parmsin(void *);
199*7c478bd9Sstevel@tonic-gate static int	fss_parmsout(void *, pc_vaparms_t *);
200*7c478bd9Sstevel@tonic-gate static int	fss_vaparmsin(void *, pc_vaparms_t *);
201*7c478bd9Sstevel@tonic-gate static int	fss_vaparmsout(void *, pc_vaparms_t *);
202*7c478bd9Sstevel@tonic-gate static int	fss_getclpri(pcpri_t *);
203*7c478bd9Sstevel@tonic-gate static int	fss_alloc(void **, int);
204*7c478bd9Sstevel@tonic-gate static void	fss_free(void *);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate static int	fss_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
207*7c478bd9Sstevel@tonic-gate static void	fss_exitclass(void *);
208*7c478bd9Sstevel@tonic-gate static int	fss_canexit(kthread_t *, cred_t *);
209*7c478bd9Sstevel@tonic-gate static int	fss_fork(kthread_t *, kthread_t *, void *);
210*7c478bd9Sstevel@tonic-gate static void	fss_forkret(kthread_t *, kthread_t *);
211*7c478bd9Sstevel@tonic-gate static void	fss_parmsget(kthread_t *, void *);
212*7c478bd9Sstevel@tonic-gate static int	fss_parmsset(kthread_t *, void *, id_t, cred_t *);
213*7c478bd9Sstevel@tonic-gate static void	fss_stop(kthread_t *, int, int);
214*7c478bd9Sstevel@tonic-gate static void	fss_exit(kthread_t *);
215*7c478bd9Sstevel@tonic-gate static void	fss_active(kthread_t *);
216*7c478bd9Sstevel@tonic-gate static void	fss_inactive(kthread_t *);
217*7c478bd9Sstevel@tonic-gate static pri_t	fss_swapin(kthread_t *, int);
218*7c478bd9Sstevel@tonic-gate static pri_t	fss_swapout(kthread_t *, int);
219*7c478bd9Sstevel@tonic-gate static void	fss_trapret(kthread_t *);
220*7c478bd9Sstevel@tonic-gate static void	fss_preempt(kthread_t *);
221*7c478bd9Sstevel@tonic-gate static void	fss_setrun(kthread_t *);
222*7c478bd9Sstevel@tonic-gate static void	fss_sleep(kthread_t *);
223*7c478bd9Sstevel@tonic-gate static void	fss_tick(kthread_t *);
224*7c478bd9Sstevel@tonic-gate static void	fss_wakeup(kthread_t *);
225*7c478bd9Sstevel@tonic-gate static int	fss_donice(kthread_t *, cred_t *, int, int *);
226*7c478bd9Sstevel@tonic-gate static pri_t	fss_globpri(kthread_t *);
227*7c478bd9Sstevel@tonic-gate static void	fss_yield(kthread_t *);
228*7c478bd9Sstevel@tonic-gate static void	fss_nullsys();
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate static struct classfuncs fss_classfuncs = {
231*7c478bd9Sstevel@tonic-gate 	/* class functions */
232*7c478bd9Sstevel@tonic-gate 	fss_admin,
233*7c478bd9Sstevel@tonic-gate 	fss_getclinfo,
234*7c478bd9Sstevel@tonic-gate 	fss_parmsin,
235*7c478bd9Sstevel@tonic-gate 	fss_parmsout,
236*7c478bd9Sstevel@tonic-gate 	fss_vaparmsin,
237*7c478bd9Sstevel@tonic-gate 	fss_vaparmsout,
238*7c478bd9Sstevel@tonic-gate 	fss_getclpri,
239*7c478bd9Sstevel@tonic-gate 	fss_alloc,
240*7c478bd9Sstevel@tonic-gate 	fss_free,
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	/* thread functions */
243*7c478bd9Sstevel@tonic-gate 	fss_enterclass,
244*7c478bd9Sstevel@tonic-gate 	fss_exitclass,
245*7c478bd9Sstevel@tonic-gate 	fss_canexit,
246*7c478bd9Sstevel@tonic-gate 	fss_fork,
247*7c478bd9Sstevel@tonic-gate 	fss_forkret,
248*7c478bd9Sstevel@tonic-gate 	fss_parmsget,
249*7c478bd9Sstevel@tonic-gate 	fss_parmsset,
250*7c478bd9Sstevel@tonic-gate 	fss_stop,
251*7c478bd9Sstevel@tonic-gate 	fss_exit,
252*7c478bd9Sstevel@tonic-gate 	fss_active,
253*7c478bd9Sstevel@tonic-gate 	fss_inactive,
254*7c478bd9Sstevel@tonic-gate 	fss_swapin,
255*7c478bd9Sstevel@tonic-gate 	fss_swapout,
256*7c478bd9Sstevel@tonic-gate 	fss_trapret,
257*7c478bd9Sstevel@tonic-gate 	fss_preempt,
258*7c478bd9Sstevel@tonic-gate 	fss_setrun,
259*7c478bd9Sstevel@tonic-gate 	fss_sleep,
260*7c478bd9Sstevel@tonic-gate 	fss_tick,
261*7c478bd9Sstevel@tonic-gate 	fss_wakeup,
262*7c478bd9Sstevel@tonic-gate 	fss_donice,
263*7c478bd9Sstevel@tonic-gate 	fss_globpri,
264*7c478bd9Sstevel@tonic-gate 	fss_nullsys,	/* set_process_group */
265*7c478bd9Sstevel@tonic-gate 	fss_yield
266*7c478bd9Sstevel@tonic-gate };
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate int
269*7c478bd9Sstevel@tonic-gate _init()
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate int
275*7c478bd9Sstevel@tonic-gate _fini()
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	return (EBUSY);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate int
281*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
282*7c478bd9Sstevel@tonic-gate {
283*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
287*7c478bd9Sstevel@tonic-gate static int
288*7c478bd9Sstevel@tonic-gate fss_project_walker(kproject_t *kpj, void *buf)
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	return (0);
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate void *
294*7c478bd9Sstevel@tonic-gate fss_allocbuf(int op, int type)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate 	fssbuf_t *fssbuf;
297*7c478bd9Sstevel@tonic-gate 	void **fsslist;
298*7c478bd9Sstevel@tonic-gate 	int cnt;
299*7c478bd9Sstevel@tonic-gate 	int i;
300*7c478bd9Sstevel@tonic-gate 	size_t size;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	ASSERT(op == FSS_NPSET_BUF || op == FSS_NPROJ_BUF || op == FSS_ONE_BUF);
303*7c478bd9Sstevel@tonic-gate 	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
304*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	fssbuf = kmem_zalloc(sizeof (fssbuf_t), KM_SLEEP);
307*7c478bd9Sstevel@tonic-gate 	switch (op) {
308*7c478bd9Sstevel@tonic-gate 	case FSS_NPSET_BUF:
309*7c478bd9Sstevel@tonic-gate 		cnt = cpupart_list(NULL, 0, CP_NONEMPTY);
310*7c478bd9Sstevel@tonic-gate 		break;
311*7c478bd9Sstevel@tonic-gate 	case FSS_NPROJ_BUF:
312*7c478bd9Sstevel@tonic-gate 		cnt = project_walk_all(ALL_ZONES, fss_project_walker, NULL);
313*7c478bd9Sstevel@tonic-gate 		break;
314*7c478bd9Sstevel@tonic-gate 	case FSS_ONE_BUF:
315*7c478bd9Sstevel@tonic-gate 		cnt = 1;
316*7c478bd9Sstevel@tonic-gate 		break;
317*7c478bd9Sstevel@tonic-gate 	}
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	switch (type) {
320*7c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_PROJ:
321*7c478bd9Sstevel@tonic-gate 		size = sizeof (fssproj_t);
322*7c478bd9Sstevel@tonic-gate 		break;
323*7c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_ZONE:
324*7c478bd9Sstevel@tonic-gate 		size = sizeof (fsszone_t);
325*7c478bd9Sstevel@tonic-gate 		break;
326*7c478bd9Sstevel@tonic-gate 	}
327*7c478bd9Sstevel@tonic-gate 	fsslist = kmem_zalloc(cnt * sizeof (void *), KM_SLEEP);
328*7c478bd9Sstevel@tonic-gate 	fssbuf->fssb_size = cnt;
329*7c478bd9Sstevel@tonic-gate 	fssbuf->fssb_list = fsslist;
330*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++)
331*7c478bd9Sstevel@tonic-gate 		fsslist[i] = kmem_zalloc(size, KM_SLEEP);
332*7c478bd9Sstevel@tonic-gate 	return (fssbuf);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate void
336*7c478bd9Sstevel@tonic-gate fss_freebuf(fssbuf_t *fssbuf, int type)
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 	void **fsslist;
339*7c478bd9Sstevel@tonic-gate 	int i;
340*7c478bd9Sstevel@tonic-gate 	size_t size;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	ASSERT(fssbuf != NULL);
343*7c478bd9Sstevel@tonic-gate 	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
344*7c478bd9Sstevel@tonic-gate 	fsslist = fssbuf->fssb_list;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	switch (type) {
347*7c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_PROJ:
348*7c478bd9Sstevel@tonic-gate 		size = sizeof (fssproj_t);
349*7c478bd9Sstevel@tonic-gate 		break;
350*7c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_ZONE:
351*7c478bd9Sstevel@tonic-gate 		size = sizeof (fsszone_t);
352*7c478bd9Sstevel@tonic-gate 		break;
353*7c478bd9Sstevel@tonic-gate 	}
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < fssbuf->fssb_size; i++) {
356*7c478bd9Sstevel@tonic-gate 		if (fsslist[i] != NULL)
357*7c478bd9Sstevel@tonic-gate 			kmem_free(fsslist[i], size);
358*7c478bd9Sstevel@tonic-gate 	}
359*7c478bd9Sstevel@tonic-gate 	kmem_free(fsslist, sizeof (void *) * fssbuf->fssb_size);
360*7c478bd9Sstevel@tonic-gate 	kmem_free(fssbuf, sizeof (fssbuf_t));
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate static fsspset_t *
364*7c478bd9Sstevel@tonic-gate fss_find_fsspset(cpupart_t *cpupart)
365*7c478bd9Sstevel@tonic-gate {
366*7c478bd9Sstevel@tonic-gate 	int i;
367*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset = NULL;
368*7c478bd9Sstevel@tonic-gate 	int found = 0;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	ASSERT(cpupart != NULL);
371*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	/*
374*7c478bd9Sstevel@tonic-gate 	 * Search for the cpupart pointer in the array of fsspsets.
375*7c478bd9Sstevel@tonic-gate 	 */
376*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_ncpus; i++) {
377*7c478bd9Sstevel@tonic-gate 		fsspset = &fsspsets[i];
378*7c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_cpupart == cpupart) {
379*7c478bd9Sstevel@tonic-gate 			ASSERT(fsspset->fssps_nproj > 0);
380*7c478bd9Sstevel@tonic-gate 			found = 1;
381*7c478bd9Sstevel@tonic-gate 			break;
382*7c478bd9Sstevel@tonic-gate 		}
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 	if (found == 0) {
385*7c478bd9Sstevel@tonic-gate 		/*
386*7c478bd9Sstevel@tonic-gate 		 * If we didn't find anything, then use the first
387*7c478bd9Sstevel@tonic-gate 		 * available slot in the fsspsets array.
388*7c478bd9Sstevel@tonic-gate 		 */
389*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < max_ncpus; i++) {
390*7c478bd9Sstevel@tonic-gate 			fsspset = &fsspsets[i];
391*7c478bd9Sstevel@tonic-gate 			if (fsspset->fssps_cpupart == NULL) {
392*7c478bd9Sstevel@tonic-gate 				ASSERT(fsspset->fssps_nproj == 0);
393*7c478bd9Sstevel@tonic-gate 				found = 1;
394*7c478bd9Sstevel@tonic-gate 				break;
395*7c478bd9Sstevel@tonic-gate 			}
396*7c478bd9Sstevel@tonic-gate 		}
397*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_cpupart = cpupart;
398*7c478bd9Sstevel@tonic-gate 	}
399*7c478bd9Sstevel@tonic-gate 	ASSERT(found == 1);
400*7c478bd9Sstevel@tonic-gate 	return (fsspset);
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate static void
404*7c478bd9Sstevel@tonic-gate fss_del_fsspset(fsspset_t *fsspset)
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
407*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
408*7c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_nproj == 0);
409*7c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_list == NULL);
410*7c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_zones == NULL);
411*7c478bd9Sstevel@tonic-gate 	fsspset->fssps_cpupart = NULL;
412*7c478bd9Sstevel@tonic-gate 	fsspset->fssps_maxfsspri = 0;
413*7c478bd9Sstevel@tonic-gate 	fsspset->fssps_shares = 0;
414*7c478bd9Sstevel@tonic-gate }
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate /*
417*7c478bd9Sstevel@tonic-gate  * The following routine returns a pointer to the fsszone structure which
418*7c478bd9Sstevel@tonic-gate  * belongs to zone "zone" and cpu partition fsspset, if such structure exists.
419*7c478bd9Sstevel@tonic-gate  */
420*7c478bd9Sstevel@tonic-gate static fsszone_t *
421*7c478bd9Sstevel@tonic-gate fss_find_fsszone(fsspset_t *fsspset, zone_t *zone)
422*7c478bd9Sstevel@tonic-gate {
423*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list != NULL) {
428*7c478bd9Sstevel@tonic-gate 		/*
429*7c478bd9Sstevel@tonic-gate 		 * There are projects/zones active on this cpu partition
430*7c478bd9Sstevel@tonic-gate 		 * already.  Try to find our zone among them.
431*7c478bd9Sstevel@tonic-gate 		 */
432*7c478bd9Sstevel@tonic-gate 		fsszone = fsspset->fssps_zones;
433*7c478bd9Sstevel@tonic-gate 		do {
434*7c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_zone == zone) {
435*7c478bd9Sstevel@tonic-gate 				return (fsszone);
436*7c478bd9Sstevel@tonic-gate 			}
437*7c478bd9Sstevel@tonic-gate 			fsszone = fsszone->fssz_next;
438*7c478bd9Sstevel@tonic-gate 		} while (fsszone != fsspset->fssps_zones);
439*7c478bd9Sstevel@tonic-gate 	}
440*7c478bd9Sstevel@tonic-gate 	return (NULL);
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate /*
444*7c478bd9Sstevel@tonic-gate  * The following routine links new fsszone structure into doubly linked list of
445*7c478bd9Sstevel@tonic-gate  * zones active on the specified cpu partition.
446*7c478bd9Sstevel@tonic-gate  */
447*7c478bd9Sstevel@tonic-gate static void
448*7c478bd9Sstevel@tonic-gate fss_insert_fsszone(fsspset_t *fsspset, zone_t *zone, fsszone_t *fsszone)
449*7c478bd9Sstevel@tonic-gate {
450*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	fsszone->fssz_zone = zone;
453*7c478bd9Sstevel@tonic-gate 	fsszone->fssz_rshares = zone->zone_shares;
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_zones == NULL) {
456*7c478bd9Sstevel@tonic-gate 		/*
457*7c478bd9Sstevel@tonic-gate 		 * This will be the first fsszone for this fsspset
458*7c478bd9Sstevel@tonic-gate 		 */
459*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_next = fsszone->fssz_prev = fsszone;
460*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = fsszone;
461*7c478bd9Sstevel@tonic-gate 	} else {
462*7c478bd9Sstevel@tonic-gate 		/*
463*7c478bd9Sstevel@tonic-gate 		 * Insert this fsszone to the doubly linked list.
464*7c478bd9Sstevel@tonic-gate 		 */
465*7c478bd9Sstevel@tonic-gate 		fsszone_t *fssz_head = fsspset->fssps_zones;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_next = fssz_head;
468*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_prev = fssz_head->fssz_prev;
469*7c478bd9Sstevel@tonic-gate 		fssz_head->fssz_prev->fssz_next = fsszone;
470*7c478bd9Sstevel@tonic-gate 		fssz_head->fssz_prev = fsszone;
471*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = fsszone;
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /*
476*7c478bd9Sstevel@tonic-gate  * The following routine removes a single fsszone structure from the doubly
477*7c478bd9Sstevel@tonic-gate  * linked list of zones active on the specified cpu partition.  Note that
478*7c478bd9Sstevel@tonic-gate  * global fsspsets_lock must be held in case this fsszone structure is the last
479*7c478bd9Sstevel@tonic-gate  * on the above mentioned list.  Also note that the fsszone structure is not
480*7c478bd9Sstevel@tonic-gate  * freed here, it is the responsibility of the caller to call kmem_free for it.
481*7c478bd9Sstevel@tonic-gate  */
482*7c478bd9Sstevel@tonic-gate static void
483*7c478bd9Sstevel@tonic-gate fss_remove_fsszone(fsspset_t *fsspset, fsszone_t *fsszone)
484*7c478bd9Sstevel@tonic-gate {
485*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
486*7c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_nproj == 0);
487*7c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_shares == 0);
488*7c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_runnable == 0);
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	if (fsszone->fssz_next != fsszone) {
491*7c478bd9Sstevel@tonic-gate 		/*
492*7c478bd9Sstevel@tonic-gate 		 * This is not the last zone in the list.
493*7c478bd9Sstevel@tonic-gate 		 */
494*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_prev->fssz_next = fsszone->fssz_next;
495*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_next->fssz_prev = fsszone->fssz_prev;
496*7c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_zones == fsszone)
497*7c478bd9Sstevel@tonic-gate 			fsspset->fssps_zones = fsszone->fssz_next;
498*7c478bd9Sstevel@tonic-gate 	} else {
499*7c478bd9Sstevel@tonic-gate 		/*
500*7c478bd9Sstevel@tonic-gate 		 * This was the last zone active in this cpu partition.
501*7c478bd9Sstevel@tonic-gate 		 */
502*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = NULL;
503*7c478bd9Sstevel@tonic-gate 	}
504*7c478bd9Sstevel@tonic-gate }
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate /*
507*7c478bd9Sstevel@tonic-gate  * The following routine returns a pointer to the fssproj structure
508*7c478bd9Sstevel@tonic-gate  * which belongs to project kpj and cpu partition fsspset, if such structure
509*7c478bd9Sstevel@tonic-gate  * exists.
510*7c478bd9Sstevel@tonic-gate  */
511*7c478bd9Sstevel@tonic-gate static fssproj_t *
512*7c478bd9Sstevel@tonic-gate fss_find_fssproj(fsspset_t *fsspset, kproject_t *kpj)
513*7c478bd9Sstevel@tonic-gate {
514*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list != NULL) {
519*7c478bd9Sstevel@tonic-gate 		/*
520*7c478bd9Sstevel@tonic-gate 		 * There are projects running on this cpu partition already.
521*7c478bd9Sstevel@tonic-gate 		 * Try to find our project among them.
522*7c478bd9Sstevel@tonic-gate 		 */
523*7c478bd9Sstevel@tonic-gate 		fssproj = fsspset->fssps_list;
524*7c478bd9Sstevel@tonic-gate 		do {
525*7c478bd9Sstevel@tonic-gate 			if (fssproj->fssp_proj == kpj) {
526*7c478bd9Sstevel@tonic-gate 				ASSERT(fssproj->fssp_pset == fsspset);
527*7c478bd9Sstevel@tonic-gate 				return (fssproj);
528*7c478bd9Sstevel@tonic-gate 			}
529*7c478bd9Sstevel@tonic-gate 			fssproj = fssproj->fssp_next;
530*7c478bd9Sstevel@tonic-gate 		} while (fssproj != fsspset->fssps_list);
531*7c478bd9Sstevel@tonic-gate 	}
532*7c478bd9Sstevel@tonic-gate 	return (NULL);
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate /*
536*7c478bd9Sstevel@tonic-gate  * The following routine links new fssproj structure into doubly linked list
537*7c478bd9Sstevel@tonic-gate  * of projects running on the specified cpu partition.
538*7c478bd9Sstevel@tonic-gate  */
539*7c478bd9Sstevel@tonic-gate static void
540*7c478bd9Sstevel@tonic-gate fss_insert_fssproj(fsspset_t *fsspset, kproject_t *kpj, fsszone_t *fsszone,
541*7c478bd9Sstevel@tonic-gate     fssproj_t *fssproj)
542*7c478bd9Sstevel@tonic-gate {
543*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 	fssproj->fssp_pset = fsspset;
546*7c478bd9Sstevel@tonic-gate 	fssproj->fssp_proj = kpj;
547*7c478bd9Sstevel@tonic-gate 	fssproj->fssp_shares = kpj->kpj_shares;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 	fsspset->fssps_nproj++;
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list == NULL) {
552*7c478bd9Sstevel@tonic-gate 		/*
553*7c478bd9Sstevel@tonic-gate 		 * This will be the first fssproj for this fsspset
554*7c478bd9Sstevel@tonic-gate 		 */
555*7c478bd9Sstevel@tonic-gate 		fssproj->fssp_next = fssproj->fssp_prev = fssproj;
556*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = fssproj;
557*7c478bd9Sstevel@tonic-gate 	} else {
558*7c478bd9Sstevel@tonic-gate 		/*
559*7c478bd9Sstevel@tonic-gate 		 * Insert this fssproj to the doubly linked list.
560*7c478bd9Sstevel@tonic-gate 		 */
561*7c478bd9Sstevel@tonic-gate 		fssproj_t *fssp_head = fsspset->fssps_list;
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 		fssproj->fssp_next = fssp_head;
564*7c478bd9Sstevel@tonic-gate 		fssproj->fssp_prev = fssp_head->fssp_prev;
565*7c478bd9Sstevel@tonic-gate 		fssp_head->fssp_prev->fssp_next = fssproj;
566*7c478bd9Sstevel@tonic-gate 		fssp_head->fssp_prev = fssproj;
567*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = fssproj;
568*7c478bd9Sstevel@tonic-gate 	}
569*7c478bd9Sstevel@tonic-gate 	fssproj->fssp_fsszone = fsszone;
570*7c478bd9Sstevel@tonic-gate 	fsszone->fssz_nproj++;
571*7c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_nproj != 0);
572*7c478bd9Sstevel@tonic-gate }
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate /*
575*7c478bd9Sstevel@tonic-gate  * The following routine removes a single fssproj structure from the doubly
576*7c478bd9Sstevel@tonic-gate  * linked list of projects running on the specified cpu partition.  Note that
577*7c478bd9Sstevel@tonic-gate  * global fsspsets_lock must be held in case if this fssproj structure is the
578*7c478bd9Sstevel@tonic-gate  * last on the above mentioned list.  Also note that the fssproj structure is
579*7c478bd9Sstevel@tonic-gate  * not freed here, it is the responsibility of the caller to call kmem_free
580*7c478bd9Sstevel@tonic-gate  * for it.
581*7c478bd9Sstevel@tonic-gate  */
582*7c478bd9Sstevel@tonic-gate static void
583*7c478bd9Sstevel@tonic-gate fss_remove_fssproj(fsspset_t *fsspset, fssproj_t *fssproj)
584*7c478bd9Sstevel@tonic-gate {
585*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
588*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
589*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproj->fssp_runnable == 0);
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	fsspset->fssps_nproj--;
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
594*7c478bd9Sstevel@tonic-gate 	fsszone->fssz_nproj--;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	if (fssproj->fssp_next != fssproj) {
597*7c478bd9Sstevel@tonic-gate 		/*
598*7c478bd9Sstevel@tonic-gate 		 * This is not the last part in the list.
599*7c478bd9Sstevel@tonic-gate 		 */
600*7c478bd9Sstevel@tonic-gate 		fssproj->fssp_prev->fssp_next = fssproj->fssp_next;
601*7c478bd9Sstevel@tonic-gate 		fssproj->fssp_next->fssp_prev = fssproj->fssp_prev;
602*7c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_list == fssproj)
603*7c478bd9Sstevel@tonic-gate 			fsspset->fssps_list = fssproj->fssp_next;
604*7c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
605*7c478bd9Sstevel@tonic-gate 			fss_remove_fsszone(fsspset, fsszone);
606*7c478bd9Sstevel@tonic-gate 	} else {
607*7c478bd9Sstevel@tonic-gate 		/*
608*7c478bd9Sstevel@tonic-gate 		 * This was the last project part running
609*7c478bd9Sstevel@tonic-gate 		 * at this cpu partition.
610*7c478bd9Sstevel@tonic-gate 		 */
611*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = NULL;
612*7c478bd9Sstevel@tonic-gate 		ASSERT(fsspset->fssps_nproj == 0);
613*7c478bd9Sstevel@tonic-gate 		ASSERT(fsszone->fssz_nproj == 0);
614*7c478bd9Sstevel@tonic-gate 		fss_remove_fsszone(fsspset, fsszone);
615*7c478bd9Sstevel@tonic-gate 		fss_del_fsspset(fsspset);
616*7c478bd9Sstevel@tonic-gate 	}
617*7c478bd9Sstevel@tonic-gate }
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate static void
620*7c478bd9Sstevel@tonic-gate fss_inactive(kthread_t *t)
621*7c478bd9Sstevel@tonic-gate {
622*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
623*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
624*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
625*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
628*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
629*7c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
630*7c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)	/* if this thread already exited */
631*7c478bd9Sstevel@tonic-gate 		return;
632*7c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
633*7c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
634*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
635*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproj->fssp_runnable > 0);
636*7c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_runnable == 0) {
637*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_shares -= fssproj->fssp_shares;
638*7c478bd9Sstevel@tonic-gate 		if (--fsszone->fssz_runnable == 0)
639*7c478bd9Sstevel@tonic-gate 			fsspset->fssps_shares -= fsszone->fssz_rshares;
640*7c478bd9Sstevel@tonic-gate 	}
641*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproc->fss_runnable == 1);
642*7c478bd9Sstevel@tonic-gate 	fssproc->fss_runnable = 0;
643*7c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
644*7c478bd9Sstevel@tonic-gate }
645*7c478bd9Sstevel@tonic-gate 
646*7c478bd9Sstevel@tonic-gate static void
647*7c478bd9Sstevel@tonic-gate fss_active(kthread_t *t)
648*7c478bd9Sstevel@tonic-gate {
649*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
650*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
651*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
652*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
655*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
656*7c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
657*7c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)	/* if this thread already exited */
658*7c478bd9Sstevel@tonic-gate 		return;
659*7c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
660*7c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
661*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
662*7c478bd9Sstevel@tonic-gate 	if (++fssproj->fssp_runnable == 1) {
663*7c478bd9Sstevel@tonic-gate 		fsszone->fssz_shares += fssproj->fssp_shares;
664*7c478bd9Sstevel@tonic-gate 		if (++fsszone->fssz_runnable == 1)
665*7c478bd9Sstevel@tonic-gate 			fsspset->fssps_shares += fsszone->fssz_rshares;
666*7c478bd9Sstevel@tonic-gate 	}
667*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproc->fss_runnable == 0);
668*7c478bd9Sstevel@tonic-gate 	fssproc->fss_runnable = 1;
669*7c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
670*7c478bd9Sstevel@tonic-gate }
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate /*
673*7c478bd9Sstevel@tonic-gate  * Fair share scheduler initialization. Called by dispinit() at boot time.
674*7c478bd9Sstevel@tonic-gate  * We can ignore clparmsz argument since we know that the smallest possible
675*7c478bd9Sstevel@tonic-gate  * parameter buffer is big enough for us.
676*7c478bd9Sstevel@tonic-gate  */
677*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
678*7c478bd9Sstevel@tonic-gate static pri_t
679*7c478bd9Sstevel@tonic-gate fss_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
680*7c478bd9Sstevel@tonic-gate {
681*7c478bd9Sstevel@tonic-gate 	int i;
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	fss_cid = cid;
686*7c478bd9Sstevel@tonic-gate 	fss_maxumdpri = minclsyspri - 1;
687*7c478bd9Sstevel@tonic-gate 	fss_maxglobpri = minclsyspri;
688*7c478bd9Sstevel@tonic-gate 	fss_minglobpri = 0;
689*7c478bd9Sstevel@tonic-gate 	fsspsets = kmem_zalloc(sizeof (fsspset_t) * max_ncpus, KM_SLEEP);
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate 	/*
692*7c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc hash table.
693*7c478bd9Sstevel@tonic-gate 	 */
694*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < FSS_LISTS; i++)
695*7c478bd9Sstevel@tonic-gate 		fss_listhead[i].fss_next = fss_listhead[i].fss_prev =
696*7c478bd9Sstevel@tonic-gate 		    &fss_listhead[i];
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	*clfuncspp = &fss_classfuncs;
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	/*
701*7c478bd9Sstevel@tonic-gate 	 * Fill in fss_nice_tick and fss_nice_decay arrays:
702*7c478bd9Sstevel@tonic-gate 	 * The cost of a tick is lower at positive nice values (so that it
703*7c478bd9Sstevel@tonic-gate 	 * will not increase its project's usage as much as normal) with 50%
704*7c478bd9Sstevel@tonic-gate 	 * drop at the maximum level and 50% increase at the minimum level.
705*7c478bd9Sstevel@tonic-gate 	 * The fsspri decay is slower at positive nice values.  fsspri values
706*7c478bd9Sstevel@tonic-gate 	 * of processes with negative nice levels must decay faster to receive
707*7c478bd9Sstevel@tonic-gate 	 * time slices more frequently than normal.
708*7c478bd9Sstevel@tonic-gate 	 */
709*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < FSS_NICE_RANGE; i++) {
710*7c478bd9Sstevel@tonic-gate 		fss_nice_tick[i] = (FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2)
711*7c478bd9Sstevel@tonic-gate 		    - i)) / FSS_NICE_RANGE;
712*7c478bd9Sstevel@tonic-gate 		fss_nice_decay[i] = FSS_DECAY_MIN +
713*7c478bd9Sstevel@tonic-gate 		    ((FSS_DECAY_MAX - FSS_DECAY_MIN) * i) /
714*7c478bd9Sstevel@tonic-gate 		    (FSS_NICE_RANGE - 1);
715*7c478bd9Sstevel@tonic-gate 	}
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	return (fss_maxglobpri);
718*7c478bd9Sstevel@tonic-gate }
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate /*
721*7c478bd9Sstevel@tonic-gate  * Calculate the new cpupri based on the usage, the number of shares and
722*7c478bd9Sstevel@tonic-gate  * the number of active threads.  Reset the tick counter for this thread.
723*7c478bd9Sstevel@tonic-gate  */
724*7c478bd9Sstevel@tonic-gate static void
725*7c478bd9Sstevel@tonic-gate fss_newpri(fssproc_t *fssproc)
726*7c478bd9Sstevel@tonic-gate {
727*7c478bd9Sstevel@tonic-gate 	kthread_t *tp;
728*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
729*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
730*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
731*7c478bd9Sstevel@tonic-gate 	fsspri_t fsspri, maxfsspri;
732*7c478bd9Sstevel@tonic-gate 	pri_t invpri;
733*7c478bd9Sstevel@tonic-gate 	uint32_t ticks;
734*7c478bd9Sstevel@tonic-gate 
735*7c478bd9Sstevel@tonic-gate 	tp = fssproc->fss_tp;
736*7c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 	if (tp->t_cid != fss_cid)
739*7c478bd9Sstevel@tonic-gate 		return;
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
744*7c478bd9Sstevel@tonic-gate 	fsszone = FSSPROJ2FSSZONE(fssproj);
745*7c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)
746*7c478bd9Sstevel@tonic-gate 		/*
747*7c478bd9Sstevel@tonic-gate 		 * No need to change priority of exited threads.
748*7c478bd9Sstevel@tonic-gate 		 */
749*7c478bd9Sstevel@tonic-gate 		return;
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
752*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	if (fssproj->fssp_shares == 0 || fsszone->fssz_rshares == 0) {
755*7c478bd9Sstevel@tonic-gate 		/*
756*7c478bd9Sstevel@tonic-gate 		 * Special case: threads with no shares.
757*7c478bd9Sstevel@tonic-gate 		 */
758*7c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = fss_minglobpri;
759*7c478bd9Sstevel@tonic-gate 		fssproc->fss_ticks = 0;
760*7c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
761*7c478bd9Sstevel@tonic-gate 		return;
762*7c478bd9Sstevel@tonic-gate 	}
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	/*
765*7c478bd9Sstevel@tonic-gate 	 * fsspri += shusage * nrunnable * ticks
766*7c478bd9Sstevel@tonic-gate 	 */
767*7c478bd9Sstevel@tonic-gate 	ticks = fssproc->fss_ticks;
768*7c478bd9Sstevel@tonic-gate 	fssproc->fss_ticks = 0;
769*7c478bd9Sstevel@tonic-gate 	fsspri = fssproc->fss_fsspri;
770*7c478bd9Sstevel@tonic-gate 	fsspri += fssproj->fssp_shusage * fssproj->fssp_runnable * ticks;
771*7c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = fsspri;
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate 	if (fsspri < fss_maxumdpri)
774*7c478bd9Sstevel@tonic-gate 		fsspri = fss_maxumdpri;	/* so that maxfsspri is != 0 */
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 	/*
777*7c478bd9Sstevel@tonic-gate 	 * The general priority formula:
778*7c478bd9Sstevel@tonic-gate 	 *
779*7c478bd9Sstevel@tonic-gate 	 *			(fsspri * umdprirange)
780*7c478bd9Sstevel@tonic-gate 	 *   pri = maxumdpri - ------------------------
781*7c478bd9Sstevel@tonic-gate 	 *				maxfsspri
782*7c478bd9Sstevel@tonic-gate 	 *
783*7c478bd9Sstevel@tonic-gate 	 * If this thread's fsspri is greater than the previous largest
784*7c478bd9Sstevel@tonic-gate 	 * fsspri, then record it as the new high and priority for this
785*7c478bd9Sstevel@tonic-gate 	 * thread will be one (the lowest priority assigned to a thread
786*7c478bd9Sstevel@tonic-gate 	 * that has non-zero shares).
787*7c478bd9Sstevel@tonic-gate 	 * Note that this formula cannot produce out of bounds priority
788*7c478bd9Sstevel@tonic-gate 	 * values; if it is changed, additional checks may need  to  be
789*7c478bd9Sstevel@tonic-gate 	 * added.
790*7c478bd9Sstevel@tonic-gate 	 */
791*7c478bd9Sstevel@tonic-gate 	maxfsspri = fsspset->fssps_maxfsspri;
792*7c478bd9Sstevel@tonic-gate 	if (fsspri >= maxfsspri) {
793*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_maxfsspri = fsspri;
794*7c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
795*7c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = 1;
796*7c478bd9Sstevel@tonic-gate 	} else {
797*7c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
798*7c478bd9Sstevel@tonic-gate 		invpri = (fsspri * (fss_maxumdpri - 1)) / maxfsspri;
799*7c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = fss_maxumdpri - invpri;
800*7c478bd9Sstevel@tonic-gate 	}
801*7c478bd9Sstevel@tonic-gate }
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate /*
804*7c478bd9Sstevel@tonic-gate  * Decays usages of all running projects and resets their tick counters.
805*7c478bd9Sstevel@tonic-gate  * Called once per second from fss_update() after updating priorities.
806*7c478bd9Sstevel@tonic-gate  */
807*7c478bd9Sstevel@tonic-gate static void
808*7c478bd9Sstevel@tonic-gate fss_decay_usage()
809*7c478bd9Sstevel@tonic-gate {
810*7c478bd9Sstevel@tonic-gate 	uint32_t zone_ext_shares, zone_int_shares;
811*7c478bd9Sstevel@tonic-gate 	uint32_t kpj_shares, pset_shares;
812*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
813*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
814*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
815*7c478bd9Sstevel@tonic-gate 	fsspri_t maxfsspri;
816*7c478bd9Sstevel@tonic-gate 	int psetid;
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
819*7c478bd9Sstevel@tonic-gate 	/*
820*7c478bd9Sstevel@tonic-gate 	 * Go through all active processor sets and decay usages of projects
821*7c478bd9Sstevel@tonic-gate 	 * running on them.
822*7c478bd9Sstevel@tonic-gate 	 */
823*7c478bd9Sstevel@tonic-gate 	for (psetid = 0; psetid < max_ncpus; psetid++) {
824*7c478bd9Sstevel@tonic-gate 		fsspset = &fsspsets[psetid];
825*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_cpupart == NULL ||
828*7c478bd9Sstevel@tonic-gate 		    (fssproj = fsspset->fssps_list) == NULL) {
829*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
830*7c478bd9Sstevel@tonic-gate 			continue;
831*7c478bd9Sstevel@tonic-gate 		}
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate 		/*
834*7c478bd9Sstevel@tonic-gate 		 * Decay maxfsspri for this cpu partition with the
835*7c478bd9Sstevel@tonic-gate 		 * fastest possible decay rate.
836*7c478bd9Sstevel@tonic-gate 		 */
837*7c478bd9Sstevel@tonic-gate 		disp_lock_enter(&fsspset->fssps_displock);
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate 		maxfsspri = (fsspset->fssps_maxfsspri *
840*7c478bd9Sstevel@tonic-gate 		    fss_nice_decay[NZERO]) / FSS_DECAY_BASE;
841*7c478bd9Sstevel@tonic-gate 		if (maxfsspri < fss_maxumdpri)
842*7c478bd9Sstevel@tonic-gate 			maxfsspri = fss_maxumdpri;
843*7c478bd9Sstevel@tonic-gate 		fsspset->fssps_maxfsspri = maxfsspri;
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 		do {
846*7c478bd9Sstevel@tonic-gate 			/*
847*7c478bd9Sstevel@tonic-gate 			 * Decay usage for each project running on
848*7c478bd9Sstevel@tonic-gate 			 * this cpu partition.
849*7c478bd9Sstevel@tonic-gate 			 */
850*7c478bd9Sstevel@tonic-gate 			fssproj->fssp_usage =
851*7c478bd9Sstevel@tonic-gate 			    (fssproj->fssp_usage * FSS_DECAY_USG) /
852*7c478bd9Sstevel@tonic-gate 			    FSS_DECAY_BASE + fssproj->fssp_ticks;
853*7c478bd9Sstevel@tonic-gate 			fssproj->fssp_ticks = 0;
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 			fsszone = fssproj->fssp_fsszone;
856*7c478bd9Sstevel@tonic-gate 			/*
857*7c478bd9Sstevel@tonic-gate 			 * Readjust the project's number of shares if it has
858*7c478bd9Sstevel@tonic-gate 			 * changed since we checked it last time.
859*7c478bd9Sstevel@tonic-gate 			 */
860*7c478bd9Sstevel@tonic-gate 			kpj_shares = fssproj->fssp_proj->kpj_shares;
861*7c478bd9Sstevel@tonic-gate 			if (fssproj->fssp_shares != kpj_shares) {
862*7c478bd9Sstevel@tonic-gate 				if (fssproj->fssp_runnable != 0) {
863*7c478bd9Sstevel@tonic-gate 					fsszone->fssz_shares -=
864*7c478bd9Sstevel@tonic-gate 					    fssproj->fssp_shares;
865*7c478bd9Sstevel@tonic-gate 					fsszone->fssz_shares += kpj_shares;
866*7c478bd9Sstevel@tonic-gate 				}
867*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shares = kpj_shares;
868*7c478bd9Sstevel@tonic-gate 			}
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate 			/*
871*7c478bd9Sstevel@tonic-gate 			 * Readjust the zone's number of shares if it
872*7c478bd9Sstevel@tonic-gate 			 * has changed since we checked it last time.
873*7c478bd9Sstevel@tonic-gate 			 */
874*7c478bd9Sstevel@tonic-gate 			zone_ext_shares = fsszone->fssz_zone->zone_shares;
875*7c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_rshares != zone_ext_shares) {
876*7c478bd9Sstevel@tonic-gate 				if (fsszone->fssz_runnable != 0) {
877*7c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
878*7c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
879*7c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares +=
880*7c478bd9Sstevel@tonic-gate 					    zone_ext_shares;
881*7c478bd9Sstevel@tonic-gate 				}
882*7c478bd9Sstevel@tonic-gate 				fsszone->fssz_rshares = zone_ext_shares;
883*7c478bd9Sstevel@tonic-gate 			}
884*7c478bd9Sstevel@tonic-gate 			zone_int_shares = fsszone->fssz_shares;
885*7c478bd9Sstevel@tonic-gate 			pset_shares = fsspset->fssps_shares;
886*7c478bd9Sstevel@tonic-gate 			/*
887*7c478bd9Sstevel@tonic-gate 			 * Calculate fssp_shusage value to be used
888*7c478bd9Sstevel@tonic-gate 			 * for fsspri increments for the next second.
889*7c478bd9Sstevel@tonic-gate 			 */
890*7c478bd9Sstevel@tonic-gate 			if (FSSPROJ2KPROJ(fssproj) == proj0p) {
891*7c478bd9Sstevel@tonic-gate 				/*
892*7c478bd9Sstevel@tonic-gate 				 * Project 0 in the global zone has 50%
893*7c478bd9Sstevel@tonic-gate 				 * of its zone.
894*7c478bd9Sstevel@tonic-gate 				 */
895*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = (fssproj->fssp_usage *
896*7c478bd9Sstevel@tonic-gate 				    zone_int_shares * zone_int_shares) /
897*7c478bd9Sstevel@tonic-gate 				    (zone_ext_shares * zone_ext_shares);
898*7c478bd9Sstevel@tonic-gate 			} else if (kpj_shares == 0 || zone_ext_shares == 0) {
899*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = 0;
900*7c478bd9Sstevel@tonic-gate 			} else {
901*7c478bd9Sstevel@tonic-gate 				/*
902*7c478bd9Sstevel@tonic-gate 				 * Thread's priority is based on its project's
903*7c478bd9Sstevel@tonic-gate 				 * normalized usage (shusage) value which gets
904*7c478bd9Sstevel@tonic-gate 				 * calculated this way:
905*7c478bd9Sstevel@tonic-gate 				 *
906*7c478bd9Sstevel@tonic-gate 				 *	   pset_shares^2    zone_int_shares^2
907*7c478bd9Sstevel@tonic-gate 				 * usage * ------------- * ------------------
908*7c478bd9Sstevel@tonic-gate 				 *	   kpj_shares^2	    zone_ext_shares^2
909*7c478bd9Sstevel@tonic-gate 				 *
910*7c478bd9Sstevel@tonic-gate 				 * Where zone_int_shares is the sum of shares
911*7c478bd9Sstevel@tonic-gate 				 * of all active projects within the zone (and
912*7c478bd9Sstevel@tonic-gate 				 * the pset), and zone_ext_shares is the number
913*7c478bd9Sstevel@tonic-gate 				 * of zone shares (ie, zone.cpu-shares).
914*7c478bd9Sstevel@tonic-gate 				 *
915*7c478bd9Sstevel@tonic-gate 				 * If there is only one zone active on the pset
916*7c478bd9Sstevel@tonic-gate 				 * the above reduces to:
917*7c478bd9Sstevel@tonic-gate 				 *
918*7c478bd9Sstevel@tonic-gate 				 * 			zone_int_shares^2
919*7c478bd9Sstevel@tonic-gate 				 * shusage = usage * ---------------------
920*7c478bd9Sstevel@tonic-gate 				 * 			kpj_shares^2
921*7c478bd9Sstevel@tonic-gate 				 *
922*7c478bd9Sstevel@tonic-gate 				 * If there's only one project active in the
923*7c478bd9Sstevel@tonic-gate 				 * zone this formula reduces to:
924*7c478bd9Sstevel@tonic-gate 				 *
925*7c478bd9Sstevel@tonic-gate 				 *			pset_shares^2
926*7c478bd9Sstevel@tonic-gate 				 * shusage = usage * ----------------------
927*7c478bd9Sstevel@tonic-gate 				 *			zone_ext_shares^2
928*7c478bd9Sstevel@tonic-gate 				 */
929*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = fssproj->fssp_usage *
930*7c478bd9Sstevel@tonic-gate 				    pset_shares * zone_int_shares;
931*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage /=
932*7c478bd9Sstevel@tonic-gate 				    kpj_shares * zone_ext_shares;
933*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage *=
934*7c478bd9Sstevel@tonic-gate 				    pset_shares * zone_int_shares;
935*7c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage /=
936*7c478bd9Sstevel@tonic-gate 				    kpj_shares * zone_ext_shares;
937*7c478bd9Sstevel@tonic-gate 			}
938*7c478bd9Sstevel@tonic-gate 			fssproj = fssproj->fssp_next;
939*7c478bd9Sstevel@tonic-gate 		} while (fssproj != fsspset->fssps_list);
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 		disp_lock_exit(&fsspset->fssps_displock);
942*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
943*7c478bd9Sstevel@tonic-gate 	}
944*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
945*7c478bd9Sstevel@tonic-gate }
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate static void
948*7c478bd9Sstevel@tonic-gate fss_change_priority(kthread_t *t, fssproc_t *fssproc)
949*7c478bd9Sstevel@tonic-gate {
950*7c478bd9Sstevel@tonic-gate 	pri_t new_pri;
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
953*7c478bd9Sstevel@tonic-gate 	new_pri = fssproc->fss_umdpri;
954*7c478bd9Sstevel@tonic-gate 	ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
955*7c478bd9Sstevel@tonic-gate 
956*7c478bd9Sstevel@tonic-gate 	if (t == curthread || t->t_state == TS_ONPROC) {
957*7c478bd9Sstevel@tonic-gate 		/*
958*7c478bd9Sstevel@tonic-gate 		 * curthread is always onproc
959*7c478bd9Sstevel@tonic-gate 		 */
960*7c478bd9Sstevel@tonic-gate 		cpu_t *cp = t->t_disp_queue->disp_cpu;
961*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, new_pri);
962*7c478bd9Sstevel@tonic-gate 		if (t == cp->cpu_dispthread)
963*7c478bd9Sstevel@tonic-gate 			cp->cpu_dispatch_pri = DISP_PRIO(t);
964*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t)) {
965*7c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
966*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
967*7c478bd9Sstevel@tonic-gate 		} else {
968*7c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
969*7c478bd9Sstevel@tonic-gate 		}
970*7c478bd9Sstevel@tonic-gate 	} else {
971*7c478bd9Sstevel@tonic-gate 		/*
972*7c478bd9Sstevel@tonic-gate 		 * When the priority of a thread is changed, it may be
973*7c478bd9Sstevel@tonic-gate 		 * necessary to adjust its position on a sleep queue or
974*7c478bd9Sstevel@tonic-gate 		 * dispatch queue.  The function thread_change_pri accomplishes
975*7c478bd9Sstevel@tonic-gate 		 * this.
976*7c478bd9Sstevel@tonic-gate 		 */
977*7c478bd9Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, 0)) {
978*7c478bd9Sstevel@tonic-gate 			/*
979*7c478bd9Sstevel@tonic-gate 			 * The thread was on a run queue.
980*7c478bd9Sstevel@tonic-gate 			 */
981*7c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
982*7c478bd9Sstevel@tonic-gate 		} else {
983*7c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
984*7c478bd9Sstevel@tonic-gate 		}
985*7c478bd9Sstevel@tonic-gate 	}
986*7c478bd9Sstevel@tonic-gate }
987*7c478bd9Sstevel@tonic-gate 
988*7c478bd9Sstevel@tonic-gate /*
989*7c478bd9Sstevel@tonic-gate  * Update priorities of all fair-sharing threads that are currently runnable
990*7c478bd9Sstevel@tonic-gate  * at a user mode priority based on the number of shares and current usage.
991*7c478bd9Sstevel@tonic-gate  * Called once per second via timeout which we reset here.
992*7c478bd9Sstevel@tonic-gate  *
993*7c478bd9Sstevel@tonic-gate  * There are several lists of fair-sharing threads broken up by a hash on the
994*7c478bd9Sstevel@tonic-gate  * thread pointer.  Each list has its own lock.  This avoids blocking all
995*7c478bd9Sstevel@tonic-gate  * fss_enterclass, fss_fork, and fss_exitclass operations while fss_update runs.
996*7c478bd9Sstevel@tonic-gate  * fss_update traverses each list in turn.
997*7c478bd9Sstevel@tonic-gate  */
998*7c478bd9Sstevel@tonic-gate static void
999*7c478bd9Sstevel@tonic-gate fss_update(void *arg)
1000*7c478bd9Sstevel@tonic-gate {
1001*7c478bd9Sstevel@tonic-gate 	int i;
1002*7c478bd9Sstevel@tonic-gate 	int new_marker = -1;
1003*7c478bd9Sstevel@tonic-gate 	static int fss_update_marker;
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 	/*
1006*7c478bd9Sstevel@tonic-gate 	 * Decay and update usages for all projects.
1007*7c478bd9Sstevel@tonic-gate 	 */
1008*7c478bd9Sstevel@tonic-gate 	fss_decay_usage();
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 	/*
1011*7c478bd9Sstevel@tonic-gate 	 * Start with the fss_update_marker list, then do the rest.
1012*7c478bd9Sstevel@tonic-gate 	 */
1013*7c478bd9Sstevel@tonic-gate 	i = fss_update_marker;
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	/*
1016*7c478bd9Sstevel@tonic-gate 	 * Go around all threads, set new priorities and decay
1017*7c478bd9Sstevel@tonic-gate 	 * per-thread CPU usages.
1018*7c478bd9Sstevel@tonic-gate 	 */
1019*7c478bd9Sstevel@tonic-gate 	do {
1020*7c478bd9Sstevel@tonic-gate 		/*
1021*7c478bd9Sstevel@tonic-gate 		 * If this is the first list after the current marker to have
1022*7c478bd9Sstevel@tonic-gate 		 * threads with priorities updates, advance the marker to this
1023*7c478bd9Sstevel@tonic-gate 		 * list for the next time fss_update runs.
1024*7c478bd9Sstevel@tonic-gate 		 */
1025*7c478bd9Sstevel@tonic-gate 		if (fss_update_list(i) &&
1026*7c478bd9Sstevel@tonic-gate 		    new_marker == -1 && i != fss_update_marker)
1027*7c478bd9Sstevel@tonic-gate 			new_marker = i;
1028*7c478bd9Sstevel@tonic-gate 	} while ((i = FSS_LIST_NEXT(i)) != fss_update_marker);
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 	/*
1031*7c478bd9Sstevel@tonic-gate 	 * Advance marker for the next fss_update call
1032*7c478bd9Sstevel@tonic-gate 	 */
1033*7c478bd9Sstevel@tonic-gate 	if (new_marker != -1)
1034*7c478bd9Sstevel@tonic-gate 		fss_update_marker = new_marker;
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 	(void) timeout(fss_update, arg, hz);
1037*7c478bd9Sstevel@tonic-gate }
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate /*
1040*7c478bd9Sstevel@tonic-gate  * Updates priority for a list of threads.  Returns 1 if the priority of one
1041*7c478bd9Sstevel@tonic-gate  * of the threads was actually updated, 0 if none were for various reasons
1042*7c478bd9Sstevel@tonic-gate  * (thread is no longer in the FSS class, is not runnable, has the preemption
1043*7c478bd9Sstevel@tonic-gate  * control no-preempt bit set, etc.)
1044*7c478bd9Sstevel@tonic-gate  */
1045*7c478bd9Sstevel@tonic-gate static int
1046*7c478bd9Sstevel@tonic-gate fss_update_list(int i)
1047*7c478bd9Sstevel@tonic-gate {
1048*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
1049*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
1050*7c478bd9Sstevel@tonic-gate 	fsspri_t fsspri;
1051*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
1052*7c478bd9Sstevel@tonic-gate 	int updated = 0;
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fss_listlock[i]);
1055*7c478bd9Sstevel@tonic-gate 	for (fssproc = fss_listhead[i].fss_next; fssproc != &fss_listhead[i];
1056*7c478bd9Sstevel@tonic-gate 	    fssproc = fssproc->fss_next) {
1057*7c478bd9Sstevel@tonic-gate 		t = fssproc->fss_tp;
1058*7c478bd9Sstevel@tonic-gate 		/*
1059*7c478bd9Sstevel@tonic-gate 		 * Lock the thread and verify the state.
1060*7c478bd9Sstevel@tonic-gate 		 */
1061*7c478bd9Sstevel@tonic-gate 		thread_lock(t);
1062*7c478bd9Sstevel@tonic-gate 		/*
1063*7c478bd9Sstevel@tonic-gate 		 * Skip the thread if it is no longer in the FSS class or
1064*7c478bd9Sstevel@tonic-gate 		 * is running with kernel mode priority.
1065*7c478bd9Sstevel@tonic-gate 		 */
1066*7c478bd9Sstevel@tonic-gate 		if (t->t_cid != fss_cid)
1067*7c478bd9Sstevel@tonic-gate 			goto next;
1068*7c478bd9Sstevel@tonic-gate 		if ((fssproc->fss_flags & FSSKPRI) != 0)
1069*7c478bd9Sstevel@tonic-gate 			goto next;
1070*7c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
1071*7c478bd9Sstevel@tonic-gate 		if (fssproj == NULL)
1072*7c478bd9Sstevel@tonic-gate 			goto next;
1073*7c478bd9Sstevel@tonic-gate 		if (fssproj->fssp_shares != 0) {
1074*7c478bd9Sstevel@tonic-gate 			/*
1075*7c478bd9Sstevel@tonic-gate 			 * Decay fsspri value.
1076*7c478bd9Sstevel@tonic-gate 			 */
1077*7c478bd9Sstevel@tonic-gate 			fsspri = fssproc->fss_fsspri;
1078*7c478bd9Sstevel@tonic-gate 			fsspri = (fsspri * fss_nice_decay[fssproc->fss_nice]) /
1079*7c478bd9Sstevel@tonic-gate 			    FSS_DECAY_BASE;
1080*7c478bd9Sstevel@tonic-gate 			fssproc->fss_fsspri = fsspri;
1081*7c478bd9Sstevel@tonic-gate 		}
1082*7c478bd9Sstevel@tonic-gate 
1083*7c478bd9Sstevel@tonic-gate 		if (t->t_schedctl && schedctl_get_nopreempt(t))
1084*7c478bd9Sstevel@tonic-gate 			goto next;
1085*7c478bd9Sstevel@tonic-gate 		if (t->t_state != TS_RUN) {
1086*7c478bd9Sstevel@tonic-gate 			/*
1087*7c478bd9Sstevel@tonic-gate 			 * Make next syscall/trap call fss_trapret
1088*7c478bd9Sstevel@tonic-gate 			 */
1089*7c478bd9Sstevel@tonic-gate 			t->t_trapret = 1;
1090*7c478bd9Sstevel@tonic-gate 			aston(t);
1091*7c478bd9Sstevel@tonic-gate 			goto next;
1092*7c478bd9Sstevel@tonic-gate 		}
1093*7c478bd9Sstevel@tonic-gate 		fss_newpri(fssproc);
1094*7c478bd9Sstevel@tonic-gate 		updated = 1;
1095*7c478bd9Sstevel@tonic-gate 
1096*7c478bd9Sstevel@tonic-gate 		/*
1097*7c478bd9Sstevel@tonic-gate 		 * Only dequeue the thread if it needs to be moved; otherwise
1098*7c478bd9Sstevel@tonic-gate 		 * it should just round-robin here.
1099*7c478bd9Sstevel@tonic-gate 		 */
1100*7c478bd9Sstevel@tonic-gate 		if (t->t_pri != fssproc->fss_umdpri)
1101*7c478bd9Sstevel@tonic-gate 			fss_change_priority(t, fssproc);
1102*7c478bd9Sstevel@tonic-gate next:
1103*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1104*7c478bd9Sstevel@tonic-gate 	}
1105*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fss_listlock[i]);
1106*7c478bd9Sstevel@tonic-gate 	return (updated);
1107*7c478bd9Sstevel@tonic-gate }
1108*7c478bd9Sstevel@tonic-gate 
1109*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1110*7c478bd9Sstevel@tonic-gate static int
1111*7c478bd9Sstevel@tonic-gate fss_admin(caddr_t uaddr, cred_t *reqpcredp)
1112*7c478bd9Sstevel@tonic-gate {
1113*7c478bd9Sstevel@tonic-gate 	fssadmin_t fssadmin;
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate 	if (copyin(uaddr, &fssadmin, sizeof (fssadmin_t)))
1116*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate 	switch (fssadmin.fss_cmd) {
1119*7c478bd9Sstevel@tonic-gate 	case FSS_SETADMIN:
1120*7c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
1121*7c478bd9Sstevel@tonic-gate 			return (EPERM);
1122*7c478bd9Sstevel@tonic-gate 		if (fssadmin.fss_quantum <= 0 || fssadmin.fss_quantum >= hz)
1123*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1124*7c478bd9Sstevel@tonic-gate 		fss_quantum = fssadmin.fss_quantum;
1125*7c478bd9Sstevel@tonic-gate 		break;
1126*7c478bd9Sstevel@tonic-gate 	case FSS_GETADMIN:
1127*7c478bd9Sstevel@tonic-gate 		fssadmin.fss_quantum = fss_quantum;
1128*7c478bd9Sstevel@tonic-gate 		if (copyout(&fssadmin, uaddr, sizeof (fssadmin_t)))
1129*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
1130*7c478bd9Sstevel@tonic-gate 		break;
1131*7c478bd9Sstevel@tonic-gate 	default:
1132*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1133*7c478bd9Sstevel@tonic-gate 	}
1134*7c478bd9Sstevel@tonic-gate 	return (0);
1135*7c478bd9Sstevel@tonic-gate }
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate static int
1138*7c478bd9Sstevel@tonic-gate fss_getclinfo(void *infop)
1139*7c478bd9Sstevel@tonic-gate {
1140*7c478bd9Sstevel@tonic-gate 	fssinfo_t *fssinfo = (fssinfo_t *)infop;
1141*7c478bd9Sstevel@tonic-gate 	fssinfo->fss_maxupri = fss_maxupri;
1142*7c478bd9Sstevel@tonic-gate 	return (0);
1143*7c478bd9Sstevel@tonic-gate }
1144*7c478bd9Sstevel@tonic-gate 
1145*7c478bd9Sstevel@tonic-gate static int
1146*7c478bd9Sstevel@tonic-gate fss_parmsin(void *parmsp)
1147*7c478bd9Sstevel@tonic-gate {
1148*7c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1149*7c478bd9Sstevel@tonic-gate 
1150*7c478bd9Sstevel@tonic-gate 	/*
1151*7c478bd9Sstevel@tonic-gate 	 * Check validity of parameters.
1152*7c478bd9Sstevel@tonic-gate 	 */
1153*7c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_uprilim > fss_maxupri ||
1154*7c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim < -fss_maxupri) &&
1155*7c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim != FSS_NOCHANGE)
1156*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_upri > fss_maxupri ||
1159*7c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri < -fss_maxupri) &&
1160*7c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri != FSS_NOCHANGE)
1161*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 	return (0);
1164*7c478bd9Sstevel@tonic-gate }
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1167*7c478bd9Sstevel@tonic-gate static int
1168*7c478bd9Sstevel@tonic-gate fss_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
1169*7c478bd9Sstevel@tonic-gate {
1170*7c478bd9Sstevel@tonic-gate 	return (0);
1171*7c478bd9Sstevel@tonic-gate }
1172*7c478bd9Sstevel@tonic-gate 
1173*7c478bd9Sstevel@tonic-gate static int
1174*7c478bd9Sstevel@tonic-gate fss_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
1175*7c478bd9Sstevel@tonic-gate {
1176*7c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1177*7c478bd9Sstevel@tonic-gate 	int priflag = 0;
1178*7c478bd9Sstevel@tonic-gate 	int limflag = 0;
1179*7c478bd9Sstevel@tonic-gate 	uint_t cnt;
1180*7c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate 	/*
1183*7c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE (-32768) is outside of the range of values for
1184*7c478bd9Sstevel@tonic-gate 	 * fss_uprilim and fss_upri.  If the structure fssparms_t is changed,
1185*7c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE should be replaced by a flag word.
1186*7c478bd9Sstevel@tonic-gate 	 */
1187*7c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = FSS_NOCHANGE;
1188*7c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = FSS_NOCHANGE;
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 	/*
1191*7c478bd9Sstevel@tonic-gate 	 * Get the varargs parameter and check validity of parameters.
1192*7c478bd9Sstevel@tonic-gate 	 */
1193*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1194*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1195*7c478bd9Sstevel@tonic-gate 
1196*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1197*7c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
1198*7c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
1199*7c478bd9Sstevel@tonic-gate 			if (limflag++)
1200*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1201*7c478bd9Sstevel@tonic-gate 			fssparmsp->fss_uprilim = (pri_t)vpp->pc_parm;
1202*7c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > fss_maxupri ||
1203*7c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_uprilim < -fss_maxupri)
1204*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1205*7c478bd9Sstevel@tonic-gate 			break;
1206*7c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
1207*7c478bd9Sstevel@tonic-gate 			if (priflag++)
1208*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1209*7c478bd9Sstevel@tonic-gate 			fssparmsp->fss_upri = (pri_t)vpp->pc_parm;
1210*7c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > fss_maxupri ||
1211*7c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_upri < -fss_maxupri)
1212*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1213*7c478bd9Sstevel@tonic-gate 			break;
1214*7c478bd9Sstevel@tonic-gate 		default:
1215*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1216*7c478bd9Sstevel@tonic-gate 		}
1217*7c478bd9Sstevel@tonic-gate 	}
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
1220*7c478bd9Sstevel@tonic-gate 		/*
1221*7c478bd9Sstevel@tonic-gate 		 * Use default parameters.
1222*7c478bd9Sstevel@tonic-gate 		 */
1223*7c478bd9Sstevel@tonic-gate 		fssparmsp->fss_upri = fssparmsp->fss_uprilim = 0;
1224*7c478bd9Sstevel@tonic-gate 	}
1225*7c478bd9Sstevel@tonic-gate 
1226*7c478bd9Sstevel@tonic-gate 	return (0);
1227*7c478bd9Sstevel@tonic-gate }
1228*7c478bd9Sstevel@tonic-gate 
1229*7c478bd9Sstevel@tonic-gate /*
1230*7c478bd9Sstevel@tonic-gate  * Copy all selected fair-sharing class parameters to the user.  The parameters
1231*7c478bd9Sstevel@tonic-gate  * are specified by a key.
1232*7c478bd9Sstevel@tonic-gate  */
1233*7c478bd9Sstevel@tonic-gate static int
1234*7c478bd9Sstevel@tonic-gate fss_vaparmsout(void *parmsp, pc_vaparms_t *vaparmsp)
1235*7c478bd9Sstevel@tonic-gate {
1236*7c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1237*7c478bd9Sstevel@tonic-gate 	int priflag = 0;
1238*7c478bd9Sstevel@tonic-gate 	int limflag = 0;
1239*7c478bd9Sstevel@tonic-gate 	uint_t cnt;
1240*7c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
1241*7c478bd9Sstevel@tonic-gate 
1242*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
1243*7c478bd9Sstevel@tonic-gate 
1244*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1245*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1248*7c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
1249*7c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
1250*7c478bd9Sstevel@tonic-gate 			if (limflag++)
1251*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1252*7c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_uprilim,
1253*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1254*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1255*7c478bd9Sstevel@tonic-gate 			break;
1256*7c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
1257*7c478bd9Sstevel@tonic-gate 			if (priflag++)
1258*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1259*7c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_upri,
1260*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1261*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1262*7c478bd9Sstevel@tonic-gate 			break;
1263*7c478bd9Sstevel@tonic-gate 		default:
1264*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1265*7c478bd9Sstevel@tonic-gate 		}
1266*7c478bd9Sstevel@tonic-gate 	}
1267*7c478bd9Sstevel@tonic-gate 
1268*7c478bd9Sstevel@tonic-gate 	return (0);
1269*7c478bd9Sstevel@tonic-gate }
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate static int
1272*7c478bd9Sstevel@tonic-gate fss_getclpri(pcpri_t *pcprip)
1273*7c478bd9Sstevel@tonic-gate {
1274*7c478bd9Sstevel@tonic-gate 	pcprip->pc_clpmax = fss_maxumdpri;
1275*7c478bd9Sstevel@tonic-gate 	pcprip->pc_clpmin = 0;
1276*7c478bd9Sstevel@tonic-gate 	return (0);
1277*7c478bd9Sstevel@tonic-gate }
1278*7c478bd9Sstevel@tonic-gate 
1279*7c478bd9Sstevel@tonic-gate static int
1280*7c478bd9Sstevel@tonic-gate fss_alloc(void **p, int flag)
1281*7c478bd9Sstevel@tonic-gate {
1282*7c478bd9Sstevel@tonic-gate 	void *bufp;
1283*7c478bd9Sstevel@tonic-gate 
1284*7c478bd9Sstevel@tonic-gate 	if ((bufp = kmem_zalloc(sizeof (fssproc_t), flag)) == NULL) {
1285*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
1286*7c478bd9Sstevel@tonic-gate 	} else {
1287*7c478bd9Sstevel@tonic-gate 		*p = bufp;
1288*7c478bd9Sstevel@tonic-gate 		return (0);
1289*7c478bd9Sstevel@tonic-gate 	}
1290*7c478bd9Sstevel@tonic-gate }
1291*7c478bd9Sstevel@tonic-gate 
1292*7c478bd9Sstevel@tonic-gate static void
1293*7c478bd9Sstevel@tonic-gate fss_free(void *bufp)
1294*7c478bd9Sstevel@tonic-gate {
1295*7c478bd9Sstevel@tonic-gate 	if (bufp)
1296*7c478bd9Sstevel@tonic-gate 		kmem_free(bufp, sizeof (fssproc_t));
1297*7c478bd9Sstevel@tonic-gate }
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate /*
1300*7c478bd9Sstevel@tonic-gate  * Thread functions
1301*7c478bd9Sstevel@tonic-gate  */
1302*7c478bd9Sstevel@tonic-gate static int
1303*7c478bd9Sstevel@tonic-gate fss_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
1304*7c478bd9Sstevel@tonic-gate     void *bufp)
1305*7c478bd9Sstevel@tonic-gate {
1306*7c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
1307*7c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc;
1308*7c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
1309*7c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
1310*7c478bd9Sstevel@tonic-gate 	static uint32_t fssexists = 0;
1311*7c478bd9Sstevel@tonic-gate 	fsspset_t	*fsspset;
1312*7c478bd9Sstevel@tonic-gate 	fssproj_t	*fssproj;
1313*7c478bd9Sstevel@tonic-gate 	fsszone_t	*fsszone;
1314*7c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
1315*7c478bd9Sstevel@tonic-gate 	zone_t		*zone;
1316*7c478bd9Sstevel@tonic-gate 	int		fsszone_allocated = 0;
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 	fssproc = (fssproc_t *)bufp;
1319*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproc != NULL);
1320*7c478bd9Sstevel@tonic-gate 
1321*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate 	/*
1324*7c478bd9Sstevel@tonic-gate 	 * Only root can move threads to FSS class.
1325*7c478bd9Sstevel@tonic-gate 	 */
1326*7c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
1327*7c478bd9Sstevel@tonic-gate 		return (EPERM);
1328*7c478bd9Sstevel@tonic-gate 	/*
1329*7c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc structure.
1330*7c478bd9Sstevel@tonic-gate 	 */
1331*7c478bd9Sstevel@tonic-gate 	fssproc->fss_umdpri = fss_maxumdpri / 2;
1332*7c478bd9Sstevel@tonic-gate 
1333*7c478bd9Sstevel@tonic-gate 	if (fssparmsp == NULL) {
1334*7c478bd9Sstevel@tonic-gate 		/*
1335*7c478bd9Sstevel@tonic-gate 		 * Use default values.
1336*7c478bd9Sstevel@tonic-gate 		 */
1337*7c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO;
1338*7c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = fssproc->fss_upri = 0;
1339*7c478bd9Sstevel@tonic-gate 	} else {
1340*7c478bd9Sstevel@tonic-gate 		/*
1341*7c478bd9Sstevel@tonic-gate 		 * Use supplied values.
1342*7c478bd9Sstevel@tonic-gate 		 */
1343*7c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_uprilim == FSS_NOCHANGE) {
1344*7c478bd9Sstevel@tonic-gate 			reqfssuprilim = 0;
1345*7c478bd9Sstevel@tonic-gate 		} else {
1346*7c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > 0 &&
1347*7c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
1348*7c478bd9Sstevel@tonic-gate 				return (EPERM);
1349*7c478bd9Sstevel@tonic-gate 			reqfssuprilim = fssparmsp->fss_uprilim;
1350*7c478bd9Sstevel@tonic-gate 		}
1351*7c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_upri == FSS_NOCHANGE) {
1352*7c478bd9Sstevel@tonic-gate 			reqfssupri = reqfssuprilim;
1353*7c478bd9Sstevel@tonic-gate 		} else {
1354*7c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > 0 &&
1355*7c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
1356*7c478bd9Sstevel@tonic-gate 				return (EPERM);
1357*7c478bd9Sstevel@tonic-gate 			/*
1358*7c478bd9Sstevel@tonic-gate 			 * Set the user priority to the requested value or
1359*7c478bd9Sstevel@tonic-gate 			 * the upri limit, whichever is lower.
1360*7c478bd9Sstevel@tonic-gate 			 */
1361*7c478bd9Sstevel@tonic-gate 			reqfssupri = fssparmsp->fss_upri;
1362*7c478bd9Sstevel@tonic-gate 			if (reqfssupri > reqfssuprilim)
1363*7c478bd9Sstevel@tonic-gate 				reqfssupri = reqfssuprilim;
1364*7c478bd9Sstevel@tonic-gate 		}
1365*7c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = reqfssuprilim;
1366*7c478bd9Sstevel@tonic-gate 		fssproc->fss_upri = reqfssupri;
1367*7c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO - (NZERO * reqfssupri) / fss_maxupri;
1368*7c478bd9Sstevel@tonic-gate 		if (fssproc->fss_nice > FSS_NICE_MAX)
1369*7c478bd9Sstevel@tonic-gate 			fssproc->fss_nice = FSS_NICE_MAX;
1370*7c478bd9Sstevel@tonic-gate 	}
1371*7c478bd9Sstevel@tonic-gate 
1372*7c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
1373*7c478bd9Sstevel@tonic-gate 	fssproc->fss_tp = t;
1374*7c478bd9Sstevel@tonic-gate 
1375*7c478bd9Sstevel@tonic-gate 	/*
1376*7c478bd9Sstevel@tonic-gate 	 * Put a lock on our fsspset structure.
1377*7c478bd9Sstevel@tonic-gate 	 */
1378*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
1379*7c478bd9Sstevel@tonic-gate 	fsspset = fss_find_fsspset(t->t_cpupart);
1380*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
1381*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
1382*7c478bd9Sstevel@tonic-gate 
1383*7c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
1384*7c478bd9Sstevel@tonic-gate 	if ((fsszone = fss_find_fsszone(fsspset, zone)) == NULL) {
1385*7c478bd9Sstevel@tonic-gate 		if ((fsszone = kmem_zalloc(sizeof (fsszone_t), KM_NOSLEEP))
1386*7c478bd9Sstevel@tonic-gate 		    == NULL) {
1387*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
1388*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
1389*7c478bd9Sstevel@tonic-gate 		} else {
1390*7c478bd9Sstevel@tonic-gate 			fsszone_allocated = 1;
1391*7c478bd9Sstevel@tonic-gate 			fss_insert_fsszone(fsspset, zone, fsszone);
1392*7c478bd9Sstevel@tonic-gate 		}
1393*7c478bd9Sstevel@tonic-gate 	}
1394*7c478bd9Sstevel@tonic-gate 	kpj = ttoproj(t);
1395*7c478bd9Sstevel@tonic-gate 	if ((fssproj = fss_find_fssproj(fsspset, kpj)) == NULL) {
1396*7c478bd9Sstevel@tonic-gate 		if ((fssproj = kmem_zalloc(sizeof (fssproj_t), KM_NOSLEEP))
1397*7c478bd9Sstevel@tonic-gate 		    == NULL) {
1398*7c478bd9Sstevel@tonic-gate 			if (fsszone_allocated) {
1399*7c478bd9Sstevel@tonic-gate 				fss_remove_fsszone(fsspset, fsszone);
1400*7c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
1401*7c478bd9Sstevel@tonic-gate 			}
1402*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
1403*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
1404*7c478bd9Sstevel@tonic-gate 		} else {
1405*7c478bd9Sstevel@tonic-gate 			fss_insert_fssproj(fsspset, kpj, fsszone, fssproj);
1406*7c478bd9Sstevel@tonic-gate 		}
1407*7c478bd9Sstevel@tonic-gate 	}
1408*7c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
1409*7c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj;
1410*7c478bd9Sstevel@tonic-gate 
1411*7c478bd9Sstevel@tonic-gate 	/*
1412*7c478bd9Sstevel@tonic-gate 	 * Reset priority. Process goes to a "user mode" priority here
1413*7c478bd9Sstevel@tonic-gate 	 * regardless of whether or not it has slept since entering the kernel.
1414*7c478bd9Sstevel@tonic-gate 	 */
1415*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1416*7c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
1417*7c478bd9Sstevel@tonic-gate 	t->t_cid = cid;
1418*7c478bd9Sstevel@tonic-gate 	t->t_cldata = (void *)fssproc;
1419*7c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_RUNQMATCH;
1420*7c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
1421*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC)
1422*7c478bd9Sstevel@tonic-gate 		fss_active(t);
1423*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1424*7c478bd9Sstevel@tonic-gate 
1425*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
1426*7c478bd9Sstevel@tonic-gate 
1427*7c478bd9Sstevel@tonic-gate 	/*
1428*7c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc list.
1429*7c478bd9Sstevel@tonic-gate 	 */
1430*7c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(fssproc);
1431*7c478bd9Sstevel@tonic-gate 
1432*7c478bd9Sstevel@tonic-gate 	/*
1433*7c478bd9Sstevel@tonic-gate 	 * If this is the first fair-sharing thread to occur since boot,
1434*7c478bd9Sstevel@tonic-gate 	 * we set up the initial call to fss_update() here. Use an atomic
1435*7c478bd9Sstevel@tonic-gate 	 * compare-and-swap since that's easier and faster than a mutex
1436*7c478bd9Sstevel@tonic-gate 	 * (but check with an ordinary load first since most of the time
1437*7c478bd9Sstevel@tonic-gate 	 * this will already be done).
1438*7c478bd9Sstevel@tonic-gate 	 */
1439*7c478bd9Sstevel@tonic-gate 	if (fssexists == 0 && cas32(&fssexists, 0, 1) == 0)
1440*7c478bd9Sstevel@tonic-gate 		(void) timeout(fss_update, NULL, hz);
1441*7c478bd9Sstevel@tonic-gate 
1442*7c478bd9Sstevel@tonic-gate 	return (0);
1443*7c478bd9Sstevel@tonic-gate }
1444*7c478bd9Sstevel@tonic-gate 
1445*7c478bd9Sstevel@tonic-gate /*
1446*7c478bd9Sstevel@tonic-gate  * Remove fssproc_t from the list.
1447*7c478bd9Sstevel@tonic-gate  */
1448*7c478bd9Sstevel@tonic-gate static void
1449*7c478bd9Sstevel@tonic-gate fss_exitclass(void *procp)
1450*7c478bd9Sstevel@tonic-gate {
1451*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = (fssproc_t *)procp;
1452*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
1453*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
1454*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
1455*7c478bd9Sstevel@tonic-gate 	kthread_t *t = fssproc->fss_tp;
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate 	/*
1458*7c478bd9Sstevel@tonic-gate 	 * We should be either getting this thread off the deathrow or
1459*7c478bd9Sstevel@tonic-gate 	 * this thread has already moved to another scheduling class and
1460*7c478bd9Sstevel@tonic-gate 	 * we're being called with its old cldata buffer pointer.  In both
1461*7c478bd9Sstevel@tonic-gate 	 * cases, the content of this buffer can not be changed while we're
1462*7c478bd9Sstevel@tonic-gate 	 * here.
1463*7c478bd9Sstevel@tonic-gate 	 */
1464*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
1465*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1466*7c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid) {
1467*7c478bd9Sstevel@tonic-gate 		/*
1468*7c478bd9Sstevel@tonic-gate 		 * We're being called as a result of the priocntl() system
1469*7c478bd9Sstevel@tonic-gate 		 * call -- someone is trying to move our thread to another
1470*7c478bd9Sstevel@tonic-gate 		 * scheduling class. We can't call fss_inactive() here
1471*7c478bd9Sstevel@tonic-gate 		 * because our thread's t_cldata pointer already points
1472*7c478bd9Sstevel@tonic-gate 		 * to another scheduling class specific data.
1473*7c478bd9Sstevel@tonic-gate 		 */
1474*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1475*7c478bd9Sstevel@tonic-gate 
1476*7c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
1477*7c478bd9Sstevel@tonic-gate 		fsspset = FSSPROJ2FSSPSET(fssproj);
1478*7c478bd9Sstevel@tonic-gate 		fsszone = fssproj->fssp_fsszone;
1479*7c478bd9Sstevel@tonic-gate 
1480*7c478bd9Sstevel@tonic-gate 		if (fssproc->fss_runnable) {
1481*7c478bd9Sstevel@tonic-gate 			disp_lock_enter_high(&fsspset->fssps_displock);
1482*7c478bd9Sstevel@tonic-gate 			if (--fssproj->fssp_runnable == 0) {
1483*7c478bd9Sstevel@tonic-gate 				fsszone->fssz_shares -= fssproj->fssp_shares;
1484*7c478bd9Sstevel@tonic-gate 				if (--fsszone->fssz_runnable == 0)
1485*7c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
1486*7c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
1487*7c478bd9Sstevel@tonic-gate 			}
1488*7c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(&fsspset->fssps_displock);
1489*7c478bd9Sstevel@tonic-gate 		}
1490*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1491*7c478bd9Sstevel@tonic-gate 
1492*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
1493*7c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_threads == 0) {
1494*7c478bd9Sstevel@tonic-gate 			fss_remove_fssproj(fsspset, fssproj);
1495*7c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_nproj == 0)
1496*7c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
1497*7c478bd9Sstevel@tonic-gate 			kmem_free(fssproj, sizeof (fssproj_t));
1498*7c478bd9Sstevel@tonic-gate 		}
1499*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
1500*7c478bd9Sstevel@tonic-gate 
1501*7c478bd9Sstevel@tonic-gate 	} else {
1502*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_state == TS_FREE);
1503*7c478bd9Sstevel@tonic-gate 		/*
1504*7c478bd9Sstevel@tonic-gate 		 * We're being called from thread_free() when our thread
1505*7c478bd9Sstevel@tonic-gate 		 * is removed from the deathrow. There is nothing we need
1506*7c478bd9Sstevel@tonic-gate 		 * do here since everything should've been done earlier
1507*7c478bd9Sstevel@tonic-gate 		 * in fss_exit().
1508*7c478bd9Sstevel@tonic-gate 		 */
1509*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1510*7c478bd9Sstevel@tonic-gate 	}
1511*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
1512*7c478bd9Sstevel@tonic-gate 
1513*7c478bd9Sstevel@tonic-gate 	FSS_LIST_DELETE(fssproc);
1514*7c478bd9Sstevel@tonic-gate 	fss_free(fssproc);
1515*7c478bd9Sstevel@tonic-gate }
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1518*7c478bd9Sstevel@tonic-gate static int
1519*7c478bd9Sstevel@tonic-gate fss_canexit(kthread_t *t, cred_t *credp)
1520*7c478bd9Sstevel@tonic-gate {
1521*7c478bd9Sstevel@tonic-gate 	/*
1522*7c478bd9Sstevel@tonic-gate 	 * A thread is allowed to exit FSS only if we have sufficient
1523*7c478bd9Sstevel@tonic-gate 	 * privileges.
1524*7c478bd9Sstevel@tonic-gate 	 */
1525*7c478bd9Sstevel@tonic-gate 	if (credp != NULL && secpolicy_setpriority(credp) != 0)
1526*7c478bd9Sstevel@tonic-gate 		return (EPERM);
1527*7c478bd9Sstevel@tonic-gate 	else
1528*7c478bd9Sstevel@tonic-gate 		return (0);
1529*7c478bd9Sstevel@tonic-gate }
1530*7c478bd9Sstevel@tonic-gate 
1531*7c478bd9Sstevel@tonic-gate /*
1532*7c478bd9Sstevel@tonic-gate  * Initialize fair-share class specific proc structure for a child.
1533*7c478bd9Sstevel@tonic-gate  */
1534*7c478bd9Sstevel@tonic-gate static int
1535*7c478bd9Sstevel@tonic-gate fss_fork(kthread_t *pt, kthread_t *ct, void *bufp)
1536*7c478bd9Sstevel@tonic-gate {
1537*7c478bd9Sstevel@tonic-gate 	fssproc_t *pfssproc;	/* ptr to parent's fssproc structure	*/
1538*7c478bd9Sstevel@tonic-gate 	fssproc_t *cfssproc;	/* ptr to child's fssproc structure	*/
1539*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
1540*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
1541*7c478bd9Sstevel@tonic-gate 
1542*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(pt)->p_lock));
1543*7c478bd9Sstevel@tonic-gate 	ASSERT(ct->t_state == TS_STOPPED);
1544*7c478bd9Sstevel@tonic-gate 
1545*7c478bd9Sstevel@tonic-gate 	cfssproc = (fssproc_t *)bufp;
1546*7c478bd9Sstevel@tonic-gate 	ASSERT(cfssproc != NULL);
1547*7c478bd9Sstevel@tonic-gate 	bzero(cfssproc, sizeof (fssproc_t));
1548*7c478bd9Sstevel@tonic-gate 
1549*7c478bd9Sstevel@tonic-gate 	thread_lock(pt);
1550*7c478bd9Sstevel@tonic-gate 	pfssproc = FSSPROC(pt);
1551*7c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(pfssproc);
1552*7c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
1553*7c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
1554*7c478bd9Sstevel@tonic-gate 
1555*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
1556*7c478bd9Sstevel@tonic-gate 	/*
1557*7c478bd9Sstevel@tonic-gate 	 * Initialize child's fssproc structure.
1558*7c478bd9Sstevel@tonic-gate 	 */
1559*7c478bd9Sstevel@tonic-gate 	thread_lock(pt);
1560*7c478bd9Sstevel@tonic-gate 	ASSERT(FSSPROJ(pt) == fssproj);
1561*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_proj = fssproj;
1562*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_timeleft = fss_quantum;
1563*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_umdpri = pfssproc->fss_umdpri;
1564*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_fsspri = 0;
1565*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_uprilim = pfssproc->fss_uprilim;
1566*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_upri = pfssproc->fss_upri;
1567*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_tp = ct;
1568*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_nice = pfssproc->fss_nice;
1569*7c478bd9Sstevel@tonic-gate 	cfssproc->fss_flags = pfssproc->fss_flags & ~(FSSKPRI | FSSBACKQ);
1570*7c478bd9Sstevel@tonic-gate 	ct->t_cldata = (void *)cfssproc;
1571*7c478bd9Sstevel@tonic-gate 	ct->t_schedflag |= TS_RUNQMATCH;
1572*7c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
1573*7c478bd9Sstevel@tonic-gate 
1574*7c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
1575*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
1576*7c478bd9Sstevel@tonic-gate 
1577*7c478bd9Sstevel@tonic-gate 	/*
1578*7c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc hash table.
1579*7c478bd9Sstevel@tonic-gate 	 */
1580*7c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(cfssproc);
1581*7c478bd9Sstevel@tonic-gate 	return (0);
1582*7c478bd9Sstevel@tonic-gate }
1583*7c478bd9Sstevel@tonic-gate 
1584*7c478bd9Sstevel@tonic-gate /*
1585*7c478bd9Sstevel@tonic-gate  * Child is placed at back of dispatcher queue and parent gives up processor
1586*7c478bd9Sstevel@tonic-gate  * so that the child runs first after the fork. This allows the child
1587*7c478bd9Sstevel@tonic-gate  * immediately execing to break the multiple use of copy on write pages with no
1588*7c478bd9Sstevel@tonic-gate  * disk home. The parent will get to steal them back rather than uselessly
1589*7c478bd9Sstevel@tonic-gate  * copying them.
1590*7c478bd9Sstevel@tonic-gate  */
1591*7c478bd9Sstevel@tonic-gate static void
1592*7c478bd9Sstevel@tonic-gate fss_forkret(kthread_t *t, kthread_t *ct)
1593*7c478bd9Sstevel@tonic-gate {
1594*7c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
1595*7c478bd9Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
1596*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
1597*7c478bd9Sstevel@tonic-gate 
1598*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1599*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1600*7c478bd9Sstevel@tonic-gate 
1601*7c478bd9Sstevel@tonic-gate 	/*
1602*7c478bd9Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure the
1603*7c478bd9Sstevel@tonic-gate 	 * process does not disappear before we set it running.
1604*7c478bd9Sstevel@tonic-gate 	 */
1605*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
1606*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1607*7c478bd9Sstevel@tonic-gate 	continuelwps(cp);
1608*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
1609*7c478bd9Sstevel@tonic-gate 
1610*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1611*7c478bd9Sstevel@tonic-gate 	continuelwps(pp);
1612*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
1613*7c478bd9Sstevel@tonic-gate 
1614*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1615*7c478bd9Sstevel@tonic-gate 
1616*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
1617*7c478bd9Sstevel@tonic-gate 	fss_newpri(fssproc);
1618*7c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
1619*7c478bd9Sstevel@tonic-gate 	t->t_pri = fssproc->fss_umdpri;
1620*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
1621*7c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSKPRI;
1622*7c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate 	/*
1625*7c478bd9Sstevel@tonic-gate 	 * We don't want to call fss_setrun(t) here because it may call
1626*7c478bd9Sstevel@tonic-gate 	 * fss_active, which we don't need.
1627*7c478bd9Sstevel@tonic-gate 	 */
1628*7c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
1629*7c478bd9Sstevel@tonic-gate 
1630*7c478bd9Sstevel@tonic-gate 	if (t->t_disp_time != lbolt)
1631*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
1632*7c478bd9Sstevel@tonic-gate 	else
1633*7c478bd9Sstevel@tonic-gate 		setfrontdq(t);
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1636*7c478bd9Sstevel@tonic-gate 
1637*7c478bd9Sstevel@tonic-gate 	swtch();
1638*7c478bd9Sstevel@tonic-gate }
1639*7c478bd9Sstevel@tonic-gate 
1640*7c478bd9Sstevel@tonic-gate /*
1641*7c478bd9Sstevel@tonic-gate  * Get the fair-sharing parameters of the thread pointed to by fssprocp into
1642*7c478bd9Sstevel@tonic-gate  * the buffer pointed by fssparmsp.
1643*7c478bd9Sstevel@tonic-gate  */
1644*7c478bd9Sstevel@tonic-gate static void
1645*7c478bd9Sstevel@tonic-gate fss_parmsget(kthread_t *t, void *parmsp)
1646*7c478bd9Sstevel@tonic-gate {
1647*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
1648*7c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1649*7c478bd9Sstevel@tonic-gate 
1650*7c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = fssproc->fss_uprilim;
1651*7c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = fssproc->fss_upri;
1652*7c478bd9Sstevel@tonic-gate }
1653*7c478bd9Sstevel@tonic-gate 
1654*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1655*7c478bd9Sstevel@tonic-gate static int
1656*7c478bd9Sstevel@tonic-gate fss_parmsset(kthread_t *t, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1657*7c478bd9Sstevel@tonic-gate {
1658*7c478bd9Sstevel@tonic-gate 	char		nice;
1659*7c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
1660*7c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
1661*7c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc = FSSPROC(t);
1662*7c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
1663*7c478bd9Sstevel@tonic-gate 
1664*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1665*7c478bd9Sstevel@tonic-gate 
1666*7c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_uprilim == FSS_NOCHANGE)
1667*7c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssproc->fss_uprilim;
1668*7c478bd9Sstevel@tonic-gate 	else
1669*7c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssparmsp->fss_uprilim;
1670*7c478bd9Sstevel@tonic-gate 
1671*7c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_upri == FSS_NOCHANGE)
1672*7c478bd9Sstevel@tonic-gate 		reqfssupri = fssproc->fss_upri;
1673*7c478bd9Sstevel@tonic-gate 	else
1674*7c478bd9Sstevel@tonic-gate 		reqfssupri = fssparmsp->fss_upri;
1675*7c478bd9Sstevel@tonic-gate 
1676*7c478bd9Sstevel@tonic-gate 	/*
1677*7c478bd9Sstevel@tonic-gate 	 * Make sure the user priority doesn't exceed the upri limit.
1678*7c478bd9Sstevel@tonic-gate 	 */
1679*7c478bd9Sstevel@tonic-gate 	if (reqfssupri > reqfssuprilim)
1680*7c478bd9Sstevel@tonic-gate 		reqfssupri = reqfssuprilim;
1681*7c478bd9Sstevel@tonic-gate 
1682*7c478bd9Sstevel@tonic-gate 	/*
1683*7c478bd9Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code for all classes
1684*7c478bd9Sstevel@tonic-gate 	 * require that a thread attempting to change the scheduling parameters
1685*7c478bd9Sstevel@tonic-gate 	 * of a target thread be privileged or have a real or effective UID
1686*7c478bd9Sstevel@tonic-gate 	 * matching that of the target thread. We are not called unless these
1687*7c478bd9Sstevel@tonic-gate 	 * basic permission checks have already passed. The fair-sharing class
1688*7c478bd9Sstevel@tonic-gate 	 * requires in addition that the calling thread be privileged if it
1689*7c478bd9Sstevel@tonic-gate 	 * is attempting to raise the upri limit above its current value.
1690*7c478bd9Sstevel@tonic-gate 	 * This may have been checked previously but if our caller passed us
1691*7c478bd9Sstevel@tonic-gate 	 * a non-NULL credential pointer we assume it hasn't and we check it
1692*7c478bd9Sstevel@tonic-gate 	 * here.
1693*7c478bd9Sstevel@tonic-gate 	 */
1694*7c478bd9Sstevel@tonic-gate 	if ((reqpcredp != NULL) &&
1695*7c478bd9Sstevel@tonic-gate 	    (reqfssuprilim > fssproc->fss_uprilim) &&
1696*7c478bd9Sstevel@tonic-gate 	    secpolicy_setpriority(reqpcredp) != 0)
1697*7c478bd9Sstevel@tonic-gate 		return (EPERM);
1698*7c478bd9Sstevel@tonic-gate 
1699*7c478bd9Sstevel@tonic-gate 	/*
1700*7c478bd9Sstevel@tonic-gate 	 * Set fss_nice to the nice value corresponding to the user priority we
1701*7c478bd9Sstevel@tonic-gate 	 * are setting.  Note that setting the nice field of the parameter
1702*7c478bd9Sstevel@tonic-gate 	 * struct won't affect upri or nice.
1703*7c478bd9Sstevel@tonic-gate 	 */
1704*7c478bd9Sstevel@tonic-gate 	nice = NZERO - (reqfssupri * NZERO) / fss_maxupri;
1705*7c478bd9Sstevel@tonic-gate 	if (nice > FSS_NICE_MAX)
1706*7c478bd9Sstevel@tonic-gate 		nice = FSS_NICE_MAX;
1707*7c478bd9Sstevel@tonic-gate 
1708*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1709*7c478bd9Sstevel@tonic-gate 
1710*7c478bd9Sstevel@tonic-gate 	fssproc->fss_uprilim = reqfssuprilim;
1711*7c478bd9Sstevel@tonic-gate 	fssproc->fss_upri = reqfssupri;
1712*7c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = nice;
1713*7c478bd9Sstevel@tonic-gate 	fss_newpri(fssproc);
1714*7c478bd9Sstevel@tonic-gate 
1715*7c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) != 0) {
1716*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1717*7c478bd9Sstevel@tonic-gate 		return (0);
1718*7c478bd9Sstevel@tonic-gate 	}
1719*7c478bd9Sstevel@tonic-gate 
1720*7c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
1721*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1722*7c478bd9Sstevel@tonic-gate 	return (0);
1723*7c478bd9Sstevel@tonic-gate 
1724*7c478bd9Sstevel@tonic-gate }
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate /*
1727*7c478bd9Sstevel@tonic-gate  * The thread is being stopped.
1728*7c478bd9Sstevel@tonic-gate  */
1729*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1730*7c478bd9Sstevel@tonic-gate static void
1731*7c478bd9Sstevel@tonic-gate fss_stop(kthread_t *t, int why, int what)
1732*7c478bd9Sstevel@tonic-gate {
1733*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1734*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1735*7c478bd9Sstevel@tonic-gate 
1736*7c478bd9Sstevel@tonic-gate 	fss_inactive(t);
1737*7c478bd9Sstevel@tonic-gate }
1738*7c478bd9Sstevel@tonic-gate 
1739*7c478bd9Sstevel@tonic-gate /*
1740*7c478bd9Sstevel@tonic-gate  * The current thread is exiting, do necessary adjustments to its project
1741*7c478bd9Sstevel@tonic-gate  */
1742*7c478bd9Sstevel@tonic-gate static void
1743*7c478bd9Sstevel@tonic-gate fss_exit(kthread_t *t)
1744*7c478bd9Sstevel@tonic-gate {
1745*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
1746*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
1747*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
1748*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
1749*7c478bd9Sstevel@tonic-gate 	int free = 0;
1750*7c478bd9Sstevel@tonic-gate 
1751*7c478bd9Sstevel@tonic-gate 	/*
1752*7c478bd9Sstevel@tonic-gate 	 * Thread t here is either a current thread (in which case we hold
1753*7c478bd9Sstevel@tonic-gate 	 * its process' p_lock), or a thread being destroyed by forklwp_fail(),
1754*7c478bd9Sstevel@tonic-gate 	 * in which case we hold pidlock and thread is no longer on the
1755*7c478bd9Sstevel@tonic-gate 	 * thread list.
1756*7c478bd9Sstevel@tonic-gate 	 */
1757*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock) || MUTEX_HELD(&pidlock));
1758*7c478bd9Sstevel@tonic-gate 
1759*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
1760*7c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
1761*7c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
1762*7c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
1765*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
1766*7c478bd9Sstevel@tonic-gate 
1767*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1768*7c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
1769*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
1770*7c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_runnable == 0) {
1771*7c478bd9Sstevel@tonic-gate 			fsszone->fssz_shares -= fssproj->fssp_shares;
1772*7c478bd9Sstevel@tonic-gate 			if (--fsszone->fssz_runnable == 0)
1773*7c478bd9Sstevel@tonic-gate 				fsspset->fssps_shares -= fsszone->fssz_rshares;
1774*7c478bd9Sstevel@tonic-gate 		}
1775*7c478bd9Sstevel@tonic-gate 		ASSERT(fssproc->fss_runnable == 1);
1776*7c478bd9Sstevel@tonic-gate 		fssproc->fss_runnable = 0;
1777*7c478bd9Sstevel@tonic-gate 	}
1778*7c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_threads == 0) {
1779*7c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj);
1780*7c478bd9Sstevel@tonic-gate 		free = 1;
1781*7c478bd9Sstevel@tonic-gate 	}
1782*7c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
1783*7c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = NULL;	/* mark this thread as already exited */
1784*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1785*7c478bd9Sstevel@tonic-gate 
1786*7c478bd9Sstevel@tonic-gate 	if (free) {
1787*7c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
1788*7c478bd9Sstevel@tonic-gate 			kmem_free(fsszone, sizeof (fsszone_t));
1789*7c478bd9Sstevel@tonic-gate 		kmem_free(fssproj, sizeof (fssproj_t));
1790*7c478bd9Sstevel@tonic-gate 	}
1791*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
1792*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
1793*7c478bd9Sstevel@tonic-gate }
1794*7c478bd9Sstevel@tonic-gate 
1795*7c478bd9Sstevel@tonic-gate static void
1796*7c478bd9Sstevel@tonic-gate fss_nullsys()
1797*7c478bd9Sstevel@tonic-gate {
1798*7c478bd9Sstevel@tonic-gate }
1799*7c478bd9Sstevel@tonic-gate 
1800*7c478bd9Sstevel@tonic-gate /*
1801*7c478bd9Sstevel@tonic-gate  * fss_swapin() returns -1 if the thread is loaded or is not eligible to be
1802*7c478bd9Sstevel@tonic-gate  * swapped in. Otherwise, it returns the thread's effective priority based
1803*7c478bd9Sstevel@tonic-gate  * on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
1804*7c478bd9Sstevel@tonic-gate  */
1805*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1806*7c478bd9Sstevel@tonic-gate static pri_t
1807*7c478bd9Sstevel@tonic-gate fss_swapin(kthread_t *t, int flags)
1808*7c478bd9Sstevel@tonic-gate {
1809*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
1810*7c478bd9Sstevel@tonic-gate 	long epri = -1;
1811*7c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
1812*7c478bd9Sstevel@tonic-gate 
1813*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1814*7c478bd9Sstevel@tonic-gate 
1815*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
1816*7c478bd9Sstevel@tonic-gate 		time_t swapout_time;
1817*7c478bd9Sstevel@tonic-gate 
1818*7c478bd9Sstevel@tonic-gate 		swapout_time = (lbolt - t->t_stime) / hz;
1819*7c478bd9Sstevel@tonic-gate 		if (INHERITED(t) || (fssproc->fss_flags & FSSKPRI)) {
1820*7c478bd9Sstevel@tonic-gate 			epri = (long)DISP_PRIO(t) + swapout_time;
1821*7c478bd9Sstevel@tonic-gate 		} else {
1822*7c478bd9Sstevel@tonic-gate 			/*
1823*7c478bd9Sstevel@tonic-gate 			 * Threads which have been out for a long time,
1824*7c478bd9Sstevel@tonic-gate 			 * have high user mode priority and are associated
1825*7c478bd9Sstevel@tonic-gate 			 * with a small address space are more deserving.
1826*7c478bd9Sstevel@tonic-gate 			 */
1827*7c478bd9Sstevel@tonic-gate 			epri = fssproc->fss_umdpri;
1828*7c478bd9Sstevel@tonic-gate 			ASSERT(epri >= 0 && epri <= fss_maxumdpri);
1829*7c478bd9Sstevel@tonic-gate 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
1830*7c478bd9Sstevel@tonic-gate 		}
1831*7c478bd9Sstevel@tonic-gate 		/*
1832*7c478bd9Sstevel@tonic-gate 		 * Scale epri so that SHRT_MAX / 2 represents zero priority.
1833*7c478bd9Sstevel@tonic-gate 		 */
1834*7c478bd9Sstevel@tonic-gate 		epri += SHRT_MAX / 2;
1835*7c478bd9Sstevel@tonic-gate 		if (epri < 0)
1836*7c478bd9Sstevel@tonic-gate 			epri = 0;
1837*7c478bd9Sstevel@tonic-gate 		else if (epri > SHRT_MAX)
1838*7c478bd9Sstevel@tonic-gate 			epri = SHRT_MAX;
1839*7c478bd9Sstevel@tonic-gate 	}
1840*7c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
1841*7c478bd9Sstevel@tonic-gate }
1842*7c478bd9Sstevel@tonic-gate 
1843*7c478bd9Sstevel@tonic-gate /*
1844*7c478bd9Sstevel@tonic-gate  * fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
1845*7c478bd9Sstevel@tonic-gate  * be swapped out. Otherwise, it returns the thread's effective priority
1846*7c478bd9Sstevel@tonic-gate  * based on if the swapper is in softswap or hardswap mode.
1847*7c478bd9Sstevel@tonic-gate  */
1848*7c478bd9Sstevel@tonic-gate static pri_t
1849*7c478bd9Sstevel@tonic-gate fss_swapout(kthread_t *t, int flags)
1850*7c478bd9Sstevel@tonic-gate {
1851*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
1852*7c478bd9Sstevel@tonic-gate 	long epri = -1;
1853*7c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
1854*7c478bd9Sstevel@tonic-gate 	time_t swapin_time;
1855*7c478bd9Sstevel@tonic-gate 
1856*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1857*7c478bd9Sstevel@tonic-gate 
1858*7c478bd9Sstevel@tonic-gate 	if (INHERITED(t) ||
1859*7c478bd9Sstevel@tonic-gate 	    (fssproc->fss_flags & FSSKPRI) ||
1860*7c478bd9Sstevel@tonic-gate 	    (t->t_proc_flag & TP_LWPEXIT) ||
1861*7c478bd9Sstevel@tonic-gate 	    (t->t_state & (TS_ZOMB | TS_FREE | TS_STOPPED | TS_ONPROC)) ||
1862*7c478bd9Sstevel@tonic-gate 	    !(t->t_schedflag & TS_LOAD) ||
1863*7c478bd9Sstevel@tonic-gate 	    !(SWAP_OK(t)))
1864*7c478bd9Sstevel@tonic-gate 		return (-1);
1865*7c478bd9Sstevel@tonic-gate 
1866*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
1867*7c478bd9Sstevel@tonic-gate 
1868*7c478bd9Sstevel@tonic-gate 	swapin_time = (lbolt - t->t_stime) / hz;
1869*7c478bd9Sstevel@tonic-gate 
1870*7c478bd9Sstevel@tonic-gate 	if (flags == SOFTSWAP) {
1871*7c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
1872*7c478bd9Sstevel@tonic-gate 			epri = 0;
1873*7c478bd9Sstevel@tonic-gate 		} else {
1874*7c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
1875*7c478bd9Sstevel@tonic-gate 		}
1876*7c478bd9Sstevel@tonic-gate 	} else {
1877*7c478bd9Sstevel@tonic-gate 		pri_t pri;
1878*7c478bd9Sstevel@tonic-gate 
1879*7c478bd9Sstevel@tonic-gate 		if ((t->t_state == TS_SLEEP && swapin_time > fss_minslp) ||
1880*7c478bd9Sstevel@tonic-gate 		    (t->t_state == TS_RUN && swapin_time > fss_minrun)) {
1881*7c478bd9Sstevel@tonic-gate 			pri = fss_maxumdpri;
1882*7c478bd9Sstevel@tonic-gate 			epri = swapin_time -
1883*7c478bd9Sstevel@tonic-gate 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
1884*7c478bd9Sstevel@tonic-gate 		} else {
1885*7c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
1886*7c478bd9Sstevel@tonic-gate 		}
1887*7c478bd9Sstevel@tonic-gate 	}
1888*7c478bd9Sstevel@tonic-gate 
1889*7c478bd9Sstevel@tonic-gate 	/*
1890*7c478bd9Sstevel@tonic-gate 	 * Scale epri so that SHRT_MAX / 2 represents zero priority.
1891*7c478bd9Sstevel@tonic-gate 	 */
1892*7c478bd9Sstevel@tonic-gate 	epri += SHRT_MAX / 2;
1893*7c478bd9Sstevel@tonic-gate 	if (epri < 0)
1894*7c478bd9Sstevel@tonic-gate 		epri = 0;
1895*7c478bd9Sstevel@tonic-gate 	else if (epri > SHRT_MAX)
1896*7c478bd9Sstevel@tonic-gate 		epri = SHRT_MAX;
1897*7c478bd9Sstevel@tonic-gate 
1898*7c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
1899*7c478bd9Sstevel@tonic-gate }
1900*7c478bd9Sstevel@tonic-gate 
1901*7c478bd9Sstevel@tonic-gate /*
1902*7c478bd9Sstevel@tonic-gate  * If thread is currently at a kernel mode priority (has slept) and is
1903*7c478bd9Sstevel@tonic-gate  * returning to the userland we assign it the appropriate user mode priority
1904*7c478bd9Sstevel@tonic-gate  * and time quantum here.  If we're lowering the thread's priority below that
1905*7c478bd9Sstevel@tonic-gate  * of other runnable threads then we will set runrun via cpu_surrender() to
1906*7c478bd9Sstevel@tonic-gate  * cause preemption.
1907*7c478bd9Sstevel@tonic-gate  */
1908*7c478bd9Sstevel@tonic-gate static void
1909*7c478bd9Sstevel@tonic-gate fss_trapret(kthread_t *t)
1910*7c478bd9Sstevel@tonic-gate {
1911*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
1912*7c478bd9Sstevel@tonic-gate 	cpu_t *cp = CPU;
1913*7c478bd9Sstevel@tonic-gate 
1914*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1915*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1916*7c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_dispthread == t);
1917*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1918*7c478bd9Sstevel@tonic-gate 
1919*7c478bd9Sstevel@tonic-gate 	t->t_kpri_req = 0;
1920*7c478bd9Sstevel@tonic-gate 	if (fssproc->fss_flags & FSSKPRI) {
1921*7c478bd9Sstevel@tonic-gate 		/*
1922*7c478bd9Sstevel@tonic-gate 		 * If thread has blocked in the kernel
1923*7c478bd9Sstevel@tonic-gate 		 */
1924*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
1925*7c478bd9Sstevel@tonic-gate 		cp->cpu_dispatch_pri = DISP_PRIO(t);
1926*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
1927*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSKPRI;
1928*7c478bd9Sstevel@tonic-gate 
1929*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t))
1930*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
1931*7c478bd9Sstevel@tonic-gate 	}
1932*7c478bd9Sstevel@tonic-gate 
1933*7c478bd9Sstevel@tonic-gate 	/*
1934*7c478bd9Sstevel@tonic-gate 	 * Swapout lwp if the swapper is waiting for this thread to reach
1935*7c478bd9Sstevel@tonic-gate 	 * a safe point.
1936*7c478bd9Sstevel@tonic-gate 	 */
1937*7c478bd9Sstevel@tonic-gate 	if (t->t_schedflag & TS_SWAPENQ) {
1938*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1939*7c478bd9Sstevel@tonic-gate 		swapout_lwp(ttolwp(t));
1940*7c478bd9Sstevel@tonic-gate 		thread_lock(t);
1941*7c478bd9Sstevel@tonic-gate 	}
1942*7c478bd9Sstevel@tonic-gate }
1943*7c478bd9Sstevel@tonic-gate 
1944*7c478bd9Sstevel@tonic-gate /*
1945*7c478bd9Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location on dispatcher queue.
1946*7c478bd9Sstevel@tonic-gate  * This is called with the current thread in TS_ONPROC and locked.
1947*7c478bd9Sstevel@tonic-gate  */
1948*7c478bd9Sstevel@tonic-gate static void
1949*7c478bd9Sstevel@tonic-gate fss_preempt(kthread_t *t)
1950*7c478bd9Sstevel@tonic-gate {
1951*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
1952*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
1953*7c478bd9Sstevel@tonic-gate 	uint_t flags;
1954*7c478bd9Sstevel@tonic-gate 
1955*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1956*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(curthread));
1957*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1958*7c478bd9Sstevel@tonic-gate 
1959*7c478bd9Sstevel@tonic-gate 	/*
1960*7c478bd9Sstevel@tonic-gate 	 * If preempted in the kernel, make sure the thread has a kernel
1961*7c478bd9Sstevel@tonic-gate 	 * priority if needed.
1962*7c478bd9Sstevel@tonic-gate 	 */
1963*7c478bd9Sstevel@tonic-gate 	lwp = curthread->t_lwp;
1964*7c478bd9Sstevel@tonic-gate 	if (!(fssproc->fss_flags & FSSKPRI) && lwp != NULL && t->t_kpri_req) {
1965*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
1966*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
1967*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
1968*7c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
1969*7c478bd9Sstevel@tonic-gate 		aston(t);
1970*7c478bd9Sstevel@tonic-gate 	}
1971*7c478bd9Sstevel@tonic-gate 	/*
1972*7c478bd9Sstevel@tonic-gate 	 * If preempted in user-land mark the thread as swappable because it
1973*7c478bd9Sstevel@tonic-gate 	 * cannot be holding any kernel locks.
1974*7c478bd9Sstevel@tonic-gate 	 */
1975*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
1976*7c478bd9Sstevel@tonic-gate 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
1977*7c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
1978*7c478bd9Sstevel@tonic-gate 
1979*7c478bd9Sstevel@tonic-gate 	/*
1980*7c478bd9Sstevel@tonic-gate 	 * Check to see if we're doing "preemption control" here.  If
1981*7c478bd9Sstevel@tonic-gate 	 * we are, and if the user has requested that this thread not
1982*7c478bd9Sstevel@tonic-gate 	 * be preempted, and if preemptions haven't been put off for
1983*7c478bd9Sstevel@tonic-gate 	 * too long, let the preemption happen here but try to make
1984*7c478bd9Sstevel@tonic-gate 	 * sure the thread is rescheduled as soon as possible.  We d
1985*7c478bd9Sstevel@tonic-gate 	 * this by putting it on the front of the highest priority run
1986*7c478bd9Sstevel@tonic-gate 	 * queue in the FSS class.  If the preemption has been put off
1987*7c478bd9Sstevel@tonic-gate 	 * for too long, clear the "nopreempt" bit and let the thread
1988*7c478bd9Sstevel@tonic-gate 	 * be preempted.
1989*7c478bd9Sstevel@tonic-gate 	 */
1990*7c478bd9Sstevel@tonic-gate 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1991*7c478bd9Sstevel@tonic-gate 		if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
1992*7c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
1993*7c478bd9Sstevel@tonic-gate 			if (!(fssproc->fss_flags & FSSKPRI)) {
1994*7c478bd9Sstevel@tonic-gate 				THREAD_CHANGE_PRI(t, fss_maxumdpri);
1995*7c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_DONT_SWAP;
1996*7c478bd9Sstevel@tonic-gate 				schedctl_set_yield(t, 1);
1997*7c478bd9Sstevel@tonic-gate 			}
1998*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
1999*7c478bd9Sstevel@tonic-gate 			return;
2000*7c478bd9Sstevel@tonic-gate 		} else {
2001*7c478bd9Sstevel@tonic-gate 			schedctl_set_nopreempt(t, 0);
2002*7c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
2003*7c478bd9Sstevel@tonic-gate 			/*
2004*7c478bd9Sstevel@tonic-gate 			 * Fall through and be preempted below.
2005*7c478bd9Sstevel@tonic-gate 			 */
2006*7c478bd9Sstevel@tonic-gate 		}
2007*7c478bd9Sstevel@tonic-gate 	}
2008*7c478bd9Sstevel@tonic-gate 
2009*7c478bd9Sstevel@tonic-gate 	flags = fssproc->fss_flags & (FSSBACKQ | FSSKPRI);
2010*7c478bd9Sstevel@tonic-gate 
2011*7c478bd9Sstevel@tonic-gate 	if (flags == FSSBACKQ) {
2012*7c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
2013*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
2014*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
2015*7c478bd9Sstevel@tonic-gate 	} else if (flags == (FSSBACKQ | FSSKPRI)) {
2016*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
2017*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
2018*7c478bd9Sstevel@tonic-gate 	} else {
2019*7c478bd9Sstevel@tonic-gate 		setfrontdq(t);
2020*7c478bd9Sstevel@tonic-gate 	}
2021*7c478bd9Sstevel@tonic-gate }
2022*7c478bd9Sstevel@tonic-gate 
2023*7c478bd9Sstevel@tonic-gate /*
2024*7c478bd9Sstevel@tonic-gate  * Called when a thread is waking up and is to be placed on the run queue.
2025*7c478bd9Sstevel@tonic-gate  */
2026*7c478bd9Sstevel@tonic-gate static void
2027*7c478bd9Sstevel@tonic-gate fss_setrun(kthread_t *t)
2028*7c478bd9Sstevel@tonic-gate {
2029*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
2030*7c478bd9Sstevel@tonic-gate 
2031*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
2032*7c478bd9Sstevel@tonic-gate 
2033*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP || t->t_state == TS_STOPPED)
2034*7c478bd9Sstevel@tonic-gate 		fss_active(t);
2035*7c478bd9Sstevel@tonic-gate 
2036*7c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
2039*7c478bd9Sstevel@tonic-gate 	/*
2040*7c478bd9Sstevel@tonic-gate 	 * If previously were running at the kernel priority then keep that
2041*7c478bd9Sstevel@tonic-gate 	 * priority and the fss_timeleft doesn't matter.
2042*7c478bd9Sstevel@tonic-gate 	 */
2043*7c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) == 0)
2044*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
2045*7c478bd9Sstevel@tonic-gate 
2046*7c478bd9Sstevel@tonic-gate 	if (t->t_disp_time != lbolt)
2047*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
2048*7c478bd9Sstevel@tonic-gate 	else
2049*7c478bd9Sstevel@tonic-gate 		setfrontdq(t);
2050*7c478bd9Sstevel@tonic-gate }
2051*7c478bd9Sstevel@tonic-gate 
2052*7c478bd9Sstevel@tonic-gate /*
2053*7c478bd9Sstevel@tonic-gate  * Prepare thread for sleep. We reset the thread priority so it will run at the
2054*7c478bd9Sstevel@tonic-gate  * kernel priority level when it wakes up.
2055*7c478bd9Sstevel@tonic-gate  */
2056*7c478bd9Sstevel@tonic-gate static void
2057*7c478bd9Sstevel@tonic-gate fss_sleep(kthread_t *t)
2058*7c478bd9Sstevel@tonic-gate {
2059*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
2060*7c478bd9Sstevel@tonic-gate 
2061*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
2062*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2063*7c478bd9Sstevel@tonic-gate 
2064*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
2065*7c478bd9Sstevel@tonic-gate 	fss_inactive(t);
2066*7c478bd9Sstevel@tonic-gate 
2067*7c478bd9Sstevel@tonic-gate 	/*
2068*7c478bd9Sstevel@tonic-gate 	 * Assign a system priority to the thread and arrange for it to be
2069*7c478bd9Sstevel@tonic-gate 	 * retained when the thread is next placed on the run queue (i.e.,
2070*7c478bd9Sstevel@tonic-gate 	 * when it wakes up) instead of being given a new pri.  Also arrange
2071*7c478bd9Sstevel@tonic-gate 	 * for trapret processing as the thread leaves the system call so it
2072*7c478bd9Sstevel@tonic-gate 	 * will drop back to normal priority range.
2073*7c478bd9Sstevel@tonic-gate 	 */
2074*7c478bd9Sstevel@tonic-gate 	if (t->t_kpri_req) {
2075*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
2076*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
2077*7c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
2078*7c478bd9Sstevel@tonic-gate 		aston(t);
2079*7c478bd9Sstevel@tonic-gate 	} else if (fssproc->fss_flags & FSSKPRI) {
2080*7c478bd9Sstevel@tonic-gate 		/*
2081*7c478bd9Sstevel@tonic-gate 		 * The thread has done a THREAD_KPRI_REQUEST(), slept, then
2082*7c478bd9Sstevel@tonic-gate 		 * done THREAD_KPRI_RELEASE() (so no t_kpri_req is 0 again),
2083*7c478bd9Sstevel@tonic-gate 		 * then slept again all without finishing the current system
2084*7c478bd9Sstevel@tonic-gate 		 * call so trapret won't have cleared FSSKPRI
2085*7c478bd9Sstevel@tonic-gate 		 */
2086*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSKPRI;
2087*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
2088*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(curthread))
2089*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
2090*7c478bd9Sstevel@tonic-gate 	}
2091*7c478bd9Sstevel@tonic-gate 	t->t_stime = lbolt;	/* time stamp for the swapper */
2092*7c478bd9Sstevel@tonic-gate }
2093*7c478bd9Sstevel@tonic-gate 
2094*7c478bd9Sstevel@tonic-gate /*
2095*7c478bd9Sstevel@tonic-gate  * A tick interrupt has ocurrend on a running thread. Check to see if our
2096*7c478bd9Sstevel@tonic-gate  * time slice has expired.  We must also clear the TS_DONT_SWAP flag in
2097*7c478bd9Sstevel@tonic-gate  * t_schedflag if the thread is eligible to be swapped out.
2098*7c478bd9Sstevel@tonic-gate  */
2099*7c478bd9Sstevel@tonic-gate static void
2100*7c478bd9Sstevel@tonic-gate fss_tick(kthread_t *t)
2101*7c478bd9Sstevel@tonic-gate {
2102*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
2103*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
2104*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
2105*7c478bd9Sstevel@tonic-gate 
2106*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
2107*7c478bd9Sstevel@tonic-gate 
2108*7c478bd9Sstevel@tonic-gate 	/*
2109*7c478bd9Sstevel@tonic-gate 	 * It's safe to access fsspset and fssproj structures because we're
2110*7c478bd9Sstevel@tonic-gate 	 * holding our p_lock here.
2111*7c478bd9Sstevel@tonic-gate 	 */
2112*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
2113*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
2114*7c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
2115*7c478bd9Sstevel@tonic-gate 	if (fssproj != NULL) {
2116*7c478bd9Sstevel@tonic-gate 		fsspset_t *fsspset = FSSPROJ2FSSPSET(fssproj);
2117*7c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&fsspset->fssps_displock);
2118*7c478bd9Sstevel@tonic-gate 		fssproj->fssp_ticks += fss_nice_tick[fssproc->fss_nice];
2119*7c478bd9Sstevel@tonic-gate 		fssproc->fss_ticks++;
2120*7c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
2121*7c478bd9Sstevel@tonic-gate 	}
2122*7c478bd9Sstevel@tonic-gate 
2123*7c478bd9Sstevel@tonic-gate 	/*
2124*7c478bd9Sstevel@tonic-gate 	 * A thread's execution time for threads running in the SYS class
2125*7c478bd9Sstevel@tonic-gate 	 * is not tracked.
2126*7c478bd9Sstevel@tonic-gate 	 */
2127*7c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) == 0) {
2128*7c478bd9Sstevel@tonic-gate 		/*
2129*7c478bd9Sstevel@tonic-gate 		 * If thread is not in kernel mode, decrement its fss_timeleft
2130*7c478bd9Sstevel@tonic-gate 		 */
2131*7c478bd9Sstevel@tonic-gate 		if (--fssproc->fss_timeleft <= 0) {
2132*7c478bd9Sstevel@tonic-gate 			pri_t new_pri;
2133*7c478bd9Sstevel@tonic-gate 
2134*7c478bd9Sstevel@tonic-gate 			/*
2135*7c478bd9Sstevel@tonic-gate 			 * If we're doing preemption control and trying to
2136*7c478bd9Sstevel@tonic-gate 			 * avoid preempting this thread, just note that the
2137*7c478bd9Sstevel@tonic-gate 			 * thread should yield soon and let it keep running
2138*7c478bd9Sstevel@tonic-gate 			 * (unless it's been a while).
2139*7c478bd9Sstevel@tonic-gate 			 */
2140*7c478bd9Sstevel@tonic-gate 			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
2141*7c478bd9Sstevel@tonic-gate 				if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
2142*7c478bd9Sstevel@tonic-gate 					DTRACE_SCHED1(schedctl__nopreempt,
2143*7c478bd9Sstevel@tonic-gate 					    kthread_t *, t);
2144*7c478bd9Sstevel@tonic-gate 					schedctl_set_yield(t, 1);
2145*7c478bd9Sstevel@tonic-gate 					thread_unlock_nopreempt(t);
2146*7c478bd9Sstevel@tonic-gate 					return;
2147*7c478bd9Sstevel@tonic-gate 				}
2148*7c478bd9Sstevel@tonic-gate 			}
2149*7c478bd9Sstevel@tonic-gate 
2150*7c478bd9Sstevel@tonic-gate 			fss_newpri(fssproc);
2151*7c478bd9Sstevel@tonic-gate 			new_pri = fssproc->fss_umdpri;
2152*7c478bd9Sstevel@tonic-gate 			ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
2153*7c478bd9Sstevel@tonic-gate 
2154*7c478bd9Sstevel@tonic-gate 			/*
2155*7c478bd9Sstevel@tonic-gate 			 * When the priority of a thread is changed, it may
2156*7c478bd9Sstevel@tonic-gate 			 * be necessary to adjust its position on a sleep queue
2157*7c478bd9Sstevel@tonic-gate 			 * or dispatch queue. The function thread_change_pri
2158*7c478bd9Sstevel@tonic-gate 			 * accomplishes this.
2159*7c478bd9Sstevel@tonic-gate 			 */
2160*7c478bd9Sstevel@tonic-gate 			if (thread_change_pri(t, new_pri, 0)) {
2161*7c478bd9Sstevel@tonic-gate 				if ((t->t_schedflag & TS_LOAD) &&
2162*7c478bd9Sstevel@tonic-gate 				    (lwp = t->t_lwp) &&
2163*7c478bd9Sstevel@tonic-gate 				    lwp->lwp_state == LWP_USER)
2164*7c478bd9Sstevel@tonic-gate 					t->t_schedflag &= ~TS_DONT_SWAP;
2165*7c478bd9Sstevel@tonic-gate 				fssproc->fss_timeleft = fss_quantum;
2166*7c478bd9Sstevel@tonic-gate 			} else {
2167*7c478bd9Sstevel@tonic-gate 				fssproc->fss_flags |= FSSBACKQ;
2168*7c478bd9Sstevel@tonic-gate 				cpu_surrender(t);
2169*7c478bd9Sstevel@tonic-gate 			}
2170*7c478bd9Sstevel@tonic-gate 		} else if (t->t_pri < t->t_disp_queue->disp_maxrunpri) {
2171*7c478bd9Sstevel@tonic-gate 			/*
2172*7c478bd9Sstevel@tonic-gate 			 * If there is a higher-priority thread which is
2173*7c478bd9Sstevel@tonic-gate 			 * waiting for a processor, then thread surrenders
2174*7c478bd9Sstevel@tonic-gate 			 * the processor.
2175*7c478bd9Sstevel@tonic-gate 			 */
2176*7c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
2177*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
2178*7c478bd9Sstevel@tonic-gate 		}
2179*7c478bd9Sstevel@tonic-gate 	}
2180*7c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
2181*7c478bd9Sstevel@tonic-gate }
2182*7c478bd9Sstevel@tonic-gate 
2183*7c478bd9Sstevel@tonic-gate /*
2184*7c478bd9Sstevel@tonic-gate  * Processes waking up go to the back of their queue.  We don't need to assign
2185*7c478bd9Sstevel@tonic-gate  * a time quantum here because thread is still at a kernel mode priority and
2186*7c478bd9Sstevel@tonic-gate  * the time slicing is not done for threads running in the kernel after
2187*7c478bd9Sstevel@tonic-gate  * sleeping.  The proper time quantum will be assigned by fss_trapret before the
2188*7c478bd9Sstevel@tonic-gate  * thread returns to user mode.
2189*7c478bd9Sstevel@tonic-gate  */
2190*7c478bd9Sstevel@tonic-gate static void
2191*7c478bd9Sstevel@tonic-gate fss_wakeup(kthread_t *t)
2192*7c478bd9Sstevel@tonic-gate {
2193*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
2194*7c478bd9Sstevel@tonic-gate 
2195*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2196*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_SLEEP);
2197*7c478bd9Sstevel@tonic-gate 
2198*7c478bd9Sstevel@tonic-gate 	fss_active(t);
2199*7c478bd9Sstevel@tonic-gate 
2200*7c478bd9Sstevel@tonic-gate 	t->t_stime = lbolt;		/* time stamp for the swapper */
2201*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
2202*7c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
2203*7c478bd9Sstevel@tonic-gate 
2204*7c478bd9Sstevel@tonic-gate 	if (fssproc->fss_flags & FSSKPRI) {
2205*7c478bd9Sstevel@tonic-gate 		/*
2206*7c478bd9Sstevel@tonic-gate 		 * If we already have a kernel priority assigned, then we
2207*7c478bd9Sstevel@tonic-gate 		 * just use it.
2208*7c478bd9Sstevel@tonic-gate 		 */
2209*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
2210*7c478bd9Sstevel@tonic-gate 	} else if (t->t_kpri_req) {
2211*7c478bd9Sstevel@tonic-gate 		/*
2212*7c478bd9Sstevel@tonic-gate 		 * Give thread a priority boost if we were asked.
2213*7c478bd9Sstevel@tonic-gate 		 */
2214*7c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
2215*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
2216*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
2217*7c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
2218*7c478bd9Sstevel@tonic-gate 		aston(t);
2219*7c478bd9Sstevel@tonic-gate 	} else {
2220*7c478bd9Sstevel@tonic-gate 		/*
2221*7c478bd9Sstevel@tonic-gate 		 * Otherwise, we recalculate the priority.
2222*7c478bd9Sstevel@tonic-gate 		 */
2223*7c478bd9Sstevel@tonic-gate 		if (t->t_disp_time == lbolt) {
2224*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
2225*7c478bd9Sstevel@tonic-gate 		} else {
2226*7c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
2227*7c478bd9Sstevel@tonic-gate 			THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
2228*7c478bd9Sstevel@tonic-gate 			setbackdq(t);
2229*7c478bd9Sstevel@tonic-gate 		}
2230*7c478bd9Sstevel@tonic-gate 	}
2231*7c478bd9Sstevel@tonic-gate }
2232*7c478bd9Sstevel@tonic-gate 
2233*7c478bd9Sstevel@tonic-gate /*
2234*7c478bd9Sstevel@tonic-gate  * fss_donice() is called when a nice(1) command is issued on the thread to
2235*7c478bd9Sstevel@tonic-gate  * alter the priority. The nice(1) command exists in Solaris for compatibility.
2236*7c478bd9Sstevel@tonic-gate  * Thread priority adjustments should be done via priocntl(1).
2237*7c478bd9Sstevel@tonic-gate  */
2238*7c478bd9Sstevel@tonic-gate static int
2239*7c478bd9Sstevel@tonic-gate fss_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2240*7c478bd9Sstevel@tonic-gate {
2241*7c478bd9Sstevel@tonic-gate 	int newnice;
2242*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
2243*7c478bd9Sstevel@tonic-gate 	fssparms_t fssparms;
2244*7c478bd9Sstevel@tonic-gate 
2245*7c478bd9Sstevel@tonic-gate 	/*
2246*7c478bd9Sstevel@tonic-gate 	 * If there is no change to priority, just return current setting.
2247*7c478bd9Sstevel@tonic-gate 	 */
2248*7c478bd9Sstevel@tonic-gate 	if (incr == 0) {
2249*7c478bd9Sstevel@tonic-gate 		if (retvalp)
2250*7c478bd9Sstevel@tonic-gate 			*retvalp = fssproc->fss_nice - NZERO;
2251*7c478bd9Sstevel@tonic-gate 		return (0);
2252*7c478bd9Sstevel@tonic-gate 	}
2253*7c478bd9Sstevel@tonic-gate 
2254*7c478bd9Sstevel@tonic-gate 	if ((incr < 0 || incr > 2 * NZERO) && secpolicy_setpriority(cr) != 0)
2255*7c478bd9Sstevel@tonic-gate 		return (EPERM);
2256*7c478bd9Sstevel@tonic-gate 
2257*7c478bd9Sstevel@tonic-gate 	/*
2258*7c478bd9Sstevel@tonic-gate 	 * Specifying a nice increment greater than the upper limit of
2259*7c478bd9Sstevel@tonic-gate 	 * FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
2260*7c478bd9Sstevel@tonic-gate 	 * value being set to the upper limit.  We check for this before
2261*7c478bd9Sstevel@tonic-gate 	 * computing the new value because otherwise we could get overflow
2262*7c478bd9Sstevel@tonic-gate 	 * if a privileged user specified some ridiculous increment.
2263*7c478bd9Sstevel@tonic-gate 	 */
2264*7c478bd9Sstevel@tonic-gate 	if (incr > FSS_NICE_MAX)
2265*7c478bd9Sstevel@tonic-gate 		incr = FSS_NICE_MAX;
2266*7c478bd9Sstevel@tonic-gate 
2267*7c478bd9Sstevel@tonic-gate 	newnice = fssproc->fss_nice + incr;
2268*7c478bd9Sstevel@tonic-gate 	if (newnice > FSS_NICE_MAX)
2269*7c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MAX;
2270*7c478bd9Sstevel@tonic-gate 	else if (newnice < FSS_NICE_MIN)
2271*7c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MIN;
2272*7c478bd9Sstevel@tonic-gate 
2273*7c478bd9Sstevel@tonic-gate 	fssparms.fss_uprilim = fssparms.fss_upri =
2274*7c478bd9Sstevel@tonic-gate 	    -((newnice - NZERO) * fss_maxupri) / NZERO;
2275*7c478bd9Sstevel@tonic-gate 
2276*7c478bd9Sstevel@tonic-gate 	/*
2277*7c478bd9Sstevel@tonic-gate 	 * Reset the uprilim and upri values of the thread.
2278*7c478bd9Sstevel@tonic-gate 	 */
2279*7c478bd9Sstevel@tonic-gate 	(void) fss_parmsset(t, (void *)&fssparms, (id_t)0, (cred_t *)NULL);
2280*7c478bd9Sstevel@tonic-gate 
2281*7c478bd9Sstevel@tonic-gate 	/*
2282*7c478bd9Sstevel@tonic-gate 	 * Although fss_parmsset already reset fss_nice it may not have been
2283*7c478bd9Sstevel@tonic-gate 	 * set to precisely the value calculated above because fss_parmsset
2284*7c478bd9Sstevel@tonic-gate 	 * determines the nice value from the user priority and we may have
2285*7c478bd9Sstevel@tonic-gate 	 * truncated during the integer conversion from nice value to user
2286*7c478bd9Sstevel@tonic-gate 	 * priority and back. We reset fss_nice to the value we calculated
2287*7c478bd9Sstevel@tonic-gate 	 * above.
2288*7c478bd9Sstevel@tonic-gate 	 */
2289*7c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = (char)newnice;
2290*7c478bd9Sstevel@tonic-gate 
2291*7c478bd9Sstevel@tonic-gate 	if (retvalp)
2292*7c478bd9Sstevel@tonic-gate 		*retvalp = newnice - NZERO;
2293*7c478bd9Sstevel@tonic-gate 	return (0);
2294*7c478bd9Sstevel@tonic-gate }
2295*7c478bd9Sstevel@tonic-gate 
2296*7c478bd9Sstevel@tonic-gate /*
2297*7c478bd9Sstevel@tonic-gate  * Return the global scheduling priority that would be assigned to a thread
2298*7c478bd9Sstevel@tonic-gate  * entering the fair-sharing class with the fss_upri.
2299*7c478bd9Sstevel@tonic-gate  */
2300*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2301*7c478bd9Sstevel@tonic-gate static pri_t
2302*7c478bd9Sstevel@tonic-gate fss_globpri(kthread_t *t)
2303*7c478bd9Sstevel@tonic-gate {
2304*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2305*7c478bd9Sstevel@tonic-gate 
2306*7c478bd9Sstevel@tonic-gate 	return (fss_maxumdpri / 2);
2307*7c478bd9Sstevel@tonic-gate }
2308*7c478bd9Sstevel@tonic-gate 
2309*7c478bd9Sstevel@tonic-gate /*
2310*7c478bd9Sstevel@tonic-gate  * Called from the yield(2) system call when a thread is yielding (surrendering)
2311*7c478bd9Sstevel@tonic-gate  * the processor. The kernel thread is placed at the back of a dispatch queue.
2312*7c478bd9Sstevel@tonic-gate  */
2313*7c478bd9Sstevel@tonic-gate static void
2314*7c478bd9Sstevel@tonic-gate fss_yield(kthread_t *t)
2315*7c478bd9Sstevel@tonic-gate {
2316*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
2317*7c478bd9Sstevel@tonic-gate 
2318*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
2319*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2320*7c478bd9Sstevel@tonic-gate 
2321*7c478bd9Sstevel@tonic-gate 	/*
2322*7c478bd9Sstevel@tonic-gate 	 * Clear the preemption control "yield" bit since the user is
2323*7c478bd9Sstevel@tonic-gate 	 * doing a yield.
2324*7c478bd9Sstevel@tonic-gate 	 */
2325*7c478bd9Sstevel@tonic-gate 	if (t->t_schedctl)
2326*7c478bd9Sstevel@tonic-gate 		schedctl_set_yield(t, 0);
2327*7c478bd9Sstevel@tonic-gate 	if (fssproc->fss_timeleft < 0) {
2328*7c478bd9Sstevel@tonic-gate 		/*
2329*7c478bd9Sstevel@tonic-gate 		 * Time slice was artificially extended to avoid preemption,
2330*7c478bd9Sstevel@tonic-gate 		 * so pretend we're preempting it now.
2331*7c478bd9Sstevel@tonic-gate 		 */
2332*7c478bd9Sstevel@tonic-gate 		DTRACE_SCHED1(schedctl__yield, int, -fssproc->fss_timeleft);
2333*7c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
2334*7c478bd9Sstevel@tonic-gate 	}
2335*7c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
2336*7c478bd9Sstevel@tonic-gate 	setbackdq(t);
2337*7c478bd9Sstevel@tonic-gate }
2338*7c478bd9Sstevel@tonic-gate 
2339*7c478bd9Sstevel@tonic-gate void
2340*7c478bd9Sstevel@tonic-gate fss_changeproj(kthread_t *t, void *kp, void *zp, fssbuf_t *projbuf,
2341*7c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
2342*7c478bd9Sstevel@tonic-gate {
2343*7c478bd9Sstevel@tonic-gate 	kproject_t *kpj_new = kp;
2344*7c478bd9Sstevel@tonic-gate 	zone_t *zone = zp;
2345*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
2346*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
2347*7c478bd9Sstevel@tonic-gate 	kproject_t *kpj_old;
2348*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
2349*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
2350*7c478bd9Sstevel@tonic-gate 	int free = 0;
2351*7c478bd9Sstevel@tonic-gate 	int id;
2352*7c478bd9Sstevel@tonic-gate 
2353*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2354*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2355*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2356*7c478bd9Sstevel@tonic-gate 
2357*7c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
2358*7c478bd9Sstevel@tonic-gate 		return;
2359*7c478bd9Sstevel@tonic-gate 
2360*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
2361*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
2362*7c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
2363*7c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
2364*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
2365*7c478bd9Sstevel@tonic-gate 		return;
2366*7c478bd9Sstevel@tonic-gate 	}
2367*7c478bd9Sstevel@tonic-gate 
2368*7c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj_old);
2369*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
2370*7c478bd9Sstevel@tonic-gate 	kpj_old = FSSPROJ2KPROJ(fssproj_old);
2371*7c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
2372*7c478bd9Sstevel@tonic-gate 
2373*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_cpupart == fsspset->fssps_cpupart);
2374*7c478bd9Sstevel@tonic-gate 
2375*7c478bd9Sstevel@tonic-gate 	if (kpj_old == kpj_new) {
2376*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
2377*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
2378*7c478bd9Sstevel@tonic-gate 		return;
2379*7c478bd9Sstevel@tonic-gate 	}
2380*7c478bd9Sstevel@tonic-gate 
2381*7c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset, zone)) == NULL) {
2382*7c478bd9Sstevel@tonic-gate 		/*
2383*7c478bd9Sstevel@tonic-gate 		 * If the zone for the new project is not currently active on
2384*7c478bd9Sstevel@tonic-gate 		 * the cpu partition we're on, get one of the pre-allocated
2385*7c478bd9Sstevel@tonic-gate 		 * buffers and link it in our per-pset zone list.  Such buffers
2386*7c478bd9Sstevel@tonic-gate 		 * should already exist.
2387*7c478bd9Sstevel@tonic-gate 		 */
2388*7c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
2389*7c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
2390*7c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset, zone, fsszone_new);
2391*7c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
2392*7c478bd9Sstevel@tonic-gate 				break;
2393*7c478bd9Sstevel@tonic-gate 			}
2394*7c478bd9Sstevel@tonic-gate 		}
2395*7c478bd9Sstevel@tonic-gate 	}
2396*7c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
2397*7c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset, kpj_new)) == NULL) {
2398*7c478bd9Sstevel@tonic-gate 		/*
2399*7c478bd9Sstevel@tonic-gate 		 * If our new project is not currently running
2400*7c478bd9Sstevel@tonic-gate 		 * on the cpu partition we're on, get one of the
2401*7c478bd9Sstevel@tonic-gate 		 * pre-allocated buffers and link it in our new cpu
2402*7c478bd9Sstevel@tonic-gate 		 * partition doubly linked list. Such buffers should already
2403*7c478bd9Sstevel@tonic-gate 		 * exist.
2404*7c478bd9Sstevel@tonic-gate 		 */
2405*7c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
2406*7c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
2407*7c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset, kpj_new,
2408*7c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
2409*7c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
2410*7c478bd9Sstevel@tonic-gate 				break;
2411*7c478bd9Sstevel@tonic-gate 			}
2412*7c478bd9Sstevel@tonic-gate 		}
2413*7c478bd9Sstevel@tonic-gate 	}
2414*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
2415*7c478bd9Sstevel@tonic-gate 
2416*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
2417*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC)
2418*7c478bd9Sstevel@tonic-gate 		fss_inactive(t);
2419*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_old->fssp_threads > 0);
2420*7c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
2421*7c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj_old);
2422*7c478bd9Sstevel@tonic-gate 		free = 1;
2423*7c478bd9Sstevel@tonic-gate 	}
2424*7c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
2425*7c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
2426*7c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
2427*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC)
2428*7c478bd9Sstevel@tonic-gate 		fss_active(t);
2429*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
2430*7c478bd9Sstevel@tonic-gate 	if (free) {
2431*7c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
2432*7c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
2433*7c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
2434*7c478bd9Sstevel@tonic-gate 	}
2435*7c478bd9Sstevel@tonic-gate 
2436*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
2437*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
2438*7c478bd9Sstevel@tonic-gate }
2439*7c478bd9Sstevel@tonic-gate 
2440*7c478bd9Sstevel@tonic-gate void
2441*7c478bd9Sstevel@tonic-gate fss_changepset(kthread_t *t, void *newcp, fssbuf_t *projbuf,
2442*7c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
2443*7c478bd9Sstevel@tonic-gate {
2444*7c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset_old, *fsspset_new;
2445*7c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
2446*7c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
2447*7c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
2448*7c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
2449*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
2450*7c478bd9Sstevel@tonic-gate 	int id;
2451*7c478bd9Sstevel@tonic-gate 
2452*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2453*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2454*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2455*7c478bd9Sstevel@tonic-gate 
2456*7c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
2457*7c478bd9Sstevel@tonic-gate 		return;
2458*7c478bd9Sstevel@tonic-gate 
2459*7c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
2460*7c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
2461*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
2462*7c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
2463*7c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
2464*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
2465*7c478bd9Sstevel@tonic-gate 		return;
2466*7c478bd9Sstevel@tonic-gate 	}
2467*7c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
2468*7c478bd9Sstevel@tonic-gate 	fsspset_old = FSSPROJ2FSSPSET(fssproj_old);
2469*7c478bd9Sstevel@tonic-gate 	kpj = FSSPROJ2KPROJ(fssproj_old);
2470*7c478bd9Sstevel@tonic-gate 
2471*7c478bd9Sstevel@tonic-gate 	if (fsspset_old->fssps_cpupart == newcp) {
2472*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
2473*7c478bd9Sstevel@tonic-gate 		return;
2474*7c478bd9Sstevel@tonic-gate 	}
2475*7c478bd9Sstevel@tonic-gate 
2476*7c478bd9Sstevel@tonic-gate 	ASSERT(ttoproj(t) == kpj);
2477*7c478bd9Sstevel@tonic-gate 
2478*7c478bd9Sstevel@tonic-gate 	fsspset_new = fss_find_fsspset(newcp);
2479*7c478bd9Sstevel@tonic-gate 
2480*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_new->fssps_lock);
2481*7c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset_new, zone)) == NULL) {
2482*7c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
2483*7c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
2484*7c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset_new, zone,
2485*7c478bd9Sstevel@tonic-gate 				    fsszone_new);
2486*7c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
2487*7c478bd9Sstevel@tonic-gate 				break;
2488*7c478bd9Sstevel@tonic-gate 			}
2489*7c478bd9Sstevel@tonic-gate 		}
2490*7c478bd9Sstevel@tonic-gate 	}
2491*7c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
2492*7c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset_new, kpj)) == NULL) {
2493*7c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
2494*7c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
2495*7c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset_new, kpj,
2496*7c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
2497*7c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
2498*7c478bd9Sstevel@tonic-gate 				break;
2499*7c478bd9Sstevel@tonic-gate 			}
2500*7c478bd9Sstevel@tonic-gate 		}
2501*7c478bd9Sstevel@tonic-gate 	}
2502*7c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
2503*7c478bd9Sstevel@tonic-gate 
2504*7c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
2505*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
2506*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC)
2507*7c478bd9Sstevel@tonic-gate 		fss_inactive(t);
2508*7c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
2509*7c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
2510*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC)
2511*7c478bd9Sstevel@tonic-gate 		fss_active(t);
2512*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
2513*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_new->fssps_lock);
2514*7c478bd9Sstevel@tonic-gate 
2515*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_old->fssps_lock);
2516*7c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
2517*7c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset_old, fssproj_old);
2518*7c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
2519*7c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
2520*7c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
2521*7c478bd9Sstevel@tonic-gate 	}
2522*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_old->fssps_lock);
2523*7c478bd9Sstevel@tonic-gate 
2524*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
2525*7c478bd9Sstevel@tonic-gate }
2526