KVM: arm64: Skip CMOs when updating a PTE pointing to non-memory
Marc Zyngier
maintainerPATCH 1/1 KVM: arm64: Skip CMOs when updating a PTE pointing to non-memory
Sumit Gupta and Krishna Reddy both reported that for MMIO regions mapped into userspace using VFIO, a PTE update can trigger a MMU notifier reaching kvm_set_spte_hva().
There is an assumption baked in kvm_set_spte_hva() that it only deals with memory pages, and not MMIO. For this purpose, it performs a cache cleaning of the potentially newly mapped page. However, for a MMIO range, this explodes as there is no linear mapping for this range (and doing cache maintenance on it would make little sense anyway).
Check for the validity of the page before performing the CMO addresses the problem.
--- arch/arm64/kvm/mmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +2 -1
@@ -1236,7 +1236,8 @@ int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)* We've moved a page around, probably through CoW, so let's treat it * just like a translation fault and clean the cache to the PoC. */ -clean_dcache_guest_page (pfn,PAGE_SIZE ); +if (!kvm_is_device_pfn (pfn)) +clean_dcache_guest_page (pfn,PAGE_SIZE );handle_hva_to_gpa (kvm, hva, end, &kvm_set_spte_handler, &pfn);return 0 ; }
Signature · 1 line
2.30.2
Marc Zyngier
maintainerQuoted 11 lines · Marc Zyngier
Sumit Gupta and Krishna Reddy both reported that for MMIO regions mapped into userspace using VFIO, a PTE update can trigger a MMU notifier reaching
kvm_set_spte_hva().There is an assumption baked in
kvm_set_spte_hva()that it only deals with memory pages, and not MMIO. For this purpose, it performs a cache cleaning of the potentially newly mapped page. However, for a MMIO range, this explodes as there is no linear mapping for this range (and doing cache maintenance on it would make little sense anyway).
Quoted 8 lines
Check for the validity of the page before performing the CMO addresses the problem.
Reported-by Krishna Reddy <[email protected]>Reported-by Sumit Gupta <[email protected]>,Tested-by Sumit Gupta <[email protected]>,Signed-off-by Marc Zyngier <[email protected]>
FWIW, I've locally added:
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Hi,
I've been trying to reproduce the panic, but I haven't had any success.
With a known working PCI passtrough device, this is how I changed kvmtool:
vfio/core.c +0 -0
@@ -261,6 +261,9 @@ int vfio_map_region(struct kvm *kvm, struct vfio_device *vdev,
return ret;
}
+ char c = *(char *)base;
+ fprintf(stderr, "c = %c\n", c);
+
return 0;
}
What the change is doing is reading from the BAR region after it's has been mmap'ed into userspace. I can see that the read hits vfio_pci_mmap_fault(), which calls io_remap_pfn_range(), but I can't figure out how I can trigger the MMU notifiers. Any suggestions?
The comment [1] suggested that the panic is triggered during page aging. vfio_pci_mmap() sets the VM_PFNMAP for the VMA and I see in the Documentation that pages with VM_PFNMAP are added to the unevictable LRU list, doesn't that mean it's not subject the page aging? I feel like there's something I'm missing.
Thanks,
Alex
Quoted 37 lines · Marc Zyngier
Sumit Gupta and Krishna Reddy both reported that for MMIO regions mapped into userspace using VFIO, a PTE update can trigger a MMU notifier reaching
kvm_set_spte_hva().There is an assumption baked in
kvm_set_spte_hva()that it only deals with memory pages, and not MMIO. For this purpose, it performs a cache cleaning of the potentially newly mapped page. However, for a MMIO range, this explodes as there is no linear mapping for this range (and doing cache maintenance on it would make little sense anyway).Check for the validity of the page before performing the CMO addresses the problem.
Reported-by Krishna Reddy <[email protected]>Reported-by Sumit Gupta <[email protected]>,Tested-by Sumit Gupta <[email protected]>,Signed-off-by Marc Zyngier <[email protected]>--- arch/arm64/kvm/mmu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/kvm/mmu.c +2
-1@@ -1236,7 +1236,8 @@ int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)* We've moved a page around, probably through CoW, so let's treat it * just like a translation fault and clean the cache to the PoC. */ -clean_dcache_guest_page (pfn,PAGE_SIZE ); +if (!kvm_is_device_pfn (pfn)) +clean_dcache_guest_page (pfn,PAGE_SIZE );handle_hva_to_gpa (kvm, hva, end, &kvm_set_spte_handler, &pfn);return 0 ; }
Quoted 1 line · Alexandru Elisei
The comment [1] suggested that the panic is triggered during page aging.
I think only with an out-of-tree patch applied
https://jpbrucker.net/git/linux/commit/?h=sva/2021-03-01&id=d32d8baaf293aaefef8a1c9b8a4508ab2ec46c61
which probably is not going upstream.
Thanks,
Jean
Quoted 10 lines
vfio_pci_mmap()sets theVM_PFNMAPfor the VMA and I see in the Documentation that pages withVM_PFNMAPare added to the unevictable LRU list, doesn't that mean it's not subject the page aging? I feel like there's something I'm missing.Thanks,
Alex
Hi Jean,
Quoted 5 lines · Jean-Philippe Brucker
Quoted 1 line · Alexandru Elisei
The comment [1] suggested that the panic is triggered during page aging.
I think only with an out-of-tree patch applied
https://jpbrucker.net/git/linux/commit/?h=sva/2021-03-01&id=d32d8baaf293aaefef8a1c9b8a4508ab2ec46c61
which probably is not going upstream.
Thanks for that, that explains why I wasn't able to trigger the notification.
I did a grep for all the places where mmu_notifier_change_pte() and set_pte_at_notify() are used in the kernel, and from what I can tell they are only called for a new pte which has a struct page. From my investigation, the notifiers are called from ksm (which deals with physical memory), swap migration (so still pages in memory) and on copy-on-write.
On Linux v5.12, I tried to trigger the copy-on-write notification by forking kvmtool right after the BAR region is mapped and then reading from the userspace BAR address, but the new pte (for which the notifier is called) is valid.
I also looked at what x86 does, but I couldn't find where cache maintenance is performed (wouldn't surprise me if it's not necessary at all).
So I guess my question is what kind of pfns the MMU notifiers for the secondary MMUs are required to handle. If the requirement is that they should handle both device and struct page backed pfns, then the patch looks correct to me (user_mem_abort() also uses kvm_is_device_pfn() to decide if dcache maintenance is needed).
Thanks,
Alex
Quoted 13 lines
Thanks,
JeanQuoted 10 lines
vfio_pci_mmap()sets theVM_PFNMAPfor the VMA and I see in the Documentation that pages withVM_PFNMAPare added to the unevictable LRU list, doesn't that mean it's not subject the page aging? I feel like there's something I'm missing.Thanks,
Alex