Skip to content

RFC KVM/VFIO: guest_memfd support for device MMIO resources

Aneesh Kumar K.V (Arm) kvm linux-coco linux-iommu linux-patches 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 RFC Latest13 patches 7 repliesIn review
  2. v1 RFC6 patches 4 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/13KVM: guest_memfd: Support PFN-backed resource providersDocumentation/virt/kvm/api.rst include/linux/guest_memfd.h include/linux/kvm_host.h +2 more+180 -370 replies
2/13KVM: guest_memfd: Support private/shared conversion of device memoryinclude/linux/guest_memfd.h include/linux/kvm_host.h virt/kvm/guest_memfd.c+213 -55 replies
3/13iommufd: Support MMIO provider attachment to vDEVICEsdrivers/iommu/iommufd/viommu.c include/linux/iommufd.h+112 -00 replies
4/13KVM: guest_memfd: Add device provider access callbacksinclude/linux/guest_memfd.h virt/kvm/guest_memfd.c+123 -00 replies
5/13vfio/pci: Track userspace BAR mappingsdrivers/vfio/pci/vfio_pci_core.c include/linux/vfio_pci_core.h+21 -00 replies
6/13vfio: Dispatch guest_memfd provider operations through the filesystemdrivers/vfio/vfio_main.c include/linux/vfio.h+118 -10 replies
7/13vfio/pci: Restrict host BAR access for guest_memfd providersdrivers/vfio/pci/Kconfig drivers/vfio/pci/Makefile drivers/vfio/pci/vfio_pci_core.c +5 more+178 -40 replies
8/13vfio/pci: Provide guest_memfd backing for PCI BARsDocumentation/virt/kvm/api.rst drivers/vfio/pci/Makefile drivers/vfio/pci/vfio_pci.c +3 more+339 -00 replies
9/13KVM: guest_memfd: Add a provider detach callbackinclude/linux/guest_memfd.h+3 -00 replies
10/13vfio/pci: Detach guest_memfd access before teardowndrivers/vfio/pci/vfio_pci_gmem.c drivers/vfio/pci/vfio_pci_gmem.h drivers/vfio/pci/vfio_pci_gmem_access.c +1 more+44 -40 replies
11/13KVM: guest_memfd: Remove device mappings on final closeDocumentation/virt/kvm/api.rst virt/kvm/guest_memfd.c+26 -40 replies
12/13KVM: Invalidate guest_memfd mappings before removing memslot bindingsvirt/kvm/guest_memfd.c virt/kvm/guest_memfd.h virt/kvm/kvm_main.c+77 -30 replies
13/13vfio/pci: Allow VFIO BAR mmap with an attached guest_memfdDocumentation/virt/kvm/api.rst drivers/vfio/pci/vfio_pci_core.c drivers/vfio/pci/vfio_pci_gmem.c +3 more+99 -300 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [RFC PATCH v2 00/13] KVM/VFIO: guest_memfd support for device MMIO resources

This series adds a guest_memfd device-resource framework for exposing PCI BAR memory to guests. The intended use is confidential device assignment, where KVM needs to coordinate private device mappings with shared access through VFIO. The Arm CCA backend is provided separately; these patches contain the guest_memfd, VFIO and IOMMUFD interfaces it uses.

The guest_memfd framework and Arm CCA backend are available together at:

https://git.gitlab.arm.com/linux-arm/linux-cca.git cca/topics/cca-da-gmem-backend

Userspace creates a device-backed guest_memfd using the ordinary VFIO device fd as resource_fd, with GUEST_MEMFD_FLAG_USE_RESOURCE and GUEST_MEMFD_FLAG_INIT_SHARED. The resource mapping superblock selects its provider; get_pfn identifies PFN-backed memory. Eligible BAR intervals are registered as guest_memfd memslots. File offsets use the existing VFIO region-offset namespace, allowing one guest_memfd to cover multiple BAR intervals. The device-backed guest_memfd fd rejects mmap and fallocate; shared guest faults obtain bare BAR PFNs from the provider instead of resolving the memslot HVA or allocating folios.

IOMMUFD resolves and pins the device's existing vDEVICE after its backend validates the VM association. Its MMIO mutex coordinates conversion and device state changes. VFIO supplies BAR PFNs, validates eligible ranges and excludes dma-buf exports while a guest_memfd provider is attached.

