Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu 16.04 - Compile Custom Kernel For Ryzen

In an effort to fix stability on Ryzen, this tutorial was produced to show how to build a custom version of the latest kernel with CONFIG_RCU_NOCB_CPU applied. This was made with a lot of help from First Last in the comments section of my Ryzen Stability post.

You probably also need to disable ASLR to achieve Ryzen stability.

Testing

I have tested that the steps below work for building and installing a custom kernel. I installed the kernel this morning (5th October 2017) and haven't had a crash since.

Steps

Install the necessary packages:

sudo apt install git build-essential kernel-package fakeroot libncurses5-dev libssl-dev ccache -y

This will go a lot faster if you have set up your own local mirror.

Create an area to work and clone the latest kernel there.

cd ~
mkdir kernelbuild
cd kernelbuild
git clone -b linux-4.13.y git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

The clone can take a while, so go make a cup of tea.

Go into the source code and lets create a basic/default config to build the kernel from:

cd linux-stable
cp /boot/config-`uname -r` .config
yes '' | make oldconfig

Tweaking The Kernel For Ryzen

Now this is the important part where we will tweak our configuration options to try and resolve issues on the Ryzen CPUs. If you just want to compile your kernel from source, and aren't using a Ryzen CPU, then feel free to skip this part. However the info may be useful if you want to tweak the kernel in other ways.

Run the following command to open a menu for changing our .config file:

make menuconfig

You will be shown the following screen:


Press / in order to perform a search, and and enter the text RCU_NOCB.


After running the search, you will see the screen below:


Press 1 to go to the option and you will see the following:

Press spacebar and you will then see:


Press spacebar again to allow advanced adjustments to RCU configuration.


Then go down to Offload RCU callback processing from boot-selected CPUs and press spacebar to [*] it.


Then press the right arrow several times to select Save and press enter to save over your .config file.


Then to exit out, you need to hover Exit and press return to exit. You have to do this multiple times until you are back at the normal terminal screen.

Build The Kernel

Now lets build the kernel. In this example I am going to use 16 threads because I have a Ryzen 7 1700 with 16 threads, If you have a Ryzen 3 with just 4 cores and no simultaneous multithreading, then you want to use a value of 4.

make clean
NUM_THREADS=16
make -j $NUM_THREADS deb-pkg LOCALVERSION=-custom

The second command will build against the appropriate number of threads for your machine. If you run into issues and need to debug, then you need to compile against a single thread with: make deb-pkg LOCALVERSION=-custom

Installing the Kernel

Now we've built the kernel, it's time to install it so that our system will use it when it boots up.

cd ..
sudo dpkg -i linux-firmware-image-4.13.4-custom_4.13.4-custom-1_amd64.deb
sudo dpkg -i linux-libc-dev_4.13.4-custom-1_amd64.deb
sudo dpkg -i linux-headers-4.13.4-custom_4.13.4-custom-1_amd64.deb
sudo dpkg -i linux-image-4.13.4-custom-dbg_4.13.4-custom-1_amd64.deb
sudo dpkg -i linux-image-4.13.4-custom_4.13.4-custom-1_amd64.deb

Now reboot to boot into your new kernel, but before you do, you may wish to update grub to set a longer timeout.

Just Download Somebody Elses Kernel

If you don't want to sit around and wait for a kernel to build, First Last has built a kernel and made it available online. You can download the relevant files here, but it's probably a lot more secure to build your own than trusting strangers.

Then run:

sudo dpkg -i linux-firmware-image-4.13.4-customrcu_4.13.4-customrcu-3_amd64.deb
sudo dpkg -i linux-libc-dev_4.13.4-customrcu-3_amd64.deb
sudo dpkg -i linux-headers-4.13.4-customrcu_4.13.4-customrcu-3_amd64.deb
sudo dpkg -i linux-image-4.13.4-customrcu-dbg_4.13.4-customrcu-3_amd64.deb
sudo dpkg -i linux-image-4.13.4-customrcu_4.13.4-customrcu-3_amd64.deb

Then reboot to boot into your new kernel.

Adding Kernel Boot Parameter

sudo vim /etc/default/grub

Find the line that states GRUB_CMDLINE_LINUX_DEFAULT= and add the contents: rcu_nocbs=0-15 like so:

...
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="rcu_nocbs=0-15"
GRUB_CMDLINE_LINUX=""
...

If you have a 6 core ryzen (12 thread) instead of 8/16, then you would need to use rcu_nocbs=0-11

Now apply that change by running:

sudo update-grub

Then reboot to boot into your kernel with the new options set.

sudo reboot

References

Last updated: 27th July 2023
First published: 16th August 2018