[RFC PATCH 0/6] KVM: guest_memfd: back guest_memfd with an imported dma-buf

From: Fred Griffoul

Date: Mon Oct 05 2026 - 05:56:12 EST


From: Fred Griffoul <fgriffo@xxxxxxxxxxxx>

This extends David Woodhouse's "KVM: Allow alternative providers of
guest_memfd backed by PFNMAP memory" RFC (v2) to let guest_memfd import
a dma-buf as its page source:

https://lore.kernel.org/kvm/20260720111259.122911-1-dwmw2@xxxxxxxxxxxxx/

The memory owner is a plain dma-buf exporter, linking against neither
KVM nor iommufd. guest_memfd imports it for the guest (stage-2) plane
and iommufd imports the same buffer for the device (DMA) plane, so one
object and one invalidation protocol serve both. This is a concrete
answer to the "how do we plug into iommufd without masquerading as a
dma-buf" question from David's cover: the provider does not pretend to
be anything; the dma-buf is the shared object.

Applies on top of David's v2 series.

Patches:

1 KVM: guest_memfd: Add a writable result to get_pfn()
Give get_pfn() a writable out-param so a backing can map a page
present but read-only (KVM_MEM_READONLY is rejected on guest_memfd
slots). A guest write then exits as a memory fault. Stage-2 only;
KVM's own host-mapped accesses are unaffected.

2 dma-buf: Add get_phys() to describe a physical run
New exporter op: for an offset+len, return one physically contiguous
backed run and an attribute word (RAM/MMIO, plus READONLY). -ENOENT
for an unbacked byte, -ENODEV when revoked. Lets a page-table-building
importer ask where the memory is instead of taking a scatterlist.
Converts vfio-pci, the iommufd selftest exporter and the sample;
iommufd behaviour is unchanged.

3 dma-buf: Add ranged mapping invalidation
Invalidate a byte range rather than the whole buffer. The callback
means "re-query this range" and covers loss, replacement and
read-only flips. Importers without the callback still get a
whole-buffer invalidation.

4 dma-buf: Allow dynamic attach without a device
Allow a dynamic importer with no struct device, for a consumer (KVM)
that only needs the physical layout and builds its own tables. Such
an importer must not map the attachment for DMA.

5 KVM: guest_memfd: Add dma-buf backing
Add GUEST_MEMFD_FLAG_USE_DMABUF and a dmabuf_fd to
KVM_CREATE_GUEST_MEMFD. guest_memfd attaches as a revocable importer
without a device, serves faults from get_phys() (largest aligned RAM
run, read-only honoured), and zaps the range on the invalidation
callback.

6 samples/kvm, selftests/kvm: Exercise dma-buf backing
Turn the sample into a dma-buf exporter (a toy memory owner: one root
region, a child per VM, move/donate/reclaim, absent and read-only
pages, a scratch page, per-child ioctl allowlists) and move the
selftests to feed one dma-buf fd to both guest_memfd and iommufd.

Scope: non-confidential VMs, RAM backings only; iommufd keeps its
single-range import for now. As with David's series, this is largely
AI-assisted and is posted to get the interface shape reviewed.

Fred Griffoul (6):
KVM: guest_memfd: Add a writable result to get_pfn()
dma-buf: Add get_phys() to describe a physical run
dma-buf: Add ranged mapping invalidation
dma-buf: Allow dynamic attach without a device
KVM: guest_memfd: Add dma-buf backing
samples/kvm, selftests/kvm: Exercise dma-buf backing

arch/arm64/kvm/mmu.c | 13 +-
arch/arm64/kvm/nested.c | 10 +-
arch/x86/kvm/mmu/mmu.c | 18 +-
arch/x86/kvm/svm/sev.c | 4 +-
drivers/dma-buf/dma-buf.c | 92 +-
drivers/iommu/iommufd/iommufd_private.h | 8 -
drivers/iommu/iommufd/iommufd_test.h | 16 +
drivers/iommu/iommufd/pages.c | 74 +-
drivers/iommu/iommufd/selftest.c | 106 +-
drivers/vfio/pci/vfio_pci_dmabuf.c | 58 +-
include/linux/dma-buf.h | 70 +
include/linux/kvm_host.h | 12 +-
include/linux/vfio_pci_core.h | 3 -
include/trace/events/dma_buf.h | 2 +-
include/uapi/linux/kvm.h | 6 +-
samples/kvm/gmem_provider.c | 1311 ++++++++++-------
samples/kvm/gmem_provider.h | 144 +-
tools/include/uapi/linux/kvm.h | 6 +-
tools/testing/selftests/kvm/Makefile.kvm | 3 +-
.../kvm/gmem_provider_nvme_dma_test.c | 28 +-
.../testing/selftests/kvm/include/kvm_util.h | 25 +-
.../testing/selftests/kvm/x86/gmem_poc_test.c | 824 +++++++++++
.../kvm/x86/gmem_provider_hugepage_test.c | 30 +-
.../kvm/x86/gmem_provider_iommufd_test.c | 42 +-
.../kvm/x86/gmem_provider_readonly_test.c | 172 +++
.../kvm/x86/gmem_provider_revoke_test.c | 34 +-
.../selftests/kvm/x86/gmem_provider_test.c | 190 +--
.../kvm/x86/gmem_provider_vfio_test.c | 45 +-
virt/kvm/Kconfig | 1 +
virt/kvm/guest_memfd.c | 252 +++-
30 files changed, 2703 insertions(+), 896 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86/gmem_poc_test.c
create mode 100644 tools/testing/selftests/kvm/x86/gmem_provider_readonly_test.c

--
2.47.3