From: Suzuki K Poulose <suzuki.poulose@arm.com>
To: Steven Price <steven.price@arm.com>,
Wei-Lin Chang <weilin.chang@arm.com>,
kvm@vger.kernel.org, kvmarm@lists.linux.dev
Cc: Catalin Marinas <catalin.marinas@arm.com>,
Marc Zyngier <maz@kernel.org>, Will Deacon <will@kernel.org>,
James Morse <james.morse@arm.com>,
Oliver Upton <oliver.upton@linux.dev>,
Zenghui Yu <yuzenghui@huawei.com>,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, Joey Gouly <joey.gouly@arm.com>,
Alexandru Elisei <alexandru.elisei@arm.com>,
Christoffer Dall <christoffer.dall@arm.com>,
Fuad Tabba <tabba@google.com>,
linux-coco@lists.linux.dev,
Ganapatrao Kulkarni <gankulkarni@os.amperecomputing.com>,
Gavin Shan <gshan@redhat.com>,
Shanker Donthineni <sdonthineni@nvidia.com>,
Alper Gun <alpergun@google.com>,
"Aneesh Kumar K . V" <aneesh.kumar@kernel.org>,
Emi Kisanuki <fj0570is@fujitsu.com>,
Vishal Annapurve <vannapurve@google.com>,
Lorenzo Pieralisi <lpieralisi@kernel.org>,
Kohei Enju <enju.kohei@fujitsu.com>
Subject: Re: [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory
Date: Fri, 20 Nov 2026 13:18:08 +0000 [thread overview]
Message-ID: <b21faaee-921d-40bc-b084-1cb77538dd07@arm.com> (raw)
In-Reply-To: <bf4da8b7-0714-4253-9841-cf83cb65b92d@arm.com>
On 19/11/2026 17:01, Steven Price wrote:
> On 19/11/2026 07:48, Wei-Lin Chang wrote:
>> Hi Steven,
>>
>> On Mon, Nov 16, 2026 at 04:43:55PM +0000, Steven Price wrote:
>>
>> [...]
>>
>>> @@ -1791,12 +1799,18 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
>>> void *memcache = NULL;
>>> gfn_t canonical_gfn;
>>> int ret;
>>> + bool is_private = false;
>>>
>>> - if (!perm_fault) {
>>> + if (KVM_BUG_ON(!kvm_slot_has_gmem(s2fd->memslot), kvm))
>>> + return -EINVAL;
>>
>> I feel this is being too cautious, gmem_abort is always run after a
>> kvm_slot_has_gmem() check.
>
> Fair point - I'll drop that.
>
>>> +
>>> + if (!perm_fault || kvm_arch_has_private_mem(kvm)) {
>>> memcache = get_mmu_memcache(s2fd->vcpu);
>>> ret = topup_mmu_memcache(s2fd->mmu, memcache);
>>> if (ret)
>>> return ret;
>>> + }
>>> + if (!perm_fault) {
>>> if (kvm_is_nested_s2_mmu(kvm, pgt->mmu)) {
>>> mapping = kmalloc_obj(struct kvm_guest_s2_mapping, GFP_KERNEL_ACCOUNT);
>>> if (!mapping)
>>> @@ -1805,7 +1819,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
>>> }
>>>
>>> s2vi.vma_pagesize = PAGE_SIZE;
>>> - s2vi.gfn = ALIGN_DOWN(s2fd->fault_ipa, s2vi.vma_pagesize) >> PAGE_SHIFT;
>>> + s2vi.gfn = gpa_to_gfn(kvm_gpa_from_fault(kvm, s2fd->fault_ipa));
>>> canonical_gfn = get_canonical_gfn(s2fd, &s2vi);
>>>
>>> write_fault = kvm_s2_fault_is_write(s2fd);
>>> @@ -1817,13 +1831,35 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
>>> /* Pairs with the smp_wmb() in kvm_mmu_invalidate_end(). */
>>> smp_rmb();
>>>
>>> + if (kvm_arch_has_private_mem(kvm)) {
>>> + switch (kvm->arch.vm_flavor) {
>>> + case VM_REALM:
>>> + is_private = kvm_realm_is_private_address(&kvm->arch.realm,
>>> + s2fd->fault_ipa);
>>> + break;
>>> + default:
>>> + break;
>>> + }
>>> +
>>> + if (is_private != kvm_gmem_is_private_gfn(kvm, canonical_gfn)) {
>>> + if (!result)
>>> + kvm_prepare_memory_fault_exit(s2fd->vcpu,
>>> + gfn_to_gpa(canonical_gfn),
>>> + PAGE_SIZE,
>>> + write_fault, exec_fault,
>>> + is_private);
>>> + kfree(mapping);
>>> + return -EFAULT;
>>> + }
>>> + }
>>> +
>>> ret = kvm_gmem_get_pfn(kvm, s2fd->memslot, canonical_gfn, &s2vi.pfn, NULL);
>>> if (ret) {
>>> /* If result is non-NULL this is a synthetic fault. */
>>> if (!result)
>>> kvm_prepare_memory_fault_exit(s2fd->vcpu, gfn_to_gpa(canonical_gfn),
>>> s2vi.vma_pagesize, write_fault, exec_fault,
>>> - false);
>>> + is_private);
>>> kfree(mapping);
>>> return ret;
>>> }
>>> @@ -1846,6 +1882,16 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
>>> goto out_unlock;
>>> }
>>>
>>> + switch (kvm->arch.vm_flavor) {
>>> + case VM_REALM:
>>> + prot &= ~KVM_PGTABLE_PROT_X;
>>> + ret = realm_map_ipa(kvm, s2fd->fault_ipa, s2vi.pfn,
>>> + PAGE_SIZE, prot, memcache);
>>> + goto out_unlock;
>>> + default:
>>> + break;
>>> + }
>>> +
>>> if (perm_fault) {
>>> /*
>>> * Drop the SW bits in favour of those stored in the
>>> @@ -2047,7 +2093,8 @@ static int kvm_s2_fault_get_vma_info(const struct kvm_s2_fault_desc *s2fd,
>>> * mapping size to ensure we find the right PFN and lay down the
>>> * mapping in the right place.
>>> */
>>> - s2vi->gfn = ALIGN_DOWN(s2fd->fault_ipa, s2vi->vma_pagesize) >> PAGE_SHIFT;
>>> + s2vi->gfn = kvm_gfn_from_fault(kvm, ALIGN_DOWN(s2fd->fault_ipa,
>>> + s2vi->vma_pagesize));
>>>
>>> s2vi->mte_allowed = kvm_vma_mte_allowed(vma);
>>>
>>> @@ -2227,7 +2274,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
>>> * possible.
>>> */
>>> if (mapping_size == PAGE_SIZE && s2vi->max_map_size != PAGE_SIZE) {
>>> - if (perm_fault_granule > PAGE_SIZE) {
>>> + if (perm_fault_granule > PAGE_SIZE && !kvm_vm_is_realm(kvm)) {
>>> mapping_size = perm_fault_granule;
>>> } else {
>>> mapping_size = transparent_hugepage_adjust(s2fd, &pfn, &gfn);
>>> @@ -2242,12 +2289,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
>>> if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
>>> sanitise_mte_tags(kvm, pfn, mapping_size);
>>>
>>> - /*
>>> - * Under the premise of getting a FSC_PERM fault, we just need to relax
>>> - * permissions only if mapping_size equals perm_fault_granule. Otherwise,
>>> - * kvm_pgtable_stage2_map() should be called to change block size.
>>> - */
>>> - if (mapping_size == perm_fault_granule) {
>>> + if (kvm_vm_is_realm(kvm)) {
>>> + ret = realm_map_ipa(kvm, s2fd->fault_ipa, pfn, mapping_size,
>>> + prot, memcache);
>>> + } else if (mapping_size == perm_fault_granule) {
>>> + /*
>>> + * Under the premise of getting a FSC_PERM fault, we just need to relax
>>> + * permissions only if mapping_size equals perm_fault_granule. Otherwise,
>>> + * kvm_pgtable_stage2_map() should be called to change block size.
>>> + */
>>> /*
>>> * Drop the SW bits in favour of those stored in the
>>> * PTE, which will be preserved.
>>> @@ -2306,15 +2356,16 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
>>> /*
>>> * Permission faults just need to update the existing leaf entry,
>>> * and so normally don't require allocations from the memcache. The
>>> - * only exception to this is when dirty logging is enabled at runtime
>>> + * exceptions are when dirty logging is enabled at runtime
>>> * and a fault needs to collapse a block entry into a table.
>>> + * Realms can also require new RTT levels when replacing a mapping.
>>> * Under pKVM a permission fault can also collapse pages into a block,
>>> * which needs a fresh mapping object, and the hypervisor requires the
>>> * min-pages memcache even when the install allocates nothing.
>>> */
>>> memcache = get_mmu_memcache(s2fd->vcpu);
>>> if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
>>> - is_protected_kvm_enabled()) {
>>> + is_protected_kvm_enabled() || kvm_vm_is_realm(s2fd->vcpu->kvm)) {
>>> ret = topup_mmu_memcache(s2fd->mmu, memcache);
>>> if (ret)
>>> return ret;
>>> @@ -2340,12 +2391,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
>>> return kvm_s2_fault_map(s2fd, &s2vi, prot, memcache, result);
>>> }
>>
>> I see that the changes to gmem_abort() and kvm_s2_fault_map() are more
>> significant, and can possibly be turned into its realm version leaving
>> the original unchanged. With this can we give realm_vm_s2_ops its own
>> .vm_mem_abort? Something like:
>>
>> First, make user_mem_abort() take a function pointer, call it at the
>> current kvm_s2_fault_map() place.
>>
>> Then realm_vm_s2_ops.vm_mem_abort = realm_mem_abort()
>>
>> And in realm_mem_abort() do:
>>
>> realm_mem_abort()
>> if gmem slot: realm_gmem_abort()
>> if regular slot: user_mem_abort(kvm_realm_fault_map)
>>
>> realm_gmem_abort() and kvm_realm_fault_map() are just the realm version
>> of gmem_abort() and kvm_s2_fault_map(). For example they won't need NV
>> related code, and vm_flavor switch case.
>>
>> Proposing this because I think we would like the CCA changes more
>> self-contained.
>
> It's a little marginal - there's quite a bit of common code between the
> realm path and the normal path. But it does have the advantage that you
> mention of making CCA more self contained. So it's probably worth it.
The only odd bit would be handling the permission fault separately for
Realms ?
Suzuki
>
> Thanks,
> Steve
>
>> Thanks,
>> Wei-Lin Chang
>>
>>>
>>> +static bool private_ipa_fault(struct kvm *kvm, phys_addr_t fault_ipa)
>>> +{
>>> + return kvm_vm_is_realm(kvm) &&
>>> + kvm_realm_is_private_address(&kvm->arch.realm, fault_ipa);
>>> +}
>>> +
>>> static int kvm_vm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
>>> {
>>> VM_WARN_ON_ONCE(kvm_s2_fault_is_perm(s2fd) &&
>>> !esr_abt_is_write_fault(s2fd->esr) &&
>>> !kvm_s2_fault_is_exec(s2fd));
>>>
>>> + if (private_ipa_fault(s2fd->vcpu->kvm, s2fd->fault_ipa) &&
>>> + !kvm_slot_has_gmem(s2fd->memslot))
>>> + return -EINVAL;
>>> +
>>> if (kvm_slot_has_gmem(s2fd->memslot))
>>> return gmem_abort(s2fd, NULL);
>>> else
>>> @@ -2458,10 +2519,10 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
>>> */
>>> int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
>>> {
>>> + struct kvm *kvm = vcpu->kvm;
>>> struct kvm_s2_trans nested_trans, *nested = NULL;
>>> unsigned long esr = kvm_vcpu_get_esr(vcpu);
>>> struct kvm_s2_mmu *mmu = vcpu->arch.hw_mmu;
>>> - struct kvm *kvm = vcpu->kvm;
>>> phys_addr_t fault_ipa; /* The address we faulted on */
>>> phys_addr_t ipa; /* Always the IPA in the L1 guest phys space */
>>> struct kvm_memory_slot *memslot;
>>> @@ -2554,8 +2615,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
>>> nested = &nested_trans;
>>> }
>>>
>>> - gfn = ipa >> PAGE_SHIFT;
>>> + gfn = kvm_gfn_from_fault(kvm, ipa);
>>> memslot = gfn_to_memslot(kvm, gfn);
>>> +
>>> hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
>>> write_fault = esr_abt_is_write_fault(esr);
>>> if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
>>> @@ -2598,7 +2660,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
>>> * of the page size.
>>> */
>>> ipa |= FAR_TO_FIPA_OFFSET(kvm_vcpu_get_hfar(vcpu));
>>> - ret = io_mem_abort(vcpu, ipa);
>>> + ret = io_mem_abort(vcpu, kvm_gpa_from_fault(kvm, ipa));
>>> goto out_unlock;
>>> }
>>>
>>
>> [...]
>
next prev parent reply other threads:[~2026-11-20 13:18 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 16:43 [PATCH v3 00/11] KVM: arm64: CCA: Prepare Realm memory management Steven Price
2026-11-16 16:43 ` [PATCH v3 01/11] KVM: arm64: guest_memfd: Check mmap support when preparing memslot Steven Price
2026-11-17 14:05 ` Fuad Tabba
2026-11-18 10:34 ` Suzuki K Poulose
2026-11-18 11:14 ` Steven Price
2026-11-18 12:04 ` Suzuki K Poulose
2026-11-18 15:18 ` Steven Price
2026-11-19 10:10 ` Suzuki K Poulose
2026-11-19 14:54 ` Sean Christopherson
2026-11-16 16:43 ` [PATCH v3 02/11] KVM: arm64: CCA: Introduce Realms Steven Price
2026-11-17 14:07 ` Fuad Tabba
2026-11-18 16:12 ` Steven Price
2026-11-16 16:43 ` [PATCH v3 03/11] KVM: arm64: Handle attributes in stage2_unmap_range Steven Price
2026-11-17 14:16 ` Fuad Tabba
2026-11-18 16:12 ` Steven Price
2026-11-16 16:43 ` [PATCH v3 04/11] KVM: arm64: CCA: Add basic infrastructure for creating a realm Steven Price
2026-11-16 16:43 ` [PATCH v3 05/11] KVM: arm64: CCA: Tear down RTTs Steven Price
2026-11-19 3:39 ` Wei-Lin Chang
2026-11-19 14:27 ` Steven Price
2026-11-16 16:43 ` [PATCH v3 06/11] KVM: arm64: CCA: Add Realm RTT mapping and invalidation helpers Steven Price
2026-11-16 16:43 ` [PATCH v3 07/11] KVM: Allow architectures to reject memslot changes before invalidation Steven Price
2026-11-17 14:31 ` Fuad Tabba
2026-11-18 16:44 ` Steven Price
2026-11-16 16:43 ` [PATCH v3 08/11] KVM: arm64: CCA: Prevent memslot modifications after Realm creation Steven Price
2026-11-17 14:42 ` Fuad Tabba
2026-11-18 16:49 ` Steven Price
2026-11-16 16:43 ` [PATCH v3 09/11] KVM: arm64: CCA: Support runtime faulting of memory Steven Price
2026-11-19 7:48 ` Wei-Lin Chang
2026-11-19 17:01 ` Steven Price
2026-11-20 13:18 ` Suzuki K Poulose [this message]
2026-11-16 16:43 ` [PATCH v3 10/11] KVM: arm64: Dispatch stage-2 destruction by VM flavor Steven Price
2026-11-17 15:02 ` Fuad Tabba
2026-11-18 17:04 ` Steven Price
2026-11-16 16:43 ` [PATCH v3 11/11] KVM: arm64: CCA: Add Realm stage-2 MMU operations Steven Price
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=b21faaee-921d-40bc-b084-1cb77538dd07@arm.com \
--to=suzuki.poulose@arm.com \
--cc=alexandru.elisei@arm.com \
--cc=alpergun@google.com \
--cc=aneesh.kumar@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=christoffer.dall@arm.com \
--cc=enju.kohei@fujitsu.com \
--cc=fj0570is@fujitsu.com \
--cc=gankulkarni@os.amperecomputing.com \
--cc=gshan@redhat.com \
--cc=james.morse@arm.com \
--cc=joey.gouly@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=lpieralisi@kernel.org \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=sdonthineni@nvidia.com \
--cc=steven.price@arm.com \
--cc=tabba@google.com \
--cc=vannapurve@google.com \
--cc=weilin.chang@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.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
all inboxes | Powered by JetHome®