summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/disp/fss.c
blob: af8826780c42b19228de58177b19d5e86a626425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/strsubr.h>
#include <sys/priocntl.h>
#include <sys/class.h>
#include <sys/disp.h>
#include <sys/procset.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/schedctl.h>
#include <sys/vmsystm.h>
#include <sys/atomic.h>
#include <sys/project.h>
#include <sys/modctl.h>
#include <sys/fss.h>
#include <sys/fsspriocntl.h>
#include <sys/cpupart.h>
#include <sys/zone.h>
#include <vm/rm.h>
#include <vm/seg_kmem.h>
#include <sys/tnf_probe.h>
#include <sys/policy.h>
#include <sys/sdt.h>
#include <sys/cpucaps.h>

/*
 * FSS Data Structures:
 *
 *                 fsszone
 *                  -----           -----
 *  -----          |     |         |     |
 * |     |-------->|     |<------->|     |<---->...
 * |     |          -----           -----
 * |     |          ^    ^            ^
 * |     |---       |     \            \
 *  -----    |      |      \            \
 * fsspset   |      |       \            \
 *           |      |        \            \
 *           |    -----       -----       -----
 *            -->|     |<--->|     |<--->|     |
 *               |     |     |     |     |     |
 *                -----       -----       -----
 *               fssproj
 *
 *
 * That is, fsspsets contain a list of fsszone's that are currently active in
 * the pset, and a list of fssproj's, corresponding to projects with runnable
 * threads on the pset.  fssproj's in turn point to the fsszone which they
 * are a member of.
 *
 * An fssproj_t is removed when there are no threads in it.
 *
 * An fsszone_t is removed when there are no projects with threads in it.
 *
 * Projects in a zone compete with each other for cpu time, receiving cpu
 * allocation within a zone proportional to fssproj->fssp_shares
 * (project.cpu-shares); at a higher level zones compete with each other,
 * receiving allocation in a pset proportional to fsszone->fssz_shares
 * (zone.cpu-shares).  See fss_decay_usage() for the precise formula.
 */

static pri_t fss_init(id_t, int, classfuncs_t **);

static struct sclass fss = {
	"FSS",
	fss_init,
	0
};

extern struct mod_ops mod_schedops;

/*
 * Module linkage information for the kernel.
 */
static struct modlsched modlsched = {
	&mod_schedops, "fair share scheduling class", &fss
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlsched, NULL
};

#define	FSS_MAXUPRI	60

/*
 * The fssproc_t structures are kept in an array of circular doubly linked
 * lists.  A hash on the thread pointer is used to determine which list each
 * thread should be placed in.  Each list has a dummy "head" which is never
 * removed, so the list is never empty.  fss_update traverses these lists to
 * update the priorities of threads that have been waiting on the run queue.
 */
#define	FSS_LISTS		16 /* number of lists, must be power of 2 */
#define	FSS_LIST_HASH(t)	(((uintptr_t)(t) >> 9) & (FSS_LISTS - 1))
#define	FSS_LIST_NEXT(i)	(((i) + 1) & (FSS_LISTS - 1))

#define	FSS_LIST_INSERT(fssproc)				\
{								\
	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
	kmutex_t *lockp = &fss_listlock[index];			\
	fssproc_t *headp = &fss_listhead[index];		\
	mutex_enter(lockp);					\
	fssproc->fss_next = headp->fss_next;			\
	fssproc->fss_prev = headp;				\
	headp->fss_next->fss_prev = fssproc;			\
	headp->fss_next = fssproc;				\
	mutex_exit(lockp);					\
}

#define	FSS_LIST_DELETE(fssproc)				\
{								\
	int index = FSS_LIST_HASH(fssproc->fss_tp);		\
	kmutex_t *lockp = &fss_listlock[index];			\
	mutex_enter(lockp);					\
	fssproc->fss_prev->fss_next = fssproc->fss_next;	\
	fssproc->fss_next->fss_prev = fssproc->fss_prev;	\
	mutex_exit(lockp);					\
}

#define	FSS_TICK_COST	1000	/* tick cost for threads with nice level = 0 */

/*
 * Decay rate percentages are based on n/128 rather than n/100 so  that
 * calculations can avoid having to do an integer divide by 100 (divide
 * by FSS_DECAY_BASE == 128 optimizes to an arithmetic shift).
 *
 * FSS_DECAY_MIN	=  83/128 ~= 65%
 * FSS_DECAY_MAX	= 108/128 ~= 85%
 * FSS_DECAY_USG	=  96/128 ~= 75%
 */
#define	FSS_DECAY_MIN	83	/* fsspri decay pct for threads w/ nice -20 */
#define	FSS_DECAY_MAX	108	/* fsspri decay pct for threads w/ nice +19 */
#define	FSS_DECAY_USG	96	/* fssusage decay pct for projects */
#define	FSS_DECAY_BASE	128	/* base for decay percentages above */

#define	FSS_NICE_MIN	0
#define	FSS_NICE_MAX	(2 * NZERO - 1)
#define	FSS_NICE_RANGE	(FSS_NICE_MAX - FSS_NICE_MIN + 1)

static int	fss_nice_tick[FSS_NICE_RANGE];
static int	fss_nice_decay[FSS_NICE_RANGE];

static pri_t	fss_maxupri = FSS_MAXUPRI; /* maximum FSS user priority */
static pri_t	fss_maxumdpri; /* maximum user mode fss priority */
static pri_t	fss_maxglobpri;	/* maximum global priority used by fss class */
static pri_t	fss_minglobpri;	/* minimum global priority */

static fssproc_t fss_listhead[FSS_LISTS];
static kmutex_t	fss_listlock[FSS_LISTS];

static fsspset_t *fsspsets;
static kmutex_t fsspsets_lock;	/* protects fsspsets */

static id_t	fss_cid;

static time_t	fss_minrun = 2;	/* t_pri becomes 59 within 2 secs */
static time_t	fss_minslp = 2;	/* min time on sleep queue for hardswap */
static int	fss_quantum = 11;

static void	fss_newpri(fssproc_t *);
static void	fss_update(void *);
static int	fss_update_list(int);
static void	fss_change_priority(kthread_t *, fssproc_t *);

static int	fss_admin(caddr_t, cred_t *);
static int	fss_getclinfo(void *);
static int	fss_parmsin(void *);
static int	fss_parmsout(void *, pc_vaparms_t *);
static int	fss_vaparmsin(void *, pc_vaparms_t *);
static int	fss_vaparmsout(void *, pc_vaparms_t *);
static int	fss_getclpri(pcpri_t *);
static int	fss_alloc(void **, int);
static void	fss_free(void *);

static int	fss_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
static void	fss_exitclass(void *);
static int	fss_canexit(kthread_t *, cred_t *);
static int	fss_fork(kthread_t *, kthread_t *, void *);
static void	fss_forkret(kthread_t *, kthread_t *);
static void	fss_parmsget(kthread_t *, void *);
static int	fss_parmsset(kthread_t *, void *, id_t, cred_t *);
static void	fss_stop(kthread_t *, int, int);
static void	fss_exit(kthread_t *);
static void	fss_active(kthread_t *);
static void	fss_inactive(kthread_t *);
static pri_t	fss_swapin(kthread_t *, int);
static pri_t	fss_swapout(kthread_t *, int);
static void	fss_trapret(kthread_t *);
static void	fss_preempt(kthread_t *);
static void	fss_setrun(kthread_t *);
static void	fss_sleep(kthread_t *);
static void	fss_tick(kthread_t *);
static void	fss_wakeup(kthread_t *);
static int	fss_donice(kthread_t *, cred_t *, int, int *);
static int	fss_doprio(kthread_t *, cred_t *, int, int *);
static pri_t	fss_globpri(kthread_t *);
static void	fss_yield(kthread_t *);
static void	fss_nullsys();

static struct classfuncs fss_classfuncs = {
	/* class functions */
	fss_admin,
	fss_getclinfo,
	fss_parmsin,
	fss_parmsout,
	fss_vaparmsin,
	fss_vaparmsout,
	fss_getclpri,
	fss_alloc,
	fss_free,

	/* thread functions */
	fss_enterclass,
	fss_exitclass,
	fss_canexit,
	fss_fork,
	fss_forkret,
	fss_parmsget,
	fss_parmsset,
	fss_stop,
	fss_exit,
	fss_active,
	fss_inactive,
	fss_swapin,
	fss_swapout,
	fss_trapret,
	fss_preempt,
	fss_setrun,
	fss_sleep,
	fss_tick,
	fss_wakeup,
	fss_donice,
	fss_globpri,
	fss_nullsys,	/* set_process_group */
	fss_yield,
	fss_doprio,
};

int
_init()
{
	return (mod_install(&modlinkage));
}

