Re: [PATCH RFC v2 09/21] kasan: inline kasan_reset_tag for tag-based modes

From: Andrey Konovalov
Date: Fri Oct 30 2020 - 12:20:08 EST


On Wed, Oct 28, 2020 at 12:05 PM Dmitry Vyukov <dvyukov@xxxxxxxxxx> wrote:
>
> On Thu, Oct 22, 2020 at 3:19 PM Andrey Konovalov <andreyknvl@xxxxxxxxxx> wrote:
> >
> > Using kasan_reset_tag() currently results in a function call. As it's
> > called quite often from the allocator code this leads to a noticeable
> > slowdown. Move it to include/linux/kasan.h and turn it into a static
> > inline function.
> >
> > Signed-off-by: Andrey Konovalov <andreyknvl@xxxxxxxxxx>
> > Link: https://linux-review.googlesource.com/id/I4d2061acfe91d480a75df00b07c22d8494ef14b5
> > ---
> > include/linux/kasan.h | 5 ++++-
> > mm/kasan/hw_tags.c | 5 -----
> > mm/kasan/kasan.h | 6 ++----
> > mm/kasan/sw_tags.c | 5 -----
> > 4 files changed, 6 insertions(+), 15 deletions(-)
> >
> > diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> > index 93d9834b7122..6377d7d3a951 100644
> > --- a/include/linux/kasan.h
> > +++ b/include/linux/kasan.h
> > @@ -187,7 +187,10 @@ static inline void kasan_record_aux_stack(void *ptr) {}
> >
> > void __init kasan_init_tags(void);
> >
> > -void *kasan_reset_tag(const void *addr);
> > +static inline void *kasan_reset_tag(const void *addr)
> > +{
> > + return (void *)arch_kasan_reset_tag(addr);
>
> It seems that all implementations already return (void *), so the cast
> is not needed.

arch_kasan_reset_tag() (->__tag_reset() -> __untagged_addr())
preserves the type of the argument, so the cast is needed.

>
> > +}
> >
> > bool kasan_report(unsigned long addr, size_t size,
> > bool is_write, unsigned long ip);
> > diff --git a/mm/kasan/hw_tags.c b/mm/kasan/hw_tags.c
> > index b372421258c8..c3a0e83b5e7a 100644
> > --- a/mm/kasan/hw_tags.c
> > +++ b/mm/kasan/hw_tags.c
> > @@ -24,11 +24,6 @@ void __init kasan_init_tags(void)
> > pr_info("KernelAddressSanitizer initialized\n");
> > }
> >
> > -void *kasan_reset_tag(const void *addr)
> > -{
> > - return reset_tag(addr);
> > -}
> > -
> > void kasan_poison_memory(const void *address, size_t size, u8 value)
> > {
> > set_mem_tag_range(reset_tag(address),
> > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> > index 456b264e5124..0ccbb3c4c519 100644
> > --- a/mm/kasan/kasan.h
> > +++ b/mm/kasan/kasan.h
> > @@ -246,15 +246,13 @@ static inline const void *arch_kasan_set_tag(const void *addr, u8 tag)
> > return addr;
> > }
> > #endif
> > -#ifndef arch_kasan_reset_tag
> > -#define arch_kasan_reset_tag(addr) ((void *)(addr))
> > -#endif
> > #ifndef arch_kasan_get_tag
> > #define arch_kasan_get_tag(addr) 0
> > #endif
> >
> > +/* kasan_reset_tag() defined in include/linux/kasan.h. */
> > +#define reset_tag(addr) ((void *)kasan_reset_tag(addr))
>
> The cast is not needed.
>
> I would also now remove reset_tag entirely by replacing it with
> kasan_reset_tag. Having 2 names for the same thing does not add
> clarity.

Will remove it.