linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] kasan: simplify and clarify Makefile
@ 2024-08-14 16:10 andrey.konovalov
  2024-08-14 16:18 ` Miguel Ojeda
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: andrey.konovalov @ 2024-08-14 16:10 UTC (permalink / raw)
  To: Marco Elver, Matthew Maurer, Miguel Ojeda
  Cc: Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov,
	Andrey Ryabinin, kasan-dev, Andrew Morton, linux-mm,
	linux-kernel

From: Andrey Konovalov <andreyknvl@gmail.com>

When KASAN support was being added to the Linux kernel, GCC did not yet
support all of the KASAN-related compiler options. Thus, the KASAN
Makefile had to probe the compiler for supported options.

Nowadays, the Linux kernel GCC version requirement is 5.1+, and thus we
don't need the probing of the -fasan-shadow-offset parameter: it exists in
all 5.1+ GCCs.

Simplify the KASAN Makefile to drop CFLAGS_KASAN_MINIMAL.

Also add a few more comments and unify the indentation.

Signed-off-by: Andrey Konovalov <andreyknvl@gmail.com>

---

Changes v1->v2:
- Comments fixes based on Miguel Ojeda's feedback.
---
 scripts/Makefile.kasan | 45 +++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index 390658a2d5b74..aab4154af00a7 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -22,30 +22,31 @@ endif
 ifdef CONFIG_KASAN_GENERIC
 
 ifdef CONFIG_KASAN_INLINE
+	# When the number of memory accesses in a function is less than this
+	# call threshold number, the compiler will use inline instrumentation.
+	# 10000 is chosen offhand as a sufficiently large number to make all
+	# kernel functions to be instrumented inline.
 	call_threshold := 10000
 else
 	call_threshold := 0
 endif
 
-CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
-
-# -fasan-shadow-offset fails without -fsanitize
-CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
-			-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
-			$(call cc-option, -fsanitize=kernel-address \
-			-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
-
-ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
-	CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
-else
-	# Now add all the compiler specific options that are valid standalone
-	CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
-	 $(call cc-param,asan-globals=1) \
-	 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
-	 $(call cc-param,asan-instrument-allocas=1)
-endif
-
-CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
+# First, enable -fsanitize=kernel-address together with providing the shadow
+# mapping offset, as for GCC, -fasan-shadow-offset fails without -fsanitize
+# (GCC accepts the shadow mapping offset via -fasan-shadow-offset instead of
+# a --param like the other KASAN parameters).
+# Instead of ifdef-checking the compiler, rely on cc-option.
+CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
+		-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
+		$(call cc-option, -fsanitize=kernel-address \
+		-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
+
+# Now, add other parameters enabled similarly in both GCC and Clang.
+# As some of them are not supported by older compilers, use cc-param.
+CFLAGS_KASAN += $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
+		$(call cc-param,asan-stack=$(stack_enable)) \
+		$(call cc-param,asan-instrument-allocas=1) \
+		$(call cc-param,asan-globals=1)
 
 # Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
 # instead. With compilers that don't support this option, compiler-inserted
@@ -57,9 +58,9 @@ endif # CONFIG_KASAN_GENERIC
 ifdef CONFIG_KASAN_SW_TAGS
 
 ifdef CONFIG_KASAN_INLINE
-    instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
+	instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
 else
-    instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
+	instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
 endif
 
 CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
@@ -70,7 +71,7 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
 
 # Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
 ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
-CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
+	CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
 endif
 
 endif # CONFIG_KASAN_SW_TAGS
-- 
2.25.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kasan: simplify and clarify Makefile
  2024-08-14 16:10 [PATCH v2] kasan: simplify and clarify Makefile andrey.konovalov
@ 2024-08-14 16:18 ` Miguel Ojeda
  2024-08-22 13:49 ` Marco Elver
  2024-09-16 16:14 ` Miguel Ojeda
  2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2024-08-14 16:18 UTC (permalink / raw)
  To: andrey.konovalov
  Cc: Marco Elver, Matthew Maurer, Miguel Ojeda, Andrey Konovalov,
	Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasan-dev,
	Andrew Morton, linux-mm, linux-kernel

