Re: [PATCH] LoongArch: Add kernel address sanitizer support

From: Andrey Konovalov
Date: Wed Mar 29 2023 - 15:02:55 EST


> diff --git a/include/linux/kasan.h b/include/linux/kasan.h
> index f7ef70661ce2..3b91b941873d 100644
> --- a/include/linux/kasan.h
> +++ b/include/linux/kasan.h
> @@ -54,11 +54,13 @@ extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
> int kasan_populate_early_shadow(const void *shadow_start,
> const void *shadow_end);
>
> +#ifndef __HAVE_ARCH_SHADOW_MAP
> static inline void *kasan_mem_to_shadow(const void *addr)
> {
> return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
> + KASAN_SHADOW_OFFSET;
> }
> +#endif
>
> int kasan_add_zero_shadow(void *start, unsigned long size);
> void kasan_remove_zero_shadow(void *start, unsigned long size);
> diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
> index e5eef670735e..f86194750df5 100644
> --- a/mm/kasan/generic.c
> +++ b/mm/kasan/generic.c
> @@ -175,6 +175,11 @@ static __always_inline bool check_region_inline(unsigned long addr,
> if (unlikely(!addr_has_metadata((void *)addr)))
> return !kasan_report(addr, size, write, ret_ip);
>
> +#ifndef __HAVE_ARCH_SHADOW_MAP
> + if (unlikely(kasan_mem_to_shadow((unsigned long *)addr) == NULL))
> + return !kasan_report(addr, size, write, ret_ip);
> +#endif

This should have been ifdef, right?

But I don't think you need this check here at all: addr_has_metadata
already checks that shadow exists.

> +
> if (likely(!memory_is_poisoned(addr, size)))
> return true;
>
> diff --git a/mm/kasan/init.c b/mm/kasan/init.c
> index cc64ed6858c6..860061a22ca9 100644
> --- a/mm/kasan/init.c
> +++ b/mm/kasan/init.c
> @@ -166,8 +166,9 @@ static int __ref zero_pud_populate(p4d_t *p4d, unsigned long addr,
> if (!p)
> return -ENOMEM;
> } else {
> - pud_populate(&init_mm, pud,
> - early_alloc(PAGE_SIZE, NUMA_NO_NODE));
> + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
> + pmd_init(p);
> + pud_populate(&init_mm, pud, p);
> }
> }
> zero_pmd_populate(pud, addr, next);
> @@ -207,8 +208,9 @@ static int __ref zero_p4d_populate(pgd_t *pgd, unsigned long addr,
> if (!p)
> return -ENOMEM;
> } else {
> - p4d_populate(&init_mm, p4d,
> - early_alloc(PAGE_SIZE, NUMA_NO_NODE));
> + p = early_alloc(PAGE_SIZE, NUMA_NO_NODE);
> + pud_init(p);
> + p4d_populate(&init_mm, p4d, p);

Please explain why these changes are needed in the patch description.

> }
> }
> zero_pud_populate(p4d, addr, next);
> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> index a61eeee3095a..033335c13b25 100644
> --- a/mm/kasan/kasan.h
> +++ b/mm/kasan/kasan.h
> @@ -291,16 +291,22 @@ struct kasan_stack_ring {
>
> #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
>
> +#ifndef __HAVE_ARCH_SHADOW_MAP
> static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
> {
> return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
> << KASAN_SHADOW_SCALE_SHIFT);
> }
> +#endif
>
> static __always_inline bool addr_has_metadata(const void *addr)
> {
> +#ifdef __HAVE_ARCH_SHADOW_MAP
> + return (kasan_mem_to_shadow((void *)addr) != NULL);
> +#else
> return (kasan_reset_tag(addr) >=
> kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
> +#endif
> }
>
> /**
> --
> 2.20.1
>