Skip to contentSkip to conversation

mm/huge_memory: Initialise the tags of the huge zero folio

PATCH In review Catalin Marinas 10 participants 24 messages Oct 31, 2025 – Nov 24, 2025

Subsystems (5)
lore
Share & tools
lore

Catalin Marinas

author maintainer
#0 lore raw

PATCH 1/1 mm/huge_memory: Initialise the tags of the huge zero folio

On arm64 with MTE enabled, a page mapped as Normal Tagged (PROT_MTE) in user space will need to have its allocation tags initialised. This is normally done in the arm64 set_pte_at() after checking the memory attributes. Such page is also marked with the PG_mte_tagged flag to avoid subsequent clearing. Since this relies on having a struct page, pte_special() mappings are ignored.

Commit d82d09e48219 ("mm/huge_memory: mark PMD mappings of the huge zero folio special") maps the huge zero folio special and the arm64 set_pmd_at() will no longer zero the tags. There is no guarantee that the tags are zero, especially if parts of this huge page have been previously tagged.

Allocate the huge zero folio with the __GFP_ZEROTAGS flag. In addition, do not warn in the arm64 __access_remote_tags() when reading tags from the huge zero page.

Fixes d82d09e48219 ("mm/huge_memory: mark PMD mappings of the huge zero folio special")
Cc David Hildenbrand <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Will Deacon <[email protected]>

---

It's fairly easy to detect this by regularly dropping the caches to force the reallocation of the huge zero folio. I bundled the arm64 change in here as well since they are both related to the commit mapping the huge zero folio as special.

I don't have any preference how this patch goes in, either the mm tree or the arm64 fixes one is fine by me.

 arch/arm64/kernel/mte.c | 3 ++-
 mm/huge_memory.c        | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +2 -1
index 43f7a2f39403..32148bf09c1d 100644
@@ -476,7 +476,8 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
 
 		folio = page_folio(page);
 		if (folio_test_hugetlb(folio))
-			WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio));
+			WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio) &&
+				     !is_huge_zero_folio(folio));
 		else
 			WARN_ON_ONCE(!page_mte_tagged(page) && !is_zero_page(page));
 
mm/huge_memory.c +2 -1
index 1b81680b4225..b7498e51282a 100644
@@ -214,7 +214,8 @@ static bool get_huge_zero_folio(void)
 	if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
 		return true;
 
-	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
+	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
+				 ~__GFP_MOVABLE,
 			HPAGE_PMD_ORDER);
 	if (!zero_folio) {
 		count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Acked-by on v1 1/1
#1 lore raw
Quoted 17 lines · Catalin Marinas

On arm64 with MTE enabled, a page mapped as Normal Tagged (PROT_MTE) in user space will need to have its allocation tags initialised. This is normally done in the arm64 set_pte_at() after checking the memory attributes. Such page is also marked with the PG_mte_tagged flag to avoid subsequent clearing. Since this relies on having a struct page, pte_special() mappings are ignored.

Commit d82d09e48219 ("mm/huge_memory: mark PMD mappings of the huge zero folio special") maps the huge zero folio special and the arm64 set_pmd_at() will no longer zero the tags. There is no guarantee that the tags are zero, especially if parts of this huge page have been previously tagged.

Allocate the huge zero folio with the __GFP_ZEROTAGS flag. In addition, do not warn in the arm64 __access_remote_tags() when reading tags from the huge zero page.

Quoted 5 lines
Fixes d82d09e48219 ("mm/huge_memory: mark PMD mappings of the huge zero folio special")
Cc David Hildenbrand <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Will Deacon <[email protected]>

Thanks, Catalin!

Signature · 3 lines
Cheers

David / dhildenb
#2 lore raw
In reply to Catalin Marinas
Quoted 6 lines · Catalin Marinas

On arm64 with MTE enabled, a page mapped as Normal Tagged (PROT_MTE) in user space will need to have its allocation tags initialised. This is normally done in the arm64 set_pte_at() after checking the memory attributes. Such page is also marked with the PG_mte_tagged flag to avoid subsequent clearing. Since this relies on having a struct page, pte_special() mappings are ignored.

We are seeing breakage in userspace on a range of arm64 platforms which bisects to this commit in -next. We see traces like:

[   59.746701] Internal error: Oops - Undefined instruction: 0000000002000000 [#1]  SMP

...

[   59.819007] pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   59.826055] pc : mte_zero_clear_page_tags+0x1c/0x40
[   59.830980] lr : tag_clear_highpage+0x68/0x118

...

[   59.911874] Call trace:
[   59.914333]  mte_zero_clear_page_tags+0x1c/0x40 (P)
[   59.919278]  get_page_from_freelist+0x1a60/0x1c80
[   59.924042]  __alloc_frozen_pages_noprof+0x178/0xd20
[   59.929068]  alloc_pages_mpol+0xb4/0x1a4
[   59.933022]  alloc_frozen_pages_noprof+0x48/0xc0
[   59.937683]  folio_alloc_noprof+0x14/0x68
[   59.941718]  mm_get_huge_zero_folio+0xf4/0x30c
[   59.946200]  do_huge_pmd_anonymous_page+0x278/0x6a0
[   59.951119]  __handle_mm_fault+0x700/0x1834
[   59.955332]  handle_mm_fault+0x8c/0x2a0
[   59.959190]  do_page_fault+0x108/0x75c
[   59.962964]  do_translation_fault+0x5c/0x6c
[   59.967181]  do_mem_abort+0x40/0x90

Looking at the codes:

mm/huge_memory.c static bool get_huge_zero_folio(void) · line 219

Quoted 4 lines
mm/huge_memory.c +2 -1
-       zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
+       zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
+                                ~__GFP_MOVABLE,
                        HPAGE_PMD_ORDER);

This adds an unonditional __GFP_ZEROTAGS - from a quick scan it looks like this was previously only enabled by vma_alloc_zeroed_movable_folio() when the VMA has VM_MTE, I think we need a similar test here.

Full log:

https://lava.sirena.org.uk/scheduler/job/2036941#L1423

