Repository navigation
Improve Thunderbolt eGPU detection and stability - #981
roger-pmta wants to merge 3 commits into
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
|
For about a year now I've been running LLM workloads on multi-eGPU setups that include an USB4 3090 eGPU (ASM2464PDX based ADT UT4G with Gigabyte RTX-3090 GAMING OC 24G) connected to both TB4 and USB4 hosts (Latitude 7430, TongFang X4) running Debian-13 with DC Nvidia drivers. I have never experienced CUDA op freezes nor crashes on unplug (as long as the eGPU being unplugged is properly unbound from the driver beforehand that is). I do experience occasional problems when booting with this USB4 eGPU connected to the X4 laptop: it seems like PCIe tunneling is not properly initialized on some attempts (20-30%, 2 red leds on the UT4G indicating a lack of a connection are active then). Re-plugging afterwards, when the boot sequence is completed, always resolves the issue. I will also add however, that when previously I was using a TH3P4G3 (JHL7540 based) 1080ti eGPU on an XPS13-9360 with the driver v535 from the Debian repos, no unbinding was necessary: it was sufficient if no processes were reported by I've tried this patch today and it has almost no effect on my setup: generally everything works as before, but booting with the USB4 eGPU connected to the X4 has not worked even once since then: I needed to re-plug afterwards every time when using this patch. Unplugging when using this patch still requires unbinding from the driver, otherwise terrible crashes occur (so no change comparing to the vanilla v590). If this patch is a make-or-break for TB5 eGPUs, then I guess it is ok to merge it, but if we could at least try to bring the success rate of booting with USB4 eGPUs somewhere close to the old values, it would be really cool ;-) I will probably be able to help with some testing and debugging. |
|
Thanks for the feedback and testing @morgwai! Let me check where these delays are happening and I'll do more testing with hardware today. Ideally I'd love cold boot to work myself, perhaps the eGPU delay I added is happening too late the initialization process (waiting for PCIe link vs. GSP firmware?) If cold boot was working for you before on TB3 and it isn't now, that's concerning. For folks who find this, @morgwai and I have been chatting in the thread at https://egpu.io/forums/thunderbolt-linux-setup/rtx-5080-via-thunderbolt-5-egpu-hard-lock-on-cuda-operations-nvidia-smi-works-at-idle/ too. |
This comment was marked as outdated.
This comment was marked as outdated.
|
I found that nvidia-persistenced wasn't helping things; I disabled it entirely. It seems to quietly reset the power limit to 360W, overriding my udev rule/script. The script I have to limit power on module load needed to be adjusted: increased timeout, then set the power limit. gpu_burn (https://github.com/wilicc/gpu-burn) is running successfully, the GPU is pulling 325-326W at its new power limit, and at that level is sustaining 11.47-11.48 TFLOP/s (significantly less than ideal, but the point is that it's working and seems relatively stable). Still getting a lot of slow RPC responses from GSP in the dmesg output though. cat <<'EOF' | sudo tee /usr/local/bin/nvidia-egpu-power-init.sh
#!/bin/bash
sleep 5
if nvidia-smi -pl 325; then
logger "nvidia-egpu-power-init: Set power limit to 325W"
else
logger "nvidia-egpu-power-init: Failed to set power limit"
fi
EOFgpu_burn, PyTorch, and Ollama are all working. See latest nvidia-bug-report.log attached. This is the absolute closest I've gotten to getting this working properly, and probably the cleanest log yet. Ran with |
Add periodic osSchedule() calls to spin loops that wait for firmware responses. This prevents soft lockups when response times are elevated, particularly for external GPUs over Thunderbolt where latency is higher. Modified functions: - kfspWaitForResponse: FSP command response polling - kgspIssueNotifyOp_GH100: GSP lockless operation polling - ksec2PollForCanSend_IMPL: SEC2 command queue polling - _kgspRpcRecvPoll: GSP RPC response polling - timeoutCondWait: condition wait polling Each loop yields every 256 iterations to allow other kernel threads to run while waiting.
Extend external GPU support to work reliably with Thunderbolt 4/5 and
USB4, and improve stability for all Thunderbolt connections.
Detection:
- Use kernel's is_thunderbolt flag (Linux 4.10+) for vendor-agnostic
detection instead of Intel-specific device IDs
- Set is_external_gpu early in nv_pci_probe(), before RM initialization
- Add TB4, TB5, USB4 bus type constants for GSP communication
- Accept either HotPlug+ or Surprise+ slot capability, as some TB5
bridges report only the latter
Timeouts:
- Extend GPU timeouts 4x for external GPUs to accommodate tunneling
latency
- Retry with exponential backoff (up to 1 second) in osHandleGpuLost()
before declaring GPU lost, per PCIe Base Spec sec 6.6.1
Cold boot:
- Retry BAR validation with exponential backoff when initial validation
fails on Thunderbolt devices (tunnel may not be ready on cold boot)
Debug:
- Add logging for GSP RPC, PRI errors, AER errors on external GPUs
|
@morgwai I cleaned up this PR and removed the need to set any driver params. The udev rule is for my own odd power situation that I mentioned on the forum. I had success with cold booting my enclosure (came up, nvidia-smi worked, but failed under CUDA workloads). I think that could be power related though. If you have time, I'm curious to see if your setup works with this branch now 🤞 |
| #define NV2080_CTRL_INTERNAL_EGPU_BUS_TYPE_TB3 (0x00000003) | ||
| #define NV2080_CTRL_INTERNAL_EGPU_BUS_TYPE_TB4 (0x00000004) | ||
| #define NV2080_CTRL_INTERNAL_EGPU_BUS_TYPE_TB5 (0x00000005) | ||
| #define NV2080_CTRL_INTERNAL_EGPU_BUS_TYPE_USB4 (0x00000006) |
There was a problem hiding this comment.
At first I thought these values were arbitrary, but the code never assigns them to any vars, only compares them with what it receives from RM_API::Control in osinit.c, meaning the values of these constants come from somewhere else, like some spec or a header file that cannot be included here: could we please add a comment where exactly these values come from?
There was a problem hiding this comment.
You're right, and good catch! This is leftover from when I was hacking around with TB vendor IDs. This file is automatically generated by an NVIDIA-internal tool, therefore I need to undo this.
//
// This file was generated with FINN, an NVIDIA coding tool.
// Source file: ctrl/ctrl2080/ctrl2080internal.finn
//There was a problem hiding this comment.
Should we file a separate bug regarding this? Does the module really treats TB4/TB5/USB4 eGPUs as dGPUs or maybe the vars/constant/comments just refer to all these cases as something-TB3-something due to historical reasons?
There was a problem hiding this comment.
That, I'm not sure. I don't have any insight into how that file is being generated... perhaps something proprietary to NVIDIA, or the GSP firmware? 🤔
Cannot add arbitrary constants to generated files. The TB4/TB5/USB4 bus type constants must come from NVIDIA when they update the FINN source. Revert osinit.c to original TB3 legacy detection logic: - Use bTb3Bridge variable name (matches original) - Require both HotPlug+ AND Surprise+ (TB3 sets both) - Keep early-out for is_thunderbolt detection - Keep diagnostic logging for TB3 path
|
The more I think about this, I don't think I'm on the right path here. By playing around with IOMMU and PCIe kernel arguments I've gotten further, which makes me think this driver patch may be unnecessary. Disabling DMA Protection in BIOS causes panics at a different point in the boot cycle. I'm not sure why yet, but it leads me to believe it's IOMMU/DMA related. I'm going to close this PR for now and will update #979 with any new findings or patches, and see if I can grab a dump in the process. Whatever is going on though seems to be happening on both 580 and 590 on kernel 6.12.x. |
|
I found that patching 590 wasn't consistent. I downgraded to 575 and am working on a patch, will post to #979 when it's done and I can run some stress tests. |
Summary
This PR adds/improves support for external GPUs (eGPUs) connected via Thunderbolt 4, Thunderbolt 5, and USB4 interfaces. The official driver only recognizes Thunderbolt 3 bridges, causing TB4/TB5/USB4 eGPUs to be (potentially) treated as internal GPUs with inappropriate timeout values and no hotplug handling, resulting in system hard-locks during CUDA operations.
Relates to: #979, #894
Changes
NV2080_CTRL_INTERNAL_EGPU_BUS_TYPE_TB5and_USB4bus type constantsis_thunderboltflag (vendor-agnostic)osSchedule()every 256 iterations)Tested Hardware
Files Modified
src/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080internal.hsrc/nvidia/arch/nvalloc/unix/src/osinit.cis_thunderboltflagsrc/nvidia/arch/nvalloc/unix/src/os.csrc/nvidia/src/kernel/gpu/gsp/kernel_gsp.csrc/nvidia/src/kernel/gpu/gsp/arch/hopper/kernel_gsp_gh100.csrc/nvidia/src/kernel/gpu/fsp/kern_fsp.ckernel-open/nvidia/nv-pci.cBuild and Install
Technical Details
Why the Official Driver Fails
The driver only recognizes Thunderbolt 3 bridges as eGPU connections:
Intel TB4/TB5 device IDs are not in the approved list, and no
TB4,TB5, orUSB4bus type exists. Without external GPU recognition:PDB_PROP_GPU_IS_EXTERNAL_GPUis not setHard-Locks
GSP polling loops spin without yielding:
Thunderbolt adds latency. These loops spin long enough to trigger soft-lockup detection or freeze the system entirely.
How nvidia-persistenced Interacts
nvidia-persistenced uses
libnvidia-cfg.so.1to manage devices:On TB5 eGPU hotplug:
/dev/nvidia*device filesnvCfgOpenPciDevice()RmInitAdapter)With these patches, the kernel detects the external GPU via the kernel's
is_thunderboltflag, automatically retries BAR validation if the Thunderbolt tunnel isn't ready, uses 4x timeouts for GSP communication, and yields the CPU during spin loops. nvidia-persistenced works unchanged.Diagnostic Evidence
From nvidia-bug-report.log:
0xBADF21is a FECS PRI error not defined in the Blackwell error table. The function name_GH100(Hopper) being called for GB203 (Blackwell) shows inherited code paths. Register read failures point to PCIe link instability.User Configuration
Required: Kernel Parameters
sudo grubby --update-kernel=ALL --args="iommu=pt pcie_ports=native pcie_aspm=off pci=noaer pcie_port_pm=off"intel_iommu=offbreaks Thunderbolt PCIe tunneling. Useiommu=ptinstead.Required: Module Options
Recommended: Power Limiting via udev
Power limits require a running driver. The udev rule fires when the driver binds, before anything else can run the GPU at full TDP.
Optional: Compute-Only Mode
Usage
Recommended: Hotplug
Cold Boot (enclosure already on)
If the enclosure is powered on during boot, the driver will automatically retry BAR validation with exponential backoff (up to 1 second per PCIe spec 6.6.1) while the Thunderbolt tunnel initializes. If this fails:
Verification
Ollama with qwen3:4b in Docker also works without issues.
What Didn't Work
intel_iommu=off- Breaks Thunderbolt PCIe tunnel creationBug Report Log
Bug report with the GPU working is attached.
nvidia-bug-report.log-1.gz