<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>GPU 驱动 on 硅基深潜</title><link>https://silicondive.com/tags/gpu-%E9%A9%B1%E5%8A%A8/</link><description>Recent content in GPU 驱动 on 硅基深潜</description><generator>Hugo</generator><language>zh-CN</language><lastBuildDate>Mon, 23 Nov 2026 23:50:21 +0800</lastBuildDate><atom:link href="https://silicondive.com/tags/gpu-%E9%A9%B1%E5%8A%A8/index.xml" rel="self" type="application/rss+xml"/><item><title>CUDA exception 进程隔离：边界、交互和测试</title><link>https://silicondive.com/posts/cuda-exception-process-isolation/</link><pubDate>Mon, 23 Nov 2026 00:00:00 +0800</pubDate><guid>https://silicondive.com/posts/cuda-exception-process-isolation/</guid><description>梳理 CUDA 中 exception 进程隔离的错误层级、context/MPS/MIG 边界、相关 feature 交互和验收标准。</description><content:encoded><![CDATA[<h2 id="先定义隔离目标">先定义隔离目标</h2>
<p>CUDA 没有一项单独名为 “exception process isolation” 的官方 API。它更像一组语义：一个进程触发 GPU exception 后，错误只终止或污染该进程的 CUDA 状态，其他进程继续运行；错误能归因到责任进程；必要时只重建受影响的 context 或分区。</p>
<p>这个目标至少要拆成三层：</p>
<ul>
<li><strong>进程隔离</strong>：进程崩溃、被杀或 CUDA 状态损坏，不影响其他进程。</li>
<li><strong>context 隔离</strong>：每个 context 有独立地址空间、队列和调度状态。</li>
<li><strong>设备隔离</strong>：多租户、MPS、MIG、SR-IOV 或虚拟机之间，连硬件错误都不互相传播。</li>
</ul>
<p>三层不是一回事。context 有 fault isolation，不代表 MPS 客户端之间能隔离 fatal fault；MPS 有独立地址空间，也不代表它有完整的 error isolation。</p>
<h2 id="cuda-错误分几层">CUDA 错误分几层</h2>
<p>CUDA runtime 的错误不是同一种严重程度。设计隔离功能前，先把错误分类。</p>
<table>
	<thead>
			<tr>
					<th>错误类型</th>
					<th>例子</th>
					<th>作用范围</th>
					<th>恢复方式</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>API / 参数错误</td>
					<td><code>cudaErrorInvalidValue</code></td>
					<td>当前调用</td>
					<td>修正参数，context 继续用</td>
			</tr>
			<tr>
					<td>启动配置错误</td>
					<td>资源不足、block 配置非法</td>
					<td>当前 launch</td>
					<td>调整配置后重试</td>
			</tr>
			<tr>
					<td>致命执行错误</td>
					<td><code>cudaErrorIllegalAddress</code>、<code>cudaErrorAssert</code>、<code>cudaErrorLaunchFailure</code></td>
					<td>当前进程的 CUDA 状态</td>
					<td>官方要求终止并重启进程</td>
			</tr>
			<tr>
					<td>context 失效</td>
					<td><code>cudaErrorContextIsDestroyed</code></td>
					<td>当前线程的 context</td>
					<td>创建新 context 或重启进程</td>
			</tr>
			<tr>
					<td>设备级错误</td>
					<td>Xid、ECC、GPU reset</td>
					<td>整卡或更大范围</td>
					<td>设备复位、隔离节点</td>
			</tr>
	</tbody>
</table>
<p>关键在第三类。NVIDIA 对 <a href="https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html"><code>cudaErrorIllegalAddress</code>、<code>cudaErrorAssert</code>、<code>cudaErrorLaunchFailure</code></a> 的说明都是：设备进入不一致状态，后续 CUDA 工作会继续返回同一错误，要恢复 CUDA 必须终止并重新启动进程。</p>
<p><code>cudaDeviceReset()</code> 只销毁当前设备、当前进程里的分配和状态，不是整卡 reset（<a href="https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html">CUDA runtime 文档</a>）。它解决不了其他进程的 context 污染，更解决不了硬件级 Xid。</p>
<h2 id="contextmpsmig-的边界">context、MPS、MIG 的边界</h2>
<h3 id="cuda-context">CUDA context</h3>
<p>NVIDIA 在 <a href="https://docs.nvidia.com/datacenter/tesla/mig-user-guide/latest/concepts.html">MIG 文档</a> 里把 GPU context 类比为 CPU process：独立地址空间、独立调度、fault isolation。一个 context 的 fatal error 不应直接破坏另一个 context。</p>
<p>这不等于“exception process isolation 已经完成”。一个进程可以有多个 context，一个 context 的 fatal error 仍可能要求整个进程退出；驱动、MPS server 和硬件级错误会跨过 context 边界。</p>
<h3 id="mps">MPS</h3>
<p>MPS 经常被当成多进程隔离方案。它让多个进程共享 GPU，并给每个 client 独立 GPU 地址空间；NVIDIA 的 <a href="https://docs.nvidia.com/deploy/mps/latest/when-to-use-mps.html">MPS 文档</a> 也明确写了“limited form of error containment”。</p>
<p>但它的隔离粒度不是 per-client：</p>
<ul>
<li>一个 client 触发 fatal GPU fault，错误会限制在共享该 GPU 的 client 子集内。</li>
<li>错误会通知该子集内的所有 client，但不指出是哪个 client 触发的。</li>
<li>受影响的 client 需要自行退出；MPS server 进入 <code>FAULT</code>，拒绝新 client，直到相关 client 全部退出后重建 context。</li>
<li>其他 GPU 上的 client 不受影响。</li>
<li>client 未同步就异常退出，可能让 MPS server 和其他 client 进入未定义状态，出现挂起、异常失败或数据损坏。</li>
</ul>
<p>MIG 文档的对比表把 MPS 的 Error Isolation 标为 <code>No</code>。这里的 <code>No</code> 指没有 MIG 那种按实例的 error isolation；MPS 仍有有限的 fault containment。对“exception 进程隔离”来说，MPS 是需要纳入设计的共享拓扑，不能作为隔离保证。</p>
<p>Reddit r/CUDA 的一条 <a href="https://www.reddit.com/r/CUDA/comments/1v0cqot/how_we_do_fractional_gpu_slicing_without_nvidia/">GPU 分片讨论</a> 也提到，MPS 下一个进程的 invalid memory access 会影响整个 MPS server 和其 client context。这和官方文档的 limited containment 是一致的：地址空间独立，不等于错误隔离。</p>
<h3 id="mig">MIG</h3>
<p>MIG 把 GPU 划成 GPU Instance 和 Compute Instance，计算、显存和带宽物理分区。<a href="https://docs.nvidia.com/datacenter/tesla/mig-user-guide/latest/concepts.html">MIG 文档</a> 给出的 Error Isolation 是 <code>Yes</code>，memory bandwidth QoS 也是 <code>Yes</code>。</p>
<p>代价是粒度粗、配置受 profile 限制、跨分区 IPC 受限。MIG 适合强隔离租户，不适合把一个进程的异常隔离成轻量 context 级操作。</p>
<h2 id="与现有-feature-的交互">与现有 feature 的交互</h2>
<p>这个 feature 涉及整条 CUDA 栈：</p>
<table>
	<thead>
			<tr>
					<th>feature</th>
					<th>交互点</th>
					<th>需要明确的行为</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>CUDA runtime / driver</td>
					<td>primary context、显式 context、错误码</td>
					<td>故障进程收到的错误码保持不变；其他进程不被污染</td>
			</tr>
			<tr>
					<td>streams / events</td>
					<td>fatal error 会污染整个 context</td>
					<td>同 context 的流全部失效；跨 context 不传播</td>
			</tr>
			<tr>
					<td>async / copy engine</td>
					<td>在途 memcpy、DMA、GPUDirect</td>
					<td>故障时取消或 drain，避免悬挂 fence</td>
			</tr>
			<tr>
					<td>CUDA Graphs</td>
					<td>graph exec、memory pool、capture</td>
					<td>launch 失败如何归因；graph 资源是否回收</td>
			</tr>
			<tr>
					<td>Unified Memory</td>
					<td>page fault、迁移、OOM</td>
					<td>fault storm、迁移失败和进程退出如何隔离</td>
			</tr>
			<tr>
					<td>CUDA IPC / P2P</td>
					<td>共享内存 handle、event、peer access</td>
					<td>一端崩溃时 handle 生命周期和错误传播</td>
			</tr>
			<tr>
					<td>NCCL / 多机</td>
					<td>通信域、rank 失败</td>
					<td>单进程隔离不等于作业隔离；集合通信可能整体失败</td>
			</tr>
			<tr>
					<td>MPS</td>
					<td>server/client、共享 GPU 子集</td>
					<td>fatal fault 的报告范围、server <code>FAULT</code>、重启策略</td>
			</tr>
			<tr>
					<td>MIG</td>
					<td>GI/CI、profile、有限 IPC</td>
					<td>分区内故障、跨分区错误、设备级 reset</td>
			</tr>
			<tr>
					<td>vGPU / SR-IOV</td>
					<td>VM 边界、VF reset</td>
					<td>VM 内隔离与整卡错误边界</td>
			</tr>
			<tr>
					<td>容器 / K8s</td>
					<td>PID namespace、device plugin、cgroup</td>
					<td>进程身份、设备清理、重启与调度</td>
			</tr>
			<tr>
					<td>ECC / Xid / reset</td>
					<td>硬件错误、驱动日志；Xid 是驱动写到内核日志的错误报告（<a href="https://docs.nvidia.com/deploy/xid-errors/latest/introduction.html">文档</a>）</td>
					<td>无法隔离的设备级错误如何升级处理</td>
			</tr>
			<tr>
					<td>调试 / profiling</td>
					<td>compute-sanitizer、cuda-gdb、Nsight、CUPTI</td>
					<td>故障进程可诊断，其他进程不被误杀</td>
			</tr>
	</tbody>
