From nobody Sat Feb 7 16:26:36 2026 Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA56D1B583E for ; Wed, 14 Aug 2024 16:10:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723651863; cv=none; b=kMrTMuOtsnSL+YUR0of+5nncmob1OGAL+Uvrr9CSqwEFx63Mk6OkRU3qCYf/hO/4rkYnxqM1ZgmIIZ/ePB1nJ3UUK3Zp32lLcMyRyXJzGGbkV5DSHZ2DgXa2VnpFGolYGDL90FPGhMm5dWEEq5mJdzYS205ZhbgdzjBeumWzkWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723651863; c=relaxed/simple; bh=078Bn7zk6NtrXcHFJUG+DC8uvq8OxMYGE+AxLhOOtl4=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Aj5wOZu8VlPycEMlT5n5sNVAMBoRZKDfpLtyZnCTtBY7kHT3S1fhDRONJ2RvX0if/ayhKflI2aZzzQ+YkSsWWfxhgtu3+er2QAhImf0LjmKrQj1UW2ZHo+pdGW4ScjlMFyf3xkNTRIeK7ioV54hIbDy1CeGAcwUmoFnAYSz68eQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=vLOP3kXA; arc=none smtp.client-ip=91.218.175.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="vLOP3kXA" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1723651857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=CK1cjU9tK76LBIellpq38/TTF/N+OU0syvysYZxFmi4=; b=vLOP3kXA/H1lb3ouW5Pxg+CoriFgubH+OK2L73y/uGDRjDduz8x4p+sJuLuvI8wXMlAo7j n1KIyXyD65A0EqUILQcMlfu+VvpiFjzUmF7dmhu6qQvh7NYjltFjxEJWegB8SRmXhUaXwl umVpD7cqjyDCscKhSbKNKVz9Kc5lvmc= From: andrey.konovalov@linux.dev To: Marco Elver , Matthew Maurer , Miguel Ojeda Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] kasan: simplify and clarify Makefile Date: Wed, 14 Aug 2024 18:10:52 +0200 Message-Id: <20240814161052.10374-1-andrey.konovalov@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Andrey Konovalov 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 Acked-by: Marco Elver Reviewed-by: Miguel Ojeda --- 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 =20 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 :=3D 10000 else call_threshold :=3D 0 endif =20 -CFLAGS_KASAN_MINIMAL :=3D -fsanitize=3Dkernel-address - -# -fasan-shadow-offset fails without -fsanitize -CFLAGS_KASAN_SHADOW :=3D $(call cc-option, -fsanitize=3Dkernel-address \ - -fasan-shadow-offset=3D$(KASAN_SHADOW_OFFSET), \ - $(call cc-option, -fsanitize=3Dkernel-address \ - -mllvm -asan-mapping-offset=3D$(KASAN_SHADOW_OFFSET))) - -ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),) - CFLAGS_KASAN :=3D $(CFLAGS_KASAN_MINIMAL) -else - # Now add all the compiler specific options that are valid standalone - CFLAGS_KASAN :=3D $(CFLAGS_KASAN_SHADOW) \ - $(call cc-param,asan-globals=3D1) \ - $(call cc-param,asan-instrumentation-with-call-threshold=3D$(call_thresh= old)) \ - $(call cc-param,asan-instrument-allocas=3D1) -endif - -CFLAGS_KASAN +=3D $(call cc-param,asan-stack=3D$(stack_enable)) +# First, enable -fsanitize=3Dkernel-address together with providing the sh= adow +# 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 :=3D $(call cc-option, -fsanitize=3Dkernel-address \ + -fasan-shadow-offset=3D$(KASAN_SHADOW_OFFSET), \ + $(call cc-option, -fsanitize=3Dkernel-address \ + -mllvm -asan-mapping-offset=3D$(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 +=3D $(call cc-param,asan-instrumentation-with-call-threshold= =3D$(call_threshold)) \ + $(call cc-param,asan-stack=3D$(stack_enable)) \ + $(call cc-param,asan-instrument-allocas=3D1) \ + $(call cc-param,asan-globals=3D1) =20 # 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 =20 ifdef CONFIG_KASAN_INLINE - instrumentation_flags :=3D $(call cc-param,hwasan-mapping-offset=3D$(K= ASAN_SHADOW_OFFSET)) + instrumentation_flags :=3D $(call cc-param,hwasan-mapping-offset=3D$(KASA= N_SHADOW_OFFSET)) else - instrumentation_flags :=3D $(call cc-param,hwasan-instrument-with-call= s=3D1) + instrumentation_flags :=3D $(call cc-param,hwasan-instrument-with-calls= =3D1) endif =20 CFLAGS_KASAN :=3D -fsanitize=3Dkernel-hwaddress \ @@ -70,7 +71,7 @@ CFLAGS_KASAN :=3D -fsanitize=3Dkernel-hwaddress \ =20 # Instrument memcpy/memset/memmove calls by using instrumented __hwasan_me= m*(). ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y) -CFLAGS_KASAN +=3D $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=3D1) + CFLAGS_KASAN +=3D $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=3D1) endif =20 endif # CONFIG_KASAN_SW_TAGS --=20 2.25.1