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
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
|
From davej Wed Aug 3 15:16:11 2011
From oleg@redhat.com Mon Nov 21 15:06:14 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:14 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 9A1F8D50F1;
Mon, 21 Nov 2011 15:06:14 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id gJzpul4rDNnA; Mon, 21 Nov 2011 15:06:14 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7DD4BD50EA;
Mon, 21 Nov 2011 15:06:14 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6BJJ022074;
Mon, 21 Nov 2011 15:06:12 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:32 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:30 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 01/33] utrace core
Message-ID: <20111121200130.GA27756@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 139951
Lines: 3867
From: Roland McGrath <roland@redhat.com>
This adds the utrace facility, a new modular interface in the kernel for
implementing user thread tracing and debugging. This fits on top of the
tracehook_* layer, so the new code is well-isolated.
The new interface is in <linux/utrace.h> and the DocBook utrace book
describes it. It allows for multiple separate tracing engines to work in
parallel without interfering with each other. Higher-level tracing
facilities can be implemented as loadable kernel modules using this layer.
The new facility is made optional under CONFIG_UTRACE.
When this is not enabled, no new code is added.
It can only be enabled on machines that have all the
prerequisites and select CONFIG_HAVE_ARCH_TRACEHOOK.
In this initial version, utrace and ptrace do not play together at all,
the next patches try to fix this.
This is is same/old utrace-core patch except:
- use task->jobctl/JOBCTL_STOP_DEQUEUED instead of removed
signal->flags/SIGNAL_STOP_DEQUEUED in utrace_get_signal()
- do not include the tracehook changes, this comes with the
next patches
Signed-off-by: Roland McGrath <roland@redhat.com>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
Documentation/DocBook/Makefile | 2 +-
Documentation/DocBook/utrace.tmpl | 589 +++++++++
fs/proc/array.c | 3 +
include/linux/sched.h | 5 +
include/linux/utrace.h | 692 +++++++++++
init/Kconfig | 9 +
kernel/Makefile | 1 +
kernel/utrace.c | 2440 +++++++++++++++++++++++++++++++++++++
8 files changed, 3740 insertions(+), 1 deletions(-)
create mode 100644 Documentation/DocBook/utrace.tmpl
create mode 100644 include/linux/utrace.h
create mode 100644 kernel/utrace.c
diff --git a/Documentation/DocBook/Makefile b/Documentation/DocBook/Makefile
index 66725a3..08ed954 100644
--- a/Documentation/DocBook/Makefile
+++ b/Documentation/DocBook/Makefile
@@ -14,7 +14,7 @@ DOCBOOKS := z8530book.xml mcabook.xml device-drivers.xml \
genericirq.xml s390-drivers.xml uio-howto.xml scsi.xml \
80211.xml debugobjects.xml sh.xml regulator.xml \
alsa-driver-api.xml writing-an-alsa-driver.xml \
- tracepoint.xml drm.xml media_api.xml
+ tracepoint.xml utrace.xml drm.xml media_api.xml
include $(srctree)/Documentation/DocBook/media/Makefile
diff --git a/Documentation/DocBook/utrace.tmpl b/Documentation/DocBook/utrace.tmpl
new file mode 100644
index 0000000..0c40add
--- /dev/null
+++ b/Documentation/DocBook/utrace.tmpl
@@ -0,0 +1,589 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
+"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
+
+<book id="utrace">
+ <bookinfo>
+ <title>The utrace User Debugging Infrastructure</title>
+ </bookinfo>
+
+ <toc></toc>
+
+ <chapter id="concepts"><title>utrace concepts</title>
+
+ <sect1 id="intro"><title>Introduction</title>
+
+ <para>
+ <application>utrace</application> is infrastructure code for tracing
+ and controlling user threads. This is the foundation for writing
+ tracing engines, which can be loadable kernel modules.
+ </para>
+
+ <para>
+ The basic actors in <application>utrace</application> are the thread
+ and the tracing engine. A tracing engine is some body of code that
+ calls into the <filename><linux/utrace.h></filename>
+ interfaces, represented by a <structname>struct
+ utrace_engine_ops</structname>. (Usually it's a kernel module,
+ though the legacy <function>ptrace</function> support is a tracing
+ engine that is not in a kernel module.) The interface operates on
+ individual threads (<structname>struct task_struct</structname>).
+ If an engine wants to treat several threads as a group, that is up
+ to its higher-level code.
+ </para>
+
+ <para>
+ Tracing begins by attaching an engine to a thread, using
+ <function>utrace_attach_task</function> or
+ <function>utrace_attach_pid</function>. If successful, it returns a
+ pointer that is the handle used in all other calls.
+ </para>
+
+ </sect1>
+
+ <sect1 id="callbacks"><title>Events and Callbacks</title>
+
+ <para>
+ An attached engine does nothing by default. An engine makes something
+ happen by requesting callbacks via <function>utrace_set_events</function>
+ and poking the thread with <function>utrace_control</function>.
+ The synchronization issues related to these two calls
+ are discussed further below in <xref linkend="teardown"/>.
+ </para>
+
+ <para>
+ Events are specified using the macro
+ <constant>UTRACE_EVENT(<replaceable>type</replaceable>)</constant>.
+ Each event type is associated with a callback in <structname>struct
+ utrace_engine_ops</structname>. A tracing engine can leave unused
+ callbacks <constant>NULL</constant>. The only callbacks required
+ are those used by the event flags it sets.
+ </para>
+
+ <para>
+ Many engines can be attached to each thread. When a thread has an
+ event, each engine gets a callback if it has set the event flag for
+ that event type. For most events, engines are called in the order they
+ attached. Engines that attach after the event has occurred do not get
+ callbacks for that event. This includes any new engines just attached
+ by an existing engine's callback function. Once the sequence of
+ callbacks for that one event has completed, such new engines are then
+ eligible in the next sequence that starts when there is another event.
+ </para>
+
+ <para>
+ Event reporting callbacks have details particular to the event type,
+ but are all called in similar environments and have the same
+ constraints. Callbacks are made from safe points, where no locks
+ are held, no special resources are pinned (usually), and the
+ user-mode state of the thread is accessible. So, callback code has
+ a pretty free hand. But to be a good citizen, callback code should
+ never block for long periods. It is fine to block in
+ <function>kmalloc</function> and the like, but never wait for i/o or
+ for user mode to do something. If you need the thread to wait, use
+ <constant>UTRACE_STOP</constant> and return from the callback
+ quickly. When your i/o finishes or whatever, you can use
+ <function>utrace_control</function> to resume the thread.
+ </para>
+
+ <para>
+ The <constant>UTRACE_EVENT(SYSCALL_ENTRY)</constant> event is a special
+ case. While other events happen in the kernel when it will return to
+ user mode soon, this event happens when entering the kernel before it
+ will proceed with the work requested from user mode. Because of this
+ difference, the <function>report_syscall_entry</function> callback is
+ special in two ways. For this event, engines are called in reverse of
+ the normal order (this includes the <function>report_quiesce</function>
+ call that precedes a <function>report_syscall_entry</function> call).
+ This preserves the semantics that the last engine to attach is called
+ "closest to user mode"--the engine that is first to see a thread's user
+ state when it enters the kernel is also the last to see that state when
+ the thread returns to user mode. For the same reason, if these
+ callbacks use <constant>UTRACE_STOP</constant> (see the next section),
+ the thread stops immediately after callbacks rather than only when it's
+ ready to return to user mode; when allowed to resume, it will actually
+ attempt the system call indicated by the register values at that time.
+ </para>
+
+ </sect1>
+
+ <sect1 id="safely"><title>Stopping Safely</title>
+
+ <sect2 id="well-behaved"><title>Writing well-behaved callbacks</title>
+
+ <para>
+ Well-behaved callbacks are important to maintain two essential
+ properties of the interface. The first of these is that unrelated
+ tracing engines should not interfere with each other. If your engine's
+ event callback does not return quickly, then another engine won't get
+ the event notification in a timely manner. The second important
+ property is that tracing should be as noninvasive as possible to the
+ normal operation of the system overall and of the traced thread in
+ particular. That is, attached tracing engines should not perturb a
+ thread's behavior, except to the extent that changing its user-visible
+ state is explicitly what you want to do. (Obviously some perturbation
+ is unavoidable, primarily timing changes, ranging from small delays due
+ to the overhead of tracing, to arbitrary pauses in user code execution
+ when a user stops a thread with a debugger for examination.) Even when
+ you explicitly want the perturbation of making the traced thread block,
+ just blocking directly in your callback has more unwanted effects. For
+ example, the <constant>CLONE</constant> event callbacks are called when
+ the new child thread has been created but not yet started running; the
+ child can never be scheduled until the <constant>CLONE</constant>
+ tracing callbacks return. (This allows engines tracing the parent to
+ attach to the child.) If a <constant>CLONE</constant> event callback
+ blocks the parent thread, it also prevents the child thread from
+ running (even to process a <constant>SIGKILL</constant>). If what you
+ want is to make both the parent and child block, then use
+ <function>utrace_attach_task</function> on the child and then use
+ <constant>UTRACE_STOP</constant> on both threads. A more crucial
+ problem with blocking in callbacks is that it can prevent
+ <constant>SIGKILL</constant> from working. A thread that is blocking
+ due to <constant>UTRACE_STOP</constant> will still wake up and die
+ immediately when sent a <constant>SIGKILL</constant>, as all threads
+ should. Relying on the <application>utrace</application>
+ infrastructure rather than on private synchronization calls in event
+ callbacks is an important way to help keep tracing robustly
+ noninvasive.
+ </para>
+
+ </sect2>
+
+ <sect2 id="UTRACE_STOP"><title>Using <constant>UTRACE_STOP</constant></title>
+
+ <para>
+ To control another thread and access its state, it must be stopped
+ with <constant>UTRACE_STOP</constant>. This means that it is
+ stopped and won't start running again while we access it. When a
+ thread is not already stopped, <function>utrace_control</function>
+ returns <constant>-EINPROGRESS</constant> and an engine must wait
+ for an event callback when the thread is ready to stop. The thread
+ may be running on another CPU or may be blocked. When it is ready
+ to be examined, it will make callbacks to engines that set the
+ <constant>UTRACE_EVENT(QUIESCE)</constant> event bit. To wake up an
+ interruptible wait, use <constant>UTRACE_INTERRUPT</constant>.
+ </para>
+
+ <para>
+ As long as some engine has used <constant>UTRACE_STOP</constant> and
+ not called <function>utrace_control</function> to resume the thread,
+ then the thread will remain stopped. <constant>SIGKILL</constant>
+ will wake it up, but it will not run user code. When the stop is
+ cleared with <function>utrace_control</function> or a callback
+ return value, the thread starts running again.
+ (See also <xref linkend="teardown"/>.)
+ </para>
+
+ </sect2>
+
+ </sect1>
+
+ <sect1 id="teardown"><title>Tear-down Races</title>
+
+ <sect2 id="SIGKILL"><title>Primacy of <constant>SIGKILL</constant></title>
+ <para>
+ Ordinarily synchronization issues for tracing engines are kept fairly
+ straightforward by using <constant>UTRACE_STOP</constant>. You ask a
+ thread to stop, and then once it makes the
+ <function>report_quiesce</function> callback it cannot do anything else
+ that would result in another callback, until you let it with a
+ <function>utrace_control</function> call. This simple arrangement
+ avoids complex and error-prone code in each one of a tracing engine's
+ event callbacks to keep them serialized with the engine's other
+ operations done on that thread from another thread of control.
+ However, giving tracing engines complete power to keep a traced thread
+ stuck in place runs afoul of a more important kind of simplicity that
+ the kernel overall guarantees: nothing can prevent or delay
+ <constant>SIGKILL</constant> from making a thread die and release its
+ resources. To preserve this important property of
+ <constant>SIGKILL</constant>, it as a special case can break
+ <constant>UTRACE_STOP</constant> like nothing else normally can. This
+ includes both explicit <constant>SIGKILL</constant> signals and the
+ implicit <constant>SIGKILL</constant> sent to each other thread in the
+ same thread group by a thread doing an exec, or processing a fatal
+ signal, or making an <function>exit_group</function> system call. A
+ tracing engine can prevent a thread from beginning the exit or exec or
+ dying by signal (other than <constant>SIGKILL</constant>) if it is
+ attached to that thread, but once the operation begins, no tracing
+ engine can prevent or delay all other threads in the same thread group
+ dying.
+ </para>
+ </sect2>
+
+ <sect2 id="reap"><title>Final callbacks</title>
+ <para>
+ The <function>report_reap</function> callback is always the final event
+ in the life cycle of a traced thread. Tracing engines can use this as
+ the trigger to clean up their own data structures. The
+ <function>report_death</function> callback is always the penultimate
+ event a tracing engine might see; it's seen unless the thread was
+ already in the midst of dying when the engine attached. Many tracing
+ engines will have no interest in when a parent reaps a dead process,
+ and nothing they want to do with a zombie thread once it dies; for
+ them, the <function>report_death</function> callback is the natural
+ place to clean up data structures and detach. To facilitate writing
+ such engines robustly, given the asynchrony of
+ <constant>SIGKILL</constant>, and without error-prone manual
+ implementation of synchronization schemes, the
+ <application>utrace</application> infrastructure provides some special
+ guarantees about the <function>report_death</function> and
+ <function>report_reap</function> callbacks. It still takes some care
+ to be sure your tracing engine is robust to tear-down races, but these
+ rules make it reasonably straightforward and concise to handle a lot of
+ corner cases correctly.
+ </para>
+ </sect2>
+
+ <sect2 id="refcount"><title>Engine and task pointers</title>
+ <para>
+ The first sort of guarantee concerns the core data structures
+ themselves. <structname>struct utrace_engine</structname> is
+ a reference-counted data structure. While you hold a reference, an
+ engine pointer will always stay valid so that you can safely pass it to
+ any <application>utrace</application> call. Each call to
+ <function>utrace_attach_task</function> or
+ <function>utrace_attach_pid</function> returns an engine pointer with a
+ reference belonging to the caller. You own that reference until you
+ drop it using <function>utrace_engine_put</function>. There is an
+ implicit reference on the engine while it is attached. So if you drop
+ your only reference, and then use
+ <function>utrace_attach_task</function> without
+ <constant>UTRACE_ATTACH_CREATE</constant> to look up that same engine,
+ you will get the same pointer with a new reference to replace the one
+ you dropped, just like calling <function>utrace_engine_get</function>.
+ When an engine has been detached, either explicitly with
+ <constant>UTRACE_DETACH</constant> or implicitly after
+ <function>report_reap</function>, then any references you hold are all
+ that keep the old engine pointer alive.
+ </para>
+
+ <para>
+ There is nothing a kernel module can do to keep a <structname>struct
+ task_struct</structname> alive outside of
+ <function>rcu_read_lock</function>. When the task dies and is reaped
+ by its parent (or itself), that structure can be freed so that any
+ dangling pointers you have stored become invalid.
+ <application>utrace</application> will not prevent this, but it can
+ help you detect it safely. By definition, a task that has been reaped
+ has had all its engines detached. All
+ <application>utrace</application> calls can be safely called on a
+ detached engine if the caller holds a reference on that engine pointer,
+ even if the task pointer passed in the call is invalid. All calls
+ return <constant>-ESRCH</constant> for a detached engine, which tells
+ you that the task pointer you passed could be invalid now. Since
+ <function>utrace_control</function> and
+ <function>utrace_set_events</function> do not block, you can call those
+ inside a <function>rcu_read_lock</function> section and be sure after
+ they don't return <constant>-ESRCH</constant> that the task pointer is
+ still valid until <function>rcu_read_unlock</function>. The
+ infrastructure never holds task references of its own. Though neither
+ <function>rcu_read_lock</function> nor any other lock is held while
+ making a callback, it's always guaranteed that the <structname>struct
+ task_struct</structname> and the <structname>struct
+ utrace_engine</structname> passed as arguments remain valid
+ until the callback function returns.
+ </para>
+
+ <para>
+ The common means for safely holding task pointers that is available to
+ kernel modules is to use <structname>struct pid</structname>, which
+ permits <function>put_pid</function> from kernel modules. When using
+ that, the calls <function>utrace_attach_pid</function>,
+ <function>utrace_control_pid</function>,
+ <function>utrace_set_events_pid</function>, and
+ <function>utrace_barrier_pid</function> are available.
+ </para>
+ </sect2>
+
+ <sect2 id="reap-after-death">
+ <title>
+ Serialization of <constant>DEATH</constant> and <constant>REAP</constant>
+ </title>
+ <para>
+ The second guarantee is the serialization of
+ <constant>DEATH</constant> and <constant>REAP</constant> event
+ callbacks for a given thread. The actual reaping by the parent
+ (<function>release_task</function> call) can occur simultaneously
+ while the thread is still doing the final steps of dying, including
+ the <function>report_death</function> callback. If a tracing engine
+ has requested both <constant>DEATH</constant> and
+ <constant>REAP</constant> event reports, it's guaranteed that the
+ <function>report_reap</function> callback will not be made until
+ after the <function>report_death</function> callback has returned.
+ If the <function>report_death</function> callback itself detaches
+ from the thread, then the <function>report_reap</function> callback
+ will never be made. Thus it is safe for a
+ <function>report_death</function> callback to clean up data
+ structures and detach.
+ </para>
+ </sect2>
+
+ <sect2 id="interlock"><title>Interlock with final callbacks</title>
+ <para>
+ The final sort of guarantee is that a tracing engine will know for sure
+ whether or not the <function>report_death</function> and/or
+ <function>report_reap</function> callbacks will be made for a certain
+ thread. These tear-down races are disambiguated by the error return
+ values of <function>utrace_set_events</function> and
+ <function>utrace_control</function>. Normally
+ <function>utrace_control</function> called with
+ <constant>UTRACE_DETACH</constant> returns zero, and this means that no
+ more callbacks will be made. If the thread is in the midst of dying,
+ it returns <constant>-EALREADY</constant> to indicate that the
+ <constant>report_death</constant> callback may already be in progress;
+ when you get this error, you know that any cleanup your
+ <function>report_death</function> callback does is about to happen or
+ has just happened--note that if the <function>report_death</function>
+ callback does not detach, the engine remains attached until the thread
+ gets reaped. If the thread is in the midst of being reaped,
+ <function>utrace_control</function> returns <constant>-ESRCH</constant>
+ to indicate that the <function>report_reap</function> callback may
+ already be in progress; this means the engine is implicitly detached
+ when the callback completes. This makes it possible for a tracing
+ engine that has decided asynchronously to detach from a thread to
+ safely clean up its data structures, knowing that no
+ <function>report_death</function> or <function>report_reap</function>
+ callback will try to do the same. <constant>utrace_detach</constant>
+ returns <constant>-ESRCH</constant> when the <structname>struct
+ utrace_engine</structname> has already been detached, but is
+ still a valid pointer because of its reference count. A tracing engine
+ can use this to safely synchronize its own independent multiple threads
+ of control with each other and with its event callbacks that detach.
+ </para>
+
+ <para>
+ In the same vein, <function>utrace_set_events</function> normally
+ returns zero; if the target thread was stopped before the call, then
+ after a successful call, no event callbacks not requested in the new
+ flags will be made. It fails with <constant>-EALREADY</constant> if
+ you try to clear <constant>UTRACE_EVENT(DEATH)</constant> when the
+ <function>report_death</function> callback may already have begun, or if
+ you try to newly set <constant>UTRACE_EVENT(DEATH)</constant> or
+ <constant>UTRACE_EVENT(QUIESCE)</constant> when the target is already
+ dead or dying. Like <function>utrace_control</function>, it returns
+ <constant>-ESRCH</constant> when the <function>report_reap</function>
+ callback may already have begun, or the thread has already been detached
+ (including forcible detach on reaping). This lets the tracing engine
+ know for sure which event callbacks it will or won't see after
+ <function>utrace_set_events</function> has returned. By checking for
+ errors, it can know whether to clean up its data structures immediately
+ or to let its callbacks do the work.
+ </para>
+ </sect2>
+
+ <sect2 id="barrier"><title>Using <function>utrace_barrier</function></title>
+ <para>
+ When a thread is safely stopped, calling
+ <function>utrace_control</function> with <constant>UTRACE_DETACH</constant>
+ or calling <function>utrace_set_events</function> to disable some events
+ ensures synchronously that your engine won't get any more of the callbacks
+ that have been disabled (none at all when detaching). But these can also
+ be used while the thread is not stopped, when it might be simultaneously
+ making a callback to your engine. For this situation, these calls return
+ <constant>-EINPROGRESS</constant> when it's possible a callback is in
+ progress. If you are not prepared to have your old callbacks still run,
+ then you can synchronize to be sure all the old callbacks are finished,
+ using <function>utrace_barrier</function>. This is necessary if the
+ kernel module containing your callback code is going to be unloaded.
+ </para>
+ <para>
+ After using <constant>UTRACE_DETACH</constant> once, further calls to
+ <function>utrace_control</function> with the same engine pointer will
+ return <constant>-ESRCH</constant>. In contrast, after getting
+ <constant>-EINPROGRESS</constant> from
+ <function>utrace_set_events</function>, you can call
+ <function>utrace_set_events</function> again later and if it returns zero
+ then know the old callbacks have finished.
+ </para>
+ <para>
+ Unlike all other calls, <function>utrace_barrier</function> (and
+ <function>utrace_barrier_pid</function>) will accept any engine pointer you
+ hold a reference on, even if <constant>UTRACE_DETACH</constant> has already
+ been used. After any <function>utrace_control</function> or
+ <function>utrace_set_events</function> call (these do not block), you can
+ call <function>utrace_barrier</function> to block until callbacks have
+ finished. This returns <constant>-ESRCH</constant> only if the engine is
+ completely detached (finished all callbacks). Otherwise it waits
+ until the thread is definitely not in the midst of a callback to this
+ engine and then returns zero, but can return
+ <constant>-ERESTARTSYS</constant> if its wait is interrupted.
+ </para>
+ </sect2>
+
+</sect1>
+
+</chapter>
+
+<chapter id="core"><title>utrace core API</title>
+
+<para>
+ The utrace API is declared in <filename><linux/utrace.h></filename>.
+</para>
+
+!Iinclude/linux/utrace.h
+!Ekernel/utrace.c
+
+</chapter>
+
+<chapter id="machine"><title>Machine State</title>
+
+<para>
+ The <function>task_current_syscall</function> function can be used on any
+ valid <structname>struct task_struct</structname> at any time, and does
+ not even require that <function>utrace_attach_task</function> was used at all.
+</para>
+
+<para>
+ The other ways to access the registers and other machine-dependent state of
+ a task can only be used on a task that is at a known safe point. The safe
+ points are all the places where <function>utrace_set_events</function> can
+ request callbacks (except for the <constant>DEATH</constant> and
+ <constant>REAP</constant> events). So at any event callback, it is safe to
+ examine <varname>current</varname>.
+</para>
+
+<para>
+ One task can examine another only after a callback in the target task that
+ returns <constant>UTRACE_STOP</constant> so that task will not return to user
+ mode after the safe point. This guarantees that the task will not resume
+ until the same engine uses <function>utrace_control</function>, unless the
+ task dies suddenly. To examine safely, one must use a pair of calls to
+ <function>utrace_prepare_examine</function> and
+ <function>utrace_finish_examine</function> surrounding the calls to
+ <structname>struct user_regset</structname> functions or direct examination
+ of task data structures. <function>utrace_prepare_examine</function> returns
+ an error if the task is not properly stopped, or is dead. After a
+ successful examination, the paired <function>utrace_finish_examine</function>
+ call returns an error if the task ever woke up during the examination. If
+ so, any data gathered may be scrambled and should be discarded. This means
+ there was a spurious wake-up (which should not happen), or a sudden death.
+</para>
+
+<sect1 id="regset"><title><structname>struct user_regset</structname></title>
+
+<para>
+ The <structname>struct user_regset</structname> API
+ is declared in <filename><linux/regset.h></filename>.
+</para>
+
+!Finclude/linux/regset.h
+
+</sect1>
+
+<sect1 id="task_current_syscall">
+ <title><filename>System Call Information</filename></title>
+
+<para>
+ This function is declared in <filename><linux/ptrace.h></filename>.
+</para>
+
+!Elib/syscall.c
+
+</sect1>
+
+<sect1 id="syscall"><title><filename>System Call Tracing</filename></title>
+
+<para>
+ The arch API for system call information is declared in
+ <filename><asm/syscall.h></filename>.
+ Each of these calls can be used only at system call entry tracing,
+ or can be used only at system call exit and the subsequent safe points
+ before returning to user mode.
+ At system call entry tracing means either during a
+ <structfield>report_syscall_entry</structfield> callback,
+ or any time after that callback has returned <constant>UTRACE_STOP</constant>.
+</para>
+
+!Finclude/asm-generic/syscall.h
+
+</sect1>
+
+</chapter>
+
+<chapter id="internals"><title>Kernel Internals</title>
+
+<para>
+ This chapter covers the interface to the tracing infrastructure
+ from the core of the kernel and the architecture-specific code.
+ This is for maintainers of the kernel and arch code, and not relevant
+ to using the tracing facilities described in preceding chapters.
+</para>
+
+<sect1 id="tracehook"><title>Core Calls In</title>
+
+<para>
+ These calls are declared in <filename><linux/tracehook.h></filename>.
+ The core kernel calls these functions at various important places.
+</para>
+
+!Finclude/linux/tracehook.h
+
+</sect1>
+
+<sect1 id="arch"><title>Architecture Calls Out</title>
+
+<para>
+ An arch that has done all these things sets
+ <constant>CONFIG_HAVE_ARCH_TRACEHOOK</constant>.
+ This is required to enable the <application>utrace</application> code.
+</para>
+
+<sect2 id="arch-ptrace"><title><filename><asm/ptrace.h></filename></title>
+
+<para>
+ An arch defines these in <filename><asm/ptrace.h></filename>
+ if it supports hardware single-step or block-step features.
+</para>
+
+!Finclude/linux/ptrace.h arch_has_single_step arch_has_block_step
+!Finclude/linux/ptrace.h user_enable_single_step user_enable_block_step
+!Finclude/linux/ptrace.h user_disable_single_step
+
+</sect2>
+
+<sect2 id="arch-syscall">
+ <title><filename><asm/syscall.h></filename></title>
+
+ <para>
+ An arch provides <filename><asm/syscall.h></filename> that
+ defines these as inlines, or declares them as exported functions.
+ These interfaces are described in <xref linkend="syscall"/>.
+ </para>
+
+</sect2>
+
+<sect2 id="arch-tracehook">
+ <title><filename><linux/tracehook.h></filename></title>
+
+ <para>
+ An arch must define <constant>TIF_NOTIFY_RESUME</constant>
+ and <constant>TIF_SYSCALL_TRACE</constant>
+ in its <filename><asm/thread_info.h></filename>.
+ The arch code must call the following functions, all declared
+ in <filename><linux/tracehook.h></filename> and
+ described in <xref linkend="tracehook"/>:
+
+ <itemizedlist>
+ <listitem>
+ <para><function>tracehook_notify_resume</function></para>
+ </listitem>
+ <listitem>
+ <para><function>tracehook_report_syscall_entry</function></para>
+ </listitem>
+ <listitem>
+ <para><function>tracehook_report_syscall_exit</function></para>
+ </listitem>
+ <listitem>
+ <para><function>tracehook_signal_handler</function></para>
+ </listitem>
+ </itemizedlist>
+
+ </para>
+
+</sect2>
+
+</sect1>
+
+</chapter>
+
+</book>
diff --git a/fs/proc/array.c b/fs/proc/array.c
index 3a1dafd..f0c0ea2 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -81,6 +81,7 @@
#include <linux/pid_namespace.h>
#include <linux/ptrace.h>
#include <linux/tracehook.h>
+#include <linux/utrace.h>
#include <asm/pgtable.h>
#include <asm/processor.h>
@@ -192,6 +193,8 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
cred->uid, cred->euid, cred->suid, cred->fsuid,
cred->gid, cred->egid, cred->sgid, cred->fsgid);
+ task_utrace_proc_status(m, p);
+
task_lock(p);
if (p->files)
fdt = files_fdtable(p->files);
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 68daf4f..4d45f93 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1403,6 +1403,11 @@ struct task_struct {
#endif
seccomp_t seccomp;
+#ifdef CONFIG_UTRACE
+ struct utrace *utrace;
+ unsigned long utrace_flags;
+#endif
+
/* Thread group tracking */
u32 parent_exec_id;
u32 self_exec_id;
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
new file mode 100644
index 0000000..f251efe
--- /dev/null
+++ b/include/linux/utrace.h
@@ -0,0 +1,692 @@
+/*
+ * utrace infrastructure interface for debugging user processes
+ *
+ * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * Red Hat Author: Roland McGrath.
+ *
+ * This interface allows for notification of interesting events in a
+ * thread. It also mediates access to thread state such as registers.
+ * Multiple unrelated users can be associated with a single thread.
+ * We call each of these a tracing engine.
+ *
+ * A tracing engine starts by calling utrace_attach_task() or
+ * utrace_attach_pid() on the chosen thread, passing in a set of hooks
+ * (&struct utrace_engine_ops), and some associated data. This produces a
+ * &struct utrace_engine, which is the handle used for all other
+ * operations. An attached engine has its ops vector, its data, and an
+ * event mask controlled by utrace_set_events().
+ *
+ * For each event bit that is set, that engine will get the
+ * appropriate ops->report_*() callback when the event occurs. The
+ * &struct utrace_engine_ops need not provide callbacks for an event
+ * unless the engine sets one of the associated event bits.
+ */
+
+#ifndef _LINUX_UTRACE_H
+#define _LINUX_UTRACE_H 1
+
+#include <linux/list.h>
+#include <linux/kref.h>
+#include <linux/signal.h>
+#include <linux/sched.h>
+
+struct linux_binprm;
+struct pt_regs;
+struct utrace;
+struct user_regset;
+struct user_regset_view;
+
+/*
+ * Event bits passed to utrace_set_events().
+ * These appear in &struct task_struct.@utrace_flags
+ * and &struct utrace_engine.@flags.
+ */
+enum utrace_events {
+ _UTRACE_EVENT_QUIESCE, /* Thread is available for examination. */
+ _UTRACE_EVENT_REAP, /* Zombie reaped, no more tracing possible. */
+ _UTRACE_EVENT_CLONE, /* Successful clone/fork/vfork just done. */
+ _UTRACE_EVENT_EXEC, /* Successful execve just completed. */
+ _UTRACE_EVENT_EXIT, /* Thread exit in progress. */
+ _UTRACE_EVENT_DEATH, /* Thread has died. */
+ _UTRACE_EVENT_SYSCALL_ENTRY, /* User entered kernel for system call. */
+ _UTRACE_EVENT_SYSCALL_EXIT, /* Returning to user after system call. */
+ _UTRACE_EVENT_SIGNAL, /* Signal delivery will run a user handler. */
+ _UTRACE_EVENT_SIGNAL_IGN, /* No-op signal to be delivered. */
+ _UTRACE_EVENT_SIGNAL_STOP, /* Signal delivery will suspend. */
+ _UTRACE_EVENT_SIGNAL_TERM, /* Signal delivery will terminate. */
+ _UTRACE_EVENT_SIGNAL_CORE, /* Signal delivery will dump core. */
+ _UTRACE_EVENT_JCTL, /* Job control stop or continue completed. */
+ _UTRACE_NEVENTS
+};
+#define UTRACE_EVENT(type) (1UL << _UTRACE_EVENT_##type)
+
+/*
+ * All the kinds of signal events.
+ * These all use the @report_signal() callback.
+ */
+#define UTRACE_EVENT_SIGNAL_ALL (UTRACE_EVENT(SIGNAL) \
+ | UTRACE_EVENT(SIGNAL_IGN) \
+ | UTRACE_EVENT(SIGNAL_STOP) \
+ | UTRACE_EVENT(SIGNAL_TERM) \
+ | UTRACE_EVENT(SIGNAL_CORE))
+/*
+ * Both kinds of syscall events; these call the @report_syscall_entry()
+ * and @report_syscall_exit() callbacks, respectively.
+ */
+#define UTRACE_EVENT_SYSCALL \
+ (UTRACE_EVENT(SYSCALL_ENTRY) | UTRACE_EVENT(SYSCALL_EXIT))
+
+/*
+ * The event reports triggered synchronously by task death.
+ */
+#define _UTRACE_DEATH_EVENTS (UTRACE_EVENT(DEATH) | UTRACE_EVENT(QUIESCE))
+
+/*
+ * Hooks in <linux/tracehook.h> call these entry points to the utrace dispatch.
+ */
+void utrace_free_task(struct task_struct *);
+bool utrace_interrupt_pending(void);
+void utrace_resume(struct task_struct *, struct pt_regs *);
+void utrace_finish_stop(void);
+void utrace_maybe_reap(struct task_struct *, struct utrace *, bool);
+int utrace_get_signal(struct task_struct *, struct pt_regs *,
+ siginfo_t *, struct k_sigaction *);
+void utrace_report_clone(unsigned long, struct task_struct *);
+void utrace_finish_vfork(struct task_struct *);
+void utrace_report_exit(long *exit_code);
+void utrace_report_death(struct task_struct *, struct utrace *, bool, int);
+void utrace_report_jctl(int notify, int type);
+void utrace_report_exec(struct linux_binfmt *, struct linux_binprm *,
+ struct pt_regs *regs);
+bool utrace_report_syscall_entry(struct pt_regs *);
+void utrace_report_syscall_exit(struct pt_regs *);
+void utrace_signal_handler(struct task_struct *, int);
+
+#ifndef CONFIG_UTRACE
+
+/*
+ * <linux/tracehook.h> uses these accessors to avoid #ifdef CONFIG_UTRACE.
+ */
+static inline unsigned long task_utrace_flags(struct task_struct *task)
+{
+ return 0;
+}
+static inline struct utrace *task_utrace_struct(struct task_struct *task)
+{
+ return NULL;
+}
+static inline void utrace_init_task(struct task_struct *child)
+{
+}
+
+static inline void task_utrace_proc_status(struct seq_file *m,
+ struct task_struct *p)
+{
+}
+
+#else /* CONFIG_UTRACE */
+
+static inline unsigned long task_utrace_flags(struct task_struct *task)
+{
+ return task->utrace_flags;
+}
+
+static inline struct utrace *task_utrace_struct(struct task_struct *task)
+{
+ struct utrace *utrace;
+
+ /*
+ * This barrier ensures that any prior load of task->utrace_flags
+ * is ordered before this load of task->utrace. We use those
+ * utrace_flags checks in the hot path to decide to call into
+ * the utrace code. The first attach installs task->utrace before
+ * setting task->utrace_flags nonzero with implicit barrier in
+ * between, see utrace_add_engine().
+ */
+ smp_rmb();
+ utrace = task->utrace;
+
+ smp_read_barrier_depends(); /* See utrace_task_alloc(). */
+ return utrace;
+}
+
+static inline void utrace_init_task(struct task_struct *task)
+{
+ task->utrace_flags = 0;
+ task->utrace = NULL;
+}
+
+void task_utrace_proc_status(struct seq_file *m, struct task_struct *p);
+
+
+/*
+ * Version number of the API defined in this file. This will change
+ * whenever a tracing engine's code would need some updates to keep
+ * working. We maintain this here for the benefit of tracing engine code
+ * that is developed concurrently with utrace API improvements before they
+ * are merged into the kernel, making LINUX_VERSION_CODE checks unwieldy.
+ */
+#define UTRACE_API_VERSION 20091216
+
+/**
+ * enum utrace_resume_action - engine's choice of action for a traced task
+ * @UTRACE_STOP: Stay quiescent after callbacks.
+ * @UTRACE_INTERRUPT: Make @report_signal() callback soon.
+ * @UTRACE_REPORT: Make some callback soon.
+ * @UTRACE_SINGLESTEP: Resume in user mode for one instruction.
+ * @UTRACE_BLOCKSTEP: Resume in user mode until next branch.
+ * @UTRACE_RESUME: Resume normally in user mode.
+ * @UTRACE_DETACH: Detach my engine (implies %UTRACE_RESUME).
+ *
+ * See utrace_control() for detailed descriptions of each action. This is
+ * encoded in the @action argument and the return value for every callback
+ * with a &u32 return value.
+ *
+ * The order of these is important. When there is more than one engine,
+ * each supplies its choice and the smallest value prevails.
+ */
+enum utrace_resume_action {
+ UTRACE_STOP,
+ UTRACE_INTERRUPT,
+ UTRACE_REPORT,
+ UTRACE_SINGLESTEP,
+ UTRACE_BLOCKSTEP,
+ UTRACE_RESUME,
+ UTRACE_DETACH,
+ UTRACE_RESUME_MAX
+};
+#define UTRACE_RESUME_BITS (ilog2(UTRACE_RESUME_MAX) + 1)
+#define UTRACE_RESUME_MASK ((1 << UTRACE_RESUME_BITS) - 1)
+
+/**
+ * utrace_resume_action - &enum utrace_resume_action from callback action
+ * @action: &u32 callback @action argument or return value
+ *
+ * This extracts the &enum utrace_resume_action from @action,
+ * which is the @action argument to a &struct utrace_engine_ops
+ * callback or the return value from one.
+ */
+static inline enum utrace_resume_action utrace_resume_action(u32 action)
+{
+ return action & UTRACE_RESUME_MASK;
+}
+
+/**
+ * enum utrace_signal_action - disposition of signal
+ * @UTRACE_SIGNAL_DELIVER: Deliver according to sigaction.
+ * @UTRACE_SIGNAL_IGN: Ignore the signal.
+ * @UTRACE_SIGNAL_TERM: Terminate the process.
+ * @UTRACE_SIGNAL_CORE: Terminate with core dump.
+ * @UTRACE_SIGNAL_STOP: Deliver as absolute stop.
+ * @UTRACE_SIGNAL_TSTP: Deliver as job control stop.
+ * @UTRACE_SIGNAL_REPORT: Reporting before pending signals.
+ * @UTRACE_SIGNAL_HANDLER: Reporting after signal handler setup.
+ *
+ * This is encoded in the @action argument and the return value for
+ * a @report_signal() callback. It says what will happen to the
+ * signal described by the &siginfo_t parameter to the callback.
+ *
+ * The %UTRACE_SIGNAL_REPORT value is used in an @action argument when
+ * a tracing report is being made before dequeuing any pending signal.
+ * If this is immediately after a signal handler has been set up, then
+ * %UTRACE_SIGNAL_HANDLER is used instead. A @report_signal callback
+ * that uses %UTRACE_SIGNAL_DELIVER|%UTRACE_SINGLESTEP will ensure
+ * it sees a %UTRACE_SIGNAL_HANDLER report.
+ */
+enum utrace_signal_action {
+ UTRACE_SIGNAL_DELIVER = 0x00,
+ UTRACE_SIGNAL_IGN = 0x10,
+ UTRACE_SIGNAL_TERM = 0x20,
+ UTRACE_SIGNAL_CORE = 0x30,
+ UTRACE_SIGNAL_STOP = 0x40,
+ UTRACE_SIGNAL_TSTP = 0x50,
+ UTRACE_SIGNAL_REPORT = 0x60,
+ UTRACE_SIGNAL_HANDLER = 0x70
+};
+#define UTRACE_SIGNAL_MASK 0xf0
+#define UTRACE_SIGNAL_HOLD 0x100 /* Flag, push signal back on queue. */
+
+/**
+ * utrace_signal_action - &enum utrace_signal_action from callback action
+ * @action: @report_signal callback @action argument or return value
+ *
+ * This extracts the &enum utrace_signal_action from @action, which
+ * is the @action argument to a @report_signal callback or the
+ * return value from one.
+ */
+static inline enum utrace_signal_action utrace_signal_action(u32 action)
+{
+ return action & UTRACE_SIGNAL_MASK;
+}
+
+/**
+ * enum utrace_syscall_action - disposition of system call attempt
+ * @UTRACE_SYSCALL_RUN: Run the system call.
+ * @UTRACE_SYSCALL_ABORT: Don't run the system call.
+ *
+ * This is encoded in the @action argument and the return value for
+ * a @report_syscall_entry callback.
+ */
+enum utrace_syscall_action {
+ UTRACE_SYSCALL_RUN = 0x00,
+ UTRACE_SYSCALL_ABORT = 0x10
+};
+#define UTRACE_SYSCALL_MASK 0xf0
+#define UTRACE_SYSCALL_RESUMED 0x100 /* Flag, report_syscall_entry() repeats */
+
+/**
+ * utrace_syscall_action - &enum utrace_syscall_action from callback action
+ * @action: @report_syscall_entry callback @action or return value
+ *
+ * This extracts the &enum utrace_syscall_action from @action, which
+ * is the @action argument to a @report_syscall_entry callback or the
+ * return value from one.
+ */
+static inline enum utrace_syscall_action utrace_syscall_action(u32 action)
+{
+ return action & UTRACE_SYSCALL_MASK;
+}
+
+/*
+ * Flags for utrace_attach_task() and utrace_attach_pid().
+ */
+#define UTRACE_ATTACH_MATCH_OPS 0x0001 /* Match engines on ops. */
+#define UTRACE_ATTACH_MATCH_DATA 0x0002 /* Match engines on data. */
+#define UTRACE_ATTACH_MATCH_MASK 0x000f
+#define UTRACE_ATTACH_CREATE 0x0010 /* Attach a new engine. */
+#define UTRACE_ATTACH_EXCLUSIVE 0x0020 /* Refuse if existing match. */
+
+/**
+ * struct utrace_engine - per-engine structure
+ * @ops: &struct utrace_engine_ops pointer passed to utrace_attach_task()
+ * @data: engine-private &void * passed to utrace_attach_task()
+ * @flags: event mask set by utrace_set_events() plus internal flag bits
+ *
+ * The task itself never has to worry about engines detaching while
+ * it's doing event callbacks. These structures are removed from the
+ * task's active list only when it's stopped, or by the task itself.
+ *
+ * utrace_engine_get() and utrace_engine_put() maintain a reference count.
+ * When it drops to zero, the structure is freed. One reference is held
+ * implicitly while the engine is attached to its task.
+ */
+struct utrace_engine {
+/* private: */
+ struct kref kref;
+ void (*release)(void *);
+ struct list_head entry;
+
+/* public: */
+ const struct utrace_engine_ops *ops;
+ void *data;
+
+ unsigned long flags;
+};
+
+/**
+ * utrace_engine_get - acquire a reference on a &struct utrace_engine
+ * @engine: &struct utrace_engine pointer
+ *
+ * You must hold a reference on @engine, and you get another.
+ */
+static inline void utrace_engine_get(struct utrace_engine *engine)
+{
+ kref_get(&engine->kref);
+}
+
+void __utrace_engine_release(struct kref *);
+
+/**
+ * utrace_engine_put - release a reference on a &struct utrace_engine
+ * @engine: &struct utrace_engine pointer
+ *
+ * You must hold a reference on @engine, and you lose that reference.
+ * If it was the last one, @engine becomes an invalid pointer.
+ */
+static inline void utrace_engine_put(struct utrace_engine *engine)
+{
+ kref_put(&engine->kref, __utrace_engine_release);
+}
+
+/**
+ * struct utrace_engine_ops - tracing engine callbacks
+ *
+ * Each @report_*() callback corresponds to an %UTRACE_EVENT(*) bit.
+ * utrace_set_events() calls on @engine choose which callbacks will
+ * be made to @engine from @task.
+ *
+ * Most callbacks take an @action argument, giving the resume action
+ * chosen by other tracing engines. All callbacks take an @engine
+ * argument. The @report_reap callback takes a @task argument that
+ * might or might not be @current. All other @report_* callbacks
+ * report an event in the @current task.
+ *
+ * For some calls, @action also includes bits specific to that event
+ * and utrace_resume_action() is used to extract the resume action.
+ * This shows what would happen if @engine wasn't there, or will if
+ * the callback's return value uses %UTRACE_RESUME. This always
+ * starts as %UTRACE_RESUME when no other tracing is being done on
+ * this task.
+ *
+ * All return values contain &enum utrace_resume_action bits. For
+ * some calls, other bits specific to that kind of event are added to
+ * the resume action bits with OR. These are the same bits used in
+ * the @action argument. The resume action returned by a callback
+ * does not override previous engines' choices, it only says what
+ * @engine wants done. What @current actually does is the action that's
+ * most constrained among the choices made by all attached engines.
+ * See utrace_control() for more information on the actions.
+ *
+ * When %UTRACE_STOP is used in @report_syscall_entry, then @current
+ * stops before attempting the system call. In this case, another
+ * @report_syscall_entry callback will follow after @current resumes if
+ * %UTRACE_REPORT or %UTRACE_INTERRUPT was returned by some callback
+ * or passed to utrace_control(). In a second or later callback,
+ * %UTRACE_SYSCALL_RESUMED is set in the @action argument to indicate
+ * a repeat callback still waiting to attempt the same system call
+ * invocation. This repeat callback gives each engine an opportunity
+ * to reexamine registers another engine might have changed while
+ * @current was held in %UTRACE_STOP.
+ *
+ * In other cases, the resume action does not take effect until @current
+ * is ready to check for signals and return to user mode. If there
+ * are more callbacks to be made, the last round of calls determines
+ * the final action. A @report_quiesce callback with @event zero, or
+ * a @report_signal callback, will always be the last one made before
+ * @current resumes. Only %UTRACE_STOP is "sticky"--if @engine returned
+ * %UTRACE_STOP then @current stays stopped unless @engine returns
+ * different from a following callback.
+ *
+ * The report_death() and report_reap() callbacks do not take @action
+ * arguments, and only %UTRACE_DETACH is meaningful in the return value
+ * from a report_death() callback. None of the resume actions applies
+ * to a dead thread.
+ *
+ * All @report_*() hooks are called with no locks held, in a generally
+ * safe environment when we will be returning to user mode soon (or just
+ * entered the kernel). It is fine to block for memory allocation and
+ * the like, but all hooks are asynchronous and must not block on
+ * external events! If you want the thread to block, use %UTRACE_STOP
+ * in your hook's return value; then later wake it up with utrace_control().
+ *
+ * @report_quiesce:
+ * Requested by %UTRACE_EVENT(%QUIESCE).
+ * This does not indicate any event, but just that @current is in a
+ * safe place for examination. This call is made before each specific
+ * event callback, except for @report_reap. The @event argument gives
+ * the %UTRACE_EVENT(@which) value for the event occurring. This
+ * callback might be made for events @engine has not requested, if
+ * some other engine is tracing the event; calling utrace_set_events()
+ * call here can request the immediate callback for this occurrence of
+ * @event. @event is zero when there is no other event, @current is
+ * now ready to check for signals and return to user mode, and some
+ * engine has used %UTRACE_REPORT or %UTRACE_INTERRUPT to request this
+ * callback. For this case, if @report_signal is not %NULL, the
+ * @report_quiesce callback may be replaced with a @report_signal
+ * callback passing %UTRACE_SIGNAL_REPORT in its @action argument,
+ * whenever @current is entering the signal-check path anyway.
+ *
+ * @report_signal:
+ * Requested by %UTRACE_EVENT(%SIGNAL_*) or %UTRACE_EVENT(%QUIESCE).
+ * Use utrace_signal_action() and utrace_resume_action() on @action.
+ * The signal action is %UTRACE_SIGNAL_REPORT when some engine has
+ * used %UTRACE_REPORT or %UTRACE_INTERRUPT; the callback can choose
+ * to stop or to deliver an artificial signal, before pending signals.
+ * It's %UTRACE_SIGNAL_HANDLER instead when signal handler setup just
+ * finished (after a previous %UTRACE_SIGNAL_DELIVER return); this
+ * serves in lieu of any %UTRACE_SIGNAL_REPORT callback requested by
+ * %UTRACE_REPORT or %UTRACE_INTERRUPT, and is also implicitly
+ * requested by %UTRACE_SINGLESTEP or %UTRACE_BLOCKSTEP into the
+ * signal delivery. The other signal actions indicate a signal about
+ * to be delivered; the previous engine's return value sets the signal
+ * action seen by the the following engine's callback. The @info data
+ * can be changed at will, including @info->si_signo. The settings in
+ * @return_ka determines what %UTRACE_SIGNAL_DELIVER does. @orig_ka
+ * is what was in force before other tracing engines intervened, and
+ * it's %NULL when this report began as %UTRACE_SIGNAL_REPORT or
+ * %UTRACE_SIGNAL_HANDLER. For a report without a new signal, @info
+ * is left uninitialized and must be set completely by an engine that
+ * chooses to deliver a signal; if there was a previous @report_signal
+ * callback ending in %UTRACE_STOP and it was just resumed using
+ * %UTRACE_REPORT or %UTRACE_INTERRUPT, then @info is left unchanged
+ * from the previous callback. In this way, the original signal can
+ * be left in @info while returning %UTRACE_STOP|%UTRACE_SIGNAL_IGN
+ * and then found again when resuming with %UTRACE_INTERRUPT.
+ * The %UTRACE_SIGNAL_HOLD flag bit can be OR'd into the return value,
+ * and might be in @action if the previous engine returned it. This
+ * flag asks that the signal in @info be pushed back on @current's queue
+ * so that it will be seen again after whatever action is taken now.
+ *
+ * @report_clone:
+ * Requested by %UTRACE_EVENT(%CLONE).
+ * Event reported for parent, before the new task @child might run.
+ * @clone_flags gives the flags used in the clone system call, or
+ * equivalent flags for a fork() or vfork() system call. This
+ * function can use utrace_attach_task() on @child. Then passing
+ * %UTRACE_STOP to utrace_control() on @child here keeps the child
+ * stopped before it ever runs in user mode, %UTRACE_REPORT or
+ * %UTRACE_INTERRUPT ensures a callback from @child before it
+ * starts in user mode.
+ *
+ * @report_jctl:
+ * Requested by %UTRACE_EVENT(%JCTL).
+ * Job control event; @type is %CLD_STOPPED or %CLD_CONTINUED,
+ * indicating whether we are stopping or resuming now. If @notify
+ * is nonzero, @current is the last thread to stop and so will send
+ * %SIGCHLD to its parent after this callback; @notify reflects
+ * what the parent's %SIGCHLD has in @si_code, which can sometimes
+ * be %CLD_STOPPED even when @type is %CLD_CONTINUED.
+ *
+ * @report_exec:
+ * Requested by %UTRACE_EVENT(%EXEC).
+ * An execve system call has succeeded and the new program is about to
+ * start running. The initial user register state is handy to be tweaked
+ * directly in @regs. @fmt and @bprm gives the details of this exec.
+ *
+ * @report_syscall_entry:
+ * Requested by %UTRACE_EVENT(%SYSCALL_ENTRY).
+ * Thread has entered the kernel to request a system call.
+ * The user register state is handy to be tweaked directly in @regs.
+ * The @action argument contains an &enum utrace_syscall_action,
+ * use utrace_syscall_action() to extract it. The return value
+ * overrides the last engine's action for the system call.
+ * If the final action is %UTRACE_SYSCALL_ABORT, no system call
+ * is made. The details of the system call being attempted can
+ * be fetched here with syscall_get_nr() and syscall_get_arguments().
+ * The parameter registers can be changed with syscall_set_arguments().
+ * See above about the %UTRACE_SYSCALL_RESUMED flag in @action.
+ * Use %UTRACE_REPORT in the return value to guarantee you get
+ * another callback (with %UTRACE_SYSCALL_RESUMED flag) in case
+ * @current stops with %UTRACE_STOP before attempting the system call.
+ *
+ * @report_syscall_exit:
+ * Requested by %UTRACE_EVENT(%SYSCALL_EXIT).
+ * Thread is about to leave the kernel after a system call request.
+ * The user register state is handy to be tweaked directly in @regs.
+ * The results of the system call attempt can be examined here using
+ * syscall_get_error() and syscall_get_return_value(). It is safe
+ * here to call syscall_set_return_value() or syscall_rollback().
+ *
+ * @report_exit:
+ * Requested by %UTRACE_EVENT(%EXIT).
+ * Thread is exiting and cannot be prevented from doing so,
+ * but all its state is still live. The @code value will be
+ * the wait result seen by the parent, and can be changed by
+ * this engine or others. The @orig_code value is the real
+ * status, not changed by any tracing engine. Returning %UTRACE_STOP
+ * here keeps @current stopped before it cleans up its state and dies,
+ * so it can be examined by other processes. When @current is allowed
+ * to run, it will die and get to the @report_death callback.
+ *
+ * @report_death:
+ * Requested by %UTRACE_EVENT(%DEATH).
+ * Thread is really dead now. It might be reaped by its parent at
+ * any time, or self-reap immediately. Though the actual reaping
+ * may happen in parallel, a report_reap() callback will always be
+ * ordered after a report_death() callback.
+ *
+ * @report_reap:
+ * Requested by %UTRACE_EVENT(%REAP).
+ * Called when someone reaps the dead task (parent, init, or self).
+ * This means the parent called wait, or else this was a detached
+ * thread or a process whose parent ignores SIGCHLD.
+ * No more callbacks are made after this one.
+ * The engine is always detached.
+ * There is nothing more a tracing engine can do about this thread.
+ * After this callback, the @engine pointer will become invalid.
+ * The @task pointer may become invalid if get_task_struct() hasn't
+ * been used to keep it alive.
+ * An engine should always request this callback if it stores the
+ * @engine pointer or stores any pointer in @engine->data, so it
+ * can clean up its data structures.
+ * Unlike other callbacks, this can be called from the parent's context
+ * rather than from the traced thread itself--it must not delay the
+ * parent by blocking.
+ *
+ * @release:
+ * If not %NULL, this is called after the last utrace_engine_put()
+ * call for a &struct utrace_engine, which could be implicit after
+ * a %UTRACE_DETACH return from another callback. Its argument is
+ * the engine's @data member.
+ */
+struct utrace_engine_ops {
+ u32 (*report_quiesce)(u32 action, struct utrace_engine *engine,
+ unsigned long event);
+ u32 (*report_signal)(u32 action, struct utrace_engine *engine,
+ struct pt_regs *regs,
+ siginfo_t *info,
+ const struct k_sigaction *orig_ka,
+ struct k_sigaction *return_ka);
+ u32 (*report_clone)(u32 action, struct utrace_engine *engine,
+ unsigned long clone_flags,
+ struct task_struct *child);
+ u32 (*report_jctl)(u32 action, struct utrace_engine *engine,
+ int type, int notify);
+ u32 (*report_exec)(u32 action, struct utrace_engine *engine,
+ const struct linux_binfmt *fmt,
+ const struct linux_binprm *bprm,
+ struct pt_regs *regs);
+ u32 (*report_syscall_entry)(u32 action, struct utrace_engine *engine,
+ struct pt_regs *regs);
+ u32 (*report_syscall_exit)(u32 action, struct utrace_engine *engine,
+ struct pt_regs *regs);
+ u32 (*report_exit)(u32 action, struct utrace_engine *engine,
+ long orig_code, long *code);
+ u32 (*report_death)(struct utrace_engine *engine,
+ bool group_dead, int signal);
+ void (*report_reap)(struct utrace_engine *engine,
+ struct task_struct *task);
+ void (*release)(void *data);
+};
+
+/**
+ * struct utrace_examiner - private state for using utrace_prepare_examine()
+ *
+ * The members of &struct utrace_examiner are private to the implementation.
+ * This data type holds the state from a call to utrace_prepare_examine()
+ * to be used by a call to utrace_finish_examine().
+ */
+struct utrace_examiner {
+/* private: */
+ long state;
+ unsigned long ncsw;
+};
+
+/*
+ * These are the exported entry points for tracing engines to use.
+ * See kernel/utrace.c for their kerneldoc comments with interface details.
+ */
+struct utrace_engine *utrace_attach_task(struct task_struct *, int,
+ const struct utrace_engine_ops *,
+ void *);
+struct utrace_engine *utrace_attach_pid(struct pid *, int,
+ const struct utrace_engine_ops *,
+ void *);
+int __must_check utrace_control(struct task_struct *,
+ struct utrace_engine *,
+ enum utrace_resume_action);
+int __must_check utrace_set_events(struct task_struct *,
+ struct utrace_engine *,
+ unsigned long eventmask);
+int __must_check utrace_barrier(struct task_struct *,
+ struct utrace_engine *);
+int __must_check utrace_prepare_examine(struct task_struct *,
+ struct utrace_engine *,
+ struct utrace_examiner *);
+int __must_check utrace_finish_examine(struct task_struct *,
+ struct utrace_engine *,
+ struct utrace_examiner *);
+
+/**
+ * utrace_control_pid - control a thread being traced by a tracing engine
+ * @pid: thread to affect
+ * @engine: attached engine to affect
+ * @action: &enum utrace_resume_action for thread to do
+ *
+ * This is the same as utrace_control(), but takes a &struct pid
+ * pointer rather than a &struct task_struct pointer. The caller must
+ * hold a ref on @pid, but does not need to worry about the task
+ * staying valid. If it's been reaped so that @pid points nowhere,
+ * then this call returns -%ESRCH.
+ */
+static inline __must_check int utrace_control_pid(
+ struct pid *pid, struct utrace_engine *engine,
+ enum utrace_resume_action action)
+{
+ /*
+ * We don't bother with rcu_read_lock() here to protect the
+ * task_struct pointer, because utrace_control will return
+ * -ESRCH without looking at that pointer if the engine is
+ * already detached. A task_struct pointer can't die before
+ * all the engines are detached in release_task() first.
+ */
+ struct task_struct *task = pid_task(pid, PIDTYPE_PID);
+ return unlikely(!task) ? -ESRCH : utrace_control(task, engine, action);
+}
+
+/**
+ * utrace_set_events_pid - choose which event reports a tracing engine gets
+ * @pid: thread to affect
+ * @engine: attached engine to affect
+ * @eventmask: new event mask
+ *
+ * This is the same as utrace_set_events(), but takes a &struct pid
+ * pointer rather than a &struct task_struct pointer. The caller must
+ * hold a ref on @pid, but does not need to worry about the task
+ * staying valid. If it's been reaped so that @pid points nowhere,
+ * then this call returns -%ESRCH.
+ */
+static inline __must_check int utrace_set_events_pid(
+ struct pid *pid, struct utrace_engine *engine, unsigned long eventmask)
+{
+ struct task_struct *task = pid_task(pid, PIDTYPE_PID);
+ return unlikely(!task) ? -ESRCH :
+ utrace_set_events(task, engine, eventmask);
+}
+
+/**
+ * utrace_barrier_pid - synchronize with simultaneous tracing callbacks
+ * @pid: thread to affect
+ * @engine: engine to affect (can be detached)
+ *
+ * This is the same as utrace_barrier(), but takes a &struct pid
+ * pointer rather than a &struct task_struct pointer. The caller must
+ * hold a ref on @pid, but does not need to worry about the task
+ * staying valid. If it's been reaped so that @pid points nowhere,
+ * then this call returns -%ESRCH.
+ */
+static inline __must_check int utrace_barrier_pid(struct pid *pid,
+ struct utrace_engine *engine)
+{
+ struct task_struct *task = pid_task(pid, PIDTYPE_PID);
+ return unlikely(!task) ? -ESRCH : utrace_barrier(task, engine);
+}
+
+#endif /* CONFIG_UTRACE */
+
+#endif /* linux/utrace.h */
diff --git a/init/Kconfig b/init/Kconfig
index 43298f9..3f670e5 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -372,6 +372,15 @@ config AUDIT_TREE
depends on AUDITSYSCALL
select FSNOTIFY
+config UTRACE
+ bool "Infrastructure for tracing and debugging user processes"
+ depends on EXPERIMENTAL
+ depends on HAVE_ARCH_TRACEHOOK
+ help
+ Enable the utrace process tracing interface. This is an internal
+ kernel interface exported to kernel modules, to track events in
+ user threads, extract and change user thread state.
+
source "kernel/irq/Kconfig"
menu "RCU Subsystem"
diff --git a/kernel/Makefile b/kernel/Makefile
index e898c5b..e43bbfb 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -68,6 +68,7 @@ obj-$(CONFIG_IKCONFIG) += configs.o
obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o
obj-$(CONFIG_SMP) += stop_machine.o
obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
+obj-$(CONFIG_UTRACE) += utrace.o
obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
obj-$(CONFIG_AUDIT_WATCH) += audit_watch.o
diff --git a/kernel/utrace.c b/kernel/utrace.c
new file mode 100644
index 0000000..ef856c9
--- /dev/null
+++ b/kernel/utrace.c
@@ -0,0 +1,2440 @@
+/*
+ * utrace infrastructure interface for debugging user processes
+ *
+ * Copyright (C) 2006-2010 Red Hat, Inc. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * Red Hat Author: Roland McGrath.
+ */
+
+#include <linux/utrace.h>
+#include <linux/tracehook.h>
+#include <linux/regset.h>
+#include <asm/syscall.h>
+#include <linux/ptrace.h>
+#include <linux/err.h>
+#include <linux/sched.h>
+#include <linux/freezer.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/seq_file.h>
+
+
+/*
+ * Per-thread structure private to utrace implementation.
+ * If task_struct.utrace_flags is nonzero, task_struct.utrace
+ * has always been allocated first. Once allocated, it is
+ * never freed until free_task().
+ *
+ * The common event reporting loops are done by the task making the
+ * report without ever taking any locks. To facilitate this, the two
+ * lists @attached and @attaching work together for smooth asynchronous
+ * attaching with low overhead. Modifying either list requires @lock.
+ * The @attaching list can be modified any time while holding @lock.
+ * New engines being attached always go on this list.
+ *
+ * The @attached list is what the task itself uses for its reporting
+ * loops. When the task itself is not quiescent, it can use the
+ * @attached list without taking any lock. Nobody may modify the list
+ * when the task is not quiescent. When it is quiescent, that means
+ * that it won't run again without taking @lock itself before using
+ * the list.
+ *
+ * At each place where we know the task is quiescent (or it's current),
+ * while holding @lock, we call splice_attaching(), below. This moves
+ * the @attaching list members on to the end of the @attached list.
+ * Since this happens at the start of any reporting pass, any new
+ * engines attached asynchronously go on the stable @attached list
+ * in time to have their callbacks seen.
+ */
+struct utrace {
+ spinlock_t lock;
+ struct list_head attached, attaching;
+
+ struct task_struct *cloning;
+
+ struct utrace_engine *reporting;
+
+ enum utrace_resume_action resume:UTRACE_RESUME_BITS;
+ unsigned int signal_handler:1;
+ unsigned int vfork_stop:1; /* need utrace_stop() before vfork wait */
+ unsigned int death:1; /* in utrace_report_death() now */
+ unsigned int reap:1; /* release_task() has run */
+ unsigned int pending_attach:1; /* need splice_attaching() */
+};
+
+static struct kmem_cache *utrace_cachep;
+static struct kmem_cache *utrace_engine_cachep;
+static const struct utrace_engine_ops utrace_detached_ops; /* forward decl */
+
+static int __init utrace_init(void)
+{
+ utrace_cachep = KMEM_CACHE(utrace, SLAB_PANIC);
+ utrace_engine_cachep = KMEM_CACHE(utrace_engine, SLAB_PANIC);
+ return 0;
+}
+module_init(utrace_init);
+
+/*
+ * Set up @task.utrace for the first time. We can have races
+ * between two utrace_attach_task() calls here. The task_lock()
+ * governs installing the new pointer. If another one got in first,
+ * we just punt the new one we allocated.
+ *
+ * This returns false only in case of a memory allocation failure.
+ */
+static bool utrace_task_alloc(struct task_struct *task)
+{
+ struct utrace *utrace = kmem_cache_zalloc(utrace_cachep, GFP_KERNEL);
+ if (unlikely(!utrace))
+ return false;
+ spin_lock_init(&utrace->lock);
+ INIT_LIST_HEAD(&utrace->attached);
+ INIT_LIST_HEAD(&utrace->attaching);
+ utrace->resume = UTRACE_RESUME;
+ task_lock(task);
+ if (likely(!task->utrace)) {
+ /*
+ * This barrier makes sure the initialization of the struct
+ * precedes the installation of the pointer. This pairs
+ * with smp_read_barrier_depends() in task_utrace_struct().
+ */
+ smp_wmb();
+ task->utrace = utrace;
+ }
+ task_unlock(task);
+
+ if (unlikely(task->utrace != utrace))
+ kmem_cache_free(utrace_cachep, utrace);
+ return true;
+}
+
+/*
+ * This is called via tracehook_free_task() from free_task()
+ * when @task is being deallocated.
+ */
+void utrace_free_task(struct task_struct *task)
+{
+ kmem_cache_free(utrace_cachep, task->utrace);
+}
+
+/*
+ * This is calledwhen the task is safely quiescent, i.e. it won't consult
+ * utrace->attached without the lock. Move any engines attached
+ * asynchronously from @utrace->attaching onto the @utrace->attached list.
+ */
+static void splice_attaching(struct utrace *utrace)
+{
+ lockdep_assert_held(&utrace->lock);
+ list_splice_tail_init(&utrace->attaching, &utrace->attached);
+ utrace->pending_attach = 0;
+}
+
+/*
+ * This is the exported function used by the utrace_engine_put() inline.
+ */
+void __utrace_engine_release(struct kref *kref)
+{
+ struct utrace_engine *engine = container_of(kref, struct utrace_engine,
+ kref);
+ BUG_ON(!list_empty(&engine->entry));
+ if (engine->release)
+ (*engine->release)(engine->data);
+ kmem_cache_free(utrace_engine_cachep, engine);
+}
+EXPORT_SYMBOL_GPL(__utrace_engine_release);
+
+static bool engine_matches(struct utrace_engine *engine, int flags,
+ const struct utrace_engine_ops *ops, void *data)
+{
+ if ((flags & UTRACE_ATTACH_MATCH_OPS) && engine->ops != ops)
+ return false;
+ if ((flags & UTRACE_ATTACH_MATCH_DATA) && engine->data != data)
+ return false;
+ return engine->ops && engine->ops != &utrace_detached_ops;
+}
+
+static struct utrace_engine *find_matching_engine(
+ struct utrace *utrace, int flags,
+ const struct utrace_engine_ops *ops, void *data)
+{
+ struct utrace_engine *engine;
+ list_for_each_entry(engine, &utrace->attached, entry)
+ if (engine_matches(engine, flags, ops, data))
+ return engine;
+ list_for_each_entry(engine, &utrace->attaching, entry)
+ if (engine_matches(engine, flags, ops, data))
+ return engine;
+ return NULL;
+}
+
+/*
+ * Enqueue @engine, or maybe don't if UTRACE_ATTACH_EXCLUSIVE.
+ */
+static int utrace_add_engine(struct task_struct *target,
+ struct utrace *utrace,
+ struct utrace_engine *engine,
+ int flags,
+ const struct utrace_engine_ops *ops,
+ void *data)
+{
+ int ret;
+
+ spin_lock(&utrace->lock);
+
+ ret = -EEXIST;
+ if ((flags & UTRACE_ATTACH_EXCLUSIVE) &&
+ unlikely(find_matching_engine(utrace, flags, ops, data)))
+ goto unlock;
+
+ /*
+ * In case we had no engines before, make sure that
+ * utrace_flags is not zero. Since we did unlock+lock
+ * at least once after utrace_task_alloc() installed
+ * ->utrace, we have the necessary barrier which pairs
+ * with rmb() in task_utrace_struct().
+ */
+ ret = -ESRCH;
+ if (!target->utrace_flags) {
+ target->utrace_flags = UTRACE_EVENT(REAP);
+ /*
+ * If we race with tracehook_prepare_release_task()
+ * make sure that either it sees utrace_flags != 0
+ * or we see exit_state == EXIT_DEAD.
+ */
+ smp_mb();
+ if (unlikely(target->exit_state == EXIT_DEAD)) {
+ target->utrace_flags = 0;
+ goto unlock;
+ }
+ }
+
+ /*
+ * Put the new engine on the pending ->attaching list.
+ * Make sure it gets onto the ->attached list by the next
+ * time it's examined. Setting ->pending_attach ensures
+ * that start_report() takes the lock and splices the lists
+ * before the next new reporting pass.
+ *
+ * When target == current, it would be safe just to call
+ * splice_attaching() right here. But if we're inside a
+ * callback, that would mean the new engine also gets
+ * notified about the event that precipitated its own
+ * creation. This is not what the user wants.
+ */
+ list_add_tail(&engine->entry, &utrace->attaching);
+ utrace->pending_attach = 1;
+ utrace_engine_get(engine);
+ ret = 0;
+unlock:
+ spin_unlock(&utrace->lock);
+
+ return ret;
+}
+
+/**
+ * utrace_attach_task - attach new engine, or look up an attached engine
+ * @target: thread to attach to
+ * @flags: flag bits combined with OR, see below
+ * @ops: callback table for new engine
+ * @data: engine private data pointer
+ *
+ * The caller must ensure that the @target thread does not get freed,
+ * i.e. hold a ref or be its parent. It is always safe to call this
+ * on @current, or on the @child pointer in a @report_clone callback.
+ * For most other cases, it's easier to use utrace_attach_pid() instead.
+ *
+ * UTRACE_ATTACH_CREATE:
+ * Create a new engine. If %UTRACE_ATTACH_CREATE is not specified, you
+ * only look up an existing engine already attached to the thread.
+ *
+ * UTRACE_ATTACH_EXCLUSIVE:
+ * Attempting to attach a second (matching) engine fails with -%EEXIST.
+ *
+ * UTRACE_ATTACH_MATCH_OPS: Only consider engines matching @ops.
+ * UTRACE_ATTACH_MATCH_DATA: Only consider engines matching @data.
+ *
+ * Calls with neither %UTRACE_ATTACH_MATCH_OPS nor %UTRACE_ATTACH_MATCH_DATA
+ * match the first among any engines attached to @target. That means that
+ * %UTRACE_ATTACH_EXCLUSIVE in such a call fails with -%EEXIST if there
+ * are any engines on @target at all.
+ */
+struct utrace_engine *utrace_attach_task(
+ struct task_struct *target, int flags,
+ const struct utrace_engine_ops *ops, void *data)
+{
+ struct utrace *utrace = task_utrace_struct(target);
+ struct utrace_engine *engine;
+ int ret;
+
+ if (!(flags & UTRACE_ATTACH_CREATE)) {
+ if (unlikely(!utrace))
+ return ERR_PTR(-ENOENT);
+ spin_lock(&utrace->lock);
+ engine = find_matching_engine(utrace, flags, ops, data);
+ if (engine)
+ utrace_engine_get(engine);
+ spin_unlock(&utrace->lock);
+ return engine ?: ERR_PTR(-ENOENT);
+ }
+
+ if (unlikely(!ops) || unlikely(ops == &utrace_detached_ops))
+ return ERR_PTR(-EINVAL);
+
+ if (unlikely(target->flags & PF_KTHREAD))
+ /*
+ * Silly kernel, utrace is for users!
+ */
+ return ERR_PTR(-EPERM);
+
+ if (!utrace) {
+ if (unlikely(!utrace_task_alloc(target)))
+ return ERR_PTR(-ENOMEM);
+ utrace = task_utrace_struct(target);
+ }
+
+ engine = kmem_cache_alloc(utrace_engine_cachep, GFP_KERNEL);
+ if (unlikely(!engine))
+ return ERR_PTR(-ENOMEM);
+
+ /*
+ * Initialize the new engine structure. It starts out with one ref
+ * to return. utrace_add_engine() adds another for being attached.
+ */
+ kref_init(&engine->kref);
+ engine->flags = 0;
+ engine->ops = ops;
+ engine->data = data;
+ engine->release = ops->release;
+
+ ret = utrace_add_engine(target, utrace, engine, flags, ops, data);
+
+ if (unlikely(ret)) {
+ kmem_cache_free(utrace_engine_cachep, engine);
+ engine = ERR_PTR(ret);
+ }
+
+
+ return engine;
+}
+EXPORT_SYMBOL_GPL(utrace_attach_task);
+
+/**
+ * utrace_attach_pid - attach new engine, or look up an attached engine
+ * @pid: &struct pid pointer representing thread to attach to
+ * @flags: flag bits combined with OR, see utrace_attach_task()
+ * @ops: callback table for new engine
+ * @data: engine private data pointer
+ *
+ * This is the same as utrace_attach_task(), but takes a &struct pid
+ * pointer rather than a &struct task_struct pointer. The caller must
+ * hold a ref on @pid, but does not need to worry about the task
+ * staying valid. If it's been reaped so that @pid points nowhere,
+ * then this call returns -%ESRCH.
+ */
+struct utrace_engine *utrace_attach_pid(
+ struct pid *pid, int flags,
+ const struct utrace_engine_ops *ops, void *data)
+{
+ struct utrace_engine *engine = ERR_PTR(-ESRCH);
+ struct task_struct *task = get_pid_task(pid, PIDTYPE_PID);
+ if (task) {
+ engine = utrace_attach_task(task, flags, ops, data);
+ put_task_struct(task);
+ }
+ return engine;
+}
+EXPORT_SYMBOL_GPL(utrace_attach_pid);
+
+/*
+ * When an engine is detached, the target thread may still see it and
+ * make callbacks until it quiesces. We install a special ops vector
+ * with these two callbacks. When the target thread quiesces, it can
+ * safely free the engine itself. For any event we will always get
+ * the report_quiesce() callback first, so we only need this one
+ * pointer to be set. The only exception is report_reap(), so we
+ * supply that callback too.
+ */
+static u32 utrace_detached_quiesce(u32 action, struct utrace_engine *engine,
+ unsigned long event)
+{
+ return UTRACE_DETACH;
+}
+
+static void utrace_detached_reap(struct utrace_engine *engine,
+ struct task_struct *task)
+{
+}
+
+static const struct utrace_engine_ops utrace_detached_ops = {
+ .report_quiesce = &utrace_detached_quiesce,
+ .report_reap = &utrace_detached_reap
+};
+
+/*
+ * The caller has to hold a ref on the engine. If the attached flag is
+ * true (all but utrace_barrier() calls), the engine is supposed to be
+ * attached. If the attached flag is false (utrace_barrier() only),
+ * then return -ERESTARTSYS for an engine marked for detach but not yet
+ * fully detached. The task pointer can be invalid if the engine is
+ * detached.
+ *
+ * Get the utrace lock for the target task.
+ * Returns the struct if locked, or ERR_PTR(-errno).
+ *
+ * This has to be robust against races with:
+ * utrace_control(target, UTRACE_DETACH) calls
+ * UTRACE_DETACH after reports
+ * utrace_report_death
+ * utrace_release_task
+ */
+static struct utrace *get_utrace_lock(struct task_struct *target,
+ struct utrace_engine *engine,
+ bool attached)
+ __acquires(utrace->lock)
+{
+ struct utrace *utrace;
+
+ rcu_read_lock();
+
+ /*
+ * If this engine was already detached, bail out before we look at
+ * the task_struct pointer at all. If it's detached after this
+ * check, then RCU is still keeping this task_struct pointer valid.
+ *
+ * The ops pointer is NULL when the engine is fully detached.
+ * It's &utrace_detached_ops when it's marked detached but still
+ * on the list. In the latter case, utrace_barrier() still works,
+ * since the target might be in the middle of an old callback.
+ */
+ if (unlikely(!engine->ops)) {
+ rcu_read_unlock();
+ return ERR_PTR(-ESRCH);
+ }
+
+ if (unlikely(engine->ops == &utrace_detached_ops)) {
+ rcu_read_unlock();
+ return attached ? ERR_PTR(-ESRCH) : ERR_PTR(-ERESTARTSYS);
+ }
+
+ utrace = task_utrace_struct(target);
+ spin_lock(&utrace->lock);
+ if (unlikely(utrace->reap) || unlikely(!engine->ops) ||
+ unlikely(engine->ops == &utrace_detached_ops)) {
+ /*
+ * By the time we got the utrace lock,
+ * it had been reaped or detached already.
+ */
+ spin_unlock(&utrace->lock);
+ utrace = ERR_PTR(-ESRCH);
+ if (!attached && engine->ops == &utrace_detached_ops)
+ utrace = ERR_PTR(-ERESTARTSYS);
+ }
+ rcu_read_unlock();
+
+ return utrace;
+}
+
+/*
+ * Now that we don't hold any locks, run through any
+ * detached engines and free their references. Each
+ * engine had one implicit ref while it was attached.
+ */
+static void put_detached_list(struct list_head *list)
+{
+ struct utrace_engine *engine, *next;
+ list_for_each_entry_safe(engine, next, list, entry) {
+ list_del_init(&engine->entry);
+ utrace_engine_put(engine);
+ }
+}
+
+/*
+ * We use an extra bit in utrace_engine.flags past the event bits,
+ * to record whether the engine is keeping the target thread stopped.
+ *
+ * This bit is set in task_struct.utrace_flags whenever it is set in any
+ * engine's flags. Only utrace_reset() resets it in utrace_flags.
+ */
+#define ENGINE_STOP (1UL << _UTRACE_NEVENTS)
+
+static void mark_engine_wants_stop(struct task_struct *task,
+ struct utrace_engine *engine)
+{
+ engine->flags |= ENGINE_STOP;
+ task->utrace_flags |= ENGINE_STOP;
+}
+
+static void clear_engine_wants_stop(struct utrace_engine *engine)
+{
+ engine->flags &= ~ENGINE_STOP;
+}
+
+static bool engine_wants_stop(struct utrace_engine *engine)
+{
+ return (engine->flags & ENGINE_STOP) != 0;
+}
+
+/**
+ * utrace_set_events - choose which event reports a tracing engine gets
+ * @target: thread to affect
+ * @engine: attached engine to affect
+ * @events: new event mask
+ *
+ * This changes the set of events for which @engine wants callbacks made.
+ *
+ * This fails with -%EALREADY and does nothing if you try to clear
+ * %UTRACE_EVENT(%DEATH) when the @report_death callback may already have
+ * begun, or if you try to newly set %UTRACE_EVENT(%DEATH) or
+ * %UTRACE_EVENT(%QUIESCE) when @target is already dead or dying.
+ *
+ * This fails with -%ESRCH if you try to clear %UTRACE_EVENT(%REAP) when
+ * the @report_reap callback may already have begun, or when @target has
+ * already been detached, including forcible detach on reaping.
+ *
+ * If @target was stopped before the call, then after a successful call,
+ * no event callbacks not requested in @events will be made; if
+ * %UTRACE_EVENT(%QUIESCE) is included in @events, then a
+ * @report_quiesce callback will be made when @target resumes.
+ *
+ * If @target was not stopped and @events excludes some bits that were
+ * set before, this can return -%EINPROGRESS to indicate that @target
+ * may have been making some callback to @engine. When this returns
+ * zero, you can be sure that no event callbacks you've disabled in
+ * @events can be made. If @events only sets new bits that were not set
+ * before on @engine, then -%EINPROGRESS will never be returned.
+ *
+ * To synchronize after an -%EINPROGRESS return, see utrace_barrier().
+ *
+ * When @target is @current, -%EINPROGRESS is not returned. But note
+ * that a newly-created engine will not receive any callbacks related to
+ * an event notification already in progress. This call enables @events
+ * callbacks to be made as soon as @engine becomes eligible for any
+ * callbacks, see utrace_attach_task().
+ *
+ * These rules provide for coherent synchronization based on %UTRACE_STOP,
+ * even when %SIGKILL is breaking its normal simple rules.
+ */
+int utrace_set_events(struct task_struct *target,
+ struct utrace_engine *engine,
+ unsigned long events)
+{
+ struct utrace *utrace;
+ unsigned long old_flags, old_utrace_flags;
+ int ret = -EALREADY;
+
+ /*
+ * We just ignore the internal bit, so callers can use
+ * engine->flags to seed bitwise ops for our argument.
+ */
+ events &= ~ENGINE_STOP;
+
+ utrace = get_utrace_lock(target, engine, true);
+ if (unlikely(IS_ERR(utrace)))
+ return PTR_ERR(utrace);
+
+ old_utrace_flags = target->utrace_flags;
+ old_flags = engine->flags & ~ENGINE_STOP;
+
+ /*
+ * If utrace_report_death() is already progress now,
+ * it's too late to clear the death event bits.
+ */
+ if (((old_flags & ~events) & _UTRACE_DEATH_EVENTS) && utrace->death)
+ goto unlock;
+
+ /*
+ * When setting these flags, it's essential that we really
+ * synchronize with exit_notify(). They cannot be set after
+ * exit_notify() takes the tasklist_lock. By holding the read
+ * lock here while setting the flags, we ensure that the calls
+ * to tracehook_notify_death() and tracehook_report_death() will
+ * see the new flags. This ensures that utrace_release_task()
+ * knows positively that utrace_report_death() will be called or
+ * that it won't.
+ */
+ if ((events & ~old_flags) & _UTRACE_DEATH_EVENTS) {
+ read_lock(&tasklist_lock);
+ if (unlikely(target->exit_state)) {
+ read_unlock(&tasklist_lock);
+ goto unlock;
+ }
+ target->utrace_flags |= events;
+ read_unlock(&tasklist_lock);
+ }
+
+ engine->flags = events | (engine->flags & ENGINE_STOP);
+ target->utrace_flags |= events;
+
+ if ((events & UTRACE_EVENT_SYSCALL) &&
+ !(old_utrace_flags & UTRACE_EVENT_SYSCALL))
+ set_tsk_thread_flag(target, TIF_SYSCALL_TRACE);
+
+ ret = 0;
+ if ((old_flags & ~events) && target != current &&
+ !task_is_stopped_or_traced(target) && !target->exit_state) {
+ /*
+ * This barrier ensures that our engine->flags changes
+ * have hit before we examine utrace->reporting,
+ * pairing with the barrier in start_callback(). If
+ * @target has not yet hit finish_callback() to clear
+ * utrace->reporting, we might be in the middle of a
+ * callback to @engine.
+ */
+ smp_mb();
+ if (utrace->reporting == engine)
+ ret = -EINPROGRESS;
+ }
+unlock:
+ spin_unlock(&utrace->lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(utrace_set_events);
+
+/*
+ * Asynchronously mark an engine as being detached.
+ *
+ * This must work while the target thread races with us doing
+ * start_callback(), defined below. It uses smp_rmb() between checking
+ * @engine->flags and using @engine->ops. Here we change @engine->ops
+ * first, then use smp_wmb() before changing @engine->flags. This ensures
+ * it can check the old flags before using the old ops, or check the old
+ * flags before using the new ops, or check the new flags before using the
+ * new ops, but can never check the new flags before using the old ops.
+ * Hence, utrace_detached_ops might be used with any old flags in place.
+ * It has report_quiesce() and report_reap() callbacks to handle all cases.
+ */
+static void mark_engine_detached(struct utrace_engine *engine)
+{
+ engine->ops = &utrace_detached_ops;
+ smp_wmb();
+ engine->flags = UTRACE_EVENT(QUIESCE);
+}
+
+/*
+ * Get @target to stop and return true if it is already stopped now.
+ * If we return false, it will make some event callback soonish.
+ * Called with @utrace locked.
+ */
+static bool utrace_do_stop(struct task_struct *target, struct utrace *utrace)
+{
+ if (task_is_stopped(target)) {
+ /*
+ * Stopped is considered quiescent; when it wakes up, it will
+ * go through utrace_finish_stop() before doing anything else.
+ */
+ spin_lock_irq(&target->sighand->siglock);
+ if (likely(task_is_stopped(target)))
+ __set_task_state(target, TASK_TRACED);
+ spin_unlock_irq(&target->sighand->siglock);
+ } else if (utrace->resume > UTRACE_REPORT) {
+ utrace->resume = UTRACE_REPORT;
+ set_notify_resume(target);
+ }
+
+ return task_is_traced(target);
+}
+
+/*
+ * If the target is not dead it should not be in tracing
+ * stop any more. Wake it unless it's in job control stop.
+ */
+static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
+{
+ lockdep_assert_held(&utrace->lock);
+ spin_lock_irq(&target->sighand->siglock);
+ if (target->signal->flags & SIGNAL_STOP_STOPPED ||
+ target->signal->group_stop_count)
+ target->state = TASK_STOPPED;
+ else
+ wake_up_state(target, __TASK_TRACED);
+ spin_unlock_irq(&target->sighand->siglock);
+}
+
+/*
+ * This is called when there might be some detached engines on the list or
+ * some stale bits in @task->utrace_flags. Clean them up and recompute the
+ * flags. Returns true if we're now fully detached.
+ *
+ * Called with @utrace->lock held, returns with it released.
+ * After this returns, @utrace might be freed if everything detached.
+ */
+static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
+ __releases(utrace->lock)
+{
+ struct utrace_engine *engine, *next;
+ unsigned long flags = 0;
+ LIST_HEAD(detached);
+
+ splice_attaching(utrace);
+
+ /*
+ * Update the set of events of interest from the union
+ * of the interests of the remaining tracing engines.
+ * For any engine marked detached, remove it from the list.
+ * We'll collect them on the detached list.
+ */
+ list_for_each_entry_safe(engine, next, &utrace->attached, entry) {
+ if (engine->ops == &utrace_detached_ops) {
+ engine->ops = NULL;
+ list_move(&engine->entry, &detached);
+ } else {
+ flags |= engine->flags | UTRACE_EVENT(REAP);
+ }
+ }
+
+ if (task->exit_state) {
+ /*
+ * Once it's already dead, we never install any flags
+ * except REAP. When ->exit_state is set and events
+ * like DEATH are not set, then they never can be set.
+ * This ensures that utrace_release_task() knows
+ * positively that utrace_report_death() can never run.
+ */
+ BUG_ON(utrace->death);
+ flags &= UTRACE_EVENT(REAP);
+ } else if (!(flags & UTRACE_EVENT_SYSCALL) &&
+ test_tsk_thread_flag(task, TIF_SYSCALL_TRACE)) {
+ clear_tsk_thread_flag(task, TIF_SYSCALL_TRACE);
+ }
+
+ if (!flags) {
+ /*
+ * No more engines, cleared out the utrace.
+ */
+ utrace->resume = UTRACE_RESUME;
+ utrace->signal_handler = 0;
+ }
+
+ /*
+ * If no more engines want it stopped, wake it up.
+ */
+ if (task_is_traced(task) && !(flags & ENGINE_STOP)) {
+ /*
+ * It just resumes, so make sure single-step
+ * is not left set.
+ */
+ if (utrace->resume == UTRACE_RESUME)
+ user_disable_single_step(task);
+ utrace_wakeup(task, utrace);
+ }
+
+ /*
+ * In theory spin_lock() doesn't imply rcu_read_lock().
+ * Once we clear ->utrace_flags this task_struct can go away
+ * because tracehook_prepare_release_task() path does not take
+ * utrace->lock when ->utrace_flags == 0.
+ */
+ rcu_read_lock();
+ task->utrace_flags = flags;
+ spin_unlock(&utrace->lock);
+ rcu_read_unlock();
+
+ put_detached_list(&detached);
+
+ return !flags;
+}
+
+void utrace_finish_stop(void)
+{
+ /*
+ * If we were task_is_traced() and then SIGKILL'ed, make
+ * sure we do nothing until the tracer drops utrace->lock.
+ */
+ if (unlikely(__fatal_signal_pending(current))) {
+ struct utrace *utrace = task_utrace_struct(current);
+ spin_unlock_wait(&utrace->lock);
+ }
+}
+
+/*
+ * Perform %UTRACE_STOP, i.e. block in TASK_TRACED until woken up.
+ * @task == current, @utrace == current->utrace, which is not locked.
+ * Return true if we were woken up by SIGKILL even though some utrace
+ * engine may still want us to stay stopped.
+ */
+static void utrace_stop(struct task_struct *task, struct utrace *utrace,
+ enum utrace_resume_action action)
+{
+relock:
+ spin_lock(&utrace->lock);
+
+ if (action < utrace->resume) {
+ /*
+ * Ensure a reporting pass when we're resumed.
+ */
+ utrace->resume = action;
+ if (action == UTRACE_INTERRUPT)
+ set_thread_flag(TIF_SIGPENDING);
+ else
+ set_thread_flag(TIF_NOTIFY_RESUME);
+ }
+
+ /*
+ * If the ENGINE_STOP bit is clear in utrace_flags, that means
+ * utrace_reset() ran after we processed some UTRACE_STOP return
+ * values from callbacks to get here. If all engines have detached
+ * or resumed us, we don't stop. This check doesn't require
+ * siglock, but it should follow the interrupt/report bookkeeping
+ * steps (this can matter for UTRACE_RESUME but not UTRACE_DETACH).
+ */
+ if (unlikely(!(task->utrace_flags & ENGINE_STOP))) {
+ utrace_reset(task, utrace);
+ if (task->utrace_flags & ENGINE_STOP)
+ goto relock;
+ return;
+ }
+
+ /*
+ * The siglock protects us against signals. As well as SIGKILL
+ * waking us up, we must synchronize with the signal bookkeeping
+ * for stop signals and SIGCONT.
+ */
+ spin_lock_irq(&task->sighand->siglock);
+
+ if (unlikely(__fatal_signal_pending(task))) {
+ spin_unlock_irq(&task->sighand->siglock);
+ spin_unlock(&utrace->lock);
+ return;
+ }
+
+ __set_current_state(TASK_TRACED);
+
+ /*
+ * If there is a group stop in progress,
+ * we must participate in the bookkeeping.
+ */
+ if (unlikely(task->signal->group_stop_count) &&
+ !--task->signal->group_stop_count)
+ task->signal->flags = SIGNAL_STOP_STOPPED;
+
+ spin_unlock_irq(&task->sighand->siglock);
+ spin_unlock(&utrace->lock);
+
+ schedule();
+
+ utrace_finish_stop();
+
+ /*
+ * While in TASK_TRACED, we were considered "frozen enough".
+ * Now that we woke up, it's crucial if we're supposed to be
+ * frozen that we freeze now before running anything substantial.
+ */
+ try_to_freeze();
+
+ /*
+ * While we were in TASK_TRACED, complete_signal() considered
+ * us "uninterested" in signal wakeups. Now make sure our
+ * TIF_SIGPENDING state is correct for normal running.
+ */
+ spin_lock_irq(&task->sighand->siglock);
+ recalc_sigpending();
+ spin_unlock_irq(&task->sighand->siglock);
+}
+
+/*
+ * Called by release_task() with @reap set to true.
+ * Called by utrace_report_death() with @reap set to false.
+ * On reap, make report_reap callbacks and clean out @utrace
+ * unless still making callbacks. On death, update bookkeeping
+ * and handle the reap work if release_task() came in first.
+ */
+void utrace_maybe_reap(struct task_struct *target, struct utrace *utrace,
+ bool reap)
+{
+ struct utrace_engine *engine, *next;
+ struct list_head attached;
+
+ spin_lock(&utrace->lock);
+
+ if (reap) {
+ /*
+ * If the target will do some final callbacks but hasn't
+ * finished them yet, we know because it clears these event
+ * bits after it's done. Instead of cleaning up here and
+ * requiring utrace_report_death() to cope with it, we
+ * delay the REAP report and the teardown until after the
+ * target finishes its death reports.
+ */
+ utrace->reap = 1;
+
+ if (target->utrace_flags & _UTRACE_DEATH_EVENTS) {
+ spin_unlock(&utrace->lock);
+ return;
+ }
+ } else {
+ /*
+ * After we unlock with this flag clear, any competing
+ * utrace_control/utrace_set_events calls know that we've
+ * finished our callbacks and any detach bookkeeping.
+ */
+ utrace->death = 0;
+
+ if (!utrace->reap) {
+ /*
+ * We're just dead, not reaped yet. This will
+ * reset @target->utrace_flags so the later call
+ * with @reap set won't hit the check above.
+ */
+ utrace_reset(target, utrace);
+ return;
+ }
+ }
+
+ /*
+ * utrace_add_engine() checks ->utrace_flags != 0. Since
+ * @utrace->reap is set, nobody can set or clear UTRACE_EVENT(REAP)
+ * in @engine->flags or change @engine->ops and nobody can change
+ * @utrace->attached after we drop the lock.
+ */
+ target->utrace_flags = 0;
+
+ /*
+ * We clear out @utrace->attached before we drop the lock so
+ * that find_matching_engine() can't come across any old engine
+ * while we are busy tearing it down.
+ */
+ list_replace_init(&utrace->attached, &attached);
+ list_splice_tail_init(&utrace->attaching, &attached);
+
+ spin_unlock(&utrace->lock);
+
+ list_for_each_entry_safe(engine, next, &attached, entry) {
+ if (engine->flags & UTRACE_EVENT(REAP))
+ engine->ops->report_reap(engine, target);
+
+ engine->ops = NULL;
+ engine->flags = 0;
+ list_del_init(&engine->entry);
+
+ utrace_engine_put(engine);
+ }
+}
+
+/*
+ * You can't do anything to a dead task but detach it.
+ * If release_task() has been called, you can't do that.
+ *
+ * On the exit path, DEATH and QUIESCE event bits are set only
+ * before utrace_report_death() has taken the lock. At that point,
+ * the death report will come soon, so disallow detach until it's
+ * done. This prevents us from racing with it detaching itself.
+ *
+ * Called only when @target->exit_state is nonzero.
+ */
+static inline int utrace_control_dead(struct task_struct *target,
+ struct utrace *utrace,
+ enum utrace_resume_action action)
+{
+ lockdep_assert_held(&utrace->lock);
+
+ if (action != UTRACE_DETACH || unlikely(utrace->reap))
+ return -ESRCH;
+
+ if (unlikely(utrace->death))
+ /*
+ * We have already started the death report. We can't
+ * prevent the report_death and report_reap callbacks,
+ * so tell the caller they will happen.
+ */
+ return -EALREADY;
+
+ return 0;
+}
+
+/**
+ * utrace_control - control a thread being traced by a tracing engine
+ * @target: thread to affect
+ * @engine: attached engine to affect
+ * @action: &enum utrace_resume_action for thread to do
+ *
+ * This is how a tracing engine asks a traced thread to do something.
+ * This call is controlled by the @action argument, which has the
+ * same meaning as the &enum utrace_resume_action value returned by
+ * event reporting callbacks.
+ *
+ * If @target is already dead (@target->exit_state nonzero),
+ * all actions except %UTRACE_DETACH fail with -%ESRCH.
+ *
+ * The following sections describe each option for the @action argument.
+ *
+ * UTRACE_DETACH:
+ *
+ * After this, the @engine data structure is no longer accessible,
+ * and the thread might be reaped. The thread will start running
+ * again if it was stopped and no longer has any attached engines
+ * that want it stopped.
+ *
+ * If the @report_reap callback may already have begun, this fails
+ * with -%ESRCH. If the @report_death callback may already have
+ * begun, this fails with -%EALREADY.
+ *
+ * If @target is not already stopped, then a callback to this engine
+ * might be in progress or about to start on another CPU. If so,
+ * then this returns -%EINPROGRESS; the detach happens as soon as
+ * the pending callback is finished. To synchronize after an
+ * -%EINPROGRESS return, see utrace_barrier().
+ *
+ * If @target is properly stopped before utrace_control() is called,
+ * then after successful return it's guaranteed that no more callbacks
+ * to the @engine->ops vector will be made.
+ *
+ * The only exception is %SIGKILL (and exec or group-exit by another
+ * thread in the group), which can cause asynchronous @report_death
+ * and/or @report_reap callbacks even when %UTRACE_STOP was used.
+ * (In that event, this fails with -%ESRCH or -%EALREADY, see above.)
+ *
+ * UTRACE_STOP:
+ *
+ * This asks that @target stop running. This returns 0 only if
+ * @target is already stopped, either for tracing or for job
+ * control. Then @target will remain stopped until another
+ * utrace_control() call is made on @engine; @target can be woken
+ * only by %SIGKILL (or equivalent, such as exec or termination by
+ * another thread in the same thread group).
+ *
+ * This returns -%EINPROGRESS if @target is not already stopped.
+ * Then the effect is like %UTRACE_REPORT. A @report_quiesce or
+ * @report_signal callback will be made soon. Your callback can
+ * then return %UTRACE_STOP to keep @target stopped.
+ *
+ * This does not interrupt system calls in progress, including ones
+ * that sleep for a long time. For that, use %UTRACE_INTERRUPT.
+ * To interrupt system calls and then keep @target stopped, your
+ * @report_signal callback can return %UTRACE_STOP.
+ *
+ * UTRACE_RESUME:
+ *
+ * Just let @target continue running normally, reversing the effect
+ * of a previous %UTRACE_STOP. If another engine is keeping @target
+ * stopped, then it remains stopped until all engines let it resume.
+ * If @target was not stopped, this has no effect.
+ *
+ * UTRACE_REPORT:
+ *
+ * This is like %UTRACE_RESUME, but also ensures that there will be
+ * a @report_quiesce or @report_signal callback made soon. If
+ * @target had been stopped, then there will be a callback before it
+ * resumes running normally. If another engine is keeping @target
+ * stopped, then there might be no callbacks until all engines let
+ * it resume.
+ *
+ * Since this is meaningless unless @report_quiesce callbacks will
+ * be made, it returns -%EINVAL if @engine lacks %UTRACE_EVENT(%QUIESCE).
+ *
+ * UTRACE_INTERRUPT:
+ *
+ * This is like %UTRACE_REPORT, but ensures that @target will make a
+ * @report_signal callback before it resumes or delivers signals.
+ * If @target was in a system call or about to enter one, work in
+ * progress will be interrupted as if by %SIGSTOP. If another
+ * engine is keeping @target stopped, then there might be no
+ * callbacks until all engines let it resume.
+ *
+ * This gives @engine an opportunity to introduce a forced signal
+ * disposition via its @report_signal callback.
+ *
+ * UTRACE_SINGLESTEP:
+ *
+ * It's invalid to use this unless arch_has_single_step() returned true.
+ * This is like %UTRACE_RESUME, but resumes for one user instruction only.
+ *
+ * Note that passing %UTRACE_SINGLESTEP or %UTRACE_BLOCKSTEP to
+ * utrace_control() or returning it from an event callback alone does
+ * not necessarily ensure that stepping will be enabled. If there are
+ * more callbacks made to any engine before returning to user mode,
+ * then the resume action is chosen only by the last set of callbacks.
+ * To be sure, enable %UTRACE_EVENT(%QUIESCE) and look for the
+ * @report_quiesce callback with a zero event mask, or the
+ * @report_signal callback with %UTRACE_SIGNAL_REPORT.
+ *
+ * Since this is not robust unless @report_quiesce callbacks will
+ * be made, it returns -%EINVAL if @engine lacks %UTRACE_EVENT(%QUIESCE).
+ *
+ * UTRACE_BLOCKSTEP:
+ *
+ * It's invalid to use this unless arch_has_block_step() returned true.
+ * This is like %UTRACE_SINGLESTEP, but resumes for one whole basic
+ * block of user instructions.
+ *
+ * Since this is not robust unless @report_quiesce callbacks will
+ * be made, it returns -%EINVAL if @engine lacks %UTRACE_EVENT(%QUIESCE).
+ *
+ * %UTRACE_BLOCKSTEP devolves to %UTRACE_SINGLESTEP when another
+ * tracing engine is using %UTRACE_SINGLESTEP at the same time.
+ */
+int utrace_control(struct task_struct *target,
+ struct utrace_engine *engine,
+ enum utrace_resume_action action)
+{
+ struct utrace *utrace;
+ bool reset;
+ int ret;
+
+ if (unlikely(action >= UTRACE_RESUME_MAX)) {
+ WARN(1, "invalid action argument to utrace_control()!");
+ return -EINVAL;
+ }
+
+ /*
+ * This is a sanity check for a programming error in the caller.
+ * Their request can only work properly in all cases by relying on
+ * a follow-up callback, but they didn't set one up! This check
+ * doesn't do locking, but it shouldn't matter. The caller has to
+ * be synchronously sure the callback is set up to be operating the
+ * interface properly.
+ */
+ if (action >= UTRACE_REPORT && action < UTRACE_RESUME &&
+ unlikely(!(engine->flags & UTRACE_EVENT(QUIESCE)))) {
+ WARN(1, "utrace_control() with no QUIESCE callback in place!");
+ return -EINVAL;
+ }
+
+ utrace = get_utrace_lock(target, engine, true);
+ if (unlikely(IS_ERR(utrace)))
+ return PTR_ERR(utrace);
+
+ reset = task_is_traced(target);
+ ret = 0;
+
+ /*
+ * ->exit_state can change under us, this doesn't matter.
+ * We do not care about ->exit_state in fact, but we do
+ * care about ->reap and ->death. If either flag is set,
+ * we must also see ->exit_state != 0.
+ */
+ if (unlikely(target->exit_state)) {
+ ret = utrace_control_dead(target, utrace, action);
+ if (ret) {
+ spin_unlock(&utrace->lock);
+ return ret;
+ }
+ reset = true;
+ }
+
+ switch (action) {
+ case UTRACE_STOP:
+ mark_engine_wants_stop(target, engine);
+ if (!reset && !utrace_do_stop(target, utrace))
+ ret = -EINPROGRESS;
+ reset = false;
+ break;
+
+ case UTRACE_DETACH:
+ if (engine_wants_stop(engine))
+ target->utrace_flags &= ~ENGINE_STOP;
+ mark_engine_detached(engine);
+ reset = reset || utrace_do_stop(target, utrace);
+ if (!reset) {
+ /*
+ * As in utrace_set_events(), this barrier ensures
+ * that our engine->flags changes have hit before we
+ * examine utrace->reporting, pairing with the barrier
+ * in start_callback(). If @target has not yet hit
+ * finish_callback() to clear utrace->reporting, we
+ * might be in the middle of a callback to @engine.
+ */
+ smp_mb();
+ if (utrace->reporting == engine)
+ ret = -EINPROGRESS;
+ }
+ break;
+
+ case UTRACE_RESUME:
+ clear_engine_wants_stop(engine);
+ break;
+
+ case UTRACE_BLOCKSTEP:
+ /*
+ * Resume from stopped, step one block.
+ * We fall through to treat it like UTRACE_SINGLESTEP.
+ */
+ if (unlikely(!arch_has_block_step())) {
+ WARN(1, "UTRACE_BLOCKSTEP when !arch_has_block_step()");
+ action = UTRACE_SINGLESTEP;
+ }
+
+ case UTRACE_SINGLESTEP:
+ /*
+ * Resume from stopped, step one instruction.
+ * We fall through to the UTRACE_REPORT case.
+ */
+ if (unlikely(!arch_has_single_step())) {
+ WARN(1,
+ "UTRACE_SINGLESTEP when !arch_has_single_step()");
+ reset = false;
+ ret = -EOPNOTSUPP;
+ break;
+ }
+
+ case UTRACE_REPORT:
+ /*
+ * Make the thread call tracehook_notify_resume() soon.
+ * But don't bother if it's already been interrupted.
+ * In that case, utrace_get_signal() will be reporting soon.
+ */
+ clear_engine_wants_stop(engine);
+ if (action < utrace->resume) {
+ utrace->resume = action;
+ set_notify_resume(target);
+ }
+ break;
+
+ case UTRACE_INTERRUPT:
+ /*
+ * Make the thread call tracehook_get_signal() soon.
+ */
+ clear_engine_wants_stop(engine);
+ if (utrace->resume == UTRACE_INTERRUPT)
+ break;
+ utrace->resume = UTRACE_INTERRUPT;
+
+ /*
+ * If it's not already stopped, interrupt it now. We need
+ * the siglock here in case it calls recalc_sigpending()
+ * and clears its own TIF_SIGPENDING. By taking the lock,
+ * we've serialized any later recalc_sigpending() after our
+ * setting of utrace->resume to force it on.
+ */
+ if (reset) {
+ /*
+ * This is really just to keep the invariant that
+ * TIF_SIGPENDING is set with UTRACE_INTERRUPT.
+ * When it's stopped, we know it's always going
+ * through utrace_get_signal() and will recalculate.
+ */
+ set_tsk_thread_flag(target, TIF_SIGPENDING);
+ } else {
+ struct sighand_struct *sighand;
+ unsigned long irqflags;
+ sighand = lock_task_sighand(target, &irqflags);
+ if (likely(sighand)) {
+ signal_wake_up(target, 0);
+ unlock_task_sighand(target, &irqflags);
+ }
+ }
+ break;
+
+ default:
+ BUG(); /* We checked it on entry. */
+ }
+
+ /*
+ * Let the thread resume running. If it's not stopped now,
+ * there is nothing more we need to do.
+ */
+ if (reset)
+ utrace_reset(target, utrace);
+ else
+ spin_unlock(&utrace->lock);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(utrace_control);
+
+/**
+ * utrace_barrier - synchronize with simultaneous tracing callbacks
+ * @target: thread to affect
+ * @engine: engine to affect (can be detached)
+ *
+ * This blocks while @target might be in the midst of making a callback to
+ * @engine. It can be interrupted by signals and will return -%ERESTARTSYS.
+ * A return value of zero means no callback from @target to @engine was
+ * in progress. Any effect of its return value (such as %UTRACE_STOP) has
+ * already been applied to @engine.
+ *
+ * It's not necessary to keep the @target pointer alive for this call.
+ * It's only necessary to hold a ref on @engine. This will return
+ * safely even if @target has been reaped and has no task refs.
+ *
+ * A successful return from utrace_barrier() guarantees its ordering
+ * with respect to utrace_set_events() and utrace_control() calls. If
+ * @target was not properly stopped, event callbacks just disabled might
+ * still be in progress; utrace_barrier() waits until there is no chance
+ * an unwanted callback can be in progress.
+ */
+int utrace_barrier(struct task_struct *target, struct utrace_engine *engine)
+{
+ struct utrace *utrace;
+ int ret = -ERESTARTSYS;
+
+ if (unlikely(target == current))
+ return 0;
+
+ do {
+ utrace = get_utrace_lock(target, engine, false);
+ if (unlikely(IS_ERR(utrace))) {
+ ret = PTR_ERR(utrace);
+ if (ret != -ERESTARTSYS)
+ break;
+ } else {
+ /*
+ * All engine state changes are done while
+ * holding the lock, i.e. before we get here.
+ * Since we have the lock, we only need to
+ * worry about @target making a callback.
+ * When it has entered start_callback() but
+ * not yet gotten to finish_callback(), we
+ * will see utrace->reporting == @engine.
+ * When @target doesn't take the lock, it uses
+ * barriers to order setting utrace->reporting
+ * before it examines the engine state.
+ */
+ if (utrace->reporting != engine)
+ ret = 0;
+ spin_unlock(&utrace->lock);
+ if (!ret)
+ break;
+ }
+ schedule_timeout_interruptible(1);
+ } while (!signal_pending(current));
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(utrace_barrier);
+
+/*
+ * This is local state used for reporting loops, perhaps optimized away.
+ */
+struct utrace_report {
+ u32 result;
+ enum utrace_resume_action action;
+ enum utrace_resume_action resume_action;
+ bool detaches;
+ bool spurious;
+};
+
+#define INIT_REPORT(var) \
+ struct utrace_report var = { \
+ .action = UTRACE_RESUME, \
+ .resume_action = UTRACE_RESUME, \
+ .spurious = true \
+ }
+
+/*
+ * We are now making the report, so clear the flag saying we need one.
+ * When there is a new attach, ->pending_attach is set just so we will
+ * know to do splice_attaching() here before the callback loop.
+ */
+static enum utrace_resume_action start_report(struct utrace *utrace)
+{
+ enum utrace_resume_action resume = utrace->resume;
+ if (utrace->pending_attach ||
+ (resume > UTRACE_INTERRUPT && resume < UTRACE_RESUME)) {
+ spin_lock(&utrace->lock);
+ splice_attaching(utrace);
+ resume = utrace->resume;
+ if (resume > UTRACE_INTERRUPT)
+ utrace->resume = UTRACE_RESUME;
+ spin_unlock(&utrace->lock);
+ }
+ return resume;
+}
+
+static inline void finish_report_reset(struct task_struct *task,
+ struct utrace *utrace,
+ struct utrace_report *report)
+{
+ if (unlikely(report->spurious || report->detaches)) {
+ spin_lock(&utrace->lock);
+ if (utrace_reset(task, utrace))
+ report->action = UTRACE_RESUME;
+ }
+}
+
+/*
+ * Complete a normal reporting pass, pairing with a start_report() call.
+ * This handles any UTRACE_DETACH or UTRACE_REPORT or UTRACE_INTERRUPT
+ * returns from engine callbacks. If @will_not_stop is true and any
+ * engine's last callback used UTRACE_STOP, we do UTRACE_REPORT here to
+ * ensure we stop before user mode. If there were no callbacks made, it
+ * will recompute @task->utrace_flags to avoid another false-positive.
+ */
+static void finish_report(struct task_struct *task, struct utrace *utrace,
+ struct utrace_report *report, bool will_not_stop)
+{
+ enum utrace_resume_action resume = report->action;
+
+ if (resume == UTRACE_STOP)
+ resume = will_not_stop ? UTRACE_REPORT : UTRACE_RESUME;
+
+ if (resume < utrace->resume) {
+ spin_lock(&utrace->lock);
+ utrace->resume = resume;
+ if (resume == UTRACE_INTERRUPT)
+ set_tsk_thread_flag(task, TIF_SIGPENDING);
+ else
+ set_tsk_thread_flag(task, TIF_NOTIFY_RESUME);
+ spin_unlock(&utrace->lock);
+ }
+
+ finish_report_reset(task, utrace, report);
+}
+
+static void finish_callback_report(struct task_struct *task,
+ struct utrace *utrace,
+ struct utrace_report *report,
+ struct utrace_engine *engine,
+ enum utrace_resume_action action)
+{
+ if (action == UTRACE_DETACH) {
+ /*
+ * By holding the lock here, we make sure that
+ * utrace_barrier() (really get_utrace_lock()) sees the
+ * effect of this detach. Otherwise utrace_barrier() could
+ * return 0 after this callback had returned UTRACE_DETACH.
+ * This way, a 0 return is an unambiguous indicator that any
+ * callback returning UTRACE_DETACH has indeed caused detach.
+ */
+ spin_lock(&utrace->lock);
+ engine->ops = &utrace_detached_ops;
+ spin_unlock(&utrace->lock);
+ }
+
+ /*
+ * If utrace_control() was used, treat that like UTRACE_DETACH here.
+ */
+ if (engine->ops == &utrace_detached_ops) {
+ report->detaches = true;
+ return;
+ }
+
+ if (action < report->action)
+ report->action = action;
+
+ if (action != UTRACE_STOP) {
+ if (action < report->resume_action)
+ report->resume_action = action;
+
+ if (engine_wants_stop(engine)) {
+ spin_lock(&utrace->lock);
+ clear_engine_wants_stop(engine);
+ spin_unlock(&utrace->lock);
+ }
+
+ return;
+ }
+
+ if (!engine_wants_stop(engine)) {
+ spin_lock(&utrace->lock);
+ /*
+ * If utrace_control() came in and detached us
+ * before we got the lock, we must not stop now.
+ */
+ if (unlikely(engine->ops == &utrace_detached_ops))
+ report->detaches = true;
+ else
+ mark_engine_wants_stop(task, engine);
+ spin_unlock(&utrace->lock);
+ }
+}
+
+/*
+ * Apply the return value of one engine callback to @report.
+ * Returns true if @engine detached and should not get any more callbacks.
+ */
+static bool finish_callback(struct task_struct *task, struct utrace *utrace,
+ struct utrace_report *report,
+ struct utrace_engine *engine,
+ u32 ret)
+{
+ report->result = ret & ~UTRACE_RESUME_MASK;
+ finish_callback_report(task, utrace, report, engine,
+ utrace_resume_action(ret));
+
+ /*
+ * Now that we have applied the effect of the return value,
+ * clear this so that utrace_barrier() can stop waiting.
+ * A subsequent utrace_control() can stop or resume @engine
+ * and know this was ordered after its callback's action.
+ *
+ * We don't need any barriers here because utrace_barrier()
+ * takes utrace->lock. If we touched engine->flags above,
+ * the lock guaranteed this change was before utrace_barrier()
+ * examined utrace->reporting.
+ */
+ utrace->reporting = NULL;
+
+ /*
+ * We've just done an engine callback. These are allowed to sleep,
+ * though all well-behaved ones restrict that to blocking kalloc()
+ * or quickly-acquired mutex_lock() and the like. This is a good
+ * place to make sure tracing engines don't introduce too much
+ * latency under voluntary preemption.
+ */
+ might_sleep();
+
+ return engine->ops == &utrace_detached_ops;
+}
+
+/*
+ * Start the callbacks for @engine to consider @event (a bit mask).
+ * This makes the report_quiesce() callback first. If @engine wants
+ * a specific callback for @event, we return the ops vector to use.
+ * If not, we return NULL. The return value from the ops->callback
+ * function called should be passed to finish_callback().
+ */
+static const struct utrace_engine_ops *start_callback(
+ struct utrace *utrace, struct utrace_report *report,
+ struct utrace_engine *engine, struct task_struct *task,
+ unsigned long event)
+{
+ const struct utrace_engine_ops *ops;
+ unsigned long want;
+
+ /*
+ * This barrier ensures that we've set utrace->reporting before
+ * we examine engine->flags or engine->ops. utrace_barrier()
+ * relies on this ordering to indicate that the effect of any
+ * utrace_control() and utrace_set_events() calls is in place
+ * by the time utrace->reporting can be seen to be NULL.
+ */
+ utrace->reporting = engine;
+ smp_mb();
+
+ /*
+ * This pairs with the barrier in mark_engine_detached().
+ * It makes sure that we never see the old ops vector with
+ * the new flags, in case the original vector had no report_quiesce.
+ */
+ want = engine->flags;
+ smp_rmb();
+ ops = engine->ops;
+
+ if ((want & UTRACE_EVENT(QUIESCE)) || ops == &utrace_detached_ops) {
+ if (finish_callback(task, utrace, report, engine,
+ (*ops->report_quiesce)(report->action,
+ engine, event)))
+ return NULL;
+
+ if (!event) {
+ /* We only got here to report QUIESCE */
+ report->spurious = false;
+ return NULL;
+ }
+
+ /*
+ * finish_callback() reset utrace->reporting after the
+ * quiesce callback. Now we set it again (as above)
+ * before re-examining engine->flags, which could have
+ * been changed synchronously by ->report_quiesce or
+ * asynchronously by utrace_control() or utrace_set_events().
+ */
+ utrace->reporting = engine;
+ smp_mb();
+ want = engine->flags;
+ }
+
+ if (want & ENGINE_STOP)
+ report->action = UTRACE_STOP;
+
+ if (want & event) {
+ report->spurious = false;
+ return ops;
+ }
+
+ utrace->reporting = NULL;
+ return NULL;
+}
+
+/*
+ * Do a normal reporting pass for engines interested in @event.
+ * @callback is the name of the member in the ops vector, and remaining
+ * args are the extras it takes after the standard three args.
+ */
+#define REPORT_CALLBACKS(rev, task, utrace, report, event, callback, ...) \
+ do { \
+ struct utrace_engine *engine; \
+ const struct utrace_engine_ops *ops; \
+ list_for_each_entry##rev(engine, &utrace->attached, entry) { \
+ ops = start_callback(utrace, report, engine, task, \
+ event); \
+ if (!ops) \
+ continue; \
+ finish_callback(task, utrace, report, engine, \
+ (*ops->callback)(__VA_ARGS__)); \
+ } \
+ } while (0)
+#define REPORT(task, utrace, report, event, callback, ...) \
+ do { \
+ start_report(utrace); \
+ REPORT_CALLBACKS(, task, utrace, report, event, callback, \
+ (report)->action, engine, ## __VA_ARGS__); \
+ finish_report(task, utrace, report, true); \
+ } while (0)
+
+/*
+ * Called iff UTRACE_EVENT(EXEC) flag is set.
+ */
+void utrace_report_exec(struct linux_binfmt *fmt, struct linux_binprm *bprm,
+ struct pt_regs *regs)
+{
+ struct task_struct *task = current;
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+
+ REPORT(task, utrace, &report, UTRACE_EVENT(EXEC),
+ report_exec, fmt, bprm, regs);
+}
+
+static u32 do_report_syscall_entry(struct pt_regs *regs,
+ struct task_struct *task,
+ struct utrace *utrace,
+ struct utrace_report *report,
+ u32 resume_report)
+{
+ start_report(utrace);
+ REPORT_CALLBACKS(_reverse, task, utrace, report,
+ UTRACE_EVENT(SYSCALL_ENTRY), report_syscall_entry,
+ resume_report | report->result | report->action,
+ engine, regs);
+ finish_report(task, utrace, report, false);
+
+ if (report->action != UTRACE_STOP)
+ return 0;
+
+ utrace_stop(task, utrace, report->resume_action);
+
+ if (fatal_signal_pending(task)) {
+ /*
+ * We are continuing despite UTRACE_STOP because of a
+ * SIGKILL. Don't let the system call actually proceed.
+ */
+ report->result = UTRACE_SYSCALL_ABORT;
+ } else if (utrace->resume <= UTRACE_REPORT) {
+ /*
+ * If we've been asked for another report after our stop,
+ * go back to report (and maybe stop) again before we run
+ * the system call. The second (and later) reports are
+ * marked with the UTRACE_SYSCALL_RESUMED flag so that
+ * engines know this is a second report at the same
+ * entry. This gives them the chance to examine the
+ * registers anew after they might have been changed
+ * while we were stopped.
+ */
+ report->detaches = false;
+ report->spurious = true;
+ report->action = report->resume_action = UTRACE_RESUME;
+ return UTRACE_SYSCALL_RESUMED;
+ }
+
+ return 0;
+}
+
+/*
+ * Called iff UTRACE_EVENT(SYSCALL_ENTRY) flag is set.
+ * Return true to prevent the system call.
+ */
+bool utrace_report_syscall_entry(struct pt_regs *regs)
+{
+ struct task_struct *task = current;
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+ u32 resume_report = 0;
+
+ do {
+ resume_report = do_report_syscall_entry(regs, task, utrace,
+ &report, resume_report);
+ } while (resume_report);
+
+ return utrace_syscall_action(report.result) == UTRACE_SYSCALL_ABORT;
+}
+
+/*
+ * Called iff UTRACE_EVENT(SYSCALL_EXIT) flag is set.
+ */
+void utrace_report_syscall_exit(struct pt_regs *regs)
+{
+ struct task_struct *task = current;
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+
+ REPORT(task, utrace, &report, UTRACE_EVENT(SYSCALL_EXIT),
+ report_syscall_exit, regs);
+}
+
+/*
+ * Called iff UTRACE_EVENT(CLONE) flag is set.
+ * This notification call blocks the wake_up_new_task call on the child.
+ * So we must not quiesce here. tracehook_report_clone_complete will do
+ * a quiescence check momentarily.
+ */
+void utrace_report_clone(unsigned long clone_flags, struct task_struct *child)
+{
+ struct task_struct *task = current;
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+
+ /*
+ * We don't use the REPORT() macro here, because we need
+ * to clear utrace->cloning before finish_report().
+ * After finish_report(), utrace can be a stale pointer
+ * in cases when report.action is still UTRACE_RESUME.
+ */
+ start_report(utrace);
+ utrace->cloning = child;
+
+ REPORT_CALLBACKS(, task, utrace, &report,
+ UTRACE_EVENT(CLONE), report_clone,
+ report.action, engine, clone_flags, child);
+
+ utrace->cloning = NULL;
+ finish_report(task, utrace, &report, !(clone_flags & CLONE_VFORK));
+
+ /*
+ * For a vfork, we will go into an uninterruptible block waiting
+ * for the child. We need UTRACE_STOP to happen before this, not
+ * after. For CLONE_VFORK, utrace_finish_vfork() will be called.
+ */
+ if (report.action == UTRACE_STOP && (clone_flags & CLONE_VFORK)) {
+ spin_lock(&utrace->lock);
+ utrace->vfork_stop = 1;
+ spin_unlock(&utrace->lock);
+ }
+}
+
+/*
+ * We're called after utrace_report_clone() for a CLONE_VFORK.
+ * If UTRACE_STOP was left from the clone report, we stop here.
+ * After this, we'll enter the uninterruptible wait_for_completion()
+ * waiting for the child.
+ */
+void utrace_finish_vfork(struct task_struct *task)
+{
+ struct utrace *utrace = task_utrace_struct(task);
+
+ if (utrace->vfork_stop) {
+ spin_lock(&utrace->lock);
+ utrace->vfork_stop = 0;
+ spin_unlock(&utrace->lock);
+ utrace_stop(task, utrace, UTRACE_RESUME); /* XXX */
+ }
+}
+
+/*
+ * Called iff UTRACE_EVENT(JCTL) flag is set.
+ *
+ * Called with siglock held.
+ */
+void utrace_report_jctl(int notify, int what)
+{
+ struct task_struct *task = current;
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+
+ spin_unlock_irq(&task->sighand->siglock);
+
+ REPORT(task, utrace, &report, UTRACE_EVENT(JCTL),
+ report_jctl, what, notify);
+
+ spin_lock_irq(&task->sighand->siglock);
+}
+
+/*
+ * Called iff UTRACE_EVENT(EXIT) flag is set.
+ */
+void utrace_report_exit(long *exit_code)
+{
+ struct task_struct *task = current;
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+ long orig_code = *exit_code;
+
+ REPORT(task, utrace, &report, UTRACE_EVENT(EXIT),
+ report_exit, orig_code, exit_code);
+
+ if (report.action == UTRACE_STOP)
+ utrace_stop(task, utrace, report.resume_action);
+}
+
+/*
+ * Called iff UTRACE_EVENT(DEATH) or UTRACE_EVENT(QUIESCE) flag is set.
+ *
+ * It is always possible that we are racing with utrace_release_task here.
+ * For this reason, utrace_release_task checks for the event bits that get
+ * us here, and delays its cleanup for us to do.
+ */
+void utrace_report_death(struct task_struct *task, struct utrace *utrace,
+ bool group_dead, int signal)
+{
+ INIT_REPORT(report);
+
+ BUG_ON(!task->exit_state);
+
+ /*
+ * We are presently considered "quiescent"--which is accurate
+ * inasmuch as we won't run any more user instructions ever again.
+ * But for utrace_control and utrace_set_events to be robust, they
+ * must be sure whether or not we will run any more callbacks. If
+ * a call comes in before we do, taking the lock here synchronizes
+ * us so we don't run any callbacks just disabled. Calls that come
+ * in while we're running the callbacks will see the exit.death
+ * flag and know that we are not yet fully quiescent for purposes
+ * of detach bookkeeping.
+ */
+ spin_lock(&utrace->lock);
+ BUG_ON(utrace->death);
+ utrace->death = 1;
+ utrace->resume = UTRACE_RESUME;
+ splice_attaching(utrace);
+ spin_unlock(&utrace->lock);
+
+ REPORT_CALLBACKS(, task, utrace, &report, UTRACE_EVENT(DEATH),
+ report_death, engine, group_dead, signal);
+
+ utrace_maybe_reap(task, utrace, false);
+}
+
+/*
+ * Finish the last reporting pass before returning to user mode.
+ */
+static void finish_resume_report(struct task_struct *task,
+ struct utrace *utrace,
+ struct utrace_report *report)
+{
+ finish_report_reset(task, utrace, report);
+
+ switch (report->action) {
+ case UTRACE_STOP:
+ utrace_stop(task, utrace, report->resume_action);
+ break;
+
+ case UTRACE_INTERRUPT:
+ if (!signal_pending(task))
+ set_tsk_thread_flag(task, TIF_SIGPENDING);
+ break;
+
+ case UTRACE_BLOCKSTEP:
+ if (likely(arch_has_block_step())) {
+ user_enable_block_step(task);
+ break;
+ }
+
+ /*
+ * This means some callback is to blame for failing
+ * to check arch_has_block_step() itself. Warn and
+ * then fall through to treat it as SINGLESTEP.
+ */
+ WARN(1, "UTRACE_BLOCKSTEP when !arch_has_block_step()");
+
+ case UTRACE_SINGLESTEP:
+ if (likely(arch_has_single_step())) {
+ user_enable_single_step(task);
+ } else {
+ /*
+ * This means some callback is to blame for failing
+ * to check arch_has_single_step() itself. Spew
+ * about it so the loser will fix his module.
+ */
+ WARN(1,
+ "UTRACE_SINGLESTEP when !arch_has_single_step()");
+ }
+ break;
+
+ case UTRACE_REPORT:
+ case UTRACE_RESUME:
+ default:
+ user_disable_single_step(task);
+ break;
+ }
+}
+
+/*
+ * This is called when TIF_NOTIFY_RESUME had been set (and is now clear).
+ * We are close to user mode, and this is the place to report or stop.
+ * When we return, we're going to user mode or into the signals code.
+ */
+void utrace_resume(struct task_struct *task, struct pt_regs *regs)
+{
+ struct utrace *utrace = task_utrace_struct(task);
+ INIT_REPORT(report);
+ struct utrace_engine *engine;
+
+ /*
+ * Some machines get here with interrupts disabled. The same arch
+ * code path leads to calling into get_signal_to_deliver(), which
+ * implicitly reenables them by virtue of spin_unlock_irq.
+ */
+ local_irq_enable();
+
+ /*
+ * If this flag is still set it's because there was a signal
+ * handler setup done but no report_signal following it. Clear
+ * the flag before we get to user so it doesn't confuse us later.
+ */
+ if (unlikely(utrace->signal_handler)) {
+ spin_lock(&utrace->lock);
+ utrace->signal_handler = 0;
+ spin_unlock(&utrace->lock);
+ }
+
+ /*
+ * Update our bookkeeping even if there are no callbacks made here.
+ */
+ report.action = start_report(utrace);
+
+ switch (report.action) {
+ case UTRACE_RESUME:
+ /*
+ * Anything we might have done was already handled by
+ * utrace_get_signal(), or this is an entirely spurious
+ * call. (The arch might use TIF_NOTIFY_RESUME for other
+ * purposes as well as calling us.)
+ */
+ return;
+ case UTRACE_REPORT:
+ if (unlikely(!(task->utrace_flags & UTRACE_EVENT(QUIESCE))))
+ break;
+ /*
+ * Do a simple reporting pass, with no specific
+ * callback after report_quiesce.
+ */
+ report.action = UTRACE_RESUME;
+ list_for_each_entry(engine, &utrace->attached, entry)
+ start_callback(utrace, &report, engine, task, 0);
+ break;
+ default:
+ /*
+ * Even if this report was truly spurious, there is no need
+ * for utrace_reset() now. TIF_NOTIFY_RESUME was already
+ * cleared--it doesn't stay spuriously set.
+ */
+ report.spurious = false;
+ break;
+ }
+
+ /*
+ * Finish the report and either stop or get ready to resume.
+ * If utrace->resume was not UTRACE_REPORT, this applies its
+ * effect now (i.e. step or interrupt).
+ */
+ finish_resume_report(task, utrace, &report);
+}
+
+/*
+ * Return true if current has forced signal_pending().
+ *
+ * This is called only when current->utrace_flags is nonzero, so we know
+ * that current->utrace must be set. It's not inlined in tracehook.h
+ * just so that struct utrace can stay opaque outside this file.
+ */
+bool utrace_interrupt_pending(void)
+{
+ return task_utrace_struct(current)->resume == UTRACE_INTERRUPT;
+}
+
+/*
+ * Take the siglock and push @info back on our queue.
+ * Returns with @task->sighand->siglock held.
+ */
+static void push_back_signal(struct task_struct *task, siginfo_t *info)
+ __acquires(task->sighand->siglock)
+{
+ struct sigqueue *q;
+
+ if (unlikely(!info->si_signo)) { /* Oh, a wise guy! */
+ spin_lock_irq(&task->sighand->siglock);
+ return;
+ }
+
+ q = sigqueue_alloc();
+ if (likely(q)) {
+ q->flags = 0;
+ copy_siginfo(&q->info, info);
+ }
+
+ spin_lock_irq(&task->sighand->siglock);
+
+ sigaddset(&task->pending.signal, info->si_signo);
+ if (likely(q))
+ list_add(&q->list, &task->pending.list);
+
+ set_tsk_thread_flag(task, TIF_SIGPENDING);
+}
+
+/*
+ * This is the hook from the signals code, called with the siglock held.
+ * Here is the ideal place to stop. We also dequeue and intercept signals.
+ */
+int utrace_get_signal(struct task_struct *task, struct pt_regs *regs,
+ siginfo_t *info, struct k_sigaction *return_ka)
+ __releases(task->sighand->siglock)
+ __acquires(task->sighand->siglock)
+{
+ struct utrace *utrace;
+ struct k_sigaction *ka;
+ INIT_REPORT(report);
+ struct utrace_engine *engine;
+ const struct utrace_engine_ops *ops;
+ unsigned long event, want;
+ u32 ret;
+ int signr;
+
+ utrace = task_utrace_struct(task);
+ if (utrace->resume < UTRACE_RESUME ||
+ utrace->pending_attach || utrace->signal_handler) {
+ enum utrace_resume_action resume;
+
+ /*
+ * We've been asked for an explicit report before we
+ * even check for pending signals.
+ */
+
+ spin_unlock_irq(&task->sighand->siglock);
+
+ spin_lock(&utrace->lock);
+
+ splice_attaching(utrace);
+
+ report.result = utrace->signal_handler ?
+ UTRACE_SIGNAL_HANDLER : UTRACE_SIGNAL_REPORT;
+ utrace->signal_handler = 0;
+
+ resume = utrace->resume;
+ utrace->resume = UTRACE_RESUME;
+
+ spin_unlock(&utrace->lock);
+
+ /*
+ * Make sure signal_pending() only returns true
+ * if there are real signals pending.
+ */
+ if (signal_pending(task)) {
+ spin_lock_irq(&task->sighand->siglock);
+ recalc_sigpending();
+ spin_unlock_irq(&task->sighand->siglock);
+ }
+
+ if (resume > UTRACE_REPORT) {
+ /*
+ * We only got here to process utrace->resume.
+ * Despite no callbacks, this report is not spurious.
+ */
+ report.action = resume;
+ report.spurious = false;
+ finish_resume_report(task, utrace, &report);
+ return -1;
+ } else if (!(task->utrace_flags & UTRACE_EVENT(QUIESCE))) {
+ /*
+ * We only got here to clear utrace->signal_handler.
+ */
+ return -1;
+ }
+
+ /*
+ * Do a reporting pass for no signal, just for EVENT(QUIESCE).
+ * The engine callbacks can fill in *info and *return_ka.
+ * We'll pass NULL for the @orig_ka argument to indicate
+ * that there was no original signal.
+ */
+ event = 0;
+ ka = NULL;
+ memset(return_ka, 0, sizeof *return_ka);
+ } else if (!(task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) ||
+ unlikely(task->signal->group_stop_count)) {
+ /*
+ * If no engine is interested in intercepting signals or
+ * we must stop, let the caller just dequeue them normally
+ * or participate in group-stop.
+ */
+ return 0;
+ } else {
+ /*
+ * Steal the next signal so we can let tracing engines
+ * examine it. From the signal number and sigaction,
+ * determine what normal delivery would do. If no
+ * engine perturbs it, we'll do that by returning the
+ * signal number after setting *return_ka.
+ */
+ signr = dequeue_signal(task, &task->blocked, info);
+ if (signr == 0)
+ return signr;
+ BUG_ON(signr != info->si_signo);
+
+ ka = &task->sighand->action[signr - 1];
+ *return_ka = *ka;
+
+ /*
+ * We are never allowed to interfere with SIGKILL.
+ * Just punt after filling in *return_ka for our caller.
+ */
+ if (signr == SIGKILL)
+ return signr;
+
+ if (ka->sa.sa_handler == SIG_IGN) {
+ event = UTRACE_EVENT(SIGNAL_IGN);
+ report.result = UTRACE_SIGNAL_IGN;
+ } else if (ka->sa.sa_handler != SIG_DFL) {
+ event = UTRACE_EVENT(SIGNAL);
+ report.result = UTRACE_SIGNAL_DELIVER;
+ } else if (sig_kernel_coredump(signr)) {
+ event = UTRACE_EVENT(SIGNAL_CORE);
+ report.result = UTRACE_SIGNAL_CORE;
+ } else if (sig_kernel_ignore(signr)) {
+ event = UTRACE_EVENT(SIGNAL_IGN);
+ report.result = UTRACE_SIGNAL_IGN;
+ } else if (signr == SIGSTOP) {
+ event = UTRACE_EVENT(SIGNAL_STOP);
+ report.result = UTRACE_SIGNAL_STOP;
+ } else if (sig_kernel_stop(signr)) {
+ event = UTRACE_EVENT(SIGNAL_STOP);
+ report.result = UTRACE_SIGNAL_TSTP;
+ } else {
+ event = UTRACE_EVENT(SIGNAL_TERM);
+ report.result = UTRACE_SIGNAL_TERM;
+ }
+
+ /*
+ * Now that we know what event type this signal is, we
+ * can short-circuit if no engines care about those.
+ */
+ if ((task->utrace_flags & (event | UTRACE_EVENT(QUIESCE))) == 0)
+ return signr;
+
+ /*
+ * We have some interested engines, so tell them about
+ * the signal and let them change its disposition.
+ */
+ spin_unlock_irq(&task->sighand->siglock);
+ }
+
+ /*
+ * This reporting pass chooses what signal disposition we'll act on.
+ */
+ list_for_each_entry(engine, &utrace->attached, entry) {
+ /*
+ * See start_callback() comment about this barrier.
+ */
+ utrace->reporting = engine;
+ smp_mb();
+
+ /*
+ * This pairs with the barrier in mark_engine_detached(),
+ * see start_callback() comments.
+ */
+ want = engine->flags;
+ smp_rmb();
+ ops = engine->ops;
+
+ if ((want & (event | UTRACE_EVENT(QUIESCE))) == 0) {
+ utrace->reporting = NULL;
+ continue;
+ }
+
+ if (ops->report_signal)
+ ret = (*ops->report_signal)(
+ report.result | report.action, engine,
+ regs, info, ka, return_ka);
+ else
+ ret = (report.result | (*ops->report_quiesce)(
+ report.action, engine, event));
+
+ /*
+ * Avoid a tight loop reporting again and again if some
+ * engine is too stupid.
+ */
+ switch (utrace_resume_action(ret)) {
+ default:
+ break;
+ case UTRACE_INTERRUPT:
+ case UTRACE_REPORT:
+ ret = (ret & ~UTRACE_RESUME_MASK) | UTRACE_RESUME;
+ break;
+ }
+
+ finish_callback(task, utrace, &report, engine, ret);
+ }
+
+ /*
+ * We express the chosen action to the signals code in terms
+ * of a representative signal whose default action does it.
+ * Our caller uses our return value (signr) to decide what to
+ * do, but uses info->si_signo as the signal number to report.
+ */
+ switch (utrace_signal_action(report.result)) {
+ case UTRACE_SIGNAL_TERM:
+ signr = SIGTERM;
+ break;
+
+ case UTRACE_SIGNAL_CORE:
+ signr = SIGQUIT;
+ break;
+
+ case UTRACE_SIGNAL_STOP:
+ signr = SIGSTOP;
+ break;
+
+ case UTRACE_SIGNAL_TSTP:
+ signr = SIGTSTP;
+ break;
+
+ case UTRACE_SIGNAL_DELIVER:
+ signr = info->si_signo;
+
+ if (return_ka->sa.sa_handler == SIG_DFL) {
+ /*
+ * We'll do signr's normal default action.
+ * For ignore, we'll fall through below.
+ * For stop/death, break locks and returns it.
+ */
+ if (likely(signr) && !sig_kernel_ignore(signr))
+ break;
+ } else if (return_ka->sa.sa_handler != SIG_IGN &&
+ likely(signr)) {
+ /*
+ * Complete the bookkeeping after the report.
+ * The handler will run. If an engine wanted to
+ * stop or step, then make sure we do another
+ * report after signal handler setup.
+ */
+ if (report.action != UTRACE_RESUME)
+ report.action = UTRACE_INTERRUPT;
+ finish_report(task, utrace, &report, true);
+
+ if (unlikely(report.result & UTRACE_SIGNAL_HOLD))
+ push_back_signal(task, info);
+ else
+ spin_lock_irq(&task->sighand->siglock);
+
+ /*
+ * We do the SA_ONESHOT work here since the
+ * normal path will only touch *return_ka now.
+ */
+ if (unlikely(return_ka->sa.sa_flags & SA_ONESHOT)) {
+ return_ka->sa.sa_flags &= ~SA_ONESHOT;
+ if (likely(valid_signal(signr))) {
+ ka = &task->sighand->action[signr - 1];
+ ka->sa.sa_handler = SIG_DFL;
+ }
+ }
+
+ return signr;
+ }
+
+ /* Fall through for an ignored signal. */
+
+ case UTRACE_SIGNAL_IGN:
+ case UTRACE_SIGNAL_REPORT:
+ default:
+ /*
+ * If the signal is being ignored, then we are on the way
+ * directly back to user mode. We can stop here, or step,
+ * as in utrace_resume(), above. After we've dealt with that,
+ * our caller will relock and come back through here.
+ */
+ finish_resume_report(task, utrace, &report);
+
+ if (unlikely(fatal_signal_pending(task))) {
+ /*
+ * The only reason we woke up now was because of a
+ * SIGKILL. Don't do normal dequeuing in case it
+ * might get a signal other than SIGKILL. That would
+ * perturb the death state so it might differ from
+ * what the debugger would have allowed to happen.
+ * Instead, pluck out just the SIGKILL to be sure
+ * we'll die immediately with nothing else different
+ * from the quiescent state the debugger wanted us in.
+ */
+ sigset_t sigkill_only;
+ siginitsetinv(&sigkill_only, sigmask(SIGKILL));
+ spin_lock_irq(&task->sighand->siglock);
+ signr = dequeue_signal(task, &sigkill_only, info);
+ BUG_ON(signr != SIGKILL);
+ *return_ka = task->sighand->action[SIGKILL - 1];
+ return signr;
+ }
+
+ if (unlikely(report.result & UTRACE_SIGNAL_HOLD)) {
+ push_back_signal(task, info);
+ spin_unlock_irq(&task->sighand->siglock);
+ }
+
+ return -1;
+ }
+
+ /*
+ * Complete the bookkeeping after the report.
+ * This sets utrace->resume if UTRACE_STOP was used.
+ */
+ finish_report(task, utrace, &report, true);
+
+ return_ka->sa.sa_handler = SIG_DFL;
+
+ /*
+ * If this signal is fatal, si_signo gets through as exit_code.
+ * We can't allow a completely bogus value there or else core
+ * kernel code can freak out. (If an engine wants to control
+ * the exit_code value exactly, it can do so in report_exit.)
+ * We'll produce a big complaint in dmesg, but won't crash.
+ * That's nicer for debugging your utrace engine.
+ */
+ if (unlikely(info->si_signo & 0x80)) {
+ WARN(1, "utrace engine left bogus si_signo value!");
+ info->si_signo = SIGTRAP;
+ }
+
+ if (unlikely(report.result & UTRACE_SIGNAL_HOLD))
+ push_back_signal(task, info);
+ else
+ spin_lock_irq(&task->sighand->siglock);
+
+ if (sig_kernel_stop(signr))
+ task->jobctl |= JOBCTL_STOP_DEQUEUED;
+
+ return signr;
+}
+
+/*
+ * This gets called after a signal handler has been set up.
+ * We set a flag so the next report knows it happened.
+ * If we're already stepping, make sure we do a report_signal.
+ * If not, make sure we get into utrace_resume() where we can
+ * clear the signal_handler flag before resuming.
+ */
+void utrace_signal_handler(struct task_struct *task, int stepping)
+{
+ struct utrace *utrace = task_utrace_struct(task);
+
+ spin_lock(&utrace->lock);
+
+ utrace->signal_handler = 1;
+ if (utrace->resume > UTRACE_INTERRUPT) {
+ if (stepping) {
+ utrace->resume = UTRACE_INTERRUPT;
+ set_tsk_thread_flag(task, TIF_SIGPENDING);
+ } else if (utrace->resume == UTRACE_RESUME) {
+ set_tsk_thread_flag(task, TIF_NOTIFY_RESUME);
+ }
+ }
+
+ spin_unlock(&utrace->lock);
+}
+
+/**
+ * utrace_prepare_examine - prepare to examine thread state
+ * @target: thread of interest, a &struct task_struct pointer
+ * @engine: engine pointer returned by utrace_attach_task()
+ * @exam: temporary state, a &struct utrace_examiner pointer
+ *
+ * This call prepares to safely examine the thread @target using
+ * &struct user_regset calls, or direct access to thread-synchronous fields.
+ *
+ * When @target is current, this call is superfluous. When @target is
+ * another thread, it must be held stopped via %UTRACE_STOP by @engine.
+ *
+ * This call may block the caller until @target stays stopped, so it must
+ * be called only after the caller is sure @target is about to unschedule.
+ * This means a zero return from a utrace_control() call on @engine giving
+ * %UTRACE_STOP, or a report_quiesce() or report_signal() callback to
+ * @engine that used %UTRACE_STOP in its return value.
+ *
+ * Returns -%ESRCH if @target is dead or -%EINVAL if %UTRACE_STOP was
+ * not used. If @target has started running again despite %UTRACE_STOP
+ * (for %SIGKILL or a spurious wakeup), this call returns -%EAGAIN.
+ *
+ * When this call returns zero, it's safe to use &struct user_regset
+ * calls and task_user_regset_view() on @target and to examine some of
+ * its fields directly. When the examination is complete, a
+ * utrace_finish_examine() call must follow to check whether it was
+ * completed safely.
+ */
+int utrace_prepare_examine(struct task_struct *target,
+ struct utrace_engine *engine,
+ struct utrace_examiner *exam)
+{
+ int ret = 0;
+
+ if (unlikely(target == current))
+ return 0;
+
+ rcu_read_lock();
+ if (unlikely(!engine_wants_stop(engine)))
+ ret = -EINVAL;
+ else if (unlikely(target->exit_state))
+ ret = -ESRCH;
+ else {
+ exam->state = target->state;
+ if (unlikely(exam->state == TASK_RUNNING))
+ ret = -EAGAIN;
+ else
+ get_task_struct(target);
+ }
+ rcu_read_unlock();
+
+ if (likely(!ret)) {
+ exam->ncsw = wait_task_inactive(target, exam->state);
+ put_task_struct(target);
+ if (unlikely(!exam->ncsw))
+ ret = -EAGAIN;
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(utrace_prepare_examine);
+
+/**
+ * utrace_finish_examine - complete an examination of thread state
+ * @target: thread of interest, a &struct task_struct pointer
+ * @engine: engine pointer returned by utrace_attach_task()
+ * @exam: pointer passed to utrace_prepare_examine() call
+ *
+ * This call completes an examination on the thread @target begun by a
+ * paired utrace_prepare_examine() call with the same arguments that
+ * returned success (zero).
+ *
+ * When @target is current, this call is superfluous. When @target is
+ * another thread, this returns zero if @target has remained unscheduled
+ * since the paired utrace_prepare_examine() call returned zero.
+ *
+ * When this returns an error, any examination done since the paired
+ * utrace_prepare_examine() call is unreliable and the data extracted
+ * should be discarded. The error is -%EINVAL if @engine is not
+ * keeping @target stopped, or -%EAGAIN if @target woke up unexpectedly.
+ */
+int utrace_finish_examine(struct task_struct *target,
+ struct utrace_engine *engine,
+ struct utrace_examiner *exam)
+{
+ int ret = 0;
+
+ if (unlikely(target == current))
+ return 0;
+
+ rcu_read_lock();
+ if (unlikely(!engine_wants_stop(engine)))
+ ret = -EINVAL;
+ else if (unlikely(target->state != exam->state))
+ ret = -EAGAIN;
+ else
+ get_task_struct(target);
+ rcu_read_unlock();
+
+ if (likely(!ret)) {
+ unsigned long ncsw = wait_task_inactive(target, exam->state);
+ if (unlikely(ncsw != exam->ncsw))
+ ret = -EAGAIN;
+ put_task_struct(target);
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(utrace_finish_examine);
+
+/*
+ * This is declared in linux/regset.h and defined in machine-dependent
+ * code. We put the export here to ensure no machine forgets it.
+ */
+EXPORT_SYMBOL_GPL(task_user_regset_view);
+
+/*
+ * Called with rcu_read_lock() held.
+ */
+void task_utrace_proc_status(struct seq_file *m, struct task_struct *p)
+{
+ seq_printf(m, "Utrace:\t%lx\n", p->utrace_flags);
+}
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:17 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:17 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 1CCED128D6B;
Mon, 21 Nov 2011 15:06:17 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id FWdJDV74o-ay; Mon, 21 Nov 2011 15:06:17 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 082081285E4;
Mon, 21 Nov 2011 15:06:17 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6E7d015251;
Mon, 21 Nov 2011 15:06:15 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:35 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:33 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 02/33] utrace: add utrace_init_task/utrace_free_task calls
Message-ID: <20111121200133.GA27759@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 1295
Lines: 47
Add the necessary copy_process()->utrace_init_task() and
free_task()->utrace_free_task() calls.
Originally this was the part of "utrace core" patch, but since
tracehooks are dying it doesn't make sense to reintroduce them.
Instead, just call the utrace_ helpers directly. This is fine
even without CONFIG_UTRACE, gcc is smart enough to optimize out
the code in free_task().
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/fork.c | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/kernel/fork.c b/kernel/fork.c
index ba0d172..b8719c2 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -66,6 +66,7 @@
#include <linux/user-return-notifier.h>
#include <linux/oom.h>
#include <linux/khugepaged.h>
+#include <linux/utrace.h>
#include <asm/pgtable.h>
#include <asm/pgalloc.h>
@@ -167,6 +168,8 @@ void free_task(struct task_struct *tsk)
free_thread_info(tsk->stack);
rt_mutex_debug_task_free(tsk);
ftrace_graph_exit_task(tsk);
+ if (task_utrace_struct(tsk))
+ utrace_free_task(tsk);
free_task_struct(tsk);
}
EXPORT_SYMBOL(free_task);
@@ -1093,6 +1096,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
if (!p)
goto fork_out;
+ utrace_init_task(p);
+
ftrace_graph_init_task(p);
rt_mutex_init_task(p);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:20 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:20 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8779AD87BC;
Mon, 21 Nov 2011 15:06:20 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id umzAMwRd7rMt; Mon, 21 Nov 2011 15:06:20 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 72C98D8707;
Mon, 21 Nov 2011 15:06:20 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6HAj015261;
Mon, 21 Nov 2011 15:06:18 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:39 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:36 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 03/33] tracehooks: add utrace hooks
Message-ID: <20111121200136.GA27766@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 2310
Lines: 75
Add the necessary utrace hooks in the tracehooks which were not
removed yet.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/tracehook.h | 22 +++++++++++++++++++++-
1 files changed, 21 insertions(+), 1 deletions(-)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index a71a292..8cc28bc 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -49,6 +49,7 @@
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/security.h>
+#include <linux/utrace.h>
struct linux_binprm;
/*
@@ -96,6 +97,9 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
static inline __must_check int tracehook_report_syscall_entry(
struct pt_regs *regs)
{
+ if ((task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_ENTRY)) &&
+ utrace_report_syscall_entry(regs))
+ return 1;
ptrace_report_syscall(regs);
return 0;
}
@@ -119,6 +123,9 @@ static inline __must_check int tracehook_report_syscall_entry(
*/
static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
{
+ if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT))
+ utrace_report_syscall_exit(regs);
+
if (step) {
siginfo_t info;
user_single_step_siginfo(current, regs, &info);
@@ -148,6 +155,8 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
const struct k_sigaction *ka,
struct pt_regs *regs, int stepping)
{
+ if (task_utrace_flags(current))
+ utrace_signal_handler(current, stepping);
if (stepping)
ptrace_notify(SIGTRAP);
}
@@ -179,10 +188,21 @@ static inline void set_notify_resume(struct task_struct *task)
* asynchronously, this will be called again before we return to
* user mode.
*
- * Called without locks.
+ * Called without locks. However, on some machines this may be
+ * called with interrupts disabled.
*/
static inline void tracehook_notify_resume(struct pt_regs *regs)
{
+ struct task_struct *task = current;
+ /*
+ * Prevent the following store/load from getting ahead of the
+ * caller which clears TIF_NOTIFY_RESUME. This pairs with the
+ * implicit mb() before setting TIF_NOTIFY_RESUME in
+ * set_notify_resume().
+ */
+ smp_mb();
+ if (task_utrace_flags(task))
+ utrace_resume(task, regs);
}
#endif /* TIF_NOTIFY_RESUME */
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:23 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:23 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6559BD4FDE;
Mon, 21 Nov 2011 15:06:23 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 6BjISiO+cS1U; Mon, 21 Nov 2011 15:06:23 -0500 (EST)
Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 50366D4C39;
Mon, 21 Nov 2011 15:06:23 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6KT9017533;
Mon, 21 Nov 2011 15:06:21 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:42 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:39 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 04/33] tracehooks: reintroduce
tracehook_consider_fatal_signal()
Message-ID: <20111121200139.GA27769@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
Status: RO
Content-Length: 3257
Lines: 90
Add the killed tracehook_consider_fatal_signal() back. It has multiple
callers and it is not easy add the necessary checks inline.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
arch/s390/kernel/traps.c | 4 ++--
include/linux/tracehook.h | 22 ++++++++++++++++++++++
kernel/signal.c | 4 ++--
3 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/arch/s390/kernel/traps.c b/arch/s390/kernel/traps.c
index a9807dd..f506e1b 100644
--- a/arch/s390/kernel/traps.c
+++ b/arch/s390/kernel/traps.c
@@ -329,7 +329,7 @@ void __kprobes do_per_trap(struct pt_regs *regs)
if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
return;
- if (!current->ptrace)
+ if (!tracehook_consider_fatal_signal(current, SIGTRAP))
return;
info.si_signo = SIGTRAP;
info.si_errno = 0;
@@ -429,7 +429,7 @@ static void __kprobes illegal_op(struct pt_regs *regs, long pgm_int_code,
if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
return;
if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
- if (current->ptrace) {
+ if (tracehook_consider_fatal_signal(current, SIGTRAP)) {
info.si_signo = SIGTRAP;
info.si_errno = 0;
info.si_code = TRAP_BRKPT;
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index 8cc28bc..ec2af67 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -161,6 +161,28 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
ptrace_notify(SIGTRAP);
}
+/**
+ * tracehook_consider_fatal_signal - suppress special handling of fatal signal
+ * @task: task receiving the signal
+ * @sig: signal number being sent
+ *
+ * Return nonzero to prevent special handling of this termination signal.
+ * Normally handler for signal is %SIG_DFL. It can be %SIG_IGN if @sig is
+ * ignored, in which case force_sig() is about to reset it to %SIG_DFL.
+ * When this returns zero, this signal might cause a quick termination
+ * that does not give the debugger a chance to intercept the signal.
+ *
+ * Called with or without @task->sighand->siglock held.
+ */
+static inline int tracehook_consider_fatal_signal(struct task_struct *task,
+ int sig)
+{
+ if (unlikely(task_utrace_flags(task) & (UTRACE_EVENT(SIGNAL_TERM) |
+ UTRACE_EVENT(SIGNAL_CORE))))
+ return 1;
+ return task->ptrace != 0;
+}
+
#ifdef TIF_NOTIFY_RESUME
/**
* set_notify_resume - cause tracehook_notify_resume() to be called
diff --git a/kernel/signal.c b/kernel/signal.c
index b3f78d0..d7b90cd 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -494,7 +494,7 @@ int unhandled_signal(struct task_struct *tsk, int sig)
if (handler != SIG_IGN && handler != SIG_DFL)
return 0;
/* if ptraced, let the tracer determine */
- return !tsk->ptrace;
+ return !tracehook_consider_fatal_signal(tsk, sig);
}
/*
@@ -982,7 +982,7 @@ static void complete_signal(int sig, struct task_struct *p, int group)
if (sig_fatal(p, sig) &&
!(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
!sigismember(&t->real_blocked, sig) &&
- (sig == SIGKILL || !t->ptrace)) {
+ (sig == SIGKILL || !tracehook_consider_fatal_signal(t, sig))) {
/*
* This signal will be fatal to the whole group.
*/
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:26 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:25 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id E7A2DD498A;
Mon, 21 Nov 2011 15:06:25 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 4L9zskFVsr4I; Mon, 21 Nov 2011 15:06:25 -0500 (EST)
Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id D3487D4C39;
Mon, 21 Nov 2011 15:06:25 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6Nnp017552;
Mon, 21 Nov 2011 15:06:24 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:44 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:42 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 05/33] add utrace hooks into sig_ignored() and
recalc_sigpending()
Message-ID: <20111121200142.GA27777@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
Status: RO
Content-Length: 1508
Lines: 51
Add the necessary and somewhat "special" hooks into sig_ignored() and
recalc_sigpending(). Basically this restores _force_sigpending() and
_consider_ignored_signal() tracehook logic.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/utrace.h | 2 ++
kernel/signal.c | 7 ++++++-
2 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index f251efe..1b8da1c 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -107,6 +107,8 @@ bool utrace_report_syscall_entry(struct pt_regs *);
void utrace_report_syscall_exit(struct pt_regs *);
void utrace_signal_handler(struct task_struct *, int);
+#define UTRACE_FLAG(task, ev) (task_utrace_flags(task) & UTRACE_EVENT(ev))
+
#ifndef CONFIG_UTRACE
/*
diff --git a/kernel/signal.c b/kernel/signal.c
index d7b90cd..8594cb2 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -87,7 +87,7 @@ static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
/*
* Tracers may want to know about even ignored signals.
*/
- return !t->ptrace;
+ return !t->ptrace && !UTRACE_FLAG(t, SIGNAL_IGN);
}
/*
@@ -150,6 +150,11 @@ void recalc_sigpending_and_wake(struct task_struct *t)
void recalc_sigpending(void)
{
+ if (task_utrace_flags(current) && utrace_interrupt_pending()) {
+ set_thread_flag(TIF_SIGPENDING);
+ return;
+ }
+
if (!recalc_sigpending_tsk(current) && !freezing(current))
clear_thread_flag(TIF_SIGPENDING);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:28 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:28 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6E4A6D4F4A;
Mon, 21 Nov 2011 15:06:28 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id vEeGLyK1+rX9; Mon, 21 Nov 2011 15:06:28 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 5AD50D4C39;
Mon, 21 Nov 2011 15:06:28 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6QOB029543;
Mon, 21 Nov 2011 15:06:26 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:47 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:45 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 06/33] restore the EXEC/EXIT/CLONE utrace hooks
Message-ID: <20111121200145.GA27780@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 2383
Lines: 83
Restore the "trivial" EXEC/EXIT/CLONE utrace hooks. Add the
simple helper, UTRACE_HOOK(), to minimize the changes.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
fs/exec.c | 5 ++++-
include/linux/utrace.h | 6 ++++++
kernel/exit.c | 1 +
kernel/fork.c | 4 ++++
4 files changed, 15 insertions(+), 1 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index 3625464..7d8f8bd 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1397,9 +1397,12 @@ int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
*/
bprm->recursion_depth = depth;
if (retval >= 0) {
- if (depth == 0)
+ if (depth == 0) {
+ UTRACE_HOOK(current, EXEC,
+ report_exec(fmt, bprm, regs));
ptrace_event(PTRACE_EVENT_EXEC,
old_pid);
+ }
put_binfmt(fmt);
allow_write_access(bprm->file);
if (bprm->file)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index 1b8da1c..9ac0b1b 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -109,6 +109,12 @@ void utrace_signal_handler(struct task_struct *, int);
#define UTRACE_FLAG(task, ev) (task_utrace_flags(task) & UTRACE_EVENT(ev))
+#define UTRACE_HOOK(task, ev, callback) \
+ do { \
+ if (UTRACE_FLAG(task, ev)) \
+ utrace_ ## callback; \
+ } while (0)
+
#ifndef CONFIG_UTRACE
/*
diff --git a/kernel/exit.c b/kernel/exit.c
index d0b7d98..f5eba3b 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -911,6 +911,7 @@ NORET_TYPE void do_exit(long code)
*/
set_fs(USER_DS);
+ UTRACE_HOOK(current, EXIT, report_exit(&code));
ptrace_event(PTRACE_EVENT_EXIT, code);
validate_creds_for_do_exit(tsk);
diff --git a/kernel/fork.c b/kernel/fork.c
index b8719c2..09d5049 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1535,6 +1535,8 @@ long do_fork(unsigned long clone_flags,
audit_finish_fork(p);
+ UTRACE_HOOK(current, CLONE, report_clone(clone_flags, p));
+
/*
* We set PF_STARTING at creation in case tracing wants to
* use this to distinguish a fully live task from one that
@@ -1546,6 +1548,8 @@ long do_fork(unsigned long clone_flags,
wake_up_new_task(p);
/* forking complete and child started to run, tell ptracer */
+ if (clone_flags & CLONE_VFORK)
+ UTRACE_HOOK(current, CLONE, finish_vfork(current));
if (unlikely(trace))
ptrace_event(trace, nr);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:30 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:30 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id D248D1284E0;
Mon, 21 Nov 2011 15:06:30 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id divvekTmLScS; Mon, 21 Nov 2011 15:06:30 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id BDC9412800E;
Mon, 21 Nov 2011 15:06:30 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6Shj029546;
Mon, 21 Nov 2011 15:06:29 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:49 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:47 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 07/33] utrace: utrace_report_death() can use
task_utrace_struct()
Message-ID: <20111121200147.GA27787@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 1692
Lines: 44
utrace_report_death() assumes that the caller (exit_notify) should
pass task->utrace as an argument. This is no longer needed, it can
safely do task_utrace_struct(). This way we avoid the nasty changes
in exit_notify().
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/utrace.h | 2 +-
kernel/utrace.c | 5 +++--
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index 9ac0b1b..9a2e2f4 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -99,7 +99,7 @@ int utrace_get_signal(struct task_struct *, struct pt_regs *,
void utrace_report_clone(unsigned long, struct task_struct *);
void utrace_finish_vfork(struct task_struct *);
void utrace_report_exit(long *exit_code);
-void utrace_report_death(struct task_struct *, struct utrace *, bool, int);
+void utrace_report_death(struct task_struct *, bool, int);
void utrace_report_jctl(int notify, int type);
void utrace_report_exec(struct linux_binfmt *, struct linux_binprm *,
struct pt_regs *regs);
diff --git a/kernel/utrace.c b/kernel/utrace.c
index ef856c9..1e750ad 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -1759,9 +1759,10 @@ void utrace_report_exit(long *exit_code)
* For this reason, utrace_release_task checks for the event bits that get
* us here, and delays its cleanup for us to do.
*/
-void utrace_report_death(struct task_struct *task, struct utrace *utrace,
- bool group_dead, int signal)
+void utrace_report_death(struct task_struct *task, bool group_dead, int signal)
{
+ struct utrace *utrace = task_utrace_struct(task);
+
INIT_REPORT(report);
BUG_ON(!task->exit_state);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:33 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:33 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 62E1BD50A3;
Mon, 21 Nov 2011 15:06:33 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id FB36YkOAHDc5; Mon, 21 Nov 2011 15:06:33 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4D161D4C39;
Mon, 21 Nov 2011 15:06:33 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6VBW022130;
Mon, 21 Nov 2011 15:06:31 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:52 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:50 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 08/33] restore the DEATH/REAP utrace hooks
Message-ID: <20111121200150.GA27790@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 2346
Lines: 70
Restore the necessary hooks in release_task() and exit_notify(),
add the corresponding helpers into utrace.h.
Note: the @signal argument passed to ->report_death() does not
match the previous behaviour. I think this shouldn't affect the
current users, and I bet nobody can really understand what this
magic argument should actually mean anyway.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/utrace.h | 22 ++++++++++++++++++++++
kernel/exit.c | 4 ++++
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index 9a2e2f4..cf13839 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -697,4 +697,26 @@ static inline __must_check int utrace_barrier_pid(struct pid *pid,
#endif /* CONFIG_UTRACE */
+static inline void utrace_release_task(struct task_struct *task)
+{
+ /* see utrace_add_engine() about this barrier */
+ smp_mb();
+ if (task_utrace_flags(task))
+ utrace_maybe_reap(task, task_utrace_struct(task), true);
+}
+
+static inline void utrace_exit_notify(struct task_struct *task,
+ int signal, int group_dead)
+{
+ /*
+ * If utrace_set_events() was just called to enable
+ * UTRACE_EVENT(DEATH), then we are obliged to call
+ * utrace_report_death() and not miss it. utrace_set_events()
+ * checks @task->exit_state under tasklist_lock to synchronize
+ * with exit_notify(), the caller.
+ */
+ if (task_utrace_flags(task) & _UTRACE_DEATH_EVENTS)
+ utrace_report_death(task, group_dead, signal);
+}
+
#endif /* linux/utrace.h */
diff --git a/kernel/exit.c b/kernel/exit.c
index f5eba3b..746c5df 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -168,6 +168,8 @@ void release_task(struct task_struct * p)
struct task_struct *leader;
int zap_leader;
repeat:
+ utrace_release_task(p);
+
/* don't need to get the RCU readlock here - the process is dead and
* can't be modifying its own credentials. But shut RCU-lockdep up */
rcu_read_lock();
@@ -858,6 +860,8 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
wake_up_process(tsk->signal->group_exit_task);
write_unlock_irq(&tasklist_lock);
+ utrace_exit_notify(tsk, autoreap ? -1 : SIGCHLD, group_dead);
+
/* If the process is dead, release it - nobody will wait for it */
if (autoreap)
release_task(tsk);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:35 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:35 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id D4AB3D8761;
Mon, 21 Nov 2011 15:06:35 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id TiShkH1f6rnj; Mon, 21 Nov 2011 15:06:35 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id BCB85D877F;
Mon, 21 Nov 2011 15:06:35 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6XNu015312;
Mon, 21 Nov 2011 15:06:34 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:54 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:52 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 09/33] utrace: remove jobctl bits
Message-ID: <20111121200152.GA27793@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 1888
Lines: 56
- change utrace_get_signal() to check JOBCTL_STOP_PENDING instead of
signal->group_stop_count. With the recent changes group_stop_count
doesn't necessarily mean this task should participate in group stop.
- remove the "participate in group stop" code from utrace_wakeup() and
utrace_stop(), this is no longer needed and wrong.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/utrace.c | 16 ++--------------
1 files changed, 2 insertions(+), 14 deletions(-)
diff --git a/kernel/utrace.c b/kernel/utrace.c
index 1e750ad..5d3974e 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -648,11 +648,7 @@ static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
{
lockdep_assert_held(&utrace->lock);
spin_lock_irq(&target->sighand->siglock);
- if (target->signal->flags & SIGNAL_STOP_STOPPED ||
- target->signal->group_stop_count)
- target->state = TASK_STOPPED;
- else
- wake_up_state(target, __TASK_TRACED);
+ wake_up_state(target, __TASK_TRACED);
spin_unlock_irq(&target->sighand->siglock);
}
@@ -805,14 +801,6 @@ relock:
__set_current_state(TASK_TRACED);
- /*
- * If there is a group stop in progress,
- * we must participate in the bookkeeping.
- */
- if (unlikely(task->signal->group_stop_count) &&
- !--task->signal->group_stop_count)
- task->signal->flags = SIGNAL_STOP_STOPPED;
-
spin_unlock_irq(&task->sighand->siglock);
spin_unlock(&utrace->lock);
@@ -2037,7 +2025,7 @@ int utrace_get_signal(struct task_struct *task, struct pt_regs *regs,
ka = NULL;
memset(return_ka, 0, sizeof *return_ka);
} else if (!(task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) ||
- unlikely(task->signal->group_stop_count)) {
+ unlikely(task->jobctl & JOBCTL_STOP_PENDING)) {
/*
* If no engine is interested in intercepting signals or
* we must stop, let the caller just dequeue them normally
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:38 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:38 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 425CED8707;
Mon, 21 Nov 2011 15:06:38 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 9paRC+dwSflh; Mon, 21 Nov 2011 15:06:38 -0500 (EST)
Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 2DDBCD8410;
Mon, 21 Nov 2011 15:06:38 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6a31031355;
Mon, 21 Nov 2011 15:06:36 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:56 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:55 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 10/33] ptrace: take ->siglock around s/TRACED/RUNNING/
Message-ID: <20111121200155.GA27801@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
Status: RO
Content-Length: 1624
Lines: 56
change ptrace_resume() and ptrace_stop() to take ->siglock around changing
task->state from TRACED to RUNNING.
With this patch __TASK_TRACED/STOPPED bits are fully protected by ->siglock,
nobody can set or clear these bits without ->siglock held.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/ptrace.c | 8 +++++++-
kernel/signal.c | 3 +++
2 files changed, 10 insertions(+), 1 deletions(-)
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 24d0447..daf47bc 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -589,6 +589,8 @@ static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
static int ptrace_resume(struct task_struct *child, long request,
unsigned long data)
{
+ unsigned long flags;
+
if (!valid_signal(data))
return -EIO;
@@ -617,7 +619,11 @@ static int ptrace_resume(struct task_struct *child, long request,
}
child->exit_code = data;
- wake_up_state(child, __TASK_TRACED);
+
+ if (lock_task_sighand(child, &flags)) {
+ wake_up_state(child, __TASK_TRACED);
+ unlock_task_sighand(child, &flags);
+ }
return 0;
}
diff --git a/kernel/signal.c b/kernel/signal.c
index 8594cb2..4512bb4 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -1886,7 +1886,10 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
if (gstop_done)
do_notify_parent_cldstop(current, false, why);
+ spin_lock_irq(¤t->sighand->siglock);
__set_current_state(TASK_RUNNING);
+ spin_unlock_irq(¤t->sighand->siglock);
+
if (clear_code)
current->exit_code = 0;
read_unlock(&tasklist_lock);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:40 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:40 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B0D19D877F;
Mon, 21 Nov 2011 15:06:40 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 3jv-cDBpMP5B; Mon, 21 Nov 2011 15:06:40 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 9C98DD8410;
Mon, 21 Nov 2011 15:06:40 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6cHF029573;
Mon, 21 Nov 2011 15:06:39 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:01:59 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:57 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 11/33] introduce wake_up_quiescent()
Message-ID: <20111121200157.GA27805@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 3186
Lines: 93
No functional changes. Add the new helper, wake_up_quiescent(task, state),
which simply returns wake_up_state(task, state). Change all callers which
do wake_up_state(STOPPED/TRACED) to use the new helper. ptrace_stop() is
a bit special, it does __set_current_state(RUNNING) in the very unlikely
case, change it as well.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/signal.h | 2 ++
kernel/ptrace.c | 2 +-
kernel/signal.c | 12 ++++++++++--
kernel/utrace.c | 2 +-
4 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/include/linux/signal.h b/include/linux/signal.h
index a822300..2be3712 100644
--- a/include/linux/signal.h
+++ b/include/linux/signal.h
@@ -239,6 +239,8 @@ static inline int valid_signal(unsigned long sig)
struct timespec;
struct pt_regs;
+extern int wake_up_quiescent(struct task_struct *p, unsigned int state);
+
extern int next_signal(struct sigpending *pending, sigset_t *mask);
extern int do_send_sig_info(int sig, struct siginfo *info,
struct task_struct *p, bool group);
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index daf47bc..8439ef1 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -621,7 +621,7 @@ static int ptrace_resume(struct task_struct *child, long request,
child->exit_code = data;
if (lock_task_sighand(child, &flags)) {
- wake_up_state(child, __TASK_TRACED);
+ wake_up_quiescent(child, __TASK_TRACED);
unlock_task_sighand(child, &flags);
}
diff --git a/kernel/signal.c b/kernel/signal.c
index 4512bb4..99a6008 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -702,6 +702,14 @@ void signal_wake_up(struct task_struct *t, int resume)
}
/*
+ * wakes up the STOPPED/TRACED task, must be called with ->siglock held.
+ */
+int wake_up_quiescent(struct task_struct *p, unsigned int state)
+{
+ return wake_up_state(p, state);
+}
+
+/*
* Remove signals in mask from the pending set and queue.
* Returns 1 if any signals were found.
*
@@ -888,7 +896,7 @@ static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
if (likely(!(t->ptrace & PT_SEIZED)))
- wake_up_state(t, __TASK_STOPPED);
+ wake_up_quiescent(t, __TASK_STOPPED);
else
ptrace_trap_notify(t);
} while_each_thread(p, t);
@@ -1887,7 +1895,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
do_notify_parent_cldstop(current, false, why);
spin_lock_irq(¤t->sighand->siglock);
- __set_current_state(TASK_RUNNING);
+ wake_up_quiescent(current, __TASK_TRACED);
spin_unlock_irq(¤t->sighand->siglock);
if (clear_code)
diff --git a/kernel/utrace.c b/kernel/utrace.c
index 5d3974e..cebc390 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -648,7 +648,7 @@ static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
{
lockdep_assert_held(&utrace->lock);
spin_lock_irq(&target->sighand->siglock);
- wake_up_state(target, __TASK_TRACED);
+ wake_up_quiescent(target, __TASK_TRACED);
spin_unlock_irq(&target->sighand->siglock);
}
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:43 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:43 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6488BD514C;
Mon, 21 Nov 2011 15:06:43 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id TKQUj5mU4NYM; Mon, 21 Nov 2011 15:06:43 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4EC82D5000;
Mon, 21 Nov 2011 15:06:43 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6fp6015343;
Mon, 21 Nov 2011 15:06:41 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:02 +0100 (CET)
Date: Mon, 21 Nov 2011 21:01:59 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 12/33] introduce ptrace_signal_wake_up()
Message-ID: <20111121200159.GA27812@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 3670
Lines: 113
Add the new helper, ptrace_signal_wake_up(), change ptrace.c/signal.c
to use it instead of signal_wake_up() to wake up a STOPPED/TRACED task.
The new helper does almost the same, except:
- it doesn't use the TASK_WAKEKILL bit to wake up the TRACED
or STOPPED task, it uses __TASK_STOPPED | __TASK_TRACED
explicitly. This is what ptrace actually wants, it should
never wake up a TASK_KILLABLE task.
This should be cleanuped upatream, signal_wake_up() should
take the state as an argument, not a boolean. Until then
we add a new static helper.
- it uses wake_up_quiescent() instead of wake_up_state().
Thereafter every change from STOPPED/TRACED to RUNNING is done via
wake_up_quiescent().
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/ptrace.h | 1 +
kernel/ptrace.c | 20 ++++++++++++++++----
kernel/signal.c | 2 +-
3 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
index 800f113..6d9282a 100644
--- a/include/linux/ptrace.h
+++ b/include/linux/ptrace.h
@@ -113,6 +113,7 @@
#include <linux/compiler.h> /* For unlikely. */
#include <linux/sched.h> /* For struct task_struct. */
+extern void ptrace_signal_wake_up(struct task_struct *p, int quiescent);
extern long arch_ptrace(struct task_struct *child, long request,
unsigned long addr, unsigned long data);
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 8439ef1..a464ab5 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -25,6 +25,18 @@
#include <linux/hw_breakpoint.h>
#include <linux/cn_proc.h>
+void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
+{
+ unsigned int state;
+
+ set_tsk_thread_flag(p, TIF_SIGPENDING);
+
+ state = TASK_INTERRUPTIBLE;
+ if (quiescent)
+ state |= (__TASK_STOPPED | __TASK_TRACED);
+ if (!wake_up_quiescent(p, state))
+ kick_process(p);
+}
static int ptrace_trapping_sleep_fn(void *flags)
{
@@ -106,7 +118,7 @@ void __ptrace_unlink(struct task_struct *child)
* TASK_KILLABLE sleeps.
*/
if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
- signal_wake_up(child, task_is_traced(child));
+ ptrace_signal_wake_up(child, task_is_traced(child));
spin_unlock(&child->sighand->siglock);
}
@@ -296,7 +308,7 @@ static int ptrace_attach(struct task_struct *task, long request,
*/
if (task_is_stopped(task) &&
task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
- signal_wake_up(task, 1);
+ ptrace_signal_wake_up(task, 1);
spin_unlock(&task->sighand->siglock);
@@ -731,7 +743,7 @@ int ptrace_request(struct task_struct *child, long request,
* tracee into STOP.
*/
if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
- signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
+ ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
unlock_task_sighand(child, &flags);
ret = 0;
@@ -757,7 +769,7 @@ int ptrace_request(struct task_struct *child, long request,
* start of this trap and now. Trigger re-trap.
*/
if (child->jobctl & JOBCTL_TRAP_NOTIFY)
- signal_wake_up(child, true);
+ ptrace_signal_wake_up(child, true);
ret = 0;
}
unlock_task_sighand(child, &flags);
diff --git a/kernel/signal.c b/kernel/signal.c
index 99a6008..7a47a93 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -854,7 +854,7 @@ static void ptrace_trap_notify(struct task_struct *t)
assert_spin_locked(&t->sighand->siglock);
task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
- signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
+ ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
}
/*
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:46 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:46 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 3A7AED8410;
Mon, 21 Nov 2011 15:06:46 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id jhYAwDyYmdhg; Mon, 21 Nov 2011 15:06:46 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 25D5FD889A;
Mon, 21 Nov 2011 15:06:46 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6h29029619;
Mon, 21 Nov 2011 15:06:44 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:04 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:02 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 13/33] wait_task_inactive: treat task->state and
match_state as bitmasks
Message-ID: <20111121200202.GA27816@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 1558
Lines: 39
Change wait_task_inactive() to check "state & match_state" instead of
"state == match_state". This should not make any difference, but this
allows us to add more "stopped" bits which can be set or cleared
independently.
IOW. wait_task_inactive() assumes that if task->state != 0, it can
only be changed to TASK_RUNNING. Currently this is true, and in this
case "state & match_state" continues to work. But, unlike the current
check, it also works if task->state has other bits set while the caller
is only interested in, say, __TASK_TRACED.
Note: I think wait_task_inactive() should be cleanuped upstrean anyway,
nowadays we have TASK_WAKING and task->state != 0 doesn't necessarily
mean it is TASK_RUNNING. It also makes sense to exclude the !TASK_REPORT
bits during the check. Finally, probably this patch makes sense anyway
even without utrace. For example, a stopped _and_ traced thread could
have task->state = TASK_STOPPED | TASK_TRACED, this can be useful.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/sched.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/kernel/sched.c b/kernel/sched.c
index 0e9344a..84ecc0a 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -2443,7 +2443,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
* is actually now running somewhere else!
*/
while (task_running(rq, p)) {
- if (match_state && unlikely(p->state != match_state))
+ if (match_state && !likely(p->state & match_state))
return 0;
cpu_relax();
}
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:48 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:48 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id C2934D5000;
Mon, 21 Nov 2011 15:06:48 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id ThT2PyhztDpQ; Mon, 21 Nov 2011 15:06:48 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id ACE02D4F84;
Mon, 21 Nov 2011 15:06:48 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6kdb022201;
Mon, 21 Nov 2011 15:06:47 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:07 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:05 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 14/33] introduce TASK_UTRACED state
Message-ID: <20111121200205.GA27823@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 2913
Lines: 84
Introduce TASK_UTRACED state, will be used by utrace instead of TASK_TRACED.
Note: this state is reported as "t (tracing stop)" to the user-space to
avoid the confusion. IOW, it looks like TASK_TRACED in /proc/pid/status.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
fs/proc/array.c | 11 ++++++-----
include/linux/sched.h | 20 +++++++++++---------
2 files changed, 17 insertions(+), 14 deletions(-)
diff --git a/fs/proc/array.c b/fs/proc/array.c
index f0c0ea2..e0daec4 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -138,11 +138,12 @@ static const char * const task_state_array[] = {
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
- "Z (zombie)", /* 16 */
- "X (dead)", /* 32 */
- "x (dead)", /* 64 */
- "K (wakekill)", /* 128 */
- "W (waking)", /* 256 */
+ "t (tracing stop)", /* 16 (stopped by utrace) */
+ "Z (zombie)", /* 32 */
+ "X (dead)", /* 64 */
+ "x (dead)", /* 128 */
+ "K (wakekill)", /* 256 */
+ "W (waking)", /* 512 */
};
static inline const char *get_task_state(struct task_struct *tsk)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 4d45f93..746b4d3 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -185,16 +185,17 @@ print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
#define TASK_UNINTERRUPTIBLE 2
#define __TASK_STOPPED 4
#define __TASK_TRACED 8
+#define __TASK_UTRACED 16
/* in tsk->exit_state */
-#define EXIT_ZOMBIE 16
-#define EXIT_DEAD 32
+#define EXIT_ZOMBIE 32
+#define EXIT_DEAD 64
/* in tsk->state again */
-#define TASK_DEAD 64
-#define TASK_WAKEKILL 128
-#define TASK_WAKING 256
-#define TASK_STATE_MAX 512
+#define TASK_DEAD 128
+#define TASK_WAKEKILL 256
+#define TASK_WAKING 512
+#define TASK_STATE_MAX 1024
-#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKW"
+#define TASK_STATE_TO_CHAR_STR "RSDTtUZXxKW"
extern char ___assert_task_state[1 - 2*!!(
sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
@@ -203,15 +204,16 @@ extern char ___assert_task_state[1 - 2*!!(
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
+#define TASK_UTRACED (TASK_WAKEKILL | __TASK_UTRACED)
/* Convenience macros for the sake of wake_up */
#define TASK_NORMAL (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
-#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED)
+#define TASK_ALL (TASK_NORMAL | __TASK_STOPPED | __TASK_TRACED | __TASK_UTRACED)
/* get_task_state() */
#define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
- __TASK_TRACED)
+ __TASK_TRACED | __TASK_UTRACED)
#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
#define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:51 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:51 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 48F35D889C;
Mon, 21 Nov 2011 15:06:51 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id ZXkDvXaDg0cN; Mon, 21 Nov 2011 15:06:51 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 34485D889A;
Mon, 21 Nov 2011 15:06:51 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6nlT022211;
Mon, 21 Nov 2011 15:06:49 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:09 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:08 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 15/33] utrace: use TASK_UTRACED instead of TASK_TRACED
Message-ID: <20111121200208.GA27826@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 4316
Lines: 130
Change utrace.c to use TASK_UTRACED instead of TASK_TRACED.
- utrace_stop/utrace_wakeup: simply use the new state
- utrace_do_stop: do not clear STOPPED/TRACED, but add the new
__TASK_UTRACED bit to state the fact that both ptrace and utrace
want this task to be stopped
- naturally, do not use task_is_traced() to check if this task was
stopped by utrace, use the new task_is_utraced() helper which
checks __TASK_UTRACED.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/utrace.c | 26 ++++++++++++++------------
1 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/kernel/utrace.c b/kernel/utrace.c
index cebc390..2097103 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -462,6 +462,8 @@ static void put_detached_list(struct list_head *list)
*/
#define ENGINE_STOP (1UL << _UTRACE_NEVENTS)
+#define task_is_utraced(task) ((task->state & __TASK_UTRACED) != 0)
+
static void mark_engine_wants_stop(struct task_struct *task,
struct utrace_engine *engine)
{
@@ -576,7 +578,7 @@ int utrace_set_events(struct task_struct *target,
ret = 0;
if ((old_flags & ~events) && target != current &&
- !task_is_stopped_or_traced(target) && !target->exit_state) {
+ !task_is_utraced(target) && !target->exit_state) {
/*
* This barrier ensures that our engine->flags changes
* have hit before we examine utrace->reporting,
@@ -623,21 +625,21 @@ static void mark_engine_detached(struct utrace_engine *engine)
*/
static bool utrace_do_stop(struct task_struct *target, struct utrace *utrace)
{
- if (task_is_stopped(target)) {
+ if (task_is_stopped_or_traced(target)) {
/*
* Stopped is considered quiescent; when it wakes up, it will
* go through utrace_finish_stop() before doing anything else.
*/
spin_lock_irq(&target->sighand->siglock);
- if (likely(task_is_stopped(target)))
- __set_task_state(target, TASK_TRACED);
+ if (likely(task_is_stopped_or_traced(target)))
+ target->state |= TASK_UTRACED;
spin_unlock_irq(&target->sighand->siglock);
} else if (utrace->resume > UTRACE_REPORT) {
utrace->resume = UTRACE_REPORT;
set_notify_resume(target);
}
- return task_is_traced(target);
+ return task_is_utraced(target);
}
/*
@@ -648,7 +650,7 @@ static void utrace_wakeup(struct task_struct *target, struct utrace *utrace)
{
lockdep_assert_held(&utrace->lock);
spin_lock_irq(&target->sighand->siglock);
- wake_up_quiescent(target, __TASK_TRACED);
+ wake_up_quiescent(target, __TASK_UTRACED);
spin_unlock_irq(&target->sighand->siglock);
}
@@ -710,7 +712,7 @@ static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
/*
* If no more engines want it stopped, wake it up.
*/
- if (task_is_traced(task) && !(flags & ENGINE_STOP)) {
+ if (task_is_utraced(task) && !(flags & ENGINE_STOP)) {
/*
* It just resumes, so make sure single-step
* is not left set.
@@ -749,7 +751,7 @@ void utrace_finish_stop(void)
}
/*
- * Perform %UTRACE_STOP, i.e. block in TASK_TRACED until woken up.
+ * Perform %UTRACE_STOP, i.e. block in TASK_UTRACED until woken up.
* @task == current, @utrace == current->utrace, which is not locked.
* Return true if we were woken up by SIGKILL even though some utrace
* engine may still want us to stay stopped.
@@ -799,7 +801,7 @@ relock:
return;
}
- __set_current_state(TASK_TRACED);
+ __set_current_state(TASK_UTRACED);
spin_unlock_irq(&task->sighand->siglock);
spin_unlock(&utrace->lock);
@@ -809,14 +811,14 @@ relock:
utrace_finish_stop();
/*
- * While in TASK_TRACED, we were considered "frozen enough".
+ * While in TASK_UTRACED, we were considered "frozen enough".
* Now that we woke up, it's crucial if we're supposed to be
* frozen that we freeze now before running anything substantial.
*/
try_to_freeze();
/*
- * While we were in TASK_TRACED, complete_signal() considered
+ * While we were in TASK_UTRACED, complete_signal() considered
* us "uninterested" in signal wakeups. Now make sure our
* TIF_SIGPENDING state is correct for normal running.
*/
@@ -1087,7 +1089,7 @@ int utrace_control(struct task_struct *target,
if (unlikely(IS_ERR(utrace)))
return PTR_ERR(utrace);
- reset = task_is_traced(target);
+ reset = task_is_utraced(target);
ret = 0;
/*
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:53 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:53 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B8F54D5296;
Mon, 21 Nov 2011 15:06:53 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id PX4Y3H1mw0hD; Mon, 21 Nov 2011 15:06:53 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id A4B5CD528E;
Mon, 21 Nov 2011 15:06:53 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6pXd029643;
Mon, 21 Nov 2011 15:06:52 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:12 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:10 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 16/33] reintroduce tracehook_finish_jctl() as
utrace_end_stop()
Message-ID: <20111121200210.GA27829@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 2401
Lines: 77
utrace_finish_stop() is needed to avoid the races with SIGKILL which
wakes up UTRACED task, and thus it should be called every time after
the STOPPED/TRACED/UTRACED returns from schedule(), remember that
TASK_UTRACED can be added while the task is STOPPED/UTRACED.
- change do_signal_state() to call this helper right after schedule(),
otherwise this logic is broken by the upstream changes
- now that utrace doesn't control TASK_TRACED bit, ptrace_stop() must
call this helper too.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/utrace.h | 11 +++++++++++
kernel/signal.c | 5 +++++
kernel/utrace.c | 2 +-
3 files changed, 17 insertions(+), 1 deletions(-)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index cf13839..0279c74 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -719,4 +719,15 @@ static inline void utrace_exit_notify(struct task_struct *task,
utrace_report_death(task, group_dead, signal);
}
+/**
+ * utrace_end_stop - report about return from STOPPED/TRACED
+ *
+ * This is called by do_signal_stop() and ptrace_stop after wakeup.
+ */
+static inline void utrace_end_stop(void)
+{
+ if (task_utrace_flags(current))
+ utrace_finish_stop();
+}
+
#endif /* linux/utrace.h */
diff --git a/kernel/signal.c b/kernel/signal.c
index 7a47a93..ba46eab 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -1903,6 +1903,8 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
read_unlock(&tasklist_lock);
}
+ utrace_end_stop();
+
/*
* While in TASK_TRACED, we were considered "frozen enough".
* Now that we woke up, it's crucial if we're supposed to be
@@ -2067,6 +2069,9 @@ static bool do_signal_stop(int signr)
/* Now we don't run again until woken by SIGCONT or SIGKILL */
schedule();
+
+ utrace_end_stop();
+
return true;
} else {
/*
diff --git a/kernel/utrace.c b/kernel/utrace.c
index 2097103..d41b982 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -741,7 +741,7 @@ static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
void utrace_finish_stop(void)
{
/*
- * If we were task_is_traced() and then SIGKILL'ed, make
+ * If we were task_is_utraced() and then SIGKILL'ed, make
* sure we do nothing until the tracer drops utrace->lock.
*/
if (unlikely(__fatal_signal_pending(current))) {
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:56 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:56 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4A487D83A1;
Mon, 21 Nov 2011 15:06:56 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 3vHogPyhojlJ; Mon, 21 Nov 2011 15:06:56 -0500 (EST)
Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 358A0D88B0;
Mon, 21 Nov 2011 15:06:56 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK6sHk017679;
Mon, 21 Nov 2011 15:06:54 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:14 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:12 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 17/33] teach wake_up_quiescent() to do "selective" wake_up
Message-ID: <20111121200212.GA27836@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
Status: RO
Content-Length: 1335
Lines: 46
Both utrace and ptrace can want the same thread to be quiescent, in this
case its state is TASK_TRACED | TASK_UTRACED. And this also means that
this task must not run unless both utrace and ptrace resume it.
Change wake_up_quiescent(p, state) to do "p->state &= ~state" and return
false unless there is no more "quiescent" bits in task->state.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/signal.c | 15 +++++++++++++++
1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/kernel/signal.c b/kernel/signal.c
index ba46eab..e06f795 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -701,11 +701,26 @@ void signal_wake_up(struct task_struct *t, int resume)
kick_process(t);
}
+#define STATE_QUIESCENT (__TASK_STOPPED | __TASK_TRACED | __TASK_UTRACED)
/*
* wakes up the STOPPED/TRACED task, must be called with ->siglock held.
*/
int wake_up_quiescent(struct task_struct *p, unsigned int state)
{
+ unsigned int quiescent = (p->state & STATE_QUIESCENT);
+
+ WARN_ON(state & ~(STATE_QUIESCENT | TASK_INTERRUPTIBLE));
+
+ if (quiescent) {
+ state &= ~TASK_INTERRUPTIBLE;
+ if ((quiescent & ~state) != 0) {
+ p->state &= ~state;
+ WARN_ON(!(p->state & STATE_QUIESCENT));
+ WARN_ON(!(p->state & TASK_WAKEKILL));
+ return 0;
+ }
+ }
+
return wake_up_state(p, state);
}
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:06:59 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:06:58 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id C88E2D528B;
Mon, 21 Nov 2011 15:06:58 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id nNrAxBD5KOME; Mon, 21 Nov 2011 15:06:58 -0500 (EST)
Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B2114D51C1;
Mon, 21 Nov 2011 15:06:58 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6uQQ031440;
Mon, 21 Nov 2011 15:06:57 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:17 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:15 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 18/33] ptrace_stop: do not assume the task is running after
wake_up_quiescent()
Message-ID: <20111121200215.GA27839@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
Status: RO
Content-Length: 956
Lines: 31
If ptrace_stop() sets TASK_TRACED and then detects we should not stop,
it can race with utrace_do_stop() which can see TASK_TRACED and add
TASK_UTRACED. In this case we should stop for utrace needs.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/signal.c | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/kernel/signal.c b/kernel/signal.c
index e06f795..9348da6 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -1916,6 +1916,14 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
if (clear_code)
current->exit_code = 0;
read_unlock(&tasklist_lock);
+
+ /*
+ * It is possible that __TASK_UTRACED was added by utrace
+ * while we were __TASK_TRACED and before we take ->siglock
+ * for wake_up_quiescent(), we need to block in this case.
+ * Otherwise this is unnecessary but absolutely harmless.
+ */
+ schedule();
}
utrace_end_stop();
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:01 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:01 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 4BF37D889A;
Mon, 21 Nov 2011 15:07:01 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 5p8tvlXAqUHr; Mon, 21 Nov 2011 15:07:01 -0500 (EST)
Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 37C22D8757;
Mon, 21 Nov 2011 15:07:01 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK6xFJ031449;
Mon, 21 Nov 2011 15:06:59 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:19 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:18 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 19/33] get_signal_to_deliver: restore/restructure
utrace/ptrace signal reporting
Message-ID: <20111121200218.GA27846@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
Status: RO
Content-Length: 3556
Lines: 115
- Reintroduce tracehook_get_signal() as utrace_hook_signal().
- Change get_signal_to_deliver() to call utrace_hook_signal() first,
before dequeue_signal()
- Always call ptrace_signal() if signal != SIGKILL, no matter whether
this signal comes from utrace or not.
Since this can change signr again, update "struct k_sigaction *ka"
in this case.
IOW, roughly, ptrace acts as if it is the last attached engine, it
takes the final decision about the signal.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/utrace.h | 31 +++++++++++++++++++++++++++++++
kernel/signal.c | 30 ++++++++++++++++++++----------
2 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index 0279c74..63103e2 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -730,4 +730,35 @@ static inline void utrace_end_stop(void)
utrace_finish_stop();
}
+/**
+ * utrace_hook_signal - deliver synthetic signal to traced task
+ * @task: @current
+ * @regs: task_pt_regs(@current)
+ * @info: details of synthetic signal
+ * @return_ka: sigaction for synthetic signal
+ *
+ * Return zero to check for a real pending signal normally.
+ * Return -1 after releasing the siglock to repeat the check.
+ * Return a signal number to induce an artificial signal delivery,
+ * setting *@info and *@return_ka to specify its details and behavior.
+ *
+ * The @return_ka->sa_handler value controls the disposition of the
+ * signal, no matter the signal number. For %SIG_DFL, the return value
+ * is a representative signal to indicate the behavior (e.g. %SIGTERM
+ * for death, %SIGQUIT for core dump, %SIGSTOP for job control stop,
+ * %SIGTSTP for stop unless in an orphaned pgrp), but the signal number
+ * reported will be @info->si_signo instead.
+ *
+ * Called with @task->sighand->siglock held, before dequeuing pending signals.
+ */
+static inline int utrace_hook_signal(struct task_struct *task,
+ struct pt_regs *regs,
+ siginfo_t *info,
+ struct k_sigaction *return_ka)
+{
+ if (unlikely(task_utrace_flags(task)))
+ return utrace_get_signal(task, regs, info, return_ka);
+ return 0;
+}
+
#endif /* linux/utrace.h */
diff --git a/kernel/signal.c b/kernel/signal.c
index 9348da6..38ea4e6 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2242,17 +2242,27 @@ relock:
for (;;) {
struct k_sigaction *ka;
- if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
- do_signal_stop(0))
+ signr = utrace_hook_signal(current, regs, info, return_ka);
+ if (unlikely(signr < 0))
goto relock;
- if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
- do_jobctl_trap();
- spin_unlock_irq(&sighand->siglock);
- goto relock;
- }
+ if (unlikely(signr != 0))
+ ka = return_ka;
+ else {
+ if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
+ do_signal_stop(0))
+ goto relock;
- signr = dequeue_signal(current, ¤t->blocked, info);
+ if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
+ do_jobctl_trap();
+ spin_unlock_irq(&sighand->siglock);
+ goto relock;
+ }
+
+ signr = dequeue_signal(current, ¤t->blocked, info);
+
+ ka = &sighand->action[signr-1];
+ }
if (!signr)
break; /* will return 0 */
@@ -2262,9 +2272,9 @@ relock:
regs, cookie);
if (!signr)
continue;
- }
- ka = &sighand->action[signr-1];
+ ka = &sighand->action[signr-1];
+ }
/* Trace actually delivered signals. */
trace_signal_deliver(signr, info, ka);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:03 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:03 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id CB362D89E9;
Mon, 21 Nov 2011 15:07:03 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id wm2JqccPnhsV; Mon, 21 Nov 2011 15:07:03 -0500 (EST)
Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B6D25D89DE;
Mon, 21 Nov 2011 15:07:03 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK715f031455;
Mon, 21 Nov 2011 15:07:02 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:22 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:20 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 20/33] utrace_get_signal:
s/JOBCTL_STOP_PENDING/JOBCTL_PENDING_MASK/
Message-ID: <20111121200220.GA27849@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
Status: RO
Content-Length: 1204
Lines: 33
utrace_get_signal() checks JOBCTL_STOP_PENDING to detect the
case when we should not try to dequeue the signal but should
try to participate in the group-stop.
With the recent changes this is not enough, everything which
contrbutes to recalc_sigpending_tsk() should be respected.
Check JOBCTL_PENDING_MASK instead. This matches the
JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK code in the caller,
get_signal_to_deliver(). Note that this code won't run if
utrace_get_signal() returns signr > 0.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/utrace.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/kernel/utrace.c b/kernel/utrace.c
index d41b982..0bb0a06 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -2027,7 +2027,7 @@ int utrace_get_signal(struct task_struct *task, struct pt_regs *regs,
ka = NULL;
memset(return_ka, 0, sizeof *return_ka);
} else if (!(task->utrace_flags & UTRACE_EVENT_SIGNAL_ALL) ||
- unlikely(task->jobctl & JOBCTL_STOP_PENDING)) {
+ unlikely(task->jobctl & JOBCTL_PENDING_MASK)) {
/*
* If no engine is interested in intercepting signals or
* we must stop, let the caller just dequeue them normally
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:07 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:06 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8C14BD4F84;
Mon, 21 Nov 2011 15:07:06 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 39eM-KKZxrZ3; Mon, 21 Nov 2011 15:07:06 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7628FD4FF4;
Mon, 21 Nov 2011 15:07:06 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK74dN022253;
Mon, 21 Nov 2011 15:07:04 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:25 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:23 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 21/33] introduce ptrace_set_syscall_trace()
Message-ID: <20111121200223.GA27856@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 1549
Lines: 52
No functional changes. Add the new helper, ptrace_set_syscall_trace(),
which should be used to set/clear TIF_SYSCALL_TRACE in ptrace code.
Currently it does nothing more.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/ptrace.c | 15 ++++++++++-----
1 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index a464ab5..43357e5 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -38,6 +38,14 @@ void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
kick_process(p);
}
+static void ptrace_set_syscall_trace(struct task_struct *p, bool on)
+{
+ if (on)
+ set_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
+ else
+ clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
+}
+
static int ptrace_trapping_sleep_fn(void *flags)
{
schedule();
@@ -418,7 +426,7 @@ static int ptrace_detach(struct task_struct *child, unsigned int data)
/* Architecture-specific hardware disable .. */
ptrace_disable(child);
- clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
+ ptrace_set_syscall_trace(child, false);
write_lock_irq(&tasklist_lock);
/*
@@ -606,10 +614,7 @@ static int ptrace_resume(struct task_struct *child, long request,
if (!valid_signal(data))
return -EIO;
- if (request == PTRACE_SYSCALL)
- set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
- else
- clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
+ ptrace_set_syscall_trace(child, request == PTRACE_SYSCALL);
#ifdef TIF_SYSCALL_EMU
if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:09 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:09 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 1ADF01285E4;
Mon, 21 Nov 2011 15:07:09 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id rF+XegsxVXnL; Mon, 21 Nov 2011 15:07:09 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 067E41287D0;
Mon, 21 Nov 2011 15:07:09 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK76tD022270;
Mon, 21 Nov 2011 15:07:07 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:27 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:25 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 22/33] introduce PT_SYSCALL_TRACE flag
Message-ID: <20111121200225.GA27860@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 2268
Lines: 73
Currently tracehooks assume that if the ptraced task has
TIF_SYSCALL_TRACE set, the tracee should report the syscall.
This is not true, this thread flag can be set by utrace.
Add the new internal ptrace flag, PT_SYSCALL_TRACE. Change
ptrace_set_syscall_trace() to set/clear this bit along with
TIF_SYSCALL_TRACE, change ptrace_report_syscall() to check
this flag instead of PT_PTRACED.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/ptrace.h | 3 +++
include/linux/tracehook.h | 2 +-
kernel/ptrace.c | 7 +++++--
3 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
index 6d9282a..c10f610 100644
--- a/include/linux/ptrace.h
+++ b/include/linux/ptrace.h
@@ -104,6 +104,8 @@
#define PT_TRACE_MASK 0x000003f4
+#define PT_SYSCALL_TRACE 0x00020000
+
/* single stepping state bits (used on ARM and PA-RISC) */
#define PT_SINGLESTEP_BIT 31
#define PT_SINGLESTEP (1<<PT_SINGLESTEP_BIT)
@@ -227,6 +229,7 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
if (unlikely(ptrace) && current->ptrace) {
child->ptrace = current->ptrace;
+ child->ptrace &= ~PT_SYSCALL_TRACE;
__ptrace_link(child, current->parent);
if (child->ptrace & PT_SEIZED)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index ec2af67..eb9fe30 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -59,7 +59,7 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
{
int ptrace = current->ptrace;
- if (!(ptrace & PT_PTRACED))
+ if (!(ptrace & PT_SYSCALL_TRACE))
return;
ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 43357e5..1ac03eb 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -40,10 +40,13 @@ void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
static void ptrace_set_syscall_trace(struct task_struct *p, bool on)
{
- if (on)
+ if (on) {
+ p->ptrace |= PT_SYSCALL_TRACE;
set_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
- else
+ } else {
+ p->ptrace &= ~PT_SYSCALL_TRACE;
clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
+ }
}
static int ptrace_trapping_sleep_fn(void *flags)
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:11 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:11 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id B7D62D4DCC;
Mon, 21 Nov 2011 15:07:11 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 1ENpGO166I-W; Mon, 21 Nov 2011 15:07:11 -0500 (EST)
Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id A3A55D498A;
Mon, 21 Nov 2011 15:07:11 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK79AS017728;
Mon, 21 Nov 2011 15:07:10 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:30 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:28 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 23/33] utrace: don't clear TIF_SYSCALL_TRACE if it is
needed by ptrace
Message-ID: <20111121200228.GA27863@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
Status: RO
Content-Length: 797
Lines: 24
TIF_SYSCALL_TRACE should be cleared only if both ptrace and utrace do
not want it, change utrace_reset() to check PT_SYSCALL_TRACE before
clear_tsk_thread_flag(TIF_SYSCALL_TRACE).
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/utrace.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/kernel/utrace.c b/kernel/utrace.c
index 0bb0a06..bebf6de 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -697,6 +697,7 @@ static bool utrace_reset(struct task_struct *task, struct utrace *utrace)
BUG_ON(utrace->death);
flags &= UTRACE_EVENT(REAP);
} else if (!(flags & UTRACE_EVENT_SYSCALL) &&
+ !(task->ptrace & PT_SYSCALL_TRACE) &&
test_tsk_thread_flag(task, TIF_SYSCALL_TRACE)) {
clear_tsk_thread_flag(task, TIF_SYSCALL_TRACE);
}
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:14 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:14 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 5376AD8774;
Mon, 21 Nov 2011 15:07:14 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 5Oc8ErfFfJSs; Mon, 21 Nov 2011 15:07:14 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 3F2CFD81E9;
Mon, 21 Nov 2011 15:07:14 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7Cw3015467;
Mon, 21 Nov 2011 15:07:12 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:32 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:30 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 24/33] introduce task_utrace_lock/task_utrace_unlock
Message-ID: <20111121200230.GA27870@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 2190
Lines: 81
- Add task_utrace_lock(task). It simply takes task->utrace->lock if
this task was ever utraced. Otherwise it takes task_lock(), this
serializes with utrace_attach_task()->utrace_task_alloc(). In both
case the caller can be sure it can't race with anything which needs
utrace->lock.
- Add task_utrace_unlock(task), it releases the corresponding lock.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/utrace.h | 9 +++++++++
kernel/utrace.c | 26 ++++++++++++++++++++++++++
2 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/include/linux/utrace.h b/include/linux/utrace.h
index 63103e2..f37373b 100644
--- a/include/linux/utrace.h
+++ b/include/linux/utrace.h
@@ -117,6 +117,12 @@ void utrace_signal_handler(struct task_struct *, int);
#ifndef CONFIG_UTRACE
+static inline void task_utrace_lock(struct task_struct *task)
+{
+}
+static inline void task_utrace_unlock(struct task_struct *task)
+{
+}
/*
* <linux/tracehook.h> uses these accessors to avoid #ifdef CONFIG_UTRACE.
*/
@@ -139,6 +145,9 @@ static inline void task_utrace_proc_status(struct seq_file *m,
#else /* CONFIG_UTRACE */
+extern void task_utrace_lock(struct task_struct *task);
+extern void task_utrace_unlock(struct task_struct *task);
+
static inline unsigned long task_utrace_flags(struct task_struct *task)
{
return task->utrace_flags;
diff --git a/kernel/utrace.c b/kernel/utrace.c
index bebf6de..960dd9e 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -79,6 +79,32 @@ static int __init utrace_init(void)
}
module_init(utrace_init);
+void task_utrace_lock(struct task_struct *task)
+{
+ struct utrace *utrace = task_utrace_struct(task);
+
+ if (!utrace) {
+ task_lock(task);
+ utrace = task_utrace_struct(task);
+ if (!utrace)
+ return;
+
+ task_unlock(task);
+ }
+
+ spin_lock(&utrace->lock);
+}
+
+void task_utrace_unlock(struct task_struct *task)
+{
+ struct utrace *utrace = task_utrace_struct(task);
+
+ if (utrace)
+ spin_unlock(&utrace->lock);
+ else
+ task_unlock(task);
+}
+
/*
* Set up @task.utrace for the first time. We can have races
* between two utrace_attach_task() calls here. The task_lock()
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:17 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:17 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 01034D498A;
Mon, 21 Nov 2011 15:07:17 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id BglmrU3H2hJr; Mon, 21 Nov 2011 15:07:16 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id E0D31D4FF4;
Mon, 21 Nov 2011 15:07:16 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7Egx015474;
Mon, 21 Nov 2011 15:07:15 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:35 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:33 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 25/33] teach ptrace_set_syscall_trace() to play well with
utrace
Message-ID: <20111121200233.GA27873@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 1545
Lines: 49
1. ptrace_set_syscall_trace(true)->set_tsk_thread_flag(TIF_SYSCALL_TRACE)
can race with utrace_control()->utrace_reset() path which can miss
PT_SYSCALL_TRACE and clear TIF_SYSCALL_TRACE after it was already set.
2. ptrace_set_syscall_trace(false) clears TIF_SYSCALL_TRACE and this is
not utrace-friendly, it can need this flag.
Change ptrace_set_syscall_trace() to take task_utrace_lock(), this is
enough to fix the 1st problem. Check task_utrace_flags() before clearing
TIF_SYSCALL_TRACE, this fixes 2.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/ptrace.c | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 1ac03eb..739183a 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -24,6 +24,7 @@
#include <linux/regset.h>
#include <linux/hw_breakpoint.h>
#include <linux/cn_proc.h>
+#include <linux/utrace.h>
void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
{
@@ -40,13 +41,16 @@ void ptrace_signal_wake_up(struct task_struct *p, int quiescent)
static void ptrace_set_syscall_trace(struct task_struct *p, bool on)
{
+ task_utrace_lock(p);
if (on) {
p->ptrace |= PT_SYSCALL_TRACE;
set_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
} else {
p->ptrace &= ~PT_SYSCALL_TRACE;
- clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
+ if (!(task_utrace_flags(p) & UTRACE_EVENT_SYSCALL))
+ clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
}
+ task_utrace_unlock(p);
}
static int ptrace_trapping_sleep_fn(void *flags)
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:19 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:19 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8F87F128EAA;
Mon, 21 Nov 2011 15:07:19 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id yittEjCd+X4F; Mon, 21 Nov 2011 15:07:19 -0500 (EST)
Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7BC701287D0;
Mon, 21 Nov 2011 15:07:19 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK7HbH031553;
Mon, 21 Nov 2011 15:07:18 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:38 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:36 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 26/33] introduce PT_SINGLE_STEP and PT_SINGLE_BLOCK
Message-ID: <20111121200236.GA27880@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11
Status: RO
Content-Length: 3723
Lines: 108
Add the new internal ptrace flags, PT_SINGLE_STEP and PT_SINGLE_BLOCK.
Like PT_SYSCALL_TRACE, this is needed to avoid the unnecessary ptrace
reports when TIF_SINGLESTEP was set by another engine, not by ptrace.
Also, we need these bits to coordinate the user_*_single_step() calls
from ptrace and utrace.
TODO: update the !x86 ptrace code which does user_disable_single_step().
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
arch/x86/kernel/ptrace.c | 1 +
include/linux/ptrace.h | 5 ++++-
include/linux/tracehook.h | 7 +++++--
kernel/ptrace.c | 3 +++
4 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 8252879..d1557dc 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -808,6 +808,7 @@ static int ioperm_get(struct task_struct *target,
*/
void ptrace_disable(struct task_struct *child)
{
+ child->ptrace &= ~(PT_SINGLE_STEP | PT_SINGLE_BLOCK);
user_disable_single_step(child);
#ifdef TIF_SYSCALL_EMU
clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
diff --git a/include/linux/ptrace.h b/include/linux/ptrace.h
index c10f610..2743315 100644
--- a/include/linux/ptrace.h
+++ b/include/linux/ptrace.h
@@ -105,6 +105,8 @@
#define PT_TRACE_MASK 0x000003f4
#define PT_SYSCALL_TRACE 0x00020000
+#define PT_SINGLE_STEP 0x00040000
+#define PT_SINGLE_BLOCK 0x00080000
/* single stepping state bits (used on ARM and PA-RISC) */
#define PT_SINGLESTEP_BIT 31
@@ -229,7 +231,8 @@ static inline void ptrace_init_task(struct task_struct *child, bool ptrace)
if (unlikely(ptrace) && current->ptrace) {
child->ptrace = current->ptrace;
- child->ptrace &= ~PT_SYSCALL_TRACE;
+ child->ptrace &=
+ ~(PT_SYSCALL_TRACE | PT_SINGLE_STEP | PT_SINGLE_BLOCK);
__ptrace_link(child, current->parent);
if (child->ptrace & PT_SEIZED)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index eb9fe30..21c8ca2 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -104,6 +104,9 @@ static inline __must_check int tracehook_report_syscall_entry(
return 0;
}
+#define ptrace_wants_step() \
+ (current->ptrace & (PT_SINGLE_STEP | PT_SINGLE_BLOCK))
+
/**
* tracehook_report_syscall_exit - task has just finished a system call
* @regs: user register state of current task
@@ -126,7 +129,7 @@ static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT))
utrace_report_syscall_exit(regs);
- if (step) {
+ if (step && ptrace_wants_step()) {
siginfo_t info;
user_single_step_siginfo(current, regs, &info);
force_sig_info(SIGTRAP, &info, current);
@@ -157,7 +160,7 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
{
if (task_utrace_flags(current))
utrace_signal_handler(current, stepping);
- if (stepping)
+ if (stepping && ptrace_wants_step())
ptrace_notify(SIGTRAP);
}
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 739183a..792080d 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -630,13 +630,16 @@ static int ptrace_resume(struct task_struct *child, long request,
clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
#endif
+ child->ptrace &= ~(PT_SINGLE_STEP | PT_SINGLE_BLOCK);
if (is_singleblock(request)) {
if (unlikely(!arch_has_block_step()))
return -EIO;
+ child->ptrace |= PT_SINGLE_BLOCK;
user_enable_block_step(child);
} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
if (unlikely(!arch_has_single_step()))
return -EIO;
+ child->ptrace |= PT_SINGLE_STEP;
user_enable_single_step(child);
} else {
user_disable_single_step(child);
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:22 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:22 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 0C136128FB0;
Mon, 21 Nov 2011 15:07:22 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id GNbS20gY5Jq9; Mon, 21 Nov 2011 15:07:21 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id EB5821287D0;
Mon, 21 Nov 2011 15:07:21 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK7Kn1029766;
Mon, 21 Nov 2011 15:07:20 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:40 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:38 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 27/33] utrace: finish_resume_report: don't do
user_xxx_step() if ptrace_wants_step()
Message-ID: <20111121200238.GA27883@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 2741
Lines: 84
finish_resume_report() should not enable/disable the stepping if
ptrace_wants_step() == T. If ptrace wants block_step while utrace
wants single_step we could "promote" the stepping, but I do not
think this really makes sense.
Unless the tracee is killed this can't race with ptrace, this is
called by the tracee itself. If it is killed we do not care.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/tracehook.h | 8 ++++----
kernel/utrace.c | 9 ++++++---
2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index 21c8ca2..b6812d4 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -104,8 +104,8 @@ static inline __must_check int tracehook_report_syscall_entry(
return 0;
}
-#define ptrace_wants_step() \
- (current->ptrace & (PT_SINGLE_STEP | PT_SINGLE_BLOCK))
+#define ptrace_wants_step(task) \
+ ((task)->ptrace & (PT_SINGLE_STEP | PT_SINGLE_BLOCK))
/**
* tracehook_report_syscall_exit - task has just finished a system call
@@ -129,7 +129,7 @@ static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
if (task_utrace_flags(current) & UTRACE_EVENT(SYSCALL_EXIT))
utrace_report_syscall_exit(regs);
- if (step && ptrace_wants_step()) {
+ if (step && ptrace_wants_step(current)) {
siginfo_t info;
user_single_step_siginfo(current, regs, &info);
force_sig_info(SIGTRAP, &info, current);
@@ -160,7 +160,7 @@ static inline void tracehook_signal_handler(int sig, siginfo_t *info,
{
if (task_utrace_flags(current))
utrace_signal_handler(current, stepping);
- if (stepping && ptrace_wants_step())
+ if (stepping && ptrace_wants_step(current))
ptrace_notify(SIGTRAP);
}
diff --git a/kernel/utrace.c b/kernel/utrace.c
index 960dd9e..05e8532 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -1829,7 +1829,8 @@ static void finish_resume_report(struct task_struct *task,
case UTRACE_BLOCKSTEP:
if (likely(arch_has_block_step())) {
- user_enable_block_step(task);
+ if (!ptrace_wants_step(task))
+ user_enable_block_step(task);
break;
}
@@ -1842,7 +1843,8 @@ static void finish_resume_report(struct task_struct *task,
case UTRACE_SINGLESTEP:
if (likely(arch_has_single_step())) {
- user_enable_single_step(task);
+ if (!ptrace_wants_step(task))
+ user_enable_single_step(task);
} else {
/*
* This means some callback is to blame for failing
@@ -1857,7 +1859,8 @@ static void finish_resume_report(struct task_struct *task,
case UTRACE_REPORT:
case UTRACE_RESUME:
default:
- user_disable_single_step(task);
+ if (!ptrace_wants_step(task))
+ user_disable_single_step(task);
break;
}
}
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:24 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:24 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8954F12914D;
Mon, 21 Nov 2011 15:07:24 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 0lU8wXKC78yu; Mon, 21 Nov 2011 15:07:24 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 759B61287D0;
Mon, 21 Nov 2011 15:07:24 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7Mi7022329;
Mon, 21 Nov 2011 15:07:23 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:43 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:41 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 28/33] ptrace: shift user_*_step() from ptrace_resume() to
ptrace_stop()
Message-ID: <20111121200241.GA27890@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 2374
Lines: 70
1. ptrace_resume() plays with the stopped task which can be also
task_is_utraced(). In this case user_enable_xxx_step() can race
with utrace_reset()->user_disable_single_step().
We could change utrace_reset() to check ptrace_wants_step() and
add the task_utrace_lock + unlock barrier after ptrace_resume()
sets PT_SINGLE_STEP.
But it is better to reassign enable/desable from the tracer to
the tracee, it can check its PT_SINGLE_ bits after wakeup. This
also makes sense because enable_step(task) can be faster if
task == current.
2. ptrace can do user_disable_single_step() while utrace needs the
stepping.
Set TIF_NOTIFY_RESUME after user_disable_single_step() to ensure
the tracee can't return to the user-space without utrace_resume().
Any correct engine which wants the stepping should reassert it.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/ptrace.c | 4 ----
kernel/signal.c | 11 +++++++++++
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 792080d..eba9a22 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -635,14 +635,10 @@ static int ptrace_resume(struct task_struct *child, long request,
if (unlikely(!arch_has_block_step()))
return -EIO;
child->ptrace |= PT_SINGLE_BLOCK;
- user_enable_block_step(child);
} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
if (unlikely(!arch_has_single_step()))
return -EIO;
child->ptrace |= PT_SINGLE_STEP;
- user_enable_single_step(child);
- } else {
- user_disable_single_step(child);
}
child->exit_code = data;
diff --git a/kernel/signal.c b/kernel/signal.c
index 38ea4e6..b13d2bc 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -1928,6 +1928,17 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
utrace_end_stop();
+ if (current->ptrace & PT_SINGLE_BLOCK)
+ user_enable_block_step(current);
+ else if (current->ptrace & PT_SINGLE_STEP)
+ user_enable_single_step(current);
+ else {
+ user_disable_single_step(current);
+ /* if utrace needs the stepping it should reassert */
+ if (task_utrace_flags(current))
+ set_thread_flag(TIF_NOTIFY_RESUME);
+ }
+
/*
* While in TASK_TRACED, we were considered "frozen enough".
* Now that we woke up, it's crucial if we're supposed to be
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:27 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:27 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 043581287D0;
Mon, 21 Nov 2011 15:07:27 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 4WO1El1jiIrJ; Mon, 21 Nov 2011 15:07:26 -0500 (EST)
Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id E3FCA129045;
Mon, 21 Nov 2011 15:07:26 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7OdV017836;
Mon, 21 Nov 2011 15:07:25 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:45 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:43 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 29/33] ptrace_disable: no need to disable stepping
Message-ID: <20111121200243.GA27893@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
Status: RO
Content-Length: 945
Lines: 29
ptrace_disable() is called when the tracee is quiescent and we
are going to untrace. This means we are going to clear ->ptrace
and wake up the tracee. Now that ptrace_stop() checks PT_ bits
and does user_disable_single_step() we can remove this code from
ptrace_disable(), it is unneeded and not utrace-friendly.
TODO: change !x86 code.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
arch/x86/kernel/ptrace.c | 2 --
1 files changed, 0 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index d1557dc..96d315a 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -808,8 +808,6 @@ static int ioperm_get(struct task_struct *target,
*/
void ptrace_disable(struct task_struct *child)
{
- child->ptrace &= ~(PT_SINGLE_STEP | PT_SINGLE_BLOCK);
- user_disable_single_step(child);
#ifdef TIF_SYSCALL_EMU
clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
#endif
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:29 2011
Return-Path: oleg@redhat.com
Received: from zmta01.collab.prod.int.phx2.redhat.com (LHLO
zmta01.collab.prod.int.phx2.redhat.com) (10.5.5.31) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:29 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 8A6E8D4C39;
Mon, 21 Nov 2011 15:07:29 -0500 (EST)
Received: from zmta01.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta01.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id NLURBx2yQes5; Mon, 21 Nov 2011 15:07:29 -0500 (EST)
Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])
by zmta01.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 7660DD498A;
Mon, 21 Nov 2011 15:07:29 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7RnP015578;
Mon, 21 Nov 2011 15:07:27 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:48 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:46 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 30/33] ptrace_report_syscall: check TIF_SYSCALL_EMU
Message-ID: <20111121200246.GA27896@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23
Status: RO
Content-Length: 858
Lines: 28
4d16a64 "introduce PT_SYSCALL_TRACE flag" breaks PTRACE_SYSEMU
which doesn't set PT_SYSCALL_TRACE.
Change ptrace_report_syscall() to check TIF_SYSCALL_EMU as well.
This can't conflict with utrace, this flag can only be set by
ptrace.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/tracehook.h | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index b6812d4..90ca578 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -59,7 +59,7 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
{
int ptrace = current->ptrace;
- if (!(ptrace & PT_SYSCALL_TRACE))
+ if (!(ptrace & PT_SYSCALL_TRACE) && !test_thread_flag(TIF_SYSCALL_EMU))
return;
ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:32 2011
Return-Path: oleg@redhat.com
Received: from zmta02.collab.prod.int.phx2.redhat.com (LHLO
zmta02.collab.prod.int.phx2.redhat.com) (10.5.5.32) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:32 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 10D1312916C;
Mon, 21 Nov 2011 15:07:32 -0500 (EST)
Received: from zmta02.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta02.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id Z14plX1oxaYL; Mon, 21 Nov 2011 15:07:32 -0500 (EST)
Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25])
by zmta02.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id F12FF128E1D;
Mon, 21 Nov 2011 15:07:31 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7T0q022460;
Mon, 21 Nov 2011 15:07:30 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:50 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:48 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 31/33] utrace_resume: check irqs_disabled() to shut up
lockdep
Message-ID: <20111121200248.GA27903@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25
Status: RO
Content-Length: 823
Lines: 26
utrace_resume() enables irqs unconditionally. With the recent changes
in lockdep.c this triggers the warning. Check irqs_disabled() before
local_irq_enable().
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
kernel/utrace.c | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/kernel/utrace.c b/kernel/utrace.c
index 05e8532..c817a46 100644
--- a/kernel/utrace.c
+++ b/kernel/utrace.c
@@ -1881,7 +1881,8 @@ void utrace_resume(struct task_struct *task, struct pt_regs *regs)
* code path leads to calling into get_signal_to_deliver(), which
* implicitly reenables them by virtue of spin_unlock_irq.
*/
- local_irq_enable();
+ if (irqs_disabled()) /* make trace_hardirqs_on() happy */
+ local_irq_enable();
/*
* If this flag is still set it's because there was a signal
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:34 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:34 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 860BCD8A3E;
Mon, 21 Nov 2011 15:07:34 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id pVTbN1oF62dg; Mon, 21 Nov 2011 15:07:34 -0500 (EST)
Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 6FBFFD87A3;
Mon, 21 Nov 2011 15:07:34 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id pALK7W0B029871;
Mon, 21 Nov 2011 15:07:32 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:53 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:51 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 32/33] ptrace_report_syscall: check if TIF_SYSCALL_EMU is
defined
Message-ID: <20111121200251.GA27906@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12
Status: RO
Content-Length: 839
Lines: 31
From: Tony Breeds <tony@bakeyournoodle.com>
TIF_SYSCALL_EMU is x86 only, add ifdef into ptrace_report_syscall().
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
include/linux/tracehook.h | 8 ++++++--
1 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/include/linux/tracehook.h b/include/linux/tracehook.h
index 90ca578..a1bac95 100644
--- a/include/linux/tracehook.h
+++ b/include/linux/tracehook.h
@@ -59,8 +59,12 @@ static inline void ptrace_report_syscall(struct pt_regs *regs)
{
int ptrace = current->ptrace;
- if (!(ptrace & PT_SYSCALL_TRACE) && !test_thread_flag(TIF_SYSCALL_EMU))
- return;
+ if (!(ptrace & PT_SYSCALL_TRACE)) {
+#ifdef TIF_SYSCALL_EMU
+ if (!test_thread_flag(TIF_SYSCALL_EMU))
+#endif
+ return;
+ }
ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
--
1.5.5.1
From oleg@redhat.com Mon Nov 21 15:07:37 2011
Return-Path: oleg@redhat.com
Received: from zmta03.collab.prod.int.phx2.redhat.com (LHLO
zmta03.collab.prod.int.phx2.redhat.com) (10.5.5.33) by
zmail13.collab.prod.int.phx2.redhat.com with LMTP; Mon, 21 Nov 2011
15:07:37 -0500 (EST)
Received: from localhost (localhost.localdomain [127.0.0.1])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 33405D8A50;
Mon, 21 Nov 2011 15:07:37 -0500 (EST)
Received: from zmta03.collab.prod.int.phx2.redhat.com ([127.0.0.1])
by localhost (zmta03.collab.prod.int.phx2.redhat.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 3WH8FhvizjEA; Mon, 21 Nov 2011 15:07:37 -0500 (EST)
Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22])
by zmta03.collab.prod.int.phx2.redhat.com (Postfix) with ESMTP id 1274FD89BB;
Mon, 21 Nov 2011 15:07:37 -0500 (EST)
Received: from tranklukator.englab.brq.redhat.com (dhcp-1-232.brq.redhat.com [10.34.1.232])
by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id pALK7YHr017898;
Mon, 21 Nov 2011 15:07:35 -0500
Received: by tranklukator.englab.brq.redhat.com (nbSMTP-1.00) for uid 500
oleg@redhat.com; Mon, 21 Nov 2011 21:02:55 +0100 (CET)
Date: Mon, 21 Nov 2011 21:02:53 +0100
From: Oleg Nesterov <oleg@redhat.com>
To: Dave Jones <davej@redhat.com>, "Frank Ch. Eigler" <fche@redhat.com>,
Josh Boyer <jwboyer@redhat.com>, Josh Stone <jistone@redhat.com>,
Kyle McMartin <kmcmartin@redhat.com>
Cc: kernel@lists.fedoraproject.org
Subject: [PATCH 33/33] utrace: s390: fix the compile problem with traps.c
Message-ID: <20111121200253.GA27913@redhat.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <20111121200039.GA27699@redhat.com>
User-Agent: Mutt/1.5.18 (2008-05-17)
X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22
Status: RO
Content-Length: 725
Lines: 25
d99e60e5 "tracehooks: reintroduce tracehook_consider_fatal_signal()"
breaks the compilation of arch/s390/kernel/traps.c. Restore the
necessary include removed by upstream 73b7d40f commit.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
arch/s390/kernel/traps.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/arch/s390/kernel/traps.c b/arch/s390/kernel/traps.c
index f506e1b..3132498 100644
--- a/arch/s390/kernel/traps.c
+++ b/arch/s390/kernel/traps.c
@@ -18,7 +18,7 @@
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
-#include <linux/ptrace.h>
+#include <linux/tracehook.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/smp.h>
--
1.5.5.1
|