no_profile fn attr and Kconfig for GCOV+PGO
Subsystems (7)
v2 cover no_profile fn attr and Kconfig for GCOV+PGO
The kernel has been using noinstr for correctness to politely request that the compiler avoid adding various forms of instrumentation to certain functions.
GCOV and PGO can both instrument functions, yet the function attribute to disable such instrumentation (no_profile_instrument_function) was not being used to suppress such implementation. Also, clang only just recently gained support for no_profile_instrument_function. GCC has supported that since 7.1+.
Add a new function annotation __no_profile that expands to __attribute__((__no_profile_instrument_function__)) and Kconfig values CC_HAS_NO_PROFILE_FN_ATTR and ARCH_WANTS_NO_INSTR. Make GCOV and PGO depend on either !ARCH_WANTS_NO_INSTR or CC_HAS_NO_PROFILE_FN_ATTR.
Changes V1 -> V2:
* s/no_profile/no_profile_instrument_function/
* fix trailing double underscore on GCC 4 define, as per Fangrui+Miguel.
* Pick up Fangrui + Miguel's reviewed-by tag.
* Add link to GCC's doc.
* Fix clang's doc format; will appear once clang-13 is released.
* New cleanup patch 2/3. Orthogonal to the series, but while I'm here...
Base is
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=for-next/clang/pgo.
Nick Desaulniers (3):
compiler_attributes.h: define __no_profile, add to noinstr compiler_attributes.h: cleanups for GCC 4.9+
Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on
for GCOV and PGO
arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + include/linux/compiler_attributes.h | 19 ++++++++++++++++--- include/linux/compiler_types.h | 2 +- init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 9 files changed, 33 insertions(+), 5 deletions(-)
base-commit: 4356bc4c0425c81e204f561acf4dd0095544a6cb
Signature · 1 line
2.32.0.288.g62a8d224e6-goog
PATCH v2 1/3 compiler_attributes.h: define __no_profile, add to noinstr
noinstr implies that we would like the compiler to avoid instrumenting a function. Add support for the compiler attribute no_profile_instrument_function to compiler_attributes.h, then add __no_profile to the definition of noinstr.
---
Changes V1 -> V2:
* s/no_profile/no_profile_instrument_function/
* fix trailing double underscore on GCC 4 define, as per Fangrui+Miguel.
* Pick up Fangrui + Miguel's reviewed-by tag.
* Add link to GCC's doc.
* Fix clang's doc format; will appear once clang-13 is released.
include/linux/compiler_attributes.h | 13 +++++++++++++ include/linux/compiler_types.h | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-)
include/linux/compiler_attributes.h +13 -0
@@ -33,6 +33,7 @@# 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)
@@ -237,6 +238,18 @@# 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
include/linux/compiler_types.h +1 -1
@@ -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__ */
Signature · 1 line
2.32.0.288.g62a8d224e6-goog
Re: [PATCH v2 1/3] compiler_attributes.h: define __no_profile, add to noinstr
Quoted 5 lines · Nick Desaulniers' via Clang Built Linux
noinstr implies that we would like the compiler to avoid instrumenting a function. Add support for the compiler attribute
no_profile_instrument_functiontocompiler_attributes.h, then add__no_profileto the definition of noinstr.
Quoted 9 lines
Reviewed-by Fangrui Song <[email protected]>Reviewed-by Miguel Ojeda <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
Quoted 57 lines
---
Changes V1 -> V2:
* s/no_profile/no_profile_instrument_function/
* fix trailing double underscore on GCC 4 define, as per Fangrui+Miguel.
* Pick up Fangrui + Miguel's reviewed-by tag.
* Add link to GCC's doc.
* Fix clang's doc format; will appear once clang-13 is released.include/linux/compiler_attributes.h | 13 +++++++++++++ include/linux/compiler_types.h | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-)include/linux/compiler_attributes.h +13
-0@@ -33,6 +33,7 @@# 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)@@ -237,6 +238,18 @@# 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 include/linux/compiler_types.h +1
-1@@ -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__ */
PATCH v2 2/3 compiler_attributes.h: cleanups for GCC 4.9+
In reply to Nick DesaulniersSince
commit 6ec4476ac825 ("Raise gcc version requirement to 4.9") we no longer support building the kernel with GCC 4.8; drop the preprocess checks for __GNUC_MINOR__ version. It's implied that if __GNUC_MAJOR__ is 4, then the only supported version of __GNUC_MINOR__ left is 9.
--- include/linux/compiler_attributes.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/compiler_attributes.h +3 -3
@@ -27,7 +27,7 @@*/ #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
@@ -35,8 +35,8 @@# 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
Signature · 1 line
2.32.0.288.g62a8d224e6-goog
Re: [PATCH v2 2/3] compiler_attributes.h: cleanups for GCC 4.9+
Quoted 7 lines · Nick Desaulniers
Since
commit6ec4476ac825("Raise gcc version requirement to 4.9") we no longer support building the kernel with GCC 4.8; drop the preprocess checks for__GNUC_MINOR__version. It's implied that if__GNUC_MAJOR__is 4, then the only supported version of__GNUC_MINOR__left is 9.
Yeah, I was waiting for the raise to 5.x to remove the entire block, but this is of course good since we did not get that yet :-)
Cheers,
Miguel
Re: [PATCH v2 2/3] compiler_attributes.h: cleanups for GCC 4.9+
In reply to Nick DesaulniersQuoted 9 lines · Nick Desaulniers' via Clang Built Linux
Since
commit6ec4476ac825("Raise gcc version requirement to 4.9") we no longer support building the kernel with GCC 4.8; drop the preprocess checks for__GNUC_MINOR__version. It's implied that if__GNUC_MAJOR__is 4, then the only supported version of__GNUC_MINOR__left is 9.Cc Miguel Ojeda <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
Quoted 29 lines
--- include/linux/compiler_attributes.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/linux/compiler_attributes.h +3
-3@@ -27,7 +27,7 @@*/ #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@@ -35,8 +35,8 @@# 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
PATCH v2 3/3 Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
In reply to Nick DesaulniersWe don't want compiler instrumentation to touch noinstr functions, which are annotated with the no_profile_instrument_function function attribute. Add a Kconfig test for this and make PGO and GCOV depend on it.
If an architecture is using noinstr, it should denote that via this Kconfig value. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.
---
Changes V1 -> V2:
* Add ARCH_WANTS_NO_INSTR
* Change depdendencies to be !ARCH_WANTS_NO_INSTR || CC_HAS_NO_PROFILE_FN_ATTR rather than list architectures explicitly, as per Nathan.
* s/no_profile/no_profile_instrument_function/
arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 7 files changed, 16 insertions(+), 1 deletion(-)
arch/Kconfig +7 -0
@@ -285,6 +285,13 @@ config ARCH_THREAD_STACK_ALLOCATOR config ARCH_WANTS_DYNAMIC_TASK_STRUCT bool +config ARCH_WANTS_NO_INSTR + bool + help + An architecure 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
arch/arm64/Kconfig +1 -0
@@ -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
arch/s390/Kconfig +1 -0
@@ -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
arch/x86/Kconfig +1 -0
@@ -114,6 +114,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
init/Kconfig +3 -0
@@ -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
kernel/gcov/Kconfig +1 -0
@@ -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
kernel/pgo/Kconfig +2 -1
@@ -8,7 +8,8 @@ config PGO_CLANG bool "Enable clang's PGO-based kernel profiling" depends on DEBUG_FS depends on ARCH_SUPPORTS_PGO_CLANG - depends on CC_IS_CLANG && CLANG_VERSION >= 120000 + depends on CC_IS_CLANG + depends on !ARCH_WANTS_NO_INSTR || CC_HAS_NO_PROFILE_FN_ATTR help This option enables clang's PGO (Profile Guided Optimization) based code profiling to better optimize the kernel.
Signature · 1 line
2.32.0.288.g62a8d224e6-goog
Re: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
Quoted 9 lines · Nick Desaulniers' via Clang Built Linux
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.
Quoted 7 lines
Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
Small nit below.
Quoted 21 lines
---
Changes V1 -> V2:
* AddARCH_WANTS_NO_INSTR
* Change depdendencies to be !ARCH_WANTS_NO_INSTR ||CC_HAS_NO_PROFILE_FN_ATTRrather than list architectures explicitly, as per Nathan.
* s/no_profile/no_profile_instrument_function/arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 7 files changed, 16 insertions(+), 1 deletion(-)arch/Kconfig +0
-0
arch/Kconfig config ARCH_THREAD_STACK_ALLOCATOR · line 291
Quoted 7 lines
arch/Kconfig +4
-0@@ -285,6 +285,13 @@ config ARCH_THREAD_STACK_ALLOCATOR config ARCH_WANTS_DYNAMIC_TASK_STRUCT bool +config ARCH_WANTS_NO_INSTR + bool + help + An architecure should select this if the noinstr macro is being used on
Architecture
Quoted 83 lines
arch/Kconfig +3
-0+ functions to denote that the toolchain should avoid instrumenting such + functions and is required for correctness. + config ARCH_32BIT_OFF_T bool depends on !64BITarch/arm64/Kconfig +1
-0@@ -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_TIMERarch/s390/Kconfig +1
-0@@ -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_SORTarch/x86/Kconfig +1
-0@@ -114,6 +114,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_64init/Kconfig +3
-0@@ -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 boolkernel/gcov/Kconfig +1
-0@@ -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 helpkernel/pgo/Kconfig +2
-1@@ -8,7 +8,8 @@ config PGO_CLANG bool "Enable clang's PGO-based kernel profiling" depends on DEBUG_FS depends on ARCH_SUPPORTS_PGO_CLANG - depends on CC_IS_CLANG && CLANG_VERSION >= 120000 + depends on CC_IS_CLANG + depends on !ARCH_WANTS_NO_INSTR || CC_HAS_NO_PROFILE_FN_ATTR help This option enables clang's PGO (Profile Guided Optimization) based code profiling to better optimize the kernel.
Re: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
In reply to Nick DesaulniersQuoted 40 lines · Nick Desaulniers
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>---
Changes V1 -> V2:
* AddARCH_WANTS_NO_INSTR
* Change depdendencies to be !ARCH_WANTS_NO_INSTR ||CC_HAS_NO_PROFILE_FN_ATTRrather than list architectures explicitly, as per Nathan.
* s/no_profile/no_profile_instrument_function/arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 7 files changed, 16 insertions(+), 1 deletion(-)arch/Kconfig +0
-0@@ -285,6 +285,13 @@ config ARCH_THREAD_STACK_ALLOCATOR config ARCH_WANTS_DYNAMIC_TASK_STRUCT bool
arch/Kconfig config ARCH_THREAD_STACK_ALLOCATOR · line 297
Quoted 10 lines
arch/Kconfig +7
-0+config ARCH_WANTS_NO_INSTR + bool + help + An architecure 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
There's also CC_HAS_WORKING_NOSANITIZE_ADDRESS in lib/Kconfig.kasan that might want to be hooked into this, but that can be done separately I suppose.
Re: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
Quoted 42 lines · Peter Zijlstra
Quoted 40 lines · Nick Desaulniers
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>---
Changes V1 -> V2:
* AddARCH_WANTS_NO_INSTR
* Change depdendencies to be !ARCH_WANTS_NO_INSTR ||CC_HAS_NO_PROFILE_FN_ATTRrather than list architectures explicitly, as per Nathan.
* s/no_profile/no_profile_instrument_function/arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 7 files changed, 16 insertions(+), 1 deletion(-)arch/Kconfig +0
-0@@ -285,6 +285,13 @@ config ARCH_THREAD_STACK_ALLOCATOR config ARCH_WANTS_DYNAMIC_TASK_STRUCT bool
Quoted 14 lines
arch/Kconfig config ARCH_THREAD_STACK_ALLOCATOR · line 297
Quoted 10 lines
arch/Kconfig +7
-0+config ARCH_WANTS_NO_INSTR + bool + help + An architecure 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 !64BITThere's also
CC_HAS_WORKING_NOSANITIZE_ADDRESSinlib/Kconfig.kasanthat might want to be hooked into this, but that can be done separately I suppose.
KASAN already depends on this for all compiler instrumentation modes, not just for 'noinstr' but also to avoid false positives. So it's not just for noinstr's benefit, and we should not weaken the requirement there.
Re: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
In reply to Nick DesaulniersQuoted 9 lines · Nick Desaulniers
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.
Quoted 7 lines
Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
FWIW, this looks good to me:
Catalin, Will, are you happy iwth the arm64 bit?
Thanks,
Makr.
Quoted 113 lines
---
Changes V1 -> V2:
* AddARCH_WANTS_NO_INSTR
* Change depdendencies to be !ARCH_WANTS_NO_INSTR ||CC_HAS_NO_PROFILE_FN_ATTRrather than list architectures explicitly, as per Nathan.
* s/no_profile/no_profile_instrument_function/arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 7 files changed, 16 insertions(+), 1 deletion(-)arch/Kconfig +7
-0@@ -285,6 +285,13 @@ config ARCH_THREAD_STACK_ALLOCATOR config ARCH_WANTS_DYNAMIC_TASK_STRUCT bool +config ARCH_WANTS_NO_INSTR + bool + help + An architecure 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 !64BITarch/arm64/Kconfig +1
-0@@ -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_TIMERarch/s390/Kconfig +1
-0@@ -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_SORTarch/x86/Kconfig +1
-0@@ -114,6 +114,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_64init/Kconfig +3
-0@@ -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 boolkernel/gcov/Kconfig +1
-0@@ -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 helpkernel/pgo/Kconfig +2
-1@@ -8,7 +8,8 @@ config PGO_CLANG bool "Enable clang's PGO-based kernel profiling" depends on DEBUG_FS depends on ARCH_SUPPORTS_PGO_CLANG - depends on CC_IS_CLANG && CLANG_VERSION >= 120000 + depends on CC_IS_CLANG + depends on !ARCH_WANTS_NO_INSTR || CC_HAS_NO_PROFILE_FN_ATTR help This option enables clang's PGO (Profile Guided Optimization) based code profiling to better optimize the kernel.Signature · 1 line
2.32.0.288.g62a8d224e6-goog
Will Deacon
maintainerRe: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
Quoted 18 lines · Mark Rutland
Quoted 9 lines · Nick Desaulniers
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.Quoted 7 lines
Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
Quoted 5 lines
FWIW, this looks good to me:
Acked-by Mark Rutland <[email protected]>Catalin, Will, are you happy iwth the arm64 bit?
Looks fine to me.
Will
Re: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
In reply to Nick DesaulniersQuoted 23 lines · Nick Desaulniers
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>---
Changes V1 -> V2:
* AddARCH_WANTS_NO_INSTR
* Change depdendencies to be !ARCH_WANTS_NO_INSTR ||CC_HAS_NO_PROFILE_FN_ATTRrather than list architectures explicitly, as per Nathan.
* s/no_profile/no_profile_instrument_function/
Quoted 8 lines
arch/Kconfig | 7 +++++++ arch/arm64/Kconfig | 1 + arch/s390/Kconfig | 1 + arch/x86/Kconfig | 1 + init/Kconfig | 3 +++ kernel/gcov/Kconfig | 1 + kernel/pgo/Kconfig | 3 ++- 7 files changed, 16 insertions(+), 1 deletion(-)
For s390:
Re: [PATCH v2 3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO
In reply to Nick DesaulniersQuoted 9 lines · Nick Desaulniers
We don't want compiler instrumentation to touch noinstr functions, which are annotated with the
no_profile_instrument_functionfunction attribute. Add aKconfigtest for this and make PGO and GCOV depend on it.If an architecture is using noinstr, it should denote that via this
Kconfigvalue. That makes Kconfigs that depend on noinstr able to express dependencies in an architecturally agnostic way.
Quoted 7 lines
Cc Masahiro Yamada <[email protected]>Cc Peter Oberparleiter <[email protected]>Suggested-by Nathan Chancellor <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>
Looks good to me - thanks for resolving this problem!
Regards,
Peter
Signature · 2 lines
Peter Oberparleiter Linux on Z Development - IBM Germany
Quoted 5 lines · Nick Desaulniers
Nick Desaulniers (3):
compiler_attributes.h: define__no_profile, add to noinstrcompiler_attributes.h: cleanups for GCC 4.9+
Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on
for GCOV and PGO
Thanks for sorting this Nick!
Quoted 4 lines · Nick Desaulniers
Add a new function annotation
__no_profilethat expands to __attribute__((__no_profile_instrument_function__)) andKconfigvaluesCC_HAS_NO_PROFILE_FN_ATTRandARCH_WANTS_NO_INSTR. Make GCOV and PGO depend on either !ARCH_WANTS_NO_INSTR orCC_HAS_NO_PROFILE_FN_ATTR.
Awesome; thanks everyone! I'm doing a Clang rebuild now, and will do kernel testing and push this to my for-next/clang/features tree shortly.
Signature · 1 line
Kees Cook
Kees Cook
AppliedQuoted 11 lines · Nick Desaulniers
The kernel has been using noinstr for correctness to politely request that the compiler avoid adding various forms of instrumentation to certain functions.
GCOV and PGO can both instrument functions, yet the function attribute to disable such instrumentation (
no_profile_instrument_function) was not being used to suppress such implementation. Also, clang only just recently gained support forno_profile_instrument_function. GCC has supported that since 7.1+.[...]
Applied to for-next/clang/features, thanks!
[1/3] compiler_attributes.h: define __no_profile, add to noinstr
https://git.kernel.org/kees/c/380d53c45ff2 [2/3] compiler_attributes.h: cleanups for GCC 4.9+ https://git.kernel.org/kees/c/ae4d682dfd33 [3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO https://git.kernel.org/kees/c/51c2ee6d121c
Note that I've tweaked the series slightly to move the PGO Kconfig change into the PGO patch.
Signature · 1 line
Kees Cook
Quoted 17 lines · Kees Cook
Quoted 11 lines · Nick Desaulniers
The kernel has been using noinstr for correctness to politely request that the compiler avoid adding various forms of instrumentation to certain functions.
GCOV and PGO can both instrument functions, yet the function attribute to disable such instrumentation (
no_profile_instrument_function) was not being used to suppress such implementation. Also, clang only just recently gained support forno_profile_instrument_function. GCC has supported that since 7.1+.[...]
Applied to for-next/clang/features, thanks!
[1/3]
compiler_attributes.h: define__no_profile, add to noinstr
Quoted 8 lines
https://git.kernel.org/kees/c/380d53c45ff2 [2/3] compiler_attributes.h: cleanups for GCC 4.9+ https://git.kernel.org/kees/c/ae4d682dfd33 [3/3] Kconfig: add ARCH_WANTS_NO_INSTR+CC_HAS_NO_PROFILE_FN_ATTR, depend on for GCOV and PGO https://git.kernel.org/kees/c/51c2ee6d121cNote that I've tweaked the series slightly to move the PGO
Kconfigchange into the PGO patch.
Ok, LGTM.
Signature · 2 lines
Thanks, ~Nick Desaulniers