Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtio-device: Mapping mmio over virtio-iommu #6297

Conversation

acarp-crusoe
Copy link
Contributor

@acarp-crusoe acarp-crusoe commented Mar 14, 2024

Add infrastructure to lookup the host address for mmio regions on external dma mapping requests. This specifically resolves vfio passthrough for virtio-iommu, allowing for nested virtualization to pass external devices through.

Fixes #6110

@acarp-crusoe acarp-crusoe requested a review from a team as a code owner March 14, 2024 23:23
@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch 2 times, most recently from 49d5bba to 5622317 Compare March 14, 2024 23:32
@rbradford
Copy link
Member

@acarp-crusoe If a commit fixes a bug please can you put "Fixes: #" in the commit message itself - if it's just relevant "See: #"

@acarp-crusoe
Copy link
Contributor Author

@acarp-crusoe If a commit fixes a bug please can you put "Fixes: #" in the commit message itself - if it's just relevant "See: #"

Commit message has been updated to include the fixed bug - thanks

@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch from 19dfd6e to fe782d7 Compare March 15, 2024 17:42
@acarp-crusoe
Copy link
Contributor Author

From what I can see, the existing workflow also appears to be failing on the main branch so there's not much I can do to address that. Everything else is ready to go though

@rbradford
Copy link
Member

@thomasbarrett would you like to review?

Copy link
Member

@rbradford rbradford left a comment

Choose a reason for hiding this comment

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

@acarp-crusoe This PR also has some extra whitespace changes - do you mind taking a look?

@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch 2 times, most recently from 59afa8c to 87a63fb Compare March 19, 2024 17:33
@acarp-crusoe
Copy link
Contributor Author

@acarp-crusoe This PR also has some extra whitespace changes - do you mind taking a look?

Sorry about that, whitespace changes should be reverted

pci/src/vfio.rs Outdated Show resolved Hide resolved
@thomasbarrett
Copy link
Contributor

It seems like we are finding ourselves needing to re-implementing a lot of the functionality from the vm_memory crate for mmio. It would be great if we could re-use the functionality from vm_memory (all of which applies to mmio as well as physical RAM). This is more of an observation... I don't think that we need to look into this for this PR.

rbradford
rbradford previously approved these changes Mar 20, 2024
@rbradford rbradford dismissed their stale review March 20, 2024 18:26

I missed @thomasbarrett's review.

@thomasbarrett
Copy link
Contributor

Sorry for the slow review @rbradford. @acarp-crusoe and I were at a conference :)

@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch 2 times, most recently from 07642e7 to 4dac0e0 Compare March 21, 2024 17:02
pci/src/vfio.rs Show resolved Hide resolved
Copy link
Member

@likebreath likebreath left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution. I'd suggest to split this change into two commits. The first commit is refactoring (e.g. no functional change) of moving VfioDmaMapping to the pci crate. And the second commit (the actual functional change) is extending the VfioDmamapping struct to support lookup the user address of mmio regions. Separating functional changes from refactoring is generally cleaner and can help a lot with code review.

Also, I wonder why a Cloud Hypervisor L2 VM works while a QEMU L2 VM does not work? Parden me that I have limited understanding of VFIO and vMMIO.

@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch 3 times, most recently from 9c1d8fd to ec4b9fe Compare March 21, 2024 23:11
@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch from ec4b9fe to 86033ce Compare March 21, 2024 23:31
@acarp-crusoe
Copy link
Contributor Author

Thank you for the contribution. I'd suggest to split this change into two commits. The first commit is refactoring (e.g. no functional change) of moving VfioDmaMapping to the pci crate. And the second commit (the actual functional change) is extending the VfioDmamapping struct to support lookup the user address of mmio regions. Separating functional changes from refactoring is generally cleaner and can help a lot with code review.

Also, I wonder why a Cloud Hypervisor L2 VM works while a QEMU L2 VM does not work? Parden me that I have limited understanding of VFIO and vMMIO.

Sounds good, the change has just been split into two commits (one the refactor and the other functional), just let me know if you'd need anything else.