Without GUEST_MEMFD_FLAG_MMAP, attachment rejects existing VFIO BAR VMAs, and new VFIO mmap calls. With the flag, existing and new BAR VMAs are allowed through the VFIO fd. Attachment invalidates existing BAR mappings, including huge mappings. Subsequent faults use base-page mappings so each page's shared-range attributes are checked separately. Mapping the PFN-backed guest_memfd fd remains disabled (We can decide on this later).

VFIO BAR read/write, ioeventfd and userspace mapping faults check the requested range's guest_memfd attributes.

This series depends on:
* Ackerley Tng's "Allow guest_memfd to be created using a resource (pool) fd" RFC, which provides the tmpfs resource-provider API:
https://lore.kernel.org/all/[email protected]/
* The IOMMUFD/TSM series, which provides the vIOMMU and vDEVICE interfaces:
https://lore.kernel.org/all/[email protected]/

Notes:
* Codex assisted with implementation, review and series organization.

* Changes from v1:
https://lore.kernel.org/all/[email protected]
* Require GUEST_MEMFD_FLAG_MMAP to allow VFIO BAR mmap while a provider is attached; disable it by default.
* Use the common guest_memfd_provider_operations for folio and PFN providers. Select operations through the resource mapping superblock and forward VFIO operations to the device driver.

Cc Ackerley Tng <[email protected]>
Cc Alex Williamson <[email protected]>
Cc David Woodhouse <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc "Joerg Roedel (AMD)" <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Will Deacon <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Xu Yilun <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Steven Price <[email protected]>
Cc Fred Griffoul <[email protected]>

Aneesh Kumar K.V (Arm) (13):
KVM: guest_memfd: Support PFN-backed resource providers
KVM: guest_memfd: Support private/shared conversion of device memory
iommufd: Support MMIO provider attachment to vDEVICEs
KVM: guest_memfd: Add device provider access callbacks vfio/pci: Track userspace BAR mappings
vfio: Dispatch guest_memfd provider operations through the filesystem vfio/pci: Restrict host BAR access for guest_memfd providers vfio/pci: Provide guest_memfd backing for PCI BARs
KVM: guest_memfd: Add a provider detach callback
vfio/pci: Detach guest_memfd access before teardown
KVM: guest_memfd: Remove device mappings on final close
KVM: Invalidate guest_memfd mappings before removing memslot bindings vfio/pci: Allow VFIO BAR mmap with an attached guest_memfd

 Documentation/virt/kvm/api.rst          |  26 +-
 drivers/iommu/iommufd/viommu.c          |  86 ++++
 drivers/vfio/pci/Kconfig                |  11 +
 drivers/vfio/pci/Makefile               |   2 +
 drivers/vfio/pci/vfio_pci.c             |   3 +
 drivers/vfio/pci/vfio_pci_core.c        |  34 ++
 drivers/vfio/pci/vfio_pci_dmabuf.c      |  10 +
 drivers/vfio/pci/vfio_pci_gmem.c        | 362 +++++++++++++++
 drivers/vfio/pci/vfio_pci_gmem.h        |  26 ++
 drivers/vfio/pci/vfio_pci_gmem_access.c | 135 ++++++
 drivers/vfio/pci/vfio_pci_rdwr.c        |  13 +-
 drivers/vfio/vfio_main.c                | 121 ++++-
 include/linux/guest_memfd.h             |  60 ++-
 include/linux/iommufd.h                 |  26 ++
 include/linux/kvm_host.h                |  11 +
 include/linux/vfio.h                    |   4 +
 include/linux/vfio_pci_core.h           |  30 ++
 mm/shmem.c                              |   3 +-
 virt/kvm/guest_memfd.c                  | 566 ++++++++++++++++++++++--
 virt/kvm/guest_memfd.h                  |   6 +
 virt/kvm/kvm_main.c                     |   4 +
 21 files changed, 1492 insertions(+), 47 deletions(-)
 create mode 100644 drivers/vfio/pci/vfio_pci_gmem.c
 create mode 100644 drivers/vfio/pci/vfio_pci_gmem.h
 create mode 100644 drivers/vfio/pci/vfio_pci_gmem_access.c
Signature · 1 line
2.43.0