Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1147292pxv; Fri, 9 Jul 2021 19:31:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5TdUAnH5J9qvaGAbepyvL5IfZnEJe9EZN4QXEReBlN0JOe6WeNFoyJT0CuMgQ7H74YmrV X-Received: by 2002:aa7:c3d0:: with SMTP id l16mr4392065edr.225.1625884269732; Fri, 09 Jul 2021 19:31:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625884269; cv=none; d=google.com; s=arc-20160816; b=Sr9ollWmdkLmc9TuZdYdzASIBRUu22h40lVAG3RaWZm7mRVmuU1oqEJ60ecivgnfCo ckAQownAtBoJFzQGHqeU+B1Ba01GzKW7ZMxPV5tzAE9vo1tO4yLsnVVsnw5rohAi/gk8 Q/jmim+BoRBXUa+ODWFafWa993m1JilPpqTt7DY5SzK/6vwnJOMfqz/2/EqGFx7pKCBT YfbviC1M/ufR87eLmnjVpFAV3F0v/Al86JyDYlyVnU1t2IFEFv6pzOOxWInbab6wrCAt tl2UbuEaOBFY2IMfk++msGaXZy77wHvVM15Hb6odY6b17JNF+bUKDGvYZDjr+PWV87ni lemA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pAnOCRZTWvXIodLg9fb8plqxaN7lJNXcDtYnYSpGcNk=; b=TC3g+1ojt/mCdy4If8vHTFRolSK2fAU0S8JRi/SFcixCj2jmYTdh2YvSGznXLvJSab ENJb2qcUwrnDFUQQYHTvs5ZGqyO6UtbJ2QJI1n6tgv33UtMhgaAyWRS6W0zMcV9XH7HG VQz8ERR20EvbaQb1ZQ7u52fmPc9ZTvQ9U4ybxuDDYle7JrTNxERf4MogoURfLLDuTMs3 FJAdMudYHHANsnFFzzIsIux/LWHF1Vp3DoulzYv/NFdppdj6YTbF8RTQSDeaQ1j9nOAb kJ6YMqwCwFuAVwgzy0J0iihmHeAZcbh5WHvimpuw9CEY5gsPfsuPnVTLwmX6CmpOPsBM 5ZOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=n7svH8CB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bl4si10869910ejb.689.2021.07.09.19.30.46; Fri, 09 Jul 2021 19:31:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=n7svH8CB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234301AbhGJC3V (ORCPT + 99 others); Fri, 9 Jul 2021 22:29:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:42924 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232807AbhGJC1Z (ORCPT ); Fri, 9 Jul 2021 22:27:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 93F43613CC; Sat, 10 Jul 2021 02:24:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625883865; bh=gpXywUWvDLQJ6VBBCEvS+PBliEG7W+d3ZG1a4eB5zq0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n7svH8CBxe6DQfR+7SFExK+9gAWDHZQfYRecZNyCifLpqqRmxwde4kbxUSJ8WsPqQ BACtcp7aYLYhtOkMcYss+UyTczTeC0Y5y0AwPSeHhMWhhJRBsBoSRt4714eYF4bwxl woMAiAA4dHion4PV01Hk1UrqulDNKQWAHxZv2gk8peTzVnSTKNYBJX52Vy0pCKAbIk 1q9o/G8EefXqVliPBajUsqhFm0QpNFB4UXqROE+wyKYaPd+hXLP1C5Q4K1Pz6Ye3M1 mkmRVw1NLtmd9YMw9S45kFyHLSOSP1Zr5A/+u3CZ0oQM0CczSr2mPCo/LSWUSiR/Bi AxL59wbu7radA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Marco Elver , Peter Zijlstra , Miguel Ojeda , Nathan Chancellor , Nick Desaulniers , Kees Cook , Will Deacon , Ard Biesheuvel , Luc Van Oostenryck , Arvind Sankar , Masahiro Yamada , Sami Tolvanen , Arnd Bergmann , Dmitry Vyukov , Mark Rutland , Andrew Morton , Linus Torvalds , Sasha Levin , clang-built-linux@googlegroups.com Subject: [PATCH AUTOSEL 5.12 104/104] kcov: add __no_sanitize_coverage to fix noinstr for all architectures Date: Fri, 9 Jul 2021 22:21:56 -0400 Message-Id: <20210710022156.3168825-104-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210710022156.3168825-1-sashal@kernel.org> References: <20210710022156.3168825-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marco Elver [ Upstream commit 540540d06e9d9b3769b46d88def90f7e7c002322 ] Until now no compiler supported an attribute to disable coverage instrumentation as used by KCOV. To work around this limitation on x86, noinstr functions have their coverage instrumentation turned into nops by objtool. However, this solution doesn't scale automatically to other architectures, such as arm64, which are migrating to use the generic entry code. Clang [1] and GCC [2] have added support for the attribute recently. [1] https://github.com/llvm/llvm-project/commit/280333021e9550d80f5c1152a34e33e81df1e178 [2] https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=cec4d4a6782c9bd8d071839c50a239c49caca689 The changes will appear in Clang 13 and GCC 12. Add __no_sanitize_coverage for both compilers, and add it to noinstr. Note: In the Clang case, __has_feature(coverage_sanitizer) is only true if the feature is enabled, and therefore we do not require an additional defined(CONFIG_KCOV) (like in the GCC case where __has_attribute(..) is always true) to avoid adding redundant attributes to functions if KCOV is off. That being said, compilers that support the attribute will not generate errors/warnings if the attribute is redundantly used; however, where possible let's avoid it as it reduces preprocessed code size and associated compile-time overheads. [elver@google.com: Implement __has_feature(coverage_sanitizer) in Clang] Link: https://lkml.kernel.org/r/20210527162655.3246381-1-elver@google.com [elver@google.com: add comment explaining __has_feature() in Clang] Link: https://lkml.kernel.org/r/20210527194448.3470080-1-elver@google.com Link: https://lkml.kernel.org/r/20210525175819.699786-1-elver@google.com Signed-off-by: Marco Elver Acked-by: Peter Zijlstra (Intel) Reviewed-by: Miguel Ojeda Reviewed-by: Nathan Chancellor Cc: Nick Desaulniers Cc: Kees Cook Cc: Will Deacon Cc: Ard Biesheuvel Cc: Luc Van Oostenryck Cc: Arvind Sankar Cc: Masahiro Yamada Cc: Sami Tolvanen Cc: Arnd Bergmann Cc: Dmitry Vyukov Cc: Mark Rutland Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- include/linux/compiler-clang.h | 17 +++++++++++++++++ include/linux/compiler-gcc.h | 6 ++++++ include/linux/compiler_types.h | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h index d217c382b02d..4d87b36dbc8c 100644 --- a/include/linux/compiler-clang.h +++ b/include/linux/compiler-clang.h @@ -13,6 +13,12 @@ /* all clang versions usable with the kernel support KASAN ABI version 5 */ #define KASAN_ABI_VERSION 5 +/* + * Note: Checking __has_feature(*_sanitizer) is only true if the feature is + * enabled. Therefore it is not required to additionally check defined(CONFIG_*) + * to avoid adding redundant attributes in other configurations. + */ + #if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer) /* Emulate GCC's __SANITIZE_ADDRESS__ flag */ #define __SANITIZE_ADDRESS__ @@ -45,6 +51,17 @@ #define __no_sanitize_undefined #endif +/* + * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together + * with no_sanitize("coverage"). Prior versions of Clang support coverage + * instrumentation, but cannot be queried for support by the preprocessor. + */ +#if __has_feature(coverage_sanitizer) +#define __no_sanitize_coverage __attribute__((no_sanitize("coverage"))) +#else +#define __no_sanitize_coverage +#endif + /* * Not all versions of clang implement the type-generic versions * of the builtin overflow checkers. Fortunately, clang implements diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h index 48750243db4c..c6ab0dcb9c0c 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -126,6 +126,12 @@ #define __no_sanitize_undefined #endif +#if defined(CONFIG_KCOV) && __has_attribute(__no_sanitize_coverage__) +#define __no_sanitize_coverage __attribute__((no_sanitize_coverage)) +#else +#define __no_sanitize_coverage +#endif + #if GCC_VERSION >= 50100 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1 #endif diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index e5dd5a4ae946..652217da3b7c 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -210,7 +210,7 @@ struct ftrace_likely_data { /* Section for code which can't be instrumented at all */ #define noinstr \ noinline notrace __attribute((__section__(".noinstr.text"))) \ - __no_kcsan __no_sanitize_address + __no_kcsan __no_sanitize_address __no_sanitize_coverage #endif /* __KERNEL__ */ -- 2.30.2