xref: /titanic_53/usr/src/uts/common/disp/fss.c (revision 482a774964a5167f49856a87b22ce7797ce5d694)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c97ad5cdSakolb  * Common Development and Distribution License (the "License").
6c97ad5cdSakolb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
219ac8606fSraf 
227c478bd9Sstevel@tonic-gate /*
23d5493db7SPramod Batni  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
2424d819e6SJerry Jelinek  * Copyright 2013, Joyent, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/cred.h>
317c478bd9Sstevel@tonic-gate #include <sys/proc.h>
327c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
337c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
347c478bd9Sstevel@tonic-gate #include <sys/class.h>
357c478bd9Sstevel@tonic-gate #include <sys/disp.h>
367c478bd9Sstevel@tonic-gate #include <sys/procset.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
437c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
447c478bd9Sstevel@tonic-gate #include <sys/project.h>
457c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
467c478bd9Sstevel@tonic-gate #include <sys/fss.h>
477c478bd9Sstevel@tonic-gate #include <sys/fsspriocntl.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
497c478bd9Sstevel@tonic-gate #include <sys/zone.h>
507c478bd9Sstevel@tonic-gate #include <vm/rm.h>
517c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
527c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
537c478bd9Sstevel@tonic-gate #include <sys/policy.h>
547c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
55c97ad5cdSakolb #include <sys/cpucaps.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
58*482a7749SJerry Jelinek  * The fair share scheduling class ensures that collections of processes
59*482a7749SJerry Jelinek  * (zones and projects) each get their configured share of CPU.  This is in
60*482a7749SJerry Jelinek  * contrast to the TS class which considers individual processes.
61*482a7749SJerry Jelinek  *
62*482a7749SJerry Jelinek  * The FSS cpu-share is set on zones using the zone.cpu-shares rctl and on
63*482a7749SJerry Jelinek  * projects using the project.cpu-shares rctl.  By default the value is 1
64*482a7749SJerry Jelinek  * and it can range from 0 - 64k.  A value of 0 means that processes in the
65*482a7749SJerry Jelinek  * collection will only get CPU resources when there are no other processes
66*482a7749SJerry Jelinek  * that need CPU. The cpu-share is used as one of the inputs to calculate a
67*482a7749SJerry Jelinek  * thread's "user-mode" priority (umdpri) for the scheduler.  The umdpri falls
68*482a7749SJerry Jelinek  * in the range 0-59.  FSS calculates other, internal, priorities which are not
69*482a7749SJerry Jelinek  * visible outside of the FSS class.
70*482a7749SJerry Jelinek  *
71*482a7749SJerry Jelinek  * The FSS class should approximate TS behavior when there are excess CPU
72*482a7749SJerry Jelinek  * resources.  When there is a backlog of runnable processes, then the share
73*482a7749SJerry Jelinek  * is used as input into the runnable process's priority calculation, where
74*482a7749SJerry Jelinek  * the final umdpri is used by the scheduler to determine when the process runs.
75*482a7749SJerry Jelinek  *
76*482a7749SJerry Jelinek  * Projects in a zone compete with each other for CPU time, receiving CPU
77*482a7749SJerry Jelinek  * allocation within a zone proportional to the project's share; at a higher
78*482a7749SJerry Jelinek  * level zones compete with each other, receiving allocation in a pset
79*482a7749SJerry Jelinek  * proportional to the zone's share.
80*482a7749SJerry Jelinek  *
81*482a7749SJerry Jelinek  * The FSS priority calculation consists of several parts.
82*482a7749SJerry Jelinek  *
83*482a7749SJerry Jelinek  * 1) Once per second the fss_update function runs. The first thing it does is
84*482a7749SJerry Jelinek  *    call fss_decay_usage. This function does three things.
85*482a7749SJerry Jelinek  *
86*482a7749SJerry Jelinek  * a) fss_decay_usage first decays the maxfsspri value for the pset.  This
87*482a7749SJerry Jelinek  *    value is used in the per-process priority calculation described in step
88*482a7749SJerry Jelinek  *    (2b).  The maxfsspri is decayed using the following formula:
89*482a7749SJerry Jelinek  *
90*482a7749SJerry Jelinek  *                      maxfsspri * fss_nice_decay[NZERO])
91*482a7749SJerry Jelinek  *        maxfsspri =  ------------------------------------
92*482a7749SJerry Jelinek  *                            FSS_DECAY_BASE
93*482a7749SJerry Jelinek  *
94*482a7749SJerry Jelinek  *
95*482a7749SJerry Jelinek  *     - NZERO is the default process priority (i.e. 20)
96*482a7749SJerry Jelinek  *
97*482a7749SJerry Jelinek  *    The fss_nice_decay array is a fixed set of values used to adjust the
98*482a7749SJerry Jelinek  *    decay rate of processes based on their nice value.  Entries in this
99*482a7749SJerry Jelinek  *    array are initialized in fss_init using the following formula:
100*482a7749SJerry Jelinek  *
101*482a7749SJerry Jelinek  *                        (FSS_DECAY_MAX - FSS_DECAY_MIN) * i
102*482a7749SJerry Jelinek  *       FSS_DECAY_MIN + -------------------------------------
103*482a7749SJerry Jelinek  *                               FSS_NICE_RANGE - 1
104*482a7749SJerry Jelinek  *
105*482a7749SJerry Jelinek  *     - FSS_DECAY_MIN is 82 = approximates 65% (82/128)
106*482a7749SJerry Jelinek  *     - FSS_DECAY_MAX is 108 = approximates 85% (108/128)
107*482a7749SJerry Jelinek  *     - FSS_NICE_RANGE is 40 (range is 0 - 39)
108*482a7749SJerry Jelinek  *
109*482a7749SJerry Jelinek  * b) The second thing fss_decay_usage does is update each project's "usage"
110*482a7749SJerry Jelinek  *    for the last second and then recalculates the project's "share usage".
111*482a7749SJerry Jelinek  *
112*482a7749SJerry Jelinek  *    The usage value is the recent CPU usage for all of the threads in the
113*482a7749SJerry Jelinek  *    project. It is decayed and updated this way:
114*482a7749SJerry Jelinek  *
115*482a7749SJerry Jelinek  *                  (usage * FSS_DECAY_USG)
116*482a7749SJerry Jelinek  *        usage =  ------------------------- + ticks;
117*482a7749SJerry Jelinek  *                       FSS_DECAY_BASE
118*482a7749SJerry Jelinek  *
119*482a7749SJerry Jelinek  *     - FSS_DECAY_BASE is 128 - used instead of 100 so we can shift vs divide
120*482a7749SJerry Jelinek  *     - FSS_DECAY_USG is 96 - approximates 75% (96/128)
121*482a7749SJerry Jelinek  *     - ticks is updated whenever a process in this project is running
122*482a7749SJerry Jelinek  *       when the scheduler's tick processing fires. This is not a simple
123*482a7749SJerry Jelinek  *       counter, the values are based on the entries in the fss_nice_tick
124*482a7749SJerry Jelinek  *       array (see section 3 below). ticks is then reset to 0 so it can track
125*482a7749SJerry Jelinek  *       the next seconds worth of nice-adjusted time for the project.
126*482a7749SJerry Jelinek  *
127*482a7749SJerry Jelinek  * c) The third thing fss_decay_usage does is update each project's "share
128*482a7749SJerry Jelinek  *    usage" (shusage). This is the normalized usage value for the project and
129*482a7749SJerry Jelinek  *    is calculated this way:
130*482a7749SJerry Jelinek  *
131*482a7749SJerry Jelinek  *                pset_shares^2    zone_int_shares^2
132*482a7749SJerry Jelinek  *        usage * ------------- * ------------------
133*482a7749SJerry Jelinek  *                kpj_shares^2	   zone_ext_shares^2
134*482a7749SJerry Jelinek  *
135*482a7749SJerry Jelinek  *    - usage - see (1b) for more details
136*482a7749SJerry Jelinek  *    - pset_shares is the total of all *active* zone shares in the pset (by
137*482a7749SJerry Jelinek  *      default there is only one pset)
138*482a7749SJerry Jelinek  *    - kpj_shares is the individual project's share (project.cpu-shares rctl)
139*482a7749SJerry Jelinek  *    - zone_int_shares is the sum of shares of all active projects within the
140*482a7749SJerry Jelinek  *      zone (the zone-internal total)
141*482a7749SJerry Jelinek  *    - zone_ext_shares is the share value for the zone (zone.cpu-shares rctl)
142*482a7749SJerry Jelinek  *
143*482a7749SJerry Jelinek  *    The shusage is used in step (2b) to calculate the thread's new internal
144*482a7749SJerry Jelinek  *    priority. A larger shusage value leads to a lower priority.
145*482a7749SJerry Jelinek  *
146*482a7749SJerry Jelinek  * 2) The fss_update function then calls fss_update_list to update the priority
147*482a7749SJerry Jelinek  *    of all threads. This does two things.
148*482a7749SJerry Jelinek  *
149*482a7749SJerry Jelinek  * a) First the thread's internal priority is decayed using the following
150*482a7749SJerry Jelinek  *    formula:
151*482a7749SJerry Jelinek  *
152*482a7749SJerry Jelinek  *                  fsspri * fss_nice_decay[nice_value])
153*482a7749SJerry Jelinek  *        fsspri =  ------------------------------------
154*482a7749SJerry Jelinek  *                            FSS_DECAY_BASE
155*482a7749SJerry Jelinek  *
156*482a7749SJerry Jelinek  *     - FSS_DECAY_BASE is 128 as described above
157*482a7749SJerry Jelinek  *
158*482a7749SJerry Jelinek  * b) Second, if the thread is runnable (TS_RUN or TS_WAIT) calls fss_newpri
159*482a7749SJerry Jelinek  *    to update the user-mode priority (umdpri) of the runnable thread.
160*482a7749SJerry Jelinek  *    Threads that are running (TS_ONPROC) or waiting for an event (TS_SLEEP)
161*482a7749SJerry Jelinek  *    are not updated at this time. The updated user-mode priority can cause
162*482a7749SJerry Jelinek  *    threads to change their position in the run queue.
163*482a7749SJerry Jelinek  *
164*482a7749SJerry Jelinek  *    The process's new internal fsspri is calculated using the following
165*482a7749SJerry Jelinek  *    formula. All runnable threads in the project will use the same shusage
166*482a7749SJerry Jelinek  *    and nrunnable values in their calculation.
167*482a7749SJerry Jelinek  *
168*482a7749SJerry Jelinek  *        fsspri += shusage * nrunnable * ticks
169*482a7749SJerry Jelinek  *
170*482a7749SJerry Jelinek  *     - shusage is the project's share usage, calculated in (1c)
171*482a7749SJerry Jelinek  *     - nrunnable is the number of runnable threads in the project
172*482a7749SJerry Jelinek  *     - ticks is the number of ticks this thread ran since the last fss_newpri
173*482a7749SJerry Jelinek  *       invocation.
174*482a7749SJerry Jelinek  *
175*482a7749SJerry Jelinek  *    Finally the process's new user-mode priority is calculated using the
176*482a7749SJerry Jelinek  *    following formula:
177*482a7749SJerry Jelinek  *
178*482a7749SJerry Jelinek  *                              (fsspri * umdprirange)
179*482a7749SJerry Jelinek  *        umdpri = maxumdpri - ------------------------
180*482a7749SJerry Jelinek  *                                    maxfsspri
181*482a7749SJerry Jelinek  *
182*482a7749SJerry Jelinek  *     - maxumdpri is MINCLSYSPRI - 1 (i.e. 59)
183*482a7749SJerry Jelinek  *     - umdprirange is maxumdpri - 1 (i.e. 58)
184*482a7749SJerry Jelinek  *     - maxfsspri is the largest fsspri seen so far, as we're iterating all
185*482a7749SJerry Jelinek  *       runnable processes
186*482a7749SJerry Jelinek  *
187*482a7749SJerry Jelinek  *    Thus, a higher internal priority (fsspri) leads to a lower user-mode
188*482a7749SJerry Jelinek  *    priority which means the thread runs less. The fsspri is higher when
189*482a7749SJerry Jelinek  *    the project's normalized share usage is higher, when the project has
190*482a7749SJerry Jelinek  *    more runnable threads, or when the thread has accumulated more run-time.
191*482a7749SJerry Jelinek  *
192*482a7749SJerry Jelinek  *    This code has various checks to ensure the resulting umdpri is in the
193*482a7749SJerry Jelinek  *    range 1-59.  See fss_newpri for more details.
194*482a7749SJerry Jelinek  *
195*482a7749SJerry Jelinek  * To reiterate, the above processing is performed once per second to recompute
196*482a7749SJerry Jelinek  * the runnable thread user-mode priorities.
197*482a7749SJerry Jelinek  *
198*482a7749SJerry Jelinek  * 3) The final major component in the priority calculation is the tick
199*482a7749SJerry Jelinek  *    processing which occurs on a thread that is running when the clock
200*482a7749SJerry Jelinek  *    calls fss_tick.
201*482a7749SJerry Jelinek  *
202*482a7749SJerry Jelinek  *    A thread can run continuously in user-land (compute-bound) for the
203*482a7749SJerry Jelinek  *    fss_quantum (see "dispadmin -c FSS -g" for the configurable properties).
204*482a7749SJerry Jelinek  *    The fss_quantum defaults to 11 (i.e. 11 ticks).
205*482a7749SJerry Jelinek  *
206*482a7749SJerry Jelinek  *    Once the quantum has been consumed, the thread will call fss_newpri to
207*482a7749SJerry Jelinek  *    recompute its umdpri priority, as described above in (2b). Threads that
208*482a7749SJerry Jelinek  *    were T_ONPROC at the one second interval when runnable thread priorities
209*482a7749SJerry Jelinek  *    were recalculated will have their umdpri priority recalculated when their
210*482a7749SJerry Jelinek  *    quanta expires.
211*482a7749SJerry Jelinek  *
212*482a7749SJerry Jelinek  *    To ensure that runnable threads within a project see the expected
213*482a7749SJerry Jelinek  *    round-robin behavior, there is a special case in fss_newpri for a thread
214*482a7749SJerry Jelinek  *    that has run for its quanta within the one second update interval.  See
215*482a7749SJerry Jelinek  *    the handling for the quanta_up parameter within fss_newpri.
216*482a7749SJerry Jelinek  *
217*482a7749SJerry Jelinek  *    Also of interest, the fss_tick code increments the project's tick value
218*482a7749SJerry Jelinek  *    using the fss_nice_tick array entry for the thread's nice value. The idea
219*482a7749SJerry Jelinek  *    behind the fss_nice_tick array is that the cost of a tick is lower at
220*482a7749SJerry Jelinek  *    positive nice values (so that it doesn't increase the project's usage
221*482a7749SJerry Jelinek  *    as much as normal) with a 50% drop at the maximum level and a 50%
222*482a7749SJerry Jelinek  *    increase at the minimum level. See (1b). The fss_nice_tick array is
223*482a7749SJerry Jelinek  *    initialized in fss_init using the following formula:
224*482a7749SJerry Jelinek  *
225*482a7749SJerry Jelinek  *         FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2) - i)
226*482a7749SJerry Jelinek  *        --------------------------------------------------
227*482a7749SJerry Jelinek  *                          FSS_NICE_RANGE
228*482a7749SJerry Jelinek  *
229*482a7749SJerry Jelinek  *     - FSS_TICK_COST is 1000, the tick cost for threads with nice level 0
230*482a7749SJerry Jelinek  *
2317c478bd9Sstevel@tonic-gate  * FSS Data Structures:
2327c478bd9Sstevel@tonic-gate  *
2337c478bd9Sstevel@tonic-gate  *                 fsszone
2347c478bd9Sstevel@tonic-gate  *                  -----           -----
2357c478bd9Sstevel@tonic-gate  *  -----          |     |         |     |
2367c478bd9Sstevel@tonic-gate  * |     |-------->|     |<------->|     |<---->...
2377c478bd9Sstevel@tonic-gate  * |     |          -----           -----
2387c478bd9Sstevel@tonic-gate  * |     |          ^    ^            ^
2397c478bd9Sstevel@tonic-gate  * |     |---       |     \            \
2407c478bd9Sstevel@tonic-gate  *  -----    |      |      \            \
2417c478bd9Sstevel@tonic-gate  * fsspset   |      |       \            \
2427c478bd9Sstevel@tonic-gate  *           |      |        \            \
2437c478bd9Sstevel@tonic-gate  *           |    -----       -----       -----
2447c478bd9Sstevel@tonic-gate  *            -->|     |<--->|     |<--->|     |
2457c478bd9Sstevel@tonic-gate  *               |     |     |     |     |     |
2467c478bd9Sstevel@tonic-gate  *                -----       -----       -----
2477c478bd9Sstevel@tonic-gate  *               fssproj
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * That is, fsspsets contain a list of fsszone's that are currently active in
2507c478bd9Sstevel@tonic-gate  * the pset, and a list of fssproj's, corresponding to projects with runnable
2517c478bd9Sstevel@tonic-gate  * threads on the pset.  fssproj's in turn point to the fsszone which they
2527c478bd9Sstevel@tonic-gate  * are a member of.
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * An fssproj_t is removed when there are no threads in it.
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * An fsszone_t is removed when there are no projects with threads in it.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static pri_t fss_init(id_t, int, classfuncs_t **);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static struct sclass fss = {
2627c478bd9Sstevel@tonic-gate 	"FSS",
2637c478bd9Sstevel@tonic-gate 	fss_init,
2647c478bd9Sstevel@tonic-gate 	0
2657c478bd9Sstevel@tonic-gate };
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate extern struct mod_ops mod_schedops;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate static struct modlsched modlsched = {
2737c478bd9Sstevel@tonic-gate 	&mod_schedops, "fair share scheduling class", &fss
2747c478bd9Sstevel@tonic-gate };
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2777c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlsched, NULL
2787c478bd9Sstevel@tonic-gate };
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #define	FSS_MAXUPRI	60
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * The fssproc_t structures are kept in an array of circular doubly linked
2847c478bd9Sstevel@tonic-gate  * lists.  A hash on the thread pointer is used to determine which list each
2857c478bd9Sstevel@tonic-gate  * thread should be placed in.  Each list has a dummy "head" which is never
2867c478bd9Sstevel@tonic-gate  * removed, so the list is never empty.  fss_update traverses these lists to
2877c478bd9Sstevel@tonic-gate  * update the priorities of threads that have been waiting on the run queue.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate #define	FSS_LISTS		16 /* number of lists, must be power of 2 */
2907c478bd9Sstevel@tonic-gate #define	FSS_LIST_HASH(t)	(((uintptr_t)(t) >> 9) & (FSS_LISTS - 1))
2917c478bd9Sstevel@tonic-gate #define	FSS_LIST_NEXT(i)	(((i) + 1) & (FSS_LISTS - 1))
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #define	FSS_LIST_INSERT(fssproc)				\
2947c478bd9Sstevel@tonic-gate {								\
2957c478bd9Sstevel@tonic-gate 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
2967c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &fss_listlock[index];			\
2977c478bd9Sstevel@tonic-gate 	fssproc_t *headp = &fss_listhead[index];		\
2987c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);					\
2997c478bd9Sstevel@tonic-gate 	fssproc->fss_next = headp->fss_next;			\
3007c478bd9Sstevel@tonic-gate 	fssproc->fss_prev = headp;				\
3017c478bd9Sstevel@tonic-gate 	headp->fss_next->fss_prev = fssproc;			\
3027c478bd9Sstevel@tonic-gate 	headp->fss_next = fssproc;				\
3037c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);					\
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate #define	FSS_LIST_DELETE(fssproc)				\
3077c478bd9Sstevel@tonic-gate {								\
3087c478bd9Sstevel@tonic-gate 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
3097c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &fss_listlock[index];			\
3107c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);					\
3117c478bd9Sstevel@tonic-gate 	fssproc->fss_prev->fss_next = fssproc->fss_next;	\
3127c478bd9Sstevel@tonic-gate 	fssproc->fss_next->fss_prev = fssproc->fss_prev;	\
3137c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);					\
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate #define	FSS_TICK_COST	1000	/* tick cost for threads with nice level = 0 */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * Decay rate percentages are based on n/128 rather than n/100 so  that
3207c478bd9Sstevel@tonic-gate  * calculations can avoid having to do an integer divide by 100 (divide
3217c478bd9Sstevel@tonic-gate  * by FSS_DECAY_BASE == 128 optimizes to an arithmetic shift).
3227c478bd9Sstevel@tonic-gate  *
3237c478bd9Sstevel@tonic-gate  * FSS_DECAY_MIN	=  83/128 ~= 65%
3247c478bd9Sstevel@tonic-gate  * FSS_DECAY_MAX	= 108/128 ~= 85%
3257c478bd9Sstevel@tonic-gate  * FSS_DECAY_USG	=  96/128 ~= 75%
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate #define	FSS_DECAY_MIN	83	/* fsspri decay pct for threads w/ nice -20 */
3287c478bd9Sstevel@tonic-gate #define	FSS_DECAY_MAX	108	/* fsspri decay pct for threads w/ nice +19 */
3297c478bd9Sstevel@tonic-gate #define	FSS_DECAY_USG	96	/* fssusage decay pct for projects */
3307c478bd9Sstevel@tonic-gate #define	FSS_DECAY_BASE	128	/* base for decay percentages above */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate #define	FSS_NICE_MIN	0
3337c478bd9Sstevel@tonic-gate #define	FSS_NICE_MAX	(2 * NZERO - 1)
3347c478bd9Sstevel@tonic-gate #define	FSS_NICE_RANGE	(FSS_NICE_MAX - FSS_NICE_MIN + 1)
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate static int	fss_nice_tick[FSS_NICE_RANGE];
3377c478bd9Sstevel@tonic-gate static int	fss_nice_decay[FSS_NICE_RANGE];
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static pri_t	fss_maxupri = FSS_MAXUPRI; /* maximum FSS user priority */
3407c478bd9Sstevel@tonic-gate static pri_t	fss_maxumdpri; /* maximum user mode fss priority */
3417c478bd9Sstevel@tonic-gate static pri_t	fss_maxglobpri;	/* maximum global priority used by fss class */
3427c478bd9Sstevel@tonic-gate static pri_t	fss_minglobpri;	/* minimum global priority */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static fssproc_t fss_listhead[FSS_LISTS];
3457c478bd9Sstevel@tonic-gate static kmutex_t	fss_listlock[FSS_LISTS];
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static fsspset_t *fsspsets;
3487c478bd9Sstevel@tonic-gate static kmutex_t fsspsets_lock;	/* protects fsspsets */
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static id_t	fss_cid;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static time_t	fss_minrun = 2;	/* t_pri becomes 59 within 2 secs */
3537c478bd9Sstevel@tonic-gate static time_t	fss_minslp = 2;	/* min time on sleep queue for hardswap */
3547c478bd9Sstevel@tonic-gate static int	fss_quantum = 11;
3557c478bd9Sstevel@tonic-gate 
356*482a7749SJerry Jelinek static void	fss_newpri(fssproc_t *, boolean_t);
3577c478bd9Sstevel@tonic-gate static void	fss_update(void *);
3587c478bd9Sstevel@tonic-gate static int	fss_update_list(int);
3597c478bd9Sstevel@tonic-gate static void	fss_change_priority(kthread_t *, fssproc_t *);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static int	fss_admin(caddr_t, cred_t *);
3627c478bd9Sstevel@tonic-gate static int	fss_getclinfo(void *);
3637c478bd9Sstevel@tonic-gate static int	fss_parmsin(void *);
3647c478bd9Sstevel@tonic-gate static int	fss_parmsout(void *, pc_vaparms_t *);
3657c478bd9Sstevel@tonic-gate static int	fss_vaparmsin(void *, pc_vaparms_t *);
3667c478bd9Sstevel@tonic-gate static int	fss_vaparmsout(void *, pc_vaparms_t *);
3677c478bd9Sstevel@tonic-gate static int	fss_getclpri(pcpri_t *);
3687c478bd9Sstevel@tonic-gate static int	fss_alloc(void **, int);
3697c478bd9Sstevel@tonic-gate static void	fss_free(void *);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static int	fss_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
3727c478bd9Sstevel@tonic-gate static void	fss_exitclass(void *);
3737c478bd9Sstevel@tonic-gate static int	fss_canexit(kthread_t *, cred_t *);
3747c478bd9Sstevel@tonic-gate static int	fss_fork(kthread_t *, kthread_t *, void *);
3757c478bd9Sstevel@tonic-gate static void	fss_forkret(kthread_t *, kthread_t *);
3767c478bd9Sstevel@tonic-gate static void	fss_parmsget(kthread_t *, void *);
3777c478bd9Sstevel@tonic-gate static int	fss_parmsset(kthread_t *, void *, id_t, cred_t *);
3787c478bd9Sstevel@tonic-gate static void	fss_stop(kthread_t *, int, int);
3797c478bd9Sstevel@tonic-gate static void	fss_exit(kthread_t *);
3807c478bd9Sstevel@tonic-gate static void	fss_active(kthread_t *);
3817c478bd9Sstevel@tonic-gate static void	fss_inactive(kthread_t *);
3827c478bd9Sstevel@tonic-gate static pri_t	fss_swapin(kthread_t *, int);
3837c478bd9Sstevel@tonic-gate static pri_t	fss_swapout(kthread_t *, int);
3847c478bd9Sstevel@tonic-gate static void	fss_trapret(kthread_t *);
3857c478bd9Sstevel@tonic-gate static void	fss_preempt(kthread_t *);
3867c478bd9Sstevel@tonic-gate static void	fss_setrun(kthread_t *);
3877c478bd9Sstevel@tonic-gate static void	fss_sleep(kthread_t *);
3887c478bd9Sstevel@tonic-gate static void	fss_tick(kthread_t *);
3897c478bd9Sstevel@tonic-gate static void	fss_wakeup(kthread_t *);
3907c478bd9Sstevel@tonic-gate static int	fss_donice(kthread_t *, cred_t *, int, int *);
391d4204c85Sraf static int	fss_doprio(kthread_t *, cred_t *, int, int *);
3927c478bd9Sstevel@tonic-gate static pri_t	fss_globpri(kthread_t *);
3937c478bd9Sstevel@tonic-gate static void	fss_yield(kthread_t *);
3947c478bd9Sstevel@tonic-gate static void	fss_nullsys();
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate static struct classfuncs fss_classfuncs = {
3977c478bd9Sstevel@tonic-gate 	/* class functions */
3987c478bd9Sstevel@tonic-gate 	fss_admin,
3997c478bd9Sstevel@tonic-gate 	fss_getclinfo,
4007c478bd9Sstevel@tonic-gate 	fss_parmsin,
4017c478bd9Sstevel@tonic-gate 	fss_parmsout,
4027c478bd9Sstevel@tonic-gate 	fss_vaparmsin,
4037c478bd9Sstevel@tonic-gate 	fss_vaparmsout,
4047c478bd9Sstevel@tonic-gate 	fss_getclpri,
4057c478bd9Sstevel@tonic-gate 	fss_alloc,
4067c478bd9Sstevel@tonic-gate 	fss_free,
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/* thread functions */
4097c478bd9Sstevel@tonic-gate 	fss_enterclass,
4107c478bd9Sstevel@tonic-gate 	fss_exitclass,
4117c478bd9Sstevel@tonic-gate 	fss_canexit,
4127c478bd9Sstevel@tonic-gate 	fss_fork,
4137c478bd9Sstevel@tonic-gate 	fss_forkret,
4147c478bd9Sstevel@tonic-gate 	fss_parmsget,
4157c478bd9Sstevel@tonic-gate 	fss_parmsset,
4167c478bd9Sstevel@tonic-gate 	fss_stop,
4177c478bd9Sstevel@tonic-gate 	fss_exit,
4187c478bd9Sstevel@tonic-gate 	fss_active,
4197c478bd9Sstevel@tonic-gate 	fss_inactive,
4207c478bd9Sstevel@tonic-gate 	fss_swapin,
4217c478bd9Sstevel@tonic-gate 	fss_swapout,
4227c478bd9Sstevel@tonic-gate 	fss_trapret,
4237c478bd9Sstevel@tonic-gate 	fss_preempt,
4247c478bd9Sstevel@tonic-gate 	fss_setrun,
4257c478bd9Sstevel@tonic-gate 	fss_sleep,
4267c478bd9Sstevel@tonic-gate 	fss_tick,
4277c478bd9Sstevel@tonic-gate 	fss_wakeup,
4287c478bd9Sstevel@tonic-gate 	fss_donice,
4297c478bd9Sstevel@tonic-gate 	fss_globpri,
4307c478bd9Sstevel@tonic-gate 	fss_nullsys,	/* set_process_group */
431d4204c85Sraf 	fss_yield,
432d4204c85Sraf 	fss_doprio,
4337c478bd9Sstevel@tonic-gate };
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate int
4367c478bd9Sstevel@tonic-gate _init()
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate int
4427c478bd9Sstevel@tonic-gate _fini()
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	return (EBUSY);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate int
4487c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4547c478bd9Sstevel@tonic-gate static int
4557c478bd9Sstevel@tonic-gate fss_project_walker(kproject_t *kpj, void *buf)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	return (0);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate void *
4617c478bd9Sstevel@tonic-gate fss_allocbuf(int op, int type)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	fssbuf_t *fssbuf;
4647c478bd9Sstevel@tonic-gate 	void **fsslist;
4657c478bd9Sstevel@tonic-gate 	int cnt;
4667c478bd9Sstevel@tonic-gate 	int i;
4677c478bd9Sstevel@tonic-gate 	size_t size;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	ASSERT(op == FSS_NPSET_BUF || op == FSS_NPROJ_BUF || op == FSS_ONE_BUF);
4707c478bd9Sstevel@tonic-gate 	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
4717c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	fssbuf = kmem_zalloc(sizeof (fssbuf_t), KM_SLEEP);
4747c478bd9Sstevel@tonic-gate 	switch (op) {
4757c478bd9Sstevel@tonic-gate 	case FSS_NPSET_BUF:
4767c478bd9Sstevel@tonic-gate 		cnt = cpupart_list(NULL, 0, CP_NONEMPTY);
4777c478bd9Sstevel@tonic-gate 		break;
4787c478bd9Sstevel@tonic-gate 	case FSS_NPROJ_BUF:
4797c478bd9Sstevel@tonic-gate 		cnt = project_walk_all(ALL_ZONES, fss_project_walker, NULL);
4807c478bd9Sstevel@tonic-gate 		break;
4817c478bd9Sstevel@tonic-gate 	case FSS_ONE_BUF:
4827c478bd9Sstevel@tonic-gate 		cnt = 1;
4837c478bd9Sstevel@tonic-gate 		break;
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	switch (type) {
4877c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_PROJ:
4887c478bd9Sstevel@tonic-gate 		size = sizeof (fssproj_t);
4897c478bd9Sstevel@tonic-gate 		break;
4907c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_ZONE:
4917c478bd9Sstevel@tonic-gate 		size = sizeof (fsszone_t);
4927c478bd9Sstevel@tonic-gate 		break;
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 	fsslist = kmem_zalloc(cnt * sizeof (void *), KM_SLEEP);
4957c478bd9Sstevel@tonic-gate 	fssbuf->fssb_size = cnt;
4967c478bd9Sstevel@tonic-gate 	fssbuf->fssb_list = fsslist;
4977c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++)
4987c478bd9Sstevel@tonic-gate 		fsslist[i] = kmem_zalloc(size, KM_SLEEP);
4997c478bd9Sstevel@tonic-gate 	return (fssbuf);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate void
5037c478bd9Sstevel@tonic-gate fss_freebuf(fssbuf_t *fssbuf, int type)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	void **fsslist;
5067c478bd9Sstevel@tonic-gate 	int i;
5077c478bd9Sstevel@tonic-gate 	size_t size;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	ASSERT(fssbuf != NULL);
5107c478bd9Sstevel@tonic-gate 	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
5117c478bd9Sstevel@tonic-gate 	fsslist = fssbuf->fssb_list;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	switch (type) {
5147c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_PROJ:
5157c478bd9Sstevel@tonic-gate 		size = sizeof (fssproj_t);
5167c478bd9Sstevel@tonic-gate 		break;
5177c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_ZONE:
5187c478bd9Sstevel@tonic-gate 		size = sizeof (fsszone_t);
5197c478bd9Sstevel@tonic-gate 		break;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	for (i = 0; i < fssbuf->fssb_size; i++) {
5237c478bd9Sstevel@tonic-gate 		if (fsslist[i] != NULL)
5247c478bd9Sstevel@tonic-gate 			kmem_free(fsslist[i], size);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	kmem_free(fsslist, sizeof (void *) * fssbuf->fssb_size);
5277c478bd9Sstevel@tonic-gate 	kmem_free(fssbuf, sizeof (fssbuf_t));
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate static fsspset_t *
5317c478bd9Sstevel@tonic-gate fss_find_fsspset(cpupart_t *cpupart)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	int i;
5347c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset = NULL;
5357c478bd9Sstevel@tonic-gate 	int found = 0;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	ASSERT(cpupart != NULL);
5387c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * Search for the cpupart pointer in the array of fsspsets.
5427c478bd9Sstevel@tonic-gate 	 */
5437c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_ncpus; i++) {
5447c478bd9Sstevel@tonic-gate 		fsspset = &fsspsets[i];
5457c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_cpupart == cpupart) {
5467c478bd9Sstevel@tonic-gate 			ASSERT(fsspset->fssps_nproj > 0);
5477c478bd9Sstevel@tonic-gate 			found = 1;
5487c478bd9Sstevel@tonic-gate 			break;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	if (found == 0) {
5527c478bd9Sstevel@tonic-gate 		/*
5537c478bd9Sstevel@tonic-gate 		 * If we didn't find anything, then use the first
5547c478bd9Sstevel@tonic-gate 		 * available slot in the fsspsets array.
5557c478bd9Sstevel@tonic-gate 		 */
5567c478bd9Sstevel@tonic-gate 		for (i = 0; i < max_ncpus; i++) {
5577c478bd9Sstevel@tonic-gate 			fsspset = &fsspsets[i];
5587c478bd9Sstevel@tonic-gate 			if (fsspset->fssps_cpupart == NULL) {
5597c478bd9Sstevel@tonic-gate 				ASSERT(fsspset->fssps_nproj == 0);
5607c478bd9Sstevel@tonic-gate 				found = 1;
5617c478bd9Sstevel@tonic-gate 				break;
5627c478bd9Sstevel@tonic-gate 			}
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 		fsspset->fssps_cpupart = cpupart;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	ASSERT(found == 1);
5677c478bd9Sstevel@tonic-gate 	return (fsspset);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate static void
5717c478bd9Sstevel@tonic-gate fss_del_fsspset(fsspset_t *fsspset)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
5747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
5757c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_nproj == 0);
5767c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_list == NULL);
5777c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_zones == NULL);
5787c478bd9Sstevel@tonic-gate 	fsspset->fssps_cpupart = NULL;
5797c478bd9Sstevel@tonic-gate 	fsspset->fssps_maxfsspri = 0;
5807c478bd9Sstevel@tonic-gate 	fsspset->fssps_shares = 0;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * The following routine returns a pointer to the fsszone structure which
5857c478bd9Sstevel@tonic-gate  * belongs to zone "zone" and cpu partition fsspset, if such structure exists.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static fsszone_t *
5887c478bd9Sstevel@tonic-gate fss_find_fsszone(fsspset_t *fsspset, zone_t *zone)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list != NULL) {
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * There are projects/zones active on this cpu partition
5977c478bd9Sstevel@tonic-gate 		 * already.  Try to find our zone among them.
5987c478bd9Sstevel@tonic-gate 		 */
5997c478bd9Sstevel@tonic-gate 		fsszone = fsspset->fssps_zones;
6007c478bd9Sstevel@tonic-gate 		do {
6017c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_zone == zone) {
6027c478bd9Sstevel@tonic-gate 				return (fsszone);
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 			fsszone = fsszone->fssz_next;
6057c478bd9Sstevel@tonic-gate 		} while (fsszone != fsspset->fssps_zones);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	return (NULL);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * The following routine links new fsszone structure into doubly linked list of
6127c478bd9Sstevel@tonic-gate  * zones active on the specified cpu partition.
6137c478bd9Sstevel@tonic-gate  */
6147c478bd9Sstevel@tonic-gate static void
6157c478bd9Sstevel@tonic-gate fss_insert_fsszone(fsspset_t *fsspset, zone_t *zone, fsszone_t *fsszone)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	fsszone->fssz_zone = zone;
6207c478bd9Sstevel@tonic-gate 	fsszone->fssz_rshares = zone->zone_shares;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_zones == NULL) {
6237c478bd9Sstevel@tonic-gate 		/*
6247c478bd9Sstevel@tonic-gate 		 * This will be the first fsszone for this fsspset
6257c478bd9Sstevel@tonic-gate 		 */
6267c478bd9Sstevel@tonic-gate 		fsszone->fssz_next = fsszone->fssz_prev = fsszone;
6277c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = fsszone;
6287c478bd9Sstevel@tonic-gate 	} else {
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * Insert this fsszone to the doubly linked list.
6317c478bd9Sstevel@tonic-gate 		 */
6327c478bd9Sstevel@tonic-gate 		fsszone_t *fssz_head = fsspset->fssps_zones;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		fsszone->fssz_next = fssz_head;
6357c478bd9Sstevel@tonic-gate 		fsszone->fssz_prev = fssz_head->fssz_prev;
6367c478bd9Sstevel@tonic-gate 		fssz_head->fssz_prev->fssz_next = fsszone;
6377c478bd9Sstevel@tonic-gate 		fssz_head->fssz_prev = fsszone;
6387c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = fsszone;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * The following routine removes a single fsszone structure from the doubly
6447c478bd9Sstevel@tonic-gate  * linked list of zones active on the specified cpu partition.  Note that
6457c478bd9Sstevel@tonic-gate  * global fsspsets_lock must be held in case this fsszone structure is the last
6467c478bd9Sstevel@tonic-gate  * on the above mentioned list.  Also note that the fsszone structure is not
6477c478bd9Sstevel@tonic-gate  * freed here, it is the responsibility of the caller to call kmem_free for it.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate static void
6507c478bd9Sstevel@tonic-gate fss_remove_fsszone(fsspset_t *fsspset, fsszone_t *fsszone)
6517c478bd9Sstevel@tonic-gate {
6527c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
6537c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_nproj == 0);
6547c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_shares == 0);
6557c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_runnable == 0);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (fsszone->fssz_next != fsszone) {
6587c478bd9Sstevel@tonic-gate 		/*
6597c478bd9Sstevel@tonic-gate 		 * This is not the last zone in the list.
6607c478bd9Sstevel@tonic-gate 		 */
6617c478bd9Sstevel@tonic-gate 		fsszone->fssz_prev->fssz_next = fsszone->fssz_next;
6627c478bd9Sstevel@tonic-gate 		fsszone->fssz_next->fssz_prev = fsszone->fssz_prev;
6637c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_zones == fsszone)
6647c478bd9Sstevel@tonic-gate 			fsspset->fssps_zones = fsszone->fssz_next;
6657c478bd9Sstevel@tonic-gate 	} else {
6667c478bd9Sstevel@tonic-gate 		/*
6677c478bd9Sstevel@tonic-gate 		 * This was the last zone active in this cpu partition.
6687c478bd9Sstevel@tonic-gate 		 */
6697c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = NULL;
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /*
6747c478bd9Sstevel@tonic-gate  * The following routine returns a pointer to the fssproj structure
6757c478bd9Sstevel@tonic-gate  * which belongs to project kpj and cpu partition fsspset, if such structure
6767c478bd9Sstevel@tonic-gate  * exists.
6777c478bd9Sstevel@tonic-gate  */
6787c478bd9Sstevel@tonic-gate static fssproj_t *
6797c478bd9Sstevel@tonic-gate fss_find_fssproj(fsspset_t *fsspset, kproject_t *kpj)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list != NULL) {
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * There are projects running on this cpu partition already.
6887c478bd9Sstevel@tonic-gate 		 * Try to find our project among them.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		fssproj = fsspset->fssps_list;
6917c478bd9Sstevel@tonic-gate 		do {
6927c478bd9Sstevel@tonic-gate 			if (fssproj->fssp_proj == kpj) {
6937c478bd9Sstevel@tonic-gate 				ASSERT(fssproj->fssp_pset == fsspset);
6947c478bd9Sstevel@tonic-gate 				return (fssproj);
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 			fssproj = fssproj->fssp_next;
6977c478bd9Sstevel@tonic-gate 		} while (fssproj != fsspset->fssps_list);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	return (NULL);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate  * The following routine links new fssproj structure into doubly linked list
7047c478bd9Sstevel@tonic-gate  * of projects running on the specified cpu partition.
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate static void
7077c478bd9Sstevel@tonic-gate fss_insert_fssproj(fsspset_t *fsspset, kproject_t *kpj, fsszone_t *fsszone,
7087c478bd9Sstevel@tonic-gate     fssproj_t *fssproj)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	fssproj->fssp_pset = fsspset;
7137c478bd9Sstevel@tonic-gate 	fssproj->fssp_proj = kpj;
7147c478bd9Sstevel@tonic-gate 	fssproj->fssp_shares = kpj->kpj_shares;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	fsspset->fssps_nproj++;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list == NULL) {
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * This will be the first fssproj for this fsspset
7217c478bd9Sstevel@tonic-gate 		 */
7227c478bd9Sstevel@tonic-gate 		fssproj->fssp_next = fssproj->fssp_prev = fssproj;
7237c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = fssproj;
7247c478bd9Sstevel@tonic-gate 	} else {
7257c478bd9Sstevel@tonic-gate 		/*
7267c478bd9Sstevel@tonic-gate 		 * Insert this fssproj to the doubly linked list.
7277c478bd9Sstevel@tonic-gate 		 */
7287c478bd9Sstevel@tonic-gate 		fssproj_t *fssp_head = fsspset->fssps_list;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		fssproj->fssp_next = fssp_head;
7317c478bd9Sstevel@tonic-gate 		fssproj->fssp_prev = fssp_head->fssp_prev;
7327c478bd9Sstevel@tonic-gate 		fssp_head->fssp_prev->fssp_next = fssproj;
7337c478bd9Sstevel@tonic-gate 		fssp_head->fssp_prev = fssproj;
7347c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = fssproj;
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 	fssproj->fssp_fsszone = fsszone;
7377c478bd9Sstevel@tonic-gate 	fsszone->fssz_nproj++;
7387c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_nproj != 0);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * The following routine removes a single fssproj structure from the doubly
7437c478bd9Sstevel@tonic-gate  * linked list of projects running on the specified cpu partition.  Note that
7447c478bd9Sstevel@tonic-gate  * global fsspsets_lock must be held in case if this fssproj structure is the
7457c478bd9Sstevel@tonic-gate  * last on the above mentioned list.  Also note that the fssproj structure is
7467c478bd9Sstevel@tonic-gate  * not freed here, it is the responsibility of the caller to call kmem_free
7477c478bd9Sstevel@tonic-gate  * for it.
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate static void
7507c478bd9Sstevel@tonic-gate fss_remove_fssproj(fsspset_t *fsspset, fssproj_t *fssproj)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
7557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
7567c478bd9Sstevel@tonic-gate 	ASSERT(fssproj->fssp_runnable == 0);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	fsspset->fssps_nproj--;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
7617c478bd9Sstevel@tonic-gate 	fsszone->fssz_nproj--;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if (fssproj->fssp_next != fssproj) {
7647c478bd9Sstevel@tonic-gate 		/*
7657c478bd9Sstevel@tonic-gate 		 * This is not the last part in the list.
7667c478bd9Sstevel@tonic-gate 		 */
7677c478bd9Sstevel@tonic-gate 		fssproj->fssp_prev->fssp_next = fssproj->fssp_next;
7687c478bd9Sstevel@tonic-gate 		fssproj->fssp_next->fssp_prev = fssproj->fssp_prev;
7697c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_list == fssproj)
7707c478bd9Sstevel@tonic-gate 			fsspset->fssps_list = fssproj->fssp_next;
7717c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
7727c478bd9Sstevel@tonic-gate 			fss_remove_fsszone(fsspset, fsszone);
7737c478bd9Sstevel@tonic-gate 	} else {
7747c478bd9Sstevel@tonic-gate 		/*
7757c478bd9Sstevel@tonic-gate 		 * This was the last project part running
7767c478bd9Sstevel@tonic-gate 		 * at this cpu partition.
7777c478bd9Sstevel@tonic-gate 		 */
7787c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = NULL;
7797c478bd9Sstevel@tonic-gate 		ASSERT(fsspset->fssps_nproj == 0);
7807c478bd9Sstevel@tonic-gate 		ASSERT(fsszone->fssz_nproj == 0);
7817c478bd9Sstevel@tonic-gate 		fss_remove_fsszone(fsspset, fsszone);
7827c478bd9Sstevel@tonic-gate 		fss_del_fsspset(fsspset);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate static void
7877c478bd9Sstevel@tonic-gate fss_inactive(kthread_t *t)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
7907c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
7917c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
7927c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
7957c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
7967c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
7977c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)	/* if this thread already exited */
7987c478bd9Sstevel@tonic-gate 		return;
7997c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
8007c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
8017c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
8027c478bd9Sstevel@tonic-gate 	ASSERT(fssproj->fssp_runnable > 0);
8037c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_runnable == 0) {
8047c478bd9Sstevel@tonic-gate 		fsszone->fssz_shares -= fssproj->fssp_shares;
8057c478bd9Sstevel@tonic-gate 		if (--fsszone->fssz_runnable == 0)
8067c478bd9Sstevel@tonic-gate 			fsspset->fssps_shares -= fsszone->fssz_rshares;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	ASSERT(fssproc->fss_runnable == 1);
8097c478bd9Sstevel@tonic-gate 	fssproc->fss_runnable = 0;
8107c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate static void
8147c478bd9Sstevel@tonic-gate fss_active(kthread_t *t)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
8177c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
8187c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
8197c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
8227c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
8237c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
8247c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)	/* if this thread already exited */
8257c478bd9Sstevel@tonic-gate 		return;
8267c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
8277c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
8287c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
8297c478bd9Sstevel@tonic-gate 	if (++fssproj->fssp_runnable == 1) {
8307c478bd9Sstevel@tonic-gate 		fsszone->fssz_shares += fssproj->fssp_shares;
8317c478bd9Sstevel@tonic-gate 		if (++fsszone->fssz_runnable == 1)
8327c478bd9Sstevel@tonic-gate 			fsspset->fssps_shares += fsszone->fssz_rshares;
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 	ASSERT(fssproc->fss_runnable == 0);
8357c478bd9Sstevel@tonic-gate 	fssproc->fss_runnable = 1;
8367c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * Fair share scheduler initialization. Called by dispinit() at boot time.
8417c478bd9Sstevel@tonic-gate  * We can ignore clparmsz argument since we know that the smallest possible
8427c478bd9Sstevel@tonic-gate  * parameter buffer is big enough for us.
8437c478bd9Sstevel@tonic-gate  */
8447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8457c478bd9Sstevel@tonic-gate static pri_t
8467c478bd9Sstevel@tonic-gate fss_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	int i;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	fss_cid = cid;
8537c478bd9Sstevel@tonic-gate 	fss_maxumdpri = minclsyspri - 1;
8547c478bd9Sstevel@tonic-gate 	fss_maxglobpri = minclsyspri;
8557c478bd9Sstevel@tonic-gate 	fss_minglobpri = 0;
8567c478bd9Sstevel@tonic-gate 	fsspsets = kmem_zalloc(sizeof (fsspset_t) * max_ncpus, KM_SLEEP);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc hash table.
8607c478bd9Sstevel@tonic-gate 	 */
8617c478bd9Sstevel@tonic-gate 	for (i = 0; i < FSS_LISTS; i++)
8627c478bd9Sstevel@tonic-gate 		fss_listhead[i].fss_next = fss_listhead[i].fss_prev =
8637c478bd9Sstevel@tonic-gate 		    &fss_listhead[i];
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	*clfuncspp = &fss_classfuncs;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * Fill in fss_nice_tick and fss_nice_decay arrays:
8697c478bd9Sstevel@tonic-gate 	 * The cost of a tick is lower at positive nice values (so that it
8707c478bd9Sstevel@tonic-gate 	 * will not increase its project's usage as much as normal) with 50%
8717c478bd9Sstevel@tonic-gate 	 * drop at the maximum level and 50% increase at the minimum level.
8727c478bd9Sstevel@tonic-gate 	 * The fsspri decay is slower at positive nice values.  fsspri values
8737c478bd9Sstevel@tonic-gate 	 * of processes with negative nice levels must decay faster to receive
8747c478bd9Sstevel@tonic-gate 	 * time slices more frequently than normal.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 	for (i = 0; i < FSS_NICE_RANGE; i++) {
8777c478bd9Sstevel@tonic-gate 		fss_nice_tick[i] = (FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2)
8787c478bd9Sstevel@tonic-gate 		    - i)) / FSS_NICE_RANGE;
8797c478bd9Sstevel@tonic-gate 		fss_nice_decay[i] = FSS_DECAY_MIN +
8807c478bd9Sstevel@tonic-gate 		    ((FSS_DECAY_MAX - FSS_DECAY_MIN) * i) /
8817c478bd9Sstevel@tonic-gate 		    (FSS_NICE_RANGE - 1);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	return (fss_maxglobpri);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
888*482a7749SJerry Jelinek  * Calculate the new fss_umdpri based on the usage, the normalized share usage
889*482a7749SJerry Jelinek  * and the number of active threads.  Reset the tick counter for this thread.
890*482a7749SJerry Jelinek  *
891*482a7749SJerry Jelinek  * When calculating the new priority using the standard formula we can hit
892*482a7749SJerry Jelinek  * a scenario where we don't have good round-robin behavior.  This would be
893*482a7749SJerry Jelinek  * most commonly seen when there is a zone with lots of runnable threads.
894*482a7749SJerry Jelinek  * In the bad scenario we will see the following behavior when using the
895*482a7749SJerry Jelinek  * standard formula and these conditions:
896*482a7749SJerry Jelinek  *
897*482a7749SJerry Jelinek  *	- there are multiple runnable threads in the zone (project)
898*482a7749SJerry Jelinek  *	- the fssps_maxfsspri is a very large value
899*482a7749SJerry Jelinek  *	- (we also know all of these threads will use the project's
900*482a7749SJerry Jelinek  *	    fssp_shusage)
901*482a7749SJerry Jelinek  *
902*482a7749SJerry Jelinek  * Under these conditions, a thread with a low fss_fsspri value is chosen
903*482a7749SJerry Jelinek  * to run and the thread gets a high fss_umdpri.  This thread can run for
904*482a7749SJerry Jelinek  * its full quanta (fss_timeleft) at which time fss_newpri is called to
905*482a7749SJerry Jelinek  * calculate the thread's new priority.
906*482a7749SJerry Jelinek  *
907*482a7749SJerry Jelinek  * In this case, because the newly calculated fsspri value is much smaller
908*482a7749SJerry Jelinek  * (orders of magnitude) than the fssps_maxfsspri value, if we used the
909*482a7749SJerry Jelinek  * standard formula the thread will still get a high fss_umdpri value and
910*482a7749SJerry Jelinek  * will run again for another quanta, even though there are other runnable
911*482a7749SJerry Jelinek  * threads in the project.
912*482a7749SJerry Jelinek  *
913*482a7749SJerry Jelinek  * For a thread that is runnable for a long time, the thread can continue
914*482a7749SJerry Jelinek  * to run for many quanta (totaling many seconds) before the thread's fsspri
915*482a7749SJerry Jelinek  * exceeds the fssps_maxfsspri and the thread's fss_umdpri is reset back
916*482a7749SJerry Jelinek  * down to 1.  This behavior also keeps the fssps_maxfsspr at a high value,
917*482a7749SJerry Jelinek  * so that the next runnable thread might repeat this cycle.
918*482a7749SJerry Jelinek  *
919*482a7749SJerry Jelinek  * This leads to the case where we don't have round-robin behavior at quanta
920*482a7749SJerry Jelinek  * granularity, but instead, runnable threads within the project only run
921*482a7749SJerry Jelinek  * at several second intervals.
922*482a7749SJerry Jelinek  *
923*482a7749SJerry Jelinek  * To prevent this scenario from occuring, when a thread has consumed its
924*482a7749SJerry Jelinek  * quanta and there are multiple runnable threads in the project, we
925*482a7749SJerry Jelinek  * immediately cause the thread to hit fssps_maxfsspri so that it gets
926*482a7749SJerry Jelinek  * reset back to 1 and another runnable thread in the project can run.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate static void
929*482a7749SJerry Jelinek fss_newpri(fssproc_t *fssproc, boolean_t quanta_up)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	kthread_t *tp;
9327c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
9337c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
9347c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
9357c478bd9Sstevel@tonic-gate 	fsspri_t fsspri, maxfsspri;
936*482a7749SJerry Jelinek 	uint32_t n_runnable;
9377c478bd9Sstevel@tonic-gate 	pri_t invpri;
9387c478bd9Sstevel@tonic-gate 	uint32_t ticks;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	tp = fssproc->fss_tp;
9417c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (tp->t_cid != fss_cid)
9447c478bd9Sstevel@tonic-gate 		return;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
9497c478bd9Sstevel@tonic-gate 	fsszone = FSSPROJ2FSSZONE(fssproj);
9507c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)
9517c478bd9Sstevel@tonic-gate 		/*
9527c478bd9Sstevel@tonic-gate 		 * No need to change priority of exited threads.
9537c478bd9Sstevel@tonic-gate 		 */
9547c478bd9Sstevel@tonic-gate 		return;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
9577c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
9587c478bd9Sstevel@tonic-gate 
959*482a7749SJerry Jelinek 	ticks = fssproc->fss_ticks;
960*482a7749SJerry Jelinek 	fssproc->fss_ticks = 0;
961*482a7749SJerry Jelinek 
9627c478bd9Sstevel@tonic-gate 	if (fssproj->fssp_shares == 0 || fsszone->fssz_rshares == 0) {
9637c478bd9Sstevel@tonic-gate 		/*
9647c478bd9Sstevel@tonic-gate 		 * Special case: threads with no shares.
9657c478bd9Sstevel@tonic-gate 		 */
9667c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = fss_minglobpri;
9677c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
9687c478bd9Sstevel@tonic-gate 		return;
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
971*482a7749SJerry Jelinek 	maxfsspri = fsspset->fssps_maxfsspri;
972*482a7749SJerry Jelinek 	n_runnable = fssproj->fssp_runnable;
973*482a7749SJerry Jelinek 
974*482a7749SJerry Jelinek 	if (quanta_up && n_runnable > 1) {
975*482a7749SJerry Jelinek 		fsspri = maxfsspri;
976*482a7749SJerry Jelinek 	} else {
9777c478bd9Sstevel@tonic-gate 		/*
978*482a7749SJerry Jelinek 		 * fsspri += fssp_shusage * nrunnable * ticks
979*482a7749SJerry Jelinek 		 * If all three values are non-0, this typically calculates to
980*482a7749SJerry Jelinek 		 * a large number (sometimes > 1M, sometimes > 100B) due to
981*482a7749SJerry Jelinek 		 * fssp_shusage which can be > 1T.
9827c478bd9Sstevel@tonic-gate 		 */
9837c478bd9Sstevel@tonic-gate 		fsspri = fssproc->fss_fsspri;
984*482a7749SJerry Jelinek 		fsspri += fssproj->fssp_shusage * n_runnable * ticks;
985*482a7749SJerry Jelinek 	}
986*482a7749SJerry Jelinek 
9877c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = fsspri;
9887c478bd9Sstevel@tonic-gate 
989*482a7749SJerry Jelinek 	/*
990*482a7749SJerry Jelinek 	 * fss_maxumdpri is normally 59, since FSS priorities are 0-59.
991*482a7749SJerry Jelinek 	 * If the previous calculation resulted in 0 (e.g. was 0 and added 0
992*482a7749SJerry Jelinek 	 * because ticks == 0), then instead of 0, we use the largest priority,
993*482a7749SJerry Jelinek 	 * which is still small in comparison to the large numbers we typically
994*482a7749SJerry Jelinek 	 * see.
995*482a7749SJerry Jelinek 	 */
9967c478bd9Sstevel@tonic-gate 	if (fsspri < fss_maxumdpri)
9977c478bd9Sstevel@tonic-gate 		fsspri = fss_maxumdpri;	/* so that maxfsspri is != 0 */
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
10007c478bd9Sstevel@tonic-gate 	 * The general priority formula:
10017c478bd9Sstevel@tonic-gate 	 *
10027c478bd9Sstevel@tonic-gate 	 *			(fsspri * umdprirange)
10037c478bd9Sstevel@tonic-gate 	 *   pri = maxumdpri - ------------------------
10047c478bd9Sstevel@tonic-gate 	 *				maxfsspri
10057c478bd9Sstevel@tonic-gate 	 *
10067c478bd9Sstevel@tonic-gate 	 * If this thread's fsspri is greater than the previous largest
10077c478bd9Sstevel@tonic-gate 	 * fsspri, then record it as the new high and priority for this
10087c478bd9Sstevel@tonic-gate 	 * thread will be one (the lowest priority assigned to a thread
1009*482a7749SJerry Jelinek 	 * that has non-zero shares). Because of this check, maxfsspri can
1010*482a7749SJerry Jelinek 	 * change as this function is called via the
1011*482a7749SJerry Jelinek 	 * fss_update -> fss_update_list -> fss_newpri code path to update
1012*482a7749SJerry Jelinek 	 * all runnable threads. See the code in fss_update for how we
1013*482a7749SJerry Jelinek 	 * mitigate this issue.
1014*482a7749SJerry Jelinek 	 *
10157c478bd9Sstevel@tonic-gate 	 * Note that this formula cannot produce out of bounds priority
1016*482a7749SJerry Jelinek 	 * values (0-59); if it is changed, additional checks may need to be
10177c478bd9Sstevel@tonic-gate 	 * added.
10187c478bd9Sstevel@tonic-gate 	 */
10197c478bd9Sstevel@tonic-gate 	if (fsspri >= maxfsspri) {
10207c478bd9Sstevel@tonic-gate 		fsspset->fssps_maxfsspri = fsspri;
10217c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
10227c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = 1;
10237c478bd9Sstevel@tonic-gate 	} else {
10247c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
10257c478bd9Sstevel@tonic-gate 		invpri = (fsspri * (fss_maxumdpri - 1)) / maxfsspri;
10267c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = fss_maxumdpri - invpri;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate /*
1031*482a7749SJerry Jelinek  * Decays usages of all running projects, resets their tick counters and
1032*482a7749SJerry Jelinek  * calcluates the projects normalized share usage. Called once per second from
1033*482a7749SJerry Jelinek  * fss_update().
10347c478bd9Sstevel@tonic-gate  */
10357c478bd9Sstevel@tonic-gate static void
10367c478bd9Sstevel@tonic-gate fss_decay_usage()
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	uint32_t zone_ext_shares, zone_int_shares;
10397c478bd9Sstevel@tonic-gate 	uint32_t kpj_shares, pset_shares;
10407c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
10417c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
10427c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
10437c478bd9Sstevel@tonic-gate 	fsspri_t maxfsspri;
10447c478bd9Sstevel@tonic-gate 	int psetid;
1045*482a7749SJerry Jelinek 	struct zone *zp;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
10487c478bd9Sstevel@tonic-gate 	/*
10497c478bd9Sstevel@tonic-gate 	 * Go through all active processor sets and decay usages of projects
10507c478bd9Sstevel@tonic-gate 	 * running on them.
10517c478bd9Sstevel@tonic-gate 	 */
10527c478bd9Sstevel@tonic-gate 	for (psetid = 0; psetid < max_ncpus; psetid++) {
10537c478bd9Sstevel@tonic-gate 		fsspset = &fsspsets[psetid];
10547c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
10557c478bd9Sstevel@tonic-gate 
1056*482a7749SJerry Jelinek 		fsspset->fssps_gen++;
1057*482a7749SJerry Jelinek 
10587c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_cpupart == NULL ||
10597c478bd9Sstevel@tonic-gate 		    (fssproj = fsspset->fssps_list) == NULL) {
10607c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
10617c478bd9Sstevel@tonic-gate 			continue;
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 		/*
10657c478bd9Sstevel@tonic-gate 		 * Decay maxfsspri for this cpu partition with the
10667c478bd9Sstevel@tonic-gate 		 * fastest possible decay rate.
10677c478bd9Sstevel@tonic-gate 		 */
10687c478bd9Sstevel@tonic-gate 		disp_lock_enter(&fsspset->fssps_displock);
10697c478bd9Sstevel@tonic-gate 
1070*482a7749SJerry Jelinek 		pset_shares = fsspset->fssps_shares;
1071*482a7749SJerry Jelinek 
10727c478bd9Sstevel@tonic-gate 		maxfsspri = (fsspset->fssps_maxfsspri *
10737c478bd9Sstevel@tonic-gate 		    fss_nice_decay[NZERO]) / FSS_DECAY_BASE;
10747c478bd9Sstevel@tonic-gate 		if (maxfsspri < fss_maxumdpri)
10757c478bd9Sstevel@tonic-gate 			maxfsspri = fss_maxumdpri;
10767c478bd9Sstevel@tonic-gate 		fsspset->fssps_maxfsspri = maxfsspri;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		do {
1079*482a7749SJerry Jelinek 			fsszone = fssproj->fssp_fsszone;
1080*482a7749SJerry Jelinek 			zp = fsszone->fssz_zone;
1081*482a7749SJerry Jelinek 
10827c478bd9Sstevel@tonic-gate 			/*
1083*482a7749SJerry Jelinek 			 * Reset zone's FSS stats if they are from a
1084*482a7749SJerry Jelinek 			 * previous cycle.
1085*482a7749SJerry Jelinek 			 */
1086*482a7749SJerry Jelinek 			if (fsspset->fssps_gen != zp->zone_fss_gen) {
1087*482a7749SJerry Jelinek 				zp->zone_fss_gen = fsspset->fssps_gen;
1088*482a7749SJerry Jelinek 				zp->zone_run_ticks = 0;
1089*482a7749SJerry Jelinek 			}
1090*482a7749SJerry Jelinek 
1091*482a7749SJerry Jelinek 			/*
1092*482a7749SJerry Jelinek 			 * Decay project usage, then add in this cycle's
1093*482a7749SJerry Jelinek 			 * nice tick value.
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			fssproj->fssp_usage =
10967c478bd9Sstevel@tonic-gate 			    (fssproj->fssp_usage * FSS_DECAY_USG) /
1097*482a7749SJerry Jelinek 			    FSS_DECAY_BASE +
1098*482a7749SJerry Jelinek 			    fssproj->fssp_ticks;
10997c478bd9Sstevel@tonic-gate 
1100*482a7749SJerry Jelinek 			fssproj->fssp_ticks = 0;
1101*482a7749SJerry Jelinek 			zp->zone_run_ticks += fssproj->fssp_tick_cnt;
1102*482a7749SJerry Jelinek 			fssproj->fssp_tick_cnt = 0;
1103*482a7749SJerry Jelinek 
11047c478bd9Sstevel@tonic-gate 			/*
11057c478bd9Sstevel@tonic-gate 			 * Readjust the project's number of shares if it has
11067c478bd9Sstevel@tonic-gate 			 * changed since we checked it last time.
11077c478bd9Sstevel@tonic-gate 			 */
11087c478bd9Sstevel@tonic-gate 			kpj_shares = fssproj->fssp_proj->kpj_shares;
11097c478bd9Sstevel@tonic-gate 			if (fssproj->fssp_shares != kpj_shares) {
11107c478bd9Sstevel@tonic-gate 				if (fssproj->fssp_runnable != 0) {
11117c478bd9Sstevel@tonic-gate 					fsszone->fssz_shares -=
11127c478bd9Sstevel@tonic-gate 					    fssproj->fssp_shares;
11137c478bd9Sstevel@tonic-gate 					fsszone->fssz_shares += kpj_shares;
11147c478bd9Sstevel@tonic-gate 				}
11157c478bd9Sstevel@tonic-gate 				fssproj->fssp_shares = kpj_shares;
11167c478bd9Sstevel@tonic-gate 			}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 			/*
11197c478bd9Sstevel@tonic-gate 			 * Readjust the zone's number of shares if it
11207c478bd9Sstevel@tonic-gate 			 * has changed since we checked it last time.
11217c478bd9Sstevel@tonic-gate 			 */
1122*482a7749SJerry Jelinek 			zone_ext_shares = zp->zone_shares;
11237c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_rshares != zone_ext_shares) {
11247c478bd9Sstevel@tonic-gate 				if (fsszone->fssz_runnable != 0) {
11257c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
11267c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
11277c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares +=
11287c478bd9Sstevel@tonic-gate 					    zone_ext_shares;
1129*482a7749SJerry Jelinek 					pset_shares = fsspset->fssps_shares;
11307c478bd9Sstevel@tonic-gate 				}
11317c478bd9Sstevel@tonic-gate 				fsszone->fssz_rshares = zone_ext_shares;
11327c478bd9Sstevel@tonic-gate 			}
11337c478bd9Sstevel@tonic-gate 			zone_int_shares = fsszone->fssz_shares;
1134*482a7749SJerry Jelinek 
1135*482a7749SJerry Jelinek 			/*
1136*482a7749SJerry Jelinek 			 * If anything is runnable in the project, track the
1137*482a7749SJerry Jelinek 			 * overall project share percent for monitoring useage.
1138*482a7749SJerry Jelinek 			 */
1139*482a7749SJerry Jelinek 			if (fssproj->fssp_runnable > 0) {
1140*482a7749SJerry Jelinek 				uint32_t zone_shr_pct;
1141*482a7749SJerry Jelinek 				uint32_t int_shr_pct;
1142*482a7749SJerry Jelinek 
1143*482a7749SJerry Jelinek 				/*
1144*482a7749SJerry Jelinek 				 * Times 1000 to get tenths of a percent
1145*482a7749SJerry Jelinek 				 *
1146*482a7749SJerry Jelinek 				 *		  zone_ext_shares
1147*482a7749SJerry Jelinek 				 * zone_shr_pct = ---------------
1148*482a7749SJerry Jelinek 				 *		  pset_shares
1149*482a7749SJerry Jelinek 				 *
1150*482a7749SJerry Jelinek 				 *		  kpj_shares
1151*482a7749SJerry Jelinek 				 * int_shr_pct =  ---------------
1152*482a7749SJerry Jelinek 				 *		  zone_int_shares
1153*482a7749SJerry Jelinek 				 */
1154*482a7749SJerry Jelinek 				if (pset_shares == 0 || zone_int_shares == 0) {
1155*482a7749SJerry Jelinek 					fssproj->fssp_shr_pct = 0;
1156*482a7749SJerry Jelinek 				} else {
1157*482a7749SJerry Jelinek 					zone_shr_pct =
1158*482a7749SJerry Jelinek 					    (zone_ext_shares * 1000) /
1159*482a7749SJerry Jelinek 					    pset_shares;
1160*482a7749SJerry Jelinek 					int_shr_pct = (kpj_shares * 1000) /
1161*482a7749SJerry Jelinek 					    zone_int_shares;
1162*482a7749SJerry Jelinek 					fssproj->fssp_shr_pct =
1163*482a7749SJerry Jelinek 					    (zone_shr_pct * int_shr_pct) /
1164*482a7749SJerry Jelinek 					    1000;
1165*482a7749SJerry Jelinek 				}
1166*482a7749SJerry Jelinek 			} else {
1167*482a7749SJerry Jelinek 				DTRACE_PROBE1(fss__prj__norun, fssproj_t *,
1168*482a7749SJerry Jelinek 				    fssproj);
1169*482a7749SJerry Jelinek 			}
1170*482a7749SJerry Jelinek 
11717c478bd9Sstevel@tonic-gate 			/*
11727c478bd9Sstevel@tonic-gate 			 * Calculate fssp_shusage value to be used
11737c478bd9Sstevel@tonic-gate 			 * for fsspri increments for the next second.
11747c478bd9Sstevel@tonic-gate 			 */
1175a14d39b7Sandrei 			if (kpj_shares == 0 || zone_ext_shares == 0) {
1176a14d39b7Sandrei 				fssproj->fssp_shusage = 0;
1177a14d39b7Sandrei 			} else if (FSSPROJ2KPROJ(fssproj) == proj0p) {
1178*482a7749SJerry Jelinek 				uint32_t zone_shr_pct;
1179*482a7749SJerry Jelinek 
11807c478bd9Sstevel@tonic-gate 				/*
11817c478bd9Sstevel@tonic-gate 				 * Project 0 in the global zone has 50%
1182*482a7749SJerry Jelinek 				 * of its zone. See calculation above for
1183*482a7749SJerry Jelinek 				 * the zone's share percent.
11847c478bd9Sstevel@tonic-gate 				 */
1185*482a7749SJerry Jelinek 				if (pset_shares == 0)
1186*482a7749SJerry Jelinek 					zone_shr_pct = 1000;
1187*482a7749SJerry Jelinek 				else
1188*482a7749SJerry Jelinek 					zone_shr_pct =
1189*482a7749SJerry Jelinek 					    (zone_ext_shares * 1000) /
1190*482a7749SJerry Jelinek 					    pset_shares;
1191*482a7749SJerry Jelinek 
1192*482a7749SJerry Jelinek 				fssproj->fssp_shr_pct = zone_shr_pct / 2;
1193*482a7749SJerry Jelinek 
11947c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = (fssproj->fssp_usage *
11957c478bd9Sstevel@tonic-gate 				    zone_int_shares * zone_int_shares) /
11967c478bd9Sstevel@tonic-gate 				    (zone_ext_shares * zone_ext_shares);
11977c478bd9Sstevel@tonic-gate 			} else {
11987c478bd9Sstevel@tonic-gate 				/*
11997c478bd9Sstevel@tonic-gate 				 * Thread's priority is based on its project's
12007c478bd9Sstevel@tonic-gate 				 * normalized usage (shusage) value which gets
12017c478bd9Sstevel@tonic-gate 				 * calculated this way:
12027c478bd9Sstevel@tonic-gate 				 *
12037c478bd9Sstevel@tonic-gate 				 *	   pset_shares^2    zone_int_shares^2
12047c478bd9Sstevel@tonic-gate 				 * usage * ------------- * ------------------
12057c478bd9Sstevel@tonic-gate 				 *	   kpj_shares^2	    zone_ext_shares^2
12067c478bd9Sstevel@tonic-gate 				 *
12077c478bd9Sstevel@tonic-gate 				 * Where zone_int_shares is the sum of shares
12087c478bd9Sstevel@tonic-gate 				 * of all active projects within the zone (and
12097c478bd9Sstevel@tonic-gate 				 * the pset), and zone_ext_shares is the number
12107c478bd9Sstevel@tonic-gate 				 * of zone shares (ie, zone.cpu-shares).
12117c478bd9Sstevel@tonic-gate 				 *
12127c478bd9Sstevel@tonic-gate 				 * If there is only one zone active on the pset
12137c478bd9Sstevel@tonic-gate 				 * the above reduces to:
12147c478bd9Sstevel@tonic-gate 				 *
12157c478bd9Sstevel@tonic-gate 				 * 			zone_int_shares^2
12167c478bd9Sstevel@tonic-gate 				 * shusage = usage * ---------------------
12177c478bd9Sstevel@tonic-gate 				 * 			kpj_shares^2
12187c478bd9Sstevel@tonic-gate 				 *
12197c478bd9Sstevel@tonic-gate 				 * If there's only one project active in the
12207c478bd9Sstevel@tonic-gate 				 * zone this formula reduces to:
12217c478bd9Sstevel@tonic-gate 				 *
12227c478bd9Sstevel@tonic-gate 				 *			pset_shares^2
12237c478bd9Sstevel@tonic-gate 				 * shusage = usage * ----------------------
12247c478bd9Sstevel@tonic-gate 				 *			zone_ext_shares^2
1225*482a7749SJerry Jelinek 				 *
1226*482a7749SJerry Jelinek 				 * shusage is one input to calculating fss_pri
1227*482a7749SJerry Jelinek 				 * in fss_newpri(). Larger values tend toward
1228*482a7749SJerry Jelinek 				 * lower priorities for processes in the proj.
12297c478bd9Sstevel@tonic-gate 				 */
12307c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = fssproj->fssp_usage *
12317c478bd9Sstevel@tonic-gate 				    pset_shares * zone_int_shares;
12327c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage /=
12337c478bd9Sstevel@tonic-gate 				    kpj_shares * zone_ext_shares;
12347c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage *=
12357c478bd9Sstevel@tonic-gate 				    pset_shares * zone_int_shares;
12367c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage /=
12377c478bd9Sstevel@tonic-gate 				    kpj_shares * zone_ext_shares;
12387c478bd9Sstevel@tonic-gate 			}
12397c478bd9Sstevel@tonic-gate 			fssproj = fssproj->fssp_next;
12407c478bd9Sstevel@tonic-gate 		} while (fssproj != fsspset->fssps_list);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		disp_lock_exit(&fsspset->fssps_displock);
12437c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate static void
12497c478bd9Sstevel@tonic-gate fss_change_priority(kthread_t *t, fssproc_t *fssproc)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	pri_t new_pri;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
12547c478bd9Sstevel@tonic-gate 	new_pri = fssproc->fss_umdpri;
12557c478bd9Sstevel@tonic-gate 	ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
12567c478bd9Sstevel@tonic-gate 
1257d4204c85Sraf 	t->t_cpri = fssproc->fss_upri;
12589ac8606fSraf 	fssproc->fss_flags &= ~FSSRESTORE;
12597c478bd9Sstevel@tonic-gate 	if (t == curthread || t->t_state == TS_ONPROC) {
12607c478bd9Sstevel@tonic-gate 		/*
12617c478bd9Sstevel@tonic-gate 		 * curthread is always onproc
12627c478bd9Sstevel@tonic-gate 		 */
12637c478bd9Sstevel@tonic-gate 		cpu_t *cp = t->t_disp_queue->disp_cpu;
12647c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, new_pri);
12657c478bd9Sstevel@tonic-gate 		if (t == cp->cpu_dispthread)
12667c478bd9Sstevel@tonic-gate 			cp->cpu_dispatch_pri = DISP_PRIO(t);
12677c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t)) {
12687c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
12697c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
12707c478bd9Sstevel@tonic-gate 		} else {
12717c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 	} else {
12747c478bd9Sstevel@tonic-gate 		/*
12757c478bd9Sstevel@tonic-gate 		 * When the priority of a thread is changed, it may be
12767c478bd9Sstevel@tonic-gate 		 * necessary to adjust its position on a sleep queue or
12777c478bd9Sstevel@tonic-gate 		 * dispatch queue.  The function thread_change_pri accomplishes
12787c478bd9Sstevel@tonic-gate 		 * this.
12797c478bd9Sstevel@tonic-gate 		 */
12807c478bd9Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, 0)) {
12817c478bd9Sstevel@tonic-gate 			/*
12827c478bd9Sstevel@tonic-gate 			 * The thread was on a run queue.
12837c478bd9Sstevel@tonic-gate 			 */
12847c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
12857c478bd9Sstevel@tonic-gate 		} else {
12867c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Update priorities of all fair-sharing threads that are currently runnable
12937c478bd9Sstevel@tonic-gate  * at a user mode priority based on the number of shares and current usage.
12947c478bd9Sstevel@tonic-gate  * Called once per second via timeout which we reset here.
12957c478bd9Sstevel@tonic-gate  *
12967c478bd9Sstevel@tonic-gate  * There are several lists of fair-sharing threads broken up by a hash on the
12977c478bd9Sstevel@tonic-gate  * thread pointer.  Each list has its own lock.  This avoids blocking all
12987c478bd9Sstevel@tonic-gate  * fss_enterclass, fss_fork, and fss_exitclass operations while fss_update runs.
12997c478bd9Sstevel@tonic-gate  * fss_update traverses each list in turn.
1300*482a7749SJerry Jelinek  *
1301*482a7749SJerry Jelinek  * Each time we're run (once/second) we may start at the next list and iterate
1302*482a7749SJerry Jelinek  * through all of the lists. By starting with a different list, we mitigate any
1303*482a7749SJerry Jelinek  * effects we would see updating the fssps_maxfsspri value in fss_newpri.
13047c478bd9Sstevel@tonic-gate  */
13057c478bd9Sstevel@tonic-gate static void
13067c478bd9Sstevel@tonic-gate fss_update(void *arg)
13077c478bd9Sstevel@tonic-gate {
13087c478bd9Sstevel@tonic-gate 	int i;
13097c478bd9Sstevel@tonic-gate 	int new_marker = -1;
13107c478bd9Sstevel@tonic-gate 	static int fss_update_marker;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	/*
13137c478bd9Sstevel@tonic-gate 	 * Decay and update usages for all projects.
13147c478bd9Sstevel@tonic-gate 	 */
13157c478bd9Sstevel@tonic-gate 	fss_decay_usage();
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * Start with the fss_update_marker list, then do the rest.
13197c478bd9Sstevel@tonic-gate 	 */
13207c478bd9Sstevel@tonic-gate 	i = fss_update_marker;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Go around all threads, set new priorities and decay
13247c478bd9Sstevel@tonic-gate 	 * per-thread CPU usages.
13257c478bd9Sstevel@tonic-gate 	 */
13267c478bd9Sstevel@tonic-gate 	do {
13277c478bd9Sstevel@tonic-gate 		/*
13287c478bd9Sstevel@tonic-gate 		 * If this is the first list after the current marker to have
1329*482a7749SJerry Jelinek 		 * threads with priority updates, advance the marker to this
13307c478bd9Sstevel@tonic-gate 		 * list for the next time fss_update runs.
13317c478bd9Sstevel@tonic-gate 		 */
13327c478bd9Sstevel@tonic-gate 		if (fss_update_list(i) &&
13337c478bd9Sstevel@tonic-gate 		    new_marker == -1 && i != fss_update_marker)
13347c478bd9Sstevel@tonic-gate 			new_marker = i;
13357c478bd9Sstevel@tonic-gate 	} while ((i = FSS_LIST_NEXT(i)) != fss_update_marker);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/*
13387c478bd9Sstevel@tonic-gate 	 * Advance marker for the next fss_update call
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	if (new_marker != -1)
13417c478bd9Sstevel@tonic-gate 		fss_update_marker = new_marker;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	(void) timeout(fss_update, arg, hz);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /*
13477c478bd9Sstevel@tonic-gate  * Updates priority for a list of threads.  Returns 1 if the priority of one
13487c478bd9Sstevel@tonic-gate  * of the threads was actually updated, 0 if none were for various reasons
13497c478bd9Sstevel@tonic-gate  * (thread is no longer in the FSS class, is not runnable, has the preemption
13507c478bd9Sstevel@tonic-gate  * control no-preempt bit set, etc.)
13517c478bd9Sstevel@tonic-gate  */
13527c478bd9Sstevel@tonic-gate static int
13537c478bd9Sstevel@tonic-gate fss_update_list(int i)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
13567c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
13577c478bd9Sstevel@tonic-gate 	fsspri_t fsspri;
1358*482a7749SJerry Jelinek 	pri_t fss_umdpri;
13597c478bd9Sstevel@tonic-gate 	kthread_t *t;
13607c478bd9Sstevel@tonic-gate 	int updated = 0;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	mutex_enter(&fss_listlock[i]);
13637c478bd9Sstevel@tonic-gate 	for (fssproc = fss_listhead[i].fss_next; fssproc != &fss_listhead[i];
13647c478bd9Sstevel@tonic-gate 	    fssproc = fssproc->fss_next) {
13657c478bd9Sstevel@tonic-gate 		t = fssproc->fss_tp;
13667c478bd9Sstevel@tonic-gate 		/*
13677c478bd9Sstevel@tonic-gate 		 * Lock the thread and verify the state.
13687c478bd9Sstevel@tonic-gate 		 */
13697c478bd9Sstevel@tonic-gate 		thread_lock(t);
13707c478bd9Sstevel@tonic-gate 		/*
13717c478bd9Sstevel@tonic-gate 		 * Skip the thread if it is no longer in the FSS class or
13727c478bd9Sstevel@tonic-gate 		 * is running with kernel mode priority.
13737c478bd9Sstevel@tonic-gate 		 */
13747c478bd9Sstevel@tonic-gate 		if (t->t_cid != fss_cid)
13757c478bd9Sstevel@tonic-gate 			goto next;
13767c478bd9Sstevel@tonic-gate 		if ((fssproc->fss_flags & FSSKPRI) != 0)
13777c478bd9Sstevel@tonic-gate 			goto next;
1378c97ad5cdSakolb 
13797c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
13807c478bd9Sstevel@tonic-gate 		if (fssproj == NULL)
13817c478bd9Sstevel@tonic-gate 			goto next;
1382*482a7749SJerry Jelinek 
13837c478bd9Sstevel@tonic-gate 		if (fssproj->fssp_shares != 0) {
13847c478bd9Sstevel@tonic-gate 			/*
13857c478bd9Sstevel@tonic-gate 			 * Decay fsspri value.
13867c478bd9Sstevel@tonic-gate 			 */
13877c478bd9Sstevel@tonic-gate 			fsspri = fssproc->fss_fsspri;
13887c478bd9Sstevel@tonic-gate 			fsspri = (fsspri * fss_nice_decay[fssproc->fss_nice]) /
13897c478bd9Sstevel@tonic-gate 			    FSS_DECAY_BASE;
13907c478bd9Sstevel@tonic-gate 			fssproc->fss_fsspri = fsspri;
13917c478bd9Sstevel@tonic-gate 		}
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		if (t->t_schedctl && schedctl_get_nopreempt(t))
13947c478bd9Sstevel@tonic-gate 			goto next;
1395c97ad5cdSakolb 		if (t->t_state != TS_RUN && t->t_state != TS_WAIT) {
13967c478bd9Sstevel@tonic-gate 			/*
13977c478bd9Sstevel@tonic-gate 			 * Make next syscall/trap call fss_trapret
13987c478bd9Sstevel@tonic-gate 			 */
13997c478bd9Sstevel@tonic-gate 			t->t_trapret = 1;
14007c478bd9Sstevel@tonic-gate 			aston(t);
1401*482a7749SJerry Jelinek 			if (t->t_state == TS_ONPROC)
1402*482a7749SJerry Jelinek 				DTRACE_PROBE1(fss__onproc, fssproc_t *,
1403*482a7749SJerry Jelinek 				    fssproc);
14047c478bd9Sstevel@tonic-gate 			goto next;
14057c478bd9Sstevel@tonic-gate 		}
1406*482a7749SJerry Jelinek 		fss_newpri(fssproc, B_FALSE);
14077c478bd9Sstevel@tonic-gate 		updated = 1;
14087c478bd9Sstevel@tonic-gate 
1409*482a7749SJerry Jelinek 		fss_umdpri = fssproc->fss_umdpri;
1410*482a7749SJerry Jelinek 
14117c478bd9Sstevel@tonic-gate 		/*
14127c478bd9Sstevel@tonic-gate 		 * Only dequeue the thread if it needs to be moved; otherwise
14137c478bd9Sstevel@tonic-gate 		 * it should just round-robin here.
14147c478bd9Sstevel@tonic-gate 		 */
1415*482a7749SJerry Jelinek 		if (t->t_pri != fss_umdpri)
14167c478bd9Sstevel@tonic-gate 			fss_change_priority(t, fssproc);
14177c478bd9Sstevel@tonic-gate next:
14187c478bd9Sstevel@tonic-gate 		thread_unlock(t);
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	mutex_exit(&fss_listlock[i]);
14217c478bd9Sstevel@tonic-gate 	return (updated);
14227c478bd9Sstevel@tonic-gate }
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14257c478bd9Sstevel@tonic-gate static int
14267c478bd9Sstevel@tonic-gate fss_admin(caddr_t uaddr, cred_t *reqpcredp)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	fssadmin_t fssadmin;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (copyin(uaddr, &fssadmin, sizeof (fssadmin_t)))
14317c478bd9Sstevel@tonic-gate 		return (EFAULT);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	switch (fssadmin.fss_cmd) {
14347c478bd9Sstevel@tonic-gate 	case FSS_SETADMIN:
14357c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
14367c478bd9Sstevel@tonic-gate 			return (EPERM);
14377c478bd9Sstevel@tonic-gate 		if (fssadmin.fss_quantum <= 0 || fssadmin.fss_quantum >= hz)
14387c478bd9Sstevel@tonic-gate 			return (EINVAL);
14397c478bd9Sstevel@tonic-gate 		fss_quantum = fssadmin.fss_quantum;
14407c478bd9Sstevel@tonic-gate 		break;
14417c478bd9Sstevel@tonic-gate 	case FSS_GETADMIN:
14427c478bd9Sstevel@tonic-gate 		fssadmin.fss_quantum = fss_quantum;
14437c478bd9Sstevel@tonic-gate 		if (copyout(&fssadmin, uaddr, sizeof (fssadmin_t)))
14447c478bd9Sstevel@tonic-gate 			return (EFAULT);
14457c478bd9Sstevel@tonic-gate 		break;
14467c478bd9Sstevel@tonic-gate 	default:
14477c478bd9Sstevel@tonic-gate 		return (EINVAL);
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 	return (0);
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate static int
14537c478bd9Sstevel@tonic-gate fss_getclinfo(void *infop)
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	fssinfo_t *fssinfo = (fssinfo_t *)infop;
14567c478bd9Sstevel@tonic-gate 	fssinfo->fss_maxupri = fss_maxupri;
14577c478bd9Sstevel@tonic-gate 	return (0);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate static int
14617c478bd9Sstevel@tonic-gate fss_parmsin(void *parmsp)
14627c478bd9Sstevel@tonic-gate {
14637c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	/*
14667c478bd9Sstevel@tonic-gate 	 * Check validity of parameters.
14677c478bd9Sstevel@tonic-gate 	 */
14687c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_uprilim > fss_maxupri ||
14697c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim < -fss_maxupri) &&
14707c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim != FSS_NOCHANGE)
14717c478bd9Sstevel@tonic-gate 		return (EINVAL);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_upri > fss_maxupri ||
14747c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri < -fss_maxupri) &&
14757c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri != FSS_NOCHANGE)
14767c478bd9Sstevel@tonic-gate 		return (EINVAL);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	return (0);
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14827c478bd9Sstevel@tonic-gate static int
14837c478bd9Sstevel@tonic-gate fss_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	return (0);
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate static int
14897c478bd9Sstevel@tonic-gate fss_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
14907c478bd9Sstevel@tonic-gate {
14917c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
14927c478bd9Sstevel@tonic-gate 	int priflag = 0;
14937c478bd9Sstevel@tonic-gate 	int limflag = 0;
14947c478bd9Sstevel@tonic-gate 	uint_t cnt;
14957c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	/*
14987c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE (-32768) is outside of the range of values for
14997c478bd9Sstevel@tonic-gate 	 * fss_uprilim and fss_upri.  If the structure fssparms_t is changed,
15007c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE should be replaced by a flag word.
15017c478bd9Sstevel@tonic-gate 	 */
15027c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = FSS_NOCHANGE;
15037c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = FSS_NOCHANGE;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/*
15067c478bd9Sstevel@tonic-gate 	 * Get the varargs parameter and check validity of parameters.
15077c478bd9Sstevel@tonic-gate 	 */
15087c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
15097c478bd9Sstevel@tonic-gate 		return (EINVAL);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
15127c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
15137c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
15147c478bd9Sstevel@tonic-gate 			if (limflag++)
15157c478bd9Sstevel@tonic-gate 				return (EINVAL);
15167c478bd9Sstevel@tonic-gate 			fssparmsp->fss_uprilim = (pri_t)vpp->pc_parm;
15177c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > fss_maxupri ||
15187c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_uprilim < -fss_maxupri)
15197c478bd9Sstevel@tonic-gate 				return (EINVAL);
15207c478bd9Sstevel@tonic-gate 			break;
15217c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
15227c478bd9Sstevel@tonic-gate 			if (priflag++)
15237c478bd9Sstevel@tonic-gate 				return (EINVAL);
15247c478bd9Sstevel@tonic-gate 			fssparmsp->fss_upri = (pri_t)vpp->pc_parm;
15257c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > fss_maxupri ||
15267c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_upri < -fss_maxupri)
15277c478bd9Sstevel@tonic-gate 				return (EINVAL);
15287c478bd9Sstevel@tonic-gate 			break;
15297c478bd9Sstevel@tonic-gate 		default:
15307c478bd9Sstevel@tonic-gate 			return (EINVAL);
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
15357c478bd9Sstevel@tonic-gate 		/*
15367c478bd9Sstevel@tonic-gate 		 * Use default parameters.
15377c478bd9Sstevel@tonic-gate 		 */
15387c478bd9Sstevel@tonic-gate 		fssparmsp->fss_upri = fssparmsp->fss_uprilim = 0;
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	return (0);
15427c478bd9Sstevel@tonic-gate }
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate /*
15457c478bd9Sstevel@tonic-gate  * Copy all selected fair-sharing class parameters to the user.  The parameters
15467c478bd9Sstevel@tonic-gate  * are specified by a key.
15477c478bd9Sstevel@tonic-gate  */
15487c478bd9Sstevel@tonic-gate static int
15497c478bd9Sstevel@tonic-gate fss_vaparmsout(void *parmsp, pc_vaparms_t *vaparmsp)
15507c478bd9Sstevel@tonic-gate {
15517c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
15527c478bd9Sstevel@tonic-gate 	int priflag = 0;
15537c478bd9Sstevel@tonic-gate 	int limflag = 0;
15547c478bd9Sstevel@tonic-gate 	uint_t cnt;
15557c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
15607c478bd9Sstevel@tonic-gate 		return (EINVAL);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
15637c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
15647c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
15657c478bd9Sstevel@tonic-gate 			if (limflag++)
15667c478bd9Sstevel@tonic-gate 				return (EINVAL);
15677c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_uprilim,
15687c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
15697c478bd9Sstevel@tonic-gate 				return (EFAULT);
15707c478bd9Sstevel@tonic-gate 			break;
15717c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
15727c478bd9Sstevel@tonic-gate 			if (priflag++)
15737c478bd9Sstevel@tonic-gate 				return (EINVAL);
15747c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_upri,
15757c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
15767c478bd9Sstevel@tonic-gate 				return (EFAULT);
15777c478bd9Sstevel@tonic-gate 			break;
15787c478bd9Sstevel@tonic-gate 		default:
15797c478bd9Sstevel@tonic-gate 			return (EINVAL);
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 	}
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	return (0);
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
1586d4204c85Sraf /*
1587d4204c85Sraf  * Return the user mode scheduling priority range.
1588d4204c85Sraf  */
15897c478bd9Sstevel@tonic-gate static int
15907c478bd9Sstevel@tonic-gate fss_getclpri(pcpri_t *pcprip)
15917c478bd9Sstevel@tonic-gate {
1592d4204c85Sraf 	pcprip->pc_clpmax = fss_maxupri;
1593d4204c85Sraf 	pcprip->pc_clpmin = -fss_maxupri;
15947c478bd9Sstevel@tonic-gate 	return (0);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate static int
15987c478bd9Sstevel@tonic-gate fss_alloc(void **p, int flag)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 	void *bufp;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	if ((bufp = kmem_zalloc(sizeof (fssproc_t), flag)) == NULL) {
16037c478bd9Sstevel@tonic-gate 		return (ENOMEM);
16047c478bd9Sstevel@tonic-gate 	} else {
16057c478bd9Sstevel@tonic-gate 		*p = bufp;
16067c478bd9Sstevel@tonic-gate 		return (0);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static void
16117c478bd9Sstevel@tonic-gate fss_free(void *bufp)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	if (bufp)
16147c478bd9Sstevel@tonic-gate 		kmem_free(bufp, sizeof (fssproc_t));
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate /*
16187c478bd9Sstevel@tonic-gate  * Thread functions
16197c478bd9Sstevel@tonic-gate  */
16207c478bd9Sstevel@tonic-gate static int
16217c478bd9Sstevel@tonic-gate fss_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
16227c478bd9Sstevel@tonic-gate     void *bufp)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
16257c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc;
16267c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
16277c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
16287c478bd9Sstevel@tonic-gate 	static uint32_t fssexists = 0;
16297c478bd9Sstevel@tonic-gate 	fsspset_t	*fsspset;
16307c478bd9Sstevel@tonic-gate 	fssproj_t	*fssproj;
16317c478bd9Sstevel@tonic-gate 	fsszone_t	*fsszone;
16327c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
16337c478bd9Sstevel@tonic-gate 	zone_t		*zone;
16347c478bd9Sstevel@tonic-gate 	int		fsszone_allocated = 0;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	fssproc = (fssproc_t *)bufp;
16377c478bd9Sstevel@tonic-gate 	ASSERT(fssproc != NULL);
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/*
16427c478bd9Sstevel@tonic-gate 	 * Only root can move threads to FSS class.
16437c478bd9Sstevel@tonic-gate 	 */
16447c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
16457c478bd9Sstevel@tonic-gate 		return (EPERM);
16467c478bd9Sstevel@tonic-gate 	/*
16477c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc structure.
16487c478bd9Sstevel@tonic-gate 	 */
16497c478bd9Sstevel@tonic-gate 	fssproc->fss_umdpri = fss_maxumdpri / 2;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	if (fssparmsp == NULL) {
16527c478bd9Sstevel@tonic-gate 		/*
16537c478bd9Sstevel@tonic-gate 		 * Use default values.
16547c478bd9Sstevel@tonic-gate 		 */
16557c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO;
16567c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = fssproc->fss_upri = 0;
16577c478bd9Sstevel@tonic-gate 	} else {
16587c478bd9Sstevel@tonic-gate 		/*
16597c478bd9Sstevel@tonic-gate 		 * Use supplied values.
16607c478bd9Sstevel@tonic-gate 		 */
16617c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_uprilim == FSS_NOCHANGE) {
16627c478bd9Sstevel@tonic-gate 			reqfssuprilim = 0;
16637c478bd9Sstevel@tonic-gate 		} else {
16647c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > 0 &&
16657c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
16667c478bd9Sstevel@tonic-gate 				return (EPERM);
16677c478bd9Sstevel@tonic-gate 			reqfssuprilim = fssparmsp->fss_uprilim;
16687c478bd9Sstevel@tonic-gate 		}
16697c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_upri == FSS_NOCHANGE) {
16707c478bd9Sstevel@tonic-gate 			reqfssupri = reqfssuprilim;
16717c478bd9Sstevel@tonic-gate 		} else {
16727c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > 0 &&
16737c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
16747c478bd9Sstevel@tonic-gate 				return (EPERM);
16757c478bd9Sstevel@tonic-gate 			/*
16767c478bd9Sstevel@tonic-gate 			 * Set the user priority to the requested value or
16777c478bd9Sstevel@tonic-gate 			 * the upri limit, whichever is lower.
16787c478bd9Sstevel@tonic-gate 			 */
16797c478bd9Sstevel@tonic-gate 			reqfssupri = fssparmsp->fss_upri;
16807c478bd9Sstevel@tonic-gate 			if (reqfssupri > reqfssuprilim)
16817c478bd9Sstevel@tonic-gate 				reqfssupri = reqfssuprilim;
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = reqfssuprilim;
16847c478bd9Sstevel@tonic-gate 		fssproc->fss_upri = reqfssupri;
16857c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO - (NZERO * reqfssupri) / fss_maxupri;
16867c478bd9Sstevel@tonic-gate 		if (fssproc->fss_nice > FSS_NICE_MAX)
16877c478bd9Sstevel@tonic-gate 			fssproc->fss_nice = FSS_NICE_MAX;
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
16917c478bd9Sstevel@tonic-gate 	fssproc->fss_tp = t;
1692c97ad5cdSakolb 	cpucaps_sc_init(&fssproc->fss_caps);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	/*
16957c478bd9Sstevel@tonic-gate 	 * Put a lock on our fsspset structure.
16967c478bd9Sstevel@tonic-gate 	 */
16977c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
16987c478bd9Sstevel@tonic-gate 	fsspset = fss_find_fsspset(t->t_cpupart);
16997c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
17007c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
17037c478bd9Sstevel@tonic-gate 	if ((fsszone = fss_find_fsszone(fsspset, zone)) == NULL) {
17047c478bd9Sstevel@tonic-gate 		if ((fsszone = kmem_zalloc(sizeof (fsszone_t), KM_NOSLEEP))
17057c478bd9Sstevel@tonic-gate 		    == NULL) {
17067c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
17077c478bd9Sstevel@tonic-gate 			return (ENOMEM);
17087c478bd9Sstevel@tonic-gate 		} else {
17097c478bd9Sstevel@tonic-gate 			fsszone_allocated = 1;
17107c478bd9Sstevel@tonic-gate 			fss_insert_fsszone(fsspset, zone, fsszone);
17117c478bd9Sstevel@tonic-gate 		}
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 	kpj = ttoproj(t);
17147c478bd9Sstevel@tonic-gate 	if ((fssproj = fss_find_fssproj(fsspset, kpj)) == NULL) {
17157c478bd9Sstevel@tonic-gate 		if ((fssproj = kmem_zalloc(sizeof (fssproj_t), KM_NOSLEEP))
17167c478bd9Sstevel@tonic-gate 		    == NULL) {
17177c478bd9Sstevel@tonic-gate 			if (fsszone_allocated) {
17187c478bd9Sstevel@tonic-gate 				fss_remove_fsszone(fsspset, fsszone);
17197c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
17207c478bd9Sstevel@tonic-gate 			}
17217c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
17227c478bd9Sstevel@tonic-gate 			return (ENOMEM);
17237c478bd9Sstevel@tonic-gate 		} else {
17247c478bd9Sstevel@tonic-gate 			fss_insert_fssproj(fsspset, kpj, fsszone, fssproj);
17257c478bd9Sstevel@tonic-gate 		}
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
17287c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * Reset priority. Process goes to a "user mode" priority here
17327c478bd9Sstevel@tonic-gate 	 * regardless of whether or not it has slept since entering the kernel.
17337c478bd9Sstevel@tonic-gate 	 */
17347c478bd9Sstevel@tonic-gate 	thread_lock(t);
17357c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
17367c478bd9Sstevel@tonic-gate 	t->t_cid = cid;
17377c478bd9Sstevel@tonic-gate 	t->t_cldata = (void *)fssproc;
17387c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_RUNQMATCH;
17397c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
1740c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
1741c97ad5cdSakolb 	    t->t_state == TS_WAIT)
17427c478bd9Sstevel@tonic-gate 		fss_active(t);
17437c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	/*
17487c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc list.
17497c478bd9Sstevel@tonic-gate 	 */
17507c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(fssproc);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	/*
17537c478bd9Sstevel@tonic-gate 	 * If this is the first fair-sharing thread to occur since boot,
17547c478bd9Sstevel@tonic-gate 	 * we set up the initial call to fss_update() here. Use an atomic
17557c478bd9Sstevel@tonic-gate 	 * compare-and-swap since that's easier and faster than a mutex
17567c478bd9Sstevel@tonic-gate 	 * (but check with an ordinary load first since most of the time
17577c478bd9Sstevel@tonic-gate 	 * this will already be done).
17587c478bd9Sstevel@tonic-gate 	 */
175975d94465SJosef 'Jeff' Sipek 	if (fssexists == 0 && atomic_cas_32(&fssexists, 0, 1) == 0)
17607c478bd9Sstevel@tonic-gate 		(void) timeout(fss_update, NULL, hz);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	return (0);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /*
17667c478bd9Sstevel@tonic-gate  * Remove fssproc_t from the list.
17677c478bd9Sstevel@tonic-gate  */
17687c478bd9Sstevel@tonic-gate static void
17697c478bd9Sstevel@tonic-gate fss_exitclass(void *procp)
17707c478bd9Sstevel@tonic-gate {
17717c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = (fssproc_t *)procp;
17727c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
17737c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
17747c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
17757c478bd9Sstevel@tonic-gate 	kthread_t *t = fssproc->fss_tp;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	/*
17787c478bd9Sstevel@tonic-gate 	 * We should be either getting this thread off the deathrow or
17797c478bd9Sstevel@tonic-gate 	 * this thread has already moved to another scheduling class and
17807c478bd9Sstevel@tonic-gate 	 * we're being called with its old cldata buffer pointer.  In both
17817c478bd9Sstevel@tonic-gate 	 * cases, the content of this buffer can not be changed while we're
17827c478bd9Sstevel@tonic-gate 	 * here.
17837c478bd9Sstevel@tonic-gate 	 */
17847c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
17857c478bd9Sstevel@tonic-gate 	thread_lock(t);
17867c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid) {
17877c478bd9Sstevel@tonic-gate 		/*
17887c478bd9Sstevel@tonic-gate 		 * We're being called as a result of the priocntl() system
17897c478bd9Sstevel@tonic-gate 		 * call -- someone is trying to move our thread to another
17907c478bd9Sstevel@tonic-gate 		 * scheduling class. We can't call fss_inactive() here
17917c478bd9Sstevel@tonic-gate 		 * because our thread's t_cldata pointer already points
17927c478bd9Sstevel@tonic-gate 		 * to another scheduling class specific data.
17937c478bd9Sstevel@tonic-gate 		 */
17947c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
17977c478bd9Sstevel@tonic-gate 		fsspset = FSSPROJ2FSSPSET(fssproj);
17987c478bd9Sstevel@tonic-gate 		fsszone = fssproj->fssp_fsszone;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		if (fssproc->fss_runnable) {
18017c478bd9Sstevel@tonic-gate 			disp_lock_enter_high(&fsspset->fssps_displock);
18027c478bd9Sstevel@tonic-gate 			if (--fssproj->fssp_runnable == 0) {
18037c478bd9Sstevel@tonic-gate 				fsszone->fssz_shares -= fssproj->fssp_shares;
18047c478bd9Sstevel@tonic-gate 				if (--fsszone->fssz_runnable == 0)
18057c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
18067c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
18077c478bd9Sstevel@tonic-gate 			}
18087c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(&fsspset->fssps_displock);
18097c478bd9Sstevel@tonic-gate 		}
18107c478bd9Sstevel@tonic-gate 		thread_unlock(t);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
18137c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_threads == 0) {
18147c478bd9Sstevel@tonic-gate 			fss_remove_fssproj(fsspset, fssproj);
18157c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_nproj == 0)
18167c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
18177c478bd9Sstevel@tonic-gate 			kmem_free(fssproj, sizeof (fssproj_t));
18187c478bd9Sstevel@tonic-gate 		}
18197c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	} else {
18227c478bd9Sstevel@tonic-gate 		ASSERT(t->t_state == TS_FREE);
18237c478bd9Sstevel@tonic-gate 		/*
18247c478bd9Sstevel@tonic-gate 		 * We're being called from thread_free() when our thread
18257c478bd9Sstevel@tonic-gate 		 * is removed from the deathrow. There is nothing we need
18267c478bd9Sstevel@tonic-gate 		 * do here since everything should've been done earlier
18277c478bd9Sstevel@tonic-gate 		 * in fss_exit().
18287c478bd9Sstevel@tonic-gate 		 */
18297c478bd9Sstevel@tonic-gate 		thread_unlock(t);
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	FSS_LIST_DELETE(fssproc);
18347c478bd9Sstevel@tonic-gate 	fss_free(fssproc);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18387c478bd9Sstevel@tonic-gate static int
18397c478bd9Sstevel@tonic-gate fss_canexit(kthread_t *t, cred_t *credp)
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate 	/*
18427c478bd9Sstevel@tonic-gate 	 * A thread is allowed to exit FSS only if we have sufficient
18437c478bd9Sstevel@tonic-gate 	 * privileges.
18447c478bd9Sstevel@tonic-gate 	 */
18457c478bd9Sstevel@tonic-gate 	if (credp != NULL && secpolicy_setpriority(credp) != 0)
18467c478bd9Sstevel@tonic-gate 		return (EPERM);
18477c478bd9Sstevel@tonic-gate 	else
18487c478bd9Sstevel@tonic-gate 		return (0);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate /*
18527c478bd9Sstevel@tonic-gate  * Initialize fair-share class specific proc structure for a child.
18537c478bd9Sstevel@tonic-gate  */
18547c478bd9Sstevel@tonic-gate static int
18557c478bd9Sstevel@tonic-gate fss_fork(kthread_t *pt, kthread_t *ct, void *bufp)
18567c478bd9Sstevel@tonic-gate {
18577c478bd9Sstevel@tonic-gate 	fssproc_t *pfssproc;	/* ptr to parent's fssproc structure	*/
18587c478bd9Sstevel@tonic-gate 	fssproc_t *cfssproc;	/* ptr to child's fssproc structure	*/
18597c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
18607c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(pt)->p_lock));
18637c478bd9Sstevel@tonic-gate 	ASSERT(ct->t_state == TS_STOPPED);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	cfssproc = (fssproc_t *)bufp;
18667c478bd9Sstevel@tonic-gate 	ASSERT(cfssproc != NULL);
18677c478bd9Sstevel@tonic-gate 	bzero(cfssproc, sizeof (fssproc_t));
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	thread_lock(pt);
18707c478bd9Sstevel@tonic-gate 	pfssproc = FSSPROC(pt);
18717c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(pfssproc);
18727c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
18737c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
18767c478bd9Sstevel@tonic-gate 	/*
18777c478bd9Sstevel@tonic-gate 	 * Initialize child's fssproc structure.
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	thread_lock(pt);
18807c478bd9Sstevel@tonic-gate 	ASSERT(FSSPROJ(pt) == fssproj);
18817c478bd9Sstevel@tonic-gate 	cfssproc->fss_proj = fssproj;
18827c478bd9Sstevel@tonic-gate 	cfssproc->fss_timeleft = fss_quantum;
18837c478bd9Sstevel@tonic-gate 	cfssproc->fss_umdpri = pfssproc->fss_umdpri;
18847c478bd9Sstevel@tonic-gate 	cfssproc->fss_fsspri = 0;
18857c478bd9Sstevel@tonic-gate 	cfssproc->fss_uprilim = pfssproc->fss_uprilim;
18867c478bd9Sstevel@tonic-gate 	cfssproc->fss_upri = pfssproc->fss_upri;
18877c478bd9Sstevel@tonic-gate 	cfssproc->fss_tp = ct;
18887c478bd9Sstevel@tonic-gate 	cfssproc->fss_nice = pfssproc->fss_nice;
1889c97ad5cdSakolb 	cpucaps_sc_init(&cfssproc->fss_caps);
1890c97ad5cdSakolb 
18919ac8606fSraf 	cfssproc->fss_flags =
18929ac8606fSraf 	    pfssproc->fss_flags & ~(FSSKPRI | FSSBACKQ | FSSRESTORE);
18937c478bd9Sstevel@tonic-gate 	ct->t_cldata = (void *)cfssproc;
18947c478bd9Sstevel@tonic-gate 	ct->t_schedflag |= TS_RUNQMATCH;
18957c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
18987c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	/*
19017c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc hash table.
19027c478bd9Sstevel@tonic-gate 	 */
19037c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(cfssproc);
19047c478bd9Sstevel@tonic-gate 	return (0);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate /*
19087c478bd9Sstevel@tonic-gate  * Child is placed at back of dispatcher queue and parent gives up processor
19097c478bd9Sstevel@tonic-gate  * so that the child runs first after the fork. This allows the child
19107c478bd9Sstevel@tonic-gate  * immediately execing to break the multiple use of copy on write pages with no
19117c478bd9Sstevel@tonic-gate  * disk home. The parent will get to steal them back rather than uselessly
19127c478bd9Sstevel@tonic-gate  * copying them.
19137c478bd9Sstevel@tonic-gate  */
19147c478bd9Sstevel@tonic-gate static void
19157c478bd9Sstevel@tonic-gate fss_forkret(kthread_t *t, kthread_t *ct)
19167c478bd9Sstevel@tonic-gate {
19177c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
19187c478bd9Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
19197c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
19227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	/*
19257c478bd9Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure the
19267c478bd9Sstevel@tonic-gate 	 * process does not disappear before we set it running.
19277c478bd9Sstevel@tonic-gate 	 */
19287c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
19297c478bd9Sstevel@tonic-gate 	continuelwps(cp);
19307c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1933d5493db7SPramod Batni 	mutex_exit(&pidlock);
19347c478bd9Sstevel@tonic-gate 	continuelwps(pp);
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	thread_lock(t);
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
1939*482a7749SJerry Jelinek 	fss_newpri(fssproc, B_FALSE);
19407c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
19417c478bd9Sstevel@tonic-gate 	t->t_pri = fssproc->fss_umdpri;
19427c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
19437c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSKPRI;
19447c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	/*
19477c478bd9Sstevel@tonic-gate 	 * We don't want to call fss_setrun(t) here because it may call
19487c478bd9Sstevel@tonic-gate 	 * fss_active, which we don't need.
19497c478bd9Sstevel@tonic-gate 	 */
19507c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
19517c478bd9Sstevel@tonic-gate 
1952d3d50737SRafael Vanoni 	if (t->t_disp_time != ddi_get_lbolt())
19537c478bd9Sstevel@tonic-gate 		setbackdq(t);
19547c478bd9Sstevel@tonic-gate 	else
19557c478bd9Sstevel@tonic-gate 		setfrontdq(t);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1958d5493db7SPramod Batni 	/*
1959d5493db7SPramod Batni 	 * Safe to drop p_lock now since it is safe to change
1960d5493db7SPramod Batni 	 * the scheduling class after this point.
1961d5493db7SPramod Batni 	 */
1962d5493db7SPramod Batni 	mutex_exit(&pp->p_lock);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	swtch();
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate /*
19687c478bd9Sstevel@tonic-gate  * Get the fair-sharing parameters of the thread pointed to by fssprocp into
19697c478bd9Sstevel@tonic-gate  * the buffer pointed by fssparmsp.
19707c478bd9Sstevel@tonic-gate  */
19717c478bd9Sstevel@tonic-gate static void
19727c478bd9Sstevel@tonic-gate fss_parmsget(kthread_t *t, void *parmsp)
19737c478bd9Sstevel@tonic-gate {
19747c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
19757c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = fssproc->fss_uprilim;
19787c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = fssproc->fss_upri;
19797c478bd9Sstevel@tonic-gate }
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19827c478bd9Sstevel@tonic-gate static int
19837c478bd9Sstevel@tonic-gate fss_parmsset(kthread_t *t, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
19847c478bd9Sstevel@tonic-gate {
19857c478bd9Sstevel@tonic-gate 	char		nice;
19867c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
19877c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
19887c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc = FSSPROC(t);
19897c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_uprilim == FSS_NOCHANGE)
19947c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssproc->fss_uprilim;
19957c478bd9Sstevel@tonic-gate 	else
19967c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssparmsp->fss_uprilim;
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_upri == FSS_NOCHANGE)
19997c478bd9Sstevel@tonic-gate 		reqfssupri = fssproc->fss_upri;
20007c478bd9Sstevel@tonic-gate 	else
20017c478bd9Sstevel@tonic-gate 		reqfssupri = fssparmsp->fss_upri;
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	/*
20047c478bd9Sstevel@tonic-gate 	 * Make sure the user priority doesn't exceed the upri limit.
20057c478bd9Sstevel@tonic-gate 	 */
20067c478bd9Sstevel@tonic-gate 	if (reqfssupri > reqfssuprilim)
20077c478bd9Sstevel@tonic-gate 		reqfssupri = reqfssuprilim;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	/*
20107c478bd9Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code for all classes
20117c478bd9Sstevel@tonic-gate 	 * require that a thread attempting to change the scheduling parameters
20127c478bd9Sstevel@tonic-gate 	 * of a target thread be privileged or have a real or effective UID
20137c478bd9Sstevel@tonic-gate 	 * matching that of the target thread. We are not called unless these
20147c478bd9Sstevel@tonic-gate 	 * basic permission checks have already passed. The fair-sharing class
20157c478bd9Sstevel@tonic-gate 	 * requires in addition that the calling thread be privileged if it
20167c478bd9Sstevel@tonic-gate 	 * is attempting to raise the upri limit above its current value.
20177c478bd9Sstevel@tonic-gate 	 * This may have been checked previously but if our caller passed us
20187c478bd9Sstevel@tonic-gate 	 * a non-NULL credential pointer we assume it hasn't and we check it
20197c478bd9Sstevel@tonic-gate 	 * here.
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	if ((reqpcredp != NULL) &&
20227c478bd9Sstevel@tonic-gate 	    (reqfssuprilim > fssproc->fss_uprilim) &&
202324d819e6SJerry Jelinek 	    secpolicy_raisepriority(reqpcredp) != 0)
20247c478bd9Sstevel@tonic-gate 		return (EPERM);
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	/*
20277c478bd9Sstevel@tonic-gate 	 * Set fss_nice to the nice value corresponding to the user priority we
20287c478bd9Sstevel@tonic-gate 	 * are setting.  Note that setting the nice field of the parameter
20297c478bd9Sstevel@tonic-gate 	 * struct won't affect upri or nice.
20307c478bd9Sstevel@tonic-gate 	 */
20317c478bd9Sstevel@tonic-gate 	nice = NZERO - (reqfssupri * NZERO) / fss_maxupri;
20327c478bd9Sstevel@tonic-gate 	if (nice > FSS_NICE_MAX)
20337c478bd9Sstevel@tonic-gate 		nice = FSS_NICE_MAX;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	thread_lock(t);
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	fssproc->fss_uprilim = reqfssuprilim;
20387c478bd9Sstevel@tonic-gate 	fssproc->fss_upri = reqfssupri;
20397c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = nice;
2040*482a7749SJerry Jelinek 	fss_newpri(fssproc, B_FALSE);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) != 0) {
20437c478bd9Sstevel@tonic-gate 		thread_unlock(t);
20447c478bd9Sstevel@tonic-gate 		return (0);
20457c478bd9Sstevel@tonic-gate 	}
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
20487c478bd9Sstevel@tonic-gate 	thread_unlock(t);
20497c478bd9Sstevel@tonic-gate 	return (0);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate /*
20547c478bd9Sstevel@tonic-gate  * The thread is being stopped.
20557c478bd9Sstevel@tonic-gate  */
20567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20577c478bd9Sstevel@tonic-gate static void
20587c478bd9Sstevel@tonic-gate fss_stop(kthread_t *t, int why, int what)
20597c478bd9Sstevel@tonic-gate {
20607c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
20617c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	fss_inactive(t);
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate /*
20677c478bd9Sstevel@tonic-gate  * The current thread is exiting, do necessary adjustments to its project
20687c478bd9Sstevel@tonic-gate  */
20697c478bd9Sstevel@tonic-gate static void
20707c478bd9Sstevel@tonic-gate fss_exit(kthread_t *t)
20717c478bd9Sstevel@tonic-gate {
20727c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
20737c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
20747c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
20757c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
20767c478bd9Sstevel@tonic-gate 	int free = 0;
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	/*
20797c478bd9Sstevel@tonic-gate 	 * Thread t here is either a current thread (in which case we hold
20807c478bd9Sstevel@tonic-gate 	 * its process' p_lock), or a thread being destroyed by forklwp_fail(),
20817c478bd9Sstevel@tonic-gate 	 * in which case we hold pidlock and thread is no longer on the
20827c478bd9Sstevel@tonic-gate 	 * thread list.
20837c478bd9Sstevel@tonic-gate 	 */
20847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock) || MUTEX_HELD(&pidlock));
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
20877c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
20887c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
20897c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
20927c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	thread_lock(t);
20957c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
20967c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
20977c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_runnable == 0) {
20987c478bd9Sstevel@tonic-gate 			fsszone->fssz_shares -= fssproj->fssp_shares;
20997c478bd9Sstevel@tonic-gate 			if (--fsszone->fssz_runnable == 0)
21007c478bd9Sstevel@tonic-gate 				fsspset->fssps_shares -= fsszone->fssz_rshares;
21017c478bd9Sstevel@tonic-gate 		}
21027c478bd9Sstevel@tonic-gate 		ASSERT(fssproc->fss_runnable == 1);
21037c478bd9Sstevel@tonic-gate 		fssproc->fss_runnable = 0;
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_threads == 0) {
21067c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj);
21077c478bd9Sstevel@tonic-gate 		free = 1;
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
21107c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = NULL;	/* mark this thread as already exited */
21117c478bd9Sstevel@tonic-gate 	thread_unlock(t);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	if (free) {
21147c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
21157c478bd9Sstevel@tonic-gate 			kmem_free(fsszone, sizeof (fsszone_t));
21167c478bd9Sstevel@tonic-gate 		kmem_free(fssproj, sizeof (fssproj_t));
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
21197c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
2120c97ad5cdSakolb 
21218c34bbb7Sakolb 	/*
21228c34bbb7Sakolb 	 * A thread could be exiting in between clock ticks, so we need to
21238c34bbb7Sakolb 	 * calculate how much CPU time it used since it was charged last time.
21248c34bbb7Sakolb 	 *
21258c34bbb7Sakolb 	 * CPU caps are not enforced on exiting processes - it is usually
21268c34bbb7Sakolb 	 * desirable to exit as soon as possible to free resources.
21278c34bbb7Sakolb 	 */
2128c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2129c97ad5cdSakolb 		thread_lock(t);
2130c97ad5cdSakolb 		fssproc = FSSPROC(t);
2131c97ad5cdSakolb 		(void) cpucaps_charge(t, &fssproc->fss_caps,
2132c97ad5cdSakolb 		    CPUCAPS_CHARGE_ONLY);
2133c97ad5cdSakolb 		thread_unlock(t);
2134c97ad5cdSakolb 	}
21357c478bd9Sstevel@tonic-gate }
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate static void
21387c478bd9Sstevel@tonic-gate fss_nullsys()
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate /*
21437c478bd9Sstevel@tonic-gate  * fss_swapin() returns -1 if the thread is loaded or is not eligible to be
21447c478bd9Sstevel@tonic-gate  * swapped in. Otherwise, it returns the thread's effective priority based
21457c478bd9Sstevel@tonic-gate  * on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
21467c478bd9Sstevel@tonic-gate  */
21477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21487c478bd9Sstevel@tonic-gate static pri_t
21497c478bd9Sstevel@tonic-gate fss_swapin(kthread_t *t, int flags)
21507c478bd9Sstevel@tonic-gate {
21517c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
21527c478bd9Sstevel@tonic-gate 	long epri = -1;
21537c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
21587c478bd9Sstevel@tonic-gate 		time_t swapout_time;
21597c478bd9Sstevel@tonic-gate 
2160d3d50737SRafael Vanoni 		swapout_time = (ddi_get_lbolt() - t->t_stime) / hz;
21617c478bd9Sstevel@tonic-gate 		if (INHERITED(t) || (fssproc->fss_flags & FSSKPRI)) {
21627c478bd9Sstevel@tonic-gate 			epri = (long)DISP_PRIO(t) + swapout_time;
21637c478bd9Sstevel@tonic-gate 		} else {
21647c478bd9Sstevel@tonic-gate 			/*
21657c478bd9Sstevel@tonic-gate 			 * Threads which have been out for a long time,
21667c478bd9Sstevel@tonic-gate 			 * have high user mode priority and are associated
21677c478bd9Sstevel@tonic-gate 			 * with a small address space are more deserving.
21687c478bd9Sstevel@tonic-gate 			 */
21697c478bd9Sstevel@tonic-gate 			epri = fssproc->fss_umdpri;
21707c478bd9Sstevel@tonic-gate 			ASSERT(epri >= 0 && epri <= fss_maxumdpri);
21717c478bd9Sstevel@tonic-gate 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
21727c478bd9Sstevel@tonic-gate 		}
21737c478bd9Sstevel@tonic-gate 		/*
21747c478bd9Sstevel@tonic-gate 		 * Scale epri so that SHRT_MAX / 2 represents zero priority.
21757c478bd9Sstevel@tonic-gate 		 */
21767c478bd9Sstevel@tonic-gate 		epri += SHRT_MAX / 2;
21777c478bd9Sstevel@tonic-gate 		if (epri < 0)
21787c478bd9Sstevel@tonic-gate 			epri = 0;
21797c478bd9Sstevel@tonic-gate 		else if (epri > SHRT_MAX)
21807c478bd9Sstevel@tonic-gate 			epri = SHRT_MAX;
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
21837c478bd9Sstevel@tonic-gate }
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate /*
21867c478bd9Sstevel@tonic-gate  * fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
21877c478bd9Sstevel@tonic-gate  * be swapped out. Otherwise, it returns the thread's effective priority
21887c478bd9Sstevel@tonic-gate  * based on if the swapper is in softswap or hardswap mode.
21897c478bd9Sstevel@tonic-gate  */
21907c478bd9Sstevel@tonic-gate static pri_t
21917c478bd9Sstevel@tonic-gate fss_swapout(kthread_t *t, int flags)
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
21947c478bd9Sstevel@tonic-gate 	long epri = -1;
21957c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
21967c478bd9Sstevel@tonic-gate 	time_t swapin_time;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	if (INHERITED(t) ||
22017c478bd9Sstevel@tonic-gate 	    (fssproc->fss_flags & FSSKPRI) ||
22027c478bd9Sstevel@tonic-gate 	    (t->t_proc_flag & TP_LWPEXIT) ||
2203d4204c85Sraf 	    (t->t_state & (TS_ZOMB|TS_FREE|TS_STOPPED|TS_ONPROC|TS_WAIT)) ||
22047c478bd9Sstevel@tonic-gate 	    !(t->t_schedflag & TS_LOAD) ||
22057c478bd9Sstevel@tonic-gate 	    !(SWAP_OK(t)))
22067c478bd9Sstevel@tonic-gate 		return (-1);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
22097c478bd9Sstevel@tonic-gate 
2210d3d50737SRafael Vanoni 	swapin_time = (ddi_get_lbolt() - t->t_stime) / hz;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	if (flags == SOFTSWAP) {
22137c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
22147c478bd9Sstevel@tonic-gate 			epri = 0;
22157c478bd9Sstevel@tonic-gate 		} else {
22167c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
22177c478bd9Sstevel@tonic-gate 		}
22187c478bd9Sstevel@tonic-gate 	} else {
22197c478bd9Sstevel@tonic-gate 		pri_t pri;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 		if ((t->t_state == TS_SLEEP && swapin_time > fss_minslp) ||
22227c478bd9Sstevel@tonic-gate 		    (t->t_state == TS_RUN && swapin_time > fss_minrun)) {
22237c478bd9Sstevel@tonic-gate 			pri = fss_maxumdpri;
22247c478bd9Sstevel@tonic-gate 			epri = swapin_time -
22257c478bd9Sstevel@tonic-gate 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
22267c478bd9Sstevel@tonic-gate 		} else {
22277c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
22287c478bd9Sstevel@tonic-gate 		}
22297c478bd9Sstevel@tonic-gate 	}
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	/*
22327c478bd9Sstevel@tonic-gate 	 * Scale epri so that SHRT_MAX / 2 represents zero priority.
22337c478bd9Sstevel@tonic-gate 	 */
22347c478bd9Sstevel@tonic-gate 	epri += SHRT_MAX / 2;
22357c478bd9Sstevel@tonic-gate 	if (epri < 0)
22367c478bd9Sstevel@tonic-gate 		epri = 0;
22377c478bd9Sstevel@tonic-gate 	else if (epri > SHRT_MAX)
22387c478bd9Sstevel@tonic-gate 		epri = SHRT_MAX;
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
22417c478bd9Sstevel@tonic-gate }
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate /*
22447c478bd9Sstevel@tonic-gate  * If thread is currently at a kernel mode priority (has slept) and is
22457c478bd9Sstevel@tonic-gate  * returning to the userland we assign it the appropriate user mode priority
22467c478bd9Sstevel@tonic-gate  * and time quantum here.  If we're lowering the thread's priority below that
22477c478bd9Sstevel@tonic-gate  * of other runnable threads then we will set runrun via cpu_surrender() to
22487c478bd9Sstevel@tonic-gate  * cause preemption.
22497c478bd9Sstevel@tonic-gate  */
22507c478bd9Sstevel@tonic-gate static void
22517c478bd9Sstevel@tonic-gate fss_trapret(kthread_t *t)
22527c478bd9Sstevel@tonic-gate {
22537c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
22547c478bd9Sstevel@tonic-gate 	cpu_t *cp = CPU;
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
22577c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
22587c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_dispthread == t);
22597c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	t->t_kpri_req = 0;
22627c478bd9Sstevel@tonic-gate 	if (fssproc->fss_flags & FSSKPRI) {
22637c478bd9Sstevel@tonic-gate 		/*
22647c478bd9Sstevel@tonic-gate 		 * If thread has blocked in the kernel
22657c478bd9Sstevel@tonic-gate 		 */
22667c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
22677c478bd9Sstevel@tonic-gate 		cp->cpu_dispatch_pri = DISP_PRIO(t);
22687c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
22697c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSKPRI;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t))
22727c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
22737c478bd9Sstevel@tonic-gate 	}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	/*
22767c478bd9Sstevel@tonic-gate 	 * Swapout lwp if the swapper is waiting for this thread to reach
22777c478bd9Sstevel@tonic-gate 	 * a safe point.
22787c478bd9Sstevel@tonic-gate 	 */
22797c478bd9Sstevel@tonic-gate 	if (t->t_schedflag & TS_SWAPENQ) {
22807c478bd9Sstevel@tonic-gate 		thread_unlock(t);
22817c478bd9Sstevel@tonic-gate 		swapout_lwp(ttolwp(t));
22827c478bd9Sstevel@tonic-gate 		thread_lock(t);
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate /*
22877c478bd9Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location on dispatcher queue.
22887c478bd9Sstevel@tonic-gate  * This is called with the current thread in TS_ONPROC and locked.
22897c478bd9Sstevel@tonic-gate  */
22907c478bd9Sstevel@tonic-gate static void
22917c478bd9Sstevel@tonic-gate fss_preempt(kthread_t *t)
22927c478bd9Sstevel@tonic-gate {
22937c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
22947c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
22957c478bd9Sstevel@tonic-gate 	uint_t flags;
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
22987c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(curthread));
22997c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	/*
23027c478bd9Sstevel@tonic-gate 	 * If preempted in the kernel, make sure the thread has a kernel
23037c478bd9Sstevel@tonic-gate 	 * priority if needed.
23047c478bd9Sstevel@tonic-gate 	 */
23057c478bd9Sstevel@tonic-gate 	lwp = curthread->t_lwp;
23067c478bd9Sstevel@tonic-gate 	if (!(fssproc->fss_flags & FSSKPRI) && lwp != NULL && t->t_kpri_req) {
23077c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
23087c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
23097c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
23107c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
23117c478bd9Sstevel@tonic-gate 		aston(t);
23127c478bd9Sstevel@tonic-gate 	}
2313c97ad5cdSakolb 
2314c97ad5cdSakolb 	/*
2315c97ad5cdSakolb 	 * This thread may be placed on wait queue by CPU Caps. In this case we
2316c97ad5cdSakolb 	 * do not need to do anything until it is removed from the wait queue.
2317c97ad5cdSakolb 	 * Do not enforce CPU caps on threads running at a kernel priority
2318c97ad5cdSakolb 	 */
2319c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2320c97ad5cdSakolb 		(void) cpucaps_charge(t, &fssproc->fss_caps,
23218c34bbb7Sakolb 		    CPUCAPS_CHARGE_ENFORCE);
2322c97ad5cdSakolb 
2323c97ad5cdSakolb 		if (!(fssproc->fss_flags & FSSKPRI) && CPUCAPS_ENFORCE(t))
2324c97ad5cdSakolb 			return;
2325c97ad5cdSakolb 	}
2326c97ad5cdSakolb 
23277c478bd9Sstevel@tonic-gate 	/*
23287c478bd9Sstevel@tonic-gate 	 * If preempted in user-land mark the thread as swappable because it
23297c478bd9Sstevel@tonic-gate 	 * cannot be holding any kernel locks.
23307c478bd9Sstevel@tonic-gate 	 */
23317c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
23327c478bd9Sstevel@tonic-gate 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
23337c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	/*
23367c478bd9Sstevel@tonic-gate 	 * Check to see if we're doing "preemption control" here.  If
23377c478bd9Sstevel@tonic-gate 	 * we are, and if the user has requested that this thread not
23387c478bd9Sstevel@tonic-gate 	 * be preempted, and if preemptions haven't been put off for
23397c478bd9Sstevel@tonic-gate 	 * too long, let the preemption happen here but try to make
23409ac8606fSraf 	 * sure the thread is rescheduled as soon as possible.  We do
23417c478bd9Sstevel@tonic-gate 	 * this by putting it on the front of the highest priority run
23427c478bd9Sstevel@tonic-gate 	 * queue in the FSS class.  If the preemption has been put off
23437c478bd9Sstevel@tonic-gate 	 * for too long, clear the "nopreempt" bit and let the thread
23447c478bd9Sstevel@tonic-gate 	 * be preempted.
23457c478bd9Sstevel@tonic-gate 	 */
23467c478bd9Sstevel@tonic-gate 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
23477c478bd9Sstevel@tonic-gate 		if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
23487c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
23497c478bd9Sstevel@tonic-gate 			if (!(fssproc->fss_flags & FSSKPRI)) {
23509ac8606fSraf 				/*
23519ac8606fSraf 				 * If not already remembered, remember current
23529ac8606fSraf 				 * priority for restoration in fss_yield().
23539ac8606fSraf 				 */
23549ac8606fSraf 				if (!(fssproc->fss_flags & FSSRESTORE)) {
23559ac8606fSraf 					fssproc->fss_scpri = t->t_pri;
23569ac8606fSraf 					fssproc->fss_flags |= FSSRESTORE;
23579ac8606fSraf 				}
23587c478bd9Sstevel@tonic-gate 				THREAD_CHANGE_PRI(t, fss_maxumdpri);
23597c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_DONT_SWAP;
23607c478bd9Sstevel@tonic-gate 			}
23619ac8606fSraf 			schedctl_set_yield(t, 1);
23627c478bd9Sstevel@tonic-gate 			setfrontdq(t);
23637c478bd9Sstevel@tonic-gate 			return;
23647c478bd9Sstevel@tonic-gate 		} else {
23659ac8606fSraf 			if (fssproc->fss_flags & FSSRESTORE) {
23669ac8606fSraf 				THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
23679ac8606fSraf 				fssproc->fss_flags &= ~FSSRESTORE;
23689ac8606fSraf 			}
23697c478bd9Sstevel@tonic-gate 			schedctl_set_nopreempt(t, 0);
23707c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
23717c478bd9Sstevel@tonic-gate 			/*
23727c478bd9Sstevel@tonic-gate 			 * Fall through and be preempted below.
23737c478bd9Sstevel@tonic-gate 			 */
23747c478bd9Sstevel@tonic-gate 		}
23757c478bd9Sstevel@tonic-gate 	}
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	flags = fssproc->fss_flags & (FSSBACKQ | FSSKPRI);
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	if (flags == FSSBACKQ) {
23807c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
23817c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
23827c478bd9Sstevel@tonic-gate 		setbackdq(t);
23837c478bd9Sstevel@tonic-gate 	} else if (flags == (FSSBACKQ | FSSKPRI)) {
23847c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
23857c478bd9Sstevel@tonic-gate 		setbackdq(t);
23867c478bd9Sstevel@tonic-gate 	} else {
23877c478bd9Sstevel@tonic-gate 		setfrontdq(t);
23887c478bd9Sstevel@tonic-gate 	}
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate /*
23927c478bd9Sstevel@tonic-gate  * Called when a thread is waking up and is to be placed on the run queue.
23937c478bd9Sstevel@tonic-gate  */
23947c478bd9Sstevel@tonic-gate static void
23957c478bd9Sstevel@tonic-gate fss_setrun(kthread_t *t)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP || t->t_state == TS_STOPPED)
24027c478bd9Sstevel@tonic-gate 		fss_active(t);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * If previously were running at the kernel priority then keep that
24097c478bd9Sstevel@tonic-gate 	 * priority and the fss_timeleft doesn't matter.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) == 0)
24127c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
24137c478bd9Sstevel@tonic-gate 
2414d3d50737SRafael Vanoni 	if (t->t_disp_time != ddi_get_lbolt())
24157c478bd9Sstevel@tonic-gate 		setbackdq(t);
24167c478bd9Sstevel@tonic-gate 	else
24177c478bd9Sstevel@tonic-gate 		setfrontdq(t);
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate /*
24217c478bd9Sstevel@tonic-gate  * Prepare thread for sleep. We reset the thread priority so it will run at the
24227c478bd9Sstevel@tonic-gate  * kernel priority level when it wakes up.
24237c478bd9Sstevel@tonic-gate  */
24247c478bd9Sstevel@tonic-gate static void
24257c478bd9Sstevel@tonic-gate fss_sleep(kthread_t *t)
24267c478bd9Sstevel@tonic-gate {
24277c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
24307c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
2433c97ad5cdSakolb 
2434c97ad5cdSakolb 	/*
2435c97ad5cdSakolb 	 * Account for time spent on CPU before going to sleep.
2436c97ad5cdSakolb 	 */
24378c34bbb7Sakolb 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2438c97ad5cdSakolb 
24397c478bd9Sstevel@tonic-gate 	fss_inactive(t);
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	/*
24427c478bd9Sstevel@tonic-gate 	 * Assign a system priority to the thread and arrange for it to be
24437c478bd9Sstevel@tonic-gate 	 * retained when the thread is next placed on the run queue (i.e.,
24447c478bd9Sstevel@tonic-gate 	 * when it wakes up) instead of being given a new pri.  Also arrange
24457c478bd9Sstevel@tonic-gate 	 * for trapret processing as the thread leaves the system call so it
24467c478bd9Sstevel@tonic-gate 	 * will drop back to normal priority range.
24477c478bd9Sstevel@tonic-gate 	 */
24487c478bd9Sstevel@tonic-gate 	if (t->t_kpri_req) {
24497c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
24507c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
24517c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
24527c478bd9Sstevel@tonic-gate 		aston(t);
24537c478bd9Sstevel@tonic-gate 	} else if (fssproc->fss_flags & FSSKPRI) {
24547c478bd9Sstevel@tonic-gate 		/*
24557c478bd9Sstevel@tonic-gate 		 * The thread has done a THREAD_KPRI_REQUEST(), slept, then
24567c478bd9Sstevel@tonic-gate 		 * done THREAD_KPRI_RELEASE() (so no t_kpri_req is 0 again),
24577c478bd9Sstevel@tonic-gate 		 * then slept again all without finishing the current system
24587c478bd9Sstevel@tonic-gate 		 * call so trapret won't have cleared FSSKPRI
24597c478bd9Sstevel@tonic-gate 		 */
24607c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSKPRI;
24617c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
24627c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(curthread))
24637c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
24647c478bd9Sstevel@tonic-gate 	}
2465d3d50737SRafael Vanoni 	t->t_stime = ddi_get_lbolt();	/* time stamp for the swapper */
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate /*
24697c478bd9Sstevel@tonic-gate  * A tick interrupt has ocurrend on a running thread. Check to see if our
24707c478bd9Sstevel@tonic-gate  * time slice has expired.  We must also clear the TS_DONT_SWAP flag in
24717c478bd9Sstevel@tonic-gate  * t_schedflag if the thread is eligible to be swapped out.
24727c478bd9Sstevel@tonic-gate  */
24737c478bd9Sstevel@tonic-gate static void
24747c478bd9Sstevel@tonic-gate fss_tick(kthread_t *t)
24757c478bd9Sstevel@tonic-gate {
24767c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
24777c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
24787c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
2479c97ad5cdSakolb 	boolean_t call_cpu_surrender = B_FALSE;
2480c97ad5cdSakolb 	boolean_t cpucaps_enforce = B_FALSE;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	/*
24857c478bd9Sstevel@tonic-gate 	 * It's safe to access fsspset and fssproj structures because we're
24867c478bd9Sstevel@tonic-gate 	 * holding our p_lock here.
24877c478bd9Sstevel@tonic-gate 	 */
24887c478bd9Sstevel@tonic-gate 	thread_lock(t);
24897c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
24907c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
24917c478bd9Sstevel@tonic-gate 	if (fssproj != NULL) {
24927c478bd9Sstevel@tonic-gate 		fsspset_t *fsspset = FSSPROJ2FSSPSET(fssproj);
24937c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&fsspset->fssps_displock);
24947c478bd9Sstevel@tonic-gate 		fssproj->fssp_ticks += fss_nice_tick[fssproc->fss_nice];
2495*482a7749SJerry Jelinek 		fssproj->fssp_tick_cnt++;
24967c478bd9Sstevel@tonic-gate 		fssproc->fss_ticks++;
24977c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
24987c478bd9Sstevel@tonic-gate 	}
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	/*
2501c97ad5cdSakolb 	 * Keep track of thread's project CPU usage.  Note that projects
2502c97ad5cdSakolb 	 * get charged even when threads are running in the kernel.
2503c97ad5cdSakolb 	 * Do not surrender CPU if running in the SYS class.
2504c97ad5cdSakolb 	 */
2505c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2506c97ad5cdSakolb 		cpucaps_enforce = cpucaps_charge(t,
2507c97ad5cdSakolb 		    &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE) &&
2508c97ad5cdSakolb 		    !(fssproc->fss_flags & FSSKPRI);
2509c97ad5cdSakolb 	}
2510c97ad5cdSakolb 
2511c97ad5cdSakolb 	/*
25127c478bd9Sstevel@tonic-gate 	 * A thread's execution time for threads running in the SYS class
25137c478bd9Sstevel@tonic-gate 	 * is not tracked.
25147c478bd9Sstevel@tonic-gate 	 */
25157c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) == 0) {
25167c478bd9Sstevel@tonic-gate 		/*
25177c478bd9Sstevel@tonic-gate 		 * If thread is not in kernel mode, decrement its fss_timeleft
25187c478bd9Sstevel@tonic-gate 		 */
25197c478bd9Sstevel@tonic-gate 		if (--fssproc->fss_timeleft <= 0) {
25207c478bd9Sstevel@tonic-gate 			pri_t new_pri;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 			/*
25237c478bd9Sstevel@tonic-gate 			 * If we're doing preemption control and trying to
25247c478bd9Sstevel@tonic-gate 			 * avoid preempting this thread, just note that the
25257c478bd9Sstevel@tonic-gate 			 * thread should yield soon and let it keep running
25267c478bd9Sstevel@tonic-gate 			 * (unless it's been a while).
25277c478bd9Sstevel@tonic-gate 			 */
25287c478bd9Sstevel@tonic-gate 			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
25297c478bd9Sstevel@tonic-gate 				if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
25307c478bd9Sstevel@tonic-gate 					DTRACE_SCHED1(schedctl__nopreempt,
25317c478bd9Sstevel@tonic-gate 					    kthread_t *, t);
25327c478bd9Sstevel@tonic-gate 					schedctl_set_yield(t, 1);
25337c478bd9Sstevel@tonic-gate 					thread_unlock_nopreempt(t);
25347c478bd9Sstevel@tonic-gate 					return;
25357c478bd9Sstevel@tonic-gate 				}
25367c478bd9Sstevel@tonic-gate 			}
25379ac8606fSraf 			fssproc->fss_flags &= ~FSSRESTORE;
25387c478bd9Sstevel@tonic-gate 
2539*482a7749SJerry Jelinek 			fss_newpri(fssproc, B_TRUE);
25407c478bd9Sstevel@tonic-gate 			new_pri = fssproc->fss_umdpri;
25417c478bd9Sstevel@tonic-gate 			ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 			/*
25447c478bd9Sstevel@tonic-gate 			 * When the priority of a thread is changed, it may
25457c478bd9Sstevel@tonic-gate 			 * be necessary to adjust its position on a sleep queue
25467c478bd9Sstevel@tonic-gate 			 * or dispatch queue. The function thread_change_pri
25477c478bd9Sstevel@tonic-gate 			 * accomplishes this.
25487c478bd9Sstevel@tonic-gate 			 */
25497c478bd9Sstevel@tonic-gate 			if (thread_change_pri(t, new_pri, 0)) {
25507c478bd9Sstevel@tonic-gate 				if ((t->t_schedflag & TS_LOAD) &&
25517c478bd9Sstevel@tonic-gate 				    (lwp = t->t_lwp) &&
25527c478bd9Sstevel@tonic-gate 				    lwp->lwp_state == LWP_USER)
25537c478bd9Sstevel@tonic-gate 					t->t_schedflag &= ~TS_DONT_SWAP;
25547c478bd9Sstevel@tonic-gate 				fssproc->fss_timeleft = fss_quantum;
25557c478bd9Sstevel@tonic-gate 			} else {
2556c97ad5cdSakolb 				call_cpu_surrender = B_TRUE;
25577c478bd9Sstevel@tonic-gate 			}
2558b3383343Smishra 		} else if (t->t_state == TS_ONPROC &&
2559b3383343Smishra 		    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
25607c478bd9Sstevel@tonic-gate 			/*
25617c478bd9Sstevel@tonic-gate 			 * If there is a higher-priority thread which is
25627c478bd9Sstevel@tonic-gate 			 * waiting for a processor, then thread surrenders
25637c478bd9Sstevel@tonic-gate 			 * the processor.
25647c478bd9Sstevel@tonic-gate 			 */
2565c97ad5cdSakolb 			call_cpu_surrender = B_TRUE;
2566c97ad5cdSakolb 		}
2567c97ad5cdSakolb 	}
2568c97ad5cdSakolb 
2569c97ad5cdSakolb 	if (cpucaps_enforce && 2 * fssproc->fss_timeleft > fss_quantum) {
2570c97ad5cdSakolb 		/*
2571c97ad5cdSakolb 		 * The thread used more than half of its quantum, so assume that
2572c97ad5cdSakolb 		 * it used the whole quantum.
2573c97ad5cdSakolb 		 *
2574c97ad5cdSakolb 		 * Update thread's priority just before putting it on the wait
2575c97ad5cdSakolb 		 * queue so that it gets charged for the CPU time from its
2576c97ad5cdSakolb 		 * quantum even before that quantum expires.
2577c97ad5cdSakolb 		 */
2578*482a7749SJerry Jelinek 		fss_newpri(fssproc, B_FALSE);
2579c97ad5cdSakolb 		if (t->t_pri != fssproc->fss_umdpri)
2580c97ad5cdSakolb 			fss_change_priority(t, fssproc);
2581c97ad5cdSakolb 
2582c97ad5cdSakolb 		/*
2583c97ad5cdSakolb 		 * We need to call cpu_surrender for this thread due to cpucaps
2584c97ad5cdSakolb 		 * enforcement, but fss_change_priority may have already done
2585c97ad5cdSakolb 		 * so. In this case FSSBACKQ is set and there is no need to call
2586c97ad5cdSakolb 		 * cpu-surrender again.
2587c97ad5cdSakolb 		 */
2588c97ad5cdSakolb 		if (!(fssproc->fss_flags & FSSBACKQ))
2589c97ad5cdSakolb 			call_cpu_surrender = B_TRUE;
2590c97ad5cdSakolb 	}
2591c97ad5cdSakolb 
2592c97ad5cdSakolb 	if (call_cpu_surrender) {
25937c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSBACKQ;
25947c478bd9Sstevel@tonic-gate 		cpu_surrender(t);
25957c478bd9Sstevel@tonic-gate 	}
2596c97ad5cdSakolb 
25977c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate /*
26017c478bd9Sstevel@tonic-gate  * Processes waking up go to the back of their queue.  We don't need to assign
26027c478bd9Sstevel@tonic-gate  * a time quantum here because thread is still at a kernel mode priority and
26037c478bd9Sstevel@tonic-gate  * the time slicing is not done for threads running in the kernel after
26047c478bd9Sstevel@tonic-gate  * sleeping.  The proper time quantum will be assigned by fss_trapret before the
26057c478bd9Sstevel@tonic-gate  * thread returns to user mode.
26067c478bd9Sstevel@tonic-gate  */
26077c478bd9Sstevel@tonic-gate static void
26087c478bd9Sstevel@tonic-gate fss_wakeup(kthread_t *t)
26097c478bd9Sstevel@tonic-gate {
26107c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
26137c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_SLEEP);
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	fss_active(t);
26167c478bd9Sstevel@tonic-gate 
2617d3d50737SRafael Vanoni 	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
26187c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
26197c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	if (fssproc->fss_flags & FSSKPRI) {
26227c478bd9Sstevel@tonic-gate 		/*
26237c478bd9Sstevel@tonic-gate 		 * If we already have a kernel priority assigned, then we
26247c478bd9Sstevel@tonic-gate 		 * just use it.
26257c478bd9Sstevel@tonic-gate 		 */
26267c478bd9Sstevel@tonic-gate 		setbackdq(t);
26277c478bd9Sstevel@tonic-gate 	} else if (t->t_kpri_req) {
26287c478bd9Sstevel@tonic-gate 		/*
26297c478bd9Sstevel@tonic-gate 		 * Give thread a priority boost if we were asked.
26307c478bd9Sstevel@tonic-gate 		 */
26317c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
26327c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
26337c478bd9Sstevel@tonic-gate 		setbackdq(t);
26347c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
26357c478bd9Sstevel@tonic-gate 		aston(t);
26367c478bd9Sstevel@tonic-gate 	} else {
26377c478bd9Sstevel@tonic-gate 		/*
26387c478bd9Sstevel@tonic-gate 		 * Otherwise, we recalculate the priority.
26397c478bd9Sstevel@tonic-gate 		 */
2640d3d50737SRafael Vanoni 		if (t->t_disp_time == ddi_get_lbolt()) {
26417c478bd9Sstevel@tonic-gate 			setfrontdq(t);
26427c478bd9Sstevel@tonic-gate 		} else {
26437c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
26447c478bd9Sstevel@tonic-gate 			THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
26457c478bd9Sstevel@tonic-gate 			setbackdq(t);
26467c478bd9Sstevel@tonic-gate 		}
26477c478bd9Sstevel@tonic-gate 	}
26487c478bd9Sstevel@tonic-gate }
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate /*
26517c478bd9Sstevel@tonic-gate  * fss_donice() is called when a nice(1) command is issued on the thread to
26527c478bd9Sstevel@tonic-gate  * alter the priority. The nice(1) command exists in Solaris for compatibility.
26537c478bd9Sstevel@tonic-gate  * Thread priority adjustments should be done via priocntl(1).
26547c478bd9Sstevel@tonic-gate  */
26557c478bd9Sstevel@tonic-gate static int
26567c478bd9Sstevel@tonic-gate fss_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
26577c478bd9Sstevel@tonic-gate {
26587c478bd9Sstevel@tonic-gate 	int newnice;
26597c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
26607c478bd9Sstevel@tonic-gate 	fssparms_t fssparms;
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	/*
26637c478bd9Sstevel@tonic-gate 	 * If there is no change to priority, just return current setting.
26647c478bd9Sstevel@tonic-gate 	 */
26657c478bd9Sstevel@tonic-gate 	if (incr == 0) {
26667c478bd9Sstevel@tonic-gate 		if (retvalp)
26677c478bd9Sstevel@tonic-gate 			*retvalp = fssproc->fss_nice - NZERO;
26687c478bd9Sstevel@tonic-gate 		return (0);
26697c478bd9Sstevel@tonic-gate 	}
26707c478bd9Sstevel@tonic-gate 
267124d819e6SJerry Jelinek 	if ((incr < 0 || incr > 2 * NZERO) && secpolicy_raisepriority(cr) != 0)
26727c478bd9Sstevel@tonic-gate 		return (EPERM);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	/*
26757c478bd9Sstevel@tonic-gate 	 * Specifying a nice increment greater than the upper limit of
26767c478bd9Sstevel@tonic-gate 	 * FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
26777c478bd9Sstevel@tonic-gate 	 * value being set to the upper limit.  We check for this before
26787c478bd9Sstevel@tonic-gate 	 * computing the new value because otherwise we could get overflow
26797c478bd9Sstevel@tonic-gate 	 * if a privileged user specified some ridiculous increment.
26807c478bd9Sstevel@tonic-gate 	 */
26817c478bd9Sstevel@tonic-gate 	if (incr > FSS_NICE_MAX)
26827c478bd9Sstevel@tonic-gate 		incr = FSS_NICE_MAX;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	newnice = fssproc->fss_nice + incr;
26857c478bd9Sstevel@tonic-gate 	if (newnice > FSS_NICE_MAX)
26867c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MAX;
26877c478bd9Sstevel@tonic-gate 	else if (newnice < FSS_NICE_MIN)
26887c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MIN;
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	fssparms.fss_uprilim = fssparms.fss_upri =
26917c478bd9Sstevel@tonic-gate 	    -((newnice - NZERO) * fss_maxupri) / NZERO;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	/*
26947c478bd9Sstevel@tonic-gate 	 * Reset the uprilim and upri values of the thread.
26957c478bd9Sstevel@tonic-gate 	 */
26967c478bd9Sstevel@tonic-gate 	(void) fss_parmsset(t, (void *)&fssparms, (id_t)0, (cred_t *)NULL);
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	/*
26997c478bd9Sstevel@tonic-gate 	 * Although fss_parmsset already reset fss_nice it may not have been
27007c478bd9Sstevel@tonic-gate 	 * set to precisely the value calculated above because fss_parmsset
27017c478bd9Sstevel@tonic-gate 	 * determines the nice value from the user priority and we may have
27027c478bd9Sstevel@tonic-gate 	 * truncated during the integer conversion from nice value to user
27037c478bd9Sstevel@tonic-gate 	 * priority and back. We reset fss_nice to the value we calculated
27047c478bd9Sstevel@tonic-gate 	 * above.
27057c478bd9Sstevel@tonic-gate 	 */
27067c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = (char)newnice;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	if (retvalp)
27097c478bd9Sstevel@tonic-gate 		*retvalp = newnice - NZERO;
27107c478bd9Sstevel@tonic-gate 	return (0);
27117c478bd9Sstevel@tonic-gate }
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate /*
2714d4204c85Sraf  * Increment the priority of the specified thread by incr and
2715d4204c85Sraf  * return the new value in *retvalp.
2716d4204c85Sraf  */
2717d4204c85Sraf static int
2718d4204c85Sraf fss_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2719d4204c85Sraf {
2720d4204c85Sraf 	int newpri;
2721d4204c85Sraf 	fssproc_t *fssproc = FSSPROC(t);
2722d4204c85Sraf 	fssparms_t fssparms;
2723d4204c85Sraf 
2724d4204c85Sraf 	/*
2725d4204c85Sraf 	 * If there is no change to priority, just return current setting.
2726d4204c85Sraf 	 */
2727d4204c85Sraf 	if (incr == 0) {
2728d4204c85Sraf 		*retvalp = fssproc->fss_upri;
2729d4204c85Sraf 		return (0);
2730d4204c85Sraf 	}
2731d4204c85Sraf 
2732d4204c85Sraf 	newpri = fssproc->fss_upri + incr;
2733d4204c85Sraf 	if (newpri > fss_maxupri || newpri < -fss_maxupri)
2734d4204c85Sraf 		return (EINVAL);
2735d4204c85Sraf 
2736d4204c85Sraf 	*retvalp = newpri;
2737d4204c85Sraf 	fssparms.fss_uprilim = fssparms.fss_upri = newpri;
2738d4204c85Sraf 
2739d4204c85Sraf 	/*
2740d4204c85Sraf 	 * Reset the uprilim and upri values of the thread.
2741d4204c85Sraf 	 */
2742d4204c85Sraf 	return (fss_parmsset(t, &fssparms, (id_t)0, cr));
2743d4204c85Sraf }
2744d4204c85Sraf 
2745d4204c85Sraf /*
27467c478bd9Sstevel@tonic-gate  * Return the global scheduling priority that would be assigned to a thread
27477c478bd9Sstevel@tonic-gate  * entering the fair-sharing class with the fss_upri.
27487c478bd9Sstevel@tonic-gate  */
27497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27507c478bd9Sstevel@tonic-gate static pri_t
27517c478bd9Sstevel@tonic-gate fss_globpri(kthread_t *t)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	return (fss_maxumdpri / 2);
27567c478bd9Sstevel@tonic-gate }
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate /*
27597c478bd9Sstevel@tonic-gate  * Called from the yield(2) system call when a thread is yielding (surrendering)
27607c478bd9Sstevel@tonic-gate  * the processor. The kernel thread is placed at the back of a dispatch queue.
27617c478bd9Sstevel@tonic-gate  */
27627c478bd9Sstevel@tonic-gate static void
27637c478bd9Sstevel@tonic-gate fss_yield(kthread_t *t)
27647c478bd9Sstevel@tonic-gate {
27657c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
27687c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 	/*
2771c97ad5cdSakolb 	 * Collect CPU usage spent before yielding
2772c97ad5cdSakolb 	 */
27738c34bbb7Sakolb 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2774c97ad5cdSakolb 
2775c97ad5cdSakolb 	/*
27767c478bd9Sstevel@tonic-gate 	 * Clear the preemption control "yield" bit since the user is
27777c478bd9Sstevel@tonic-gate 	 * doing a yield.
27787c478bd9Sstevel@tonic-gate 	 */
27797c478bd9Sstevel@tonic-gate 	if (t->t_schedctl)
27807c478bd9Sstevel@tonic-gate 		schedctl_set_yield(t, 0);
27819ac8606fSraf 	/*
27829ac8606fSraf 	 * If fss_preempt() artifically increased the thread's priority
27839ac8606fSraf 	 * to avoid preemption, restore the original priority now.
27849ac8606fSraf 	 */
27859ac8606fSraf 	if (fssproc->fss_flags & FSSRESTORE) {
27869ac8606fSraf 		THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
27879ac8606fSraf 		fssproc->fss_flags &= ~FSSRESTORE;
27889ac8606fSraf 	}
27897c478bd9Sstevel@tonic-gate 	if (fssproc->fss_timeleft < 0) {
27907c478bd9Sstevel@tonic-gate 		/*
27917c478bd9Sstevel@tonic-gate 		 * Time slice was artificially extended to avoid preemption,
27927c478bd9Sstevel@tonic-gate 		 * so pretend we're preempting it now.
27937c478bd9Sstevel@tonic-gate 		 */
27947c478bd9Sstevel@tonic-gate 		DTRACE_SCHED1(schedctl__yield, int, -fssproc->fss_timeleft);
27957c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
27987c478bd9Sstevel@tonic-gate 	setbackdq(t);
27997c478bd9Sstevel@tonic-gate }
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate void
28027c478bd9Sstevel@tonic-gate fss_changeproj(kthread_t *t, void *kp, void *zp, fssbuf_t *projbuf,
28037c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate 	kproject_t *kpj_new = kp;
28067c478bd9Sstevel@tonic-gate 	zone_t *zone = zp;
28077c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
28087c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
28097c478bd9Sstevel@tonic-gate 	kproject_t *kpj_old;
28107c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
28117c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
28127c478bd9Sstevel@tonic-gate 	int free = 0;
28137c478bd9Sstevel@tonic-gate 	int id;
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
28167c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
28177c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
28207c478bd9Sstevel@tonic-gate 		return;
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
28237c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
28247c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
28257c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
28267c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
28277c478bd9Sstevel@tonic-gate 		return;
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj_old);
28317c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
28327c478bd9Sstevel@tonic-gate 	kpj_old = FSSPROJ2KPROJ(fssproj_old);
28337c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	ASSERT(t->t_cpupart == fsspset->fssps_cpupart);
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	if (kpj_old == kpj_new) {
28387c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
28397c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
28407c478bd9Sstevel@tonic-gate 		return;
28417c478bd9Sstevel@tonic-gate 	}
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset, zone)) == NULL) {
28447c478bd9Sstevel@tonic-gate 		/*
28457c478bd9Sstevel@tonic-gate 		 * If the zone for the new project is not currently active on
28467c478bd9Sstevel@tonic-gate 		 * the cpu partition we're on, get one of the pre-allocated
28477c478bd9Sstevel@tonic-gate 		 * buffers and link it in our per-pset zone list.  Such buffers
28487c478bd9Sstevel@tonic-gate 		 * should already exist.
28497c478bd9Sstevel@tonic-gate 		 */
28507c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
28517c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
28527c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset, zone, fsszone_new);
28537c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
28547c478bd9Sstevel@tonic-gate 				break;
28557c478bd9Sstevel@tonic-gate 			}
28567c478bd9Sstevel@tonic-gate 		}
28577c478bd9Sstevel@tonic-gate 	}
28587c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
28597c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset, kpj_new)) == NULL) {
28607c478bd9Sstevel@tonic-gate 		/*
28617c478bd9Sstevel@tonic-gate 		 * If our new project is not currently running
28627c478bd9Sstevel@tonic-gate 		 * on the cpu partition we're on, get one of the
28637c478bd9Sstevel@tonic-gate 		 * pre-allocated buffers and link it in our new cpu
28647c478bd9Sstevel@tonic-gate 		 * partition doubly linked list. Such buffers should already
28657c478bd9Sstevel@tonic-gate 		 * exist.
28667c478bd9Sstevel@tonic-gate 		 */
28677c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
28687c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
28697c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset, kpj_new,
28707c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
28717c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
28727c478bd9Sstevel@tonic-gate 				break;
28737c478bd9Sstevel@tonic-gate 			}
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	thread_lock(t);
2879c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2880c97ad5cdSakolb 	    t->t_state == TS_WAIT)
28817c478bd9Sstevel@tonic-gate 		fss_inactive(t);
28827c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_old->fssp_threads > 0);
28837c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
28847c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj_old);
28857c478bd9Sstevel@tonic-gate 		free = 1;
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
28887c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
28897c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
2890c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2891c97ad5cdSakolb 	    t->t_state == TS_WAIT)
28927c478bd9Sstevel@tonic-gate 		fss_active(t);
28937c478bd9Sstevel@tonic-gate 	thread_unlock(t);
28947c478bd9Sstevel@tonic-gate 	if (free) {
28957c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
28967c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
28977c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
28987c478bd9Sstevel@tonic-gate 	}
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
29017c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
29027c478bd9Sstevel@tonic-gate }
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate void
29057c478bd9Sstevel@tonic-gate fss_changepset(kthread_t *t, void *newcp, fssbuf_t *projbuf,
29067c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
29077c478bd9Sstevel@tonic-gate {
29087c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset_old, *fsspset_new;
29097c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
29107c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
29117c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
29127c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
29137c478bd9Sstevel@tonic-gate 	zone_t *zone;
29147c478bd9Sstevel@tonic-gate 	int id;
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
29177c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
29187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
29217c478bd9Sstevel@tonic-gate 		return;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
29247c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
29257c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
29267c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
29277c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
29287c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
29297c478bd9Sstevel@tonic-gate 		return;
29307c478bd9Sstevel@tonic-gate 	}
29317c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
29327c478bd9Sstevel@tonic-gate 	fsspset_old = FSSPROJ2FSSPSET(fssproj_old);
29337c478bd9Sstevel@tonic-gate 	kpj = FSSPROJ2KPROJ(fssproj_old);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (fsspset_old->fssps_cpupart == newcp) {
29367c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
29377c478bd9Sstevel@tonic-gate 		return;
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	ASSERT(ttoproj(t) == kpj);
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	fsspset_new = fss_find_fsspset(newcp);
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_new->fssps_lock);
29457c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset_new, zone)) == NULL) {
29467c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
29477c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
29487c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset_new, zone,
29497c478bd9Sstevel@tonic-gate 				    fsszone_new);
29507c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
29517c478bd9Sstevel@tonic-gate 				break;
29527c478bd9Sstevel@tonic-gate 			}
29537c478bd9Sstevel@tonic-gate 		}
29547c478bd9Sstevel@tonic-gate 	}
29557c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
29567c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset_new, kpj)) == NULL) {
29577c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
29587c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
29597c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset_new, kpj,
29607c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
29617c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
29627c478bd9Sstevel@tonic-gate 				break;
29637c478bd9Sstevel@tonic-gate 			}
29647c478bd9Sstevel@tonic-gate 		}
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
29697c478bd9Sstevel@tonic-gate 	thread_lock(t);
2970c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2971c97ad5cdSakolb 	    t->t_state == TS_WAIT)
29727c478bd9Sstevel@tonic-gate 		fss_inactive(t);
29737c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
29747c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
2975c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2976c97ad5cdSakolb 	    t->t_state == TS_WAIT)
29777c478bd9Sstevel@tonic-gate 		fss_active(t);
29787c478bd9Sstevel@tonic-gate 	thread_unlock(t);
29797c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_new->fssps_lock);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_old->fssps_lock);
29827c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
29837c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset_old, fssproj_old);
29847c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
29857c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
29867c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
29877c478bd9Sstevel@tonic-gate 	}
29887c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_old->fssps_lock);
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
29917c478bd9Sstevel@tonic-gate }
2992