Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I write a Linux kernel module?

Some people want to move code from user space to kernel space in Linux for some reason. A lot of times the reason seems to be that the code should have particularly high priority or simply "kernel space is faster".

This seems strange to me. When should I consider writing a kernel module? Are there a set of criterias?

How can I motivate keeping code in user space that (I believe) belong there?

like image 552
Jordfräs Avatar asked Sep 29 '08 14:09

Jordfräs


People also ask

What is the purpose of a Linux kernel module?

Linux Kernel Modules. Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. A module can be configured as built-in or loadable.

What is the difference between kernel module and device driver?

a module is a "piece of software", of any kind. it is a part of the main kernel, not a "user program". a driver is a "piece of software" of one specific kind: it is needed to communicate with hardware components.

Can I write my own kernel?

If you are writing your own bootloader for loading a kernel you need to know the overall addressing/interrupts of memory as well as BIOS. Mostly each operating system has specific bootloader for it. There are lots of bootloaders available out there in online market.

What is the purpose of loadable kernel modules?

In computing, a loadable kernel module (LKM) is an object file that contains code to extend the running kernel, or so-called base kernel, of an operating system. LKMs are typically used to add support for new hardware (as device drivers) and/or filesystems, or for adding system calls.


2 Answers

Rule of thumb: try your absolute best to keep your code in user-space. If you don't think you can, spend as much time researching alternatives to kernel code as you would writing the code (ie: a long time), and then try again to implement it in user-space. If you still can't, research more to ensure you're making the right choice, then very cautiously move into the kernel. As others have said, there are very few circumstances that dictate writing kernel modules and debugging kernel code can be quite hellish, so steer clear at all costs.

As far as concrete conditions you should check for when considering writing kernel-mode code, here are a few: Does it need access to extremely low-level resources, such as interrupts? Is your code defining a new interface/driver for hardware that cannot be built on top of currently exported functionality? Does your code require access to data structures or primitives that are not exported out of kernel space? Are you writing something that will be primarily used by other kernel subsystems, such as a scheduler or VM system (even here it isn't entirely necessary that the subsystem be kernel-mode: Mach has strong support for user-mode virtual memory pagers, so it can definitely be done)?

like image 97
rpj Avatar answered Sep 20 '22 05:09

rpj


There are very limited reasons to put stuff into the kernel. If you're writing device drivers it's ok. Any standard application: never.

The drawbacks are huge. Debugging gets harder, errors become more frequent and hard to find. You might compromise security and stability. You might have to adapt to kernel changes more frequently. It becomes impossible to port to other UNIX OSs.

The closest I've ever come to the kernel was a custom filesystem (with mysql in the background) and even for that we used FUSE (where the U stands for userspace).

like image 23
Matthias Winkelmann Avatar answered Sep 22 '22 05:09

Matthias Winkelmann