From nobody Thu Nov 28 04:50:05 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3B6321F40A; Fri, 4 Oct 2024 15:29:35 +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=1728055777; cv=none; b=YrQZAfvr72Kpu7qloM8IRLUkX+u5FV98BstE8xxA4P/pf+ThKMJMNTGoVQm3E9weofTtQ2cnZcS8NQ6uyNL81HYhEwOIlT3dJ32+L4i9f1dBkDa5jekHDsaovw+l3wSFTqL2URlFrgovrFCUO9atcAu0FVsUri+ujzvYsNU9FMI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728055777; c=relaxed/simple; bh=hheqrrlgh22yPCUiXmAoozzxYJobSZCmXvfdgA02VYo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R8okT3ujsQDIzpI2MnNXp2VlvAkL5pa51lU26G7VOGwr1rHGykthvlGCtroDBbbGtKI+5qK5i/FXWw3gl2AGVjXYCvEdwBxkgFuAWFQB68ZlTD651tx+HE48krnMVr9v5vVpc4VYo/4plFhtzpu12sJlrjWZtleVLYRd4RamS5s= 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 04382150C; Fri, 4 Oct 2024 08:30:05 -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 5BA8A3F640; Fri, 4 Oct 2024 08:29:31 -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 17/43] arm64: RME: Allow VMM to set RIPAS Date: Fri, 4 Oct 2024 16:27:38 +0100 Message-Id: <20241004152804.72508-18-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" Each page within the protected region of the realm guest can be marked as either RAM or EMPTY. Allow the VMM to control this before the guest has started and provide the equivalent functions to change this (with the guest's approval) at runtime. When transitioning from RIPAS RAM (1) to RIPAS EMPTY (0) the memory is unmapped from the guest and undelegated allowing the memory to be reused by the host. When transitioning to RIPAS RAM the actual population of the leaf RTTs is done later on stage 2 fault, however it may be necessary to allocate additional RTTs to allow the RMM track the RIPAS for the requested range. When freeing a block mapping it is necessary to temporarily unfold the RTT which requires delegating an extra page to the RMM, this page can then be recovered once the contents of the block mapping have been freed. A spare, delegated page (spare_page) is used for this purpose. Signed-off-by: Steven Price --- Changes from v2: * {alloc,free}_delegated_page() moved from previous patch to this one. * alloc_delegated_page() now takes a gfp_t flags parameter. * Fix the reference counting of guestmem pages to avoid leaking memory. * Several misc code improvements and extra comments. --- arch/arm64/include/asm/kvm_rme.h | 17 ++ arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/rme.c | 481 ++++++++++++++++++++++++++++++- 3 files changed, 501 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/kvm_rme.h b/arch/arm64/include/asm/kvm_= rme.h index 3a3aaf5d591c..c064bfb080ad 100644 --- a/arch/arm64/include/asm/kvm_rme.h +++ b/arch/arm64/include/asm/kvm_rme.h @@ -96,6 +96,15 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits= ); int kvm_create_rec(struct kvm_vcpu *vcpu); void kvm_destroy_rec(struct kvm_vcpu *vcpu); =20 +void kvm_realm_unmap_range(struct kvm *kvm, + unsigned long ipa, + u64 size, + bool unmap_private); +int realm_set_ipa_state(struct kvm_vcpu *vcpu, + unsigned long addr, unsigned long end, + unsigned long ripas, + unsigned long *top_ipa); + #define RME_RTT_BLOCK_LEVEL 2 #define RME_RTT_MAX_LEVEL 3 =20 @@ -114,4 +123,12 @@ static inline unsigned long rme_rtt_level_mapsize(int = level) return (1UL << RME_RTT_LEVEL_SHIFT(level)); } =20 +static inline bool realm_is_addr_protected(struct realm *realm, + unsigned long addr) +{ + unsigned int ia_bits =3D realm->ia_bits; + + return !(addr & ~(BIT(ia_bits - 1) - 1)); +} + #endif diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index a26cdac59eb3..23346b1d29cb 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -310,6 +310,7 @@ static void invalidate_icache_guest_page(void *va, size= _t size) * @start: The intermediate physical base address of the range to unmap * @size: The size of the area to unmap * @may_block: Whether or not we are permitted to block + * @only_shared: If true then protected mappings should not be unmapped * * Clear a range of stage-2 mappings, lowering the various ref-counts. Mu= st * be called while holding mmu_lock (unless for freeing the stage2 pgd bef= ore @@ -317,7 +318,7 @@ static void invalidate_icache_guest_page(void *va, size= _t size) * with things behind our backs. */ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start= , u64 size, - bool may_block) + bool may_block, bool only_shared) { struct kvm *kvm =3D kvm_s2_mmu_to_kvm(mmu); phys_addr_t end =3D start + size; @@ -330,7 +331,7 @@ static void __unmap_stage2_range(struct kvm_s2_mmu *mmu= , phys_addr_t start, u64 =20 void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64= size) { - __unmap_stage2_range(mmu, start, size, true); + __unmap_stage2_range(mmu, start, size, true, false); } =20 void kvm_stage2_flush_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys= _addr_t end) @@ -1919,7 +1920,8 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_= gfn_range *range) =20 __unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT, (range->end - range->start) << PAGE_SHIFT, - range->may_block); + range->may_block, + range->only_shared); =20 kvm_nested_s2_unmap(kvm); return false; diff --git a/arch/arm64/kvm/rme.c b/arch/arm64/kvm/rme.c index 6f0ced6e0cc1..1fa9991d708b 100644 --- a/arch/arm64/kvm/rme.c +++ b/arch/arm64/kvm/rme.c @@ -47,9 +47,197 @@ static int rmi_check_version(void) return 0; } =20 -u32 kvm_realm_ipa_limit(void) +static phys_addr_t alloc_delegated_page(struct realm *realm, + struct kvm_mmu_memory_cache *mc, + gfp_t flags) { - return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ); + phys_addr_t phys =3D PHYS_ADDR_MAX; + void *virt; + + if (realm->spare_page !=3D PHYS_ADDR_MAX) { + swap(realm->spare_page, phys); + goto out; + } + + if (mc) + virt =3D kvm_mmu_memory_cache_alloc(mc); + else + virt =3D (void *)__get_free_page(flags); + + if (!virt) + goto out; + + phys =3D virt_to_phys(virt); + + if (rmi_granule_delegate(phys)) { + free_page((unsigned long)virt); + + phys =3D PHYS_ADDR_MAX; + } + +out: + return phys; +} + +static void free_delegated_page(struct realm *realm, phys_addr_t phys) +{ + if (realm->spare_page =3D=3D PHYS_ADDR_MAX) { + realm->spare_page =3D phys; + return; + } + + if (WARN_ON(rmi_granule_undelegate(phys))) { + /* Undelegate failed: leak the page */ + return; + } + + free_page((unsigned long)phys_to_virt(phys)); +} + +static int realm_rtt_create(struct realm *realm, + unsigned long addr, + int level, + phys_addr_t phys) +{ + addr =3D ALIGN_DOWN(addr, rme_rtt_level_mapsize(level - 1)); + return rmi_rtt_create(virt_to_phys(realm->rd), phys, addr, level); +} + +static int realm_rtt_fold(struct realm *realm, + unsigned long addr, + int level, + phys_addr_t *rtt_granule) +{ + unsigned long out_rtt; + int ret; + + ret =3D rmi_rtt_fold(virt_to_phys(realm->rd), addr, level, &out_rtt); + + if (RMI_RETURN_STATUS(ret) =3D=3D RMI_SUCCESS && rtt_granule) + *rtt_granule =3D out_rtt; + + return ret; +} + +static int realm_destroy_protected(struct realm *realm, + unsigned long ipa, + unsigned long *next_addr) +{ + unsigned long rd =3D virt_to_phys(realm->rd); + unsigned long addr; + phys_addr_t rtt; + int ret; + +loop: + ret =3D rmi_data_destroy(rd, ipa, &addr, next_addr); + if (RMI_RETURN_STATUS(ret) =3D=3D RMI_ERROR_RTT) { + if (*next_addr > ipa) + return 0; /* UNASSIGNED */ + rtt =3D alloc_delegated_page(realm, NULL, GFP_KERNEL); + if (WARN_ON(rtt =3D=3D PHYS_ADDR_MAX)) + return -1; + /* + * ASSIGNED - ipa is mapped as a block, so split. The index + * from the return code should be 2 otherwise it appears + * there's a huge page bigger than allowed + */ + WARN_ON(RMI_RETURN_INDEX(ret) !=3D 2); + ret =3D realm_rtt_create(realm, ipa, 3, rtt); + if (WARN_ON(ret)) { + free_delegated_page(realm, rtt); + return -1; + } + /* retry */ + goto loop; + } else if (WARN_ON(ret)) { + return -1; + } + ret =3D rmi_granule_undelegate(addr); + + /* + * If the undelegate fails then something has gone seriously + * wrong: take an extra reference to just leak the page + */ + if (!WARN_ON(ret)) + put_page(phys_to_page(addr)); + + return 0; +} + +static void realm_unmap_range_shared(struct kvm *kvm, + int level, + unsigned long start, + unsigned long end) +{ + struct realm *realm =3D &kvm->arch.realm; + unsigned long rd =3D virt_to_phys(realm->rd); + ssize_t map_size =3D rme_rtt_level_mapsize(level); + unsigned long next_addr, addr; + unsigned long shared_bit =3D BIT(realm->ia_bits - 1); + + if (WARN_ON(level > RME_RTT_MAX_LEVEL)) + return; + + start |=3D shared_bit; + end |=3D shared_bit; + + for (addr =3D start; addr < end; addr =3D next_addr) { + unsigned long align_addr =3D ALIGN(addr, map_size); + int ret; + + next_addr =3D ALIGN(addr + 1, map_size); + + if (align_addr !=3D addr || next_addr > end) { + /* Need to recurse deeper */ + if (addr < align_addr) + next_addr =3D align_addr; + realm_unmap_range_shared(kvm, level + 1, addr, + min(next_addr, end)); + continue; + } + + ret =3D rmi_rtt_unmap_unprotected(rd, addr, level, &next_addr); + switch (RMI_RETURN_STATUS(ret)) { + case RMI_SUCCESS: + break; + case RMI_ERROR_RTT: + if (next_addr =3D=3D addr) { + /* + * There's a mapping here, but it's not a block + * mapping, so reset next_addr to the next block + * boundary and recurse to clear out the pages + * one level deeper. + */ + next_addr =3D ALIGN(addr + 1, map_size); + realm_unmap_range_shared(kvm, level + 1, addr, + next_addr); + } + break; + default: + WARN_ON(1); + return; + } + } +} + +static void realm_unmap_range_private(struct kvm *kvm, + unsigned long start, + unsigned long end) +{ + struct realm *realm =3D &kvm->arch.realm; + ssize_t map_size =3D RME_PAGE_SIZE; + unsigned long next_addr, addr; + + for (addr =3D start; addr < end; addr =3D next_addr) { + int ret; + + next_addr =3D ALIGN(addr + 1, map_size); + + ret =3D realm_destroy_protected(realm, addr, &next_addr); + + if (WARN_ON(ret)) + break; + } } =20 static int get_start_level(struct realm *realm) @@ -57,6 +245,26 @@ static int get_start_level(struct realm *realm) return 4 - stage2_pgtable_levels(realm->ia_bits); } =20 +static void realm_unmap_range(struct kvm *kvm, + unsigned long start, + unsigned long end, + bool unmap_private) +{ + struct realm *realm =3D &kvm->arch.realm; + + if (realm->state =3D=3D REALM_STATE_NONE) + return; + + realm_unmap_range_shared(kvm, get_start_level(realm), start, end); + if (unmap_private) + realm_unmap_range_private(kvm, start, end); +} + +u32 kvm_realm_ipa_limit(void) +{ + return u64_get_bits(rmm_feat_reg0, RMI_FEATURE_REGISTER_0_S2SZ); +} + static int realm_create_rd(struct kvm *kvm) { struct realm *realm =3D &kvm->arch.realm; @@ -140,6 +348,30 @@ static int realm_rtt_destroy(struct realm *realm, unsi= gned long addr, return ret; } =20 +static int realm_create_rtt_levels(struct realm *realm, + unsigned long ipa, + int level, + int max_level, + struct kvm_mmu_memory_cache *mc) +{ + if (WARN_ON(level =3D=3D max_level)) + return 0; + + while (level++ < max_level) { + phys_addr_t rtt =3D alloc_delegated_page(realm, mc, GFP_KERNEL); + + if (rtt =3D=3D PHYS_ADDR_MAX) + return -ENOMEM; + + if (realm_rtt_create(realm, ipa, level, rtt)) { + free_delegated_page(realm, rtt); + return -ENXIO; + } + } + + return 0; +} + static int realm_tear_down_rtt_level(struct realm *realm, int level, unsigned long start, unsigned long end) { @@ -231,6 +463,90 @@ static int realm_tear_down_rtt_range(struct realm *rea= lm, start, end); } =20 +/* + * Returns 0 on successful fold, a negative value on error, a positive val= ue if + * we were not able to fold all tables at this level. + */ +static int realm_fold_rtt_level(struct realm *realm, int level, + unsigned long start, unsigned long end) +{ + int not_folded =3D 0; + ssize_t map_size; + unsigned long addr, next_addr; + + if (WARN_ON(level > RME_RTT_MAX_LEVEL)) + return -EINVAL; + + map_size =3D rme_rtt_level_mapsize(level - 1); + + for (addr =3D start; addr < end; addr =3D next_addr) { + phys_addr_t rtt_granule; + int ret; + unsigned long align_addr =3D ALIGN(addr, map_size); + + next_addr =3D ALIGN(addr + 1, map_size); + + ret =3D realm_rtt_fold(realm, align_addr, level, &rtt_granule); + + switch (RMI_RETURN_STATUS(ret)) { + case RMI_SUCCESS: + if (!WARN_ON(rmi_granule_undelegate(rtt_granule))) + free_page((unsigned long)phys_to_virt(rtt_granule)); + break; + case RMI_ERROR_RTT: + if (level =3D=3D RME_RTT_MAX_LEVEL || + RMI_RETURN_INDEX(ret) < level) { + not_folded++; + break; + } + /* Recurse a level deeper */ + ret =3D realm_fold_rtt_level(realm, + level + 1, + addr, + next_addr); + if (ret < 0) + return ret; + else if (ret =3D=3D 0) + /* Try again at this level */ + next_addr =3D addr; + break; + default: + WARN_ON(1); + return -ENXIO; + } + } + + return not_folded; +} + +static int realm_fold_rtt_range(struct realm *realm, + unsigned long start, unsigned long end) +{ + return realm_fold_rtt_level(realm, get_start_level(realm) + 1, + start, end); +} + +static void ensure_spare_page(struct realm *realm) +{ + phys_addr_t tmp_rtt; + + /* + * Make sure we have a spare delegated page for tearing down the + * block mappings. We do this by allocating then freeing a page. + * We must use Atomic allocations as we are called with kvm->mmu_lock + * held. + */ + tmp_rtt =3D alloc_delegated_page(realm, NULL, GFP_ATOMIC); + + /* + * If the allocation failed, continue as we may not have a block level + * mapping so it may not be fatal, otherwise free it to assign it + * to the spare page. + */ + if (tmp_rtt !=3D PHYS_ADDR_MAX) + free_delegated_page(realm, tmp_rtt); +} + void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_bits) { struct realm *realm =3D &kvm->arch.realm; @@ -238,6 +554,155 @@ void kvm_realm_destroy_rtts(struct kvm *kvm, u32 ia_b= its) WARN_ON(realm_tear_down_rtt_range(realm, 0, (1UL << ia_bits))); } =20 +void kvm_realm_unmap_range(struct kvm *kvm, unsigned long ipa, u64 size, + bool unmap_private) +{ + unsigned long end =3D ipa + size; + struct realm *realm =3D &kvm->arch.realm; + + end =3D min(BIT(realm->ia_bits - 1), end); + + ensure_spare_page(realm); + + realm_unmap_range(kvm, ipa, end, unmap_private); + + if (unmap_private) + realm_fold_rtt_range(realm, ipa, end); +} + +static int find_map_level(struct realm *realm, + unsigned long start, + unsigned long end) +{ + int level =3D RME_RTT_MAX_LEVEL; + + while (level > get_start_level(realm)) { + unsigned long map_size =3D rme_rtt_level_mapsize(level - 1); + + if (!IS_ALIGNED(start, map_size) || + (start + map_size) > end) + break; + + level--; + } + + return level; +} + +int realm_set_ipa_state(struct kvm_vcpu *vcpu, + unsigned long start, + unsigned long end, + unsigned long ripas, + unsigned long *top_ipa) +{ + struct kvm *kvm =3D vcpu->kvm; + struct realm *realm =3D &kvm->arch.realm; + struct realm_rec *rec =3D &vcpu->arch.rec; + phys_addr_t rd_phys =3D virt_to_phys(realm->rd); + phys_addr_t rec_phys =3D virt_to_phys(rec->rec_page); + struct kvm_mmu_memory_cache *memcache =3D &vcpu->arch.mmu_page_cache; + unsigned long ipa =3D start; + int ret =3D 0; + + while (ipa < end) { + unsigned long next; + + ret =3D rmi_rtt_set_ripas(rd_phys, rec_phys, ipa, end, &next); + + if (RMI_RETURN_STATUS(ret) =3D=3D RMI_ERROR_RTT) { + int walk_level =3D RMI_RETURN_INDEX(ret); + int level =3D find_map_level(realm, ipa, end); + + /* + * If the RMM walk ended early then more tables are + * needed to reach the required depth to set the RIPAS. + */ + if (walk_level < level) { + ret =3D realm_create_rtt_levels(realm, ipa, + walk_level, + level, + memcache); + /* Retry with RTTs created */ + if (!ret) + continue; + } else { + ret =3D -EINVAL; + } + + break; + } else if (RMI_RETURN_STATUS(ret) !=3D RMI_SUCCESS) { + WARN(1, "Unexpected error in %s: %#x\n", __func__, + ret); + ret =3D -EINVAL; + break; + } + ipa =3D next; + } + + *top_ipa =3D ipa; + + if (ripas =3D=3D RMI_EMPTY && ipa !=3D start) { + realm_unmap_range_private(kvm, start, ipa); + realm_fold_rtt_range(realm, start, ipa); + } + + return ret; +} + +static int realm_init_ipa_state(struct realm *realm, + unsigned long ipa, + unsigned long end) +{ + phys_addr_t rd_phys =3D virt_to_phys(realm->rd); + int ret; + + while (ipa < end) { + unsigned long next; + + ret =3D rmi_rtt_init_ripas(rd_phys, ipa, end, &next); + + if (RMI_RETURN_STATUS(ret) =3D=3D RMI_ERROR_RTT) { + int err_level =3D RMI_RETURN_INDEX(ret); + int level =3D find_map_level(realm, ipa, end); + + if (WARN_ON(err_level >=3D level)) + return -ENXIO; + + ret =3D realm_create_rtt_levels(realm, ipa, + err_level, + level, NULL); + if (ret) + return ret; + /* Retry with the RTT levels in place */ + continue; + } else if (WARN_ON(ret)) { + return -ENXIO; + } + + ipa =3D next; + } + + return 0; +} + +static int kvm_init_ipa_range_realm(struct kvm *kvm, + struct kvm_cap_arm_rme_init_ipa_args *args) +{ + gpa_t addr, end; + struct realm *realm =3D &kvm->arch.realm; + + addr =3D args->init_ipa_base; + end =3D addr + args->init_ipa_size; + + if (end < addr) + return -EINVAL; + + if (kvm_realm_state(kvm) !=3D REALM_STATE_NEW) + return -EINVAL; + + return realm_init_ipa_state(realm, addr, end); +} + /* Protects access to rme_vmid_bitmap */ static DEFINE_SPINLOCK(rme_vmid_lock); static unsigned long *rme_vmid_bitmap; @@ -363,6 +828,18 @@ int kvm_realm_enable_cap(struct kvm *kvm, struct kvm_e= nable_cap *cap) case KVM_CAP_ARM_RME_CREATE_RD: r =3D kvm_create_realm(kvm); break; + case KVM_CAP_ARM_RME_INIT_IPA_REALM: { + struct kvm_cap_arm_rme_init_ipa_args args; + void __user *argp =3D u64_to_user_ptr(cap->args[1]); + + if (copy_from_user(&args, argp, sizeof(args))) { + r =3D -EFAULT; + break; + } + + r =3D kvm_init_ipa_range_realm(kvm, &args); + break; + } default: r =3D -EINVAL; break; --=20 2.34.1