linux内核禁止优化的设置

linux内核禁止优化的设置

环境介绍

内核版本:linux-4.15.2
编译器:gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf

关闭优化的原因

为了方便调试linux 内核,需要把内核的优化设置从默认的-O2调整为-Og。这里的优化点有很多,不仅仅是修改Makefile 把O2变成Og这么简单,还要修改内核源码。否则编译会报错。

关闭优化的相关操作

准确工作

  • 禁止内核优化,为了是调试时方便,先设置 开启内核调试信息
Kernel hacking  --->
     Compile-time checks and compiler options  ---> 
         [*] Compile the kernel with debug info
  • 设置Makefile 见Makefile 补充
--- raw/linux-4.15.2/Makefile	2018-02-07 11:14:15.000000000 -0800
+++ linux-4.15.2/Makefile	2022-09-15 02:03:35.769800577 -0700
@@ -634,6 +634,10 @@
 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	+= $(call cc-option, -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,)
@@ -644,6 +648,7 @@
 KBUILD_CFLAGS   += -O2
 endif
 endif
+endif
 
 KBUILD_CFLAGS += $(call cc-ifversion, -lt, 0409, \
 			$(call cc-disable-warning,maybe-uninitialized,))
@@ -751,6 +756,12 @@
 		   $(call cc-option,-fno-var-tracking)
 endif
 
+ifdef CONFIG_NO_AUTO_INLINE
+KBUILD_CFLAGS   += $(call cc-option, -fno-inline-functions) \
+		   $(call cc-option, -fno-inline-small-functions) \
+		   $(call cc-option, -fno-inline-functions-called-once)
+endif
+
 ifdef CONFIG_FUNCTION_TRACER
 ifndef CC_FLAGS_FTRACE
 CC_FLAGS_FTRACE := -pg
  • 设置 /include/linux/compiler-gcc.h
--- raw/linux-4.15.2/include/linux/compiler-gcc.h	2018-02-07 11:14:15.000000000 -0800
+++ linux-4.15.2/include/linux/compiler-gcc.h	2022-09-14 23:19:30.839359338 -0700
@@ -190,7 +190,8 @@
 
 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
 
-#ifndef __CHECKER__
+//#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)))
 #endif /* __CHECKER__ */
  • 设置/include/linux/compiler.h
--- raw/linux-4.15.2/include/linux/compiler.h	2018-02-07 11:14:15.000000000 -0800
+++ linux-4.15.2/include/linux/compiler.h	2022-09-14 23:21:12.646884095 -0700
@@ -286,7 +286,8 @@
  * 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__
+//# 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
  • 设置/init/Kconfig
--- raw/linux-4.15.2/init/Kconfig	2018-02-07 11:14:15.000000000 -0800
+++ linux-4.15.2/init/Kconfig	2022-09-14 23:32:16.816711523 -0700
@@ -1037,6 +1037,25 @@
 
 	  If unsure, say N.
 
+config CC_OPTIMIZE_FOR_DEBUGGING
+	bool "Optimize for better debugging experience (-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 SYSCTL
  • 设置/arch/arm/mm/mmu.c (解决编译报 mm: fix build error in fix_to_virt 错误的问题)
--- raw/linux-4.15.2/arch/arm/mm/mmu.c	2018-02-07 11:14:15.000000000 -0800
+++ linux-4.15.2//arch/arm/mm/mmu.c	2022-09-15 02:11:45.407671905 -0700
@@ -1599,7 +1599,7 @@
 		pte_t *pte;
 		struct map_desc map;
 
-		map.virtual = fix_to_virt(i);
+		map.virtual = __fix_to_virt(i);
 		pte = pte_offset_early_fixmap(pmd_off_k(map.virtual), map.virtual);
 
 		/* Only i/o device mappings are supported ATM */
  • 设置/lib/Kconfig.debug
--- raw/linux-4.15.2/lib/Kconfig.debug	2018-02-07 11:14:15.000000000 -0800
+++ linux-4.15.2/lib/Kconfig.debug	2022-09-15 02:06:38.596717691 -0700
@@ -198,6 +198,32 @@
 	  instance. See Documentation/dev-tools/gdb-kernel-debugging.rst
 	  for further details.
 
+config NO_AUTO_INLINE
+	bool "Disable compiler auto-inline optimizations"
+	help
+	  This will prevent the compiler from optimizing the kernel by
+	  auto-inlining functions not marked with the inline keyword.
+	  With this option, only functions explicitly marked with
+	  "inline" will be inlined. This will allow the function tracer
+	  to trace more functions because it only traces functions that
+	  the compiler has not inlined.
+
+	  Enabling this function can help debugging a kernel if using
+	  the function tracer. But it can also change how the kernel
+	  works, because inlining functions may change the timing,
+	  which could make it difficult while debugging race conditions.
+
+	  If unsure, select N.
+
+config ENABLE_WARN_DEPRECATED
+	bool "Enable __deprecated logic"
+	default y
+	help
+	  Enable the __deprecated logic in the kernel build.
+	  Disable this to suppress the "warning: 'foo' is deprecated
+	  (declared at kernel/power/somefile.c:1234)" messages.
+
+
 config ENABLE_WARN_DEPRECATED
 	bool "Enable __deprecated logic"
 	default y
  • 然后就是make menuconfig 的设置
    选择

Compiler optimization level

  (X) Optimize for better debugging experience (-Og) 

参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux内核参数优化是指通过调整Linux内核的配置参数,以提升系统性能、安全性和稳定性的过程。下面是一些常见的Linux内核参数优化方法: 1. 文件描述符限制:通过修改`/etc/security/limits.conf`文件中的`nofile`参数,增加系统允许的最大文件描述符数量。可以使用`ulimit -n`命令查看当前限制。 2. 网络参数优化:可以通过修改`/etc/sysctl.conf`文件来调整网络相关的内核参数。例如,增加TCP连接的最大数量、优化TCP拥塞控制算法、增加网络缓冲区大小等。 3. 内存管理:可以通过调整内核参数来优化内存管理,如修改`vm.swappiness`参数来控制交换空间的使用、调整`vm.dirty_ratio`和`vm.dirty_background_ratio`参数来控制脏页的写回行为等。 4. 文件系统参数:可以通过修改文件系统相关的内核参数来优化文件系统性能。例如,调整`fs.file-max`参数来增加系统允许的最大文件数量、调整`fs.inotify.max_user_watches`参数来增加inotify监视器的数量等。 5. CPU调度器:可以通过修改CPU调度器相关的内核参数来优化系统的CPU利用率和响应性能。例如,调整`/proc/sys/kernel/sched_min_granularity_ns`和`/proc/sys/kernel/sched_wakeup_granularity_ns`参数来优化调度器的精度。 6. 硬件参数:根据具体硬件配置,可以调整一些与硬件相关的内核参数,如网络接口的中断处理、磁盘调度算法等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值