Re: [PATCH v2 1/6] KVM: x86/mmu: add a new mmu zap helper to indicate memtype changes

Sean Christopherson
2023-05-25T15:55:02Z

On Thu, May 25, 2023, Yan Zhao wrote:
> On Wed, May 24, 2023 at 07:50:24AM -0700, Sean Christopherson wrote:
> > bool __kvm_mmu_honors_guest_mtrrs(struct kvm *kvm, bool vm_has_noncoherent_dma);
> >
> > static inline bool kvm_mmu_honors_guest_mtrrs(struct kvm *kvm)
> > {
> >
> > return __kvm_mmu_honors_guest_mtrrs(kvm, kvm_arch_has_noncoherent_dma(kvm));
> > }
>
> This should work and it centralizes the comments into one place, though I dislike
> having to pass true as vm_has_noncoherent_dma in case of 1->0 transition. :)

Yeah, I don't love it either, but the whole 1=>0 transition is awkward. FWIW,
KVM doesn't strictly need to zap in that case since the guest isn't relying on
WB for functionality, i.e. we could just skip it.

> > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > > index 41d7bb51a297..ad0c43d7f532 100644
> > > --- a/arch/x86/kvm/x86.c
> > > +++ b/arch/x86/kvm/x86.c
> > > @@ -13146,13 +13146,19 @@ EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
> > >
> > > void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
> > > {
> > > - atomic_inc(&kvm->arch.noncoherent_dma_count);
> > > + if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) {
> > > + if (kvm_mmu_cap_honors_guest_mtrrs(kvm))
> > > + kvm_zap_gfn_range(kvm, 0, ~0ULL);
> >
> > No need for multiple if statements. Though rather than have identical code in
> > both the start/end paths, how about this? That provides a single location for a
> > comment. Or maybe first/last instead of start/end?
> >
> > static void kvm_noncoherent_dma_start_or_end(struct kvm *kvm)
> What does start_or_end or first_or_last stand for?

Start/End of device (un)assignment, or First/Last device (un)assigned. Definitely
feel free to pick a better name.