From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.229 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795518141; b=EsZwFOSt1sXhRwKiahdH5hBOs/s3b9wD/qcR6ocZKmdbVI42ThmKVXLdJeDLfynp/s+X e9RV2klSYgFsVLxF6/hB6AYMzHgCR4VAI+iaONaLcuA31DFadZ6j+A+GOkHtN7dynCYSq 4xjMlEbWEzQXyhyWPQeJhCJ8QXPRdyvdxUj9VXkYow3DK6d0U3z9saHDGP3RnaJM2db14 2MWsGczemiXCYh9a7KoAhKie0mAbNWXsqHE4H8i8w4GI7OmQdZIvNGHvM4D1akrXEAhrZ D6XtZ8Hjxn7OhbBwwC6d47b4JMO5c6pj4pQkRRdnxXnc8rm55K+khVGI1QDphbGnTeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795518141; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=KAvHK6j0hREgOFUkVu/pGTkn7fy5PDfJjXV7BZ0L4ak=; b=2qGLGEOwMO46Kv5fa2RXLRj9BQ8RjEOMJH6UOI84vfeFUIQm73DLRgwopcCivT8fkGip lBV8lhGrqHjlOURTTVWkNDcCqdlY7IkLumg1C87kcLVjRQpMn+YK7SZBmnjQYfP1uDFuy ExEtvlvbiAlYCE16JAC1lV0XrHQpQmXJ2Z37lIC1TPaeiaCnebDrgd9LtkOd9LcXvp/7P 9eRJSNcerau/8L4a5EdQP5Aq95m8LnyYcHzNiInRTNhpCco0XeQiZVu6POvG+hHVlkKC2 hCHUzl0as3RblP7g0QZEKXetTx9/D8R3PCfYtgdgoFV1aEhiG/p3is77cz8L82QvPcQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=mHAjw4tY; arc=none smtp.remote-ip=95.215.58.229 Received: from mta1.migadu.com (out-229.mta1.migadu.com [95.215.58.229]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE37A606A31E for ; Tue, 24 Nov 2026 11:02:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AE37A606A31E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=mHAjw4tY X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=YQOhx28BDze68y36zy90w+D/2RW09RYxRy95T7kvqVE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795518138; v=1; x=1796122938; b=mHAjw4tYzSAbLJMfkcEYcq0YIf63aj6uOu6qCOkBwPC9lFDTox4JZQHX+hqk/29Qy48Ta4AE 0zsD0hoswVQYR/4upBbAsovAW+PpPOlVCjGJs5pNJR8Mh18Y0wTATB4fnlphyx7P24tdcpzC7BG 8UvW+9cJhuO/KG1gieRzi5LY= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id e2ce8471a6143467; Tue, 24 Nov 2026 11:02:18 +0000 X-Mizu-Trace-ID: e2ce8471a6143467 X-Migadu-Flow: FLOW_OUT From: Fuad Tabba 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@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Fuad Tabba , 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 Message-ID: <20261124110213.587871-1-fuad.tabba@linux.dev> X-Mailer: git-send-email 2.47.3 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Closes: https://lore.kernel.org/all/auPCV6VdMEz4uZkK@arm.com/ Cc: stable@vger.kernel.org Signed-off-by: Fuad Tabba --- 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 #include #include +#include #include #include @@ -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