xref: /titanic_52/usr/src/uts/common/disp/fss.c (revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa)
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 (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/sysmacros.h>
29 #include <sys/cred.h>
30 #include <sys/proc.h>
31 #include <sys/strsubr.h>
32 #include <sys/priocntl.h>
33 #include <sys/class.h>
34 #include <sys/disp.h>
35 #include <sys/procset.h>
36 #include <sys/debug.h>
37 #include <sys/kmem.h>
38 #include <sys/errno.h>
39 #include <sys/systm.h>
40 #include <sys/schedctl.h>
41 #include <sys/vmsystm.h>
42 #include <sys/atomic.h>
43 #include <sys/project.h>
44 #include <sys/modctl.h>
45 #include <sys/fss.h>
46 #include <sys/fsspriocntl.h>
47 #include <sys/cpupart.h>
48 #include <sys/zone.h>
49 #include <vm/rm.h>
50 #include <vm/seg_kmem.h>
51 #include <sys/tnf_probe.h>
52 #include <sys/policy.h>
53 #include <sys/sdt.h>
54 #include <sys/cpucaps.h>
55 
56 /*
57  * FSS Data Structures:
58  *
59  *                 fsszone
60  *                  -----           -----
61  *  -----          |     |         |     |
62  * |     |-------->|     |<------->|     |<---->...
63  * |     |          -----           -----
64  * |     |          ^    ^            ^
65  * |     |---       |     \            \
66  *  -----    |      |      \            \
67  * fsspset   |      |       \            \
68  *           |      |        \            \
69  *           |    -----       -----       -----
70  *            -->|     |<--->|     |<--->|     |
71  *               |     |     |     |     |     |
72  *                -----       -----       -----
73  *               fssproj
74  *
75  *
76  * That is, fsspsets contain a list of fsszone's that are currently active in
77  * the pset, and a list of fssproj's, corresponding to projects with runnable
78  * threads on the pset.  fssproj's in turn point to the fsszone which they
79  * are a member of.
80  *
81  * An fssproj_t is removed when there are no threads in it.
82  *
83  * An fsszone_t is removed when there are no projects with threads in it.
84  *
85  * Projects in a zone compete with each other for cpu time, receiving cpu
86  * allocation within a zone proportional to fssproj->fssp_shares
87  * (project.cpu-shares); at a higher level zones compete with each other,
88  * receiving allocation in a pset proportional to fsszone->fssz_shares
89  * (zone.cpu-shares).  See fss_decay_usage() for the precise formula.
90  */
91 
92 static pri_t fss_init(id_t, int, classfuncs_t **);
93 
94 static struct sclass fss = {
95 	"FSS",
96 	fss_init,
97 	0
98 };
99 
100 extern struct mod_ops mod_schedops;
101 
102 /*
103  * Module linkage information for the kernel.
104  */
105 static struct modlsched modlsched = {
106 	&mod_schedops, "fair share scheduling class", &fss
107 };
108 
109 static struct modlinkage modlinkage = {
110 	MODREV_1, (void *)&modlsched, NULL
111 };
112 
113 #define	FSS_MAXUPRI	60
114 
115 /*
116  * The fssproc_t structures are kept in an array of circular doubly linked
117  * lists.  A hash on the thread pointer is used to determine which list each
118  * thread should be placed in.  Each list has a dummy "head" which is never
119  * removed, so the list is never empty.  fss_update traverses these lists to
120  * update the priorities of threads that have been waiting on the run queue.
121  */
122 #define	FSS_LISTS		16 /* number of lists, must be power of 2 */
123 #define	FSS_LIST_HASH(t)	(((uintptr_t)(t) >> 9) & (FSS_LISTS - 1))
124 #define	FSS_LIST_NEXT(i)	(((i) + 1) & (FSS_LISTS - 1))
125 
126 #define	FSS_LIST_INSERT(fssproc)				\
127 {								\
128 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
129 	kmutex_t *lockp = &fss_listlock[index];			\
130 	fssproc_t *headp = &fss_listhead[index];		\
131 	mutex_enter(lockp);					\
132 	fssproc->fss_next = headp->fss_next;			\
133 	fssproc->fss_prev = headp;				\
134 	headp->fss_next->fss_prev = fssproc;			\
135 	headp->fss_next = fssproc;				\
136 	mutex_exit(lockp);					\
137 }
138 
139 #define	FSS_LIST_DELETE(fssproc)				\
140 {								\
141 	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
142 	kmutex_t *lockp = &fss_listlock[index];			\
143 	mutex_enter(lockp);					\
144 	fssproc->fss_prev->fss_next = fssproc->fss_next;	\
145 	fssproc->fss_next->fss_prev = fssproc->fss_prev;	\
146 	mutex_exit(lockp);					\
147 }
148 
149 #define	FSS_TICK_COST	1000	/* tick cost for threads with nice level = 0 */
150 
151 /*
152  * Decay rate percentages are based on n/128 rather than n/100 so  that
153  * calculations can avoid having to do an integer divide by 100 (divide
154  * by FSS_DECAY_BASE == 128 optimizes to an arithmetic shift).
155  *
156  * FSS_DECAY_MIN	=  83/128 ~= 65%
157  * FSS_DECAY_MAX	= 108/128 ~= 85%
158  * FSS_DECAY_USG	=  96/128 ~= 75%
159  */
160 #define	FSS_DECAY_MIN	83	/* fsspri decay pct for threads w/ nice -20 */
161 #define	FSS_DECAY_MAX	108	/* fsspri decay pct for threads w/ nice +19 */
162 #define	FSS_DECAY_USG	96	/* fssusage decay pct for projects */
163 #define	FSS_DECAY_BASE	128	/* base for decay percentages above */
164 
165 #define	FSS_NICE_MIN	0
166 #define	FSS_NICE_MAX	(2 * NZERO - 1)
167 #define	FSS_NICE_RANGE	(FSS_NICE_MAX - FSS_NICE_MIN + 1)
168 
169 static int	fss_nice_tick[FSS_NICE_RANGE];
170 static int	fss_nice_decay[FSS_NICE_RANGE];
171 
172 static pri_t	fss_maxupri = FSS_MAXUPRI; /* maximum FSS user priority */
173 static pri_t	fss_maxumdpri; /* maximum user mode fss priority */
174 static pri_t	fss_maxglobpri;	/* maximum global priority used by fss class */
175 static pri_t	fss_minglobpri;	/* minimum global priority */
176 
177 static fssproc_t fss_listhead[FSS_LISTS];
178 static kmutex_t	fss_listlock[FSS_LISTS];
179 
180 static fsspset_t *fsspsets;
181 static kmutex_t fsspsets_lock;	/* protects fsspsets */
182 
183 static id_t	fss_cid;
184 
185 static time_t	fss_minrun = 2;	/* t_pri becomes 59 within 2 secs */
186 static time_t	fss_minslp = 2;	/* min time on sleep queue for hardswap */
187 static int	fss_quantum = 11;
188 
189 static void	fss_newpri(fssproc_t *);
190 static void	fss_update(void *);
191 static int	fss_update_list(int);
192 static void	fss_change_priority(kthread_t *, fssproc_t *);
193 
194 static int	fss_admin(caddr_t, cred_t *);
195 static int	fss_getclinfo(void *);
196 static int	fss_parmsin(void *);
197 static int	fss_parmsout(void *, pc_vaparms_t *);
198 static int	fss_vaparmsin(void *, pc_vaparms_t *);
199 static int	fss_vaparmsout(void *, pc_vaparms_t *);
200 static int	fss_getclpri(pcpri_t *);
201 static int	fss_alloc(void **, int);
202 static void	fss_free(void *);
203 
204 static int	fss_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
205 static void	fss_exitclass(void *);
206 static int	fss_canexit(kthread_t *, cred_t *);
207 static int	fss_fork(kthread_t *, kthread_t *, void *);
208 static void	fss_forkret(kthread_t *, kthread_t *);
209 static void	fss_parmsget(kthread_t *, void *);
210 static int	fss_parmsset(kthread_t *, void *, id_t, cred_t *);
211 static void	fss_stop(kthread_t *, int, int);
212 static void	fss_exit(kthread_t *);
213 static void	fss_active(kthread_t *);
214 static void	fss_inactive(kthread_t *);
215 static pri_t	fss_swapin(kthread_t *, int);
216 static pri_t	fss_swapout(kthread_t *, int);
217 static void	fss_trapret(kthread_t *);
218 static void	fss_preempt(kthread_t *);
219 static void	fss_setrun(kthread_t *);
220 static void	fss_sleep(kthread_t *);
221 static void	fss_tick(kthread_t *);
222 static void	fss_wakeup(kthread_t *);
223 static int	fss_donice(kthread_t *, cred_t *, int, int *);
224 static int	fss_doprio(kthread_t *, cred_t *, int, int *);
225 static pri_t	fss_globpri(kthread_t *);
226 static void	fss_yield(kthread_t *);
227 static void	fss_nullsys();
228 
229 static struct classfuncs fss_classfuncs = {
230 	/* class functions */
231 	fss_admin,
232 	fss_getclinfo,
233 	fss_parmsin,
234 	fss_parmsout,
235 	fss_vaparmsin,
236 	fss_vaparmsout,
237 	fss_getclpri,
238 	fss_alloc,
239 	fss_free,
240 
241 	/* thread functions */
242 	fss_enterclass,
243 	fss_exitclass,
244 	fss_canexit,
245 	fss_fork,
246 	fss_forkret,
247 	fss_parmsget,
248 	fss_parmsset,
249 	fss_stop,
250 	fss_exit,
251 	fss_active,
252 	fss_inactive,
253 	fss_swapin,
254 	fss_swapout,
255 	fss_trapret,
256 	fss_preempt,
257 	fss_setrun,
258 	fss_sleep,
259 	fss_tick,
260 	fss_wakeup,
261 	fss_donice,
262 	fss_globpri,
263 	fss_nullsys,	/* set_process_group */
264 	fss_yield,
265 	fss_doprio,
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 	t->t_cpri = fssproc->fss_upri;
957 	fssproc->fss_flags &= ~FSSRESTORE;
958 	if (t == curthread || t->t_state == TS_ONPROC) {
959 		/*
960 		 * curthread is always onproc
961 		 */
962 		cpu_t *cp = t->t_disp_queue->disp_cpu;
963 		THREAD_CHANGE_PRI(t, new_pri);
964 		if (t == cp->cpu_dispthread)
965 			cp->cpu_dispatch_pri = DISP_PRIO(t);
966 		if (DISP_MUST_SURRENDER(t)) {
967 			fssproc->fss_flags |= FSSBACKQ;
968 			cpu_surrender(t);
969 		} else {
970 			fssproc->fss_timeleft = fss_quantum;
971 		}
972 	} else {
973 		/*
974 		 * When the priority of a thread is changed, it may be
975 		 * necessary to adjust its position on a sleep queue or
976 		 * dispatch queue.  The function thread_change_pri accomplishes
977 		 * this.
978 		 */
979 		if (thread_change_pri(t, new_pri, 0)) {
980 			/*
981 			 * The thread was on a run queue.
982 			 */
983 			fssproc->fss_timeleft = fss_quantum;
984 		} else {
985 			fssproc->fss_flags |= FSSBACKQ;
986 		}
987 	}
988 }
989 
990 /*
991  * Update priorities of all fair-sharing threads that are currently runnable
992  * at a user mode priority based on the number of shares and current usage.
993  * Called once per second via timeout which we reset here.
994  *
995  * There are several lists of fair-sharing threads broken up by a hash on the
996  * thread pointer.  Each list has its own lock.  This avoids blocking all
997  * fss_enterclass, fss_fork, and fss_exitclass operations while fss_update runs.
998  * fss_update traverses each list in turn.
999  */
1000 static void
1001 fss_update(void *arg)
1002 {
1003 	int i;
1004 	int new_marker = -1;
1005 	static int fss_update_marker;
1006 
1007 	/*
1008 	 * Decay and update usages for all projects.
1009 	 */
1010 	fss_decay_usage();
1011 
1012 	/*
1013 	 * Start with the fss_update_marker list, then do the rest.
1014 	 */
1015 	i = fss_update_marker;
1016 
1017 	/*
1018 	 * Go around all threads, set new priorities and decay
1019 	 * per-thread CPU usages.
1020 	 */
1021 	do {
1022 		/*
1023 		 * If this is the first list after the current marker to have
1024 		 * threads with priorities updates, advance the marker to this
1025 		 * list for the next time fss_update runs.
1026 		 */
1027 		if (fss_update_list(i) &&
1028 		    new_marker == -1 && i != fss_update_marker)
1029 			new_marker = i;
1030 	} while ((i = FSS_LIST_NEXT(i)) != fss_update_marker);
1031 
1032 	/*
1033 	 * Advance marker for the next fss_update call
1034 	 */
1035 	if (new_marker != -1)
1036 		fss_update_marker = new_marker;
1037 
1038 	(void) timeout(fss_update, arg, hz);
1039 }
1040 
1041 /*
1042  * Updates priority for a list of threads.  Returns 1 if the priority of one
1043  * of the threads was actually updated, 0 if none were for various reasons
1044  * (thread is no longer in the FSS class, is not runnable, has the preemption
1045  * control no-preempt bit set, etc.)
1046  */
1047 static int
1048 fss_update_list(int i)
1049 {
1050 	fssproc_t *fssproc;
1051 	fssproj_t *fssproj;
1052 	fsspri_t fsspri;
1053 	kthread_t *t;
1054 	int updated = 0;
1055 
1056 	mutex_enter(&fss_listlock[i]);
1057 	for (fssproc = fss_listhead[i].fss_next; fssproc != &fss_listhead[i];
1058 	    fssproc = fssproc->fss_next) {
1059 		t = fssproc->fss_tp;
1060 		/*
1061 		 * Lock the thread and verify the state.
1062 		 */
1063 		thread_lock(t);
1064 		/*
1065 		 * Skip the thread if it is no longer in the FSS class or
1066 		 * is running with kernel mode priority.
1067 		 */
1068 		if (t->t_cid != fss_cid)
1069 			goto next;
1070 		if ((fssproc->fss_flags & FSSKPRI) != 0)
1071 			goto next;
1072 
1073 		fssproj = FSSPROC2FSSPROJ(fssproc);
1074 		if (fssproj == NULL)
1075 			goto next;
1076 		if (fssproj->fssp_shares != 0) {
1077 			/*
1078 			 * Decay fsspri value.
1079 			 */
1080 			fsspri = fssproc->fss_fsspri;
1081 			fsspri = (fsspri * fss_nice_decay[fssproc->fss_nice]) /
1082 			    FSS_DECAY_BASE;
1083 			fssproc->fss_fsspri = fsspri;
1084 		}
1085 
1086 		if (t->t_schedctl && schedctl_get_nopreempt(t))
1087 			goto next;
1088 		if (t->t_state != TS_RUN && t->t_state != TS_WAIT) {
1089 			/*
1090 			 * Make next syscall/trap call fss_trapret
1091 			 */
1092 			t->t_trapret = 1;
1093 			aston(t);
1094 			goto next;
1095 		}
1096 		fss_newpri(fssproc);
1097 		updated = 1;
1098 
1099 		/*
1100 		 * Only dequeue the thread if it needs to be moved; otherwise
1101 		 * it should just round-robin here.
1102 		 */
1103 		if (t->t_pri != fssproc->fss_umdpri)
1104 			fss_change_priority(t, fssproc);
1105 next:
1106 		thread_unlock(t);
1107 	}
1108 	mutex_exit(&fss_listlock[i]);
1109 	return (updated);
1110 }
1111 
1112 /*ARGSUSED*/
1113 static int
1114 fss_admin(caddr_t uaddr, cred_t *reqpcredp)
1115 {
1116 	fssadmin_t fssadmin;
1117 
1118 	if (copyin(uaddr, &fssadmin, sizeof (fssadmin_t)))
1119 		return (EFAULT);
1120 
1121 	switch (fssadmin.fss_cmd) {
1122 	case FSS_SETADMIN:
1123 		if (secpolicy_dispadm(reqpcredp) != 0)
1124 			return (EPERM);
1125 		if (fssadmin.fss_quantum <= 0 || fssadmin.fss_quantum >= hz)
1126 			return (EINVAL);
1127 		fss_quantum = fssadmin.fss_quantum;
1128 		break;
1129 	case FSS_GETADMIN:
1130 		fssadmin.fss_quantum = fss_quantum;
1131 		if (copyout(&fssadmin, uaddr, sizeof (fssadmin_t)))
1132 			return (EFAULT);
1133 		break;
1134 	default:
1135 		return (EINVAL);
1136 	}
1137 	return (0);
1138 }
1139 
1140 static int
1141 fss_getclinfo(void *infop)
1142 {
1143 	fssinfo_t *fssinfo = (fssinfo_t *)infop;
1144 	fssinfo->fss_maxupri = fss_maxupri;
1145 	return (0);
1146 }
1147 
1148 static int
1149 fss_parmsin(void *parmsp)
1150 {
1151 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1152 
1153 	/*
1154 	 * Check validity of parameters.
1155 	 */
1156 	if ((fssparmsp->fss_uprilim > fss_maxupri ||
1157 	    fssparmsp->fss_uprilim < -fss_maxupri) &&
1158 	    fssparmsp->fss_uprilim != FSS_NOCHANGE)
1159 		return (EINVAL);
1160 
1161 	if ((fssparmsp->fss_upri > fss_maxupri ||
1162 	    fssparmsp->fss_upri < -fss_maxupri) &&
1163 	    fssparmsp->fss_upri != FSS_NOCHANGE)
1164 		return (EINVAL);
1165 
1166 	return (0);
1167 }
1168 
1169 /*ARGSUSED*/
1170 static int
1171 fss_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
1172 {
1173 	return (0);
1174 }
1175 
1176 static int
1177 fss_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
1178 {
1179 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1180 	int priflag = 0;
1181 	int limflag = 0;
1182 	uint_t cnt;
1183 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
1184 
1185 	/*
1186 	 * FSS_NOCHANGE (-32768) is outside of the range of values for
1187 	 * fss_uprilim and fss_upri.  If the structure fssparms_t is changed,
1188 	 * FSS_NOCHANGE should be replaced by a flag word.
1189 	 */
1190 	fssparmsp->fss_uprilim = FSS_NOCHANGE;
1191 	fssparmsp->fss_upri = FSS_NOCHANGE;
1192 
1193 	/*
1194 	 * Get the varargs parameter and check validity of parameters.
1195 	 */
1196 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1197 		return (EINVAL);
1198 
1199 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1200 		switch (vpp->pc_key) {
1201 		case FSS_KY_UPRILIM:
1202 			if (limflag++)
1203 				return (EINVAL);
1204 			fssparmsp->fss_uprilim = (pri_t)vpp->pc_parm;
1205 			if (fssparmsp->fss_uprilim > fss_maxupri ||
1206 			    fssparmsp->fss_uprilim < -fss_maxupri)
1207 				return (EINVAL);
1208 			break;
1209 		case FSS_KY_UPRI:
1210 			if (priflag++)
1211 				return (EINVAL);
1212 			fssparmsp->fss_upri = (pri_t)vpp->pc_parm;
1213 			if (fssparmsp->fss_upri > fss_maxupri ||
1214 			    fssparmsp->fss_upri < -fss_maxupri)
1215 				return (EINVAL);
1216 			break;
1217 		default:
1218 			return (EINVAL);
1219 		}
1220 	}
1221 
1222 	if (vaparmsp->pc_vaparmscnt == 0) {
1223 		/*
1224 		 * Use default parameters.
1225 		 */
1226 		fssparmsp->fss_upri = fssparmsp->fss_uprilim = 0;
1227 	}
1228 
1229 	return (0);
1230 }
1231 
1232 /*
1233  * Copy all selected fair-sharing class parameters to the user.  The parameters
1234  * are specified by a key.
1235  */
1236 static int
1237 fss_vaparmsout(void *parmsp, pc_vaparms_t *vaparmsp)
1238 {
1239 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1240 	int priflag = 0;
1241 	int limflag = 0;
1242 	uint_t cnt;
1243 	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
1244 
1245 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
1246 
1247 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1248 		return (EINVAL);
1249 
1250 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1251 		switch (vpp->pc_key) {
1252 		case FSS_KY_UPRILIM:
1253 			if (limflag++)
1254 				return (EINVAL);
1255 			if (copyout(&fssparmsp->fss_uprilim,
1256 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1257 				return (EFAULT);
1258 			break;
1259 		case FSS_KY_UPRI:
1260 			if (priflag++)
1261 				return (EINVAL);
1262 			if (copyout(&fssparmsp->fss_upri,
1263 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1264 				return (EFAULT);
1265 			break;
1266 		default:
1267 			return (EINVAL);
1268 		}
1269 	}
1270 
1271 	return (0);
1272 }
1273 
1274 /*
1275  * Return the user mode scheduling priority range.
1276  */
1277 static int
1278 fss_getclpri(pcpri_t *pcprip)
1279 {
1280 	pcprip->pc_clpmax = fss_maxupri;
1281 	pcprip->pc_clpmin = -fss_maxupri;
1282 	return (0);
1283 }
1284 
1285 static int
1286 fss_alloc(void **p, int flag)
1287 {
1288 	void *bufp;
1289 
1290 	if ((bufp = kmem_zalloc(sizeof (fssproc_t), flag)) == NULL) {
1291 		return (ENOMEM);
1292 	} else {
1293 		*p = bufp;
1294 		return (0);
1295 	}
1296 }
1297 
1298 static void
1299 fss_free(void *bufp)
1300 {
1301 	if (bufp)
1302 		kmem_free(bufp, sizeof (fssproc_t));
1303 }
1304 
1305 /*
1306  * Thread functions
1307  */
1308 static int
1309 fss_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
1310     void *bufp)
1311 {
1312 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
1313 	fssproc_t	*fssproc;
1314 	pri_t		reqfssuprilim;
1315 	pri_t		reqfssupri;
1316 	static uint32_t fssexists = 0;
1317 	fsspset_t	*fsspset;
1318 	fssproj_t	*fssproj;
1319 	fsszone_t	*fsszone;
1320 	kproject_t	*kpj;
1321 	zone_t		*zone;
1322 	int		fsszone_allocated = 0;
1323 
1324 	fssproc = (fssproc_t *)bufp;
1325 	ASSERT(fssproc != NULL);
1326 
1327 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1328 
1329 	/*
1330 	 * Only root can move threads to FSS class.
1331 	 */
1332 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
1333 		return (EPERM);
1334 	/*
1335 	 * Initialize the fssproc structure.
1336 	 */
1337 	fssproc->fss_umdpri = fss_maxumdpri / 2;
1338 
1339 	if (fssparmsp == NULL) {
1340 		/*
1341 		 * Use default values.
1342 		 */
1343 		fssproc->fss_nice = NZERO;
1344 		fssproc->fss_uprilim = fssproc->fss_upri = 0;
1345 	} else {
1346 		/*
1347 		 * Use supplied values.
1348 		 */
1349 		if (fssparmsp->fss_uprilim == FSS_NOCHANGE) {
1350 			reqfssuprilim = 0;
1351 		} else {
1352 			if (fssparmsp->fss_uprilim > 0 &&
1353 			    secpolicy_setpriority(reqpcredp) != 0)
1354 				return (EPERM);
1355 			reqfssuprilim = fssparmsp->fss_uprilim;
1356 		}
1357 		if (fssparmsp->fss_upri == FSS_NOCHANGE) {
1358 			reqfssupri = reqfssuprilim;
1359 		} else {
1360 			if (fssparmsp->fss_upri > 0 &&
1361 			    secpolicy_setpriority(reqpcredp) != 0)
1362 				return (EPERM);
1363 			/*
1364 			 * Set the user priority to the requested value or
1365 			 * the upri limit, whichever is lower.
1366 			 */
1367 			reqfssupri = fssparmsp->fss_upri;
1368 			if (reqfssupri > reqfssuprilim)
1369 				reqfssupri = reqfssuprilim;
1370 		}
1371 		fssproc->fss_uprilim = reqfssuprilim;
1372 		fssproc->fss_upri = reqfssupri;
1373 		fssproc->fss_nice = NZERO - (NZERO * reqfssupri) / fss_maxupri;
1374 		if (fssproc->fss_nice > FSS_NICE_MAX)
1375 			fssproc->fss_nice = FSS_NICE_MAX;
1376 	}
1377 
1378 	fssproc->fss_timeleft = fss_quantum;
1379 	fssproc->fss_tp = t;
1380 	cpucaps_sc_init(&fssproc->fss_caps);
1381 
1382 	/*
1383 	 * Put a lock on our fsspset structure.
1384 	 */
1385 	mutex_enter(&fsspsets_lock);
1386 	fsspset = fss_find_fsspset(t->t_cpupart);
1387 	mutex_enter(&fsspset->fssps_lock);
1388 	mutex_exit(&fsspsets_lock);
1389 
1390 	zone = ttoproc(t)->p_zone;
1391 	if ((fsszone = fss_find_fsszone(fsspset, zone)) == NULL) {
1392 		if ((fsszone = kmem_zalloc(sizeof (fsszone_t), KM_NOSLEEP))
1393 		    == NULL) {
1394 			mutex_exit(&fsspset->fssps_lock);
1395 			return (ENOMEM);
1396 		} else {
1397 			fsszone_allocated = 1;
1398 			fss_insert_fsszone(fsspset, zone, fsszone);
1399 		}
1400 	}
1401 	kpj = ttoproj(t);
1402 	if ((fssproj = fss_find_fssproj(fsspset, kpj)) == NULL) {
1403 		if ((fssproj = kmem_zalloc(sizeof (fssproj_t), KM_NOSLEEP))
1404 		    == NULL) {
1405 			if (fsszone_allocated) {
1406 				fss_remove_fsszone(fsspset, fsszone);
1407 				kmem_free(fsszone, sizeof (fsszone_t));
1408 			}
1409 			mutex_exit(&fsspset->fssps_lock);
1410 			return (ENOMEM);
1411 		} else {
1412 			fss_insert_fssproj(fsspset, kpj, fsszone, fssproj);
1413 		}
1414 	}
1415 	fssproj->fssp_threads++;
1416 	fssproc->fss_proj = fssproj;
1417 
1418 	/*
1419 	 * Reset priority. Process goes to a "user mode" priority here
1420 	 * regardless of whether or not it has slept since entering the kernel.
1421 	 */
1422 	thread_lock(t);
1423 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
1424 	t->t_cid = cid;
1425 	t->t_cldata = (void *)fssproc;
1426 	t->t_schedflag |= TS_RUNQMATCH;
1427 	fss_change_priority(t, fssproc);
1428 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
1429 	    t->t_state == TS_WAIT)
1430 		fss_active(t);
1431 	thread_unlock(t);
1432 
1433 	mutex_exit(&fsspset->fssps_lock);
1434 
1435 	/*
1436 	 * Link new structure into fssproc list.
1437 	 */
1438 	FSS_LIST_INSERT(fssproc);
1439 
1440 	/*
1441 	 * If this is the first fair-sharing thread to occur since boot,
1442 	 * we set up the initial call to fss_update() here. Use an atomic
1443 	 * compare-and-swap since that's easier and faster than a mutex
1444 	 * (but check with an ordinary load first since most of the time
1445 	 * this will already be done).
1446 	 */
1447 	if (fssexists == 0 && cas32(&fssexists, 0, 1) == 0)
1448 		(void) timeout(fss_update, NULL, hz);
1449 
1450 	return (0);
1451 }
1452 
1453 /*
1454  * Remove fssproc_t from the list.
1455  */
1456 static void
1457 fss_exitclass(void *procp)
1458 {
1459 	fssproc_t *fssproc = (fssproc_t *)procp;
1460 	fssproj_t *fssproj;
1461 	fsspset_t *fsspset;
1462 	fsszone_t *fsszone;
1463 	kthread_t *t = fssproc->fss_tp;
1464 
1465 	/*
1466 	 * We should be either getting this thread off the deathrow or
1467 	 * this thread has already moved to another scheduling class and
1468 	 * we're being called with its old cldata buffer pointer.  In both
1469 	 * cases, the content of this buffer can not be changed while we're
1470 	 * here.
1471 	 */
1472 	mutex_enter(&fsspsets_lock);
1473 	thread_lock(t);
1474 	if (t->t_cid != fss_cid) {
1475 		/*
1476 		 * We're being called as a result of the priocntl() system
1477 		 * call -- someone is trying to move our thread to another
1478 		 * scheduling class. We can't call fss_inactive() here
1479 		 * because our thread's t_cldata pointer already points
1480 		 * to another scheduling class specific data.
1481 		 */
1482 		ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1483 
1484 		fssproj = FSSPROC2FSSPROJ(fssproc);
1485 		fsspset = FSSPROJ2FSSPSET(fssproj);
1486 		fsszone = fssproj->fssp_fsszone;
1487 
1488 		if (fssproc->fss_runnable) {
1489 			disp_lock_enter_high(&fsspset->fssps_displock);
1490 			if (--fssproj->fssp_runnable == 0) {
1491 				fsszone->fssz_shares -= fssproj->fssp_shares;
1492 				if (--fsszone->fssz_runnable == 0)
1493 					fsspset->fssps_shares -=
1494 					    fsszone->fssz_rshares;
1495 			}
1496 			disp_lock_exit_high(&fsspset->fssps_displock);
1497 		}
1498 		thread_unlock(t);
1499 
1500 		mutex_enter(&fsspset->fssps_lock);
1501 		if (--fssproj->fssp_threads == 0) {
1502 			fss_remove_fssproj(fsspset, fssproj);
1503 			if (fsszone->fssz_nproj == 0)
1504 				kmem_free(fsszone, sizeof (fsszone_t));
1505 			kmem_free(fssproj, sizeof (fssproj_t));
1506 		}
1507 		mutex_exit(&fsspset->fssps_lock);
1508 
1509 	} else {
1510 		ASSERT(t->t_state == TS_FREE);
1511 		/*
1512 		 * We're being called from thread_free() when our thread
1513 		 * is removed from the deathrow. There is nothing we need
1514 		 * do here since everything should've been done earlier
1515 		 * in fss_exit().
1516 		 */
1517 		thread_unlock(t);
1518 	}
1519 	mutex_exit(&fsspsets_lock);
1520 
1521 	FSS_LIST_DELETE(fssproc);
1522 	fss_free(fssproc);
1523 }
1524 
1525 /*ARGSUSED*/
1526 static int
1527 fss_canexit(kthread_t *t, cred_t *credp)
1528 {
1529 	/*
1530 	 * A thread is allowed to exit FSS only if we have sufficient
1531 	 * privileges.
1532 	 */
1533 	if (credp != NULL && secpolicy_setpriority(credp) != 0)
1534 		return (EPERM);
1535 	else
1536 		return (0);
1537 }
1538 
1539 /*
1540  * Initialize fair-share class specific proc structure for a child.
1541  */
1542 static int
1543 fss_fork(kthread_t *pt, kthread_t *ct, void *bufp)
1544 {
1545 	fssproc_t *pfssproc;	/* ptr to parent's fssproc structure	*/
1546 	fssproc_t *cfssproc;	/* ptr to child's fssproc structure	*/
1547 	fssproj_t *fssproj;
1548 	fsspset_t *fsspset;
1549 
1550 	ASSERT(MUTEX_HELD(&ttoproc(pt)->p_lock));
1551 	ASSERT(ct->t_state == TS_STOPPED);
1552 
1553 	cfssproc = (fssproc_t *)bufp;
1554 	ASSERT(cfssproc != NULL);
1555 	bzero(cfssproc, sizeof (fssproc_t));
1556 
1557 	thread_lock(pt);
1558 	pfssproc = FSSPROC(pt);
1559 	fssproj = FSSPROC2FSSPROJ(pfssproc);
1560 	fsspset = FSSPROJ2FSSPSET(fssproj);
1561 	thread_unlock(pt);
1562 
1563 	mutex_enter(&fsspset->fssps_lock);
1564 	/*
1565 	 * Initialize child's fssproc structure.
1566 	 */
1567 	thread_lock(pt);
1568 	ASSERT(FSSPROJ(pt) == fssproj);
1569 	cfssproc->fss_proj = fssproj;
1570 	cfssproc->fss_timeleft = fss_quantum;
1571 	cfssproc->fss_umdpri = pfssproc->fss_umdpri;
1572 	cfssproc->fss_fsspri = 0;
1573 	cfssproc->fss_uprilim = pfssproc->fss_uprilim;
1574 	cfssproc->fss_upri = pfssproc->fss_upri;
1575 	cfssproc->fss_tp = ct;
1576 	cfssproc->fss_nice = pfssproc->fss_nice;
1577 	cpucaps_sc_init(&cfssproc->fss_caps);
1578 
1579 	cfssproc->fss_flags =
1580 	    pfssproc->fss_flags & ~(FSSKPRI | FSSBACKQ | FSSRESTORE);
1581 	ct->t_cldata = (void *)cfssproc;
1582 	ct->t_schedflag |= TS_RUNQMATCH;
1583 	thread_unlock(pt);
1584 
1585 	fssproj->fssp_threads++;
1586 	mutex_exit(&fsspset->fssps_lock);
1587 
1588 	/*
1589 	 * Link new structure into fssproc hash table.
1590 	 */
1591 	FSS_LIST_INSERT(cfssproc);
1592 	return (0);
1593 }
1594 
1595 /*
1596  * Child is placed at back of dispatcher queue and parent gives up processor
1597  * so that the child runs first after the fork. This allows the child
1598  * immediately execing to break the multiple use of copy on write pages with no
1599  * disk home. The parent will get to steal them back rather than uselessly
1600  * copying them.
1601  */
1602 static void
1603 fss_forkret(kthread_t *t, kthread_t *ct)
1604 {
1605 	proc_t *pp = ttoproc(t);
1606 	proc_t *cp = ttoproc(ct);
1607 	fssproc_t *fssproc;
1608 
1609 	ASSERT(t == curthread);
1610 	ASSERT(MUTEX_HELD(&pidlock));
1611 
1612 	/*
1613 	 * Grab the child's p_lock before dropping pidlock to ensure the
1614 	 * process does not disappear before we set it running.
1615 	 */
1616 	mutex_enter(&cp->p_lock);
1617 	continuelwps(cp);
1618 	mutex_exit(&cp->p_lock);
1619 
1620 	mutex_enter(&pp->p_lock);
1621 	mutex_exit(&pidlock);
1622 	continuelwps(pp);
1623 
1624 	thread_lock(t);
1625 
1626 	fssproc = FSSPROC(t);
1627 	fss_newpri(fssproc);
1628 	fssproc->fss_timeleft = fss_quantum;
1629 	t->t_pri = fssproc->fss_umdpri;
1630 	ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
1631 	fssproc->fss_flags &= ~FSSKPRI;
1632 	THREAD_TRANSITION(t);
1633 
1634 	/*
1635 	 * We don't want to call fss_setrun(t) here because it may call
1636 	 * fss_active, which we don't need.
1637 	 */
1638 	fssproc->fss_flags &= ~FSSBACKQ;
1639 
1640 	if (t->t_disp_time != ddi_get_lbolt())
1641 		setbackdq(t);
1642 	else
1643 		setfrontdq(t);
1644 
1645 	thread_unlock(t);
1646 	/*
1647 	 * Safe to drop p_lock now since it is safe to change
1648 	 * the scheduling class after this point.
1649 	 */
1650 	mutex_exit(&pp->p_lock);
1651 
1652 	swtch();
1653 }
1654 
1655 /*
1656  * Get the fair-sharing parameters of the thread pointed to by fssprocp into
1657  * the buffer pointed by fssparmsp.
1658  */
1659 static void
1660 fss_parmsget(kthread_t *t, void *parmsp)
1661 {
1662 	fssproc_t *fssproc = FSSPROC(t);
1663 	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
1664 
1665 	fssparmsp->fss_uprilim = fssproc->fss_uprilim;
1666 	fssparmsp->fss_upri = fssproc->fss_upri;
1667 }
1668 
1669 /*ARGSUSED*/
1670 static int
1671 fss_parmsset(kthread_t *t, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1672 {
1673 	char		nice;
1674 	pri_t		reqfssuprilim;
1675 	pri_t		reqfssupri;
1676 	fssproc_t	*fssproc = FSSPROC(t);
1677 	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
1678 
1679 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1680 
1681 	if (fssparmsp->fss_uprilim == FSS_NOCHANGE)
1682 		reqfssuprilim = fssproc->fss_uprilim;
1683 	else
1684 		reqfssuprilim = fssparmsp->fss_uprilim;
1685 
1686 	if (fssparmsp->fss_upri == FSS_NOCHANGE)
1687 		reqfssupri = fssproc->fss_upri;
1688 	else
1689 		reqfssupri = fssparmsp->fss_upri;
1690 
1691 	/*
1692 	 * Make sure the user priority doesn't exceed the upri limit.
1693 	 */
1694 	if (reqfssupri > reqfssuprilim)
1695 		reqfssupri = reqfssuprilim;
1696 
1697 	/*
1698 	 * Basic permissions enforced by generic kernel code for all classes
1699 	 * require that a thread attempting to change the scheduling parameters
1700 	 * of a target thread be privileged or have a real or effective UID
1701 	 * matching that of the target thread. We are not called unless these
1702 	 * basic permission checks have already passed. The fair-sharing class
1703 	 * requires in addition that the calling thread be privileged if it
1704 	 * is attempting to raise the upri limit above its current value.
1705 	 * This may have been checked previously but if our caller passed us
1706 	 * a non-NULL credential pointer we assume it hasn't and we check it
1707 	 * here.
1708 	 */
1709 	if ((reqpcredp != NULL) &&
1710 	    (reqfssuprilim > fssproc->fss_uprilim) &&
1711 	    secpolicy_setpriority(reqpcredp) != 0)
1712 		return (EPERM);
1713 
1714 	/*
1715 	 * Set fss_nice to the nice value corresponding to the user priority we
1716 	 * are setting.  Note that setting the nice field of the parameter
1717 	 * struct won't affect upri or nice.
1718 	 */
1719 	nice = NZERO - (reqfssupri * NZERO) / fss_maxupri;
1720 	if (nice > FSS_NICE_MAX)
1721 		nice = FSS_NICE_MAX;
1722 
1723 	thread_lock(t);
1724 
1725 	fssproc->fss_uprilim = reqfssuprilim;
1726 	fssproc->fss_upri = reqfssupri;
1727 	fssproc->fss_nice = nice;
1728 	fss_newpri(fssproc);
1729 
1730 	if ((fssproc->fss_flags & FSSKPRI) != 0) {
1731 		thread_unlock(t);
1732 		return (0);
1733 	}
1734 
1735 	fss_change_priority(t, fssproc);
1736 	thread_unlock(t);
1737 	return (0);
1738 
1739 }
1740 
1741 /*
1742  * The thread is being stopped.
1743  */
1744 /*ARGSUSED*/
1745 static void
1746 fss_stop(kthread_t *t, int why, int what)
1747 {
1748 	ASSERT(THREAD_LOCK_HELD(t));
1749 	ASSERT(t == curthread);
1750 
1751 	fss_inactive(t);
1752 }
1753 
1754 /*
1755  * The current thread is exiting, do necessary adjustments to its project
1756  */
1757 static void
1758 fss_exit(kthread_t *t)
1759 {
1760 	fsspset_t *fsspset;
1761 	fssproj_t *fssproj;
1762 	fssproc_t *fssproc;
1763 	fsszone_t *fsszone;
1764 	int free = 0;
1765 
1766 	/*
1767 	 * Thread t here is either a current thread (in which case we hold
1768 	 * its process' p_lock), or a thread being destroyed by forklwp_fail(),
1769 	 * in which case we hold pidlock and thread is no longer on the
1770 	 * thread list.
1771 	 */
1772 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock) || MUTEX_HELD(&pidlock));
1773 
1774 	fssproc = FSSPROC(t);
1775 	fssproj = FSSPROC2FSSPROJ(fssproc);
1776 	fsspset = FSSPROJ2FSSPSET(fssproj);
1777 	fsszone = fssproj->fssp_fsszone;
1778 
1779 	mutex_enter(&fsspsets_lock);
1780 	mutex_enter(&fsspset->fssps_lock);
1781 
1782 	thread_lock(t);
1783 	disp_lock_enter_high(&fsspset->fssps_displock);
1784 	if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
1785 		if (--fssproj->fssp_runnable == 0) {
1786 			fsszone->fssz_shares -= fssproj->fssp_shares;
1787 			if (--fsszone->fssz_runnable == 0)
1788 				fsspset->fssps_shares -= fsszone->fssz_rshares;
1789 		}
1790 		ASSERT(fssproc->fss_runnable == 1);
1791 		fssproc->fss_runnable = 0;
1792 	}
1793 	if (--fssproj->fssp_threads == 0) {
1794 		fss_remove_fssproj(fsspset, fssproj);
1795 		free = 1;
1796 	}
1797 	disp_lock_exit_high(&fsspset->fssps_displock);
1798 	fssproc->fss_proj = NULL;	/* mark this thread as already exited */
1799 	thread_unlock(t);
1800 
1801 	if (free) {
1802 		if (fsszone->fssz_nproj == 0)
1803 			kmem_free(fsszone, sizeof (fsszone_t));
1804 		kmem_free(fssproj, sizeof (fssproj_t));
1805 	}
1806 	mutex_exit(&fsspset->fssps_lock);
1807 	mutex_exit(&fsspsets_lock);
1808 
1809 	/*
1810 	 * A thread could be exiting in between clock ticks, so we need to
1811 	 * calculate how much CPU time it used since it was charged last time.
1812 	 *
1813 	 * CPU caps are not enforced on exiting processes - it is usually
1814 	 * desirable to exit as soon as possible to free resources.
1815 	 */
1816 	if (CPUCAPS_ON()) {
1817 		thread_lock(t);
1818 		fssproc = FSSPROC(t);
1819 		(void) cpucaps_charge(t, &fssproc->fss_caps,
1820 		    CPUCAPS_CHARGE_ONLY);
1821 		thread_unlock(t);
1822 	}
1823 }
1824 
1825 static void
1826 fss_nullsys()
1827 {
1828 }
1829 
1830 /*
1831  * fss_swapin() returns -1 if the thread is loaded or is not eligible to be
1832  * swapped in. Otherwise, it returns the thread's effective priority based
1833  * on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
1834  */
1835 /*ARGSUSED*/
1836 static pri_t
1837 fss_swapin(kthread_t *t, int flags)
1838 {
1839 	fssproc_t *fssproc = FSSPROC(t);
1840 	long epri = -1;
1841 	proc_t *pp = ttoproc(t);
1842 
1843 	ASSERT(THREAD_LOCK_HELD(t));
1844 
1845 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
1846 		time_t swapout_time;
1847 
1848 		swapout_time = (ddi_get_lbolt() - t->t_stime) / hz;
1849 		if (INHERITED(t) || (fssproc->fss_flags & FSSKPRI)) {
1850 			epri = (long)DISP_PRIO(t) + swapout_time;
1851 		} else {
1852 			/*
1853 			 * Threads which have been out for a long time,
1854 			 * have high user mode priority and are associated
1855 			 * with a small address space are more deserving.
1856 			 */
1857 			epri = fssproc->fss_umdpri;
1858 			ASSERT(epri >= 0 && epri <= fss_maxumdpri);
1859 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
1860 		}
1861 		/*
1862 		 * Scale epri so that SHRT_MAX / 2 represents zero priority.
1863 		 */
1864 		epri += SHRT_MAX / 2;
1865 		if (epri < 0)
1866 			epri = 0;
1867 		else if (epri > SHRT_MAX)
1868 			epri = SHRT_MAX;
1869 	}
1870 	return ((pri_t)epri);
1871 }
1872 
1873 /*
1874  * fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
1875  * be swapped out. Otherwise, it returns the thread's effective priority
1876  * based on if the swapper is in softswap or hardswap mode.
1877  */
1878 static pri_t
1879 fss_swapout(kthread_t *t, int flags)
1880 {
1881 	fssproc_t *fssproc = FSSPROC(t);
1882 	long epri = -1;
1883 	proc_t *pp = ttoproc(t);
1884 	time_t swapin_time;
1885 
1886 	ASSERT(THREAD_LOCK_HELD(t));
1887 
1888 	if (INHERITED(t) ||
1889 	    (fssproc->fss_flags & FSSKPRI) ||
1890 	    (t->t_proc_flag & TP_LWPEXIT) ||
1891 	    (t->t_state & (TS_ZOMB|TS_FREE|TS_STOPPED|TS_ONPROC|TS_WAIT)) ||
1892 	    !(t->t_schedflag & TS_LOAD) ||
1893 	    !(SWAP_OK(t)))
1894 		return (-1);
1895 
1896 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
1897 
1898 	swapin_time = (ddi_get_lbolt() - t->t_stime) / hz;
1899 
1900 	if (flags == SOFTSWAP) {
1901 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
1902 			epri = 0;
1903 		} else {
1904 			return ((pri_t)epri);
1905 		}
1906 	} else {
1907 		pri_t pri;
1908 
1909 		if ((t->t_state == TS_SLEEP && swapin_time > fss_minslp) ||
1910 		    (t->t_state == TS_RUN && swapin_time > fss_minrun)) {
1911 			pri = fss_maxumdpri;
1912 			epri = swapin_time -
1913 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
1914 		} else {
1915 			return ((pri_t)epri);
1916 		}
1917 	}
1918 
1919 	/*
1920 	 * Scale epri so that SHRT_MAX / 2 represents zero priority.
1921 	 */
1922 	epri += SHRT_MAX / 2;
1923 	if (epri < 0)
1924 		epri = 0;
1925 	else if (epri > SHRT_MAX)
1926 		epri = SHRT_MAX;
1927 
1928 	return ((pri_t)epri);
1929 }
1930 
1931 /*
1932  * If thread is currently at a kernel mode priority (has slept) and is
1933  * returning to the userland we assign it the appropriate user mode priority
1934  * and time quantum here.  If we're lowering the thread's priority below that
1935  * of other runnable threads then we will set runrun via cpu_surrender() to
1936  * cause preemption.
1937  */
1938 static void
1939 fss_trapret(kthread_t *t)
1940 {
1941 	fssproc_t *fssproc = FSSPROC(t);
1942 	cpu_t *cp = CPU;
1943 
1944 	ASSERT(THREAD_LOCK_HELD(t));
1945 	ASSERT(t == curthread);
1946 	ASSERT(cp->cpu_dispthread == t);
1947 	ASSERT(t->t_state == TS_ONPROC);
1948 
1949 	t->t_kpri_req = 0;
1950 	if (fssproc->fss_flags & FSSKPRI) {
1951 		/*
1952 		 * If thread has blocked in the kernel
1953 		 */
1954 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
1955 		cp->cpu_dispatch_pri = DISP_PRIO(t);
1956 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
1957 		fssproc->fss_flags &= ~FSSKPRI;
1958 
1959 		if (DISP_MUST_SURRENDER(t))
1960 			cpu_surrender(t);
1961 	}
1962 
1963 	/*
1964 	 * Swapout lwp if the swapper is waiting for this thread to reach
1965 	 * a safe point.
1966 	 */
1967 	if (t->t_schedflag & TS_SWAPENQ) {
1968 		thread_unlock(t);
1969 		swapout_lwp(ttolwp(t));
1970 		thread_lock(t);
1971 	}
1972 }
1973 
1974 /*
1975  * Arrange for thread to be placed in appropriate location on dispatcher queue.
1976  * This is called with the current thread in TS_ONPROC and locked.
1977  */
1978 static void
1979 fss_preempt(kthread_t *t)
1980 {
1981 	fssproc_t *fssproc = FSSPROC(t);
1982 	klwp_t *lwp;
1983 	uint_t flags;
1984 
1985 	ASSERT(t == curthread);
1986 	ASSERT(THREAD_LOCK_HELD(curthread));
1987 	ASSERT(t->t_state == TS_ONPROC);
1988 
1989 	/*
1990 	 * If preempted in the kernel, make sure the thread has a kernel
1991 	 * priority if needed.
1992 	 */
1993 	lwp = curthread->t_lwp;
1994 	if (!(fssproc->fss_flags & FSSKPRI) && lwp != NULL && t->t_kpri_req) {
1995 		fssproc->fss_flags |= FSSKPRI;
1996 		THREAD_CHANGE_PRI(t, minclsyspri);
1997 		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
1998 		t->t_trapret = 1;	/* so that fss_trapret will run */
1999 		aston(t);
2000 	}
2001 
2002 	/*
2003 	 * This thread may be placed on wait queue by CPU Caps. In this case we
2004 	 * do not need to do anything until it is removed from the wait queue.
2005 	 * Do not enforce CPU caps on threads running at a kernel priority
2006 	 */
2007 	if (CPUCAPS_ON()) {
2008 		(void) cpucaps_charge(t, &fssproc->fss_caps,
2009 		    CPUCAPS_CHARGE_ENFORCE);
2010 
2011 		if (!(fssproc->fss_flags & FSSKPRI) && CPUCAPS_ENFORCE(t))
2012 			return;
2013 	}
2014 
2015 	/*
2016 	 * If preempted in user-land mark the thread as swappable because it
2017 	 * cannot be holding any kernel locks.
2018 	 */
2019 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
2020 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
2021 		t->t_schedflag &= ~TS_DONT_SWAP;
2022 
2023 	/*
2024 	 * Check to see if we're doing "preemption control" here.  If
2025 	 * we are, and if the user has requested that this thread not
2026 	 * be preempted, and if preemptions haven't been put off for
2027 	 * too long, let the preemption happen here but try to make
2028 	 * sure the thread is rescheduled as soon as possible.  We do
2029 	 * this by putting it on the front of the highest priority run
2030 	 * queue in the FSS class.  If the preemption has been put off
2031 	 * for too long, clear the "nopreempt" bit and let the thread
2032 	 * be preempted.
2033 	 */
2034 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
2035 		if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
2036 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
2037 			if (!(fssproc->fss_flags & FSSKPRI)) {
2038 				/*
2039 				 * If not already remembered, remember current
2040 				 * priority for restoration in fss_yield().
2041 				 */
2042 				if (!(fssproc->fss_flags & FSSRESTORE)) {
2043 					fssproc->fss_scpri = t->t_pri;
2044 					fssproc->fss_flags |= FSSRESTORE;
2045 				}
2046 				THREAD_CHANGE_PRI(t, fss_maxumdpri);
2047 				t->t_schedflag |= TS_DONT_SWAP;
2048 			}
2049 			schedctl_set_yield(t, 1);
2050 			setfrontdq(t);
2051 			return;
2052 		} else {
2053 			if (fssproc->fss_flags & FSSRESTORE) {
2054 				THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
2055 				fssproc->fss_flags &= ~FSSRESTORE;
2056 			}
2057 			schedctl_set_nopreempt(t, 0);
2058 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
2059 			/*
2060 			 * Fall through and be preempted below.
2061 			 */
2062 		}
2063 	}
2064 
2065 	flags = fssproc->fss_flags & (FSSBACKQ | FSSKPRI);
2066 
2067 	if (flags == FSSBACKQ) {
2068 		fssproc->fss_timeleft = fss_quantum;
2069 		fssproc->fss_flags &= ~FSSBACKQ;
2070 		setbackdq(t);
2071 	} else if (flags == (FSSBACKQ | FSSKPRI)) {
2072 		fssproc->fss_flags &= ~FSSBACKQ;
2073 		setbackdq(t);
2074 	} else {
2075 		setfrontdq(t);
2076 	}
2077 }
2078 
2079 /*
2080  * Called when a thread is waking up and is to be placed on the run queue.
2081  */
2082 static void
2083 fss_setrun(kthread_t *t)
2084 {
2085 	fssproc_t *fssproc = FSSPROC(t);
2086 
2087 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
2088 
2089 	if (t->t_state == TS_SLEEP || t->t_state == TS_STOPPED)
2090 		fss_active(t);
2091 
2092 	fssproc->fss_timeleft = fss_quantum;
2093 
2094 	fssproc->fss_flags &= ~FSSBACKQ;
2095 	/*
2096 	 * If previously were running at the kernel priority then keep that
2097 	 * priority and the fss_timeleft doesn't matter.
2098 	 */
2099 	if ((fssproc->fss_flags & FSSKPRI) == 0)
2100 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
2101 
2102 	if (t->t_disp_time != ddi_get_lbolt())
2103 		setbackdq(t);
2104 	else
2105 		setfrontdq(t);
2106 }
2107 
2108 /*
2109  * Prepare thread for sleep. We reset the thread priority so it will run at the
2110  * kernel priority level when it wakes up.
2111  */
2112 static void
2113 fss_sleep(kthread_t *t)
2114 {
2115 	fssproc_t *fssproc = FSSPROC(t);
2116 
2117 	ASSERT(t == curthread);
2118 	ASSERT(THREAD_LOCK_HELD(t));
2119 
2120 	ASSERT(t->t_state == TS_ONPROC);
2121 
2122 	/*
2123 	 * Account for time spent on CPU before going to sleep.
2124 	 */
2125 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2126 
2127 	fss_inactive(t);
2128 
2129 	/*
2130 	 * Assign a system priority to the thread and arrange for it to be
2131 	 * retained when the thread is next placed on the run queue (i.e.,
2132 	 * when it wakes up) instead of being given a new pri.  Also arrange
2133 	 * for trapret processing as the thread leaves the system call so it
2134 	 * will drop back to normal priority range.
2135 	 */
2136 	if (t->t_kpri_req) {
2137 		THREAD_CHANGE_PRI(t, minclsyspri);
2138 		fssproc->fss_flags |= FSSKPRI;
2139 		t->t_trapret = 1;	/* so that fss_trapret will run */
2140 		aston(t);
2141 	} else if (fssproc->fss_flags & FSSKPRI) {
2142 		/*
2143 		 * The thread has done a THREAD_KPRI_REQUEST(), slept, then
2144 		 * done THREAD_KPRI_RELEASE() (so no t_kpri_req is 0 again),
2145 		 * then slept again all without finishing the current system
2146 		 * call so trapret won't have cleared FSSKPRI
2147 		 */
2148 		fssproc->fss_flags &= ~FSSKPRI;
2149 		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
2150 		if (DISP_MUST_SURRENDER(curthread))
2151 			cpu_surrender(t);
2152 	}
2153 	t->t_stime = ddi_get_lbolt();	/* time stamp for the swapper */
2154 }
2155 
2156 /*
2157  * A tick interrupt has ocurrend on a running thread. Check to see if our
2158  * time slice has expired.  We must also clear the TS_DONT_SWAP flag in
2159  * t_schedflag if the thread is eligible to be swapped out.
2160  */
2161 static void
2162 fss_tick(kthread_t *t)
2163 {
2164 	fssproc_t *fssproc;
2165 	fssproj_t *fssproj;
2166 	klwp_t *lwp;
2167 	boolean_t call_cpu_surrender = B_FALSE;
2168 	boolean_t cpucaps_enforce = B_FALSE;
2169 
2170 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
2171 
2172 	/*
2173 	 * It's safe to access fsspset and fssproj structures because we're
2174 	 * holding our p_lock here.
2175 	 */
2176 	thread_lock(t);
2177 	fssproc = FSSPROC(t);
2178 	fssproj = FSSPROC2FSSPROJ(fssproc);
2179 	if (fssproj != NULL) {
2180 		fsspset_t *fsspset = FSSPROJ2FSSPSET(fssproj);
2181 		disp_lock_enter_high(&fsspset->fssps_displock);
2182 		fssproj->fssp_ticks += fss_nice_tick[fssproc->fss_nice];
2183 		fssproc->fss_ticks++;
2184 		disp_lock_exit_high(&fsspset->fssps_displock);
2185 	}
2186 
2187 	/*
2188 	 * Keep track of thread's project CPU usage.  Note that projects
2189 	 * get charged even when threads are running in the kernel.
2190 	 * Do not surrender CPU if running in the SYS class.
2191 	 */
2192 	if (CPUCAPS_ON()) {
2193 		cpucaps_enforce = cpucaps_charge(t,
2194 		    &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE) &&
2195 		    !(fssproc->fss_flags & FSSKPRI);
2196 	}
2197 
2198 	/*
2199 	 * A thread's execution time for threads running in the SYS class
2200 	 * is not tracked.
2201 	 */
2202 	if ((fssproc->fss_flags & FSSKPRI) == 0) {
2203 		/*
2204 		 * If thread is not in kernel mode, decrement its fss_timeleft
2205 		 */
2206 		if (--fssproc->fss_timeleft <= 0) {
2207 			pri_t new_pri;
2208 
2209 			/*
2210 			 * If we're doing preemption control and trying to
2211 			 * avoid preempting this thread, just note that the
2212 			 * thread should yield soon and let it keep running
2213 			 * (unless it's been a while).
2214 			 */
2215 			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
2216 				if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
2217 					DTRACE_SCHED1(schedctl__nopreempt,
2218 					    kthread_t *, t);
2219 					schedctl_set_yield(t, 1);
2220 					thread_unlock_nopreempt(t);
2221 					return;
2222 				}
2223 			}
2224 			fssproc->fss_flags &= ~FSSRESTORE;
2225 
2226 			fss_newpri(fssproc);
2227 			new_pri = fssproc->fss_umdpri;
2228 			ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);
2229 
2230 			/*
2231 			 * When the priority of a thread is changed, it may
2232 			 * be necessary to adjust its position on a sleep queue
2233 			 * or dispatch queue. The function thread_change_pri
2234 			 * accomplishes this.
2235 			 */
2236 			if (thread_change_pri(t, new_pri, 0)) {
2237 				if ((t->t_schedflag & TS_LOAD) &&
2238 				    (lwp = t->t_lwp) &&
2239 				    lwp->lwp_state == LWP_USER)
2240 					t->t_schedflag &= ~TS_DONT_SWAP;
2241 				fssproc->fss_timeleft = fss_quantum;
2242 			} else {
2243 				call_cpu_surrender = B_TRUE;
2244 			}
2245 		} else if (t->t_state == TS_ONPROC &&
2246 		    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
2247 			/*
2248 			 * If there is a higher-priority thread which is
2249 			 * waiting for a processor, then thread surrenders
2250 			 * the processor.
2251 			 */
2252 			call_cpu_surrender = B_TRUE;
2253 		}
2254 	}
2255 
2256 	if (cpucaps_enforce && 2 * fssproc->fss_timeleft > fss_quantum) {
2257 		/*
2258 		 * The thread used more than half of its quantum, so assume that
2259 		 * it used the whole quantum.
2260 		 *
2261 		 * Update thread's priority just before putting it on the wait
2262 		 * queue so that it gets charged for the CPU time from its
2263 		 * quantum even before that quantum expires.
2264 		 */
2265 		fss_newpri(fssproc);
2266 		if (t->t_pri != fssproc->fss_umdpri)
2267 			fss_change_priority(t, fssproc);
2268 
2269 		/*
2270 		 * We need to call cpu_surrender for this thread due to cpucaps
2271 		 * enforcement, but fss_change_priority may have already done
2272 		 * so. In this case FSSBACKQ is set and there is no need to call
2273 		 * cpu-surrender again.
2274 		 */
2275 		if (!(fssproc->fss_flags & FSSBACKQ))
2276 			call_cpu_surrender = B_TRUE;
2277 	}
2278 
2279 	if (call_cpu_surrender) {
2280 		fssproc->fss_flags |= FSSBACKQ;
2281 		cpu_surrender(t);
2282 	}
2283 
2284 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
2285 }
2286 
2287 /*
2288  * Processes waking up go to the back of their queue.  We don't need to assign
2289  * a time quantum here because thread is still at a kernel mode priority and
2290  * the time slicing is not done for threads running in the kernel after
2291  * sleeping.  The proper time quantum will be assigned by fss_trapret before the
2292  * thread returns to user mode.
2293  */
2294 static void
2295 fss_wakeup(kthread_t *t)
2296 {
2297 	fssproc_t *fssproc;
2298 
2299 	ASSERT(THREAD_LOCK_HELD(t));
2300 	ASSERT(t->t_state == TS_SLEEP);
2301 
2302 	fss_active(t);
2303 
2304 	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
2305 	fssproc = FSSPROC(t);
2306 	fssproc->fss_flags &= ~FSSBACKQ;
2307 
2308 	if (fssproc->fss_flags & FSSKPRI) {
2309 		/*
2310 		 * If we already have a kernel priority assigned, then we
2311 		 * just use it.
2312 		 */
2313 		setbackdq(t);
2314 	} else if (t->t_kpri_req) {
2315 		/*
2316 		 * Give thread a priority boost if we were asked.
2317 		 */
2318 		fssproc->fss_flags |= FSSKPRI;
2319 		THREAD_CHANGE_PRI(t, minclsyspri);
2320 		setbackdq(t);
2321 		t->t_trapret = 1;	/* so that fss_trapret will run */
2322 		aston(t);
2323 	} else {
2324 		/*
2325 		 * Otherwise, we recalculate the priority.
2326 		 */
2327 		if (t->t_disp_time == ddi_get_lbolt()) {
2328 			setfrontdq(t);
2329 		} else {
2330 			fssproc->fss_timeleft = fss_quantum;
2331 			THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
2332 			setbackdq(t);
2333 		}
2334 	}
2335 }
2336 
2337 /*
2338  * fss_donice() is called when a nice(1) command is issued on the thread to
2339  * alter the priority. The nice(1) command exists in Solaris for compatibility.
2340  * Thread priority adjustments should be done via priocntl(1).
2341  */
2342 static int
2343 fss_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2344 {
2345 	int newnice;
2346 	fssproc_t *fssproc = FSSPROC(t);
2347 	fssparms_t fssparms;
2348 
2349 	/*
2350 	 * If there is no change to priority, just return current setting.
2351 	 */
2352 	if (incr == 0) {
2353 		if (retvalp)
2354 			*retvalp = fssproc->fss_nice - NZERO;
2355 		return (0);
2356 	}
2357 
2358 	if ((incr < 0 || incr > 2 * NZERO) && secpolicy_setpriority(cr) != 0)
2359 		return (EPERM);
2360 
2361 	/*
2362 	 * Specifying a nice increment greater than the upper limit of
2363 	 * FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
2364 	 * value being set to the upper limit.  We check for this before
2365 	 * computing the new value because otherwise we could get overflow
2366 	 * if a privileged user specified some ridiculous increment.
2367 	 */
2368 	if (incr > FSS_NICE_MAX)
2369 		incr = FSS_NICE_MAX;
2370 
2371 	newnice = fssproc->fss_nice + incr;
2372 	if (newnice > FSS_NICE_MAX)
2373 		newnice = FSS_NICE_MAX;
2374 	else if (newnice < FSS_NICE_MIN)
2375 		newnice = FSS_NICE_MIN;
2376 
2377 	fssparms.fss_uprilim = fssparms.fss_upri =
2378 	    -((newnice - NZERO) * fss_maxupri) / NZERO;
2379 
2380 	/*
2381 	 * Reset the uprilim and upri values of the thread.
2382 	 */
2383 	(void) fss_parmsset(t, (void *)&fssparms, (id_t)0, (cred_t *)NULL);
2384 
2385 	/*
2386 	 * Although fss_parmsset already reset fss_nice it may not have been
2387 	 * set to precisely the value calculated above because fss_parmsset
2388 	 * determines the nice value from the user priority and we may have
2389 	 * truncated during the integer conversion from nice value to user
2390 	 * priority and back. We reset fss_nice to the value we calculated
2391 	 * above.
2392 	 */
2393 	fssproc->fss_nice = (char)newnice;
2394 
2395 	if (retvalp)
2396 		*retvalp = newnice - NZERO;
2397 	return (0);
2398 }
2399 
2400 /*
2401  * Increment the priority of the specified thread by incr and
2402  * return the new value in *retvalp.
2403  */
2404 static int
2405 fss_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2406 {
2407 	int newpri;
2408 	fssproc_t *fssproc = FSSPROC(t);
2409 	fssparms_t fssparms;
2410 
2411 	/*
2412 	 * If there is no change to priority, just return current setting.
2413 	 */
2414 	if (incr == 0) {
2415 		*retvalp = fssproc->fss_upri;
2416 		return (0);
2417 	}
2418 
2419 	newpri = fssproc->fss_upri + incr;
2420 	if (newpri > fss_maxupri || newpri < -fss_maxupri)
2421 		return (EINVAL);
2422 
2423 	*retvalp = newpri;
2424 	fssparms.fss_uprilim = fssparms.fss_upri = newpri;
2425 
2426 	/*
2427 	 * Reset the uprilim and upri values of the thread.
2428 	 */
2429 	return (fss_parmsset(t, &fssparms, (id_t)0, cr));
2430 }
2431 
2432 /*
2433  * Return the global scheduling priority that would be assigned to a thread
2434  * entering the fair-sharing class with the fss_upri.
2435  */
2436 /*ARGSUSED*/
2437 static pri_t
2438 fss_globpri(kthread_t *t)
2439 {
2440 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2441 
2442 	return (fss_maxumdpri / 2);
2443 }
2444 
2445 /*
2446  * Called from the yield(2) system call when a thread is yielding (surrendering)
2447  * the processor. The kernel thread is placed at the back of a dispatch queue.
2448  */
2449 static void
2450 fss_yield(kthread_t *t)
2451 {
2452 	fssproc_t *fssproc = FSSPROC(t);
2453 
2454 	ASSERT(t == curthread);
2455 	ASSERT(THREAD_LOCK_HELD(t));
2456 
2457 	/*
2458 	 * Collect CPU usage spent before yielding
2459 	 */
2460 	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);
2461 
2462 	/*
2463 	 * Clear the preemption control "yield" bit since the user is
2464 	 * doing a yield.
2465 	 */
2466 	if (t->t_schedctl)
2467 		schedctl_set_yield(t, 0);
2468 	/*
2469 	 * If fss_preempt() artifically increased the thread's priority
2470 	 * to avoid preemption, restore the original priority now.
2471 	 */
2472 	if (fssproc->fss_flags & FSSRESTORE) {
2473 		THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
2474 		fssproc->fss_flags &= ~FSSRESTORE;
2475 	}
2476 	if (fssproc->fss_timeleft < 0) {
2477 		/*
2478 		 * Time slice was artificially extended to avoid preemption,
2479 		 * so pretend we're preempting it now.
2480 		 */
2481 		DTRACE_SCHED1(schedctl__yield, int, -fssproc->fss_timeleft);
2482 		fssproc->fss_timeleft = fss_quantum;
2483 	}
2484 	fssproc->fss_flags &= ~FSSBACKQ;
2485 	setbackdq(t);
2486 }
2487 
2488 void
2489 fss_changeproj(kthread_t *t, void *kp, void *zp, fssbuf_t *projbuf,
2490     fssbuf_t *zonebuf)
2491 {
2492 	kproject_t *kpj_new = kp;
2493 	zone_t *zone = zp;
2494 	fssproj_t *fssproj_old, *fssproj_new;
2495 	fsspset_t *fsspset;
2496 	kproject_t *kpj_old;
2497 	fssproc_t *fssproc;
2498 	fsszone_t *fsszone_old, *fsszone_new;
2499 	int free = 0;
2500 	int id;
2501 
2502 	ASSERT(MUTEX_HELD(&cpu_lock));
2503 	ASSERT(MUTEX_HELD(&pidlock));
2504 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2505 
2506 	if (t->t_cid != fss_cid)
2507 		return;
2508 
2509 	fssproc = FSSPROC(t);
2510 	mutex_enter(&fsspsets_lock);
2511 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
2512 	if (fssproj_old == NULL) {
2513 		mutex_exit(&fsspsets_lock);
2514 		return;
2515 	}
2516 
2517 	fsspset = FSSPROJ2FSSPSET(fssproj_old);
2518 	mutex_enter(&fsspset->fssps_lock);
2519 	kpj_old = FSSPROJ2KPROJ(fssproj_old);
2520 	fsszone_old = fssproj_old->fssp_fsszone;
2521 
2522 	ASSERT(t->t_cpupart == fsspset->fssps_cpupart);
2523 
2524 	if (kpj_old == kpj_new) {
2525 		mutex_exit(&fsspset->fssps_lock);
2526 		mutex_exit(&fsspsets_lock);
2527 		return;
2528 	}
2529 
2530 	if ((fsszone_new = fss_find_fsszone(fsspset, zone)) == NULL) {
2531 		/*
2532 		 * If the zone for the new project is not currently active on
2533 		 * the cpu partition we're on, get one of the pre-allocated
2534 		 * buffers and link it in our per-pset zone list.  Such buffers
2535 		 * should already exist.
2536 		 */
2537 		for (id = 0; id < zonebuf->fssb_size; id++) {
2538 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
2539 				fss_insert_fsszone(fsspset, zone, fsszone_new);
2540 				zonebuf->fssb_list[id] = NULL;
2541 				break;
2542 			}
2543 		}
2544 	}
2545 	ASSERT(fsszone_new != NULL);
2546 	if ((fssproj_new = fss_find_fssproj(fsspset, kpj_new)) == NULL) {
2547 		/*
2548 		 * If our new project is not currently running
2549 		 * on the cpu partition we're on, get one of the
2550 		 * pre-allocated buffers and link it in our new cpu
2551 		 * partition doubly linked list. Such buffers should already
2552 		 * exist.
2553 		 */
2554 		for (id = 0; id < projbuf->fssb_size; id++) {
2555 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
2556 				fss_insert_fssproj(fsspset, kpj_new,
2557 				    fsszone_new, fssproj_new);
2558 				projbuf->fssb_list[id] = NULL;
2559 				break;
2560 			}
2561 		}
2562 	}
2563 	ASSERT(fssproj_new != NULL);
2564 
2565 	thread_lock(t);
2566 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2567 	    t->t_state == TS_WAIT)
2568 		fss_inactive(t);
2569 	ASSERT(fssproj_old->fssp_threads > 0);
2570 	if (--fssproj_old->fssp_threads == 0) {
2571 		fss_remove_fssproj(fsspset, fssproj_old);
2572 		free = 1;
2573 	}
2574 	fssproc->fss_proj = fssproj_new;
2575 	fssproc->fss_fsspri = 0;
2576 	fssproj_new->fssp_threads++;
2577 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2578 	    t->t_state == TS_WAIT)
2579 		fss_active(t);
2580 	thread_unlock(t);
2581 	if (free) {
2582 		if (fsszone_old->fssz_nproj == 0)
2583 			kmem_free(fsszone_old, sizeof (fsszone_t));
2584 		kmem_free(fssproj_old, sizeof (fssproj_t));
2585 	}
2586 
2587 	mutex_exit(&fsspset->fssps_lock);
2588 	mutex_exit(&fsspsets_lock);
2589 }
2590 
2591 void
2592 fss_changepset(kthread_t *t, void *newcp, fssbuf_t *projbuf,
2593     fssbuf_t *zonebuf)
2594 {
2595 	fsspset_t *fsspset_old, *fsspset_new;
2596 	fssproj_t *fssproj_old, *fssproj_new;
2597 	fsszone_t *fsszone_old, *fsszone_new;
2598 	fssproc_t *fssproc;
2599 	kproject_t *kpj;
2600 	zone_t *zone;
2601 	int id;
2602 
2603 	ASSERT(MUTEX_HELD(&cpu_lock));
2604 	ASSERT(MUTEX_HELD(&pidlock));
2605 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2606 
2607 	if (t->t_cid != fss_cid)
2608 		return;
2609 
2610 	fssproc = FSSPROC(t);
2611 	zone = ttoproc(t)->p_zone;
2612 	mutex_enter(&fsspsets_lock);
2613 	fssproj_old = FSSPROC2FSSPROJ(fssproc);
2614 	if (fssproj_old == NULL) {
2615 		mutex_exit(&fsspsets_lock);
2616 		return;
2617 	}
2618 	fsszone_old = fssproj_old->fssp_fsszone;
2619 	fsspset_old = FSSPROJ2FSSPSET(fssproj_old);
2620 	kpj = FSSPROJ2KPROJ(fssproj_old);
2621 
2622 	if (fsspset_old->fssps_cpupart == newcp) {
2623 		mutex_exit(&fsspsets_lock);
2624 		return;
2625 	}
2626 
2627 	ASSERT(ttoproj(t) == kpj);
2628 
2629 	fsspset_new = fss_find_fsspset(newcp);
2630 
2631 	mutex_enter(&fsspset_new->fssps_lock);
2632 	if ((fsszone_new = fss_find_fsszone(fsspset_new, zone)) == NULL) {
2633 		for (id = 0; id < zonebuf->fssb_size; id++) {
2634 			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
2635 				fss_insert_fsszone(fsspset_new, zone,
2636 				    fsszone_new);
2637 				zonebuf->fssb_list[id] = NULL;
2638 				break;
2639 			}
2640 		}
2641 	}
2642 	ASSERT(fsszone_new != NULL);
2643 	if ((fssproj_new = fss_find_fssproj(fsspset_new, kpj)) == NULL) {
2644 		for (id = 0; id < projbuf->fssb_size; id++) {
2645 			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
2646 				fss_insert_fssproj(fsspset_new, kpj,
2647 				    fsszone_new, fssproj_new);
2648 				projbuf->fssb_list[id] = NULL;
2649 				break;
2650 			}
2651 		}
2652 	}
2653 	ASSERT(fssproj_new != NULL);
2654 
2655 	fssproj_new->fssp_threads++;
2656 	thread_lock(t);
2657 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2658 	    t->t_state == TS_WAIT)
2659 		fss_inactive(t);
2660 	fssproc->fss_proj = fssproj_new;
2661 	fssproc->fss_fsspri = 0;
2662 	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
2663 	    t->t_state == TS_WAIT)
2664 		fss_active(t);
2665 	thread_unlock(t);
2666 	mutex_exit(&fsspset_new->fssps_lock);
2667 
2668 	mutex_enter(&fsspset_old->fssps_lock);
2669 	if (--fssproj_old->fssp_threads == 0) {
2670 		fss_remove_fssproj(fsspset_old, fssproj_old);
2671 		if (fsszone_old->fssz_nproj == 0)
2672 			kmem_free(fsszone_old, sizeof (fsszone_t));
2673 		kmem_free(fssproj_old, sizeof (fssproj_t));
2674 	}
2675 	mutex_exit(&fsspset_old->fssps_lock);
2676 
2677 	mutex_exit(&fsspsets_lock);
2678 }
2679