Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp1335559ybs; Mon, 25 May 2020 13:18:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfxV4EQIMq0mNcOkH211GH4Nx6fI1T2QavWZl8ReI6K+NUWOzQdnof1LQWzwYhp8fGrUc2 X-Received: by 2002:a17:906:8304:: with SMTP id j4mr19992554ejx.200.1590437904949; Mon, 25 May 2020 13:18:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590437904; cv=none; d=google.com; s=arc-20160816; b=eESb1rFVNBcdrcpk9mZc7J9m3c2gNSrGZmqBLeGs0EfqFG+S9PZBF7uIMJT9IKpEF+ x5MPsHmGRqydzOBtyWOKdKjdPbG6xsr51FrSC2Lp8fnFFZQyVGu27j83TxudgHE32BYN FmfuXgpxWYyDScS5kCYBCbEVE3iQvGa8NQekpVbhgOFraJENhHeb26LLK8NWL3OgFeAH anCIYmS0jL/Ij5bleWqXskeUp9cfRAZl2Be7H/GpJflcPa+UjWWykpyw5NnG0+lDt4Hj tBv9muZTn56pnX1iDYGiQQYROLA6lBgY5oGMebKDXp7/t5lmf/C4sVMki0V6iB0msTG/ ez/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=vWJAvIfLp/ls3CU6ri71WvGMS84EgdwciUt3+sRgt6w=; b=Upvs2Rj2M7WnB3IW8TlmmIDrk2oaggIN/RMjkjjP8szMMgBwLVrQ08/QRHDcmHqASw uLHzqHLJmgj77re6y/Ap6QflO6pIXuT2w6ieG6bVpm3Rk3Xmro97lEopBYkzHIi+eWAe 4DZ4q/7g6yyMl2t7usRPYuDr/j3xnSbQQDc19yfa/IM5tHFqTx7v21YgPXtUGcZC9/+Z AchcC+R1QsLZeyMzGtUa2QN+uxZlbkmBwXcTYlbrOF/VUSdNChAtDI7ItUTHSNE68kPg 6/cJncTfQ36Y/kdeHG/LMhqU3U0Kl8trmm75UbYINLJX8F0HtoKODB1EwnoUf4HC29AK SvaQ== 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 d20si10706719edj.13.2020.05.25.13.18.01; Mon, 25 May 2020 13:18:24 -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; 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 S2390187AbgEYLZZ (ORCPT + 99 others); Mon, 25 May 2020 07:25:25 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:47114 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2390173AbgEYLZR (ORCPT ); Mon, 25 May 2020 07:25:17 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 9506F6CC1EAF105C267D; Mon, 25 May 2020 19:25:14 +0800 (CST) Received: from DESKTOP-5IS4806.china.huawei.com (10.173.221.230) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Mon, 25 May 2020 19:25:07 +0800 From: Keqian Zhu To: , , , CC: Catalin Marinas , Marc Zyngier , James Morse , Will Deacon , "Suzuki K Poulose" , Sean Christopherson , Julien Thierry , Mark Brown , "Thomas Gleixner" , Andrew Morton , Alexios Zavras , , , Keqian Zhu Subject: [RFC PATCH 6/7] kvm: arm64: Save stage2 PTE dirty info if it is coverred Date: Mon, 25 May 2020 19:24:05 +0800 Message-ID: <20200525112406.28224-7-zhukeqian1@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 In-Reply-To: <20200525112406.28224-1-zhukeqian1@huawei.com> References: <20200525112406.28224-1-zhukeqian1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.173.221.230] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org kvm_set_pte is called to replace a target PTE with a desired one. We always replace it, but if hw DBM is enalbled and dirty info is coverred, should let caller know it. Caller can decide to whether save the dirty info. kvm_set_pmd and kvm_set_pud is not modified, because we only use DBM in PTEs for now. Signed-off-by: Keqian Zhu --- virt/kvm/arm/mmu.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c index e1d9e4b98cb6..43d89c6333f0 100644 --- a/virt/kvm/arm/mmu.c +++ b/virt/kvm/arm/mmu.c @@ -185,10 +185,34 @@ static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr put_page(virt_to_page(pmd)); } -static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte) +/* + * @ret: true if dirty info is coverred. + */ +static inline bool kvm_set_pte(pte_t *ptep, pte_t new_pte) { +#ifdef CONFIG_ARM64_HW_AFDBM + pteval_t old_pteval, new_pteval, pteval; + + if (!kvm_hw_dbm_enabled() || pte_none(*ptep) || + !kvm_s2pte_readonly(&new_pte)) { + WRITE_ONCE(*ptep, new_pte); + dsb(ishst); + return false; + } + + new_pteval = pte_val(new_pte); + pteval = READ_ONCE(pte_val(*ptep)); + do { + old_pteval = pteval; + pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, new_pteval); + } while (pteval != old_pteval); + + return !kvm_s2pte_readonly((pte_t *)&pteval); +#else WRITE_ONCE(*ptep, new_pte); dsb(ishst); + return false; +#endif } static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd) @@ -249,7 +273,10 @@ static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd, if (!pte_none(*pte)) { pte_t old_pte = *pte; - kvm_set_pte(pte, __pte(0)); + if (kvm_set_pte(pte, __pte(0))) { + mark_page_dirty(kvm, addr >> PAGE_SHIFT); + } + kvm_tlb_flush_vmid_ipa(kvm, addr); /* No need to invalidate the cache for device mappings */ @@ -1291,13 +1318,17 @@ static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, if (pte_val(old_pte) == pte_val(*new_pte)) return 0; - kvm_set_pte(pte, __pte(0)); + if (kvm_set_pte(pte, __pte(0))) { + mark_page_dirty(kvm, addr >> PAGE_SHIFT); + } kvm_tlb_flush_vmid_ipa(kvm, addr); } else { get_page(virt_to_page(pte)); } - kvm_set_pte(pte, *new_pte); + if (kvm_set_pte(pte, *new_pte)) { + mark_page_dirty(kvm, addr >> PAGE_SHIFT); + } return 0; } -- 2.19.1