Merge tag 'clang-features-v5.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel...
authorLinus Torvalds <torvalds@linux-foundation.org>
Wed, 30 Jun 2021 21:33:25 +0000 (14:33 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 30 Jun 2021 21:33:25 +0000 (14:33 -0700)
Pull clang feature updates from Kees Cook:

 - Add CC_HAS_NO_PROFILE_FN_ATTR in preparation for PGO support in the
   face of the noinstr attribute, paving the way for PGO and fixing
   GCOV. (Nick Desaulniers)

 - x86_64 LTO coverage is expanded to 32-bit x86. (Nathan Chancellor)

 - Small fixes to CFI. (Mark Rutland, Nathan Chancellor)

* tag 'clang-features-v5.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux:
  qemu_fw_cfg: Make fw_cfg_rev_attr a proper kobj_attribute
  Kconfig: Introduce ARCH_WANTS_NO_INSTR and CC_HAS_NO_PROFILE_FN_ATTR
  compiler_attributes.h: cleanups for GCC 4.9+
  compiler_attributes.h: define __no_profile, add to noinstr
  x86, lto: Enable Clang LTO for 32-bit as well
  CFI: Move function_nocfi() into compiler.h
  MAINTAINERS: Add Clang CFI section

14 files changed:
MAINTAINERS
arch/Kconfig
arch/arm64/Kconfig
arch/arm64/include/asm/compiler.h
arch/arm64/include/asm/memory.h
arch/s390/Kconfig
arch/x86/Kconfig
drivers/firmware/qemu_fw_cfg.c
include/linux/compiler.h
include/linux/compiler_attributes.h
include/linux/compiler_types.h
include/linux/mm.h
init/Kconfig
kernel/gcov/Kconfig

index 9999ae6..f4af84a 100644 (file)
@@ -4447,6 +4447,18 @@ F:       include/linux/compiler-clang.h
 F:     scripts/clang-tools/
 K:     \b(?i:clang|llvm)\b
 
+CLANG CONTROL FLOW INTEGRITY SUPPORT
+M:     Sami Tolvanen <samitolvanen@google.com>
+M:     Kees Cook <keescook@chromium.org>
+R:     Nathan Chancellor <nathan@kernel.org>
+R:     Nick Desaulniers <ndesaulniers@google.com>
+L:     clang-built-linux@googlegroups.com
+S:     Supported
+B:     https://github.com/ClangBuiltLinux/linux/issues
+T:     git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/clang/features
+F:     include/linux/cfi.h
+F:     kernel/cfi.c
+
 CLEANCACHE API
 M:     Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
 L:     linux-kernel@vger.kernel.org
index c45b770..129df49 100644 (file)
@@ -285,6 +285,13 @@ config ARCH_THREAD_STACK_ALLOCATOR
 config ARCH_WANTS_DYNAMIC_TASK_STRUCT
        bool
 
+config ARCH_WANTS_NO_INSTR
+       bool
+       help
+         An architecture should select this if the noinstr macro is being used on
+         functions to denote that the toolchain should avoid instrumenting such
+         functions and is required for correctness.
+
 config ARCH_32BIT_OFF_T
        bool
        depends on !64BIT
index a6a09cb..be90838 100644 (file)
@@ -93,6 +93,7 @@ config ARM64
        select ARCH_WANT_FRAME_POINTERS
        select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36)
        select ARCH_WANT_LD_ORPHAN_WARN
+       select ARCH_WANTS_NO_INSTR
        select ARCH_HAS_UBSAN_SANITIZE_ALL
        select ARM_AMBA
        select ARM_ARCH_TIMER
index 6fb2e6b..dc3ea40 100644 (file)
 #define __builtin_return_address(val)                                  \
        (void *)(ptrauth_clear_pac((unsigned long)__builtin_return_address(val)))
 
+#ifdef CONFIG_CFI_CLANG
+/*
+ * With CONFIG_CFI_CLANG, the compiler replaces function address
+ * references with the address of the function's CFI jump table
+ * entry. The function_nocfi macro always returns the address of the
+ * actual function instead.
+ */
+#define function_nocfi(x) ({                                           \
+       void *addr;                                                     \
+       asm("adrp %0, " __stringify(x) "\n\t"                           \
+           "add  %0, %0, :lo12:" __stringify(x)                        \
+           : "=r" (addr));                                             \
+       addr;                                                           \
+})
+#endif
+
 #endif /* __ASM_COMPILER_H */
index 7b36096..1a35a44 100644 (file)
@@ -321,22 +321,6 @@ static inline void *phys_to_virt(phys_addr_t x)
 #define virt_to_pfn(x)         __phys_to_pfn(__virt_to_phys((unsigned long)(x)))
 #define sym_to_pfn(x)          __phys_to_pfn(__pa_symbol(x))
 
-#ifdef CONFIG_CFI_CLANG
-/*
- * With CONFIG_CFI_CLANG, the compiler replaces function address
- * references with the address of the function's CFI jump table
- * entry. The function_nocfi macro always returns the address of the
- * actual function instead.
- */
-#define function_nocfi(x) ({                                           \
-       void *addr;                                                     \
-       asm("adrp %0, " __stringify(x) "\n\t"                           \
-           "add  %0, %0, :lo12:" __stringify(x)                        \
-           : "=r" (addr));                                             \
-       addr;                                                           \
-})
-#endif
-
 /*
  *  virt_to_page(x)    convert a _valid_ virtual address to struct page *
  *  virt_addr_valid(x) indicates whether a virtual address is valid
index 707afbc..a499716 100644 (file)
@@ -117,6 +117,7 @@ config S390
        select ARCH_USE_BUILTIN_BSWAP
        select ARCH_USE_CMPXCHG_LOCKREF
        select ARCH_WANTS_DYNAMIC_TASK_STRUCT
+       select ARCH_WANTS_NO_INSTR
        select ARCH_WANT_DEFAULT_BPF_JIT
        select ARCH_WANT_IPC_PARSE_VERSION
        select BUILDTIME_TABLE_SORT
index 49ffb69..867e793 100644 (file)
@@ -103,8 +103,8 @@ config X86
        select ARCH_SUPPORTS_DEBUG_PAGEALLOC
        select ARCH_SUPPORTS_NUMA_BALANCING     if X86_64
        select ARCH_SUPPORTS_KMAP_LOCAL_FORCE_MAP       if NR_CPUS <= 4096
-       select ARCH_SUPPORTS_LTO_CLANG          if X86_64
-       select ARCH_SUPPORTS_LTO_CLANG_THIN     if X86_64
+       select ARCH_SUPPORTS_LTO_CLANG
+       select ARCH_SUPPORTS_LTO_CLANG_THIN
        select ARCH_USE_BUILTIN_BSWAP
        select ARCH_USE_MEMTEST
        select ARCH_USE_QUEUED_RWLOCKS
@@ -113,6 +113,7 @@ config X86
        select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
        select ARCH_WANT_DEFAULT_BPF_JIT        if X86_64
        select ARCH_WANTS_DYNAMIC_TASK_STRUCT
+       select ARCH_WANTS_NO_INSTR
        select ARCH_WANT_HUGE_PMD_SHARE
        select ARCH_WANT_LD_ORPHAN_WARN
        select ARCH_WANTS_THP_SWAP              if X86_64
index 0078260..172c751 100644 (file)
@@ -299,15 +299,13 @@ static int fw_cfg_do_platform_probe(struct platform_device *pdev)
        return 0;
 }
 
-static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf)
+static ssize_t fw_cfg_showrev(struct kobject *k, struct kobj_attribute *a,
+                             char *buf)
 {
        return sprintf(buf, "%u\n", fw_cfg_rev);
 }
 
-static const struct {
-       struct attribute attr;
-       ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf);
-} fw_cfg_rev_attr = {
+static const struct kobj_attribute fw_cfg_rev_attr = {
        .attr = { .name = "rev", .mode = S_IRUSR },
        .show = fw_cfg_showrev,
 };
index 7704790..b67261a 100644 (file)
@@ -219,6 +219,16 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
        __v;                                                            \
 })
 
+/*
+ * With CONFIG_CFI_CLANG, the compiler replaces function addresses in
+ * instrumented C code with jump table addresses. Architectures that
+ * support CFI can define this macro to return the actual function address
+ * when needed.
+ */
+#ifndef function_nocfi
+#define function_nocfi(x) (x)
+#endif
+
 #endif /* __KERNEL__ */
 
 /*
index 183ddd5..2487be0 100644 (file)
  */
 #ifndef __has_attribute
 # define __has_attribute(x) __GCC4_has_attribute_##x
