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=-11.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SIGNED_OFF_BY,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 E6180C433E7 for ; Thu, 3 Sep 2020 04:34:54 +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 AA16320716 for ; Thu, 3 Sep 2020 04:34:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="gkwTIJa+"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QDQ+3no2" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AA16320716 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com 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-Type: Content-Transfer-Encoding:Cc:Reply-To:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date: Message-ID:From:References:To:Subject:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JQpNezQtj+FBzDBDOLbYfDN/a34jyni/dicponcIIXY=; b=gkwTIJa+3MDLCr rnolgsWfqWiuoA54Kexk/+XQgEs9emidCCtAvzRpgE4ER8r0C/0R38RzctRTH48z6bCe1QKjytnpO +wUSfHn05MbpovX9yiNb3kwyMjEEraXr031oy5ykBSogFt1dyB+qzMrSIAxuOvZAsUEVCyj6LUzv8 5NcFDUOJmJ7gLUIlpYQZYO5XVxS93mXx2yL4p+vWLHeMnsABZzMPslq1mHa/LMNTdLoRjzDc24U6q IoS6agOhw5HchgvsOWXuonbtnmHiFZibzSHH6Yij7bRPOtofqYx69KX3MBA6mTLwaSBpExVu5rqIa X1itS2389bsTKgA6JU8Q==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kDgwK-0003Zx-ST; Thu, 03 Sep 2020 04:33:37 +0000 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120] helo=us-smtp-1.mimecast.com) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kDgwH-0003ZJ-4S for linux-arm-kernel@lists.infradead.org; Thu, 03 Sep 2020 04:33:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599107612; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vt7kfHGkGj9zGg+0//Wby/pFzlHEV7ADUQ2DWJ1V+M0=; b=QDQ+3no2rdxNEsU4K6DU+YcG444cX8pQSK50FF/1w9j9VxhVvtcqyE1CkdwsgSDW/9C2Fo DAmyd3sdAyjma3ijkvXZU/gwHE0GDHYy+BqnU0RBUp3SJ1oaFV8CnOqqkugc/8e/lzlarl w509I4yQFGFwdUDsiw5g+13JuIEdpTQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-276-Qs2VcZiiNQCJnOGmj2DyWg-1; Thu, 03 Sep 2020 00:33:28 -0400 X-MC-Unique: Qs2VcZiiNQCJnOGmj2DyWg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2277618A2256; Thu, 3 Sep 2020 04:33:27 +0000 (UTC) Received: from [10.64.54.159] (vpn2-54-159.bne.redhat.com [10.64.54.159]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8B3587EB72; Thu, 3 Sep 2020 04:33:24 +0000 (UTC) Subject: Re: [PATCH v3 10/21] KVM: arm64: Add support for stage-2 page-aging in generic page-table To: Will Deacon , kvmarm@lists.cs.columbia.edu References: <20200825093953.26493-1-will@kernel.org> <20200825093953.26493-11-will@kernel.org> From: Gavin Shan Message-ID: Date: Thu, 3 Sep 2020 14:33:22 +1000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.0 MIME-Version: 1.0 In-Reply-To: <20200825093953.26493-11-will@kernel.org> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=gshan@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200903_003333_215612_899CCA06 X-CRM114-Status: GOOD ( 30.59 ) 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: , Reply-To: Gavin Shan Cc: Suzuki Poulose , Marc Zyngier , Quentin Perret , James Morse , Catalin Marinas , kernel-team@android.com, linux-arm-kernel@lists.infradead.org Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Will, On 8/25/20 7:39 PM, Will Deacon wrote: > Add stage-2 mkyoung(), mkold() and is_young() operations to the generic > page-table code. > > Cc: Marc Zyngier > Cc: Quentin Perret > Signed-off-by: Will Deacon > --- > arch/arm64/include/asm/kvm_pgtable.h | 38 ++++++++++++ > arch/arm64/kvm/hyp/pgtable.c | 86 ++++++++++++++++++++++++++++ > 2 files changed, 124 insertions(+) > With the following one question resolved: Reviewed-by: Gavin Shan > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h > index 8ab0d5f43817..ae56534f87a0 100644 > --- a/arch/arm64/include/asm/kvm_pgtable.h > +++ b/arch/arm64/include/asm/kvm_pgtable.h > @@ -173,6 +173,44 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, > */ > int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size); > > +/** > + * kvm_pgtable_stage2_mkyoung() - Set the access flag in a page-table entry. > + * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init(). > + * @addr: Intermediate physical address to identify the page-table entry. > + * > + * If there is a valid, leaf page-table entry used to translate @addr, then > + * set the access flag in that entry. > + * > + * Return: The old page-table entry prior to setting the flag, 0 on failure. > + */ > +kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr); > + > +/** > + * kvm_pgtable_stage2_mkold() - Clear the access flag in a page-table entry. > + * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init(). > + * @addr: Intermediate physical address to identify the page-table entry. > + * > + * If there is a valid, leaf page-table entry used to translate @addr, then > + * clear the access flag in that entry. > + * > + * Note that it is the caller's responsibility to invalidate the TLB after > + * calling this function to ensure that the updated permissions are visible > + * to the CPUs. > + * > + * Return: The old page-table entry prior to clearing the flag, 0 on failure. > + */ > +kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr); > + > +/** > + * kvm_pgtable_stage2_is_young() - Test whether a page-table entry has the > + * access flag set. > + * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init(). > + * @addr: Intermediate physical address to identify the page-table entry. > + * > + * Return: True if the page-table entry has the access flag set, false otherwise. > + */ > +bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr); > + > /** > * kvm_pgtable_walk() - Walk a page-table. > * @pgt: Page-table structure initialised by kvm_pgtable_*_init(). > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 6f65d3841ec9..30713eb773e0 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -670,6 +670,92 @@ int kvm_pgtable_stage2_unmap(struct kvm_pgtable *pgt, u64 addr, u64 size) > return kvm_pgtable_walk(pgt, addr, size, &walker); > } > > +struct stage2_attr_data { > + kvm_pte_t attr_set; > + kvm_pte_t attr_clr; > + kvm_pte_t pte; > +}; > + > +static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, > + enum kvm_pgtable_walk_flags flag, > + void * const arg) > +{ > + kvm_pte_t pte = *ptep; > + struct stage2_attr_data *data = arg; > + > + if (!kvm_pte_valid(pte)) > + return 0; > + > + data->pte = pte; > + pte &= ~data->attr_clr; > + pte |= data->attr_set; > + > + /* > + * We may race with the CPU trying to set the access flag here, > + * but worst-case the access flag update gets lost and will be > + * set on the next access instead. > + */ > + if (data->pte != pte) > + WRITE_ONCE(*ptep, pte); > + > + return 0; > +} > + > +static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr, > + u64 size, kvm_pte_t attr_set, > + kvm_pte_t attr_clr, kvm_pte_t *orig_pte) > +{ > + int ret; > + kvm_pte_t attr_mask = KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI; > + struct stage2_attr_data data = { > + .attr_set = attr_set & attr_mask, > + .attr_clr = attr_clr & attr_mask, > + }; > + struct kvm_pgtable_walker walker = { > + .cb = stage2_attr_walker, > + .arg = &data, > + .flags = KVM_PGTABLE_WALK_LEAF, > + }; > + > + ret = kvm_pgtable_walk(pgt, addr, size, &walker); > + if (ret) > + return ret; > + > + if (orig_pte) > + *orig_pte = data.pte; > + return 0; > +} > + The @size is always 1 from the caller, which means the parameter can be dropped from stage2_update_leaf_attrs(). In the meanwhile, we don't know the page is mapped by PUD, PMD or PTE. So to have fixed value ("1") looks meaningless. > +kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr) > +{ > + kvm_pte_t pte = 0; > + stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0, > + &pte); > + dsb(ishst); > + return pte; > +} > + > +kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr) > +{ > + kvm_pte_t pte = 0; > + stage2_update_leaf_attrs(pgt, addr, 1, 0, KVM_PTE_LEAF_ATTR_LO_S2_AF, > + &pte); > + /* > + * "But where's the TLBI?!", you scream. > + * "Over in the core code", I sigh. > + * > + * See the '->clear_flush_young()' callback on the KVM mmu notifier. > + */ > + return pte; > +} > + > +bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr) > +{ > + kvm_pte_t pte = 0; > + stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte); > + return pte & KVM_PTE_LEAF_ATTR_LO_S2_AF; > +} > + > int kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm *kvm) > { > size_t pgd_sz; > Thanks, Gavin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel