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
|
/*
* 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) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, Joyent, Inc. All rights reserved.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
#include <sys/param.h>
#include <sys/user.h>
#include <sys/mman.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/cmn_err.h>
#include <sys/systm.h>
#include <sys/tuneable.h>
#include <vm/hat.h>
#include <vm/seg.h>
#include <vm/as.h>
#include <vm/anon.h>
#include <vm/page.h>
#include <sys/buf.h>
#include <sys/swap.h>
#include <sys/atomic.h>
#include <vm/seg_spt.h>
#include <sys/debug.h>
#include <sys/vtrace.h>
#include <sys/shm.h>
#include <sys/shm_impl.h>
#include <sys/lgrp.h>
#include <sys/vmsystm.h>
#include <sys/policy.h>
#include <sys/project.h>
#include <sys/tnf_probe.h>
#include <sys/zone.h>
#define SEGSPTADDR (caddr_t)0x0
/*
* # pages used for spt
*/
size_t spt_used;
/*
* segspt_minfree is the memory left for system after ISM
* locked its pages; it is set up to 5% of availrmem in
* sptcreate when ISM is created. ISM should not use more
* than ~90% of availrmem; if it does, then the performance
* of the system may decrease. Machines with large memories may
* be able to use up more memory for ISM so we set the default
* segspt_minfree to 5% (which gives ISM max 95% of availrmem.
* If somebody wants even more memory for ISM (risking hanging
* the system) they can patch the segspt_minfree to smaller number.
*/
pgcnt_t segspt_minfree = 0;
static int segspt_create(struct seg *seg, caddr_t argsp);
static int segspt_unmap(struct seg *seg, caddr_t raddr, size_t ssize);
static void segspt_free(struct seg *seg);
static void segspt_free_pages(struct seg *seg, caddr_t addr, size_t len);
static lgrp_mem_policy_info_t *segspt_getpolicy(struct seg *seg, caddr_t addr);
static void
segspt_badop()
{
panic("segspt_badop called");
/*NOTREACHED*/
}
#define SEGSPT_BADOP(t) (t(*)())segspt_badop
struct seg_ops segspt_ops = {
SEGSPT_BADOP(int), /* dup */
segspt_unmap,
segspt_free,
SEGSPT_BADOP(int), /* fault */
SEGSPT_BADOP(faultcode_t), /* faulta */
SEGSPT_BADOP(int), /* setprot */
SEGSPT_BADOP(int), /* checkprot */
SEGSPT_BADOP(int), /* kluster */
SEGSPT_BADOP(size_t), /* swapout */
SEGSPT_BADOP(int), /* sync */
SEGSPT_BADOP(size_t), /* incore */
SEGSPT_BADOP(int), /* lockop */
SEGSPT_BADOP(int), /* getprot */
SEGSPT_BADOP(u_offset_t), /* getoffset */
SEGSPT_BADOP(int), /* gettype */
SEGSPT_BADOP(int), /* getvp */
SEGSPT_BADOP(int), /* advise */
SEGSPT_BADOP(void), /* dump */
SEGSPT_BADOP(int), /* pagelock */
SEGSPT_BADOP(int), /* setpgsz */
SEGSPT_BADOP(int), /* getmemid */
segspt_getpolicy, /* getpolicy */
SEGSPT_BADOP(int), /* capable */
seg_inherit_notsup /* inherit */
};
static int segspt_shmdup(struct seg *seg, struct seg *newseg);
static int segspt_shmunmap(struct seg *seg, caddr_t raddr, size_t ssize);
static void segspt_shmfree(struct seg *seg);
static faultcode_t segspt_shmfault(struct hat *hat, struct seg *seg,
caddr_t addr, size_t len, enum fault_type type, enum seg_rw rw);
static faultcode_t segspt_shmfaulta(struct seg *seg, caddr_t addr);
static int segspt_shmsetprot(register struct seg *seg, register caddr_t addr,
register size_t len, register uint_t prot);
static int segspt_shmcheckprot(struct seg *seg, caddr_t addr, size_t size,
uint_t prot);
static int segspt_shmkluster(struct seg *seg, caddr_t addr, ssize_t delta);
static size_t segspt_shmswapout(struct seg *seg);
static size_t segspt_shmincore(struct seg *seg, caddr_t addr, size_t len,
register char *vec);
static int segspt_shmsync(struct seg *seg, register caddr_t addr, size_t len,
int attr, uint_t flags);
static int segspt_shmlockop(struct seg *seg, caddr_t addr, size_t len,
int attr, int op, ulong_t *lockmap, size_t pos);
static int segspt_shmgetprot(struct seg *seg, caddr_t addr, size_t len,
uint_t *protv);
static u_offset_t segspt_shmgetoffset(struct seg *seg, caddr_t addr);
static int segspt_shmgettype(struct seg *seg, caddr_t addr);
static int segspt_shmgetvp(struct seg *seg, caddr_t addr, struct vnode **vpp);
static int segspt_shmadvise(struct seg *seg, caddr_t addr, size_t len,
uint_t behav);
static void segspt_shmdump(struct seg *seg);
static int segspt_shmpagelock(struct seg *, caddr_t, size_t,
struct page ***, enum lock_type, enum seg_rw);
static int segspt_shmsetpgsz(struct seg *, caddr_t, size_t, uint_t);
static int segspt_shmgetmemid(struct seg *, caddr_t, memid_t *);
static lgrp_mem_policy_info_t *segspt_shmgetpolicy(struct seg *, caddr_t);
static int segspt_shmcapable(struct seg *, segcapability_t);
struct seg_ops segspt_shmops = {
segspt_shmdup,
segspt_shmunmap,
segspt_shmfree,
segspt_shmfault,
segspt_shmfaulta,
segspt_shmsetprot,
segspt_shmcheckprot,
segspt_shmkluster,
segspt_shmswapout,
segspt_shmsync,
segspt_shmincore,
segspt_shmlockop,
segspt_shmgetprot,
segspt_shmgetoffset,
segspt_shmgettype,
segspt_shmgetvp,
segspt_shmadvise, /* advise */
segspt_shmdump,
segspt_shmpagelock,
segspt_shmsetpgsz,
segspt_shmgetmemid,
segspt_shmgetpolicy,
segspt_shmcapable,
seg_inherit_notsup
};
static void segspt_purge(struct seg *seg);
static int segspt_reclaim(void *, caddr_t, size_t, struct page **,
enum seg_rw, int);
static int spt_anon_getpages(struct seg *seg, caddr_t addr, size_t len,
page_t **ppa);
/*ARGSUSED*/
int
sptcreate(size_t size, struct seg **sptseg, struct anon_map *amp,
uint_t prot, uint_t flags, uint_t share_szc)
{
int err;
struct as *newas;
struct segspt_crargs sptcargs;
#ifdef DEBUG
TNF_PROBE_1(sptcreate, "spt", /* CSTYLED */,
tnf_ulong, size, size );
#endif
if (segspt_minfree == 0) /* leave min 5% of availrmem for */
segspt_minfree = availrmem/20; /* for the system */
if (!hat_supported(HAT_SHARED_PT, (void *)0))
return (EINVAL);
/*
* get a new as for this shared memory segment
*/
newas = as_alloc();
newas->a_proc = NULL;
sptcargs.amp = amp;
sptcargs.prot = prot;
sptcargs.flags = flags;
sptcargs.szc = share_szc;
/*
* create a shared page table (spt) segment
*/
if (err = as_map(newas, SEGSPTADDR, size, segspt_create, &sptcargs)) {
as_free(newas);
return (err);
}
*sptseg = sptcargs.seg_spt;
return (0);
}
void
sptdestroy(struct as *as, struct anon_map *amp)
{
#ifdef DEBUG
TNF_PROBE_0(sptdestroy, "spt", /* CSTYLED */);
#endif
(void) as_unmap(as, SEGSPTADDR, amp->size);
as_free(as);
}
/*
* called from seg_free().
* free (i.e., unlock, unmap, return to free list)
* all the pages in the given seg.
*/
void
segspt_free(struct seg *seg)
{
struct spt_data *sptd = (struct spt_data *)seg->s_data;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
if (sptd != NULL) {
if (sptd->spt_realsize)
segspt_free_pages(seg, seg->s_base, sptd->spt_realsize);
if (sptd->spt_ppa_lckcnt) {
kmem_free(sptd->spt_ppa_lckcnt,
sizeof (*sptd->spt_ppa_lckcnt)
* btopr(sptd->spt_amp->size));
}
kmem_free(sptd->spt_vp, sizeof (*sptd->spt_vp));
cv_destroy(&sptd->spt_cv);
mutex_destroy(&sptd->spt_lock);
kmem_free(sptd, sizeof (*sptd));
}
}
/*ARGSUSED*/
static int
segspt_shmsync(struct seg *seg, caddr_t addr, size_t len, int attr,
uint_t flags)
{
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
return (0);
}
/*ARGSUSED*/
static size_t
segspt_shmincore(struct seg *seg, caddr_t addr, size_t len, char *vec)
{
caddr_t eo_seg;
pgcnt_t npages;
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg;
struct spt_data *sptd;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
#ifdef lint
seg = seg;
#endif
sptseg = shmd->shm_sptseg;
sptd = sptseg->s_data;
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
eo_seg = addr + len;
while (addr < eo_seg) {
/* page exists, and it's locked. */
*vec++ = SEG_PAGE_INCORE | SEG_PAGE_LOCKED |
SEG_PAGE_ANON;
addr += PAGESIZE;
}
return (len);
} else {
struct anon_map *amp = shmd->shm_amp;
struct anon *ap;
page_t *pp;
pgcnt_t anon_index;
struct vnode *vp;
u_offset_t off;
ulong_t i;
int ret;
anon_sync_obj_t cookie;
addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
anon_index = seg_page(seg, addr);
npages = btopr(len);
if (anon_index + npages > btopr(shmd->shm_amp->size)) {
return (EINVAL);
}
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
for (i = 0; i < npages; i++, anon_index++) {
ret = 0;
anon_array_enter(amp, anon_index, &cookie);
ap = anon_get_ptr(amp->ahp, anon_index);
if (ap != NULL) {
swap_xlate(ap, &vp, &off);
anon_array_exit(&cookie);
pp = page_lookup_nowait(vp, off, SE_SHARED);
if (pp != NULL) {
ret |= SEG_PAGE_INCORE | SEG_PAGE_ANON;
page_unlock(pp);
}
} else {
anon_array_exit(&cookie);
}
if (shmd->shm_vpage[anon_index] & DISM_PG_LOCKED) {
ret |= SEG_PAGE_LOCKED;
}
*vec++ = (char)ret;
}
ANON_LOCK_EXIT(&->a_rwlock);
return (len);
}
}
static int
segspt_unmap(struct seg *seg, caddr_t raddr, size_t ssize)
{
size_t share_size;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
/*
* seg.s_size may have been rounded up to the largest page size
* in shmat().
* XXX This should be cleanedup. sptdestroy should take a length
* argument which should be the same as sptcreate. Then
* this rounding would not be needed (or is done in shm.c)
* Only the check for full segment will be needed.
*
* XXX -- shouldn't raddr == 0 always? These tests don't seem
* to be useful at all.
*/
share_size = page_get_pagesize(seg->s_szc);
ssize = P2ROUNDUP(ssize, share_size);
if (raddr == seg->s_base && ssize == seg->s_size) {
seg_free(seg);
return (0);
} else
return (EINVAL);
}
int
segspt_create(struct seg *seg, caddr_t argsp)
{
int err;
caddr_t addr = seg->s_base;
struct spt_data *sptd;
struct segspt_crargs *sptcargs = (struct segspt_crargs *)argsp;
struct anon_map *amp = sptcargs->amp;
struct kshmid *sp = amp->a_sp;
struct cred *cred = CRED();
ulong_t i, j, anon_index = 0;
pgcnt_t npages = btopr(amp->size);
struct vnode *vp;
page_t **ppa;
uint_t hat_flags;
size_t pgsz;
pgcnt_t pgcnt;
caddr_t a;
pgcnt_t pidx;
size_t sz;
proc_t *procp = curproc;
rctl_qty_t lockedbytes = 0;
kproject_t *proj;
/*
* We are holding the a_lock on the underlying dummy as,
* so we can make calls to the HAT layer.
*/
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
ASSERT(sp != NULL);
#ifdef DEBUG
TNF_PROBE_2(segspt_create, "spt", /* CSTYLED */,
tnf_opaque, addr, addr, tnf_ulong, len, seg->s_size);
#endif
if ((sptcargs->flags & SHM_PAGEABLE) == 0) {
if (err = anon_swap_adjust(npages))
return (err);
}
err = ENOMEM;
if ((sptd = kmem_zalloc(sizeof (*sptd), KM_NOSLEEP)) == NULL)
goto out1;
if ((sptcargs->flags & SHM_PAGEABLE) == 0) {
if ((ppa = kmem_zalloc(((sizeof (page_t *)) * npages),
KM_NOSLEEP)) == NULL)
goto out2;
}
mutex_init(&sptd->spt_lock, NULL, MUTEX_DEFAULT, NULL);
if ((vp = kmem_zalloc(sizeof (*vp), KM_NOSLEEP)) == NULL)
goto out3;
seg->s_ops = &segspt_ops;
sptd->spt_vp = vp;
sptd->spt_amp = amp;
sptd->spt_prot = sptcargs->prot;
sptd->spt_flags = sptcargs->flags;
seg->s_data = (caddr_t)sptd;
sptd->spt_ppa = NULL;
sptd->spt_ppa_lckcnt = NULL;
seg->s_szc = sptcargs->szc;
cv_init(&sptd->spt_cv, NULL, CV_DEFAULT, NULL);
sptd->spt_gen = 0;
ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
if (seg->s_szc > amp->a_szc) {
amp->a_szc = seg->s_szc;
}
ANON_LOCK_EXIT(&->a_rwlock);
/*
* Set policy to affect initial allocation of pages in
* anon_map_createpages()
*/
(void) lgrp_shm_policy_set(LGRP_MEM_POLICY_DEFAULT, amp, anon_index,
NULL, 0, ptob(npages));
if (sptcargs->flags & SHM_PAGEABLE) {
size_t share_sz;
pgcnt_t new_npgs, more_pgs;
struct anon_hdr *nahp;
zone_t *zone;
share_sz = page_get_pagesize(seg->s_szc);
if (!IS_P2ALIGNED(amp->size, share_sz)) {
/*
* We are rounding up the size of the anon array
* on 4 M boundary because we always create 4 M
* of page(s) when locking, faulting pages and we
* don't have to check for all corner cases e.g.
* if there is enough space to allocate 4 M
* page.
*/
new_npgs = btop(P2ROUNDUP(amp->size, share_sz));
more_pgs = new_npgs - npages;
/*
* The zone will never be NULL, as a fully created
* shm always has an owning zone.
*/
zone = sp->shm_perm.ipc_zone_ref.zref_zone;
ASSERT(zone != NULL);
if (anon_resv_zone(ptob(more_pgs), zone) == 0) {
err = ENOMEM;
goto out4;
}
nahp = anon_create(new_npgs, ANON_SLEEP);
ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
(void) anon_copy_ptr(amp->ahp, 0, nahp, 0, npages,
ANON_SLEEP);
anon_release(amp->ahp, npages);
amp->ahp = nahp;
ASSERT(amp->swresv == ptob(npages));
amp->swresv = amp->size = ptob(new_npgs);
ANON_LOCK_EXIT(&->a_rwlock);
npages = new_npgs;
}
sptd->spt_ppa_lckcnt = kmem_zalloc(npages *
sizeof (*sptd->spt_ppa_lckcnt), KM_SLEEP);
sptd->spt_pcachecnt = 0;
sptd->spt_realsize = ptob(npages);
sptcargs->seg_spt = seg;
return (0);
}
/*
* get array of pages for each anon slot in amp
*/
if ((err = anon_map_createpages(amp, anon_index, ptob(npages), ppa,
seg, addr, S_CREATE, cred)) != 0)
goto out4;
mutex_enter(&sp->shm_mlock);
/* May be partially locked, so, count bytes to charge for locking */
for (i = 0; i < npages; i++)
if (ppa[i]->p_lckcnt == 0)
lockedbytes += PAGESIZE;
proj = sp->shm_perm.ipc_proj;
if (lockedbytes > 0) {
mutex_enter(&procp->p_lock);
if (rctl_incr_locked_mem(procp, proj, lockedbytes, 0)) {
mutex_exit(&procp->p_lock);
mutex_exit(&sp->shm_mlock);
for (i = 0; i < npages; i++)
page_unlock(ppa[i]);
err = ENOMEM;
goto out4;
}
mutex_exit(&procp->p_lock);
}
/*
* addr is initial address corresponding to the first page on ppa list
*/
for (i = 0; i < npages; i++) {
/* attempt to lock all pages */
if (page_pp_lock(ppa[i], 0, 1) == 0) {
/*
* if unable to lock any page, unlock all
* of them and return error
*/
for (j = 0; j < i; j++)
page_pp_unlock(ppa[j], 0, 1);
for (i = 0; i < npages; i++)
page_unlock(ppa[i]);
rctl_decr_locked_mem(NULL, proj, lockedbytes, 0);
mutex_exit(&sp->shm_mlock);
err = ENOMEM;
goto out4;
}
}
mutex_exit(&sp->shm_mlock);
/*
* Some platforms assume that ISM mappings are HAT_LOAD_LOCK
* for the entire life of the segment. For example platforms
* that do not support Dynamic Reconfiguration.
*/
hat_flags = HAT_LOAD_SHARE;
if (!hat_supported(HAT_DYNAMIC_ISM_UNMAP, NULL))
hat_flags |= HAT_LOAD_LOCK;
/*
* Load translations one lare page at a time
* to make sure we don't create mappings bigger than
* segment's size code in case underlying pages
* are shared with segvn's segment that uses bigger
* size code than we do.
*/
pgsz = page_get_pagesize(seg->s_szc);
pgcnt = page_get_pagecnt(seg->s_szc);
for (a = addr, pidx = 0; pidx < npages; a += pgsz, pidx += pgcnt) {
sz = MIN(pgsz, ptob(npages - pidx));
hat_memload_array(seg->s_as->a_hat, a, sz,
&ppa[pidx], sptd->spt_prot, hat_flags);
}
/*
* On platforms that do not support HAT_DYNAMIC_ISM_UNMAP,
* we will leave the pages locked SE_SHARED for the life
* of the ISM segment. This will prevent any calls to
* hat_pageunload() on this ISM segment for those platforms.
*/
if (!(hat_flags & HAT_LOAD_LOCK)) {
/*
* On platforms that support HAT_DYNAMIC_ISM_UNMAP,
* we no longer need to hold the SE_SHARED lock on the pages,
* since L_PAGELOCK and F_SOFTLOCK calls will grab the
* SE_SHARED lock on the pages as necessary.
*/
for (i = 0; i < npages; i++)
page_unlock(ppa[i]);
}
sptd->spt_pcachecnt = 0;
kmem_free(ppa, ((sizeof (page_t *)) * npages));
sptd->spt_realsize = ptob(npages);
atomic_add_long(&spt_used, npages);
sptcargs->seg_spt = seg;
return (0);
out4:
seg->s_data = NULL;
kmem_free(vp, sizeof (*vp));
cv_destroy(&sptd->spt_cv);
out3:
mutex_destroy(&sptd->spt_lock);
if ((sptcargs->flags & SHM_PAGEABLE) == 0)
kmem_free(ppa, (sizeof (*ppa) * npages));
out2:
kmem_free(sptd, sizeof (*sptd));
out1:
if ((sptcargs->flags & SHM_PAGEABLE) == 0)
anon_swap_restore(npages);
return (err);
}
/*ARGSUSED*/
void
segspt_free_pages(struct seg *seg, caddr_t addr, size_t len)
{
struct page *pp;
struct spt_data *sptd = (struct spt_data *)seg->s_data;
pgcnt_t npages;
ulong_t anon_idx;
struct anon_map *amp;
struct anon *ap;
struct vnode *vp;
u_offset_t off;
uint_t hat_flags;
int root = 0;
pgcnt_t pgs, curnpgs = 0;
page_t *rootpp;
rctl_qty_t unlocked_bytes = 0;
kproject_t *proj;
kshmid_t *sp;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
len = P2ROUNDUP(len, PAGESIZE);
npages = btop(len);
hat_flags = HAT_UNLOAD_UNLOCK | HAT_UNLOAD_UNMAP;
if ((hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) ||
(sptd->spt_flags & SHM_PAGEABLE)) {
hat_flags = HAT_UNLOAD_UNMAP;
}
hat_unload(seg->s_as->a_hat, addr, len, hat_flags);
amp = sptd->spt_amp;
if (sptd->spt_flags & SHM_PAGEABLE)
npages = btop(amp->size);
ASSERT(amp != NULL);
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
sp = amp->a_sp;
proj = sp->shm_perm.ipc_proj;
mutex_enter(&sp->shm_mlock);
}
for (anon_idx = 0; anon_idx < npages; anon_idx++) {
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) {
panic("segspt_free_pages: null app");
/*NOTREACHED*/
}
} else {
if ((ap = anon_get_next_ptr(amp->ahp, &anon_idx))
== NULL)
continue;
}
ASSERT(ANON_ISBUSY(anon_get_slot(amp->ahp, anon_idx)) == 0);
swap_xlate(ap, &vp, &off);
/*
* If this platform supports HAT_DYNAMIC_ISM_UNMAP,
* the pages won't be having SE_SHARED lock at this
* point.
*
* On platforms that do not support HAT_DYNAMIC_ISM_UNMAP,
* the pages are still held SE_SHARED locked from the
* original segspt_create()
*
* Our goal is to get SE_EXCL lock on each page, remove
* permanent lock on it and invalidate the page.
*/
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
if (hat_flags == HAT_UNLOAD_UNMAP)
pp = page_lookup(vp, off, SE_EXCL);
else {
if ((pp = page_find(vp, off)) == NULL) {
panic("segspt_free_pages: "
"page not locked");
/*NOTREACHED*/
}
if (!page_tryupgrade(pp)) {
page_unlock(pp);
pp = page_lookup(vp, off, SE_EXCL);
}
}
if (pp == NULL) {
panic("segspt_free_pages: "
"page not in the system");
/*NOTREACHED*/
}
ASSERT(pp->p_lckcnt > 0);
page_pp_unlock(pp, 0, 1);
if (pp->p_lckcnt == 0)
unlocked_bytes += PAGESIZE;
} else {
if ((pp = page_lookup(vp, off, SE_EXCL)) == NULL)
continue;
}
/*
* It's logical to invalidate the pages here as in most cases
* these were created by segspt.
*/
if (pp->p_szc != 0) {
if (root == 0) {
ASSERT(curnpgs == 0);
root = 1;
rootpp = pp;
pgs = curnpgs = page_get_pagecnt(pp->p_szc);
ASSERT(pgs > 1);
ASSERT(IS_P2ALIGNED(pgs, pgs));
ASSERT(!(page_pptonum(pp) & (pgs - 1)));
curnpgs--;
} else if ((page_pptonum(pp) & (pgs - 1)) == pgs - 1) {
ASSERT(curnpgs == 1);
ASSERT(page_pptonum(pp) ==
page_pptonum(rootpp) + (pgs - 1));
page_destroy_pages(rootpp);
root = 0;
curnpgs = 0;
} else {
ASSERT(curnpgs > 1);
ASSERT(page_pptonum(pp) ==
page_pptonum(rootpp) + (pgs - curnpgs));
curnpgs--;
}
} else {
if (root != 0 || curnpgs != 0) {
panic("segspt_free_pages: bad large page");
/*NOTREACHED*/
}
/*
* Before destroying the pages, we need to take care
* of the rctl locked memory accounting. For that
* we need to calculte the unlocked_bytes.
*/
if (pp->p_lckcnt > 0)
unlocked_bytes += PAGESIZE;
/*LINTED: constant in conditional context */
VN_DISPOSE(pp, B_INVAL, 0, kcred);
}
}
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
if (unlocked_bytes > 0)
rctl_decr_locked_mem(NULL, proj, unlocked_bytes, 0);
mutex_exit(&sp->shm_mlock);
}
if (root != 0 || curnpgs != 0) {
panic("segspt_free_pages: bad large page");
/*NOTREACHED*/
}
/*
* mark that pages have been released
*/
sptd->spt_realsize = 0;
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
atomic_add_long(&spt_used, -npages);
anon_swap_restore(npages);
}
}
/*
* Get memory allocation policy info for specified address in given segment
*/
static lgrp_mem_policy_info_t *
segspt_getpolicy(struct seg *seg, caddr_t addr)
{
struct anon_map *amp;
ulong_t anon_index;
lgrp_mem_policy_info_t *policy_info;
struct spt_data *spt_data;
ASSERT(seg != NULL);
/*
* Get anon_map from segspt
*
* Assume that no lock needs to be held on anon_map, since
* it should be protected by its reference count which must be
* nonzero for an existing segment
* Need to grab readers lock on policy tree though
*/
spt_data = (struct spt_data *)seg->s_data;
if (spt_data == NULL)
return (NULL);
amp = spt_data->spt_amp;
ASSERT(amp->refcnt != 0);
/*
* Get policy info
*
* Assume starting anon index of 0
*/
anon_index = seg_page(seg, addr);
policy_info = lgrp_shm_policy_get(amp, anon_index, NULL, 0);
return (policy_info);
}
/*
* DISM only.
* Return locked pages over a given range.
*
* We will cache all DISM locked pages and save the pplist for the
* entire segment in the ppa field of the underlying DISM segment structure.
* Later, during a call to segspt_reclaim() we will use this ppa array
* to page_unlock() all of the pages and then we will free this ppa list.
*/
/*ARGSUSED*/
static int
segspt_dismpagelock(struct seg *seg, caddr_t addr, size_t len,
struct page ***ppp, enum lock_type type, enum seg_rw rw)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg = shmd->shm_sptseg;
struct spt_data *sptd = sptseg->s_data;
pgcnt_t pg_idx, npages, tot_npages, npgs;
struct page **pplist, **pl, **ppa, *pp;
struct anon_map *amp;
spgcnt_t an_idx;
int ret = ENOTSUP;
uint_t pl_built = 0;
struct anon *ap;
struct vnode *vp;
u_offset_t off;
pgcnt_t claim_availrmem = 0;
uint_t szc;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
ASSERT(type == L_PAGELOCK || type == L_PAGEUNLOCK);
/*
* We want to lock/unlock the entire ISM segment. Therefore,
* we will be using the underlying sptseg and it's base address
* and length for the caching arguments.
*/
ASSERT(sptseg);
ASSERT(sptd);
pg_idx = seg_page(seg, addr);
npages = btopr(len);
/*
* check if the request is larger than number of pages covered
* by amp
*/
if (pg_idx + npages > btopr(sptd->spt_amp->size)) {
*ppp = NULL;
return (ENOTSUP);
}
if (type == L_PAGEUNLOCK) {
ASSERT(sptd->spt_ppa != NULL);
seg_pinactive(seg, NULL, seg->s_base, sptd->spt_amp->size,
sptd->spt_ppa, S_WRITE, SEGP_FORCE_WIRED, segspt_reclaim);
/*
* If someone is blocked while unmapping, we purge
* segment page cache and thus reclaim pplist synchronously
* without waiting for seg_pasync_thread. This speeds up
* unmapping in cases where munmap(2) is called, while
* raw async i/o is still in progress or where a thread
* exits on data fault in a multithreaded application.
*/
if ((sptd->spt_flags & DISM_PPA_CHANGED) ||
(AS_ISUNMAPWAIT(seg->s_as) &&
shmd->shm_softlockcnt > 0)) {
segspt_purge(seg);
}
return (0);
}
/* The L_PAGELOCK case ... */
if (sptd->spt_flags & DISM_PPA_CHANGED) {
segspt_purge(seg);
/*
* for DISM ppa needs to be rebuild since
* number of locked pages could be changed
*/
*ppp = NULL;
return (ENOTSUP);
}
/*
* First try to find pages in segment page cache, without
* holding the segment lock.
*/
pplist = seg_plookup(seg, NULL, seg->s_base, sptd->spt_amp->size,
S_WRITE, SEGP_FORCE_WIRED);
if (pplist != NULL) {
ASSERT(sptd->spt_ppa != NULL);
ASSERT(sptd->spt_ppa == pplist);
ppa = sptd->spt_ppa;
for (an_idx = pg_idx; an_idx < pg_idx + npages; ) {
if (ppa[an_idx] == NULL) {
seg_pinactive(seg, NULL, seg->s_base,
sptd->spt_amp->size, ppa,
S_WRITE, SEGP_FORCE_WIRED, segspt_reclaim);
*ppp = NULL;
return (ENOTSUP);
}
if ((szc = ppa[an_idx]->p_szc) != 0) {
npgs = page_get_pagecnt(szc);
an_idx = P2ROUNDUP(an_idx + 1, npgs);
} else {
an_idx++;
}
}
/*
* Since we cache the entire DISM segment, we want to
* set ppp to point to the first slot that corresponds
* to the requested addr, i.e. pg_idx.
*/
*ppp = &(sptd->spt_ppa[pg_idx]);
return (0);
}
mutex_enter(&sptd->spt_lock);
/*
* try to find pages in segment page cache with mutex
*/
pplist = seg_plookup(seg, NULL, seg->s_base, sptd->spt_amp->size,
S_WRITE, SEGP_FORCE_WIRED);
if (pplist != NULL) {
ASSERT(sptd->spt_ppa != NULL);
ASSERT(sptd->spt_ppa == pplist);
ppa = sptd->spt_ppa;
for (an_idx = pg_idx; an_idx < pg_idx + npages; ) {
if (ppa[an_idx] == NULL) {
mutex_exit(&sptd->spt_lock);
seg_pinactive(seg, NULL, seg->s_base,
sptd->spt_amp->size, ppa,
S_WRITE, SEGP_FORCE_WIRED, segspt_reclaim);
*ppp = NULL;
return (ENOTSUP);
}
if ((szc = ppa[an_idx]->p_szc) != 0) {
npgs = page_get_pagecnt(szc);
an_idx = P2ROUNDUP(an_idx + 1, npgs);
} else {
an_idx++;
}
}
/*
* Since we cache the entire DISM segment, we want to
* set ppp to point to the first slot that corresponds
* to the requested addr, i.e. pg_idx.
*/
mutex_exit(&sptd->spt_lock);
*ppp = &(sptd->spt_ppa[pg_idx]);
return (0);
}
if (seg_pinsert_check(seg, NULL, seg->s_base, sptd->spt_amp->size,
SEGP_FORCE_WIRED) == SEGP_FAIL) {
mutex_exit(&sptd->spt_lock);
*ppp = NULL;
return (ENOTSUP);
}
/*
* No need to worry about protections because DISM pages are always rw.
*/
pl = pplist = NULL;
amp = sptd->spt_amp;
/*
* Do we need to build the ppa array?
*/
if (sptd->spt_ppa == NULL) {
pgcnt_t lpg_cnt = 0;
pl_built = 1;
tot_npages = btopr(sptd->spt_amp->size);
ASSERT(sptd->spt_pcachecnt == 0);
pplist = kmem_zalloc(sizeof (page_t *) * tot_npages, KM_SLEEP);
pl = pplist;
ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
for (an_idx = 0; an_idx < tot_npages; ) {
ap = anon_get_ptr(amp->ahp, an_idx);
/*
* Cache only mlocked pages. For large pages
* if one (constituent) page is mlocked
* all pages for that large page
* are cached also. This is for quick
* lookups of ppa array;
*/
if ((ap != NULL) && (lpg_cnt != 0 ||
(sptd->spt_ppa_lckcnt[an_idx] != 0))) {
swap_xlate(ap, &vp, &off);
pp = page_lookup(vp, off, SE_SHARED);
ASSERT(pp != NULL);
if (lpg_cnt == 0) {
lpg_cnt++;
/*
* For a small page, we are done --
* lpg_count is reset to 0 below.
*
* For a large page, we are guaranteed
* to find the anon structures of all
* constituent pages and a non-zero
* lpg_cnt ensures that we don't test
* for mlock for these. We are done
* when lpg_count reaches (npgs + 1).
* If we are not the first constituent
* page, restart at the first one.
*/
npgs = page_get_pagecnt(pp->p_szc);
if (!IS_P2ALIGNED(an_idx, npgs)) {
an_idx = P2ALIGN(an_idx, npgs);
page_unlock(pp);
continue;
}
}
if (++lpg_cnt > npgs)
lpg_cnt = 0;
/*
* availrmem is decremented only
* for unlocked pages
*/
if (sptd->spt_ppa_lckcnt[an_idx] == 0)
claim_availrmem++;
pplist[an_idx] = pp;
}
an_idx++;
}
ANON_LOCK_EXIT(&->a_rwlock);
if (claim_availrmem) {
mutex_enter(&freemem_lock);
if (availrmem < tune.t_minarmem + claim_availrmem) {
mutex_exit(&freemem_lock);
ret = ENOTSUP;
claim_availrmem = 0;
goto insert_fail;
} else {
availrmem -= claim_availrmem;
}
mutex_exit(&freemem_lock);
}
sptd->spt_ppa = pl;
} else {
/*
* We already have a valid ppa[].
*/
pl = sptd->spt_ppa;
}
ASSERT(pl != NULL);
ret = seg_pinsert(seg, NULL, seg->s_base, sptd->spt_amp->size,
sptd->spt_amp->size, pl, S_WRITE, SEGP_FORCE_WIRED,
segspt_reclaim);
if (ret == SEGP_FAIL) {
/*
* seg_pinsert failed. We return
* ENOTSUP, so that the as_pagelock() code will
* then try the slower F_SOFTLOCK path.
*/
if (pl_built) {
/*
* No one else has referenced the ppa[].
* We created it and we need to destroy it.
*/
sptd->spt_ppa = NULL;
}
ret = ENOTSUP;
goto insert_fail;
}
/*
* In either case, we increment softlockcnt on the 'real' segment.
*/
sptd->spt_pcachecnt++;
atomic_inc_ulong((ulong_t *)(&(shmd->shm_softlockcnt)));
ppa = sptd->spt_ppa;
for (an_idx = pg_idx; an_idx < pg_idx + npages; ) {
if (ppa[an_idx] == NULL) {
mutex_exit(&sptd->spt_lock);
seg_pinactive(seg, NULL, seg->s_base,
sptd->spt_amp->size,
pl, S_WRITE, SEGP_FORCE_WIRED, segspt_reclaim);
*ppp = NULL;
return (ENOTSUP);
}
if ((szc = ppa[an_idx]->p_szc) != 0) {
npgs = page_get_pagecnt(szc);
an_idx = P2ROUNDUP(an_idx + 1, npgs);
} else {
an_idx++;
}
}
/*
* We can now drop the sptd->spt_lock since the ppa[]
* exists and we have incremented pacachecnt.
*/
mutex_exit(&sptd->spt_lock);
/*
* Since we cache the entire segment, we want to
* set ppp to point to the first slot that corresponds
* to the requested addr, i.e. pg_idx.
*/
*ppp = &(sptd->spt_ppa[pg_idx]);
return (0);
insert_fail:
/*
* We will only reach this code if we tried and failed.
*
* And we can drop the lock on the dummy seg, once we've failed
* to set up a new ppa[].
*/
mutex_exit(&sptd->spt_lock);
if (pl_built) {
if (claim_availrmem) {
mutex_enter(&freemem_lock);
availrmem += claim_availrmem;
mutex_exit(&freemem_lock);
}
/*
* We created pl and we need to destroy it.
*/
pplist = pl;
for (an_idx = 0; an_idx < tot_npages; an_idx++) {
if (pplist[an_idx] != NULL)
page_unlock(pplist[an_idx]);
}
kmem_free(pl, sizeof (page_t *) * tot_npages);
}
if (shmd->shm_softlockcnt <= 0) {
if (AS_ISUNMAPWAIT(seg->s_as)) {
mutex_enter(&seg->s_as->a_contents);
if (AS_ISUNMAPWAIT(seg->s_as)) {
AS_CLRUNMAPWAIT(seg->s_as);
cv_broadcast(&seg->s_as->a_cv);
}
mutex_exit(&seg->s_as->a_contents);
}
}
*ppp = NULL;
return (ret);
}
/*
* return locked pages over a given range.
*
* We will cache the entire ISM segment and save the pplist for the
* entire segment in the ppa field of the underlying ISM segment structure.
* Later, during a call to segspt_reclaim() we will use this ppa array
* to page_unlock() all of the pages and then we will free this ppa list.
*/
/*ARGSUSED*/
static int
segspt_shmpagelock(struct seg *seg, caddr_t addr, size_t len,
struct page ***ppp, enum lock_type type, enum seg_rw rw)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg = shmd->shm_sptseg;
struct spt_data *sptd = sptseg->s_data;
pgcnt_t np, page_index, npages;
caddr_t a, spt_base;
struct page **pplist, **pl, *pp;
struct anon_map *amp;
ulong_t anon_index;
int ret = ENOTSUP;
uint_t pl_built = 0;
struct anon *ap;
struct vnode *vp;
u_offset_t off;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
ASSERT(type == L_PAGELOCK || type == L_PAGEUNLOCK);
/*
* We want to lock/unlock the entire ISM segment. Therefore,
* we will be using the underlying sptseg and it's base address
* and length for the caching arguments.
*/
ASSERT(sptseg);
ASSERT(sptd);
if (sptd->spt_flags & SHM_PAGEABLE) {
return (segspt_dismpagelock(seg, addr, len, ppp, type, rw));
}
page_index = seg_page(seg, addr);
npages = btopr(len);
/*
* check if the request is larger than number of pages covered
* by amp
*/
if (page_index + npages > btopr(sptd->spt_amp->size)) {
*ppp = NULL;
return (ENOTSUP);
}
if (type == L_PAGEUNLOCK) {
ASSERT(sptd->spt_ppa != NULL);
seg_pinactive(seg, NULL, seg->s_base, sptd->spt_amp->size,
sptd->spt_ppa, S_WRITE, SEGP_FORCE_WIRED, segspt_reclaim);
/*
* If someone is blocked while unmapping, we purge
* segment page cache and thus reclaim pplist synchronously
* without waiting for seg_pasync_thread. This speeds up
* unmapping in cases where munmap(2) is called, while
* raw async i/o is still in progress or where a thread
* exits on data fault in a multithreaded application.
*/
if (AS_ISUNMAPWAIT(seg->s_as) && (shmd->shm_softlockcnt > 0)) {
segspt_purge(seg);
}
return (0);
}
/* The L_PAGELOCK case... */
/*
* First try to find pages in segment page cache, without
* holding the segment lock.
*/
pplist = seg_plookup(seg, NULL, seg->s_base, sptd->spt_amp->size,
S_WRITE, SEGP_FORCE_WIRED);
if (pplist != NULL) {
ASSERT(sptd->spt_ppa == pplist);
ASSERT(sptd->spt_ppa[page_index]);
/*
* Since we cache the entire ISM segment, we want to
* set ppp to point to the first slot that corresponds
* to the requested addr, i.e. page_index.
*/
*ppp = &(sptd->spt_ppa[page_index]);
return (0);
}
mutex_enter(&sptd->spt_lock);
/*
* try to find pages in segment page cache
*/
pplist = seg_plookup(seg, NULL, seg->s_base, sptd->spt_amp->size,
S_WRITE, SEGP_FORCE_WIRED);
if (pplist != NULL) {
ASSERT(sptd->spt_ppa == pplist);
/*
* Since we cache the entire segment, we want to
* set ppp to point to the first slot that corresponds
* to the requested addr, i.e. page_index.
*/
mutex_exit(&sptd->spt_lock);
*ppp = &(sptd->spt_ppa[page_index]);
return (0);
}
if (seg_pinsert_check(seg, NULL, seg->s_base, sptd->spt_amp->size,
SEGP_FORCE_WIRED) == SEGP_FAIL) {
mutex_exit(&sptd->spt_lock);
*ppp = NULL;
return (ENOTSUP);
}
/*
* No need to worry about protections because ISM pages
* are always rw.
*/
pl = pplist = NULL;
/*
* Do we need to build the ppa array?
*/
if (sptd->spt_ppa == NULL) {
ASSERT(sptd->spt_ppa == pplist);
spt_base = sptseg->s_base;
pl_built = 1;
/*
* availrmem is decremented once during anon_swap_adjust()
* and is incremented during the anon_unresv(), which is
* called from shm_rm_amp() when the segment is destroyed.
*/
amp = sptd->spt_amp;
ASSERT(amp != NULL);
/* pcachecnt is protected by sptd->spt_lock */
ASSERT(sptd->spt_pcachecnt == 0);
pplist = kmem_zalloc(sizeof (page_t *)
* btopr(sptd->spt_amp->size), KM_SLEEP);
pl = pplist;
anon_index = seg_page(sptseg, spt_base);
ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
for (a = spt_base; a < (spt_base + sptd->spt_amp->size);
a += PAGESIZE, anon_index++, pplist++) {
ap = anon_get_ptr(amp->ahp, anon_index);
ASSERT(ap != NULL);
swap_xlate(ap, &vp, &off);
pp = page_lookup(vp, off, SE_SHARED);
ASSERT(pp != NULL);
*pplist = pp;
}
ANON_LOCK_EXIT(&->a_rwlock);
if (a < (spt_base + sptd->spt_amp->size)) {
ret = ENOTSUP;
goto insert_fail;
}
sptd->spt_ppa = pl;
} else {
/*
* We already have a valid ppa[].
*/
pl = sptd->spt_ppa;
}
ASSERT(pl != NULL);
ret = seg_pinsert(seg, NULL, seg->s_base, sptd->spt_amp->size,
sptd->spt_amp->size, pl, S_WRITE, SEGP_FORCE_WIRED,
segspt_reclaim);
if (ret == SEGP_FAIL) {
/*
* seg_pinsert failed. We return
* ENOTSUP, so that the as_pagelock() code will
* then try the slower F_SOFTLOCK path.
*/
if (pl_built) {
/*
* No one else has referenced the ppa[].
* We created it and we need to destroy it.
*/
sptd->spt_ppa = NULL;
}
ret = ENOTSUP;
goto insert_fail;
}
/*
* In either case, we increment softlockcnt on the 'real' segment.
*/
sptd->spt_pcachecnt++;
atomic_inc_ulong((ulong_t *)(&(shmd->shm_softlockcnt)));
/*
* We can now drop the sptd->spt_lock since the ppa[]
* exists and we have incremented pacachecnt.
*/
mutex_exit(&sptd->spt_lock);
/*
* Since we cache the entire segment, we want to
* set ppp to point to the first slot that corresponds
* to the requested addr, i.e. page_index.
*/
*ppp = &(sptd->spt_ppa[page_index]);
return (0);
insert_fail:
/*
* We will only reach this code if we tried and failed.
*
* And we can drop the lock on the dummy seg, once we've failed
* to set up a new ppa[].
*/
mutex_exit(&sptd->spt_lock);
if (pl_built) {
/*
* We created pl and we need to destroy it.
*/
pplist = pl;
np = (((uintptr_t)(a - spt_base)) >> PAGESHIFT);
while (np) {
page_unlock(*pplist);
np--;
pplist++;
}
kmem_free(pl, sizeof (page_t *) * btopr(sptd->spt_amp->size));
}
if (shmd->shm_softlockcnt <= 0) {
if (AS_ISUNMAPWAIT(seg->s_as)) {
mutex_enter(&seg->s_as->a_contents);
if (AS_ISUNMAPWAIT(seg->s_as)) {
AS_CLRUNMAPWAIT(seg->s_as);
cv_broadcast(&seg->s_as->a_cv);
}
mutex_exit(&seg->s_as->a_contents);
}
}
*ppp = NULL;
return (ret);
}
/*
* purge any cached pages in the I/O page cache
*/
static void
segspt_purge(struct seg *seg)
{
seg_ppurge(seg, NULL, SEGP_FORCE_WIRED);
}
static int
segspt_reclaim(void *ptag, caddr_t addr, size_t len, struct page **pplist,
enum seg_rw rw, int async)
{
struct seg *seg = (struct seg *)ptag;
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg;
struct spt_data *sptd;
pgcnt_t npages, i, free_availrmem = 0;
int done = 0;
#ifdef lint
addr = addr;
#endif
sptseg = shmd->shm_sptseg;
sptd = sptseg->s_data;
npages = (len >> PAGESHIFT);
ASSERT(npages);
ASSERT(sptd->spt_pcachecnt != 0);
ASSERT(sptd->spt_ppa == pplist);
ASSERT(npages == btopr(sptd->spt_amp->size));
ASSERT(async || AS_LOCK_HELD(seg->s_as));
/*
* Acquire the lock on the dummy seg and destroy the
* ppa array IF this is the last pcachecnt.
*/
mutex_enter(&sptd->spt_lock);
if (--sptd->spt_pcachecnt == 0) {
for (i = 0; i < npages; i++) {
if (pplist[i] == NULL) {
continue;
}
if (rw == S_WRITE) {
hat_setrefmod(pplist[i]);
} else {
hat_setref(pplist[i]);
}
if ((sptd->spt_flags & SHM_PAGEABLE) &&
(sptd->spt_ppa_lckcnt[i] == 0))
free_availrmem++;
page_unlock(pplist[i]);
}
if ((sptd->spt_flags & SHM_PAGEABLE) && free_availrmem) {
mutex_enter(&freemem_lock);
availrmem += free_availrmem;
mutex_exit(&freemem_lock);
}
/*
* Since we want to cach/uncache the entire ISM segment,
* we will track the pplist in a segspt specific field
* ppa, that is initialized at the time we add an entry to
* the cache.
*/
ASSERT(sptd->spt_pcachecnt == 0);
kmem_free(pplist, sizeof (page_t *) * npages);
sptd->spt_ppa = NULL;
sptd->spt_flags &= ~DISM_PPA_CHANGED;
sptd->spt_gen++;
cv_broadcast(&sptd->spt_cv);
done = 1;
}
mutex_exit(&sptd->spt_lock);
/*
* If we are pcache async thread or called via seg_ppurge_wiredpp() we
* may not hold AS lock (in this case async argument is not 0). This
* means if softlockcnt drops to 0 after the decrement below address
* space may get freed. We can't allow it since after softlock
* derement to 0 we still need to access as structure for possible
* wakeup of unmap waiters. To prevent the disappearance of as we take
* this segment's shm_segfree_syncmtx. segspt_shmfree() also takes
* this mutex as a barrier to make sure this routine completes before
* segment is freed.
*
* The second complication we have to deal with in async case is a
* possibility of missed wake up of unmap wait thread. When we don't
* hold as lock here we may take a_contents lock before unmap wait
* thread that was first to see softlockcnt was still not 0. As a
* result we'll fail to wake up an unmap wait thread. To avoid this
* race we set nounmapwait flag in as structure if we drop softlockcnt
* to 0 if async is not 0. unmapwait thread
* will not block if this flag is set.
*/
if (async)
mutex_enter(&shmd->shm_segfree_syncmtx);
/*
* Now decrement softlockcnt.
*/
ASSERT(shmd->shm_softlockcnt > 0);
atomic_dec_ulong((ulong_t *)(&(shmd->shm_softlockcnt)));
if (shmd->shm_softlockcnt <= 0) {
if (async || AS_ISUNMAPWAIT(seg->s_as)) {
mutex_enter(&seg->s_as->a_contents);
if (async)
AS_SETNOUNMAPWAIT(seg->s_as);
if (AS_ISUNMAPWAIT(seg->s_as)) {
AS_CLRUNMAPWAIT(seg->s_as);
cv_broadcast(&seg->s_as->a_cv);
}
mutex_exit(&seg->s_as->a_contents);
}
}
if (async)
mutex_exit(&shmd->shm_segfree_syncmtx);
return (done);
}
/*
* Do a F_SOFTUNLOCK call over the range requested.
* The range must have already been F_SOFTLOCK'ed.
*
* The calls to acquire and release the anon map lock mutex were
* removed in order to avoid a deadly embrace during a DR
* memory delete operation. (Eg. DR blocks while waiting for a
* exclusive lock on a page that is being used for kaio; the
* thread that will complete the kaio and call segspt_softunlock
* blocks on the anon map lock; another thread holding the anon
* map lock blocks on another page lock via the segspt_shmfault
* -> page_lookup -> page_lookup_create -> page_lock_es code flow.)
*
* The appropriateness of the removal is based upon the following:
* 1. If we are holding a segment's reader lock and the page is held
* shared, then the corresponding element in anonmap which points to
* anon struct cannot change and there is no need to acquire the
* anonymous map lock.
* 2. Threads in segspt_softunlock have a reader lock on the segment
* and already have the shared page lock, so we are guaranteed that
* the anon map slot cannot change and therefore can call anon_get_ptr()
* without grabbing the anonymous map lock.
* 3. Threads that softlock a shared page break copy-on-write, even if
* its a read. Thus cow faults can be ignored with respect to soft
* unlocking, since the breaking of cow means that the anon slot(s) will
* not be shared.
*/
static void
segspt_softunlock(struct seg *seg, caddr_t sptseg_addr,
size_t len, enum seg_rw rw)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg;
struct spt_data *sptd;
page_t *pp;
caddr_t adr;
struct vnode *vp;
u_offset_t offset;
ulong_t anon_index;
struct anon_map *amp; /* XXX - for locknest */
struct anon *ap = NULL;
pgcnt_t npages;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
sptseg = shmd->shm_sptseg;
sptd = sptseg->s_data;
/*
* Some platforms assume that ISM mappings are HAT_LOAD_LOCK
* and therefore their pages are SE_SHARED locked
* for the entire life of the segment.
*/
if ((!hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) &&
((sptd->spt_flags & SHM_PAGEABLE) == 0)) {
goto softlock_decrement;
}
/*
* Any thread is free to do a page_find and
* page_unlock() on the pages within this seg.
*
* We are already holding the as->a_lock on the user's
* real segment, but we need to hold the a_lock on the
* underlying dummy as. This is mostly to satisfy the
* underlying HAT layer.
*/
AS_LOCK_ENTER(sptseg->s_as, RW_READER);
hat_unlock(sptseg->s_as->a_hat, sptseg_addr, len);
AS_LOCK_EXIT(sptseg->s_as);
amp = sptd->spt_amp;
ASSERT(amp != NULL);
anon_index = seg_page(sptseg, sptseg_addr);
for (adr = sptseg_addr; adr < sptseg_addr + len; adr += PAGESIZE) {
ap = anon_get_ptr(amp->ahp, anon_index++);
ASSERT(ap != NULL);
swap_xlate(ap, &vp, &offset);
/*
* Use page_find() instead of page_lookup() to
* find the page since we know that it has a
* "shared" lock.
*/
pp = page_find(vp, offset);
ASSERT(ap == anon_get_ptr(amp->ahp, anon_index - 1));
if (pp == NULL) {
panic("segspt_softunlock: "
"addr %p, ap %p, vp %p, off %llx",
(void *)adr, (void *)ap, (void *)vp, offset);
/*NOTREACHED*/
}
if (rw == S_WRITE) {
hat_setrefmod(pp);
} else if (rw != S_OTHER) {
hat_setref(pp);
}
page_unlock(pp);
}
softlock_decrement:
npages = btopr(len);
ASSERT(shmd->shm_softlockcnt >= npages);
atomic_add_long((ulong_t *)(&(shmd->shm_softlockcnt)), -npages);
if (shmd->shm_softlockcnt == 0) {
/*
* All SOFTLOCKS are gone. Wakeup any waiting
* unmappers so they can try again to unmap.
* Check for waiters first without the mutex
* held so we don't always grab the mutex on
* softunlocks.
*/
if (AS_ISUNMAPWAIT(seg->s_as)) {
mutex_enter(&seg->s_as->a_contents);
if (AS_ISUNMAPWAIT(seg->s_as)) {
AS_CLRUNMAPWAIT(seg->s_as);
cv_broadcast(&seg->s_as->a_cv);
}
mutex_exit(&seg->s_as->a_contents);
}
}
}
int
segspt_shmattach(struct seg *seg, caddr_t *argsp)
{
struct shm_data *shmd_arg = (struct shm_data *)argsp;
struct shm_data *shmd;
struct anon_map *shm_amp = shmd_arg->shm_amp;
struct spt_data *sptd;
int error = 0;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
shmd = kmem_zalloc((sizeof (*shmd)), KM_NOSLEEP);
if (shmd == NULL)
return (ENOMEM);
shmd->shm_sptas = shmd_arg->shm_sptas;
shmd->shm_amp = shm_amp;
shmd->shm_sptseg = shmd_arg->shm_sptseg;
(void) lgrp_shm_policy_set(LGRP_MEM_POLICY_DEFAULT, shm_amp, 0,
NULL, 0, seg->s_size);
mutex_init(&shmd->shm_segfree_syncmtx, NULL, MUTEX_DEFAULT, NULL);
seg->s_data = (void *)shmd;
seg->s_ops = &segspt_shmops;
seg->s_szc = shmd->shm_sptseg->s_szc;
sptd = shmd->shm_sptseg->s_data;
if (sptd->spt_flags & SHM_PAGEABLE) {
if ((shmd->shm_vpage = kmem_zalloc(btopr(shm_amp->size),
KM_NOSLEEP)) == NULL) {
seg->s_data = (void *)NULL;
kmem_free(shmd, (sizeof (*shmd)));
return (ENOMEM);
}
shmd->shm_lckpgs = 0;
if (hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) {
if ((error = hat_share(seg->s_as->a_hat, seg->s_base,
shmd_arg->shm_sptas->a_hat, SEGSPTADDR,
seg->s_size, seg->s_szc)) != 0) {
kmem_free(shmd->shm_vpage,
btopr(shm_amp->size));
}
}
} else {
error = hat_share(seg->s_as->a_hat, seg->s_base,
shmd_arg->shm_sptas->a_hat, SEGSPTADDR,
seg->s_size, seg->s_szc);
}
if (error) {
seg->s_szc = 0;
seg->s_data = (void *)NULL;
kmem_free(shmd, (sizeof (*shmd)));
} else {
ANON_LOCK_ENTER(&shm_amp->a_rwlock, RW_WRITER);
shm_amp->refcnt++;
ANON_LOCK_EXIT(&shm_amp->a_rwlock);
}
return (error);
}
int
segspt_shmunmap(struct seg *seg, caddr_t raddr, size_t ssize)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
int reclaim = 1;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
retry:
if (shmd->shm_softlockcnt > 0) {
if (reclaim == 1) {
segspt_purge(seg);
reclaim = 0;
goto retry;
}
return (EAGAIN);
}
if (ssize != seg->s_size) {
#ifdef DEBUG
cmn_err(CE_WARN, "Incompatible ssize %lx s_size %lx\n",
ssize, seg->s_size);
#endif
return (EINVAL);
}
(void) segspt_shmlockop(seg, raddr, shmd->shm_amp->size, 0, MC_UNLOCK,
NULL, 0);
hat_unshare(seg->s_as->a_hat, raddr, ssize, seg->s_szc);
seg_free(seg);
return (0);
}
void
segspt_shmfree(struct seg *seg)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct anon_map *shm_amp = shmd->shm_amp;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
(void) segspt_shmlockop(seg, seg->s_base, shm_amp->size, 0,
MC_UNLOCK, NULL, 0);
/*
* Need to increment refcnt when attaching
* and decrement when detaching because of dup().
*/
ANON_LOCK_ENTER(&shm_amp->a_rwlock, RW_WRITER);
shm_amp->refcnt--;
ANON_LOCK_EXIT(&shm_amp->a_rwlock);
if (shmd->shm_vpage) { /* only for DISM */
kmem_free(shmd->shm_vpage, btopr(shm_amp->size));
shmd->shm_vpage = NULL;
}
/*
* Take shm_segfree_syncmtx lock to let segspt_reclaim() finish if it's
* still working with this segment without holding as lock.
*/
ASSERT(shmd->shm_softlockcnt == 0);
mutex_enter(&shmd->shm_segfree_syncmtx);
mutex_destroy(&shmd->shm_segfree_syncmtx);
kmem_free(shmd, sizeof (*shmd));
}
/*ARGSUSED*/
int
segspt_shmsetprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
{
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
/*
* Shared page table is more than shared mapping.
* Individual process sharing page tables can't change prot
* because there is only one set of page tables.
* This will be allowed after private page table is
* supported.
*/
/* need to return correct status error? */
return (0);
}
faultcode_t
segspt_dismfault(struct hat *hat, struct seg *seg, caddr_t addr,
size_t len, enum fault_type type, enum seg_rw rw)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg = shmd->shm_sptseg;
struct as *curspt = shmd->shm_sptas;
struct spt_data *sptd = sptseg->s_data;
pgcnt_t npages;
size_t size;
caddr_t segspt_addr, shm_addr;
page_t **ppa;
int i;
ulong_t an_idx = 0;
int err = 0;
int dyn_ism_unmap = hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0);
size_t pgsz;
pgcnt_t pgcnt;
caddr_t a;
pgcnt_t pidx;
#ifdef lint
hat = hat;
#endif
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
/*
* Because of the way spt is implemented
* the realsize of the segment does not have to be
* equal to the segment size itself. The segment size is
* often in multiples of a page size larger than PAGESIZE.
* The realsize is rounded up to the nearest PAGESIZE
* based on what the user requested. This is a bit of
* ungliness that is historical but not easily fixed
* without re-designing the higher levels of ISM.
*/
ASSERT(addr >= seg->s_base);
if (((addr + len) - seg->s_base) > sptd->spt_realsize)
return (FC_NOMAP);
/*
* For all of the following cases except F_PROT, we need to
* make any necessary adjustments to addr and len
* and get all of the necessary page_t's into an array called ppa[].
*
* The code in shmat() forces base addr and len of ISM segment
* to be aligned to largest page size supported. Therefore,
* we are able to handle F_SOFTLOCK and F_INVAL calls in "large
* pagesize" chunks. We want to make sure that we HAT_LOAD_LOCK
* in large pagesize chunks, or else we will screw up the HAT
* layer by calling hat_memload_array() with differing page sizes
* over a given virtual range.
*/
pgsz = page_get_pagesize(sptseg->s_szc);
pgcnt = page_get_pagecnt(sptseg->s_szc);
shm_addr = (caddr_t)P2ALIGN((uintptr_t)(addr), pgsz);
size = P2ROUNDUP((uintptr_t)(((addr + len) - shm_addr)), pgsz);
npages = btopr(size);
/*
* Now we need to convert from addr in segshm to addr in segspt.
*/
an_idx = seg_page(seg, shm_addr);
segspt_addr = sptseg->s_base + ptob(an_idx);
ASSERT((segspt_addr + ptob(npages)) <=
(sptseg->s_base + sptd->spt_realsize));
ASSERT(segspt_addr < (sptseg->s_base + sptseg->s_size));
switch (type) {
case F_SOFTLOCK:
atomic_add_long((ulong_t *)(&(shmd->shm_softlockcnt)), npages);
/*
* Fall through to the F_INVAL case to load up the hat layer
* entries with the HAT_LOAD_LOCK flag.
*/
/* FALLTHRU */
case F_INVAL:
if ((rw == S_EXEC) && !(sptd->spt_prot & PROT_EXEC))
return (FC_NOMAP);
ppa = kmem_zalloc(npages * sizeof (page_t *), KM_SLEEP);
err = spt_anon_getpages(sptseg, segspt_addr, size, ppa);
if (err != 0) {
if (type == F_SOFTLOCK) {
atomic_add_long((ulong_t *)(
&(shmd->shm_softlockcnt)), -npages);
}
goto dism_err;
}
AS_LOCK_ENTER(sptseg->s_as, RW_READER);
a = segspt_addr;
pidx = 0;
if (type == F_SOFTLOCK) {
/*
* Load up the translation keeping it
* locked and don't unlock the page.
*/
for (; pidx < npages; a += pgsz, pidx += pgcnt) {
hat_memload_array(sptseg->s_as->a_hat,
a, pgsz, &ppa[pidx], sptd->spt_prot,
HAT_LOAD_LOCK | HAT_LOAD_SHARE);
}
} else {
/*
* Migrate pages marked for migration
*/
if (lgrp_optimizations())
page_migrate(seg, shm_addr, ppa, npages);
for (; pidx < npages; a += pgsz, pidx += pgcnt) {
hat_memload_array(sptseg->s_as->a_hat,
a, pgsz, &ppa[pidx],
sptd->spt_prot,
HAT_LOAD_SHARE);
}
/*
* And now drop the SE_SHARED lock(s).
*/
if (dyn_ism_unmap) {
for (i = 0; i < npages; i++) {
page_unlock(ppa[i]);
}
}
}
if (!dyn_ism_unmap) {
if (hat_share(seg->s_as->a_hat, shm_addr,
curspt->a_hat, segspt_addr, ptob(npages),
seg->s_szc) != 0) {
panic("hat_share err in DISM fault");
/* NOTREACHED */
}
if (type == F_INVAL) {
for (i = 0; i < npages; i++) {
page_unlock(ppa[i]);
}
}
}
AS_LOCK_EXIT(sptseg->s_as);
dism_err:
kmem_free(ppa, npages * sizeof (page_t *));
return (err);
case F_SOFTUNLOCK:
/*
* This is a bit ugly, we pass in the real seg pointer,
* but the segspt_addr is the virtual address within the
* dummy seg.
*/
segspt_softunlock(seg, segspt_addr, size, rw);
return (0);
case F_PROT:
/*
* This takes care of the unusual case where a user
* allocates a stack in shared memory and a register
* window overflow is written to that stack page before
* it is otherwise modified.
*
* We can get away with this because ISM segments are
* always rw. Other than this unusual case, there
* should be no instances of protection violations.
*/
return (0);
default:
#ifdef DEBUG
panic("segspt_dismfault default type?");
#else
return (FC_NOMAP);
#endif
}
}
faultcode_t
segspt_shmfault(struct hat *hat, struct seg *seg, caddr_t addr,
size_t len, enum fault_type type, enum seg_rw rw)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct seg *sptseg = shmd->shm_sptseg;
struct as *curspt = shmd->shm_sptas;
struct spt_data *sptd = sptseg->s_data;
pgcnt_t npages;
size_t size;
caddr_t sptseg_addr, shm_addr;
page_t *pp, **ppa;
int i;
u_offset_t offset;
ulong_t anon_index = 0;
struct vnode *vp;
struct anon_map *amp; /* XXX - for locknest */
struct anon *ap = NULL;
size_t pgsz;
pgcnt_t pgcnt;
caddr_t a;
pgcnt_t pidx;
size_t sz;
#ifdef lint
hat = hat;
#endif
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
if (sptd->spt_flags & SHM_PAGEABLE) {
return (segspt_dismfault(hat, seg, addr, len, type, rw));
}
/*
* Because of the way spt is implemented
* the realsize of the segment does not have to be
* equal to the segment size itself. The segment size is
* often in multiples of a page size larger than PAGESIZE.
* The realsize is rounded up to the nearest PAGESIZE
* based on what the user requested. This is a bit of
* ungliness that is historical but not easily fixed
* without re-designing the higher levels of ISM.
*/
ASSERT(addr >= seg->s_base);
if (((addr + len) - seg->s_base) > sptd->spt_realsize)
return (FC_NOMAP);
/*
* For all of the following cases except F_PROT, we need to
* make any necessary adjustments to addr and len
* and get all of the necessary page_t's into an array called ppa[].
*
* The code in shmat() forces base addr and len of ISM segment
* to be aligned to largest page size supported. Therefore,
* we are able to handle F_SOFTLOCK and F_INVAL calls in "large
* pagesize" chunks. We want to make sure that we HAT_LOAD_LOCK
* in large pagesize chunks, or else we will screw up the HAT
* layer by calling hat_memload_array() with differing page sizes
* over a given virtual range.
*/
pgsz = page_get_pagesize(sptseg->s_szc);
pgcnt = page_get_pagecnt(sptseg->s_szc);
shm_addr = (caddr_t)P2ALIGN((uintptr_t)(addr), pgsz);
size = P2ROUNDUP((uintptr_t)(((addr + len) - shm_addr)), pgsz);
npages = btopr(size);
/*
* Now we need to convert from addr in segshm to addr in segspt.
*/
anon_index = seg_page(seg, shm_addr);
sptseg_addr = sptseg->s_base + ptob(anon_index);
/*
* And now we may have to adjust npages downward if we have
* exceeded the realsize of the segment or initial anon
* allocations.
*/
if ((sptseg_addr + ptob(npages)) >
(sptseg->s_base + sptd->spt_realsize))
size = (sptseg->s_base + sptd->spt_realsize) - sptseg_addr;
npages = btopr(size);
ASSERT(sptseg_addr < (sptseg->s_base + sptseg->s_size));
ASSERT((sptd->spt_flags & SHM_PAGEABLE) == 0);
switch (type) {
case F_SOFTLOCK:
/*
* availrmem is decremented once during anon_swap_adjust()
* and is incremented during the anon_unresv(), which is
* called from shm_rm_amp() when the segment is destroyed.
*/
atomic_add_long((ulong_t *)(&(shmd->shm_softlockcnt)), npages);
/*
* Some platforms assume that ISM pages are SE_SHARED
* locked for the entire life of the segment.
*/
if (!hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0))
return (0);
/*
* Fall through to the F_INVAL case to load up the hat layer
* entries with the HAT_LOAD_LOCK flag.
*/
/* FALLTHRU */
case F_INVAL:
if ((rw == S_EXEC) && !(sptd->spt_prot & PROT_EXEC))
return (FC_NOMAP);
/*
* Some platforms that do NOT support DYNAMIC_ISM_UNMAP
* may still rely on this call to hat_share(). That
* would imply that those hat's can fault on a
* HAT_LOAD_LOCK translation, which would seem
* contradictory.
*/
if (!hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) {
if (hat_share(seg->s_as->a_hat, seg->s_base,
curspt->a_hat, sptseg->s_base,
sptseg->s_size, sptseg->s_szc) != 0) {
panic("hat_share error in ISM fault");
/*NOTREACHED*/
}
return (0);
}
ppa = kmem_zalloc(sizeof (page_t *) * npages, KM_SLEEP);
/*
* I see no need to lock the real seg,
* here, because all of our work will be on the underlying
* dummy seg.
*
* sptseg_addr and npages now account for large pages.
*/
amp = sptd->spt_amp;
ASSERT(amp != NULL);
anon_index = seg_page(sptseg, sptseg_addr);
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
for (i = 0; i < npages; i++) {
ap = anon_get_ptr(amp->ahp, anon_index++);
ASSERT(ap != NULL);
swap_xlate(ap, &vp, &offset);
pp = page_lookup(vp, offset, SE_SHARED);
ASSERT(pp != NULL);
ppa[i] = pp;
}
ANON_LOCK_EXIT(&->a_rwlock);
ASSERT(i == npages);
/*
* We are already holding the as->a_lock on the user's
* real segment, but we need to hold the a_lock on the
* underlying dummy as. This is mostly to satisfy the
* underlying HAT layer.
*/
AS_LOCK_ENTER(sptseg->s_as, RW_READER);
a = sptseg_addr;
pidx = 0;
if (type == F_SOFTLOCK) {
/*
* Load up the translation keeping it
* locked and don't unlock the page.
*/
for (; pidx < npages; a += pgsz, pidx += pgcnt) {
sz = MIN(pgsz, ptob(npages - pidx));
hat_memload_array(sptseg->s_as->a_hat, a,
sz, &ppa[pidx], sptd->spt_prot,
HAT_LOAD_LOCK | HAT_LOAD_SHARE);
}
} else {
/*
* Migrate pages marked for migration.
*/
if (lgrp_optimizations())
page_migrate(seg, shm_addr, ppa, npages);
for (; pidx < npages; a += pgsz, pidx += pgcnt) {
sz = MIN(pgsz, ptob(npages - pidx));
hat_memload_array(sptseg->s_as->a_hat,
a, sz, &ppa[pidx],
sptd->spt_prot, HAT_LOAD_SHARE);
}
/*
* And now drop the SE_SHARED lock(s).
*/
for (i = 0; i < npages; i++)
page_unlock(ppa[i]);
}
AS_LOCK_EXIT(sptseg->s_as);
kmem_free(ppa, sizeof (page_t *) * npages);
return (0);
case F_SOFTUNLOCK:
/*
* This is a bit ugly, we pass in the real seg pointer,
* but the sptseg_addr is the virtual address within the
* dummy seg.
*/
segspt_softunlock(seg, sptseg_addr, ptob(npages), rw);
return (0);
case F_PROT:
/*
* This takes care of the unusual case where a user
* allocates a stack in shared memory and a register
* window overflow is written to that stack page before
* it is otherwise modified.
*
* We can get away with this because ISM segments are
* always rw. Other than this unusual case, there
* should be no instances of protection violations.
*/
return (0);
default:
#ifdef DEBUG
cmn_err(CE_WARN, "segspt_shmfault default type?");
#endif
return (FC_NOMAP);
}
}
/*ARGSUSED*/
static faultcode_t
segspt_shmfaulta(struct seg *seg, caddr_t addr)
{
return (0);
}
/*ARGSUSED*/
static int
segspt_shmkluster(struct seg *seg, caddr_t addr, ssize_t delta)
{
return (0);
}
/*ARGSUSED*/
static size_t
segspt_shmswapout(struct seg *seg)
{
return (0);
}
/*
* duplicate the shared page tables
*/
int
segspt_shmdup(struct seg *seg, struct seg *newseg)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct anon_map *amp = shmd->shm_amp;
struct shm_data *shmd_new;
struct seg *spt_seg = shmd->shm_sptseg;
struct spt_data *sptd = spt_seg->s_data;
int error = 0;
ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as));
shmd_new = kmem_zalloc((sizeof (*shmd_new)), KM_SLEEP);
newseg->s_data = (void *)shmd_new;
shmd_new->shm_sptas = shmd->shm_sptas;
shmd_new->shm_amp = amp;
shmd_new->shm_sptseg = shmd->shm_sptseg;
newseg->s_ops = &segspt_shmops;
newseg->s_szc = seg->s_szc;
ASSERT(seg->s_szc == shmd->shm_sptseg->s_szc);
ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
amp->refcnt++;
ANON_LOCK_EXIT(&->a_rwlock);
if (sptd->spt_flags & SHM_PAGEABLE) {
shmd_new->shm_vpage = kmem_zalloc(btopr(amp->size), KM_SLEEP);
shmd_new->shm_lckpgs = 0;
if (hat_supported(HAT_DYNAMIC_ISM_UNMAP, (void *)0)) {
if ((error = hat_share(newseg->s_as->a_hat,
newseg->s_base, shmd->shm_sptas->a_hat, SEGSPTADDR,
seg->s_size, seg->s_szc)) != 0) {
kmem_free(shmd_new->shm_vpage,
btopr(amp->size));
}
}
return (error);
} else {
return (hat_share(newseg->s_as->a_hat, newseg->s_base,
shmd->shm_sptas->a_hat, SEGSPTADDR, seg->s_size,
seg->s_szc));
}
}
/*ARGSUSED*/
int
segspt_shmcheckprot(struct seg *seg, caddr_t addr, size_t size, uint_t prot)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct spt_data *sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
/*
* ISM segment is always rw.
*/
return (((sptd->spt_prot & prot) != prot) ? EACCES : 0);
}
/*
* Return an array of locked large pages, for empty slots allocate
* private zero-filled anon pages.
*/
static int
spt_anon_getpages(
struct seg *sptseg,
caddr_t sptaddr,
size_t len,
page_t *ppa[])
{
struct spt_data *sptd = sptseg->s_data;
struct anon_map *amp = sptd->spt_amp;
enum seg_rw rw = sptd->spt_prot;
uint_t szc = sptseg->s_szc;
size_t pg_sz, share_sz = page_get_pagesize(szc);
pgcnt_t lp_npgs;
caddr_t lp_addr, e_sptaddr;
uint_t vpprot, ppa_szc = 0;
struct vpage *vpage = NULL;
ulong_t j, ppa_idx;
int err, ierr = 0;
pgcnt_t an_idx;
anon_sync_obj_t cookie;
int anon_locked = 0;
pgcnt_t amp_pgs;
ASSERT(IS_P2ALIGNED(sptaddr, share_sz) && IS_P2ALIGNED(len, share_sz));
ASSERT(len != 0);
pg_sz = share_sz;
lp_npgs = btop(pg_sz);
lp_addr = sptaddr;
e_sptaddr = sptaddr + len;
an_idx = seg_page(sptseg, sptaddr);
ppa_idx = 0;
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
amp_pgs = page_get_pagecnt(amp->a_szc);
/*CONSTCOND*/
while (1) {
for (; lp_addr < e_sptaddr;
an_idx += lp_npgs, lp_addr += pg_sz, ppa_idx += lp_npgs) {
/*
* If we're currently locked, and we get to a new
* page, unlock our current anon chunk.
*/
if (anon_locked && P2PHASE(an_idx, amp_pgs) == 0) {
anon_array_exit(&cookie);
anon_locked = 0;
}
if (!anon_locked) {
anon_array_enter(amp, an_idx, &cookie);
anon_locked = 1;
}
ppa_szc = (uint_t)-1;
ierr = anon_map_getpages(amp, an_idx, szc, sptseg,
lp_addr, sptd->spt_prot, &vpprot, &ppa[ppa_idx],
&ppa_szc, vpage, rw, 0, segvn_anypgsz, 0, kcred);
if (ierr != 0) {
if (ierr > 0) {
err = FC_MAKE_ERR(ierr);
goto lpgs_err;
}
break;
}
}
if (lp_addr == e_sptaddr) {
break;
}
ASSERT(lp_addr < e_sptaddr);
/*
* ierr == -1 means we failed to allocate a large page.
* so do a size down operation.
*
* ierr == -2 means some other process that privately shares
* pages with this process has allocated a larger page and we
* need to retry with larger pages. So do a size up
* operation. This relies on the fact that large pages are
* never partially shared i.e. if we share any constituent
* page of a large page with another process we must share the
* entire large page. Note this cannot happen for SOFTLOCK
* case, unless current address (lpaddr) is at the beginning
* of the next page size boundary because the other process
* couldn't have relocated locked pages.
*/
ASSERT(ierr == -1 || ierr == -2);
if (segvn_anypgsz) {
ASSERT(ierr == -2 || szc != 0);
ASSERT(ierr == -1 || szc < sptseg->s_szc);
szc = (ierr == -1) ? szc - 1 : szc + 1;
} else {
/*
* For faults and segvn_anypgsz == 0
* we need to be careful not to loop forever
* if existing page is found with szc other
* than 0 or seg->s_szc. This could be due
* to page relocations on behalf of DR or
* more likely large page creation. For this
* case simply re-size to existing page's szc
* if returned by anon_map_getpages().
*/
if (ppa_szc == (uint_t)-1) {
szc = (ierr == -1) ? 0 : sptseg->s_szc;
} else {
ASSERT(ppa_szc <= sptseg->s_szc);
ASSERT(ierr == -2 || ppa_szc < szc);
ASSERT(ierr == -1 || ppa_szc > szc);
szc = ppa_szc;
}
}
pg_sz = page_get_pagesize(szc);
lp_npgs = btop(pg_sz);
ASSERT(IS_P2ALIGNED(lp_addr, pg_sz));
}
if (anon_locked) {
anon_array_exit(&cookie);
}
ANON_LOCK_EXIT(&->a_rwlock);
return (0);
lpgs_err:
if (anon_locked) {
anon_array_exit(&cookie);
}
ANON_LOCK_EXIT(&->a_rwlock);
for (j = 0; j < ppa_idx; j++)
page_unlock(ppa[j]);
return (err);
}
/*
* count the number of bytes in a set of spt pages that are currently not
* locked
*/
static rctl_qty_t
spt_unlockedbytes(pgcnt_t npages, page_t **ppa)
{
ulong_t i;
rctl_qty_t unlocked = 0;
for (i = 0; i < npages; i++) {
if (ppa[i]->p_lckcnt == 0)
unlocked += PAGESIZE;
}
return (unlocked);
}
extern u_longlong_t randtick(void);
/* number of locks to reserve/skip by spt_lockpages() and spt_unlockpages() */
#define NLCK (NCPU_P2)
/* Random number with a range [0, n-1], n must be power of two */
#define RAND_P2(n) \
((((long)curthread >> PTR24_LSB) ^ (long)randtick()) & ((n) - 1))
int
spt_lockpages(struct seg *seg, pgcnt_t anon_index, pgcnt_t npages,
page_t **ppa, ulong_t *lockmap, size_t pos,
rctl_qty_t *locked)
{
struct shm_data *shmd = seg->s_data;
struct spt_data *sptd = shmd->shm_sptseg->s_data;
ulong_t i;
int kernel;
pgcnt_t nlck = 0;
int rv = 0;
int use_reserved = 1;
/* return the number of bytes actually locked */
*locked = 0;
/*
* To avoid contention on freemem_lock, availrmem and pages_locked
* global counters are updated only every nlck locked pages instead of
* every time. Reserve nlck locks up front and deduct from this
* reservation for each page that requires a lock. When the reservation
* is consumed, reserve again. nlck is randomized, so the competing
* threads do not fall into a cyclic lock contention pattern. When
* memory is low, the lock ahead is disabled, and instead page_pp_lock()
* is used to lock pages.
*/
for (i = 0; i < npages; anon_index++, pos++, i++) {
if (nlck == 0 && use_reserved == 1) {
nlck = NLCK + RAND_P2(NLCK);
/* if fewer loops left, decrease nlck */
nlck = MIN(nlck, npages - i);
/*
* Reserve nlck locks up front and deduct from this
* reservation for each page that requires a lock. When
* the reservation is consumed, reserve again.
*/
mutex_enter(&freemem_lock);
if ((availrmem - nlck) < pages_pp_maximum) {
/* Do not do advance memory reserves */
use_reserved = 0;
} else {
availrmem -= nlck;
pages_locked += nlck;
}
mutex_exit(&freemem_lock);
}
if (!(shmd->shm_vpage[anon_index] & DISM_PG_LOCKED)) {
if (sptd->spt_ppa_lckcnt[anon_index] <
(ushort_t)DISM_LOCK_MAX) {
if (++sptd->spt_ppa_lckcnt[anon_index] ==
(ushort_t)DISM_LOCK_MAX) {
cmn_err(CE_WARN,
"DISM page lock limit "
"reached on DISM offset 0x%lx\n",
anon_index << PAGESHIFT);
}
kernel = (sptd->spt_ppa &&
sptd->spt_ppa[anon_index]);
if (!page_pp_lock(ppa[i], 0, kernel ||
use_reserved)) {
sptd->spt_ppa_lckcnt[anon_index]--;
rv = EAGAIN;
break;
}
/* if this is a newly locked page, count it */
if (ppa[i]->p_lckcnt == 1) {
if (kernel == 0 && use_reserved == 1)
nlck--;
*locked += PAGESIZE;
}
shmd->shm_lckpgs++;
shmd->shm_vpage[anon_index] |= DISM_PG_LOCKED;
if (lockmap != NULL)
BT_SET(lockmap, pos);
}
}
}
/* Return unused lock reservation */
if (nlck != 0 && use_reserved == 1) {
mutex_enter(&freemem_lock);
availrmem += nlck;
pages_locked -= nlck;
mutex_exit(&freemem_lock);
}
return (rv);
}
int
spt_unlockpages(struct seg *seg, pgcnt_t anon_index, pgcnt_t npages,
rctl_qty_t *unlocked)
{
struct shm_data *shmd = seg->s_data;
struct spt_data *sptd = shmd->shm_sptseg->s_data;
struct anon_map *amp = sptd->spt_amp;
struct anon *ap;
struct vnode *vp;
u_offset_t off;
struct page *pp;
int kernel;
anon_sync_obj_t cookie;
ulong_t i;
pgcnt_t nlck = 0;
pgcnt_t nlck_limit = NLCK;
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
for (i = 0; i < npages; i++, anon_index++) {
if (shmd->shm_vpage[anon_index] & DISM_PG_LOCKED) {
anon_array_enter(amp, anon_index, &cookie);
ap = anon_get_ptr(amp->ahp, anon_index);
ASSERT(ap);
swap_xlate(ap, &vp, &off);
anon_array_exit(&cookie);
pp = page_lookup(vp, off, SE_SHARED);
ASSERT(pp);
/*
* availrmem is decremented only for pages which are not
* in seg pcache, for pages in seg pcache availrmem was
* decremented in _dismpagelock()
*/
kernel = (sptd->spt_ppa && sptd->spt_ppa[anon_index]);
ASSERT(pp->p_lckcnt > 0);
/*
* lock page but do not change availrmem, we do it
* ourselves every nlck loops.
*/
page_pp_unlock(pp, 0, 1);
if (pp->p_lckcnt == 0) {
if (kernel == 0)
nlck++;
*unlocked += PAGESIZE;
}
page_unlock(pp);
shmd->shm_vpage[anon_index] &= ~DISM_PG_LOCKED;
sptd->spt_ppa_lckcnt[anon_index]--;
shmd->shm_lckpgs--;
}
/*
* To reduce freemem_lock contention, do not update availrmem
* until at least NLCK pages have been unlocked.
* 1. No need to update if nlck is zero
* 2. Always update if the last iteration
*/
if (nlck > 0 && (nlck == nlck_limit || i == npages - 1)) {
mutex_enter(&freemem_lock);
availrmem += nlck;
pages_locked -= nlck;
mutex_exit(&freemem_lock);
nlck = 0;
nlck_limit = NLCK + RAND_P2(NLCK);
}
}
ANON_LOCK_EXIT(&->a_rwlock);
return (0);
}
/*ARGSUSED*/
static int
segspt_shmlockop(struct seg *seg, caddr_t addr, size_t len,
int attr, int op, ulong_t *lockmap, size_t pos)
{
struct shm_data *shmd = seg->s_data;
struct seg *sptseg = shmd->shm_sptseg;
struct spt_data *sptd = sptseg->s_data;
struct kshmid *sp = sptd->spt_amp->a_sp;
pgcnt_t npages, a_npages;
page_t **ppa;
pgcnt_t an_idx, a_an_idx, ppa_idx;
caddr_t spt_addr, a_addr; /* spt and aligned address */
size_t a_len; /* aligned len */
size_t share_sz;
ulong_t i;
int sts = 0;
rctl_qty_t unlocked = 0;
rctl_qty_t locked = 0;
struct proc *p = curproc;
kproject_t *proj;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
ASSERT(sp != NULL);
if ((sptd->spt_flags & SHM_PAGEABLE) == 0) {
return (0);
}
addr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
an_idx = seg_page(seg, addr);
npages = btopr(len);
if (an_idx + npages > btopr(shmd->shm_amp->size)) {
return (ENOMEM);
}
/*
* A shm's project never changes, so no lock needed.
* The shm has a hold on the project, so it will not go away.
* Since we have a mapping to shm within this zone, we know
* that the zone will not go away.
*/
proj = sp->shm_perm.ipc_proj;
if (op == MC_LOCK) {
/*
* Need to align addr and size request if they are not
* aligned so we can always allocate large page(s) however
* we only lock what was requested in initial request.
*/
share_sz = page_get_pagesize(sptseg->s_szc);
a_addr = (caddr_t)P2ALIGN((uintptr_t)(addr), share_sz);
a_len = P2ROUNDUP((uintptr_t)(((addr + len) - a_addr)),
share_sz);
a_npages = btop(a_len);
a_an_idx = seg_page(seg, a_addr);
spt_addr = sptseg->s_base + ptob(a_an_idx);
ppa_idx = an_idx - a_an_idx;
if ((ppa = kmem_zalloc(((sizeof (page_t *)) * a_npages),
KM_NOSLEEP)) == NULL) {
return (ENOMEM);
}
/*
* Don't cache any new pages for IO and
* flush any cached pages.
*/
mutex_enter(&sptd->spt_lock);
if (sptd->spt_ppa != NULL)
sptd->spt_flags |= DISM_PPA_CHANGED;
sts = spt_anon_getpages(sptseg, spt_addr, a_len, ppa);
if (sts != 0) {
mutex_exit(&sptd->spt_lock);
kmem_free(ppa, ((sizeof (page_t *)) * a_npages));
return (sts);
}
mutex_enter(&sp->shm_mlock);
/* enforce locked memory rctl */
unlocked = spt_unlockedbytes(npages, &ppa[ppa_idx]);
mutex_enter(&p->p_lock);
if (rctl_incr_locked_mem(p, proj, unlocked, 0)) {
mutex_exit(&p->p_lock);
sts = EAGAIN;
} else {
mutex_exit(&p->p_lock);
sts = spt_lockpages(seg, an_idx, npages,
&ppa[ppa_idx], lockmap, pos, &locked);
/*
* correct locked count if not all pages could be
* locked
*/
if ((unlocked - locked) > 0) {
rctl_decr_locked_mem(NULL, proj,
(unlocked - locked), 0);
}
}
/*
* unlock pages
*/
for (i = 0; i < a_npages; i++)
page_unlock(ppa[i]);
if (sptd->spt_ppa != NULL)
sptd->spt_flags |= DISM_PPA_CHANGED;
mutex_exit(&sp->shm_mlock);
mutex_exit(&sptd->spt_lock);
kmem_free(ppa, ((sizeof (page_t *)) * a_npages));
} else if (op == MC_UNLOCK) { /* unlock */
page_t **ppa;
mutex_enter(&sptd->spt_lock);
if (shmd->shm_lckpgs == 0) {
mutex_exit(&sptd->spt_lock);
return (0);
}
/*
* Don't cache new IO pages.
*/
if (sptd->spt_ppa != NULL)
sptd->spt_flags |= DISM_PPA_CHANGED;
mutex_enter(&sp->shm_mlock);
sts = spt_unlockpages(seg, an_idx, npages, &unlocked);
if ((ppa = sptd->spt_ppa) != NULL)
sptd->spt_flags |= DISM_PPA_CHANGED;
mutex_exit(&sptd->spt_lock);
rctl_decr_locked_mem(NULL, proj, unlocked, 0);
mutex_exit(&sp->shm_mlock);
if (ppa != NULL)
seg_ppurge_wiredpp(ppa);
}
return (sts);
}
/*ARGSUSED*/
int
segspt_shmgetprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct spt_data *sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
spgcnt_t pgno = seg_page(seg, addr+len) - seg_page(seg, addr) + 1;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
/*
* ISM segment is always rw.
*/
while (--pgno >= 0)
*protv++ = sptd->spt_prot;
return (0);
}
/*ARGSUSED*/
u_offset_t
segspt_shmgetoffset(struct seg *seg, caddr_t addr)
{
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
/* Offset does not matter in ISM memory */
return ((u_offset_t)0);
}
/* ARGSUSED */
int
segspt_shmgettype(struct seg *seg, caddr_t addr)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct spt_data *sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
/*
* The shared memory mapping is always MAP_SHARED, SWAP is only
* reserved for DISM
*/
return (MAP_SHARED |
((sptd->spt_flags & SHM_PAGEABLE) ? 0 : MAP_NORESERVE));
}
/*ARGSUSED*/
int
segspt_shmgetvp(struct seg *seg, caddr_t addr, struct vnode **vpp)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct spt_data *sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
*vpp = sptd->spt_vp;
return (0);
}
/*
* We need to wait for pending IO to complete to a DISM segment in order for
* pages to get kicked out of the seg_pcache. 120 seconds should be more
* than enough time to wait.
*/
static clock_t spt_pcache_wait = 120;
/*ARGSUSED*/
static int
segspt_shmadvise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct spt_data *sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
struct anon_map *amp;
pgcnt_t pg_idx;
ushort_t gen;
clock_t end_lbolt;
int writer;
page_t **ppa;
ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as));
if (behav == MADV_FREE || behav == MADV_PURGE) {
if ((sptd->spt_flags & SHM_PAGEABLE) == 0)
return (0);
amp = sptd->spt_amp;
pg_idx = seg_page(seg, addr);
mutex_enter(&sptd->spt_lock);
if ((ppa = sptd->spt_ppa) == NULL) {
mutex_exit(&sptd->spt_lock);
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
(void) anon_disclaim(amp, pg_idx, len, behav, NULL);
ANON_LOCK_EXIT(&->a_rwlock);
return (0);
}
sptd->spt_flags |= DISM_PPA_CHANGED;
gen = sptd->spt_gen;
mutex_exit(&sptd->spt_lock);
/*
* Purge all DISM cached pages
*/
seg_ppurge_wiredpp(ppa);
/*
* Drop the AS_LOCK so that other threads can grab it
* in the as_pageunlock path and hopefully get the segment
* kicked out of the seg_pcache. We bump the shm_softlockcnt
* to keep this segment resident.
*/
writer = AS_WRITE_HELD(seg->s_as);
atomic_inc_ulong((ulong_t *)(&(shmd->shm_softlockcnt)));
AS_LOCK_EXIT(seg->s_as);
mutex_enter(&sptd->spt_lock);
end_lbolt = ddi_get_lbolt() + (hz * spt_pcache_wait);
/*
* Try to wait for pages to get kicked out of the seg_pcache.
*/
while (sptd->spt_gen == gen &&
(sptd->spt_flags & DISM_PPA_CHANGED) &&
ddi_get_lbolt() < end_lbolt) {
if (!cv_timedwait_sig(&sptd->spt_cv,
&sptd->spt_lock, end_lbolt)) {
break;
}
}
mutex_exit(&sptd->spt_lock);
/* Regrab the AS_LOCK and release our hold on the segment */
AS_LOCK_ENTER(seg->s_as, writer ? RW_WRITER : RW_READER);
atomic_dec_ulong((ulong_t *)(&(shmd->shm_softlockcnt)));
if (shmd->shm_softlockcnt <= 0) {
if (AS_ISUNMAPWAIT(seg->s_as)) {
mutex_enter(&seg->s_as->a_contents);
if (AS_ISUNMAPWAIT(seg->s_as)) {
AS_CLRUNMAPWAIT(seg->s_as);
cv_broadcast(&seg->s_as->a_cv);
}
mutex_exit(&seg->s_as->a_contents);
}
}
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
(void) anon_disclaim(amp, pg_idx, len, behav, NULL);
ANON_LOCK_EXIT(&->a_rwlock);
} else if (lgrp_optimizations() && (behav == MADV_ACCESS_LWP ||
behav == MADV_ACCESS_MANY || behav == MADV_ACCESS_DEFAULT)) {
int already_set;
ulong_t anon_index;
lgrp_mem_policy_t policy;
caddr_t shm_addr;
size_t share_size;
size_t size;
struct seg *sptseg = shmd->shm_sptseg;
caddr_t sptseg_addr;
/*
* Align address and length to page size of underlying segment
*/
share_size = page_get_pagesize(shmd->shm_sptseg->s_szc);
shm_addr = (caddr_t)P2ALIGN((uintptr_t)(addr), share_size);
size = P2ROUNDUP((uintptr_t)(((addr + len) - shm_addr)),
share_size);
amp = shmd->shm_amp;
anon_index = seg_page(seg, shm_addr);
/*
* And now we may have to adjust size downward if we have
* exceeded the realsize of the segment or initial anon
* allocations.
*/
sptseg_addr = sptseg->s_base + ptob(anon_index);
if ((sptseg_addr + size) >
(sptseg->s_base + sptd->spt_realsize))
size = (sptseg->s_base + sptd->spt_realsize) -
sptseg_addr;
/*
* Set memory allocation policy for this segment
*/
policy = lgrp_madv_to_policy(behav, len, MAP_SHARED);
already_set = lgrp_shm_policy_set(policy, amp, anon_index,
NULL, 0, len);
/*
* If random memory allocation policy set already,
* don't bother reapplying it.
*/
if (already_set && !LGRP_MEM_POLICY_REAPPLICABLE(policy))
return (0);
/*
* Mark any existing pages in the given range for
* migration, flushing the I/O page cache, and using
* underlying segment to calculate anon index and get
* anonmap and vnode pointer from
*/
if (shmd->shm_softlockcnt > 0)
segspt_purge(seg);
page_mark_migrate(seg, shm_addr, size, amp, 0, NULL, 0, 0);
}
return (0);
}
/*ARGSUSED*/
void
segspt_shmdump(struct seg *seg)
{
/* no-op for ISM segment */
}
/*ARGSUSED*/
static faultcode_t
segspt_shmsetpgsz(struct seg *seg, caddr_t addr, size_t len, uint_t szc)
{
return (ENOTSUP);
}
/*
* get a memory ID for an addr in a given segment
*/
static int
segspt_shmgetmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
{
struct shm_data *shmd = (struct shm_data *)seg->s_data;
struct anon *ap;
size_t anon_index;
struct anon_map *amp = shmd->shm_amp;
struct spt_data *sptd = shmd->shm_sptseg->s_data;
struct seg *sptseg = shmd->shm_sptseg;
anon_sync_obj_t cookie;
anon_index = seg_page(seg, addr);
if (addr > (seg->s_base + sptd->spt_realsize)) {
return (EFAULT);
}
ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
anon_array_enter(amp, anon_index, &cookie);
ap = anon_get_ptr(amp->ahp, anon_index);
if (ap == NULL) {
struct page *pp;
caddr_t spt_addr = sptseg->s_base + ptob(anon_index);
pp = anon_zero(sptseg, spt_addr, &ap, kcred);
if (pp == NULL) {
anon_array_exit(&cookie);
ANON_LOCK_EXIT(&->a_rwlock);
return (ENOMEM);
}
(void) anon_set_ptr(amp->ahp, anon_index, ap, ANON_SLEEP);
page_unlock(pp);
}
anon_array_exit(&cookie);
ANON_LOCK_EXIT(&->a_rwlock);
memidp->val[0] = (uintptr_t)ap;
memidp->val[1] = (uintptr_t)addr & PAGEOFFSET;
return (0);
}
/*
* Get memory allocation policy info for specified address in given segment
*/
static lgrp_mem_policy_info_t *
segspt_shmgetpolicy(struct seg *seg, caddr_t addr)
{
struct anon_map *amp;
ulong_t anon_index;
lgrp_mem_policy_info_t *policy_info;
struct shm_data *shm_data;
ASSERT(seg != NULL);
/*
* Get anon_map from segshm
*
* Assume that no lock needs to be held on anon_map, since
* it should be protected by its reference count which must be
* nonzero for an existing segment
* Need to grab readers lock on policy tree though
*/
shm_data = (struct shm_data *)seg->s_data;
if (shm_data == NULL)
return (NULL);
amp = shm_data->shm_amp;
ASSERT(amp->refcnt != 0);
/*
* Get policy info
*
* Assume starting anon index of 0
*/
anon_index = seg_page(seg, addr);
policy_info = lgrp_shm_policy_get(amp, anon_index, NULL, 0);
return (policy_info);
}
/*ARGSUSED*/
static int
segspt_shmcapable(struct seg *seg, segcapability_t capability)
{
return (0);
}
|