</table>
<p>几个容易漏的点：</p>
<p><strong>Unified Memory。</strong> 页错误发生在 GPU 访问未驻留页时，驱动需要处理迁移、映射和 OOM。故障进程的 page fault 不能拖住其他 context；同时要防止 fault storm 把驱动队列打满。</p>
<p><strong>CUDA IPC 和 P2P。</strong> 地址空间隔离不等于共享对象隔离。导出的内存 handle、event 和 peer mapping 都有生命周期，一端异常退出后，另一端可能拿到失效 handle。</p>
<p><strong>CUDA Graphs。</strong> graph 把多个 kernel 和拷贝封装成一次 launch。异常发生时，要能判断是 graph 定义、实例化、参数更新还是执行阶段失败，并回收 graph 私有内存池。</p>
<p><strong>MPS 的多用户模式。</strong> MPS 可以配置成不同 UID 共享一个 server，这会放弃用户间隔离。fatal fault 可能影响其他用户的 client，不能把这种部署当成安全隔离。</p>
<h2 id="feature-设计要先定语义">feature 设计要先定语义</h2>
<h3 id="故障域">故障域</h3>
<p>先定义 fault domain：进程、context、MPS client group，还是 MIG instance。不同域的恢复动作不同。</p>
<ul>
<li>进程级：终止故障进程，清理它的 context、VA、队列、事件和 IPC 资源。</li>
<li>context 级：销毁故障 context，保留进程和其他 context。</li>
<li>客户端组级：MPS 下按受影响 GPU 子集处理。</li>
<li>设备级：Xid、ECC、硬件 reset，只能升级为设备复位或节点隔离。</li>
</ul>
<h3 id="错误归因">错误归因</h3>
<p>每个 exception 至少要能关联：</p>
<ul>
<li>PID、UID、namespace</li>
<li>context ID、device ID</li>
<li>channel / queue / engine</li>
<li>触发时的 CUDA API 和 kernel</li>
<li>Xid、ECC、reset 事件</li>
</ul>
<p>MPS 目前对非内存类 fatal fault 不一定能指出具体 client。新 feature 如果承诺进程级归因，需要补上这条链路。</p>
<h3 id="恢复动作">恢复动作</h3>
<p>故障进程的 CUDA 状态通常不可恢复。隔离功能能做的是：</p>
<ol>
<li>停止该 fault domain 的新提交。</li>
<li>取消或 drain 在途工作。</li>
<li>等待 fence 和 DMA 完成或安全终止。</li>
<li>释放 VA、内存、队列、event、graph 和 IPC handle。</li>
<li>销毁 context，必要时重建 MPS/MIG 资源。</li>
<li>把错误返回给责任进程，其他域继续运行。</li>
</ol>
<p>设备级 fatal error 不能走这条路径。必须明确升级条件，避免把“进程隔离”误宣传成“硬件故障隔离”。</p>
<h3 id="cuda-api-语义">CUDA API 语义</h3>
<p>隔离功能不能改变 CUDA API 的既有语义：</p>
<ul>
<li>致命错误在故障进程内仍应表现为对应的 CUDA 错误。</li>
<li><code>cudaGetLastError</code> / <code>cudaPeekAtLastError</code> 的可见性不应跨进程串扰。</li>
<li><code>cudaDeviceReset</code> 仍只作用于当前进程的当前设备。</li>
<li>MPS/MIG 模式下的错误码、server 状态和 IPC 行为要逐项写清。</li>
<li>fork、多线程、多个 context、driver/runtime 混用都要覆盖。</li>
</ul>
<h2 id="开发难点">开发难点</h2>
<p><strong>错误归因。</strong> GPU 错误是异步上报的。CUDA API 可能在后一次调用才返回前一个 kernel 的错误；MPS 对非内存类 fatal fault 不指出具体 client；Xid 是驱动日志，不天然带 PID 和 context。要把异常限制到进程，driver 必须维护 process、context、channel、queue 和 fault event 的对应关系。</p>
<p><strong>故障域恢复。</strong> CUDA fatal execution error 不是“清一个错误码”就能恢复。<code>cudaErrorIllegalAddress</code>、<code>cudaErrorAssert</code>、<code>cudaErrorLaunchFailure</code> 都要求进程终止。隔离功能要决定是杀进程、销毁 context，还是重建 MPS client group；每种动作的资源清理边界不同。</p>
<p><strong>在途工作。</strong> 异常发生时可能还有 kernel、async copy、DMA、fence、event 和 graph 在执行。直接杀进程会留下悬挂队列、未完成 fence 和 IPC handle。driver 需要先停止新提交，再取消或 drain 旧工作，最后回收 VA、内存、队列和事件。</p>
<p><strong>MPS 的共享状态。</strong> MPS client 有独立地址空间，但共享 MPS server 和 GPU 子集。一个 client 的 fatal fault 会让 server 进入 <code>FAULT</code>，影响共享同一 GPU 的多个 client；非内存 fault 还不一定指出责任方。想在这个拓扑上做 per-process 隔离，必须改 MPS 的故障处理和归因链路。</p>
<p><strong>MIG 的粒度。</strong> MIG 有实例级 error isolation，但配置受 profile 限制，跨分区 IPC 受限，也不能把一个进程的异常轻量地隔离成子分区。隔离功能要识别 MIG 拓扑，避免把设备级故障误判成进程级故障。</p>
<p><strong>跨 feature 状态。</strong> Unified Memory 的 page fault、CUDA Graphs 的私有内存池、IPC/P2P 的共享 handle、NCCL 的通信域，都有独立生命周期。异常清理要覆盖这些对象，否则进程退出后仍会留下脏状态。</p>
<p><strong>兼容性和性能。</strong> 每个 CUDA 版本、驱动版本、MPS/MIG 配置和容器环境都要保持错误码和 API 行为一致。隔离路径还会增加跟踪、fence 和清理开销，不能把正常路径拖慢。</p>
<p><strong>安全边界。</strong> 恶意进程可以主动触发 fault。隔离功能要保证它不能借异常影响其他租户，也不能通过错误信息、handle 或 telemetry 泄露其他进程的数据。</p>
<h2 id="涉及哪些软件栈模块">涉及哪些软件栈模块</h2>
<p>这个 feature 不能只做在 CUDA runtime。最小闭环要跨 user-space driver、kernel driver、firmware 和硬件；MPS/MIG 还要各自处理。</p>
<table>
	<thead>
			<tr>
					<th>模块</th>
					<th>负责什么</th>
					<th>feature 需要改什么</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>CUDA runtime</td>
					<td>API 语义、错误码、context 初始化、<code>cudaDeviceReset</code></td>
					<td>保持错误可见性和进程退出行为；通常不负责真正的隔离</td>
			</tr>
			<tr>
					<td>CUDA driver（user space）</td>
					<td>context、stream、queue、channel、IPC、UVM、CUDA Graph</td>
					<td>fault domain、错误归因、资源清理、MPS 接口</td>
			</tr>
			<tr>
					<td>kernel driver</td>
					<td>中断、MMU fault、channel teardown、reset、Xid</td>
					<td>故障路由、进程退出处理、设备隔离</td>
			</tr>
			<tr>
					<td>firmware / GSP</td>
					<td>调度、engine reset、异常上报</td>
					<td>故障范围、reset 范围、上报格式</td>
			</tr>
			<tr>
					<td>GPU 硬件</td>
					<td>VMID、channel、engine、MMU、MIG 分区</td>
					<td>per-process 故障归因和隔离能力</td>
			</tr>
			<tr>
					<td>MPS</td>
					<td>共享 server、client、GPU 子集</td>
					<td>client 故障组、责任方归因、server <code>FAULT</code> 恢复</td>
			</tr>
			<tr>
					<td>MIG / NVML</td>
					<td>GI/CI 分区、拓扑、遥测</td>
					<td>识别隔离边界、分区 reset、错误上报</td>
			</tr>
			<tr>
					<td>容器 / K8s</td>
					<td>进程生命周期、设备分配、重启</td>
					<td>故障进程清理、设备重新调度、状态回收</td>
			</tr>
	</tbody>
