Yet another guide on the way to linux kernel hacking

Hacking goals

Trade time for space


define _ASM_EXTABLE_HANDLE(from, to, handler)

    .pushsection "__ex_table","a" ;                         \
    .balign 4 ;                                             \
    .long (from) - . ;                                      \
    .long (to) - . ;                                        \
    .long (handler) - . ;                                   \

Hacking area

Variable-length arrays and the max() mess
Sub-system Update: Kernel Self-Protection Project - Kees Cook, Google

Kernel refactoring
Kernel Recipes 2017 - Refactoring the Linux Kernel - Thomas Gleixner


some results or progress information for PFRA

Write your own kernel

Writing an OS in Rust
BrokenThorn Entertainment Operating System Development Series


  • if no .config, every config tools make a .config from scrach!
  • oldconfig just for new moduels patch merged in trunk. This no any relations to .config.old and /boot/config.x.y.z
  • make localmodconfig will reduce many unused kernel config.
  • make bzImage #kmods will not build that configured with M!
  • make modules_install INSTALL_MOD_PATH=/home/firo/kmods
    ##build signle kernel module
    make menuconfig
    make oldconfig && make prepare
    make -C $(pwd) M=/home/firo/linux/fs/ext3 modules V=1
    make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnu- drivers/pcmcia/sa11xx_base.o

Git repos

Git tree maintenance

next tree - 2.6.1x ~ latest tree; please rebase your patch against this tree before send it to upstream

Working with linux-next
git add git://

tglx tree - 2.4 - 2.6.1x

origin git:// (fetch)
origin git:// (push)

history tree - 0 - 2.4

origin (fetch)
origin (push)
git checkout -b 240p 2.4.0-prerelease



Unreliable Guide To Hacking The Linux Kernel
Bootlin linux kernel labs

Mailing list archives

The Linux Kernel Archives
LKML archives on

Source code navigator

Just make tags; make cscope
‘Source code reading’ related sites


Building a KVM host machine.
Speeding up kernel development with QEMU
How to Build A Custom Linux Kernel For Qemu
Rapid kernel development with dracut and Qemu


zypper –root /home/firo/ws/suse addrepo foss
zypper –root /home/firo/ws/suse install kernel-default-devel

Build minimal bootable rootfs - fedora

can be gotten by supermin –prepare bash -o /tmp/supermin.d
or just copy from you host to some place like /home/firo/kernel/k/testfs/
sudo dnf –releasever=27 –installroot=/home/firo/kernel/k/testfs/ –setopt=reposdir=/home/firo/kernel/k/testfs/etc/yum.repos.d install dnf udev passwd

How to build a minimal kernel for testing?

[tiny config @][5]
[3 attempts to reduce the configurations][6]
Fedora equivalent of debootstrap
Then enable following config option
Some ftrace stuff
CONFIG_KASAN Build a tiny kernel

make initrd for NFS

Upgrade kernel and initrd in Linux
sudo chroot suse mkinitrd -m ‘nfs nfsv3 nfsv4 iwlwifi’ -A -D wlp4s0


qemu-system-x86_64 -nographic -enable-kvm -kernel ./bzImag -append ‘ console=ttyS0 ip=dhcp root=/dev/nfs nfsroot=,nfsvers=3,tcp rw nfsrootdebug debug raid=noautodetect selinux=0 enforcing=0 ‘
dnf install nfs-utils
cat /etc/exports # For more details, man exports
systemctl start nfs-server.service
systemctl status nfs-server.service
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Active: active (exited) since Sat 2018-03-17 17:52:29 CST; 4s ago

Test the nfs

sudo mount -t nfs localhost://home/firo/kernel/k/testfs /mnt
if touch prermission denied, add no_all_squash to/etc/exports

NFS errors

[ 54.600121] NFS: sending MNT request for
[ 54.600121] NFS: failed to create MNT RPC client, status=-101
[ 54.600121] NFS: unable to mount server, error -101
try: nfsvers=3,tcp and or are mandatory!

NFS vers

rpcinfo -t localhost nfs
program 100003 version 3 ready and waiting
program 100003 version 4 ready and waiting
rpcinfo -p | grep nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl

Submitting patch

Patch prefix

git log --oneline  path/to/file.c

Resending the patch

First revision, version, second revision
Versioning one patch revision
For example, if you’re sending the second revision of a patch, you should use [PATCH v2]
use PATCHv2 (or PATCHv3 and so on) in the subject lines instead of PATCH … To update the subject lines, add the -v 2 (or -v 3, etc) options to git format-patch
Finally, to send your new patch series as a reply to the previous one, first look up the Message-Id of the cover letter (or the one-and-only patch) in your previous patch series, and then pass that to the –in-reply-to= option of either git format-patch or git send-email.
Patch v2, v3, … Changes, tags

Patch in series with a cover

proxychains git send-email --subject "[PATCH v2 0/15] Remove unneeded casts of memory-alloc function return values" --thread --compose --confirm=compose --to *.patch

Dave S Miller’s perferences

@@ -325,13 +325,15 @@ static inline void empty_child_dec(struct key_vector *n)
static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
- struct tnode *kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
- struct key_vector *l = kv->kv;
+ struct tnode *kv;
+ struct key_vector *l;
Dave Miller usually prefers it if variables are ordered from longest to shortest. So you should probably have l defined first, and then kv.

Julia Lawall Sorry to be picky,

but normally people put a space after the colon. Also,
the subject line could be shorter: Remove unneeded cast.
The description part of the subject doesnt have to be unique,
just the whole thing, asfter the [PATCH] part.

Dan Carpenter

Otherwise your patch was fine, btw. Other improvements.
Don’t put “Drivers:” in the subject.
On Wed, Apr 22, 2015 at 09:10:50PM +0800, Firo Yang wrote:
> From: Firo Yang
Don’t include this line. We can get it from you email address.
Include everyone from the ./scripts/ output except
don’t include if there is another mailing
list there already.


│17:31:57 mkubecek | For the record, once the commit is in net or net-next tree, there is no chance to tweak its commit message. │ vtsironis_ho
│17:32:26 alesak | mkubecek: ok, thanks for the clarification │ wpreston
│17:33:19 mkubecek | These trees do not rebase and some people (like me) would appreciate if other subsystem trees didn’t │ Zara
│ | either. │ zuzka
│17:34:20 mkubecek | We could avoid spurious git_sort failures and commits like kernel-source 174731527683

git mutt patch

Greg: How to Apply a Patch to the Linux Kernel Stable Tree

newbies! what to do?

Peter Zijlstra: From DOS to kernel hacking


    make CHECK="~/path/to/smatch/smatch -p=kernel" C=1 \
            bzImage modules | tee warns.txt