From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: Ackerley Tng <ackerleytng@google.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@ziepe.ca>,
"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>,
Will Deacon <will@kernel.org>, 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: Re: [RFC PATCH v2 02/13] KVM: guest_memfd: Support private/shared conversion of device memory
Date: Sun, 01 Nov 2026 12:58:48 +0530 [thread overview]
Message-ID: <yq5a33tl9fin.fsf@kernel.org> (raw)
In-Reply-To: <CAEvNRgFEKqVZcNYCL0LpB6BTyBFe+ot1x1nFfEqOrSTsu134PQ@mail.gmail.com>
Ackerley Tng <ackerleytng@google.com> writes:
> Ackerley Tng <ackerleytng@google.com> writes:
>
>> "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org> writes:
>>
>>>
>>> [...snip...]
>>>
>>> +static int kvm_gmem_device_convert(struct inode *inode, pgoff_t start,
>>> + pgoff_t nr_pages, const struct guest_memfd_device_request *req,
>>> + const struct kvm_memory_slot *slot)
>>> +{
>>> + struct gmem_inode *gi = GMEM_I(inode);
>>> + u64 attrs = req ? KVM_MEMORY_ATTRIBUTE_PRIVATE : 0;
>>> + bool prepared = false;
>>> + int ret;
>>> +
>>> + MA_STATE(mas, &gi->attributes, start, start + nr_pages - 1);
>>> +
>>> + filemap_invalidate_lock(inode->i_mapping);
>>> + if (__kvm_gmem_range_has_attributes(inode, start, nr_pages, attrs)) {
>>> + ret = 0;
>>> + goto out;
>>> + }
>>> +
>>> + if (req && !__kvm_gmem_range_has_attributes(inode, start, nr_pages, 0)) {
>>> + ret = -EEXIST;
>>> + goto out;
>>> + }
>>> +
>>> + ret = kvm_gmem_mas_preallocate(&mas, attrs, start, nr_pages);
>>> + if (ret)
>>> + goto out;
>>> +
>>> + kvm_gmem_invalidate_start(inode, start, start + nr_pages, KVM_FILTER_SHARED);
>>> + if (req && READ_ONCE(gi->device.kvm->vm_dead))
>>> + ret = -EIO;
>>> + else
>>> + ret = gi->provider_ops->prepare_conversion(gi->provider, req);
>>> + if (!ret) {
>>> + prepared = true;
>>> + if (req)
>>> + ret = kvm_gmem_device_make_private(inode, slot, req, nr_pages);
>>
>> Is there a way to avoid making private from within the conversion flow?
>>
>> kvm_gmem_device_map() below already does it as part of vcpu_run.
>>
>> If we do the above, we'd be aligned with regular conversions, where
>> to-shared conversions eagerly update arch state (make_shared) but
>> to-private notifications to the firmware are done lazily.
>>
>
> I was a bit confused above. IIUC to handle
> RMI_EXIT_VDEV_VALIDATE_MAPPING, the conversion is not done from outside
> the kernel, instead it is done in the vcpu_run completion hook.
>
All of that has changed in the latest series.
kvm_complete_vdev_map_validate() now does the following. For the dependent
changes in other subsystems, you may want to fetch the Git branch
mentioned in the cover letter.
static void kvm_complete_vdev_map_validate(struct kvm_vcpu *vcpu)
{
struct kvm *kvm = vcpu->kvm;
struct realm_rec *rec = &vcpu->arch.rec;
struct realm *realm = &kvm->arch.realm;
struct kvm_memory_slot *slot;
unsigned long start = rec->run->exit.dev_mem_base;
unsigned long top = rec->run->exit.dev_mem_top;
unsigned long rd = virt_to_phys(realm->rd);
unsigned long rec_phys = virt_to_phys(rec->rec_page);
int idx, ret;
if (vcpu->run->cca_exit.response != KVM_ARM64_TIO_RESPONSE_APPROVE ||
top <= start || !PAGE_ALIGNED(start | top | rec->run->exit.dev_mem_pa) ||
!kvm_realm_is_private_address(realm, start) ||
!kvm_realm_is_private_address(realm, top - 1))
goto reject;
idx = srcu_read_lock(&kvm->srcu);
slot = gfn_to_memslot(kvm, gpa_to_gfn(start));
if (!slot || (slot->flags & KVM_MEMSLOT_INVALID) ||
!(slot->flags & KVM_MEMSLOT_GMEM_DEVICE) ||
gpa_to_gfn(top - 1) >= slot->base_gfn + slot->npages ||
slot->arch.device.vdev_id != rec->run->exit.vdev_id_1)
goto out_reject;
/* Userspace installs the mapping through guest_memfd before approval. */
if (!kvm_gmem_range_has_attributes(slot, gpa_to_gfn(start),
gpa_to_gfn(top), KVM_MEMORY_ATTRIBUTE_PRIVATE))
goto out_reject;
while (start < top) {
unsigned long next, rmi_ret;
ret = rmi_rtt_dev_validate(rd, rec_phys, start, top, &next, &rmi_ret);
if (ret || rmi_ret || next <= start || next > top)
goto out_reject;
start = next;
}
srcu_read_unlock(&kvm->srcu, idx);
return;
out_reject:
srcu_read_unlock(&kvm->srcu, idx);
reject:
rec->run->enter.flags |= REC_ENTER_FLAG_DEV_MEM_RESPONSE;
}
-aneesh
next prev parent reply other threads:[~2026-11-01 7:28 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-17 7:31 [RFC PATCH v2 00/13] KVM/VFIO: guest_memfd support for device MMIO resources Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 01/13] KVM: guest_memfd: Support PFN-backed resource providers Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 02/13] KVM: guest_memfd: Support private/shared conversion of device memory Aneesh Kumar K.V (Arm)
2026-10-30 20:36 ` Ackerley Tng
2026-10-30 21:19 ` Ackerley Tng
2026-10-30 22:44 ` Ackerley Tng
2026-11-01 7:28 ` Aneesh Kumar K.V [this message]
2026-11-01 7:03 ` Aneesh Kumar K.V
2026-10-17 7:31 ` [RFC PATCH v2 03/13] iommufd: Support MMIO provider attachment to vDEVICEs Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 04/13] KVM: guest_memfd: Add device provider access callbacks Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 05/13] vfio/pci: Track userspace BAR mappings Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 06/13] vfio: Dispatch guest_memfd provider operations through the filesystem Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 07/13] vfio/pci: Restrict host BAR access for guest_memfd providers Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 08/13] vfio/pci: Provide guest_memfd backing for PCI BARs Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 09/13] KVM: guest_memfd: Add a provider detach callback Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 10/13] vfio/pci: Detach guest_memfd access before teardown Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 11/13] KVM: guest_memfd: Remove device mappings on final close Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 12/13] KVM: Invalidate guest_memfd mappings before removing memslot bindings Aneesh Kumar K.V (Arm)
2026-10-17 7:31 ` [RFC PATCH v2 13/13] vfio/pci: Allow VFIO BAR mmap with an attached guest_memfd Aneesh Kumar K.V (Arm)
2026-10-29 14:08 ` [RFC PATCH v2 00/13] KVM/VFIO: guest_memfd support for device MMIO resources Ackerley Tng
2026-10-29 14:44 ` Aneesh Kumar K.V
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=yq5a33tl9fin.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@ziepe.ca \
--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=will@kernel.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.