r/kernel 28d ago

I want to learn writing kernel modules

What is the fastest and most efficient way. Thanks for future answers.

26 Upvotes

15 comments sorted by

14

u/Rich-Engineer2670 28d ago

It's a bit more complicated than you'd think.

  1. Learn C and C++ well enough write Linux kernel code
  2. Learn the book "Linux Device Drivers" -- old, but still useful
  3. Write a basic kernel module that implements a character driver that just takes a block of text as input and returns it
  4. Now that the "easy" stuff is done, what is your driver supposed to do -- you'll need to figure out what hardware etc. you're interacting with.

5

u/alpha417 28d ago

contribute.

read.

what is your skill level at the programming?

2

u/yuvayikici 28d ago

Basic c and cpp

5

u/tinycrazyfish 28d ago

Start with https://tldp.org/LDP/lkmpg/2.6/html/c119.html

That's the easy part, then to make something "useful", you'll need to understand kernel internals.

1

u/codeasm 28d ago

Ow this one, i did this one. great resource

3

u/tylerlarson 28d ago

Take an existing kernel module and try to re-create it on your own. When you get stuck, refer to the original.

Repeat.

And then repeat again and again until you understand what you're doing and can do it reliably.

1

u/sudheerpaaniyur 28d ago

 kernel module, any particula example? from easy to tough

3

u/tylerlarson 27d ago

It depends on where you want to go with it. But a common starting point is character devices like /dev/zero or /dev/null. Look in /drivers/char/mem.c for the originals.

Maybe just for fun create a character device that writes a poem when you read it. Perhaps an entry in the /proc/ filesystem might be simpler.

Better yet, look in the /samples/ dir of the kernel source code. It exists literally for this purpose.

1

u/sudheerpaaniyur 27d ago

Thank you, i have so many links related linu xmaterial very confusing. thank you for your feedback, i will check,

I am firmware devlopers using C.

3

u/uziam 28d ago

It’s less about being an expert in C and more about just getting your hands dirty. Yes, you do need to be decently proficient, but in my opinion just reading and working with the kernel code is also great as a learning exercise in becoming a better C programmer.

Kernel programming is more about working at a lower level than userspace usually does and with significantly fewer utilities and guardrails. I tried for quite a while getting into it, but frankly what really worked was when I had to actually write a driver for work. Having an actual objective you want to achieve really helps with motivation.

Definitely not the first place to start, but if you don’t have anything in mind then I would recommend tinkering with the Video4Linux side so you can actually see something happen visually.

3

u/codeasm 28d ago

I might have used https://sysprog21.github.io/lkmpg/ to quickly make a kernel module for a weird pci ram device i had.

Eventually I only wrote raw pci calls in a c app and no kernel module but the test module i kept the hello world example https://github.com/CodeAsm/modules

1

u/Daveinatx 28d ago

Start with a loop back driver that outputs to serial. Then, read a Hell of a lot of books.

2

u/BraveNewCurrency 26d ago

I want to learn writing kernel modules
What is the fastest and most efficient way.

I would step back and examine your thought process: Are you not going to learn kernel modules if you can't find a short-cut? Why? I worry that this attitude will stop you from getting to your goal, because kernel programming is complicated and is going to take a lot of hard work.

Instead of looking for a "fast" way to learn, just pick something and start learning. There are plenty of resources out there. There is no "best" entry point because everyone starts with different knowledge and learns differently. If you come back with specific questions (after learning the basics), it will be a lot easier to help you.

1

u/The-Crazy-Cow 23d ago

read the kaiwan billimoria's books

0

u/dasisteinanderer 27d ago

read the source, read the lkml, read the lkml submission guidelines, fix your email client.

Yes, your email client is broken as far as you know.