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
|
diff --git a/kernel-open/common/inc/nvkms-kapi.h b/kernel-open/common/inc/nvkms-kapi.h
index 8e838eaf..2011874e 100644
--- a/kernel-open/common/inc/nvkms-kapi.h
+++ b/kernel-open/common/inc/nvkms-kapi.h
@@ -679,6 +679,17 @@ struct NvKmsKapiFunctionsTable {
*/
void (*freeDevice)(struct NvKmsKapiDevice *device);
+ /*!
+ * Frees a device during surprise removal (e.g., Thunderbolt eGPU unplug).
+ * This skips all hardware access and only releases kernel resources.
+ * Use this instead of freeDevice() when the GPU hardware is no longer
+ * accessible to avoid page faults and hangs.
+ *
+ * \param [in] device A device returned by allocateDevice().
+ * This function is a no-op if device is not valid.
+ */
+ void (*freeDeviceForSurpriseRemoval)(struct NvKmsKapiDevice *device);
+
/*!
* Grab ownership of device, ownership is required to do modeset.
*
diff --git a/kernel-open/nvidia-drm/nvidia-drm-crtc.c b/kernel-open/nvidia-drm/nvidia-drm-crtc.c
index 17958f8a..ce0b8334 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-crtc.c
+++ b/kernel-open/nvidia-drm/nvidia-drm-crtc.c
@@ -3152,6 +3152,7 @@ static void nv_drm_vblank_enable_fn(void *t)
{
struct nv_drm_crtc *nv_crtc = t;
struct nv_drm_device *nv_dev = to_nv_device(nv_crtc->base.dev);
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
struct drm_device *dev = nv_crtc->base.dev;
unsigned long irqflags;
@@ -3165,9 +3166,11 @@ static void nv_drm_vblank_enable_fn(void *t)
nv_crtc->vblank_intr_fired = false;
spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
- if (nv_crtc->vblankIntrCallback == NULL) {
+ if (pDevice != NULL && !READ_ONCE(nv_dev->inSurpriseRemoval) &&
+ !READ_ONCE(nv_dev->inRemoval) &&
+ nv_crtc->vblankIntrCallback == NULL) {
nv_crtc->vblankIntrCallback =
- nvKms->registerVblankIntrCallback(nv_dev->pDevice, nv_crtc->head,
+ nvKms->registerVblankIntrCallback(pDevice, nv_crtc->head,
nv_drm_crtc_vblank_callback, (NvU64)(NvUPtr)nv_crtc);
}
@@ -3179,13 +3182,17 @@ static void nv_drm_vblank_disable_fn(void *t)
struct nv_drm_crtc *nv_crtc = t;
struct drm_device *dev = nv_crtc->base.dev;
struct nv_drm_device *nv_dev = to_nv_device(dev);
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
unsigned long irqflags;
mutex_lock(&nv_crtc->vblank_q_lock);
if (nv_crtc->vblankIntrCallback != NULL) {
- nvKms->unregisterVblankIntrCallback(nv_dev->pDevice, nv_crtc->head,
- nv_crtc->vblankIntrCallback);
+ if (pDevice != NULL && !READ_ONCE(nv_dev->inSurpriseRemoval) &&
+ !READ_ONCE(nv_dev->inRemoval)) {
+ nvKms->unregisterVblankIntrCallback(pDevice, nv_crtc->head,
+ nv_crtc->vblankIntrCallback);
+ }
nv_crtc->vblankIntrCallback = NULL;
}
diff --git a/kernel-open/nvidia-drm/nvidia-drm-drv.c b/kernel-open/nvidia-drm/nvidia-drm-drv.c
index df29f491..8c1fb7ac 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-drv.c
+++ b/kernel-open/nvidia-drm/nvidia-drm-drv.c
@@ -907,6 +907,56 @@ static void nv_drm_dev_unload(struct drm_device *dev)
return;
}
+ /*
+ * During surprise removal (e.g., Thunderbolt eGPU hot-unplug),
+ * the GPU hardware is no longer accessible. Skip NVKMS calls that
+ * would access hardware to prevent page faults and crashes.
+ * Use freeDeviceForSurpriseRemoval which only releases kernel resources
+ * without attempting any hardware access.
+ */
+ /*
+ * For Thunderbolt eGPU hot-unplug, pci_channel_offline() returns false
+ * (it's a controlled PCIe removal, not a bus error), so inSurpriseRemoval
+ * is not set. However, the GPU hardware is physically gone. Use
+ * freeDeviceForSurpriseRemoval for ALL removals triggered via nv_drm_remove
+ * (inRemoval = TRUE) so we never attempt hardware-level RM teardown against
+ * a device that is no longer on the bus. This suppresses the
+ * kgmmuInvalidateTlb / dmaFreeMapping error flood in the kernel log.
+ */
+ if (READ_ONCE(nv_dev->inSurpriseRemoval) ||
+ READ_ONCE(nv_dev->inRemoval)) {
+ NV_DRM_DEV_LOG_INFO(nv_dev,
+ "GPU removal detected (surprise or eGPU hot-unplug), skipping hardware access");
+
+ /* Wake up any processes waiting on flip events */
+ wake_up_all(&nv_dev->flip_event_wq);
+
+ cancel_delayed_work_sync(&nv_dev->hotplug_event_work);
+ mutex_lock(&nv_dev->lock);
+
+ atomic_set(&nv_dev->enable_event_handling, false);
+ drm_kms_helper_poll_fini(dev);
+ drm_mode_config_cleanup(dev);
+
+ pDevice = READ_ONCE(nv_dev->pDevice);
+ WRITE_ONCE(nv_dev->pDevice, NULL);
+
+ mutex_unlock(&nv_dev->lock);
+
+ /*
+ * Use freeDeviceForSurpriseRemoval instead of freeDevice.
+ * This skips KmsFreeDevice() and RmFreeDevice() which would try
+ * to access GPU hardware via ioctls/RM API calls and cause
+ * page faults since the GPU memory is unmapped.
+ * It only calls nvkms_close_gpu() to release the GPU reference
+ * count, allowing the eGPU to be re-initialized when reconnected.
+ */
+ if (pDevice != NULL) {
+ nvKms->freeDeviceForSurpriseRemoval(pDevice);
+ }
+ return;
+ }
+
/* Release modeset ownership if fbdev is enabled */
#if defined(NV_DRM_FBDEV_AVAILABLE)
@@ -943,8 +993,8 @@ static void nv_drm_dev_unload(struct drm_device *dev)
/* Unset NvKmsKapiDevice */
- pDevice = nv_dev->pDevice;
- nv_dev->pDevice = NULL;
+ pDevice = READ_ONCE(nv_dev->pDevice);
+ WRITE_ONCE(nv_dev->pDevice, NULL);
mutex_unlock(&nv_dev->lock);
@@ -1892,6 +1942,8 @@ static const struct drm_ioctl_desc nv_drm_ioctls[] = {
DRM_UNLOCKED|DRM_MASTER),
};
+static void nv_drm_dev_release(struct drm_device *dev);
+
static struct drm_driver nv_drm_driver = {
.driver_features =
@@ -1964,6 +2016,7 @@ static struct drm_driver nv_drm_driver = {
#endif
.load = nv_drm_load_noop,
+ .release = nv_drm_dev_release,
.postclose = nv_drm_postclose,
.open = nv_drm_open,
@@ -2156,10 +2209,13 @@ failed_drm_register:
failed_drm_load:
drm_dev_put(dev);
+ nv_dev = NULL;
failed_drm_alloc:
- nv_drm_free(nv_dev);
+ if (nv_dev != NULL) {
+ nv_drm_free(nv_dev);
+ }
}
/*
@@ -2231,7 +2287,44 @@ static void nv_drm_dev_destroy(struct nv_drm_device *nv_dev)
nv_drm_dev_unload(dev);
drm_dev_put(dev);
- nv_drm_free(nv_dev);
+}
+
+static void nv_drm_dev_release(struct drm_device *dev)
+{
+ struct nv_drm_device *nv_dev;
+
+ if (dev == NULL) {
+ return;
+ }
+
+ nv_dev = dev->dev_private;
+ dev->dev_private = NULL;
+
+ if (nv_dev != NULL) {
+ nv_drm_free(nv_dev);
+ }
+}
+
+/*
+ * Helper to get PCI device from DRM device, handling both old and new kernels.
+ * Returns NULL if not a PCI device or device not available.
+ */
+static struct pci_dev *nv_drm_get_pci_dev(struct drm_device *dev)
+{
+ if (dev == NULL) {
+ return NULL;
+ }
+
+#if defined(NV_DRM_DEVICE_HAS_PDEV)
+ return dev->pdev;
+#else
+ /* On newer kernels (5.14+), drm_device.pdev was removed.
+ * Get PCI device from the parent device. */
+ if (dev->dev != NULL && dev->dev->bus == &pci_bus_type) {
+ return to_pci_dev(dev->dev);
+ }
+ return NULL;
+#endif
}
/*
@@ -2242,7 +2335,42 @@ void nv_drm_remove(NvU32 gpuId)
struct nv_drm_device *nv_dev = nv_drm_find_and_remove_device(gpuId);
if (nv_dev) {
+ struct pci_dev *pdev;
+
NV_DRM_DEV_LOG_INFO(nv_dev, "Removing device");
+
+ /*
+ * Mark the device as being removed BEFORE drm_dev_unplug to close
+ * the race window where delayed_fput callbacks (e.g. DMA-buf release)
+ * check pDevice != NULL and proceed to call into nvidia_modeset state
+ * that may have already been freed by nv_drm_dev_unload running
+ * concurrently on another CPU. Setting inRemoval first ensures all
+ * destructor guards skip nvKms calls from this point on.
+ */
+ WRITE_ONCE(nv_dev->inRemoval, NV_TRUE);
+
+ /*
+ * Check if this is a surprise removal (hot-unplug) by testing
+ * if the PCI channel is offline. This happens when:
+ * - Thunderbolt eGPU is physically disconnected
+ * - GPU falls off the bus unexpectedly
+ *
+ * Note: for Thunderbolt eGPU hot-unplug pci_channel_offline() often
+ * returns false (controlled PCIe hot-remove, not a bus error), so
+ * inSurpriseRemoval may not be set. inRemoval above handles that.
+ * We keep inSurpriseRemoval for the hardware-access-skip path in
+ * nv_drm_dev_unload (freeDeviceForSurpriseRemoval vs freeDevice).
+ */
+ pdev = nv_drm_get_pci_dev(nv_dev->dev);
+ if (pdev != NULL && pci_channel_offline(pdev)) {
+ NV_DRM_DEV_LOG_INFO(nv_dev,
+ "PCI channel offline - surprise removal detected");
+ WRITE_ONCE(nv_dev->inSurpriseRemoval, NV_TRUE);
+
+ /* Wake up any processes waiting on flip events */
+ wake_up_all(&nv_dev->flip_event_wq);
+ }
+
drm_dev_unplug(nv_dev->dev);
nv_drm_dev_destroy(nv_dev);
}
diff --git a/kernel-open/nvidia-drm/nvidia-drm-fb.c b/kernel-open/nvidia-drm/nvidia-drm-fb.c
index 95fd4f0c..b59051df 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-fb.c
+++ b/kernel-open/nvidia-drm/nvidia-drm-fb.c
@@ -55,6 +55,7 @@ static void __nv_drm_framebuffer_free(struct nv_drm_framebuffer *nv_fb)
static void nv_drm_framebuffer_destroy(struct drm_framebuffer *fb)
{
struct nv_drm_device *nv_dev = to_nv_device(fb->dev);
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
struct nv_drm_framebuffer *nv_fb = to_nv_framebuffer(fb);
/* Cleaup core framebuffer object */
@@ -63,8 +64,15 @@ static void nv_drm_framebuffer_destroy(struct drm_framebuffer *fb)
/* Free NvKmsKapiSurface associated with this framebuffer object */
- if (nv_fb->pSurface != NULL) {
- nvKms->destroySurface(nv_dev->pDevice, nv_fb->pSurface);
+ /*
+ * Only call nvKms->destroySurface if pDevice is valid and removal has not
+ * started. During hot-unplug, nvidia_modeset internal state may be
+ * corrupted before this destructor runs from delayed_fput.
+ */
+
+ if (pDevice != NULL && !READ_ONCE(nv_dev->inSurpriseRemoval) &&
+ !READ_ONCE(nv_dev->inRemoval) && nv_fb->pSurface != NULL) {
+ nvKms->destroySurface(pDevice, nv_fb->pSurface);
}
__nv_drm_framebuffer_free(nv_fb);
diff --git a/kernel-open/nvidia-drm/nvidia-drm-fence.c b/kernel-open/nvidia-drm/nvidia-drm-fence.c
index 7af1ed7f..8dc021ea 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-fence.c
+++ b/kernel-open/nvidia-drm/nvidia-drm-fence.c
@@ -209,14 +209,30 @@ static void __nv_drm_prime_fence_context_destroy(
struct nv_drm_fence_context *nv_fence_context)
{
struct nv_drm_device *nv_dev = nv_fence_context->nv_dev;
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
struct nv_drm_prime_fence_context *nv_prime_fence_context =
to_nv_prime_fence_context(nv_fence_context);
+ /*
+ * Skip nvKms calls if device is being removed or was surprise-removed.
+ * The nvidia_modeset internal state may be freed before this destructor
+ * runs from delayed_fput (race between drm_dev_unplug and pDevice NULL).
+ */
+ if (pDevice == NULL || READ_ONCE(nv_dev->inSurpriseRemoval) ||
+ READ_ONCE(nv_dev->inRemoval)) {
+ /* Force signal pending fences and free */
+ spin_lock(&nv_prime_fence_context->lock);
+ nv_drm_gem_prime_force_fence_signal(nv_prime_fence_context);
+ spin_unlock(&nv_prime_fence_context->lock);
+ nv_drm_free(nv_fence_context);
+ return;
+ }
+
/*
* Free channel event before destroying the fence context, otherwise event
* callback continue to get called.
*/
- nvKms->freeChannelEvent(nv_dev->pDevice, nv_prime_fence_context->cb);
+ nvKms->freeChannelEvent(pDevice, nv_prime_fence_context->cb);
/* Force signal all pending fences and empty pending list */
spin_lock(&nv_prime_fence_context->lock);
@@ -227,12 +243,12 @@ static void __nv_drm_prime_fence_context_destroy(
/* Free nvkms resources */
- nvKms->unmapMemory(nv_dev->pDevice,
+ nvKms->unmapMemory(pDevice,
nv_prime_fence_context->pSemSurface,
NVKMS_KAPI_MAPPING_TYPE_KERNEL,
(void *) nv_prime_fence_context->pLinearAddress);
- nvKms->freeMemory(nv_dev->pDevice, nv_prime_fence_context->pSemSurface);
+ nvKms->freeMemory(pDevice, nv_prime_fence_context->pSemSurface);
nv_drm_free(nv_fence_context);
}
@@ -1000,13 +1016,20 @@ __nv_drm_semsurf_ctx_reg_callbacks(struct nv_drm_semsurf_fence_ctx *ctx)
{
struct nv_drm_device *nv_dev = ctx->base.nv_dev;
- struct nv_drm_semsurf_fence_callback *newCallback =
- __nv_drm_semsurf_new_callback(ctx);
+ struct nv_drm_semsurf_fence_callback *newCallback;
struct NvKmsKapiSemaphoreSurfaceCallback *newNvKmsCallback;
NvU64 newWaitValue;
unsigned long newTimeout;
NvKmsKapiRegisterWaiterResult kapiRet;
+ if (READ_ONCE(nv_dev->pDevice) == NULL ||
+ READ_ONCE(nv_dev->inSurpriseRemoval) ||
+ READ_ONCE(nv_dev->inRemoval)) {
+ return;
+ }
+
+ newCallback = __nv_drm_semsurf_new_callback(ctx);
+
if (!newCallback) {
NV_DRM_DEV_LOG_ERR(
nv_dev,
@@ -1118,6 +1141,7 @@ static void __nv_drm_semsurf_fence_ctx_destroy(
struct nv_drm_device *nv_dev = nv_fence_context->nv_dev;
struct nv_drm_semsurf_fence_ctx *ctx =
to_semsurf_fence_ctx(nv_fence_context);
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
struct NvKmsKapiSemaphoreSurfaceCallback *pendingNvKmsCallback;
NvU64 pendingWaitValue;
unsigned long flags;
@@ -1130,6 +1154,20 @@ static void __nv_drm_semsurf_fence_ctx_destroy(
nv_timer_delete_sync(&ctx->timer.kernel_timer);
+ if (pDevice == NULL || READ_ONCE(nv_dev->inSurpriseRemoval) ||
+ READ_ONCE(nv_dev->inRemoval)) {
+ if (ctx->callback.local) {
+ nv_drm_free(ctx->callback.local);
+ ctx->callback.local = NULL;
+ ctx->callback.nvKms = NULL;
+ }
+
+ __nv_drm_semsurf_force_complete_pending(ctx);
+
+ nv_drm_free(nv_fence_context);
+ return;
+ }
+
/*
* The semaphore surface could still be sending callbacks, so it is still
* not safe to dereference the ctx->callback pointers. However,
@@ -1147,14 +1185,14 @@ static void __nv_drm_semsurf_fence_ctx_destroy(
if (pendingNvKmsCallback) {
WARN_ON(pendingWaitValue == 0);
- nvKms->unregisterSemaphoreSurfaceCallback(nv_dev->pDevice,
+ nvKms->unregisterSemaphoreSurfaceCallback(pDevice,
ctx->pSemSurface,
ctx->base.fenceSemIndex,
pendingWaitValue,
pendingNvKmsCallback);
}
- nvKms->freeSemaphoreSurface(nv_dev->pDevice, ctx->pSemSurface);
+ nvKms->freeSemaphoreSurface(pDevice, ctx->pSemSurface);
/*
* Now that the semaphore surface, the timer, and the workthread are gone:
diff --git a/kernel-open/nvidia-drm/nvidia-drm-gem-dma-buf.c b/kernel-open/nvidia-drm/nvidia-drm-gem-dma-buf.c
index 163a8ecf..e93cab60 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-gem-dma-buf.c
+++ b/kernel-open/nvidia-drm/nvidia-drm-gem-dma-buf.c
@@ -41,11 +41,19 @@ static inline
void __nv_drm_gem_dma_buf_free(struct nv_drm_gem_object *nv_gem)
{
struct nv_drm_device *nv_dev = nv_gem->nv_dev;
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
struct nv_drm_gem_dma_buf *nv_dma_buf = to_nv_dma_buf(nv_gem);
- if (nv_dma_buf->base.pMemory) {
+ /*
+ * Only call nvKms->freeMemory if pDevice is valid and removal has not
+ * started. During hot-unplug, nvidia_modeset internal state may be
+ * corrupted before this destructor runs from delayed_fput.
+ */
+ if (nv_dma_buf->base.pMemory && pDevice != NULL &&
+ !READ_ONCE(nv_dev->inSurpriseRemoval) &&
+ !READ_ONCE(nv_dev->inRemoval)) {
/* Free NvKmsKapiMemory handle associated with this gem object */
- nvKms->freeMemory(nv_dev->pDevice, nv_dma_buf->base.pMemory);
+ nvKms->freeMemory(pDevice, nv_dma_buf->base.pMemory);
}
drm_prime_gem_destroy(&nv_gem->base, nv_dma_buf->sgt);
diff --git a/kernel-open/nvidia-drm/nvidia-drm-gem-nvkms-memory.c b/kernel-open/nvidia-drm/nvidia-drm-gem-nvkms-memory.c
index 0785dddf..a42cd5c2 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-gem-nvkms-memory.c
+++ b/kernel-open/nvidia-drm/nvidia-drm-gem-nvkms-memory.c
@@ -39,9 +39,22 @@
static void __nv_drm_gem_nvkms_memory_free(struct nv_drm_gem_object *nv_gem)
{
struct nv_drm_device *nv_dev = nv_gem->nv_dev;
+ struct NvKmsKapiDevice *pDevice = READ_ONCE(nv_dev->pDevice);
struct nv_drm_gem_nvkms_memory *nv_nvkms_memory =
to_nv_nvkms_memory(nv_gem);
+ /*
+ * Skip nvKms calls if pDevice is NULL or removal has started.
+ * During hot-unplug, the nvidia_modeset internal state (semaphores,
+ * memory handles) may be corrupted or freed before this destructor
+ * runs from delayed_fput. The memory resources are gone with the GPU.
+ */
+ if (pDevice == NULL || READ_ONCE(nv_dev->inSurpriseRemoval) ||
+ READ_ONCE(nv_dev->inRemoval)) {
+ nv_drm_free(nv_nvkms_memory);
+ return;
+ }
+
if (nv_nvkms_memory->physically_mapped) {
if (nv_nvkms_memory->pWriteCombinedIORemapAddress != NULL) {
iounmap(nv_nvkms_memory->pWriteCombinedIORemapAddress);
@@ -53,7 +66,7 @@ static void __nv_drm_gem_nvkms_memory_free(struct nv_drm_gem_object *nv_gem)
}
#endif
- nvKms->unmapMemory(nv_dev->pDevice,
+ nvKms->unmapMemory(pDevice,
nv_nvkms_memory->base.pMemory,
NVKMS_KAPI_MAPPING_TYPE_USER,
nv_nvkms_memory->pPhysicalAddress);
@@ -65,12 +78,12 @@ static void __nv_drm_gem_nvkms_memory_free(struct nv_drm_gem_object *nv_gem)
/* Decrement GC6 blocker if it's still held */
if (nv_nvkms_memory->was_mmapped) {
- nvKms->gc6BlockerRefCntDec(nv_dev->pDevice);
+ nvKms->gc6BlockerRefCntDec(pDevice);
}
/* Free NvKmsKapiMemory handle associated with this gem object */
- nvKms->freeMemory(nv_dev->pDevice, nv_nvkms_memory->base.pMemory);
+ nvKms->freeMemory(pDevice, nv_nvkms_memory->base.pMemory);
nv_drm_free(nv_nvkms_memory);
}
diff --git a/kernel-open/nvidia-drm/nvidia-drm-priv.h b/kernel-open/nvidia-drm/nvidia-drm-priv.h
index 0201b95c..3951204f 100644
--- a/kernel-open/nvidia-drm/nvidia-drm-priv.h
+++ b/kernel-open/nvidia-drm/nvidia-drm-priv.h
@@ -159,6 +159,30 @@ struct nv_drm_device {
NvBool subOwnershipGranted;
NvBool hasFramebufferConsole;
+ /*
+ * Set to NV_TRUE for external GPUs (e.g., Thunderbolt/USB4 eGPU).
+ * External GPUs use the fast removal path to avoid hangs during
+ * both surprise removal and "safe" software-initiated disconnect.
+ */
+ NvBool isExternalGpu;
+
+ /*
+ * Set to NV_TRUE when the device is being removed due to
+ * surprise removal (e.g., Thunderbolt eGPU hot-unplug).
+ * When set, NVKMS operations that would access GPU hardware
+ * are skipped to prevent crashes from accessing unmapped memory.
+ */
+ NvBool inSurpriseRemoval;
+
+ /*
+ * Set to NV_TRUE for ALL removals (both surprise and normal) before
+ * drm_dev_unplug is called. This closes the race window between
+ * drm_dev_unplug and pDevice being NULLed in nv_drm_dev_unload,
+ * during which delayed_fput callbacks could call into freed
+ * nvidia_modeset state (e.g. drm_gem_dmabuf_release path).
+ */
+ NvBool inRemoval;
+
struct drm_property *nv_out_fence_property;
struct drm_property *nv_input_colorspace_property;
diff --git a/kernel-open/nvidia-modeset/nvidia-modeset-linux.c b/kernel-open/nvidia-modeset/nvidia-modeset-linux.c
index 8c2cf374..2a2663ae 100644
--- a/kernel-open/nvidia-modeset/nvidia-modeset-linux.c
+++ b/kernel-open/nvidia-modeset/nvidia-modeset-linux.c
@@ -1227,6 +1227,27 @@ void nvkms_close_gpu(NvU32 gpuId, NvBool reset_aware)
__rm_ops.free_stack(stack);
}
+void nvkms_gpu_lost(NvU32 gpuId)
+{
+ /*
+ * Mark the GPU as lost in NVKMS. This prevents hardware access
+ * and cancels pending timers that might try to access the removed GPU.
+ *
+ * NOTE: We intentionally do NOT take nvkms_lock here because this function
+ * may be called from contexts that already hold the lock (e.g., during
+ * module unload). The gpuLost flag is a simple boolean that can be safely
+ * written without a lock - any racing operation will either:
+ * 1. See gpuLost=TRUE and bail out early
+ * 2. See gpuLost=FALSE but hit the 0xFFFFFFFF check when reading hardware
+ *
+ * A memory barrier ensures the write is visible to other CPUs promptly.
+ */
+ nvKmsGpuLost(gpuId);
+
+ /* Ensure gpuLost write is visible to other CPUs */
+ smp_wmb();
+}
+
NvU32 nvkms_enumerate_gpus(nv_gpu_info_t *gpu_info)
{
return __rm_ops.enumerate_gpus(gpu_info);
diff --git a/kernel-open/nvidia-modeset/nvidia-modeset-os-interface.h b/kernel-open/nvidia-modeset/nvidia-modeset-os-interface.h
index a634ec2b..d6709b46 100644
--- a/kernel-open/nvidia-modeset/nvidia-modeset-os-interface.h
+++ b/kernel-open/nvidia-modeset/nvidia-modeset-os-interface.h
@@ -317,6 +317,12 @@ void* nvkms_get_per_open_data(int fd);
NvBool nvkms_open_gpu(NvU32 gpuId, NvBool reset_aware);
void nvkms_close_gpu(NvU32 gpuId, NvBool reset_aware);
+/*!
+ * Mark a GPU as lost (surprise removal, e.g., Thunderbolt eGPU unplug).
+ * This prevents hardware access and cancels pending timers.
+ */
+void nvkms_gpu_lost(NvU32 gpuId);
+
/*!
* Enumerate nvidia gpus.
diff --git a/kernel-open/nvidia-modeset/nvkms.h b/kernel-open/nvidia-modeset/nvkms.h
index d350ef75..668fa8c2 100644
--- a/kernel-open/nvidia-modeset/nvkms.h
+++ b/kernel-open/nvidia-modeset/nvkms.h
@@ -88,6 +88,8 @@ void nvKmsModuleUnload(void);
void nvKmsSuspend(NvU32 gpuId);
void nvKmsResume(NvU32 gpuId);
+void nvKmsGpuLost(NvU32 gpuId);
+
void nvKmsGetProcFiles(const nvkms_procfs_file_t **ppProcFiles);
NvBool nvKmsReadConf(const char *buff, size_t size,
diff --git a/kernel-open/nvidia-uvm/uvm_channel.c b/kernel-open/nvidia-uvm/uvm_channel.c
index 2d0d3034..c2558fd9 100644
--- a/kernel-open/nvidia-uvm/uvm_channel.c
+++ b/kernel-open/nvidia-uvm/uvm_channel.c
@@ -27,6 +27,8 @@
#include "uvm_common.h"
#include "uvm_global.h"
#include "uvm_hal.h"
+#include "uvm_gpu.h"
+#include "uvm_gpu_isr.h"
#include "uvm_procfs.h"
#include "uvm_push.h"
#include "uvm_gpu_semaphore.h"
@@ -2310,10 +2312,14 @@ static void channel_destroy(uvm_channel_pool_t *pool, uvm_channel_t *channel)
free_conf_computing_buffers(channel);
}
- if (uvm_channel_is_proxy(channel))
- uvm_rm_locked_call_void(nvUvmInterfacePagingChannelDestroy(channel->proxy.handle));
- else
- uvm_rm_locked_call_void(nvUvmInterfaceChannelDestroy(channel->handle));
+ // Skip RM calls if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (uvm_parent_gpu_is_accessible(pool->manager->gpu->parent)) {
+ if (uvm_channel_is_proxy(channel))
+ uvm_rm_locked_call_void(nvUvmInterfacePagingChannelDestroy(channel->proxy.handle));
+ else
+ uvm_rm_locked_call_void(nvUvmInterfaceChannelDestroy(channel->handle));
+ }
uvm_gpu_tracking_semaphore_free(&channel->tracking_sem);
@@ -2657,7 +2663,11 @@ static void tsg_destroy(uvm_channel_pool_t *pool, uvmGpuTsgHandle tsg_handle)
{
UVM_ASSERT(pool->num_tsgs > 0);
- uvm_rm_locked_call_void(nvUvmInterfaceTsgDestroy(tsg_handle));
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (uvm_parent_gpu_is_accessible(pool->manager->gpu->parent))
+ uvm_rm_locked_call_void(nvUvmInterfaceTsgDestroy(tsg_handle));
+
pool->num_tsgs--;
}
diff --git a/kernel-open/nvidia-uvm/uvm_gpu.c b/kernel-open/nvidia-uvm/uvm_gpu.c
index a4661a05..3a83133a 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu.c
+++ b/kernel-open/nvidia-uvm/uvm_gpu.c
@@ -46,6 +46,7 @@
#include "uvm_linux.h"
#include "uvm_mmu.h"
#include "uvm_kvmalloc.h"
+#include "uvm_gpu_isr.h"
#define UVM_PROC_GPUS_PEER_DIR_NAME "peers"
@@ -1367,7 +1368,8 @@ static NV_STATUS configure_address_space(uvm_gpu_t *gpu)
static void deconfigure_address_space(uvm_gpu_t *gpu)
{
- if (gpu->rm_address_space_moved_to_page_tree)
+ // Skip RM call if GPU is not accessible (e.g., hot-unplugged).
+ if (gpu->rm_address_space_moved_to_page_tree && uvm_parent_gpu_is_accessible(gpu->parent))
uvm_rm_locked_call_void(nvUvmInterfaceUnsetPageDirectory(gpu->rm_address_space));
if (gpu->address_space_tree.root)
@@ -1777,6 +1779,10 @@ static void remove_gpu_from_parent_gpu(uvm_gpu_t *gpu)
static void deinit_parent_gpu(uvm_parent_gpu_t *parent_gpu)
{
+ // Check GPU accessibility before pci_dev is cleared.
+ // If the GPU was hot-unplugged, skip RM calls that would crash.
+ bool gpu_accessible = uvm_parent_gpu_is_accessible(parent_gpu);
+
// All channels should have been removed before the retained count went to 0
UVM_ASSERT(uvm_rb_tree_empty(&parent_gpu->instance_ptr_table));
UVM_ASSERT(uvm_rb_tree_empty(&parent_gpu->tsg_table));
@@ -1802,7 +1808,9 @@ static void deinit_parent_gpu(uvm_parent_gpu_t *parent_gpu)
if (parent_gpu->rm_info.isSimulated)
--g_uvm_global.num_simulated_devices;
- if (parent_gpu->rm_device != 0)
+ // Skip RM call if GPU was not accessible (e.g., hot-unplugged).
+ // The nvidia module's internal state is corrupted when the GPU is gone.
+ if (parent_gpu->rm_device != 0 && gpu_accessible)
uvm_rm_locked_call_void(nvUvmInterfaceDeviceDestroy(parent_gpu->rm_device));
uvm_parent_gpu_kref_put(parent_gpu);
@@ -1845,16 +1853,20 @@ static void deinit_gpu(uvm_gpu_t *gpu)
uvm_pmm_gpu_deinit(&gpu->pmm);
- if (gpu->rm_address_space != 0)
- uvm_rm_locked_call_void(nvUvmInterfaceAddressSpaceDestroy(gpu->rm_address_space));
-
- deinit_procfs_dirs(gpu);
+ // Skip RM calls if GPU is not accessible (e.g., hot-unplugged).
+ // The nvidia module's internal state is corrupted when the GPU is gone.
+ if (uvm_parent_gpu_is_accessible(gpu->parent)) {
+ if (gpu->rm_address_space != 0)
+ uvm_rm_locked_call_void(nvUvmInterfaceAddressSpaceDestroy(gpu->rm_address_space));
- if (gpu->parent->smc.enabled) {
- if (gpu->smc.rm_device != 0)
- uvm_rm_locked_call_void(nvUvmInterfaceDeviceDestroy(gpu->smc.rm_device));
+ if (gpu->parent->smc.enabled) {
+ if (gpu->smc.rm_device != 0)
+ uvm_rm_locked_call_void(nvUvmInterfaceDeviceDestroy(gpu->smc.rm_device));
+ }
}
+ deinit_procfs_dirs(gpu);
+
gpu->magic = 0;
}
diff --git a/kernel-open/nvidia-uvm/uvm_gpu_access_counters.c b/kernel-open/nvidia-uvm/uvm_gpu_access_counters.c
index d2e1cd94..100ee0bb 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu_access_counters.c
+++ b/kernel-open/nvidia-uvm/uvm_gpu_access_counters.c
@@ -25,6 +25,7 @@
#include "uvm_gpu_access_counters.h"
#include "uvm_global.h"
#include "uvm_api.h"
+#include "uvm_gpu_isr.h"
#include "uvm_gpu.h"
#include "uvm_hal.h"
#include "uvm_kvmalloc.h"
@@ -505,11 +506,15 @@ void uvm_parent_gpu_deinit_access_counters(uvm_parent_gpu_t *parent_gpu, NvU32 n
}
if (access_counters && access_counters->rm_info.accessCntrBufferHandle) {
- NV_STATUS status = uvm_rm_locked_call(nvUvmInterfaceDestroyAccessCntrInfo(parent_gpu->rm_device,
- &access_counters->rm_info));
uvm_access_counter_service_batch_context_t *batch_context = &access_counters->batch_service_context;
- UVM_ASSERT(status == NV_OK);
+ // Skip RM call if GPU is not accessible (e.g., hot-unplugged).
+ // The nvidia module's internal state is corrupted when the GPU is gone.
+ if (uvm_parent_gpu_is_accessible(parent_gpu)) {
+ NV_STATUS status = uvm_rm_locked_call(nvUvmInterfaceDestroyAccessCntrInfo(parent_gpu->rm_device,
+ &access_counters->rm_info));
+ UVM_ASSERT(status == NV_OK);
+ }
access_counters->rm_info.accessCntrBufferHandle = 0;
uvm_kvfree(batch_context->notification_cache);
@@ -593,9 +598,12 @@ static void access_counters_yield_ownership(uvm_parent_gpu_t *parent_gpu, NvU32
if (status != NV_OK)
UVM_ASSERT(status == uvm_global_get_status());
- status = uvm_rm_locked_call(nvUvmInterfaceDisableAccessCntr(parent_gpu->rm_device,
- &access_counters->rm_info));
- UVM_ASSERT(status == NV_OK);
+ // Skip RM call if GPU is not accessible (e.g., hot-unplugged).
+ if (uvm_parent_gpu_is_accessible(parent_gpu)) {
+ status = uvm_rm_locked_call(nvUvmInterfaceDisableAccessCntr(parent_gpu->rm_device,
+ &access_counters->rm_info));
+ UVM_ASSERT(status == NV_OK);
+ }
}
// Increment the refcount of access counter enablement. If this is the first
@@ -1753,6 +1761,11 @@ void uvm_service_access_counters(uvm_access_counter_buffer_t *access_counters)
{
NV_STATUS status = NV_OK;
uvm_access_counter_service_batch_context_t *batch_context;
+ uvm_parent_gpu_t *parent_gpu = access_counters->parent_gpu;
+
+ // Check if GPU is still accessible (e.g., not hot-unplugged)
+ if (!uvm_parent_gpu_is_accessible(parent_gpu))
+ return;
batch_context = &access_counters->batch_service_context;
diff --git a/kernel-open/nvidia-uvm/uvm_gpu_isr.c b/kernel-open/nvidia-uvm/uvm_gpu_isr.c
index 48f4b541..db0fecfb 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu_isr.c
+++ b/kernel-open/nvidia-uvm/uvm_gpu_isr.c
@@ -29,6 +29,7 @@
#include "uvm_gpu_access_counters.h"
#include "uvm_gpu_non_replayable_faults.h"
#include "uvm_thread_context.h"
+#include <linux/pci.h>
// Level-based vs pulse-based interrupts
// =====================================
@@ -63,6 +64,21 @@ static void non_replayable_faults_isr_bottom_half_entry(void *args);
// half, only.
static void access_counters_isr_bottom_half_entry(void *args);
+// Check if GPU hardware is accessible (not hot-unplugged).
+// This must be called before any HAL function that accesses GPU registers.
+bool uvm_parent_gpu_is_accessible(uvm_parent_gpu_t *parent_gpu)
+{
+ // If pci_dev is NULL, the GPU has been unregistered
+ if (parent_gpu->pci_dev == NULL)
+ return false;
+
+ // Check if PCI channel is offline (surprise removal/hot-unplug)
+ if (pci_channel_offline(parent_gpu->pci_dev))
+ return false;
+
+ return true;
+}
+
// Increments the reference count tracking whether replayable page fault
// interrupts should be disabled. The caller is guaranteed that replayable page
// faults are disabled upon return. Interrupts might already be disabled prior
@@ -871,7 +887,9 @@ static void uvm_parent_gpu_replayable_faults_intr_disable(uvm_parent_gpu_t *pare
{
uvm_assert_spinlock_locked(&parent_gpu->isr.interrupts_lock);
- if (parent_gpu->isr.replayable_faults.handling && parent_gpu->isr.replayable_faults.disable_intr_ref_count == 0)
+ if (parent_gpu->isr.replayable_faults.handling &&
+ parent_gpu->isr.replayable_faults.disable_intr_ref_count == 0 &&
+ uvm_parent_gpu_is_accessible(parent_gpu))
parent_gpu->fault_buffer_hal->disable_replayable_faults(parent_gpu);
++parent_gpu->isr.replayable_faults.disable_intr_ref_count;
@@ -883,7 +901,9 @@ static void uvm_parent_gpu_replayable_faults_intr_enable(uvm_parent_gpu_t *paren
UVM_ASSERT(parent_gpu->isr.replayable_faults.disable_intr_ref_count > 0);
--parent_gpu->isr.replayable_faults.disable_intr_ref_count;
- if (parent_gpu->isr.replayable_faults.handling && parent_gpu->isr.replayable_faults.disable_intr_ref_count == 0)
+ if (parent_gpu->isr.replayable_faults.handling &&
+ parent_gpu->isr.replayable_faults.disable_intr_ref_count == 0 &&
+ uvm_parent_gpu_is_accessible(parent_gpu))
parent_gpu->fault_buffer_hal->enable_replayable_faults(parent_gpu);
}
@@ -900,7 +920,8 @@ void uvm_access_counters_intr_disable(uvm_access_counter_buffer_t *access_counte
// (disable_intr_ref_count > 0), so the check always returns false when the
// race occurs
if (parent_gpu->isr.access_counters[notif_buf_index].handling_ref_count > 0 &&
- parent_gpu->isr.access_counters[notif_buf_index].disable_intr_ref_count == 0) {
+ parent_gpu->isr.access_counters[notif_buf_index].disable_intr_ref_count == 0 &&
+ uvm_parent_gpu_is_accessible(parent_gpu)) {
parent_gpu->access_counter_buffer_hal->disable_access_counter_notifications(access_counters);
}
@@ -919,7 +940,8 @@ void uvm_access_counters_intr_enable(uvm_access_counter_buffer_t *access_counter
--parent_gpu->isr.access_counters[notif_buf_index].disable_intr_ref_count;
if (parent_gpu->isr.access_counters[notif_buf_index].handling_ref_count > 0 &&
- parent_gpu->isr.access_counters[notif_buf_index].disable_intr_ref_count == 0) {
+ parent_gpu->isr.access_counters[notif_buf_index].disable_intr_ref_count == 0 &&
+ uvm_parent_gpu_is_accessible(parent_gpu)) {
parent_gpu->access_counter_buffer_hal->enable_access_counter_notifications(access_counters);
}
}
diff --git a/kernel-open/nvidia-uvm/uvm_gpu_isr.h b/kernel-open/nvidia-uvm/uvm_gpu_isr.h
index 99fb1ad3..b793bbf4 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu_isr.h
+++ b/kernel-open/nvidia-uvm/uvm_gpu_isr.h
@@ -198,6 +198,11 @@ void uvm_access_counters_intr_enable(uvm_access_counter_buffer_t *access_counter
// g_uvm_global.global_lock is held so that the returned pointer remains valid.
uvm_gpu_t *uvm_parent_gpu_find_first_valid_gpu(uvm_parent_gpu_t *parent_gpu);
+// Check if GPU hardware is accessible (not hot-unplugged).
+// This must be called before any HAL function that accesses GPU registers.
+// Returns false if pci_dev is NULL or PCI channel is offline.
+bool uvm_parent_gpu_is_accessible(uvm_parent_gpu_t *parent_gpu);
+
// Check if any non-replayable faults are pending, and if so schedule a bottom
// half to service them.
//
diff --git a/kernel-open/nvidia-uvm/uvm_gpu_non_replayable_faults.c b/kernel-open/nvidia-uvm/uvm_gpu_non_replayable_faults.c
index 748856e3..4476f7ff 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu_non_replayable_faults.c
+++ b/kernel-open/nvidia-uvm/uvm_gpu_non_replayable_faults.c
@@ -24,6 +24,7 @@
#include "uvm_common.h"
#include "uvm_api.h"
#include "uvm_gpu_non_replayable_faults.h"
+#include "uvm_gpu_isr.h"
#include "uvm_gpu.h"
#include "uvm_hal.h"
#include "uvm_lock.h"
@@ -833,6 +834,11 @@ void uvm_parent_gpu_service_non_replayable_fault_buffer(uvm_parent_gpu_t *parent
{
NvU32 cached_faults;
+ // Check if GPU is still accessible before servicing faults.
+ // After hot-unplug, accessing GPU registers would cause a crash.
+ if (!uvm_parent_gpu_is_accessible(parent_gpu))
+ return;
+
// If this handler is modified to handle fewer than all of the outstanding
// faults, then special handling will need to be added to uvm_suspend()
// to guarantee that fault processing has completed before control is
diff --git a/kernel-open/nvidia-uvm/uvm_gpu_replayable_faults.c b/kernel-open/nvidia-uvm/uvm_gpu_replayable_faults.c
index 5d102298..a635494b 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu_replayable_faults.c
+++ b/kernel-open/nvidia-uvm/uvm_gpu_replayable_faults.c
@@ -27,6 +27,7 @@
#include "uvm_linux.h"
#include "uvm_global.h"
#include "uvm_gpu_replayable_faults.h"
+#include "uvm_gpu_isr.h"
#include "uvm_hal.h"
#include "uvm_kvmalloc.h"
#include "uvm_tools.h"
@@ -299,11 +300,15 @@ void uvm_parent_gpu_fault_buffer_deinit(uvm_parent_gpu_t *parent_gpu)
fault_buffer_deinit_replayable_faults(parent_gpu);
if (parent_gpu->fault_buffer.rm_info.faultBufferHandle) {
- status = uvm_rm_locked_call(nvUvmInterfaceOwnPageFaultIntr(parent_gpu->rm_device, NV_FALSE));
- UVM_ASSERT(status == NV_OK);
-
- uvm_rm_locked_call_void(nvUvmInterfaceDestroyFaultInfo(parent_gpu->rm_device,
- &parent_gpu->fault_buffer.rm_info));
+ // Skip RM calls if GPU is not accessible (e.g., hot-unplugged).
+ // The nvidia module's internal state is corrupted when the GPU is gone.
+ if (uvm_parent_gpu_is_accessible(parent_gpu)) {
+ status = uvm_rm_locked_call(nvUvmInterfaceOwnPageFaultIntr(parent_gpu->rm_device, NV_FALSE));
+ UVM_ASSERT(status == NV_OK);
+
+ uvm_rm_locked_call_void(nvUvmInterfaceDestroyFaultInfo(parent_gpu->rm_device,
+ &parent_gpu->fault_buffer.rm_info));
+ }
parent_gpu->fault_buffer.rm_info.faultBufferHandle = 0;
}
@@ -661,9 +666,21 @@ NV_STATUS uvm_gpu_replayable_buffer_flush(uvm_gpu_t *gpu)
{
NV_STATUS status = NV_OK;
+ // Check if GPU hardware is still accessible before attempting to flush.
+ // After hot-unplug, the GPU registers are no longer mapped and accessing
+ // them would cause a page fault crash.
+ if (!uvm_parent_gpu_is_accessible(gpu->parent))
+ return NV_ERR_GPU_IS_LOST;
+
// Disables replayable fault interrupts and fault servicing
uvm_parent_gpu_replayable_faults_isr_lock(gpu->parent);
+ // Re-check after acquiring the lock in case GPU was removed concurrently
+ if (!uvm_parent_gpu_is_accessible(gpu->parent)) {
+ uvm_parent_gpu_replayable_faults_isr_unlock(gpu->parent);
+ return NV_ERR_GPU_IS_LOST;
+ }
+
status = fault_buffer_flush_locked(gpu->parent,
gpu,
UVM_GPU_BUFFER_FLUSH_MODE_WAIT_UPDATE_PUT,
@@ -2903,6 +2920,11 @@ void uvm_parent_gpu_service_replayable_faults(uvm_parent_gpu_t *parent_gpu)
uvm_replayable_fault_buffer_t *replayable_faults = &parent_gpu->fault_buffer.replayable;
uvm_fault_service_batch_context_t *batch_context = &replayable_faults->batch_service_context;
+ // Check if GPU is still accessible before servicing faults.
+ // After hot-unplug, accessing GPU registers would cause a crash.
+ if (!uvm_parent_gpu_is_accessible(parent_gpu))
+ return;
+
uvm_tracker_init(&batch_context->tracker);
// Process all faults in the buffer
diff --git a/kernel-open/nvidia-uvm/uvm_gpu_semaphore.c b/kernel-open/nvidia-uvm/uvm_gpu_semaphore.c
index 478c1aaf..dae09f31 100644
--- a/kernel-open/nvidia-uvm/uvm_gpu_semaphore.c
+++ b/kernel-open/nvidia-uvm/uvm_gpu_semaphore.c
@@ -27,6 +27,7 @@
#include "uvm_kvmalloc.h"
#include "uvm_channel.h" // For UVM_GPU_SEMAPHORE_MAX_JUMP
#include "uvm_conf_computing.h"
+#include "uvm_gpu_isr.h"
#define UVM_SEMAPHORE_SIZE 4
#define UVM_SEMAPHORE_PAGE_SIZE PAGE_SIZE
@@ -822,10 +823,18 @@ static NvU64 update_completed_value_locked(uvm_gpu_tracking_semaphore_t *trackin
NvU64 uvm_gpu_tracking_semaphore_update_completed_value(uvm_gpu_tracking_semaphore_t *tracking_semaphore)
{
NvU64 completed;
+ uvm_gpu_t *gpu = tracking_semaphore->semaphore.page->pool->gpu;
// Check that the GPU which owns the semaphore is still present
UVM_ASSERT(tracking_semaphore_check_gpu(tracking_semaphore));
+ // If the GPU is not accessible (surprise removed), return the cached
+ // completed value without reading from GPU memory. Reading from GPU
+ // memory after surprise removal returns garbage values that cause
+ // assertion failures.
+ if (!uvm_parent_gpu_is_accessible(gpu->parent))
+ return atomic64_read(&tracking_semaphore->completed_value);
+
if (tracking_semaphore_uses_mutex(tracking_semaphore))
uvm_mutex_lock(&tracking_semaphore->m_lock);
else
@@ -844,10 +853,16 @@ NvU64 uvm_gpu_tracking_semaphore_update_completed_value(uvm_gpu_tracking_semapho
bool uvm_gpu_tracking_semaphore_is_value_completed(uvm_gpu_tracking_semaphore_t *tracking_sem, NvU64 value)
{
NvU64 completed = atomic64_read(&tracking_sem->completed_value);
+ uvm_gpu_t *gpu = tracking_sem->semaphore.page->pool->gpu;
// Check that the GPU which owns the semaphore is still present
UVM_ASSERT(tracking_semaphore_check_gpu(tracking_sem));
+ // If the GPU is not accessible, consider all values completed to avoid
+ // spinning forever waiting for a GPU that's gone.
+ if (!uvm_parent_gpu_is_accessible(gpu->parent))
+ return true;
+
if (completed >= value) {
// atomic64_read() doesn't imply any memory barriers and we need all
// subsequent memory accesses in this thread to be ordered after the
diff --git a/kernel-open/nvidia-uvm/uvm_pmm_gpu.c b/kernel-open/nvidia-uvm/uvm_pmm_gpu.c
index 34e56f22..3295442e 100644
--- a/kernel-open/nvidia-uvm/uvm_pmm_gpu.c
+++ b/kernel-open/nvidia-uvm/uvm_pmm_gpu.c
@@ -165,6 +165,7 @@
#include "nv_uvm_interface.h"
#include "uvm_api.h"
#include "uvm_gpu.h"
+#include "uvm_gpu_isr.h"
#include "uvm_pmm_gpu.h"
#include "uvm_mem.h"
#include "uvm_mmu.h"
@@ -2082,6 +2083,14 @@ void free_root_chunk(uvm_pmm_gpu_t *pmm, uvm_gpu_root_chunk_t *root_chunk, free_
if (chunk->is_zero)
flags |= UVM_PMA_FREE_IS_ZERO;
+ // Skip PMA free if GPU is not accessible (e.g., hot-unplugged).
+ // Calling into the nvidia module with a gone GPU causes hangs
+ // due to corrupted locks.
+ if (!uvm_parent_gpu_is_accessible(gpu->parent)) {
+ uvm_up_read(&pmm->pma_lock);
+ return;
+ }
+
nvUvmInterfacePmaFreePages(pmm->pma, &chunk->address, 1, UVM_CHUNK_SIZE_MAX, flags);
uvm_up_read(&pmm->pma_lock);
@@ -3120,7 +3129,10 @@ void uvm_pmm_gpu_deinit(uvm_pmm_gpu_t *pmm)
UVM_ASSERT(uvm_devmem_check_orphan_pages(pmm));
release_free_root_chunks(pmm);
- if (gpu->mem_info.size != 0 && gpu_supports_pma_eviction(gpu))
+ // Skip unregistering callbacks if GPU is not accessible (hot-unplugged).
+ // The nvidia module's internal state is corrupted when the GPU is gone.
+ if (gpu->mem_info.size != 0 && gpu_supports_pma_eviction(gpu) &&
+ uvm_parent_gpu_is_accessible(gpu->parent))
nvUvmInterfacePmaUnregisterEvictionCallbacks(pmm->pma);
// TODO: Bug 1766184: Handle ECC/RC
diff --git a/kernel-open/nvidia-uvm/uvm_rm_mem.c b/kernel-open/nvidia-uvm/uvm_rm_mem.c
index 756080fb..767f9e8d 100644
--- a/kernel-open/nvidia-uvm/uvm_rm_mem.c
+++ b/kernel-open/nvidia-uvm/uvm_rm_mem.c
@@ -23,6 +23,7 @@
#include "uvm_rm_mem.h"
#include "uvm_gpu.h"
+#include "uvm_gpu_isr.h"
#include "uvm_global.h"
#include "uvm_kvmalloc.h"
#include "uvm_linux.h"
@@ -298,8 +299,11 @@ void uvm_rm_mem_unmap_cpu(uvm_rm_mem_t *rm_mem)
if (!uvm_rm_mem_mapped_on_cpu(rm_mem))
return;
- uvm_rm_locked_call_void(nvUvmInterfaceMemoryCpuUnMap(rm_mem->gpu_owner->rm_address_space,
- uvm_rm_mem_get_cpu_va(rm_mem)));
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (uvm_parent_gpu_is_accessible(rm_mem->gpu_owner->parent))
+ uvm_rm_locked_call_void(nvUvmInterfaceMemoryCpuUnMap(rm_mem->gpu_owner->rm_address_space,
+ uvm_rm_mem_get_cpu_va(rm_mem)));
rm_mem_clear_cpu_va(rm_mem);
}
@@ -355,7 +359,12 @@ static void rm_mem_unmap_gpu(uvm_rm_mem_t *rm_mem, uvm_gpu_t *gpu)
rm_mem_unmap_gpu_proxy(rm_mem, gpu);
va = uvm_rm_mem_get_gpu_uvm_va(rm_mem, gpu);
- uvm_rm_locked_call_void(nvUvmInterfaceMemoryFree(gpu->rm_address_space, va));
+
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (uvm_parent_gpu_is_accessible(gpu->parent))
+ uvm_rm_locked_call_void(nvUvmInterfaceMemoryFree(gpu->rm_address_space, va));
+
rm_mem_clear_gpu_va(rm_mem, gpu);
}
diff --git a/kernel-open/nvidia-uvm/uvm_user_channel.c b/kernel-open/nvidia-uvm/uvm_user_channel.c
index 75e04e3a..e40eb42b 100644
--- a/kernel-open/nvidia-uvm/uvm_user_channel.c
+++ b/kernel-open/nvidia-uvm/uvm_user_channel.c
@@ -32,6 +32,7 @@
#include "uvm_kvmalloc.h"
#include "uvm_api.h"
#include "uvm_gpu.h"
+#include "uvm_gpu_isr.h"
#include "uvm_tracker.h"
#include "uvm_map_external.h"
#include "nv_uvm_interface.h"
@@ -782,6 +783,14 @@ void uvm_user_channel_stop(uvm_user_channel_t *user_channel)
// write mode.
uvm_assert_rwsem_locked_read(&va_space->lock);
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // client handles will result in repeated NV_ERR_INVALID_OBJECT_HANDLE
+ // errors during teardown.
+ if (!uvm_parent_gpu_is_accessible(user_channel->gpu->parent)) {
+ atomic_set(&user_channel->is_bound, 0);
+ return;
+ }
+
// TODO: Bug 1737765. This doesn't stop the user from putting the
// channel back on the runlist, which could put stale instance
// pointers back in the fault buffer.
@@ -886,7 +895,9 @@ void uvm_user_channel_destroy_detached(uvm_user_channel_t *user_channel)
uvm_kvfree(user_channel->resources);
}
- if (user_channel->rm_retained_channel)
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (user_channel->rm_retained_channel && uvm_parent_gpu_is_accessible(user_channel->gpu->parent))
uvm_rm_locked_call_void(nvUvmInterfaceReleaseChannel(user_channel->rm_retained_channel));
uvm_user_channel_release(user_channel);
diff --git a/kernel-open/nvidia-uvm/uvm_va_space.c b/kernel-open/nvidia-uvm/uvm_va_space.c
index 53407e3f..78e37400 100644
--- a/kernel-open/nvidia-uvm/uvm_va_space.c
+++ b/kernel-open/nvidia-uvm/uvm_va_space.c
@@ -32,6 +32,7 @@
#include "uvm_tools.h"
#include "uvm_thread_context.h"
#include "uvm_hal.h"
+#include "uvm_gpu_isr.h"
#include "uvm_map_external.h"
#include "uvm_ats.h"
#include "uvm_gpu_replayable_faults.h"
@@ -1458,6 +1459,13 @@ void uvm_gpu_va_space_unset_page_dir(uvm_gpu_va_space_t *gpu_va_space)
if (gpu_va_space->did_set_page_directory) {
NV_STATUS status;
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (!uvm_parent_gpu_is_accessible(gpu_va_space->gpu->parent)) {
+ gpu_va_space->did_set_page_directory = false;
+ return;
+ }
+
status = uvm_rm_locked_call(nvUvmInterfaceUnsetPageDirectory(gpu_va_space->duped_gpu_va_space));
UVM_ASSERT_MSG(status == NV_OK,
"nvUvmInterfaceUnsetPageDirectory() failed: %s, GPU %s\n",
@@ -1509,7 +1517,9 @@ static void destroy_gpu_va_space(uvm_gpu_va_space_t *gpu_va_space)
if (gpu_va_space->page_tables.root)
uvm_page_tree_deinit(&gpu_va_space->page_tables);
- if (gpu_va_space->duped_gpu_va_space)
+ // Skip RM call if GPU has been surprise removed. Calling RM with stale
+ // handles will result in NV_ERR_INVALID_OBJECT_HANDLE errors.
+ if (gpu_va_space->duped_gpu_va_space && uvm_parent_gpu_is_accessible(gpu_va_space->gpu->parent))
uvm_rm_locked_call_void(nvUvmInterfaceAddressSpaceDestroy(gpu_va_space->duped_gpu_va_space));
// If the state is DEAD, then this GPU VA space is tracked in
diff --git a/kernel-open/nvidia/nv-acpi.c b/kernel-open/nvidia/nv-acpi.c
index d0fe94a0..ee0ae995 100644
--- a/kernel-open/nvidia/nv-acpi.c
+++ b/kernel-open/nvidia/nv-acpi.c
@@ -252,12 +252,28 @@ static void nv_acpi_notify_event(acpi_handle handle, u32 event_type, void *data)
{
nv_acpi_t *pNvAcpiObject = data;
nv_state_t *nvl = pNvAcpiObject->notifier_data;
+ nv_state_t *nv;
+
+ if (nvl == NULL)
+ return;
+
+ nv = NV_STATE_PTR(nvl);
+ if (nv == NULL)
+ return;
+
+ /*
+ * Check if we're in surprise removal before processing ACPI events.
+ * This can happen during Thunderbolt eGPU hot-unplug where the device
+ * is being removed but ACPI events are still being delivered.
+ */
+ if (nv->flags & NV_FLAG_IN_SURPRISE_REMOVAL)
+ return;
/*
* Function to handle device specific ACPI events such as display hotplug,
* GPS and D-notifier events.
*/
- rm_acpi_notify(pNvAcpiObject->sp, NV_STATE_PTR(nvl), event_type);
+ rm_acpi_notify(pNvAcpiObject->sp, nv, event_type);
}
void nv_acpi_register_notifier(nv_linux_state_t *nvl)
diff --git a/kernel-open/nvidia/nv-i2c.c b/kernel-open/nvidia/nv-i2c.c
index a0f61ed2..4da6bfca 100644
--- a/kernel-open/nvidia/nv-i2c.c
+++ b/kernel-open/nvidia/nv-i2c.c
@@ -44,6 +44,15 @@ static int nv_i2c_algo_master_xfer(struct i2c_adapter *adapter, struct i2c_msg m
#endif
;
+ /*
+ * Check if the GPU is in surprise removal (e.g., Thunderbolt unplug).
+ * If so, return immediately to avoid hanging on RPC calls to GSP.
+ */
+ if (nv_check_gpu_state(nv) != NV_OK)
+ {
+ return -ENODEV;
+ }
+
rc = nv_kmem_cache_alloc_stack(&sp);
if (rc != 0)
{
@@ -93,6 +102,15 @@ static int nv_i2c_algo_smbus_xfer(
NV_STATUS rmStatus = NV_OK;
nvidia_stack_t *sp = NULL;
+ /*
+ * Check if the GPU is in surprise removal (e.g., Thunderbolt unplug).
+ * If so, return immediately to avoid hanging on RPC calls to GSP.
+ */
+ if (nv_check_gpu_state(nv) != NV_OK)
+ {
+ return -ENODEV;
+ }
+
rc = nv_kmem_cache_alloc_stack(&sp);
if (rc != 0)
{
@@ -196,6 +214,15 @@ static u32 nv_i2c_algo_functionality(struct i2c_adapter *adapter)
u32 ret = I2C_FUNC_I2C;
nvidia_stack_t *sp = NULL;
+ /*
+ * Check if the GPU is in surprise removal (e.g., Thunderbolt unplug).
+ * If so, return 0 to indicate no functionality available.
+ */
+ if (nv_check_gpu_state(nv) != NV_OK)
+ {
+ return 0;
+ }
+
if (nv_kmem_cache_alloc_stack(&sp) != 0)
{
return 0;
diff --git a/kernel-open/nvidia/nv-pci.c b/kernel-open/nvidia/nv-pci.c
index 82338b8e..01e92398 100644
--- a/kernel-open/nvidia/nv-pci.c
+++ b/kernel-open/nvidia/nv-pci.c
@@ -27,6 +27,7 @@
#include "nv-msi.h"
#include "nv-hypervisor.h"
#include "nv-reg.h"
+#include "nv-rsync.h"
#if defined(NV_VGPU_KVM_BUILD)
#include "nv-vgpu-vfio-interface.h"
@@ -2470,20 +2471,33 @@ static void nv_pci_remove_helper(struct pci_dev *pci_dev, bool block_if_gpu_in_u
/*
* Sanity check: A removed device shouldn't have a non-zero usage_count.
* For eGPU, fall off the bus along with clients active is a valid scenario.
- * Hence skipping the sanity check for eGPU.
+ * We still wait for a short time to allow in-progress close operations
+ * to complete, but with a timeout to prevent hangs.
*/
- if ((atomic64_read(&nvl->usage_count) != 0) && !(nv->is_external_gpu))
+ if (atomic64_read(&nvl->usage_count) != 0)
{
+ /*
+ * For external GPU: wait up to 5 seconds (10 iterations * 500ms)
+ * For internal GPU: wait up to 60 seconds (120 iterations * 500ms)
+ * This prevents indefinite hangs while still allowing time for
+ * graceful cleanup of in-progress operations.
+ */
+ int max_wait_iterations = nv->is_external_gpu ? 10 : 120;
+ int wait_iterations = 0;
+
nv_printf(NV_DBG_ERRORS,
- "NVRM: Attempting to remove device %04x:%02x:%02x.%x with non-zero usage count!\n",
+ "NVRM: Attempting to remove device %04x:%02x:%02x.%x with non-zero usage count (%lld)%s\n",
NV_PCI_DOMAIN_NUMBER(pci_dev), NV_PCI_BUS_NUMBER(pci_dev),
- NV_PCI_SLOT_NUMBER(pci_dev), PCI_FUNC(pci_dev->devfn));
+ NV_PCI_SLOT_NUMBER(pci_dev), PCI_FUNC(pci_dev->devfn),
+ atomic64_read(&nvl->usage_count),
+ nv->is_external_gpu ? " (external GPU)" : "");
/*
* We can't return from this function without corrupting state, so we wait for
- * the usage count to go to zero.
+ * the usage count to go to zero, but with a timeout.
*/
- while (atomic64_read(&nvl->usage_count) != 0)
+ while ((atomic64_read(&nvl->usage_count) != 0) &&
+ (wait_iterations < max_wait_iterations))
{
/*
* While waiting, release the locks so that other threads can make
@@ -2493,6 +2507,7 @@ static void nv_pci_remove_helper(struct pci_dev *pci_dev, bool block_if_gpu_in_u
UNLOCK_NV_LINUX_DEVICES();
os_delay(500);
+ wait_iterations++;
/* Re-acquire the locks before checking again */
LOCK_NV_LINUX_DEVICES();
@@ -2511,10 +2526,32 @@ static void nv_pci_remove_helper(struct pci_dev *pci_dev, bool block_if_gpu_in_u
down(&nvl->ldata_lock);
}
- nv_printf(NV_DBG_ERRORS,
- "NVRM: Continuing with GPU removal for device %04x:%02x:%02x.%x\n",
- NV_PCI_DOMAIN_NUMBER(pci_dev), NV_PCI_BUS_NUMBER(pci_dev),
- NV_PCI_SLOT_NUMBER(pci_dev), PCI_FUNC(pci_dev->devfn));
+ if (atomic64_read(&nvl->usage_count) != 0)
+ {
+ nv_printf(NV_DBG_ERRORS,
+ "NVRM: Timeout waiting for usage count on device %04x:%02x:%02x.%x (remaining: %lld). Forcing removal.\n",
+ NV_PCI_DOMAIN_NUMBER(pci_dev), NV_PCI_BUS_NUMBER(pci_dev),
+ NV_PCI_SLOT_NUMBER(pci_dev), PCI_FUNC(pci_dev->devfn),
+ atomic64_read(&nvl->usage_count));
+ /*
+ * Force the surprise removal flag so that any remaining
+ * close operations will take the fast-path.
+ */
+ nv->flags |= NV_FLAG_IN_SURPRISE_REMOVAL;
+
+ /*
+ * Mark that we had a surprise removal so rsync cleanup
+ * warnings are suppressed during module unload.
+ */
+ nv_set_rsync_had_surprise_removal();
+ }
+ else
+ {
+ nv_printf(NV_DBG_ERRORS,
+ "NVRM: Continuing with GPU removal for device %04x:%02x:%02x.%x\n",
+ NV_PCI_DOMAIN_NUMBER(pci_dev), NV_PCI_BUS_NUMBER(pci_dev),
+ NV_PCI_SLOT_NUMBER(pci_dev), PCI_FUNC(pci_dev->devfn));
+ }
}
rm_check_for_gpu_surprise_removal(sp, nv);
diff --git a/kernel-open/nvidia/nv-rsync.c b/kernel-open/nvidia/nv-rsync.c
index 88863dab..f98c52c8 100644
--- a/kernel-open/nvidia/nv-rsync.c
+++ b/kernel-open/nvidia/nv-rsync.c
@@ -31,6 +31,7 @@ void nv_init_rsync_info(
)
{
g_rsync_info.relaxed_ordering_mode = NV_FALSE;
+ g_rsync_info.had_surprise_removal = NV_FALSE;
g_rsync_info.usage_count = 0;
g_rsync_info.data = NULL;
NV_INIT_MUTEX(&g_rsync_info.lock);
@@ -40,9 +41,17 @@ void nv_destroy_rsync_info(
void
)
{
- WARN_ON(g_rsync_info.data);
- WARN_ON(g_rsync_info.usage_count);
- WARN_ON(g_rsync_info.relaxed_ordering_mode);
+ /*
+ * After GPU surprise removal (e.g., Thunderbolt eGPU hot-unplug),
+ * these may not have been properly cleaned up. Skip warnings in
+ * that case since the cleanup failure is expected.
+ */
+ if (!g_rsync_info.had_surprise_removal)
+ {
+ WARN_ON(g_rsync_info.data);
+ WARN_ON(g_rsync_info.usage_count);
+ WARN_ON(g_rsync_info.relaxed_ordering_mode);
+ }
}
int nv_get_rsync_info(
@@ -100,6 +109,18 @@ void nv_put_rsync_info(
up(&g_rsync_info.lock);
}
+/*
+ * Mark that a GPU surprise removal occurred. This is used to suppress
+ * warnings about unclean rsync state during module unload, since the
+ * cleanup may be incomplete after forced removal.
+ */
+void nv_set_rsync_had_surprise_removal(
+ void
+)
+{
+ g_rsync_info.had_surprise_removal = NV_TRUE;
+}
+
int nv_register_rsync_driver(
int (*get_relaxed_ordering_mode)(int *mode, void *data),
void (*put_relaxed_ordering_mode)(int mode, void *data),
diff --git a/kernel-open/nvidia/nv-rsync.h b/kernel-open/nvidia/nv-rsync.h
index cc0e1a2e..c1cadefb 100644
--- a/kernel-open/nvidia/nv-rsync.h
+++ b/kernel-open/nvidia/nv-rsync.h
@@ -31,6 +31,7 @@ typedef struct nv_rsync_info
struct semaphore lock;
uint32_t usage_count;
NvBool relaxed_ordering_mode;
+ NvBool had_surprise_removal;
int (*get_relaxed_ordering_mode)(int *mode, void *data);
void (*put_relaxed_ordering_mode)(int mode, void *data);
void (*wait_for_rsync)(struct pci_dev *gpu, void *data);
@@ -41,6 +42,7 @@ void nv_init_rsync_info(void);
void nv_destroy_rsync_info(void);
int nv_get_rsync_info(void);
void nv_put_rsync_info(void);
+void nv_set_rsync_had_surprise_removal(void);
int nv_register_rsync_driver(
int (*get_relaxed_ordering_mode)(int *mode, void *data),
void (*put_relaxed_ordering_mode)(int mode, void *data),
diff --git a/kernel-open/nvidia/nv.c b/kernel-open/nvidia/nv.c
index b771a001..315a6b82 100644
--- a/kernel-open/nvidia/nv.c
+++ b/kernel-open/nvidia/nv.c
@@ -2218,14 +2218,49 @@ nvidia_close_callback(
static void nvidia_close_deferred(void *data)
{
nv_linux_file_private_t *nvlfp = data;
+ nv_linux_state_t *nvl = nvlfp->nvptr;
+ nv_state_t *nv = nvl ? NV_STATE_PTR(nvl) : NULL;
+ NvBool got_lock = NV_FALSE;
+ NvBool in_surprise_removal = NV_FALSE;
nv_wait_open_complete(nvlfp);
- down_read(&nv_system_pm_lock);
+ /*
+ * Check if we're in surprise removal before trying to acquire the lock.
+ * If the device is being removed (e.g., Thunderbolt unplug), we should
+ * not block on the PM lock as it may be held by the removal path.
+ */
+ if (nv != NULL)
+ {
+ in_surprise_removal = NV_IS_DEVICE_IN_SURPRISE_REMOVAL(nv);
+ }
+
+ if (in_surprise_removal)
+ {
+ /*
+ * For surprise removal, try to acquire the lock but don't block.
+ * If we can't get it, proceed without it - cleanup will be minimal
+ * anyway since the hardware is gone.
+ */
+ got_lock = down_read_trylock(&nv_system_pm_lock);
+ if (!got_lock)
+ {
+ nv_printf(NV_DBG_INFO,
+ "NVRM: Surprise removal - proceeding with close without PM lock\n");
+ }
+ }
+ else
+ {
+ down_read(&nv_system_pm_lock);
+ got_lock = NV_TRUE;
+ }
nvidia_close_callback(nvlfp);
- up_read(&nv_system_pm_lock);
+ if (got_lock)
+ {
+ up_read(&nv_system_pm_lock);
+ }
}
int
@@ -2236,6 +2271,9 @@ nvidia_close(
{
int rc;
nv_linux_file_private_t *nvlfp = NV_GET_LINUX_FILE_PRIVATE(file);
+ nv_linux_state_t *nvl;
+ nv_state_t *nv;
+ NvBool in_surprise_removal = NV_FALSE;
nv_printf(NV_DBG_INFO,
"NVRM: nvidia_close on GPU with minor number %d\n",
@@ -2248,10 +2286,44 @@ nvidia_close(
NV_SET_FILE_PRIVATE(file, NULL);
+ /*
+ * Check if the device is in surprise removal (e.g., Thunderbolt unplug).
+ * If so, we should not block waiting for the PM lock as it may be held
+ * by the removal path, causing a deadlock.
+ */
+ nvl = nvlfp->nvptr;
+ if (nvl != NULL)
+ {
+ nv = NV_STATE_PTR(nvl);
+ in_surprise_removal = NV_IS_DEVICE_IN_SURPRISE_REMOVAL(nv);
+ }
+
rc = nv_wait_open_complete_interruptible(nvlfp);
if (rc == 0)
{
- rc = nv_down_read_interruptible(&nv_system_pm_lock);
+ if (in_surprise_removal)
+ {
+ /*
+ * For surprise removal, try to acquire the lock but don't block.
+ * If we can't get it, defer the close to a worker thread that
+ * will handle it properly.
+ */
+ if (down_read_trylock(&nv_system_pm_lock))
+ {
+ nvidia_close_callback(nvlfp);
+ up_read(&nv_system_pm_lock);
+ return 0;
+ }
+ /*
+ * Couldn't get the lock - fall through to defer the close.
+ * Set rc to indicate we need to defer.
+ */
+ rc = -EAGAIN;
+ }
+ else
+ {
+ rc = nv_down_read_interruptible(&nv_system_pm_lock);
+ }
}
if (rc == 0)
@@ -5320,6 +5392,8 @@ int nvidia_dev_get(NvU32 gpu_id, nvidia_stack_t *sp, NvBool reset_aware)
void nvidia_dev_put(NvU32 gpu_id, nvidia_stack_t *sp, NvBool reset_aware)
{
nv_linux_state_t *nvl;
+ nv_state_t *nv;
+ NV_STATUS status;
/* Takes nvl->ldata_lock */
nvl = find_gpu_id(gpu_id);
diff --git a/src/nvidia-modeset/exports_link_command.txt b/src/nvidia-modeset/exports_link_command.txt
index 76064078..4929a370 100644
--- a/src/nvidia-modeset/exports_link_command.txt
+++ b/src/nvidia-modeset/exports_link_command.txt
@@ -1 +1 @@
- --undefined=nvKmsIoctl --undefined=nvKmsClose --undefined=nvKmsOpen --undefined=nvKmsModuleLoad --undefined=nvKmsModuleUnload --undefined=nvKmsSuspend --undefined=nvKmsResume --undefined=nvKmsGetProcFiles --undefined=nvKmsReadConf --undefined=nvKmsKapiHandleEventQueueChange --undefined=nvKmsKapiGetFunctionsTableInternal --undefined=nvKmsKapiProbe --undefined=nvKmsKapiRemove --undefined=nvKmsKapiSuspendResume --undefined=nvKmsSetBacklight --undefined=nvKmsGetBacklight --undefined=nvKmsKapiF16ToF32Internal --undefined=nvKmsKapiF32ToF16Internal --undefined=nvKmsKapiF32MulInternal --undefined=nvKmsKapiF32DivInternal --undefined=nvKmsKapiF32AddInternal --undefined=nvKmsKapiF32ToUI32RMinMagInternal --undefined=nvKmsKapiUI32ToF32Internal
+ --undefined=nvKmsIoctl --undefined=nvKmsClose --undefined=nvKmsOpen --undefined=nvKmsModuleLoad --undefined=nvKmsModuleUnload --undefined=nvKmsSuspend --undefined=nvKmsResume --undefined=nvKmsGetProcFiles --undefined=nvKmsReadConf --undefined=nvKmsKapiHandleEventQueueChange --undefined=nvKmsKapiGetFunctionsTableInternal --undefined=nvKmsKapiProbe --undefined=nvKmsKapiRemove --undefined=nvKmsKapiSuspendResume --undefined=nvKmsSetBacklight --undefined=nvKmsGetBacklight --undefined=nvKmsKapiF16ToF32Internal --undefined=nvKmsKapiF32ToF16Internal --undefined=nvKmsKapiF32MulInternal --undefined=nvKmsKapiF32DivInternal --undefined=nvKmsKapiF32AddInternal --undefined=nvKmsKapiF32ToUI32RMinMagInternal --undefined=nvKmsKapiUI32ToF32Internal --undefined=nvKmsGpuLost
diff --git a/src/nvidia-modeset/include/nvkms-private.h b/src/nvidia-modeset/include/nvkms-private.h
index 31c76081..df468bc9 100644
--- a/src/nvidia-modeset/include/nvkms-private.h
+++ b/src/nvidia-modeset/include/nvkms-private.h
@@ -35,6 +35,10 @@ struct NvKmsPerOpenDev *nvAllocPerOpenDev(struct NvKmsPerOpen *pOpen,
void nvRevokeDevice(NVDevEvoPtr pDevEvo);
+void nvInvalidateDeviceReferences(NVDevEvoPtr pDevEvo);
+
+NvBool nvReinitializeGlobalClientAfterGpuLost(void);
+
void nvFreePerOpenDev(struct NvKmsPerOpen *pOpen,
struct NvKmsPerOpenDev *pOpenDev);
diff --git a/src/nvidia-modeset/include/nvkms-types.h b/src/nvidia-modeset/include/nvkms-types.h
index b0c60a60..dae8848c 100644
--- a/src/nvidia-modeset/include/nvkms-types.h
+++ b/src/nvidia-modeset/include/nvkms-types.h
@@ -1164,6 +1164,13 @@ typedef struct _NVEvoDevRec {
*/
NvBool isHeadSurfaceSupported : 1;
+ /*
+ * Indicates the GPU has been lost (e.g., Thunderbolt/eGPU hot-unplug).
+ * When set, any operations that would access GPU hardware should be
+ * skipped to avoid kernel crashes.
+ */
+ NvBool gpuLost : 1;
+
nvkms_timer_handle_t *postFlipIMPTimer;
nvkms_timer_handle_t *consoleRestoreTimer;
diff --git a/src/nvidia-modeset/kapi/interface/nvkms-kapi.h b/src/nvidia-modeset/kapi/interface/nvkms-kapi.h
index 8e838eaf..2011874e 100644
--- a/src/nvidia-modeset/kapi/interface/nvkms-kapi.h
+++ b/src/nvidia-modeset/kapi/interface/nvkms-kapi.h
@@ -679,6 +679,17 @@ struct NvKmsKapiFunctionsTable {
*/
void (*freeDevice)(struct NvKmsKapiDevice *device);
+ /*!
+ * Frees a device during surprise removal (e.g., Thunderbolt eGPU unplug).
+ * This skips all hardware access and only releases kernel resources.
+ * Use this instead of freeDevice() when the GPU hardware is no longer
+ * accessible to avoid page faults and hangs.
+ *
+ * \param [in] device A device returned by allocateDevice().
+ * This function is a no-op if device is not valid.
+ */
+ void (*freeDeviceForSurpriseRemoval)(struct NvKmsKapiDevice *device);
+
/*!
* Grab ownership of device, ownership is required to do modeset.
*
diff --git a/src/nvidia-modeset/kapi/src/nvkms-kapi.c b/src/nvidia-modeset/kapi/src/nvkms-kapi.c
index 51178eee..29da3a8f 100644
--- a/src/nvidia-modeset/kapi/src/nvkms-kapi.c
+++ b/src/nvidia-modeset/kapi/src/nvkms-kapi.c
@@ -641,6 +641,78 @@ static void FreeDevice(struct NvKmsKapiDevice *device)
nvKmsKapiFree(device);
}
+/*
+ * FreeDeviceForSurpriseRemoval - Free device without hardware access.
+ *
+ * This is used for Thunderbolt eGPU hot-unplug or other surprise removal
+ * scenarios where the GPU hardware is no longer accessible. We skip all
+ * hardware operations (NVKMS ioctls, RM API calls) that would cause page
+ * faults or hangs when trying to access unmapped GPU memory.
+ *
+ * We only:
+ * 1. Mark GPU as lost to prevent hardware access
+ * 2. Release the GPU reference count (nvkms_close_gpu)
+ * 3. Clean up kernel memory resources (handle allocator, semaphore, device struct)
+ *
+ * We skip:
+ * - KmsFreeDevice() - would call nvkms_ioctl_from_kapi() which accesses hardware
+ * - RmFreeDevice() - would call nvRmApiFree() which accesses hardware
+ *
+ * The hardware resources will be cleaned up when the GPU is physically
+ * removed from the system.
+ */
+static void FreeDeviceForSurpriseRemoval(struct NvKmsKapiDevice *device)
+{
+ if (device == NULL) {
+ return;
+ }
+
+ /*
+ * Mark the GPU as lost in NVKMS. This sets the gpuLost flag to prevent
+ * any hardware access, and cancels pending timers that might try to
+ * access the removed GPU.
+ */
+ nvkms_gpu_lost(device->gpuId);
+
+ /*
+ * Clear device handles to prevent any stale references.
+ * Don't call nvRmApiFree() as that would access hardware.
+ */
+ device->hKmsDevice = 0;
+ device->hKmsDisp = 0;
+ device->hRmSubDevice = 0;
+ device->hRmDevice = 0;
+ device->hRmClient = 0;
+ device->smgGpuInstSubscriptionHdl = 0;
+ device->smgComputeInstSubscriptionHdl = 0;
+
+ /*
+ * Tear down the handle allocator - this only frees kernel memory
+ * (bitmaps), no hardware access.
+ */
+ nvTearDownUnixRmHandleAllocator(&device->handleAllocator);
+ device->deviceInstance = 0;
+
+ /*
+ * Clear pKmsOpen - we can't call nvkms_close_from_kapi() as that
+ * would try to access hardware through nvKmsClose(). The popen
+ * structure will be leaked, but this only happens during surprise
+ * removal which is an abnormal condition.
+ */
+ device->pKmsOpen = NULL;
+
+ /* Lower the reference count of gpu - this is safe, no hardware access */
+ nvkms_close_gpu(device->gpuId, NV_TRUE /* reset_aware */);
+
+ /* Free kernel memory resources */
+ if (device->pSema != NULL) {
+ nvkms_sema_free(device->pSema);
+ device->pSema = NULL;
+ }
+
+ nvKmsKapiFree(device);
+}
+
NvBool nvKmsKapiAllocateSystemMemory(struct NvKmsKapiDevice *device,
NvU32 hRmHandle,
enum NvKmsSurfaceMemoryLayout layout,
@@ -4138,6 +4210,7 @@ NvBool nvKmsKapiGetFunctionsTableInternal
funcsTable->allocateDevice = AllocateDevice;
funcsTable->freeDevice = FreeDevice;
+ funcsTable->freeDeviceForSurpriseRemoval = FreeDeviceForSurpriseRemoval;
funcsTable->grabOwnership = GrabOwnership;
funcsTable->releaseOwnership = ReleaseOwnership;
diff --git a/src/nvidia-modeset/os-interface/include/nvidia-modeset-os-interface.h b/src/nvidia-modeset/os-interface/include/nvidia-modeset-os-interface.h
index a634ec2b..d6709b46 100644
--- a/src/nvidia-modeset/os-interface/include/nvidia-modeset-os-interface.h
+++ b/src/nvidia-modeset/os-interface/include/nvidia-modeset-os-interface.h
@@ -317,6 +317,12 @@ void* nvkms_get_per_open_data(int fd);
NvBool nvkms_open_gpu(NvU32 gpuId, NvBool reset_aware);
void nvkms_close_gpu(NvU32 gpuId, NvBool reset_aware);
+/*!
+ * Mark a GPU as lost (surprise removal, e.g., Thunderbolt eGPU unplug).
+ * This prevents hardware access and cancels pending timers.
+ */
+void nvkms_gpu_lost(NvU32 gpuId);
+
/*!
* Enumerate nvidia gpus.
diff --git a/src/nvidia-modeset/os-interface/include/nvkms.h b/src/nvidia-modeset/os-interface/include/nvkms.h
index d350ef75..668fa8c2 100644
--- a/src/nvidia-modeset/os-interface/include/nvkms.h
+++ b/src/nvidia-modeset/os-interface/include/nvkms.h
@@ -88,6 +88,8 @@ void nvKmsModuleUnload(void);
void nvKmsSuspend(NvU32 gpuId);
void nvKmsResume(NvU32 gpuId);
+void nvKmsGpuLost(NvU32 gpuId);
+
void nvKmsGetProcFiles(const nvkms_procfs_file_t **ppProcFiles);
NvBool nvKmsReadConf(const char *buff, size_t size,
diff --git a/src/nvidia-modeset/src/nvkms-console-restore.c b/src/nvidia-modeset/src/nvkms-console-restore.c
index 0c6cc5b2..2cfb5958 100644
--- a/src/nvidia-modeset/src/nvkms-console-restore.c
+++ b/src/nvidia-modeset/src/nvkms-console-restore.c
@@ -765,6 +765,11 @@ NvBool nvEvoRestoreConsole(NVDevEvoPtr pDevEvo, const NvBool allowMST)
pDevEvo->fbConsoleSurfaceHandle);
struct NvKmsSetModeParams *params;
+ /* Skip if GPU has been lost (e.g., Thunderbolt unplug) */
+ if (pDevEvo->gpuLost) {
+ goto done;
+ }
+
/*
* If this function fails to restore a console then NVKMS frees
* and reallocates the core channel, to attempt the console
diff --git a/src/nvidia-modeset/src/nvkms-dma.c b/src/nvidia-modeset/src/nvkms-dma.c
index 0da2c2e2..80c08126 100644
--- a/src/nvidia-modeset/src/nvkms-dma.c
+++ b/src/nvidia-modeset/src/nvkms-dma.c
@@ -37,7 +37,18 @@ static void EvoCoreKickoff(NVDmaBufferEvoPtr push_buffer, NvU32 putOffset);
void nvDmaKickoffEvo(NVEvoChannelPtr pChannel)
{
NVDmaBufferEvoPtr p = &pChannel->pb;
- NvU32 putOffset = (NvU32)((char *)p->buffer - (char *)p->base);
+ NvU32 putOffset;
+
+ /*
+ * Skip DMA kickoff if the GPU has been lost (e.g., Thunderbolt eGPU
+ * surprise removal). Attempting to access DMA control registers when
+ * the GPU is gone will crash the kernel.
+ */
+ if (p->pDevEvo == NULL || p->pDevEvo->gpuLost) {
+ return;
+ }
+
+ putOffset = (NvU32)((char *)p->buffer - (char *)p->base);
if (p->put_offset == putOffset) {
return;
@@ -48,9 +59,20 @@ void nvDmaKickoffEvo(NVEvoChannelPtr pChannel)
static void EvoCoreKickoff(NVDmaBufferEvoPtr push_buffer, NvU32 putOffset)
{
+ NVDevEvoPtr pDevEvo = push_buffer->pDevEvo;
+ int i;
+
nvAssert(putOffset % 4 == 0);
nvAssert(putOffset <= push_buffer->offset_max);
+ /*
+ * Defense-in-depth: check gpuLost again. The caller should have already
+ * checked this, but verify to avoid writing to invalid mapped memory.
+ */
+ if (pDevEvo == NULL || pDevEvo->gpuLost) {
+ return;
+ }
+
#if NVCPU_IS_X86_64
__asm__ __volatile__ ("sfence\n\t" : : : "memory");
#elif NVCPU_IS_FAMILY_ARM
@@ -73,8 +95,23 @@ NvBool nvEvoPollForEmptyChannel(NVEvoChannelPtr pChannel, NvU32 sd,
{
NVDmaBufferEvoPtr push_buffer = &pChannel->pb;
+ /* Return early if GPU is lost to avoid accessing invalid registers. */
+ if (push_buffer->pDevEvo == NULL || push_buffer->pDevEvo->gpuLost) {
+ return FALSE;
+ }
+
do {
- if (EvoCoreReadGet(push_buffer) == push_buffer->put_offset) {
+ NvU32 getOffset = EvoCoreReadGet(push_buffer);
+
+ /*
+ * Check for GPU removal: reading 0xFFFFFFFF typically indicates
+ * the device has been removed from the bus.
+ */
+ if (getOffset == 0xFFFFFFFF) {
+ return FALSE;
+ }
+
+ if (getOffset == push_buffer->put_offset) {
break;
}
@@ -95,6 +132,21 @@ void nvEvoMakeRoom(NVEvoChannelPtr pChannel, NvU32 count)
NvU32 putOffset;
NvU64 startTime = 0;
const NvU64 timeout = 5000000; /* 5 seconds */
+ /*
+ * Maximum number of consecutive timeouts before we give up.
+ * This prevents infinite hangs when the GPU is removed (e.g., Thunderbolt
+ * unplug). After 5 timeouts (25 seconds), we assume the GPU is gone.
+ */
+ const NvU32 maxTimeoutCount = 5;
+ NvU32 timeoutCount = 0;
+
+ /*
+ * Skip if the GPU has been lost. No point trying to make room in a
+ * push buffer for a GPU that's no longer there.
+ */
+ if (push_buffer->pDevEvo == NULL || push_buffer->pDevEvo->gpuLost) {
+ return;
+ }
putOffset = (NvU32) ((char *)push_buffer->buffer -
(char *)push_buffer->base);
@@ -109,6 +161,16 @@ void nvEvoMakeRoom(NVEvoChannelPtr pChannel, NvU32 count)
while (1) {
getOffset = EvoCoreReadGet(push_buffer);
+ /*
+ * Check for GPU removal: reading 0xFFFFFFFF from PCI config space
+ * typically indicates the device has been removed from the bus.
+ */
+ if (getOffset == 0xFFFFFFFF) {
+ nvEvoLogDev(push_buffer->pDevEvo, EVO_LOG_ERROR,
+ "GPU appears to have been removed (read 0xFFFFFFFF)");
+ break;
+ }
+
if (putOffset >= getOffset) {
push_buffer->fifo_free_count =
(push_buffer->offset_max - putOffset) >> 2;
@@ -142,16 +204,25 @@ void nvEvoMakeRoom(NVEvoChannelPtr pChannel, NvU32 count)
}
/*
- * If we have been waiting too long, print an error message. There
- * isn't much we can do as currently structured, so just reset
- * startTime.
+ * If we have been waiting too long, print an error message.
+ * After too many consecutive timeouts, give up to prevent
+ * infinite hangs during GPU surprise removal.
*/
if (nvExceedsTimeoutUSec(push_buffer->pDevEvo, &startTime, timeout)) {
+ timeoutCount++;
nvEvoLogDev(push_buffer->pDevEvo, EVO_LOG_ERROR,
"Error while waiting for GPU progress: "
- "0x%08x:%d %d:%d:%d:%d",
+ "0x%08x:%d %d:%d:%d:%d (timeout %d/%d)",
pChannel->hwclass, pChannel->instance,
- count, push_buffer->fifo_free_count, getOffset, putOffset);
+ count, push_buffer->fifo_free_count, getOffset, putOffset,
+ timeoutCount, maxTimeoutCount);
+
+ if (timeoutCount >= maxTimeoutCount) {
+ nvEvoLogDev(push_buffer->pDevEvo, EVO_LOG_ERROR,
+ "GPU not responding after %d timeouts, assuming removed",
+ timeoutCount);
+ break;
+ }
startTime = 0;
}
@@ -179,6 +250,11 @@ void nvWriteEvoCoreNotifier(
NvU32 value)
{
NVDevEvoPtr pDevEvo = pDispEvo->pDevEvo;
+ /* Skip if GPU is lost to avoid writing to invalid memory. */
+ if (pDevEvo->gpuLost || pDevEvo->core == NULL) {
+ return;
+ }
+
volatile NvU32 *pNotifier = pDevEvo->core->notifierDma.cpuAddress;
EvoWriteNotifier(pNotifier + offset, value);
@@ -198,11 +274,34 @@ static NvBool EvoCheckNotifier(const NVDispEvoRec *pDispEvo,
nvAssert(pNotifier != NULL);
pNotifier += offset;
+ /*
+ * Maximum number of timeout cycles before giving up.
+ * Prevents infinite hangs during GPU surprise removal.
+ */
+ const NvU32 maxTimeoutCount = 5;
+ NvU32 timeoutCount = 0;
+
+ /* Return early if GPU is lost to avoid accessing invalid memory. */
+ if (pDevEvo->gpuLost || pDevEvo->core == NULL) {
+ return FALSE;
+ }
+
// While the completion notifier is not set to done_true
do {
const NvU32 val = *pNotifier;
const NvU32 done_mask = DRF_SHIFTMASK(done_extent_bit:done_base_bit);
const NvU32 done_val = done_value << done_base_bit;
+ NvU32 getOffset;
+
+ /*
+ * Check for GPU removal: reading 0xFFFFFFFF typically indicates
+ * the device has been removed from the bus.
+ */
+ if (val == 0xFFFFFFFF) {
+ nvEvoLogDisp(pDispEvo, EVO_LOG_WARN,
+ "GPU appears removed (notifier read 0xFFFFFFFF)");
+ return FALSE;
+ }
if ((val & done_mask) == done_val) {
return TRUE;
@@ -215,14 +314,39 @@ static NvBool EvoCheckNotifier(const NVDispEvoRec *pDispEvo,
if (nvExceedsTimeoutUSec(
pDevEvo,
&startTime,
- NV_EVO_NOTIFIER_SHORT_TIMEOUT_USEC) &&
- (p->put_offset == EvoCoreReadGet(p)))
+ NV_EVO_NOTIFIER_SHORT_TIMEOUT_USEC))
{
- nvEvoLogDisp(pDispEvo, EVO_LOG_WARN,
- "Lost display notification (%d:0x%08x); "
- "continuing.", sd, val);
- EvoWriteNotifier(pNotifier, done_value << done_base_bit);
- return TRUE;
+ getOffset = EvoCoreReadGet(p);
+
+ /*
+ * Check for GPU removal in get offset as well.
+ */
+ if (getOffset == 0xFFFFFFFF) {
+ nvEvoLogDisp(pDispEvo, EVO_LOG_WARN,
+ "GPU appears removed (GET read 0xFFFFFFFF)");
+ return FALSE;
+ }
+
+ if (p->put_offset == getOffset)
+ {
+ nvEvoLogDisp(pDispEvo, EVO_LOG_WARN,
+ "Lost display notification (%d:0x%08x); "
+ "continuing.", sd, val);
+ EvoWriteNotifier(pNotifier, done_value << done_base_bit);
+ return TRUE;
+ }
+
+ /*
+ * Count timeouts. After too many, assume GPU is gone.
+ */
+ timeoutCount++;
+ if (timeoutCount >= maxTimeoutCount) {
+ nvEvoLogDisp(pDispEvo, EVO_LOG_ERROR,
+ "GPU not responding after %d timeouts (%d:0x%08x)",
+ timeoutCount, sd, val);
+ return FALSE;
+ }
+ startTime = 0;
}
nvkms_yield();
diff --git a/src/nvidia-modeset/src/nvkms-event.c b/src/nvidia-modeset/src/nvkms-event.c
index f7684e0e..8b536869 100644
--- a/src/nvidia-modeset/src/nvkms-event.c
+++ b/src/nvidia-modeset/src/nvkms-event.c
@@ -63,6 +63,11 @@ nvHandleHotplugEventDeferredWork(void *dataPtr, NvU32 dataU32)
NVDpyEvoPtr pDpyEvo;
NVDevEvoPtr pDevEvo = pDispEvo->pDevEvo;
+ /* Skip hardware access if GPU has been lost (e.g., Thunderbolt unplug) */
+ if (pDevEvo->gpuLost) {
+ return;
+ }
+
if (!pDevEvo->displaylessHw) {
// Get the hotplug state.
if ((ret = nvRmApiControl(
diff --git a/src/nvidia-modeset/src/nvkms-evo.c b/src/nvidia-modeset/src/nvkms-evo.c
index 82fa6fe7..796e12d9 100644
--- a/src/nvidia-modeset/src/nvkms-evo.c
+++ b/src/nvidia-modeset/src/nvkms-evo.c
@@ -8787,6 +8787,28 @@ NvBool nvFreeDevEvo(NVDevEvoPtr pDevEvo)
return FALSE;
}
+ /*
+ * If the GPU was lost (surprise removal), skip all hardware-related
+ * cleanup. Just free software resources and remove from device list.
+ *
+ * NOTE: We do NOT call nvFreePerOpenDev() here because the pNvKmsOpenDev
+ * is still in the global open list. It will be properly cleaned up during
+ * module unload when nvKmsClose iterates through all open handles.
+ * Calling nvFreePerOpenDev here would cause a double-free crash.
+ */
+ if (pDevEvo->gpuLost) {
+ nvEvoLogDev(pDevEvo, EVO_LOG_INFO,
+ "Freeing device after GPU lost, skipping hardware cleanup");
+
+ /*
+ * Invalidate all pOpenDev references to this device before freeing it.
+ * This ensures nvKmsClose won't try to access the freed pDevEvo.
+ */
+ nvInvalidateDeviceReferences(pDevEvo);
+
+ goto free_software_resources;
+ }
+
if (pDevEvo->pDifrState) {
nvRmUnregisterDIFREventHandler(pDevEvo);
nvDIFRFree(pDevEvo->pDifrState);
@@ -8826,22 +8848,44 @@ NvBool nvFreeDevEvo(NVDevEvoPtr pDevEvo)
nvRmDestroyDisplays(pDevEvo);
- nvkms_free_timer(pDevEvo->consoleRestoreTimer);
- pDevEvo->consoleRestoreTimer = NULL;
+free_software_resources:
+ {
+ nvkms_free_timer(pDevEvo->consoleRestoreTimer);
+ pDevEvo->consoleRestoreTimer = NULL;
- nvkms_free_timer(pDevEvo->gpuCpuTimeDiffRefreshTimer);
- pDevEvo->gpuCpuTimeDiffRefreshTimer = NULL;
+ nvkms_free_timer(pDevEvo->gpuCpuTimeDiffRefreshTimer);
+ pDevEvo->gpuCpuTimeDiffRefreshTimer = NULL;
+
+ nvPreallocFree(pDevEvo);
- nvPreallocFree(pDevEvo);
+ /*
+ * Skip RM device cleanup if GPU is lost - handles are already invalid
+ * and RM API calls will fail.
+ */
+ if (!pDevEvo->gpuLost) {
+ nvRmFreeDeviceEvo(pDevEvo);
+ }
- nvRmFreeDeviceEvo(pDevEvo);
+ nvListDel(&pDevEvo->devListEntry);
- nvListDel(&pDevEvo->devListEntry);
+ nvkms_free_ref_ptr(pDevEvo->ref_ptr);
- nvkms_free_ref_ptr(pDevEvo->ref_ptr);
+ nvFree(pDevEvo);
- nvFree(pDevEvo);
- return TRUE;
+ /*
+ * NOTE: We intentionally do NOT call nvKmsReinitializeGlobalClient()
+ * here even if the device list is empty. The global client handle
+ * is still referenced by open handles (pNvKmsOpenDev) that will be
+ * cleaned up during module unload by nvKmsClose(). Reinitializing
+ * the client here would corrupt those handles and cause a crash.
+ *
+ * If the user reconnects the GPU before unloading the module, it will
+ * work because AllocDevice checks for stale gpuLost devices and cleans
+ * them up. The global client doesn't need to be reinitialized for that.
+ */
+
+ return TRUE;
+ }
}
static void AssignNumberOfApiHeads(NVDevEvoRec *pDevEvo)
diff --git a/src/nvidia-modeset/src/nvkms-evo3.c b/src/nvidia-modeset/src/nvkms-evo3.c
index 5666f01a..40b8360e 100644
--- a/src/nvidia-modeset/src/nvkms-evo3.c
+++ b/src/nvidia-modeset/src/nvkms-evo3.c
@@ -6444,9 +6444,16 @@ static NvBool GetChannelState(NVDevEvoPtr pDevEvo,
NVC370_CTRL_CMD_GET_CHANNEL_INFO,
&info, sizeof(info));
if (ret != NVOS_STATUS_SUCCESS) {
- nvEvoLogDev(pDevEvo, EVO_LOG_ERROR,
- "Failed to query display engine channel state: 0x%08x:%d:%d:0x%08x",
- pChan->hwclass, pChan->instance, sd, ret);
+ /*
+ * When the GPU is lost (e.g., Thunderbolt/eGPU hot-unplug),
+ * suppress the error log to avoid flooding dmesg. The callers
+ * will handle the failure appropriately.
+ */
+ if (ret != NVOS_STATUS_ERROR_GPU_IS_LOST) {
+ nvEvoLogDev(pDevEvo, EVO_LOG_ERROR,
+ "Failed to query display engine channel state: 0x%08x:%d:%d:0x%08x",
+ pChan->hwclass, pChan->instance, sd, ret);
+ }
return FALSE;
}
@@ -7553,7 +7560,7 @@ void nvEvoSendHdmiInfoFrameC8(const NVDispEvoRec *pDispEvo,
NVHDMIPKT_RESULT ret;
ADVANCED_INFOFRAME advancedInfoFrame = { };
NvBool swChecksum;
-
+
/*
* These structures are weird. The NVT_VIDEO_INFOFRAME,
* NVT_VENDOR_SPECIFIC_INFOFRAME,
@@ -7705,7 +7712,7 @@ static void EvoDisableAdaptiveSyncSdpC6(const NVDispEvoRec *pDispEvo,
{
nvEvo1DisableAdaptiveSyncSdp(pDispEvo, head, NVHDMIPKT_TYPE_SHARED_GENERIC1);
}
-
+
static NvU32 EvoAllocSurfaceDescriptorC3(
NVDevEvoPtr pDevEvo, NVSurfaceDescriptor *pSurfaceDesc,
NvU32 memoryHandle, NvU32 localCtxDmaFlags,
diff --git a/src/nvidia-modeset/src/nvkms-hw-flip.c b/src/nvidia-modeset/src/nvkms-hw-flip.c
index e90a847a..1b5521b1 100644
--- a/src/nvidia-modeset/src/nvkms-hw-flip.c
+++ b/src/nvidia-modeset/src/nvkms-hw-flip.c
@@ -2541,6 +2541,11 @@ static void LowerDispBandwidth(void *dataPtr, NvU32 dataU32)
NvU32 head;
NvBool ret;
+ /* Skip if GPU has been lost (e.g., Thunderbolt unplug) */
+ if (pDevEvo->gpuLost) {
+ return;
+ }
+
guaranteedAndCurrent =
nvCalloc(1, sizeof(*guaranteedAndCurrent) * NVKMS_MAX_HEADS_PER_DISP);
if (guaranteedAndCurrent == NULL) {
@@ -2727,6 +2732,11 @@ TryToDoPostFlipIMP(void *dataPtr, NvU32 dataU32)
pDevEvo->postFlipIMPTimer = NULL;
+ /* Skip if GPU has been lost (e.g., Thunderbolt unplug) */
+ if (pDevEvo->gpuLost) {
+ return;
+ }
+
FOR_ALL_EVO_DISPLAYS(pDispEvo, sd, pDevEvo) {
NVEvoUpdateState updateState = { };
NvBool update = FALSE;
diff --git a/src/nvidia-modeset/src/nvkms-rm.c b/src/nvidia-modeset/src/nvkms-rm.c
index eea1fce5..7967b0d5 100644
--- a/src/nvidia-modeset/src/nvkms-rm.c
+++ b/src/nvidia-modeset/src/nvkms-rm.c
@@ -2426,6 +2426,11 @@ NVDpyIdList nvRmGetConnectedDpys(const NVDispEvoRec *pDispEvo,
NVDevEvoPtr pDevEvo = pDispEvo->pDevEvo;
NvU32 ret;
+ /* Skip hardware access if GPU has been lost (e.g., Thunderbolt unplug) */
+ if (pDevEvo->gpuLost) {
+ return nvEmptyDpyIdList();
+ }
+
if (pDevEvo->displaylessHw) {
return DisplaylessRmGetConnectedDpys(pDispEvo, dpyIdList);
}
@@ -3419,6 +3424,15 @@ NvBool nvRMSyncEvoChannel(
{
NvBool ret = TRUE;
+ /*
+ * Skip channel sync if the GPU has been lost (e.g., Thunderbolt eGPU
+ * surprise removal). The DMA control registers are invalid and would
+ * cause a crash.
+ */
+ if (pDevEvo->gpuLost) {
+ return FALSE;
+ }
+
if (pChannel) {
NvU32 sd;
diff --git a/src/nvidia-modeset/src/nvkms.c b/src/nvidia-modeset/src/nvkms.c
index 133c38ea..67192e3f 100644
--- a/src/nvidia-modeset/src/nvkms.c
+++ b/src/nvidia-modeset/src/nvkms.c
@@ -1395,6 +1395,34 @@ static NvBool AllocDevice(struct NvKmsPerOpen *pOpen,
pDevEvo = nvFindDevEvoByDeviceId(pParams->request.deviceId);
+ /*
+ * If we found an existing device that was marked as lost (e.g., from a
+ * previous Thunderbolt surprise removal), we need to clean it up before
+ * allocating a new device for the reconnected GPU.
+ */
+ if (pDevEvo != NULL && pDevEvo->gpuLost) {
+ nvEvoLogDev(pDevEvo, EVO_LOG_INFO,
+ "Cleaning up stale device from previous surprise removal");
+ /*
+ * Force cleanup of the stale device. Set allocRefCnt to 1 so that
+ * nvFreeDevEvo will actually free it.
+ */
+ pDevEvo->allocRefCnt = 1;
+ nvFreeDevEvo(pDevEvo);
+ pDevEvo = NULL;
+
+ /*
+ * After cleaning up a gpuLost device, reinitialize the global RM
+ * client handle. RM may have invalidated internal state when the
+ * GPU was lost, causing subsequent API calls to fail with
+ * NV_ERR_INVALID_OBJECT_HANDLE.
+ */
+ if (!nvReinitializeGlobalClientAfterGpuLost()) {
+ pParams->reply.status = NVKMS_ALLOC_DEVICE_STATUS_FATAL_ERROR;
+ return FALSE;
+ }
+ }
+
if (pDevEvo == NULL) {
pDevEvo = nvAllocDevEvo(&pParams->request, &pParams->reply.status);
if (pDevEvo == NULL) {
@@ -1603,6 +1631,24 @@ static void DisableRemainingVblankSemControls(
static void FreeDeviceReference(struct NvKmsPerOpen *pOpen,
struct NvKmsPerOpenDev *pOpenDev)
{
+ NVDevEvoPtr pDevEvo = pOpenDev->pDevEvo;
+
+ /*
+ * If pDevEvo is NULL, the device was already freed due to GPU loss
+ * (surprise removal). In this case, skip all hardware-related cleanup
+ * and just free the software structures.
+ *
+ * Also check if the device is marked as gpuLost - this can happen if
+ * nvInvalidateDeviceReferences hasn't been called yet (e.g., during
+ * concurrent cleanup) or if there's a race between GPU loss detection
+ * and this close path.
+ */
+ if (pDevEvo == NULL || pDevEvo->gpuLost) {
+ pOpenDev->pDevEvo = NULL;
+ nvFreePerOpenDev(pOpen, pOpenDev);
+ return;
+ }
+
/* Disable all client-owned vblank sync objects that still exist. */
DisableRemainingVblankSyncObjects(pOpen, pOpenDev);
@@ -5333,6 +5379,31 @@ void nvRevokeDevice(NVDevEvoPtr pDevEvo)
}
}
+/*
+ * Invalidate all pOpenDev references to a device.
+ * Called when GPU is lost to ensure nvKmsClose doesn't access freed pDevEvo.
+ * This sets pOpenDev->pDevEvo to NULL for all open handles.
+ */
+void nvInvalidateDeviceReferences(NVDevEvoPtr pDevEvo)
+{
+ struct NvKmsPerOpen *pOpen;
+ struct NvKmsPerOpenDev *pOpenDev;
+ NvKmsGenericHandle dev;
+
+ if (pDevEvo == NULL) {
+ return;
+ }
+
+ nvListForEachEntry(pOpen, &perOpenIoctlList, perOpenIoctlListEntry) {
+ FOR_ALL_POINTERS_IN_EVO_API_HANDLES(&pOpen->ioctl.devHandles,
+ pOpenDev, dev) {
+ if (pOpenDev->pDevEvo == pDevEvo) {
+ pOpenDev->pDevEvo = NULL;
+ }
+ }
+ }
+}
+
/*!
* Open callback.
*
@@ -6345,6 +6416,40 @@ static void FreeGlobalState(void)
nvClearDpyOverrides();
}
+NvBool nvReinitializeGlobalClientAfterGpuLost(void)
+{
+ NvU32 ret;
+
+ /* Only reinitialize if we have a client handle */
+ if (nvEvoGlobal.clientHandle == 0) {
+ return TRUE;
+ }
+
+ nvEvoLog(EVO_LOG_INFO, "Reinitializing global client after GPU lost");
+
+ /* Free the old client handle */
+ nvRmApiFree(nvEvoGlobal.clientHandle, nvEvoGlobal.clientHandle,
+ nvEvoGlobal.clientHandle);
+ nvEvoGlobal.clientHandle = 0;
+
+ /* Allocate a new client handle */
+ ret = nvRmApiAlloc(NV01_NULL_OBJECT,
+ NV01_NULL_OBJECT,
+ NV01_NULL_OBJECT,
+ NV01_ROOT,
+ &nvEvoGlobal.clientHandle);
+
+ if (ret != NVOS_STATUS_SUCCESS) {
+ nvEvoLog(EVO_LOG_ERROR, "Failed to reinitialize global client");
+ return FALSE;
+ }
+
+ /* Update RM context */
+ nvEvoGlobal.rmSmgContext.clientHandle = nvEvoGlobal.clientHandle;
+
+ return TRUE;
+}
+
/*
* Wrappers to help SMG access NvKmsKAPI's RM context.
*/
@@ -6440,6 +6545,11 @@ static void ConsoleRestoreTimerFired(void *dataPtr, NvU32 dataU32)
{
NVDevEvoPtr pDevEvo = dataPtr;
+ /* Skip if GPU has been lost (e.g., Thunderbolt unplug) */
+ if (pDevEvo->gpuLost) {
+ return;
+ }
+
if (pDevEvo->modesetOwner == NULL && pDevEvo->handleConsoleHotplugs) {
pDevEvo->skipConsoleRestore = FALSE;
nvEvoRestoreConsole(pDevEvo, TRUE /* allowMST */);
@@ -6932,6 +7042,43 @@ void nvKmsResume(NvU32 gpuId)
}
}
+/*!
+ * Mark a GPU as lost (e.g., Thunderbolt/eGPU hot-unplug).
+ *
+ * This prevents any hardware access attempts that would cause kernel crashes.
+ * The device's timers are cancelled and the gpuLost flag is set so that
+ * subsequent operations bail out early.
+ */
+void nvKmsGpuLost(NvU32 gpuId)
+{
+ NVDevEvoPtr pDevEvo;
+ NvU32 i;
+
+ FOR_ALL_EVO_DEVS(pDevEvo) {
+ for (i = 0; i < ARRAY_LEN(pDevEvo->openedGpuIds); i++) {
+ if (pDevEvo->openedGpuIds[i] == gpuId) {
+ nvEvoLogDev(pDevEvo, EVO_LOG_INFO,
+ "GPU lost (surprise removal), disabling hardware access");
+
+ /* Mark device as lost to prevent hardware access */
+ pDevEvo->gpuLost = TRUE;
+
+ /* Cancel timers that might try to access hardware */
+ nvkms_free_timer(pDevEvo->consoleRestoreTimer);
+ pDevEvo->consoleRestoreTimer = NULL;
+
+ nvkms_free_timer(pDevEvo->postFlipIMPTimer);
+ pDevEvo->postFlipIMPTimer = NULL;
+
+ nvkms_free_timer(pDevEvo->lowerDispBandwidthTimer);
+ pDevEvo->lowerDispBandwidthTimer = NULL;
+
+ return;
+ }
+ }
+ }
+}
+
static void ServiceOneDeferredRequestFifo(
NVDevEvoPtr pDevEvo,
NVDeferredRequestFifoRec *pDeferredRequestFifo)
diff --git a/src/nvidia/arch/nvalloc/unix/src/osapi.c b/src/nvidia/arch/nvalloc/unix/src/osapi.c
index 894cd663..76f5f732 100644
--- a/src/nvidia/arch/nvalloc/unix/src/osapi.c
+++ b/src/nvidia/arch/nvalloc/unix/src/osapi.c
@@ -400,6 +400,17 @@ void
RmLogGpuCrash(OBJGPU *pGpu)
{
NvBool bGpuIsLost, bGpuIsConnected;
+ NvBool bIsExternalGpu = pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_EXTERNAL_GPU);
+
+ //
+ // For external GPUs (eGPUs) that have been disconnected, skip the crash
+ // dump entirely. The GPU is simply gone and attempting to save crash data
+ // will just produce noise in the logs.
+ //
+ if (bIsExternalGpu && pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ return;
+ }
//
// Re-evaluate whether or not the GPU is accessible. This could be called
@@ -4513,7 +4524,30 @@ void NV_API_CALL rm_power_source_change_event(
OBJGPU *pGpu = gpumgrGetGpu(0);
if (pGpu != NULL)
{
+ //
+ // Check if the GPU is lost or inaccessible before proceeding.
+ // This can happen during hot-unplug (e.g., Thunderbolt eGPU removal)
+ // where ACPI events may still be delivered after the GPU is gone.
+ //
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST) ||
+ !pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_CONNECTED))
+ {
+ rmapiLockRelease();
+ goto done;
+ }
+
nv = NV_GET_NV_STATE(pGpu);
+
+ //
+ // For external GPUs (Thunderbolt eGPU), check if we're in surprise
+ // removal before proceeding with power state changes.
+ //
+ if (nv->flags & NV_FLAG_IN_SURPRISE_REMOVAL)
+ {
+ rmapiLockRelease();
+ goto done;
+ }
+
if ((rmStatus = os_ref_dynamic_power(nv, NV_DYNAMIC_PM_FINE)) ==
NV_OK)
{
@@ -4533,6 +4567,7 @@ void NV_API_CALL rm_power_source_change_event(
}
}
+done:
if (rmStatus != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
@@ -6065,7 +6100,30 @@ void NV_API_CALL rm_acpi_nvpcf_notify(
OBJGPU *pGpu = gpumgrGetGpu(0);
if (pGpu != NULL)
{
+ //
+ // Check if the GPU is lost or inaccessible before proceeding.
+ // This can happen during hot-unplug (e.g., Thunderbolt eGPU removal)
+ // where ACPI events may still be delivered after the GPU is gone.
+ //
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST) ||
+ !pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_CONNECTED))
+ {
+ rmapiLockRelease();
+ goto done_nvpcf;
+ }
+
nv_state_t *nv = NV_GET_NV_STATE(pGpu);
+
+ //
+ // For external GPUs (Thunderbolt eGPU), check if we're in surprise
+ // removal before proceeding with power state changes.
+ //
+ if (nv->flags & NV_FLAG_IN_SURPRISE_REMOVAL)
+ {
+ rmapiLockRelease();
+ goto done_nvpcf;
+ }
+
if ((rmStatus = os_ref_dynamic_power(nv, NV_DYNAMIC_PM_FINE)) ==
NV_OK)
{
@@ -6077,6 +6135,7 @@ void NV_API_CALL rm_acpi_nvpcf_notify(
rmapiLockRelease();
}
+done_nvpcf:
threadStateFree(&threadState, THREAD_STATE_FLAGS_NONE);
NV_EXIT_RM_RUNTIME(sp,fp);
}
diff --git a/src/nvidia/arch/nvalloc/unix/src/osinit.c b/src/nvidia/arch/nvalloc/unix/src/osinit.c
index b3fd3437..4bfc60fd 100644
--- a/src/nvidia/arch/nvalloc/unix/src/osinit.c
+++ b/src/nvidia/arch/nvalloc/unix/src/osinit.c
@@ -357,12 +357,14 @@ osHandleGpuLost
pmc_boot_0 = NV_PRIV_REG_RD32(nv->regs->map_u, NV_PMC_BOOT_0);
if (pmc_boot_0 != nvp->pmc_boot_0)
{
+ NvBool bIsExternalGpu = pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_EXTERNAL_GPU);
+
//
// This doesn't support PEX Reset and Recovery yet.
// This will help to prevent accessing registers of a GPU
// which has fallen off the bus.
//
- if (bEmitXid)
+ if (bEmitXid && !bIsExternalGpu)
{
nvErrorLog_va((void *)pGpu, ROBUST_CHANNEL_GPU_HAS_FALLEN_OFF_THE_BUS,
"GPU has fallen off the bus.");
@@ -371,7 +373,14 @@ osHandleGpuLost
gpuNotifySubDeviceEvent(pGpu, NV2080_NOTIFIERS_GPU_UNAVAILABLE, NULL,
0, ROBUST_CHANNEL_GPU_HAS_FALLEN_OFF_THE_BUS, 0);
- NV_DEV_PRINTF(NV_DBG_ERRORS, nv, "GPU has fallen off the bus.\n");
+ if (bIsExternalGpu)
+ {
+ NV_DEV_PRINTF(NV_DBG_WARNINGS, nv, "External GPU disconnected.\n");
+ }
+ else
+ {
+ NV_DEV_PRINTF(NV_DBG_ERRORS, nv, "GPU has fallen off the bus.\n");
+ }
if (pGpu->boardInfo != NULL && pGpu->boardInfo->serialNumber[0] != '\0')
{
@@ -2444,13 +2453,28 @@ void RmShutdownAdapter(
if (nvp->flags & NV_INIT_FLAG_GPU_STATE_LOAD)
{
rmStatus = gpuStateUnload(pGpu, GPU_STATE_DEFAULT);
- NV_ASSERT(rmStatus == NV_OK);
+ //
+ // During surprise removal (e.g., Thunderbolt eGPU hot-unplug),
+ // this may fail. Log but don't assert since we're tearing down anyway.
+ //
+ if (rmStatus != NV_OK)
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "gpuStateUnload failed during teardown: 0x%x\n", rmStatus);
+ }
}
if (nvp->flags & NV_INIT_FLAG_GPU_STATE)
{
rmStatus = gpuStateDestroy(pGpu);
- NV_ASSERT(rmStatus == NV_OK);
+ //
+ // During surprise removal, this may fail. Log but don't assert.
+ //
+ if (rmStatus != NV_OK)
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "gpuStateDestroy failed during teardown: 0x%x\n", rmStatus);
+ }
}
if (IS_DCE_CLIENT(pGpu))
@@ -2602,7 +2626,14 @@ void RmDisableAdapter(
if (nvp->flags & NV_INIT_FLAG_GPU_STATE_LOAD)
{
rmStatus = gpuStateUnload(pGpu, GPU_STATE_DEFAULT);
- NV_ASSERT(rmStatus == NV_OK);
+ //
+ // During surprise removal, this may fail. Log but don't assert.
+ //
+ if (rmStatus != NV_OK)
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "gpuStateUnload failed during eGPU teardown: 0x%x\n", rmStatus);
+ }
nvp->flags &= ~NV_INIT_FLAG_GPU_STATE_LOAD;
}
diff --git a/src/nvidia/src/kernel/core/thread_state.c b/src/nvidia/src/kernel/core/thread_state.c
index 07ec7aef..07ed1728 100644
--- a/src/nvidia/src/kernel/core/thread_state.c
+++ b/src/nvidia/src/kernel/core/thread_state.c
@@ -407,7 +407,10 @@ static NV_STATUS _threadNodeCheckTimeout(OBJGPU *pGpu, THREAD_STATE_NODE *pThrea
{
if (!API_GPU_ATTACHED_SANITY_CHECK(pGpu))
{
- NV_PRINTF(LEVEL_ERROR, "API_GPU_ATTACHED_SANITY_CHECK failed!\n");
+ //
+ // Don't log error during surprise removal - this is expected
+ // when GPU is hot-unplugged (e.g., Thunderbolt eGPU).
+ //
return NV_ERR_TIMEOUT;
}
}
diff --git a/src/nvidia/src/kernel/diagnostics/nv_debug_dump.c b/src/nvidia/src/kernel/diagnostics/nv_debug_dump.c
index d76f255a..7d028da9 100644
--- a/src/nvidia/src/kernel/diagnostics/nv_debug_dump.c
+++ b/src/nvidia/src/kernel/diagnostics/nv_debug_dump.c
@@ -220,6 +220,17 @@ nvdDoEngineDump_IMPL
NVD_ENGINE_CALLBACK *pEngineCallback;
NV_STATUS nvStatus = NV_OK;
+ //
+ // Skip engine dumps for expected external GPU surprise removal.
+ // Engine dump attempts will fail with GPU_IS_LOST errors which
+ // are expected and just add noise to the log.
+ //
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_EXTERNAL_GPU) &&
+ pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ return NV_ERR_GPU_IS_LOST;
+ }
+
NV_CHECK_OK_OR_RETURN(LEVEL_ERROR,
prbEncNestedStart(pPrbEnc, NVDEBUG_NVDUMP_GPU_INFO));
@@ -263,6 +274,17 @@ nvdDumpAllEngines_IMPL
NVD_ENGINE_CALLBACK *pEngineCallback;
NV_STATUS nvStatus = NV_OK;
+ //
+ // Skip engine dumps for expected external GPU surprise removal.
+ // Engine dump attempts will fail with GPU_IS_LOST errors which
+ // are expected and just add noise to the log.
+ //
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_EXTERNAL_GPU) &&
+ pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ return NV_ERR_GPU_IS_LOST;
+ }
+
NV_CHECK_OK_OR_RETURN(LEVEL_ERROR,
prbEncNestedStart(pPrbEnc, NVDEBUG_NVDUMP_GPU_INFO));
diff --git a/src/nvidia/src/kernel/gpu/disp/kern_disp.c b/src/nvidia/src/kernel/gpu/disp/kern_disp.c
index e133d989..5f23ce8f 100644
--- a/src/nvidia/src/kernel/gpu/disp/kern_disp.c
+++ b/src/nvidia/src/kernel/gpu/disp/kern_disp.c
@@ -419,7 +419,7 @@ kdispDestroyCommonHandle_IMPL
rmStatus = pRmApi->FreeWithSecInfo(pRmApi, pKernelDisplay->hInternalClient,
pKernelDisplay->hDispCommonHandle,
RMAPI_ALLOC_FLAGS_NONE, &pRmApi->defaultSecInfo);
- NV_ASSERT(rmStatus == NV_OK);
+ NV_ASSERT((rmStatus == NV_OK) || (rmStatus == NV_ERR_GPU_IN_FULLCHIP_RESET) || (rmStatus == NV_ERR_GPU_IS_LOST));
rmapiutilFreeClientAndDeviceHandles(pRmApi, &pKernelDisplay->hInternalClient,
&pKernelDisplay->hInternalDevice,
diff --git a/src/nvidia/src/kernel/gpu/falcon/arch/turing/kernel_falcon_tu102.c b/src/nvidia/src/kernel/gpu/falcon/arch/turing/kernel_falcon_tu102.c
index 8b828fc6..4cf70bef 100644
--- a/src/nvidia/src/kernel/gpu/falcon/arch/turing/kernel_falcon_tu102.c
+++ b/src/nvidia/src/kernel/gpu/falcon/arch/turing/kernel_falcon_tu102.c
@@ -184,8 +184,13 @@ kflcnReset_TU102
NV_ASSERT_OK_OR_RETURN(kflcnPreResetWait_HAL(pGpu, pKernelFlcn));
NV_ASSERT_OK(kflcnResetHw(pGpu, pKernelFlcn));
status = kflcnWaitForResetToFinish_HAL(pGpu, pKernelFlcn);
- NV_ASSERT_OR_RETURN((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET), status);
- if (status == NV_ERR_GPU_IN_FULLCHIP_RESET)
+ //
+ // During surprise removal, this may return NV_ERR_TIMEOUT in addition to
+ // NV_ERR_GPU_IS_LOST. Both are acceptable during teardown.
+ //
+ NV_ASSERT_OR_RETURN((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) ||
+ (status == NV_ERR_GPU_IS_LOST) || (status == NV_ERR_TIMEOUT), status);
+ if (status != NV_OK)
return status;
kflcnSwitchToFalcon_HAL(pGpu, pKernelFlcn);
kflcnRegWrite_HAL(pGpu, pKernelFlcn, NV_PFALCON_FALCON_RM,
diff --git a/src/nvidia/src/kernel/gpu/gpu.c b/src/nvidia/src/kernel/gpu/gpu.c
index 0ec00eb3..b8533ab3 100644
--- a/src/nvidia/src/kernel/gpu/gpu.c
+++ b/src/nvidia/src/kernel/gpu/gpu.c
@@ -5470,6 +5470,17 @@ gpuSetDisconnectedProperties_IMPL
OBJGPU *pGpu
)
{
+ //
+ // Log GPU disconnection once. This is expected during Thunderbolt eGPU
+ // hot-unplug but should be noted for debugging purposes.
+ //
+ if (!pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ NV_PRINTF(LEVEL_NOTICE,
+ "GPU 0x%x marked as disconnected/lost\n",
+ pGpu->gpuInstance);
+ }
+
pGpu->setProperty(pGpu, PDB_PROP_GPU_IS_LOST, NV_TRUE);
pGpu->setProperty(pGpu, PDB_PROP_GPU_IS_CONNECTED, NV_FALSE);
pGpu->setProperty(pGpu, PDB_PROP_GPU_IN_PM_CODEPATH, NV_FALSE);
diff --git a/src/nvidia/src/kernel/gpu/gpu_user_shared_data.c b/src/nvidia/src/kernel/gpu/gpu_user_shared_data.c
index 6032b017..ab8cb76f 100644
--- a/src/nvidia/src/kernel/gpu/gpu_user_shared_data.c
+++ b/src/nvidia/src/kernel/gpu/gpu_user_shared_data.c
@@ -259,12 +259,15 @@ _gpushareddataDestroyGsp
params.bInit = NV_FALSE;
- // Free Memdesc on GSP-side
- NV_CHECK_OK(status, LEVEL_ERROR,
- pRmApi->Control(pRmApi, pGpu->hInternalClient,
- pGpu->hInternalSubdevice,
- NV2080_CTRL_CMD_INTERNAL_INIT_USER_SHARED_DATA,
- ¶ms, sizeof(params)));
+ // Free Memdesc on GSP-side - ignore GPU_IS_LOST during surprise removal
+ status = pRmApi->Control(pRmApi, pGpu->hInternalClient,
+ pGpu->hInternalSubdevice,
+ NV2080_CTRL_CMD_INTERNAL_INIT_USER_SHARED_DATA,
+ ¶ms, sizeof(params));
+ if ((status != NV_OK) && (status != NV_ERR_GPU_IS_LOST))
+ {
+ NV_PRINTF(LEVEL_ERROR, "Failed to free user shared data on GSP: 0x%x\n", status);
+ }
}
NV_STATUS
@@ -530,9 +533,9 @@ _gpushareddataSendDataPollRpc
NV2080_CTRL_CMD_INTERNAL_USER_SHARED_DATA_SET_DATA_POLL,
¶ms, sizeof(params));
NV_CHECK_OR_RETURN(LEVEL_ERROR,
- (status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET),
+ (status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST),
status);
- if (status == NV_ERR_GPU_IN_FULLCHIP_RESET)
+ if ((status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST))
return status;
pGpu->userSharedData.lastPolledDataMask = polledDataMask;
pGpu->userSharedData.pollingIntervalMs = pollingIntervalMs;
diff --git a/src/nvidia/src/kernel/gpu/gr/fecs_event_list.c b/src/nvidia/src/kernel/gpu/gr/fecs_event_list.c
index 0755fa37..06b1aad1 100644
--- a/src/nvidia/src/kernel/gpu/gr/fecs_event_list.c
+++ b/src/nvidia/src/kernel/gpu/gr/fecs_event_list.c
@@ -1620,8 +1620,8 @@ fecsBufferDisableHw
NV2080_CTRL_CMD_INTERNAL_GR_GET_FECS_TRACE_HW_ENABLE,
&getHwEnableParams,
sizeof(getHwEnableParams));
- NV_ASSERT_OR_RETURN_VOID((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
- if (status == NV_ERR_GPU_IN_FULLCHIP_RESET)
+ NV_ASSERT_OR_RETURN_VOID((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
+ if ((status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST))
return;
if (getHwEnableParams.bEnable)
@@ -1636,7 +1636,7 @@ fecsBufferDisableHw
NV2080_CTRL_CMD_INTERNAL_GR_SET_FECS_TRACE_HW_ENABLE,
&setHwEnableParams,
sizeof(setHwEnableParams));
- NV_ASSERT_OR_RETURN_VOID((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT_OR_RETURN_VOID((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
}
}
diff --git a/src/nvidia/src/kernel/gpu/gr/kernel_graphics.c b/src/nvidia/src/kernel/gpu/gr/kernel_graphics.c
index 18accf8c..b4097969 100644
--- a/src/nvidia/src/kernel/gpu/gr/kernel_graphics.c
+++ b/src/nvidia/src/kernel/gpu/gr/kernel_graphics.c
@@ -2605,7 +2605,7 @@ void kgraphicsFreeGlobalCtxBuffers_IMPL
{
NV_STATUS status;
status = kmemsysCacheOp_HAL(pGpu, pKernelMemorySystem, NULL, FB_CACHE_VIDEO_MEMORY, FB_CACHE_EVICT);
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
}
}
diff --git a/src/nvidia/src/kernel/gpu/gsp/arch/ampere/kernel_gsp_falcon_ga102.c b/src/nvidia/src/kernel/gpu/gsp/arch/ampere/kernel_gsp_falcon_ga102.c
index b03e04df..3c418928 100644
--- a/src/nvidia/src/kernel/gpu/gsp/arch/ampere/kernel_gsp_falcon_ga102.c
+++ b/src/nvidia/src/kernel/gpu/gsp/arch/ampere/kernel_gsp_falcon_ga102.c
@@ -197,6 +197,10 @@ kgspExecuteHsFalcon_GA102
NvU32 data = 0;
NvU32 dmaCmd;
+ // Check for surprise removal (e.g., Thunderbolt eGPU hot-unplug)
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ return NV_ERR_GPU_IS_LOST;
+
NV_ASSERT_OR_RETURN(pFlcnUcode != NULL, NV_ERR_INVALID_ARGUMENT);
NV_ASSERT_OR_RETURN(pKernelFlcn != NULL, NV_ERR_INVALID_STATE);
diff --git a/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_booter_tu102.c b/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_booter_tu102.c
index 208d551c..8fca083b 100644
--- a/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_booter_tu102.c
+++ b/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_booter_tu102.c
@@ -141,6 +141,10 @@ kgspExecuteBooterUnloadIfNeeded_TU102
if (API_GPU_IN_RESET_SANITY_CHECK(pGpu))
return NV_ERR_GPU_IN_FULLCHIP_RESET;
+ // Check for surprise removal (e.g., Thunderbolt eGPU hot-unplug)
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ return NV_ERR_GPU_IS_LOST;
+
// skip actually executing Booter Unload if WPR2 is not up
if (!kgspIsWpr2Up_HAL(pGpu, pKernelGsp))
{
@@ -151,7 +155,16 @@ kgspExecuteBooterUnloadIfNeeded_TU102
NV_PRINTF(LEVEL_INFO, "executing Booter Unload\n");
NV_ASSERT_OR_RETURN(pKernelGsp->pBooterUnloadUcode != NULL, NV_ERR_INVALID_STATE);
- NV_ASSERT_OK(kflcnReset_HAL(pGpu, staticCast(pKernelSec2, KernelFalcon)));
+ // Falcon reset may timeout during surprise removal - don't assert
+ status = kflcnReset_HAL(pGpu, staticCast(pKernelSec2, KernelFalcon));
+ if ((status != NV_OK) && (status != NV_ERR_TIMEOUT) && (status != NV_ERR_GPU_IS_LOST))
+ {
+ NV_ASSERT(0);
+ }
+ if (status != NV_OK)
+ {
+ return status;
+ }
// SR code
if (sysmemAddrOfSuspendResumeData != 0)
diff --git a/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_tu102.c b/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_tu102.c
index 0ee595fc..ada1acd6 100644
--- a/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_tu102.c
+++ b/src/nvidia/src/kernel/gpu/gsp/arch/turing/kernel_gsp_tu102.c
@@ -678,7 +678,18 @@ kgspTeardown_TU102
// Reset GSP so we can load FWSEC-SB
status = kflcnReset_HAL(pGpu, staticCast(pKernelGsp, KernelFalcon));
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ //
+ // During surprise removal, this may return NV_ERR_TIMEOUT in addition to
+ // NV_ERR_GPU_IS_LOST. Both are acceptable during teardown.
+ //
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) ||
+ (status == NV_ERR_GPU_IS_LOST) || (status == NV_ERR_TIMEOUT));
+
+ // Skip remaining hardware operations if GPU is lost/timeout - can't talk to it anyway
+ if (status != NV_OK)
+ {
+ goto skip_fwsec;
+ }
// Invoke FWSEC-SB to put back PreOsApps during driver unload
status = kgspPrepareForFwsecSb_HAL(pGpu, pKernelGsp, pKernelGsp->pFwsecUcode, &preparedCmd);
@@ -690,7 +701,7 @@ kgspTeardown_TU102
else
{
status = kgspExecuteFwsec_HAL(pGpu, pKernelGsp, &preparedCmd);
- if ((status != NV_OK) && (status != NV_ERR_GPU_IN_FULLCHIP_RESET))
+ if ((status != NV_OK) && (status != NV_ERR_GPU_IN_FULLCHIP_RESET) && (status != NV_ERR_GPU_IS_LOST))
{
NV_PRINTF(LEVEL_ERROR, "failed to execute FWSEC-SB for PreOsApps during driver unload: 0x%x\n", status);
NV_ASSERT_FAILED("FWSEC-SB failed");
@@ -698,6 +709,8 @@ kgspTeardown_TU102
}
}
+skip_fwsec:
+
// Execute Booter Unload
status = kgspExecuteBooterUnloadIfNeeded_HAL(pGpu, pKernelGsp,
_kgspGetBooterUnloadArgs(pKernelGsp, unloadMode));
diff --git a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c b/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c
index 4da1151c..b4e45e63 100644
--- a/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c
+++ b/src/nvidia/src/kernel/gpu/gsp/kernel_gsp.c
@@ -139,6 +139,8 @@ static NV_STATUS _kgspRpcRecvPoll(OBJGPU *, OBJRPC *, NvU32, NvU32);
static NV_STATUS _kgspRpcDrainEvents(OBJGPU *, KernelGsp *, NvU32, NvU32, KernelGspRpcEventHandlerContext);
static void _kgspRpcIncrementTimeoutCountAndRateLimitPrints(OBJGPU *, OBJRPC *);
+static NvBool _kgspIsExternalGpuSurpriseRemoval(OBJGPU *);
+
static NV_STATUS _kgspAllocSimAccessBuffer(OBJGPU *pGpu, KernelGsp *pKernelGsp);
static void _kgspFreeSimAccessBuffer(OBJGPU *pGpu, KernelGsp *pKernelGsp);
@@ -300,11 +302,13 @@ _kgspRpcSanityCheck(OBJGPU *pGpu, KernelGsp *pKernelGsp, OBJRPC *pRpc)
pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
{
NV_PRINTF(LEVEL_INFO, "GPU lost, skipping RPC\n");
+ pRpc->bQuietPrints = NV_TRUE;
return NV_ERR_GPU_IS_LOST;
}
if (osIsGpuShutdown(pGpu))
{
NV_PRINTF(LEVEL_INFO, "GPU shutdown, skipping RPC\n");
+ pRpc->bQuietPrints = NV_TRUE;
return NV_ERR_GPU_IS_LOST;
}
if (!gpuIsGpuFullPowerForPmResume(pGpu))
@@ -2095,6 +2099,20 @@ kgspLogRpcDebugInfoToProtobuf
prbEncNestedEnd(pProtobufData);
}
+/*!
+ * Check if this is an expected external GPU surprise removal.
+ * Used to suppress noisy debug output during normal eGPU hot-unplug.
+ */
+static NvBool
+_kgspIsExternalGpuSurpriseRemoval
+(
+ OBJGPU *pGpu
+)
+{
+ return pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_EXTERNAL_GPU) &&
+ pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST);
+}
+
void
kgspLogRpcDebugInfo
(
@@ -2110,6 +2128,15 @@ kgspLogRpcDebugInfo
NvU64 activeData[2];
const NvU32 rpcEntriesToLog = (RPC_HISTORY_DEPTH > 8) ? 8 : RPC_HISTORY_DEPTH;
+ //
+ // Suppress detailed RPC debug output for expected external GPU surprise removal.
+ // This keeps the log clean during normal Thunderbolt eGPU hot-unplug.
+ //
+ if (_kgspIsExternalGpuSurpriseRemoval(pGpu))
+ {
+ return;
+ }
+
_kgspGetActiveRpcDebugData(pRpc, pMsgHdr->function,
&activeData[0], &activeData[1]);
NV_ERROR_LOG_DATA(pGpu, errorNum,
@@ -2193,6 +2220,15 @@ _kgspCheckSlowRpc
NV_ASSERT_OR_RETURN_VOID(tsFreqUs > 0);
+ //
+ // Suppress slow RPC warnings for expected external GPU surprise removal.
+ // During normal Thunderbolt eGPU hot-unplug, slow/stalled RPCs are expected.
+ //
+ if (_kgspIsExternalGpuSurpriseRemoval(pGpu))
+ {
+ return;
+ }
+
duration = (pHistoryEntry->ts_end - pHistoryEntry->ts_start) / tsFreqUs;
if (duration > SLOW_RPC_THRESHOLD_US)
@@ -2522,6 +2558,14 @@ _kgspLogRpcTimeout
KernelFalcon *pKernelFlcn = staticCast(pKernelGsp, KernelFalcon);
NvBool bFullReport = bIsFatalTimeout || pRpc->timeoutCount == 1;
+ // Suppress Xid 119 logging for expected external GPU surprise removal.
+ // During normal Thunderbolt eGPU hot-unplug, RPC timeouts are expected.
+ //
+ if (_kgspIsExternalGpuSurpriseRemoval(pGpu))
+ {
+ return;
+ }
+
// Report any GSP-FMC errors if needed
gpuReportGspFmcErrorCode_HAL(pGpu);
diff --git a/src/nvidia/src/kernel/gpu/intr/intr.c b/src/nvidia/src/kernel/gpu/intr/intr.c
index caf9791c..f6e52049 100644
--- a/src/nvidia/src/kernel/gpu/intr/intr.c
+++ b/src/nvidia/src/kernel/gpu/intr/intr.c
@@ -122,6 +122,18 @@ intrServiceStall_IMPL(OBJGPU *pGpu, Intr *pIntr)
if (!RMCFG_FEATURE_PLATFORM_GSP)
{
+ //
+ // Check if GPU is already known to be lost/detached before doing any
+ // register reads. This prevents log spam during surprise removal
+ // (e.g., Thunderbolt eGPU hot-unplug).
+ //
+ if (!API_GPU_ATTACHED_SANITY_CHECK(pGpu) ||
+ API_GPU_IN_RESET_SANITY_CHECK(pGpu) ||
+ pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ goto exit;
+ }
+
//
// If the GPU is off the BUS or surprise removed during servicing DPC for ISRs
// we wont know about GPU state until after we start processing DPCs for every
@@ -137,18 +149,17 @@ intrServiceStall_IMPL(OBJGPU *pGpu, Intr *pIntr)
if (regReadValue == GPU_REG_VALUE_INVALID)
{
- NV_PRINTF(LEVEL_ERROR,
- "Failed GPU reg read : 0x%x. Check whether GPU is present on the bus\n",
- regReadValue);
- }
-
- if (!API_GPU_ATTACHED_SANITY_CHECK(pGpu))
- {
- goto exit;
- }
-
- if (API_GPU_IN_RESET_SANITY_CHECK(pGpu))
- {
+ //
+ // GPU has been surprise removed. Mark it as lost and return early.
+ // Log once when first detected.
+ //
+ if (!pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "GPU 0x%x surprise removed (reg read returned 0xFFFFFFFF)\n",
+ pGpu->gpuInstance);
+ pGpu->setProperty(pGpu, PDB_PROP_GPU_IS_LOST, NV_TRUE);
+ }
goto exit;
}
}
@@ -1548,6 +1559,18 @@ _intrServiceStallCommonCheckBegin
if (!RMCFG_FEATURE_PLATFORM_GSP)
{
+ //
+ // Check if GPU is already known to be lost/detached before doing any
+ // register reads. This prevents log spam during surprise removal
+ // (e.g., Thunderbolt eGPU hot-unplug).
+ //
+ if (!API_GPU_ATTACHED_SANITY_CHECK(pGpu) ||
+ API_GPU_IN_RESET_SANITY_CHECK(pGpu) ||
+ pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ return NV_ERR_GPU_IS_LOST;
+ }
+
//
// If the GPU is off the BUS or surprise removed during servicing DPC for ISRs
// we wont know about GPU state until after we start processing DPCs for every
@@ -1563,14 +1586,17 @@ _intrServiceStallCommonCheckBegin
if (regReadValue == GPU_REG_VALUE_INVALID)
{
- NV_PRINTF(LEVEL_ERROR,
- "Failed GPU reg read : 0x%x. Check whether GPU is present on the bus\n",
- regReadValue);
- }
-
- // Dont service interrupts if GPU is surprise removed
- if (!API_GPU_ATTACHED_SANITY_CHECK(pGpu) || API_GPU_IN_RESET_SANITY_CHECK(pGpu))
- {
+ //
+ // GPU has been surprise removed. Mark it as lost and return early.
+ // Log once when first detected.
+ //
+ if (!pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "GPU 0x%x surprise removed (reg read returned 0xFFFFFFFF)\n",
+ pGpu->gpuInstance);
+ pGpu->setProperty(pGpu, PDB_PROP_GPU_IS_LOST, NV_TRUE);
+ }
return NV_ERR_GPU_IS_LOST;
}
}
@@ -1628,7 +1654,16 @@ intrServiceStallList_IMPL
NvBool bPending;
CALL_CONTEXT *pOldContext = NULL;
- NV_ASSERT_OK_OR_ELSE(status, _intrServiceStallCommonCheckBegin(pGpu, pIntr, &pOldContext), return);
+ //
+ // Don't use NV_ASSERT_OK_OR_ELSE here - NV_ERR_GPU_IS_LOST is expected
+ // during surprise removal (e.g., Thunderbolt eGPU hot-unplug) and
+ // should not spam the logs with assertion messages.
+ //
+ status = _intrServiceStallCommonCheckBegin(pGpu, pIntr, &pOldContext);
+ if (status != NV_OK)
+ {
+ return;
+ }
do
{
@@ -1681,7 +1716,16 @@ intrServiceStallSingle_IMPL
bitVectorClrAll(&engines);
bitVectorSet(&engines, engIdx);
- NV_ASSERT_OK_OR_ELSE(status, _intrServiceStallCommonCheckBegin(pGpu, pIntr, &pOldContext), return);
+ //
+ // Don't use NV_ASSERT_OK_OR_ELSE here - NV_ERR_GPU_IS_LOST is expected
+ // during surprise removal (e.g., Thunderbolt eGPU hot-unplug) and
+ // should not spam the logs with assertion messages.
+ //
+ status = _intrServiceStallCommonCheckBegin(pGpu, pIntr, &pOldContext);
+ if (status != NV_OK)
+ {
+ return;
+ }
do
{
diff --git a/src/nvidia/src/kernel/gpu/mem_mgr/ce_utils.c b/src/nvidia/src/kernel/gpu/mem_mgr/ce_utils.c
index a3902c5b..0e7f4edb 100644
--- a/src/nvidia/src/kernel/gpu/mem_mgr/ce_utils.c
+++ b/src/nvidia/src/kernel/gpu/mem_mgr/ce_utils.c
@@ -344,10 +344,17 @@ ceutilsDestruct_IMPL
// process all callbacks while CeUtils is fully functional
_ceutilsProcessCompletionCallbacks(pCeUtils);
portSyncSpinlockAcquire(pCeUtils->pCallbackLock);
- NV_ASSERT(listCount(&pCeUtils->completionCallbacks) == 0);
+ // During surprise removal, callbacks may not complete cleanly - skip assertion if GPU is lost
+ if (!pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ NV_ASSERT(listCount(&pCeUtils->completionCallbacks) == 0);
+ }
portSyncSpinlockRelease(pCeUtils->pCallbackLock);
// make sure no new work was queued from callbacks
- NV_ASSERT(pCeUtils->lastCompletedPayload == lastSubmittedPayload);
+ if (!pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ NV_ASSERT(pCeUtils->lastCompletedPayload == lastSubmittedPayload);
+ }
if ((pChannel->bClientUserd) && (pChannel->pControlGPFifo != NULL))
{
diff --git a/src/nvidia/src/kernel/gpu/mem_mgr/vaspace_api.c b/src/nvidia/src/kernel/gpu/mem_mgr/vaspace_api.c
index 86b62f1b..32c96523 100644
--- a/src/nvidia/src/kernel/gpu/mem_mgr/vaspace_api.c
+++ b/src/nvidia/src/kernel/gpu/mem_mgr/vaspace_api.c
@@ -570,7 +570,7 @@ skip_destroy:
if ((IS_VIRTUAL(pGpu) || IS_GSP_CLIENT(pGpu)) && !bBar1VA && !bFlaVA)
{
NV_RM_RPC_FREE(pGpu, hClient, hParent, hVASpace, status);
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
}
NV_PRINTF(LEVEL_INFO,
diff --git a/src/nvidia/src/kernel/mem_mgr/mem.c b/src/nvidia/src/kernel/mem_mgr/mem.c
index 9bfe6250..5db10366 100644
--- a/src/nvidia/src/kernel/mem_mgr/mem.c
+++ b/src/nvidia/src/kernel/mem_mgr/mem.c
@@ -175,7 +175,7 @@ memDestruct_IMPL
if (pMemory->bRpcAlloc && (IS_VIRTUAL(pGpu) || IS_FW_CLIENT(pGpu)))
{
NV_RM_RPC_FREE(pGpu, hClient, hParent, hMemory, status);
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
}
}
diff --git a/src/nvidia/src/kernel/rmapi/nv_gpu_ops.c b/src/nvidia/src/kernel/rmapi/nv_gpu_ops.c
index 2bba5b53..269b47a2 100644
--- a/src/nvidia/src/kernel/rmapi/nv_gpu_ops.c
+++ b/src/nvidia/src/kernel/rmapi/nv_gpu_ops.c
@@ -898,10 +898,19 @@ NV_STATUS nvGpuOpsDestroySession(struct gpuSession *session)
if (!session)
return NV_OK;
- // Sanity Check: There should not be any attached devices with the session!
- NV_ASSERT(!session->devices);
- // Sanity Check: If there are no devices, there should also be no p2p Info!
- NV_ASSERT(!session->p2pInfo);
+ // During surprise removal (GPU lost), devices may not have been properly
+ // detached. In normal operation, these assertions catch programming errors.
+ // When the GPU is lost, we log and continue to avoid blocking cleanup.
+ if (session->devices)
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "Destroying session with devices still attached (GPU may be lost)\n");
+ }
+ if (session->p2pInfo)
+ {
+ NV_PRINTF(LEVEL_WARNING,
+ "Destroying session with p2p info still present (GPU may be lost)\n");
+ }
// freeing session will free everything under it
pRmApi->Free(pRmApi, session->handle, session->handle);
diff --git a/src/nvidia/src/kernel/vgpu/rpc.c b/src/nvidia/src/kernel/vgpu/rpc.c
index fdf05db0..c11c4b7d 100644
--- a/src/nvidia/src/kernel/vgpu/rpc.c
+++ b/src/nvidia/src/kernel/vgpu/rpc.c
@@ -1887,6 +1887,16 @@ static NV_STATUS _issueRpcAndWait(OBJGPU *pGpu, OBJRPC *pRpc)
NvU32 expectedFunc = pVgpuRpcHeader->function;
NvU32 expectedSequence = 0;
+ //
+ // Suppress RPC error logging for expected external GPU surprise removal.
+ // During normal Thunderbolt eGPU hot-unplug, RPC failures are expected.
+ //
+ if (pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_EXTERNAL_GPU) &&
+ pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_LOST))
+ {
+ pRpc->bQuietPrints = NV_TRUE;
+ }
+
status = rpcSendMessage(pGpu, pRpc, &expectedSequence);
if (status != NV_OK)
{
diff --git a/src/nvidia/src/libraries/resserv/src/rs_client.c b/src/nvidia/src/libraries/resserv/src/rs_client.c
index 0fa885c9..8a475c9b 100644
--- a/src/nvidia/src/libraries/resserv/src/rs_client.c
+++ b/src/nvidia/src/libraries/resserv/src/rs_client.c
@@ -841,7 +841,7 @@ clientFreeResource_IMPL
_refRemoveAllDependencies(pResourceRef);
status = serverFreeResourceRpcUnderLock(pServer, pParams);
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
// NV_PRINTF(LEVEL_INFO, "hClient %x: Freeing hResource: %x\n",
// pClient->hClient, pResourceRef->hResource);
diff --git a/src/nvidia/src/libraries/resserv/src/rs_server.c b/src/nvidia/src/libraries/resserv/src/rs_server.c
index 533b87e0..79bbcab7 100644
--- a/src/nvidia/src/libraries/resserv/src/rs_server.c
+++ b/src/nvidia/src/libraries/resserv/src/rs_server.c
@@ -256,7 +256,7 @@ NV_STATUS serverFreeResourceTreeUnderLock(RsServer *pServer, RS_RES_FREE_PARAMS
goto done;
status = clientFreeResource(pResourceRef->pClient, pServer, pFreeParams);
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
serverResLock_Epilogue(pServer, LOCK_ACCESS_WRITE, pLockInfo, &releaseFlags);
}
@@ -1372,7 +1372,7 @@ serverFreeResourceTree
freeParams.bInvalidateOnly = bInvalidateOnly;
freeParams.pSecInfo = pParams->pSecInfo;
status = serverFreeResourceTreeUnderLock(pServer, &freeParams);
- NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET));
+ NV_ASSERT((status == NV_OK) || (status == NV_ERR_GPU_IN_FULLCHIP_RESET) || (status == NV_ERR_GPU_IS_LOST));
if (pServer->bDebugFreeList)
{
|