Skip to contentSkip to conversation

KVM: arm64: Fix stale MTE tags reaching a guest

PATCH Applied Fuad Tabba 4 participants 12 messages Oct 21 – Nov 3

Subsystems (2)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

v2 cover KVM: arm64: Fix stale MTE tags reaching a guest

Hi folks,

Changes since v1 [1]:
- Added patch 4, mte_clear_folio_tags(). (Lorenzo)
- Moved patch 3's sanitise_mte_tags() call into the kvm_pgtable_stage2_map() branch. (Lorenzo)
- Patch 1 loops with folio_page() rather than reusing page, and its comment now matches patch 2's. (Lorenzo)
- Collected Reviewed-by from Lorenzo on patches 1-3. (thanks!)

Three fixes for hugetlb and THP pages that reach an MTE guest without their tags cleared, so the guest can read whatever tags the pages' previous owners left.

The first clears the whole of a hugetlb folio before marking it tagged, rather than only the pages being mapped. The second does the same when KVM_ARM_MTE_COPY_TAGS copies tags into a page. The third sanitises when a permission fault installs a THP block, and runs the VM_MTE_ALLOWED check on permission faults too. A VMM that maps guest memory with PROT_MTE isn't affected, as faulting that memory into its mapping clears the tags first. QEMU and kvmtool map it without.

The fourth is a cleanup: it factors out the first two patches' folio-clearing loop into mte_clear_folio_tags(), which mte_sync_tags() now uses too.

I have a reproducer that reads the previous owner's tag from 511 of 512 pages in each case. With this series applied, it reads none.

Based on Linux 7.3-rc3 (fd73f4a665989; also applies to kvmarm/next dd0bc00e3e920 with a three-way merge).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/

Fuad Tabba (4):
KVM: arm64: Clear the whole hugetlb folio when sanitising MTE tags
KVM: arm64: Clear the whole hugetlb folio when storing MTE tags
KVM: arm64: Sanitise MTE tags when a permission fault installs a
mapping
arm64: mte: Add mte_clear_folio_tags()

 arch/arm64/include/asm/mte.h |  4 ++++
 arch/arm64/kernel/mte.c      | 15 ++++++++++-----
 arch/arm64/kvm/guest.c       | 11 +++++++----
 arch/arm64/kvm/mmu.c         | 13 ++++++-------
 4 files changed, 27 insertions(+), 16 deletions(-)

base-commit: fd73f4a6659897191fa0d40695fe370925dd3780

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH v2 1/4 KVM: arm64: Clear the whole hugetlb folio when sanitising MTE tags

sanitise_mte_tags() clears the tags of the pages about to be mapped at stage-2 before marking them tagged. For a hugetlb folio the flag covers every page in the folio, but the loop only covers the mapping being installed. Stage-2 maps a folio in pieces for the contiguous hugetlb sizes, and for any size under dirty logging. The first fault then marks the whole folio after clearing only its own piece, and the rest keeps its previous owner's tags for the guest to read.

Clear the whole folio, as mte_sync_tags() does.

Fixes 25c17c4b55def ("hugetlb: arm64: add mte support")
---
 arch/arm64/kvm/mmu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +3 -3
index 9ba86450fe4af..a551b6bb13c21 100644
@@ -1512,10 +1512,10 @@ static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
 	}
 
 	if (folio_test_hugetlb(folio)) {
-		/* Hugetlb has MTE flags set on head page only */
+		/* A hugetlb folio is marked tagged as a whole, so clear every page */
 		if (folio_try_hugetlb_mte_tagging(folio)) {
-			for (i = 0; i < nr_pages; i++, page++)
-				mte_clear_page_tags(page_address(page));
+			for (i = 0; i < folio_nr_pages(folio); i++)
+				mte_clear_page_tags(page_address(folio_page(folio, i)));
 			folio_set_hugetlb_mte_tagged(folio);
 		}
 		return;
Signature · 1 line
2.47.3
maintainer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] KVM: arm64: Clear the whole hugetlb folio when sanitising MTE tags

Quoted 8 lines · Fuad Tabba

sanitise_mte_tags() clears the tags of the pages about to be mapped at stage-2 before marking them tagged. For a hugetlb folio the flag covers every page in the folio, but the loop only covers the mapping being installed. Stage-2 maps a folio in pieces for the contiguous hugetlb sizes, and for any size under dirty logging. The first fault then marks the whole folio after clearing only its own piece, and the rest keeps its previous owner's tags for the guest to read.

