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 <zhukeqian1@xxxxxxxxxx> --- 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