Re: [PATCH v18 044/121] KVM: x86/mmu: Assume guest MMIOs are shared

From: Isaku Yamahata
Date: Mon Feb 26 2024 - 13:06:39 EST


On Mon, Feb 12, 2024 at 11:29:51AM +0100,
Paolo Bonzini <pbonzini@xxxxxxxxxx> wrote:

> On Tue, Jan 23, 2024 at 12:55 AM <isaku.yamahata@xxxxxxxxx> wrote:
> >
> > From: Chao Gao <chao.gao@xxxxxxxxx>
> >
> > Guest TD doesn't necessarily invoke MAP_GPA to convert the virtual MMIO
> > range to shared before accessing it. When TD tries to access the virtual
> > device's MMIO as shared, an EPT violation is raised first.
> > kvm_mem_is_private() checks whether the GFN is shared or private. If
> > MAP_GPA is not called for the GPA, KVM thinks the GPA is private and
> > refuses shared access, and doesn't set up shared EPT entry. The guest
> > can't make progress.
> >
> > Instead of requiring the guest to invoke MAP_GPA for regions of virtual
> > MMIOs assume regions of virtual MMIOs are shared in KVM as well (i.e., GPAs
> > either have no kvm_memory_slot or are backed by host MMIOs). So that guests
> > can access those MMIO regions.
>
> I'm not sure how the patch below deals with host MMIOs?

It falls back to shared case to hit KVM_PFN_NOSLOT. It will be handled as
MMIO.

Anyway I found it breaks SW_PROTECTED case. So I came up with the following.
I think we'd like to handle as
- SW_PROTECTED => KVM_EXIT_MEMORY_FAULT
- SNP, TDX => MMIO.



- if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
+ /*
+ * !fault->slot means MMIO for SNP and TDX. Don't require explicit GPA
+ * conversion for MMIO because MMIO is assigned at the boot time. Fall
+ * to !is_private case to get pfn = KVM_PFN_NOSLOT.
+ */
+ force_mmio = !slot &&
+ vcpu->kvm->arch.vm_type != KVM_X86_DEFAULT_VM &&
+ vcpu->kvm->arch.vm_type != KVM_X86_SW_PROTECTED_VM;
+ if (!force_mmio &&
+ fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) {
kvm_mmu_prepare_memory_fault_exit(vcpu, fault);
return -EFAULT;
}

- if (fault->is_private)
+ if (!force_mmio && fault->is_private)
return kvm_faultin_pfn_private(vcpu, fault);

--
Isaku Yamahata <isaku.yamahata@xxxxxxxxxxxxxxx>