TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Write your first Linux kernel module

287 pointsby benevalmost 11 years ago

10 comments

jvnsalmost 11 years ago
This is so great!<p>Writing toy kernel modules on your own is a super fun way to get into kernel programming. There&#x27;s no danger of anything going wrong, especially if you develop inside a VM.<p>My partner wrote a kernel module for a talk I gave that rickrolls your file system -- it overrides the &#x27;open&#x27; system call so that any time you open a &#x27;.mp3&#x27; file, it plays Rick Astley instead. (this requires you to have Rick Astley already on your computer =))<p>The source code for that (and a few other toy kernel modules) is at <a href="https://github.com/jvns/kernel-module-fun" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jvns&#x2F;kernel-module-fun</a><p>The rickroll module (by Kamal Marhubi) is the most well commented and fun: <a href="https://github.com/jvns/kernel-module-fun/blob/master/rickroll.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jvns&#x2F;kernel-module-fun&#x2F;blob&#x2F;master&#x2F;rickro...</a><p>I wrote a blog post with more about this kernel-programming-for-fun philosophy: <a href="http://jvns.ca/blog/2014/01/04/4-paths-to-being-a-kernel-hacker/" rel="nofollow">http:&#x2F;&#x2F;jvns.ca&#x2F;blog&#x2F;2014&#x2F;01&#x2F;04&#x2F;4-paths-to-being-a-kernel-hac...</a><p>Of course, writing code for the <i>mainline kernel</i> is a responsible task, but writing kernel code on your own machine that nobody will ever run in production is a great way to learn, and I think it&#x27;s made me a better programmer.<p>A few concrete ways I think it&#x27;s made me a better programmer:<p>- I now understand the interface between kernel space and userspace much better (I use strace all the time to debug my regular userspace code!)<p>- realizing that the Linux kernel is just written in C and it&#x27;s something I can read (and understand! and change!) was a big deal for me<p>- understanding the basics of the underlying operating system helps me reason about my programs better<p>- I now feel like if I ran into an <i>actual</i> kernel bug or performance problem, it&#x27;s something I could conceivably attempt to fix and understand, instead of hiding under the bed and saying OH NO THE KERNEL IT IS TOO HARD
deutroniumalmost 11 years ago
This is my first kernel module, which &#x27;fakes&#x27; your system uptime:<p><a href="http://www.anfractuosity.com/projects/uptime/" rel="nofollow">http:&#x2F;&#x2F;www.anfractuosity.com&#x2F;projects&#x2F;uptime&#x2F;</a><p>Personally I think people should be encouraged to hack around with the kernel, rather than be afraid of it, how else can we learn?
pdevralmost 11 years ago
This is a good starting point for anyone interested in the Eudyptula Challenge [1].<p>[1] <a href="http://eudyptula-challenge.org/" rel="nofollow">http:&#x2F;&#x2F;eudyptula-challenge.org&#x2F;</a>
评论 #7821846 未加载
chrisBobalmost 11 years ago
Do most kernel programmers start out working on their regular daily-use machine? My only experience is with an embedded linux environment, and I would probably recommend starting with something like a RPi so that your environment is easy to restore if you mess something up.<p>I also recommend &quot;Linux Device Drivers&quot; from O&#x27;Reilly. <a href="http://lwn.net/Kernel/LDD3/" rel="nofollow">http:&#x2F;&#x2F;lwn.net&#x2F;Kernel&#x2F;LDD3&#x2F;</a>
评论 #7821525 未加载
评论 #7821447 未加载
评论 #7822954 未加载
评论 #7821530 未加载
jhieseyalmost 11 years ago
I might be missing something, but the final version still looks susceptible to deadlock to me.<p>Suppose the buffer is empty. Process A, which is reading, gets through the mutex_unlock() call and into wait_event_interruptible(), which seems to first internally check the condition. It is then switched out before it internally waits.<p>Process B then runs all the way through, acquiring the mutex, doing its work, and runs wake_up_interruptible(), which won&#x27;t do anything, because process A hasn&#x27;t started waiting. Process B then releases the mutex and gets switched out.<p>Process A then gets switched back in, and then immediately starts waiting, with no more events to wake it up. Why won&#x27;t it just wait forever (deadlock)?<p>Am I missing something? Do all processes occasionally get woken up without an explicit wake up?
Aardwolfalmost 11 years ago
VirtualBox uses a kernel module, and that is annoying because the linux kernel API changes with every update, which means its kernel module needs to be recompiled. Since I update my Linux quite often, that basically means:<p>Everytime some friends call me to play a certain game online with each other, they need to be waiting a bit longer for me because I have yet again to recompile VirtualBox&#x27;s kernel modules.<p>Is there a way to convert something that requires kernel modules, to run fully in userspace?<p>I&#x27;d be happy to lose some efficiency for this. Just a 100% reliable game to run my game would be perfect.<p>Are there things you can do only in kernel modules and not in userspace, and does VB need this? In theory userspace can do everything you need, right? Graphics, networking, audio, ...
评论 #7823282 未加载
评论 #7824748 未加载
评论 #7824032 未加载
benjojo12almost 11 years ago
I had great fun writing a silly toy kernel module a few weeks back, ( <a href="https://github.com/benjojo/dev_markov" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;benjojo&#x2F;dev_markov</a> ) Would totally recommend anyone curious giving it a go.
Nursiealmost 11 years ago
Cool, definitely relevant to my interests. I&#x27;ve done various ARM&#x2F;orion5x platform porting related stuff before but never a module...
Myrmornisalmost 11 years ago
Really great article.
thegeomasteralmost 11 years ago
Maybe it&#x27;s worth emphasizing that writing kernel code is a very responsible task, not to be taken lightly. Hackers coming from a userspace background tend to think it&#x27;s just &quot;userspace with a few quirks&quot; which is grossly wrong: your code has to be <i>completely free of bugs</i>, as a dangling pointer, for instance, won&#x27;t just send you a friendly SIGSEGV and terminate your ass, it will crash the whole system, leading to consequences for everyone who relies on its stability. Just imagine introducing a bug which crashes a Linux-based life support system. You could be responsible for endangering human lives. (I ageee that Linux isn&#x27;t a sane choice here, but just for the sake of the argument.)<p>Also, standards to get your pull requests merged into mainline Linux are insanely high, but even given that, you can never rely solely on maintainers&#x27; code review to catch your mistakes. Take kernel hacking seriously.
评论 #7821287 未加载
评论 #7822138 未加载
评论 #7821335 未加载
评论 #7823378 未加载
评论 #7821604 未加载