Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4220234pxu; Mon, 30 Nov 2020 22:22:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwQrVKnTpHlaNHDbgepyn+NTeCJOe1EPzti9K9fTLkbl7WGGspxHDajPZct2YnWe/2Ynm3D X-Received: by 2002:a50:a40f:: with SMTP id u15mr1466228edb.307.1606803743662; Mon, 30 Nov 2020 22:22:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606803743; cv=none; d=google.com; s=arc-20160816; b=TN2mgFD2BILoCeH9PuBkJH+Kn0CsEGO93S0T/7TSp+lRfpgMWgLehgMxl0/beW5gVQ Ng/iwdRQCQIx60XShsHxCd7vg5nS+URb6dwQ67Fxk5/J3DNoZPWxq9GfD2EkjIWoUz8u P39WxhagAX34Mkf8NZvBMJI+rjdGnkIt/BMO/x3bFjitHWEAF4zrmV5JcvCyRJ5gh4i6 fmAQdj518FInelyV16wZ76cYf2BBtubnkQHcuv9cIG+J4hCIVTAM69x35eImksFT/ZC/ PDQzKKE6sjJzJ5ucisnACQWFPGx/lffOgu9cE8Sm5caIjwOGRogVAtqiFKi9DK0jRbgL uhQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=bRBDbudiu1w/mLWyU4iXReuRiths0bQ+QtoQQH8tOMA=; b=yD7gTJH5w/9JBEZLnYGCMyySPiZUdoHmiqmrdEqbPfSqUGOc/S/Y1UMaSYqNXlaqJH FSGQ9PvVSgHmZkDqsZUGL+KubmBftkLyeb80a20f5MWHAwWpumvukncRZJTMrOwHvYWJ uZIt9ZE6NrHG7ej7K90SNbOqY+q8QFQtY/j+nswZ1fICe18BBKtNWGJG+91xKzDuzUXS 5y/hRu654z6O734Nut9dR8eWjBGCDytt3m69d4OusL4ooeV8hhs6F8Ikhow56D9j0lN4 7r3of9QeVlJklEJoM9ohp39xpkLnWERK7G0VBiW8m3QdhADSrps6HKj76ITNpXIT92Ha DU2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v3si514142edt.166.2020.11.30.22.22.00; Mon, 30 Nov 2020 22:22:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726684AbgLAGFG (ORCPT + 99 others); Tue, 1 Dec 2020 01:05:06 -0500 Received: from szxga03-in.huawei.com ([45.249.212.189]:2386 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726064AbgLAGFF (ORCPT ); Tue, 1 Dec 2020 01:05:05 -0500 Received: from dggeme768-chm.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4ClWkW0TyDz4yHX; Tue, 1 Dec 2020 14:03:47 +0800 (CST) Received: from [10.174.186.123] (10.174.186.123) by dggeme768-chm.china.huawei.com (10.3.19.114) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Tue, 1 Dec 2020 14:04:18 +0800 Subject: Re: [RFC PATCH 3/3] KVM: arm64: Add usage of stage 2 fault lookup level in user_mem_abort() To: Will Deacon CC: , , Marc Zyngier , Catalin Marinas , James Morse , Julien Thierry , Suzuki K Poulose , Gavin Shan , Quentin Perret , , , , , , , References: <20201130121847.91808-1-wangyanan55@huawei.com> <20201130121847.91808-4-wangyanan55@huawei.com> <20201130134913.GC24837@willie-the-truck> From: "wangyanan (Y)" Message-ID: Date: Tue, 1 Dec 2020 14:04:18 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <20201130134913.GC24837@willie-the-truck> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.174.186.123] X-ClientProxiedBy: dggeme716-chm.china.huawei.com (10.1.199.112) To dggeme768-chm.china.huawei.com (10.3.19.114) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Will, On 2020/11/30 21:49, Will Deacon wrote: > On Mon, Nov 30, 2020 at 08:18:47PM +0800, Yanan Wang wrote: >> If we get a FSC_PERM fault, just using (logging_active && writable) to determine >> calling kvm_pgtable_stage2_map(). There will be two more cases we should consider. >> >> (1) After logging_active is configged back to false from true. When we get a >> FSC_PERM fault with write_fault and adjustment of hugepage is needed, we should >> merge tables back to a block entry. This case is ignored by still calling >> kvm_pgtable_stage2_relax_perms(), which will lead to an endless loop and guest >> panic due to soft lockup. >> >> (2) We use (FSC_PERM && logging_active && writable) to determine collapsing >> a block entry into a table by calling kvm_pgtable_stage2_map(). But sometimes >> we may only need to relax permissions when trying to write to a page other than >> a block. In this condition, using kvm_pgtable_stage2_relax_perms() will be fine. >> >> The ISS filed bit[1:0] in ESR_EL2 regesiter indicates the stage2 lookup level >> at which a D-abort or I-abort occured. By comparing granule of the fault lookup >> level with vma_pagesize, we can strictly distinguish conditions of calling >> kvm_pgtable_stage2_relax_perms() or kvm_pgtable_stage2_map(), and the above >> two cases will be well considered. >> >> Suggested-by: Keqian Zhu >> Signed-off-by: Yanan Wang >> --- >> arch/arm64/include/asm/esr.h | 1 + >> arch/arm64/include/asm/kvm_emulate.h | 5 +++++ >> arch/arm64/kvm/mmu.c | 11 +++++++++-- >> 3 files changed, 15 insertions(+), 2 deletions(-) >> >> diff --git a/arch/arm64/include/asm/esr.h b/arch/arm64/include/asm/esr.h >> index 22c81f1edda2..85a3e49f92f4 100644 >> --- a/arch/arm64/include/asm/esr.h >> +++ b/arch/arm64/include/asm/esr.h >> @@ -104,6 +104,7 @@ >> /* Shared ISS fault status code(IFSC/DFSC) for Data/Instruction aborts */ >> #define ESR_ELx_FSC (0x3F) >> #define ESR_ELx_FSC_TYPE (0x3C) >> +#define ESR_ELx_FSC_LEVEL (0x03) >> #define ESR_ELx_FSC_EXTABT (0x10) >> #define ESR_ELx_FSC_SERROR (0x11) >> #define ESR_ELx_FSC_ACCESS (0x08) >> diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h >> index 5ef2669ccd6c..2e0e8edf6306 100644 >> --- a/arch/arm64/include/asm/kvm_emulate.h >> +++ b/arch/arm64/include/asm/kvm_emulate.h >> @@ -350,6 +350,11 @@ static __always_inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vc >> return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_TYPE; >> } >> >> +static __always_inline u8 kvm_vcpu_trap_get_fault_level(const struct kvm_vcpu *vcpu) >> +{ >> + return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_LEVEL; >> +{ >> + >> static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) >> { >> switch (kvm_vcpu_trap_get_fault(vcpu)) { >> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c >> index 1a01da9fdc99..75814a02d189 100644 >> --- a/arch/arm64/kvm/mmu.c >> +++ b/arch/arm64/kvm/mmu.c >> @@ -754,10 +754,12 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, >> gfn_t gfn; >> kvm_pfn_t pfn; >> bool logging_active = memslot_is_logging(memslot); >> - unsigned long vma_pagesize; >> + unsigned long fault_level = kvm_vcpu_trap_get_fault_level(vcpu); >> + unsigned long vma_pagesize, fault_granule; >> enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; >> struct kvm_pgtable *pgt; >> >> + fault_granule = 1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(fault_level); > I like the idea, but is this macro reliable for stage-2 page-tables, given > that we could have a concatenated pgd? > > Will > . Yes, it's fine even when we have a concatenated pgd table. No matter a concatenated pgd will be made or not, the initial lookup level (start _level) is set in VTCR_EL2 register. The MMU hardware walker will know the start_level according to information in VTCR_EL2. This idea runs well in practice on host where ia_bits is 40, PAGE_SIZE is 4k, and a concatenated pgd is made for guest stage2. According to the kernel info printed, the start_level is 1, and stage 2 translation runs as expected. Yanan