Skip to contentSkip to conversation

mm/slab: add is_kmalloc_cache() helper macro

PATCH In review Feng Tang 4 participants 11 messages Nov 21, 2022 – Nov 23, 2022

Subsystems (3)
lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH 1/2 mm/slab: add is_kmalloc_cache() helper macro

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.

---
 include/linux/slab.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/slab.h +6 -0
index 1c670c16c737..ee6499088ad3 100644
@@ -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
  *
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 mm/kasan: simplify is_kmalloc check

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

---
 include/linux/kasan.h | 9 ---------
 mm/kasan/common.c     | 9 ++-------
 mm/slab_common.c      | 1 -
 3 files changed, 2 insertions(+), 17 deletions(-)
include/linux/kasan.h +0 -9
index dff604912687..fc46f5d6f404 100644
@@ -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) {}
mm/kasan/common.c +2 -7
index 1f30080a7a4c..f7e0e5067e7a 100644
@@ -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(). */
mm/slab_common.c +0 -1
index 8276022f0da4..a5480d67f391 100644
@@ -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;
Signature · 1 line
2.34.1
author
#2 lore raw

Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check

Quoted 61 lines · Feng

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

---
 include/linux/kasan.h | 9 ---------
 mm/kasan/common.c     | 9 ++-------
 mm/slab_common.c      | 1 -
 3 files changed, 2 insertions(+), 17 deletions(-)
include/linux/kasan.h +0 -9
index dff604912687..fc46f5d6f404 100644
@@ -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) {}
mm/kasan/common.c +0 -5
index 1f30080a7a4c..f7e0e5067e7a 100644
@@ -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);

mm/kasan/common.c void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, · line 324

Quoted 5 lines
mm/kasan/common.c +1 -1
@@ -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

Quoted 27 lines
mm/kasan/common.c +1 -1
 		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(). */
mm/slab_common.c +0 -1
index 8276022f0da4..a5480d67f391 100644
@@ -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;
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check

In reply to Feng Tang
Quoted 5 lines · Feng Tang

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

Hi Feng,

Nice simplification!

Quoted 11 lines
---
 include/linux/kasan.h | 9 ---------
 mm/kasan/common.c     | 9 ++-------
 mm/slab_common.c      | 1 -
 3 files changed, 2 insertions(+), 17 deletions(-)
include/linux/kasan.h +0 -0
index dff604912687..fc46f5d6f404 100644

include/linux/kasan.h struct kasan_cache { · line 105

Quoted 5 lines
include/linux/kasan.h +0 -1
@@ -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.

Quoted 73 lines
include/linux/kasan.h +0 -8
 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) {}
mm/kasan/common.c +2 -7
index 1f30080a7a4c..f7e0e5067e7a 100644
@@ -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(). */
mm/slab_common.c +0 -2
index 8276022f0da4..a5480d67f391 100644
@@ -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

author
#4 lore raw

Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check

Quoted 17 lines · Andrey Konovalov
Quoted 5 lines · Feng Tang

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

Hi Feng,

Nice simplification!

Quoted 6 lines
---
 include/linux/kasan.h | 9 ---------
 mm/kasan/common.c     | 9 ++-------
 mm/slab_common.c      | 1 -
 3 files changed, 2 insertions(+), 17 deletions(-)
Quoted 14 lines

include/linux/kasan.h struct kasan_cache { · line 105

Quoted 10 lines
include/linux/kasan.h +0 -1
index dff604912687..fc46f5d6f404 100644
@@ -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

---

include/linux/kasan.h +5 -8
index 0ac6505367ee..f2e41290094e 100644
@@ -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,
include/linux/slab_def.h +1 -1
index f0ffad6a3365..39f7f1f95de2 100644
@@ -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
 
include/linux/slub_def.h +1 -1
index f9c68a9dac04..4e7cdada4bbb 100644
@@ -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
 
#5 lore raw

Re: [PATCH -next 2/2] mm/kasan: simplify is_kmalloc check

Quoted 93 lines · Feng Tang
Quoted 17 lines · Andrey Konovalov
Quoted 5 lines · Feng Tang

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

Hi Feng,

Nice simplification!

Quoted 6 lines
---
 include/linux/kasan.h | 9 ---------
 mm/kasan/common.c     | 9 ++-------
 mm/slab_common.c      | 1 -
 3 files changed, 2 insertions(+), 17 deletions(-)
Quoted 14 lines

include/linux/kasan.h struct kasan_cache { · line 105

Quoted 10 lines
include/linux/kasan.h +0 -1
index dff604912687..fc46f5d6f404 100644
@@ -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

---

include/linux/kasan.h +5 -8
index 0ac6505367ee..f2e41290094e 100644
@@ -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,
include/linux/slab_def.h +1 -1
index f0ffad6a3365..39f7f1f95de2 100644
@@ -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
 
include/linux/slub_def.h +0 -0
index f9c68a9dac04..4e7cdada4bbb 100644

include/linux/slub_def.h struct kmem_cache { · line 137

Quoted 7 lines
include/linux/slub_def.h +1 -1
@@ -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!

maintainer
#6 lore raw
In reply to Feng Tang

include/linux/slab.h extern void kvfree_sensitive(const void *addr, size_t len); · line 765

Quoted 5 lines · On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <[email protected]> wrote:
include/linux/slab.h +5 -0
+#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.

author
#7 lore raw
Quoted 9 lines · Andrew Morton

Review

Quoted 5 lines · On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <[email protected]> wrote:
(quoted diff) +5 -0
+#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 :)

Quoted 6 lines

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

---

include/linux/slab.h +10 -0
index 067f0e80be9e..e4fcdbfb3477 100644
@@ -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
  */
mm/slab_common.c +7 -0
index a5480d67f391..860e804b7c0a 100644
@@ -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
  */
maintainer
#8 lore raw
Quoted 9 lines · On Tue, 22 Nov 2022 13:30:19 +0800 Feng Tang <[email protected]> wrote:
Quoted 6 lines

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.

#9 lore raw
In reply to Feng Tang
Quoted 20 lines · Feng Tang
Quoted 9 lines · Andrew Morton

include/linux/slab.h line 160

Quoted 5 lines · On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <[email protected]> wrote:
include/linux/slab.h +5 -0
+#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);
| ^~
"

Quoted 3 lines

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.

Quoted 4 lines

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 :)

Quoted 9 lines
Quoted 6 lines

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.

Quoted 43 lines

Thanks,
Feng

---

include/linux/slab.h +10 -0
index 067f0e80be9e..e4fcdbfb3477 100644
@@ -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
  */
mm/slab_common.c +7 -0
index a5480d67f391..860e804b7c0a 100644
@@ -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
  */
author
#10 lore raw
Quoted 21 lines · Vlastimil Babka
Quoted 20 lines · Feng Tang
Quoted 9 lines · Andrew Morton

include/linux/slab.h line 160

Quoted 5 lines · On Mon, 21 Nov 2022 21:50:23 +0800 Feng Tang <[email protected]> wrote:
include/linux/slab.h +5 -0
+#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);
| ^~
"

Quoted 7 lines
Quoted 3 lines

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".

Quoted 12 lines
Quoted 4 lines

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 :)

Quoted 5 lines
Quoted 5 lines

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

	x = is_kmalloc_cache(s++);
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

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