-# define __GCC4_has_attribute___assume_aligned__      (__GNUC_MINOR__ >= 9)
+# define __GCC4_has_attribute___assume_aligned__      1
 # define __GCC4_has_attribute___copy__                0
 # define __GCC4_has_attribute___designated_init__     0
 # define __GCC4_has_attribute___externally_visible__  1
 # define __GCC4_has_attribute___no_caller_saved_registers__ 0
 # define __GCC4_has_attribute___noclone__             1
+# define __GCC4_has_attribute___no_profile_instrument_function__ 0
 # define __GCC4_has_attribute___nonstring__           0
-# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
-# define __GCC4_has_attribute___no_sanitize_undefined__ (__GNUC_MINOR__ >= 9)
+# define __GCC4_has_attribute___no_sanitize_address__ 1
+# define __GCC4_has_attribute___no_sanitize_undefined__ 1
 # define __GCC4_has_attribute___fallthrough__         0
 #endif
 
 # define __nonstring
 #endif
 
+/*
+ * Optional: only supported since GCC >= 7.1, clang >= 13.0.
+ *
+ *      gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fprofile_005finstrument_005ffunction-function-attribute
+ *    clang: https://clang.llvm.org/docs/AttributeReference.html#no-profile-instrument-function
+ */
+#if __has_attribute(__no_profile_instrument_function__)
+# define __no_profile                  __attribute__((__no_profile_instrument_function__))
+#else
+# define __no_profile
+#endif
+
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
  * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
index d29bda7..d509169 100644 (file)
@@ -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_profile
 
 #endif /* __KERNEL__ */
 
index 6d0f827..7ec25dd 100644 (file)
@@ -124,16 +124,6 @@ extern int mmap_rnd_compat_bits __read_mostly;
 #define lm_alias(x)    __va(__pa_symbol(x))
 #endif
 
-/*
- * With CONFIG_CFI_CLANG, the compiler replaces function addresses in
- * instrumented C code with jump table addresses. Architectures that
- * support CFI can define this macro to return the actual function address
- * when needed.
- */
-#ifndef function_nocfi
-#define function_nocfi(x) (x)
-#endif
-
 /*
  * To prevent common memory management code establishing
  * a zero page mapping on a read fault.
index a61c920..55f9f77 100644 (file)
@@ -83,6 +83,9 @@ config TOOLS_SUPPORT_RELR
 config CC_HAS_ASM_INLINE
        def_bool $(success,echo 'void foo(void) { asm inline (""); }' | $(CC) -x c - -c -o /dev/null)
 
+config CC_HAS_NO_PROFILE_FN_ATTR
+       def_bool $(success,echo '__attribute__((no_profile_instrument_function)) int x();' | $(CC) -x c - -c -o /dev/null -Werror)
+
 config CONSTRUCTORS
        bool
 
index 58f87a3..0534471 100644 (file)
@@ -5,6 +5,7 @@ config GCOV_KERNEL
        bool "Enable gcov-based kernel profiling"
        depends on DEBUG_FS
        depends on !CC_IS_CLANG || CLANG_VERSION >= 110000
+       depends on !ARCH_WANTS_NO_INSTR || CC_HAS_NO_PROFILE_FN_ATTR
        select CONSTRUCTORS
        default n
        help