Skip to contentSkip to conversation

kasan-simplify-and-clarify-makefile.patch removed from -mm tree

merged mm-stable Andrew Morton 2 participants 4 messages Sep 4, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: kasan: simplify and clarify Makefile
has been removed from the -mm tree. Its filename was
kasan-simplify-and-clarify-makefile.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Andrey Konovalov <[email protected]>
Subject: kasan: simplify and clarify Makefile
Date: Wed, 14 Aug 2024 00:40:27 +0200

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.

---

 scripts/Makefile.kasan |   45 +++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 22 deletions(-)
scripts/Makefile.kasan renamed from scripts/Makefile.kasan~kasan-simplify-and-clarify-makefile +23 -22
@@ -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-hwaddr
 
 # 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

_

Patches currently in -mm which might be from [email protected] are

#1 lore raw

Hi Andrew,

Quoted 8 lines · Andrew Morton

The quilt patch titled
Subject: kasan: simplify and clarify Makefile
has been removed from the -mm tree. Its filename was
kasan-simplify-and-clarify-makefile.patch

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

I think we were taking this one through the Rust tree so that we could merge the Rust KASAN support through there (and handle the other interdependencies we have this cycle), right?

I will apply those very soon (in case you moved it into mm-stable because you didn't see it in linux-next).

Thanks!

Cheers,
Miguel

#2 lore raw
Quoted 9 lines · On Wed, 4 Sep 2024 11:59:06 +0200 Miguel Ojeda…

Hi Andrew,

Quoted 6 lines · Andrew Morton

The quilt patch titled
Subject: kasan: simplify and clarify Makefile
has been removed from the -mm tree. Its filename was
kasan-simplify-and-clarify-makefile.patch

Quoted 6 lines
Quoted 2 lines

This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

I think we were taking this one through the Rust tree so that we could merge the Rust KASAN support through there (and handle the other interdependencies we have this cycle), right?

That's what I thought but after more than two weeks it still wasn't in linux-next so I assumed it had been forgotten, or that plans changed.

Quoted 2 lines

I will apply those very soon (in case you moved it into mm-stable because you didn't see it in linux-next).

OK thanks. I expect Stephen will notify us when there's a duplicate patch. As long as the two patches are identical code changes, git will sort it out when we merge with Linus. Otherwise I'll drop the mm.git copy.

#3 lore raw
Quoted 3 lines · Andrew Morton

That's what I thought but after more than two weeks it still wasn't in linux-next so I assumed it had been forgotten, or that plans changed.

Yeah, sorry, I wanted to pick it up together with the Rust KASAN one, and ideally that would go after the RUSTC_VERSION one that I am applying now.

Quoted 4 lines

OK thanks. I expect Stephen will notify us when there's a duplicate patch. As long as the two patches are identical code changes, git will sort it out when we merge with Linus. Otherwise I'll drop the mm.git copy.

Sounds good. Thanks!

Cheers,
Miguel