All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro
@ 2022-11-21 13:50 Feng Tang
  2022-11-21 13:50 ` [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check Feng Tang
  2022-11-21 20:19 ` [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Andrew Morton
  0 siblings, 2 replies; 11+ messages in thread
From: Feng Tang @ 2022-11-21 13:50 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo,
	Andrey Konovalov, Dmitry Vyukov, Andrey Ryabinin,
	Alexander Potapenko, Vincenzo Frascino
  Cc: linux-mm, kasan-dev, linux-kernel, Feng Tang

commit 6edf2576a6cc ("mm/slub: enable debugging memory wasting of
kmalloc") introduces 'SLAB_KMALLOC' bit specifying whether a
kmem_cache is a kmalloc cache for slab/slub (slob doesn't have
dedicated kmalloc caches).

Add a helper macro for other components like kasan to simplify code.

Signed-off-by: Feng Tang <feng.tang@intel.com>
---
 include/linux/slab.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 1c670c16c737..ee6499088ad3 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -758,6 +758,12 @@ extern void kvfree_sensitive(const void *addr, size_t len);
 
 unsigned int kmem_cache_size(struct kmem_cache *s);
 
+#ifndef CONFIG_SLOB
+#define is_kmalloc_cache(s) ((s)->flags & SLAB_KMALLOC)
+#else
+#define is_kmalloc_cache(s) (false)
+#endif
+
 /**
  * kmalloc_size_roundup - Report allocation bucket size for the given size
  *
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check
  2022-11-21 13:50 [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Feng Tang
@ 2022-11-21 13:50 ` Feng Tang
  2022-11-21 14:13   ` Feng Tang
  2022-11-21 15:15   ` Andrey Konovalov
  2022-11-21 20:19 ` [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Andrew Morton
  1 sibling, 2 replies; 11+ messages in thread
From: Feng Tang @ 2022-11-21 13:50 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo,
	Andrey Konovalov, Dmitry Vyukov, Andrey Ryabinin,
	Alexander Potapenko, Vincenzo Frascino
  Cc: linux-mm, kasan-dev, linux-kernel, Feng Tang

Use new is_kmalloc_cache() to simplify the code of checking whether
a kmem_cache is a kmalloc cache.

Signed-off-by: Feng Tang <feng.tang@intel.com>
---
 include/linux/kasan.h | 9 ---------
 mm/kasan/common.c     | 9 ++-------
 mm/slab_common.c      | 1 -
 3 files changed, 2 insertions(+), 17 deletions(-)

diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index dff604912687..fc46f5d6f404 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -102,7 +102,6 @@ struct kasan_cache {
 	int alloc_meta_offset;
 	int free_meta_offset;
 #endif
-	bool is_kmalloc;
 };
 
 void __kasan_unpoison_range(const void *addr, size_t size);
@@ -129,13 +128,6 @@ static __always_inline bool kasan_unpoison_pages(struct page *page,
 	return false;
 }
 
-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);
-}
-
 void __kasan_poison_slab(struct slab *slab);
 static __always_inline void kasan_poison_slab(struct slab *slab)
 {
@@ -252,7 +244,6 @@ 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) { return false; }
-static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {}
 static inline void kasan_poison_slab(struct slab *slab) {}
 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
 					void *object) {}
diff --git a/mm/kasan/common.c b/mm/kasan/common.c
index 1f30080a7a4c..f7e0e5067e7a 100644
--- a/mm/kasan/common.c
+++ b/mm/kasan/common.c
@@ -122,11 +122,6 @@ void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
 			     KASAN_PAGE_FREE, init);
 }
 
-void __kasan_cache_create_kmalloc(struct kmem_cache *cache)
-{
-	cache->kasan_info.is_kmalloc = true;
-}
-
 void __kasan_poison_slab(struct slab *slab)
 {
 	struct page *page = slab_page(slab);
@@ -326,7 +321,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
 	kasan_unpoison(tagged_object, cache->object_size, init);
 
 	/* Save alloc info (if possible) for non-kmalloc() allocations. */
-	if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc)
+	if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
 		kasan_save_alloc_info(cache, tagged_object, flags);
 
 	return tagged_object;
@@ -372,7 +367,7 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache,
 	 * Save alloc info (if possible) for kmalloc() allocations.
 	 * This also rewrites the alloc info when called from kasan_krealloc().
 	 */
-	if (kasan_stack_collection_enabled() && cache->kasan_info.is_kmalloc)
+	if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
 		kasan_save_alloc_info(cache, (void *)object, flags);
 
 	/* Keep the tag that was set by kasan_slab_alloc(). */
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 8276022f0da4..a5480d67f391 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -663,7 +663,6 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name,
 
 	create_boot_cache(s, name, size, flags | SLAB_KMALLOC, useroffset,
 								usersize);
-	kasan_cache_create_kmalloc(s);
 	list_add(&s->list, &slab_caches);
 	s->refcount = 1;
 	return s;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check
  2022-11-21 13:50 ` [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check Feng Tang
@ 2022-11-21 14:13   ` Feng Tang
  2022-11-21 15:15   ` Andrey Konovalov
  1 sibling, 0 replies; 11+ messages in thread
From: Feng Tang @ 2022-11-21 14:13 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo,
	Andrey Konovalov, Dmitry Vyukov, Andrey Ryabinin,
	Alexander Potapenko, Vincenzo Frascino
  Cc: linux-mm, kasan-dev, linux-kernel

On Mon, Nov 21, 2022 at 09:50:24PM +0800, Tang, Feng wrote:
> Use new is_kmalloc_cache() to simplify the code of checking whether
> a kmem_cache is a kmalloc cache.
> 
> Signed-off-by: Feng Tang <feng.tang@intel.com>
> ---
>  include/linux/kasan.h | 9 ---------
>  mm/kasan/common.c     | 9 ++-------
>  mm/slab_common.c      | 1 -
>  3 files changed, 2 insertions(+), 17 deletions(-)
> 
> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index dff604912687..fc46f5d6f404 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -102,7 +102,6 @@ struct kasan_cache {
>  	int alloc_meta_offset;
>  	int free_meta_offset;
>  #endif
> -	bool is_kmalloc;
>  };
>  
>  void __kasan_unpoison_range(const void *addr, size_t size);
> @@ -129,13 +128,6 @@ static __always_inline bool kasan_unpoison_pages(struct page *page,
>  	return false;
>  }
>  
> -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);
> -}
> -
>  void __kasan_poison_slab(struct slab *slab);
>  static __always_inline void kasan_poison_slab(struct slab *slab)
>  {
> @@ -252,7 +244,6 @@ 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) { return false; }
> -static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {}
>  static inline void kasan_poison_slab(struct slab *slab) {}
>  static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
>  					void *object) {}
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 1f30080a7a4c..f7e0e5067e7a 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -122,11 +122,6 @@ void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
>  			     KASAN_PAGE_FREE, init);
>  }
>  
> -void __kasan_cache_create_kmalloc(struct kmem_cache *cache)
> -{
> -	cache->kasan_info.is_kmalloc = true;
> -}
> -
>  void __kasan_poison_slab(struct slab *slab)
>  {
>  	struct page *page = slab_page(slab);
> @@ -326,7 +321,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
>  	kasan_unpoison(tagged_object, cache->object_size, init);
>  
>  	/* Save alloc info (if possible) for non-kmalloc() allocations. */
> -	if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc)
> +	if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))

Sorry, it should be: 

 -	if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc)
 +	if (kasan_stack_collection_enabled() && !is_kmalloc_cache(cache))

Thanks,
Feng

>  		kasan_save_alloc_info(cache, tagged_object, flags);
>  
>  	return tagged_object;
> @@ -372,7 +367,7 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache,
>  	 * Save alloc info (if possible) for kmalloc() allocations.
>  	 * This also rewrites the alloc info when called from kasan_krealloc().
>  	 */
> -	if (kasan_stack_collection_enabled() && cache->kasan_info.is_kmalloc)
> +	if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
>  		kasan_save_alloc_info(cache, (void *)object, flags);
>  
>  	/* Keep the tag that was set by kasan_slab_alloc(). */
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index 8276022f0da4..a5480d67f391 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -663,7 +663,6 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name,
>  
>  	create_boot_cache(s, name, size, flags | SLAB_KMALLOC, useroffset,
>  								usersize);
> -	kasan_cache_create_kmalloc(s);
>  	list_add(&s->list, &slab_caches);
>  	s->refcount = 1;
>  	return s;
> -- 
> 2.34.1
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check
  2022-11-21 13:50 ` [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check Feng Tang
  2022-11-21 14:13   ` Feng Tang
@ 2022-11-21 15:15   ` Andrey Konovalov
  2022-11-22  6:53     ` Feng Tang
  1 sibling, 1 reply; 11+ messages in thread
From: Andrey Konovalov @ 2022-11-21 15:15 UTC (permalink / raw)
  To: Feng Tang
  Cc: Andrew Morton, Vlastimil Babka, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo,
	Dmitry Vyukov, Andrey Ryabinin, Alexander Potapenko,
	Vincenzo Frascino, linux-mm, kasan-dev, linux-kernel

On Mon, Nov 21, 2022 at 2:53 PM Feng Tang <feng.tang@intel.com> wrote:
>
> Use new is_kmalloc_cache() to simplify the code of checking whether
> a kmem_cache is a kmalloc cache.
>
> Signed-off-by: Feng Tang <feng.tang@intel.com>

Hi Feng,

Nice simplification!

> ---
>  include/linux/kasan.h | 9 ---------
>  mm/kasan/common.c     | 9 ++-------
>  mm/slab_common.c      | 1 -
>  3 files changed, 2 insertions(+), 17 deletions(-)
>
> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index dff604912687..fc46f5d6f404 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -102,7 +102,6 @@ struct kasan_cache {
>         int alloc_meta_offset;
>         int free_meta_offset;
>  #endif
> -       bool is_kmalloc;
>  };

We can go even further here, and only define the kasan_cache struct
and add the kasan_info field to kmem_cache when CONFIG_KASAN_GENERIC
is enabled.

>
>  void __kasan_unpoison_range(const void *addr, size_t size);
> @@ -129,13 +128,6 @@ static __always_inline bool kasan_unpoison_pages(struct page *page,
>         return false;
>  }
>
> -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);
> -}
> -
>  void __kasan_poison_slab(struct slab *slab);
>  static __always_inline void kasan_poison_slab(struct slab *slab)
>  {
> @@ -252,7 +244,6 @@ 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) { return false; }
> -static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {}
>  static inline void kasan_poison_slab(struct slab *slab) {}
>  static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
>                                         void *object) {}
> diff --git a/mm/kasan/common.c b/mm/kasan/common.c
> index 1f30080a7a4c..f7e0e5067e7a 100644
> --- a/mm/kasan/common.c
> +++ b/mm/kasan/common.c
> @@ -122,11 +122,6 @@ void __kasan_poison_pages(struct page *page, unsigned int order, bool init)
>                              KASAN_PAGE_FREE, init);
>  }
>
> -void __kasan_cache_create_kmalloc(struct kmem_cache *cache)
> -{
> -       cache->kasan_info.is_kmalloc = true;
> -}
> -
>  void __kasan_poison_slab(struct slab *slab)
>  {
>         struct page *page = slab_page(slab);
> @@ -326,7 +321,7 @@ void * __must_check __kasan_slab_alloc(struct kmem_cache *cache,
>         kasan_unpoison(tagged_object, cache->object_size, init);
>
>         /* Save alloc info (if possible) for non-kmalloc() allocations. */
> -       if (kasan_stack_collection_enabled() && !cache->kasan_info.is_kmalloc)
> +       if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
>                 kasan_save_alloc_info(cache, tagged_object, flags);
>
>         return tagged_object;
> @@ -372,7 +367,7 @@ static inline void *____kasan_kmalloc(struct kmem_cache *cache,
>          * Save alloc info (if possible) for kmalloc() allocations.
>          * This also rewrites the alloc info when called from kasan_krealloc().
>          */
> -       if (kasan_stack_collection_enabled() && cache->kasan_info.is_kmalloc)
> +       if (kasan_stack_collection_enabled() && is_kmalloc_cache(cache))
>                 kasan_save_alloc_info(cache, (void *)object, flags);
>
>         /* Keep the tag that was set by kasan_slab_alloc(). */
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index 8276022f0da4..a5480d67f391 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -663,7 +663,6 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name,
>
>         create_boot_cache(s, name, size, flags | SLAB_KMALLOC, useroffset,
>                                                                 usersize);
> -       kasan_cache_create_kmalloc(s);
>         list_add(&s->list, &slab_caches);
>         s->refcount = 1;
>         return s;
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro
  2022-11-21 13:50 [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Feng Tang
  2022-11-21 13:50 ` [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check Feng Tang
@ 2022-11-21 20:19 ` Andrew Morton
  2022-11-22  5:30   ` Feng Tang
  1 sibling, 1 reply; 11+ messages in thread
From: Andrew Morton @ 2022-11-21 20:19 UTC (permalink / raw)
  To: Feng Tang
  Cc: Vlastimil Babka, Christoph Lameter, Pekka Enberg, David Rientjes,
	Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo, Andrey Konovalov,
	Dmitry Vyukov, Andrey Ryabinin, Alexander Potapenko,
	Vincenzo Frascino, linux-mm, kasan-dev, linux-kernel

On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <feng.tang@intel.com> wrote:

> +#ifndef CONFIG_SLOB
> +#define is_kmalloc_cache(s) ((s)->flags & SLAB_KMALLOC)
> +#else
> +#define is_kmalloc_cache(s) (false)
> +#endif

Could be implemented as a static inline C function, yes?

If so, that's always best.  For (silly) example, consider the behaviour
of

	x = is_kmalloc_cache(s++);

with and without CONFIG_SLOB.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro
  2022-11-21 20:19 ` [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Andrew Morton
@ 2022-11-22  5:30   ` Feng Tang
  2022-11-22 23:17     ` Andrew Morton
  2022-11-23  9:21     ` Vlastimil Babka
  0 siblings, 2 replies; 11+ messages in thread
From: Feng Tang @ 2022-11-22  5:30 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Vlastimil Babka, Christoph Lameter, Pekka Enberg, David Rientjes,
	Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo, Andrey Konovalov,
	Dmitry Vyukov, Andrey Ryabinin, Alexander Potapenko,
	Vincenzo Frascino, linux-mm, kasan-dev, linux-kernel

On Mon, Nov 21, 2022 at 12:19:38PM -0800, Andrew Morton wrote:
> On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <feng.tang@intel.com> wrote:
> 
> > +#ifndef CONFIG_SLOB
> > +#define is_kmalloc_cache(s) ((s)->flags & SLAB_KMALLOC)
> > +#else
> > +#define is_kmalloc_cache(s) (false)
> > +#endif
> 
> Could be implemented as a static inline C function, yes?

Right, I also did try inline function first, and met compilation error: 

"
./include/linux/slab.h: In function ‘is_kmalloc_cache’:
./include/linux/slab.h:159:18: error: invalid use of undefined type ‘struct kmem_cache’
  159 |         return (s->flags & SLAB_KMALLOC);
      |                  ^~
"

The reason is 'struct kmem_cache' definition for slab/slub/slob sit
separately in slab_def.h, slub_def.h and mm/slab.h, and they are not
included in this 'include/linux/slab.h'. So I chose the macro way.

Btw, I've worked on some patches related with sl[auo]b recently, and
really felt the pain when dealing with 3 allocators, on both reading
code and writing patches. And I really like the idea of fading away
SLOB as the first step :)

> If so, that's always best.  For (silly) example, consider the behaviour
> of
> 
> 	x = is_kmalloc_cache(s++);
> 
> with and without CONFIG_SLOB.

Another solution I can think of is putting the implementation into
slab_common.c, like the below?

Thanks,
Feng

---
diff --git a/include/linux/slab.h b/include/linux/slab.h
index 067f0e80be9e..e4fcdbfb3477 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -149,6 +149,17 @@
 
 struct list_lru;
 struct mem_cgroup;
+
+#ifndef CONFIG_SLOB
+extern bool is_kmalloc_cache(struct kmem_cache *s);
+#else
+static inline bool is_kmalloc_cache(struct kmem_cache *s)
+{
+	return false;
+}
+#endif
+
 /*
  * struct kmem_cache related prototypes
  */
diff --git a/mm/slab_common.c b/mm/slab_common.c
index a5480d67f391..860e804b7c0a 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -77,6 +77,13 @@ __setup_param("slub_merge", slub_merge, setup_slab_merge, 0);
 __setup("slab_nomerge", setup_slab_nomerge);
 __setup("slab_merge", setup_slab_merge);
 
+#ifndef CONFIG_SLOB
+bool is_kmalloc_cache(struct kmem_cache *s)
+{
+	return (s->flags & SLAB_KMALLOC);
+}
+#endif
+
 /*
  * Determine the size of a slab object
  */

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check
  2022-11-21 15:15   ` Andrey Konovalov
@ 2022-11-22  6:53     ` Feng Tang
  2022-11-22  9:57       ` Andrey Konovalov
  0 siblings, 1 reply; 11+ messages in thread
From: Feng Tang @ 2022-11-22  6:53 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Andrew Morton, Vlastimil Babka, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo,
	Dmitry Vyukov, Andrey Ryabinin, Alexander Potapenko,
	Vincenzo Frascino, linux-mm, kasan-dev, linux-kernel

On Mon, Nov 21, 2022 at 04:15:32PM +0100, Andrey Konovalov wrote:
> On Mon, Nov 21, 2022 at 2:53 PM Feng Tang <feng.tang@intel.com> wrote:
> >
> > Use new is_kmalloc_cache() to simplify the code of checking whether
> > a kmem_cache is a kmalloc cache.
> >
> > Signed-off-by: Feng Tang <feng.tang@intel.com>
> 
> Hi Feng,
> 
> Nice simplification!
> 
> > ---
> >  include/linux/kasan.h | 9 ---------
> >  mm/kasan/common.c     | 9 ++-------
> >  mm/slab_common.c      | 1 -
> >  3 files changed, 2 insertions(+), 17 deletions(-)
> >
> > diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> > index dff604912687..fc46f5d6f404 100644
> > --- a/include/linux/kasan.h
> > +++ b/include/linux/kasan.h
> > @@ -102,7 +102,6 @@ struct kasan_cache {
> >         int alloc_meta_offset;
> >         int free_meta_offset;
> >  #endif
> > -       bool is_kmalloc;
> >  };
> 
> We can go even further here, and only define the kasan_cache struct
> and add the kasan_info field to kmem_cache when CONFIG_KASAN_GENERIC
> is enabled.

Good idea. thanks!

I mainly checked the kasan_cache related code, and make an add-on
patch below, please let me know if my understanding is wrong or I
missed anything.

Thanks,
Feng

---
diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index 0ac6505367ee..f2e41290094e 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -96,14 +96,6 @@ static inline bool kasan_has_integrated_init(void)
 }
 
 #ifdef CONFIG_KASAN
-
-struct kasan_cache {
-#ifdef CONFIG_KASAN_GENERIC
-	int alloc_meta_offset;
-	int free_meta_offset;
-#endif
-};
-
 void __kasan_unpoison_range(const void *addr, size_t size);
 static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
 {
@@ -293,6 +285,11 @@ static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
 
 #ifdef CONFIG_KASAN_GENERIC
 
+struct kasan_cache {
+	int alloc_meta_offset;
+	int free_meta_offset;
+};
+
 size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object);
 slab_flags_t kasan_never_merge(void);
 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h
index f0ffad6a3365..39f7f1f95de2 100644
--- a/include/linux/slab_def.h
+++ b/include/linux/slab_def.h
@@ -72,7 +72,7 @@ struct kmem_cache {
 	int obj_offset;
 #endif /* CONFIG_DEBUG_SLAB */
 
-#ifdef CONFIG_KASAN
+#ifdef CONFIG_KASAN_GENERIC
 	struct kasan_cache kasan_info;
 #endif
 
diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h
index f9c68a9dac04..4e7cdada4bbb 100644
--- a/include/linux/slub_def.h
+++ b/include/linux/slub_def.h
@@ -132,7 +132,7 @@ struct kmem_cache {
 	unsigned int *random_seq;
 #endif
 
-#ifdef CONFIG_KASAN
+#ifdef CONFIG_KASAN_GENERIC
 	struct kasan_cache kasan_info;
 #endif
 



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check
  2022-11-22  6:53     ` Feng Tang
@ 2022-11-22  9:57       ` Andrey Konovalov
  0 siblings, 0 replies; 11+ messages in thread
From: Andrey Konovalov @ 2022-11-22  9:57 UTC (permalink / raw)
  To: Feng Tang
  Cc: Andrew Morton, Vlastimil Babka, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo,
	Dmitry Vyukov, Andrey Ryabinin, Alexander Potapenko,
	Vincenzo Frascino, linux-mm, kasan-dev, linux-kernel

On Tue, Nov 22, 2022 at 7:56 AM Feng Tang <feng.tang@intel.com> wrote:
>
> On Mon, Nov 21, 2022 at 04:15:32PM +0100, Andrey Konovalov wrote:
> > On Mon, Nov 21, 2022 at 2:53 PM Feng Tang <feng.tang@intel.com> wrote:
> > >
> > > Use new is_kmalloc_cache() to simplify the code of checking whether
> > > a kmem_cache is a kmalloc cache.
> > >
> > > Signed-off-by: Feng Tang <feng.tang@intel.com>
> >
> > Hi Feng,
> >
> > Nice simplification!
> >
> > > ---
> > >  include/linux/kasan.h | 9 ---------
> > >  mm/kasan/common.c     | 9 ++-------
> > >  mm/slab_common.c      | 1 -
> > >  3 files changed, 2 insertions(+), 17 deletions(-)
> > >
> > > diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> > > index dff604912687..fc46f5d6f404 100644
> > > --- a/include/linux/kasan.h
> > > +++ b/include/linux/kasan.h
> > > @@ -102,7 +102,6 @@ struct kasan_cache {
> > >         int alloc_meta_offset;
> > >         int free_meta_offset;
> > >  #endif
> > > -       bool is_kmalloc;
> > >  };
> >
> > We can go even further here, and only define the kasan_cache struct
> > and add the kasan_info field to kmem_cache when CONFIG_KASAN_GENERIC
> > is enabled.
>
> Good idea. thanks!
>
> I mainly checked the kasan_cache related code, and make an add-on
> patch below, please let me know if my understanding is wrong or I
> missed anything.
>
> Thanks,
> Feng
>
> ---
> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index 0ac6505367ee..f2e41290094e 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -96,14 +96,6 @@ static inline bool kasan_has_integrated_init(void)
>  }
>
>  #ifdef CONFIG_KASAN
> -
> -struct kasan_cache {
> -#ifdef CONFIG_KASAN_GENERIC
> -       int alloc_meta_offset;
> -       int free_meta_offset;
> -#endif
> -};
> -
>  void __kasan_unpoison_range(const void *addr, size_t size);
>  static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
>  {
> @@ -293,6 +285,11 @@ static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
>
>  #ifdef CONFIG_KASAN_GENERIC
>
> +struct kasan_cache {
> +       int alloc_meta_offset;
> +       int free_meta_offset;
> +};
> +
>  size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object);
>  slab_flags_t kasan_never_merge(void);
>  void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
> diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h
> index f0ffad6a3365..39f7f1f95de2 100644
> --- a/include/linux/slab_def.h
> +++ b/include/linux/slab_def.h
> @@ -72,7 +72,7 @@ struct kmem_cache {
>         int obj_offset;
>  #endif /* CONFIG_DEBUG_SLAB */
>
> -#ifdef CONFIG_KASAN
> +#ifdef CONFIG_KASAN_GENERIC
>         struct kasan_cache kasan_info;
>  #endif
>
> diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h
> index f9c68a9dac04..4e7cdada4bbb 100644
> --- a/include/linux/slub_def.h
> +++ b/include/linux/slub_def.h
> @@ -132,7 +132,7 @@ struct kmem_cache {
>         unsigned int *random_seq;
>  #endif
>
> -#ifdef CONFIG_KASAN
> +#ifdef CONFIG_KASAN_GENERIC
>         struct kasan_cache kasan_info;
>  #endif

Yes, this looks good.

Please resend as a v2 and I'll give a Reviewed-by.

Thanks!

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro
  2022-11-22  5:30   ` Feng Tang
@ 2022-11-22 23:17     ` Andrew Morton
  2022-11-23  9:21     ` Vlastimil Babka
  1 sibling, 0 replies; 11+ messages in thread
From: Andrew Morton @ 2022-11-22 23:17 UTC (permalink / raw)
  To: Feng Tang
  Cc: Vlastimil Babka, Christoph Lameter, Pekka Enberg, David Rientjes,
	Joonsoo Kim, Roman Gushchin, Hyeonggon Yoo, Andrey Konovalov,
	Dmitry Vyukov, Andrey Ryabinin, Alexander Potapenko,
	Vincenzo Frascino, linux-mm, kasan-dev, linux-kernel

On Tue, 22 Nov 2022 13:30:19 +0800 Feng Tang <feng.tang@intel.com> wrote:

> > If so, that's always best.  For (silly) example, consider the behaviour
> > of
> > 
> > 	x = is_kmalloc_cache(s++);
> > 
> > with and without CONFIG_SLOB.
> 
> Another solution I can think of is putting the implementation into
> slab_common.c, like the below?

I'm not sure that's much of an improvement on the macro :(

How about we go with the macro and avoid the
expression-with-side-effects gotcha (and the potential CONFIG_SLOB=n
unused-variable gotcha)?  That would involve evaluating the arg within
the CONFIG_SLOB=y version of the macro.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro
  2022-11-22  5:30   ` Feng Tang
  2022-11-22 23:17     ` Andrew Morton
@ 2022-11-23  9:21     ` Vlastimil Babka
  2022-11-23 12:17       ` Feng Tang
  1 sibling, 1 reply; 11+ messages in thread
From: Vlastimil Babka @ 2022-11-23  9:21 UTC (permalink / raw)
  To: Feng Tang, Andrew Morton
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Roman Gushchin, Hyeonggon Yoo, Andrey Konovalov, Dmitry Vyukov,
	Andrey Ryabinin, Alexander Potapenko, Vincenzo Frascino,
	linux-mm, kasan-dev, linux-kernel

On 11/22/22 06:30, Feng Tang wrote:
> On Mon, Nov 21, 2022 at 12:19:38PM -0800, Andrew Morton wrote:
>> On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <feng.tang@intel.com> wrote:
>> 
>> > +#ifndef CONFIG_SLOB
>> > +#define is_kmalloc_cache(s) ((s)->flags & SLAB_KMALLOC)
>> > +#else
>> > +#define is_kmalloc_cache(s) (false)
>> > +#endif
>> 
>> Could be implemented as a static inline C function, yes?
> 
> Right, I also did try inline function first, and met compilation error: 
> 
> "
> ./include/linux/slab.h: In function ‘is_kmalloc_cache’:
> ./include/linux/slab.h:159:18: error: invalid use of undefined type ‘struct kmem_cache’
>   159 |         return (s->flags & SLAB_KMALLOC);
>       |                  ^~
> "
> 
> The reason is 'struct kmem_cache' definition for slab/slub/slob sit
> separately in slab_def.h, slub_def.h and mm/slab.h, and they are not
> included in this 'include/linux/slab.h'. So I chose the macro way.

You could try mm/slab.h instead, below the slub_def.h includes there.
is_kmalloc_cache(s) shouldn't have random consumers in the kernel anyway.
It's fine if kasan includes it, as it's intertwined with slab a lot anyway.

> Btw, I've worked on some patches related with sl[auo]b recently, and
> really felt the pain when dealing with 3 allocators, on both reading
> code and writing patches. And I really like the idea of fading away
> SLOB as the first step :)

Can't agree more :)

>> If so, that's always best.  For (silly) example, consider the behaviour
>> of
>> 
>> 	x = is_kmalloc_cache(s++);
>> 
>> with and without CONFIG_SLOB.
> 
> Another solution I can think of is putting the implementation into
> slab_common.c, like the below?

The overhead of function call between compilation units (sans LTO) is not
worth it.

> Thanks,
> Feng
> 
> ---
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index 067f0e80be9e..e4fcdbfb3477 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -149,6 +149,17 @@
>  
>  struct list_lru;
>  struct mem_cgroup;
> +
> +#ifndef CONFIG_SLOB
> +extern bool is_kmalloc_cache(struct kmem_cache *s);
> +#else
> +static inline bool is_kmalloc_cache(struct kmem_cache *s)
> +{
> +	return false;
> +}
> +#endif
> +
>  /*
>   * struct kmem_cache related prototypes
>   */
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index a5480d67f391..860e804b7c0a 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -77,6 +77,13 @@ __setup_param("slub_merge", slub_merge, setup_slab_merge, 0);
>  __setup("slab_nomerge", setup_slab_nomerge);
>  __setup("slab_merge", setup_slab_merge);
>  
> +#ifndef CONFIG_SLOB
> +bool is_kmalloc_cache(struct kmem_cache *s)
> +{
> +	return (s->flags & SLAB_KMALLOC);
> +}
> +#endif
> +
>  /*
>   * Determine the size of a slab object
>   */


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro
  2022-11-23  9:21     ` Vlastimil Babka
@ 2022-11-23 12:17       ` Feng Tang
  0 siblings, 0 replies; 11+ messages in thread
From: Feng Tang @ 2022-11-23 12:17 UTC (permalink / raw)
  To: Vlastimil Babka, Andrew Morton
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Roman Gushchin, Hyeonggon Yoo, Andrey Konovalov, Dmitry Vyukov,
	Andrey Ryabinin, Alexander Potapenko, Vincenzo Frascino,
	linux-mm, kasan-dev, linux-kernel

On Wed, Nov 23, 2022 at 10:21:03AM +0100, Vlastimil Babka wrote:
> On 11/22/22 06:30, Feng Tang wrote:
> > On Mon, Nov 21, 2022 at 12:19:38PM -0800, Andrew Morton wrote:
> >> On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <feng.tang@intel.com> wrote:
> >> 
> >> > +#ifndef CONFIG_SLOB
> >> > +#define is_kmalloc_cache(s) ((s)->flags & SLAB_KMALLOC)
> >> > +#else
> >> > +#define is_kmalloc_cache(s) (false)
> >> > +#endif
> >> 
> >> Could be implemented as a static inline C function, yes?
> > 
> > Right, I also did try inline function first, and met compilation error: 
> > 
> > "
> > ./include/linux/slab.h: In function ‘is_kmalloc_cache’:
> > ./include/linux/slab.h:159:18: error: invalid use of undefined type ‘struct kmem_cache’
> >   159 |         return (s->flags & SLAB_KMALLOC);
> >       |                  ^~
> > "
> > 
> > The reason is 'struct kmem_cache' definition for slab/slub/slob sit
> > separately in slab_def.h, slub_def.h and mm/slab.h, and they are not
> > included in this 'include/linux/slab.h'. So I chose the macro way.
> 
> You could try mm/slab.h instead, below the slub_def.h includes there.
> is_kmalloc_cache(s) shouldn't have random consumers in the kernel anyway.
> It's fine if kasan includes it, as it's intertwined with slab a lot anyway.
 
Good suggestion! thanks! This can address Andrew's concern and also
avoid extra cost.    

And yes, besides sanity code like kasan/kfence, rare code will care
whether other kmem_cache is a kmalloc cache or not. And kasan code
already includes "../slab.h".

> > Btw, I've worked on some patches related with sl[auo]b recently, and
> > really felt the pain when dealing with 3 allocators, on both reading
> > code and writing patches. And I really like the idea of fading away
> > SLOB as the first step :)
> 
> Can't agree more :)
> 
> >> If so, that's always best.  For (silly) example, consider the behaviour
> >> of
> >> 
> >> 	x = is_kmalloc_cache(s++);
> >> 
> >> with and without CONFIG_SLOB.
> > 
> > Another solution I can think of is putting the implementation into
> > slab_common.c, like the below?
> 
> The overhead of function call between compilation units (sans LTO) is not
> worth it.

Yes. Will send out the v2 patches. 

Thanks,
Feng

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-11-23 12:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-21 13:50 [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Feng Tang
2022-11-21 13:50 ` [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check Feng Tang
2022-11-21 14:13   ` Feng Tang
2022-11-21 15:15   ` Andrey Konovalov
2022-11-22  6:53     ` Feng Tang
2022-11-22  9:57       ` Andrey Konovalov
2022-11-21 20:19 ` [PATCH -next 1/2] mm/slab: add is_kmalloc_cache() helper macro Andrew Morton
2022-11-22  5:30   ` Feng Tang
2022-11-22 23:17     ` Andrew Morton
2022-11-23  9:21     ` Vlastimil Babka
2022-11-23 12:17       ` Feng Tang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.