linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Marco Elver <elver@google.com>
To: Andrey Konovalov <andreyknvl@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>,
	Alexander Potapenko <glider@google.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will.deacon@arm.com>,
	Vincenzo Frascino <vincenzo.frascino@arm.com>,
	Evgenii Stepanov <eugenis@google.com>,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Branislav Rankov <Branislav.Rankov@arm.com>,
	Kevin Brodsky <kevin.brodsky@arm.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 19/20] kasan, mm: allow cache merging with no metadata
Date: Wed, 11 Nov 2020 16:13:36 +0100	[thread overview]
Message-ID: <20201111151336.GA517454@elver.google.com> (raw)
In-Reply-To: <936c0c198145b663e031527c49a6895bd21ac3a0.1605046662.git.andreyknvl@google.com>

On Tue, Nov 10, 2020 at 11:20PM +0100, Andrey Konovalov wrote:
> 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: Andrey Konovalov <andreyknvl@google.com>
> Link: https://linux-review.googlesource.com/id/Ia114847dfb2244f297d2cb82d592bf6a07455dba
> ---
>  include/linux/kasan.h | 26 ++++++++++++++++++++++++--
>  mm/kasan/common.c     | 11 +++++++++++
>  mm/slab_common.c      | 11 ++++++++---
>  3 files changed, 43 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index 534ab3e2935a..c754eca356f7 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -81,17 +81,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)
> @@ -240,6 +258,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,
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 940b42231069..25b18c145b06 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -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;
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index f1b0c4a22f08..3042ee8ea9ce 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -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)

Rather than changing this to require using slab_never_merge() which
removes SLAB_KASAN, could we not just have a function
kasan_never_merge() that returns KASAN-specific flags that should never
result in merging -- because as-is now, making kasan_never_merge()
remove the SLAB_KASAN flag seems the wrong way around.

Could we not just do this:

  #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  		SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
  		SLAB_FAILSLAB | kasan_never_merge())

??

Of course that might be problematic if this always needs to be a
compile-time constant, but currently that's not a requirement.

> +/* KASAN allows merging in some configurations and will remove SLAB_KASAN. */
> +#define slab_never_merge() (kasan_never_merge(SLAB_NEVER_MERGE))

Braces unnecessary.

>  #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) {
> -- 
> 2.29.2.222.g5d2a92d10f8-goog
> 


  reply	other threads:[~2020-11-11 15:13 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-10 22:20 [PATCH v2 00/20] kasan: boot parameters for hardware tag-based mode Andrey Konovalov
2020-11-10 22:20 ` [PATCH v2 01/20] kasan: simplify quarantine_put call site Andrey Konovalov
2020-11-11 16:08   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 02/20] kasan: rename get_alloc/free_info Andrey Konovalov
2020-11-11 16:09   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 03/20] kasan: introduce set_alloc_info Andrey Konovalov
2020-11-11 16:10   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 04/20] kasan, arm64: unpoison stack only with CONFIG_KASAN_STACK Andrey Konovalov
2020-11-11 16:13   ` Marco Elver
2020-11-12  9:51   ` Catalin Marinas
2020-11-12 19:38     ` Andrey Konovalov
2020-11-10 22:20 ` [PATCH v2 05/20] kasan: allow VMAP_STACK for HW_TAGS mode Andrey Konovalov
2020-11-11 16:20   ` Marco Elver
2020-11-12  0:24     ` Andrey Konovalov
2020-11-12 10:31   ` Catalin Marinas
2020-11-10 22:20 ` [PATCH v2 06/20] kasan: remove __kasan_unpoison_stack Andrey Konovalov
2020-11-11 16:42   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 07/20] kasan: inline kasan_reset_tag for tag-based modes Andrey Konovalov
2020-11-11 16:54   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 08/20] kasan: inline random_tag for HW_TAGS Andrey Konovalov
2020-11-11 17:02   ` Marco Elver
2020-11-12  0:21     ` Andrey Konovalov
2020-11-10 22:20 ` [PATCH v2 09/20] kasan: inline kasan_poison_memory and check_invalid_free Andrey Konovalov
2020-11-11 17:50   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 10/20] kasan: inline and rename kasan_unpoison_memory Andrey Konovalov
2020-11-11 17:49   ` Marco Elver
2020-11-12 19:45     ` Andrey Konovalov
2020-11-12 19:52       ` Marco Elver
2020-11-12 20:54         ` Andrey Konovalov
2020-11-12 22:20           ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 11/20] kasan: add and integrate kasan boot parameters Andrey Konovalov
2020-11-11 18:29   ` Marco Elver
2020-11-12 19:51     ` Andrey Konovalov
2020-11-12 11:35   ` Catalin Marinas
2020-11-12 11:53     ` Marco Elver
2020-11-12 12:54       ` Catalin Marinas
2020-11-12 19:52         ` Andrey Konovalov
2020-11-13 17:52   ` Marco Elver
2020-11-13 17:55     ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 12/20] kasan, mm: check kasan_enabled in annotations Andrey Konovalov
2020-11-11 14:48   ` Marco Elver
2020-11-12  0:36     ` Andrey Konovalov
2020-11-10 22:20 ` [PATCH v2 13/20] kasan: simplify kasan_poison_kfree Andrey Konovalov
2020-11-11 18:42   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 14/20] kasan, mm: rename kasan_poison_kfree Andrey Konovalov
2020-11-11 18:53   ` Marco Elver
2020-11-12  1:05     ` Andrey Konovalov
2020-11-10 22:20 ` [PATCH v2 15/20] kasan: don't round_up too much Andrey Konovalov
2020-11-11 19:08   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 16/20] kasan: simplify assign_tag and set_tag calls Andrey Konovalov
2020-11-11 19:17   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 17/20] kasan: clarify comment in __kasan_kfree_large Andrey Konovalov
2020-11-11 19:18   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 18/20] kasan: clean up metadata allocation and usage Andrey Konovalov
2020-11-11 23:06   ` Marco Elver
2020-11-12 20:11     ` Andrey Konovalov
2020-11-10 22:20 ` [PATCH v2 19/20] kasan, mm: allow cache merging with no metadata Andrey Konovalov
2020-11-11 15:13   ` Marco Elver [this message]
2020-11-12 23:00     ` Andrey Konovalov
2020-11-11 23:27   ` Marco Elver
2020-11-10 22:20 ` [PATCH v2 20/20] kasan: update documentation Andrey Konovalov
2020-11-11 16:03   ` Marco Elver
2020-11-12  0:51     ` Andrey Konovalov
2020-11-10 22:24 ` [PATCH v2 00/20] kasan: boot parameters for hardware tag-based mode Andrey Konovalov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201111151336.GA517454@elver.google.com \
    --to=elver@google.com \
    --cc=Branislav.Rankov@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=catalin.marinas@arm.com \
    --cc=dvyukov@google.com \
    --cc=eugenis@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kevin.brodsky@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=vincenzo.frascino@arm.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).