From nobody Thu Nov 28 05:49:43 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 243DC1C878C; Fri, 4 Oct 2024 15:29:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728055797; cv=none; b=jIayAEkHYvXVmuHbM+mCXB1X8Ej5QqVj2/IV9bPjcKUki+eLx/43ZKh7wIEuSFVpOax1Qd9E0VsmgVfmh/iyyc8m8I1auYh+Ep9o5oRiTAM8Z5XcaiMNh5r9Yt9ODiK0y+2V0z5IK5ooBUYraQhrduD1bI2jkgMgfSzKCXII/wg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728055797; c=relaxed/simple; bh=r4a7OTasquQXUDvBCgxUlHc6bxB2v9JzsPhraI8dV4E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=duzIvi0ikjoMiy88QAK3uyRoNVCDNwEHhaJRgN8DaPfLr9lr65oSnxDt+BsOB6QYrJqNHLx5q/NG4idPonmgtvor7wm7ye73vkijKbIVD7H+Fb1JF1Z72HFDmuJ5lFn/Fgy6mT6dmbibo6AANnD8tQh3tvVhGdU8Dnh2brMdZeI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 36C07150C; Fri, 4 Oct 2024 08:30:23 -0700 (PDT) Received: from e122027.cambridge.arm.com (unknown [10.1.25.25]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 70FC23F640; Fri, 4 Oct 2024 08:29:49 -0700 (PDT) From: Steven Price To: kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: Steven Price , Catalin Marinas , Marc Zyngier , Will Deacon , James Morse , Oliver Upton , Suzuki K Poulose , Zenghui Yu , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Joey Gouly , Alexandru Elisei , Christoffer Dall , Fuad Tabba , linux-coco@lists.linux.dev, Ganapatrao Kulkarni , Gavin Shan , Shanker Donthineni , Alper Gun , "Aneesh Kumar K . V" Subject: [PATCH v5 21/43] arm64: RME: Runtime faulting of memory Date: Fri, 4 Oct 2024 16:27:42 +0100 Message-Id: <20241004152804.72508-22-steven.price@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241004152804.72508-1-steven.price@arm.com> References: <20241004152804.72508-1-steven.price@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" At runtime if the realm guest accesses memory which hasn't yet been mapped then KVM needs to either populate the region or fault the guest. For memory in the lower (protected) region of IPA a fresh page is provided to the RMM which will zero the contents. For memory in the upper (shared) region of IPA, the memory from the memslot is mapped into the realm VM non secure. Signed-off-by: Steven Price --- Changes since v4: * Code cleanup following review feedback. * Drop the PTE_SHARED bit when creating unprotected page table entries. This is now set by the RMM and the host has no control of it and the spec requires the bit to be set to zero. Changes since v2: * Avoid leaking memory if failing to map it in the realm. * Correctly mask RTT based on LPA2 flag (see rtt_get_phys()). * Adapt to changes in previous patches. --- arch/arm64/include/asm/kvm_emulate.h | 10 ++ arch/arm64/include/asm/kvm_rme.h | 10 ++ arch/arm64/kvm/mmu.c | 124 +++++++++++++++- arch/arm64/kvm/rme.c | 205 +++++++++++++++++++++++++-- 4 files changed, 328 insertions(+), 21 deletions(-) diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/= kvm_emulate.h index 7430c77574e3..fa03520d7933 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -710,6 +710,16 @@ static inline bool kvm_realm_is_created(struct kvm *kv= m) return kvm_is_realm(kvm) && kvm_realm_state(kvm) !=3D REALM_STATE_NONE; } =20 +static inline gpa_t kvm_gpa_from_fault(struct kvm *kvm, phys_addr_t fault_= ipa) +{ + if (kvm_is_realm(kvm)) { + struct realm *realm =3D &kvm->arch.realm; + + return fault_ipa & ~BIT(realm->ia_bits - 1); + } + return fault_ipa; +} + static inline bool vcpu_is_rec(struct kvm_vcpu *vcpu) { if (static_branch_unlikely(&kvm_rme_is_available)) diff --git a/arch/arm64/include/asm/kvm_rme.h b/arch/arm64/include/asm/kvm_= rme.h index 889fe120283a..b8e6f8e7a5e5 100644 --- a/arch/arm64/include/asm/kvm_rme.h +++ b/arch/arm64/include/asm/kvm_rme.h @@ -103,6 +103,16 @@ void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size, bool unmap_private); +int realm_map_protected(struct realm *realm, + unsigned long base_ipa, + struct page *dst_page, + unsigned long map_size, + struct kvm_mmu_memory_cache *memcache); +int realm_map_non_secure(struct realm *realm, + unsigned long ipa, + struct page *page, + unsigned long map_size, + struct kvm_mmu_memory_cache *memcache); int realm_set_ipa_state(struct kvm_vcpu *vcpu, unsigned long addr, unsigned long end, unsigned long ripas, diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 23346b1d29cb..1c78738a2645 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -325,8 +325,13 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mm= u, phys_addr_t start, u64 =20 lockdep_assert_held_write(&kvm->mmu_lock); WARN_ON(size & ~PAGE_MASK); - WARN_ON(stage2_apply_range(mmu, start, end, kvm_pgtable_stage2_unmap, - may_block)); + + if (kvm_is_realm(kvm)) + kvm_realm_unmap_range(kvm, start, size, !only_shared); + else + WARN_ON(stage2_apply_range(mmu, start, end, + kvm_pgtable_stage2_unmap, + may_block)); } =20 void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64= size) @@ -345,7 +350,10 @@ static void stage2_flush_memslot(struct kvm *kvm, phys_addr_t addr =3D memslot->base_gfn << PAGE_SHIFT; phys_addr_t end =3D addr + PAGE_SIZE * memslot->npages; =20 - kvm_stage2_flush_range(&kvm->arch.mmu, addr, end); + if (kvm_is_realm(kvm)) + kvm_realm_unmap_range(kvm, addr, end - addr, false); + else + kvm_stage2_flush_range(&kvm->arch.mmu, addr, end); } =20 /** @@ -1037,6 +1045,10 @@ void stage2_unmap_vm(struct kvm *kvm) struct kvm_memory_slot *memslot; int idx, bkt; =20 + /* For realms this is handled by the RMM so nothing to do here */ + if (kvm_is_realm(kvm)) + return; + idx =3D srcu_read_lock(&kvm->srcu); mmap_read_lock(current->mm); write_lock(&kvm->mmu_lock); @@ -1062,6 +1074,7 @@ void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu) if (kvm_is_realm(kvm) && (kvm_realm_state(kvm) !=3D REALM_STATE_DEAD && kvm_realm_state(kvm) !=3D REALM_STATE_NONE)) { + kvm_stage2_unmap_range(mmu, 0, (~0ULL) & PAGE_MASK); write_unlock(&kvm->mmu_lock); kvm_realm_destroy_rtts(kvm, pgt->ia_bits); return; @@ -1428,6 +1441,76 @@ static bool kvm_vma_mte_allowed(struct vm_area_struc= t *vma) return vma->vm_flags & VM_MTE_ALLOWED; } =20 +static int realm_map_ipa(struct kvm *kvm, phys_addr_t ipa, + kvm_pfn_t pfn, unsigned long map_size, + enum kvm_pgtable_prot prot, + struct kvm_mmu_memory_cache *memcache) +{ + struct realm *realm =3D &kvm->arch.realm; + struct page *page =3D pfn_to_page(pfn); + + if (WARN_ON(!(prot & KVM_PGTABLE_PROT_W))) + return -EFAULT; + + if (!realm_is_addr_protected(realm, ipa)) + return realm_map_non_secure(realm, ipa, page, map_size, + memcache); + + return realm_map_protected(realm, ipa, page, map_size, memcache); +} + +static int private_memslot_fault(struct kvm_vcpu *vcpu, + phys_addr_t fault_ipa, + struct kvm_memory_slot *memslot) +{ + struct kvm *kvm =3D vcpu->kvm; + gpa_t gpa =3D kvm_gpa_from_fault(kvm, fault_ipa); + gfn_t gfn =3D gpa >> PAGE_SHIFT; + bool priv_exists =3D kvm_mem_is_private(kvm, gfn); + struct kvm_mmu_memory_cache *memcache =3D &vcpu->arch.mmu_page_cache; + kvm_pfn_t pfn; + int ret; + /* + * For Realms, the shared address is an alias of the private GPA with + * the top bit set. Thus is the fault address matches the GPA then it + * is the private alias. + */ + bool is_priv_gfn =3D (gpa =3D=3D fault_ipa); + + if (priv_exists !=3D is_priv_gfn) { + kvm_prepare_memory_fault_exit(vcpu, + gpa, + PAGE_SIZE, + kvm_is_write_fault(vcpu), + false, is_priv_gfn); + + return -EFAULT; + } + + if (!is_priv_gfn) { + /* Not a private mapping, handling normally */ + return -EINVAL; + } + + ret =3D kvm_mmu_topup_memory_cache(memcache, + kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu)); + if (ret) + return ret; + + ret =3D kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, NULL); + if (ret) + return ret; + + /* FIXME: Should be able to use bigger than PAGE_SIZE mappings */ + ret =3D realm_map_ipa(kvm, fault_ipa, pfn, PAGE_SIZE, KVM_PGTABLE_PROT_W, + memcache); + if (!ret) + return 1; /* Handled */ + + put_page(pfn_to_page(pfn)); + return ret; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva, @@ -1453,6 +1536,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phy= s_addr_t fault_ipa, if (fault_is_perm) fault_granule =3D kvm_vcpu_trap_get_perm_fault_granule(vcpu); write_fault =3D kvm_is_write_fault(vcpu); + + /* + * Realms cannot map protected pages read-only + * FIXME: It should be possible to map unprotected pages read-only + */ + if (vcpu_is_rec(vcpu)) + write_fault =3D true; + exec_fault =3D kvm_vcpu_trap_is_exec_fault(vcpu); VM_BUG_ON(write_fault && exec_fault); =20 @@ -1560,7 +1651,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys= _addr_t fault_ipa, ipa &=3D ~(vma_pagesize - 1); } =20 - gfn =3D ipa >> PAGE_SHIFT; + gfn =3D kvm_gpa_from_fault(kvm, ipa) >> PAGE_SHIFT; mte_allowed =3D kvm_vma_mte_allowed(vma); =20 vfio_allow_any_uc =3D vma->vm_flags & VM_ALLOW_ANY_UNCACHED; @@ -1641,7 +1732,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys= _addr_t fault_ipa, * If we are not forced to use page mapping, check if we are * backed by a THP and thus use block mapping if possible. */ - if (vma_pagesize =3D=3D PAGE_SIZE && !(force_pte || device)) { + /* FIXME: We shouldn't need to disable this for realms */ + if (vma_pagesize =3D=3D PAGE_SIZE && !(force_pte || device || kvm_is_real= m(kvm))) { if (fault_is_perm && fault_granule > PAGE_SIZE) vma_pagesize =3D fault_granule; else @@ -1693,6 +1785,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys= _addr_t fault_ipa, */ prot &=3D ~KVM_NV_GUEST_MAP_SZ; ret =3D kvm_pgtable_stage2_relax_perms(pgt, fault_ipa, prot); + } else if (kvm_is_realm(kvm)) { + ret =3D realm_map_ipa(kvm, fault_ipa, pfn, vma_pagesize, + prot, memcache); } else { ret =3D kvm_pgtable_stage2_map(pgt, fault_ipa, vma_pagesize, __pfn_to_phys(pfn), prot, @@ -1841,8 +1936,15 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) nested =3D &nested_trans; } =20 - gfn =3D ipa >> PAGE_SHIFT; + gfn =3D kvm_gpa_from_fault(vcpu->kvm, ipa) >> PAGE_SHIFT; memslot =3D gfn_to_memslot(vcpu->kvm, gfn); + + if (kvm_slot_can_be_private(memslot)) { + ret =3D private_memslot_fault(vcpu, ipa, memslot); + if (ret !=3D -EINVAL) + goto out; + } + hva =3D gfn_to_hva_memslot_prot(memslot, gfn, &writable); write_fault =3D kvm_is_write_fault(vcpu); if (kvm_is_error_hva(hva) || (write_fault && !writable)) { @@ -1886,7 +1988,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) * of the page size. */ ipa |=3D kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0); - ret =3D io_mem_abort(vcpu, ipa); + ret =3D io_mem_abort(vcpu, kvm_gpa_from_fault(vcpu->kvm, ipa)); goto out_unlock; } =20 @@ -1934,6 +2036,10 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_ran= ge *range) if (!kvm->arch.mmu.pgt) return false; =20 + /* We don't support aging for Realms */ + if (kvm_is_realm(kvm)) + return true; + return kvm_pgtable_stage2_test_clear_young(kvm->arch.mmu.pgt, range->start << PAGE_SHIFT, size, true); @@ -1950,6 +2056,10 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gf= n_range *range) if (!kvm->arch.mmu.pgt) return false; =20 + /* We don't support aging for Realms */ + if (kvm_is_realm(kvm)) + return true; + return kvm_pgtable_stage2_test_clear_young(kvm->arch.mmu.pgt, range->start << PAGE_SHIFT, size, false); diff --git a/arch/arm64/kvm/rme.c b/arch/arm64/kvm/rme.c index b794673b6a5d..f3e809c2087d 100644 --- a/arch/arm64/kvm/rme.c +++ b/arch/arm64/kvm/rme.c @@ -627,6 +627,181 @@ static int fold_rtt(struct realm *realm, unsigned lon= g addr, int level) return 0; } =20 +static phys_addr_t rtt_get_phys(struct realm *realm, struct rtt_entry *rtt) +{ + /* FIXME: For now LPA2 isn't supported in a realm guest */ + bool lpa2 =3D false; + + if (lpa2) + return rtt->desc & GENMASK(49, 12); + return rtt->desc & GENMASK(47, 12); +} + +int realm_map_protected(struct realm *realm, + unsigned long base_ipa, + struct page *dst_page, + unsigned long map_size, + struct kvm_mmu_memory_cache *memcache) +{ + phys_addr_t dst_phys =3D page_to_phys(dst_page); + phys_addr_t rd =3D virt_to_phys(realm->rd); + unsigned long phys =3D dst_phys; + unsigned long ipa =3D base_ipa; + unsigned long size; + int map_level; + int ret =3D 0; + + if (WARN_ON(!IS_ALIGNED(ipa, map_size))) + return -EINVAL; + + switch (map_size) { + case PAGE_SIZE: + map_level =3D 3; + break; + case RME_L2_BLOCK_SIZE: + map_level =3D 2; + break; + default: + return -EINVAL; + } + + if (map_level < RME_RTT_MAX_LEVEL) { + /* + * A temporary RTT is needed during the map, precreate it, + * however if there is an error (e.g. missing parent tables) + * this will be handled below. + */ + realm_create_rtt_levels(realm, ipa, map_level, + RME_RTT_MAX_LEVEL, memcache); + } + + for (size =3D 0; size < map_size; size +=3D PAGE_SIZE) { + if (rmi_granule_delegate(phys)) { + struct rtt_entry rtt; + + /* + * It's possible we raced with another VCPU on the same + * fault. If the entry exists and matches then exit + * early and assume the other VCPU will handle the + * mapping. + */ + if (rmi_rtt_read_entry(rd, ipa, RME_RTT_MAX_LEVEL, &rtt)) + goto err; + + /* + * FIXME: For a block mapping this could race at level + * 2 or 3... currently we don't support block mappings + */ + if (WARN_ON((rtt.walk_level !=3D RME_RTT_MAX_LEVEL || + rtt.state !=3D RMI_ASSIGNED || + rtt_get_phys(realm, &rtt) !=3D phys))) { + goto err; + } + + return 0; + } + + ret =3D rmi_data_create_unknown(rd, phys, ipa); + + if (RMI_RETURN_STATUS(ret) =3D=3D RMI_ERROR_RTT) { + /* Create missing RTTs and retry */ + int level =3D RMI_RETURN_INDEX(ret); + + ret =3D realm_create_rtt_levels(realm, ipa, level, + RME_RTT_MAX_LEVEL, + memcache); + WARN_ON(ret); + if (ret) + goto err_undelegate; + + ret =3D rmi_data_create_unknown(rd, phys, ipa); + } + WARN_ON(ret); + + if (ret) + goto err_undelegate; + + phys +=3D PAGE_SIZE; + ipa +=3D PAGE_SIZE; + } + + if (map_size =3D=3D RME_L2_BLOCK_SIZE) + ret =3D fold_rtt(realm, base_ipa, map_level); + if (WARN_ON(ret)) + goto err; + + return 0; + +err_undelegate: + if (WARN_ON(rmi_granule_undelegate(phys))) { + /* Page can't be returned to NS world so is lost */ + get_page(phys_to_page(phys)); + } +err: + while (size > 0) { + unsigned long data, top; + + phys -=3D PAGE_SIZE; + size -=3D PAGE_SIZE; + ipa -=3D PAGE_SIZE; + + WARN_ON(rmi_data_destroy(rd, ipa, &data, &top)); + + if (WARN_ON(rmi_granule_undelegate(phys))) { + /* Page can't be returned to NS world so is lost */ + get_page(phys_to_page(phys)); + } + } + return -ENXIO; +} + +int realm_map_non_secure(struct realm *realm, + unsigned long ipa, + struct page *page, + unsigned long map_size, + struct kvm_mmu_memory_cache *memcache) +{ + phys_addr_t rd =3D virt_to_phys(realm->rd); + int map_level; + int ret =3D 0; + unsigned long desc =3D page_to_phys(page) | + PTE_S2_MEMATTR(MT_S2_FWB_NORMAL) | + /* FIXME: Read+Write permissions for now */ + (3 << 6); + + if (WARN_ON(!IS_ALIGNED(ipa, map_size))) + return -EINVAL; + + switch (map_size) { + case PAGE_SIZE: + map_level =3D 3; + break; + case RME_L2_BLOCK_SIZE: + map_level =3D 2; + break; + default: + return -EINVAL; + } + + ret =3D rmi_rtt_map_unprotected(rd, ipa, map_level, desc); + + if (RMI_RETURN_STATUS(ret) =3D=3D RMI_ERROR_RTT) { + /* Create missing RTTs and retry */ + int level =3D RMI_RETURN_INDEX(ret); + + ret =3D realm_create_rtt_levels(realm, ipa, level, map_level, + memcache); + if (WARN_ON(ret)) + return -ENXIO; + + ret =3D rmi_rtt_map_unprotected(rd, ipa, map_level, desc); + } + if (WARN_ON(ret)) + return -ENXIO; + + return 0; +} + static int populate_par_region(struct kvm *kvm, phys_addr_t ipa_base, phys_addr_t ipa_end, @@ -638,7 +813,6 @@ static int populate_par_region(struct kvm *kvm, int idx; phys_addr_t ipa; int ret =3D 0; - struct page *tmp_page; unsigned long data_flags =3D 0; =20 base_gfn =3D gpa_to_gfn(ipa_base); @@ -660,9 +834,8 @@ static int populate_par_region(struct kvm *kvm, goto out; } =20 - tmp_page =3D alloc_page(GFP_KERNEL); - if (!tmp_page) { - ret =3D -ENOMEM; + if (!kvm_slot_can_be_private(memslot)) { + ret =3D -EINVAL; goto out; } =20 @@ -729,28 +902,32 @@ static int populate_par_region(struct kvm *kvm, for (offset =3D 0; offset < map_size && !ret; offset +=3D PAGE_SIZE, page++) { phys_addr_t page_ipa =3D ipa + offset; + kvm_pfn_t priv_pfn; + int order; + + ret =3D kvm_gmem_get_pfn(kvm, memslot, + page_ipa >> PAGE_SHIFT, + &priv_pfn, &order); + if (ret) + break; =20 ret =3D realm_create_protected_data_page(realm, page_ipa, - page, tmp_page, - data_flags); + pfn_to_page(priv_pfn), + page, data_flags); } + + kvm_release_pfn_clean(pfn); + if (ret) - goto err_release_pfn; + break; =20 if (level =3D=3D 2) fold_rtt(realm, ipa, level); =20 ipa +=3D map_size; - kvm_release_pfn_dirty(pfn); -err_release_pfn: - if (ret) { - kvm_release_pfn_clean(pfn); - break; - } } =20 mmap_read_unlock(current->mm); - __free_page(tmp_page); =20 out: srcu_read_unlock(&kvm->srcu, idx); --=20 2.34.1