Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: PPC: Use kvm_vcpu_map() to map guest memory to patch dcbz instructions

Use kvm_vcpu_map() when patching dcbz in guest memory, as a regular GUP
isn't technically sufficient when writing to data in the target pages.
As per Documentation/core-api/pin_user_pages.rst:

Correct (uses FOLL_PIN calls):
pin_user_pages()
write to the data within the pages
unpin_user_pages()

INCORRECT (uses FOLL_GET calls):
get_user_pages()
write to the data within the pages
put_page()

As a happy bonus, using kvm_vcpu_{,un}map() takes care of creating a
mapping and marking the page dirty.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Tested-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-ID: <20241010182427.1434605-75-seanjc@google.com>

authored by

Sean Christopherson and committed by
Paolo Bonzini
ee0fa693 17b7dbaf

+6 -7
+6 -7
arch/powerpc/kvm/book3s_pr.c
··· 639 639 */ 640 640 static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) 641 641 { 642 - struct page *hpage; 642 + struct kvm_host_map map; 643 643 u64 hpage_offset; 644 644 u32 *page; 645 - int i; 645 + int i, r; 646 646 647 - hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); 648 - if (!hpage) 647 + r = kvm_vcpu_map(vcpu, pte->raddr >> PAGE_SHIFT, &map); 648 + if (r) 649 649 return; 650 650 651 651 hpage_offset = pte->raddr & ~PAGE_MASK; 652 652 hpage_offset &= ~0xFFFULL; 653 653 hpage_offset /= 4; 654 654 655 - page = kmap_atomic(hpage); 655 + page = map.hva; 656 656 657 657 /* patch dcbz into reserved instruction, so we trap */ 658 658 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++) 659 659 if ((be32_to_cpu(page[i]) & 0xff0007ff) == INS_DCBZ) 660 660 page[i] &= cpu_to_be32(0xfffffff7); 661 661 662 - kunmap_atomic(page); 663 - put_page(hpage); 662 + kvm_vcpu_unmap(vcpu, &map); 664 663 } 665 664 666 665 static bool kvmppc_visible_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)