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
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
|
/*
* 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) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, Joyent, Inc. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
/*
* VM - anonymous pages.
*
* This layer sits immediately above the vm_swap layer. It manages
* physical pages that have no permanent identity in the file system
* name space, using the services of the vm_swap layer to allocate
* backing storage for these pages. Since these pages have no external
* identity, they are discarded when the last reference is removed.
*
* An important function of this layer is to manage low-level sharing
* of pages that are logically distinct but that happen to be
* physically identical (e.g., the corresponding pages of the processes
* resulting from a fork before one process or the other changes their
* contents). This pseudo-sharing is present only as an optimization
* and is not to be confused with true sharing in which multiple
* address spaces deliberately contain references to the same object;
* such sharing is managed at a higher level.
*
* The key data structure here is the anon struct, which contains a
* reference count for its associated physical page and a hint about
* the identity of that page. Anon structs typically live in arrays,
* with an instance's position in its array determining where the
* corresponding backing storage is allocated; however, the swap_xlate()
* routine abstracts away this representation information so that the
* rest of the anon layer need not know it. (See the swap layer for
* more details on anon struct layout.)
*
* In the future versions of the system, the association between an
* anon struct and its position on backing store will change so that
* we don't require backing store all anonymous pages in the system.
* This is important for consideration for large memory systems.
* We can also use this technique to delay binding physical locations
* to anonymous pages until pageout/swapout time where we can make
* smarter allocation decisions to improve anonymous klustering.
*
* Many of the routines defined here take a (struct anon **) argument,
* which allows the code at this level to manage anon pages directly,
* so that callers can regard anon structs as opaque objects and not be
* concerned with assigning or inspecting their contents.
*
* Clients of this layer refer to anon pages indirectly. That is, they
* maintain arrays of pointers to anon structs rather than maintaining
* anon structs themselves. The (struct anon **) arguments mentioned
* above are pointers to entries in these arrays. It is these arrays
* that capture the mapping between offsets within a given segment and
* the corresponding anonymous backing storage address.
*/
#ifdef DEBUG
#define ANON_DEBUG
#endif
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mman.h>
#include <sys/cred.h>
#include <sys/thread.h>
#include <sys/vnode.h>
#include <sys/cpuvar.h>
#include <sys/swap.h>
#include <sys/cmn_err.h>
#include <sys/vtrace.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/bitmap.h>
#include <sys/vmsystm.h>
#include <sys/tuneable.h>
#include <sys/debug.h>
#include <sys/fs/swapnode.h>
#include <sys/tnf_probe.h>
#include <sys/lgrp.h>
#include <sys/policy.h>
#include <sys/condvar_impl.h>
#include <sys/mutex_impl.h>
#include <sys/rctl.h>
#include <vm/as.h>
#include <vm/hat.h>
#include <vm/anon.h>
#include <vm/page.h>
#include <vm/vpage.h>
#include <vm/seg.h>
#include <vm/rm.h>
#include <fs/fs_subr.h>
struct vnode *anon_vp;
int anon_debug;
kmutex_t anoninfo_lock;
struct k_anoninfo k_anoninfo;
ani_free_t *ani_free_pool;
pad_mutex_t anon_array_lock[ANON_LOCKSIZE];
kcondvar_t anon_array_cv[ANON_LOCKSIZE];
/*
* Global hash table for (vp, off) -> anon slot
*/
extern int swap_maxcontig;
size_t anon_hash_size;
unsigned int anon_hash_shift;
struct anon **anon_hash;
static struct kmem_cache *anon_cache;
static struct kmem_cache *anonmap_cache;
pad_mutex_t *anonhash_lock;
/*
* Used to make the increment of all refcnts of all anon slots of a large
* page appear to be atomic. The lock is grabbed for the first anon slot of
* a large page.
*/
pad_mutex_t *anonpages_hash_lock;
#define APH_MUTEX(vp, off) \
(&anonpages_hash_lock[(ANON_HASH((vp), (off)) & \
(AH_LOCK_SIZE - 1))].pad_mutex)
#ifdef VM_STATS
static struct anonvmstats_str {
ulong_t getpages[30];
ulong_t privatepages[10];
ulong_t demotepages[9];
ulong_t decrefpages[9];
ulong_t dupfillholes[4];
ulong_t freepages[1];
} anonvmstats;
#endif /* VM_STATS */
/*ARGSUSED*/
static int
anonmap_cache_constructor(void *buf, void *cdrarg, int kmflags)
{
struct anon_map *amp = buf;
rw_init(&->a_rwlock, NULL, RW_DEFAULT, NULL);
cv_init(&->a_purgecv, NULL, CV_DEFAULT, NULL);
mutex_init(&->a_pmtx, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&->a_purgemtx, NULL, MUTEX_DEFAULT, NULL);
return (0);
}
/*ARGSUSED1*/
static void
anonmap_cache_destructor(void *buf, void *cdrarg)
{
struct anon_map *amp = buf;
rw_destroy(&->a_rwlock);
cv_destroy(&->a_purgecv);
mutex_destroy(&->a_pmtx);
mutex_destroy(&->a_purgemtx);
}
void
anon_init(void)
{
int i;
pad_mutex_t *tmp;
/* These both need to be powers of 2 so round up to the next power */
anon_hash_shift = highbit((physmem / ANON_HASHAVELEN) - 1);
anon_hash_size = 1L << anon_hash_shift;
/*
* We need to align the anonhash_lock and anonpages_hash_lock arrays
* to a 64B boundary to avoid false sharing. We add 63B to our
* allocation so that we can get a 64B aligned address to use.
* We allocate both of these together to avoid wasting an additional
* 63B.
*/
tmp = kmem_zalloc((2 * AH_LOCK_SIZE * sizeof (pad_mutex_t)) + 63,
KM_SLEEP);
anonhash_lock = (pad_mutex_t *)P2ROUNDUP((uintptr_t)tmp, 64);
anonpages_hash_lock = anonhash_lock + AH_LOCK_SIZE;
for (i = 0; i < AH_LOCK_SIZE; i++) {
mutex_init(&anonhash_lock[i].pad_mutex, NULL, MUTEX_DEFAULT,
NULL);
mutex_init(&anonpages_hash_lock[i].pad_mutex, NULL,
MUTEX_DEFAULT, NULL);
}
for (i = 0; i < ANON_LOCKSIZE; i++) {
mutex_init(&anon_array_lock[i].pad_mutex, NULL,
MUTEX_DEFAULT, NULL);
cv_init(&anon_array_cv[i], NULL, CV_DEFAULT, NULL);
}
anon_hash = (struct anon **)
kmem_zalloc(sizeof (struct anon *) * anon_hash_size, KM_SLEEP);
anon_cache = kmem_cache_create("anon_cache", sizeof (struct anon),
AN_CACHE_ALIGN, NULL, NULL, NULL, NULL, NULL, KMC_PREFILL);
anonmap_cache = kmem_cache_create("anonmap_cache",
sizeof (struct anon_map), 0,
anonmap_cache_constructor, anonmap_cache_destructor, NULL,
NULL, NULL, 0);
swap_maxcontig = (1024 * 1024) >> PAGESHIFT; /* 1MB of pages */
tmp = kmem_zalloc((ANI_MAX_POOL * sizeof (ani_free_t)) + 63, KM_SLEEP);
/* Round ani_free_pool to cacheline boundary to avoid false sharing. */
ani_free_pool = (ani_free_t *)P2ROUNDUP((uintptr_t)tmp, 64);
anon_vp = vn_alloc(KM_SLEEP);
vn_setops(anon_vp, swap_vnodeops);
anon_vp->v_type = VREG;
anon_vp->v_flag |= (VISSWAP|VISSWAPFS);
}
/*
* Global anon slot hash table manipulation.
*/
static void
anon_addhash(struct anon *ap)
{
int index;
ASSERT(MUTEX_HELD(AH_MUTEX(ap->an_vp, ap->an_off)));
index = ANON_HASH(ap->an_vp, ap->an_off);
ap->an_hash = anon_hash[index];
anon_hash[index] = ap;
}
static void
anon_rmhash(struct anon *ap)
{
struct anon **app;
ASSERT(MUTEX_HELD(AH_MUTEX(ap->an_vp, ap->an_off)));
for (app = &anon_hash[ANON_HASH(ap->an_vp, ap->an_off)];
*app; app = &((*app)->an_hash)) {
if (*app == ap) {
*app = ap->an_hash;
break;
}
}
}
/*
* The anon array interfaces. Functions allocating,
* freeing array of pointers, and returning/setting
* entries in the array of pointers for a given offset.
*
* Create the list of pointers
*/
struct anon_hdr *
anon_create(pgcnt_t npages, int flags)
{
struct anon_hdr *ahp;
ulong_t nchunks;
int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
if ((ahp = kmem_zalloc(sizeof (struct anon_hdr), kmemflags)) == NULL) {
return (NULL);
}
mutex_init(&ahp->serial_lock, NULL, MUTEX_DEFAULT, NULL);
/*
* Single level case.
*/
ahp->size = npages;
if (npages <= ANON_CHUNK_SIZE || (flags & ANON_ALLOC_FORCE)) {
if (flags & ANON_ALLOC_FORCE)
ahp->flags |= ANON_ALLOC_FORCE;
ahp->array_chunk = kmem_zalloc(
ahp->size * sizeof (struct anon *), kmemflags);
if (ahp->array_chunk == NULL) {
kmem_free(ahp, sizeof (struct anon_hdr));
return (NULL);
}
} else {
/*
* 2 Level case.
* anon hdr size needs to be rounded off to be a multiple
* of ANON_CHUNK_SIZE. This is important as various anon
* related functions depend on this.
* NOTE -
* anon_grow() makes anon hdr size a multiple of
* ANON_CHUNK_SIZE.
* amp size is <= anon hdr size.
* anon_index + seg_pgs <= anon hdr size.
*/
ahp->size = P2ROUNDUP(npages, ANON_CHUNK_SIZE);
nchunks = ahp->size >> ANON_CHUNK_SHIFT;
ahp->array_chunk = kmem_zalloc(nchunks * sizeof (ulong_t *),
kmemflags);
if (ahp->array_chunk == NULL) {
kmem_free(ahp, sizeof (struct anon_hdr));
return (NULL);
}
}
return (ahp);
}
/*
* Free the array of pointers
*/
void
anon_release(struct anon_hdr *ahp, pgcnt_t npages)
{
ulong_t i;
void **ppp;
ulong_t nchunks;
ASSERT(npages <= ahp->size);
/*
* Single level case.
*/
if (npages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
kmem_free(ahp->array_chunk, ahp->size * sizeof (struct anon *));
} else {
/*
* 2 level case.
*/
nchunks = ahp->size >> ANON_CHUNK_SHIFT;
for (i = 0; i < nchunks; i++) {
ppp = &ahp->array_chunk[i];
if (*ppp != NULL)
kmem_free(*ppp, PAGESIZE);
}
kmem_free(ahp->array_chunk, nchunks * sizeof (ulong_t *));
}
mutex_destroy(&ahp->serial_lock);
kmem_free(ahp, sizeof (struct anon_hdr));
}
/*
* Return the pointer from the list for a
* specified anon index.
*/
struct anon *
anon_get_ptr(struct anon_hdr *ahp, ulong_t an_idx)
{
struct anon **app;
ASSERT(an_idx < ahp->size);
/*
* Single level case.
*/
if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
return ((struct anon *)
((uintptr_t)ahp->array_chunk[an_idx] & ANON_PTRMASK));
} else {
/*
* 2 level case.
*/
app = ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
if (app) {
return ((struct anon *)
((uintptr_t)app[an_idx & ANON_CHUNK_OFF] &
ANON_PTRMASK));
} else {
return (NULL);
}
}
}
/*
* Return the anon pointer for the first valid entry in the anon list,
* starting from the given index.
*/
struct anon *
anon_get_next_ptr(struct anon_hdr *ahp, ulong_t *index)
{
struct anon *ap;
struct anon **app;
ulong_t chunkoff;
ulong_t i;
ulong_t j;
pgcnt_t size;
i = *index;
size = ahp->size;
ASSERT(i < size);
if ((size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
/*
* 1 level case
*/
while (i < size) {
ap = (struct anon *)
((uintptr_t)ahp->array_chunk[i] & ANON_PTRMASK);
if (ap) {
*index = i;
return (ap);
}
i++;
}
} else {
/*
* 2 level case
*/
chunkoff = i & ANON_CHUNK_OFF;
while (i < size) {
app = ahp->array_chunk[i >> ANON_CHUNK_SHIFT];
if (app)
for (j = chunkoff; j < ANON_CHUNK_SIZE; j++) {
ap = (struct anon *)
((uintptr_t)app[j] & ANON_PTRMASK);
if (ap) {
*index = i + (j - chunkoff);
return (ap);
}
}
chunkoff = 0;
i = (i + ANON_CHUNK_SIZE) & ~ANON_CHUNK_OFF;
}
}
*index = size;
return (NULL);
}
/*
* Set list entry with a given pointer for a specified offset
*/
int
anon_set_ptr(struct anon_hdr *ahp, ulong_t an_idx, struct anon *ap, int flags)
{
void **ppp;
struct anon **app;
int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
uintptr_t *ap_addr;
ASSERT(an_idx < ahp->size);
/*
* Single level case.
*/
if (ahp->size <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
ap_addr = (uintptr_t *)&ahp->array_chunk[an_idx];
} else {
/*
* 2 level case.
*/
ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
ASSERT(ppp != NULL);
if (*ppp == NULL) {
mutex_enter(&ahp->serial_lock);
ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
if (*ppp == NULL) {
*ppp = kmem_zalloc(PAGESIZE, kmemflags);
if (*ppp == NULL) {
mutex_exit(&ahp->serial_lock);
return (ENOMEM);
}
}
mutex_exit(&ahp->serial_lock);
}
app = *ppp;
ap_addr = (uintptr_t *)&app[an_idx & ANON_CHUNK_OFF];
}
*ap_addr = (*ap_addr & ~ANON_PTRMASK) | (uintptr_t)ap;
return (0);
}
/*
* Copy anon array into a given new anon array
*/
int
anon_copy_ptr(struct anon_hdr *sahp, ulong_t s_idx,
struct anon_hdr *dahp, ulong_t d_idx,
pgcnt_t npages, int flags)
{
void **sapp, **dapp;
void *ap;
int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
ASSERT((s_idx < sahp->size) && (d_idx < dahp->size));
ASSERT((npages <= sahp->size) && (npages <= dahp->size));
/*
* Both arrays are 1 level.
*/
if (((sahp->size <= ANON_CHUNK_SIZE) &&
(dahp->size <= ANON_CHUNK_SIZE)) ||
((sahp->flags & ANON_ALLOC_FORCE) &&
(dahp->flags & ANON_ALLOC_FORCE))) {
bcopy(&sahp->array_chunk[s_idx], &dahp->array_chunk[d_idx],
npages * sizeof (struct anon *));
return (0);
}
/*
* Both arrays are 2 levels.
*/
if (sahp->size > ANON_CHUNK_SIZE &&
dahp->size > ANON_CHUNK_SIZE &&
((sahp->flags & ANON_ALLOC_FORCE) == 0) &&
((dahp->flags & ANON_ALLOC_FORCE) == 0)) {
ulong_t sapidx, dapidx;
ulong_t *sap, *dap;
ulong_t chknp;
while (npages != 0) {
sapidx = s_idx & ANON_CHUNK_OFF;
dapidx = d_idx & ANON_CHUNK_OFF;
chknp = ANON_CHUNK_SIZE - MAX(sapidx, dapidx);
if (chknp > npages)
chknp = npages;
sapp = &sahp->array_chunk[s_idx >> ANON_CHUNK_SHIFT];
if ((sap = *sapp) != NULL) {
dapp = &dahp->array_chunk[d_idx
>> ANON_CHUNK_SHIFT];
if ((dap = *dapp) == NULL) {
*dapp = kmem_zalloc(PAGESIZE,
kmemflags);
if ((dap = *dapp) == NULL)
return (ENOMEM);
}
bcopy((sap + sapidx), (dap + dapidx),
chknp << ANON_PTRSHIFT);
}
s_idx += chknp;
d_idx += chknp;
npages -= chknp;
}
return (0);
}
/*
* At least one of the arrays is 2 level.
*/
while (npages--) {
if ((ap = anon_get_ptr(sahp, s_idx)) != NULL) {
ASSERT(!ANON_ISBUSY(anon_get_slot(sahp, s_idx)));
if (anon_set_ptr(dahp, d_idx, ap, flags) == ENOMEM)
return (ENOMEM);
}
s_idx++;
d_idx++;
}
return (0);
}
/*
* ANON_INITBUF is a convenience macro for anon_grow() below. It
* takes a buffer dst, which is at least as large as buffer src. It
* does a bcopy from src into dst, and then bzeros the extra bytes
* of dst. If tail is set, the data in src is tail aligned within
* dst instead of head aligned.
*/
#define ANON_INITBUF(src, srclen, dst, dstsize, tail) \
if (tail) { \
bzero((dst), (dstsize) - (srclen)); \
bcopy((src), (char *)(dst) + (dstsize) - (srclen), (srclen)); \
} else { \
bcopy((src), (dst), (srclen)); \
bzero((char *)(dst) + (srclen), (dstsize) - (srclen)); \
}
#define ANON_1_LEVEL_INC (ANON_CHUNK_SIZE / 8)
#define ANON_2_LEVEL_INC (ANON_1_LEVEL_INC * ANON_CHUNK_SIZE)
/*
* anon_grow() is used to efficiently extend an existing anon array.
* startidx_p points to the index into the anon array of the first page
* that is in use. oldseg_pgs is the number of pages in use, starting at
* *startidx_p. newpages is the number of additional pages desired.
*
* If startidx_p == NULL, startidx is taken to be 0 and cannot be changed.
*
* The growth is done by creating a new top level of the anon array,
* and (if the array is 2-level) reusing the existing second level arrays.
*
* flags can be used to specify ANON_NOSLEEP and ANON_GROWDOWN.
*
* Returns the new number of pages in the anon array.
*/
pgcnt_t
anon_grow(struct anon_hdr *ahp, ulong_t *startidx_p, pgcnt_t oldseg_pgs,
pgcnt_t newseg_pgs, int flags)
{
ulong_t startidx = startidx_p ? *startidx_p : 0;
pgcnt_t oldamp_pgs = ahp->size, newamp_pgs;
pgcnt_t oelems, nelems, totpages;
void **level1;
int kmemflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
int growdown = (flags & ANON_GROWDOWN);
size_t newarrsz, oldarrsz;
void *level2;
ASSERT(!(startidx_p == NULL && growdown));
ASSERT(startidx + oldseg_pgs <= ahp->size);
/*
* Determine the total number of pages needed in the new
* anon array. If growing down, totpages is all pages from
* startidx through the end of the array, plus <newseg_pgs>
* pages. If growing up, keep all pages from page 0 through
* the last page currently in use, plus <newseg_pgs> pages.
*/
if (growdown)
totpages = oldamp_pgs - startidx + newseg_pgs;
else
totpages = startidx + oldseg_pgs + newseg_pgs;
/* If the array is already large enough, just return. */
if (oldamp_pgs >= totpages) {
if (growdown)
*startidx_p = oldamp_pgs - totpages;
return (oldamp_pgs);
}
/*
* oldamp_pgs/newamp_pgs are the total numbers of pages represented
* by the corresponding arrays.
* oelems/nelems are the number of pointers in the top level arrays
* which may be either level 1 or level 2.
* Will the new anon array be one level or two levels?
*/
if (totpages <= ANON_CHUNK_SIZE || (ahp->flags & ANON_ALLOC_FORCE)) {
newamp_pgs = P2ROUNDUP(totpages, ANON_1_LEVEL_INC);
oelems = oldamp_pgs;
nelems = newamp_pgs;
} else {
newamp_pgs = P2ROUNDUP(totpages, ANON_2_LEVEL_INC);
oelems = (oldamp_pgs + ANON_CHUNK_OFF) >> ANON_CHUNK_SHIFT;
nelems = newamp_pgs >> ANON_CHUNK_SHIFT;
}
newarrsz = nelems * sizeof (void *);
level1 = kmem_alloc(newarrsz, kmemflags);
if (level1 == NULL)
return (0);
/* Are we converting from a one level to a two level anon array? */
if (newamp_pgs > ANON_CHUNK_SIZE && oldamp_pgs <= ANON_CHUNK_SIZE &&
!(ahp->flags & ANON_ALLOC_FORCE)) {
/*
* Yes, we're converting to a two level. Reuse old level 1
* as new level 2 if it is exactly PAGESIZE. Otherwise
* alloc a new level 2 and copy the old level 1 data into it.
*/
if (oldamp_pgs == ANON_CHUNK_SIZE) {
level2 = (void *)ahp->array_chunk;
} else {
level2 = kmem_alloc(PAGESIZE, kmemflags);
if (level2 == NULL) {
kmem_free(level1, newarrsz);
return (0);
}
oldarrsz = oldamp_pgs * sizeof (void *);
ANON_INITBUF(ahp->array_chunk, oldarrsz,
level2, PAGESIZE, growdown);
kmem_free(ahp->array_chunk, oldarrsz);
}
bzero(level1, newarrsz);
if (growdown)
level1[nelems - 1] = level2;
else
level1[0] = level2;
} else {
oldarrsz = oelems * sizeof (void *);
ANON_INITBUF(ahp->array_chunk, oldarrsz,
level1, newarrsz, growdown);
kmem_free(ahp->array_chunk, oldarrsz);
}
ahp->array_chunk = level1;
ahp->size = newamp_pgs;
if (growdown)
*startidx_p = newamp_pgs - totpages;
return (newamp_pgs);
}
/*
* Called to sync ani_free value.
*/
void
set_anoninfo(void)
{
processorid_t ix, max_seqid;
pgcnt_t total = 0;
static clock_t last_time;
clock_t new_time;
if (ani_free_pool == NULL)
return;
/*
* Recompute ani_free at most once per tick. Use max_cpu_seqid_ever to
* identify the maximum number of CPUs were ever online.
*/
new_time = ddi_get_lbolt();
if (new_time > last_time) {
max_seqid = max_cpu_seqid_ever;
ASSERT(ANI_MAX_POOL > max_seqid);
for (ix = 0; ix <= max_seqid; ix++)
total += ani_free_pool[ix].ani_count;
last_time = new_time;
k_anoninfo.ani_free = total;
}
}
/*
* Reserve anon space.
*
* It's no longer simply a matter of incrementing ani_resv to
* reserve swap space, we need to check memory-based as well
* as disk-backed (physical) swap. The following algorithm
* is used:
* Check the space on physical swap
* i.e. amount needed < ani_max - ani_phys_resv
* If we are swapping on swapfs check
* amount needed < (availrmem - swapfs_minfree)
* Since the algorithm to check for the quantity of swap space is
* almost the same as that for reserving it, we'll just use anon_resvmem
* with a flag to decrement availrmem.
*
* Return non-zero on success.
*/
int
anon_resvmem(size_t size, boolean_t takemem, zone_t *zone, int tryhard)
{
pgcnt_t npages = btopr(size);
pgcnt_t mswap_pages = 0;
pgcnt_t pswap_pages = 0;
proc_t *p = curproc;
if (zone != NULL) {
/* test zone.max-swap resource control */
mutex_enter(&p->p_lock);
if (rctl_incr_swap(p, zone, ptob(npages)) != 0) {
mutex_exit(&p->p_lock);
if (takemem)
atomic_add_64(&zone->zone_anon_alloc_fail, 1);
return (0);
}
if (!takemem)
rctl_decr_swap(zone, ptob(npages));
mutex_exit(&p->p_lock);
}
mutex_enter(&anoninfo_lock);
/*
* pswap_pages is the number of pages we can take from
* physical (i.e. disk-backed) swap.
*/
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
pswap_pages = k_anoninfo.ani_max - k_anoninfo.ani_phys_resv;
ANON_PRINT(A_RESV,
("anon_resvmem: npages %lu takemem %u pswap %lu caller %p\n",
npages, takemem, pswap_pages, (void *)caller()));
if (npages <= pswap_pages) {
/*
* we have enough space on a physical swap
*/
if (takemem)
k_anoninfo.ani_phys_resv += npages;
mutex_exit(&anoninfo_lock);
return (1);
} else if (pswap_pages != 0) {
/*
* we have some space on a physical swap
*/
if (takemem) {
/*
* use up remainder of phys swap
*/
k_anoninfo.ani_phys_resv += pswap_pages;
ASSERT(k_anoninfo.ani_phys_resv == k_anoninfo.ani_max);
}
}
/*
* since (npages > pswap_pages) we need mem swap
* mswap_pages is the number of pages needed from availrmem
*/
ASSERT(npages > pswap_pages);
mswap_pages = npages - pswap_pages;
ANON_PRINT(A_RESV, ("anon_resvmem: need %ld pages from memory\n",
mswap_pages));
/*
* priv processes can reserve memory as swap as long as availrmem
* remains greater than swapfs_minfree; in the case of non-priv
* processes, memory can be reserved as swap only if availrmem
* doesn't fall below (swapfs_minfree + swapfs_reserve). Thus,
* swapfs_reserve amount of memswap is not available to non-priv
* processes. This protects daemons such as automounter dying
* as a result of application processes eating away almost entire
* membased swap. This safeguard becomes useless if apps are run
* with root access.
*
* swapfs_reserve is minimum of 4Mb or 1/16 of physmem.
*
*/
if (tryhard) {
pgcnt_t floor_pages;
if (secpolicy_resource_anon_mem(CRED())) {
floor_pages = swapfs_minfree;
} else {
floor_pages = swapfs_minfree + swapfs_reserve;
}
mutex_exit(&anoninfo_lock);
(void) page_reclaim_mem(mswap_pages, floor_pages, 0);
mutex_enter(&anoninfo_lock);
}
mutex_enter(&freemem_lock);
if (availrmem > (swapfs_minfree + swapfs_reserve + mswap_pages) ||
(availrmem > (swapfs_minfree + mswap_pages) &&
secpolicy_resource(CRED()) == 0)) {
if (takemem) {
/*
* Take the memory from the rest of the system.
*/
availrmem -= mswap_pages;
mutex_exit(&freemem_lock);
k_anoninfo.ani_mem_resv += mswap_pages;
ANI_ADD(mswap_pages);
ANON_PRINT((A_RESV | A_MRESV),
("anon_resvmem: took %ld pages of availrmem\n",
mswap_pages));
} else {
mutex_exit(&freemem_lock);
}
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
mutex_exit(&anoninfo_lock);
return (1);
} else {
/*
* Fail if not enough memory
*/
if (takemem) {
k_anoninfo.ani_phys_resv -= pswap_pages;
}
mutex_exit(&freemem_lock);
mutex_exit(&anoninfo_lock);
ANON_PRINT(A_RESV,
("anon_resvmem: not enough space from swapfs\n"));
if (zone != NULL && takemem)
rctl_decr_swap(zone, ptob(npages));
return (0);
}
}
/*
* Give back an anon reservation.
*/
void
anon_unresvmem(size_t size, zone_t *zone)
{
pgcnt_t npages = btopr(size);
spgcnt_t mem_free_pages = 0;
pgcnt_t phys_free_slots;
#ifdef ANON_DEBUG
pgcnt_t mem_resv;
#endif
if (zone != NULL)
rctl_decr_swap(zone, ptob(npages));
mutex_enter(&anoninfo_lock);
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
/*
* If some of this reservation belonged to swapfs
* give it back to availrmem.
* ani_mem_resv is the amount of availrmem swapfs has reserved.
* but some of that memory could be locked by segspt so we can only
* return non locked ani_mem_resv back to availrmem
*/
if (k_anoninfo.ani_mem_resv > k_anoninfo.ani_locked_swap) {
ANON_PRINT((A_RESV | A_MRESV),
("anon_unresv: growing availrmem by %ld pages\n",
MIN(k_anoninfo.ani_mem_resv, npages)));
mem_free_pages = MIN((spgcnt_t)(k_anoninfo.ani_mem_resv -
k_anoninfo.ani_locked_swap), npages);
mutex_enter(&freemem_lock);
availrmem += mem_free_pages;
mutex_exit(&freemem_lock);
k_anoninfo.ani_mem_resv -= mem_free_pages;
ANI_ADD(-mem_free_pages);
}
/*
* The remainder of the pages is returned to phys swap
*/
ASSERT(npages >= mem_free_pages);
phys_free_slots = npages - mem_free_pages;
if (phys_free_slots) {
k_anoninfo.ani_phys_resv -= phys_free_slots;
}
#ifdef ANON_DEBUG
mem_resv = k_anoninfo.ani_mem_resv;
#endif
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
mutex_exit(&anoninfo_lock);
ANON_PRINT(A_RESV, ("anon_unresv: %lu, tot %lu, caller %p\n",
npages, mem_resv, (void *)caller()));
}
/*
* Allocate an anon slot and return it with the lock held.
*/
struct anon *
anon_alloc(struct vnode *vp, anoff_t off)
{
struct anon *ap;
kmutex_t *ahm;
ap = kmem_cache_alloc(anon_cache, KM_SLEEP);
if (vp == NULL) {
swap_alloc(ap);
} else {
ap->an_vp = vp;
ap->an_off = off;
}
ap->an_refcnt = 1;
ap->an_pvp = NULL;
ap->an_poff = 0;
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
anon_addhash(ap);
mutex_exit(ahm);
ANI_ADD(-1);
ANON_PRINT(A_ANON, ("anon_alloc: returning ap %p, vp %p\n",
(void *)ap, (ap ? (void *)ap->an_vp : NULL)));
return (ap);
}
/*
* Called for pages locked in memory via softlock/pagelock/mlock to make sure
* such pages don't consume any physical swap resources needed for swapping
* unlocked pages.
*/
void
anon_swap_free(struct anon *ap, page_t *pp)
{
kmutex_t *ahm;
ASSERT(ap != NULL);
ASSERT(pp != NULL);
ASSERT(PAGE_LOCKED(pp));
ASSERT(pp->p_vnode != NULL);
ASSERT(IS_SWAPFSVP(pp->p_vnode));
ASSERT(ap->an_refcnt != 0);
ASSERT(pp->p_vnode == ap->an_vp);
ASSERT(pp->p_offset == ap->an_off);
if (ap->an_pvp == NULL)
return;
page_io_lock(pp);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
ASSERT(ap->an_refcnt != 0);
ASSERT(pp->p_vnode == ap->an_vp);
ASSERT(pp->p_offset == ap->an_off);
if (ap->an_pvp != NULL) {
swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
ap->an_pvp = NULL;
ap->an_poff = 0;
mutex_exit(ahm);
hat_setmod(pp);
} else {
mutex_exit(ahm);
}
page_io_unlock(pp);
}
/*
* Decrement the reference count of an anon page.
* If reference count goes to zero, free it and
* its associated page (if any).
*/
void
anon_decref(struct anon *ap)
{
page_t *pp;
struct vnode *vp;
anoff_t off;
kmutex_t *ahm;
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
ASSERT(ap->an_refcnt != 0);
if (ap->an_refcnt == 0)
panic("anon_decref: slot count 0");
if (--ap->an_refcnt == 0) {
swap_xlate(ap, &vp, &off);
anon_rmhash(ap);
if (ap->an_pvp != NULL)
swap_phys_free(ap->an_pvp, ap->an_poff, PAGESIZE);
mutex_exit(ahm);
/*
* If there is a page for this anon slot we will need to
* call VN_DISPOSE to get rid of the vp association and
* put the page back on the free list as really free.
* Acquire the "exclusive" lock to ensure that any
* pending i/o always completes before the swap slot
* is freed.
*/
pp = page_lookup(vp, (u_offset_t)off, SE_EXCL);
if (pp != NULL) {
/*LINTED: constant in conditional context */
VN_DISPOSE(pp, B_INVAL, 0, kcred);
}
ANON_PRINT(A_ANON, ("anon_decref: free ap %p, vp %p\n",
(void *)ap, (void *)ap->an_vp));
kmem_cache_free(anon_cache, ap);
ANI_ADD(1);
} else {
mutex_exit(ahm);
}
}
/*
* check an_refcnt of the root anon slot (anon_index argument is aligned at
* seg->s_szc level) to determine whether COW processing is required.
* anonpages_hash_lock[] held on the root ap ensures that if root's
* refcnt is 1 all other refcnt's are 1 as well (and they can't increase
* later since this process can't fork while its AS lock is held).
*
* returns 1 if the root anon slot has a refcnt > 1 otherwise returns 0.
*/
int
anon_szcshare(struct anon_hdr *ahp, ulong_t anon_index)
{
struct anon *ap;
kmutex_t *ahmpages = NULL;
ap = anon_get_ptr(ahp, anon_index);
if (ap == NULL)
return (0);
ahmpages = APH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahmpages);
ASSERT(ap->an_refcnt >= 1);
if (ap->an_refcnt == 1) {
mutex_exit(ahmpages);
return (0);
}
mutex_exit(ahmpages);
return (1);
}
/*
* Check 'nslots' anon slots for refcnt > 1.
*
* returns 1 if any of the 'nslots' anon slots has a refcnt > 1 otherwise
* returns 0.
*/
static int
anon_share(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots)
{
struct anon *ap;
while (nslots-- > 0) {
if ((ap = anon_get_ptr(ahp, anon_index)) != NULL &&
ap->an_refcnt > 1)
return (1);
anon_index++;
}
return (0);
}
static void
anon_decref_pages(
struct anon_hdr *ahp,
ulong_t an_idx,
uint_t szc)
{
struct anon *ap = anon_get_ptr(ahp, an_idx);
kmutex_t *ahmpages = NULL;
page_t *pp;
pgcnt_t pgcnt = page_get_pagecnt(szc);
pgcnt_t i;
struct vnode *vp;
anoff_t off;
kmutex_t *ahm;
#ifdef DEBUG
int refcnt = 1;
#endif
ASSERT(szc != 0);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
ASSERT(an_idx < ahp->size);
if (ahp->size - an_idx < pgcnt) {
/*
* In case of shared mappings total anon map size may not be
* the largest page size aligned.
*/
pgcnt = ahp->size - an_idx;
}
VM_STAT_ADD(anonvmstats.decrefpages[0]);
if (ap != NULL) {
ahmpages = APH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahmpages);
ASSERT((refcnt = ap->an_refcnt) != 0);
VM_STAT_ADD(anonvmstats.decrefpages[1]);
if (ap->an_refcnt == 1) {
VM_STAT_ADD(anonvmstats.decrefpages[2]);
ASSERT(!anon_share(ahp, an_idx, pgcnt));
mutex_exit(ahmpages);
ahmpages = NULL;
}
}
i = 0;
while (i < pgcnt) {
if ((ap = anon_get_ptr(ahp, an_idx + i)) == NULL) {
ASSERT(refcnt == 1 && ahmpages == NULL);
i++;
continue;
}
ASSERT(ap->an_refcnt == refcnt);
ASSERT(ahmpages != NULL || ap->an_refcnt == 1);
ASSERT(ahmpages == NULL || ap->an_refcnt > 1);
if (ahmpages == NULL) {
swap_xlate(ap, &vp, &off);
pp = page_lookup(vp, (u_offset_t)off, SE_EXCL);
if (pp == NULL || pp->p_szc == 0) {
VM_STAT_ADD(anonvmstats.decrefpages[3]);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
(void) anon_set_ptr(ahp, an_idx + i, NULL,
ANON_SLEEP);
mutex_enter(ahm);
ap->an_refcnt--;
ASSERT(ap->an_refcnt == 0);
anon_rmhash(ap);
if (ap->an_pvp)
swap_phys_free(ap->an_pvp, ap->an_poff,
PAGESIZE);
mutex_exit(ahm);
if (pp == NULL) {
pp = page_lookup(vp, (u_offset_t)off,
SE_EXCL);
ASSERT(pp == NULL || pp->p_szc == 0);
}
if (pp != NULL) {
VM_STAT_ADD(anonvmstats.decrefpages[4]);
/*LINTED*/
VN_DISPOSE(pp, B_INVAL, 0, kcred);
}
kmem_cache_free(anon_cache, ap);
ANI_ADD(1);
i++;
} else {
pgcnt_t j;
pgcnt_t curpgcnt =
page_get_pagecnt(pp->p_szc);
size_t ppasize = curpgcnt * sizeof (page_t *);
page_t **ppa = kmem_alloc(ppasize, KM_SLEEP);
int dispose = 0;
VM_STAT_ADD(anonvmstats.decrefpages[5]);
ASSERT(pp->p_szc <= szc);
ASSERT(IS_P2ALIGNED(curpgcnt, curpgcnt));
ASSERT(IS_P2ALIGNED(i, curpgcnt));
ASSERT(i + curpgcnt <= pgcnt);
ASSERT(!(page_pptonum(pp) & (curpgcnt - 1)));
ppa[0] = pp;
for (j = i + 1; j < i + curpgcnt; j++) {
ap = anon_get_ptr(ahp, an_idx + j);
ASSERT(ap != NULL &&
ap->an_refcnt == 1);
swap_xlate(ap, &vp, &off);
pp = page_lookup(vp, (u_offset_t)off,
SE_EXCL);
if (pp == NULL)
panic("anon_decref_pages: "
"no page");
(void) hat_pageunload(pp,
HAT_FORCE_PGUNLOAD);
ASSERT(pp->p_szc == ppa[0]->p_szc);
ASSERT(page_pptonum(pp) - 1 ==
page_pptonum(ppa[j - i - 1]));
ppa[j - i] = pp;
if (ap->an_pvp != NULL &&
!vn_matchopval(ap->an_pvp,
VOPNAME_DISPOSE,
(fs_generic_func_p)fs_dispose))
dispose = 1;
}
for (j = i; j < i + curpgcnt; j++) {
ap = anon_get_ptr(ahp, an_idx + j);
ASSERT(ap != NULL &&
ap->an_refcnt == 1);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
(void) anon_set_ptr(ahp, an_idx + j,
NULL, ANON_SLEEP);
mutex_enter(ahm);
ap->an_refcnt--;
ASSERT(ap->an_refcnt == 0);
anon_rmhash(ap);
if (ap->an_pvp)
swap_phys_free(ap->an_pvp,
ap->an_poff, PAGESIZE);
mutex_exit(ahm);
kmem_cache_free(anon_cache, ap);
ANI_ADD(1);
}
if (!dispose) {
VM_STAT_ADD(anonvmstats.decrefpages[6]);
page_destroy_pages(ppa[0]);
} else {
VM_STAT_ADD(anonvmstats.decrefpages[7]);
for (j = 0; j < curpgcnt; j++) {
ASSERT(PAGE_EXCL(ppa[j]));
ppa[j]->p_szc = 0;
}
for (j = 0; j < curpgcnt; j++) {
ASSERT(!hat_page_is_mapped(
ppa[j]));
/*LINTED*/
VN_DISPOSE(ppa[j], B_INVAL, 0,
kcred);
}
}
kmem_free(ppa, ppasize);
i += curpgcnt;
}
} else {
VM_STAT_ADD(anonvmstats.decrefpages[8]);
(void) anon_set_ptr(ahp, an_idx + i, NULL, ANON_SLEEP);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
ap->an_refcnt--;
mutex_exit(ahm);
i++;
}
}
if (ahmpages != NULL) {
mutex_exit(ahmpages);
}
}
/*
* Duplicate references to size bytes worth of anon pages.
* Used when duplicating a segment that contains private anon pages.
* This code assumes that procedure calling this one has already used
* hat_chgprot() to disable write access to the range of addresses that
* that *old actually refers to.
*/
void
anon_dup(struct anon_hdr *old, ulong_t old_idx, struct anon_hdr *new,
ulong_t new_idx, size_t size)
{
spgcnt_t npages;
kmutex_t *ahm;
struct anon *ap;
ulong_t off;
ulong_t index;
npages = btopr(size);
while (npages > 0) {
index = old_idx;
if ((ap = anon_get_next_ptr(old, &index)) == NULL)
break;
ASSERT(!ANON_ISBUSY(anon_get_slot(old, index)));
off = index - old_idx;
npages -= off;
if (npages <= 0)
break;
(void) anon_set_ptr(new, new_idx + off, ap, ANON_SLEEP);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
ap->an_refcnt++;
mutex_exit(ahm);
off++;
new_idx += off;
old_idx += off;
npages--;
}
}
/*
* Just like anon_dup but also guarantees there are no holes (unallocated anon
* slots) within any large page region. That means if a large page region is
* empty in the old array it will skip it. If there are 1 or more valid slots
* in the large page region of the old array it will make sure to fill in any
* unallocated ones and also copy them to the new array. If noalloc is 1 large
* page region should either have no valid anon slots or all slots should be
* valid.
*/
void
anon_dup_fill_holes(
struct anon_hdr *old,
ulong_t old_idx,
struct anon_hdr *new,
ulong_t new_idx,
size_t size,
uint_t szc,
int noalloc)
{
struct anon *ap;
spgcnt_t npages;
kmutex_t *ahm, *ahmpages = NULL;
pgcnt_t pgcnt, i;
ulong_t index, off;
#ifdef DEBUG
int refcnt;
#endif
ASSERT(szc != 0);
pgcnt = page_get_pagecnt(szc);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
npages = btopr(size);
ASSERT(IS_P2ALIGNED(npages, pgcnt));
ASSERT(IS_P2ALIGNED(old_idx, pgcnt));
VM_STAT_ADD(anonvmstats.dupfillholes[0]);
while (npages > 0) {
index = old_idx;
/*
* Find the next valid slot.
*/
if (anon_get_next_ptr(old, &index) == NULL)
break;
ASSERT(!ANON_ISBUSY(anon_get_slot(old, index)));
/*
* Now backup index to the beginning of the
* current large page region of the old array.
*/
index = P2ALIGN(index, pgcnt);
off = index - old_idx;
ASSERT(IS_P2ALIGNED(off, pgcnt));
npages -= off;
if (npages <= 0)
break;
/*
* Fill and copy a large page regions worth
* of anon slots.
*/
for (i = 0; i < pgcnt; i++) {
if ((ap = anon_get_ptr(old, index + i)) == NULL) {
if (noalloc) {
panic("anon_dup_fill_holes: "
"empty anon slot\n");
}
VM_STAT_ADD(anonvmstats.dupfillholes[1]);
ap = anon_alloc(NULL, 0);
(void) anon_set_ptr(old, index + i, ap,
ANON_SLEEP);
} else if (i == 0) {
/*
* make the increment of all refcnts of all
* anon slots of a large page appear atomic by
* getting an anonpages_hash_lock for the
* first anon slot of a large page.
*/
VM_STAT_ADD(anonvmstats.dupfillholes[2]);
ahmpages = APH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahmpages);
/*LINTED*/
ASSERT(refcnt = ap->an_refcnt);
VM_STAT_COND_ADD(ap->an_refcnt > 1,
anonvmstats.dupfillholes[3]);
}
(void) anon_set_ptr(new, new_idx + off + i, ap,
ANON_SLEEP);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
ASSERT(ahmpages != NULL || ap->an_refcnt == 1);
ASSERT(i == 0 || ahmpages == NULL ||
refcnt == ap->an_refcnt);
ap->an_refcnt++;
mutex_exit(ahm);
}
if (ahmpages != NULL) {
mutex_exit(ahmpages);
ahmpages = NULL;
}
off += pgcnt;
new_idx += off;
old_idx += off;
npages -= pgcnt;
}
}
/*
* Used when a segment with a vnode changes szc. similarly to
* anon_dup_fill_holes() makes sure each large page region either has no anon
* slots or all of them. but new slots are created by COWing the file
* pages. on entrance no anon slots should be shared.
*/
int
anon_fill_cow_holes(
struct seg *seg,
caddr_t addr,
struct anon_hdr *ahp,
ulong_t an_idx,
struct vnode *vp,
u_offset_t vp_off,
size_t size,
uint_t szc,
uint_t prot,
struct vpage vpage[],
struct cred *cred)
{
struct anon *ap;
spgcnt_t npages;
pgcnt_t pgcnt, i;
ulong_t index, off;
int err = 0;
int pageflags = 0;
ASSERT(szc != 0);
pgcnt = page_get_pagecnt(szc);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
npages = btopr(size);
ASSERT(IS_P2ALIGNED(npages, pgcnt));
ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
while (npages > 0) {
index = an_idx;
/*
* Find the next valid slot.
*/
if (anon_get_next_ptr(ahp, &index) == NULL) {
break;
}
ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
/*
* Now backup index to the beginning of the
* current large page region of the anon array.
*/
index = P2ALIGN(index, pgcnt);
off = index - an_idx;
ASSERT(IS_P2ALIGNED(off, pgcnt));
npages -= off;
if (npages <= 0)
break;
an_idx += off;
vp_off += ptob(off);
addr += ptob(off);
if (vpage != NULL) {
vpage += off;
}
for (i = 0; i < pgcnt; i++, an_idx++, vp_off += PAGESIZE) {
if ((ap = anon_get_ptr(ahp, an_idx)) == NULL) {
page_t *pl[1 + 1];
page_t *pp;
err = VOP_GETPAGE(vp, vp_off, PAGESIZE, NULL,
pl, PAGESIZE, seg, addr, S_READ, cred,
NULL);
if (err) {
break;
}
if (vpage != NULL) {
prot = VPP_PROT(vpage);
pageflags = VPP_ISPPLOCK(vpage) ?
LOCK_PAGE : 0;
}
pp = anon_private(&ap, seg, addr, prot, pl[0],
pageflags, cred);
if (pp == NULL) {
err = ENOMEM;
break;
}
(void) anon_set_ptr(ahp, an_idx, ap,
ANON_SLEEP);
page_unlock(pp);
}
ASSERT(ap->an_refcnt == 1);
addr += PAGESIZE;
if (vpage != NULL) {
vpage++;
}
}
npages -= pgcnt;
}
return (err);
}
/*
* Free a group of "size" anon pages, size in bytes,
* and clear out the pointers to the anon entries.
*/
void
anon_free(struct anon_hdr *ahp, ulong_t index, size_t size)
{
spgcnt_t npages;
struct anon *ap;
ulong_t old;
npages = btopr(size);
while (npages > 0) {
old = index;
if ((ap = anon_get_next_ptr(ahp, &index)) == NULL)
break;
ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
npages -= index - old;
if (npages <= 0)
break;
(void) anon_set_ptr(ahp, index, NULL, ANON_SLEEP);
anon_decref(ap);
/*
* Bump index and decrement page count
*/
index++;
npages--;
}
}
void
anon_free_pages(
struct anon_hdr *ahp,
ulong_t an_idx,
size_t size,
uint_t szc)
{
spgcnt_t npages;
pgcnt_t pgcnt;
ulong_t index, off;
ASSERT(szc != 0);
pgcnt = page_get_pagecnt(szc);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
npages = btopr(size);
ASSERT(IS_P2ALIGNED(npages, pgcnt));
ASSERT(IS_P2ALIGNED(an_idx, pgcnt));
ASSERT(an_idx < ahp->size);
VM_STAT_ADD(anonvmstats.freepages[0]);
while (npages > 0) {
index = an_idx;
/*
* Find the next valid slot.
*/
if (anon_get_next_ptr(ahp, &index) == NULL)
break;
ASSERT(!ANON_ISBUSY(anon_get_slot(ahp, index)));
/*
* Now backup index to the beginning of the
* current large page region of the old array.
*/
index = P2ALIGN(index, pgcnt);
off = index - an_idx;
ASSERT(IS_P2ALIGNED(off, pgcnt));
npages -= off;
if (npages <= 0)
break;
anon_decref_pages(ahp, index, szc);
off += pgcnt;
an_idx += off;
npages -= pgcnt;
}
}
/*
* Make anonymous pages discardable
*/
int
anon_disclaim(struct anon_map *amp, ulong_t index, size_t size,
uint_t behav, pgcnt_t *purged)
{
spgcnt_t npages = btopr(size);
struct anon *ap;
struct vnode *vp;
anoff_t off;
page_t *pp, *root_pp;
kmutex_t *ahm;
pgcnt_t pgcnt, npurged = 0;
ulong_t old_idx, idx, i;
struct anon_hdr *ahp = amp->ahp;
anon_sync_obj_t cookie;
int err = 0;
VERIFY(behav == MADV_FREE || behav == MADV_PURGE);
ASSERT(RW_READ_HELD(&->a_rwlock));
pgcnt = 1;
for (; npages > 0; index = (pgcnt == 1) ? index + 1 :
P2ROUNDUP(index + 1, pgcnt), npages -= pgcnt) {
/*
* get anon pointer and index for the first valid entry
* in the anon list, starting from "index"
*/
old_idx = index;
if ((ap = anon_get_next_ptr(ahp, &index)) == NULL)
break;
/*
* decrement npages by number of NULL anon slots we skipped
*/
npages -= index - old_idx;
if (npages <= 0)
break;
anon_array_enter(amp, index, &cookie);
ap = anon_get_ptr(ahp, index);
ASSERT(ap != NULL);
/*
* Get anonymous page and try to lock it SE_EXCL;
* if we couldn't grab the lock we skip to next page.
*/
swap_xlate(ap, &vp, &off);
pp = page_lookup_nowait(vp, (u_offset_t)off, SE_EXCL);
if (pp == NULL) {
segadvstat.MADV_FREE_miss.value.ul++;
pgcnt = 1;
anon_array_exit(&cookie);
continue;
}
pgcnt = page_get_pagecnt(pp->p_szc);
/*
* we cannot free a page which is permanently locked.
* The page_struct_lock need not be acquired to examine
* these fields since the page has an "exclusive" lock.
*/
if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
page_unlock(pp);
segadvstat.MADV_FREE_miss.value.ul++;
anon_array_exit(&cookie);
err = EBUSY;
continue;
}
ahm = AH_MUTEX(vp, off);
mutex_enter(ahm);
ASSERT(ap->an_refcnt != 0);
/*
* skip this one if copy-on-write is not yet broken.
*/
if (ap->an_refcnt > 1) {
mutex_exit(ahm);
page_unlock(pp);
segadvstat.MADV_FREE_miss.value.ul++;
anon_array_exit(&cookie);
continue;
}
if (behav == MADV_PURGE && pp->p_szc != 0) {
/*
* If we're purging and we have a large page, simplify
* things a bit by demoting ourselves into the base
* page case.
*/
(void) page_try_demote_pages(pp);
}
if (pp->p_szc == 0) {
pgcnt = 1;
/*
* free swap slot;
*/
if (ap->an_pvp) {
swap_phys_free(ap->an_pvp, ap->an_poff,
PAGESIZE);
ap->an_pvp = NULL;
ap->an_poff = 0;
}
if (behav == MADV_PURGE) {
/*
* If we're purging (instead of merely freeing),
* rip out this anon structure entirely to
* assure that any subsequent fault pulls from
* the backing vnode (if any).
*/
if (--ap->an_refcnt == 0)
anon_rmhash(ap);
mutex_exit(ahm);
(void) anon_set_ptr(ahp, index,
NULL, ANON_SLEEP);
npurged++;
ANI_ADD(1);
kmem_cache_free(anon_cache, ap);
} else {
mutex_exit(ahm);
}
segadvstat.MADV_FREE_hit.value.ul++;
/*
* while we are at it, unload all the translations
* and attempt to free the page.
*/
(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
/*LINTED: constant in conditional context */
VN_DISPOSE(pp,
behav == MADV_FREE ? B_FREE : B_INVAL, 0, kcred);
anon_array_exit(&cookie);
continue;
}
pgcnt = page_get_pagecnt(pp->p_szc);
if (!IS_P2ALIGNED(index, pgcnt) || npages < pgcnt) {
if (!page_try_demote_pages(pp)) {
mutex_exit(ahm);
page_unlock(pp);
segadvstat.MADV_FREE_miss.value.ul++;
anon_array_exit(&cookie);
err = EBUSY;
continue;
} else {
pgcnt = 1;
if (ap->an_pvp) {
swap_phys_free(ap->an_pvp,
ap->an_poff, PAGESIZE);
ap->an_pvp = NULL;
ap->an_poff = 0;
}
mutex_exit(ahm);
(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
/*LINTED*/
VN_DISPOSE(pp, B_FREE, 0, kcred);
segadvstat.MADV_FREE_hit.value.ul++;
anon_array_exit(&cookie);
continue;
}
}
mutex_exit(ahm);
root_pp = pp;
/*
* try to lock remaining pages
*/
for (idx = 1; idx < pgcnt; idx++) {
pp++;
if (!page_trylock(pp, SE_EXCL))
break;
if (pp->p_lckcnt != 0 || pp->p_cowcnt != 0) {
page_unlock(pp);
break;
}
}
if (idx == pgcnt) {
for (i = 0; i < pgcnt; i++) {
ap = anon_get_ptr(ahp, index + i);
if (ap == NULL)
break;
swap_xlate(ap, &vp, &off);
ahm = AH_MUTEX(vp, off);
mutex_enter(ahm);
ASSERT(ap->an_refcnt != 0);
/*
* skip this one if copy-on-write
* is not yet broken.
*/
if (ap->an_refcnt > 1) {
mutex_exit(ahm);
goto skiplp;
}
if (ap->an_pvp) {
swap_phys_free(ap->an_pvp,
ap->an_poff, PAGESIZE);
ap->an_pvp = NULL;
ap->an_poff = 0;
}
mutex_exit(ahm);
}
page_destroy_pages(root_pp);
segadvstat.MADV_FREE_hit.value.ul += pgcnt;
anon_array_exit(&cookie);
continue;
}
skiplp:
segadvstat.MADV_FREE_miss.value.ul += pgcnt;
for (i = 0, pp = root_pp; i < idx; pp++, i++)
page_unlock(pp);
anon_array_exit(&cookie);
}
if (purged != NULL)
*purged = npurged;
return (err);
}
/*
* Return the kept page(s) and protections back to the segment driver.
*/
int
anon_getpage(
struct anon **app,
uint_t *protp,
page_t *pl[],
size_t plsz,
struct seg *seg,
caddr_t addr,
enum seg_rw rw,
struct cred *cred)
{
page_t *pp;
struct anon *ap = *app;
struct vnode *vp;
anoff_t off;
int err;
kmutex_t *ahm;
swap_xlate(ap, &vp, &off);
/*
* Lookup the page. If page is being paged in,
* wait for it to finish as we must return a list of
* pages since this routine acts like the VOP_GETPAGE
* routine does.
*/
if (pl != NULL && (pp = page_lookup(vp, (u_offset_t)off, SE_SHARED))) {
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
if (ap->an_refcnt == 1)
*protp = PROT_ALL;
else
*protp = PROT_ALL & ~PROT_WRITE;
mutex_exit(ahm);
pl[0] = pp;
pl[1] = NULL;
return (0);
}
/*
* Simply treat it as a vnode fault on the anon vp.
*/
TRACE_3(TR_FAC_VM, TR_ANON_GETPAGE,
"anon_getpage:seg %x addr %x vp %x",
seg, addr, vp);
err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, protp, pl, plsz,
seg, addr, rw, cred, NULL);
if (err == 0 && pl != NULL) {
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
if (ap->an_refcnt != 1)
*protp &= ~PROT_WRITE; /* make read-only */
mutex_exit(ahm);
}
return (err);
}
/*
* Creates or returns kept pages to the segment driver. returns -1 if a large
* page cannot be allocated. returns -2 if some other process has allocated a
* larger page.
*
* For cowfault it will allocate any size pages to fill the requested area to
* avoid partially overwriting anon slots (i.e. sharing only some of the anon
* slots within a large page with other processes). This policy greatly
* simplifies large page freeing (which is only freed when all anon slot
* refcnts are 0).
*/
int
anon_map_getpages(
struct anon_map *amp,
ulong_t start_idx,
uint_t szc,
struct seg *seg,
caddr_t addr,
uint_t prot,
uint_t *protp,
page_t *ppa[],
uint_t *ppa_szc,
struct vpage vpage[],
enum seg_rw rw,
int brkcow,
int anypgsz,
int pgflags,
struct cred *cred)
{
pgcnt_t pgcnt;
struct anon *ap;
struct vnode *vp;
anoff_t off;
page_t *pp, *pl[2], *conpp = NULL;
caddr_t vaddr;
ulong_t pg_idx, an_idx, i;
spgcnt_t nreloc = 0;
int prealloc = 1;
int err, slotcreate;
uint_t vpprot;
int upsize = (szc < seg->s_szc);
#if !defined(__i386) && !defined(__amd64)
ASSERT(seg->s_szc != 0);
#endif
ASSERT(szc <= seg->s_szc);
ASSERT(ppa_szc != NULL);
ASSERT(rw != S_CREATE);
*protp = PROT_ALL;
VM_STAT_ADD(anonvmstats.getpages[0]);
if (szc == 0) {
VM_STAT_ADD(anonvmstats.getpages[1]);
if ((ap = anon_get_ptr(amp->ahp, start_idx)) != NULL) {
err = anon_getpage(&ap, protp, pl, PAGESIZE, seg,
addr, rw, cred);
if (err)
return (err);
ppa[0] = pl[0];
if (brkcow == 0 || (*protp & PROT_WRITE)) {
VM_STAT_ADD(anonvmstats.getpages[2]);
if (ppa[0]->p_szc != 0 && upsize) {
VM_STAT_ADD(anonvmstats.getpages[3]);
*ppa_szc = MIN(ppa[0]->p_szc,
seg->s_szc);
page_unlock(ppa[0]);
return (-2);
}
return (0);
}
panic("anon_map_getpages: cowfault for szc 0");
} else {
VM_STAT_ADD(anonvmstats.getpages[4]);
ppa[0] = anon_zero(seg, addr, &ap, cred);
if (ppa[0] == NULL)
return (ENOMEM);
(void) anon_set_ptr(amp->ahp, start_idx, ap,
ANON_SLEEP);
return (0);
}
}
pgcnt = page_get_pagecnt(szc);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
/*
* First we check for the case that the requtested large
* page or larger page already exists in the system.
* Actually we only check if the first constituent page
* exists and only preallocate if it's not found.
*/
ap = anon_get_ptr(amp->ahp, start_idx);
if (ap) {
uint_t pszc;
swap_xlate(ap, &vp, &off);
if (page_exists_forreal(vp, (u_offset_t)off, &pszc)) {
if (pszc > szc && upsize) {
*ppa_szc = MIN(pszc, seg->s_szc);
return (-2);
}
if (pszc >= szc) {
prealloc = 0;
}
}
}
VM_STAT_COND_ADD(prealloc == 0, anonvmstats.getpages[5]);
VM_STAT_COND_ADD(prealloc != 0, anonvmstats.getpages[6]);
top:
/*
* If a smaller page or no page at all was found,
* grab a large page off the freelist.
*/
if (prealloc) {
ASSERT(conpp == NULL);
if (page_alloc_pages(anon_vp, seg, addr, NULL, ppa,
szc, 0, pgflags) != 0) {
VM_STAT_ADD(anonvmstats.getpages[7]);
if (brkcow == 0 || szc < seg->s_szc ||
!anon_szcshare(amp->ahp, start_idx)) {
/*
* If the refcnt's of all anon slots are <= 1
* they can't increase since we are holding
* the address space's lock. So segvn can
* safely decrease szc without risking to
* generate a cow fault for the region smaller
* than the segment's largest page size.
*/
VM_STAT_ADD(anonvmstats.getpages[8]);
return (-1);
}
docow:
/*
* This is a cow fault. Copy away the entire 1 large
* page region of this segment.
*/
if (szc != seg->s_szc)
panic("anon_map_getpages: cowfault for szc %d",
szc);
vaddr = addr;
for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt;
pg_idx++, an_idx++, vaddr += PAGESIZE) {
if ((ap = anon_get_ptr(amp->ahp, an_idx)) !=
NULL) {
err = anon_getpage(&ap, &vpprot, pl,
PAGESIZE, seg, vaddr, rw, cred);
if (err) {
for (i = 0; i < pg_idx; i++) {
if ((pp = ppa[i]) !=
NULL)
page_unlock(pp);
}
return (err);
}
ppa[pg_idx] = pl[0];
} else {
/*
* Since this is a cowfault we know
* that this address space has a
* parent or children which means
* anon_dup_fill_holes() has initialized
* all anon slots within a large page
* region that had at least one anon
* slot at the time of fork().
*/
panic("anon_map_getpages: "
"cowfault but anon slot is empty");
}
}
VM_STAT_ADD(anonvmstats.getpages[9]);
*protp = PROT_ALL;
return (anon_map_privatepages(amp, start_idx, szc, seg,
addr, prot, ppa, vpage, anypgsz, pgflags, cred));
}
}
VM_STAT_ADD(anonvmstats.getpages[10]);
an_idx = start_idx;
pg_idx = 0;
vaddr = addr;
while (pg_idx < pgcnt) {
slotcreate = 0;
if ((ap = anon_get_ptr(amp->ahp, an_idx)) == NULL) {
VM_STAT_ADD(anonvmstats.getpages[11]);
/*
* For us to have decided not to preallocate
* would have meant that a large page
* was found. Which also means that all of the
* anon slots for that page would have been
* already created for us.
*/
if (prealloc == 0)
panic("anon_map_getpages: prealloc = 0");
slotcreate = 1;
ap = anon_alloc(NULL, 0);
}
swap_xlate(ap, &vp, &off);
/*
* Now setup our preallocated page to pass down
* to swap_getpage().
*/
if (prealloc) {
ASSERT(ppa[pg_idx]->p_szc == szc);
conpp = ppa[pg_idx];
}
ASSERT(prealloc || conpp == NULL);
/*
* If we just created this anon slot then call
* with S_CREATE to prevent doing IO on the page.
* Similar to the anon_zero case.
*/
err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE,
NULL, pl, PAGESIZE, conpp, ppa_szc, &nreloc, seg, vaddr,
slotcreate == 1 ? S_CREATE : rw, cred);
if (err) {
ASSERT(err != -2 || upsize);
VM_STAT_ADD(anonvmstats.getpages[12]);
ASSERT(slotcreate == 0);
goto io_err;
}
pp = pl[0];
if (pp->p_szc < szc || (pp->p_szc > szc && upsize)) {
VM_STAT_ADD(anonvmstats.getpages[13]);
ASSERT(slotcreate == 0);
ASSERT(prealloc == 0);
ASSERT(pg_idx == 0);
if (pp->p_szc > szc) {
ASSERT(upsize);
*ppa_szc = MIN(pp->p_szc, seg->s_szc);
page_unlock(pp);
VM_STAT_ADD(anonvmstats.getpages[14]);
return (-2);
}
page_unlock(pp);
prealloc = 1;
goto top;
}
/*
* If we decided to preallocate but VOP_GETPAGE
* found a page in the system that satisfies our
* request then free up our preallocated large page
* and continue looping accross the existing large
* page via VOP_GETPAGE.
*/
if (prealloc && pp != ppa[pg_idx]) {
VM_STAT_ADD(anonvmstats.getpages[15]);
ASSERT(slotcreate == 0);
ASSERT(pg_idx == 0);
conpp = NULL;
prealloc = 0;
page_free_pages(ppa[0]);
}
if (prealloc && nreloc > 1) {
/*
* we have relocated out of a smaller large page.
* skip npgs - 1 iterations and continue which will
* increment by one the loop indices.
*/
spgcnt_t npgs = nreloc;
VM_STAT_ADD(anonvmstats.getpages[16]);
ASSERT(pp == ppa[pg_idx]);
ASSERT(slotcreate == 0);
ASSERT(pg_idx + npgs <= pgcnt);
if ((*protp & PROT_WRITE) &&
anon_share(amp->ahp, an_idx, npgs)) {
*protp &= ~PROT_WRITE;
}
pg_idx += npgs;
an_idx += npgs;
vaddr += PAGESIZE * npgs;
continue;
}
VM_STAT_ADD(anonvmstats.getpages[17]);
/*
* Anon_zero case.
*/
if (slotcreate) {
ASSERT(prealloc);
pagezero(pp, 0, PAGESIZE);
CPU_STATS_ADD_K(vm, zfod, 1);
hat_setrefmod(pp);
}
ASSERT(prealloc == 0 || ppa[pg_idx] == pp);
ASSERT(prealloc != 0 || PAGE_SHARED(pp));
ASSERT(prealloc == 0 || PAGE_EXCL(pp));
if (pg_idx > 0 &&
((page_pptonum(pp) != page_pptonum(ppa[pg_idx - 1]) + 1) ||
(pp->p_szc != ppa[pg_idx - 1]->p_szc))) {
panic("anon_map_getpages: unexpected page");
} else if (pg_idx == 0 && (page_pptonum(pp) & (pgcnt - 1))) {
panic("anon_map_getpages: unaligned page");
}
if (prealloc == 0) {
ppa[pg_idx] = pp;
}
if (ap->an_refcnt > 1) {
VM_STAT_ADD(anonvmstats.getpages[18]);
*protp &= ~PROT_WRITE;
}
/*
* If this is a new anon slot then initialize
* the anon array entry.
*/
if (slotcreate) {
(void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP);
}
pg_idx++;
an_idx++;
vaddr += PAGESIZE;
}
/*
* Since preallocated pages come off the freelist
* they are locked SE_EXCL. Simply downgrade and return.
*/
if (prealloc) {
VM_STAT_ADD(anonvmstats.getpages[19]);
conpp = NULL;
for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
page_downgrade(ppa[pg_idx]);
}
}
ASSERT(conpp == NULL);
if (brkcow == 0 || (*protp & PROT_WRITE)) {
VM_STAT_ADD(anonvmstats.getpages[20]);
return (0);
}
if (szc < seg->s_szc)
panic("anon_map_getpages: cowfault for szc %d", szc);
VM_STAT_ADD(anonvmstats.getpages[21]);
*protp = PROT_ALL;
return (anon_map_privatepages(amp, start_idx, szc, seg, addr, prot,
ppa, vpage, anypgsz, pgflags, cred));
io_err:
/*
* We got an IO error somewhere in our large page.
* If we were using a preallocated page then just demote
* all the constituent pages that we've succeeded with sofar
* to PAGESIZE pages and leave them in the system
* unlocked.
*/
ASSERT(err != -2 || ((pg_idx == 0) && upsize));
VM_STAT_COND_ADD(err > 0, anonvmstats.getpages[22]);
VM_STAT_COND_ADD(err == -1, anonvmstats.getpages[23]);
VM_STAT_COND_ADD(err == -2, anonvmstats.getpages[24]);
if (prealloc) {
conpp = NULL;
if (pg_idx > 0) {
VM_STAT_ADD(anonvmstats.getpages[25]);
for (i = 0; i < pgcnt; i++) {
pp = ppa[i];
ASSERT(PAGE_EXCL(pp));
ASSERT(pp->p_szc == szc);
pp->p_szc = 0;
}
for (i = 0; i < pg_idx; i++) {
ASSERT(!hat_page_is_mapped(ppa[i]));
page_unlock(ppa[i]);
}
/*
* Now free up the remaining unused constituent
* pages.
*/
while (pg_idx < pgcnt) {
ASSERT(!hat_page_is_mapped(ppa[pg_idx]));
page_free(ppa[pg_idx], 0);
pg_idx++;
}
} else {
VM_STAT_ADD(anonvmstats.getpages[26]);
page_free_pages(ppa[0]);
}
} else {
VM_STAT_ADD(anonvmstats.getpages[27]);
ASSERT(err > 0);
for (i = 0; i < pg_idx; i++)
page_unlock(ppa[i]);
}
ASSERT(conpp == NULL);
if (err != -1)
return (err);
/*
* we are here because we failed to relocate.
*/
ASSERT(prealloc);
if (brkcow == 0 || szc < seg->s_szc ||
!anon_szcshare(amp->ahp, start_idx)) {
VM_STAT_ADD(anonvmstats.getpages[28]);
return (-1);
}
VM_STAT_ADD(anonvmstats.getpages[29]);
goto docow;
}
/*
* Turn a reference to an object or shared anon page
* into a private page with a copy of the data from the
* original page which is always locked by the caller.
* This routine unloads the translation and unlocks the
* original page, if it isn't being stolen, before returning
* to the caller.
*
* NOTE: The original anon slot is not freed by this routine
* It must be freed by the caller while holding the
* "anon_map" lock to prevent races which can occur if
* a process has multiple lwps in its address space.
*/
page_t *
anon_private(
struct anon **app,
struct seg *seg,
caddr_t addr,
uint_t prot,
page_t *opp,
int oppflags,
struct cred *cred)
{
struct anon *old = *app;
struct anon *new;
page_t *pp = NULL;
struct vnode *vp;
anoff_t off;
page_t *anon_pl[1 + 1];
int err;
if (oppflags & STEAL_PAGE)
ASSERT(PAGE_EXCL(opp));
else
ASSERT(PAGE_LOCKED(opp));
CPU_STATS_ADD_K(vm, cow_fault, 1);
/* Kernel probe */
TNF_PROBE_1(anon_private, "vm pagefault", /* CSTYLED */,
tnf_opaque, address, addr);
*app = new = anon_alloc(NULL, 0);
swap_xlate(new, &vp, &off);
if (oppflags & STEAL_PAGE) {
page_rename(opp, vp, (u_offset_t)off);
pp = opp;
TRACE_5(TR_FAC_VM, TR_ANON_PRIVATE,
"anon_private:seg %p addr %x pp %p vp %p off %lx",
seg, addr, pp, vp, off);
hat_setmod(pp);
/* bug 4026339 */
page_downgrade(pp);
return (pp);
}
/*
* Call the VOP_GETPAGE routine to create the page, thereby
* enabling the vnode driver to allocate any filesystem
* space (e.g., disk block allocation for UFS). This also
* prevents more than one page from being added to the
* vnode at the same time.
*/
err = VOP_GETPAGE(vp, (u_offset_t)off, PAGESIZE, NULL,
anon_pl, PAGESIZE, seg, addr, S_CREATE, cred, NULL);
if (err)
goto out;
pp = anon_pl[0];
/*
* If the original page was locked, we need to move the lock
* to the new page by transfering 'cowcnt/lckcnt' of the original
* page to 'cowcnt/lckcnt' of the new page.
*
* See Statement at the beginning of segvn_lockop() and
* comments in page_pp_useclaim() regarding the way
* cowcnts/lckcnts are handled.
*
* Also availrmem must be decremented up front for read only mapping
* before calling page_pp_useclaim. page_pp_useclaim will bump it back
* if availrmem did not need to be decremented after all.
*/
if (oppflags & LOCK_PAGE) {
if ((prot & PROT_WRITE) == 0) {
mutex_enter(&freemem_lock);
if (availrmem > pages_pp_maximum) {
availrmem--;
pages_useclaim++;
} else {
mutex_exit(&freemem_lock);
goto out;
}
mutex_exit(&freemem_lock);
}
page_pp_useclaim(opp, pp, prot & PROT_WRITE);
}
/*
* Now copy the contents from the original page,
* which is locked and loaded in the MMU by
* the caller to prevent yet another page fault.
*/
/* XXX - should set mod bit in here */
if (ppcopy(opp, pp) == 0) {
/*
* Before ppcopy could hanlde UE or other faults, we
* would have panicked here, and still have no option
* but to do so now.
*/
panic("anon_private, ppcopy failed, opp = 0x%p, pp = 0x%p",
(void *)opp, (void *)pp);
}
hat_setrefmod(pp); /* mark as modified */
/*
* Unload the old translation.
*/
hat_unload(seg->s_as->a_hat, addr, PAGESIZE, HAT_UNLOAD);
/*
* Free unmapped, unmodified original page.
* or release the lock on the original page,
* otherwise the process will sleep forever in
* anon_decref() waiting for the "exclusive" lock
* on the page.
*/
(void) page_release(opp, 1);
/*
* we are done with page creation so downgrade the new
* page's selock to shared, this helps when multiple
* as_fault(...SOFTLOCK...) are done to the same
* page(aio)
*/
page_downgrade(pp);
/*
* NOTE: The original anon slot must be freed by the
* caller while holding the "anon_map" lock, if we
* copied away from an anonymous page.
*/
return (pp);
out:
*app = old;
if (pp)
page_unlock(pp);
anon_decref(new);
page_unlock(opp);
return ((page_t *)NULL);
}
int
anon_map_privatepages(
struct anon_map *amp,
ulong_t start_idx,
uint_t szc,
struct seg *seg,
caddr_t addr,
uint_t prot,
page_t *ppa[],
struct vpage vpage[],
int anypgsz,
int pgflags,
struct cred *cred)
{
pgcnt_t pgcnt;
struct vnode *vp;
anoff_t off;
page_t *pl[2], *conpp = NULL;
int err;
int prealloc = 1;
struct anon *ap, *oldap;
caddr_t vaddr;
page_t *pplist, *pp;
ulong_t pg_idx, an_idx;
spgcnt_t nreloc = 0;
int pagelock = 0;
kmutex_t *ahmpages = NULL;
#ifdef DEBUG
int refcnt;
#endif
ASSERT(szc != 0);
ASSERT(szc == seg->s_szc);
VM_STAT_ADD(anonvmstats.privatepages[0]);
pgcnt = page_get_pagecnt(szc);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
ASSERT(amp != NULL);
ap = anon_get_ptr(amp->ahp, start_idx);
ASSERT(ap == NULL || ap->an_refcnt >= 1);
VM_STAT_COND_ADD(ap == NULL, anonvmstats.privatepages[1]);
/*
* Now try and allocate the large page. If we fail then just
* let VOP_GETPAGE give us PAGESIZE pages. Normally we let
* the caller make this decision but to avoid added complexity
* it's simplier to handle that case here.
*/
if (anypgsz == -1) {
VM_STAT_ADD(anonvmstats.privatepages[2]);
prealloc = 0;
} else if (page_alloc_pages(anon_vp, seg, addr, &pplist, NULL, szc,
anypgsz, pgflags) != 0) {
VM_STAT_ADD(anonvmstats.privatepages[3]);
prealloc = 0;
}
/*
* make the decrement of all refcnts of all
* anon slots of a large page appear atomic by
* getting an anonpages_hash_lock for the
* first anon slot of a large page.
*/
if (ap != NULL) {
ahmpages = APH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahmpages);
if (ap->an_refcnt == 1) {
VM_STAT_ADD(anonvmstats.privatepages[4]);
ASSERT(!anon_share(amp->ahp, start_idx, pgcnt));
mutex_exit(ahmpages);
if (prealloc) {
page_free_replacement_page(pplist);
page_create_putback(pgcnt);
}
ASSERT(ppa[0]->p_szc <= szc);
if (ppa[0]->p_szc == szc) {
VM_STAT_ADD(anonvmstats.privatepages[5]);
return (0);
}
for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
ASSERT(ppa[pg_idx] != NULL);
page_unlock(ppa[pg_idx]);
}
return (-1);
}
}
/*
* If we are passed in the vpage array and this is
* not PROT_WRITE then we need to decrement availrmem
* up front before we try anything. If we need to and
* can't decrement availrmem then its better to fail now
* than in the middle of processing the new large page.
* page_pp_usclaim() on behalf of each constituent page
* below will adjust availrmem back for the cases not needed.
*/
if (vpage != NULL && (prot & PROT_WRITE) == 0) {
for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
if (VPP_ISPPLOCK(&vpage[pg_idx])) {
pagelock = 1;
break;
}
}
if (pagelock) {
VM_STAT_ADD(anonvmstats.privatepages[6]);
mutex_enter(&freemem_lock);
if (availrmem >= pages_pp_maximum + pgcnt) {
availrmem -= pgcnt;
pages_useclaim += pgcnt;
} else {
VM_STAT_ADD(anonvmstats.privatepages[7]);
mutex_exit(&freemem_lock);
if (ahmpages != NULL) {
mutex_exit(ahmpages);
}
if (prealloc) {
page_free_replacement_page(pplist);
page_create_putback(pgcnt);
}
for (pg_idx = 0; pg_idx < pgcnt; pg_idx++)
if (ppa[pg_idx] != NULL)
page_unlock(ppa[pg_idx]);
return (ENOMEM);
}
mutex_exit(&freemem_lock);
}
}
CPU_STATS_ADD_K(vm, cow_fault, pgcnt);
VM_STAT_ADD(anonvmstats.privatepages[8]);
an_idx = start_idx;
pg_idx = 0;
vaddr = addr;
for (; pg_idx < pgcnt; pg_idx++, an_idx++, vaddr += PAGESIZE) {
ASSERT(ppa[pg_idx] != NULL);
oldap = anon_get_ptr(amp->ahp, an_idx);
ASSERT(ahmpages != NULL || oldap == NULL);
ASSERT(ahmpages == NULL || oldap != NULL);
ASSERT(ahmpages == NULL || oldap->an_refcnt > 1);
ASSERT(ahmpages == NULL || pg_idx != 0 ||
(refcnt = oldap->an_refcnt));
ASSERT(ahmpages == NULL || pg_idx == 0 ||
refcnt == oldap->an_refcnt);
ap = anon_alloc(NULL, 0);
swap_xlate(ap, &vp, &off);
/*
* Now setup our preallocated page to pass down to
* swap_getpage().
*/
if (prealloc) {
pp = pplist;
page_sub(&pplist, pp);
conpp = pp;
}
err = swap_getconpage(vp, (u_offset_t)off, PAGESIZE, NULL, pl,
PAGESIZE, conpp, NULL, &nreloc, seg, vaddr,
S_CREATE, cred);
/*
* Impossible to fail this is S_CREATE.
*/
if (err)
panic("anon_map_privatepages: VOP_GETPAGE failed");
ASSERT(prealloc ? pp == pl[0] : pl[0]->p_szc == 0);
ASSERT(prealloc == 0 || nreloc == 1);
pp = pl[0];
/*
* If the original page was locked, we need to move
* the lock to the new page by transfering
* 'cowcnt/lckcnt' of the original page to 'cowcnt/lckcnt'
* of the new page. pg_idx can be used to index
* into the vpage array since the caller will guarentee
* that vpage struct passed in corresponds to addr
* and forward.
*/
if (vpage != NULL && VPP_ISPPLOCK(&vpage[pg_idx])) {
page_pp_useclaim(ppa[pg_idx], pp, prot & PROT_WRITE);
} else if (pagelock) {
mutex_enter(&freemem_lock);
availrmem++;
pages_useclaim--;
mutex_exit(&freemem_lock);
}
/*
* Now copy the contents from the original page.
*/
if (ppcopy(ppa[pg_idx], pp) == 0) {
/*
* Before ppcopy could hanlde UE or other faults, we
* would have panicked here, and still have no option
* but to do so now.
*/
panic("anon_map_privatepages, ppcopy failed");
}
hat_setrefmod(pp); /* mark as modified */
/*
* Release the lock on the original page,
* derement the old slot, and down grade the lock
* on the new copy.
*/
page_unlock(ppa[pg_idx]);
if (!prealloc)
page_downgrade(pp);
ppa[pg_idx] = pp;
/*
* Now reflect the copy in the new anon array.
*/
ASSERT(ahmpages == NULL || oldap->an_refcnt > 1);
if (oldap != NULL)
anon_decref(oldap);
(void) anon_set_ptr(amp->ahp, an_idx, ap, ANON_SLEEP);
}
/*
* Unload the old large page translation.
*/
hat_unload(seg->s_as->a_hat, addr, pgcnt << PAGESHIFT, HAT_UNLOAD);
if (ahmpages != NULL) {
mutex_exit(ahmpages);
}
ASSERT(prealloc == 0 || pplist == NULL);
if (prealloc) {
VM_STAT_ADD(anonvmstats.privatepages[9]);
for (pg_idx = 0; pg_idx < pgcnt; pg_idx++) {
page_downgrade(ppa[pg_idx]);
}
}
return (0);
}
/*
* Allocate a private zero-filled anon page.
*/
page_t *
anon_zero(struct seg *seg, caddr_t addr, struct anon **app, struct cred *cred)
{
struct anon *ap;
page_t *pp;
struct vnode *vp;
anoff_t off;
page_t *anon_pl[1 + 1];
int err;
/* Kernel probe */
TNF_PROBE_1(anon_zero, "vm pagefault", /* CSTYLED */,
tnf_opaque, address, addr);
*app = ap = anon_alloc(NULL, 0);
swap_xlate(ap, &vp, &off);
/*
* Call the VOP_GETPAGE routine to create the page, thereby
* enabling the vnode driver to allocate any filesystem
* dependent structures (e.g., disk block allocation for UFS).
* This also prevents more than on page from being added to
* the vnode at the same time since it is locked.
*/
err = VOP_GETPAGE(vp, off, PAGESIZE, NULL,
anon_pl, PAGESIZE, seg, addr, S_CREATE, cred, NULL);
if (err) {
*app = NULL;
anon_decref(ap);
return (NULL);
}
pp = anon_pl[0];
pagezero(pp, 0, PAGESIZE); /* XXX - should set mod bit */
page_downgrade(pp);
CPU_STATS_ADD_K(vm, zfod, 1);
hat_setrefmod(pp); /* mark as modified so pageout writes back */
return (pp);
}
/*
* Allocate array of private zero-filled anon pages for empty slots
* and kept pages for non empty slots within given range.
*
* NOTE: This rontine will try and use large pages
* if available and supported by underlying platform.
*/
int
anon_map_createpages(
struct anon_map *amp,
ulong_t start_index,
size_t len,
page_t *ppa[],
struct seg *seg,
caddr_t addr,
enum seg_rw rw,
struct cred *cred)
{
struct anon *ap;
struct vnode *ap_vp;
page_t *pp, *pplist, *anon_pl[1 + 1], *conpp = NULL;
int err = 0;
ulong_t p_index, index;
pgcnt_t npgs, pg_cnt;
spgcnt_t nreloc = 0;
uint_t l_szc, szc, prot;
anoff_t ap_off;
size_t pgsz;
lgrp_t *lgrp;
kmutex_t *ahm;
/*
* XXX For now only handle S_CREATE.
*/
ASSERT(rw == S_CREATE);
index = start_index;
p_index = 0;
npgs = btopr(len);
/*
* If this platform supports multiple page sizes
* then try and allocate directly from the free
* list for pages larger than PAGESIZE.
*
* NOTE:When we have page_create_ru we can stop
* directly allocating from the freelist.
*/
l_szc = seg->s_szc;
ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
while (npgs) {
/*
* if anon slot already exists
* (means page has been created)
* so 1) look up the page
* 2) if the page is still in memory, get it.
* 3) if not, create a page and
* page in from physical swap device.
* These are done in anon_getpage().
*/
ap = anon_get_ptr(amp->ahp, index);
if (ap) {
err = anon_getpage(&ap, &prot, anon_pl, PAGESIZE,
seg, addr, S_READ, cred);
if (err) {
ANON_LOCK_EXIT(&->a_rwlock);
panic("anon_map_createpages: anon_getpage");
}
pp = anon_pl[0];
ppa[p_index++] = pp;
/*
* an_pvp can become non-NULL after SysV's page was
* paged out before ISM was attached to this SysV
* shared memory segment. So free swap slot if needed.
*/
if (ap->an_pvp != NULL) {
page_io_lock(pp);
ahm = AH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahm);
if (ap->an_pvp != NULL) {
swap_phys_free(ap->an_pvp,
ap->an_poff, PAGESIZE);
ap->an_pvp = NULL;
ap->an_poff = 0;
mutex_exit(ahm);
hat_setmod(pp);
} else {
mutex_exit(ahm);
}
page_io_unlock(pp);
}
addr += PAGESIZE;
index++;
npgs--;
continue;
}
/*
* Now try and allocate the largest page possible
* for the current address and range.
* Keep dropping down in page size until:
*
* 1) Properly aligned
* 2) Does not overlap existing anon pages
* 3) Fits in remaining range.
* 4) able to allocate one.
*
* NOTE: XXX When page_create_ru is completed this code
* will change.
*/
szc = l_szc;
pplist = NULL;
pg_cnt = 0;
while (szc) {
pgsz = page_get_pagesize(szc);
pg_cnt = pgsz >> PAGESHIFT;
if (IS_P2ALIGNED(addr, pgsz) && pg_cnt <= npgs &&
anon_pages(amp->ahp, index, pg_cnt) == 0) {
/*
* XXX
* Since we are faking page_create()
* we also need to do the freemem and
* pcf accounting.
*/
(void) page_create_wait(pg_cnt, PG_WAIT);
/*
* Get lgroup to allocate next page of shared
* memory from and use it to specify where to
* allocate the physical memory
*/
lgrp = lgrp_mem_choose(seg, addr, pgsz);
pplist = page_get_freelist(
anon_vp, (u_offset_t)0, seg,
addr, pgsz, 0, lgrp);
if (pplist == NULL) {
page_create_putback(pg_cnt);
}
/*
* If a request for a page of size
* larger than PAGESIZE failed
* then don't try that size anymore.
*/
if (pplist == NULL) {
l_szc = szc - 1;
} else {
break;
}
}
szc--;
}
/*
* If just using PAGESIZE pages then don't
* directly allocate from the free list.
*/
if (pplist == NULL) {
ASSERT(szc == 0);
pp = anon_zero(seg, addr, &ap, cred);
if (pp == NULL) {
ANON_LOCK_EXIT(&->a_rwlock);
panic("anon_map_createpages: anon_zero");
}
ppa[p_index++] = pp;
ASSERT(anon_get_ptr(amp->ahp, index) == NULL);
(void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP);
addr += PAGESIZE;
index++;
npgs--;
continue;
}
/*
* pplist is a list of pg_cnt PAGESIZE pages.
* These pages are locked SE_EXCL since they
* came directly off the free list.
*/
ASSERT(IS_P2ALIGNED(pg_cnt, pg_cnt));
ASSERT(IS_P2ALIGNED(index, pg_cnt));
ASSERT(conpp == NULL);
while (pg_cnt--) {
ap = anon_alloc(NULL, 0);
swap_xlate(ap, &ap_vp, &ap_off);
ASSERT(pplist != NULL);
pp = pplist;
page_sub(&pplist, pp);
PP_CLRFREE(pp);
PP_CLRAGED(pp);
conpp = pp;
err = swap_getconpage(ap_vp, ap_off, PAGESIZE,
(uint_t *)NULL, anon_pl, PAGESIZE, conpp, NULL,
&nreloc, seg, addr, S_CREATE, cred);
if (err) {
ANON_LOCK_EXIT(&->a_rwlock);
panic("anon_map_createpages: S_CREATE");
}
ASSERT(anon_pl[0] == pp);
ASSERT(nreloc == 1);
pagezero(pp, 0, PAGESIZE);
CPU_STATS_ADD_K(vm, zfod, 1);
hat_setrefmod(pp);
ASSERT(anon_get_ptr(amp->ahp, index) == NULL);
(void) anon_set_ptr(amp->ahp, index, ap, ANON_SLEEP);
ppa[p_index++] = pp;
addr += PAGESIZE;
index++;
npgs--;
}
conpp = NULL;
pg_cnt = pgsz >> PAGESHIFT;
p_index = p_index - pg_cnt;
while (pg_cnt--) {
page_downgrade(ppa[p_index++]);
}
}
ANON_LOCK_EXIT(&->a_rwlock);
return (0);
}
static int
anon_try_demote_pages(
struct anon_hdr *ahp,
ulong_t sidx,
uint_t szc,
page_t **ppa,
int private)
{
struct anon *ap;
pgcnt_t pgcnt = page_get_pagecnt(szc);
page_t *pp;
pgcnt_t i;
kmutex_t *ahmpages = NULL;
int root = 0;
pgcnt_t npgs;
pgcnt_t curnpgs = 0;
size_t ppasize = 0;
ASSERT(szc != 0);
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
ASSERT(IS_P2ALIGNED(sidx, pgcnt));
ASSERT(sidx < ahp->size);
if (ppa == NULL) {
ppasize = pgcnt * sizeof (page_t *);
ppa = kmem_alloc(ppasize, KM_SLEEP);
}
ap = anon_get_ptr(ahp, sidx);
if (ap != NULL && private) {
VM_STAT_ADD(anonvmstats.demotepages[1]);
ahmpages = APH_MUTEX(ap->an_vp, ap->an_off);
mutex_enter(ahmpages);
}
if (ap != NULL && ap->an_refcnt > 1) {
if (ahmpages != NULL) {
VM_STAT_ADD(anonvmstats.demotepages[2]);
mutex_exit(ahmpages);
}
if (ppasize != 0) {
kmem_free(ppa, ppasize);
}
return (0);
}
if (ahmpages != NULL) {
mutex_exit(ahmpages);
}
if (ahp->size - sidx < pgcnt) {
ASSERT(private == 0);
pgcnt = ahp->size - sidx;
}
for (i = 0; i < pgcnt; i++, sidx++) {
ap = anon_get_ptr(ahp, sidx);
if (ap != NULL) {
if (ap->an_refcnt != 1) {
panic("anon_try_demote_pages: an_refcnt != 1");
}
pp = ppa[i] = page_lookup(ap->an_vp, ap->an_off,
SE_EXCL);
if (pp != NULL) {
(void) hat_pageunload(pp,
HAT_FORCE_PGUNLOAD);
}
} else {
ppa[i] = NULL;
}
}
for (i = 0; i < pgcnt; i++) {
if ((pp = ppa[i]) != NULL && pp->p_szc != 0) {
ASSERT(pp->p_szc <= szc);
if (!root) {
VM_STAT_ADD(anonvmstats.demotepages[3]);
if (curnpgs != 0)
panic("anon_try_demote_pages: "
"bad large page");
root = 1;
curnpgs = npgs =
page_get_pagecnt(pp->p_szc);
ASSERT(npgs <= pgcnt);
ASSERT(IS_P2ALIGNED(npgs, npgs));
ASSERT(!(page_pptonum(pp) & (npgs - 1)));
} else {
ASSERT(i > 0);
ASSERT(page_pptonum(pp) - 1 ==
page_pptonum(ppa[i - 1]));
if ((page_pptonum(pp) & (npgs - 1)) ==
npgs - 1)
root = 0;
}
ASSERT(PAGE_EXCL(pp));
pp->p_szc = 0;
ASSERT(curnpgs > 0);
curnpgs--;
}
}
if (root != 0 || curnpgs != 0)
panic("anon_try_demote_pages: bad large page");
for (i = 0; i < pgcnt; i++) {
if ((pp = ppa[i]) != NULL) {
ASSERT(!hat_page_is_mapped(pp));
ASSERT(pp->p_szc == 0);
page_unlock(pp);
}
}
if (ppasize != 0) {
kmem_free(ppa, ppasize);
}
return (1);
}
/*
* anon_map_demotepages() can only be called by MAP_PRIVATE segments.
*/
int
anon_map_demotepages(
struct anon_map *amp,
ulong_t start_idx,
struct seg *seg,
caddr_t addr,
uint_t prot,
struct vpage vpage[],
struct cred *cred)
{
struct anon *ap;
uint_t szc = seg->s_szc;
pgcnt_t pgcnt = page_get_pagecnt(szc);
size_t ppasize = pgcnt * sizeof (page_t *);
page_t **ppa = kmem_alloc(ppasize, KM_SLEEP);
page_t *pp;
page_t *pl[2];
pgcnt_t i, pg_idx;
ulong_t an_idx;
caddr_t vaddr;
int err;
int retry = 0;
uint_t vpprot;
ASSERT(RW_WRITE_HELD(&->a_rwlock));
ASSERT(IS_P2ALIGNED(pgcnt, pgcnt));
ASSERT(IS_P2ALIGNED(start_idx, pgcnt));
ASSERT(ppa != NULL);
ASSERT(szc != 0);
ASSERT(szc == amp->a_szc);
VM_STAT_ADD(anonvmstats.demotepages[0]);
top:
if (anon_try_demote_pages(amp->ahp, start_idx, szc, ppa, 1)) {
kmem_free(ppa, ppasize);
return (0);
}
VM_STAT_ADD(anonvmstats.demotepages[4]);
ASSERT(retry == 0); /* we can be here only once */
vaddr = addr;
for (pg_idx = 0, an_idx = start_idx; pg_idx < pgcnt;
pg_idx++, an_idx++, vaddr += PAGESIZE) {
ap = anon_get_ptr(amp->ahp, an_idx);
if (ap == NULL)
panic("anon_map_demotepages: no anon slot");
err = anon_getpage(&ap, &vpprot, pl, PAGESIZE, seg, vaddr,
S_READ, cred);
if (err) {
for (i = 0; i < pg_idx; i++) {
if ((pp = ppa[i]) != NULL)
page_unlock(pp);
}
kmem_free(ppa, ppasize);
return (err);
}
ppa[pg_idx] = pl[0];
}
err = anon_map_privatepages(amp, start_idx, szc, seg, addr, prot, ppa,
vpage, -1, 0, cred);
if (err > 0) {
VM_STAT_ADD(anonvmstats.demotepages[5]);
kmem_free(ppa, ppasize);
return (err);
}
ASSERT(err == 0 || err == -1);
if (err == -1) {
VM_STAT_ADD(anonvmstats.demotepages[6]);
retry = 1;
goto top;
}
for (i = 0; i < pgcnt; i++) {
ASSERT(ppa[i] != NULL);
if (ppa[i]->p_szc != 0)
retry = 1;
page_unlock(ppa[i]);
}
if (retry) {
VM_STAT_ADD(anonvmstats.demotepages[7]);
goto top;
}
VM_STAT_ADD(anonvmstats.demotepages[8]);
kmem_free(ppa, ppasize);
return (0);
}
/*
* Free pages of shared anon map. It's assumed that anon maps don't share anon
* structures with private anon maps. Therefore all anon structures should
* have at most one reference at this point. This means underlying pages can
* be exclusively locked and demoted or freed. If not freeing the entire
* large pages demote the ends of the region we free to be able to free
* subpages. Page roots correspond to aligned index positions in anon map.
*/
void
anon_shmap_free_pages(struct anon_map *amp, ulong_t sidx, size_t len)
{
ulong_t eidx = sidx + btopr(len);
pgcnt_t pages = page_get_pagecnt(amp->a_szc);
struct anon_hdr *ahp = amp->ahp;
ulong_t tidx;
size_t size;
ulong_t sidx_aligned;
ulong_t eidx_aligned;
ASSERT(ANON_WRITE_HELD(&->a_rwlock));
ASSERT(amp->refcnt <= 1);
ASSERT(amp->a_szc > 0);
ASSERT(eidx <= ahp->size);
ASSERT(!anon_share(ahp, sidx, btopr(len)));
if (len == 0) { /* XXX */
return;
}
sidx_aligned = P2ALIGN(sidx, pages);
if (sidx_aligned != sidx ||
(eidx < sidx_aligned + pages && eidx < ahp->size)) {
if (!anon_try_demote_pages(ahp, sidx_aligned,
amp->a_szc, NULL, 0)) {
panic("anon_shmap_free_pages: demote failed");
}
size = (eidx <= sidx_aligned + pages) ? (eidx - sidx) :
P2NPHASE(sidx, pages);
size <<= PAGESHIFT;
anon_free(ahp, sidx, size);
sidx = sidx_aligned + pages;
if (eidx <= sidx) {
return;
}
}
eidx_aligned = P2ALIGN(eidx, pages);
if (sidx < eidx_aligned) {
anon_free_pages(ahp, sidx,
(eidx_aligned - sidx) << PAGESHIFT,
amp->a_szc);
sidx = eidx_aligned;
}
ASSERT(sidx == eidx_aligned);
if (eidx == eidx_aligned) {
return;
}
tidx = eidx;
if (eidx != ahp->size && anon_get_next_ptr(ahp, &tidx) != NULL &&
tidx - sidx < pages) {
if (!anon_try_demote_pages(ahp, sidx, amp->a_szc, NULL, 0)) {
panic("anon_shmap_free_pages: demote failed");
}
size = (eidx - sidx) << PAGESHIFT;
anon_free(ahp, sidx, size);
} else {
anon_free_pages(ahp, sidx, pages << PAGESHIFT, amp->a_szc);
}
}
/*
* This routine should be called with amp's writer lock when there're no other
* users of amp. All pcache entries of this amp must have been already
* inactivated. We must not drop a_rwlock here to prevent new users from
* attaching to this amp.
*/
void
anonmap_purge(struct anon_map *amp)
{
ASSERT(ANON_WRITE_HELD(&->a_rwlock));
ASSERT(amp->refcnt <= 1);
if (amp->a_softlockcnt != 0) {
seg_ppurge(NULL, amp, 0);
}
/*
* Since all pcache entries were already inactive before this routine
* was called seg_ppurge() couldn't return while there're still
* entries that can be found via the list anchored at a_phead. So we
* can assert this list is empty now. a_softlockcnt may be still non 0
* if asynchronous thread that manages pcache already removed pcache
* entries but hasn't unlocked the pages yet. If a_softlockcnt is non
* 0 we just wait on a_purgecv for shamp_reclaim() to finish. Even if
* a_softlockcnt is 0 we grab a_purgemtx to avoid freeing anon map
* before shamp_reclaim() is done with it. a_purgemtx also taken by
* shamp_reclaim() while a_softlockcnt was still not 0 acts as a
* barrier that prevents anonmap_purge() to complete while
* shamp_reclaim() may still be referencing this amp.
*/
ASSERT(amp->a_phead.p_lnext == &->a_phead);
ASSERT(amp->a_phead.p_lprev == &->a_phead);
mutex_enter(&->a_purgemtx);
while (amp->a_softlockcnt != 0) {
ASSERT(amp->a_phead.p_lnext == &->a_phead);
ASSERT(amp->a_phead.p_lprev == &->a_phead);
amp->a_purgewait = 1;
cv_wait(&->a_purgecv, &->a_purgemtx);
}
mutex_exit(&->a_purgemtx);
ASSERT(amp->a_phead.p_lnext == &->a_phead);
ASSERT(amp->a_phead.p_lprev == &->a_phead);
ASSERT(amp->a_softlockcnt == 0);
}
/*
* Allocate and initialize an anon_map structure for seg
* associating the given swap reservation with the new anon_map.
*/
struct anon_map *
anonmap_alloc(size_t size, size_t swresv, int flags)
{
struct anon_map *amp;
int kmflags = (flags & ANON_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
amp = kmem_cache_alloc(anonmap_cache, kmflags);
if (amp == NULL) {
ASSERT(kmflags == KM_NOSLEEP);
return (NULL);
}
amp->ahp = anon_create(btopr(size), flags);
if (amp->ahp == NULL) {
ASSERT(flags == ANON_NOSLEEP);
kmem_cache_free(anonmap_cache, amp);
return (NULL);
}
amp->refcnt = 1;
amp->size = size;
amp->swresv = swresv;
amp->locality = 0;
amp->a_szc = 0;
amp->a_sp = NULL;
amp->a_softlockcnt = 0;
amp->a_purgewait = 0;
amp->a_phead.p_lnext = &->a_phead;
amp->a_phead.p_lprev = &->a_phead;
return (amp);
}
void
anonmap_free(struct anon_map *amp)
{
ASSERT(amp->ahp != NULL);
ASSERT(amp->refcnt == 0);
ASSERT(amp->a_softlockcnt == 0);
ASSERT(amp->a_phead.p_lnext == &->a_phead);
ASSERT(amp->a_phead.p_lprev == &->a_phead);
lgrp_shm_policy_fini(amp, NULL);
anon_release(amp->ahp, btopr(amp->size));
kmem_cache_free(anonmap_cache, amp);
}
/*
* Returns true if the app array has some empty slots.
* The offp and lenp parameters are in/out parameters. On entry
* these values represent the starting offset and length of the
* mapping. When true is returned, these values may be modified
* to be the largest range which includes empty slots.
*/
int
non_anon(struct anon_hdr *ahp, ulong_t anon_idx, u_offset_t *offp,
size_t *lenp)
{
ulong_t i, el;
ssize_t low, high;
struct anon *ap;
low = -1;
for (i = 0, el = *lenp; i < el; i += PAGESIZE, anon_idx++) {
ap = anon_get_ptr(ahp, anon_idx);
if (ap == NULL) {
if (low == -1)
low = i;
high = i;
}
}
if (low != -1) {
/*
* Found at least one non-anon page.
* Set up the off and len return values.
*/
if (low != 0)
*offp += low;
*lenp = high - low + PAGESIZE;
return (1);
}
return (0);
}
/*
* Return a count of the number of existing anon pages in the anon array
* app in the range (off, off+len). The array and slots must be guaranteed
* stable by the caller.
*/
pgcnt_t
anon_pages(struct anon_hdr *ahp, ulong_t anon_index, pgcnt_t nslots)
{
pgcnt_t cnt = 0;
while (nslots-- > 0) {
if ((anon_get_ptr(ahp, anon_index)) != NULL)
cnt++;
anon_index++;
}
return (cnt);
}
/*
* Move reserved phys swap into memory swap (unreserve phys swap
* and reserve mem swap by the same amount).
* Used by segspt when it needs to lock reserved swap npages in memory
*/
int
anon_swap_adjust(pgcnt_t npages)
{
pgcnt_t unlocked_mem_swap;
mutex_enter(&anoninfo_lock);
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
unlocked_mem_swap = k_anoninfo.ani_mem_resv
- k_anoninfo.ani_locked_swap;
if (npages > unlocked_mem_swap) {
spgcnt_t adjusted_swap = npages - unlocked_mem_swap;
/*
* if there is not enough unlocked mem swap we take missing
* amount from phys swap and give it to mem swap
*/
if (!page_reclaim_mem(adjusted_swap, segspt_minfree, 1)) {
mutex_exit(&anoninfo_lock);
return (ENOMEM);
}
k_anoninfo.ani_mem_resv += adjusted_swap;
ASSERT(k_anoninfo.ani_phys_resv >= adjusted_swap);
k_anoninfo.ani_phys_resv -= adjusted_swap;
ANI_ADD(adjusted_swap);
}
k_anoninfo.ani_locked_swap += npages;
ASSERT(k_anoninfo.ani_mem_resv >= k_anoninfo.ani_locked_swap);
ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
mutex_exit(&anoninfo_lock);
return (0);
}
/*
* 'unlocked' reserved mem swap so when it is unreserved it
* can be moved back phys (disk) swap
*/
void
anon_swap_restore(pgcnt_t npages)
{
mutex_enter(&anoninfo_lock);
ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv);
ASSERT(k_anoninfo.ani_locked_swap >= npages);
k_anoninfo.ani_locked_swap -= npages;
ASSERT(k_anoninfo.ani_locked_swap <= k_anoninfo.ani_mem_resv);
mutex_exit(&anoninfo_lock);
}
/*
* Return the pointer from the list for a
* specified anon index.
*/
ulong_t *
anon_get_slot(struct anon_hdr *ahp, ulong_t an_idx)
{
struct anon **app;
void **ppp;
ASSERT(an_idx < ahp->size);
/*
* Single level case.
*/
if ((ahp->size <= ANON_CHUNK_SIZE) || (ahp->flags & ANON_ALLOC_FORCE)) {
return ((ulong_t *)&ahp->array_chunk[an_idx]);
} else {
/*
* 2 level case.
*/
ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
if (*ppp == NULL) {
mutex_enter(&ahp->serial_lock);
ppp = &ahp->array_chunk[an_idx >> ANON_CHUNK_SHIFT];
if (*ppp == NULL)
*ppp = kmem_zalloc(PAGESIZE, KM_SLEEP);
mutex_exit(&ahp->serial_lock);
}
app = *ppp;
return ((ulong_t *)&app[an_idx & ANON_CHUNK_OFF]);
}
}
void
anon_array_enter(struct anon_map *amp, ulong_t an_idx, anon_sync_obj_t *sobj)
{
ulong_t *ap_slot;
kmutex_t *mtx;
kcondvar_t *cv;
int hash;
/*
* Use szc to determine anon slot(s) to appear atomic.
* If szc = 0, then lock the anon slot and mark it busy.
* If szc > 0, then lock the range of slots by getting the
* anon_array_lock for the first anon slot, and mark only the
* first anon slot busy to represent whole range being busy.
*/
ASSERT(RW_READ_HELD(&->a_rwlock));
an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc));
hash = ANON_ARRAY_HASH(amp, an_idx);
sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex;
sobj->sync_cv = cv = &anon_array_cv[hash];
mutex_enter(mtx);
ap_slot = anon_get_slot(amp->ahp, an_idx);
while (ANON_ISBUSY(ap_slot))
cv_wait(cv, mtx);
ANON_SETBUSY(ap_slot);
sobj->sync_data = ap_slot;
mutex_exit(mtx);
}
int
anon_array_try_enter(struct anon_map *amp, ulong_t an_idx,
anon_sync_obj_t *sobj)
{
ulong_t *ap_slot;
kmutex_t *mtx;
int hash;
/*
* Try to lock a range of anon slots.
* Use szc to determine anon slot(s) to appear atomic.
* If szc = 0, then lock the anon slot and mark it busy.
* If szc > 0, then lock the range of slots by getting the
* anon_array_lock for the first anon slot, and mark only the
* first anon slot busy to represent whole range being busy.
* Fail if the mutex or the anon_array are busy.
*/
ASSERT(RW_READ_HELD(&->a_rwlock));
an_idx = P2ALIGN(an_idx, page_get_pagecnt(amp->a_szc));
hash = ANON_ARRAY_HASH(amp, an_idx);
sobj->sync_mutex = mtx = &anon_array_lock[hash].pad_mutex;
sobj->sync_cv = &anon_array_cv[hash];
if (!mutex_tryenter(mtx)) {
return (EWOULDBLOCK);
}
ap_slot = anon_get_slot(amp->ahp, an_idx);
if (ANON_ISBUSY(ap_slot)) {
mutex_exit(mtx);
return (EWOULDBLOCK);
}
ANON_SETBUSY(ap_slot);
sobj->sync_data = ap_slot;
mutex_exit(mtx);
return (0);
}
void
anon_array_exit(anon_sync_obj_t *sobj)
{
mutex_enter(sobj->sync_mutex);
ASSERT(ANON_ISBUSY(sobj->sync_data));
ANON_CLRBUSY(sobj->sync_data);
if (CV_HAS_WAITERS(sobj->sync_cv))
cv_broadcast(sobj->sync_cv);
mutex_exit(sobj->sync_mutex);
}
|