</table>
<p>CUDA runtime 是应用能看到的接口层；真正决定隔离能力的是 driver、kernel driver、firmware 和硬件。硬件不能提供 per-channel 或 per-VMID 的错误归因时，软件只能做到“事后杀进程”，不能保证其他进程完全不受影响。</p>
<p>MPS 又特殊一层：MPS server 是用户态进程，但 fault containment 依赖 driver 和后续 context 重建。MIG 则把隔离边界下移到硬件分区，但配置、IPC 和 reset 仍要 NVML/driver 配合。</p>
<h3 id="kmd-层的处理">KMD 层的处理</h3>
<p>KMD 负责把硬件 fault 变成进程或 context 级事件。以 NVIDIA 为例，就是 <code>nvidia.ko</code>、<code>nvidia-uvm.ko</code> 这些内核模块。</p>
<p><strong>fault 归因。</strong> 硬件上报 fault 时通常带 VMID、channel、engine、fault buffer 信息。KMD 要把 VMID/channel 映射到进程、context 和用户态 driver 实例。映射不准，后面的隔离和清理都会错。</p>
<p><strong>channel 和 VM 拆除。</strong> 责任进程发生 fatal fault 后，KMD 要停止该 channel 的新提交，抢占或 drain 在途工作，等待 fence，释放 GPU VA、页表、队列和 semaphore，再销毁 context。其他 channel 不能因为这次拆除被误伤。</p>
<p><strong>page fault 和 UVM。</strong> MMU fault 分可重放和不可重放。合法缺页交给 UVM 迁移或映射；非法地址、权限错误和 fault storm 要终止责任进程，同时避免阻塞其他进程的 fault 处理队列。</p>
<p><strong>interrupt 和 reset。</strong> fault interrupt 不能直接在中断上下文里做重活；KMD 通常把清理和 reset 放到 workqueue。reset 要区分 channel、engine、GPU、MIG instance 和 VF/FLR。进程级错误不应触发整卡 reset；硬件级 fatal fault 才升级到设备复位。</p>
<p><strong>进程退出。</strong> <code>close(fd)</code>、<code>exit_mmap</code>、<code>kill -9</code> 和 ioctl 并发发生时，KMD 要保证引用计数、对象生命周期和清理顺序正确。进程已经退出，但 interrupt 还在引用它的 channel，是最典型的 use-after-free 场景。</p>
<p><strong>内存和 IPC。</strong> GPU VA、pin 住的页、DMA mapping、P2P mapping、CUDA IPC handle 都要在 fault domain 销毁时回收。一个进程退出不能留下其他进程可访问的残留映射。</p>
<p><strong>MIG / SR-IOV。</strong> KMD 要识别 GI/CI 或 VF 的归属，reset 只作用于责任实例；跨实例 IPC 和错误上报要明确。设备级错误仍可能穿透分区，不能承诺无限隔离。</p>
<p><strong>遥测和 RAS。</strong> Xid、ECC、fault buffer、PID、VMID、device ID 要进日志和 telemetry，但不要泄露其他进程的地址或 handle。</p>
<p>KMD 的核心约束是：进程级 fault 不能升级成全局 reset；设备级 fatal 必须明确升级；所有拆路径都要可重入、可并发、可清理。</p>
<h2 id="测试场景">测试场景</h2>
<table>
	<thead>
			<tr>
					<th>故障</th>
					<th>拓扑</th>
					<th>期望</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>illegal address</td>
					<td>两进程、无 MPS</td>
					<td>故障进程退出；另一进程继续</td>
			</tr>
			<tr>
					<td>assert / launch failure</td>
					<td>同进程多 context</td>
					<td>故障 context 失效；其他 context 可继续（若产品承诺）</td>
			</tr>
			<tr>
					<td>OOM</td>
					<td>多进程共享显存</td>
					<td>责任进程失败；其他进程不被 OOM 连带</td>
			</tr>
			<tr>
					<td>Unified Memory page fault</td>
					<td>UVM + 多进程</td>
					<td>fault 归因正确；无 fault storm</td>
			</tr>
			<tr>
					<td>CUDA Graph 执行失败</td>
					<td>graph + 多流</td>
					<td>graph 资源回收；错误可归因</td>
			</tr>
			<tr>
					<td>MPS fatal fault</td>
					<td>同 GPU 多 client</td>
					<td>受影响 client 收到错误；其他 GPU client 继续</td>
			</tr>
			<tr>
					<td>MPS client 未同步退出</td>
					<td>多 client</td>
					<td>server 不进入未定义状态；可恢复</td>
			</tr>
			<tr>
					<td>MIG fatal fault</td>
					<td>不同 GI/CI</td>
					<td>分区隔离；跨分区 IPC 行为明确</td>
			</tr>
			<tr>
					<td>Xid / ECC</td>
					<td>整卡</td>
					<td>按设备级错误升级；不能假装进程级隔离</td>
			</tr>
			<tr>
					<td>进程被 kill -9</td>
					<td>多进程</td>
					<td>资源回收；无残留 channel/fence</td>
			</tr>
	</tbody>