int
_fini()
{
	return (EBUSY);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

/*ARGSUSED*/
static int
fss_project_walker(kproject_t *kpj, void *buf)
{
	return (0);
}

void *
fss_allocbuf(int op, int type)
{
	fssbuf_t *fssbuf;
	void **fsslist;
	int cnt;
	int i;
	size_t size;

	ASSERT(op == FSS_NPSET_BUF || op == FSS_NPROJ_BUF || op == FSS_ONE_BUF);
	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
	ASSERT(MUTEX_HELD(&cpu_lock));

	fssbuf = kmem_zalloc(sizeof (fssbuf_t), KM_SLEEP);
	switch (op) {
	case FSS_NPSET_BUF:
		cnt = cpupart_list(NULL, 0, CP_NONEMPTY);
		break;
	case FSS_NPROJ_BUF:
		cnt = project_walk_all(ALL_ZONES, fss_project_walker, NULL);
		break;
	case FSS_ONE_BUF:
		cnt = 1;
		break;
	}

	switch (type) {
	case FSS_ALLOC_PROJ:
		size = sizeof (fssproj_t);
		break;
	case FSS_ALLOC_ZONE:
		size = sizeof (fsszone_t);
		break;
	}
	fsslist = kmem_zalloc(cnt * sizeof (void *), KM_SLEEP);
	fssbuf->fssb_size = cnt;
	fssbuf->fssb_list = fsslist;
	for (i = 0; i < cnt; i++)
		fsslist[i] = kmem_zalloc(size, KM_SLEEP);
	return (fssbuf);
}

void
fss_freebuf(fssbuf_t *fssbuf, int type)
{
	void **fsslist;
	int i;
	size_t size;

	ASSERT(fssbuf != NULL);
	ASSERT(type == FSS_ALLOC_PROJ || type == FSS_ALLOC_ZONE);
	fsslist = fssbuf->fssb_list;

	switch (type) {
	case FSS_ALLOC_PROJ:
		size = sizeof (fssproj_t);
		break;
	case FSS_ALLOC_ZONE:
		size = sizeof (fsszone_t);
		break;
	}

	for (i = 0; i < fssbuf->fssb_size; i++) {
		if (fsslist[i] != NULL)
			kmem_free(fsslist[i], size);
	}
	kmem_free(fsslist, sizeof (void *) * fssbuf->fssb_size);
	kmem_free(fssbuf, sizeof (fssbuf_t));
}

static fsspset_t *
fss_find_fsspset(cpupart_t *cpupart)
{
	int i;
	fsspset_t *fsspset = NULL;
	int found = 0;

	ASSERT(cpupart != NULL);
	ASSERT(MUTEX_HELD(&fsspsets_lock));

	/*
	 * Search for the cpupart pointer in the array of fsspsets.
	 */
	for (i = 0; i < max_ncpus; i++) {
		fsspset = &fsspsets[i];
		if (fsspset->fssps_cpupart == cpupart) {
			ASSERT(fsspset->fssps_nproj > 0);
			found = 1;
			break;
		}
	}
	if (found == 0) {
		/*
		 * If we didn't find anything, then use the first
		 * available slot in the fsspsets array.
		 */
		for (i = 0; i < max_ncpus; i++) {
			fsspset = &fsspsets[i];
			if (fsspset->fssps_cpupart == NULL) {
				ASSERT(fsspset->fssps_nproj == 0);
				found = 1;
				break;
			}
		}
		fsspset->fssps_cpupart = cpupart;
	}
	ASSERT(found == 1);
	return (fsspset);
}

static void
fss_del_fsspset(fsspset_t *fsspset)
{
	ASSERT(MUTEX_HELD(&fsspsets_lock));
	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
	ASSERT(fsspset->fssps_nproj == 0);
	ASSERT(fsspset->fssps_list == NULL);
	ASSERT(fsspset->fssps_zones == NULL);
	fsspset->fssps_cpupart = NULL;
	fsspset->fssps_maxfsspri = 0;
	fsspset->fssps_shares = 0;
}

/*
 * The following routine returns a pointer to the fsszone structure which
 * belongs to zone "zone" and cpu partition fsspset, if such structure exists.
 */
static fsszone_t *
fss_find_fsszone(fsspset_t *fsspset, zone_t *zone)
{
	fsszone_t *fsszone;

	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));

	if (fsspset->fssps_list != NULL) {
		/*
		 * There are projects/zones active on this cpu partition
		 * already.  Try to find our zone among them.
		 */
		fsszone = fsspset->fssps_zones;
		do {
			if (fsszone->fssz_zone == zone) {
				return (fsszone);
			}
			fsszone = fsszone->fssz_next;
		} while (fsszone != fsspset->fssps_zones);
	}
	return (NULL);
}

/*
 * The following routine links new fsszone structure into doubly linked list of
 * zones active on the specified cpu partition.
 */
static void
fss_insert_fsszone(fsspset_t *fsspset, zone_t *zone, fsszone_t *fsszone)
{
	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));

	fsszone->fssz_zone = zone;
	fsszone->fssz_rshares = zone->zone_shares;

	if (fsspset->fssps_zones == NULL) {
		/*
		 * This will be the first fsszone for this fsspset
		 */
		fsszone->fssz_next = fsszone->fssz_prev = fsszone;
		fsspset->fssps_zones = fsszone;
	} else {
		/*
		 * Insert this fsszone to the doubly linked list.
		 */
		fsszone_t *fssz_head = fsspset->fssps_zones;

		fsszone->fssz_next = fssz_head;
		fsszone->fssz_prev = fssz_head->fssz_prev;
		fssz_head->fssz_prev->fssz_next = fsszone;
		fssz_head->fssz_prev = fsszone;
		fsspset->fssps_zones = fsszone;
	}
}

/*
 * The following routine removes a single fsszone structure from the doubly
 * linked list of zones active on the specified cpu partition.  Note that
 * global fsspsets_lock must be held in case this fsszone structure is the last
 * on the above mentioned list.  Also note that the fsszone structure is not
 * freed here, it is the responsibility of the caller to call kmem_free for it.
 */
static void
fss_remove_fsszone(fsspset_t *fsspset, fsszone_t *fsszone)
{
	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
	ASSERT(fsszone->fssz_nproj == 0);
	ASSERT(fsszone->fssz_shares == 0);
	ASSERT(fsszone->fssz_runnable == 0);

	if (fsszone->fssz_next != fsszone) {
		/*
		 * This is not the last zone in the list.
		 */
		fsszone->fssz_prev->fssz_next = fsszone->fssz_next;
		fsszone->fssz_next->fssz_prev = fsszone->fssz_prev;
		if (fsspset->fssps_zones == fsszone)
			fsspset->fssps_zones = fsszone->fssz_next;
	} else {
		/*
		 * This was the last zone active in this cpu partition.
		 */
		fsspset->fssps_zones = NULL;
	}
}

/*
 * The following routine returns a pointer to the fssproj structure
 * which belongs to project kpj and cpu partition fsspset, if such structure
 * exists.
 */
static fssproj_t *
fss_find_fssproj(fsspset_t *fsspset, kproject_t *kpj)
{
	fssproj_t *fssproj;

	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));

	if (fsspset->fssps_list != NULL) {
		/*
		 * There are projects running on this cpu partition already.
		 * Try to find our project among them.
		 */
		fssproj = fsspset->fssps_list;
		do {
			if (fssproj->fssp_proj == kpj) {
				ASSERT(fssproj->fssp_pset == fsspset);
				return (fssproj);
			}
			fssproj = fssproj->fssp_next;
		} while (fssproj != fsspset->fssps_list);
	}
	return (NULL);
}

/*
 * The following routine links new fssproj structure into doubly linked list
 * of projects running on the specified cpu partition.
 */
static void
fss_insert_fssproj(fsspset_t *fsspset, kproject_t *kpj, fsszone_t *fsszone,
    fssproj_t *fssproj)
{
	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));

	fssproj->fssp_pset = fsspset;
	fssproj->fssp_proj = kpj;
	fssproj->fssp_shares = kpj->kpj_shares;

	fsspset->fssps_nproj++;

	if (fsspset->fssps_list == NULL) {
		/*
		 * This will be the first fssproj for this fsspset
		 */
		fssproj->fssp_next = fssproj->fssp_prev = fssproj;
		fsspset->fssps_list = fssproj;
	} else {
		/*
		 * Insert this fssproj to the doubly linked list.
		 */
		fssproj_t *fssp_head = fsspset->fssps_list;

		fssproj->fssp_next = fssp_head;
		fssproj->fssp_prev = fssp_head->fssp_prev;
		fssp_head->fssp_prev->fssp_next = fssproj;
		fssp_head->fssp_prev = fssproj;
		fsspset->fssps_list = fssproj;
	}
	fssproj->fssp_fsszone = fsszone;
	fsszone->fssz_nproj++;
	ASSERT(fsszone->fssz_nproj != 0);
}

/*
 * The following routine removes a single fssproj structure from the doubly
 * linked list of projects running on the specified cpu partition.  Note that
 * global fsspsets_lock must be held in case if this fssproj structure is the
 * last on the above mentioned list.  Also note that the fssproj structure is
 * not freed here, it is the responsibility of the caller to call kmem_free
 * for it.
 */
static void
fss_remove_fssproj(fsspset_t *fsspset, fssproj_t *fssproj)
{
	fsszone_t *fsszone;

	ASSERT(MUTEX_HELD(&fsspsets_lock));
	ASSERT(MUTEX_HELD(&fsspset->fssps_lock));
	ASSERT(fssproj->fssp_runnable == 0);

	fsspset->fssps_nproj--;

	fsszone = fssproj->fssp_fsszone;
	fsszone->fssz_nproj--;

	if (fssproj->fssp_next != fssproj) {
		/*
		 * This is not the last part in the list.
		 */
		fssproj->fssp_prev->fssp_next = fssproj->fssp_next;
		fssproj->fssp_next->fssp_prev = fssproj->fssp_prev;
		if (fsspset->fssps_list == fssproj)
			fsspset->fssps_list = fssproj->fssp_next;
		if (fsszone->fssz_nproj == 0)
			fss_remove_fsszone(fsspset, fsszone);
	} else {
		/*
		 * This was the last project part running
		 * at this cpu partition.
		 */
		fsspset->fssps_list = NULL;
		ASSERT(fsspset->fssps_nproj == 0);
		ASSERT(fsszone->fssz_nproj == 0);
		fss_remove_fsszone(fsspset, fsszone);
		fss_del_fsspset(fsspset);
	}
}

static void
fss_inactive(kthread_t *t)
{
	fssproc_t *fssproc;
	fssproj_t *fssproj;
	fsspset_t *fsspset;
	fsszone_t *fsszone;

	ASSERT(THREAD_LOCK_HELD(t));
	fssproc = FSSPROC(t);
	fssproj = FSSPROC2FSSPROJ(fssproc);
	if (fssproj == NULL)	/* if this thread already exited */
		return;
	fsspset = FSSPROJ2FSSPSET(fssproj);
	fsszone = fssproj->fssp_fsszone;
	disp_lock_enter_high(&fsspset->fssps_displock);
	ASSERT(fssproj->fssp_runnable > 0);
	if (--fssproj->fssp_runnable == 0) {
		fsszone->fssz_shares -= fssproj->fssp_shares;
		if (--fsszone->fssz_runnable == 0)
			fsspset->fssps_shares -= fsszone->fssz_rshares;
	}
	ASSERT(fssproc->fss_runnable == 1);
	fssproc->fss_runnable = 0;
	disp_lock_exit_high(&fsspset->fssps_displock);
}

static void
fss_active(kthread_t *t)
{
	fssproc_t *fssproc;
	fssproj_t *fssproj;
	fsspset_t *fsspset;
	fsszone_t *fsszone;

	ASSERT(THREAD_LOCK_HELD(t));
	fssproc = FSSPROC(t);
	fssproj = FSSPROC2FSSPROJ(fssproc);
	if (fssproj == NULL)	/* if this thread already exited */
		return;
	fsspset = FSSPROJ2FSSPSET(fssproj);
	fsszone = fssproj->fssp_fsszone;
	disp_lock_enter_high(&fsspset->fssps_displock);
	if (++fssproj->fssp_runnable == 1) {
		fsszone->fssz_shares += fssproj->fssp_shares;
		if (++fsszone->fssz_runnable == 1)
			fsspset->fssps_shares += fsszone->fssz_rshares;
	}
	ASSERT(fssproc->fss_runnable == 0);
	fssproc->fss_runnable = 1;
	disp_lock_exit_high(&fsspset->fssps_displock);
}

/*
 * Fair share scheduler initialization. Called by dispinit() at boot time.
 * We can ignore clparmsz argument since we know that the smallest possible
 * parameter buffer is big enough for us.
 */
