kasan: use separate (un)poison implementation for integrated init
Subsystems (7)
PATCH 1/3 kasan: use separate (un)poison implementation for integrated init
Currently with integrated init page_alloc.c needs to know whether kasan_alloc_pages() will zero initialize memory, but this will start becoming more complicated once we start adding tag initialization support for user pages. To avoid page_alloc.c needing to know more details of what integrated init will do, move the unpoisoning logic for integrated init into the HW tags implementation. Currently the logic is identical but it will diverge in subsequent patches.
For symmetry do the same for poisoning although this logic will be unaffected by subsequent patches.
--- include/linux/kasan.h | 63 ++++++++++++++++++++++++++----------------- mm/kasan/common.c | 4 +-- mm/kasan/hw_tags.c | 14 ++++++++++ mm/mempool.c | 6 +++-- mm/page_alloc.c | 56 ++++++++++++++++++++------------------ 5 files changed, 88 insertions(+), 55 deletions(-)
include/linux/kasan.h +38 -25
@@ -79,14 +79,6 @@ static inline void kasan_disable_current(void) {}#endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ -#ifdef CONFIG_KASAN - -struct kasan_cache { -int alloc_meta_offset; -int free_meta_offset; -bool is_kmalloc; -}; -#ifdef CONFIG_KASAN_HW_TAGS DECLARE_STATIC_KEY_FALSE (kasan_flag_enabled);
@@ -101,11 +93,18 @@ static inline bool kasan_has_integrated_init(void) return kasan_enabled(); } +void kasan_alloc_pages (struct page *page,unsigned int order,gfp_t flags); +void kasan_free_pages (struct page *page,unsigned int order); +#else /* CONFIG_KASAN_HW_TAGS */ static inline bool kasan_enabled (void ) { +#ifdef CONFIG_KASAN return true; +#else +return false; +#endif }static inline bool kasan_has_integrated_init (void )
@@ -113,8 +112,30 @@ static inline bool kasan_has_integrated_init(void)return false; } +static __always_inline void kasan_alloc_pages(struct page *page, +unsigned int order,gfp_t flags) +{ +/* Only available for integrated init. */ +BUG (); +} + +static __always_inline void kasan_free_pages(struct page *page, +unsigned int order) +{ +/* Only available for integrated init. */ +BUG (); +} +#endif /* CONFIG_KASAN_HW_TAGS */ +#ifdef CONFIG_KASAN + +struct kasan_cache { +int alloc_meta_offset; +int free_meta_offset; +bool is_kmalloc; +}; +slab_flags_t __kasan_never_merge (void ); static __always_inline slab_flags_t kasan_never_merge(void) {
@@ -130,20 +151,20 @@ static __always_inline void kasan_unpoison_range(const void *addr, size_t size)__kasan_unpoison_range (addr, size); } -void __kasan_alloc_pages (struct page *page,unsigned int order,bool init); -static __always_inline void kasan_alloc_pages(struct page *page, +void __kasan_poison_pages (struct page *page,unsigned int order,bool init); +static __always_inline void kasan_poison_pages(struct page *page,unsigned int order,bool init) {if (kasan_enabled ()) -__kasan_alloc_pages (page, order, init); +__kasan_poison_pages (page, order, init); } -void __kasan_free_pages (struct page *page,unsigned int order,bool init); -static __always_inline void kasan_free_pages(struct page *page, -unsigned int order,bool init) +void __kasan_unpoison_pages (struct page *page,unsigned int order,bool init); +static __always_inline void kasan_unpoison_pages(struct page *page, +unsigned int order,bool init) {if (kasan_enabled ()) -__kasan_free_pages (page, order, init); +__kasan_unpoison_pages (page, order, init); } void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
@@ -285,21 +306,13 @@ void kasan_restore_multi_shot(bool enabled);#else /* CONFIG_KASAN */ -static inline bool kasan_enabled (void ) -{ -return false; -} -static inline bool kasan_has_integrated_init (void ) -{ -return false; -}static inline slab_flags_t kasan_never_merge (void ) {return 0 ; }static inline void kasan_unpoison_range (const void *address,size_t size) {} -static inline void kasan_alloc_pages (struct page *page,unsigned int order,bool init) {} -static inline void kasan_free_pages (struct page *page,unsigned int order,bool init) {} +static inline void kasan_poison_pages (struct page *page,unsigned int order) {} +static inline void kasan_unpoison_pages (struct page *page,unsigned int order) {}static inline void kasan_cache_create (struct kmem_cache *cache,unsigned int *size,slab_flags_t *flags) {}
mm/kasan/common.c +2 -2
@@ -97,7 +97,7 @@ slab_flags_t __kasan_never_merge(void)return 0 ; } -void __kasan_alloc_pages (struct page *page,unsigned int order,bool init) +void __kasan_unpoison_pages (struct page *page,unsigned int order,bool init) {u8 tag;unsigned long i;
@@ -111,7 +111,7 @@ void __kasan_alloc_pages(struct page *page, unsigned int order, bool init)kasan_unpoison (page_address (page),PAGE_SIZE << order, init); } -void __kasan_free_pages (struct page *page,unsigned int order,bool init) +void __kasan_poison_pages (struct page *page,unsigned int order,bool init) {if (likely (!PageHighMem (page))) kasan_poison(page_address(page), PAGE_SIZE << order,
mm/kasan/hw_tags.c +14 -0
@@ -238,6 +238,20 @@ struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,return &alloc_meta->free_track[0 ]; } +void kasan_alloc_pages (struct page *page,unsigned int order,gfp_t flags) +{ +bool init = !want_init_on_free () &&want_init_on_alloc (flags); + +kasan_unpoison_pages (page, order, init); +} + +void kasan_free_pages (struct page *page,unsigned int order) +{ +bool init =want_init_on_free (); + +kasan_poison_pages (page, order, init); +} +#if IS_ENABLED (CONFIG_KASAN_KUNIT_TEST )void kasan_set_tagging_report_once (bool state)
mm/mempool.c +4 -2
@@ -106,7 +106,8 @@ static __always_inline void kasan_poison_element(mempool_t *pool, void *element)if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)kasan_slab_free_mempool (element);else if (pool->alloc == mempool_alloc_pages) -kasan_free_pages (element, (unsigned long )pool->pool_data, false); +kasan_poison_pages (element, (unsigned long )pool->pool_data, + false); }static void kasan_unpoison_element (mempool_t *pool,void *element)
@@ -114,7 +115,8 @@ static void kasan_unpoison_element(mempool_t *pool, void *element)if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)kasan_unpoison_range (element,__ksize (element));else if (pool->alloc == mempool_alloc_pages) -kasan_alloc_pages (element, (unsigned long )pool->pool_data, false); +kasan_unpoison_pages (element, (unsigned long )pool->pool_data, + false); } static __always_inline void add_element(mempool_t *pool, void *element)
mm/page_alloc.c +30 -26
100 lines not on this page · raw message
Signature · 1 line
2.31.1.607.g51e8a6a459-goog
PATCH 2/3 arm64: mte: handle tags zeroing at page allocation time
Currently, on an anonymous page fault, the kernel allocates a zeroed page and maps it in user space. If the mapping is tagged (PROT_MTE), set_pte_at() additionally clears the tags. It is, however, more efficient to clear the tags at the same time as zeroing the data on allocation. To avoid clearing the tags on any page (which may not be mapped as tagged), only do this if the vma flags contain VM_MTE. This requires introducing a new GFP flag that is used to determine whether to clear the tags.
The DC GZVA instruction with a 0 top byte (and 0 tag) requires top-byte-ignore. Set the TCR_EL1.{TBI1,TBID1} bits irrespective of whether KASAN_HW is enabled.
--- arch/arm64/include/asm/mte.h | 4 ++++ arch/arm64/include/asm/page.h | 11 +++++++++-- arch/arm64/lib/mte.S | 20 ++++++++++++++++++++ arch/arm64/mm/fault.c | 25 +++++++++++++++++++++++++ arch/arm64/mm/proc.S | 10 +++++++--- include/linux/gfp.h | 9 +++++++-- include/linux/highmem.h | 10 ++++++++++ mm/kasan/hw_tags.c | 9 ++++++++- mm/page_alloc.c | 14 +++++++++++--- 9 files changed, 101 insertions(+), 11 deletions(-)
arch/arm64/include/asm/mte.h +4 -0
@@ -37,6 +37,7 @@ void mte_free_tag_storage(char *storage);/* track which pages have valid allocation tags */ #define PG_mte_tagged PG_arch_2 +void mte_zero_clear_page_tags (void *addr);void mte_sync_tags (pte_t *ptep,pte_t pte);void mte_copy_page_tags (void *kto,const void *kfrom);void mte_thread_init_user (void );
@@ -53,6 +54,9 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request,/* unused if !CONFIG_ARM64_MTE, silence the compiler */ #define PG_mte_tagged 0 +static inline void mte_zero_clear_page_tags (void *addr) +{ +}static inline void mte_sync_tags (pte_t *ptep,pte_t pte) { }
arch/arm64/include/asm/page.h +9 -2
@@ -13,6 +13,7 @@#ifndef __ASSEMBLY__#include <linux/personality.h> /* for READ_IMPLIES_EXEC */ +#include <linux/types.h> /* for gfp_t */ #include <asm/pgtable-types.h> struct page ;
@@ -28,10 +29,16 @@ void copy_user_highpage(struct page *to, struct page *from,void copy_highpage (struct page *to,struct page *from);#define __HAVE_ARCH_COPY_HIGHPAGE -#define __alloc_zeroed_user_highpage (movableflags, vma, vaddr) \ - alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr) +struct page *__alloc_zeroed_user_highpage (gfp_t movableflags, +struct vm_area_struct *vma, +unsigned long vaddr);#define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE +#define want_zero_tags_on_free () system_supports_mte() + +void tag_clear_highpage (struct page *to); +#define __HAVE_ARCH_TAG_CLEAR_HIGHPAGE +#define clear_user_page (page, vaddr, pg) clear_page(page)#define copy_user_page (to, from, vaddr, pg) copy_page(to, from)
arch/arm64/lib/mte.S +20 -0
@@ -36,6 +36,26 @@ SYM_FUNC_START(mte_clear_page_tags) ret SYM_FUNC_END(mte_clear_page_tags) +/* + * Zero the page and tags at the same time + * + * Parameters: + * x0 - address to the beginning of the page + */ +SYM_FUNC_START(mte_zero_clear_page_tags) + mrs x1, dczid_el0 + and w1, w1, #0xf + mov x2, #4 + lsl x1, x2, x1 + and x0, x0, #(1 << MTE_TAG_SHIFT) - 1 // clear the tag + +1: dc gzva, x0 + add x0, x0, x1 + tst x0, #(PAGE_SIZE - 1) + b.ne 1b + ret +SYM_FUNC_END(mte_zero_clear_page_tags) + /* * Copy the tags from the source page to the destination one * x0 - address of the destination page
arch/arm64/mm/fault.c +25 -0
@@ -921,3 +921,28 @@ void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr,debug_exception_exit (regs); }NOKPROBE_SYMBOL (do_debug_exception); + +/* +* Used during anonymous page fault handling. +*/ +struct page *__alloc_zeroed_user_highpage (gfp_t flags, +struct vm_area_struct *vma, +unsigned long vaddr) +{ +/* +* If the page is mapped with PROT_MTE, initialise the tags at the +* point of allocation and page zeroing as this is usually faster than +* separate DC ZVA and STGM. +*/ +if (vma->vm_flags &VM_MTE ) + flags |= __GFP_ZEROTAGS; + +return alloc_page_vma (GFP_HIGHUSER | __GFP_ZERO | flags, vma, vaddr); +} + +void tag_clear_highpage (struct page *page) +{ +mte_zero_clear_page_tags (page_address (page)); +page_kasan_tag_reset (page); +set_bit (PG_mte_tagged, &page->flags); +}
arch/arm64/mm/proc.S +7 -3
@@ -46,9 +46,13 @@ #endif #ifdef CONFIG_KASAN_HW_TAGS -#define TCR_KASAN_HW_FLAGS SYS_TCR_EL1_TCMA1 | TCR_TBI1 | TCR_TBID1 +#define TCR_MTE_FLAGS SYS_TCR_EL1_TCMA1 | TCR_TBI1 | TCR_TBID1 #else -#define TCR_KASAN_HW_FLAGS 0 +/* + * The mte_zero_clear_page_tags() implementation uses DC GZVA, which relies on + * TBI being enabled at EL1. + */ +#define TCR_MTE_FLAGS TCR_TBI1 | TCR_TBID1 #endif /*
@@ -452,7 +456,7 @@ SYM_FUNC_START(__cpu_setup) msr_s SYS_TFSRE0_EL1, xzr /* set the TCR_EL1 bits */ - mov_q x10, TCR_KASAN_HW_FLAGS + mov_q x10, TCR_MTE_FLAGS orr tcr, tcr, x10 1: #endif
include/linux/gfp.h +7 -2
@@ -53,8 +53,9 @@ struct vm_area_struct;#define ___GFP_HARDWALL 0x100000u#define ___GFP_THISNODE 0x200000u#define ___GFP_ACCOUNT 0x400000u +#define ___GFP_ZEROTAGS 0x800000u#ifdef CONFIG_LOCKDEP -#define ___GFP_NOLOCKDEP 0x800000u +#define ___GFP_NOLOCKDEP 0x1000000u#else #define ___GFP_NOLOCKDEP 0#endif
@@ -229,16 +230,20 @@ struct vm_area_struct;* %__GFP_COMP address compound page metadata. * * %__GFP_ZERO returns a zeroed page on success. +* +* %__GFP_ZEROTAGS returns a page with zeroed memory tags on success, if +* __GFP_ZERO is set. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) +#define __GFP_ZEROTAGS ((__force gfp_t)___GFP_ZEROTAGS)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (23 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
include/linux/highmem.h +10 -0
@@ -204,6 +204,16 @@ static inline void clear_highpage(struct page *page)kunmap_atomic (kaddr); } +#ifndef __HAVE_ARCH_TAG_CLEAR_HIGHPAGE + +#define want_zero_tags_on_free () false + +static inline void tag_clear_highpage (struct page *page) +{ +} + +#endif +/* * If we pass in a base or tail page, we can zero up to PAGE_SIZE. * If we pass in a head page, we can zero up to the size of the compound page.
mm/kasan/hw_tags.c +8 -1
@@ -242,7 +242,14 @@ void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags) {bool init = !want_init_on_free () &&want_init_on_alloc (flags); -kasan_unpoison_pages (page, order, init); +if (flags & __GFP_ZEROTAGS) { +int i; + +for (i =0 ; i !=1 << order; ++i) +tag_clear_highpage (page + i); + }else { +kasan_unpoison_pages (page, order, init); + } }void kasan_free_pages (struct page *page,unsigned int order)
mm/page_alloc.c +11 -3
@@ -1219,10 +1219,16 @@ static int free_tail_pages_check(struct page *head_page, struct page *page)return ret; } -static void kernel_init_free_pages (struct page *page,int numpages) +static void kernel_init_free_pages (struct page *page,int numpages,bool zero_tags) {int i; +if (zero_tags) { +for (i =0 ; i < numpages; i++) +tag_clear_highpage (page + i); +return ; + } +/* s390's use of memset() could override KASAN redzones. */ kasan_disable_current ();for (i =0 ; i < numpages; i++) {
@@ -1314,7 +1320,8 @@ static __always_inline bool free_pages_prepare(struct page *page,bool init =want_init_on_free ();if (init) -kernel_init_free_pages (page,1 << order); +kernel_init_free_pages (page,1 << order, +want_zero_tags_on_free ());if (!skip_kasan_poison)kasan_poison_pages (page, order, init); }
@@ -2350,7 +2357,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order,kasan_unpoison_pages (page, order, init);if (init) -kernel_init_free_pages (page,1 << order); +kernel_init_free_pages (page,1 << order, + gfp_flags & __GFP_ZEROTAGS); }set_page_owner (page, order, gfp_flags);
Signature · 1 line
2.31.1.607.g51e8a6a459-goog
PATCH 3/3 kasan: allow freed user page poisoning to be disabled with HW tags
In reply to Peter CollingbournePoisoning freed pages protects against kernel use-after-free. The likelihood of such a bug involving kernel pages is significantly higher than that for user pages. At the same time, poisoning freed pages can impose a significant performance cost, which cannot always be justified for user pages given the lower probability of finding a bug. Therefore, make it possible to configure the kernel to disable freed user page poisoning when using HW tags via the new kasan.skip_user_poison_on_free command line option.
--- include/linux/gfp.h | 13 ++++++++++--- include/linux/page-flags.h | 9 +++++++++ include/trace/events/mmflags.h | 9 ++++++++- mm/kasan/hw_tags.c | 10 ++++++++++ mm/page_alloc.c | 12 +++++++----- 5 files changed, 44 insertions(+), 9 deletions(-)
include/linux/gfp.h +10 -3
@@ -54,8 +54,9 @@ struct vm_area_struct;#define ___GFP_THISNODE 0x200000u#define ___GFP_ACCOUNT 0x400000u#define ___GFP_ZEROTAGS 0x800000u +#define ___GFP_SKIP_KASAN_POISON 0x1000000u#ifdef CONFIG_LOCKDEP -#define ___GFP_NOLOCKDEP 0x1000000u +#define ___GFP_NOLOCKDEP 0x2000000u#else #define ___GFP_NOLOCKDEP 0#endif
@@ -233,17 +234,22 @@ struct vm_area_struct;* * %__GFP_ZEROTAGS returns a page with zeroed memory tags on success, if * __GFP_ZERO is set. +* +* %__GFP_SKIP_KASAN_POISON returns a page which does not need to be poisoned +* on deallocation. Typically used for userspace pages. Currently only has an +* effect in HW tags mode, and only if a command line option is set. */ #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)#define __GFP_COMP ((__force gfp_t)___GFP_COMP)#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO)#define __GFP_ZEROTAGS ((__force gfp_t)___GFP_ZEROTAGS) +#define __GFP_SKIP_KASAN_POISON ((__force gfp_t)___GFP_SKIP_KASAN_POISON)/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (25 + IS_ENABLED(CONFIG_LOCKDEP))#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
@@ -320,7 +326,8 @@ struct vm_area_struct;#define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM)#define GFP_NOIO (__GFP_RECLAIM)#define GFP_NOFS (__GFP_RECLAIM | __GFP_IO) -#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL) +#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | \ + __GFP_HARDWALL | __GFP_SKIP_KASAN_POISON)#define GFP_DMA __GFP_DMA#define GFP_DMA32 __GFP_DMA32#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM)
include/linux/page-flags.h +9 -0
@@ -137,6 +137,9 @@ enum pageflags {#endif #ifdef CONFIG_64BIT PG_arch_2, +#endif +#ifdef CONFIG_KASAN_HW_TAGS + PG_skip_kasan_poison, #endif __NR_PAGEFLAGS,
@@ -443,6 +446,12 @@ TESTCLEARFLAG(Young, young, PF_ANY) PAGEFLAG(Idle, idle, PF_ANY)#endif +#ifdef CONFIG_KASAN_HW_TAGS +PAGEFLAG(SkipKASanPoison, skip_kasan_poison, PF_HEAD) +#else +PAGEFLAG_FALSE(SkipKASanPoison) +#endif +/* * PageReported() is used to track reported free pages within the Buddy * allocator. We can use the non-atomic version of the test and set
include/trace/events/mmflags.h +8 -1
@@ -85,6 +85,12 @@#define IF_HAVE_PG_ARCH_2 (flag,string)#endif +#ifdef CONFIG_KASAN_HW_TAGS +#define IF_HAVE_PG_SKIP_KASAN_POISON (flag,string) ,{1UL << flag, string} +#else +#define IF_HAVE_PG_SKIP_KASAN_POISON (flag,string) +#endif +#define __def_pageflag_names \ {1UL << PG_locked, "locked" }, \ {1UL << PG_waiters, "waiters" }, \
@@ -112,7 +118,8 @@ IF_HAVE_PG_UNCACHED(PG_uncached, "uncached" ) \ IF_HAVE_PG_HWPOISON(PG_hwpoison, "hwpoison" ) \ IF_HAVE_PG_IDLE(PG_young, "young" ) \ IF_HAVE_PG_IDLE(PG_idle, "idle" ) \ -IF_HAVE_PG_ARCH_2(PG_arch_2, "arch_2" ) +IF_HAVE_PG_ARCH_2(PG_arch_2, "arch_2" ) \ +IF_HAVE_PG_SKIP_KASAN_POISON(PG_skip_kasan_poison, "skip_kasan_poison")#define show_page_flags (flags) \ (flags) ? __print_flags(flags, "|", \
mm/kasan/hw_tags.c +10 -0
@@ -238,10 +238,20 @@ struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,return &alloc_meta->free_track[0 ]; } +static bool skip_user_poison_on_free; +static int __init skip_user_poison_on_free_param (char *buf) +{ +return kstrtobool (buf, &skip_user_poison_on_free); +} +early_param ("kasan.skip_user_poison_on_free" , skip_user_poison_on_free_param); +void kasan_alloc_pages (struct page *page,unsigned int order,gfp_t flags) {bool init = !want_init_on_free () &&want_init_on_alloc (flags); +if (skip_user_poison_on_free && (flags & __GFP_SKIP_KASAN_POISON)) +SetPageSkipKASanPoison (page); +if (flags & __GFP_ZEROTAGS) {int i;
mm/page_alloc.c +7 -5
@@ -394,11 +394,12 @@ static DEFINE_STATIC_KEY_TRUE(deferred_pages);* on-demand allocation and then freed again before the deferred pages * initialization is done, but this is not likely to happen. */ -static inline bool should_skip_kasan_poison (fpi_t fpi_flags) +static inline bool should_skip_kasan_poison (struct page *page,fpi_t fpi_flags) {return static_branch_unlikely (&deferred_pages) || (!IS_ENABLED (CONFIG_KASAN_GENERIC ) && - (fpi_flags &FPI_SKIP_KASAN_POISON )); + (fpi_flags &FPI_SKIP_KASAN_POISON )) || +PageSkipKASanPoison (page); }/* Returns true if the struct page for the pfn is uninitialised */
@@ -449,10 +450,11 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn) return false; }#else -static inline bool should_skip_kasan_poison (fpi_t fpi_flags) +static inline bool should_skip_kasan_poison (struct page *page,fpi_t fpi_flags) {return (!IS_ENABLED (CONFIG_KASAN_GENERIC ) && - (fpi_flags &FPI_SKIP_KASAN_POISON )); + (fpi_flags &FPI_SKIP_KASAN_POISON )) || +PageSkipKASanPoison (page); }static inline bool early_page_uninitialised (unsigned long pfn)
@@ -1244,7 +1246,7 @@ static __always_inline bool free_pages_prepare(struct page *page, unsigned int order, bool check_free, fpi_t fpi_flags) { int bad = 0; -bool skip_kasan_poison =should_skip_kasan_poison (fpi_flags); +bool skip_kasan_poison =should_skip_kasan_poison (page, fpi_flags);VM_BUG_ON_PAGE (PageTail (page), page);
Signature · 1 line
2.31.1.607.g51e8a6a459-goog
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Peter,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.13-rc1 next-20210511]
[cannot apply to hnaz-linux-mm/master arm64/for-next/core tip/perf/core] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Peter-Collingbourne/kasan-use-separate-un-poison-implementation-for-integrated-init/20210511-153308 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 1140ab592e2ebf8153d2b322604031a8868ce7a5 config: powerpc64-randconfig-r033-20210511 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project a0fed635fe1701470062495a6ffee1c608f3f1bc)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install powerpc64 cross compiling tool for clang build
# apt-get install binutils-powerpc64-linux-gnu
# https://github.com/0day-ci/linux/commit/4c25c2115c30074af7d702e9db8ba9d0d0b2c578
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Peter-Collingbourne/kasan-use-separate-un-poison-implementation-for-integrated-init/20210511-153308 git checkout 4c25c2115c30074af7d702e9db8ba9d0d0b2c578
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=powerpc64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> mm/mempool.c:110:8: error: too many arguments to function call, expected 2, have 3
false);
^~~~~
include/linux/kasan.h:314:20: note: 'kasan_poison_pages' declared here
static inline void kasan_poison_pages(struct page *page, unsigned int order) {}
^
mm/mempool.c:119:10: error: too many arguments to function call, expected 2, have 3
false);
^~~~~
include/linux/kasan.h:315:20: note: 'kasan_unpoison_pages' declared here
static inline void kasan_unpoison_pages(struct page *page, unsigned int order) {}
^
2 errors generated.
--
>> mm/page_alloc.c:1319:36: error: too many arguments to function call, expected 2, have 3
kasan_poison_pages(page, order, init);
~~~~~~~~~~~~~~~~~~ ^~~~
include/linux/kasan.h:314:20: note: 'kasan_poison_pages' declared here
static inline void kasan_poison_pages(struct page *page, unsigned int order) {}
^
mm/page_alloc.c:2351:37: error: too many arguments to function call, expected 2, have 3
kasan_unpoison_pages(page, order, init);
~~~~~~~~~~~~~~~~~~~~ ^~~~
include/linux/kasan.h:315:20: note: 'kasan_unpoison_pages' declared here
static inline void kasan_unpoison_pages(struct page *page, unsigned int order) {}
^
mm/page_alloc.c:3655:15: warning: no previous prototype for function 'should_fail_alloc_page' [-Wmissing-prototypes]
noinline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
^
mm/page_alloc.c:3655:10: note: declare 'static' if the function is not intended to be used outside of this translation unit
noinline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
^
static 1 warning and 2 errors generated.
vim +110 mm/mempool.c
103
104 static __always_inline void kasan_poison_element(mempool_t *pool, void *element)
105 {
106 if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
107 kasan_slab_free_mempool(element);
108 else if (pool->alloc == mempool_alloc_pages)
109 kasan_poison_pages(element, (unsigned long)pool->pool_data,
> 110 false);
111 }
112
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Peter,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v5.13-rc1 next-20210511]
[cannot apply to hnaz-linux-mm/master arm64/for-next/core tip/perf/core] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Peter-Collingbourne/kasan-use-separate-un-poison-implementation-for-integrated-init/20210511-153308 base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 1140ab592e2ebf8153d2b322604031a8868ce7a5 config: arm64-randconfig-r025-20210511 (attached as .config) compiler: aarch64-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/4c25c2115c30074af7d702e9db8ba9d0d0b2c578
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Peter-Collingbourne/kasan-use-separate-un-poison-implementation-for-integrated-init/20210511-153308 git checkout 4c25c2115c30074af7d702e9db8ba9d0d0b2c578
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross W=1 ARCH=arm64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from arch/arm64/kernel/probes/kprobes.c:10:
include/linux/kasan.h: In function 'kasan_alloc_pages':
>> include/linux/kasan.h:119:2: error: implicit declaration of function 'BUG' [-Werror=implicit-function-declaration]
119 | BUG();
| ^~~cc1: some warnings being treated as errors
vim +/BUG +119 include/linux/kasan.h
114
115 static __always_inline void kasan_alloc_pages(struct page *page,
116 unsigned int order, gfp_t flags)
117 {
118 /* Only available for integrated init. */
> 119 BUG();
120 }
121
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Catalin Marinas
maintainerRe: [PATCH 2/3] arm64: mte: handle tags zeroing at page allocation time
In reply to Peter CollingbourneHi Peter,
First of all, could you please add a cover letter to your series (in general) explaining the rationale for the patches, e.g. optimise tag initialisation for user pages? It makes it a lot easier to review if the overall picture is presented in the cover.
Quoted 9 lines · Peter Collingbourne
Currently, on an anonymous page fault, the kernel allocates a zeroed page and maps it in user space. If the mapping is tagged (
PROT_MTE),set_pte_at()additionally clears the tags. It is, however, more efficient to clear the tags at the same time as zeroing the data on allocation. To avoid clearing the tags on any page (which may not be mapped as tagged), only do this if the vma flags containVM_MTE. This requires introducing a new GFP flag that is used to determine whether to clear the tags.
Quoted 8 lines
The DC GZVA instruction with a 0 top byte (and 0 tag) requires top-byte-ignore. Set the TCR_EL1.{TBI1,TBID1} bits irrespective of whether
KASAN_HWis enabled.Signed-off-by Peter Collingbourne <[email protected]>Co-developed-by Catalin Marinas <[email protected]>Signed-off-by Catalin Marinas <[email protected]>
This doesn't mention that the patch adds tag clearing on free as well. I'd actually leave this part out for a separate patch. It's not done for tags in current mainline when kasan is disabled, AFAICT.
Quoted 14 lines
arch/arm64/include/asm/page.h +1
-0@@ -13,6 +13,7 @@#ifndef __ASSEMBLY__#include <linux/personality.h> /* for READ_IMPLIES_EXEC */ +#include <linux/types.h> /* for gfp_t */ #include <asm/pgtable-types.h> struct page ;@@ -28,10 +29,16 @@ void copy_user_highpage(struct page *to, struct page *from,void copy_highpage (struct page *to,struct page *from);
arch/arm64/include/asm/page.h void copy_user_highpage(struct page *to, struct page *from, · line 37
Quoted 10 lines
arch/arm64/include/asm/page.h +4
-2#define __HAVE_ARCH_COPY_HIGHPAGE -#define __alloc_zeroed_user_highpage (movableflags, vma, vaddr) \ - alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr) +struct page *__alloc_zeroed_user_highpage (gfp_t movableflags, +struct vm_area_struct *vma, +unsigned long vaddr);#define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE +#define want_zero_tags_on_free () system_supports_mte()
As I said above, unless essential to this patch, please move it to a separate one.
Also, do we need this even when the kernel doesn't have kasan_hw?
Quoted 23 lines
arch/arm64/mm/fault.c +15
-0@@ -921,3 +921,28 @@ void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr,debug_exception_exit (regs); }NOKPROBE_SYMBOL (do_debug_exception); + +/* +* Used during anonymous page fault handling. +*/ +struct page *__alloc_zeroed_user_highpage (gfp_t flags, +struct vm_area_struct *vma, +unsigned long vaddr) +{ +/* +* If the page is mapped with PROT_MTE, initialise the tags at the +* point of allocation and page zeroing as this is usually faster than +* separate DC ZVA and STGM. +*/ +if (vma->vm_flags &VM_MTE ) + flags |= __GFP_ZEROTAGS;
arch/arm64/mm/fault.c void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr, · line 948
Quoted 10 lines
arch/arm64/mm/fault.c +10
-0+ +return alloc_page_vma (GFP_HIGHUSER | __GFP_ZERO | flags, vma, vaddr); +} + +void tag_clear_highpage (struct page *page) +{ +mte_zero_clear_page_tags (page_address (page)); +page_kasan_tag_reset (page); +set_bit (PG_mte_tagged, &page->flags); +}
Do we need the page_kasan_tag_reset() here? Maybe we do. Is it because kasan_alloc_pages() is no longer calls kasan_unpoison_pages() below?
Quoted 32 lines
mm/kasan/hw_tags.c +8
-1@@ -242,7 +242,14 @@ void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags) {bool init = !want_init_on_free () &&want_init_on_alloc (flags); -kasan_unpoison_pages (page, order, init); +if (flags & __GFP_ZEROTAGS) { +int i; + +for (i =0 ; i !=1 << order; ++i) +tag_clear_highpage (page + i); + }else { +kasan_unpoison_pages (page, order, init); + } }void kasan_free_pages (struct page *page,unsigned int order)mm/page_alloc.c +1
-1@@ -1219,10 +1219,16 @@ static int free_tail_pages_check(struct page *head_page, struct page *page)return ret; } -static void kernel_init_free_pages (struct page *page,int numpages) +static void kernel_init_free_pages (struct page *page,int numpages,bool zero_tags) {int i;
mm/page_alloc.c static int free_tail_pages_check(struct page *head_page, struct page *page) · line 1234
Quoted 10 lines
mm/page_alloc.c +6
-0+if (zero_tags) { +for (i =0 ; i < numpages; i++) +tag_clear_highpage (page + i); +return ; + } +/* s390's use of memset() could override KASAN redzones. */ kasan_disable_current ();for (i =0 ; i < numpages; i++) {
This function has another loop calling clear_highpage(). Do we end up zeroing the page twice?
mm/page_alloc.c static __always_inline bool free_pages_prepare(struct page *page, · line 1327
Quoted 10 lines
mm/page_alloc.c +2
-1@@ -1314,7 +1320,8 @@ static __always_inline bool free_pages_prepare(struct page *page,bool init =want_init_on_free ();if (init) -kernel_init_free_pages (page,1 << order); +kernel_init_free_pages (page,1 << order, +want_zero_tags_on_free ());if (!skip_kasan_poison)kasan_poison_pages (page, order, init); }
I think passing 'false' here to kernel_init_free_pages() matches the current mainline. You could make this dependent on kasan_hw being enabled rather than just system_supports_mte(). With kasan_hw disabled, the kernel accesses are not checked anyway, so it's pointless to erase the tags on free.
Signature · 1 line
Catalin
Re: [PATCH 2/3] arm64: mte: handle tags zeroing at page allocation time
Quoted 7 lines · Catalin Marinas
Hi Peter,
First of all, could you please add a cover letter to your series (in general) explaining the rationale for the patches, e.g. optimise tag initialisation for user pages? It makes it a lot easier to review if the overall picture is presented in the cover.
Sure. It seems appropriate in cases where the series is doing a number of different things like this series, but maybe not in simpler cases (e.g. one cleanup patch followed by a main patch with a self-explanatory commit message).
Quoted 14 lines · Peter Collingbourne
Quoted 13 lines
Currently, on an anonymous page fault, the kernel allocates a zeroed page and maps it in user space. If the mapping is tagged (
PROT_MTE),set_pte_at()additionally clears the tags. It is, however, more efficient to clear the tags at the same time as zeroing the data on allocation. To avoid clearing the tags on any page (which may not be mapped as tagged), only do this if the vma flags containVM_MTE. This requires introducing a new GFP flag that is used to determine whether to clear the tags.The DC GZVA instruction with a 0 top byte (and 0 tag) requires top-byte-ignore. Set the TCR_EL1.{TBI1,TBID1} bits irrespective of whether
KASAN_HWis enabled.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Peter Collingbourne <[email protected]>Co-developed-by Catalin Marinas <[email protected]>Signed-off-by Catalin Marinas <[email protected]>This doesn't mention that the patch adds tag clearing on free as well. I'd actually leave this part out for a separate patch. It's not done for tags in current mainline when kasan is disabled, AFAICT.
The tag clearing on free was thought to be necessary (because clear on free implies no clear on alloc) but, upon further reflection, it isn't. This is because we clear the struct page flags, including PG_mte_tagged, on free, which means that we will set the tags if we later end up needing to reuse the page as a tagged page. This means that clear on free is quite inefficient, but no less efficient than before. As you mentioned, we can leave any improvements there to a separate patch.
Quoted 23 lines
Quoted 23 lines
arch/arm64/include/asm/page.h +4
-2@@ -13,6 +13,7 @@#ifndef __ASSEMBLY__#include <linux/personality.h> /* for READ_IMPLIES_EXEC */ +#include <linux/types.h> /* for gfp_t */ #include <asm/pgtable-types.h> struct page ;@@ -28,10 +29,16 @@ void copy_user_highpage(struct page *to, struct page *from,void copy_highpage (struct page *to,struct page *from);#define __HAVE_ARCH_COPY_HIGHPAGE -#define __alloc_zeroed_user_highpage (movableflags, vma, vaddr) \ - alloc_page_vma(GFP_HIGHUSER | __GFP_ZERO | movableflags, vma, vaddr) +struct page *__alloc_zeroed_user_highpage (gfp_t movableflags, +struct vm_area_struct *vma, +unsigned long vaddr);#define __HAVE_ARCH_ALLOC_ZEROED_USER_HIGHPAGE
Quoted 4 lines
arch/arm64/include/asm/page.h void copy_user_highpage(struct page *to, struct page *from, · line 37
Quoted 1 line
arch/arm64/include/asm/page.h +1
-0+#define want_zero_tags_on_free () system_supports_mte()As I said above, unless essential to this patch, please move it to a separate one.
Will do.
Quoted 1 line
Also, do we need this even when the kernel doesn't have
kasan_hw?
Yes, if we preserved PG_mte_tagged across page free/alloc then this would be needed even without KASAN.
Quoted 28 lines
Quoted 28 lines
arch/arm64/mm/fault.c +20
-0@@ -921,3 +921,28 @@ void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr,debug_exception_exit (regs); }NOKPROBE_SYMBOL (do_debug_exception); + +/* +* Used during anonymous page fault handling. +*/ +struct page *__alloc_zeroed_user_highpage (gfp_t flags, +struct vm_area_struct *vma, +unsigned long vaddr) +{ +/* +* If the page is mapped with PROT_MTE, initialise the tags at the +* point of allocation and page zeroing as this is usually faster than +* separate DC ZVA and STGM. +*/ +if (vma->vm_flags &VM_MTE ) + flags |= __GFP_ZEROTAGS; + +return alloc_page_vma (GFP_HIGHUSER | __GFP_ZERO | flags, vma, vaddr); +} + +void tag_clear_highpage (struct page *page)
Quoted 8 lines
arch/arm64/mm/fault.c void do_debug_exception(unsigned long addr_if_watchpoint, unsigned int esr, · line 948
Quoted 5 lines
arch/arm64/mm/fault.c +5
-0+{ +mte_zero_clear_page_tags (page_address (page)); +page_kasan_tag_reset (page); +set_bit (PG_mte_tagged, &page->flags); +}Do we need the
page_kasan_tag_reset()here? Maybe we do. Is it becausekasan_alloc_pages()is no longer callskasan_unpoison_pages()below?
Yes, otherwise the page tag will be left at an arbitrary (most likely poison) value, which would mean that any kernel-side accesses to these pages would fail (unless userspace happened to tag its memory using the page tag). page_kasan_tag_reset() sets the page tag to the TCMA tag which allows those accesses to succeed. We need to call page_kasan_tag_reset() in mte_sync_page_tags() for similar reasons. It's unrelated to no longer calling kasan_unpoison_pages() because even if we were still calling it the end result would still be that userspace's tags won't necessarily match the kernel's page tag.
Quoted 33 lines
Quoted 33 lines
mm/kasan/hw_tags.c +8
-1@@ -242,7 +242,14 @@ void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags) {bool init = !want_init_on_free () &&want_init_on_alloc (flags); -kasan_unpoison_pages (page, order, init); +if (flags & __GFP_ZEROTAGS) { +int i; + +for (i =0 ; i !=1 << order; ++i) +tag_clear_highpage (page + i); + }else { +kasan_unpoison_pages (page, order, init); + } }void kasan_free_pages (struct page *page,unsigned int order)mm/page_alloc.c +1
-1@@ -1219,10 +1219,16 @@ static int free_tail_pages_check(struct page *head_page, struct page *page)return ret; } -static void kernel_init_free_pages (struct page *page,int numpages) +static void kernel_init_free_pages (struct page *page,int numpages,bool zero_tags) {int i;
Quoted 12 lines
mm/page_alloc.c static int free_tail_pages_check(struct page *head_page, struct page *page) · line 1234
Quoted 9 lines
mm/page_alloc.c +6
-0+if (zero_tags) { +for (i =0 ; i < numpages; i++) +tag_clear_highpage (page + i); +return ; + } +/* s390's use of memset() could override KASAN redzones. */ kasan_disable_current ();for (i =0 ; i < numpages; i++) {This function has another loop calling
clear_highpage(). Do we end up zeroing the page twice?
No because we return after the loop that calls tag_clear_highpage().
Quoted 16 lines
mm/page_alloc.c static __always_inline bool free_pages_prepare(struct page *page, · line 1327
Quoted 10 lines
mm/page_alloc.c +2
-1@@ -1314,7 +1320,8 @@ static __always_inline bool free_pages_prepare(struct page *page,bool init =want_init_on_free ();if (init) -kernel_init_free_pages (page,1 << order); +kernel_init_free_pages (page,1 << order, +want_zero_tags_on_free ());if (!skip_kasan_poison)kasan_poison_pages (page, order, init); }I think passing 'false' here to
kernel_init_free_pages()matches the current mainline. You could make this dependent onkasan_hwbeing enabled rather than justsystem_supports_mte(). Withkasan_hwdisabled, the kernel accesses are not checked anyway, so it's pointless to erase the tags on free.
I'll just pass false here as it's unneeded for KASAN.
Peter