</table>
<h3 id="怎么触发异常">怎么触发异常</h3>
<p>构造特定 kernel 是最直接的办法，但只覆盖 device execution 这一层。</p>
<table>
	<thead>
			<tr>
					<th>注入方式</th>
					<th>例子</th>
					<th>触发层次</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>kernel trap</td>
					<td><code>assert(0)</code>、<code>__trap()</code>、非法地址访问</td>
					<td>设备执行异常</td>
			</tr>
			<tr>
					<td>launch / API</td>
					<td>资源超限、OOM、无效 handle</td>
					<td>API 或 launch 错误，不一定产生 device exception</td>
			</tr>
			<tr>
					<td>系统 / 硬件</td>
					<td><code>kill -9</code>、hang/timeout、Xid/ECC 注入</td>
					<td>进程或设备级故障</td>
			</tr>
	</tbody>
</table>
<p>测试不要只用一个必崩 kernel。故障点要参数化：idle、kernel 执行中、async copy 中、Graph launch 中、fence 等待中。不同故障点决定是否还有在途工作、是否会留下悬挂队列，也决定故障域清理的难度。</p>
<p>构造 kernel 时优先用 <code>assert</code>、trap 和非法地址，不要用未定义指令或故意破坏硬件。<code>CUDA_LAUNCH_BLOCKING=1</code> 只适合定位，不用于并发测试。<code>compute-sanitizer</code> 用来找非法访存，不要和故意注入的 fault 同时跑。</p>
<h3 id="cuda-runtime--driver-注入">CUDA runtime / driver 注入</h3>
<p>API 层的 exception 大多是错误返回值。它们适合测参数校验、资源生命周期和 context 语义，但不能都当成 fatal device exception。</p>
<table>
	<thead>
			<tr>
					<th>类别</th>
					<th>Runtime 侧</th>
					<th>Driver 侧</th>
					<th>典型结果</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>无效设备 / 参数</td>
					<td><code>cudaSetDevice(-1)</code>、非法枚举值</td>
					<td><code>cuCtxCreate</code> 传非法 flags</td>
					<td><code>cudaErrorInvalidDevice</code> / <code>CUDA_ERROR_INVALID_VALUE</code></td>
			</tr>
			<tr>
					<td>无效 handle</td>
					<td>重复销毁 stream/event、使用已销毁对象</td>
					<td>重复 <code>cuCtxDestroy</code>、非法 module</td>
					<td><code>cudaErrorInvalidResourceHandle</code> / <code>CUDA_ERROR_INVALID_HANDLE</code></td>
			</tr>
			<tr>
					<td>launch 配置</td>
					<td>grid/block 为 0、线程或 shared memory 超限</td>
					<td><code>cuLaunchKernel</code> 参数不合法</td>
					<td><code>cudaErrorInvalidConfiguration</code> / <code>LaunchOutOfResources</code>，context 通常还能用</td>
			</tr>
			<tr>
					<td>显存不足</td>
					<td>申请超大 buffer</td>
					<td><code>cuMemAlloc</code> 超大请求</td>
					<td><code>cudaErrorMemoryAllocation</code> / <code>CUDA_ERROR_OUT_OF_MEMORY</code>，释放后可恢复</td>
			</tr>
			<tr>
					<td>context 生命周期</td>
					<td><code>cudaDeviceReset</code> 后继续调用</td>
					<td><code>cuCtxDestroy</code> 后使用 context</td>
					<td><code>cudaErrorContextIsDestroyed</code> / <code>CUDA_ERROR_CONTEXT_IS_DESTROYED</code></td>
			</tr>
			<tr>
					<td>stream / event</td>
					<td>销毁后继续 wait、非法 event 顺序</td>
					<td><code>cuStreamWaitEvent</code> 非法 handle</td>
					<td>资源错误；是否有在途工作决定清理复杂度</td>
			</tr>
			<tr>
					<td>Graph / capture</td>
					<td>非法节点、capture 中同步、exec update 不匹配</td>
					<td><code>cuGraph*</code> 非法依赖或实例化失败</td>
					<td>capture / graph 错误，通常只影响当前 context</td>
			</tr>
			<tr>
					<td>module / PTX</td>
					<td>N/A</td>
					<td>加载坏 cubin/PTX、找不到函数</td>
					<td><code>CUDA_ERROR_INVALID_IMAGE</code> / <code>INVALID_PTX</code></td>
			</tr>
			<tr>
					<td>IPC / UVM</td>
					<td>非法 IPC handle、跨设备映射</td>
					<td><code>cuIpcOpenMemHandle</code> 非法 handle</td>
					<td>跨进程资源错误，清理不完整会留下脏 handle</td>
			</tr>
			<tr>
					<td>异步 fatal</td>
					<td>kernel 非法地址在后续 sync 才返回</td>
					<td><code>cuCtxSynchronize</code> 收到 launch failure</td>
					<td><code>cudaErrorIllegalAddress</code> / <code>CUDA_ERROR_LAUNCH_FAILED</code>，进程须终止</td>
			</tr>
			<tr>
					<td>MPS</td>
					<td>server <code>FAULT</code> 时新建 client</td>
					<td>连接 MPS server 失败</td>
					<td><code>CUDA_ERROR_MPS_SERVER_NOT_READY</code>，按 MPS client group 处理</td>
			</tr>
	</tbody>
