xref: /titanic_51/usr/src/uts/common/disp/fss.c (revision 2c164fafa089aa352e513b095e1ecd0abd29c61f)
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.
24*2c164fafSPatrick Mooney  * Copyright 2019 Joyent, Inc.
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 /*
58482a7749SJerry Jelinek  * The fair share scheduling class ensures that collections of processes
59482a7749SJerry Jelinek  * (zones and projects) each get their configured share of CPU.  This is in
60482a7749SJerry Jelinek  * contrast to the TS class which considers individual processes.
61482a7749SJerry Jelinek  *
62482a7749SJerry Jelinek  * The FSS cpu-share is set on zones using the zone.cpu-shares rctl and on
63482a7749SJerry Jelinek  * projects using the project.cpu-shares rctl.  By default the value is 1
64482a7749SJerry Jelinek  * and it can range from 0 - 64k.  A value of 0 means that processes in the
65482a7749SJerry Jelinek  * collection will only get CPU resources when there are no other processes
66482a7749SJerry Jelinek  * that need CPU. The cpu-share is used as one of the inputs to calculate a
67482a7749SJerry Jelinek  * thread's "user-mode" priority (umdpri) for the scheduler.  The umdpri falls
68482a7749SJerry Jelinek  * in the range 0-59.  FSS calculates other, internal, priorities which are not
69482a7749SJerry Jelinek  * visible outside of the FSS class.
70482a7749SJerry Jelinek  *
71482a7749SJerry Jelinek  * The FSS class should approximate TS behavior when there are excess CPU
72482a7749SJerry Jelinek  * resources.  When there is a backlog of runnable processes, then the share
73482a7749SJerry Jelinek  * is used as input into the runnable process's priority calculation, where
74482a7749SJerry Jelinek  * the final umdpri is used by the scheduler to determine when the process runs.
75482a7749SJerry Jelinek  *
76482a7749SJerry Jelinek  * Projects in a zone compete with each other for CPU time, receiving CPU
77482a7749SJerry Jelinek  * allocation within a zone proportional to the project's share; at a higher
78482a7749SJerry Jelinek  * level zones compete with each other, receiving allocation in a pset
79482a7749SJerry Jelinek  * proportional to the zone's share.
80482a7749SJerry Jelinek  *
81482a7749SJerry Jelinek  * The FSS priority calculation consists of several parts.
82482a7749SJerry Jelinek  *
83482a7749SJerry Jelinek  * 1) Once per second the fss_update function runs. The first thing it does is
84482a7749SJerry Jelinek  *    call fss_decay_usage. This function does three things.
85482a7749SJerry Jelinek  *
86482a7749SJerry Jelinek  * a) fss_decay_usage first decays the maxfsspri value for the pset.  This
87482a7749SJerry Jelinek  *    value is used in the per-process priority calculation described in step
88482a7749SJerry Jelinek  *    (2b).  The maxfsspri is decayed using the following formula:
89482a7749SJerry Jelinek  *
90482a7749SJerry Jelinek  *                      maxfsspri * fss_nice_decay[NZERO])
91482a7749SJerry Jelinek  *        maxfsspri =  ------------------------------------
92482a7749SJerry Jelinek  *                            FSS_DECAY_BASE
93482a7749SJerry Jelinek  *
94482a7749SJerry Jelinek  *
95482a7749SJerry Jelinek  *     - NZERO is the default process priority (i.e. 20)
96482a7749SJerry Jelinek  *
97482a7749SJerry Jelinek  *    The fss_nice_decay array is a fixed set of values used to adjust the
98482a7749SJerry Jelinek  *    decay rate of processes based on their nice value.  Entries in this
99482a7749SJerry Jelinek  *    array are initialized in fss_init using the following formula:
100482a7749SJerry Jelinek  *
101482a7749SJerry Jelinek  *                        (FSS_DECAY_MAX - FSS_DECAY_MIN) * i
102482a7749SJerry Jelinek  *       FSS_DECAY_MIN + -------------------------------------
103482a7749SJerry Jelinek  *                               FSS_NICE_RANGE - 1
104482a7749SJerry Jelinek  *
105482a7749SJerry Jelinek  *     - FSS_DECAY_MIN is 82 = approximates 65% (82/128)
106482a7749SJerry Jelinek  *     - FSS_DECAY_MAX is 108 = approximates 85% (108/128)
107482a7749SJerry Jelinek  *     - FSS_NICE_RANGE is 40 (range is 0 - 39)
108482a7749SJerry Jelinek  *
109482a7749SJerry Jelinek  * b) The second thing fss_decay_usage does is update each project's "usage"
110482a7749SJerry Jelinek  *    for the last second and then recalculates the project's "share usage".
111482a7749SJerry Jelinek  *
112482a7749SJerry Jelinek  *    The usage value is the recent CPU usage for all of the threads in the
113482a7749SJerry Jelinek  *    project. It is decayed and updated this way:
114482a7749SJerry Jelinek  *
115482a7749SJerry Jelinek  *                  (usage * FSS_DECAY_USG)
116482a7749SJerry Jelinek  *        usage =  ------------------------- + ticks;
117482a7749SJerry Jelinek  *                       FSS_DECAY_BASE
118482a7749SJerry Jelinek  *
119482a7749SJerry Jelinek  *     - FSS_DECAY_BASE is 128 - used instead of 100 so we can shift vs divide
120482a7749SJerry Jelinek  *     - FSS_DECAY_USG is 96 - approximates 75% (96/128)
121482a7749SJerry Jelinek  *     - ticks is updated whenever a process in this project is running
122482a7749SJerry Jelinek  *       when the scheduler's tick processing fires. This is not a simple
123482a7749SJerry Jelinek  *       counter, the values are based on the entries in the fss_nice_tick
124482a7749SJerry Jelinek  *       array (see section 3 below). ticks is then reset to 0 so it can track
125482a7749SJerry Jelinek  *       the next seconds worth of nice-adjusted time for the project.
126482a7749SJerry Jelinek  *
127482a7749SJerry Jelinek  * c) The third thing fss_decay_usage does is update each project's "share
128482a7749SJerry Jelinek  *    usage" (shusage). This is the normalized usage value for the project and
129482a7749SJerry Jelinek  *    is calculated this way:
130482a7749SJerry Jelinek  *
131482a7749SJerry Jelinek  *                pset_shares^2    zone_int_shares^2
132482a7749SJerry Jelinek  *        usage * ------------- * ------------------
133482a7749SJerry Jelinek  *                kpj_shares^2	   zone_ext_shares^2
134482a7749SJerry Jelinek  *
135482a7749SJerry Jelinek  *    - usage - see (1b) for more details
136482a7749SJerry Jelinek  *    - pset_shares is the total of all *active* zone shares in the pset (by
137482a7749SJerry Jelinek  *      default there is only one pset)
138482a7749SJerry Jelinek  *    - kpj_shares is the individual project's share (project.cpu-shares rctl)
139482a7749SJerry Jelinek  *    - zone_int_shares is the sum of shares of all active projects within the
140482a7749SJerry Jelinek  *      zone (the zone-internal total)
141482a7749SJerry Jelinek  *    - zone_ext_shares is the share value for the zone (zone.cpu-shares rctl)
142482a7749SJerry Jelinek  *
143482a7749SJerry Jelinek  *    The shusage is used in step (2b) to calculate the thread's new internal
144482a7749SJerry Jelinek  *    priority. A larger shusage value leads to a lower priority.
145482a7749SJerry Jelinek  *
146482a7749SJerry Jelinek  * 2) The fss_update function then calls fss_update_list to update the priority
147482a7749SJerry Jelinek  *    of all threads. This does two things.
148482a7749SJerry Jelinek  *
149482a7749SJerry Jelinek  * a) First the thread's internal priority is decayed using the following
150482a7749SJerry Jelinek  *    formula:
151482a7749SJerry Jelinek  *
152482a7749SJerry Jelinek  *                  fsspri * fss_nice_decay[nice_value])
153482a7749SJerry Jelinek  *        fsspri =  ------------------------------------
154482a7749SJerry Jelinek  *                            FSS_DECAY_BASE
155482a7749SJerry Jelinek  *
156482a7749SJerry Jelinek  *     - FSS_DECAY_BASE is 128 as described above
157482a7749SJerry Jelinek  *
158482a7749SJerry Jelinek  * b) Second, if the thread is runnable (TS_RUN or TS_WAIT) calls fss_newpri
159482a7749SJerry Jelinek  *    to update the user-mode priority (umdpri) of the runnable thread.
160482a7749SJerry Jelinek  *    Threads that are running (TS_ONPROC) or waiting for an event (TS_SLEEP)
161482a7749SJerry Jelinek  *    are not updated at this time. The updated user-mode priority can cause
162482a7749SJerry Jelinek  *    threads to change their position in the run queue.
163482a7749SJerry Jelinek  *
164482a7749SJerry Jelinek  *    The process's new internal fsspri is calculated using the following
165482a7749SJerry Jelinek  *    formula. All runnable threads in the project will use the same shusage
166482a7749SJerry Jelinek  *    and nrunnable values in their calculation.
167482a7749SJerry Jelinek  *
168482a7749SJerry Jelinek  *        fsspri += shusage * nrunnable * ticks
169482a7749SJerry Jelinek  *
170482a7749SJerry Jelinek  *     - shusage is the project's share usage, calculated in (1c)
171482a7749SJerry Jelinek  *     - nrunnable is the number of runnable threads in the project
172482a7749SJerry Jelinek  *     - ticks is the number of ticks this thread ran since the last fss_newpri
173482a7749SJerry Jelinek  *       invocation.
174482a7749SJerry Jelinek  *
175482a7749SJerry Jelinek  *    Finally the process's new user-mode priority is calculated using the
176482a7749SJerry Jelinek  *    following formula:
177482a7749SJerry Jelinek  *
178482a7749SJerry Jelinek  *                              (fsspri * umdprirange)
179482a7749SJerry Jelinek  *        umdpri = maxumdpri - ------------------------
180482a7749SJerry Jelinek  *                                    maxfsspri
181482a7749SJerry Jelinek  *
182482a7749SJerry Jelinek  *     - maxumdpri is MINCLSYSPRI - 1 (i.e. 59)
183482a7749SJerry Jelinek  *     - umdprirange is maxumdpri - 1 (i.e. 58)
184482a7749SJerry Jelinek  *     - maxfsspri is the largest fsspri seen so far, as we're iterating all
185482a7749SJerry Jelinek  *       runnable processes
186482a7749SJerry Jelinek  *
187482a7749SJerry Jelinek  *    Thus, a higher internal priority (fsspri) leads to a lower user-mode
188482a7749SJerry Jelinek  *    priority which means the thread runs less. The fsspri is higher when
189482a7749SJerry Jelinek  *    the project's normalized share usage is higher, when the project has
190482a7749SJerry Jelinek  *    more runnable threads, or when the thread has accumulated more run-time.
191482a7749SJerry Jelinek  *
192482a7749SJerry Jelinek  *    This code has various checks to ensure the resulting umdpri is in the
193482a7749SJerry Jelinek  *    range 1-59.  See fss_newpri for more details.
194482a7749SJerry Jelinek  *
195482a7749SJerry Jelinek  * To reiterate, the above processing is performed once per second to recompute
196482a7749SJerry Jelinek  * the runnable thread user-mode priorities.
197482a7749SJerry Jelinek  *
198482a7749SJerry Jelinek  * 3) The final major component in the priority calculation is the tick
199482a7749SJerry Jelinek  *    processing which occurs on a thread that is running when the clock
200482a7749SJerry Jelinek  *    calls fss_tick.
201482a7749SJerry Jelinek  *
202482a7749SJerry Jelinek  *    A thread can run continuously in user-land (compute-bound) for the
203482a7749SJerry Jelinek  *    fss_quantum (see "dispadmin -c FSS -g" for the configurable properties).
204482a7749SJerry Jelinek  *    The fss_quantum defaults to 11 (i.e. 11 ticks).
205482a7749SJerry Jelinek  *
206482a7749SJerry Jelinek  *    Once the quantum has been consumed, the thread will call fss_newpri to
207482a7749SJerry Jelinek  *    recompute its umdpri priority, as described above in (2b). Threads that
208482a7749SJerry Jelinek  *    were T_ONPROC at the one second interval when runnable thread priorities
209482a7749SJerry Jelinek  *    were recalculated will have their umdpri priority recalculated when their
210482a7749SJerry Jelinek  *    quanta expires.
211482a7749SJerry Jelinek  *
212482a7749SJerry Jelinek  *    To ensure that runnable threads within a project see the expected
213482a7749SJerry Jelinek  *    round-robin behavior, there is a special case in fss_newpri for a thread
214482a7749SJerry Jelinek  *    that has run for its quanta within the one second update interval.  See
215482a7749SJerry Jelinek  *    the handling for the quanta_up parameter within fss_newpri.
216482a7749SJerry Jelinek  *
217482a7749SJerry Jelinek  *    Also of interest, the fss_tick code increments the project's tick value
218482a7749SJerry Jelinek  *    using the fss_nice_tick array entry for the thread's nice value. The idea
219482a7749SJerry Jelinek  *    behind the fss_nice_tick array is that the cost of a tick is lower at
220482a7749SJerry Jelinek  *    positive nice values (so that it doesn't increase the project's usage
221482a7749SJerry Jelinek  *    as much as normal) with a 50% drop at the maximum level and a 50%
222482a7749SJerry Jelinek  *    increase at the minimum level. See (1b). The fss_nice_tick array is
223482a7749SJerry Jelinek  *    initialized in fss_init using the following formula:
224482a7749SJerry Jelinek  *
225482a7749SJerry Jelinek  *         FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2) - i)
226482a7749SJerry Jelinek  *        --------------------------------------------------
227482a7749SJerry Jelinek  *                          FSS_NICE_RANGE
228482a7749SJerry Jelinek  *
229482a7749SJerry Jelinek  *     - FSS_TICK_COST is 1000, the tick cost for threads with nice level 0
230482a7749SJerry 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 
356482a7749SJerry 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 /*
888482a7749SJerry Jelinek  * Calculate the new fss_umdpri based on the usage, the normalized share usage
889482a7749SJerry Jelinek  * and the number of active threads.  Reset the tick counter for this thread.
890482a7749SJerry Jelinek  *
891482a7749SJerry Jelinek  * When calculating the new priority using the standard formula we can hit
892482a7749SJerry Jelinek  * a scenario where we don't have good round-robin behavior.  This would be
893482a7749SJerry Jelinek  * most commonly seen when there is a zone with lots of runnable threads.
894482a7749SJerry Jelinek  * In the bad scenario we will see the following behavior when using the
895482a7749SJerry Jelinek  * standard formula and these conditions:
896482a7749SJerry Jelinek  *
897482a7749SJerry Jelinek  *	- there are multiple runnable threads in the zone (project)
898482a7749SJerry Jelinek  *	- the fssps_maxfsspri is a very large value
899482a7749SJerry Jelinek  *	- (we also know all of these threads will use the project's
900482a7749SJerry Jelinek  *	    fssp_shusage)
901482a7749SJerry Jelinek  *
902482a7749SJerry Jelinek  * Under these conditions, a thread with a low fss_fsspri value is chosen
903482a7749SJerry Jelinek  * to run and the thread gets a high fss_umdpri.  This thread can run for
904482a7749SJerry Jelinek  * its full quanta (fss_timeleft) at which time fss_newpri is called to
905482a7749SJerry Jelinek  * calculate the thread's new priority.
906482a7749SJerry Jelinek  *
907482a7749SJerry Jelinek  * In this case, because the newly calculated fsspri value is much smaller
908482a7749SJerry Jelinek  * (orders of magnitude) than the fssps_maxfsspri value, if we used the
909482a7749SJerry Jelinek  * standard formula the thread will still get a high fss_umdpri value and
910482a7749SJerry Jelinek  * will run again for another quanta, even though there are other runnable
911482a7749SJerry Jelinek  * threads in the project.
912482a7749SJerry Jelinek  *
913482a7749SJerry Jelinek  * For a thread that is runnable for a long time, the thread can continue
914482a7749SJerry Jelinek  * to run for many quanta (totaling many seconds) before the thread's fsspri
915482a7749SJerry Jelinek  * exceeds the fssps_maxfsspri and the thread's fss_umdpri is reset back
916482a7749SJerry Jelinek  * down to 1.  This behavior also keeps the fssps_maxfsspr at a high value,
917482a7749SJerry Jelinek  * so that the next runnable thread might repeat this cycle.
918482a7749SJerry Jelinek  *
919482a7749SJerry Jelinek  * This leads to the case where we don't have round-robin behavior at quanta
920482a7749SJerry Jelinek  * granularity, but instead, runnable threads within the project only run
921482a7749SJerry Jelinek  * at several second intervals.
922482a7749SJerry Jelinek  *
923482a7749SJerry Jelinek  * To prevent this scenario from occuring, when a thread has consumed its
924482a7749SJerry Jelinek  * quanta and there are multiple runnable threads in the project, we
925482a7749SJerry Jelinek  * immediately cause the thread to hit fssps_maxfsspri so that it gets
926482a7749SJerry Jelinek  * reset back to 1 and another runnable thread in the project can run.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate static void
929482a7749SJerry 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;
936482a7749SJerry 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 
959482a7749SJerry Jelinek 	ticks = fssproc->fss_ticks;
960482a7749SJerry Jelinek 	fssproc->fss_ticks = 0;
961482a7749SJerry 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 
971482a7749SJerry Jelinek 	maxfsspri = fsspset->fssps_maxfsspri;
972482a7749SJerry Jelinek 	n_runnable = fssproj->fssp_runnable;
973482a7749SJerry Jelinek 
974482a7749SJerry Jelinek 	if (quanta_up && n_runnable > 1) {
975482a7749SJerry Jelinek 		fsspri = maxfsspri;
976482a7749SJerry Jelinek 	} else {
9777c478bd9Sstevel@tonic-gate 		/*
978482a7749SJerry Jelinek 		 * fsspri += fssp_shusage * nrunnable * ticks
979482a7749SJerry Jelinek 		 * If all three values are non-0, this typically calculates to
980482a7749SJerry Jelinek 		 * a large number (sometimes > 1M, sometimes > 100B) due to
981482a7749SJerry Jelinek 		 * fssp_shusage which can be > 1T.
9827c478bd9Sstevel@tonic-gate 		 */
9837c478bd9Sstevel@tonic-gate 		fsspri = fssproc->fss_fsspri;
984482a7749SJerry Jelinek 		fsspri += fssproj->fssp_shusage * n_runnable * ticks;
985482a7749SJerry Jelinek 	}
986482a7749SJerry Jelinek 
9877c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = fsspri;
9887c478bd9Sstevel@tonic-gate 
989482a7749SJerry Jelinek 	/*
990482a7749SJerry Jelinek 	 * fss_maxumdpri is normally 59, since FSS priorities are 0-59.
991482a7749SJerry Jelinek 	 * If the previous calculation resulted in 0 (e.g. was 0 and added 0
992482a7749SJerry Jelinek 	 * because ticks == 0), then instead of 0, we use the largest priority,
993482a7749SJerry Jelinek 	 * which is still small in comparison to the large numbers we typically
994482a7749SJerry Jelinek 	 * see.
995482a7749SJerry 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
1009482a7749SJerry Jelinek 	 * that has non-zero shares). Because of this check, maxfsspri can
1010482a7749SJerry Jelinek 	 * change as this function is called via the
1011482a7749SJerry Jelinek 	 * fss_update -> fss_update_list -> fss_newpri code path to update
1012482a7749SJerry Jelinek 	 * all runnable threads. See the code in fss_update for how we
1013482a7749SJerry Jelinek 	 * mitigate this issue.
1014482a7749SJerry Jelinek 	 *
10157c478bd9Sstevel@tonic-gate 	 * Note that this formula cannot produce out of bounds priority
1016482a7749SJerry 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 /*
1031482a7749SJerry Jelinek  * Decays usages of all running projects, resets their tick counters and
1032482a7749SJerry Jelinek  * calcluates the projects normalized share usage. Called once per second from
1033482a7749SJerry 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;
1045482a7749SJerry 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 
1056482a7749SJerry Jelinek 		fsspset->fssps_gen++;
1057482a7749SJerry 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 
1070482a7749SJerry Jelinek 		pset_shares = fsspset->fssps_shares;
1071482a7749SJerry 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 {
1079482a7749SJerry Jelinek 			fsszone = fssproj->fssp_fsszone;
1080482a7749SJerry Jelinek 			zp = fsszone->fssz_zone;
1081482a7749SJerry Jelinek 
10827c478bd9Sstevel@tonic-gate 			/*
1083482a7749SJerry Jelinek 			 * Reset zone's FSS stats if they are from a
1084482a7749SJerry Jelinek 			 * previous cycle.
1085482a7749SJerry Jelinek 			 */
1086482a7749SJerry Jelinek 			if (fsspset->fssps_gen != zp->zone_fss_gen) {
1087482a7749SJerry Jelinek 				zp->zone_fss_gen = fsspset->fssps_gen;
1088482a7749SJerry Jelinek 				zp->zone_run_ticks = 0;
1089482a7749SJerry Jelinek 			}
1090482a7749SJerry Jelinek 
1091482a7749SJerry Jelinek 			/*
1092482a7749SJerry Jelinek 			 * Decay project usage, then add in this cycle's
1093482a7749SJerry Jelinek 			 * nice tick value.
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			fssproj->fssp_usage =
10967c478bd9Sstevel@tonic-gate 			    (fssproj->fssp_usage * FSS_DECAY_USG) /
1097482a7749SJerry Jelinek 			    FSS_DECAY_BASE +
1098482a7749SJerry Jelinek 			    fssproj->fssp_ticks;
10997c478bd9Sstevel@tonic-gate 
1100482a7749SJerry Jelinek 			fssproj->fssp_ticks = 0;
1101482a7749SJerry Jelinek 			zp->zone_run_ticks += fssproj->fssp_tick_cnt;
1102482a7749SJerry Jelinek 			fssproj->fssp_tick_cnt = 0;
1103482a7749SJerry 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 			 */
1122482a7749SJerry 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;
1129482a7749SJerry 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;
1134482a7749SJerry Jelinek 
1135482a7749SJerry Jelinek 			/*
1136482a7749SJerry Jelinek 			 * If anything is runnable in the project, track the
1137482a7749SJerry Jelinek 			 * overall project share percent for monitoring useage.
1138482a7749SJerry Jelinek 			 */
1139482a7749SJerry Jelinek 			if (fssproj->fssp_runnable > 0) {
1140482a7749SJerry Jelinek 				uint32_t zone_shr_pct;
1141482a7749SJerry Jelinek 				uint32_t int_shr_pct;
1142482a7749SJerry Jelinek 
1143482a7749SJerry Jelinek 				/*
1144482a7749SJerry Jelinek 				 * Times 1000 to get tenths of a percent
1145482a7749SJerry Jelinek 				 *
1146482a7749SJerry Jelinek 				 *		  zone_ext_shares
1147482a7749SJerry Jelinek 				 * zone_shr_pct = ---------------
1148482a7749SJerry Jelinek 				 *		  pset_shares
1149482a7749SJerry Jelinek 				 *
1150482a7749SJerry Jelinek 				 *		  kpj_shares
1151482a7749SJerry Jelinek 				 * int_shr_pct =  ---------------
1152482a7749SJerry Jelinek 				 *		  zone_int_shares
1153482a7749SJerry Jelinek 				 */
1154482a7749SJerry Jelinek 				if (pset_shares == 0 || zone_int_shares == 0) {
1155482a7749SJerry Jelinek 					fssproj->fssp_shr_pct = 0;
1156482a7749SJerry Jelinek 				} else {
1157482a7749SJerry Jelinek 					zone_shr_pct =
1158482a7749SJerry Jelinek 					    (zone_ext_shares * 1000) /
1159482a7749SJerry Jelinek 					    pset_shares;
1160482a7749SJerry Jelinek 					int_shr_pct = (kpj_shares * 1000) /
1161482a7749SJerry Jelinek 					    zone_int_shares;
1162482a7749SJerry Jelinek 					fssproj->fssp_shr_pct =
1163482a7749SJerry Jelinek 					    (zone_shr_pct * int_shr_pct) /
1164482a7749SJerry Jelinek 					    1000;
1165482a7749SJerry Jelinek 				}
1166482a7749SJerry Jelinek 			} else {
1167482a7749SJerry Jelinek 				DTRACE_PROBE1(fss__prj__norun, fssproj_t *,
1168482a7749SJerry Jelinek 				    fssproj);
1169482a7749SJerry Jelinek 			}
1170482a7749SJerry 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) {
1178482a7749SJerry Jelinek 				uint32_t zone_shr_pct;
1179482a7749SJerry Jelinek 
11807c478bd9Sstevel@tonic-gate 				/*
11817c478bd9Sstevel@tonic-gate 				 * Project 0 in the global zone has 50%
1182482a7749SJerry Jelinek 				 * of its zone. See calculation above for
1183482a7749SJerry Jelinek 				 * the zone's share percent.
11847c478bd9Sstevel@tonic-gate 				 */
1185482a7749SJerry Jelinek 				if (pset_shares == 0)
1186482a7749SJerry Jelinek 					zone_shr_pct = 1000;
1187482a7749SJerry Jelinek 				else
1188482a7749SJerry Jelinek 					zone_shr_pct =
1189482a7749SJerry Jelinek 					    (zone_ext_shares * 1000) /
1190482a7749SJerry Jelinek 					    pset_shares;
1191482a7749SJerry Jelinek 
1192482a7749SJerry Jelinek 				fssproj->fssp_shr_pct = zone_shr_pct / 2;
1193482a7749SJerry 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
1225482a7749SJerry Jelinek 				 *
1226482a7749SJerry Jelinek 				 * shusage is one input to calculating fss_pri
1227482a7749SJerry Jelinek 				 * in fss_newpri(). Larger values tend toward
1228482a7749SJerry 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.
1300482a7749SJerry Jelinek  *
1301482a7749SJerry Jelinek  * Each time we're run (once/second) we may start at the next list and iterate
1302482a7749SJerry Jelinek  * through all of the lists. By starting with a different list, we mitigate any
1303482a7749SJerry 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
1329482a7749SJerry 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;
1358482a7749SJerry 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;
1376c97ad5cdSakolb 
13777c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
13787c478bd9Sstevel@tonic-gate 		if (fssproj == NULL)
13797c478bd9Sstevel@tonic-gate 			goto next;
1380482a7749SJerry Jelinek 
13817c478bd9Sstevel@tonic-gate 		if (fssproj->fssp_shares != 0) {
13827c478bd9Sstevel@tonic-gate 			/*
13837c478bd9Sstevel@tonic-gate 			 * Decay fsspri value.
13847c478bd9Sstevel@tonic-gate 			 */
13857c478bd9Sstevel@tonic-gate 			fsspri = fssproc->fss_fsspri;
13867c478bd9Sstevel@tonic-gate 			fsspri = (fsspri * fss_nice_decay[fssproc->fss_nice]) /
13877c478bd9Sstevel@tonic-gate 			    FSS_DECAY_BASE;
13887c478bd9Sstevel@tonic-gate 			fssproc->fss_fsspri = fsspri;
13897c478bd9Sstevel@tonic-gate 		}
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		if (t->t_schedctl && schedctl_get_nopreempt(t))
13927c478bd9Sstevel@tonic-gate 			goto next;
1393c97ad5cdSakolb 		if (t->t_state != TS_RUN && t->t_state != TS_WAIT) {
13947c478bd9Sstevel@tonic-gate 			/*
13957c478bd9Sstevel@tonic-gate 			 * Make next syscall/trap call fss_trapret
13967c478bd9Sstevel@tonic-gate 			 */
13977c478bd9Sstevel@tonic-gate 			t->t_trapret = 1;
13987c478bd9Sstevel@tonic-gate 			aston(t);
1399482a7749SJerry Jelinek 			if (t->t_state == TS_ONPROC)
1400482a7749SJerry Jelinek 				DTRACE_PROBE1(fss__onproc, fssproc_t *,
1401482a7749SJerry Jelinek 				    fssproc);
14027c478bd9Sstevel@tonic-gate 			goto next;
14037c478bd9Sstevel@tonic-gate 		}
1404482a7749SJerry Jelinek 		fss_newpri(fssproc, B_FALSE);
14057c478bd9Sstevel@tonic-gate 		updated = 1;
14067c478bd9Sstevel@tonic-gate 
1407482a7749SJerry Jelinek 		fss_umdpri = fssproc->fss_umdpri;
1408482a7749SJerry Jelinek 
14097c478bd9Sstevel@tonic-gate 		/*
14107c478bd9Sstevel@tonic-gate 		 * Only dequeue the thread if it needs to be moved; otherwise
14117c478bd9Sstevel@tonic-gate 		 * it should just round-robin here.
14127c478bd9Sstevel@tonic-gate 		 */
1413482a7749SJerry Jelinek 		if (t->t_pri != fss_umdpri)
14147c478bd9Sstevel@tonic-gate 			fss_change_priority(t, fssproc);
14157c478bd9Sstevel@tonic-gate next:
14167c478bd9Sstevel@tonic-gate 		thread_unlock(t);
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 	mutex_exit(&fss_listlock[i]);
14197c478bd9Sstevel@tonic-gate 	return (updated);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14237c478bd9Sstevel@tonic-gate static int
14247c478bd9Sstevel@tonic-gate fss_admin(caddr_t uaddr, cred_t *reqpcredp)
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	fssadmin_t fssadmin;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if (copyin(uaddr, &fssadmin, sizeof (fssadmin_t)))
14297c478bd9Sstevel@tonic-gate 		return (EFAULT);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	switch (fssadmin.fss_cmd) {
14327c478bd9Sstevel@tonic-gate 	case FSS_SETADMIN:
14337c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
14347c478bd9Sstevel@tonic-gate 			return (EPERM);
14357c478bd9Sstevel@tonic-gate 		if (fssadmin.fss_quantum <= 0 || fssadmin.fss_quantum >= hz)
14367c478bd9Sstevel@tonic-gate 			return (EINVAL);
14377c478bd9Sstevel@tonic-gate 		fss_quantum = fssadmin.fss_quantum;
14387c478bd9Sstevel@tonic-gate 		break;
14397c478bd9Sstevel@tonic-gate 	case FSS_GETADMIN:
14407c478bd9Sstevel@tonic-gate 		fssadmin.fss_quantum = fss_quantum;
14417c478bd9Sstevel@tonic-gate 		if (copyout(&fssadmin, uaddr, sizeof (fssadmin_t)))
14427c478bd9Sstevel@tonic-gate 			return (EFAULT);
14437c478bd9Sstevel@tonic-gate 		break;
14447c478bd9Sstevel@tonic-gate 	default:
14457c478bd9Sstevel@tonic-gate 		return (EINVAL);
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 	return (0);
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate static int
14517c478bd9Sstevel@tonic-gate fss_getclinfo(void *infop)
14527c478bd9Sstevel@tonic-gate {
14537c478bd9Sstevel@tonic-gate 	fssinfo_t *fssinfo = (fssinfo_t *)infop;
14547c478bd9Sstevel@tonic-gate 	fssinfo->fss_maxupri = fss_maxupri;
14557c478bd9Sstevel@tonic-gate 	return (0);
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate static int
14597c478bd9Sstevel@tonic-gate fss_parmsin(void *parmsp)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	/*
14647c478bd9Sstevel@tonic-gate 	 * Check validity of parameters.
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_uprilim > fss_maxupri ||
14677c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim < -fss_maxupri) &&
14687c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim != FSS_NOCHANGE)
14697c478bd9Sstevel@tonic-gate 		return (EINVAL);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_upri > fss_maxupri ||
14727c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri < -fss_maxupri) &&
14737c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri != FSS_NOCHANGE)
14747c478bd9Sstevel@tonic-gate 		return (EINVAL);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	return (0);
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14807c478bd9Sstevel@tonic-gate static int
14817c478bd9Sstevel@tonic-gate fss_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	return (0);
14847c478bd9Sstevel@tonic-gate }
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate static int
14877c478bd9Sstevel@tonic-gate fss_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
14887c478bd9Sstevel@tonic-gate {
14897c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
14907c478bd9Sstevel@tonic-gate 	int priflag = 0;
14917c478bd9Sstevel@tonic-gate 	int limflag = 0;
14927c478bd9Sstevel@tonic-gate 	uint_t cnt;
14937c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/*
14967c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE (-32768) is outside of the range of values for
14977c478bd9Sstevel@tonic-gate 	 * fss_uprilim and fss_upri.  If the structure fssparms_t is changed,
14987c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE should be replaced by a flag word.
14997c478bd9Sstevel@tonic-gate 	 */
15007c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = FSS_NOCHANGE;
15017c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = FSS_NOCHANGE;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	/*
15047c478bd9Sstevel@tonic-gate 	 * Get the varargs parameter and check validity of parameters.
15057c478bd9Sstevel@tonic-gate 	 */
15067c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
15077c478bd9Sstevel@tonic-gate 		return (EINVAL);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
15107c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
15117c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
15127c478bd9Sstevel@tonic-gate 			if (limflag++)
15137c478bd9Sstevel@tonic-gate 				return (EINVAL);
15147c478bd9Sstevel@tonic-gate 			fssparmsp->fss_uprilim = (pri_t)vpp->pc_parm;
15157c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > fss_maxupri ||
15167c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_uprilim < -fss_maxupri)
15177c478bd9Sstevel@tonic-gate 				return (EINVAL);
15187c478bd9Sstevel@tonic-gate 			break;
15197c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
15207c478bd9Sstevel@tonic-gate 			if (priflag++)
15217c478bd9Sstevel@tonic-gate 				return (EINVAL);
15227c478bd9Sstevel@tonic-gate 			fssparmsp->fss_upri = (pri_t)vpp->pc_parm;
15237c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > fss_maxupri ||
15247c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_upri < -fss_maxupri)
15257c478bd9Sstevel@tonic-gate 				return (EINVAL);
15267c478bd9Sstevel@tonic-gate 			break;
15277c478bd9Sstevel@tonic-gate 		default:
15287c478bd9Sstevel@tonic-gate 			return (EINVAL);
15297c478bd9Sstevel@tonic-gate 		}
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
15337c478bd9Sstevel@tonic-gate 		/*
15347c478bd9Sstevel@tonic-gate 		 * Use default parameters.
15357c478bd9Sstevel@tonic-gate 		 */
15367c478bd9Sstevel@tonic-gate 		fssparmsp->fss_upri = fssparmsp->fss_uprilim = 0;
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	return (0);
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate /*
15437c478bd9Sstevel@tonic-gate  * Copy all selected fair-sharing class parameters to the user.  The parameters
15447c478bd9Sstevel@tonic-gate  * are specified by a key.
15457c478bd9Sstevel@tonic-gate  */
15467c478bd9Sstevel@tonic-gate static int
15477c478bd9Sstevel@tonic-gate fss_vaparmsout(void *parmsp, pc_vaparms_t *vaparmsp)
15487c478bd9Sstevel@tonic-gate {
15497c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
15507c478bd9Sstevel@tonic-gate 	int priflag = 0;
15517c478bd9Sstevel@tonic-gate 	int limflag = 0;
15527c478bd9Sstevel@tonic-gate 	uint_t cnt;
15537c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
15587c478bd9Sstevel@tonic-gate 		return (EINVAL);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
15617c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
15627c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
15637c478bd9Sstevel@tonic-gate 			if (limflag++)
15647c478bd9Sstevel@tonic-gate 				return (EINVAL);
15657c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_uprilim,
15667c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
15677c478bd9Sstevel@tonic-gate 				return (EFAULT);
15687c478bd9Sstevel@tonic-gate 			break;
15697c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
15707c478bd9Sstevel@tonic-gate 			if (priflag++)
15717c478bd9Sstevel@tonic-gate 				return (EINVAL);
15727c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_upri,
15737c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
15747c478bd9Sstevel@tonic-gate 				return (EFAULT);
15757c478bd9Sstevel@tonic-gate 			break;
15767c478bd9Sstevel@tonic-gate 		default:
15777c478bd9Sstevel@tonic-gate 			return (EINVAL);
15787c478bd9Sstevel@tonic-gate 		}
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	return (0);
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
1584d4204c85Sraf /*
1585d4204c85Sraf  * Return the user mode scheduling priority range.
1586d4204c85Sraf  */
15877c478bd9Sstevel@tonic-gate static int
15887c478bd9Sstevel@tonic-gate fss_getclpri(pcpri_t *pcprip)
15897c478bd9Sstevel@tonic-gate {
1590d4204c85Sraf 	pcprip->pc_clpmax = fss_maxupri;
1591d4204c85Sraf 	pcprip->pc_clpmin = -fss_maxupri;
15927c478bd9Sstevel@tonic-gate 	return (0);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate static int
15967c478bd9Sstevel@tonic-gate fss_alloc(void **p, int flag)
15977c478bd9Sstevel@tonic-gate {
15987c478bd9Sstevel@tonic-gate 	void *bufp;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	if ((bufp = kmem_zalloc(sizeof (fssproc_t), flag)) == NULL) {
16017c478bd9Sstevel@tonic-gate 		return (ENOMEM);
16027c478bd9Sstevel@tonic-gate 	} else {
16037c478bd9Sstevel@tonic-gate 		*p = bufp;
16047c478bd9Sstevel@tonic-gate 		return (0);
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate }
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate static void
16097c478bd9Sstevel@tonic-gate fss_free(void *bufp)
16107c478bd9Sstevel@tonic-gate {
16117c478bd9Sstevel@tonic-gate 	if (bufp)
16127c478bd9Sstevel@tonic-gate 		kmem_free(bufp, sizeof (fssproc_t));
16137c478bd9Sstevel@tonic-gate }
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate /*
16167c478bd9Sstevel@tonic-gate  * Thread functions
16177c478bd9Sstevel@tonic-gate  */
16187c478bd9Sstevel@tonic-gate static int
16197c478bd9Sstevel@tonic-gate fss_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
16207c478bd9Sstevel@tonic-gate     void *bufp)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
16237c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc;
16247c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
16257c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
16267c478bd9Sstevel@tonic-gate 	static uint32_t fssexists = 0;
16277c478bd9Sstevel@tonic-gate 	fsspset_t	*fsspset;
16287c478bd9Sstevel@tonic-gate 	fssproj_t	*fssproj;
16297c478bd9Sstevel@tonic-gate 	fsszone_t	*fsszone;
16307c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
16317c478bd9Sstevel@tonic-gate 	zone_t		*zone;
16327c478bd9Sstevel@tonic-gate 	int		fsszone_allocated = 0;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	fssproc = (fssproc_t *)bufp;
16357c478bd9Sstevel@tonic-gate 	ASSERT(fssproc != NULL);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * Only root can move threads to FSS class.
16417c478bd9Sstevel@tonic-gate 	 */
16427c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
16437c478bd9Sstevel@tonic-gate 		return (EPERM);
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc structure.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	fssproc->fss_umdpri = fss_maxumdpri / 2;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (fssparmsp == NULL) {
16507c478bd9Sstevel@tonic-gate 		/*
16517c478bd9Sstevel@tonic-gate 		 * Use default values.
16527c478bd9Sstevel@tonic-gate 		 */
16537c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO;
16547c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = fssproc->fss_upri = 0;
16557c478bd9Sstevel@tonic-gate 	} else {
16567c478bd9Sstevel@tonic-gate 		/*
16577c478bd9Sstevel@tonic-gate 		 * Use supplied values.
16587c478bd9Sstevel@tonic-gate 		 */
16597c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_uprilim == FSS_NOCHANGE) {
16607c478bd9Sstevel@tonic-gate 			reqfssuprilim = 0;
16617c478bd9Sstevel@tonic-gate 		} else {
16627c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > 0 &&
16637c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
16647c478bd9Sstevel@tonic-gate 				return (EPERM);
16657c478bd9Sstevel@tonic-gate 			reqfssuprilim = fssparmsp->fss_uprilim;
16667c478bd9Sstevel@tonic-gate 		}
16677c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_upri == FSS_NOCHANGE) {
16687c478bd9Sstevel@tonic-gate 			reqfssupri = reqfssuprilim;
16697c478bd9Sstevel@tonic-gate 		} else {
16707c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > 0 &&
16717c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
16727c478bd9Sstevel@tonic-gate 				return (EPERM);
16737c478bd9Sstevel@tonic-gate 			/*
16747c478bd9Sstevel@tonic-gate 			 * Set the user priority to the requested value or
16757c478bd9Sstevel@tonic-gate 			 * the upri limit, whichever is lower.
16767c478bd9Sstevel@tonic-gate 			 */
16777c478bd9Sstevel@tonic-gate 			reqfssupri = fssparmsp->fss_upri;
16787c478bd9Sstevel@tonic-gate 			if (reqfssupri > reqfssuprilim)
16797c478bd9Sstevel@tonic-gate 				reqfssupri = reqfssuprilim;
16807c478bd9Sstevel@tonic-gate 		}
16817c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = reqfssuprilim;
16827c478bd9Sstevel@tonic-gate 		fssproc->fss_upri = reqfssupri;
16837c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO - (NZERO * reqfssupri) / fss_maxupri;
16847c478bd9Sstevel@tonic-gate 		if (fssproc->fss_nice > FSS_NICE_MAX)
16857c478bd9Sstevel@tonic-gate 			fssproc->fss_nice = FSS_NICE_MAX;
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
16897c478bd9Sstevel@tonic-gate 	fssproc->fss_tp = t;
1690c97ad5cdSakolb 	cpucaps_sc_init(&fssproc->fss_caps);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * Put a lock on our fsspset structure.
16947c478bd9Sstevel@tonic-gate 	 */
16957c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
16967c478bd9Sstevel@tonic-gate 	fsspset = fss_find_fsspset(t->t_cpupart);
16977c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
16987c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
17017c478bd9Sstevel@tonic-gate 	if ((fsszone = fss_find_fsszone(fsspset, zone)) == NULL) {
17027c478bd9Sstevel@tonic-gate 		if ((fsszone = kmem_zalloc(sizeof (fsszone_t), KM_NOSLEEP))
17037c478bd9Sstevel@tonic-gate 		    == NULL) {
17047c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
17057c478bd9Sstevel@tonic-gate 			return (ENOMEM);
17067c478bd9Sstevel@tonic-gate 		} else {
17077c478bd9Sstevel@tonic-gate 			fsszone_allocated = 1;
17087c478bd9Sstevel@tonic-gate 			fss_insert_fsszone(fsspset, zone, fsszone);
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 	kpj = ttoproj(t);
17127c478bd9Sstevel@tonic-gate 	if ((fssproj = fss_find_fssproj(fsspset, kpj)) == NULL) {
17137c478bd9Sstevel@tonic-gate 		if ((fssproj = kmem_zalloc(sizeof (fssproj_t), KM_NOSLEEP))
17147c478bd9Sstevel@tonic-gate 		    == NULL) {
17157c478bd9Sstevel@tonic-gate 			if (fsszone_allocated) {
17167c478bd9Sstevel@tonic-gate 				fss_remove_fsszone(fsspset, fsszone);
17177c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
17187c478bd9Sstevel@tonic-gate 			}
17197c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
17207c478bd9Sstevel@tonic-gate 			return (ENOMEM);
17217c478bd9Sstevel@tonic-gate 		} else {
17227c478bd9Sstevel@tonic-gate 			fss_insert_fssproj(fsspset, kpj, fsszone, fssproj);
17237c478bd9Sstevel@tonic-gate 		}
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
17267c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	/*
17297c478bd9Sstevel@tonic-gate 	 * Reset priority. Process goes to a "user mode" priority here
17307c478bd9Sstevel@tonic-gate 	 * regardless of whether or not it has slept since entering the kernel.
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 	thread_lock(t);
17337c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
17347c478bd9Sstevel@tonic-gate 	t->t_cid = cid;
17357c478bd9Sstevel@tonic-gate 	t->t_cldata = (void *)fssproc;
17367c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_RUNQMATCH;
17377c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
1738c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
1739c97ad5cdSakolb 	    t->t_state == TS_WAIT)
17407c478bd9Sstevel@tonic-gate 		fss_active(t);
17417c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	/*
17467c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc list.
17477c478bd9Sstevel@tonic-gate 	 */
17487c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(fssproc);
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	/*
17517c478bd9Sstevel@tonic-gate 	 * If this is the first fair-sharing thread to occur since boot,
17527c478bd9Sstevel@tonic-gate 	 * we set up the initial call to fss_update() here. Use an atomic
17537c478bd9Sstevel@tonic-gate 	 * compare-and-swap since that's easier and faster than a mutex
17547c478bd9Sstevel@tonic-gate 	 * (but check with an ordinary load first since most of the time
17557c478bd9Sstevel@tonic-gate 	 * this will already be done).
17567c478bd9Sstevel@tonic-gate 	 */
175775d94465SJosef 'Jeff' Sipek 	if (fssexists == 0 && atomic_cas_32(&fssexists, 0, 1) == 0)
17587c478bd9Sstevel@tonic-gate 		(void) timeout(fss_update, NULL, hz);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	return (0);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate  * Remove fssproc_t from the list.
17657c478bd9Sstevel@tonic-gate  */
17667c478bd9Sstevel@tonic-gate static void
17677c478bd9Sstevel@tonic-gate fss_exitclass(void *procp)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = (fssproc_t *)procp;
17707c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
17717c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
17727c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
17737c478bd9Sstevel@tonic-gate 	kthread_t *t = fssproc->fss_tp;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	/*
17767c478bd9Sstevel@tonic-gate 	 * We should be either getting this thread off the deathrow or
17777c478bd9Sstevel@tonic-gate 	 * this thread has already moved to another scheduling class and
17787c478bd9Sstevel@tonic-gate 	 * we're being called with its old cldata buffer pointer.  In both
17797c478bd9Sstevel@tonic-gate 	 * cases, the content of this buffer can not be changed while we're
17807c478bd9Sstevel@tonic-gate 	 * here.
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
17837c478bd9Sstevel@tonic-gate 	thread_lock(t);
17847c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid) {
17857c478bd9Sstevel@tonic-gate 		/*
17867c478bd9Sstevel@tonic-gate 		 * We're being called as a result of the priocntl() system
17877c478bd9Sstevel@tonic-gate 		 * call -- someone is trying to move our thread to another
17887c478bd9Sstevel@tonic-gate 		 * scheduling class. We can't call fss_inactive() here
17897c478bd9Sstevel@tonic-gate 		 * because our thread's t_cldata pointer already points
17907c478bd9Sstevel@tonic-gate 		 * to another scheduling class specific data.
17917c478bd9Sstevel@tonic-gate 		 */
17927c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
17957c478bd9Sstevel@tonic-gate 		fsspset = FSSPROJ2FSSPSET(fssproj);
17967c478bd9Sstevel@tonic-gate 		fsszone = fssproj->fssp_fsszone;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 		if (fssproc->fss_runnable) {
17997c478bd9Sstevel@tonic-gate 			disp_lock_enter_high(&fsspset->fssps_displock);
18007c478bd9Sstevel@tonic-gate 			if (--fssproj->fssp_runnable == 0) {
18017c478bd9Sstevel@tonic-gate 				fsszone->fssz_shares -= fssproj->fssp_shares;
18027c478bd9Sstevel@tonic-gate 				if (--fsszone->fssz_runnable == 0)
18037c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
18047c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
18057c478bd9Sstevel@tonic-gate 			}
18067c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(&fsspset->fssps_displock);
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 		thread_unlock(t);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
18117c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_threads == 0) {
18127c478bd9Sstevel@tonic-gate 			fss_remove_fssproj(fsspset, fssproj);
18137c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_nproj == 0)
18147c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
18157c478bd9Sstevel@tonic-gate 			kmem_free(fssproj, sizeof (fssproj_t));
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	} else {
18207c478bd9Sstevel@tonic-gate 		ASSERT(t->t_state == TS_FREE);
18217c478bd9Sstevel@tonic-gate 		/*
18227c478bd9Sstevel@tonic-gate 		 * We're being called from thread_free() when our thread
18237c478bd9Sstevel@tonic-gate 		 * is removed from the deathrow. There is nothing we need
18247c478bd9Sstevel@tonic-gate 		 * do here since everything should've been done earlier
18257c478bd9Sstevel@tonic-gate 		 * in fss_exit().
18267c478bd9Sstevel@tonic-gate 		 */
18277c478bd9Sstevel@tonic-gate 		thread_unlock(t);
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	FSS_LIST_DELETE(fssproc);
18327c478bd9Sstevel@tonic-gate 	fss_free(fssproc);
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18367c478bd9Sstevel@tonic-gate static int
18377c478bd9Sstevel@tonic-gate fss_canexit(kthread_t *t, cred_t *credp)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate 	/*
18407c478bd9Sstevel@tonic-gate 	 * A thread is allowed to exit FSS only if we have sufficient
18417c478bd9Sstevel@tonic-gate 	 * privileges.
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	if (credp != NULL && secpolicy_setpriority(credp) != 0)
18447c478bd9Sstevel@tonic-gate 		return (EPERM);
18457c478bd9Sstevel@tonic-gate 	else
18467c478bd9Sstevel@tonic-gate 		return (0);
18477c478bd9Sstevel@tonic-gate }
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate /*
18507c478bd9Sstevel@tonic-gate  * Initialize fair-share class specific proc structure for a child.
18517c478bd9Sstevel@tonic-gate  */
18527c478bd9Sstevel@tonic-gate static int
18537c478bd9Sstevel@tonic-gate fss_fork(kthread_t *pt, kthread_t *ct, void *bufp)
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate 	fssproc_t *pfssproc;	/* ptr to parent's fssproc structure	*/
18567c478bd9Sstevel@tonic-gate 	fssproc_t *cfssproc;	/* ptr to child's fssproc structure	*/
18577c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
18587c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(pt)->p_lock));
18617c478bd9Sstevel@tonic-gate 	ASSERT(ct->t_state == TS_STOPPED);
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	cfssproc = (fssproc_t *)bufp;
18647c478bd9Sstevel@tonic-gate 	ASSERT(cfssproc != NULL);
18657c478bd9Sstevel@tonic-gate 	bzero(cfssproc, sizeof (fssproc_t));
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	thread_lock(pt);
18687c478bd9Sstevel@tonic-gate 	pfssproc = FSSPROC(pt);
18697c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(pfssproc);
18707c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
18717c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	 * Initialize child's fssproc structure.
18767c478bd9Sstevel@tonic-gate 	 */
18777c478bd9Sstevel@tonic-gate 	thread_lock(pt);
18787c478bd9Sstevel@tonic-gate 	ASSERT(FSSPROJ(pt) == fssproj);
18797c478bd9Sstevel@tonic-gate 	cfssproc->fss_proj = fssproj;
18807c478bd9Sstevel@tonic-gate 	cfssproc->fss_timeleft = fss_quantum;
18817c478bd9Sstevel@tonic-gate 	cfssproc->fss_umdpri = pfssproc->fss_umdpri;
18827c478bd9Sstevel@tonic-gate 	cfssproc->fss_fsspri = 0;
18837c478bd9Sstevel@tonic-gate 	cfssproc->fss_uprilim = pfssproc->fss_uprilim;
18847c478bd9Sstevel@tonic-gate 	cfssproc->fss_upri = pfssproc->fss_upri;
18857c478bd9Sstevel@tonic-gate 	cfssproc->fss_tp = ct;
18867c478bd9Sstevel@tonic-gate 	cfssproc->fss_nice = pfssproc->fss_nice;
1887c97ad5cdSakolb 	cpucaps_sc_init(&cfssproc->fss_caps);
1888c97ad5cdSakolb 
18899ac8606fSraf 	cfssproc->fss_flags =
1890*2c164fafSPatrick Mooney 	    pfssproc->fss_flags & ~(FSSBACKQ | FSSRESTORE);
18917c478bd9Sstevel@tonic-gate 	ct->t_cldata = (void *)cfssproc;
18927c478bd9Sstevel@tonic-gate 	ct->t_schedflag |= TS_RUNQMATCH;
18937c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
18967c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	/*
18997c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc hash table.
19007c478bd9Sstevel@tonic-gate 	 */
19017c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(cfssproc);
19027c478bd9Sstevel@tonic-gate 	return (0);
19037c478bd9Sstevel@tonic-gate }
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate /*
19067c478bd9Sstevel@tonic-gate  * Child is placed at back of dispatcher queue and parent gives up processor
19077c478bd9Sstevel@tonic-gate  * so that the child runs first after the fork. This allows the child
19087c478bd9Sstevel@tonic-gate  * immediately execing to break the multiple use of copy on write pages with no
19097c478bd9Sstevel@tonic-gate  * disk home. The parent will get to steal them back rather than uselessly
19107c478bd9Sstevel@tonic-gate  * copying them.
19117c478bd9Sstevel@tonic-gate  */
19127c478bd9Sstevel@tonic-gate static void
19137c478bd9Sstevel@tonic-gate fss_forkret(kthread_t *t, kthread_t *ct)
19147c478bd9Sstevel@tonic-gate {
19157c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
19167c478bd9Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
19177c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
19207c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	/*
19237c478bd9Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure the
19247c478bd9Sstevel@tonic-gate 	 * process does not disappear before we set it running.
19257c478bd9Sstevel@tonic-gate 	 */
19267c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
19277c478bd9Sstevel@tonic-gate 	continuelwps(cp);
19287c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1931d5493db7SPramod Batni 	mutex_exit(&pidlock);
19327c478bd9Sstevel@tonic-gate 	continuelwps(pp);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	thread_lock(t);
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
1937482a7749SJerry Jelinek 	fss_newpri(fssproc, B_FALSE);
19387c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
19397c478bd9Sstevel@tonic-gate 	t->t_pri = fssproc->fss_umdpri;
19407c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
19417c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	/*
19447c478bd9Sstevel@tonic-gate 	 * We don't want to call fss_setrun(t) here because it may call
19457c478bd9Sstevel@tonic-gate 	 * fss_active, which we don't need.
19467c478bd9Sstevel@tonic-gate 	 */
19477c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
19487c478bd9Sstevel@tonic-gate 
1949d3d50737SRafael Vanoni 	if (t->t_disp_time != ddi_get_lbolt())
19507c478bd9Sstevel@tonic-gate 		setbackdq(t);
19517c478bd9Sstevel@tonic-gate 	else
19527c478bd9Sstevel@tonic-gate 		setfrontdq(t);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1955d5493db7SPramod Batni 	/*
1956d5493db7SPramod Batni 	 * Safe to drop p_lock now since it is safe to change
1957d5493db7SPramod Batni 	 * the scheduling class after this point.
1958d5493db7SPramod Batni 	 */
1959d5493db7SPramod Batni 	mutex_exit(&pp->p_lock);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	swtch();
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate /*
19657c478bd9Sstevel@tonic-gate  * Get the fair-sharing parameters of the thread pointed to by fssprocp into
19667c478bd9Sstevel@tonic-gate  * the buffer pointed by fssparmsp.
19677c478bd9Sstevel@tonic-gate  */
19687c478bd9Sstevel@tonic-gate static void
19697c478bd9Sstevel@tonic-gate fss_parmsget(kthread_t *t, void *parmsp)
19707c478bd9Sstevel@tonic-gate {
19717c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
19727c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = fssproc->fss_uprilim;
19757c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = fssproc->fss_upri;
19767c478bd9Sstevel@tonic-gate }
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19797c478bd9Sstevel@tonic-gate static int
19807c478bd9Sstevel@tonic-gate fss_parmsset(kthread_t *t, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
19817c478bd9Sstevel@tonic-gate {
19827c478bd9Sstevel@tonic-gate 	char		nice;
19837c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
19847c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
19857c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc = FSSPROC(t);
19867c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_uprilim == FSS_NOCHANGE)
19917c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssproc->fss_uprilim;
19927c478bd9Sstevel@tonic-gate 	else
19937c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssparmsp->fss_uprilim;
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_upri == FSS_NOCHANGE)
19967c478bd9Sstevel@tonic-gate 		reqfssupri = fssproc->fss_upri;
19977c478bd9Sstevel@tonic-gate 	else
19987c478bd9Sstevel@tonic-gate 		reqfssupri = fssparmsp->fss_upri;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	/*
20017c478bd9Sstevel@tonic-gate 	 * Make sure the user priority doesn't exceed the upri limit.
20027c478bd9Sstevel@tonic-gate 	 */
20037c478bd9Sstevel@tonic-gate 	if (reqfssupri > reqfssuprilim)
20047c478bd9Sstevel@tonic-gate 		reqfssupri = reqfssuprilim;
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	/*
20077c478bd9Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code for all classes
20087c478bd9Sstevel@tonic-gate 	 * require that a thread attempting to change the scheduling parameters
20097c478bd9Sstevel@tonic-gate 	 * of a target thread be privileged or have a real or effective UID
20107c478bd9Sstevel@tonic-gate 	 * matching that of the target thread. We are not called unless these
20117c478bd9Sstevel@tonic-gate 	 * basic permission checks have already passed. The fair-sharing class
20127c478bd9Sstevel@tonic-gate 	 * requires in addition that the calling thread be privileged if it
20137c478bd9Sstevel@tonic-gate 	 * is attempting to raise the upri limit above its current value.
20147c478bd9Sstevel@tonic-gate 	 * This may have been checked previously but if our caller passed us
20157c478bd9Sstevel@tonic-gate 	 * a non-NULL credential pointer we assume it hasn't and we check it
20167c478bd9Sstevel@tonic-gate 	 * here.
20177c478bd9Sstevel@tonic-gate 	 */
20187c478bd9Sstevel@tonic-gate 	if ((reqpcredp != NULL) &&
20197c478bd9Sstevel@tonic-gate 	    (reqfssuprilim > fssproc->fss_uprilim) &&
202024d819e6SJerry Jelinek 	    secpolicy_raisepriority(reqpcredp) != 0)
20217c478bd9Sstevel@tonic-gate 		return (EPERM);
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	/*
20247c478bd9Sstevel@tonic-gate 	 * Set fss_nice to the nice value corresponding to the user priority we
20257c478bd9Sstevel@tonic-gate 	 * are setting.  Note that setting the nice field of the parameter
20267c478bd9Sstevel@tonic-gate 	 * struct won't affect upri or nice.
20277c478bd9Sstevel@tonic-gate 	 */
20287c478bd9Sstevel@tonic-gate 	nice = NZERO - (reqfssupri * NZERO) / fss_maxupri;
20297c478bd9Sstevel@tonic-gate 	if (nice > FSS_NICE_MAX)
20307c478bd9Sstevel@tonic-gate 		nice = FSS_NICE_MAX;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	thread_lock(t);
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	fssproc->fss_uprilim = reqfssuprilim;
20357c478bd9Sstevel@tonic-gate 	fssproc->fss_upri = reqfssupri;
20367c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = nice;
2037482a7749SJerry Jelinek 	fss_newpri(fssproc, B_FALSE);
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
20407c478bd9Sstevel@tonic-gate 	thread_unlock(t);
20417c478bd9Sstevel@tonic-gate 	return (0);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * The thread is being stopped.
20477c478bd9Sstevel@tonic-gate  */
20487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20497c478bd9Sstevel@tonic-gate static void
20507c478bd9Sstevel@tonic-gate fss_stop(kthread_t *t, int why, int what)
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
20537c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	fss_inactive(t);
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate  * The current thread is exiting, do necessary adjustments to its project
20607c478bd9Sstevel@tonic-gate  */
20617c478bd9Sstevel@tonic-gate static void
20627c478bd9Sstevel@tonic-gate fss_exit(kthread_t *t)
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
20657c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
20667c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
20677c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
20687c478bd9Sstevel@tonic-gate 	int free = 0;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	/*
20717c478bd9Sstevel@tonic-gate 	 * Thread t here is either a current thread (in which case we hold
20727c478bd9Sstevel@tonic-gate 	 * its process' p_lock), or a thread being destroyed by forklwp_fail(),
20737c478bd9Sstevel@tonic-gate 	 * in which case we hold pidlock and thread is no longer on the
20747c478bd9Sstevel@tonic-gate 	 * thread list.
20757c478bd9Sstevel@tonic-gate 	 */
20767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock) || MUTEX_HELD(&pidlock));
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
20797c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
20807c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
20817c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
20847c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	thread_lock(t);
20877c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
20887c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
20897c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_runnable == 0) {
20907c478bd9Sstevel@tonic-gate 			fsszone->fssz_shares -= fssproj->fssp_shares;
20917c478bd9Sstevel@tonic-gate 			if (--fsszone->fssz_runnable == 0)
20927c478bd9Sstevel@tonic-gate 				fsspset->fssps_shares -= fsszone->fssz_rshares;
20937c478bd9Sstevel@tonic-gate 		}
20947c478bd9Sstevel@tonic-gate 		ASSERT(fssproc->fss_runnable == 1);
20957c478bd9Sstevel@tonic-gate 		fssproc->fss_runnable = 0;
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_threads == 0) {
20987c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj);
20997c478bd9Sstevel@tonic-gate 		free = 1;
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
21027c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = NULL;	/* mark this thread as already exited */
21037c478bd9Sstevel@tonic-gate 	thread_unlock(t);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	if (free) {
21067c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
21077c478bd9Sstevel@tonic-gate 			kmem_free(fsszone, sizeof (fsszone_t));
21087c478bd9Sstevel@tonic-gate 		kmem_free(fssproj, sizeof (fssproj_t));
21097c478bd9Sstevel@tonic-gate 	}
21107c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
21117c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
2112c97ad5cdSakolb 
21138c34bbb7Sakolb 	/*
21148c34bbb7Sakolb 	 * A thread could be exiting in between clock ticks, so we need to
21158c34bbb7Sakolb 	 * calculate how much CPU time it used since it was charged last time.
21168c34bbb7Sakolb 	 *
21178c34bbb7Sakolb 	 * CPU caps are not enforced on exiting processes - it is usually
21188c34bbb7Sakolb 	 * desirable to exit as soon as possible to free resources.
21198c34bbb7Sakolb 	 */
2120c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2121c97ad5cdSakolb 		thread_lock(t);
2122c97ad5cdSakolb 		fssproc = FSSPROC(t);
2123c97ad5cdSakolb 		(void) cpucaps_charge(t, &fssproc->fss_caps,
2124c97ad5cdSakolb 		    CPUCAPS_CHARGE_ONLY);
2125c97ad5cdSakolb 		thread_unlock(t);
2126c97ad5cdSakolb 	}
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate static void
21307c478bd9Sstevel@tonic-gate fss_nullsys()
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate }
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate /*
21357c478bd9Sstevel@tonic-gate  * fss_swapin() returns -1 if the thread is loaded or is not eligible to be
21367c478bd9Sstevel@tonic-gate  * swapped in. Otherwise, it returns the thread's effective priority based
21377c478bd9Sstevel@tonic-gate  * on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
21387c478bd9Sstevel@tonic-gate  */
21397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21407c478bd9Sstevel@tonic-gate static pri_t
21417c478bd9Sstevel@tonic-gate fss_swapin(kthread_t *t, int flags)
21427c478bd9Sstevel@tonic-gate {
21437c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
21447c478bd9Sstevel@tonic-gate 	long epri = -1;
21457c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
21507c478bd9Sstevel@tonic-gate 		time_t swapout_time;
21517c478bd9Sstevel@tonic-gate 
2152d3d50737SRafael Vanoni 		swapout_time = (ddi_get_lbolt() - t->t_stime) / hz;
2153*2c164fafSPatrick Mooney 		if (INHERITED(t)) {
21547c478bd9Sstevel@tonic-gate 			epri = (long)DISP_PRIO(t) + swapout_time;
21557c478bd9Sstevel@tonic-gate 		} else {
21567c478bd9Sstevel@tonic-gate 			/*
21577c478bd9Sstevel@tonic-gate 			 * Threads which have been out for a long time,
21587c478bd9Sstevel@tonic-gate 			 * have high user mode priority and are associated
21597c478bd9Sstevel@tonic-gate 			 * with a small address space are more deserving.
21607c478bd9Sstevel@tonic-gate 			 */
21617c478bd9Sstevel@tonic-gate 			epri = fssproc->fss_umdpri;
21627c478bd9Sstevel@tonic-gate 			ASSERT(epri >= 0 && epri <= fss_maxumdpri);
21637c478bd9Sstevel@tonic-gate 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
21647c478bd9Sstevel@tonic-gate 		}
21657c478bd9Sstevel@tonic-gate 		/*
21667c478bd9Sstevel@tonic-gate 		 * Scale epri so that SHRT_MAX / 2 represents zero priority.
21677c478bd9Sstevel@tonic-gate 		 */
21687c478bd9Sstevel@tonic-gate 		epri += SHRT_MAX / 2;
21697c478bd9Sstevel@tonic-gate 		if (epri < 0)
21707c478bd9Sstevel@tonic-gate 			epri = 0;
21717c478bd9Sstevel@tonic-gate 		else if (epri > SHRT_MAX)
21727c478bd9Sstevel@tonic-gate 			epri = SHRT_MAX;
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate /*
21787c478bd9Sstevel@tonic-gate  * fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
21797c478bd9Sstevel@tonic-gate  * be swapped out. Otherwise, it returns the thread's effective priority
21807c478bd9Sstevel@tonic-gate  * based on if the swapper is in softswap or hardswap mode.
21817c478bd9Sstevel@tonic-gate  */
21827c478bd9Sstevel@tonic-gate static pri_t
21837c478bd9Sstevel@tonic-gate fss_swapout(kthread_t *t, int flags)
21847c478bd9Sstevel@tonic-gate {
21857c478bd9Sstevel@tonic-gate 	long epri = -1;
21867c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
21877c478bd9Sstevel@tonic-gate 	time_t swapin_time;
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	if (INHERITED(t) ||
21927c478bd9Sstevel@tonic-gate 	    (t->t_proc_flag & TP_LWPEXIT) ||
2193d4204c85Sraf 	    (t->t_state & (TS_ZOMB|TS_FREE|TS_STOPPED|TS_ONPROC|TS_WAIT)) ||
21947c478bd9Sstevel@tonic-gate 	    !(t->t_schedflag & TS_LOAD) ||
21957c478bd9Sstevel@tonic-gate 	    !(SWAP_OK(t)))
21967c478bd9Sstevel@tonic-gate 		return (-1);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
21997c478bd9Sstevel@tonic-gate 
2200d3d50737SRafael Vanoni 	swapin_time = (ddi_get_lbolt() - t->t_stime) / hz;
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	if (flags == SOFTSWAP) {
22037c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
22047c478bd9Sstevel@tonic-gate 			epri = 0;
22057c478bd9Sstevel@tonic-gate 		} else {
22067c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
22077c478bd9Sstevel@tonic-gate 		}
22087c478bd9Sstevel@tonic-gate 	} else {
22097c478bd9Sstevel@tonic-gate 		pri_t pri;
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 		if ((t->t_state == TS_SLEEP && swapin_time > fss_minslp) ||
22127c478bd9Sstevel@tonic-gate 		    (t->t_state == TS_RUN && swapin_time > fss_minrun)) {
22137c478bd9Sstevel@tonic-gate 			pri = fss_maxumdpri;
22147c478bd9Sstevel@tonic-gate 			epri = swapin_time -
22157c478bd9Sstevel@tonic-gate 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
22167c478bd9Sstevel@tonic-gate 		} else {
22177c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
22187c478bd9Sstevel@tonic-gate 		}
22197c478bd9Sstevel@tonic-gate 	}
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	/*
22227c478bd9Sstevel@tonic-gate 	 * Scale epri so that SHRT_MAX / 2 represents zero priority.
22237c478bd9Sstevel@tonic-gate 	 */
22247c478bd9Sstevel@tonic-gate 	epri += SHRT_MAX / 2;
22257c478bd9Sstevel@tonic-gate 	if (epri < 0)
22267c478bd9Sstevel@tonic-gate 		epri = 0;
22277c478bd9Sstevel@tonic-gate 	else if (epri > SHRT_MAX)
22287c478bd9Sstevel@tonic-gate 		epri = SHRT_MAX;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
22317c478bd9Sstevel@tonic-gate }
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate /*
2234*2c164fafSPatrick Mooney  * Run swap-out checks when returning to userspace.
22357c478bd9Sstevel@tonic-gate  */
22367c478bd9Sstevel@tonic-gate static void
22377c478bd9Sstevel@tonic-gate fss_trapret(kthread_t *t)
22387c478bd9Sstevel@tonic-gate {
22397c478bd9Sstevel@tonic-gate 	cpu_t *cp = CPU;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
22427c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
22437c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_dispthread == t);
22447c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 	/*
22477c478bd9Sstevel@tonic-gate 	 * Swapout lwp if the swapper is waiting for this thread to reach
22487c478bd9Sstevel@tonic-gate 	 * a safe point.
22497c478bd9Sstevel@tonic-gate 	 */
22507c478bd9Sstevel@tonic-gate 	if (t->t_schedflag & TS_SWAPENQ) {
22517c478bd9Sstevel@tonic-gate 		thread_unlock(t);
22527c478bd9Sstevel@tonic-gate 		swapout_lwp(ttolwp(t));
22537c478bd9Sstevel@tonic-gate 		thread_lock(t);
22547c478bd9Sstevel@tonic-gate 	}
22557c478bd9Sstevel@tonic-gate }
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate /*
22587c478bd9Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location on dispatcher queue.
22597c478bd9Sstevel@tonic-gate  * This is called with the current thread in TS_ONPROC and locked.
22607c478bd9Sstevel@tonic-gate  */
22617c478bd9Sstevel@tonic-gate static void
22627c478bd9Sstevel@tonic-gate fss_preempt(kthread_t *t)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
22657c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
22667c478bd9Sstevel@tonic-gate 	uint_t flags;
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
22697c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(curthread));
22707c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	/*
2273c97ad5cdSakolb 	 * This thread may be placed on wait queue by CPU Caps. In this case we
2274c97ad5cdSakolb 	 * do not need to do anything until it is removed from the wait queue.
2275c97ad5cdSakolb 	 * Do not enforce CPU caps on threads running at a kernel priority
2276c97ad5cdSakolb 	 */
2277c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2278c97ad5cdSakolb 		(void) cpucaps_charge(t, &fssproc->fss_caps,
22798c34bbb7Sakolb 		    CPUCAPS_CHARGE_ENFORCE);
2280c97ad5cdSakolb 
2281*2c164fafSPatrick Mooney 		if (CPUCAPS_ENFORCE(t))
2282c97ad5cdSakolb 			return;
2283c97ad5cdSakolb 	}
2284c97ad5cdSakolb 
22857c478bd9Sstevel@tonic-gate 	/*
22867c478bd9Sstevel@tonic-gate 	 * If preempted in user-land mark the thread as swappable because it
22877c478bd9Sstevel@tonic-gate 	 * cannot be holding any kernel locks.
22887c478bd9Sstevel@tonic-gate 	 */
22897c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
2290*2c164fafSPatrick Mooney 	lwp = ttolwp(t);
22917c478bd9Sstevel@tonic-gate 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
22927c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	/*
22957c478bd9Sstevel@tonic-gate 	 * Check to see if we're doing "preemption control" here.  If
22967c478bd9Sstevel@tonic-gate 	 * we are, and if the user has requested that this thread not
22977c478bd9Sstevel@tonic-gate 	 * be preempted, and if preemptions haven't been put off for
22987c478bd9Sstevel@tonic-gate 	 * too long, let the preemption happen here but try to make
22999ac8606fSraf 	 * sure the thread is rescheduled as soon as possible.  We do
23007c478bd9Sstevel@tonic-gate 	 * this by putting it on the front of the highest priority run
23017c478bd9Sstevel@tonic-gate 	 * queue in the FSS class.  If the preemption has been put off
23027c478bd9Sstevel@tonic-gate 	 * for too long, clear the "nopreempt" bit and let the thread
23037c478bd9Sstevel@tonic-gate 	 * be preempted.
23047c478bd9Sstevel@tonic-gate 	 */
23057c478bd9Sstevel@tonic-gate 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
23067c478bd9Sstevel@tonic-gate 		if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
23077c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
23089ac8606fSraf 			/*
23099ac8606fSraf 			 * If not already remembered, remember current
23109ac8606fSraf 			 * priority for restoration in fss_yield().
23119ac8606fSraf 			 */
23129ac8606fSraf 			if (!(fssproc->fss_flags & FSSRESTORE)) {
23139ac8606fSraf 				fssproc->fss_scpri = t->t_pri;
23149ac8606fSraf 				fssproc->fss_flags |= FSSRESTORE;
23159ac8606fSraf 			}
23167c478bd9Sstevel@tonic-gate 			THREAD_CHANGE_PRI(t, fss_maxumdpri);
23177c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_DONT_SWAP;
23189ac8606fSraf 			schedctl_set_yield(t, 1);
23197c478bd9Sstevel@tonic-gate 			setfrontdq(t);
23207c478bd9Sstevel@tonic-gate 			return;
23217c478bd9Sstevel@tonic-gate 		} else {
23229ac8606fSraf 			if (fssproc->fss_flags & FSSRESTORE) {
23239ac8606fSraf 				THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
23249ac8606fSraf 				fssproc->fss_flags &= ~FSSRESTORE;
23259ac8606fSraf 			}
23267c478bd9Sstevel@tonic-gate 			schedctl_set_nopreempt(t, 0);
23277c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
23287c478bd9Sstevel@tonic-gate 			/*
23297c478bd9Sstevel@tonic-gate 			 * Fall through and be preempted below.
23307c478bd9Sstevel@tonic-gate 			 */
23317c478bd9Sstevel@tonic-gate 		}
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 
2334*2c164fafSPatrick Mooney 	flags = fssproc->fss_flags & FSSBACKQ;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	if (flags == FSSBACKQ) {
23377c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
23387c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
23397c478bd9Sstevel@tonic-gate 		setbackdq(t);
23407c478bd9Sstevel@tonic-gate 	} else {
23417c478bd9Sstevel@tonic-gate 		setfrontdq(t);
23427c478bd9Sstevel@tonic-gate 	}
23437c478bd9Sstevel@tonic-gate }
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate /*
23467c478bd9Sstevel@tonic-gate  * Called when a thread is waking up and is to be placed on the run queue.
23477c478bd9Sstevel@tonic-gate  */
23487c478bd9Sstevel@tonic-gate static void
23497c478bd9Sstevel@tonic-gate fss_setrun(kthread_t *t)
23507c478bd9Sstevel@tonic-gate {
23517c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP || t->t_state == TS_STOPPED)
23567c478bd9Sstevel@tonic-gate 		fss_active(t);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
23617c478bd9Sstevel@tonic-gate 	THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
23627c478bd9Sstevel@tonic-gate 
2363d3d50737SRafael Vanoni 	if (t->t_disp_time != ddi_get_lbolt())
23647c478bd9Sstevel@tonic-gate 		setbackdq(t);
23657c478bd9Sstevel@tonic-gate 	else
23667c478bd9Sstevel@tonic-gate 		setfrontdq(t);
23677c478bd9Sstevel@tonic-gate }
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate /*
2370*2c164fafSPatrick Mooney  * Prepare thread for sleep.
23717c478bd9Sstevel@tonic-gate  */
23727c478bd9Sstevel@tonic-gate static void
23737c478bd9Sstevel@tonic-gate fss_sleep(kthread_t *t)
23747c478bd9Sstevel@tonic-gate {
23757c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
23787c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
2381c97ad5cdSakolb 
2382c97ad5cdSakolb 	/*
2383c97ad5cdSakolb 	 * Account for time spent on CPU before going to sleep.
2384c97ad5cdSakolb 	 */
23858c34bbb7Sakolb 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2386c97ad5cdSakolb 
23877c478bd9Sstevel@tonic-gate 	fss_inactive(t);
2388d3d50737SRafael Vanoni 	t->t_stime = ddi_get_lbolt();	/* time stamp for the swapper */
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate /*
23927c478bd9Sstevel@tonic-gate  * A tick interrupt has ocurrend on a running thread. Check to see if our
23937c478bd9Sstevel@tonic-gate  * time slice has expired.  We must also clear the TS_DONT_SWAP flag in
23947c478bd9Sstevel@tonic-gate  * t_schedflag if the thread is eligible to be swapped out.
23957c478bd9Sstevel@tonic-gate  */
23967c478bd9Sstevel@tonic-gate static void
23977c478bd9Sstevel@tonic-gate fss_tick(kthread_t *t)
23987c478bd9Sstevel@tonic-gate {
23997c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
24007c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
24017c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
2402c97ad5cdSakolb 	boolean_t call_cpu_surrender = B_FALSE;
2403c97ad5cdSakolb 	boolean_t cpucaps_enforce = B_FALSE;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * It's safe to access fsspset and fssproj structures because we're
24097c478bd9Sstevel@tonic-gate 	 * holding our p_lock here.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	thread_lock(t);
24127c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
24137c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
24147c478bd9Sstevel@tonic-gate 	if (fssproj != NULL) {
24157c478bd9Sstevel@tonic-gate 		fsspset_t *fsspset = FSSPROJ2FSSPSET(fssproj);
24167c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&fsspset->fssps_displock);
24177c478bd9Sstevel@tonic-gate 		fssproj->fssp_ticks += fss_nice_tick[fssproc->fss_nice];
2418482a7749SJerry Jelinek 		fssproj->fssp_tick_cnt++;
24197c478bd9Sstevel@tonic-gate 		fssproc->fss_ticks++;
24207c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	/*
2424c97ad5cdSakolb 	 * Keep track of thread's project CPU usage.  Note that projects
2425c97ad5cdSakolb 	 * get charged even when threads are running in the kernel.
2426c97ad5cdSakolb 	 * Do not surrender CPU if running in the SYS class.
2427c97ad5cdSakolb 	 */
2428c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2429*2c164fafSPatrick Mooney 		cpucaps_enforce = cpucaps_charge(t, &fssproc->fss_caps,
2430*2c164fafSPatrick Mooney 		    CPUCAPS_CHARGE_ENFORCE);
2431c97ad5cdSakolb 	}
2432c97ad5cdSakolb 
24337c478bd9Sstevel@tonic-gate 	if (--fssproc->fss_timeleft <= 0) {
24347c478bd9Sstevel@tonic-gate 		pri_t new_pri;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		/*
2437*2c164fafSPatrick Mooney 		 * If we're doing preemption control and trying to avoid
2438*2c164fafSPatrick Mooney 		 * preempting this thread, just note that the thread should
2439*2c164fafSPatrick Mooney 		 * yield soon and let it keep running (unless it's been a
2440*2c164fafSPatrick Mooney 		 * while).
24417c478bd9Sstevel@tonic-gate 		 */
24427c478bd9Sstevel@tonic-gate 		if (t->t_schedctl && schedctl_get_nopreempt(t)) {
24437c478bd9Sstevel@tonic-gate 			if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
24447c478bd9Sstevel@tonic-gate 				DTRACE_SCHED1(schedctl__nopreempt,
24457c478bd9Sstevel@tonic-gate 				    kthread_t *, t);
24467c478bd9Sstevel@tonic-gate 				schedctl_set_yield(t, 1);
24477c478bd9Sstevel@tonic-gate 				thread_unlock_nopreempt(t);
24487c478bd9Sstevel@tonic-gate 				return;
24497c478bd9Sstevel@tonic-gate 			}
24507c478bd9Sstevel@tonic-gate 		}
24519ac8606fSraf 		fssproc->fss_flags &= ~FSSRESTORE;
24527c478bd9Sstevel@tonic-gate 
2453482a7749SJerry Jelinek 		fss_newpri(fssproc, B_TRUE);
24547c478bd9Sstevel@tonic-gate 		new_pri = fssproc->fss_umdpri;
24557c478bd9Sstevel@tonic-gate 		ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 		/*
2458*2c164fafSPatrick Mooney 		 * When the priority of a thread is changed, it may be
2459*2c164fafSPatrick Mooney 		 * necessary to adjust its position on a sleep queue or
2460*2c164fafSPatrick Mooney 		 * dispatch queue. The function thread_change_pri accomplishes
2461*2c164fafSPatrick Mooney 		 * this.
24627c478bd9Sstevel@tonic-gate 		 */
24637c478bd9Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, 0)) {
24647c478bd9Sstevel@tonic-gate 			if ((t->t_schedflag & TS_LOAD) &&
24657c478bd9Sstevel@tonic-gate 			    (lwp = t->t_lwp) &&
24667c478bd9Sstevel@tonic-gate 			    lwp->lwp_state == LWP_USER)
24677c478bd9Sstevel@tonic-gate 				t->t_schedflag &= ~TS_DONT_SWAP;
24687c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
24697c478bd9Sstevel@tonic-gate 		} else {
2470c97ad5cdSakolb 			call_cpu_surrender = B_TRUE;
24717c478bd9Sstevel@tonic-gate 		}
2472b3383343Smishra 	} else if (t->t_state == TS_ONPROC &&
2473b3383343Smishra 	    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
24747c478bd9Sstevel@tonic-gate 		/*
2475*2c164fafSPatrick Mooney 		 * If there is a higher-priority thread which is waiting for a
2476*2c164fafSPatrick Mooney 		 * processor, then thread surrenders the processor.
24777c478bd9Sstevel@tonic-gate 		 */
2478c97ad5cdSakolb 		call_cpu_surrender = B_TRUE;
2479c97ad5cdSakolb 	}
2480c97ad5cdSakolb 
2481c97ad5cdSakolb 	if (cpucaps_enforce && 2 * fssproc->fss_timeleft > fss_quantum) {
2482c97ad5cdSakolb 		/*
2483c97ad5cdSakolb 		 * The thread used more than half of its quantum, so assume that
2484c97ad5cdSakolb 		 * it used the whole quantum.
2485c97ad5cdSakolb 		 *
2486c97ad5cdSakolb 		 * Update thread's priority just before putting it on the wait
2487c97ad5cdSakolb 		 * queue so that it gets charged for the CPU time from its
2488c97ad5cdSakolb 		 * quantum even before that quantum expires.
2489c97ad5cdSakolb 		 */
2490482a7749SJerry Jelinek 		fss_newpri(fssproc, B_FALSE);
2491c97ad5cdSakolb 		if (t->t_pri != fssproc->fss_umdpri)
2492c97ad5cdSakolb 			fss_change_priority(t, fssproc);
2493c97ad5cdSakolb 
2494c97ad5cdSakolb 		/*
2495c97ad5cdSakolb 		 * We need to call cpu_surrender for this thread due to cpucaps
2496c97ad5cdSakolb 		 * enforcement, but fss_change_priority may have already done
2497c97ad5cdSakolb 		 * so. In this case FSSBACKQ is set and there is no need to call
2498c97ad5cdSakolb 		 * cpu-surrender again.
2499c97ad5cdSakolb 		 */
2500c97ad5cdSakolb 		if (!(fssproc->fss_flags & FSSBACKQ))
2501c97ad5cdSakolb 			call_cpu_surrender = B_TRUE;
2502c97ad5cdSakolb 	}
2503c97ad5cdSakolb 
2504c97ad5cdSakolb 	if (call_cpu_surrender) {
25057c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSBACKQ;
25067c478bd9Sstevel@tonic-gate 		cpu_surrender(t);
25077c478bd9Sstevel@tonic-gate 	}
2508c97ad5cdSakolb 
25097c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate /*
25137c478bd9Sstevel@tonic-gate  * Processes waking up go to the back of their queue.  We don't need to assign
25147c478bd9Sstevel@tonic-gate  * a time quantum here because thread is still at a kernel mode priority and
25157c478bd9Sstevel@tonic-gate  * the time slicing is not done for threads running in the kernel after
25167c478bd9Sstevel@tonic-gate  * sleeping.  The proper time quantum will be assigned by fss_trapret before the
25177c478bd9Sstevel@tonic-gate  * thread returns to user mode.
25187c478bd9Sstevel@tonic-gate  */
25197c478bd9Sstevel@tonic-gate static void
25207c478bd9Sstevel@tonic-gate fss_wakeup(kthread_t *t)
25217c478bd9Sstevel@tonic-gate {
25227c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
25257c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_SLEEP);
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 	fss_active(t);
25287c478bd9Sstevel@tonic-gate 
2529d3d50737SRafael Vanoni 	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
25307c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
25317c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
25327c478bd9Sstevel@tonic-gate 
2533*2c164fafSPatrick Mooney 	/* Recalculate the priority. */
2534d3d50737SRafael Vanoni 	if (t->t_disp_time == ddi_get_lbolt()) {
25357c478bd9Sstevel@tonic-gate 		setfrontdq(t);
25367c478bd9Sstevel@tonic-gate 	} else {
25377c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
25387c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
25397c478bd9Sstevel@tonic-gate 		setbackdq(t);
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate /*
25447c478bd9Sstevel@tonic-gate  * fss_donice() is called when a nice(1) command is issued on the thread to
25457c478bd9Sstevel@tonic-gate  * alter the priority. The nice(1) command exists in Solaris for compatibility.
25467c478bd9Sstevel@tonic-gate  * Thread priority adjustments should be done via priocntl(1).
25477c478bd9Sstevel@tonic-gate  */
25487c478bd9Sstevel@tonic-gate static int
25497c478bd9Sstevel@tonic-gate fss_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
25507c478bd9Sstevel@tonic-gate {
25517c478bd9Sstevel@tonic-gate 	int newnice;
25527c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
25537c478bd9Sstevel@tonic-gate 	fssparms_t fssparms;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	/*
25567c478bd9Sstevel@tonic-gate 	 * If there is no change to priority, just return current setting.
25577c478bd9Sstevel@tonic-gate 	 */
25587c478bd9Sstevel@tonic-gate 	if (incr == 0) {
25597c478bd9Sstevel@tonic-gate 		if (retvalp)
25607c478bd9Sstevel@tonic-gate 			*retvalp = fssproc->fss_nice - NZERO;
25617c478bd9Sstevel@tonic-gate 		return (0);
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 
256424d819e6SJerry Jelinek 	if ((incr < 0 || incr > 2 * NZERO) && secpolicy_raisepriority(cr) != 0)
25657c478bd9Sstevel@tonic-gate 		return (EPERM);
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	/*
25687c478bd9Sstevel@tonic-gate 	 * Specifying a nice increment greater than the upper limit of
25697c478bd9Sstevel@tonic-gate 	 * FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
25707c478bd9Sstevel@tonic-gate 	 * value being set to the upper limit.  We check for this before
25717c478bd9Sstevel@tonic-gate 	 * computing the new value because otherwise we could get overflow
25727c478bd9Sstevel@tonic-gate 	 * if a privileged user specified some ridiculous increment.
25737c478bd9Sstevel@tonic-gate 	 */
25747c478bd9Sstevel@tonic-gate 	if (incr > FSS_NICE_MAX)
25757c478bd9Sstevel@tonic-gate 		incr = FSS_NICE_MAX;
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	newnice = fssproc->fss_nice + incr;
25787c478bd9Sstevel@tonic-gate 	if (newnice > FSS_NICE_MAX)
25797c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MAX;
25807c478bd9Sstevel@tonic-gate 	else if (newnice < FSS_NICE_MIN)
25817c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MIN;
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	fssparms.fss_uprilim = fssparms.fss_upri =
25847c478bd9Sstevel@tonic-gate 	    -((newnice - NZERO) * fss_maxupri) / NZERO;
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 	/*
25877c478bd9Sstevel@tonic-gate 	 * Reset the uprilim and upri values of the thread.
25887c478bd9Sstevel@tonic-gate 	 */
25897c478bd9Sstevel@tonic-gate 	(void) fss_parmsset(t, (void *)&fssparms, (id_t)0, (cred_t *)NULL);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 	/*
25927c478bd9Sstevel@tonic-gate 	 * Although fss_parmsset already reset fss_nice it may not have been
25937c478bd9Sstevel@tonic-gate 	 * set to precisely the value calculated above because fss_parmsset
25947c478bd9Sstevel@tonic-gate 	 * determines the nice value from the user priority and we may have
25957c478bd9Sstevel@tonic-gate 	 * truncated during the integer conversion from nice value to user
25967c478bd9Sstevel@tonic-gate 	 * priority and back. We reset fss_nice to the value we calculated
25977c478bd9Sstevel@tonic-gate 	 * above.
25987c478bd9Sstevel@tonic-gate 	 */
25997c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = (char)newnice;
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 	if (retvalp)
26027c478bd9Sstevel@tonic-gate 		*retvalp = newnice - NZERO;
26037c478bd9Sstevel@tonic-gate 	return (0);
26047c478bd9Sstevel@tonic-gate }
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate /*
2607d4204c85Sraf  * Increment the priority of the specified thread by incr and
2608d4204c85Sraf  * return the new value in *retvalp.
2609d4204c85Sraf  */
2610d4204c85Sraf static int
2611d4204c85Sraf fss_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2612d4204c85Sraf {
2613d4204c85Sraf 	int newpri;
2614d4204c85Sraf 	fssproc_t *fssproc = FSSPROC(t);
2615d4204c85Sraf 	fssparms_t fssparms;
2616d4204c85Sraf 
2617d4204c85Sraf 	/*
2618d4204c85Sraf 	 * If there is no change to priority, just return current setting.
2619d4204c85Sraf 	 */
2620d4204c85Sraf 	if (incr == 0) {
2621d4204c85Sraf 		*retvalp = fssproc->fss_upri;
2622d4204c85Sraf 		return (0);
2623d4204c85Sraf 	}
2624d4204c85Sraf 
2625d4204c85Sraf 	newpri = fssproc->fss_upri + incr;
2626d4204c85Sraf 	if (newpri > fss_maxupri || newpri < -fss_maxupri)
2627d4204c85Sraf 		return (EINVAL);
2628d4204c85Sraf 
2629d4204c85Sraf 	*retvalp = newpri;
2630d4204c85Sraf 	fssparms.fss_uprilim = fssparms.fss_upri = newpri;
2631d4204c85Sraf 
2632d4204c85Sraf 	/*
2633d4204c85Sraf 	 * Reset the uprilim and upri values of the thread.
2634d4204c85Sraf 	 */
2635d4204c85Sraf 	return (fss_parmsset(t, &fssparms, (id_t)0, cr));
2636d4204c85Sraf }
2637d4204c85Sraf 
2638d4204c85Sraf /*
26397c478bd9Sstevel@tonic-gate  * Return the global scheduling priority that would be assigned to a thread
26407c478bd9Sstevel@tonic-gate  * entering the fair-sharing class with the fss_upri.
26417c478bd9Sstevel@tonic-gate  */
26427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26437c478bd9Sstevel@tonic-gate static pri_t
26447c478bd9Sstevel@tonic-gate fss_globpri(kthread_t *t)
26457c478bd9Sstevel@tonic-gate {
26467c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	return (fss_maxumdpri / 2);
26497c478bd9Sstevel@tonic-gate }
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate /*
26527c478bd9Sstevel@tonic-gate  * Called from the yield(2) system call when a thread is yielding (surrendering)
26537c478bd9Sstevel@tonic-gate  * the processor. The kernel thread is placed at the back of a dispatch queue.
26547c478bd9Sstevel@tonic-gate  */
26557c478bd9Sstevel@tonic-gate static void
26567c478bd9Sstevel@tonic-gate fss_yield(kthread_t *t)
26577c478bd9Sstevel@tonic-gate {
26587c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
26617c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	/*
2664c97ad5cdSakolb 	 * Collect CPU usage spent before yielding
2665c97ad5cdSakolb 	 */
26668c34bbb7Sakolb 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2667c97ad5cdSakolb 
2668c97ad5cdSakolb 	/*
26697c478bd9Sstevel@tonic-gate 	 * Clear the preemption control "yield" bit since the user is
26707c478bd9Sstevel@tonic-gate 	 * doing a yield.
26717c478bd9Sstevel@tonic-gate 	 */
26727c478bd9Sstevel@tonic-gate 	if (t->t_schedctl)
26737c478bd9Sstevel@tonic-gate 		schedctl_set_yield(t, 0);
26749ac8606fSraf 	/*
26759ac8606fSraf 	 * If fss_preempt() artifically increased the thread's priority
26769ac8606fSraf 	 * to avoid preemption, restore the original priority now.
26779ac8606fSraf 	 */
26789ac8606fSraf 	if (fssproc->fss_flags & FSSRESTORE) {
26799ac8606fSraf 		THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
26809ac8606fSraf 		fssproc->fss_flags &= ~FSSRESTORE;
26819ac8606fSraf 	}
26827c478bd9Sstevel@tonic-gate 	if (fssproc->fss_timeleft < 0) {
26837c478bd9Sstevel@tonic-gate 		/*
26847c478bd9Sstevel@tonic-gate 		 * Time slice was artificially extended to avoid preemption,
26857c478bd9Sstevel@tonic-gate 		 * so pretend we're preempting it now.
26867c478bd9Sstevel@tonic-gate 		 */
26877c478bd9Sstevel@tonic-gate 		DTRACE_SCHED1(schedctl__yield, int, -fssproc->fss_timeleft);
26887c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
26917c478bd9Sstevel@tonic-gate 	setbackdq(t);
26927c478bd9Sstevel@tonic-gate }
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate void
26957c478bd9Sstevel@tonic-gate fss_changeproj(kthread_t *t, void *kp, void *zp, fssbuf_t *projbuf,
26967c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
26977c478bd9Sstevel@tonic-gate {
26987c478bd9Sstevel@tonic-gate 	kproject_t *kpj_new = kp;
26997c478bd9Sstevel@tonic-gate 	zone_t *zone = zp;
27007c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
27017c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
27027c478bd9Sstevel@tonic-gate 	kproject_t *kpj_old;
27037c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
27047c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
27057c478bd9Sstevel@tonic-gate 	int free = 0;
27067c478bd9Sstevel@tonic-gate 	int id;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
27097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
27107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
27137c478bd9Sstevel@tonic-gate 		return;
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
27167c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
27177c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
27187c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
27197c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
27207c478bd9Sstevel@tonic-gate 		return;
27217c478bd9Sstevel@tonic-gate 	}
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj_old);
27247c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
27257c478bd9Sstevel@tonic-gate 	kpj_old = FSSPROJ2KPROJ(fssproj_old);
27267c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	ASSERT(t->t_cpupart == fsspset->fssps_cpupart);
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 	if (kpj_old == kpj_new) {
27317c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
27327c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
27337c478bd9Sstevel@tonic-gate 		return;
27347c478bd9Sstevel@tonic-gate 	}
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset, zone)) == NULL) {
27377c478bd9Sstevel@tonic-gate 		/*
27387c478bd9Sstevel@tonic-gate 		 * If the zone for the new project is not currently active on
27397c478bd9Sstevel@tonic-gate 		 * the cpu partition we're on, get one of the pre-allocated
27407c478bd9Sstevel@tonic-gate 		 * buffers and link it in our per-pset zone list.  Such buffers
27417c478bd9Sstevel@tonic-gate 		 * should already exist.
27427c478bd9Sstevel@tonic-gate 		 */
27437c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
27447c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
27457c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset, zone, fsszone_new);
27467c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
27477c478bd9Sstevel@tonic-gate 				break;
27487c478bd9Sstevel@tonic-gate 			}
27497c478bd9Sstevel@tonic-gate 		}
27507c478bd9Sstevel@tonic-gate 	}
27517c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
27527c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset, kpj_new)) == NULL) {
27537c478bd9Sstevel@tonic-gate 		/*
27547c478bd9Sstevel@tonic-gate 		 * If our new project is not currently running
27557c478bd9Sstevel@tonic-gate 		 * on the cpu partition we're on, get one of the
27567c478bd9Sstevel@tonic-gate 		 * pre-allocated buffers and link it in our new cpu
27577c478bd9Sstevel@tonic-gate 		 * partition doubly linked list. Such buffers should already
27587c478bd9Sstevel@tonic-gate 		 * exist.
27597c478bd9Sstevel@tonic-gate 		 */
27607c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
27617c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
27627c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset, kpj_new,
27637c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
27647c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
27657c478bd9Sstevel@tonic-gate 				break;
27667c478bd9Sstevel@tonic-gate 			}
27677c478bd9Sstevel@tonic-gate 		}
27687c478bd9Sstevel@tonic-gate 	}
27697c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	thread_lock(t);
2772c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2773c97ad5cdSakolb 	    t->t_state == TS_WAIT)
27747c478bd9Sstevel@tonic-gate 		fss_inactive(t);
27757c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_old->fssp_threads > 0);
27767c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
27777c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj_old);
27787c478bd9Sstevel@tonic-gate 		free = 1;
27797c478bd9Sstevel@tonic-gate 	}
27807c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
27817c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
27827c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
2783c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2784c97ad5cdSakolb 	    t->t_state == TS_WAIT)
27857c478bd9Sstevel@tonic-gate 		fss_active(t);
27867c478bd9Sstevel@tonic-gate 	thread_unlock(t);
27877c478bd9Sstevel@tonic-gate 	if (free) {
27887c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
27897c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
27907c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
27917c478bd9Sstevel@tonic-gate 	}
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
27947c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate void
27987c478bd9Sstevel@tonic-gate fss_changepset(kthread_t *t, void *newcp, fssbuf_t *projbuf,
27997c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
28007c478bd9Sstevel@tonic-gate {
28017c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset_old, *fsspset_new;
28027c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
28037c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
28047c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
28057c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
28067c478bd9Sstevel@tonic-gate 	zone_t *zone;
28077c478bd9Sstevel@tonic-gate 	int id;
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
28107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
28117c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
28147c478bd9Sstevel@tonic-gate 		return;
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
28177c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
28187c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
28197c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
28207c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
28217c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
28227c478bd9Sstevel@tonic-gate 		return;
28237c478bd9Sstevel@tonic-gate 	}
28247c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
28257c478bd9Sstevel@tonic-gate 	fsspset_old = FSSPROJ2FSSPSET(fssproj_old);
28267c478bd9Sstevel@tonic-gate 	kpj = FSSPROJ2KPROJ(fssproj_old);
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	if (fsspset_old->fssps_cpupart == newcp) {
28297c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
28307c478bd9Sstevel@tonic-gate 		return;
28317c478bd9Sstevel@tonic-gate 	}
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate 	ASSERT(ttoproj(t) == kpj);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	fsspset_new = fss_find_fsspset(newcp);
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_new->fssps_lock);
28387c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset_new, zone)) == NULL) {
28397c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
28407c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
28417c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset_new, zone,
28427c478bd9Sstevel@tonic-gate 				    fsszone_new);
28437c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
28447c478bd9Sstevel@tonic-gate 				break;
28457c478bd9Sstevel@tonic-gate 			}
28467c478bd9Sstevel@tonic-gate 		}
28477c478bd9Sstevel@tonic-gate 	}
28487c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
28497c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset_new, kpj)) == NULL) {
28507c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
28517c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
28527c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset_new, kpj,
28537c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
28547c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
28557c478bd9Sstevel@tonic-gate 				break;
28567c478bd9Sstevel@tonic-gate 			}
28577c478bd9Sstevel@tonic-gate 		}
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
28627c478bd9Sstevel@tonic-gate 	thread_lock(t);
2863c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2864c97ad5cdSakolb 	    t->t_state == TS_WAIT)
28657c478bd9Sstevel@tonic-gate 		fss_inactive(t);
28667c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
28677c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
2868c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2869c97ad5cdSakolb 	    t->t_state == TS_WAIT)
28707c478bd9Sstevel@tonic-gate 		fss_active(t);
28717c478bd9Sstevel@tonic-gate 	thread_unlock(t);
28727c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_new->fssps_lock);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_old->fssps_lock);
28757c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
28767c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset_old, fssproj_old);
28777c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
28787c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
28797c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
28807c478bd9Sstevel@tonic-gate 	}
28817c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_old->fssps_lock);
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
28847c478bd9Sstevel@tonic-gate }
2885