And to answer your question - both Cloud Hypervisor and Qemu worked as L2 VMs within Cloud Hypervisor. However, if you tried to include any external pci devices as part of the L2 VM, that wouldn't work. The issue lied in the fact that the mapping requests that arrived in the virtio iommu device did not check against mmio regions at all within VfioDmaMapping. Originally VfioDmaMapping just attempted to dma map RAM using the guest_memory mmap stored in the memory_manager, but mmio regions are not included in the guest_memory mmap. This PR adds mmio_regions as a separate object in the device_manager so the VfioDmaMapping is able to check both guest memory (RAM) as well as mmio. With this change, we can now map mmio over virtio iommu properly, whereas before the L1 VM would crash because it receiving a mapping request for a band of mmio memory it couldn't locate.

Copy link
Member

@likebreath likebreath left a comment

Choose a reason for hiding this comment

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

The code changes make sense and look good to me. As I am not an VFIO expert, another review will be very helpful. @rbradford @thomasbarrett

pci/src/vfio.rs Outdated Show resolved Hide resolved
pci/src/vfio.rs Outdated Show resolved Hide resolved
@likebreath
Copy link
Member

And to answer your question - both Cloud Hypervisor and Qemu worked as L2 VMs within Cloud Hypervisor. However, if you tried to include any external pci devices as part of the L2 VM, that wouldn't work.

This makes sense if both Cloud Hypervisor and QEMU L2 VM with VFIO devices are failing. I thought the observation was that L2 Cloud Hypervisor with VFIO works according to this comment: #6110 (comment). Thanks for clarifying that.

@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch 2 times, most recently from dcb3f82 to f9bad5e Compare March 22, 2024 23:39
@acarp-crusoe
Copy link
Contributor Author

And to answer your question - both Cloud Hypervisor and Qemu worked as L2 VMs within Cloud Hypervisor. However, if you tried to include any external pci devices as part of the L2 VM, that wouldn't work.

This makes sense if both Cloud Hypervisor and QEMU L2 VM with VFIO devices are failing. I thought the observation was that L2 Cloud Hypervisor with VFIO works according to this comment: #6110 (comment). Thanks for clarifying that.

No problem, and that previous comment you've linked was when I was trying to get an understanding of the bug that @thomasbarrett produced, so it's actually a little off. I added a followup comment to clarify the behavior of the bug now that we have a good understanding.

Copy link
Member

@likebreath likebreath left a comment

Choose a reason for hiding this comment

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

Few more comments below. Also, it is good to use verb to start the commit message. Say:

virtio-devices: Move VfioDmaMapping to be in the pci crate
virtio-devices: Map mmio over virtio-iommu

pci/src/vfio.rs Outdated Show resolved Hide resolved
pci/src/vfio.rs Outdated Show resolved Hide resolved
VfioUserDmaMapping is already in the pci crate, this moves
VfioDmaMapping to match the behavior. This is a necessary change to
allow the VfioDmaMapping trait to have access to MmioRegion memory
without creating a circular dependency. The VfioDmaMapping trait
needs to have access to mmio regions to map external devices over
mmio (a follow-up commit).

Signed-off-by: Andrew Carp <acarp@crusoeenergy.com>
Add infrastructure to lookup the host address for mmio regions on
external dma mapping requests. This specifically resolves vfio
passthrough for virtio-iommu, allowing for nested virtualization to pass
external devices through.

Fixes cloud-hypervisor#6110

Signed-off-by: Andrew Carp <acarp@crusoeenergy.com>
@acarp-crusoe acarp-crusoe force-pushed the vfio-external-dma-map-mmio-regions branch from f9bad5e to c04ff32 Compare March 25, 2024 23:33
@likebreath
Copy link
Member

The PR looks good to me. @thomasbarrett @rbradford And additional comments?

@thomasbarrett
Copy link
Contributor

No additional comments. Looks good to me :)

@rbradford rbradford added this pull request to the merge queue Apr 1, 2024
Merged via the queue into cloud-hypervisor:main with commit 045964d Apr 1, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

Nested virtualization not working with VFIO devices.
4 participants