Sample bisect log (with links to further runtime logs:

# bad: [3575af345aa2424636e69ac101a568bda249abe6] Merge branch 'i2c/i2c-host-fixes' of https://git.kernel.org/pub/scm/linux/kernel/git/andi.shyti/linux.git
# good: [6146a0f1dfae5d37442a9ddcba012add260bceb0] Linux 6.18-rc4
git bisect start '3575af345aa2424636e69ac101a568bda249abe6' '6146a0f1dfae5d37442a9ddcba012add260bceb0'

# test job: [3575af345aa2424636e69ac101a568bda249abe6] https://lava.sirena.org.uk/scheduler/job/2036941
# bad: [3575af345aa2424636e69ac101a568bda249abe6] Merge branch 'i2c/i2c-host-fixes' of https://git.kernel.org/pub/scm/linux/kernel/git/andi.shyti/linux.git
git bisect bad 3575af345aa2424636e69ac101a568bda249abe6
# test job: [81f07f151b0759666fee3184651aab90ef1c5ed5] https://lava.sirena.org.uk/scheduler/job/2037418
# bad: [81f07f151b0759666fee3184651aab90ef1c5ed5] Merge branch 'usb-linus' of https://git.kernel.org/pub/scm/linux/kernel/git/johan/usb-serial.git
git bisect bad 81f07f151b0759666fee3184651aab90ef1c5ed5
# test job: [661821dc41b20c505d5adad10bb71fe5746c57fb] https://lava.sirena.org.uk/scheduler/job/2037588
# bad: [661821dc41b20c505d5adad10bb71fe5746c57fb] Merge branch 'fixes' of https://git.kernel.org/pub/scm/linux/kernel/git/krzk/linux-mem-ctrl.git
git bisect bad 661821dc41b20c505d5adad10bb71fe5746c57fb
# test job: [972cbfc499182c200bd3d2fb8fe4173df0d4d3e7] https://lava.sirena.org.uk/scheduler/job/2037773
# bad: [972cbfc499182c200bd3d2fb8fe4173df0d4d3e7] mm/huge_memory: initialise the tags of the huge zero folio
git bisect bad 972cbfc499182c200bd3d2fb8fe4173df0d4d3e7
# test job: [6923ff319e1b0d3541cd240b6ed494e1cb6287d1] https://lava.sirena.org.uk/scheduler/job/2037885
# good: [6923ff319e1b0d3541cd240b6ed494e1cb6287d1] fs/proc: fix uaf in proc_readdir_de()
git bisect good 6923ff319e1b0d3541cd240b6ed494e1cb6287d1
# test job: [e356021a7589f6dd1c946b86c933462dca0bc1ec] https://lava.sirena.org.uk/scheduler/job/2037973
# good: [e356021a7589f6dd1c946b86c933462dca0bc1ec] codetag: debug: handle existing CODETAG_EMPTY in mark_objexts_empty for slabobj_ext
git bisect good e356021a7589f6dd1c946b86c933462dca0bc1ec
# test job: [decb83d6239894a14b07c8f5ebf9feb5c87ec651] https://lava.sirena.org.uk/scheduler/job/2038008
# good: [decb83d6239894a14b07c8f5ebf9feb5c87ec651] mm/damon/sysfs: change next_update_jiffies to a global variable
git bisect good decb83d6239894a14b07c8f5ebf9feb5c87ec651
# test job: [53cf2716958490997b8ce11cc4665256905b23dd] https://lava.sirena.org.uk/scheduler/job/2038076
# good: [53cf2716958490997b8ce11cc4665256905b23dd] nilfs2: avoid having an active sc_timer before freeing sci
git bisect good 53cf2716958490997b8ce11cc4665256905b23dd
# first bad commit: [972cbfc499182c200bd3d2fb8fe4173df0d4d3e7] mm/huge_memory: initialise the tags of the huge zero folio
author maintainer
#3 lore raw
Quoted 22 lines · Mark Brown
Quoted 6 lines · Catalin Marinas

On arm64 with MTE enabled, a page mapped as Normal Tagged (PROT_MTE) in user space will need to have its allocation tags initialised. This is normally done in the arm64 set_pte_at() after checking the memory attributes. Such page is also marked with the PG_mte_tagged flag to avoid subsequent clearing. Since this relies on having a struct page, pte_special() mappings are ignored.

We are seeing breakage in userspace on a range of arm64 platforms which bisects to this commit in -next. We see traces like:

[   59.746701] Internal error: Oops - Undefined instruction: 0000000002000000 [#1]  SMP

...

[   59.819007] pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   59.826055] pc : mte_zero_clear_page_tags+0x1c/0x40
[   59.830980] lr : tag_clear_highpage+0x68/0x118

...

Quoted 14 lines
[   59.911874] Call trace:
[   59.914333]  mte_zero_clear_page_tags+0x1c/0x40 (P)
[   59.919278]  get_page_from_freelist+0x1a60/0x1c80
[   59.924042]  __alloc_frozen_pages_noprof+0x178/0xd20
[   59.929068]  alloc_pages_mpol+0xb4/0x1a4
[   59.933022]  alloc_frozen_pages_noprof+0x48/0xc0
[   59.937683]  folio_alloc_noprof+0x14/0x68
[   59.941718]  mm_get_huge_zero_folio+0xf4/0x30c
[   59.946200]  do_huge_pmd_anonymous_page+0x278/0x6a0
[   59.951119]  __handle_mm_fault+0x700/0x1834
[   59.955332]  handle_mm_fault+0x8c/0x2a0
[   59.959190]  do_page_fault+0x108/0x75c
[   59.962964]  do_translation_fault+0x5c/0x6c
[   59.967181]  do_mem_abort+0x40/0x90

Thanks for the report. I missed the fact that the arch mte_zero_clear_page_tags() arch code issues MTE instructions irrespective of whether the hardware supports it. We got away with this so far since we check the VM_MTE flag and that's only set if the hardware supports MTE.

Quoted 10 lines

Looking at the codes:

Review

Quoted 4 lines
(quoted diff) +2 -1
-       zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
+       zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
+                                ~__GFP_MOVABLE,
                        HPAGE_PMD_ORDER);

This adds an unonditional __GFP_ZEROTAGS - from a quick scan it looks like this was previously only enabled by vma_alloc_zeroed_movable_folio() when the VMA has VM_MTE, I think we need a similar test here.

We can't do this for the huge zero page since this will be shared by other vmas and not all would have VM_MTE set. I'll fix it in the arch code:

-----------8<---------------------------------------------

arch/arm64/mm/fault.c +10 -0
index d816ff44faff..125dfa6c613b 100644
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 
 void tag_clear_highpage(struct page *page)
 {
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}
+
 	/* Newly allocated page, shouldn't have been tagged yet */
 	WARN_ON_ONCE(!try_page_mte_tagging(page));
 	mte_zero_clear_page_tags(page_address(page));

------------------8<------------------------------------------

Testing now.

Signature · 1 line
Catalin
#4 lore raw
Quoted 10 lines

-----------8<---------------------------------------------

arch/arm64/mm/fault.c +0 -0
index d816ff44faff..125dfa6c613b 100644
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 
  void tag_clear_highpage(struct page *page)
  {

arch/arm64/mm/fault.c struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, · line 981

Quoted 10 lines
arch/arm64/mm/fault.c +10 -0
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}
+

LGTM!

Signature · 3 lines
Cheers

David
author maintainer
#5 lore raw
Quoted 10 lines · David Hildenbrand (Red Hat)
Quoted 10 lines
arch/arm64/mm/fault.c +3 -0
index d816ff44faff..125dfa6c613b 100644
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
  void tag_clear_highpage(struct page *page)
  {
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
Quoted 8 lines

arch/arm64/mm/fault.c struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, · line 980

Quoted 6 lines
arch/arm64/mm/fault.c +6 -0
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}

LGTM!

I tested it with and without MTE and it works fine.

Andrew, would you like a separate patch or are you ok with folding this into the previous patch?

Thanks.

Signature · 1 line
Catalin
#6 lore raw
Quoted 18 lines · Catalin Marinas
Quoted 9 lines · David Hildenbrand (Red Hat)
Quoted 9 lines
arch/arm64/mm/fault.c +2 -0
index d816ff44faff..125dfa6c613b 100644
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
   void tag_clear_highpage(struct page *page)
   {
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
Quoted 8 lines

arch/arm64/mm/fault.c struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, · line 980

Quoted 7 lines
arch/arm64/mm/fault.c +7 -0
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}

LGTM!

Quoted 1 line

I tested it with and without MTE and it works fine.

I tested the above patch on ARM64 based TI J7200 EVM board. Boots fine. Feel free to use my T/B:

Thanks,
Beleswar

Quoted 6 lines

Andrew, would you like a separate patch or are you ok with folding this into the previous patch?

Thanks.

maintainer
#7 lore raw
In reply to Catalin Marinas
Quoted 17 lines · On Mon, 3 Nov 2025 15:59:39 +0000 Catalin Marinas <[email protected]>…
Quoted 8 lines
Quoted 8 lines
arch/arm64/mm/fault.c +3 -0
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
  void tag_clear_highpage(struct page *page)
  {
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
Quoted 8 lines

arch/arm64/mm/fault.c struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, · line 980

Quoted 6 lines
arch/arm64/mm/fault.c +6 -0
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}

LGTM!

Quoted 4 lines

I tested it with and without MTE and it works fine.

Andrew, would you like a separate patch or are you ok with folding this into the previous patch?

