Commit 92850134 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Linus Torvalds

kasan, mm: don't save alloc stacks twice

Patch series "kasan: optimizations and fixes for HW_TAGS", v4.

This patchset makes the HW_TAGS mode more efficient, mostly by reworking
poisoning approaches and simplifying/inlining some internal helpers.

With this change, the overhead of HW_TAGS annotations excluding setting
and checking memory tags is ~3%.  The performance impact caused by tags
will be unknown until we have hardware that supports MTE.

As a side-effect, this patchset speeds up generic KASAN by ~15%.

This patch (of 13):

Currently KASAN saves allocation stacks in both kasan_slab_alloc() and
kasan_kmalloc() annotations.  This patch changes KASAN to save allocation
stacks for slab objects from kmalloc caches in kasan_kmalloc() only, and
stacks for other slab objects in kasan_slab_alloc() only.

This change requires ____kasan_kmalloc() knowing whether the object
belongs to a kmalloc cache.  This is implemented by adding a flag field to
the kasan_info structure.  That flag is only set for kmalloc caches via a
new kasan_cache_create_kmalloc() annotation.

Link: https://lkml.kernel.org/r/cover.1612546384.git.andreyknvl@google.com
Link: https://lkml.kernel.org/r/7c673ebca8d00f40a7ad6f04ab9a2bddeeae2097.1612546384.git.andreyknvl@google.comSigned-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Reviewed-by: default avatarMarco Elver <elver@google.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Peter Collingbourne <pcc@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Branislav Rankov <Branislav.Rankov@arm.com>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d3a61f74
...@@ -83,6 +83,7 @@ static inline void kasan_disable_current(void) {} ...@@ -83,6 +83,7 @@ static inline void kasan_disable_current(void) {}
struct kasan_cache { struct kasan_cache {
int alloc_meta_offset; int alloc_meta_offset;
int free_meta_offset; int free_meta_offset;
bool is_kmalloc;
}; };
#ifdef CONFIG_KASAN_HW_TAGS #ifdef CONFIG_KASAN_HW_TAGS
...@@ -143,6 +144,13 @@ static __always_inline void kasan_cache_create(struct kmem_cache *cache, ...@@ -143,6 +144,13 @@ static __always_inline void kasan_cache_create(struct kmem_cache *cache,
__kasan_cache_create(cache, size, flags); __kasan_cache_create(cache, size, flags);
} }
void __kasan_cache_create_kmalloc(struct kmem_cache *cache);
static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache)
{
if (kasan_enabled())
__kasan_cache_create_kmalloc(cache);
}
size_t __kasan_metadata_size(struct kmem_cache *cache); size_t __kasan_metadata_size(struct kmem_cache *cache);
static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache) static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache)
{ {
...@@ -278,6 +286,7 @@ static inline void kasan_free_pages(struct page *page, unsigned int order) {} ...@@ -278,6 +286,7 @@ static inline void kasan_free_pages(struct page *page, unsigned int order) {}
static inline void kasan_cache_create(struct kmem_cache *cache, static inline void kasan_cache_create(struct kmem_cache *cache,
unsigned int *size, unsigned int *size,
slab_flags_t *flags) {} slab_flags_t *flags) {}
static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {}
static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
static inline void kasan_poison_slab(struct page *page) {} static inline void kasan_poison_slab(struct page *page) {}
static inline void kasan_unpoison_object_data(struct kmem_cache *cache, static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
......
...@@ -210,6 +210,11 @@ void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, ...@@ -210,6 +210,11 @@ void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
*size = optimal_size; *size = optimal_size;
} }
void __kasan_cache_create_kmalloc(struct kmem_cache *cache)
{
cache->kasan_info.is_kmalloc = true;
}
size_t __kasan_metadata_size(struct kmem_cache *cache) size_t __kasan_metadata_size(struct kmem_cache *cache)
{ {
if (!kasan_stack_collection_enabled()) if (!kasan_stack_collection_enabled())
...@@ -394,17 +399,22 @@ void __kasan_slab_free_mempool(void *ptr, unsigned long ip) ...@@ -394,17 +399,22 @@ void __kasan_slab_free_mempool(void *ptr, unsigned long ip)
} }
} }
static void set_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) static void set_alloc_info(struct kmem_cache *cache, void *object,
gfp_t flags, bool is_kmalloc)
{ {
struct kasan_alloc_meta *alloc_meta; struct kasan_alloc_meta *alloc_meta;
/* Don't save alloc info for kmalloc caches in kasan_slab_alloc(). */
if (cache->kasan_info.is_kmalloc && !is_kmalloc)
return;
alloc_meta = kasan_get_alloc_meta(cache, object); alloc_meta = kasan_get_alloc_meta(cache, object);
if (alloc_meta) if (alloc_meta)
kasan_set_track(&alloc_meta->alloc_track, flags); kasan_set_track(&alloc_meta->alloc_track, flags);
} }
static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object, static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object,
size_t size, gfp_t flags, bool keep_tag) size_t size, gfp_t flags, bool is_kmalloc)
{ {
unsigned long redzone_start; unsigned long redzone_start;
unsigned long redzone_end; unsigned long redzone_end;
...@@ -423,7 +433,7 @@ static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object, ...@@ -423,7 +433,7 @@ static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object,
KASAN_GRANULE_SIZE); KASAN_GRANULE_SIZE);
redzone_end = round_up((unsigned long)object + cache->object_size, redzone_end = round_up((unsigned long)object + cache->object_size,
KASAN_GRANULE_SIZE); KASAN_GRANULE_SIZE);
tag = assign_tag(cache, object, false, keep_tag); tag = assign_tag(cache, object, false, is_kmalloc);
/* Tag is ignored in set_tag without CONFIG_KASAN_SW/HW_TAGS */ /* Tag is ignored in set_tag without CONFIG_KASAN_SW/HW_TAGS */
kasan_unpoison(set_tag(object, tag), size); kasan_unpoison(set_tag(object, tag), size);
...@@ -431,7 +441,7 @@ static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object, ...@@ -431,7 +441,7 @@ static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object,
KASAN_KMALLOC_REDZONE); KASAN_KMALLOC_REDZONE);
if (kasan_stack_collection_enabled()) if (kasan_stack_collection_enabled())
set_alloc_info(cache, (void *)object, flags); set_alloc_info(cache, (void *)object, flags, is_kmalloc);
return set_tag(object, tag); return set_tag(object, tag);
} }
......
...@@ -643,6 +643,7 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name, ...@@ -643,6 +643,7 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name,
panic("Out of memory when creating slab %s\n", name); panic("Out of memory when creating slab %s\n", name);
create_boot_cache(s, name, size, flags, useroffset, usersize); create_boot_cache(s, name, size, flags, useroffset, usersize);
kasan_cache_create_kmalloc(s);
list_add(&s->list, &slab_caches); list_add(&s->list, &slab_caches);
s->refcount = 1; s->refcount = 1;
return s; return s;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment