From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev
Cc: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>,
Ackerley Tng <ackerleytng@google.com>,
Alex Williamson <alex@shazbot.org>,
David Woodhouse <dwmw2@infradead.org>,
David Hildenbrand <david@kernel.org>,
Jason Gunthorpe <jgg@nvidia.com>,
"Joerg Roedel (AMD)" <joro@8bytes.org>,
Kevin Tian <kevin.tian@intel.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Robin Murphy <robin.murphy@arm.com>,
Sean Christopherson <seanjc@google.com>,
Alexey Kardashevskiy <aik@amd.com>,
Xu Yilun <yilun.xu@linux.intel.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Steven Price <steven.price@arm.com>,
Fred Griffoul <griffoul@gmail.com>,
iommu@lists.linux.dev, kvm@vger.kernel.org,
patches@lists.linux.dev
Subject: [RFC PATCH v4 25/25] KVM: Document device-backed guest_memfd and VFIO usage
Date: Wed, 4 Nov 2026 20:33:22 +0530 [thread overview]
Message-ID: <20261104150322.3516320-26-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261104150322.3516320-1-aneesh.kumar@kernel.org>
Describe device setup, VFIO access restrictions, mappings and lock ordering.
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Alex Williamson <alex@shazbot.org>
Cc: David Hildenbrand <david@kernel.org>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: "Joerg Roedel (AMD)" <joro@8bytes.org>
Cc: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
Documentation/virt/kvm/api.rst | 50 ++++++++++++++++++++++++++----
Documentation/virt/kvm/locking.rst | 27 ++++++++++++++++
2 files changed, 71 insertions(+), 6 deletions(-)
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 4482d34ec281..289b42224258 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6513,8 +6513,8 @@ The capability KVM_CAP_GUEST_MEMFD_FLAGS enumerates the `flags` that can be
specified via KVM_CREATE_GUEST_MEMFD. Currently defined flags:
============================= ================================================
- GUEST_MEMFD_FLAG_MMAP Enable using mmap() on the guest_memfd file
- descriptor.
+ GUEST_MEMFD_FLAG_MMAP Enable userspace mappings through guest_memfd,
+ including shared VFIO PCI BAR resources.
GUEST_MEMFD_FLAG_INIT_SHARED Make all memory in the file shared during
KVM_CREATE_GUEST_MEMFD (memory files created
without INIT_SHARED will be marked private).
@@ -6540,10 +6540,48 @@ provider. PFN_MODE selects the PFN callbacks; providers that supply device
operations additionally require INIT_SHARED and a VM supporting protected
device mappings.
-KVM_SET_MEMORY_ATTRIBUTES2 converts device memory using guest_memfd offsets.
-For private conversion, KVM resolves the guest address through the memslot
-binding and obtains the physical address from the provider. Shared conversion
-removes the protected device mappings before publishing shared attributes.
+For VFIO PCI device resources, resource_fd must refer to a VFIO device fd
+bound to IOMMUFD, with a vDEVICE belonging to the same VM. Set USE_RESOURCE,
+PFN_MODE and INIT_SHARED. File offsets use the VFIO region-offset namespace.
+Memslots must cover page-aligned ranges within a single memory-mapped I/O
+BAR that supports mmap. Dirty logging and read-only memslots are rejected
+with EINVAL.
+
+Creation fails with EBUSY if the device already supplies another guest_memfd,
+has VFIO BAR mappings, dma-buf exports or ioeventfd handlers, or userspace has
+enabled VFIO runtime suspend. The device is resumed and must be in D0; it is
+kept active until guest_memfd releases it.
+
+While guest_memfd uses the device, the following operations fail with EBUSY
+through any VFIO fd referring to it, even if all ranges are shared:
+
+- BAR read/write, mmap, new ioeventfd registration and dma-buf export.
+- VFIO_DEVICE_RESET, PCIe/AF function-level reset and hot reset affecting
+ the device.
+- Requests for D1, D2 or D3, enabling VFIO runtime suspend, and configuration
+ writes that change Memory Enable or Bus Master Enable.
+
+Rejected configuration writes leave the hardware and virtual registers
+unchanged. Unrelated configuration writes, queries and IRQ configuration
+remain available.
+
+GUEST_MEMFD_FLAG_MMAP permits MAP_SHARED mappings through guest_memfd.
+Without this flag, mmap fails with ENODEV.
+
+MSI-X SET_IRQS remains available. Userspace must keep the MSI-X table and
+PBA pages shared while host-managed MSI-X is in use and reject guest private
+mapping requests for those pages. The kernel does not enforce this policy.
+
+Use :ref:`KVM_SET_MEMORY_ATTRIBUTES2` to convert device memory by guest_memfd
+offset. Private conversion uses the memslot binding and provider PFNs to
+install protected device mappings before publishing private attributes.
+Shared conversion removes those mappings before publishing shared attributes.
+
+Revocation and memslot deletion remove shared and private mappings without
+changing file attributes. Private ranges still prevent device unlock after
+these operations; userspace must share them or release the last guest_memfd
+reference. Final release removes mappings and clears the private-range
+summary before releasing the device resources.
When the KVM MMU performs a PFN lookup to service a guest fault, the fault will
always be consumed from guest_memfd, regardless of whether it is a shared or a
diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst
index 662231e958a0..a2a6b4dd9a1f 100644
--- a/Documentation/virt/kvm/locking.rst
+++ b/Documentation/virt/kvm/locking.rst
@@ -58,6 +58,33 @@ On x86:
kvm->arch.tdp_mmu_pages_lock and kvm->arch.mmu_unsync_pages_lock must
also take kvm->arch.mmu_lock
+For device-backed guest_memfd, conversion takes locks in this order:
+
+- inode->i_mapping->invalidate_lock for write
+- IOMMUFD vDEVICE mmio_lock
+- VFIO vdev->memory_lock for read, during PFN lookup
+
+The inode invalidate lock protects attributes and offset-to-GPA bindings.
+The provider's lock()/unlock() callbacks hold mmio_lock across
+invalidation, conversion and publication of attributes. Device unlock takes
+the same mutex when checking for private ranges. Range revocation, memslot
+unbind and final file cleanup use the same order.
+
+PFN lookup during faults takes the inode invalidate lock for read, then
+memory_lock for read, without taking mmio_lock. memory_lock protects the
+power and Memory Enable checks and BAR PFN lookup against PCI state
+restore. Host faults retain the inode invalidate lock through PTE insertion.
+
+VFIO provider setup and release take gmem_lock for write, then
+dev_set->lock, before taking mmio_lock. Both outer locks protect updates to
+gmem_attached. VFIO file operations read this state under gmem_lock for
+read; reset checks can instead use dev_set->lock.
+
+VFIO resumes the device and takes a runtime-PM reference before acquiring
+mmio_lock during provider setup. It drops that reference after removing
+the provider from the vDEVICE during release. Runtime PM callbacks do not
+take the guest_memfd inode invalidate lock.
+
Everything else is a leaf: no other lock is taken inside the critical
sections.
--
2.43.0
prev parent reply other threads:[~2026-11-04 15:06 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 15:02 [RFC PATCH v4 00/25] KVM/VFIO: Add guest_memfd support for device MMIO Aneesh Kumar K.V (Arm)
2026-11-04 15:02 ` [RFC PATCH v4 01/25] KVM: guest_memfd: Let providers prepare memory regions Aneesh Kumar K.V (Arm)
2026-11-04 15:02 ` [RFC PATCH v4 02/25] KVM: guest_memfd: Dispatch conversion through backing operations Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 03/25] KVM: guest_memfd: Let providers coordinate mapping changes Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 04/25] KVM: guest_memfd: Dispatch revocation through backing operations Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 05/25] KVM: guest_memfd: Split population by backing type Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 06/25] KVM: guest_memfd: Support device-backed PFN providers Aneesh Kumar K.V (Arm)
2026-11-04 18:37 ` Fred Griffoul
2026-11-04 15:03 ` [RFC PATCH v4 07/25] KVM: guest_memfd: Support private/shared conversion of device memory Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 08/25] KVM: guest_memfd: Track private device ranges after attribute updates Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 09/25] KVM: guest_memfd: Support range revocation of device mappings Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 10/25] KVM: guest_memfd: Add shared device mmap through PFN providers Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 11/25] KVM: guest_memfd: Clear private device state on final close Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 12/25] KVM: guest_memfd: Invalidate mappings before memslot unbind Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 13/25] vfio: Track device use by guest_memfd Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 14/25] vfio/pci: Add helpers to check and serialize BAR state changes Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 15/25] vfio/pci: Check command enable changes before updating device state Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 16/25] vfio/pci: Check whether BAR state changes are allowed before reset Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 17/25] vfio/pci: Check low-power requests before updating device state Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 18/25] vfio/pci: Track userspace BAR mappings Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 19/25] vfio/pci: Restrict host BAR access for guest_memfd providers Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 20/25] vfio: Dispatch guest_memfd provider operations through the device file Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 21/25] vfio: Serialize guest_memfd attachment with device file operations Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 22/25] vfio/pci: Require D0 before providing guest_memfd BAR backing Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 23/25] iommufd: Support MMIO provider attachment to vDEVICEs Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` [RFC PATCH v4 24/25] vfio/pci: Add a guest_memfd resource provider for MMIO BARs Aneesh Kumar K.V (Arm)
2026-11-04 15:03 ` Aneesh Kumar K.V (Arm) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261104150322.3516320-26-aneesh.kumar@kernel.org \
--to=aneesh.kumar@kernel.org \
--cc=ackerleytng@google.com \
--cc=aik@amd.com \
--cc=alex@shazbot.org \
--cc=catalin.marinas@arm.com \
--cc=david@kernel.org \
--cc=dwmw2@infradead.org \
--cc=griffoul@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=patches@lists.linux.dev \
--cc=pbonzini@redhat.com \
--cc=robin.murphy@arm.com \
--cc=seanjc@google.com \
--cc=steven.price@arm.com \
--cc=suzuki.poulose@arm.com \
--cc=yilun.xu@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox