Skip to content

Commit 6a288a4

Browse files
davidhildenbrandakpm00
authored andcommitted
mm/page_alloc: fix initialization of tags of the huge zero folio with init_on_free
__GFP_ZEROTAGS semantics are currently a bit weird, but effectively this flag is only ever set alongside __GFP_ZERO and __GFP_SKIP_KASAN. If we run with init_on_free, we will zero out pages during __free_pages_prepare(), to skip zeroing on the allocation path. However, when allocating with __GFP_ZEROTAG set, post_alloc_hook() will consequently not only skip clearing page content, but also skip clearing tag memory. Not clearing tags through __GFP_ZEROTAGS is irrelevant for most pages that will get mapped to user space through set_pte_at() later: set_pte_at() and friends will detect that the tags have not been initialized yet (PG_mte_tagged not set), and initialize them. However, for the huge zero folio, which will be mapped through a PMD marked as special, this initialization will not be performed, ending up exposing whatever tags were still set for the pages. The docs (Documentation/arch/arm64/memory-tagging-extension.rst) state that allocation tags are set to 0 when a page is first mapped to user space. That no longer holds with the huge zero folio when init_on_free is enabled. Fix it by decoupling __GFP_ZEROTAGS from __GFP_ZERO, passing to tag_clear_highpages() whether we want to also clear page content. Invert the meaning of the tag_clear_highpages() return value to have clearer semantics. Reproduced with the huge zero folio by modifying the check_buffer_fill arm64/mte selftest to use a 2 MiB area, after making sure that pages have a non-0 tag set when freeing (note that, during boot, we will not actually initialize tags, but only set KASAN_TAG_KERNEL in the page flags). $ ./check_buffer_fill 1..20 ... not ok 17 Check initial tags with private mapping, sync error mode and mmap memory not ok 18 Check initial tags with private mapping, sync error mode and mmap/mprotect memory ... This code needs more cleanups; we'll tackle that next, like decoupling __GFP_ZEROTAGS from __GFP_SKIP_KASAN. [akpm@linux-foundation.org: s/__GPF_ZERO/__GFP_ZERO/, per David] Link: https://lore.kernel.org/20260421-zerotags-v2-1-05cb1035482e@kernel.org Fixes: adfb660 ("mm/huge_memory: initialise the tags of the huge zero folio") Signed-off-by: David Hildenbrand (Arm) <david@kernel.org> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Tested-by: Lance Yang <lance.yang@linux.dev> Cc: Brendan Jackman <jackmanb@google.com> Cc: Dev Jain <dev.jain@arm.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Liam Howlett <liam@infradead.org> Cc: Lorenzo Stoakes (Oracle) <ljs@kernel.org> Cc: Mark Brown <broonie@kernel.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Will Deacon <will@kernel.org> Cc: Zi Yan <ziy@nvidia.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ec9f2ee commit 6a288a4

5 files changed

Lines changed: 21 additions & 17 deletions

File tree

arch/arm64/include/asm/page.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
3333
unsigned long vaddr);
3434
#define vma_alloc_zeroed_movable_folio vma_alloc_zeroed_movable_folio
3535

36-
bool tag_clear_highpages(struct page *to, int numpages);
36+
bool tag_clear_highpages(struct page *to, int numpages, bool clear_pages);
3737
#define __HAVE_ARCH_TAG_CLEAR_HIGHPAGES
3838

3939
#define copy_user_page(to, from, vaddr, pg) copy_page(to, from)

arch/arm64/mm/fault.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,21 +1018,24 @@ struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma,
10181018
return vma_alloc_folio(flags, 0, vma, vaddr);
10191019
}
10201020

1021-
bool tag_clear_highpages(struct page *page, int numpages)
1021+
bool tag_clear_highpages(struct page *page, int numpages, bool clear_pages)
10221022
{
10231023
/*
10241024
* Check if MTE is supported and fall back to clear_highpage().
10251025
* get_huge_zero_folio() unconditionally passes __GFP_ZEROTAGS and
10261026
* post_alloc_hook() will invoke tag_clear_highpages().
10271027
*/
10281028
if (!system_supports_mte())
1029-
return false;
1029+
return clear_pages;
10301030

10311031
/* Newly allocated pages, shouldn't have been tagged yet */
10321032
for (int i = 0; i < numpages; i++, page++) {
10331033
WARN_ON_ONCE(!try_page_mte_tagging(page));
1034-
mte_zero_clear_page_tags(page_address(page));
1034+
if (clear_pages)
1035+
mte_zero_clear_page_tags(page_address(page));
1036+
else
1037+
mte_clear_page_tags(page_address(page));
10351038
set_page_mte_tagged(page);
10361039
}
1037-
return true;
1040+
return false;
10381041
}

include/linux/gfp_types.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ enum {
273273
*
274274
* %__GFP_ZERO returns a zeroed page on success.
275275
*
276-
* %__GFP_ZEROTAGS zeroes memory tags at allocation time if the memory itself
277-
* is being zeroed (either via __GFP_ZERO or via init_on_alloc, provided that
278-
* __GFP_SKIP_ZERO is not set). This flag is intended for optimization: setting
279-
* memory tags at the same time as zeroing memory has minimal additional
280-
* performance impact.
276+
* %__GFP_ZEROTAGS zeroes memory tags at allocation time. Setting memory tags at
277+
* the same time as zeroing memory (e.g., with __GFP_ZERO) has minimal
278+
* additional performance impact. However, __GFP_ZEROTAGS also zeroes the tags
279+
* even if memory is not getting zeroed at allocation time (e.g.,
280+
* with init_on_free).
281281
*
282282
* %__GFP_SKIP_KASAN makes KASAN skip unpoisoning on page allocation.
283283
* Used for userspace and vmalloc pages; the latter are unpoisoned by

include/linux/highmem.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,11 @@ static inline void clear_highpage_kasan_tagged(struct page *page)
347347

348348
#ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGES
349349

350-
/* Return false to let people know we did not initialize the pages */
351-
static inline bool tag_clear_highpages(struct page *page, int numpages)
350+
/* Returns true if the caller has to initialize the pages */
351+
static inline bool tag_clear_highpages(struct page *page, int numpages,
352+
bool clear_pages)
352353
{
353-
return false;
354+
return clear_pages;
354355
}
355356

356357
#endif

mm/page_alloc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,9 +1808,9 @@ static inline bool should_skip_init(gfp_t flags)
18081808
inline void post_alloc_hook(struct page *page, unsigned int order,
18091809
gfp_t gfp_flags)
18101810
{
1811+
const bool zero_tags = gfp_flags & __GFP_ZEROTAGS;
18111812
bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
18121813
!should_skip_init(gfp_flags);
1813-
bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS);
18141814
int i;
18151815

18161816
set_page_private(page, 0);
@@ -1832,11 +1832,11 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
18321832
*/
18331833

18341834
/*
1835-
* If memory tags should be zeroed
1836-
* (which happens only when memory should be initialized as well).
1835+
* Clearing tags can efficiently clear the memory for us as well, if
1836+
* required.
18371837
*/
18381838
if (zero_tags)
1839-
init = !tag_clear_highpages(page, 1 << order);
1839+
init = tag_clear_highpages(page, 1 << order, /* clear_pages= */init);
18401840

18411841
if (!should_skip_kasan_unpoison(gfp_flags) &&
18421842
kasan_unpoison_pages(page, order, init)) {

0 commit comments

Comments
 (0)