Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MartyMacGyver/24be4a153fc5c02c84c1dec1c9835adb to your computer and use it in GitHub Desktop.
Save MartyMacGyver/24be4a153fc5c02c84c1dec1c9835adb to your computer and use it in GitHub Desktop.
Building the kernel for the Raspberry Pi/Pi2/Pi3 (with headers and optional memory leak debugging steps)
###############################################################################
# Compiling the RPi kernel in situ, with optional memory leak debugging
#
# Extends https://www.raspberrypi.org/documentation/linux/kernel/building.md
###############################################################################
# On the Raspberry Pi
cd ~/Projects
# Install rerequisites
sudo apt-get install bc
# Shallow clone of the repo, or update of current repo
git clone --depth=1 https://github.com/raspberrypi/linux
cd linux
# git pull --depth=1 # Update to current - didn't work right though - fetch instead?
# Target kernel name
# KERNEL=kernel # Pi1
KERNEL=kernel7 # Pi2,3
# Cleanup
# git clean -f # Really thorough!
make clean # May skip if just rebuilding with new options
# Initialize the config
#make bcmrpi_defconfig 2>&1 | tee ../make-config-pi1.log # Pi1
make bcm2709_defconfig 2>&1 | tee ../make-config-pi2-pi3.log # Pi2,3
cp -p .config ../.config.default # For reference
# Customize the config to configure kernel memory leak debugging:
# Either edit `.config` directly or...
make menuconfig # Menu driven, uses .config as a starting point
# Kernel memory leak debugging:
# Kernel hacking → Memory Debugging
CONFIG_SLUB_DEBUG_ON=y
CONFIG_SLUB_STATS=y
CONFIG_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=4096
CONFIG_DEBUG_KMEMLEAK_TEST=m
# Broadcom WiFi device debugging:
# Under Device Drivers → Network device support → Wireless LAN
CONFIG_BRCM_TRACING=y # Broadcom device tracing (maybe mutually exclusive to DBG below)
CONFIG_BRCMDBG=y # Broadcom driver debug functions
# Save your changes before exiting!
# Inspect the changes
diff ../.config.default .config
# Build and install the kernel, modules, and device tree blobs
make -j5 zImage modules dtbs 2>&1 | tee ../make-all.log
sudo make modules_install 2>&1 | tee ../make-modules_install.log
sudo make headers_install 2>&1 | tee ../make-headers_install.log
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo scripts/mkknlimg arch/arm/boot/zImage /boot/$KERNEL.img
# Sample output (note we've generated build "#3" here):
# Version: Linux version 4.4.10-v7+ (pi@mypi) (gcc version 4.9.2 (Raspbian 4.9.2-10) ) #3 SMP Wed May 18 22:21:34 PDT 2016
# DT: y
# DDT: y
# 270x: y
# 283x: n
# Reboot with the new kernel
sudo reboot
# See that the new kernel is active (note we are now running build "#3" which we created earlier)
uname -a
# Linux mypi 4.4.10-v7+ #3 SMP Wed May 18 22:21:34 PDT 2016 armv7l GNU/Linux
# Using kernel memory leak debugging
# sudo mount -t debugfs nodev /sys/kernel/debug # superfluous - already mounted
sudo bash -c "echo scan > /sys/kernel/debug/kmemleak"
sudo cat /sys/kernel/debug/kmemleak # Repeat to see updated info
# IF the `echo scan...` part above *fails*, check /var/log/kern.log
# What I saw: "Kernel memory leak detector disabled",
# "Early log buffer exceeded (####), please increase DEBUG_KMEMLEAK_EARLY_LOG_SIZE"
# I increased CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE and rebuilt/reinstalled the kernel
# Another handy command:
sudo slabtop -sc
@MartyMacGyver
Copy link
Author

I've learned that gist has a major old bug wherein you don't get notifications for comments to gists.

If you leave a comment, it would be appreciated if you ping me elsewhere so I know to find it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment