Commit 7a68b2f5 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Vincenzo Frascino
Browse files

kasan, mm: allow cache merging with no metadata



The reason cache merging is disabled with KASAN is because KASAN puts its
metadata right after the allocated object. When the merged caches have
slightly different sizes, the metadata ends up in different places, which
KASAN doesn't support.

It might be possible to adjust the metadata allocation algorithm and make
it friendly to the cache merging code. Instead this change takes a simpler
approach and allows merging caches when no metadata is present. Which is
the case for hardware tag-based KASAN with kasan.mode=prod.

Signed-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Link: https://linux-review.googlesource.com/id/Ia114847dfb2244f297d2cb82d592bf6a07455dba
parent 7df4cbd4
......@@ -79,17 +79,35 @@ struct kasan_cache {
};
#ifdef CONFIG_KASAN_HW_TAGS
DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
static inline kasan_enabled(void)
{
return static_branch_likely(&kasan_flag_enabled);
}
#else
slab_flags_t __kasan_never_merge(slab_flags_t flags);
static inline slab_flags_t kasan_never_merge(slab_flags_t flags)
{
if (kasan_enabled())
return __kasan_never_merge(flags);
return flags;
}
#else /* CONFIG_KASAN_HW_TAGS */
static inline kasan_enabled(void)
{
return true;
}
#endif
static inline slab_flags_t kasan_never_merge(slab_flags_t flags)
{
return flags;
}
#endif /* CONFIG_KASAN_HW_TAGS */
void __kasan_alloc_pages(struct page *page, unsigned int order);
static inline void kasan_alloc_pages(struct page *page, unsigned int order)
......@@ -238,6 +256,10 @@ static inline kasan_enabled(void)
{
return false;
}
static inline slab_flags_t kasan_never_merge(slab_flags_t flags)
{
return flags;
}
static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
static inline void kasan_free_pages(struct page *page, unsigned int order) {}
static inline void kasan_cache_create(struct kmem_cache *cache,
......
......@@ -81,6 +81,17 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
}
#endif /* CONFIG_KASAN_STACK */
/*
* Only allow cache merging when stack collection is disabled and no metadata
* is present.
*/
slab_flags_t __kasan_never_merge(slab_flags_t flags)
{
if (kasan_stack_collection_enabled())
return flags;
return flags & ~SLAB_KASAN;
}
void __kasan_alloc_pages(struct page *page, unsigned int order)
{
u8 tag;
......
......@@ -18,6 +18,7 @@
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/debugfs.h>
#include <linux/kasan.h>
#include <asm/cacheflush.h>
#include <asm/tlbflush.h>
#include <asm/page.h>
......@@ -49,12 +50,16 @@ static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
slab_caches_to_rcu_destroy_workfn);
/*
* Set of flags that will prevent slab merging
* Set of flags that will prevent slab merging.
* Use slab_never_merge() instead.
*/
#define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
SLAB_FAILSLAB | SLAB_KASAN)
/* KASAN allows merging in some configurations and will remove SLAB_KASAN. */
#define slab_never_merge() (kasan_never_merge(SLAB_NEVER_MERGE))
#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
......@@ -164,7 +169,7 @@ static unsigned int calculate_alignment(slab_flags_t flags,
*/
int slab_unmergeable(struct kmem_cache *s)
{
if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
if (slab_nomerge || (s->flags & slab_never_merge()))
return 1;
if (s->ctor)
......@@ -198,7 +203,7 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
size = ALIGN(size, align);
flags = kmem_cache_flags(size, flags, name, NULL);
if (flags & SLAB_NEVER_MERGE)
if (flags & slab_never_merge())
return NULL;
list_for_each_entry_reverse(s, &slab_caches, list) {
......
Supports Markdown
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