Re: [RFC PATCH] KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler

From: Marc Zyngier
Date: Tue Dec 15 2020 - 08:19:40 EST


Hi Yanan,

On 2020-12-14 07:20, wangyanan (Y) wrote:

diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index a74a62283012..e3c6133567c4 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -45,6 +45,10 @@

 #define KVM_PTE_LEAF_ATTR_HI_S2_XN     BIT(54)

+#define KVM_PTE_LEAF_ATTR_S2_PERMS (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
+        KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
+ KVM_PTE_LEAF_ATTR_HI_S2_XN)
+
 struct kvm_pgtable_walk_data {
        struct kvm_pgtable                   *pgt;
        struct kvm_pgtable_walker       *walker;
@@ -473,8 +477,13 @@ static bool stage2_map_walker_try_leaf(u64 addr,
u64 end, u32 level,

        new = kvm_init_valid_leaf_pte(phys, data->attr, level);
        if (kvm_pte_valid(old)) {
-               /* Tolerate KVM recreating the exact same mapping. */
-               if (old == new)
+               /*
+                * Skip updating the PTE with break-before-make if we are trying
+                * to recreate the exact same mapping or only change the access
+                * permissions. Actually, change of permissions will be handled
+                * through the relax_perms path next time if necessary.
+                */
+               if (!((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS)))
                        goto out;

                /* There's an existing different valid leaf entry, so perform

I think there is a bit more work to do on this.

One obvious issue is that we currently flag a page as dirty before handling
the fault. With an early exit, we end-up having spurious dirty pages.

It's not a big deal, but I'd rather mark the page dirty after the mapping
or the permission update having been successful (at the moment, it cannot
fails).

Thanks,

M.
--
Jazz is not dead. It just smells funny...