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