Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: allow selective VFIO BAR mapping - #7991

Merged
rbradford merged 3 commits into
cloud-hypervisor:mainfrom
DamianB-BitFlipper:selective-bar-mmap-main
May 6, 2026
Merged

rbradford merged 3 commits into
cloud-hypervisor:mainfrom
DamianB-BitFlipper:selective-bar-mmap-main

Conversation

@DamianB-BitFlipper

@DamianB-BitFlipper DamianB-BitFlipper commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Purpose

On older host kernels, mmaping a very large VFIO BAR can add noticeable boot and hotplug latency because of host-side BAR mmap overhead and page-table growth. This option allows selected BARs to stay trap-and-emulate instead of being mmapped into the guest when that tradeoff is preferable.

For the original GPU use case, the large VRAM BAR was the main target: keeping that BAR out of the mmap path improved attach time while leaving smaller BARs available to be mmapped normally.

Summary

  • add x_exclude_mmap_bars to VFIO device configuration so specific BARs can stay trap-and-emulate instead of being mmapped into the guest
  • thread the new setting through the API/config/device-manager path and skip BAR mmap in VfioPciDevice::map_mmio_regions() for the selected BAR indices
  • document the new option and add VFIO integration coverage for the NVIDIA passthrough test path

@DamianB-BitFlipper
DamianB-BitFlipper requested a review from a team as a code owner April 10, 2026 15:26
@saravan2

Copy link
Copy Markdown
Member

@DamianB-BitFlipper It would help reviewers if you could explain "why" we need this feature in the PR description.
I would also recommend you to check the current vfio integration test suite and ascertain whether we could develop an integration test to cover your new changes. The commit messages should have a description. Whenever a PR introduces a new argument, it should also have documentation update commit in that series.
You could move this PR to draft and change its status when you are ready.

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

My bad, should have been a draft...

@DamianB-BitFlipper
DamianB-BitFlipper marked this pull request as draft April 10, 2026 17:54
@DamianB-BitFlipper

DamianB-BitFlipper commented Apr 10, 2026

Copy link
Copy Markdown
Contributor Author

@saravan2 Do you know who would be most capable to review these changes, before I proceed to add integration changes and fix the commit history?

I just want to make sure that these changes make sense. I also updated the description, adding the purpose of this PR.

@DemiMarie

Copy link
Copy Markdown
Contributor

@DamianB-BitFlipper What about lazily mapping the BARs on first access, at 1G granularity?

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

It might be a bit of a skills issue from my end, that I didn't implement it this way.

But if I get some pointers as to how to do it, I'll happily try it out and test it too on our system.

@saravan2

saravan2 commented Apr 14, 2026

Copy link
Copy Markdown
Member

@DamianB-BitFlipper I can review your changes. The maintainers would chime in and shape your PR to get it ready for merge.

@DemiMarie We can consider lazy bar mapping as a feature request because it requires significant changes to the MMIO exit path.

@DamianB-BitFlipper
DamianB-BitFlipper marked this pull request as ready for review April 14, 2026 22:18
@saravan2

saravan2 commented Apr 14, 2026

Copy link
Copy Markdown
Member

When mounting a GPU device with 100+GB of VRAM, a lot of time is spent simply during the IOMMU mmaping.

Can you check if you have --platform vfio_p2p_dma=on in your config ? Thats the only path I could think of that would issue synchronous VFIO_IOMMU_MAP_DMA to the host kernel.

Every other path I checked is lazy fault-based.

So if your ML applications never touch the large BARs, with vfio_p2p_dma=off there shouldn't be any penalty for mmaping the 100+GB VRAM BAR.

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

I do not pass it, but vfio_p2p_dma defaults to on. I didn't even know about this setting, and will give vfio_p2p_dma off a try.

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

I just did an AB test where I booted four VMs with a single H200 passthrough. Two were with vfio_p2p_dma off and no x_no_mmap_bars set. The other is with vfio_p2p_dma left on by default and x_no_mmap_bars selectively excluding the frame buffer.

Here are the attach times:

vfio_p2p_dma off -> 12.336837652s, 12.514751692s
using x_no_mmap_bars -> 5.039115267s, 5.056194296s

With these numbers in mind, I think this feature is quite useful.

@saravan2

Copy link
Copy Markdown
Member

For curiosity's sake, what was the time taken for vfio_p2p_dma left on by default and no x_no_mmap_bars ? It could serve a valuable datapoint to turn vfio_p2p_dma off by default for the next cloud-hypervisor release.

@DamianB-BitFlipper
DamianB-BitFlipper marked this pull request as draft April 15, 2026 01:19
@DamianB-BitFlipper
DamianB-BitFlipper marked this pull request as ready for review April 15, 2026 01:19
@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

vfio_p2p_dma explicitly on and not using x_no_mmap_bars: 12.387654951s, 12.136960855s

This is peculiar, I double checked I compiled and ran the right code. I am; those are the timings.

@saravan2

Copy link
Copy Markdown
Member

@DamianB-BitFlipper Is it possible that your host distro is Rocky 9 or RHEL 9 based with a 5.1x kernel?

Older kernels do not have hugepage VFIO support, so the mmap of VRAM BAR forces the host kernel to perform ioremap and KVM memslot metadata allocation at 4K granularity. The ~7 seconds you are shaving off with x_no_mmap_bars PR is by skipping the mmap entirely.

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

Nope. I'm running debian and kernel 6.16.9. I tried really hard to enable hugepage mmap for the iommu, but couldn't.

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

My bad, that's the guest. On the host, I am running Ubuntu 24.04 and 6.8.0.

@saravan2

saravan2 commented Apr 15, 2026

Copy link
Copy Markdown
Member

Linux 6.8 kernel does not have hugepage VFIO mmap support. It was introduced in kernel 6.11.

On 6.8, the ioremap inside vfio_pci_core_mmap and KVM memslot metadata allocation both operate at 4K granularity.

On a 6.11+ kernel with hugepage support enabled, the same mmap should be significantly faster and the savings from x_no_mmap_bars would be much smaller.

@DamianB-BitFlipper

DamianB-BitFlipper commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

Interesting, I didn't know. Though changing the host's kernel might be out of our control unfortunately.

@saravan2

Copy link
Copy Markdown
Member

@rbradford Would the maintainers accept this PR that primarily helps cloud-hypervisor VMs boot and hotplug GPUs faster on hosts running older kernels ( pre 6.11 ) without hugepage VFIO mmap support ?

@phip1611

Copy link
Copy Markdown
Member

@rbradford Would the maintainers accept this PR that primarily helps cloud-hypervisor VMs boot and hotplug GPUs faster on hosts running older kernels ( pre 6.11 ) without hugepage VFIO mmap support ?

You could also discuss this in tomorrows Cloud Hypervisor Office Hour.

@DamianB-BitFlipper same link, same time!

@saravan2

saravan2 commented Apr 15, 2026

Copy link
Copy Markdown
Member

On 6.8, the ioremap inside vfio_pci_core_mmap and KVM memslot metadata allocation both operate at 4K granularity

You can verify this thesis by comparing page table sizes grep VmPTE /proc/<CH PID>/status between master and x_no_mmap_bars on VM started with H200 passthrough.

When we dont mmap the large VRAM BAR on Linux 6.8 host kernel, the pagetable bloat would not be seen in CH and results in faster boot.

@DamianB-BitFlipper Since iommu mapping is not the source of latency, can you update the PR description with accurate information if you are going to table this in the office hour ?

@rbradford

Copy link
Copy Markdown
Member

We discussed today - all I think we need is a.) @saravan2's comment about it being page table related not IOMMU b.) Change to x_exclude_mmap_bars

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

Changes should be in!

@saravan2 saravan2 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall PR looks good.
Requested minor changes.

Comment thread vmm/src/config.rs Outdated
Comment thread docs/vfio.md Outdated
Comment thread vmm/src/api/openapi/cloud-hypervisor.yaml
Comment thread vmm/src/api/openapi/cloud-hypervisor.yaml
@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

I think @saravan2 recommended to widen to u64. See #7991 (comment)

@rbradford

Copy link
Copy Markdown
Member

I think @saravan2 recommended to widen to u64. See #7991 (comment)

Okey dokey - still have a whitespace issue to fix?

@likebreath likebreath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delayed review. Overall this looks good. Just one clarification needed on the logging message, see below.

Comment thread pci/src/vfio.rs
Allow VFIO devices to list BAR indices that should not be
mmapped into the guest. This lets operators skip large BARs that
are known not to be used by their workload.

When a BAR is skipped, the log also calls out that P2P DMA
mapping is skipped because the VFIO DMA map path uses the same
mmap backing.

Signed-off-by: Damian Barabonkov <[email protected]>
Assisted-by: OpenCode:gpt-5.5
Explain how operators can exclude selected VFIO BARs from mmap
when they know their workloads do not require MMIO access to those
regions.

Documenting the option separately keeps the new device argument
discoverable and calls out the valid BAR index range.

Signed-off-by: Damian Barabonkov <[email protected]>
Assisted-by: OpenCode:gpt-5.5
@rbradford
rbradford added this pull request to the merge queue May 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 6, 2026
@rbradford
rbradford added this pull request to the merge queue May 6, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 6, 2026
@rbradford

Copy link
Copy Markdown
Member

Fails on the VFIO test - timeout reached. Please investigate - might just need timeout increasing or could be a real issue

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

I'll give it a look

Exercise the new VFIO BAR exclusion option with NVIDIA
passthrough tests so the integration suite checks that selected
BARs are skipped.

The tests cover both legacy VFIO and iommufd paths while
preserving the existing hardware availability guards.

Signed-off-by: Damian Barabonkov <[email protected]>
Assisted-by: OpenCode:gpt-5.5
@DamianB-BitFlipper
DamianB-BitFlipper force-pushed the selective-bar-mmap-main branch from 8dd9d95 to b317097 Compare May 6, 2026 13:10
@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

@rbradford I updated the integration test. Can you retry adding to the merge queue please?

@rbradford
rbradford enabled auto-merge May 6, 2026 13:48
@rbradford
rbradford added this pull request to the merge queue May 6, 2026
Merged via the queue into cloud-hypervisor:main with commit 4eb1717 May 6, 2026
41 checks passed
@rbradford

Copy link
Copy Markdown
Member

@DamianB-BitFlipper Your new test caused integration test failures that kicked this change off the MQ - please investigate: https://github.com/cloud-hypervisor/cloud-hypervisor/actions/runs/25453310169/job/74677202205

@DamianB-BitFlipper

Copy link
Copy Markdown
Contributor Author

@saravan2 made some suggestions in the review comment that should address the failing integration test.

I'll open a follow up PR

@saravan2

saravan2 commented May 25, 2026

Copy link
Copy Markdown
Member

@saravan2 made some suggestions in the review comment that should address the failing integration test.

I'll open a follow up PR

#7991 (comment)

@DamianB-BitFlipper I had an offline conversation with @likebreath

The standard hugepages supported in IOMMU via iommu_pgsize did not accelerate vfio_dma_map portion.

That was because the PFN walk in drivers/vfio/vfio_iommu_type1.c, vaddr_get_pfns(), returned only "one" 4 KiB PFN at a time in 6.8 Linux kernel and forced the outer vfio_pin_pages_remote() loop to iterate on 4KiB granularity (instead of hugepage as I assumed based on this result from your earlier experiment in #7991 (comment))

https://elixir.bootlin.com/linux/v6.8/source/drivers/vfio/vfio_iommu_type1.c#L608-L722

The support to map in hugepage size was added later in Linux kernel in commit
torvalds/linux@0fd06844de5d

So as @likebreath mentioned in his comment the tradeoff for fast boot/hotplug achieved through x_exclude_mmap_bars is also via disabling p2p dma and the users cannot expect performant GPUDirect RDMA.

The current implementation we merged is correct and no further changes are necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

7 participants