diff mbox series

[05/16] kasan: fix hwasan build for gcc

Message ID 20210409202713.NIELx3Tx9%akpm@linux-foundation.org (mailing list archive)
State New, archived
Headers show
Series [01/16] MAINTAINERS: update CZ.NIC's Turris information | expand

Commit Message

Andrew Morton April 9, 2021, 8:27 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de>
Subject: kasan: fix hwasan build for gcc

gcc-11 adds support for -fsanitize=kernel-hwaddress, so it becomes
possible to enable CONFIG_KASAN_SW_TAGS.

Unfortunately this fails to build at the moment, because the corresponding
command line arguments use llvm specific syntax.

Change it to use the cc-param macro instead, which works on both clang and
gcc.

Link: https://lkml.kernel.org/r/20210323124112.1229772-1-arnd@kernel.org
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Marco Elver <elver@google.com>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Michal Marek <michal.lkml@markovi.net>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 scripts/Makefile.kasan |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Andrey Konovalov April 9, 2021, 8:39 p.m. UTC | #1
On Fri, Apr 9, 2021 at 10:27 PM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> From: Arnd Bergmann <arnd@arndb.de>
> Subject: kasan: fix hwasan build for gcc
>
> gcc-11 adds support for -fsanitize=kernel-hwaddress, so it becomes
> possible to enable CONFIG_KASAN_SW_TAGS.
>
> Unfortunately this fails to build at the moment, because the corresponding
> command line arguments use llvm specific syntax.
>
> Change it to use the cc-param macro instead, which works on both clang and
> gcc.
>
> Link: https://lkml.kernel.org/r/20210323124112.1229772-1-arnd@kernel.org
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> Reviewed-by: Marco Elver <elver@google.com>
> Cc: Masahiro Yamada <masahiroy@kernel.org>
> Cc: Michal Marek <michal.lkml@markovi.net>
> Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
> Cc: Nathan Chancellor <nathan@kernel.org>
> Cc: Nick Desaulniers <ndesaulniers@google.com>
> Cc: Alexander Potapenko <glider@google.com>
> Cc: Andrey Konovalov <andreyknvl@gmail.com>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>
>  scripts/Makefile.kasan |    8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> --- a/scripts/Makefile.kasan~kasan-fix-hwasan-build-for-gcc
> +++ a/scripts/Makefile.kasan
> @@ -36,14 +36,14 @@ endif # CONFIG_KASAN_GENERIC
>  ifdef CONFIG_KASAN_SW_TAGS
>
>  ifdef CONFIG_KASAN_INLINE
> -    instrumentation_flags := -mllvm -hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)
> +    instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
>  else
> -    instrumentation_flags := -mllvm -hwasan-instrument-with-calls=1
> +    instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
>  endif
>
>  CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
> -               -mllvm -hwasan-instrument-stack=$(CONFIG_KASAN_STACK) \
> -               -mllvm -hwasan-use-short-granules=0 \
> +               $(call cc-param,hwasan-instrument-stack=$(CONFIG_KASAN_STACK)) \
> +               $(call cc-param,hwasan-use-short-granules=0) \
>                 $(instrumentation_flags)
>
>  endif # CONFIG_KASAN_SW_TAGS
> _

Hi,

As I commented on the patch, this breaks SW_TAGS build with Clang for me with:

arch/arm64/include/asm/current.h:19: undefined reference to `__hwasan_tls'

The reason for this is that cc-param is only defined for
KASAN_GENERIC, the definition needs to be moved.

Thanks!
Andrew Morton April 9, 2021, 8:58 p.m. UTC | #2
On Fri, 9 Apr 2021 22:39:46 +0200 Andrey Konovalov <andreyknvl@gmail.com> wrote:

> >
> >  endif # CONFIG_KASAN_SW_TAGS
> > _
> 
> Hi,
> 
> As I commented on the patch, this breaks SW_TAGS build with Clang for me with:
> 
> arch/arm64/include/asm/current.h:19: undefined reference to `__hwasan_tls'
> 
> The reason for this is that cc-param is only defined for
> KASAN_GENERIC, the definition needs to be moved.
> 

Oh.  I thought that had been fixed.

Please send a patch?
Linus Torvalds April 9, 2021, 9:55 p.m. UTC | #3
On Fri, Apr 9, 2021 at 1:39 PM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
> As I commented on the patch, this breaks SW_TAGS build with Clang for me with:
>
> arch/arm64/include/asm/current.h:19: undefined reference to `__hwasan_tls'

I've dropped this patch and the next one ("[PATCH 06/16] kasan: remove
redundant config option") that depended on it.

             Linus
Marco Elver April 12, 2021, 9:56 a.m. UTC | #4
On Fri, Apr 09, 2021 at 01:58PM -0700, Andrew Morton wrote:
> On Fri, 9 Apr 2021 22:39:46 +0200 Andrey Konovalov <andreyknvl@gmail.com> wrote:
> 
> > >
> > >  endif # CONFIG_KASAN_SW_TAGS
> > > _
> > 
> > Hi,
> > 
> > As I commented on the patch, this breaks SW_TAGS build with Clang for me with:
> > 
> > arch/arm64/include/asm/current.h:19: undefined reference to `__hwasan_tls'
> > 
> > The reason for this is that cc-param is only defined for
> > KASAN_GENERIC, the definition needs to be moved.
> > 
> 
> Oh.  I thought that had been fixed.
> 
> Please send a patch?

I think we need something like the below.

Unless a fixed version has already been sent, feel free to squash
(applies immediately after "kasan: fix hwasan build for gcc", and before
the conflicting "kasan: remove redundant config option").

Thanks,
-- Marco

------ >8 ------

From: Marco Elver <elver@google.com>
Date: Sun, 11 Apr 2021 21:32:01 +0200
Subject: [PATCH] fixup for "kasan: fix hwasan build for gcc"

Signed-off-by: Marco Elver <elver@google.com>
---
 scripts/Makefile.kasan | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index 0a2789783d1b..127012f45166 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -2,6 +2,8 @@
 CFLAGS_KASAN_NOSANITIZE := -fno-builtin
 KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
 
+cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
+
 ifdef CONFIG_KASAN_GENERIC
 
 ifdef CONFIG_KASAN_INLINE
@@ -12,8 +14,6 @@ endif
 
 CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
-cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
-
 # -fasan-shadow-offset fails without -fsanitize
 CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
 			-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
Andrey Konovalov April 12, 2021, 12:54 p.m. UTC | #5
On Mon, Apr 12, 2021 at 11:56 AM Marco Elver <elver@google.com> wrote:
>
> On Fri, Apr 09, 2021 at 01:58PM -0700, Andrew Morton wrote:
> > On Fri, 9 Apr 2021 22:39:46 +0200 Andrey Konovalov <andreyknvl@gmail.com> wrote:
> >
> > > >
> > > >  endif # CONFIG_KASAN_SW_TAGS
> > > > _
> > >
> > > Hi,
> > >
> > > As I commented on the patch, this breaks SW_TAGS build with Clang for me with:
> > >
> > > arch/arm64/include/asm/current.h:19: undefined reference to `__hwasan_tls'
> > >
> > > The reason for this is that cc-param is only defined for
> > > KASAN_GENERIC, the definition needs to be moved.
> > >
> >
> > Oh.  I thought that had been fixed.
> >
> > Please send a patch?
>
> I think we need something like the below.
>
> Unless a fixed version has already been sent, feel free to squash
> (applies immediately after "kasan: fix hwasan build for gcc", and before
> the conflicting "kasan: remove redundant config option").
>
> Thanks,
> -- Marco
>
> ------ >8 ------
>
> From: Marco Elver <elver@google.com>
> Date: Sun, 11 Apr 2021 21:32:01 +0200
> Subject: [PATCH] fixup for "kasan: fix hwasan build for gcc"
>
> Signed-off-by: Marco Elver <elver@google.com>
> ---
>  scripts/Makefile.kasan | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
> index 0a2789783d1b..127012f45166 100644
> --- a/scripts/Makefile.kasan
> +++ b/scripts/Makefile.kasan
> @@ -2,6 +2,8 @@
>  CFLAGS_KASAN_NOSANITIZE := -fno-builtin
>  KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
>
> +cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
> +
>  ifdef CONFIG_KASAN_GENERIC
>
>  ifdef CONFIG_KASAN_INLINE
> @@ -12,8 +14,6 @@ endif
>
>  CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
>
> -cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
> -
>  # -fasan-shadow-offset fails without -fsanitize
>  CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
>                         -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
> --
> 2.31.1.295.g9ea45b61b8-goog
>

This fix-up looks good to me.

Thank you, Marco!
diff mbox series

Patch

--- a/scripts/Makefile.kasan~kasan-fix-hwasan-build-for-gcc
+++ a/scripts/Makefile.kasan
@@ -36,14 +36,14 @@  endif # CONFIG_KASAN_GENERIC
 ifdef CONFIG_KASAN_SW_TAGS
 
 ifdef CONFIG_KASAN_INLINE
-    instrumentation_flags := -mllvm -hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)
+    instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
 else
-    instrumentation_flags := -mllvm -hwasan-instrument-with-calls=1
+    instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
 endif
 
 CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
-		-mllvm -hwasan-instrument-stack=$(CONFIG_KASAN_STACK) \
-		-mllvm -hwasan-use-short-granules=0 \
+		$(call cc-param,hwasan-instrument-stack=$(CONFIG_KASAN_STACK)) \
+		$(call cc-param,hwasan-use-short-granules=0) \
 		$(instrumentation_flags)
 
 endif # CONFIG_KASAN_SW_TAGS