Logo Matteo Salonia
Custom Kernel

If you use a GNU/Linux distro, and are capable of compiling your own Kernel, on this page you'll find how I modified my Kernel, and a script to facilitate Kernel configuration, and compilation.

Go to:
Introduction Default config Script usage Performance Source code

I'll start off by saying that I use the Gentoo Linux arrow_forward distro — it's a GNU/Linux distro where all packages (programs) are compiled from source, rather than simply install pre-built/pre-packages programs, that cannot be configured. This allows the user to choose which components of his system to compile, and which specific packages to install, in order to allow configurations that use not too many resources (even less than 100MB RAM usage!), totally functioning and very efficient. In fact, as you may have well understood, it's also possible to configure your own Kernel, and compile it later. On my laptop, ThinkPad T440p, I use the pf-sources arrow_forward Kernel, which includes Gentoo Kernel patches (genpatches), and many other patches which allow the Kernel to run way faster.

This Kernel is updated regularly, with continuous support for my laptop, ThinkPad T440p, removing features that aren't used nor necessary, like NUMA, NVIDIA, AMD, and so on. In this guide you'll see that you have to modify some options, like the directory that contains the Kernel source, etc. Also, if you use setups different from mine, you'll have to modify the Kernel config, especially the max number of CPUs (set to 8), max GPUs (set to 2), etc.

Make sure you cloned the repository:

git clone "https://github.com/saloniamatteo/kernel" usr-kernel
cd usr-kernel

Select a Kernel version to compile, based on the available versions. For example, I'll compile the 5.19_p1-pf Kernel:

cd 5.19_p1-pf

Even if you don't have the exact version, you can modify the script to includere the version you want; for this example, I'll modify the file with the vim text editor, but you can use any editor you want:

vim build.sh

To modify the version, take a look at the following variables:

KVER="5.19"
PVER="_p1-pf"

KVER contains the Kernel's primary version (5.19), meanwhile PVER contains the patch version (_p1-pf). Together they make KERNVER, the Kernel's complete version (5.19_p1-pf). For example, if your custom version has the same primary version as my Kernel's (5.19), but a different patch version, for example _mykernel, then you have to set the variables like this:

KVER="5.19"
PVER="_mykernel"

In the script, build.sh, there's a variable, ARCHVER. This variable contains a number from 1 to 41; each number represents a different processor family. In the same file, there's a list that explains which number to use, depending on the processor's family.

In my config, this variable is set as follows:

ARCHVER=24

ARCHVER is set to 24 because 24 represents the Intel CPU family Haswell.

# Note: ARCHVER is the sub-architecture number,
# which will be used to apply optimizations for your specific CPU.
# Down below is a list of numbers and their corresponding CPU.
[ ... ]
# 24. Intel Haswell (MHASWELL)

I recommend you change this number, because the performance impact is notable. Here's the full list:

# 1. AMD Opteron/Athlon64/Hammer/K8 (MK8)
# 2. AMD Opteron/Athlon64/Hammer/K8 with SSE3 (MK8SSE3)
# 3. AMD 61xx/7x50/PhenomX3/X4/II/K10 (MK10)
# 4. AMD Barcelona (MBARCELONA)
# 5. AMD Bobcat (MBOBCAT)
# 6. AMD Jaguar (MJAGUAR)
# 7. AMD Bulldozer (MBULLDOZER)
# 8. AMD Piledriver (MPILEDRIVER)
# 9. AMD Steamroller (MSTEAMROLLER)
# 10. AMD Excavator (MEXCAVATOR)
# 11. AMD Zen (MZEN)
# 12. AMD Zen 2 (MZEN2)
# 13. AMD Zen 3 (MZEN3)
# 14. Intel P4 / older Netburst based Xeon (MPSC)
# 15. Intel Core 2 (MCORE2)
# 16. Intel Atom (MATOM)
# 17. Intel Nehalem (MNEHALEM)
# 18. Intel Westmere (MWESTMERE)
# 19. Intel Silvermont (MSILVERMONT)
# 20. Intel Goldmont (MGOLDMONT)
# 21. Intel Goldmont Plus (MGOLDMONTPLUS)
# 22. Intel Sandy Bridge (MSANDYBRIDGE)
# 23. Intel Ivy Bridge (MIVYBRIDGE)
# 24. Intel Haswell (MHASWELL)
# 25. Intel Broadwell (MBROADWELL)
# 26. Intel Skylake (MSKYLAKE)
# 27. Intel Skylake X (MSKYLAKEX)
# 28. Intel Cannon Lake (MCANNONLAKE)
# 29. Intel Ice Lake (MICELAKE)
# 30. Intel Cascade Lake (MCASCADELAKE)
# 31. Intel Cooper Lake (MCOOPERLAKE)
# 32. Intel Tiger Lake (MTIGERLAKE)
# 33. Intel Sapphire Rapids (MSAPPHIRERAPIDS)
# 34. Intel Rocket Lake (MROCKETLAKE)
# 35. Intel Alder Lake (MALDERLAKE)
# 36. Generic-x86-64 (GENERIC_CPU)
# 37. Generic-x86-64-v2 (GENERIC_CPU2)
# 38. Generic-x86-64-v3 (GENERIC_CPU3)
# 39. Generic-x86-64-v4 (GENERIC_CPU4)
# 40. Intel-Native optimizations autodetected by GCC (MNATIVE_INTEL)
# 41. AMD-Native optimizations autodetected by GCC (MNATIVE_AMD)

