Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Fuad Tabba <fuad.tabba@linux.dev>
To: Marc Zyngier <maz@kernel.org>, Catalin Marinas <catalin.marinas@arm.com>
Cc: Oliver Upton <oupton@kernel.org>, Joey Gouly <joey.gouly@arm.com>,
	Steffen Eiden <seiden@linux.ibm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>, Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Steven Price <steven.price@arm.com>,
	Peter Collingbourne <peter@pcc.me.uk>,
	Cornelia Huck <cohuck@redhat.com>,
	linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
	linux-kernel@vger.kernel.org, Fuad Tabba <tabba@google.com>,
	stable@vger.kernel.org
Subject: [PATCH] KVM: arm64: Set PG_mte_tagged before copying MTE tags from userspace
Date: Tue, 24 Nov 2026 11:02:13 +0000	[thread overview]
Message-ID: <20261124110213.587871-1-fuad.tabba@linux.dev> (raw)

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



                 reply	other threads:[~2026-11-24 11:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124110213.587871-1-fuad.tabba@linux.dev \
    --to=fuad.tabba@linux.dev \
    --cc=catalin.marinas@arm.com \
    --cc=cohuck@redhat.com \
    --cc=joey.gouly@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=peter@pcc.me.uk \
    --cc=seiden@linux.ibm.com \
    --cc=stable@vger.kernel.org \
    --cc=steven.price@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=tabba@google.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox