How to compile a kernel for Ryzen processors

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

How to compile a kernel for Ryzen processors

Post by catweazel »

Warning
Newcomers to linux are warned that this procedure may b0rk proprietary drivers and some third-party non-free software. Before proceeding to build your own kernel you should be proficient in searching google for solutions. If you b0rk your machine with this procedure then you're basically on your own because you've installed an unsupported kernel. You have been warned. If you have a spare hard disk, you are urged to perform a full backup before proceeding.

I use the NVIDIA proprietary driver and don't need to make any adjustments for it in the steps below. I cannot tell you if the same applies to the AMD proprietary driver.

Important
These instructions are for Linux Mint 18.2. They should work on other editions but if you're not using 18.2 then you'll need to make any adjustments without my help.

Introduction
For high-end machines it is highly recommended to install a low-latency kernel. A low-latency kernel is fractionally (imperceptibly) slower than a generic kernel, but it offers fantastic support for high-end multi-core CPUs and load sharing. The difference a low-latency kernel makes is very much noticeable when playing videos, doing video transcoding and performing other CPU intensive tasks. Additionally Mint 18.2 uses gcc version 5.4, which is capable of implementing CPU optimisations for Ryzen processors. Unfortunately these optimisations are not enabled by default in the kernel, so we must build our own kernel to activate them.

The reason for compiling a Ryzen-specific kernel is to deal with (for example)
certain bugs with the Ryzen processor, which, funnily enough, aren't found in the newer ThreadRipper series, and to obtain the benefits of CPU optimisations specific to the Ryzen CPU. The goal is increased stability.

Prerequisites
  • DO NOT overclock. Compiling while overclocking triggers one of the Ryzen bugs this
    procedure is intended to defeat. If you find that all userland applications, keyboard,
    mouse and desktop environment suddenly switch off but the computer remains on,
    congratulations, you've triggered the bug.
  • Pack a lunch box and a flagon of ice water. One of the steps takes quite some time. The
    download size is over 1.1GB so be aware of that if you're on a metered connection.
  • In case you do b0rk your machine, be aware that you can enter GrUB and select a
    previous kernel. Usually you hold down the left shift key during boot if you don't
    normally see the GrUB menu.
  • In case you do b0rk your machine and can't recover using a previous kernel, make sure
    you have access to a spare device that can access the internet so you can search
    Go-ogle[1] for solutions, but it shouldn't come to this.
  • If you've had to do anything to your system to get it running properly, make sure that
    you have sufficient notes to recreate the steps. For example, you may have to reinstall
    wifi drivers.
[1] ogle : verb : to stare at in a lecherous manner.
First, if one is using select proprietary or out-of-tree modules (ex. vitualbox, nvidia, fglrx, bcmwl, etc.) unless there is an extra package available for the version you are testing, you will need to uninstall the module first, in order to test the mainline kernel. If you do not uninstall these modules first, then the upstream kernel more than likely will not boot.
Source: Ubuntu Mainline builds wiki

The kernel will boot, but you may have to reinstall "out-of-tree modules", possibly including wifi drivers, from a terminal. Use Ctrl-Alt-F1 if X won't start and issue the appropriate commands to reinstall your video drivers. Alternatively you can boot the new kernel in recovery mode from the GrUB menu. Virtualbox should be ok because newer versions no longer rely on dkms.

Step 1: Install development tools
Start a terminal and stay in your home directory. If you prefer to build your kernel in /usr/src then make the necessary adjustments, otherwise just issue these commands:

Code: Select all

sudo apt install git build-essential kernel-package fakeroot libncurses5-dev libssl-dev ccache -y
Next, verify that your gcc version is 4.9 or greater:

Code: Select all

gcc --version
Next, issue these commands:

Code: Select all

mkdir build_kernel
cd build_kernel
Step 2: Get the kernel sources and patch the kernel
This next step is where you'll need your packed lunch and flagon of ice water. I have a T5 connection so the download is reasonably quick but it won't be so for many people.

Code: Select all

git clone -b linux-4.13.y git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
Next, cd into the kernel source directory:

Code: Select all

cd linux-stable/
Start a browser and go to this link. On the right side of the screen you will see a green button. Click it and select "Download ZIP". Use your file manager to extract only the file named "optimizations_for_gcc_v4.9+_kernel_v4.13+.patch". Once it's extracted, copy it to ~/build_kernel/linux-stable/