/*ARGSUSED*/
static pri_t
fss_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
{
	int i;

	ASSERT(MUTEX_HELD(&cpu_lock));

	fss_cid = cid;
	fss_maxumdpri = minclsyspri - 1;
	fss_maxglobpri = minclsyspri;
	fss_minglobpri = 0;
	fsspsets = kmem_zalloc(sizeof (fsspset_t) * max_ncpus, KM_SLEEP);

	/*
	 * Initialize the fssproc hash table.
	 */
	for (i = 0; i < FSS_LISTS; i++)
		fss_listhead[i].fss_next = fss_listhead[i].fss_prev =
		    &fss_listhead[i];

	*clfuncspp = &fss_classfuncs;

	/*
	 * Fill in fss_nice_tick and fss_nice_decay arrays:
	 * The cost of a tick is lower at positive nice values (so that it
	 * will not increase its project's usage as much as normal) with 50%
	 * drop at the maximum level and 50% increase at the minimum level.
	 * The fsspri decay is slower at positive nice values.  fsspri values
	 * of processes with negative nice levels must decay faster to receive
	 * time slices more frequently than normal.
	 */
	for (i = 0; i < FSS_NICE_RANGE; i++) {
		fss_nice_tick[i] = (FSS_TICK_COST * (((3 * FSS_NICE_RANGE) / 2)
		    - i)) / FSS_NICE_RANGE;
		fss_nice_decay[i] = FSS_DECAY_MIN +
		    ((FSS_DECAY_MAX - FSS_DECAY_MIN) * i) /
		    (FSS_NICE_RANGE - 1);
	}

	return (fss_maxglobpri);
}

/*
 * Calculate the new cpupri based on the usage, the number of shares and
 * the number of active threads.  Reset the tick counter for this thread.
 */
static void
fss_newpri(fssproc_t *fssproc)
{
	kthread_t *tp;
	fssproj_t *fssproj;
	fsspset_t *fsspset;
	fsszone_t *fsszone;
	fsspri_t fsspri, maxfsspri;
	pri_t invpri;
	uint32_t ticks;

	tp = fssproc->fss_tp;
	ASSERT(tp != NULL);

	if (tp->t_cid != fss_cid)
		return;

	ASSERT(THREAD_LOCK_HELD(tp));

	fssproj = FSSPROC2FSSPROJ(fssproc);
	fsszone = FSSPROJ2FSSZONE(fssproj);
	if (fssproj == NULL)
		/*
		 * No need to change priority of exited threads.
		 */
		return;

	fsspset = FSSPROJ2FSSPSET(fssproj);
	disp_lock_enter_high(&fsspset->fssps_displock);

	if (fssproj->fssp_shares == 0 || fsszone->fssz_rshares == 0) {
		/*
		 * Special case: threads with no shares.
		 */
		fssproc->fss_umdpri = fss_minglobpri;
		fssproc->fss_ticks = 0;
		disp_lock_exit_high(&fsspset->fssps_displock);
		return;
	}

	/*
	 * fsspri += shusage * nrunnable * ticks
	 */
	ticks = fssproc->fss_ticks;
	fssproc->fss_ticks = 0;
	fsspri = fssproc->fss_fsspri;
	fsspri += fssproj->fssp_shusage * fssproj->fssp_runnable * ticks;
	fssproc->fss_fsspri = fsspri;

	if (fsspri < fss_maxumdpri)
		fsspri = fss_maxumdpri;	/* so that maxfsspri is != 0 */

	/*
	 * The general priority formula:
	 *
	 *			(fsspri * umdprirange)
	 *   pri = maxumdpri - ------------------------
	 *				maxfsspri
	 *
	 * If this thread's fsspri is greater than the previous largest
	 * fsspri, then record it as the new high and priority for this
	 * thread will be one (the lowest priority assigned to a thread
	 * that has non-zero shares).
	 * Note that this formula cannot produce out of bounds priority
	 * values; if it is changed, additional checks may need  to  be
	 * added.
	 */
	maxfsspri = fsspset->fssps_maxfsspri;
	if (fsspri >= maxfsspri) {
		fsspset->fssps_maxfsspri = fsspri;
		disp_lock_exit_high(&fsspset->fssps_displock);
		fssproc->fss_umdpri = 1;
	} else {
		disp_lock_exit_high(&fsspset->fssps_displock);
		invpri = (fsspri * (fss_maxumdpri - 1)) / maxfsspri;
		fssproc->fss_umdpri = fss_maxumdpri - invpri;
	}
}

/*
 * Decays usages of all running projects and resets their tick counters.
 * Called once per second from fss_update() after updating priorities.
 */
static void
fss_decay_usage()
{
	uint32_t zone_ext_shares, zone_int_shares;
	uint32_t kpj_shares, pset_shares;
	fsspset_t *fsspset;
	fssproj_t *fssproj;
	fsszone_t *fsszone;
	fsspri_t maxfsspri;
	int psetid;
	struct zone *zp;

	mutex_enter(&fsspsets_lock);
	/*
	 * Go through all active processor sets and decay usages of projects
	 * running on them.
	 */
	for (psetid = 0; psetid < max_ncpus; psetid++) {
		fsspset = &fsspsets[psetid];
		mutex_enter(&fsspset->fssps_lock);

		fsspset->fssps_gen++;

		if (fsspset->fssps_cpupart == NULL ||
		    (fssproj = fsspset->fssps_list) == NULL) {
			mutex_exit(&fsspset->fssps_lock);
			continue;
		}

		/*
		 * Decay maxfsspri for this cpu partition with the
		 * fastest possible decay rate.
		 */
		disp_lock_enter(&fsspset->fssps_displock);

		maxfsspri = (fsspset->fssps_maxfsspri *
		    fss_nice_decay[NZERO]) / FSS_DECAY_BASE;
		if (maxfsspri < fss_maxumdpri)
			maxfsspri = fss_maxumdpri;
		fsspset->fssps_maxfsspri = maxfsspri;

		do {
			fsszone = fssproj->fssp_fsszone;
			zp = fsszone->fssz_zone;

			/*
			 * Reset zone's FSS kstats if they are from a
			 * previous cycle.
			 */
			if (fsspset->fssps_gen != zp->zone_fss_gen) {
				zp->zone_fss_gen = fsspset->fssps_gen;
				zp->zone_fss_pri_hi = 0;
				zp->zone_runq_cntr = 0;
				zp->zone_fss_shr_pct = 0;
				zp->zone_proc_cnt = 0;
			}

			/*
			 * Decay usage for each project running on
			 * this cpu partition.
			 */
			fssproj->fssp_usage =
			    (fssproj->fssp_usage * FSS_DECAY_USG) /
			    FSS_DECAY_BASE + fssproj->fssp_ticks;

			fssproj->fssp_ticks = 0;

			zp->zone_run_ticks += fssproj->fssp_zone_ticks;
			/*
			 * This is the count for this one second cycle only,
			 * and not cumulative.
			 */
			zp->zone_runq_cntr += fssproj->fssp_runnable;

			fssproj->fssp_zone_ticks = 0;

			/*
			 * Readjust the project's number of shares if it has
			 * changed since we checked it last time.
			 */
			kpj_shares = fssproj->fssp_proj->kpj_shares;
			if (fssproj->fssp_shares != kpj_shares) {
				if (fssproj->fssp_runnable != 0) {
					fsszone->fssz_shares -=
					    fssproj->fssp_shares;
					fsszone->fssz_shares += kpj_shares;
				}
				fssproj->fssp_shares = kpj_shares;
			}

			/*
			 * Readjust the zone's number of shares if it
			 * has changed since we checked it last time.
			 */
			zone_ext_shares = zp->zone_shares;
			if (fsszone->fssz_rshares != zone_ext_shares) {
				if (fsszone->fssz_runnable != 0) {
					fsspset->fssps_shares -=
					    fsszone->fssz_rshares;
					fsspset->fssps_shares +=
					    zone_ext_shares;
				}
				fsszone->fssz_rshares = zone_ext_shares;
			}
			zone_int_shares = fsszone->fssz_shares;
			pset_shares = fsspset->fssps_shares;

			if (zp->zone_runq_cntr > 0 && pset_shares > 0)
				/* in tenths of a pct */
				zp->zone_fss_shr_pct =
				    (zone_ext_shares * 1000) / pset_shares;

			/*
			 * Calculate fssp_shusage value to be used
			 * for fsspri increments for the next second.
			 */
			if (kpj_shares == 0 || zone_ext_shares == 0) {
				fssproj->fssp_shusage = 0;
			} else if (FSSPROJ2KPROJ(fssproj) == proj0p) {
				/*
				 * Project 0 in the global zone has 50%
				 * of its zone.
				 */
				fssproj->fssp_shusage = (fssproj->fssp_usage *
				    zone_int_shares * zone_int_shares) /
				    (zone_ext_shares * zone_ext_shares);
			} else {
				/*
				 * Thread's priority is based on its project's
				 * normalized usage (shusage) value which gets
				 * calculated this way:
				 *
				 *	   pset_shares^2    zone_int_shares^2
				 * usage * ------------- * ------------------
				 *	   kpj_shares^2	    zone_ext_shares^2
				 *
				 * Where zone_int_shares is the sum of shares
				 * of all active projects within the zone (and
				 * the pset), and zone_ext_shares is the number
				 * of zone shares (ie, zone.cpu-shares).
				 *
				 * If there is only one zone active on the pset
				 * the above reduces to:
				 *
				 * 			zone_int_shares^2
				 * shusage = usage * ---------------------
				 * 			kpj_shares^2
				 *
				 * If there's only one project active in the
				 * zone this formula reduces to:
				 *
				 *			pset_shares^2
				 * shusage = usage * ----------------------
				 *			zone_ext_shares^2
				 */
				fssproj->fssp_shusage = fssproj->fssp_usage *
				    pset_shares * zone_int_shares;
				fssproj->fssp_shusage /=
				    kpj_shares * zone_ext_shares;
				fssproj->fssp_shusage *=
				    pset_shares * zone_int_shares;
				fssproj->fssp_shusage /=
				    kpj_shares * zone_ext_shares;
			}
			fssproj = fssproj->fssp_next;
		} while (fssproj != fsspset->fssps_list);

		disp_lock_exit(&fsspset->fssps_displock);
		mutex_exit(&fsspset->fssps_lock);
	}
	mutex_exit(&fsspsets_lock);
}

static void
fss_change_priority(kthread_t *t, fssproc_t *fssproc)
{
	pri_t new_pri;

	ASSERT(THREAD_LOCK_HELD(t));
	new_pri = fssproc->fss_umdpri;
	ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);

	t->t_cpri = fssproc->fss_upri;
	fssproc->fss_flags &= ~FSSRESTORE;
	if (t == curthread || t->t_state == TS_ONPROC) {
		/*
		 * curthread is always onproc
		 */
		cpu_t *cp = t->t_disp_queue->disp_cpu;
		THREAD_CHANGE_PRI(t, new_pri);
		if (t == cp->cpu_dispthread)
			cp->cpu_dispatch_pri = DISP_PRIO(t);
		if (DISP_MUST_SURRENDER(t)) {
			fssproc->fss_flags |= FSSBACKQ;
			cpu_surrender(t);
		} else {
			fssproc->fss_timeleft = fss_quantum;
		}
	} else {
		/*
		 * When the priority of a thread is changed, it may be
		 * necessary to adjust its position on a sleep queue or
		 * dispatch queue.  The function thread_change_pri accomplishes
		 * this.
		 */
		if (thread_change_pri(t, new_pri, 0)) {
			/*
			 * The thread was on a run queue.
			 */
			fssproc->fss_timeleft = fss_quantum;
		} else {
			fssproc->fss_flags |= FSSBACKQ;
		}
	}
}

