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

From: Feng Tang
Date: Tue Nov 22 2022 - 01:57:10 EST


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@xxxxxxxxx> 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@xxxxxxxxx>
>
> 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