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