/*
 * Update priorities of all fair-sharing threads that are currently runnable
 * at a user mode priority based on the number of shares and current usage.
 * Called once per second via timeout which we reset here.
 *
 * There are several lists of fair-sharing threads broken up by a hash on the
 * thread pointer.  Each list has its own lock.  This avoids blocking all
 * fss_enterclass, fss_fork, and fss_exitclass operations while fss_update runs.
 * fss_update traverses each list in turn.
 */
static void
fss_update(void *arg)
{
	int i;
	int new_marker = -1;
	static int fss_update_marker;

	/*
	 * Decay and update usages for all projects.
	 */
	fss_decay_usage();

	/*
	 * Start with the fss_update_marker list, then do the rest.
	 */
	i = fss_update_marker;

	/*
	 * Go around all threads, set new priorities and decay
	 * per-thread CPU usages.
	 */
	do {
		/*
		 * If this is the first list after the current marker to have
		 * threads with priorities updates, advance the marker to this
		 * list for the next time fss_update runs.
		 */
		if (fss_update_list(i) &&
		    new_marker == -1 && i != fss_update_marker)
			new_marker = i;
	} while ((i = FSS_LIST_NEXT(i)) != fss_update_marker);

	/*
	 * Advance marker for the next fss_update call
	 */
	if (new_marker != -1)
		fss_update_marker = new_marker;

	(void) timeout(fss_update, arg, hz);
}

/*
 * Updates priority for a list of threads.  Returns 1 if the priority of one
 * of the threads was actually updated, 0 if none were for various reasons
 * (thread is no longer in the FSS class, is not runnable, has the preemption
 * control no-preempt bit set, etc.)
 */
static int
fss_update_list(int i)
{
	fssproc_t *fssproc;
	fssproj_t *fssproj;
	fsspri_t fsspri;
	struct zone *zp;
	pri_t fss_umdpri;
	kthread_t *t;
	int updated = 0;

	mutex_enter(&fss_listlock[i]);
	for (fssproc = fss_listhead[i].fss_next; fssproc != &fss_listhead[i];
	    fssproc = fssproc->fss_next) {
		t = fssproc->fss_tp;
		/*
		 * Lock the thread and verify the state.
		 */
		thread_lock(t);
		/*
		 * Skip the thread if it is no longer in the FSS class or
		 * is running with kernel mode priority.
		 */
		if (t->t_cid != fss_cid)
			goto next;
		if ((fssproc->fss_flags & FSSKPRI) != 0)
			goto next;

		fssproj = FSSPROC2FSSPROJ(fssproc);
		if (fssproj == NULL)
			goto next;

		if (fssproj->fssp_shares != 0) {
			/*
			 * Decay fsspri value.
			 */
			fsspri = fssproc->fss_fsspri;
			fsspri = (fsspri * fss_nice_decay[fssproc->fss_nice]) /
			    FSS_DECAY_BASE;
			fssproc->fss_fsspri = fsspri;
		}

		if (t->t_schedctl && schedctl_get_nopreempt(t))
			goto next;
		if (t->t_state != TS_RUN && t->t_state != TS_WAIT) {
			/*
			 * Make next syscall/trap call fss_trapret
			 */
			t->t_trapret = 1;
			aston(t);
			goto next;
		}
		fss_newpri(fssproc);
		updated = 1;

		fss_umdpri = fssproc->fss_umdpri;

		/*
		 * Summarize a zone's process priorities for runnable
		 * procs.
		 */
		zp = fssproj->fssp_fsszone->fssz_zone;

		if (fss_umdpri > zp->zone_fss_pri_hi)
			zp->zone_fss_pri_hi = fss_umdpri;

		if (zp->zone_proc_cnt++ == 0)
			zp->zone_fss_pri_avg = fss_umdpri;
		else
			zp->zone_fss_pri_avg =
			    (zp->zone_fss_pri_avg + fss_umdpri) / 2;

		/*
		 * Only dequeue the thread if it needs to be moved; otherwise
		 * it should just round-robin here.
		 */
		if (t->t_pri != fss_umdpri)
			fss_change_priority(t, fssproc);
next:
		thread_unlock(t);
	}
	mutex_exit(&fss_listlock[i]);
	return (updated);
}

/*ARGSUSED*/
static int
fss_admin(caddr_t uaddr, cred_t *reqpcredp)
{
	fssadmin_t fssadmin;

	if (copyin(uaddr, &fssadmin, sizeof (fssadmin_t)))
		return (EFAULT);

	switch (fssadmin.fss_cmd) {
	case FSS_SETADMIN:
		if (secpolicy_dispadm(reqpcredp) != 0)
			return (EPERM);
		if (fssadmin.fss_quantum <= 0 || fssadmin.fss_quantum >= hz)
			return (EINVAL);
		fss_quantum = fssadmin.fss_quantum;
		break;
	case FSS_GETADMIN:
		fssadmin.fss_quantum = fss_quantum;
		if (copyout(&fssadmin, uaddr, sizeof (fssadmin_t)))
			return (EFAULT);
		break;
	default:
		return (EINVAL);
	}
	return (0);
}

static int
fss_getclinfo(void *infop)
{
	fssinfo_t *fssinfo = (fssinfo_t *)infop;
	fssinfo->fss_maxupri = fss_maxupri;
	return (0);
}

static int
fss_parmsin(void *parmsp)
{
	fssparms_t *fssparmsp = (fssparms_t *)parmsp;

	/*
	 * Check validity of parameters.
	 */
	if ((fssparmsp->fss_uprilim > fss_maxupri ||
	    fssparmsp->fss_uprilim < -fss_maxupri) &&
	    fssparmsp->fss_uprilim != FSS_NOCHANGE)
		return (EINVAL);

	if ((fssparmsp->fss_upri > fss_maxupri ||
	    fssparmsp->fss_upri < -fss_maxupri) &&
	    fssparmsp->fss_upri != FSS_NOCHANGE)
		return (EINVAL);

	return (0);
}

/*ARGSUSED*/
static int
fss_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
{
	return (0);
}

static int
fss_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
{
	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
	int priflag = 0;
	int limflag = 0;
	uint_t cnt;
	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];

	/*
	 * FSS_NOCHANGE (-32768) is outside of the range of values for
	 * fss_uprilim and fss_upri.  If the structure fssparms_t is changed,
	 * FSS_NOCHANGE should be replaced by a flag word.
	 */
	fssparmsp->fss_uprilim = FSS_NOCHANGE;
	fssparmsp->fss_upri = FSS_NOCHANGE;

	/*
	 * Get the varargs parameter and check validity of parameters.
	 */
	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
		return (EINVAL);

	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
		switch (vpp->pc_key) {
		case FSS_KY_UPRILIM:
			if (limflag++)
				return (EINVAL);
			fssparmsp->fss_uprilim = (pri_t)vpp->pc_parm;
			if (fssparmsp->fss_uprilim > fss_maxupri ||
			    fssparmsp->fss_uprilim < -fss_maxupri)
				return (EINVAL);
			break;
		case FSS_KY_UPRI:
			if (priflag++)
				return (EINVAL);
			fssparmsp->fss_upri = (pri_t)vpp->pc_parm;
			if (fssparmsp->fss_upri > fss_maxupri ||
			    fssparmsp->fss_upri < -fss_maxupri)
				return (EINVAL);
			break;
		default:
			return (EINVAL);
		}
	}

	if (vaparmsp->pc_vaparmscnt == 0) {
		/*
		 * Use default parameters.
		 */
		fssparmsp->fss_upri = fssparmsp->fss_uprilim = 0;
	}

	return (0);
}

/*
 * Copy all selected fair-sharing class parameters to the user.  The parameters
 * are specified by a key.
 */
