Re: [PATCH 06/24] kvm: x86/mmu: Skip no-op changes in TDP MMU functions

From: Ben Gardon
Date: Mon Jan 25 2021 - 18:53:03 EST


On Wed, Jan 20, 2021 at 11:51 AM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
>
> On Tue, Jan 12, 2021, Ben Gardon wrote:
> > Skip setting SPTEs if no change is expected.
> >
> > Reviewed-by: Peter Feiner <pfeiner@xxxxxxxxxx>
> >
> Nit on all of these, can you remove the extra newline between the Reviewed-by
> and SOB?

Yeah, that line is annoying. I'll make sure it's not there on future patches.

>
> > Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
> > ---
> > arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++
> > 1 file changed, 6 insertions(+)
> >
> > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> > index 1987da0da66e..2650fa9fe066 100644
> > --- a/arch/x86/kvm/mmu/tdp_mmu.c
> > +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> > @@ -882,6 +882,9 @@ static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
> > !is_last_spte(iter.old_spte, iter.level))
> > continue;
> >
> > + if (!(iter.old_spte & PT_WRITABLE_MASK))
>
> Include the new check with the existing if statement? I think it makes sense to
> group all the checks on old_spte.

I agree that' s cleaner. I'll group the checks in the next patch set version.

>
> > + continue;
> > +
> > new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
> >
> > tdp_mmu_set_spte_no_dirty_log(kvm, &iter, new_spte);
> > @@ -1079,6 +1082,9 @@ static bool set_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
> > if (!is_shadow_present_pte(iter.old_spte))
> > continue;
> >
> > + if (iter.old_spte & shadow_dirty_mask)
>
> Same comment here.
>
> > + continue;
> > +
>
> Unrelated to this patch, but it got me looking at the code: shouldn't
> clear_dirty_pt_masked() clear the bit in @mask before checking whether or not
> the spte needs to be modified? That way the early break kicks in after sptes
> are checked, not necessarily written. E.g.
>
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 2650fa9fe066..d8eeae910cbf 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -1010,21 +1010,21 @@ static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,
> !(mask & (1UL << (iter.gfn - gfn))))
> continue;
>
> - if (wrprot || spte_ad_need_write_protect(iter.old_spte)) {
> - if (is_writable_pte(iter.old_spte))
> - new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
> - else
> - continue;
> - } else {
> - if (iter.old_spte & shadow_dirty_mask)
> - new_spte = iter.old_spte & ~shadow_dirty_mask;
> - else
> - continue;
> - }
> -
> - tdp_mmu_set_spte_no_dirty_log(kvm, &iter, new_spte);
> -
> mask &= ~(1UL << (iter.gfn - gfn));
> +
> + if (wrprot || spte_ad_need_write_protect(iter.old_spte)) {
> + if (is_writable_pte(iter.old_spte))
> + new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
> + else
> + continue;
> + } else {
> + if (iter.old_spte & shadow_dirty_mask)
> + new_spte = iter.old_spte & ~shadow_dirty_mask;
> + else
> + continue;
> + }
> +
> + tdp_mmu_set_spte_no_dirty_log(kvm, &iter, new_spte);
> }
> }
>

Great point, that doesn't work as intended at all. I'll adopt your
proposed fix and include it in a patch after this one in the next
version of the series.

>
> > new_spte = iter.old_spte | shadow_dirty_mask;
> >
> > tdp_mmu_set_spte(kvm, &iter, new_spte);
> > --
> > 2.30.0.284.gd98b1dd5eaa7-goog
> >