The Book You Need to Get Started With Linux Kernel Development
I had always scoured the internet searching for a book that was in equivalence to books like the ‘Linux Kernel Development’ by Robert Love and ‘Understanding the Linux Kernel’ by Marco Cesati.
Both are amazing books, but are now sadly outdated by the fact that the Linux kernel’s codebase moves fast.
I have had the fortune of receiving the second edition of Linux Kernel Programming early, while also owning my own copy of the first edition. Let’s get to the review!
If you find me sympathising with the author, it is because I understand that the Linux kernel is an unfathomably large project and has complexities unheard of.
Linux Kernel Programming (Second Edition)
As the title mentions, this review is for the second edition of Linux Kernel Programming book, written by Kaiwan N. Billimoria. In this section, I will give you an overview of the second edition. The primary difference between the first and second edition is the upgrade from the 5.4 to the 6.1 version of the Linux kernel.
This is a hands-on book and as such, you are expected to perform the exercises. That is how you will learn. All the necessary code is provided in this GitHub repository. Fork at will!
The book is available on Amazon and Packt website.
This book comes with assignments that you can solve to challenge your understanding and revisit topics that you find tough.
It starts with a gentle introduction to the Linux kernel, its source code and origins. It has 13 chapters in total, each chapter covering more detail than the previous one and filling the gaps about questions you might have had.
While I won’t copy-paste the book’s index here, I will lay out the general flow that the author takes, to introduce you to the Linux kernel.
Basic introduction to the Linux kernel
To introduce you to the Linux kernel, the author covers the following topics:
- Creating a dedicated virtual machine for Linux Kernel development and setting up the environment. You wouldn’t want a bad kernel or kernel module crashing your main PC now, would you?
- Getting familiar with the Linux version nomenclature, including all the various trees that exist.
- Creating a simple custom menu entry in the Linux Kernel
Kconfig
(kernel configuration) file. - Building the Linux kernel and installing it. This is done for native x86_64 targets as well as cross compilation for the 64-bit ARM targets like the Raspberry Pi 4.
- Understanding concepts like the kernel image, kernel modules. Also learning about initramfs and even how to manually generate the initramfs for your particular kernel.
As expected, the author does a good job of explaining these concepts to someone who might be new to kernel programming. Some topics like the Linux kernel modules are specific only to the Linux kernel. He covers them in a satisfactory manner, building up for the next chapter.
Writing code for the Linux kernel
Once you have a brief idea about the Linux kernel source tree and build process, the author then dives into writing code for the Linux kernel. You are highly encouraged to follow along on your physical/virtual machine in building and running this “out-of-tree” code.
To get you writing code for the Linux kernel, the author starts by making you familiar with the Linux kernel’s module system. Topics like the following are covered:
- Understanding all the elements of writing a simple Linux kernel module. “Equivalents” of userspace programming conventions like the
main()
function in the kernel code (init()
andexit()
functions). - Building these kernel modules. Loading and unloading them. Where to see their output.
- Experiments and approaches when you have multiple kernel modules and source files. How to make one source file “depend” on another?
I will repeat this again. Please follow along with the code examples. They are for you to understand and are quite nice in explaining each topic at hand.
Linux kernel internals: overview
As the next step, the author then gives you an overview of Linux kernel internals. Foundational blocks that a kernel developer must know, like the following, are covered:
- Processes and threads. Diving into the
task_struct
data structure of the Linux for tracking the processes. - Internals of the Linux kernel memory management.
- Memory allocation for kernel module(s).
- Creating a custom slab cache.
- Kernel’s “internal” APIs like
kmalloc()
,kzalloc()
,kfree()
,vmalloc()
etc.
The author also goes above and beyond (in my opinion) by covering a necessary, but often ignored feature: KASLR (Kernel Address Space Layout Randomisation).
This section is finally concluded by learning about the Linux kernel out-of-memory (OOM) killer.
Linux kernel internals: scheduler
Once the topics that you might have encountered in the Operating Systems class are covered, the author then covers the final, most important task that all the kernels of every operating system in existence covers: the task scheduler.
Topics like the following are covered in this section:
- KSE and its purpose in scheduling tasks in the Linux kernel.
- CFS task scheduler that is part of the Linux kernel’s v6.1.y release, that this book targets.
- Kernel synchronisation and techniques: spinlocks, locking, etc.
- Understanding Cgroups (v2).
By covering a majority of aspects of the Linux kernel’s scheduler, the author concludes the end of this book.
Things I liked in the book
This book comes with a strong recommendation from me for being an excellent resource that is up-to-date with the Linux kernel’s source code.
Following are some aspects of this book that I personally liked:
- Hands-on approach: This book focuses a lot of code and a majority of the text (non-code) in this book is mostly about explaining the code, what it does, what can be done instead and some other ideas for you to explore.
- Assignments: This is in addition to the first point about a hands-on approach. The author has some fun assignments that you should at least try!
- In-depth explanation: Almost all of the topics covered have good, in-depth explanation. Those that don’t, encourage you to explore further.
- Highly technical content: This book focuses very little on generic OS fundamentals–unlike your typical CS course book on OS. Instead, it dives straight into how stuff works in the Linux kernel. (I personally prefer this over generic fundamentals because you apply on a known base (Linux) and see the results immediately.)
- Software licensing: While many software developers don’t have to bother with a software license, it is important to know about software licensing as a kernel developer due to various restrictions (discussed in the book). This is a tiny, but important topic.
Things I wish the book had covered
While this is a great resource, nothing is perfect and as such, I have a teeny tiny list of things that I would have loved to see included.
- Coverage of VFS, for a better understanding of file-system included in the Linux kernel codebase.
- A tiny section on driver development. Letting readers branch out other than “core” kernel development.
- Inclusion of some Rust code, but that would only come with a later kernel version, so I’m willing to let go of this.
Who knows? If you like it and end up purchasing this book, the author might be incentivised for a third edition! 😉
Conclusion
The second edition of the ‘Linux Kernel Programming’ book, written by Kaiwan N. Billimoria is an excellent resource for someone seeking a book that helps you get started with Linux kernel programming. It has a lot of hand-holding and rightfully so because the Linux kernel is a huge codebase.
I know that many people these days prefer online and video content for programming and other tech related learnings but nothing beats the handiness of a good book. Not only you learn at your pace, revisiting a topic is much easier with books than videos. This is coming from someone who is in early 20s, so that says a lot.