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

Yan Zhao
2023-05-30T10:13:31Z

On Thu, May 25, 2023 at 08:54:15AM -0700, Sean Christopherson wrote:
> > > 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.
Hi Sean,
start_or_end gave me a first impression that it stands for gfn_start, gfn_end.
so currently I changed it to start/stop to avoid confusion.

+static void kvm_noncoherent_dma_assignment_start_or_stop(struct kvm *kvm)
+{
+ /*
+ * Only necessary to zap KVM TDP if guest MTRR is honored.
+ * As whether a VM has noncoherent DMA can affect whether
+ * KVM honors guest MTRR and the resulting memory type in TDP,
+ * specify its value as true here to test if guest MTRR
+ * is honored after the assignment starts or
+ * was honored before the assignment stops.
+ */
+ if (kvm_mmu_honors_guest_mtrrs(kvm, true))
+ kvm_zap_gfn_range(kvm, 0, ~0ULL);
+}

And I combined the __kvm_mmu_honors_guest_mtrrs() into
kvm_mmu_honors_guest_mtrrs(). Not sure if you like it :)

+/*
+ * Returns if KVM honors guest MTRRs
+ * @override_vm_has_noncoherent_dma: Allow caller to override non-coherent DMA
+ * status returned from
+ * kvm_arch_has_noncoherent_dma()
+ */
+bool kvm_mmu_honors_guest_mtrrs(struct kvm *kvm,
+ bool override_vm_has_noncoherent_dma)
+{
+ bool noncoherent_dma = override_vm_has_noncoherent_dma ? true :
+ kvm_arch_has_noncoherent_dma(kvm);
+
+ /*
+ * If the TDP is enabled, the host MTRRs are ignored by TDP
+ * (shadow_memtype_mask is non-zero), and the VM has non-coherent DMA
+ * (DMA doesn't snoop CPU caches), KVM's ABI is to honor the memtype
+ * from the guest's MTRRs so that guest accesses to memory that is
+ * DMA'd aren't cached against the guest's wishes.
+ *
+ * Note, KVM may still ultimately ignore guest MTRRs for certain PFNs,
+ * e.g. KVM will force UC memtype for host MMIO.
+ */
+ return noncoherent_dma && tdp_enabled && shadow_memtype_mask;
+}
+

Thanks
Yan