These functions are page_alloc lifecycle hooks, called exclusively from __free_pages_prepare() and post_alloc_hook(), and are more than raw page-poisoning primitives, as their name would suggest by analogy with kasan_[un]poison(). Therefore, rename them to kasan_page_{alloc,free}(), matching kasan_slab_{alloc,free}() and page_table_check_{alloc,free}(), which clarifies their allocator-hook semantics and frees up their previous names for upcoming page-granularity tagging helpers. Signed-off-by: Pierre-Clément Tosi --- include/linux/kasan.h | 24 ++++++++++++------------ mm/kasan/common.c | 8 ++++---- mm/page_alloc.c | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index c1ac7971601e..d1a7bbb8c217 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -110,20 +110,20 @@ static __always_inline void kasan_unpoison_range(const void *addr, size_t size) __kasan_unpoison_range(addr, size); } -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) +void __kasan_page_free(struct page *page, unsigned int order, bool init); +static __always_inline void kasan_page_free(struct page *page, + unsigned int order, bool init) { if (kasan_enabled()) - __kasan_poison_pages(page, order, init); + __kasan_page_free(page, order, init); } -bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init); -static __always_inline bool kasan_unpoison_pages(struct page *page, - unsigned int order, bool init) +bool __kasan_page_alloc(struct page *page, unsigned int order, bool init); +static __always_inline bool kasan_page_alloc(struct page *page, + unsigned int order, bool init) { if (kasan_enabled()) - return __kasan_unpoison_pages(page, order, init); + return __kasan_page_alloc(page, order, init); return false; } @@ -410,10 +410,10 @@ static __always_inline bool kasan_check_byte(const void *addr) #else /* CONFIG_KASAN */ static inline void kasan_unpoison_range(const void *address, size_t size) {} -static inline void kasan_poison_pages(struct page *page, unsigned int order, - bool init) {} -static inline bool kasan_unpoison_pages(struct page *page, unsigned int order, - bool init) +static inline void kasan_page_free(struct page *page, unsigned int order, + bool init) {} +static inline bool kasan_page_alloc(struct page *page, unsigned int order, + bool init) { return false; } diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 0c198ad895b4..b9f58320eff1 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -150,7 +150,7 @@ static void unpoison_page_alloc(struct page *page, unsigned int order, page_kasan_tag_set(page + i, tag); } -bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) +bool __kasan_page_alloc(struct page *page, unsigned int order, bool init) { if (unlikely(PageHighMem(page))) return false; @@ -163,7 +163,7 @@ bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init) return true; } -void __kasan_poison_pages(struct page *page, unsigned int order, bool init) +void __kasan_page_free(struct page *page, unsigned int order, bool init) { if (likely(!PageHighMem(page))) kasan_poison(page_address(page), PAGE_SIZE << order, @@ -343,7 +343,7 @@ void __kasan_kfree_large(void *ptr, unsigned long ip) { check_page_allocation(ptr, ip); - /* The object will be poisoned by kasan_poison_pages(). */ + /* The object will be poisoned by kasan_page_free(). */ } static inline void unpoison_slab_object(struct kmem_cache *cache, void *object, @@ -469,7 +469,7 @@ void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, if (unlikely(ptr == NULL)) return NULL; - /* The object has already been unpoisoned by kasan_unpoison_pages(). */ + /* The object has already been unpoisoned by kasan_page_alloc(). */ poison_kmalloc_large_redzone(ptr, size, flags); /* Keep the tag that was set by alloc_pages(). */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index aa1ed599c7c6..24a4d8f10de9 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1192,7 +1192,7 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) * 2. The allocation was not unpoisoned due to __GFP_SKIP_KASAN, with the * exception of pages unpoisoned by kasan_unpoison_vmalloc. * 3. The allocation was excluded from being checked due to sampling, - * see the call to kasan_unpoison_pages. + * see the call to kasan_page_alloc. * * Poisoning pages during deferred memory init will greatly lengthen the * process and cause problem in large memory systems as the deferred pages @@ -1433,7 +1433,7 @@ static __always_inline bool __free_pages_prepare(struct page *page, * page becomes unavailable via debug_pagealloc or arch_free_page. */ if (!skip_kasan_poison) { - kasan_poison_pages(page, order, init); + kasan_page_free(page, order, init); /* Memory is already initialized if KASAN did it internally. */ if (kasan_has_integrated_init()) @@ -1853,7 +1853,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order, init = tag_clear_highpages(page, 1 << order, /* clear_pages= */init); if (!should_skip_kasan_unpoison(gfp_flags) && - kasan_unpoison_pages(page, order, init)) { + kasan_page_alloc(page, order, init)) { /* Take note that memory was initialized by KASAN. */ if (kasan_has_integrated_init()) init = false; -- 2.56.1.839.g22a8290db9-goog Multiple KASAN callers deal with whole-page ranges, which they pass directly into the byte-granularity kasan_[un]poison() API. However, certain KASAN implementations such as MTE (HW_TAGS on arm64) already provide optimized page-granularity routines, for tagged user memory. Therefore, introduce a KASAN abstraction for such arch APIs, which are expected to perform better (purpose-built & I-cache resident if user MTE is active) and lead to more compact KASAN code by replacing the inlined arch_set_mem_tag_range() and its unnecessary head/tail handling code. Fall back to the byte-granularity API if this API isn't supported. For now, that includes arm64 but a future change will switch it over. Convert whole-page users to the new API, slightly improving readability. Note that npages==0 is short-circuited since there's nothing to do, permitting arch_set_mem_tag_pages() to directly wrap arch routines that assume a non-zero page count. Signed-off-by: Pierre-Clément Tosi --- mm/kasan/common.c | 19 ++++++++++--------- mm/kasan/kasan.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index b9f58320eff1..574146d852b4 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -144,8 +144,8 @@ static void unpoison_page_alloc(struct page *page, unsigned int order, unsigned long i; tag = kasan_random_tag(); - kasan_unpoison(set_tag(page_address(page), tag), - PAGE_SIZE << order, init); + kasan_unpoison_pages(set_tag(page_address(page), tag), + 1U << order, init); for (i = 0; i < (1UL << order); i++) page_kasan_tag_set(page + i, tag); } @@ -166,19 +166,19 @@ bool __kasan_page_alloc(struct page *page, unsigned int order, bool init) void __kasan_page_free(struct page *page, unsigned int order, bool init) { if (likely(!PageHighMem(page))) - kasan_poison(page_address(page), PAGE_SIZE << order, - KASAN_PAGE_FREE, init); + kasan_poison_pages(page_address(page), 1U << order, + KASAN_PAGE_FREE, init); } void __kasan_poison_slab(struct slab *slab) { struct page *page = slab_page(slab); + unsigned long nr = compound_nr(page); unsigned long i; - for (i = 0; i < compound_nr(page); i++) + for (i = 0; i < nr; i++) page_kasan_tag_reset(page + i); - kasan_poison(page_address(page), page_size(page), - KASAN_SLAB_REDZONE, false); + kasan_poison_pages(page_address(page), nr, KASAN_SLAB_REDZONE, false); } void __kasan_unpoison_new_object(struct kmem_cache *cache, void *object) @@ -525,7 +525,7 @@ bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, if (check_page_allocation(ptr, ip)) return false; - kasan_poison(ptr, PAGE_SIZE << order, KASAN_PAGE_FREE, false); + kasan_poison_pages(ptr, 1U << order, KASAN_PAGE_FREE, false); return true; } @@ -552,7 +552,8 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) if (unlikely(PageLargeKmalloc(page))) { if (check_page_allocation(ptr, ip)) return false; - kasan_poison(ptr, page_size(page), KASAN_PAGE_FREE, false); + kasan_poison_pages(ptr, compound_nr(page), KASAN_PAGE_FREE, + false); return true; } diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 43d608899544..ee31fedad722 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -458,6 +458,11 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag) #ifdef CONFIG_KASAN_HW_TAGS +#ifndef arch_set_mem_tag_pages +#define arch_set_mem_tag_pages(addr, npages, tag, init) \ + arch_set_mem_tag_range((addr), (size_t)(npages) << PAGE_SHIFT, (tag), (init)) +#endif + #define hw_enable_tag_checks_sync() arch_enable_tag_checks_sync() #define hw_enable_tag_checks_async() arch_enable_tag_checks_async() #define hw_enable_tag_checks_asymm() arch_enable_tag_checks_asymm() @@ -469,6 +474,8 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag) #define hw_get_mem_tag(addr) arch_get_mem_tag(addr) #define hw_set_mem_tag_range(addr, size, tag, init) \ arch_set_mem_tag_range((addr), (size), (tag), (init)) +#define hw_set_mem_tag_pages(addr, npages, tag, init) \ + arch_set_mem_tag_pages((addr), (npages), (tag), (init)) void kasan_enable_hw_tags(void); @@ -529,6 +536,30 @@ static inline void kasan_unpoison(const void *addr, size_t size, bool init) hw_set_mem_tag_range(kasan_reset_tag(addr), size, tag, init); } +static inline void kasan_poison_pages(const void *addr, unsigned long npages, + u8 value, bool init) +{ + if (!npages) + return; + if (WARN_ON((unsigned long)addr & ~PAGE_MASK)) + return; + + hw_set_mem_tag_pages(kasan_reset_tag(addr), npages, value, init); +} + +static inline void kasan_unpoison_pages(const void *addr, unsigned long npages, + bool init) +{ + u8 tag = get_tag(addr); + + if (!npages) + return; + if (WARN_ON((unsigned long)addr & ~PAGE_MASK)) + return; + + hw_set_mem_tag_pages(kasan_reset_tag(addr), npages, tag, init); +} + static inline bool kasan_byte_accessible(const void *addr) { u8 ptr_tag = get_tag(addr); @@ -561,6 +592,31 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init); */ void kasan_unpoison(const void *addr, size_t size, bool init); +/** + * kasan_poison_pages - mark the page range as inaccessible + * @addr: page range start address, must be aligned to PAGE_SIZE + * @npages: page count + * @value: value that's written to metadata for the range + * @init: whether to initialize the memory range (only for hardware tag-based) + */ +static inline void kasan_poison_pages(const void *addr, unsigned long npages, + u8 value, bool init) +{ + kasan_poison(addr, (size_t)npages << PAGE_SHIFT, value, init); +} + +/** + * kasan_unpoison_pages - mark the page range as accessible + * @addr: page range start address, must be aligned to PAGE_SIZE + * @npages: page count + * @init: whether to initialize the memory range (only for hardware tag-based) + */ +static inline void kasan_unpoison_pages(const void *addr, unsigned long npages, + bool init) +{ + kasan_unpoison(addr, (size_t)npages << PAGE_SHIFT, init); +} + bool kasan_byte_accessible(const void *addr); #endif /* CONFIG_KASAN_HW_TAGS */ -- 2.56.1.839.g22a8290db9-goog Add a dedicated function for tagging up to the next page boundary. Dedupe flag accesses with a more concise local 'init' variable. This prepares for a change to __kasan_unpoison_vmalloc, to improve both reviewability of that patch and readability of the final code. Signed-off-by: Pierre-Clément Tosi --- mm/kasan/hw_tags.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c index a848eb2f9910..8efe6b1f88a7 100644 --- a/mm/kasan/hw_tags.c +++ b/mm/kasan/hw_tags.c @@ -9,6 +9,7 @@ #define pr_fmt(fmt) "kasan: " fmt #include +#include #include #include #include @@ -316,14 +317,24 @@ static void init_vmalloc_pages(const void *start, unsigned long size) } } +static void poison_redzone(const void *start, bool init) +{ + size_t size; + + start = PTR_ALIGN(start, KASAN_GRANULE_SIZE); + size = (size_t)(PTR_ALIGN(start, PAGE_SIZE) - start); + + kasan_poison(start, size, KASAN_TAG_INVALID, init); +} + void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, kasan_vmalloc_flags_t flags) { + bool init = flags & KASAN_VMALLOC_INIT; u8 tag; - unsigned long redzone_start, redzone_size; if (!kasan_vmalloc_enabled()) { - if (flags & KASAN_VMALLOC_INIT) + if (init) init_vmalloc_pages(start, size); return (void *)start; } @@ -348,7 +359,7 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, * For non-VM_ALLOC allocations, page_alloc memory is tagged as usual. */ if (!(flags & KASAN_VMALLOC_VM_ALLOC)) { - WARN_ON(flags & KASAN_VMALLOC_INIT); + WARN_ON(init); return (void *)start; } @@ -357,7 +368,7 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, * The kernel doesn't tolerate having the PC register tagged. */ if (!(flags & KASAN_VMALLOC_PROT_NORMAL)) { - WARN_ON(flags & KASAN_VMALLOC_INIT); + WARN_ON(init); return (void *)start; } @@ -365,18 +376,14 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, start = set_tag(start, tag); /* Unpoison and initialize memory up to size. */ - kasan_unpoison(start, size, flags & KASAN_VMALLOC_INIT); + kasan_unpoison(start, size, init); /* * Explicitly poison and initialize the in-page vmalloc() redzone. * Unlike software KASAN modes, hardware tag-based KASAN doesn't * unpoison memory when populating shadow for vmalloc() space. */ - redzone_start = round_up((unsigned long)start + size, - KASAN_GRANULE_SIZE); - redzone_size = round_up(redzone_start, PAGE_SIZE) - redzone_start; - kasan_poison((void *)redzone_start, redzone_size, KASAN_TAG_INVALID, - flags & KASAN_VMALLOC_INIT); + poison_redzone(start + size, init); /* * Set per-page tag flags to allow accessing physical memory for the -- 2.56.1.839.g22a8290db9-goog Use kasan_unpoison_pages() on the leading whole page(s) of ranges that are page-aligned, which is very common with vmalloc() allocations. Signed-off-by: Pierre-Clément Tosi --- mm/kasan/hw_tags.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c index 8efe6b1f88a7..c25268b76569 100644 --- a/mm/kasan/hw_tags.c +++ b/mm/kasan/hw_tags.c @@ -331,6 +331,7 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, kasan_vmalloc_flags_t flags) { bool init = flags & KASAN_VMALLOC_INIT; + const void *pos; u8 tag; if (!kasan_vmalloc_enabled()) { @@ -374,16 +375,26 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, tag = (flags & KASAN_VMALLOC_KEEP_TAG) ? get_tag(start) : kasan_random_tag(); start = set_tag(start, tag); + pos = start; /* Unpoison and initialize memory up to size. */ - kasan_unpoison(start, size, init); + if (PAGE_ALIGNED(pos)) { + unsigned long nr_pages; + + size = round_up(size, KASAN_GRANULE_SIZE); + nr_pages = size >> PAGE_SHIFT; + kasan_unpoison_pages(pos, nr_pages, init); + pos += nr_pages << PAGE_SHIFT; + size &= ~PAGE_MASK; + } + kasan_unpoison(pos, size, init); /* * Explicitly poison and initialize the in-page vmalloc() redzone. * Unlike software KASAN modes, hardware tag-based KASAN doesn't * unpoison memory when populating shadow for vmalloc() space. */ - poison_redzone(start + size, init); + poison_redzone(pos + size, init); /* * Set per-page tag flags to allow accessing physical memory for the -- 2.56.1.839.g22a8290db9-goog