# Compiling Into the Kernel vs. Loadable Modules



## SecretAsianMan (Aug 16, 2010)

Many parts of the FreeBSD kernel can be either:
(A) included in the kernel itself; or,
(B) built as a module loadable at runtime.

**How** to configure either choice is a well-documented topic.  It is much more difficult to find guidance on which choice to make.  When should a thing be compiled into the kernel?  When should it be compiled as a module?  Are there any best practices to follow?

For instance, should I:
- Compile everything the machine might ever need into the kernel, and don't build any modules?
- Compile the machine's everyday needs into the kernel, and build modules for occasional needs?
- Compile as minimal kernel as possible, and load everything else from modules?

Should I prefer to load modules in loader.conf, or via kldload?

To what extent will the system automatically load modules?

(My RTFM has discovered that the boot loader will load the acpi module automatically.  The rc scripts will load pf, ipfw, and ipfilter automatically.  A few of the geom commands will load their respective module if not loaded.  As for other situations, it seems a manual process: the operator will need to invoke kldload prior to using the module's functionality.)

What's the direction FreeBSD is taking in the future with respect to this topic?


----------



## UNIXgod (Aug 16, 2010)

I usually keep my nic driver. most everything else is loaded unless specified that it has to be in the kernel. Never been much into giant monolithic kernels. Slimmer the better. But that's just me. I'm sure others have their thoughts about it.


----------



## aragon (Aug 17, 2010)

SecretAsianMan said:
			
		

> For instance, should I:
> - Compile everything the machine might ever need into the kernel, and don't build any modules?
> - Compile the machine's everyday needs into the kernel, and build modules for occasional needs?
> - Compile as minimal kernel as possible, and load everything else from modules?


Highly subjective.  I generally balance between option 1 and 2.  Option 3 can be a real pain if a crucial module can't be loaded for whatever reason.  Option 1 is nice for simplicity and in embedded environments.



			
				SecretAsianMan said:
			
		

> Should I prefer to load modules in loader.conf, or via kldload?


Depends if the module can be loaded after bootup and if you mind running kldload when you need it. 



			
				SecretAsianMan said:
			
		

> To what extent will the system automatically load modules?


Some daemons and scripts auto load modules (eg. ppp), and the kernel auto loads to satisfy module dependencies, but FreeBSD doesn't have all the autoloading magic that linux has, for example.


----------



## SecretAsianMan (Aug 17, 2010)

aragon said:
			
		

> I generally balance between option 1 and 2.  Option 3 can be a real pain if a crucial module can't be loaded for whatever reason.  Option 1 is nice for simplicity and in embedded environments.


That's sorta what I expected and hoped the answer would be.


----------



## nakal (Aug 17, 2010)

What do you mean by "invoking kldload manually"? All modules I need are loaded in _/boot/loader.conf_ (automatically, before starting the kernel).

That's why I generally prefer building GENERIC. There are a few rare cases where you need a custom kernel configuration (e.g. NFSv4, ALTQ, ...).

But as far as I understand, one should always prefer GENERIC and just load the modules on start. I also think, this is what the most people do. And besides that, GENERIC kernel is supported, custom kernels _are not_, if you run into very general trouble (of course, specific driver problems are always being solved by their developers).


----------



## jalla (Aug 17, 2010)

There's no good reason why you would stick to GENERIC. It's full of stuff you're never gonna need, like ISA-bus nics, lots of scsi and raid controllers, etc, etc. Generic has support for 30 different PCI nics built in. 

I'd say strip it down to the bare minimum and load modules for your specific needs.


----------



## Beastie (Aug 17, 2010)

jalla said:
			
		

> It's full of stuff you're never gonna need, like ISA-bus nics, lots of scsi and raid controllers, etc, etc. Generic has support for 30 different PCI nics built in.


True. But the GENERIC kernel file is only 10MB big and the entire system barely uses ~60MB of memory on a fresh startup.
So the question is: what would you gain from removing the extra drivers? A few hundred KB? 1 or 2 MB at best? How about speed? The kernel is read from disk and loaded within a single second, or at most 2. Most modern machines (e.g. PIII and up) are so fast you cannot even see the "loading text section", "loading data section", etc. messages.


----------



## User23 (Aug 17, 2010)

I thought a smaller kernel could increasing its efficiency under heavy syscall loads. Is that obsolete?


----------



## UNIXgod (Aug 17, 2010)

You compile your own kernel because :
1) You can and it's a rite of passage.
2) It's more secure.
3) Faster boot and better memory management. (more obvious with older hardware)

If you run GENERIC only and are not new to the UNIX world you need to reevaluate your administration ethic.

No one runs GENERIC on a production machine.


----------



## SecretAsianMan (Aug 17, 2010)

While I appreciate the last several responses, the question of whether or not to compile a custom kernel has nothing to do with this thread topic.

I'm using ALTQ, so a custom kernel is required.


----------



## rambetter (Aug 17, 2010)

I'm running the PAE kernel, so loadable modules are out of the question.


----------

