Ask HN: Looking for a resource on Linux kernel module development
I'd like to break into this area in my next role and I'm looking for resources to learn about this over the next few weeks so I can try to get a role in the area. My context is embedded linux.
EDIT: I don’t know what I don’t know so I’m looking for a “point in the right direction” and anything the fine people of HN think is a good resource
45 comments
[ 2.5 ms ] story [ 136 ms ] threadhttps://man7.org/tlpi/ This book is considered the best overview of linux kernel interface. Its old, but still gives a broad coverage of topics.
https://lkml.org/ Browsing LKML is really helpful since it shows you where current development is happening and how kernel devs think. Some conversations get very advanced and nuanced.
https://kernelnewbies.org/ is a good starting place for starting to compile and deploying kernel builds
Other than that, reading and tracing the source is probably the most productive thing to do for an experienced dev to do
Have you already gotten started with your LLM of choice, and need something more?
Don’t forget "Embedded Linux Primer" by Christopher Hallinan.
in fact, most of linux kernel books are aged these days.
https://sysprog21.github.io/lkmpg/ https://linux-kernel-labs.github.io/refs/heads/master/so2/in...
Drivers:
https://www.amazon.com/Linux-Device-Drivers-Development-cust... https://www.oreilly.com/library/view/linux-device-drivers/05...
https://www.packtpub.com/authors/kaiwan-n-billimoria
I’m halfway into their first Linux Kernel Programming book and like it so far.
Linux kernel and its insides (https://0xax.gitbooks.io/linux-insides/content/index.html)
Designing BSD Rootkits
https://nostarch.com/rootkits.htm
FreeBSD Device Drivers
https://nostarch.com/bsddrivers.htm
Playing Games With Kernel Memory ... FreeBSD Style
http://phrack.org/issues/63/7.html#article
Although it's based on 3.x kernel, it still is pretty relevant.
Good Luck!
Also get a random raspberry pi and a simple peripheral for it: some 1 wire temperature sensor or something on i2c.
Then ignore the existing drivers and do your own. Do a character device and have a read from it trigger a read in kernel and copying of the data to user space, maybe in human readable form.
Should be good practice.
And for general embedded Linux development you also want to take a look at Yocto.
However, I found great joy in using Nix for more modern development t of higher-end embedded devices.
If your target platform is powerful enough to accommodate NixOS, you might want to give it a try.
But what's the extra effort if your hw vendor doesn't support it? The examples on their homepage look more like cloud computing not embedded.
But if your platform can run NixOS ... then ... let's see...
With nix I have a file that exactly describes the git sources (pinnable on precise changesets if required) for all my components (including custom kernel patches, device drivers, etc).
I use a nix config to create a fully GPLV3 compliant rootfs and another set of nix config files in a separate repo to create our device specific intellectual property, in a separate encrypted image different from the rootfs.
The nix config files easily describes everything i need to fully support Secure boot, as well as code to unseal a TPM luks password used to decrypt the image that contains our intellectual property for that device (basically the application).
All this is fully reproducible, so I can provide our clients with the nix config files for our rootfs, tell them to use nix and they can reproduce our exact rootfs image to run in the device , for GPLv3 compliance. Of course , then the device is "broken" , because our IP will not be decryptBle anymore. But the GpLv3 does not mandate full functionality when running foreign images...
Now I am absolutely certain all this can be also done with Yocto... but with less simplicity ... less joy ... :)
I used pSOS in the past, got burned when Integrated Systems was sold... then stayed away from proprietary OSes as much as I could. Used Debian with XEN later on... then Yocto in many products, then back to a Debian-like OS and now , finally, using Nix. It just works.
I thought v3 was specifically made to prevent tivoization? And that sounds like you even used the same implementation as TiVo.
Our IP is obviously not covered by the GPLv3.
What is the problem?
If you work long enough in embedded Linux you will encounter it for sure.
I would advise against starting with yocto for a beginner. It is very complex, most tasks look more like weird magical incantations than actual code. It will discourage many, as things will feel completely incomprehensible for months.
You kind of have to know the problems it solves to appreciate it.
In another comment I pointed to OpenWRT and Raspbian. They have real world devices they target, you can git clone, build and flash real devices to get you started and see real results faster. Their build systems do not have all the complexity of layers and are easier to follow.
If you want to try out VFIO, you could try writing a userspace program that interfaces with some simple PCI device, like a serial card.
Edit: A cursory glance suggests this is for connecting existing devices to virtualized guests, how does that replace kernel modules for hardware components?
DPDK [1] is an example of a non-VM VFIO user.
[1] https://doc.dpdk.org/guides/linux_gsg/linux_drivers.html
VFIO is still mostly about virtual machines, as a beginner I won't touch it for now.
I will stay in the kernel for a while before doing userspace drivers.
More than half the value was in sending mails and getting feedback, but it's still a great introduction that gradually gets you used to more and more complicated kernel dev
There are solutions lying around on the internet for the various tasks, but I'd advise not looking. The kernel is sparsely documented (some areas, rigorously undocumented), so a bit of experience searching and figuring out how to get the information on your own is genuinely valuable
Both systems have code bases and build systems that will build root filesystem and kernels for a wide range of devices.
By following development of a device (aka target) that interests you, you will see how drivers are stabilized with various patches applied over time.
QEMU is nice for learning because you can more easily debug kernel code with gdb.
If also choose some class of device and get to know it. Many Linux drivers are very similar across a class of devices. For example CSI cameras on top of V4L. Thankfully the code in the Linux kernel is very readable. Also it is well documented online.
I definitely recommend a good ide too.
978-0672329463
- Still trying to find the How To Build a Formally Verified Kernel Module from Zero tut
Edit:
- /? Kernel module https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que...
- "Linux Kernel Module written in Scratch (a visual programming language for kids)" (2022) https://news.ycombinator.com/item?id=31921996 (EduBlocks does Scratch with Python blocks and/or text code)
- "How to Discover and Prevent Linux Kernel Zero-day Exploit using Formal Verification" (2021) [w/ Coq] http://digamma.ai/blog/discover-prevent-linux-kernel-zero-da... https://news.ycombinator.com/item?id=31617335