Kernel/Optimization

This article Linux kernel]].

Prerequisites
This article assumes is the symbolic link to the current kernel. Change directory to before continuing:

One way to optimize the kernel is to remove what users don't need. For example, if not using KVM, then remove CONFIG_KVM :

Kbuild
The Kernel build system can be used to change how Kernel builds in a more advanced way than, similar to GNU Make. Kbuild also support Environment Variables like LLVM=1. For example, the kernel will be build with LLVM and with aggressive optimization flags:

Clang/LLVM
Make sure the LLVM toolchain is installed before proceeding:

By default, the kernel is build under GNU binutils. The following environment variables are used: CC, LD, AR, NM, STRIP, OBJCOPY, OBJDUMP, READELF, HOSTCC, HOSTCXX, HOSTAR, and HOSTLD. But instead, the kernel may be build using LLVM binutils:

The same but more verbose command:

For more information, see this link.

*FLAGS
By default, most of the kernel is build with C's  (some code, like Random Number Generation, does not work with optimizations and sometimes checked with the C macro  ). This can be changed via Kbuild mention earlier.

This section will list what to do with some flags:

Performance
Performance means how fast the kernel runs.

GCC LTO
Enabling Link Time Optimization is not simple as. Andi Kleen and others has a experimental patch for this and will used to apply GCC LTO. For more information, see LWN article.

There are many ways to apply the LKML patch but only one will covered. To install, download https://raw.githubusercontent.com/CachyOS/kernel-patches/master/6.2/misc/gcc-lto/0001-gcc-LTO-support-for-the-kernel.patch, where 6.2 is the user's kernel version:

Then, git apply the patch and update the .config file:

To remove the patch:

Clang LTO
Clang's Link Time Optimization can be either FullLTO or ThinLTO for the Linux kernel:

The difference between these two are that ThinLTO compiles faster due to parallelization at the cost of performance.

GCC PGO
To use Profile-Guided Optimization, activate debugfs and gcov support (See this for modern info):

The environment variable CFLAGS_GCOV, used when CONFIG_GCOV_KERNEL is on, defaults to , but can be changed to   or similar in Instrumentation Options:

Then build as usual, setup the kernel and reboot the system using the command:

After booted back to system, run the system with many programs: play sound, game, run Firefox and so on. The longer the system is run and with more different programs, the higher instrumented data gets. When satisfied with the instrumented data, copy files to :

Then disable CONFIG_GCOV_KERNEL and CONFIG_GCOV_PROFILE_ALL and edit the KCFLAGS :

Like before, setup the kernel and finally reboot. To remove files, run the command:

Hardened
Hardening refers to reducing the potential for malware to damage the system:

Pietinger, Kicksecure, and Clip OS has more hardened config options to the kernel.

Size
This section describes reducing kernel memory usage (useful for embedded systems).

The kernel officially support  flag:

may also be instead use to more aggressively reduce size than :

External resources

 * Original Linux GCC PGO script part 1
 * Original Linux GCC PGO script part 2
 * Original Linux GCC PGO script part 3