It's good practice to check the Kernel config before compiling it. In order to do so, the build.sh script allows you to modify the config, executing make menuconfig inside the Kernel directory, with the option to copy the included config, or to use your own.

Make sure you have administator rights before executing this command; if you are not admins, put sudo or doas before the command, according to whichever you use.

dangerous If you haven't undertood what's written above, stop immediately the Kernel configuration, to avoid making your system unusable. Even if the risk isn't high, it's better to prevent further problems. Continue at your own risk.

warning Make sure to save/copy the .config file after configuration!

# Open the configuration menu, copying the included config
./build.sh -m

# Open the configuration menu, without copying the included config ./build.sh -c -m

After having applied CPU Optimisations and having re-checked the Kernel config, it's time to choose the settings to notably improve performance. Here's the options list:

flag Short flag

Long flag

Description

-e
--ccache
Use ccache to speed-up compilation. It's recommended only when compiling the same Kernel more than two times, to speed-up further compilations. (See article)
-f
--fastmath
Compile Kernel using Unsafe Fast Math. Tipically, it does not have negative effects, and performance is better compared to Safe Fast Math.
-F
--fastmath-s
Compile Kernel using Safe Fast Math. Use this is Unsafe Fast Math gives you problems, on rare occasions.
-g
--graphite
Compile Kernel with Graphite. Tipically this option reduces performance.
-l
--clearl-ps
Compile Kernel applying Clear Linux patches. These patches are catered towards further increasing performance, and are applied directly from the Clear Linux repository. Highly recommended for Intel CPUs.
-p
--patches
Compile Kernel applying the included patches. These patches too are catered towards further increasing performance — most of these patches comes from LKML.

After having chosen which flags to apply, and after having chosen whether to copy the included .config file with the flag "-c", you're ready to compile the Kernel.

# Example 1: compile Kernel, copying the included config,
# applying Clear Linux patches and included patches,
# increasing performance with the flags mentioned above
./build.sh -f -l -p

# Example 2: compile Kernel, copying the included config
# using ccache to make compilation faster,
# applying Clear Linux patches and included patches,
# increasing performance with the flags mentioned above
./build.sh -e -f -l -p

# Example 3: compile Kernel, without copying the included config
# because the custom file is already in /usr/src/linux/.config,
# applying Clear Linux patches and included patches,
# increasing performance with the flags mentioned above
./build.sh -c -f -l -p

To establish which Kernel is the fastest, I ran 4 compilations with different flags, using the same Kernel configuration. These tests were ran on my laptop, ThinkPad T440p, with CPU Intel Core i7-4700MQ, 8GB RAM, Kernel Gentoo Linux 5.16.0-pf2. To find out which Kernel is the most performant (aka the fastest), I used every single Kernel with the XMRig program, running it for 5 minutes maximum, and looking at the max hashrate. Here's a list of the tested Kernels, from the most performing to the least:

settings Settings

schedule Compile time

Max H/s

Unsafe Fast Math, Clear Linux patches, Included patches

7m, 7s

1715

Unsafe Fast Math, Graphite, Clear Linux patches, Included patches

7m, 3s

1500

Clear Linux patches, Included patches

7m

1463

Included patches

7m

1460