For allocations that will be accessed only with match-all pointers (e.g., kernel stacks), setting tags is wasted work. If the caller already set __GFP_SKIP_KASAN, don’t skip zeroing the pages and don’t set KASAN_VMALLOC_PROT_NORMAL so kasan_unpoison_vmalloc() returns early without tagging. Before this patch, __GFP_SKIP_KASAN wasn't being used with vmalloc APIs. So it wasn't being checked. Now its being checked and acted upon. Other KASAN modes are unchanged because __GFP_SKIP_KASAN isn't defined there. This is a preparatory patch for optimizing kernel stack allocations. Signed-off-by: Muhammad Usama Anjum --- Changes since v1: - Simplify skip conditions based on the fact that __GFP_SKIP_KASAN is zero in non-hw-tags mode. - Add __GFP_SKIP_KASAN to GFP_VMALLOC_SUPPORTED list of flags --- mm/vmalloc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index c607307c657a6..69ae205effb46 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3939,7 +3939,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, __GFP_NOFAIL | __GFP_ZERO |\ __GFP_NORETRY | __GFP_RETRY_MAYFAIL |\ GFP_NOFS | GFP_NOIO | GFP_KERNEL_ACCOUNT |\ - GFP_USER | __GFP_NOLOCKDEP) + GFP_USER | __GFP_NOLOCKDEP | __GFP_SKIP_KASAN) static gfp_t vmalloc_fix_flags(gfp_t flags) { @@ -3980,6 +3980,8 @@ static gfp_t vmalloc_fix_flags(gfp_t flags) * * %__GFP_NOWARN can be used to suppress failure messages. * + * %__GFP_SKIP_KASAN can be used to skip poisoning + * * Can not be called from interrupt nor NMI contexts. * Return: the address of the area or %NULL on failure */ @@ -4041,7 +4043,9 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, * kasan_unpoison_vmalloc(). */ if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL)) { - if (kasan_hw_tags_enabled()) { + bool skip_kasan = gfp_mask & __GFP_SKIP_KASAN; + + if (kasan_hw_tags_enabled() && !skip_kasan) { /* * Modify protection bits to allow tagging. * This must be done before mapping. @@ -4057,7 +4061,8 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align, } /* Take note that the mapping is PAGE_KERNEL. */ - kasan_flags |= KASAN_VMALLOC_PROT_NORMAL; + if (!skip_kasan) + kasan_flags |= KASAN_VMALLOC_PROT_NORMAL; } /* Allocate physical pages and map them into vmalloc space. */ -- 2.47.3