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=-17.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham 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 A8812C433E0 for ; Wed, 13 Jan 2021 15:45:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6017F208BA for ; Wed, 13 Jan 2021 15:45:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726645AbhAMPpD (ORCPT ); Wed, 13 Jan 2021 10:45:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:36186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725977AbhAMPpC (ORCPT ); Wed, 13 Jan 2021 10:45:02 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id D01FA208BA; Wed, 13 Jan 2021 15:44:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1610552661; bh=+rZ1G0MM72WaqgZtoKvVZ0XdQ+wClUuZnuEWYmqhNAg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=K8P+Ll0pkHS0co8mduKu2x4XQGzTmwp/re9ivY0Rm6Y1SaRk8tCzdlCMgVW/Qqs3q cSRjWLsbk8Knxh7HzOYiZK2NwryIfOEO8iSHNGCdtVI6yZvrs4tEJy3hj8uQa07WEr eWw+IQ2YYMOTevQ/oEeUGlVWF7fofd0qjyVxJVFS9PigiDSr+AHPBtpomjpgV8cQN/ AOG/1qqHCNkggeEipinOedyNFqdzAJg5OTfwuaUFad32FMACRtwbwOasrGCpMQRqTR DDprXhjv2lobyQFXua7wUfZZKJN1o+14+R8r2G4ZNJltSNFsY5qyvrQAN9guO2DgUD PpEL+qjJ3Hbww== Date: Wed, 13 Jan 2021 15:44:15 +0000 From: Will Deacon To: Yanan Wang Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Marc Zyngier , Catalin Marinas , James Morse , Julien Thierry , Suzuki K Poulose , Gavin Shan , Quentin Perret , wanghaibin.wang@huawei.com, yezengruan@huawei.com, zhukeqian1@huawei.com, yuzenghui@huawei.com Subject: Re: [PATCH v2 2/3] KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler Message-ID: <20210113154414.GA11892@willie-the-truck> References: <20201216122844.25092-1-wangyanan55@huawei.com> <20201216122844.25092-3-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201216122844.25092-3-wangyanan55@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 16, 2020 at 08:28:43PM +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 update the old PTE with a new one if they are different. > > (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 update the old PTE with a new > one if they are different. > > The worst case can be like this: vCPU A causes a translation fault with RW > prot and sets the leaf PTE with RW permissions, and then the next vCPU B > with RO prot updates the PTE back to RO permissions with break-before-make. > And the BBM-invalid moment may trigger more unnecessary translation faults, > then some useless small loops might occur which could lead to vCPU stuck. > > To avoid unnecessary update and small loops, add prejudgement in the > translation fault handler: Skip updating the PTE with break-before-make > if we are trying to recreate the exact same mapping or only change the > access permissions. Actually, change of permissions will be handled > through the relax_perms path next time if necessary. > > Signed-off-by: Yanan Wang > --- > arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++--------- > 1 file changed, 19 insertions(+), 9 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 350f9f810930..8225ced49bad 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -45,6 +45,10 @@ > > #define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) > > +#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) > + > struct kvm_pgtable_walk_data { > struct kvm_pgtable *pgt; > struct kvm_pgtable_walker *walker; > @@ -460,7 +464,7 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot, > return 0; > } > > -static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > +static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > kvm_pte_t *ptep, > struct stage2_map_data *data) > { > @@ -469,13 +473,18 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > struct page *page = virt_to_page(ptep); > > if (!kvm_block_mapping_supported(addr, end, phys, level)) > - return false; > + return 1; It would probably be cleaner to return another error code here, as we have failed to install a mapping (e.g. E2BIG or perhaps more perversely, ENOTBLK). Then the caller can decide to install a trable. Will 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.7 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 7A5F6C433DB for ; Wed, 13 Jan 2021 15:46: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 1DF852339F for ; Wed, 13 Jan 2021 15:46:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1DF852339F 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=f3REqlYrIVp/otGndh/VwiNxtd8ZCBZ4r4w64WuDsTI=; b=jn4fgkvibhqy4iYM13dXRHDy/ P5HBNrZMFOUCVFbmnLEyG/obCqmpfEF4WKjK9RIR8uNjoKVyOhGOM3+P04aril+gC4TahcKA7WUaW 6Yaf64tjavFZd6UwiEi963AXJ6BXzbidBgnyiNmTbT5q1rv9jHqR88brRmCe0bUxAw54pIhhh/yd5 fhcIF8MYTvZQV0h9gwhi6+rhLRwokxyP0kRiCd2Bx9MIwzOXIotDaU8keM1MmhWHxoPPnzfHGOt4d O4q27Cr3WAe4Q4C5CvaBuHlJRJ+HRUr0DNq+43sHcy/vi/whaZvPhWPF6Anl/u65GowTfwu+pxc9s 3qmxaleVw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kziJs-0006WI-Ht; Wed, 13 Jan 2021 15:44:24 +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 1kziJq-0006Vx-5i for linux-arm-kernel@lists.infradead.org; Wed, 13 Jan 2021 15:44:23 +0000 Received: by mail.kernel.org (Postfix) with ESMTPSA id D01FA208BA; Wed, 13 Jan 2021 15:44:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1610552661; bh=+rZ1G0MM72WaqgZtoKvVZ0XdQ+wClUuZnuEWYmqhNAg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=K8P+Ll0pkHS0co8mduKu2x4XQGzTmwp/re9ivY0Rm6Y1SaRk8tCzdlCMgVW/Qqs3q cSRjWLsbk8Knxh7HzOYiZK2NwryIfOEO8iSHNGCdtVI6yZvrs4tEJy3hj8uQa07WEr eWw+IQ2YYMOTevQ/oEeUGlVWF7fofd0qjyVxJVFS9PigiDSr+AHPBtpomjpgV8cQN/ AOG/1qqHCNkggeEipinOedyNFqdzAJg5OTfwuaUFad32FMACRtwbwOasrGCpMQRqTR DDprXhjv2lobyQFXua7wUfZZKJN1o+14+R8r2G4ZNJltSNFsY5qyvrQAN9guO2DgUD PpEL+qjJ3Hbww== Date: Wed, 13 Jan 2021 15:44:15 +0000 From: Will Deacon To: Yanan Wang Subject: Re: [PATCH v2 2/3] KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler Message-ID: <20210113154414.GA11892@willie-the-truck> References: <20201216122844.25092-1-wangyanan55@huawei.com> <20201216122844.25092-3-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20201216122844.25092-3-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-20210113_104422_376908_D480BB1D X-CRM114-Status: GOOD ( 23.82 ) 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: Gavin Shan , Suzuki K Poulose , Marc Zyngier , Quentin Perret , 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 On Wed, Dec 16, 2020 at 08:28:43PM +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 update the old PTE with a new one if they are different. > > (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 update the old PTE with a new > one if they are different. > > The worst case can be like this: vCPU A causes a translation fault with RW > prot and sets the leaf PTE with RW permissions, and then the next vCPU B > with RO prot updates the PTE back to RO permissions with break-before-make. > And the BBM-invalid moment may trigger more unnecessary translation faults, > then some useless small loops might occur which could lead to vCPU stuck. > > To avoid unnecessary update and small loops, add prejudgement in the > translation fault handler: Skip updating the PTE with break-before-make > if we are trying to recreate the exact same mapping or only change the > access permissions. Actually, change of permissions will be handled > through the relax_perms path next time if necessary. > > Signed-off-by: Yanan Wang > --- > arch/arm64/kvm/hyp/pgtable.c | 28 +++++++++++++++++++--------- > 1 file changed, 19 insertions(+), 9 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 350f9f810930..8225ced49bad 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -45,6 +45,10 @@ > > #define KVM_PTE_LEAF_ATTR_HI_S2_XN BIT(54) > > +#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) > + > struct kvm_pgtable_walk_data { > struct kvm_pgtable *pgt; > struct kvm_pgtable_walker *walker; > @@ -460,7 +464,7 @@ static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot, > return 0; > } > > -static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > +static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > kvm_pte_t *ptep, > struct stage2_map_data *data) > { > @@ -469,13 +473,18 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > struct page *page = virt_to_page(ptep); > > if (!kvm_block_mapping_supported(addr, end, phys, level)) > - return false; > + return 1; It would probably be cleaner to return another error code here, as we have failed to install a mapping (e.g. E2BIG or perhaps more perversely, ENOTBLK). Then the caller can decide to install a trable. Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel