Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp918553pxb; Fri, 22 Jan 2021 02:32:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzH9tg+1+vmWT3LmS5/UDsthPWn+eF7hHQzCut9oAdOfQeH+eolJKb1prEluTVTpCsMUznG X-Received: by 2002:a05:6402:b2f:: with SMTP id bo15mr2779217edb.146.1611311530288; Fri, 22 Jan 2021 02:32:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611311530; cv=none; d=google.com; s=arc-20160816; b=Dnt13gQCfUyLpNkjFu+Ah6hMVnUs5GAXTYmxKr9ppxI/BoeqGT+EMvO/e5+Z+a5/cz LIuNsjLrmcbCTDqVvtGRuk1GDE0p9knv/5kntsORFkb/5XrPsWQ5VHhZ0Z9EVxi+2lPN C4VhmZEGds6BJbe4UE74c33XnIBOJkaKe8UrH8Qw9egr3jnUa0jTp/bnJaoHaxJwL70X XyB6Lic5yMuuuoKPibSSwqoE14H5nNLmmrHkqB2Qh77q8LOjhzGJc8sqYFk3/FDi8bMT ORsvW9zTC0WLtDyofqgMcT1UxI4KL0m4f+xYlq0ghjZPpecYXBrFzGOFrxJdr7iuNngp 5k4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=ny/HN/5n7wIglh6TH14xE8N2Mwf4AOn8kXZ4hQOT0r0=; b=nyKyNbcQJikfS3YQpGRLSI6oHNE/IN580OJwZr+bEpeyJ6zFf8L6r2R6L5qjlUWtyX 6yrJzMB33ZbXTAsdQLXt/OzDDmIfs52/gCL7yYb2LytmedGEOieEdd5bZzR1JfcSWAtk 5JoSczy1QVO9RDpyChnGE67mwRg8AomPRt0Euniflvm+p7WGA7+toi0KK2YH5xf9J3eU sZlQQ0E9icxbNvm2z6tOM3B92PVDPjPtmEZ1ylcIt+c7fo5azk/9AVpeODUyjCWUXxl7 zvhfVeZnhOLLZOoCcXFn43nnMNoH3MhoaiFJzHIvJS66BEv6w2f5YDRcvbU8COjY+wnl kDCQ== 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 b14si3320560ede.90.2021.01.22.02.31.45; Fri, 22 Jan 2021 02:32:10 -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 S1727777AbhAVKbB (ORCPT + 99 others); Fri, 22 Jan 2021 05:31:01 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:11128 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727776AbhAVKOw (ORCPT ); Fri, 22 Jan 2021 05:14:52 -0500 Received: from DGGEMS403-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DMZp50VG5z15x19; Fri, 22 Jan 2021 18:13:01 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS403-HUB.china.huawei.com (10.3.19.203) with Microsoft SMTP Server id 14.3.498.0; Fri, 22 Jan 2021 18:14:03 +0800 From: Yanan Wang To: Marc Zyngier , Will Deacon , "Catalin Marinas" , , , , CC: James Morse , Julien Thierry , Suzuki K Poulose , Gavin Shan , Quentin Perret , , , , Yanan Wang Subject: [RFC PATCH v4 1/2] KVM: arm64: Adjust partial code of hyp stage-1 map and guest stage-2 map Date: Fri, 22 Jan 2021 18:13:57 +0800 Message-ID: <20210122101358.379956-2-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20210122101358.379956-1-wangyanan55@huawei.com> References: <20210122101358.379956-1-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.174.187.128] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Procedures of hyp stage-1 map and guest stage-2 map are quite different, but they are tied closely by function kvm_set_valid_leaf_pte(). So adjust the relative code for ease of code maintenance in the future. Signed-off-by: Yanan Wang --- arch/arm64/kvm/hyp/pgtable.c | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index bdf8e55ed308..2878aaf53b3c 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -170,10 +170,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 +180,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 +335,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; } @@ -465,27 +464,29 @@ static bool stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, struct stage2_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; - /* - * If the PTE was already valid, drop the refcount on the table - * early, as it will be bumped-up again in stage2_map_walk_leaf(). - * This ensures that the refcount stays constant across a valid to - * valid PTE update. - */ - if (kvm_pte_valid(*ptep)) - put_page(virt_to_page(ptep)); + new = kvm_init_valid_leaf_pte(phys, data->attr, level); + if (kvm_pte_valid(old)) { + /* Tolerate KVM recreating the exact same mapping */ + if (old == new) + goto out; - if (kvm_set_valid_leaf_pte(ptep, phys, data->attr, level)) - goto out; + /* + * There's an existing different valid leaf entry, so perform + * break-before-make. + */ + kvm_set_invalid_pte(ptep); + kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); + put_page(virt_to_page(ptep)); + } - /* There's an existing valid leaf entry, so perform break-before-make */ - kvm_set_invalid_pte(ptep); - kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, data->mmu, addr, level); - kvm_set_valid_leaf_pte(ptep, phys, data->attr, level); + smp_store_release(ptep, new); + get_page(virt_to_page(ptep)); out: data->phys += granule; return true; @@ -527,7 +528,7 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, } if (stage2_map_walker_try_leaf(addr, end, level, ptep, data)) - goto out_get_page; + return 0; if (WARN_ON(level == KVM_PGTABLE_MAX_LEVELS - 1)) return -EINVAL; @@ -551,9 +552,8 @@ static int stage2_map_walk_leaf(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, } kvm_set_table_pte(ptep, childp); - -out_get_page: get_page(page); + return 0; } -- 2.19.1