You may want to make a backup copy of the sources before you proceed to the next step just in case something goes wrong. I always do this and I recommend that you do also. You can use your file manager to copy/paste the build_kernel directory to a safe place. If something goes wrong, you can just delete the botched sources directories and replace them with copy/paste. Then you can start afresh.

Go back into the terminal and issue this command to apply the patch:

Code: Select all

patch -p1 < enable_additional_cpu_optimizations_for_gcc_v4.9+_kernel_v4.13+.patch
You should see output like this:

Code: Select all

catweazel@Fenella ~/build_kernel/linux-stable $ patch -p1 < enable_additional_cpu_optimizations_for_gcc_v4.9+_kernel_v4.13+.patch
patching file arch/x86/include/asm/module.h
patching file arch/x86/Kconfig.cpu
patching file arch/x86/Makefile
patching file arch/x86/Makefile_32.cpu
Congratulations! You just patched the kernel. This particular patch activates new options in gcc for a whole host of different CPUs.

Step 3: Prepare to configure the kernel
In the terminal, issue these commands to copy your current kernel config and rebiuld the copied config for the patch you applied earlier:

Code: Select all

cp /boot/config-`uname -r` .config
yes '' | make oldconfig
You should see output similar to this:

Code: Select all

  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
[...]
Kernel Live Patching (LIVEPATCH) [Y/n/?] y
#
# configuration written to .config
#
Next, edit the kernel config file manually to turn off building debug symbols. Without this change, your kernel build will take a very long time indeed:

Code: Select all

nano -w .config
Type ctrl-w to search. Enter CONFIG_DEBUG_INFO as the search term. You should be taken to a line that reads:

Code: Select all

# CONFIG_DEBUG_INFO is not set
Change that line to read CONFIG_DEBUG_INFO=n <--- You are replacing the entire line, including the #.

Type ctrl-o and press enter to save changes, and ctrl-x to exit.

Finally for this step, build the kernel make scripts with this command:

Code: Select all

make scripts
Step 4: Configure the kernel
Issue this command:

Code: Select all

make menuconfig
You should get an ncurses screen and a list of kernel options. Navigate through the options and set them as shown here:

Code: Select all

Processor type and features  --->
  [*] Symmetric multi-processing support
  [*] AMD ACPI2Platform devices support
  Processor family (MZEN)  --->
    (X) AMD Zen
  [*] Supported processor vendors  --->
    [*]   Support AMD processors
  [*] SMT (Hyperthreading) scheduler support
  [*] Multi-core scheduler support
      Preemption Model (Preemptible Kernel (Low-Latency Desktop))  --->
  [*] Machine Check / overheating reporting
  [*]   AMD MCE features
  Performance monitoring  --->
    <*> AMD Processor Power Reporting Mechanism
  [*]   AMD microcode loading support
Power management and ACPI options  --->
  CPU Frequency scaling  --->
    <*>   AMD Opteron/Athlon64 PowerNow!
    <*>   AMD frequency sensitivity feedback powersave bias
Device Drivers  --->
  [*] IOMMU Hardware Support  --->
    [*]   AMD IOMMU support
    <*>     AMD IOMMU Version 2 driver
Use the up and down arrow keys to navigate the menu. Press enter to select an option sub-menu (--->). Use the left and right arrow keys to select the save and exit options. Use the space key to mark a module <*>. Note that when you go down one level (--->) in the menu system, you use the Exit button to go back up. At the top level, Exit will exit menuconfig.

When performing this step, don't be tempted to switch off other Intel options. Since you're not using an Intel processor, these items will never get executed in the kernel so they just take up space. If you know what you're doing then you can disable whatever you like, but if you've got little or no experience with the kernel, leave the Intel options alone.

When all the settings above have been set, save your changes and exit.

Step 5: Compile the kernel
Important
It is not necessary to issue the make clean command the first time you build your kernel but you should do it anyway. Note that you will need to issue make clean before you recompile if your compile fails. Alternatively if you made a backup as described above, delete the old source directories and just copy/paste your backup for a fresh start.

Important
The make command below uses NN to represent the number of jobs or threads. You should replace NN with the number of threads supported by your Ryzen CPU. In my case it's 16 so on my machine I use make -j 16 deb-pkg LOCALVERSION=-ryzen. Don't forget, replace NN with the number of threads.

Code: Select all

make clean
make -j NN deb-pkg LOCALVERSION=-ryzen
You should see reams and reams of text flying by. At some point, it should end with:

Code: Select all

  INSTALL debian/headertmp/usr/include/linux/usb/ (11 files)
  INSTALL debian/headertmp/usr/include/linux/wimax/ (1 file)
  INSTALL debian/headertmp/usr/include/asm/ (62 files)
dpkg-deb: building package 'linux-firmware-image-4.13.8-ryzen' in '../linux-firmware-image-4.13.8-ryzen_4.13.8-ryzen-1_amd64.deb'.
dpkg-deb: building package 'linux-headers-4.13.8-ryzen' in '../linux-headers-4.13.8-ryzen_4.13.8-ryzen-1_amd64.deb'.
dpkg-deb: building package 'linux-libc-dev' in '../linux-libc-dev_4.13.8-ryzen-1_amd64.deb'.
dpkg-deb: building package 'linux-image-4.13.8-ryzen' in '../linux-image-4.13.8-ryzen_4.13.8-ryzen-1_amd64.deb'.
dpkg-source: info: using source format '3.0 (custom)'
dpkg-source: info: building linux-4.13.8-ryzen in linux-4.13.8-ryzen_4.13.8-ryzen-1.dsc
dpkg-source: warning: missing information for output field Standards-Version
dpkg-genchanges: including full source code in upload
Output like the above indicates a successful compilation and the creation of four .deb files.

Step 6: Install the kernel
Go up one level in the terminal to ~/build_kernel using the cd command and list the files:

Code: Select all

cd ..
ls -l
You should see your shiny new .deb files:

Code: Select all

-rw-rw-r--  1 Fenella Fenella      3059 Oct 21 10:17 linux-4.13.8-ryzen_4.13.8-ryzen-1_amd64.changes
-rw-rw-r--  1 Fenella Fenella      1246 Oct 21 10:17 linux-4.13.8-ryzen_4.13.8-ryzen-1.debian.tar.gz
-rw-rw-r--  1 Fenella Fenella      1187 Oct 21 10:17 linux-4.13.8-ryzen_4.13.8-ryzen-1.dsc
-rw-rw-r--  1 Fenella Fenella 161366410 Oct 21 10:05 linux-4.13.8-ryzen_4.13.8-ryzen.orig.tar.gz
-rw-r--r--  1 Fenella Fenella    963984 Oct 21 10:14 linux-firmware-image-4.13.8-ryzen_4.13.8-ryzen-1_amd64.deb
-rw-r--r--  1 Fenella Fenella  10839168 Oct 21 10:15 linux-headers-4.13.8-ryzen_4.13.8-ryzen-1_amd64.deb
-rw-r--r--  1 Fenella Fenella  50864894 Oct 21 10:17 linux-image-4.13.8-ryzen_4.13.8-ryzen-1_amd64.deb
-rw-r--r--  1 Fenella Fenella    949990 Oct 21 10:15 linux-libc-dev_4.13.8-ryzen-1_amd64.deb
drwxrwxr-x 27 Fenella Fenella      4096 Oct 21 10:17 linux-stable
Side note: If you're wondering about Fenella, see this link.

Next, install the .deb files:

Code: Select all

sudo dpkg -i linux*.deb
When the .debs have been installed you may see some errors. I get these:

Code: Select all

Setting up linux-image-4.13.8-ryzen (4.13.8-ryzen-1) ...
Error! Bad return status for module build on kernel: 4.13.8-ryzen (x86_64)
Consult /var/lib/dkms/ndiswrapper/1.60/build/make.log for more information.
Error! Bad return status for module build on kernel: 4.13.8-ryzen (x86_64)
Consult /var/lib/dkms/virtualbox-guest/5.0.40/build/make.log for more information.
In the first error, ndiswrapper is complaining. For now, ignore it. We'll fix it later.

The virtualbox plumbing is complaining in the second error. This can be ignored as it will be fixed when you reboot.

Reboot into your shiny new kernel. To see it in a terminal:

Code: Select all

uname -a
The output will be similar to this:

Code: Select all

Linux Fenella 4.13.8-ryzen #1 SMP PREEMPT Sat Oct 21 10:13:25 AEDT 2017 x86_64 x86_64 x86_64 GNU/Linux
If you get the ndiswrapper complaint, issue this command to reinstall it:

Code: Select all

sudo apt install --reisntall ndiswrapper
All that's left is for you to fix anything else that may have broken. Congratulations! You've built your very own patched kernel.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Post Reply

Return to “Tutorials”