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