From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C12E0C4361B for ; Fri, 11 Dec 2020 09:55:12 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5E02E23F2A for ; Fri, 11 Dec 2020 09:55:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5E02E23F2A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=2rrXn6ghrjflWqgVafKHzK4Qer0t7DG/vpnMpagPeTA=; b=iSwe1faxaKhpp24hb5sGDnJOK B4jT9LT8H4PM3pUSlnkiJXNUY5GzB8g1qKzbGdA8srliaBrTxysJQAKKWNMZvKD9IxXe1lt/qXg2w RhABEJ728+8Qcw1Dv6d3G5xtI+uw6xrJhSAPA2AmSArb6u/8p+efxZAlveaSXjIzXuuV0FOh9hADe TZx/g1ShB5QqyvUnESYQMm1eGSnZ9Do2nQYo33Y09MAkEG65PBJ2GpoPKyaEc2A8gK2cuKlr5Tyu5 RnLRDoq2N8kQjWmKhYQva/QlvtSWRCVHwe01eJ+eJrRsn2CMwlQjb4aEYObb62xemD0lph3CKtt4H 95IWUc8Zg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1knf7V-0001wJ-Bs; Fri, 11 Dec 2020 09:53:49 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1knf7R-0001vO-MS for linux-arm-kernel@lists.infradead.org; Fri, 11 Dec 2020 09:53:46 +0000 Date: Fri, 11 Dec 2020 09:53:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1607680424; bh=QsIVnJ8CGRl70yVewj8Ed9nYo9b0SK7ijo6uVqyomD0=; h=From:To:Cc:Subject:References:In-Reply-To:From; b=S6841Py29DfCupVk+JTMy1wYlPbw+kneOmgnZxgAehUaZQzhYedmBD300AxCeSe3h lm4Jb9lK2IX2eEhGWL7Nrf9uiZ846OTXTxtIbb/+QqL5R6BKnJwlb+renKMjJpmbfl SdLiw5v75hCqBsBxQbqbyi8R8G9SMTNaYuisHYpJsFaQvXH24Jl/4/q6WzlVWcOtsZ f76YdRRa/Y3zj643s3F3uFjZ0GBwEnUjRBdMgFJnHSGHyGQlQ3PgGPZiczArzszw6a 7hVBb3hlHPVgcBSwFeWcmxL5wKTkGa8sn6wINCyzfExJ+xhkad8DyaLoMamrD//Deu d84P2IgaFzYXg== From: Will Deacon To: Yanan Wang Subject: Re: [RFC PATCH] KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler Message-ID: <20201211095337.GA11280@willie-the-truck> References: <20201211080115.21460-1-wangyanan55@huawei.com> <20201211080115.21460-2-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20201211080115.21460-2-wangyanan55@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201211_045345_957622_FB97A46D X-CRM114-Status: GOOD ( 33.01 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jiangkunkun@huawei.com, Gavin Shan , Suzuki K Poulose , Marc Zyngier , wangjingyi11@huawei.com, Quentin Perret , lushenming@huawei.com, linux-kernel@vger.kernel.org, yezengruan@huawei.com, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , yuzenghui@huawei.com, wanghaibin.wang@huawei.com, zhukeqian1@huawei.com, Julien Thierry Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Yanan, On Fri, Dec 11, 2020 at 04:01:15PM +0800, Yanan Wang wrote: > In dirty-logging, or dirty-logging-stopped time, even normal running > time of a guest configed with huge mappings and numbers of vCPUs, > translation faults by different vCPUs on the same GPA could occur > successively almost at the same time. There are two reasons for it. > > (1) If there are some vCPUs accessing the same GPA at the same time > and the leaf PTE is not set yet, then they will all cause translation > faults and the first vCPU holding mmu_lock will set valid leaf PTE, > and the others will later choose to update the leaf PTE or not. > > (2) When changing a leaf entry or a table entry with break-before-make, > if there are some vCPUs accessing the same GPA just catch the moment > when the target PTE is set invalid in a BBM procedure coincidentally, > they will all cause translation faults and will later choose to update > the leaf PTE or not. > > The worst case can be like this: some vCPUs cause translation faults > on the same GPA with different prots, they will fight each other by > changing back access permissions of the PTE with break-before-make. > And the BBM-invalid moment might trigger more unnecessary translation > faults. As a result, some useless small loops will occur, which could > lead to vCPU stuck. > > To avoid unnecessary update and small loops, add prejudgement in the > translation fault handler: Skip updating the valid leaf PTE if we are > trying to recreate exactly the same mapping or to reduce access > permissions only(such as RW-->RO). And update the valid leaf PTE without > break-before-make if we are trying to add more permissions only. > > Signed-off-by: Yanan Wang > --- > arch/arm64/kvm/hyp/pgtable.c | 73 +++++++++++++++++++++++++----------- > 1 file changed, 52 insertions(+), 21 deletions(-) Cheers for this. Given that this patch is solving a few different problems, do you think you could split it up please? That would certainly make it much easier to review, as there's quite a lot going on here. A chunk of the changes seem to be the diff I posted previously: https://lore.kernel.org/r/20201201141632.GC26973@willie-the-truck so maybe that could be its own patch? > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 23a01dfcb27a..f8b3248cef1c 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -45,6 +45,8 @@ > > #define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) > > +#define KVM_PTE_LEAF_ATTR_PERMS (GENMASK(7, 6) | BIT(54)) You only use this on the S2 path, so how about: #define KVM_PTE_LEAF_ATTR_S2_PERMS KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \ KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ KVM_PTE_LEAF_ATTR_HI_S2_XN or something like that? > struct kvm_pgtable_walk_data { > struct kvm_pgtable *pgt; > struct kvm_pgtable_walker *walker; > @@ -170,10 +172,9 @@ static void kvm_set_table_pte(kvm_pte_t *ptep, kvm_pte_t *childp) > smp_store_release(ptep, pte); > } > > -static bool kvm_set_valid_leaf_pte(kvm_pte_t *ptep, u64 pa, kvm_pte_t attr, > - u32 level) > +static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level) > { > - kvm_pte_t old = *ptep, pte = kvm_phys_to_pte(pa); > + kvm_pte_t pte = kvm_phys_to_pte(pa); > u64 type = (level == KVM_PGTABLE_MAX_LEVELS - 1) ? KVM_PTE_TYPE_PAGE : > KVM_PTE_TYPE_BLOCK; > > @@ -181,12 +182,7 @@ static bool kvm_set_valid_leaf_pte(kvm_pte_t *ptep, u64 pa, kvm_pte_t attr, > pte |= FIELD_PREP(KVM_PTE_TYPE, type); > pte |= KVM_PTE_VALID; > > - /* Tolerate KVM recreating the exact same mapping. */ > - if (kvm_pte_valid(old)) > - return old == pte; > - > - smp_store_release(ptep, pte); > - return true; > + return pte; > } > > static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr, > @@ -341,12 +337,17 @@ static int hyp_map_set_prot_attr(enum kvm_pgtable_prot prot, > static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level, > kvm_pte_t *ptep, struct hyp_map_data *data) > { > + kvm_pte_t new, old = *ptep; > u64 granule = kvm_granule_size(level), phys = data->phys; > > if (!kvm_block_mapping_supported(addr, end, phys, level)) > return false; > > - WARN_ON(!kvm_set_valid_leaf_pte(ptep, phys, data->attr, level)); > + /* Tolerate KVM recreating the exact same mapping. */ > + new = kvm_init_valid_leaf_pte(phys, data->attr, level); > + if (old != new && !WARN_ON(kvm_pte_valid(old))) > + smp_store_release(ptep, new); > + > data->phys += granule; > return true; > } > @@ -461,25 +462,56 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot, > return 0; > } > > +static bool stage2_set_valid_leaf_pte_pre(u64 addr, u32 level, > + kvm_pte_t *ptep, kvm_pte_t new, > + struct stage2_map_data *data) > +{ > + kvm_pte_t old = *ptep, old_attr, new_attr; > + > + if ((old ^ new) & (~KVM_PTE_LEAF_ATTR_PERMS)) > + return false; > + > + /* > + * Skip updating if we are trying to recreate exactly the same mapping > + * or to reduce the access permissions only. And update the valid leaf > + * PTE without break-before-make if we are trying to add more access > + * permissions only. > + */ > + old_attr = (old & KVM_PTE_LEAF_ATTR_PERMS) ^ KVM_PTE_LEAF_ATTR_HI_S2_XN; > + new_attr = (new & KVM_PTE_LEAF_ATTR_PERMS) ^ KVM_PTE_LEAF_ATTR_HI_S2_XN; > + if (new_attr <= old_attr) > + return true; I think this is a significant change in behaviour for kvm_pgtable_stage2_map() and I worry that it could catch somebody out in the future. Please can you update the kerneldoc in kvm_pgtable.h with a note about this? Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel