CUDA fails in guests with a passed-through NVIDIA GPU: the 64-bit PCI window leaves no free range for HMM

Aperta
#9,269 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
linux, rust
Ambito
cloud

Direzione di ricerca

Start from mmio64_aperture_weight in the per-segment PCI aperture computation, and from PciSegmentConfig where the new optional field would be parsed and validated. The issue proposes a size bound on the 64-bit window that leaves the rest unclaimed, with the segment's start address kept. Done means the option is validated (non-zero, 4 KiB multiple), the default layout is unchanged, and the unit tests for the computed apertures and old-config deserialization pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Elevator pitch
An opt-in per-segment cap on the 64-bit MMIO aperture, next to the existing mmio64_aperture_weight:

--pci-segment pci_segment=0,mmio64_aperture_size=64G

It leaves the rest of the guest physical address space unclaimed. Without it, CUDA fails in guests with a passed-through NVIDIA GPU: the 64-bit PCI window leaves no free range for the driver's HMM.

Motivation
With an NVIDIA GPU passed through via VFIO, nvidia-smi works in the guest, but every CUDA program fails in cuInit with CUDA_ERROR_NOT_INITIALIZED (3).

Setup:

  • Host: AMD EPYC, RTX 4090.
  • Guest: Ubuntu 24.04, kernel 6.8.0-147, NVIDIA open driver 580.178.04.
  • Cloud Hypervisor v51.1 and v54.0 both fail.

Cause: Cloud Hypervisor's 64-bit PCI window runs from the end of RAM to just below the top of the guest physical address space. In an 8 GiB guest with 46 physical address bits:

100000000-23fffffff : System RAM
240000000-3fffffeeffff : PCI Bus 0000:00

The GPU itself uses much less (288 MiB) than the reserved window.

NVIDIA's nvidia_uvm uses Linux HMM by default on kernels with CONFIG_DEVICE_PRIVATE (e.g. Ubuntu's generic kernel). HMM registers the GPU's memory as device-private memory, which needs a free physical range the size of VRAM. It asks for one using request_free_mem_region() and finds none, upon which it errors:

nvidia-uvm: uvm_pmm_gpu.c:3252 devmem_alloc_pagemap[pid:767] request_free_mem_region() err -34
nvidia-uvm: ... init_parent_gpu ... failed to intialize device private memory: Out of memory [NV_ERR_NO_MEMORY]

UVM then does not initialize. nvidia-smi does not use UVM, so it keeps working.

Workarounds that work today, and confirm the cause, both with drawbacks:

  • nvidia_uvm.uvm_disable_hmm=1 on the guest command line (3/3): requires changing the guest, and turns off HMM-based features in it.
  • --memory hotplug_size=64G (3/3) or 480G (2/2): the hotplug area is not claimed in iomem, so the driver finds a free range there. This reserves address space the user may not want, and the HMM region then sits inside the hotplug area.

Prior art
QEMU's q35 bounds its 64-bit window (pci-hole64-size, 32 GiB by default) and leaves the rest unclaimed. The same guest image and GPU work there: CUDA succeeded 11/11 with HMM on. The driver places its HMM region at the top of the space:

e0000000000-e07ffffffff : PCI Bus 0000:00
3ffa18000000-3fffffffffff : nvidia-uvm-hmm

In this project, mmio64_aperture_weight already lets a user shape the 64-bit apertures per segment; this adds a size bound next to it.

API/CLI

  • CLI: a new optional mmio64_aperture_size=<size> on --pci-segment.
  • API: a matching optional field on PciSegmentConfig (OpenAPI schema updated).
  • Validation: non-zero, multiple of 4 KiB.
  • Without the option nothing changes, and configs without the field deserialize as before.

Testing

  • Unit tests:
    • the computed apertures: no bound, a bound above the share, single and multi-segment, the aarch64/riscv64 base;
    • config parsing and validation;
    • serde of an old config without the field.
  • On hardware, same binary, same guest, HMM on:
--pci-segment                                CUDA ok   64-bit window in guest iomem
(none)                                       0/3       240000000-3fffffeeffff
pci_segment=0,mmio64_aperture_size=64G       3/3       240000000-123fffffff
pci_segment=0,mmio64_aperture_size=32G       2/2       240000000-a3fffffff
  • With two segments and segment 0 capped, segment 1 kept its default range (200040000000-3fffffeeffff).
  • CI: the GPU job would not catch this, because it checks only nvidia-smi (check_nvidia_gpu). A small CUDA step (e.g. cuInit + cuDeviceGet) in vfio::test_nvidia would. That needs the CUDA driver API in the guest image, but no new hardware.

Interactions

  • Device hotplug: the bound must leave room for the 64-bit BARs of every device on the segment, including devices hotplugged later. The default stays unbounded, so large-BAR hotplug is unaffected unless the option is set.
  • Memory hotplug (ACPI and virtio-mem): those areas sit below the 64-bit window and are unchanged.
  • Multiple segments and weights: a capped segment keeps its start address and the rest of its weighted share is left unclaimed. Other segments stay in place.
  • aarch64/riscv64: the FDT describes the 64-bit window only from PCI_HIGH_BASE (8 GiB) up, so there the bound counts from PCI_HIGH_BASE.
  • Snapshot/restore and live migration: configs without the field restore as before. A VM using the option, restored on an older binary, loses the bound silently (unknown fields are ignored). Its devices keep their BARs, but a later hotplug could land above the window the guest was told about.

Implementation
Per the contribution guidelines I'm opening this as an issue first rather than a PR. A tested implementation is on a branch in my fork: https://github.com/cloud-hypervisor/cloud-hypervisor/compare/main...mushroomcarbon:cloud-hypervisor:vmm-mmio64-aperture-size. Happy to open it as a PR if acceptable.

Full feature description
Things we ruled out as the cause:

  • PCI topology: QEMU with the GPU directly on pcie.0 (no root port) still works. Ubicloud's B200 write-up and kata-containers #13992 attribute this failure to the flat PCI topology, but that is not what we found on our hardware and setup.
  • Where the BARs land: max_phys_bits=40 still fails.
  • KASLR: nokaslr still fails. NVIDIA's CUDA 13.0 release notes attribute this error line to KASLR, but that is not the cause here.

open-gpu-kernel-modules #901 reports the same symptom with HMM on bare metal. We have not tested on B200s or other datacenter GPU setups.

Alternatives considered:

  • A bounded default: simpler for users, but it changes the default layout, which raises the same cross-version migration concern as #8279. It also limits how large a BAR can be hotplugged by default. An opt-in avoids both.
  • Allocating BARs at the start of the window (#8195) or lowering max_phys_bits: neither frees address space, because the guest is still told the whole window is in use.
Lingua principale
Rust
Stelle
6.3k
Fork
786
Merge medio
1g 3h
PR unite (30g)
195

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloud-hypervisor/cloud-hypervisor

Tutte le issue di cloud-hypervisor/cloud-hypervisor

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.