I added it as a -fix patch thanks.

And I added a Signed-off-by-you-by-me ;)

author maintainer
#8 lore raw
Quoted 18 lines · Andrew Morton
Quoted 17 lines · On Mon, 3 Nov 2025 15:59:39 +0000 Catalin Marinas <[email protected]>…
Quoted 16 lines
Quoted 4 lines
arch/arm64/mm/fault.c +0 -0
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
  void tag_clear_highpage(struct page *page)

arch/arm64/mm/fault.c struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, · line 979

Quoted 10 lines
arch/arm64/mm/fault.c +9 -0
  {
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}

LGTM!

Quoted 8 lines
Quoted 4 lines

I tested it with and without MTE and it works fine.

Andrew, would you like a separate patch or are you ok with folding this into the previous patch?

I added it as a -fix patch thanks.

And I added a Signed-off-by-you-by-me ;)

Thanks Andrew.

Signature · 1 line
Catalin
Reviewed-by
#9 lore raw

Re: [PATCH] mm/huge_memory: Initialise the tags of the huge zero

In reply to Catalin Marinas

From: Lance Yang <[email protected]>

Quoted 83 lines · Catalin Marinas
Quoted 22 lines · Mark Brown
Quoted 6 lines · Catalin Marinas

On arm64 with MTE enabled, a page mapped as Normal Tagged (PROT_MTE) in user space will need to have its allocation tags initialised. This is normally done in the arm64 set_pte_at() after checking the memory attributes. Such page is also marked with the PG_mte_tagged flag to avoid subsequent clearing. Since this relies on having a struct page, pte_special() mappings are ignored.

We are seeing breakage in userspace on a range of arm64 platforms which bisects to this commit in -next. We see traces like:

[   59.746701] Internal error: Oops - Undefined instruction: 0000000002000000 [#1]  SMP

...

[   59.819007] pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   59.826055] pc : mte_zero_clear_page_tags+0x1c/0x40
[   59.830980] lr : tag_clear_highpage+0x68/0x118

...

Quoted 14 lines
[   59.911874] Call trace:
[   59.914333]  mte_zero_clear_page_tags+0x1c/0x40 (P)
[   59.919278]  get_page_from_freelist+0x1a60/0x1c80
[   59.924042]  __alloc_frozen_pages_noprof+0x178/0xd20
[   59.929068]  alloc_pages_mpol+0xb4/0x1a4
[   59.933022]  alloc_frozen_pages_noprof+0x48/0xc0
[   59.937683]  folio_alloc_noprof+0x14/0x68
[   59.941718]  mm_get_huge_zero_folio+0xf4/0x30c
[   59.946200]  do_huge_pmd_anonymous_page+0x278/0x6a0
[   59.951119]  __handle_mm_fault+0x700/0x1834
[   59.955332]  handle_mm_fault+0x8c/0x2a0
[   59.959190]  do_page_fault+0x108/0x75c
[   59.962964]  do_translation_fault+0x5c/0x6c
[   59.967181]  do_mem_abort+0x40/0x90

Thanks for the report. I missed the fact that the arch mte_zero_clear_page_tags() arch code issues MTE instructions irrespective of whether the hardware supports it. We got away with this so far since we check the VM_MTE flag and that's only set if the hardware supports MTE.

Quoted 10 lines

Looking at the codes:

Review

Quoted 4 lines
(quoted diff) +2 -1
-       zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
+       zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
+                                ~__GFP_MOVABLE,
                        HPAGE_PMD_ORDER);

This adds an unonditional __GFP_ZEROTAGS - from a quick scan it looks like this was previously only enabled by vma_alloc_zeroed_movable_folio() when the VMA has VM_MTE, I think we need a similar test here.

We can't do this for the huge zero page since this will be shared by other vmas and not all would have VM_MTE set. I'll fix it in the arch code:

-----------8<---------------------------------------------

arch/arm64/mm/fault.c +10 -1
index d816ff44faff..125dfa6c613b 100644
@@ -969,6 +969,16 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 
 void tag_clear_highpage(struct page *page)
 {
+	/*
+	 * Check if MTE is supported and fall back to clear_highpage().
+	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
+	 * post_alloc_hook() will invoke tag_clear_highpage().
+	 */
+	if (!system_supports_mte()) {
+		clear_highpage(page);
+		return;
+	}
+
 	/* Newly allocated page, shouldn't have been tagged yet */
 	WARN_ON_ONCE(!try_page_mte_tagging(page));
 	mte_zero_clear_page_tags(page_address(page));
------------------8<------------------------------------------
Quoted 2 lines

Testing now.

Good catch! LGTM, feel free to add:

#10 lore raw

PATCH 1/1 mm/huge_memory: initialise the tags of the huge zero folio

In reply to Catalin Marinas

From: Catalin Marinas <[email protected]>

On arm64 with MTE enabled, a page mapped as Normal Tagged (PROT_MTE) in user space will need to have its allocation tags initialised. This is normally done in the arm64 set_pte_at() after checking the memory attributes. Such page is also marked with the PG_mte_tagged flag to avoid subsequent clearing. Since this relies on having a struct page, pte_special() mappings are ignored.

Commit d82d09e48219 ("mm/huge_memory: mark PMD mappings of the huge zero folio special") maps the huge zero folio special and the arm64 set_pmd_at() will no longer zero the tags. There is no guarantee that the tags are zero, especially if parts of this huge page have been previously tagged.

It's fairly easy to detect this by regularly dropping the caches to force the reallocation of the huge zero folio.

Allocate the huge zero folio with the __GFP_ZEROTAGS flag. In addition, do not warn in the arm64 __access_remote_tags() when reading tags from the huge zero page.

I bundled the arm64 change in here as well since they are both related to the commit mapping the huge zero folio as special.

Fixes d82d09e48219 ("mm/huge_memory: mark PMD mappings of the huge zero folio special")
Cc Will Deacon <[email protected]>
---
 arch/arm64/kernel/mte.c | 3 ++-
 mm/huge_memory.c        | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kernel/mte.c +2 -1
index 43f7a2f39403..32148bf09c1d 100644
@@ -476,7 +476,8 @@ static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
 
 		folio = page_folio(page);
 		if (folio_test_hugetlb(folio))
-			WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio));
+			WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio) &&
+				     !is_huge_zero_folio(folio));
 		else
 			WARN_ON_ONCE(!page_mte_tagged(page) && !is_zero_page(page));
 
mm/huge_memory.c +2 -1
index b4ff49d96501..323654fb4f8c 100644
@@ -214,7 +214,8 @@ static bool get_huge_zero_folio(void)
 	if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
 		return true;
 
-	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
+	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
+				 ~__GFP_MOVABLE,
 			HPAGE_PMD_ORDER);
 	if (!zero_folio) {
 		count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Signature · 1 line
2.48.1
#12 lore raw

PATCH 1/1 mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

In reply to Catalin Marinas

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

Restrict the use of __GFP_ZEROTAGS to architectures that support hardware memory tagging (currently arm64 with MTE or KASAN HW tags). This avoids unintended side effects on other platforms.

Fixes 1579227fe0f0 ("mm/huge_memory: initialise the tags of the huge zero folio")
---
 mm/huge_memory.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/huge_memory.c +5 -4
index aae283b00857..0c1794656d7a 100644
@@ -209,14 +209,15 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 
 static bool get_huge_zero_folio(void)
 {
+	gfp_t gfp = (GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE;
 	struct folio *zero_folio;
 retry:
 	if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
 		return true;
-
-	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
-				 ~__GFP_MOVABLE,
-			HPAGE_PMD_ORDER);
+#if IS_ENABLED(CONFIG_KASAN_HW_TAGS) || IS_ENABLED(CONFIG_ARM64_MTE)
+	gfp |= __GFP_ZEROTAGS;
+#endif
+	zero_folio = folio_alloc(gfp, HPAGE_PMD_ORDER);
 	if (!zero_folio) {
 		count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
 		return false;

--
2.48.1

#13 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 26 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

Restrict the use of __GFP_ZEROTAGS to architectures that support hardware memory tagging (currently arm64 with MTE or KASAN HW tags). This avoids unintended side effects on other platforms.

Fixes 1579227fe0f0 ("mm/huge_memory: initialise the tags of the huge zero folio")
---
  mm/huge_memory.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
mm/huge_memory.c +1 -0
index aae283b00857..0c1794656d7a 100644
@@ -209,14 +209,15 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 
  static bool get_huge_zero_folio(void)
  {
+	gfp_t gfp = (GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE;
  	struct folio *zero_folio;

mm/huge_memory.c unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 219

Quoted 10 lines
mm/huge_memory.c +3 -4
  retry:
  	if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
  		return true;
-
-	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
-				 ~__GFP_MOVABLE,
-			HPAGE_PMD_ORDER);
+#if IS_ENABLED(CONFIG_KASAN_HW_TAGS) || IS_ENABLED(CONFIG_ARM64_MTE)
+	gfp |= __GFP_ZEROTAGS;
+#endif

That looks like the wrong approach. If an architecture does not support __GFP_ZEROTAGS it should not trigger anything. __GFP_ZEROTAGS should be ignored.

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);

	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

Gah.

I wonder if the following would work:

include/linux/gfp_types.h +6 -0
index 65db9349f9053..56b82e116cb79 100644
@@ -47,7 +47,9 @@ enum {
        ___GFP_HARDWALL_BIT,
        ___GFP_THISNODE_BIT,
        ___GFP_ACCOUNT_BIT,
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
        ___GFP_ZEROTAGS_BIT,
+#endif
 #ifdef CONFIG_KASAN_HW_TAGS
        ___GFP_SKIP_ZERO_BIT,
        ___GFP_SKIP_KASAN_BIT,
@@ -85,7 +87,11 @@ enum {
 #define ___GFP_HARDWALL                BIT(___GFP_HARDWALL_BIT)
 #define ___GFP_THISNODE                BIT(___GFP_THISNODE_BIT)
 #define ___GFP_ACCOUNT         BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 #define ___GFP_ZEROTAGS                BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS                0
+#endif
 #ifdef CONFIG_KASAN_HW_TAGS
 #define ___GFP_SKIP_ZERO       BIT(___GFP_SKIP_ZERO_BIT)
 #define ___GFP_SKIP_KASAN      BIT(___GFP_SKIP_KASAN_BIT)

Likely we'd have to make __HAVE_ARCH_TAG_CLEAR_HIGHPAGE a proper kconfig option.

Then we could turn the default implementation of
tag_clear_highpage() into a BUILD_BUG.

#14 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 87 lines · David Hildenbrand (Red Hat)
Quoted 26 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

Restrict the use of __GFP_ZEROTAGS to architectures that support hardware memory tagging (currently arm64 with MTE or KASAN HW tags). This avoids unintended side effects on other platforms.

Fixes 1579227fe0f0 ("mm/huge_memory: initialise the tags of the huge zero folio")
---
  mm/huge_memory.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
mm/huge_memory.c +1 -0
index aae283b00857..0c1794656d7a 100644
@@ -209,14 +209,15 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 
  static bool get_huge_zero_folio(void)
  {
+	gfp_t gfp = (GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE;
  	struct folio *zero_folio;

mm/huge_memory.c unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 219

Quoted 10 lines
mm/huge_memory.c +3 -4
  retry:
  	if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
  		return true;
-
-	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
-				 ~__GFP_MOVABLE,
-			HPAGE_PMD_ORDER);
+#if IS_ENABLED(CONFIG_KASAN_HW_TAGS) || IS_ENABLED(CONFIG_ARM64_MTE)
+	gfp |= __GFP_ZEROTAGS;
+#endif

That looks like the wrong approach. If an architecture does not support __GFP_ZEROTAGS it should not trigger anything. __GFP_ZEROTAGS should be ignored.

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);

	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

Gah.

I wonder if the following would work:

include/linux/gfp_types.h +6 -0
index 65db9349f9053..56b82e116cb79 100644
@@ -47,7 +47,9 @@ enum {
        ___GFP_HARDWALL_BIT,
        ___GFP_THISNODE_BIT,
        ___GFP_ACCOUNT_BIT,
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
        ___GFP_ZEROTAGS_BIT,
+#endif
 #ifdef CONFIG_KASAN_HW_TAGS
        ___GFP_SKIP_ZERO_BIT,
        ___GFP_SKIP_KASAN_BIT,
@@ -85,7 +87,11 @@ enum {
 #define ___GFP_HARDWALL                BIT(___GFP_HARDWALL_BIT)
 #define ___GFP_THISNODE                BIT(___GFP_THISNODE_BIT)
 #define ___GFP_ACCOUNT         BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 #define ___GFP_ZEROTAGS                BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS                0
+#endif
 #ifdef CONFIG_KASAN_HW_TAGS
 #define ___GFP_SKIP_ZERO       BIT(___GFP_SKIP_ZERO_BIT)
 #define ___GFP_SKIP_KASAN      BIT(___GFP_SKIP_KASAN_BIT)
Quoted 7 lines

Likely we'd have to make __HAVE_ARCH_TAG_CLEAR_HIGHPAGE a proper kconfig option.

Then we could turn the default implementation of
tag_clear_highpage() into a BUILD_BUG.

I'd like to suggest to keep the enum untouched and only use the second part of your suggestion.
Which works by the way for our arch (s390).

 include/linux/gfp_types.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/gfp_types.h +4 -0
index 65db9349f905..c12d8a601bb3 100644
@@ -85,7 +85,11 @@ enum {
 #define ___GFP_HARDWALL        BIT(___GFP_HARDWALL_BIT)
 #define ___GFP_THISNODE        BIT(___GFP_THISNODE_BIT)
 #define ___GFP_ACCOUNT     BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 #define ___GFP_ZEROTAGS        BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS        0
+#endif
 #ifdef CONFIG_KASAN_HW_TAGS
 #define ___GFP_SKIP_ZERO   BIT(___GFP_SKIP_ZERO_BIT)
 #define ___GFP_SKIP_KASAN  BIT(___GFP_SKIP_KASAN_BIT)

This solution would be sufficient from my side, and I would appreciate a quick application if there are no objections.

Thank you David.

#15 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 92 lines · Jan Polensky
Quoted 91 lines · David Hildenbrand (Red Hat)
Quoted 26 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

Restrict the use of __GFP_ZEROTAGS to architectures that support hardware memory tagging (currently arm64 with MTE or KASAN HW tags). This avoids unintended side effects on other platforms.

Fixes 1579227fe0f0 ("mm/huge_memory: initialise the tags of the huge zero folio")
---
   mm/huge_memory.c | 9 +++++----
   1 file changed, 5 insertions(+), 4 deletions(-)
mm/huge_memory.c +1 -0
index aae283b00857..0c1794656d7a 100644
@@ -209,14 +209,15 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
 
   static bool get_huge_zero_folio(void)
   {
+	gfp_t gfp = (GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE;
   	struct folio *zero_folio;

mm/huge_memory.c unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, · line 219

Quoted 10 lines
mm/huge_memory.c +3 -4
   retry:
   	if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
   		return true;
-
-	zero_folio = folio_alloc((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) &
-				 ~__GFP_MOVABLE,
-			HPAGE_PMD_ORDER);
+#if IS_ENABLED(CONFIG_KASAN_HW_TAGS) || IS_ENABLED(CONFIG_ARM64_MTE)
+	gfp |= __GFP_ZEROTAGS;
+#endif

That looks like the wrong approach. If an architecture does not support __GFP_ZEROTAGS it should not trigger anything. __GFP_ZEROTAGS should be ignored.

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);

	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

Gah.

I wonder if the following would work:

include/linux/gfp_types.h +6 -0
index 65db9349f9053..56b82e116cb79 100644
@@ -47,7 +47,9 @@ enum {
         ___GFP_HARDWALL_BIT,
         ___GFP_THISNODE_BIT,
         ___GFP_ACCOUNT_BIT,
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
         ___GFP_ZEROTAGS_BIT,
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
         ___GFP_SKIP_ZERO_BIT,
         ___GFP_SKIP_KASAN_BIT,
@@ -85,7 +87,11 @@ enum {
  #define ___GFP_HARDWALL                BIT(___GFP_HARDWALL_BIT)
  #define ___GFP_THISNODE                BIT(___GFP_THISNODE_BIT)
  #define ___GFP_ACCOUNT         BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  #define ___GFP_ZEROTAGS                BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS                0
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
  #define ___GFP_SKIP_ZERO       BIT(___GFP_SKIP_ZERO_BIT)
  #define ___GFP_SKIP_KASAN      BIT(___GFP_SKIP_KASAN_BIT)

Likely we'd have to make __HAVE_ARCH_TAG_CLEAR_HIGHPAGE a proper kconfig option.

Quoted 5 lines
Quoted 3 lines

Then we could turn the default implementation of
tag_clear_highpage() into a BUILD_BUG.

I'd like to suggest to keep the enum untouched and only use the second part of your suggestion.

Why? We also do that for CONFIG_KASAN_HW_TAGS, CONFIG_LOCKDEP and CONFIG_SLAB_OBJ_EXT.

Quoted 22 lines

Which works by the way for our arch (s390).

  include/linux/gfp_types.h | 4 ++++
  1 file changed, 4 insertions(+)
include/linux/gfp_types.h +4 -0
index 65db9349f905..c12d8a601bb3 100644
@@ -85,7 +85,11 @@ enum {
  #define ___GFP_HARDWALL        BIT(___GFP_HARDWALL_BIT)
  #define ___GFP_THISNODE        BIT(___GFP_THISNODE_BIT)
  #define ___GFP_ACCOUNT     BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  #define ___GFP_ZEROTAGS        BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS        0
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
  #define ___GFP_SKIP_ZERO   BIT(___GFP_SKIP_ZERO_BIT)
  #define ___GFP_SKIP_KASAN  BIT(___GFP_SKIP_KASAN_BIT)
Quoted 2 lines

This solution would be sufficient from my side, and I would appreciate a quick application if there are no objections.

As raised, to be sure that __HAVE_ARCH_TAG_CLEAR_HIGHPAGE is always seen early in that file, it should likely become a CONFIG_ thing.

author maintainer
#16 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 7 lines · David Hildenbrand (Red Hat)
Quoted 6 lines · Jan Polensky
Quoted 5 lines · David Hildenbrand (Red Hat)
Quoted 4 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

Hmm, another thing I missed. Sorry about this.

Quoted 22 lines
Quoted 22 lines

Which works by the way for our arch (s390).

  include/linux/gfp_types.h | 4 ++++
  1 file changed, 4 insertions(+)
include/linux/gfp_types.h +4 -0
index 65db9349f905..c12d8a601bb3 100644
@@ -85,7 +85,11 @@ enum {
  #define ___GFP_HARDWALL        BIT(___GFP_HARDWALL_BIT)
  #define ___GFP_THISNODE        BIT(___GFP_THISNODE_BIT)
  #define ___GFP_ACCOUNT     BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  #define ___GFP_ZEROTAGS        BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS        0
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
  #define ___GFP_SKIP_ZERO   BIT(___GFP_SKIP_ZERO_BIT)
  #define ___GFP_SKIP_KASAN  BIT(___GFP_SKIP_KASAN_BIT)
Quoted 5 lines
Quoted 2 lines

This solution would be sufficient from my side, and I would appreciate a quick application if there are no objections.

As raised, to be sure that __HAVE_ARCH_TAG_CLEAR_HIGHPAGE is always seen early in that file, it should likely become a CONFIG_ thing.

I'm fine with either option above but I'll throw one more in the mix:

--------------------8<--------------------------------

arch/arm64/include/asm/page.h +1 -0
index 2312e6ee595f..dcff91533590 100644
@@ -33,6 +33,7 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 						unsigned long vaddr);
 #define vma_alloc_zeroed_movable_folio vma_alloc_zeroed_movable_folio
 
+bool arch_has_tag_clear_highpage(void);
 void tag_clear_highpage(struct page *to);
 #define __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
arch/arm64/mm/fault.c +5 -10
index 125dfa6c613b..318d091db843 100644
@@ -967,18 +967,13 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 	return vma_alloc_folio(flags, 0, vma, vaddr);
 }
 
+bool arch_has_tag_clear_highpage(void)
+{
+	return system_supports_mte();
+}
+
 void tag_clear_highpage(struct page *page)
 {
-	/*
-	 * Check if MTE is supported and fall back to clear_highpage().
-	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
-	 * post_alloc_hook() will invoke tag_clear_highpage().
-	 */
-	if (!system_supports_mte()) {
-		clear_highpage(page);
-		return;
-	}
-
 	/* Newly allocated page, shouldn't have been tagged yet */
 	WARN_ON_ONCE(!try_page_mte_tagging(page));
 	mte_zero_clear_page_tags(page_address(page));
include/linux/highmem.h +5 -0
index 105cc4c00cc3..7aa56179ccef 100644
@@ -251,6 +251,11 @@ static inline void clear_highpage_kasan_tagged(struct page *page)
 
 #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
+static inline bool arch_has_tag_clear_highpage(void)
+{
+	return false;
+}
+
 static inline void tag_clear_highpage(struct page *page)
 {
 }
mm/page_alloc.c +2 -1
index e4efda1158b2..5ab15431bc06 100644
@@ -1798,7 +1798,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 {
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
 			!should_skip_init(gfp_flags);
-	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS) &&
+		arch_has_tag_clear_highpage();
 	int i;
 
 	set_page_private(page, 0);

--------------------8<--------------------------------

Reasoning: with MTE on arm64, you can't have kasan-tagged pages in the kernel which are also exposed to user because the tags are shared (same physical location). The 'zero_tags' initialisation in post_alloc_hook() makes sense for this behaviour. With virtual tagging (briefly announced in [1], full specs not public yet), both the user and the kernel can have their own tags - more like KASAN_SW_TAGS but without the compiler instrumentation. The kernel won't be able to zero the tags for the user since they are in virtual space. It can, however, continue to use Kasan tags even if the pages are mapped in user space. In this case, I'd rather use the kernel_init_pages() call further down in post_alloc_hook() than replicating it in tag_clear_highpage(). When we get to upstreaming virtual tagging (informally vMTE, sometime next year), I'd like to have a kernel image that supports both, so the decision on whether to call tag_clear_highpage() will need to be dynamic.

[1] https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/future-architecture-technologies-poe2-and-vmte

Signature · 1 line
Catalin
author maintainer
#17 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 21 lines · Catalin Marinas
Quoted 7 lines · David Hildenbrand (Red Hat)
Quoted 6 lines · Jan Polensky
Quoted 5 lines · David Hildenbrand (Red Hat)
Quoted 4 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

[...]

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);

	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

[...]

Quoted 72 lines
arch/arm64/include/asm/page.h +1 -0
index 2312e6ee595f..dcff91533590 100644
@@ -33,6 +33,7 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 						unsigned long vaddr);
 #define vma_alloc_zeroed_movable_folio vma_alloc_zeroed_movable_folio
 
+bool arch_has_tag_clear_highpage(void);
 void tag_clear_highpage(struct page *to);
 #define __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
arch/arm64/mm/fault.c +5 -10
index 125dfa6c613b..318d091db843 100644
@@ -967,18 +967,13 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 	return vma_alloc_folio(flags, 0, vma, vaddr);
 }
 
+bool arch_has_tag_clear_highpage(void)
+{
+	return system_supports_mte();
+}
+
 void tag_clear_highpage(struct page *page)
 {
-	/*
-	 * Check if MTE is supported and fall back to clear_highpage().
-	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
-	 * post_alloc_hook() will invoke tag_clear_highpage().
-	 */
-	if (!system_supports_mte()) {
-		clear_highpage(page);
-		return;
-	}
-
 	/* Newly allocated page, shouldn't have been tagged yet */
 	WARN_ON_ONCE(!try_page_mte_tagging(page));
 	mte_zero_clear_page_tags(page_address(page));
include/linux/highmem.h +5 -0
index 105cc4c00cc3..7aa56179ccef 100644
@@ -251,6 +251,11 @@ static inline void clear_highpage_kasan_tagged(struct page *page)
 
 #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
+static inline bool arch_has_tag_clear_highpage(void)
+{
+	return false;
+}
+
 static inline void tag_clear_highpage(struct page *page)
 {
 }
mm/page_alloc.c +2 -2
index e4efda1158b2..5ab15431bc06 100644
@@ -1798,7 +1798,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 {
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
 			!should_skip_init(gfp_flags);
-	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS) &&
+		arch_has_tag_clear_highpage();
 	int i;
 
 	set_page_private(page, 0);
--------------------8<--------------------------------
Quoted 15 lines

Reasoning: with MTE on arm64, you can't have kasan-tagged pages in the kernel which are also exposed to user because the tags are shared (same physical location). The 'zero_tags' initialisation in post_alloc_hook() makes sense for this behaviour. With virtual tagging (briefly announced in [1], full specs not public yet), both the user and the kernel can have their own tags - more like KASAN_SW_TAGS but without the compiler instrumentation. The kernel won't be able to zero the tags for the user since they are in virtual space. It can, however, continue to use Kasan tags even if the pages are mapped in user space. In this case, I'd rather use the kernel_init_pages() call further down in post_alloc_hook() than replicating it in tag_clear_highpage(). When we get to upstreaming virtual tagging (informally vMTE, sometime next year), I'd like to have a kernel image that supports both, so the decision on whether to call tag_clear_highpage() will need to be dynamic.

Actually, there's not much to kernel_init_pages() other than disabling kasan temporarily since the unpoisoning already took place a few lines up. The arm64 tag_clear_highpage() calling clear_highpage() directly is fine before unpoisoning. So we can cope with this even in the vMTE case.

A simple patch hiding the enum is fine by me.

Signature · 1 line
Catalin
#18 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

In reply to Catalin Marinas
Quoted 143 lines · Catalin Marinas
Quoted 7 lines · David Hildenbrand (Red Hat)
Quoted 6 lines · Jan Polensky
Quoted 5 lines · David Hildenbrand (Red Hat)
Quoted 4 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

Hmm, another thing I missed. Sorry about this.

Quoted 22 lines
Quoted 22 lines

Which works by the way for our arch (s390).

  include/linux/gfp_types.h | 4 ++++
  1 file changed, 4 insertions(+)
include/linux/gfp_types.h +4 -0
index 65db9349f905..c12d8a601bb3 100644
@@ -85,7 +85,11 @@ enum {
  #define ___GFP_HARDWALL        BIT(___GFP_HARDWALL_BIT)
  #define ___GFP_THISNODE        BIT(___GFP_THISNODE_BIT)
  #define ___GFP_ACCOUNT     BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  #define ___GFP_ZEROTAGS        BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS        0
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
  #define ___GFP_SKIP_ZERO   BIT(___GFP_SKIP_ZERO_BIT)
  #define ___GFP_SKIP_KASAN  BIT(___GFP_SKIP_KASAN_BIT)
Quoted 5 lines
Quoted 2 lines

This solution would be sufficient from my side, and I would appreciate a quick application if there are no objections.

As raised, to be sure that __HAVE_ARCH_TAG_CLEAR_HIGHPAGE is always seen early in that file, it should likely become a CONFIG_ thing.

I'm fine with either option above but I'll throw one more in the mix:

--------------------8<--------------------------------

arch/arm64/include/asm/page.h +1 -0
index 2312e6ee595f..dcff91533590 100644
@@ -33,6 +33,7 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 						unsigned long vaddr);
 #define vma_alloc_zeroed_movable_folio vma_alloc_zeroed_movable_folio
 
+bool arch_has_tag_clear_highpage(void);
 void tag_clear_highpage(struct page *to);
 #define __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
arch/arm64/mm/fault.c +5 -10
index 125dfa6c613b..318d091db843 100644
@@ -967,18 +967,13 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
 	return vma_alloc_folio(flags, 0, vma, vaddr);
 }
 
+bool arch_has_tag_clear_highpage(void)
+{
+	return system_supports_mte();
+}
+
 void tag_clear_highpage(struct page *page)
 {
-	/*
-	 * Check if MTE is supported and fall back to clear_highpage().
-	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
-	 * post_alloc_hook() will invoke tag_clear_highpage().
-	 */
-	if (!system_supports_mte()) {
-		clear_highpage(page);
-		return;
-	}
-
 	/* Newly allocated page, shouldn't have been tagged yet */
 	WARN_ON_ONCE(!try_page_mte_tagging(page));
 	mte_zero_clear_page_tags(page_address(page));
include/linux/highmem.h +5 -0
index 105cc4c00cc3..7aa56179ccef 100644
@@ -251,6 +251,11 @@ static inline void clear_highpage_kasan_tagged(struct page *page)
 
 #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
+static inline bool arch_has_tag_clear_highpage(void)
+{
+	return false;
+}
+
 static inline void tag_clear_highpage(struct page *page)
 {
 }
mm/page_alloc.c +2 -2
index e4efda1158b2..5ab15431bc06 100644
@@ -1798,7 +1798,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
 {
 	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
 			!should_skip_init(gfp_flags);
-	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS) &&
+		arch_has_tag_clear_highpage();
 	int i;
 
 	set_page_private(page, 0);
--------------------8<--------------------------------

Reasoning: with MTE on arm64, you can't have kasan-tagged pages in the kernel which are also exposed to user because the tags are shared (same physical location). The 'zero_tags' initialisation in post_alloc_hook() makes sense for this behaviour. With virtual tagging (briefly announced in [1], full specs not public yet), both the user and the kernel can have their own tags - more like KASAN_SW_TAGS but without the compiler instrumentation. The kernel won't be able to zero the tags for the user since they are in virtual space. It can, however, continue to use Kasan tags even if the pages are mapped in user space. In this case, I'd rather use the kernel_init_pages() call further down in post_alloc_hook() than replicating it in tag_clear_highpage(). When we get to upstreaming virtual tagging (informally vMTE, sometime next year), I'd like to have a kernel image that supports both, so the decision on whether to call tag_clear_highpage() will need to be dynamic.

Quoted 2 lines

[1] https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/future-architecture-technologies-poe2-and-vmte

I've run into the issue where due to init being set to false if zero_tags was set, the system does not clear the zero_folio. I just spent a lot of time debugging it :)

Catalin, were you going to send out this patch as a fix to be included in mm-unstable? I've for now reverted your __GFP_ZEROTAGS change to get_huge_zero_folio() for my testing

I am on the current mm-new branch.

Balbir

#19 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 150 lines · Balbir Singh
Quoted 143 lines · Catalin Marinas
Quoted 7 lines · David Hildenbrand (Red Hat)
Quoted 6 lines · Jan Polensky
Quoted 5 lines · David Hildenbrand (Red Hat)
Quoted 4 lines · Jan Polensky

The previous change added __GFP_ZEROTAGS when allocating the huge zero folio to ensure tag initialization for arm64 with MTE enabled. However, on s390 this flag is unnecessary and triggers a regression (observed as a crash during repeated 'dnf makecache').

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

I think the problem is that post_alloc_hook() does

if (zero_tags) {
	/* Initialize both memory and memory tags. */
	for (i = 0; i != 1 << order; ++i)
		tag_clear_highpage(page + i);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
	/* Take note that memory was initialized by the loop above. */
	init = false;
}

And tag_clear_highpage() is a NOP on other architectures.

Hmm, another thing I missed. Sorry about this.

Quoted 22 lines
Quoted 22 lines

Which works by the way for our arch (s390).

   include/linux/gfp_types.h | 4 ++++
   1 file changed, 4 insertions(+)
include/linux/gfp_types.h +4 -0
index 65db9349f905..c12d8a601bb3 100644
@@ -85,7 +85,11 @@ enum {
   #define ___GFP_HARDWALL        BIT(___GFP_HARDWALL_BIT)
   #define ___GFP_THISNODE        BIT(___GFP_THISNODE_BIT)
   #define ___GFP_ACCOUNT     BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
   #define ___GFP_ZEROTAGS        BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS        0
+#endif
   #ifdef CONFIG_KASAN_HW_TAGS
   #define ___GFP_SKIP_ZERO   BIT(___GFP_SKIP_ZERO_BIT)
   #define ___GFP_SKIP_KASAN  BIT(___GFP_SKIP_KASAN_BIT)
Quoted 5 lines
Quoted 2 lines

This solution would be sufficient from my side, and I would appreciate a quick application if there are no objections.

As raised, to be sure that __HAVE_ARCH_TAG_CLEAR_HIGHPAGE is always seen early in that file, it should likely become a CONFIG_ thing.

I'm fine with either option above but I'll throw one more in the mix:

--------------------8<--------------------------------

arch/arm64/include/asm/page.h +1 -0
index 2312e6ee595f..dcff91533590 100644
@@ -33,6 +33,7 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
  						unsigned long vaddr);
  #define vma_alloc_zeroed_movable_folio vma_alloc_zeroed_movable_folio
 
+bool arch_has_tag_clear_highpage(void);
  void tag_clear_highpage(struct page *to);
  #define __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
arch/arm64/mm/fault.c +5 -10
index 125dfa6c613b..318d091db843 100644
@@ -967,18 +967,13 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
  	return vma_alloc_folio(flags, 0, vma, vaddr);
  }
 
+bool arch_has_tag_clear_highpage(void)
+{
+	return system_supports_mte();
+}
+
  void tag_clear_highpage(struct page *page)
  {
-	/*
-	 * Check if MTE is supported and fall back to clear_highpage().
-	 * get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
-	 * post_alloc_hook() will invoke tag_clear_highpage().
-	 */
-	if (!system_supports_mte()) {
-		clear_highpage(page);
-		return;
-	}
-
  	/* Newly allocated page, shouldn't have been tagged yet */
  	WARN_ON_ONCE(!try_page_mte_tagging(page));
  	mte_zero_clear_page_tags(page_address(page));
include/linux/highmem.h +5 -0
index 105cc4c00cc3..7aa56179ccef 100644
@@ -251,6 +251,11 @@ static inline void clear_highpage_kasan_tagged(struct page *page)
 
  #ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
 
+static inline bool arch_has_tag_clear_highpage(void)
+{
+	return false;
+}
+
  static inline void tag_clear_highpage(struct page *page)
  {
  }
mm/page_alloc.c +2 -2
index e4efda1158b2..5ab15431bc06 100644
@@ -1798,7 +1798,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
  {
  	bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
  			!should_skip_init(gfp_flags);
-	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS);
+	bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS) &&
+		arch_has_tag_clear_highpage();
  	int i;
 
  	set_page_private(page, 0);
--------------------8<--------------------------------

Reasoning: with MTE on arm64, you can't have kasan-tagged pages in the kernel which are also exposed to user because the tags are shared (same physical location). The 'zero_tags' initialisation in post_alloc_hook() makes sense for this behaviour. With virtual tagging (briefly announced in [1], full specs not public yet), both the user and the kernel can have their own tags - more like KASAN_SW_TAGS but without the compiler instrumentation. The kernel won't be able to zero the tags for the user since they are in virtual space. It can, however, continue to use Kasan tags even if the pages are mapped in user space. In this case, I'd rather use the kernel_init_pages() call further down in post_alloc_hook() than replicating it in tag_clear_highpage(). When we get to upstreaming virtual tagging (informally vMTE, sometime next year), I'd like to have a kernel image that supports both, so the decision on whether to call tag_clear_highpage() will need to be dynamic.

Quoted 2 lines

[1] https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/future-architecture-technologies-poe2-and-vmte

I've run into the issue where due to init being set to false if zero_tags was set, the system does not clear the zero_folio. I just spent a lot of time debugging it :)

Quoted 4 lines

Catalin, were you going to send out this patch as a fix to be included in mm-unstable? I've for now reverted your __GFP_ZEROTAGS change to get_huge_zero_folio() for my testing

I am on the current mm-new branch.

We have a fix upstream now:

commit 5bebe8de19264946d398ead4e6c20c229454a552

Author: Linus Torvalds <[email protected]>
Date:   Tue Nov 18 08:21:27 2025 -0800

mm/huge_memory: Fix initialization of huge zero folio

Andrew could consider picking it up as well temporarily to fix the issue until we rebase on top of the new kernel.

Signature · 3 lines
Cheers

David
maintainer
#20 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 11 lines · On Mon, 24 Nov 2025 11:57:09 +0100 "David Hildenbrand (Red Hat)"…
Quoted 7 lines

I've run into the issue where due to init being set to false if zero_tags was set, the system does not clear the zero_folio. I just spent a lot of time debugging it :)

Catalin, were you going to send out this patch as a fix to be included in mm-unstable? I've for now reverted your __GFP_ZEROTAGS change to get_huge_zero_folio() for my testing

I am on the current mm-new branch.

We have a fix upstream now:

commit 5bebe8de19264946d398ead4e6c20c229454a552

Quoted 8 lines
Author: Linus Torvalds <[email protected]>
Date:   Tue Nov 18 08:21:27 2025 -0800

mm/huge_memory: Fix initialization of huge zero folio

Andrew could consider picking it up as well temporarily to fix the issue until we rebase on top of the new kernel.

Sure, I added 5bebe8de19264946d398ead4e6c20c229454a552 to mm-new as a temp thing.

#21 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

In reply to David Hildenbrand (Red Hat)
Quoted 3 lines · David Hildenbrand (Red Hat)
Quoted 2 lines · Jan Polensky
Quoted 1 line · David Hildenbrand (Red Hat)

On 09.11.25 01:36, Jan Polensky wrote:

---8<--- snip ---8<---

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

I wonder if the following would work:

include/linux/gfp_types.h +6 -0
index 65db9349f9053..56b82e116cb79 100644
@@ -47,7 +47,9 @@ enum {
         ___GFP_HARDWALL_BIT,
         ___GFP_THISNODE_BIT,
         ___GFP_ACCOUNT_BIT,
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
         ___GFP_ZEROTAGS_BIT,
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
         ___GFP_SKIP_ZERO_BIT,
         ___GFP_SKIP_KASAN_BIT,
@@ -85,7 +87,11 @@ enum {
  #define ___GFP_HARDWALL                BIT(___GFP_HARDWALL_BIT)
  #define ___GFP_THISNODE                BIT(___GFP_THISNODE_BIT)
  #define ___GFP_ACCOUNT         BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  #define ___GFP_ZEROTAGS                BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS                0
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
  #define ___GFP_SKIP_ZERO       BIT(___GFP_SKIP_ZERO_BIT)
  #define ___GFP_SKIP_KASAN      BIT(___GFP_SKIP_KASAN_BIT)

Likely we'd have to make __HAVE_ARCH_TAG_CLEAR_HIGHPAGE a proper kconfig option.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Then we could turn the default implementation of
tag_clear_highpage() into a BUILD_BUG.

I'd like to suggest to keep the enum untouched and only use the second part of your suggestion.

Why? We also do that for CONFIG_KASAN_HW_TAGS, CONFIG_LOCKDEP and CONFIG_SLAB_OBJ_EXT.

If we remove the enum entry, we’d also need to update mmflags.h because the trace macros reference it.
Enums are compile-time only, so they don’t affect the generated binary. My thought was to keep the enum list as it is and just apply the second part of your suggestion.
That way, the trace definitions stay consistent without extra changes. Just an idea, happy to go with whatever you prefer.

Quoted 29 lines
Quoted 23 lines

Which works by the way for our arch (s390).

  include/linux/gfp_types.h | 4 ++++
  1 file changed, 4 insertions(+)
include/linux/gfp_types.h +4 -0
index 65db9349f905..c12d8a601bb3 100644
@@ -85,7 +85,11 @@ enum {
  #define ___GFP_HARDWALL        BIT(___GFP_HARDWALL_BIT)
  #define ___GFP_THISNODE        BIT(___GFP_THISNODE_BIT)
  #define ___GFP_ACCOUNT     BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
  #define ___GFP_ZEROTAGS        BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS        0
+#endif
  #ifdef CONFIG_KASAN_HW_TAGS
  #define ___GFP_SKIP_ZERO   BIT(___GFP_SKIP_ZERO_BIT)
  #define ___GFP_SKIP_KASAN  BIT(___GFP_SKIP_KASAN_BIT)
Quoted 2 lines

This solution would be sufficient from my side, and I would appreciate a quick application if there are no objections.

As raised, to be sure that __HAVE_ARCH_TAG_CLEAR_HIGHPAGE is always seen early in that file, it should likely become a CONFIG_ thing.

#22 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 48 lines · Jan Polensky
Quoted 3 lines · David Hildenbrand (Red Hat)
Quoted 2 lines · Jan Polensky
Quoted 1 line · David Hildenbrand (Red Hat)

On 09.11.25 01:36, Jan Polensky wrote:

---8<--- snip ---8<---

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

I wonder if the following would work:

include/linux/gfp_types.h +6 -0
index 65db9349f9053..56b82e116cb79 100644
@@ -47,7 +47,9 @@ enum {
          ___GFP_HARDWALL_BIT,
          ___GFP_THISNODE_BIT,
          ___GFP_ACCOUNT_BIT,
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
          ___GFP_ZEROTAGS_BIT,
+#endif
   #ifdef CONFIG_KASAN_HW_TAGS
          ___GFP_SKIP_ZERO_BIT,
          ___GFP_SKIP_KASAN_BIT,
@@ -85,7 +87,11 @@ enum {
   #define ___GFP_HARDWALL                BIT(___GFP_HARDWALL_BIT)
   #define ___GFP_THISNODE                BIT(___GFP_THISNODE_BIT)
   #define ___GFP_ACCOUNT         BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
   #define ___GFP_ZEROTAGS                BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS                0
+#endif
   #ifdef CONFIG_KASAN_HW_TAGS
   #define ___GFP_SKIP_ZERO       BIT(___GFP_SKIP_ZERO_BIT)
   #define ___GFP_SKIP_KASAN      BIT(___GFP_SKIP_KASAN_BIT)

Likely we'd have to make __HAVE_ARCH_TAG_CLEAR_HIGHPAGE a proper kconfig option.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Then we could turn the default implementation of
tag_clear_highpage() into a BUILD_BUG.

I'd like to suggest to keep the enum untouched and only use the second part of your suggestion.

Why? We also do that for CONFIG_KASAN_HW_TAGS, CONFIG_LOCKDEP and CONFIG_SLAB_OBJ_EXT.

Quoted 7 lines

If we remove the enum entry, we’d also need to update mmflags.h because the trace macros reference it.
Enums are compile-time only, so they don’t affect the generated binary. My thought was to keep the enum list as it is and just apply the second part of your suggestion.
That way, the trace definitions stay consistent without extra changes. Just an idea, happy to go with whatever you prefer.

I think we'd remove the enum value as well, because then there is no way it could accidentally be reused.

And yes, as you correctly state we'll have to update mmflags as well like we did for CONFIG_KASAN_HW_TAGS etc.

Signature · 3 lines
Cheers

David
#23 lore raw

Re: [PATCH] mm/huge_memory: restrict __GFP_ZEROTAGS to HW tagging architectures

Quoted 57 lines · David Hildenbrand (Red Hat)
Quoted 48 lines · Jan Polensky
Quoted 3 lines · David Hildenbrand (Red Hat)
Quoted 2 lines · Jan Polensky
Quoted 1 line · David Hildenbrand (Red Hat)

On 09.11.25 01:36, Jan Polensky wrote:

---8<--- snip ---8<---

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

I wonder if the following would work:

include/linux/gfp_types.h +6 -0
index 65db9349f9053..56b82e116cb79 100644
@@ -47,7 +47,9 @@ enum {
           ___GFP_HARDWALL_BIT,
           ___GFP_THISNODE_BIT,
           ___GFP_ACCOUNT_BIT,
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
           ___GFP_ZEROTAGS_BIT,
+#endif
    #ifdef CONFIG_KASAN_HW_TAGS
           ___GFP_SKIP_ZERO_BIT,
           ___GFP_SKIP_KASAN_BIT,
@@ -85,7 +87,11 @@ enum {
    #define ___GFP_HARDWALL                BIT(___GFP_HARDWALL_BIT)
    #define ___GFP_THISNODE                BIT(___GFP_THISNODE_BIT)
    #define ___GFP_ACCOUNT         BIT(___GFP_ACCOUNT_BIT)
+#ifdef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE
    #define ___GFP_ZEROTAGS                BIT(___GFP_ZEROTAGS_BIT)
+#else
+#define ___GFP_ZEROTAGS                0
+#endif
    #ifdef CONFIG_KASAN_HW_TAGS
    #define ___GFP_SKIP_ZERO       BIT(___GFP_SKIP_ZERO_BIT)
    #define ___GFP_SKIP_KASAN      BIT(___GFP_SKIP_KASAN_BIT)

Likely we'd have to make __HAVE_ARCH_TAG_CLEAR_HIGHPAGE a proper kconfig option.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Then we could turn the default implementation of
tag_clear_highpage() into a BUILD_BUG.

I'd like to suggest to keep the enum untouched and only use the second part of your suggestion.

Why? We also do that for CONFIG_KASAN_HW_TAGS, CONFIG_LOCKDEP and CONFIG_SLAB_OBJ_EXT.

Quoted 7 lines

If we remove the enum entry, we’d also need to update mmflags.h because the trace macros reference it.
Enums are compile-time only, so they don’t affect the generated binary. My thought was to keep the enum list as it is and just apply the second part of your suggestion.
That way, the trace definitions stay consistent without extra changes. Just an idea, happy to go with whatever you prefer.

Quoted 5 lines

I think we'd remove the enum value as well, because then there is no way it could accidentally be reused.

And yes, as you correctly state we'll have to update mmflags as well like we did for CONFIG_KASAN_HW_TAGS etc.

/me realizing that my mail client decided to use yet another mail alias, I hope I have it fixed now such that everything is sent from my kernel.org account ...

Signature · 3 lines
Cheers

David