Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp473919pxj; Wed, 2 Jun 2021 04:02:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqvzKUcZvSRogV3GIBi6UkSm1P+/VJEat+AdpyKL2s0I00TKWEvyeSwAPViUDa1O+PjOH1 X-Received: by 2002:aa7:c782:: with SMTP id n2mr38162210eds.77.1622631733768; Wed, 02 Jun 2021 04:02:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622631733; cv=none; d=google.com; s=arc-20160816; b=yMADPjSJCoCULtXYOr5Y1TXX6FNWN4bCt4f3P/+du2PQtv5JBM2ZDFlNJV3afwYbnW yZUJTCY77WRHYOPItjKndcLHdJS2rMBhjG0yDF782/ljOYwZIQ4388gi1jICWKKhaS9r 8QUWbgdS29TQhlxANJ5ANxMyLJ9xjhsqk9dCYiziZNbAKv1rVZs/6U3Qi8GBehuB1MUq C32T6yudOLSNJor6356hjMJuwLJbotk4NC91swzLcI8va+No/A1YXmfTNnjaXvi9NL7h pwv3pYeOBTsAZngaSxQQRg2PpEwaoxueXlLexffdtiOa8cFf+OrGxC9/Dw8lGVWvbvgh ahJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=O4j3rnMAVVtQuafSexYtSyjSly5WDUnGVtpX8LTwkWk=; b=osP9NuExAesmS8YV0LxuQHryzKuHF6J165oyoR0+9Ei7uqDfG+dAFLq0cSL5pf037L YVUzfnGDruBwB3IEQjtwWyuzKnGNbixBFHcYlsTFkeXgXmHF6nqlNpbXxqSty2yWFmpV cNWX87TtYdBwa/+m5pQzwTL6sR+HOh6nthame8RqGctOkXEZxmTxp4VmQLKc8btXG4xw /bY3DzP8TBvjTqPP3rVLGH0t5OWqHz3462jXsDcJmMDyt82iTDSqh3L+nYGN+Q+FzAa8 qjMt5ZipZnASFl/aaamVabmsEZppML/5VofCt7rofHFR2R1aHQLw8qkJ0+EgsEz6CiNG jPXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=d9OMAA5J; 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 l18si6277213ejr.283.2021.06.02.04.01.45; Wed, 02 Jun 2021 04:02:13 -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=d9OMAA5J; 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 S231652AbhFBLAt (ORCPT + 99 others); Wed, 2 Jun 2021 07:00:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232086AbhFBLA1 (ORCPT ); Wed, 2 Jun 2021 07:00:27 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB184C061343 for ; Wed, 2 Jun 2021 03:58:32 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id n4so1844664wrw.3 for ; Wed, 02 Jun 2021 03:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=O4j3rnMAVVtQuafSexYtSyjSly5WDUnGVtpX8LTwkWk=; b=d9OMAA5JQL8jC8IxP/OtQ63ClDX5Ld+AUFGF/BIOaD/SPONLkodo+0jo+RoR5mTEoG PgEtpTeFjZuBJL9c8NqbqT4O2UEaEcx8ML3eRmuh0UsgOU9zhHUnaTUwSksBRv50EFc4 /5c1l39b3D/IhKxRwmtDrUarmu7vHmsK2uTIYSofXXK4E/7OAeHrYCBLWmlw0NiySvuD 8OQG/6n06I2QQCQrFJvnYGrFmG3Xm6Oa5OvJSwdktqi9f0XB5AWk5Z8d943IYaK4XrOZ NIJ+adU/E5fxPEDKhHTWUPvO89BHwHgJ1hxssujS/JX1wQNwa0PwvOnQC6krwwxh+nWU q3WQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=O4j3rnMAVVtQuafSexYtSyjSly5WDUnGVtpX8LTwkWk=; b=Ro3862mCevRVzx7PGvCgbH+fNxuM+rQedM4/C8PNB5VGnO5Zrcsw9U1gFiwBXU89Y2 ledg9Bo+UkNabyJLuxZLR6AgbYRff+pmWE2olBubk9j8EEAxrt3XwkHIS8EFW8nH42/p yE8YOnAdjCRNb720A76Zru+V4k9Onb8RkeLvnaM3UvxOlbbeNrBxplnATxySC3mwPRYI QWEtFBm47LUD5/IvIofyw6q9IX2e3YHOLkjekiOrGnKDxViHZJruw41Z7+5a/KIC5Dpq s8zuY+GqOCCbDNcFyWXOz0u3BO0vKP0WekR0D6lrtMAwe5NSk5dKGvL9aXstsKi1yLfq raCQ== X-Gm-Message-State: AOAM533LFTEdjzoHWjRUgdXkM2FekzSs6sB2Xlbu5oWoAczdDWZRYTgp cJ0d+qi/rvs+sywZsF9E+tou5g== X-Received: by 2002:adf:f346:: with SMTP id e6mr5572912wrp.179.1622631511395; Wed, 02 Jun 2021 03:58:31 -0700 (PDT) Received: from google.com (105.168.195.35.bc.googleusercontent.com. [35.195.168.105]) by smtp.gmail.com with ESMTPSA id p20sm2282579wmq.10.2021.06.02.03.58.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Jun 2021 03:58:30 -0700 (PDT) Date: Wed, 2 Jun 2021 10:58:28 +0000 From: Quentin Perret To: Yanan Wang Cc: Marc Zyngier , Will Deacon , Alexandru Elisei , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Catalin Marinas , James Morse , Julien Thierry , Suzuki K Poulose , Gavin Shan , wanghaibin.wang@huawei.com, zhukeqian1@huawei.com, yuzenghui@huawei.com Subject: Re: [PATCH v5 5/6] KVM: arm64: Move I-cache flush to the fault handlers Message-ID: References: <20210415115032.35760-1-wangyanan55@huawei.com> <20210415115032.35760-6-wangyanan55@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210415115032.35760-6-wangyanan55@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thursday 15 Apr 2021 at 19:50:31 (+0800), Yanan Wang wrote: > In this patch, we move invalidation of I-cache to the fault handlers to Nit: please avoid using 'This patch' in commit messages, see Documentation/process/submitting-patches.rst. > avoid unnecessary I-cache maintenances. On the map path, invalidate the > I-cache if we are going to create an executable stage-2 mapping for guest. > And on the permission path, invalidate the I-cache if we are going to add > an executable permission to the existing guest stage-2 mapping. > > Signed-off-by: Yanan Wang > --- > arch/arm64/include/asm/kvm_mmu.h | 15 -------------- > arch/arm64/kvm/hyp/pgtable.c | 35 +++++++++++++++++++++++++++++++- > arch/arm64/kvm/mmu.c | 9 +------- > 3 files changed, 35 insertions(+), 24 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h > index e9b163c5f023..155492fe5b15 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -187,21 +187,6 @@ static inline bool vcpu_has_cache_enabled(struct kvm_vcpu *vcpu) > return (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & 0b101) == 0b101; > } > > -static inline void __invalidate_icache_guest_page(kvm_pfn_t pfn, > - unsigned long size) > -{ > - if (icache_is_aliasing()) { > - /* any kind of VIPT cache */ > - __flush_icache_all(); > - } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) { > - /* PIPT or VPIPT at EL2 (see comment in __kvm_tlb_flush_vmid_ipa) */ > - void *va = page_address(pfn_to_page(pfn)); > - > - invalidate_icache_range((unsigned long)va, > - (unsigned long)va + size); > - } > -} > - > void kvm_set_way_flush(struct kvm_vcpu *vcpu); > void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled); > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index b480f6d1171e..9f4429d80df0 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -568,6 +568,26 @@ static bool stage2_pte_cacheable(struct kvm_pgtable *pgt, kvm_pte_t pte) > 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 void stage2_invalidate_icache(void *addr, u64 size) > +{ > + if (icache_is_aliasing()) { > + /* Any kind of VIPT cache */ > + __flush_icache_all(); > + } else if (is_kernel_in_hyp_mode() || !icache_is_vpipt()) { > + /* > + * See comment in __kvm_tlb_flush_vmid_ipa(). > + * Invalidate PIPT, or VPIPT at EL2. > + */ > + invalidate_icache_range((unsigned long)addr, > + (unsigned long)addr + size); > + } > +} > + > static void stage2_put_pte(kvm_pte_t *ptep, struct kvm_s2_mmu *mmu, u64 addr, > u32 level, struct kvm_pgtable_mm_ops *mm_ops) > { > @@ -618,6 +638,10 @@ static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > if (stage2_pte_cacheable(pgt, new) && !stage2_has_fwb(pgt)) > __flush_dcache_area(mm_ops->phys_to_virt(phys), > granule); > + > + if (stage2_pte_executable(new)) > + stage2_invalidate_icache(mm_ops->phys_to_virt(phys), > + granule); > } > > smp_store_release(ptep, new); > @@ -896,8 +920,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 the instruction cache before updating > + * if we are going to add the executable permission > + * for the guest stage-2 PTE. > + */ > + if (!stage2_pte_executable(*ptep) && stage2_pte_executable(pte)) > + stage2_invalidate_icache(kvm_pte_follow(pte, data->mm_ops), > + kvm_granule_size(level)); > WRITE_ONCE(*ptep, pte); > + } As for the dcache stuff, it seems like this would be best placed in an optional mm_ops callback, and have the kernel implement it. Thanks, Quentin