Linux Confidential Computing Development
 help / color / mirror / Atom feed
From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: Ackerley Tng <ackerleytng@google.com>,
	Fred Griffoul <griffoul@gmail.com>,
	linux-coco@lists.linux.dev
Cc: 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: Tue, 10 Nov 2026 23:26:13 +0530	[thread overview]
Message-ID: <yq5acxsctvsy.fsf@kernel.org> (raw)
In-Reply-To: <CAEvNRgFE5=u8Q+fCUxLT7M_gAjkgNddSVrM3axuTUxBScffcbQ@mail.gmail.com>

Ackerley Tng <ackerleytng@google.com> writes:

> Aneesh Kumar K.V <aneesh.kumar@kernel.org> writes:
>
>> 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.
>>>
>>
>> I would say that the VFIO provider expects the conversion to complete at
>> conversion time, rather than being triggered by a fault.
>
> guest_memfd currently does .gmem_make_shared() eagerly and
> .gmem_make_private() lazily (at fault time).
>
> Technically conversion time spans everything from guest exit to request
> a conversion, including the guest_memfd conversion ioctl, then till the
> vCPU run completion hook.
>
> Can we move the mapping out of the conversion ioctl and into some
> dedicated CCA-specific mapping ioctl, kind of like KVM_ARM_RMI_POPULATE
> but doesn't actually perform writes, just does mapping?
>
> The mapping ioctl should check the necessary prereqs, same for the
> vcpu_run completion hook.
>
> Here are some advantages I can think of:
>
> 1. Decouples mapping of VFIO addresses from guest_memfd and provide more
>    understandable errors for userspace, since the mapping step has a
>    clear and separate error (conversion is complicated enough with many
>    failure points)
>
> 2. Better aligned conversions in general where marking private at the
>    platform level is lazily performed. In this case, doing it outside of
>    the conversions ioctl is not much lazier, but slightly :)
>
>    Not quite mapping, but Suzuki did bring up that for regular memory
>    (not VFIO), ARM CCA requires vCPU context for SMC_RMI_RTT_SET_RIPAS
>    in .gmem_make_private(), hence that's done in the vcpu_run completion
>    hook, so that's kind of lazier, relative to doing
>    SMC_RMI_RTT_SET_RIPAS in the conversion ioctl.
>


Is there a reason why setting private attributes must not create new KVM
mappings? Is it because a guest_memfd can be bound to multiple VMs, and a
set_memory_attributes() operation should not affect the stage-2 mappings of
other VMs?

But yes, we can move the mapping to a separate operation. I am trying to
make device memory behave more like normal guest memory in this respect.
With VFIO passthrough, normal guest memory requires a private mapping
before the guest accesses it because a device can start DMA to the region
without any prior guest access. I currently use KVM's prefault path for
this and can extend it to handle device MMIO regions.

For an MMIO validation request, VMM would set the guest_memfd range's
memory attributes to private, prefault the protected IPA range and then
approve the request. Prefaulting would perform DEV_MAP.

This means that we need to track private device mappings as part of the
Realm. MMIO validation requires the device PFNs to be mapped already;
that work cannot be deferred until a subsequent guest fault. Before
issuing DEV_VALIDATE, the MMIO validation completion path would need to
check both the Realm's private mapping state and the guest_memfd memory
attributes.


>
>
>> This is also
>> required because, when an MMIO validation request completes, the guest
>> expects the range to be mapped. The current flow is that the MMIO
>> validation request exits to the VMM, which uses set_memory_attributes()
>> to convert the range to private.
>>
>>>   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.
>>>
>>
>>
>> Do you mean this code? vm_page_prot is used for the host mapping created
>> by mmap() on the guest_memfd, and its setup follows
>> vfio_pci_core_mmap(). I think the only device-specific difference here
>> should be pgprot_noncached(). Shouldn't the rest of the protection
>> remain unchanged even for device PFNs?
>>
>>
>> +	if (gmem_provider_is_device(gi)) {
>> +		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
>> +		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
>> +		vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
>> +		vma->vm_ops = &kvm_gmem_device_vm_ops;
>> +		return 0;
>> +	}
>> +
>>
>
> What do you think of just forwarding .mmap to VFIO to figure out? This
> makes VFIO less special of a provider, since I imagine different
> providers may want to have different page protections or VMA flags.
>
> guest_memfd just enforces VM_PFNMAP for PFN-mode providers.
>

I have reworked the patch set to drop backing_ops and consolidate
everything under provider ops. The generic guest_memfd path now contains
only provider callbacks; the folio, shmem, RAM PFN, and device PFN
differences are handled by those callbacks.

We might still want the generic code to apply:

	pgprot_noncached(vma->vm_page_prot)

These prot bits are exposed to VMM mappings, and if the attribute is not
reflected in vma->vm_page_prot, mprotect() may set the PTE protection bits
incorrectly.

I will send a v5 of the reworked patch series soon.

-aneesh

  reply	other threads:[~2026-11-10 17:56 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 [this message]
2026-11-09  9:40     ` Aneesh Kumar K.V
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=yq5acxsctvsy.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