static int
fss_vaparmsout(void *parmsp, pc_vaparms_t *vaparmsp)
{
	fssparms_t *fssparmsp = (fssparms_t *)parmsp;
	int priflag = 0;
	int limflag = 0;
	uint_t cnt;
	pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];

	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));

	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
		return (EINVAL);

	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
		switch (vpp->pc_key) {
		case FSS_KY_UPRILIM:
			if (limflag++)
				return (EINVAL);
			if (copyout(&fssparmsp->fss_uprilim,
			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
				return (EFAULT);
			break;
		case FSS_KY_UPRI:
			if (priflag++)
				return (EINVAL);
			if (copyout(&fssparmsp->fss_upri,
			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
				return (EFAULT);
			break;
		default:
			return (EINVAL);
		}
	}

	return (0);
}

/*
 * Return the user mode scheduling priority range.
 */
static int
fss_getclpri(pcpri_t *pcprip)
{
	pcprip->pc_clpmax = fss_maxupri;
	pcprip->pc_clpmin = -fss_maxupri;
	return (0);
}

static int
fss_alloc(void **p, int flag)
{
	void *bufp;

	if ((bufp = kmem_zalloc(sizeof (fssproc_t), flag)) == NULL) {
		return (ENOMEM);
	} else {
		*p = bufp;
		return (0);
	}
}

static void
fss_free(void *bufp)
{
	if (bufp)
		kmem_free(bufp, sizeof (fssproc_t));
}

/*
 * Thread functions
 */
static int
fss_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
    void *bufp)
{
	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;
	fssproc_t	*fssproc;
	pri_t		reqfssuprilim;
	pri_t		reqfssupri;
	static uint32_t fssexists = 0;
	fsspset_t	*fsspset;
	fssproj_t	*fssproj;
	fsszone_t	*fsszone;
	kproject_t	*kpj;
	zone_t		*zone;
	int		fsszone_allocated = 0;

	fssproc = (fssproc_t *)bufp;
	ASSERT(fssproc != NULL);

	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

	/*
	 * Only root can move threads to FSS class.
	 */
	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
		return (EPERM);
	/*
	 * Initialize the fssproc structure.
	 */
	fssproc->fss_umdpri = fss_maxumdpri / 2;

	if (fssparmsp == NULL) {
		/*
		 * Use default values.
		 */
		fssproc->fss_nice = NZERO;
		fssproc->fss_uprilim = fssproc->fss_upri = 0;
	} else {
		/*
		 * Use supplied values.
		 */
		if (fssparmsp->fss_uprilim == FSS_NOCHANGE) {
			reqfssuprilim = 0;
		} else {
			if (fssparmsp->fss_uprilim > 0 &&
			    secpolicy_setpriority(reqpcredp) != 0)
				return (EPERM);
			reqfssuprilim = fssparmsp->fss_uprilim;
		}
		if (fssparmsp->fss_upri == FSS_NOCHANGE) {
			reqfssupri = reqfssuprilim;
		} else {
			if (fssparmsp->fss_upri > 0 &&
			    secpolicy_setpriority(reqpcredp) != 0)
				return (EPERM);
			/*
			 * Set the user priority to the requested value or
			 * the upri limit, whichever is lower.
			 */
			reqfssupri = fssparmsp->fss_upri;
			if (reqfssupri > reqfssuprilim)
				reqfssupri = reqfssuprilim;
		}
		fssproc->fss_uprilim = reqfssuprilim;
		fssproc->fss_upri = reqfssupri;
		fssproc->fss_nice = NZERO - (NZERO * reqfssupri) / fss_maxupri;
		if (fssproc->fss_nice > FSS_NICE_MAX)
			fssproc->fss_nice = FSS_NICE_MAX;
	}

	fssproc->fss_timeleft = fss_quantum;
	fssproc->fss_tp = t;
	cpucaps_sc_init(&fssproc->fss_caps);

	/*
	 * Put a lock on our fsspset structure.
	 */
	mutex_enter(&fsspsets_lock);
	fsspset = fss_find_fsspset(t->t_cpupart);
	mutex_enter(&fsspset->fssps_lock);
	mutex_exit(&fsspsets_lock);

	zone = ttoproc(t)->p_zone;
	if ((fsszone = fss_find_fsszone(fsspset, zone)) == NULL) {
		if ((fsszone = kmem_zalloc(sizeof (fsszone_t), KM_NOSLEEP))
		    == NULL) {
			mutex_exit(&fsspset->fssps_lock);
			return (ENOMEM);
		} else {
			fsszone_allocated = 1;
			fss_insert_fsszone(fsspset, zone, fsszone);
		}
	}
	kpj = ttoproj(t);
	if ((fssproj = fss_find_fssproj(fsspset, kpj)) == NULL) {
		if ((fssproj = kmem_zalloc(sizeof (fssproj_t), KM_NOSLEEP))
		    == NULL) {
			if (fsszone_allocated) {
				fss_remove_fsszone(fsspset, fsszone);
				kmem_free(fsszone, sizeof (fsszone_t));
			}
			mutex_exit(&fsspset->fssps_lock);
			return (ENOMEM);
		} else {
			fss_insert_fssproj(fsspset, kpj, fsszone, fssproj);
		}
	}
	fssproj->fssp_threads++;
	fssproc->fss_proj = fssproj;

	/*
	 * Reset priority. Process goes to a "user mode" priority here
	 * regardless of whether or not it has slept since entering the kernel.
	 */
	thread_lock(t);
	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
	t->t_cid = cid;
	t->t_cldata = (void *)fssproc;
	t->t_schedflag |= TS_RUNQMATCH;
	fss_change_priority(t, fssproc);
	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
	    t->t_state == TS_WAIT)
		fss_active(t);
	thread_unlock(t);

	mutex_exit(&fsspset->fssps_lock);

	/*
	 * Link new structure into fssproc list.
	 */
	FSS_LIST_INSERT(fssproc);

	/*
	 * If this is the first fair-sharing thread to occur since boot,
	 * we set up the initial call to fss_update() here. Use an atomic
	 * compare-and-swap since that's easier and faster than a mutex
	 * (but check with an ordinary load first since most of the time
	 * this will already be done).
	 */
	if (fssexists == 0 && cas32(&fssexists, 0, 1) == 0)
		(void) timeout(fss_update, NULL, hz);

	return (0);
}

/*
 * Remove fssproc_t from the list.
 */
static void
fss_exitclass(void *procp)
{
	fssproc_t *fssproc = (fssproc_t *)procp;
	fssproj_t *fssproj;
	fsspset_t *fsspset;
	fsszone_t *fsszone;
	kthread_t *t = fssproc->fss_tp;

	/*
	 * We should be either getting this thread off the deathrow or
	 * this thread has already moved to another scheduling class and
	 * we're being called with its old cldata buffer pointer.  In both
	 * cases, the content of this buffer can not be changed while we're
	 * here.
	 */
	mutex_enter(&fsspsets_lock);
	thread_lock(t);
	if (t->t_cid != fss_cid) {
		/*
		 * We're being called as a result of the priocntl() system
		 * call -- someone is trying to move our thread to another
		 * scheduling class. We can't call fss_inactive() here
		 * because our thread's t_cldata pointer already points
		 * to another scheduling class specific data.
		 */
		ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

		fssproj = FSSPROC2FSSPROJ(fssproc);
		fsspset = FSSPROJ2FSSPSET(fssproj);
		fsszone = fssproj->fssp_fsszone;

		if (fssproc->fss_runnable) {
			disp_lock_enter_high(&fsspset->fssps_displock);
			if (--fssproj->fssp_runnable == 0) {
				fsszone->fssz_shares -= fssproj->fssp_shares;
				if (--fsszone->fssz_runnable == 0)
					fsspset->fssps_shares -=
					    fsszone->fssz_rshares;
			}
			disp_lock_exit_high(&fsspset->fssps_displock);
		}
		thread_unlock(t);

		mutex_enter(&fsspset->fssps_lock);
		if (--fssproj->fssp_threads == 0) {
			fss_remove_fssproj(fsspset, fssproj);
			if (fsszone->fssz_nproj == 0)
				kmem_free(fsszone, sizeof (fsszone_t));
			kmem_free(fssproj, sizeof (fssproj_t));
		}
		mutex_exit(&fsspset->fssps_lock);

	} else {
		ASSERT(t->t_state == TS_FREE);
		/*
		 * We're being called from thread_free() when our thread
		 * is removed from the deathrow. There is nothing we need
		 * do here since everything should've been done earlier
		 * in fss_exit().
		 */
		thread_unlock(t);
	}
	mutex_exit(&fsspsets_lock);

	FSS_LIST_DELETE(fssproc);
	fss_free(fssproc);
}

/*ARGSUSED*/
static int
fss_canexit(kthread_t *t, cred_t *credp)
{
	/*
	 * A thread is allowed to exit FSS only if we have sufficient
	 * privileges.
	 */
	if (credp != NULL && secpolicy_setpriority(credp) != 0)
		return (EPERM);
	else
		return (0);
}

/*
 * Initialize fair-share class specific proc structure for a child.
 */
static int
fss_fork(kthread_t *pt, kthread_t *ct, void *bufp)
{
	fssproc_t *pfssproc;	/* ptr to parent's fssproc structure	*/
	fssproc_t *cfssproc;	/* ptr to child's fssproc structure	*/
	fssproj_t *fssproj;
	fsspset_t *fsspset;

	ASSERT(MUTEX_HELD(&ttoproc(pt)->p_lock));
	ASSERT(ct->t_state == TS_STOPPED);

	cfssproc = (fssproc_t *)bufp;
	ASSERT(cfssproc != NULL);
	bzero(cfssproc, sizeof (fssproc_t));

	thread_lock(pt);
	pfssproc = FSSPROC(pt);
	fssproj = FSSPROC2FSSPROJ(pfssproc);
	fsspset = FSSPROJ2FSSPSET(fssproj);
	thread_unlock(pt);

	mutex_enter(&fsspset->fssps_lock);
	/*
	 * Initialize child's fssproc structure.
	 */
	thread_lock(pt);
	ASSERT(FSSPROJ(pt) == fssproj);
	cfssproc->fss_proj = fssproj;
	cfssproc->fss_timeleft = fss_quantum;
	cfssproc->fss_umdpri = pfssproc->fss_umdpri;
	cfssproc->fss_fsspri = 0;
	cfssproc->fss_uprilim = pfssproc->fss_uprilim;
	cfssproc->fss_upri = pfssproc->fss_upri;
	cfssproc->fss_tp = ct;
	cfssproc->fss_nice = pfssproc->fss_nice;
	cpucaps_sc_init(&cfssproc->fss_caps);

	cfssproc->fss_flags =
	    pfssproc->fss_flags & ~(FSSKPRI | FSSBACKQ | FSSRESTORE);
	ct->t_cldata = (void *)cfssproc;
	ct->t_schedflag |= TS_RUNQMATCH;
	thread_unlock(pt);

	fssproj->fssp_threads++;
	mutex_exit(&fsspset->fssps_lock);

	/*
	 * Link new structure into fssproc hash table.
	 */
	FSS_LIST_INSERT(cfssproc);
	return (0);
}

/*
 * Child is placed at back of dispatcher queue and parent gives up processor
 * so that the child runs first after the fork. This allows the child
 * immediately execing to break the multiple use of copy on write pages with no
 * disk home. The parent will get to steal them back rather than uselessly
 * copying them.
 */
static void
fss_forkret(kthread_t *t, kthread_t *ct)
{
	proc_t *pp = ttoproc(t);
	proc_t *cp = ttoproc(ct);
	fssproc_t *fssproc;

	ASSERT(t == curthread);
	ASSERT(MUTEX_HELD(&pidlock));

	/*
	 * Grab the child's p_lock before dropping pidlock to ensure the
	 * process does not disappear before we set it running.
	 */
	mutex_enter(&cp->p_lock);
	continuelwps(cp);
	mutex_exit(&cp->p_lock);

	mutex_enter(&pp->p_lock);
	mutex_exit(&pidlock);
	continuelwps(pp);

	thread_lock(t);

	fssproc = FSSPROC(t);
	fss_newpri(fssproc);
	fssproc->fss_timeleft = fss_quantum;
	t->t_pri = fssproc->fss_umdpri;
	ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
	fssproc->fss_flags &= ~FSSKPRI;
	THREAD_TRANSITION(t);

	/*
	 * We don't want to call fss_setrun(t) here because it may call
	 * fss_active, which we don't need.
	 */
	fssproc->fss_flags &= ~FSSBACKQ;

	if (t->t_disp_time != ddi_get_lbolt())
		setbackdq(t);
	else
		setfrontdq(t);

	thread_unlock(t);
	/*
	 * Safe to drop p_lock now since it is safe to change
	 * the scheduling class after this point.
	 */
	mutex_exit(&pp->p_lock);

	swtch();
}

/*
 * Get the fair-sharing parameters of the thread pointed to by fssprocp into
 * the buffer pointed by fssparmsp.
 */
static void
fss_parmsget(kthread_t *t, void *parmsp)
{
	fssproc_t *fssproc = FSSPROC(t);
	fssparms_t *fssparmsp = (fssparms_t *)parmsp;

	fssparmsp->fss_uprilim = fssproc->fss_uprilim;
	fssparmsp->fss_upri = fssproc->fss_upri;
}

