Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A702C6FC5 for ; Mon, 6 Mar 2023 20:02:20 +0000 (UTC) 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 7954311FB; Mon, 6 Mar 2023 12:03:03 -0800 (PST) Received: from [10.57.79.176] (unknown [10.57.79.176]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D42463F71A; Mon, 6 Mar 2023 12:02:18 -0800 (PST) Message-ID: Date: Mon, 6 Mar 2023 20:02:17 +0000 Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH v2 09/12] KVM: arm64: Support up to 5 levels of translation in kvm_pgtable To: Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Suzuki K Poulose , Ard Biesheuvel , Anshuman Khandual Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev References: <20230306195438.1557851-1-ryan.roberts@arm.com> <20230306195438.1557851-10-ryan.roberts@arm.com> Content-Language: en-US From: Ryan Roberts In-Reply-To: <20230306195438.1557851-10-ryan.roberts@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 06/03/2023 19:54, Ryan Roberts wrote: > FEAT_LPA2 increases the maximum levels of translation from 4 to 5 for > the 4KB page case, when IA is >48 bits. While we can still use 4 levels > for stage2 translation in this case (due to stage2 allowing concatenated > page tables for first level lookup), the same kvm_pgtable library is > used for the hyp stage1 page tables and stage1 does not support > concatenation. > > Therefore, modify the library to support up to 5 levels. Previous > patches already laid the groundwork for this by refactoring code to work > in terms of KVM_PGTABLE_FIRST_LEVEL and KVM_PGTABLE_LAST_LEVEL. So we > just need to change these macros. > > The hardware sometimes encodes the new level differently from the > others: One such place is when reading the level from the FSC field in > the ESR_EL2 register. We never expect to see the lowest level (-1) here > since the stage 2 page tables always use concatenated tables for first > level lookup and therefore only use 4 levels of lookup. So we get away > with just adding a comment to explain why we are not being careful about > decoding level -1. > > For stage2 VTCR_EL2.SL2 is introduced to encode the new start level. > However, since we always use concatenated page tables for first level > look up at stage2 (and therefore we will never need the new extra level) > we never touch this new field. > > Signed-off-by: Ryan Roberts > --- > arch/arm64/include/asm/kvm_emulate.h | 10 ++++++++++ > arch/arm64/include/asm/kvm_pgtable.h | 2 +- > arch/arm64/kvm/hyp/pgtable.c | 10 ++++++++++ > 3 files changed, 21 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index 26666a623fa8..542575aad159 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -405,6 +405,16 @@ static __always_inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vc > > static __always_inline s8 kvm_vcpu_trap_get_fault_level(const struct kvm_vcpu *vcpu) > { > + /* > + * Note: With the introduction of FEAT_LPA2 an extra level of > + * translation (level -1) is added. This level (obviously) doesn't > + * follow the previous convention of encoding the 4 levels in the 2 LSBs > + * of the FSC so this function breaks if the fault is for level -1. > + * > + * However, stage2 tables always use concatenated tables for first level > + * lookup and therefore it is guaranteed that the level will be between > + * 0 and 3, and this function continues to work. > + */ > return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_LEVEL; > } > > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h > index 8c7e65147777..a5d96592c658 100644 > --- a/arch/arm64/include/asm/kvm_pgtable.h > +++ b/arch/arm64/include/asm/kvm_pgtable.h > @@ -11,7 +11,7 @@ > #include > #include > > -#define KVM_PGTABLE_FIRST_LEVEL 0 > +#define KVM_PGTABLE_FIRST_LEVEL -1 > #define KVM_PGTABLE_LAST_LEVEL 3 > > /* > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 939eab245205..a09c17242da7 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -612,6 +612,15 @@ u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift) > lvls = stage2_pgtable_levels(phys_shift); > if (lvls < 2) > lvls = 2; > + > + /* > + * When LPA2 is enabled, the HW supports an extra level of translation > + * (for 5 in total) when using 4K pages. It also introduces VTCR_EL2.SL2 > + * to as an addition to SL0 to enable encoding this extra start level. > + * However, since we always use concatenated pages for the first level > + * lookup, we will never need this extra level and therefore do not need > + * to touch SL2. > + */ > vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls); > > #ifdef CONFIG_ARM64_HW_AFDBM > @@ -1053,6 +1062,7 @@ struct stage2_attr_data { > kvm_pte_t attr_clr; > kvm_pte_t pte; > s8 level; > + struct kvm_pgtable_mm_ops *mm_ops; Naturally, as soon as I sent this out, I noticed this, which clearly should not be here. I suspect it's an artifact from rebasing. Sorry about that, please ignore and I'll fix it in the next version. > }; > > static int stage2_attr_walker(const struct kvm_pgtable_visit_ctx *ctx,