# KC7: Compiling directly into a kernel vs. modules

## wilburpan

Navigation:  [Kernel Compilation] [Table of Contents]

Compiling directly into a kernel vs. modules

You have two options for adding functionality to the kernel: building functions into the kernel (making a monolithic kernel) or adding them as modules.   

Monolithic kernels:

Building a function into the kernel directly ensures that that function will be available at all times.  The downside is that it makes your kernel bigger, increasing boot time, and ultimately using that much more memory to hold the kernel.  If you are compiling a kernel to fit on a floppy so that you can boot Linux off a rescue floppy, space will become an issue. 

Modules:

Building a function as a module allows that function to be loaded into memory as needed and unloaded when it is no longer needed.  This helps keep your kernel small.  It is very useful if, say, you are swapping hardware in and out of your system frequently.  You could compile support for a variety of, say, sound cards as modules, and your Linux system will theoretically only load the driver that is appropriate for the hardware setup at the time.  

Another benefit of building functions as modules is that parameters can be passed to the modules which can be useful in debugging your system if problems occur.

There are some considerations to be made when deciding if a kernel function should be modularized.  A small performance penalty is paid as it takes a little time to get the module loaded and unloaded.  There are some functions that are needed at boot time and these cannot be compiled as modules -- they need to be present in the kernel so your system can be loaded.  For example, ext2/ext3/reiserfs file system support needs to be built into the kernel so that your partitions can be read, as you need to be able to read the filesystem to load modules.  In my case, if I have PCMCIA support built into the kernel, then metworking works.  If PCMCIA support is modularized, networking fails to start, probably because the PCMCIA support needs to be available very early in the boot process to set up networking.

General approach

The way that I approached kernel building was to see which functions I would need all the time versus the ones that I would only use infrequently.  If it was a function that I would be using a lot, I built it directly into the kernel.  Otherwise, I compiled it as a module.  As I got more comfortable with recompiling the kernel, I started making more functions as modules, and honestly, have not seen that much change in system performance.  On the other hand, my hardware setup is pretty static -- my machine is a laptop, and the only hardware option that I have is whether or not I have a USB mouse plugged into it.

[Cribbed from a post I made to another thread]

[edited to incorporate suggested changes. Thanks!]Last edited by wilburpan on Thu Jun 12, 2003 4:04 pm; edited 2 times in total

----------

## pjp

Comments on this FAQ can be made in  [FAQF] KC7: Compiling directly into a kernel vs. modules

Thanks for the FAQ.

----------