/*ARGSUSED*/
static int
fss_parmsset(kthread_t *t, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
{
	char		nice;
	pri_t		reqfssuprilim;
	pri_t		reqfssupri;
	fssproc_t	*fssproc = FSSPROC(t);
	fssparms_t	*fssparmsp = (fssparms_t *)parmsp;

	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));

	if (fssparmsp->fss_uprilim == FSS_NOCHANGE)
		reqfssuprilim = fssproc->fss_uprilim;
	else
		reqfssuprilim = fssparmsp->fss_uprilim;

	if (fssparmsp->fss_upri == FSS_NOCHANGE)
		reqfssupri = fssproc->fss_upri;
	else
		reqfssupri = fssparmsp->fss_upri;

	/*
	 * Make sure the user priority doesn't exceed the upri limit.
	 */
	if (reqfssupri > reqfssuprilim)
		reqfssupri = reqfssuprilim;

	/*
	 * Basic permissions enforced by generic kernel code for all classes
	 * require that a thread attempting to change the scheduling parameters
	 * of a target thread be privileged or have a real or effective UID
	 * matching that of the target thread. We are not called unless these
	 * basic permission checks have already passed. The fair-sharing class
	 * requires in addition that the calling thread be privileged if it
	 * is attempting to raise the upri limit above its current value.
	 * This may have been checked previously but if our caller passed us
	 * a non-NULL credential pointer we assume it hasn't and we check it
	 * here.
	 */
	if ((reqpcredp != NULL) &&
	    (reqfssuprilim > fssproc->fss_uprilim) &&
	    secpolicy_setpriority(reqpcredp) != 0)
		return (EPERM);

	/*
	 * Set fss_nice to the nice value corresponding to the user priority we
	 * are setting.  Note that setting the nice field of the parameter
	 * struct won't affect upri or nice.
	 */
	nice = NZERO - (reqfssupri * NZERO) / fss_maxupri;
	if (nice > FSS_NICE_MAX)
		nice = FSS_NICE_MAX;

	thread_lock(t);

	fssproc->fss_uprilim = reqfssuprilim;
	fssproc->fss_upri = reqfssupri;
	fssproc->fss_nice = nice;
	fss_newpri(fssproc);

	if ((fssproc->fss_flags & FSSKPRI) != 0) {
		thread_unlock(t);
		return (0);
	}

	fss_change_priority(t, fssproc);
	thread_unlock(t);
	return (0);

}

/*
 * The thread is being stopped.
 */
/*ARGSUSED*/
static void
fss_stop(kthread_t *t, int why, int what)
{
	ASSERT(THREAD_LOCK_HELD(t));
	ASSERT(t == curthread);

	fss_inactive(t);
}

/*
 * The current thread is exiting, do necessary adjustments to its project
 */
static void
fss_exit(kthread_t *t)
{
	fsspset_t *fsspset;
	fssproj_t *fssproj;
	fssproc_t *fssproc;
	fsszone_t *fsszone;
	int free = 0;

	/*
	 * Thread t here is either a current thread (in which case we hold
	 * its process' p_lock), or a thread being destroyed by forklwp_fail(),
	 * in which case we hold pidlock and thread is no longer on the
	 * thread list.
	 */
	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock) || MUTEX_HELD(&pidlock));

	fssproc = FSSPROC(t);
	fssproj = FSSPROC2FSSPROJ(fssproc);
	fsspset = FSSPROJ2FSSPSET(fssproj);
	fsszone = fssproj->fssp_fsszone;

	mutex_enter(&fsspsets_lock);
	mutex_enter(&fsspset->fssps_lock);

	thread_lock(t);
	disp_lock_enter_high(&fsspset->fssps_displock);
	if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
		if (--fssproj->fssp_runnable == 0) {
			fsszone->fssz_shares -= fssproj->fssp_shares;
			if (--fsszone->fssz_runnable == 0)
				fsspset->fssps_shares -= fsszone->fssz_rshares;
		}
		ASSERT(fssproc->fss_runnable == 1);
		fssproc->fss_runnable = 0;
	}
	if (--fssproj->fssp_threads == 0) {
		fss_remove_fssproj(fsspset, fssproj);
		free = 1;
	}
	disp_lock_exit_high(&fsspset->fssps_displock);
	fssproc->fss_proj = NULL;	/* mark this thread as already exited */
	thread_unlock(t);

	if (free) {
		if (fsszone->fssz_nproj == 0)
			kmem_free(fsszone, sizeof (fsszone_t));
		kmem_free(fssproj, sizeof (fssproj_t));
	}
	mutex_exit(&fsspset->fssps_lock);
	mutex_exit(&fsspsets_lock);

	/*
	 * A thread could be exiting in between clock ticks, so we need to
	 * calculate how much CPU time it used since it was charged last time.
	 *
	 * CPU caps are not enforced on exiting processes - it is usually
	 * desirable to exit as soon as possible to free resources.
	 */
	if (CPUCAPS_ON()) {
		thread_lock(t);
		fssproc = FSSPROC(t);
		(void) cpucaps_charge(t, &fssproc->fss_caps,
		    CPUCAPS_CHARGE_ONLY);
		thread_unlock(t);
	}
}

static void
fss_nullsys()
{
}

/*
 * fss_swapin() returns -1 if the thread is loaded or is not eligible to be
 * swapped in. Otherwise, it returns the thread's effective priority based
 * on swapout time and size of process (0 <= epri <= 0 SHRT_MAX).
 */
/*ARGSUSED*/
static pri_t
fss_swapin(kthread_t *t, int flags)
{
	fssproc_t *fssproc = FSSPROC(t);
	long epri = -1;
	proc_t *pp = ttoproc(t);

	ASSERT(THREAD_LOCK_HELD(t));

	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
		time_t swapout_time;

		swapout_time = (ddi_get_lbolt() - t->t_stime) / hz;
		if (INHERITED(t) || (fssproc->fss_flags & FSSKPRI)) {
			epri = (long)DISP_PRIO(t) + swapout_time;
		} else {
			/*
			 * Threads which have been out for a long time,
			 * have high user mode priority and are associated
			 * with a small address space are more deserving.
			 */
			epri = fssproc->fss_umdpri;
			ASSERT(epri >= 0 && epri <= fss_maxumdpri);
			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
		}
		/*
		 * Scale epri so that SHRT_MAX / 2 represents zero priority.
		 */
		epri += SHRT_MAX / 2;
		if (epri < 0)
			epri = 0;
		else if (epri > SHRT_MAX)
			epri = SHRT_MAX;
	}
	return ((pri_t)epri);
}

/*
 * fss_swapout() returns -1 if the thread isn't loaded or is not eligible to
 * be swapped out. Otherwise, it returns the thread's effective priority
 * based on if the swapper is in softswap or hardswap mode.
 */
static pri_t
fss_swapout(kthread_t *t, int flags)
{
	fssproc_t *fssproc = FSSPROC(t);
	long epri = -1;
	proc_t *pp = ttoproc(t);
	time_t swapin_time;

	ASSERT(THREAD_LOCK_HELD(t));

	if (INHERITED(t) ||
	    (fssproc->fss_flags & FSSKPRI) ||
	    (t->t_proc_flag & TP_LWPEXIT) ||
	    (t->t_state & (TS_ZOMB|TS_FREE|TS_STOPPED|TS_ONPROC|TS_WAIT)) ||
	    !(t->t_schedflag & TS_LOAD) ||
	    !(SWAP_OK(t)))
		return (-1);

	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));

	swapin_time = (ddi_get_lbolt() - t->t_stime) / hz;

	if (flags == SOFTSWAP) {
		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
			epri = 0;
		} else {
			return ((pri_t)epri);
		}
	} else {
		pri_t pri;

		if ((t->t_state == TS_SLEEP && swapin_time > fss_minslp) ||
		    (t->t_state == TS_RUN && swapin_time > fss_minrun)) {
			pri = fss_maxumdpri;
			epri = swapin_time -
			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
		} else {
			return ((pri_t)epri);
		}
	}

	/*
	 * Scale epri so that SHRT_MAX / 2 represents zero priority.
	 */
	epri += SHRT_MAX / 2;
	if (epri < 0)
		epri = 0;
	else if (epri > SHRT_MAX)
		epri = SHRT_MAX;

	return ((pri_t)epri);
}

/*
 * If thread is currently at a kernel mode priority (has slept) and is
 * returning to the userland we assign it the appropriate user mode priority
 * and time quantum here.  If we're lowering the thread's priority below that
 * of other runnable threads then we will set runrun via cpu_surrender() to
 * cause preemption.
 */
static void
fss_trapret(kthread_t *t)
{
	fssproc_t *fssproc = FSSPROC(t);
	cpu_t *cp = CPU;

	ASSERT(THREAD_LOCK_HELD(t));
	ASSERT(t == curthread);
	ASSERT(cp->cpu_dispthread == t);
	ASSERT(t->t_state == TS_ONPROC);

	t->t_kpri_req = 0;
	if (fssproc->fss_flags & FSSKPRI) {
		/*
		 * If thread has blocked in the kernel
		 */
		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
		cp->cpu_dispatch_pri = DISP_PRIO(t);
		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
		fssproc->fss_flags &= ~FSSKPRI;

		if (DISP_MUST_SURRENDER(t))
			cpu_surrender(t);
	}

	/*
	 * Swapout lwp if the swapper is waiting for this thread to reach
	 * a safe point.
	 */
	if (t->t_schedflag & TS_SWAPENQ) {
		thread_unlock(t);
		swapout_lwp(ttolwp(t));
		thread_lock(t);
	}
}

/*
 * Arrange for thread to be placed in appropriate location on dispatcher queue.
 * This is called with the current thread in TS_ONPROC and locked.
 */
