Message ID | 20181028130945.23581-5-changbin.du@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | kernel hacking: GCC optimization for better debug experience (-Og) | expand |
On Sun, Oct 28, 2018 at 10:11 PM Changbin Du <changbin.du@gmail.com> wrote: > > This will apply GCC '-Og' optimization level which is supported > since GCC 4.8. This optimization level offers a reasonable level > of optimization while maintaining fast compilation and a good > debugging experience. It is similar to '-O1' while perferring > to keep debug ability over runtime speed. > > If enabling this option breaks your kernel, you should either > disable this or find a fix (mostly in the arch code). Currently > this option has only been tested on x86_64 and arm platform. > > This option can satisfy people who was searching for a method > to disable compiler optimizations so to achieve better kernel > debugging experience with kgdb or qemu. > > The main problem of '-Og' is we must not use __attribute__((error(msg))). > The compiler will report error though the call to error function > still can be optimize out. So we must fallback to array tricky. I removed the sentence "So we must fallback to array tricky." Commit 81b45683487a51b0f4d3b29d37f20d6d078544e4 killed the fallback to the negative array trick. I also resolved a conflict. Your series is now available in the following branch. git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild kbuild Please double check if I did it correctly. -- Best Regards Masahiro Yamada
On Mon, Oct 29, 2018 at 10:16:02PM +0900, Masahiro Yamada wrote: > On Sun, Oct 28, 2018 at 10:11 PM Changbin Du <changbin.du@gmail.com> wrote: > > > > This will apply GCC '-Og' optimization level which is supported > > since GCC 4.8. This optimization level offers a reasonable level > > of optimization while maintaining fast compilation and a good > > debugging experience. It is similar to '-O1' while perferring > > to keep debug ability over runtime speed. > > > > If enabling this option breaks your kernel, you should either > > disable this or find a fix (mostly in the arch code). Currently > > this option has only been tested on x86_64 and arm platform. > > > > This option can satisfy people who was searching for a method > > to disable compiler optimizations so to achieve better kernel > > debugging experience with kgdb or qemu. > > > > The main problem of '-Og' is we must not use __attribute__((error(msg))). > > The compiler will report error though the call to error function > > still can be optimize out. So we must fallback to array tricky. > > > I removed the sentence "So we must fallback to array tricky." > > Commit 81b45683487a51b0f4d3b29d37f20d6d078544e4 > killed the fallback to the negative array trick. > > > I also resolved a conflict. > > Your series is now available in the following branch. > > git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild kbuild > > > Please double check if I did it correctly. > I have tested your kbuild branch and no issues found. Thanks for checking this series! > > -- > Best Regards > Masahiro Yamada
diff --git a/Makefile b/Makefile index 04beb822ddfc..10de245a3325 100644 --- a/Makefile +++ b/Makefile @@ -657,6 +657,10 @@ KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation) KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow) KBUILD_CFLAGS += $(call cc-disable-warning, int-in-bool-context) +ifdef CONFIG_CC_OPTIMIZE_FOR_DEBUGGING +KBUILD_CFLAGS += -Og +KBUILD_CFLAGS += $(call cc-disable-warning,maybe-uninitialized,) +else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE KBUILD_CFLAGS += $(call cc-option,-Oz,-Os) KBUILD_CFLAGS += $(call cc-disable-warning,maybe-uninitialized,) @@ -667,6 +671,7 @@ else KBUILD_CFLAGS += -O2 endif endif +endif KBUILD_CFLAGS += $(call cc-ifversion, -lt, 0409, \ $(call cc-disable-warning,maybe-uninitialized,)) diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h index 90ddfefb6c2b..4832c98c7885 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -85,7 +85,7 @@ #define __compiletime_object_size(obj) __builtin_object_size(obj, 0) -#ifndef __CHECKER__ +#if !defined(__CHECKER__) && !defined(CONFIG_CC_OPTIMIZE_FOR_DEBUGGING) #define __compiletime_warning(message) __attribute__((warning(message))) #define __compiletime_error(message) __attribute__((error(message))) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 1921545c6351..3836397bf477 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -350,7 +350,7 @@ static inline void *offset_to_ptr(const int *off) * sparse see a constant array size without breaking compiletime_assert on old * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether. */ -# ifndef __CHECKER__ +# if !defined(__CHECKER__) && !defined(CONFIG_CC_OPTIMIZE_FOR_DEBUGGING) # define __compiletime_error_fallback(condition) \ do { ((void)sizeof(char[1 - 2 * condition])); } while (0) # endif diff --git a/init/Kconfig b/init/Kconfig index a4112e95724a..0fb9c0b5f1a1 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1105,6 +1105,26 @@ config CC_OPTIMIZE_FOR_SIZE If unsure, say N. +config CC_OPTIMIZE_FOR_DEBUGGING + bool "Optimize for better debugging experience (-Og)" + depends on $(cc-option,-Og) + select NO_AUTO_INLINE + help + This will apply GCC '-Og' optimization level which is supported + since GCC 4.8. This optimization level offers a reasonable level + of optimization while maintaining fast compilation and a good + debugging experience. It is similar to '-O1' while preferring to + keep debug ability over runtime speed. The overall performance + will drop a bit (~6%). + + Use only if you want to debug the kernel, especially if you want + to have better kernel debugging experience with gdb facilities + like kgdb or qemu. If enabling this option breaks your kernel, + you should either disable this or find a fix (mostly in the arch + code). + + If unsure, select N. + endchoice config HAVE_LD_DEAD_CODE_DATA_ELIMINATION diff --git a/kernel/configs/tiny.config b/kernel/configs/tiny.config index 7fa0c4ae6394..599ea86b0800 100644 --- a/kernel/configs/tiny.config +++ b/kernel/configs/tiny.config @@ -1,5 +1,6 @@ # CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set CONFIG_CC_OPTIMIZE_FOR_SIZE=y +# CONFIG_CC_OPTIMIZE_FOR_DEBUGGING is not set # CONFIG_KERNEL_GZIP is not set # CONFIG_KERNEL_BZIP2 is not set # CONFIG_KERNEL_LZMA is not set