Through The Looking Glass
Are you looking for a present to get for your favorite developer? Davyd Madeley reviews Robert Love’s Linux Kernel Development, 2nd Ed, and finds an interesting use besides the author’s suggested ones.
I am not an operating systems designer; I am an electronic engineering student. I have done a unit on operating systems, but I enjoyed it less than I enjoyed the Pink Shirt Book (you know that one by Peter Norton?). I can program, but I do not class myself as a programmer. All the same, Robert let me read, then write about, the new edition of his book, Linux Kernel Development.
Linux Kernel Development is incredibly comprehensive. Robert starts the reader off gently with an introduction to what Linux is, and how to download it, build it and handle kernel patches. This is the bread and butter of any development with the kernel. From then on we are dazzled as we are shown how Linux implements processes and the scheduler. System calls, interrupt handlers, bottom halves, synchronisation, locking, timers, memory, virtual file systems, block I/O, the list goes on. If there is something you wanted to know about the kernel, it’s covered somewhere.
Most of it was simple enough for even me to understand, yet detailed enough that I think I could actually implement a driver if I wanted. The book includes tables of functions with extensive comments on what they do. However, the book is not an API reference. The would-be kernel developer is given insight into designs in the kernel and the history of previous kernel design. Like touring a museum of old kernel designs, we are told about Linus Elevators, the antique BH interface, and many other designs now replaced in the Linux kernel. This history of the kernel is novel in that it allows new developers to understand why some things are the way they are, what ideas were tried in the past, what turned out to be not such a good idea, and why it was bad. You come to realise that the design of the kernel is very much an experiment in applying the technologies and techniques of the computer scientist. Robert is not afraid to say when these techniques were applied poorly.
Even though each component of the kernel is covered in extensive detail, some parts of it can still be quite daunting. Robert recommends you read the book in conjunction with a copy of the kernel sources as a reference. Some sections of the kernel, like the virtual file system layer, can be quite confusing to understand. By the time you have finished reading pages and pages of detailed C structures, you can’t really recall how they all fit together in the first place. This can also be a problem when more complicated and subtle algorithms and data structures are employed.
While trying to explain the concepts simply and succinctly, Robert still encourages new developers to simply roll up their sleeves, fire up their favourite source editor and get their hands dirty, so to speak.
In addition to those who wanted to get started with kernel development, the book also acts as an excellent reference for anyone who actually has no interest in writing code for the Linux kernel. The information we learn about the implementation of scheduling process management and system calls helps the user space-oriented developer to better understand the core of the systems they are dealing with. I have found that understanding how the Linux kernel works gives the me an insight into understand the entire operating system and why sometimes one implementation is non-obviously better than another. The book refers to both UNIX history and UNIX idioms in an effort to educate the reader in UNIX-like operating system principles as well as be entertaining.
System administrators will find the book an invaluable resource for getting detailed information on the various processor and disk scheduling algorithms available in the kernel as well as how to optimise other features of the Linux kernel.
Not too many people want to hack on the kernel core, and instead usually have an itch to scratch with regards to support for new types of hardware. Linux Kernel Development will be of particular interest to people looking to develop a module or driver for the kernel. We are told what is required to initialise and deinitialise a driver, how to configure it to be built with the rest of the kernel, using kobjects to interface with the common driver interface that was a feature of Linux 2.6 and using sysfs to set hardware properties useful for user space applications. All of this is made easier through additional tips and hints with regards to portability and debugging. Unfortunately there is not enough space in the book to cover every single driver subsystem (such as USB, ALSA, and Network) available in the Linux kernel. Of course, each of these subsystems is complex enough, with its own set of tips and hints, that it would warrant its own book.
There is a saying that any piece of code is three times harder to debug than it was to develop. To help out in this regard, Robert shares with us some of his personal favourite techniques for debugging the kernel, both using the ‘printk’ school of debugging and using a variety of debugging tools such as gdb and kdb. Robert gives examples of subtle tricks that can make the new kernel developer’s life a lot easier, like rate limiting debugging messages, generating back traces for parts of the kernel and exposing information through the /proc and /sys interfaces.
Robert Love’s charming and witty style is prevalent throughout the book. As a more senior developer, and developer of a number of important kernel improvements such as the preemptive kernel space and inotify file change notification interface, he’s not afraid to poke fun at the kernel when appropriate, especially at the sometimes non-sensical nomenclature. New developers realise that sometimes function names don’t make sense even to those who wrote them.
While the first edition of this book was still excellent, you might consider getting the more recent version if you want updated information on the Linux 2.6 kernel. The new edition also includes more information on kernel modules, as well as virtual memory, using the kernel memory allocation functions, getting involved in the Linux kernel community, more debugging tips and lots of new examples.
I recently met up with Robert at a conference in Australia. He was quite modest about his book when I told him how good I found it. A technical bookstore that had set up at the conference sold out of copies of the book within days. In addition to Robert’s suggested uses for the book (lighting fires and beating your cat) I think that Linux Kernel Development is an excellent resource for the would-be or existing kernel developer, operating system enthusiast or student. I think it has a place on every technical shelf and on the book list for any UNIX operating systems course. If asked to give a rating, I would say about 9 out of 10.