Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: arm64: Set PG_mte_tagged before copying MTE tags from userspace
@ 2026-11-24 11:02 Fuad Tabba
  0 siblings, 0 replies; only message in thread
From: Fuad Tabba @ 2026-11-24 11:02 UTC (permalink / raw)
  To: Marc Zyngier, Catalin Marinas
  Cc: Oliver Upton, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
	Zenghui Yu, Will Deacon, Mark Rutland, Steven Price,
	Peter Collingbourne, Cornelia Huck, linux-arm-kernel, kvmarm,
	linux-kernel, Fuad Tabba, stable

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.

Fixes: d77e59a8fccde ("arm64: mte: Lock a page for MTE tag initialisation")
Reported-by: Catalin Marinas <catalin.marinas@arm.com>
Closes: https://lore.kernel.org/all/auPCV6VdMEz4uZkK@arm.com/
Cc: stable@vger.kernel.org
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---

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(-)

diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
index 32392abf2e21c..d0f66426f139f 100644
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -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);
diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c
index b7d36c2abfdf9..82911d1287611 100644
--- a/arch/arm64/kvm/guest.c
+++ b/arch/arm64/kvm/guest.c
@@ -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);
 		}
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index a0d4981ebb998..9365d83a646d7 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -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
-- 
2.47.3



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-24 11:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 11:02 [PATCH] KVM: arm64: Set PG_mte_tagged before copying MTE tags from userspace Fuad Tabba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox