Linux Confidential Computing Development
 help / color / mirror / Atom feed
From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: Fred Griffoul <griffoul@gmail.com>, linux-coco@lists.linux.dev
Cc: 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>,
	iommu@lists.linux.dev, kvm@vger.kernel.org,
	patches@lists.linux.dev
Subject: Re: [RFC PATCH v4 06/25] KVM: guest_memfd: Support device-backed PFN providers
Date: Mon, 09 Nov 2026 15:10:40 +0530	[thread overview]
Message-ID: <yq5amrritk9z.fsf@kernel.org> (raw)
In-Reply-To: <20261104183754.10457-1-griffoul@gmail.com>

Fred Griffoul <griffoul@gmail.com> writes:

> "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org> writes:
>
>> Device resources such as PCI BARs supply PFNs directly and cannot use
>> the folio allocation path. Store the mapping owner and device ID in the
>> core context during attach(), and use them when preparing device
>> memslots. Keep device memory separate from ordinary PFN-backed RAM.
>
> The device rules you wrote down in api.rst and locking.rst helped a
> lot. If I read v4 right, the device backing differs from the RAM PFN
> backing in these places:
>
>   1. PFN mode calls RAM arch hooks: kvm_arch_gmem_make_shared() at
>      conversion, kvm_arch_gmem_make_private() at fault, and
>      kvm_arch_gmem_reclaim() on revoke and release. The device path
>      has to skip them.
>   2. lookup_pfn_range() only feeds those hooks, so the device path
>      does not need it.
>   3. The host mmap and stage-2 need a device memory type.
>   4. In PFN mode, a conversion to private only updates the attributes,
>      and the guest fault maps the page later. CCA has to map device
>      memory at conversion, because the guest validates the mapping
>      before it touches it.
>   5. The device provider needs lock()/unlock(), the private range
>      summary and prepare_memory_region().
>   6. The provider gets the VM and the vDEVICE binding at attach.
>


Your KVM suggestion below prompted me to take a closer look at
eliminating the distinction between kvm_gmem_ram_pfn_backing_ops and
kvm_gmem_device_backing_ops. This appears to be doable.

> Would these changes to PFN mode be enough to drop the device table?
>
>   1. The arch hooks take the frame attributes. For RAM the arch does
>      the RMP work. For MMIO it skips it, or does the RMM work instead.
>

We discussed making get_pfn() and lookup_pfn_range() return PFN
attributes. We could define:

#define GUEST_MEMFD_PFN_READONLY	(1ULL << 0)
#define GUEST_MEMFD_PFN_MMIO	(1ULL << 1)
#define GUEST_MEMFD_PFN_NOEXEC	(1ULL << 2)

We can then use GUEST_MEMFD_PFN_MMIO to drive this. The attribute
conversion then becomes:

kvm_gmem_pfn_convert_attributes() {
          gmem_provider_lookup_pfn_range()
          ret = kvm_arch_gmem_convert(kvm, slot, gfn, pfn, nr_pages,
				    pfn_attrs, attrs);

}

For RAM PFNs, there should be nothing to convert because
lookup_pfn_range() will not find any pfns before they have been faulted-in?

>      
>   2. lookup_pfn_range() stays required. For vfio-pci it is the BAR
>      start plus the offset.
>   3. get_pfn() and lookup_pfn_range() report a memory type (MMIO) in
>      their attributes, next to read only. The core mmap and the KVM
>      fault path use it.
>   4. A conversion calls an arch hook in both directions, and the hook
>      can fail. CCA maps the private device range there.
>   5. lock()/unlock(), the private range summary and
>      prepare_memory_region() become optional ops for any PFN provider.
>      A provider that moves RAM between guests needs them too: it has
>      to order its moves with conversions and leave private pages
>      alone.
>
> I'm less sure about 6. Could KVM ask the provider for the vDEVICE when
> the memslot is created, passing the VM? The provider would then not
> need to keep a VM pointer.
>

We can pass that as an argument. This is also preferable because it
avoids storing a KVM reference without a clearly documented lifetime
rule.

-aneesh

  parent reply	other threads:[~2026-11-09  9:40 UTC|newest]

Thread overview: 31+ 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-09  6:25     ` Aneesh Kumar K.V
2026-11-09 22:20       ` Ackerley Tng
2026-11-10 17:56         ` Aneesh Kumar K.V
2026-11-09  9:40     ` Aneesh Kumar K.V [this message]
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 ` [RFC PATCH v4 25/25] KVM: Document device-backed guest_memfd and VFIO usage Aneesh Kumar K.V (Arm)

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=yq5amrritk9z.fsf@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