Skip to content

Commit d155aab

Browse files
ramosian-gliderakpm00
authored andcommitted
mm/kfence: fix KASAN hardware tag faults during late enablement
When KASAN hardware tags are enabled, re-enabling KFENCE late (via /sys/module/kfence/parameters/sample_interval) causes KASAN faults. This happens because the KFENCE pool and metadata are allocated via the page allocator, which tags the memory, while KFENCE continues to access it using untagged pointers during initialization. Use __GFP_SKIP_KASAN for late KFENCE pool and metadata allocations to ensure the memory remains untagged, consistent with early allocations from memblock. To support this, add __GFP_SKIP_KASAN to the allowlist in __alloc_contig_verify_gfp_mask(). Link: https://lkml.kernel.org/r/20260220144940.2779209-1-glider@google.com Fixes: 0ce20dd ("mm: add Kernel Electric-Fence infrastructure") Signed-off-by: Alexander Potapenko <glider@google.com> Suggested-by: Ernesto Martinez Garcia <ernesto.martinezgarcia@tugraz.at> Cc: Andrey Konovalov <andreyknvl@gmail.com> Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Greg KH <gregkh@linuxfoundation.org> Cc: Kees Cook <kees@kernel.org> Cc: Marco Elver <elver@google.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent c80f46a commit d155aab

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

mm/kfence/core.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,14 +1004,14 @@ static int kfence_init_late(void)
10041004
#ifdef CONFIG_CONTIG_ALLOC
10051005
struct page *pages;
10061006

1007-
pages = alloc_contig_pages(nr_pages_pool, GFP_KERNEL, first_online_node,
1008-
NULL);
1007+
pages = alloc_contig_pages(nr_pages_pool, GFP_KERNEL | __GFP_SKIP_KASAN,
1008+
first_online_node, NULL);
10091009
if (!pages)
10101010
return -ENOMEM;
10111011

10121012
__kfence_pool = page_to_virt(pages);
1013-
pages = alloc_contig_pages(nr_pages_meta, GFP_KERNEL, first_online_node,
1014-
NULL);
1013+
pages = alloc_contig_pages(nr_pages_meta, GFP_KERNEL | __GFP_SKIP_KASAN,
1014+
first_online_node, NULL);
10151015
if (pages)
10161016
kfence_metadata_init = page_to_virt(pages);
10171017
#else
@@ -1021,11 +1021,13 @@ static int kfence_init_late(void)
10211021
return -EINVAL;
10221022
}
10231023

1024-
__kfence_pool = alloc_pages_exact(KFENCE_POOL_SIZE, GFP_KERNEL);
1024+
__kfence_pool = alloc_pages_exact(KFENCE_POOL_SIZE,
1025+
GFP_KERNEL | __GFP_SKIP_KASAN);
10251026
if (!__kfence_pool)
10261027
return -ENOMEM;
10271028

1028-
kfence_metadata_init = alloc_pages_exact(KFENCE_METADATA_SIZE, GFP_KERNEL);
1029+
kfence_metadata_init = alloc_pages_exact(KFENCE_METADATA_SIZE,
1030+
GFP_KERNEL | __GFP_SKIP_KASAN);
10291031
#endif
10301032

10311033
if (!kfence_metadata_init)

mm/page_alloc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6928,7 +6928,8 @@ static int __alloc_contig_verify_gfp_mask(gfp_t gfp_mask, gfp_t *gfp_cc_mask)
69286928
{
69296929
const gfp_t reclaim_mask = __GFP_IO | __GFP_FS | __GFP_RECLAIM;
69306930
const gfp_t action_mask = __GFP_COMP | __GFP_RETRY_MAYFAIL | __GFP_NOWARN |
6931-
__GFP_ZERO | __GFP_ZEROTAGS | __GFP_SKIP_ZERO;
6931+
__GFP_ZERO | __GFP_ZEROTAGS | __GFP_SKIP_ZERO |
6932+
__GFP_SKIP_KASAN;
69326933
const gfp_t cc_action_mask = __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
69336934

69346935
/*

0 commit comments

Comments
 (0)