Comment 32 for bug 1926938

Revision history for this message
DanglingPointer (ferncasado) wrote :

@eitch

Try Dougss steps without the aggressive optimisations first so you get the hang of it. Make sure it works and your hardware all works.

My steps are a little different to Doug's and perhaps a little more complicated. But once you get the hang of it, it is repeatable and can be scripted. It also means you'll have optimised kernel for your hardware.
In a high level outline these are my steps.
Prerequisite is install gcc-11 and the dwarves version from Ubuntu archives.
test...
$ cc --version; gcov --vresion
You shoudl see respectively these:
cc (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0
gcov (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0

1) wget the kernel you want from kernel.org
This is more efficient cleaner then linus's git since it is a single file download and there's no git metadata overhead nor hidden folder and files.
Ensure it is downloaded to a directory location where there are no "deb" files.

2) To play safe with hardware compatibility we will leverage Ubuntu curated compiled kernel configuration by copying it to our custom build.
I do this by temporarily installing the Ubuntu compiled kernel using Mainline CLI - https://ubuntuhandbook.org/index.php/2020/08/mainline-install-latest-kernel-ubuntu-linux-mint/
Like I said, the kernel won't work, we just want the configuration they spent a lot of time curating for that exact version.

3) Uncompress the kernel you downloaded and change into the new directory created

4) Copy the config from /boot/ into the new kernel directory from step 3 but name the file ".config"

5) Uninstall the Ubuntu compiled kernel you installed. I use Mainline CLI

6) edit the ".config" file and look for "CONFIG_SYSTEM_TRUSTED_KEYS". Delete the string between the quotes. It should look like CONFIG_SYSTEM_TRUSTED_KEYS=""
We need to do this because we are doing a custom kernel. Otherwise build may fail as this could conflict.

7) I also optionally use xz compression for my kernel instead of the default LZ4. There are other options.
To change look for "CONFIG_KERNEL_LZ4=y" and comment it out then uncomment
Then uncomment "# CONFIG_KERNEL_XZ is not set" and change to "CONFIG_KERNEL_XZ=y"
Like I said this is optional. Only needed really if your /boot/ is a separate small partition with limited space.

8) Remember to save in steps 6 and 7 above

9) edit the MakeFile
I use nano for this
copy and replace every single "O2" with "O3"
This will ensure no gcc conflicts if by chance both are quoted in a compile command as option parameters.
Don't forget to save

10) export KCFLAGS="-march=native -mtune=native -O3 -pipe" KCPPFLAGS="-march=native -mtune=native -O3 -pipe"
To ensure we get to use the aggressive optimisations, we'll quote "-O3" again here just to be doubly sure!
-march=native and -mtune=native are pretty self explanatory. This is where we're basically saying... build this kernel for this CPU specifically and tune it for this CPU.
-pipe basically means '|' the output to the next thing that needs it instead of writing files. This will use more RAM which is faster than disk IO.
(special note on LTO using -flto; this is still not supported in the latest 5.12.x kernels with gcc-11. However I have seen people say they've gotten simple setups to work with -flto but using clang-12 instead of gcc. I haven't tried with clang-12 but it definitely fails with gcc-11 which I have tried. I look forward to when it works!)

11) $ make oldconfig

12) $ make clean

13) $ time make -j $(( $(nproc) + 1 )) deb-pkg LOCALVERSION=-eitch-uber-optimisation
This will compile the kernel using all your cores+1.
It will take a while depending on what CPU you have, be patient, have htop or gnome-system-monitor running so you can tell there's activity. "time" command infront will provide you a benchmark for your build when finished.
You can use gnome-system-monitor "Processes" tab and filter on 'cc' to see a compiler command line. Highlight one with your mouse and check the very long commandline that it has no "-O2" and is all "-O3" multiple are fine as long as the last one is "-O3". Also if you know what you're looking for, search for your architecture options which should have been auto-figured out using -march=native.
You will end up with multiple files in the parent folder of the folder you're in (i.e. ../.). Go up one level into that folder.
Assuming you followed step 1 above and there were no deb files where you downloaded the kernel with wget && you followed step 5 and uninstalled the generic one from Ubuntu; THEN you can now install the brand spanking new native-optimised kernel... $ sudo dpkg -i ./*.deb

14) Enjoy... if something feels off, you can always go back to the previous through grub on restart.

15) You can uninstall using Mainline CLI when replacing to newer or rolling back or making space in /boot.
$ mainline --list
$ sudo mainline --remove 5.x.y.z
there's a 'z' because ubuntu's one has no 'z' so it auto distinguishes it from official generic ones by adding 'z'.