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