</table>
<p>关键区别是“调用返回错误”与“context 已污染”。无效参数、launch 配置、OOM 通常是可恢复的；非法地址、assert、launch failure 是 fatal，必须在故障进程内终止并重建 context。</p>
<p>测试时把两类分开断言：</p>
<ul>
<li>可恢复错误：同一 context 修复参数后还能继续工作。</li>
<li>fatal 错误：责任进程退出或 context 被销毁；其他进程不受影响。</li>
<li>跨进程错误：IPC handle、MPS server、UVM page fault 要检查资源回收和对端状态。</li>
</ul>
<p><code>cudaGetLastError</code> 和 <code>cudaPeekAtLastError</code> 的清除语义不同，测试要避免一个用例把另一个用例的错误吞掉。</p>
<h3 id="mps-的-blast-radius">MPS 的 blast radius</h3>
<p>MPS 测试不能只断言“故障 client 退出”。要按拓扑定义受影响集合：</p>
<ul>
<li>同 GPU 子集内的 client 是否收到 fatal error。</li>
<li>其他 GPU 上的 client 是否继续运行。</li>
<li>MPS server 是否进入 <code>FAULT</code>，新 client 是否收到 <code>CUDA_ERROR_MPS_SERVER_NOT_READY</code>。</li>
<li>受影响 client 全部退出后，server 是否回到 <code>ACTIVE</code>，新 client 能否正常创建 context。</li>
<li>非内存 fatal fault 是否仍然不指出责任 client；如果产品承诺进程级归因，这里就是缺口。</li>
</ul>
<p>测试 harness 要记录每个 client 的 PID、context、device、错误码和存活状态。故障注入覆盖 idle、kernel 执行中、async copy 中、Graph launch 中，以及 <code>kill -9</code>、assert、illegal address、OOM。并发触发两个 client 的异常，观察 server 状态和恢复路径。</p>
<p>MPS 的校验目标不是一个进程的退出码，而是 blast radius：哪些 client 应该受影响、哪些必须存活、server 如何恢复、新请求何时可以重新进入。这个用例也可以作为进程隔离 feature 的负向基线：如果 feature 声称隔离 MPS client，这个场景必须通过；如果做不到，就要在支持矩阵里明确。</p>
<p>还要做并发和压力测试：两个进程同时触发异常、异常和 reset 竞争、反复创建销毁 context、MPS server 反复进入 <code>FAULT</code>、容器重启、长时间 soak。隔离功能最容易在竞态和清理路径上出问题。</p>
<h2 id="验收指标">验收指标</h2>
<ul>
<li>其他进程存活率：故障后非责任进程应全部继续运行。</li>
<li>错误归因准确率：PID、context、device、Xid 能对应到故障源。</li>
<li>资源回收：VA、内存、队列、event、graph、IPC handle 无泄漏。</li>
<li>恢复时间：故障进程退出到新请求可服务的耗时。</li>
<li>API 语义：错误码和 API 行为与基线一致。</li>
<li>性能开销：正常路径的提交、同步和显存开销可测。</li>
<li>长稳：反复故障注入后没有死锁、悬挂 fence 或 server 状态卡死。</li>
</ul>
<h2 id="常见误区">常见误区</h2>
<p><strong>“MPS 有独立地址空间，所以就是进程隔离。”</strong> 地址空间隔离不等于错误隔离。MPS 的 fatal fault 会通知共享同一 GPU 子集的多个 client，且不指出责任 client。</p>
<p><strong>“context 有 fault isolation，所以进程一定安全。”</strong> fatal execution error 仍要求进程退出；MPS server 和硬件级错误会跨过 context。</p>
<p><strong>“MIG 能隔离所有故障。”</strong> MIG 提供实例级 error isolation，但设备级 Xid、ECC、PCIe 或固件问题仍可能影响整卡。</p>
<p><strong>“<code>cudaDeviceReset</code> 会重置整张 GPU。”</strong> 它只清理当前进程、当前设备的 CUDA 状态；不能修复其他进程或硬件。</p>
<p><code>exception 进程隔离</code> 要解决的是：把错误限制在明确的 fault domain 内，同时保持 CUDA API 语义一致、资源可回收、错误可归因。只做异常捕获，不算进程隔离。</p>
]]></content:encoded></item></channel></rss>