On Wed, Aug 14, 2024 at 6:11 PM <andrey.konovalov@linux.dev> wrote:
>
> From: Andrey Konovalov <andreyknvl@gmail.com>
>
> When KASAN support was being added to the Linux kernel, GCC did not yet
> support all of the KASAN-related compiler options. Thus, the KASAN
> Makefile had to probe the compiler for supported options.
>
> Nowadays, the Linux kernel GCC version requirement is 5.1+, and thus we
> don't need the probing of the -fasan-shadow-offset parameter: it exists in
> all 5.1+ GCCs.
>
> Simplify the KASAN Makefile to drop CFLAGS_KASAN_MINIMAL.
>
> Also add a few more comments and unify the indentation.
>
> Signed-off-by: Andrey Konovalov <andreyknvl@gmail.com>

Looks good to me! (I didn't actually test it, though!)

Reviewed-by: Miguel Ojeda <ojeda@kernel.org>

Cheers,
Miguel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kasan: simplify and clarify Makefile
  2024-08-14 16:10 [PATCH v2] kasan: simplify and clarify Makefile andrey.konovalov
  2024-08-14 16:18 ` Miguel Ojeda
@ 2024-08-22 13:49 ` Marco Elver
  2024-08-22 21:46   ` Andrey Konovalov
  2024-09-16 16:14 ` Miguel Ojeda
  2 siblings, 1 reply; 5+ messages in thread
From: Marco Elver @ 2024-08-22 13:49 UTC (permalink / raw)
  To: andrey.konovalov
  Cc: Matthew Maurer, Miguel Ojeda, Andrey Konovalov,
	Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasan-dev,
	Andrew Morton, linux-mm, linux-kernel

On Wed, 14 Aug 2024 at 18:11, <andrey.konovalov@linux.dev> wrote:
>
> From: Andrey Konovalov <andreyknvl@gmail.com>
>
> When KASAN support was being added to the Linux kernel, GCC did not yet
> support all of the KASAN-related compiler options. Thus, the KASAN
> Makefile had to probe the compiler for supported options.
>
> Nowadays, the Linux kernel GCC version requirement is 5.1+, and thus we
> don't need the probing of the -fasan-shadow-offset parameter: it exists in
> all 5.1+ GCCs.
>
> Simplify the KASAN Makefile to drop CFLAGS_KASAN_MINIMAL.
>
> Also add a few more comments and unify the indentation.
>
> Signed-off-by: Andrey Konovalov <andreyknvl@gmail.com>

Acked-by: Marco Elver <elver@google.com>

Just in case, did you test SW and HW tags modes as well?

> ---
>
> Changes v1->v2:
> - Comments fixes based on Miguel Ojeda's feedback.
> ---
>  scripts/Makefile.kasan | 45 +++++++++++++++++++++---------------------
>  1 file changed, 23 insertions(+), 22 deletions(-)
>
> diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
> index 390658a2d5b74..aab4154af00a7 100644
> --- a/scripts/Makefile.kasan
> +++ b/scripts/Makefile.kasan
> @@ -22,30 +22,31 @@ endif
>  ifdef CONFIG_KASAN_GENERIC
>
>  ifdef CONFIG_KASAN_INLINE
> +       # When the number of memory accesses in a function is less than this
> +       # call threshold number, the compiler will use inline instrumentation.
> +       # 10000 is chosen offhand as a sufficiently large number to make all
> +       # kernel functions to be instrumented inline.
>         call_threshold := 10000
>  else
>         call_threshold := 0
>  endif
>
> -CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
> -
> -# -fasan-shadow-offset fails without -fsanitize
> -CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
> -                       -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
> -                       $(call cc-option, -fsanitize=kernel-address \
> -                       -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
> -
> -ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
> -       CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
> -else
> -       # Now add all the compiler specific options that are valid standalone
> -       CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
> -        $(call cc-param,asan-globals=1) \
> -        $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
> -        $(call cc-param,asan-instrument-allocas=1)
> -endif
> -
> -CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
> +# First, enable -fsanitize=kernel-address together with providing the shadow
> +# mapping offset, as for GCC, -fasan-shadow-offset fails without -fsanitize
> +# (GCC accepts the shadow mapping offset via -fasan-shadow-offset instead of
> +# a --param like the other KASAN parameters).
> +# Instead of ifdef-checking the compiler, rely on cc-option.
> +CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
> +               -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
> +               $(call cc-option, -fsanitize=kernel-address \
> +               -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
> +
> +# Now, add other parameters enabled similarly in both GCC and Clang.
> +# As some of them are not supported by older compilers, use cc-param.
> +CFLAGS_KASAN += $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
> +               $(call cc-param,asan-stack=$(stack_enable)) \
> +               $(call cc-param,asan-instrument-allocas=1) \
> +               $(call cc-param,asan-globals=1)
>
>  # Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
>  # instead. With compilers that don't support this option, compiler-inserted
> @@ -57,9 +58,9 @@ endif # CONFIG_KASAN_GENERIC
>  ifdef CONFIG_KASAN_SW_TAGS
>
>  ifdef CONFIG_KASAN_INLINE
> -    instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
> +       instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
>  else
> -    instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
> +       instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
>  endif
>
>  CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
> @@ -70,7 +71,7 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
>
>  # Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
>  ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
> -CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
> +       CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
>  endif
>
>  endif # CONFIG_KASAN_SW_TAGS
> --
> 2.25.1
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kasan: simplify and clarify Makefile
  2024-08-22 13:49 ` Marco Elver
@ 2024-08-22 21:46   ` Andrey Konovalov
  0 siblings, 0 replies; 5+ messages in thread
From: Andrey Konovalov @ 2024-08-22 21:46 UTC (permalink / raw)
  To: Marco Elver
  Cc: andrey.konovalov, Matthew Maurer, Miguel Ojeda,
	Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasan-dev,
	Andrew Morton, linux-mm, linux-kernel

On Thu, Aug 22, 2024 at 3:49 PM Marco Elver <elver@google.com> wrote:
>
> On Wed, 14 Aug 2024 at 18:11, <andrey.konovalov@linux.dev> wrote:
> >
> > From: Andrey Konovalov <andreyknvl@gmail.com>
> >
> > When KASAN support was being added to the Linux kernel, GCC did not yet
> > support all of the KASAN-related compiler options. Thus, the KASAN
> > Makefile had to probe the compiler for supported options.
> >
> > Nowadays, the Linux kernel GCC version requirement is 5.1+, and thus we
> > don't need the probing of the -fasan-shadow-offset parameter: it exists in
> > all 5.1+ GCCs.
> >
> > Simplify the KASAN Makefile to drop CFLAGS_KASAN_MINIMAL.
> >
> > Also add a few more comments and unify the indentation.
> >
> > Signed-off-by: Andrey Konovalov <andreyknvl@gmail.com>
>
> Acked-by: Marco Elver <elver@google.com>
>
> Just in case, did you test SW and HW tags modes as well?

HW_TAGS doesn't rely on Makefile.kasan, and for SW_TAGS, this change
is a no-op. But I did just test them just in case - everything works.
Thanks!


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kasan: simplify and clarify Makefile
  2024-08-14 16:10 [PATCH v2] kasan: simplify and clarify Makefile andrey.konovalov
  2024-08-14 16:18 ` Miguel Ojeda
  2024-08-22 13:49 ` Marco Elver
@ 2024-09-16 16:14 ` Miguel Ojeda
  2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2024-09-16 16:14 UTC (permalink / raw)
  To: andrey.konovalov
  Cc: Marco Elver, Matthew Maurer, Miguel Ojeda, Andrey Konovalov,
	Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasan-dev,
	Andrew Morton, linux-mm, linux-kernel

On Wed, Aug 14, 2024 at 6:11 PM <andrey.konovalov@linux.dev> wrote:
>
> When KASAN support was being added to the Linux kernel, GCC did not yet
> support all of the KASAN-related compiler options. Thus, the KASAN
> Makefile had to probe the compiler for supported options.
>
> Nowadays, the Linux kernel GCC version requirement is 5.1+, and thus we
> don't need the probing of the -fasan-shadow-offset parameter: it exists in
> all 5.1+ GCCs.
>
> Simplify the KASAN Makefile to drop CFLAGS_KASAN_MINIMAL.
>
> Also add a few more comments and unify the indentation.
>
> Signed-off-by: Andrey Konovalov <andreyknvl@gmail.com>

Applied to `rust-next` for the Rust KASAN patch series. Thanks everyone!

(Andrew is also carrying it, but we agreed to do that).

Cheers,
Miguel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-09-16 16:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-14 16:10 [PATCH v2] kasan: simplify and clarify Makefile andrey.konovalov
2024-08-14 16:18 ` Miguel Ojeda
2024-08-22 13:49 ` Marco Elver
2024-08-22 21:46   ` Andrey Konovalov
2024-09-16 16:14 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox