xref: /titanic_53/usr/src/uts/common/disp/fss.c (revision d4204c85a44d2589b9afff2c81db7044e97f2d1d)
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 /*
23*d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/cred.h>
337c478bd9Sstevel@tonic-gate #include <sys/proc.h>
347c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
357c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
367c478bd9Sstevel@tonic-gate #include <sys/class.h>
377c478bd9Sstevel@tonic-gate #include <sys/disp.h>
387c478bd9Sstevel@tonic-gate #include <sys/procset.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
447c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
467c478bd9Sstevel@tonic-gate #include <sys/project.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/fss.h>
497c478bd9Sstevel@tonic-gate #include <sys/fsspriocntl.h>
507c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
517c478bd9Sstevel@tonic-gate #include <sys/zone.h>
527c478bd9Sstevel@tonic-gate #include <vm/rm.h>
537c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
547c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
557c478bd9Sstevel@tonic-gate #include <sys/policy.h>
567c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
57c97ad5cdSakolb #include <sys/cpucaps.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * FSS Data Structures:
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  *                 fsszone
637c478bd9Sstevel@tonic-gate  *                  -----           -----
647c478bd9Sstevel@tonic-gate  *  -----          |     |         |     |
657c478bd9Sstevel@tonic-gate  * |     |-------->|     |<------->|     |<---->...
667c478bd9Sstevel@tonic-gate  * |     |          -----           -----
677c478bd9Sstevel@tonic-gate  * |     |          ^    ^            ^
687c478bd9Sstevel@tonic-gate  * |     |---       |     \            \
697c478bd9Sstevel@tonic-gate  *  -----    |      |      \            \
707c478bd9Sstevel@tonic-gate  * fsspset   |      |       \            \
717c478bd9Sstevel@tonic-gate  *           |      |        \            \
727c478bd9Sstevel@tonic-gate  *           |    -----       -----       -----
737c478bd9Sstevel@tonic-gate  *            -->|     |<--->|     |<--->|     |
747c478bd9Sstevel@tonic-gate  *               |     |     |     |     |     |
757c478bd9Sstevel@tonic-gate  *                -----       -----       -----
767c478bd9Sstevel@tonic-gate  *               fssproj
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * That is, fsspsets contain a list of fsszone's that are currently active in
807c478bd9Sstevel@tonic-gate  * the pset, and a list of fssproj's, corresponding to projects with runnable
817c478bd9Sstevel@tonic-gate  * threads on the pset.  fssproj's in turn point to the fsszone which they
827c478bd9Sstevel@tonic-gate  * are a member of.
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * An fssproj_t is removed when there are no threads in it.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * An fsszone_t is removed when there are no projects with threads in it.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Projects in a zone compete with each other for cpu time, receiving cpu
897c478bd9Sstevel@tonic-gate  * allocation within a zone proportional to fssproj->fssp_shares
907c478bd9Sstevel@tonic-gate  * (project.cpu-shares); at a higher level zones compete with each other,
917c478bd9Sstevel@tonic-gate  * receiving allocation in a pset proportional to fsszone->fssz_shares
927c478bd9Sstevel@tonic-gate  * (zone.cpu-shares).  See fss_decay_usage() for the precise formula.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static pri_t fss_init(id_t, int, classfuncs_t **);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static struct sclass fss = {
987c478bd9Sstevel@tonic-gate 	"FSS",
997c478bd9Sstevel@tonic-gate 	fss_init,
1007c478bd9Sstevel@tonic-gate 	0
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate extern struct mod_ops mod_schedops;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate static struct modlsched modlsched = {
1097c478bd9Sstevel@tonic-gate 	&mod_schedops, "fair share scheduling class", &fss
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1137c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlsched, NULL
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #define	FSS_MAXUPRI	60
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * The fssproc_t structures are kept in an array of circular doubly linked
1207c478bd9Sstevel@tonic-gate  * lists.  A hash on the thread pointer is used to determine which list each
1217c478bd9Sstevel@tonic-gate  * thread should be placed in.  Each list has a dummy "head" which is never
1227c478bd9Sstevel@tonic-gate  * removed, so the list is never empty.  fss_update traverses these lists to
1237c478bd9Sstevel@tonic-gate  * update the priorities of threads that have been waiting on the run queue.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate #define	FSS_LISTS		16 /* number of lists, must be power of 2 */
1267c478bd9Sstevel@tonic-gate #define	FSS_LIST_HASH(t)	(((uintptr_t)(t) >> 9) & (FSS_LISTS - 1))
1277c478bd9Sstevel@tonic-gate #define	FSS_LIST_NEXT(i)	(((i) + 1) & (FSS_LISTS - 1))
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	FSS_LIST_INSERT(fssproc)				\
1307c478bd9Sstevel@tonic-gate {								\
1317c478bd9Sstevel@tonic-gate 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
1327c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &fss_listlock[index];			\
1337c478bd9Sstevel@tonic-gate 	fssproc_t *headp = &fss_listhead[index];		\
1347c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);					\
1357c478bd9Sstevel@tonic-gate 	fssproc->fss_next = headp->fss_next;			\
1367c478bd9Sstevel@tonic-gate 	fssproc->fss_prev = headp;				\
1377c478bd9Sstevel@tonic-gate 	headp->fss_next->fss_prev = fssproc;			\
1387c478bd9Sstevel@tonic-gate 	headp->fss_next = fssproc;				\
1397c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);					\
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #define	FSS_LIST_DELETE(fssproc)				\
1437c478bd9Sstevel@tonic-gate {								\
1447c478bd9Sstevel@tonic-gate 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
1457c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &fss_listlock[index];			\
1467c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);					\
1477c478bd9Sstevel@tonic-gate 	fssproc->fss_prev->fss_next = fssproc->fss_next;	\
1487c478bd9Sstevel@tonic-gate 	fssproc->fss_next->fss_prev = fssproc->fss_prev;	\
1497c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);					\
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #define	FSS_TICK_COST	1000	/* tick cost for threads with nice level = 0 */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Decay rate percentages are based on n/128 rather than n/100 so  that
1567c478bd9Sstevel@tonic-gate  * calculations can avoid having to do an integer divide by 100 (divide
1577c478bd9Sstevel@tonic-gate  * by FSS_DECAY_BASE == 128 optimizes to an arithmetic shift).
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * FSS_DECAY_MIN	=  83/128 ~= 65%
1607c478bd9Sstevel@tonic-gate  * FSS_DECAY_MAX	= 108/128 ~= 85%
1617c478bd9Sstevel@tonic-gate  * FSS_DECAY_USG	=  96/128 ~= 75%
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate #define	FSS_DECAY_MIN	83	/* fsspri decay pct for threads w/ nice -20 */
1647c478bd9Sstevel@tonic-gate #define	FSS_DECAY_MAX	108	/* fsspri decay pct for threads w/ nice +19 */
1657c478bd9Sstevel@tonic-gate #define	FSS_DECAY_USG	96	/* fssusage decay pct for projects */
1667c478bd9Sstevel@tonic-gate #define	FSS_DECAY_BASE	128	/* base for decay percentages above */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #define	FSS_NICE_MIN	0
1697c478bd9Sstevel@tonic-gate #define	FSS_NICE_MAX	(2 * NZERO - 1)
1707c478bd9Sstevel@tonic-gate #define	FSS_NICE_RANGE	(FSS_NICE_MAX - FSS_NICE_MIN + 1)
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static int	fss_nice_tick[FSS_NICE_RANGE];
1737c478bd9Sstevel@tonic-gate static int	fss_nice_decay[FSS_NICE_RANGE];
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static pri_t	fss_maxupri = FSS_MAXUPRI; /* maximum FSS user priority */
1767c478bd9Sstevel@tonic-gate static pri_t	fss_maxumdpri; /* maximum user mode fss priority */
1777c478bd9Sstevel@tonic-gate static pri_t	fss_maxglobpri;	/* maximum global priority used by fss class */
1787c478bd9Sstevel@tonic-gate static pri_t	fss_minglobpri;	/* minimum global priority */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static fssproc_t fss_listhead[FSS_LISTS];
1817c478bd9Sstevel@tonic-gate static kmutex_t	fss_listlock[FSS_LISTS];
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static fsspset_t *fsspsets;
1847c478bd9Sstevel@tonic-gate static kmutex_t fsspsets_lock;	/* protects fsspsets */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static id_t	fss_cid;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate static time_t	fss_minrun = 2;	/* t_pri becomes 59 within 2 secs */
1897c478bd9Sstevel@tonic-gate static time_t	fss_minslp = 2;	/* min time on sleep queue for hardswap */
1907c478bd9Sstevel@tonic-gate static int	fss_quantum = 11;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static void	fss_newpri(fssproc_t *);
1937c478bd9Sstevel@tonic-gate static void	fss_update(void *);
1947c478bd9Sstevel@tonic-gate static int	fss_update_list(int);
1957c478bd9Sstevel@tonic-gate static void	fss_change_priority(kthread_t *, fssproc_t *);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static int	fss_admin(caddr_t, cred_t *);
1987c478bd9Sstevel@tonic-gate static int	fss_getclinfo(void *);
1997c478bd9Sstevel@tonic-gate static int	fss_parmsin(void *);
2007c478bd9Sstevel@tonic-gate static int	fss_parmsout(void *, pc_vaparms_t *);
2017c478bd9Sstevel@tonic-gate static int	fss_vaparmsin(void *, pc_vaparms_t *);
2027c478bd9Sstevel@tonic-gate static int	fss_vaparmsout(void *, pc_vaparms_t *);
2037c478bd9Sstevel@tonic-gate static int	fss_getclpri(pcpri_t *);
2047c478bd9Sstevel@tonic-gate static int	fss_alloc(void **, int);
2057c478bd9Sstevel@tonic-gate static void	fss_free(void *);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate static int	fss_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
2087c478bd9Sstevel@tonic-gate static void	fss_exitclass(void *);
2097c478bd9Sstevel@tonic-gate static int	fss_canexit(kthread_t *, cred_t *);
2107c478bd9Sstevel@tonic-gate static int	fss_fork(kthread_t *, kthread_t *, void *);
2117c478bd9Sstevel@tonic-gate static void	fss_forkret(kthread_t *, kthread_t *);
2127c478bd9Sstevel@tonic-gate static void	fss_parmsget(kthread_t *, void *);
2137c478bd9Sstevel@tonic-gate static int	fss_parmsset(kthread_t *, void *, id_t, cred_t *);
2147c478bd9Sstevel@tonic-gate static void	fss_stop(kthread_t *, int, int);
2157c478bd9Sstevel@tonic-gate static void	fss_exit(kthread_t *);
2167c478bd9Sstevel@tonic-gate static void	fss_active(kthread_t *);
2177c478bd9Sstevel@tonic-gate static void	fss_inactive(kthread_t *);
2187c478bd9Sstevel@tonic-gate static pri_t	fss_swapin(kthread_t *, int);
2197c478bd9Sstevel@tonic-gate static pri_t	fss_swapout(kthread_t *, int);
2207c478bd9Sstevel@tonic-gate static void	fss_trapret(kthread_t *);
2217c478bd9Sstevel@tonic-gate static void	fss_preempt(kthread_t *);
2227c478bd9Sstevel@tonic-gate static void	fss_setrun(kthread_t *);
2237c478bd9Sstevel@tonic-gate static void	fss_sleep(kthread_t *);
2247c478bd9Sstevel@tonic-gate static void	fss_tick(kthread_t *);
2257c478bd9Sstevel@tonic-gate static void	fss_wakeup(kthread_t *);
2267c478bd9Sstevel@tonic-gate static int	fss_donice(kthread_t *, cred_t *, int, int *);
227*d4204c85Sraf static int	fss_doprio(kthread_t *, cred_t *, int, int *);
2287c478bd9Sstevel@tonic-gate static pri_t	fss_globpri(kthread_t *);
2297c478bd9Sstevel@tonic-gate static void	fss_yield(kthread_t *);
2307c478bd9Sstevel@tonic-gate static void	fss_nullsys();
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static struct classfuncs fss_classfuncs = {
2337c478bd9Sstevel@tonic-gate 	/* class functions */
2347c478bd9Sstevel@tonic-gate 	fss_admin,
2357c478bd9Sstevel@tonic-gate 	fss_getclinfo,
2367c478bd9Sstevel@tonic-gate 	fss_parmsin,
2377c478bd9Sstevel@tonic-gate 	fss_parmsout,
2387c478bd9Sstevel@tonic-gate 	fss_vaparmsin,
2397c478bd9Sstevel@tonic-gate 	fss_vaparmsout,
2407c478bd9Sstevel@tonic-gate 	fss_getclpri,
2417c478bd9Sstevel@tonic-gate 	fss_alloc,
2427c478bd9Sstevel@tonic-gate 	fss_free,
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* thread functions */
2457c478bd9Sstevel@tonic-gate 	fss_enterclass,
2467c478bd9Sstevel@tonic-gate 	fss_exitclass,
2477c478bd9Sstevel@tonic-gate 	fss_canexit,
2487c478bd9Sstevel@tonic-gate 	fss_fork,
2497c478bd9Sstevel@tonic-gate 	fss_forkret,
2507c478bd9Sstevel@tonic-gate 	fss_parmsget,
2517c478bd9Sstevel@tonic-gate 	fss_parmsset,
2527c478bd9Sstevel@tonic-gate 	fss_stop,
2537c478bd9Sstevel@tonic-gate 	fss_exit,
2547c478bd9Sstevel@tonic-gate 	fss_active,
2557c478bd9Sstevel@tonic-gate 	fss_inactive,
2567c478bd9Sstevel@tonic-gate 	fss_swapin,
2577c478bd9Sstevel@tonic-gate 	fss_swapout,
2587c478bd9Sstevel@tonic-gate 	fss_trapret,
2597c478bd9Sstevel@tonic-gate 	fss_preempt,
2607c478bd9Sstevel@tonic-gate 	fss_setrun,
2617c478bd9Sstevel@tonic-gate 	fss_sleep,
2627c478bd9Sstevel@tonic-gate 	fss_tick,
2637c478bd9Sstevel@tonic-gate 	fss_wakeup,
2647c478bd9Sstevel@tonic-gate 	fss_donice,
2657c478bd9Sstevel@tonic-gate 	fss_globpri,
2667c478bd9Sstevel@tonic-gate 	fss_nullsys,	/* set_process_group */
267*d4204c85Sraf 	fss_yield,
268*d4204c85Sraf 	fss_doprio,
2697c478bd9Sstevel@tonic-gate };
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate int
2727c478bd9Sstevel@tonic-gate _init()
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate int
2787c478bd9Sstevel@tonic-gate _fini()
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	return (EBUSY);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate int
2847c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2907c478bd9Sstevel@tonic-gate static int
2917c478bd9Sstevel@tonic-gate fss_project_walker(kproject_t *kpj, void *buf)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate void *
2977c478bd9Sstevel@tonic-gate fss_allocbuf(int op, int type)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	fssbuf_t *fssbuf;
3007c478bd9Sstevel@tonic-gate 	void **fsslist;
3017c478bd9Sstevel@tonic-gate 	int cnt;
3027c478bd9Sstevel@tonic-gate 	int i;
3037c478bd9Sstevel@tonic-gate 	size_t size;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	ASSERT(op == FSS_NPSET_BUF || op == FSS_NPROJ_BUF || op == FSS_ONE_BUF);
3067c478bd9Sstevel@tonic-gate 	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
3077c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	fssbuf = kmem_zalloc(sizeof (fssbuf_t), KM_SLEEP);
3107c478bd9Sstevel@tonic-gate 	switch (op) {
3117c478bd9Sstevel@tonic-gate 	case FSS_NPSET_BUF:
3127c478bd9Sstevel@tonic-gate 		cnt = cpupart_list(NULL, 0, CP_NONEMPTY);
3137c478bd9Sstevel@tonic-gate 		break;
3147c478bd9Sstevel@tonic-gate 	case FSS_NPROJ_BUF:
3157c478bd9Sstevel@tonic-gate 		cnt = project_walk_all(ALL_ZONES, fss_project_walker, NULL);
3167c478bd9Sstevel@tonic-gate 		break;
3177c478bd9Sstevel@tonic-gate 	case FSS_ONE_BUF:
3187c478bd9Sstevel@tonic-gate 		cnt = 1;
3197c478bd9Sstevel@tonic-gate 		break;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	switch (type) {
3237c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_PROJ:
3247c478bd9Sstevel@tonic-gate 		size = sizeof (fssproj_t);
3257c478bd9Sstevel@tonic-gate 		break;
3267c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_ZONE:
3277c478bd9Sstevel@tonic-gate 		size = sizeof (fsszone_t);
3287c478bd9Sstevel@tonic-gate 		break;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	fsslist = kmem_zalloc(cnt * sizeof (void *), KM_SLEEP);
3317c478bd9Sstevel@tonic-gate 	fssbuf->fssb_size = cnt;
3327c478bd9Sstevel@tonic-gate 	fssbuf->fssb_list = fsslist;
3337c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++)
3347c478bd9Sstevel@tonic-gate 		fsslist[i] = kmem_zalloc(size, KM_SLEEP);
3357c478bd9Sstevel@tonic-gate 	return (fssbuf);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate void
3397c478bd9Sstevel@tonic-gate fss_freebuf(fssbuf_t *fssbuf, int type)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	void **fsslist;
3427c478bd9Sstevel@tonic-gate 	int i;
3437c478bd9Sstevel@tonic-gate 	size_t size;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	ASSERT(fssbuf != NULL);
3467c478bd9Sstevel@tonic-gate 	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
3477c478bd9Sstevel@tonic-gate 	fsslist = fssbuf->fssb_list;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	switch (type) {
3507c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_PROJ:
3517c478bd9Sstevel@tonic-gate 		size = sizeof (fssproj_t);
3527c478bd9Sstevel@tonic-gate 		break;
3537c478bd9Sstevel@tonic-gate 	case FSS_ALLOC_ZONE:
3547c478bd9Sstevel@tonic-gate 		size = sizeof (fsszone_t);
3557c478bd9Sstevel@tonic-gate 		break;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	for (i = 0; i < fssbuf->fssb_size; i++) {
3597c478bd9Sstevel@tonic-gate 		if (fsslist[i] != NULL)
3607c478bd9Sstevel@tonic-gate 			kmem_free(fsslist[i], size);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	kmem_free(fsslist, sizeof (void *) * fssbuf->fssb_size);
3637c478bd9Sstevel@tonic-gate 	kmem_free(fssbuf, sizeof (fssbuf_t));
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate static fsspset_t *
3677c478bd9Sstevel@tonic-gate fss_find_fsspset(cpupart_t *cpupart)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	int i;
3707c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset = NULL;
3717c478bd9Sstevel@tonic-gate 	int found = 0;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	ASSERT(cpupart != NULL);
3747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * Search for the cpupart pointer in the array of fsspsets.
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_ncpus; i++) {
3807c478bd9Sstevel@tonic-gate 		fsspset = &fsspsets[i];
3817c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_cpupart == cpupart) {
3827c478bd9Sstevel@tonic-gate 			ASSERT(fsspset->fssps_nproj > 0);
3837c478bd9Sstevel@tonic-gate 			found = 1;
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	if (found == 0) {
3887c478bd9Sstevel@tonic-gate 		/*
3897c478bd9Sstevel@tonic-gate 		 * If we didn't find anything, then use the first
3907c478bd9Sstevel@tonic-gate 		 * available slot in the fsspsets array.
3917c478bd9Sstevel@tonic-gate 		 */
3927c478bd9Sstevel@tonic-gate 		for (i = 0; i < max_ncpus; i++) {
3937c478bd9Sstevel@tonic-gate 			fsspset = &fsspsets[i];
3947c478bd9Sstevel@tonic-gate 			if (fsspset->fssps_cpupart == NULL) {
3957c478bd9Sstevel@tonic-gate 				ASSERT(fsspset->fssps_nproj == 0);
3967c478bd9Sstevel@tonic-gate 				found = 1;
3977c478bd9Sstevel@tonic-gate 				break;
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		fsspset->fssps_cpupart = cpupart;
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	ASSERT(found == 1);
4037c478bd9Sstevel@tonic-gate 	return (fsspset);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static void
4077c478bd9Sstevel@tonic-gate fss_del_fsspset(fsspset_t *fsspset)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
4107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
4117c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_nproj == 0);
4127c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_list == NULL);
4137c478bd9Sstevel@tonic-gate 	ASSERT(fsspset->fssps_zones == NULL);
4147c478bd9Sstevel@tonic-gate 	fsspset->fssps_cpupart = NULL;
4157c478bd9Sstevel@tonic-gate 	fsspset->fssps_maxfsspri = 0;
4167c478bd9Sstevel@tonic-gate 	fsspset->fssps_shares = 0;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * The following routine returns a pointer to the fsszone structure which
4217c478bd9Sstevel@tonic-gate  * belongs to zone "zone" and cpu partition fsspset, if such structure exists.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate static fsszone_t *
4247c478bd9Sstevel@tonic-gate fss_find_fsszone(fsspset_t *fsspset, zone_t *zone)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list != NULL) {
4317c478bd9Sstevel@tonic-gate 		/*
4327c478bd9Sstevel@tonic-gate 		 * There are projects/zones active on this cpu partition
4337c478bd9Sstevel@tonic-gate 		 * already.  Try to find our zone among them.
4347c478bd9Sstevel@tonic-gate 		 */
4357c478bd9Sstevel@tonic-gate 		fsszone = fsspset->fssps_zones;
4367c478bd9Sstevel@tonic-gate 		do {
4377c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_zone == zone) {
4387c478bd9Sstevel@tonic-gate 				return (fsszone);
4397c478bd9Sstevel@tonic-gate 			}
4407c478bd9Sstevel@tonic-gate 			fsszone = fsszone->fssz_next;
4417c478bd9Sstevel@tonic-gate 		} while (fsszone != fsspset->fssps_zones);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	return (NULL);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * The following routine links new fsszone structure into doubly linked list of
4487c478bd9Sstevel@tonic-gate  * zones active on the specified cpu partition.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate static void
4517c478bd9Sstevel@tonic-gate fss_insert_fsszone(fsspset_t *fsspset, zone_t *zone, fsszone_t *fsszone)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	fsszone->fssz_zone = zone;
4567c478bd9Sstevel@tonic-gate 	fsszone->fssz_rshares = zone->zone_shares;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_zones == NULL) {
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * This will be the first fsszone for this fsspset
4617c478bd9Sstevel@tonic-gate 		 */
4627c478bd9Sstevel@tonic-gate 		fsszone->fssz_next = fsszone->fssz_prev = fsszone;
4637c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = fsszone;
4647c478bd9Sstevel@tonic-gate 	} else {
4657c478bd9Sstevel@tonic-gate 		/*
4667c478bd9Sstevel@tonic-gate 		 * Insert this fsszone to the doubly linked list.
4677c478bd9Sstevel@tonic-gate 		 */
4687c478bd9Sstevel@tonic-gate 		fsszone_t *fssz_head = fsspset->fssps_zones;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		fsszone->fssz_next = fssz_head;
4717c478bd9Sstevel@tonic-gate 		fsszone->fssz_prev = fssz_head->fssz_prev;
4727c478bd9Sstevel@tonic-gate 		fssz_head->fssz_prev->fssz_next = fsszone;
4737c478bd9Sstevel@tonic-gate 		fssz_head->fssz_prev = fsszone;
4747c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = fsszone;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate  * The following routine removes a single fsszone structure from the doubly
4807c478bd9Sstevel@tonic-gate  * linked list of zones active on the specified cpu partition.  Note that
4817c478bd9Sstevel@tonic-gate  * global fsspsets_lock must be held in case this fsszone structure is the last
4827c478bd9Sstevel@tonic-gate  * on the above mentioned list.  Also note that the fsszone structure is not
4837c478bd9Sstevel@tonic-gate  * freed here, it is the responsibility of the caller to call kmem_free for it.
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate static void
4867c478bd9Sstevel@tonic-gate fss_remove_fsszone(fsspset_t *fsspset, fsszone_t *fsszone)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
4897c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_nproj == 0);
4907c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_shares == 0);
4917c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_runnable == 0);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (fsszone->fssz_next != fsszone) {
4947c478bd9Sstevel@tonic-gate 		/*
4957c478bd9Sstevel@tonic-gate 		 * This is not the last zone in the list.
4967c478bd9Sstevel@tonic-gate 		 */
4977c478bd9Sstevel@tonic-gate 		fsszone->fssz_prev->fssz_next = fsszone->fssz_next;
4987c478bd9Sstevel@tonic-gate 		fsszone->fssz_next->fssz_prev = fsszone->fssz_prev;
4997c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_zones == fsszone)
5007c478bd9Sstevel@tonic-gate 			fsspset->fssps_zones = fsszone->fssz_next;
5017c478bd9Sstevel@tonic-gate 	} else {
5027c478bd9Sstevel@tonic-gate 		/*
5037c478bd9Sstevel@tonic-gate 		 * This was the last zone active in this cpu partition.
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		fsspset->fssps_zones = NULL;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * The following routine returns a pointer to the fssproj structure
5117c478bd9Sstevel@tonic-gate  * which belongs to project kpj and cpu partition fsspset, if such structure
5127c478bd9Sstevel@tonic-gate  * exists.
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate static fssproj_t *
5157c478bd9Sstevel@tonic-gate fss_find_fssproj(fsspset_t *fsspset, kproject_t *kpj)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list != NULL) {
5227c478bd9Sstevel@tonic-gate 		/*
5237c478bd9Sstevel@tonic-gate 		 * There are projects running on this cpu partition already.
5247c478bd9Sstevel@tonic-gate 		 * Try to find our project among them.
5257c478bd9Sstevel@tonic-gate 		 */
5267c478bd9Sstevel@tonic-gate 		fssproj = fsspset->fssps_list;
5277c478bd9Sstevel@tonic-gate 		do {
5287c478bd9Sstevel@tonic-gate 			if (fssproj->fssp_proj == kpj) {
5297c478bd9Sstevel@tonic-gate 				ASSERT(fssproj->fssp_pset == fsspset);
5307c478bd9Sstevel@tonic-gate 				return (fssproj);
5317c478bd9Sstevel@tonic-gate 			}
5327c478bd9Sstevel@tonic-gate 			fssproj = fssproj->fssp_next;
5337c478bd9Sstevel@tonic-gate 		} while (fssproj != fsspset->fssps_list);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	return (NULL);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * The following routine links new fssproj structure into doubly linked list
5407c478bd9Sstevel@tonic-gate  * of projects running on the specified cpu partition.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate static void
5437c478bd9Sstevel@tonic-gate fss_insert_fssproj(fsspset_t *fsspset, kproject_t *kpj, fsszone_t *fsszone,
5447c478bd9Sstevel@tonic-gate     fssproj_t *fssproj)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	fssproj->fssp_pset = fsspset;
5497c478bd9Sstevel@tonic-gate 	fssproj->fssp_proj = kpj;
5507c478bd9Sstevel@tonic-gate 	fssproj->fssp_shares = kpj->kpj_shares;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	fsspset->fssps_nproj++;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (fsspset->fssps_list == NULL) {
5557c478bd9Sstevel@tonic-gate 		/*
5567c478bd9Sstevel@tonic-gate 		 * This will be the first fssproj for this fsspset
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		fssproj->fssp_next = fssproj->fssp_prev = fssproj;
5597c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = fssproj;
5607c478bd9Sstevel@tonic-gate 	} else {
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * Insert this fssproj to the doubly linked list.
5637c478bd9Sstevel@tonic-gate 		 */
5647c478bd9Sstevel@tonic-gate 		fssproj_t *fssp_head = fsspset->fssps_list;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		fssproj->fssp_next = fssp_head;
5677c478bd9Sstevel@tonic-gate 		fssproj->fssp_prev = fssp_head->fssp_prev;
5687c478bd9Sstevel@tonic-gate 		fssp_head->fssp_prev->fssp_next = fssproj;
5697c478bd9Sstevel@tonic-gate 		fssp_head->fssp_prev = fssproj;
5707c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = fssproj;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	fssproj->fssp_fsszone = fsszone;
5737c478bd9Sstevel@tonic-gate 	fsszone->fssz_nproj++;
5747c478bd9Sstevel@tonic-gate 	ASSERT(fsszone->fssz_nproj != 0);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * The following routine removes a single fssproj structure from the doubly
5797c478bd9Sstevel@tonic-gate  * linked list of projects running on the specified cpu partition.  Note that
5807c478bd9Sstevel@tonic-gate  * global fsspsets_lock must be held in case if this fssproj structure is the
5817c478bd9Sstevel@tonic-gate  * last on the above mentioned list.  Also note that the fssproj structure is
5827c478bd9Sstevel@tonic-gate  * not freed here, it is the responsibility of the caller to call kmem_free
5837c478bd9Sstevel@tonic-gate  * for it.
5847c478bd9Sstevel@tonic-gate  */
5857c478bd9Sstevel@tonic-gate static void
5867c478bd9Sstevel@tonic-gate fss_remove_fssproj(fsspset_t *fsspset, fssproj_t *fssproj)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspsets_lock));
5917c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
5927c478bd9Sstevel@tonic-gate 	ASSERT(fssproj->fssp_runnable == 0);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	fsspset->fssps_nproj--;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
5977c478bd9Sstevel@tonic-gate 	fsszone->fssz_nproj--;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (fssproj->fssp_next != fssproj) {
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * This is not the last part in the list.
6027c478bd9Sstevel@tonic-gate 		 */
6037c478bd9Sstevel@tonic-gate 		fssproj->fssp_prev->fssp_next = fssproj->fssp_next;
6047c478bd9Sstevel@tonic-gate 		fssproj->fssp_next->fssp_prev = fssproj->fssp_prev;
6057c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_list == fssproj)
6067c478bd9Sstevel@tonic-gate 			fsspset->fssps_list = fssproj->fssp_next;
6077c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
6087c478bd9Sstevel@tonic-gate 			fss_remove_fsszone(fsspset, fsszone);
6097c478bd9Sstevel@tonic-gate 	} else {
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * This was the last project part running
6127c478bd9Sstevel@tonic-gate 		 * at this cpu partition.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		fsspset->fssps_list = NULL;
6157c478bd9Sstevel@tonic-gate 		ASSERT(fsspset->fssps_nproj == 0);
6167c478bd9Sstevel@tonic-gate 		ASSERT(fsszone->fssz_nproj == 0);
6177c478bd9Sstevel@tonic-gate 		fss_remove_fsszone(fsspset, fsszone);
6187c478bd9Sstevel@tonic-gate 		fss_del_fsspset(fsspset);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate static void
6237c478bd9Sstevel@tonic-gate fss_inactive(kthread_t *t)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
6267c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
6277c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
6287c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
6317c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
6327c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
6337c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)	/* if this thread already exited */
6347c478bd9Sstevel@tonic-gate 		return;
6357c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
6367c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
6377c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
6387c478bd9Sstevel@tonic-gate 	ASSERT(fssproj->fssp_runnable > 0);
6397c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_runnable == 0) {
6407c478bd9Sstevel@tonic-gate 		fsszone->fssz_shares -= fssproj->fssp_shares;
6417c478bd9Sstevel@tonic-gate 		if (--fsszone->fssz_runnable == 0)
6427c478bd9Sstevel@tonic-gate 			fsspset->fssps_shares -= fsszone->fssz_rshares;
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 	ASSERT(fssproc->fss_runnable == 1);
6457c478bd9Sstevel@tonic-gate 	fssproc->fss_runnable = 0;
6467c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate static void
6507c478bd9Sstevel@tonic-gate fss_active(kthread_t *t)
6517c478bd9Sstevel@tonic-gate {
6527c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
6537c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
6547c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
6557c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
6587c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
6597c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
6607c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)	/* if this thread already exited */
6617c478bd9Sstevel@tonic-gate 		return;
6627c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
6637c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
6647c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
6657c478bd9Sstevel@tonic-gate 	if (++fssproj->fssp_runnable == 1) {
6667c478bd9Sstevel@tonic-gate 		fsszone->fssz_shares += fssproj->fssp_shares;
6677c478bd9Sstevel@tonic-gate 		if (++fsszone->fssz_runnable == 1)
6687c478bd9Sstevel@tonic-gate 			fsspset->fssps_shares += fsszone->fssz_rshares;
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	ASSERT(fssproc->fss_runnable == 0);
6717c478bd9Sstevel@tonic-gate 	fssproc->fss_runnable = 1;
6727c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate /*
6767c478bd9Sstevel@tonic-gate  * Fair share scheduler initialization. Called by dispinit() at boot time.
6777c478bd9Sstevel@tonic-gate  * We can ignore clparmsz argument since we know that the smallest possible
6787c478bd9Sstevel@tonic-gate  * parameter buffer is big enough for us.
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6817c478bd9Sstevel@tonic-gate static pri_t
6827c478bd9Sstevel@tonic-gate fss_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	int i;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	fss_cid = cid;
6897c478bd9Sstevel@tonic-gate 	fss_maxumdpri = minclsyspri - 1;
6907c478bd9Sstevel@tonic-gate 	fss_maxglobpri = minclsyspri;
6917c478bd9Sstevel@tonic-gate 	fss_minglobpri = 0;
6927c478bd9Sstevel@tonic-gate 	fsspsets = kmem_zalloc(sizeof (fsspset_t) * max_ncpus, KM_SLEEP);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/*
6957c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc hash table.
6967c478bd9Sstevel@tonic-gate 	 */
6977c478bd9Sstevel@tonic-gate 	for (i = 0; i < FSS_LISTS; i++)
6987c478bd9Sstevel@tonic-gate 		fss_listhead[i].fss_next = fss_listhead[i].fss_prev =
6997c478bd9Sstevel@tonic-gate 		    &fss_listhead[i];
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	*clfuncspp = &fss_classfuncs;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * Fill in fss_nice_tick and fss_nice_decay arrays:
7057c478bd9Sstevel@tonic-gate 	 * The cost of a tick is lower at positive nice values (so that it
7067c478bd9Sstevel@tonic-gate 	 * will not increase its project's usage as much as normal) with 50%
7077c478bd9Sstevel@tonic-gate 	 * drop at the maximum level and 50% increase at the minimum level.
7087c478bd9Sstevel@tonic-gate 	 * The fsspri decay is slower at positive nice values.  fsspri values
7097c478bd9Sstevel@tonic-gate 	 * of processes with negative nice levels must decay faster to receive
7107c478bd9Sstevel@tonic-gate 	 * time slices more frequently than normal.
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	for (i = 0; i < FSS_NICE_RANGE; i++) {
7137c478bd9Sstevel@tonic-gate 		fss_nice_tick[i] = (FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2)
7147c478bd9Sstevel@tonic-gate 		    - i)) / FSS_NICE_RANGE;
7157c478bd9Sstevel@tonic-gate 		fss_nice_decay[i] = FSS_DECAY_MIN +
7167c478bd9Sstevel@tonic-gate 		    ((FSS_DECAY_MAX - FSS_DECAY_MIN) * i) /
7177c478bd9Sstevel@tonic-gate 		    (FSS_NICE_RANGE - 1);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	return (fss_maxglobpri);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate  * Calculate the new cpupri based on the usage, the number of shares and
7257c478bd9Sstevel@tonic-gate  * the number of active threads.  Reset the tick counter for this thread.
7267c478bd9Sstevel@tonic-gate  */
7277c478bd9Sstevel@tonic-gate static void
7287c478bd9Sstevel@tonic-gate fss_newpri(fssproc_t *fssproc)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	kthread_t *tp;
7317c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
7327c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
7337c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
7347c478bd9Sstevel@tonic-gate 	fsspri_t fsspri, maxfsspri;
7357c478bd9Sstevel@tonic-gate 	pri_t invpri;
7367c478bd9Sstevel@tonic-gate 	uint32_t ticks;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	tp = fssproc->fss_tp;
7397c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (tp->t_cid != fss_cid)
7427c478bd9Sstevel@tonic-gate 		return;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(tp));
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
7477c478bd9Sstevel@tonic-gate 	fsszone = FSSPROJ2FSSZONE(fssproj);
7487c478bd9Sstevel@tonic-gate 	if (fssproj == NULL)
7497c478bd9Sstevel@tonic-gate 		/*
7507c478bd9Sstevel@tonic-gate 		 * No need to change priority of exited threads.
7517c478bd9Sstevel@tonic-gate 		 */
7527c478bd9Sstevel@tonic-gate 		return;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
7557c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (fssproj->fssp_shares == 0 || fsszone->fssz_rshares == 0) {
7587c478bd9Sstevel@tonic-gate 		/*
7597c478bd9Sstevel@tonic-gate 		 * Special case: threads with no shares.
7607c478bd9Sstevel@tonic-gate 		 */
7617c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = fss_minglobpri;
7627c478bd9Sstevel@tonic-gate 		fssproc->fss_ticks = 0;
7637c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
7647c478bd9Sstevel@tonic-gate 		return;
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/*
7687c478bd9Sstevel@tonic-gate 	 * fsspri += shusage * nrunnable * ticks
7697c478bd9Sstevel@tonic-gate 	 */
7707c478bd9Sstevel@tonic-gate 	ticks = fssproc->fss_ticks;
7717c478bd9Sstevel@tonic-gate 	fssproc->fss_ticks = 0;
7727c478bd9Sstevel@tonic-gate 	fsspri = fssproc->fss_fsspri;
7737c478bd9Sstevel@tonic-gate 	fsspri += fssproj->fssp_shusage * fssproj->fssp_runnable * ticks;
7747c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = fsspri;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (fsspri < fss_maxumdpri)
7777c478bd9Sstevel@tonic-gate 		fsspri = fss_maxumdpri;	/* so that maxfsspri is != 0 */
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	/*
7807c478bd9Sstevel@tonic-gate 	 * The general priority formula:
7817c478bd9Sstevel@tonic-gate 	 *
7827c478bd9Sstevel@tonic-gate 	 *			(fsspri * umdprirange)
7837c478bd9Sstevel@tonic-gate 	 *   pri = maxumdpri - ------------------------
7847c478bd9Sstevel@tonic-gate 	 *				maxfsspri
7857c478bd9Sstevel@tonic-gate 	 *
7867c478bd9Sstevel@tonic-gate 	 * If this thread's fsspri is greater than the previous largest
7877c478bd9Sstevel@tonic-gate 	 * fsspri, then record it as the new high and priority for this
7887c478bd9Sstevel@tonic-gate 	 * thread will be one (the lowest priority assigned to a thread
7897c478bd9Sstevel@tonic-gate 	 * that has non-zero shares).
7907c478bd9Sstevel@tonic-gate 	 * Note that this formula cannot produce out of bounds priority
7917c478bd9Sstevel@tonic-gate 	 * values; if it is changed, additional checks may need  to  be
7927c478bd9Sstevel@tonic-gate 	 * added.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 	maxfsspri = fsspset->fssps_maxfsspri;
7957c478bd9Sstevel@tonic-gate 	if (fsspri >= maxfsspri) {
7967c478bd9Sstevel@tonic-gate 		fsspset->fssps_maxfsspri = fsspri;
7977c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
7987c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = 1;
7997c478bd9Sstevel@tonic-gate 	} else {
8007c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
8017c478bd9Sstevel@tonic-gate 		invpri = (fsspri * (fss_maxumdpri - 1)) / maxfsspri;
8027c478bd9Sstevel@tonic-gate 		fssproc->fss_umdpri = fss_maxumdpri - invpri;
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * Decays usages of all running projects and resets their tick counters.
8087c478bd9Sstevel@tonic-gate  * Called once per second from fss_update() after updating priorities.
8097c478bd9Sstevel@tonic-gate  */
8107c478bd9Sstevel@tonic-gate static void
8117c478bd9Sstevel@tonic-gate fss_decay_usage()
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	uint32_t zone_ext_shares, zone_int_shares;
8147c478bd9Sstevel@tonic-gate 	uint32_t kpj_shares, pset_shares;
8157c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
8167c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
8177c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
8187c478bd9Sstevel@tonic-gate 	fsspri_t maxfsspri;
8197c478bd9Sstevel@tonic-gate 	int psetid;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
8227c478bd9Sstevel@tonic-gate 	/*
8237c478bd9Sstevel@tonic-gate 	 * Go through all active processor sets and decay usages of projects
8247c478bd9Sstevel@tonic-gate 	 * running on them.
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 	for (psetid = 0; psetid < max_ncpus; psetid++) {
8277c478bd9Sstevel@tonic-gate 		fsspset = &fsspsets[psetid];
8287c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		if (fsspset->fssps_cpupart == NULL ||
8317c478bd9Sstevel@tonic-gate 		    (fssproj = fsspset->fssps_list) == NULL) {
8327c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
8337c478bd9Sstevel@tonic-gate 			continue;
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * Decay maxfsspri for this cpu partition with the
8387c478bd9Sstevel@tonic-gate 		 * fastest possible decay rate.
8397c478bd9Sstevel@tonic-gate 		 */
8407c478bd9Sstevel@tonic-gate 		disp_lock_enter(&fsspset->fssps_displock);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		maxfsspri = (fsspset->fssps_maxfsspri *
8437c478bd9Sstevel@tonic-gate 		    fss_nice_decay[NZERO]) / FSS_DECAY_BASE;
8447c478bd9Sstevel@tonic-gate 		if (maxfsspri < fss_maxumdpri)
8457c478bd9Sstevel@tonic-gate 			maxfsspri = fss_maxumdpri;
8467c478bd9Sstevel@tonic-gate 		fsspset->fssps_maxfsspri = maxfsspri;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 		do {
8497c478bd9Sstevel@tonic-gate 			/*
8507c478bd9Sstevel@tonic-gate 			 * Decay usage for each project running on
8517c478bd9Sstevel@tonic-gate 			 * this cpu partition.
8527c478bd9Sstevel@tonic-gate 			 */
8537c478bd9Sstevel@tonic-gate 			fssproj->fssp_usage =
8547c478bd9Sstevel@tonic-gate 			    (fssproj->fssp_usage * FSS_DECAY_USG) /
8557c478bd9Sstevel@tonic-gate 			    FSS_DECAY_BASE + fssproj->fssp_ticks;
8567c478bd9Sstevel@tonic-gate 			fssproj->fssp_ticks = 0;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 			fsszone = fssproj->fssp_fsszone;
8597c478bd9Sstevel@tonic-gate 			/*
8607c478bd9Sstevel@tonic-gate 			 * Readjust the project's number of shares if it has
8617c478bd9Sstevel@tonic-gate 			 * changed since we checked it last time.
8627c478bd9Sstevel@tonic-gate 			 */
8637c478bd9Sstevel@tonic-gate 			kpj_shares = fssproj->fssp_proj->kpj_shares;
8647c478bd9Sstevel@tonic-gate 			if (fssproj->fssp_shares != kpj_shares) {
8657c478bd9Sstevel@tonic-gate 				if (fssproj->fssp_runnable != 0) {
8667c478bd9Sstevel@tonic-gate 					fsszone->fssz_shares -=
8677c478bd9Sstevel@tonic-gate 					    fssproj->fssp_shares;
8687c478bd9Sstevel@tonic-gate 					fsszone->fssz_shares += kpj_shares;
8697c478bd9Sstevel@tonic-gate 				}
8707c478bd9Sstevel@tonic-gate 				fssproj->fssp_shares = kpj_shares;
8717c478bd9Sstevel@tonic-gate 			}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			/*
8747c478bd9Sstevel@tonic-gate 			 * Readjust the zone's number of shares if it
8757c478bd9Sstevel@tonic-gate 			 * has changed since we checked it last time.
8767c478bd9Sstevel@tonic-gate 			 */
8777c478bd9Sstevel@tonic-gate 			zone_ext_shares = fsszone->fssz_zone->zone_shares;
8787c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_rshares != zone_ext_shares) {
8797c478bd9Sstevel@tonic-gate 				if (fsszone->fssz_runnable != 0) {
8807c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
8817c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
8827c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares +=
8837c478bd9Sstevel@tonic-gate 					    zone_ext_shares;
8847c478bd9Sstevel@tonic-gate 				}
8857c478bd9Sstevel@tonic-gate 				fsszone->fssz_rshares = zone_ext_shares;
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 			zone_int_shares = fsszone->fssz_shares;
8887c478bd9Sstevel@tonic-gate 			pset_shares = fsspset->fssps_shares;
8897c478bd9Sstevel@tonic-gate 			/*
8907c478bd9Sstevel@tonic-gate 			 * Calculate fssp_shusage value to be used
8917c478bd9Sstevel@tonic-gate 			 * for fsspri increments for the next second.
8927c478bd9Sstevel@tonic-gate 			 */
893a14d39b7Sandrei 			if (kpj_shares == 0 || zone_ext_shares == 0) {
894a14d39b7Sandrei 				fssproj->fssp_shusage = 0;
895a14d39b7Sandrei 			} else if (FSSPROJ2KPROJ(fssproj) == proj0p) {
8967c478bd9Sstevel@tonic-gate 				/*
8977c478bd9Sstevel@tonic-gate 				 * Project 0 in the global zone has 50%
8987c478bd9Sstevel@tonic-gate 				 * of its zone.
8997c478bd9Sstevel@tonic-gate 				 */
9007c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = (fssproj->fssp_usage *
9017c478bd9Sstevel@tonic-gate 				    zone_int_shares * zone_int_shares) /
9027c478bd9Sstevel@tonic-gate 				    (zone_ext_shares * zone_ext_shares);
9037c478bd9Sstevel@tonic-gate 			} else {
9047c478bd9Sstevel@tonic-gate 				/*
9057c478bd9Sstevel@tonic-gate 				 * Thread's priority is based on its project's
9067c478bd9Sstevel@tonic-gate 				 * normalized usage (shusage) value which gets
9077c478bd9Sstevel@tonic-gate 				 * calculated this way:
9087c478bd9Sstevel@tonic-gate 				 *
9097c478bd9Sstevel@tonic-gate 				 *	   pset_shares^2    zone_int_shares^2
9107c478bd9Sstevel@tonic-gate 				 * usage * ------------- * ------------------
9117c478bd9Sstevel@tonic-gate 				 *	   kpj_shares^2	    zone_ext_shares^2
9127c478bd9Sstevel@tonic-gate 				 *
9137c478bd9Sstevel@tonic-gate 				 * Where zone_int_shares is the sum of shares
9147c478bd9Sstevel@tonic-gate 				 * of all active projects within the zone (and
9157c478bd9Sstevel@tonic-gate 				 * the pset), and zone_ext_shares is the number
9167c478bd9Sstevel@tonic-gate 				 * of zone shares (ie, zone.cpu-shares).
9177c478bd9Sstevel@tonic-gate 				 *
9187c478bd9Sstevel@tonic-gate 				 * If there is only one zone active on the pset
9197c478bd9Sstevel@tonic-gate 				 * the above reduces to:
9207c478bd9Sstevel@tonic-gate 				 *
9217c478bd9Sstevel@tonic-gate 				 * 			zone_int_shares^2
9227c478bd9Sstevel@tonic-gate 				 * shusage = usage * ---------------------
9237c478bd9Sstevel@tonic-gate 				 * 			kpj_shares^2
9247c478bd9Sstevel@tonic-gate 				 *
9257c478bd9Sstevel@tonic-gate 				 * If there's only one project active in the
9267c478bd9Sstevel@tonic-gate 				 * zone this formula reduces to:
9277c478bd9Sstevel@tonic-gate 				 *
9287c478bd9Sstevel@tonic-gate 				 *			pset_shares^2
9297c478bd9Sstevel@tonic-gate 				 * shusage = usage * ----------------------
9307c478bd9Sstevel@tonic-gate 				 *			zone_ext_shares^2
9317c478bd9Sstevel@tonic-gate 				 */
9327c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage = fssproj->fssp_usage *
9337c478bd9Sstevel@tonic-gate 				    pset_shares * zone_int_shares;
9347c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage /=
9357c478bd9Sstevel@tonic-gate 				    kpj_shares * zone_ext_shares;
9367c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage *=
9377c478bd9Sstevel@tonic-gate 				    pset_shares * zone_int_shares;
9387c478bd9Sstevel@tonic-gate 				fssproj->fssp_shusage /=
9397c478bd9Sstevel@tonic-gate 				    kpj_shares * zone_ext_shares;
9407c478bd9Sstevel@tonic-gate 			}
9417c478bd9Sstevel@tonic-gate 			fssproj = fssproj->fssp_next;
9427c478bd9Sstevel@tonic-gate 		} while (fssproj != fsspset->fssps_list);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		disp_lock_exit(&fsspset->fssps_displock);
9457c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate static void
9517c478bd9Sstevel@tonic-gate fss_change_priority(kthread_t *t, fssproc_t *fssproc)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate 	pri_t new_pri;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9567c478bd9Sstevel@tonic-gate 	new_pri = fssproc->fss_umdpri;
9577c478bd9Sstevel@tonic-gate 	ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
9587c478bd9Sstevel@tonic-gate 
959*d4204c85Sraf 	t->t_cpri = fssproc->fss_upri;
9609ac8606fSraf 	fssproc->fss_flags &= ~FSSRESTORE;
9617c478bd9Sstevel@tonic-gate 	if (t == curthread || t->t_state == TS_ONPROC) {
9627c478bd9Sstevel@tonic-gate 		/*
9637c478bd9Sstevel@tonic-gate 		 * curthread is always onproc
9647c478bd9Sstevel@tonic-gate 		 */
9657c478bd9Sstevel@tonic-gate 		cpu_t *cp = t->t_disp_queue->disp_cpu;
9667c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, new_pri);
9677c478bd9Sstevel@tonic-gate 		if (t == cp->cpu_dispthread)
9687c478bd9Sstevel@tonic-gate 			cp->cpu_dispatch_pri = DISP_PRIO(t);
9697c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t)) {
9707c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
9717c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
9727c478bd9Sstevel@tonic-gate 		} else {
9737c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 	} else {
9767c478bd9Sstevel@tonic-gate 		/*
9777c478bd9Sstevel@tonic-gate 		 * When the priority of a thread is changed, it may be
9787c478bd9Sstevel@tonic-gate 		 * necessary to adjust its position on a sleep queue or
9797c478bd9Sstevel@tonic-gate 		 * dispatch queue.  The function thread_change_pri accomplishes
9807c478bd9Sstevel@tonic-gate 		 * this.
9817c478bd9Sstevel@tonic-gate 		 */
9827c478bd9Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, 0)) {
9837c478bd9Sstevel@tonic-gate 			/*
9847c478bd9Sstevel@tonic-gate 			 * The thread was on a run queue.
9857c478bd9Sstevel@tonic-gate 			 */
9867c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
9877c478bd9Sstevel@tonic-gate 		} else {
9887c478bd9Sstevel@tonic-gate 			fssproc->fss_flags |= FSSBACKQ;
9897c478bd9Sstevel@tonic-gate 		}
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate  * Update priorities of all fair-sharing threads that are currently runnable
9957c478bd9Sstevel@tonic-gate  * at a user mode priority based on the number of shares and current usage.
9967c478bd9Sstevel@tonic-gate  * Called once per second via timeout which we reset here.
9977c478bd9Sstevel@tonic-gate  *
9987c478bd9Sstevel@tonic-gate  * There are several lists of fair-sharing threads broken up by a hash on the
9997c478bd9Sstevel@tonic-gate  * thread pointer.  Each list has its own lock.  This avoids blocking all
10007c478bd9Sstevel@tonic-gate  * fss_enterclass, fss_fork, and fss_exitclass operations while fss_update runs.
10017c478bd9Sstevel@tonic-gate  * fss_update traverses each list in turn.
10027c478bd9Sstevel@tonic-gate  */
10037c478bd9Sstevel@tonic-gate static void
10047c478bd9Sstevel@tonic-gate fss_update(void *arg)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	int i;
10077c478bd9Sstevel@tonic-gate 	int new_marker = -1;
10087c478bd9Sstevel@tonic-gate 	static int fss_update_marker;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Decay and update usages for all projects.
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	fss_decay_usage();
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/*
10167c478bd9Sstevel@tonic-gate 	 * Start with the fss_update_marker list, then do the rest.
10177c478bd9Sstevel@tonic-gate 	 */
10187c478bd9Sstevel@tonic-gate 	i = fss_update_marker;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * Go around all threads, set new priorities and decay
10227c478bd9Sstevel@tonic-gate 	 * per-thread CPU usages.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	do {
10257c478bd9Sstevel@tonic-gate 		/*
10267c478bd9Sstevel@tonic-gate 		 * If this is the first list after the current marker to have
10277c478bd9Sstevel@tonic-gate 		 * threads with priorities updates, advance the marker to this
10287c478bd9Sstevel@tonic-gate 		 * list for the next time fss_update runs.
10297c478bd9Sstevel@tonic-gate 		 */
10307c478bd9Sstevel@tonic-gate 		if (fss_update_list(i) &&
10317c478bd9Sstevel@tonic-gate 		    new_marker == -1 && i != fss_update_marker)
10327c478bd9Sstevel@tonic-gate 			new_marker = i;
10337c478bd9Sstevel@tonic-gate 	} while ((i = FSS_LIST_NEXT(i)) != fss_update_marker);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * Advance marker for the next fss_update call
10377c478bd9Sstevel@tonic-gate 	 */
10387c478bd9Sstevel@tonic-gate 	if (new_marker != -1)
10397c478bd9Sstevel@tonic-gate 		fss_update_marker = new_marker;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	(void) timeout(fss_update, arg, hz);
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * Updates priority for a list of threads.  Returns 1 if the priority of one
10467c478bd9Sstevel@tonic-gate  * of the threads was actually updated, 0 if none were for various reasons
10477c478bd9Sstevel@tonic-gate  * (thread is no longer in the FSS class, is not runnable, has the preemption
10487c478bd9Sstevel@tonic-gate  * control no-preempt bit set, etc.)
10497c478bd9Sstevel@tonic-gate  */
10507c478bd9Sstevel@tonic-gate static int
10517c478bd9Sstevel@tonic-gate fss_update_list(int i)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
10547c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
10557c478bd9Sstevel@tonic-gate 	fsspri_t fsspri;
10567c478bd9Sstevel@tonic-gate 	kthread_t *t;
10577c478bd9Sstevel@tonic-gate 	int updated = 0;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	mutex_enter(&fss_listlock[i]);
10607c478bd9Sstevel@tonic-gate 	for (fssproc = fss_listhead[i].fss_next; fssproc != &fss_listhead[i];
10617c478bd9Sstevel@tonic-gate 	    fssproc = fssproc->fss_next) {
10627c478bd9Sstevel@tonic-gate 		t = fssproc->fss_tp;
10637c478bd9Sstevel@tonic-gate 		/*
10647c478bd9Sstevel@tonic-gate 		 * Lock the thread and verify the state.
10657c478bd9Sstevel@tonic-gate 		 */
10667c478bd9Sstevel@tonic-gate 		thread_lock(t);
10677c478bd9Sstevel@tonic-gate 		/*
10687c478bd9Sstevel@tonic-gate 		 * Skip the thread if it is no longer in the FSS class or
10697c478bd9Sstevel@tonic-gate 		 * is running with kernel mode priority.
10707c478bd9Sstevel@tonic-gate 		 */
10717c478bd9Sstevel@tonic-gate 		if (t->t_cid != fss_cid)
10727c478bd9Sstevel@tonic-gate 			goto next;
10737c478bd9Sstevel@tonic-gate 		if ((fssproc->fss_flags & FSSKPRI) != 0)
10747c478bd9Sstevel@tonic-gate 			goto next;
1075c97ad5cdSakolb 
10767c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
10777c478bd9Sstevel@tonic-gate 		if (fssproj == NULL)
10787c478bd9Sstevel@tonic-gate 			goto next;
10797c478bd9Sstevel@tonic-gate 		if (fssproj->fssp_shares != 0) {
10807c478bd9Sstevel@tonic-gate 			/*
10817c478bd9Sstevel@tonic-gate 			 * Decay fsspri value.
10827c478bd9Sstevel@tonic-gate 			 */
10837c478bd9Sstevel@tonic-gate 			fsspri = fssproc->fss_fsspri;
10847c478bd9Sstevel@tonic-gate 			fsspri = (fsspri * fss_nice_decay[fssproc->fss_nice]) /
10857c478bd9Sstevel@tonic-gate 			    FSS_DECAY_BASE;
10867c478bd9Sstevel@tonic-gate 			fssproc->fss_fsspri = fsspri;
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		if (t->t_schedctl && schedctl_get_nopreempt(t))
10907c478bd9Sstevel@tonic-gate 			goto next;
1091c97ad5cdSakolb 		if (t->t_state != TS_RUN && t->t_state != TS_WAIT) {
10927c478bd9Sstevel@tonic-gate 			/*
10937c478bd9Sstevel@tonic-gate 			 * Make next syscall/trap call fss_trapret
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			t->t_trapret = 1;
10967c478bd9Sstevel@tonic-gate 			aston(t);
10977c478bd9Sstevel@tonic-gate 			goto next;
10987c478bd9Sstevel@tonic-gate 		}
10997c478bd9Sstevel@tonic-gate 		fss_newpri(fssproc);
11007c478bd9Sstevel@tonic-gate 		updated = 1;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		/*
11037c478bd9Sstevel@tonic-gate 		 * Only dequeue the thread if it needs to be moved; otherwise
11047c478bd9Sstevel@tonic-gate 		 * it should just round-robin here.
11057c478bd9Sstevel@tonic-gate 		 */
11067c478bd9Sstevel@tonic-gate 		if (t->t_pri != fssproc->fss_umdpri)
11077c478bd9Sstevel@tonic-gate 			fss_change_priority(t, fssproc);
11087c478bd9Sstevel@tonic-gate next:
11097c478bd9Sstevel@tonic-gate 		thread_unlock(t);
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 	mutex_exit(&fss_listlock[i]);
11127c478bd9Sstevel@tonic-gate 	return (updated);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11167c478bd9Sstevel@tonic-gate static int
11177c478bd9Sstevel@tonic-gate fss_admin(caddr_t uaddr, cred_t *reqpcredp)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	fssadmin_t fssadmin;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (copyin(uaddr, &fssadmin, sizeof (fssadmin_t)))
11227c478bd9Sstevel@tonic-gate 		return (EFAULT);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	switch (fssadmin.fss_cmd) {
11257c478bd9Sstevel@tonic-gate 	case FSS_SETADMIN:
11267c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
11277c478bd9Sstevel@tonic-gate 			return (EPERM);
11287c478bd9Sstevel@tonic-gate 		if (fssadmin.fss_quantum <= 0 || fssadmin.fss_quantum >= hz)
11297c478bd9Sstevel@tonic-gate 			return (EINVAL);
11307c478bd9Sstevel@tonic-gate 		fss_quantum = fssadmin.fss_quantum;
11317c478bd9Sstevel@tonic-gate 		break;
11327c478bd9Sstevel@tonic-gate 	case FSS_GETADMIN:
11337c478bd9Sstevel@tonic-gate 		fssadmin.fss_quantum = fss_quantum;
11347c478bd9Sstevel@tonic-gate 		if (copyout(&fssadmin, uaddr, sizeof (fssadmin_t)))
11357c478bd9Sstevel@tonic-gate 			return (EFAULT);
11367c478bd9Sstevel@tonic-gate 		break;
11377c478bd9Sstevel@tonic-gate 	default:
11387c478bd9Sstevel@tonic-gate 		return (EINVAL);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	return (0);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate static int
11447c478bd9Sstevel@tonic-gate fss_getclinfo(void *infop)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	fssinfo_t *fssinfo = (fssinfo_t *)infop;
11477c478bd9Sstevel@tonic-gate 	fssinfo->fss_maxupri = fss_maxupri;
11487c478bd9Sstevel@tonic-gate 	return (0);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate static int
11527c478bd9Sstevel@tonic-gate fss_parmsin(void *parmsp)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/*
11577c478bd9Sstevel@tonic-gate 	 * Check validity of parameters.
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_uprilim > fss_maxupri ||
11607c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim < -fss_maxupri) &&
11617c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_uprilim != FSS_NOCHANGE)
11627c478bd9Sstevel@tonic-gate 		return (EINVAL);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if ((fssparmsp->fss_upri > fss_maxupri ||
11657c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri < -fss_maxupri) &&
11667c478bd9Sstevel@tonic-gate 	    fssparmsp->fss_upri != FSS_NOCHANGE)
11677c478bd9Sstevel@tonic-gate 		return (EINVAL);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	return (0);
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11737c478bd9Sstevel@tonic-gate static int
11747c478bd9Sstevel@tonic-gate fss_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate 	return (0);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate static int
11807c478bd9Sstevel@tonic-gate fss_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
11837c478bd9Sstevel@tonic-gate 	int priflag = 0;
11847c478bd9Sstevel@tonic-gate 	int limflag = 0;
11857c478bd9Sstevel@tonic-gate 	uint_t cnt;
11867c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/*
11897c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE (-32768) is outside of the range of values for
11907c478bd9Sstevel@tonic-gate 	 * fss_uprilim and fss_upri.  If the structure fssparms_t is changed,
11917c478bd9Sstevel@tonic-gate 	 * FSS_NOCHANGE should be replaced by a flag word.
11927c478bd9Sstevel@tonic-gate 	 */
11937c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = FSS_NOCHANGE;
11947c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = FSS_NOCHANGE;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * Get the varargs parameter and check validity of parameters.
11987c478bd9Sstevel@tonic-gate 	 */
11997c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
12007c478bd9Sstevel@tonic-gate 		return (EINVAL);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
12037c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
12047c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
12057c478bd9Sstevel@tonic-gate 			if (limflag++)
12067c478bd9Sstevel@tonic-gate 				return (EINVAL);
12077c478bd9Sstevel@tonic-gate 			fssparmsp->fss_uprilim = (pri_t)vpp->pc_parm;
12087c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > fss_maxupri ||
12097c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_uprilim < -fss_maxupri)
12107c478bd9Sstevel@tonic-gate 				return (EINVAL);
12117c478bd9Sstevel@tonic-gate 			break;
12127c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
12137c478bd9Sstevel@tonic-gate 			if (priflag++)
12147c478bd9Sstevel@tonic-gate 				return (EINVAL);
12157c478bd9Sstevel@tonic-gate 			fssparmsp->fss_upri = (pri_t)vpp->pc_parm;
12167c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > fss_maxupri ||
12177c478bd9Sstevel@tonic-gate 			    fssparmsp->fss_upri < -fss_maxupri)
12187c478bd9Sstevel@tonic-gate 				return (EINVAL);
12197c478bd9Sstevel@tonic-gate 			break;
12207c478bd9Sstevel@tonic-gate 		default:
12217c478bd9Sstevel@tonic-gate 			return (EINVAL);
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
12267c478bd9Sstevel@tonic-gate 		/*
12277c478bd9Sstevel@tonic-gate 		 * Use default parameters.
12287c478bd9Sstevel@tonic-gate 		 */
12297c478bd9Sstevel@tonic-gate 		fssparmsp->fss_upri = fssparmsp->fss_uprilim = 0;
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	return (0);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate /*
12367c478bd9Sstevel@tonic-gate  * Copy all selected fair-sharing class parameters to the user.  The parameters
12377c478bd9Sstevel@tonic-gate  * are specified by a key.
12387c478bd9Sstevel@tonic-gate  */
12397c478bd9Sstevel@tonic-gate static int
12407c478bd9Sstevel@tonic-gate fss_vaparmsout(void *parmsp, pc_vaparms_t *vaparmsp)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
12437c478bd9Sstevel@tonic-gate 	int priflag = 0;
12447c478bd9Sstevel@tonic-gate 	int limflag = 0;
12457c478bd9Sstevel@tonic-gate 	uint_t cnt;
12467c478bd9Sstevel@tonic-gate 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
12517c478bd9Sstevel@tonic-gate 		return (EINVAL);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
12547c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
12557c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRILIM:
12567c478bd9Sstevel@tonic-gate 			if (limflag++)
12577c478bd9Sstevel@tonic-gate 				return (EINVAL);
12587c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_uprilim,
12597c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
12607c478bd9Sstevel@tonic-gate 				return (EFAULT);
12617c478bd9Sstevel@tonic-gate 			break;
12627c478bd9Sstevel@tonic-gate 		case FSS_KY_UPRI:
12637c478bd9Sstevel@tonic-gate 			if (priflag++)
12647c478bd9Sstevel@tonic-gate 				return (EINVAL);
12657c478bd9Sstevel@tonic-gate 			if (copyout(&fssparmsp->fss_upri,
12667c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
12677c478bd9Sstevel@tonic-gate 				return (EFAULT);
12687c478bd9Sstevel@tonic-gate 			break;
12697c478bd9Sstevel@tonic-gate 		default:
12707c478bd9Sstevel@tonic-gate 			return (EINVAL);
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	return (0);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
1277*d4204c85Sraf /*
1278*d4204c85Sraf  * Return the user mode scheduling priority range.
1279*d4204c85Sraf  */
12807c478bd9Sstevel@tonic-gate static int
12817c478bd9Sstevel@tonic-gate fss_getclpri(pcpri_t *pcprip)
12827c478bd9Sstevel@tonic-gate {
1283*d4204c85Sraf 	pcprip->pc_clpmax = fss_maxupri;
1284*d4204c85Sraf 	pcprip->pc_clpmin = -fss_maxupri;
12857c478bd9Sstevel@tonic-gate 	return (0);
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static int
12897c478bd9Sstevel@tonic-gate fss_alloc(void **p, int flag)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	void *bufp;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	if ((bufp = kmem_zalloc(sizeof (fssproc_t), flag)) == NULL) {
12947c478bd9Sstevel@tonic-gate 		return (ENOMEM);
12957c478bd9Sstevel@tonic-gate 	} else {
12967c478bd9Sstevel@tonic-gate 		*p = bufp;
12977c478bd9Sstevel@tonic-gate 		return (0);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate static void
13027c478bd9Sstevel@tonic-gate fss_free(void *bufp)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	if (bufp)
13057c478bd9Sstevel@tonic-gate 		kmem_free(bufp, sizeof (fssproc_t));
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate /*
13097c478bd9Sstevel@tonic-gate  * Thread functions
13107c478bd9Sstevel@tonic-gate  */
13117c478bd9Sstevel@tonic-gate static int
13127c478bd9Sstevel@tonic-gate fss_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
13137c478bd9Sstevel@tonic-gate     void *bufp)
13147c478bd9Sstevel@tonic-gate {
13157c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
13167c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc;
13177c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
13187c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
13197c478bd9Sstevel@tonic-gate 	static uint32_t fssexists = 0;
13207c478bd9Sstevel@tonic-gate 	fsspset_t	*fsspset;
13217c478bd9Sstevel@tonic-gate 	fssproj_t	*fssproj;
13227c478bd9Sstevel@tonic-gate 	fsszone_t	*fsszone;
13237c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
13247c478bd9Sstevel@tonic-gate 	zone_t		*zone;
13257c478bd9Sstevel@tonic-gate 	int		fsszone_allocated = 0;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	fssproc = (fssproc_t *)bufp;
13287c478bd9Sstevel@tonic-gate 	ASSERT(fssproc != NULL);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/*
13337c478bd9Sstevel@tonic-gate 	 * Only root can move threads to FSS class.
13347c478bd9Sstevel@tonic-gate 	 */
13357c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
13367c478bd9Sstevel@tonic-gate 		return (EPERM);
13377c478bd9Sstevel@tonic-gate 	/*
13387c478bd9Sstevel@tonic-gate 	 * Initialize the fssproc structure.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	fssproc->fss_umdpri = fss_maxumdpri / 2;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (fssparmsp == NULL) {
13437c478bd9Sstevel@tonic-gate 		/*
13447c478bd9Sstevel@tonic-gate 		 * Use default values.
13457c478bd9Sstevel@tonic-gate 		 */
13467c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO;
13477c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = fssproc->fss_upri = 0;
13487c478bd9Sstevel@tonic-gate 	} else {
13497c478bd9Sstevel@tonic-gate 		/*
13507c478bd9Sstevel@tonic-gate 		 * Use supplied values.
13517c478bd9Sstevel@tonic-gate 		 */
13527c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_uprilim == FSS_NOCHANGE) {
13537c478bd9Sstevel@tonic-gate 			reqfssuprilim = 0;
13547c478bd9Sstevel@tonic-gate 		} else {
13557c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_uprilim > 0 &&
13567c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
13577c478bd9Sstevel@tonic-gate 				return (EPERM);
13587c478bd9Sstevel@tonic-gate 			reqfssuprilim = fssparmsp->fss_uprilim;
13597c478bd9Sstevel@tonic-gate 		}
13607c478bd9Sstevel@tonic-gate 		if (fssparmsp->fss_upri == FSS_NOCHANGE) {
13617c478bd9Sstevel@tonic-gate 			reqfssupri = reqfssuprilim;
13627c478bd9Sstevel@tonic-gate 		} else {
13637c478bd9Sstevel@tonic-gate 			if (fssparmsp->fss_upri > 0 &&
13647c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
13657c478bd9Sstevel@tonic-gate 				return (EPERM);
13667c478bd9Sstevel@tonic-gate 			/*
13677c478bd9Sstevel@tonic-gate 			 * Set the user priority to the requested value or
13687c478bd9Sstevel@tonic-gate 			 * the upri limit, whichever is lower.
13697c478bd9Sstevel@tonic-gate 			 */
13707c478bd9Sstevel@tonic-gate 			reqfssupri = fssparmsp->fss_upri;
13717c478bd9Sstevel@tonic-gate 			if (reqfssupri > reqfssuprilim)
13727c478bd9Sstevel@tonic-gate 				reqfssupri = reqfssuprilim;
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 		fssproc->fss_uprilim = reqfssuprilim;
13757c478bd9Sstevel@tonic-gate 		fssproc->fss_upri = reqfssupri;
13767c478bd9Sstevel@tonic-gate 		fssproc->fss_nice = NZERO - (NZERO * reqfssupri) / fss_maxupri;
13777c478bd9Sstevel@tonic-gate 		if (fssproc->fss_nice > FSS_NICE_MAX)
13787c478bd9Sstevel@tonic-gate 			fssproc->fss_nice = FSS_NICE_MAX;
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
13827c478bd9Sstevel@tonic-gate 	fssproc->fss_tp = t;
1383c97ad5cdSakolb 	cpucaps_sc_init(&fssproc->fss_caps);
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/*
13867c478bd9Sstevel@tonic-gate 	 * Put a lock on our fsspset structure.
13877c478bd9Sstevel@tonic-gate 	 */
13887c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
13897c478bd9Sstevel@tonic-gate 	fsspset = fss_find_fsspset(t->t_cpupart);
13907c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
13917c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
13947c478bd9Sstevel@tonic-gate 	if ((fsszone = fss_find_fsszone(fsspset, zone)) == NULL) {
13957c478bd9Sstevel@tonic-gate 		if ((fsszone = kmem_zalloc(sizeof (fsszone_t), KM_NOSLEEP))
13967c478bd9Sstevel@tonic-gate 		    == NULL) {
13977c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
13987c478bd9Sstevel@tonic-gate 			return (ENOMEM);
13997c478bd9Sstevel@tonic-gate 		} else {
14007c478bd9Sstevel@tonic-gate 			fsszone_allocated = 1;
14017c478bd9Sstevel@tonic-gate 			fss_insert_fsszone(fsspset, zone, fsszone);
14027c478bd9Sstevel@tonic-gate 		}
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 	kpj = ttoproj(t);
14057c478bd9Sstevel@tonic-gate 	if ((fssproj = fss_find_fssproj(fsspset, kpj)) == NULL) {
14067c478bd9Sstevel@tonic-gate 		if ((fssproj = kmem_zalloc(sizeof (fssproj_t), KM_NOSLEEP))
14077c478bd9Sstevel@tonic-gate 		    == NULL) {
14087c478bd9Sstevel@tonic-gate 			if (fsszone_allocated) {
14097c478bd9Sstevel@tonic-gate 				fss_remove_fsszone(fsspset, fsszone);
14107c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
14117c478bd9Sstevel@tonic-gate 			}
14127c478bd9Sstevel@tonic-gate 			mutex_exit(&fsspset->fssps_lock);
14137c478bd9Sstevel@tonic-gate 			return (ENOMEM);
14147c478bd9Sstevel@tonic-gate 		} else {
14157c478bd9Sstevel@tonic-gate 			fss_insert_fssproj(fsspset, kpj, fsszone, fssproj);
14167c478bd9Sstevel@tonic-gate 		}
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
14197c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	/*
14227c478bd9Sstevel@tonic-gate 	 * Reset priority. Process goes to a "user mode" priority here
14237c478bd9Sstevel@tonic-gate 	 * regardless of whether or not it has slept since entering the kernel.
14247c478bd9Sstevel@tonic-gate 	 */
14257c478bd9Sstevel@tonic-gate 	thread_lock(t);
14267c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
14277c478bd9Sstevel@tonic-gate 	t->t_cid = cid;
14287c478bd9Sstevel@tonic-gate 	t->t_cldata = (void *)fssproc;
14297c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_RUNQMATCH;
14307c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
1431c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
1432c97ad5cdSakolb 	    t->t_state == TS_WAIT)
14337c478bd9Sstevel@tonic-gate 		fss_active(t);
14347c478bd9Sstevel@tonic-gate 	thread_unlock(t);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	/*
14397c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc list.
14407c478bd9Sstevel@tonic-gate 	 */
14417c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(fssproc);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	/*
14447c478bd9Sstevel@tonic-gate 	 * If this is the first fair-sharing thread to occur since boot,
14457c478bd9Sstevel@tonic-gate 	 * we set up the initial call to fss_update() here. Use an atomic
14467c478bd9Sstevel@tonic-gate 	 * compare-and-swap since that's easier and faster than a mutex
14477c478bd9Sstevel@tonic-gate 	 * (but check with an ordinary load first since most of the time
14487c478bd9Sstevel@tonic-gate 	 * this will already be done).
14497c478bd9Sstevel@tonic-gate 	 */
14507c478bd9Sstevel@tonic-gate 	if (fssexists == 0 && cas32(&fssexists, 0, 1) == 0)
14517c478bd9Sstevel@tonic-gate 		(void) timeout(fss_update, NULL, hz);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	return (0);
14547c478bd9Sstevel@tonic-gate }
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate /*
14577c478bd9Sstevel@tonic-gate  * Remove fssproc_t from the list.
14587c478bd9Sstevel@tonic-gate  */
14597c478bd9Sstevel@tonic-gate static void
14607c478bd9Sstevel@tonic-gate fss_exitclass(void *procp)
14617c478bd9Sstevel@tonic-gate {
14627c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = (fssproc_t *)procp;
14637c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
14647c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
14657c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
14667c478bd9Sstevel@tonic-gate 	kthread_t *t = fssproc->fss_tp;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * We should be either getting this thread off the deathrow or
14707c478bd9Sstevel@tonic-gate 	 * this thread has already moved to another scheduling class and
14717c478bd9Sstevel@tonic-gate 	 * we're being called with its old cldata buffer pointer.  In both
14727c478bd9Sstevel@tonic-gate 	 * cases, the content of this buffer can not be changed while we're
14737c478bd9Sstevel@tonic-gate 	 * here.
14747c478bd9Sstevel@tonic-gate 	 */
14757c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
14767c478bd9Sstevel@tonic-gate 	thread_lock(t);
14777c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid) {
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * We're being called as a result of the priocntl() system
14807c478bd9Sstevel@tonic-gate 		 * call -- someone is trying to move our thread to another
14817c478bd9Sstevel@tonic-gate 		 * scheduling class. We can't call fss_inactive() here
14827c478bd9Sstevel@tonic-gate 		 * because our thread's t_cldata pointer already points
14837c478bd9Sstevel@tonic-gate 		 * to another scheduling class specific data.
14847c478bd9Sstevel@tonic-gate 		 */
14857c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 		fssproj = FSSPROC2FSSPROJ(fssproc);
14887c478bd9Sstevel@tonic-gate 		fsspset = FSSPROJ2FSSPSET(fssproj);
14897c478bd9Sstevel@tonic-gate 		fsszone = fssproj->fssp_fsszone;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		if (fssproc->fss_runnable) {
14927c478bd9Sstevel@tonic-gate 			disp_lock_enter_high(&fsspset->fssps_displock);
14937c478bd9Sstevel@tonic-gate 			if (--fssproj->fssp_runnable == 0) {
14947c478bd9Sstevel@tonic-gate 				fsszone->fssz_shares -= fssproj->fssp_shares;
14957c478bd9Sstevel@tonic-gate 				if (--fsszone->fssz_runnable == 0)
14967c478bd9Sstevel@tonic-gate 					fsspset->fssps_shares -=
14977c478bd9Sstevel@tonic-gate 					    fsszone->fssz_rshares;
14987c478bd9Sstevel@tonic-gate 			}
14997c478bd9Sstevel@tonic-gate 			disp_lock_exit_high(&fsspset->fssps_displock);
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 		thread_unlock(t);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		mutex_enter(&fsspset->fssps_lock);
15047c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_threads == 0) {
15057c478bd9Sstevel@tonic-gate 			fss_remove_fssproj(fsspset, fssproj);
15067c478bd9Sstevel@tonic-gate 			if (fsszone->fssz_nproj == 0)
15077c478bd9Sstevel@tonic-gate 				kmem_free(fsszone, sizeof (fsszone_t));
15087c478bd9Sstevel@tonic-gate 			kmem_free(fssproj, sizeof (fssproj_t));
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	} else {
15137c478bd9Sstevel@tonic-gate 		ASSERT(t->t_state == TS_FREE);
15147c478bd9Sstevel@tonic-gate 		/*
15157c478bd9Sstevel@tonic-gate 		 * We're being called from thread_free() when our thread
15167c478bd9Sstevel@tonic-gate 		 * is removed from the deathrow. There is nothing we need
15177c478bd9Sstevel@tonic-gate 		 * do here since everything should've been done earlier
15187c478bd9Sstevel@tonic-gate 		 * in fss_exit().
15197c478bd9Sstevel@tonic-gate 		 */
15207c478bd9Sstevel@tonic-gate 		thread_unlock(t);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	FSS_LIST_DELETE(fssproc);
15257c478bd9Sstevel@tonic-gate 	fss_free(fssproc);
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15297c478bd9Sstevel@tonic-gate static int
15307c478bd9Sstevel@tonic-gate fss_canexit(kthread_t *t, cred_t *credp)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	/*
15337c478bd9Sstevel@tonic-gate 	 * A thread is allowed to exit FSS only if we have sufficient
15347c478bd9Sstevel@tonic-gate 	 * privileges.
15357c478bd9Sstevel@tonic-gate 	 */
15367c478bd9Sstevel@tonic-gate 	if (credp != NULL && secpolicy_setpriority(credp) != 0)
15377c478bd9Sstevel@tonic-gate 		return (EPERM);
15387c478bd9Sstevel@tonic-gate 	else
15397c478bd9Sstevel@tonic-gate 		return (0);
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate /*
15437c478bd9Sstevel@tonic-gate  * Initialize fair-share class specific proc structure for a child.
15447c478bd9Sstevel@tonic-gate  */
15457c478bd9Sstevel@tonic-gate static int
15467c478bd9Sstevel@tonic-gate fss_fork(kthread_t *pt, kthread_t *ct, void *bufp)
15477c478bd9Sstevel@tonic-gate {
15487c478bd9Sstevel@tonic-gate 	fssproc_t *pfssproc;	/* ptr to parent's fssproc structure	*/
15497c478bd9Sstevel@tonic-gate 	fssproc_t *cfssproc;	/* ptr to child's fssproc structure	*/
15507c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
15517c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(pt)->p_lock));
15547c478bd9Sstevel@tonic-gate 	ASSERT(ct->t_state == TS_STOPPED);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	cfssproc = (fssproc_t *)bufp;
15577c478bd9Sstevel@tonic-gate 	ASSERT(cfssproc != NULL);
15587c478bd9Sstevel@tonic-gate 	bzero(cfssproc, sizeof (fssproc_t));
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	thread_lock(pt);
15617c478bd9Sstevel@tonic-gate 	pfssproc = FSSPROC(pt);
15627c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(pfssproc);
15637c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
15647c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Initialize child's fssproc structure.
15697c478bd9Sstevel@tonic-gate 	 */
15707c478bd9Sstevel@tonic-gate 	thread_lock(pt);
15717c478bd9Sstevel@tonic-gate 	ASSERT(FSSPROJ(pt) == fssproj);
15727c478bd9Sstevel@tonic-gate 	cfssproc->fss_proj = fssproj;
15737c478bd9Sstevel@tonic-gate 	cfssproc->fss_timeleft = fss_quantum;
15747c478bd9Sstevel@tonic-gate 	cfssproc->fss_umdpri = pfssproc->fss_umdpri;
15757c478bd9Sstevel@tonic-gate 	cfssproc->fss_fsspri = 0;
15767c478bd9Sstevel@tonic-gate 	cfssproc->fss_uprilim = pfssproc->fss_uprilim;
15777c478bd9Sstevel@tonic-gate 	cfssproc->fss_upri = pfssproc->fss_upri;
15787c478bd9Sstevel@tonic-gate 	cfssproc->fss_tp = ct;
15797c478bd9Sstevel@tonic-gate 	cfssproc->fss_nice = pfssproc->fss_nice;
1580c97ad5cdSakolb 	cpucaps_sc_init(&cfssproc->fss_caps);
1581c97ad5cdSakolb 
15829ac8606fSraf 	cfssproc->fss_flags =
15839ac8606fSraf 	    pfssproc->fss_flags & ~(FSSKPRI | FSSBACKQ | FSSRESTORE);
15847c478bd9Sstevel@tonic-gate 	ct->t_cldata = (void *)cfssproc;
15857c478bd9Sstevel@tonic-gate 	ct->t_schedflag |= TS_RUNQMATCH;
15867c478bd9Sstevel@tonic-gate 	thread_unlock(pt);
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	fssproj->fssp_threads++;
15897c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	/*
15927c478bd9Sstevel@tonic-gate 	 * Link new structure into fssproc hash table.
15937c478bd9Sstevel@tonic-gate 	 */
15947c478bd9Sstevel@tonic-gate 	FSS_LIST_INSERT(cfssproc);
15957c478bd9Sstevel@tonic-gate 	return (0);
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate /*
15997c478bd9Sstevel@tonic-gate  * Child is placed at back of dispatcher queue and parent gives up processor
16007c478bd9Sstevel@tonic-gate  * so that the child runs first after the fork. This allows the child
16017c478bd9Sstevel@tonic-gate  * immediately execing to break the multiple use of copy on write pages with no
16027c478bd9Sstevel@tonic-gate  * disk home. The parent will get to steal them back rather than uselessly
16037c478bd9Sstevel@tonic-gate  * copying them.
16047c478bd9Sstevel@tonic-gate  */
16057c478bd9Sstevel@tonic-gate static void
16067c478bd9Sstevel@tonic-gate fss_forkret(kthread_t *t, kthread_t *ct)
16077c478bd9Sstevel@tonic-gate {
16087c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
16097c478bd9Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
16107c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
16137c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure the
16177c478bd9Sstevel@tonic-gate 	 * process does not disappear before we set it running.
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
16207c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
16217c478bd9Sstevel@tonic-gate 	continuelwps(cp);
16227c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
16257c478bd9Sstevel@tonic-gate 	continuelwps(pp);
16267c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	thread_lock(t);
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
16317c478bd9Sstevel@tonic-gate 	fss_newpri(fssproc);
16327c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
16337c478bd9Sstevel@tonic-gate 	t->t_pri = fssproc->fss_umdpri;
16347c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
16357c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSKPRI;
16367c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	/*
16397c478bd9Sstevel@tonic-gate 	 * We don't want to call fss_setrun(t) here because it may call
16407c478bd9Sstevel@tonic-gate 	 * fss_active, which we don't need.
16417c478bd9Sstevel@tonic-gate 	 */
16427c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	if (t->t_disp_time != lbolt)
16457c478bd9Sstevel@tonic-gate 		setbackdq(t);
16467c478bd9Sstevel@tonic-gate 	else
16477c478bd9Sstevel@tonic-gate 		setfrontdq(t);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	thread_unlock(t);
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	swtch();
16527c478bd9Sstevel@tonic-gate }
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate /*
16557c478bd9Sstevel@tonic-gate  * Get the fair-sharing parameters of the thread pointed to by fssprocp into
16567c478bd9Sstevel@tonic-gate  * the buffer pointed by fssparmsp.
16577c478bd9Sstevel@tonic-gate  */
16587c478bd9Sstevel@tonic-gate static void
16597c478bd9Sstevel@tonic-gate fss_parmsget(kthread_t *t, void *parmsp)
16607c478bd9Sstevel@tonic-gate {
16617c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
16627c478bd9Sstevel@tonic-gate 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	fssparmsp->fss_uprilim = fssproc->fss_uprilim;
16657c478bd9Sstevel@tonic-gate 	fssparmsp->fss_upri = fssproc->fss_upri;
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16697c478bd9Sstevel@tonic-gate static int
16707c478bd9Sstevel@tonic-gate fss_parmsset(kthread_t *t, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
16717c478bd9Sstevel@tonic-gate {
16727c478bd9Sstevel@tonic-gate 	char		nice;
16737c478bd9Sstevel@tonic-gate 	pri_t		reqfssuprilim;
16747c478bd9Sstevel@tonic-gate 	pri_t		reqfssupri;
16757c478bd9Sstevel@tonic-gate 	fssproc_t	*fssproc = FSSPROC(t);
16767c478bd9Sstevel@tonic-gate 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_uprilim == FSS_NOCHANGE)
16817c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssproc->fss_uprilim;
16827c478bd9Sstevel@tonic-gate 	else
16837c478bd9Sstevel@tonic-gate 		reqfssuprilim = fssparmsp->fss_uprilim;
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	if (fssparmsp->fss_upri == FSS_NOCHANGE)
16867c478bd9Sstevel@tonic-gate 		reqfssupri = fssproc->fss_upri;
16877c478bd9Sstevel@tonic-gate 	else
16887c478bd9Sstevel@tonic-gate 		reqfssupri = fssparmsp->fss_upri;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	/*
16917c478bd9Sstevel@tonic-gate 	 * Make sure the user priority doesn't exceed the upri limit.
16927c478bd9Sstevel@tonic-gate 	 */
16937c478bd9Sstevel@tonic-gate 	if (reqfssupri > reqfssuprilim)
16947c478bd9Sstevel@tonic-gate 		reqfssupri = reqfssuprilim;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	/*
16977c478bd9Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code for all classes
16987c478bd9Sstevel@tonic-gate 	 * require that a thread attempting to change the scheduling parameters
16997c478bd9Sstevel@tonic-gate 	 * of a target thread be privileged or have a real or effective UID
17007c478bd9Sstevel@tonic-gate 	 * matching that of the target thread. We are not called unless these
17017c478bd9Sstevel@tonic-gate 	 * basic permission checks have already passed. The fair-sharing class
17027c478bd9Sstevel@tonic-gate 	 * requires in addition that the calling thread be privileged if it
17037c478bd9Sstevel@tonic-gate 	 * is attempting to raise the upri limit above its current value.
17047c478bd9Sstevel@tonic-gate 	 * This may have been checked previously but if our caller passed us
17057c478bd9Sstevel@tonic-gate 	 * a non-NULL credential pointer we assume it hasn't and we check it
17067c478bd9Sstevel@tonic-gate 	 * here.
17077c478bd9Sstevel@tonic-gate 	 */
17087c478bd9Sstevel@tonic-gate 	if ((reqpcredp != NULL) &&
17097c478bd9Sstevel@tonic-gate 	    (reqfssuprilim > fssproc->fss_uprilim) &&
17107c478bd9Sstevel@tonic-gate 	    secpolicy_setpriority(reqpcredp) != 0)
17117c478bd9Sstevel@tonic-gate 		return (EPERM);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	/*
17147c478bd9Sstevel@tonic-gate 	 * Set fss_nice to the nice value corresponding to the user priority we
17157c478bd9Sstevel@tonic-gate 	 * are setting.  Note that setting the nice field of the parameter
17167c478bd9Sstevel@tonic-gate 	 * struct won't affect upri or nice.
17177c478bd9Sstevel@tonic-gate 	 */
17187c478bd9Sstevel@tonic-gate 	nice = NZERO - (reqfssupri * NZERO) / fss_maxupri;
17197c478bd9Sstevel@tonic-gate 	if (nice > FSS_NICE_MAX)
17207c478bd9Sstevel@tonic-gate 		nice = FSS_NICE_MAX;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	thread_lock(t);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	fssproc->fss_uprilim = reqfssuprilim;
17257c478bd9Sstevel@tonic-gate 	fssproc->fss_upri = reqfssupri;
17267c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = nice;
17277c478bd9Sstevel@tonic-gate 	fss_newpri(fssproc);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) != 0) {
17307c478bd9Sstevel@tonic-gate 		thread_unlock(t);
17317c478bd9Sstevel@tonic-gate 		return (0);
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	fss_change_priority(t, fssproc);
17357c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17367c478bd9Sstevel@tonic-gate 	return (0);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate  * The thread is being stopped.
17427c478bd9Sstevel@tonic-gate  */
17437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17447c478bd9Sstevel@tonic-gate static void
17457c478bd9Sstevel@tonic-gate fss_stop(kthread_t *t, int why, int what)
17467c478bd9Sstevel@tonic-gate {
17477c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
17487c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	fss_inactive(t);
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate /*
17547c478bd9Sstevel@tonic-gate  * The current thread is exiting, do necessary adjustments to its project
17557c478bd9Sstevel@tonic-gate  */
17567c478bd9Sstevel@tonic-gate static void
17577c478bd9Sstevel@tonic-gate fss_exit(kthread_t *t)
17587c478bd9Sstevel@tonic-gate {
17597c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
17607c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
17617c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
17627c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone;
17637c478bd9Sstevel@tonic-gate 	int free = 0;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	/*
17667c478bd9Sstevel@tonic-gate 	 * Thread t here is either a current thread (in which case we hold
17677c478bd9Sstevel@tonic-gate 	 * its process' p_lock), or a thread being destroyed by forklwp_fail(),
17687c478bd9Sstevel@tonic-gate 	 * in which case we hold pidlock and thread is no longer on the
17697c478bd9Sstevel@tonic-gate 	 * thread list.
17707c478bd9Sstevel@tonic-gate 	 */
17717c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock) || MUTEX_HELD(&pidlock));
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
17747c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
17757c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj);
17767c478bd9Sstevel@tonic-gate 	fsszone = fssproj->fssp_fsszone;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
17797c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	thread_lock(t);
17827c478bd9Sstevel@tonic-gate 	disp_lock_enter_high(&fsspset->fssps_displock);
17837c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
17847c478bd9Sstevel@tonic-gate 		if (--fssproj->fssp_runnable == 0) {
17857c478bd9Sstevel@tonic-gate 			fsszone->fssz_shares -= fssproj->fssp_shares;
17867c478bd9Sstevel@tonic-gate 			if (--fsszone->fssz_runnable == 0)
17877c478bd9Sstevel@tonic-gate 				fsspset->fssps_shares -= fsszone->fssz_rshares;
17887c478bd9Sstevel@tonic-gate 		}
17897c478bd9Sstevel@tonic-gate 		ASSERT(fssproc->fss_runnable == 1);
17907c478bd9Sstevel@tonic-gate 		fssproc->fss_runnable = 0;
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 	if (--fssproj->fssp_threads == 0) {
17937c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj);
17947c478bd9Sstevel@tonic-gate 		free = 1;
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(&fsspset->fssps_displock);
17977c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = NULL;	/* mark this thread as already exited */
17987c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if (free) {
18017c478bd9Sstevel@tonic-gate 		if (fsszone->fssz_nproj == 0)
18027c478bd9Sstevel@tonic-gate 			kmem_free(fsszone, sizeof (fsszone_t));
18037c478bd9Sstevel@tonic-gate 		kmem_free(fssproj, sizeof (fssproj_t));
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
18067c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
1807c97ad5cdSakolb 
18088c34bbb7Sakolb 	/*
18098c34bbb7Sakolb 	 * A thread could be exiting in between clock ticks, so we need to
18108c34bbb7Sakolb 	 * calculate how much CPU time it used since it was charged last time.
18118c34bbb7Sakolb 	 *
18128c34bbb7Sakolb 	 * CPU caps are not enforced on exiting processes - it is usually
18138c34bbb7Sakolb 	 * desirable to exit as soon as possible to free resources.
18148c34bbb7Sakolb 	 */
1815c97ad5cdSakolb 	if (CPUCAPS_ON()) {
1816c97ad5cdSakolb 		thread_lock(t);
1817c97ad5cdSakolb 		fssproc = FSSPROC(t);
1818c97ad5cdSakolb 		(void) cpucaps_charge(t, &fssproc->fss_caps,
1819c97ad5cdSakolb 		    CPUCAPS_CHARGE_ONLY);
1820c97ad5cdSakolb 		thread_unlock(t);
1821c97ad5cdSakolb 	}
18227c478bd9Sstevel@tonic-gate }
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate static void
18257c478bd9Sstevel@tonic-gate fss_nullsys()
18267c478bd9Sstevel@tonic-gate {
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate /*
18307c478bd9Sstevel@tonic-gate  * fss_swapin() returns -1 if the thread is loaded or is not eligible to be
18317c478bd9Sstevel@tonic-gate  * swapped in. Otherwise, it returns the thread's effective priority based
18327c478bd9Sstevel@tonic-gate  * on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
18337c478bd9Sstevel@tonic-gate  */
18347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18357c478bd9Sstevel@tonic-gate static pri_t
18367c478bd9Sstevel@tonic-gate fss_swapin(kthread_t *t, int flags)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
18397c478bd9Sstevel@tonic-gate 	long epri = -1;
18407c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
18457c478bd9Sstevel@tonic-gate 		time_t swapout_time;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 		swapout_time = (lbolt - t->t_stime) / hz;
18487c478bd9Sstevel@tonic-gate 		if (INHERITED(t) || (fssproc->fss_flags & FSSKPRI)) {
18497c478bd9Sstevel@tonic-gate 			epri = (long)DISP_PRIO(t) + swapout_time;
18507c478bd9Sstevel@tonic-gate 		} else {
18517c478bd9Sstevel@tonic-gate 			/*
18527c478bd9Sstevel@tonic-gate 			 * Threads which have been out for a long time,
18537c478bd9Sstevel@tonic-gate 			 * have high user mode priority and are associated
18547c478bd9Sstevel@tonic-gate 			 * with a small address space are more deserving.
18557c478bd9Sstevel@tonic-gate 			 */
18567c478bd9Sstevel@tonic-gate 			epri = fssproc->fss_umdpri;
18577c478bd9Sstevel@tonic-gate 			ASSERT(epri >= 0 && epri <= fss_maxumdpri);
18587c478bd9Sstevel@tonic-gate 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 		/*
18617c478bd9Sstevel@tonic-gate 		 * Scale epri so that SHRT_MAX / 2 represents zero priority.
18627c478bd9Sstevel@tonic-gate 		 */
18637c478bd9Sstevel@tonic-gate 		epri += SHRT_MAX / 2;
18647c478bd9Sstevel@tonic-gate 		if (epri < 0)
18657c478bd9Sstevel@tonic-gate 			epri = 0;
18667c478bd9Sstevel@tonic-gate 		else if (epri > SHRT_MAX)
18677c478bd9Sstevel@tonic-gate 			epri = SHRT_MAX;
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
18707c478bd9Sstevel@tonic-gate }
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
18747c478bd9Sstevel@tonic-gate  * be swapped out. Otherwise, it returns the thread's effective priority
18757c478bd9Sstevel@tonic-gate  * based on if the swapper is in softswap or hardswap mode.
18767c478bd9Sstevel@tonic-gate  */
18777c478bd9Sstevel@tonic-gate static pri_t
18787c478bd9Sstevel@tonic-gate fss_swapout(kthread_t *t, int flags)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
18817c478bd9Sstevel@tonic-gate 	long epri = -1;
18827c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
18837c478bd9Sstevel@tonic-gate 	time_t swapin_time;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	if (INHERITED(t) ||
18887c478bd9Sstevel@tonic-gate 	    (fssproc->fss_flags & FSSKPRI) ||
18897c478bd9Sstevel@tonic-gate 	    (t->t_proc_flag & TP_LWPEXIT) ||
1890*d4204c85Sraf 	    (t->t_state & (TS_ZOMB|TS_FREE|TS_STOPPED|TS_ONPROC|TS_WAIT)) ||
18917c478bd9Sstevel@tonic-gate 	    !(t->t_schedflag & TS_LOAD) ||
18927c478bd9Sstevel@tonic-gate 	    !(SWAP_OK(t)))
18937c478bd9Sstevel@tonic-gate 		return (-1);
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	swapin_time = (lbolt - t->t_stime) / hz;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	if (flags == SOFTSWAP) {
19007c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
19017c478bd9Sstevel@tonic-gate 			epri = 0;
19027c478bd9Sstevel@tonic-gate 		} else {
19037c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 	} else {
19067c478bd9Sstevel@tonic-gate 		pri_t pri;
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		if ((t->t_state == TS_SLEEP && swapin_time > fss_minslp) ||
19097c478bd9Sstevel@tonic-gate 		    (t->t_state == TS_RUN && swapin_time > fss_minrun)) {
19107c478bd9Sstevel@tonic-gate 			pri = fss_maxumdpri;
19117c478bd9Sstevel@tonic-gate 			epri = swapin_time -
19127c478bd9Sstevel@tonic-gate 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
19137c478bd9Sstevel@tonic-gate 		} else {
19147c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
19157c478bd9Sstevel@tonic-gate 		}
19167c478bd9Sstevel@tonic-gate 	}
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	/*
19197c478bd9Sstevel@tonic-gate 	 * Scale epri so that SHRT_MAX / 2 represents zero priority.
19207c478bd9Sstevel@tonic-gate 	 */
19217c478bd9Sstevel@tonic-gate 	epri += SHRT_MAX / 2;
19227c478bd9Sstevel@tonic-gate 	if (epri < 0)
19237c478bd9Sstevel@tonic-gate 		epri = 0;
19247c478bd9Sstevel@tonic-gate 	else if (epri > SHRT_MAX)
19257c478bd9Sstevel@tonic-gate 		epri = SHRT_MAX;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate /*
19317c478bd9Sstevel@tonic-gate  * If thread is currently at a kernel mode priority (has slept) and is
19327c478bd9Sstevel@tonic-gate  * returning to the userland we assign it the appropriate user mode priority
19337c478bd9Sstevel@tonic-gate  * and time quantum here.  If we're lowering the thread's priority below that
19347c478bd9Sstevel@tonic-gate  * of other runnable threads then we will set runrun via cpu_surrender() to
19357c478bd9Sstevel@tonic-gate  * cause preemption.
19367c478bd9Sstevel@tonic-gate  */
19377c478bd9Sstevel@tonic-gate static void
19387c478bd9Sstevel@tonic-gate fss_trapret(kthread_t *t)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
19417c478bd9Sstevel@tonic-gate 	cpu_t *cp = CPU;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
19447c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
19457c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_dispthread == t);
19467c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	t->t_kpri_req = 0;
19497c478bd9Sstevel@tonic-gate 	if (fssproc->fss_flags & FSSKPRI) {
19507c478bd9Sstevel@tonic-gate 		/*
19517c478bd9Sstevel@tonic-gate 		 * If thread has blocked in the kernel
19527c478bd9Sstevel@tonic-gate 		 */
19537c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
19547c478bd9Sstevel@tonic-gate 		cp->cpu_dispatch_pri = DISP_PRIO(t);
19557c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
19567c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSKPRI;
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t))
19597c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	/*
19637c478bd9Sstevel@tonic-gate 	 * Swapout lwp if the swapper is waiting for this thread to reach
19647c478bd9Sstevel@tonic-gate 	 * a safe point.
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 	if (t->t_schedflag & TS_SWAPENQ) {
19677c478bd9Sstevel@tonic-gate 		thread_unlock(t);
19687c478bd9Sstevel@tonic-gate 		swapout_lwp(ttolwp(t));
19697c478bd9Sstevel@tonic-gate 		thread_lock(t);
19707c478bd9Sstevel@tonic-gate 	}
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate /*
19747c478bd9Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location on dispatcher queue.
19757c478bd9Sstevel@tonic-gate  * This is called with the current thread in TS_ONPROC and locked.
19767c478bd9Sstevel@tonic-gate  */
19777c478bd9Sstevel@tonic-gate static void
19787c478bd9Sstevel@tonic-gate fss_preempt(kthread_t *t)
19797c478bd9Sstevel@tonic-gate {
19807c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
19817c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
19827c478bd9Sstevel@tonic-gate 	uint_t flags;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
19857c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(curthread));
19867c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/*
19897c478bd9Sstevel@tonic-gate 	 * If preempted in the kernel, make sure the thread has a kernel
19907c478bd9Sstevel@tonic-gate 	 * priority if needed.
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	lwp = curthread->t_lwp;
19937c478bd9Sstevel@tonic-gate 	if (!(fssproc->fss_flags & FSSKPRI) && lwp != NULL && t->t_kpri_req) {
19947c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
19957c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
19967c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
19977c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
19987c478bd9Sstevel@tonic-gate 		aston(t);
19997c478bd9Sstevel@tonic-gate 	}
2000c97ad5cdSakolb 
2001c97ad5cdSakolb 	/*
2002c97ad5cdSakolb 	 * This thread may be placed on wait queue by CPU Caps. In this case we
2003c97ad5cdSakolb 	 * do not need to do anything until it is removed from the wait queue.
2004c97ad5cdSakolb 	 * Do not enforce CPU caps on threads running at a kernel priority
2005c97ad5cdSakolb 	 */
2006c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2007c97ad5cdSakolb 		(void) cpucaps_charge(t, &fssproc->fss_caps,
20088c34bbb7Sakolb 		    CPUCAPS_CHARGE_ENFORCE);
2009c97ad5cdSakolb 
2010c97ad5cdSakolb 		if (!(fssproc->fss_flags & FSSKPRI) && CPUCAPS_ENFORCE(t))
2011c97ad5cdSakolb 			return;
2012c97ad5cdSakolb 	}
2013c97ad5cdSakolb 
20147c478bd9Sstevel@tonic-gate 	/*
20157c478bd9Sstevel@tonic-gate 	 * If preempted in user-land mark the thread as swappable because it
20167c478bd9Sstevel@tonic-gate 	 * cannot be holding any kernel locks.
20177c478bd9Sstevel@tonic-gate 	 */
20187c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
20197c478bd9Sstevel@tonic-gate 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
20207c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	/*
20237c478bd9Sstevel@tonic-gate 	 * Check to see if we're doing "preemption control" here.  If
20247c478bd9Sstevel@tonic-gate 	 * we are, and if the user has requested that this thread not
20257c478bd9Sstevel@tonic-gate 	 * be preempted, and if preemptions haven't been put off for
20267c478bd9Sstevel@tonic-gate 	 * too long, let the preemption happen here but try to make
20279ac8606fSraf 	 * sure the thread is rescheduled as soon as possible.  We do
20287c478bd9Sstevel@tonic-gate 	 * this by putting it on the front of the highest priority run
20297c478bd9Sstevel@tonic-gate 	 * queue in the FSS class.  If the preemption has been put off
20307c478bd9Sstevel@tonic-gate 	 * for too long, clear the "nopreempt" bit and let the thread
20317c478bd9Sstevel@tonic-gate 	 * be preempted.
20327c478bd9Sstevel@tonic-gate 	 */
20337c478bd9Sstevel@tonic-gate 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
20347c478bd9Sstevel@tonic-gate 		if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
20357c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
20367c478bd9Sstevel@tonic-gate 			if (!(fssproc->fss_flags & FSSKPRI)) {
20379ac8606fSraf 				/*
20389ac8606fSraf 				 * If not already remembered, remember current
20399ac8606fSraf 				 * priority for restoration in fss_yield().
20409ac8606fSraf 				 */
20419ac8606fSraf 				if (!(fssproc->fss_flags & FSSRESTORE)) {
20429ac8606fSraf 					fssproc->fss_scpri = t->t_pri;
20439ac8606fSraf 					fssproc->fss_flags |= FSSRESTORE;
20449ac8606fSraf 				}
20457c478bd9Sstevel@tonic-gate 				THREAD_CHANGE_PRI(t, fss_maxumdpri);
20467c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_DONT_SWAP;
20477c478bd9Sstevel@tonic-gate 			}
20489ac8606fSraf 			schedctl_set_yield(t, 1);
20497c478bd9Sstevel@tonic-gate 			setfrontdq(t);
20507c478bd9Sstevel@tonic-gate 			return;
20517c478bd9Sstevel@tonic-gate 		} else {
20529ac8606fSraf 			if (fssproc->fss_flags & FSSRESTORE) {
20539ac8606fSraf 				THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
20549ac8606fSraf 				fssproc->fss_flags &= ~FSSRESTORE;
20559ac8606fSraf 			}
20567c478bd9Sstevel@tonic-gate 			schedctl_set_nopreempt(t, 0);
20577c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
20587c478bd9Sstevel@tonic-gate 			/*
20597c478bd9Sstevel@tonic-gate 			 * Fall through and be preempted below.
20607c478bd9Sstevel@tonic-gate 			 */
20617c478bd9Sstevel@tonic-gate 		}
20627c478bd9Sstevel@tonic-gate 	}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	flags = fssproc->fss_flags & (FSSBACKQ | FSSKPRI);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	if (flags == FSSBACKQ) {
20677c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
20687c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
20697c478bd9Sstevel@tonic-gate 		setbackdq(t);
20707c478bd9Sstevel@tonic-gate 	} else if (flags == (FSSBACKQ | FSSKPRI)) {
20717c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSBACKQ;
20727c478bd9Sstevel@tonic-gate 		setbackdq(t);
20737c478bd9Sstevel@tonic-gate 	} else {
20747c478bd9Sstevel@tonic-gate 		setfrontdq(t);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate }
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate /*
20797c478bd9Sstevel@tonic-gate  * Called when a thread is waking up and is to be placed on the run queue.
20807c478bd9Sstevel@tonic-gate  */
20817c478bd9Sstevel@tonic-gate static void
20827c478bd9Sstevel@tonic-gate fss_setrun(kthread_t *t)
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP || t->t_state == TS_STOPPED)
20897c478bd9Sstevel@tonic-gate 		fss_active(t);
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	fssproc->fss_timeleft = fss_quantum;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
20947c478bd9Sstevel@tonic-gate 	/*
20957c478bd9Sstevel@tonic-gate 	 * If previously were running at the kernel priority then keep that
20967c478bd9Sstevel@tonic-gate 	 * priority and the fss_timeleft doesn't matter.
20977c478bd9Sstevel@tonic-gate 	 */
20987c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) == 0)
20997c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	if (t->t_disp_time != lbolt)
21027c478bd9Sstevel@tonic-gate 		setbackdq(t);
21037c478bd9Sstevel@tonic-gate 	else
21047c478bd9Sstevel@tonic-gate 		setfrontdq(t);
21057c478bd9Sstevel@tonic-gate }
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate /*
21087c478bd9Sstevel@tonic-gate  * Prepare thread for sleep. We reset the thread priority so it will run at the
21097c478bd9Sstevel@tonic-gate  * kernel priority level when it wakes up.
21107c478bd9Sstevel@tonic-gate  */
21117c478bd9Sstevel@tonic-gate static void
21127c478bd9Sstevel@tonic-gate fss_sleep(kthread_t *t)
21137c478bd9Sstevel@tonic-gate {
21147c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
21177c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
2120c97ad5cdSakolb 
2121c97ad5cdSakolb 	/*
2122c97ad5cdSakolb 	 * Account for time spent on CPU before going to sleep.
2123c97ad5cdSakolb 	 */
21248c34bbb7Sakolb 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2125c97ad5cdSakolb 
21267c478bd9Sstevel@tonic-gate 	fss_inactive(t);
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	/*
21297c478bd9Sstevel@tonic-gate 	 * Assign a system priority to the thread and arrange for it to be
21307c478bd9Sstevel@tonic-gate 	 * retained when the thread is next placed on the run queue (i.e.,
21317c478bd9Sstevel@tonic-gate 	 * when it wakes up) instead of being given a new pri.  Also arrange
21327c478bd9Sstevel@tonic-gate 	 * for trapret processing as the thread leaves the system call so it
21337c478bd9Sstevel@tonic-gate 	 * will drop back to normal priority range.
21347c478bd9Sstevel@tonic-gate 	 */
21357c478bd9Sstevel@tonic-gate 	if (t->t_kpri_req) {
21367c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
21377c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
21387c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
21397c478bd9Sstevel@tonic-gate 		aston(t);
21407c478bd9Sstevel@tonic-gate 	} else if (fssproc->fss_flags & FSSKPRI) {
21417c478bd9Sstevel@tonic-gate 		/*
21427c478bd9Sstevel@tonic-gate 		 * The thread has done a THREAD_KPRI_REQUEST(), slept, then
21437c478bd9Sstevel@tonic-gate 		 * done THREAD_KPRI_RELEASE() (so no t_kpri_req is 0 again),
21447c478bd9Sstevel@tonic-gate 		 * then slept again all without finishing the current system
21457c478bd9Sstevel@tonic-gate 		 * call so trapret won't have cleared FSSKPRI
21467c478bd9Sstevel@tonic-gate 		 */
21477c478bd9Sstevel@tonic-gate 		fssproc->fss_flags &= ~FSSKPRI;
21487c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
21497c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(curthread))
21507c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
21517c478bd9Sstevel@tonic-gate 	}
21527c478bd9Sstevel@tonic-gate 	t->t_stime = lbolt;	/* time stamp for the swapper */
21537c478bd9Sstevel@tonic-gate }
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate /*
21567c478bd9Sstevel@tonic-gate  * A tick interrupt has ocurrend on a running thread. Check to see if our
21577c478bd9Sstevel@tonic-gate  * time slice has expired.  We must also clear the TS_DONT_SWAP flag in
21587c478bd9Sstevel@tonic-gate  * t_schedflag if the thread is eligible to be swapped out.
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate static void
21617c478bd9Sstevel@tonic-gate fss_tick(kthread_t *t)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
21647c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj;
21657c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
2166c97ad5cdSakolb 	boolean_t call_cpu_surrender = B_FALSE;
2167c97ad5cdSakolb 	boolean_t cpucaps_enforce = B_FALSE;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	/*
21727c478bd9Sstevel@tonic-gate 	 * It's safe to access fsspset and fssproj structures because we're
21737c478bd9Sstevel@tonic-gate 	 * holding our p_lock here.
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 	thread_lock(t);
21767c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
21777c478bd9Sstevel@tonic-gate 	fssproj = FSSPROC2FSSPROJ(fssproc);
21787c478bd9Sstevel@tonic-gate 	if (fssproj != NULL) {
21797c478bd9Sstevel@tonic-gate 		fsspset_t *fsspset = FSSPROJ2FSSPSET(fssproj);
21807c478bd9Sstevel@tonic-gate 		disp_lock_enter_high(&fsspset->fssps_displock);
21817c478bd9Sstevel@tonic-gate 		fssproj->fssp_ticks += fss_nice_tick[fssproc->fss_nice];
21827c478bd9Sstevel@tonic-gate 		fssproc->fss_ticks++;
21837c478bd9Sstevel@tonic-gate 		disp_lock_exit_high(&fsspset->fssps_displock);
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	/*
2187c97ad5cdSakolb 	 * Keep track of thread's project CPU usage.  Note that projects
2188c97ad5cdSakolb 	 * get charged even when threads are running in the kernel.
2189c97ad5cdSakolb 	 * Do not surrender CPU if running in the SYS class.
2190c97ad5cdSakolb 	 */
2191c97ad5cdSakolb 	if (CPUCAPS_ON()) {
2192c97ad5cdSakolb 		cpucaps_enforce = cpucaps_charge(t,
2193c97ad5cdSakolb 		    &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE) &&
2194c97ad5cdSakolb 		    !(fssproc->fss_flags & FSSKPRI);
2195c97ad5cdSakolb 	}
2196c97ad5cdSakolb 
2197c97ad5cdSakolb 	/*
21987c478bd9Sstevel@tonic-gate 	 * A thread's execution time for threads running in the SYS class
21997c478bd9Sstevel@tonic-gate 	 * is not tracked.
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	if ((fssproc->fss_flags & FSSKPRI) == 0) {
22027c478bd9Sstevel@tonic-gate 		/*
22037c478bd9Sstevel@tonic-gate 		 * If thread is not in kernel mode, decrement its fss_timeleft
22047c478bd9Sstevel@tonic-gate 		 */
22057c478bd9Sstevel@tonic-gate 		if (--fssproc->fss_timeleft <= 0) {
22067c478bd9Sstevel@tonic-gate 			pri_t new_pri;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 			/*
22097c478bd9Sstevel@tonic-gate 			 * If we're doing preemption control and trying to
22107c478bd9Sstevel@tonic-gate 			 * avoid preempting this thread, just note that the
22117c478bd9Sstevel@tonic-gate 			 * thread should yield soon and let it keep running
22127c478bd9Sstevel@tonic-gate 			 * (unless it's been a while).
22137c478bd9Sstevel@tonic-gate 			 */
22147c478bd9Sstevel@tonic-gate 			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
22157c478bd9Sstevel@tonic-gate 				if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
22167c478bd9Sstevel@tonic-gate 					DTRACE_SCHED1(schedctl__nopreempt,
22177c478bd9Sstevel@tonic-gate 					    kthread_t *, t);
22187c478bd9Sstevel@tonic-gate 					schedctl_set_yield(t, 1);
22197c478bd9Sstevel@tonic-gate 					thread_unlock_nopreempt(t);
22207c478bd9Sstevel@tonic-gate 					return;
22217c478bd9Sstevel@tonic-gate 				}
22227c478bd9Sstevel@tonic-gate 			}
22239ac8606fSraf 			fssproc->fss_flags &= ~FSSRESTORE;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 			fss_newpri(fssproc);
22267c478bd9Sstevel@tonic-gate 			new_pri = fssproc->fss_umdpri;
22277c478bd9Sstevel@tonic-gate 			ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 			/*
22307c478bd9Sstevel@tonic-gate 			 * When the priority of a thread is changed, it may
22317c478bd9Sstevel@tonic-gate 			 * be necessary to adjust its position on a sleep queue
22327c478bd9Sstevel@tonic-gate 			 * or dispatch queue. The function thread_change_pri
22337c478bd9Sstevel@tonic-gate 			 * accomplishes this.
22347c478bd9Sstevel@tonic-gate 			 */
22357c478bd9Sstevel@tonic-gate 			if (thread_change_pri(t, new_pri, 0)) {
22367c478bd9Sstevel@tonic-gate 				if ((t->t_schedflag & TS_LOAD) &&
22377c478bd9Sstevel@tonic-gate 				    (lwp = t->t_lwp) &&
22387c478bd9Sstevel@tonic-gate 				    lwp->lwp_state == LWP_USER)
22397c478bd9Sstevel@tonic-gate 					t->t_schedflag &= ~TS_DONT_SWAP;
22407c478bd9Sstevel@tonic-gate 				fssproc->fss_timeleft = fss_quantum;
22417c478bd9Sstevel@tonic-gate 			} else {
2242c97ad5cdSakolb 				call_cpu_surrender = B_TRUE;
22437c478bd9Sstevel@tonic-gate 			}
2244b3383343Smishra 		} else if (t->t_state == TS_ONPROC &&
2245b3383343Smishra 		    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
22467c478bd9Sstevel@tonic-gate 			/*
22477c478bd9Sstevel@tonic-gate 			 * If there is a higher-priority thread which is
22487c478bd9Sstevel@tonic-gate 			 * waiting for a processor, then thread surrenders
22497c478bd9Sstevel@tonic-gate 			 * the processor.
22507c478bd9Sstevel@tonic-gate 			 */
2251c97ad5cdSakolb 			call_cpu_surrender = B_TRUE;
2252c97ad5cdSakolb 		}
2253c97ad5cdSakolb 	}
2254c97ad5cdSakolb 
2255c97ad5cdSakolb 	if (cpucaps_enforce && 2 * fssproc->fss_timeleft > fss_quantum) {
2256c97ad5cdSakolb 		/*
2257c97ad5cdSakolb 		 * The thread used more than half of its quantum, so assume that
2258c97ad5cdSakolb 		 * it used the whole quantum.
2259c97ad5cdSakolb 		 *
2260c97ad5cdSakolb 		 * Update thread's priority just before putting it on the wait
2261c97ad5cdSakolb 		 * queue so that it gets charged for the CPU time from its
2262c97ad5cdSakolb 		 * quantum even before that quantum expires.
2263c97ad5cdSakolb 		 */
2264c97ad5cdSakolb 		fss_newpri(fssproc);
2265c97ad5cdSakolb 		if (t->t_pri != fssproc->fss_umdpri)
2266c97ad5cdSakolb 			fss_change_priority(t, fssproc);
2267c97ad5cdSakolb 
2268c97ad5cdSakolb 		/*
2269c97ad5cdSakolb 		 * We need to call cpu_surrender for this thread due to cpucaps
2270c97ad5cdSakolb 		 * enforcement, but fss_change_priority may have already done
2271c97ad5cdSakolb 		 * so. In this case FSSBACKQ is set and there is no need to call
2272c97ad5cdSakolb 		 * cpu-surrender again.
2273c97ad5cdSakolb 		 */
2274c97ad5cdSakolb 		if (!(fssproc->fss_flags & FSSBACKQ))
2275c97ad5cdSakolb 			call_cpu_surrender = B_TRUE;
2276c97ad5cdSakolb 	}
2277c97ad5cdSakolb 
2278c97ad5cdSakolb 	if (call_cpu_surrender) {
22797c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSBACKQ;
22807c478bd9Sstevel@tonic-gate 		cpu_surrender(t);
22817c478bd9Sstevel@tonic-gate 	}
2282c97ad5cdSakolb 
22837c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate /*
22877c478bd9Sstevel@tonic-gate  * Processes waking up go to the back of their queue.  We don't need to assign
22887c478bd9Sstevel@tonic-gate  * a time quantum here because thread is still at a kernel mode priority and
22897c478bd9Sstevel@tonic-gate  * the time slicing is not done for threads running in the kernel after
22907c478bd9Sstevel@tonic-gate  * sleeping.  The proper time quantum will be assigned by fss_trapret before the
22917c478bd9Sstevel@tonic-gate  * thread returns to user mode.
22927c478bd9Sstevel@tonic-gate  */
22937c478bd9Sstevel@tonic-gate static void
22947c478bd9Sstevel@tonic-gate fss_wakeup(kthread_t *t)
22957c478bd9Sstevel@tonic-gate {
22967c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
22997c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_SLEEP);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	fss_active(t);
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	t->t_stime = lbolt;		/* time stamp for the swapper */
23047c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
23057c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	if (fssproc->fss_flags & FSSKPRI) {
23087c478bd9Sstevel@tonic-gate 		/*
23097c478bd9Sstevel@tonic-gate 		 * If we already have a kernel priority assigned, then we
23107c478bd9Sstevel@tonic-gate 		 * just use it.
23117c478bd9Sstevel@tonic-gate 		 */
23127c478bd9Sstevel@tonic-gate 		setbackdq(t);
23137c478bd9Sstevel@tonic-gate 	} else if (t->t_kpri_req) {
23147c478bd9Sstevel@tonic-gate 		/*
23157c478bd9Sstevel@tonic-gate 		 * Give thread a priority boost if we were asked.
23167c478bd9Sstevel@tonic-gate 		 */
23177c478bd9Sstevel@tonic-gate 		fssproc->fss_flags |= FSSKPRI;
23187c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, minclsyspri);
23197c478bd9Sstevel@tonic-gate 		setbackdq(t);
23207c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that fss_trapret will run */
23217c478bd9Sstevel@tonic-gate 		aston(t);
23227c478bd9Sstevel@tonic-gate 	} else {
23237c478bd9Sstevel@tonic-gate 		/*
23247c478bd9Sstevel@tonic-gate 		 * Otherwise, we recalculate the priority.
23257c478bd9Sstevel@tonic-gate 		 */
23267c478bd9Sstevel@tonic-gate 		if (t->t_disp_time == lbolt) {
23277c478bd9Sstevel@tonic-gate 			setfrontdq(t);
23287c478bd9Sstevel@tonic-gate 		} else {
23297c478bd9Sstevel@tonic-gate 			fssproc->fss_timeleft = fss_quantum;
23307c478bd9Sstevel@tonic-gate 			THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
23317c478bd9Sstevel@tonic-gate 			setbackdq(t);
23327c478bd9Sstevel@tonic-gate 		}
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate /*
23377c478bd9Sstevel@tonic-gate  * fss_donice() is called when a nice(1) command is issued on the thread to
23387c478bd9Sstevel@tonic-gate  * alter the priority. The nice(1) command exists in Solaris for compatibility.
23397c478bd9Sstevel@tonic-gate  * Thread priority adjustments should be done via priocntl(1).
23407c478bd9Sstevel@tonic-gate  */
23417c478bd9Sstevel@tonic-gate static int
23427c478bd9Sstevel@tonic-gate fss_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate 	int newnice;
23457c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
23467c478bd9Sstevel@tonic-gate 	fssparms_t fssparms;
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	/*
23497c478bd9Sstevel@tonic-gate 	 * If there is no change to priority, just return current setting.
23507c478bd9Sstevel@tonic-gate 	 */
23517c478bd9Sstevel@tonic-gate 	if (incr == 0) {
23527c478bd9Sstevel@tonic-gate 		if (retvalp)
23537c478bd9Sstevel@tonic-gate 			*retvalp = fssproc->fss_nice - NZERO;
23547c478bd9Sstevel@tonic-gate 		return (0);
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	if ((incr < 0 || incr > 2 * NZERO) && secpolicy_setpriority(cr) != 0)
23587c478bd9Sstevel@tonic-gate 		return (EPERM);
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	/*
23617c478bd9Sstevel@tonic-gate 	 * Specifying a nice increment greater than the upper limit of
23627c478bd9Sstevel@tonic-gate 	 * FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
23637c478bd9Sstevel@tonic-gate 	 * value being set to the upper limit.  We check for this before
23647c478bd9Sstevel@tonic-gate 	 * computing the new value because otherwise we could get overflow
23657c478bd9Sstevel@tonic-gate 	 * if a privileged user specified some ridiculous increment.
23667c478bd9Sstevel@tonic-gate 	 */
23677c478bd9Sstevel@tonic-gate 	if (incr > FSS_NICE_MAX)
23687c478bd9Sstevel@tonic-gate 		incr = FSS_NICE_MAX;
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	newnice = fssproc->fss_nice + incr;
23717c478bd9Sstevel@tonic-gate 	if (newnice > FSS_NICE_MAX)
23727c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MAX;
23737c478bd9Sstevel@tonic-gate 	else if (newnice < FSS_NICE_MIN)
23747c478bd9Sstevel@tonic-gate 		newnice = FSS_NICE_MIN;
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	fssparms.fss_uprilim = fssparms.fss_upri =
23777c478bd9Sstevel@tonic-gate 	    -((newnice - NZERO) * fss_maxupri) / NZERO;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	/*
23807c478bd9Sstevel@tonic-gate 	 * Reset the uprilim and upri values of the thread.
23817c478bd9Sstevel@tonic-gate 	 */
23827c478bd9Sstevel@tonic-gate 	(void) fss_parmsset(t, (void *)&fssparms, (id_t)0, (cred_t *)NULL);
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	/*
23857c478bd9Sstevel@tonic-gate 	 * Although fss_parmsset already reset fss_nice it may not have been
23867c478bd9Sstevel@tonic-gate 	 * set to precisely the value calculated above because fss_parmsset
23877c478bd9Sstevel@tonic-gate 	 * determines the nice value from the user priority and we may have
23887c478bd9Sstevel@tonic-gate 	 * truncated during the integer conversion from nice value to user
23897c478bd9Sstevel@tonic-gate 	 * priority and back. We reset fss_nice to the value we calculated
23907c478bd9Sstevel@tonic-gate 	 * above.
23917c478bd9Sstevel@tonic-gate 	 */
23927c478bd9Sstevel@tonic-gate 	fssproc->fss_nice = (char)newnice;
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	if (retvalp)
23957c478bd9Sstevel@tonic-gate 		*retvalp = newnice - NZERO;
23967c478bd9Sstevel@tonic-gate 	return (0);
23977c478bd9Sstevel@tonic-gate }
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate /*
2400*d4204c85Sraf  * Increment the priority of the specified thread by incr and
2401*d4204c85Sraf  * return the new value in *retvalp.
2402*d4204c85Sraf  */
2403*d4204c85Sraf static int
2404*d4204c85Sraf fss_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2405*d4204c85Sraf {
2406*d4204c85Sraf 	int newpri;
2407*d4204c85Sraf 	fssproc_t *fssproc = FSSPROC(t);
2408*d4204c85Sraf 	fssparms_t fssparms;
2409*d4204c85Sraf 
2410*d4204c85Sraf 	/*
2411*d4204c85Sraf 	 * If there is no change to priority, just return current setting.
2412*d4204c85Sraf 	 */
2413*d4204c85Sraf 	if (incr == 0) {
2414*d4204c85Sraf 		*retvalp = fssproc->fss_upri;
2415*d4204c85Sraf 		return (0);
2416*d4204c85Sraf 	}
2417*d4204c85Sraf 
2418*d4204c85Sraf 	newpri = fssproc->fss_upri + incr;
2419*d4204c85Sraf 	if (newpri > fss_maxupri || newpri < -fss_maxupri)
2420*d4204c85Sraf 		return (EINVAL);
2421*d4204c85Sraf 
2422*d4204c85Sraf 	*retvalp = newpri;
2423*d4204c85Sraf 	fssparms.fss_uprilim = fssparms.fss_upri = newpri;
2424*d4204c85Sraf 
2425*d4204c85Sraf 	/*
2426*d4204c85Sraf 	 * Reset the uprilim and upri values of the thread.
2427*d4204c85Sraf 	 */
2428*d4204c85Sraf 	return (fss_parmsset(t, &fssparms, (id_t)0, cr));
2429*d4204c85Sraf }
2430*d4204c85Sraf 
2431*d4204c85Sraf /*
24327c478bd9Sstevel@tonic-gate  * Return the global scheduling priority that would be assigned to a thread
24337c478bd9Sstevel@tonic-gate  * entering the fair-sharing class with the fss_upri.
24347c478bd9Sstevel@tonic-gate  */
24357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24367c478bd9Sstevel@tonic-gate static pri_t
24377c478bd9Sstevel@tonic-gate fss_globpri(kthread_t *t)
24387c478bd9Sstevel@tonic-gate {
24397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	return (fss_maxumdpri / 2);
24427c478bd9Sstevel@tonic-gate }
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate /*
24457c478bd9Sstevel@tonic-gate  * Called from the yield(2) system call when a thread is yielding (surrendering)
24467c478bd9Sstevel@tonic-gate  * the processor. The kernel thread is placed at the back of a dispatch queue.
24477c478bd9Sstevel@tonic-gate  */
24487c478bd9Sstevel@tonic-gate static void
24497c478bd9Sstevel@tonic-gate fss_yield(kthread_t *t)
24507c478bd9Sstevel@tonic-gate {
24517c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc = FSSPROC(t);
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
24547c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	/*
2457c97ad5cdSakolb 	 * Collect CPU usage spent before yielding
2458c97ad5cdSakolb 	 */
24598c34bbb7Sakolb 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2460c97ad5cdSakolb 
2461c97ad5cdSakolb 	/*
24627c478bd9Sstevel@tonic-gate 	 * Clear the preemption control "yield" bit since the user is
24637c478bd9Sstevel@tonic-gate 	 * doing a yield.
24647c478bd9Sstevel@tonic-gate 	 */
24657c478bd9Sstevel@tonic-gate 	if (t->t_schedctl)
24667c478bd9Sstevel@tonic-gate 		schedctl_set_yield(t, 0);
24679ac8606fSraf 	/*
24689ac8606fSraf 	 * If fss_preempt() artifically increased the thread's priority
24699ac8606fSraf 	 * to avoid preemption, restore the original priority now.
24709ac8606fSraf 	 */
24719ac8606fSraf 	if (fssproc->fss_flags & FSSRESTORE) {
24729ac8606fSraf 		THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
24739ac8606fSraf 		fssproc->fss_flags &= ~FSSRESTORE;
24749ac8606fSraf 	}
24757c478bd9Sstevel@tonic-gate 	if (fssproc->fss_timeleft < 0) {
24767c478bd9Sstevel@tonic-gate 		/*
24777c478bd9Sstevel@tonic-gate 		 * Time slice was artificially extended to avoid preemption,
24787c478bd9Sstevel@tonic-gate 		 * so pretend we're preempting it now.
24797c478bd9Sstevel@tonic-gate 		 */
24807c478bd9Sstevel@tonic-gate 		DTRACE_SCHED1(schedctl__yield, int, -fssproc->fss_timeleft);
24817c478bd9Sstevel@tonic-gate 		fssproc->fss_timeleft = fss_quantum;
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 	fssproc->fss_flags &= ~FSSBACKQ;
24847c478bd9Sstevel@tonic-gate 	setbackdq(t);
24857c478bd9Sstevel@tonic-gate }
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate void
24887c478bd9Sstevel@tonic-gate fss_changeproj(kthread_t *t, void *kp, void *zp, fssbuf_t *projbuf,
24897c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
24907c478bd9Sstevel@tonic-gate {
24917c478bd9Sstevel@tonic-gate 	kproject_t *kpj_new = kp;
24927c478bd9Sstevel@tonic-gate 	zone_t *zone = zp;
24937c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
24947c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset;
24957c478bd9Sstevel@tonic-gate 	kproject_t *kpj_old;
24967c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
24977c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
24987c478bd9Sstevel@tonic-gate 	int free = 0;
24997c478bd9Sstevel@tonic-gate 	int id;
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
25027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
25037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
25067c478bd9Sstevel@tonic-gate 		return;
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
25097c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
25107c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
25117c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
25127c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
25137c478bd9Sstevel@tonic-gate 		return;
25147c478bd9Sstevel@tonic-gate 	}
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	fsspset = FSSPROJ2FSSPSET(fssproj_old);
25177c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset->fssps_lock);
25187c478bd9Sstevel@tonic-gate 	kpj_old = FSSPROJ2KPROJ(fssproj_old);
25197c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	ASSERT(t->t_cpupart == fsspset->fssps_cpupart);
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	if (kpj_old == kpj_new) {
25247c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspset->fssps_lock);
25257c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
25267c478bd9Sstevel@tonic-gate 		return;
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset, zone)) == NULL) {
25307c478bd9Sstevel@tonic-gate 		/*
25317c478bd9Sstevel@tonic-gate 		 * If the zone for the new project is not currently active on
25327c478bd9Sstevel@tonic-gate 		 * the cpu partition we're on, get one of the pre-allocated
25337c478bd9Sstevel@tonic-gate 		 * buffers and link it in our per-pset zone list.  Such buffers
25347c478bd9Sstevel@tonic-gate 		 * should already exist.
25357c478bd9Sstevel@tonic-gate 		 */
25367c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
25377c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
25387c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset, zone, fsszone_new);
25397c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
25407c478bd9Sstevel@tonic-gate 				break;
25417c478bd9Sstevel@tonic-gate 			}
25427c478bd9Sstevel@tonic-gate 		}
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
25457c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset, kpj_new)) == NULL) {
25467c478bd9Sstevel@tonic-gate 		/*
25477c478bd9Sstevel@tonic-gate 		 * If our new project is not currently running
25487c478bd9Sstevel@tonic-gate 		 * on the cpu partition we're on, get one of the
25497c478bd9Sstevel@tonic-gate 		 * pre-allocated buffers and link it in our new cpu
25507c478bd9Sstevel@tonic-gate 		 * partition doubly linked list. Such buffers should already
25517c478bd9Sstevel@tonic-gate 		 * exist.
25527c478bd9Sstevel@tonic-gate 		 */
25537c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
25547c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
25557c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset, kpj_new,
25567c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
25577c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
25587c478bd9Sstevel@tonic-gate 				break;
25597c478bd9Sstevel@tonic-gate 			}
25607c478bd9Sstevel@tonic-gate 		}
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	thread_lock(t);
2565c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2566c97ad5cdSakolb 	    t->t_state == TS_WAIT)
25677c478bd9Sstevel@tonic-gate 		fss_inactive(t);
25687c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_old->fssp_threads > 0);
25697c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
25707c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset, fssproj_old);
25717c478bd9Sstevel@tonic-gate 		free = 1;
25727c478bd9Sstevel@tonic-gate 	}
25737c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
25747c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
25757c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
2576c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2577c97ad5cdSakolb 	    t->t_state == TS_WAIT)
25787c478bd9Sstevel@tonic-gate 		fss_active(t);
25797c478bd9Sstevel@tonic-gate 	thread_unlock(t);
25807c478bd9Sstevel@tonic-gate 	if (free) {
25817c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
25827c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
25837c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
25847c478bd9Sstevel@tonic-gate 	}
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset->fssps_lock);
25877c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
25887c478bd9Sstevel@tonic-gate }
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate void
25917c478bd9Sstevel@tonic-gate fss_changepset(kthread_t *t, void *newcp, fssbuf_t *projbuf,
25927c478bd9Sstevel@tonic-gate     fssbuf_t *zonebuf)
25937c478bd9Sstevel@tonic-gate {
25947c478bd9Sstevel@tonic-gate 	fsspset_t *fsspset_old, *fsspset_new;
25957c478bd9Sstevel@tonic-gate 	fssproj_t *fssproj_old, *fssproj_new;
25967c478bd9Sstevel@tonic-gate 	fsszone_t *fsszone_old, *fsszone_new;
25977c478bd9Sstevel@tonic-gate 	fssproc_t *fssproc;
25987c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
25997c478bd9Sstevel@tonic-gate 	zone_t *zone;
26007c478bd9Sstevel@tonic-gate 	int id;
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
26037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
26047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	if (t->t_cid != fss_cid)
26077c478bd9Sstevel@tonic-gate 		return;
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	fssproc = FSSPROC(t);
26107c478bd9Sstevel@tonic-gate 	zone = ttoproc(t)->p_zone;
26117c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspsets_lock);
26127c478bd9Sstevel@tonic-gate 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
26137c478bd9Sstevel@tonic-gate 	if (fssproj_old == NULL) {
26147c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
26157c478bd9Sstevel@tonic-gate 		return;
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 	fsszone_old = fssproj_old->fssp_fsszone;
26187c478bd9Sstevel@tonic-gate 	fsspset_old = FSSPROJ2FSSPSET(fssproj_old);
26197c478bd9Sstevel@tonic-gate 	kpj = FSSPROJ2KPROJ(fssproj_old);
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	if (fsspset_old->fssps_cpupart == newcp) {
26227c478bd9Sstevel@tonic-gate 		mutex_exit(&fsspsets_lock);
26237c478bd9Sstevel@tonic-gate 		return;
26247c478bd9Sstevel@tonic-gate 	}
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	ASSERT(ttoproj(t) == kpj);
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 	fsspset_new = fss_find_fsspset(newcp);
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_new->fssps_lock);
26317c478bd9Sstevel@tonic-gate 	if ((fsszone_new = fss_find_fsszone(fsspset_new, zone)) == NULL) {
26327c478bd9Sstevel@tonic-gate 		for (id = 0; id < zonebuf->fssb_size; id++) {
26337c478bd9Sstevel@tonic-gate 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
26347c478bd9Sstevel@tonic-gate 				fss_insert_fsszone(fsspset_new, zone,
26357c478bd9Sstevel@tonic-gate 				    fsszone_new);
26367c478bd9Sstevel@tonic-gate 				zonebuf->fssb_list[id] = NULL;
26377c478bd9Sstevel@tonic-gate 				break;
26387c478bd9Sstevel@tonic-gate 			}
26397c478bd9Sstevel@tonic-gate 		}
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 	ASSERT(fsszone_new != NULL);
26427c478bd9Sstevel@tonic-gate 	if ((fssproj_new = fss_find_fssproj(fsspset_new, kpj)) == NULL) {
26437c478bd9Sstevel@tonic-gate 		for (id = 0; id < projbuf->fssb_size; id++) {
26447c478bd9Sstevel@tonic-gate 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
26457c478bd9Sstevel@tonic-gate 				fss_insert_fssproj(fsspset_new, kpj,
26467c478bd9Sstevel@tonic-gate 				    fsszone_new, fssproj_new);
26477c478bd9Sstevel@tonic-gate 				projbuf->fssb_list[id] = NULL;
26487c478bd9Sstevel@tonic-gate 				break;
26497c478bd9Sstevel@tonic-gate 			}
26507c478bd9Sstevel@tonic-gate 		}
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 	ASSERT(fssproj_new != NULL);
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 	fssproj_new->fssp_threads++;
26557c478bd9Sstevel@tonic-gate 	thread_lock(t);
2656c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2657c97ad5cdSakolb 	    t->t_state == TS_WAIT)
26587c478bd9Sstevel@tonic-gate 		fss_inactive(t);
26597c478bd9Sstevel@tonic-gate 	fssproc->fss_proj = fssproj_new;
26607c478bd9Sstevel@tonic-gate 	fssproc->fss_fsspri = 0;
2661c97ad5cdSakolb 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2662c97ad5cdSakolb 	    t->t_state == TS_WAIT)
26637c478bd9Sstevel@tonic-gate 		fss_active(t);
26647c478bd9Sstevel@tonic-gate 	thread_unlock(t);
26657c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_new->fssps_lock);
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	mutex_enter(&fsspset_old->fssps_lock);
26687c478bd9Sstevel@tonic-gate 	if (--fssproj_old->fssp_threads == 0) {
26697c478bd9Sstevel@tonic-gate 		fss_remove_fssproj(fsspset_old, fssproj_old);
26707c478bd9Sstevel@tonic-gate 		if (fsszone_old->fssz_nproj == 0)
26717c478bd9Sstevel@tonic-gate 			kmem_free(fsszone_old, sizeof (fsszone_t));
26727c478bd9Sstevel@tonic-gate 		kmem_free(fssproj_old, sizeof (fssproj_t));
26737c478bd9Sstevel@tonic-gate 	}
26747c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspset_old->fssps_lock);
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	mutex_exit(&fsspsets_lock);
26777c478bd9Sstevel@tonic-gate }
2678