KVM: arm64: Set PG_mte_tagged before copying MTE tags from userspace
Subsystems (3)
Fuad Tabba
reviewerPATCH 1/1 KVM: arm64: Set PG_mte_tagged before copying MTE tags from userspace
KVM_ARM_MTE_COPY_TAGS takes PG_mte_lock on a page's first tagging, and sets PG_mte_tagged only after copying the tags in from userspace. However, that copy faults on the same page if the VMM passes a PROT_MTE mapping of the page as the tags buffer. The fault then spins forever in mte_sync_tags(), waiting for PG_mte_tagged to be set.
Call sanitise_mte_tags() before the copy, so that PG_mte_tagged is already set if the copy faults. The ioctl calls it holding a reference on the page rather than kvm->mmu_lock, and either keeps the page from being freed. A racing vCPU fault or __set_ptes() then finds the page tagged and doesn't touch its tags. The guest and the VMM can read cleared tags from the page until the copy finishes.
---
Notes:
Based on kvmarm/fixes (012e4bc01e339), which has the MTE fixes this patch depends on.
I didn't go with Catalin's suggestion of a copy_from_user() into a local buffer, since there's no helper that writes tags held in the ioctl's one-tag-per-byte format from a kernel buffer to a page.
arch/arm64/include/asm/kvm_mmu.h | 1 + arch/arm64/kvm/guest.c | 18 ++++-------------- arch/arm64/kvm/mmu.c | 7 +++---- 3 files changed, 8 insertions(+), 18 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +1 -0
@@ -196,6 +196,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,int kvm_handle_guest_sea (struct kvm_vcpu *vcpu);int kvm_handle_guest_abort (struct kvm_vcpu *vcpu); +void sanitise_mte_tags (struct kvm *kvm,kvm_pfn_t pfn,unsigned long size);phys_addr_t kvm_mmu_get_httbr (void );phys_addr_t kvm_get_idmap_vector (void );
arch/arm64/kvm/guest.c +4 -14
@@ -24,6 +24,7 @@#include <asm/fpsimd.h> #include <asm/kvm.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_mmu.h> #include <asm/kvm_nested.h> #include <asm/sigcontext.h>
@@ -1057,27 +1058,16 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,kvm_release_page_clean (page); }else {/* -* Only locking to serialise with a concurrent -* __set_ptes() in the VMM but still overriding the -* tags. A hugetlb folio is marked tagged as a whole, -* so its first tagging clears every page. +* The user copy can fault on this page, and that fault +* spins until PG_mte_tagged is set, so set it first. */ -if (folio_test_hugetlb (folio)) { -if (folio_try_hugetlb_mte_tagging (folio)) -mte_clear_folio_tags (folio); - }else { -try_page_mte_tagging (page); - } +sanitise_mte_tags (kvm,page_to_pfn (page),PAGE_SIZE ); num_tags =mte_copy_tags_from_user (maddr, tags,MTE_GRANULES_PER_PAGE );/* uaccess failed, don't leave stale tags */ if (num_tags !=MTE_GRANULES_PER_PAGE )mte_clear_page_tags (maddr); -if (folio_test_hugetlb (folio)) -folio_set_hugetlb_mte_tagged (folio); -else -set_page_mte_tagged (page);kvm_release_page_dirty (page); }
arch/arm64/kvm/mmu.c +3 -4
@@ -1598,11 +1598,10 @@ static int get_vma_page_shift(struct vm_area_struct *vma)* able to see the page's tags and therefore they must be initialised first. If * PG_mte_tagged is set, tags have already been initialised. * -* Must be called with kvm->mmu_lock held to ensure the memory remains mapped -* while the tags are zeroed. +* The caller must hold kvm->mmu_lock or a reference on the page, so that the +* page isn't freed while the tags are zeroed. */ -static void sanitise_mte_tags (struct kvm *kvm,kvm_pfn_t pfn, -unsigned long size) +void sanitise_mte_tags (struct kvm *kvm,kvm_pfn_t pfn,unsigned long size) {unsigned long i, nr_pages = size >>PAGE_SHIFT ;struct page *page =pfn_to_page (pfn);
base-commit: 012e4bc01e339a65dc7c59181b5efb33b171d12c
Signature · 1 line
2.47.3