Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1354802pxj; Fri, 18 Jun 2021 05:29:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2Z7t16fS7dbY7JBGL8hOSix/MGtNp+C9nEVwXUF91wt7I7XOZdW83iOVp8yLSwyuteVoC X-Received: by 2002:a17:907:c16:: with SMTP id ga22mr10944844ejc.116.1624019399024; Fri, 18 Jun 2021 05:29:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624019399; cv=none; d=google.com; s=arc-20160816; b=thEzsPhYsQibUDqT0b6T2gBpqKLEXnbDV4D3yEzOMM0gABq8sULfWvJCoKcwA0wpsU yPU1FAXLwMyIP5/ybX6imZyA+W5KH0gqlsSYZhcEjmvGTR4JbaxROjmUNrgdmhZZWhog My0eNXC/9M0XnzTMkwj6ZXsityj8B/biJj5GY50ZComFchUgBkCmSsi/TxChsJn50E51 Nt2sNxwmy8L3X57VCfQIAkn9NEcBdVDLD9sY2vTixuM7aCigKonCOVnLxaiqa4T+rDNe SRsZCSpzp0/OYz9GpFz7LG3+OsSYF/HPUYy9EdMYxrLyqwONtrUTH3SDuphtkA81hdxs Gz6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=GPeRH4hmuREp1clHnwfw9qScnyR9MdpgxRYZe37BmEE=; b=0yH8SDrkR45Wt4jAG5wpp5F51y3N24Ocq+2outovDnuYBe/S7pA2ICrTxDNzHYBh4a Zh48+1CD+5B5hYQddBicMqjuvBVZiNKzW0BO4kTnuHLROD5a8mp8HN3/nEu3EA5jE5kg KRicymI6DGg4C6WJJiLWkAIHNzqIohd/jXarZWtyBUCbQ2tMwQvuWF9fbfIso2fflzog xLOhxWA+wfhEjhf8ZkhW14ye1zdHnsUI5ev0UKFhcZKIt/E8iTfs+uQaL6tZNizz1gsB UlDDXFjAzE3xx3cyex8yv5Qu9ds9RZA/VNIuHgRFhY1lhsndYEHy95Buf+aRceyMAf2x 9tlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pMSTcZFH; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rv24si2204276ejb.690.2021.06.18.05.29.35; Fri, 18 Jun 2021 05:29:59 -0700 (PDT) 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; dkim=pass header.i=@google.com header.s=20161025 header.b=pMSTcZFH; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231633AbhFRJeQ (ORCPT + 99 others); Fri, 18 Jun 2021 05:34:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232203AbhFRJeC (ORCPT ); Fri, 18 Jun 2021 05:34:02 -0400 Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27265C0611C0 for ; Fri, 18 Jun 2021 02:31:18 -0700 (PDT) Received: by mail-ot1-x32f.google.com with SMTP id w23-20020a9d5a970000b02903d0ef989477so9057335oth.9 for ; Fri, 18 Jun 2021 02:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GPeRH4hmuREp1clHnwfw9qScnyR9MdpgxRYZe37BmEE=; b=pMSTcZFHJwl7kCAJEOdswnD5xOFqmsX98UB70ZEyYyVDghAwEyBerXqBibqrwow426 yYBrOM2azwYmGF0JUPol1DBiNdkCs2o+AbOr5zS2BZvBP7Bo/lOrQBaAFeDJMADVMUOc HMtUq1tBiceMSgQ+1vmfrwbVbBLDV2zCSmu6SY1yMrS29xfsUn+pHtiIPJ5C+zOlrIwF Doqgjl6y7HYDtSLnuUtyvBkn8GJGMrJiKa+yLUDCC96txA8mXVHkVfrYm4NJoXwNygFT PlyYTn2U4T+GPsGlZfm3oRU4ndDDo54s50l9cbhFV04x3ACVmJvG2EGZ9A7IJ84clsbc Cydg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GPeRH4hmuREp1clHnwfw9qScnyR9MdpgxRYZe37BmEE=; b=fK1mVBF6kHkuSlTm6FsffaetoQbWAXGLl2yZkc4l3njhcROXqPoWE1DdBI1GldDFCO xML1cAxzHwGkEwyDpOgz6LT70qs5SR+3ptoZO+/151ut2cmuYgUwNYQozXNl+q3+jBgY guytNScI/CyvQK6Mcz17ZBp1wvr8xsEYgky5V2U6KqrQVWURZRQHe2a+TSMdyYHRl13L NMMInyTSuzpMFTu8ywlaqzL4E0S38jRynh6xvBciR9FO+KRo2j8lQEXedVMR8avNi/pg wBMFkR64zzk2q7p/Lia4RTU/afefqBwmzApBigGrsDahqWKnGpY301hOcjYwC66Hjdis wAHA== X-Gm-Message-State: AOAM5312mTvT99RmuajIxO2U9jBfqowaYmRRBYl5K8R0ZZiwUP39Aj61 gsEsxB9QCUq6U6gKj6LYV+yBtN/1toOyT+9EdmI9Fg== X-Received: by 2002:a05:6830:1598:: with SMTP id i24mr8609437otr.52.1624008677336; Fri, 18 Jun 2021 02:31:17 -0700 (PDT) MIME-Version: 1.0 References: <20210617105824.31752-1-wangyanan55@huawei.com> <20210617105824.31752-5-wangyanan55@huawei.com> In-Reply-To: <20210617105824.31752-5-wangyanan55@huawei.com> From: Fuad Tabba Date: Fri, 18 Jun 2021 10:30:41 +0100 Message-ID: Subject: Re: [PATCH v7 4/4] KVM: arm64: Move guest CMOs to the fault handlers To: Yanan Wang Cc: Marc Zyngier , Will Deacon , Quentin Perret , Alexandru Elisei , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Catalin Marinas Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Yanan, On Thu, Jun 17, 2021 at 11:58 AM Yanan Wang wrote: > > We currently uniformly permorm CMOs of D-cache and I-cache in function Nit: permorm -> perform > user_mem_abort before calling the fault handlers. If we get concurrent > guest faults(e.g. translation faults, permission faults) or some really > unnecessary guest faults caused by BBM, CMOs for the first vcpu are > necessary while the others later are not. > > By moving CMOs to the fault handlers, we can easily identify conditions > where they are really needed and avoid the unnecessary ones. As it's a > time consuming process to perform CMOs especially when flushing a block > range, so this solution reduces much load of kvm and improve efficiency > of the stage-2 page table code. > > We can imagine two specific scenarios which will gain much benefit: > 1) In a normal VM startup, this solution will improve the efficiency of > handling guest page faults incurred by vCPUs, when initially populating > stage-2 page tables. > 2) After live migration, the heavy workload will be resumed on the > destination VM, however all the stage-2 page tables need to be rebuilt > at the moment. So this solution will ease the performance drop during > resuming stage. > > Signed-off-by: Yanan Wang > --- > arch/arm64/kvm/hyp/pgtable.c | 38 +++++++++++++++++++++++++++++------- > arch/arm64/kvm/mmu.c | 37 ++++++++++++++--------------------- > 2 files changed, 46 insertions(+), 29 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index d99789432b05..760c551f61da 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -577,12 +577,24 @@ static void stage2_put_pte(kvm_pte_t *ptep, struct kvm_s2_mmu *mmu, u64 addr, > mm_ops->put_page(ptep); > } > > +static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte) > +{ > + u64 memattr = pte & KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR; > + return memattr == KVM_S2_MEMATTR(pgt, NORMAL); > +} > + > +static bool stage2_pte_executable(kvm_pte_t pte) > +{ > + return !(pte & KVM_PTE_LEAF_ATTR_HI_S2_XN); > +} > + > static int 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; > + struct kvm_pgtable *pgt = data->mmu->pgt; > struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; > > if (!kvm_block_mapping_supported(addr, end, phys, level)) > @@ -606,6 +618,14 @@ static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > stage2_put_pte(ptep, data->mmu, addr, level, mm_ops); > } > > + /* Perform CMOs before installation of the guest stage-2 PTE */ > + if (mm_ops->clean_invalidate_dcache && stage2_pte_cacheable(pgt, new)) > + mm_ops->clean_invalidate_dcache(kvm_pte_follow(new, mm_ops), > + granule); > + > + if (mm_ops->invalidate_icache && stage2_pte_executable(new)) > + mm_ops->invalidate_icache(kvm_pte_follow(new, mm_ops), granule); > + > smp_store_release(ptep, new); > if (stage2_pte_is_counted(new)) > mm_ops->get_page(ptep); > @@ -798,12 +818,6 @@ int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, > return ret; > } > > -static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte) > -{ > - u64 memattr = pte & KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR; > - return memattr == KVM_S2_MEMATTR(pgt, NORMAL); > -} > - > static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, > enum kvm_pgtable_walk_flags flag, > void * const arg) > @@ -874,6 +888,7 @@ static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, > { > kvm_pte_t pte = *ptep; > struct stage2_attr_data *data = arg; > + struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; > > if (!kvm_pte_valid(pte)) > return 0; > @@ -888,8 +903,17 @@ static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, > * but worst-case the access flag update gets lost and will be > * set on the next access instead. > */ > - if (data->pte != pte) > + if (data->pte != pte) { > + /* > + * Invalidate instruction cache before updating the guest > + * stage-2 PTE if we are going to add executable permission. > + */ > + if (mm_ops->invalidate_icache && > + stage2_pte_executable(pte) && !stage2_pte_executable(*ptep)) > + mm_ops->invalidate_icache(kvm_pte_follow(pte, mm_ops), > + kvm_granule_size(level)); > WRITE_ONCE(*ptep, pte); > + } > > return 0; > } > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index b980f8a47cbb..c9f002d74ab4 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -434,14 +434,16 @@ int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size, > } > > static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = { > - .zalloc_page = stage2_memcache_zalloc_page, > - .zalloc_pages_exact = kvm_host_zalloc_pages_exact, > - .free_pages_exact = free_pages_exact, > - .get_page = kvm_host_get_page, > - .put_page = kvm_host_put_page, > - .page_count = kvm_host_page_count, > - .phys_to_virt = kvm_host_va, > - .virt_to_phys = kvm_host_pa, > + .zalloc_page = stage2_memcache_zalloc_page, > + .zalloc_pages_exact = kvm_host_zalloc_pages_exact, > + .free_pages_exact = free_pages_exact, > + .get_page = kvm_host_get_page, > + .put_page = kvm_host_put_page, > + .page_count = kvm_host_page_count, > + .phys_to_virt = kvm_host_va, > + .virt_to_phys = kvm_host_pa, > + .clean_invalidate_dcache = clean_dcache_guest_page, > + .invalidate_icache = invalidate_icache_guest_page, > }; > > /** > @@ -1012,15 +1014,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, > if (writable) > prot |= KVM_PGTABLE_PROT_W; > > - if (fault_status != FSC_PERM && !device) > - clean_dcache_guest_page(page_address(pfn_to_page(pfn)), > - vma_pagesize); > - > - if (exec_fault) { > + if (exec_fault) > prot |= KVM_PGTABLE_PROT_X; > - invalidate_icache_guest_page(page_address(pfn_to_page(pfn)), > - vma_pagesize); > - } > > if (device) > prot |= KVM_PGTABLE_PROT_DEVICE; > @@ -1218,12 +1213,10 @@ bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) > WARN_ON(range->end - range->start != 1); > > /* > - * We've moved a page around, probably through CoW, so let's treat it > - * just like a translation fault and clean the cache to the PoC. > - */ > - clean_dcache_guest_page(page_address(pfn_to_page(pfn), PAGE_SIZE); > - > - /* > + * We've moved a page around, probably through CoW, so let's treat > + * it just like a translation fault and the map handler will clean > + * the cache to the PoC. > + * > * The MMU notifiers will have unmapped a huge PMD before calling > * ->change_pte() (which in turn calls kvm_set_spte_gfn()) and > * therefore we never need to clear out a huge PMD through this Reviewed-by: Fuad Tabba Thanks, /fuad > -- > 2.23.0 > > _______________________________________________ > kvmarm mailing list > kvmarm@lists.cs.columbia.edu > https://lists.cs.columbia.edu/mailman/listinfo/kvmarm