static void
fss_preempt(kthread_t *t)
{
	fssproc_t *fssproc = FSSPROC(t);
	klwp_t *lwp;
	uint_t flags;

	ASSERT(t == curthread);
	ASSERT(THREAD_LOCK_HELD(curthread));
	ASSERT(t->t_state == TS_ONPROC);

	/*
	 * If preempted in the kernel, make sure the thread has a kernel
	 * priority if needed.
	 */
	lwp = curthread->t_lwp;
	if (!(fssproc->fss_flags & FSSKPRI) && lwp != NULL && t->t_kpri_req) {
		fssproc->fss_flags |= FSSKPRI;
		THREAD_CHANGE_PRI(t, minclsyspri);
		ASSERT(t->t_pri >= 0 && t->t_pri <= fss_maxglobpri);
		t->t_trapret = 1;	/* so that fss_trapret will run */
		aston(t);
	}

	/*
	 * This thread may be placed on wait queue by CPU Caps. In this case we
	 * do not need to do anything until it is removed from the wait queue.
	 * Do not enforce CPU caps on threads running at a kernel priority
	 */
	if (CPUCAPS_ON()) {
		(void) cpucaps_charge(t, &fssproc->fss_caps,
		    CPUCAPS_CHARGE_ENFORCE);

		if (!(fssproc->fss_flags & FSSKPRI) && CPUCAPS_ENFORCE(t))
			return;
	}

	/*
	 * If preempted in user-land mark the thread as swappable because it
	 * cannot be holding any kernel locks.
	 */
	ASSERT(t->t_schedflag & TS_DONT_SWAP);
	if (lwp != NULL && lwp->lwp_state == LWP_USER)
		t->t_schedflag &= ~TS_DONT_SWAP;

	/*
	 * Check to see if we're doing "preemption control" here.  If
	 * we are, and if the user has requested that this thread not
	 * be preempted, and if preemptions haven't been put off for
	 * too long, let the preemption happen here but try to make
	 * sure the thread is rescheduled as soon as possible.  We do
	 * this by putting it on the front of the highest priority run
	 * queue in the FSS class.  If the preemption has been put off
	 * for too long, clear the "nopreempt" bit and let the thread
	 * be preempted.
	 */
	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
		if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
			if (!(fssproc->fss_flags & FSSKPRI)) {
				/*
				 * If not already remembered, remember current
				 * priority for restoration in fss_yield().
				 */
				if (!(fssproc->fss_flags & FSSRESTORE)) {
					fssproc->fss_scpri = t->t_pri;
					fssproc->fss_flags |= FSSRESTORE;
				}
				THREAD_CHANGE_PRI(t, fss_maxumdpri);
				t->t_schedflag |= TS_DONT_SWAP;
			}
			schedctl_set_yield(t, 1);
			setfrontdq(t);
			return;
		} else {
			if (fssproc->fss_flags & FSSRESTORE) {
				THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
				fssproc->fss_flags &= ~FSSRESTORE;
			}
			schedctl_set_nopreempt(t, 0);
			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
			/*
			 * Fall through and be preempted below.
			 */
		}
	}

	flags = fssproc->fss_flags & (FSSBACKQ | FSSKPRI);

	if (flags == FSSBACKQ) {
		fssproc->fss_timeleft = fss_quantum;
		fssproc->fss_flags &= ~FSSBACKQ;
		setbackdq(t);
	} else if (flags == (FSSBACKQ | FSSKPRI)) {
		fssproc->fss_flags &= ~FSSBACKQ;
		setbackdq(t);
	} else {
		setfrontdq(t);
	}
}

/*
 * Called when a thread is waking up and is to be placed on the run queue.
 */
static void
fss_setrun(kthread_t *t)
{
	fssproc_t *fssproc = FSSPROC(t);

	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */

	if (t->t_state == TS_SLEEP || t->t_state == TS_STOPPED)
		fss_active(t);

	fssproc->fss_timeleft = fss_quantum;

	fssproc->fss_flags &= ~FSSBACKQ;
	/*
	 * If previously were running at the kernel priority then keep that
	 * priority and the fss_timeleft doesn't matter.
	 */
	if ((fssproc->fss_flags & FSSKPRI) == 0)
		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);

	if (t->t_disp_time != ddi_get_lbolt())
		setbackdq(t);
	else
		setfrontdq(t);
}

/*
 * Prepare thread for sleep. We reset the thread priority so it will run at the
 * kernel priority level when it wakes up.
 */
static void
fss_sleep(kthread_t *t)
{
	fssproc_t *fssproc = FSSPROC(t);

	ASSERT(t == curthread);
	ASSERT(THREAD_LOCK_HELD(t));

	ASSERT(t->t_state == TS_ONPROC);

	/*
	 * Account for time spent on CPU before going to sleep.
	 */
	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);

	fss_inactive(t);

	/*
	 * Assign a system priority to the thread and arrange for it to be
	 * retained when the thread is next placed on the run queue (i.e.,
	 * when it wakes up) instead of being given a new pri.  Also arrange
	 * for trapret processing as the thread leaves the system call so it
	 * will drop back to normal priority range.
	 */
	if (t->t_kpri_req) {
		THREAD_CHANGE_PRI(t, minclsyspri);
		fssproc->fss_flags |= FSSKPRI;
		t->t_trapret = 1;	/* so that fss_trapret will run */
		aston(t);
	} else if (fssproc->fss_flags & FSSKPRI) {
		/*
		 * The thread has done a THREAD_KPRI_REQUEST(), slept, then
		 * done THREAD_KPRI_RELEASE() (so no t_kpri_req is 0 again),
		 * then slept again all without finishing the current system
		 * call so trapret won't have cleared FSSKPRI
		 */
		fssproc->fss_flags &= ~FSSKPRI;
		THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
		if (DISP_MUST_SURRENDER(curthread))
			cpu_surrender(t);
	}
	t->t_stime = ddi_get_lbolt();	/* time stamp for the swapper */
}

/*
 * A tick interrupt has ocurrend on a running thread. Check to see if our
 * time slice has expired.  We must also clear the TS_DONT_SWAP flag in
 * t_schedflag if the thread is eligible to be swapped out.
 */
static void
fss_tick(kthread_t *t)
{
	fssproc_t *fssproc;
	fssproj_t *fssproj;
	klwp_t *lwp;
	boolean_t call_cpu_surrender = B_FALSE;
	boolean_t cpucaps_enforce = B_FALSE;

	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));

	/*
	 * It's safe to access fsspset and fssproj structures because we're
	 * holding our p_lock here.
	 */
	thread_lock(t);
	fssproc = FSSPROC(t);
	fssproj = FSSPROC2FSSPROJ(fssproc);
	if (fssproj != NULL) {
		fsspset_t *fsspset = FSSPROJ2FSSPSET(fssproj);
		disp_lock_enter_high(&fsspset->fssps_displock);
		fssproj->fssp_ticks += fss_nice_tick[fssproc->fss_nice];
		fssproj->fssp_zone_ticks++;
		fssproc->fss_ticks++;
		disp_lock_exit_high(&fsspset->fssps_displock);
	}

	/*
	 * Keep track of thread's project CPU usage.  Note that projects
	 * get charged even when threads are running in the kernel.
	 * Do not surrender CPU if running in the SYS class.
	 */
	if (CPUCAPS_ON()) {
		cpucaps_enforce = cpucaps_charge(t,
		    &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE) &&
		    !(fssproc->fss_flags & FSSKPRI);
	}

	/*
	 * A thread's execution time for threads running in the SYS class
	 * is not tracked.
	 */
	if ((fssproc->fss_flags & FSSKPRI) == 0) {
		/*
		 * If thread is not in kernel mode, decrement its fss_timeleft
		 */
		if (--fssproc->fss_timeleft <= 0) {
			pri_t new_pri;

			/*
			 * If we're doing preemption control and trying to
			 * avoid preempting this thread, just note that the
			 * thread should yield soon and let it keep running
			 * (unless it's been a while).
			 */
			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
				if (fssproc->fss_timeleft > -SC_MAX_TICKS) {
					DTRACE_SCHED1(schedctl__nopreempt,
					    kthread_t *, t);
					schedctl_set_yield(t, 1);
					thread_unlock_nopreempt(t);
					return;
				}
			}
			fssproc->fss_flags &= ~FSSRESTORE;

			fss_newpri(fssproc);
			new_pri = fssproc->fss_umdpri;
			ASSERT(new_pri >= 0 && new_pri <= fss_maxglobpri);

			/*
			 * When the priority of a thread is changed, it may
			 * be necessary to adjust its position on a sleep queue
			 * or dispatch queue. The function thread_change_pri
			 * accomplishes this.
			 */
			if (thread_change_pri(t, new_pri, 0)) {
				if ((t->t_schedflag & TS_LOAD) &&
				    (lwp = t->t_lwp) &&
				    lwp->lwp_state == LWP_USER)
					t->t_schedflag &= ~TS_DONT_SWAP;
				fssproc->fss_timeleft = fss_quantum;
			} else {
				call_cpu_surrender = B_TRUE;
			}
		} else if (t->t_state == TS_ONPROC &&
		    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
			/*
			 * If there is a higher-priority thread which is
			 * waiting for a processor, then thread surrenders
			 * the processor.
			 */
			call_cpu_surrender = B_TRUE;
		}
	}

	if (cpucaps_enforce && 2 * fssproc->fss_timeleft > fss_quantum) {
		/*
		 * The thread used more than half of its quantum, so assume that
		 * it used the whole quantum.
		 *
		 * Update thread's priority just before putting it on the wait
		 * queue so that it gets charged for the CPU time from its
		 * quantum even before that quantum expires.
		 */
		fss_newpri(fssproc);
		if (t->t_pri != fssproc->fss_umdpri)
			fss_change_priority(t, fssproc);

		/*
		 * We need to call cpu_surrender for this thread due to cpucaps
		 * enforcement, but fss_change_priority may have already done
		 * so. In this case FSSBACKQ is set and there is no need to call
		 * cpu-surrender again.
		 */
		if (!(fssproc->fss_flags & FSSBACKQ))
			call_cpu_surrender = B_TRUE;
	}

	if (call_cpu_surrender) {
		fssproc->fss_flags |= FSSBACKQ;
		cpu_surrender(t);
	}

	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
}

/*
 * Processes waking up go to the back of their queue.  We don't need to assign
 * a time quantum here because thread is still at a kernel mode priority and
 * the time slicing is not done for threads running in the kernel after
 * sleeping.  The proper time quantum will be assigned by fss_trapret before the
 * thread returns to user mode.
 */
static void
fss_wakeup(kthread_t *t)
{
	fssproc_t *fssproc;

	ASSERT(THREAD_LOCK_HELD(t));
	ASSERT(t->t_state == TS_SLEEP);

	fss_active(t);

	t->t_stime = ddi_get_lbolt();		/* time stamp for the swapper */
	fssproc = FSSPROC(t);
	fssproc->fss_flags &= ~FSSBACKQ;

	if (fssproc->fss_flags & FSSKPRI) {
		/*
		 * If we already have a kernel priority assigned, then we
		 * just use it.
		 */
		setbackdq(t);
	} else if (t->t_kpri_req) {
		/*
		 * Give thread a priority boost if we were asked.
		 */
		fssproc->fss_flags |= FSSKPRI;
		THREAD_CHANGE_PRI(t, minclsyspri);
		setbackdq(t);
		t->t_trapret = 1;	/* so that fss_trapret will run */
		aston(t);
	} else {
		/*
		 * Otherwise, we recalculate the priority.
		 */
		if (t->t_disp_time == ddi_get_lbolt()) {
			setfrontdq(t);
		} else {
			fssproc->fss_timeleft = fss_quantum;
			THREAD_CHANGE_PRI(t, fssproc->fss_umdpri);
			setbackdq(t);
		}
	}
}

/*
 * fss_donice() is called when a nice(1) command is issued on the thread to
 * alter the priority. The nice(1) command exists in Solaris for compatibility.
 * Thread priority adjustments should be done via priocntl(1).
 */
static int
fss_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
{
	int newnice;
	fssproc_t *fssproc = FSSPROC(t);
	fssparms_t fssparms;

	/*
	 * If there is no change to priority, just return current setting.
	 */
	if (incr == 0) {
		if (retvalp)
			*retvalp = fssproc->fss_nice - NZERO;
		return (0);
	}

	if ((incr < 0 || incr > 2 * NZERO) && secpolicy_setpriority(cr) != 0)
		return (EPERM);

	/*
	 * Specifying a nice increment greater than the upper limit of
	 * FSS_NICE_MAX (== 2 * NZERO - 1) will result in the thread's nice
	 * value being set to the upper limit.  We check for this before
	 * computing the new value because otherwise we could get overflow
	 * if a privileged user specified some ridiculous increment.
	 */
	if (incr > FSS_NICE_MAX)
		incr = FSS_NICE_MAX;

	newnice = fssproc->fss_nice + incr;
	if (newnice > FSS_NICE_MAX)
		newnice = FSS_NICE_MAX;
	else if (newnice < FSS_NICE_MIN)
		newnice = FSS_NICE_MIN;

	fssparms.fss_uprilim = fssparms.fss_upri =
	    -((newnice - NZERO) * fss_maxupri) / NZERO;

	/*
	 * Reset the uprilim and upri values of the thread.
	 */
	(void) fss_parmsset(t, (void *)&fssparms, (id_t)0, (cred_t *)NULL);

	/*
	 * Although fss_parmsset already reset fss_nice it may not have been
	 * set to precisely the value calculated above because fss_parmsset
	 * determines the nice value from the user priority and we may have
	 * truncated during the integer conversion from nice value to user
	 * priority and back. We reset fss_nice to the value we calculated
	 * above.
	 */
	fssproc->fss_nice = (char)newnice;

	if (retvalp)
		*retvalp = newnice - NZERO;
	return (0);
}

/*
 * Increment the priority of the specified thread by incr and
 * return the new value in *retvalp.
 */
static int
fss_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
{
	int newpri;
	fssproc_t *fssproc = FSSPROC(t);
	fssparms_t fssparms;

	/*
	 * If there is no change to priority, just return current setting.
	 */
	if (incr == 0) {
		*retvalp = fssproc->fss_upri;
		return (0);
	}

	newpri = fssproc->fss_upri + incr;
	if (newpri > fss_maxupri || newpri < -fss_maxupri)
		return (EINVAL);

	*retvalp = newpri;
	fssparms.fss_uprilim = fssparms.fss_upri = newpri;

	/*
	 * Reset the uprilim and upri values of the thread.
	 */
	return (fss_parmsset(t, &fssparms, (id_t)0, cr));
}

/*
 * Return the global scheduling priority that would be assigned to a thread
 * entering the fair-sharing class with the fss_upri.
 */
/*ARGSUSED*/
static pri_t
fss_globpri(kthread_t *t)
{
	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

	return (fss_maxumdpri / 2);
}

/*
 * Called from the yield(2) system call when a thread is yielding (surrendering)
 * the processor. The kernel thread is placed at the back of a dispatch queue.
 */
static void
fss_yield(kthread_t *t)
{
	fssproc_t *fssproc = FSSPROC(t);

	ASSERT(t == curthread);
	ASSERT(THREAD_LOCK_HELD(t));

	/*
	 * Collect CPU usage spent before yielding
	 */
	(void) CPUCAPS_CHARGE(t, &fssproc->fss_caps, CPUCAPS_CHARGE_ENFORCE);

	/*
	 * Clear the preemption control "yield" bit since the user is
	 * doing a yield.
	 */
	if (t->t_schedctl)
		schedctl_set_yield(t, 0);
	/*
	 * If fss_preempt() artifically increased the thread's priority
	 * to avoid preemption, restore the original priority now.
	 */
	if (fssproc->fss_flags & FSSRESTORE) {
		THREAD_CHANGE_PRI(t, fssproc->fss_scpri);
		fssproc->fss_flags &= ~FSSRESTORE;
	}
	if (fssproc->fss_timeleft < 0) {
		/*
		 * Time slice was artificially extended to avoid preemption,
		 * so pretend we're preempting it now.
		 */
		DTRACE_SCHED1(schedctl__yield, int, -fssproc->fss_timeleft);
		fssproc->fss_timeleft = fss_quantum;
	}
	fssproc->fss_flags &= ~FSSBACKQ;
	setbackdq(t);
}

void
fss_changeproj(kthread_t *t, void *kp, void *zp, fssbuf_t *projbuf,
    fssbuf_t *zonebuf)
{
	kproject_t *kpj_new = kp;
	zone_t *zone = zp;
	fssproj_t *fssproj_old, *fssproj_new;
	fsspset_t *fsspset;
	kproject_t *kpj_old;
	fssproc_t *fssproc;
	fsszone_t *fsszone_old, *fsszone_new;
	int free = 0;
	int id;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(MUTEX_HELD(&pidlock));
	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

	if (t->t_cid != fss_cid)
		return;

	fssproc = FSSPROC(t);
	mutex_enter(&fsspsets_lock);
	fssproj_old = FSSPROC2FSSPROJ(fssproc);
	if (fssproj_old == NULL) {
		mutex_exit(&fsspsets_lock);
		return;
	}

	fsspset = FSSPROJ2FSSPSET(fssproj_old);
	mutex_enter(&fsspset->fssps_lock);
	kpj_old = FSSPROJ2KPROJ(fssproj_old);
	fsszone_old = fssproj_old->fssp_fsszone;

	ASSERT(t->t_cpupart == fsspset->fssps_cpupart);

	if (kpj_old == kpj_new) {
		mutex_exit(&fsspset->fssps_lock);
		mutex_exit(&fsspsets_lock);
		return;
	}

	if ((fsszone_new = fss_find_fsszone(fsspset, zone)) == NULL) {
		/*
		 * If the zone for the new project is not currently active on
		 * the cpu partition we're on, get one of the pre-allocated
		 * buffers and link it in our per-pset zone list.  Such buffers
		 * should already exist.
		 */
		for (id = 0; id < zonebuf->fssb_size; id++) {
			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
				fss_insert_fsszone(fsspset, zone, fsszone_new);
				zonebuf->fssb_list[id] = NULL;
				break;
			}
		}
	}
	ASSERT(fsszone_new != NULL);
	if ((fssproj_new = fss_find_fssproj(fsspset, kpj_new)) == NULL) {
		/*
		 * If our new project is not currently running
		 * on the cpu partition we're on, get one of the
		 * pre-allocated buffers and link it in our new cpu
		 * partition doubly linked list. Such buffers should already
		 * exist.
		 */
		for (id = 0; id < projbuf->fssb_size; id++) {
			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
				fss_insert_fssproj(fsspset, kpj_new,
				    fsszone_new, fssproj_new);
				projbuf->fssb_list[id] = NULL;
				break;
			}
		}
	}
	ASSERT(fssproj_new != NULL);

	thread_lock(t);
	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
	    t->t_state == TS_WAIT)
		fss_inactive(t);
	ASSERT(fssproj_old->fssp_threads > 0);
	if (--fssproj_old->fssp_threads == 0) {
		fss_remove_fssproj(fsspset, fssproj_old);
		free = 1;
	}
	fssproc->fss_proj = fssproj_new;
	fssproc->fss_fsspri = 0;
	fssproj_new->fssp_threads++;
	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
	    t->t_state == TS_WAIT)
		fss_active(t);
	thread_unlock(t);
	if (free) {
		if (fsszone_old->fssz_nproj == 0)
			kmem_free(fsszone_old, sizeof (fsszone_t));
		kmem_free(fssproj_old, sizeof (fssproj_t));
	}

	mutex_exit(&fsspset->fssps_lock);
	mutex_exit(&fsspsets_lock);
}

void
fss_changepset(kthread_t *t, void *newcp, fssbuf_t *projbuf,
    fssbuf_t *zonebuf)
{
	fsspset_t *fsspset_old, *fsspset_new;
	fssproj_t *fssproj_old, *fssproj_new;
	fsszone_t *fsszone_old, *fsszone_new;
	fssproc_t *fssproc;
	kproject_t *kpj;
	zone_t *zone;
	int id;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(MUTEX_HELD(&pidlock));
	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));

	if (t->t_cid != fss_cid)
		return;

	fssproc = FSSPROC(t);
	zone = ttoproc(t)->p_zone;
	mutex_enter(&fsspsets_lock);
	fssproj_old = FSSPROC2FSSPROJ(fssproc);
	if (fssproj_old == NULL) {
		mutex_exit(&fsspsets_lock);
		return;
	}
	fsszone_old = fssproj_old->fssp_fsszone;
	fsspset_old = FSSPROJ2FSSPSET(fssproj_old);
	kpj = FSSPROJ2KPROJ(fssproj_old);

	if (fsspset_old->fssps_cpupart == newcp) {
		mutex_exit(&fsspsets_lock);
		return;
	}

	ASSERT(ttoproj(t) == kpj);

	fsspset_new = fss_find_fsspset(newcp);

	mutex_enter(&fsspset_new->fssps_lock);
	if ((fsszone_new = fss_find_fsszone(fsspset_new, zone)) == NULL) {
		for (id = 0; id < zonebuf->fssb_size; id++) {
			if ((fsszone_new = zonebuf->fssb_list[id]) != NULL) {
				fss_insert_fsszone(fsspset_new, zone,
				    fsszone_new);
				zonebuf->fssb_list[id] = NULL;
				break;
			}
		}
	}
	ASSERT(fsszone_new != NULL);
	if ((fssproj_new = fss_find_fssproj(fsspset_new, kpj)) == NULL) {
		for (id = 0; id < projbuf->fssb_size; id++) {
			if ((fssproj_new = projbuf->fssb_list[id]) != NULL) {
				fss_insert_fssproj(fsspset_new, kpj,
				    fsszone_new, fssproj_new);
				projbuf->fssb_list[id] = NULL;
				break;
			}
		}
	}
	ASSERT(fssproj_new != NULL);

	fssproj_new->fssp_threads++;
	thread_lock(t);
	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
	    t->t_state == TS_WAIT)
		fss_inactive(t);
	fssproc->fss_proj = fssproj_new;
	fssproc->fss_fsspri = 0;
	if (t->t_state == TS_RUN || t->t_state == TS_ONPROC ||
	    t->t_state == TS_WAIT)
		fss_active(t);
	thread_unlock(t);
	mutex_exit(&fsspset_new->fssps_lock);

	mutex_enter(&fsspset_old->fssps_lock);
	if (--fssproj_old->fssp_threads == 0) {
		fss_remove_fssproj(fsspset_old, fssproj_old);
		if (fsszone_old->fssz_nproj == 0)
			kmem_free(fsszone_old, sizeof (fsszone_t));
		kmem_free(fssproj_old, sizeof (fssproj_t));
	}
	mutex_exit(&fsspset_old->fssps_lock);

	mutex_exit(&fsspsets_lock);
}