Quoted 6 lines

Clear the whole folio, as mte_sync_tags() does.

Fixes 25c17c4b55def ("hugetlb: arm64: add mte support")
author reviewer
#3 lore raw

PATCH v2 2/4 KVM: arm64: Clear the whole hugetlb folio when storing MTE tags

In reply to Fuad Tabba

When KVM_ARM_MTE_COPY_TAGS copies tags into a page, it marks the page as tagged. For a hugetlb folio the flag covers every page in the folio. If the folio wasn't tagged yet, its other pages are marked as tagged with their previous owner's tags still in place. The stage-2 fault path then maps them into the guest without clearing them. The ioctl's read side also copies them out to the VMM.

Clear the whole folio on its first tagging, before copying in this page's tags.

Fixes 25c17c4b55def ("hugetlb: arm64: add mte support")
---
 arch/arm64/kvm/guest.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
arch/arm64/kvm/guest.c +7 -4
index b01d6622b8720..cda655fbff657 100644
@@ -1012,6 +1012,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 		void *maddr;
 		unsigned long num_tags;
 		struct folio *folio;
+		unsigned long i;
 
 		if (!page) {
 			ret = -EFAULT;
@@ -1042,12 +1043,14 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 			/*
 			 * Only locking to serialise with a concurrent
 			 * __set_ptes() in the VMM but still overriding the
-			 * tags, hence ignoring the return value.
+			 * tags. A hugetlb folio is marked tagged as a whole,
+			 * so its first tagging clears every page.
 			 */
-			if (folio_test_hugetlb(folio))
-				folio_try_hugetlb_mte_tagging(folio);
-			else
+			if (!folio_test_hugetlb(folio))
 				try_page_mte_tagging(page);
+			else if (folio_try_hugetlb_mte_tagging(folio))
+				for (i = 0; i < folio_nr_pages(folio); i++)
+					mte_clear_page_tags(page_address(folio_page(folio, i)));
 			num_tags = mte_copy_tags_from_user(maddr, tags,
 							MTE_GRANULES_PER_PAGE);
 
Signature · 1 line
2.47.3
maintainer Reviewed-by on v2 2/4
#4 lore raw

Re: [PATCH v2 2/4] KVM: arm64: Clear the whole hugetlb folio when storing MTE tags

Quoted 36 lines · Fuad Tabba

When KVM_ARM_MTE_COPY_TAGS copies tags into a page, it marks the page as tagged. For a hugetlb folio the flag covers every page in the folio. If the folio wasn't tagged yet, its other pages are marked as tagged with their previous owner's tags still in place. The stage-2 fault path then maps them into the guest without clearing them. The ioctl's read side also copies them out to the VMM.

Clear the whole folio on its first tagging, before copying in this page's tags.

Fixes 25c17c4b55def ("hugetlb: arm64: add mte support")
---
 arch/arm64/kvm/guest.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
arch/arm64/kvm/guest.c +2 -1
index b01d6622b8720..cda655fbff657 100644
@@ -1012,6 +1012,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 		void *maddr;
 		unsigned long num_tags;
 		struct folio *folio;
+		unsigned long i;
 
 		if (!page) {
 			ret = -EFAULT;
@@ -1042,12 +1043,14 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 			/*
 			 * Only locking to serialise with a concurrent
 			 * __set_ptes() in the VMM but still overriding the
-			 * tags, hence ignoring the return value.
+			 * tags. A hugetlb folio is marked tagged as a whole,

arch/arm64/kvm/guest.c int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, · line 1053

Quoted 10 lines
arch/arm64/kvm/guest.c +5 -3
+			 * so its first tagging clears every page.
 			 */
-			if (folio_test_hugetlb(folio))
-				folio_try_hugetlb_mte_tagging(folio);
-			else
+			if (!folio_test_hugetlb(folio))
 				try_page_mte_tagging(page);
+			else if (folio_try_hugetlb_mte_tagging(folio))
+				for (i = 0; i < folio_nr_pages(folio); i++)
+					mte_clear_page_tags(page_address(folio_page(folio, i)));

I guess we could have skipped the actual page we are going to copy into but not worth it.

arch/arm64/kvm/guest.c int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, · line 1055

Quoted 2 lines
arch/arm64/kvm/guest.c +0 -0
 			num_tags = mte_copy_tags_from_user(maddr, tags,
 							MTE_GRANULES_PER_PAGE);

Something Claude spotted and unrelated to your patch. A malicious VMM using memfd (not guest_memfd) and MAP_SHARED could construct some mapping so that the VM slot is !PROT_MTE while the same page is mapped in the VMM with PROT_MTE. try_page_mte_tagging() (or the folio one) above set PG_mte_lock but a subsequent fault on the uaccess to VMM memory will attempt to zero the tags and wait (forever) on the page lock. Not tested to prove it.

If you get bored and want to fix it, I guess a copy_from_user() into a local buffer and then setting the page tags.

Signature · 1 line
Catalin
author reviewer
#5 lore raw

Re: [PATCH v2 2/4] KVM: arm64: Clear the whole hugetlb folio when storing MTE tags

On Thu, 29 Oct 2026 at 22:02, Catalin Marinas <[email protected]> wrote:
[...]

Quoted 28 lines

arch/arm64/kvm/guest.c int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, · line 1053

Quoted 11 lines
arch/arm64/kvm/guest.c +6 -3
+                      * tags. A hugetlb folio is marked tagged as a whole,
+                      * so its first tagging clears every page.
                       */
-                     if (folio_test_hugetlb(folio))
-                             folio_try_hugetlb_mte_tagging(folio);
-                     else
+                     if (!folio_test_hugetlb(folio))
                              try_page_mte_tagging(page);
+                     else if (folio_try_hugetlb_mte_tagging(folio))
+                             for (i = 0; i < folio_nr_pages(folio); i++)
+                                     mte_clear_page_tags(page_address(folio_page(folio, i)));

I guess we could have skipped the actual page we are going to copy into but not worth it.

Quoted 2 lines

num_tags = mte_copy_tags_from_user(maddr, tags,
MTE_GRANULES_PER_PAGE);

Something Claude spotted and unrelated to your patch. A malicious VMM using memfd (not guest_memfd) and MAP_SHARED could construct some mapping so that the VM slot is !PROT_MTE while the same page is mapped in the VMM with PROT_MTE. try_page_mte_tagging() (or the folio one) above set PG_mte_lock but a subsequent fault on the uaccess to VMM memory will attempt to zero the tags and wait (forever) on the page lock. Not tested to prove it.

Quoted 2 lines

If you get bored and want to fix it, I guess a copy_from_user() into a local buffer and then setting the page tags.

Thanks Catalin. I'll look into this and send a fix.

Cheers,
/fuad

author reviewer
#6 lore raw

PATCH v2 3/4 KVM: arm64: Sanitise MTE tags when a permission fault installs a mapping

In reply to Fuad Tabba

The stage-2 fault path sanitises MTE tags only on translation faults, as a permission fault normally just relaxes an existing leaf. However, when the page is backed by a THP, a permission fault at page granule installs a block whose other pages may never have been mapped or sanitised. Once dirty logging is off, a guest write to a page mapped read-only during logging installs the block, and the guest can read the tags those pages' previous owners left.

Sanitise whenever the fault path calls kvm_pgtable_stage2_map() rather than relaxing permissions. If the VMM replaces the VMA after the guest takes a permission fault but before vma_lookup(), that path would map and sanitise memory from the new VMA without checking it. Run the VM_MTE_ALLOWED check in kvm_s2_fault_compute_prot() on permission faults too.

Fixes ea7fc1bb1cd1b ("KVM: arm64: Introduce MTE VM feature")
---
 arch/arm64/kvm/mmu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +4 -4
index a551b6bb13c21..6e10aa9913d71 100644
@@ -2004,7 +2004,7 @@ static int kvm_s2_fault_compute_prot(const struct kvm_s2_fault_desc *s2fd,
 	if (s2fd->nested)
 		*prot = adjust_nested_exec_perms(kvm, s2fd->nested, *prot);
 
-	if (!kvm_s2_fault_is_perm(s2fd) && !s2vi->map_non_cacheable && kvm_has_mte(kvm)) {
+	if (!s2vi->map_non_cacheable && kvm_has_mte(kvm)) {
 		/* Check the VMM hasn't introduced a new disallowed VMA */
 		if (!s2vi->mte_allowed)
 			return -EFAULT;
@@ -2059,9 +2059,6 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 		}
 	}
 
-	if (!perm_fault_granule && !s2vi->map_non_cacheable && kvm_has_mte(kvm))
-		sanitise_mte_tags(kvm, pfn, mapping_size);
-
 	/*
 	 * Under the premise of getting a FSC_PERM fault, we just need to relax
 	 * permissions only if mapping_size equals perm_fault_granule. Otherwise,
@@ -2076,6 +2073,9 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,
 		ret = KVM_PGT_FN(kvm_pgtable_stage2_relax_perms)(pgt, gfn_to_gpa(gfn),
 								 prot, flags);
 	} else {
+		if (!s2vi->map_non_cacheable && kvm_has_mte(kvm))
+			sanitise_mte_tags(kvm, pfn, mapping_size);
+
 		ret = KVM_PGT_FN(kvm_pgtable_stage2_map)(pgt, gfn_to_gpa(gfn), mapping_size,
 							 __pfn_to_phys(pfn), prot,
 							 memcache, flags);
Signature · 1 line
2.47.3
maintainer Reviewed-by on v2 3/4
#7 lore raw

Re: [PATCH v2 3/4] KVM: arm64: Sanitise MTE tags when a permission fault installs a mapping

Quoted 15 lines · Fuad Tabba

The stage-2 fault path sanitises MTE tags only on translation faults, as a permission fault normally just relaxes an existing leaf. However, when the page is backed by a THP, a permission fault at page granule installs a block whose other pages may never have been mapped or sanitised. Once dirty logging is off, a guest write to a page mapped read-only during logging installs the block, and the guest can read the tags those pages' previous owners left.

Sanitise whenever the fault path calls kvm_pgtable_stage2_map() rather than relaxing permissions. If the VMM replaces the VMA after the guest takes a permission fault but before vma_lookup(), that path would map and sanitise memory from the new VMA without checking it. Run the VM_MTE_ALLOWED check in kvm_s2_fault_compute_prot() on permission faults too.

Quoted 4 lines
Fixes ea7fc1bb1cd1b ("KVM: arm64: Introduce MTE VM feature")
author reviewer
#8 lore raw

PATCH v2 4/4 arm64: mte: Add mte_clear_folio_tags()

In reply to Fuad Tabba

mte_sync_tags(), sanitise_mte_tags() and kvm_vm_ioctl_mte_copy_tags() each open-code the loop that clears the tags of every page in a hugetlb folio. Add mte_clear_folio_tags() and use it in all three. kvm_vm_ioctl_mte_copy_tags() then tests for hugetlb first, as it does where it sets the tagged flag below.

No functional change intended.

---
 arch/arm64/include/asm/mte.h |  4 ++++
 arch/arm64/kernel/mte.c      | 15 ++++++++++-----
 arch/arm64/kvm/guest.c       | 10 +++++-----
 arch/arm64/kvm/mmu.c         |  3 +--
 4 files changed, 20 insertions(+), 12 deletions(-)
arch/arm64/include/asm/mte.h +4 -0
index 7f7b97e099968..5a2d2814c0416 100644
@@ -96,6 +96,7 @@ static inline bool try_page_mte_tagging(struct page *page)
 }
 
 void mte_zero_clear_page_tags(void *addr);
+void mte_clear_folio_tags(struct folio *folio);
 void mte_sync_tags(pte_t pte, unsigned int nr_pages);
 void mte_copy_page_tags(void *kto, const void *kfrom);
 void mte_thread_init_user(void);
@@ -128,6 +129,9 @@ static inline bool try_page_mte_tagging(struct page *page)
 static inline void mte_zero_clear_page_tags(void *addr)
 {
 }
+static inline void mte_clear_folio_tags(struct folio *folio)
+{
+}
 static inline void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 {
 }
arch/arm64/kernel/mte.c +10 -5
index 1a9aad6ef22a0..fb2598f8c0ccd 100644
@@ -36,6 +36,14 @@ DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
 EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode);
 #endif
 
+void mte_clear_folio_tags(struct folio *folio)
+{
+	unsigned long i;
+
+	for (i = 0; i < folio_nr_pages(folio); i++)
+		mte_clear_page_tags(page_address(folio_page(folio, i)));
+}
+
 void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 {
 	struct page *page = pte_page(pte);
@@ -43,12 +51,9 @@ void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 	unsigned long i;
 
 	if (folio_test_hugetlb(folio)) {
-		unsigned long nr = folio_nr_pages(folio);
-
-		/* Hugetlb MTE flags are set for head page only */
+		/* A hugetlb folio is marked tagged as a whole, so clear every page */
 		if (folio_try_hugetlb_mte_tagging(folio)) {
-			for (i = 0; i < nr; i++, page++)
-				mte_clear_page_tags(page_address(page));
+			mte_clear_folio_tags(folio);
 			folio_set_hugetlb_mte_tagged(folio);
 		}
 
arch/arm64/kvm/guest.c +5 -5
index cda655fbff657..c75466384f8ac 100644
@@ -1012,7 +1012,6 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 		void *maddr;
 		unsigned long num_tags;
 		struct folio *folio;
-		unsigned long i;
 
 		if (!page) {
 			ret = -EFAULT;
@@ -1046,11 +1045,12 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 			 * tags. A hugetlb folio is marked tagged as a whole,
 			 * so its first tagging clears every page.
 			 */
-			if (!folio_test_hugetlb(folio))
+			if (folio_test_hugetlb(folio)) {
+				if (folio_try_hugetlb_mte_tagging(folio))
+					mte_clear_folio_tags(folio);
+			} else {
 				try_page_mte_tagging(page);
-			else if (folio_try_hugetlb_mte_tagging(folio))
-				for (i = 0; i < folio_nr_pages(folio); i++)
-					mte_clear_page_tags(page_address(folio_page(folio, i)));
+			}
 			num_tags = mte_copy_tags_from_user(maddr, tags,
 							MTE_GRANULES_PER_PAGE);
 
arch/arm64/kvm/mmu.c +1 -2
index 6e10aa9913d71..f8139c0a54deb 100644
@@ -1514,8 +1514,7 @@ static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
 	if (folio_test_hugetlb(folio)) {
 		/* A hugetlb folio is marked tagged as a whole, so clear every page */
 		if (folio_try_hugetlb_mte_tagging(folio)) {
-			for (i = 0; i < folio_nr_pages(folio); i++)
-				mte_clear_page_tags(page_address(folio_page(folio, i)));
+			mte_clear_folio_tags(folio);
 			folio_set_hugetlb_mte_tagged(folio);
 		}
 		return;
Signature · 1 line
2.47.3
Reviewed-by on v2 4/4
#9 lore raw

Re: [PATCH v2 4/4] arm64: mte: Add mte_clear_folio_tags()

Quoted 10 lines · Fuad Tabba

mte_sync_tags(), sanitise_mte_tags() and kvm_vm_ioctl_mte_copy_tags() each open-code the loop that clears the tags of every page in a hugetlb folio. Add mte_clear_folio_tags() and use it in all three. kvm_vm_ioctl_mte_copy_tags() then tests for hugetlb first, as it does where it sets the tagged flag below.

No functional change intended.

Great LGTM so:

Quoted 107 lines
---
 arch/arm64/include/asm/mte.h |  4 ++++
 arch/arm64/kernel/mte.c      | 15 ++++++++++-----
 arch/arm64/kvm/guest.c       | 10 +++++-----
 arch/arm64/kvm/mmu.c         |  3 +--
 4 files changed, 20 insertions(+), 12 deletions(-)
arch/arm64/include/asm/mte.h +4 -0
index 7f7b97e099968..5a2d2814c0416 100644
@@ -96,6 +96,7 @@ static inline bool try_page_mte_tagging(struct page *page)
 }
 
 void mte_zero_clear_page_tags(void *addr);
+void mte_clear_folio_tags(struct folio *folio);
 void mte_sync_tags(pte_t pte, unsigned int nr_pages);
 void mte_copy_page_tags(void *kto, const void *kfrom);
 void mte_thread_init_user(void);
@@ -128,6 +129,9 @@ static inline bool try_page_mte_tagging(struct page *page)
 static inline void mte_zero_clear_page_tags(void *addr)
 {
 }
+static inline void mte_clear_folio_tags(struct folio *folio)
+{
+}
 static inline void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 {
 }
arch/arm64/kernel/mte.c +10 -5
index 1a9aad6ef22a0..fb2598f8c0ccd 100644
@@ -36,6 +36,14 @@ DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
 EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode);
 #endif
 
+void mte_clear_folio_tags(struct folio *folio)
+{
+	unsigned long i;
+
+	for (i = 0; i < folio_nr_pages(folio); i++)
+		mte_clear_page_tags(page_address(folio_page(folio, i)));
+}
+
 void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 {
 	struct page *page = pte_page(pte);
@@ -43,12 +51,9 @@ void mte_sync_tags(pte_t pte, unsigned int nr_pages)
 	unsigned long i;
 
 	if (folio_test_hugetlb(folio)) {
-		unsigned long nr = folio_nr_pages(folio);
-
-		/* Hugetlb MTE flags are set for head page only */
+		/* A hugetlb folio is marked tagged as a whole, so clear every page */
 		if (folio_try_hugetlb_mte_tagging(folio)) {
-			for (i = 0; i < nr; i++, page++)
-				mte_clear_page_tags(page_address(page));
+			mte_clear_folio_tags(folio);
 			folio_set_hugetlb_mte_tagged(folio);
 		}
 
arch/arm64/kvm/guest.c +5 -5
index cda655fbff657..c75466384f8ac 100644
@@ -1012,7 +1012,6 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 		void *maddr;
 		unsigned long num_tags;
 		struct folio *folio;
-		unsigned long i;
 
 		if (!page) {
 			ret = -EFAULT;
@@ -1046,11 +1045,12 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm,
 			 * tags. A hugetlb folio is marked tagged as a whole,
 			 * so its first tagging clears every page.
 			 */
-			if (!folio_test_hugetlb(folio))
+			if (folio_test_hugetlb(folio)) {
+				if (folio_try_hugetlb_mte_tagging(folio))
+					mte_clear_folio_tags(folio);
+			} else {
 				try_page_mte_tagging(page);
-			else if (folio_try_hugetlb_mte_tagging(folio))
-				for (i = 0; i < folio_nr_pages(folio); i++)
-					mte_clear_page_tags(page_address(folio_page(folio, i)));
+			}
 			num_tags = mte_copy_tags_from_user(maddr, tags,
 							MTE_GRANULES_PER_PAGE);
 
arch/arm64/kvm/mmu.c +1 -3
index 6e10aa9913d71..f8139c0a54deb 100644
@@ -1514,8 +1514,7 @@ static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
 	if (folio_test_hugetlb(folio)) {
 		/* A hugetlb folio is marked tagged as a whole, so clear every page */
 		if (folio_try_hugetlb_mte_tagging(folio)) {
-			for (i = 0; i < folio_nr_pages(folio); i++)
-				mte_clear_page_tags(page_address(folio_page(folio, i)));
+			mte_clear_folio_tags(folio);
 			folio_set_hugetlb_mte_tagged(folio);
 		}
 		return;
--
Quoted 2 lines

2.47.3

--
Cheers, Lorenzo

maintainer Reviewed-by on v2 4/4
#10 lore raw

Re: [PATCH v2 4/4] arm64: mte: Add mte_clear_folio_tags()

In reply to Fuad Tabba
Quoted 10 lines · Fuad Tabba

mte_sync_tags(), sanitise_mte_tags() and kvm_vm_ioctl_mte_copy_tags() each open-code the loop that clears the tags of every page in a hugetlb folio. Add mte_clear_folio_tags() and use it in all three. kvm_vm_ioctl_mte_copy_tags() then tests for hugetlb first, as it does where it sets the tagged flag below.

No functional change intended.

maintainer Applied
#11 lore raw
In reply to Fuad Tabba
Quoted 9 lines · Fuad Tabba

Changes since v1 [1]:
- Added patch 4, mte_clear_folio_tags(). (Lorenzo)
- Moved patch 3's sanitise_mte_tags() call into the kvm_pgtable_stage2_map() branch. (Lorenzo)
- Patch 1 loops with folio_page() rather than reusing page, and its comment now matches patch 2's. (Lorenzo)
- Collected Reviewed-by from Lorenzo on patches 1-3. (thanks!)

[...]

Applied to fixes, thanks!

[1/4] KVM: arm64: Clear the whole hugetlb folio when sanitising MTE tags

      commit: cbf47fe57a568f178ac55176d98a13caba1d8598
[2/4] KVM: arm64: Clear the whole hugetlb folio when storing MTE tags
      commit: 62b9e1f75a3c39cfc38b3950bbad017ac5f95bcc
[3/4] KVM: arm64: Sanitise MTE tags when a permission fault installs a mapping
      commit: 249e053eaacb2959794a5ee3d27737063f4e3dad
[4/4] arm64: mte: Add mte_clear_folio_tags()
      commit: da9f24dc43361b1cdcbba2fee7b90f4706819fa7

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.